summaryrefslogtreecommitdiff
path: root/gcc/c-family/c-omp.cc
blob: 9232f5af10e30da4256059d0c1226dc9653ac4eb (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
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
/* This file contains routines to construct OpenACC and OpenMP constructs,
   called from parsing in the C and C++ front ends.

   Copyright (C) 2005-2022 Free Software Foundation, Inc.
   Contributed by Richard Henderson <rth@redhat.com>,
		  Diego Novillo <dnovillo@redhat.com>.

This file is part of GCC.

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

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

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

#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "options.h"
#include "c-common.h"
#include "gimple-expr.h"
#include "c-pragma.h"
#include "stringpool.h"
#include "omp-general.h"
#include "gomp-constants.h"
#include "memmodel.h"
#include "attribs.h"
#include "gimplify.h"
#include "langhooks.h"
#include "bitmap.h"
#include "gimple-fold.h"
#include "tree-iterator.h"

/* Complete a #pragma oacc wait construct.  LOC is the location of
   the #pragma.  */

tree
c_finish_oacc_wait (location_t loc, tree parms, tree clauses)
{
  const int nparms = list_length (parms);
  tree stmt, t;
  vec<tree, va_gc> *args;

  vec_alloc (args, nparms + 2);
  stmt = builtin_decl_explicit (BUILT_IN_GOACC_WAIT);

  if (omp_find_clause (clauses, OMP_CLAUSE_ASYNC))
    t = OMP_CLAUSE_ASYNC_EXPR (clauses);
  else
    t = build_int_cst (integer_type_node, GOMP_ASYNC_SYNC);

  args->quick_push (t);
  args->quick_push (build_int_cst (integer_type_node, nparms));

  for (t = parms; t; t = TREE_CHAIN (t))
    {
      if (TREE_CODE (OMP_CLAUSE_WAIT_EXPR (t)) == INTEGER_CST)
	args->quick_push (build_int_cst (integer_type_node,
			TREE_INT_CST_LOW (OMP_CLAUSE_WAIT_EXPR (t))));
      else
	args->quick_push (OMP_CLAUSE_WAIT_EXPR (t));
    }

  stmt = build_call_expr_loc_vec (loc, stmt, args);

  vec_free (args);

  return stmt;
}

/* Complete a #pragma omp master construct.  STMT is the structured-block
   that follows the pragma.  LOC is the location of the #pragma.  */

tree
c_finish_omp_master (location_t loc, tree stmt)
{
  tree t = add_stmt (build1 (OMP_MASTER, void_type_node, stmt));
  SET_EXPR_LOCATION (t, loc);
  return t;
}

/* Complete a #pragma omp masked construct.  BODY is the structured-block
   that follows the pragma.  LOC is the location of the #pragma.  */

tree
c_finish_omp_masked (location_t loc, tree body, tree clauses)
{
  tree stmt = make_node (OMP_MASKED);
  TREE_TYPE (stmt) = void_type_node;
  OMP_MASKED_BODY (stmt) = body;
  OMP_MASKED_CLAUSES (stmt) = clauses;
  SET_EXPR_LOCATION (stmt, loc);
  return add_stmt (stmt);
}

/* Complete a #pragma omp taskgroup construct.  BODY is the structured-block
   that follows the pragma.  LOC is the location of the #pragma.  */

tree
c_finish_omp_taskgroup (location_t loc, tree body, tree clauses)
{
  tree stmt = make_node (OMP_TASKGROUP);
  TREE_TYPE (stmt) = void_type_node;
  OMP_TASKGROUP_BODY (stmt) = body;
  OMP_TASKGROUP_CLAUSES (stmt) = clauses;
  SET_EXPR_LOCATION (stmt, loc);
  return add_stmt (stmt);
}

/* Complete a #pragma omp critical construct.  BODY is the structured-block
   that follows the pragma, NAME is the identifier in the pragma, or null
   if it was omitted.  LOC is the location of the #pragma.  */

tree
c_finish_omp_critical (location_t loc, tree body, tree name, tree clauses)
{
  gcc_assert (!clauses || OMP_CLAUSE_CODE (clauses) == OMP_CLAUSE_HINT);
  if (name == NULL_TREE
      && clauses != NULL_TREE
      && integer_nonzerop (OMP_CLAUSE_HINT_EXPR (clauses)))
    {
      error_at (OMP_CLAUSE_LOCATION (clauses),
		"%<#pragma omp critical%> with %<hint%> clause requires "
		"a name, except when %<omp_sync_hint_none%> is used");
      return error_mark_node;
    }

  tree stmt = make_node (OMP_CRITICAL);
  TREE_TYPE (stmt) = void_type_node;
  OMP_CRITICAL_BODY (stmt) = body;
  OMP_CRITICAL_NAME (stmt) = name;
  OMP_CRITICAL_CLAUSES (stmt) = clauses;
  SET_EXPR_LOCATION (stmt, loc);
  return add_stmt (stmt);
}

/* Complete a #pragma omp ordered construct.  STMT is the structured-block
   that follows the pragma.  LOC is the location of the #pragma.  */

tree
c_finish_omp_ordered (location_t loc, tree clauses, tree stmt)
{
  tree t = make_node (OMP_ORDERED);
  TREE_TYPE (t) = void_type_node;
  OMP_ORDERED_BODY (t) = stmt;
  if (!flag_openmp	/* flag_openmp_simd */
      && (OMP_CLAUSE_CODE (clauses) != OMP_CLAUSE_SIMD
	  || OMP_CLAUSE_CHAIN (clauses)))
    clauses = build_omp_clause (loc, OMP_CLAUSE_SIMD);
  OMP_ORDERED_CLAUSES (t) = clauses;
  SET_EXPR_LOCATION (t, loc);
  return add_stmt (t);
}


/* Complete a #pragma omp barrier construct.  LOC is the location of
   the #pragma.  */

void
c_finish_omp_barrier (location_t loc)
{
  tree x;

  x = builtin_decl_explicit (BUILT_IN_GOMP_BARRIER);
  x = build_call_expr_loc (loc, x, 0);
  add_stmt (x);
}


/* Complete a #pragma omp taskwait construct.  LOC is the location of the
   pragma.  */

void
c_finish_omp_taskwait (location_t loc)
{
  tree x;

  x = builtin_decl_explicit (BUILT_IN_GOMP_TASKWAIT);
  x = build_call_expr_loc (loc, x, 0);
  add_stmt (x);
}


/* Complete a #pragma omp taskyield construct.  LOC is the location of the
   pragma.  */

void
c_finish_omp_taskyield (location_t loc)
{
  tree x;

  x = builtin_decl_explicit (BUILT_IN_GOMP_TASKYIELD);
  x = build_call_expr_loc (loc, x, 0);
  add_stmt (x);
}


/* Complete a #pragma omp atomic construct.  For CODE OMP_ATOMIC
   the expression to be implemented atomically is LHS opcode= RHS. 
   For OMP_ATOMIC_READ V = LHS, for OMP_ATOMIC_CAPTURE_{NEW,OLD} LHS
   opcode= RHS with the new or old content of LHS returned.
   LOC is the location of the atomic statement.  The value returned
   is either error_mark_node (if the construct was erroneous) or an
   OMP_ATOMIC* node which should be added to the current statement
   tree with add_stmt.  If TEST is set, avoid calling save_expr
   or create_tmp_var*.  */

tree
c_finish_omp_atomic (location_t loc, enum tree_code code,
		     enum tree_code opcode, tree lhs, tree rhs,
		     tree v, tree lhs1, tree rhs1, tree r, bool swapped,
		     enum omp_memory_order memory_order, bool weak,
		     bool test)
{
  tree x, type, addr, pre = NULL_TREE, rtmp = NULL_TREE, vtmp = NULL_TREE;
  HOST_WIDE_INT bitpos = 0, bitsize = 0;
  enum tree_code orig_opcode = opcode;

  if (lhs == error_mark_node || rhs == error_mark_node
      || v == error_mark_node || lhs1 == error_mark_node
      || rhs1 == error_mark_node || r == error_mark_node)
    return error_mark_node;

  /* ??? According to one reading of the OpenMP spec, complex type are
     supported, but there are no atomic stores for any architecture.
     But at least icc 9.0 doesn't support complex types here either.
     And lets not even talk about vector types...  */
  type = TREE_TYPE (lhs);
  if (!INTEGRAL_TYPE_P (type)
      && !POINTER_TYPE_P (type)
      && !SCALAR_FLOAT_TYPE_P (type))
    {
      error_at (loc, "invalid expression type for %<#pragma omp atomic%>");
      return error_mark_node;
    }
  if (TYPE_ATOMIC (type))
    {
      error_at (loc, "%<_Atomic%> expression in %<#pragma omp atomic%>");
      return error_mark_node;
    }
  if (r && r != void_list_node && !INTEGRAL_TYPE_P (TREE_TYPE (r)))
    {
      error_at (loc, "%<#pragma omp atomic compare capture%> with non-integral "
		     "comparison result");
      return error_mark_node;
    }

  if (opcode == RDIV_EXPR)
    opcode = TRUNC_DIV_EXPR;

  /* ??? Validate that rhs does not overlap lhs.  */
  tree blhs = NULL;
  if (TREE_CODE (lhs) == COMPONENT_REF
      && TREE_CODE (TREE_OPERAND (lhs, 1)) == FIELD_DECL
      && DECL_C_BIT_FIELD (TREE_OPERAND (lhs, 1))
      && DECL_BIT_FIELD_REPRESENTATIVE (TREE_OPERAND (lhs, 1)))
    {
      tree field = TREE_OPERAND (lhs, 1);
      tree repr = DECL_BIT_FIELD_REPRESENTATIVE (field);
      if (tree_fits_uhwi_p (DECL_FIELD_OFFSET (field))
	  && tree_fits_uhwi_p (DECL_FIELD_OFFSET (repr)))
	bitpos = (tree_to_uhwi (DECL_FIELD_OFFSET (field))
		  - tree_to_uhwi (DECL_FIELD_OFFSET (repr))) * BITS_PER_UNIT;
      else
	bitpos = 0;
      bitpos += (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field))
		 - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (repr)));
      gcc_assert (tree_fits_shwi_p (DECL_SIZE (field)));
      bitsize = tree_to_shwi (DECL_SIZE (field));
      blhs = lhs;
      type = TREE_TYPE (repr);
      lhs = build3 (COMPONENT_REF, TREE_TYPE (repr), TREE_OPERAND (lhs, 0),
		    repr, TREE_OPERAND (lhs, 2));
    }

  /* Take and save the address of the lhs.  From then on we'll reference it
     via indirection.  */
  addr = build_unary_op (loc, ADDR_EXPR, lhs, false);
  if (addr == error_mark_node)
    return error_mark_node;
  if (!test)
    addr = save_expr (addr);
  if (!test
      && TREE_CODE (addr) != SAVE_EXPR
      && (TREE_CODE (addr) != ADDR_EXPR
	  || !VAR_P (TREE_OPERAND (addr, 0))))
    {
      /* Make sure LHS is simple enough so that goa_lhs_expr_p can recognize
	 it even after unsharing function body.  */
      tree var = create_tmp_var_raw (TREE_TYPE (addr));
      DECL_CONTEXT (var) = current_function_decl;
      addr = build4 (TARGET_EXPR, TREE_TYPE (addr), var, addr, NULL, NULL);
    }
  tree orig_lhs = lhs;
  lhs = build_indirect_ref (loc, addr, RO_NULL);
  tree new_lhs = lhs;

  if (code == OMP_ATOMIC_READ)
    {
      x = build1 (OMP_ATOMIC_READ, type, addr);
      SET_EXPR_LOCATION (x, loc);
      OMP_ATOMIC_MEMORY_ORDER (x) = memory_order;
      gcc_assert (!weak);
      if (blhs)
	x = build3_loc (loc, BIT_FIELD_REF, TREE_TYPE (blhs), x,
			bitsize_int (bitsize), bitsize_int (bitpos));
      return build_modify_expr (loc, v, NULL_TREE, NOP_EXPR,
				loc, x, NULL_TREE);
    }

  /* There are lots of warnings, errors, and conversions that need to happen
     in the course of interpreting a statement.  Use the normal mechanisms
     to do this, and then take it apart again.  */
  if (blhs)
    {
      lhs = build3_loc (loc, BIT_FIELD_REF, TREE_TYPE (blhs), lhs,
			bitsize_int (bitsize), bitsize_int (bitpos));
      if (opcode == COND_EXPR)
	{
	  bool save = in_late_binary_op;
	  in_late_binary_op = true;
	  std::swap (rhs, rhs1);
	  rhs1 = build_binary_op (loc, EQ_EXPR, lhs, rhs1, true);
	  in_late_binary_op = save;
	}
      else if (swapped)
	rhs = build_binary_op (loc, opcode, rhs, lhs, true);
      else if (opcode != NOP_EXPR)
	rhs = build_binary_op (loc, opcode, lhs, rhs, true);
      opcode = NOP_EXPR;
    }
  else if (opcode == COND_EXPR)
    {
      bool save = in_late_binary_op;
      in_late_binary_op = true;
      std::swap (rhs, rhs1);
      rhs1 = build_binary_op (loc, EQ_EXPR, lhs, rhs1, true);
      in_late_binary_op = save;
      opcode = NOP_EXPR;
    }
  else if (swapped)
    {
      rhs = build_binary_op (loc, opcode, rhs, lhs, true);
      opcode = NOP_EXPR;
    }
  bool save = in_late_binary_op;
  in_late_binary_op = true;
  if ((opcode == MIN_EXPR || opcode == MAX_EXPR)
      && build_binary_op (loc, LT_EXPR, blhs ? blhs : lhs, rhs,
			  true) == error_mark_node)
    x = error_mark_node;
  else
    x = build_modify_expr (loc, blhs ? blhs : lhs, NULL_TREE, opcode,
			   loc, rhs, NULL_TREE);
  in_late_binary_op = save;
  if (x == error_mark_node)
    return error_mark_node;
  if (TREE_CODE (x) == COMPOUND_EXPR)
    {
      pre = TREE_OPERAND (x, 0);
      gcc_assert (TREE_CODE (pre) == SAVE_EXPR || tree_invariant_p (pre));
      x = TREE_OPERAND (x, 1);
    }
  gcc_assert (TREE_CODE (x) == MODIFY_EXPR);
  rhs = TREE_OPERAND (x, 1);

  if (blhs)
    rhs = build3_loc (loc, BIT_INSERT_EXPR, type, new_lhs,
		      rhs, bitsize_int (bitpos));
  if (orig_opcode == COND_EXPR)
    {
      if (error_operand_p (rhs1))
	return error_mark_node;
      gcc_assert (TREE_CODE (rhs1) == EQ_EXPR);
      tree cmptype = TREE_TYPE (TREE_OPERAND (rhs1, 0));
      if (SCALAR_FLOAT_TYPE_P (cmptype) && !test)
	{
	  bool clear_padding = false;
	  HOST_WIDE_INT non_padding_start = 0;
	  HOST_WIDE_INT non_padding_end = 0;
	  if (BITS_PER_UNIT == 8
	      && CHAR_BIT == 8
	      && clear_padding_type_may_have_padding_p (cmptype))
	    {
	      HOST_WIDE_INT sz = int_size_in_bytes (cmptype), i;
	      gcc_assert (sz > 0);
	      unsigned char *buf = XALLOCAVEC (unsigned char, sz);
	      memset (buf, ~0, sz);
	      clear_type_padding_in_mask (cmptype, buf);
	      for (i = 0; i < sz; i++)
		if (buf[i] != (unsigned char) ~0)
		  {
		    clear_padding = true;
		    break;
		  }
	      if (clear_padding && buf[i] == 0)
		{
		  /* Try to optimize.  In the common case where
		     non-padding bits are all continuous and start
		     and end at a byte boundary, we can just adjust
		     the memcmp call arguments and don't need to
		     emit __builtin_clear_padding calls.  */
		  if (i == 0)
		    {
		      for (i = 0; i < sz; i++)
			if (buf[i] != 0)
			  break;
		      if (i < sz && buf[i] == (unsigned char) ~0)
			{
			  non_padding_start = i;
			  for (; i < sz; i++)
			    if (buf[i] != (unsigned char) ~0)
			      break;
			}
		      else
			i = 0;
		    }
		  if (i != 0)
		    {
		      non_padding_end = i;
		      for (; i < sz; i++)
			if (buf[i] != 0)
			  {
			    non_padding_start = 0;
			    non_padding_end = 0;
			    break;
			  }
		    }
		}
	    }
	  tree inttype = NULL_TREE;
	  if (!clear_padding && tree_fits_uhwi_p (TYPE_SIZE (cmptype)))
	    {
	      HOST_WIDE_INT prec = tree_to_uhwi (TYPE_SIZE (cmptype));
	      inttype = c_common_type_for_size (prec, 1);
	      if (inttype
		  && (!tree_int_cst_equal (TYPE_SIZE (cmptype),
					   TYPE_SIZE (inttype))
		      || TYPE_PRECISION (inttype) != prec))
		inttype = NULL_TREE;
	    }
	  if (inttype)
	    {
	      TREE_OPERAND (rhs1, 0)
		= build1_loc (loc, VIEW_CONVERT_EXPR, inttype,
			      TREE_OPERAND (rhs1, 0));
	      TREE_OPERAND (rhs1, 1)
		= build1_loc (loc, VIEW_CONVERT_EXPR, inttype,
			      TREE_OPERAND (rhs1, 1));
	    }
	  else
	    {
	      tree pcmptype = build_pointer_type (cmptype);
	      tree tmp1 = create_tmp_var_raw (cmptype);
	      TREE_ADDRESSABLE (tmp1) = 1;
	      DECL_CONTEXT (tmp1) = current_function_decl;
	      tmp1 = build4 (TARGET_EXPR, cmptype, tmp1,
			     TREE_OPERAND (rhs1, 0), NULL, NULL);
	      tmp1 = build1 (ADDR_EXPR, pcmptype, tmp1);
	      tree tmp2 = create_tmp_var_raw (cmptype);
	      TREE_ADDRESSABLE (tmp2) = 1;
	      DECL_CONTEXT (tmp2) = current_function_decl;
	      tmp2 = build4 (TARGET_EXPR, cmptype, tmp2,
			     TREE_OPERAND (rhs1, 1), NULL, NULL);
	      tmp2 = build1 (ADDR_EXPR, pcmptype, tmp2);
	      if (non_padding_start)
		{
		  tmp1 = build2 (POINTER_PLUS_EXPR, pcmptype, tmp1,
				 size_int (non_padding_start));
		  tmp2 = build2 (POINTER_PLUS_EXPR, pcmptype, tmp2,
				 size_int (non_padding_start));
		}
	      tree fndecl = builtin_decl_explicit (BUILT_IN_MEMCMP);
	      rhs1 = build_call_expr_loc (loc, fndecl, 3, tmp1, tmp2,
					  non_padding_end
					  ? size_int (non_padding_end
						      - non_padding_start)
					  : TYPE_SIZE_UNIT (cmptype));
	      rhs1 = build2 (EQ_EXPR, boolean_type_node, rhs1,
			     integer_zero_node);
	      if (clear_padding && non_padding_end == 0)
		{
		  fndecl = builtin_decl_explicit (BUILT_IN_CLEAR_PADDING);
		  tree cp1 = build_call_expr_loc (loc, fndecl, 1, tmp1);
		  tree cp2 = build_call_expr_loc (loc, fndecl, 1, tmp2);
		  rhs1 = omit_two_operands_loc (loc, boolean_type_node,
						rhs1, cp2, cp1);
		}
	    }
	}
      if (r && test)
	rtmp = rhs1;
      else if (r)
	{
	  tree var = create_tmp_var_raw (boolean_type_node);
	  DECL_CONTEXT (var) = current_function_decl;
	  rtmp = build4 (TARGET_EXPR, boolean_type_node, var,
			 boolean_false_node, NULL, NULL);
	  save = in_late_binary_op;
	  in_late_binary_op = true;
	  x = build_modify_expr (loc, var, NULL_TREE, NOP_EXPR,
				 loc, rhs1, NULL_TREE);
	  in_late_binary_op = save;
	  if (x == error_mark_node)
	    return error_mark_node;
	  gcc_assert (TREE_CODE (x) == MODIFY_EXPR
		      && TREE_OPERAND (x, 0) == var);
	  TREE_OPERAND (x, 0) = rtmp;
	  rhs1 = omit_one_operand_loc (loc, boolean_type_node, x, rtmp);
	}
      rhs = build3_loc (loc, COND_EXPR, type, rhs1, rhs, new_lhs);
      rhs1 = NULL_TREE;
    }

  /* Punt the actual generation of atomic operations to common code.  */
  if (code == OMP_ATOMIC)
    type = void_type_node;
  x = build2 (code, type, addr, rhs);
  SET_EXPR_LOCATION (x, loc);
  OMP_ATOMIC_MEMORY_ORDER (x) = memory_order;
  OMP_ATOMIC_WEAK (x) = weak;

  /* Generally it is hard to prove lhs1 and lhs are the same memory
     location, just diagnose different variables.  */
  if (rhs1
      && VAR_P (rhs1)
      && VAR_P (orig_lhs)
      && rhs1 != orig_lhs
      && !test)
    {
      if (code == OMP_ATOMIC)
	error_at (loc, "%<#pragma omp atomic update%> uses two different "
		       "variables for memory");
      else
	error_at (loc, "%<#pragma omp atomic capture%> uses two different "
		       "variables for memory");
      return error_mark_node;
    }

  if (lhs1
      && lhs1 != orig_lhs
      && TREE_CODE (lhs1) == COMPONENT_REF
      && TREE_CODE (TREE_OPERAND (lhs1, 1)) == FIELD_DECL
      && DECL_C_BIT_FIELD (TREE_OPERAND (lhs1, 1))
      && DECL_BIT_FIELD_REPRESENTATIVE (TREE_OPERAND (lhs1, 1)))
    {
      tree field = TREE_OPERAND (lhs1, 1);
      tree repr = DECL_BIT_FIELD_REPRESENTATIVE (field);
      lhs1 = build3 (COMPONENT_REF, TREE_TYPE (repr), TREE_OPERAND (lhs1, 0),
		     repr, TREE_OPERAND (lhs1, 2));
    }
  if (rhs1
      && rhs1 != orig_lhs
      && TREE_CODE (rhs1) == COMPONENT_REF
      && TREE_CODE (TREE_OPERAND (rhs1, 1)) == FIELD_DECL
      && DECL_C_BIT_FIELD (TREE_OPERAND (rhs1, 1))
      && DECL_BIT_FIELD_REPRESENTATIVE (TREE_OPERAND (rhs1, 1)))
    {
      tree field = TREE_OPERAND (rhs1, 1);
      tree repr = DECL_BIT_FIELD_REPRESENTATIVE (field);
      rhs1 = build3 (COMPONENT_REF, TREE_TYPE (repr), TREE_OPERAND (rhs1, 0),
		     repr, TREE_OPERAND (rhs1, 2));
    }

  if (code != OMP_ATOMIC)
    {
      /* Generally it is hard to prove lhs1 and lhs are the same memory
	 location, just diagnose different variables.  */
      if (lhs1 && VAR_P (lhs1) && VAR_P (orig_lhs))
	{
	  if (lhs1 != orig_lhs && !test)
	    {
	      error_at (loc, "%<#pragma omp atomic capture%> uses two "
			     "different variables for memory");
	      return error_mark_node;
	    }
	}
      if (blhs)
	x = build3_loc (loc, BIT_FIELD_REF, TREE_TYPE (blhs), x,
			bitsize_int (bitsize), bitsize_int (bitpos));
      if (r && !test)
	{
	  vtmp = create_tmp_var_raw (TREE_TYPE (x));
	  DECL_CONTEXT (vtmp) = current_function_decl;
	}
      else
	vtmp = v;
      x = build_modify_expr (loc, vtmp, NULL_TREE, NOP_EXPR,
			     loc, x, NULL_TREE);
      if (x == error_mark_node)
	return error_mark_node;
      type = TREE_TYPE (x);
      if (r && !test)
	{
	  vtmp = build4 (TARGET_EXPR, TREE_TYPE (vtmp), vtmp,
			 build_zero_cst (TREE_TYPE (vtmp)), NULL, NULL);
	  gcc_assert (TREE_CODE (x) == MODIFY_EXPR
		      && TREE_OPERAND (x, 0) == TARGET_EXPR_SLOT (vtmp));
	  TREE_OPERAND (x, 0) = vtmp;
	}
      if (rhs1 && rhs1 != orig_lhs)
	{
	  tree rhs1addr = build_unary_op (loc, ADDR_EXPR, rhs1, false);
	  if (rhs1addr == error_mark_node)
	    return error_mark_node;
	  x = omit_one_operand_loc (loc, type, x, rhs1addr);
	}
      if (lhs1 && lhs1 != orig_lhs)
	{
	  tree lhs1addr = build_unary_op (loc, ADDR_EXPR, lhs1, false);
	  if (lhs1addr == error_mark_node)
	    return error_mark_node;
	  if (code == OMP_ATOMIC_CAPTURE_OLD)
	    x = omit_one_operand_loc (loc, type, x, lhs1addr);
	  else
	    {
	      if (!test)
		x = save_expr (x);
	      x = omit_two_operands_loc (loc, type, x, x, lhs1addr);
	    }
	}
    }
  else if (rhs1 && rhs1 != orig_lhs)
    {
      tree rhs1addr = build_unary_op (loc, ADDR_EXPR, rhs1, false);
      if (rhs1addr == error_mark_node)
	return error_mark_node;
      x = omit_one_operand_loc (loc, type, x, rhs1addr);
    }

  if (pre)
    x = omit_one_operand_loc (loc, type, x, pre);
  if (r && r != void_list_node)
    {
      in_late_binary_op = true;
      tree x2 = build_modify_expr (loc, r, NULL_TREE, NOP_EXPR,
				   loc, rtmp, NULL_TREE);
      in_late_binary_op = save;
      if (x2 == error_mark_node)
	return error_mark_node;
      x = omit_one_operand_loc (loc, TREE_TYPE (x2), x2, x);
    }
  if (v && vtmp != v)
    {
      in_late_binary_op = true;
      tree x2 = build_modify_expr (loc, v, NULL_TREE, NOP_EXPR,
				   loc, vtmp, NULL_TREE);
      in_late_binary_op = save;
      if (x2 == error_mark_node)
	return error_mark_node;
      x2 = build3_loc (loc, COND_EXPR, void_type_node, rtmp,
		       void_node, x2);
      x = omit_one_operand_loc (loc, TREE_TYPE (x2), x2, x);
    }
  return x;
}


