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
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
|
/* Optimize by combining instructions for GNU compiler.
Copyright (C) 1987, 1988 Free Software Foundation, Inc.
This file is part of GNU CC.
GNU CC 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 1, or (at your option)
any later version.
GNU CC 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 GNU CC; see the file COPYING. If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
/* This module is essentially the "combiner" phase of the U. of Arizona
Portable Optimizer, but redone to work on our list-structured
representation for RTL instead of their string representation.
The LOG_LINKS of each insn identify the most recent assignment
to each REG used in the insn. It is a list of previous insns,
each of which contains a SET for a REG that is used in this insn
and not used or set in between. LOG_LINKs never cross basic blocks.
They were set up by the preceding pass (lifetime analysis).
We try to combine each pair of insns joined by a logical link.
We also try to combine triples of insns A, B and C when
C has a link back to B and B has a link back to A.
LOG_LINKS does not have links for use of the CC0. They don't
need to, because the insn that sets the CC0 is always immediately
before the insn that tests it. So we always regard a branch
insn as having a logical link to the preceding insn.
We check (with use_crosses_set_p) to avoid combining in such a way
as to move a computation to a place where its value would be different.
Combination is done by mathematically substituting the previous
insn(s) values for the regs they set into the expressions in
the later insns that refer to these regs. If the result is a valid insn
for our target machine, according to the machine description,
we install it, delete the earlier insns, and update the data flow
information (LOG_LINKS and REG_NOTES) for what we did.
To simplify substitution, we combine only when the earlier insn(s)
consist of only a single assignment. To simplify updating afterward,
we never combine when a subroutine call appears in the middle.
Since we do not represent assignments to CC0 explicitly except when that
is all an insn does, there is no LOG_LINKS entry in an insn that uses
the condition code for the insn that set the condition code.
Fortunately, these two insns must be consecutive.
Therefore, every JUMP_INSN is taken to have an implicit logical link
to the preceding insn. This is not quite right, since non-jumps can
also use the condition code; but in practice such insns would not
combine anyway. */
#include <stdio.h>
#include "config.h"
#include "rtl.h"
#include "flags.h"
#include "regs.h"
#include "basic-block.h"
#include "insn-config.h"
#include "recog.h"
#define max(A,B) ((A) > (B) ? (A) : (B))
#define min(A,B) ((A) < (B) ? (A) : (B))
/* It is not safe to use ordinary gen_lowpart in combine.
Use gen_lowpart_for_combine instead. See comments there. */
#define gen_lowpart dont_use_gen_lowpart_you_dummy
/* Number of attempts to combine instructions in this function. */
static int combine_attempts;
static int distrib_attempts;
/* Number of attempts that got as far as substitution in this function. */
static int combine_merges;
static int distrib_merges_1, distrib_merges_2;
/* Number of instructions combined with added SETs in this function. */
static int combine_extras;
/* Number of instructions combined in this function. */
static int combine_successes;
static int distrib_successes;
/* Totals over entire compilation. */
static int total_attempts, total_merges, total_extras, total_successes;
static int total_distrib_attempts, total_distrib_merges_1, total_distrib_merges_2, total_distrib_successes;
/* Vector mapping INSN_UIDs to cuids.
The cuids are like uids but increase monononically always.
Combine always uses cuids so that it can compare them.
But actually renumbering the uids, which we used to do,
proves to be a bad idea because it makes it hard to compare
the dumps produced by earlier passes with those from later passes. */
static int *uid_cuid;
/* Get the cuid of an insn. */
#define INSN_CUID(INSN) (uid_cuid[INSN_UID (INSN)])
/* Record last point of death of (hard or pseudo) register n. */
static rtx *reg_last_death;
/* Record last point of modification of (hard or pseudo) register n. */
static rtx *reg_last_set;
/* Record the cuid of the last insn that invalidated memory
(anything that writes memory, and subroutine calls). */
static int mem_last_set;
/* Record the cuid of the last CALL_INSN
so we can tell whether a potential combination crosses any calls. */
static int last_call_cuid;
/* When `subst' is called, this is the insn that is being modified
(by combining in a previous insn). The PATTERN of this insn
is still the old pattern partially modified and it should not be
looked at, but this may be used to examine the successors of the insn
to judge whether a simplification is valid. */
static rtx subst_insn;
/* Record one modification to rtl structure
to be undone by storing old_contents into *where.
is_int is 1 if the contents are an int. */
struct undo
{
rtx *where;
rtx old_contents;
int is_int;
};
struct undo_int
{
int *where;
int old_contents;
int is_int;
};
/* Record a bunch of changes to be undone, up to MAX_UNDO of them.
num_undo says how many are currently recorded.
storage is nonzero if we must undo the allocation of new storage.
The value of storage is what to pass to obfree. */
#define MAX_UNDO 10
struct undobuf
{
int num_undo;
char *storage;
struct undo undo[MAX_UNDO];
};
static struct undobuf undobuf;
/* Number of times the pseudo being substituted for
was found and replaced. */
static int n_occurrences;
static void move_deaths ();
static void move_deaths_2 ();
void remove_death ();
static void record_dead_and_set_regs ();
int regno_dead_p ();
static int use_crosses_set_p ();
static int try_combine ();
static rtx try_distrib ();
static rtx subst ();
static void undo_all ();
static void copy_substitutions ();
static void add_links ();
static void remove_links ();
static void add_incs ();
static int adjacent_insns_p ();
static int check_asm_operands ();
static rtx simplify_and_const_int ();
static rtx gen_lowpart_for_combine ();
static void simplify_set_cc0_and ();
/* Main entry point for combiner. F is the first insn of the function.
NREGS is the first unused pseudo-reg number. */
void
combine_instructions (f, nregs)
rtx f;
int nregs;
{
register rtx insn;
register int i;
register rtx links, nextlinks;
rtx prev;
combine_attempts = 0;
combine_merges = 0;
combine_extras = 0;
combine_successes = 0;
distrib_attempts = 0;
distrib_merges_1 = 0;
distrib_merges_2 = 0;
distrib_successes = 0;
reg_last_death = (rtx *) alloca (nregs * sizeof (rtx));
reg_last_set = (rtx *) alloca (nregs * sizeof (rtx));
bzero (reg_last_death, nregs * sizeof (rtx));
bzero (reg_last_set, nregs * sizeof (rtx));
init_recog ();
/* Compute maximum uid value so uid_cuid can be allocated. */
for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
if (INSN_UID (insn) > i)
i = INSN_UID (insn);
uid_cuid = (int *) alloca ((i + 1) * sizeof (int));
/* Compute the mapping from uids to cuids.
Cuids are numbers assigned to insns, like uids,
except that cuids increase monotonically through the code. */
for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
INSN_CUID (insn) = ++i;
/* Now scan all the insns in forward order. */
last_call_cuid = 0;
mem_last_set = 0;
prev = 0;
for (insn = f; insn; insn = NEXT_INSN (insn))
{
if (GET_CODE (insn) == INSN
|| GET_CODE (insn) == CALL_INSN
|| GET_CODE (insn) == JUMP_INSN)
{
retry:
/* Try this insn with each insn it links back to. */
for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
if (try_combine (insn, XEXP (links, 0), 0))
goto retry;
/* Try each sequence of three linked insns ending with this one. */
for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
if (GET_CODE (XEXP (links, 0)) != NOTE)
for (nextlinks = LOG_LINKS (XEXP (links, 0)); nextlinks;
nextlinks = XEXP (nextlinks, 1))
if (try_combine (insn, XEXP (links, 0), XEXP (nextlinks, 0)))
goto retry;
/* Try to combine a jump insn that uses CC0
with a preceding insn that sets CC0, and maybe with its
logical predecessor as well.
This is how we make decrement-and-branch insns.
We need this special code because data flow connections
via CC0 do not get entered in LOG_LINKS. */
if (GET_CODE (insn) == JUMP_INSN
&& prev != 0
&& GET_CODE (prev) == INSN
&& GET_CODE (PATTERN (prev)) == SET
&& GET_CODE (SET_DEST (PATTERN (prev))) == CC0)
{
if (try_combine (insn, prev, 0))
goto retry;
if (GET_CODE (prev) != NOTE)
for (nextlinks = LOG_LINKS (prev); nextlinks;
nextlinks = XEXP (nextlinks, 1))
if (try_combine (insn, prev, XEXP (nextlinks, 0)))
goto retry;
}
/* Try to apply the distributive law to this insn
and two insns that compute the operands of this one. */
for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
if (GET_CODE (XEXP (links, 0)) != NOTE)
for (nextlinks = XEXP (links, 1); nextlinks; nextlinks = XEXP (nextlinks, 1))
if (GET_CODE (XEXP (nextlinks, 0)) != NOTE)
{
rtx try_from = 0;
if (GET_CODE (PATTERN (XEXP (links, 0))) == SET
&& find_reg_note (insn, REG_DEAD, SET_DEST (PATTERN (XEXP (links, 0))))
&& GET_CODE (PATTERN (XEXP (nextlinks, 0))) == SET
&& find_reg_note (insn, REG_DEAD, SET_DEST (PATTERN (XEXP (nextlinks, 0)))))
try_from = try_distrib (insn, XEXP (links, 0), XEXP (nextlinks, 0));
if (try_from != 0)
{
insn = try_from;
goto retry;
}
}
#if 0
/* Turned off because on 68020 it takes four insns to make
something like (a[b / 32] & (1 << (31 - (b % 32)))) != 0
that could actually be optimized, and that's an unlikely piece of code. */
/* If an insn gets or sets a bit field, try combining it
with two different insns whose results it uses. */
if (GET_CODE (insn) == INSN
&& GET_CODE (PATTERN (insn)) == SET
&& (GET_CODE (SET_DEST (PATTERN (insn))) == ZERO_EXTRACT
|| GET_CODE (SET_DEST (PATTERN (insn))) == SIGN_EXTRACT
|| GET_CODE (SET_SRC (PATTERN (insn))) == ZERO_EXTRACT
|| GET_CODE (SET_SRC (PATTERN (insn))) == SIGN_EXTRACT))
{
for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
if (GET_CODE (XEXP (links, 0)) != NOTE)
for (nextlinks = XEXP (links, 1); nextlinks;
nextlinks = XEXP (nextlinks, 1))
if (try_combine (insn, XEXP (links, 0), XEXP (nextlinks, 0)))
goto retry;
}
#endif
if (GET_CODE (insn) != NOTE)
record_dead_and_set_regs (insn);
prev = insn;
}
else if (GET_CODE (insn) != NOTE)
prev = 0;
}
total_attempts += combine_attempts;
total_merges += combine_merges;
total_extras += combine_extras;
total_successes += combine_successes;
}
/* Try to combine the insns I1 and I2 into I3.
Here I1 appears earlier than I2, which is earlier than I3.
I1 can be zero; then we combine just I2 into I3.
Return 1 if successful; if that happens, I1 and I2 are pseudo-deleted
by turning them into NOTEs, and I3 is modified.
Return 0 if the combination does not work. Then nothing is changed. */
static int
try_combine (i3, i2, i1)
register rtx i3, i2, i1;
{
register rtx newpat;
int added_sets_1 = 0;
int added_sets_2 = 0;
int total_sets;
int i2_is_used;
register rtx link;
int insn_code_number;
rtx i2dest, i2src;
rtx i1dest, i1src;
int maxreg;
rtx temp;
int i;
combine_attempts++;
/* Don't combine with something already used up by combination. */
if (GET_CODE (i2) == NOTE
|| (i1 && GET_CODE (i1) == NOTE))
return 0;
/* Don't combine across a CALL_INSN, because that would possibly
change whether the life span of some REGs crosses calls or not,
and it is a pain to update that information. */
if (INSN_CUID (i2) < last_call_cuid
|| (i1 && INSN_CUID (i1) < last_call_cuid))
return 0;
/* Can combine only if previous insn is a SET of a REG, a SUBREG or CC0.
That REG must be either set or dead by the final instruction
(so that we can safely forget about setting it).
Also test use_crosses_set_p to make sure that the value
that is to be substituted for the register
does not use any registers whose values alter in between.
Do not try combining with moves from one register to another
since it is better to let them be tied by register allocation.
(There is a switch to permit such combination; except the insns
that copy a function value into another register are never combined
because moving that too far away from the function call could cause
something else to be stored in that register in the interim.)
A set of a SUBREG is considered as if it were a set from
SUBREG. Thus, (SET (SUBREG:X (REG:Y...)) (something:X...))
is handled by substituting (SUBREG:Y (something:X...)) for (REG:Y...). */
if (GET_CODE (PATTERN (i2)) != SET)
return 0;
i2dest = SET_DEST (PATTERN (i2));
i2src = SET_SRC (PATTERN (i2));
if (GET_CODE (i2dest) == SUBREG)
{
i2dest = SUBREG_REG (i2dest);
i2src = gen_rtx (SUBREG, GET_MODE (i2dest), i2src, 0);
}
/* Don't eliminate a store in the stack pointer. */
if (i2dest == stack_pointer_rtx)
return 0;
/* Don't install a subreg involving two modes not tieable.
It can worsen register allocation, and can even make invalid reload insns,
since the reg inside may need to be copied from in the outside mode,
and that may be invalid if it is an fp reg copied in integer mode. */
if (GET_CODE (i2src) == SUBREG
&& ! MODES_TIEABLE_P (GET_MODE (i2src), GET_MODE (SUBREG_REG (i2src))))
return 0;
if (GET_CODE (i2dest) != CC0
&& (GET_CODE (i2dest) != REG
|| (GET_CODE (i2src) == REG
/* Do allow the combination of y = x; x = y; (with x dead)
because the result will turn into nothing. */
&& !(GET_CODE (PATTERN (i3)) == SET
&& i2src == SET_DEST (PATTERN (i3)))
&& (!flag_combine_regs
/* Don't substitute a function value reg for any other. */
|| FUNCTION_VALUE_REGNO_P (REGNO (i2src))))
|| GET_CODE (i2src) == CALL
/* Don't substitute into an incremented register. */
|| find_reg_note (i3, REG_INC, i2dest)
|| use_crosses_set_p (i2src, INSN_CUID (i2))))
return 0;
if (GET_CODE (i2src) == ASM_OPERANDS && MEM_VOLATILE_P (i2src))
return 0;
/* Don't substitute for a register intended as a clobberable operand. */
if (GET_CODE (PATTERN (i3)) == PARALLEL)
for (i = 0; i < XVECLEN (PATTERN (i3), 0); i++)
if (GET_CODE (XVECEXP (PATTERN (i3), 0, i)) == CLOBBER
&& XEXP (XVECEXP (PATTERN (i3), 0, i), 0) == i2dest)
return 0;
if (i1 != 0)
{
if (GET_CODE (PATTERN (i1)) != SET)
return 0;
i1dest = SET_DEST (PATTERN (i1));
i1src = SET_SRC (PATTERN (i1));
if (GET_CODE (i1dest) == SUBREG)
{
i1dest = SUBREG_REG (i1dest);
i1src = gen_rtx (SUBREG, GET_MODE (i1dest), i1src, 0);
}
if (i1dest == stack_pointer_rtx)
return 0;
if (GET_CODE (i1src) == SUBREG
&& ! MODES_TIEABLE_P (GET_MODE (i1src),
GET_MODE (SUBREG_REG (i1src))))
return 0;
if (GET_CODE (i1dest) != CC0
&& (GET_CODE (i1dest) != REG
|| (GET_CODE (i1src) == REG
&& (!flag_combine_regs
|| FUNCTION_VALUE_REGNO_P (REGNO (i1src))))
|| GET_CODE (i1src) == CALL
|| find_reg_note (i3, REG_INC, i1dest)
|| find_reg_note (i2, REG_INC, i1dest)
|| use_crosses_set_p (i1src, INSN_CUID (i1))))
return 0;
if (GET_CODE (i1src) == ASM_OPERANDS && MEM_VOLATILE_P (i1src))
return 0;
/* Don't substitute for a register intended as a clobberable operand. */
if (GET_CODE (PATTERN (i3)) == PARALLEL)
for (i = 0; i < XVECLEN (PATTERN (i3), 0); i++)
if (GET_CODE (XVECEXP (PATTERN (i3), 0, i)) == CLOBBER
&& XEXP (XVECEXP (PATTERN (i3), 0, i), 0) == i1dest)
return 0;
}
/* If it is better that two different modes keep two different pseudos,
avoid combining them. */
if (GET_CODE (PATTERN (i3)) == SET)
{
rtx i3dest = SET_DEST (PATTERN (i3));
while (GET_CODE (i3dest) == SUBREG
|| GET_CODE (i3dest) == STRICT_LOW_PART
|| GET_CODE (i3dest) == SIGN_EXTRACT
|| GET_CODE (i3dest) == ZERO_EXTRACT)
i3dest = SUBREG_REG (i3dest);
if (SET_SRC (PATTERN (i3)) == i2dest
&& GET_CODE (i3dest) == REG
&& ! MODES_TIEABLE_P (GET_MODE (i2dest), GET_MODE (i3dest)))
return 0;
}
/* If I2 contains anything volatile, reject, unless nothing
volatile comes between it and I3. */
if (volatile_refs_p (PATTERN (i2)))
{
rtx insn;
for (insn = NEXT_INSN (i2); insn != i3; insn = NEXT_INSN (insn))
if (GET_CODE (insn) == INSN || GET_CODE (insn) == CALL_INSN
|| GET_CODE (insn) == JUMP_INSN)
if (volatile_refs_p (PATTERN (insn)))
return 0;
}
/* Likewise for I1; nothing volatile can come between it and I3,
except optionally I2. */
if (i1 && volatile_refs_p (PATTERN (i1)))
{
rtx insn;
rtx end = (volatile_refs_p (PATTERN (i2)) ? i2 : i3);
for (insn = NEXT_INSN (i1); insn != end; insn = NEXT_INSN (insn))
if (GET_CODE (insn) == INSN || GET_CODE (insn) == CALL_INSN
|| GET_CODE (insn) == JUMP_INSN)
if (volatile_refs_p (PATTERN (insn)))
return 0;
}
/* If I1 or I2 contains an autoincrement or autodecrement,
make sure that register is not used between there and I3,
and not already used in I3 either.
Also insist that I3 not be a jump; if it were one
and the incremented register were spilled, we would lose. */
for (link = REG_NOTES (i2); link; link = XEXP (link, 1))
if (REG_NOTE_KIND (link) == REG_INC
&& (GET_CODE (i3) == JUMP_INSN
|| reg_used_between_p (XEXP (link, 0), i2, i3)
|| reg_mentioned_p (XEXP (link, 0), PATTERN (i3))))
return 0;
if (i1)
for (link = REG_NOTES (i1); link; link = XEXP (link, 1))
if (REG_NOTE_KIND (link) == REG_INC
&& (GET_CODE (i3) == JUMP_INSN
|| reg_used_between_p (XEXP (link, 0), i1, i3)
|| reg_mentioned_p (XEXP (link, 0), PATTERN (i3))))
return 0;
/* If I3 has an inc, then give up if I1 or I2 uses the reg that is inc'd,
EXCEPT in one case: I3 has a post-inc in an output operand. */
if (!(GET_CODE (PATTERN (i3)) == SET
&& GET_CODE (SET_SRC (PATTERN (i3))) == REG
&& GET_CODE (SET_DEST (PATTERN (i3))) == MEM
&& (GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_INC
|| GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_DEC)))
/* It's not the exception. */
for (link = REG_NOTES (i3); link; link = XEXP (link, 1))
if (REG_NOTE_KIND (link) == REG_INC
&& (reg_mentioned_p (XEXP (link, 0), PATTERN (i2))
|| (i1 != 0
&& reg_mentioned_p (XEXP (link, 0), PATTERN (i1)))))
return 0;
/* Don't combine an insn I1 or I2 that follows a CC0-setting insn.
An insn that uses CC0 must not be separated from the one that sets it.
It would be more logical to test whether CC0 occurs inside I1 or I2,
but that would be much slower, and this ought to be equivalent. */
temp = PREV_INSN (i2);
while (temp && GET_CODE (temp) == NOTE)
temp = PREV_INSN (temp);
if (temp && GET_CODE (temp) == INSN && sets_cc0_p (PATTERN (temp)))
return 0;
if (i1)
{
temp = PREV_INSN (i2);
while (temp && GET_CODE (temp) == NOTE)
temp = PREV_INSN (temp);
if (temp && GET_CODE (temp) == INSN && sets_cc0_p (PATTERN (temp)))
return 0;
}
/* See if the SETs in i1 or i2 need to be kept around in the merged
instruction: whenever the value set there is still needed past i3. */
added_sets_2 = (GET_CODE (i2dest) != CC0
&& ! dead_or_set_p (i3, i2dest));
if (i1)
added_sets_1 = ! (dead_or_set_p (i3, i1dest)
|| dead_or_set_p (i2, i1dest));
combine_merges++;
undobuf.num_undo = 0;
undobuf.storage = 0;
/* Substitute in the latest insn for the regs set by the earlier ones. */
maxreg = max_reg_num ();
subst_insn = i3;
n_occurrences = 0; /* `subst' counts here */
newpat = subst (PATTERN (i3), i2dest, i2src);
/* Record whether i2's body now appears within i3's body. */
i2_is_used = n_occurrences;
if (i1)
{
n_occurrences = 0;
newpat = subst (newpat, i1dest, i1src);
}
if (GET_CODE (PATTERN (i3)) == SET
&& SET_DEST (PATTERN (i3)) == cc0_rtx
&& (GET_CODE (SET_SRC (PATTERN (i3))) == AND
|| GET_CODE (SET_SRC (PATTERN (i3))) == LSHIFTRT)
&& next_insn_tests_no_inequality (i3))
simplify_set_cc0_and (i3);
if (max_reg_num () != maxreg)
abort ();
/* If the actions of the earler insns must be kept
in addition to substituting them into the latest one,
we must make a new PARALLEL for the latest insn
to hold additional the SETs. */
if (added_sets_1 || added_sets_2)
{
combine_extras++;
/* Arrange to free later what we allocate now
if we don't accept this combination. */
if (!undobuf.storage)
undobuf.storage = (char *) oballoc (0);
if (GET_CODE (newpat) == PARALLEL)
{
rtvec old = XVEC (newpat, 0);
total_sets = XVECLEN (newpat, 0) + added_sets_1 + added_sets_2;
newpat = gen_rtx (PARALLEL, VOIDmode, rtvec_alloc (total_sets));
bcopy (&old->elem[0], &XVECEXP (newpat, 0, 0),
sizeof (old->elem[0]) * old->num_elem);
}
else
{
rtx old = newpat;
total_sets = 1 + added_sets_1 + added_sets_2;
newpat = gen_rtx (PARALLEL, VOIDmode, rtvec_alloc (total_sets));
XVECEXP (newpat, 0, 0) = old;
}
if (added_sets_1)
{
XVECEXP (newpat, 0, --total_sets) = PATTERN (i1);
}
if (added_sets_2)
{
/* If there is no I1, use I2's body as is. */
if (i1 == 0
/* If I2 was stuck into I3, then anything within it has
already had I1 substituted into it when that was done to I3. */
|| i2_is_used)
{
XVECEXP (newpat, 0, --total_sets) = PATTERN (i2);
}
else
XVECEXP (newpat, 0, --total_sets)
= subst (PATTERN (i2), i1dest, i1src);
}
}
/* Fail if an autoincrement side-effect has been duplicated. */
if ((i2_is_used > 1 && find_reg_note (i2, REG_INC, 0) != 0)
|| (i1 != 0 && n_occurrences > 1 && find_reg_note (i1, REG_INC, 0) != 0))
{
undo_all ();
return 0;
}
/* Is the result of combination a valid instruction? */
insn_code_number = recog (newpat, i3);
if (insn_code_number >= 0
/* Is the result a reasonable ASM_OPERANDS? */
|| (check_asm_operands (newpat) && ! added_sets_1 && ! added_sets_2))
{
/* Yes. Install it. */
register int regno;
INSN_CODE (i3) = insn_code_number;
PATTERN (i3) = newpat;
/* If anything was substituted more than once,
copy it to avoid invalid shared rtl structure. */
copy_substitutions ();
/* The data flowing into I2 now flows into I3.
But we cannot always move all of I2's LOG_LINKS into I3,
since they must go to a setting of a REG from the
first use following. If I2 was the first use following a set,
I3 is now a use, but it is not the first use
if some instruction between I2 and I3 is also a use.
Here, for simplicity, we move all the links only if
there are no real insns between I2 and I3.
Otherwise, we move only links that correspond to regs
that used to die in I2. They are always safe to move. */
add_links (i3, i2, adjacent_insns_p (i2, i3));
/* Most REGs that previously died in I2 now die in I3. */
move_deaths (i2src, INSN_CUID (i2), i3);
if (GET_CODE (i2dest) == REG)
{
/* If the reg formerly set in I2 died only once and that was in I3,
zero its use count so it won't make `reload' do any work. */
regno = REGNO (i2dest);
if (! added_sets_2)
{
reg_n_sets[regno]--;
/* Used to check && regno_dead_p (regno, i3) also here. */
if (reg_n_sets[regno] == 0
&& ! (basic_block_live_at_start[0][regno / HOST_BITS_PER_INT]
& (1 << (regno % HOST_BITS_PER_INT))))
reg_n_refs[regno] = 0;
}
/* If a ref to REGNO was substituted into I3 from I2,
then it still dies there if it previously did.
Otherwise either REGNO never did die in I3 so remove_death is safe
or this entire life of REGNO is gone so remove its death. */
if (!added_sets_2
&& ! reg_mentioned_p (i2dest, PATTERN (i3)))
remove_death (regno, i3);
}
/* Any registers previously autoincremented in I2
are now incremented in I3. */
add_incs (i3, REG_NOTES (i2));
if (i1)
{
/* Likewise, merge the info from I1 and get rid of it. */
add_links (i3, i1,
adjacent_insns_p (i1, i2) && adjacent_insns_p (i2, i3));
move_deaths (i1src, INSN_CUID (i1), i3);
if (GET_CODE (i1dest) == REG)
{
regno = REGNO (i1dest);
if (! added_sets_1)
{
reg_n_sets[regno]--;
/* Used to also check && regno_dead_p (regno, i3) here. */
if (reg_n_sets[regno] == 0
&& ! (basic_block_live_at_start[0][regno / HOST_BITS_PER_INT]
& (1 << (regno % HOST_BITS_PER_INT))))
reg_n_refs[regno] = 0;
}
/* If a ref to REGNO was substituted into I3 from I1,
then it still dies there if it previously did.
Else either REGNO never did die in I3 so remove_death is safe
or this entire life of REGNO is gone so remove its death. */
if (! added_sets_1
&& ! reg_mentioned_p (i1dest, PATTERN (i3)))
remove_death (regno, i3);
}
add_incs (i3, REG_NOTES (i1));
LOG_LINKS (i1) = 0;
PUT_CODE (i1, NOTE);
NOTE_LINE_NUMBER (i1) = NOTE_INSN_DELETED;
NOTE_SOURCE_FILE (i1) = 0;
}
/* Get rid of I2. */
LOG_LINKS (i2) = 0;
PUT_CODE (i2, NOTE);
NOTE_LINE_NUMBER (i2) = NOTE_INSN_DELETED;
NOTE_SOURCE_FILE (i2) = 0;
combine_successes++;
return 1;
}
/* Failure: change I3 back the way it was. */
undo_all ();
return 0;
}
/* Undo all the modifications recorded in undobuf. */
static void
undo_all ()
{
register int i;
if (undobuf.num_undo > MAX_UNDO)
undobuf.num_undo = MAX_UNDO;
for (i = undobuf.num_undo - 1; i >= 0; i--)
*undobuf.undo[i].where = undobuf.undo[i].old_contents;
if (undobuf.storage)
obfree (undobuf.storage);
undobuf.num_undo = 0;
undobuf.storage = 0;
}
/* If this insn had more than one substitution,
copy all but one, so that no invalid shared substructure is introduced. */
static void
copy_substitutions ()
{
register int i;
if (undobuf.num_undo > 1)
{
for (i = undobuf.num_undo - 1; i >= 1; i--)
if (! undobuf.undo[i].is_int)
*undobuf.undo[i].where = copy_rtx (*undobuf.undo[i].where);
}
}
/* Throughout X, replace FROM with TO, and return the result.
The result is TO if X is FROM;
otherwise the result is X, but its contents may have been modified.
If they were modified, a record was made in undobuf so that
undo_all will (among other things) return X to its original state.
If the number of changes necessary is too much to record to undo,
the excess changes are not made, so the result is invalid.
The changes already made can still be undone.
undobuf.num_undo is incremented for such changes, so by testing that
the caller can tell whether the result is valid.
`n_occurrences' is incremented each time FROM is replaced. */
static rtx
subst (x, from, to)
register rtx x, from, to;
{
register char *fmt;
register int len, i;
register enum rtx_code code;
char was_replaced[2];
#define SUBST(INTO, NEWVAL) \
do { if (undobuf.num_undo < MAX_UNDO) \
{ \
undobuf.undo[undobuf.num_undo].where = &INTO; \
undobuf.undo[undobuf.num_undo].old_contents = INTO; \
undobuf.undo[undobuf.num_undo].is_int = 0; \
INTO = NEWVAL; \
} \
undobuf.num_undo++; } while (0)
#define SUBST_INT(INTO, NEWVAL) \
do { if (undobuf.num_undo < MAX_UNDO) \
{ \
struct undo_int *u = (struct undo_int *)&undobuf.undo[undobuf.num_undo];\
u->where = &INTO; \
u->old_contents = INTO; \
u->is_int = 1; \
INTO = NEWVAL; \
} \
undobuf.num_undo++; } while (0)
/* FAKE_EXTEND_SAFE_P (MODE, FROM) is 1 if (subreg:MODE FROM 0) is a safe
replacement for (zero_extend:MODE FROM) or (sign_extend:MODE FROM).
If it is 0, that cannot be done. We can now do this for any MEM
because (SUBREG (MEM...)) is guaranteed to cause the MEM to be reloaded.
If not for that, MEM's would very rarely be safe. */
/* Reject MODEs bigger than a word, because we might not be able
to reference a two-register group starting with an arbitrary register
(and currently gen_lowpart might crash for a SUBREG). */
#define FAKE_EXTEND_SAFE_P(MODE, FROM) \
(GET_MODE_SIZE (MODE) <= UNITS_PER_WORD \
&& (GET_CODE (FROM) == REG || GET_CODE (FROM) == SUBREG \
|| GET_CODE (FROM) == MEM))
if (x == from)
return to;
/* It is possible to have a subexpression appear twice in the insn.
Suppose that FROM is a register that appears within TO.
Then, after that subexpression has been scanned once by `subst',
the second time it is scanned, TO may be found. If we were
to scan TO here, we would find FROM within it and create a
self-referent rtl structure which is completely wrong. */
if (x == to)
return to;
code = GET_CODE (x);
/* A little bit of algebraic simplification here. */
switch (code)
{
/* This case has no effect except to speed things up. */
case REG:
case CONST_INT:
case CONST:
case SYMBOL_REF:
case LABEL_REF:
case PC:
case CC0:
return x;
}
was_replaced[0] = 0;
was_replaced[1] = 0;
len = GET_RTX_LENGTH (code);
fmt = GET_RTX_FORMAT (code);
/* Don't replace FROM where it is being stored in rather than used. */
if (code == SET && SET_DEST (x) == from)
fmt = "ie";
if (code == SET && GET_CODE (SET_DEST (x)) == SUBREG
&& SUBREG_REG (SET_DEST (x)) == from)
fmt = "ie";
for (i = 0; i < len; i++)
{
if (fmt[i] == 'E')
{
register int j;
for (j = XVECLEN (x, i) - 1; j >= 0; j--)
{
register rtx new;
if (XVECEXP (x, i, j) == from)
new = to, n_occurrences++;
else
new = subst (XVECEXP (x, i, j), from, to);
if (new != XVECEXP (x, i, j))
SUBST (XVECEXP (x, i, j), new);
}
}
else if (fmt[i] == 'e')
{
register rtx new;
if (XEXP (x, i) == from)
{
new = to;
n_occurrences++;
if (i < 2)
was_replaced[i] = 1;
}
else
new = subst (XEXP (x, i), from, to);
if (new != XEXP (x, i))
SUBST (XEXP (x, i), new);
}
}
/* A little bit of algebraic simplification here. */
switch (code)
{
case SUBREG:
/* Changing mode twice with SUBREG => just change it once,
or not at all if changing back to starting mode. */
if (SUBREG_REG (x) == to
&& GET_CODE (to) == SUBREG)
{
if (GET_MODE (x) == GET_MODE (SUBREG_REG (to)))
if (SUBREG_WORD (x) == 0 && SUBREG_WORD (to) == 0)
return SUBREG_REG (to);
SUBST (SUBREG_REG (x), SUBREG_REG (to));
if (SUBREG_WORD (to) != 0)
SUBST_INT (SUBREG_WORD (x), SUBREG_WORD (x) + SUBREG_WORD (to));
}
if (SUBREG_REG (x) == to
&& (GET_CODE (to) == SIGN_EXTEND || GET_CODE (to) == ZERO_EXTEND)
&& subreg_lowpart_p (x))
{
/* (subreg (sign_extend X)) is X, if it has same mode as X. */
if (GET_MODE (x) == GET_MODE (XEXP (to, 0)))
return XEXP (to, 0);
/* (subreg (sign_extend X)), if it has a mode wider than X,
can be done with (sign_extend X). */
if (GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (GET_MODE (XEXP (to, 0))))
{
if (!undobuf.storage)
undobuf.storage = (char *) oballoc (0);
return gen_rtx (GET_CODE (to), GET_MODE (x), XEXP (to, 0));
}
/* Extend and then truncate smaller than it was to start with:
no need to extend. */
if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (GET_MODE (XEXP (to, 0))))
{
SUBST (XEXP (x, 0), XEXP (to, 0));
}
}
/* (subreg:A (mem:B X) N) becomes a modified MEM.
If we can't do that safely, then it becomes something nonsensical
so that this combination won't take place.
This avoids producing any (subreg (mem))s except in the special
paradoxical case where gen_lowpart_for_combine makes them. */
if (SUBREG_REG (x) == to
&& GET_CODE (to) == MEM)
{
int endian_offset = 0;
/* Don't combine this if mode A is wider than B. */
if (GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (GET_MODE (to)))
return gen_rtx (CLOBBER, VOIDmode, const0_rtx);
/* Don't change the mode of the MEM
if that would change the meaning of the address. */
if (mode_dependent_address_p (XEXP (to, 0)))
return gen_rtx (CLOBBER, VOIDmode, const0_rtx);
#ifdef BYTES_BIG_ENDIAN
if (GET_MODE_SIZE (GET_MODE (x)) < UNITS_PER_WORD)
endian_offset += UNITS_PER_WORD - GET_MODE_SIZE (GET_MODE (x));
if (GET_MODE_SIZE (GET_MODE (to)) < UNITS_PER_WORD)
endian_offset -= UNITS_PER_WORD - GET_MODE_SIZE (GET_MODE (to));
#endif
if (!undobuf.storage)
undobuf.storage = (char *) oballoc (0);
/* Note if the plus_constant doesn't make a valid address
then this combination won't be accepted. */
return gen_rtx (MEM, GET_MODE (x),
plus_constant (XEXP (to, 0),
(SUBREG_WORD (x) * UNITS_PER_WORD
+ endian_offset)));
}
break;
case NOT:
/* (not (minus X 1)) can become (neg X). */
if (was_replaced[0]
&& ((GET_CODE (to) == PLUS && INTVAL (XEXP (to, 1)) == -1)
|| (GET_CODE (to) == MINUS && XEXP (to, 1) == const1_rtx)))
{
if (!undobuf.storage)
undobuf.storage = (char *) oballoc (0);
return gen_rtx (NEG, GET_MODE (to), XEXP (to, 0));
}
/* Don't let substitution introduce double-negatives. */
if (was_replaced[0]
&& GET_CODE (to) == code)
return XEXP (to, 0);
break;
case NEG:
/* (neg (minus X Y)) can become (minus Y X). */
if (was_replaced[0] && GET_CODE (to) == MINUS)
{
if (!undobuf.storage)
undobuf.storage = (char *) oballoc (0);
return gen_rtx (MINUS, GET_MODE (to),
XEXP (to, 1), XEXP (to, 0));
}
/* Don't let substitution introduce double-negatives. */
if (was_replaced[0]
&& GET_CODE (to) == code)
return XEXP (to, 0);
break;
case FLOAT_TRUNCATE:
/* (float_truncate:SF (float_extend:DF foo:SF)) = foo:SF. */
if (was_replaced[0]
&& GET_CODE (to) == FLOAT_EXTEND
&& GET_MODE (XEXP (to, 0)) == GET_MODE (x))
return XEXP (to, 0);
break;
#if 0
case COMPARE:
/* -x>0 if 0>x. */
if (GET_CODE (XEXP (x, 0)) == NEG && XEXP (x, 1) == const0_rtx)
{
SUBST (XEXP (x, 1), XEXP (XEXP (x, 0), 0));
SUBST (XEXP (x, 0), const0_rtx);
}
if (GET_CODE (XEXP (x, 1)) == NEG && XEXP (x, 0) == const0_rtx)
{
SUBST (XEXP (x, 0), XEXP (XEXP (x, 1), 0));
SUBST (XEXP (x, 1), const0_rtx);
}
break;
#endif
case PLUS:
#if 0 /* Turned off for caution: turn it on after 1.36. */
/* Identify constant sums as such. */
if ((was_replaced[0] || was_replaced[1])
&& CONSTANT_P (XEXP (x, 0))
&& CONSTANT_P (XEXP (x, 1)))
{
if (!undobuf.storage)
undobuf.storage = (char *) oballoc (0);
return gen_rtx (CONST, GET_MODE (x), x);
}
#endif
/* In (plus <foo> (ashift <bar> <n>))
change the shift to a multiply so we can recognize
scaled indexed addresses. */
if ((was_replaced[0]
|| was_replaced[1])
&& GET_CODE (to) == ASHIFT
&& GET_CODE (XEXP (to, 1)) == CONST_INT
&& INTVAL (XEXP (to, 1)) < HOST_BITS_PER_INT)
{
rtx temp;
if (!undobuf.storage)
undobuf.storage = (char *) oballoc (0);
temp = gen_rtx (MULT, GET_MODE (to),
XEXP (to, 0),
gen_rtx (CONST_INT, VOIDmode,
1 << INTVAL (XEXP (to, 1))));
if (was_replaced[0])
SUBST (XEXP (x, 0), temp);
else
SUBST (XEXP (x, 1), temp);
}
/* (plus X (neg Y)) becomes (minus X Y). */
if (GET_CODE (XEXP (x, 1)) == NEG)
{
if (!undobuf.storage)
undobuf.storage = (char *) oballoc (0);
return gen_rtx (MINUS, GET_MODE (x),
XEXP (x, 0), XEXP (XEXP (x, 1), 0));
}
/* (plus (neg X) Y) becomes (minus Y X). */
if (GET_CODE (XEXP (x, 0)) == NEG)
{
if (!undobuf.storage)
undobuf.storage = (char *) oballoc (0);
return gen_rtx (MINUS, GET_MODE (x),
XEXP (x, 1), XEXP (XEXP (x, 0), 0));
}
/* (plus (plus x c1) c2) => (plus x c1+c2) */
if (GET_CODE (XEXP (x, 1)) == CONST_INT
&& GET_CODE (XEXP (x, 0)) == PLUS
&& GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)
{
int sum = (INTVAL (XEXP (x, 1))
+ INTVAL (XEXP (XEXP (x, 0), 1)));
if (sum == 0)
return XEXP (XEXP (x, 0), 0);
if (!undobuf.storage)
undobuf.storage = (char *) oballoc (0);
SUBST (XEXP (x, 1), gen_rtx (CONST_INT, VOIDmode, sum));
SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
break;
}
/* If we have something (putative index) being added to a sum,
associate it so that any constant term is outermost.
That's because that's the way indexed addresses are
now supposed to appear. */
if (((was_replaced[0] && GET_CODE (XEXP (x, 1)) == PLUS)
|| (was_replaced[1] && GET_CODE (XEXP (x, 0)) == PLUS))
||
((was_replaced[0] || was_replaced[1])
&& GET_CODE (to) == PLUS))
{
rtx offset = 0, base, index;
if (GET_CODE (to) != PLUS)
{
index = to;
base = was_replaced[0] ? XEXP (x, 1) : XEXP (x, 0);
}
else
{
index = was_replaced[0] ? XEXP (x, 1) : XEXP (x, 0);
base = to;
}
if (CONSTANT_ADDRESS_P (XEXP (base, 0)))
{
offset = XEXP (base, 0);
base = XEXP (base, 1);
}
else if (CONSTANT_ADDRESS_P (XEXP (base, 1)))
{
offset = XEXP (base, 1);
base = XEXP (base, 0);
}
if (offset != 0)
{
if (!undobuf.storage)
undobuf.storage = (char *) oballoc (0);
if (GET_CODE (offset) == CONST_INT)
return plus_constant (gen_rtx (PLUS, GET_MODE (index),
base, index),
INTVAL (offset));
if (GET_CODE (index) == CONST_INT)
return plus_constant (gen_rtx (PLUS, GET_MODE (offset),
base, offset),
INTVAL (index));
return gen_rtx (PLUS, GET_MODE (index),
gen_rtx (PLUS, GET_MODE (index),
base, index),
offset);
}
}
break;
case EQ:
case NE:
/* If comparing a subreg against zero, discard the subreg. */
if (was_replaced[0]
&& GET_CODE (to) == SUBREG
&& SUBREG_WORD (to) == 0
&& XEXP (x, 1) == const0_rtx)
SUBST (XEXP (x, 0), SUBREG_REG (to));
/* If comparing a ZERO_EXTRACT against zero,
canonicalize to a SIGN_EXTRACT,
since the two are equivalent here. */
if (was_replaced[0]
&& GET_CODE (to) == ZERO_EXTRACT
&& XEXP (x, 1) == const0_rtx)
{
if (!undobuf.storage)
undobuf.storage = (char *) oballoc (0);
SUBST (XEXP (x, 0),
gen_rtx (SIGN_EXTRACT, GET_MODE (to),
XEXP (to, 0), XEXP (to, 1),
XEXP (to, 2)));
}
#ifndef BITS_BIG_ENDIAN
/* If we are putting (ASHIFT 1 x) into (EQ (AND ... y) 0),
arrange to return (EQ (SIGN_EXTRACT y 1 x) 0),
which is what jump-on-bit instructions are written with. */
else if (XEXP (x, 1) == const0_rtx
&& GET_CODE (XEXP (x, 0)) == AND
&& (XEXP (XEXP (x, 0), 0) == to
|| XEXP (XEXP (x, 0), 1) == to)
&& GET_CODE (to) == ASHIFT
&& XEXP (to, 0) == const1_rtx)
{
register rtx y = XEXP (XEXP (x, 0),
XEXP (XEXP (x, 0), 0) == to);
if (!undobuf.storage)
undobuf.storage = (char *) oballoc (0);
SUBST (XEXP (x, 0),
gen_rtx (SIGN_EXTRACT, GET_MODE (to),
y,
const1_rtx, XEXP (to, 1)));
}
#endif /* not BITS_BIG_ENDIAN */
/* Negation is a no-op before equality test against zero. */
if (GET_CODE (XEXP (x, 0)) == NEG && XEXP (x, 1) == const0_rtx)
{
SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
}
if (GET_CODE (XEXP (x, 1)) == NEG && XEXP (x, 0) == const0_rtx)
{
SUBST (XEXP (x, 1), XEXP (XEXP (x, 1), 0));
}
break;
case ZERO_EXTEND:
/* Nested zero-extends are equivalent to just one. */
if (was_replaced[0]
&& GET_CODE (to) == ZERO_EXTEND)
SUBST (XEXP (x, 0), XEXP (to, 0));
/* Zero extending a constant int can be replaced
by a zero-extended constant. */
if (was_replaced[0]
&& HOST_BITS_PER_INT >= GET_MODE_BITSIZE (GET_MODE (from))
&& GET_CODE (to) == CONST_INT)
{
int intval = INTVAL (to) & GET_MODE_MASK (GET_MODE (from));
if (!undobuf.storage)
undobuf.storage = (char *) oballoc (0);
return gen_rtx (CONST_INT, VOIDmode, intval);
}
/* Zero-extending the result of an and with a constant can be done
with a wider and. */
if (was_replaced[0]
&& GET_CODE (to) == AND
&& GET_CODE (XEXP (to, 1)) == CONST_INT
&& FAKE_EXTEND_SAFE_P (GET_MODE (x), XEXP (to, 0))
/* Avoid getting wrong result if the constant has high bits set
that are irrelevant in the narrow mode where it is being used. */
&& 0 == (INTVAL (XEXP (to, 1))
& ~ GET_MODE_MASK (GET_MODE (to))))
{
if (!undobuf.storage)
undobuf.storage = (char *) oballoc (0);
return gen_rtx (AND, GET_MODE (x),
gen_lowpart_for_combine (GET_MODE (x), XEXP (to, 0)),
XEXP (to, 1));
}
/* Change (zero_extend:M (subreg:N (zero_extract:M ...) 0))
to (zero_extract:M ...) if the field extracted fits in mode N. */
if (GET_CODE (XEXP (x, 0)) == SUBREG
&& GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTRACT
&& GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == CONST_INT
&& (INTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))
<= GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))))
{
return XEXP (XEXP (x, 0), 0);
}
/* Change (zero_extend:M (subreg:N (and:M ... <const>) 0))
to (and:M ...) if the significant bits fit in mode N. */
if (GET_CODE (XEXP (x, 0)) == SUBREG
&& SUBREG_REG (XEXP (x, 0)) == to
&& SUBREG_WORD (XEXP (x, 0)) == 0
&& GET_CODE (to) == AND
&& GET_CODE (XEXP (to, 1)) == CONST_INT
&& FAKE_EXTEND_SAFE_P (GET_MODE (x), XEXP (to, 0))
/* Avoid getting wrong result if the constant has high bits set
that are irrelevant in the narrow mode where it is being used. */
&& 0 == (INTVAL (XEXP (to, 1))
& ~ GET_MODE_MASK (GET_MODE (to))))
{
if (!undobuf.storage)
undobuf.storage = (char *) oballoc (0);
return gen_rtx (AND, GET_MODE (x),
gen_lowpart_for_combine (GET_MODE (x), XEXP (to, 0)),
XEXP (to, 1));
}
/* In (zero_extend:M (subreg:N (lshiftrt:M REG))),
where REG was assigned from (zero_extend:M (any:N ...)),
remove the outer zero extension. */
if (GET_CODE (XEXP (x, 0)) == SUBREG
&& SUBREG_REG (XEXP (x, 0)) == to
&& SUBREG_WORD (XEXP (x, 0)) == 0
&& GET_CODE (to) == LSHIFTRT)
{
rtx tmp = XEXP (to, 0);
/* See if arg of LSHIFTRT is a register whose value we can find. */
if (GET_CODE (tmp) == REG)
if (reg_n_sets[REGNO (tmp)] == 1
&& reg_last_set[REGNO (tmp)] != 0
&& SET_DEST (PATTERN (reg_last_set[REGNO (tmp)])) == tmp)
tmp = SET_SRC (PATTERN (reg_last_set[REGNO (tmp)]));
else
break;
if (GET_CODE (tmp) == ZERO_EXTEND
&& GET_MODE (tmp) == GET_MODE (x)
&& GET_MODE (XEXP (tmp, 0)) == GET_MODE (XEXP (x, 0)))
return SUBREG_REG (XEXP (x, 0));
}
break;
case SIGN_EXTEND:
/* Nested sign-extends are equivalent to just one. */
if (was_replaced[0]
&& GET_CODE (to) == SIGN_EXTEND)
SUBST (XEXP (x, 0), XEXP (to, 0));
/* Sign extending a constant int can be replaced
by a sign-extended constant. */
if (was_replaced[0]
&& HOST_BITS_PER_INT >= GET_MODE_BITSIZE (GET_MODE (from))
&& GET_CODE (to) == CONST_INT)
{
int intval = INTVAL (to);
if (!undobuf.storage)
undobuf.storage = (char *) oballoc (0);
if (intval > 0
&& (intval & (1 << (GET_MODE_BITSIZE (GET_MODE (from)) - 1))))
intval |= ~ GET_MODE_MASK (GET_MODE (from));
return gen_rtx (CONST_INT, VOIDmode, intval);
}
/* Sign-extending the result of an and with a constant can be done
with a wider and, provided the high bit of the constant is 0. */
if (was_replaced[0]
&& GET_CODE (to) == AND
&& GET_CODE (XEXP (to, 1)) == CONST_INT
&& FAKE_EXTEND_SAFE_P (GET_MODE (x), XEXP (to, 0))
&& ((INTVAL (XEXP (to, 1))
& (-1 << (GET_MODE_BITSIZE (GET_MODE (to)) - 1)))
== 0))
{
if (!undobuf.storage)
undobuf.storage = (char *) oballoc (0);
return gen_rtx (AND, GET_MODE (x),
gen_lowpart_for_combine (GET_MODE (x), XEXP (to, 0)),
XEXP (to, 1));
}
/* hacks added by tiemann. */
/* Change (sign_extend:M (subreg:N (and:M ... <const>) 0))
to (and:M ...), provided the result fits in mode N,
and the high bit of the constant is 0 in mode N. */
if (GET_CODE (XEXP (x, 0)) == SUBREG
&& SUBREG_REG (XEXP (x, 0)) == to
&& SUBREG_WORD (XEXP (x, 0)) == 0
&& GET_CODE (to) == AND
&& GET_CODE (XEXP (to, 1)) == CONST_INT
&& FAKE_EXTEND_SAFE_P (GET_MODE (x), XEXP (to, 0))
&& ((INTVAL (XEXP (to, 1))
& (-1 << (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - 1)))
== 0))
{
if (!undobuf.storage)
undobuf.storage = (char *) oballoc (0);
return gen_rtx (AND, GET_MODE (x),
gen_lowpart_for_combine (GET_MODE (x), XEXP (to, 0)),
XEXP (to, 1));
}
/* In (sign_extend:M (subreg:N (ashiftrt:M REG))),
where REG was assigned from (sign_extend:M (any:N ...)),
remove the outer sign extension. */
if (GET_CODE (XEXP (x, 0)) == SUBREG
&& SUBREG_REG (XEXP (x, 0)) == to
&& SUBREG_WORD (XEXP (x, 0)) == 0
&& GET_CODE (to) == ASHIFTRT)
{
rtx tmp = XEXP (to, 0);
/* See if arg of LSHIFTRT is a register whose value we can find. */
if (GET_CODE (tmp) == REG)
if (reg_n_sets[REGNO (tmp)] == 1
&& reg_last_set[REGNO (tmp)] != 0
&& SET_DEST (PATTERN (reg_last_set[REGNO (tmp)])) == tmp)
tmp = SET_SRC (PATTERN (reg_last_set[REGNO (tmp)]));
else
break;
if (GET_CODE (tmp) == SIGN_EXTEND
&& GET_MODE (tmp) == GET_MODE (x)
&& GET_MODE (XEXP (tmp, 0)) == GET_MODE (XEXP (x, 0)))
return SUBREG_REG (XEXP (x, 0));
}
break;
case SET:
/* In (set (zero-extract <x> <n> <y>) (and <foo> <(2**n-1) | anything>))
the `and' can be deleted. This can happen when storing a bit
that came from a set-flag insn followed by masking to one bit. */
if (GET_CODE (XEXP (x, 0)) == ZERO_EXTRACT
&& GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
&& was_replaced[1]
&& GET_CODE (to) == AND
&& GET_CODE (XEXP (to, 1)) == CONST_INT
&& 0 == (((1 << INTVAL (XEXP (XEXP (x, 0), 1))) - 1)
& ~ INTVAL (XEXP (to, 1))))
{
SUBST (XEXP (x, 1), XEXP (to, 0));
}
/* In (set (zero-extract <x> <n> <y>)
(subreg (and <foo> <(2**n-1) | anything>)))
the `and' can be deleted. */
if (GET_CODE (XEXP (x, 0)) == ZERO_EXTRACT
&& GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
&& GET_CODE (XEXP (x, 1)) == SUBREG
&& SUBREG_WORD (XEXP (x, 1)) == 0
&& GET_CODE (SUBREG_REG (XEXP (x, 1))) == AND
&& GET_CODE (XEXP (SUBREG_REG (XEXP (x, 1)), 1)) == CONST_INT
&& 0 == (((1 << INTVAL (XEXP (XEXP (x, 0), 1))) - 1)
& ~ INTVAL (XEXP (SUBREG_REG (XEXP (x, 1)), 1))))
{
SUBST (SUBREG_REG (XEXP (x, 1)), XEXP (SUBREG_REG (XEXP (x, 1)), 0));
}
/* (set (zero_extract ...) (and/or/xor (zero_extract ...) const)),
if both zero_extracts have the same location, size and position,
can be changed to avoid the byte extracts. */
if ((GET_CODE (XEXP (x, 0)) == ZERO_EXTRACT
|| GET_CODE (XEXP (x, 0)) == SIGN_EXTRACT)
&& GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
&& (GET_CODE (XEXP (x, 1)) == AND
|| GET_CODE (XEXP (x, 1)) == IOR
|| GET_CODE (XEXP (x, 1)) == XOR)
&& rtx_equal_p (XEXP (x, 0), XEXP (XEXP (x, 1), 0))
&& GET_CODE (XEXP (XEXP (x, 1), 0)) == GET_CODE (XEXP (x, 0))
&& GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
/* zero_extract can apply to a QImode even if the bits extracted
don't fit inside that byte. In such a case, we may not do this
optimization, since the OR or AND insn really would need
to fit in a byte. */
&& (INTVAL (XEXP (XEXP (x, 0), 1)) + INTVAL (XEXP (XEXP (x, 0), 2))
< GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (x, 0), 0)))))
{
int shiftcount;
int newmask;
#ifdef BITS_BIG_ENDIAN
shiftcount
= GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (x, 0), 0)))
- INTVAL (XEXP (XEXP (x, 0), 1)) - INTVAL (XEXP (XEXP (x, 0), 2));
#else
shiftcount
= INTVAL (XEXP (XEXP (x, 0), 2));
#endif
newmask = ((INTVAL (XEXP (XEXP (x, 1), 1)) << shiftcount)
+ (GET_CODE (XEXP (x, 1)) == AND
? (1 << shiftcount) - 1
: 0));
if (GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (x, 0), 0)))
< HOST_BITS_PER_INT)
newmask &= (1 << GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (x, 0), 0)))) - 1;
if (!undobuf.storage)
undobuf.storage = (char *) oballoc (0);
return
gen_rtx (SET, VOIDmode,
XEXP (XEXP (x, 0), 0),
gen_rtx (GET_CODE (XEXP (x, 1)),
GET_MODE (XEXP (XEXP (x, 0), 0)),
XEXP (XEXP (XEXP (x, 1), 0), 0),
gen_rtx (CONST_INT, VOIDmode, newmask)));
}
/* Can simplify (set (cc0) (compare (zero/sign_extend FOO) CONST))
to (set (cc0) (compare FOO CONST)) if CONST fits in FOO's mode
and we are only testing equality.
In fact, this is valid for zero_extend if what follows is an
unsigned comparison, and for sign_extend with a signed comparison. */
if (SET_DEST (x) == cc0_rtx
&& GET_CODE (SET_SRC (x)) == COMPARE
&& (GET_CODE (XEXP (SET_SRC (x), 0)) == ZERO_EXTEND
|| GET_CODE (XEXP (SET_SRC (x), 0)) == SIGN_EXTEND)
&& next_insn_tests_no_inequality (subst_insn)
&& GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
/* This is overly cautious by one bit, but saves worrying about
whether it is zero-extension or sign extension. */
&& ((unsigned) INTVAL (XEXP (SET_SRC (x), 1))
< (1 << (GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (SET_SRC (x), 0), 0))) - 1))))
SUBST (XEXP (SET_SRC (x), 0), XEXP (XEXP (SET_SRC (x), 0), 0));
break;
case AND:
if (GET_CODE (XEXP (x, 1)) == CONST_INT)
{
rtx tem = simplify_and_const_int (x, to);
if (tem)
return tem;
}
break;
case IOR:
case XOR:
/* (ior (ior x c1) c2) => (ior x c1|c2); likewise for xor. */
if (GET_CODE (XEXP (x, 1)) == CONST_INT
&& GET_CODE (XEXP (x, 0)) == code
&& GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)
{
int c0 = INTVAL (XEXP (x, 1));
int c1 = INTVAL (XEXP (XEXP (x, 0), 1));
int combined = (code == IOR ? c0 | c1 : c0 ^ c1);
if (combined == 0)
return XEXP (XEXP (x, 0), 0);
if (!undobuf.storage)
undobuf.storage = (char *) oballoc (0);
SUBST (XEXP (x, 1), gen_rtx (CONST_INT, VOIDmode, combined));
SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
break;
}
case FLOAT:
/* (float (sign_extend <X>)) = (float <X>). */
if (was_replaced[0]
&& GET_CODE (to) == SIGN_EXTEND)
SUBST (XEXP (x, 0), XEXP (to, 0));
break;
case ZERO_EXTRACT:
/* (ZERO_EXTRACT (TRUNCATE x)...)
can become (ZERO_EXTRACT x ...). */
if (was_replaced[0]
&& GET_CODE (to) == TRUNCATE)
{
#ifdef BITS_BIG_ENDIAN
if (GET_CODE (XEXP (x, 2)) == CONST_INT)
{
if (!undobuf.storage)
undobuf.storage = (char *) oballoc (0);
/* On a big-endian machine, must increment the bit-number
since sign bit is farther away in the pre-truncated value. */
return gen_rtx (ZERO_EXTRACT, GET_MODE (x),
XEXP (to, 0),
XEXP (x, 1),
gen_rtx (CONST_INT, VOIDmode,
(INTVAL (XEXP (x, 2))
+ GET_MODE_BITSIZE (GET_MODE (XEXP (to, 0)))
- GET_MODE_BITSIZE (GET_MODE (to)))));
}
#else
SUBST (XEXP (x, 0), XEXP (to, 0));
#endif
}
/* Extracting a single bit from the result of a shift:
see which bit it was before the shift and extract that directly. */
if (was_replaced[0]
&& (GET_CODE (to) == ASHIFTRT || GET_CODE (to) == LSHIFTRT
|| GET_CODE (to) == ASHIFT || GET_CODE (to) == LSHIFT)
&& GET_CODE (XEXP (to, 1)) == CONST_INT
&& XEXP (x, 1) == const1_rtx
&& GET_CODE (XEXP (x, 2)) == CONST_INT)
{
int shift = INTVAL (XEXP (to, 1));
int newpos;
if (GET_CODE (to) == ASHIFT || GET_CODE (to) == LSHIFT)
shift = - shift;
#ifdef BITS_BIG_ENDIAN
shift = - shift;
#endif
newpos = INTVAL (XEXP (x, 2)) + shift;
if (newpos >= 0 &&
newpos < GET_MODE_BITSIZE (GET_MODE (to)))
{
if (!undobuf.storage)
undobuf.storage = (char *) oballoc (0);
return gen_rtx (ZERO_EXTRACT, GET_MODE (x),
XEXP (to, 0), const1_rtx,
gen_rtx (CONST_INT, VOIDmode, newpos));
}
}
break;
case LSHIFTRT:
case ASHIFTRT:
case ROTATE:
case ROTATERT:
#ifdef SHIFT_COUNT_TRUNCATED
/* (lshift <X> (sign_extend <Y>)) = (lshift <X> <Y>) (most machines).
True for all kinds of shifts and also for zero_extend. */
if (was_replaced[1]
&& (GET_CODE (to) == SIGN_EXTEND
|| GET_CODE (to) == ZERO_EXTEND)
&& FAKE_EXTEND_SAFE_P (GET_MODE (to), XEXP (to, 0)))
{
if (!undobuf.storage)
undobuf.storage = (char *) oballoc (0);
SUBST (XEXP (x, 1),
/* This is a perverse SUBREG, wider than its base. */
gen_lowpart_for_combine (GET_MODE (to), XEXP (to, 0)));
}
#endif
/* Two shifts in a row of same kind
in same direction with constant counts
may be combined. */
if (was_replaced[0]
&& GET_CODE (to) == GET_CODE (x)
&& GET_CODE (XEXP (x, 1)) == CONST_INT
&& GET_CODE (XEXP (to, 1)) == CONST_INT
&& INTVAL (XEXP (to, 1)) > 0
&& INTVAL (XEXP (x, 1)) > 0
&& (INTVAL (XEXP (x, 1)) + INTVAL (XEXP (to, 1))
< GET_MODE_BITSIZE (GET_MODE (x))))
{
if (!undobuf.storage)
undobuf.storage = (char *) oballoc (0);
return gen_rtx (GET_CODE (x), GET_MODE (x),
XEXP (to, 0),
gen_rtx (CONST_INT, VOIDmode,
INTVAL (XEXP (x, 1))
+ INTVAL (XEXP (to, 1))));
}
break;
case LSHIFT:
case ASHIFT:
#ifdef SHIFT_COUNT_TRUNCATED
/* (lshift <X> (sign_extend <Y>)) = (lshift <X> <Y>) (most machines).
True for all kinds of shifts and also for zero_extend. */
if (was_replaced[1]
&& (GET_CODE (to) == SIGN_EXTEND
|| GET_CODE (to) == ZERO_EXTEND)
&& FAKE_EXTEND_SAFE_P (GET_MODE (to), XEXP (to, 0)))
{
if (!undobuf.storage)
undobuf.storage = (char *) oballoc (0);
SUBST (XEXP (x, 1),
/* This is a perverse SUBREG, wider than its base. */
gen_lowpart_for_combine (GET_MODE (to), XEXP (to, 0)));
}
#endif
/* (lshift (and (lshiftrt <foo> <X>) <Y>) <X>)
happens copying between bit fields in similar structures.
It can be replaced by one and instruction.
It does not matter whether the shifts are logical or arithmetic. */
if (GET_CODE (XEXP (x, 0)) == AND
&& GET_CODE (XEXP (x, 1)) == CONST_INT
&& INTVAL (XEXP (x, 1)) > 0
&& GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
&& XEXP (XEXP (x, 0), 0) == to
&& (GET_CODE (to) == LSHIFTRT
|| GET_CODE (to) == ASHIFTRT)
#if 0
/* I now believe this restriction is unnecessary.
The outer shift will discard those bits in any case, right? */
/* If inner shift is arithmetic, either it shifts left or
the bits it shifts the sign into are zeroed by the and. */
&& (INTVAL (XEXP (x, 1)) < 0
|| ((unsigned) INTVAL (XEXP (XEXP (x, 0), 1))
< 1 << (GET_MODE_BITSIZE (GET_MODE (x))
- INTVAL (XEXP (x, 0)))))
#endif
&& GET_CODE (XEXP (to, 1)) == CONST_INT
&& INTVAL (XEXP (x, 1)) == INTVAL (XEXP (to, 1)))
{
if (!undobuf.storage)
undobuf.storage = (char *) oballoc (0);
/* The constant in the new `and' is <Y> << <X>
but clear out all bits that don't belong in our mode. */
return gen_rtx (AND, GET_MODE (x), XEXP (to, 0),
gen_rtx (CONST_INT, VOIDmode,
(GET_MODE_MASK (GET_MODE (x))
& ((GET_MODE_MASK (GET_MODE (x))
& INTVAL (XEXP (XEXP (x, 0), 1)))
<< INTVAL (XEXP (x, 1))))));
}
/* Two shifts in a row in same direction with constant counts
may be combined. */
if (was_replaced[0]
&& (GET_CODE (to) == ASHIFT || GET_CODE (to) == LSHIFT)
&& GET_CODE (XEXP (x, 1)) == CONST_INT
&& GET_CODE (XEXP (to, 1)) == CONST_INT
&& INTVAL (XEXP (to, 1)) > 0
&& INTVAL (XEXP (x, 1)) > 0
&& (INTVAL (XEXP (x, 1)) + INTVAL (XEXP (to, 1))
< GET_MODE_BITSIZE (GET_MODE (x))))
{
if (!undobuf.storage)
undobuf.storage = (char *) oballoc (0);
return gen_rtx (GET_CODE (x), GET_MODE (x),
XEXP (to, 0),
gen_rtx (CONST_INT, VOIDmode,
INTVAL (XEXP (x, 1))
+ INTVAL (XEXP (to, 1))));
}
/* (ashift (ashiftrt <foo> <X>) <X>)
(or, on some machines, (ashift (ashift <foo> <-X>) <X>) instead)
happens if you divide by 2**N and then multiply by 2**N.
It can be replaced by one `and' instruction.
It does not matter whether the shifts are logical or arithmetic. */
if (GET_CODE (XEXP (x, 1)) == CONST_INT
&& INTVAL (XEXP (x, 1)) > 0
&& was_replaced[0]
&& (((GET_CODE (to) == LSHIFTRT || GET_CODE (to) == ASHIFTRT)
&& GET_CODE (XEXP (to, 1)) == CONST_INT
&& INTVAL (XEXP (x, 1)) == INTVAL (XEXP (to, 1)))
||
((GET_CODE (to) == LSHIFT || GET_CODE (to) == ASHIFT)
&& GET_CODE (XEXP (to, 1)) == CONST_INT
&& INTVAL (XEXP (x, 1)) == - INTVAL (XEXP (to, 1)))))
{
if (!undobuf.storage)
undobuf.storage = (char *) oballoc (0);
/* The constant in the new `and' is -1 << <X>
but clear out all bits that don't belong in our mode. */
return gen_rtx (AND, GET_MODE (x), XEXP (to, 0),
gen_rtx (CONST_INT, VOIDmode,
(GET_MODE_MASK (GET_MODE (x))
& (GET_MODE_MASK (GET_MODE (x))
<< INTVAL (XEXP (x, 1))))));
}
}
return x;
}
/* This is the AND case of the function subst. */
static rtx
simplify_and_const_int (x, to)
rtx x, to;
{
register rtx varop = XEXP (x, 0);
register int constop = INTVAL (XEXP (x, 1));
/* (and (subreg (and <foo> <constant>) 0) <constant>)
results from an andsi followed by an andqi,
which happens frequently when storing bit-fields
on something whose result comes from an andsi. */
if (GET_CODE (varop) == SUBREG
&& XEXP (varop, 0) == to
&& subreg_lowpart_p (varop)
&& GET_CODE (to) == AND
&& GET_CODE (XEXP (to, 1)) == CONST_INT
/* Verify that the result of the outer `and'
is not affected by any bits not defined in the inner `and'.
True if the outer mode is narrower, or if the outer constant
masks to zero all the bits that the inner mode doesn't have. */
&& (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (GET_MODE (to))
|| (constop & ~ GET_MODE_MASK (GET_MODE (to))) == 0))
{
if (!undobuf.storage)
undobuf.storage = (char *) oballoc (0);
return gen_rtx (AND, GET_MODE (x),
gen_lowpart_for_combine (GET_MODE (x), XEXP (to, 0)),
gen_rtx (CONST_INT, VOIDmode,
constop
/* Remember that the bits outside that mode
are not being changed, so the effect
is as if they were all 1. */
& INTVAL (XEXP (to, 1))));
}
/* (and:SI (zero_extract:SI ...) <constant>)
results from an andsi following a byte-fetch on risc machines.
When the constant includes all bits extracted, eliminate the `and'. */
if (GET_CODE (varop) == ZERO_EXTRACT
&& GET_CODE (XEXP (varop, 1)) == CONST_INT
/* The `and' must not clear any bits that the extract can give. */
&& (~ constop & ((1 << INTVAL (XEXP (varop, 1))) - 1)) == 0)
return varop;
/* (and (zero_extend <foo>) <constant>)
often results from storing in a bit-field something
that was calculated as a short. Replace with a single `and'
in whose constant all bits not in <foo>'s mode are zero. */
if (varop == to
&& GET_CODE (to) == ZERO_EXTEND
&& FAKE_EXTEND_SAFE_P (GET_MODE (x), XEXP (to, 0)))
{
if (!undobuf.storage)
undobuf.storage = (char *) oballoc (0);
return gen_rtx (AND, GET_MODE (x),
/* This is a perverse SUBREG, wider than its base. */
gen_lowpart_for_combine (GET_MODE (x), XEXP (to, 0)),
gen_rtx (CONST_INT, VOIDmode,
constop & GET_MODE_MASK (GET_MODE (XEXP (to, 0)))));
}
/* (and (sign_extend <foo>) <constant>)
can be replaced with (and (subreg <foo>) <constant>)
if <constant> is narrower than <foo>'s mode,
or with (zero_extend <foo>) if <constant> is a mask for that mode. */
if (varop == to
&& GET_CODE (to) == SIGN_EXTEND
&& ((unsigned) constop <= GET_MODE_MASK (GET_MODE (XEXP (to, 0))))
&& FAKE_EXTEND_SAFE_P (GET_MODE (x), XEXP (to, 0)))
{
if (!undobuf.storage)
undobuf.storage = (char *) oballoc (0);
if (constop == GET_MODE_MASK (GET_MODE (XEXP (to, 0))))
return gen_rtx (ZERO_EXTEND, GET_MODE (x), XEXP (to, 0));
return gen_rtx (AND, GET_MODE (x),
/* This is a perverse SUBREG, wider than its base. */
gen_lowpart_for_combine (GET_MODE (x), XEXP (to, 0)),
XEXP (x, 1));
}
/* (and (and <foo> <constant>) <constant>)
comes from two and instructions in a row. */
if (varop == to
&& GET_CODE (to) == AND
&& GET_CODE (XEXP (to, 1)) == CONST_INT)
{
if (!undobuf.storage)
undobuf.storage = (char *) oballoc (0);
return gen_rtx (AND, GET_MODE (x),
XEXP (to, 0),
gen_rtx (CONST_INT, VOIDmode,
constop
& INTVAL (XEXP (to, 1))));
}
/* (and (ashiftrt (ashift FOO N) N) CONST)
may be simplified to (and FOO CONST) if CONST masks off the bits
changed by the two shifts. */
if (GET_CODE (varop) == ASHIFTRT
&& GET_CODE (XEXP (varop, 1)) == CONST_INT
&& XEXP (varop, 0) == to
&& GET_CODE (to) == ASHIFT
&& GET_CODE (XEXP (to, 1)) == CONST_INT
&& INTVAL (XEXP (varop, 1)) == INTVAL (XEXP (to, 1))
&& ((unsigned) constop >> INTVAL (XEXP (varop, 1))) == 0)
{
if (!undobuf.storage)
undobuf.storage = (char *) oballoc (0);
/* If CONST is a mask for the low byte,
change this into a zero-extend instruction
from just the low byte of FOO. */
if (constop == GET_MODE_MASK (QImode))
{
rtx temp = gen_lowpart_for_combine (QImode, XEXP (to, 0));
if (GET_CODE (temp) != CLOBBER)
return gen_rtx (ZERO_EXTEND, GET_MODE (x), temp);
}
return gen_rtx (AND, GET_MODE (x),
XEXP (to, 0), XEXP (x, 1));
}
/* (and (ashiftrt (zero_extend FOO) N) CONST)
may be simplified to (and (ashiftrt (subreg FOO) N) CONST)
if CONST masks off the bits changed by extension. */
if ((GET_CODE (varop) == ASHIFTRT || GET_CODE (varop) == LSHIFTRT)
&& GET_CODE (XEXP (varop, 1)) == CONST_INT
&& XEXP (varop, 0) == to
&& (GET_CODE (to) == ZERO_EXTEND || GET_CODE (to) == SIGN_EXTEND)
/* Verify the and discards all the extended bits. */
&& (((unsigned) constop << INTVAL (XEXP (varop, 1)))
>> GET_MODE_BITSIZE (GET_MODE (XEXP (to, 0)))) == 0
&& FAKE_EXTEND_SAFE_P (GET_MODE (x), XEXP (to, 0)))
{
if (!undobuf.storage)
undobuf.storage = (char *) oballoc (0);
SUBST (XEXP (varop, 0),
gen_lowpart_for_combine (GET_MODE (x), XEXP (to, 0)));
return x;
}
/* (and x const) may be converted to (zero_extend (subreg x 0)). */
if (constop == GET_MODE_MASK (QImode)
&& GET_CODE (varop) == REG)
{
if (!undobuf.storage)
undobuf.storage = (char *) oballoc (0);
return gen_rtx (ZERO_EXTEND, GET_MODE (x),
gen_rtx (SUBREG, QImode, varop, 0));
}
if (constop == GET_MODE_MASK (HImode)
&& GET_CODE (varop) == REG)
{
if (!undobuf.storage)
undobuf.storage = (char *) oballoc (0);
return gen_rtx (ZERO_EXTEND, GET_MODE (x),
gen_rtx (SUBREG, HImode, varop, 0));
}
/* No simplification applies. */
return 0;
}
/* Like gen_lowpart but for use by combine. In combine it is not possible
to create any new pseudoregs. However, it is safe to create
invalid memory addresses, because combine will try to recognize
them and all they will do is make the combine attempt fail.
If for some reason this cannot do its job, an rtx
(clobber (const_int 0)) is returned.
An insn containing that will not be recognized. */
#undef gen_lowpart
static rtx
gen_lowpart_for_combine (mode, x)
enum machine_mode mode;
register rtx x;
{
if (GET_CODE (x) == SUBREG || GET_CODE (x) == REG)
return gen_lowpart (mode, x);
if (GET_MODE (x) == mode)
return gen_rtx (CLOBBER, VOIDmode, const0_rtx);
if (GET_CODE (x) == MEM)
{
register int offset = 0;
/* Refuse to work on a volatile memory ref. */
if (MEM_VOLATILE_P (x))
return gen_rtx (CLOBBER, VOIDmode, const0_rtx);
/* If we want to refer to something bigger than the original memref,
generate a perverse subreg instead. That will force a reload
of the original memref X. */
if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode))
return gen_rtx (SUBREG, mode, x, 0);
#ifdef WORDS_BIG_ENDIAN
offset = (max (GET_MODE_SIZE (GET_MODE (x)), UNITS_PER_WORD)
- max (GET_MODE_SIZE (mode), UNITS_PER_WORD));
#endif
#ifdef BYTES_BIG_ENDIAN
/* Adjust the address so that the address-after-the-data
is unchanged. */
offset -= (min (UNITS_PER_WORD, GET_MODE_SIZE (mode))
- min (UNITS_PER_WORD, GET_MODE_SIZE (GET_MODE (x))));
#endif
return gen_rtx (MEM, mode, plus_constant (XEXP (x, 0),
offset));
}
else
return gen_rtx (CLOBBER, VOIDmode, const0_rtx);
}
/* After substitution, if the resulting pattern looks like
(set (cc0) (and ...)) or (set (cc0) (lshiftrt ...)),
this function is called to simplify the
pattern into a bit-field operation if possible. */
static void
simplify_set_cc0_and (insn)
rtx insn;
{
register rtx value = XEXP (PATTERN (insn), 1);
register rtx op0 = XEXP (value, 0);
register rtx op1 = XEXP (value, 1);
int offset = 0;
rtx var = 0;
rtx bitnum = 0;
int temp;
int unit;
rtx newpat;
if (GET_CODE (value) == AND)
{
op0 = XEXP (value, 0);
op1 = XEXP (value, 1);
}
else if (GET_CODE (value) == LSHIFTRT)
{
/* If there is no AND, but there is a shift that discards
all but the sign bit, we can pretend that the shift result
is ANDed with 1. Otherwise we cannot handle just a shift. */
if (GET_CODE (XEXP (value, 1)) == CONST_INT
&& (INTVAL (XEXP (value, 1))
== GET_MODE_BITSIZE (GET_MODE (value)) - 1))
{
op0 = value;
op1 = const1_rtx;
}
else
return;
}
else
abort ();
/* Look for a constant power of 2 or a shifted 1
on either side of the AND. Set VAR to the other side.
Set BITNUM to the shift count of the 1 (as an rtx).
Or, if bit number is constant, set OFFSET to the bit number. */
switch (GET_CODE (op0))
{
case CONST_INT:
temp = exact_log2 (INTVAL (op0));
if (temp < 0)
return;
offset = temp;
var = op1;
break;
case ASHIFT:
case LSHIFT:
if (XEXP (op0, 0) == const1_rtx)
{
bitnum = XEXP (op0, 1);
var = op1;
}
}
if (var == 0)
switch (GET_CODE (op1))
{
case CONST_INT:
temp = exact_log2 (INTVAL (op1));
if (temp < 0)
return;
offset = temp;
var = op0;
break;
case ASHIFT:
case LSHIFT:
if (XEXP (op1, 0) == const1_rtx)
{
bitnum = XEXP (op1, 1);
var = op0;
}
}
/* If VAR is 0, we didn't find something recognizable. */
if (var == 0)
return;
if (!undobuf.storage)
undobuf.storage = (char *) oballoc (0);
/* If the bit position is currently exactly 0,
extract a right-shift from the variable portion. */
if (offset == 0
&& (GET_CODE (var) == ASHIFTRT || GET_CODE (var) == LSHIFTRT))
{
bitnum = XEXP (var, 1);
var = XEXP (var, 0);
}
if (GET_CODE (var) == SUBREG && SUBREG_WORD (var) == 0)
var = SUBREG_REG (var);
/* Note that BITNUM and OFFSET are always little-endian thru here
even on a big-endian machine. */
#ifdef BITS_BIG_ENDIAN
unit = GET_MODE_BITSIZE (GET_MODE (var)) - 1;
if (bitnum != 0)
bitnum = gen_rtx (MINUS, SImode,
gen_rtx (CONST_INT, VOIDmode, unit), bitnum);
else
offset = unit - offset;
#endif
if (bitnum == 0)
bitnum = gen_rtx (CONST_INT, VOIDmode, offset);
newpat = gen_rtx (SET, VOIDmode, cc0_rtx,
gen_rtx (ZERO_EXTRACT, VOIDmode, var, const1_rtx, bitnum));
if (recog (newpat, insn) >= 0)
{
if (undobuf.num_undo < MAX_UNDO)
{
undobuf.undo[undobuf.num_undo].where = &XEXP (PATTERN (insn), 1);
undobuf.undo[undobuf.num_undo].old_contents = value;
XEXP (PATTERN (insn), 1) = XEXP (newpat, 1);
}
undobuf.num_undo++;
}
}
/* Update the records of when each REG was most recently set or killed
for the things done by INSN. This is the last thing done in processing
INSN in the combiner loop.
We update reg_last_set, reg_last_death, and also the similar information
mem_last_set (which insn most recently modified memory)
and last_call_cuid (which insn was the most recent subroutine call). */
static void
record_dead_and_set_regs (insn)
rtx insn;
{
register rtx link;
for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
{
if (REG_NOTE_KIND (link) == REG_DEAD)
reg_last_death[REGNO (XEXP (link, 0))] = insn;
else if (REG_NOTE_KIND (link) == REG_INC)
reg_last_set[REGNO (XEXP (link, 0))] = insn;
}
if (GET_CODE (insn) == CALL_INSN)
last_call_cuid = mem_last_set = INSN_CUID (insn);
if (GET_CODE (PATTERN (insn)) == PARALLEL)
{
register int i;
for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
{
register rtx elt = XVECEXP (PATTERN (insn), 0, i);
register enum rtx_code code = GET_CODE (elt);
if (code == SET || code == CLOBBER)
{
rtx dest = XEXP (elt, 0);
while (GET_CODE (dest) == SUBREG
|| GET_CODE (dest) == STRICT_LOW_PART
|| GET_CODE (dest) == SIGN_EXTRACT
|| GET_CODE (dest) == ZERO_EXTRACT)
dest = XEXP (dest, 0);
if (GET_CODE (dest) == REG)
reg_last_set[REGNO (dest)] = insn;
else if (GET_CODE (dest) == MEM)
mem_last_set = INSN_CUID (insn);
}
}
}
else if (GET_CODE (PATTERN (insn)) == SET
|| GET_CODE (PATTERN (insn)) == CLOBBER)
{
register rtx dest = XEXP (PATTERN (insn), 0);
while (GET_CODE (dest) == SUBREG
|| GET_CODE (dest) == STRICT_LOW_PART
|| GET_CODE (dest) == SIGN_EXTRACT
|| GET_CODE (dest) == ZERO_EXTRACT)
dest = XEXP (dest, 0);
if (GET_CODE (dest) == REG)
reg_last_set[REGNO (dest)] = insn;
else if (GET_CODE (dest) == MEM)
mem_last_set = INSN_CUID (insn);
}
}
/* Return nonzero if expression X refers to a REG or to memory
that is set in an instruction more recent than FROM_CUID. */
static int
use_crosses_set_p (x, from_cuid)
register rtx x;
int from_cuid;
{
register char *fmt;
register int i;
register enum rtx_code code = GET_CODE (x);
if (code == REG)
{
register int regno = REGNO (x);
#ifdef PUSH_ROUNDING
/* Don't allow uses of the stack pointer to be moved,
because we don't know whether the move crosses a push insn. */
if (regno == STACK_POINTER_REGNUM)
return 1;
#endif
return (reg_last_set[regno]
&& INSN_CUID (reg_last_set[regno]) > from_cuid);
}
if (code == MEM && mem_last_set > from_cuid)
return 1;
fmt = GET_RTX_FORMAT (code);
for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
{
if (fmt[i] == 'E')
{
register int j;
for (j = XVECLEN (x, i) - 1; j >= 0; j--)
if (use_crosses_set_p (XVECEXP (x, i, j), from_cuid))
return 1;
}
else if (fmt[i] == 'e'
&& use_crosses_set_p (XEXP (x, i), from_cuid))
return 1;
}
return 0;
}
/* Return nonzero if reg REGNO is marked as dying in INSN. */
int
regno_dead_p (regno, insn)
int regno;
rtx insn;
{
register rtx link;
for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
if ((REG_NOTE_KIND (link) == REG_DEAD
|| REG_NOTE_KIND (link) == REG_INC)
&& REGNO (XEXP (link, 0)) == regno)
return 1;
return 0;
}
/* Return nonzero if J is the first insn following I,
not counting labels, line numbers, etc.
We assume that J follows I. */
static int
adjacent_insns_p (i, j)
rtx i, j;
{
register rtx insn;
for (insn = NEXT_INSN (i); insn != j; insn = NEXT_INSN (insn))
if (GET_CODE (insn) == INSN
|| GET_CODE (insn) == CALL_INSN
|| GET_CODE (insn) == JUMP_INSN)
return 0;
return 1;
}
/* Check that X is an insn-body for an `asm' with operands
and that the operands mentioned in it are legitimate. */
static int
check_asm_operands (x)
rtx x;
{
int noperands = asm_noperands (x);
rtx *operands;
int i;
if (noperands < 0)
return 0;
if (noperands == 0)
return 1;
operands = (rtx *) alloca (noperands * sizeof (rtx));
decode_asm_operands (x, operands, 0, 0, 0);
for (i = 0; i < noperands; i++)
if (!general_operand (operands[i], VOIDmode))
return 0;
return 1;
}
/* Concatenate the list of logical links of OINSN
into INSN's list of logical links.
Modifies OINSN destructively.
If ALL_LINKS is nonzero, move all the links that OINSN has.
Otherwise, move only those that point to insns that set regs
that die in the insn OINSN.
Other links are clobbered so that they are no longer effective. */
static void
add_links (insn, oinsn, all_links)
rtx insn, oinsn;
int all_links;
{
register rtx links = LOG_LINKS (oinsn);
if (! all_links)
{
rtx tail;
for (tail = links; tail; tail = XEXP (tail, 1))
{
rtx target = XEXP (tail, 0);
if (GET_CODE (target) != INSN
|| GET_CODE (PATTERN (target)) != SET
|| GET_CODE (SET_DEST (PATTERN (target))) != REG
|| ! dead_or_set_p (oinsn, SET_DEST (PATTERN (target))))
/* OINSN is going to become a NOTE
so a link pointing there will have no effect. */
XEXP (tail, 0) = oinsn;
}
}
if (LOG_LINKS (insn) == 0)
LOG_LINKS (insn) = links;
else
{
register rtx next, prev = LOG_LINKS (insn);
while (next = XEXP (prev, 1))
prev = next;
XEXP (prev, 1) = links;
}
}
/* Delete any LOG_LINKS of INSN which point at OINSN. */
static void
remove_links (insn, oinsn)
rtx insn, oinsn;
{
register rtx next = LOG_LINKS (insn), prev = 0;
while (next)
{
if (XEXP (next, 0) == oinsn)
{
if (prev)
XEXP (prev, 1) = XEXP (next, 1);
else
LOG_LINKS (insn) = XEXP (next, 1);
}
else
prev = next;
next = XEXP (next, 1);
}
}
/* Concatenate the any elements of the list of reg-notes INCS
which are of type REG_INC
into INSN's list of reg-notes. */
static void
add_incs (insn, incs)
rtx insn, incs;
{
register rtx tail;
for (tail = incs; tail; tail = XEXP (tail, 1))
if (REG_NOTE_KIND (tail) == REG_INC)
REG_NOTES (insn)
= gen_rtx (EXPR_LIST, REG_INC, XEXP (tail, 0), REG_NOTES (insn));
}
/* Remove register number REGNO from the dead registers list of INSN. */
void
remove_death (regno, insn)
int regno;
rtx insn;
{
register rtx link, next;
while ((link = REG_NOTES (insn))
&& REG_NOTE_KIND (link) == REG_DEAD
&& REGNO (XEXP (link, 0)) == regno)
REG_NOTES (insn) = XEXP (link, 1);
if (link)
while (next = XEXP (link, 1))
{
if (REG_NOTE_KIND (next) == REG_DEAD
&& REGNO (XEXP (next, 0)) == regno)
XEXP (link, 1) = XEXP (next, 1);
else
link = next;
}
}
/* For each register (hardware or pseudo) used within expression X,
if its death is in an instruction with cuid
between FROM_CUID (inclusive) and TO_INSN (exclusive),
mark it as dead in TO_INSN instead.
This is done when X is being merged by combination into TO_INSN. */
static void
move_deaths (x, from_cuid, to_insn)
rtx x;
int from_cuid;
rtx to_insn;
{
register char *fmt;
register int len, i;
register enum rtx_code code = GET_CODE (x);
if (code == REG)
{
register rtx where_dead = reg_last_death[REGNO (x)];
if (where_dead && INSN_CUID (where_dead) >= from_cuid
&& INSN_CUID (where_dead) < INSN_CUID (to_insn))
{
remove_death (REGNO (x), reg_last_death[REGNO (x)]);
if (! dead_or_set_p (to_insn, x))
REG_NOTES (to_insn)
= gen_rtx (EXPR_LIST, REG_DEAD, x, REG_NOTES (to_insn));
}
return;
}
len = GET_RTX_LENGTH (code);
fmt = GET_RTX_FORMAT (code);
for (i = 0; i < len; i++)
{
if (fmt[i] == 'E')
{
register int j;
for (j = XVECLEN (x, i) - 1; j >= 0; j--)
move_deaths (XVECEXP (x, i, j), from_cuid, to_insn);
}
else if (fmt[i] == 'e')
move_deaths (XEXP (x, i), from_cuid, to_insn);
}
}
/* Like move_deaths, but deaths are moving both forward
(from FROM_CUID to TO_INSN), and backwards
(from FROM_INSN to TO_INSN). This is what happens
when an insn is removed after applying the distributive law. */
static void
move_deaths_2 (x, from_cuid, from_insn, to_insn)
rtx x;
int from_cuid;
rtx from_insn, to_insn;
{
register char *fmt;
register int len, i;
register enum rtx_code code = GET_CODE (x);
if (code == REG)
{
register rtx where_dead = reg_last_death[REGNO (x)];
if (where_dead && INSN_CUID (where_dead) >= from_cuid
&& INSN_CUID (where_dead) < INSN_CUID (to_insn))
{
remove_death (REGNO (x), reg_last_death[REGNO (x)]);
if (! dead_or_set_p (to_insn, x))
REG_NOTES (to_insn)
= gen_rtx (EXPR_LIST, REG_DEAD, x, REG_NOTES (to_insn));
}
/* Can't use where_dead for from_insn because it has
not been computed yet. */
else if (dead_or_set_p (from_insn, x))
{
remove_death (REGNO (x), from_insn);
if (! dead_or_set_p (to_insn, x))
REG_NOTES (to_insn)
= gen_rtx (EXPR_LIST, REG_DEAD, x, REG_NOTES (to_insn));
}
return;
}
len = GET_RTX_LENGTH (code);
fmt = GET_RTX_FORMAT (code);
for (i = 0; i < len; i++)
{
if (fmt[i] == 'E')
{
register int j;
for (j = XVECLEN (x, i) - 1; j >= 0; j--)
move_deaths_2 (XVECEXP (x, i, j), from_cuid, from_insn, to_insn);
}
else if (fmt[i] == 'e')
move_deaths_2 (XEXP (x, i), from_cuid, from_insn, to_insn);
}
}
/* The distrib combiner rewrites groups of insns so that optimizations
can be more easily recognized. The front-end does not know how to
group certain kinds of operations for efficient execution, and the
resulting code can be quite poor. For example, on a machine without
bitfield instructions, bitfield references look like
(and (lshiftrt ... n) m)
When combining two bitfield operations, such as with ||, this can
yield code like
(set z
(or (and (lshiftrt x n) 1)
(and (lshiftrt y n) 1)))
which can be more efficiently executed as
(set z
(lshiftrt (and (or x y)
(1 << m)) n))
From there, the combiner attempts to rewrite the insns,
keeping flow information accurate for later passes,
and reducing the total number of insns executed.
This function returns the point at which we should try
looking for more simplifications. This will be before
INSN if the call succeeds. We do not need to fear
infinite loops, since this function is guaranteed to
eliminate at least one (non-note) instruction if it returns
successfully. */
static rtx
try_distrib (insn, xprev1, xprev2)
rtx insn, xprev1, xprev2;
{
rtx pat = PATTERN (insn);
rtx prev1, prev2, pat1, pat2, src1, src2;
rtx to_prev, to_insn;
enum rtx_code code;
int insn_code_number, prev_code_number, regno;
rtx new_insn_pat, new_prev_pat;
distrib_attempts++;
/* ??? Need to implement a test that PREV2 and PREV1
are completely independent. Right now their
recognition ability is sufficiently limited that
it should not be necessary, but better safe than sorry. */
/* Let PREV1 be the later of the two insns, and PREV2 the earlier. */
if (INSN_CUID (xprev1) > INSN_CUID (xprev2))
{
prev1 = xprev1;
prev2 = xprev2;
}
else
{
prev1 = xprev2;
prev2 = xprev1;
}
pat1 = PATTERN (prev1);
pat2 = PATTERN (prev2);
/* First, see if INSN, PREV1, and PREV2 have patterns we can expect
to simplify. */
if (GET_CODE (pat) != SET
|| GET_CODE (pat1) != SET
|| GET_CODE (pat2) != SET)
return 0;
code = GET_CODE (SET_SRC (pat));
src1 = SET_SRC (pat1);
src2 = SET_SRC (pat2);
if (GET_CODE (SET_DEST (pat1)) != REG
|| GET_CODE (SET_DEST (pat2)) != REG)
return 0;
switch (code)
{
default:
return 0;
case IOR:
case AND:
case XOR:
case PLUS:
;
}
/* Insns PREV1 and PREV2 must provide the two operands of the arithmetic
that is done in INSN. */
if (! ((XEXP (SET_SRC (pat), 0) == SET_DEST (pat1)
&& XEXP (SET_SRC (pat), 1) == SET_DEST (pat2))
||
(XEXP (SET_SRC (pat), 0) == SET_DEST (pat2)
&& XEXP (SET_SRC (pat), 1) == SET_DEST (pat1))))
return 0;
/* They must not be used in any other way in INSN.
In particular, they must not be used in a result memory address. */
if (reg_mentioned_p (SET_DEST (pat1), SET_DEST (pat))
|| reg_mentioned_p (SET_DEST (pat2), SET_DEST (pat)))
return 0;
/* Give up if the two operands' modes don't match. */
if (GET_MODE (src1) != GET_MODE (src2))
return 0;
/* PREV1 and PREV2 must compute the same operation.
Actually, there are other cases that could be handled,
but are not implemented. For example:
(set (reg:SI 94)
(and:SI (reg:SI 73)
(const_int 223)))
(set (reg:SI 95)
(zero_extend:SI (subreg:QI (reg:SI 91) 0)))
(set (reg:SI 96)
(ior:SI (reg:SI 94)
(reg:SI 95)))
In this case, we know that because (reg:SI 94) has
been anded with 223, there is no need to zero_extend
(reg:SI 91), and we could eliminate (reg:SI 95). */
if (GET_CODE (src1) != GET_CODE (src2))
return 0;
/* The SETs in PREV1 and PREV2 do not need to be kept around. */
undobuf.num_undo = 0;
undobuf.storage = 0;
/* Substitute in the latest insn for the regs set by the earlier ones. */
subst_insn = insn;
n_occurrences = 0; /* `subst' counts here */
switch (GET_CODE (src1))
{
/* case XOR: Does not distribute through anything! */
case LSHIFTRT:
case ASHIFTRT:
/* Right-shift can't distribute through addition
since the round-off would happen differently. */
case AND:
case IOR:
/* Boolean ops don't distribute through addition. */
if (code == PLUS)
return 0;
goto do_distrib;
case LSHIFT:
case ASHIFT:
/* Left shifts are multiplication; they distribute through
addition. Also, since they work bitwise, they
distribute through boolean operations. */
#ifdef NEGATIVE_SHIFT_COUNTS
/* Negative count is really a right-shift. */
if (NEGATIVE_SHIFT_COUNTS
&& code == PLUS
&& !(GET_CODE (XEXP (src1, 1))
== CONST_INT && INTVAL (XEXP (src1, 1)) >= 0))
return 0;
#endif
goto do_distrib;
case MULT:
/* Multiplication distributes through addition only. */
if (code != PLUS)
return 0;
do_distrib:
if (GET_CODE (XEXP (src1, 1)) != CONST_INT
|| GET_CODE (XEXP (src2, 1)) != CONST_INT
|| INTVAL (XEXP (src1, 1)) != INTVAL (XEXP (src2, 1)))
return 0;
/* Give up if we would move a use of a reg across an alteration.
Note this is unnecessarily conservative, since a problem really
happens only if this reg is set *between* PREV2 and PREV1
But this test is easier. */
if (use_crosses_set_p (XEXP (src2, 0), INSN_CUID (prev2)))
return 0;
/* Try changing (+ (* x c) (* y c)) to (* (+ x y) c). */
to_prev = gen_rtx (code, GET_MODE (src1),
XEXP (src1, 0), XEXP (src2, 0));
to_insn = gen_rtx (GET_CODE (src1), GET_MODE (src1), SET_DEST (pat1), XEXP (src1, 1));
break;
case ZERO_EXTEND:
case SIGN_EXTEND:
/* Extension can't distribute through addition;
the carries could be changed. */
if (code == PLUS)
return 0;
{
rtx inner1 = XEXP (src1, 0), inner2 = XEXP (src2, 0);
int subreg_needed = 0;
/* Try changing (& (extend x) (extend y)) to (extend (& x y)). */
/* But keep extend insns together with their subregs. */
if (GET_CODE (inner1) == SUBREG)
{
if (SUBREG_WORD (inner1) != 0)
return 0;
else
{
subreg_needed = 1;
inner1 = SUBREG_REG (inner1);
}
}
if (GET_CODE (inner2) == SUBREG)
{
if (SUBREG_WORD (inner2) != 0)
return 0;
else
{
subreg_needed = 1;
inner2 = SUBREG_REG (inner2);
}
}
/* Give up if we would move a use of a reg across an alteration.
Note this is unnecessarily conservative, since a problem really
happens only if this reg is set *between* PREV2 and PREV1
But this test is easier. */
if (use_crosses_set_p (inner2, INSN_CUID (prev2)))
return 0;
to_prev = gen_rtx (code, GET_MODE (src1), inner1, inner2);
to_insn = gen_rtx (GET_CODE (src1), GET_MODE (src1),
subreg_needed
? gen_rtx (SUBREG, GET_MODE (XEXP (src1, 0)),
SET_DEST (pat1), 0)
: SET_DEST (pat1));
}
break;
default:
return 0;
}
/* Are the results of this "substitution" a valid instruction? */
new_insn_pat = subst (PATTERN (insn), SET_SRC (PATTERN (insn)), to_insn);
distrib_merges_1++;
insn_code_number = recog (new_insn_pat, insn);
if (insn_code_number < 0)
{
undo_all ();
return 0;
}
subst_insn = prev1;
new_prev_pat = subst (pat1, src1, to_prev);
distrib_merges_2++;
prev_code_number = recog (new_prev_pat, prev1);
if (prev_code_number < 0)
{
undo_all ();
return 0;
}
/* Everything worked; install the new patterns. */
INSN_CODE (insn) = insn_code_number;
PATTERN (insn) = new_insn_pat;
INSN_CODE (prev1) = prev_code_number;
PATTERN (prev1) = new_prev_pat;
/* Need to change LOG_LINKS around...PREV1 now gets
whatever flowed into PREV2. PREV2 is going to
become a NOTE, so we clear out its LOG_LINKS. */
remove_links (insn, prev2);
add_links (prev1, prev2, adjacent_insns_p (prev2, prev1));
/* Registers which died in PREV2 now die in PREV1.
Also, registers born in PREV2 dying in INSN now die in PREV1. */
move_deaths_2 (src2, INSN_CUID (prev2), insn, prev1);
regno = REGNO (SET_DEST (pat2));
reg_n_sets[regno]--;
if (reg_n_sets[regno] == 0
&& ! (basic_block_live_at_start[0][regno / HOST_BITS_PER_INT]
& (1 << (regno % HOST_BITS_PER_INT))))
reg_n_refs[regno] = 0;
remove_death (regno, insn);
PUT_CODE (prev2, NOTE);
NOTE_LINE_NUMBER (prev2) = NOTE_INSN_DELETED;
NOTE_SOURCE_FILE (prev2) = 0;
distrib_successes++;
return prev1;
}
void
dump_combine_stats (file)
FILE *file;
{
fprintf
(file,
";; Combiner statistics: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n\n",
combine_attempts, combine_merges, combine_extras, combine_successes);
fprintf
(file,
";; Distributer statistics: %d attempts, %d:%d substitutions,\n;; %d successes.\n\n",
distrib_attempts, distrib_merges_1,
distrib_merges_2, distrib_successes);
}
void
dump_combine_total_stats (file)
FILE *file;
{
fprintf
(file,
"\n;; Combiner totals: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n",
total_attempts, total_merges, total_extras, total_successes);
fprintf
(file,
"\n;; Distributer totals: %d attempts, %d:%d substitutions,\n;; %d successes.\n",
total_distrib_attempts, total_distrib_merges_1,
total_distrib_merges_2, total_distrib_successes);
}
|