aboutsummaryrefslogtreecommitdiff
path: root/gcc/config/rs6000/rs6000-builtin.cc
blob: 3ce729c1e6de81a4eaf37b931c74bbfd34761a05 (plain)
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
/* Target-specific built-in function support for the Power architecture.
   See also rs6000-c.c, rs6000-gen-builtins.c, rs6000-builtins.def, and
   rs6000-overloads.def.
   Note that "normal" builtins (generic math functions, etc.) are handled
   in rs6000.c.

   Copyright (C) 2002-2022 Free Software Foundation, Inc.

   This file is part of GCC.

   GCC is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published
   by the Free Software Foundation; either version 3, or (at your
   option) any later version.

   GCC is distributed in the hope that it will be useful, but WITHOUT
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
   License for more details.

   You should have received a copy of the GNU General Public License
   along with GCC; see the file COPYING3.  If not see
   <http://www.gnu.org/licenses/>.  */

#define IN_TARGET_CODE 1

#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "target.h"
#include "backend.h"
#include "rtl.h"
#include "tree.h"
#include "memmodel.h"
#include "gimple.h"
#include "tm_p.h"
#include "optabs.h"
#include "recog.h"
#include "diagnostic-core.h"
#include "fold-const.h"
#include "stor-layout.h"
#include "calls.h"
#include "varasm.h"
#include "explow.h"
#include "expr.h"
#include "langhooks.h"
#include "gimplify.h"
#include "gimple-iterator.h"
#include "gimple-fold.h"
#include "ssa.h"
#include "tree-ssa-propagate.h"
#include "builtins.h"
#include "tree-vector-builder.h"
#include "ppc-auxv.h"
#include "rs6000-internal.h"

/* Built in types.  */
tree rs6000_builtin_types[RS6000_BTI_MAX];

/* Support targetm.vectorize.builtin_mask_for_load.  */
tree altivec_builtin_mask_for_load;

/* **** General support functions **** */

/* Raise an error message for a builtin function that is called without the
   appropriate target options being set.  */

void
rs6000_invalid_builtin (enum rs6000_gen_builtins fncode)
{
  size_t j = (size_t) fncode;
  const char *name = rs6000_builtin_info[j].bifname;

  switch (rs6000_builtin_info[j].enable)
    {
    case ENB_P5:
      error ("%qs requires the %qs option", name, "-mcpu=power5");
      break;
    case ENB_P6:
      error ("%qs requires the %qs option", name, "-mcpu=power6");
      break;
    case ENB_P6_64:
      error ("%qs requires the %qs option and either the %qs or %qs option",
	     name, "-mcpu=power6", "-m64", "-mpowerpc64");
      break;
    case ENB_ALTIVEC:
      error ("%qs requires the %qs option", name, "-maltivec");
      break;
    case ENB_CELL:
      error ("%qs requires the %qs option", name, "-mcpu=cell");
      break;
    case ENB_VSX:
      error ("%qs requires the %qs option", name, "-mvsx");
      break;
    case ENB_P7:
      error ("%qs requires the %qs option", name, "-mcpu=power7");
      break;
    case ENB_P7_64:
      error ("%qs requires the %qs option and either the %qs or %qs option",
	     name, "-mcpu=power7", "-m64", "-mpowerpc64");
      break;
    case ENB_P8:
      error ("%qs requires the %qs option", name, "-mcpu=power8");
      break;
    case ENB_P8V:
      error ("%qs requires the %qs and %qs options", name, "-mcpu=power8",
	     "-mvsx");
      break;
    case ENB_P9:
      error ("%qs requires the %qs option", name, "-mcpu=power9");
      break;
    case ENB_P9_64:
      error ("%qs requires the %qs option and either the %qs or %qs option",
	     name, "-mcpu=power9", "-m64", "-mpowerpc64");
      break;
    case ENB_P9V:
      error ("%qs requires the %qs and %qs options", name, "-mcpu=power9",
	     "-mvsx");
      break;
    case ENB_IEEE128_HW:
      error ("%qs requires quad-precision floating-point arithmetic", name);
      break;
    case ENB_DFP:
      error ("%qs requires the %qs option", name, "-mhard-dfp");
      break;
    case ENB_CRYPTO:
      error ("%qs requires the %qs option", name, "-mcrypto");
      break;
    case ENB_HTM:
      error ("%qs requires the %qs option", name, "-mhtm");
      break;
    case ENB_P10:
      error ("%qs requires the %qs option", name, "-mcpu=power10");
      break;
    case ENB_P10_64:
      error ("%qs requires the %qs option and either the %qs or %qs option",
	     name, "-mcpu=power10", "-m64", "-mpowerpc64");
      break;
    case ENB_MMA:
      error ("%qs requires the %qs option", name, "-mmma");
      break;
    default:
    case ENB_ALWAYS:
      gcc_unreachable ();
    }
}

/* Check whether a builtin function is supported in this target
   configuration.  */
bool
rs6000_builtin_is_supported (enum rs6000_gen_builtins fncode)
{
  switch (rs6000_builtin_info[(size_t) fncode].enable)
    {
    case ENB_ALWAYS:
      return true;
    case ENB_P5:
      return TARGET_POPCNTB;
    case ENB_P6:
      return TARGET_CMPB;
    case ENB_P6_64:
      return TARGET_CMPB && TARGET_POWERPC64;
    case ENB_P7:
      return TARGET_POPCNTD;
    case ENB_P7_64:
      return TARGET_POPCNTD && TARGET_POWERPC64;
    case ENB_P8:
      return TARGET_DIRECT_MOVE;
    case ENB_P8V:
      return TARGET_P8_VECTOR;
    case ENB_P9:
      return TARGET_MODULO;
    case ENB_P9_64:
      return TARGET_MODULO && TARGET_POWERPC64;
    case ENB_P9V:
      return TARGET_P9_VECTOR;
    case ENB_P10:
      return TARGET_POWER10;
    case ENB_P10_64:
      return TARGET_POWER10 && TARGET_POWERPC64;
    case ENB_ALTIVEC:
      return TARGET_ALTIVEC;
    case ENB_VSX:
      return TARGET_VSX;
    case ENB_CELL:
      return TARGET_ALTIVEC && rs6000_cpu == PROCESSOR_CELL;
    case ENB_IEEE128_HW:
      return TARGET_FLOAT128_HW;
    case ENB_DFP:
      return TARGET_DFP;
    case ENB_CRYPTO:
      return TARGET_CRYPTO;
    case ENB_HTM:
      return TARGET_HTM;
    case ENB_MMA:
      return TARGET_MMA;
    default:
      gcc_unreachable ();
    }
  gcc_unreachable ();
}

/* Target hook for early folding of built-ins, shamelessly stolen
   from ia64.cc.  */

tree
rs6000_fold_builtin (tree fndecl ATTRIBUTE_UNUSED,
		     int n_args ATTRIBUTE_UNUSED,
		     tree *args ATTRIBUTE_UNUSED,
		     bool ignore ATTRIBUTE_UNUSED)
{
#ifdef SUBTARGET_FOLD_BUILTIN
  return SUBTARGET_FOLD_BUILTIN (fndecl, n_args, args, ignore);
#else
  return NULL_TREE;
#endif
}

tree
rs6000_builtin_decl (unsigned code, bool /* initialize_p */)
{
  rs6000_gen_builtins fcode = (rs6000_gen_builtins) code;

  if (fcode >= RS6000_OVLD_MAX)
    return error_mark_node;

  return rs6000_builtin_decls[code];
}

/* Implement targetm.vectorize.builtin_mask_for_load.  */
tree
rs6000_builtin_mask_for_load (void)
{
  /* Don't use lvsl/vperm for P8 and similarly efficient machines.  */
  if ((TARGET_ALTIVEC && !TARGET_VSX)
      || (TARGET_VSX && !TARGET_EFFICIENT_UNALIGNED_VSX))
    return altivec_builtin_mask_for_load;
  else
    return 0;
}

/* Implement targetm.vectorize.builtin_md_vectorized_function.  */

tree
rs6000_builtin_md_vectorized_function (tree fndecl, tree type_out,
				       tree type_in)
{
  machine_mode in_mode, out_mode;
  int in_n, out_n;

  if (TARGET_DEBUG_BUILTIN)
    fprintf (stderr,
	     "rs6000_builtin_md_vectorized_function (%s, %s, %s)\n",
	     IDENTIFIER_POINTER (DECL_NAME (fndecl)),
	     GET_MODE_NAME (TYPE_MODE (type_out)),
	     GET_MODE_NAME (TYPE_MODE (type_in)));

  /* TODO: Should this be gcc_assert?  */
  if (TREE_CODE (type_out) != VECTOR_TYPE
      || TREE_CODE (type_in) != VECTOR_TYPE)
    return NULL_TREE;

  out_mode = TYPE_MODE (TREE_TYPE (type_out));
  out_n = TYPE_VECTOR_SUBPARTS (type_out);
  in_mode = TYPE_MODE (TREE_TYPE (type_in));
  in_n = TYPE_VECTOR_SUBPARTS (type_in);

  enum rs6000_gen_builtins fn
    = (enum rs6000_gen_builtins) DECL_MD_FUNCTION_CODE (fndecl);
  switch (fn)
    {
    case RS6000_BIF_RSQRTF:
      if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (V4SFmode)
	  && out_mode == SFmode && out_n == 4
	  && in_mode == SFmode && in_n == 4)
	return rs6000_builtin_decls[RS6000_BIF_VRSQRTFP];
      break;
    case RS6000_BIF_RSQRT:
      if (VECTOR_UNIT_VSX_P (V2DFmode)
	  && out_mode == DFmode && out_n == 2
	  && in_mode == DFmode && in_n == 2)
	return rs6000_builtin_decls[RS6000_BIF_RSQRT_2DF];
      break;
    case RS6000_BIF_RECIPF:
      if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (V4SFmode)
	  && out_mode == SFmode && out_n == 4
	  && in_mode == SFmode && in_n == 4)
	return rs6000_builtin_decls[RS6000_BIF_VRECIPFP];
      break;
    case RS6000_BIF_RECIP:
      if (VECTOR_UNIT_VSX_P (V2DFmode)
	  && out_mode == DFmode && out_n == 2
	  && in_mode == DFmode && in_n == 2)
	return rs6000_builtin_decls[RS6000_BIF_RECIP_V2DF];
      break;
    default:
      break;
    }

  machine_mode in_vmode = TYPE_MODE (type_in);
  machine_mode out_vmode = TYPE_MODE (type_out);

  /* Power10 supported vectorized built-in functions.  */
  if (TARGET_POWER10
      && in_vmode == out_vmode
      && VECTOR_UNIT_ALTIVEC_OR_VSX_P (in_vmode))
    {
      machine_mode exp_mode = DImode;
      machine_mode exp_vmode = V2DImode;
      enum rs6000_gen_builtins bif;
      switch (fn)
	{
	case RS6000_BIF_DIVWE:
	case RS6000_BIF_DIVWEU:
	  exp_mode = SImode;
	  exp_vmode = V4SImode;
	  if (fn == RS6000_BIF_DIVWE)
	    bif = RS6000_BIF_VDIVESW;
	  else
	    bif = RS6000_BIF_VDIVEUW;
	  break;
	case RS6000_BIF_DIVDE:
	case RS6000_BIF_DIVDEU:
	  if (fn == RS6000_BIF_DIVDE)
	    bif = RS6000_BIF_VDIVESD;
	  else
	    bif = RS6000_BIF_VDIVEUD;
	  break;
	case RS6000_BIF_CFUGED:
	  bif = RS6000_BIF_VCFUGED;
	  break;
	case RS6000_BIF_CNTLZDM:
	  bif = RS6000_BIF_VCLZDM;
	  break;
	case RS6000_BIF_CNTTZDM:
	  bif = RS6000_BIF_VCTZDM;
	  break;
	case RS6000_BIF_PDEPD:
	  bif = RS6000_BIF_VPDEPD;
	  break;
	case RS6000_BIF_PEXTD:
	  bif = RS6000_BIF_VPEXTD;
	  break;
	default:
	  return NULL_TREE;
	}

      if (in_mode == exp_mode && in_vmode == exp_vmode)
	return rs6000_builtin_decls[bif];
    }

  return NULL_TREE;
}

/* Returns a code for a target-specific builtin that implements
   reciprocal of the function, or NULL_TREE if not available.  */

tree
rs6000_builtin_reciprocal (tree fndecl)
{
  switch (DECL_MD_FUNCTION_CODE (fndecl))
    {
    case RS6000_BIF_XVSQRTDP:
      if (!RS6000_RECIP_AUTO_RSQRTE_P (V2DFmode))
	return NULL_TREE;

      return rs6000_builtin_decls[RS6000_BIF_RSQRT_2DF];

    case RS6000_BIF_XVSQRTSP:
      if (!RS6000_RECIP_AUTO_RSQRTE_P (V4SFmode))
	return NULL_TREE;

      return rs6000_builtin_decls[RS6000_BIF_RSQRT_4SF];

    default:
      return NULL_TREE;
    }
}

/* **** Initialization support **** */

/* Create a builtin vector type with a name.  Taking care not to give
   the canonical type a name.  */

static tree
rs6000_vector_type (const char *name, tree elt_type, unsigned num_elts)
{
  tree result = build_vector_type (elt_type, num_elts);

  /* Copy so we don't give the canonical type a name.  */
  result = build_variant_type_copy (result);

  add_builtin_type (name, result);

  return result;
}

/* Debug utility to translate a type node to a single textual token.  */
static
const char *rs6000_type_string (tree type_node)
{
  if (type_node == NULL_TREE)
    return "**NULL**";
  else if (type_node == void_type_node)
    return "void";
  else if (type_node == long_integer_type_node)
    return "long";
  else if (type_node == long_unsigned_type_node)
    return "ulong";
  else if (type_node == long_long_integer_type_node)
    return "longlong";
  else if (type_node == long_long_unsigned_type_node)
    return "ulonglong";
  else if (type_node == bool_V2DI_type_node)
    return "vbll";
  else if (type_node == bool_V4SI_type_node)
    return "vbi";
  else if (type_node == bool_V8HI_type_node)
    return "vbs";
  else if (type_node == bool_V16QI_type_node)
    return "vbc";
  else if (type_node == bool_int_type_node)
    return "bool";
  else if (type_node == dfloat64_type_node)
    return "_Decimal64";
  else if (type_node == double_type_node)
    return "double";
  else if (type_node == intDI_type_node)
    return "sll";
  else if (type_node == intHI_type_node)
    return "ss";
  else if (type_node == ibm128_float_type_node)
    return "__ibm128";
  else if (type_node == ieee128_float_type_node)
    return "__ieee128";
  else if (type_node == opaque_V4SI_type_node)
    return "opaque";
  else if (POINTER_TYPE_P (type_node))
    return "void*";
  else if (type_node == intQI_type_node || type_node == char_type_node)
    return "sc";
  else if (type_node == dfloat32_type_node)
    return "_Decimal32";
  else if (type_node == float_type_node)
    return "float";
  else if (type_node == intSI_type_node || type_node == integer_type_node)
    return "si";
  else if (type_node == dfloat128_type_node)
    return "_Decimal128";
  else if (type_node == long_double_type_node)
    return "longdouble";
  else if (type_node == intTI_type_node)
    return "sq";
  else if (type_node == unsigned_intDI_type_node)
    return "ull";
  else if (type_node == unsigned_intHI_type_node)
    return "us";
  else if (type_node == unsigned_intQI_type_node)
    return "uc";
  else if (type_node == unsigned_intSI_type_node)
    return "ui";
  else if (type_node == unsigned_intTI_type_node)
    return "uq";
  else if (type_node == unsigned_V1TI_type_node)
    return "vuq";
  else if (type_node == unsigned_V2DI_type_node)
    return "vull";
  else if (type_node == unsigned_V4SI_type_node)
    return "vui";
  else if (type_node == unsigned_V8HI_type_node)
    return "vus";
  else if (type_node == unsigned_V16QI_type_node)
    return "vuc";
  else if (type_node == V16QI_type_node)
    return "vsc";
  else if (type_node == V1TI_type_node)
    return "vsq";
  else if (type_node == V2DF_type_node)
    return "vd";
  else if (type_node == V2DI_type_node)
    return "vsll";
  else if (type_node == V4SF_type_node)
    return "vf";
  else if (type_node == V4SI_type_node)
    return "vsi";
  else if (type_node == V8HI_type_node)
    return "vss";
  else if (type_node == pixel_V8HI_type_node)
    return "vp";
  else if (type_node == pcvoid_type_node)
    return "voidc*";
  else if (type_node == float128_type_node)
    return "_Float128";
  else if (type_node == vector_pair_type_node)
    return "__vector_pair";
  else if (type_node == vector_quad_type_node)
    return "__vector_quad";

  return "unknown";
}