/* Return true if TYPE is the implementation's omp_depend_t.  */

bool
c_omp_depend_t_p (tree type)
{
  type = TYPE_MAIN_VARIANT (type);
  return (TREE_CODE (type) == RECORD_TYPE
	  && TYPE_NAME (type)
	  && ((TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
	       ? DECL_NAME (TYPE_NAME (type)) : TYPE_NAME (type))
	      == get_identifier ("omp_depend_t"))
	  && (!TYPE_CONTEXT (type)
	      || TREE_CODE (TYPE_CONTEXT (type)) == TRANSLATION_UNIT_DECL)
	  && COMPLETE_TYPE_P (type)
	  && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
	  && !compare_tree_int (TYPE_SIZE (type),
				2 * tree_to_uhwi (TYPE_SIZE (ptr_type_node))));
}


/* Complete a #pragma omp depobj construct.  LOC is the location of the
   #pragma.  */

void
c_finish_omp_depobj (location_t loc, tree depobj,
		     enum omp_clause_depend_kind kind, tree clause)
{
  tree t = NULL_TREE;
  if (!error_operand_p (depobj))
    {
      if (!c_omp_depend_t_p (TREE_TYPE (depobj)))
	{
	  error_at (EXPR_LOC_OR_LOC (depobj, loc),
		    "type of %<depobj%> expression is not %<omp_depend_t%>");
	  depobj = error_mark_node;
	}
      else if (TYPE_READONLY (TREE_TYPE (depobj)))
	{
	  error_at (EXPR_LOC_OR_LOC (depobj, loc),
		    "%<const%> qualified %<depobj%> expression");
	  depobj = error_mark_node;
	}
    }
  else
    depobj = error_mark_node;

  if (clause == error_mark_node)
    return;

  if (clause)
    {
      gcc_assert (TREE_CODE (clause) == OMP_CLAUSE);
      if (OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_DOACROSS)
	{
	  error_at (OMP_CLAUSE_LOCATION (clause),
		    "%<depend(%s)%> is only allowed in %<omp ordered%>",
		    OMP_CLAUSE_DOACROSS_KIND (clause)
		    == OMP_CLAUSE_DOACROSS_SOURCE
		    ? "source" : "sink");
	  return;
	}
      gcc_assert (OMP_CLAUSE_CODE (clause) == OMP_CLAUSE_DEPEND);
      if (OMP_CLAUSE_CHAIN (clause))
	error_at (OMP_CLAUSE_LOCATION (clause),
		  "more than one locator in %<depend%> clause on %<depobj%> "
		  "construct");
      switch (OMP_CLAUSE_DEPEND_KIND (clause))
	{
	case OMP_CLAUSE_DEPEND_DEPOBJ:
	  error_at (OMP_CLAUSE_LOCATION (clause),
		    "%<depobj%> dependence type specified in %<depend%> "
		    "clause on %<depobj%> construct");
	  return;
	case OMP_CLAUSE_DEPEND_IN:
	case OMP_CLAUSE_DEPEND_OUT:
	case OMP_CLAUSE_DEPEND_INOUT:
	case OMP_CLAUSE_DEPEND_MUTEXINOUTSET:
	case OMP_CLAUSE_DEPEND_INOUTSET:
	  kind = OMP_CLAUSE_DEPEND_KIND (clause);
	  t = OMP_CLAUSE_DECL (clause);
	  gcc_assert (t);
	  if (TREE_CODE (t) == TREE_LIST
	      && TREE_PURPOSE (t)
	      && TREE_CODE (TREE_PURPOSE (t)) == TREE_VEC)
	    {
	      error_at (OMP_CLAUSE_LOCATION (clause),
			"%<iterator%> modifier may not be specified on "
			"%<depobj%> construct");
	      return;
	    }
	  if (TREE_CODE (t) == COMPOUND_EXPR)
	    {
	      tree t1 = build_fold_addr_expr (TREE_OPERAND (t, 1));
	      t = build2 (COMPOUND_EXPR, TREE_TYPE (t1), TREE_OPERAND (t, 0),
			  t1);
	    }
	  else if (t != null_pointer_node)
	    t = build_fold_addr_expr (t);
	  break;
	default:
	  gcc_unreachable ();
	}
    }
  else
    gcc_assert (kind != OMP_CLAUSE_DEPEND_INVALID);

  if (depobj == error_mark_node)
    return;

  depobj = build_fold_addr_expr_loc (EXPR_LOC_OR_LOC (depobj, loc), depobj);
  tree dtype
    = build_pointer_type_for_mode (ptr_type_node, TYPE_MODE (ptr_type_node),
				   true);
  depobj = fold_convert (dtype, depobj);
  tree r;
  if (clause)
    {
      depobj = save_expr (depobj);
      r = build_indirect_ref (loc, depobj, RO_UNARY_STAR);
      add_stmt (build2 (MODIFY_EXPR, void_type_node, r, t));
    }
  int k;
  switch (kind)
    {
    case OMP_CLAUSE_DEPEND_IN:
      k = GOMP_DEPEND_IN;
      break;
    case OMP_CLAUSE_DEPEND_OUT:
      k = GOMP_DEPEND_OUT;
      break;
    case OMP_CLAUSE_DEPEND_INOUT:
      k = GOMP_DEPEND_INOUT;
      break;
    case OMP_CLAUSE_DEPEND_MUTEXINOUTSET:
      k = GOMP_DEPEND_MUTEXINOUTSET;
      break;
    case OMP_CLAUSE_DEPEND_INOUTSET:
      k = GOMP_DEPEND_INOUTSET;
      break;
    case OMP_CLAUSE_DEPEND_LAST:
      k = -1;
      break;
    default:
      gcc_unreachable ();
    }
  t = build_int_cst (ptr_type_node, k);
  depobj = build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (depobj), depobj,
		       TYPE_SIZE_UNIT (ptr_type_node));
  r = build_indirect_ref (loc, depobj, RO_UNARY_STAR);
  add_stmt (build2 (MODIFY_EXPR, void_type_node, r, t));
}


/* Complete a #pragma omp flush construct.  We don't do anything with
   the variable list that the syntax allows.  LOC is the location of
   the #pragma.  */

void
c_finish_omp_flush (location_t loc, int mo)
{
  tree x;

  if (mo == MEMMODEL_LAST || mo == MEMMODEL_SEQ_CST)
    {
      x = builtin_decl_explicit (BUILT_IN_SYNC_SYNCHRONIZE);
      x = build_call_expr_loc (loc, x, 0);
    }
  else
    {
      x = builtin_decl_explicit (BUILT_IN_ATOMIC_THREAD_FENCE);
      x = build_call_expr_loc (loc, x, 1,
			       build_int_cst (integer_type_node, mo));
    }
  add_stmt (x);
}


/* Check and canonicalize OMP_FOR increment expression.
   Helper function for c_finish_omp_for.  */

static tree
check_omp_for_incr_expr (location_t loc, tree exp, tree decl)
{
  tree t;

  if (!INTEGRAL_TYPE_P (TREE_TYPE (exp))
      || TYPE_PRECISION (TREE_TYPE (exp)) < TYPE_PRECISION (TREE_TYPE (decl)))
    return error_mark_node;

  if (exp == decl)
    return build_int_cst (TREE_TYPE (exp), 0);

  switch (TREE_CODE (exp))
    {
    CASE_CONVERT:
      t = check_omp_for_incr_expr (loc, TREE_OPERAND (exp, 0), decl);
      if (t != error_mark_node)
        return fold_convert_loc (loc, TREE_TYPE (exp), t);
      break;
    case MINUS_EXPR:
      t = check_omp_for_incr_expr (loc, TREE_OPERAND (exp, 0), decl);
      if (t != error_mark_node)
        return fold_build2_loc (loc, MINUS_EXPR,
				TREE_TYPE (exp), t, TREE_OPERAND (exp, 1));
      break;
    case PLUS_EXPR:
      t = check_omp_for_incr_expr (loc, TREE_OPERAND (exp, 0), decl);
      if (t != error_mark_node)
        return fold_build2_loc (loc, PLUS_EXPR,
				TREE_TYPE (exp), t, TREE_OPERAND (exp, 1));
      t = check_omp_for_incr_expr (loc, TREE_OPERAND (exp, 1), decl);
      if (t != error_mark_node)
        return fold_build2_loc (loc, PLUS_EXPR,
				TREE_TYPE (exp), TREE_OPERAND (exp, 0), t);
      break;
    case COMPOUND_EXPR:
      {
	/* cp_build_modify_expr forces preevaluation of the RHS to make
	   sure that it is evaluated before the lvalue-rvalue conversion
	   is applied to the LHS.  Reconstruct the original expression.  */
	tree op0 = TREE_OPERAND (exp, 0);
	if (TREE_CODE (op0) == TARGET_EXPR
	    && !VOID_TYPE_P (TREE_TYPE (op0)))
	  {
	    tree op1 = TREE_OPERAND (exp, 1);
	    tree temp = TARGET_EXPR_SLOT (op0);
	    if (BINARY_CLASS_P (op1)
		&& TREE_OPERAND (op1, 1) == temp)
	      {
		op1 = copy_node (op1);
		TREE_OPERAND (op1, 1) = TARGET_EXPR_INITIAL (op0);
		return check_omp_for_incr_expr (loc, op1, decl);
	      }
	  }
	break;
      }
    default:
      break;
    }

  return error_mark_node;
}

/* If the OMP_FOR increment expression in INCR is of pointer type,
   canonicalize it into an expression handled by gimplify_omp_for()
   and return it.  DECL is the iteration variable.  */

static tree
c_omp_for_incr_canonicalize_ptr (location_t loc, tree decl, tree incr)
{
  if (POINTER_TYPE_P (TREE_TYPE (decl))
      && TREE_OPERAND (incr, 1))
    {
      tree t = fold_convert_loc (loc,
				 sizetype, TREE_OPERAND (incr, 1));

      if (TREE_CODE (incr) == POSTDECREMENT_EXPR
	  || TREE_CODE (incr) == PREDECREMENT_EXPR)
	t = fold_build1_loc (loc, NEGATE_EXPR, sizetype, t);
      t = fold_build_pointer_plus (decl, t);
      incr = build2 (MODIFY_EXPR, void_type_node, decl, t);
    }
  return incr;
}

/* State of annotation traversal for FOR loops in kernels regions,
   used to control processing and diagnostic messages that are deferred until
   the entire loop has been scanned.  */
enum annotation_state {
  as_outer,
  as_in_kernels_region,
  as_in_kernels_loop,
  /* The remaining state values represent conversion failures caught
     while in as_in_kernels_loop state.  To test whether the traversal is
     in the body of a kernels loop, use (state >= as_in_kernels_loop).  */
  as_invalid_variable_type,
  as_missing_initializer,
  as_invalid_initializer,
  as_missing_predicate,
  as_invalid_predicate,
  as_missing_increment,
  as_invalid_increment,
  as_explicit_annotation,
  as_invalid_control_flow,
  as_invalid_break,
  as_invalid_return,
  as_invalid_call,
  as_invalid_modification
};

/* Structure used to hold state for automatic annotation of FOR loops
   in kernels regions.  LOOP is the nearest enclosing loop, or
   NULL_TREE if outside of a loop context.  VARS is a tree_list
   containing the variables controlling LOOP's termination (the
   induction variable and a possible limit variable).  STATE keeps
   track of whether loop satisfies all criteria making it legal to
   parallelize.  Otherwise, REASON is a statement that blocks
   automatic parallelization, such as an unstructured jump or an
   assignment to a variable in VARS, used for printing diagnostics.

   These structures are chained through NEXT, which points to the
   next-closest enclosing loop's or the kernels region's annotation info, if
   any.  */

struct annotation_info
{
  tree loop;
  tree vars;
  bool break_ok;
  enum annotation_state state;
  tree reason;
  struct annotation_info *next;
};

/* Mark the current loop's INFO as not OK to annotate, recording STATE
   and REASON for producing diagnostics later.  */

static void
do_not_annotate_loop (struct annotation_info *info,
		      enum annotation_state state, tree reason)
{
  if (info->state == as_in_kernels_loop)
    {
      info->state = state;
      info->reason = reason;
    }
}

/* Mark the current loop identified by INFO and all of its ancestors (i.e.,
   enclosing loops) as not OK to annotate.  Arguments are the same as
   for do_not_annotate_loop.  */

static void
do_not_annotate_loop_nest (struct annotation_info *info,
			   enum annotation_state state, tree reason)
{
  while (info != NULL)
    {
      do_not_annotate_loop (info, state, reason);
      info = info->next;
    }
}

/* If INFO is non-null, call do_not_annotate_loop with STATE and REASON
   to record info for diagnosing an error later.  Otherwise emit an error now
   at ELOCUS with message MSG and the optional arguments.  */

static void annotation_error (struct annotation_info *,
			      enum annotation_state, tree, location_t,
			      const char *, ...) ATTRIBUTE_GCC_DIAG(5,6);
static
void annotation_error (struct annotation_info *info,
			      enum annotation_state state,
			      tree reason,
			      location_t elocus,
			      const char *msg, ...)
{
  if (info)
    do_not_annotate_loop (info, state, reason);
  else
    {
      auto_diagnostic_group d;
      va_list ap;
      va_start (ap, msg);
      emit_diagnostic_valist (DK_ERROR, elocus, -1, msg, &ap);
      va_end (ap);
    }
}

/* Validate and generate OMP_FOR.
   DECLV is a vector of iteration variables, for each collapsed loop.

   ORIG_DECLV, if non-NULL, is a vector with the original iteration
   variables (prior to any transformations, by say, C++ iterators).

   INITV, CONDV and INCRV are vectors containing initialization
   expressions, controlling predicates and increment expressions.
   BODY is the body of the loop and PRE_BODY statements that go before
   the loop.  FINAL_P is true if not inside a C++ template.

   INFO is null if called to parse an explicitly-annotated OMP for
   loop, otherwise it holds state information for automatically
   annotating a regular FOR loop in a kernels region.  In the former case,
   malformed loops are hard errors; otherwise we just record the annotation
   failure in INFO.  */

