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
|
/* Generate pattern matching and transform code shared between
GENERIC and GIMPLE folding code from match-and-simplify description.
Copyright (C) 2014-2021 Free Software Foundation, Inc.
Contributed by Richard Biener <rguenther@suse.de>
and Prathamesh Kulkarni <bilbotheelffriend@gmail.com>
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. */
#include "bconfig.h"
#include "system.h"
#include "coretypes.h"
#include <cpplib.h>
#include "errors.h"
#include "hash-table.h"
#include "hash-set.h"
#include "is-a.h"
/* Stubs for GGC referenced through instantiations triggered by hash-map. */
void *ggc_internal_cleared_alloc (size_t, void (*)(void *),
size_t, size_t MEM_STAT_DECL)
{
return NULL;
}
void ggc_free (void *)
{
}
/* Global state. */
/* Verboseness. 0 is quiet, 1 adds some warnings, 2 is for debugging. */
unsigned verbose;
/* libccp helpers. */
static class line_maps *line_table;
/* The rich_location class within libcpp requires a way to expand
location_t instances, and relies on the client code
providing a symbol named
linemap_client_expand_location_to_spelling_point
to do this.
This is the implementation for genmatch. */
expanded_location
linemap_client_expand_location_to_spelling_point (location_t loc,
enum location_aspect)
{
const struct line_map_ordinary *map;
loc = linemap_resolve_location (line_table, loc, LRK_SPELLING_LOCATION, &map);
return linemap_expand_location (line_table, map, loc);
}
static bool
#if GCC_VERSION >= 4001
__attribute__((format (printf, 5, 0)))
#endif
diagnostic_cb (cpp_reader *, enum cpp_diagnostic_level errtype,
enum cpp_warning_reason, rich_location *richloc,
const char *msg, va_list *ap)
{
const line_map_ordinary *map;
location_t location = richloc->get_loc ();
linemap_resolve_location (line_table, location, LRK_SPELLING_LOCATION, &map);
expanded_location loc = linemap_expand_location (line_table, map, location);
fprintf (stderr, "%s:%d:%d %s: ", loc.file, loc.line, loc.column,
(errtype == CPP_DL_WARNING) ? "warning" : "error");
vfprintf (stderr, msg, *ap);
fprintf (stderr, "\n");
FILE *f = fopen (loc.file, "r");
if (f)
{
char buf[128];
while (loc.line > 0)
{
if (!fgets (buf, 128, f))
goto notfound;
if (buf[strlen (buf) - 1] != '\n')
{
if (loc.line > 1)
loc.line++;
}
loc.line--;
}
fprintf (stderr, "%s", buf);
for (int i = 0; i < loc.column - 1; ++i)
fputc (' ', stderr);
fputc ('^', stderr);
fputc ('\n', stderr);
notfound:
fclose (f);
}
if (errtype == CPP_DL_FATAL)
exit (1);
return false;
}
static void
#if GCC_VERSION >= 4001
__attribute__((format (printf, 2, 3)))
#endif
fatal_at (const cpp_token *tk, const char *msg, ...)
{
rich_location richloc (line_table, tk->src_loc);
va_list ap;
va_start (ap, msg);
diagnostic_cb (NULL, CPP_DL_FATAL, CPP_W_NONE, &richloc, msg, &ap);
va_end (ap);
}
static void
#if GCC_VERSION >= 4001
__attribute__((format (printf, 2, 3)))
#endif
fatal_at (location_t loc, const char *msg, ...)
{
rich_location richloc (line_table, loc);
va_list ap;
va_start (ap, msg);
diagnostic_cb (NULL, CPP_DL_FATAL, CPP_W_NONE, &richloc, msg, &ap);
va_end (ap);
}
static void
#if GCC_VERSION >= 4001
__attribute__((format (printf, 2, 3)))
#endif
warning_at (const cpp_token *tk, const char *msg, ...)
{
rich_location richloc (line_table, tk->src_loc);
va_list ap;
va_start (ap, msg);
diagnostic_cb (NULL, CPP_DL_WARNING, CPP_W_NONE, &richloc, msg, &ap);
va_end (ap);
}
static void
#if GCC_VERSION >= 4001
__attribute__((format (printf, 2, 3)))
#endif
warning_at (location_t loc, const char *msg, ...)
{
rich_location richloc (line_table, loc);
va_list ap;
va_start (ap, msg);
diagnostic_cb (NULL, CPP_DL_WARNING, CPP_W_NONE, &richloc, msg, &ap);
va_end (ap);
}
/* Like fprintf, but print INDENT spaces at the beginning. */
static void
#if GCC_VERSION >= 4001
__attribute__((format (printf, 3, 4)))
#endif
fprintf_indent (FILE *f, unsigned int indent, const char *format, ...)
{
va_list ap;
for (; indent >= 8; indent -= 8)
fputc ('\t', f);
fprintf (f, "%*s", indent, "");
va_start (ap, format);
vfprintf (f, format, ap);
va_end (ap);
}
static void
output_line_directive (FILE *f, location_t location,
bool dumpfile = false, bool fnargs = false)
{
const line_map_ordinary *map;
linemap_resolve_location (line_table, location, LRK_SPELLING_LOCATION, &map);
expanded_location loc = linemap_expand_location (line_table, map, location);
if (dumpfile)
{
/* When writing to a dumpfile only dump the filename. */
const char *file = strrchr (loc.file, DIR_SEPARATOR);
#if defined(DIR_SEPARATOR_2)
const char *pos2 = strrchr (loc.file, DIR_SEPARATOR_2);
if (pos2 && (!file || (pos2 > file)))
file = pos2;
#endif
if (!file)
file = loc.file;
else
++file;
if (fnargs)
fprintf (f, "\"%s\", %d", file, loc.line);
else
fprintf (f, "%s:%d", file, loc.line);
}
else
/* Other gen programs really output line directives here, at least for
development it's right now more convenient to have line information
from the generated file. Still keep the directives as comment for now
to easily back-point to the meta-description. */
fprintf (f, "/* #line %d \"%s\" */\n", loc.line, loc.file);
}
/* Pull in tree codes and builtin function codes from their
definition files. */
#define DEFTREECODE(SYM, STRING, TYPE, NARGS) SYM,
enum tree_code {
#include "tree.def"
MAX_TREE_CODES
};
#undef DEFTREECODE
#define DEF_BUILTIN(ENUM, N, C, T, LT, B, F, NA, AT, IM, COND) ENUM,
enum built_in_function {
#include "builtins.def"
END_BUILTINS
};
#define DEF_INTERNAL_FN(CODE, FLAGS, FNSPEC) IFN_##CODE,
enum internal_fn {
#include "internal-fn.def"
IFN_LAST
};
enum combined_fn {
#define DEF_BUILTIN(ENUM, N, C, T, LT, B, F, NA, AT, IM, COND) \
CFN_##ENUM = int (ENUM),
#include "builtins.def"
#define DEF_INTERNAL_FN(CODE, FLAGS, FNSPEC) \
CFN_##CODE = int (END_BUILTINS) + int (IFN_##CODE),
#include "internal-fn.def"
CFN_LAST
};
#include "case-cfn-macros.h"
/* Return true if CODE represents a commutative tree code. Otherwise
return false. */
bool
commutative_tree_code (enum tree_code code)
{
switch (code)
{
case PLUS_EXPR:
case MULT_EXPR:
case MULT_HIGHPART_EXPR:
case MIN_EXPR:
case MAX_EXPR:
case BIT_IOR_EXPR:
case BIT_XOR_EXPR:
case BIT_AND_EXPR:
case NE_EXPR:
case EQ_EXPR:
case UNORDERED_EXPR:
case ORDERED_EXPR:
case UNEQ_EXPR:
case LTGT_EXPR:
case TRUTH_AND_EXPR:
case TRUTH_XOR_EXPR:
case TRUTH_OR_EXPR:
case WIDEN_MULT_EXPR:
case VEC_WIDEN_MULT_HI_EXPR:
case VEC_WIDEN_MULT_LO_EXPR:
case VEC_WIDEN_MULT_EVEN_EXPR:
case VEC_WIDEN_MULT_ODD_EXPR:
return true;
default:
break;
}
return false;
}
/* Return true if CODE represents a ternary tree code for which the
first two operands are commutative. Otherwise return false. */
bool
commutative_ternary_tree_code (enum tree_code code)
{
switch (code)
{
case WIDEN_MULT_PLUS_EXPR:
case WIDEN_MULT_MINUS_EXPR:
case DOT_PROD_EXPR:
return true;
default:
break;
}
return false;
}
/* Return true if CODE is a comparison. */
bool
comparison_code_p (enum tree_code code)
{
switch (code)
{
case EQ_EXPR:
case NE_EXPR:
case ORDERED_EXPR:
case UNORDERED_EXPR:
case LTGT_EXPR:
case UNEQ_EXPR:
case GT_EXPR:
case GE_EXPR:
case LT_EXPR:
case LE_EXPR:
case UNGT_EXPR:
case UNGE_EXPR:
case UNLT_EXPR:
case UNLE_EXPR:
return true;
default:
break;
}
return false;
}
/* Base class for all identifiers the parser knows. */
class id_base : public nofree_ptr_hash<id_base>
{
public:
enum id_kind { CODE, FN, PREDICATE, USER, NULL_ID } kind;
id_base (id_kind, const char *, int = -1);
hashval_t hashval;
int nargs;
const char *id;
/* hash_table support. */
static inline hashval_t hash (const id_base *);
static inline int equal (const id_base *, const id_base *);
};
inline hashval_t
id_base::hash (const id_base *op)
{
return op->hashval;
}
inline int
id_base::equal (const id_base *op1,
const id_base *op2)
{
return (op1->hashval == op2->hashval
&& strcmp (op1->id, op2->id) == 0);
}
/* The special id "null", which matches nothing. */
static id_base *null_id;
/* Hashtable of known pattern operators. This is pre-seeded from
all known tree codes and all known builtin function ids. */
static hash_table<id_base> *operators;
id_base::id_base (id_kind kind_, const char *id_, int nargs_)
{
kind = kind_;
id = id_;
nargs = nargs_;
hashval = htab_hash_string (id);
}
/* Identifier that maps to a tree code. */
class operator_id : public id_base
{
public:
operator_id (enum tree_code code_, const char *id_, unsigned nargs_,
const char *tcc_)
: id_base (id_base::CODE, id_, nargs_), code (code_), tcc (tcc_) {}
enum tree_code code;
const char *tcc;
};
/* Identifier that maps to a builtin or internal function code. */
class fn_id : public id_base
{
public:
fn_id (enum built_in_function fn_, const char *id_)
: id_base (id_base::FN, id_), fn (fn_) {}
fn_id (enum internal_fn fn_, const char *id_)
: id_base (id_base::FN, id_), fn (int (END_BUILTINS) + int (fn_)) {}
unsigned int fn;
};
class simplify;
/* Identifier that maps to a user-defined predicate. */
class predicate_id : public id_base
{
public:
predicate_id (const char *id_)
: id_base (id_base::PREDICATE, id_), matchers (vNULL) {}
vec<simplify *> matchers;
};
/* Identifier that maps to a operator defined by a 'for' directive. */
class user_id : public id_base
{
public:
user_id (const char *id_, bool is_oper_list_ = false)
: id_base (id_base::USER, id_), substitutes (vNULL),
used (false), is_oper_list (is_oper_list_) {}
vec<id_base *> substitutes;
bool used;
bool is_oper_list;
};
template<>
template<>
inline bool
is_a_helper <fn_id *>::test (id_base *id)
{
return id->kind == id_base::FN;
}
template<>
template<>
inline bool
is_a_helper <operator_id *>::test (id_base *id)
{
return id->kind == id_base::CODE;
}
template<>
template<>
inline bool
is_a_helper <predicate_id *>::test (id_base *id)
{
return id->kind == id_base::PREDICATE;
}
template<>
template<>
inline bool
is_a_helper <user_id *>::test (id_base *id)
{
return id->kind == id_base::USER;
}
/* If ID has a pair of consecutive, commutative operands, return the
index of the first, otherwise return -1. */
static int
commutative_op (id_base *id)
{
if (operator_id *code = dyn_cast <operator_id *> (id))
{
if (commutative_tree_code (code->code)
|| commutative_ternary_tree_code (code->code))
return 0;
return -1;
}
if (fn_id *fn = dyn_cast <fn_id *> (id))
switch (fn->fn)
{
CASE_CFN_FMA:
case CFN_FMS:
case CFN_FNMA:
case CFN_FNMS:
return 0;
default:
return -1;
}
if (user_id *uid = dyn_cast<user_id *> (id))
{
int res = commutative_op (uid->substitutes[0]);
if (res < 0)
return 0;
for (unsigned i = 1; i < uid->substitutes.length (); ++i)
if (res != commutative_op (uid->substitutes[i]))
return -1;
return res;
}
return -1;
}
/* Add a predicate identifier to the hash. */
static predicate_id *
add_predicate (const char *id)
{
predicate_id *p = new predicate_id (id);
id_base **slot = operators->find_slot_with_hash (p, p->hashval, INSERT);
if (*slot)
fatal ("duplicate id definition");
*slot = p;
return p;
}
/* Add a tree code identifier to the hash. */
static void
add_operator (enum tree_code code, const char *id,
const char *tcc, unsigned nargs)
{
if (strcmp (tcc, "tcc_unary") != 0
&& strcmp (tcc, "tcc_binary") != 0
&& strcmp (tcc, "tcc_comparison") != 0
&& strcmp (tcc, "tcc_expression") != 0
/* For {REAL,IMAG}PART_EXPR and VIEW_CONVERT_EXPR. */
&& strcmp (tcc, "tcc_reference") != 0
/* To have INTEGER_CST and friends as "predicate operators". */
&& strcmp (tcc, "tcc_constant") != 0
/* And allow CONSTRUCTOR for vector initializers. */
&& !(code == CONSTRUCTOR)
/* Allow SSA_NAME as predicate operator. */
&& !(code == SSA_NAME))
return;
/* Treat ADDR_EXPR as atom, thus don't allow matching its operand. */
if (code == ADDR_EXPR)
nargs = 0;
operator_id *op = new operator_id (code, id, nargs, tcc);
id_base **slot = operators->find_slot_with_hash (op, op->hashval, INSERT);
if (*slot)
fatal ("duplicate id definition");
*slot = op;
}
/* Add a built-in or internal function identifier to the hash. ID is
the name of its CFN_* enumeration value. */
template <typename T>
static void
add_function (T code, const char *id)
{
fn_id *fn = new fn_id (code, id);
id_base **slot = operators->find_slot_with_hash (fn, fn->hashval, INSERT);
if (*slot)
fatal ("duplicate id definition");
*slot = fn;
}
/* Helper for easy comparing ID with tree code CODE. */
static bool
operator==(id_base &id, enum tree_code code)
{
if (operator_id *oid = dyn_cast <operator_id *> (&id))
return oid->code == code;
return false;
}
/* Lookup the identifier ID. Allow "null" if ALLOW_NULL. */
id_base *
get_operator (const char *id, bool allow_null = false)
{
if (allow_null && strcmp (id, "null") == 0)
return null_id;
id_base tem (id_base::CODE, id);
id_base *op = operators->find_with_hash (&tem, tem.hashval);
if (op)
{
/* If this is a user-defined identifier track whether it was used. */
if (user_id *uid = dyn_cast<user_id *> (op))
uid->used = true;
return op;
}
char *id2;
bool all_upper = true;
bool all_lower = true;
for (unsigned int i = 0; id[i]; ++i)
if (ISUPPER (id[i]))
all_lower = false;
else if (ISLOWER (id[i]))
all_upper = false;
if (all_lower)
{
/* Try in caps with _EXPR appended. */
id2 = ACONCAT ((id, "_EXPR", NULL));
for (unsigned int i = 0; id2[i]; ++i)
id2[i] = TOUPPER (id2[i]);
}
else if (all_upper && strncmp (id, "IFN_", 4) == 0)
/* Try CFN_ instead of IFN_. */
id2 = ACONCAT (("CFN_", id + 4, NULL));
else if (all_upper && strncmp (id, "BUILT_IN_", 9) == 0)
/* Try prepending CFN_. */
id2 = ACONCAT (("CFN_", id, NULL));
else
return NULL;
new (&tem) id_base (id_base::CODE, id2);
return operators->find_with_hash (&tem, tem.hashval);
}
/* Return the comparison operators that results if the operands are
swapped. This is safe for floating-point. */
id_base *
swap_tree_comparison (operator_id *p)
{
switch (p->code)
{
case EQ_EXPR:
case NE_EXPR:
case ORDERED_EXPR:
case UNORDERED_EXPR:
case LTGT_EXPR:
case UNEQ_EXPR:
return p;
case GT_EXPR:
return get_operator ("LT_EXPR");
case GE_EXPR:
return get_operator ("LE_EXPR");
case LT_EXPR:
return get_operator ("GT_EXPR");
case LE_EXPR:
return get_operator ("GE_EXPR");
case UNGT_EXPR:
return get_operator ("UNLT_EXPR");
case UNGE_EXPR:
return get_operator ("UNLE_EXPR");
case UNLT_EXPR:
return get_operator ("UNGT_EXPR");
case UNLE_EXPR:
return get_operator ("UNGE_EXPR");
default:
gcc_unreachable ();
}
}
typedef hash_map<nofree_string_hash, unsigned> cid_map_t;
/* The AST produced by parsing of the pattern definitions. */
class dt_operand;
class capture_info;
/* The base class for operands. */
class operand {
public:
enum op_type { OP_PREDICATE, OP_EXPR, OP_CAPTURE, OP_C_EXPR, OP_IF, OP_WITH };
operand (enum op_type type_, location_t loc_)
: type (type_), location (loc_) {}
enum op_type type;
location_t location;
virtual void gen_transform (FILE *, int, const char *, bool, int,
const char *, capture_info *,
dt_operand ** = 0,
int = 0)
{ gcc_unreachable (); }
};
/* A predicate operand. Predicates are leafs in the AST. */
class predicate : public operand
{
public:
predicate (predicate_id *p_, location_t loc)
: operand (OP_PREDICATE, loc), p (p_) {}
predicate_id *p;
};
/* An operand that constitutes an expression. Expressions include
function calls and user-defined predicate invocations. */
class expr : public operand
{
public:
expr (id_base *operation_, location_t loc, bool is_commutative_ = false)
: operand (OP_EXPR, loc), operation (operation_),
ops (vNULL), expr_type (NULL), is_commutative (is_commutative_),
is_generic (false), force_single_use (false), force_leaf (false),
opt_grp (0) {}
expr (expr *e)
: operand (OP_EXPR, e->location), operation (e->operation),
ops (vNULL), expr_type (e->expr_type), is_commutative (e->is_commutative),
is_generic (e->is_generic), force_single_use (e->force_single_use),
force_leaf (e->force_leaf), opt_grp (e->opt_grp) {}
void append_op (operand *op) { ops.safe_push (op); }
/* The operator and its operands. */
id_base *operation;
vec<operand *> ops;
/* An explicitely specified type - used exclusively for conversions. */
const char *expr_type;
/* Whether the operation is to be applied commutatively. This is
later lowered to two separate patterns. */
bool is_commutative;
/* Whether the expression is expected to be in GENERIC form. */
bool is_generic;
/* Whether pushing any stmt to the sequence should be conditional
on this expression having a single-use. */
bool force_single_use;
/* Whether in the result expression this should be a leaf node
with any children simplified down to simple operands. */
bool force_leaf;
/* If non-zero, the group for optional handling. */
unsigned char opt_grp;
virtual void gen_transform (FILE *f, int, const char *, bool, int,
const char *, capture_info *,
dt_operand ** = 0, int = 0);
};
/* An operator that is represented by native C code. This is always
a leaf operand in the AST. This class is also used to represent
the code to be generated for 'if' and 'with' expressions. */
class c_expr : public operand
{
public:
/* A mapping of an identifier and its replacement. Used to apply
'for' lowering. */
class id_tab {
public:
const char *id;
const char *oper;
id_tab (const char *id_, const char *oper_): id (id_), oper (oper_) {}
};
c_expr (cpp_reader *r_, location_t loc,
vec<cpp_token> code_, unsigned nr_stmts_,
vec<id_tab> ids_, cid_map_t *capture_ids_)
: operand (OP_C_EXPR, loc), r (r_), code (code_),
capture_ids (capture_ids_), nr_stmts (nr_stmts_), ids (ids_) {}
/* cpplib tokens and state to transform this back to source. */
cpp_reader *r;
vec<cpp_token> code;
cid_map_t *capture_ids;
/* The number of statements parsed (well, the number of ';'s). */
unsigned nr_stmts;
/* The identifier replacement vector. */
vec<id_tab> ids;
virtual void gen_transform (FILE *f, int, const char *, bool, int,
const char *, capture_info *,
dt_operand ** = 0, int = 0);
};
/* A wrapper around another operand that captures its value. */
class capture : public operand
{
public:
capture (location_t loc, unsigned where_, operand *what_, bool value_)
: operand (OP_CAPTURE, loc), where (where_), value_match (value_),
what (what_) {}
/* Identifier index for the value. */
unsigned where;
/* Whether in a match of two operands the compare should be for
equal values rather than equal atoms (boils down to a type
check or not). */
bool value_match;
/* The captured value. */
operand *what;
virtual void gen_transform (FILE *f, int, const char *, bool, int,
const char *, capture_info *,
dt_operand ** = 0, int = 0);
};
/* if expression. */
class if_expr : public operand
{
public:
if_expr (location_t loc)
: operand (OP_IF, loc), cond (NULL), trueexpr (NULL), falseexpr (NULL) {}
c_expr *cond;
operand *trueexpr;
operand *falseexpr;
};
/* with expression. */
class with_expr : public operand
{
public:
with_expr (location_t loc)
: operand (OP_WITH, loc), with (NULL), subexpr (NULL) {}
c_expr *with;
operand *subexpr;
};
template<>
template<>
inline bool
is_a_helper <capture *>::test (operand *op)
{
return op->type == operand::OP_CAPTURE;
}
template<>
template<>
inline bool
is_a_helper <predicate *>::test (operand *op)
{
return op->type == operand::OP_PREDICATE;
}
template<>
template<>
inline bool
is_a_helper <c_expr *>::test (operand *op)
{
return op->type == operand::OP_C_EXPR;
}
template<>
template<>
inline bool
is_a_helper <expr *>::test (operand *op)
{
return op->type == operand::OP_EXPR;
}
template<>
template<>
inline bool
is_a_helper <if_expr *>::test (operand *op)
{
return op->type == operand::OP_IF;
}
template<>
template<>
inline bool
is_a_helper <with_expr *>::test (operand *op)
{
return op->type == operand::OP_WITH;
}
/* The main class of a pattern and its transform. This is used to
represent both (simplify ...) and (match ...) kinds. The AST
duplicates all outer 'if' and 'for' expressions here so each
simplify can exist in isolation. */
class simplify
{
public:
enum simplify_kind { SIMPLIFY, MATCH };
simplify (simplify_kind kind_, unsigned id_, operand *match_,
operand *result_, vec<vec<user_id *> > for_vec_,
cid_map_t *capture_ids_)
: kind (kind_), id (id_), match (match_), result (result_),
for_vec (for_vec_), for_subst_vec (vNULL),
capture_ids (capture_ids_), capture_max (capture_ids_->elements () - 1) {}
simplify_kind kind;
/* ID. This is kept to easily associate related simplifies expanded
from the same original one. */
unsigned id;
/* The expression that is matched against the GENERIC or GIMPLE IL. */
operand *match;
/* For a (simplify ...) an expression with ifs and withs with the expression
produced when the pattern applies in the leafs.
For a (match ...) the leafs are either empty if it is a simple predicate
or the single expression specifying the matched operands. */
class operand *result;
/* Collected 'for' expression operators that have to be replaced
in the lowering phase. */
vec<vec<user_id *> > for_vec;
vec<std::pair<user_id *, id_base *> > for_subst_vec;
/* A map of capture identifiers to indexes. */
cid_map_t *capture_ids;
int capture_max;
};
/* Debugging routines for dumping the AST. */
DEBUG_FUNCTION void
print_operand (operand *o, FILE *f = stderr, bool flattened = false)
{
if (capture *c = dyn_cast<capture *> (o))
{
if (c->what && flattened == false)
print_operand (c->what, f, flattened);
fprintf (f, "@%u", c->where);
}
else if (predicate *p = dyn_cast<predicate *> (o))
fprintf (f, "%s", p->p->id);
else if (is_a<c_expr *> (o))
fprintf (f, "c_expr");
else if (expr *e = dyn_cast<expr *> (o))
{
if (e->ops.length () == 0)
fprintf (f, "%s", e->operation->id);
else
{
fprintf (f, "(%s", e->operation->id);
if (flattened == false)
{
for (unsigned i = 0; i < e->ops.length (); ++i)
{
putc (' ', f);
print_operand (e->ops[i], f, flattened);
}
}
putc (')', f);
}
}
else
gcc_unreachable ();
}
DEBUG_FUNCTION void
print_matches (class simplify *s, FILE *f = stderr)
{
fprintf (f, "for expression: ");
print_operand (s->match, f);
putc ('\n', f);
}
/* AST lowering. */
/* Lowering of commutative operators. */
static void
cartesian_product (const vec< vec<operand *> >& ops_vector,
vec< vec<operand *> >& result, vec<operand *>& v, unsigned n)
{
if (n == ops_vector.length ())
{
vec<operand *> xv = v.copy ();
result.safe_push (xv);
return;
}
for (unsigned i = 0; i < ops_vector[n].length (); ++i)
{
v[n] = ops_vector[n][i];
cartesian_product (ops_vector, result, v, n + 1);
}
}
/* Lower OP to two operands in case it is marked as commutative. */
static vec<operand *>
commutate (operand *op, vec<vec<user_id *> > &for_vec)
{
vec<operand *> ret = vNULL;
if (capture *c = dyn_cast <capture *> (op))
{
if (!c->what)
{
ret.safe_push (op);
return ret;
}
vec<operand *> v = commutate (c->what, for_vec);
for (unsigned i = 0; i < v.length (); ++i)
{
capture *nc = new capture (c->location, c->where, v[i],
c->value_match);
ret.safe_push (nc);
}
return ret;
}
expr *e = dyn_cast <expr *> (op);
if (!e || e->ops.length () == 0)
{
ret.safe_push (op);
return ret;
}
vec< vec<operand *> > ops_vector = vNULL;
for (unsigned i = 0; i < e->ops.length (); ++i)
ops_vector.safe_push (commutate (e->ops[i], for_vec));
auto_vec< vec<operand *> > result;
auto_vec<operand *> v (e->ops.length ());
v.quick_grow_cleared (e->ops.length ());
cartesian_product (ops_vector, result, v, 0);
for (unsigned i = 0; i < result.length (); ++i)
{
expr *ne = new expr (e);
ne->is_commutative = false;
for (unsigned j = 0; j < result[i].length (); ++j)
ne->append_op (result[i][j]);
ret.safe_push (ne);
}
if (!e->is_commutative)
return ret;
/* The operation is always binary if it isn't inherently commutative. */
int natural_opno = commutative_op (e->operation);
unsigned int opno = natural_opno >= 0 ? natural_opno : 0;
for (unsigned i = 0; i < result.length (); ++i)
{
expr *ne = new expr (e);
if (operator_id *r = dyn_cast <operator_id *> (ne->operation))
{
if (comparison_code_p (r->code))
ne->operation = swap_tree_comparison (r);
}
else if (user_id *p = dyn_cast <user_id *> (ne->operation))
{
bool found_compare = false;
for (unsigned j = 0; j < p->substitutes.length (); ++j)
if (operator_id *q = dyn_cast <operator_id *> (p->substitutes[j]))
{
if (comparison_code_p (q->code)
&& swap_tree_comparison (q) != q)
{
found_compare = true;
break;
}
}
if (found_compare)
{
user_id *newop = new user_id ("<internal>");
for (unsigned j = 0; j < p->substitutes.length (); ++j)
{
id_base *subst = p->substitutes[j];
if (operator_id *q = dyn_cast <operator_id *> (subst))
{
if (comparison_code_p (q->code))
subst = swap_tree_comparison (q);
}
newop->substitutes.safe_push (subst);
}
ne->operation = newop;
/* Search for 'p' inside the for vector and push 'newop'
to the same level. */
for (unsigned j = 0; newop && j < for_vec.length (); ++j)
for (unsigned k = 0; k < for_vec[j].length (); ++k)
if (for_vec[j][k] == p)
{
for_vec[j].safe_push (newop);
newop = NULL;
break;
}
}
}
ne->is_commutative = false;
for (unsigned j = 0; j < result[i].length (); ++j)
{
int old_j = (j == opno ? opno + 1 : j == opno + 1 ? opno : j);
ne->append_op (result[i][old_j]);
}
ret.safe_push (ne);
}
return ret;
}
/* Lower operations marked as commutative in the AST of S and push
the resulting patterns to SIMPLIFIERS. */
static void
lower_commutative (simplify *s, vec<simplify *>& simplifiers)
{
vec<operand *> matchers = commutate (s->match, s->for_vec);
for (unsigned i = 0; i < matchers.length (); ++i)
{
simplify *ns = new simplify (s->kind, s->id, matchers[i], s->result,
s->for_vec, s->capture_ids);
simplifiers.safe_push (ns);
}
}
/* Strip conditional operations using group GRP from O and its
children if STRIP, else replace them with an unconditional operation. */
operand *
lower_opt (operand *o, unsigned char grp, bool strip)
{
if (capture *c = dyn_cast<capture *> (o))
{
if (c->what)
return new capture (c->location, c->where,
lower_opt (c->what, grp, strip),
c->value_match);
else
return c;
}
expr *e = dyn_cast<expr *> (o);
if (!e)
return o;
if (e->opt_grp == grp)
{
if (strip)
return lower_opt (e->ops[0], grp, strip);
expr *ne = new expr (e);
ne->opt_grp = 0;
ne->append_op (lower_opt (e->ops[0], grp, strip));
return ne;
}
expr *ne = new expr (e);
for (unsigned i = 0; i < e->ops.length (); ++i)
ne->append_op (lower_opt (e->ops[i], grp, strip));
return ne;
}
/* Determine whether O or its children uses the conditional operation
group GRP. */
static bool
has_opt (operand *o, unsigned char grp)
{
if (capture *c = dyn_cast<capture *> (o))
{
if (c->what)
return has_opt (c->what, grp);
else
return false;
}
expr *e = dyn_cast<expr *> (o);
if (!e)
return false;
if (e->opt_grp == grp)
return true;
for (unsigned i = 0; i < e->ops.length (); ++i)
if (has_opt (e->ops[i], grp))
return true;
return false;
}
/* Lower conditional convert operators in O, expanding it to a vector
if required. */
static vec<operand *>
lower_opt (operand *o)
{
vec<operand *> v1 = vNULL, v2;
v1.safe_push (o);
/* Conditional operations are lowered to a pattern with the
operation and one without. All different conditional operation
groups are lowered separately. */
for (unsigned i = 1; i <= 10; ++i)
{
v2 = vNULL;
for (unsigned j = 0; j < v1.length (); ++j)
if (has_opt (v1[j], i))
{
v2.safe_push (lower_opt (v1[j], i, false));
v2.safe_push (lower_opt (v1[j], i, true));
}
if (v2 != vNULL)
{
v1 = vNULL;
for (unsigned j = 0; j < v2.length (); ++j)
v1.safe_push (v2[j]);
}
}
return v1;
}
/* Lower conditional convert operators in the AST of S and push
the resulting multiple patterns to SIMPLIFIERS. */
static void
lower_opt (simplify *s, vec<simplify *>& simplifiers)
{
vec<operand *> matchers = lower_opt (s->match);
for (unsigned i = 0; i < matchers.length (); ++i)
{
simplify *ns = new simplify (s->kind, s->id, matchers[i], s->result,
s->for_vec, s->capture_ids);
simplifiers.safe_push (ns);
}
}
/* Lower the compare operand of COND_EXPRs and VEC_COND_EXPRs to a
GENERIC and a GIMPLE variant. */
static vec<operand *>
lower_cond (operand *o)
{
vec<operand *> ro = vNULL;
if (capture *c = dyn_cast<capture *> (o))
{
if (c->what)
{
vec<operand *> lop = vNULL;
lop = lower_cond (c->what);
for (unsigned i = 0; i < lop.length (); ++i)
ro.safe_push (new capture (c->location, c->where, lop[i],
c->value_match));
return ro;
}
}
expr *e = dyn_cast<expr *> (o);
if (!e || e->ops.length () == 0)
{
ro.safe_push (o);
return ro;
}
vec< vec<operand *> > ops_vector = vNULL;
for (unsigned i = 0; i < e->ops.length (); ++i)
ops_vector.safe_push (lower_cond (e->ops[i]));
auto_vec< vec<operand *> > result;
auto_vec<operand *> v (e->ops.length ());
v.quick_grow_cleared (e->ops.length ());
cartesian_product (ops_vector, result, v, 0);
for (unsigned i = 0; i < result.length (); ++i)
{
expr *ne = new expr (e);
for (unsigned j = 0; j < result[i].length (); ++j)
ne->append_op (result[i][j]);
ro.safe_push (ne);
/* If this is a COND with a captured expression or an
expression with two operands then also match a GENERIC
form on the compare. */
if ((*e->operation == COND_EXPR
|| *e->operation == VEC_COND_EXPR)
&& ((is_a <capture *> (e->ops[0])
&& as_a <capture *> (e->ops[0])->what
&& is_a <expr *> (as_a <capture *> (e->ops[0])->what)
&& as_a <expr *>
(as_a <capture *> (e->ops[0])->what)->ops.length () == 2)
|| (is_a <expr *> (e->ops[0])
&& as_a <expr *> (e->ops[0])->ops.length () == 2)))
{
ne = new expr (e);
for (unsigned j = 0; j < result[i].length (); ++j)
ne->append_op (result[i][j]);
if (capture *c = dyn_cast <capture *> (ne->ops[0]))
{
expr *ocmp = as_a <expr *> (c->what);
expr *cmp = new expr (ocmp);
for (unsigned j = 0; j < ocmp->ops.length (); ++j)
cmp->append_op (ocmp->ops[j]);
cmp->is_generic = true;
ne->ops[0] = new capture (c->location, c->where, cmp,
c->value_match);
}
else
{
expr *ocmp = as_a <expr *> (ne->ops[0]);
expr *cmp = new expr (ocmp);
for (unsigned j = 0; j < ocmp->ops.length (); ++j)
cmp->append_op (ocmp->ops[j]);
cmp->is_generic = true;
ne->ops[0] = cmp;
}
ro.safe_push (ne);
}
}
return ro;
}
/* Lower the compare operand of COND_EXPRs and VEC_COND_EXPRs to a
GENERIC and a GIMPLE variant. */
static void
lower_cond (simplify *s, vec<simplify *>& simplifiers)
{
vec<operand *> matchers = lower_cond (s->match);
for (unsigned i = 0; i < matchers.length (); ++i)
{
simplify *ns = new simplify (s->kind, s->id, matchers[i], s->result,
s->for_vec, s->capture_ids);
simplifiers.safe_push (ns);
}
}
/* Return true if O refers to ID. */
bool
contains_id (operand *o, user_id *id)
{
if (capture *c = dyn_cast<capture *> (o))
return c->what && contains_id (c->what, id);
if (expr *e = dyn_cast<expr *> (o))
{
if (e->operation == id)
return true;
for (unsigned i = 0; i < e->ops.length (); ++i)
if (contains_id (e->ops[i], id))
return true;
return false;
}
if (with_expr *w = dyn_cast <with_expr *> (o))
return (contains_id (w->with, id)
|| contains_id (w->subexpr, id));
if (if_expr *ife = dyn_cast <if_expr *> (o))
return (contains_id (ife->cond, id)
|| contains_id (ife->trueexpr, id)
|| (ife->falseexpr && contains_id (ife->falseexpr, id)));
if (c_expr *ce = dyn_cast<c_expr *> (o))
return ce->capture_ids && ce->capture_ids->get (id->id);
return false;
}
/* In AST operand O replace operator ID with operator WITH. */
operand *
replace_id (operand *o, user_id *id, id_base *with)
{
/* Deep-copy captures and expressions, replacing operations as
needed. */
if (capture *c = dyn_cast<capture *> (o))
{
if (!c->what)
return c;
return new capture (c->location, c->where,
replace_id (c->what, id, with), c->value_match);
}
else if (expr *e = dyn_cast<expr *> (o))
{
expr *ne = new expr (e);
if (e->operation == id)
ne->operation = with;
for (unsigned i = 0; i < e->ops.length (); ++i)
ne->append_op (replace_id (e->ops[i], id, with));
return ne;
}
else if (with_expr *w = dyn_cast <with_expr *> (o))
{
with_expr *nw = new with_expr (w->location);
nw->with = as_a <c_expr *> (replace_id (w->with, id, with));
nw->subexpr = replace_id (w->subexpr, id, with);
return nw;
}
else if (if_expr *ife = dyn_cast <if_expr *> (o))
{
if_expr *nife = new if_expr (ife->location);
nife->cond = as_a <c_expr *> (replace_id (ife->cond, id, with));
nife->trueexpr = replace_id (ife->trueexpr, id, with);
if (ife->falseexpr)
nife->falseexpr = replace_id (ife->falseexpr, id, with);
return nife;
}
/* For c_expr we simply record a string replacement table which is
applied at code-generation time. */
if (c_expr *ce = dyn_cast<c_expr *> (o))
{
vec<c_expr::id_tab> ids = ce->ids.copy ();
ids.safe_push (c_expr::id_tab (id->id, with->id));
return new c_expr (ce->r, ce->location,
ce->code, ce->nr_stmts, ids, ce->capture_ids);
}
return o;
}
/* Return true if the binary operator OP is ok for delayed substitution
during for lowering. */
static bool
binary_ok (operator_id *op)
{
switch (op->code)
{
case PLUS_EXPR:
case MINUS_EXPR:
case MULT_EXPR:
case TRUNC_DIV_EXPR:
case CEIL_DIV_EXPR:
case FLOOR_DIV_EXPR:
case ROUND_DIV_EXPR:
case TRUNC_MOD_EXPR:
case CEIL_MOD_EXPR:
case FLOOR_MOD_EXPR:
case ROUND_MOD_EXPR:
case RDIV_EXPR:
case EXACT_DIV_EXPR:
case MIN_EXPR:
case MAX_EXPR:
case BIT_IOR_EXPR:
case BIT_XOR_EXPR:
case BIT_AND_EXPR:
return true;
default:
return false;
}
}
/* Lower recorded fors for SIN and output to SIMPLIFIERS. */
static void
lower_for (simplify *sin, vec<simplify *>& simplifiers)
{
vec<vec<user_id *> >& for_vec = sin->for_vec;
unsigned worklist_start = 0;
auto_vec<simplify *> worklist;
worklist.safe_push (sin);
/* Lower each recorded for separately, operating on the
set of simplifiers created by the previous one.
Lower inner-to-outer so inner for substitutes can refer
to operators replaced by outer fors. */
for (int fi = for_vec.length () - 1; fi >= 0; --fi)
{
vec<user_id *>& ids = for_vec[fi];
unsigned n_ids = ids.length ();
unsigned max_n_opers = 0;
bool can_delay_subst = (sin->kind == simplify::SIMPLIFY);
for (unsigned i = 0; i < n_ids; ++i)
{
if (ids[i]->substitutes.length () > max_n_opers)
max_n_opers = ids[i]->substitutes.length ();
/* Require that all substitutes are of the same kind so that
if we delay substitution to the result op code generation
can look at the first substitute for deciding things like
types of operands. */
enum id_base::id_kind kind = ids[i]->substitutes[0]->kind;
for (unsigned j = 0; j < ids[i]->substitutes.length (); ++j)
if (ids[i]->substitutes[j]->kind != kind)
can_delay_subst = false;
else if (operator_id *op
= dyn_cast <operator_id *> (ids[i]->substitutes[j]))
{
operator_id *op0
= as_a <operator_id *> (ids[i]->substitutes[0]);
if (strcmp (op->tcc, "tcc_comparison") == 0
&& strcmp (op0->tcc, "tcc_comparison") == 0)
;
/* Unfortunately we can't just allow all tcc_binary. */
else if (strcmp (op->tcc, "tcc_binary") == 0
&& strcmp (op0->tcc, "tcc_binary") == 0
&& binary_ok (op)
&& binary_ok (op0))
;
else if ((strcmp (op->id + 1, "SHIFT_EXPR") == 0
|| strcmp (op->id + 1, "ROTATE_EXPR") == 0)
&& (strcmp (op0->id + 1, "SHIFT_EXPR") == 0
|| strcmp (op0->id + 1, "ROTATE_EXPR") == 0))
;
else
can_delay_subst = false;
}
else if (is_a <fn_id *> (ids[i]->substitutes[j]))
;
else
can_delay_subst = false;
}
unsigned worklist_end = worklist.length ();
for (unsigned si = worklist_start; si < worklist_end; ++si)
{
simplify *s = worklist[si];
for (unsigned j = 0; j < max_n_opers; ++j)
{
operand *match_op = s->match;
operand *result_op = s->result;
auto_vec<std::pair<user_id *, id_base *> > subst (n_ids);
bool skip = false;
for (unsigned i = 0; i < n_ids; ++i)
{
user_id *id = ids[i];
id_base *oper = id->substitutes[j % id->substitutes.length ()];
if (oper == null_id
&& (contains_id (match_op, id)
|| contains_id (result_op, id)))
{
skip = true;
break;
}
subst.quick_push (std::make_pair (id, oper));
match_op = replace_id (match_op, id, oper);
if (result_op
&& !can_delay_subst)
result_op = replace_id (result_op, id, oper);
}
if (skip)
continue;
simplify *ns = new simplify (s->kind, s->id, match_op, result_op,
vNULL, s->capture_ids);
ns->for_subst_vec.safe_splice (s->for_subst_vec);
if (result_op
&& can_delay_subst)
ns->for_subst_vec.safe_splice (subst);
worklist.safe_push (ns);
}
}
worklist_start = worklist_end;
}
/* Copy out the result from the last for lowering. */
for (unsigned i = worklist_start; i < worklist.length (); ++i)
simplifiers.safe_push (worklist[i]);
}
/* Lower the AST for everything in SIMPLIFIERS. */
static void
lower (vec<simplify *>& simplifiers, bool gimple)
{
auto_vec<simplify *> out_simplifiers;
for (unsigned i = 0; i < simplifiers.length (); ++i)
lower_opt (simplifiers[i], out_simplifiers);
simplifiers.truncate (0);
for (unsigned i = 0; i < out_simplifiers.length (); ++i)
lower_commutative (out_simplifiers[i], simplifiers);
out_simplifiers.truncate (0);
if (gimple)
for (unsigned i = 0; i < simplifiers.length (); ++i)
lower_cond (simplifiers[i], out_simplifiers);
else
out_simplifiers.safe_splice (simplifiers);
simplifiers.truncate (0);
for (unsigned i = 0; i < out_simplifiers.length (); ++i)
lower_for (out_simplifiers[i], simplifiers);
}
/* The decision tree built for generating GIMPLE and GENERIC pattern
matching code. It represents the 'match' expression of all
simplifies and has those as its leafs. */
class dt_simplify;
/* A hash-map collecting semantically equivalent leafs in the decision
tree for splitting out to separate functions. */
struct sinfo
{
dt_simplify *s;
const char *fname;
unsigned cnt;
};
struct sinfo_hashmap_traits : simple_hashmap_traits<pointer_hash<dt_simplify>,
sinfo *>
{
static inline hashval_t hash (const key_type &);
static inline bool equal_keys (const key_type &, const key_type &);
template <typename T> static inline void remove (T &) {}
};
typedef hash_map<void * /* unused */, sinfo *, sinfo_hashmap_traits>
sinfo_map_t;
/* Current simplifier ID we are processing during insertion into the
decision tree. */
static unsigned current_id;
/* Decision tree base class, used for DT_NODE. */
class dt_node
{
public:
enum dt_type { DT_NODE, DT_OPERAND, DT_TRUE, DT_MATCH, DT_SIMPLIFY };
enum dt_type type;
unsigned level;
dt_node *parent;
vec<dt_node *> kids;
/* Statistics. */
unsigned num_leafs;
unsigned total_size;
unsigned max_level;
dt_node (enum dt_type type_, dt_node *parent_)
: type (type_), level (0), parent (parent_), kids (vNULL) {}
dt_node *append_node (dt_node *);
dt_node *append_op (operand *, dt_node *parent, unsigned pos);
dt_node *append_true_op (operand *, dt_node *parent, unsigned pos);
dt_node *append_match_op (operand *, dt_operand *, dt_node *parent,
unsigned pos);
dt_node *append_simplify (simplify *, unsigned, dt_operand **);
virtual void gen (FILE *, int, bool, int) {}
void gen_kids (FILE *, int, bool, int);
void gen_kids_1 (FILE *, int, bool, int,
vec<dt_operand *>, vec<dt_operand *>, vec<dt_operand *>,
vec<dt_operand *>, vec<dt_operand *>, vec<dt_node *>);
void analyze (sinfo_map_t &);
};
/* Generic decision tree node used for DT_OPERAND, DT_MATCH and DT_TRUE. */
class dt_operand : public dt_node
{
public:
operand *op;
dt_operand *match_dop;
unsigned pos;
bool value_match;
unsigned for_id;
dt_operand (enum dt_type type, operand *op_, dt_operand *match_dop_,
dt_operand *parent_, unsigned pos_)
: dt_node (type, parent_), op (op_), match_dop (match_dop_),
pos (pos_), value_match (false), for_id (current_id) {}
void gen (FILE *, int, bool, int);
unsigned gen_predicate (FILE *, int, const char *, bool);
unsigned gen_match_op (FILE *, int, const char *, bool);
unsigned gen_gimple_expr (FILE *, int, int);
unsigned gen_generic_expr (FILE *, int, const char *);
char *get_name (char *);
void gen_opname (char *, unsigned);
};
/* Leaf node of the decision tree, used for DT_SIMPLIFY. */
class dt_simplify : public dt_node
{
public:
simplify *s;
unsigned pattern_no;
dt_operand **indexes;
sinfo *info;
dt_simplify (simplify *s_, unsigned pattern_no_, dt_operand **indexes_)
: dt_node (DT_SIMPLIFY, NULL), s (s_), pattern_no (pattern_no_),
indexes (indexes_), info (NULL) {}
void gen_1 (FILE *, int, bool, operand *);
void gen (FILE *f, int, bool, int);
};
template<>
template<>
inline bool
is_a_helper <dt_operand *>::test (dt_node *n)
{
return (n->type == dt_node::DT_OPERAND
|| n->type == dt_node::DT_MATCH
|| n->type == dt_node::DT_TRUE);
}
template<>
template<>
inline bool
is_a_helper <dt_simplify *>::test (dt_node *n)
{
return n->type == dt_node::DT_SIMPLIFY;
}
/* A container for the actual decision tree. */
class decision_tree
{
public:
dt_node *root;
void insert (class simplify *, unsigned);
void gen (FILE *f, bool gimple);
void print (FILE *f = stderr);
decision_tree () { root = new dt_node (dt_node::DT_NODE, NULL); }
static dt_node *insert_operand (dt_node *, operand *, dt_operand **indexes,
unsigned pos = 0, dt_node *parent = 0);
static dt_node *find_node (vec<dt_node *>&, dt_node *);
static bool cmp_node (dt_node *, dt_node *);
static void print_node (dt_node *, FILE *f = stderr, unsigned = 0);
};
/* Compare two AST operands O1 and O2 and return true if they are equal. */
bool
cmp_operand (operand *o1, operand *o2)
{
if (!o1 || !o2 || o1->type != o2->type)
return false;
if (o1->type == operand::OP_PREDICATE)
{
predicate *p1 = as_a<predicate *>(o1);
predicate *p2 = as_a<predicate *>(o2);
return p1->p == p2->p;
}
else if (o1->type == operand::OP_EXPR)
{
expr *e1 = static_cast<expr *>(o1);
expr *e2 = static_cast<expr *>(o2);
return (e1->operation == e2->operation
&& e1->is_generic == e2->is_generic);
}
else
return false;
}
/* Compare two decision tree nodes N1 and N2 and return true if they
are equal. */
bool
decision_tree::cmp_node (dt_node *n1, dt_node *n2)
{
if (!n1 || !n2 || n1->type != n2->type)
return false;
if (n1 == n2)
return true;
if (n1->type == dt_node::DT_TRUE)
return false;
if (n1->type == dt_node::DT_OPERAND)
return cmp_operand ((as_a<dt_operand *> (n1))->op,
(as_a<dt_operand *> (n2))->op);
else if (n1->type == dt_node::DT_MATCH)
return (((as_a<dt_operand *> (n1))->match_dop
== (as_a<dt_operand *> (n2))->match_dop)
&& ((as_a<dt_operand *> (n1))->value_match
== (as_a<dt_operand *> (n2))->value_match));
return false;
}
/* Search OPS for a decision tree node like P and return it if found. */
dt_node *
decision_tree::find_node (vec<dt_node *>& ops, dt_node *p)
{
/* We can merge adjacent DT_TRUE. */
if (p->type == dt_node::DT_TRUE
&& !ops.is_empty ()
&& ops.last ()->type == dt_node::DT_TRUE)
return ops.last ();
dt_operand *true_node = NULL;
for (int i = ops.length () - 1; i >= 0; --i)
{
/* But we can't merge across DT_TRUE nodes as they serve as
pattern order barriers to make sure that patterns apply
in order of appearance in case multiple matches are possible. */
if (ops[i]->type == dt_node::DT_TRUE)
{
if (! true_node
|| as_a <dt_operand *> (ops[i])->for_id > true_node->for_id)
true_node = as_a <dt_operand *> (ops[i]);
}
if (decision_tree::cmp_node (ops[i], p))
{
/* Unless we are processing the same pattern or the blocking
pattern is before the one we are going to merge with. */
if (true_node
&& true_node->for_id != current_id
&& true_node->for_id > as_a <dt_operand *> (ops[i])->for_id)
{
if (verbose >= 1)
{
location_t p_loc = 0;
if (p->type == dt_node::DT_OPERAND)
p_loc = as_a <dt_operand *> (p)->op->location;
location_t op_loc = 0;
if (ops[i]->type == dt_node::DT_OPERAND)
op_loc = as_a <dt_operand *> (ops[i])->op->location;
location_t true_loc = 0;
true_loc = true_node->op->location;
warning_at (p_loc,
"failed to merge decision tree node");
warning_at (op_loc,
"with the following");
warning_at (true_loc,
"because of the following which serves as ordering "
"barrier");
}
return NULL;
}
return ops[i];
}
}
return NULL;
}
/* Append N to the decision tree if it there is not already an existing
identical child. */
dt_node *
dt_node::append_node (dt_node *n)
{
dt_node *kid;
kid = decision_tree::find_node (kids, n);
if (kid)
return kid;
kids.safe_push (n);
n->level = this->level + 1;
return n;
}
/* Append OP to the decision tree. */
dt_node *
dt_node::append_op (operand *op, dt_node *parent, unsigned pos)
{
dt_operand *parent_ = safe_as_a<dt_operand *> (parent);
dt_operand *n = new dt_operand (DT_OPERAND, op, 0, parent_, pos);
return append_node (n);
}
/* Append a DT_TRUE decision tree node. */
dt_node *
dt_node::append_true_op (operand *op, dt_node *parent, unsigned pos)
{
dt_operand *parent_ = safe_as_a<dt_operand *> (parent);
dt_operand *n = new dt_operand (DT_TRUE, op, 0, parent_, pos);
return append_node (n);
}
/* Append a DT_MATCH decision tree node. */
dt_node *
dt_node::append_match_op (operand *op, dt_operand *match_dop,
dt_node *parent, unsigned pos)
{
dt_operand *parent_ = as_a<dt_operand *> (parent);
dt_operand *n = new dt_operand (DT_MATCH, op, match_dop, parent_, pos);
return append_node (n);
}
/* Append S to the decision tree. */
dt_node *
dt_node::append_simplify (simplify *s, unsigned pattern_no,
dt_operand **indexes)
{
dt_simplify *s2;
dt_simplify *n = new dt_simplify (s, pattern_no, indexes);
for (unsigned i = 0; i < kids.length (); ++i)
if ((s2 = dyn_cast <dt_simplify *> (kids[i]))
&& (verbose >= 1
|| s->match->location != s2->s->match->location))
{
/* With a nested patters, it's hard to avoid these in order
to keep match.pd rules relatively small. */
warning_at (s->match->location, "duplicate pattern");
warning_at (s2->s->match->location, "previous pattern defined here");
print_operand (s->match, stderr);
fprintf (stderr, "\n");
}
return append_node (n);
}
/* Analyze the node and its children. */
void
dt_node::analyze (sinfo_map_t &map)
{
num_leafs = 0;
total_size = 1;
max_level = level;
if (type == DT_SIMPLIFY)
{
/* Populate the map of equivalent simplifies. */
dt_simplify *s = as_a <dt_simplify *> (this);
bool existed;
sinfo *&si = map.get_or_insert (s, &existed);
if (!existed)
{
si = new sinfo;
si->s = s;
si->cnt = 1;
si->fname = NULL;
}
else
si->cnt++;
s->info = si;
num_leafs = 1;
return;
}
for (unsigned i = 0; i < kids.length (); ++i)
{
kids[i]->analyze (map);
num_leafs += kids[i]->num_leafs;
total_size += kids[i]->total_size;
max_level = MAX (max_level, kids[i]->max_level);
}
}
/* Insert O into the decision tree and return the decision tree node found
or created. */
dt_node *
decision_tree::insert_operand (dt_node *p, operand *o, dt_operand **indexes,
unsigned pos, dt_node *parent)
{
dt_node *q, *elm = 0;
if (capture *c = dyn_cast<capture *> (o))
{
unsigned capt_index = c->where;
if (indexes[capt_index] == 0)
{
if (c->what)
q = insert_operand (p, c->what, indexes, pos, parent);
else
{
q = elm = p->append_true_op (o, parent, pos);
goto at_assert_elm;
}
// get to the last capture
for (operand *what = c->what;
what && is_a<capture *> (what);
c = as_a<capture *> (what), what = c->what)
;
if (!c->what)
{
unsigned cc_index = c->where;
dt_operand *match_op = indexes[cc_index];
dt_operand temp (dt_node::DT_TRUE, 0, 0, 0, 0);
elm = decision_tree::find_node (p->kids, &temp);
if (elm == 0)
{
dt_operand match (dt_node::DT_MATCH, 0, match_op, 0, 0);
match.value_match = c->value_match;
elm = decision_tree::find_node (p->kids, &match);
}
}
else
{
dt_operand temp (dt_node::DT_OPERAND, c->what, 0, 0, 0);
elm = decision_tree::find_node (p->kids, &temp);
}
at_assert_elm:
gcc_assert (elm->type == dt_node::DT_TRUE
|| elm->type == dt_node::DT_OPERAND
|| elm->type == dt_node::DT_MATCH);
indexes[capt_index] = static_cast<dt_operand *> (elm);
return q;
}
else
{
p = p->append_match_op (o, indexes[capt_index], parent, pos);
as_a <dt_operand *>(p)->value_match = c->value_match;
if (c->what)
return insert_operand (p, c->what, indexes, 0, p);
else
return p;
}
}
p = p->append_op (o, parent, pos);
q = p;
if (expr *e = dyn_cast <expr *>(o))
{
for (unsigned i = 0; i < e->ops.length (); ++i)
q = decision_tree::insert_operand (q, e->ops[i], indexes, i, p);
}
return q;
}
/* Insert S into the decision tree. */
void
decision_tree::insert (class simplify *s, unsigned pattern_no)
{
current_id = s->id;
dt_operand **indexes = XCNEWVEC (dt_operand *, s->capture_max + 1);
dt_node *p = decision_tree::insert_operand (root, s->match, indexes);
p->append_simplify (s, pattern_no, indexes);
}
/* Debug functions to dump the decision tree. */
DEBUG_FUNCTION void
decision_tree::print_node (dt_node *p, FILE *f, unsigned indent)
{
if (p->type == dt_node::DT_NODE)
fprintf (f, "root");
else
{
fprintf (f, "|");
for (unsigned i = 0; i < indent; i++)
fprintf (f, "-");
if (p->type == dt_node::DT_OPERAND)
{
dt_operand *dop = static_cast<dt_operand *>(p);
print_operand (dop->op, f, true);
}
else if (p->type == dt_node::DT_TRUE)
fprintf (f, "true");
else if (p->type == dt_node::DT_MATCH)
fprintf (f, "match (%p)", (void *)((as_a<dt_operand *>(p))->match_dop));
else if (p->type == dt_node::DT_SIMPLIFY)
{
dt_simplify *s = static_cast<dt_simplify *> (p);
fprintf (f, "simplify_%u { ", s->pattern_no);
for (int i = 0; i <= s->s->capture_max; ++i)
fprintf (f, "%p, ", (void *) s->indexes[i]);
fprintf (f, " } ");
}
if (is_a <dt_operand *> (p))
fprintf (f, " [%u]", as_a <dt_operand *> (p)->for_id);
}
fprintf (stderr, " (%p, %p), %u, %u\n",
(void *) p, (void *) p->parent, p->level, p->kids.length ());
for (unsigned i = 0; i < p->kids.length (); ++i)
decision_tree::print_node (p->kids[i], f, indent + 2);
}
DEBUG_FUNCTION void
decision_tree::print (FILE *f)
{
return decision_tree::print_node (root, f);
}
/* For GENERIC we have to take care of wrapping multiple-used
expressions with side-effects in save_expr and preserve side-effects
of expressions with omit_one_operand. Analyze captures in
match, result and with expressions and perform early-outs
on the outermost match expression operands for cases we cannot
handle. */
class capture_info
{
public:
capture_info (simplify *s, operand *, bool);
void walk_match (operand *o, unsigned toplevel_arg, bool, bool);
bool walk_result (operand *o, bool, operand *);
void walk_c_expr (c_expr *);
struct cinfo
{
bool expr_p;
bool cse_p;
bool force_no_side_effects_p;
bool force_single_use;
bool cond_expr_cond_p;
unsigned long toplevel_msk;
unsigned match_use_count;
unsigned result_use_count;
unsigned same_as;
capture *c;
};
auto_vec<cinfo> info;
unsigned long force_no_side_effects;
bool gimple;
};
/* Analyze captures in S. */
capture_info::capture_info (simplify *s, operand *result, bool gimple_)
{
gimple = gimple_;
expr *e;
if (s->kind == simplify::MATCH)
{
force_no_side_effects = -1;
return;
}
force_no_side_effects = 0;
info.safe_grow_cleared (s->capture_max + 1, true);
for (int i = 0; i <= s->capture_max; ++i)
info[i].same_as = i;
e = as_a <expr *> (s->match);
for (unsigned i = 0; i < e->ops.length (); ++i)
walk_match (e->ops[i], i,
(i != 0 && *e->operation == COND_EXPR)
|| *e->operation == TRUTH_ANDIF_EXPR
|| *e->operation == TRUTH_ORIF_EXPR,
i == 0
&& (*e->operation == COND_EXPR
|| *e->operation == VEC_COND_EXPR));
walk_result (s->result, false, result);
}
/* Analyze captures in the match expression piece O. */
void
capture_info::walk_match (operand *o, unsigned toplevel_arg,
bool conditional_p, bool cond_expr_cond_p)
{
if (capture *c = dyn_cast <capture *> (o))
{
unsigned where = c->where;
info[where].match_use_count++;
info[where].toplevel_msk |= 1 << toplevel_arg;
info[where].force_no_side_effects_p |= conditional_p;
info[where].cond_expr_cond_p |= cond_expr_cond_p;
if (!info[where].c)
info[where].c = c;
if (!c->what)
return;
/* Recurse to exprs and captures. */
if (is_a <capture *> (c->what)
|| is_a <expr *> (c->what))
walk_match (c->what, toplevel_arg, conditional_p, false);
/* We need to look past multiple captures to find a captured
expression as with conditional converts two captures
can be collapsed onto the same expression. Also collect
what captures capture the same thing. */
while (c->what && is_a <capture *> (c->what))
{
c = as_a <capture *> (c->what);
if (info[c->where].same_as != c->where
&& info[c->where].same_as != info[where].same_as)
fatal_at (c->location, "cannot handle this collapsed capture");
info[c->where].same_as = info[where].same_as;
}
/* Mark expr (non-leaf) captures and forced single-use exprs. */
expr *e;
if (c->what
&& (e = dyn_cast <expr *> (c->what)))
{
/* Zero-operand expression captures like ADDR_EXPR@0 are
similar as predicates -- if they are not mentioned in
the result we have to force them to have no side-effects. */
if (e->ops.length () != 0)
info[where].expr_p = true;
info[where].force_single_use |= e->force_single_use;
}
}
else if (expr *e = dyn_cast <expr *> (o))
{
for (unsigned i = 0; i < e->ops.length (); ++i)
{
bool cond_p = conditional_p;
bool expr_cond_p = false;
if (i != 0 && *e->operation == COND_EXPR)
cond_p = true;
else if (*e->operation == TRUTH_ANDIF_EXPR
|| *e->operation == TRUTH_ORIF_EXPR)
cond_p = true;
if (i == 0
&& (*e->operation == COND_EXPR
|| *e->operation == VEC_COND_EXPR))
expr_cond_p = true;
walk_match (e->ops[i], toplevel_arg, cond_p, expr_cond_p);
}
}
else if (is_a <predicate *> (o))
{
/* Mark non-captured leafs toplevel arg for checking. */
force_no_side_effects |= 1 << toplevel_arg;
if (verbose >= 1
&& !gimple)
warning_at (o->location,
"forcing no side-effects on possibly lost leaf");
}
else
gcc_unreachable ();
}
/* Analyze captures in the result expression piece O. Return true
if RESULT was visited in one of the children. Only visit
non-if/with children if they are rooted on RESULT. */
bool
capture_info::walk_result (operand *o, bool conditional_p, operand *result)
{
if (capture *c = dyn_cast <capture *> (o))
{
unsigned where = info[c->where].same_as;
info[where].result_use_count++;
/* If we substitute an expression capture we don't know
which captures this will end up using (well, we don't
compute that). Force the uses to be side-effect free
which means forcing the toplevels that reach the
expression side-effect free. */
if (info[where].expr_p)
force_no_side_effects |= info[where].toplevel_msk;
/* Mark CSE capture uses as forced to have no side-effects. */
if (c->what
&& is_a <expr *> (c->what))
{
info[where].cse_p = true;
walk_result (c->what, true, result);
}
}
else if (expr *e = dyn_cast <expr *> (o))
{
id_base *opr = e->operation;
if (user_id *uid = dyn_cast <user_id *> (opr))
opr = uid->substitutes[0];
for (unsigned i = 0; i < e->ops.length (); ++i)
{
bool cond_p = conditional_p;
if (i != 0 && *e->operation == COND_EXPR)
cond_p = true;
else if (*e->operation == TRUTH_ANDIF_EXPR
|| *e->operation == TRUTH_ORIF_EXPR)
cond_p = true;
walk_result (e->ops[i], cond_p, result);
}
}
else if (if_expr *ie = dyn_cast <if_expr *> (o))
{
/* 'if' conditions should be all fine. */
if (ie->trueexpr == result)
{
walk_result (ie->trueexpr, false, result);
return true;
}
if (ie->falseexpr == result)
{
walk_result (ie->falseexpr, false, result);
return true;
}
bool res = false;
if (is_a <if_expr *> (ie->trueexpr)
|| is_a <with_expr *> (ie->trueexpr))
res |= walk_result (ie->trueexpr, false, result);
if (ie->falseexpr
&& (is_a <if_expr *> (ie->falseexpr)
|| is_a <with_expr *> (ie->falseexpr)))
res |= walk_result (ie->falseexpr, false, result);
return res;
}
else if (with_expr *we = dyn_cast <with_expr *> (o))
{
bool res = (we->subexpr == result);
if (res
|| is_a <if_expr *> (we->subexpr)
|| is_a <with_expr *> (we->subexpr))
res |= walk_result (we->subexpr, false, result);
if (res)
walk_c_expr (we->with);
return res;
}
else if (c_expr *ce = dyn_cast <c_expr *> (o))
walk_c_expr (ce);
else
gcc_unreachable ();
return false;
}
/* Look for captures in the C expr E. */
void
capture_info::walk_c_expr (c_expr *e)
{
/* Give up for C exprs mentioning captures not inside TREE_TYPE,
TREE_REAL_CST, TREE_CODE or a predicate where they cannot
really escape through. */
unsigned p_depth = 0;
for (unsigned i = 0; i < e->code.length (); ++i)
{
const cpp_token *t = &e->code[i];
const cpp_token *n = i < e->code.length () - 1 ? &e->code[i+1] : NULL;
id_base *id;
if (t->type == CPP_NAME
&& (strcmp ((const char *)CPP_HASHNODE
(t->val.node.node)->ident.str, "TREE_TYPE") == 0
|| strcmp ((const char *)CPP_HASHNODE
(t->val.node.node)->ident.str, "TREE_CODE") == 0
|| strcmp ((const char *)CPP_HASHNODE
(t->val.node.node)->ident.str, "TREE_REAL_CST") == 0
|| ((id = get_operator ((const char *)CPP_HASHNODE
(t->val.node.node)->ident.str))
&& is_a <predicate_id *> (id)))
&& n->type == CPP_OPEN_PAREN)
p_depth++;
else if (t->type == CPP_CLOSE_PAREN
&& p_depth > 0)
p_depth--;
else if (p_depth == 0
&& t->type == CPP_ATSIGN
&& (n->type == CPP_NUMBER
|| n->type == CPP_NAME)
&& !(n->flags & PREV_WHITE))
{
const char *id1;
if (n->type == CPP_NUMBER)
id1 = (const char *)n->val.str.text;
else
id1 = (const char *)CPP_HASHNODE (n->val.node.node)->ident.str;
unsigned *where = e->capture_ids->get(id1);
if (! where)
fatal_at (n, "unknown capture id '%s'", id1);
info[info[*where].same_as].force_no_side_effects_p = true;
if (verbose >= 1
&& !gimple)
warning_at (t, "capture escapes");
}
}
}
/* The current label failing the current matched pattern during
code generation. */
static char *fail_label;
/* Code generation off the decision tree and the refered AST nodes. */
bool
is_conversion (id_base *op)
{
return (*op == CONVERT_EXPR
|| *op == NOP_EXPR
|| *op == FLOAT_EXPR
|| *op == FIX_TRUNC_EXPR
|| *op == VIEW_CONVERT_EXPR);
}
/* Get the type to be used for generating operand POS of OP from the
various sources. */
static const char *
get_operand_type (id_base *op, unsigned pos,
const char *in_type,
const char *expr_type,
const char *other_oprnd_type)
{
/* Generally operands whose type does not match the type of the
expression generated need to know their types but match and
thus can fall back to 'other_oprnd_type'. */
if (is_conversion (op))
return other_oprnd_type;
else if (*op == REALPART_EXPR
|| *op == IMAGPART_EXPR)
return other_oprnd_type;
else if (is_a <operator_id *> (op)
&& strcmp (as_a <operator_id *> (op)->tcc, "tcc_comparison") == 0)
return other_oprnd_type;
else if (*op == COND_EXPR
&& pos == 0)
return "boolean_type_node";
else if (strncmp (op->id, "CFN_COND_", 9) == 0)
{
/* IFN_COND_* operands 1 and later by default have the same type
as the result. The type of operand 0 needs to be specified
explicitly. */
if (pos > 0 && expr_type)
return expr_type;
else if (pos > 0 && in_type)
return in_type;
else
return NULL;
}
else
{
/* Otherwise all types should match - choose one in order of
preference. */
if (expr_type)
return expr_type;
else if (in_type)
return in_type;
else
return other_oprnd_type;
}
}
/* Generate transform code for an expression. */
void
expr::gen_transform (FILE *f, int indent, const char *dest, bool gimple,
int depth, const char *in_type, capture_info *cinfo,
dt_operand **indexes, int)
{
id_base *opr = operation;
/* When we delay operator substituting during lowering of fors we
make sure that for code-gen purposes the effects of each substitute
are the same. Thus just look at that. */
if (user_id *uid = dyn_cast <user_id *> (opr))
opr = uid->substitutes[0];
bool conversion_p = is_conversion (opr);
const char *type = expr_type;
char optype[64];
if (type)
/* If there was a type specification in the pattern use it. */
;
else if (conversion_p)
/* For conversions we need to build the expression using the
outer type passed in. */
type = in_type;
else if (*opr == REALPART_EXPR
|| *opr == IMAGPART_EXPR)
{
/* __real and __imag use the component type of its operand. */
snprintf (optype, sizeof (optype), "TREE_TYPE (TREE_TYPE (_o%d[0]))",
depth);
type = optype;
}
else if (is_a <operator_id *> (opr)
&& !strcmp (as_a <operator_id *> (opr)->tcc, "tcc_comparison"))
{
/* comparisons use boolean_type_node (or what gets in), but
their operands need to figure out the types themselves. */
if (in_type)
type = in_type;
else
{
snprintf (optype, sizeof (optype), "boolean_type_node");
type = optype;
}
in_type = NULL;
}
else if (*opr == COND_EXPR
|| *opr == VEC_COND_EXPR
|| strncmp (opr->id, "CFN_COND_", 9) == 0)
{
/* Conditions are of the same type as their first alternative. */
snprintf (optype, sizeof (optype), "TREE_TYPE (_o%d[1])", depth);
type = optype;
}
else
{
/* Other operations are of the same type as their first operand. */
snprintf (optype, sizeof (optype), "TREE_TYPE (_o%d[0])", depth);
type = optype;
}
if (!type)
fatal_at (location, "cannot determine type of operand");
fprintf_indent (f, indent, "{\n");
indent += 2;
fprintf_indent (f, indent,
"tree _o%d[%u], _r%d;\n", depth, ops.length (), depth);
char op0type[64];
snprintf (op0type, sizeof (op0type), "TREE_TYPE (_o%d[0])", depth);
for (unsigned i = 0; i < ops.length (); ++i)
{
char dest1[32];
snprintf (dest1, sizeof (dest1), "_o%d[%u]", depth, i);
const char *optype1
= get_operand_type (opr, i, in_type, expr_type,
i == 0 ? NULL : op0type);
ops[i]->gen_transform (f, indent, dest1, gimple, depth + 1, optype1,
cinfo, indexes,
(*opr == COND_EXPR
|| *opr == VEC_COND_EXPR) && i == 0 ? 1 : 2);
}
const char *opr_name;
if (*operation == CONVERT_EXPR)
opr_name = "NOP_EXPR";
else
opr_name = operation->id;
if (gimple)
{
if (*opr == CONVERT_EXPR)
{
fprintf_indent (f, indent,
"if (%s != TREE_TYPE (_o%d[0])\n",
type, depth);
fprintf_indent (f, indent,
" && !useless_type_conversion_p (%s, TREE_TYPE "
"(_o%d[0])))\n",
type, depth);
fprintf_indent (f, indent + 2, "{\n");
indent += 4;
}
/* ??? Building a stmt can fail for various reasons here, seq being
NULL or the stmt referencing SSA names occuring in abnormal PHIs.
So if we fail here we should continue matching other patterns. */
fprintf_indent (f, indent, "gimple_match_op tem_op "
"(res_op->cond.any_else (), %s, %s", opr_name, type);
for (unsigned i = 0; i < ops.length (); ++i)
fprintf (f, ", _o%d[%u]", depth, i);
fprintf (f, ");\n");
fprintf_indent (f, indent, "tem_op.resimplify (lseq, valueize);\n");
fprintf_indent (f, indent,
"_r%d = maybe_push_res_to_seq (&tem_op, %s);\n", depth,
!force_leaf ? "lseq" : "NULL");
fprintf_indent (f, indent,
"if (!_r%d) goto %s;\n",
depth, fail_label);
if (*opr == CONVERT_EXPR)
{
indent -= 4;
fprintf_indent (f, indent, " }\n");
fprintf_indent (f, indent, "else\n");
fprintf_indent (f, indent, " _r%d = _o%d[0];\n", depth, depth);
}
}
else
{
if (*opr == CONVERT_EXPR)
{
fprintf_indent (f, indent, "if (TREE_TYPE (_o%d[0]) != %s)\n",
depth, type);
indent += 2;
}
if (opr->kind == id_base::CODE)
fprintf_indent (f, indent, "_r%d = fold_build%d_loc (loc, %s, %s",
depth, ops.length(), opr_name, type);
else
{
fprintf_indent (f, indent, "{\n");
fprintf_indent (f, indent, " _r%d = maybe_build_call_expr_loc (loc, "
"%s, %s, %d", depth, opr_name, type, ops.length());
}
for (unsigned i = 0; i < ops.length (); ++i)
fprintf (f, ", _o%d[%u]", depth, i);
fprintf (f, ");\n");
if (opr->kind != id_base::CODE)
{
fprintf_indent (f, indent, " if (!_r%d)\n", depth);
fprintf_indent (f, indent, " goto %s;\n", fail_label);
fprintf_indent (f, indent, "}\n");
}
if (*opr == CONVERT_EXPR)
{
indent -= 2;
fprintf_indent (f, indent, "else\n");
fprintf_indent (f, indent, " _r%d = _o%d[0];\n", depth, depth);
}
}
fprintf_indent (f, indent, "%s = _r%d;\n", dest, depth);
indent -= 2;
fprintf_indent (f, indent, "}\n");
}
/* Generate code for a c_expr which is either the expression inside
an if statement or a sequence of statements which computes a
result to be stored to DEST. */
void
c_expr::gen_transform (FILE *f, int indent, const char *dest,
bool, int, const char *, capture_info *,
dt_operand **, int)
{
if (dest && nr_stmts == 1)
fprintf_indent (f, indent, "%s = ", dest);
unsigned stmt_nr = 1;
int prev_line = -1;
for (unsigned i = 0; i < code.length (); ++i)
{
const cpp_token *token = &code[i];
/* We can't recover from all lexing losses but we can roughly restore line
breaks from location info. */
const line_map_ordinary *map;
linemap_resolve_location (line_table, token->src_loc,
LRK_SPELLING_LOCATION, &map);
expanded_location loc = linemap_expand_location (line_table, map,
token->src_loc);
if (prev_line != -1 && loc.line != prev_line)
fputc ('\n', f);
prev_line = loc.line;
/* Replace captures for code-gen. */
if (token->type == CPP_ATSIGN)
{
const cpp_token *n = &code[i+1];
if ((n->type == CPP_NUMBER
|| n->type == CPP_NAME)
&& !(n->flags & PREV_WHITE))
{
if (token->flags & PREV_WHITE)
fputc (' ', f);
const char *id;
if (n->type == CPP_NUMBER)
id = (const char *)n->val.str.text;
else
id = (const char *)CPP_HASHNODE (n->val.node.node)->ident.str;
unsigned *cid = capture_ids->get (id);
if (!cid)
fatal_at (token, "unknown capture id");
fprintf (f, "captures[%u]", *cid);
++i;
continue;
}
}
if (token->flags & PREV_WHITE)
fputc (' ', f);
if (token->type == CPP_NAME)
{
const char *id = (const char *) NODE_NAME (token->val.node.node);
unsigned j;
for (j = 0; j < ids.length (); ++j)
{
if (strcmp (id, ids[j].id) == 0)
{
fprintf (f, "%s", ids[j].oper);
break;
}
}
if (j < ids.length ())
continue;
}
/* Output the token as string. */
char *tk = (char *)cpp_token_as_text (r, token);
fputs (tk, f);
if (token->type == CPP_SEMICOLON)
{
stmt_nr++;
if (dest && stmt_nr == nr_stmts)
fprintf_indent (f, indent, "%s = ", dest);
}
}
fputc ('\n', f);
}
/* Generate transform code for a capture. */
void
capture::gen_transform (FILE *f, int indent, const char *dest, bool gimple,
int depth, const char *in_type, capture_info *cinfo,
dt_operand **indexes, int cond_handling)
{
if (what && is_a<expr *> (what))
{
if (indexes[where] == 0)
{
char buf[20];
snprintf (buf, sizeof (buf), "captures[%u]", where);
what->gen_transform (f, indent, buf, gimple, depth, in_type,
cinfo, NULL);
}
}
/* If in GENERIC some capture is used multiple times, unshare it except
when emitting the last use. */
if (!gimple
&& cinfo->info.exists ()
&& cinfo->info[cinfo->info[where].same_as].result_use_count > 1)
{
fprintf_indent (f, indent, "%s = unshare_expr (captures[%u]);\n",
dest, where);
cinfo->info[cinfo->info[where].same_as].result_use_count--;
}
else
fprintf_indent (f, indent, "%s = captures[%u];\n", dest, where);
/* ??? Stupid tcc_comparison GENERIC trees in COND_EXPRs. Deal
with substituting a capture of that. */
if (gimple
&& cond_handling != 0
&& cinfo->info[where].cond_expr_cond_p)
{
/* If substituting into a cond_expr condition, unshare. */
if (cond_handling == 1)
fprintf_indent (f, indent, "%s = unshare_expr (%s);\n", dest, dest);
/* If substituting elsewhere we might need to decompose it. */
else if (cond_handling == 2)
{
/* ??? Returning false here will also not allow any other patterns
to match unless this generator was split out. */
fprintf_indent (f, indent, "if (COMPARISON_CLASS_P (%s))\n", dest);
fprintf_indent (f, indent, " {\n");
fprintf_indent (f, indent, " if (!seq) return false;\n");
fprintf_indent (f, indent, " %s = gimple_build (seq,"
" TREE_CODE (%s),"
" TREE_TYPE (%s), TREE_OPERAND (%s, 0),"
" TREE_OPERAND (%s, 1));\n",
dest, dest, dest, dest, dest);
fprintf_indent (f, indent, " }\n");
}
}
}
/* Return the name of the operand representing the decision tree node.
Use NAME as space to generate it. */
char *
dt_operand::get_name (char *name)
{
if (! parent)
sprintf (name, "t");
else if (parent->level == 1)
sprintf (name, "_p%u", pos);
else if (parent->type == dt_node::DT_MATCH)
return as_a <dt_operand *> (parent)->get_name (name);
else
sprintf (name, "_q%u%u", parent->level, pos);
return name;
}
/* Fill NAME with the operand name at position POS. */
void
dt_operand::gen_opname (char *name, unsigned pos)
{
if (! parent)
sprintf (name, "_p%u", pos);
else
sprintf (name, "_q%u%u", level, pos);
}
/* Generate matching code for the decision tree operand which is
a predicate. */
unsigned
dt_operand::gen_predicate (FILE *f, int indent, const char *opname, bool gimple)
{
predicate *p = as_a <predicate *> (op);
if (p->p->matchers.exists ())
{
/* If this is a predicate generated from a pattern mangle its
name and pass on the valueize hook. */
if (gimple)
fprintf_indent (f, indent, "if (gimple_%s (%s, valueize))\n",
p->p->id, opname);
else
fprintf_indent (f, indent, "if (tree_%s (%s))\n", p->p->id, opname);
}
else
fprintf_indent (f, indent, "if (%s (%s))\n", p->p->id, opname);
fprintf_indent (f, indent + 2, "{\n");
return 1;
}
/* Generate matching code for the decision tree operand which is
a capture-match. */
unsigned
dt_operand::gen_match_op (FILE *f, int indent, const char *opname, bool)
{
char match_opname[20];
match_dop->get_name (match_opname);
if (value_match)
fprintf_indent (f, indent, "if ((%s == %s && ! TREE_SIDE_EFFECTS (%s)) "
"|| operand_equal_p (%s, %s, 0))\n",
opname, match_opname, opname, opname, match_opname);
else
fprintf_indent (f, indent, "if ((%s == %s && ! TREE_SIDE_EFFECTS (%s)) "
"|| (operand_equal_p (%s, %s, 0) "
"&& types_match (%s, %s)))\n",
opname, match_opname, opname, opname, match_opname,
opname, match_opname);
fprintf_indent (f, indent + 2, "{\n");
return 1;
}
/* Generate GIMPLE matching code for the decision tree operand. */
unsigned
dt_operand::gen_gimple_expr (FILE *f, int indent, int depth)
{
expr *e = static_cast<expr *> (op);
id_base *id = e->operation;
unsigned n_ops = e->ops.length ();
unsigned n_braces = 0;
for (unsigned i = 0; i < n_ops; ++i)
{
char child_opname[20];
gen_opname (child_opname, i);
if (id->kind == id_base::CODE)
{
if (e->is_generic
|| *id == REALPART_EXPR || *id == IMAGPART_EXPR
|| *id == BIT_FIELD_REF || *id == VIEW_CONVERT_EXPR)
{
/* ??? If this is a memory operation we can't (and should not)
match this. The only sensible operand types are
SSA names and invariants. */
if (e->is_generic)
{
char opname[20];
get_name (opname);
fprintf_indent (f, indent,
"tree %s = TREE_OPERAND (%s, %i);\n",
child_opname, opname, i);
}
else
fprintf_indent (f, indent,
"tree %s = TREE_OPERAND "
"(gimple_assign_rhs1 (_a%d), %i);\n",
child_opname, depth, i);
fprintf_indent (f, indent,
"if ((TREE_CODE (%s) == SSA_NAME\n",
child_opname);
fprintf_indent (f, indent,
" || is_gimple_min_invariant (%s)))\n",
child_opname);
fprintf_indent (f, indent,
" {\n");
indent += 4;
n_braces++;
fprintf_indent (f, indent,
"%s = do_valueize (valueize, %s);\n",
child_opname, child_opname);
continue;
}
else
fprintf_indent (f, indent,
"tree %s = gimple_assign_rhs%u (_a%d);\n",
child_opname, i + 1, depth);
}
else
fprintf_indent (f, indent,
"tree %s = gimple_call_arg (_c%d, %u);\n",
child_opname, depth, i);
fprintf_indent (f, indent,
"%s = do_valueize (valueize, %s);\n",
child_opname, child_opname);
}
/* While the toplevel operands are canonicalized by the caller
after valueizing operands of sub-expressions we have to
re-canonicalize operand order. */
int opno = commutative_op (id);
if (opno >= 0)
{
char child_opname0[20], child_opname1[20];
gen_opname (child_opname0, opno);
gen_opname (child_opname1, opno + 1);
fprintf_indent (f, indent,
"if (tree_swap_operands_p (%s, %s))\n",
child_opname0, child_opname1);
fprintf_indent (f, indent,
" std::swap (%s, %s);\n",
child_opname0, child_opname1);
}
return n_braces;
}
/* Generate GENERIC matching code for the decision tree operand. */
unsigned
dt_operand::gen_generic_expr (FILE *f, int indent, const char *opname)
{
expr *e = static_cast<expr *> (op);
unsigned n_ops = e->ops.length ();
for (unsigned i = 0; i < n_ops; ++i)
{
char child_opname[20];
gen_opname (child_opname, i);
if (e->operation->kind == id_base::CODE)
fprintf_indent (f, indent, "tree %s = TREE_OPERAND (%s, %u);\n",
child_opname, opname, i);
else
fprintf_indent (f, indent, "tree %s = CALL_EXPR_ARG (%s, %u);\n",
child_opname, opname, i);
}
return 0;
}
/* Generate matching code for the children of the decision tree node. */
void
dt_node::gen_kids (FILE *f, int indent, bool gimple, int depth)
{
auto_vec<dt_operand *> gimple_exprs;
auto_vec<dt_operand *> generic_exprs;
auto_vec<dt_operand *> fns;
auto_vec<dt_operand *> generic_fns;
auto_vec<dt_operand *> preds;
auto_vec<dt_node *> others;
for (unsigned i = 0; i < kids.length (); ++i)
{
if (kids[i]->type == dt_node::DT_OPERAND)
{
dt_operand *op = as_a<dt_operand *> (kids[i]);
if (expr *e = dyn_cast <expr *> (op->op))
{
if (e->ops.length () == 0
&& (!gimple || !(*e->operation == CONSTRUCTOR)))
generic_exprs.safe_push (op);
else if (e->operation->kind == id_base::FN)
{
if (gimple)
fns.safe_push (op);
else
generic_fns.safe_push (op);
}
else if (e->operation->kind == id_base::PREDICATE)
preds.safe_push (op);
else
{
if (gimple && !e->is_generic)
gimple_exprs.safe_push (op);
else
generic_exprs.safe_push (op);
}
}
else if (op->op->type == operand::OP_PREDICATE)
others.safe_push (kids[i]);
else
gcc_unreachable ();
}
else if (kids[i]->type == dt_node::DT_SIMPLIFY)
others.safe_push (kids[i]);
else if (kids[i]->type == dt_node::DT_MATCH
|| kids[i]->type == dt_node::DT_TRUE)
{
/* A DT_TRUE operand serves as a barrier - generate code now
for what we have collected sofar.
Like DT_TRUE, DT_MATCH serves as a barrier as it can cause
dependent matches to get out-of-order. Generate code now
for what we have collected sofar. */
gen_kids_1 (f, indent, gimple, depth, gimple_exprs, generic_exprs,
fns, generic_fns, preds, others);
/* And output the true operand itself. */
kids[i]->gen (f, indent, gimple, depth);
gimple_exprs.truncate (0);
generic_exprs.truncate (0);
fns.truncate (0);
generic_fns.truncate (0);
preds.truncate (0);
others.truncate (0);
}
else
gcc_unreachable ();
}
/* Generate code for the remains. */
gen_kids_1 (f, indent, gimple, depth, gimple_exprs, generic_exprs,
fns, generic_fns, preds, others);
}
/* Generate matching code for the children of the decision tree node. */
void
dt_node::gen_kids_1 (FILE *f, int indent, bool gimple, int depth,
vec<dt_operand *> gimple_exprs,
vec<dt_operand *> generic_exprs,
vec<dt_operand *> fns,
vec<dt_operand *> generic_fns,
vec<dt_operand *> preds,
vec<dt_node *> others)
{
char buf[128];
char *kid_opname = buf;
unsigned exprs_len = gimple_exprs.length ();
unsigned gexprs_len = generic_exprs.length ();
unsigned fns_len = fns.length ();
unsigned gfns_len = generic_fns.length ();
if (exprs_len || fns_len || gexprs_len || gfns_len)
{
if (exprs_len)
gimple_exprs[0]->get_name (kid_opname);
else if (fns_len)
fns[0]->get_name (kid_opname);
else if (gfns_len)
generic_fns[0]->get_name (kid_opname);
else
generic_exprs[0]->get_name (kid_opname);
fprintf_indent (f, indent, "switch (TREE_CODE (%s))\n", kid_opname);
fprintf_indent (f, indent, " {\n");
indent += 2;
}
if (exprs_len || fns_len)
{
depth++;
fprintf_indent (f, indent,
"case SSA_NAME:\n");
fprintf_indent (f, indent,
" if (gimple *_d%d = get_def (valueize, %s))\n",
depth, kid_opname);
fprintf_indent (f, indent,
" {\n");
indent += 6;
if (exprs_len)
{
fprintf_indent (f, indent,
"if (gassign *_a%d = dyn_cast <gassign *> (_d%d))\n",
depth, depth);
fprintf_indent (f, indent,
" switch (gimple_assign_rhs_code (_a%d))\n",
depth);
indent += 4;
fprintf_indent (f, indent, "{\n");
for (unsigned i = 0; i < exprs_len; ++i)
{
expr *e = as_a <expr *> (gimple_exprs[i]->op);
id_base *op = e->operation;
if (*op == CONVERT_EXPR || *op == NOP_EXPR)
fprintf_indent (f, indent, "CASE_CONVERT:\n");
else
fprintf_indent (f, indent, "case %s:\n", op->id);
fprintf_indent (f, indent, " {\n");
gimple_exprs[i]->gen (f, indent + 4, true, depth);
fprintf_indent (f, indent, " break;\n");
fprintf_indent (f, indent, " }\n");
}
fprintf_indent (f, indent, "default:;\n");
fprintf_indent (f, indent, "}\n");
indent -= 4;
}
if (fns_len)
{
fprintf_indent (f, indent,
"%sif (gcall *_c%d = dyn_cast <gcall *> (_d%d))\n",
exprs_len ? "else " : "", depth, depth);
fprintf_indent (f, indent,
" switch (gimple_call_combined_fn (_c%d))\n",
depth);
indent += 4;
fprintf_indent (f, indent, "{\n");
for (unsigned i = 0; i < fns_len; ++i)
{
expr *e = as_a <expr *>(fns[i]->op);
fprintf_indent (f, indent, "case %s:\n", e->operation->id);
/* We need to be defensive against bogus prototypes allowing
calls with not enough arguments. */
fprintf_indent (f, indent,
" if (gimple_call_num_args (_c%d) == %d)\n",
depth, e->ops.length ());
fprintf_indent (f, indent, " {\n");
fns[i]->gen (f, indent + 6, true, depth);
fprintf_indent (f, indent, " }\n");
fprintf_indent (f, indent, " break;\n");
}
fprintf_indent (f, indent, "default:;\n");
fprintf_indent (f, indent, "}\n");
indent -= 4;
}
indent -= 6;
depth--;
fprintf_indent (f, indent, " }\n");
/* See if there is SSA_NAME among generic_exprs and if yes, emit it
here rather than in the next loop. */
for (unsigned i = 0; i < generic_exprs.length (); ++i)
{
expr *e = as_a <expr *>(generic_exprs[i]->op);
id_base *op = e->operation;
if (*op == SSA_NAME && (exprs_len || fns_len))
{
fprintf_indent (f, indent + 4, "{\n");
generic_exprs[i]->gen (f, indent + 6, gimple, depth);
fprintf_indent (f, indent + 4, "}\n");
}
}
fprintf_indent (f, indent, " break;\n");
}
for (unsigned i = 0; i < generic_exprs.length (); ++i)
{
expr *e = as_a <expr *>(generic_exprs[i]->op);
id_base *op = e->operation;
if (*op == CONVERT_EXPR || *op == NOP_EXPR)
fprintf_indent (f, indent, "CASE_CONVERT:\n");
else if (*op == SSA_NAME && (exprs_len || fns_len))
/* Already handled above. */
continue;
else
fprintf_indent (f, indent, "case %s:\n", op->id);
fprintf_indent (f, indent, " {\n");
generic_exprs[i]->gen (f, indent + 4, gimple, depth);
fprintf_indent (f, indent, " break;\n");
fprintf_indent (f, indent, " }\n");
}
if (gfns_len)
{
fprintf_indent (f, indent,
"case CALL_EXPR:\n");
fprintf_indent (f, indent,
" switch (get_call_combined_fn (%s))\n",
kid_opname);
fprintf_indent (f, indent,
" {\n");
indent += 4;
for (unsigned j = 0; j < generic_fns.length (); ++j)
{
expr *e = as_a <expr *>(generic_fns[j]->op);
gcc_assert (e->operation->kind == id_base::FN);
fprintf_indent (f, indent, "case %s:\n", e->operation->id);
fprintf_indent (f, indent, " if (call_expr_nargs (%s) == %d)\n"
" {\n", kid_opname, e->ops.length ());
generic_fns[j]->gen (f, indent + 6, false, depth);
fprintf_indent (f, indent, " }\n"
" break;\n");
}
fprintf_indent (f, indent, "default:;\n");
indent -= 4;
fprintf_indent (f, indent, " }\n");
fprintf_indent (f, indent, " break;\n");
}
/* Close switch (TREE_CODE ()). */
if (exprs_len || fns_len || gexprs_len || gfns_len)
{
indent -= 4;
fprintf_indent (f, indent, " default:;\n");
fprintf_indent (f, indent, " }\n");
}
for (unsigned i = 0; i < preds.length (); ++i)
{
expr *e = as_a <expr *> (preds[i]->op);
predicate_id *p = as_a <predicate_id *> (e->operation);
preds[i]->get_name (kid_opname);
fprintf_indent (f, indent, "{\n");
indent += 2;
fprintf_indent (f, indent, "tree %s_pops[%d];\n", kid_opname, p->nargs);
fprintf_indent (f, indent, "if (%s_%s (%s, %s_pops%s))\n",
gimple ? "gimple" : "tree",
p->id, kid_opname, kid_opname,
gimple ? ", valueize" : "");
fprintf_indent (f, indent, " {\n");
for (int j = 0; j < p->nargs; ++j)
{
char child_opname[20];
preds[i]->gen_opname (child_opname, j);
fprintf_indent (f, indent + 4, "tree %s = %s_pops[%d];\n",
child_opname, kid_opname, j);
}
preds[i]->gen_kids (f, indent + 4, gimple, depth);
fprintf (f, "}\n");
indent -= 2;
fprintf_indent (f, indent, "}\n");
}
for (unsigned i = 0; i < others.length (); ++i)
others[i]->gen (f, indent, gimple, depth);
}
/* Generate matching code for the decision tree operand. */
void
dt_operand::gen (FILE *f, int indent, bool gimple, int depth)
{
char opname[20];
get_name (opname);
unsigned n_braces = 0;
if (type == DT_OPERAND)
switch (op->type)
{
case operand::OP_PREDICATE:
n_braces = gen_predicate (f, indent, opname, gimple);
break;
case operand::OP_EXPR:
if (gimple)
n_braces = gen_gimple_expr (f, indent, depth);
else
n_braces = gen_generic_expr (f, indent, opname);
break;
default:
gcc_unreachable ();
}
else if (type == DT_TRUE)
;
else if (type == DT_MATCH)
n_braces = gen_match_op (f, indent, opname, gimple);
else
gcc_unreachable ();
indent += 4 * n_braces;
gen_kids (f, indent, gimple, depth);
for (unsigned i = 0; i < n_braces; ++i)
{
indent -= 4;
if (indent < 0)
indent = 0;
fprintf_indent (f, indent, " }\n");
}
}
/* Generate code for the '(if ...)', '(with ..)' and actual transform
step of a '(simplify ...)' or '(match ...)'. This handles everything
that is not part of the decision tree (simplify->match).
Main recursive worker. */
void
dt_simplify::gen_1 (FILE *f, int indent, bool gimple, operand *result)
{
if (result)
{
if (with_expr *w = dyn_cast <with_expr *> (result))
{
fprintf_indent (f, indent, "{\n");
indent += 4;
output_line_directive (f, w->location);
w->with->gen_transform (f, indent, NULL, true, 1, "type", NULL);
gen_1 (f, indent, gimple, w->subexpr);
indent -= 4;
fprintf_indent (f, indent, "}\n");
return;
}
else if (if_expr *ife = dyn_cast <if_expr *> (result))
{
output_line_directive (f, ife->location);
fprintf_indent (f, indent, "if (");
ife->cond->gen_transform (f, indent, NULL, true, 1, "type", NULL);
fprintf (f, ")\n");
fprintf_indent (f, indent + 2, "{\n");
indent += 4;
gen_1 (f, indent, gimple, ife->trueexpr);
indent -= 4;
fprintf_indent (f, indent + 2, "}\n");
if (ife->falseexpr)
{
fprintf_indent (f, indent, "else\n");
fprintf_indent (f, indent + 2, "{\n");
indent += 4;
gen_1 (f, indent, gimple, ife->falseexpr);
indent -= 4;
fprintf_indent (f, indent + 2, "}\n");
}
return;
}
}
static unsigned fail_label_cnt;
char local_fail_label[256];
snprintf (local_fail_label, 256, "next_after_fail%u", ++fail_label_cnt);
fail_label = local_fail_label;
/* Analyze captures and perform early-outs on the incoming arguments
that cover cases we cannot handle. */
capture_info cinfo (s, result, gimple);
if (s->kind == simplify::SIMPLIFY)
{
if (!gimple)
{
for (unsigned i = 0; i < as_a <expr *> (s->match)->ops.length (); ++i)
if (cinfo.force_no_side_effects & (1 << i))
{
fprintf_indent (f, indent,
"if (TREE_SIDE_EFFECTS (_p%d)) goto %s;\n",
i, fail_label);
if (verbose >= 1)
warning_at (as_a <expr *> (s->match)->ops[i]->location,
"forcing toplevel operand to have no "
"side-effects");
}
for (int i = 0; i <= s->capture_max; ++i)
if (cinfo.info[i].cse_p)
;
else if (cinfo.info[i].force_no_side_effects_p
&& (cinfo.info[i].toplevel_msk
& cinfo.force_no_side_effects) == 0)
{
fprintf_indent (f, indent,
"if (TREE_SIDE_EFFECTS (captures[%d])) "
"goto %s;\n", i, fail_label);
if (verbose >= 1)
warning_at (cinfo.info[i].c->location,
"forcing captured operand to have no "
"side-effects");
}
else if ((cinfo.info[i].toplevel_msk
& cinfo.force_no_side_effects) != 0)
/* Mark capture as having no side-effects if we had to verify
that via forced toplevel operand checks. */
cinfo.info[i].force_no_side_effects_p = true;
}
if (gimple)
{
/* Force single-use restriction by only allowing simple
results via setting seq to NULL. */
fprintf_indent (f, indent, "gimple_seq *lseq = seq;\n");
bool first_p = true;
for (int i = 0; i <= s->capture_max; ++i)
if (cinfo.info[i].force_single_use)
{
if (first_p)
{
fprintf_indent (f, indent, "if (lseq\n");
fprintf_indent (f, indent, " && (");
first_p = false;
}
else
{
fprintf (f, "\n");
fprintf_indent (f, indent, " || ");
}
fprintf (f, "!single_use (captures[%d])", i);
}
if (!first_p)
{
fprintf (f, "))\n");
fprintf_indent (f, indent, " lseq = NULL;\n");
}
}
}
if (s->kind == simplify::SIMPLIFY)
fprintf_indent (f, indent, "if (__builtin_expect (!dbg_cnt (match), 0)) goto %s;\n", fail_label);
fprintf_indent (f, indent, "if (__builtin_expect (dump_file && (dump_flags & TDF_FOLDING), 0)) "
"fprintf (dump_file, \"%s ",
s->kind == simplify::SIMPLIFY
? "Applying pattern" : "Matching expression");
fprintf (f, "%%s:%%d, %%s:%%d\\n\", ");
output_line_directive (f,
result ? result->location : s->match->location, true,
true);
fprintf (f, ", __FILE__, __LINE__);\n");
fprintf_indent (f, indent, "{\n");
indent += 2;
if (!result)
{
/* If there is no result then this is a predicate implementation. */
fprintf_indent (f, indent, "return true;\n");
}
else if (gimple)
{
/* For GIMPLE simply drop NON_LVALUE_EXPR (which only appears
in outermost position). */
if (result->type == operand::OP_EXPR
&& *as_a <expr *> (result)->operation == NON_LVALUE_EXPR)
result = as_a <expr *> (result)->ops[0];
if (result->type == operand::OP_EXPR)
{
expr *e = as_a <expr *> (result);
id_base *opr = e->operation;
bool is_predicate = false;
/* When we delay operator substituting during lowering of fors we
make sure that for code-gen purposes the effects of each substitute
are the same. Thus just look at that. */
if (user_id *uid = dyn_cast <user_id *> (opr))
opr = uid->substitutes[0];
else if (is_a <predicate_id *> (opr))
is_predicate = true;
if (!is_predicate)
fprintf_indent (f, indent, "res_op->set_op (%s, type, %d);\n",
*e->operation == CONVERT_EXPR
? "NOP_EXPR" : e->operation->id,
e->ops.length ());
for (unsigned j = 0; j < e->ops.length (); ++j)
{
char dest[32];
if (is_predicate)
snprintf (dest, sizeof (dest), "res_ops[%d]", j);
else
snprintf (dest, sizeof (dest), "res_op->ops[%d]", j);
const char *optype
= get_operand_type (opr, j,
"type", e->expr_type,
j == 0 ? NULL
: "TREE_TYPE (res_op->ops[0])");
/* We need to expand GENERIC conditions we captured from
COND_EXPRs and we need to unshare them when substituting
into COND_EXPRs. */
int cond_handling = 0;
if (!is_predicate)
cond_handling = ((*opr == COND_EXPR
|| *opr == VEC_COND_EXPR) && j == 0) ? 1 : 2;
e->ops[j]->gen_transform (f, indent, dest, true, 1, optype,
&cinfo, indexes, cond_handling);
}
/* Re-fold the toplevel result. It's basically an embedded
gimple_build w/o actually building the stmt. */
if (!is_predicate)
{
fprintf_indent (f, indent,
"res_op->resimplify (lseq, valueize);\n");
if (e->force_leaf)
fprintf_indent (f, indent,
"if (!maybe_push_res_to_seq (res_op, NULL)) "
"goto %s;\n", fail_label);
}
}
else if (result->type == operand::OP_CAPTURE
|| result->type == operand::OP_C_EXPR)
{
fprintf_indent (f, indent, "tree tem;\n");
result->gen_transform (f, indent, "tem", true, 1, "type",
&cinfo, indexes);
fprintf_indent (f, indent, "res_op->set_value (tem);\n");
if (is_a <capture *> (result)
&& cinfo.info[as_a <capture *> (result)->where].cond_expr_cond_p)
{
/* ??? Stupid tcc_comparison GENERIC trees in COND_EXPRs. Deal
with substituting a capture of that. */
fprintf_indent (f, indent,
"if (COMPARISON_CLASS_P (tem))\n");
fprintf_indent (f, indent,
" {\n");
fprintf_indent (f, indent,
" res_op->ops[0] = TREE_OPERAND (tem, 0);\n");
fprintf_indent (f, indent,
" res_op->ops[1] = TREE_OPERAND (tem, 1);\n");
fprintf_indent (f, indent,
" }\n");
}
}
else
gcc_unreachable ();
fprintf_indent (f, indent, "return true;\n");
}
else /* GENERIC */
{
bool is_predicate = false;
if (result->type == operand::OP_EXPR)
{
expr *e = as_a <expr *> (result);
id_base *opr = e->operation;
/* When we delay operator substituting during lowering of fors we
make sure that for code-gen purposes the effects of each substitute
are the same. Thus just look at that. */
if (user_id *uid = dyn_cast <user_id *> (opr))
opr = uid->substitutes[0];
else if (is_a <predicate_id *> (opr))
is_predicate = true;
/* Search for captures used multiple times in the result expression
and wrap them in a SAVE_EXPR. Allow as many uses as in the
original expression. */
if (!is_predicate)
for (int i = 0; i < s->capture_max + 1; ++i)
{
if (cinfo.info[i].same_as != (unsigned)i
|| cinfo.info[i].cse_p)
continue;
if (cinfo.info[i].result_use_count
> cinfo.info[i].match_use_count)
fprintf_indent (f, indent,
"if (! tree_invariant_p (captures[%d])) "
"goto %s;\n", i, fail_label);
}
for (unsigned j = 0; j < e->ops.length (); ++j)
{
char dest[32];
if (is_predicate)
snprintf (dest, sizeof (dest), "res_ops[%d]", j);
else
{
fprintf_indent (f, indent, "tree res_op%d;\n", j);
snprintf (dest, sizeof (dest), "res_op%d", j);
}
const char *optype
= get_operand_type (opr, j,
"type", e->expr_type,
j == 0
? NULL : "TREE_TYPE (res_op0)");
e->ops[j]->gen_transform (f, indent, dest, false, 1, optype,
&cinfo, indexes);
}
if (is_predicate)
fprintf_indent (f, indent, "return true;\n");
else
{
fprintf_indent (f, indent, "tree _r;\n");
/* Re-fold the toplevel result. Use non_lvalue to
build NON_LVALUE_EXPRs so they get properly
ignored when in GIMPLE form. */
if (*opr == NON_LVALUE_EXPR)
fprintf_indent (f, indent,
"_r = non_lvalue_loc (loc, res_op0);\n");
else
{
if (is_a <operator_id *> (opr))
fprintf_indent (f, indent,
"_r = fold_build%d_loc (loc, %s, type",
e->ops.length (),
*e->operation == CONVERT_EXPR
? "NOP_EXPR" : e->operation->id);
else
fprintf_indent (f, indent,
"_r = maybe_build_call_expr_loc (loc, "
"%s, type, %d", e->operation->id,
e->ops.length());
for (unsigned j = 0; j < e->ops.length (); ++j)
fprintf (f, ", res_op%d", j);
fprintf (f, ");\n");
if (!is_a <operator_id *> (opr))
{
fprintf_indent (f, indent, "if (!_r)\n");
fprintf_indent (f, indent, " goto %s;\n", fail_label);
}
}
}
}
else if (result->type == operand::OP_CAPTURE
|| result->type == operand::OP_C_EXPR)
{
fprintf_indent (f, indent, "tree _r;\n");
result->gen_transform (f, indent, "_r", false, 1, "type",
&cinfo, indexes);
}
else
gcc_unreachable ();
if (!is_predicate)
{
/* Search for captures not used in the result expression and dependent
on TREE_SIDE_EFFECTS emit omit_one_operand. */
for (int i = 0; i < s->capture_max + 1; ++i)
{
if (cinfo.info[i].same_as != (unsigned)i)
continue;
if (!cinfo.info[i].force_no_side_effects_p
&& !cinfo.info[i].expr_p
&& cinfo.info[i].result_use_count == 0)
{
fprintf_indent (f, indent,
"if (TREE_SIDE_EFFECTS (captures[%d]))\n",
i);
fprintf_indent (f, indent + 2,
"_r = build2_loc (loc, COMPOUND_EXPR, type, "
"fold_ignored_result (captures[%d]), _r);\n",
i);
}
}
fprintf_indent (f, indent, "return _r;\n");
}
}
indent -= 2;
fprintf_indent (f, indent, "}\n");
fprintf (f, "%s:;\n", fail_label);
fail_label = NULL;
}
/* Generate code for the '(if ...)', '(with ..)' and actual transform
step of a '(simplify ...)' or '(match ...)'. This handles everything
that is not part of the decision tree (simplify->match). */
void
dt_simplify::gen (FILE *f, int indent, bool gimple, int depth ATTRIBUTE_UNUSED)
{
fprintf_indent (f, indent, "{\n");
indent += 2;
output_line_directive (f,
s->result ? s->result->location : s->match->location);
if (s->capture_max >= 0)
{
char opname[20];
fprintf_indent (f, indent, "tree captures[%u] ATTRIBUTE_UNUSED = { %s",
s->capture_max + 1, indexes[0]->get_name (opname));
for (int i = 1; i <= s->capture_max; ++i)
{
if (!indexes[i])
break;
fprintf (f, ", %s", indexes[i]->get_name (opname));
}
fprintf (f, " };\n");
}
/* If we have a split-out function for the actual transform, call it. */
if (info && info->fname)
{
if (gimple)
{
fprintf_indent (f, indent, "if (%s (res_op, seq, "
"valueize, type, captures", info->fname);
for (unsigned i = 0; i < s->for_subst_vec.length (); ++i)
if (s->for_subst_vec[i].first->used)
fprintf (f, ", %s", s->for_subst_vec[i].second->id);
fprintf (f, "))\n");
fprintf_indent (f, indent, " return true;\n");
}
else
{
fprintf_indent (f, indent, "tree res = %s (loc, type",
info->fname);
for (unsigned i = 0; i < as_a <expr *> (s->match)->ops.length (); ++i)
fprintf (f, ", _p%d", i);
fprintf (f, ", captures");
for (unsigned i = 0; i < s->for_subst_vec.length (); ++i)
{
if (s->for_subst_vec[i].first->used)
fprintf (f, ", %s", s->for_subst_vec[i].second->id);
}
fprintf (f, ");\n");
fprintf_indent (f, indent, "if (res) return res;\n");
}
}
else
{
for (unsigned i = 0; i < s->for_subst_vec.length (); ++i)
{
if (! s->for_subst_vec[i].first->used)
continue;
if (is_a <operator_id *> (s->for_subst_vec[i].second))
fprintf_indent (f, indent, "const enum tree_code %s = %s;\n",
s->for_subst_vec[i].first->id,
s->for_subst_vec[i].second->id);
else if (is_a <fn_id *> (s->for_subst_vec[i].second))
fprintf_indent (f, indent, "const combined_fn %s = %s;\n",
s->for_subst_vec[i].first->id,
s->for_subst_vec[i].second->id);
else
gcc_unreachable ();
}
gen_1 (f, indent, gimple, s->result);
}
indent -= 2;
fprintf_indent (f, indent, "}\n");
}
/* Hash function for finding equivalent transforms. */
hashval_t
sinfo_hashmap_traits::hash (const key_type &v)
{
/* Only bother to compare those originating from the same source pattern. */
return v->s->result->location;
}
/* Compare function for finding equivalent transforms. */
static bool
compare_op (operand *o1, simplify *s1, operand *o2, simplify *s2)
{
if (o1->type != o2->type)
return false;
switch (o1->type)
{
case operand::OP_IF:
{
if_expr *if1 = as_a <if_expr *> (o1);
if_expr *if2 = as_a <if_expr *> (o2);
/* ??? Properly compare c-exprs. */
if (if1->cond != if2->cond)
return false;
if (!compare_op (if1->trueexpr, s1, if2->trueexpr, s2))
return false;
if (if1->falseexpr != if2->falseexpr
|| (if1->falseexpr
&& !compare_op (if1->falseexpr, s1, if2->falseexpr, s2)))
return false;
return true;
}
case operand::OP_WITH:
{
with_expr *with1 = as_a <with_expr *> (o1);
with_expr *with2 = as_a <with_expr *> (o2);
if (with1->with != with2->with)
return false;
return compare_op (with1->subexpr, s1, with2->subexpr, s2);
}
default:;
}
/* We've hit a result. Time to compare capture-infos - this is required
in addition to the conservative pointer-equivalency of the result IL. */
capture_info cinfo1 (s1, o1, true);
capture_info cinfo2 (s2, o2, true);
if (cinfo1.force_no_side_effects != cinfo2.force_no_side_effects
|| cinfo1.info.length () != cinfo2.info.length ())
return false;
for (unsigned i = 0; i < cinfo1.info.length (); ++i)
{
if (cinfo1.info[i].expr_p != cinfo2.info[i].expr_p
|| cinfo1.info[i].cse_p != cinfo2.info[i].cse_p
|| (cinfo1.info[i].force_no_side_effects_p
!= cinfo2.info[i].force_no_side_effects_p)
|| cinfo1.info[i].force_single_use != cinfo2.info[i].force_single_use
|| cinfo1.info[i].cond_expr_cond_p != cinfo2.info[i].cond_expr_cond_p
/* toplevel_msk is an optimization */
|| cinfo1.info[i].result_use_count != cinfo2.info[i].result_use_count
|| cinfo1.info[i].same_as != cinfo2.info[i].same_as
/* the pointer back to the capture is for diagnostics only */)
return false;
}
/* ??? Deep-compare the actual result. */
return o1 == o2;
}
bool
sinfo_hashmap_traits::equal_keys (const key_type &v,
const key_type &candidate)
{
return compare_op (v->s->result, v->s, candidate->s->result, candidate->s);
}
/* Main entry to generate code for matching GIMPLE IL off the decision
tree. */
void
decision_tree::gen (FILE *f, bool gimple)
{
sinfo_map_t si;
root->analyze (si);
fprintf (stderr, "%s decision tree has %u leafs, maximum depth %u and "
"a total number of %u nodes\n",
gimple ? "GIMPLE" : "GENERIC",
root->num_leafs, root->max_level, root->total_size);
/* First split out the transform part of equal leafs. */
unsigned rcnt = 0;
unsigned fcnt = 1;
for (sinfo_map_t::iterator iter = si.begin ();
iter != si.end (); ++iter)
{
sinfo *s = (*iter).second;
/* Do not split out single uses. */
if (s->cnt <= 1)
continue;
rcnt += s->cnt - 1;
if (verbose >= 1)
{
fprintf (stderr, "found %u uses of", s->cnt);
output_line_directive (stderr, s->s->s->result->location);
}
/* Generate a split out function with the leaf transform code. */
s->fname = xasprintf ("%s_simplify_%u", gimple ? "gimple" : "generic",
fcnt++);
if (gimple)
fprintf (f, "\nstatic bool\n"
"%s (gimple_match_op *res_op, gimple_seq *seq,\n"
" tree (*valueize)(tree) ATTRIBUTE_UNUSED,\n"
" const tree ARG_UNUSED (type), tree *ARG_UNUSED "
"(captures)\n",
s->fname);
else
{
fprintf (f, "\nstatic tree\n"
"%s (location_t ARG_UNUSED (loc), const tree ARG_UNUSED (type),\n",
(*iter).second->fname);
for (unsigned i = 0;
i < as_a <expr *>(s->s->s->match)->ops.length (); ++i)
fprintf (f, " tree ARG_UNUSED (_p%d),", i);
fprintf (f, " tree *captures\n");
}
for (unsigned i = 0; i < s->s->s->for_subst_vec.length (); ++i)
{
if (! s->s->s->for_subst_vec[i].first->used)
continue;
if (is_a <operator_id *> (s->s->s->for_subst_vec[i].second))
fprintf (f, ", const enum tree_code ARG_UNUSED (%s)",
s->s->s->for_subst_vec[i].first->id);
else if (is_a <fn_id *> (s->s->s->for_subst_vec[i].second))
fprintf (f, ", const combined_fn ARG_UNUSED (%s)",
s->s->s->for_subst_vec[i].first->id);
}
fprintf (f, ")\n{\n");
s->s->gen_1 (f, 2, gimple, s->s->s->result);
if (gimple)
fprintf (f, " return false;\n");
else
fprintf (f, " return NULL_TREE;\n");
fprintf (f, "}\n");
}
fprintf (stderr, "removed %u duplicate tails\n", rcnt);
for (unsigned n = 1; n <= 5; ++n)
{
bool has_kids_p = false;
/* First generate split-out functions. */
for (unsigned j = 0; j < root->kids.length (); j++)
{
dt_operand *dop = static_cast<dt_operand *>(root->kids[j]);
expr *e = static_cast<expr *>(dop->op);
if (e->ops.length () != n
/* Builtin simplifications are somewhat premature on
GENERIC. The following drops patterns with outermost
calls. It's easy to emit overloads for function code
though if necessary. */
|| (!gimple
&& e->operation->kind != id_base::CODE))
continue;
if (gimple)
fprintf (f, "\nstatic bool\n"
"gimple_simplify_%s (gimple_match_op *res_op,"
" gimple_seq *seq,\n"
" tree (*valueize)(tree) "
"ATTRIBUTE_UNUSED,\n"
" code_helper ARG_UNUSED (code), tree "
"ARG_UNUSED (type)\n",
e->operation->id);
else
fprintf (f, "\nstatic tree\n"
"generic_simplify_%s (location_t ARG_UNUSED (loc), enum "
"tree_code ARG_UNUSED (code), const tree ARG_UNUSED (type)",
e->operation->id);
for (unsigned i = 0; i < n; ++i)
fprintf (f, ", tree _p%d", i);
fprintf (f, ")\n");
fprintf (f, "{\n");
dop->gen_kids (f, 2, gimple, 0);
if (gimple)
fprintf (f, " return false;\n");
else
fprintf (f, " return NULL_TREE;\n");
fprintf (f, "}\n");
has_kids_p = true;
}
/* If this main entry has no children, avoid generating code
with compiler warnings, by generating a simple stub. */
if (! has_kids_p)
{
if (gimple)
fprintf (f, "\nstatic bool\n"
"gimple_simplify (gimple_match_op*, gimple_seq*,\n"
" tree (*)(tree), code_helper,\n"
" const tree");
else
fprintf (f, "\ntree\n"
"generic_simplify (location_t, enum tree_code,\n"
" const tree");
for (unsigned i = 0; i < n; ++i)
fprintf (f, ", tree");
fprintf (f, ")\n");
fprintf (f, "{\n");
if (gimple)
fprintf (f, " return false;\n");
else
fprintf (f, " return NULL_TREE;\n");
fprintf (f, "}\n");
continue;
}
/* Then generate the main entry with the outermost switch and
tail-calls to the split-out functions. */
if (gimple)
fprintf (f, "\nstatic bool\n"
"gimple_simplify (gimple_match_op *res_op, gimple_seq *seq,\n"
" tree (*valueize)(tree) ATTRIBUTE_UNUSED,\n"
" code_helper code, const tree type");
else
fprintf (f, "\ntree\n"
"generic_simplify (location_t loc, enum tree_code code, "
"const tree type ATTRIBUTE_UNUSED");
for (unsigned i = 0; i < n; ++i)
fprintf (f, ", tree _p%d", i);
fprintf (f, ")\n");
fprintf (f, "{\n");
if (gimple)
fprintf (f, " switch (code.get_rep())\n"
" {\n");
else
fprintf (f, " switch (code)\n"
" {\n");
for (unsigned i = 0; i < root->kids.length (); i++)
{
dt_operand *dop = static_cast<dt_operand *>(root->kids[i]);
expr *e = static_cast<expr *>(dop->op);
if (e->ops.length () != n
/* Builtin simplifications are somewhat premature on
GENERIC. The following drops patterns with outermost
calls. It's easy to emit overloads for function code
though if necessary. */
|| (!gimple
&& e->operation->kind != id_base::CODE))
continue;
if (*e->operation == CONVERT_EXPR
|| *e->operation == NOP_EXPR)
fprintf (f, " CASE_CONVERT:\n");
else
fprintf (f, " case %s%s:\n",
is_a <fn_id *> (e->operation) ? "-" : "",
e->operation->id);
if (gimple)
fprintf (f, " return gimple_simplify_%s (res_op, "
"seq, valueize, code, type", e->operation->id);
else
fprintf (f, " return generic_simplify_%s (loc, code, type",
e->operation->id);
for (unsigned j = 0; j < n; ++j)
fprintf (f, ", _p%d", j);
fprintf (f, ");\n");
}
fprintf (f, " default:;\n"
" }\n");
if (gimple)
fprintf (f, " return false;\n");
else
fprintf (f, " return NULL_TREE;\n");
fprintf (f, "}\n");
}
}
/* Output code to implement the predicate P from the decision tree DT. */
void
write_predicate (FILE *f, predicate_id *p, decision_tree &dt, bool gimple)
{
fprintf (f, "\nbool\n"
"%s%s (tree t%s%s)\n"
"{\n", gimple ? "gimple_" : "tree_", p->id,
p->nargs > 0 ? ", tree *res_ops" : "",
gimple ? ", tree (*valueize)(tree) ATTRIBUTE_UNUSED" : "");
/* Conveniently make 'type' available. */
fprintf_indent (f, 2, "const tree type = TREE_TYPE (t);\n");
if (!gimple)
fprintf_indent (f, 2, "if (TREE_SIDE_EFFECTS (t)) return false;\n");
dt.root->gen_kids (f, 2, gimple, 0);
fprintf_indent (f, 2, "return false;\n"
"}\n");
}
/* Write the common header for the GIMPLE/GENERIC IL matching routines. */
static void
write_header (FILE *f, const char *head)
{
fprintf (f, "/* Generated automatically by the program `genmatch' from\n");
fprintf (f, " a IL pattern matching and simplification description. */\n");
/* Include the header instead of writing it awkwardly quoted here. */
fprintf (f, "\n#include \"%s\"\n", head);
}
/* AST parsing. */
class parser
{
public:
parser (cpp_reader *, bool gimple);
private:
const cpp_token *next ();
const cpp_token *peek (unsigned = 1);
const cpp_token *peek_ident (const char * = NULL, unsigned = 1);
const cpp_token *expect (enum cpp_ttype);
const cpp_token *eat_token (enum cpp_ttype);
const char *get_string ();
const char *get_ident ();
const cpp_token *eat_ident (const char *);
const char *get_number ();
unsigned get_internal_capture_id ();
id_base *parse_operation (unsigned char &);
operand *parse_capture (operand *, bool);
operand *parse_expr ();
c_expr *parse_c_expr (cpp_ttype);
operand *parse_op ();
void record_operlist (location_t, user_id *);
void parse_pattern ();
operand *parse_result (operand *, predicate_id *);
void push_simplify (simplify::simplify_kind,
vec<simplify *>&, operand *, operand *);
void parse_simplify (simplify::simplify_kind,
vec<simplify *>&, predicate_id *, operand *);
void parse_for (location_t);
void parse_if (location_t);
void parse_predicates (location_t);
void parse_operator_list (location_t);
void finish_match_operand (operand *);
cpp_reader *r;
bool gimple;
vec<c_expr *> active_ifs;
vec<vec<user_id *> > active_fors;
hash_set<user_id *> *oper_lists_set;
vec<user_id *> oper_lists;
cid_map_t *capture_ids;
unsigned last_id;
public:
vec<simplify *> simplifiers;
vec<predicate_id *> user_predicates;
bool parsing_match_operand;
};
/* Lexing helpers. */
/* Read the next non-whitespace token from R. */
const cpp_token *
parser::next ()
{
const cpp_token *token;
do
{
token = cpp_get_token (r);
}
while (token->type == CPP_PADDING);
return token;
}
/* Peek at the next non-whitespace token from R. */
const cpp_token *
parser::peek (unsigned num)
{
const cpp_token *token;
unsigned i = 0;
do
{
token = cpp_peek_token (r, i++);
}
while (token->type == CPP_PADDING
|| (--num > 0));
/* If we peek at EOF this is a fatal error as it leaves the
cpp_reader in unusable state. Assume we really wanted a
token and thus this EOF is unexpected. */
if (token->type == CPP_EOF)
fatal_at (token, "unexpected end of file");
return token;
}
/* Peek at the next identifier token (or return NULL if the next
token is not an identifier or equal to ID if supplied). */
const cpp_token *
parser::peek_ident (const char *id, unsigned num)
{
const cpp_token *token = peek (num);
if (token->type != CPP_NAME)
return 0;
if (id == 0)
return token;
const char *t = (const char *) CPP_HASHNODE (token->val.node.node)->ident.str;
if (strcmp (id, t) == 0)
return token;
return 0;
}
/* Read the next token from R and assert it is of type TK. */
const cpp_token *
parser::expect (enum cpp_ttype tk)
{
const cpp_token *token = next ();
if (token->type != tk)
fatal_at (token, "expected %s, got %s",
cpp_type2name (tk, 0), cpp_type2name (token->type, 0));
return token;
}
/* Consume the next token from R and assert it is of type TK. */
const cpp_token *
parser::eat_token (enum cpp_ttype tk)
{
return expect (tk);
}
/* Read the next token from R and assert it is of type CPP_STRING and
return its value. */
const char *
parser::get_string ()
{
const cpp_token *token = expect (CPP_STRING);
return (const char *)token->val.str.text;
}
/* Read the next token from R and assert it is of type CPP_NAME and
return its value. */
const char *
parser::get_ident ()
{
const cpp_token *token = expect (CPP_NAME);
return (const char *)CPP_HASHNODE (token->val.node.node)->ident.str;
}
/* Eat an identifier token with value S from R. */
const cpp_token *
parser::eat_ident (const char *s)
{
const cpp_token *token = peek ();
const char *t = get_ident ();
if (strcmp (s, t) != 0)
fatal_at (token, "expected '%s' got '%s'\n", s, t);
return token;
}
/* Read the next token from R and assert it is of type CPP_NUMBER and
return its value. */
const char *
parser::get_number ()
{
const cpp_token *token = expect (CPP_NUMBER);
return (const char *)token->val.str.text;
}
/* Return a capture ID that can be used internally. */
unsigned
parser::get_internal_capture_id ()
{
unsigned newid = capture_ids->elements ();
/* Big enough for a 32-bit UINT_MAX plus prefix. */
char id[13];
bool existed;
snprintf (id, sizeof (id), "__%u", newid);
capture_ids->get_or_insert (xstrdup (id), &existed);
if (existed)
fatal ("reserved capture id '%s' already used", id);
return newid;
}
/* Record an operator-list use for transparent for handling. */
void
parser::record_operlist (location_t loc, user_id *p)
{
if (!oper_lists_set->add (p))
{
if (!oper_lists.is_empty ()
&& oper_lists[0]->substitutes.length () != p->substitutes.length ())
fatal_at (loc, "User-defined operator list does not have the "
"same number of entries as others used in the pattern");
oper_lists.safe_push (p);
}
}
/* Parse the operator ID, special-casing convert?, convert1? and
convert2? */
id_base *
parser::parse_operation (unsigned char &opt_grp)
{
const cpp_token *id_tok = peek ();
char *alt_id = NULL;
const char *id = get_ident ();
const cpp_token *token = peek ();
opt_grp = 0;
if (token->type == CPP_QUERY
&& !(token->flags & PREV_WHITE))
{
if (!parsing_match_operand)
fatal_at (id_tok, "conditional convert can only be used in "
"match expression");
if (ISDIGIT (id[strlen (id) - 1]))
{
opt_grp = id[strlen (id) - 1] - '0' + 1;
alt_id = xstrdup (id);
alt_id[strlen (id) - 1] = '\0';
if (opt_grp == 1)
fatal_at (id_tok, "use '%s?' here", alt_id);
}
else
opt_grp = 1;
eat_token (CPP_QUERY);
}
id_base *op = get_operator (alt_id ? alt_id : id);
if (!op)
fatal_at (id_tok, "unknown operator %s", alt_id ? alt_id : id);
if (alt_id)
free (alt_id);
user_id *p = dyn_cast<user_id *> (op);
if (p && p->is_oper_list)
{
if (active_fors.length() == 0)
record_operlist (id_tok->src_loc, p);
else
fatal_at (id_tok, "operator-list %s cannot be expanded inside 'for'", id);
}
return op;
}
/* Parse a capture.
capture = '@'<number> */
class operand *
parser::parse_capture (operand *op, bool require_existing)
{
location_t src_loc = eat_token (CPP_ATSIGN)->src_loc;
const cpp_token *token = peek ();
const char *id = NULL;
bool value_match = false;
/* For matches parse @@ as a value-match denoting the prevailing operand. */
if (token->type == CPP_ATSIGN
&& ! (token->flags & PREV_WHITE)
&& parsing_match_operand)
{
eat_token (CPP_ATSIGN);
token = peek ();
value_match = true;
}
if (token->type == CPP_NUMBER)
id = get_number ();
else if (token->type == CPP_NAME)
id = get_ident ();
else
fatal_at (token, "expected number or identifier");
unsigned next_id = capture_ids->elements ();
bool existed;
unsigned &num = capture_ids->get_or_insert (id, &existed);
if (!existed)
{
if (require_existing)
fatal_at (src_loc, "unknown capture id");
num = next_id;
}
return new capture (src_loc, num, op, value_match);
}
/* Parse an expression
expr = '(' <operation>[capture][flag][type] <operand>... ')' */
class operand *
parser::parse_expr ()
{
const cpp_token *token = peek ();
unsigned char opt_grp;
expr *e = new expr (parse_operation (opt_grp), token->src_loc);
token = peek ();
operand *op;
bool is_commutative = false;
bool force_capture = false;
const char *expr_type = NULL;
if (!parsing_match_operand
&& token->type == CPP_NOT
&& !(token->flags & PREV_WHITE))
{
if (!gimple)
fatal_at (token, "forcing simplification to a leaf is not supported "
"for GENERIC");
eat_token (CPP_NOT);
e->force_leaf = true;
}
if (token->type == CPP_COLON
&& !(token->flags & PREV_WHITE))
{
eat_token (CPP_COLON);
token = peek ();
if (token->type == CPP_NAME
&& !(token->flags & PREV_WHITE))
{
const char *s = get_ident ();
if (!parsing_match_operand)
expr_type = s;
else
{
const char *sp = s;
while (*sp)
{
if (*sp == 'c')
{
if (operator_id *o
= dyn_cast<operator_id *> (e->operation))
{
if (!commutative_tree_code (o->code)
&& !comparison_code_p (o->code))
fatal_at (token, "operation is not commutative");
}
else if (user_id *p = dyn_cast<user_id *> (e->operation))
for (unsigned i = 0;
i < p->substitutes.length (); ++i)
{
if (operator_id *q
= dyn_cast<operator_id *> (p->substitutes[i]))
{
if (!commutative_tree_code (q->code)
&& !comparison_code_p (q->code))
fatal_at (token, "operation %s is not "
"commutative", q->id);
}
}
is_commutative = true;
}
else if (*sp == 'C')
is_commutative = true;
else if (*sp == 's')
{
e->force_single_use = true;
force_capture = true;
}
else
fatal_at (token, "flag %c not recognized", *sp);
sp++;
}
}
token = peek ();
}
else
fatal_at (token, "expected flag or type specifying identifier");
}
if (token->type == CPP_ATSIGN
&& !(token->flags & PREV_WHITE))
op = parse_capture (e, false);
else if (force_capture)
{
unsigned num = get_internal_capture_id ();
op = new capture (token->src_loc, num, e, false);
}
else
op = e;
do
{
token = peek ();
if (token->type == CPP_CLOSE_PAREN)
{
if (e->operation->nargs != -1
&& e->operation->nargs != (int) e->ops.length ())
fatal_at (token, "'%s' expects %u operands, not %u",
e->operation->id, e->operation->nargs, e->ops.length ());
if (is_commutative)
{
if (e->ops.length () == 2
|| commutative_op (e->operation) >= 0)
e->is_commutative = true;
else
fatal_at (token, "only binary operators or functions with "
"two arguments can be marked commutative, "
"unless the operation is known to be inherently "
"commutative");
}
e->expr_type = expr_type;
if (opt_grp != 0)
{
if (e->ops.length () != 1)
fatal_at (token, "only unary operations can be conditional");
e->opt_grp = opt_grp;
}
return op;
}
else if (!(token->flags & PREV_WHITE))
fatal_at (token, "expected expression operand");
e->append_op (parse_op ());
}
while (1);
}
/* Lex native C code delimited by START recording the preprocessing tokens
for later processing.
c_expr = ('{'|'(') <pp token>... ('}'|')') */
c_expr *
parser::parse_c_expr (cpp_ttype start)
{
const cpp_token *token;
cpp_ttype end;
unsigned opencnt;
vec<cpp_token> code = vNULL;
unsigned nr_stmts = 0;
location_t loc = eat_token (start)->src_loc;
if (start == CPP_OPEN_PAREN)
end = CPP_CLOSE_PAREN;
else if (start == CPP_OPEN_BRACE)
end = CPP_CLOSE_BRACE;
else
gcc_unreachable ();
opencnt = 1;
do
{
token = next ();
/* Count brace pairs to find the end of the expr to match. */
if (token->type == start)
opencnt++;
else if (token->type == end
&& --opencnt == 0)
break;
else if (token->type == CPP_EOF)
fatal_at (token, "unexpected end of file");
/* This is a lame way of counting the number of statements. */
if (token->type == CPP_SEMICOLON)
nr_stmts++;
/* If this is possibly a user-defined identifier mark it used. */
if (token->type == CPP_NAME)
{
id_base *idb = get_operator ((const char *)CPP_HASHNODE
(token->val.node.node)->ident.str);
user_id *p;
if (idb && (p = dyn_cast<user_id *> (idb)) && p->is_oper_list)
record_operlist (token->src_loc, p);
}
/* Record the token. */
code.safe_push (*token);
}
while (1);
return new c_expr (r, loc, code, nr_stmts, vNULL, capture_ids);
}
/* Parse an operand which is either an expression, a predicate or
a standalone capture.
op = predicate | expr | c_expr | capture */
class operand *
parser::parse_op ()
{
const cpp_token *token = peek ();
class operand *op = NULL;
if (token->type == CPP_OPEN_PAREN)
{
eat_token (CPP_OPEN_PAREN);
op = parse_expr ();
eat_token (CPP_CLOSE_PAREN);
}
else if (token->type == CPP_OPEN_BRACE)
{
op = parse_c_expr (CPP_OPEN_BRACE);
}
else
{
/* Remaining ops are either empty or predicates */
if (token->type == CPP_NAME)
{
const char *id = get_ident ();
id_base *opr = get_operator (id);
if (!opr)
fatal_at (token, "expected predicate name");
if (operator_id *code1 = dyn_cast <operator_id *> (opr))
{
if (code1->nargs != 0)
fatal_at (token, "using an operator with operands as predicate");
/* Parse the zero-operand operator "predicates" as
expression. */
op = new expr (opr, token->src_loc);
}
else if (user_id *code2 = dyn_cast <user_id *> (opr))
{
if (code2->nargs != 0)
fatal_at (token, "using an operator with operands as predicate");
/* Parse the zero-operand operator "predicates" as
expression. */
op = new expr (opr, token->src_loc);
}
else if (predicate_id *p = dyn_cast <predicate_id *> (opr))
op = new predicate (p, token->src_loc);
else
fatal_at (token, "using an unsupported operator as predicate");
if (!parsing_match_operand)
fatal_at (token, "predicates are only allowed in match expression");
token = peek ();
if (token->flags & PREV_WHITE)
return op;
}
else if (token->type != CPP_COLON
&& token->type != CPP_ATSIGN)
fatal_at (token, "expected expression or predicate");
/* optionally followed by a capture and a predicate. */
if (token->type == CPP_COLON)
fatal_at (token, "not implemented: predicate on leaf operand");
if (token->type == CPP_ATSIGN)
op = parse_capture (op, !parsing_match_operand);
}
return op;
}
/* Create a new simplify from the current parsing state and MATCH,
MATCH_LOC, RESULT and RESULT_LOC and push it to SIMPLIFIERS. */
void
parser::push_simplify (simplify::simplify_kind kind,
vec<simplify *>& simplifiers,
operand *match, operand *result)
{
/* Build and push a temporary for operator list uses in expressions. */
if (!oper_lists.is_empty ())
active_fors.safe_push (oper_lists);
simplifiers.safe_push
(new simplify (kind, last_id++, match, result,
active_fors.copy (), capture_ids));
if (!oper_lists.is_empty ())
active_fors.pop ();
}
/* Parse
<result-op> = <op> | <if> | <with>
<if> = '(' 'if' '(' <c-expr> ')' <result-op> ')'
<with> = '(' 'with' '{' <c-expr> '}' <result-op> ')'
and return it. */
operand *
parser::parse_result (operand *result, predicate_id *matcher)
{
const cpp_token *token = peek ();
if (token->type != CPP_OPEN_PAREN)
return parse_op ();
eat_token (CPP_OPEN_PAREN);
if (peek_ident ("if"))
{
eat_ident ("if");
if_expr *ife = new if_expr (token->src_loc);
ife->cond = parse_c_expr (CPP_OPEN_PAREN);
if (peek ()->type == CPP_OPEN_PAREN)
{
ife->trueexpr = parse_result (result, matcher);
if (peek ()->type == CPP_OPEN_PAREN)
ife->falseexpr = parse_result (result, matcher);
else if (peek ()->type != CPP_CLOSE_PAREN)
ife->falseexpr = parse_op ();
}
else if (peek ()->type != CPP_CLOSE_PAREN)
{
ife->trueexpr = parse_op ();
if (peek ()->type == CPP_OPEN_PAREN)
ife->falseexpr = parse_result (result, matcher);
else if (peek ()->type != CPP_CLOSE_PAREN)
ife->falseexpr = parse_op ();
}
/* If this if is immediately closed then it contains a
manual matcher or is part of a predicate definition. */
else /* if (peek ()->type == CPP_CLOSE_PAREN) */
{
if (!matcher)
fatal_at (peek (), "manual transform not implemented");
ife->trueexpr = result;
}
eat_token (CPP_CLOSE_PAREN);
return ife;
}
else if (peek_ident ("with"))
{
eat_ident ("with");
with_expr *withe = new with_expr (token->src_loc);
/* Parse (with c-expr expr) as (if-with (true) expr). */
withe->with = parse_c_expr (CPP_OPEN_BRACE);
withe->with->nr_stmts = 0;
withe->subexpr = parse_result (result, matcher);
eat_token (CPP_CLOSE_PAREN);
return withe;
}
else if (peek_ident ("switch"))
{
token = eat_ident ("switch");
location_t ifloc = eat_token (CPP_OPEN_PAREN)->src_loc;
eat_ident ("if");
if_expr *ife = new if_expr (ifloc);
operand *res = ife;
ife->cond = parse_c_expr (CPP_OPEN_PAREN);
if (peek ()->type == CPP_OPEN_PAREN)
ife->trueexpr = parse_result (result, matcher);
else
ife->trueexpr = parse_op ();
eat_token (CPP_CLOSE_PAREN);
if (peek ()->type != CPP_OPEN_PAREN
|| !peek_ident ("if", 2))
fatal_at (token, "switch can be implemented with a single if");
while (peek ()->type != CPP_CLOSE_PAREN)
{
if (peek ()->type == CPP_OPEN_PAREN)
{
if (peek_ident ("if", 2))
{
ifloc = eat_token (CPP_OPEN_PAREN)->src_loc;
eat_ident ("if");
ife->falseexpr = new if_expr (ifloc);
ife = as_a <if_expr *> (ife->falseexpr);
ife->cond = parse_c_expr (CPP_OPEN_PAREN);
if (peek ()->type == CPP_OPEN_PAREN)
ife->trueexpr = parse_result (result, matcher);
else
ife->trueexpr = parse_op ();
eat_token (CPP_CLOSE_PAREN);
}
else
{
/* switch default clause */
ife->falseexpr = parse_result (result, matcher);
eat_token (CPP_CLOSE_PAREN);
return res;
}
}
else
{
/* switch default clause */
ife->falseexpr = parse_op ();
eat_token (CPP_CLOSE_PAREN);
return res;
}
}
eat_token (CPP_CLOSE_PAREN);
return res;
}
else
{
operand *op = result;
if (!matcher)
op = parse_expr ();
eat_token (CPP_CLOSE_PAREN);
return op;
}
}
/* Parse
simplify = 'simplify' <expr> <result-op>
or
match = 'match' <ident> <expr> [<result-op>]
and fill SIMPLIFIERS with the results. */
void
parser::parse_simplify (simplify::simplify_kind kind,
vec<simplify *>& simplifiers, predicate_id *matcher,
operand *result)
{
/* Reset the capture map. */
if (!capture_ids)
capture_ids = new cid_map_t;
/* Reset oper_lists and set. */
hash_set <user_id *> olist;
oper_lists_set = &olist;
oper_lists = vNULL;
const cpp_token *loc = peek ();
parsing_match_operand = true;
class operand *match = parse_op ();
finish_match_operand (match);
parsing_match_operand = false;
if (match->type == operand::OP_CAPTURE && !matcher)
fatal_at (loc, "outermost expression cannot be captured");
if (match->type == operand::OP_EXPR
&& is_a <predicate_id *> (as_a <expr *> (match)->operation))
fatal_at (loc, "outermost expression cannot be a predicate");
/* Splice active_ifs onto result and continue parsing the
"then" expr. */
if_expr *active_if = NULL;
for (int i = active_ifs.length (); i > 0; --i)
{
if_expr *ifc = new if_expr (active_ifs[i-1]->location);
ifc->cond = active_ifs[i-1];
ifc->trueexpr = active_if;
active_if = ifc;
}
if_expr *outermost_if = active_if;
while (active_if && active_if->trueexpr)
active_if = as_a <if_expr *> (active_if->trueexpr);
const cpp_token *token = peek ();
/* If this if is immediately closed then it is part of a predicate
definition. Push it. */
if (token->type == CPP_CLOSE_PAREN)
{
if (!matcher)
fatal_at (token, "expected transform expression");
if (active_if)
{
active_if->trueexpr = result;
result = outermost_if;
}
push_simplify (kind, simplifiers, match, result);
return;
}
operand *tem = parse_result (result, matcher);
if (active_if)
{
active_if->trueexpr = tem;
result = outermost_if;
}
else
result = tem;
push_simplify (kind, simplifiers, match, result);
}
/* Parsing of the outer control structures. */
/* Parse a for expression
for = '(' 'for' <subst>... <pattern> ')'
subst = <ident> '(' <ident>... ')' */
void
parser::parse_for (location_t)
{
auto_vec<const cpp_token *> user_id_tokens;
vec<user_id *> user_ids = vNULL;
const cpp_token *token;
unsigned min_n_opers = 0, max_n_opers = 0;
while (1)
{
token = peek ();
if (token->type != CPP_NAME)
break;
/* Insert the user defined operators into the operator hash. */
const char *id = get_ident ();
if (get_operator (id, true) != NULL)
fatal_at (token, "operator already defined");
user_id *op = new user_id (id);
id_base **slot = operators->find_slot_with_hash (op, op->hashval, INSERT);
*slot = op;
user_ids.safe_push (op);
user_id_tokens.safe_push (token);
eat_token (CPP_OPEN_PAREN);
int arity = -1;
while ((token = peek_ident ()) != 0)
{
const char *oper = get_ident ();
id_base *idb = get_operator (oper, true);
if (idb == NULL)
fatal_at (token, "no such operator '%s'", oper);
if (arity == -1)
arity = idb->nargs;
else if (idb->nargs == -1)
;
else if (idb->nargs != arity)
fatal_at (token, "operator '%s' with arity %d does not match "
"others with arity %d", oper, idb->nargs, arity);
user_id *p = dyn_cast<user_id *> (idb);
if (p)
{
if (p->is_oper_list)
op->substitutes.safe_splice (p->substitutes);
else
fatal_at (token, "iterator cannot be used as operator-list");
}
else
op->substitutes.safe_push (idb);
}
op->nargs = arity;
token = expect (CPP_CLOSE_PAREN);
unsigned nsubstitutes = op->substitutes.length ();
if (nsubstitutes == 0)
fatal_at (token, "A user-defined operator must have at least "
"one substitution");
if (max_n_opers == 0)
{
min_n_opers = nsubstitutes;
max_n_opers = nsubstitutes;
}
else
{
if (nsubstitutes % min_n_opers != 0
&& min_n_opers % nsubstitutes != 0)
fatal_at (token, "All user-defined identifiers must have a "
"multiple number of operator substitutions of the "
"smallest number of substitutions");
if (nsubstitutes < min_n_opers)
min_n_opers = nsubstitutes;
else if (nsubstitutes > max_n_opers)
max_n_opers = nsubstitutes;
}
}
unsigned n_ids = user_ids.length ();
if (n_ids == 0)
fatal_at (token, "for requires at least one user-defined identifier");
token = peek ();
if (token->type == CPP_CLOSE_PAREN)
fatal_at (token, "no pattern defined in for");
active_fors.safe_push (user_ids);
while (1)
{
token = peek ();
if (token->type == CPP_CLOSE_PAREN)
break;
parse_pattern ();
}
active_fors.pop ();
/* Remove user-defined operators from the hash again. */
for (unsigned i = 0; i < user_ids.length (); ++i)
{
if (!user_ids[i]->used)
warning_at (user_id_tokens[i],
"operator %s defined but not used", user_ids[i]->id);
operators->remove_elt (user_ids[i]);
}
}
/* Parse an identifier associated with a list of operators.
oprs = '(' 'define_operator_list' <ident> <ident>... ')' */
void
parser::parse_operator_list (location_t)
{
const cpp_token *token = peek ();
const char *id = get_ident ();
if (get_operator (id, true) != 0)
fatal_at (token, "operator %s already defined", id);
user_id *op = new user_id (id, true);
int arity = -1;
while ((token = peek_ident ()) != 0)
{
token = peek ();
const char *oper = get_ident ();
id_base *idb = get_operator (oper, true);
if (idb == 0)
fatal_at (token, "no such operator '%s'", oper);
if (arity == -1)
arity = idb->nargs;
else if (idb->nargs == -1)
;
else if (arity != idb->nargs)
fatal_at (token, "operator '%s' with arity %d does not match "
"others with arity %d", oper, idb->nargs, arity);
/* We allow composition of multiple operator lists. */
if (user_id *p = dyn_cast<user_id *> (idb))
op->substitutes.safe_splice (p->substitutes);
else
op->substitutes.safe_push (idb);
}
// Check that there is no junk after id-list
token = peek();
if (token->type != CPP_CLOSE_PAREN)
fatal_at (token, "expected identifier got %s", cpp_type2name (token->type, 0));
if (op->substitutes.length () == 0)
fatal_at (token, "operator-list cannot be empty");
op->nargs = arity;
id_base **slot = operators->find_slot_with_hash (op, op->hashval, INSERT);
*slot = op;
}
/* Parse an outer if expression.
if = '(' 'if' '(' <c-expr> ')' <pattern> ')' */
void
parser::parse_if (location_t)
{
c_expr *ifexpr = parse_c_expr (CPP_OPEN_PAREN);
const cpp_token *token = peek ();
if (token->type == CPP_CLOSE_PAREN)
fatal_at (token, "no pattern defined in if");
active_ifs.safe_push (ifexpr);
while (1)
{
token = peek ();
if (token->type == CPP_CLOSE_PAREN)
break;
parse_pattern ();
}
active_ifs.pop ();
}
/* Parse a list of predefined predicate identifiers.
preds = '(' 'define_predicates' <ident>... ')' */
void
parser::parse_predicates (location_t)
{
do
{
const cpp_token *token = peek ();
if (token->type != CPP_NAME)
break;
add_predicate (get_ident ());
}
while (1);
}
/* Parse outer control structures.
pattern = <preds>|<for>|<if>|<simplify>|<match> */
void
parser::parse_pattern ()
{
/* All clauses start with '('. */
eat_token (CPP_OPEN_PAREN);
const cpp_token *token = peek ();
const char *id = get_ident ();
if (strcmp (id, "simplify") == 0)
{
parse_simplify (simplify::SIMPLIFY, simplifiers, NULL, NULL);
capture_ids = NULL;
}
else if (strcmp (id, "match") == 0)
{
bool with_args = false;
location_t e_loc = peek ()->src_loc;
if (peek ()->type == CPP_OPEN_PAREN)
{
eat_token (CPP_OPEN_PAREN);
with_args = true;
}
const char *name = get_ident ();
id_base *id1 = get_operator (name);
predicate_id *p;
if (!id1)
{
p = add_predicate (name);
user_predicates.safe_push (p);
}
else if ((p = dyn_cast <predicate_id *> (id1)))
;
else
fatal_at (token, "cannot add a match to a non-predicate ID");
/* Parse (match <id> <arg>... (match-expr)) here. */
expr *e = NULL;
if (with_args)
{
capture_ids = new cid_map_t;
e = new expr (p, e_loc);
while (peek ()->type == CPP_ATSIGN)
e->append_op (parse_capture (NULL, false));
eat_token (CPP_CLOSE_PAREN);
}
if (p->nargs != -1
&& ((e && e->ops.length () != (unsigned)p->nargs)
|| (!e && p->nargs != 0)))
fatal_at (token, "non-matching number of match operands");
p->nargs = e ? e->ops.length () : 0;
parse_simplify (simplify::MATCH, p->matchers, p, e);
capture_ids = NULL;
}
else if (strcmp (id, "for") == 0)
parse_for (token->src_loc);
else if (strcmp (id, "if") == 0)
parse_if (token->src_loc);
else if (strcmp (id, "define_predicates") == 0)
{
if (active_ifs.length () > 0
|| active_fors.length () > 0)
fatal_at (token, "define_predicates inside if or for is not supported");
parse_predicates (token->src_loc);
}
else if (strcmp (id, "define_operator_list") == 0)
{
if (active_ifs.length () > 0
|| active_fors.length () > 0)
fatal_at (token, "operator-list inside if or for is not supported");
parse_operator_list (token->src_loc);
}
else
fatal_at (token, "expected %s'simplify', 'match', 'for' or 'if'",
active_ifs.length () == 0 && active_fors.length () == 0
? "'define_predicates', " : "");
eat_token (CPP_CLOSE_PAREN);
}
/* Helper for finish_match_operand, collecting captures of OP in CPTS
recursively. */
static void
walk_captures (operand *op, vec<vec<capture *> > cpts)
{
if (! op)
return;
if (capture *c = dyn_cast <capture *> (op))
{
cpts[c->where].safe_push (c);
walk_captures (c->what, cpts);
}
else if (expr *e = dyn_cast <expr *> (op))
for (unsigned i = 0; i < e->ops.length (); ++i)
walk_captures (e->ops[i], cpts);
}
/* Finish up OP which is a match operand. */
void
parser::finish_match_operand (operand *op)
{
/* Look for matching captures, diagnose mis-uses of @@ and apply
early lowering and distribution of value_match. */
auto_vec<vec<capture *> > cpts;
cpts.safe_grow_cleared (capture_ids->elements (), true);
walk_captures (op, cpts);
for (unsigned i = 0; i < cpts.length (); ++i)
{
capture *value_match = NULL;
for (unsigned j = 0; j < cpts[i].length (); ++j)
{
if (cpts[i][j]->value_match)
{
if (value_match)
fatal_at (cpts[i][j]->location, "duplicate @@");
value_match = cpts[i][j];
}
}
if (cpts[i].length () == 1 && value_match)
fatal_at (value_match->location, "@@ without a matching capture");
if (value_match)
{
/* Duplicate prevailing capture with the existing ID, create
a fake ID and rewrite all captures to use it. This turns
@@1 into @__<newid>@1 and @1 into @__<newid>. */
value_match->what = new capture (value_match->location,
value_match->where,
value_match->what, false);
/* Create a fake ID and rewrite all captures to use it. */
unsigned newid = get_internal_capture_id ();
for (unsigned j = 0; j < cpts[i].length (); ++j)
{
cpts[i][j]->where = newid;
cpts[i][j]->value_match = true;
}
}
cpts[i].release ();
}
}
/* Main entry of the parser. Repeatedly parse outer control structures. */
parser::parser (cpp_reader *r_, bool gimple_)
{
r = r_;
gimple = gimple_;
active_ifs = vNULL;
active_fors = vNULL;
simplifiers = vNULL;
oper_lists_set = NULL;
oper_lists = vNULL;
capture_ids = NULL;
user_predicates = vNULL;
parsing_match_operand = false;
last_id = 0;
const cpp_token *token = next ();
while (token->type != CPP_EOF)
{
_cpp_backup_tokens (r, 1);
parse_pattern ();
token = next ();
}
}
/* Helper for the linemap code. */
static size_t
round_alloc_size (size_t s)
{
return s;
}
/* The genmatch generator program. It reads from a pattern description
and outputs GIMPLE or GENERIC IL matching and simplification routines. */
int
main (int argc, char **argv)
{
cpp_reader *r;
progname = "genmatch";
if (argc < 2)
return 1;
bool gimple = true;
char *input = argv[argc-1];
for (int i = 1; i < argc - 1; ++i)
{
if (strcmp (argv[i], "--gimple") == 0)
gimple = true;
else if (strcmp (argv[i], "--generic") == 0)
gimple = false;
else if (strcmp (argv[i], "-v") == 0)
verbose = 1;
else if (strcmp (argv[i], "-vv") == 0)
verbose = 2;
else
{
fprintf (stderr, "Usage: genmatch "
"[--gimple] [--generic] [-v[v]] input\n");
return 1;
}
}
line_table = XCNEW (class line_maps);
linemap_init (line_table, 0);
line_table->reallocator = xrealloc;
line_table->round_alloc_size = round_alloc_size;
r = cpp_create_reader (CLK_GNUC99, NULL, line_table);
cpp_callbacks *cb = cpp_get_callbacks (r);
cb->diagnostic = diagnostic_cb;
/* Add the build directory to the #include "" search path. */
cpp_dir *dir = XCNEW (cpp_dir);
dir->name = getpwd ();
if (!dir->name)
dir->name = ASTRDUP (".");
cpp_set_include_chains (r, dir, NULL, false);
if (!cpp_read_main_file (r, input))
return 1;
cpp_define (r, gimple ? "GIMPLE=1": "GENERIC=1");
cpp_define (r, gimple ? "GENERIC=0": "GIMPLE=0");
null_id = new id_base (id_base::NULL_ID, "null");
/* Pre-seed operators. */
operators = new hash_table<id_base> (1024);
#define DEFTREECODE(SYM, STRING, TYPE, NARGS) \
add_operator (SYM, # SYM, # TYPE, NARGS);
#define END_OF_BASE_TREE_CODES
#include "tree.def"
#undef END_OF_BASE_TREE_CODES
#undef DEFTREECODE
/* Pre-seed builtin functions.
??? Cannot use N (name) as that is targetm.emultls.get_address
for BUILT_IN_EMUTLS_GET_ADDRESS ... */
#define DEF_BUILTIN(ENUM, N, C, T, LT, B, F, NA, AT, IM, COND) \
add_function (ENUM, "CFN_" # ENUM);
#include "builtins.def"
#define DEF_INTERNAL_FN(CODE, NAME, FNSPEC) \
add_function (IFN_##CODE, "CFN_" #CODE);
#include "internal-fn.def"
/* Parse ahead! */
parser p (r, gimple);
if (gimple)
write_header (stdout, "gimple-match-head.c");
else
write_header (stdout, "generic-match-head.c");
/* Go over all predicates defined with patterns and perform
lowering and code generation. */
for (unsigned i = 0; i < p.user_predicates.length (); ++i)
{
predicate_id *pred = p.user_predicates[i];
lower (pred->matchers, gimple);
if (verbose == 2)
for (unsigned j = 0; j < pred->matchers.length (); ++j)
print_matches (pred->matchers[j]);
decision_tree dt;
for (unsigned j = 0; j < pred->matchers.length (); ++j)
dt.insert (pred->matchers[j], j);
if (verbose == 2)
dt.print (stderr);
write_predicate (stdout, pred, dt, gimple);
}
/* Lower the main simplifiers and generate code for them. */
lower (p.simplifiers, gimple);
if (verbose == 2)
for (unsigned i = 0; i < p.simplifiers.length (); ++i)
print_matches (p.simplifiers[i]);
decision_tree dt;
for (unsigned i = 0; i < p.simplifiers.length (); ++i)
dt.insert (p.simplifiers[i], i);
if (verbose == 2)
dt.print (stderr);
dt.gen (stdout, gimple);
/* Finalize. */
cpp_finish (r, NULL);
cpp_destroy (r);
delete operators;
return 0;
}
|