void
rs6000_init_builtins (void)
{
  tree tdecl;
  tree t;

  if (TARGET_DEBUG_BUILTIN)
    fprintf (stderr, "rs6000_init_builtins%s%s\n",
	     (TARGET_ALTIVEC)	   ? ", altivec" : "",
	     (TARGET_VSX)	   ? ", vsx"	 : "");

  V2DI_type_node = rs6000_vector_type ("__vector long long",
				       long_long_integer_type_node, 2);
  ptr_V2DI_type_node
    = build_pointer_type (build_qualified_type (V2DI_type_node,
						TYPE_QUAL_CONST));

  V2DF_type_node = rs6000_vector_type ("__vector double", double_type_node, 2);
  ptr_V2DF_type_node
    = build_pointer_type (build_qualified_type (V2DF_type_node,
						TYPE_QUAL_CONST));

  V4SI_type_node = rs6000_vector_type ("__vector signed int",
				       intSI_type_node, 4);
  ptr_V4SI_type_node
    = build_pointer_type (build_qualified_type (V4SI_type_node,
						TYPE_QUAL_CONST));

  V4SF_type_node = rs6000_vector_type ("__vector float", float_type_node, 4);
  ptr_V4SF_type_node
    = build_pointer_type (build_qualified_type (V4SF_type_node,
						TYPE_QUAL_CONST));

  V8HI_type_node = rs6000_vector_type ("__vector signed short",
				       intHI_type_node, 8);
  ptr_V8HI_type_node
    = build_pointer_type (build_qualified_type (V8HI_type_node,
						TYPE_QUAL_CONST));

  V16QI_type_node = rs6000_vector_type ("__vector signed char",
					intQI_type_node, 16);
  ptr_V16QI_type_node
    = build_pointer_type (build_qualified_type (V16QI_type_node,
						TYPE_QUAL_CONST));

  unsigned_V16QI_type_node = rs6000_vector_type ("__vector unsigned char",
					unsigned_intQI_type_node, 16);
  ptr_unsigned_V16QI_type_node
    = build_pointer_type (build_qualified_type (unsigned_V16QI_type_node,
						TYPE_QUAL_CONST));

  unsigned_V8HI_type_node = rs6000_vector_type ("__vector unsigned short",
				       unsigned_intHI_type_node, 8);
  ptr_unsigned_V8HI_type_node
    = build_pointer_type (build_qualified_type (unsigned_V8HI_type_node,
						TYPE_QUAL_CONST));

  unsigned_V4SI_type_node = rs6000_vector_type ("__vector unsigned int",
				       unsigned_intSI_type_node, 4);
  ptr_unsigned_V4SI_type_node
    = build_pointer_type (build_qualified_type (unsigned_V4SI_type_node,
						TYPE_QUAL_CONST));

  unsigned_V2DI_type_node
    = rs6000_vector_type ("__vector unsigned long long",
			  long_long_unsigned_type_node, 2);

  ptr_unsigned_V2DI_type_node
    = build_pointer_type (build_qualified_type (unsigned_V2DI_type_node,
						TYPE_QUAL_CONST));

  opaque_V4SI_type_node = build_opaque_vector_type (intSI_type_node, 4);

  const_str_type_node
    = build_pointer_type (build_qualified_type (char_type_node,
						TYPE_QUAL_CONST));

  /* We use V1TI mode as a special container to hold __int128_t items that
     must live in VSX registers.  */
  if (intTI_type_node)
    {
      V1TI_type_node = rs6000_vector_type ("__vector __int128",
					   intTI_type_node, 1);
      ptr_V1TI_type_node
	= build_pointer_type (build_qualified_type (V1TI_type_node,
						    TYPE_QUAL_CONST));
      unsigned_V1TI_type_node
	= rs6000_vector_type ("__vector unsigned __int128",
			      unsigned_intTI_type_node, 1);
      ptr_unsigned_V1TI_type_node
	= build_pointer_type (build_qualified_type (unsigned_V1TI_type_node,
						    TYPE_QUAL_CONST));
    }

  /* The 'vector bool ...' types must be kept distinct from 'vector unsigned ...'
     types, especially in C++ land.  Similarly, 'vector pixel' is distinct from
     'vector unsigned short'.  */

  bool_char_type_node = build_distinct_type_copy (unsigned_intQI_type_node);
  bool_short_type_node = build_distinct_type_copy (unsigned_intHI_type_node);
  bool_int_type_node = build_distinct_type_copy (unsigned_intSI_type_node);
  bool_long_long_type_node = build_distinct_type_copy (unsigned_intDI_type_node);
  pixel_type_node = build_distinct_type_copy (unsigned_intHI_type_node);

  long_integer_type_internal_node = long_integer_type_node;
  long_unsigned_type_internal_node = long_unsigned_type_node;
  long_long_integer_type_internal_node = long_long_integer_type_node;
  long_long_unsigned_type_internal_node = long_long_unsigned_type_node;
  intQI_type_internal_node = intQI_type_node;
  uintQI_type_internal_node = unsigned_intQI_type_node;
  intHI_type_internal_node = intHI_type_node;
  uintHI_type_internal_node = unsigned_intHI_type_node;
  intSI_type_internal_node = intSI_type_node;
  uintSI_type_internal_node = unsigned_intSI_type_node;
  intDI_type_internal_node = intDI_type_node;
  uintDI_type_internal_node = unsigned_intDI_type_node;
  intTI_type_internal_node = intTI_type_node;
  uintTI_type_internal_node = unsigned_intTI_type_node;
  float_type_internal_node = float_type_node;
  double_type_internal_node = double_type_node;
  long_double_type_internal_node = long_double_type_node;
  dfloat64_type_internal_node = dfloat64_type_node;
  dfloat128_type_internal_node = dfloat128_type_node;
  void_type_internal_node = void_type_node;

  ptr_intQI_type_node
    = build_pointer_type (build_qualified_type (intQI_type_internal_node,
						TYPE_QUAL_CONST));
  ptr_uintQI_type_node
    = build_pointer_type (build_qualified_type (uintQI_type_internal_node,
						TYPE_QUAL_CONST));
  ptr_intHI_type_node
    = build_pointer_type (build_qualified_type (intHI_type_internal_node,
						TYPE_QUAL_CONST));
  ptr_uintHI_type_node
    = build_pointer_type (build_qualified_type (uintHI_type_internal_node,
						TYPE_QUAL_CONST));
  ptr_intSI_type_node
    = build_pointer_type (build_qualified_type (intSI_type_internal_node,
						TYPE_QUAL_CONST));
  ptr_uintSI_type_node
    = build_pointer_type (build_qualified_type (uintSI_type_internal_node,
						TYPE_QUAL_CONST));
  ptr_intDI_type_node
    = build_pointer_type (build_qualified_type (intDI_type_internal_node,
						TYPE_QUAL_CONST));
  ptr_uintDI_type_node
    = build_pointer_type (build_qualified_type (uintDI_type_internal_node,
						TYPE_QUAL_CONST));
  ptr_intTI_type_node
    = build_pointer_type (build_qualified_type (intTI_type_internal_node,
						TYPE_QUAL_CONST));
  ptr_uintTI_type_node
    = build_pointer_type (build_qualified_type (uintTI_type_internal_node,
						TYPE_QUAL_CONST));

  t = build_qualified_type (long_integer_type_internal_node, TYPE_QUAL_CONST);
  ptr_long_integer_type_node = build_pointer_type (t);

  t = build_qualified_type (long_unsigned_type_internal_node, TYPE_QUAL_CONST);
  ptr_long_unsigned_type_node = build_pointer_type (t);

  ptr_float_type_node
    = build_pointer_type (build_qualified_type (float_type_internal_node,
						TYPE_QUAL_CONST));
  ptr_double_type_node
    = build_pointer_type (build_qualified_type (double_type_internal_node,
						TYPE_QUAL_CONST));
  ptr_long_double_type_node
    = build_pointer_type (build_qualified_type (long_double_type_internal_node,
						TYPE_QUAL_CONST));
  if (dfloat64_type_node)
    {
      t = build_qualified_type (dfloat64_type_internal_node, TYPE_QUAL_CONST);
      ptr_dfloat64_type_node = build_pointer_type (t);
    }
  else
    ptr_dfloat64_type_node = NULL;

  if (dfloat128_type_node)
    {
      t = build_qualified_type (dfloat128_type_internal_node, TYPE_QUAL_CONST);
      ptr_dfloat128_type_node = build_pointer_type (t);
    }
  else
    ptr_dfloat128_type_node = NULL;

  t = build_qualified_type (long_long_integer_type_internal_node,
			    TYPE_QUAL_CONST);
  ptr_long_long_integer_type_node  = build_pointer_type (t);

  t = build_qualified_type (long_long_unsigned_type_internal_node,
			    TYPE_QUAL_CONST);
  ptr_long_long_unsigned_type_node = build_pointer_type (t);

  /* 128-bit floating point support.  KFmode is IEEE 128-bit floating point.
     IFmode is the IBM extended 128-bit format that is a pair of doubles.
     TFmode will be either IEEE 128-bit floating point or the IBM double-double
     format that uses a pair of doubles, depending on the switches and
     defaults.

     If we don't support for either 128-bit IBM double double or IEEE 128-bit
     floating point, we need make sure the type is non-zero or else self-test
     fails during bootstrap.

     Always create __ibm128 as a separate type, even if the current long double
     format is IBM extended double.

     For IEEE 128-bit floating point, always create the type __ieee128.  If the
     user used -mfloat128, rs6000-c.cc will create a define from __float128 to
     __ieee128.  */
  if (TARGET_LONG_DOUBLE_128 && (!TARGET_IEEEQUAD || TARGET_FLOAT128_TYPE))
    {
      if (!TARGET_IEEEQUAD)
	ibm128_float_type_node = long_double_type_node;
      else
	{
	  ibm128_float_type_node = make_node (REAL_TYPE);
	  TYPE_PRECISION (ibm128_float_type_node) = 128;
	  SET_TYPE_MODE (ibm128_float_type_node, IFmode);
	  layout_type (ibm128_float_type_node);
	}
      t = build_qualified_type (ibm128_float_type_node, TYPE_QUAL_CONST);
      lang_hooks.types.register_builtin_type (ibm128_float_type_node,
					      "__ibm128");
    }
  else
    ibm128_float_type_node = NULL_TREE;

  if (TARGET_FLOAT128_TYPE)
    {
      if (TARGET_IEEEQUAD && TARGET_LONG_DOUBLE_128)
	ieee128_float_type_node = long_double_type_node;
      else
	ieee128_float_type_node = float128_type_node;
      t = build_qualified_type (ieee128_float_type_node, TYPE_QUAL_CONST);
      lang_hooks.types.register_builtin_type (ieee128_float_type_node,
					      "__ieee128");
    }
  else
    ieee128_float_type_node = NULL_TREE;

  /* Vector pair and vector quad support.  */
  vector_pair_type_node = make_node (OPAQUE_TYPE);
  SET_TYPE_MODE (vector_pair_type_node, OOmode);
  TYPE_SIZE (vector_pair_type_node) = bitsize_int (GET_MODE_BITSIZE (OOmode));
  TYPE_PRECISION (vector_pair_type_node) = GET_MODE_BITSIZE (OOmode);
  TYPE_SIZE_UNIT (vector_pair_type_node) = size_int (GET_MODE_SIZE (OOmode));
  SET_TYPE_ALIGN (vector_pair_type_node, 256);
  TYPE_USER_ALIGN (vector_pair_type_node) = 0;
  lang_hooks.types.register_builtin_type (vector_pair_type_node,
					  "__vector_pair");
  t = build_qualified_type (vector_pair_type_node, TYPE_QUAL_CONST);
  ptr_vector_pair_type_node = build_pointer_type (t);

  vector_quad_type_node = make_node (OPAQUE_TYPE);
  SET_TYPE_MODE (vector_quad_type_node, XOmode);
  TYPE_SIZE (vector_quad_type_node) = bitsize_int (GET_MODE_BITSIZE (XOmode));
  TYPE_PRECISION (vector_quad_type_node) = GET_MODE_BITSIZE (XOmode);
  TYPE_SIZE_UNIT (vector_quad_type_node) = size_int (GET_MODE_SIZE (XOmode));
  SET_TYPE_ALIGN (vector_quad_type_node, 512);
  TYPE_USER_ALIGN (vector_quad_type_node) = 0;
  lang_hooks.types.register_builtin_type (vector_quad_type_node,
					  "__vector_quad");
  t = build_qualified_type (vector_quad_type_node, TYPE_QUAL_CONST);
  ptr_vector_quad_type_node = build_pointer_type (t);

  tdecl = add_builtin_type ("__bool char", bool_char_type_node);
  TYPE_NAME (bool_char_type_node) = tdecl;

  tdecl = add_builtin_type ("__bool short", bool_short_type_node);
  TYPE_NAME (bool_short_type_node) = tdecl;

  tdecl = add_builtin_type ("__bool int", bool_int_type_node);
  TYPE_NAME (bool_int_type_node) = tdecl;

  tdecl = add_builtin_type ("__pixel", pixel_type_node);
  TYPE_NAME (pixel_type_node) = tdecl;

  bool_V16QI_type_node = rs6000_vector_type ("__vector __bool char",
					     bool_char_type_node, 16);
  ptr_bool_V16QI_type_node
    = build_pointer_type (build_qualified_type (bool_V16QI_type_node,
						TYPE_QUAL_CONST));

  bool_V8HI_type_node = rs6000_vector_type ("__vector __bool short",
					    bool_short_type_node, 8);
  ptr_bool_V8HI_type_node
    = build_pointer_type (build_qualified_type (bool_V8HI_type_node,
						TYPE_QUAL_CONST));

  bool_V4SI_type_node = rs6000_vector_type ("__vector __bool int",
					    bool_int_type_node, 4);
  ptr_bool_V4SI_type_node
    = build_pointer_type (build_qualified_type (bool_V4SI_type_node,
						TYPE_QUAL_CONST));

  bool_V2DI_type_node = rs6000_vector_type (TARGET_POWERPC64
					    ? "__vector __bool long"
					    : "__vector __bool long long",
					    bool_long_long_type_node, 2);
  ptr_bool_V2DI_type_node
    = build_pointer_type (build_qualified_type (bool_V2DI_type_node,
						TYPE_QUAL_CONST));

  bool_V1TI_type_node = rs6000_vector_type ("__vector __bool __int128",
					    intTI_type_node, 1);
  ptr_bool_V1TI_type_node
    = build_pointer_type (build_qualified_type (bool_V1TI_type_node,
						TYPE_QUAL_CONST));

  pixel_V8HI_type_node = rs6000_vector_type ("__vector __pixel",
					     pixel_type_node, 8);
  ptr_pixel_V8HI_type_node
    = build_pointer_type (build_qualified_type (pixel_V8HI_type_node,
						TYPE_QUAL_CONST));
  pcvoid_type_node
    = build_pointer_type (build_qualified_type (void_type_node,
						TYPE_QUAL_CONST));

  /* Execute the autogenerated initialization code for builtins.  */
  rs6000_init_generated_builtins ();

  if (TARGET_DEBUG_BUILTIN)
    {
      fprintf (stderr, "\nAutogenerated built-in functions:\n\n");
      for (int i = 1; i < (int) RS6000_BIF_MAX; i++)
	{
	  enum rs6000_gen_builtins fn_code = (enum rs6000_gen_builtins) i;
	  if (!rs6000_builtin_is_supported (fn_code))
	    continue;
	  tree fntype = rs6000_builtin_info[i].fntype;
	  tree t = TREE_TYPE (fntype);
	  fprintf (stderr, "%s %s (", rs6000_type_string (t),
		   rs6000_builtin_info[i].bifname);
	  t = TYPE_ARG_TYPES (fntype);
	  while (t && TREE_VALUE (t) != void_type_node)
	    {
	      fprintf (stderr, "%s",
		       rs6000_type_string (TREE_VALUE (t)));
	      t = TREE_CHAIN (t);
	      if (t && TREE_VALUE (t) != void_type_node)
		fprintf (stderr, ", ");
	    }
	  fprintf (stderr, "); %s [%4d]\n",
		   rs6000_builtin_info[i].attr_string, (int) i);
	}
      fprintf (stderr, "\nEnd autogenerated built-in functions.\n\n\n");
     }

  if (TARGET_XCOFF)
    {
      /* AIX libm provides clog as __clog.  */
      if ((tdecl = builtin_decl_explicit (BUILT_IN_CLOG)) != NULL_TREE)
	set_user_assembler_name (tdecl, "__clog");

      /* When long double is 64 bit, some long double builtins of libc
	 functions (like __builtin_frexpl) must call the double version
	 (frexp) not the long double version (frexpl) that expects a 128 bit
	 argument.  */
      if (! TARGET_LONG_DOUBLE_128)
	{
	  if ((tdecl = builtin_decl_explicit (BUILT_IN_FMODL)) != NULL_TREE)
	    set_user_assembler_name (tdecl, "fmod");
	  if ((tdecl = builtin_decl_explicit (BUILT_IN_FREXPL)) != NULL_TREE)
	    set_user_assembler_name (tdecl, "frexp");
	  if ((tdecl = builtin_decl_explicit (BUILT_IN_LDEXPL)) != NULL_TREE)
	    set_user_assembler_name (tdecl, "ldexp");
	  if ((tdecl = builtin_decl_explicit (BUILT_IN_MODFL)) != NULL_TREE)
	    set_user_assembler_name (tdecl, "modf");
	}
    }

  altivec_builtin_mask_for_load
    = rs6000_builtin_decls[RS6000_BIF_MASK_FOR_LOAD];

#ifdef SUBTARGET_INIT_BUILTINS
  SUBTARGET_INIT_BUILTINS;
#endif

  return;
}

/* **** GIMPLE folding support **** */

/* Helper function to handle the gimple folding of a vector compare
   operation.  This sets up true/false vectors, and uses the
   VEC_COND_EXPR operation.
   CODE indicates which comparison is to be made. (EQ, GT, ...).
   TYPE indicates the type of the result.
   Code is inserted before GSI.  */
static tree
fold_build_vec_cmp (tree_code code, tree type, tree arg0, tree arg1,
		    gimple_stmt_iterator *gsi)
{
  tree cmp_type = truth_type_for (type);
  tree zero_vec = build_zero_cst (type);
  tree minus_one_vec = build_minus_one_cst (type);
  tree temp = create_tmp_reg_or_ssa_name (cmp_type);
  gimple *g = gimple_build_assign (temp, code, arg0, arg1);
  gsi_insert_before (gsi, g, GSI_SAME_STMT);
  return fold_build3 (VEC_COND_EXPR, type, temp, minus_one_vec, zero_vec);
}

/* Helper function to handle the in-between steps for the
   vector compare built-ins.  */
static void
fold_compare_helper (gimple_stmt_iterator *gsi, tree_code code, gimple *stmt)
{
  tree arg0 = gimple_call_arg (stmt, 0);
  tree arg1 = gimple_call_arg (stmt, 1);
  tree lhs = gimple_call_lhs (stmt);
  tree cmp = fold_build_vec_cmp (code, TREE_TYPE (lhs), arg0, arg1, gsi);
  gimple *g = gimple_build_assign (lhs, cmp);
  gimple_set_location (g, gimple_location (stmt));
  gsi_replace (gsi, g, true);
}

/* Helper function to map V2DF and V4SF types to their
 integral equivalents (V2DI and V4SI).  */
tree map_to_integral_tree_type (tree input_tree_type)
{
  if (INTEGRAL_TYPE_P (TREE_TYPE (input_tree_type)))
    return input_tree_type;
  else
    {
      if (types_compatible_p (TREE_TYPE (input_tree_type),
			      TREE_TYPE (V2DF_type_node)))
	return V2DI_type_node;
      else if (types_compatible_p (TREE_TYPE (input_tree_type),
				   TREE_TYPE (V4SF_type_node)))
	return V4SI_type_node;
      else
	gcc_unreachable ();
    }
}

/* Helper function to handle the vector merge[hl] built-ins.  The
   implementation difference between h and l versions for this code are in
   the values used when building of the permute vector for high word versus
   low word merge.  The variance is keyed off the use_high parameter.  */
static void
fold_mergehl_helper (gimple_stmt_iterator *gsi, gimple *stmt, int use_high)
{
  tree arg0 = gimple_call_arg (stmt, 0);
  tree arg1 = gimple_call_arg (stmt, 1);
  tree lhs = gimple_call_lhs (stmt);
  tree lhs_type = TREE_TYPE (lhs);
  int n_elts = TYPE_VECTOR_SUBPARTS (lhs_type);
  int midpoint = n_elts / 2;
  int offset = 0;

  if (use_high == 1)
    offset = midpoint;

  /* The permute_type will match the lhs for integral types.  For double and
     float types, the permute type needs to map to the V2 or V4 type that
     matches size.  */
  tree permute_type;
  permute_type = map_to_integral_tree_type (lhs_type);
  tree_vector_builder elts (permute_type, VECTOR_CST_NELTS (arg0), 1);

  for (int i = 0; i < midpoint; i++)
    {
      elts.safe_push (build_int_cst (TREE_TYPE (permute_type),
				     offset + i));
      elts.safe_push (build_int_cst (TREE_TYPE (permute_type),
				     offset + n_elts + i));
    }

  tree permute = elts.build ();

  gimple *g = gimple_build_assign (lhs, VEC_PERM_EXPR, arg0, arg1, permute);
  gimple_set_location (g, gimple_location (stmt));
  gsi_replace (gsi, g, true);
}

/* Helper function to handle the vector merge[eo] built-ins.  */
static void
fold_mergeeo_helper (gimple_stmt_iterator *gsi, gimple *stmt, int use_odd)
{
  tree arg0 = gimple_call_arg (stmt, 0);
  tree arg1 = gimple_call_arg (stmt, 1);
  tree lhs = gimple_call_lhs (stmt);
  tree lhs_type = TREE_TYPE (lhs);
  int n_elts = TYPE_VECTOR_SUBPARTS (lhs_type);

  /* The permute_type will match the lhs for integral types.  For double and
     float types, the permute type needs to map to the V2 or V4 type that
     matches size.  */
  tree permute_type;
  permute_type = map_to_integral_tree_type (lhs_type);

  tree_vector_builder elts (permute_type, VECTOR_CST_NELTS (arg0), 1);

 /* Build the permute vector.  */
  for (int i = 0; i < n_elts / 2; i++)
    {
      elts.safe_push (build_int_cst (TREE_TYPE (permute_type),
				     2*i + use_odd));
      elts.safe_push (build_int_cst (TREE_TYPE (permute_type),
				     2*i + use_odd + n_elts));
    }

  tree permute = elts.build ();

  gimple *g = gimple_build_assign (lhs, VEC_PERM_EXPR, arg0, arg1, permute);
  gimple_set_location (g, gimple_location (stmt));
  gsi_replace (gsi, g, true);
}