static tree
c_finish_omp_for_internal (location_t locus, enum tree_code code, tree declv,
			   tree orig_declv, tree initv, tree condv, tree incrv,
			   tree body, tree pre_body, bool final_p,
			   struct annotation_info *info)
{
  location_t elocus;
  bool fail = false;
  int i;

  gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (initv));
  gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (condv));
  gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (incrv));
  for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
    {
      tree decl = TREE_VEC_ELT (declv, i);
      tree init = TREE_VEC_ELT (initv, i);
      tree cond = TREE_VEC_ELT (condv, i);
      tree incr = TREE_VEC_ELT (incrv, i);

      elocus = locus;
      if (EXPR_HAS_LOCATION (init))
	elocus = EXPR_LOCATION (init);

      /* Validate the iteration variable.  */
      if (!INTEGRAL_TYPE_P (TREE_TYPE (decl))
	  && TREE_CODE (TREE_TYPE (decl)) != POINTER_TYPE)
	{
	  annotation_error (info, as_invalid_variable_type, decl, elocus,
			    "invalid type for iteration variable %qE", decl);
	  fail = true;
	}
      else if (TYPE_ATOMIC (TREE_TYPE (decl)))
	{
	  annotation_error (info, as_invalid_variable_type, decl, elocus,
			    "%<_Atomic%> iteration variable %qE", decl);
	  fail = true;
	  /* _Atomic iterator confuses stuff too much, so we risk ICE
	     trying to diagnose it further.  */
	  continue;
	}

      /* In the case of "for (int i = 0...)", init will be a decl.  It should
	 have a DECL_INITIAL that we can turn into an assignment.  */
      if (init == decl)
	{
	  elocus = DECL_SOURCE_LOCATION (decl);

	  init = DECL_INITIAL (decl);
	  if (init == NULL)
	    {
	      annotation_error (info, as_missing_initializer, decl, elocus,
				"%qE is not initialized", decl);
	      init = integer_zero_node;
	      fail = true;
	    }
	  DECL_INITIAL (decl) = NULL_TREE;

	  init = build_modify_expr (elocus, decl, NULL_TREE, NOP_EXPR,
	      			    /* FIXME diagnostics: This should
				       be the location of the INIT.  */
	      			    elocus,
				    init,
				    NULL_TREE);
	}
      if (init != error_mark_node)
	{
	  gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
	  gcc_assert (TREE_OPERAND (init, 0) == decl);
	}

      if (cond == NULL_TREE)
	{
	  annotation_error (info, as_missing_predicate, NULL_TREE, elocus,
			    "missing controlling predicate");
	  fail = true;
	}
      else
	{
	  bool cond_ok = false;

	  /* E.g. C sizeof (vla) could add COMPOUND_EXPRs with
	     evaluation of the vla VAR_DECL.  We need to readd
	     them to the non-decl operand.  See PR45784.  */
	  while (TREE_CODE (cond) == COMPOUND_EXPR)
	    cond = TREE_OPERAND (cond, 1);

	  if (EXPR_HAS_LOCATION (cond))
	    elocus = EXPR_LOCATION (cond);

	  enum tree_code condcode = TREE_CODE (cond);

	  if (condcode == LT_EXPR
	      || condcode == LE_EXPR
	      || condcode == GT_EXPR
	      || condcode == GE_EXPR
	      || condcode == NE_EXPR
	      || condcode == EQ_EXPR)
	    {
	      tree op0 = TREE_OPERAND (cond, 0);
	      tree op1 = TREE_OPERAND (cond, 1);

	      /* 2.5.1.  The comparison in the condition is computed in
		 the type of DECL, otherwise the behavior is undefined.

		 For example:
		 long n; int i;
		 i < n;

		 according to ISO will be evaluated as:
		 (long)i < n;

		 We want to force:
		 i < (int)n;  */
	      if (TREE_CODE (op0) == NOP_EXPR
		  && decl == TREE_OPERAND (op0, 0))
		{
		  op0 = TREE_OPERAND (op0, 0);
		  op1 = fold_build1_loc (elocus, NOP_EXPR, TREE_TYPE (decl),
					 op1);
		}
	      else if (TREE_CODE (op1) == NOP_EXPR
		       && decl == TREE_OPERAND (op1, 0))
		{
		  op1 = TREE_OPERAND (op1, 0);
		  op0 = fold_build1_loc (elocus, NOP_EXPR, TREE_TYPE (decl),
					 op0);
		}

	      if (decl == op0)
		cond_ok = true;
	      else if (decl == op1)
		{
		  condcode = swap_tree_comparison (condcode);
		  op1 = op0;
		  op0 = decl;
		  cond_ok = true;
		}

	      if (condcode == NE_EXPR || condcode == EQ_EXPR)
		{
		  if (!INTEGRAL_TYPE_P (TREE_TYPE (decl)))
		    {
		      if (code == OACC_LOOP || condcode == EQ_EXPR)
			cond_ok = false;
		    }
		  else if (operand_equal_p (op1,
					    TYPE_MIN_VALUE (TREE_TYPE (decl)),
					    0))
		    condcode = (condcode == NE_EXPR ? GT_EXPR : LE_EXPR);
		  else if (operand_equal_p (op1,
					    TYPE_MAX_VALUE (TREE_TYPE (decl)),
					    0))
		    condcode = (condcode == NE_EXPR ? LT_EXPR : GE_EXPR);
		  else if (code == OACC_LOOP || condcode == EQ_EXPR)
		    cond_ok = false;
		}

	      if (cond_ok)
		{
		  /* We postponed destructive changes to canonicalize
		     cond until we're sure it is OK.  In the !error_p
		     case where we are trying to transform a regular FOR_STMT
		     to OMP_FOR, we don't want to destroy the original
		     condition if we aren't going to be able to do the
		     transformation anyway.  */
		  TREE_SET_CODE (cond, condcode);
		  TREE_OPERAND (cond, 0) = op0;
		  TREE_OPERAND (cond, 1) = op1;

		  if (TREE_VEC_ELT (condv, i) != cond)
		    {
		      tree ce = NULL_TREE, *pce = &ce;
		      tree type = TREE_TYPE (op1);
		      for (tree c = TREE_VEC_ELT (condv, i); c != cond;
			   c = TREE_OPERAND (c, 1))
			{
			  *pce = build2 (COMPOUND_EXPR, type,
					 TREE_OPERAND (c, 0), op1);
			  pce = &TREE_OPERAND (*pce, 1);
			}
		      op1 = ce;
		      TREE_VEC_ELT (condv, i) = cond;
		    }
		}
	    }

	  if (!cond_ok)
	    {
	      annotation_error (info, as_invalid_predicate, cond, elocus,
				"invalid controlling predicate");
	      fail = true;
	    }
	}

      if (incr == NULL_TREE)
	{
	  annotation_error (info, as_missing_increment, NULL_TREE, elocus,
			    "missing increment expression");
	  fail = true;
	}
      else
	{
	  bool incr_ok = false;

	  if (EXPR_HAS_LOCATION (incr))
	    elocus = EXPR_LOCATION (incr);

	  /* Check all the valid increment expressions: v++, v--, ++v, --v,
	     v = v + incr, v = incr + v and v = v - incr.  */
	  switch (TREE_CODE (incr))
	    {
	    case POSTINCREMENT_EXPR:
	    case PREINCREMENT_EXPR:
	    case POSTDECREMENT_EXPR:
	    case PREDECREMENT_EXPR:
	      if (TREE_OPERAND (incr, 0) != decl)
		break;

	      incr_ok = true;
	      if (!fail
		  && TREE_CODE (cond) == NE_EXPR
		  && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
		  && TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)))
		  && (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl))))
		      != INTEGER_CST))
		{
		  /* For pointer to VLA, transform != into < or >
		     depending on whether incr is increment or decrement.  */
		  if (TREE_CODE (incr) == PREINCREMENT_EXPR
		      || TREE_CODE (incr) == POSTINCREMENT_EXPR)
		    TREE_SET_CODE (cond, LT_EXPR);
		  else
		    TREE_SET_CODE (cond, GT_EXPR);
		}
	      incr = c_omp_for_incr_canonicalize_ptr (elocus, decl, incr);
	      break;

	    case COMPOUND_EXPR:
	      if (TREE_CODE (TREE_OPERAND (incr, 0)) != SAVE_EXPR
		  || TREE_CODE (TREE_OPERAND (incr, 1)) != MODIFY_EXPR)
		break;
	      incr = TREE_OPERAND (incr, 1);
	      /* FALLTHRU */
	    case MODIFY_EXPR:
	      if (TREE_OPERAND (incr, 0) != decl)
		break;
	      if (TREE_OPERAND (incr, 1) == decl)
		break;
	      if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
		  && (TREE_OPERAND (TREE_OPERAND (incr, 1), 0) == decl
		      || TREE_OPERAND (TREE_OPERAND (incr, 1), 1) == decl))
		incr_ok = true;
	      else if ((TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR
			|| (TREE_CODE (TREE_OPERAND (incr, 1))
			    == POINTER_PLUS_EXPR))
		       && TREE_OPERAND (TREE_OPERAND (incr, 1), 0) == decl)
		incr_ok = true;
	      else
		{
		  tree t = check_omp_for_incr_expr (elocus,
						    TREE_OPERAND (incr, 1),
						    decl);
		  if (t != error_mark_node)
		    {
		      incr_ok = true;
		      t = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, t);
		      incr = build2 (MODIFY_EXPR, void_type_node, decl, t);
		    }
		}
	      if (!fail
		  && incr_ok
		  && TREE_CODE (cond) == NE_EXPR)
		{
		  tree i = TREE_OPERAND (incr, 1);
		  i = TREE_OPERAND (i, TREE_OPERAND (i, 0) == decl);
		  i = c_fully_fold (i, false, NULL);
		  if (!final_p
		      && TREE_CODE (i) != INTEGER_CST)
		    ;
		  else if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
		    {
		      tree unit
			= TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (decl)));
		      if (unit)
			{
			  enum tree_code ccode = GT_EXPR;
			  unit = c_fully_fold (unit, false, NULL);
			  i = fold_convert (TREE_TYPE (unit), i);
			  if (operand_equal_p (unit, i, 0))
			    ccode = LT_EXPR;
			  if (ccode == GT_EXPR)
			    {
			      i = fold_unary (NEGATE_EXPR, TREE_TYPE (i), i);
			      if (i == NULL_TREE
				  || !operand_equal_p (unit, i, 0))
				{
				  annotation_error (info,
						    as_invalid_increment,
						    incr, elocus,
						    "increment is not constant 1 or "
						    "-1 for %<!=%> condition");
				  fail = true;
				}
			    }
			  if (TREE_CODE (unit) != INTEGER_CST)
			    /* For pointer to VLA, transform != into < or >
			       depending on whether the pointer is
			       incremented or decremented in each
			       iteration.  */
			    TREE_SET_CODE (cond, ccode);
			}
		    }
		  else
		    {
		      if (!integer_onep (i) && !integer_minus_onep (i))
			{
			  annotation_error (info, as_invalid_increment,
					    incr, elocus,
					    "increment is not constant 1 or -1 for"
					    " %<!=%> condition");
			  fail = true;
			}
		    }
		}
	      break;

	    default:
	      break;
	    }
	  if (!incr_ok)
	    {
	      annotation_error (info, as_invalid_increment, incr,
				elocus, "invalid increment expression");
	      fail = true;
	    }
	}

      TREE_VEC_ELT (initv, i) = init;
      TREE_VEC_ELT (incrv, i) = incr;
    }

  if (fail)
    return NULL;
  else
    {
      tree t = make_node (code);

      TREE_TYPE (t) = void_type_node;
      OMP_FOR_INIT (t) = initv;
      OMP_FOR_COND (t) = condv;
      OMP_FOR_INCR (t) = incrv;
      OMP_FOR_BODY (t) = body;
      OMP_FOR_PRE_BODY (t) = pre_body;
      OMP_FOR_ORIG_DECLS (t) = orig_declv;

      SET_EXPR_LOCATION (t, locus);
      return t;
    }
}

/* External entry point to c_finish_omp_for_internal, called from the
   parsers.  See above for description of the arguments.  */

tree
c_finish_omp_for (location_t locus, enum tree_code code, tree declv,
		  tree orig_declv, tree initv, tree condv, tree incrv,
		  tree body, tree pre_body, bool final_p)
{
  return c_finish_omp_for_internal (locus, code, declv,
				    orig_declv, initv, condv, incrv,
				    body, pre_body, final_p, NULL);
}


/* Type for passing data in between c_omp_check_loop_iv and
   c_omp_check_loop_iv_r.  */

struct c_omp_check_loop_iv_data
{
  tree declv;
  bool fail;
  bool maybe_nonrect;
  location_t stmt_loc;
  location_t expr_loc;
  int kind;
  int idx;
  walk_tree_lh lh;
  hash_set<tree> *ppset;
};

/* Return -1 if DECL is not a loop iterator in loop nest D, otherwise
   return the index of the loop in which it is an iterator.
   Return TREE_VEC_LENGTH (d->declv) if it is a C++ range for iterator.  */

static int
c_omp_is_loop_iterator (tree decl, struct c_omp_check_loop_iv_data *d)
{
  for (int i = 0; i < TREE_VEC_LENGTH (d->declv); i++)
    if (decl == TREE_VEC_ELT (d->declv, i)
	|| (TREE_CODE (TREE_VEC_ELT (d->declv, i)) == TREE_LIST
	    && decl == TREE_PURPOSE (TREE_VEC_ELT (d->declv, i))))
      return i;
    else if (TREE_CODE (TREE_VEC_ELT (d->declv, i)) == TREE_LIST
	     && TREE_CHAIN (TREE_VEC_ELT (d->declv, i))
	     && (TREE_CODE (TREE_CHAIN (TREE_VEC_ELT (d->declv, i)))
		 == TREE_VEC)
	     && decl == TREE_VEC_ELT (TREE_CHAIN (TREE_VEC_ELT (d->declv,
						  i)), 2))
      return TREE_VEC_LENGTH (d->declv);
  return -1;
}

/* Helper function called via walk_tree, to diagnose uses
   of associated loop IVs inside of lb, b and incr expressions
   of OpenMP loops.  */
   
static tree
c_omp_check_loop_iv_r (tree *tp, int *walk_subtrees, void *data)
{
  struct c_omp_check_loop_iv_data *d
    = (struct c_omp_check_loop_iv_data *) data;
  if (DECL_P (*tp))
    {
      int idx = c_omp_is_loop_iterator (*tp, d);
      if (idx == -1)
	return NULL_TREE;

      if ((d->kind & 4) && idx < d->idx)
	{
	  d->maybe_nonrect = true;
	  return NULL_TREE;
	}

      if (d->ppset->add (*tp))
	return NULL_TREE;

      location_t loc = d->expr_loc;
      if (loc == UNKNOWN_LOCATION)
	loc = d->stmt_loc;

      switch (d->kind & 3)
	{
	case 0:
	  error_at (loc, "initializer expression refers to "
			 "iteration variable %qD", *tp);
	  break;
	case 1:
	  error_at (loc, "condition expression refers to "
			 "iteration variable %qD", *tp);
	  break;
	case 2:
	  error_at (loc, "increment expression refers to "
			 "iteration variable %qD", *tp);
	  break;
	}
      d->fail = true;
    }
  else if ((d->kind & 4)
	   && TREE_CODE (*tp) != TREE_VEC
	   && TREE_CODE (*tp) != PLUS_EXPR
	   && TREE_CODE (*tp) != MINUS_EXPR
	   && TREE_CODE (*tp) != MULT_EXPR
	   && TREE_CODE (*tp) != POINTER_PLUS_EXPR
	   && !CONVERT_EXPR_P (*tp))
    {
      *walk_subtrees = 0;
      d->kind &= 3;
      walk_tree_1 (tp, c_omp_check_loop_iv_r, data, NULL, d->lh);
      d->kind |= 4;
      return NULL_TREE;
    }
  else if (d->ppset->add (*tp))
    *walk_subtrees = 0;
  /* Don't walk dtors added by C++ wrap_cleanups_r.  */
  else if (TREE_CODE (*tp) == TRY_CATCH_EXPR
	   && TRY_CATCH_IS_CLEANUP (*tp))
    {
      *walk_subtrees = 0;
      return walk_tree_1 (&TREE_OPERAND (*tp, 0), c_omp_check_loop_iv_r, data,
			  NULL, d->lh);
    }

  return NULL_TREE;
}

/* Check the allowed expressions for non-rectangular loop nest lb and b
   expressions.  Return the outer var decl referenced in the expression.  */

static tree
c_omp_check_nonrect_loop_iv (tree *tp, struct c_omp_check_loop_iv_data *d,
			     walk_tree_lh lh)
{
  d->maybe_nonrect = false;
  if (d->fail)
    return NULL_TREE;

  hash_set<tree> pset;
  hash_set<tree> *ppset = d->ppset;
  d->ppset = &pset;

  tree t = *tp;
  if (TREE_CODE (t) == TREE_VEC
      && TREE_VEC_LENGTH (t) == 3
      && DECL_P (TREE_VEC_ELT (t, 0))
      && c_omp_is_loop_iterator (TREE_VEC_ELT (t, 0), d) >= 0)
    {
      d->kind &= 3;
      walk_tree_1 (&TREE_VEC_ELT (t, 1), c_omp_check_loop_iv_r, d, NULL, lh);
      walk_tree_1 (&TREE_VEC_ELT (t, 1), c_omp_check_loop_iv_r, d, NULL, lh);
      d->ppset = ppset;
      return d->fail ? NULL_TREE : TREE_VEC_ELT (t, 0);
    }

  while (CONVERT_EXPR_P (t))
    t = TREE_OPERAND (t, 0);

  tree a1 = t, a2 = integer_zero_node;
  bool neg_a1 = false, neg_a2 = false;
  switch (TREE_CODE (t))
    {
    case PLUS_EXPR:
    case MINUS_EXPR:
      a1 = TREE_OPERAND (t, 0);
      a2 = TREE_OPERAND (t, 1);
      while (CONVERT_EXPR_P (a1))
	a1 = TREE_OPERAND (a1, 0);
      while (CONVERT_EXPR_P (a2))
	a2 = TREE_OPERAND (a2, 0);
      if (DECL_P (a1) && c_omp_is_loop_iterator (a1, d) >= 0)
	{
	  a2 = TREE_OPERAND (t, 1);
	  if (TREE_CODE (t) == MINUS_EXPR)
	    neg_a2 = true;
	  t = a1;
	  break;
	}
      if (DECL_P (a2) && c_omp_is_loop_iterator (a2, d) >= 0)
	{
	  a1 = TREE_OPERAND (t, 0);
	  if (TREE_CODE (t) == MINUS_EXPR)
	    neg_a1 = true;
	  t = a2;
	  a2 = a1;
	  break;
	}
      if (TREE_CODE (a1) == MULT_EXPR && TREE_CODE (a2) == MULT_EXPR)
	{
	  tree o1 = TREE_OPERAND (a1, 0);
	  tree o2 = TREE_OPERAND (a1, 1);
	  while (CONVERT_EXPR_P (o1))
	    o1 = TREE_OPERAND (o1, 0);
	  while (CONVERT_EXPR_P (o2))
	    o2 = TREE_OPERAND (o2, 0);
	  if ((DECL_P (o1) && c_omp_is_loop_iterator (o1, d) >= 0)
	      || (DECL_P (o2) && c_omp_is_loop_iterator (o2, d) >= 0))
	    {
	      a2 = TREE_OPERAND (t, 1);
	      if (TREE_CODE (t) == MINUS_EXPR)
		neg_a2 = true;
	      t = a1;
	      break;
	    }
	}
      if (TREE_CODE (a2) == MULT_EXPR)
	{
	  a1 = TREE_OPERAND (t, 0);
	  if (TREE_CODE (t) == MINUS_EXPR)
	    neg_a1 = true;
	  t = a2;
	  a2 = a1;
	  break;
	}
      if (TREE_CODE (a1) == MULT_EXPR)
	{
	  a2 = TREE_OPERAND (t, 1);
	  if (TREE_CODE (t) == MINUS_EXPR)
	    neg_a2 = true;
	  t = a1;
	  break;
	}
      a2 = integer_zero_node;
      break;
    case POINTER_PLUS_EXPR:
      a1 = TREE_OPERAND (t, 0);
      a2 = TREE_OPERAND (t, 1);
      while (CONVERT_EXPR_P (a1))
	a1 = TREE_OPERAND (a1, 0);
      if (DECL_P (a1) && c_omp_is_loop_iterator (a1, d) >= 0)
	{
	  a2 = TREE_OPERAND (t, 1);
	  t = a1;
	  break;
	}
      break;
    default:
      break;
    }

  a1 = integer_one_node;
  if (TREE_CODE (t) == MULT_EXPR)
    {
      tree o1 = TREE_OPERAND (t, 0);
      tree o2 = TREE_OPERAND (t, 1);
      while (CONVERT_EXPR_P (o1))
	o1 = TREE_OPERAND (o1, 0);
      while (CONVERT_EXPR_P (o2))
	o2 = TREE_OPERAND (o2, 0);
      if (DECL_P (o1) && c_omp_is_loop_iterator (o1, d) >= 0)
	{
	  a1 = TREE_OPERAND (t, 1);
	  t = o1;
	}
      else if (DECL_P (o2) && c_omp_is_loop_iterator (o2, d) >= 0)
	{
	  a1 = TREE_OPERAND (t, 0);
	  t = o2;
	}
    }

  d->kind &= 3;
  tree ret = NULL_TREE;
  if (DECL_P (t) && c_omp_is_loop_iterator (t, d) >= 0)
    {
      location_t loc = d->expr_loc;
      if (loc == UNKNOWN_LOCATION)
	loc = d->stmt_loc;
      if (!lang_hooks.types_compatible_p (TREE_TYPE (*tp), TREE_TYPE (t)))
	{
	  if (d->kind == 0)
	    error_at (loc, "outer iteration variable %qD used in initializer"
			   " expression has type other than %qT",
		      t, TREE_TYPE (*tp));
	  else
	    error_at (loc, "outer iteration variable %qD used in condition"
			   " expression has type other than %qT",
		      t, TREE_TYPE (*tp));
	  d->fail = true;
	}
      else if (!INTEGRAL_TYPE_P (TREE_TYPE (a1)))
	{
	  error_at (loc, "outer iteration variable %qD multiplier expression"
			 " %qE is not integral", t, a1);
	  d->fail = true;
	}
      else if (!INTEGRAL_TYPE_P (TREE_TYPE (a2)))
	{
	  error_at (loc, "outer iteration variable %qD addend expression"
			 " %qE is not integral", t, a2);
	  d->fail = true;
	}
      else
	{
	  walk_tree_1 (&a1, c_omp_check_loop_iv_r, d, NULL, lh);
	  walk_tree_1 (&a2, c_omp_check_loop_iv_r, d, NULL, lh);
        }
      if (!d->fail)
	{
	  a1 = fold_convert (TREE_TYPE (*tp), a1);
	  a2 = fold_convert (TREE_TYPE (*tp), a2);
	  if (neg_a1)
	    a1 = fold_build1 (NEGATE_EXPR, TREE_TYPE (a1), a1);
	  if (neg_a2)
	    a2 = fold_build1 (NEGATE_EXPR, TREE_TYPE (a2), a2);
	  ret = t;
	  *tp = make_tree_vec (3);
	  TREE_VEC_ELT (*tp, 0) = t;
	  TREE_VEC_ELT (*tp, 1) = a1;
	  TREE_VEC_ELT (*tp, 2) = a2;
	}
    }
  else
    walk_tree_1 (&t, c_omp_check_loop_iv_r, d, NULL, lh);

  d->ppset = ppset;
  return ret;
}

