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
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
|
/* Move constant computations out of loops.
Copyright (C) 1987, 1988, 1989 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 is the loop optimization pass of the compiler.
It finds invariant computations within loops and moves them
to the beginning of the loop. Then it identifies basic and
general induction variables. Strength reduction is applied to the general
induction variables, and induction variable elimination is applied to
the basic induction variables.
It also finds cases where
a register is set within the loop by zero-extending a narrower value
and changes these to zero the entire register once before the loop
and merely copy the low part within the loop.
Most of the complexity is in heuristics to decide when it is worth
while to do these things. */
/* ??? verify_loop would run faster if we made one table
of the minimum and maximum luids from which each label is reached.
Also, it would be faster if loop_store_addrs were a hash table. */
#include "config.h"
#include "rtl.h"
#include "expr.h"
#include "insn-config.h"
#include "regs.h"
#include "hard-reg-set.h"
#include "recog.h"
#include "flags.h"
#include <stdio.h>
/* Vector mapping INSN_UIDs to luids.
The luids are like uids but increase monononically always.
We use them to see whether a jump comes from outside a given loop. */
static int *uid_luid;
/* Get the luid of an insn. */
#define INSN_LUID(INSN) (uid_luid[INSN_UID (INSN)])
/* 1 + largest uid of any insn. */
static int max_uid;
/* 1 + luid of last insn. */
static int max_luid;
/* Nonzero if somewhere in the current loop
there is either a subroutine call,
or a store into a memory address that is not fixed,
or a store in a BLKmode memory operand,
or too many different fixed addresses stored in
to record them all in `loop_store_addrs'.
In any of these cases, no memory location can be regarded
as invariant. */
static int unknown_address_altered;
/* Nonzero if somewhere in the current loop there is a store
into a memory address that is not fixed but is known to be
part of an aggregate.
In this case, no memory reference in an aggregate may be
considered invariant. */
static int unknown_aggregate_altered;
/* Nonzero if somewhere in the current loop there is a store
into a memory address other than a fixed address not in an aggregate.
In this case, no memory reference in an aggregate at a varying address
may be considered invariant. */
static int fixed_aggregate_altered;
/* Nonzero if there is a subroutine call in the current loop.
(unknown_address_altered is also nonzero in this case.) */
static int loop_has_call;
/* Added loop_continue which is the NOTE_INSN_LOOP_CONT of the
current loop. A continue statement will generate a branch to
NEXT_INSN (loop_continue). */
static rtx loop_continue;
/* Indexed by register number, contains the number of times the reg
is set during the loop being scanned.
During code motion, -1 indicates a reg that has been made a candidate.
After code motion, regs moved have 0 (which is accurate now)
while the failed candidates have the original number of times set.
Therefore, at all times, 0 indicates an invariant register;
-1 a conditionally invariant one. */
static short *n_times_set;
/* Original value of n_times_set; same except that this value
is not set to -1 for a reg whose sets have been made candidates
and not set to 0 for a reg that is moved. */
static short *n_times_used;
/* Index by register number, 1 indicates that the register
cannot be moved or strength reduced. */
static char *may_not_optimize;
/* Nonzero means reg N has already been moved out of one loop.
This reduces the desire to move it out of another. */
static char *moved_once;
/* Array of fixed memory addresses that are stored in this loop.
If there are too many to fit here,
we just turn on unknown_address_altered. */
#define NUM_STORES 10
static rtx loop_store_addrs[NUM_STORES];
static int loop_store_widths[NUM_STORES];
/* Index of first available slot in above array. */
static int loop_store_addrs_idx;
/* Count of movable (i.e. invariant) instructions discovered in the loop. */
static int num_movables;
/* Count of memory write instructions discovered in the loop. */
static int num_mem_sets;
/* Number of loops contained within the current one, including itself. */
static int loops_enclosed;
/* Bound on pseudo register number before loop optimization.
A pseudo has valid regscan info if its number is < old_max_reg. */
static int old_max_reg;
/* During the analysis of a loop, a chain of `struct movable's
is made to record all the movable insns found.
Then the entire chain can be scanned to decide which to move. */
struct movable
{
rtx insn; /* A movable insn */
rtx set_src; /* The expression this reg is set from.
Either SET_SRC (body) or a REG_EQUAL. */
int consec; /* Number of consecutive following insns
that must be moved with this one. */
int regno; /* The register it sets */
short lifetime; /* lifetime of that register;
may be adjusted when matching movables
that load the same value are found. */
short savings; /* Number of insns we can move for this reg,
including other movables that force this
or match this one. */
unsigned int cond : 1; /* 1 if only conditionally movable */
unsigned int force : 1; /* 1 means MUST move this insn */
unsigned int global : 1; /* 1 means reg is live outside this loop */
/* If PARTIAL is 1, GLOBAL means something different:
that the reg is live outside the range from where it is set
to the following label. */
unsigned int done : 1; /* 1 inhibits further processing of this */
/* 1 in PARTIAL means this reg is used for zero-extending.
In particular, moving it does not make it invariant. */
unsigned int partial : 1;
enum machine_mode savemode; /* Nonzero means it is a mode for a low part
that we should avoid changing when clearing
the rest of the reg. */
struct movable *match; /* First entry for same value */
struct movable *forces; /* An insn that must be moved if this is */
struct movable *next;
};
static FILE *loop_dump_stream;
/* Forward declarations. */
struct induction;
struct iv_class;
static rtx loop_find_reg_equal ();
static int reg_in_basic_block_p ();
static rtx verify_loop ();
static int invariant_p ();
static int consec_sets_invariant_p ();
static int can_jump_into_range_p ();
static int labels_in_range_p ();
static void count_loop_regs_set ();
static void note_addr_stored ();
static int loop_reg_used_before_p ();
static void constant_high_bytes ();
static void scan_loop ();
static rtx replace_regs ();
static void replace_call_address ();
static rtx skip_consec_insns ();
static void ignore_some_movables ();
static void force_movables ();
static void combine_movables ();
static int rtx_equal_for_loop_p ();
static void move_movables ();
static void strength_reduce ();
static void find_mem_givs ();
static void record_giv ();
static void delete_insn_forces ();
static int basic_induction_var ();
static int general_induction_var ();
static int consec_sets_giv ();
static int check_dbra_loop ();
static void emit_iv_init_code ();
static int product_cheap_p ();
static void emit_iv_inc ();
static void check_eliminate_biv ();
static int can_eliminate_biv_p ();
static void eliminate_biv ();
static rtx final_biv_value ();
static int last_use_this_basic_block ();
/* Entry point of this file. Perform loop optimization
on the current function. F is the first insn of the function
and DUMPFILE is a stream for output of a trace of actions taken
(or 0 if none should be output). */
void
loop_optimize (f, dumpfile)
/* f is the first instruction of a chain of insns for one function */
rtx f;
FILE *dumpfile;
{
register rtx insn;
register int i;
rtx end;
rtx last_insn;
loop_dump_stream = dumpfile;
init_recog ();
old_max_reg = max_reg_num ();
moved_once = (char *) alloca (old_max_reg);
bzero (moved_once, old_max_reg);
/* First find the last real insn, and count the number of insns,
and assign insns their luids. */
for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
if (INSN_UID (insn) > i)
i = INSN_UID (insn);
max_uid = i + 1;
uid_luid = (int *) alloca ((i + 1) * sizeof (int));
bzero (uid_luid, (i + 1) * sizeof (int));
/* Compute the mapping from uids to luids.
LUIDs are numbers assigned to insns, like uids,
except that luids increase monotonically through the code.
Don't assign luids to line-number NOTEs, so that the distance in luids
between two insns is not affected by -g. */
for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
{
last_insn = insn;
if (GET_CODE (insn) != NOTE
|| NOTE_LINE_NUMBER (insn) < 0)
INSN_LUID (insn) = ++i;
else
/* Give a line number note the same luid as preceding insn. */
INSN_LUID (insn) = i;
}
max_luid = i;
/* Don't leave gaps in uid_luid for insns that have been
deleted. It is possible that the first or last insn
using some register has been deleted by cross-jumping.
Make sure that uid_luid for that former insn's uid
points to the general area where that insn used to be. */
for (i = 0; i < max_uid; i++)
{
uid_luid[0] = uid_luid[i];
if (uid_luid[0] != 0)
break;
}
for (i = 0; i < max_uid; i++)
if (uid_luid[i] == 0)
uid_luid[i] = uid_luid[i - 1];
/* Find and process each loop.
We scan from the end, and process each loop when its start is seen,
so we process innermost loops first. */
for (insn = last_insn; insn; insn = PREV_INSN (insn))
if (GET_CODE (insn) == NOTE
&& NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
{
/* Make sure it really is a loop -- no jumps in from outside. */
end = verify_loop (f, insn);
if (end != 0)
/* If so, optimize this loop. */
scan_loop (insn, end, max_reg_num ());
else if (loop_dump_stream)
fprintf (loop_dump_stream,
"\nLoop at %d ignored due to multiple entry points.\n",
INSN_UID (insn));
}
}
/* Optimize one loop whose start is LOOP_START and end is END.
LOOP_START is the NOTE_INSN_LOOP_BEG and END is the matching
NOTE_INSN_LOOP_END. */
/* ??? can also move memory writes out of loop if destination
address is invariant? */
static void
scan_loop (loop_start, end, nregs)
rtx loop_start, end;
int nregs;
{
register int i;
register rtx p = NEXT_INSN (loop_start);
/* 1 if we are scanning insns that could be executed zero times. */
int maybe_never = 0;
/* 1 if we are scanning insns that might never be executed
due to a subroutine call which might exit before they are reached. */
int call_passed = 0;
/* For a rotated loop that is entered near the bottom,
this is the label at the top. Otherwise it is zero. */
rtx loop_top = 0;
/* Jump insn that enters the loop, or 0 if control drops in. */
rtx loop_entry_jump = 0;
/* Place in the loop where control enters. */
rtx scan_start;
/* Number of insns in the loop. */
int insn_count;
int tem;
rtx temp;
/* Chain describing insns movable in current loop. */
struct movable *movables = 0;
/* Last element in `movables' -- so we can add elements at the end. */
struct movable *last_movable = 0;
/* Ratio of extra register life span we can justify
for saving an instruction. More if loop doesn't call subroutines
since in that case saving an insn makes more difference
and more registers are available. */
int threshold = loop_has_call ? 15 : 30;
/* Nonzero if the insn that jumps into the real loop
is not the very first thing after the loop-beginning note. */
int something_before_entry_jump = 0;
n_times_set = (short *) alloca (nregs * sizeof (short));
n_times_used = (short *) alloca (nregs * sizeof (short));
may_not_optimize = (char *) alloca (nregs);
/* Determine whether this loop starts with a jump down
to a test at the end. */
while (p != end
&& GET_CODE (p) != CODE_LABEL && GET_CODE (p) != JUMP_INSN)
{
if (GET_CODE (p) == CALL_INSN || GET_CODE (p) == INSN)
something_before_entry_jump = 1;
p = NEXT_INSN (p);
}
/* "Loop" contains neither jumps nor labels;
it must have been a dummy. Think no more about it. */
if (p == end)
return;
scan_start = p;
/* If loop has a jump before the first label,
the true entry is the target of that jump.
Start scan from there.
But record in LOOP_TOP the place where the end-test jumps
back to so we can scan that after the end of the loop. */
if (GET_CODE (p) == JUMP_INSN)
{
loop_entry_jump = p;
loop_top = NEXT_INSN (p);
/* Loop entry will never be a conditional jump.
If we see one, this must not be a real loop.
Also, a return-insn isn't a jump to enter the loop. */
if (GET_CODE (loop_top) != BARRIER
|| GET_CODE (PATTERN (p)) != SET)
return;
/* Get the label at which the loop is entered. */
p = XEXP (SET_SRC (PATTERN (p)), 0);
/* Check to see whether the jump actually
jumps out of the loop (meaning it's no loop).
This case can happen for things like
do {..} while (0). */
if (p == 0
|| INSN_LUID (p) < INSN_LUID (loop_start)
|| INSN_LUID (p) >= INSN_LUID (end))
{
if (loop_dump_stream)
fprintf (loop_dump_stream, "\nLoop from %d to %d is phony.\n\n",
INSN_UID (loop_start), INSN_UID (end));
return;
}
/* Find the first label after the entry-jump. */
while (GET_CODE (loop_top) != CODE_LABEL)
{
loop_top = NEXT_INSN (loop_top);
if (loop_top == 0)
abort ();
}
/* Maybe rearrange the loop to drop straight in
with a new test to jump around it entirely.
(The latter is considered outside the loop.)
If this is done, we no longer enter with a jump. */
if (! something_before_entry_jump
&& loop_skip_over (loop_start, end, loop_entry_jump))
{
scan_start = loop_top;
loop_top = 0;
}
else
/* We really do enter with a jump;
scan the loop from the place where the jump jumps to. */
scan_start = p;
}
/* Count number of times each reg is set during this loop.
Set may_not_optimize[I] if it is not safe to move out
the setting of register I. */
bzero (n_times_set, nregs * sizeof (short));
bzero (may_not_optimize, nregs);
count_loop_regs_set (loop_top ? loop_top : loop_start, end,
may_not_optimize, &insn_count, nregs);
for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
may_not_optimize[i] = 1, n_times_set[i] = 1;
bcopy (n_times_set, n_times_used, nregs * sizeof (short));
if (loop_dump_stream)
{
fprintf (loop_dump_stream, "\nLoop from %d to %d: %d real insns.\n",
INSN_UID (loop_start), INSN_UID (end), insn_count);
if (loop_continue)
fprintf (loop_dump_stream, "Continue at insn %d.\n",
INSN_UID (loop_continue));
}
/* Scan through the loop finding insns that are safe to move.
In each such insn, store QImode as the mode, to mark it.
Then set n_times_set to -1 for the reg being set, so that
this reg will be considered invariant for subsequent insns.
We consider whether subsequent insns use the reg
in deciding whether it is worth actually moving.
MAYBE_NEVER is nonzero if we have passed a conditional jump insn
and therefore it is possible that the insns we are scanning
would never be executed. At such times, we must make sure
that it is safe to execute the insn once instead of zero times.
When MAYBE_NEVER is 0, all insns will be executed at least once
so that is not a problem. */
p = scan_start;
while (1)
{
p = NEXT_INSN (p);
/* At end of a straight-in loop, we are done.
At end of a loop entered at the bottom, scan the top. */
if (p == scan_start)
break;
if (p == end)
{
if (loop_top != 0)
p = NEXT_INSN (loop_top);
else
break;
if (p == scan_start)
break;
}
if (GET_CODE (p) == INSN
&& GET_CODE (PATTERN (p)) == SET
&& GET_CODE (SET_DEST (PATTERN (p))) == REG
&& ! may_not_optimize[REGNO (SET_DEST (PATTERN (p)))])
{
int tem1 = 0;
/* Don't try to optimize a register that was made
by loop-optimization for an inner loop.
We don't know its life-span, so we can't compute the benefit. */
if (REGNO (SET_DEST (PATTERN (p))) >= old_max_reg)
;
/* IN order to move a register, we need to have one of three cases:
(1) it is used only in the same basic block as the set
(2) it is not a user variable.
(3) the set is guaranteed to be executed once the loop starts,
and the reg is not used until after that. */
else if (! ((! maybe_never
&& ! loop_reg_used_before_p (p, loop_start, scan_start, end))
|| ! REG_USERVAR_P (SET_DEST (PATTERN (p)))
|| reg_in_basic_block_p (p, SET_DEST (PATTERN (p)))))
;
else if (((tem = invariant_p (SET_SRC (PATTERN (p))))
|| ((temp = loop_find_reg_equal (p))
&& (tem = invariant_p (XEXP (temp, 0)))))
&& (n_times_set[REGNO (SET_DEST (PATTERN (p)))] == 1
|| (tem1
= consec_sets_invariant_p (SET_DEST (PATTERN (p)),
n_times_set[REGNO (SET_DEST (PATTERN (p)))],
p)))
/* If the insn can cause a trap (such as divide by zero),
can't move it unless it's guaranteed to be executed
once loop is entered. Even a function call might
prevent the trap insn from being reached
(since it might exit!) */
&& ! ((maybe_never || call_passed)
&& (may_trap_p (SET_SRC (PATTERN (p)))
|| ((temp = loop_find_reg_equal (p))
&& may_trap_p (XEXP (temp, 0))))))
{
register struct movable *m;
register int regno = REGNO (SET_DEST (PATTERN (p)));
int count;
m = (struct movable *) alloca (sizeof (struct movable));
m->next = 0;
m->insn = p;
temp = loop_find_reg_equal (p);
if (temp)
m->set_src = XEXP (temp, 0);
else
m->set_src = SET_SRC (PATTERN (p));
m->force = 0;
m->consec = n_times_set[REGNO (SET_DEST (PATTERN (p)))] - 1;
m->done = 0;
m->forces = 0;
m->partial = 0;
m->savemode = VOIDmode;
m->regno = regno;
/* Set M->cond if either invariant_p or consec_sets_invariant_p
returned 2 (only conditionally invariant). */
m->cond = ((tem | tem1) > 1);
m->global = (uid_luid[regno_last_uid[regno]] > INSN_LUID (end)
|| uid_luid[regno_first_uid[regno]] < INSN_LUID (loop_start));
m->match = 0;
m->lifetime = (uid_luid[regno_last_uid[regno]]
- uid_luid[regno_first_uid[regno]]);
m->savings = n_times_used[regno];
n_times_set[regno] = -1;
/* Add M to the end of the chain MOVABLES. */
if (movables == 0)
movables = m;
else
last_movable->next = m;
last_movable = m;
if (m->consec > 0)
{
/* Skip this insn, not checking REG_LIBCALL notes. */
p = NEXT_INSN (p);
/* Skip the consecutive insns, if there are any. */
p = skip_consec_insns (p, m->consec);
/* Back up, so the main loop will advance to the right place. */
p = PREV_INSN (p);
}
}
/* If this register is always set within a STRICT_LOW_PART
or set to zero, then its high bytes are constant.
So clear them outside the loop and within the loop
just load the low bytes.
We must check that the machine has an instruction to do so.
Also, if the value loaded into the register
depends on the same register, this cannot be done. */
else if (SET_SRC (PATTERN (p)) == const0_rtx
&& GET_CODE (NEXT_INSN (p)) == INSN
&& GET_CODE (PATTERN (NEXT_INSN (p))) == SET
&& (GET_CODE (SET_DEST (PATTERN (NEXT_INSN (p))))
== STRICT_LOW_PART)
&& (GET_CODE (XEXP (SET_DEST (PATTERN (NEXT_INSN (p))), 0))
== SUBREG)
&& (SUBREG_REG (XEXP (SET_DEST (PATTERN (NEXT_INSN (p))), 0))
== SET_DEST (PATTERN (p)))
&& !reg_mentioned_p (SET_DEST (PATTERN (p)),
SET_SRC (PATTERN (NEXT_INSN (p)))))
{
register int regno = REGNO (SET_DEST (PATTERN (p)));
if (n_times_set[regno] == 2)
{
register struct movable *m;
int count;
m = (struct movable *) alloca (sizeof (struct movable));
m->next = 0;
m->insn = p;
m->force = 0;
m->consec = 0;
m->done = 0;
m->forces = 0;
m->partial = 1;
/* If the insn may not be executed on some cycles,
we can't clear the whole reg; clear just high part.
Not even if the reg is used only within this loop.
Consider this:
while (1)
while (s != t) {
if (foo ()) x = *s;
use (x);
}
Clearing x before the inner loop could clobber a value
being saved from the last time around the outer loop.
However, if the reg is not used outside this loop
and all uses of the register are in the same
basic block as the store, there is no problem. */
m->global = (uid_luid[regno_last_uid[regno]] > INSN_LUID (end)
|| uid_luid[regno_first_uid[regno]] < INSN_LUID (p)
|| (labels_in_range_p
(p, uid_luid[regno_first_uid[regno]])));
if (maybe_never && m->global)
m->savemode = GET_MODE (SET_SRC (PATTERN (NEXT_INSN (p))));
else
m->savemode = VOIDmode;
m->regno = regno;
m->cond = 0;
m->match = 0;
m->lifetime = (uid_luid[regno_last_uid[regno]]
- uid_luid[regno_first_uid[regno]]);
m->savings = 1;
n_times_set[regno] = -1;
/* Add M to the end of the chain MOVABLES. */
if (movables == 0)
movables = m;
else
last_movable->next = m;
last_movable = m;
}
}
}
/* Past a call insn, we get to insns which might not be executed
because the call might exit. This matters for insns that trap. */
else if (GET_CODE (p) == CALL_INSN)
call_passed = 1;
/* Past a label or a jump, we get to insns for which we
can't count on whether or how many times they will be
executed during each iteration. Therefore, we can
only move out sets of trivial variables
(those not used after the loop). */
else if ((GET_CODE (p) == CODE_LABEL || GET_CODE (p) == JUMP_INSN)
/* If we enter the loop in the middle, and scan around
to the beginning, don't set maybe_never for that. */
&& ! (NEXT_INSN (p) == end && GET_CODE (p) == JUMP_INSN
&& simplejump_p (p)))
maybe_never = 1;
}
/* If one movable subsumes another, ignore that other. */
ignore_some_movables (movables);
/* For each movable insn, see if the reg that it loads
leads when it dies right into another conditionally movable insn.
If so, record that the second insn "forces" the first one,
since the second can be moved only if the first is. */
force_movables (movables);
/* See if there are multiple movable insns that load the same value.
If there are, make all but the first point at the first one
through the `match' field, and add the priorities of them
all together as the priority of the first. */
combine_movables (movables, nregs);
/* Now consider each movable insn to decide whether it is worth moving.
Store 0 in n_times_set for each reg that is moved. */
move_movables (movables, threshold,
insn_count, loop_start, end, nregs);
/* Now candidates that still have -1 are those not moved.
Change n_times_set to indicate that those are not actually invariant. */
for (i = 0; i < nregs; i++)
if (n_times_set[i] < 0)
n_times_set[i] = n_times_used[i];
if (flag_strength_reduce)
strength_reduce (scan_start, end, loop_top,
insn_count, loop_start, end, nregs);
}
/* Return 1 if all uses of REG
are between INSN and the end of the basic block. */
static int
reg_in_basic_block_p (insn, reg)
rtx insn, reg;
{
int regno = REGNO (reg);
rtx p;
if (regno_first_uid[regno] != INSN_UID (insn))
return 0;
/* Search this basic block for the already recorded last use of the reg. */
for (p = insn; p; p = NEXT_INSN (p))
{
switch (GET_CODE (p))
{
case NOTE:
break;
case INSN:
case CALL_INSN:
/* Ordinary insn: if this is the last use, we win. */
if (regno_last_uid[regno] == INSN_UID (p))
return 1;
break;
case JUMP_INSN:
/* Jump insn: if this is the last use, we win. */
if (regno_last_uid[regno] == INSN_UID (p))
return 1;
/* Otherwise, it's the end of the basic block, so we lose. */
return 0;
case CODE_LABEL:
case BARRIER:
/* It's the end of the basic block, so we lose. */
return 0;
}
}
/* The "last use" doesn't follow the "first use"?? */
abort ();
}
/* Skip COUNT insns from INSN, counting library calls as 1 insn. */
static rtx
skip_consec_insns (insn, count)
rtx insn;
int count;
{
for (; count > 0; count--)
{
if (GET_CODE (insn) == NOTE)
insn = NEXT_INSN (insn);
else if (GET_CODE (insn) == BARRIER || GET_CODE (insn) == CODE_LABEL)
abort ();
else
{
rtx i1, temp;
/* If first insn of gnulib call sequence, skip to end. */
/* Do this at start of loop, since INSN is guaranteed to
be an insn here. */
if (temp = find_reg_note (insn, REG_LIBCALL, 0))
insn = XEXP (temp, 0);
do insn = NEXT_INSN (insn);
while (GET_CODE (insn) == NOTE);
}
}
return insn;
}
/* Find a REG_EQUAL note in INSN but only if it is safe to use for our
purposes. Those put in by CSE are not safe since they may fail to
use the registers that appear in the actual insn source. */
static rtx
loop_find_reg_equal (insn)
rtx insn;
{
return (find_reg_note (insn, REG_RETVAL, 0)
? find_reg_note (insn, REG_EQUAL, 0)
: 0);
}
/* Ignore any movable whose insn falls within a libcall
which is part of another movable.
We make use of the fact that the movable for the libcall value
was made later and so appears later on the chain. */
static void
ignore_some_movables (movables)
struct movable *movables;
{
register struct movable *m, *m1;
for (m = movables; m; m = m->next)
{
/* Is this a movable for the value of a libcall? */
rtx note = find_reg_note (m->insn, REG_RETVAL, 0);
if (note)
{
/* Find the beginning of that libcall. */
rtx first_insn = XEXP (note, 0);
/* Check for earlier movables inside that range,
and mark them invalid. */
for (m1 = movables; m1 != m; m1 = m1->next)
if (INSN_LUID (m1->insn) >= INSN_LUID (first_insn)
&& INSN_LUID (m1->insn) < INSN_LUID (m->insn))
m1->done = 1;
}
}
}
/* For each movable insn, see if the reg that it loads
leads when it dies right into another conditionally movable insn.
If so, record that the second insn "forces" the first one,
since the second can be moved only if the first is. */
static void
force_movables (movables)
struct movable *movables;
{
register struct movable *m, *m1;
for (m1 = movables; m1; m1 = m1->next)
/* Omit this if moving just the (SET (REG) 0) of a zero-extend. */
if (!m1->partial && !m1->done)
{
int regno = m1->regno;
for (m = m1->next; m; m = m->next)
/* ??? Could this be a bug? What if CSE caused the
register of M1 to be used after this insn?
Since CSE does not update regno_last_uid,
this insn M->insn might not be where it dies.
But very likely this doesn't matter; what matters is
that M's reg is computed from M1's reg. */
if (INSN_UID (m->insn) == regno_last_uid[regno]
&& !m->done)
break;
if (m != 0 && m->set_src == SET_DEST (PATTERN (m1->insn)))
m = 0;
/* Increase the priority of the moving the first insn
since it permits the second to be moved as well. */
if (m != 0)
{
m->forces = m1;
m1->lifetime += m->lifetime;
m1->savings += m1->savings;
}
}
}
/* Find invariant expressions that are equal and can be combined into
one register. */
static void
combine_movables (movables, nregs)
struct movable *movables;
int nregs;
{
register struct movable *m;
char *matched_regs = (char *) alloca (nregs);
enum machine_mode mode;
/* Regs that are set more than once are not allowed to match
or be matched. I'm no longer sure why not. */
/* Perhaps testing m->consec_sets would be more appropriate here? */
for (m = movables; m; m = m->next)
if (m->match == 0 && n_times_used[m->regno] == 1 && !m->partial)
{
register struct movable *m1;
int regno = m->regno;
bzero (matched_regs, nregs);
matched_regs[regno] = 1;
for (m1 = m->next; m1; m1 = m1->next)
if (m1->match == 0 && n_times_used[m1->regno] == 1
/* A reg used outside the loop mustn't be eliminated. */
&& !m1->global
/* A reg used for zero-extending mustn't be eliminated. */
&& !m1->partial
&& (matched_regs[m1->regno]
||
(
/* Can't combine regs with different modes
even if loaded from the same constant. */
(GET_MODE (SET_DEST (PATTERN (m->insn)))
== GET_MODE (SET_DEST (PATTERN (m1->insn))))
/* See if the source of M1 says it matches M. */
&& ((GET_CODE (m1->set_src) == REG
&& matched_regs[REGNO (m1->set_src)])
|| rtx_equal_for_loop_p (m->set_src, m1->set_src,
movables)
|| (REG_NOTES (m->insn) && REG_NOTES (m1->insn)
&& REG_NOTE_KIND (REG_NOTES (m->insn)) == REG_EQUIV
&& REG_NOTE_KIND (REG_NOTES (m1->insn)) == REG_EQUIV
&& rtx_equal_p (XEXP (REG_NOTES (m->insn), 0),
XEXP (REG_NOTES (m1->insn), 0)))))))
{
m->lifetime += m1->lifetime;
m->savings += m1->savings;
m1->match = m;
matched_regs[m1->regno] = 1;
}
}
/* Now combine the regs used for zero-extension.
This can be done for those not marked `global'
provided their lives don't overlap. */
for (mode = VOIDmode; (int) mode < (int) MAX_MACHINE_MODE;
mode = (enum machine_mode) ((int) mode + 1))
if (GET_MODE_CLASS (mode) == MODE_INT)
{
register struct movable *m0 = 0;
/* Combine all the registers for extension from mode MODE.
Don't combine any that are used outside this loop. */
for (m = movables; m; m = m->next)
if (m->partial && ! m->global
&& mode == GET_MODE (SET_SRC (PATTERN (NEXT_INSN (m->insn)))))
{
register struct movable *m1;
int first = uid_luid[regno_first_uid[m->regno]];
int last = uid_luid[regno_last_uid[m->regno]];
if (m0 == 0)
{
/* First one: don't check for overlap, just record it. */
m0 = m;
continue;
}
/* Make sure they extend to the same mode.
(Almost always true.) */
if (GET_MODE (SET_DEST (PATTERN (m->insn)))
!= GET_MODE (SET_DEST (PATTERN (m0->insn))))
continue;
/* We already have one: check for overlap with those
already combined together. */
for (m1 = movables; m1 != m; m1 = m1->next)
if (m1 == m0 || (m1->partial && m1->match == m0))
if (! (uid_luid[regno_first_uid[m1->regno]] > last
|| uid_luid[regno_last_uid[m1->regno]] < first))
goto overlap;
/* No overlap: we can combine this with the others. */
m0->lifetime += m->lifetime;
m0->savings += m->savings;
m->match = m0;
overlap: ;
}
}
}
/* Return 1 if regs X and Y will become the same if moved. */
static int
regs_match_p (x, y, movables)
rtx x, y;
struct movable *movables;
{
int xn = REGNO (x);
int yn = REGNO (y);
struct movable *mx, *my;
for (mx = movables; mx; mx = mx->next)
if (mx->regno == xn)
break;
for (my = movables; my; my = my->next)
if (my->regno == yn)
break;
return (mx && my
&& ((mx->match == my->match && mx->match != 0)
|| mx->match == my
|| mx == my->match));
}
/* Return 1 if X and Y are identical-looking rtx's.
This is the Lisp function EQUAL for rtx arguments. */
static int
rtx_equal_for_loop_p (x, y, movables)
rtx x, y;
struct movable *movables;
{
register int i;
register int j;
register enum rtx_code code;
register char *fmt;
if (x == y)
return 1;
if (x == 0 || y == 0)
return 0;
code = GET_CODE (x);
/* Rtx's of different codes cannot be equal. */
if (code != GET_CODE (y))
return 0;
/* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
(REG:SI x) and (REG:HI x) are NOT equivalent. */
if (GET_MODE (x) != GET_MODE (y))
return 0;
/* These three types of rtx's can be compared nonrecursively. */
/* Until the end of reload,
don't consider the a reference to the return register of the current
function the same as the return from a called function. This eases
the job of function integration. Once the distinction no longer
matters, the insn will be deleted. */
if (code == REG)
return ((REGNO (x) == REGNO (y)
&& REG_FUNCTION_VALUE_P (x) == REG_FUNCTION_VALUE_P (y))
|| regs_match_p (x, y, movables));
if (code == LABEL_REF)
return XEXP (x, 0) == XEXP (y, 0);
if (code == SYMBOL_REF)
return XSTR (x, 0) == XSTR (y, 0);
/* Compare the elements. If any pair of corresponding elements
fail to match, return 0 for the whole things. */
fmt = GET_RTX_FORMAT (code);
for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
{
switch (fmt[i])
{
case 'i':
if (XINT (x, i) != XINT (y, i))
return 0;
break;
case 'E':
/* Two vectors must have the same length. */
if (XVECLEN (x, i) != XVECLEN (y, i))
return 0;
/* And the corresponding elements must match. */
for (j = 0; j < XVECLEN (x, i); j++)
if (rtx_equal_for_loop_p (XVECEXP (x, i, j), XVECEXP (y, i, j), movables) == 0)
return 0;
break;
case 'e':
if (rtx_equal_for_loop_p (XEXP (x, i), XEXP (y, i), movables) == 0)
return 0;
break;
case 's':
if (strcmp (XSTR (x, i), XSTR (y, i)))
return 0;
break;
case 'u':
/* These are just backpointers, so they don't matter. */
break;
case '0':
break;
/* It is believed that rtx's at this level will never
contain anything but integers and other rtx's,
except for within LABEL_REFs and SYMBOL_REFs. */
default:
abort ();
}
}
return 1;
}
/* Scan MOVABLES, and move the insns that deserve to be moved.
If two matching movables are combined, replace one reg with the
other throughout. */
static void
move_movables (movables, threshold, insn_count, loop_start, end, nregs)
struct movable *movables;
int threshold;
int insn_count;
rtx loop_start;
rtx end;
int nregs;
{
rtx new_start = 0;
register struct movable *m;
register rtx p;
/* Map of pseudo-register replacements to handle combining
when we move several insns that load the same value
into different pseudo-registers. */
rtx *reg_map = (rtx *) alloca (nregs * sizeof (rtx));
char *already_moved = (char *) alloca (nregs);
bzero (already_moved, nregs);
bzero (reg_map, nregs * sizeof (rtx));
num_movables = 0;
for (m = movables; m; m = m->next)
{
/* Describe this movable insn. */
if (loop_dump_stream)
{
fprintf (loop_dump_stream, "Insn %d: regno %d (life %d), ",
INSN_UID (m->insn), m->regno, m->lifetime);
if (m->consec > 0)
fprintf (loop_dump_stream, "consec %d, ", m->consec);
if (m->cond)
fprintf (loop_dump_stream, "cond ");
if (m->force)
fprintf (loop_dump_stream, "force ");
if (m->global)
fprintf (loop_dump_stream, "global ");
if (m->done)
fprintf (loop_dump_stream, "done ");
if (m->match)
fprintf (loop_dump_stream, "matches %d ",
INSN_UID (m->match->insn));
if (m->forces)
fprintf (loop_dump_stream, "forces %d ",
INSN_UID (m->forces->insn));
}
/* Count movables. Value used in heuristics in strength_reduce. */
num_movables++;
/* Ignore the insn if it's already done (it matched something else).
Otherwise, see if it is now safe to move. */
if (!m->done
&& (! m->cond
|| (1 == invariant_p (m->set_src)
&& (m->consec == 0
|| 1 == consec_sets_invariant_p (SET_DEST (PATTERN (m->insn)),
m->consec + 1,
m->insn))))
&& (! m->forces || m->forces->done))
{
register int regno;
register rtx p;
int savings = m->savings;
/* We have an insn that is safe to move.
Compute its desirability. */
p = m->insn;
regno = m->regno;
if (loop_dump_stream)
fprintf (loop_dump_stream, "savings %d ", savings);
if (moved_once[regno])
{
insn_count *= 2;
if (loop_dump_stream)
fprintf (loop_dump_stream, "halved since already moved ");
}
/* An insn MUST be moved if we already moved something else
which is safe only if this one is moved too: that is,
if already_moved[REGNO] is nonzero. */
/* An insn is desirable to move if the new lifetime of the
register is no more than THRESHOLD times the old lifetime.
If it's not desirable, it means the loop is so big
that moving won't speed things up much,
and it is liable to make register usage worse. */
/* It is also desirable to move if it can be moved at no
extra cost because something else was already moved. */
if (already_moved[regno]
|| (threshold * savings * m->lifetime) >= insn_count
|| (m->forces && m->forces->done
&& n_times_used[m->forces->regno] == 1))
{
int count;
register struct movable *m1;
rtx first;
/* Now move the insns that set the reg. */
for (count = m->consec; count >= 0; count--)
{
rtx i1, temp;
/* If first insn of gnulib call sequence, skip to end. */
/* Do this at start of loop, since p is guaranteed to
be an insn here. */
if (temp = find_reg_note (p, REG_LIBCALL, 0))
p = XEXP (temp, 0);
/* If last insn of gnulib call sequence, move all
insns except the last before the loop. The last insn is
handled in the normal manner. */
if (temp = find_reg_note (p, REG_RETVAL
, 0))
{
rtx fn_address = 0;
rtx fn_reg = 0;
first = 0;
for (temp = XEXP (temp, 0); temp != p;
temp = NEXT_INSN (temp))
{
rtx body = PATTERN (temp);
rtx n;
/* Extract the function address from the insn
that loads it into a register.
If this insn was cse'd, we get incorrect code.
So delete it and stick the fn address right
into the call insn. Since the moved insns
won't be cse'd, that does no harm. */
if (GET_CODE (NEXT_INSN (temp)) == CALL_INSN
&& GET_CODE (body) == SET
&& GET_CODE (SET_DEST (body)) == REG
&& (n = find_reg_note (temp, REG_EQUIV, 0)))
{
fn_reg = SET_SRC (body);
if (GET_CODE (fn_reg) != REG)
fn_reg = SET_DEST (body);
fn_address = XEXP (n, 0);
continue;
}
/* We have the call insn.
Substitute the fn address for the reg
that we believe this insn will use. */
if (GET_CODE (temp) == CALL_INSN
&& fn_address != 0)
replace_call_address (body, fn_reg, fn_address);
if (GET_CODE (temp) == CALL_INSN)
i1 = emit_call_insn_before (body, loop_start);
else
i1 = emit_insn_before (body, loop_start);
if (first == 0)
first = i1;
REG_NOTES (i1) = REG_NOTES (temp);
delete_insn (temp);
}
}
if (m->savemode != VOIDmode)
{
/* P sets REG to zero; but we should clear only the bits
that are not covered by the mode m->savemode. */
rtx reg = SET_DEST (PATTERN (p));
i1 = emit_insn_before
(gen_rtx (SET, VOIDmode, reg,
gen_rtx (AND, GET_MODE (reg),
reg,
gen_rtx (CONST_INT, VOIDmode,
(1 << GET_MODE_BITSIZE (m->savemode)) - 1))),
loop_start);
}
else if (GET_CODE (PATTERN (p)) == CALL_INSN)
i1 = emit_call_insn_before (PATTERN (p), loop_start);
else
i1 = emit_insn_before (PATTERN (p), loop_start);
if (new_start == 0)
new_start = i1;
if (loop_dump_stream)
fprintf (loop_dump_stream, " moved to %d", INSN_UID (i1));
/* Mark the moved, invariant reg as being equivalent to
its constant value. */
REG_NOTES (i1) = REG_NOTES (p);
if (REG_NOTES (i1) == 0
&& ! m->partial /* But not if it's a zero-extend clr. */
&& ! m->global /* and not if used outside the loop
(since it might get set outside). */
&& CONSTANT_P (SET_SRC (PATTERN (p))))
REG_NOTES (i1)
= gen_rtx (EXPR_LIST, REG_EQUIV,
SET_SRC (PATTERN (p)), REG_NOTES (i1));
/* If library call, now fix the REG_NOTES that contain
insn pointers, namely REG_LIBCALL on FIRST
and REG_RETVAL on I1. */
if (temp = find_reg_note (i1, REG_RETVAL, 0))
{
XEXP (temp, 0) = first;
temp = find_reg_note (first, REG_LIBCALL, 0);
XEXP (temp, 0) = i1;
}
delete_insn (p);
do p = NEXT_INSN (p);
while (p != 0 && GET_CODE (p) == NOTE);
}
/* The more regs we move, the less we like moving them. */
threshold -= 3;
/* Any other movable that loads the same register
MUST be moved. */
already_moved[regno] = 1;
/* This reg has been moved out of one loop. */
moved_once[regno] = 1;
/* The reg set here is now invariant. */
if (! m->partial)
n_times_set[regno] = 0;
m->done = 1;
/* Change the length-of-life info for the register
to say it lives at least the full length of this loop.
This will help guide optimizations in outer loops. */
if (uid_luid[regno_first_uid[regno]] > INSN_LUID (loop_start))
/* This is the old insn before all the moved insns.
We can't use the moved insn because it is out of range
in uid_luid. Only the old insns have luids. */
regno_first_uid[regno] = INSN_UID (loop_start);
if (uid_luid[regno_last_uid[regno]] < INSN_LUID (end))
regno_last_uid[regno] = INSN_UID (end);
/* Combine with this moved insn any other matching movables. */
for (m1 = m->next; m1; m1 = m1->next)
if (m1->match == m)
{
rtx temp;
/* Schedule the reg loaded by M1
for replacement so that shares the reg of M. */
reg_map[m1->regno] = SET_DEST (PATTERN (m->insn));
/* Get rid of the matching insn
and prevent further processing of it. */
m1->done = 1;
/* if library call, delete all insn except last, which
is deleted below */
if (temp = find_reg_note (m1->insn, REG_RETVAL, 0))
{
for (temp = XEXP (temp, 0); temp != m1->insn;
temp = NEXT_INSN (temp))
delete_insn (temp);
}
delete_insn (m1->insn);
/* Any other movable that loads the same register
MUST be moved. */
already_moved[m1->regno] = 1;
/* The reg merged here is now invariant,
if the reg it matches is invariant. */
if (! m->partial)
n_times_set[m1->regno] = 0;
}
}
else if (loop_dump_stream)
fprintf (loop_dump_stream, "not desirable");
}
else if (loop_dump_stream && !m->match)
fprintf (loop_dump_stream, "not safe");
if (loop_dump_stream)
fprintf (loop_dump_stream, "\n");
}
if (new_start == 0)
new_start = loop_start;
/* Go through all the instructions in the loop, making
all the register substitutions scheduled in REG_MAP. */
for (p = new_start; p != end; p = NEXT_INSN (p))
if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
|| GET_CODE (p) == CALL_INSN)
{
rtx tail;
replace_regs (PATTERN (p), reg_map, nregs);
/* Subsitute registers in the equivalent expression also. */
for (tail = REG_NOTES (p); tail; tail = XEXP (tail, 1))
if (REG_NOTE_KIND (tail) == REG_EQUAL
|| REG_NOTE_KIND (tail) == REG_EQUIV)
replace_regs (XEXP (tail, 0), reg_map, nregs);
}
}
/* Optionally change a loop which enters just before the endtest
to one which falls straight in
after skipping around the entire loop if the endtest would drop out.
Returns 1 if the change was made, 0 if the loop was not really suitable. */
int
loop_skip_over (start, end, loop_entry_jump)
rtx start;
rtx end;
rtx loop_entry_jump;
{
rtx entry_insn;
rtx endtest;
rtx endtestjump;
register rtx p = JUMP_LABEL (loop_entry_jump);
while (GET_CODE (p) != INSN && GET_CODE (p) != JUMP_INSN
&& GET_CODE (p) != CALL_INSN)
p = NEXT_INSN (p);
entry_insn = p;
/* Skip any ordinary arithmetic insns to find the compare. */
for (; p != 0; p = NEXT_INSN (p))
if (GET_CODE (p) != NOTE)
if (GET_CODE (p) != INSN || sets_cc0_p (PATTERN (p)))
break;
if (p == 0 || GET_CODE (p) != INSN)
return 0;
endtest = p;
endtestjump = next_real_insn (p);
/* Check that (1) we have reached a compare insn and (2)
the insn (presumably a jump) following that compare
is the last in the loop and jumps back to the loop beginning. */
if (sets_cc0_p (PATTERN (endtest)) > 0
&& endtestjump == prev_real_insn (end)
&& prev_real_insn (JUMP_LABEL (endtestjump)) == loop_entry_jump)
{
rtx newlab;
/* This is the jump that we insert. */
rtx new_jump;
/* Duplicate the ordinary arith insns before the compare. */
for (p = entry_insn; p != endtest; p = NEXT_INSN (p))
if (GET_CODE (p) == INSN)
{
rtx new = emit_insn_before (copy_rtx (PATTERN (p)), start);
if (REG_NOTES (p))
REG_NOTES (new) = copy_rtx (REG_NOTES (p));
}
/* Ok, duplicate that test before start of loop. */
emit_insn_before (copy_rtx (PATTERN (endtest)), start);
/* Make a new entry-jump (before the original one)
whose condition is opposite to the loop-around endtest
and which jumps around the loop (to just after the ending NOTE). */
newlab = gen_label_rtx ();
emit_label_after (newlab, end);
emit_jump_insn_before (copy_rtx (PATTERN (endtestjump)), start);
new_jump = PREV_INSN (start);
JUMP_LABEL (new_jump) = JUMP_LABEL (endtestjump);
LABEL_NUSES (JUMP_LABEL (endtestjump))++;
invert_jump (new_jump, newlab);
/* Delete the original entry-jump. */
delete_insn (loop_entry_jump);
return 1;
}
return 0;
}
/* Throughout the rtx X, replace many registers according to REG_MAP.
Return the replacement for X (which may be X with altered contents).
REG_MAP[R] is the replacement for register R, or 0 for don't replace.
NREGS is the length of REG_MAP; regs >= NREGS are not mapped. */
static rtx
replace_regs (x, reg_map, nregs)
rtx x;
rtx *reg_map;
int nregs;
{
register enum rtx_code code;
register int i;
register char *fmt;
if (x == 0)
return x;
code = GET_CODE (x);
switch (code)
{
case PC:
case CC0:
case CONST_INT:
case CONST_DOUBLE:
case CONST:
case SYMBOL_REF:
case LABEL_REF:
return x;
case REG:
/* Verify that the register has an entry before trying to access it. */
if (REGNO (x) < nregs && reg_map[REGNO (x)] != 0)
return reg_map[REGNO (x)];
return x;
}
fmt = GET_RTX_FORMAT (code);
for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
{
if (fmt[i] == 'e')
XEXP (x, i) = replace_regs (XEXP (x, i), reg_map, nregs);
if (fmt[i] == 'E')
{
register int j;
for (j = 0; j < XVECLEN (x, i); j++)
XVECEXP (x, i, j) = replace_regs (XVECEXP (x, i, j), reg_map, nregs);
}
}
return x;
}
/* Scan X and replace the address of any MEM in it with ADDR.
REG is the address that MEM should have before the replacement. */
static void
replace_call_address (x, reg, addr)
rtx x, reg, addr;
{
register enum rtx_code code;
register int i;
register char *fmt;
if (x == 0)
return;
code = GET_CODE (x);
switch (code)
{
case PC:
case CC0:
case CONST_INT:
case CONST_DOUBLE:
case CONST:
case SYMBOL_REF:
case LABEL_REF:
case REG:
return;
case SET:
/* Short cut for very common case. */
replace_call_address (XEXP (x, 1), reg, addr);
return;
case CALL:
/* Short cut for very common case. */
replace_call_address (XEXP (x, 0), reg, addr);
return;
case MEM:
/* If this MEM uses a reg other than the one we expected,
something is wrong. */
if (XEXP (x, 0) != reg)
abort ();
XEXP (x, 0) = addr;
return;
}
fmt = GET_RTX_FORMAT (code);
for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
{
if (fmt[i] == 'e')
replace_call_address (XEXP (x, i), reg, addr);
if (fmt[i] == 'E')
{
register int j;
for (j = 0; j < XVECLEN (x, i); j++)
replace_call_address (XVECEXP (x, i, j), reg, addr);
}
}
}
/* Return the number of memory refs to addresses that vary
in the rtx X. */
static int
count_nonfixed_reads (x)
rtx x;
{
register enum rtx_code code;
register int i;
register char *fmt;
int value;
if (x == 0)
return 0;
code = GET_CODE (x);
switch (code)
{
case PC:
case CC0:
case CONST_INT:
case CONST_DOUBLE:
case CONST:
case SYMBOL_REF:
case LABEL_REF:
case REG:
return 0;
case MEM:
return rtx_varies_p (XEXP (x, 0)) + count_nonfixed_reads (XEXP (x, 0));
}
value = 0;
fmt = GET_RTX_FORMAT (code);
for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
{
if (fmt[i] == 'e')
value += count_nonfixed_reads (XEXP (x, i));
if (fmt[i] == 'E')
{
register int j;
for (j = 0; j < XVECLEN (x, i); j++)
value += count_nonfixed_reads (XVECEXP (x, i, j));
}
}
return value;
}
#if 0
/* P is an instruction that sets a register to the result of a ZERO_EXTEND.
Replace it with an instruction to load just the low bytes
if the machine supports such an instruction,
and insert above LOOP_START an instruction to clear the register. */
static void
constant_high_bytes (p, loop_start)
rtx p, loop_start;
{
register rtx new;
register int insn_code_number;
/* Try to change (SET (REG ...) (ZERO_EXTEND (..:B ...)))
to (SET (STRICT_LOW_PART (SUBREG:B (REG...))) ...). */
new = gen_rtx (SET, VOIDmode,
gen_rtx (STRICT_LOW_PART, VOIDmode,
gen_rtx (SUBREG, GET_MODE (XEXP (SET_SRC (PATTERN (p)), 0)),
SET_DEST (PATTERN (p)),
0)),
XEXP (SET_SRC (PATTERN (p)), 0));
insn_code_number = recog (new, p);
if (insn_code_number)
{
register int i;
/* Clear destination register before the loop. */
emit_insn_before (gen_rtx (SET, VOIDmode,
SET_DEST (PATTERN (p)),
const0_rtx),
loop_start);
/* Inside the loop, just load the low part. */
PATTERN (p) = new;
}
}
#endif
/* Verify that the ostensible loop starting at START
really is a loop: nothing jumps into it from outside.
Return the marker for the end of the loop, or zero if not a real loop.
Also set the variables `unknown_*_altered' and `loop_has_call',
and fill in the array `loop_store_addrs'. */
static rtx
verify_loop (f, start)
rtx f, start;
{
register int level = 1;
register rtx insn, end;
/* First find the LOOP_END that matches.
Also check each insn for storing in memory and record where. */
unknown_address_altered = 0;
unknown_aggregate_altered = 0;
fixed_aggregate_altered = 0;
loop_has_call = 0;
loop_store_addrs_idx = 0;
num_mem_sets = 0;
loops_enclosed = 1;
loop_continue = 0;
for (insn = NEXT_INSN (start); level > 0; insn = NEXT_INSN (insn))
{
if (insn == 0)
/* Parse errors can cause a loop-beg with no loop-end. */
return 0;
if (GET_CODE (insn) == NOTE)
{
if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
{
++level;
/* Count number of loops contained in this one. */
loops_enclosed++;
}
else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END)
{
--level;
if (level == 0)
{
end = insn;
break;
}
}
else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_CONT)
{
if (level == 1)
loop_continue = insn;
}
/* Don't optimize loops containing setjmps.
On some machines, longjmp does not restore the reg
values as of the time of the setjmp. */
else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_SETJMP)
return 0;
}
else if (GET_CODE (insn) == CALL_INSN)
{
unknown_address_altered = 1;
loop_has_call = 1;
}
/* ???
else if (! unknown_address_altered) */
else
{
if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN)
note_stores (PATTERN (insn), note_addr_stored);
}
}
/* Now scan all jumps in the function and see if any of them can
reach a label within the range of the loop. */
for (insn = f; insn; insn = NEXT_INSN (insn))
if (GET_CODE (insn) == JUMP_INSN
/* Don't get fooled by jumps inserted by loop-optimize.
They don't have valid LUIDs, and they never jump into loops. */
&& INSN_UID (insn) < max_uid
&& (INSN_LUID (insn) < INSN_LUID (start)
|| INSN_LUID (insn) > INSN_LUID (end))
/* We have a jump that is outside the loop.
Does it jump into the loop? */
&& can_jump_into_range_p (PATTERN (insn),
INSN_LUID (start), INSN_LUID (end)))
return 0;
#if 0
/* Now scan all labels between them and check for any jumps from outside.
This uses the ref-chains set up by find_basic_blocks.
This code is not used because it's more convenient for other reasons
to do the loop optimization before find_basic_blocks. */
for (insn = start; insn != end; insn = NEXT_INSN (insn))
if (GET_CODE (insn) == CODE_LABEL)
{
register rtx y;
for (y = LABEL_REFS (insn); y != insn; y = LABEL_NEXTREF (y))
if (INSN_LUID (CONTAINING_INSN (y)) < INSN_LUID (start)
|| INSN_LUID (CONTAINING_INSN (y)) > INSN_LUID (end))
return 0;
}
#endif
return end;
}
/* Return 1 if somewhere in X is a LABEL_REF to a label
located between BEG and END. */
static int
can_jump_into_range_p (x, beg, end)
rtx x;
int beg, end;
{
register enum rtx_code code = GET_CODE (x);
register int i;
register char *fmt;
if (code == LABEL_REF)
{
register int luid = INSN_LUID (XEXP (x, 0));
return luid > beg && luid < end;
}
fmt = GET_RTX_FORMAT (code);
for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
{
if (fmt[i] == 'e')
{
if (can_jump_into_range_p (XEXP (x, i), beg, end))
return 1;
}
else if (fmt[i] == 'E')
{
register int j;
for (j = 0; j < XVECLEN (x, i); j++)
if (can_jump_into_range_p (XVECEXP (x, i, j), beg, end))
return 1;
}
}
return 0;
}
/* Return nonzero if there is a label in the range from
insn INSN to the insn whose luid is END. */
static int
labels_in_range_p (insn, end)
rtx insn;
int end;
{
while (insn && INSN_LUID (insn) <= end)
{
if (GET_CODE (insn) == CODE_LABEL)
return 0;
insn = NEXT_INSN (insn);
}
return 0;
}
/* Record that a memory reference X is being set. */
static void
note_addr_stored (x)
rtx x;
{
if (x == 0 || GET_CODE (x) != MEM)
return;
/* Count number of memory writes.
This affects heuristics in strength_reduce. */
num_mem_sets++;
if (unknown_address_altered)
return;
if (GET_MODE (x) == BLKmode)
unknown_address_altered = 1;
else if (rtx_addr_varies_p (x))
{
if (GET_CODE (XEXP (x, 0)) == PLUS)
unknown_aggregate_altered = 1;
else
unknown_address_altered = 1;
}
else
{
register int i;
register rtx addr = XEXP (x, 0);
if (MEM_IN_STRUCT_P (x))
fixed_aggregate_altered = 1;
for (i = 0; i < loop_store_addrs_idx; i++)
if (rtx_equal_p (loop_store_addrs[i], addr))
{
if (loop_store_widths[i] < GET_MODE_SIZE (GET_MODE (x)))
loop_store_widths[i] = GET_MODE_SIZE (GET_MODE (x));
break;
}
if (i == NUM_STORES)
unknown_address_altered = 1;
else if (i == loop_store_addrs_idx)
{
loop_store_widths[i] = GET_MODE_SIZE (GET_MODE (x));
loop_store_addrs[loop_store_addrs_idx++] = addr;
}
}
}
/* Return nonzero if the rtx X is invariant over the current loop.
The value is 2 if we refer to something only conditionally invariant.
If `unknown_address_altered' is nonzero, no memory ref is invariant.
Otherwise if `unknown_aggregate_altered' is nonzero,
a memory ref is invariant if it is not part of an aggregate
and its address is fixed and not in `loop_store_addrs'.
Otherwise if `fixed_aggregate_altered' is nonzero,
a memory ref is invariant
if its address is fixed and not in `loop_store_addrs'.
Otherwise, a memory ref is invariant if its address is fixed and not in
`loop_store_addrs' or if it is not an aggregate. */
static int
invariant_p (x)
register rtx x;
{
register int i;
register enum rtx_code code;
register char *fmt;
int conditional = 0;
if (x == 0)
return 1;
code = GET_CODE (x);
switch (code)
{
case CONST_INT:
case CONST_DOUBLE:
case SYMBOL_REF:
case LABEL_REF:
case CONST:
return 1;
case PC:
case CC0:
return 0;
case REG:
/* We used to check RTX_UNCHANGING_P (x) here, but that is invalid
since the reg might be set by initialization within the loop. */
if (x == frame_pointer_rtx || x == arg_pointer_rtx)
return 1;
if (n_times_set[REGNO (x)] == -1)
return 2;
return n_times_set[REGNO (x)] == 0;
case MEM:
/* Constants in the constant pool are invariant.
?? Really we should detect any constant address in the
text section. */
if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
&& CONSTANT_POOL_ADDRESS_P (XEXP (x, 0)))
return 1;
/* A store in a varying-address scalar (or a subroutine call)
could clobber anything in memory. */
if (unknown_address_altered)
return 0;
/* Don't mess with volatile memory references. */
if (MEM_VOLATILE_P (x))
return 0;
#if 0
/* If it's declared read-only, it is invariant
if its address is invariant. */
if (RTX_UNCHANGING_P (x))
return invariant_p (XEXP (x, 0));
#endif
/* A store in a varying-address aggregate component
could clobber anything except a scalar with a fixed address. */
if (unknown_aggregate_altered
&& ((MEM_IN_STRUCT_P (x) || GET_CODE (XEXP (x, 0)) == PLUS)
|| rtx_addr_varies_p (x)))
return 0;
/* A store in a fixed-address aggregate component
could clobber anything whose address is not fixed,
even an aggregate component. */
if (fixed_aggregate_altered
&& rtx_addr_varies_p (x))
return 0;
/* Any store could clobber a varying-address scalar. */
if (loop_store_addrs_idx
&& !(MEM_IN_STRUCT_P (x) || GET_CODE (XEXP (x, 0)) == PLUS)
&& rtx_addr_varies_p (x))
return 0;
/* A store in a fixed address clobbers overlapping references. */
for (i = loop_store_addrs_idx - 1; i >= 0; i--)
if (addr_overlap_p (x, loop_store_addrs[i], loop_store_widths[i]))
return 0;
/* It's not invalidated by a store in memory
but we must still verify the address is invariant. */
break;
case ASM_OPERANDS:
/* Don't mess with insns declared volatile. */
if (MEM_VOLATILE_P (x))
return 0;
}
fmt = GET_RTX_FORMAT (code);
for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
{
if (fmt[i] == 'e')
{
int tem = invariant_p (XEXP (x, i));
if (tem == 0)
return 0;
if (tem == 2)
conditional = 1;
}
else if (fmt[i] == 'E')
{
register int j;
for (j = 0; j < XVECLEN (x, i); j++)
{
int tem = invariant_p (XVECEXP (x, i, j));
if (tem == 0)
return 0;
if (tem == 2)
conditional = 1;
}
}
}
return 1 + conditional;
}
/* Return 1 if OTHER (a mem ref) overlaps the area of memory
which is SIZE bytes starting at BASE. */
int
addr_overlap_p (other, base, size)
rtx other;
rtx base;
int size;
{
int start = 0, end;
if (GET_CODE (base) == CONST)
base = XEXP (base, 0);
if (GET_CODE (base) == PLUS
&& GET_CODE (XEXP (base, 1)) == CONST_INT)
{
start = INTVAL (XEXP (base, 1));
base = XEXP (base, 0);
}
end = start + size;
return refers_to_mem_p (other, base, start, end);
}
/* Return nonzero if all the insns in the loop that set REG
are INSN and the immediately following insns,
and if each of those insns sets REG in an invariant way
(not counting uses of REG in them).
The value is 2 if some of these insns are only conditionally invariant.
We assume that INSN itself is the first set of REG
and that its source is invariant. */
static int
consec_sets_invariant_p (reg, n_sets, insn)
int n_sets;
rtx reg, insn;
{
register rtx p = insn;
register int regno = REGNO (reg);
rtx temp;
/* Number of sets we have to insist on finding after INSN. */
int count = n_sets - 1;
int old = n_times_set[regno];
int value = 0;
int this;
/* If N_SETS hit the limit, we can't rely on its value. */
if (n_sets == 127)
return 0;
n_times_set[regno] = 0;
while (count > 0)
{
register enum rtx_code code;
p = NEXT_INSN (p);
code = GET_CODE (p);
/* If library call, skip to end of of it. */
if (code == INSN && (temp = find_reg_note (p, REG_LIBCALL, 0)))
p = XEXP (temp, 0);
this = 0;
if (code == INSN && GET_CODE (PATTERN (p)) == SET
&& GET_CODE (SET_DEST (PATTERN (p))) == REG
&& REGNO (SET_DEST (PATTERN (p))) == regno)
{
this = invariant_p (SET_SRC (PATTERN (p)));
if (this != 0)
value |= this;
else if (temp = loop_find_reg_equal (p))
{
this = invariant_p (XEXP (temp, 0));
if (this != 0)
value |= this;
}
}
if (this != 0)
count--;
else if (code != NOTE)
{
n_times_set[regno] = old;
return 0;
}
}
n_times_set[regno] = old;
/* If invariant_p ever returned 2, we return 2. */
return 1 + (value & 2);
}
#if 0
/* I don't think this condition is sufficient to allow INSN
to be moved, so we no longer test it. */
/* Return 1 if all insns in the basic block of INSN and following INSN
that set REG are invariant according to TABLE. */
static int
all_sets_invariant_p (reg, insn, table)
rtx reg, insn;
short *table;
{
register rtx p = insn;
register int regno = REGNO (reg);
while (1)
{
register enum rtx_code code;
p = NEXT_INSN (p);
code = GET_CODE (p);
if (code == CODE_LABEL || code == JUMP_INSN)
return 1;
if (code == INSN && GET_CODE (PATTERN (p)) == SET
&& GET_CODE (SET_DEST (PATTERN (p))) == REG
&& REGNO (SET_DEST (PATTERN (p))) == regno)
{
if (!invariant_p (SET_SRC (PATTERN (p)), table))
return 0;
}
}
}
#endif /* 0 */
/* Increment N_TIMES_SET at the index of each register
that is modified by an insn between FROM and TO.
If the value of an element of N_TIMES_SET becomes 127 or more,
stop incrementing it, to avoid overflow.
Store in *COUNT_PTR the number of actual instruction
in the loop. We use this to decide what is worth moving out. */
/* last_set[n] is nonzero iff reg n has been set in the current basic block.
In that case, it is the insn that last set reg n. */
static void
count_loop_regs_set (from, to, may_not_move, count_ptr, nregs)
register rtx from, to;
char *may_not_move;
int *count_ptr;
int nregs;
{
register rtx *last_set = (rtx *) alloca (nregs * sizeof (rtx));
register rtx insn;
register int count = 0;
register rtx dest;
bzero (last_set, nregs * sizeof (rtx));
for (insn = from; insn != to; insn = NEXT_INSN (insn))
{
if (GET_CODE (insn) == CALL_INSN)
{
/* If a register is used as a subroutine address,
don't allow this register's setting to be moved out of the loop.
This condition is not at all logically correct
but it averts a very common lossage pattern
and creates lossage much less often. */
if (GET_CODE (PATTERN (insn)) == CALL
&& GET_CODE (XEXP (PATTERN (insn), 0)) == MEM
&& GET_CODE (XEXP (XEXP (PATTERN (insn), 0), 0)) == REG)
{
register int regno
= REGNO (XEXP (XEXP (PATTERN (insn), 0), 0));
may_not_move[regno] = 1;
}
else if (GET_CODE (PATTERN (insn)) == SET
&& GET_CODE (SET_SRC (PATTERN (insn))) == CALL
&& GET_CODE (XEXP (SET_SRC (PATTERN (insn)), 0)) == MEM
&& GET_CODE (XEXP (XEXP (SET_SRC (PATTERN (insn)), 0), 0)) == REG)
{
register int regno
= REGNO (XEXP (XEXP (SET_SRC (PATTERN (insn)), 0), 0));
may_not_move[regno] = 1;
/* The call insn itself sets a reg, which cannot be moved. */
may_not_move[REGNO (SET_DEST (PATTERN (insn)))] = 1;
if (n_times_set[REGNO (SET_DEST (PATTERN (insn)))] < 127)
n_times_set[REGNO (SET_DEST (PATTERN (insn)))]++;
}
}
if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN)
{
++count;
if (GET_CODE (PATTERN (insn)) == CLOBBER
&& GET_CODE (XEXP (PATTERN (insn), 0)) == REG)
/* Don't move a reg that has an explicit clobber.
We might do so sometimes, but it's not worth the pain. */
may_not_move[REGNO (XEXP (PATTERN (insn), 0))] = 1;
else if (GET_CODE (PATTERN (insn)) == SET)
{
dest = SET_DEST (PATTERN (insn));
while (GET_CODE (dest) == SUBREG
|| GET_CODE (dest) == ZERO_EXTRACT
|| GET_CODE (dest) == SIGN_EXTRACT
|| GET_CODE (dest) == STRICT_LOW_PART)
dest = XEXP (dest, 0);
if (GET_CODE (dest) == REG)
{
register int regno = REGNO (dest);
/* If this is the first setting of this reg
in current basic block, and it was set before,
it must be set in two basic blocks, so it cannot
be moved out of the loop. */
if (n_times_set[regno] > 0 && last_set[regno] == 0)
may_not_move[regno] = 1;
/* If this is not first setting in current basic block,
see if reg was used in between previous one and this.
If so, neither one can be moved. */
if (last_set[regno] != 0
&& reg_used_between_p (dest, last_set[regno], insn))
may_not_move[regno] = 1;
if (n_times_set[regno] < 127)
++n_times_set[regno];
last_set[regno] = insn;
}
}
else if (GET_CODE (PATTERN (insn)) == PARALLEL)
{
register int i;
for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
{
register rtx x = XVECEXP (PATTERN (insn), 0, i);
if (GET_CODE (x) == CLOBBER && GET_CODE (XEXP (x, 0)) == REG)
/* Don't move a reg that has an explicit clobber.
It's not worth the pain to try to do it correctly. */
may_not_move[REGNO (XEXP (x, 0))] = 1;
if (GET_CODE (x) == SET)
{
dest = SET_DEST (x);
while (GET_CODE (dest) == SUBREG
|| GET_CODE (dest) == ZERO_EXTRACT
|| GET_CODE (dest) == SIGN_EXTRACT
|| GET_CODE (dest) == STRICT_LOW_PART)
dest = XEXP (dest, 0);
if (GET_CODE (dest) == REG)
{
register int regno = REGNO (dest);
if (n_times_set[regno] < 127)
++n_times_set[regno];
may_not_move[regno] = 1;
last_set[regno] = insn;
}
}
}
}
}
if (GET_CODE (insn) == CODE_LABEL || GET_CODE (insn) == JUMP_INSN)
bzero (last_set, nregs * sizeof (rtx));
}
*count_ptr = count;
}
/* Given a loop that is bounded by LOOP_START and LOOP_END
and that is entered at SCAN_START,
return 1 if the register set by insn INSN is used by
any insn that precedes INSN in cyclic order starting
from the loop entry point. */
static int
loop_reg_used_before_p (insn, loop_start, scan_start, loop_end)
rtx insn, loop_start, scan_start, loop_end;
{
rtx reg = SET_DEST (PATTERN (insn));
rtx p;
/* Scan forward checking for register usage. If we hit INSN, we
are done. Otherwise, if we hit LOOP_END, wrap around to LOOP_START. */
for (p = scan_start; p != insn; p = NEXT_INSN (p))
{
if ((GET_CODE (p) == INSN || GET_CODE (p) == CALL_INSN
|| GET_CODE (p) == JUMP_INSN)
&& reg_overlap_mentioned_p (reg, PATTERN (p)))
return 1;
if (p == loop_end)
p = loop_start;
}
return 0;
}
/* A "basic induction variable" or biv is a pseudo reg that is set
(within this loop) only by incrementing or decrementing it. */
/* A "general induction variable" or giv is a pseudo reg whose
value is a linear function of a biv. */
/* Bivs are recognized by `basic_induction_var';
Givs by `general_induct_var'. */
/* An enum for the two different types of givs, those that are used
as memory addresses and those that are calculated into registers. */
enum g_types { DEST_ADDR, DEST_REG };
/* A `struct induction' is created for every instruction that sets
an induction variable (either a biv or a giv). */
struct induction
{
rtx insn; /* The insn that sets a biv or giv */
rtx new_reg; /* New register, containing strength reduced
version of this giv. */
int src_regno; /* Biv from which this giv is computed.
(If this is a biv, then this is the biv.) */
enum g_types giv_type; /* Indicate whether DEST_ADDR or DEST_REG giv */
int dest_regno; /* Destination register for insn: this is the
register which was the biv or giv.
For a biv, this equals src_reg.
For a DEST_ADDR type giv, this is 0. */
rtx *location; /* Place in the insn where this giv occurs.
If GIV_TYPE is DEST_REG, this is 0. */
enum machine_mode mode; /* The mode of this biv or giv */
rtx mult_val; /* Multiplicative factor for src_reg. */
rtx add_val; /* Additive constant for that product. */
int benefit; /* Gain from eliminating this insn. */
int consec; /* The number of consecutive insn that set this
register; they are all eliminated if this
one is. */
char replaceable; /* 1 if we can substitute the strength-reduced
variable for the original variable.
0 means they must be kept separate and the
new one must be copied into the old pseudo
reg each time the old one is set. */
char ignore; /* 1 prohibits further processing of this giv */
int lifetime; /* Length of life of this giv */
int times_used; /* # times this giv is used. */
struct induction *family; /* Links together all induction variables that
have the same src register. */
struct induction *forces; /* Points to an induction variable insn which
is used only once, to compute this giv,
and hence can be deleted if this insn is
strength reduced. */
struct induction *forces2; /* Likewise. */
struct induction *same; /* Links together all induction variables that
have the same tuple (src, mult, add). */
};
/* A `struct iv_class' is created for each biv. */
struct iv_class {
int regno; /* Pseudo reg which is the biv. */
int biv_count; /* Number of insns setting this reg. */
struct induction *biv; /* List of all insns that set this reg. */
int giv_count; /* Number of DEST_REG givs computed from this
biv. The resulting count is only used in
check_dbra_loop. */
struct induction *giv; /* List of all insns that compute a giv
from this reg. */
int total_benefit; /* Sum of BENEFITs of all those givs */
rtx initial_value; /* Value of reg at loop start */
struct iv_class *next; /* Links all class structures together */
rtx init_insn; /* insn which intializes biv, 0 if none seen. */
char eliminable; /* 1 if plausible candidate for elimination. */
char nonneg; /* 1 if we added a REG_NONNEG note for this. */
};
/* Definitions used by the basic induction variable discovery code. */
enum iv_mode { UNKNOWN_INDUCT, BASIC_INDUCT, NOT_BASIC_INDUCT,
GENERAL_INDUCT };
/* Relative gain of eliminating various kinds of operations. */
#define NO_BENEFIT 0
#define ADD_BENEFIT 1
#define SHIFT_BENEFIT 2
#define MULT_BENEFIT 4
#define LIBCALL_BENEFIT 15
/* Benefit penalty, if a giv is not replaceable, i.e. must emit an insn to
copy the value of the strength reduced giv to its original register. */
#define COPY_PENALTY 2
/* Indexed by register number, indicates whether or not register is an
induction variable, and if so what type. */
static enum iv_mode *induct_var;
/* Indexed by register number, contains pointer to `struct induction'
if register is a general induction variable. */
static struct induction **induct_struct;
/* Indexed by register number, contains pointer to `struct iv_class'
if register is a basic induction variable. */
static struct iv_class **class_struct;
/*********************************/
/* ??? Unfinished optimizations, wilson@ji.Berkeley.EDU */
/* strength reduce addresses found in sources (set () (mem ())*/
/* There is one more optimization you might be interested in doing: to
allocate pseudo registers for frequently-accessed memory locations.
If the same memory location is referenced each time around, it might
be possible to copy it into a register before and out after.
This is especially useful when the memory location is a variable which
is in a stack slot because somewhere its address is taken. If the
loop doesn't contain a function call and the variable isn't volatile,
it is safe to keep the value in a register for the duration of the
loop. One tricky thing is that the copying of the value back from the
register has to be done on all exits from the loop. You need to check that
all the exits from the loop go to the same place. */
/* WARNING: the interaction of biv elimination, and recognizing 'constant'
bivs may cause problems */
/* add heuristic so that DEST_ADDR strength reduction does not cause
performance problems */
/* don't eliminate things that can be combined with an addressing mode?
find all giv that have same biv and mult_val (now must also have
same add_val), then for each giv, check to see if its only use
dies in a following memory address, generate a new memory address
and check to see if valid, if valid then store modified mem addr,
else if not valid addr mark giv as not done so that it will get its
own iv */
/* consec_sets_giv does not calculate replaceable and forces correctly,
forces should be a more general linked list instead of two entries */
/* try to optimize branches when it is known that a biv is always positive */
/* when replace biv in compare insn, should replace with closest giv so that
an optimized branch can still be recognized by combiner, i.e. VAXen acb */
/* should merge final_value calculation in check_dbra_loop with the
new final_biv_value function */
/* many of the checks involving uid_luid could be simplified if regscan
was rerun in loop_optimize() whenever a register was added or moved,
also some of the optimizations could be a little less conservative */
/* Perform strength reduction and induction variable elimination. */
/* Pseudo registers created during this function will be beyond the last
valid index in several tables including n_times_set and regno_last_uid.
This does not cause a problem here, because the added registers cannot be
givs outside of their loop, and hence will never be reconsidered.
But scan_loop must check regnos to make sure they are in bounds. */
static void
strength_reduce (scan_start, end, loop_top, insn_count,
loop_start, loop_end, nregs)
rtx scan_start;
rtx end;
rtx loop_top;
int insn_count;
rtx loop_start;
rtx loop_end;
int nregs;
{
rtx p;
rtx inc_val;
rtx mult_val;
int dest_regno;
int biv_found;
/* This is 1 if current insn could be executed zero times in the loop. */
int maybe_never = 0;
/* List of all possible basic induction variables. */
struct iv_class *iv_list = 0;
/* Temporary list pointers for traversing iv_list. */
struct iv_class *bl, *backbl;
/* Ratio of extra register life span we can justify
for saving an instruction. More if loop doesn't call subroutines
since in that case saving an insn makes more difference
and more registers are available. */
/* ??? could set this to last value of threshold in move_movables */
int threshold = loop_has_call ? 17 : 34;
/* Map of pseudo-register replacements. */
rtx *reg_map;
int call_seen;
induct_var = (enum iv_mode *) alloca (nregs * sizeof (induct_var[0]));
bzero ((char *)induct_var, nregs * sizeof (induct_var[0]));
induct_struct = (struct induction **)
alloca (nregs * sizeof (struct induction *));
bzero ((char *)induct_struct, nregs * sizeof (struct induction *));
class_struct = (struct iv_class **)
alloca (nregs * sizeof (struct iv_class *));
bzero ((char *)class_struct, nregs * sizeof (struct iv_class *));
/* Scan through loop to find all possible bivs. */
for (p = NEXT_INSN (loop_start); p != end; p = NEXT_INSN (p))
{
if (GET_CODE (p) == INSN
&& GET_CODE (PATTERN (p)) == SET
&& GET_CODE (SET_DEST (PATTERN (p))) == REG)
{
dest_regno = REGNO (SET_DEST (PATTERN (p)));
if (induct_var[dest_regno] != NOT_BASIC_INDUCT
&& dest_regno >= FIRST_PSEUDO_REGISTER)
{
if (basic_induction_var (SET_SRC (PATTERN (p)), dest_regno,
&inc_val, &mult_val))
{
/* It is a possible basic induction variable.
Create and initialize an induction structure for it. */
struct induction *v =
(struct induction *) alloca (sizeof (struct induction));
v->insn = p;
v->src_regno = dest_regno;
v->dest_regno = dest_regno;
v->mult_val = mult_val;
v->add_val = inc_val;
v->mode = GET_MODE (SET_DEST (PATTERN (p)));
/* Add this to the reg's iv_class, creating a class
if this is the first incrementation of the reg. */
bl = class_struct[dest_regno];
if (bl)
{
v->family = bl->biv;
bl->biv = v;
bl->biv_count++;
}
else
{
/* Create and initialize new iv_class. */
bl = (struct iv_class *) alloca (sizeof (struct iv_class));
bl->regno = dest_regno;
bl->biv = v;
v->family = 0;
bl->giv = 0;
bl->biv_count = 1;
bl->giv_count = 0;
/* Set initial value to the reg itself. */
bl->initial_value = SET_DEST (PATTERN (p));
/* We haven't seen the intializing insn yet */
bl->init_insn = 0;
bl->eliminable = 0;
bl->nonneg = 0;
/* Add this insn to iv_list. */
bl->next = iv_list;
iv_list = bl;
/* Put it in the array of iv_lists. */
class_struct[dest_regno] = bl;
}
induct_var[dest_regno] = BASIC_INDUCT;
if (loop_dump_stream)
{
fprintf (loop_dump_stream,
"Insn %d: possible biv, reg %d,",
INSN_UID (p), dest_regno);
if (GET_CODE (inc_val) == CONST_INT)
fprintf (loop_dump_stream, " const = %d\n",
INTVAL (inc_val));
else
{
fprintf (loop_dump_stream, " const = ");
print_rtl (loop_dump_stream, inc_val);
fprintf (loop_dump_stream, "\n");
}
}
}
else
induct_var[dest_regno] = NOT_BASIC_INDUCT;
}
}
}
/* Scan iv_list to remove all regs that proved not to be bivs.
Make a sanity check against n_times_set. */
biv_found = 0;
for (backbl = bl = iv_list; bl; backbl = bl, bl = bl->next)
{
if (induct_var[bl->regno] != BASIC_INDUCT)
{
/* Not a basic induction variable, remove this iv_class. */
if (backbl == bl)
iv_list = bl->next;
else
backbl->next = bl->next;
if (loop_dump_stream)
fprintf (loop_dump_stream, "Reg %d: biv discarded, not induct\n",
bl->regno);
}
else if (n_times_set[bl->regno] != bl->biv_count)
{
/* This happens if register modified by subreg, etc. */
/* Make sure it is not recognized as a basic induction var: */
/* remove this iv_class from iv_list. */
induct_var[bl->regno] = NOT_BASIC_INDUCT;
if (backbl == bl)
iv_list = bl->next;
else
backbl->next = bl->next;
if (loop_dump_stream)
fprintf (loop_dump_stream, "Reg %d: biv discarded, count error\n",
bl->regno);
}
else
{
/* This is a valid basic induction variable. */
biv_found++;
if (loop_dump_stream)
fprintf (loop_dump_stream, "Reg %d: biv verified\n", bl->regno);
}
}
/* Exit if there are no bivs. */
if (!iv_list)
return;
/* Find initial value for each biv. */
/* Search backwards from loop_start, halting at first label
or when all bivs have been seen. */
call_seen = 0;
p = loop_start;
while (biv_found)
{
p = PREV_INSN (p);
if (p == 0)
break;
if (GET_CODE (p) == CALL_INSN)
call_seen = 1;
if (GET_CODE (p) == INSN
&& GET_CODE (PATTERN (p)) == SET)
{
rtx dest = SET_DEST (PATTERN (p));
while (GET_CODE (dest) == SUBREG
|| GET_CODE (dest) == ZERO_EXTRACT
|| GET_CODE (dest) == SIGN_EXTRACT
|| GET_CODE (dest) == STRICT_LOW_PART)
dest = XEXP (dest, 0);
if (GET_CODE (dest) == REG)
{
int dest_regno = REGNO (dest);
if (induct_var[dest_regno] == BASIC_INDUCT
&& class_struct[dest_regno]->init_insn == 0)
{
/* This is the first modification found for this reg. */
rtx src = SET_SRC (PATTERN (p));
/* Record the intializing INSN */
class_struct[dest_regno]->init_insn = p;
if (loop_dump_stream)
fprintf (loop_dump_stream, "Biv %d initialized at insn %d: ",
dest_regno, INSN_UID (p));
/* Save value if it is a constant or register. */
if (CONSTANT_P (src)
|| (GET_CODE (src) == REG
/* Don't try to use a value in a hard reg
across a call which clobbers it. */
&& ! (REGNO (src) < FIRST_PSEUDO_REGISTER
&& call_used_regs[REGNO (src)]
&& call_seen)
&& ! reg_set_between_p (src, p, loop_start)))
{
class_struct[dest_regno]->initial_value = src;
if (loop_dump_stream)
fprintf (loop_dump_stream, "initial value ");
if (loop_dump_stream)
{
if (GET_CODE (src) == CONST_INT)
fprintf (loop_dump_stream, "%d\n", INTVAL (src));
else
{
print_rtl (loop_dump_stream, src);
fprintf (loop_dump_stream, "\n");
}
}
}
else
{
/* Biv initial value is not simple move,
so let it keep intial value of "itself". */
if (loop_dump_stream)
fprintf (loop_dump_stream, "complex initial value\n");
}
biv_found--;
}
}
}
else if (GET_CODE (p) == CODE_LABEL)
break;
}
/* Search the loop for general induction variables. */
/* A register is a giv if: it is only set once, it is a function of a
biv and a constant (or invariant), and it is not a biv. */
p = scan_start;
while (1)
{
p = NEXT_INSN (p);
/* At end of a straight-in loop, we are done.
At end of a loop entered at the bottom, scan the top. */
if (p == scan_start)
break;
if (p == end)
{
if (loop_top != 0)
p = NEXT_INSN (loop_top);
else
break;
if (p == scan_start)
break;
}
/* Look for a general induction variable in a register. */
if (GET_CODE (p) == INSN
&& GET_CODE (PATTERN (p)) == SET
&& GET_CODE (SET_DEST (PATTERN (p))) == REG
&& ! may_not_optimize[REGNO (SET_DEST (PATTERN (p)))])
{
int src_regno;
rtx add_val;
rtx mult_val;
int benefit;
rtx regnote = 0;
struct induction *forces = 0;
struct induction *forces2 = 0;
dest_regno = REGNO (SET_DEST (PATTERN (p)));
if (dest_regno < FIRST_PSEUDO_REGISTER)
continue;
if (/* Normal giv. */
((benefit = general_induction_var (SET_SRC (PATTERN (p)),
&src_regno, &add_val,
&mult_val,
&forces, &forces2))
/* Giv set with call to a library routine. */
|| ((regnote = loop_find_reg_equal (p))
&&
(benefit = general_induction_var (XEXP (regnote, 0),
&src_regno,
&add_val, &mult_val,
&forces, &forces2))))
/* Don't try to handle any regs made by loop optimization.
We have nothing on them in regno_first_uid, etc. */
&& dest_regno < old_max_reg
/* Don't recognize a BASIC_INDUCT_VAR here. */
&& dest_regno != src_regno
/* This must be the only place where the register is set. */
&& (n_times_set[dest_regno] == 1
|| (benefit = consec_sets_giv (benefit, p,
src_regno, dest_regno,
&add_val, &mult_val))))
{
int count;
struct induction *v =
(struct induction *) alloca (sizeof (struct induction));
rtx temp;
record_giv (v, p, src_regno, dest_regno, mult_val, add_val, benefit,
forces, forces2, DEST_REG, maybe_never, 0, loop_end);
/* Skip the consecutive insns, if there are any. */
for (count = v->consec - 1; count >= 0; count--)
{
/* If first insn of libcall sequence, skip to end. */
/* Do this at start of loop, since INSN is guaranteed to
be an insn here. */
if (temp = find_reg_note (p, REG_LIBCALL, 0))
{
/* Eliminating a libcall does more good than
eliminating a single insn to do the same job. */
benefit += LIBCALL_BENEFIT;
p = XEXP (temp, 0);
}
do p = NEXT_INSN (p);
while (GET_CODE (p) == NOTE);
}
}
}
#ifndef DONT_REDUCE_ADDR
/* Look for givs which are memory addresses. */
/* This resulted in worse code on a VAX 8600. I wonder if it
still does. */
if (GET_CODE (p) == INSN)
find_mem_givs (PATTERN (p), p, maybe_never, loop_end);
#endif
/* Past a label or a jump, we get to insns for which we can't count
on whether or how many times they will be executed during each
iteration. Givs found afterwards cannot be marked replaceable. */
if (GET_CODE (p) == CODE_LABEL || GET_CODE (p) == JUMP_INSN)
maybe_never = 1;
}
/* Try to prove that the loop counter variable (if any) is always
nonnegative; if so, record that fact with a REG_NONNEG note
so that "decrement and branch until zero" insn can be used. */
check_dbra_loop (loop_end, iv_list, insn_count, loop_start);
/* Create reg_map to hold substitutions for replaceable giv regs. */
reg_map = (rtx *) alloca (nregs * sizeof (rtx));
bzero ((char *)reg_map, nregs * sizeof (rtx));
/* Examine each iv class for feasibility of strength reduction/induction
variable elimination. */
for (bl = iv_list; bl; bl = bl->next)
{
struct induction *v;
int benefit;
int replaceable;
int all_reduced;
rtx final_value = 0;
/* Test whether it will be possible to eliminate this biv
provided all givs are reduced. This is possible if either
the reg is not used outside the loop, or we can compute
what its final value will be.
Don't try if we put a REG_NONNEG note on the endtest for this biv.
??? That should be only on machines that have dbra insns. */
/* Compare against bl->init_insn rather than loop_start.
We aren't concerned with any uses of the biv between
init_insn and loop_start since these won't be affected
by the value of the biv elsewhere in the function, so
long as init_insn doesn't use the biv itself.
March 14, 1989 -- self@bayes.arc.nasa.gov */
if ((uid_luid[regno_last_uid[bl->regno]] < INSN_LUID (loop_end)
&& bl->init_insn
&& INSN_UID (bl->init_insn) < max_uid
&& uid_luid[regno_first_uid[bl->regno]] >= INSN_LUID (bl->init_insn)
&& ! reg_mentioned_p (SET_DEST (PATTERN (bl->biv->insn)),
SET_SRC (PATTERN (bl->init_insn)))
&& ! bl->nonneg)
|| (final_value = final_biv_value (bl, loop_end)))
check_eliminate_biv (bl, loop_start, end);
else
{
if (loop_dump_stream)
{
fprintf (loop_dump_stream,
"Cannot eliminate biv %d.\n",
bl->regno);
fprintf (loop_dump_stream,
"First use: insn %d, last use: insn %d.\n",
regno_first_uid[bl->regno],
regno_last_uid[bl->regno]);
}
}
/* This will be true at the end, if all givs which depend on this
biv have been strength reduced.
We can't (currently) eliminate the biv unless this is so. */
all_reduced = 1;
/* Check each giv in this class. */
for (v = bl->giv; v; v = v->family)
{
struct induction *tv;
if (v->ignore)
continue;
benefit = v->benefit;
replaceable = v->replaceable;
/* Reduce benefit if not replaceable, since we will insert
a move-insn to replace the insn that calculates this giv. */
if (!replaceable && ! bl->eliminable)
benefit -= COPY_PENALTY;
/* Decrease the benefit to count the add-insns that we will
insert to increment the reduced reg for the giv. */
benefit -= ADD_BENEFIT * bl->biv_count;
/* Find all equivalent givs (that bear same relation to the biv).
Link them via the `same' field and add their benefits together.
They can be replaced with a single register. */
for (tv = v->family; tv; tv = tv->family)
{
if (tv->ignore == 0
&& tv->src_regno == v->src_regno
&& rtx_equal_p (tv->mult_val, v->mult_val)
&& rtx_equal_p (tv->add_val, v->add_val))
{
benefit += tv->benefit;
if (! tv->replaceable)
benefit -= COPY_PENALTY;
v->lifetime += tv->lifetime;
v->times_used += tv->times_used;
tv->ignore = 1;
/* Link them together via `same' field. */
tv->same = v->same;
v->same = tv;
if (loop_dump_stream)
fprintf (loop_dump_stream,
"giv of insn %d combined with that of %d.\n",
INSN_UID (v->insn), INSN_UID (tv->insn));
}
}
/* Decide whether to strength-reduce this giv
or to leave the code unchanged
(recompute it from the biv each time it is used).
This decision can be made independently for each giv. */
/* ??? Perhaps attempt to guess whether autoincrement will handle
some of the new add insns; if so, can increase BENEFIT
(undo the subtraction of ADD_BENEFIT that was done above). */
/* If an insn is not to be strength reduced, then set its ignore
flag, and clear all_reduced. */
/* Is it right to consider times_used? */
/* ??? What about the insns that are 'forced' by this one?
Although this insn is not worthwhile to reduce, it may be
worthwhile to reduce the simpler givs used to compute this
complex giv. */
/* ??? Hey! If a giv has its forces field set, then that means
it is not computed directly from the biv, it is instead computed
from a simpler giv. If we define UNFORCE_INSNS, then the simpler
giv will be considered for strength reduction, and this giv should
not cause all_reduced to be cleared because it DOESN'T use the
biv!!! If the simpler giv can not be reduced, then that simpler
biv will still cause all_reduced to be cleared. */
if (benefit <= 0)
{
if (loop_dump_stream)
fprintf (loop_dump_stream, "giv of insn %d, no benefit\n",
INSN_UID (v->insn));
v->ignore = 1;
all_reduced = 0;
}
if (v->lifetime * threshold * benefit < insn_count)
{
if (loop_dump_stream)
fprintf (loop_dump_stream,
"giv of insn %d not worth while, %d vs %d.\n",
INSN_UID (v->insn),
v->lifetime * threshold * benefit, insn_count);
v->ignore = 1;
all_reduced = 0;
}
/* Now check that we can increment the reduced giv
without needing a multiply insn. If not, reject it. */
if (! v->ignore)
{
int success = 1;
for (tv = bl->biv; tv; tv = tv->family)
if (tv->mult_val == const1_rtx)
success &= product_cheap_p (tv->add_val, v->mult_val);
if (! success)
{
if (loop_dump_stream)
fprintf (loop_dump_stream,
"giv of insn %d: would need a multiply.\n",
INSN_UID (v->insn));
v->ignore = 1;
all_reduced = 0;
}
}
}
/* Reduce each giv that we decided to reduce. */
for (v = bl->giv; v; v = v->family)
{
struct induction *tv;
if (! v->ignore)
{
rtx new_reg;
/* Note Iris compiler dies if ?: is used inside gen_reg_rtx. */
if (v->giv_type == DEST_ADDR)
new_reg = gen_reg_rtx (Pmode);
else
new_reg = gen_reg_rtx (GET_MODE (SET_DEST (PATTERN (v->insn))));
/* For each place where the biv is incremented,
add an insn to increment the new, reduced reg for the giv.
Insert it before the insn that sets the biv,
so that the biv increment remains last before the endtest,
so that dbra will still be recognized. */
for (tv = bl->biv; tv; tv = tv->family)
{
struct induction *iv;
rtx before_insn = tv->insn;
/* If this increment is between the setting of the giv and
its use, don't increment until after the use. */
for (iv = v; iv; iv = iv->same)
{
if (INSN_LUID (tv->insn) <= INSN_LUID (iv->insn)
&& ((iv->forces
&& (INSN_LUID (tv->insn)
>= INSN_LUID (iv->forces->insn))
|| (iv->forces2
&& (INSN_LUID (tv->insn)
>= INSN_LUID (iv->forces2->insn))))))
{
before_insn = NEXT_INSN (iv->insn);
break;
}
}
if (tv->mult_val == const1_rtx)
emit_iv_inc (tv->add_val, v->mult_val,
new_reg, before_insn);
else /* tv->mult_val == const0_rtx */
/* A multiply is acceptable here
since this is presumed to be seldom executed. */
emit_iv_init_code (tv->add_val, v->mult_val,
v->add_val, new_reg, before_insn);
}
/* Add code at loop start to initialize giv's reduced reg. */
emit_iv_init_code (bl->initial_value, v->mult_val,
v->add_val, new_reg, loop_start);
/* If the initial value uses a register,
then we may have just extended its range of appearance.
Update this conservatively for the sake of outer loops. */
if (GET_CODE (bl->initial_value) == REG
&& (uid_luid[regno_last_uid[REGNO (bl->initial_value)]]
< INSN_LUID (loop_start)))
uid_luid[regno_last_uid[REGNO (bl->initial_value)]]
= INSN_LUID (loop_start);
/* For each giv register that can be reduced now:
delete old insn that modifies the giv,
if replaceable, substitute reduced reg
wherever the old giv occurs;
else add new move insn "giv_reg = reduced_reg". */
for (tv = v; tv; tv = tv->same)
{
/* Record the identity of the reduced reg. */
tv->new_reg = new_reg;
if (tv->giv_type == DEST_ADDR)
{
/* Store reduced reg as the address in the memref
where we found this giv. */
* tv->location = new_reg;
}
else if (tv->replaceable)
{
reg_map[tv->dest_regno] = new_reg;
/* If giv lives after end of loop,
emit insn to copy reduced reg into old reg,
at the end of the loop.
?? insufficient; used before loop could
mean live after loop, due to surrounding loop. */
/* Currently a giv used outside
the loop will not be marked replaceable,
so these deficiencies don't really hurt. */
if (uid_luid[regno_last_uid[tv->dest_regno]]
> uid_luid[INSN_UID (loop_end)])
{
/* ?? This won't work. We need to do this at
ALL exits. */
emit_insn_after (gen_rtx (SET, VOIDmode,
SET_DEST (PATTERN (tv->insn)),
new_reg),
loop_end);
abort ();
}
}
else
{
/* Not replaceable; emit an insn to set the
original giv reg from the reduced giv. */
int count;
rtx after_insn = tv->insn;
for (count = tv->consec; count > 0; count--)
after_insn = next_real_insn (after_insn);
/* Put new insn after, not before, in case
after_insn is the end of a libcall. */
emit_insn_after (gen_rtx (SET, VOIDmode,
SET_DEST (PATTERN (tv->insn)),
new_reg),
after_insn);
}
/* Delete the insn that used to set the old giv reg,
unless we modified an address in it.
In any case, delete the other insns used for this one. */
delete_insn_forces (tv, tv->giv_type != DEST_ADDR);
if (loop_dump_stream)
fprintf (loop_dump_stream, "giv at %d reduced to reg %d\n",
INSN_UID (tv->insn), REGNO (new_reg));
}
/* One set of equivalent givs has been strength-reduced. */
}
#if 0
else if (v->new_reg == 0)
{
/* This giv wasn't reduced and is not worth reducing. */
for (tv = v; tv; tv = tv->same)
if (loop_dump_stream)
fprintf (loop_dump_stream, "giv at %d not reduced\n",
INSN_UID (tv->insn));
all_reduced = 0;
}
#endif
}
/* All the givs in this family have been reduced if they merit it. */
/* Try to eliminate the biv, if it is a candidate.
This won't work if ! all_reduced,
since the givs we planned to use might not have been reduced. */
if (all_reduced == 1 && bl->eliminable)
{
/* Get the REG rtx for the biv. */
rtx reg = SET_DEST (PATTERN (bl->biv->insn));
for (p = loop_start; p != end; p = NEXT_INSN (p))
{
enum rtx_code code = GET_CODE (p);
if ((code == INSN || code == JUMP_INSN || code == CALL_INSN)
&& reg_mentioned_p (reg, PATTERN (p))
&& SET_DEST (PATTERN (p)) == cc0_rtx)
/* Found a compare instruction using this biv;
rewrite it to use a related giv. */
{
struct induction *v1;
/* If this is an insn which uses the biv ONLY in the
calculation of a giv which is in the family of this
biv, it's ok becuase it will go away when the giv is
reduced. */
for (v1 = bl->giv; v1; v1 = v1->family)
if (v1->insn == p)
{
if (v1->giv_type == DEST_REG
|| (v1->giv_type == DEST_ADDR
/* I thought the test was backwards,
but then I found the real problem
was in the subroutine. */
&& ! other_reg_use_p (reg, *(v1->location),
PATTERN (p))))
break;
}
if (!v1)
eliminate_biv (p, bl, loop_start);
}
}
/* Biv is no longer really needed inside the loop,
so delete all insns that set the biv. */
for (v = bl->biv; v; v = v->family)
delete_insn (v->insn);
/* ?? If we created a new test to bypass the loop entirely,
or otherwise drop straight in, based on this test, then
we might want to rewrite it also. This way some later
pass has more hope of removing the intialization of this
biv entirely. */
/* If final_value != 0, then biv may be used after loop end
and we must emit an insn to set it just in case. */
if (final_value != 0)
emit_insn_after (gen_rtx (SET, VOIDmode, reg, final_value),
loop_end);
if (loop_dump_stream)
fprintf (loop_dump_stream, "Reg %d: biv eliminated\n",
bl->regno);
}
}
/* Go through all the instructions in the loop, making all the
register substitutions scheduled in REG_MAP. */
for (p = loop_start; p != end; p = NEXT_INSN (p))
if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
|| GET_CODE (p) == CALL_INSN)
{
rtx tail;
replace_regs (PATTERN (p), reg_map, nregs);
/* Subsitute registers in the equivalent expression also. */
for (tail = REG_NOTES (p); tail; tail = XEXP (tail, 1))
if (REG_NOTE_KIND (tail) == REG_EQUAL
|| REG_NOTE_KIND (tail) == REG_EQUIV)
replace_regs (XEXP (tail, 0), reg_map, nregs);
}
if (loop_dump_stream)
fprintf (loop_dump_stream, "\n");
}
/* Nonzero if register REG appears somewhere within IN, other than in
subexpressions EQ to EXPR. This is a modification of reg_mentioned_p. */
int
other_reg_use_p (reg, expr, in)
register rtx reg, expr, in;
{
register char *fmt;
register int i;
register enum rtx_code code;
if (in == 0 || in == expr)
return 0;
if (reg == in)
return 1;
code = GET_CODE (in);
switch (code)
{
/* Compare registers by number. */
case REG:
return GET_CODE (reg) == REG && REGNO (in) == REGNO (reg);
/* These codes have no constituent expressions
and are unique. */
case CC0:
case PC:
case CONST_INT:
case CONST_DOUBLE:
case SYMBOL_REF:
case CODE_LABEL:
return 0;
}
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 (in, i) - 1; j >= 0; j--)
if (other_reg_use_p (reg, expr, XVECEXP (in, i, j)))
return 1;
}
else if (fmt[i] == 'e'
&& other_reg_use_p (reg, expr, XEXP (in, i)))
return 1;
}
return 0;
}
/* Scan X for memory refs and check each memory address
as a possible giv. INSN is the insn whose pattern X comes from.
MAYBE_NEVER is 1 if the loop might execute INSN zero times. */
static void
find_mem_givs (x, insn, maybe_never, loop_end)
rtx x;
rtx insn;
int maybe_never;
rtx loop_end;
{
register int i, j;
register enum rtx_code code;
register char *fmt;
if (x == 0)
return;
code = GET_CODE (x);
switch (code)
{
case REG:
case CONST_INT:
case CONST:
case CONST_DOUBLE:
case SYMBOL_REF:
case LABEL_REF:
case PC:
case CC0:
case ADDR_VEC:
case ADDR_DIFF_VEC:
case USE:
case CLOBBER:
return;
case MEM:
{
int src_regno;
rtx add_val;
rtx mult_val;
int benefit;
struct induction *forces = 0;
struct induction *forces2 = 0;
benefit = general_induction_var (XEXP (x, 0),
&src_regno, &add_val, &mult_val,
&forces, &forces2);
if (benefit > 0)
{
/* Found one; record it. */
struct induction *v =
(struct induction *) oballoc (sizeof (struct induction));
record_giv (v, insn, src_regno, 0, mult_val, add_val, benefit,
forces, forces2, DEST_ADDR, maybe_never, &XEXP (x, 0),
loop_end);
}
return;
}
}
/* Recursively scan the subexpressions for other mem refs. */
fmt = GET_RTX_FORMAT (code);
for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
if (fmt[i] == 'e')
find_mem_givs (XEXP (x, i), insn, maybe_never, loop_end);
else if (fmt[i] == 'E')
for (j = 0; j < XVECLEN (x, i); j++)
find_mem_givs (XVECEXP (x, i, j), insn, maybe_never, loop_end);
}
/* Fill in the data about one giv.
V is the `struct induction' in which we record the giv. (It is
allocated by the caller, with alloca.)
INSN is the insn that sets it.
BENEFIT estimates the savings from deleting this insn.
TYPE is DEST_REG or DEST_ADDR; it says whether the giv is computed
into a register or is used as a memory address.
SRC_REGNO is the biv reg number which the giv is computed from.
DEST_REGNO is the giv's reg number (if the giv is stored in a reg).
MULT_VAL and ADD_VAL are the coefficients used to compute the giv.
FORCES and FORCES2, if nonzero, are other `struct induction's for
other givs which are used to compute this giv indirectly.
LOCATION points to the place where this giv's value appears in INSN. */
static void
record_giv (v, insn, src_regno, dest_regno, mult_val, add_val, benefit,
forces, forces2, type, maybe_never, location, loop_end)
struct induction *v;
rtx insn;
int src_regno, dest_regno;
rtx mult_val, add_val;
int benefit;
struct induction *forces, *forces2;
enum g_types type;
int maybe_never;
rtx *location;
rtx loop_end;
{
struct induction *b;
struct iv_class *bl;
v->insn = insn;
v->src_regno = src_regno;
v->giv_type = type;
v->dest_regno = dest_regno;
v->mult_val = mult_val;
v->add_val = add_val;
v->benefit = benefit;
v->location = location;
if (type == DEST_ADDR)
{
v->mode = GET_MODE (*location);
v->consec = 0;
v->lifetime = 1;
v->times_used = 1;
}
else /* type == DEST_REG */
{
v->mode = GET_MODE (SET_DEST (PATTERN (insn)));
v->consec = n_times_set[dest_regno] - 1;
v->lifetime = (uid_luid[regno_last_uid[dest_regno]]
- uid_luid[regno_first_uid[dest_regno]]);
v->times_used = n_times_used[dest_regno];
}
v->same = 0;
v->forces = 0;
v->forces2 = 0;
v->ignore = 0;
v->new_reg = 0;
/* Mark giv as forced if it is only used to compute another giv. */
/* This check is not sufficient as INSN may have been moved giving
it a new uid, so make another check by calculating lifetimes.
This is overconservative but seems to be correct. */
if (forces)
{
v->benefit += forces->benefit;
if ((regno_last_uid[forces->dest_regno] == INSN_UID (insn)
||
((uid_luid[regno_last_uid[forces->dest_regno]]
- uid_luid[regno_first_uid[forces->dest_regno]])
== (INSN_LUID (insn) - INSN_LUID (forces->insn))))
&& !reg_used_between_p (SET_DEST (PATTERN (forces->insn)),
forces->insn, insn))
{
v->forces = forces;
forces->ignore = 1;
}
}
if (forces2)
{
v->benefit += forces2->benefit;
if ((regno_last_uid[forces2->dest_regno] == INSN_UID (insn)
||
((uid_luid[regno_last_uid[forces2->dest_regno]]
- uid_luid[regno_first_uid[forces2->dest_regno]])
== (INSN_LUID (insn) - INSN_LUID (forces2->insn))))
&& !reg_used_between_p (SET_DEST (PATTERN (forces2->insn)),
forces2->insn, insn))
{
if (v->forces)
v->forces2 = forces2;
else
v->forces = forces2;
forces2->ignore = 1;
}
}
if (type == DEST_REG)
{
induct_var[dest_regno] = GENERAL_INDUCT;
induct_struct[dest_regno] = v;
}
/* Add the giv to the class of givs computed from one biv. */
bl = class_struct[src_regno];
if (bl)
{
v->family = bl->giv;
bl->giv = v;
/* Don't count DEST_ADDR. This is supposed to count the number of
insns that calculate givs. */
if (type == DEST_REG)
bl->giv_count++;
bl->total_benefit += benefit;
}
else
/* Fatal error, biv missing for this giv? */
abort ();
if (type == DEST_ADDR)
v->replaceable = 1;
else
{
/* The giv can be replaced outright by the reduced register if
- the insn that sets the giv is always executed on any iteration
on which the giv is used at all
(there are two ways to deduce this:
either the insn is executed on every iteration,
or all uses follow that insn in the same basic block),
- the giv is not used before the insn that sets it,
i.e. no definition outside loop reaches into loop
- no assignments to the biv occur during the giv's lifetime. */
/* Is this right? Don't we need to make sure the giv is not used
outside the loop. Someday we will know where all the loop exits
are so we can do better, but until then....
March 18, 1989 -- self@bayes.arc.nasa.gov */
if (regno_first_uid[dest_regno] == INSN_UID (insn)
/* Previous line always fails if INSN was moved by loop opt. */
&& uid_luid[regno_last_uid[dest_regno]] < INSN_LUID (loop_end)
&& (!maybe_never || last_use_this_basic_block (dest_regno, insn)))
{
v->replaceable = 1;
for (b = bl->biv; b; b = b->family)
{
if ((uid_luid[INSN_UID (b->insn)] >= uid_luid[regno_first_uid[dest_regno]])
&&
(uid_luid[INSN_UID (b->insn)]
<= uid_luid[regno_last_uid[dest_regno]]))
{
v->replaceable = 0;
break;
}
}
}
else
v->replaceable = 0;
}
if (loop_dump_stream)
{
if (type == DEST_REG)
fprintf (loop_dump_stream, "Insn %d: giv reg %d",
INSN_UID (insn), dest_regno);
else
fprintf (loop_dump_stream, "Insn %d: dest address",
INSN_UID (insn));
fprintf (loop_dump_stream, " src reg %d benefit %d",
src_regno, v->benefit);
fprintf (loop_dump_stream, " used %d lifetime %d",
v->times_used, v->lifetime);
if (v->replaceable)
fprintf (loop_dump_stream, " replaceable");
if (GET_CODE (mult_val) == CONST_INT)
fprintf (loop_dump_stream, " mult %d",
INTVAL (mult_val));
else
{
fprintf (loop_dump_stream, " mult ");
print_rtl (loop_dump_stream, mult_val);
}
if (GET_CODE (add_val) == CONST_INT)
fprintf (loop_dump_stream, " add %d",
INTVAL (add_val));
else
{
fprintf (loop_dump_stream, " add ");
print_rtl (loop_dump_stream, add_val);
}
}
if (loop_dump_stream && v->forces)
fprintf (loop_dump_stream, " forces insn %d", INSN_UID (v->forces->insn));
if (loop_dump_stream && v->forces2)
fprintf (loop_dump_stream, " forces insn %d", INSN_UID (v->forces2->insn));
if (loop_dump_stream && v->consec)
fprintf (loop_dump_stream, " consec %d", v->consec);
if (loop_dump_stream)
fprintf (loop_dump_stream, "\n");
}
/* Delete the insns forced by the insn described by V.
If THIS_TOO is nonzero, delete that insn itself as well. */
static void
delete_insn_forces (v, this_too)
struct induction *v;
int this_too;
{
rtx x, p;
int count;
rtx insn;
if (this_too)
{
insn = v->insn;
for (count = v->consec; count >= 0; count--)
{
/* If first insn of libcall sequence, skip to end. */
/* Do this at start of loop, since p is guaranteed to
be an insn here. */
if (x = find_reg_note (insn, REG_LIBCALL, 0))
insn = XEXP (x, 0);
if (x = find_reg_note (insn, REG_RETVAL, 0))
{
/* This is a library call; delete all insns backward until get to
first insn in this group. */
rtx first = XEXP (x, 0);
for (p = insn; p != first; p = PREV_INSN (p))
delete_insn (p);
/* Delete first insn also. */
delete_insn (p);
}
else
delete_insn (insn);
do insn = NEXT_INSN (insn);
while (GET_CODE (insn) == NOTE);
}
}
if (v->forces)
delete_insn_forces (v->forces, 1);
if (v->forces2)
delete_insn_forces (v->forces2, 1);
}
/* Check whether an insn is an increment legitimate for a basic induction var.
X is the source of the insn.
DEST_REG is the putative biv, also the destination of the insn.
We accept patterns of these forms:
REG = REG + INVARIANT
REG = INVARIANT + REG
REG = REG - CONSTANT
If X is suitable, we return 1,
and store the factor multiplying REF in X into *MULT_VAL
and the additive term into *INC_VAL.
Otherwise we return 0. */
static int
basic_induction_var (x, dest_regno, inc_val, mult_val)
register rtx x;
int dest_regno;
rtx *inc_val;
rtx *mult_val;
{
register enum rtx_code code;
rtx arg;
if (x == 0)
return 0;
code = GET_CODE (x);
switch (code)
{
case PLUS:
if (GET_CODE (XEXP (x, 0)) == REG
&& REGNO (XEXP (x, 0)) == dest_regno)
arg = XEXP (x, 1);
else if (GET_CODE (XEXP (x, 1)) == REG
&& REGNO (XEXP (x, 1)) == dest_regno)
arg = XEXP (x, 0);
else
return 0;
if (invariant_p (arg) == 1)
*inc_val = arg;
else
return 0;
*mult_val = const1_rtx;
return 1;
case MINUS:
if (GET_CODE (XEXP (x, 0)) == REG
&& REGNO (XEXP (x, 0)) == dest_regno
&& GET_CODE (XEXP (x, 1)) == CONST_INT)
*inc_val = gen_rtx (CONST_INT, VOIDmode,
- INTVAL (XEXP (x, 1)));
else
return 0;
*mult_val = const1_rtx;
return 1;
/* Can accept constant setting of biv only when inside inner most loop.
Otherwise, a biv of an inner loop may be incorrectly recognized
as a biv of the outer loop,
causing code to be moved INTO the inner loop. */
case REG:
if (!invariant_p (x))
return 0;
case CONST_INT:
case SYMBOL_REF:
case CONST:
if (loops_enclosed == 1)
{
*inc_val = x;
*mult_val = const0_rtx;
return 1;
}
else
return 0;
default:
return 0;
}
}
/* A general induction variable (giv) is any quantity that is a linear function
of a basic induction variable, i.e. giv = biv * mult_val + add_val.
The coefficients can be any loop invariant quantity.
A giv need not be computed directly from the biv;
it can be computed by way of other givs. */
/* Determine whether X computes a giv.
If it does, return a nonzero value
which is the benefit from eliminating the computation of X;
set *SRC_REGNO to the register number of the biv that it is computed from;
set *ADD_VAL and *MULT_VAL to the coefficients,
such that the value of X is biv * mult + add;
set forces (and forces2) to identify any other givs that are used
solely to compute this one. */
/* This routine recognizes four types of patterns that generate givs:
- giv = biv op invariant v = 0, g = 0
- giv1 = giv2 op invariant v = 0, g = giv2
where giv1 and giv2 are functions of the same biv
- giv1 = biv op giv2 v = giv2, g = 0
where giv2 is a function of biv
- giv1 = giv2 op giv3 v = giv3, g = giv2
where giv2 and giv3 are functions of the save biv */
static int
general_induction_var (x, src_regno, add_val, mult_val, forces, forces2)
rtx x;
int *src_regno;
rtx *add_val;
rtx *mult_val;
struct induction **forces;
struct induction **forces2;
{
register enum rtx_code code;
rtx arg;
struct induction *g = 0;
struct induction *v = 0;
int subexp = 0;
int tem;
if (x == 0)
return 0;
code = GET_CODE (x);
switch (code)
{
case NEG:
/* This can generate givs also, but it is not handled. */
return 0;
case MULT:
case UMULT:
/* Reject widening multiply in version 1.
That is safer than trying to handle it. */
{
enum machine_mode m0 = GET_MODE (XEXP (x, 0));
enum machine_mode m1 = GET_MODE (XEXP (x, 1));
if (m0 != VOIDmode && m0 != GET_MODE (x))
return 0;
if (m1 != VOIDmode && m1 != GET_MODE (x))
return 0;
}
case PLUS:
case MINUS:
/* Result is linear in both operands. */
/* Determine which operand is the biv, and put the other in ARG. */
if (GET_CODE (XEXP (x, 0)) == REG
&& induct_var[REGNO (XEXP (x, 0))] == BASIC_INDUCT)
{
*src_regno = REGNO (XEXP (x, 0));
arg = XEXP (x, 1);
}
else if (GET_CODE (XEXP (x, 1)) == REG
&& induct_var[REGNO (XEXP (x, 1))] == BASIC_INDUCT)
{
*src_regno = REGNO (XEXP (x, 1));
arg = XEXP (x, 0);
}
/* Check for an rtl subexpression that is a giv. Memory address
givs often look like (plus (reg) (mult (biv) (const))). */
/* Do this before checking for a giv operand, as this function will
fail if this special operand is not recognized. */
#ifndef DONT_REDUCE_ADDR
else if (tem = general_induction_var (XEXP (x, 1), src_regno,
add_val, mult_val,
forces, forces2)
&& code != MINUS)
{
/* Set subexp true so that this can be handled a little
differently from the normal case of g set. */
/* Note that SRC_REGNO is already set. */
subexp = TRUE;
g = (struct induction *) alloca (sizeof (struct induction));
g->mult_val = *mult_val;
g->add_val = *add_val;
/* Fake out the test below. */
g->replaceable = 1;
/* Count this multiply as a shift, since that's what it
really will do. */
if (tem == MULT_BENEFIT)
g->benefit = SHIFT_BENEFIT;
else
g->benefit = tem;
arg = XEXP (x, 0);
}
else if (tem = general_induction_var (XEXP (x, 0), src_regno,
add_val, mult_val,
forces, forces2))
{
/* Set subexp true so that this can be handled a little
differently from the normal case of g set. */
/* Note that SRC_REGNO is already set. */
subexp = TRUE;
g = (struct induction *) alloca (sizeof (struct induction));
g->mult_val = *mult_val;
g->add_val = *add_val;
/* Fake out the test below. */
g->replaceable = 1;
/* Count this multiply as a shift, since that's what it
really will do. */
if (tem == MULT_BENEFIT)
g->benefit = SHIFT_BENEFIT;
else
g->benefit = tem;
arg = XEXP (x, 1);
}
#endif
/* Also allow general induction variables.
Could have a mult followed by an add (i.e. an address calculation),
thereby generating two related general induction variables
of which only the second is actually used. */
/* Do this after checking both args for basic induction variables. */
else if (GET_CODE (XEXP (x, 0)) == REG
&& induct_var[REGNO (XEXP (x, 0))] == GENERAL_INDUCT)
{
g = induct_struct[REGNO (XEXP (x, 0))];
*src_regno = g->src_regno;
arg = XEXP (x, 1);
}
else if (GET_CODE (XEXP (x, 1)) == REG
&& induct_var[REGNO (XEXP (x, 1))] == GENERAL_INDUCT
&& code != MINUS)
{
g = induct_struct[REGNO (XEXP (x, 1))];
*src_regno = g->src_regno;
arg = XEXP (x, 0);
}
else
return 0;
/* Overall form of expression looks good. */
break;
/* Could handle these also. */
case DIV:
case UDIV:
/* For a 68020 could handle these? */
case LSHIFT:
case ASHIFT:
case ASHIFTRT:
case LSHIFTRT:
/* These operations are linear only in first operand.
Check for a biv or giv there; if found, put other operand in ARG. */
if (GET_CODE (XEXP (x, 0)) == REG
&& induct_var[REGNO (XEXP (x, 0))] == BASIC_INDUCT)
{
*src_regno = REGNO (XEXP (x, 0));
arg = XEXP (x, 1);
}
/* Also allow general induction variable. */
else if (GET_CODE (XEXP (x, 0)) == REG
&& induct_var[REGNO (XEXP (x, 0))] == GENERAL_INDUCT)
{
g = induct_struct[REGNO (XEXP (x, 0))];
*src_regno = g->src_regno;
arg = XEXP (x, 1);
}
else
return 0;
/* Overall form of expression looks good. */
break;
default:
return 0;
}
/* ARG is the operand that is NOT a biv or giv.
Test it for superficial validity. */
/* This is just a special case of invariant values,
it is not really needed, but it's a shortcut. */
if (GET_CODE (arg) == CONST_INT)
;
/* Depends on previous general induction variable, which has
the same basic induction variable */
/* This code detects mults that have been generated as shift and add. */
else if (GET_CODE (arg) == REG
&& induct_var[REGNO (arg)] == GENERAL_INDUCT
&& induct_struct[REGNO (arg)]->src_regno == *src_regno)
{
v = induct_struct[REGNO (arg)];
/* Dependence indicated by forces, sort of kludgey. */
}
/* Invariant expression, could be a constant-valued register. */
else if (invariant_p (arg) == 1)
;
/* Failure */
else
return 0;
/* Until we can do the correct thing, suppress use of nonreplaceable givs
as sources for other givs. */
if ((g && ! g->replaceable)
|| (v && ! v->replaceable))
return 0;
/* Now we know looks like a giv; extract the coefficients.
We can still fail if the coefficients are not what we can handle. */
/* Only succeed if result mult_val and add_val are only one level of rtl,
for example, (NEG:SI (REG:SI 34)) is not accepted.
This mainly causes problems with the MINUS code. */
switch (code)
{
case PLUS:
if (v && g)
{
if (GET_CODE (g->mult_val) == CONST_INT)
{
if (g->mult_val == const0_rtx)
*mult_val = v->mult_val;
else if (GET_CODE (v->mult_val) == CONST_INT)
*mult_val = gen_rtx (CONST_INT, VOIDmode,
INTVAL (g->mult_val)
+ INTVAL (v->mult_val));
else
return 0;
}
else if (v->mult_val == const0_rtx)
*mult_val = g->mult_val;
else
return 0;
if (GET_CODE (g->add_val) == CONST_INT)
{
if (g->add_val == const0_rtx)
*add_val = v->add_val;
else if (GET_CODE (v->add_val) == CONST_INT)
*add_val = gen_rtx (CONST_INT, VOIDmode,
INTVAL (g->add_val)
+ INTVAL (v->add_val));
else
return 0;
}
else if (v->add_val == const0_rtx)
*add_val = g->add_val;
else
return 0;
if (subexp)
{
/* g deleted when return, can't return pointer to it */
if (*forces2 == 0)
*forces2 = v;
return ADD_BENEFIT + g->benefit;
}
else
{
*forces = g;
*forces2 = v;
return ADD_BENEFIT;
}
}
else if (v)
{
if (GET_CODE (v->mult_val) == CONST_INT)
*mult_val = gen_rtx (CONST_INT, VOIDmode,
INTVAL (v->mult_val) + 1);
else
return 0;
*add_val = v->add_val;
*forces = v;
return ADD_BENEFIT;
}
else if (g)
{
*mult_val = g->mult_val;
if (GET_CODE (g->add_val) == CONST_INT
&& nonmemory_operand (arg, GET_MODE (arg)))
*add_val = plus_constant (arg, INTVAL (g->add_val));
else if (GET_CODE (arg) == CONST_INT
&& nonmemory_operand (g->add_val, GET_MODE (g->add_val)))
*add_val = plus_constant (g->add_val, INTVAL (arg));
else
/* Could succeed if arg == 0, but that will never occur. */
return 0;
if (subexp)
/* g deleted when return, can't return pointer to it */
return ADD_BENEFIT + g->benefit;
else
{
*forces = g;
return ADD_BENEFIT;
}
}
else
{
*mult_val = const1_rtx;
*add_val = arg;
return ADD_BENEFIT;
}
/* Takes a lot of code and will rarely succeed. */
case MINUS:
if (v && g)
{
/* G is the first argument of MINUS. */
if (GET_CODE (g->mult_val) == CONST_INT)
{
if (g->mult_val == const0_rtx)
#if 0 /* Should not have to fail here */
*mult_val = gen_rtx (NEG, SImode, v->mult_val);
#endif
return 0;
else if (GET_CODE (v->mult_val) == CONST_INT)
*mult_val = gen_rtx (CONST_INT, VOIDmode,
INTVAL (g->mult_val)
- INTVAL (v->mult_val));
else
return 0;
}
else if (v->mult_val == const0_rtx)
*mult_val = g->mult_val;
else
return 0;
if (GET_CODE (g->add_val) == CONST_INT)
{
if (g->add_val == const0_rtx)
#if 0 /* should not have to fail here */
*add_val = v->add_val;
#endif
return 0;
else if (GET_CODE (v->add_val) == CONST_INT)
*add_val = gen_rtx (CONST_INT, VOIDmode,
INTVAL (g->add_val)
- INTVAL (v->add_val));
else
return 0;
}
else if (v->add_val == const0_rtx)
*add_val = g->add_val;
else
return 0;
if (subexp)
{
/* G deleted when return, can't return pointer to it */
if (*forces2 == 0)
*forces2 = v;
return ADD_BENEFIT + g->benefit;
}
else
{
*forces = g;
*forces2 = v;
return ADD_BENEFIT;
}
}
else if (v)
{
if (GET_CODE (v->mult_val) != CONST_INT)
return 0;
if (arg == XEXP (x, 0)) /* giv1 = giv2 - biv */
{
*mult_val = gen_rtx (CONST_INT, VOIDmode,
INTVAL (v->mult_val) - 1);
*add_val = v->add_val;
}
else /* giv1 = biv - giv2 */
{
*mult_val = gen_rtx (CONST_INT, VOIDmode,
1 - INTVAL (v->mult_val));
if (GET_CODE (v->add_val) == CONST_INT)
*add_val = gen_rtx (CONST_INT, VOIDmode,
- INTVAL (v->add_val));
else
return 0;
}
*forces = v;
return ADD_BENEFIT;
}
else if (g)
{
if (arg == XEXP (x, 1))
*mult_val = g->mult_val;
else
{
if (GET_CODE (g->mult_val) == CONST_INT)
*mult_val = gen_rtx (CONST_INT, VOIDmode,
- INTVAL (g->mult_val));
else
return 0;
}
if (GET_CODE (g->add_val) == CONST_INT)
{
if (g->add_val == const0_rtx)
{
if (arg == XEXP (x, 1)) /* giv1 = giv2 - arg */
{
/* Fail unless arg is a constant. */
if (GET_CODE (arg) == CONST_INT)
*add_val = gen_rtx (CONST_INT, VOIDmode,
-INTVAL (arg));
else
return 0;
}
else /* giv1 = arg - giv2 */
*add_val = arg;
}
else if (GET_CODE (arg) == CONST_INT)
{
if (arg == XEXP (x, 1)) /* giv1 = giv2 - arg */
*add_val = gen_rtx (CONST_INT, VOIDmode,
INTVAL (g->add_val)
- INTVAL (arg));
else /* giv1 = arg - giv2 */
*add_val = gen_rtx (CONST_INT, VOIDmode,
INTVAL (arg),
- INTVAL (g->add_val));
}
else
return 0;
}
else
/* Could succeed if arg == 0, but that will never occur. */
return 0;
if (subexp)
/* G deleted when return, can't return pointer to it. */
return ADD_BENEFIT + g->benefit;
else
{
*forces = g;
return ADD_BENEFIT;
}
}
else if (GET_CODE (arg) == CONST_INT)
{
if (arg == XEXP (x, 1))
{
*add_val = gen_rtx (CONST_INT, VOIDmode, - INTVAL (arg));
*mult_val = const1_rtx;
}
else
{
*add_val = arg;
*mult_val = gen_rtx (CONST_INT, VOIDmode, -1);
}
return ADD_BENEFIT;
}
else
return 0;
/* UMULT can be handled like MULT since C ignores overflows. */
case MULT:
case UMULT:
if (v && g)
{
/* Quadratic term, just fail. */
return 0;
}
else if (v)
{
/* Quadratic term, just fail. */
return 0;
}
else if (g)
{
/* Takes a lot of code and will rarely succeed. */
/* dest = m * arg * b + a * arg */
if (GET_CODE (g->mult_val) == CONST_INT)
{
if (g->mult_val == const0_rtx)
*mult_val = const0_rtx;
else if (g->mult_val == const1_rtx)
*mult_val = arg;
else if (GET_CODE (arg) == CONST_INT)
*mult_val = gen_rtx (CONST_INT, VOIDmode,
INTVAL (g->mult_val) * INTVAL (arg));
else
return 0;
}
else
/* Could suceed if arg == 1 or 0, but this will never occur. */
return 0;
if (GET_CODE (g->add_val) == CONST_INT)
{
if (g->add_val == const0_rtx)
*add_val = const0_rtx;
else if (g->add_val == const1_rtx)
*add_val = arg;
else if (GET_CODE (arg) == CONST_INT)
*add_val = gen_rtx (CONST_INT, VOIDmode,
INTVAL (g->add_val) * INTVAL (arg));
else
return 0;
}
else
/* Could suceed if arg == 1 or 0, but this will never occur. */
return 0;
if (subexp)
/* G deleted when return, can't return pointer to it. */
return MULT_BENEFIT + g->benefit;
else
{
*forces = g;
return MULT_BENEFIT;
}
}
else
{
*mult_val = arg;
*add_val = const0_rtx;
return MULT_BENEFIT;
}
/* These are not worth the trouble. */
case DIV:
case UDIV:
return 0;
/* Handle these, but only for left shift. */
case LSHIFT:
case ASHIFT:
if (v && g)
{
/* Quadratic term, just fail. */
return 0;
}
else if (v)
{
/* Quadratic term, just fail. */
return 0;
}
else if (g)
{
/* Takes a lot of code and will rarely succeed. */
/* dest = ((m * b) << arg) + (a << arg) */
if (GET_CODE (g->mult_val) == CONST_INT)
{
if (g->mult_val == const0_rtx)
*mult_val = const0_rtx;
else if (GET_CODE (arg) == CONST_INT && INTVAL (arg) >= 0)
*mult_val = gen_rtx (CONST_INT, VOIDmode,
INTVAL (g->mult_val)
* (1 << INTVAL (arg)));
else
return 0;
}
else
/* Could suceed if arg == 0, but this will never occur. */
return 0;
if (GET_CODE (g->add_val) == CONST_INT)
{
if (g->add_val == const0_rtx)
*add_val = const0_rtx;
else if (GET_CODE (arg) == CONST_INT)
*add_val = gen_rtx (CONST_INT, VOIDmode,
INTVAL (g->add_val)
* (1 << INTVAL (arg)));
else
return 0;
}
else
/* Could suceed if arg == 0, but this will never occur. */
return 0;
if (subexp)
/* G deleted when return, can't return pointer to it. */
return SHIFT_BENEFIT + g->benefit;
else
{
*forces = g;
return SHIFT_BENEFIT;
}
}
if (GET_CODE (arg) == CONST_INT && INTVAL (arg) >= 0)
*mult_val = gen_rtx (CONST_INT, VOIDmode, 1 << INTVAL (arg));
else
return 0;
*add_val = const0_rtx;
return SHIFT_BENEFIT;
/* These are not worth the trouble. */
case ASHIFTRT:
case LSHIFTRT:
return 0;
/* should never reach here */
default:
abort ();
return 0;
}
}
/* Help detect a giv that is calculated by several consecutive insns;
for example,
giv = biv * M
giv = giv + A
The caller has already identified the first insn P as having a giv as dest;
we check that all other insns that set the same register follow
immediately after P, that they alter nothing else,
and that the result of the last is still a giv.
The value is 0 if the reg set in P is not really a giv.
Otherwise, the value is the amount gained by eliminating
all the consecutive insns that compute the value.
FIRST_BENEFIT is the amount gained by eliminating the first insn, P.
SRC_REGNO is the regno of the biv; DEST_REGNO is that of the giv.
The coefficients of the ultimate giv value are stored in
*MULT_VAL and *ADD_VAL. */
static int
consec_sets_giv (first_benefit, p, src_regno, dest_regno,
add_val, mult_val)
int first_benefit;
rtx p;
int src_regno;
int dest_regno;
rtx *add_val;
rtx *mult_val;
{
int count;
int benefit = first_benefit;
enum rtx_code code;
struct induction *forces, *forces2;
rtx temp;
int tem;
/* Initialize info used by general_induction_var. */
struct induction *v =
(struct induction *) oballoc (sizeof (struct induction));
v->src_regno = src_regno;
v->mult_val = *mult_val;
v->add_val = *add_val;
induct_var[dest_regno] = GENERAL_INDUCT;
induct_struct[dest_regno] = v;
count = n_times_set[dest_regno] - 1;
while (count > 0)
{
p = NEXT_INSN (p);
code = GET_CODE (p);
/* If libcall, skip to end of call sequence. */
if (code == INSN && (temp = find_reg_note (p, REG_LIBCALL, 0)))
p = XEXP (temp, 0);
if (code == INSN && GET_CODE (PATTERN (p)) == SET
&& GET_CODE (SET_DEST (PATTERN (p))) == REG
&& REGNO (SET_DEST (PATTERN (p))) == dest_regno
&& ((tem = general_induction_var (SET_SRC (PATTERN (p)), &src_regno,
add_val, mult_val,
&forces, &forces2))
/* Giv created by call to library routine. */
|| ((temp = loop_find_reg_equal (p))
&& (tem = general_induction_var (XEXP (temp, 0), &src_regno,
add_val, mult_val,
&forces, &forces2))))
&& src_regno == v->src_regno)
{
count--;
benefit += tem;
v->mult_val = *mult_val;
v->add_val = *add_val;
}
else if (code != NOTE)
{
induct_var[dest_regno] = UNKNOWN_INDUCT;
return 0;
}
}
return benefit;
}
/* Generate a SEQUENCE to multiply OP0 and OP1 with result in TARGET.
Use expand_mult to "optimally" do the multiply.
This also works for machines that do not have multiply insns.
If one of the operands is a constant, it must be the second. */
static rtx
gen_iv_mult (mode, op0, op1, target)
enum machine_mode mode;
register rtx op0, op1, target;
{
extern rtx gen_sequence ();
extern rtx start_sequence ();
rtx saved, result, temp;
saved = start_sequence ();
/* ??? It is very unmodular to use expand_mult here!
This should be redesigned. */
/* UNSIGNEDP arg can be zero since operands/target always same width. */
temp = expand_mult (mode, op0, op1, target, 0);
/* Move to target register, if expand_mult did not put it there. */
if (target != 0 && temp != target)
emit_move_insn (target, temp);
result = gen_sequence ();
end_sequence (saved);
return result;
}
/* Emit code to initialize an induction variable created by strength
reduction.
More precisely, emit code before INSERT_BEFORE
to set REG = B * M + A. */
static void
emit_iv_init_code (b, m, a, reg, insert_before)
rtx b; /* initial value of basic induction variable */
rtx m; /* multiplicative constant */
rtx a; /* additive constant */
rtx reg; /* destination register */
rtx insert_before;
{
rtx seq;
rtx result;
/* Prevent unexpected sharing of these rtx. */
a = copy_rtx (a);
b = copy_rtx (b);
start_sequence ();
result = expand_mult_add (b, m, a, GET_MODE (reg), 0);
if (reg != result)
emit_move_insn (reg, result);
seq = gen_sequence ();
end_sequence ();
emit_insn_before (seq, insert_before);
}
/* Emit code to increment the induction variable inside the loop.
Try to emit optimal code for the expression
REG = REG + BIV_ADD * GIV_MULT. */
static void
emit_iv_inc (biv_add, giv_mult, reg, insn)
rtx biv_add; /* increment value for biv */
rtx giv_mult; /* multiply value of giv */
rtx reg; /* create insn to set this reg */
rtx insn; /* where to insert the new insn */
{
emit_iv_init_code (biv_add, giv_mult, reg, reg, insn);
}
/* Test whethen BIV_ADD * GIV_MULT can be computed without
an actual multiply insn. Value is 1 if so. */
static int
product_cheap_p (biv_add, giv_mult)
rtx biv_add;
rtx giv_mult;
{
/* Indicates which of MULT/ADD are constants. */
int status = 0;
int const_val;
rtx tmp;
if (GET_CODE (biv_add) == CONST_INT)
status |= 0x1;
if (GET_CODE (giv_mult) == CONST_INT)
status |= 0x2;
switch (status)
{
case 0:
/* Neither is constant: would need a multiply insn, so fail. */
return 0;
case 1:
/* BIV_ADD value is constant */
/* Equivalent to state 2, just switch values of BIV_ADD and GIV_MULT
and fall through. */
tmp = biv_add;
biv_add = giv_mult;
giv_mult = tmp;
case 2:
/* GIV_MULT value is constant.
If it is 1, 0 or -1 then we win. */
const_val = INTVAL (giv_mult);
if (const_val < -1 || const_val > 1)
{
tmp = gen_iv_mult (GET_MODE (biv_add), biv_add, giv_mult, 0);
/* Don't emit a multiply insn, just fail instead. */
if ((GET_CODE (tmp) == SET && GET_CODE (SET_SRC (tmp)) == MULT)
/* Also fail if library call (which generates more
then two insn) is needed. */
|| (GET_CODE (tmp) == SEQUENCE && XVECLEN (tmp, 0) > 2))
return 0;
}
return 1;
case 3:
/* Both BIV_ADD and GIV_MULT are constant;
can compute the product at compile time. */
return 1;
default:
abort ();
}
}
/* Check to see if loop can be terminated by a "decrement and branch until
zero" instruction. If so, add a REG_NONNEG note to the branch insn if so.
Also try reversing an increment loop to a decrement loop
to see if the optimization can be performed.
Value is nonzero if optimization was performed. */
static int
check_dbra_loop (loop_end, iv_list, insn_count, loop_start)
rtx loop_end;
struct iv_class *iv_list;
int insn_count;
rtx loop_start;
{
struct iv_class *bl;
rtx reg;
rtx jump_label;
rtx final_value;
rtx start_value;
enum rtx_code branch_code;
rtx new_add_val;
rtx tested_before_loop = 0;
rtx p;
/* See if the loop is contained in `if (X >= 0)' for some reg X.
If so, then we know X is initially nonnegative even though
we don't know its initial value.
Record X in TESTED_BEFORE_LOOP. */
for (p = loop_start; p != 0; p = PREV_INSN (p))
if (GET_CODE (p) != NOTE)
break;
/* See if a conditional branch preceeds the loop.
There may be no other insns or labels between it and
the beginning of the loop. */
if (p != 0 && GET_CODE (p) == JUMP_INSN && condjump_p (p)
&& SET_SRC (PATTERN (p)) != pc_rtx
&& ((GET_CODE (XEXP (SET_SRC (PATTERN (p)), 0)) == LT
&& XEXP (SET_SRC (PATTERN (p)), 2) == pc_rtx)
||
(GET_CODE (XEXP (SET_SRC (PATTERN (p)), 0)) == GE
&& XEXP (SET_SRC (PATTERN (p)), 1) == pc_rtx))
&& next_real_insn (JUMP_LABEL (p)) == next_real_insn (loop_end))
{
/* Before the branch should be a test or compare.
See if we are comparing something against zero. */
p = PREV_INSN (p);
if (GET_CODE (p) == INSN && GET_CODE (PATTERN (p)) == SET
&& SET_DEST (PATTERN (p)) == cc0_rtx)
{
if (GET_CODE (SET_SRC (PATTERN (p))) == REG)
tested_before_loop = SET_SRC (PATTERN (p));
else if (GET_CODE (SET_SRC (PATTERN (p))) == COMPARE
&& GET_CODE (XEXP (SET_SRC (PATTERN (p)), 0)) == REG
&& XEXP (SET_SRC (PATTERN (p)), 1) == const0_rtx)
tested_before_loop = XEXP (SET_SRC (PATTERN (p)), 0);
else if (GET_CODE (SET_SRC (PATTERN (p))) == COMPARE
&& GET_CODE (XEXP (SET_SRC (PATTERN (p)), 1)) == REG
&& XEXP (SET_SRC (PATTERN (p)), 0) == const0_rtx)
tested_before_loop = XEXP (SET_SRC (PATTERN (p)), 1);
}
}
/* If last insn is a conditional branch, and the insn before tests a register
value, then try to optimize it. */
if (GET_CODE (PREV_INSN (loop_end)) == JUMP_INSN
&& GET_CODE (PATTERN (PREV_INSN (loop_end))) == SET
&& GET_CODE (SET_SRC (PATTERN (PREV_INSN (loop_end)))) == IF_THEN_ELSE
&& GET_CODE (PREV_INSN (PREV_INSN (loop_end))) == INSN
&& GET_CODE (PATTERN (PREV_INSN (PREV_INSN (loop_end)))) == SET
&& (GET_CODE (SET_DEST (PATTERN (PREV_INSN (PREV_INSN (loop_end))))) ==
CC0))
{
/* Check all of the bivs to see if the compare uses one of them. */
for (bl = iv_list; bl; bl = bl->next)
{
if (reg_mentioned_p (SET_DEST (PATTERN (bl->biv->insn)),
PREV_INSN (PREV_INSN (loop_end))))
break;
}
/* If biv set more than once, then give up.
We can't guarantee that it will be zero on the last iteration.
Also give up if the biv is used between its update and the test
insn. */
if (bl && bl->biv_count == 1
&& ! reg_used_between_p (regno_reg_rtx[bl->regno], bl->biv->insn,
PREV_INSN (PREV_INSN (loop_end))))
{
/* Look for the case where the basic induction variable is always
nonnegative, and equals zero on the last iteration.
In this case, add a reg_note REG_NONNEG, which allows the
m68k DBRA instruction to be used. */
/* the decrement case */
if (GET_CODE (bl->biv->add_val) == CONST_INT
&& INTVAL (bl->biv->add_val) < 0)
{
/* Initial value must be greater than 0,
init_val % -dec_value == 0 to ensure that it equals zero on
the last iteration */
if (GET_CODE (bl->initial_value) == CONST_INT
&& INTVAL (bl->initial_value) > 0
&& (INTVAL (bl->initial_value) %
(-INTVAL (bl->biv->add_val))) == 0)
{
/* register always nonnegative, add REG_NOTE to branch */
REG_NOTES (PREV_INSN (loop_end))
= gen_rtx (EXPR_LIST, REG_NONNEG, 0,
REG_NOTES (PREV_INSN (loop_end)));
bl->nonneg = 1;
return 1;
}
/* If the decrement is 1 and the value was tested as >= 0 before
the loop, then we can safely optimize. */
if (SET_DEST (PATTERN (bl->biv->insn)) == tested_before_loop
&& INTVAL (bl->biv->add_val) == -1)
{
REG_NOTES (PREV_INSN (loop_end))
= gen_rtx (EXPR_LIST, REG_NONNEG, 0,
REG_NOTES (PREV_INSN (loop_end)));
bl->nonneg = 1;
return 1;
}
}
else if (num_mem_sets <= 1)
{
/* Try to change inc to dec, so can apply above optimization. */
/* Can do this if:
all registers modified are induction variables or invariant,
all memory references have non-overlapping addresses
(obviously true if only one write)
allow 2 insns for the compare/jump at the end of the loop. */
int num_nonfixed_reads = 0;
rtx p;
/* 1 if the iteration var is used only to count iterations. */
int no_use_except_counting = 0;
for (p = loop_start; p != loop_end; p = NEXT_INSN (p))
if (GET_CODE (p) == INSN || GET_CODE (p) == CALL_INSN
|| GET_CODE (p) == JUMP_INSN)
num_nonfixed_reads += count_nonfixed_reads (PATTERN (p));
if (bl->giv_count == 0)
{
rtx bivreg = regno_reg_rtx[bl->regno];
/* If there are no givs for this biv,
see if perhaps there are no uses except to count. */
no_use_except_counting = 1;
for (p = loop_start; p != loop_end; p = NEXT_INSN (p))
if (GET_CODE (p) == INSN || GET_CODE (p) == CALL_INSN
|| GET_CODE (p) == JUMP_INSN)
{
if (GET_CODE (PATTERN (p)) == SET
&& GET_CODE (SET_DEST (PATTERN (p))) == REG
&& REGNO (SET_DEST (PATTERN (p))) == bl->regno)
/* An insn that sets the biv is okay. */
;
else if (p == PREV_INSN (PREV_INSN (loop_end))
|| p == PREV_INSN (loop_end))
/* Don't bother about the end test. */
;
else if (reg_mentioned_p (bivreg, PATTERN (p)))
/* Any other use of the biv is no good. */
{
no_use_except_counting = 0;
break;
}
}
}
/* This code only acts for innermost loops. Also it simplifies
the memory address check by only reversing loops with
zero or one memory access.
Two memory accesses could involve parts of the same array,
and that can't be reversed. */
if (num_nonfixed_reads <= 1
&& !loop_has_call
&& (no_use_except_counting
|| (bl->giv_count + bl->biv_count + num_mem_sets
+ num_movables + 2 == insn_count)))
{
rtx src_two_before_end;
int constant;
int win;
/* Loop can be reversed. */
if (loop_dump_stream)
fprintf (loop_dump_stream, "Can reverse loop\n");
/* Now check other conditions:
initial_value must be zero,
final_value % add_val == 0, so that when reversed, the
biv will be zero on the last iteration. */
/* Calculating the final value non trivial.
If branch is (LT (CC0) (CONST 0),
then value in compare is final value.
If branch is (LE (CC0) (CONST 0),
then value in compare is final_value - add_val */
branch_code
= GET_CODE (XEXP (SET_SRC (PATTERN (PREV_INSN (loop_end))), 0));
src_two_before_end
= SET_SRC (PATTERN (PREV_INSN (PREV_INSN (loop_end))));
win = 1;
if (GET_CODE (src_two_before_end) == REG)
constant = 0;
else if (GET_CODE (src_two_before_end) == COMPARE
&& GET_CODE (XEXP (src_two_before_end, 1)) == CONST_INT)
constant = INTVAL (XEXP (src_two_before_end, 1));
else
win = 0;
if (win && bl->initial_value == const0_rtx
&& (branch_code == LT || branch_code == LE)
&& XEXP (XEXP (SET_SRC (PATTERN (PREV_INSN (loop_end))), 0), 1) == const0_rtx
&& (constant % INTVAL (bl->biv->add_val)) == 0)
{
/* Register will always be nonnegative, with value
0 on last iteration if loop reversed */
/* Save some info needed to produce the new insns. */
reg = SET_DEST (PATTERN (bl->biv->insn));
jump_label = XEXP (SET_SRC (PATTERN (PREV_INSN (loop_end))), 1);
new_add_val = gen_rtx (CONST_INT, VOIDmode,
- INTVAL (bl->biv->add_val));
if (branch_code == LT)
{
final_value
= gen_rtx (CONST_INT, VOIDmode, constant);
start_value
= gen_rtx (CONST_INT, VOIDmode,
(constant - INTVAL (bl->biv->add_val)));
}
else /* branch_code == LE */
{
start_value
= gen_rtx (CONST_INT, VOIDmode, constant);
final_value
= gen_rtx (CONST_INT, VOIDmode,
(constant + INTVAL (bl->biv->add_val)));
}
/* Initialize biv to start_value before loop start.
The old initializing insn will be deleted as a
dead store by flow.c. */
emit_insn_before (gen_rtx (SET, VOIDmode, reg,
start_value),
loop_start);
/* Add insn to decrement register, and delete insn
that incremented the register. */
emit_insn_before (gen_rtx (SET, VOIDmode, reg,
gen_rtx (PLUS, GET_MODE (reg), reg,
new_add_val)),
bl->biv->insn);
/* Update biv info to reflect its new status. */
bl->biv->insn = PREV_INSN (bl->biv->insn);
delete_insn (NEXT_INSN (bl->biv->insn));
/* Inc LABEL_NUSES so that delete_insn will
not delete the label. */
LABEL_NUSES (XEXP (jump_label, 0)) ++;
if (regno_last_uid[bl->regno] != INSN_UID (PREV_INSN (loop_end)))
emit_insn_after (gen_rtx (SET, VOIDmode, reg,
final_value),
loop_end);
/* Delete compare/branch at end of loop. */
delete_insn (PREV_INSN (loop_end));
delete_insn (PREV_INSN (loop_end));
/* Add new compare/branch insn at end of loop. */
emit_insn_before (gen_rtx (SET, VOIDmode, cc0_rtx, reg),
loop_end);
emit_jump_insn_before (gen_rtx (SET, VOIDmode, pc_rtx,
gen_rtx (IF_THEN_ELSE, VOIDmode,
gen_rtx (GE, VOIDmode, cc0_rtx,
const0_rtx),
jump_label,
pc_rtx)),
loop_end);
JUMP_LABEL (PREV_INSN (loop_end)) = XEXP (jump_label, 0);
/* Increment of LABEL_NUSES done above. */
/* Register is now always nonnegative,
so add REG_NONNEG note to the branch. */
REG_NOTES (PREV_INSN (loop_end))
= gen_rtx (EXPR_LIST, REG_NONNEG, 0,
REG_NOTES (PREV_INSN (loop_end)));
bl->nonneg = 1;
/* Update rest of biv info. */
bl->initial_value = start_value;
bl->biv->add_val = new_add_val;
if (loop_dump_stream)
fprintf (loop_dump_stream, "Reversed loop and added reg_nonneg\n");
return 1;
}
}
}
}
}
return 0;
}
/* Verify whether the biv BL appears to be eliminable,
based on the insns in the loop that refer to it.
LOOP_START is the first insn of the loop, and END is the end insn. */
static void
check_eliminate_biv (bl, loop_start, end)
struct iv_class *bl;
rtx loop_start;
rtx end;
{
/* Get the REG rtx for the biv. */
rtx reg = SET_DEST (PATTERN (bl->biv->insn));
rtx p;
struct induction *v;
bl->eliminable = 0;
for (p = loop_start; p != end; p = NEXT_INSN (p))
{
enum rtx_code code = GET_CODE (p);
if ((code == INSN || code == JUMP_INSN || code == CALL_INSN)
&& reg_mentioned_p (reg, PATTERN (p)))
{
/* This insn uses the biv. If we can't understand it,
then we can't eliminate the biv. */
if (GET_CODE (PATTERN (p)) != SET)
{
if (loop_dump_stream)
fprintf (loop_dump_stream,
"Cannot eliminate biv %d: cannot understand insn %d.\n",
bl->regno, INSN_UID (p));
break;
}
/* The insns that increment the biv are no problem. */
if (SET_DEST (PATTERN (p)) == reg)
continue;
/* If this is an insn which uses the biv ONLY in the
calculation of a giv which is in the family of this
biv, it's ok becuase it will go away when the giv is
reduced. March 14, 1989 -- self@bayes.arc.nasa.gov */
for (v = bl->giv; v; v = v->family)
if (v->insn == p)
{
if (v->giv_type == DEST_REG
|| (v->giv_type == DEST_ADDR
&& ! other_reg_use_p (reg, *(v->location),
PATTERN (p))))
break;
}
if (v)
continue;
/* If can rewrite this insn not to use the biv, it's ok. */
if (can_eliminate_biv_p (p, bl))
continue;
/* Biv is used in a way we cannot eliminate. */
if (loop_dump_stream)
fprintf (loop_dump_stream,
"Cannot eliminate biv %d: biv used in insn %d.\n",
bl->regno, INSN_UID (p));
break;
}
}
if (p == end)
{
bl->eliminable = 1;
if (loop_dump_stream)
fprintf (loop_dump_stream, "Can eliminate biv %d.\n",
bl->regno);
}
}
/* Return 1 if INSN, a compare insn which tests the biv described by BL,
can be rewritten to use instead some reduced giv related to that biv.
Does not change the rtl.
We make the assumption that all the givs depending on this biv
will be reduced, since only in that case will an attempt to eliminate
the biv actually be made.
The following function is very parallel to this one. */
static int
can_eliminate_biv_p (insn, bl)
rtx insn;
struct iv_class *bl;
{
rtx src;
enum rtx_code code;
struct induction *v, *tv;
rtx arg;
int arg_operand;
/* Mode of this biv. */
enum machine_mode mode = bl->biv->mode;
if (SET_DEST (PATTERN (insn)) != cc0_rtx)
return 0;
src = SET_SRC (PATTERN (insn));
code = GET_CODE (src);
switch (code)
{
/* a test insn */
case REG:
/* Can replace with any giv that has (MULT_VAL != 0) and (ADD_VAL == 0)
Require a constant integer for MULT_VAL, so we know it's nonzero. */
for (v = bl->giv; v; v = v->family)
if (GET_CODE (v->mult_val) == CONST_INT && v->mult_val != const0_rtx
&& v->add_val == const0_rtx
&& ! v->ignore
&& v->mode == mode)
return 1;
/* Look for a giv with (MULT_VAL != 0) and (ADD_VAL != 0)
where ADD_VAL is a constant or a register;
can replace test insn with a compare insn (cmp REDUCED_GIV ADD_VAL).
Require a constant integer for MULT_VAL, so we know it's nonzero. */
if (next_insn_tests_no_inequality (insn))
for (v = bl->giv; v; v = v->family)
if (GET_CODE (v->mult_val) == CONST_INT && v->mult_val != const0_rtx
&& (GET_CODE (v->add_val) == REG || GET_CODE (v->add_val) == CONST_INT)
&& ! v->ignore
&& v->mode == mode)
return 1;
if (loop_dump_stream)
fprintf (loop_dump_stream, "Cannot eliminate biv %d in test insn %d: no appropriate giv.\n",
bl->regno, INSN_UID (insn));
return 0;
/* a compare insn */
case COMPARE:
/* Figure out which operand is the biv. */
if ((GET_CODE (XEXP (src, 0)) == REG)
&& (REGNO (XEXP (src, 0)) == bl->regno))
{
arg = XEXP (src, 1);
arg_operand = 1;
}
else if ((GET_CODE (XEXP (src, 1)) == REG)
&& (REGNO (XEXP (src, 1)) == bl->regno))
{
arg = XEXP (src, 0);
arg_operand = 0;
}
else
return 0;
if (GET_CODE (arg) == CONST_INT)
{
/* Can replace with any giv that has constant coefficients. */
for (v = bl->giv; v; v = v->family)
if (GET_CODE (v->mult_val) == CONST_INT
&& GET_CODE (v->add_val) == CONST_INT
&& ! v->ignore
&& v->mode == mode)
{
/* Make sure there's no overflow in the range for the giv. */
if (INTVAL (v->mult_val) == 0
||
(INTVAL (arg) * INTVAL (v->mult_val) / INTVAL (v->mult_val)
!= INTVAL (arg))
||
((0 < (INTVAL (arg) * INTVAL (v->mult_val)
+ INTVAL (v->add_val)))
!= (0 < INTVAL (arg))))
return 0;
return 1;
}
if (next_insn_tests_no_inequality (insn))
{
/* Look for giv with constant mult_val and nonconst add_val,
since we can insert add insn before loop
to calculate new compare value. */
/* This is not safe if the end-test is not == or !=,
since we can't tell whether the giv will overflow. */
for (v = bl->giv; v; v = v->family)
if (GET_CODE (v->mult_val) == CONST_INT
&& ! v->ignore
&& v->mode == mode)
return 1;
}
}
else if ((GET_CODE (arg) == REG || GET_CODE (arg) == MEM)
&& next_insn_tests_no_inequality (insn))
{
/* Comparing against invariant register or memref can be handled. */
if (invariant_p (arg))
{
/* Look for giv with constant mult_val and nonconst add_val.
Insert add-insn before loop to compute new compare value. */
for (v = bl->giv; v; v = v->family)
if ((GET_CODE (v->mult_val) == CONST_INT)
&& ! v->ignore
&& v->mode == mode)
return 1;
}
/* Otherwise, only comparing against a biv can be handled. */
if (GET_CODE (arg) != REG
|| induct_var[REGNO (arg)] != BASIC_INDUCT)
return 0;
/* Look for a giv for each biv that have identical
values for mult_val and add_val. */
for (v = bl->giv; v; v = v->family)
if (v->mode == mode
&& ! v->ignore)
{
for (tv = class_struct[REGNO (arg)]->giv; tv; tv = tv->family)
if ((tv->new_reg != 0)
&& rtx_equal_p (tv->mult_val, v->mult_val)
&& rtx_equal_p (tv->mult_val, v->mult_val)
&& ! tv->ignore
&& tv->mode == mode)
return 1;
}
}
return 0;
default:
return 0;
}
}
/* Rewrite a compare insn INSN which uses the biv described by BL
so that it doesn't use that biv any more.
Instead it will use some reduced giv related to that biv.
The preceding function is very parallel to this one. */
static void
eliminate_biv (insn, bl, loop_start)
rtx insn;
struct iv_class *bl;
rtx loop_start;
{
rtx src = SET_SRC (PATTERN (insn));
enum rtx_code code = GET_CODE (src);
struct induction *v, *tv;
rtx arg, temp;
int arg_operand;
/* Mode of this biv. */
enum machine_mode mode = bl->biv->mode;
switch (code)
{
/* a test insn */
case REG:
/* Can replace with any giv that was reduced and
that has (MULT_VAL != 0) and (ADD_VAL == 0).
Require a constant integer for MULT_VAL, so we know it's nonzero. */
for (v = bl->giv; v; v = v->family)
if (GET_CODE (v->mult_val) == CONST_INT && v->mult_val != const0_rtx
&& v->add_val == const0_rtx
&& v->new_reg != 0
&& v->mode == mode)
break;
if (v)
{
/* We can test the sign of that giv's reduced reg. */
SET_SRC (PATTERN (insn)) = v->new_reg;
/* If the giv has the opposite direction of change,
then reverse the comparison. */
if (INTVAL (v->mult_val) < 0)
SET_SRC (PATTERN (insn))
= gen_rtx (COMPARE, GET_MODE (v->new_reg),
const0_rtx, v->new_reg);
return;
}
/* Look for a giv with (MULT_VAL != 0) and (ADD_VAL != 0)
where ADD_VAL is a constant or a register;
replace test insn with a compare insn (cmp REDUCED_GIV ADD_VAL).
Require a constant integer for MULT_VAL, so we know it's nonzero. */
for (v = bl->giv; v; v = v->family)
if (GET_CODE (v->mult_val) == CONST_INT && v->mult_val != const0_rtx
&& (GET_CODE (v->add_val) == REG || GET_CODE (v->add_val) == CONST_INT)
&& v->new_reg != 0
&& v->mode == mode)
break;
if (v)
{
/* Replace biv with the giv's reduced register. */
SET_SRC (PATTERN (insn)) = gen_rtx (COMPARE, GET_MODE (v->new_reg),
v->new_reg,
copy_rtx (v->add_val));
/* If the giv has the opposite direction of change,
then reverse the comparison. */
if (INTVAL (v->mult_val) < 0)
{
XEXP (SET_SRC (PATTERN (insn)), 0)
= XEXP (SET_SRC (PATTERN (insn)), 1);
XEXP (SET_SRC (PATTERN (insn)), 1) = v->new_reg;
}
#if 0
/* add_val must be invariant, so don't bother storing in a register */
/* calculate the appropriate constant to compare against */
emit_insn_before (gen_rtx (SET, VOIDmode, compare_value,
copy_rtx (v->add_val)),
loop_start);
#endif
return;
}
abort ();
break;
/* a compare insn */
case COMPARE:
/* Figure out which operand is the biv. */
if (GET_CODE (XEXP (src, 0)) == REG
&& REGNO (XEXP (src, 0)) == bl->regno)
{
arg = XEXP (src, 1);
arg_operand = 1;
}
else if (GET_CODE (XEXP (src, 1)) == REG
&& REGNO (XEXP (src, 1)) == bl->regno)
{
arg = XEXP (src, 0);
arg_operand = 0;
}
else
abort ();
if (GET_CODE (arg) == CONST_INT)
{
/* Can replace with any giv that has constant mult_val and add_val.
Make sure it was strength reduced by checking new_reg != 0. */
for (v = bl->giv; v; v = v->family)
if (GET_CODE (v->mult_val) == CONST_INT
&& GET_CODE (v->add_val) == CONST_INT
&& v->new_reg
&& v->mode == mode)
{
/* Make sure there's no overflow in the range for the giv. */
if (! (INTVAL (v->mult_val) == 0
||
(INTVAL (arg) * INTVAL (v->mult_val) / INTVAL (v->mult_val)
!= INTVAL (arg))
||
((0 < (INTVAL (arg) * INTVAL (v->mult_val)
+ INTVAL (v->add_val)))
!= (0 < INTVAL (arg)))))
break;
}
if (v)
{
rtx newval;
/* Replace biv with the giv's reduced reg. */
XEXP (src, 1-arg_operand) = v->new_reg;
/* Calculate the appropriate constant to compare against. */
newval = gen_rtx (CONST_INT, VOIDmode,
(INTVAL (arg) * INTVAL (v->mult_val)
+ INTVAL (v->add_val)));
XEXP (src, arg_operand) = newval;
/* If that constant is no good in a compare,
put it in a register. */
if (recog (PATTERN (insn), insn) < 0)
{
temp = gen_reg_rtx (mode);
emit_iv_init_code (arg, v->mult_val, v->add_val,
temp, loop_start);
XEXP (src, arg_operand) = temp;
}
/* If the giv has the opposite direction of change,
then reverse the comparison. */
if (INTVAL (v->mult_val) < 0)
{
temp = XEXP (src, 0);
XEXP (src, 0) = XEXP (src, 1);
XEXP (src, 1) = temp;
}
return;
}
/* Look for giv with constant mult_val and nonconst add_val.
Insert add insn before loop to calculate new compare value. */
for (v = bl->giv; v; v = v->family)
if (GET_CODE (v->mult_val) == CONST_INT
&& v->new_reg
&& v->mode == mode)
break;
if (v)
{
rtx compare_value = gen_reg_rtx (mode);
/* Replace biv with giv's reduced register. */
XEXP (src, 1-arg_operand) = v->new_reg;
/* At start of loop, compute value to compare against. */
emit_iv_init_code (arg, v->mult_val, v->add_val,
compare_value, loop_start);
/* Use it in this insn. */
XEXP (src, arg_operand) = compare_value;
/* If the giv has the opposite direction of change,
then reverse the comparison. */
if (INTVAL (v->mult_val) < 0)
{
temp = XEXP (src, 0);
XEXP (src, 0) = XEXP (src, 1);
XEXP (src, 1) = temp;
}
return;
}
abort ();
}
else if (GET_CODE (arg) == REG || GET_CODE (arg) == MEM)
{
if (invariant_p (arg))
{
/* Look for giv with constant mult_val and nonconst add_val.
Insert add-insn before loop to compute new compare value. */
for (v = bl->giv; v; v = v->family)
if (GET_CODE (v->mult_val) == CONST_INT
&& v->new_reg
&& v->mode == mode)
break;
if (v)
{
rtx compare_value = gen_reg_rtx (mode);
/* Replace biv with giv's reduced register. */
XEXP (src, 1-arg_operand) = v->new_reg;
/* At start of loop, compute value to compare against. */
emit_iv_init_code (arg, v->mult_val, v->add_val,
compare_value, loop_start);
XEXP (src, arg_operand) = compare_value;
/* If the giv has the opposite direction of change,
then reverse the comparison. */
if (INTVAL (v->mult_val) < 0)
{
temp = XEXP (src, 0);
XEXP (src, 0) = XEXP (src, 1);
XEXP (src, 1) = temp;
}
return;
}
}
/* Otherwise the reg compared with had better be a biv. */
if (GET_CODE (arg) != REG
|| induct_var[REGNO (arg)] != BASIC_INDUCT)
abort ();
/* Look for a pair of givs, one for each biv,
with identical coefficients. */
for (v = bl->giv; v; v = v->family)
{
if (!v->new_reg && v->mode == mode)
continue;
for (tv = class_struct[REGNO (arg)]->giv; tv; tv = tv->family)
if ((tv->new_reg != 0)
&& rtx_equal_p (tv->mult_val, v->mult_val)
&& rtx_equal_p (tv->add_val, v->add_val)
&& tv->mode == mode)
break;
if (tv)
break;
}
if (v)
{
/* Replace biv with its giv's reduced reg. */
XEXP (src, 1-arg_operand) = v->new_reg;
/* Replace other operand with the other giv's reduced reg. */
XEXP (src, arg_operand) = tv->new_reg;
/* If the giv has the opposite direction of change,
then reverse the comparison. */
if (INTVAL (v->mult_val) < 0)
{
temp = XEXP (src, 0);
XEXP (src, 0) = XEXP (src, 1);
XEXP (src, 1) = temp;
}
return;
}
}
abort ();
default:
abort ();
}
}
/* Try to calculate the final value of the biv,
the value it will have at the end of the loop.
If we can do it, return that value. */
/* ??? One case that should be simple to handle
is when the biv is incremented by an invariant
exactly once each time around the loop,
and when the number of iterations can be determined
(as the value of some invariant).
Then the final value would be BIV + (INCREMENT * NUM_ITERATIONS).
Once that case is handled, it would become desirable to detect empty
loops and delete them, since this optimization could make empty loops. */
static rtx
final_biv_value (bl, loop_end)
struct iv_class *bl;
rtx loop_end;
{
/* wimpy, but guaranteed to work */
return 0;
}
/* Return nonzero if the last use of reg REGNO
is in an insn following INSN in the same basic block. */
static int
last_use_this_basic_block (regno, insn)
int regno;
rtx insn;
{
rtx n;
for (n = insn;
n && GET_CODE (n) != CODE_LABEL && GET_CODE (n) != JUMP_INSN;
n = NEXT_INSN (n))
{
if (regno_last_uid[regno] == INSN_UID (n))
return 1;
}
return 0;
}
|