/*  Helper function to sort out which built-ins may be valid without having
    a LHS.  */
static bool
rs6000_builtin_valid_without_lhs (enum rs6000_gen_builtins fn_code,
				  tree fndecl)
{
  if (TREE_TYPE (TREE_TYPE (fndecl)) == void_type_node)
    return true;

  switch (fn_code)
    {
    case RS6000_BIF_STVX_V16QI:
    case RS6000_BIF_STVX_V8HI:
    case RS6000_BIF_STVX_V4SI:
    case RS6000_BIF_STVX_V4SF:
    case RS6000_BIF_STVX_V2DI:
    case RS6000_BIF_STVX_V2DF:
    case RS6000_BIF_STXVW4X_V16QI:
    case RS6000_BIF_STXVW4X_V8HI:
    case RS6000_BIF_STXVW4X_V4SF:
    case RS6000_BIF_STXVW4X_V4SI:
    case RS6000_BIF_STXVD2X_V2DF:
    case RS6000_BIF_STXVD2X_V2DI:
      return true;
    default:
      return false;
    }
}

/* Expand the MMA built-ins early, so that we can convert the pass-by-reference
   __vector_quad arguments into pass-by-value arguments, leading to more
   efficient code generation.  */
static bool
rs6000_gimple_fold_mma_builtin (gimple_stmt_iterator *gsi,
				rs6000_gen_builtins fn_code)
{
  gimple *stmt = gsi_stmt (*gsi);
  size_t fncode = (size_t) fn_code;

  if (!bif_is_mma (rs6000_builtin_info[fncode]))
    return false;

  /* Each call that can be gimple-expanded has an associated built-in
     function that it will expand into.  If this one doesn't, we have
     already expanded it!  Exceptions: lxvp and stxvp.  */
  if (rs6000_builtin_info[fncode].assoc_bif == RS6000_BIF_NONE
      && fncode != RS6000_BIF_LXVP
      && fncode != RS6000_BIF_STXVP)
    return false;

  bifdata *bd = &rs6000_builtin_info[fncode];
  unsigned nopnds = bd->nargs;
  gimple_seq new_seq = NULL;
  gimple *new_call;
  tree new_decl;

  /* Compatibility built-ins; we used to call these
     __builtin_mma_{dis,}assemble_pair, but now we call them
     __builtin_vsx_{dis,}assemble_pair.  Handle the old versions.  */
  if (fncode == RS6000_BIF_ASSEMBLE_PAIR)
    fncode = RS6000_BIF_ASSEMBLE_PAIR_V;
  else if (fncode == RS6000_BIF_DISASSEMBLE_PAIR)
    fncode = RS6000_BIF_DISASSEMBLE_PAIR_V;

  if (fncode == RS6000_BIF_DISASSEMBLE_ACC
      || fncode == RS6000_BIF_DISASSEMBLE_PAIR_V)
    {
      /* This is an MMA disassemble built-in function.  */
      push_gimplify_context (true);
      unsigned nvec = (fncode == RS6000_BIF_DISASSEMBLE_ACC) ? 4 : 2;
      tree dst_ptr = gimple_call_arg (stmt, 0);
      tree src_ptr = gimple_call_arg (stmt, 1);
      tree src_type = (fncode == RS6000_BIF_DISASSEMBLE_ACC)
		      ? build_pointer_type (vector_quad_type_node)
		      : build_pointer_type (vector_pair_type_node);
      if (TREE_TYPE (src_ptr) != src_type)
	src_ptr = build1 (NOP_EXPR, src_type, src_ptr);

      tree src = create_tmp_reg_or_ssa_name (TREE_TYPE (src_type));
      gimplify_assign (src, build_simple_mem_ref (src_ptr), &new_seq);

      /* If we are not disassembling an accumulator/pair or our destination is
	 another accumulator/pair, then just copy the entire thing as is.  */
      if ((fncode == RS6000_BIF_DISASSEMBLE_ACC
	   && TREE_TYPE (TREE_TYPE (dst_ptr)) == vector_quad_type_node)
	  || (fncode == RS6000_BIF_DISASSEMBLE_PAIR_V
	      && TREE_TYPE (TREE_TYPE (dst_ptr)) == vector_pair_type_node))
	{
	  tree dst = build_simple_mem_ref (build1 (NOP_EXPR,
						   src_type, dst_ptr));
	  gimplify_assign (dst, src, &new_seq);
	  pop_gimplify_context (NULL);
	  gsi_replace_with_seq (gsi, new_seq, true);
	  return true;
	}

      /* If we're disassembling an accumulator into a different type, we need
	 to emit a xxmfacc instruction now, since we cannot do it later.  */
      if (fncode == RS6000_BIF_DISASSEMBLE_ACC)
	{
	  new_decl = rs6000_builtin_decls[RS6000_BIF_XXMFACC_INTERNAL];
	  new_call = gimple_build_call (new_decl, 1, src);
	  src = create_tmp_reg_or_ssa_name (vector_quad_type_node);
	  gimple_call_set_lhs (new_call, src);
	  gimple_seq_add_stmt (&new_seq, new_call);
	}

      /* Copy the accumulator/pair vector by vector.  */
      new_decl
	= rs6000_builtin_decls[rs6000_builtin_info[fncode].assoc_bif];
      tree dst_type = build_pointer_type_for_mode (unsigned_V16QI_type_node,
						   ptr_mode, true);
      tree dst_base = build1 (NOP_EXPR, dst_type, dst_ptr);
      for (unsigned i = 0; i < nvec; i++)
	{
	  unsigned index = WORDS_BIG_ENDIAN ? i : nvec - 1 - i;
	  tree dst = build2 (MEM_REF, unsigned_V16QI_type_node, dst_base,
			     build_int_cst (dst_type, index * 16));
	  tree dstssa = create_tmp_reg_or_ssa_name (unsigned_V16QI_type_node);
	  new_call = gimple_build_call (new_decl, 2, src,
					build_int_cstu (uint16_type_node, i));
	  gimple_call_set_lhs (new_call, dstssa);
	  gimple_seq_add_stmt (&new_seq, new_call);
	  gimplify_assign (dst, dstssa, &new_seq);
	}
      pop_gimplify_context (NULL);
      gsi_replace_with_seq (gsi, new_seq, true);
      return true;
    }

  /* TODO: Do some factoring on these two chunks.  */
  if (fncode == RS6000_BIF_LXVP)
    {
      push_gimplify_context (true);
      tree offset = gimple_call_arg (stmt, 0);
      tree ptr = gimple_call_arg (stmt, 1);
      tree lhs = gimple_call_lhs (stmt);
      if (TREE_TYPE (TREE_TYPE (ptr)) != vector_pair_type_node)
	ptr = build1 (NOP_EXPR,
		      build_pointer_type (vector_pair_type_node), ptr);
      tree mem = build_simple_mem_ref (build2 (POINTER_PLUS_EXPR,
					       TREE_TYPE (ptr), ptr, offset));
      gimplify_assign (lhs, mem, &new_seq);
      pop_gimplify_context (NULL);
      gsi_replace_with_seq (gsi, new_seq, true);
      return true;
    }

  if (fncode == RS6000_BIF_STXVP)
    {
      push_gimplify_context (true);
      tree src = gimple_call_arg (stmt, 0);
      tree offset = gimple_call_arg (stmt, 1);
      tree ptr = gimple_call_arg (stmt, 2);
      if (TREE_TYPE (TREE_TYPE (ptr)) != vector_pair_type_node)
	ptr = build1 (NOP_EXPR,
		      build_pointer_type (vector_pair_type_node), ptr);
      tree mem = build_simple_mem_ref (build2 (POINTER_PLUS_EXPR,
					       TREE_TYPE (ptr), ptr, offset));
      gimplify_assign (mem, src, &new_seq);
      pop_gimplify_context (NULL);
      gsi_replace_with_seq (gsi, new_seq, true);
      return true;
    }

  /* Convert this built-in into an internal version that uses pass-by-value
     arguments.  The internal built-in is found in the assoc_bif field.  */
  new_decl = rs6000_builtin_decls[rs6000_builtin_info[fncode].assoc_bif];
  tree lhs, op[MAX_MMA_OPERANDS];
  tree acc = gimple_call_arg (stmt, 0);
  push_gimplify_context (true);

  if (bif_is_quad (*bd))
    {
      /* This built-in has a pass-by-reference accumulator input, so load it
	 into a temporary accumulator for use as a pass-by-value input.  */
      op[0] = create_tmp_reg_or_ssa_name (vector_quad_type_node);
      for (unsigned i = 1; i < nopnds; i++)
	op[i] = gimple_call_arg (stmt, i);
      gimplify_assign (op[0], build_simple_mem_ref (acc), &new_seq);
    }
  else
    {
      /* This built-in does not use its pass-by-reference accumulator argument
	 as an input argument, so remove it from the input list.  */
      nopnds--;
      for (unsigned i = 0; i < nopnds; i++)
	op[i] = gimple_call_arg (stmt, i + 1);
    }

  switch (nopnds)
    {
    case 0:
      new_call = gimple_build_call (new_decl, 0);
      break;
    case 1:
      new_call = gimple_build_call (new_decl, 1, op[0]);
      break;
    case 2:
      new_call = gimple_build_call (new_decl, 2, op[0], op[1]);
      break;
    case 3:
      new_call = gimple_build_call (new_decl, 3, op[0], op[1], op[2]);
      break;
    case 4:
      new_call = gimple_build_call (new_decl, 4, op[0], op[1], op[2], op[3]);
      break;
    case 5:
      new_call = gimple_build_call (new_decl, 5, op[0], op[1], op[2], op[3],
				    op[4]);
      break;
    case 6:
      new_call = gimple_build_call (new_decl, 6, op[0], op[1], op[2], op[3],
				    op[4], op[5]);
      break;
    case 7:
      new_call = gimple_build_call (new_decl, 7, op[0], op[1], op[2], op[3],
				    op[4], op[5], op[6]);
      break;
    default:
      gcc_unreachable ();
    }

  if (fncode == RS6000_BIF_BUILD_PAIR || fncode == RS6000_BIF_ASSEMBLE_PAIR_V)
    lhs = create_tmp_reg_or_ssa_name (vector_pair_type_node);
  else
    lhs = create_tmp_reg_or_ssa_name (vector_quad_type_node);
  gimple_call_set_lhs (new_call, lhs);
  gimple_seq_add_stmt (&new_seq, new_call);
  gimplify_assign (build_simple_mem_ref (acc), lhs, &new_seq);
  pop_gimplify_context (NULL);
  gsi_replace_with_seq (gsi, new_seq, true);

  return true;
}

/* Fold a machine-dependent built-in in GIMPLE.  (For folding into
   a constant, use rs6000_fold_builtin.)  */