/* Diagnose invalid references to loop iterators in lb, b and incr
   expressions.  */

bool
c_omp_check_loop_iv (tree stmt, tree declv, walk_tree_lh lh)
{
  hash_set<tree> pset;
  struct c_omp_check_loop_iv_data data;
  int i;

  data.declv = declv;
  data.fail = false;
  data.maybe_nonrect = false;
  data.stmt_loc = EXPR_LOCATION (stmt);
  data.lh = lh;
  data.ppset = &pset;
  for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (stmt)); i++)
    {
      tree init = TREE_VEC_ELT (OMP_FOR_INIT (stmt), i);
      gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
      tree decl = TREE_OPERAND (init, 0);
      tree cond = TREE_VEC_ELT (OMP_FOR_COND (stmt), i);
      gcc_assert (COMPARISON_CLASS_P (cond));
      gcc_assert (TREE_OPERAND (cond, 0) == decl);
      tree incr = TREE_VEC_ELT (OMP_FOR_INCR (stmt), i);
      data.expr_loc = EXPR_LOCATION (TREE_OPERAND (init, 1));
      tree vec_outer1 = NULL_TREE, vec_outer2 = NULL_TREE;
      int kind = 0;
      if (i > 0
	  && (unsigned) c_omp_is_loop_iterator (decl, &data) < (unsigned) i)
	{
	  location_t loc = data.expr_loc;
	  if (loc == UNKNOWN_LOCATION)
	    loc = data.stmt_loc;
	  error_at (loc, "the same loop iteration variables %qD used in "
			 "multiple associated loops", decl);
	  data.fail = true;
	}
      /* Handle non-rectangular loop nests.  */
      if (TREE_CODE (stmt) != OACC_LOOP && i > 0)
	kind = 4;
      data.kind = kind;
      data.idx = i;
      walk_tree_1 (&TREE_OPERAND (init, 1),
		   c_omp_check_loop_iv_r, &data, NULL, lh);
      if (data.maybe_nonrect)
	vec_outer1 = c_omp_check_nonrect_loop_iv (&TREE_OPERAND (init, 1),
						  &data, lh);
      /* Don't warn for C++ random access iterators here, the
	 expression then involves the subtraction and always refers
	 to the original value.  The C++ FE needs to warn on those
	 earlier.  */
      if (decl == TREE_VEC_ELT (declv, i)
	  || (TREE_CODE (TREE_VEC_ELT (declv, i)) == TREE_LIST
	      && decl == TREE_PURPOSE (TREE_VEC_ELT (declv, i))))
	{
	  data.expr_loc = EXPR_LOCATION (cond);
	  data.kind = kind | 1;
	  walk_tree_1 (&TREE_OPERAND (cond, 1),
		       c_omp_check_loop_iv_r, &data, NULL, lh);
	  if (data.maybe_nonrect)
	    vec_outer2 = c_omp_check_nonrect_loop_iv (&TREE_OPERAND (cond, 1),
						      &data, lh);
	}
      if (vec_outer1 && vec_outer2 && vec_outer1 != vec_outer2)
	{
	  location_t loc = data.expr_loc;
	  if (loc == UNKNOWN_LOCATION)
	    loc = data.stmt_loc;
	  error_at (loc, "two different outer iteration variables %qD and %qD"
			 " used in a single loop", vec_outer1, vec_outer2);
	  data.fail = true;
	}
      if (vec_outer1 || vec_outer2)
	OMP_FOR_NON_RECTANGULAR (stmt) = 1;
      if (TREE_CODE (incr) == MODIFY_EXPR)
	{
	  gcc_assert (TREE_OPERAND (incr, 0) == decl);
	  incr = TREE_OPERAND (incr, 1);
	  data.kind = 2;
	  if (TREE_CODE (incr) == PLUS_EXPR
	      && TREE_OPERAND (incr, 1) == decl)
	    {
	      data.expr_loc = EXPR_LOCATION (TREE_OPERAND (incr, 0));
	      walk_tree_1 (&TREE_OPERAND (incr, 0),
			   c_omp_check_loop_iv_r, &data, NULL, lh);
	    }
	  else
	    {
	      data.expr_loc = EXPR_LOCATION (TREE_OPERAND (incr, 1));
	      walk_tree_1 (&TREE_OPERAND (incr, 1),
			   c_omp_check_loop_iv_r, &data, NULL, lh);
	    }
	}
    }
  return !data.fail;
}

/* Similar, but allows to check the init or cond expressions individually.  */

bool
c_omp_check_loop_iv_exprs (location_t stmt_loc, enum tree_code code,
			   tree declv, int i, tree decl, tree init, tree cond,
			   walk_tree_lh lh)
{
  hash_set<tree> pset;
  struct c_omp_check_loop_iv_data data;
  int kind = (code != OACC_LOOP && i > 0) ? 4 : 0;

  data.declv = declv;
  data.fail = false;
  data.maybe_nonrect = false;
  data.stmt_loc = stmt_loc;
  data.lh = lh;
  data.ppset = &pset;
  data.idx = i;
  if (i > 0
      && (unsigned) c_omp_is_loop_iterator (decl, &data) < (unsigned) i)
    {
      error_at (stmt_loc, "the same loop iteration variables %qD used in "
      			  "multiple associated loops", decl);
      data.fail = true;
    }
  if (init)
    {
      data.expr_loc = EXPR_LOCATION (init);
      data.kind = kind;
      walk_tree_1 (&init,
		   c_omp_check_loop_iv_r, &data, NULL, lh);
    }
  if (cond)
    {
      gcc_assert (COMPARISON_CLASS_P (cond));
      data.expr_loc = EXPR_LOCATION (init);
      data.kind = kind | 1;
      if (TREE_OPERAND (cond, 0) == decl)
	walk_tree_1 (&TREE_OPERAND (cond, 1),
		     c_omp_check_loop_iv_r, &data, NULL, lh);
      else
	walk_tree_1 (&TREE_OPERAND (cond, 0),
		     c_omp_check_loop_iv_r, &data, NULL, lh);
    }
  return !data.fail;
}

/* This function splits clauses for OpenACC combined loop
   constructs.  OpenACC combined loop constructs are:
   #pragma acc kernels loop
   #pragma acc parallel loop  */

tree
c_oacc_split_loop_clauses (tree clauses, tree *not_loop_clauses,
			   bool is_parallel)
{
  tree next, loop_clauses, nc;

  loop_clauses = *not_loop_clauses = NULL_TREE;
  for (; clauses ; clauses = next)
    {
      next = OMP_CLAUSE_CHAIN (clauses);

      switch (OMP_CLAUSE_CODE (clauses))
        {
	  /* Loop clauses.  */
	case OMP_CLAUSE_COLLAPSE:
	case OMP_CLAUSE_OACC_TILE:
	case OMP_CLAUSE_GANG:
	case OMP_CLAUSE_WORKER:
	case OMP_CLAUSE_VECTOR:
	case OMP_CLAUSE_AUTO:
	case OMP_CLAUSE_SEQ:
	case OMP_CLAUSE_INDEPENDENT:
	case OMP_CLAUSE_PRIVATE:
	  OMP_CLAUSE_CHAIN (clauses) = loop_clauses;
	  loop_clauses = clauses;
	  break;

	  /* Reductions must be duplicated on both constructs.  */
	case OMP_CLAUSE_REDUCTION:
	  if (is_parallel)
	    {
	      nc = build_omp_clause (OMP_CLAUSE_LOCATION (clauses),
				     OMP_CLAUSE_REDUCTION);
	      OMP_CLAUSE_DECL (nc) = OMP_CLAUSE_DECL (clauses);
	      OMP_CLAUSE_REDUCTION_CODE (nc)
		= OMP_CLAUSE_REDUCTION_CODE (clauses);
	      OMP_CLAUSE_CHAIN (nc) = *not_loop_clauses;
	      *not_loop_clauses = nc;
	    }

	  OMP_CLAUSE_CHAIN (clauses) = loop_clauses;
	  loop_clauses = clauses;
	  break;

	  /* Parallel/kernels clauses.  */
	default:
	  OMP_CLAUSE_CHAIN (clauses) = *not_loop_clauses;
	  *not_loop_clauses = clauses;
	  break;
	}
    }

  return loop_clauses;
}

/* This function attempts to split or duplicate clauses for OpenMP
   combined/composite constructs.  Right now there are 30 different
   constructs.  CODE is the innermost construct in the combined construct,
   and MASK allows to determine which constructs are combined together,
   as every construct has at least one clause that no other construct
   has (except for OMP_SECTIONS, but that can be only combined with parallel,
   and OMP_MASTER, which doesn't have any clauses at all).
   OpenMP combined/composite constructs are:
   #pragma omp distribute parallel for
   #pragma omp distribute parallel for simd
   #pragma omp distribute simd
   #pragma omp for simd
   #pragma omp masked taskloop
   #pragma omp masked taskloop simd
   #pragma omp master taskloop
   #pragma omp master taskloop simd
   #pragma omp parallel for
   #pragma omp parallel for simd
   #pragma omp parallel loop
   #pragma omp parallel masked
   #pragma omp parallel masked taskloop
   #pragma omp parallel masked taskloop simd
   #pragma omp parallel master
   #pragma omp parallel master taskloop
   #pragma omp parallel master taskloop simd
   #pragma omp parallel sections
   #pragma omp target parallel
   #pragma omp target parallel for
   #pragma omp target parallel for simd
   #pragma omp target parallel loop
   #pragma omp target teams
   #pragma omp target teams distribute
   #pragma omp target teams distribute parallel for
   #pragma omp target teams distribute parallel for simd
   #pragma omp target teams distribute simd
   #pragma omp target teams loop
   #pragma omp target simd
   #pragma omp taskloop simd
   #pragma omp teams distribute
   #pragma omp teams distribute parallel for
   #pragma omp teams distribute parallel for simd
   #pragma omp teams distribute simd
   #pragma omp teams loop  */

