1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
|
//===-- AArch64TargetTransformInfo.cpp - AArch64 specific TTI -------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "AArch64TargetTransformInfo.h"
#include "AArch64ExpandImm.h"
#include "AArch64PerfectShuffle.h"
#include "MCTargetDesc/AArch64AddressingModes.h"
#include "Utils/AArch64SMEAttributes.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/Analysis/LoopInfo.h"
#include "llvm/Analysis/TargetTransformInfo.h"
#include "llvm/CodeGen/BasicTTIImpl.h"
#include "llvm/CodeGen/CostTable.h"
#include "llvm/CodeGen/TargetLowering.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/Intrinsics.h"
#include "llvm/IR/IntrinsicsAArch64.h"
#include "llvm/IR/PatternMatch.h"
#include "llvm/Support/Debug.h"
#include "llvm/TargetParser/AArch64TargetParser.h"
#include "llvm/Transforms/InstCombine/InstCombiner.h"
#include "llvm/Transforms/Utils/UnrollLoop.h"
#include "llvm/Transforms/Vectorize/LoopVectorizationLegality.h"
#include <algorithm>
#include <optional>
using namespace llvm;
using namespace llvm::PatternMatch;
#define DEBUG_TYPE "aarch64tti"
static cl::opt<bool> EnableFalkorHWPFUnrollFix("enable-falkor-hwpf-unroll-fix",
cl::init(true), cl::Hidden);
static cl::opt<bool> SVEPreferFixedOverScalableIfEqualCost(
"sve-prefer-fixed-over-scalable-if-equal", cl::Hidden);
static cl::opt<unsigned> SVEGatherOverhead("sve-gather-overhead", cl::init(10),
cl::Hidden);
static cl::opt<unsigned> SVEScatterOverhead("sve-scatter-overhead",
cl::init(10), cl::Hidden);
static cl::opt<unsigned> SVETailFoldInsnThreshold("sve-tail-folding-insn-threshold",
cl::init(15), cl::Hidden);
static cl::opt<unsigned>
NeonNonConstStrideOverhead("neon-nonconst-stride-overhead", cl::init(10),
cl::Hidden);
static cl::opt<unsigned> CallPenaltyChangeSM(
"call-penalty-sm-change", cl::init(5), cl::Hidden,
cl::desc(
"Penalty of calling a function that requires a change to PSTATE.SM"));
static cl::opt<unsigned> InlineCallPenaltyChangeSM(
"inline-call-penalty-sm-change", cl::init(10), cl::Hidden,
cl::desc("Penalty of inlining a call that requires a change to PSTATE.SM"));
static cl::opt<bool> EnableOrLikeSelectOpt("enable-aarch64-or-like-select",
cl::init(true), cl::Hidden);
static cl::opt<bool> EnableLSRCostOpt("enable-aarch64-lsr-cost-opt",
cl::init(true), cl::Hidden);
// A complete guess as to a reasonable cost.
static cl::opt<unsigned>
BaseHistCntCost("aarch64-base-histcnt-cost", cl::init(8), cl::Hidden,
cl::desc("The cost of a histcnt instruction"));
static cl::opt<unsigned> DMBLookaheadThreshold(
"dmb-lookahead-threshold", cl::init(10), cl::Hidden,
cl::desc("The number of instructions to search for a redundant dmb"));
namespace {
class TailFoldingOption {
// These bitfields will only ever be set to something non-zero in operator=,
// when setting the -sve-tail-folding option. This option should always be of
// the form (default|simple|all|disable)[+(Flag1|Flag2|etc)], where here
// InitialBits is one of (disabled|all|simple). EnableBits represents
// additional flags we're enabling, and DisableBits for those flags we're
// disabling. The default flag is tracked in the variable NeedsDefault, since
// at the time of setting the option we may not know what the default value
// for the CPU is.
TailFoldingOpts InitialBits = TailFoldingOpts::Disabled;
TailFoldingOpts EnableBits = TailFoldingOpts::Disabled;
TailFoldingOpts DisableBits = TailFoldingOpts::Disabled;
// This value needs to be initialised to true in case the user does not
// explicitly set the -sve-tail-folding option.
bool NeedsDefault = true;
void setInitialBits(TailFoldingOpts Bits) { InitialBits = Bits; }
void setNeedsDefault(bool V) { NeedsDefault = V; }
void setEnableBit(TailFoldingOpts Bit) {
EnableBits |= Bit;
DisableBits &= ~Bit;
}
void setDisableBit(TailFoldingOpts Bit) {
EnableBits &= ~Bit;
DisableBits |= Bit;
}
TailFoldingOpts getBits(TailFoldingOpts DefaultBits) const {
TailFoldingOpts Bits = TailFoldingOpts::Disabled;
assert((InitialBits == TailFoldingOpts::Disabled || !NeedsDefault) &&
"Initial bits should only include one of "
"(disabled|all|simple|default)");
Bits = NeedsDefault ? DefaultBits : InitialBits;
Bits |= EnableBits;
Bits &= ~DisableBits;
return Bits;
}
void reportError(std::string Opt) {
errs() << "invalid argument '" << Opt
<< "' to -sve-tail-folding=; the option should be of the form\n"
" (disabled|all|default|simple)[+(reductions|recurrences"
"|reverse|noreductions|norecurrences|noreverse)]\n";
report_fatal_error("Unrecognised tail-folding option");
}
public:
void operator=(const std::string &Val) {
// If the user explicitly sets -sve-tail-folding= then treat as an error.
if (Val.empty()) {
reportError("");
return;
}
// Since the user is explicitly setting the option we don't automatically
// need the default unless they require it.
setNeedsDefault(false);
SmallVector<StringRef, 4> TailFoldTypes;
StringRef(Val).split(TailFoldTypes, '+', -1, false);
unsigned StartIdx = 1;
if (TailFoldTypes[0] == "disabled")
setInitialBits(TailFoldingOpts::Disabled);
else if (TailFoldTypes[0] == "all")
setInitialBits(TailFoldingOpts::All);
else if (TailFoldTypes[0] == "default")
setNeedsDefault(true);
else if (TailFoldTypes[0] == "simple")
setInitialBits(TailFoldingOpts::Simple);
else {
StartIdx = 0;
setInitialBits(TailFoldingOpts::Disabled);
}
for (unsigned I = StartIdx; I < TailFoldTypes.size(); I++) {
if (TailFoldTypes[I] == "reductions")
setEnableBit(TailFoldingOpts::Reductions);
else if (TailFoldTypes[I] == "recurrences")
setEnableBit(TailFoldingOpts::Recurrences);
else if (TailFoldTypes[I] == "reverse")
setEnableBit(TailFoldingOpts::Reverse);
else if (TailFoldTypes[I] == "noreductions")
setDisableBit(TailFoldingOpts::Reductions);
else if (TailFoldTypes[I] == "norecurrences")
setDisableBit(TailFoldingOpts::Recurrences);
else if (TailFoldTypes[I] == "noreverse")
setDisableBit(TailFoldingOpts::Reverse);
else
reportError(Val);
}
}
bool satisfies(TailFoldingOpts DefaultBits, TailFoldingOpts Required) const {
return (getBits(DefaultBits) & Required) == Required;
}
};
} // namespace
TailFoldingOption TailFoldingOptionLoc;
static cl::opt<TailFoldingOption, true, cl::parser<std::string>> SVETailFolding(
"sve-tail-folding",
cl::desc(
"Control the use of vectorisation using tail-folding for SVE where the"
" option is specified in the form (Initial)[+(Flag1|Flag2|...)]:"
"\ndisabled (Initial) No loop types will vectorize using "
"tail-folding"
"\ndefault (Initial) Uses the default tail-folding settings for "
"the target CPU"
"\nall (Initial) All legal loop types will vectorize using "
"tail-folding"
"\nsimple (Initial) Use tail-folding for simple loops (not "
"reductions or recurrences)"
"\nreductions Use tail-folding for loops containing reductions"
"\nnoreductions Inverse of above"
"\nrecurrences Use tail-folding for loops containing fixed order "
"recurrences"
"\nnorecurrences Inverse of above"
"\nreverse Use tail-folding for loops requiring reversed "
"predicates"
"\nnoreverse Inverse of above"),
cl::location(TailFoldingOptionLoc));
// Experimental option that will only be fully functional when the
// code-generator is changed to use SVE instead of NEON for all fixed-width
// operations.
static cl::opt<bool> EnableFixedwidthAutovecInStreamingMode(
"enable-fixedwidth-autovec-in-streaming-mode", cl::init(false), cl::Hidden);
// Experimental option that will only be fully functional when the cost-model
// and code-generator have been changed to avoid using scalable vector
// instructions that are not legal in streaming SVE mode.
static cl::opt<bool> EnableScalableAutovecInStreamingMode(
"enable-scalable-autovec-in-streaming-mode", cl::init(false), cl::Hidden);
static bool isSMEABIRoutineCall(const CallInst &CI,
const AArch64TargetLowering &TLI) {
const auto *F = CI.getCalledFunction();
return F && SMEAttrs(F->getName(), TLI).isSMEABIRoutine();
}
/// Returns true if the function has explicit operations that can only be
/// lowered using incompatible instructions for the selected mode. This also
/// returns true if the function F may use or modify ZA state.
static bool hasPossibleIncompatibleOps(const Function *F,
const AArch64TargetLowering &TLI) {
for (const BasicBlock &BB : *F) {
for (const Instruction &I : BB) {
// Be conservative for now and assume that any call to inline asm or to
// intrinsics could could result in non-streaming ops (e.g. calls to
// @llvm.aarch64.* or @llvm.gather/scatter intrinsics). We can assume that
// all native LLVM instructions can be lowered to compatible instructions.
if (isa<CallInst>(I) && !I.isDebugOrPseudoInst() &&
(cast<CallInst>(I).isInlineAsm() || isa<IntrinsicInst>(I) ||
isSMEABIRoutineCall(cast<CallInst>(I), TLI)))
return true;
}
}
return false;
}
APInt AArch64TTIImpl::getFeatureMask(const Function &F) const {
StringRef AttributeStr =
isMultiversionedFunction(F) ? "fmv-features" : "target-features";
StringRef FeatureStr = F.getFnAttribute(AttributeStr).getValueAsString();
SmallVector<StringRef, 8> Features;
FeatureStr.split(Features, ",");
return AArch64::getFMVPriority(Features);
}
bool AArch64TTIImpl::isMultiversionedFunction(const Function &F) const {
return F.hasFnAttribute("fmv-features");
}
const FeatureBitset AArch64TTIImpl::InlineInverseFeatures = {
AArch64::FeatureExecuteOnly,
};
bool AArch64TTIImpl::areInlineCompatible(const Function *Caller,
const Function *Callee) const {
SMECallAttrs CallAttrs(*Caller, *Callee);
// Never inline a function explicitly marked as being streaming,
// into a non-streaming function. Assume it was marked as streaming
// for a reason.
if (CallAttrs.caller().hasNonStreamingInterfaceAndBody() &&
CallAttrs.callee().hasStreamingInterfaceOrBody())
return false;
// When inlining, we should consider the body of the function, not the
// interface.
if (CallAttrs.callee().hasStreamingBody()) {
CallAttrs.callee().set(SMEAttrs::SM_Compatible, false);
CallAttrs.callee().set(SMEAttrs::SM_Enabled, true);
}
if (CallAttrs.callee().isNewZA() || CallAttrs.callee().isNewZT0())
return false;
if (CallAttrs.requiresLazySave() || CallAttrs.requiresSMChange() ||
CallAttrs.requiresPreservingZT0() ||
CallAttrs.requiresPreservingAllZAState()) {
if (hasPossibleIncompatibleOps(Callee, *getTLI()))
return false;
}
const TargetMachine &TM = getTLI()->getTargetMachine();
const FeatureBitset &CallerBits =
TM.getSubtargetImpl(*Caller)->getFeatureBits();
const FeatureBitset &CalleeBits =
TM.getSubtargetImpl(*Callee)->getFeatureBits();
// Adjust the feature bitsets by inverting some of the bits. This is needed
// for target features that represent restrictions rather than capabilities,
// for example a "+execute-only" callee can be inlined into a caller without
// "+execute-only", but not vice versa.
FeatureBitset EffectiveCallerBits = CallerBits ^ InlineInverseFeatures;
FeatureBitset EffectiveCalleeBits = CalleeBits ^ InlineInverseFeatures;
return (EffectiveCallerBits & EffectiveCalleeBits) == EffectiveCalleeBits;
}
bool AArch64TTIImpl::areTypesABICompatible(
const Function *Caller, const Function *Callee,
const ArrayRef<Type *> &Types) const {
if (!BaseT::areTypesABICompatible(Caller, Callee, Types))
return false;
// We need to ensure that argument promotion does not attempt to promote
// pointers to fixed-length vector types larger than 128 bits like
// <8 x float> (and pointers to aggregate types which have such fixed-length
// vector type members) into the values of the pointees. Such vector types
// are used for SVE VLS but there is no ABI for SVE VLS arguments and the
// backend cannot lower such value arguments. The 128-bit fixed-length SVE
// types can be safely treated as 128-bit NEON types and they cannot be
// distinguished in IR.
if (ST->useSVEForFixedLengthVectors() && llvm::any_of(Types, [](Type *Ty) {
auto FVTy = dyn_cast<FixedVectorType>(Ty);
return FVTy &&
FVTy->getScalarSizeInBits() * FVTy->getNumElements() > 128;
}))
return false;
return true;
}
unsigned
AArch64TTIImpl::getInlineCallPenalty(const Function *F, const CallBase &Call,
unsigned DefaultCallPenalty) const {
// This function calculates a penalty for executing Call in F.
//
// There are two ways this function can be called:
// (1) F:
// call from F -> G (the call here is Call)
//
// For (1), Call.getCaller() == F, so it will always return a high cost if
// a streaming-mode change is required (thus promoting the need to inline the
// function)
//
// (2) F:
// call from F -> G (the call here is not Call)
// G:
// call from G -> H (the call here is Call)
//
// For (2), if after inlining the body of G into F the call to H requires a
// streaming-mode change, and the call to G from F would also require a
// streaming-mode change, then there is benefit to do the streaming-mode
// change only once and avoid inlining of G into F.
SMEAttrs FAttrs(*F);
SMECallAttrs CallAttrs(Call, getTLI());
if (SMECallAttrs(FAttrs, CallAttrs.callee()).requiresSMChange()) {
if (F == Call.getCaller()) // (1)
return CallPenaltyChangeSM * DefaultCallPenalty;
if (SMECallAttrs(FAttrs, CallAttrs.caller()).requiresSMChange()) // (2)
return InlineCallPenaltyChangeSM * DefaultCallPenalty;
}
return DefaultCallPenalty;
}
bool AArch64TTIImpl::shouldMaximizeVectorBandwidth(
TargetTransformInfo::RegisterKind K) const {
assert(K != TargetTransformInfo::RGK_Scalar);
return (K == TargetTransformInfo::RGK_FixedWidthVector &&
ST->isNeonAvailable());
}
/// Calculate the cost of materializing a 64-bit value. This helper
/// method might only calculate a fraction of a larger immediate. Therefore it
/// is valid to return a cost of ZERO.
InstructionCost AArch64TTIImpl::getIntImmCost(int64_t Val) const {
// Check if the immediate can be encoded within an instruction.
if (Val == 0 || AArch64_AM::isLogicalImmediate(Val, 64))
return 0;
if (Val < 0)
Val = ~Val;
// Calculate how many moves we will need to materialize this constant.
SmallVector<AArch64_IMM::ImmInsnModel, 4> Insn;
AArch64_IMM::expandMOVImm(Val, 64, Insn);
return Insn.size();
}
/// Calculate the cost of materializing the given constant.
InstructionCost
AArch64TTIImpl::getIntImmCost(const APInt &Imm, Type *Ty,
TTI::TargetCostKind CostKind) const {
assert(Ty->isIntegerTy());
unsigned BitSize = Ty->getPrimitiveSizeInBits();
if (BitSize == 0)
return ~0U;
// Sign-extend all constants to a multiple of 64-bit.
APInt ImmVal = Imm;
if (BitSize & 0x3f)
ImmVal = Imm.sext((BitSize + 63) & ~0x3fU);
// Split the constant into 64-bit chunks and calculate the cost for each
// chunk.
InstructionCost Cost = 0;
for (unsigned ShiftVal = 0; ShiftVal < BitSize; ShiftVal += 64) {
APInt Tmp = ImmVal.ashr(ShiftVal).sextOrTrunc(64);
int64_t Val = Tmp.getSExtValue();
Cost += getIntImmCost(Val);
}
// We need at least one instruction to materialze the constant.
return std::max<InstructionCost>(1, Cost);
}
InstructionCost AArch64TTIImpl::getIntImmCostInst(unsigned Opcode, unsigned Idx,
const APInt &Imm, Type *Ty,
TTI::TargetCostKind CostKind,
Instruction *Inst) const {
assert(Ty->isIntegerTy());
unsigned BitSize = Ty->getPrimitiveSizeInBits();
// There is no cost model for constants with a bit size of 0. Return TCC_Free
// here, so that constant hoisting will ignore this constant.
if (BitSize == 0)
return TTI::TCC_Free;
unsigned ImmIdx = ~0U;
switch (Opcode) {
default:
return TTI::TCC_Free;
case Instruction::GetElementPtr:
// Always hoist the base address of a GetElementPtr.
if (Idx == 0)
return 2 * TTI::TCC_Basic;
return TTI::TCC_Free;
case Instruction::Store:
ImmIdx = 0;
break;
case Instruction::Add:
case Instruction::Sub:
case Instruction::Mul:
case Instruction::UDiv:
case Instruction::SDiv:
case Instruction::URem:
case Instruction::SRem:
case Instruction::And:
case Instruction::Or:
case Instruction::Xor:
case Instruction::ICmp:
ImmIdx = 1;
break;
// Always return TCC_Free for the shift value of a shift instruction.
case Instruction::Shl:
case Instruction::LShr:
case Instruction::AShr:
if (Idx == 1)
return TTI::TCC_Free;
break;
case Instruction::Trunc:
case Instruction::ZExt:
case Instruction::SExt:
case Instruction::IntToPtr:
case Instruction::PtrToInt:
case Instruction::BitCast:
case Instruction::PHI:
case Instruction::Call:
case Instruction::Select:
case Instruction::Ret:
case Instruction::Load:
break;
}
if (Idx == ImmIdx) {
int NumConstants = (BitSize + 63) / 64;
InstructionCost Cost = AArch64TTIImpl::getIntImmCost(Imm, Ty, CostKind);
return (Cost <= NumConstants * TTI::TCC_Basic)
? static_cast<int>(TTI::TCC_Free)
: Cost;
}
return AArch64TTIImpl::getIntImmCost(Imm, Ty, CostKind);
}
InstructionCost
AArch64TTIImpl::getIntImmCostIntrin(Intrinsic::ID IID, unsigned Idx,
const APInt &Imm, Type *Ty,
TTI::TargetCostKind CostKind) const {
assert(Ty->isIntegerTy());
unsigned BitSize = Ty->getPrimitiveSizeInBits();
// There is no cost model for constants with a bit size of 0. Return TCC_Free
// here, so that constant hoisting will ignore this constant.
if (BitSize == 0)
return TTI::TCC_Free;
// Most (all?) AArch64 intrinsics do not support folding immediates into the
// selected instruction, so we compute the materialization cost for the
// immediate directly.
if (IID >= Intrinsic::aarch64_addg && IID <= Intrinsic::aarch64_udiv)
return AArch64TTIImpl::getIntImmCost(Imm, Ty, CostKind);
switch (IID) {
default:
return TTI::TCC_Free;
case Intrinsic::sadd_with_overflow:
case Intrinsic::uadd_with_overflow:
case Intrinsic::ssub_with_overflow:
case Intrinsic::usub_with_overflow:
case Intrinsic::smul_with_overflow:
case Intrinsic::umul_with_overflow:
if (Idx == 1) {
int NumConstants = (BitSize + 63) / 64;
InstructionCost Cost = AArch64TTIImpl::getIntImmCost(Imm, Ty, CostKind);
return (Cost <= NumConstants * TTI::TCC_Basic)
? static_cast<int>(TTI::TCC_Free)
: Cost;
}
break;
case Intrinsic::experimental_stackmap:
if ((Idx < 2) || (Imm.getBitWidth() <= 64 && isInt<64>(Imm.getSExtValue())))
return TTI::TCC_Free;
break;
case Intrinsic::experimental_patchpoint_void:
case Intrinsic::experimental_patchpoint:
if ((Idx < 4) || (Imm.getBitWidth() <= 64 && isInt<64>(Imm.getSExtValue())))
return TTI::TCC_Free;
break;
case Intrinsic::experimental_gc_statepoint:
if ((Idx < 5) || (Imm.getBitWidth() <= 64 && isInt<64>(Imm.getSExtValue())))
return TTI::TCC_Free;
break;
}
return AArch64TTIImpl::getIntImmCost(Imm, Ty, CostKind);
}
TargetTransformInfo::PopcntSupportKind
AArch64TTIImpl::getPopcntSupport(unsigned TyWidth) const {
assert(isPowerOf2_32(TyWidth) && "Ty width must be power of 2");
if (TyWidth == 32 || TyWidth == 64)
return TTI::PSK_FastHardware;
// TODO: AArch64TargetLowering::LowerCTPOP() supports 128bit popcount.
return TTI::PSK_Software;
}
static bool isUnpackedVectorVT(EVT VecVT) {
return VecVT.isScalableVector() &&
VecVT.getSizeInBits().getKnownMinValue() < AArch64::SVEBitsPerBlock;
}
static InstructionCost getHistogramCost(const AArch64Subtarget *ST,
const IntrinsicCostAttributes &ICA) {
// We need to know at least the number of elements in the vector of buckets
// and the size of each element to update.
if (ICA.getArgTypes().size() < 2)
return InstructionCost::getInvalid();
// Only interested in costing for the hardware instruction from SVE2.
if (!ST->hasSVE2())
return InstructionCost::getInvalid();
Type *BucketPtrsTy = ICA.getArgTypes()[0]; // Type of vector of pointers
Type *EltTy = ICA.getArgTypes()[1]; // Type of bucket elements
unsigned TotalHistCnts = 1;
unsigned EltSize = EltTy->getScalarSizeInBits();
// Only allow (up to 64b) integers or pointers
if ((!EltTy->isIntegerTy() && !EltTy->isPointerTy()) || EltSize > 64)
return InstructionCost::getInvalid();
// FIXME: We should be able to generate histcnt for fixed-length vectors
// using ptrue with a specific VL.
if (VectorType *VTy = dyn_cast<VectorType>(BucketPtrsTy)) {
unsigned EC = VTy->getElementCount().getKnownMinValue();
if (!isPowerOf2_64(EC) || !VTy->isScalableTy())
return InstructionCost::getInvalid();
// HistCnt only supports 32b and 64b element types
unsigned LegalEltSize = EltSize <= 32 ? 32 : 64;
if (EC == 2 || (LegalEltSize == 32 && EC == 4))
return InstructionCost(BaseHistCntCost);
unsigned NaturalVectorWidth = AArch64::SVEBitsPerBlock / LegalEltSize;
TotalHistCnts = EC / NaturalVectorWidth;
return InstructionCost(BaseHistCntCost * TotalHistCnts);
}
return InstructionCost::getInvalid();
}
InstructionCost
AArch64TTIImpl::getIntrinsicInstrCost(const IntrinsicCostAttributes &ICA,
TTI::TargetCostKind CostKind) const {
// The code-generator is currently not able to handle scalable vectors
// of <vscale x 1 x eltty> yet, so return an invalid cost to avoid selecting
// it. This change will be removed when code-generation for these types is
// sufficiently reliable.
auto *RetTy = ICA.getReturnType();
if (auto *VTy = dyn_cast<ScalableVectorType>(RetTy))
if (VTy->getElementCount() == ElementCount::getScalable(1))
return InstructionCost::getInvalid();
switch (ICA.getID()) {
case Intrinsic::experimental_vector_histogram_add: {
InstructionCost HistCost = getHistogramCost(ST, ICA);
// If the cost isn't valid, we may still be able to scalarize
if (HistCost.isValid())
return HistCost;
break;
}
case Intrinsic::umin:
case Intrinsic::umax:
case Intrinsic::smin:
case Intrinsic::smax: {
static const auto ValidMinMaxTys = {MVT::v8i8, MVT::v16i8, MVT::v4i16,
MVT::v8i16, MVT::v2i32, MVT::v4i32,
MVT::nxv16i8, MVT::nxv8i16, MVT::nxv4i32,
MVT::nxv2i64};
auto LT = getTypeLegalizationCost(RetTy);
// v2i64 types get converted to cmp+bif hence the cost of 2
if (LT.second == MVT::v2i64)
return LT.first * 2;
if (any_of(ValidMinMaxTys, [<](MVT M) { return M == LT.second; }))
return LT.first;
break;
}
case Intrinsic::sadd_sat:
case Intrinsic::ssub_sat:
case Intrinsic::uadd_sat:
case Intrinsic::usub_sat: {
static const auto ValidSatTys = {MVT::v8i8, MVT::v16i8, MVT::v4i16,
MVT::v8i16, MVT::v2i32, MVT::v4i32,
MVT::v2i64};
auto LT = getTypeLegalizationCost(RetTy);
// This is a base cost of 1 for the vadd, plus 3 extract shifts if we
// need to extend the type, as it uses shr(qadd(shl, shl)).
unsigned Instrs =
LT.second.getScalarSizeInBits() == RetTy->getScalarSizeInBits() ? 1 : 4;
if (any_of(ValidSatTys, [<](MVT M) { return M == LT.second; }))
return LT.first * Instrs;
TypeSize TS = getDataLayout().getTypeSizeInBits(RetTy);
uint64_t VectorSize = TS.getKnownMinValue();
if (ST->isSVEAvailable() && VectorSize >= 128 && isPowerOf2_64(VectorSize))
return LT.first * Instrs;
break;
}
case Intrinsic::abs: {
static const auto ValidAbsTys = {MVT::v8i8, MVT::v16i8, MVT::v4i16,
MVT::v8i16, MVT::v2i32, MVT::v4i32,
MVT::v2i64};
auto LT = getTypeLegalizationCost(RetTy);
if (any_of(ValidAbsTys, [<](MVT M) { return M == LT.second; }))
return LT.first;
break;
}
case Intrinsic::bswap: {
static const auto ValidAbsTys = {MVT::v4i16, MVT::v8i16, MVT::v2i32,
MVT::v4i32, MVT::v2i64};
auto LT = getTypeLegalizationCost(RetTy);
if (any_of(ValidAbsTys, [<](MVT M) { return M == LT.second; }) &&
LT.second.getScalarSizeInBits() == RetTy->getScalarSizeInBits())
return LT.first;
break;
}
case Intrinsic::fma:
case Intrinsic::fmuladd: {
// Given a fma or fmuladd, cost it the same as a fmul instruction which are
// usually the same for costs. TODO: Add fp16 and bf16 expansion costs.
Type *EltTy = RetTy->getScalarType();
if (EltTy->isFloatTy() || EltTy->isDoubleTy() ||
(EltTy->isHalfTy() && ST->hasFullFP16()))
return getArithmeticInstrCost(Instruction::FMul, RetTy, CostKind);
break;
}
case Intrinsic::stepvector: {
InstructionCost Cost = 1; // Cost of the `index' instruction
auto LT = getTypeLegalizationCost(RetTy);
// Legalisation of illegal vectors involves an `index' instruction plus
// (LT.first - 1) vector adds.
if (LT.first > 1) {
Type *LegalVTy = EVT(LT.second).getTypeForEVT(RetTy->getContext());
InstructionCost AddCost =
getArithmeticInstrCost(Instruction::Add, LegalVTy, CostKind);
Cost += AddCost * (LT.first - 1);
}
return Cost;
}
case Intrinsic::vector_extract:
case Intrinsic::vector_insert: {
// If both the vector and subvector types are legal types and the index
// is 0, then this should be a no-op or simple operation; return a
// relatively low cost.
// If arguments aren't actually supplied, then we cannot determine the
// value of the index. We also want to skip predicate types.
if (ICA.getArgs().size() != ICA.getArgTypes().size() ||
ICA.getReturnType()->getScalarType()->isIntegerTy(1))
break;
LLVMContext &C = RetTy->getContext();
EVT VecVT = getTLI()->getValueType(DL, ICA.getArgTypes()[0]);
bool IsExtract = ICA.getID() == Intrinsic::vector_extract;
EVT SubVecVT = IsExtract ? getTLI()->getValueType(DL, RetTy)
: getTLI()->getValueType(DL, ICA.getArgTypes()[1]);
// Skip this if either the vector or subvector types are unpacked
// SVE types; they may get lowered to stack stores and loads.
if (isUnpackedVectorVT(VecVT) || isUnpackedVectorVT(SubVecVT))
break;
TargetLoweringBase::LegalizeKind SubVecLK =
getTLI()->getTypeConversion(C, SubVecVT);
TargetLoweringBase::LegalizeKind VecLK =
getTLI()->getTypeConversion(C, VecVT);
const Value *Idx = IsExtract ? ICA.getArgs()[1] : ICA.getArgs()[2];
const ConstantInt *CIdx = cast<ConstantInt>(Idx);
if (SubVecLK.first == TargetLoweringBase::TypeLegal &&
VecLK.first == TargetLoweringBase::TypeLegal && CIdx->isZero())
return TTI::TCC_Free;
break;
}
case Intrinsic::bitreverse: {
static const CostTblEntry BitreverseTbl[] = {
{Intrinsic::bitreverse, MVT::i32, 1},
{Intrinsic::bitreverse, MVT::i64, 1},
{Intrinsic::bitreverse, MVT::v8i8, 1},
{Intrinsic::bitreverse, MVT::v16i8, 1},
{Intrinsic::bitreverse, MVT::v4i16, 2},
{Intrinsic::bitreverse, MVT::v8i16, 2},
{Intrinsic::bitreverse, MVT::v2i32, 2},
{Intrinsic::bitreverse, MVT::v4i32, 2},
{Intrinsic::bitreverse, MVT::v1i64, 2},
{Intrinsic::bitreverse, MVT::v2i64, 2},
};
const auto LegalisationCost = getTypeLegalizationCost(RetTy);
const auto *Entry =
CostTableLookup(BitreverseTbl, ICA.getID(), LegalisationCost.second);
if (Entry) {
// Cost Model is using the legal type(i32) that i8 and i16 will be
// converted to +1 so that we match the actual lowering cost
if (TLI->getValueType(DL, RetTy, true) == MVT::i8 ||
TLI->getValueType(DL, RetTy, true) == MVT::i16)
return LegalisationCost.first * Entry->Cost + 1;
return LegalisationCost.first * Entry->Cost;
}
break;
}
case Intrinsic::ctpop: {
if (!ST->hasNEON()) {
// 32-bit or 64-bit ctpop without NEON is 12 instructions.
return getTypeLegalizationCost(RetTy).first * 12;
}
static const CostTblEntry CtpopCostTbl[] = {
{ISD::CTPOP, MVT::v2i64, 4},
{ISD::CTPOP, MVT::v4i32, 3},
{ISD::CTPOP, MVT::v8i16, 2},
{ISD::CTPOP, MVT::v16i8, 1},
{ISD::CTPOP, MVT::i64, 4},
{ISD::CTPOP, MVT::v2i32, 3},
{ISD::CTPOP, MVT::v4i16, 2},
{ISD::CTPOP, MVT::v8i8, 1},
{ISD::CTPOP, MVT::i32, 5},
};
auto LT = getTypeLegalizationCost(RetTy);
MVT MTy = LT.second;
if (const auto *Entry = CostTableLookup(CtpopCostTbl, ISD::CTPOP, MTy)) {
// Extra cost of +1 when illegal vector types are legalized by promoting
// the integer type.
int ExtraCost = MTy.isVector() && MTy.getScalarSizeInBits() !=
RetTy->getScalarSizeInBits()
? 1
: 0;
return LT.first * Entry->Cost + ExtraCost;
}
break;
}
case Intrinsic::sadd_with_overflow:
case Intrinsic::uadd_with_overflow:
case Intrinsic::ssub_with_overflow:
case Intrinsic::usub_with_overflow:
case Intrinsic::smul_with_overflow:
case Intrinsic::umul_with_overflow: {
static const CostTblEntry WithOverflowCostTbl[] = {
{Intrinsic::sadd_with_overflow, MVT::i8, 3},
{Intrinsic::uadd_with_overflow, MVT::i8, 3},
{Intrinsic::sadd_with_overflow, MVT::i16, 3},
{Intrinsic::uadd_with_overflow, MVT::i16, 3},
{Intrinsic::sadd_with_overflow, MVT::i32, 1},
{Intrinsic::uadd_with_overflow, MVT::i32, 1},
{Intrinsic::sadd_with_overflow, MVT::i64, 1},
{Intrinsic::uadd_with_overflow, MVT::i64, 1},
{Intrinsic::ssub_with_overflow, MVT::i8, 3},
{Intrinsic::usub_with_overflow, MVT::i8, 3},
{Intrinsic::ssub_with_overflow, MVT::i16, 3},
{Intrinsic::usub_with_overflow, MVT::i16, 3},
{Intrinsic::ssub_with_overflow, MVT::i32, 1},
{Intrinsic::usub_with_overflow, MVT::i32, 1},
{Intrinsic::ssub_with_overflow, MVT::i64, 1},
{Intrinsic::usub_with_overflow, MVT::i64, 1},
{Intrinsic::smul_with_overflow, MVT::i8, 5},
{Intrinsic::umul_with_overflow, MVT::i8, 4},
{Intrinsic::smul_with_overflow, MVT::i16, 5},
{Intrinsic::umul_with_overflow, MVT::i16, 4},
{Intrinsic::smul_with_overflow, MVT::i32, 2}, // eg umull;tst
{Intrinsic::umul_with_overflow, MVT::i32, 2}, // eg umull;cmp sxtw
{Intrinsic::smul_with_overflow, MVT::i64, 3}, // eg mul;smulh;cmp
{Intrinsic::umul_with_overflow, MVT::i64, 3}, // eg mul;umulh;cmp asr
};
EVT MTy = TLI->getValueType(DL, RetTy->getContainedType(0), true);
if (MTy.isSimple())
if (const auto *Entry = CostTableLookup(WithOverflowCostTbl, ICA.getID(),
MTy.getSimpleVT()))
return Entry->Cost;
break;
}
case Intrinsic::fptosi_sat:
case Intrinsic::fptoui_sat: {
if (ICA.getArgTypes().empty())
break;
bool IsSigned = ICA.getID() == Intrinsic::fptosi_sat;
auto LT = getTypeLegalizationCost(ICA.getArgTypes()[0]);
EVT MTy = TLI->getValueType(DL, RetTy);
// Check for the legal types, which are where the size of the input and the
// output are the same, or we are using cvt f64->i32 or f32->i64.
if ((LT.second == MVT::f32 || LT.second == MVT::f64 ||
LT.second == MVT::v2f32 || LT.second == MVT::v4f32 ||
LT.second == MVT::v2f64)) {
if ((LT.second.getScalarSizeInBits() == MTy.getScalarSizeInBits() ||
(LT.second == MVT::f64 && MTy == MVT::i32) ||
(LT.second == MVT::f32 && MTy == MVT::i64)))
return LT.first;
// Extending vector types v2f32->v2i64, fcvtl*2 + fcvt*2
if (LT.second.getScalarType() == MVT::f32 && MTy.isFixedLengthVector() &&
MTy.getScalarSizeInBits() == 64)
return LT.first * (MTy.getVectorNumElements() > 2 ? 4 : 2);
}
// Similarly for fp16 sizes. Without FullFP16 we generally need to fcvt to
// f32.
if (LT.second.getScalarType() == MVT::f16 && !ST->hasFullFP16())
return LT.first + getIntrinsicInstrCost(
{ICA.getID(),
RetTy,
{ICA.getArgTypes()[0]->getWithNewType(
Type::getFloatTy(RetTy->getContext()))}},
CostKind);
if ((LT.second == MVT::f16 && MTy == MVT::i32) ||
(LT.second == MVT::f16 && MTy == MVT::i64) ||
((LT.second == MVT::v4f16 || LT.second == MVT::v8f16) &&
(LT.second.getScalarSizeInBits() == MTy.getScalarSizeInBits())))
return LT.first;
// Extending vector types v8f16->v8i32, fcvtl*2 + fcvt*2
if (LT.second.getScalarType() == MVT::f16 && MTy.isFixedLengthVector() &&
MTy.getScalarSizeInBits() == 32)
return LT.first * (MTy.getVectorNumElements() > 4 ? 4 : 2);
// Extending vector types v8f16->v8i32. These current scalarize but the
// codegen could be better.
if (LT.second.getScalarType() == MVT::f16 && MTy.isFixedLengthVector() &&
MTy.getScalarSizeInBits() == 64)
return MTy.getVectorNumElements() * 3;
// If we can we use a legal convert followed by a min+max
if ((LT.second.getScalarType() == MVT::f32 ||
LT.second.getScalarType() == MVT::f64 ||
LT.second.getScalarType() == MVT::f16) &&
LT.second.getScalarSizeInBits() >= MTy.getScalarSizeInBits()) {
Type *LegalTy =
Type::getIntNTy(RetTy->getContext(), LT.second.getScalarSizeInBits());
if (LT.second.isVector())
LegalTy = VectorType::get(LegalTy, LT.second.getVectorElementCount());
InstructionCost Cost = 1;
IntrinsicCostAttributes Attrs1(IsSigned ? Intrinsic::smin : Intrinsic::umin,
LegalTy, {LegalTy, LegalTy});
Cost += getIntrinsicInstrCost(Attrs1, CostKind);
IntrinsicCostAttributes Attrs2(IsSigned ? Intrinsic::smax : Intrinsic::umax,
LegalTy, {LegalTy, LegalTy});
Cost += getIntrinsicInstrCost(Attrs2, CostKind);
return LT.first * Cost +
((LT.second.getScalarType() != MVT::f16 || ST->hasFullFP16()) ? 0
: 1);
}
// Otherwise we need to follow the default expansion that clamps the value
// using a float min/max with a fcmp+sel for nan handling when signed.
Type *FPTy = ICA.getArgTypes()[0]->getScalarType();
RetTy = RetTy->getScalarType();
if (LT.second.isVector()) {
FPTy = VectorType::get(FPTy, LT.second.getVectorElementCount());
RetTy = VectorType::get(RetTy, LT.second.getVectorElementCount());
}
IntrinsicCostAttributes Attrs1(Intrinsic::minnum, FPTy, {FPTy, FPTy});
InstructionCost Cost = getIntrinsicInstrCost(Attrs1, CostKind);
IntrinsicCostAttributes Attrs2(Intrinsic::maxnum, FPTy, {FPTy, FPTy});
Cost += getIntrinsicInstrCost(Attrs2, CostKind);
Cost +=
getCastInstrCost(IsSigned ? Instruction::FPToSI : Instruction::FPToUI,
RetTy, FPTy, TTI::CastContextHint::None, CostKind);
if (IsSigned) {
Type *CondTy = RetTy->getWithNewBitWidth(1);
Cost += getCmpSelInstrCost(BinaryOperator::FCmp, FPTy, CondTy,
CmpInst::FCMP_UNO, CostKind);
Cost += getCmpSelInstrCost(BinaryOperator::Select, RetTy, CondTy,
CmpInst::FCMP_UNO, CostKind);
}
return LT.first * Cost;
}
case Intrinsic::fshl:
case Intrinsic::fshr: {
if (ICA.getArgs().empty())
break;
// TODO: Add handling for fshl where third argument is not a constant.
const TTI::OperandValueInfo OpInfoZ = TTI::getOperandInfo(ICA.getArgs()[2]);
if (!OpInfoZ.isConstant())
break;
const auto LegalisationCost = getTypeLegalizationCost(RetTy);
if (OpInfoZ.isUniform()) {
static const CostTblEntry FshlTbl[] = {
{Intrinsic::fshl, MVT::v4i32, 2}, // shl + usra
{Intrinsic::fshl, MVT::v2i64, 2}, {Intrinsic::fshl, MVT::v16i8, 2},
{Intrinsic::fshl, MVT::v8i16, 2}, {Intrinsic::fshl, MVT::v2i32, 2},
{Intrinsic::fshl, MVT::v8i8, 2}, {Intrinsic::fshl, MVT::v4i16, 2}};
// Costs for both fshl & fshr are the same, so just pass Intrinsic::fshl
// to avoid having to duplicate the costs.
const auto *Entry =
CostTableLookup(FshlTbl, Intrinsic::fshl, LegalisationCost.second);
if (Entry)
return LegalisationCost.first * Entry->Cost;
}
auto TyL = getTypeLegalizationCost(RetTy);
if (!RetTy->isIntegerTy())
break;
// Estimate cost manually, as types like i8 and i16 will get promoted to
// i32 and CostTableLookup will ignore the extra conversion cost.
bool HigherCost = (RetTy->getScalarSizeInBits() != 32 &&
RetTy->getScalarSizeInBits() < 64) ||
(RetTy->getScalarSizeInBits() % 64 != 0);
unsigned ExtraCost = HigherCost ? 1 : 0;
if (RetTy->getScalarSizeInBits() == 32 ||
RetTy->getScalarSizeInBits() == 64)
ExtraCost = 0; // fhsl/fshr for i32 and i64 can be lowered to a single
// extr instruction.
else if (HigherCost)
ExtraCost = 1;
else
break;
return TyL.first + ExtraCost;
}
case Intrinsic::get_active_lane_mask: {
auto *RetTy = dyn_cast<FixedVectorType>(ICA.getReturnType());
if (RetTy) {
EVT RetVT = getTLI()->getValueType(DL, RetTy);
EVT OpVT = getTLI()->getValueType(DL, ICA.getArgTypes()[0]);
if (!getTLI()->shouldExpandGetActiveLaneMask(RetVT, OpVT) &&
!getTLI()->isTypeLegal(RetVT)) {
// We don't have enough context at this point to determine if the mask
// is going to be kept live after the block, which will force the vXi1
// type to be expanded to legal vectors of integers, e.g. v4i1->v4i32.
// For now, we just assume the vectorizer created this intrinsic and
// the result will be the input for a PHI. In this case the cost will
// be extremely high for fixed-width vectors.
// NOTE: getScalarizationOverhead returns a cost that's far too
// pessimistic for the actual generated codegen. In reality there are
// two instructions generated per lane.
return RetTy->getNumElements() * 2;
}
}
break;
}
case Intrinsic::experimental_vector_match: {
auto *NeedleTy = cast<FixedVectorType>(ICA.getArgTypes()[1]);
EVT SearchVT = getTLI()->getValueType(DL, ICA.getArgTypes()[0]);
unsigned SearchSize = NeedleTy->getNumElements();
if (!getTLI()->shouldExpandVectorMatch(SearchVT, SearchSize)) {
// Base cost for MATCH instructions. At least on the Neoverse V2 and
// Neoverse V3, these are cheap operations with the same latency as a
// vector ADD. In most cases, however, we also need to do an extra DUP.
// For fixed-length vectors we currently need an extra five--six
// instructions besides the MATCH.
InstructionCost Cost = 4;
if (isa<FixedVectorType>(RetTy))
Cost += 10;
return Cost;
}
break;
}
case Intrinsic::experimental_cttz_elts: {
EVT ArgVT = getTLI()->getValueType(DL, ICA.getArgTypes()[0]);
if (!getTLI()->shouldExpandCttzElements(ArgVT)) {
// This will consist of a SVE brkb and a cntp instruction. These
// typically have the same latency and half the throughput as a vector
// add instruction.
return 4;
}
break;
}
default:
break;
}
return BaseT::getIntrinsicInstrCost(ICA, CostKind);
}
/// The function will remove redundant reinterprets casting in the presence
/// of the control flow
static std::optional<Instruction *> processPhiNode(InstCombiner &IC,
IntrinsicInst &II) {
SmallVector<Instruction *, 32> Worklist;
auto RequiredType = II.getType();
auto *PN = dyn_cast<PHINode>(II.getArgOperand(0));
assert(PN && "Expected Phi Node!");
// Don't create a new Phi unless we can remove the old one.
if (!PN->hasOneUse())
return std::nullopt;
for (Value *IncValPhi : PN->incoming_values()) {
auto *Reinterpret = dyn_cast<IntrinsicInst>(IncValPhi);
if (!Reinterpret ||
Reinterpret->getIntrinsicID() !=
Intrinsic::aarch64_sve_convert_to_svbool ||
RequiredType != Reinterpret->getArgOperand(0)->getType())
return std::nullopt;
}
// Create the new Phi
IC.Builder.SetInsertPoint(PN);
PHINode *NPN = IC.Builder.CreatePHI(RequiredType, PN->getNumIncomingValues());
Worklist.push_back(PN);
for (unsigned I = 0; I < PN->getNumIncomingValues(); I++) {
auto *Reinterpret = cast<Instruction>(PN->getIncomingValue(I));
NPN->addIncoming(Reinterpret->getOperand(0), PN->getIncomingBlock(I));
Worklist.push_back(Reinterpret);
}
// Cleanup Phi Node and reinterprets
return IC.replaceInstUsesWith(II, NPN);
}
// A collection of properties common to SVE intrinsics that allow for combines
// to be written without needing to know the specific intrinsic.
struct SVEIntrinsicInfo {
//
// Helper routines for common intrinsic definitions.
//
// e.g. llvm.aarch64.sve.add pg, op1, op2
// with IID ==> llvm.aarch64.sve.add_u
static SVEIntrinsicInfo
defaultMergingOp(Intrinsic::ID IID = Intrinsic::not_intrinsic) {
return SVEIntrinsicInfo()
.setGoverningPredicateOperandIdx(0)
.setOperandIdxInactiveLanesTakenFrom(1)
.setMatchingUndefIntrinsic(IID);
}
// e.g. llvm.aarch64.sve.neg inactive, pg, op
static SVEIntrinsicInfo defaultMergingUnaryOp() {
return SVEIntrinsicInfo()
.setGoverningPredicateOperandIdx(1)
.setOperandIdxInactiveLanesTakenFrom(0)
.setOperandIdxWithNoActiveLanes(0);
}
// e.g. llvm.aarch64.sve.fcvtnt inactive, pg, op
static SVEIntrinsicInfo defaultMergingUnaryNarrowingTopOp() {
return SVEIntrinsicInfo()
.setGoverningPredicateOperandIdx(1)
.setOperandIdxInactiveLanesTakenFrom(0);
}
// e.g. llvm.aarch64.sve.add_u pg, op1, op2
static SVEIntrinsicInfo defaultUndefOp() {
return SVEIntrinsicInfo()
.setGoverningPredicateOperandIdx(0)
.setInactiveLanesAreNotDefined();
}
// e.g. llvm.aarch64.sve.prf pg, ptr (GPIndex = 0)
// llvm.aarch64.sve.st1 data, pg, ptr (GPIndex = 1)
static SVEIntrinsicInfo defaultVoidOp(unsigned GPIndex) {
return SVEIntrinsicInfo()
.setGoverningPredicateOperandIdx(GPIndex)
.setInactiveLanesAreUnused();
}
// e.g. llvm.aarch64.sve.cmpeq pg, op1, op2
// llvm.aarch64.sve.ld1 pg, ptr
static SVEIntrinsicInfo defaultZeroingOp() {
return SVEIntrinsicInfo()
.setGoverningPredicateOperandIdx(0)
.setInactiveLanesAreUnused()
.setResultIsZeroInitialized();
}
// All properties relate to predication and thus having a general predicate
// is the minimum requirement to say there is intrinsic info to act on.
explicit operator bool() const { return hasGoverningPredicate(); }
//
// Properties relating to the governing predicate.
//
bool hasGoverningPredicate() const {
return GoverningPredicateIdx != std::numeric_limits<unsigned>::max();
}
unsigned getGoverningPredicateOperandIdx() const {
assert(hasGoverningPredicate() && "Propery not set!");
return GoverningPredicateIdx;
}
SVEIntrinsicInfo &setGoverningPredicateOperandIdx(unsigned Index) {
assert(!hasGoverningPredicate() && "Cannot set property twice!");
GoverningPredicateIdx = Index;
return *this;
}
//
// Properties relating to operations the intrinsic could be transformed into.
// NOTE: This does not mean such a transformation is always possible, but the
// knowledge makes it possible to reuse existing optimisations without needing
// to embed specific handling for each intrinsic. For example, instruction
// simplification can be used to optimise an intrinsic's active lanes.
//
bool hasMatchingUndefIntrinsic() const {
return UndefIntrinsic != Intrinsic::not_intrinsic;
}
Intrinsic::ID getMatchingUndefIntrinsic() const {
assert(hasMatchingUndefIntrinsic() && "Propery not set!");
return UndefIntrinsic;
}
SVEIntrinsicInfo &setMatchingUndefIntrinsic(Intrinsic::ID IID) {
assert(!hasMatchingUndefIntrinsic() && "Cannot set property twice!");
UndefIntrinsic = IID;
return *this;
}
bool hasMatchingIROpode() const { return IROpcode != 0; }
unsigned getMatchingIROpode() const {
assert(hasMatchingIROpode() && "Propery not set!");
return IROpcode;
}
SVEIntrinsicInfo &setMatchingIROpcode(unsigned Opcode) {
assert(!hasMatchingIROpode() && "Cannot set property twice!");
IROpcode = Opcode;
return *this;
}
//
// Properties relating to the result of inactive lanes.
//
bool inactiveLanesTakenFromOperand() const {
return ResultLanes == InactiveLanesTakenFromOperand;
}
unsigned getOperandIdxInactiveLanesTakenFrom() const {
assert(inactiveLanesTakenFromOperand() && "Propery not set!");
return OperandIdxForInactiveLanes;
}
SVEIntrinsicInfo &setOperandIdxInactiveLanesTakenFrom(unsigned Index) {
assert(ResultLanes == Uninitialized && "Cannot set property twice!");
ResultLanes = InactiveLanesTakenFromOperand;
OperandIdxForInactiveLanes = Index;
return *this;
}
bool inactiveLanesAreNotDefined() const {
return ResultLanes == InactiveLanesAreNotDefined;
}
SVEIntrinsicInfo &setInactiveLanesAreNotDefined() {
assert(ResultLanes == Uninitialized && "Cannot set property twice!");
ResultLanes = InactiveLanesAreNotDefined;
return *this;
}
bool inactiveLanesAreUnused() const {
return ResultLanes == InactiveLanesAreUnused;
}
SVEIntrinsicInfo &setInactiveLanesAreUnused() {
assert(ResultLanes == Uninitialized && "Cannot set property twice!");
ResultLanes = InactiveLanesAreUnused;
return *this;
}
// NOTE: Whilst not limited to only inactive lanes, the common use case is:
// inactiveLanesAreZeroed =
// resultIsZeroInitialized() && inactiveLanesAreUnused()
bool resultIsZeroInitialized() const { return ResultIsZeroInitialized; }
SVEIntrinsicInfo &setResultIsZeroInitialized() {
ResultIsZeroInitialized = true;
return *this;
}
//
// The first operand of unary merging operations is typically only used to
// set the result for inactive lanes. Knowing this allows us to deadcode the
// operand when we can prove there are no inactive lanes.
//
bool hasOperandWithNoActiveLanes() const {
return OperandIdxWithNoActiveLanes != std::numeric_limits<unsigned>::max();
}
unsigned getOperandIdxWithNoActiveLanes() const {
assert(hasOperandWithNoActiveLanes() && "Propery not set!");
return OperandIdxWithNoActiveLanes;
}
SVEIntrinsicInfo &setOperandIdxWithNoActiveLanes(unsigned Index) {
assert(!hasOperandWithNoActiveLanes() && "Cannot set property twice!");
OperandIdxWithNoActiveLanes = Index;
return *this;
}
private:
unsigned GoverningPredicateIdx = std::numeric_limits<unsigned>::max();
Intrinsic::ID UndefIntrinsic = Intrinsic::not_intrinsic;
unsigned IROpcode = 0;
enum PredicationStyle {
Uninitialized,
InactiveLanesTakenFromOperand,
InactiveLanesAreNotDefined,
InactiveLanesAreUnused
} ResultLanes = Uninitialized;
bool ResultIsZeroInitialized = false;
unsigned OperandIdxForInactiveLanes = std::numeric_limits<unsigned>::max();
unsigned OperandIdxWithNoActiveLanes = std::numeric_limits<unsigned>::max();
};
static SVEIntrinsicInfo constructSVEIntrinsicInfo(IntrinsicInst &II) {
// Some SVE intrinsics do not use scalable vector types, but since they are
// not relevant from an SVEIntrinsicInfo perspective, they are also ignored.
if (!isa<ScalableVectorType>(II.getType()) &&
all_of(II.args(), [&](const Value *V) {
return !isa<ScalableVectorType>(V->getType());
}))
return SVEIntrinsicInfo();
Intrinsic::ID IID = II.getIntrinsicID();
switch (IID) {
default:
break;
case Intrinsic::aarch64_sve_fcvt_bf16f32_v2:
case Intrinsic::aarch64_sve_fcvt_f16f32:
case Intrinsic::aarch64_sve_fcvt_f16f64:
case Intrinsic::aarch64_sve_fcvt_f32f16:
case Intrinsic::aarch64_sve_fcvt_f32f64:
case Intrinsic::aarch64_sve_fcvt_f64f16:
case Intrinsic::aarch64_sve_fcvt_f64f32:
case Intrinsic::aarch64_sve_fcvtlt_f32f16:
case Intrinsic::aarch64_sve_fcvtlt_f64f32:
case Intrinsic::aarch64_sve_fcvtx_f32f64:
case Intrinsic::aarch64_sve_fcvtzs:
case Intrinsic::aarch64_sve_fcvtzs_i32f16:
case Intrinsic::aarch64_sve_fcvtzs_i32f64:
case Intrinsic::aarch64_sve_fcvtzs_i64f16:
case Intrinsic::aarch64_sve_fcvtzs_i64f32:
case Intrinsic::aarch64_sve_fcvtzu:
case Intrinsic::aarch64_sve_fcvtzu_i32f16:
case Intrinsic::aarch64_sve_fcvtzu_i32f64:
case Intrinsic::aarch64_sve_fcvtzu_i64f16:
case Intrinsic::aarch64_sve_fcvtzu_i64f32:
case Intrinsic::aarch64_sve_scvtf:
case Intrinsic::aarch64_sve_scvtf_f16i32:
case Intrinsic::aarch64_sve_scvtf_f16i64:
case Intrinsic::aarch64_sve_scvtf_f32i64:
case Intrinsic::aarch64_sve_scvtf_f64i32:
case Intrinsic::aarch64_sve_ucvtf:
case Intrinsic::aarch64_sve_ucvtf_f16i32:
case Intrinsic::aarch64_sve_ucvtf_f16i64:
case Intrinsic::aarch64_sve_ucvtf_f32i64:
case Intrinsic::aarch64_sve_ucvtf_f64i32:
return SVEIntrinsicInfo::defaultMergingUnaryOp();
case Intrinsic::aarch64_sve_fcvtnt_bf16f32_v2:
case Intrinsic::aarch64_sve_fcvtnt_f16f32:
case Intrinsic::aarch64_sve_fcvtnt_f32f64:
case Intrinsic::aarch64_sve_fcvtxnt_f32f64:
return SVEIntrinsicInfo::defaultMergingUnaryNarrowingTopOp();
case Intrinsic::aarch64_sve_fabd:
return SVEIntrinsicInfo::defaultMergingOp(Intrinsic::aarch64_sve_fabd_u);
case Intrinsic::aarch64_sve_fadd:
return SVEIntrinsicInfo::defaultMergingOp(Intrinsic::aarch64_sve_fadd_u)
.setMatchingIROpcode(Instruction::FAdd);
case Intrinsic::aarch64_sve_fdiv:
return SVEIntrinsicInfo::defaultMergingOp(Intrinsic::aarch64_sve_fdiv_u)
.setMatchingIROpcode(Instruction::FDiv);
case Intrinsic::aarch64_sve_fmax:
return SVEIntrinsicInfo::defaultMergingOp(Intrinsic::aarch64_sve_fmax_u);
case Intrinsic::aarch64_sve_fmaxnm:
return SVEIntrinsicInfo::defaultMergingOp(Intrinsic::aarch64_sve_fmaxnm_u);
case Intrinsic::aarch64_sve_fmin:
return SVEIntrinsicInfo::defaultMergingOp(Intrinsic::aarch64_sve_fmin_u);
case Intrinsic::aarch64_sve_fminnm:
return SVEIntrinsicInfo::defaultMergingOp(Intrinsic::aarch64_sve_fminnm_u);
case Intrinsic::aarch64_sve_fmla:
return SVEIntrinsicInfo::defaultMergingOp(Intrinsic::aarch64_sve_fmla_u);
case Intrinsic::aarch64_sve_fmls:
return SVEIntrinsicInfo::defaultMergingOp(Intrinsic::aarch64_sve_fmls_u);
case Intrinsic::aarch64_sve_fmul:
return SVEIntrinsicInfo::defaultMergingOp(Intrinsic::aarch64_sve_fmul_u)
.setMatchingIROpcode(Instruction::FMul);
case Intrinsic::aarch64_sve_fmulx:
return SVEIntrinsicInfo::defaultMergingOp(Intrinsic::aarch64_sve_fmulx_u);
case Intrinsic::aarch64_sve_fnmla:
return SVEIntrinsicInfo::defaultMergingOp(Intrinsic::aarch64_sve_fnmla_u);
case Intrinsic::aarch64_sve_fnmls:
return SVEIntrinsicInfo::defaultMergingOp(Intrinsic::aarch64_sve_fnmls_u);
case Intrinsic::aarch64_sve_fsub:
return SVEIntrinsicInfo::defaultMergingOp(Intrinsic::aarch64_sve_fsub_u)
.setMatchingIROpcode(Instruction::FSub);
case Intrinsic::aarch64_sve_add:
return SVEIntrinsicInfo::defaultMergingOp(Intrinsic::aarch64_sve_add_u)
.setMatchingIROpcode(Instruction::Add);
case Intrinsic::aarch64_sve_mla:
return SVEIntrinsicInfo::defaultMergingOp(Intrinsic::aarch64_sve_mla_u);
case Intrinsic::aarch64_sve_mls:
return SVEIntrinsicInfo::defaultMergingOp(Intrinsic::aarch64_sve_mls_u);
case Intrinsic::aarch64_sve_mul:
return SVEIntrinsicInfo::defaultMergingOp(Intrinsic::aarch64_sve_mul_u)
.setMatchingIROpcode(Instruction::Mul);
case Intrinsic::aarch64_sve_sabd:
return SVEIntrinsicInfo::defaultMergingOp(Intrinsic::aarch64_sve_sabd_u);
case Intrinsic::aarch64_sve_sdiv:
return SVEIntrinsicInfo::defaultMergingOp(Intrinsic::aarch64_sve_sdiv_u)
.setMatchingIROpcode(Instruction::SDiv);
case Intrinsic::aarch64_sve_smax:
return SVEIntrinsicInfo::defaultMergingOp(Intrinsic::aarch64_sve_smax_u);
case Intrinsic::aarch64_sve_smin:
return SVEIntrinsicInfo::defaultMergingOp(Intrinsic::aarch64_sve_smin_u);
case Intrinsic::aarch64_sve_smulh:
return SVEIntrinsicInfo::defaultMergingOp(Intrinsic::aarch64_sve_smulh_u);
case Intrinsic::aarch64_sve_sub:
return SVEIntrinsicInfo::defaultMergingOp(Intrinsic::aarch64_sve_sub_u)
.setMatchingIROpcode(Instruction::Sub);
case Intrinsic::aarch64_sve_uabd:
return SVEIntrinsicInfo::defaultMergingOp(Intrinsic::aarch64_sve_uabd_u);
case Intrinsic::aarch64_sve_udiv:
return SVEIntrinsicInfo::defaultMergingOp(Intrinsic::aarch64_sve_udiv_u)
.setMatchingIROpcode(Instruction::UDiv);
case Intrinsic::aarch64_sve_umax:
return SVEIntrinsicInfo::defaultMergingOp(Intrinsic::aarch64_sve_umax_u);
case Intrinsic::aarch64_sve_umin:
return SVEIntrinsicInfo::defaultMergingOp(Intrinsic::aarch64_sve_umin_u);
case Intrinsic::aarch64_sve_umulh:
return SVEIntrinsicInfo::defaultMergingOp(Intrinsic::aarch64_sve_umulh_u);
case Intrinsic::aarch64_sve_asr:
return SVEIntrinsicInfo::defaultMergingOp(Intrinsic::aarch64_sve_asr_u)
.setMatchingIROpcode(Instruction::AShr);
case Intrinsic::aarch64_sve_lsl:
return SVEIntrinsicInfo::defaultMergingOp(Intrinsic::aarch64_sve_lsl_u)
.setMatchingIROpcode(Instruction::Shl);
case Intrinsic::aarch64_sve_lsr:
return SVEIntrinsicInfo::defaultMergingOp(Intrinsic::aarch64_sve_lsr_u)
.setMatchingIROpcode(Instruction::LShr);
case Intrinsic::aarch64_sve_and:
return SVEIntrinsicInfo::defaultMergingOp(Intrinsic::aarch64_sve_and_u)
.setMatchingIROpcode(Instruction::And);
case Intrinsic::aarch64_sve_bic:
return SVEIntrinsicInfo::defaultMergingOp(Intrinsic::aarch64_sve_bic_u);
case Intrinsic::aarch64_sve_eor:
return SVEIntrinsicInfo::defaultMergingOp(Intrinsic::aarch64_sve_eor_u)
.setMatchingIROpcode(Instruction::Xor);
case Intrinsic::aarch64_sve_orr:
return SVEIntrinsicInfo::defaultMergingOp(Intrinsic::aarch64_sve_orr_u)
.setMatchingIROpcode(Instruction::Or);
case Intrinsic::aarch64_sve_sqsub:
return SVEIntrinsicInfo::defaultMergingOp(Intrinsic::aarch64_sve_sqsub_u);
case Intrinsic::aarch64_sve_uqsub:
return SVEIntrinsicInfo::defaultMergingOp(Intrinsic::aarch64_sve_uqsub_u);
case Intrinsic::aarch64_sve_add_u:
return SVEIntrinsicInfo::defaultUndefOp().setMatchingIROpcode(
Instruction::Add);
case Intrinsic::aarch64_sve_and_u:
return SVEIntrinsicInfo::defaultUndefOp().setMatchingIROpcode(
Instruction::And);
case Intrinsic::aarch64_sve_asr_u:
return SVEIntrinsicInfo::defaultUndefOp().setMatchingIROpcode(
Instruction::AShr);
case Intrinsic::aarch64_sve_eor_u:
return SVEIntrinsicInfo::defaultUndefOp().setMatchingIROpcode(
Instruction::Xor);
case Intrinsic::aarch64_sve_fadd_u:
return SVEIntrinsicInfo::defaultUndefOp().setMatchingIROpcode(
Instruction::FAdd);
case Intrinsic::aarch64_sve_fdiv_u:
return SVEIntrinsicInfo::defaultUndefOp().setMatchingIROpcode(
Instruction::FDiv);
case Intrinsic::aarch64_sve_fmul_u:
return SVEIntrinsicInfo::defaultUndefOp().setMatchingIROpcode(
Instruction::FMul);
case Intrinsic::aarch64_sve_fsub_u:
return SVEIntrinsicInfo::defaultUndefOp().setMatchingIROpcode(
Instruction::FSub);
case Intrinsic::aarch64_sve_lsl_u:
return SVEIntrinsicInfo::defaultUndefOp().setMatchingIROpcode(
Instruction::Shl);
case Intrinsic::aarch64_sve_lsr_u:
return SVEIntrinsicInfo::defaultUndefOp().setMatchingIROpcode(
Instruction::LShr);
case Intrinsic::aarch64_sve_mul_u:
return SVEIntrinsicInfo::defaultUndefOp().setMatchingIROpcode(
Instruction::Mul);
case Intrinsic::aarch64_sve_orr_u:
return SVEIntrinsicInfo::defaultUndefOp().setMatchingIROpcode(
Instruction::Or);
case Intrinsic::aarch64_sve_sdiv_u:
return SVEIntrinsicInfo::defaultUndefOp().setMatchingIROpcode(
Instruction::SDiv);
case Intrinsic::aarch64_sve_sub_u:
return SVEIntrinsicInfo::defaultUndefOp().setMatchingIROpcode(
Instruction::Sub);
case Intrinsic::aarch64_sve_udiv_u:
return SVEIntrinsicInfo::defaultUndefOp().setMatchingIROpcode(
Instruction::UDiv);
case Intrinsic::aarch64_sve_addqv:
case Intrinsic::aarch64_sve_and_z:
case Intrinsic::aarch64_sve_bic_z:
case Intrinsic::aarch64_sve_brka_z:
case Intrinsic::aarch64_sve_brkb_z:
case Intrinsic::aarch64_sve_brkn_z:
case Intrinsic::aarch64_sve_brkpa_z:
case Intrinsic::aarch64_sve_brkpb_z:
case Intrinsic::aarch64_sve_cntp:
case Intrinsic::aarch64_sve_compact:
case Intrinsic::aarch64_sve_eor_z:
case Intrinsic::aarch64_sve_eorv:
case Intrinsic::aarch64_sve_eorqv:
case Intrinsic::aarch64_sve_nand_z:
case Intrinsic::aarch64_sve_nor_z:
case Intrinsic::aarch64_sve_orn_z:
case Intrinsic::aarch64_sve_orr_z:
case Intrinsic::aarch64_sve_orv:
case Intrinsic::aarch64_sve_orqv:
case Intrinsic::aarch64_sve_pnext:
case Intrinsic::aarch64_sve_rdffr_z:
case Intrinsic::aarch64_sve_saddv:
case Intrinsic::aarch64_sve_uaddv:
case Intrinsic::aarch64_sve_umaxv:
case Intrinsic::aarch64_sve_umaxqv:
case Intrinsic::aarch64_sve_cmpeq:
case Intrinsic::aarch64_sve_cmpeq_wide:
case Intrinsic::aarch64_sve_cmpge:
case Intrinsic::aarch64_sve_cmpge_wide:
case Intrinsic::aarch64_sve_cmpgt:
case Intrinsic::aarch64_sve_cmpgt_wide:
case Intrinsic::aarch64_sve_cmphi:
case Intrinsic::aarch64_sve_cmphi_wide:
case Intrinsic::aarch64_sve_cmphs:
case Intrinsic::aarch64_sve_cmphs_wide:
case Intrinsic::aarch64_sve_cmple_wide:
case Intrinsic::aarch64_sve_cmplo_wide:
case Intrinsic::aarch64_sve_cmpls_wide:
case Intrinsic::aarch64_sve_cmplt_wide:
case Intrinsic::aarch64_sve_cmpne:
case Intrinsic::aarch64_sve_cmpne_wide:
case Intrinsic::aarch64_sve_facge:
case Intrinsic::aarch64_sve_facgt:
case Intrinsic::aarch64_sve_fcmpeq:
case Intrinsic::aarch64_sve_fcmpge:
case Intrinsic::aarch64_sve_fcmpgt:
case Intrinsic::aarch64_sve_fcmpne:
case Intrinsic::aarch64_sve_fcmpuo:
case Intrinsic::aarch64_sve_ld1:
case Intrinsic::aarch64_sve_ld1_gather:
case Intrinsic::aarch64_sve_ld1_gather_index:
case Intrinsic::aarch64_sve_ld1_gather_scalar_offset:
case Intrinsic::aarch64_sve_ld1_gather_sxtw:
case Intrinsic::aarch64_sve_ld1_gather_sxtw_index:
case Intrinsic::aarch64_sve_ld1_gather_uxtw:
case Intrinsic::aarch64_sve_ld1_gather_uxtw_index:
case Intrinsic::aarch64_sve_ld1q_gather_index:
case Intrinsic::aarch64_sve_ld1q_gather_scalar_offset:
case Intrinsic::aarch64_sve_ld1q_gather_vector_offset:
case Intrinsic::aarch64_sve_ld1ro:
case Intrinsic::aarch64_sve_ld1rq:
case Intrinsic::aarch64_sve_ld1udq:
case Intrinsic::aarch64_sve_ld1uwq:
case Intrinsic::aarch64_sve_ld2_sret:
case Intrinsic::aarch64_sve_ld2q_sret:
case Intrinsic::aarch64_sve_ld3_sret:
case Intrinsic::aarch64_sve_ld3q_sret:
case Intrinsic::aarch64_sve_ld4_sret:
case Intrinsic::aarch64_sve_ld4q_sret:
case Intrinsic::aarch64_sve_ldff1:
case Intrinsic::aarch64_sve_ldff1_gather:
case Intrinsic::aarch64_sve_ldff1_gather_index:
case Intrinsic::aarch64_sve_ldff1_gather_scalar_offset:
case Intrinsic::aarch64_sve_ldff1_gather_sxtw:
case Intrinsic::aarch64_sve_ldff1_gather_sxtw_index:
case Intrinsic::aarch64_sve_ldff1_gather_uxtw:
case Intrinsic::aarch64_sve_ldff1_gather_uxtw_index:
case Intrinsic::aarch64_sve_ldnf1:
case Intrinsic::aarch64_sve_ldnt1:
case Intrinsic::aarch64_sve_ldnt1_gather:
case Intrinsic::aarch64_sve_ldnt1_gather_index:
case Intrinsic::aarch64_sve_ldnt1_gather_scalar_offset:
case Intrinsic::aarch64_sve_ldnt1_gather_uxtw:
return SVEIntrinsicInfo::defaultZeroingOp();
case Intrinsic::aarch64_sve_prf:
case Intrinsic::aarch64_sve_prfb_gather_index:
case Intrinsic::aarch64_sve_prfb_gather_scalar_offset:
case Intrinsic::aarch64_sve_prfb_gather_sxtw_index:
case Intrinsic::aarch64_sve_prfb_gather_uxtw_index:
case Intrinsic::aarch64_sve_prfd_gather_index:
case Intrinsic::aarch64_sve_prfd_gather_scalar_offset:
case Intrinsic::aarch64_sve_prfd_gather_sxtw_index:
case Intrinsic::aarch64_sve_prfd_gather_uxtw_index:
case Intrinsic::aarch64_sve_prfh_gather_index:
case Intrinsic::aarch64_sve_prfh_gather_scalar_offset:
case Intrinsic::aarch64_sve_prfh_gather_sxtw_index:
case Intrinsic::aarch64_sve_prfh_gather_uxtw_index:
case Intrinsic::aarch64_sve_prfw_gather_index:
case Intrinsic::aarch64_sve_prfw_gather_scalar_offset:
case Intrinsic::aarch64_sve_prfw_gather_sxtw_index:
case Intrinsic::aarch64_sve_prfw_gather_uxtw_index:
return SVEIntrinsicInfo::defaultVoidOp(0);
case Intrinsic::aarch64_sve_st1_scatter:
case Intrinsic::aarch64_sve_st1_scatter_scalar_offset:
case Intrinsic::aarch64_sve_st1_scatter_sxtw:
case Intrinsic::aarch64_sve_st1_scatter_sxtw_index:
case Intrinsic::aarch64_sve_st1_scatter_uxtw:
case Intrinsic::aarch64_sve_st1_scatter_uxtw_index:
case Intrinsic::aarch64_sve_st1dq:
case Intrinsic::aarch64_sve_st1q_scatter_index:
case Intrinsic::aarch64_sve_st1q_scatter_scalar_offset:
case Intrinsic::aarch64_sve_st1q_scatter_vector_offset:
case Intrinsic::aarch64_sve_st1wq:
case Intrinsic::aarch64_sve_stnt1:
case Intrinsic::aarch64_sve_stnt1_scatter:
case Intrinsic::aarch64_sve_stnt1_scatter_index:
case Intrinsic::aarch64_sve_stnt1_scatter_scalar_offset:
case Intrinsic::aarch64_sve_stnt1_scatter_uxtw:
return SVEIntrinsicInfo::defaultVoidOp(1);
case Intrinsic::aarch64_sve_st2:
case Intrinsic::aarch64_sve_st2q:
return SVEIntrinsicInfo::defaultVoidOp(2);
case Intrinsic::aarch64_sve_st3:
case Intrinsic::aarch64_sve_st3q:
return SVEIntrinsicInfo::defaultVoidOp(3);
case Intrinsic::aarch64_sve_st4:
case Intrinsic::aarch64_sve_st4q:
return SVEIntrinsicInfo::defaultVoidOp(4);
}
return SVEIntrinsicInfo();
}
static bool isAllActivePredicate(Value *Pred) {
// Look through convert.from.svbool(convert.to.svbool(...) chain.
Value *UncastedPred;
if (match(Pred, m_Intrinsic<Intrinsic::aarch64_sve_convert_from_svbool>(
m_Intrinsic<Intrinsic::aarch64_sve_convert_to_svbool>(
m_Value(UncastedPred)))))
// If the predicate has the same or less lanes than the uncasted
// predicate then we know the casting has no effect.
if (cast<ScalableVectorType>(Pred->getType())->getMinNumElements() <=
cast<ScalableVectorType>(UncastedPred->getType())->getMinNumElements())
Pred = UncastedPred;
auto *C = dyn_cast<Constant>(Pred);
return (C && C->isAllOnesValue());
}
// Simplify `V` by only considering the operations that affect active lanes.
// This function should only return existing Values or newly created Constants.
static Value *stripInactiveLanes(Value *V, const Value *Pg) {
auto *Dup = dyn_cast<IntrinsicInst>(V);
if (Dup && Dup->getIntrinsicID() == Intrinsic::aarch64_sve_dup &&
Dup->getOperand(1) == Pg && isa<Constant>(Dup->getOperand(2)))
return ConstantVector::getSplat(
cast<VectorType>(V->getType())->getElementCount(),
cast<Constant>(Dup->getOperand(2)));
return V;
}
static std::optional<Instruction *>
simplifySVEIntrinsicBinOp(InstCombiner &IC, IntrinsicInst &II,
const SVEIntrinsicInfo &IInfo) {
const unsigned Opc = IInfo.getMatchingIROpode();
assert(Instruction::isBinaryOp(Opc) && "Expected a binary operation!");
Value *Pg = II.getOperand(0);
Value *Op1 = II.getOperand(1);
Value *Op2 = II.getOperand(2);
const DataLayout &DL = II.getDataLayout();
// Canonicalise constants to the RHS.
if (Instruction::isCommutative(Opc) && IInfo.inactiveLanesAreNotDefined() &&
isa<Constant>(Op1) && !isa<Constant>(Op2)) {
IC.replaceOperand(II, 1, Op2);
IC.replaceOperand(II, 2, Op1);
return &II;
}
// Only active lanes matter when simplifying the operation.
Op1 = stripInactiveLanes(Op1, Pg);
Op2 = stripInactiveLanes(Op2, Pg);
Value *SimpleII;
if (auto FII = dyn_cast<FPMathOperator>(&II))
SimpleII = simplifyBinOp(Opc, Op1, Op2, FII->getFastMathFlags(), DL);
else
SimpleII = simplifyBinOp(Opc, Op1, Op2, DL);
// An SVE intrinsic's result is always defined. However, this is not the case
// for its equivalent IR instruction (e.g. when shifting by an amount more
// than the data's bitwidth). Simplifications to an undefined result must be
// ignored to preserve the intrinsic's expected behaviour.
if (!SimpleII || isa<UndefValue>(SimpleII))
return std::nullopt;
if (IInfo.inactiveLanesAreNotDefined())
return IC.replaceInstUsesWith(II, SimpleII);
Value *Inactive = II.getOperand(IInfo.getOperandIdxInactiveLanesTakenFrom());
// The intrinsic does nothing (e.g. sve.mul(pg, A, 1.0)).
if (SimpleII == Inactive)
return IC.replaceInstUsesWith(II, SimpleII);
// Inactive lanes must be preserved.
SimpleII = IC.Builder.CreateSelect(Pg, SimpleII, Inactive);
return IC.replaceInstUsesWith(II, SimpleII);
}
// Use SVE intrinsic info to eliminate redundant operands and/or canonicalise
// to operations with less strict inactive lane requirements.
static std::optional<Instruction *>
simplifySVEIntrinsic(InstCombiner &IC, IntrinsicInst &II,
const SVEIntrinsicInfo &IInfo) {
if (!IInfo.hasGoverningPredicate())
return std::nullopt;
auto *OpPredicate = II.getOperand(IInfo.getGoverningPredicateOperandIdx());
// If there are no active lanes.
if (match(OpPredicate, m_ZeroInt())) {
if (IInfo.inactiveLanesTakenFromOperand())
return IC.replaceInstUsesWith(
II, II.getOperand(IInfo.getOperandIdxInactiveLanesTakenFrom()));
if (IInfo.inactiveLanesAreUnused()) {
if (IInfo.resultIsZeroInitialized())
IC.replaceInstUsesWith(II, Constant::getNullValue(II.getType()));
return IC.eraseInstFromFunction(II);
}
}
// If there are no inactive lanes.
if (isAllActivePredicate(OpPredicate)) {
if (IInfo.hasOperandWithNoActiveLanes()) {
unsigned OpIdx = IInfo.getOperandIdxWithNoActiveLanes();
if (!isa<UndefValue>(II.getOperand(OpIdx)))
return IC.replaceOperand(II, OpIdx, UndefValue::get(II.getType()));
}
if (IInfo.hasMatchingUndefIntrinsic()) {
auto *NewDecl = Intrinsic::getOrInsertDeclaration(
II.getModule(), IInfo.getMatchingUndefIntrinsic(), {II.getType()});
II.setCalledFunction(NewDecl);
return &II;
}
}
// Operation specific simplifications.
if (IInfo.hasMatchingIROpode() &&
Instruction::isBinaryOp(IInfo.getMatchingIROpode()))
return simplifySVEIntrinsicBinOp(IC, II, IInfo);
return std::nullopt;
}
// (from_svbool (binop (to_svbool pred) (svbool_t _) (svbool_t _))))
// => (binop (pred) (from_svbool _) (from_svbool _))
//
// The above transformation eliminates a `to_svbool` in the predicate
// operand of bitwise operation `binop` by narrowing the vector width of
// the operation. For example, it would convert a `<vscale x 16 x i1>
// and` into a `<vscale x 4 x i1> and`. This is profitable because
// to_svbool must zero the new lanes during widening, whereas
// from_svbool is free.
static std::optional<Instruction *>
tryCombineFromSVBoolBinOp(InstCombiner &IC, IntrinsicInst &II) {
auto BinOp = dyn_cast<IntrinsicInst>(II.getOperand(0));
if (!BinOp)
return std::nullopt;
auto IntrinsicID = BinOp->getIntrinsicID();
switch (IntrinsicID) {
case Intrinsic::aarch64_sve_and_z:
case Intrinsic::aarch64_sve_bic_z:
case Intrinsic::aarch64_sve_eor_z:
case Intrinsic::aarch64_sve_nand_z:
case Intrinsic::aarch64_sve_nor_z:
case Intrinsic::aarch64_sve_orn_z:
case Intrinsic::aarch64_sve_orr_z:
break;
default:
return std::nullopt;
}
auto BinOpPred = BinOp->getOperand(0);
auto BinOpOp1 = BinOp->getOperand(1);
auto BinOpOp2 = BinOp->getOperand(2);
auto PredIntr = dyn_cast<IntrinsicInst>(BinOpPred);
if (!PredIntr ||
PredIntr->getIntrinsicID() != Intrinsic::aarch64_sve_convert_to_svbool)
return std::nullopt;
auto PredOp = PredIntr->getOperand(0);
auto PredOpTy = cast<VectorType>(PredOp->getType());
if (PredOpTy != II.getType())
return std::nullopt;
SmallVector<Value *> NarrowedBinOpArgs = {PredOp};
auto NarrowBinOpOp1 = IC.Builder.CreateIntrinsic(
Intrinsic::aarch64_sve_convert_from_svbool, {PredOpTy}, {BinOpOp1});
NarrowedBinOpArgs.push_back(NarrowBinOpOp1);
if (BinOpOp1 == BinOpOp2)
NarrowedBinOpArgs.push_back(NarrowBinOpOp1);
else
NarrowedBinOpArgs.push_back(IC.Builder.CreateIntrinsic(
Intrinsic::aarch64_sve_convert_from_svbool, {PredOpTy}, {BinOpOp2}));
auto NarrowedBinOp =
IC.Builder.CreateIntrinsic(IntrinsicID, {PredOpTy}, NarrowedBinOpArgs);
return IC.replaceInstUsesWith(II, NarrowedBinOp);
}
static std::optional<Instruction *>
instCombineConvertFromSVBool(InstCombiner &IC, IntrinsicInst &II) {
// If the reinterpret instruction operand is a PHI Node
if (isa<PHINode>(II.getArgOperand(0)))
return processPhiNode(IC, II);
if (auto BinOpCombine = tryCombineFromSVBoolBinOp(IC, II))
return BinOpCombine;
// Ignore converts to/from svcount_t.
if (isa<TargetExtType>(II.getArgOperand(0)->getType()) ||
isa<TargetExtType>(II.getType()))
return std::nullopt;
SmallVector<Instruction *, 32> CandidatesForRemoval;
Value *Cursor = II.getOperand(0), *EarliestReplacement = nullptr;
const auto *IVTy = cast<VectorType>(II.getType());
// Walk the chain of conversions.
while (Cursor) {
// If the type of the cursor has fewer lanes than the final result, zeroing
// must take place, which breaks the equivalence chain.
const auto *CursorVTy = cast<VectorType>(Cursor->getType());
if (CursorVTy->getElementCount().getKnownMinValue() <
IVTy->getElementCount().getKnownMinValue())
break;
// If the cursor has the same type as I, it is a viable replacement.
if (Cursor->getType() == IVTy)
EarliestReplacement = Cursor;
auto *IntrinsicCursor = dyn_cast<IntrinsicInst>(Cursor);
// If this is not an SVE conversion intrinsic, this is the end of the chain.
if (!IntrinsicCursor || !(IntrinsicCursor->getIntrinsicID() ==
Intrinsic::aarch64_sve_convert_to_svbool ||
IntrinsicCursor->getIntrinsicID() ==
Intrinsic::aarch64_sve_convert_from_svbool))
break;
CandidatesForRemoval.insert(CandidatesForRemoval.begin(), IntrinsicCursor);
Cursor = IntrinsicCursor->getOperand(0);
}
// If no viable replacement in the conversion chain was found, there is
// nothing to do.
if (!EarliestReplacement)
return std::nullopt;
return IC.replaceInstUsesWith(II, EarliestReplacement);
}
static std::optional<Instruction *> instCombineSVESel(InstCombiner &IC,
IntrinsicInst &II) {
// svsel(ptrue, x, y) => x
auto *OpPredicate = II.getOperand(0);
if (isAllActivePredicate(OpPredicate))
return IC.replaceInstUsesWith(II, II.getOperand(1));
auto Select =
IC.Builder.CreateSelect(OpPredicate, II.getOperand(1), II.getOperand(2));
return IC.replaceInstUsesWith(II, Select);
}
static std::optional<Instruction *> instCombineSVEDup(InstCombiner &IC,
IntrinsicInst &II) {
IntrinsicInst *Pg = dyn_cast<IntrinsicInst>(II.getArgOperand(1));
if (!Pg)
return std::nullopt;
if (Pg->getIntrinsicID() != Intrinsic::aarch64_sve_ptrue)
return std::nullopt;
const auto PTruePattern =
cast<ConstantInt>(Pg->getOperand(0))->getZExtValue();
if (PTruePattern != AArch64SVEPredPattern::vl1)
return std::nullopt;
// The intrinsic is inserting into lane zero so use an insert instead.
auto *IdxTy = Type::getInt64Ty(II.getContext());
auto *Insert = InsertElementInst::Create(
II.getArgOperand(0), II.getArgOperand(2), ConstantInt::get(IdxTy, 0));
Insert->insertBefore(II.getIterator());
Insert->takeName(&II);
return IC.replaceInstUsesWith(II, Insert);
}
static std::optional<Instruction *> instCombineSVEDupX(InstCombiner &IC,
IntrinsicInst &II) {
// Replace DupX with a regular IR splat.
auto *RetTy = cast<ScalableVectorType>(II.getType());
Value *Splat = IC.Builder.CreateVectorSplat(RetTy->getElementCount(),
II.getArgOperand(0));
Splat->takeName(&II);
return IC.replaceInstUsesWith(II, Splat);
}
static std::optional<Instruction *> instCombineSVECmpNE(InstCombiner &IC,
IntrinsicInst &II) {
LLVMContext &Ctx = II.getContext();
if (!isAllActivePredicate(II.getArgOperand(0)))
return std::nullopt;
// Check that we have a compare of zero..
auto *SplatValue =
dyn_cast_or_null<ConstantInt>(getSplatValue(II.getArgOperand(2)));
if (!SplatValue || !SplatValue->isZero())
return std::nullopt;
// ..against a dupq
auto *DupQLane = dyn_cast<IntrinsicInst>(II.getArgOperand(1));
if (!DupQLane ||
DupQLane->getIntrinsicID() != Intrinsic::aarch64_sve_dupq_lane)
return std::nullopt;
// Where the dupq is a lane 0 replicate of a vector insert
auto *DupQLaneIdx = dyn_cast<ConstantInt>(DupQLane->getArgOperand(1));
if (!DupQLaneIdx || !DupQLaneIdx->isZero())
return std::nullopt;
auto *VecIns = dyn_cast<IntrinsicInst>(DupQLane->getArgOperand(0));
if (!VecIns || VecIns->getIntrinsicID() != Intrinsic::vector_insert)
return std::nullopt;
// Where the vector insert is a fixed constant vector insert into undef at
// index zero
if (!isa<UndefValue>(VecIns->getArgOperand(0)))
return std::nullopt;
if (!cast<ConstantInt>(VecIns->getArgOperand(2))->isZero())
return std::nullopt;
auto *ConstVec = dyn_cast<Constant>(VecIns->getArgOperand(1));
if (!ConstVec)
return std::nullopt;
auto *VecTy = dyn_cast<FixedVectorType>(ConstVec->getType());
auto *OutTy = dyn_cast<ScalableVectorType>(II.getType());
if (!VecTy || !OutTy || VecTy->getNumElements() != OutTy->getMinNumElements())
return std::nullopt;
unsigned NumElts = VecTy->getNumElements();
unsigned PredicateBits = 0;
// Expand intrinsic operands to a 16-bit byte level predicate
for (unsigned I = 0; I < NumElts; ++I) {
auto *Arg = dyn_cast<ConstantInt>(ConstVec->getAggregateElement(I));
if (!Arg)
return std::nullopt;
if (!Arg->isZero())
PredicateBits |= 1 << (I * (16 / NumElts));
}
// If all bits are zero bail early with an empty predicate
if (PredicateBits == 0) {
auto *PFalse = Constant::getNullValue(II.getType());
PFalse->takeName(&II);
return IC.replaceInstUsesWith(II, PFalse);
}
// Calculate largest predicate type used (where byte predicate is largest)
unsigned Mask = 8;
for (unsigned I = 0; I < 16; ++I)
if ((PredicateBits & (1 << I)) != 0)
Mask |= (I % 8);
unsigned PredSize = Mask & -Mask;
auto *PredType = ScalableVectorType::get(
Type::getInt1Ty(Ctx), AArch64::SVEBitsPerBlock / (PredSize * 8));
// Ensure all relevant bits are set
for (unsigned I = 0; I < 16; I += PredSize)
if ((PredicateBits & (1 << I)) == 0)
return std::nullopt;
auto *PTruePat =
ConstantInt::get(Type::getInt32Ty(Ctx), AArch64SVEPredPattern::all);
auto *PTrue = IC.Builder.CreateIntrinsic(Intrinsic::aarch64_sve_ptrue,
{PredType}, {PTruePat});
auto *ConvertToSVBool = IC.Builder.CreateIntrinsic(
Intrinsic::aarch64_sve_convert_to_svbool, {PredType}, {PTrue});
auto *ConvertFromSVBool =
IC.Builder.CreateIntrinsic(Intrinsic::aarch64_sve_convert_from_svbool,
{II.getType()}, {ConvertToSVBool});
ConvertFromSVBool->takeName(&II);
return IC.replaceInstUsesWith(II, ConvertFromSVBool);
}
static std::optional<Instruction *> instCombineSVELast(InstCombiner &IC,
IntrinsicInst &II) {
Value *Pg = II.getArgOperand(0);
Value *Vec = II.getArgOperand(1);
auto IntrinsicID = II.getIntrinsicID();
bool IsAfter = IntrinsicID == Intrinsic::aarch64_sve_lasta;
// lastX(splat(X)) --> X
if (auto *SplatVal = getSplatValue(Vec))
return IC.replaceInstUsesWith(II, SplatVal);
// If x and/or y is a splat value then:
// lastX (binop (x, y)) --> binop(lastX(x), lastX(y))
Value *LHS, *RHS;
if (match(Vec, m_OneUse(m_BinOp(m_Value(LHS), m_Value(RHS))))) {
if (isSplatValue(LHS) || isSplatValue(RHS)) {
auto *OldBinOp = cast<BinaryOperator>(Vec);
auto OpC = OldBinOp->getOpcode();
auto *NewLHS =
IC.Builder.CreateIntrinsic(IntrinsicID, {Vec->getType()}, {Pg, LHS});
auto *NewRHS =
IC.Builder.CreateIntrinsic(IntrinsicID, {Vec->getType()}, {Pg, RHS});
auto *NewBinOp = BinaryOperator::CreateWithCopiedFlags(
OpC, NewLHS, NewRHS, OldBinOp, OldBinOp->getName(), II.getIterator());
return IC.replaceInstUsesWith(II, NewBinOp);
}
}
auto *C = dyn_cast<Constant>(Pg);
if (IsAfter && C && C->isNullValue()) {
// The intrinsic is extracting lane 0 so use an extract instead.
auto *IdxTy = Type::getInt64Ty(II.getContext());
auto *Extract = ExtractElementInst::Create(Vec, ConstantInt::get(IdxTy, 0));
Extract->insertBefore(II.getIterator());
Extract->takeName(&II);
return IC.replaceInstUsesWith(II, Extract);
}
auto *IntrPG = dyn_cast<IntrinsicInst>(Pg);
if (!IntrPG)
return std::nullopt;
if (IntrPG->getIntrinsicID() != Intrinsic::aarch64_sve_ptrue)
return std::nullopt;
const auto PTruePattern =
cast<ConstantInt>(IntrPG->getOperand(0))->getZExtValue();
// Can the intrinsic's predicate be converted to a known constant index?
unsigned MinNumElts = getNumElementsFromSVEPredPattern(PTruePattern);
if (!MinNumElts)
return std::nullopt;
unsigned Idx = MinNumElts - 1;
// Increment the index if extracting the element after the last active
// predicate element.
if (IsAfter)
++Idx;
// Ignore extracts whose index is larger than the known minimum vector
// length. NOTE: This is an artificial constraint where we prefer to
// maintain what the user asked for until an alternative is proven faster.
auto *PgVTy = cast<ScalableVectorType>(Pg->getType());
if (Idx >= PgVTy->getMinNumElements())
return std::nullopt;
// The intrinsic is extracting a fixed lane so use an extract instead.
auto *IdxTy = Type::getInt64Ty(II.getContext());
auto *Extract = ExtractElementInst::Create(Vec, ConstantInt::get(IdxTy, Idx));
Extract->insertBefore(II.getIterator());
Extract->takeName(&II);
return IC.replaceInstUsesWith(II, Extract);
}
static std::optional<Instruction *> instCombineSVECondLast(InstCombiner &IC,
IntrinsicInst &II) {
// The SIMD&FP variant of CLAST[AB] is significantly faster than the scalar
// integer variant across a variety of micro-architectures. Replace scalar
// integer CLAST[AB] intrinsic with optimal SIMD&FP variant. A simple
// bitcast-to-fp + clast[ab] + bitcast-to-int will cost a cycle or two more
// depending on the micro-architecture, but has been observed as generally
// being faster, particularly when the CLAST[AB] op is a loop-carried
// dependency.
Value *Pg = II.getArgOperand(0);
Value *Fallback = II.getArgOperand(1);
Value *Vec = II.getArgOperand(2);
Type *Ty = II.getType();
if (!Ty->isIntegerTy())
return std::nullopt;
Type *FPTy;
switch (cast<IntegerType>(Ty)->getBitWidth()) {
default:
return std::nullopt;
case 16:
FPTy = IC.Builder.getHalfTy();
break;
case 32:
FPTy = IC.Builder.getFloatTy();
break;
case 64:
FPTy = IC.Builder.getDoubleTy();
break;
}
Value *FPFallBack = IC.Builder.CreateBitCast(Fallback, FPTy);
auto *FPVTy = VectorType::get(
FPTy, cast<VectorType>(Vec->getType())->getElementCount());
Value *FPVec = IC.Builder.CreateBitCast(Vec, FPVTy);
auto *FPII = IC.Builder.CreateIntrinsic(
II.getIntrinsicID(), {FPVec->getType()}, {Pg, FPFallBack, FPVec});
Value *FPIItoInt = IC.Builder.CreateBitCast(FPII, II.getType());
return IC.replaceInstUsesWith(II, FPIItoInt);
}
static std::optional<Instruction *> instCombineRDFFR(InstCombiner &IC,
IntrinsicInst &II) {
LLVMContext &Ctx = II.getContext();
// Replace rdffr with predicated rdffr.z intrinsic, so that optimizePTestInstr
// can work with RDFFR_PP for ptest elimination.
auto *AllPat =
ConstantInt::get(Type::getInt32Ty(Ctx), AArch64SVEPredPattern::all);
auto *PTrue = IC.Builder.CreateIntrinsic(Intrinsic::aarch64_sve_ptrue,
{II.getType()}, {AllPat});
auto *RDFFR =
IC.Builder.CreateIntrinsic(Intrinsic::aarch64_sve_rdffr_z, {PTrue});
RDFFR->takeName(&II);
return IC.replaceInstUsesWith(II, RDFFR);
}
static std::optional<Instruction *>
instCombineSVECntElts(InstCombiner &IC, IntrinsicInst &II, unsigned NumElts) {
const auto Pattern = cast<ConstantInt>(II.getArgOperand(0))->getZExtValue();
if (Pattern == AArch64SVEPredPattern::all) {
Value *Cnt = IC.Builder.CreateElementCount(
II.getType(), ElementCount::getScalable(NumElts));
Cnt->takeName(&II);
return IC.replaceInstUsesWith(II, Cnt);
}
unsigned MinNumElts = getNumElementsFromSVEPredPattern(Pattern);
return MinNumElts && NumElts >= MinNumElts
? std::optional<Instruction *>(IC.replaceInstUsesWith(
II, ConstantInt::get(II.getType(), MinNumElts)))
: std::nullopt;
}
static std::optional<Instruction *>
instCombineSMECntsd(InstCombiner &IC, IntrinsicInst &II,
const AArch64Subtarget *ST) {
if (!ST->isStreaming())
return std::nullopt;
// In streaming-mode, aarch64_sme_cntds is equivalent to aarch64_sve_cntd
// with SVEPredPattern::all
Value *Cnt =
IC.Builder.CreateElementCount(II.getType(), ElementCount::getScalable(2));
Cnt->takeName(&II);
return IC.replaceInstUsesWith(II, Cnt);
}
static std::optional<Instruction *> instCombineSVEPTest(InstCombiner &IC,
IntrinsicInst &II) {
Value *PgVal = II.getArgOperand(0);
Value *OpVal = II.getArgOperand(1);
// PTEST_<FIRST|LAST>(X, X) is equivalent to PTEST_ANY(X, X).
// Later optimizations prefer this form.
if (PgVal == OpVal &&
(II.getIntrinsicID() == Intrinsic::aarch64_sve_ptest_first ||
II.getIntrinsicID() == Intrinsic::aarch64_sve_ptest_last)) {
Value *Ops[] = {PgVal, OpVal};
Type *Tys[] = {PgVal->getType()};
auto *PTest =
IC.Builder.CreateIntrinsic(Intrinsic::aarch64_sve_ptest_any, Tys, Ops);
PTest->takeName(&II);
return IC.replaceInstUsesWith(II, PTest);
}
IntrinsicInst *Pg = dyn_cast<IntrinsicInst>(PgVal);
IntrinsicInst *Op = dyn_cast<IntrinsicInst>(OpVal);
if (!Pg || !Op)
return std::nullopt;
Intrinsic::ID OpIID = Op->getIntrinsicID();
if (Pg->getIntrinsicID() == Intrinsic::aarch64_sve_convert_to_svbool &&
OpIID == Intrinsic::aarch64_sve_convert_to_svbool &&
Pg->getArgOperand(0)->getType() == Op->getArgOperand(0)->getType()) {
Value *Ops[] = {Pg->getArgOperand(0), Op->getArgOperand(0)};
Type *Tys[] = {Pg->getArgOperand(0)->getType()};
auto *PTest = IC.Builder.CreateIntrinsic(II.getIntrinsicID(), Tys, Ops);
PTest->takeName(&II);
return IC.replaceInstUsesWith(II, PTest);
}
// Transform PTEST_ANY(X=OP(PG,...), X) -> PTEST_ANY(PG, X)).
// Later optimizations may rewrite sequence to use the flag-setting variant
// of instruction X to remove PTEST.
if ((Pg == Op) && (II.getIntrinsicID() == Intrinsic::aarch64_sve_ptest_any) &&
((OpIID == Intrinsic::aarch64_sve_brka_z) ||
(OpIID == Intrinsic::aarch64_sve_brkb_z) ||
(OpIID == Intrinsic::aarch64_sve_brkpa_z) ||
(OpIID == Intrinsic::aarch64_sve_brkpb_z) ||
(OpIID == Intrinsic::aarch64_sve_rdffr_z) ||
(OpIID == Intrinsic::aarch64_sve_and_z) ||
(OpIID == Intrinsic::aarch64_sve_bic_z) ||
(OpIID == Intrinsic::aarch64_sve_eor_z) ||
(OpIID == Intrinsic::aarch64_sve_nand_z) ||
(OpIID == Intrinsic::aarch64_sve_nor_z) ||
(OpIID == Intrinsic::aarch64_sve_orn_z) ||
(OpIID == Intrinsic::aarch64_sve_orr_z))) {
Value *Ops[] = {Pg->getArgOperand(0), Pg};
Type *Tys[] = {Pg->getType()};
auto *PTest = IC.Builder.CreateIntrinsic(II.getIntrinsicID(), Tys, Ops);
PTest->takeName(&II);
return IC.replaceInstUsesWith(II, PTest);
}
return std::nullopt;
}
template <Intrinsic::ID MulOpc, typename Intrinsic::ID FuseOpc>
static std::optional<Instruction *>
instCombineSVEVectorFuseMulAddSub(InstCombiner &IC, IntrinsicInst &II,
bool MergeIntoAddendOp) {
Value *P = II.getOperand(0);
Value *MulOp0, *MulOp1, *AddendOp, *Mul;
if (MergeIntoAddendOp) {
AddendOp = II.getOperand(1);
Mul = II.getOperand(2);
} else {
AddendOp = II.getOperand(2);
Mul = II.getOperand(1);
}
if (!match(Mul, m_Intrinsic<MulOpc>(m_Specific(P), m_Value(MulOp0),
m_Value(MulOp1))))
return std::nullopt;
if (!Mul->hasOneUse())
return std::nullopt;
Instruction *FMFSource = nullptr;
if (II.getType()->isFPOrFPVectorTy()) {
llvm::FastMathFlags FAddFlags = II.getFastMathFlags();
// Stop the combine when the flags on the inputs differ in case dropping
// flags would lead to us missing out on more beneficial optimizations.
if (FAddFlags != cast<CallInst>(Mul)->getFastMathFlags())
return std::nullopt;
if (!FAddFlags.allowContract())
return std::nullopt;
FMFSource = &II;
}
CallInst *Res;
if (MergeIntoAddendOp)
Res = IC.Builder.CreateIntrinsic(FuseOpc, {II.getType()},
{P, AddendOp, MulOp0, MulOp1}, FMFSource);
else
Res = IC.Builder.CreateIntrinsic(FuseOpc, {II.getType()},
{P, MulOp0, MulOp1, AddendOp}, FMFSource);
return IC.replaceInstUsesWith(II, Res);
}
static std::optional<Instruction *>
instCombineSVELD1(InstCombiner &IC, IntrinsicInst &II, const DataLayout &DL) {
Value *Pred = II.getOperand(0);
Value *PtrOp = II.getOperand(1);
Type *VecTy = II.getType();
if (isAllActivePredicate(Pred)) {
LoadInst *Load = IC.Builder.CreateLoad(VecTy, PtrOp);
Load->copyMetadata(II);
return IC.replaceInstUsesWith(II, Load);
}
CallInst *MaskedLoad =
IC.Builder.CreateMaskedLoad(VecTy, PtrOp, PtrOp->getPointerAlignment(DL),
Pred, ConstantAggregateZero::get(VecTy));
MaskedLoad->copyMetadata(II);
return IC.replaceInstUsesWith(II, MaskedLoad);
}
static std::optional<Instruction *>
instCombineSVEST1(InstCombiner &IC, IntrinsicInst &II, const DataLayout &DL) {
Value *VecOp = II.getOperand(0);
Value *Pred = II.getOperand(1);
Value *PtrOp = II.getOperand(2);
if (isAllActivePredicate(Pred)) {
StoreInst *Store = IC.Builder.CreateStore(VecOp, PtrOp);
Store->copyMetadata(II);
return IC.eraseInstFromFunction(II);
}
CallInst *MaskedStore = IC.Builder.CreateMaskedStore(
VecOp, PtrOp, PtrOp->getPointerAlignment(DL), Pred);
MaskedStore->copyMetadata(II);
return IC.eraseInstFromFunction(II);
}
static Instruction::BinaryOps intrinsicIDToBinOpCode(unsigned Intrinsic) {
switch (Intrinsic) {
case Intrinsic::aarch64_sve_fmul_u:
return Instruction::BinaryOps::FMul;
case Intrinsic::aarch64_sve_fadd_u:
return Instruction::BinaryOps::FAdd;
case Intrinsic::aarch64_sve_fsub_u:
return Instruction::BinaryOps::FSub;
default:
return Instruction::BinaryOpsEnd;
}
}
static std::optional<Instruction *>
instCombineSVEVectorBinOp(InstCombiner &IC, IntrinsicInst &II) {
// Bail due to missing support for ISD::STRICT_ scalable vector operations.
if (II.isStrictFP())
return std::nullopt;
auto *OpPredicate = II.getOperand(0);
auto BinOpCode = intrinsicIDToBinOpCode(II.getIntrinsicID());
if (BinOpCode == Instruction::BinaryOpsEnd ||
!isAllActivePredicate(OpPredicate))
return std::nullopt;
auto BinOp = IC.Builder.CreateBinOpFMF(
BinOpCode, II.getOperand(1), II.getOperand(2), II.getFastMathFlags());
return IC.replaceInstUsesWith(II, BinOp);
}
static std::optional<Instruction *> instCombineSVEVectorAdd(InstCombiner &IC,
IntrinsicInst &II) {
if (auto MLA = instCombineSVEVectorFuseMulAddSub<Intrinsic::aarch64_sve_mul,
Intrinsic::aarch64_sve_mla>(
IC, II, true))
return MLA;
if (auto MAD = instCombineSVEVectorFuseMulAddSub<Intrinsic::aarch64_sve_mul,
Intrinsic::aarch64_sve_mad>(
IC, II, false))
return MAD;
return std::nullopt;
}
static std::optional<Instruction *>
instCombineSVEVectorFAdd(InstCombiner &IC, IntrinsicInst &II) {
if (auto FMLA =
instCombineSVEVectorFuseMulAddSub<Intrinsic::aarch64_sve_fmul,
Intrinsic::aarch64_sve_fmla>(IC, II,
true))
return FMLA;
if (auto FMAD =
instCombineSVEVectorFuseMulAddSub<Intrinsic::aarch64_sve_fmul,
Intrinsic::aarch64_sve_fmad>(IC, II,
false))
return FMAD;
if (auto FMLA =
instCombineSVEVectorFuseMulAddSub<Intrinsic::aarch64_sve_fmul_u,
Intrinsic::aarch64_sve_fmla>(IC, II,
true))
return FMLA;
return std::nullopt;
}
static std::optional<Instruction *>
instCombineSVEVectorFAddU(InstCombiner &IC, IntrinsicInst &II) {
if (auto FMLA =
instCombineSVEVectorFuseMulAddSub<Intrinsic::aarch64_sve_fmul,
Intrinsic::aarch64_sve_fmla>(IC, II,
true))
return FMLA;
if (auto FMAD =
instCombineSVEVectorFuseMulAddSub<Intrinsic::aarch64_sve_fmul,
Intrinsic::aarch64_sve_fmad>(IC, II,
false))
return FMAD;
if (auto FMLA_U =
instCombineSVEVectorFuseMulAddSub<Intrinsic::aarch64_sve_fmul_u,
Intrinsic::aarch64_sve_fmla_u>(
IC, II, true))
return FMLA_U;
return instCombineSVEVectorBinOp(IC, II);
}
static std::optional<Instruction *>
instCombineSVEVectorFSub(InstCombiner &IC, IntrinsicInst &II) {
if (auto FMLS =
instCombineSVEVectorFuseMulAddSub<Intrinsic::aarch64_sve_fmul,
Intrinsic::aarch64_sve_fmls>(IC, II,
true))
return FMLS;
if (auto FMSB =
instCombineSVEVectorFuseMulAddSub<Intrinsic::aarch64_sve_fmul,
Intrinsic::aarch64_sve_fnmsb>(
IC, II, false))
return FMSB;
if (auto FMLS =
instCombineSVEVectorFuseMulAddSub<Intrinsic::aarch64_sve_fmul_u,
Intrinsic::aarch64_sve_fmls>(IC, II,
true))
return FMLS;
return std::nullopt;
}
static std::optional<Instruction *>
instCombineSVEVectorFSubU(InstCombiner &IC, IntrinsicInst &II) {
if (auto FMLS =
instCombineSVEVectorFuseMulAddSub<Intrinsic::aarch64_sve_fmul,
Intrinsic::aarch64_sve_fmls>(IC, II,
true))
return FMLS;
if (auto FMSB =
instCombineSVEVectorFuseMulAddSub<Intrinsic::aarch64_sve_fmul,
Intrinsic::aarch64_sve_fnmsb>(
IC, II, false))
return FMSB;
if (auto FMLS_U =
instCombineSVEVectorFuseMulAddSub<Intrinsic::aarch64_sve_fmul_u,
Intrinsic::aarch64_sve_fmls_u>(
IC, II, true))
return FMLS_U;
return instCombineSVEVectorBinOp(IC, II);
}
static std::optional<Instruction *> instCombineSVEVectorSub(InstCombiner &IC,
IntrinsicInst &II) {
if (auto MLS = instCombineSVEVectorFuseMulAddSub<Intrinsic::aarch64_sve_mul,
Intrinsic::aarch64_sve_mls>(
IC, II, true))
return MLS;
return std::nullopt;
}
static std::optional<Instruction *> instCombineSVEUnpack(InstCombiner &IC,
IntrinsicInst &II) {
Value *UnpackArg = II.getArgOperand(0);
auto *RetTy = cast<ScalableVectorType>(II.getType());
bool IsSigned = II.getIntrinsicID() == Intrinsic::aarch64_sve_sunpkhi ||
II.getIntrinsicID() == Intrinsic::aarch64_sve_sunpklo;
// Hi = uunpkhi(splat(X)) --> Hi = splat(extend(X))
// Lo = uunpklo(splat(X)) --> Lo = splat(extend(X))
if (auto *ScalarArg = getSplatValue(UnpackArg)) {
ScalarArg =
IC.Builder.CreateIntCast(ScalarArg, RetTy->getScalarType(), IsSigned);
Value *NewVal =
IC.Builder.CreateVectorSplat(RetTy->getElementCount(), ScalarArg);
NewVal->takeName(&II);
return IC.replaceInstUsesWith(II, NewVal);
}
return std::nullopt;
}
static std::optional<Instruction *> instCombineSVETBL(InstCombiner &IC,
IntrinsicInst &II) {
auto *OpVal = II.getOperand(0);
auto *OpIndices = II.getOperand(1);
VectorType *VTy = cast<VectorType>(II.getType());
// Check whether OpIndices is a constant splat value < minimal element count
// of result.
auto *SplatValue = dyn_cast_or_null<ConstantInt>(getSplatValue(OpIndices));
if (!SplatValue ||
SplatValue->getValue().uge(VTy->getElementCount().getKnownMinValue()))
return std::nullopt;
// Convert sve_tbl(OpVal sve_dup_x(SplatValue)) to
// splat_vector(extractelement(OpVal, SplatValue)) for further optimization.
auto *Extract = IC.Builder.CreateExtractElement(OpVal, SplatValue);
auto *VectorSplat =
IC.Builder.CreateVectorSplat(VTy->getElementCount(), Extract);
VectorSplat->takeName(&II);
return IC.replaceInstUsesWith(II, VectorSplat);
}
static std::optional<Instruction *> instCombineSVEUzp1(InstCombiner &IC,
IntrinsicInst &II) {
Value *A, *B;
Type *RetTy = II.getType();
constexpr Intrinsic::ID FromSVB = Intrinsic::aarch64_sve_convert_from_svbool;
constexpr Intrinsic::ID ToSVB = Intrinsic::aarch64_sve_convert_to_svbool;
// uzp1(to_svbool(A), to_svbool(B)) --> <A, B>
// uzp1(from_svbool(to_svbool(A)), from_svbool(to_svbool(B))) --> <A, B>
if ((match(II.getArgOperand(0),
m_Intrinsic<FromSVB>(m_Intrinsic<ToSVB>(m_Value(A)))) &&
match(II.getArgOperand(1),
m_Intrinsic<FromSVB>(m_Intrinsic<ToSVB>(m_Value(B))))) ||
(match(II.getArgOperand(0), m_Intrinsic<ToSVB>(m_Value(A))) &&
match(II.getArgOperand(1), m_Intrinsic<ToSVB>(m_Value(B))))) {
auto *TyA = cast<ScalableVectorType>(A->getType());
if (TyA == B->getType() &&
RetTy == ScalableVectorType::getDoubleElementsVectorType(TyA)) {
auto *SubVec = IC.Builder.CreateInsertVector(
RetTy, PoisonValue::get(RetTy), A, uint64_t(0));
auto *ConcatVec = IC.Builder.CreateInsertVector(RetTy, SubVec, B,
TyA->getMinNumElements());
ConcatVec->takeName(&II);
return IC.replaceInstUsesWith(II, ConcatVec);
}
}
return std::nullopt;
}
static std::optional<Instruction *> instCombineSVEZip(InstCombiner &IC,
IntrinsicInst &II) {
// zip1(uzp1(A, B), uzp2(A, B)) --> A
// zip2(uzp1(A, B), uzp2(A, B)) --> B
Value *A, *B;
if (match(II.getArgOperand(0),
m_Intrinsic<Intrinsic::aarch64_sve_uzp1>(m_Value(A), m_Value(B))) &&
match(II.getArgOperand(1), m_Intrinsic<Intrinsic::aarch64_sve_uzp2>(
m_Specific(A), m_Specific(B))))
return IC.replaceInstUsesWith(
II, (II.getIntrinsicID() == Intrinsic::aarch64_sve_zip1 ? A : B));
return std::nullopt;
}
static std::optional<Instruction *>
instCombineLD1GatherIndex(InstCombiner &IC, IntrinsicInst &II) {
Value *Mask = II.getOperand(0);
Value *BasePtr = II.getOperand(1);
Value *Index = II.getOperand(2);
Type *Ty = II.getType();
Value *PassThru = ConstantAggregateZero::get(Ty);
// Contiguous gather => masked load.
// (sve.ld1.gather.index Mask BasePtr (sve.index IndexBase 1))
// => (masked.load (gep BasePtr IndexBase) Align Mask zeroinitializer)
Value *IndexBase;
if (match(Index, m_Intrinsic<Intrinsic::aarch64_sve_index>(
m_Value(IndexBase), m_SpecificInt(1)))) {
Align Alignment =
BasePtr->getPointerAlignment(II.getDataLayout());
Value *Ptr = IC.Builder.CreateGEP(cast<VectorType>(Ty)->getElementType(),
BasePtr, IndexBase);
CallInst *MaskedLoad =
IC.Builder.CreateMaskedLoad(Ty, Ptr, Alignment, Mask, PassThru);
MaskedLoad->takeName(&II);
return IC.replaceInstUsesWith(II, MaskedLoad);
}
return std::nullopt;
}
static std::optional<Instruction *>
instCombineST1ScatterIndex(InstCombiner &IC, IntrinsicInst &II) {
Value *Val = II.getOperand(0);
Value *Mask = II.getOperand(1);
Value *BasePtr = II.getOperand(2);
Value *Index = II.getOperand(3);
Type *Ty = Val->getType();
// Contiguous scatter => masked store.
// (sve.st1.scatter.index Value Mask BasePtr (sve.index IndexBase 1))
// => (masked.store Value (gep BasePtr IndexBase) Align Mask)
Value *IndexBase;
if (match(Index, m_Intrinsic<Intrinsic::aarch64_sve_index>(
m_Value(IndexBase), m_SpecificInt(1)))) {
Align Alignment =
BasePtr->getPointerAlignment(II.getDataLayout());
Value *Ptr = IC.Builder.CreateGEP(cast<VectorType>(Ty)->getElementType(),
BasePtr, IndexBase);
(void)IC.Builder.CreateMaskedStore(Val, Ptr, Alignment, Mask);
return IC.eraseInstFromFunction(II);
}
return std::nullopt;
}
static std::optional<Instruction *> instCombineSVESDIV(InstCombiner &IC,
IntrinsicInst &II) {
Type *Int32Ty = IC.Builder.getInt32Ty();
Value *Pred = II.getOperand(0);
Value *Vec = II.getOperand(1);
Value *DivVec = II.getOperand(2);
Value *SplatValue = getSplatValue(DivVec);
ConstantInt *SplatConstantInt = dyn_cast_or_null<ConstantInt>(SplatValue);
if (!SplatConstantInt)
return std::nullopt;
APInt Divisor = SplatConstantInt->getValue();
const int64_t DivisorValue = Divisor.getSExtValue();
if (DivisorValue == -1)
return std::nullopt;
if (DivisorValue == 1)
IC.replaceInstUsesWith(II, Vec);
if (Divisor.isPowerOf2()) {
Constant *DivisorLog2 = ConstantInt::get(Int32Ty, Divisor.logBase2());
auto ASRD = IC.Builder.CreateIntrinsic(
Intrinsic::aarch64_sve_asrd, {II.getType()}, {Pred, Vec, DivisorLog2});
return IC.replaceInstUsesWith(II, ASRD);
}
if (Divisor.isNegatedPowerOf2()) {
Divisor.negate();
Constant *DivisorLog2 = ConstantInt::get(Int32Ty, Divisor.logBase2());
auto ASRD = IC.Builder.CreateIntrinsic(
Intrinsic::aarch64_sve_asrd, {II.getType()}, {Pred, Vec, DivisorLog2});
auto NEG = IC.Builder.CreateIntrinsic(
Intrinsic::aarch64_sve_neg, {ASRD->getType()}, {ASRD, Pred, ASRD});
return IC.replaceInstUsesWith(II, NEG);
}
return std::nullopt;
}
bool SimplifyValuePattern(SmallVector<Value *> &Vec, bool AllowPoison) {
size_t VecSize = Vec.size();
if (VecSize == 1)
return true;
if (!isPowerOf2_64(VecSize))
return false;
size_t HalfVecSize = VecSize / 2;
for (auto LHS = Vec.begin(), RHS = Vec.begin() + HalfVecSize;
RHS != Vec.end(); LHS++, RHS++) {
if (*LHS != nullptr && *RHS != nullptr) {
if (*LHS == *RHS)
continue;
else
return false;
}
if (!AllowPoison)
return false;
if (*LHS == nullptr && *RHS != nullptr)
*LHS = *RHS;
}
Vec.resize(HalfVecSize);
SimplifyValuePattern(Vec, AllowPoison);
return true;
}
// Try to simplify dupqlane patterns like dupqlane(f32 A, f32 B, f32 A, f32 B)
// to dupqlane(f64(C)) where C is A concatenated with B
static std::optional<Instruction *> instCombineSVEDupqLane(InstCombiner &IC,
IntrinsicInst &II) {
Value *CurrentInsertElt = nullptr, *Default = nullptr;
if (!match(II.getOperand(0),
m_Intrinsic<Intrinsic::vector_insert>(
m_Value(Default), m_Value(CurrentInsertElt), m_Value())) ||
!isa<FixedVectorType>(CurrentInsertElt->getType()))
return std::nullopt;
auto IIScalableTy = cast<ScalableVectorType>(II.getType());
// Insert the scalars into a container ordered by InsertElement index
SmallVector<Value *> Elts(IIScalableTy->getMinNumElements(), nullptr);
while (auto InsertElt = dyn_cast<InsertElementInst>(CurrentInsertElt)) {
auto Idx = cast<ConstantInt>(InsertElt->getOperand(2));
Elts[Idx->getValue().getZExtValue()] = InsertElt->getOperand(1);
CurrentInsertElt = InsertElt->getOperand(0);
}
bool AllowPoison =
isa<PoisonValue>(CurrentInsertElt) && isa<PoisonValue>(Default);
if (!SimplifyValuePattern(Elts, AllowPoison))
return std::nullopt;
// Rebuild the simplified chain of InsertElements. e.g. (a, b, a, b) as (a, b)
Value *InsertEltChain = PoisonValue::get(CurrentInsertElt->getType());
for (size_t I = 0; I < Elts.size(); I++) {
if (Elts[I] == nullptr)
continue;
InsertEltChain = IC.Builder.CreateInsertElement(InsertEltChain, Elts[I],
IC.Builder.getInt64(I));
}
if (InsertEltChain == nullptr)
return std::nullopt;
// Splat the simplified sequence, e.g. (f16 a, f16 b, f16 c, f16 d) as one i64
// value or (f16 a, f16 b) as one i32 value. This requires an InsertSubvector
// be bitcast to a type wide enough to fit the sequence, be splatted, and then
// be narrowed back to the original type.
unsigned PatternWidth = IIScalableTy->getScalarSizeInBits() * Elts.size();
unsigned PatternElementCount = IIScalableTy->getScalarSizeInBits() *
IIScalableTy->getMinNumElements() /
PatternWidth;
IntegerType *WideTy = IC.Builder.getIntNTy(PatternWidth);
auto *WideScalableTy = ScalableVectorType::get(WideTy, PatternElementCount);
auto *WideShuffleMaskTy =
ScalableVectorType::get(IC.Builder.getInt32Ty(), PatternElementCount);
auto InsertSubvector = IC.Builder.CreateInsertVector(
II.getType(), PoisonValue::get(II.getType()), InsertEltChain,
uint64_t(0));
auto WideBitcast =
IC.Builder.CreateBitOrPointerCast(InsertSubvector, WideScalableTy);
auto WideShuffleMask = ConstantAggregateZero::get(WideShuffleMaskTy);
auto WideShuffle = IC.Builder.CreateShuffleVector(
WideBitcast, PoisonValue::get(WideScalableTy), WideShuffleMask);
auto NarrowBitcast =
IC.Builder.CreateBitOrPointerCast(WideShuffle, II.getType());
return IC.replaceInstUsesWith(II, NarrowBitcast);
}
static std::optional<Instruction *> instCombineMaxMinNM(InstCombiner &IC,
IntrinsicInst &II) {
Value *A = II.getArgOperand(0);
Value *B = II.getArgOperand(1);
if (A == B)
return IC.replaceInstUsesWith(II, A);
return std::nullopt;
}
static std::optional<Instruction *> instCombineSVESrshl(InstCombiner &IC,
IntrinsicInst &II) {
Value *Pred = II.getOperand(0);
Value *Vec = II.getOperand(1);
Value *Shift = II.getOperand(2);
// Convert SRSHL into the simpler LSL intrinsic when fed by an ABS intrinsic.
Value *AbsPred, *MergedValue;
if (!match(Vec, m_Intrinsic<Intrinsic::aarch64_sve_sqabs>(
m_Value(MergedValue), m_Value(AbsPred), m_Value())) &&
!match(Vec, m_Intrinsic<Intrinsic::aarch64_sve_abs>(
m_Value(MergedValue), m_Value(AbsPred), m_Value())))
return std::nullopt;
// Transform is valid if any of the following are true:
// * The ABS merge value is an undef or non-negative
// * The ABS predicate is all active
// * The ABS predicate and the SRSHL predicates are the same
if (!isa<UndefValue>(MergedValue) && !match(MergedValue, m_NonNegative()) &&
AbsPred != Pred && !isAllActivePredicate(AbsPred))
return std::nullopt;
// Only valid when the shift amount is non-negative, otherwise the rounding
// behaviour of SRSHL cannot be ignored.
if (!match(Shift, m_NonNegative()))
return std::nullopt;
auto LSL = IC.Builder.CreateIntrinsic(Intrinsic::aarch64_sve_lsl,
{II.getType()}, {Pred, Vec, Shift});
return IC.replaceInstUsesWith(II, LSL);
}
static std::optional<Instruction *> instCombineSVEInsr(InstCombiner &IC,
IntrinsicInst &II) {
Value *Vec = II.getOperand(0);
if (getSplatValue(Vec) == II.getOperand(1))
return IC.replaceInstUsesWith(II, Vec);
return std::nullopt;
}
static std::optional<Instruction *> instCombineDMB(InstCombiner &IC,
IntrinsicInst &II) {
// If this barrier is post-dominated by identical one we can remove it
auto *NI = II.getNextNode();
unsigned LookaheadThreshold = DMBLookaheadThreshold;
auto CanSkipOver = [](Instruction *I) {
return !I->mayReadOrWriteMemory() && !I->mayHaveSideEffects();
};
while (LookaheadThreshold-- && CanSkipOver(NI)) {
auto *NIBB = NI->getParent();
NI = NI->getNextNode();
if (!NI) {
if (auto *SuccBB = NIBB->getUniqueSuccessor())
NI = &*SuccBB->getFirstNonPHIOrDbgOrLifetime();
else
break;
}
}
auto *NextII = dyn_cast_or_null<IntrinsicInst>(NI);
if (NextII && II.isIdenticalTo(NextII))
return IC.eraseInstFromFunction(II);
return std::nullopt;
}
static std::optional<Instruction *> instCombineWhilelo(InstCombiner &IC,
IntrinsicInst &II) {
return IC.replaceInstUsesWith(
II,
IC.Builder.CreateIntrinsic(Intrinsic::get_active_lane_mask,
{II.getType(), II.getOperand(0)->getType()},
{II.getOperand(0), II.getOperand(1)}));
}
static std::optional<Instruction *> instCombinePTrue(InstCombiner &IC,
IntrinsicInst &II) {
if (match(II.getOperand(0), m_ConstantInt<AArch64SVEPredPattern::all>()))
return IC.replaceInstUsesWith(II, Constant::getAllOnesValue(II.getType()));
return std::nullopt;
}
static std::optional<Instruction *> instCombineSVEUxt(InstCombiner &IC,
IntrinsicInst &II,
unsigned NumBits) {
Value *Passthru = II.getOperand(0);
Value *Pg = II.getOperand(1);
Value *Op = II.getOperand(2);
// Convert UXT[BHW] to AND.
if (isa<UndefValue>(Passthru) || isAllActivePredicate(Pg)) {
auto *Ty = cast<VectorType>(II.getType());
auto MaskValue = APInt::getLowBitsSet(Ty->getScalarSizeInBits(), NumBits);
auto *Mask = ConstantInt::get(Ty, MaskValue);
auto *And = IC.Builder.CreateIntrinsic(Intrinsic::aarch64_sve_and_u, {Ty},
{Pg, Op, Mask});
return IC.replaceInstUsesWith(II, And);
}
return std::nullopt;
}
static std::optional<Instruction *>
instCombineInStreamingMode(InstCombiner &IC, IntrinsicInst &II) {
SMEAttrs FnSMEAttrs(*II.getFunction());
bool IsStreaming = FnSMEAttrs.hasStreamingInterfaceOrBody();
if (IsStreaming || !FnSMEAttrs.hasStreamingCompatibleInterface())
return IC.replaceInstUsesWith(
II, ConstantInt::getBool(II.getType(), IsStreaming));
return std::nullopt;
}
std::optional<Instruction *>
AArch64TTIImpl::instCombineIntrinsic(InstCombiner &IC,
IntrinsicInst &II) const {
const SVEIntrinsicInfo &IInfo = constructSVEIntrinsicInfo(II);
if (std::optional<Instruction *> I = simplifySVEIntrinsic(IC, II, IInfo))
return I;
Intrinsic::ID IID = II.getIntrinsicID();
switch (IID) {
default:
break;
case Intrinsic::aarch64_dmb:
return instCombineDMB(IC, II);
case Intrinsic::aarch64_neon_fmaxnm:
case Intrinsic::aarch64_neon_fminnm:
return instCombineMaxMinNM(IC, II);
case Intrinsic::aarch64_sve_convert_from_svbool:
return instCombineConvertFromSVBool(IC, II);
case Intrinsic::aarch64_sve_dup:
return instCombineSVEDup(IC, II);
case Intrinsic::aarch64_sve_dup_x:
return instCombineSVEDupX(IC, II);
case Intrinsic::aarch64_sve_cmpne:
case Intrinsic::aarch64_sve_cmpne_wide:
return instCombineSVECmpNE(IC, II);
case Intrinsic::aarch64_sve_rdffr:
return instCombineRDFFR(IC, II);
case Intrinsic::aarch64_sve_lasta:
case Intrinsic::aarch64_sve_lastb:
return instCombineSVELast(IC, II);
case Intrinsic::aarch64_sve_clasta_n:
case Intrinsic::aarch64_sve_clastb_n:
return instCombineSVECondLast(IC, II);
case Intrinsic::aarch64_sve_cntd:
return instCombineSVECntElts(IC, II, 2);
case Intrinsic::aarch64_sve_cntw:
return instCombineSVECntElts(IC, II, 4);
case Intrinsic::aarch64_sve_cnth:
return instCombineSVECntElts(IC, II, 8);
case Intrinsic::aarch64_sve_cntb:
return instCombineSVECntElts(IC, II, 16);
case Intrinsic::aarch64_sme_cntsd:
return instCombineSMECntsd(IC, II, ST);
case Intrinsic::aarch64_sve_ptest_any:
case Intrinsic::aarch64_sve_ptest_first:
case Intrinsic::aarch64_sve_ptest_last:
return instCombineSVEPTest(IC, II);
case Intrinsic::aarch64_sve_fadd:
return instCombineSVEVectorFAdd(IC, II);
case Intrinsic::aarch64_sve_fadd_u:
return instCombineSVEVectorFAddU(IC, II);
case Intrinsic::aarch64_sve_fmul_u:
return instCombineSVEVectorBinOp(IC, II);
case Intrinsic::aarch64_sve_fsub:
return instCombineSVEVectorFSub(IC, II);
case Intrinsic::aarch64_sve_fsub_u:
return instCombineSVEVectorFSubU(IC, II);
case Intrinsic::aarch64_sve_add:
return instCombineSVEVectorAdd(IC, II);
case Intrinsic::aarch64_sve_add_u:
return instCombineSVEVectorFuseMulAddSub<Intrinsic::aarch64_sve_mul_u,
Intrinsic::aarch64_sve_mla_u>(
IC, II, true);
case Intrinsic::aarch64_sve_sub:
return instCombineSVEVectorSub(IC, II);
case Intrinsic::aarch64_sve_sub_u:
return instCombineSVEVectorFuseMulAddSub<Intrinsic::aarch64_sve_mul_u,
Intrinsic::aarch64_sve_mls_u>(
IC, II, true);
case Intrinsic::aarch64_sve_tbl:
return instCombineSVETBL(IC, II);
case Intrinsic::aarch64_sve_uunpkhi:
case Intrinsic::aarch64_sve_uunpklo:
case Intrinsic::aarch64_sve_sunpkhi:
case Intrinsic::aarch64_sve_sunpklo:
return instCombineSVEUnpack(IC, II);
case Intrinsic::aarch64_sve_uzp1:
return instCombineSVEUzp1(IC, II);
case Intrinsic::aarch64_sve_zip1:
case Intrinsic::aarch64_sve_zip2:
return instCombineSVEZip(IC, II);
case Intrinsic::aarch64_sve_ld1_gather_index:
return instCombineLD1GatherIndex(IC, II);
case Intrinsic::aarch64_sve_st1_scatter_index:
return instCombineST1ScatterIndex(IC, II);
case Intrinsic::aarch64_sve_ld1:
return instCombineSVELD1(IC, II, DL);
case Intrinsic::aarch64_sve_st1:
return instCombineSVEST1(IC, II, DL);
case Intrinsic::aarch64_sve_sdiv:
return instCombineSVESDIV(IC, II);
case Intrinsic::aarch64_sve_sel:
return instCombineSVESel(IC, II);
case Intrinsic::aarch64_sve_srshl:
return instCombineSVESrshl(IC, II);
case Intrinsic::aarch64_sve_dupq_lane:
return instCombineSVEDupqLane(IC, II);
case Intrinsic::aarch64_sve_insr:
return instCombineSVEInsr(IC, II);
case Intrinsic::aarch64_sve_whilelo:
return instCombineWhilelo(IC, II);
case Intrinsic::aarch64_sve_ptrue:
return instCombinePTrue(IC, II);
case Intrinsic::aarch64_sve_uxtb:
return instCombineSVEUxt(IC, II, 8);
case Intrinsic::aarch64_sve_uxth:
return instCombineSVEUxt(IC, II, 16);
case Intrinsic::aarch64_sve_uxtw:
return instCombineSVEUxt(IC, II, 32);
case Intrinsic::aarch64_sme_in_streaming_mode:
return instCombineInStreamingMode(IC, II);
}
return std::nullopt;
}
std::optional<Value *> AArch64TTIImpl::simplifyDemandedVectorEltsIntrinsic(
InstCombiner &IC, IntrinsicInst &II, APInt OrigDemandedElts,
APInt &UndefElts, APInt &UndefElts2, APInt &UndefElts3,
std::function<void(Instruction *, unsigned, APInt, APInt &)>
SimplifyAndSetOp) const {
switch (II.getIntrinsicID()) {
default:
break;
case Intrinsic::aarch64_neon_fcvtxn:
case Intrinsic::aarch64_neon_rshrn:
case Intrinsic::aarch64_neon_sqrshrn:
case Intrinsic::aarch64_neon_sqrshrun:
case Intrinsic::aarch64_neon_sqshrn:
case Intrinsic::aarch64_neon_sqshrun:
case Intrinsic::aarch64_neon_sqxtn:
case Intrinsic::aarch64_neon_sqxtun:
case Intrinsic::aarch64_neon_uqrshrn:
case Intrinsic::aarch64_neon_uqshrn:
case Intrinsic::aarch64_neon_uqxtn:
SimplifyAndSetOp(&II, 0, OrigDemandedElts, UndefElts);
break;
}
return std::nullopt;
}
bool AArch64TTIImpl::enableScalableVectorization() const {
return ST->isSVEAvailable() || (ST->isSVEorStreamingSVEAvailable() &&
EnableScalableAutovecInStreamingMode);
}
TypeSize
AArch64TTIImpl::getRegisterBitWidth(TargetTransformInfo::RegisterKind K) const {
switch (K) {
case TargetTransformInfo::RGK_Scalar:
return TypeSize::getFixed(64);
case TargetTransformInfo::RGK_FixedWidthVector:
if (ST->useSVEForFixedLengthVectors() &&
(ST->isSVEAvailable() || EnableFixedwidthAutovecInStreamingMode))
return TypeSize::getFixed(
std::max(ST->getMinSVEVectorSizeInBits(), 128u));
else if (ST->isNeonAvailable())
return TypeSize::getFixed(128);
else
return TypeSize::getFixed(0);
case TargetTransformInfo::RGK_ScalableVector:
if (ST->isSVEAvailable() || (ST->isSVEorStreamingSVEAvailable() &&
EnableScalableAutovecInStreamingMode))
return TypeSize::getScalable(128);
else
return TypeSize::getScalable(0);
}
llvm_unreachable("Unsupported register kind");
}
bool AArch64TTIImpl::isWideningInstruction(Type *DstTy, unsigned Opcode,
ArrayRef<const Value *> Args,
Type *SrcOverrideTy) const {
// A helper that returns a vector type from the given type. The number of
// elements in type Ty determines the vector width.
auto toVectorTy = [&](Type *ArgTy) {
return VectorType::get(ArgTy->getScalarType(),
cast<VectorType>(DstTy)->getElementCount());
};
// Exit early if DstTy is not a vector type whose elements are one of [i16,
// i32, i64]. SVE doesn't generally have the same set of instructions to
// perform an extend with the add/sub/mul. There are SMULLB style
// instructions, but they operate on top/bottom, requiring some sort of lane
// interleaving to be used with zext/sext.
unsigned DstEltSize = DstTy->getScalarSizeInBits();
if (!useNeonVector(DstTy) || Args.size() != 2 ||
(DstEltSize != 16 && DstEltSize != 32 && DstEltSize != 64))
return false;
// Determine if the operation has a widening variant. We consider both the
// "long" (e.g., usubl) and "wide" (e.g., usubw) versions of the
// instructions.
//
// TODO: Add additional widening operations (e.g., shl, etc.) once we
// verify that their extending operands are eliminated during code
// generation.
Type *SrcTy = SrcOverrideTy;
switch (Opcode) {
case Instruction::Add: // UADDL(2), SADDL(2), UADDW(2), SADDW(2).
case Instruction::Sub: // USUBL(2), SSUBL(2), USUBW(2), SSUBW(2).
// The second operand needs to be an extend
if (isa<SExtInst>(Args[1]) || isa<ZExtInst>(Args[1])) {
if (!SrcTy)
SrcTy =
toVectorTy(cast<Instruction>(Args[1])->getOperand(0)->getType());
} else
return false;
break;
case Instruction::Mul: { // SMULL(2), UMULL(2)
// Both operands need to be extends of the same type.
if ((isa<SExtInst>(Args[0]) && isa<SExtInst>(Args[1])) ||
(isa<ZExtInst>(Args[0]) && isa<ZExtInst>(Args[1]))) {
if (!SrcTy)
SrcTy =
toVectorTy(cast<Instruction>(Args[0])->getOperand(0)->getType());
} else if (isa<ZExtInst>(Args[0]) || isa<ZExtInst>(Args[1])) {
// If one of the operands is a Zext and the other has enough zero bits to
// be treated as unsigned, we can still general a umull, meaning the zext
// is free.
KnownBits Known =
computeKnownBits(isa<ZExtInst>(Args[0]) ? Args[1] : Args[0], DL);
if (Args[0]->getType()->getScalarSizeInBits() -
Known.Zero.countLeadingOnes() >
DstTy->getScalarSizeInBits() / 2)
return false;
if (!SrcTy)
SrcTy = toVectorTy(Type::getIntNTy(DstTy->getContext(),
DstTy->getScalarSizeInBits() / 2));
} else
return false;
break;
}
default:
return false;
}
// Legalize the destination type and ensure it can be used in a widening
// operation.
auto DstTyL = getTypeLegalizationCost(DstTy);
if (!DstTyL.second.isVector() || DstEltSize != DstTy->getScalarSizeInBits())
return false;
// Legalize the source type and ensure it can be used in a widening
// operation.
assert(SrcTy && "Expected some SrcTy");
auto SrcTyL = getTypeLegalizationCost(SrcTy);
unsigned SrcElTySize = SrcTyL.second.getScalarSizeInBits();
if (!SrcTyL.second.isVector() || SrcElTySize != SrcTy->getScalarSizeInBits())
return false;
// Get the total number of vector elements in the legalized types.
InstructionCost NumDstEls =
DstTyL.first * DstTyL.second.getVectorMinNumElements();
InstructionCost NumSrcEls =
SrcTyL.first * SrcTyL.second.getVectorMinNumElements();
// Return true if the legalized types have the same number of vector elements
// and the destination element type size is twice that of the source type.
return NumDstEls == NumSrcEls && 2 * SrcElTySize == DstEltSize;
}
// s/urhadd instructions implement the following pattern, making the
// extends free:
// %x = add ((zext i8 -> i16), 1)
// %y = (zext i8 -> i16)
// trunc i16 (lshr (add %x, %y), 1) -> i8
//
bool AArch64TTIImpl::isExtPartOfAvgExpr(const Instruction *ExtUser, Type *Dst,
Type *Src) const {
// The source should be a legal vector type.
if (!Src->isVectorTy() || !TLI->isTypeLegal(TLI->getValueType(DL, Src)) ||
(Src->isScalableTy() && !ST->hasSVE2()))
return false;
if (ExtUser->getOpcode() != Instruction::Add || !ExtUser->hasOneUse())
return false;
// Look for trunc/shl/add before trying to match the pattern.
const Instruction *Add = ExtUser;
auto *AddUser =
dyn_cast_or_null<Instruction>(Add->getUniqueUndroppableUser());
if (AddUser && AddUser->getOpcode() == Instruction::Add)
Add = AddUser;
auto *Shr = dyn_cast_or_null<Instruction>(Add->getUniqueUndroppableUser());
if (!Shr || Shr->getOpcode() != Instruction::LShr)
return false;
auto *Trunc = dyn_cast_or_null<Instruction>(Shr->getUniqueUndroppableUser());
if (!Trunc || Trunc->getOpcode() != Instruction::Trunc ||
Src->getScalarSizeInBits() !=
cast<CastInst>(Trunc)->getDestTy()->getScalarSizeInBits())
return false;
// Try to match the whole pattern. Ext could be either the first or second
// m_ZExtOrSExt matched.
Instruction *Ex1, *Ex2;
if (!(match(Add, m_c_Add(m_Instruction(Ex1),
m_c_Add(m_Instruction(Ex2), m_SpecificInt(1))))))
return false;
// Ensure both extends are of the same type
if (match(Ex1, m_ZExtOrSExt(m_Value())) &&
Ex1->getOpcode() == Ex2->getOpcode())
return true;
return false;
}
InstructionCost AArch64TTIImpl::getCastInstrCost(unsigned Opcode, Type *Dst,
Type *Src,
TTI::CastContextHint CCH,
TTI::TargetCostKind CostKind,
const Instruction *I) const {
int ISD = TLI->InstructionOpcodeToISD(Opcode);
assert(ISD && "Invalid opcode");
// If the cast is observable, and it is used by a widening instruction (e.g.,
// uaddl, saddw, etc.), it may be free.
if (I && I->hasOneUser()) {
auto *SingleUser = cast<Instruction>(*I->user_begin());
SmallVector<const Value *, 4> Operands(SingleUser->operand_values());
if (isWideningInstruction(Dst, SingleUser->getOpcode(), Operands, Src)) {
// For adds only count the second operand as free if both operands are
// extends but not the same operation. (i.e both operands are not free in
// add(sext, zext)).
if (SingleUser->getOpcode() == Instruction::Add) {
if (I == SingleUser->getOperand(1) ||
(isa<CastInst>(SingleUser->getOperand(1)) &&
cast<CastInst>(SingleUser->getOperand(1))->getOpcode() == Opcode))
return 0;
} else // Others are free so long as isWideningInstruction returned true.
return 0;
}
// The cast will be free for the s/urhadd instructions
if ((isa<ZExtInst>(I) || isa<SExtInst>(I)) &&
isExtPartOfAvgExpr(SingleUser, Dst, Src))
return 0;
}
// TODO: Allow non-throughput costs that aren't binary.
auto AdjustCost = [&CostKind](InstructionCost Cost) -> InstructionCost {
if (CostKind != TTI::TCK_RecipThroughput)
return Cost == 0 ? 0 : 1;
return Cost;
};
EVT SrcTy = TLI->getValueType(DL, Src);
EVT DstTy = TLI->getValueType(DL, Dst);
if (!SrcTy.isSimple() || !DstTy.isSimple())
return AdjustCost(
BaseT::getCastInstrCost(Opcode, Dst, Src, CCH, CostKind, I));
// For the moment we do not have lowering for SVE1-only fptrunc f64->bf16 as
// we use fcvtx under SVE2. Give them invalid costs.
if (!ST->hasSVE2() && !ST->isStreamingSVEAvailable() &&
ISD == ISD::FP_ROUND && SrcTy.isScalableVector() &&
DstTy.getScalarType() == MVT::bf16 && SrcTy.getScalarType() == MVT::f64)
return InstructionCost::getInvalid();
static const TypeConversionCostTblEntry BF16Tbl[] = {
{ISD::FP_ROUND, MVT::bf16, MVT::f32, 1}, // bfcvt
{ISD::FP_ROUND, MVT::bf16, MVT::f64, 1}, // bfcvt
{ISD::FP_ROUND, MVT::v4bf16, MVT::v4f32, 1}, // bfcvtn
{ISD::FP_ROUND, MVT::v8bf16, MVT::v8f32, 2}, // bfcvtn+bfcvtn2
{ISD::FP_ROUND, MVT::v2bf16, MVT::v2f64, 2}, // bfcvtn+fcvtn
{ISD::FP_ROUND, MVT::v4bf16, MVT::v4f64, 3}, // fcvtn+fcvtl2+bfcvtn
{ISD::FP_ROUND, MVT::v8bf16, MVT::v8f64, 6}, // 2 * fcvtn+fcvtn2+bfcvtn
{ISD::FP_ROUND, MVT::nxv2bf16, MVT::nxv2f32, 1}, // bfcvt
{ISD::FP_ROUND, MVT::nxv4bf16, MVT::nxv4f32, 1}, // bfcvt
{ISD::FP_ROUND, MVT::nxv8bf16, MVT::nxv8f32, 3}, // bfcvt+bfcvt+uzp1
{ISD::FP_ROUND, MVT::nxv2bf16, MVT::nxv2f64, 2}, // fcvtx+bfcvt
{ISD::FP_ROUND, MVT::nxv4bf16, MVT::nxv4f64, 5}, // 2*fcvtx+2*bfcvt+uzp1
{ISD::FP_ROUND, MVT::nxv8bf16, MVT::nxv8f64, 11}, // 4*fcvt+4*bfcvt+3*uzp
};
if (ST->hasBF16())
if (const auto *Entry = ConvertCostTableLookup(
BF16Tbl, ISD, DstTy.getSimpleVT(), SrcTy.getSimpleVT()))
return AdjustCost(Entry->Cost);
// Symbolic constants for the SVE sitofp/uitofp entries in the table below
// The cost of unpacking twice is artificially increased for now in order
// to avoid regressions against NEON, which will use tbl instructions directly
// instead of multiple layers of [s|u]unpk[lo|hi].
// We use the unpacks in cases where the destination type is illegal and
// requires splitting of the input, even if the input type itself is legal.
const unsigned int SVE_EXT_COST = 1;
const unsigned int SVE_FCVT_COST = 1;
const unsigned int SVE_UNPACK_ONCE = 4;
const unsigned int SVE_UNPACK_TWICE = 16;
static const TypeConversionCostTblEntry ConversionTbl[] = {
{ISD::TRUNCATE, MVT::v2i8, MVT::v2i64, 1}, // xtn
{ISD::TRUNCATE, MVT::v2i16, MVT::v2i64, 1}, // xtn
{ISD::TRUNCATE, MVT::v2i32, MVT::v2i64, 1}, // xtn
{ISD::TRUNCATE, MVT::v4i8, MVT::v4i32, 1}, // xtn
{ISD::TRUNCATE, MVT::v4i8, MVT::v4i64, 3}, // 2 xtn + 1 uzp1
{ISD::TRUNCATE, MVT::v4i16, MVT::v4i32, 1}, // xtn
{ISD::TRUNCATE, MVT::v4i16, MVT::v4i64, 2}, // 1 uzp1 + 1 xtn
{ISD::TRUNCATE, MVT::v4i32, MVT::v4i64, 1}, // 1 uzp1
{ISD::TRUNCATE, MVT::v8i8, MVT::v8i16, 1}, // 1 xtn
{ISD::TRUNCATE, MVT::v8i8, MVT::v8i32, 2}, // 1 uzp1 + 1 xtn
{ISD::TRUNCATE, MVT::v8i8, MVT::v8i64, 4}, // 3 x uzp1 + xtn
{ISD::TRUNCATE, MVT::v8i16, MVT::v8i32, 1}, // 1 uzp1
{ISD::TRUNCATE, MVT::v8i16, MVT::v8i64, 3}, // 3 x uzp1
{ISD::TRUNCATE, MVT::v8i32, MVT::v8i64, 2}, // 2 x uzp1
{ISD::TRUNCATE, MVT::v16i8, MVT::v16i16, 1}, // uzp1
{ISD::TRUNCATE, MVT::v16i8, MVT::v16i32, 3}, // (2 + 1) x uzp1
{ISD::TRUNCATE, MVT::v16i8, MVT::v16i64, 7}, // (4 + 2 + 1) x uzp1
{ISD::TRUNCATE, MVT::v16i16, MVT::v16i32, 2}, // 2 x uzp1
{ISD::TRUNCATE, MVT::v16i16, MVT::v16i64, 6}, // (4 + 2) x uzp1
{ISD::TRUNCATE, MVT::v16i32, MVT::v16i64, 4}, // 4 x uzp1
// Truncations on nxvmiN
{ISD::TRUNCATE, MVT::nxv2i1, MVT::nxv2i8, 2},
{ISD::TRUNCATE, MVT::nxv2i1, MVT::nxv2i16, 2},
{ISD::TRUNCATE, MVT::nxv2i1, MVT::nxv2i32, 2},
{ISD::TRUNCATE, MVT::nxv2i1, MVT::nxv2i64, 2},
{ISD::TRUNCATE, MVT::nxv4i1, MVT::nxv4i8, 2},
{ISD::TRUNCATE, MVT::nxv4i1, MVT::nxv4i16, 2},
{ISD::TRUNCATE, MVT::nxv4i1, MVT::nxv4i32, 2},
{ISD::TRUNCATE, MVT::nxv4i1, MVT::nxv4i64, 5},
{ISD::TRUNCATE, MVT::nxv8i1, MVT::nxv8i8, 2},
{ISD::TRUNCATE, MVT::nxv8i1, MVT::nxv8i16, 2},
{ISD::TRUNCATE, MVT::nxv8i1, MVT::nxv8i32, 5},
{ISD::TRUNCATE, MVT::nxv8i1, MVT::nxv8i64, 11},
{ISD::TRUNCATE, MVT::nxv16i1, MVT::nxv16i8, 2},
{ISD::TRUNCATE, MVT::nxv2i8, MVT::nxv2i16, 0},
{ISD::TRUNCATE, MVT::nxv2i8, MVT::nxv2i32, 0},
{ISD::TRUNCATE, MVT::nxv2i8, MVT::nxv2i64, 0},
{ISD::TRUNCATE, MVT::nxv2i16, MVT::nxv2i32, 0},
{ISD::TRUNCATE, MVT::nxv2i16, MVT::nxv2i64, 0},
{ISD::TRUNCATE, MVT::nxv2i32, MVT::nxv2i64, 0},
{ISD::TRUNCATE, MVT::nxv4i8, MVT::nxv4i16, 0},
{ISD::TRUNCATE, MVT::nxv4i8, MVT::nxv4i32, 0},
{ISD::TRUNCATE, MVT::nxv4i8, MVT::nxv4i64, 1},
{ISD::TRUNCATE, MVT::nxv4i16, MVT::nxv4i32, 0},
{ISD::TRUNCATE, MVT::nxv4i16, MVT::nxv4i64, 1},
{ISD::TRUNCATE, MVT::nxv4i32, MVT::nxv4i64, 1},
{ISD::TRUNCATE, MVT::nxv8i8, MVT::nxv8i16, 0},
{ISD::TRUNCATE, MVT::nxv8i8, MVT::nxv8i32, 1},
{ISD::TRUNCATE, MVT::nxv8i8, MVT::nxv8i64, 3},
{ISD::TRUNCATE, MVT::nxv8i16, MVT::nxv8i32, 1},
{ISD::TRUNCATE, MVT::nxv8i16, MVT::nxv8i64, 3},
{ISD::TRUNCATE, MVT::nxv16i8, MVT::nxv16i16, 1},
{ISD::TRUNCATE, MVT::nxv16i8, MVT::nxv16i32, 3},
{ISD::TRUNCATE, MVT::nxv16i8, MVT::nxv16i64, 7},
// The number of shll instructions for the extension.
{ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i16, 3},
{ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i16, 3},
{ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i32, 2},
{ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i32, 2},
{ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i8, 3},
{ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i8, 3},
{ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i16, 2},
{ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i16, 2},
{ISD::SIGN_EXTEND, MVT::v8i64, MVT::v8i8, 7},
{ISD::ZERO_EXTEND, MVT::v8i64, MVT::v8i8, 7},
{ISD::SIGN_EXTEND, MVT::v8i64, MVT::v8i16, 6},
{ISD::ZERO_EXTEND, MVT::v8i64, MVT::v8i16, 6},
{ISD::SIGN_EXTEND, MVT::v16i16, MVT::v16i8, 2},
{ISD::ZERO_EXTEND, MVT::v16i16, MVT::v16i8, 2},
{ISD::SIGN_EXTEND, MVT::v16i32, MVT::v16i8, 6},
{ISD::ZERO_EXTEND, MVT::v16i32, MVT::v16i8, 6},
// FP Ext and trunc
{ISD::FP_EXTEND, MVT::f64, MVT::f32, 1}, // fcvt
{ISD::FP_EXTEND, MVT::v2f64, MVT::v2f32, 1}, // fcvtl
{ISD::FP_EXTEND, MVT::v4f64, MVT::v4f32, 2}, // fcvtl+fcvtl2
// FP16
{ISD::FP_EXTEND, MVT::f32, MVT::f16, 1}, // fcvt
{ISD::FP_EXTEND, MVT::f64, MVT::f16, 1}, // fcvt
{ISD::FP_EXTEND, MVT::v4f32, MVT::v4f16, 1}, // fcvtl
{ISD::FP_EXTEND, MVT::v8f32, MVT::v8f16, 2}, // fcvtl+fcvtl2
{ISD::FP_EXTEND, MVT::v2f64, MVT::v2f16, 2}, // fcvtl+fcvtl
{ISD::FP_EXTEND, MVT::v4f64, MVT::v4f16, 3}, // fcvtl+fcvtl2+fcvtl
{ISD::FP_EXTEND, MVT::v8f64, MVT::v8f16, 6}, // 2 * fcvtl+fcvtl2+fcvtl
// BF16 (uses shift)
{ISD::FP_EXTEND, MVT::f32, MVT::bf16, 1}, // shl
{ISD::FP_EXTEND, MVT::f64, MVT::bf16, 2}, // shl+fcvt
{ISD::FP_EXTEND, MVT::v4f32, MVT::v4bf16, 1}, // shll
{ISD::FP_EXTEND, MVT::v8f32, MVT::v8bf16, 2}, // shll+shll2
{ISD::FP_EXTEND, MVT::v2f64, MVT::v2bf16, 2}, // shll+fcvtl
{ISD::FP_EXTEND, MVT::v4f64, MVT::v4bf16, 3}, // shll+fcvtl+fcvtl2
{ISD::FP_EXTEND, MVT::v8f64, MVT::v8bf16, 6}, // 2 * shll+fcvtl+fcvtl2
// FP Ext and trunc
{ISD::FP_ROUND, MVT::f32, MVT::f64, 1}, // fcvt
{ISD::FP_ROUND, MVT::v2f32, MVT::v2f64, 1}, // fcvtn
{ISD::FP_ROUND, MVT::v4f32, MVT::v4f64, 2}, // fcvtn+fcvtn2
// FP16
{ISD::FP_ROUND, MVT::f16, MVT::f32, 1}, // fcvt
{ISD::FP_ROUND, MVT::f16, MVT::f64, 1}, // fcvt
{ISD::FP_ROUND, MVT::v4f16, MVT::v4f32, 1}, // fcvtn
{ISD::FP_ROUND, MVT::v8f16, MVT::v8f32, 2}, // fcvtn+fcvtn2
{ISD::FP_ROUND, MVT::v2f16, MVT::v2f64, 2}, // fcvtn+fcvtn
{ISD::FP_ROUND, MVT::v4f16, MVT::v4f64, 3}, // fcvtn+fcvtn2+fcvtn
{ISD::FP_ROUND, MVT::v8f16, MVT::v8f64, 6}, // 2 * fcvtn+fcvtn2+fcvtn
// BF16 (more complex, with +bf16 is handled above)
{ISD::FP_ROUND, MVT::bf16, MVT::f32, 8}, // Expansion is ~8 insns
{ISD::FP_ROUND, MVT::bf16, MVT::f64, 9}, // fcvtn + above
{ISD::FP_ROUND, MVT::v2bf16, MVT::v2f32, 8},
{ISD::FP_ROUND, MVT::v4bf16, MVT::v4f32, 8},
{ISD::FP_ROUND, MVT::v8bf16, MVT::v8f32, 15},
{ISD::FP_ROUND, MVT::v2bf16, MVT::v2f64, 9},
{ISD::FP_ROUND, MVT::v4bf16, MVT::v4f64, 10},
{ISD::FP_ROUND, MVT::v8bf16, MVT::v8f64, 19},
// LowerVectorINT_TO_FP:
{ISD::SINT_TO_FP, MVT::v2f32, MVT::v2i32, 1},
{ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i32, 1},
{ISD::SINT_TO_FP, MVT::v2f64, MVT::v2i64, 1},
{ISD::UINT_TO_FP, MVT::v2f32, MVT::v2i32, 1},
{ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i32, 1},
{ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i64, 1},
// SVE: to nxv2f16
{ISD::SINT_TO_FP, MVT::nxv2f16, MVT::nxv2i8,
SVE_EXT_COST + SVE_FCVT_COST},
{ISD::SINT_TO_FP, MVT::nxv2f16, MVT::nxv2i16, SVE_FCVT_COST},
{ISD::SINT_TO_FP, MVT::nxv2f16, MVT::nxv2i32, SVE_FCVT_COST},
{ISD::SINT_TO_FP, MVT::nxv2f16, MVT::nxv2i64, SVE_FCVT_COST},
{ISD::UINT_TO_FP, MVT::nxv2f16, MVT::nxv2i8,
SVE_EXT_COST + SVE_FCVT_COST},
{ISD::UINT_TO_FP, MVT::nxv2f16, MVT::nxv2i16, SVE_FCVT_COST},
{ISD::UINT_TO_FP, MVT::nxv2f16, MVT::nxv2i32, SVE_FCVT_COST},
{ISD::UINT_TO_FP, MVT::nxv2f16, MVT::nxv2i64, SVE_FCVT_COST},
// SVE: to nxv4f16
{ISD::SINT_TO_FP, MVT::nxv4f16, MVT::nxv4i8,
SVE_EXT_COST + SVE_FCVT_COST},
{ISD::SINT_TO_FP, MVT::nxv4f16, MVT::nxv4i16, SVE_FCVT_COST},
{ISD::SINT_TO_FP, MVT::nxv4f16, MVT::nxv4i32, SVE_FCVT_COST},
{ISD::UINT_TO_FP, MVT::nxv4f16, MVT::nxv4i8,
SVE_EXT_COST + SVE_FCVT_COST},
{ISD::UINT_TO_FP, MVT::nxv4f16, MVT::nxv4i16, SVE_FCVT_COST},
{ISD::UINT_TO_FP, MVT::nxv4f16, MVT::nxv4i32, SVE_FCVT_COST},
// SVE: to nxv8f16
{ISD::SINT_TO_FP, MVT::nxv8f16, MVT::nxv8i8,
SVE_EXT_COST + SVE_FCVT_COST},
{ISD::SINT_TO_FP, MVT::nxv8f16, MVT::nxv8i16, SVE_FCVT_COST},
{ISD::UINT_TO_FP, MVT::nxv8f16, MVT::nxv8i8,
SVE_EXT_COST + SVE_FCVT_COST},
{ISD::UINT_TO_FP, MVT::nxv8f16, MVT::nxv8i16, SVE_FCVT_COST},
// SVE: to nxv16f16
{ISD::SINT_TO_FP, MVT::nxv16f16, MVT::nxv16i8,
SVE_UNPACK_ONCE + 2 * SVE_FCVT_COST},
{ISD::UINT_TO_FP, MVT::nxv16f16, MVT::nxv16i8,
SVE_UNPACK_ONCE + 2 * SVE_FCVT_COST},
// Complex: to v2f32
{ISD::SINT_TO_FP, MVT::v2f32, MVT::v2i8, 3},
{ISD::SINT_TO_FP, MVT::v2f32, MVT::v2i16, 3},
{ISD::UINT_TO_FP, MVT::v2f32, MVT::v2i8, 3},
{ISD::UINT_TO_FP, MVT::v2f32, MVT::v2i16, 3},
// SVE: to nxv2f32
{ISD::SINT_TO_FP, MVT::nxv2f32, MVT::nxv2i8,
SVE_EXT_COST + SVE_FCVT_COST},
{ISD::SINT_TO_FP, MVT::nxv2f32, MVT::nxv2i16, SVE_FCVT_COST},
{ISD::SINT_TO_FP, MVT::nxv2f32, MVT::nxv2i32, SVE_FCVT_COST},
{ISD::SINT_TO_FP, MVT::nxv2f32, MVT::nxv2i64, SVE_FCVT_COST},
{ISD::UINT_TO_FP, MVT::nxv2f32, MVT::nxv2i8,
SVE_EXT_COST + SVE_FCVT_COST},
{ISD::UINT_TO_FP, MVT::nxv2f32, MVT::nxv2i16, SVE_FCVT_COST},
{ISD::UINT_TO_FP, MVT::nxv2f32, MVT::nxv2i32, SVE_FCVT_COST},
{ISD::UINT_TO_FP, MVT::nxv2f32, MVT::nxv2i64, SVE_FCVT_COST},
// Complex: to v4f32
{ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i8, 4},
{ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i16, 2},
{ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i8, 3},
{ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i16, 2},
// SVE: to nxv4f32
{ISD::SINT_TO_FP, MVT::nxv4f32, MVT::nxv4i8,
SVE_EXT_COST + SVE_FCVT_COST},
{ISD::SINT_TO_FP, MVT::nxv4f32, MVT::nxv4i16, SVE_FCVT_COST},
{ISD::SINT_TO_FP, MVT::nxv4f32, MVT::nxv4i32, SVE_FCVT_COST},
{ISD::UINT_TO_FP, MVT::nxv4f32, MVT::nxv4i8,
SVE_EXT_COST + SVE_FCVT_COST},
{ISD::UINT_TO_FP, MVT::nxv4f32, MVT::nxv4i16, SVE_FCVT_COST},
{ISD::SINT_TO_FP, MVT::nxv4f32, MVT::nxv4i32, SVE_FCVT_COST},
// Complex: to v8f32
{ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i8, 10},
{ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i16, 4},
{ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i8, 10},
{ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i16, 4},
// SVE: to nxv8f32
{ISD::SINT_TO_FP, MVT::nxv8f32, MVT::nxv8i8,
SVE_EXT_COST + SVE_UNPACK_ONCE + 2 * SVE_FCVT_COST},
{ISD::SINT_TO_FP, MVT::nxv8f32, MVT::nxv8i16,
SVE_UNPACK_ONCE + 2 * SVE_FCVT_COST},
{ISD::UINT_TO_FP, MVT::nxv8f32, MVT::nxv8i8,
SVE_EXT_COST + SVE_UNPACK_ONCE + 2 * SVE_FCVT_COST},
{ISD::UINT_TO_FP, MVT::nxv8f32, MVT::nxv8i16,
SVE_UNPACK_ONCE + 2 * SVE_FCVT_COST},
// SVE: to nxv16f32
{ISD::SINT_TO_FP, MVT::nxv16f32, MVT::nxv16i8,
SVE_UNPACK_TWICE + 4 * SVE_FCVT_COST},
{ISD::UINT_TO_FP, MVT::nxv16f32, MVT::nxv16i8,
SVE_UNPACK_TWICE + 4 * SVE_FCVT_COST},
// Complex: to v16f32
{ISD::SINT_TO_FP, MVT::v16f32, MVT::v16i8, 21},
{ISD::UINT_TO_FP, MVT::v16f32, MVT::v16i8, 21},
// Complex: to v2f64
{ISD::SINT_TO_FP, MVT::v2f64, MVT::v2i8, 4},
{ISD::SINT_TO_FP, MVT::v2f64, MVT::v2i16, 4},
{ISD::SINT_TO_FP, MVT::v2f64, MVT::v2i32, 2},
{ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i8, 4},
{ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i16, 4},
{ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i32, 2},
// SVE: to nxv2f64
{ISD::SINT_TO_FP, MVT::nxv2f64, MVT::nxv2i8,
SVE_EXT_COST + SVE_FCVT_COST},
{ISD::SINT_TO_FP, MVT::nxv2f64, MVT::nxv2i16, SVE_FCVT_COST},
{ISD::SINT_TO_FP, MVT::nxv2f64, MVT::nxv2i32, SVE_FCVT_COST},
{ISD::SINT_TO_FP, MVT::nxv2f64, MVT::nxv2i64, SVE_FCVT_COST},
{ISD::UINT_TO_FP, MVT::nxv2f64, MVT::nxv2i8,
SVE_EXT_COST + SVE_FCVT_COST},
{ISD::UINT_TO_FP, MVT::nxv2f64, MVT::nxv2i16, SVE_FCVT_COST},
{ISD::UINT_TO_FP, MVT::nxv2f64, MVT::nxv2i32, SVE_FCVT_COST},
{ISD::UINT_TO_FP, MVT::nxv2f64, MVT::nxv2i64, SVE_FCVT_COST},
// Complex: to v4f64
{ISD::SINT_TO_FP, MVT::v4f64, MVT::v4i32, 4},
{ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i32, 4},
// SVE: to nxv4f64
{ISD::SINT_TO_FP, MVT::nxv4f64, MVT::nxv4i8,
SVE_EXT_COST + SVE_UNPACK_ONCE + 2 * SVE_FCVT_COST},
{ISD::SINT_TO_FP, MVT::nxv4f64, MVT::nxv4i16,
SVE_UNPACK_ONCE + 2 * SVE_FCVT_COST},
{ISD::SINT_TO_FP, MVT::nxv4f64, MVT::nxv4i32,
SVE_UNPACK_ONCE + 2 * SVE_FCVT_COST},
{ISD::UINT_TO_FP, MVT::nxv4f64, MVT::nxv4i8,
SVE_EXT_COST + SVE_UNPACK_ONCE + 2 * SVE_FCVT_COST},
{ISD::UINT_TO_FP, MVT::nxv4f64, MVT::nxv4i16,
SVE_UNPACK_ONCE + 2 * SVE_FCVT_COST},
{ISD::UINT_TO_FP, MVT::nxv4f64, MVT::nxv4i32,
SVE_UNPACK_ONCE + 2 * SVE_FCVT_COST},
// SVE: to nxv8f64
{ISD::SINT_TO_FP, MVT::nxv8f64, MVT::nxv8i8,
SVE_EXT_COST + SVE_UNPACK_TWICE + 4 * SVE_FCVT_COST},
{ISD::SINT_TO_FP, MVT::nxv8f64, MVT::nxv8i16,
SVE_UNPACK_TWICE + 4 * SVE_FCVT_COST},
{ISD::UINT_TO_FP, MVT::nxv8f64, MVT::nxv8i8,
SVE_EXT_COST + SVE_UNPACK_TWICE + 4 * SVE_FCVT_COST},
{ISD::UINT_TO_FP, MVT::nxv8f64, MVT::nxv8i16,
SVE_UNPACK_TWICE + 4 * SVE_FCVT_COST},
// LowerVectorFP_TO_INT
{ISD::FP_TO_SINT, MVT::v2i32, MVT::v2f32, 1},
{ISD::FP_TO_SINT, MVT::v4i32, MVT::v4f32, 1},
{ISD::FP_TO_SINT, MVT::v2i64, MVT::v2f64, 1},
{ISD::FP_TO_UINT, MVT::v2i32, MVT::v2f32, 1},
{ISD::FP_TO_UINT, MVT::v4i32, MVT::v4f32, 1},
{ISD::FP_TO_UINT, MVT::v2i64, MVT::v2f64, 1},
// Complex, from v2f32: legal type is v2i32 (no cost) or v2i64 (1 ext).
{ISD::FP_TO_SINT, MVT::v2i64, MVT::v2f32, 2},
{ISD::FP_TO_SINT, MVT::v2i16, MVT::v2f32, 1},
{ISD::FP_TO_SINT, MVT::v2i8, MVT::v2f32, 1},
{ISD::FP_TO_UINT, MVT::v2i64, MVT::v2f32, 2},
{ISD::FP_TO_UINT, MVT::v2i16, MVT::v2f32, 1},
{ISD::FP_TO_UINT, MVT::v2i8, MVT::v2f32, 1},
// Complex, from v4f32: legal type is v4i16, 1 narrowing => ~2
{ISD::FP_TO_SINT, MVT::v4i16, MVT::v4f32, 2},
{ISD::FP_TO_SINT, MVT::v4i8, MVT::v4f32, 2},
{ISD::FP_TO_UINT, MVT::v4i16, MVT::v4f32, 2},
{ISD::FP_TO_UINT, MVT::v4i8, MVT::v4f32, 2},
// Complex, from nxv2f32.
{ISD::FP_TO_SINT, MVT::nxv2i64, MVT::nxv2f32, 1},
{ISD::FP_TO_SINT, MVT::nxv2i32, MVT::nxv2f32, 1},
{ISD::FP_TO_SINT, MVT::nxv2i16, MVT::nxv2f32, 1},
{ISD::FP_TO_SINT, MVT::nxv2i8, MVT::nxv2f32, 1},
{ISD::FP_TO_UINT, MVT::nxv2i64, MVT::nxv2f32, 1},
{ISD::FP_TO_UINT, MVT::nxv2i32, MVT::nxv2f32, 1},
{ISD::FP_TO_UINT, MVT::nxv2i16, MVT::nxv2f32, 1},
{ISD::FP_TO_UINT, MVT::nxv2i8, MVT::nxv2f32, 1},
// Complex, from v2f64: legal type is v2i32, 1 narrowing => ~2.
{ISD::FP_TO_SINT, MVT::v2i32, MVT::v2f64, 2},
{ISD::FP_TO_SINT, MVT::v2i16, MVT::v2f64, 2},
{ISD::FP_TO_SINT, MVT::v2i8, MVT::v2f64, 2},
{ISD::FP_TO_UINT, MVT::v2i32, MVT::v2f64, 2},
{ISD::FP_TO_UINT, MVT::v2i16, MVT::v2f64, 2},
{ISD::FP_TO_UINT, MVT::v2i8, MVT::v2f64, 2},
// Complex, from nxv2f64.
{ISD::FP_TO_SINT, MVT::nxv2i64, MVT::nxv2f64, 1},
{ISD::FP_TO_SINT, MVT::nxv2i32, MVT::nxv2f64, 1},
{ISD::FP_TO_SINT, MVT::nxv2i16, MVT::nxv2f64, 1},
{ISD::FP_TO_SINT, MVT::nxv2i8, MVT::nxv2f64, 1},
{ISD::FP_TO_SINT, MVT::nxv2i1, MVT::nxv2f64, 1},
{ISD::FP_TO_UINT, MVT::nxv2i64, MVT::nxv2f64, 1},
{ISD::FP_TO_UINT, MVT::nxv2i32, MVT::nxv2f64, 1},
{ISD::FP_TO_UINT, MVT::nxv2i16, MVT::nxv2f64, 1},
{ISD::FP_TO_UINT, MVT::nxv2i8, MVT::nxv2f64, 1},
{ISD::FP_TO_UINT, MVT::nxv2i1, MVT::nxv2f64, 1},
// Complex, from nxv4f32.
{ISD::FP_TO_SINT, MVT::nxv4i64, MVT::nxv4f32, 4},
{ISD::FP_TO_SINT, MVT::nxv4i32, MVT::nxv4f32, 1},
{ISD::FP_TO_SINT, MVT::nxv4i16, MVT::nxv4f32, 1},
{ISD::FP_TO_SINT, MVT::nxv4i8, MVT::nxv4f32, 1},
{ISD::FP_TO_SINT, MVT::nxv4i1, MVT::nxv4f32, 1},
{ISD::FP_TO_UINT, MVT::nxv4i64, MVT::nxv4f32, 4},
{ISD::FP_TO_UINT, MVT::nxv4i32, MVT::nxv4f32, 1},
{ISD::FP_TO_UINT, MVT::nxv4i16, MVT::nxv4f32, 1},
{ISD::FP_TO_UINT, MVT::nxv4i8, MVT::nxv4f32, 1},
{ISD::FP_TO_UINT, MVT::nxv4i1, MVT::nxv4f32, 1},
// Complex, from nxv8f64. Illegal -> illegal conversions not required.
{ISD::FP_TO_SINT, MVT::nxv8i16, MVT::nxv8f64, 7},
{ISD::FP_TO_SINT, MVT::nxv8i8, MVT::nxv8f64, 7},
{ISD::FP_TO_UINT, MVT::nxv8i16, MVT::nxv8f64, 7},
{ISD::FP_TO_UINT, MVT::nxv8i8, MVT::nxv8f64, 7},
// Complex, from nxv4f64. Illegal -> illegal conversions not required.
{ISD::FP_TO_SINT, MVT::nxv4i32, MVT::nxv4f64, 3},
{ISD::FP_TO_SINT, MVT::nxv4i16, MVT::nxv4f64, 3},
{ISD::FP_TO_SINT, MVT::nxv4i8, MVT::nxv4f64, 3},
{ISD::FP_TO_UINT, MVT::nxv4i32, MVT::nxv4f64, 3},
{ISD::FP_TO_UINT, MVT::nxv4i16, MVT::nxv4f64, 3},
{ISD::FP_TO_UINT, MVT::nxv4i8, MVT::nxv4f64, 3},
// Complex, from nxv8f32. Illegal -> illegal conversions not required.
{ISD::FP_TO_SINT, MVT::nxv8i16, MVT::nxv8f32, 3},
{ISD::FP_TO_SINT, MVT::nxv8i8, MVT::nxv8f32, 3},
{ISD::FP_TO_UINT, MVT::nxv8i16, MVT::nxv8f32, 3},
{ISD::FP_TO_UINT, MVT::nxv8i8, MVT::nxv8f32, 3},
// Complex, from nxv8f16.
{ISD::FP_TO_SINT, MVT::nxv8i64, MVT::nxv8f16, 10},
{ISD::FP_TO_SINT, MVT::nxv8i32, MVT::nxv8f16, 4},
{ISD::FP_TO_SINT, MVT::nxv8i16, MVT::nxv8f16, 1},
{ISD::FP_TO_SINT, MVT::nxv8i8, MVT::nxv8f16, 1},
{ISD::FP_TO_SINT, MVT::nxv8i1, MVT::nxv8f16, 1},
{ISD::FP_TO_UINT, MVT::nxv8i64, MVT::nxv8f16, 10},
{ISD::FP_TO_UINT, MVT::nxv8i32, MVT::nxv8f16, 4},
{ISD::FP_TO_UINT, MVT::nxv8i16, MVT::nxv8f16, 1},
{ISD::FP_TO_UINT, MVT::nxv8i8, MVT::nxv8f16, 1},
{ISD::FP_TO_UINT, MVT::nxv8i1, MVT::nxv8f16, 1},
// Complex, from nxv4f16.
{ISD::FP_TO_SINT, MVT::nxv4i64, MVT::nxv4f16, 4},
{ISD::FP_TO_SINT, MVT::nxv4i32, MVT::nxv4f16, 1},
{ISD::FP_TO_SINT, MVT::nxv4i16, MVT::nxv4f16, 1},
{ISD::FP_TO_SINT, MVT::nxv4i8, MVT::nxv4f16, 1},
{ISD::FP_TO_UINT, MVT::nxv4i64, MVT::nxv4f16, 4},
{ISD::FP_TO_UINT, MVT::nxv4i32, MVT::nxv4f16, 1},
{ISD::FP_TO_UINT, MVT::nxv4i16, MVT::nxv4f16, 1},
{ISD::FP_TO_UINT, MVT::nxv4i8, MVT::nxv4f16, 1},
// Complex, from nxv2f16.
{ISD::FP_TO_SINT, MVT::nxv2i64, MVT::nxv2f16, 1},
{ISD::FP_TO_SINT, MVT::nxv2i32, MVT::nxv2f16, 1},
{ISD::FP_TO_SINT, MVT::nxv2i16, MVT::nxv2f16, 1},
{ISD::FP_TO_SINT, MVT::nxv2i8, MVT::nxv2f16, 1},
{ISD::FP_TO_UINT, MVT::nxv2i64, MVT::nxv2f16, 1},
{ISD::FP_TO_UINT, MVT::nxv2i32, MVT::nxv2f16, 1},
{ISD::FP_TO_UINT, MVT::nxv2i16, MVT::nxv2f16, 1},
{ISD::FP_TO_UINT, MVT::nxv2i8, MVT::nxv2f16, 1},
// Truncate from nxvmf32 to nxvmf16.
{ISD::FP_ROUND, MVT::nxv2f16, MVT::nxv2f32, 1},
{ISD::FP_ROUND, MVT::nxv4f16, MVT::nxv4f32, 1},
{ISD::FP_ROUND, MVT::nxv8f16, MVT::nxv8f32, 3},
// Truncate from nxvmf32 to nxvmbf16.
{ISD::FP_ROUND, MVT::nxv2bf16, MVT::nxv2f32, 8},
{ISD::FP_ROUND, MVT::nxv4bf16, MVT::nxv4f32, 8},
{ISD::FP_ROUND, MVT::nxv8bf16, MVT::nxv8f32, 17},
// Truncate from nxvmf64 to nxvmf16.
{ISD::FP_ROUND, MVT::nxv2f16, MVT::nxv2f64, 1},
{ISD::FP_ROUND, MVT::nxv4f16, MVT::nxv4f64, 3},
{ISD::FP_ROUND, MVT::nxv8f16, MVT::nxv8f64, 7},
// Truncate from nxvmf64 to nxvmbf16.
{ISD::FP_ROUND, MVT::nxv2bf16, MVT::nxv2f64, 9},
{ISD::FP_ROUND, MVT::nxv4bf16, MVT::nxv4f64, 19},
{ISD::FP_ROUND, MVT::nxv8bf16, MVT::nxv8f64, 39},
// Truncate from nxvmf64 to nxvmf32.
{ISD::FP_ROUND, MVT::nxv2f32, MVT::nxv2f64, 1},
{ISD::FP_ROUND, MVT::nxv4f32, MVT::nxv4f64, 3},
{ISD::FP_ROUND, MVT::nxv8f32, MVT::nxv8f64, 6},
// Extend from nxvmf16 to nxvmf32.
{ISD::FP_EXTEND, MVT::nxv2f32, MVT::nxv2f16, 1},
{ISD::FP_EXTEND, MVT::nxv4f32, MVT::nxv4f16, 1},
{ISD::FP_EXTEND, MVT::nxv8f32, MVT::nxv8f16, 2},
// Extend from nxvmbf16 to nxvmf32.
{ISD::FP_EXTEND, MVT::nxv2f32, MVT::nxv2bf16, 1}, // lsl
{ISD::FP_EXTEND, MVT::nxv4f32, MVT::nxv4bf16, 1}, // lsl
{ISD::FP_EXTEND, MVT::nxv8f32, MVT::nxv8bf16, 4}, // unpck+unpck+lsl+lsl
// Extend from nxvmf16 to nxvmf64.
{ISD::FP_EXTEND, MVT::nxv2f64, MVT::nxv2f16, 1},
{ISD::FP_EXTEND, MVT::nxv4f64, MVT::nxv4f16, 2},
{ISD::FP_EXTEND, MVT::nxv8f64, MVT::nxv8f16, 4},
// Extend from nxvmbf16 to nxvmf64.
{ISD::FP_EXTEND, MVT::nxv2f64, MVT::nxv2bf16, 2}, // lsl+fcvt
{ISD::FP_EXTEND, MVT::nxv4f64, MVT::nxv4bf16, 6}, // 2*unpck+2*lsl+2*fcvt
{ISD::FP_EXTEND, MVT::nxv8f64, MVT::nxv8bf16, 14}, // 6*unpck+4*lsl+4*fcvt
// Extend from nxvmf32 to nxvmf64.
{ISD::FP_EXTEND, MVT::nxv2f64, MVT::nxv2f32, 1},
{ISD::FP_EXTEND, MVT::nxv4f64, MVT::nxv4f32, 2},
{ISD::FP_EXTEND, MVT::nxv8f64, MVT::nxv8f32, 6},
// Bitcasts from float to integer
{ISD::BITCAST, MVT::nxv2f16, MVT::nxv2i16, 0},
{ISD::BITCAST, MVT::nxv4f16, MVT::nxv4i16, 0},
{ISD::BITCAST, MVT::nxv2f32, MVT::nxv2i32, 0},
// Bitcasts from integer to float
{ISD::BITCAST, MVT::nxv2i16, MVT::nxv2f16, 0},
{ISD::BITCAST, MVT::nxv4i16, MVT::nxv4f16, 0},
{ISD::BITCAST, MVT::nxv2i32, MVT::nxv2f32, 0},
// Add cost for extending to illegal -too wide- scalable vectors.
// zero/sign extend are implemented by multiple unpack operations,
// where each operation has a cost of 1.
{ISD::ZERO_EXTEND, MVT::nxv16i16, MVT::nxv16i8, 2},
{ISD::ZERO_EXTEND, MVT::nxv16i32, MVT::nxv16i8, 6},
{ISD::ZERO_EXTEND, MVT::nxv16i64, MVT::nxv16i8, 14},
{ISD::ZERO_EXTEND, MVT::nxv8i32, MVT::nxv8i16, 2},
{ISD::ZERO_EXTEND, MVT::nxv8i64, MVT::nxv8i16, 6},
{ISD::ZERO_EXTEND, MVT::nxv4i64, MVT::nxv4i32, 2},
{ISD::SIGN_EXTEND, MVT::nxv16i16, MVT::nxv16i8, 2},
{ISD::SIGN_EXTEND, MVT::nxv16i32, MVT::nxv16i8, 6},
{ISD::SIGN_EXTEND, MVT::nxv16i64, MVT::nxv16i8, 14},
{ISD::SIGN_EXTEND, MVT::nxv8i32, MVT::nxv8i16, 2},
{ISD::SIGN_EXTEND, MVT::nxv8i64, MVT::nxv8i16, 6},
{ISD::SIGN_EXTEND, MVT::nxv4i64, MVT::nxv4i32, 2},
};
// We have to estimate a cost of fixed length operation upon
// SVE registers(operations) with the number of registers required
// for a fixed type to be represented upon SVE registers.
EVT WiderTy = SrcTy.bitsGT(DstTy) ? SrcTy : DstTy;
if (SrcTy.isFixedLengthVector() && DstTy.isFixedLengthVector() &&
SrcTy.getVectorNumElements() == DstTy.getVectorNumElements() &&
ST->useSVEForFixedLengthVectors(WiderTy)) {
std::pair<InstructionCost, MVT> LT =
getTypeLegalizationCost(WiderTy.getTypeForEVT(Dst->getContext()));
unsigned NumElements =
AArch64::SVEBitsPerBlock / LT.second.getScalarSizeInBits();
return AdjustCost(
LT.first *
getCastInstrCost(
Opcode, ScalableVectorType::get(Dst->getScalarType(), NumElements),
ScalableVectorType::get(Src->getScalarType(), NumElements), CCH,
CostKind, I));
}
if (const auto *Entry = ConvertCostTableLookup(
ConversionTbl, ISD, DstTy.getSimpleVT(), SrcTy.getSimpleVT()))
return AdjustCost(Entry->Cost);
static const TypeConversionCostTblEntry FP16Tbl[] = {
{ISD::FP_TO_SINT, MVT::v4i8, MVT::v4f16, 1}, // fcvtzs
{ISD::FP_TO_UINT, MVT::v4i8, MVT::v4f16, 1},
{ISD::FP_TO_SINT, MVT::v4i16, MVT::v4f16, 1}, // fcvtzs
{ISD::FP_TO_UINT, MVT::v4i16, MVT::v4f16, 1},
{ISD::FP_TO_SINT, MVT::v4i32, MVT::v4f16, 2}, // fcvtl+fcvtzs
{ISD::FP_TO_UINT, MVT::v4i32, MVT::v4f16, 2},
{ISD::FP_TO_SINT, MVT::v8i8, MVT::v8f16, 2}, // fcvtzs+xtn
{ISD::FP_TO_UINT, MVT::v8i8, MVT::v8f16, 2},
{ISD::FP_TO_SINT, MVT::v8i16, MVT::v8f16, 1}, // fcvtzs
{ISD::FP_TO_UINT, MVT::v8i16, MVT::v8f16, 1},
{ISD::FP_TO_SINT, MVT::v8i32, MVT::v8f16, 4}, // 2*fcvtl+2*fcvtzs
{ISD::FP_TO_UINT, MVT::v8i32, MVT::v8f16, 4},
{ISD::FP_TO_SINT, MVT::v16i8, MVT::v16f16, 3}, // 2*fcvtzs+xtn
{ISD::FP_TO_UINT, MVT::v16i8, MVT::v16f16, 3},
{ISD::FP_TO_SINT, MVT::v16i16, MVT::v16f16, 2}, // 2*fcvtzs
{ISD::FP_TO_UINT, MVT::v16i16, MVT::v16f16, 2},
{ISD::FP_TO_SINT, MVT::v16i32, MVT::v16f16, 8}, // 4*fcvtl+4*fcvtzs
{ISD::FP_TO_UINT, MVT::v16i32, MVT::v16f16, 8},
{ISD::UINT_TO_FP, MVT::v8f16, MVT::v8i8, 2}, // ushll + ucvtf
{ISD::SINT_TO_FP, MVT::v8f16, MVT::v8i8, 2}, // sshll + scvtf
{ISD::UINT_TO_FP, MVT::v16f16, MVT::v16i8, 4}, // 2 * ushl(2) + 2 * ucvtf
{ISD::SINT_TO_FP, MVT::v16f16, MVT::v16i8, 4}, // 2 * sshl(2) + 2 * scvtf
};
if (ST->hasFullFP16())
if (const auto *Entry = ConvertCostTableLookup(
FP16Tbl, ISD, DstTy.getSimpleVT(), SrcTy.getSimpleVT()))
return AdjustCost(Entry->Cost);
// INT_TO_FP of i64->f32 will scalarize, which is required to avoid
// double-rounding issues.
if ((ISD == ISD::SINT_TO_FP || ISD == ISD::UINT_TO_FP) &&
DstTy.getScalarType() == MVT::f32 && SrcTy.getScalarSizeInBits() > 32 &&
isa<FixedVectorType>(Dst) && isa<FixedVectorType>(Src))
return AdjustCost(
cast<FixedVectorType>(Dst)->getNumElements() *
getCastInstrCost(Opcode, Dst->getScalarType(), Src->getScalarType(),
CCH, CostKind) +
BaseT::getScalarizationOverhead(cast<FixedVectorType>(Src), false, true,
CostKind) +
BaseT::getScalarizationOverhead(cast<FixedVectorType>(Dst), true, false,
CostKind));
if ((ISD == ISD::ZERO_EXTEND || ISD == ISD::SIGN_EXTEND) &&
CCH == TTI::CastContextHint::Masked &&
ST->isSVEorStreamingSVEAvailable() &&
TLI->getTypeAction(Src->getContext(), SrcTy) ==
TargetLowering::TypePromoteInteger &&
TLI->getTypeAction(Dst->getContext(), DstTy) ==
TargetLowering::TypeSplitVector) {
// The standard behaviour in the backend for these cases is to split the
// extend up into two parts:
// 1. Perform an extending load or masked load up to the legal type.
// 2. Extend the loaded data to the final type.
std::pair<InstructionCost, MVT> SrcLT = getTypeLegalizationCost(Src);
Type *LegalTy = EVT(SrcLT.second).getTypeForEVT(Src->getContext());
InstructionCost Part1 = AArch64TTIImpl::getCastInstrCost(
Opcode, LegalTy, Src, CCH, CostKind, I);
InstructionCost Part2 = AArch64TTIImpl::getCastInstrCost(
Opcode, Dst, LegalTy, TTI::CastContextHint::None, CostKind, I);
return Part1 + Part2;
}
// The BasicTTIImpl version only deals with CCH==TTI::CastContextHint::Normal,
// but we also want to include the TTI::CastContextHint::Masked case too.
if ((ISD == ISD::ZERO_EXTEND || ISD == ISD::SIGN_EXTEND) &&
CCH == TTI::CastContextHint::Masked &&
ST->isSVEorStreamingSVEAvailable() && TLI->isTypeLegal(DstTy))
CCH = TTI::CastContextHint::Normal;
return AdjustCost(
BaseT::getCastInstrCost(Opcode, Dst, Src, CCH, CostKind, I));
}
InstructionCost
AArch64TTIImpl::getExtractWithExtendCost(unsigned Opcode, Type *Dst,
VectorType *VecTy, unsigned Index,
TTI::TargetCostKind CostKind) const {
// Make sure we were given a valid extend opcode.
assert((Opcode == Instruction::SExt || Opcode == Instruction::ZExt) &&
"Invalid opcode");
// We are extending an element we extract from a vector, so the source type
// of the extend is the element type of the vector.
auto *Src = VecTy->getElementType();
// Sign- and zero-extends are for integer types only.
assert(isa<IntegerType>(Dst) && isa<IntegerType>(Src) && "Invalid type");
// Get the cost for the extract. We compute the cost (if any) for the extend
// below.
InstructionCost Cost = getVectorInstrCost(Instruction::ExtractElement, VecTy,
CostKind, Index, nullptr, nullptr);
// Legalize the types.
auto VecLT = getTypeLegalizationCost(VecTy);
auto DstVT = TLI->getValueType(DL, Dst);
auto SrcVT = TLI->getValueType(DL, Src);
// If the resulting type is still a vector and the destination type is legal,
// we may get the extension for free. If not, get the default cost for the
// extend.
if (!VecLT.second.isVector() || !TLI->isTypeLegal(DstVT))
return Cost + getCastInstrCost(Opcode, Dst, Src, TTI::CastContextHint::None,
CostKind);
// The destination type should be larger than the element type. If not, get
// the default cost for the extend.
if (DstVT.getFixedSizeInBits() < SrcVT.getFixedSizeInBits())
return Cost + getCastInstrCost(Opcode, Dst, Src, TTI::CastContextHint::None,
CostKind);
switch (Opcode) {
default:
llvm_unreachable("Opcode should be either SExt or ZExt");
// For sign-extends, we only need a smov, which performs the extension
// automatically.
case Instruction::SExt:
return Cost;
// For zero-extends, the extend is performed automatically by a umov unless
// the destination type is i64 and the element type is i8 or i16.
case Instruction::ZExt:
if (DstVT.getSizeInBits() != 64u || SrcVT.getSizeInBits() == 32u)
return Cost;
}
// If we are unable to perform the extend for free, get the default cost.
return Cost + getCastInstrCost(Opcode, Dst, Src, TTI::CastContextHint::None,
CostKind);
}
InstructionCost AArch64TTIImpl::getCFInstrCost(unsigned Opcode,
TTI::TargetCostKind CostKind,
const Instruction *I) const {
if (CostKind != TTI::TCK_RecipThroughput)
return Opcode == Instruction::PHI ? 0 : 1;
assert(CostKind == TTI::TCK_RecipThroughput && "unexpected CostKind");
// Branches are assumed to be predicted.
return 0;
}
InstructionCost AArch64TTIImpl::getVectorInstrCostHelper(
unsigned Opcode, Type *Val, TTI::TargetCostKind CostKind, unsigned Index,
const Instruction *I, Value *Scalar,
ArrayRef<std::tuple<Value *, User *, int>> ScalarUserAndIdx) const {
assert(Val->isVectorTy() && "This must be a vector type");
if (Index != -1U) {
// Legalize the type.
std::pair<InstructionCost, MVT> LT = getTypeLegalizationCost(Val);
// This type is legalized to a scalar type.
if (!LT.second.isVector())
return 0;
// The type may be split. For fixed-width vectors we can normalize the
// index to the new type.
if (LT.second.isFixedLengthVector()) {
unsigned Width = LT.second.getVectorNumElements();
Index = Index % Width;
}
// The element at index zero is already inside the vector.
// - For a insert-element or extract-element
// instruction that extracts integers, an explicit FPR -> GPR move is
// needed. So it has non-zero cost.
if (Index == 0 && !Val->getScalarType()->isIntegerTy())
return 0;
// This is recognising a LD1 single-element structure to one lane of one
// register instruction. I.e., if this is an `insertelement` instruction,
// and its second operand is a load, then we will generate a LD1, which
// are expensive instructions.
if (I && dyn_cast<LoadInst>(I->getOperand(1)))
return CostKind == TTI::TCK_CodeSize
? 0
: ST->getVectorInsertExtractBaseCost() + 1;
// i1 inserts and extract will include an extra cset or cmp of the vector
// value. Increase the cost by 1 to account.
if (Val->getScalarSizeInBits() == 1)
return CostKind == TTI::TCK_CodeSize
? 2
: ST->getVectorInsertExtractBaseCost() + 1;
// FIXME:
// If the extract-element and insert-element instructions could be
// simplified away (e.g., could be combined into users by looking at use-def
// context), they have no cost. This is not done in the first place for
// compile-time considerations.
}
// In case of Neon, if there exists extractelement from lane != 0 such that
// 1. extractelement does not necessitate a move from vector_reg -> GPR.
// 2. extractelement result feeds into fmul.
// 3. Other operand of fmul is an extractelement from lane 0 or lane
// equivalent to 0.
// then the extractelement can be merged with fmul in the backend and it
// incurs no cost.
// e.g.
// define double @foo(<2 x double> %a) {
// %1 = extractelement <2 x double> %a, i32 0
// %2 = extractelement <2 x double> %a, i32 1
// %res = fmul double %1, %2
// ret double %res
// }
// %2 and %res can be merged in the backend to generate fmul d0, d0, v1.d[1]
auto ExtractCanFuseWithFmul = [&]() {
// We bail out if the extract is from lane 0.
if (Index == 0)
return false;
// Check if the scalar element type of the vector operand of ExtractElement
// instruction is one of the allowed types.
auto IsAllowedScalarTy = [&](const Type *T) {
return T->isFloatTy() || T->isDoubleTy() ||
(T->isHalfTy() && ST->hasFullFP16());
};
// Check if the extractelement user is scalar fmul.
auto IsUserFMulScalarTy = [](const Value *EEUser) {
// Check if the user is scalar fmul.
const auto *BO = dyn_cast<BinaryOperator>(EEUser);
return BO && BO->getOpcode() == BinaryOperator::FMul &&
!BO->getType()->isVectorTy();
};
// Check if the extract index is from lane 0 or lane equivalent to 0 for a
// certain scalar type and a certain vector register width.
auto IsExtractLaneEquivalentToZero = [&](unsigned Idx, unsigned EltSz) {
auto RegWidth =
getRegisterBitWidth(TargetTransformInfo::RGK_FixedWidthVector)
.getFixedValue();
return Idx == 0 || (RegWidth != 0 && (Idx * EltSz) % RegWidth == 0);
};
// Check if the type constraints on input vector type and result scalar type
// of extractelement instruction are satisfied.
if (!isa<FixedVectorType>(Val) || !IsAllowedScalarTy(Val->getScalarType()))
return false;
if (Scalar) {
DenseMap<User *, unsigned> UserToExtractIdx;
for (auto *U : Scalar->users()) {
if (!IsUserFMulScalarTy(U))
return false;
// Recording entry for the user is important. Index value is not
// important.
UserToExtractIdx[U];
}
if (UserToExtractIdx.empty())
return false;
for (auto &[S, U, L] : ScalarUserAndIdx) {
for (auto *U : S->users()) {
if (UserToExtractIdx.contains(U)) {
auto *FMul = cast<BinaryOperator>(U);
auto *Op0 = FMul->getOperand(0);
auto *Op1 = FMul->getOperand(1);
if ((Op0 == S && Op1 == S) || Op0 != S || Op1 != S) {
UserToExtractIdx[U] = L;
break;
}
}
}
}
for (auto &[U, L] : UserToExtractIdx) {
if (!IsExtractLaneEquivalentToZero(Index, Val->getScalarSizeInBits()) &&
!IsExtractLaneEquivalentToZero(L, Val->getScalarSizeInBits()))
return false;
}
} else {
const auto *EE = cast<ExtractElementInst>(I);
const auto *IdxOp = dyn_cast<ConstantInt>(EE->getIndexOperand());
if (!IdxOp)
return false;
return !EE->users().empty() && all_of(EE->users(), [&](const User *U) {
if (!IsUserFMulScalarTy(U))
return false;
// Check if the other operand of extractelement is also extractelement
// from lane equivalent to 0.
const auto *BO = cast<BinaryOperator>(U);
const auto *OtherEE = dyn_cast<ExtractElementInst>(
BO->getOperand(0) == EE ? BO->getOperand(1) : BO->getOperand(0));
if (OtherEE) {
const auto *IdxOp = dyn_cast<ConstantInt>(OtherEE->getIndexOperand());
if (!IdxOp)
return false;
return IsExtractLaneEquivalentToZero(
cast<ConstantInt>(OtherEE->getIndexOperand())
->getValue()
.getZExtValue(),
OtherEE->getType()->getScalarSizeInBits());
}
return true;
});
}
return true;
};
if (Opcode == Instruction::ExtractElement && (I || Scalar) &&
ExtractCanFuseWithFmul())
return 0;
// All other insert/extracts cost this much.
return CostKind == TTI::TCK_CodeSize ? 1
: ST->getVectorInsertExtractBaseCost();
}
InstructionCost AArch64TTIImpl::getVectorInstrCost(unsigned Opcode, Type *Val,
TTI::TargetCostKind CostKind,
unsigned Index,
const Value *Op0,
const Value *Op1) const {
// Treat insert at lane 0 into a poison vector as having zero cost. This
// ensures vector broadcasts via an insert + shuffle (and will be lowered to a
// single dup) are treated as cheap.
if (Opcode == Instruction::InsertElement && Index == 0 && Op0 &&
isa<PoisonValue>(Op0))
return 0;
return getVectorInstrCostHelper(Opcode, Val, CostKind, Index);
}
InstructionCost AArch64TTIImpl::getVectorInstrCost(
unsigned Opcode, Type *Val, TTI::TargetCostKind CostKind, unsigned Index,
Value *Scalar,
ArrayRef<std::tuple<Value *, User *, int>> ScalarUserAndIdx) const {
return getVectorInstrCostHelper(Opcode, Val, CostKind, Index, nullptr, Scalar,
ScalarUserAndIdx);
}
InstructionCost AArch64TTIImpl::getVectorInstrCost(const Instruction &I,
Type *Val,
TTI::TargetCostKind CostKind,
unsigned Index) const {
return getVectorInstrCostHelper(I.getOpcode(), Val, CostKind, Index, &I);
}
InstructionCost
AArch64TTIImpl::getIndexedVectorInstrCostFromEnd(unsigned Opcode, Type *Val,
TTI::TargetCostKind CostKind,
unsigned Index) const {
if (isa<FixedVectorType>(Val))
return BaseT::getIndexedVectorInstrCostFromEnd(Opcode, Val, CostKind,
Index);
// This typically requires both while and lastb instructions in order
// to extract the last element. If this is in a loop the while
// instruction can at least be hoisted out, although it will consume a
// predicate register. The cost should be more expensive than the base
// extract cost, which is 2 for most CPUs.
return CostKind == TTI::TCK_CodeSize
? 2
: ST->getVectorInsertExtractBaseCost() + 1;
}
InstructionCost AArch64TTIImpl::getScalarizationOverhead(
VectorType *Ty, const APInt &DemandedElts, bool Insert, bool Extract,
TTI::TargetCostKind CostKind, bool ForPoisonSrc,
ArrayRef<Value *> VL) const {
if (isa<ScalableVectorType>(Ty))
return InstructionCost::getInvalid();
if (Ty->getElementType()->isFloatingPointTy())
return BaseT::getScalarizationOverhead(Ty, DemandedElts, Insert, Extract,
CostKind);
unsigned VecInstCost =
CostKind == TTI::TCK_CodeSize ? 1 : ST->getVectorInsertExtractBaseCost();
return DemandedElts.popcount() * (Insert + Extract) * VecInstCost;
}
std::optional<InstructionCost> AArch64TTIImpl::getFP16BF16PromoteCost(
Type *Ty, TTI::TargetCostKind CostKind, TTI::OperandValueInfo Op1Info,
TTI::OperandValueInfo Op2Info, bool IncludeTrunc,
std::function<InstructionCost(Type *)> InstCost) const {
if (!Ty->getScalarType()->isHalfTy() && !Ty->getScalarType()->isBFloatTy())
return std::nullopt;
if (Ty->getScalarType()->isHalfTy() && ST->hasFullFP16())
return std::nullopt;
Type *PromotedTy = Ty->getWithNewType(Type::getFloatTy(Ty->getContext()));
InstructionCost Cost = getCastInstrCost(Instruction::FPExt, PromotedTy, Ty,
TTI::CastContextHint::None, CostKind);
if (!Op1Info.isConstant() && !Op2Info.isConstant())
Cost *= 2;
Cost += InstCost(PromotedTy);
if (IncludeTrunc)
Cost += getCastInstrCost(Instruction::FPTrunc, Ty, PromotedTy,
TTI::CastContextHint::None, CostKind);
return Cost;
}
InstructionCost AArch64TTIImpl::getArithmeticInstrCost(
unsigned Opcode, Type *Ty, TTI::TargetCostKind CostKind,
TTI::OperandValueInfo Op1Info, TTI::OperandValueInfo Op2Info,
ArrayRef<const Value *> Args, const Instruction *CxtI) const {
// The code-generator is currently not able to handle scalable vectors
// of <vscale x 1 x eltty> yet, so return an invalid cost to avoid selecting
// it. This change will be removed when code-generation for these types is
// sufficiently reliable.
if (auto *VTy = dyn_cast<ScalableVectorType>(Ty))
if (VTy->getElementCount() == ElementCount::getScalable(1))
return InstructionCost::getInvalid();
// TODO: Handle more cost kinds.
if (CostKind != TTI::TCK_RecipThroughput)
return BaseT::getArithmeticInstrCost(Opcode, Ty, CostKind, Op1Info,
Op2Info, Args, CxtI);
// Legalize the type.
std::pair<InstructionCost, MVT> LT = getTypeLegalizationCost(Ty);
int ISD = TLI->InstructionOpcodeToISD(Opcode);
// Increase the cost for half and bfloat types if not architecturally
// supported.
if (ISD == ISD::FADD || ISD == ISD::FSUB || ISD == ISD::FMUL ||
ISD == ISD::FDIV || ISD == ISD::FREM)
if (auto PromotedCost = getFP16BF16PromoteCost(
Ty, CostKind, Op1Info, Op2Info, /*IncludeTrunc=*/true,
[&](Type *PromotedTy) {
return getArithmeticInstrCost(Opcode, PromotedTy, CostKind,
Op1Info, Op2Info);
}))
return *PromotedCost;
switch (ISD) {
default:
return BaseT::getArithmeticInstrCost(Opcode, Ty, CostKind, Op1Info,
Op2Info);
case ISD::SREM:
case ISD::SDIV:
/*
Notes for sdiv/srem specific costs:
1. This only considers the cases where the divisor is constant, uniform and
(pow-of-2/non-pow-of-2). Other cases are not important since they either
result in some form of (ldr + adrp), corresponding to constant vectors, or
scalarization of the division operation.
2. Constant divisors, either negative in whole or partially, don't result in
significantly different codegen as compared to positive constant divisors.
So, we don't consider negative divisors separately.
3. If the codegen is significantly different with SVE, it has been indicated
using comments at appropriate places.
sdiv specific cases:
-----------------------------------------------------------------------
codegen | pow-of-2 | Type
-----------------------------------------------------------------------
add + cmp + csel + asr | Y | i64
add + cmp + csel + asr | Y | i32
-----------------------------------------------------------------------
srem specific cases:
-----------------------------------------------------------------------
codegen | pow-of-2 | Type
-----------------------------------------------------------------------
negs + and + and + csneg | Y | i64
negs + and + and + csneg | Y | i32
-----------------------------------------------------------------------
other sdiv/srem cases:
-------------------------------------------------------------------------
common codegen | + srem | + sdiv | pow-of-2 | Type
-------------------------------------------------------------------------
smulh + asr + add + add | - | - | N | i64
smull + lsr + add + add | - | - | N | i32
usra | and + sub | sshr | Y | <2 x i64>
2 * (scalar code) | - | - | N | <2 x i64>
usra | bic + sub | sshr + neg | Y | <4 x i32>
smull2 + smull + uzp2 | mls | - | N | <4 x i32>
+ sshr + usra | | | |
-------------------------------------------------------------------------
*/
if (Op2Info.isConstant() && Op2Info.isUniform()) {
InstructionCost AddCost =
getArithmeticInstrCost(Instruction::Add, Ty, CostKind,
Op1Info.getNoProps(), Op2Info.getNoProps());
InstructionCost AsrCost =
getArithmeticInstrCost(Instruction::AShr, Ty, CostKind,
Op1Info.getNoProps(), Op2Info.getNoProps());
InstructionCost MulCost =
getArithmeticInstrCost(Instruction::Mul, Ty, CostKind,
Op1Info.getNoProps(), Op2Info.getNoProps());
// add/cmp/csel/csneg should have similar cost while asr/negs/and should
// have similar cost.
auto VT = TLI->getValueType(DL, Ty);
if (VT.isScalarInteger() && VT.getSizeInBits() <= 64) {
if (Op2Info.isPowerOf2() || Op2Info.isNegatedPowerOf2()) {
// Neg can be folded into the asr instruction.
return ISD == ISD::SDIV ? (3 * AddCost + AsrCost)
: (3 * AsrCost + AddCost);
} else {
return MulCost + AsrCost + 2 * AddCost;
}
} else if (VT.isVector()) {
InstructionCost UsraCost = 2 * AsrCost;
if (Op2Info.isPowerOf2() || Op2Info.isNegatedPowerOf2()) {
// Division with scalable types corresponds to native 'asrd'
// instruction when SVE is available.
// e.g. %1 = sdiv <vscale x 4 x i32> %a, splat (i32 8)
// One more for the negation in SDIV
InstructionCost Cost =
(Op2Info.isNegatedPowerOf2() && ISD == ISD::SDIV) ? AsrCost : 0;
if (Ty->isScalableTy() && ST->hasSVE())
Cost += 2 * AsrCost;
else {
Cost +=
UsraCost +
(ISD == ISD::SDIV
? (LT.second.getScalarType() == MVT::i64 ? 1 : 2) * AsrCost
: 2 * AddCost);
}
return Cost;
} else if (LT.second == MVT::v2i64) {
return VT.getVectorNumElements() *
getArithmeticInstrCost(Opcode, Ty->getScalarType(), CostKind,
Op1Info.getNoProps(),
Op2Info.getNoProps());
} else {
// When SVE is available, we get:
// smulh + lsr + add/sub + asr + add/sub.
if (Ty->isScalableTy() && ST->hasSVE())
return MulCost /*smulh cost*/ + 2 * AddCost + 2 * AsrCost;
return 2 * MulCost + AddCost /*uzp2 cost*/ + AsrCost + UsraCost;
}
}
}
if (Op2Info.isConstant() && !Op2Info.isUniform() &&
LT.second.isFixedLengthVector()) {
// FIXME: When the constant vector is non-uniform, this may result in
// loading the vector from constant pool or in some cases, may also result
// in scalarization. For now, we are approximating this with the
// scalarization cost.
auto ExtractCost = 2 * getVectorInstrCost(Instruction::ExtractElement, Ty,
CostKind, -1, nullptr, nullptr);
auto InsertCost = getVectorInstrCost(Instruction::InsertElement, Ty,
CostKind, -1, nullptr, nullptr);
unsigned NElts = cast<FixedVectorType>(Ty)->getNumElements();
return ExtractCost + InsertCost +
NElts * getArithmeticInstrCost(Opcode, Ty->getScalarType(),
CostKind, Op1Info.getNoProps(),
Op2Info.getNoProps());
}
[[fallthrough]];
case ISD::UDIV:
case ISD::UREM: {
auto VT = TLI->getValueType(DL, Ty);
if (Op2Info.isConstant()) {
// If the operand is a power of 2 we can use the shift or and cost.
if (ISD == ISD::UDIV && Op2Info.isPowerOf2())
return getArithmeticInstrCost(Instruction::LShr, Ty, CostKind,
Op1Info.getNoProps(),
Op2Info.getNoProps());
if (ISD == ISD::UREM && Op2Info.isPowerOf2())
return getArithmeticInstrCost(Instruction::And, Ty, CostKind,
Op1Info.getNoProps(),
Op2Info.getNoProps());
if (ISD == ISD::UDIV || ISD == ISD::UREM) {
// Divides by a constant are expanded to MULHU + SUB + SRL + ADD + SRL.
// The MULHU will be expanded to UMULL for the types not listed below,
// and will become a pair of UMULL+MULL2 for 128bit vectors.
bool HasMULH = VT == MVT::i64 || LT.second == MVT::nxv2i64 ||
LT.second == MVT::nxv4i32 || LT.second == MVT::nxv8i16 ||
LT.second == MVT::nxv16i8;
bool Is128bit = LT.second.is128BitVector();
InstructionCost MulCost =
getArithmeticInstrCost(Instruction::Mul, Ty, CostKind,
Op1Info.getNoProps(), Op2Info.getNoProps());
InstructionCost AddCost =
getArithmeticInstrCost(Instruction::Add, Ty, CostKind,
Op1Info.getNoProps(), Op2Info.getNoProps());
InstructionCost ShrCost =
getArithmeticInstrCost(Instruction::AShr, Ty, CostKind,
Op1Info.getNoProps(), Op2Info.getNoProps());
InstructionCost DivCost = MulCost * (Is128bit ? 2 : 1) + // UMULL/UMULH
(HasMULH ? 0 : ShrCost) + // UMULL shift
AddCost * 2 + ShrCost;
return DivCost + (ISD == ISD::UREM ? MulCost + AddCost : 0);
}
}
// div i128's are lowered as libcalls. Pass nullptr as (u)divti3 calls are
// emitted by the backend even when those functions are not declared in the
// module.
if (!VT.isVector() && VT.getSizeInBits() > 64)
return getCallInstrCost(/*Function*/ nullptr, Ty, {Ty, Ty}, CostKind);
InstructionCost Cost = BaseT::getArithmeticInstrCost(
Opcode, Ty, CostKind, Op1Info, Op2Info);
if (Ty->isVectorTy() && (ISD == ISD::SDIV || ISD == ISD::UDIV)) {
if (TLI->isOperationLegalOrCustom(ISD, LT.second) && ST->hasSVE()) {
// SDIV/UDIV operations are lowered using SVE, then we can have less
// costs.
if (VT.isSimple() && isa<FixedVectorType>(Ty) &&
Ty->getPrimitiveSizeInBits().getFixedValue() < 128) {
static const CostTblEntry DivTbl[]{
{ISD::SDIV, MVT::v2i8, 5}, {ISD::SDIV, MVT::v4i8, 8},
{ISD::SDIV, MVT::v8i8, 8}, {ISD::SDIV, MVT::v2i16, 5},
{ISD::SDIV, MVT::v4i16, 5}, {ISD::SDIV, MVT::v2i32, 1},
{ISD::UDIV, MVT::v2i8, 5}, {ISD::UDIV, MVT::v4i8, 8},
{ISD::UDIV, MVT::v8i8, 8}, {ISD::UDIV, MVT::v2i16, 5},
{ISD::UDIV, MVT::v4i16, 5}, {ISD::UDIV, MVT::v2i32, 1}};
const auto *Entry = CostTableLookup(DivTbl, ISD, VT.getSimpleVT());
if (nullptr != Entry)
return Entry->Cost;
}
// For 8/16-bit elements, the cost is higher because the type
// requires promotion and possibly splitting:
if (LT.second.getScalarType() == MVT::i8)
Cost *= 8;
else if (LT.second.getScalarType() == MVT::i16)
Cost *= 4;
return Cost;
} else {
// If one of the operands is a uniform constant then the cost for each
// element is Cost for insertion, extraction and division.
// Insertion cost = 2, Extraction Cost = 2, Division = cost for the
// operation with scalar type
if ((Op1Info.isConstant() && Op1Info.isUniform()) ||
(Op2Info.isConstant() && Op2Info.isUniform())) {
if (auto *VTy = dyn_cast<FixedVectorType>(Ty)) {
InstructionCost DivCost = BaseT::getArithmeticInstrCost(
Opcode, Ty->getScalarType(), CostKind, Op1Info, Op2Info);
return (4 + DivCost) * VTy->getNumElements();
}
}
// On AArch64, without SVE, vector divisions are expanded
// into scalar divisions of each pair of elements.
Cost += getVectorInstrCost(Instruction::ExtractElement, Ty, CostKind,
-1, nullptr, nullptr);
Cost += getVectorInstrCost(Instruction::InsertElement, Ty, CostKind, -1,
nullptr, nullptr);
}
// TODO: if one of the arguments is scalar, then it's not necessary to
// double the cost of handling the vector elements.
Cost += Cost;
}
return Cost;
}
case ISD::MUL:
// When SVE is available, then we can lower the v2i64 operation using
// the SVE mul instruction, which has a lower cost.
if (LT.second == MVT::v2i64 && ST->hasSVE())
return LT.first;
// When SVE is not available, there is no MUL.2d instruction,
// which means mul <2 x i64> is expensive as elements are extracted
// from the vectors and the muls scalarized.
// As getScalarizationOverhead is a bit too pessimistic, we
// estimate the cost for a i64 vector directly here, which is:
// - four 2-cost i64 extracts,
// - two 2-cost i64 inserts, and
// - two 1-cost muls.
// So, for a v2i64 with LT.First = 1 the cost is 14, and for a v4i64 with
// LT.first = 2 the cost is 28. If both operands are extensions it will not
// need to scalarize so the cost can be cheaper (smull or umull).
// so the cost can be cheaper (smull or umull).
if (LT.second != MVT::v2i64 || isWideningInstruction(Ty, Opcode, Args))
return LT.first;
return cast<VectorType>(Ty)->getElementCount().getKnownMinValue() *
(getArithmeticInstrCost(Opcode, Ty->getScalarType(), CostKind) +
getVectorInstrCost(Instruction::ExtractElement, Ty, CostKind, -1,
nullptr, nullptr) *
2 +
getVectorInstrCost(Instruction::InsertElement, Ty, CostKind, -1,
nullptr, nullptr));
case ISD::ADD:
case ISD::XOR:
case ISD::OR:
case ISD::AND:
case ISD::SRL:
case ISD::SRA:
case ISD::SHL:
// These nodes are marked as 'custom' for combining purposes only.
// We know that they are legal. See LowerAdd in ISelLowering.
return LT.first;
case ISD::FNEG:
// Scalar fmul(fneg) or fneg(fmul) can be converted to fnmul
if ((Ty->isFloatTy() || Ty->isDoubleTy() ||
(Ty->isHalfTy() && ST->hasFullFP16())) &&
CxtI &&
((CxtI->hasOneUse() &&
match(*CxtI->user_begin(), m_FMul(m_Value(), m_Value()))) ||
match(CxtI->getOperand(0), m_FMul(m_Value(), m_Value()))))
return 0;
[[fallthrough]];
case ISD::FADD:
case ISD::FSUB:
if (!Ty->getScalarType()->isFP128Ty())
return LT.first;
[[fallthrough]];
case ISD::FMUL:
case ISD::FDIV:
// These nodes are marked as 'custom' just to lower them to SVE.
// We know said lowering will incur no additional cost.
if (!Ty->getScalarType()->isFP128Ty())
return 2 * LT.first;
return BaseT::getArithmeticInstrCost(Opcode, Ty, CostKind, Op1Info,
Op2Info);
case ISD::FREM:
// Pass nullptr as fmod/fmodf calls are emitted by the backend even when
// those functions are not declared in the module.
if (!Ty->isVectorTy())
return getCallInstrCost(/*Function*/ nullptr, Ty, {Ty, Ty}, CostKind);
return BaseT::getArithmeticInstrCost(Opcode, Ty, CostKind, Op1Info,
Op2Info);
}
}
InstructionCost
AArch64TTIImpl::getAddressComputationCost(Type *PtrTy, ScalarEvolution *SE,
const SCEV *Ptr,
TTI::TargetCostKind CostKind) const {
// Address computations in vectorized code with non-consecutive addresses will
// likely result in more instructions compared to scalar code where the
// computation can more often be merged into the index mode. The resulting
// extra micro-ops can significantly decrease throughput.
unsigned NumVectorInstToHideOverhead = NeonNonConstStrideOverhead;
int MaxMergeDistance = 64;
if (PtrTy->isVectorTy() && SE &&
!BaseT::isConstantStridedAccessLessThan(SE, Ptr, MaxMergeDistance + 1))
return NumVectorInstToHideOverhead;
// In many cases the address computation is not merged into the instruction
// addressing mode.
return 1;
}
/// Check whether Opcode1 has less throughput according to the scheduling
/// model than Opcode2.
bool AArch64TTIImpl::hasKnownLowerThroughputFromSchedulingModel(
unsigned Opcode1, unsigned Opcode2) const {
const MCSchedModel &Sched = ST->getSchedModel();
const TargetInstrInfo *TII = ST->getInstrInfo();
if (!Sched.hasInstrSchedModel())
return false;
const MCSchedClassDesc *SCD1 =
Sched.getSchedClassDesc(TII->get(Opcode1).getSchedClass());
const MCSchedClassDesc *SCD2 =
Sched.getSchedClassDesc(TII->get(Opcode2).getSchedClass());
// We cannot handle variant scheduling classes without an MI. If we need to
// support them for any of the instructions we query the information of we
// might need to add a way to resolve them without a MI or not use the
// scheduling info.
assert(!SCD1->isVariant() && !SCD2->isVariant() &&
"Cannot handle variant scheduling classes without an MI");
if (!SCD1->isValid() || !SCD2->isValid())
return false;
return MCSchedModel::getReciprocalThroughput(*ST, *SCD1) >
MCSchedModel::getReciprocalThroughput(*ST, *SCD2);
}
InstructionCost AArch64TTIImpl::getCmpSelInstrCost(
unsigned Opcode, Type *ValTy, Type *CondTy, CmpInst::Predicate VecPred,
TTI::TargetCostKind CostKind, TTI::OperandValueInfo Op1Info,
TTI::OperandValueInfo Op2Info, const Instruction *I) const {
// We don't lower some vector selects well that are wider than the register
// width. TODO: Improve this with different cost kinds.
if (isa<FixedVectorType>(ValTy) && Opcode == Instruction::Select) {
// We would need this many instructions to hide the scalarization happening.
const int AmortizationCost = 20;
// If VecPred is not set, check if we can get a predicate from the context
// instruction, if its type matches the requested ValTy.
if (VecPred == CmpInst::BAD_ICMP_PREDICATE && I && I->getType() == ValTy) {
CmpPredicate CurrentPred;
if (match(I, m_Select(m_Cmp(CurrentPred, m_Value(), m_Value()), m_Value(),
m_Value())))
VecPred = CurrentPred;
}
// Check if we have a compare/select chain that can be lowered using
// a (F)CMxx & BFI pair.
if (CmpInst::isIntPredicate(VecPred) || VecPred == CmpInst::FCMP_OLE ||
VecPred == CmpInst::FCMP_OLT || VecPred == CmpInst::FCMP_OGT ||
VecPred == CmpInst::FCMP_OGE || VecPred == CmpInst::FCMP_OEQ ||
VecPred == CmpInst::FCMP_UNE) {
static const auto ValidMinMaxTys = {
MVT::v8i8, MVT::v16i8, MVT::v4i16, MVT::v8i16, MVT::v2i32,
MVT::v4i32, MVT::v2i64, MVT::v2f32, MVT::v4f32, MVT::v2f64};
static const auto ValidFP16MinMaxTys = {MVT::v4f16, MVT::v8f16};
auto LT = getTypeLegalizationCost(ValTy);
if (any_of(ValidMinMaxTys, [<](MVT M) { return M == LT.second; }) ||
(ST->hasFullFP16() &&
any_of(ValidFP16MinMaxTys, [<](MVT M) { return M == LT.second; })))
return LT.first;
}
static const TypeConversionCostTblEntry VectorSelectTbl[] = {
{Instruction::Select, MVT::v2i1, MVT::v2f32, 2},
{Instruction::Select, MVT::v2i1, MVT::v2f64, 2},
{Instruction::Select, MVT::v4i1, MVT::v4f32, 2},
{Instruction::Select, MVT::v4i1, MVT::v4f16, 2},
{Instruction::Select, MVT::v8i1, MVT::v8f16, 2},
{Instruction::Select, MVT::v16i1, MVT::v16i16, 16},
{Instruction::Select, MVT::v8i1, MVT::v8i32, 8},
{Instruction::Select, MVT::v16i1, MVT::v16i32, 16},
{Instruction::Select, MVT::v4i1, MVT::v4i64, 4 * AmortizationCost},
{Instruction::Select, MVT::v8i1, MVT::v8i64, 8 * AmortizationCost},
{Instruction::Select, MVT::v16i1, MVT::v16i64, 16 * AmortizationCost}};
EVT SelCondTy = TLI->getValueType(DL, CondTy);
EVT SelValTy = TLI->getValueType(DL, ValTy);
if (SelCondTy.isSimple() && SelValTy.isSimple()) {
if (const auto *Entry = ConvertCostTableLookup(VectorSelectTbl, Opcode,
SelCondTy.getSimpleVT(),
SelValTy.getSimpleVT()))
return Entry->Cost;
}
}
if (Opcode == Instruction::FCmp) {
if (auto PromotedCost = getFP16BF16PromoteCost(
ValTy, CostKind, Op1Info, Op2Info, /*IncludeTrunc=*/false,
[&](Type *PromotedTy) {
InstructionCost Cost =
getCmpSelInstrCost(Opcode, PromotedTy, CondTy, VecPred,
CostKind, Op1Info, Op2Info);
if (isa<VectorType>(PromotedTy))
Cost += getCastInstrCost(
Instruction::Trunc,
VectorType::getInteger(cast<VectorType>(ValTy)),
VectorType::getInteger(cast<VectorType>(PromotedTy)),
TTI::CastContextHint::None, CostKind);
return Cost;
}))
return *PromotedCost;
auto LT = getTypeLegalizationCost(ValTy);
// Model unknown fp compares as a libcall.
if (LT.second.getScalarType() != MVT::f64 &&
LT.second.getScalarType() != MVT::f32 &&
LT.second.getScalarType() != MVT::f16)
return LT.first * getCallInstrCost(/*Function*/ nullptr, ValTy,
{ValTy, ValTy}, CostKind);
// Some comparison operators require expanding to multiple compares + or.
unsigned Factor = 1;
if (!CondTy->isVectorTy() &&
(VecPred == FCmpInst::FCMP_ONE || VecPred == FCmpInst::FCMP_UEQ))
Factor = 2; // fcmp with 2 selects
else if (isa<FixedVectorType>(ValTy) &&
(VecPred == FCmpInst::FCMP_ONE || VecPred == FCmpInst::FCMP_UEQ ||
VecPred == FCmpInst::FCMP_ORD || VecPred == FCmpInst::FCMP_UNO))
Factor = 3; // fcmxx+fcmyy+or
else if (isa<ScalableVectorType>(ValTy) &&
(VecPred == FCmpInst::FCMP_ONE || VecPred == FCmpInst::FCMP_UEQ))
Factor = 3; // fcmxx+fcmyy+or
if (isa<ScalableVectorType>(ValTy) &&
CostKind == TTI::TCK_RecipThroughput &&
hasKnownLowerThroughputFromSchedulingModel(AArch64::FCMEQ_PPzZZ_S,
AArch64::FCMEQv4f32))
Factor *= 2;
return Factor * (CostKind == TTI::TCK_Latency ? 2 : LT.first);
}
// Treat the icmp in icmp(and, 0) or icmp(and, -1/1) when it can be folded to
// icmp(and, 0) as free, as we can make use of ands, but only if the
// comparison is not unsigned. FIXME: Enable for non-throughput cost kinds
// providing it will not cause performance regressions.
if (CostKind == TTI::TCK_RecipThroughput && ValTy->isIntegerTy() &&
Opcode == Instruction::ICmp && I && !CmpInst::isUnsigned(VecPred) &&
TLI->isTypeLegal(TLI->getValueType(DL, ValTy)) &&
match(I->getOperand(0), m_And(m_Value(), m_Value()))) {
if (match(I->getOperand(1), m_Zero()))
return 0;
// x >= 1 / x < 1 -> x > 0 / x <= 0
if (match(I->getOperand(1), m_One()) &&
(VecPred == CmpInst::ICMP_SLT || VecPred == CmpInst::ICMP_SGE))
return 0;
// x <= -1 / x > -1 -> x > 0 / x <= 0
if (match(I->getOperand(1), m_AllOnes()) &&
(VecPred == CmpInst::ICMP_SLE || VecPred == CmpInst::ICMP_SGT))
return 0;
}
// The base case handles scalable vectors fine for now, since it treats the
// cost as 1 * legalization cost.
return BaseT::getCmpSelInstrCost(Opcode, ValTy, CondTy, VecPred, CostKind,
Op1Info, Op2Info, I);
}
AArch64TTIImpl::TTI::MemCmpExpansionOptions
AArch64TTIImpl::enableMemCmpExpansion(bool OptSize, bool IsZeroCmp) const {
TTI::MemCmpExpansionOptions Options;
if (ST->requiresStrictAlign()) {
// TODO: Add cost modeling for strict align. Misaligned loads expand to
// a bunch of instructions when strict align is enabled.
return Options;
}
Options.AllowOverlappingLoads = true;
Options.MaxNumLoads = TLI->getMaxExpandSizeMemcmp(OptSize);
Options.NumLoadsPerBlock = Options.MaxNumLoads;
// TODO: Though vector loads usually perform well on AArch64, in some targets
// they may wake up the FP unit, which raises the power consumption. Perhaps
// they could be used with no holds barred (-O3).
Options.LoadSizes = {8, 4, 2, 1};
Options.AllowedTailExpansions = {3, 5, 6};
return Options;
}
bool AArch64TTIImpl::prefersVectorizedAddressing() const {
return ST->hasSVE();
}
InstructionCost
AArch64TTIImpl::getMaskedMemoryOpCost(unsigned Opcode, Type *Src,
Align Alignment, unsigned AddressSpace,
TTI::TargetCostKind CostKind) const {
if (useNeonVector(Src))
return BaseT::getMaskedMemoryOpCost(Opcode, Src, Alignment, AddressSpace,
CostKind);
auto LT = getTypeLegalizationCost(Src);
if (!LT.first.isValid())
return InstructionCost::getInvalid();
// Return an invalid cost for element types that we are unable to lower.
auto *VT = cast<VectorType>(Src);
if (VT->getElementType()->isIntegerTy(1))
return InstructionCost::getInvalid();
// The code-generator is currently not able to handle scalable vectors
// of <vscale x 1 x eltty> yet, so return an invalid cost to avoid selecting
// it. This change will be removed when code-generation for these types is
// sufficiently reliable.
if (VT->getElementCount() == ElementCount::getScalable(1))
return InstructionCost::getInvalid();
return LT.first;
}
// This function returns gather/scatter overhead either from
// user-provided value or specialized values per-target from \p ST.
static unsigned getSVEGatherScatterOverhead(unsigned Opcode,
const AArch64Subtarget *ST) {
assert((Opcode == Instruction::Load || Opcode == Instruction::Store) &&
"Should be called on only load or stores.");
switch (Opcode) {
case Instruction::Load:
if (SVEGatherOverhead.getNumOccurrences() > 0)
return SVEGatherOverhead;
return ST->getGatherOverhead();
break;
case Instruction::Store:
if (SVEScatterOverhead.getNumOccurrences() > 0)
return SVEScatterOverhead;
return ST->getScatterOverhead();
break;
default:
llvm_unreachable("Shouldn't have reached here");
}
}
InstructionCost AArch64TTIImpl::getGatherScatterOpCost(
unsigned Opcode, Type *DataTy, const Value *Ptr, bool VariableMask,
Align Alignment, TTI::TargetCostKind CostKind, const Instruction *I) const {
if (useNeonVector(DataTy) || !isLegalMaskedGatherScatter(DataTy))
return BaseT::getGatherScatterOpCost(Opcode, DataTy, Ptr, VariableMask,
Alignment, CostKind, I);
auto *VT = cast<VectorType>(DataTy);
auto LT = getTypeLegalizationCost(DataTy);
if (!LT.first.isValid())
return InstructionCost::getInvalid();
// Return an invalid cost for element types that we are unable to lower.
if (!LT.second.isVector() ||
!isElementTypeLegalForScalableVector(VT->getElementType()) ||
VT->getElementType()->isIntegerTy(1))
return InstructionCost::getInvalid();
// The code-generator is currently not able to handle scalable vectors
// of <vscale x 1 x eltty> yet, so return an invalid cost to avoid selecting
// it. This change will be removed when code-generation for these types is
// sufficiently reliable.
if (VT->getElementCount() == ElementCount::getScalable(1))
return InstructionCost::getInvalid();
ElementCount LegalVF = LT.second.getVectorElementCount();
InstructionCost MemOpCost =
getMemoryOpCost(Opcode, VT->getElementType(), Alignment, 0, CostKind,
{TTI::OK_AnyValue, TTI::OP_None}, I);
// Add on an overhead cost for using gathers/scatters.
MemOpCost *= getSVEGatherScatterOverhead(Opcode, ST);
return LT.first * MemOpCost * getMaxNumElements(LegalVF);
}
bool AArch64TTIImpl::useNeonVector(const Type *Ty) const {
return isa<FixedVectorType>(Ty) && !ST->useSVEForFixedLengthVectors();
}
InstructionCost AArch64TTIImpl::getMemoryOpCost(unsigned Opcode, Type *Ty,
Align Alignment,
unsigned AddressSpace,
TTI::TargetCostKind CostKind,
TTI::OperandValueInfo OpInfo,
const Instruction *I) const {
EVT VT = TLI->getValueType(DL, Ty, true);
// Type legalization can't handle structs
if (VT == MVT::Other)
return BaseT::getMemoryOpCost(Opcode, Ty, Alignment, AddressSpace,
CostKind);
auto LT = getTypeLegalizationCost(Ty);
if (!LT.first.isValid())
return InstructionCost::getInvalid();
// The code-generator is currently not able to handle scalable vectors
// of <vscale x 1 x eltty> yet, so return an invalid cost to avoid selecting
// it. This change will be removed when code-generation for these types is
// sufficiently reliable.
// We also only support full register predicate loads and stores.
if (auto *VTy = dyn_cast<ScalableVectorType>(Ty))
if (VTy->getElementCount() == ElementCount::getScalable(1) ||
(VTy->getElementType()->isIntegerTy(1) &&
!VTy->getElementCount().isKnownMultipleOf(
ElementCount::getScalable(16))))
return InstructionCost::getInvalid();
// TODO: consider latency as well for TCK_SizeAndLatency.
if (CostKind == TTI::TCK_CodeSize || CostKind == TTI::TCK_SizeAndLatency)
return LT.first;
if (CostKind != TTI::TCK_RecipThroughput)
return 1;
if (ST->isMisaligned128StoreSlow() && Opcode == Instruction::Store &&
LT.second.is128BitVector() && Alignment < Align(16)) {
// Unaligned stores are extremely inefficient. We don't split all
// unaligned 128-bit stores because the negative impact that has shown in
// practice on inlined block copy code.
// We make such stores expensive so that we will only vectorize if there
// are 6 other instructions getting vectorized.
const int AmortizationCost = 6;
return LT.first * 2 * AmortizationCost;
}
// Opaque ptr or ptr vector types are i64s and can be lowered to STP/LDPs.
if (Ty->isPtrOrPtrVectorTy())
return LT.first;
if (useNeonVector(Ty)) {
// Check truncating stores and extending loads.
if (Ty->getScalarSizeInBits() != LT.second.getScalarSizeInBits()) {
// v4i8 types are lowered to scalar a load/store and sshll/xtn.
if (VT == MVT::v4i8)
return 2;
// Otherwise we need to scalarize.
return cast<FixedVectorType>(Ty)->getNumElements() * 2;
}
EVT EltVT = VT.getVectorElementType();
unsigned EltSize = EltVT.getScalarSizeInBits();
if (!isPowerOf2_32(EltSize) || EltSize < 8 || EltSize > 64 ||
VT.getVectorNumElements() >= (128 / EltSize) || Alignment != Align(1))
return LT.first;
// FIXME: v3i8 lowering currently is very inefficient, due to automatic
// widening to v4i8, which produces suboptimal results.
if (VT.getVectorNumElements() == 3 && EltVT == MVT::i8)
return LT.first;
// Check non-power-of-2 loads/stores for legal vector element types with
// NEON. Non-power-of-2 memory ops will get broken down to a set of
// operations on smaller power-of-2 ops, including ld1/st1.
LLVMContext &C = Ty->getContext();
InstructionCost Cost(0);
SmallVector<EVT> TypeWorklist;
TypeWorklist.push_back(VT);
while (!TypeWorklist.empty()) {
EVT CurrVT = TypeWorklist.pop_back_val();
unsigned CurrNumElements = CurrVT.getVectorNumElements();
if (isPowerOf2_32(CurrNumElements)) {
Cost += 1;
continue;
}
unsigned PrevPow2 = NextPowerOf2(CurrNumElements) / 2;
TypeWorklist.push_back(EVT::getVectorVT(C, EltVT, PrevPow2));
TypeWorklist.push_back(
EVT::getVectorVT(C, EltVT, CurrNumElements - PrevPow2));
}
return Cost;
}
return LT.first;
}
InstructionCost AArch64TTIImpl::getInterleavedMemoryOpCost(
unsigned Opcode, Type *VecTy, unsigned Factor, ArrayRef<unsigned> Indices,
Align Alignment, unsigned AddressSpace, TTI::TargetCostKind CostKind,
bool UseMaskForCond, bool UseMaskForGaps) const {
assert(Factor >= 2 && "Invalid interleave factor");
auto *VecVTy = cast<VectorType>(VecTy);
if (VecTy->isScalableTy() && !ST->hasSVE())
return InstructionCost::getInvalid();
// Scalable VFs will emit vector.[de]interleave intrinsics, and currently we
// only have lowering for power-of-2 factors.
// TODO: Add lowering for vector.[de]interleave3 intrinsics and support in
// InterleavedAccessPass for ld3/st3
if (VecTy->isScalableTy() && !isPowerOf2_32(Factor))
return InstructionCost::getInvalid();
// Vectorization for masked interleaved accesses is only enabled for scalable
// VF.
if (!VecTy->isScalableTy() && (UseMaskForCond || UseMaskForGaps))
return InstructionCost::getInvalid();
if (!UseMaskForGaps && Factor <= TLI->getMaxSupportedInterleaveFactor()) {
unsigned MinElts = VecVTy->getElementCount().getKnownMinValue();
auto *SubVecTy =
VectorType::get(VecVTy->getElementType(),
VecVTy->getElementCount().divideCoefficientBy(Factor));
// ldN/stN only support legal vector types of size 64 or 128 in bits.
// Accesses having vector types that are a multiple of 128 bits can be
// matched to more than one ldN/stN instruction.
bool UseScalable;
if (MinElts % Factor == 0 &&
TLI->isLegalInterleavedAccessType(SubVecTy, DL, UseScalable))
return Factor * TLI->getNumInterleavedAccesses(SubVecTy, DL, UseScalable);
}
return BaseT::getInterleavedMemoryOpCost(Opcode, VecTy, Factor, Indices,
Alignment, AddressSpace, CostKind,
UseMaskForCond, UseMaskForGaps);
}
InstructionCost
AArch64TTIImpl::getCostOfKeepingLiveOverCall(ArrayRef<Type *> Tys) const {
InstructionCost Cost = 0;
TTI::TargetCostKind CostKind = TTI::TCK_RecipThroughput;
for (auto *I : Tys) {
if (!I->isVectorTy())
continue;
if (I->getScalarSizeInBits() * cast<FixedVectorType>(I)->getNumElements() ==
128)
Cost += getMemoryOpCost(Instruction::Store, I, Align(128), 0, CostKind) +
getMemoryOpCost(Instruction::Load, I, Align(128), 0, CostKind);
}
return Cost;
}
unsigned AArch64TTIImpl::getMaxInterleaveFactor(ElementCount VF) const {
return ST->getMaxInterleaveFactor();
}
// For Falkor, we want to avoid having too many strided loads in a loop since
// that can exhaust the HW prefetcher resources. We adjust the unroller
// MaxCount preference below to attempt to ensure unrolling doesn't create too
// many strided loads.
static void
getFalkorUnrollingPreferences(Loop *L, ScalarEvolution &SE,
TargetTransformInfo::UnrollingPreferences &UP) {
enum { MaxStridedLoads = 7 };
auto countStridedLoads = [](Loop *L, ScalarEvolution &SE) {
int StridedLoads = 0;
// FIXME? We could make this more precise by looking at the CFG and
// e.g. not counting loads in each side of an if-then-else diamond.
for (const auto BB : L->blocks()) {
for (auto &I : *BB) {
LoadInst *LMemI = dyn_cast<LoadInst>(&I);
if (!LMemI)
continue;
Value *PtrValue = LMemI->getPointerOperand();
if (L->isLoopInvariant(PtrValue))
continue;
const SCEV *LSCEV = SE.getSCEV(PtrValue);
const SCEVAddRecExpr *LSCEVAddRec = dyn_cast<SCEVAddRecExpr>(LSCEV);
if (!LSCEVAddRec || !LSCEVAddRec->isAffine())
continue;
// FIXME? We could take pairing of unrolled load copies into account
// by looking at the AddRec, but we would probably have to limit this
// to loops with no stores or other memory optimization barriers.
++StridedLoads;
// We've seen enough strided loads that seeing more won't make a
// difference.
if (StridedLoads > MaxStridedLoads / 2)
return StridedLoads;
}
}
return StridedLoads;
};
int StridedLoads = countStridedLoads(L, SE);
LLVM_DEBUG(dbgs() << "falkor-hwpf: detected " << StridedLoads
<< " strided loads\n");
// Pick the largest power of 2 unroll count that won't result in too many
// strided loads.
if (StridedLoads) {
UP.MaxCount = 1 << Log2_32(MaxStridedLoads / StridedLoads);
LLVM_DEBUG(dbgs() << "falkor-hwpf: setting unroll MaxCount to "
<< UP.MaxCount << '\n');
}
}
// This function returns true if the loop:
// 1. Has a valid cost, and
// 2. Has a cost within the supplied budget.
// Otherwise it returns false.
static bool isLoopSizeWithinBudget(Loop *L, const AArch64TTIImpl &TTI,
InstructionCost Budget,
unsigned *FinalSize) {
// Estimate the size of the loop.
InstructionCost LoopCost = 0;
for (auto *BB : L->getBlocks()) {
for (auto &I : *BB) {
SmallVector<const Value *, 4> Operands(I.operand_values());
InstructionCost Cost =
TTI.getInstructionCost(&I, Operands, TTI::TCK_CodeSize);
// This can happen with intrinsics that don't currently have a cost model
// or for some operations that require SVE.
if (!Cost.isValid())
return false;
LoopCost += Cost;
if (LoopCost > Budget)
return false;
}
}
if (FinalSize)
*FinalSize = LoopCost.getValue();
return true;
}
static bool shouldUnrollMultiExitLoop(Loop *L, ScalarEvolution &SE,
const AArch64TTIImpl &TTI) {
// Only consider loops with unknown trip counts for which we can determine
// a symbolic expression. Multi-exit loops with small known trip counts will
// likely be unrolled anyway.
const SCEV *BTC = SE.getSymbolicMaxBackedgeTakenCount(L);
if (isa<SCEVConstant>(BTC) || isa<SCEVCouldNotCompute>(BTC))
return false;
// It might not be worth unrolling loops with low max trip counts. Restrict
// this to max trip counts > 32 for now.
unsigned MaxTC = SE.getSmallConstantMaxTripCount(L);
if (MaxTC > 0 && MaxTC <= 32)
return false;
// Make sure the loop size is <= 5.
if (!isLoopSizeWithinBudget(L, TTI, 5, nullptr))
return false;
// Small search loops with multiple exits can be highly beneficial to unroll.
// We only care about loops with exactly two exiting blocks, although each
// block could jump to the same exit block.
ArrayRef<BasicBlock *> Blocks = L->getBlocks();
if (Blocks.size() != 2)
return false;
if (any_of(Blocks, [](BasicBlock *BB) {
return !isa<BranchInst>(BB->getTerminator());
}))
return false;
return true;
}
/// For Apple CPUs, we want to runtime-unroll loops to make better use if the
/// OOO engine's wide instruction window and various predictors.
static void
getAppleRuntimeUnrollPreferences(Loop *L, ScalarEvolution &SE,
TargetTransformInfo::UnrollingPreferences &UP,
const AArch64TTIImpl &TTI) {
// Limit loops with structure that is highly likely to benefit from runtime
// unrolling; that is we exclude outer loops and loops with many blocks (i.e.
// likely with complex control flow). Note that the heuristics here may be
// overly conservative and we err on the side of avoiding runtime unrolling
// rather than unroll excessively. They are all subject to further refinement.
if (!L->isInnermost() || L->getNumBlocks() > 8)
return;
// Loops with multiple exits are handled by common code.
if (!L->getExitBlock())
return;
// Check if the loop contains any reductions that could be parallelized when
// unrolling. If so, enable partial unrolling, if the trip count is know to be
// a multiple of 2.
bool HasParellelizableReductions =
L->getNumBlocks() == 1 &&
any_of(L->getHeader()->phis(),
[&SE, L](PHINode &Phi) {
return canParallelizeReductionWhenUnrolling(Phi, L, &SE);
}) &&
isLoopSizeWithinBudget(L, TTI, 12, nullptr);
if (HasParellelizableReductions &&
SE.getSmallConstantTripMultiple(L, L->getExitingBlock()) % 2 == 0) {
UP.Partial = true;
UP.MaxCount = 4;
UP.AddAdditionalAccumulators = true;
}
const SCEV *BTC = SE.getSymbolicMaxBackedgeTakenCount(L);
if (isa<SCEVConstant>(BTC) || isa<SCEVCouldNotCompute>(BTC) ||
(SE.getSmallConstantMaxTripCount(L) > 0 &&
SE.getSmallConstantMaxTripCount(L) <= 32))
return;
if (findStringMetadataForLoop(L, "llvm.loop.isvectorized"))
return;
if (SE.getSymbolicMaxBackedgeTakenCount(L) != SE.getBackedgeTakenCount(L))
return;
// Limit to loops with trip counts that are cheap to expand.
UP.SCEVExpansionBudget = 1;
if (HasParellelizableReductions) {
UP.Runtime = true;
UP.DefaultUnrollRuntimeCount = 4;
UP.AddAdditionalAccumulators = true;
}
// Try to unroll small loops, of few-blocks with low budget, if they have
// load/store dependencies, to expose more parallel memory access streams,
// or if they do little work inside a block (i.e. load -> X -> store pattern).
BasicBlock *Header = L->getHeader();
BasicBlock *Latch = L->getLoopLatch();
if (Header == Latch) {
// Estimate the size of the loop.
unsigned Size;
unsigned Width = 10;
if (!isLoopSizeWithinBudget(L, TTI, Width, &Size))
return;
// Try to find an unroll count that maximizes the use of the instruction
// window, i.e. trying to fetch as many instructions per cycle as possible.
unsigned MaxInstsPerLine = 16;
unsigned UC = 1;
unsigned BestUC = 1;
unsigned SizeWithBestUC = BestUC * Size;
while (UC <= 8) {
unsigned SizeWithUC = UC * Size;
if (SizeWithUC > 48)
break;
if ((SizeWithUC % MaxInstsPerLine) == 0 ||
(SizeWithBestUC % MaxInstsPerLine) < (SizeWithUC % MaxInstsPerLine)) {
BestUC = UC;
SizeWithBestUC = BestUC * Size;
}
UC++;
}
if (BestUC == 1)
return;
SmallPtrSet<Value *, 8> LoadedValuesPlus;
SmallVector<StoreInst *> Stores;
for (auto *BB : L->blocks()) {
for (auto &I : *BB) {
Value *Ptr = getLoadStorePointerOperand(&I);
if (!Ptr)
continue;
const SCEV *PtrSCEV = SE.getSCEV(Ptr);
if (SE.isLoopInvariant(PtrSCEV, L))
continue;
if (isa<LoadInst>(&I)) {
LoadedValuesPlus.insert(&I);
// Include in-loop 1st users of loaded values.
for (auto *U : I.users())
if (L->contains(cast<Instruction>(U)))
LoadedValuesPlus.insert(U);
} else
Stores.push_back(cast<StoreInst>(&I));
}
}
if (none_of(Stores, [&LoadedValuesPlus](StoreInst *SI) {
return LoadedValuesPlus.contains(SI->getOperand(0));
}))
return;
UP.Runtime = true;
UP.DefaultUnrollRuntimeCount = BestUC;
return;
}
// Try to runtime-unroll loops with early-continues depending on loop-varying
// loads; this helps with branch-prediction for the early-continues.
auto *Term = dyn_cast<BranchInst>(Header->getTerminator());
SmallVector<BasicBlock *> Preds(predecessors(Latch));
if (!Term || !Term->isConditional() || Preds.size() == 1 ||
!llvm::is_contained(Preds, Header) ||
none_of(Preds, [L](BasicBlock *Pred) { return L->contains(Pred); }))
return;
std::function<bool(Instruction *, unsigned)> DependsOnLoopLoad =
[&](Instruction *I, unsigned Depth) -> bool {
if (isa<PHINode>(I) || L->isLoopInvariant(I) || Depth > 8)
return false;
if (isa<LoadInst>(I))
return true;
return any_of(I->operands(), [&](Value *V) {
auto *I = dyn_cast<Instruction>(V);
return I && DependsOnLoopLoad(I, Depth + 1);
});
};
CmpPredicate Pred;
Instruction *I;
if (match(Term, m_Br(m_ICmp(Pred, m_Instruction(I), m_Value()), m_Value(),
m_Value())) &&
DependsOnLoopLoad(I, 0)) {
UP.Runtime = true;
}
}
void AArch64TTIImpl::getUnrollingPreferences(
Loop *L, ScalarEvolution &SE, TTI::UnrollingPreferences &UP,
OptimizationRemarkEmitter *ORE) const {
// Enable partial unrolling and runtime unrolling.
BaseT::getUnrollingPreferences(L, SE, UP, ORE);
UP.UpperBound = true;
// For inner loop, it is more likely to be a hot one, and the runtime check
// can be promoted out from LICM pass, so the overhead is less, let's try
// a larger threshold to unroll more loops.
if (L->getLoopDepth() > 1)
UP.PartialThreshold *= 2;
// Disable partial & runtime unrolling on -Os.
UP.PartialOptSizeThreshold = 0;
// Scan the loop: don't unroll loops with calls as this could prevent
// inlining. Don't unroll auto-vectorized loops either, though do allow
// unrolling of the scalar remainder.
bool IsVectorized = getBooleanLoopAttribute(L, "llvm.loop.isvectorized");
for (auto *BB : L->getBlocks()) {
for (auto &I : *BB) {
// Both auto-vectorized loops and the scalar remainder have the
// isvectorized attribute, so differentiate between them by the presence
// of vector instructions.
if (IsVectorized && I.getType()->isVectorTy())
return;
if (isa<CallBase>(I)) {
if (isa<CallInst>(I) || isa<InvokeInst>(I))
if (const Function *F = cast<CallBase>(I).getCalledFunction())
if (!isLoweredToCall(F))
continue;
return;
}
}
}
// Apply subtarget-specific unrolling preferences.
switch (ST->getProcFamily()) {
case AArch64Subtarget::AppleA14:
case AArch64Subtarget::AppleA15:
case AArch64Subtarget::AppleA16:
case AArch64Subtarget::AppleM4:
getAppleRuntimeUnrollPreferences(L, SE, UP, *this);
break;
case AArch64Subtarget::Falkor:
if (EnableFalkorHWPFUnrollFix)
getFalkorUnrollingPreferences(L, SE, UP);
break;
default:
break;
}
// If this is a small, multi-exit loop similar to something like std::find,
// then there is typically a performance improvement achieved by unrolling.
if (!L->getExitBlock() && shouldUnrollMultiExitLoop(L, SE, *this)) {
UP.RuntimeUnrollMultiExit = true;
UP.Runtime = true;
// Limit unroll count.
UP.DefaultUnrollRuntimeCount = 4;
// Allow slightly more costly trip-count expansion to catch search loops
// with pointer inductions.
UP.SCEVExpansionBudget = 5;
return;
}
// Enable runtime unrolling for in-order models
// If mcpu is omitted, getProcFamily() returns AArch64Subtarget::Others, so by
// checking for that case, we can ensure that the default behaviour is
// unchanged
if (ST->getProcFamily() != AArch64Subtarget::Generic &&
!ST->getSchedModel().isOutOfOrder()) {
UP.Runtime = true;
UP.Partial = true;
UP.UnrollRemainder = true;
UP.DefaultUnrollRuntimeCount = 4;
UP.UnrollAndJam = true;
UP.UnrollAndJamInnerLoopThreshold = 60;
}
}
void AArch64TTIImpl::getPeelingPreferences(Loop *L, ScalarEvolution &SE,
TTI::PeelingPreferences &PP) const {
BaseT::getPeelingPreferences(L, SE, PP);
}
Value *AArch64TTIImpl::getOrCreateResultFromMemIntrinsic(IntrinsicInst *Inst,
Type *ExpectedType,
bool CanCreate) const {
switch (Inst->getIntrinsicID()) {
default:
return nullptr;
case Intrinsic::aarch64_neon_st2:
case Intrinsic::aarch64_neon_st3:
case Intrinsic::aarch64_neon_st4: {
// Create a struct type
StructType *ST = dyn_cast<StructType>(ExpectedType);
if (!CanCreate || !ST)
return nullptr;
unsigned NumElts = Inst->arg_size() - 1;
if (ST->getNumElements() != NumElts)
return nullptr;
for (unsigned i = 0, e = NumElts; i != e; ++i) {
if (Inst->getArgOperand(i)->getType() != ST->getElementType(i))
return nullptr;
}
Value *Res = PoisonValue::get(ExpectedType);
IRBuilder<> Builder(Inst);
for (unsigned i = 0, e = NumElts; i != e; ++i) {
Value *L = Inst->getArgOperand(i);
Res = Builder.CreateInsertValue(Res, L, i);
}
return Res;
}
case Intrinsic::aarch64_neon_ld2:
case Intrinsic::aarch64_neon_ld3:
case Intrinsic::aarch64_neon_ld4:
if (Inst->getType() == ExpectedType)
return Inst;
return nullptr;
}
}
bool AArch64TTIImpl::getTgtMemIntrinsic(IntrinsicInst *Inst,
MemIntrinsicInfo &Info) const {
switch (Inst->getIntrinsicID()) {
default:
break;
case Intrinsic::aarch64_neon_ld2:
case Intrinsic::aarch64_neon_ld3:
case Intrinsic::aarch64_neon_ld4:
Info.ReadMem = true;
Info.WriteMem = false;
Info.PtrVal = Inst->getArgOperand(0);
break;
case Intrinsic::aarch64_neon_st2:
case Intrinsic::aarch64_neon_st3:
case Intrinsic::aarch64_neon_st4:
Info.ReadMem = false;
Info.WriteMem = true;
Info.PtrVal = Inst->getArgOperand(Inst->arg_size() - 1);
break;
}
switch (Inst->getIntrinsicID()) {
default:
return false;
case Intrinsic::aarch64_neon_ld2:
case Intrinsic::aarch64_neon_st2:
Info.MatchingId = VECTOR_LDST_TWO_ELEMENTS;
break;
case Intrinsic::aarch64_neon_ld3:
case Intrinsic::aarch64_neon_st3:
Info.MatchingId = VECTOR_LDST_THREE_ELEMENTS;
break;
case Intrinsic::aarch64_neon_ld4:
case Intrinsic::aarch64_neon_st4:
Info.MatchingId = VECTOR_LDST_FOUR_ELEMENTS;
break;
}
return true;
}
/// See if \p I should be considered for address type promotion. We check if \p
/// I is a sext with right type and used in memory accesses. If it used in a
/// "complex" getelementptr, we allow it to be promoted without finding other
/// sext instructions that sign extended the same initial value. A getelementptr
/// is considered as "complex" if it has more than 2 operands.
bool AArch64TTIImpl::shouldConsiderAddressTypePromotion(
const Instruction &I, bool &AllowPromotionWithoutCommonHeader) const {
bool Considerable = false;
AllowPromotionWithoutCommonHeader = false;
if (!isa<SExtInst>(&I))
return false;
Type *ConsideredSExtType =
Type::getInt64Ty(I.getParent()->getParent()->getContext());
if (I.getType() != ConsideredSExtType)
return false;
// See if the sext is the one with the right type and used in at least one
// GetElementPtrInst.
for (const User *U : I.users()) {
if (const GetElementPtrInst *GEPInst = dyn_cast<GetElementPtrInst>(U)) {
Considerable = true;
// A getelementptr is considered as "complex" if it has more than 2
// operands. We will promote a SExt used in such complex GEP as we
// expect some computation to be merged if they are done on 64 bits.
if (GEPInst->getNumOperands() > 2) {
AllowPromotionWithoutCommonHeader = true;
break;
}
}
}
return Considerable;
}
bool AArch64TTIImpl::isLegalToVectorizeReduction(
const RecurrenceDescriptor &RdxDesc, ElementCount VF) const {
if (!VF.isScalable())
return true;
Type *Ty = RdxDesc.getRecurrenceType();
if (Ty->isBFloatTy() || !isElementTypeLegalForScalableVector(Ty))
return false;
switch (RdxDesc.getRecurrenceKind()) {
case RecurKind::Sub:
case RecurKind::AddChainWithSubs:
case RecurKind::Add:
case RecurKind::FAdd:
case RecurKind::And:
case RecurKind::Or:
case RecurKind::Xor:
case RecurKind::SMin:
case RecurKind::SMax:
case RecurKind::UMin:
case RecurKind::UMax:
case RecurKind::FMin:
case RecurKind::FMax:
case RecurKind::FMulAdd:
case RecurKind::AnyOf:
return true;
default:
return false;
}
}
InstructionCost
AArch64TTIImpl::getMinMaxReductionCost(Intrinsic::ID IID, VectorType *Ty,
FastMathFlags FMF,
TTI::TargetCostKind CostKind) const {
// The code-generator is currently not able to handle scalable vectors
// of <vscale x 1 x eltty> yet, so return an invalid cost to avoid selecting
// it. This change will be removed when code-generation for these types is
// sufficiently reliable.
if (auto *VTy = dyn_cast<ScalableVectorType>(Ty))
if (VTy->getElementCount() == ElementCount::getScalable(1))
return InstructionCost::getInvalid();
std::pair<InstructionCost, MVT> LT = getTypeLegalizationCost(Ty);
if (LT.second.getScalarType() == MVT::f16 && !ST->hasFullFP16())
return BaseT::getMinMaxReductionCost(IID, Ty, FMF, CostKind);
InstructionCost LegalizationCost = 0;
if (LT.first > 1) {
Type *LegalVTy = EVT(LT.second).getTypeForEVT(Ty->getContext());
IntrinsicCostAttributes Attrs(IID, LegalVTy, {LegalVTy, LegalVTy}, FMF);
LegalizationCost = getIntrinsicInstrCost(Attrs, CostKind) * (LT.first - 1);
}
return LegalizationCost + /*Cost of horizontal reduction*/ 2;
}
InstructionCost AArch64TTIImpl::getArithmeticReductionCostSVE(
unsigned Opcode, VectorType *ValTy, TTI::TargetCostKind CostKind) const {
std::pair<InstructionCost, MVT> LT = getTypeLegalizationCost(ValTy);
InstructionCost LegalizationCost = 0;
if (LT.first > 1) {
Type *LegalVTy = EVT(LT.second).getTypeForEVT(ValTy->getContext());
LegalizationCost = getArithmeticInstrCost(Opcode, LegalVTy, CostKind);
LegalizationCost *= LT.first - 1;
}
int ISD = TLI->InstructionOpcodeToISD(Opcode);
assert(ISD && "Invalid opcode");
// Add the final reduction cost for the legal horizontal reduction
switch (ISD) {
case ISD::ADD:
case ISD::AND:
case ISD::OR:
case ISD::XOR:
case ISD::FADD:
return LegalizationCost + 2;
default:
return InstructionCost::getInvalid();
}
}
InstructionCost
AArch64TTIImpl::getArithmeticReductionCost(unsigned Opcode, VectorType *ValTy,
std::optional<FastMathFlags> FMF,
TTI::TargetCostKind CostKind) const {
// The code-generator is currently not able to handle scalable vectors
// of <vscale x 1 x eltty> yet, so return an invalid cost to avoid selecting
// it. This change will be removed when code-generation for these types is
// sufficiently reliable.
if (auto *VTy = dyn_cast<ScalableVectorType>(ValTy))
if (VTy->getElementCount() == ElementCount::getScalable(1))
return InstructionCost::getInvalid();
if (TTI::requiresOrderedReduction(FMF)) {
if (auto *FixedVTy = dyn_cast<FixedVectorType>(ValTy)) {
InstructionCost BaseCost =
BaseT::getArithmeticReductionCost(Opcode, ValTy, FMF, CostKind);
// Add on extra cost to reflect the extra overhead on some CPUs. We still
// end up vectorizing for more computationally intensive loops.
return BaseCost + FixedVTy->getNumElements();
}
if (Opcode != Instruction::FAdd)
return InstructionCost::getInvalid();
auto *VTy = cast<ScalableVectorType>(ValTy);
InstructionCost Cost =
getArithmeticInstrCost(Opcode, VTy->getScalarType(), CostKind);
Cost *= getMaxNumElements(VTy->getElementCount());
return Cost;
}
if (isa<ScalableVectorType>(ValTy))
return getArithmeticReductionCostSVE(Opcode, ValTy, CostKind);
std::pair<InstructionCost, MVT> LT = getTypeLegalizationCost(ValTy);
MVT MTy = LT.second;
int ISD = TLI->InstructionOpcodeToISD(Opcode);
assert(ISD && "Invalid opcode");
// Horizontal adds can use the 'addv' instruction. We model the cost of these
// instructions as twice a normal vector add, plus 1 for each legalization
// step (LT.first). This is the only arithmetic vector reduction operation for
// which we have an instruction.
// OR, XOR and AND costs should match the codegen from:
// OR: llvm/test/CodeGen/AArch64/reduce-or.ll
// XOR: llvm/test/CodeGen/AArch64/reduce-xor.ll
// AND: llvm/test/CodeGen/AArch64/reduce-and.ll
static const CostTblEntry CostTblNoPairwise[]{
{ISD::ADD, MVT::v8i8, 2},
{ISD::ADD, MVT::v16i8, 2},
{ISD::ADD, MVT::v4i16, 2},
{ISD::ADD, MVT::v8i16, 2},
{ISD::ADD, MVT::v2i32, 2},
{ISD::ADD, MVT::v4i32, 2},
{ISD::ADD, MVT::v2i64, 2},
{ISD::OR, MVT::v8i8, 5}, // fmov + orr_lsr + orr_lsr + lsr + orr
{ISD::OR, MVT::v16i8, 7}, // ext + orr + same as v8i8
{ISD::OR, MVT::v4i16, 4}, // fmov + orr_lsr + lsr + orr
{ISD::OR, MVT::v8i16, 6}, // ext + orr + same as v4i16
{ISD::OR, MVT::v2i32, 3}, // fmov + lsr + orr
{ISD::OR, MVT::v4i32, 5}, // ext + orr + same as v2i32
{ISD::OR, MVT::v2i64, 3}, // ext + orr + fmov
{ISD::XOR, MVT::v8i8, 5}, // Same as above for or...
{ISD::XOR, MVT::v16i8, 7},
{ISD::XOR, MVT::v4i16, 4},
{ISD::XOR, MVT::v8i16, 6},
{ISD::XOR, MVT::v2i32, 3},
{ISD::XOR, MVT::v4i32, 5},
{ISD::XOR, MVT::v2i64, 3},
{ISD::AND, MVT::v8i8, 5}, // Same as above for or...
{ISD::AND, MVT::v16i8, 7},
{ISD::AND, MVT::v4i16, 4},
{ISD::AND, MVT::v8i16, 6},
{ISD::AND, MVT::v2i32, 3},
{ISD::AND, MVT::v4i32, 5},
{ISD::AND, MVT::v2i64, 3},
};
switch (ISD) {
default:
break;
case ISD::FADD:
if (Type *EltTy = ValTy->getScalarType();
// FIXME: For half types without fullfp16 support, this could extend and
// use a fp32 faddp reduction but current codegen unrolls.
MTy.isVector() && (EltTy->isFloatTy() || EltTy->isDoubleTy() ||
(EltTy->isHalfTy() && ST->hasFullFP16()))) {
const unsigned NElts = MTy.getVectorNumElements();
if (ValTy->getElementCount().getFixedValue() >= 2 && NElts >= 2 &&
isPowerOf2_32(NElts))
// Reduction corresponding to series of fadd instructions is lowered to
// series of faddp instructions. faddp has latency/throughput that
// matches fadd instruction and hence, every faddp instruction can be
// considered to have a relative cost = 1 with
// CostKind = TCK_RecipThroughput.
// An faddp will pairwise add vector elements, so the size of input
// vector reduces by half every time, requiring
// #(faddp instructions) = log2_32(NElts).
return (LT.first - 1) + /*No of faddp instructions*/ Log2_32(NElts);
}
break;
case ISD::ADD:
if (const auto *Entry = CostTableLookup(CostTblNoPairwise, ISD, MTy))
return (LT.first - 1) + Entry->Cost;
break;
case ISD::XOR:
case ISD::AND:
case ISD::OR:
const auto *Entry = CostTableLookup(CostTblNoPairwise, ISD, MTy);
if (!Entry)
break;
auto *ValVTy = cast<FixedVectorType>(ValTy);
if (MTy.getVectorNumElements() <= ValVTy->getNumElements() &&
isPowerOf2_32(ValVTy->getNumElements())) {
InstructionCost ExtraCost = 0;
if (LT.first != 1) {
// Type needs to be split, so there is an extra cost of LT.first - 1
// arithmetic ops.
auto *Ty = FixedVectorType::get(ValTy->getElementType(),
MTy.getVectorNumElements());
ExtraCost = getArithmeticInstrCost(Opcode, Ty, CostKind);
ExtraCost *= LT.first - 1;
}
// All and/or/xor of i1 will be lowered with maxv/minv/addv + fmov
auto Cost = ValVTy->getElementType()->isIntegerTy(1) ? 2 : Entry->Cost;
return Cost + ExtraCost;
}
break;
}
return BaseT::getArithmeticReductionCost(Opcode, ValTy, FMF, CostKind);
}
InstructionCost AArch64TTIImpl::getExtendedReductionCost(
unsigned Opcode, bool IsUnsigned, Type *ResTy, VectorType *VecTy,
std::optional<FastMathFlags> FMF, TTI::TargetCostKind CostKind) const {
EVT VecVT = TLI->getValueType(DL, VecTy);
EVT ResVT = TLI->getValueType(DL, ResTy);
if (Opcode == Instruction::Add && VecVT.isSimple() && ResVT.isSimple() &&
VecVT.getSizeInBits() >= 64) {
std::pair<InstructionCost, MVT> LT = getTypeLegalizationCost(VecTy);
// The legal cases are:
// UADDLV 8/16/32->32
// UADDLP 32->64
unsigned RevVTSize = ResVT.getSizeInBits();
if (((LT.second == MVT::v8i8 || LT.second == MVT::v16i8) &&
RevVTSize <= 32) ||
((LT.second == MVT::v4i16 || LT.second == MVT::v8i16) &&
RevVTSize <= 32) ||
((LT.second == MVT::v2i32 || LT.second == MVT::v4i32) &&
RevVTSize <= 64))
return (LT.first - 1) * 2 + 2;
}
return BaseT::getExtendedReductionCost(Opcode, IsUnsigned, ResTy, VecTy, FMF,
CostKind);
}
InstructionCost
AArch64TTIImpl::getMulAccReductionCost(bool IsUnsigned, unsigned RedOpcode,
Type *ResTy, VectorType *VecTy,
TTI::TargetCostKind CostKind) const {
EVT VecVT = TLI->getValueType(DL, VecTy);
EVT ResVT = TLI->getValueType(DL, ResTy);
if (ST->hasDotProd() && VecVT.isSimple() && ResVT.isSimple() &&
RedOpcode == Instruction::Add) {
std::pair<InstructionCost, MVT> LT = getTypeLegalizationCost(VecTy);
// The legal cases with dotprod are
// UDOT 8->32
// Which requires an additional uaddv to sum the i32 values.
if ((LT.second == MVT::v8i8 || LT.second == MVT::v16i8) &&
ResVT == MVT::i32)
return LT.first + 2;
}
return BaseT::getMulAccReductionCost(IsUnsigned, RedOpcode, ResTy, VecTy,
CostKind);
}
InstructionCost
AArch64TTIImpl::getSpliceCost(VectorType *Tp, int Index,
TTI::TargetCostKind CostKind) const {
static const CostTblEntry ShuffleTbl[] = {
{ TTI::SK_Splice, MVT::nxv16i8, 1 },
{ TTI::SK_Splice, MVT::nxv8i16, 1 },
{ TTI::SK_Splice, MVT::nxv4i32, 1 },
{ TTI::SK_Splice, MVT::nxv2i64, 1 },
{ TTI::SK_Splice, MVT::nxv2f16, 1 },
{ TTI::SK_Splice, MVT::nxv4f16, 1 },
{ TTI::SK_Splice, MVT::nxv8f16, 1 },
{ TTI::SK_Splice, MVT::nxv2bf16, 1 },
{ TTI::SK_Splice, MVT::nxv4bf16, 1 },
{ TTI::SK_Splice, MVT::nxv8bf16, 1 },
{ TTI::SK_Splice, MVT::nxv2f32, 1 },
{ TTI::SK_Splice, MVT::nxv4f32, 1 },
{ TTI::SK_Splice, MVT::nxv2f64, 1 },
};
// The code-generator is currently not able to handle scalable vectors
// of <vscale x 1 x eltty> yet, so return an invalid cost to avoid selecting
// it. This change will be removed when code-generation for these types is
// sufficiently reliable.
if (Tp->getElementCount() == ElementCount::getScalable(1))
return InstructionCost::getInvalid();
std::pair<InstructionCost, MVT> LT = getTypeLegalizationCost(Tp);
Type *LegalVTy = EVT(LT.second).getTypeForEVT(Tp->getContext());
EVT PromotedVT = LT.second.getScalarType() == MVT::i1
? TLI->getPromotedVTForPredicate(EVT(LT.second))
: LT.second;
Type *PromotedVTy = EVT(PromotedVT).getTypeForEVT(Tp->getContext());
InstructionCost LegalizationCost = 0;
if (Index < 0) {
LegalizationCost =
getCmpSelInstrCost(Instruction::ICmp, PromotedVTy, PromotedVTy,
CmpInst::BAD_ICMP_PREDICATE, CostKind) +
getCmpSelInstrCost(Instruction::Select, PromotedVTy, LegalVTy,
CmpInst::BAD_ICMP_PREDICATE, CostKind);
}
// Predicated splice are promoted when lowering. See AArch64ISelLowering.cpp
// Cost performed on a promoted type.
if (LT.second.getScalarType() == MVT::i1) {
LegalizationCost +=
getCastInstrCost(Instruction::ZExt, PromotedVTy, LegalVTy,
TTI::CastContextHint::None, CostKind) +
getCastInstrCost(Instruction::Trunc, LegalVTy, PromotedVTy,
TTI::CastContextHint::None, CostKind);
}
const auto *Entry =
CostTableLookup(ShuffleTbl, TTI::SK_Splice, PromotedVT.getSimpleVT());
assert(Entry && "Illegal Type for Splice");
LegalizationCost += Entry->Cost;
return LegalizationCost * LT.first;
}
InstructionCost AArch64TTIImpl::getPartialReductionCost(
unsigned Opcode, Type *InputTypeA, Type *InputTypeB, Type *AccumType,
ElementCount VF, TTI::PartialReductionExtendKind OpAExtend,
TTI::PartialReductionExtendKind OpBExtend, std::optional<unsigned> BinOp,
TTI::TargetCostKind CostKind) const {
InstructionCost Invalid = InstructionCost::getInvalid();
InstructionCost Cost(TTI::TCC_Basic);
if (CostKind != TTI::TCK_RecipThroughput)
return Invalid;
// Sub opcodes currently only occur in chained cases.
// Independent partial reduction subtractions are still costed as an add
if ((Opcode != Instruction::Add && Opcode != Instruction::Sub) ||
OpAExtend == TTI::PR_None)
return Invalid;
// We only support multiply binary operations for now, and for muls we
// require the types being extended to be the same.
// NOTE: For muls AArch64 supports lowering mixed extensions to a usdot but
// only if the i8mm or sve/streaming features are available.
if (BinOp && (*BinOp != Instruction::Mul || InputTypeA != InputTypeB ||
OpBExtend == TTI::PR_None ||
(OpAExtend != OpBExtend && !ST->hasMatMulInt8() &&
!ST->isSVEorStreamingSVEAvailable())))
return Invalid;
assert((BinOp || (OpBExtend == TTI::PR_None && !InputTypeB)) &&
"Unexpected values for OpBExtend or InputTypeB");
EVT InputEVT = EVT::getEVT(InputTypeA);
EVT AccumEVT = EVT::getEVT(AccumType);
unsigned VFMinValue = VF.getKnownMinValue();
if (VF.isScalable()) {
if (!ST->isSVEorStreamingSVEAvailable())
return Invalid;
// Don't accept a partial reduction if the scaled accumulator is vscale x 1,
// since we can't lower that type.
unsigned Scale =
AccumEVT.getScalarSizeInBits() / InputEVT.getScalarSizeInBits();
if (VFMinValue == Scale)
return Invalid;
}
if (VF.isFixed() &&
(!ST->isNeonAvailable() || !ST->hasDotProd() || AccumEVT == MVT::i64))
return Invalid;
if (InputEVT == MVT::i8) {
switch (VFMinValue) {
default:
return Invalid;
case 8:
if (AccumEVT == MVT::i32)
Cost *= 2;
else if (AccumEVT != MVT::i64)
return Invalid;
break;
case 16:
if (AccumEVT == MVT::i64)
Cost *= 2;
else if (AccumEVT != MVT::i32)
return Invalid;
break;
}
} else if (InputEVT == MVT::i16) {
// FIXME: Allow i32 accumulator but increase cost, as we would extend
// it to i64.
if (VFMinValue != 8 || AccumEVT != MVT::i64)
return Invalid;
} else
return Invalid;
return Cost;
}
InstructionCost
AArch64TTIImpl::getShuffleCost(TTI::ShuffleKind Kind, VectorType *DstTy,
VectorType *SrcTy, ArrayRef<int> Mask,
TTI::TargetCostKind CostKind, int Index,
VectorType *SubTp, ArrayRef<const Value *> Args,
const Instruction *CxtI) const {
assert((Mask.empty() || DstTy->isScalableTy() ||
Mask.size() == DstTy->getElementCount().getKnownMinValue()) &&
"Expected the Mask to match the return size if given");
assert(SrcTy->getScalarType() == DstTy->getScalarType() &&
"Expected the same scalar types");
std::pair<InstructionCost, MVT> LT = getTypeLegalizationCost(SrcTy);
// If we have a Mask, and the LT is being legalized somehow, split the Mask
// into smaller vectors and sum the cost of each shuffle.
if (!Mask.empty() && isa<FixedVectorType>(SrcTy) && LT.second.isVector() &&
LT.second.getScalarSizeInBits() * Mask.size() > 128 &&
SrcTy->getScalarSizeInBits() == LT.second.getScalarSizeInBits() &&
Mask.size() > LT.second.getVectorNumElements() && !Index && !SubTp) {
// Check for LD3/LD4 instructions, which are represented in llvm IR as
// deinterleaving-shuffle(load). The shuffle cost could potentially be free,
// but we model it with a cost of LT.first so that LD3/LD4 have a higher
// cost than just the load.
if (Args.size() >= 1 && isa<LoadInst>(Args[0]) &&
(ShuffleVectorInst::isDeInterleaveMaskOfFactor(Mask, 3) ||
ShuffleVectorInst::isDeInterleaveMaskOfFactor(Mask, 4)))
return std::max<InstructionCost>(1, LT.first / 4);
// Check for ST3/ST4 instructions, which are represented in llvm IR as
// store(interleaving-shuffle). The shuffle cost could potentially be free,
// but we model it with a cost of LT.first so that ST3/ST4 have a higher
// cost than just the store.
if (CxtI && CxtI->hasOneUse() && isa<StoreInst>(*CxtI->user_begin()) &&
(ShuffleVectorInst::isInterleaveMask(
Mask, 4, SrcTy->getElementCount().getKnownMinValue() * 2) ||
ShuffleVectorInst::isInterleaveMask(
Mask, 3, SrcTy->getElementCount().getKnownMinValue() * 2)))
return LT.first;
unsigned TpNumElts = Mask.size();
unsigned LTNumElts = LT.second.getVectorNumElements();
unsigned NumVecs = (TpNumElts + LTNumElts - 1) / LTNumElts;
VectorType *NTp = VectorType::get(SrcTy->getScalarType(),
LT.second.getVectorElementCount());
InstructionCost Cost;
std::map<std::tuple<unsigned, unsigned, SmallVector<int>>, InstructionCost>
PreviousCosts;
for (unsigned N = 0; N < NumVecs; N++) {
SmallVector<int> NMask;
// Split the existing mask into chunks of size LTNumElts. Track the source
// sub-vectors to ensure the result has at most 2 inputs.
unsigned Source1 = -1U, Source2 = -1U;
unsigned NumSources = 0;
for (unsigned E = 0; E < LTNumElts; E++) {
int MaskElt = (N * LTNumElts + E < TpNumElts) ? Mask[N * LTNumElts + E]
: PoisonMaskElem;
if (MaskElt < 0) {
NMask.push_back(PoisonMaskElem);
continue;
}
// Calculate which source from the input this comes from and whether it
// is new to us.
unsigned Source = MaskElt / LTNumElts;
if (NumSources == 0) {
Source1 = Source;
NumSources = 1;
} else if (NumSources == 1 && Source != Source1) {
Source2 = Source;
NumSources = 2;
} else if (NumSources >= 2 && Source != Source1 && Source != Source2) {
NumSources++;
}
// Add to the new mask. For the NumSources>2 case these are not correct,
// but are only used for the modular lane number.
if (Source == Source1)
NMask.push_back(MaskElt % LTNumElts);
else if (Source == Source2)
NMask.push_back(MaskElt % LTNumElts + LTNumElts);
else
NMask.push_back(MaskElt % LTNumElts);
}
// Check if we have already generated this sub-shuffle, which means we
// will have already generated the output. For example a <16 x i32> splat
// will be the same sub-splat 4 times, which only needs to be generated
// once and reused.
auto Result =
PreviousCosts.insert({std::make_tuple(Source1, Source2, NMask), 0});
// Check if it was already in the map (already costed).
if (!Result.second)
continue;
// If the sub-mask has at most 2 input sub-vectors then re-cost it using
// getShuffleCost. If not then cost it using the worst case as the number
// of element moves into a new vector.
InstructionCost NCost =
NumSources <= 2
? getShuffleCost(NumSources <= 1 ? TTI::SK_PermuteSingleSrc
: TTI::SK_PermuteTwoSrc,
NTp, NTp, NMask, CostKind, 0, nullptr, Args,
CxtI)
: LTNumElts;
Result.first->second = NCost;
Cost += NCost;
}
return Cost;
}
Kind = improveShuffleKindFromMask(Kind, Mask, SrcTy, Index, SubTp);
bool IsExtractSubvector = Kind == TTI::SK_ExtractSubvector;
// A subvector extract can be implemented with a NEON/SVE ext (or trivial
// extract, if from lane 0) for 128-bit NEON vectors or legal SVE vectors.
// This currently only handles low or high extracts to prevent SLP vectorizer
// regressions.
// Note that SVE's ext instruction is destructive, but it can be fused with
// a movprfx to act like a constructive instruction.
if (IsExtractSubvector && LT.second.isFixedLengthVector()) {
if (LT.second.getFixedSizeInBits() >= 128 &&
cast<FixedVectorType>(SubTp)->getNumElements() ==
LT.second.getVectorNumElements() / 2) {
if (Index == 0)
return 0;
if (Index == (int)LT.second.getVectorNumElements() / 2)
return 1;
}
Kind = TTI::SK_PermuteSingleSrc;
}
// FIXME: This was added to keep the costs equal when adding DstTys. Update
// the code to handle length-changing shuffles.
if (Kind == TTI::SK_InsertSubvector) {
LT = getTypeLegalizationCost(DstTy);
SrcTy = DstTy;
}
// Segmented shuffle matching.
if (Kind == TTI::SK_PermuteSingleSrc && isa<FixedVectorType>(SrcTy) &&
!Mask.empty() && SrcTy->getPrimitiveSizeInBits().isNonZero() &&
SrcTy->getPrimitiveSizeInBits().isKnownMultipleOf(
AArch64::SVEBitsPerBlock)) {
FixedVectorType *VTy = cast<FixedVectorType>(SrcTy);
unsigned Segments =
VTy->getPrimitiveSizeInBits() / AArch64::SVEBitsPerBlock;
unsigned SegmentElts = VTy->getNumElements() / Segments;
// dupq zd.t, zn.t[idx]
if ((ST->hasSVE2p1() || ST->hasSME2p1()) &&
ST->isSVEorStreamingSVEAvailable() &&
isDUPQMask(Mask, Segments, SegmentElts))
return LT.first;
// mov zd.q, vn
if (ST->isSVEorStreamingSVEAvailable() &&
isDUPFirstSegmentMask(Mask, Segments, SegmentElts))
return LT.first;
}
// Check for broadcast loads, which are supported by the LD1R instruction.
// In terms of code-size, the shuffle vector is free when a load + dup get
// folded into a LD1R. That's what we check and return here. For performance
// and reciprocal throughput, a LD1R is not completely free. In this case, we
// return the cost for the broadcast below (i.e. 1 for most/all types), so
// that we model the load + dup sequence slightly higher because LD1R is a
// high latency instruction.
if (CostKind == TTI::TCK_CodeSize && Kind == TTI::SK_Broadcast) {
bool IsLoad = !Args.empty() && isa<LoadInst>(Args[0]);
if (IsLoad && LT.second.isVector() &&
isLegalBroadcastLoad(SrcTy->getElementType(),
LT.second.getVectorElementCount()))
return 0;
}
// If we have 4 elements for the shuffle and a Mask, get the cost straight
// from the perfect shuffle tables.
if (Mask.size() == 4 &&
SrcTy->getElementCount() == ElementCount::getFixed(4) &&
(SrcTy->getScalarSizeInBits() == 16 ||
SrcTy->getScalarSizeInBits() == 32) &&
all_of(Mask, [](int E) { return E < 8; }))
return getPerfectShuffleCost(Mask);
// Check for identity masks, which we can treat as free.
if (!Mask.empty() && LT.second.isFixedLengthVector() &&
(Kind == TTI::SK_PermuteTwoSrc || Kind == TTI::SK_PermuteSingleSrc) &&
all_of(enumerate(Mask), [](const auto &M) {
return M.value() < 0 || M.value() == (int)M.index();
}))
return 0;
// Check for other shuffles that are not SK_ kinds but we have native
// instructions for, for example ZIP and UZP.
unsigned Unused;
if (LT.second.isFixedLengthVector() &&
LT.second.getVectorNumElements() == Mask.size() &&
(Kind == TTI::SK_PermuteTwoSrc || Kind == TTI::SK_PermuteSingleSrc) &&
(isZIPMask(Mask, LT.second.getVectorNumElements(), Unused) ||
isUZPMask(Mask, LT.second.getVectorNumElements(), Unused) ||
isREVMask(Mask, LT.second.getScalarSizeInBits(),
LT.second.getVectorNumElements(), 16) ||
isREVMask(Mask, LT.second.getScalarSizeInBits(),
LT.second.getVectorNumElements(), 32) ||
isREVMask(Mask, LT.second.getScalarSizeInBits(),
LT.second.getVectorNumElements(), 64) ||
// Check for non-zero lane splats
all_of(drop_begin(Mask),
[&Mask](int M) { return M < 0 || M == Mask[0]; })))
return 1;
if (Kind == TTI::SK_Broadcast || Kind == TTI::SK_Transpose ||
Kind == TTI::SK_Select || Kind == TTI::SK_PermuteSingleSrc ||
Kind == TTI::SK_Reverse || Kind == TTI::SK_Splice) {
static const CostTblEntry ShuffleTbl[] = {
// Broadcast shuffle kinds can be performed with 'dup'.
{TTI::SK_Broadcast, MVT::v8i8, 1},
{TTI::SK_Broadcast, MVT::v16i8, 1},
{TTI::SK_Broadcast, MVT::v4i16, 1},
{TTI::SK_Broadcast, MVT::v8i16, 1},
{TTI::SK_Broadcast, MVT::v2i32, 1},
{TTI::SK_Broadcast, MVT::v4i32, 1},
{TTI::SK_Broadcast, MVT::v2i64, 1},
{TTI::SK_Broadcast, MVT::v4f16, 1},
{TTI::SK_Broadcast, MVT::v8f16, 1},
{TTI::SK_Broadcast, MVT::v4bf16, 1},
{TTI::SK_Broadcast, MVT::v8bf16, 1},
{TTI::SK_Broadcast, MVT::v2f32, 1},
{TTI::SK_Broadcast, MVT::v4f32, 1},
{TTI::SK_Broadcast, MVT::v2f64, 1},
// Transpose shuffle kinds can be performed with 'trn1/trn2' and
// 'zip1/zip2' instructions.
{TTI::SK_Transpose, MVT::v8i8, 1},
{TTI::SK_Transpose, MVT::v16i8, 1},
{TTI::SK_Transpose, MVT::v4i16, 1},
{TTI::SK_Transpose, MVT::v8i16, 1},
{TTI::SK_Transpose, MVT::v2i32, 1},
{TTI::SK_Transpose, MVT::v4i32, 1},
{TTI::SK_Transpose, MVT::v2i64, 1},
{TTI::SK_Transpose, MVT::v4f16, 1},
{TTI::SK_Transpose, MVT::v8f16, 1},
{TTI::SK_Transpose, MVT::v4bf16, 1},
{TTI::SK_Transpose, MVT::v8bf16, 1},
{TTI::SK_Transpose, MVT::v2f32, 1},
{TTI::SK_Transpose, MVT::v4f32, 1},
{TTI::SK_Transpose, MVT::v2f64, 1},
// Select shuffle kinds.
// TODO: handle vXi8/vXi16.
{TTI::SK_Select, MVT::v2i32, 1}, // mov.
{TTI::SK_Select, MVT::v4i32, 2}, // rev+trn (or similar).
{TTI::SK_Select, MVT::v2i64, 1}, // mov.
{TTI::SK_Select, MVT::v2f32, 1}, // mov.
{TTI::SK_Select, MVT::v4f32, 2}, // rev+trn (or similar).
{TTI::SK_Select, MVT::v2f64, 1}, // mov.
// PermuteSingleSrc shuffle kinds.
{TTI::SK_PermuteSingleSrc, MVT::v2i32, 1}, // mov.
{TTI::SK_PermuteSingleSrc, MVT::v4i32, 3}, // perfectshuffle worst case.
{TTI::SK_PermuteSingleSrc, MVT::v2i64, 1}, // mov.
{TTI::SK_PermuteSingleSrc, MVT::v2f32, 1}, // mov.
{TTI::SK_PermuteSingleSrc, MVT::v4f32, 3}, // perfectshuffle worst case.
{TTI::SK_PermuteSingleSrc, MVT::v2f64, 1}, // mov.
{TTI::SK_PermuteSingleSrc, MVT::v4i16, 3}, // perfectshuffle worst case.
{TTI::SK_PermuteSingleSrc, MVT::v4f16, 3}, // perfectshuffle worst case.
{TTI::SK_PermuteSingleSrc, MVT::v4bf16, 3}, // same
{TTI::SK_PermuteSingleSrc, MVT::v8i16, 8}, // constpool + load + tbl
{TTI::SK_PermuteSingleSrc, MVT::v8f16, 8}, // constpool + load + tbl
{TTI::SK_PermuteSingleSrc, MVT::v8bf16, 8}, // constpool + load + tbl
{TTI::SK_PermuteSingleSrc, MVT::v8i8, 8}, // constpool + load + tbl
{TTI::SK_PermuteSingleSrc, MVT::v16i8, 8}, // constpool + load + tbl
// Reverse can be lowered with `rev`.
{TTI::SK_Reverse, MVT::v2i32, 1}, // REV64
{TTI::SK_Reverse, MVT::v4i32, 2}, // REV64; EXT
{TTI::SK_Reverse, MVT::v2i64, 1}, // EXT
{TTI::SK_Reverse, MVT::v2f32, 1}, // REV64
{TTI::SK_Reverse, MVT::v4f32, 2}, // REV64; EXT
{TTI::SK_Reverse, MVT::v2f64, 1}, // EXT
{TTI::SK_Reverse, MVT::v8f16, 2}, // REV64; EXT
{TTI::SK_Reverse, MVT::v8bf16, 2}, // REV64; EXT
{TTI::SK_Reverse, MVT::v8i16, 2}, // REV64; EXT
{TTI::SK_Reverse, MVT::v16i8, 2}, // REV64; EXT
{TTI::SK_Reverse, MVT::v4f16, 1}, // REV64
{TTI::SK_Reverse, MVT::v4bf16, 1}, // REV64
{TTI::SK_Reverse, MVT::v4i16, 1}, // REV64
{TTI::SK_Reverse, MVT::v8i8, 1}, // REV64
// Splice can all be lowered as `ext`.
{TTI::SK_Splice, MVT::v2i32, 1},
{TTI::SK_Splice, MVT::v4i32, 1},
{TTI::SK_Splice, MVT::v2i64, 1},
{TTI::SK_Splice, MVT::v2f32, 1},
{TTI::SK_Splice, MVT::v4f32, 1},
{TTI::SK_Splice, MVT::v2f64, 1},
{TTI::SK_Splice, MVT::v8f16, 1},
{TTI::SK_Splice, MVT::v8bf16, 1},
{TTI::SK_Splice, MVT::v8i16, 1},
{TTI::SK_Splice, MVT::v16i8, 1},
{TTI::SK_Splice, MVT::v4f16, 1},
{TTI::SK_Splice, MVT::v4bf16, 1},
{TTI::SK_Splice, MVT::v4i16, 1},
{TTI::SK_Splice, MVT::v8i8, 1},
// Broadcast shuffle kinds for scalable vectors
{TTI::SK_Broadcast, MVT::nxv16i8, 1},
{TTI::SK_Broadcast, MVT::nxv8i16, 1},
{TTI::SK_Broadcast, MVT::nxv4i32, 1},
{TTI::SK_Broadcast, MVT::nxv2i64, 1},
{TTI::SK_Broadcast, MVT::nxv2f16, 1},
{TTI::SK_Broadcast, MVT::nxv4f16, 1},
{TTI::SK_Broadcast, MVT::nxv8f16, 1},
{TTI::SK_Broadcast, MVT::nxv2bf16, 1},
{TTI::SK_Broadcast, MVT::nxv4bf16, 1},
{TTI::SK_Broadcast, MVT::nxv8bf16, 1},
{TTI::SK_Broadcast, MVT::nxv2f32, 1},
{TTI::SK_Broadcast, MVT::nxv4f32, 1},
{TTI::SK_Broadcast, MVT::nxv2f64, 1},
{TTI::SK_Broadcast, MVT::nxv16i1, 1},
{TTI::SK_Broadcast, MVT::nxv8i1, 1},
{TTI::SK_Broadcast, MVT::nxv4i1, 1},
{TTI::SK_Broadcast, MVT::nxv2i1, 1},
// Handle the cases for vector.reverse with scalable vectors
{TTI::SK_Reverse, MVT::nxv16i8, 1},
{TTI::SK_Reverse, MVT::nxv8i16, 1},
{TTI::SK_Reverse, MVT::nxv4i32, 1},
{TTI::SK_Reverse, MVT::nxv2i64, 1},
{TTI::SK_Reverse, MVT::nxv2f16, 1},
{TTI::SK_Reverse, MVT::nxv4f16, 1},
{TTI::SK_Reverse, MVT::nxv8f16, 1},
{TTI::SK_Reverse, MVT::nxv2bf16, 1},
{TTI::SK_Reverse, MVT::nxv4bf16, 1},
{TTI::SK_Reverse, MVT::nxv8bf16, 1},
{TTI::SK_Reverse, MVT::nxv2f32, 1},
{TTI::SK_Reverse, MVT::nxv4f32, 1},
{TTI::SK_Reverse, MVT::nxv2f64, 1},
{TTI::SK_Reverse, MVT::nxv16i1, 1},
{TTI::SK_Reverse, MVT::nxv8i1, 1},
{TTI::SK_Reverse, MVT::nxv4i1, 1},
{TTI::SK_Reverse, MVT::nxv2i1, 1},
};
if (const auto *Entry = CostTableLookup(ShuffleTbl, Kind, LT.second))
return LT.first * Entry->Cost;
}
if (Kind == TTI::SK_Splice && isa<ScalableVectorType>(SrcTy))
return getSpliceCost(SrcTy, Index, CostKind);
// Inserting a subvector can often be done with either a D, S or H register
// move, so long as the inserted vector is "aligned".
if (Kind == TTI::SK_InsertSubvector && LT.second.isFixedLengthVector() &&
LT.second.getSizeInBits() <= 128 && SubTp) {
std::pair<InstructionCost, MVT> SubLT = getTypeLegalizationCost(SubTp);
if (SubLT.second.isVector()) {
int NumElts = LT.second.getVectorNumElements();
int NumSubElts = SubLT.second.getVectorNumElements();
if ((Index % NumSubElts) == 0 && (NumElts % NumSubElts) == 0)
return SubLT.first;
}
}
// Restore optimal kind.
if (IsExtractSubvector)
Kind = TTI::SK_ExtractSubvector;
return BaseT::getShuffleCost(Kind, DstTy, SrcTy, Mask, CostKind, Index, SubTp,
Args, CxtI);
}
static bool containsDecreasingPointers(Loop *TheLoop,
PredicatedScalarEvolution *PSE) {
const auto &Strides = DenseMap<Value *, const SCEV *>();
for (BasicBlock *BB : TheLoop->blocks()) {
// Scan the instructions in the block and look for addresses that are
// consecutive and decreasing.
for (Instruction &I : *BB) {
if (isa<LoadInst>(&I) || isa<StoreInst>(&I)) {
Value *Ptr = getLoadStorePointerOperand(&I);
Type *AccessTy = getLoadStoreType(&I);
if (getPtrStride(*PSE, AccessTy, Ptr, TheLoop, Strides, /*Assume=*/true,
/*ShouldCheckWrap=*/false)
.value_or(0) < 0)
return true;
}
}
}
return false;
}
bool AArch64TTIImpl::preferFixedOverScalableIfEqualCost(bool IsEpilogue) const {
if (SVEPreferFixedOverScalableIfEqualCost.getNumOccurrences())
return SVEPreferFixedOverScalableIfEqualCost;
// For cases like post-LTO vectorization, when we eventually know the trip
// count, epilogue with fixed-width vectorization can be deleted if the trip
// count is less than the epilogue iterations. That's why we prefer
// fixed-width vectorization in epilogue in case of equal costs.
if (IsEpilogue)
return true;
return ST->useFixedOverScalableIfEqualCost();
}
unsigned AArch64TTIImpl::getEpilogueVectorizationMinVF() const {
return ST->getEpilogueVectorizationMinVF();
}
bool AArch64TTIImpl::preferPredicateOverEpilogue(TailFoldingInfo *TFI) const {
if (!ST->hasSVE())
return false;
// We don't currently support vectorisation with interleaving for SVE - with
// such loops we're better off not using tail-folding. This gives us a chance
// to fall back on fixed-width vectorisation using NEON's ld2/st2/etc.
if (TFI->IAI->hasGroups())
return false;
TailFoldingOpts Required = TailFoldingOpts::Disabled;
if (TFI->LVL->getReductionVars().size())
Required |= TailFoldingOpts::Reductions;
if (TFI->LVL->getFixedOrderRecurrences().size())
Required |= TailFoldingOpts::Recurrences;
// We call this to discover whether any load/store pointers in the loop have
// negative strides. This will require extra work to reverse the loop
// predicate, which may be expensive.
if (containsDecreasingPointers(TFI->LVL->getLoop(),
TFI->LVL->getPredicatedScalarEvolution()))
Required |= TailFoldingOpts::Reverse;
if (Required == TailFoldingOpts::Disabled)
Required |= TailFoldingOpts::Simple;
if (!TailFoldingOptionLoc.satisfies(ST->getSVETailFoldingDefaultOpts(),
Required))
return false;
// Don't tail-fold for tight loops where we would be better off interleaving
// with an unpredicated loop.
unsigned NumInsns = 0;
for (BasicBlock *BB : TFI->LVL->getLoop()->blocks()) {
NumInsns += BB->sizeWithoutDebug();
}
// We expect 4 of these to be a IV PHI, IV add, IV compare and branch.
return NumInsns >= SVETailFoldInsnThreshold;
}
InstructionCost
AArch64TTIImpl::getScalingFactorCost(Type *Ty, GlobalValue *BaseGV,
StackOffset BaseOffset, bool HasBaseReg,
int64_t Scale, unsigned AddrSpace) const {
// Scaling factors are not free at all.
// Operands | Rt Latency
// -------------------------------------------
// Rt, [Xn, Xm] | 4
// -------------------------------------------
// Rt, [Xn, Xm, lsl #imm] | Rn: 4 Rm: 5
// Rt, [Xn, Wm, <extend> #imm] |
TargetLoweringBase::AddrMode AM;
AM.BaseGV = BaseGV;
AM.BaseOffs = BaseOffset.getFixed();
AM.HasBaseReg = HasBaseReg;
AM.Scale = Scale;
AM.ScalableOffset = BaseOffset.getScalable();
if (getTLI()->isLegalAddressingMode(DL, AM, Ty, AddrSpace))
// Scale represents reg2 * scale, thus account for 1 if
// it is not equal to 0 or 1.
return AM.Scale != 0 && AM.Scale != 1;
return InstructionCost::getInvalid();
}
bool AArch64TTIImpl::shouldTreatInstructionLikeSelect(
const Instruction *I) const {
if (EnableOrLikeSelectOpt) {
// For the binary operators (e.g. or) we need to be more careful than
// selects, here we only transform them if they are already at a natural
// break point in the code - the end of a block with an unconditional
// terminator.
if (I->getOpcode() == Instruction::Or &&
isa<BranchInst>(I->getNextNode()) &&
cast<BranchInst>(I->getNextNode())->isUnconditional())
return true;
if (I->getOpcode() == Instruction::Add ||
I->getOpcode() == Instruction::Sub)
return true;
}
return BaseT::shouldTreatInstructionLikeSelect(I);
}
bool AArch64TTIImpl::isLSRCostLess(
const TargetTransformInfo::LSRCost &C1,
const TargetTransformInfo::LSRCost &C2) const {
// AArch64 specific here is adding the number of instructions to the
// comparison (though not as the first consideration, as some targets do)
// along with changing the priority of the base additions.
// TODO: Maybe a more nuanced tradeoff between instruction count
// and number of registers? To be investigated at a later date.
if (EnableLSRCostOpt)
return std::tie(C1.NumRegs, C1.Insns, C1.NumBaseAdds, C1.AddRecCost,
C1.NumIVMuls, C1.ScaleCost, C1.ImmCost, C1.SetupCost) <
std::tie(C2.NumRegs, C2.Insns, C2.NumBaseAdds, C2.AddRecCost,
C2.NumIVMuls, C2.ScaleCost, C2.ImmCost, C2.SetupCost);
return TargetTransformInfoImplBase::isLSRCostLess(C1, C2);
}
static bool isSplatShuffle(Value *V) {
if (auto *Shuf = dyn_cast<ShuffleVectorInst>(V))
return all_equal(Shuf->getShuffleMask());
return false;
}
/// Check if both Op1 and Op2 are shufflevector extracts of either the lower
/// or upper half of the vector elements.
static bool areExtractShuffleVectors(Value *Op1, Value *Op2,
bool AllowSplat = false) {
// Scalable types can't be extract shuffle vectors.
if (Op1->getType()->isScalableTy() || Op2->getType()->isScalableTy())
return false;
auto areTypesHalfed = [](Value *FullV, Value *HalfV) {
auto *FullTy = FullV->getType();
auto *HalfTy = HalfV->getType();
return FullTy->getPrimitiveSizeInBits().getFixedValue() ==
2 * HalfTy->getPrimitiveSizeInBits().getFixedValue();
};
auto extractHalf = [](Value *FullV, Value *HalfV) {
auto *FullVT = cast<FixedVectorType>(FullV->getType());
auto *HalfVT = cast<FixedVectorType>(HalfV->getType());
return FullVT->getNumElements() == 2 * HalfVT->getNumElements();
};
ArrayRef<int> M1, M2;
Value *S1Op1 = nullptr, *S2Op1 = nullptr;
if (!match(Op1, m_Shuffle(m_Value(S1Op1), m_Undef(), m_Mask(M1))) ||
!match(Op2, m_Shuffle(m_Value(S2Op1), m_Undef(), m_Mask(M2))))
return false;
// If we allow splats, set S1Op1/S2Op1 to nullptr for the relevant arg so that
// it is not checked as an extract below.
if (AllowSplat && isSplatShuffle(Op1))
S1Op1 = nullptr;
if (AllowSplat && isSplatShuffle(Op2))
S2Op1 = nullptr;
// Check that the operands are half as wide as the result and we extract
// half of the elements of the input vectors.
if ((S1Op1 && (!areTypesHalfed(S1Op1, Op1) || !extractHalf(S1Op1, Op1))) ||
(S2Op1 && (!areTypesHalfed(S2Op1, Op2) || !extractHalf(S2Op1, Op2))))
return false;
// Check the mask extracts either the lower or upper half of vector
// elements.
int M1Start = 0;
int M2Start = 0;
int NumElements = cast<FixedVectorType>(Op1->getType())->getNumElements() * 2;
if ((S1Op1 &&
!ShuffleVectorInst::isExtractSubvectorMask(M1, NumElements, M1Start)) ||
(S2Op1 &&
!ShuffleVectorInst::isExtractSubvectorMask(M2, NumElements, M2Start)))
return false;
if ((M1Start != 0 && M1Start != (NumElements / 2)) ||
(M2Start != 0 && M2Start != (NumElements / 2)))
return false;
if (S1Op1 && S2Op1 && M1Start != M2Start)
return false;
return true;
}
/// Check if Ext1 and Ext2 are extends of the same type, doubling the bitwidth
/// of the vector elements.
static bool areExtractExts(Value *Ext1, Value *Ext2) {
auto areExtDoubled = [](Instruction *Ext) {
return Ext->getType()->getScalarSizeInBits() ==
2 * Ext->getOperand(0)->getType()->getScalarSizeInBits();
};
if (!match(Ext1, m_ZExtOrSExt(m_Value())) ||
!match(Ext2, m_ZExtOrSExt(m_Value())) ||
!areExtDoubled(cast<Instruction>(Ext1)) ||
!areExtDoubled(cast<Instruction>(Ext2)))
return false;
return true;
}
/// Check if Op could be used with vmull_high_p64 intrinsic.
static bool isOperandOfVmullHighP64(Value *Op) {
Value *VectorOperand = nullptr;
ConstantInt *ElementIndex = nullptr;
return match(Op, m_ExtractElt(m_Value(VectorOperand),
m_ConstantInt(ElementIndex))) &&
ElementIndex->getValue() == 1 &&
isa<FixedVectorType>(VectorOperand->getType()) &&
cast<FixedVectorType>(VectorOperand->getType())->getNumElements() == 2;
}
/// Check if Op1 and Op2 could be used with vmull_high_p64 intrinsic.
static bool areOperandsOfVmullHighP64(Value *Op1, Value *Op2) {
return isOperandOfVmullHighP64(Op1) && isOperandOfVmullHighP64(Op2);
}
static bool shouldSinkVectorOfPtrs(Value *Ptrs, SmallVectorImpl<Use *> &Ops) {
// Restrict ourselves to the form CodeGenPrepare typically constructs.
auto *GEP = dyn_cast<GetElementPtrInst>(Ptrs);
if (!GEP || GEP->getNumOperands() != 2)
return false;
Value *Base = GEP->getOperand(0);
Value *Offsets = GEP->getOperand(1);
// We only care about scalar_base+vector_offsets.
if (Base->getType()->isVectorTy() || !Offsets->getType()->isVectorTy())
return false;
// Sink extends that would allow us to use 32-bit offset vectors.
if (isa<SExtInst>(Offsets) || isa<ZExtInst>(Offsets)) {
auto *OffsetsInst = cast<Instruction>(Offsets);
if (OffsetsInst->getType()->getScalarSizeInBits() > 32 &&
OffsetsInst->getOperand(0)->getType()->getScalarSizeInBits() <= 32)
Ops.push_back(&GEP->getOperandUse(1));
}
// Sink the GEP.
return true;
}
/// We want to sink following cases:
/// (add|sub|gep) A, ((mul|shl) vscale, imm); (add|sub|gep) A, vscale;
/// (add|sub|gep) A, ((mul|shl) zext(vscale), imm);
static bool shouldSinkVScale(Value *Op, SmallVectorImpl<Use *> &Ops) {
if (match(Op, m_VScale()))
return true;
if (match(Op, m_Shl(m_VScale(), m_ConstantInt())) ||
match(Op, m_Mul(m_VScale(), m_ConstantInt()))) {
Ops.push_back(&cast<Instruction>(Op)->getOperandUse(0));
return true;
}
if (match(Op, m_Shl(m_ZExt(m_VScale()), m_ConstantInt())) ||
match(Op, m_Mul(m_ZExt(m_VScale()), m_ConstantInt()))) {
Value *ZExtOp = cast<Instruction>(Op)->getOperand(0);
Ops.push_back(&cast<Instruction>(ZExtOp)->getOperandUse(0));
Ops.push_back(&cast<Instruction>(Op)->getOperandUse(0));
return true;
}
return false;
}
/// Check if sinking \p I's operands to I's basic block is profitable, because
/// the operands can be folded into a target instruction, e.g.
/// shufflevectors extracts and/or sext/zext can be folded into (u,s)subl(2).
bool AArch64TTIImpl::isProfitableToSinkOperands(
Instruction *I, SmallVectorImpl<Use *> &Ops) const {
if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
switch (II->getIntrinsicID()) {
case Intrinsic::aarch64_neon_smull:
case Intrinsic::aarch64_neon_umull:
if (areExtractShuffleVectors(II->getOperand(0), II->getOperand(1),
/*AllowSplat=*/true)) {
Ops.push_back(&II->getOperandUse(0));
Ops.push_back(&II->getOperandUse(1));
return true;
}
[[fallthrough]];
case Intrinsic::fma:
case Intrinsic::fmuladd:
if (isa<VectorType>(I->getType()) &&
cast<VectorType>(I->getType())->getElementType()->isHalfTy() &&
!ST->hasFullFP16())
return false;
[[fallthrough]];
case Intrinsic::aarch64_neon_sqdmull:
case Intrinsic::aarch64_neon_sqdmulh:
case Intrinsic::aarch64_neon_sqrdmulh:
// Sink splats for index lane variants
if (isSplatShuffle(II->getOperand(0)))
Ops.push_back(&II->getOperandUse(0));
if (isSplatShuffle(II->getOperand(1)))
Ops.push_back(&II->getOperandUse(1));
return !Ops.empty();
case Intrinsic::aarch64_neon_fmlal:
case Intrinsic::aarch64_neon_fmlal2:
case Intrinsic::aarch64_neon_fmlsl:
case Intrinsic::aarch64_neon_fmlsl2:
// Sink splats for index lane variants
if (isSplatShuffle(II->getOperand(1)))
Ops.push_back(&II->getOperandUse(1));
if (isSplatShuffle(II->getOperand(2)))
Ops.push_back(&II->getOperandUse(2));
return !Ops.empty();
case Intrinsic::aarch64_sve_ptest_first:
case Intrinsic::aarch64_sve_ptest_last:
if (auto *IIOp = dyn_cast<IntrinsicInst>(II->getOperand(0)))
if (IIOp->getIntrinsicID() == Intrinsic::aarch64_sve_ptrue)
Ops.push_back(&II->getOperandUse(0));
return !Ops.empty();
case Intrinsic::aarch64_sme_write_horiz:
case Intrinsic::aarch64_sme_write_vert:
case Intrinsic::aarch64_sme_writeq_horiz:
case Intrinsic::aarch64_sme_writeq_vert: {
auto *Idx = dyn_cast<Instruction>(II->getOperand(1));
if (!Idx || Idx->getOpcode() != Instruction::Add)
return false;
Ops.push_back(&II->getOperandUse(1));
return true;
}
case Intrinsic::aarch64_sme_read_horiz:
case Intrinsic::aarch64_sme_read_vert:
case Intrinsic::aarch64_sme_readq_horiz:
case Intrinsic::aarch64_sme_readq_vert:
case Intrinsic::aarch64_sme_ld1b_vert:
case Intrinsic::aarch64_sme_ld1h_vert:
case Intrinsic::aarch64_sme_ld1w_vert:
case Intrinsic::aarch64_sme_ld1d_vert:
case Intrinsic::aarch64_sme_ld1q_vert:
case Intrinsic::aarch64_sme_st1b_vert:
case Intrinsic::aarch64_sme_st1h_vert:
case Intrinsic::aarch64_sme_st1w_vert:
case Intrinsic::aarch64_sme_st1d_vert:
case Intrinsic::aarch64_sme_st1q_vert:
case Intrinsic::aarch64_sme_ld1b_horiz:
case Intrinsic::aarch64_sme_ld1h_horiz:
case Intrinsic::aarch64_sme_ld1w_horiz:
case Intrinsic::aarch64_sme_ld1d_horiz:
case Intrinsic::aarch64_sme_ld1q_horiz:
case Intrinsic::aarch64_sme_st1b_horiz:
case Intrinsic::aarch64_sme_st1h_horiz:
case Intrinsic::aarch64_sme_st1w_horiz:
case Intrinsic::aarch64_sme_st1d_horiz:
case Intrinsic::aarch64_sme_st1q_horiz: {
auto *Idx = dyn_cast<Instruction>(II->getOperand(3));
if (!Idx || Idx->getOpcode() != Instruction::Add)
return false;
Ops.push_back(&II->getOperandUse(3));
return true;
}
case Intrinsic::aarch64_neon_pmull:
if (!areExtractShuffleVectors(II->getOperand(0), II->getOperand(1)))
return false;
Ops.push_back(&II->getOperandUse(0));
Ops.push_back(&II->getOperandUse(1));
return true;
case Intrinsic::aarch64_neon_pmull64:
if (!areOperandsOfVmullHighP64(II->getArgOperand(0),
II->getArgOperand(1)))
return false;
Ops.push_back(&II->getArgOperandUse(0));
Ops.push_back(&II->getArgOperandUse(1));
return true;
case Intrinsic::masked_gather:
if (!shouldSinkVectorOfPtrs(II->getArgOperand(0), Ops))
return false;
Ops.push_back(&II->getArgOperandUse(0));
return true;
case Intrinsic::masked_scatter:
if (!shouldSinkVectorOfPtrs(II->getArgOperand(1), Ops))
return false;
Ops.push_back(&II->getArgOperandUse(1));
return true;
default:
return false;
}
}
auto ShouldSinkCondition = [](Value *Cond,
SmallVectorImpl<Use *> &Ops) -> bool {
if (!isa<IntrinsicInst>(Cond))
return false;
auto *II = dyn_cast<IntrinsicInst>(Cond);
if (II->getIntrinsicID() != Intrinsic::vector_reduce_or ||
!isa<ScalableVectorType>(II->getOperand(0)->getType()))
return false;
if (isa<CmpInst>(II->getOperand(0)))
Ops.push_back(&II->getOperandUse(0));
return true;
};
switch (I->getOpcode()) {
case Instruction::GetElementPtr:
case Instruction::Add:
case Instruction::Sub:
// Sink vscales closer to uses for better isel
for (unsigned Op = 0; Op < I->getNumOperands(); ++Op) {
if (shouldSinkVScale(I->getOperand(Op), Ops)) {
Ops.push_back(&I->getOperandUse(Op));
return true;
}
}
break;
case Instruction::Select: {
if (!ShouldSinkCondition(I->getOperand(0), Ops))
return false;
Ops.push_back(&I->getOperandUse(0));
return true;
}
case Instruction::Br: {
if (cast<BranchInst>(I)->isUnconditional())
return false;
if (!ShouldSinkCondition(cast<BranchInst>(I)->getCondition(), Ops))
return false;
Ops.push_back(&I->getOperandUse(0));
return true;
}
default:
break;
}
if (!I->getType()->isVectorTy())
return false;
switch (I->getOpcode()) {
case Instruction::Sub:
case Instruction::Add: {
if (!areExtractExts(I->getOperand(0), I->getOperand(1)))
return false;
// If the exts' operands extract either the lower or upper elements, we
// can sink them too.
auto Ext1 = cast<Instruction>(I->getOperand(0));
auto Ext2 = cast<Instruction>(I->getOperand(1));
if (areExtractShuffleVectors(Ext1->getOperand(0), Ext2->getOperand(0))) {
Ops.push_back(&Ext1->getOperandUse(0));
Ops.push_back(&Ext2->getOperandUse(0));
}
Ops.push_back(&I->getOperandUse(0));
Ops.push_back(&I->getOperandUse(1));
return true;
}
case Instruction::Or: {
// Pattern: Or(And(MaskValue, A), And(Not(MaskValue), B)) ->
// bitselect(MaskValue, A, B) where Not(MaskValue) = Xor(MaskValue, -1)
if (ST->hasNEON()) {
Instruction *OtherAnd, *IA, *IB;
Value *MaskValue;
// MainAnd refers to And instruction that has 'Not' as one of its operands
if (match(I, m_c_Or(m_OneUse(m_Instruction(OtherAnd)),
m_OneUse(m_c_And(m_OneUse(m_Not(m_Value(MaskValue))),
m_Instruction(IA)))))) {
if (match(OtherAnd,
m_c_And(m_Specific(MaskValue), m_Instruction(IB)))) {
Instruction *MainAnd = I->getOperand(0) == OtherAnd
? cast<Instruction>(I->getOperand(1))
: cast<Instruction>(I->getOperand(0));
// Both Ands should be in same basic block as Or
if (I->getParent() != MainAnd->getParent() ||
I->getParent() != OtherAnd->getParent())
return false;
// Non-mask operands of both Ands should also be in same basic block
if (I->getParent() != IA->getParent() ||
I->getParent() != IB->getParent())
return false;
Ops.push_back(
&MainAnd->getOperandUse(MainAnd->getOperand(0) == IA ? 1 : 0));
Ops.push_back(&I->getOperandUse(0));
Ops.push_back(&I->getOperandUse(1));
return true;
}
}
}
return false;
}
case Instruction::Mul: {
auto ShouldSinkSplatForIndexedVariant = [](Value *V) {
auto *Ty = cast<VectorType>(V->getType());
// For SVE the lane-indexing is within 128-bits, so we can't fold splats.
if (Ty->isScalableTy())
return false;
// Indexed variants of Mul exist for i16 and i32 element types only.
return Ty->getScalarSizeInBits() == 16 || Ty->getScalarSizeInBits() == 32;
};
int NumZExts = 0, NumSExts = 0;
for (auto &Op : I->operands()) {
// Make sure we are not already sinking this operand
if (any_of(Ops, [&](Use *U) { return U->get() == Op; }))
continue;
if (match(&Op, m_ZExtOrSExt(m_Value()))) {
auto *Ext = cast<Instruction>(Op);
auto *ExtOp = Ext->getOperand(0);
if (isSplatShuffle(ExtOp) && ShouldSinkSplatForIndexedVariant(ExtOp))
Ops.push_back(&Ext->getOperandUse(0));
Ops.push_back(&Op);
if (isa<SExtInst>(Ext))
NumSExts++;
else
NumZExts++;
continue;
}
ShuffleVectorInst *Shuffle = dyn_cast<ShuffleVectorInst>(Op);
if (!Shuffle)
continue;
// If the Shuffle is a splat and the operand is a zext/sext, sinking the
// operand and the s/zext can help create indexed s/umull. This is
// especially useful to prevent i64 mul being scalarized.
if (isSplatShuffle(Shuffle) &&
match(Shuffle->getOperand(0), m_ZExtOrSExt(m_Value()))) {
Ops.push_back(&Shuffle->getOperandUse(0));
Ops.push_back(&Op);
if (match(Shuffle->getOperand(0), m_SExt(m_Value())))
NumSExts++;
else
NumZExts++;
continue;
}
Value *ShuffleOperand = Shuffle->getOperand(0);
InsertElementInst *Insert = dyn_cast<InsertElementInst>(ShuffleOperand);
if (!Insert)
continue;
Instruction *OperandInstr = dyn_cast<Instruction>(Insert->getOperand(1));
if (!OperandInstr)
continue;
ConstantInt *ElementConstant =
dyn_cast<ConstantInt>(Insert->getOperand(2));
// Check that the insertelement is inserting into element 0
if (!ElementConstant || !ElementConstant->isZero())
continue;
unsigned Opcode = OperandInstr->getOpcode();
if (Opcode == Instruction::SExt)
NumSExts++;
else if (Opcode == Instruction::ZExt)
NumZExts++;
else {
// If we find that the top bits are known 0, then we can sink and allow
// the backend to generate a umull.
unsigned Bitwidth = I->getType()->getScalarSizeInBits();
APInt UpperMask = APInt::getHighBitsSet(Bitwidth, Bitwidth / 2);
if (!MaskedValueIsZero(OperandInstr, UpperMask, DL))
continue;
NumZExts++;
}
// And(Load) is excluded to prevent CGP getting stuck in a loop of sinking
// the And, just to hoist it again back to the load.
if (!match(OperandInstr, m_And(m_Load(m_Value()), m_Value())))
Ops.push_back(&Insert->getOperandUse(1));
Ops.push_back(&Shuffle->getOperandUse(0));
Ops.push_back(&Op);
}
// It is profitable to sink if we found two of the same type of extends.
if (!Ops.empty() && (NumSExts == 2 || NumZExts == 2))
return true;
// Otherwise, see if we should sink splats for indexed variants.
if (!ShouldSinkSplatForIndexedVariant(I))
return false;
Ops.clear();
if (isSplatShuffle(I->getOperand(0)))
Ops.push_back(&I->getOperandUse(0));
if (isSplatShuffle(I->getOperand(1)))
Ops.push_back(&I->getOperandUse(1));
return !Ops.empty();
}
case Instruction::FMul: {
// For SVE the lane-indexing is within 128-bits, so we can't fold splats.
if (I->getType()->isScalableTy())
return false;
if (cast<VectorType>(I->getType())->getElementType()->isHalfTy() &&
!ST->hasFullFP16())
return false;
// Sink splats for index lane variants
if (isSplatShuffle(I->getOperand(0)))
Ops.push_back(&I->getOperandUse(0));
if (isSplatShuffle(I->getOperand(1)))
Ops.push_back(&I->getOperandUse(1));
return !Ops.empty();
}
default:
return false;
}
return false;
}
|