bool
rs6000_gimple_fold_builtin (gimple_stmt_iterator *gsi)
{
  gimple *stmt = gsi_stmt (*gsi);
  tree fndecl = gimple_call_fndecl (stmt);
  gcc_checking_assert (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD);
  enum rs6000_gen_builtins fn_code
    = (enum rs6000_gen_builtins) DECL_MD_FUNCTION_CODE (fndecl);
  tree arg0, arg1, lhs, temp;
  enum tree_code bcode;
  gimple *g;

  /* For an unresolved overloaded builtin, return early here since there
     is no builtin info for it and we are unable to fold it.  */
  if (fn_code > RS6000_OVLD_NONE)
    return false;

  size_t uns_fncode = (size_t) fn_code;
  enum insn_code icode = rs6000_builtin_info[uns_fncode].icode;
  const char *fn_name1 = rs6000_builtin_info[uns_fncode].bifname;
  const char *fn_name2 = (icode != CODE_FOR_nothing)
			  ? get_insn_name ((int) icode)
			  : "nothing";

  if (TARGET_DEBUG_BUILTIN)
      fprintf (stderr, "rs6000_gimple_fold_builtin %d %s %s\n",
	       fn_code, fn_name1, fn_name2);

  /* Prevent gimple folding for code that does not have a LHS, unless it is
     allowed per the rs6000_builtin_valid_without_lhs helper function.  */
  if (!gimple_call_lhs (stmt)
      && !rs6000_builtin_valid_without_lhs (fn_code, fndecl))
    return false;

  /* Don't fold invalid builtins, let rs6000_expand_builtin diagnose it.  */
  if (!rs6000_builtin_is_supported (fn_code))
    return false;

  if (rs6000_gimple_fold_mma_builtin (gsi, fn_code))
    return true;

  switch (fn_code)
    {
    /* Flavors of vec_add.  We deliberately don't expand
       RS6000_BIF_VADDUQM as it gets lowered from V1TImode to
       TImode, resulting in much poorer code generation.  */
    case RS6000_BIF_VADDUBM:
    case RS6000_BIF_VADDUHM:
    case RS6000_BIF_VADDUWM:
    case RS6000_BIF_VADDUDM:
    case RS6000_BIF_VADDFP:
    case RS6000_BIF_XVADDDP:
    case RS6000_BIF_XVADDSP:
      bcode = PLUS_EXPR;
    do_binary:
      arg0 = gimple_call_arg (stmt, 0);
      arg1 = gimple_call_arg (stmt, 1);
      lhs = gimple_call_lhs (stmt);
      if (INTEGRAL_TYPE_P (TREE_TYPE (TREE_TYPE (lhs)))
	  && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (TREE_TYPE (lhs))))
	{
	  /* Ensure the binary operation is performed in a type
	     that wraps if it is integral type.  */
	  gimple_seq stmts = NULL;
	  tree type = unsigned_type_for (TREE_TYPE (lhs));
	  tree uarg0 = gimple_build (&stmts, VIEW_CONVERT_EXPR,
				     type, arg0);
	  tree uarg1 = gimple_build (&stmts, VIEW_CONVERT_EXPR,
				     type, arg1);
	  tree res = gimple_build (&stmts, gimple_location (stmt), bcode,
				   type, uarg0, uarg1);
	  gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
	  g = gimple_build_assign (lhs, VIEW_CONVERT_EXPR,
				   build1 (VIEW_CONVERT_EXPR,
					   TREE_TYPE (lhs), res));
	  gsi_replace (gsi, g, true);
	  return true;
	}
      g = gimple_build_assign (lhs, bcode, arg0, arg1);
      gimple_set_location (g, gimple_location (stmt));
      gsi_replace (gsi, g, true);
      return true;
    /* Flavors of vec_sub.  We deliberately don't expand
       RS6000_BIF_VSUBUQM. */
    case RS6000_BIF_VSUBUBM:
    case RS6000_BIF_VSUBUHM:
    case RS6000_BIF_VSUBUWM:
    case RS6000_BIF_VSUBUDM:
    case RS6000_BIF_VSUBFP:
    case RS6000_BIF_XVSUBDP:
    case RS6000_BIF_XVSUBSP:
      bcode = MINUS_EXPR;
      goto do_binary;
    case RS6000_BIF_XVMULSP:
    case RS6000_BIF_XVMULDP:
      arg0 = gimple_call_arg (stmt, 0);
      arg1 = gimple_call_arg (stmt, 1);
      lhs = gimple_call_lhs (stmt);
      g = gimple_build_assign (lhs, MULT_EXPR, arg0, arg1);
      gimple_set_location (g, gimple_location (stmt));
      gsi_replace (gsi, g, true);
      return true;
    /* Even element flavors of vec_mul (signed). */
    case RS6000_BIF_VMULESB:
    case RS6000_BIF_VMULESH:
    case RS6000_BIF_VMULESW:
    /* Even element flavors of vec_mul (unsigned).  */
    case RS6000_BIF_VMULEUB:
    case RS6000_BIF_VMULEUH:
    case RS6000_BIF_VMULEUW:
      arg0 = gimple_call_arg (stmt, 0);
      arg1 = gimple_call_arg (stmt, 1);
      lhs = gimple_call_lhs (stmt);
      g = gimple_build_assign (lhs, VEC_WIDEN_MULT_EVEN_EXPR, arg0, arg1);
      gimple_set_location (g, gimple_location (stmt));
      gsi_replace (gsi, g, true);
      return true;
    /* Odd element flavors of vec_mul (signed).  */
    case RS6000_BIF_VMULOSB:
    case RS6000_BIF_VMULOSH:
    case RS6000_BIF_VMULOSW:
    /* Odd element flavors of vec_mul (unsigned). */
    case RS6000_BIF_VMULOUB:
    case RS6000_BIF_VMULOUH:
    case RS6000_BIF_VMULOUW:
      arg0 = gimple_call_arg (stmt, 0);
      arg1 = gimple_call_arg (stmt, 1);
      lhs = gimple_call_lhs (stmt);
      g = gimple_build_assign (lhs, VEC_WIDEN_MULT_ODD_EXPR, arg0, arg1);
      gimple_set_location (g, gimple_location (stmt));
      gsi_replace (gsi, g, true);
      return true;
    /* Flavors of vec_div (Integer).  */
    case RS6000_BIF_DIV_V2DI:
    case RS6000_BIF_UDIV_V2DI:
      arg0 = gimple_call_arg (stmt, 0);
      arg1 = gimple_call_arg (stmt, 1);
      lhs = gimple_call_lhs (stmt);
      g = gimple_build_assign (lhs, TRUNC_DIV_EXPR, arg0, arg1);
      gimple_set_location (g, gimple_location (stmt));
      gsi_replace (gsi, g, true);
      return true;
    /* Flavors of vec_div (Float).  */
    case RS6000_BIF_XVDIVSP:
    case RS6000_BIF_XVDIVDP:
      arg0 = gimple_call_arg (stmt, 0);
      arg1 = gimple_call_arg (stmt, 1);
      lhs = gimple_call_lhs (stmt);
      g = gimple_build_assign (lhs, RDIV_EXPR, arg0, arg1);
      gimple_set_location (g, gimple_location (stmt));
      gsi_replace (gsi, g, true);
      return true;
    /* Flavors of vec_and.  */
    case RS6000_BIF_VAND_V16QI_UNS:
    case RS6000_BIF_VAND_V16QI:
    case RS6000_BIF_VAND_V8HI_UNS:
    case RS6000_BIF_VAND_V8HI:
    case RS6000_BIF_VAND_V4SI_UNS:
    case RS6000_BIF_VAND_V4SI:
    case RS6000_BIF_VAND_V2DI_UNS:
    case RS6000_BIF_VAND_V2DI:
    case RS6000_BIF_VAND_V4SF:
    case RS6000_BIF_VAND_V2DF:
      arg0 = gimple_call_arg (stmt, 0);
      arg1 = gimple_call_arg (stmt, 1);
      lhs = gimple_call_lhs (stmt);
      g = gimple_build_assign (lhs, BIT_AND_EXPR, arg0, arg1);
      gimple_set_location (g, gimple_location (stmt));
      gsi_replace (gsi, g, true);
      return true;
    /* Flavors of vec_andc.  */
    case RS6000_BIF_VANDC_V16QI_UNS:
    case RS6000_BIF_VANDC_V16QI:
    case RS6000_BIF_VANDC_V8HI_UNS:
    case RS6000_BIF_VANDC_V8HI:
    case RS6000_BIF_VANDC_V4SI_UNS:
    case RS6000_BIF_VANDC_V4SI:
    case RS6000_BIF_VANDC_V2DI_UNS:
    case RS6000_BIF_VANDC_V2DI:
    case RS6000_BIF_VANDC_V4SF:
    case RS6000_BIF_VANDC_V2DF:
      arg0 = gimple_call_arg (stmt, 0);
      arg1 = gimple_call_arg (stmt, 1);
      lhs = gimple_call_lhs (stmt);
      temp = create_tmp_reg_or_ssa_name (TREE_TYPE (arg1));
      g = gimple_build_assign (temp, BIT_NOT_EXPR, arg1);
      gimple_set_location (g, gimple_location (stmt));
      gsi_insert_before (gsi, g, GSI_SAME_STMT);
      g = gimple_build_assign (lhs, BIT_AND_EXPR, arg0, temp);
      gimple_set_location (g, gimple_location (stmt));
      gsi_replace (gsi, g, true);
      return true;
    /* Flavors of vec_nand.  */
    case RS6000_BIF_NAND_V16QI_UNS:
    case RS6000_BIF_NAND_V16QI:
    case RS6000_BIF_NAND_V8HI_UNS:
    case RS6000_BIF_NAND_V8HI:
    case RS6000_BIF_NAND_V4SI_UNS:
    case RS6000_BIF_NAND_V4SI:
    case RS6000_BIF_NAND_V2DI_UNS:
    case RS6000_BIF_NAND_V2DI:
    case RS6000_BIF_NAND_V4SF:
    case RS6000_BIF_NAND_V2DF:
      arg0 = gimple_call_arg (stmt, 0);
      arg1 = gimple_call_arg (stmt, 1);
      lhs = gimple_call_lhs (stmt);
      temp = create_tmp_reg_or_ssa_name (TREE_TYPE (arg1));
      g = gimple_build_assign (temp, BIT_AND_EXPR, arg0, arg1);
      gimple_set_location (g, gimple_location (stmt));
      gsi_insert_before (gsi, g, GSI_SAME_STMT);
      g = gimple_build_assign (lhs, BIT_NOT_EXPR, temp);
      gimple_set_location (g, gimple_location (stmt));
      gsi_replace (gsi, g, true);
      return true;
    /* Flavors of vec_or.  */
    case RS6000_BIF_VOR_V16QI_UNS:
    case RS6000_BIF_VOR_V16QI:
    case RS6000_BIF_VOR_V8HI_UNS:
    case RS6000_BIF_VOR_V8HI:
    case RS6000_BIF_VOR_V4SI_UNS:
    case RS6000_BIF_VOR_V4SI:
    case RS6000_BIF_VOR_V2DI_UNS:
    case RS6000_BIF_VOR_V2DI:
    case RS6000_BIF_VOR_V4SF:
    case RS6000_BIF_VOR_V2DF:
      arg0 = gimple_call_arg (stmt, 0);
      arg1 = gimple_call_arg (stmt, 1);
      lhs = gimple_call_lhs (stmt);
      g = gimple_build_assign (lhs, BIT_IOR_EXPR, arg0, arg1);
      gimple_set_location (g, gimple_location (stmt));
      gsi_replace (gsi, g, true);
      return true;
    /* flavors of vec_orc.  */
    case RS6000_BIF_ORC_V16QI_UNS:
    case RS6000_BIF_ORC_V16QI:
    case RS6000_BIF_ORC_V8HI_UNS:
    case RS6000_BIF_ORC_V8HI:
    case RS6000_BIF_ORC_V4SI_UNS:
    case RS6000_BIF_ORC_V4SI:
    case RS6000_BIF_ORC_V2DI_UNS:
    case RS6000_BIF_ORC_V2DI:
    case RS6000_BIF_ORC_V4SF:
    case RS6000_BIF_ORC_V2DF:
      arg0 = gimple_call_arg (stmt, 0);
      arg1 = gimple_call_arg (stmt, 1);
      lhs = gimple_call_lhs (stmt);
      temp = create_tmp_reg_or_ssa_name (TREE_TYPE (arg1));
      g = gimple_build_assign (temp, BIT_NOT_EXPR, arg1);
      gimple_set_location (g, gimple_location (stmt));
      gsi_insert_before (gsi, g, GSI_SAME_STMT);
      g = gimple_build_assign (lhs, BIT_IOR_EXPR, arg0, temp);
      gimple_set_location (g, gimple_location (stmt));
      gsi_replace (gsi, g, true);
      return true;
    /* Flavors of vec_xor.  */
    case RS6000_BIF_VXOR_V16QI_UNS:
    case RS6000_BIF_VXOR_V16QI:
    case RS6000_BIF_VXOR_V8HI_UNS:
    case RS6000_BIF_VXOR_V8HI:
    case RS6000_BIF_VXOR_V4SI_UNS:
    case RS6000_BIF_VXOR_V4SI:
    case RS6000_BIF_VXOR_V2DI_UNS:
    case RS6000_BIF_VXOR_V2DI:
    case RS6000_BIF_VXOR_V4SF:
    case RS6000_BIF_VXOR_V2DF:
      arg0 = gimple_call_arg (stmt, 0);
      arg1 = gimple_call_arg (stmt, 1);
      lhs = gimple_call_lhs (stmt);
      g = gimple_build_assign (lhs, BIT_XOR_EXPR, arg0, arg1);
      gimple_set_location (g, gimple_location (stmt));
      gsi_replace (gsi, g, true);
      return true;
    /* Flavors of vec_nor.  */
    case RS6000_BIF_VNOR_V16QI_UNS:
    case RS6000_BIF_VNOR_V16QI:
    case RS6000_BIF_VNOR_V8HI_UNS:
    case RS6000_BIF_VNOR_V8HI:
    case RS6000_BIF_VNOR_V4SI_UNS:
    case RS6000_BIF_VNOR_V4SI:
    case RS6000_BIF_VNOR_V2DI_UNS:
    case RS6000_BIF_VNOR_V2DI:
    case RS6000_BIF_VNOR_V4SF:
    case RS6000_BIF_VNOR_V2DF:
      arg0 = gimple_call_arg (stmt, 0);
      arg1 = gimple_call_arg (stmt, 1);
      lhs = gimple_call_lhs (stmt);
      temp = create_tmp_reg_or_ssa_name (TREE_TYPE (arg1));
      g = gimple_build_assign (temp, BIT_IOR_EXPR, arg0, arg1);
      gimple_set_location (g, gimple_location (stmt));
      gsi_insert_before (gsi, g, GSI_SAME_STMT);
      g = gimple_build_assign (lhs, BIT_NOT_EXPR, temp);
      gimple_set_location (g, gimple_location (stmt));
      gsi_replace (gsi, g, true);
      return true;
    /* flavors of vec_abs.  */
    case RS6000_BIF_ABS_V16QI:
    case RS6000_BIF_ABS_V8HI:
    case RS6000_BIF_ABS_V4SI:
    case RS6000_BIF_ABS_V4SF:
    case RS6000_BIF_ABS_V2DI:
    case RS6000_BIF_XVABSDP:
    case RS6000_BIF_XVABSSP:
      arg0 = gimple_call_arg (stmt, 0);
      if (INTEGRAL_TYPE_P (TREE_TYPE (TREE_TYPE (arg0)))
	  && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (TREE_TYPE (arg0))))
	return false;
      lhs = gimple_call_lhs (stmt);
      g = gimple_build_assign (lhs, ABS_EXPR, arg0);
      gimple_set_location (g, gimple_location (stmt));
      gsi_replace (gsi, g, true);
      return true;
    /* flavors of vec_min.  */
    case RS6000_BIF_XVMINDP:
    case RS6000_BIF_XVMINSP:
    case RS6000_BIF_VMINFP:
      {
	lhs = gimple_call_lhs (stmt);
	tree type = TREE_TYPE (lhs);
	if (HONOR_NANS (type))
	  return false;
	gcc_fallthrough ();
      }
    case RS6000_BIF_VMINSD:
    case RS6000_BIF_VMINUD:
    case RS6000_BIF_VMINSB:
    case RS6000_BIF_VMINSH:
    case RS6000_BIF_VMINSW:
    case RS6000_BIF_VMINUB:
    case RS6000_BIF_VMINUH:
    case RS6000_BIF_VMINUW:
      arg0 = gimple_call_arg (stmt, 0);
      arg1 = gimple_call_arg (stmt, 1);
      lhs = gimple_call_lhs (stmt);
      g = gimple_build_assign (lhs, MIN_EXPR, arg0, arg1);
      gimple_set_location (g, gimple_location (stmt));
      gsi_replace (gsi, g, true);
      return true;
    /* flavors of vec_max.  */
    case RS6000_BIF_XVMAXDP:
    case RS6000_BIF_XVMAXSP:
    case RS6000_BIF_VMAXFP:
      {
	lhs = gimple_call_lhs (stmt);
	tree type = TREE_TYPE (lhs);
	if (HONOR_NANS (type))
	  return false;
	gcc_fallthrough ();
      }
    case RS6000_BIF_VMAXSD:
    case RS6000_BIF_VMAXUD:
    case RS6000_BIF_VMAXSB:
    case RS6000_BIF_VMAXSH:
    case RS6000_BIF_VMAXSW:
    case RS6000_BIF_VMAXUB:
    case RS6000_BIF_VMAXUH:
    case RS6000_BIF_VMAXUW:
      arg0 = gimple_call_arg (stmt, 0);
      arg1 = gimple_call_arg (stmt, 1);
      lhs = gimple_call_lhs (stmt);
      g = gimple_build_assign (lhs, MAX_EXPR, arg0, arg1);
      gimple_set_location (g, gimple_location (stmt));
      gsi_replace (gsi, g, true);
      return true;
    /* Flavors of vec_eqv.  */
    case RS6000_BIF_EQV_V16QI:
    case RS6000_BIF_EQV_V8HI:
    case RS6000_BIF_EQV_V4SI:
    case RS6000_BIF_EQV_V4SF:
    case RS6000_BIF_EQV_V2DF:
    case RS6000_BIF_EQV_V2DI:
      arg0 = gimple_call_arg (stmt, 0);
      arg1 = gimple_call_arg (stmt, 1);
      lhs = gimple_call_lhs (stmt);
      temp = create_tmp_reg_or_ssa_name (TREE_TYPE (arg1));
      g = gimple_build_assign (temp, BIT_XOR_EXPR, arg0, arg1);
      gimple_set_location (g, gimple_location (stmt));
      gsi_insert_before (gsi, g, GSI_SAME_STMT);
      g = gimple_build_assign (lhs, BIT_NOT_EXPR, temp);
      gimple_set_location (g, gimple_location (stmt));
      gsi_replace (gsi, g, true);
      return true;
    /* Flavors of vec_rotate_left.  */
    case RS6000_BIF_VRLB:
    case RS6000_BIF_VRLH:
    case RS6000_BIF_VRLW:
    case RS6000_BIF_VRLD:
      arg0 = gimple_call_arg (stmt, 0);
      arg1 = gimple_call_arg (stmt, 1);
      lhs = gimple_call_lhs (stmt);
      g = gimple_build_assign (lhs, LROTATE_EXPR, arg0, arg1);
      gimple_set_location (g, gimple_location (stmt));
      gsi_replace (gsi, g, true);
      return true;
  /* Flavors of vector shift right algebraic.
     vec_sra{b,h,w} -> vsra{b,h,w}.  */
    case RS6000_BIF_VSRAB:
    case RS6000_BIF_VSRAH:
    case RS6000_BIF_VSRAW:
    case RS6000_BIF_VSRAD:
      {
	arg0 = gimple_call_arg (stmt, 0);
	arg1 = gimple_call_arg (stmt, 1);
	lhs = gimple_call_lhs (stmt);
	tree arg1_type = TREE_TYPE (arg1);
	tree unsigned_arg1_type = unsigned_type_for (TREE_TYPE (arg1));
	tree unsigned_element_type = unsigned_type_for (TREE_TYPE (arg1_type));
	location_t loc = gimple_location (stmt);
	/* Force arg1 into the range valid matching the arg0 type.  */
	/* Build a vector consisting of the max valid bit-size values.  */
	int n_elts = VECTOR_CST_NELTS (arg1);
	tree element_size = build_int_cst (unsigned_element_type,
					   128 / n_elts);
	tree_vector_builder elts (unsigned_arg1_type, n_elts, 1);
	for (int i = 0; i < n_elts; i++)
	  elts.safe_push (element_size);
	tree modulo_tree = elts.build ();
	/* Modulo the provided shift value against that vector.  */
	gimple_seq stmts = NULL;
	tree unsigned_arg1 = gimple_build (&stmts, VIEW_CONVERT_EXPR,
					   unsigned_arg1_type, arg1);
	tree new_arg1 = gimple_build (&stmts, loc, TRUNC_MOD_EXPR,
				      unsigned_arg1_type, unsigned_arg1,
				      modulo_tree);
	gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
	/* And finally, do the shift.  */
	g = gimple_build_assign (lhs, RSHIFT_EXPR, arg0, new_arg1);
	gimple_set_location (g, loc);
	gsi_replace (gsi, g, true);
	return true;
      }
   /* Flavors of vector shift left.
      builtin_altivec_vsl{b,h,w} -> vsl{b,h,w}.  */
    case RS6000_BIF_VSLB:
    case RS6000_BIF_VSLH:
    case RS6000_BIF_VSLW:
    case RS6000_BIF_VSLD:
      {
	location_t loc;
	gimple_seq stmts = NULL;
	arg0 = gimple_call_arg (stmt, 0);
	tree arg0_type = TREE_TYPE (arg0);
	if (INTEGRAL_TYPE_P (TREE_TYPE (arg0_type))
	    && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0_type)))
	  return false;
	arg1 = gimple_call_arg (stmt, 1);
	tree arg1_type = TREE_TYPE (arg1);
	tree unsigned_arg1_type = unsigned_type_for (TREE_TYPE (arg1));
	tree unsigned_element_type = unsigned_type_for (TREE_TYPE (arg1_type));
	loc = gimple_location (stmt);
	lhs = gimple_call_lhs (stmt);
	/* Force arg1 into the range valid matching the arg0 type.  */
	/* Build a vector consisting of the max valid bit-size values.  */
	int n_elts = VECTOR_CST_NELTS (arg1);
	int tree_size_in_bits = TREE_INT_CST_LOW (size_in_bytes (arg1_type))
				* BITS_PER_UNIT;
	tree element_size = build_int_cst (unsigned_element_type,
					   tree_size_in_bits / n_elts);
	tree_vector_builder elts (unsigned_type_for (arg1_type), n_elts, 1);
	for (int i = 0; i < n_elts; i++)
	  elts.safe_push (element_size);
	tree modulo_tree = elts.build ();
	/* Modulo the provided shift value against that vector.  */
	tree unsigned_arg1 = gimple_build (&stmts, VIEW_CONVERT_EXPR,
					   unsigned_arg1_type, arg1);
	tree new_arg1 = gimple_build (&stmts, loc, TRUNC_MOD_EXPR,
				      unsigned_arg1_type, unsigned_arg1,
				      modulo_tree);
	gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
	/* And finally, do the shift.  */
	g = gimple_build_assign (lhs, LSHIFT_EXPR, arg0, new_arg1);
	gimple_set_location (g, gimple_location (stmt));
	gsi_replace (gsi, g, true);
	return true;
      }
    /* Flavors of vector shift right.  */
    case RS6000_BIF_VSRB:
    case RS6000_BIF_VSRH:
    case RS6000_BIF_VSRW:
    case RS6000_BIF_VSRD:
      {
	arg0 = gimple_call_arg (stmt, 0);
	arg1 = gimple_call_arg (stmt, 1);
	lhs = gimple_call_lhs (stmt);
	tree arg1_type = TREE_TYPE (arg1);
	tree unsigned_arg1_type = unsigned_type_for (TREE_TYPE (arg1));
	tree unsigned_element_type = unsigned_type_for (TREE_TYPE (arg1_type));
	location_t loc = gimple_location (stmt);
	gimple_seq stmts = NULL;
	/* Convert arg0 to unsigned.  */
	tree arg0_unsigned
	  = gimple_build (&stmts, VIEW_CONVERT_EXPR,
			  unsigned_type_for (TREE_TYPE (arg0)), arg0);
	/* Force arg1 into the range valid matching the arg0 type.  */
	/* Build a vector consisting of the max valid bit-size values.  */
	int n_elts = VECTOR_CST_NELTS (arg1);
	tree element_size = build_int_cst (unsigned_element_type,
					   128 / n_elts);
	tree_vector_builder elts (unsigned_arg1_type, n_elts, 1);
	for (int i = 0; i < n_elts; i++)
	  elts.safe_push (element_size);
	tree modulo_tree = elts.build ();
	/* Modulo the provided shift value against that vector.  */
	tree unsigned_arg1 = gimple_build (&stmts, VIEW_CONVERT_EXPR,
					   unsigned_arg1_type, arg1);
	tree new_arg1 = gimple_build (&stmts, loc, TRUNC_MOD_EXPR,
				      unsigned_arg1_type, unsigned_arg1,
				      modulo_tree);
	/* Do the shift.  */
	tree res
	  = gimple_build (&stmts, RSHIFT_EXPR,
			  TREE_TYPE (arg0_unsigned), arg0_unsigned, new_arg1);
	/* Convert result back to the lhs type.  */
	res = gimple_build (&stmts, VIEW_CONVERT_EXPR, TREE_TYPE (lhs), res);
	gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
	replace_call_with_value (gsi, res);
	return true;
      }
    /* Vector loads.  */
    case RS6000_BIF_LVX_V16QI:
    case RS6000_BIF_LVX_V8HI:
    case RS6000_BIF_LVX_V4SI:
    case RS6000_BIF_LVX_V4SF:
    case RS6000_BIF_LVX_V2DI:
    case RS6000_BIF_LVX_V2DF:
    case RS6000_BIF_LVX_V1TI:
      {
	arg0 = gimple_call_arg (stmt, 0);  // offset
	arg1 = gimple_call_arg (stmt, 1);  // address
	lhs = gimple_call_lhs (stmt);
	location_t loc = gimple_location (stmt);
	/* Since arg1 may be cast to a different type, just use ptr_type_node
	   here instead of trying to enforce TBAA on pointer types.  */
	tree arg1_type = ptr_type_node;
	tree lhs_type = TREE_TYPE (lhs);
	/* POINTER_PLUS_EXPR wants the offset to be of type 'sizetype'.  Create
	   the tree using the value from arg0.  The resulting type will match
	   the type of arg1.  */
	gimple_seq stmts = NULL;
	tree temp_offset = gimple_convert (&stmts, loc, sizetype, arg0);
	tree temp_addr = gimple_build (&stmts, loc, POINTER_PLUS_EXPR,
				       arg1_type, arg1, temp_offset);
	/* Mask off any lower bits from the address.  */
	tree aligned_addr = gimple_build (&stmts, loc, BIT_AND_EXPR,
					  arg1_type, temp_addr,
					  build_int_cst (arg1_type, -16));
	gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
	if (!is_gimple_mem_ref_addr (aligned_addr))
	  {
	    tree t = make_ssa_name (TREE_TYPE (aligned_addr));
	    gimple *g = gimple_build_assign (t, aligned_addr);
	    gsi_insert_before (gsi, g, GSI_SAME_STMT);
	    aligned_addr = t;
	  }
	/* Use the build2 helper to set up the mem_ref.  The MEM_REF could also
	   take an offset, but since we've already incorporated the offset
	   above, here we just pass in a zero.  */
	gimple *g
	  = gimple_build_assign (lhs, build2 (MEM_REF, lhs_type, aligned_addr,
					      build_int_cst (arg1_type, 0)));
	gimple_set_location (g, loc);
	gsi_replace (gsi, g, true);
	return true;
      }
    /* Vector stores.  */
    case RS6000_BIF_STVX_V16QI:
    case RS6000_BIF_STVX_V8HI:
    case RS6000_BIF_STVX_V4SI:
    case RS6000_BIF_STVX_V4SF:
    case RS6000_BIF_STVX_V2DI:
    case RS6000_BIF_STVX_V2DF:
      {
	arg0 = gimple_call_arg (stmt, 0); /* Value to be stored.  */
	arg1 = gimple_call_arg (stmt, 1); /* Offset.  */
	tree arg2 = gimple_call_arg (stmt, 2); /* Store-to address.  */
	location_t loc = gimple_location (stmt);
	tree arg0_type = TREE_TYPE (arg0);
	/* Use ptr_type_node (no TBAA) for the arg2_type.
	   FIXME: (Richard)  "A proper fix would be to transition this type as
	   seen from the frontend to GIMPLE, for example in a similar way we
	   do for MEM_REFs by piggy-backing that on an extra argument, a
	   constant zero pointer of the alias pointer type to use (which would
	   also serve as a type indicator of the store itself).  I'd use a
	   target specific internal function for this (not sure if we can have
	   those target specific, but I guess if it's folded away then that's
	   fine) and get away with the overload set."  */
	tree arg2_type = ptr_type_node;
	/* POINTER_PLUS_EXPR wants the offset to be of type 'sizetype'.  Create
	   the tree using the value from arg0.  The resulting type will match
	   the type of arg2.  */
	gimple_seq stmts = NULL;
	tree temp_offset = gimple_convert (&stmts, loc, sizetype, arg1);
	tree temp_addr = gimple_build (&stmts, loc, POINTER_PLUS_EXPR,
				       arg2_type, arg2, temp_offset);
	/* Mask off any lower bits from the address.  */
	tree aligned_addr = gimple_build (&stmts, loc, BIT_AND_EXPR,
					  arg2_type, temp_addr,
					  build_int_cst (arg2_type, -16));
	gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
	if (!is_gimple_mem_ref_addr (aligned_addr))
	  {
	    tree t = make_ssa_name (TREE_TYPE (aligned_addr));
	    gimple *g = gimple_build_assign (t, aligned_addr);
	    gsi_insert_before (gsi, g, GSI_SAME_STMT);
	    aligned_addr = t;
	  }
	/* The desired gimple result should be similar to:
	   MEM[(__vector floatD.1407 *)_1] = vf1D.2697;  */
	gimple *g
	  = gimple_build_assign (build2 (MEM_REF, arg0_type, aligned_addr,
					 build_int_cst (arg2_type, 0)), arg0);
	gimple_set_location (g, loc);
	gsi_replace (gsi, g, true);
	return true;
      }

    /* unaligned Vector loads.  */
    case RS6000_BIF_LXVW4X_V16QI:
    case RS6000_BIF_LXVW4X_V8HI:
    case RS6000_BIF_LXVW4X_V4SF:
    case RS6000_BIF_LXVW4X_V4SI:
    case RS6000_BIF_LXVD2X_V2DF:
    case RS6000_BIF_LXVD2X_V2DI:
      {
	arg0 = gimple_call_arg (stmt, 0);  // offset
	arg1 = gimple_call_arg (stmt, 1);  // address
	lhs = gimple_call_lhs (stmt);
	location_t loc = gimple_location (stmt);
	/* Since arg1 may be cast to a different type, just use ptr_type_node
	   here instead of trying to enforce TBAA on pointer types.  */
	tree arg1_type = ptr_type_node;
	tree lhs_type = TREE_TYPE (lhs);
	/* In GIMPLE the type of the MEM_REF specifies the alignment.  The
	  required alignment (power) is 4 bytes regardless of data type.  */
	tree align_ltype = build_aligned_type (lhs_type, 4);
	/* POINTER_PLUS_EXPR wants the offset to be of type 'sizetype'.  Create
	   the tree using the value from arg0.  The resulting type will match
	   the type of arg1.  */
	gimple_seq stmts = NULL;
	tree temp_offset = gimple_convert (&stmts, loc, sizetype, arg0);
	tree temp_addr = gimple_build (&stmts, loc, POINTER_PLUS_EXPR,
				       arg1_type, arg1, temp_offset);
	gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
	if (!is_gimple_mem_ref_addr (temp_addr))
	  {
	    tree t = make_ssa_name (TREE_TYPE (temp_addr));
	    gimple *g = gimple_build_assign (t, temp_addr);
	    gsi_insert_before (gsi, g, GSI_SAME_STMT);
	    temp_addr = t;
	  }
	/* Use the build2 helper to set up the mem_ref.  The MEM_REF could also
	   take an offset, but since we've already incorporated the offset
	   above, here we just pass in a zero.  */
	gimple *g;
	g = gimple_build_assign (lhs, build2 (MEM_REF, align_ltype, temp_addr,
					      build_int_cst (arg1_type, 0)));
	gimple_set_location (g, loc);
	gsi_replace (gsi, g, true);
	return true;
      }

    /* unaligned Vector stores.  */
    case RS6000_BIF_STXVW4X_V16QI:
    case RS6000_BIF_STXVW4X_V8HI:
    case RS6000_BIF_STXVW4X_V4SF:
    case RS6000_BIF_STXVW4X_V4SI:
    case RS6000_BIF_STXVD2X_V2DF:
    case RS6000_BIF_STXVD2X_V2DI:
      {
	arg0 = gimple_call_arg (stmt, 0); /* Value to be stored.  */
	arg1 = gimple_call_arg (stmt, 1); /* Offset.  */
	tree arg2 = gimple_call_arg (stmt, 2); /* Store-to address.  */
	location_t loc = gimple_location (stmt);
	tree arg0_type = TREE_TYPE (arg0);
	/* Use ptr_type_node (no TBAA) for the arg2_type.  */
	tree arg2_type = ptr_type_node;
	/* In GIMPLE the type of the MEM_REF specifies the alignment.  The
	   required alignment (power) is 4 bytes regardless of data type.  */
	tree align_stype = build_aligned_type (arg0_type, 4);
	/* POINTER_PLUS_EXPR wants the offset to be of type 'sizetype'.  Create
	   the tree using the value from arg1.  */
	gimple_seq stmts = NULL;
	tree temp_offset = gimple_convert (&stmts, loc, sizetype, arg1);
	tree temp_addr = gimple_build (&stmts, loc, POINTER_PLUS_EXPR,
				       arg2_type, arg2, temp_offset);
	gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
	if (!is_gimple_mem_ref_addr (temp_addr))
	  {
	    tree t = make_ssa_name (TREE_TYPE (temp_addr));
	    gimple *g = gimple_build_assign (t, temp_addr);
	    gsi_insert_before (gsi, g, GSI_SAME_STMT);
	    temp_addr = t;
	  }
	gimple *g;
	g = gimple_build_assign (build2 (MEM_REF, align_stype, temp_addr,
					 build_int_cst (arg2_type, 0)), arg0);
	gimple_set_location (g, loc);
	gsi_replace (gsi, g, true);
	return true;
      }

    /* Vector Fused multiply-add (fma).  */
    case RS6000_BIF_VMADDFP:
    case RS6000_BIF_XVMADDDP:
    case RS6000_BIF_XVMADDSP:
    case RS6000_BIF_VMLADDUHM:
      {
	arg0 = gimple_call_arg (stmt, 0);
	arg1 = gimple_call_arg (stmt, 1);
	tree arg2 = gimple_call_arg (stmt, 2);
	lhs = gimple_call_lhs (stmt);
	gcall *g = gimple_build_call_internal (IFN_FMA, 3, arg0, arg1, arg2);
	gimple_call_set_lhs (g, lhs);
	gimple_call_set_nothrow (g, true);
	gimple_set_location (g, gimple_location (stmt));
	gsi_replace (gsi, g, true);
	return true;
      }

    /* Vector compares; EQ, NE, GE, GT, LE.  */
    case RS6000_BIF_VCMPEQUB:
    case RS6000_BIF_VCMPEQUH:
    case RS6000_BIF_VCMPEQUW:
    case RS6000_BIF_VCMPEQUD:
    case RS6000_BIF_VCMPEQUT:
      fold_compare_helper (gsi, EQ_EXPR, stmt);
      return true;

    case RS6000_BIF_VCMPNEB:
    case RS6000_BIF_VCMPNEH:
    case RS6000_BIF_VCMPNEW:
    case RS6000_BIF_VCMPNET:
      fold_compare_helper (gsi, NE_EXPR, stmt);
      return true;

    case RS6000_BIF_CMPGE_16QI:
    case RS6000_BIF_CMPGE_U16QI:
    case RS6000_BIF_CMPGE_8HI:
    case RS6000_BIF_CMPGE_U8HI:
    case RS6000_BIF_CMPGE_4SI:
    case RS6000_BIF_CMPGE_U4SI:
    case RS6000_BIF_CMPGE_2DI:
    case RS6000_BIF_CMPGE_U2DI:
    case RS6000_BIF_CMPGE_1TI:
    case RS6000_BIF_CMPGE_U1TI:
      fold_compare_helper (gsi, GE_EXPR, stmt);
      return true;

    case RS6000_BIF_VCMPGTSB:
    case RS6000_BIF_VCMPGTUB:
    case RS6000_BIF_VCMPGTSH:
    case RS6000_BIF_VCMPGTUH:
    case RS6000_BIF_VCMPGTSW:
    case RS6000_BIF_VCMPGTUW:
    case RS6000_BIF_VCMPGTUD:
    case RS6000_BIF_VCMPGTSD:
    case RS6000_BIF_VCMPGTUT:
    case RS6000_BIF_VCMPGTST:
      fold_compare_helper (gsi, GT_EXPR, stmt);
      return true;

    case RS6000_BIF_CMPLE_16QI:
    case RS6000_BIF_CMPLE_U16QI:
    case RS6000_BIF_CMPLE_8HI:
    case RS6000_BIF_CMPLE_U8HI:
    case RS6000_BIF_CMPLE_4SI:
    case RS6000_BIF_CMPLE_U4SI:
    case RS6000_BIF_CMPLE_2DI:
    case RS6000_BIF_CMPLE_U2DI:
    case RS6000_BIF_CMPLE_1TI:
    case RS6000_BIF_CMPLE_U1TI:
      fold_compare_helper (gsi, LE_EXPR, stmt);
      return true;

    /* flavors of vec_splat_[us]{8,16,32}.  */
    case RS6000_BIF_VSPLTISB:
    case RS6000_BIF_VSPLTISH:
    case RS6000_BIF_VSPLTISW:
      {
	arg0 = gimple_call_arg (stmt, 0);
	lhs = gimple_call_lhs (stmt);

	/* Only fold the vec_splat_*() if the lower bits of arg 0 is a
	   5-bit signed constant in range -16 to +15.  */
	if (TREE_CODE (arg0) != INTEGER_CST
	    || !IN_RANGE (TREE_INT_CST_LOW (arg0), -16, 15))
	  return false;
	gimple_seq stmts = NULL;
	location_t loc = gimple_location (stmt);
	tree splat_value = gimple_convert (&stmts, loc,
					   TREE_TYPE (TREE_TYPE (lhs)), arg0);
	gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
	tree splat_tree = build_vector_from_val (TREE_TYPE (lhs), splat_value);
	g = gimple_build_assign (lhs, splat_tree);
	gimple_set_location (g, gimple_location (stmt));
	gsi_replace (gsi, g, true);
	return true;
      }

    /* Flavors of vec_splat.  */
    /* a = vec_splat (b, 0x3) becomes a = { b[3],b[3],b[3],...};  */
    case RS6000_BIF_VSPLTB:
    case RS6000_BIF_VSPLTH:
    case RS6000_BIF_VSPLTW:
    case RS6000_BIF_XXSPLTD_V2DI:
    case RS6000_BIF_XXSPLTD_V2DF:
      {
	arg0 = gimple_call_arg (stmt, 0); /* input vector.  */
	arg1 = gimple_call_arg (stmt, 1); /* index into arg0.  */
	/* Only fold the vec_splat_*() if arg1 is both a constant value and
	   is a valid index into the arg0 vector.  */
	unsigned int n_elts = VECTOR_CST_NELTS (arg0);
	if (TREE_CODE (arg1) != INTEGER_CST
	    || TREE_INT_CST_LOW (arg1) > (n_elts -1))
	  return false;
	lhs = gimple_call_lhs (stmt);
	tree lhs_type = TREE_TYPE (lhs);
	tree arg0_type = TREE_TYPE (arg0);
	tree splat;
	if (TREE_CODE (arg0) == VECTOR_CST)
	  splat = VECTOR_CST_ELT (arg0, TREE_INT_CST_LOW (arg1));
	else
	  {
	    /* Determine (in bits) the length and start location of the
	       splat value for a call to the tree_vec_extract helper.  */
	    int splat_elem_size = TREE_INT_CST_LOW (size_in_bytes (arg0_type))
				  * BITS_PER_UNIT / n_elts;
	    int splat_start_bit = TREE_INT_CST_LOW (arg1) * splat_elem_size;
	    tree len = build_int_cst (bitsizetype, splat_elem_size);
	    tree start = build_int_cst (bitsizetype, splat_start_bit);
	    splat = tree_vec_extract (gsi, TREE_TYPE (lhs_type), arg0,
				      len, start);
	  }
	/* And finally, build the new vector.  */
	tree splat_tree = build_vector_from_val (lhs_type, splat);
	g = gimple_build_assign (lhs, splat_tree);
	gimple_set_location (g, gimple_location (stmt));
	gsi_replace (gsi, g, true);
	return true;
      }

    /* vec_mergel (integrals).  */
    case RS6000_BIF_VMRGLH:
    case RS6000_BIF_VMRGLW:
    case RS6000_BIF_XXMRGLW_4SI:
    case RS6000_BIF_VMRGLB:
    case RS6000_BIF_VEC_MERGEL_V2DI:
    case RS6000_BIF_XXMRGLW_4SF:
    case RS6000_BIF_VEC_MERGEL_V2DF:
      fold_mergehl_helper (gsi, stmt, 1);
      return true;
    /* vec_mergeh (integrals).  */
    case RS6000_BIF_VMRGHH:
    case RS6000_BIF_VMRGHW:
    case RS6000_BIF_XXMRGHW_4SI:
    case RS6000_BIF_VMRGHB:
    case RS6000_BIF_VEC_MERGEH_V2DI:
    case RS6000_BIF_XXMRGHW_4SF:
    case RS6000_BIF_VEC_MERGEH_V2DF:
      fold_mergehl_helper (gsi, stmt, 0);
      return true;

    /* Flavors of vec_mergee.  */
    case RS6000_BIF_VMRGEW_V4SI:
    case RS6000_BIF_VMRGEW_V2DI:
    case RS6000_BIF_VMRGEW_V4SF:
    case RS6000_BIF_VMRGEW_V2DF:
      fold_mergeeo_helper (gsi, stmt, 0);
      return true;
    /* Flavors of vec_mergeo.  */
    case RS6000_BIF_VMRGOW_V4SI:
    case RS6000_BIF_VMRGOW_V2DI:
    case RS6000_BIF_VMRGOW_V4SF:
    case RS6000_BIF_VMRGOW_V2DF:
      fold_mergeeo_helper (gsi, stmt, 1);
      return true;

    /* d = vec_pack (a, b) */
    case RS6000_BIF_VPKUDUM:
    case RS6000_BIF_VPKUHUM:
    case RS6000_BIF_VPKUWUM:
      {
	arg0 = gimple_call_arg (stmt, 0);
	arg1 = gimple_call_arg (stmt, 1);
	lhs = gimple_call_lhs (stmt);
	gimple *g = gimple_build_assign (lhs, VEC_PACK_TRUNC_EXPR, arg0, arg1);
	gimple_set_location (g, gimple_location (stmt));
	gsi_replace (gsi, g, true);
	return true;
      }

    /* d = vec_unpackh (a) */
    /* Note that the UNPACK_{HI,LO}_EXPR used in the gimple_build_assign call
       in this code is sensitive to endian-ness, and needs to be inverted to
       handle both LE and BE targets.  */
    case RS6000_BIF_VUPKHSB:
    case RS6000_BIF_VUPKHSH:
    case RS6000_BIF_VUPKHSW:
      {
	arg0 = gimple_call_arg (stmt, 0);
	lhs = gimple_call_lhs (stmt);
	if (BYTES_BIG_ENDIAN)
	  g = gimple_build_assign (lhs, VEC_UNPACK_HI_EXPR, arg0);
	else
	  g = gimple_build_assign (lhs, VEC_UNPACK_LO_EXPR, arg0);
	gimple_set_location (g, gimple_location (stmt));
	gsi_replace (gsi, g, true);
	return true;
      }
    /* d = vec_unpackl (a) */
    case RS6000_BIF_VUPKLSB:
    case RS6000_BIF_VUPKLSH:
    case RS6000_BIF_VUPKLSW:
      {
	arg0 = gimple_call_arg (stmt, 0);
	lhs = gimple_call_lhs (stmt);
	if (BYTES_BIG_ENDIAN)
	  g = gimple_build_assign (lhs, VEC_UNPACK_LO_EXPR, arg0);
	else
	  g = gimple_build_assign (lhs, VEC_UNPACK_HI_EXPR, arg0);
	gimple_set_location (g, gimple_location (stmt));
	gsi_replace (gsi, g, true);
	return true;
      }
    /* There is no gimple type corresponding with pixel, so just return.  */
    case RS6000_BIF_VUPKHPX:
    case RS6000_BIF_VUPKLPX:
      return false;

    /* vec_perm.  */
    case RS6000_BIF_VPERM_16QI:
    case RS6000_BIF_VPERM_8HI:
    case RS6000_BIF_VPERM_4SI:
    case RS6000_BIF_VPERM_2DI:
    case RS6000_BIF_VPERM_4SF:
    case RS6000_BIF_VPERM_2DF:
    case RS6000_BIF_VPERM_16QI_UNS:
    case RS6000_BIF_VPERM_8HI_UNS:
    case RS6000_BIF_VPERM_4SI_UNS:
    case RS6000_BIF_VPERM_2DI_UNS:
      {
	arg0 = gimple_call_arg (stmt, 0);
	arg1 = gimple_call_arg (stmt, 1);
	tree permute = gimple_call_arg (stmt, 2);
	lhs = gimple_call_lhs (stmt);
	location_t loc = gimple_location (stmt);
	gimple_seq stmts = NULL;
	// convert arg0 and arg1 to match the type of the permute
	// for the VEC_PERM_EXPR operation.
	tree permute_type = (TREE_TYPE (permute));
	tree arg0_ptype = gimple_build (&stmts, loc, VIEW_CONVERT_EXPR,
					permute_type, arg0);
	tree arg1_ptype = gimple_build (&stmts, loc, VIEW_CONVERT_EXPR,
					permute_type, arg1);
	tree lhs_ptype = gimple_build (&stmts, loc, VEC_PERM_EXPR,
				      permute_type, arg0_ptype, arg1_ptype,
				      permute);
	// Convert the result back to the desired lhs type upon completion.
	tree temp = gimple_build (&stmts, loc, VIEW_CONVERT_EXPR,
				  TREE_TYPE (lhs), lhs_ptype);
	gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
	g = gimple_build_assign (lhs, temp);
	gimple_set_location (g, loc);
	gsi_replace (gsi, g, true);
	return true;
      }

    default:
      if (TARGET_DEBUG_BUILTIN)
	fprintf (stderr, "gimple builtin intrinsic not matched:%d %s %s\n",
		 fn_code, fn_name1, fn_name2);
      break;
    }

  return false;
}