void
c_omp_split_clauses (location_t loc, enum tree_code code,
		     omp_clause_mask mask, tree clauses, tree *cclauses)
{
  tree next, c;
  enum c_omp_clause_split s;
  int i;
  bool has_dup_allocate = false;

  for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
    cclauses[i] = NULL;
  /* Add implicit nowait clause on
     #pragma omp parallel {for,for simd,sections}.  */
  if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS)) != 0)
    switch (code)
      {
      case OMP_FOR:
      case OMP_SIMD:
	if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE)) != 0)
	  cclauses[C_OMP_CLAUSE_SPLIT_FOR]
	    = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
	break;
      case OMP_SECTIONS:
	cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS]
	  = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
	break;
      default:
	break;
      }

  for (; clauses ; clauses = next)
    {
      next = OMP_CLAUSE_CHAIN (clauses);

      switch (OMP_CLAUSE_CODE (clauses))
	{
	/* First the clauses that are unique to some constructs.  */
	case OMP_CLAUSE_DEVICE:
	case OMP_CLAUSE_MAP:
	case OMP_CLAUSE_IS_DEVICE_PTR:
	case OMP_CLAUSE_HAS_DEVICE_ADDR:
	case OMP_CLAUSE_DEFAULTMAP:
	case OMP_CLAUSE_DEPEND:
	case OMP_CLAUSE_USES_ALLOCATORS:
	  s = C_OMP_CLAUSE_SPLIT_TARGET;
	  break;
	case OMP_CLAUSE_DOACROSS:
	  /* This can happen with invalid depend(source) or
	     depend(sink:vec) on target combined with other constructs.  */
	  gcc_assert (OMP_CLAUSE_DOACROSS_DEPEND (clauses));
	  s = C_OMP_CLAUSE_SPLIT_TARGET;
	  break;
	case OMP_CLAUSE_NUM_TEAMS:
	  s = C_OMP_CLAUSE_SPLIT_TEAMS;
	  break;
	case OMP_CLAUSE_DIST_SCHEDULE:
	  s = C_OMP_CLAUSE_SPLIT_DISTRIBUTE;
	  break;
	case OMP_CLAUSE_COPYIN:
	case OMP_CLAUSE_NUM_THREADS:
	case OMP_CLAUSE_PROC_BIND:
	  s = C_OMP_CLAUSE_SPLIT_PARALLEL;
	  break;
	case OMP_CLAUSE_ORDERED:
	  s = C_OMP_CLAUSE_SPLIT_FOR;
	  break;
	case OMP_CLAUSE_SCHEDULE:
	  s = C_OMP_CLAUSE_SPLIT_FOR;
	  if (code != OMP_SIMD)
	    OMP_CLAUSE_SCHEDULE_SIMD (clauses) = 0;
	  break;
	case OMP_CLAUSE_SAFELEN:
	case OMP_CLAUSE_SIMDLEN:
	case OMP_CLAUSE_ALIGNED:
	case OMP_CLAUSE_NONTEMPORAL:
	  s = C_OMP_CLAUSE_SPLIT_SIMD;
	  break;
	case OMP_CLAUSE_GRAINSIZE:
	case OMP_CLAUSE_NUM_TASKS:
	case OMP_CLAUSE_FINAL:
	case OMP_CLAUSE_UNTIED:
	case OMP_CLAUSE_MERGEABLE:
	case OMP_CLAUSE_NOGROUP:
	case OMP_CLAUSE_PRIORITY:
	  s = C_OMP_CLAUSE_SPLIT_TASKLOOP;
	  break;
	case OMP_CLAUSE_BIND:
	  s = C_OMP_CLAUSE_SPLIT_LOOP;
	  break;
	case OMP_CLAUSE_FILTER:
	  s = C_OMP_CLAUSE_SPLIT_MASKED;
	  break;
	/* Duplicate this to all of taskloop, distribute, for, simd and
	   loop.  */
	case OMP_CLAUSE_COLLAPSE:
	  if (code == OMP_SIMD)
	    {
	      if ((mask & ((OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE)
			   | (OMP_CLAUSE_MASK_1
			      << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)
			   | (OMP_CLAUSE_MASK_1
			      << PRAGMA_OMP_CLAUSE_NOGROUP))) != 0)
		{
		  c = build_omp_clause (OMP_CLAUSE_LOCATION (clauses),
					OMP_CLAUSE_COLLAPSE);
		  OMP_CLAUSE_COLLAPSE_EXPR (c)
		    = OMP_CLAUSE_COLLAPSE_EXPR (clauses);
		  OMP_CLAUSE_CHAIN (c) = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
		  cclauses[C_OMP_CLAUSE_SPLIT_SIMD] = c;
		}
	      else
		{
		  /* This must be #pragma omp target simd */
		  s = C_OMP_CLAUSE_SPLIT_SIMD;
		  break;
		}
	    }
	  if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE)) != 0)
	    {
	      if ((mask & (OMP_CLAUSE_MASK_1
			   << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
		{
		  c = build_omp_clause (OMP_CLAUSE_LOCATION (clauses),
					OMP_CLAUSE_COLLAPSE);
		  OMP_CLAUSE_COLLAPSE_EXPR (c)
		    = OMP_CLAUSE_COLLAPSE_EXPR (clauses);
		  OMP_CLAUSE_CHAIN (c) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
		  cclauses[C_OMP_CLAUSE_SPLIT_FOR] = c;
		  s = C_OMP_CLAUSE_SPLIT_DISTRIBUTE;
		}
	      else
		s = C_OMP_CLAUSE_SPLIT_FOR;
	    }
	  else if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP))
		   != 0)
	    s = C_OMP_CLAUSE_SPLIT_TASKLOOP;
	  else if (code == OMP_LOOP)
	    s = C_OMP_CLAUSE_SPLIT_LOOP;
	  else
	    s = C_OMP_CLAUSE_SPLIT_DISTRIBUTE;
	  break;
	/* Private clause is supported on all constructs but master/masked,
	   it is enough to put it on the innermost one other than
	   master/masked.  For #pragma omp {for,sections} put it on parallel
	   though, as that's what we did for OpenMP 3.1.  */
	case OMP_CLAUSE_PRIVATE:
	  switch (code)
	    {
	    case OMP_SIMD: s = C_OMP_CLAUSE_SPLIT_SIMD; break;
	    case OMP_FOR: case OMP_SECTIONS:
	    case OMP_PARALLEL: s = C_OMP_CLAUSE_SPLIT_PARALLEL; break;
	    case OMP_DISTRIBUTE: s = C_OMP_CLAUSE_SPLIT_DISTRIBUTE; break;
	    case OMP_TEAMS: s = C_OMP_CLAUSE_SPLIT_TEAMS; break;
	    case OMP_MASTER: s = C_OMP_CLAUSE_SPLIT_PARALLEL; break;
	    case OMP_MASKED: s = C_OMP_CLAUSE_SPLIT_PARALLEL; break;
	    case OMP_TASKLOOP: s = C_OMP_CLAUSE_SPLIT_TASKLOOP; break;
	    case OMP_LOOP: s = C_OMP_CLAUSE_SPLIT_LOOP; break;
	    default: gcc_unreachable ();
	    }
	  break;
	/* Firstprivate clause is supported on all constructs but
	   simd, master, masked and loop.  Put it on the outermost of those
	   and duplicate on teams and parallel.  */
	case OMP_CLAUSE_FIRSTPRIVATE:
	  if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP))
	      != 0)
	    {
	      if (code == OMP_SIMD
		  && (mask & ((OMP_CLAUSE_MASK_1
			       << PRAGMA_OMP_CLAUSE_NUM_THREADS)
			      | (OMP_CLAUSE_MASK_1
				 << PRAGMA_OMP_CLAUSE_NUM_TEAMS))) == 0)
		{
		  /* This must be #pragma omp target simd.  */
		  s = C_OMP_CLAUSE_SPLIT_TARGET;
		  OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (clauses) = 1;
		  OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (clauses) = 1;
		  break;
		}
	      c = build_omp_clause (OMP_CLAUSE_LOCATION (clauses),
				    OMP_CLAUSE_FIRSTPRIVATE);
	      /* firstprivate should not be applied to target if it is
		 also lastprivate or on the combined/composite construct,
		 or if it is mentioned in map clause.  OMP_CLAUSE_DECLs
		 may need to go through FE handling though (instantiation,
		 C++ non-static data members, array section lowering), so
		 add the clause with OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT and
		 let *finish_omp_clauses and the gimplifier handle it
		 right.  */
	      OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c) = 1;
	      OMP_CLAUSE_DECL (c) = OMP_CLAUSE_DECL (clauses);
	      OMP_CLAUSE_CHAIN (c) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
	      cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = c;
	    }
	  if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS))
	      != 0)
	    {
	      if ((mask & ((OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS)
			   | (OMP_CLAUSE_MASK_1
			      << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE))) != 0)
		{
		  c = build_omp_clause (OMP_CLAUSE_LOCATION (clauses),
					OMP_CLAUSE_FIRSTPRIVATE);
		  OMP_CLAUSE_DECL (c) = OMP_CLAUSE_DECL (clauses);
		  OMP_CLAUSE_CHAIN (c) = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
		  cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] = c;
		  if ((mask & (OMP_CLAUSE_MASK_1
			       << PRAGMA_OMP_CLAUSE_NUM_TEAMS)) != 0)
		    s = C_OMP_CLAUSE_SPLIT_TEAMS;
		  else
		    s = C_OMP_CLAUSE_SPLIT_DISTRIBUTE;
		}
	      else if ((mask & (OMP_CLAUSE_MASK_1
				<< PRAGMA_OMP_CLAUSE_NOGROUP)) != 0)
		/* This must be
		   #pragma omp parallel mas{ked,ter} taskloop{, simd}.  */
		s = C_OMP_CLAUSE_SPLIT_TASKLOOP;
	      else
		/* This must be
		   #pragma omp parallel{, for{, simd}, sections,loop}
		   or
		   #pragma omp target parallel.  */
		s = C_OMP_CLAUSE_SPLIT_PARALLEL;
	    }
	  else if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS))
		   != 0)
	    {
	      /* This must be one of
		 #pragma omp {,target }teams {distribute,loop}
		 #pragma omp target teams
		 #pragma omp {,target }teams distribute simd.  */
	      gcc_assert (code == OMP_DISTRIBUTE
			  || code == OMP_LOOP
			  || code == OMP_TEAMS
			  || code == OMP_SIMD);
	      s = C_OMP_CLAUSE_SPLIT_TEAMS;
	    }
	  else if ((mask & (OMP_CLAUSE_MASK_1
			    << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
	    {
	      /* This must be #pragma omp distribute simd.  */
	      gcc_assert (code == OMP_SIMD);
	      s = C_OMP_CLAUSE_SPLIT_DISTRIBUTE;
	    }
	  else if ((mask & (OMP_CLAUSE_MASK_1
			    << PRAGMA_OMP_CLAUSE_NOGROUP)) != 0)
	    {
	      /* This must be
		 #pragma omp {,{,parallel }mas{ked,ter} }taskloop simd
		 or
		 #pragma omp {,parallel }mas{ked,ter} taskloop.  */
	      gcc_assert (code == OMP_SIMD || code == OMP_TASKLOOP);
	      s = C_OMP_CLAUSE_SPLIT_TASKLOOP;
	    }
	  else
	    {
	      /* This must be #pragma omp for simd.  */
	      gcc_assert (code == OMP_SIMD);
	      s = C_OMP_CLAUSE_SPLIT_FOR;
	    }
	  break;
	/* Lastprivate is allowed on distribute, for, sections, taskloop, loop
	   and simd.  In parallel {for{, simd},sections} we actually want to
	   put it on parallel rather than for or sections.  */
	case OMP_CLAUSE_LASTPRIVATE:
	  if (code == OMP_DISTRIBUTE)
	    {
	      s = C_OMP_CLAUSE_SPLIT_DISTRIBUTE;
	      break;
	    }
	  if ((mask & (OMP_CLAUSE_MASK_1
		       << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
	    {
	      c = build_omp_clause (OMP_CLAUSE_LOCATION (clauses),
				    OMP_CLAUSE_LASTPRIVATE);
	      OMP_CLAUSE_DECL (c) = OMP_CLAUSE_DECL (clauses);
	      OMP_CLAUSE_CHAIN (c) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
	      OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c)
		= OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (clauses);
	      cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE] = c;
	    }
	  if (code == OMP_FOR || code == OMP_SECTIONS)
	    {
	      if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS))
		  != 0)
		s = C_OMP_CLAUSE_SPLIT_PARALLEL;
	      else
		s = C_OMP_CLAUSE_SPLIT_FOR;
	      break;
	    }
	  if (code == OMP_TASKLOOP)
	    {
	      s = C_OMP_CLAUSE_SPLIT_TASKLOOP;
	      break;
	    }
	  if (code == OMP_LOOP)
	    {
	      s = C_OMP_CLAUSE_SPLIT_LOOP;
	      break;
	    }
	  gcc_assert (code == OMP_SIMD);
	  if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE)) != 0)
	    {
	      c = build_omp_clause (OMP_CLAUSE_LOCATION (clauses),
				    OMP_CLAUSE_LASTPRIVATE);
	      OMP_CLAUSE_DECL (c) = OMP_CLAUSE_DECL (clauses);
	      OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c)
		= OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (clauses);
	      if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS))
		  != 0)
		s = C_OMP_CLAUSE_SPLIT_PARALLEL;
	      else
		s = C_OMP_CLAUSE_SPLIT_FOR;
	      OMP_CLAUSE_CHAIN (c) = cclauses[s];
	      cclauses[s] = c;
	    }
	  if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP)) != 0)
	    {
	      c = build_omp_clause (OMP_CLAUSE_LOCATION (clauses),
				    OMP_CLAUSE_LASTPRIVATE);
	      OMP_CLAUSE_DECL (c) = OMP_CLAUSE_DECL (clauses);
	      OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c)
		= OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (clauses);
	      OMP_CLAUSE_CHAIN (c) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
	      cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP] = c;
	    }
	  s = C_OMP_CLAUSE_SPLIT_SIMD;
	  break;
	/* Shared and default clauses are allowed on parallel, teams and
	   taskloop.  */
	case OMP_CLAUSE_SHARED:
	case OMP_CLAUSE_DEFAULT:
	  if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP))
	      != 0)
	    {
	      if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS))
		  != 0)
		{
		  c = build_omp_clause (OMP_CLAUSE_LOCATION (clauses),
					OMP_CLAUSE_CODE (clauses));
		  if (OMP_CLAUSE_CODE (clauses) == OMP_CLAUSE_SHARED)
		    OMP_CLAUSE_DECL (c) = OMP_CLAUSE_DECL (clauses);
		  else
		    OMP_CLAUSE_DEFAULT_KIND (c)
		      = OMP_CLAUSE_DEFAULT_KIND (clauses);
		  OMP_CLAUSE_CHAIN (c) = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
		  cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] = c;
		}
	      s = C_OMP_CLAUSE_SPLIT_TASKLOOP;
	      break;
	    }
	  if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS))
	      != 0)
	    {
	      if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS))
		  == 0)
		{
		  s = C_OMP_CLAUSE_SPLIT_TEAMS;
		  break;
		}
	      c = build_omp_clause (OMP_CLAUSE_LOCATION (clauses),
				    OMP_CLAUSE_CODE (clauses));
	      if (OMP_CLAUSE_CODE (clauses) == OMP_CLAUSE_SHARED)
		OMP_CLAUSE_DECL (c) = OMP_CLAUSE_DECL (clauses);
	      else
		OMP_CLAUSE_DEFAULT_KIND (c)
		  = OMP_CLAUSE_DEFAULT_KIND (clauses);
	      OMP_CLAUSE_CHAIN (c) = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
	      cclauses[C_OMP_CLAUSE_SPLIT_TEAMS] = c;
	    }
	  s = C_OMP_CLAUSE_SPLIT_PARALLEL;
	  break;
	/* order clauses are allowed on distribute, for, simd and loop.  */
	case OMP_CLAUSE_ORDER:
	  if ((mask & (OMP_CLAUSE_MASK_1
		       << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
	    {
	      if (code == OMP_DISTRIBUTE)
		{
		  s = C_OMP_CLAUSE_SPLIT_DISTRIBUTE;
		  break;
		}
	      c = build_omp_clause (OMP_CLAUSE_LOCATION (clauses),
				    OMP_CLAUSE_ORDER);
	      OMP_CLAUSE_ORDER_UNCONSTRAINED (c)
		= OMP_CLAUSE_ORDER_UNCONSTRAINED (clauses);
	      OMP_CLAUSE_ORDER_REPRODUCIBLE (c)
		= OMP_CLAUSE_ORDER_REPRODUCIBLE (clauses);
	      OMP_CLAUSE_CHAIN (c) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
	      cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE] = c;
	    }
	  if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE)) != 0)
	    {
	      if (code == OMP_SIMD)
		{
		  c = build_omp_clause (OMP_CLAUSE_LOCATION (clauses),
					OMP_CLAUSE_ORDER);
		  OMP_CLAUSE_ORDER_UNCONSTRAINED (c)
		    = OMP_CLAUSE_ORDER_UNCONSTRAINED (clauses);
		  OMP_CLAUSE_ORDER_REPRODUCIBLE (c)
		    = OMP_CLAUSE_ORDER_REPRODUCIBLE (clauses);
		  OMP_CLAUSE_CHAIN (c) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
		  cclauses[C_OMP_CLAUSE_SPLIT_FOR] = c;
		  s = C_OMP_CLAUSE_SPLIT_SIMD;
		}
	      else
		s = C_OMP_CLAUSE_SPLIT_FOR;
	    }
	  else if (code == OMP_LOOP)
	    s = C_OMP_CLAUSE_SPLIT_LOOP;
	  else
	    s = C_OMP_CLAUSE_SPLIT_SIMD;
	  break;
	/* Reduction is allowed on simd, for, parallel, sections, taskloop,
	   teams and loop.  Duplicate it on all of them, but omit on for or
	   sections if parallel is present (unless inscan, in that case
	   omit on parallel).  If taskloop or loop is combined with
	   parallel, omit it on parallel.  */
	case OMP_CLAUSE_REDUCTION:
	  if (OMP_CLAUSE_REDUCTION_TASK (clauses))
	    {
	      if (code == OMP_SIMD || code == OMP_LOOP)
		{
		  error_at (OMP_CLAUSE_LOCATION (clauses),
			    "invalid %<task%> reduction modifier on construct "
			    "combined with %<simd%> or %<loop%>");
		  OMP_CLAUSE_REDUCTION_TASK (clauses) = 0;
		}
	      else if (code != OMP_SECTIONS
		       && (mask & (OMP_CLAUSE_MASK_1
				   << PRAGMA_OMP_CLAUSE_NUM_THREADS)) == 0
		       && (mask & (OMP_CLAUSE_MASK_1
				   << PRAGMA_OMP_CLAUSE_SCHEDULE)) == 0)
		{
		  error_at (OMP_CLAUSE_LOCATION (clauses),
			    "invalid %<task%> reduction modifier on construct "
			    "not combined with %<parallel%>, %<for%> or "
			    "%<sections%>");
		  OMP_CLAUSE_REDUCTION_TASK (clauses) = 0;
		}
	    }
	  if (OMP_CLAUSE_REDUCTION_INSCAN (clauses)
	      && ((mask & ((OMP_CLAUSE_MASK_1
			    << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)
			   | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)))
		  != 0))
	    {
	      error_at (OMP_CLAUSE_LOCATION (clauses),
			"%<inscan%> %<reduction%> clause on construct other "
			"than %<for%>, %<simd%>, %<for simd%>, "
			"%<parallel for%>, %<parallel for simd%>");
	      OMP_CLAUSE_REDUCTION_INSCAN (clauses) = 0;
	    }
	  if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0)
	    {
	      c = build_omp_clause (OMP_CLAUSE_LOCATION (clauses),
				    OMP_CLAUSE_MAP);
	      OMP_CLAUSE_DECL (c) = OMP_CLAUSE_DECL (clauses);
	      OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TOFROM);
	      OMP_CLAUSE_MAP_IMPLICIT (c) = 1;
	      OMP_CLAUSE_CHAIN (c) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
	      cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = c;
	    }
	  if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE)) != 0)
	    {
	      if (code == OMP_SIMD)
		{
		  c = build_omp_clause (OMP_CLAUSE_LOCATION (clauses),
					OMP_CLAUSE_REDUCTION);
		  OMP_CLAUSE_DECL (c) = OMP_CLAUSE_DECL (clauses);
		  OMP_CLAUSE_REDUCTION_CODE (c)
		    = OMP_CLAUSE_REDUCTION_CODE (clauses);
		  OMP_CLAUSE_REDUCTION_PLACEHOLDER (c)
		    = OMP_CLAUSE_REDUCTION_PLACEHOLDER (clauses);
		  OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c)
		    = OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clauses);
		  OMP_CLAUSE_REDUCTION_INSCAN (c)
		    = OMP_CLAUSE_REDUCTION_INSCAN (clauses);
		  OMP_CLAUSE_CHAIN (c) = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
		  cclauses[C_OMP_CLAUSE_SPLIT_SIMD] = c;
		}
	      if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS))
		  != 0)
		{
		  c = build_omp_clause (OMP_CLAUSE_LOCATION (clauses),
					OMP_CLAUSE_REDUCTION);
		  OMP_CLAUSE_DECL (c) = OMP_CLAUSE_DECL (clauses);
		  OMP_CLAUSE_REDUCTION_CODE (c)
		    = OMP_CLAUSE_REDUCTION_CODE (clauses);
		  OMP_CLAUSE_REDUCTION_PLACEHOLDER (c)
		    = OMP_CLAUSE_REDUCTION_PLACEHOLDER (clauses);
		  OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c)
		    = OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clauses);
		  OMP_CLAUSE_REDUCTION_INSCAN (c)
		    = OMP_CLAUSE_REDUCTION_INSCAN (clauses);
		  OMP_CLAUSE_CHAIN (c) = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
		  cclauses[C_OMP_CLAUSE_SPLIT_TEAMS] = c;
		  s = C_OMP_CLAUSE_SPLIT_PARALLEL;
		}
	      else if ((mask & (OMP_CLAUSE_MASK_1
				<< PRAGMA_OMP_CLAUSE_NUM_THREADS)) != 0
		       && !OMP_CLAUSE_REDUCTION_INSCAN (clauses))
		s = C_OMP_CLAUSE_SPLIT_PARALLEL;
	      else
		s = C_OMP_CLAUSE_SPLIT_FOR;
	    }
	  else if (code == OMP_SECTIONS
		   || code == OMP_PARALLEL
		   || code == OMP_MASTER
		   || code == OMP_MASKED)
	    s = C_OMP_CLAUSE_SPLIT_PARALLEL;
	  else if (code == OMP_TASKLOOP)
	    s = C_OMP_CLAUSE_SPLIT_TASKLOOP;
	  else if (code == OMP_LOOP)
	    s = C_OMP_CLAUSE_SPLIT_LOOP;
	  else if (code == OMP_SIMD)
	    {
	      if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP))
		  != 0)
		{
		  c = build_omp_clause (OMP_CLAUSE_LOCATION (clauses),
					OMP_CLAUSE_REDUCTION);
		  OMP_CLAUSE_DECL (c) = OMP_CLAUSE_DECL (clauses);
		  OMP_CLAUSE_REDUCTION_CODE (c)
		    = OMP_CLAUSE_REDUCTION_CODE (clauses);
		  OMP_CLAUSE_REDUCTION_PLACEHOLDER (c)
		    = OMP_CLAUSE_REDUCTION_PLACEHOLDER (clauses);
		  OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c)
		    = OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clauses);
		  OMP_CLAUSE_REDUCTION_INSCAN (c)
		    = OMP_CLAUSE_REDUCTION_INSCAN (clauses);
		  OMP_CLAUSE_CHAIN (c) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
		  cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP] = c;
		}
	      else if ((mask & (OMP_CLAUSE_MASK_1
				<< PRAGMA_OMP_CLAUSE_NUM_TEAMS)) != 0)
		{
		  c = build_omp_clause (OMP_CLAUSE_LOCATION (clauses),
					OMP_CLAUSE_REDUCTION);
		  OMP_CLAUSE_DECL (c) = OMP_CLAUSE_DECL (clauses);
		  OMP_CLAUSE_REDUCTION_CODE (c)
		    = OMP_CLAUSE_REDUCTION_CODE (clauses);
		  OMP_CLAUSE_REDUCTION_PLACEHOLDER (c)
		    = OMP_CLAUSE_REDUCTION_PLACEHOLDER (clauses);
		  OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c)
		    = OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clauses);
		  OMP_CLAUSE_REDUCTION_INSCAN (c)
		    = OMP_CLAUSE_REDUCTION_INSCAN (clauses);
		  OMP_CLAUSE_CHAIN (c) = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
		  cclauses[C_OMP_CLAUSE_SPLIT_TEAMS] = c;
		}
	      s = C_OMP_CLAUSE_SPLIT_SIMD;
	    }
	  else
	    s = C_OMP_CLAUSE_SPLIT_TEAMS;
	  break;
	case OMP_CLAUSE_IN_REDUCTION:
	  if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0)
	    {
	      /* When on target, map(always, tofrom: item) is added as
		 well.  For non-combined target it is added in the FEs.  */
	      c = build_omp_clause (OMP_CLAUSE_LOCATION (clauses),
				    OMP_CLAUSE_MAP);
	      OMP_CLAUSE_DECL (c) = OMP_CLAUSE_DECL (clauses);
	      OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_ALWAYS_TOFROM);
	      OMP_CLAUSE_CHAIN (c) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
	      cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = c;
	      s = C_OMP_CLAUSE_SPLIT_TARGET;
	      break;
	    }
	  /* in_reduction on taskloop simd becomes reduction on the simd
	     and keeps being in_reduction on taskloop.  */
	  if (code == OMP_SIMD)
	    {
	      c = build_omp_clause (OMP_CLAUSE_LOCATION (clauses),
				    OMP_CLAUSE_REDUCTION);
	      OMP_CLAUSE_DECL (c) = OMP_CLAUSE_DECL (clauses);
	      OMP_CLAUSE_REDUCTION_CODE (c)
		= OMP_CLAUSE_REDUCTION_CODE (clauses);
	      OMP_CLAUSE_REDUCTION_PLACEHOLDER (c)
		= OMP_CLAUSE_REDUCTION_PLACEHOLDER (clauses);
	      OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c)
		= OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (clauses);
	      OMP_CLAUSE_CHAIN (c) = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
	      cclauses[C_OMP_CLAUSE_SPLIT_SIMD] = c;
	    }
	  s = C_OMP_CLAUSE_SPLIT_TASKLOOP;
	  break;
	case OMP_CLAUSE_IF:
	  if (OMP_CLAUSE_IF_MODIFIER (clauses) != ERROR_MARK)
	    {
	      s = C_OMP_CLAUSE_SPLIT_COUNT;
	      switch (OMP_CLAUSE_IF_MODIFIER (clauses))
		{
		case OMP_PARALLEL:
		  if ((mask & (OMP_CLAUSE_MASK_1
			       << PRAGMA_OMP_CLAUSE_NUM_THREADS)) != 0)
		    s = C_OMP_CLAUSE_SPLIT_PARALLEL;
		  break;
		case OMP_SIMD:
		  if (code == OMP_SIMD)
		    s = C_OMP_CLAUSE_SPLIT_SIMD;
		  break;
		case OMP_TASKLOOP:
		  if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP))
		      != 0)
		    s = C_OMP_CLAUSE_SPLIT_TASKLOOP;
		  break;
		case OMP_TARGET:
		  if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP))
		      != 0)
		    s = C_OMP_CLAUSE_SPLIT_TARGET;
		  break;
		default:
		  break;
		}
	      if (s != C_OMP_CLAUSE_SPLIT_COUNT)
		break;
	      /* Error-recovery here, invalid if-modifier specified, add the
		 clause to just one construct.  */
	      if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0)
		s = C_OMP_CLAUSE_SPLIT_TARGET;
	      else if ((mask & (OMP_CLAUSE_MASK_1
				<< PRAGMA_OMP_CLAUSE_NUM_THREADS)) != 0)
		s = C_OMP_CLAUSE_SPLIT_PARALLEL;
	      else if ((mask & (OMP_CLAUSE_MASK_1
				<< PRAGMA_OMP_CLAUSE_NOGROUP)) != 0)
		s = C_OMP_CLAUSE_SPLIT_TASKLOOP;
	      else if (code == OMP_SIMD)
		s = C_OMP_CLAUSE_SPLIT_SIMD;
	      else
		gcc_unreachable ();
	      break;
	    }
	  /* Otherwise, duplicate if clause to all constructs.  */
	  if (code == OMP_SIMD)
	    {
	      if ((mask & ((OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)
			   | (OMP_CLAUSE_MASK_1
			      << PRAGMA_OMP_CLAUSE_NUM_THREADS)
			   | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP)))
		  != 0)
		{
		  c = build_omp_clause (OMP_CLAUSE_LOCATION (clauses),
					OMP_CLAUSE_IF);
		  OMP_CLAUSE_IF_MODIFIER (c)
		    = OMP_CLAUSE_IF_MODIFIER (clauses);
		  OMP_CLAUSE_IF_EXPR (c) = OMP_CLAUSE_IF_EXPR (clauses);
		  OMP_CLAUSE_CHAIN (c) = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
		  cclauses[C_OMP_CLAUSE_SPLIT_SIMD] = c;
		}
	      else
		{
		  s = C_OMP_CLAUSE_SPLIT_SIMD;
		  break;
		}
	    }
	  if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP))
	      != 0)
	    {
	      if ((mask & (OMP_CLAUSE_MASK_1
			   << PRAGMA_OMP_CLAUSE_NUM_THREADS)) != 0)
		{
		  c = build_omp_clause (OMP_CLAUSE_LOCATION (clauses),
					OMP_CLAUSE_IF);
		  OMP_CLAUSE_IF_MODIFIER (c)
		    = OMP_CLAUSE_IF_MODIFIER (clauses);
		  OMP_CLAUSE_IF_EXPR (c) = OMP_CLAUSE_IF_EXPR (clauses);
		  OMP_CLAUSE_CHAIN (c) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
		  cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP] = c;
		  s = C_OMP_CLAUSE_SPLIT_PARALLEL;
		}
	      else
		s = C_OMP_CLAUSE_SPLIT_TASKLOOP;
	    }
	  else if ((mask & (OMP_CLAUSE_MASK_1
			    << PRAGMA_OMP_CLAUSE_NUM_THREADS)) != 0)
	    {
	      if ((mask & (OMP_CLAUSE_MASK_1
			   << PRAGMA_OMP_CLAUSE_MAP)) != 0)
		{
		  c = build_omp_clause (OMP_CLAUSE_LOCATION (clauses),
					OMP_CLAUSE_IF);
		  OMP_CLAUSE_IF_MODIFIER (c)
		    = OMP_CLAUSE_IF_MODIFIER (clauses);
		  OMP_CLAUSE_IF_EXPR (c) = OMP_CLAUSE_IF_EXPR (clauses);
		  OMP_CLAUSE_CHAIN (c) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
		  cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = c;
		  s = C_OMP_CLAUSE_SPLIT_PARALLEL;
		}
	      else
		s = C_OMP_CLAUSE_SPLIT_PARALLEL;
	    }
	  else
	    s = C_OMP_CLAUSE_SPLIT_TARGET;
	  break;
	case OMP_CLAUSE_LINEAR:
	  /* Linear clause is allowed on simd and for.  Put it on the
	     innermost construct.  */
	  if (code == OMP_SIMD)
	    s = C_OMP_CLAUSE_SPLIT_SIMD;
	  else
	    s = C_OMP_CLAUSE_SPLIT_FOR;
	  break;
	case OMP_CLAUSE_NOWAIT:
	  /* Nowait clause is allowed on target, for and sections, but
	     is not allowed on parallel for or parallel sections.  Therefore,
	     put it on target construct if present, because that can only
	     be combined with parallel for{, simd} and not with for{, simd},
	     otherwise to the worksharing construct.  */
	  if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP))
	      != 0)
	    s = C_OMP_CLAUSE_SPLIT_TARGET;
	  else
	    s = C_OMP_CLAUSE_SPLIT_FOR;
	  break;
	  /* thread_limit is allowed on target and teams.  Distribute it
	     to all.  */
	case OMP_CLAUSE_THREAD_LIMIT:
	  if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP))
	      != 0)
	    {
	      if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS))
		  != 0)
		{
		  c = build_omp_clause (OMP_CLAUSE_LOCATION (clauses),
					OMP_CLAUSE_THREAD_LIMIT);
		  OMP_CLAUSE_THREAD_LIMIT_EXPR (c)
		    = OMP_CLAUSE_THREAD_LIMIT_EXPR (clauses);
		  OMP_CLAUSE_CHAIN (c) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
		  cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = c;
		}
	      else
		{
		  s = C_OMP_CLAUSE_SPLIT_TARGET;
		  break;
		}
	    }
	  s = C_OMP_CLAUSE_SPLIT_TEAMS;
	  break;
	/* Allocate clause is allowed on target, teams, distribute, parallel,
	   for, sections and taskloop.  Distribute it to all.  */
	case OMP_CLAUSE_ALLOCATE:
	  s = C_OMP_CLAUSE_SPLIT_COUNT;
	  for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
	    {
	      switch (i)
		{
		case C_OMP_CLAUSE_SPLIT_TARGET:
		  if ((mask & (OMP_CLAUSE_MASK_1
			       << PRAGMA_OMP_CLAUSE_MAP)) == 0)
		    continue;
		  break;
		case C_OMP_CLAUSE_SPLIT_TEAMS:
		  if ((mask & (OMP_CLAUSE_MASK_1
			       << PRAGMA_OMP_CLAUSE_NUM_TEAMS)) == 0)
		    continue;
		  break;
		case C_OMP_CLAUSE_SPLIT_DISTRIBUTE:
		  if ((mask & (OMP_CLAUSE_MASK_1
			       << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
		    continue;
		  break;
		case C_OMP_CLAUSE_SPLIT_PARALLEL:
		  if ((mask & (OMP_CLAUSE_MASK_1
			       << PRAGMA_OMP_CLAUSE_NUM_THREADS)) == 0)
		    continue;
		  break;
		case C_OMP_CLAUSE_SPLIT_FOR:
		  STATIC_ASSERT (C_OMP_CLAUSE_SPLIT_SECTIONS
				 == C_OMP_CLAUSE_SPLIT_FOR
				 && (C_OMP_CLAUSE_SPLIT_TASKLOOP
				     == C_OMP_CLAUSE_SPLIT_FOR)
				 && (C_OMP_CLAUSE_SPLIT_LOOP
				     == C_OMP_CLAUSE_SPLIT_FOR));
		  if (code == OMP_SECTIONS)
		    break;
		  if ((mask & (OMP_CLAUSE_MASK_1
			       << PRAGMA_OMP_CLAUSE_SCHEDULE)) != 0)
		    break;
		  if ((mask & (OMP_CLAUSE_MASK_1
			       << PRAGMA_OMP_CLAUSE_NOGROUP)) != 0)
		    break;
		  continue;
		case C_OMP_CLAUSE_SPLIT_SIMD:
		  continue;
		default:
		  gcc_unreachable ();
		}
	      if (s != C_OMP_CLAUSE_SPLIT_COUNT)
		{
		  c = build_omp_clause (OMP_CLAUSE_LOCATION (clauses),
					OMP_CLAUSE_ALLOCATE);
		  OMP_CLAUSE_DECL (c)
		    = OMP_CLAUSE_DECL (clauses);
		  OMP_CLAUSE_ALLOCATE_ALLOCATOR (c)
		    = OMP_CLAUSE_ALLOCATE_ALLOCATOR (clauses);
		  OMP_CLAUSE_ALLOCATE_ALIGN (c)
		    = OMP_CLAUSE_ALLOCATE_ALIGN (clauses);
		  OMP_CLAUSE_CHAIN (c) = cclauses[s];
		  cclauses[s] = c;
		  has_dup_allocate = true;
		}
	      s = (enum c_omp_clause_split) i;
	    }
	  gcc_assert (s != C_OMP_CLAUSE_SPLIT_COUNT);
	  break;
	default:
	  gcc_unreachable ();
	}
      OMP_CLAUSE_CHAIN (clauses) = cclauses[s];
      cclauses[s] = clauses;
    }

  if (has_dup_allocate)
    {
      bool need_prune = false;
      bitmap_obstack_initialize (NULL);
      for (i = 0; i < C_OMP_CLAUSE_SPLIT_SIMD - (code == OMP_LOOP); i++)
	if (cclauses[i])
	  {
	    bitmap_head allocate_head;
	    bitmap_initialize (&allocate_head, &bitmap_default_obstack);
	    for (c = cclauses[i]; c; c = OMP_CLAUSE_CHAIN (c))
	      if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ALLOCATE
		  && DECL_P (OMP_CLAUSE_DECL (c)))
		bitmap_set_bit (&allocate_head,
				DECL_UID (OMP_CLAUSE_DECL (c)));
	    for (c = cclauses[i]; c; c = OMP_CLAUSE_CHAIN (c))
	      switch (OMP_CLAUSE_CODE (c))
		{
		case OMP_CLAUSE_REDUCTION:
		case OMP_CLAUSE_IN_REDUCTION:
		case OMP_CLAUSE_TASK_REDUCTION:
		  if (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF)
		    {
		      tree t = TREE_OPERAND (OMP_CLAUSE_DECL (c), 0);
		      if (TREE_CODE (t) == POINTER_PLUS_EXPR)
			t = TREE_OPERAND (t, 0);
		      if (TREE_CODE (t) == ADDR_EXPR
			  || TREE_CODE (t) == INDIRECT_REF)
			t = TREE_OPERAND (t, 0);
		      if (DECL_P (t))
			bitmap_clear_bit (&allocate_head, DECL_UID (t));
		      break;
		    }
		  else if (TREE_CODE (OMP_CLAUSE_DECL (c)) == TREE_LIST)
		    {
		      tree t;
		      for (t = OMP_CLAUSE_DECL (c);
			   TREE_CODE (t) == TREE_LIST; t = TREE_CHAIN (t))
			;
		      if (DECL_P (t))
			bitmap_clear_bit (&allocate_head, DECL_UID (t));
		      break;
		    }
		  /* FALLTHRU */
		case OMP_CLAUSE_PRIVATE:
		case OMP_CLAUSE_FIRSTPRIVATE:
		case OMP_CLAUSE_LASTPRIVATE:
		case OMP_CLAUSE_LINEAR:
		  if (DECL_P (OMP_CLAUSE_DECL (c)))
		    bitmap_clear_bit (&allocate_head,
				      DECL_UID (OMP_CLAUSE_DECL (c)));
		  break;
		default:
		  break;
		}
	    for (c = cclauses[i]; c; c = OMP_CLAUSE_CHAIN (c))
	      if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ALLOCATE
		  && DECL_P (OMP_CLAUSE_DECL (c))
		  && bitmap_bit_p (&allocate_head,
				   DECL_UID (OMP_CLAUSE_DECL (c))))
		{
		  /* Mark allocate clauses which don't have corresponding
		     explicit data sharing clause.  */
		  OMP_CLAUSE_ALLOCATE_COMBINED (c) = 1;
		  need_prune = true;
		}
	  }
      bitmap_obstack_release (NULL);
      if (need_prune)
	{
	  /* At least one allocate clause has been marked.  Walk all the
	     duplicated allocate clauses in sync.  If it is marked in all
	     constituent constructs, diagnose it as invalid and remove
	     them.  Otherwise, remove all marked inner clauses inside
	     a construct that doesn't have them marked.  Keep the outer
	     marked ones, because some clause duplication is done only
	     during gimplification.  */
	  tree *p[C_OMP_CLAUSE_SPLIT_COUNT];
	  for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
	    if (cclauses[i] == NULL_TREE
		|| i == C_OMP_CLAUSE_SPLIT_SIMD
		|| (i == C_OMP_CLAUSE_SPLIT_LOOP && code == OMP_LOOP))
	      p[i] = NULL;
	    else
	      p[i] = &cclauses[i];
	  do
	    {
	      int j = -1;
	      tree seen = NULL_TREE;
	      for (i = C_OMP_CLAUSE_SPLIT_COUNT - 1; i >= 0; i--)
		if (p[i])
		  {
		    while (*p[i]
			   && OMP_CLAUSE_CODE (*p[i]) != OMP_CLAUSE_ALLOCATE)
		      p[i] = &OMP_CLAUSE_CHAIN (*p[i]);
		    if (*p[i] == NULL_TREE)
		      {
			i = C_OMP_CLAUSE_SPLIT_COUNT;
			break;
		      }
		    if (!OMP_CLAUSE_ALLOCATE_COMBINED (*p[i]) && j == -1)
		      j = i;
		    seen = *p[i];
		  }
	      if (i == C_OMP_CLAUSE_SPLIT_COUNT)
		break;
	      if (j == -1)
		error_at (OMP_CLAUSE_LOCATION (seen),
			  "%qD specified in %<allocate%> clause but not in "
			  "an explicit privatization clause",
			  OMP_CLAUSE_DECL (seen));
	      for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
		if (p[i])
		  {
		    if (i > j)
		      /* Remove.  */
		      *p[i] = OMP_CLAUSE_CHAIN (*p[i]);
		    else
		      /* Keep.  */
		      p[i] = &OMP_CLAUSE_CHAIN (*p[i]);
		  }
	    }
	  while (1);
	}
    }

  if (!flag_checking)
    return;

  if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
    gcc_assert (cclauses[C_OMP_CLAUSE_SPLIT_TARGET] == NULL_TREE);
  if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS)) == 0)
    gcc_assert (cclauses[C_OMP_CLAUSE_SPLIT_TEAMS] == NULL_TREE);
  if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0
      && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FILTER)) == 0)
    gcc_assert (cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE] == NULL_TREE);
  if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS)) == 0)
    gcc_assert (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] == NULL_TREE);
  if ((mask & ((OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE)
	       | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP))) == 0
      && code != OMP_SECTIONS
      && code != OMP_LOOP)
    gcc_assert (cclauses[C_OMP_CLAUSE_SPLIT_FOR] == NULL_TREE);
  if (code != OMP_SIMD)
    gcc_assert (cclauses[C_OMP_CLAUSE_SPLIT_SIMD] == NULL_TREE);
}


/* qsort callback to compare #pragma omp declare simd clauses.  */

static int
c_omp_declare_simd_clause_cmp (const void *p, const void *q)
{
  tree a = *(const tree *) p;
  tree b = *(const tree *) q;
  if (OMP_CLAUSE_CODE (a) != OMP_CLAUSE_CODE (b))
    {
      if (OMP_CLAUSE_CODE (a) > OMP_CLAUSE_CODE (b))
	return -1;
      return 1;
    }
  if (OMP_CLAUSE_CODE (a) != OMP_CLAUSE_SIMDLEN
      && OMP_CLAUSE_CODE (a) != OMP_CLAUSE_INBRANCH
      && OMP_CLAUSE_CODE (a) != OMP_CLAUSE_NOTINBRANCH)
    {
      int c = tree_to_shwi (OMP_CLAUSE_DECL (a));
      int d = tree_to_shwi (OMP_CLAUSE_DECL (b));
      if (c < d)
	return 1;
      if (c > d)
	return -1;
    }
  return 0;
}

/* Change PARM_DECLs in OMP_CLAUSE_DECL of #pragma omp declare simd
   CLAUSES on FNDECL into argument indexes and sort them.  */

tree
c_omp_declare_simd_clauses_to_numbers (tree parms, tree clauses)
{
  tree c;
  vec<tree> clvec = vNULL;

  for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
    {
      if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SIMDLEN
	  && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_INBRANCH
	  && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_NOTINBRANCH)
	{
	  tree decl = OMP_CLAUSE_DECL (c);
	  tree arg;
	  int idx;
	  for (arg = parms, idx = 0; arg;
	       arg = TREE_CHAIN (arg), idx++)
	    if (arg == decl)
	      break;
	  if (arg == NULL_TREE)
	    {
	      error_at (OMP_CLAUSE_LOCATION (c),
			"%qD is not a function argument", decl);
	      continue;
	    }
	  OMP_CLAUSE_DECL (c) = build_int_cst (integer_type_node, idx);
	  if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
	      && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c))
	    {
	      decl = OMP_CLAUSE_LINEAR_STEP (c);
	      for (arg = parms, idx = 0; arg;
		   arg = TREE_CHAIN (arg), idx++)
		if (arg == decl)
		  break;
	      if (arg == NULL_TREE)
		{
		  error_at (OMP_CLAUSE_LOCATION (c),
			    "%qD is not a function argument", decl);
		  continue;
		}
	      OMP_CLAUSE_LINEAR_STEP (c)
		= build_int_cst (integer_type_node, idx);
	    }
	}
      clvec.safe_push (c);
    }
  if (!clvec.is_empty ())
    {
      unsigned int len = clvec.length (), i;
      clvec.qsort (c_omp_declare_simd_clause_cmp);
      clauses = clvec[0];
      for (i = 0; i < len; i++)
	OMP_CLAUSE_CHAIN (clvec[i]) = (i < len - 1) ? clvec[i + 1] : NULL_TREE;
    }
  else
    clauses = NULL_TREE;
  clvec.release ();
  return clauses;
}

/* Change argument indexes in CLAUSES of FNDECL back to PARM_DECLs.  */

void
c_omp_declare_simd_clauses_to_decls (tree fndecl, tree clauses)
{
  tree c;

  for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
    if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SIMDLEN
	&& OMP_CLAUSE_CODE (c) != OMP_CLAUSE_INBRANCH
	&& OMP_CLAUSE_CODE (c) != OMP_CLAUSE_NOTINBRANCH)
      {
	int idx = tree_to_shwi (OMP_CLAUSE_DECL (c)), i;
	tree arg;
	for (arg = DECL_ARGUMENTS (fndecl), i = 0; arg;
	     arg = TREE_CHAIN (arg), i++)
	  if (i == idx)
	    break;
	gcc_assert (arg);
	OMP_CLAUSE_DECL (c) = arg;
	if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
	    && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c))
	  {
	    idx = tree_to_shwi (OMP_CLAUSE_LINEAR_STEP (c));
	    for (arg = DECL_ARGUMENTS (fndecl), i = 0; arg;
		 arg = TREE_CHAIN (arg), i++)
	      if (i == idx)
		break;
	    gcc_assert (arg);
	    OMP_CLAUSE_LINEAR_STEP (c) = arg;
	  }
      }
}

/* Return true for __func__ and similar function-local predefined
   variables (which are in OpenMP predetermined shared, allowed in
   shared/firstprivate clauses).  */