/* **** Expansion support ****  */

static rtx
altivec_expand_predicate_builtin (enum insn_code icode, tree exp, rtx target)
{
  rtx pat, scratch;
  tree cr6_form = CALL_EXPR_ARG (exp, 0);
  tree arg0 = CALL_EXPR_ARG (exp, 1);
  tree arg1 = CALL_EXPR_ARG (exp, 2);
  rtx op0 = expand_normal (arg0);
  rtx op1 = expand_normal (arg1);
  machine_mode tmode = SImode;
  machine_mode mode0 = insn_data[icode].operand[1].mode;
  machine_mode mode1 = insn_data[icode].operand[2].mode;
  int cr6_form_int;

  if (TREE_CODE (cr6_form) != INTEGER_CST)
    {
      error ("argument 1 of %qs must be a constant",
	     "__builtin_altivec_predicate");
      return const0_rtx;
    }
  else
    cr6_form_int = TREE_INT_CST_LOW (cr6_form);

  gcc_assert (mode0 == mode1);

  /* If we have invalid arguments, bail out before generating bad rtl.  */
  if (arg0 == error_mark_node || arg1 == error_mark_node)
    return const0_rtx;

  if (target == 0
      || GET_MODE (target) != tmode
      || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
    target = gen_reg_rtx (tmode);

  if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
    op0 = copy_to_mode_reg (mode0, op0);
  if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
    op1 = copy_to_mode_reg (mode1, op1);

  /* Note that for many of the relevant operations (e.g. cmpne or
     cmpeq) with float or double operands, it makes more sense for the
     mode of the allocated scratch register to select a vector of
     integer.  But the choice to copy the mode of operand 0 was made
     long ago and there are no plans to change it.  */
  scratch = gen_reg_rtx (mode0);

  pat = GEN_FCN (icode) (scratch, op0, op1);
  if (! pat)
    return 0;
  emit_insn (pat);

  /* The vec_any* and vec_all* predicates use the same opcodes for two
     different operations, but the bits in CR6 will be different
     depending on what information we want.  So we have to play tricks
     with CR6 to get the right bits out.

     If you think this is disgusting, look at the specs for the
     AltiVec predicates.  */

  switch (cr6_form_int)
    {
    case 0:
      emit_insn (gen_cr6_test_for_zero (target));
      break;
    case 1:
      emit_insn (gen_cr6_test_for_zero_reverse (target));
      break;
    case 2:
      emit_insn (gen_cr6_test_for_lt (target));
      break;
    case 3:
      emit_insn (gen_cr6_test_for_lt_reverse (target));
      break;
    default:
      error ("argument 1 of %qs is out of range",
	     "__builtin_altivec_predicate");
      break;
    }

  return target;
}

/* Expand vec_init builtin.  */
static rtx
altivec_expand_vec_init_builtin (tree type, tree exp, rtx target)
{
  machine_mode tmode = TYPE_MODE (type);
  machine_mode inner_mode = GET_MODE_INNER (tmode);
  int i, n_elt = GET_MODE_NUNITS (tmode);

  gcc_assert (VECTOR_MODE_P (tmode));
  gcc_assert (n_elt == call_expr_nargs (exp));

  if (!target || !register_operand (target, tmode))
    target = gen_reg_rtx (tmode);

  /* If we have a vector compromised of a single element, such as V1TImode, do
     the initialization directly.  */
  if (n_elt == 1 && GET_MODE_SIZE (tmode) == GET_MODE_SIZE (inner_mode))
    {
      rtx x = expand_normal (CALL_EXPR_ARG (exp, 0));
      emit_move_insn (target, gen_lowpart (tmode, x));
    }
  else
    {
      rtvec v = rtvec_alloc (n_elt);

      for (i = 0; i < n_elt; ++i)
	{
	  rtx x = expand_normal (CALL_EXPR_ARG (exp, i));
	  RTVEC_ELT (v, i) = gen_lowpart (inner_mode, x);
	}

      rs6000_expand_vector_init (target, gen_rtx_PARALLEL (tmode, v));
    }

  return target;
}

/* Return the integer constant in ARG.  Constrain it to be in the range
   of the subparts of VEC_TYPE; issue an error if not.  */

static int
get_element_number (tree vec_type, tree arg)
{
  unsigned HOST_WIDE_INT elt, max = TYPE_VECTOR_SUBPARTS (vec_type) - 1;

  if (!tree_fits_uhwi_p (arg)
      || (elt = tree_to_uhwi (arg), elt > max))
    {
      error ("selector must be an integer constant in the range [0, %wi]", max);
      return 0;
    }

  return elt;
}

/* Expand vec_set builtin.  */
static rtx
altivec_expand_vec_set_builtin (tree exp)
{
  machine_mode tmode, mode1;
  tree arg0, arg1, arg2;
  int elt;
  rtx op0, op1;

  arg0 = CALL_EXPR_ARG (exp, 0);
  arg1 = CALL_EXPR_ARG (exp, 1);
  arg2 = CALL_EXPR_ARG (exp, 2);

  tmode = TYPE_MODE (TREE_TYPE (arg0));
  mode1 = TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0)));
  gcc_assert (VECTOR_MODE_P (tmode));

  op0 = expand_expr (arg0, NULL_RTX, tmode, EXPAND_NORMAL);
  op1 = expand_expr (arg1, NULL_RTX, mode1, EXPAND_NORMAL);
  elt = get_element_number (TREE_TYPE (arg0), arg2);

  if (GET_MODE (op1) != mode1 && GET_MODE (op1) != VOIDmode)
    op1 = convert_modes (mode1, GET_MODE (op1), op1, true);

  op0 = force_reg (tmode, op0);
  op1 = force_reg (mode1, op1);

  rs6000_expand_vector_set (op0, op1, GEN_INT (elt));

  return op0;
}

/* Expand vec_ext builtin.  */
static rtx
altivec_expand_vec_ext_builtin (tree exp, rtx target)
{
  machine_mode tmode, mode0;
  tree arg0, arg1;
  rtx op0;
  rtx op1;

  arg0 = CALL_EXPR_ARG (exp, 0);
  arg1 = CALL_EXPR_ARG (exp, 1);

  op0 = expand_normal (arg0);
  op1 = expand_normal (arg1);

  if (TREE_CODE (arg1) == INTEGER_CST)
    {
      unsigned HOST_WIDE_INT elt;
      unsigned HOST_WIDE_INT size = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
      unsigned int truncated_selector;
      /* Even if !tree_fits_uhwi_p (arg1)), TREE_INT_CST_LOW (arg0)
	 returns low-order bits of INTEGER_CST for modulo indexing.  */
      elt = TREE_INT_CST_LOW (arg1);
      truncated_selector = elt % size;
      op1 = GEN_INT (truncated_selector);
    }

  tmode = TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0)));
  mode0 = TYPE_MODE (TREE_TYPE (arg0));
  gcc_assert (VECTOR_MODE_P (mode0));

  op0 = force_reg (mode0, op0);

  if (optimize || !target || !register_operand (target, tmode))
    target = gen_reg_rtx (tmode);

  rs6000_expand_vector_extract (target, op0, op1);

  return target;
}

/* Expand ALTIVEC_BUILTIN_MASK_FOR_LOAD.  */
rtx
rs6000_expand_ldst_mask (rtx target, tree arg0)
{
  int icode2 = BYTES_BIG_ENDIAN ? (int) CODE_FOR_altivec_lvsr_direct
				: (int) CODE_FOR_altivec_lvsl_direct;
  machine_mode tmode = insn_data[icode2].operand[0].mode;
  machine_mode mode = insn_data[icode2].operand[1].mode;

  gcc_assert (TARGET_ALTIVEC);

  gcc_assert (POINTER_TYPE_P (TREE_TYPE (arg0)));
  rtx op = expand_expr (arg0, NULL_RTX, Pmode, EXPAND_NORMAL);
  rtx addr = memory_address (mode, op);
  /* We need to negate the address.  */
  op = gen_reg_rtx (GET_MODE (addr));
  emit_insn (gen_rtx_SET (op, gen_rtx_NEG (GET_MODE (addr), addr)));
  op = gen_rtx_MEM (mode, op);

  if (target == 0
      || GET_MODE (target) != tmode
      || !insn_data[icode2].operand[0].predicate (target, tmode))
    target = gen_reg_rtx (tmode);

  rtx pat = GEN_FCN (icode2) (target, op);
  if (!pat)
    return 0;
  emit_insn (pat);

  return target;
}

/* Used by __builtin_cpu_is(), mapping from PLATFORM names to values.  */
static const struct
{
  const char *cpu;
  unsigned int cpuid;
} cpu_is_info[] = {
  { "power10",	   PPC_PLATFORM_POWER10 },
  { "power9",	   PPC_PLATFORM_POWER9 },
  { "power8",	   PPC_PLATFORM_POWER8 },
  { "power7",	   PPC_PLATFORM_POWER7 },
  { "power6x",	   PPC_PLATFORM_POWER6X },
  { "power6",	   PPC_PLATFORM_POWER6 },
  { "power5+",	   PPC_PLATFORM_POWER5_PLUS },
  { "power5",	   PPC_PLATFORM_POWER5 },
  { "ppc970",	   PPC_PLATFORM_PPC970 },
  { "power4",	   PPC_PLATFORM_POWER4 },
  { "ppca2",	   PPC_PLATFORM_PPCA2 },
  { "ppc476",	   PPC_PLATFORM_PPC476 },
  { "ppc464",	   PPC_PLATFORM_PPC464 },
  { "ppc440",	   PPC_PLATFORM_PPC440 },
  { "ppc405",	   PPC_PLATFORM_PPC405 },
  { "ppc-cell-be", PPC_PLATFORM_CELL_BE }
};

/* Used by __builtin_cpu_supports(), mapping from HWCAP names to masks.  */
static const struct
{
  const char *hwcap;
  int mask;
  unsigned int id;
} cpu_supports_info[] = {
  /* AT_HWCAP masks.  */
  { "4xxmac",		PPC_FEATURE_HAS_4xxMAC,		0 },
  { "altivec",		PPC_FEATURE_HAS_ALTIVEC,	0 },
  { "arch_2_05",	PPC_FEATURE_ARCH_2_05,		0 },
  { "arch_2_06",	PPC_FEATURE_ARCH_2_06,		0 },
  { "archpmu",		PPC_FEATURE_PERFMON_COMPAT,	0 },
  { "booke",		PPC_FEATURE_BOOKE,		0 },
  { "cellbe",		PPC_FEATURE_CELL_BE,		0 },
  { "dfp",		PPC_FEATURE_HAS_DFP,		0 },
  { "efpdouble",	PPC_FEATURE_HAS_EFP_DOUBLE,	0 },
  { "efpsingle",	PPC_FEATURE_HAS_EFP_SINGLE,	0 },
  { "fpu",		PPC_FEATURE_HAS_FPU,		0 },
  { "ic_snoop",		PPC_FEATURE_ICACHE_SNOOP,	0 },
  { "mmu",		PPC_FEATURE_HAS_MMU,		0 },
  { "notb",		PPC_FEATURE_NO_TB,		0 },
  { "pa6t",		PPC_FEATURE_PA6T,		0 },
  { "power4",		PPC_FEATURE_POWER4,		0 },
  { "power5",		PPC_FEATURE_POWER5,		0 },
  { "power5+",		PPC_FEATURE_POWER5_PLUS,	0 },
  { "power6x",		PPC_FEATURE_POWER6_EXT,		0 },
  { "ppc32",		PPC_FEATURE_32,			0 },
  { "ppc601",		PPC_FEATURE_601_INSTR,		0 },
  { "ppc64",		PPC_FEATURE_64,			0 },
  { "ppcle",		PPC_FEATURE_PPC_LE,		0 },
  { "smt",		PPC_FEATURE_SMT,		0 },
  { "spe",		PPC_FEATURE_HAS_SPE,		0 },
  { "true_le",		PPC_FEATURE_TRUE_LE,		0 },
  { "ucache",		PPC_FEATURE_UNIFIED_CACHE,	0 },
  { "vsx",		PPC_FEATURE_HAS_VSX,		0 },

  /* AT_HWCAP2 masks.  */
  { "arch_2_07",	PPC_FEATURE2_ARCH_2_07,		1 },
  { "dscr",		PPC_FEATURE2_HAS_DSCR,		1 },
  { "ebb",		PPC_FEATURE2_HAS_EBB,		1 },
  { "htm",		PPC_FEATURE2_HAS_HTM,		1 },
  { "htm-nosc",		PPC_FEATURE2_HTM_NOSC,		1 },
  { "htm-no-suspend",	PPC_FEATURE2_HTM_NO_SUSPEND,	1 },
  { "isel",		PPC_FEATURE2_HAS_ISEL,		1 },
  { "tar",		PPC_FEATURE2_HAS_TAR,		1 },
  { "vcrypto",		PPC_FEATURE2_HAS_VEC_CRYPTO,	1 },
  { "arch_3_00",	PPC_FEATURE2_ARCH_3_00,		1 },
  { "ieee128",		PPC_FEATURE2_HAS_IEEE128,	1 },
  { "darn",		PPC_FEATURE2_DARN,		1 },
  { "scv",		PPC_FEATURE2_SCV,		1 },
  { "arch_3_1",		PPC_FEATURE2_ARCH_3_1,		1 },
  { "mma",		PPC_FEATURE2_MMA,		1 },
};

/* Expand the CPU builtin in FCODE and store the result in TARGET.  */
static rtx
cpu_expand_builtin (enum rs6000_gen_builtins fcode,
		    tree exp ATTRIBUTE_UNUSED, rtx target)
{
  /* __builtin_cpu_init () is a nop, so expand to nothing.  */
  if (fcode == RS6000_BIF_CPU_INIT)
    return const0_rtx;

  if (target == 0 || GET_MODE (target) != SImode)
    target = gen_reg_rtx (SImode);

  /* TODO: Factor the #ifdef'd code into a separate function.  */
#ifdef TARGET_LIBC_PROVIDES_HWCAP_IN_TCB
  tree arg = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0);
  /* Target clones creates an ARRAY_REF instead of STRING_CST, convert it back
     to a STRING_CST.  */
  if (TREE_CODE (arg) == ARRAY_REF
      && TREE_CODE (TREE_OPERAND (arg, 0)) == STRING_CST
      && TREE_CODE (TREE_OPERAND (arg, 1)) == INTEGER_CST
      && compare_tree_int (TREE_OPERAND (arg, 1), 0) == 0)
    arg = TREE_OPERAND (arg, 0);

  if (TREE_CODE (arg) != STRING_CST)
    {
      error ("builtin %qs only accepts a string argument",
	     rs6000_builtin_info[(size_t) fcode].bifname);
      return const0_rtx;
    }

  if (fcode == RS6000_BIF_CPU_IS)
    {
      const char *cpu = TREE_STRING_POINTER (arg);
      rtx cpuid = NULL_RTX;
      for (size_t i = 0; i < ARRAY_SIZE (cpu_is_info); i++)
	if (strcmp (cpu, cpu_is_info[i].cpu) == 0)
	  {
	    /* The CPUID value in the TCB is offset by _DL_FIRST_PLATFORM.  */
	    cpuid = GEN_INT (cpu_is_info[i].cpuid + _DL_FIRST_PLATFORM);
	    break;
	  }
      if (cpuid == NULL_RTX)
	{
	  /* Invalid CPU argument.  */
	  error ("cpu %qs is an invalid argument to builtin %qs",
		 cpu, rs6000_builtin_info[(size_t) fcode].bifname);
	  return const0_rtx;
	}

      rtx platform = gen_reg_rtx (SImode);
      rtx address = gen_rtx_PLUS (Pmode,
				  gen_rtx_REG (Pmode, TLS_REGNUM),
				  GEN_INT (TCB_PLATFORM_OFFSET));
      rtx tcbmem = gen_const_mem (SImode, address);
      emit_move_insn (platform, tcbmem);
      emit_insn (gen_eqsi3 (target, platform, cpuid));
    }
  else if (fcode == RS6000_BIF_CPU_SUPPORTS)
    {
      const char *hwcap = TREE_STRING_POINTER (arg);
      rtx mask = NULL_RTX;
      int hwcap_offset;
      for (size_t i = 0; i < ARRAY_SIZE (cpu_supports_info); i++)
	if (strcmp (hwcap, cpu_supports_info[i].hwcap) == 0)
	  {
	    mask = GEN_INT (cpu_supports_info[i].mask);
	    hwcap_offset = TCB_HWCAP_OFFSET (cpu_supports_info[i].id);
	    break;
	  }
      if (mask == NULL_RTX)
	{
	  /* Invalid HWCAP argument.  */
	  error ("%s %qs is an invalid argument to builtin %qs",
		 "hwcap", hwcap,
		 rs6000_builtin_info[(size_t) fcode].bifname);
	  return const0_rtx;
	}

      rtx tcb_hwcap = gen_reg_rtx (SImode);
      rtx address = gen_rtx_PLUS (Pmode,
				  gen_rtx_REG (Pmode, TLS_REGNUM),
				  GEN_INT (hwcap_offset));
      rtx tcbmem = gen_const_mem (SImode, address);
      emit_move_insn (tcb_hwcap, tcbmem);
      rtx scratch1 = gen_reg_rtx (SImode);
      emit_insn (gen_rtx_SET (scratch1,
			      gen_rtx_AND (SImode, tcb_hwcap, mask)));
      rtx scratch2 = gen_reg_rtx (SImode);
      emit_insn (gen_eqsi3 (scratch2, scratch1, const0_rtx));
      emit_insn (gen_rtx_SET (target,
			      gen_rtx_XOR (SImode, scratch2, const1_rtx)));
    }
  else
    gcc_unreachable ();

  /* Record that we have expanded a CPU builtin, so that we can later
     emit a reference to the special symbol exported by LIBC to ensure we
     do not link against an old LIBC that doesn't support this feature.  */
  cpu_builtin_p = true;

#else
  warning (0, "builtin %qs needs GLIBC (2.23 and newer) that exports hardware "
	   "capability bits", rs6000_builtin_info[(size_t) fcode].bifname);

  /* For old LIBCs, always return FALSE.  */
  emit_move_insn (target, GEN_INT (0));
#endif /* TARGET_LIBC_PROVIDES_HWCAP_IN_TCB */

  return target;
}

/* For the element-reversing load/store built-ins, produce the correct
   insn_code depending on the target endianness.  */