bool
c_omp_predefined_variable (tree decl)
{
  if (VAR_P (decl)
      && DECL_ARTIFICIAL (decl)
      && TREE_STATIC (decl)
      && DECL_NAME (decl))
    {
      if (TREE_READONLY (decl)
	  && (DECL_NAME (decl) == ridpointers[RID_C99_FUNCTION_NAME]
	      || DECL_NAME (decl) == ridpointers[RID_FUNCTION_NAME]
	      || DECL_NAME (decl) == ridpointers[RID_PRETTY_FUNCTION_NAME]))
	return true;
      /* For UBSan handle the same also ubsan_create_data created
	 variables.  There is no magic flag for those, but user variables
	 shouldn't be DECL_ARTIFICIAL or have TYPE_ARTIFICIAL type with
	 such names.  */
      if ((flag_sanitize & (SANITIZE_UNDEFINED
			    | SANITIZE_UNDEFINED_NONDEFAULT)) != 0
	  && DECL_IGNORED_P (decl)
	  && !TREE_READONLY (decl)
	  && TREE_CODE (DECL_NAME (decl)) == IDENTIFIER_NODE
	  && TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
	  && TYPE_ARTIFICIAL (TREE_TYPE (decl))
	  && TYPE_NAME (TREE_TYPE (decl))
	  && TREE_CODE (TYPE_NAME (TREE_TYPE (decl))) == TYPE_DECL
	  && DECL_NAME (TYPE_NAME (TREE_TYPE (decl)))
	  && (TREE_CODE (DECL_NAME (TYPE_NAME (TREE_TYPE (decl))))
	      == IDENTIFIER_NODE))
	{
	  tree id1 = DECL_NAME (decl);
	  tree id2 = DECL_NAME (TYPE_NAME (TREE_TYPE (decl)));
	  if (IDENTIFIER_LENGTH (id1) >= sizeof ("ubsan_data") - 1
	      && IDENTIFIER_LENGTH (id2) >= sizeof ("__ubsan__data")
	      && !memcmp (IDENTIFIER_POINTER (id2), "__ubsan_",
			  sizeof ("__ubsan_") - 1)
	      && !memcmp (IDENTIFIER_POINTER (id2) + IDENTIFIER_LENGTH (id2)
			  - sizeof ("_data") + 1, "_data",
			  sizeof ("_data") - 1)
	      && strstr (IDENTIFIER_POINTER (id1), "ubsan_data"))
	    return true;
	}
    }
  return false;
}

/* OMP_CLAUSE_DEFAULT_UNSPECIFIED unless OpenMP sharing attribute of DECL
   is predetermined.  */

enum omp_clause_default_kind
c_omp_predetermined_sharing (tree decl)
{
  /* Predetermine artificial variables holding integral values, those
     are usually result of gimplify_one_sizepos or SAVE_EXPR
     gimplification.  */
  if (VAR_P (decl)
      && DECL_ARTIFICIAL (decl)
      && INTEGRAL_TYPE_P (TREE_TYPE (decl)))
    return OMP_CLAUSE_DEFAULT_SHARED;

  if (c_omp_predefined_variable (decl))
    return OMP_CLAUSE_DEFAULT_SHARED;

  return OMP_CLAUSE_DEFAULT_UNSPECIFIED;
}

/* OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED unless OpenMP mapping attribute
   of DECL is predetermined.  */

enum omp_clause_defaultmap_kind
c_omp_predetermined_mapping (tree decl)
{
  /* Predetermine artificial variables holding integral values, those
     are usually result of gimplify_one_sizepos or SAVE_EXPR
     gimplification.  */
  if (VAR_P (decl)
      && DECL_ARTIFICIAL (decl)
      && INTEGRAL_TYPE_P (TREE_TYPE (decl)))
    return OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE;

  if (c_omp_predefined_variable (decl))
    return OMP_CLAUSE_DEFAULTMAP_TO;

  return OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED;
}

/* The following functions implement automatic recognition and annotation of
   for loops in OpenACC kernels regions.  Inside a kernels region, a nest of
   for loops that does not contain any annotated OpenACC loops, nor break
   or goto statements or assignments to the variables controlling loop
   termination, is converted to an OMP_FOR node with an "acc loop auto"
   annotation on each loop.  This feature is controlled by
   flag_openacc_kernels_annotate_loops.  */

/* Check whether DECL is the declaration of a local variable (or function
   parameter) of integral type that does not have its address taken.  */

static bool
is_local_var (tree decl)
{
  return ((TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL)
	  && DECL_CONTEXT (decl) != NULL
	  && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
	  && INTEGRAL_TYPE_P (TREE_TYPE (decl))
	  && !TREE_ADDRESSABLE (decl));
}

/* EXP is a loop bound expression for a comparison against local
   variable DECL.  Check whether this is potentially valid in an OpenACC loop
   context, namely that it can be precomputed when entering the loop
   construct per the OpenACC specification.  Local variables referenced
   in both DECL and EXP that may not be modified in the body of the loop
   are added to the list in INFO to be checked later.

   FIXME: Ideally we would like to make this test permissive rather than
   restrictive, and allow the later conversion of the "auto" attribute to
   either "seq" or "independent" to make the determination using dataflow,
   alias analysis, etc rather than a tree traversal.  But presently it does
   not do that and always just hoists the loop bound expression.  So the
   current implementation only considers expressions involving unmodified
   local variables and constants, using a tree walk.  */

static tree
end_test_ok_for_annotation_r (tree *tp, int *walk_subtrees,
			      void *data)
{
  tree exp = *tp;
  struct annotation_info *info = (struct annotation_info *) data;

  switch (TREE_CODE_CLASS (TREE_CODE (exp)))
    {
    case tcc_constant:
      /* Constants are trivially known to be invariant.  */
      return NULL_TREE;

    case tcc_declaration:
      if (is_local_var (exp))
	{
	  tree t;
	  /* Add it to the list of variables that can't be modified in the
	     loop, only if not already present.  */
	  for (t = info->vars; t && TREE_VALUE (t) != exp;
	       t = TREE_CHAIN (t))
	    ;
	  if (!t)
	    info->vars = tree_cons (NULL_TREE, exp, info->vars);
	  return NULL_TREE;
	}
      else if (TREE_CODE (exp) == VAR_DECL && TREE_READONLY (exp))
	return NULL_TREE;
      else if (TREE_CODE (exp) == FUNCTION_DECL)
	return NULL_TREE;
      break;

    case tcc_unary:
    case tcc_binary:
    case tcc_comparison:
      /* Allow arithmetic expressions and comparisons provided
	 that the operands are good.  */
      return NULL_TREE;

    default:
      /* Handle some special cases.  */
      switch (TREE_CODE (exp))
	{
	case COND_EXPR:
	case TRUTH_ANDIF_EXPR:
	case TRUTH_ORIF_EXPR:
	case TRUTH_AND_EXPR:
	case TRUTH_OR_EXPR:
	case TRUTH_XOR_EXPR:
	case TRUTH_NOT_EXPR:
	  /* ?: and boolean operators are OK.  */
	  return NULL_TREE;

	case CALL_EXPR:
	  /* Allow calls to constant functions with invariant operands.  */
	  {
	    tree fndecl = get_callee_fndecl (exp);
	    if (fndecl && TREE_READONLY (fndecl))
	      return NULL_TREE;
	  }
	  break;

	case ADDR_EXPR:
	  /* We can expect addresses of things to be invariant.  */
	  return NULL_TREE;

	default:
	  break;
	}
    }

  /* Reject anything else.  */
  *walk_subtrees = 0;
  return exp;
}

static bool
end_test_ok_for_annotation (tree decl, tree exp,
			    struct annotation_info *info)
{
  /* Traversal returns NULL_TREE if all is well.  */
  if (!walk_tree (&exp, end_test_ok_for_annotation_r, info, NULL))
    {
      /* So far, so good.  Check the decl against any variables collected
	 in the exp.  */
      tree t;
      for (t = info->vars; t; t = TREE_CHAIN (t))
	if (TREE_VALUE (t) == decl)
	  return false;
      info->vars = tree_cons (NULL_TREE, decl, info->vars);
      return true;
    }
  return false;
}

/* The initializer for a FOR_STMT is sometimes wrapped in various other
   language-specific tree structures.  We need a hook to unwrap them.
   This function takes a tree argument and should return either a
   MODIFY_EXPR, VAR_DECL, or NULL_TREE.  */

static tree (*lang_specific_unwrap_initializer) (tree);

/* Try to annotate the given NODE, which must be a FOR_STMT, with a
   "#pragma acc loop auto" annotation.  In practice, this means
   building an OMP_FOR node for it.  DECL and INIT are the
   previously-verified iteration variable and initializer.  Annotating
   the loop may fail, in which case INFO is used to record the cause
   of the failure and the original loop remains unchanged.  This
   function returns the transformed loop if the transformation
   succeeded, the original node otherwise.  */

static tree
annotate_for_loop (tree node, tree decl, tree init,
		   struct annotation_info *info)
{
  gcc_checking_assert (TREE_CODE (node) == FOR_STMT);

  location_t loc = EXPR_LOCATION (node);
  tree cond = FOR_COND (node);
  tree incr = FOR_EXPR (node);

  gcc_assert (decl);
  gcc_assert (cond);
  gcc_assert (decl && TREE_CODE (decl) == VAR_DECL);

  /* The C++ frontend can wrap the increment two levels deep inside a
     cleanup expression, but c_finish_omp_for does not care about that.  */
  if (incr != NULL_TREE && TREE_CODE (incr) == CLEANUP_POINT_EXPR)
    incr = TREE_OPERAND (TREE_OPERAND (incr, 0), 0);
  tree body = FOR_BODY (node);

  tree declv = make_tree_vec (1);
  tree initv = make_tree_vec (1);
  tree condv = make_tree_vec (1);
  tree incrv = make_tree_vec (1);
  TREE_VEC_ELT (declv, 0) = decl;
  TREE_VEC_ELT (initv, 0) = init;
  TREE_VEC_ELT (condv, 0) = cond;
  TREE_VEC_ELT (incrv, 0) = incr;

  /* Do the actual transformation.  This can still fail because
     c_finish_omp_for has some stricter checks than we have performed up to
     this point.  */
  tree omp_for = c_finish_omp_for_internal (loc, OACC_LOOP, declv, NULL_TREE,
					    initv, condv, incrv, body,
					    NULL_TREE, false, info);
  if (omp_for != NULL_TREE)
    {
      /* Add an auto clause, then return the new loop.  */
      tree auto_clause = build_omp_clause (loc, OMP_CLAUSE_AUTO);
      OMP_CLAUSE_CHAIN (auto_clause) = OMP_FOR_CLAUSES (omp_for);
      OMP_FOR_CLAUSES (omp_for) = auto_clause;
      return omp_for;
    }

  return node;
}

/* Forward declaration.  */
static tree annotate_loops_in_kernels_regions (tree *, int *, void *);

/* Given a FOR_STMT NODE that is a candidate for parallelization, check its
   body for validity, then try to annotate it with
   "#pragma oacc loop auto", possibly modifying the current node in place.
   The INFO argument contains the traversal state at the point the loop
   appears.  */

static void
check_and_annotate_for_loop (tree *nodeptr, tree_stmt_iterator *prev_tsi,
			     struct annotation_info *info)
{
  tree node = *nodeptr;
  gcc_assert (TREE_CODE (node) == FOR_STMT);
  tree init = FOR_INIT_STMT (node);
  tree cond = FOR_COND (node);
  tree prev_stmt = NULL_TREE;
  tree decl = NULL_TREE;
  bool unlink_prev = false;
  bool fix_decl = false;

  /* This structure describes the current loop statement.  */
  struct annotation_info loop_info
    = { node, NULL_TREE, false, as_in_kernels_loop, NULL_TREE, info };

  /* If we are in the body of an explicitly-annotated loop, do not add
     annotations to this loop or any other nested loops.  */
  if (info->state == as_explicit_annotation)
    do_not_annotate_loop (&loop_info, as_explicit_annotation, info->reason);

  /* We need to find the controlling variable for the loop in order
     to detect whether it is modified in the body of the loop.
     That is why we are doing some checks on the loop condition
     that duplicate what c_finish_omp_for is doing.  */

  /* First we need to find the decl and initializer for the
     controlling variable.  Both the C and C++ front ends normally put
     the initializer in the statement list just before the FOR_STMT
     instead of in FOR_INIT_STMT.  If FOR_INIT_STMT happens to exist
     but isn't a MODIFY_EXPR, give up.
     handle it.  */

  else if (init != NULL_TREE && TREE_CODE (init) != MODIFY_EXPR)
    do_not_annotate_loop (&loop_info, as_invalid_initializer, NULL_TREE);

  /* Examine the statement before the loop to see if it is a
     valid initializer.  It must be either a MODIFY_EXPR or VAR_DECL,
     possibly wrapped in language-specific structure.  */
  else if (init == NULL_TREE && prev_tsi != NULL && tsi_stmt (*prev_tsi))
    {
      prev_stmt = tsi_stmt (*prev_tsi);

      /* Call the language-specific hook to unwrap prev_stmt.  */
      prev_stmt = (*lang_specific_unwrap_initializer) (prev_stmt);

      /* See if we have a valid MODIFY_EXPR.  */
      if (TREE_CODE (prev_stmt) == MODIFY_EXPR
	  && is_local_var (TREE_OPERAND (prev_stmt, 0))
	  && !TREE_SIDE_EFFECTS (TREE_OPERAND (prev_stmt, 1)))
	{
	  decl = TREE_OPERAND (prev_stmt, 0);
	  init = prev_stmt;
	  unlink_prev = true;
	}
      else if (is_local_var (prev_stmt)
	       && DECL_INITIAL (prev_stmt)
	       && !TREE_SIDE_EFFECTS (DECL_INITIAL (prev_stmt)))
	{
	  /* If the preceding statement is the declaration of the loop
	     variable with its initialization, build an assignment
	     expression for the loop's initializer.  */
	  decl = prev_stmt;
	  init = build2 (MODIFY_EXPR, TREE_TYPE (decl), decl,
			 DECL_INITIAL (decl));
	  /* We need to remove the initializer from the decl if we
	     end up using the init we just built instead.  */
	  fix_decl = true;
	}
    }

  if (init == NULL_TREE || decl == NULL_TREE)
    /* There is nothing we can do to find the correct init statement for
       this loop.  */
    do_not_annotate_loop (&loop_info, as_missing_initializer, NULL_TREE);

  /* The condition must be a comparison of the decl we found in
     the initializer against an expression that can be hoisted
     outside the loop.  */
  if (loop_info.state > as_in_kernels_loop)
    /* Skip validating condition if we've already got an error.  */
    ;
  else if (cond == NULL_TREE)
    do_not_annotate_loop (&loop_info, as_missing_predicate, NULL_TREE);
  else if (TREE_CODE_CLASS (TREE_CODE (cond)) != tcc_comparison)
    do_not_annotate_loop (&loop_info, as_invalid_predicate, cond);
  else
    {
      tree limit_exp = NULL_TREE;

      if (TREE_OPERAND (cond, 0) == decl)
	limit_exp = TREE_OPERAND (cond, 1);
      else if (TREE_OPERAND (cond, 1) == decl)
	limit_exp = TREE_OPERAND (cond, 0);

      if (!limit_exp
	  || !end_test_ok_for_annotation (decl, limit_exp, &loop_info))
	do_not_annotate_loop (&loop_info, as_invalid_predicate, cond);
    }

  /* Walk the body.  This will process any nested loops, so we have to do it
     even if we have already rejected this loop as a candidate for
     annotation.  */
  walk_tree (&FOR_BODY (node), annotate_loops_in_kernels_regions,
	     (void *) &loop_info, NULL);

  if (loop_info.state == as_in_kernels_loop)
    {
      /* If the traversal of the loop and all nested loops didn't hit
	 any problems, attempt the actual transformation.  If it
	 succeeds, replace this node with the annotated loop.  */
      tree result = annotate_for_loop (node, decl, init, &loop_info);
      if (result != node)
	{
	  /* Success!  */
	  *nodeptr = result;

	  if (unlink_prev)
	    /* We don't need the previous statement that we consumed
	       as an initializer in the new OMP_FOR any more.  */
	    tsi_delink (prev_tsi);

	  if (fix_decl)
	    /* We no longer need the initializer expression on the
	       decl of the loop variable and don't want to duplicate
	       it.  The kernels conversion pass would interpret it as
	       a stray assignment in a gang-single region.  */
	    DECL_INITIAL (decl) = NULL_TREE;

	  return;
	}
    }

  /* If we got here, we have a FOR_STMT we could not convert to an
     OMP loop.  */

  if (loop_info.state == as_invalid_return)
    /* This is diagnosed elsewhere as a hard error, so no warning is
       needed here.  */
    return;

  /* Issue warnings about other problems.  */
  auto_diagnostic_group d;
  if (warning_at (EXPR_LOCATION (node),
		  OPT_Wopenacc_kernels_annotate_loops,
		  "loop cannot be annotated for OpenACC parallelization"))
    {
      location_t locus;
      if (loop_info.reason && EXPR_HAS_LOCATION (loop_info.reason))
	locus = EXPR_LOCATION (loop_info.reason);
      else
	locus = EXPR_LOCATION (node);
      switch (loop_info.state)
	{
	case as_invalid_variable_type:
	  inform (locus, "invalid type for iteration variable %qE",
		  loop_info.reason);
	  break;
	case as_missing_initializer:
	  inform (locus, "missing iteration variable initializer");
	  break;
	case as_invalid_initializer:
	  inform (locus, "unrecognized initializer");
	  break;
	case as_missing_predicate:
	  inform (locus, "missing controlling predicate");
	  break;
	case as_invalid_predicate:
	  inform (locus, "invalid controlling predicate");
	  break;
	case as_missing_increment:
	  inform (locus, "missing increment expression");
	  break;
	case as_invalid_increment:
	  inform (locus, "invalid increment expression");
	  break;
	case as_explicit_annotation:
	  inform (locus, "explicit OpenACC annotation in loop nest");
	  break;
	case as_invalid_control_flow:
	  inform (locus, "loop contains unstructured control flow");
	  break;
	case as_invalid_break:
	  inform (locus, "loop contains %<break%> statement");
	  break;
	case as_invalid_call:
	  inform (locus, "loop contains call to non-oacc function");
	  break;
	case as_invalid_modification:
	  inform (locus, "invalid modification of controlling variable");
	  break;
	default:
	  gcc_unreachable ();
	}
    }
}

/* Traversal function for walk_tree.  Visit the tree, finding OpenACC
   kernels regions.  DATA is NULL if we are outside of a kernels region,
   otherwise it is a pointer to the enclosing kernels region's
   annotation_info struct.  If the traversal encounters a for loop inside a
   kernels region that is a candidate for parallelization, annotate it
   with OpenACC loop directives.  */