static insn_code
elemrev_icode (rs6000_gen_builtins fcode)
{
  switch (fcode)
    {
    case RS6000_BIF_ST_ELEMREV_V1TI:
      return BYTES_BIG_ENDIAN ? CODE_FOR_vsx_store_v1ti
			      : CODE_FOR_vsx_st_elemrev_v1ti;

    case RS6000_BIF_ST_ELEMREV_V2DF:
      return BYTES_BIG_ENDIAN ? CODE_FOR_vsx_store_v2df
			      : CODE_FOR_vsx_st_elemrev_v2df;

    case RS6000_BIF_ST_ELEMREV_V2DI:
      return BYTES_BIG_ENDIAN ? CODE_FOR_vsx_store_v2di
			      : CODE_FOR_vsx_st_elemrev_v2di;

    case RS6000_BIF_ST_ELEMREV_V4SF:
      return BYTES_BIG_ENDIAN ? CODE_FOR_vsx_store_v4sf
			      : CODE_FOR_vsx_st_elemrev_v4sf;

    case RS6000_BIF_ST_ELEMREV_V4SI:
      return BYTES_BIG_ENDIAN ? CODE_FOR_vsx_store_v4si
			      : CODE_FOR_vsx_st_elemrev_v4si;

    case RS6000_BIF_ST_ELEMREV_V8HI:
      return BYTES_BIG_ENDIAN ? CODE_FOR_vsx_store_v8hi
			      : CODE_FOR_vsx_st_elemrev_v8hi;

    case RS6000_BIF_ST_ELEMREV_V16QI:
      return BYTES_BIG_ENDIAN ? CODE_FOR_vsx_store_v16qi
			      : CODE_FOR_vsx_st_elemrev_v16qi;

    case RS6000_BIF_LD_ELEMREV_V2DF:
      return BYTES_BIG_ENDIAN ? CODE_FOR_vsx_load_v2df
			      : CODE_FOR_vsx_ld_elemrev_v2df;

    case RS6000_BIF_LD_ELEMREV_V1TI:
      return BYTES_BIG_ENDIAN ? CODE_FOR_vsx_load_v1ti
			      : CODE_FOR_vsx_ld_elemrev_v1ti;

    case RS6000_BIF_LD_ELEMREV_V2DI:
      return BYTES_BIG_ENDIAN ? CODE_FOR_vsx_load_v2di
			      : CODE_FOR_vsx_ld_elemrev_v2di;

    case RS6000_BIF_LD_ELEMREV_V4SF:
      return BYTES_BIG_ENDIAN ? CODE_FOR_vsx_load_v4sf
			      : CODE_FOR_vsx_ld_elemrev_v4sf;

    case RS6000_BIF_LD_ELEMREV_V4SI:
      return BYTES_BIG_ENDIAN ? CODE_FOR_vsx_load_v4si
			      : CODE_FOR_vsx_ld_elemrev_v4si;

    case RS6000_BIF_LD_ELEMREV_V8HI:
      return BYTES_BIG_ENDIAN ? CODE_FOR_vsx_load_v8hi
			      : CODE_FOR_vsx_ld_elemrev_v8hi;

    case RS6000_BIF_LD_ELEMREV_V16QI:
      return BYTES_BIG_ENDIAN ? CODE_FOR_vsx_load_v16qi
			      : CODE_FOR_vsx_ld_elemrev_v16qi;
    default:
      ;
    }

  gcc_unreachable ();
}

/* Expand an AltiVec vector load builtin, and return the expanded rtx.  */
static rtx
ldv_expand_builtin (rtx target, insn_code icode, rtx *op, machine_mode tmode)
{
  if (target == 0
      || GET_MODE (target) != tmode
      || !insn_data[icode].operand[0].predicate (target, tmode))
    target = gen_reg_rtx (tmode);

  op[1] = copy_to_mode_reg (Pmode, op[1]);

  /* These CELL built-ins use BLKmode instead of tmode for historical
     (i.e., unknown) reasons.  TODO: Is this necessary?  */
  bool blk = (icode == CODE_FOR_altivec_lvlx
	      || icode == CODE_FOR_altivec_lvlxl
	      || icode == CODE_FOR_altivec_lvrx
	      || icode == CODE_FOR_altivec_lvrxl);

  /* For LVX, express the RTL accurately by ANDing the address with -16.
     LVXL and LVE*X expand to use UNSPECs to hide their special behavior,
     so the raw address is fine.  */
  /* TODO: That statement seems wrong, as the UNSPECs don't surround the
     memory expression, so a latent bug may lie here.  The &-16 is likely
     needed for all VMX-style loads.  */
  if (icode == CODE_FOR_altivec_lvx_v1ti
      || icode == CODE_FOR_altivec_lvx_v2df
      || icode == CODE_FOR_altivec_lvx_v2di
      || icode == CODE_FOR_altivec_lvx_v4sf
      || icode == CODE_FOR_altivec_lvx_v4si
      || icode == CODE_FOR_altivec_lvx_v8hi
      || icode == CODE_FOR_altivec_lvx_v16qi)
    {
      rtx rawaddr;
      if (op[0] == const0_rtx)
	rawaddr = op[1];
      else
	{
	  op[0] = copy_to_mode_reg (Pmode, op[0]);
	  rawaddr = gen_rtx_PLUS (Pmode, op[1], op[0]);
	}
      rtx addr = gen_rtx_AND (Pmode, rawaddr, gen_rtx_CONST_INT (Pmode, -16));
      addr = gen_rtx_MEM (blk ? BLKmode : tmode, addr);

      emit_insn (gen_rtx_SET (target, addr));
    }
  else
    {
      rtx addr;
      if (op[0] == const0_rtx)
	addr = gen_rtx_MEM (blk ? BLKmode : tmode, op[1]);
      else
	{
	  op[0] = copy_to_mode_reg (Pmode, op[0]);
	  addr = gen_rtx_MEM (blk ? BLKmode : tmode,
			      gen_rtx_PLUS (Pmode, op[1], op[0]));
	}

      rtx pat = GEN_FCN (icode) (target, addr);
      if (!pat)
	return 0;
      emit_insn (pat);
    }

  return target;
}

/* Expand a builtin function that loads a scalar into a vector register
   with sign extension, and return the expanded rtx.  */
static rtx
lxvrse_expand_builtin (rtx target, insn_code icode, rtx *op,
		       machine_mode tmode, machine_mode smode)
{
  rtx pat, addr;
  op[1] = copy_to_mode_reg (Pmode, op[1]);

  if (op[0] == const0_rtx)
    addr = gen_rtx_MEM (tmode, op[1]);
  else
    {
      op[0] = copy_to_mode_reg (Pmode, op[0]);
      addr = gen_rtx_MEM (smode,
			  gen_rtx_PLUS (Pmode, op[1], op[0]));
    }

  rtx discratch = gen_reg_rtx (V2DImode);
  rtx tiscratch = gen_reg_rtx (TImode);

  /* Emit the lxvr*x insn.  */
  pat = GEN_FCN (icode) (tiscratch, addr);
  if (!pat)
    return 0;
  emit_insn (pat);

  /* Emit a sign extension from V16QI,V8HI,V4SI to V2DI.  */
  rtx temp1;
  if (icode == CODE_FOR_vsx_lxvrbx)
    {
      temp1  = simplify_gen_subreg (V16QImode, tiscratch, TImode, 0);
      emit_insn (gen_vsx_sign_extend_qi_v2di (discratch, temp1));
    }
  else if (icode == CODE_FOR_vsx_lxvrhx)
    {
      temp1  = simplify_gen_subreg (V8HImode, tiscratch, TImode, 0);
      emit_insn (gen_vsx_sign_extend_hi_v2di (discratch, temp1));
    }
  else if (icode == CODE_FOR_vsx_lxvrwx)
    {
      temp1  = simplify_gen_subreg (V4SImode, tiscratch, TImode, 0);
      emit_insn (gen_vsx_sign_extend_si_v2di (discratch, temp1));
    }
  else if (icode == CODE_FOR_vsx_lxvrdx)
    discratch = simplify_gen_subreg (V2DImode, tiscratch, TImode, 0);
  else
    gcc_unreachable ();

  /* Emit the sign extension from V2DI (double) to TI (quad).  */
  rtx temp2 = simplify_gen_subreg (TImode, discratch, V2DImode, 0);
  emit_insn (gen_extendditi2_vector (target, temp2));

  return target;
}

/* Expand a builtin function that loads a scalar into a vector register
   with zero extension, and return the expanded rtx.  */
static rtx
lxvrze_expand_builtin (rtx target, insn_code icode, rtx *op,
		       machine_mode tmode, machine_mode smode)
{
  rtx pat, addr;
  op[1] = copy_to_mode_reg (Pmode, op[1]);

  if (op[0] == const0_rtx)
    addr = gen_rtx_MEM (tmode, op[1]);
  else
    {
      op[0] = copy_to_mode_reg (Pmode, op[0]);
      addr = gen_rtx_MEM (smode,
			  gen_rtx_PLUS (Pmode, op[1], op[0]));
    }

  pat = GEN_FCN (icode) (target, addr);
  if (!pat)
    return 0;
  emit_insn (pat);
  return target;
}

/* Expand an AltiVec vector store builtin, and return the expanded rtx.  */
static rtx
stv_expand_builtin (insn_code icode, rtx *op,
		    machine_mode tmode, machine_mode smode)
{
  op[2] = copy_to_mode_reg (Pmode, op[2]);

  /* For STVX, express the RTL accurately by ANDing the address with -16.
     STVXL and STVE*X expand to use UNSPECs to hide their special behavior,
     so the raw address is fine.  */
  /* TODO: That statement seems wrong, as the UNSPECs don't surround the
     memory expression, so a latent bug may lie here.  The &-16 is likely
     needed for all VMX-style stores.  */
  if (icode == CODE_FOR_altivec_stvx_v2df
      || icode == CODE_FOR_altivec_stvx_v2di
      || icode == CODE_FOR_altivec_stvx_v4sf
      || icode == CODE_FOR_altivec_stvx_v4si
      || icode == CODE_FOR_altivec_stvx_v8hi
      || icode == CODE_FOR_altivec_stvx_v16qi)
    {
      rtx rawaddr;
      if (op[1] == const0_rtx)
	rawaddr = op[2];
      else
	{
	  op[1] = copy_to_mode_reg (Pmode, op[1]);
	  rawaddr = gen_rtx_PLUS (Pmode, op[2], op[1]);
	}

      rtx addr = gen_rtx_AND (Pmode, rawaddr, gen_rtx_CONST_INT (Pmode, -16));
      addr = gen_rtx_MEM (tmode, addr);
      op[0] = copy_to_mode_reg (tmode, op[0]);
      emit_insn (gen_rtx_SET (addr, op[0]));
    }
  else if (icode == CODE_FOR_vsx_stxvrbx
	   || icode == CODE_FOR_vsx_stxvrhx
	   || icode == CODE_FOR_vsx_stxvrwx
	   || icode == CODE_FOR_vsx_stxvrdx)
    {
      rtx truncrtx = gen_rtx_TRUNCATE (tmode, op[0]);
      op[0] = copy_to_mode_reg (E_TImode, truncrtx);

      rtx addr;
      if (op[1] == const0_rtx)
	addr = gen_rtx_MEM (Pmode, op[2]);
      else
	{
	  op[1] = copy_to_mode_reg (Pmode, op[1]);
	  addr = gen_rtx_MEM (tmode, gen_rtx_PLUS (Pmode, op[2], op[1]));
	}
      rtx pat = GEN_FCN (icode) (addr, op[0]);
      if (pat)
	emit_insn (pat);
    }
  else
    {
      if (!insn_data[icode].operand[1].predicate (op[0], smode))
	op[0] = copy_to_mode_reg (smode, op[0]);

      rtx addr;
      if (op[1] == const0_rtx)
	addr = gen_rtx_MEM (tmode, op[2]);
      else
	{
	  op[1] = copy_to_mode_reg (Pmode, op[1]);
	  addr = gen_rtx_MEM (tmode, gen_rtx_PLUS (Pmode, op[2], op[1]));
	}

      rtx pat = GEN_FCN (icode) (addr, op[0]);
      if (pat)
	emit_insn (pat);
    }

  return NULL_RTX;
}

/* Expand the MMA built-in in EXP, and return it.  */
static rtx
mma_expand_builtin (tree exp, rtx target, insn_code icode,
		    rs6000_gen_builtins fcode)
{
  tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
  bool void_func = TREE_TYPE (TREE_TYPE (fndecl)) == void_type_node;
  machine_mode tmode = VOIDmode;
  rtx op[MAX_MMA_OPERANDS];
  unsigned nopnds = 0;

  if (!void_func)
    {
      tmode = insn_data[icode].operand[0].mode;
      if (!(target
	    && GET_MODE (target) == tmode
	    && insn_data[icode].operand[0].predicate (target, tmode)))
	target = gen_reg_rtx (tmode);
      op[nopnds++] = target;
    }
  else
    target = const0_rtx;

  call_expr_arg_iterator iter;
  tree arg;
  FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
    {
      if (arg == error_mark_node)
	return const0_rtx;

      rtx opnd;
      const struct insn_operand_data *insn_op;
      insn_op = &insn_data[icode].operand[nopnds];
      if (TREE_CODE (arg) == ADDR_EXPR
	  && MEM_P (DECL_RTL (TREE_OPERAND (arg, 0))))
	opnd = DECL_RTL (TREE_OPERAND (arg, 0));
      else
	opnd = expand_normal (arg);

      if (!insn_op->predicate (opnd, insn_op->mode))
	{
	  /* TODO: This use of constraints needs explanation.  */
	  if (!strcmp (insn_op->constraint, "n"))
	    {
	      if (!CONST_INT_P (opnd))
		error ("argument %d must be an unsigned literal", nopnds);
	      else
		error ("argument %d is an unsigned literal that is "
		       "out of range", nopnds);
	      return const0_rtx;
	    }
	  opnd = copy_to_mode_reg (insn_op->mode, opnd);
	}

      /* Some MMA instructions have INOUT accumulator operands, so force
	 their target register to be the same as their input register.  */
      if (!void_func
	  && nopnds == 1
	  && !strcmp (insn_op->constraint, "0")
	  && insn_op->mode == tmode
	  && REG_P (opnd)
	  && insn_data[icode].operand[0].predicate (opnd, tmode))
	target = op[0] = opnd;

      op[nopnds++] = opnd;
    }

  rtx pat;
  switch (nopnds)
    {
    case 1:
      pat = GEN_FCN (icode) (op[0]);
      break;
    case 2:
      pat = GEN_FCN (icode) (op[0], op[1]);
      break;
    case 3:
      /* The ASSEMBLE builtin source operands are reversed in little-endian
	 mode, so reorder them.  */
      if (fcode == RS6000_BIF_ASSEMBLE_PAIR_V_INTERNAL && !WORDS_BIG_ENDIAN)
	std::swap (op[1], op[2]);
      pat = GEN_FCN (icode) (op[0], op[1], op[2]);
      break;
    case 4:
      pat = GEN_FCN (icode) (op[0], op[1], op[2], op[3]);
      break;
    case 5:
      /* The ASSEMBLE builtin source operands are reversed in little-endian
	 mode, so reorder them.  */
      if (fcode == RS6000_BIF_ASSEMBLE_ACC_INTERNAL && !WORDS_BIG_ENDIAN)
	{
	  std::swap (op[1], op[4]);
	  std::swap (op[2], op[3]);
	}
      pat = GEN_FCN (icode) (op[0], op[1], op[2], op[3], op[4]);
      break;
    case 6:
      pat = GEN_FCN (icode) (op[0], op[1], op[2], op[3], op[4], op[5]);
      break;
    case 7:
      pat = GEN_FCN (icode) (op[0], op[1], op[2], op[3], op[4], op[5], op[6]);
      break;
    default:
      gcc_unreachable ();
    }

  if (!pat)
    return NULL_RTX;

  emit_insn (pat);
  return target;
}

/* Return the correct ICODE value depending on whether we are
   setting or reading the HTM SPRs.  */
static inline enum insn_code
rs6000_htm_spr_icode (bool nonvoid)
{
  if (nonvoid)
    return (TARGET_POWERPC64) ? CODE_FOR_htm_mfspr_di : CODE_FOR_htm_mfspr_si;
  else
    return (TARGET_POWERPC64) ? CODE_FOR_htm_mtspr_di : CODE_FOR_htm_mtspr_si;
}

/* Return the appropriate SPR number associated with the given builtin.  */
static inline HOST_WIDE_INT
htm_spr_num (enum rs6000_gen_builtins code)
{
  if (code == RS6000_BIF_GET_TFHAR
      || code == RS6000_BIF_SET_TFHAR)
    return TFHAR_SPR;
  else if (code == RS6000_BIF_GET_TFIAR
	   || code == RS6000_BIF_SET_TFIAR)
    return TFIAR_SPR;
  else if (code == RS6000_BIF_GET_TEXASR
	   || code == RS6000_BIF_SET_TEXASR)
    return TEXASR_SPR;
  gcc_assert (code == RS6000_BIF_GET_TEXASRU
	      || code == RS6000_BIF_SET_TEXASRU);
  return TEXASRU_SPR;
}

/* Expand the HTM builtin in EXP and store the result in TARGET.
   Return the expanded rtx.  */
static rtx
htm_expand_builtin (bifdata *bifaddr, rs6000_gen_builtins fcode,
		    tree exp, rtx target)
{
  if (!TARGET_POWERPC64
      && (fcode == RS6000_BIF_TABORTDC
	  || fcode == RS6000_BIF_TABORTDCI))
    {
      error ("builtin %qs is only valid in 64-bit mode", bifaddr->bifname);
      return const0_rtx;
    }

  tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
  bool nonvoid = TREE_TYPE (TREE_TYPE (fndecl)) != void_type_node;
  bool uses_spr = bif_is_htmspr (*bifaddr);
  insn_code icode = bifaddr->icode;

  if (uses_spr)
    icode = rs6000_htm_spr_icode (nonvoid);

  rtx op[MAX_HTM_OPERANDS];
  int nopnds = 0;
  const insn_operand_data *insn_op = &insn_data[icode].operand[0];

  if (nonvoid)
    {
      machine_mode tmode = (uses_spr) ? insn_op->mode : E_SImode;
      if (!target
	  || GET_MODE (target) != tmode
	  || (uses_spr && !insn_op->predicate (target, tmode)))
	target = gen_reg_rtx (tmode);
      if (uses_spr)
	op[nopnds++] = target;
    }

  tree arg;
  call_expr_arg_iterator iter;

  FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
    {
      if (arg == error_mark_node || nopnds >= MAX_HTM_OPERANDS)
	return const0_rtx;

      insn_op = &insn_data[icode].operand[nopnds];
      op[nopnds] = expand_normal (arg);

      if (!insn_op->predicate (op[nopnds], insn_op->mode))
	{
	  /* TODO: This use of constraints could use explanation.
	     This happens a couple of places, perhaps make that a
	     function to document what's happening.  */
	  if (!strcmp (insn_op->constraint, "n"))
	    {
	      int arg_num = nonvoid ? nopnds : nopnds + 1;
	      if (!CONST_INT_P (op[nopnds]))
		error ("argument %d must be an unsigned literal", arg_num);
	      else
		error ("argument %d is an unsigned literal that is "
		       "out of range", arg_num);
	      return const0_rtx;
	    }
	  op[nopnds] = copy_to_mode_reg (insn_op->mode, op[nopnds]);
	}

      nopnds++;
    }

  /* Handle the builtins for extended mnemonics.  These accept
     no arguments, but map to builtins that take arguments.  */
  switch (fcode)
    {
    case RS6000_BIF_TENDALL:  /* Alias for: tend. 1  */
    case RS6000_BIF_TRESUME:  /* Alias for: tsr. 1  */
      op[nopnds++] = GEN_INT (1);
      break;
    case RS6000_BIF_TSUSPEND: /* Alias for: tsr. 0  */
      op[nopnds++] = GEN_INT (0);
      break;
    default:
      break;
    }

  /* If this builtin accesses SPRs, then pass in the appropriate
     SPR number and SPR regno as the last two operands.  */
  rtx cr = NULL_RTX;
  if (uses_spr)
    {
      machine_mode mode = TARGET_POWERPC64 ? DImode : SImode;
      op[nopnds++] = gen_rtx_CONST_INT (mode, htm_spr_num (fcode));
    }
  /* If this builtin accesses a CR field, then pass in a scratch
     CR field as the last operand.  */
  else if (bif_is_htmcr (*bifaddr))
    {
      cr = gen_reg_rtx (CCmode);
      op[nopnds++] = cr;
    }

  rtx pat;
  switch (nopnds)
    {
    case 1:
      pat = GEN_FCN (icode) (op[0]);
      break;
    case 2:
      pat = GEN_FCN (icode) (op[0], op[1]);
      break;
    case 3:
      pat = GEN_FCN (icode) (op[0], op[1], op[2]);
      break;
    case 4:
      pat = GEN_FCN (icode) (op[0], op[1], op[2], op[3]);
      break;
    default:
      gcc_unreachable ();
    }
  if (!pat)
    return NULL_RTX;
  emit_insn (pat);

  if (bif_is_htmcr (*bifaddr))
    {
      if (fcode == RS6000_BIF_TBEGIN)
	{
	  /* Emit code to set TARGET to true or false depending on
	     whether the tbegin. instruction succeeded or failed
	     to start a transaction.  We do this by placing the 1's
	     complement of CR's EQ bit into TARGET.  */
	  rtx scratch = gen_reg_rtx (SImode);
	  emit_insn (gen_rtx_SET (scratch,
				  gen_rtx_EQ (SImode, cr,
					      const0_rtx)));
	  emit_insn (gen_rtx_SET (target,
				  gen_rtx_XOR (SImode, scratch,
					       GEN_INT (1))));
	}
      else
	{
	  /* Emit code to copy the 4-bit condition register field
	     CR into the least significant end of register TARGET.  */
	  rtx scratch1 = gen_reg_rtx (SImode);
	  rtx scratch2 = gen_reg_rtx (SImode);
	  rtx subreg = simplify_gen_subreg (CCmode, scratch1, SImode, 0);
	  emit_insn (gen_movcc (subreg, cr));
	  emit_insn (gen_lshrsi3 (scratch2, scratch1, GEN_INT (28)));
	  emit_insn (gen_andsi3 (target, scratch2, GEN_INT (0xf)));
	}
    }

  if (nonvoid)
    return target;
  return const0_rtx;
}

/* Expand an expression EXP that calls a built-in function,
   with result going to TARGET if that's convenient
   (and in mode MODE if that's convenient).
   SUBTARGET may be used as the target for computing one of EXP's operands.
   IGNORE is nonzero if the value is to be ignored.
   Use the new builtin infrastructure.  */
rtx
rs6000_expand_builtin (tree exp, rtx target, rtx /* subtarget */,
		       machine_mode /* mode */, int ignore)
{
  tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
  enum rs6000_gen_builtins fcode
    = (enum rs6000_gen_builtins) DECL_MD_FUNCTION_CODE (fndecl);

  /* Emit error message if it's an unresolved overloaded builtin.  */
  if (fcode > RS6000_OVLD_NONE)
    {
      error ("unresolved overload for builtin %qF", fndecl);
      return const0_rtx;
    }

  size_t uns_fcode = (size_t)fcode;
  enum insn_code icode = rs6000_builtin_info[uns_fcode].icode;

  /* TODO: The following commentary and code is inherited from the original
     builtin processing code.  The commentary is a bit confusing, with the
     intent being that KFmode is always IEEE-128, IFmode is always IBM
     double-double, and TFmode is the current long double.  The code is
     confusing in that it converts from KFmode to TFmode pattern names,
     when the other direction is more intuitive.  Try to address this.  */

  /* We have two different modes (KFmode, TFmode) that are the IEEE
     128-bit floating point type, depending on whether long double is the
     IBM extended double (KFmode) or long double is IEEE 128-bit (TFmode).
     It is simpler if we only define one variant of the built-in function,
     and switch the code when defining it, rather than defining two built-
     ins and using the overload table in rs6000-c.cc to switch between the
     two.  If we don't have the proper assembler, don't do this switch
     because CODE_FOR_*kf* and CODE_FOR_*tf* will be CODE_FOR_nothing.  */
  if (FLOAT128_IEEE_P (TFmode))
    switch (icode)
      {
      case CODE_FOR_sqrtkf2_odd:
	icode = CODE_FOR_sqrttf2_odd;
	break;
      case CODE_FOR_trunckfdf2_odd:
	icode = CODE_FOR_trunctfdf2_odd;
	break;
      case CODE_FOR_addkf3_odd:
	icode = CODE_FOR_addtf3_odd;
	break;
      case CODE_FOR_subkf3_odd:
	icode = CODE_FOR_subtf3_odd;
	break;
      case CODE_FOR_mulkf3_odd:
	icode = CODE_FOR_multf3_odd;
	break;
      case CODE_FOR_divkf3_odd:
	icode = CODE_FOR_divtf3_odd;
	break;
      case CODE_FOR_fmakf4_odd:
	icode = CODE_FOR_fmatf4_odd;
	break;
      case CODE_FOR_xsxexpqp_kf:
	icode = CODE_FOR_xsxexpqp_tf;
	break;
      case CODE_FOR_xsxsigqp_kf:
	icode = CODE_FOR_xsxsigqp_tf;
	break;
      case CODE_FOR_xststdcnegqp_kf:
	icode = CODE_FOR_xststdcnegqp_tf;
	break;
      case CODE_FOR_xsiexpqp_kf:
	icode = CODE_FOR_xsiexpqp_tf;
	break;
      case CODE_FOR_xsiexpqpf_kf:
	icode = CODE_FOR_xsiexpqpf_tf;
	break;
      case CODE_FOR_xststdcqp_kf:
	icode = CODE_FOR_xststdcqp_tf;
	break;
      case CODE_FOR_xscmpexpqp_eq_kf:
	icode = CODE_FOR_xscmpexpqp_eq_tf;
	break;
      case CODE_FOR_xscmpexpqp_lt_kf:
	icode = CODE_FOR_xscmpexpqp_lt_tf;
	break;
      case CODE_FOR_xscmpexpqp_gt_kf:
	icode = CODE_FOR_xscmpexpqp_gt_tf;
	break;
      case CODE_FOR_xscmpexpqp_unordered_kf:
	icode = CODE_FOR_xscmpexpqp_unordered_tf;
	break;
      default:
	break;
      }

  /* In case of "#pragma target" changes, we initialize all builtins
     but check for actual availability now, during expand time.  For
     invalid builtins, generate a normal call.  */
  bifdata *bifaddr = &rs6000_builtin_info[uns_fcode];

  if (!rs6000_builtin_is_supported (fcode))
    {
      rs6000_invalid_builtin (fcode);
      return expand_call (exp, target, ignore);
    }

  if (bif_is_nosoft (*bifaddr)
      && rs6000_isa_flags & OPTION_MASK_SOFT_FLOAT)
    {
      error ("%qs not supported with %<-msoft-float%>",
	     bifaddr->bifname);
      return const0_rtx;
    }

  if (bif_is_no32bit (*bifaddr) && TARGET_32BIT)
    {
      error ("%qs is not supported in 32-bit mode", bifaddr->bifname);
      return const0_rtx;
    }

  if (bif_is_ibmld (*bifaddr) && !FLOAT128_2REG_P (TFmode))
    {
      error ("%qs requires %<long double%> to be IBM 128-bit format",
	     bifaddr->bifname);
      return const0_rtx;
    }

  if (bif_is_ibm128 (*bifaddr) && !ibm128_float_type_node)
    {
      error ("%qs requires %<__ibm128%> type support",
	     bifaddr->bifname);
      return const0_rtx;
    }

  if (bif_is_cpu (*bifaddr))
    return cpu_expand_builtin (fcode, exp, target);

  if (bif_is_init (*bifaddr))
    return altivec_expand_vec_init_builtin (TREE_TYPE (exp), exp, target);

  if (bif_is_set (*bifaddr))
    return altivec_expand_vec_set_builtin (exp);

  if (bif_is_extract (*bifaddr))
    return altivec_expand_vec_ext_builtin (exp, target);

  if (bif_is_predicate (*bifaddr))
    return altivec_expand_predicate_builtin (icode, exp, target);

  if (bif_is_htm (*bifaddr))
    return htm_expand_builtin (bifaddr, fcode, exp, target);

  if (bif_is_32bit (*bifaddr) && TARGET_32BIT)
    {
      if (fcode == RS6000_BIF_MFTB)
	icode = CODE_FOR_rs6000_mftb_si;
      else if (fcode == RS6000_BIF_BPERMD)
	icode = CODE_FOR_bpermd_si;
      else if (fcode == RS6000_BIF_DARN)
	icode = CODE_FOR_darn_64_si;
      else if (fcode == RS6000_BIF_DARN_32)
	icode = CODE_FOR_darn_32_si;
      else if (fcode == RS6000_BIF_DARN_RAW)
	icode = CODE_FOR_darn_raw_si;
      else
	gcc_unreachable ();
    }

  if (bif_is_endian (*bifaddr) && BYTES_BIG_ENDIAN)
    {
      if (fcode == RS6000_BIF_LD_ELEMREV_V1TI)
	icode = CODE_FOR_vsx_load_v1ti;
      else if (fcode == RS6000_BIF_LD_ELEMREV_V2DF)
	icode = CODE_FOR_vsx_load_v2df;
      else if (fcode == RS6000_BIF_LD_ELEMREV_V2DI)
	icode = CODE_FOR_vsx_load_v2di;
      else if (fcode == RS6000_BIF_LD_ELEMREV_V4SF)
	icode = CODE_FOR_vsx_load_v4sf;
      else if (fcode == RS6000_BIF_LD_ELEMREV_V4SI)
	icode = CODE_FOR_vsx_load_v4si;
      else if (fcode == RS6000_BIF_LD_ELEMREV_V8HI)
	icode = CODE_FOR_vsx_load_v8hi;
      else if (fcode == RS6000_BIF_LD_ELEMREV_V16QI)
	icode = CODE_FOR_vsx_load_v16qi;
      else if (fcode == RS6000_BIF_ST_ELEMREV_V1TI)
	icode = CODE_FOR_vsx_store_v1ti;
      else if (fcode == RS6000_BIF_ST_ELEMREV_V2DF)
	icode = CODE_FOR_vsx_store_v2df;
      else if (fcode == RS6000_BIF_ST_ELEMREV_V2DI)
	icode = CODE_FOR_vsx_store_v2di;
      else if (fcode == RS6000_BIF_ST_ELEMREV_V4SF)
	icode = CODE_FOR_vsx_store_v4sf;
      else if (fcode == RS6000_BIF_ST_ELEMREV_V4SI)
	icode = CODE_FOR_vsx_store_v4si;
      else if (fcode == RS6000_BIF_ST_ELEMREV_V8HI)
	icode = CODE_FOR_vsx_store_v8hi;
      else if (fcode == RS6000_BIF_ST_ELEMREV_V16QI)
	icode = CODE_FOR_vsx_store_v16qi;
      else if (fcode == RS6000_BIF_VCLZLSBB_V16QI)
	icode = CODE_FOR_vclzlsbb_v16qi;
      else if (fcode == RS6000_BIF_VCLZLSBB_V4SI)
	icode = CODE_FOR_vclzlsbb_v4si;
      else if (fcode == RS6000_BIF_VCLZLSBB_V8HI)
	icode = CODE_FOR_vclzlsbb_v8hi;
      else if (fcode == RS6000_BIF_VCTZLSBB_V16QI)
	icode = CODE_FOR_vctzlsbb_v16qi;
      else if (fcode == RS6000_BIF_VCTZLSBB_V4SI)
	icode = CODE_FOR_vctzlsbb_v4si;
      else if (fcode == RS6000_BIF_VCTZLSBB_V8HI)
	icode = CODE_FOR_vctzlsbb_v8hi;
      else
	gcc_unreachable ();
    }

  if (bif_is_ibm128 (*bifaddr) && TARGET_LONG_DOUBLE_128 && !TARGET_IEEEQUAD)
    {
      if (fcode == RS6000_BIF_PACK_IF)
	{
	  icode = CODE_FOR_packtf;
	  fcode = RS6000_BIF_PACK_TF;
	  uns_fcode = (size_t) fcode;
	}
      else if (fcode == RS6000_BIF_UNPACK_IF)
	{
	  icode = CODE_FOR_unpacktf;
	  fcode = RS6000_BIF_UNPACK_TF;
	  uns_fcode = (size_t) fcode;
	}
    }

  /* TRUE iff the built-in function returns void.  */
  bool void_func = TREE_TYPE (TREE_TYPE (fndecl)) == void_type_node;
  /* Position of first argument (0 for void-returning functions, else 1).  */
  int k;
  /* Modes for the return value, if any, and arguments.  */
  const int MAX_BUILTIN_ARGS = 6;
  machine_mode mode[MAX_BUILTIN_ARGS + 1];

  if (void_func)
    k = 0;
  else
    {
      k = 1;
      mode[0] = insn_data[icode].operand[0].mode;
    }

  /* Tree expressions for each argument.  */
  tree arg[MAX_BUILTIN_ARGS];
  /* RTL expressions for each argument.  */
  rtx op[MAX_BUILTIN_ARGS];

  int nargs = bifaddr->nargs;
  gcc_assert (nargs <= MAX_BUILTIN_ARGS);


  for (int i = 0; i < nargs; i++)
    {
      arg[i] = CALL_EXPR_ARG (exp, i);
      if (arg[i] == error_mark_node)
	return const0_rtx;
      STRIP_NOPS (arg[i]);
      op[i] = expand_normal (arg[i]);
      /* We have a couple of pesky patterns that don't specify the mode...  */
      mode[i+k] = insn_data[icode].operand[i+k].mode;
      if (!mode[i+k])
	mode[i+k] = Pmode;
    }

  /* Check for restricted constant arguments.  */
  for (int i = 0; i < 2; i++)
    {
      switch (bifaddr->restr[i])
	{
	case RES_BITS:
	  {
	    size_t mask = 1;
	    mask <<= bifaddr->restr_val1[i];
	    mask--;
	    tree restr_arg = arg[bifaddr->restr_opnd[i] - 1];
	    STRIP_NOPS (restr_arg);
	    if (!(TREE_CODE (restr_arg) == INTEGER_CST
		  && (TREE_INT_CST_LOW (restr_arg) & ~mask) == 0))
	      {
		unsigned p = (1U << bifaddr->restr_val1[i]) - 1;
		error ("argument %d must be a literal between 0 and %d,"
		       " inclusive",
		       bifaddr->restr_opnd[i], p);
		return CONST0_RTX (mode[0]);
	      }
	    break;
	  }
	case RES_RANGE:
	  {
	    tree restr_arg = arg[bifaddr->restr_opnd[i] - 1];
	    STRIP_NOPS (restr_arg);
	    if (!(TREE_CODE (restr_arg) == INTEGER_CST
		  && IN_RANGE (tree_to_shwi (restr_arg),
			       bifaddr->restr_val1[i],
			       bifaddr->restr_val2[i])))
	      {
		error ("argument %d must be a literal between %d and %d,"
		       " inclusive",
		       bifaddr->restr_opnd[i], bifaddr->restr_val1[i],
		       bifaddr->restr_val2[i]);
		return CONST0_RTX (mode[0]);
	      }
	    break;
	  }
	case RES_VAR_RANGE:
	  {
	    tree restr_arg = arg[bifaddr->restr_opnd[i] - 1];
	    STRIP_NOPS (restr_arg);
	    if (TREE_CODE (restr_arg) == INTEGER_CST
		&& !IN_RANGE (tree_to_shwi (restr_arg),
			      bifaddr->restr_val1[i],
			      bifaddr->restr_val2[i]))
	      {
		error ("argument %d must be a variable or a literal "
		       "between %d and %d, inclusive",
		       bifaddr->restr_opnd[i], bifaddr->restr_val1[i],
		       bifaddr->restr_val2[i]);
		return CONST0_RTX (mode[0]);
	      }
	    break;
	  }
	case RES_VALUES:
	  {
	    tree restr_arg = arg[bifaddr->restr_opnd[i] - 1];
	    STRIP_NOPS (restr_arg);
	    if (!(TREE_CODE (restr_arg) == INTEGER_CST
		  && (tree_to_shwi (restr_arg) == bifaddr->restr_val1[i]
		      || tree_to_shwi (restr_arg) == bifaddr->restr_val2[i])))
	      {
		error ("argument %d must be either a literal %d or a "
		       "literal %d",
		       bifaddr->restr_opnd[i], bifaddr->restr_val1[i],
		       bifaddr->restr_val2[i]);
		return CONST0_RTX (mode[0]);
	      }
	    break;
	  }
	default:
	case RES_NONE:
	  break;
	}
    }

  if (bif_is_ldstmask (*bifaddr))
    return rs6000_expand_ldst_mask (target, arg[0]);

  if (bif_is_stvec (*bifaddr))
    {
      if (bif_is_reve (*bifaddr))
	icode = elemrev_icode (fcode);
      return stv_expand_builtin (icode, op, mode[0], mode[1]);
    }

  if (bif_is_ldvec (*bifaddr))
    {
      if (bif_is_reve (*bifaddr))
	icode = elemrev_icode (fcode);
      return ldv_expand_builtin (target, icode, op, mode[0]);
    }

  if (bif_is_lxvrse (*bifaddr))
    return lxvrse_expand_builtin (target, icode, op, mode[0], mode[1]);

  if (bif_is_lxvrze (*bifaddr))
    return lxvrze_expand_builtin (target, icode, op, mode[0], mode[1]);

  if (bif_is_mma (*bifaddr))
    return mma_expand_builtin (exp, target, icode, fcode);

  if (TREE_TYPE (TREE_TYPE (fndecl)) == void_type_node)
    target = NULL_RTX;
  else if (target == 0
	   || GET_MODE (target) != mode[0]
	   || !insn_data[icode].operand[0].predicate (target, mode[0]))
    target = gen_reg_rtx (mode[0]);

  for (int i = 0; i < nargs; i++)
    if (!insn_data[icode].operand[i+k].predicate (op[i], mode[i+k]))
      op[i] = copy_to_mode_reg (mode[i+k], op[i]);

  rtx pat;

  switch (nargs)
    {
    case 0:
      pat = (void_func
	     ? GEN_FCN (icode) ()
	     : GEN_FCN (icode) (target));
      break;
    case 1:
      pat = (void_func
	     ? GEN_FCN (icode) (op[0])
	     : GEN_FCN (icode) (target, op[0]));
      break;
    case 2:
      pat = (void_func
	     ? GEN_FCN (icode) (op[0], op[1])
	     : GEN_FCN (icode) (target, op[0], op[1]));
      break;
    case 3:
      pat = (void_func
	     ? GEN_FCN (icode) (op[0], op[1], op[2])
	     : GEN_FCN (icode) (target, op[0], op[1], op[2]));
      break;
    case 4:
      pat = (void_func
	     ? GEN_FCN (icode) (op[0], op[1], op[2], op[3])
	     : GEN_FCN (icode) (target, op[0], op[1], op[2], op[3]));
      break;
    case 5:
      pat = (void_func
	     ? GEN_FCN (icode) (op[0], op[1], op[2], op[3], op[4])
	     : GEN_FCN (icode) (target, op[0], op[1], op[2], op[3], op[4]));
      break;
    case 6:
      pat = (void_func
	     ? GEN_FCN (icode) (op[0], op[1], op[2], op[3], op[4], op[5])
	     : GEN_FCN (icode) (target, op[0], op[1],
				op[2], op[3], op[4], op[5]));
      break;
    default:
      gcc_assert (MAX_BUILTIN_ARGS == 6);
      gcc_unreachable ();
    }

  if (!pat)
    return 0;

  emit_insn (pat);
  return target;
}