static tree
annotate_loops_in_kernels_regions (tree *nodeptr, int *walk_subtrees,
				   void *data)
{
  tree node = *nodeptr;
  struct annotation_info *info = (struct annotation_info *) data;
  gcc_assert (info);

  switch (TREE_CODE (node))
    {
    case OACC_KERNELS:
      /* Recursively process the body of the kernels region in a new info
	 scope.  */
      if (info->state == as_outer)
	{
	  struct annotation_info nested_info
	    = { NULL_TREE, NULL_TREE, true,
		as_in_kernels_region, NULL_TREE, info };
	  walk_tree (&OMP_BODY (node), annotate_loops_in_kernels_regions,
		     (void *) &nested_info, NULL);
	  *walk_subtrees = 0;
	}
      break;

    case OACC_LOOP:
      /* Do not try to add automatic OpenACC annotations inside manually
	 annotated loops.  Presumably, the user avoided doing it on
	 purpose; for example, all available levels of parallelism may
	 have been used up.  However, assume that the combined construct
	 "#pragma acc kernels loop" means to try to process the whole
	 loop nest.
	 Note that a single OACC_LOOP construct represents an entire set
	 of collapsed loops so we do not have to deal explicitly with the
	 collapse clause here, as the Fortran front end does.  */
      if (info->state == as_in_kernels_region && OACC_LOOP_COMBINED (node))
	{
	  walk_tree (&OMP_BODY (node), annotate_loops_in_kernels_regions,
		     (void *) info, NULL);
	  *walk_subtrees = 0;
	}
      else
	{
	  struct annotation_info nested_info
	    = { NULL_TREE, NULL_TREE, false, as_explicit_annotation,
		node, info };
	  if (info->state >= as_in_kernels_region)
	    do_not_annotate_loop_nest (info, as_explicit_annotation,
				       node);
	  walk_tree (&OMP_BODY (node), annotate_loops_in_kernels_regions,
		     (void *) &nested_info, NULL);
	  *walk_subtrees = 0;
	}
      break;

    case FOR_STMT:
      /* Try to annotate the loop if we are in a kernels region.
	 This will do a recursive traversal of the loop body in a new
	 info scope.  */
      if (info->state >= as_in_kernels_region)
	{
	  check_and_annotate_for_loop (nodeptr, NULL, info);
	  *walk_subtrees = 0;
	}
      break;

    case LABEL_EXPR:
      /* Possibly unstructured control flow.  Unless we perform further
	 analyses, we must assume that such control flow may enter the
	 current loop.  In this case, we must not parallelize the loop.  */
      if (info->state >= as_in_kernels_loop
	  && TREE_USED (LABEL_EXPR_LABEL (node)))
	do_not_annotate_loop_nest (info, as_invalid_control_flow, node);
      break;

    case GOTO_EXPR:
      /* Possibly unstructured control flow.  Unless we perform further
	 analyses, we must assume that such control flow may leave the
	 current loop.  In this case, we must not parallelize the loop.  */
      if (info->state >= as_in_kernels_loop)
	do_not_annotate_loop_nest (info, as_invalid_control_flow, node);
      break;

    case BREAK_STMT:
      /* A break statement.  Whether or not this is valid depends on the
	 enclosing context.  */
      if (info->state >= as_in_kernels_loop && !info->break_ok)
	do_not_annotate_loop (info, as_invalid_break, node);
      break;

    case RETURN_EXPR:
      /* A return leaves the entire loop nest.  */
      if (info->state >= as_in_kernels_loop)
	do_not_annotate_loop_nest (info, as_invalid_return, node);
      break;

    case CALL_EXPR:
      /* Direct function calls to builtins and functions marked as
	 OpenACC routines are allowed.  Reject indirect calls or calls
	 to non-routines.  */
      if (info->state >= as_in_kernels_loop)
	{
	  tree fn = CALL_EXPR_FN (node), fn_decl = NULL_TREE;
	  if (fn != NULL_TREE && TREE_CODE (fn) == FUNCTION_DECL)
	    fn_decl = fn;
	  else if (fn != NULL_TREE && TREE_CODE (fn) == ADDR_EXPR)
	    {
	      tree fn_op = TREE_OPERAND (fn, 0);
	      if (fn_op != NULL_TREE && TREE_CODE (fn_op) == FUNCTION_DECL)
		fn_decl = fn_op;
	    }
	  if (fn_decl == NULL_TREE)
	    do_not_annotate_loop_nest (info, as_invalid_call, node);
	  else if (!fndecl_built_in_p (fn_decl, BUILT_IN_NORMAL)
		   && !lookup_attribute ("oacc function",
					 DECL_ATTRIBUTES (fn_decl)))
	    do_not_annotate_loop_nest (info, as_invalid_call, node);
	}
      break;

    case MODIFY_EXPR:
      /* See if this assignment's LHS is one of the variables that must
	 not be modified in the loop body because they control termination
	 of the loop (or an enclosing loop in the nest).  */
      if (info->state >= as_in_kernels_loop)
	{
	  tree lhs = TREE_OPERAND (node, 0);
	  if (!is_local_var (lhs))
	    /* Early exit: This cannot be a variable we care about.  */
	    break;
	  /* Walk up the loop stack.  Invalidate the ones controlled by this
	     variable.  There may be several, if this variable is the common
	     iteration limit for several nested loops.  */
	  for (struct annotation_info *outer_loop = info; outer_loop != NULL;
	       outer_loop = outer_loop->next)
	    for (tree t = outer_loop->vars; t != NULL_TREE; t = TREE_CHAIN (t))
	      if (TREE_VALUE (t) == lhs)
		{
		  do_not_annotate_loop (outer_loop,
					as_invalid_modification,
					node);
		  break;
		}
	}
      break;

    case SWITCH_STMT:
      /* Needs special handling to allow break in the body.  */
      if (info->state >= as_in_kernels_loop)
	{
	  bool save_break_ok = info->break_ok;

	  walk_tree (&SWITCH_STMT_COND (node),
		     annotate_loops_in_kernels_regions,
		     (void *) info, NULL);
	  info->break_ok = true;
	  walk_tree (&SWITCH_STMT_BODY (node),
		     annotate_loops_in_kernels_regions,
		     (void *) info, NULL);
	  info->break_ok = save_break_ok;
	  *walk_subtrees = 0;
	}
      break;

    case WHILE_STMT:
      /* Needs special handling to allow break in the body.  */
      if (info->state >= as_in_kernels_loop)
	{
	  bool save_break_ok = info->break_ok;

	  walk_tree (&WHILE_COND (node), annotate_loops_in_kernels_regions,
		     (void *) info, NULL);
	  info->break_ok = true;
	  walk_tree (&WHILE_BODY (node), annotate_loops_in_kernels_regions,
		     (void *) info, NULL);
	  info->break_ok = save_break_ok;
	  *walk_subtrees = 0;
	}
      break;

    case DO_STMT:
      /* Needs special handling to allow break in the body.  */
      if (info->state >= as_in_kernels_loop)
	{
	  bool save_break_ok = info->break_ok;

	  walk_tree (&DO_COND (node), annotate_loops_in_kernels_regions,
		     (void *) info, NULL);
	  info->break_ok = true;
	  walk_tree (&DO_BODY (node), annotate_loops_in_kernels_regions,
		     (void *) info, NULL);
	  info->break_ok = save_break_ok;
	  *walk_subtrees = 0;
	}
      break;

    case STATEMENT_LIST:
      /* We iterate over these explicitly so that we can track the previous
	 statement in the chain.  It may be the initializer for a following
	 FOR_STMT node.  */
      if (info->state >= as_in_kernels_region)
	{
	  tree_stmt_iterator i = tsi_start (node);
	  tree_stmt_iterator prev, *prev_tsi = NULL;
	  while (!tsi_end_p (i))
	    {
	      tree *stmtptr = tsi_stmt_ptr (i);
	      if (TREE_CODE (*stmtptr) == FOR_STMT)
		{
		  check_and_annotate_for_loop (stmtptr, prev_tsi, info);
		  *walk_subtrees = 0;
		}
	      else
		walk_tree (stmtptr, annotate_loops_in_kernels_regions,
			   (void *) info, NULL);
	      prev = i;
	      prev_tsi = &prev;
	      tsi_next (&i);
	    }
	  *walk_subtrees = 0;
	}
      break;

    default:
      break;
    }

  return NULL_TREE;
}

/* Find for loops in OpenACC kernels regions that do not have OpenACC
   annotations but look like they might benefit from automatic
   parallelization.  Convert them from FOR_STMT to OMP_FOR nodes and
   add the equivalent of "#pragma acc loop auto" annotations for them.
   Assumes flag_openacc_kernels_annotate_loops is set.  */

void
c_oacc_annotate_loops_in_kernels_regions (tree decl,
					  tree (*unwrap_fn) (tree))
{
  struct annotation_info info
    = { NULL_TREE, NULL_TREE, true, as_outer, NULL_TREE, NULL };
  lang_specific_unwrap_initializer = unwrap_fn;
  walk_tree (&DECL_SAVED_TREE (decl), annotate_loops_in_kernels_regions,
	     (void *) &info, NULL);
}

/* Used to merge map clause information in c_omp_adjust_map_clauses.  */
struct map_clause
{
  tree clause;
  bool firstprivate_ptr_p;
  bool decl_mapped;
  bool omp_declare_target;
  map_clause (void) : clause (NULL_TREE), firstprivate_ptr_p (false),
    decl_mapped (false), omp_declare_target (false) { }
};

/* Adjust map clauses after normal clause parsing, mainly to turn specific
   base-pointer map cases into attach/detach and mark them addressable.  */
void
c_omp_adjust_map_clauses (tree clauses, bool is_target)
{
  if (!is_target)
    {
      /* If this is not a target construct, just turn firstprivate pointers
	 into attach/detach, the runtime will check and do the rest.  */

      for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
	if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
	    && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER
	    && DECL_P (OMP_CLAUSE_DECL (c))
	    && POINTER_TYPE_P (TREE_TYPE (OMP_CLAUSE_DECL (c))))
	  {
	    tree ptr = OMP_CLAUSE_DECL (c);
	    OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_ATTACH_DETACH);
	    c_common_mark_addressable_vec (ptr);
	  }
      return;
    }

  hash_map<tree, map_clause> maps;

  for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
    if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
	&& DECL_P (OMP_CLAUSE_DECL (c)))
      {
	/* If this is for a target construct, the firstprivate pointer
	   is changed to attach/detach if either is true:
	   (1) the base-pointer is mapped in this same construct, or
	   (2) the base-pointer is a variable place on the device by
	       "declare target" directives.

	   Here we iterate through all map clauses collecting these cases,
	   and merge them with a hash_map to process below.  */

	if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER
	    && POINTER_TYPE_P (TREE_TYPE (OMP_CLAUSE_DECL (c))))
	  {
	    tree ptr = OMP_CLAUSE_DECL (c);
	    map_clause &mc = maps.get_or_insert (ptr);
	    if (mc.clause == NULL_TREE)
	      mc.clause = c;
	    mc.firstprivate_ptr_p = true;

	    if (is_global_var (ptr)
		&& lookup_attribute ("omp declare target",
				     DECL_ATTRIBUTES (ptr)))
	      mc.omp_declare_target = true;
	  }
	else if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALLOC
		 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_TO
		 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FROM
		 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_TOFROM
		 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_TO
		 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_FROM
		 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_TOFROM)
	  {
	    map_clause &mc = maps.get_or_insert (OMP_CLAUSE_DECL (c));
	    mc.decl_mapped = true;
	  }
      }

  for (hash_map<tree, map_clause>::iterator i = maps.begin ();
       i != maps.end (); ++i)
    {
      map_clause &mc = (*i).second;

      if (mc.firstprivate_ptr_p
	  && (mc.decl_mapped || mc.omp_declare_target))
	{
	  OMP_CLAUSE_SET_MAP_KIND (mc.clause, GOMP_MAP_ATTACH_DETACH);
	  c_common_mark_addressable_vec (OMP_CLAUSE_DECL (mc.clause));
	}
    }
}

const struct c_omp_directive c_omp_directives[] = {
  /* Keep this alphabetically sorted by the first word.  Non-null second/third
     if any should precede null ones.  */
  { "allocate", nullptr, nullptr, PRAGMA_OMP_ALLOCATE,
    C_OMP_DIR_DECLARATIVE, false },
  { "assume", nullptr, nullptr, PRAGMA_OMP_ASSUME,
    C_OMP_DIR_INFORMATIONAL, false },
  { "assumes", nullptr, nullptr, PRAGMA_OMP_ASSUMES,
    C_OMP_DIR_INFORMATIONAL, false },
  { "atomic", nullptr, nullptr, PRAGMA_OMP_ATOMIC,
    C_OMP_DIR_CONSTRUCT, false },
  { "barrier", nullptr, nullptr, PRAGMA_OMP_BARRIER,
    C_OMP_DIR_STANDALONE, false },
  { "begin", "assumes", nullptr, PRAGMA_OMP_BEGIN,
    C_OMP_DIR_INFORMATIONAL, false },
  { "begin", "declare", "target", PRAGMA_OMP_BEGIN,
    C_OMP_DIR_DECLARATIVE, false },
  /* { "begin", "declare", "variant", PRAGMA_OMP_BEGIN,
    C_OMP_DIR_DECLARATIVE, false }, */
  /* { "begin", "metadirective", nullptr, PRAGMA_OMP_BEGIN,
    C_OMP_DIR_META, false },  */
  { "cancel", nullptr, nullptr, PRAGMA_OMP_CANCEL,
    C_OMP_DIR_STANDALONE, false },
  { "cancellation", "point", nullptr, PRAGMA_OMP_CANCELLATION_POINT,
    C_OMP_DIR_STANDALONE, false },
  { "critical", nullptr, nullptr, PRAGMA_OMP_CRITICAL,
    C_OMP_DIR_CONSTRUCT, false },
  /* { "declare", "mapper", nullptr, PRAGMA_OMP_DECLARE,
    C_OMP_DIR_DECLARATIVE, false },  */
  { "declare", "reduction", nullptr, PRAGMA_OMP_DECLARE,
    C_OMP_DIR_DECLARATIVE, true },
  { "declare", "simd", nullptr, PRAGMA_OMP_DECLARE,
    C_OMP_DIR_DECLARATIVE, true },
  { "declare", "target", nullptr, PRAGMA_OMP_DECLARE,
    C_OMP_DIR_DECLARATIVE, false },
  { "declare", "variant", nullptr, PRAGMA_OMP_DECLARE,
    C_OMP_DIR_DECLARATIVE, false },
  { "depobj", nullptr, nullptr, PRAGMA_OMP_DEPOBJ,
    C_OMP_DIR_STANDALONE, false },
  /* { "dispatch", nullptr, nullptr, PRAGMA_OMP_DISPATCH,
    C_OMP_DIR_CONSTRUCT, false },  */
  { "distribute", nullptr, nullptr, PRAGMA_OMP_DISTRIBUTE,
    C_OMP_DIR_CONSTRUCT, true },
  { "end", "assumes", nullptr, PRAGMA_OMP_END,
    C_OMP_DIR_INFORMATIONAL, false },
  { "end", "declare", "target", PRAGMA_OMP_END,
    C_OMP_DIR_DECLARATIVE, false },
  /* { "end", "declare", "variant", PRAGMA_OMP_END,
    C_OMP_DIR_DECLARATIVE, false }, */
  /* { "end", "metadirective", nullptr, PRAGMA_OMP_END,
    C_OMP_DIR_META, false },  */
  /* error with at(execution) is C_OMP_DIR_STANDALONE.  */
  { "error", nullptr, nullptr, PRAGMA_OMP_ERROR,
    C_OMP_DIR_UTILITY, false },
  { "flush", nullptr, nullptr, PRAGMA_OMP_FLUSH,
    C_OMP_DIR_STANDALONE, false },
  { "for", nullptr, nullptr, PRAGMA_OMP_FOR,
    C_OMP_DIR_CONSTRUCT, true },
  /* { "interop", nullptr, nullptr, PRAGMA_OMP_INTEROP,
    C_OMP_DIR_STANDALONE, false },  */
  { "loop", nullptr, nullptr, PRAGMA_OMP_LOOP,
    C_OMP_DIR_CONSTRUCT, true },
  { "masked", nullptr, nullptr, PRAGMA_OMP_MASKED,
    C_OMP_DIR_CONSTRUCT, true },
  { "master", nullptr, nullptr, PRAGMA_OMP_MASTER,
    C_OMP_DIR_CONSTRUCT, true },
  { "metadirective", nullptr, nullptr, PRAGMA_OMP_METADIRECTIVE,
    C_OMP_DIR_META, false },
  { "nothing", nullptr, nullptr, PRAGMA_OMP_NOTHING,
    C_OMP_DIR_UTILITY, false },
  /* ordered with depend clause is C_OMP_DIR_STANDALONE.  */
  { "ordered", nullptr, nullptr, PRAGMA_OMP_ORDERED,
    C_OMP_DIR_CONSTRUCT, true },
  { "parallel", nullptr, nullptr, PRAGMA_OMP_PARALLEL,
    C_OMP_DIR_CONSTRUCT, true },
  { "requires", nullptr, nullptr, PRAGMA_OMP_REQUIRES,
    C_OMP_DIR_INFORMATIONAL, false },
  { "scan", nullptr, nullptr, PRAGMA_OMP_SCAN,
    C_OMP_DIR_CONSTRUCT, true },
  { "scope", nullptr, nullptr, PRAGMA_OMP_SCOPE,
    C_OMP_DIR_CONSTRUCT, false },
  { "section", nullptr, nullptr, PRAGMA_OMP_SECTION,
    C_OMP_DIR_CONSTRUCT, false },
  { "sections", nullptr, nullptr, PRAGMA_OMP_SECTIONS,
    C_OMP_DIR_CONSTRUCT, false },
  { "simd", nullptr, nullptr, PRAGMA_OMP_SIMD,
    C_OMP_DIR_CONSTRUCT, true },
  { "single", nullptr, nullptr, PRAGMA_OMP_SINGLE,
    C_OMP_DIR_CONSTRUCT, false },
  { "target", "data", nullptr, PRAGMA_OMP_TARGET,
    C_OMP_DIR_CONSTRUCT, false },
  { "target", "enter", "data", PRAGMA_OMP_TARGET,
    C_OMP_DIR_STANDALONE, false },
  { "target", "exit", "data", PRAGMA_OMP_TARGET,
    C_OMP_DIR_STANDALONE, false },
  { "target", "update", nullptr, PRAGMA_OMP_TARGET,
    C_OMP_DIR_STANDALONE, false },
  { "target", nullptr, nullptr, PRAGMA_OMP_TARGET,
    C_OMP_DIR_CONSTRUCT, true },
  { "task", nullptr, nullptr, PRAGMA_OMP_TASK,
    C_OMP_DIR_CONSTRUCT, false },
  { "taskgroup", nullptr, nullptr, PRAGMA_OMP_TASKGROUP,
    C_OMP_DIR_CONSTRUCT, false },
  { "taskloop", nullptr, nullptr, PRAGMA_OMP_TASKLOOP,
    C_OMP_DIR_CONSTRUCT, true },
  { "taskwait", nullptr, nullptr, PRAGMA_OMP_TASKWAIT,
    C_OMP_DIR_STANDALONE, false },
  { "taskyield", nullptr, nullptr, PRAGMA_OMP_TASKYIELD,
    C_OMP_DIR_STANDALONE, false },
  /* { "tile", nullptr, nullptr, PRAGMA_OMP_TILE,
    C_OMP_DIR_CONSTRUCT, false },  */
  { "teams", nullptr, nullptr, PRAGMA_OMP_TEAMS,
    C_OMP_DIR_CONSTRUCT, true },
  { "threadprivate", nullptr, nullptr, PRAGMA_OMP_THREADPRIVATE,
    C_OMP_DIR_DECLARATIVE, false },
 { "unroll", nullptr, nullptr, PRAGMA_OMP_UNROLL,
    C_OMP_DIR_CONSTRUCT, false },
};

/* Find (non-combined/composite) OpenMP directive (if any) which starts
   with FIRST keyword and for multi-word directives has SECOND and
   THIRD keyword after it.  */

const struct c_omp_directive *
c_omp_categorize_directive (const char *first, const char *second,
			    const char *third)
{
  const size_t n_omp_directives = ARRAY_SIZE (c_omp_directives);
  for (size_t i = 0; i < n_omp_directives; i++)
    {
      if ((unsigned char) c_omp_directives[i].first[0]
	  < (unsigned char) first[0])
	continue;
      if ((unsigned char) c_omp_directives[i].first[0]
	  > (unsigned char) first[0])
	break;
      if (strcmp (c_omp_directives[i].first, first))
	continue;
      if (!c_omp_directives[i].second)
	return &c_omp_directives[i];
      if (!second || strcmp (c_omp_directives[i].second, second))
	continue;
      if (!c_omp_directives[i].third)
	return &c_omp_directives[i];
      if (!third || strcmp (c_omp_directives[i].third, third))
	continue;
      return &c_omp_directives[i];
    }
  return NULL;
}

static tree
c_omp_expand_metadirective_r (vec<struct omp_metadirective_variant> &candidates,
			      hash_map<tree, tree> &body_labels,
			      unsigned index)
{
  struct omp_metadirective_variant &candidate = candidates[index];
  tree if_block = push_stmt_list ();
  if (candidate.directive != NULL_TREE)
    add_stmt (candidate.directive);
  if (candidate.body != NULL_TREE)
    {
      tree *label = body_labels.get (candidate.body);
      if (label != NULL)
	add_stmt (build1 (GOTO_EXPR, void_type_node, *label));
      else
	{
	  tree body_label = create_artificial_label (UNKNOWN_LOCATION);
	  add_stmt (build1 (LABEL_EXPR, void_type_node, body_label));
	  add_stmt (candidate.body);
	  body_labels.put (candidate.body, body_label);
	}
    }
  if_block = pop_stmt_list (if_block);

  if (index == candidates.length () - 1)
    return if_block;

  tree cond = candidate.selector;
  gcc_assert (cond != NULL_TREE);

  tree else_block = c_omp_expand_metadirective_r (candidates, body_labels,
						  index + 1);
  tree ret = push_stmt_list ();
  tree stmt = build3 (COND_EXPR, void_type_node, cond, if_block, else_block);
  add_stmt (stmt);
  ret = pop_stmt_list (ret);

  return ret;
}

tree
c_omp_expand_metadirective (vec<struct omp_metadirective_variant> &candidates)
{
  hash_map<tree, tree> body_labels;
  return c_omp_expand_metadirective_r (candidates, body_labels, 0);
}