aboutsummaryrefslogtreecommitdiff
path: root/gcc/config/avr/avr-passes.cc
blob: 49473efbb0d389e49edc09e8022d26b7bc8857d6 (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
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
/* Support for avr-passes.def for AVR 8-bit microcontrollers.
   Copyright (C) 2024 Free Software Foundation, Inc.

   This file is part of GCC.

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

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

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

#define IN_TARGET_CODE 1

#define INCLUDE_ARRAY
#define INCLUDE_VECTOR
#include "config.h"
#include "system.h"
#include "intl.h"
#include "coretypes.h"
#include "backend.h"
#include "target.h"
#include "rtl.h"
#include "tree.h"
#include "cfghooks.h"
#include "cfganal.h"
#include "df.h"
#include "memmodel.h"
#include "tm_p.h"
#include "optabs.h"
#include "regs.h"
#include "emit-rtl.h"
#include "recog.h"
#include "explow.h"
#include "cfgrtl.h"
#include "context.h"
#include "tree-pass.h"
#include "insn-attr.h"


#define CONST_INT_OR_FIXED_P(X) (CONST_INT_P (X) || CONST_FIXED_P (X))

#define FIRST_GPR (AVR_TINY ? REG_18 : REG_2)

namespace
{

/////////////////////////////////////////////////////////////////////////////
// Before we start with the very code, introduce some helpers that are
// quite generic, though up to now only avr-fuse-add makes use of them.

/* Get the next / previous NONDEBUG_INSN_P after INSN in basic block BB.
   This assumes we are in CFG layout mode so that BLOCK_FOR_INSN()
   can be used.  */

static rtx_insn *
next_nondebug_insn_bb (basic_block bb, rtx_insn *insn, bool forward = true)
{
  while (insn)
    {
      insn = forward ? NEXT_INSN (insn) : PREV_INSN (insn);

      if (insn && NONDEBUG_INSN_P (insn))
	return BLOCK_FOR_INSN (insn) == bb ? insn : nullptr;
    }

  return insn;
}

static rtx_insn *
prev_nondebug_insn_bb (basic_block bb, rtx_insn *insn)
{
  return next_nondebug_insn_bb (bb, insn, false);
}


/* Like `single_set' with the addition that it sets REGNO_SCRATCH when the
   insn is a single_set with a QImode scratch register.  When the insn has
   no QImode scratch or just a scratch:QI, then set REGNO_SCRATCH = 0.
   The assumption is that the function is only used after the splits for
   REG_CC so that the pattern is a parallel with 2 elements (INSN has no
   scratch operand), or 3 elements (INSN does have a scratch operand).  */

static rtx
single_set_with_scratch (rtx_insn *insn, int &regno_scratch)
{
  regno_scratch = 0;

  if (! INSN_P (insn))
    return NULL_RTX;

  rtx set, clo, reg, pat = PATTERN (insn);

  // Search for SET + CLOBBER(QI) + CLOBBER(CC).
  if (GET_CODE (pat) == PARALLEL
      && XVECLEN (pat, 0) == 3
      && GET_CODE (set = XVECEXP (pat, 0, 0)) == SET
      // At this pass, all insn are endowed with clobber(CC).
      && GET_CODE (clo = XVECEXP (pat, 0, 2)) == CLOBBER
      && GET_MODE (XEXP (clo, 0)) == CCmode
      && GET_CODE (clo = XVECEXP (pat, 0, 1)) == CLOBBER
      && REG_P (reg = XEXP (clo, 0))
      && GET_MODE (reg) == QImode)
    {
      regno_scratch = REGNO (reg);
      return set;
    }

  return single_set (insn);
}

// Emit pattern PAT, and ICE when the insn is not valid / not recognized.

static rtx_insn *
emit_valid_insn (rtx pat)
{
  rtx_insn *insn = emit_insn (pat);

  if (! valid_insn_p (insn))  // Also runs recog().
    fatal_insn ("emit unrecognizable insn", insn);

  return insn;
}

// Emit a single_set with an optional scratch operand.  This function
// asserts that the new insn is valid and recognized.

static rtx_insn *
emit_valid_move_clobbercc (rtx dest, rtx src, rtx scratch = NULL_RTX)
{
  rtx pat = scratch
    ? gen_gen_move_clobbercc_scratch (dest, src, scratch)
    : gen_gen_move_clobbercc (dest, src);

  return emit_valid_insn (pat);
}

// One bit for each GRP in REG_0 ... REG_31.
using gprmask_t = uint32_t;

// True when this is a valid GPR number for ordinary code, e.g.
// registers wider than 2 bytes have to start at an exven regno.
// TMP_REG and ZERO_REG are not considered valid, even though
// the C source can use register vars with them.
static inline bool
gpr_regno_p (int regno, int n_bytes = 1)
{
  return (IN_RANGE (regno, FIRST_GPR, REG_32 - n_bytes)
	  // Size in { 1, 2, 3, 4, 8 } bytes.
	  && ((1u << n_bytes) & 0x11e)
	  // Registers >= 2 bytes start at an even regno.
	  && (n_bytes == 1 || regno % 2 == 0));
}

// There are cases where the C source defines local reg vars
// for R1 etc.  The assumption is that this is handled before
// calling this function, e.g. by skipping code when a register
// overlaps with a fixed register.
static inline gprmask_t
regmask (int regno, int size)
{
  gcc_checking_assert (gpr_regno_p (regno, size));
  gprmask_t bits = (1u << size) - 1;

  return bits << regno;
}

// Mask for hard register X that's some GPR, including fixed regs like R0.
static gprmask_t
regmask (rtx x)
{
  gcc_assert (REG_P (x));
  gprmask_t bits = (1u << GET_MODE_SIZE (GET_MODE (x))) - 1;

  return bits << REGNO (x);
}


// Whether X has bits in the range [B0 ... B1]
static inline bool
has_bits_in (gprmask_t x, int b0, int b1)
{
  if (b0 > b1 || b0 > 31 || b1 < 0)
    return false;

  const gprmask_t m = (2u << (b1 - b0)) - 1;
  return x & (m << b0);
}


template<typename T>
T bad_case ()
{
  gcc_unreachable ();
}

#define select false ? bad_case


namespace AVRasm
{
  // Returns true when we a scratch reg is needed in order to get
  // (siged or unsigned) 8-bit value VAL in some GPR.
  // When it's about costs rather than the sheer requirement for a
  // scratch, see also AVRasm::constant_cost.
  static inline bool ldi_needs_scratch (int regno, int val)
  {
    return regno < REG_16 && IN_RANGE (val & 0xff, 2, 254);
  }

  // Return a byte value x >= 0 such that  x <code> y == x for all y, or -1.
  static inline int neutral_val (rtx_code code)
  {
    return select<int>()
      : code == AND ? 0xff
      : code == IOR ? 0x00
      : code == XOR ? 0x00
      : code == PLUS ? 0
      : -1;
  }

  // When there exists a value x such that the image of the function
  //   y -> y <code> x  has order 1, then return that x.  Else return -1.
  static inline int image1_val (rtx_code code)
  {
    return select<int>()
      : code == AND ? 0x00
      : code == IOR ? 0xff
      : -1;
  }

  // Cost of 8-bit binary operation  x o= VAL  provided a scratch is
  // available as needed.
  static int constant_cost (rtx_code code, int regno, uint8_t val)
  {
    bool needs_scratch_p = select<bool>()
      : code == PLUS ? regno < REG_16 && val != 1 && val != 0xff
      : code == XOR ? val != 0xff && (regno < REG_16 || val != 0x80)
      : code == IOR ? regno < REG_16
      : code == AND ? regno < REG_16 && val != 0
      : code == SET ? regno < REG_16 && val != 0
      : bad_case<bool> ();

    return val == AVRasm::neutral_val (code)
      ? 0
      : 1 + needs_scratch_p;
  }
}; // AVRasm


// Returns the mode mask for a mode size of SIZE bytes.
static uint64_t size_to_mask (int size)
{
  return ((uint64_t) 2 << (8 * size - 1)) - 1;
}

// Return the scalar int mode for a modesize of 1, 2, 3, 4 or 8 bytes.
static machine_mode size_to_mode (int size)
{
  return select<machine_mode>()
    : size == 1 ? QImode
    : size == 2 ? HImode
    : size == 3 ? PSImode
    : size == 4 ? SImode
    : size == 8 ? DImode
    : bad_case<machine_mode> ();
}


//////////////////////////////////////////////////////////////////////////////
// Optimize moves after reload: -mfuse-move=<0,23>

/* The purpose of this pass is to perform optimizations after reload
   like the following ones:

   Without optimization		     |	 With optimization
   ====================		     |	 =================

   long long fn_zero (void)	    (1)
   {
      return 0;
   }

   ldi r18, 0	  ;  movqi_insn	     |	 ldi r18, 0	;  movqi_insn
   ldi r19, 0	  ;  movqi_insn	     |	 ldi r19, 0	;  movqi_insn
   ldi r20, 0	  ;  movqi_insn	     |	 movw r20, r18	;  *movhi
   ldi r21, 0	  ;  movqi_insn	     |
   ldi r22, 0	  ;  movqi_insn	     |	 movw r22, r18	;  *movhi
   ldi r23, 0	  ;  movqi_insn	     |
   ldi r24, 0	  ;  movqi_insn	     |	 movw r24, r18	;  *movhi
   ldi r25, 0	  ;  movqi_insn	     |
   ret				     |	 ret

   int fn_eq0 (char c)		    (2)
   {
       return c == 0;
   }

   mov r18, r24	   ;  movqi_insn     |	 mov r18, r24	;  movqi_insn
   ldi r24, 1	   ;  *movhi	     |	 ldi r24, 1	;  *movhi
   ldi r25, 0			     |	 ldi r25, 0
   cp  r18, ZERO   ;  cmpqi3	     |	 cpse r18, ZERO ;  peephole
   breq .+4	   ;  branch	     |
   ldi r24, 0	   ;  *movhi	     |	 ldi r24, 0	;  movqi_insn
   ldi r25, 0			     |
   ret				     |	 ret

   int a, b;			    (3)

   void fn_store_ab (void)
   {
       a = 1;
       b = -1;
   }

   ldi r24, 1	   ;  *movhi	     |	ldi r24, 1	 ;  *movhi
   ldi r25, 0			     |	ldi r25, 0
   sts a+1, r25	   ;  *movhi	     |	sts a+1, r25	 ;  *movhi
   sts a,   r24			     |	sts a,	 r24
   ldi r24, -1	   ;  *movhi	     |	sbiw r24, 2	 ;  *addhi3
   ldi r25, -1			     |
   sts b+1, r25	   ;  *movhi	     |	sts b+1, r25	 ;  *movhi
   sts b,   r24			     |	sts b,	 r24
   ret				     |	ret

   unsigned fn_crc (unsigned x, unsigned y)   (4)
   {
       for (char i = 8; i--; x <<= 1)
	   y ^= (x ^ y) & 0x80 ? 79U : 0U;
       return y;
   }

   movw r18, r24   ;  *movhi	     |	movw r18, r24	 ;  *movhi
   movw r24, r22   ;  *movhi	     |	movw r24, r22	 ;  *movhi
   ldi	r22, 8	   ;  movqi_insn     |	ldi  r22, 8	 ;  movqi_insn
  .L13:				     | .L13:
   movw r30, r18   ;  *movhi	     |	movw r30, r18	 ;  *movhi
   eor	r30, r24   ;  *xorqi3	     |	eor  r30, r24	 ;  *xorqi3
   eor	r31, r25   ;  *xorqi3	     |	eor  r31, r25	 ;  *xorqi3
   mov	r20, r30   ;  *andhi3	     |	mov  r20, r30	 ;  *andqi3
   andi r20, 1<<7		     |	andi r20, 1<<7
   clr	r21			     |
   sbrs r30, 7	   ;  *sbrx_branchhi |	sbrc r30, 7	 ;  *sbrx_branchhi
   rjmp .+4			     |
   ldi	r20, 79	   ;  movqi_insn     |	ldi  r20, 79	 ;  movqi_insn
   ldi	r21, 0	   ;  movqi_insn     |
   eor	r24, r20   ;  *xorqi3	     |	eor r24, r20	 ;  *xorqi3
   eor	r25, r21   ;  *xorqi3	     |
   lsl	r18	   ;  *ashlhi3_const |	lsl  r18	 ;  *ashlhi3_const
   rol	r19			     |	rol  r19
   subi r22, 1	   ;  *op8.for.cczn.p|	subi r22, 1	 ;  *op8.for.cczn.plus
   brne .L13	   ;  branch_ZN	     |	brne .L13	 ;  branch_ZN
   ret				     |	ret

   #define SPDR (*(uint8_t volatile*) 0x2c)     (5)

   void fn_PR49807 (long big)
   {
       SPDR = big >> 24;
       SPDR = big >> 16;
       SPDR = big >> 8;
       SPDR = big;
   }

   movw r20, r22   ;  *movhi	     |	movw r20, r22	 ;  *movhi
   movw r22, r24   ;  *movhi	     |	movw r22, r24	 ;  *movhi
   mov	r24, r23   ;  *ashrsi3_const |
   clr	r27			     |
   sbrc r24,7			     |
   com	r27			     |
   mov	r25, r27		     |
   mov	r26, r27		     |
   out	0xc, r24   ;  movqi_insn     |	out 0xc, r23	 ;  movqi_insn
   movw r24, r22   ;  *ashrsi3_const |
   clr	r27			     |
   sbrc r25, 7			     |
   com	r27			     |
   mov	r26, r27		     |
   out	0xc, r24   ;  movqi_insn     |	out 0xc, r24	 ;  movqi_insn
   clr	r27	   ;  *ashrsi3_const |
   sbrc r23, 7			     |
   dec	r27			     |
   mov	r26, r23		     |
   mov	r25, r22		     |
   mov	r24, r21		     |
   out	0xc, r24   ;  movqi_insn     |	out 0xc, r21	 ;  movqi_insn
   out	0xc, r20   ;  movqi_insn     |	out 0xc, r20	 ;  movqi_insn
   ret				     |	ret

   The insns of each basic block are traversed from first to last.
   Each insn is optimized on its own, or may be fused with the
   previous insn like in example (1).
      As the insns are traversed, memento_t keeps track of known values
   held in the GPRs (general purpse registers) R2 ... R31 by simulating
   the effect of the current insn in memento_t.apply_insn().
      The basic blocks are traversed in reverse post order so as to
   maximize the chance that GPRs from all preceding blocks are known,
   which is the case in example (2).  The traversal of the basic block
   is performed by bbinfo_t.optimize_one_function().
      bbinfo_t.optimize_one_block() traverses the insns of a BB and tries
   the following optimizations:

   bbinfo_t::try_fuse_p
      Try to fuse two 8-bit insns to one MOVW like in (1).

   bbinfo_t::try_simplify_p
      Only perform the simplest optimizations that don't impede the
      traceability of the generated code, which are:
      - Transform operations like  Rn = Rn=0 ^ Rm  to  Rn = Rm.
      - Remove insns that are no-ops like  Rn = Rn ^ Rm=0.

   bbinfo_t::try_bin_arg1_p
      In insns like  EOR Rn, arg1  where arg1 is known or is a reg that
      dies in the insn, *and* there is a different register Rm that's
      known to contain the same value, then arg1 is replaced with Rm.

   bbinfo_t::try_split_ldi_p
      Tries to simplify loads of constants like in examples (1), (2) and (3).
      It may use arithmetic instructions like AND with registers that
      are holding known values when this is profitable.

   bbinfo_t::try_split_any_p
      Split all insns where the operation can be performed on individual
      bytes, like andsi3.  In example (4) the andhi3 can be optimized
      to an andqi3.
*/


// A basic block with additional information like the GPR state.
// The main entry point for the pass.  Runs various strategies
// like try_fuse, try_simplify, try_bin_arg1, try_split_ldi, try_split_any
// depending on -mfuse-add=<0,11>.
struct bbinfo_t;

// Additional insn information on a  REG = non-memory  single_set insn
// for quick access.  Only valid when the m_size member is non-zero.
struct insninfo_t;

// Helper classes with data needed by the try_xxx optimizers.
struct optimize_data_t;
struct insn_optimize_data_t;

// Records which GPRs R0 ... R31 are holding a known value,
// and which values these are.
struct memento_t;

// Abstract Interpretation of expressions.
// absint_val_t represents an 8-bit value that equals the content of
//    some GPR, or equals some known value (or both, or none of them).
// absint_byte_t represents an 8-bit entity that is equivalent to
//    an absint_val_t, or is equivalent to some (unary or binary) operation
//    on absint_val_t's like NOT, AND, IOR, XOR that operate bit-wise (and
//    hence also byte-wise).
// absint_t represents an array of absint_byte_t's.  When some insn is applied
//    to a GPR state, then memento_t.apply_insn() represents the RHS of
//    a single_set as an absint_t, and then applies that result to the GPRs.
//    For example, in  int y = x << 8  the representation is  x = [r25; r24]
//    and  RHS = [r24; 00].
struct absint_val_t;
class absint_byte_t;
struct absint_t;

// A ply_t is a potential step towards an optimal sequence to load a constant
// value into a multi-byte register.  A ply_t loosely relates to one AVR
// instruction, but it may also represent a sequence of instructions.
// For example, loading a constant into a lower register when no sratch reg
// is available may take up to 4 instructions.  There is no 1:1 correspondence
// to insns, either.
//    try_split_ldi determines the best sequence of ply_t's by means of a
// brute-force search with tree pruning:  It's much too complicated to
// construct a good sequence directly, but there are many conditions that
// good sequence will satisfy, implemented in bbinfo_t::find_plies.
struct ply_t;
struct plies_t;

// The maximal number of ply_t's in any conceivable optimal solution
// that is better than what a vanilla mov<mode> generates.
// This is 6 for modes <= 4 and 8 for modes == 8.
static constexpr int N_BEST_PLYS = 8;

#define FUSE_MOVE_MAX_MODESIZE 8

#include "avr-passes-fuse-move.h"

// Static members.

gprmask_t memento_t::fixed_regs_mask;

// Statistics.
int ply_t::n_ply_ts;
int ply_t::max_n_ply_ts;
int plies_t::max_n_plies;

bbinfo_t *bbinfo_t::bb_info;
int bbinfo_t::tick;
bbinfo_t::find_plies_data_t *bbinfo_t::fpd;

// Which optimizations should be performed.
bool bbinfo_t::try_fuse_p;
bool bbinfo_t::try_bin_arg1_p;
bool bbinfo_t::try_split_ldi_p;
bool bbinfo_t::try_split_any_p;
bool bbinfo_t::try_simplify_p;
bool bbinfo_t::use_arith_p;
bool bbinfo_t::use_set_some_p;


// Abstract Interpretation of expressions.
// A bunch of absint_byte_t's.

struct absint_t
{
  static constexpr int eq_size = FUSE_MOVE_MAX_MODESIZE;
  std::array<absint_byte_t, eq_size> eq;

  rtx xexp = NULL_RTX;
  rtx xexp_new = NULL_RTX;

  absint_byte_t &operator[] (int i)
  {
    gcc_assert (IN_RANGE (i, 0, absint_t::eq_size - 1));
    return eq[i];
  }

  const absint_byte_t &operator[] (int i) const
  {
    gcc_assert (IN_RANGE (i, 0, absint_t::eq_size - 1));
    return eq[i];
  }

  absint_t () {}

  absint_t (rtx xold)
    : xexp(xold)
  {}

  absint_t (rtx xold, rtx xnew, int n_bytes)
    : xexp(xold), xexp_new(xnew)
  {
    gcc_assert (n_bytes <= eq_size);
    if (xnew)
      for (int i = 0; i < n_bytes; ++i)
	eq[i].learn_val8 (avr_uint8 (xnew, i));
  }

  // CODE != UNKNOWN: Maximal index of a byte with code CODE, or -1.
  // CODE == UNKNOWN: Maximal index of a byte with known CODE, or -1.
  int max_knows (rtx_code code = UNKNOWN) const
  {
    for (int i = eq_size - 1; i >= 0; --i)
      if ((code == UNKNOWN && ! eq[i].can (UNKNOWN))
	  || (code != UNKNOWN && eq[i].can (code)))
	return i;
    return -1;
  }

  // CODE != UNKNOWN: Maximal i such that all bytes < i have code CODE.
  // CODE == UNKNOWN: Maximal i such that all bytes < i have code != UNKNOWN.
  int end_knows (rtx_code code = UNKNOWN) const
  {
    for (int i = 0; i < eq_size; ++i)
      if ((code == UNKNOWN && eq[i].can (UNKNOWN))
	  || (code != UNKNOWN && ! eq[i].can (code)))
	return i;
    return eq_size;
  }

  // Number of bytes for which there is usable information.
  int popcount () const
  {
    int pop = 0;
    for (int i = 0; i < eq_size; ++i)
      pop += ! eq[i].can (UNKNOWN);
    return pop;
  }

  // Get the value under the assumption that all eq[].val8 are known.
  uint64_t get_value (int n_bytes, bool strict = true) const
  {
    gcc_assert (IN_RANGE (n_bytes, 1, eq_size));
    gcc_assert (! strict || end_knows (CONST_INT) >= n_bytes);

    uint64_t val = 0;
    for (int i = n_bytes - 1; i >= 0; --i)
      val = 256 * val + eq[i].val8 (strict);
    return val;
  }

  // Get n-byte value as a const_int, or NULL_RTX when (partially) unknown.
  rtx get_value_as_const_int (int n_bytes) const
  {
    gcc_checking_assert (gpr_regno_p (REG_24, n_bytes));

    if (end_knows (CONST_INT) < n_bytes)
      return NULL_RTX;

    const uint64_t val = get_value (n_bytes);
    const machine_mode mode = size_to_mode (n_bytes);

    return gen_int_mode (val, mode);
  }

  // Find a 16-bit register that contains the same value like held
  // in positions I1 and I2 (if any).  Return 0 when nothing appropriate
  // for a MOVW is found.
  int reg16_with_value (int i1, int i2, const memento_t &memo) const
  {
    if (i1 == (i2 ^ 1))
      {
	const int lo8 = eq[i1 & ~1].val8 (false);
	const int hi8 = eq[i1 | 1].val8 (false);
	if (lo8 >= 0 && hi8 >= 0)
	  return memo.reg16_with_value (lo8, hi8, 0);
      }
    return 0;
  }

  // When X is a REG rtx with a known content as of MEMO, then return
  // the respective value as a constant for mode MODE.
  // If X is NULL_RTX, or not a REG, or not known, then return NULL_RTX.
  static rtx maybe_fold (rtx x, const memento_t &memo)
  {
    int n_bytes;

    if (x != NULL_RTX
	&& REG_P (x)
	&& (n_bytes = GET_MODE_SIZE (GET_MODE (x))) <= FUSE_MOVE_MAX_MODESIZE
	&& gpr_regno_p (REGNO (x), n_bytes))
      {
	rtx xval = memo.get_value_as_const_int (REGNO (x), n_bytes);
	if (xval)
	  return avr_chunk (GET_MODE (x), xval, 0);
      }

    return NULL_RTX;
  }

  // Try to conclude about the bytes that comprise X.  DEST_MODE is the
  // context mode that is used when X is CONST_INT and has VOIDmode.
  static absint_t explore (rtx x, const memento_t &memo,
			   machine_mode dest_mode = VOIDmode)
  {
    const rtx_code code = GET_CODE (x);
    bool worth_dumping = dump_file && (dump_flags & TDF_FOLDING);

    const machine_mode mode = GET_MODE (x) == VOIDmode
      ? dest_mode
      : GET_MODE (x);

    const int n_bytes = mode == VOIDmode && CONST_INT_P (x)
      ? absint_t::eq_size
      : GET_MODE_SIZE (mode);

    if (! IN_RANGE (n_bytes, 1, absint_t::eq_size))
      return absint_t (x);

    // Eat our own dog food as produced by try_plit_ldi.

    rtx xop0 = BINARY_P (x) || UNARY_P (x) ? XEXP (x, 0) : NULL_RTX;
    rtx xval0 = xop0 && CONST_INT_OR_FIXED_P (xop0)
      ? xop0
      : absint_t::maybe_fold (xop0, memo);

    if (UNARY_P (x)
	&& REG_P (xop0)
	&& GET_MODE (xop0) == mode
	&& xval0)
      {
	rtx y = simplify_unary_operation (code, mode, xval0, mode);
	if (y && CONST_INT_OR_FIXED_P (y))
	  return absint_t (x, y, n_bytes);
      }

    rtx xop1 = BINARY_P (x) ? XEXP (x, 1) : NULL_RTX;
    rtx xval1 = xop1 && CONST_INT_OR_FIXED_P (xop1)
      ? xop1
      : absint_t::maybe_fold (xop1, memo);

    if (BINARY_P (x)
	&& xval0 && xval1)
      {
	rtx y = simplify_binary_operation (code, mode, xval0, xval1);
	if (y && CONST_INT_OR_FIXED_P (y))
	  return absint_t (x, y, n_bytes);
      }

    // No fold to a constant value was found:
    // Look at the individual bytes more closely.

    absint_t ai (x);

    switch (code)
      {
      default:
	worth_dumping = false;
	break;

      case REG:
	if (END_REGNO (x) <= REG_32
	    && ! (regmask (x) & memento_t::fixed_regs_mask))
	  for (unsigned r = REGNO (x); r < END_REGNO (x); ++r)
	    {
	      ai[r - REGNO (x)].learn_regno (r);
	      if (memo.knows (r))
		ai[r - REGNO (x)].learn_val8 (memo.value (r));
	    }
	break;

      CASE_CONST_UNIQUE:
	ai = absint_t (x, x, n_bytes);
	break;

      case ASHIFT:
      case ASHIFTRT:
      case LSHIFTRT:
      case ROTATE:
      case ROTATERT:
	if ((CONST_INT_P (xop1) && INTVAL (xop1) >= 8)
	    // DImode shift offsets for transparent calls are shipped in R16.
	    || n_bytes == 8)
	  ai = explore_shift (x, memo);
	break;

      case AND:
      case IOR:
      case XOR:
	{
	  const absint_t ai0 = absint_t::explore (xop0, memo, mode);
	  const absint_t ai1 = absint_t::explore (xop1, memo, mode);
	  for (int i = 0; i < n_bytes; ++i)
	    ai[i] = absint_byte_t (code, ai0[i], ai1[i]);
	}
	break;

      case NOT:
	{
	  const absint_t ai0 = absint_t::explore (xop0, memo);
	  for (int i = 0; i < n_bytes; ++i)
	    ai[i] = absint_byte_t (NOT, ai0[i]);
	}
	break;

      case ZERO_EXTEND:
      case SIGN_EXTEND:
	{
	  const absint_t ai0 = absint_t::explore (xop0, memo);
	  const int ai0_size = GET_MODE_SIZE (GET_MODE (xop0));
	  const absint_byte_t b_signs = ai0[ai0_size - 1].get_signs (code);
	  for (int i = 0; i < n_bytes; ++i)
	    ai[i] = i < ai0_size ? ai0[i] : b_signs;
	}
	break;

      case PLUS:
      case MINUS:
	if (SCALAR_INT_MODE_P (mode)
	    || ALL_SCALAR_FIXED_POINT_MODE_P (mode))
	  {
	    const absint_t ai0 = absint_t::explore (xop0, memo, mode);
	    const absint_t ai1 = absint_t::explore (xop1, memo, mode);
	    if (code == MINUS)
	      for (int i = 0; i < n_bytes && ai1[i].val8 (false) == 0; ++i)
		ai[i] = ai0[i];

	    if (code == PLUS)
	      for (int i = 0; i < n_bytes; ++i)
		{
		  if (ai0[i].val8 (false) == 0)
		    ai[i] = ai1[i];
		  else if (ai1[i].val8 (false) == 0)
		    ai[i] = ai0[i];
		  else
		    {
		      ai[i] = absint_byte_t (code, ai0[i], ai1[i]);
		      break;
		    }
		}

	    if (code == PLUS
		&& GET_CODE (xop0) == ZERO_EXTEND
		&& CONST_INT_P (xop1))
	      {
		rtx exop = XEXP (xop0, 0);
		int exsize = GET_MODE_SIZE (GET_MODE (exop));
		rtx lo_xop1 = avr_chunk (GET_MODE (exop), xop1, 0);
		if (lo_xop1 == const0_rtx)
		  for (int i = exsize; i < n_bytes; ++i)
		    ai[i] = ai1[i];
	      }
	  }
	break; // PLUS, MINUS

      case MULT:
	if (GET_MODE (xop0) == mode
	    && SCALAR_INT_MODE_P (mode))
	  {
	    // The constant may be located in xop0's zero_extend...
	    const absint_t ai0 = absint_t::explore (xop0, memo, mode);
	    const absint_t ai1 = absint_t::explore (xop1, memo, mode);
	    const int end0 = ai0.end_knows (CONST_INT);
	    const int end1 = ai1.end_knows (CONST_INT);
	    const uint64_t mul0 = end0 > 0 ? ai0.get_value (end0) : 1;
	    const uint64_t mul1 = end1 > 0 ? ai1.get_value (end1) : 1;
	    // Shifting in off/8 zero bytes from the right.
	    const int off = mul0 * mul1 != 0 ? ctz_hwi (mul0 * mul1) : 0;
	    for (int i = 0; i < off / 8; ++i)
	      ai[i].learn_val8 (0);
	  }
	break; // MULT

      case BSWAP:
	if (GET_MODE (xop0) == mode)
	  {
	    const absint_t ai0 = absint_t::explore (xop0, memo);
	    for (int i = 0; i < n_bytes; ++i)
	      ai[i] = ai0[n_bytes - 1 - i];
	  }
	break;
      } // switch code

    if (worth_dumping)
      {
	avr_dump (";; AI.explore %C:%m ", code, mode);
	ai.dump ();
      }

    for (int i = 0; i < n_bytes; ++i)
      gcc_assert (ai[i].check ());

    return ai;
  }

  // Helper for the method above.
  static absint_t explore_shift (rtx x, const memento_t &memo)
  {
    absint_t ai (x);

    const rtx_code code = GET_CODE (x);
    const machine_mode mode = GET_MODE (x);
    const int n_bytes = GET_MODE_SIZE (mode);

    if (! BINARY_P (x))
      return ai;

    rtx xop0 = XEXP (x, 0);
    rtx xop1 = XEXP (x, 1);

    // Look at shift offsets of DImode more closely;
    // they are in R16 for __lshrdi3 etc.  Patch xop1 on success.
    if (n_bytes == 8
	&& ! CONST_INT_P (xop1)
	&& GET_MODE (xop0) == mode)
      {
	const int n_off = GET_MODE_SIZE (GET_MODE (xop1));
	const absint_t aoff = absint_t::explore (xop1, memo);
	xop1 = aoff.get_value_as_const_int (n_off);
      }

    if (! xop1
	|| GET_MODE (xop0) != mode
	|| ! IN_RANGE (n_bytes, 1, FUSE_MOVE_MAX_MODESIZE)
	|| ! CONST_INT_P (xop1)
	|| ! IN_RANGE (INTVAL (xop1), 8, 8 * n_bytes - 1))
      return ai;

    const int off = INTVAL (xop1);
    const absint_t ai0 = absint_t::explore (xop0, memo);

    switch (GET_CODE (x))
      {
      default:
	break;

      case ASHIFT:
	// Shifting in 0x00's from the right.
	for (int i = 0; i < off / 8; ++i)
	  ai[i].learn_val8 (0);
	break;

      case LSHIFTRT:
      case ASHIFTRT:
	{
	  // Shifting in 0x00's or signs from the left.
	  absint_byte_t b_signs = ai0[n_bytes - 1].get_signs (GET_CODE (x));
	  for (int i = n_bytes - off / 8; i < n_bytes; ++i)
	    ai[i] = b_signs;
	  if (off == 8 * n_bytes - 1)
	    if (code == ASHIFTRT)
	      ai[0] = b_signs;
	}
	break;
      }

    if (off % 8 != 0
	|| ai0.popcount () == 0)
      return ai;

    // For shift offsets that are a multiple of 8, record the
    // action on the constituent bytes.

    // Bytes are moving left by this offset (or zero for "none").
    const int boffL = select<int>()
      : code == ROTATE || code == ASHIFT ? off / 8
      : code == ROTATERT ? n_bytes - off / 8
      : 0;

    // Bytes are moving right by this offset (or zero for "none").
    const int boffR = select<int>()
      : code == ROTATERT || code == ASHIFTRT || code == LSHIFTRT ? off / 8
      : code == ROTATE ? n_bytes - off / 8
      : 0;

    if (dump_flags & TDF_FOLDING)
      {
	avr_dump (";; AI.explore_shift %C:%m ", code, mode);
	if (boffL)
	  avr_dump ("<< %d%s", 8 * boffL, boffL && boffR ? ", " : "");
	if (boffR)
	  avr_dump (">> %d", 8 * boffR);
	avr_dump ("\n");
      }

    if (boffL)
      for (int i = 0; i < n_bytes - boffL; ++i)
	ai[i + boffL] = ai0[i];

    if (boffR)
      for (int i = boffR; i < n_bytes; ++i)
	ai[i - boffR] = ai0[i];

    return ai;
  }

  void dump (const char *msg = nullptr, FILE *f = dump_file) const
  {
    if (f)
      dump (NULL_RTX, msg, f);
  }

  void dump (rtx dest, const char *msg = nullptr, FILE *f = dump_file) const
  {
    if (f)
      {
	int regno = dest && REG_P (dest) ? REGNO (dest) : 0;

	msg = msg && msg[0] ? msg : "AI=[%s]\n";
	const char *const xs = strstr (msg, "%s");
	gcc_assert (xs);

	fprintf (f, "%.*s", (int) (xs - msg), msg);
	for (int i = max_knows (); i >= 0; --i)
	  {
	    const int sub_regno = eq[i].regno (false /*nonstrict*/);
	    const bool nop = regno &&  sub_regno == regno + i;
	    eq[i].dump (nop ? "%s=nop" : "%s", f);
	    fprintf (f, "%s", i ? "; " : "");
	  }
	fprintf (f, "%s", xs + strlen ("%s"));
      }
  }
}; // absint_t


// Information for a REG = non-memory single_set.

struct insninfo_t
{
  // This is an insn that sets the m_size bytes of m_regno to either
  // - A compile time constant m_isrc (m_code = CONST_INT), or
  // - The contents of register number m_rsrc (m_code = REG).
  int m_size;
  int m_regno;
  int m_rsrc;
  rtx_code m_code;
  uint64_t m_isrc;
  rtx_insn *m_insn;
  rtx m_set = NULL_RTX;
  rtx m_src = NULL_RTX;
  int m_scratch = 0; // 0 or the register number of a QImode scratch.
  rtx_code m_old_code = UNKNOWN;

  // Knowledge about the bytes of the SET_SRC:  A byte may have a known
  // value, may be known to equal some register (e.g. with BSWAP),
  // or both, or may be unknown.
  absint_t m_ai;

  // May be set for binary operations.
  absint_byte_t m_new_src;

  bool init1 (insn_optimize_data_t &, int max_size, const char *purpose);

  // Upper bound for the cost (in words) of a move<mode> insn that
  // performs a REG = CONST_XXX = .m_isrc move of modesize .m_size.
  int cost () const;
  bool combine (const insninfo_t &prev, const insninfo_t &curr);
  int emit_insn () const;

  bool needs_scratch () const
  {
    gcc_assert (m_code == CONST_INT);

    for (int i = 0; i < m_size; ++i)
      if (AVRasm::ldi_needs_scratch (m_regno, m_isrc >> (8 * i)))
	return true;

    return false;
  }

  int hamming (const memento_t &memo) const
  {
    gcc_assert (m_code == CONST_INT);

    int h = 0;
    for (int i = 0; i < m_size; ++i)
      h += ! memo.have_value (m_regno + i, 1, 0xff & (m_isrc >> (8 * i)));

    return h;
  }

  // Upper bound for the number of ply_t's of a solution, given Hamming
  // distance of HAMM (-1 for unknown).
  int n_best_plys (int hamm = -1) const
  {
    gcc_assert (m_code == CONST_INT);

    if (m_size == 8)
      return (hamm >= 0 ? hamm : m_size);
    else if (hamm <= 4)
      return (hamm >= 0 ? hamm : m_size)
	// The following terms is the max number of MOVWs with a
	// Hamming difference of less than 2.
	+ (AVR_HAVE_MOVW && m_regno < REG_14) * m_size / 2
	+ (AVR_HAVE_MOVW && m_regno == REG_14) * std::max (0, m_size - 2)
	- (AVR_HAVE_MOVW && hamm == 4 && (uint32_t) m_isrc % 0x10001 == 0);
    else
      gcc_unreachable ();
  }
}; // insninfo_t


struct insn_optimize_data_t
{
  // Known values held in GPRs prior to the action of .insn / .ii,
  memento_t &regs;
  rtx_insn *insn;
  insninfo_t ii;
  bool unused;

  insn_optimize_data_t () = delete;

  insn_optimize_data_t (memento_t &memo)
    : regs(memo)
  {}
}; // insn_optimize_data_t

struct optimize_data_t
{
  insn_optimize_data_t prev;
  insn_optimize_data_t curr;

  // Number >= 0 of new insns that replace the curr insn and maybe also the
  // prev insn.  -1 when no replacement has been found.
  int n_new_insns = -1;

  // .prev will be removed provided we have (potentially zero) new insns.
  bool delete_prev_p = false;

  // Ignore these GPRs when comparing the simulation results of
  // old and new insn sequences.  Usually some scratch reg(s).
  gprmask_t ignore_mask = 0;

  optimize_data_t () = delete;

  optimize_data_t (memento_t &prev_regs, memento_t &curr_regs)
    : prev(prev_regs), curr(curr_regs)
  {}

  bool try_fuse (bbinfo_t *);
  bool try_bin_arg1 (bbinfo_t *);
  bool try_simplify (bbinfo_t *);
  bool try_split_ldi (bbinfo_t *);
  bool try_split_any (bbinfo_t *);
  bool fail (const char *reason);
  bool emit_signs (int r_sign, gprmask_t);
  void emit_move_mask (int dest, int src, int n_bytes, gprmask_t &);
  rtx_insn *emit_sequence (basic_block, rtx_insn *);
  bool get_2ary_operands (rtx_code &, const absint_byte_t &,
			  insn_optimize_data_t &, int r_dest,
			  absint_val_t &, absint_val_t &, int &ex_cost);
  rtx_insn *emit_and_apply_move (memento_t &, rtx dest, rtx src);

  // M2 is the state of GPRs as the sequence starts; M1 is the state one before.
  static void apply_sequence (const std::vector<rtx_insn *> &insns,
			      memento_t &m1, memento_t &m2)
  {
    gcc_assert (insns.size () >= 1);

    for (auto &i : insns)
      {
	m1 = m2;
	m2.apply_insn (i, false);
      }
  }
}; // optimize_data_t


// Emit INSNS before .curr.insn, replacing .curr.insn and also .prev.insn when
// .delete_prev_p is on.  Adjusts .curr.regs and .prev.regs accordingly.
rtx_insn *
optimize_data_t::emit_sequence (basic_block bb, rtx_insn *insns)
{
  gcc_assert (n_new_insns >= 0);

  // The old insns will be replaced by and simulated...
  const std::vector<rtx_insn *> old_insns = delete_prev_p
    ? std::vector<rtx_insn *> { prev.insn, curr.insn }
    : std::vector<rtx_insn *> { curr.insn };

  // ...against the new insns.
  std::vector<rtx_insn *> new_insns;
  for (rtx_insn *i = insns; i; i = NEXT_INSN (i))
    new_insns.push_back (i);

  rtx_insn *new_curr_insn;

  memento_t &m1 = prev.regs;
  memento_t &m2 = curr.regs;

  if (new_insns.empty ())
    {
      if (delete_prev_p)
	{
	  m2 = m1;
	  m1.known = 0;
	  new_curr_insn = prev_nondebug_insn_bb (bb, prev.insn);
	}
      else
	new_curr_insn = prev.insn;
    }
  else
    {
      // We are going to emit at least one new insn.  Simulate the effect of
      // the new sequence and compare it against the effect of the old one.
      // Both effects must be the same (modulo scratch regs).

      memento_t n1 = m1;
      memento_t n2 = m2;

      if (delete_prev_p)
	{
	  m2 = m1, m1.known = 0;
	  n2 = n1, n1.known = 0;
	}

      avr_dump (";; Applying new route...\n");
      optimize_data_t::apply_sequence (new_insns, n1, n2);

      avr_dump (";; Applying old route...\n");
      optimize_data_t::apply_sequence (old_insns, m1, m2);
      avr_dump ("\n");

      if (! m2.equals (n2, ignore_mask))
	{
	  // When we come here, then
	  // - We have a genuine bug, and/or
	  // - We did produce insns that are opaque to absint_t's explore().
	  avr_dump ("INCOMPLETE APPLICATION:\n");
	  m2.dump ("regs old route=%s\n\n");
	  n2.dump ("regs new route=%s\n\n");
	  avr_dump ("The new insns are:\n%L", insns);

	  fatal_insn ("incomplete application of insn", insns);
	}

      // Use N1 and N2 as the new GPR states.  Even though they are equal
      // modulo ignore_mask, N2 may know more about GPRs when it doesn't
      // clobber the scratch reg.
      m1 = n1;
      m2 = n2;

      emit_insn_before (insns, curr.insn);

      new_curr_insn = new_insns.back ();
    }

  if (delete_prev_p)
    SET_INSN_DELETED (prev.insn);

  SET_INSN_DELETED (curr.insn);

  return new_curr_insn;
}


const pass_data avr_pass_data_fuse_move =
{
  RTL_PASS,	 // type
  "",		 // name (will be patched)
  OPTGROUP_NONE, // optinfo_flags
  TV_MACH_DEP,	 // tv_id
  0,		 // properties_required
  0,		 // properties_provided
  0,		 // properties_destroyed
  0,		 // todo_flags_start
  TODO_df_finish | TODO_df_verify // todo_flags_finish
};


class avr_pass_fuse_move : public rtl_opt_pass
{
public:
  avr_pass_fuse_move (gcc::context *ctxt, const char *name)
    : rtl_opt_pass (avr_pass_data_fuse_move, ctxt)
  {
    this->name = name;
  }

  unsigned int execute (function *func) final override
  {
    if (optimize > 0 && avr_fuse_move > 0)
      {
	df_note_add_problem ();
	df_analyze ();

	bbinfo_t::optimize_one_function (func);
      }

    return 0;
  }
}; // avr_pass_fuse_move


// Append PLY to .plies[].  A SET or BLD ply may start a new sequence of
// SETs or BLDs and gets assigned the overhead of the sequence like for an
// initial SET or CLT instruction.  A SET ply my be added in two flavours:
// One that starts a sequence of single_sets, and one that represents the
// payload of a set_some insn.  MEMO is the GPR state prior to PLY.
void
plies_t::add (ply_t ply, const ply_t *prev, const memento_t &memo,
	      bool maybe_set_some)
{
  if (ply.code == SET)
    {
      if (prev && prev->code == SET)
	{
	  // Proceed with the SET sequence flavour.
	  ply.in_set_some = prev->in_set_some;

	  if (ply.in_set_some)
	    ply.scratch = 0;
	  else if (! ply.scratch && ply.needs_scratch ())
	    ply.cost += 2;
	}
      else
	{
	  // The 1st SET in a sequence.  May use set_some to set
	  // all bytes in one insn, or a bunch of single_sets.

	  // Route1: Bunch of single_sets.
	  const int ply_cost = ply.cost;
	  if (! ply.scratch && ply.needs_scratch ())
	    ply.cost += 2;
	  ply.in_set_some = false;

	  add (ply);

	  if (maybe_set_some)
	    {
	      // Route 2: One set_some: The 1st SET gets all the overhead.
	      ply.scratch = 0;
	      ply.cost = ply_cost + 1 + ! memo.known_dregno ();
	      ply.in_set_some = true;
	    }
	}
    } // SET
  else if (ply.is_bld ())
    {
      // The first BLD in a series of BLDs gets the extra costs
      // for the SET / CLT that precedes the BLDs.
      ply.cost += ! ply.is_same_bld (prev);
    }

  add (ply);
}


// Emit insns for .plies[] and return the number of emitted insns.
// The emitted insns represent the effect of II with MEMO, which
// is the GPR knowledge before II is executed.
int
plies_t::emit_insns (const insninfo_t &ii, const memento_t &memo) const
{
  int n_insns = 0;

  for (int i = 0; i < n_plies; ++i)
    {
      const ply_t &p = plies[i];

      // SETs and BLDs are dumped by their emit_xxxs().
      if (p.code != SET && ! p.is_bld ())
	p.dump ();

      rtx src1 = NULL_RTX;
      rtx src2 = NULL_RTX;
      rtx dest = NULL_RTX;
      rtx xscratch = NULL_RTX;
      rtx_code code = p.code;

      switch (p.code)
	{
	default:
	  avr_dump ("\n\n;; Bad ply_t:\n");
	  p.dump (i + 1);
	  gcc_unreachable ();
	  break;

	case REG: // *movhi = MOVW; movqi_insn = MOV
	  dest = gen_rtx_REG (p.size == 1 ? QImode : HImode, p.regno);
	  src1 = gen_rtx_REG (p.size == 1 ? QImode : HImode, p.arg);
	  break;

	case SET: // movqi_insn = LDI, CLR; set_some = (LDI + MOV) ** size.
	  i += emit_sets (ii, n_insns, memo, i) - 1;
	  continue;

	case MOD: // *ior<mode>3, *and<mode>3 = SET + BLD... / CLT + BLD...
	  i += emit_blds (ii, n_insns, i) - 1;
	  continue;

	case MINUS: // *subqi3 = SUB
	case PLUS:  // *addqi3 = ADD
	case AND: // *andqi3 = AND
	case IOR: // *iorqi3 = OR
	case XOR: // *xorqi3 = EOR
	  dest = gen_rtx_REG (QImode, p.regno);
	  src2 = gen_rtx_REG (QImode, p.arg);
	  break;

	case PRE_INC: // *addqi3 = INC
	case PRE_DEC: // *addqi3 = DEC
	  code = PLUS;
	  dest = gen_rtx_REG (QImode, p.regno);
	  src2 = p.code == PRE_INC ? const1_rtx : constm1_rtx;
	  break;

	case NEG: // *negqi2 = NEG
	case NOT: // *one_cmplqi2 = COM
	  dest = gen_rtx_REG (QImode, p.regno);
	  src1 = dest;
	  break;

	case ROTATE:   // *rotlqi3 = SWAP
	case ASHIFT:   // *ashlqi3 = LSL
	case ASHIFTRT: // *ashrqi3 = ASR
	case LSHIFTRT: // *lshrqi3 = LSR
	  dest = gen_rtx_REG (QImode, p.regno);
	  src2 = GEN_INT (code == ROTATE ? 4 : 1);
	  break;

	case SS_PLUS: // *addhi3 = ADIW, SBIW
	  code = PLUS;
	  dest = gen_rtx_REG (HImode, p.regno);
	  src2 = gen_int_mode (p.arg, HImode);
	  break;
	} // switch p.code

      gcc_assert (dest && (! src1) + (! src2) == 1);

      rtx src = code == REG || code == SET
	? src1
	: (src2
	   ? gen_rtx_fmt_ee (code, GET_MODE (dest), dest, src2)
	   : gen_rtx_fmt_e (code, GET_MODE (dest), src1));

      emit_valid_move_clobbercc (dest, src, xscratch);
      n_insns += 1;
    }

  return n_insns;
}


// Helper for .emit_insns().  Emit an ior<mode>3 or and<mode>3 insns
// that's equivalent to a sequence of contiguous BLDs starting at
// .plies[ISTART].  Updates N_INSNS according to the number of insns emitted
// and returns the number of consumed plys in .plies[].
int
plies_t::emit_blds (const insninfo_t &ii, int &n_insns, int istart) const
{
  const ply_t &first = plies[istart];

  gcc_assert (ii.m_size <= 4);
  gcc_assert (first.is_bld ());

  const rtx_code code = first.is_setbld () ? IOR : AND;
  const machine_mode mode = size_to_mode (ii.m_size);

  // Determine mask and number of BLDs.

  uint32_t mask = 0;
  int n_blds = 0;

  for (int i = istart; i < n_plies; ++i, ++n_blds)
    {
      const ply_t &p = plies[i];
      if (! p.is_bld () || ! p.is_same_bld (& first))
	break;

      // For AND, work on the 1-complement of the mask,
      // i.e. 1's specify which bits to clear.
      uint8_t mask8 = code == IOR ? p.arg : ~p.arg;
      mask |= mask8 << (8 * (p.regno - ii.m_regno));
    }

  mask = GET_MODE_MASK (mode) & (code == IOR ? mask : ~mask);

  if (dump_file)
    {
      fprintf (dump_file, ";; emit_blds[%d...%d] R%d[%d]%s=%0*x\n",
	       istart, istart + n_blds - 1, ii.m_regno, ii.m_size,
	       code == IOR ? "|" : "&", 2 * ii.m_size, (int) mask);
    }

  for (int i = 0; i < n_blds; ++i)
    plies[i + istart].dump ();

  rtx dest = gen_rtx_REG (mode, ii.m_regno);
  rtx src = gen_rtx_fmt_ee (code, mode, dest, gen_int_mode (mask, mode));
  rtx xscratch = mode == QImode ? NULL_RTX : gen_rtx_SCRATCH (QImode);

  emit_valid_move_clobbercc (dest, src, xscratch);
  n_insns += 1;

  return n_blds;
}


// Emit insns for a contiguous sequence of SET ply_t's starting at
// .plies[ISTART].  Advances N_INSNS by the number of emitted insns.
// MEMO ist the state of the GPRs before II es executed, where II
// represents the insn under optimization.
// The emitted insns are "movqi_insn" or "*reload_inqi"
// when .plies[ISTART].in_set_some is not set, and one "set_some" insn
// when .plies[ISTART].in_set_some is set.
int
plies_t::emit_sets (const insninfo_t &ii, int &n_insns, const memento_t &memo,
		    int istart) const
{
  gcc_assert (plies[istart].code == SET);

  const bool in_set_some = plies[istart].in_set_some;

  // Some d-regno that holds a compile-time constant, or 0.
  const int known_dregno = memo.known_dregno ();

  // Determine number of contiguous SETs,
  // and sort them in ps[] such that smaller regnos come first.

  const ply_t *ps[FUSE_MOVE_MAX_MODESIZE];
  int n_sets = 0;

  for (int i = istart; i < n_plies && plies[i].code == SET; ++i)
    ps[n_sets++] = & plies[i];

  if (dump_file)
    {
      fprintf (dump_file, ";; emit_sets[%d...%d] R%d[%d]=%0*" PRIx64,
	       istart, istart + n_sets - 1, ii.m_regno, ii.m_size,
	       2 * ii.m_size, ii.m_isrc);
      fprintf (dump_file, ", scratch=%s%d", "R" + ! ii.m_scratch, ii.m_scratch);
      fprintf (dump_file, ", known_dreg=%s%d, set_some=%d\n",
	       "R" + ! known_dregno, known_dregno, in_set_some);
    }

  for (int i = 0; i < n_sets; ++i)
    ps[i]->dump ();

  // Sort.  This is most useful on regs like (reg:SI REG_14).
  for (int i = 0; i < n_sets - 1; ++i)
    for (int j = i + 1; j < n_sets; ++j)
      if (ps[i]->regno > ps[j]->regno)
	std::swap (ps[i], ps[j]);

  // Prepare operands.
  rtx dst[FUSE_MOVE_MAX_MODESIZE];
  rtx src[FUSE_MOVE_MAX_MODESIZE];
  for (int i = 0; i < n_sets; ++i)
    {
      dst[i] = gen_rtx_REG (QImode, ps[i]->regno);
      src[i] = gen_int_mode (ps[i]->arg, QImode);
    }

  if (in_set_some)
    {
      // Emit a "set_some" insn that sets all of the collected 8-bit SETs.
      // This is a parallel with n_sets QImode SETs as payload.

      gcc_assert (! known_dregno || memo.knows (known_dregno));

      // A scratch reg...
      rtx op1 = known_dregno
	? gen_rtx_REG (QImode, known_dregno)
	: const0_rtx;
      // ...with a known content, so it can be restored without saving.
      rtx op2 = known_dregno
	? gen_int_mode (memo.values[known_dregno], QImode)
	: const0_rtx;
      // Target register envelope.
      rtx op3 = GEN_INT (ii.m_regno);
      rtx op4 = GEN_INT (ii.m_size);

      // Payload.
      for (int i = 0; i < n_sets; ++i)
	dst[i] = gen_rtx_SET (dst[i], src[i]);

      rtvec vec = gen_rtvec (5 + n_sets,
			     gen_rtx_USE (VOIDmode, op1),
			     gen_rtx_USE (VOIDmode, op2),
			     gen_rtx_USE (VOIDmode, op3),
			     gen_rtx_USE (VOIDmode, op4),
			     gen_rtx_CLOBBER (VOIDmode, cc_reg_rtx),
			     dst[0], dst[1], dst[2], dst[3]);
      rtx pattern = gen_rtx_PARALLEL (VOIDmode, vec);

      emit_valid_insn (pattern);
      n_insns += 1;
    }
  else
    {
      // Emit a bunch of movqi_insn / *reload_inqi insns.

      for (int i = 0; i < n_sets; ++i)
	if (ii.m_scratch
	    && AVRasm::constant_cost (SET, ps[i]->regno, ps[i]->arg) > 1)
	  {
	    rtx scratch = gen_rtx_REG (QImode, ii.m_scratch);
	    bool use_reload_inqi = true;
	    if (use_reload_inqi)
	      {
		emit_valid_move_clobbercc (dst[i], src[i], scratch);
		n_insns += 1;
	      }
	    else
	      {
		emit_valid_move_clobbercc (scratch, src[i]);
		emit_valid_move_clobbercc (dst[i], scratch);
		n_insns += 2;
	      }
	  }
	else
	  {
	    emit_valid_move_clobbercc (dst[i], src[i]);
	    n_insns += 1;
	  }
    }

  return n_sets;
}


// Try to find an operation such that  Y = op (X).
// Shifts and rotates are regarded as unary operaions with
// an implied 2nd operand.
static rtx_code
find_arith (uint8_t y, uint8_t x)
{
#define RETIF(ex, code) y == (0xff & (ex)) ? code
  return select<rtx_code>()
    : RETIF (x + 1, PRE_INC)
    : RETIF (x - 1, PRE_DEC)
    : RETIF ((x << 4) | (x >> 4), ROTATE)
    : RETIF (-x, NEG)
    : RETIF (~x, NOT)
    : RETIF (x >> 1, LSHIFTRT)
    : RETIF (x << 1, ASHIFT)
    : RETIF ((x >> 1) | (x & 0x80), ASHIFTRT)
    : UNKNOWN;
#undef RETIF
}


// Try to find an operation such that  Z = X op X.
static rtx_code
find_arith2 (uint8_t z, uint8_t x, uint8_t y)
{
#define RETIF(ex, code) z == (0xff & (ex)) ? code
  return select<rtx_code>()
    : RETIF (x + y, PLUS)
    : RETIF (x - y, MINUS)
    : RETIF (x & y, AND)
    : RETIF (x | y, IOR)
    : RETIF (x ^ y, XOR)
    : UNKNOWN;
#undef RETIF
}


// Add plies to .plies[] that represent a MOVW, but only ones that reduce the
// Hamming distance from REGNO[SIZE] to VAL by exactly DHAMM.
void
plies_t::add_plies_movw (int regno, int size, uint64_t val,
			 int dhamm, const memento_t &memo)
{
  if (! AVR_HAVE_MOVW || size < 2)
    return;

  for (int i = 0; i < size - 1; i += 2)
    {
      // MOVW that sets less than 2 regs to the target value is
      // not needed for the upper regs.
      if (dhamm != 2 && regno + i >= REG_16)
	continue;

      const uint16_t val16 = val >> (8 * i);
      const uint8_t lo8 = val16;
      const uint8_t hi8 = val16 >> 8;

      // When one of the target bytes is already as expected, then
      // no MOVW is needed for an optimal sequence.
      if (memo.have_value (regno + i, 1, lo8)
	  || memo.have_value (regno + i + 1, 1, hi8))
	continue;

      const int h_old = memo.hamming (regno + i, 2, val16);

      // Record MOVWs that reduce the Hamming distance by DHAMM as requested.
      for (int j = FIRST_GPR; j < REG_32; j += 2)
	if (j != regno + i
	    && memo.knows (j, 2))
	  {
	    const int h_new = memo.hamming (j, 2, val16);
	    if (h_new == h_old - dhamm)
	      add (ply_t { regno + i, 2, REG, j, 1, dhamm });
	  }
    }
}


// Set PS to plys that reduce the Hamming distance from II.m_regno to
// compile-time constant II.m_isrc by 2, 1 or 0.  PREV is NULL or points
// to a previous ply_t.  MEMO is the GPR state after PREV and prior to the
// added plys.
void
bbinfo_t::get_plies (plies_t &ps, const insninfo_t &ii, const memento_t &memo,
		     const ply_t *prev)
{
  ps.reset ();

  fpd->n_get_plies += 1;

  const bool maybe_set_some = (bbinfo_t::use_set_some_p && ii.needs_scratch ());

  // Start with cheap plies, then continue to more expensive ones.
  const int regno = ii.m_regno;
  const int size = ii.m_size;
  const uint64_t val = ii.m_isrc;

  // Find MOVW with a Hamming delta of 2.
  ps.add_plies_movw (regno, size, val, 2, memo);

  // Find ADIW / SBIW
  if (AVR_HAVE_ADIW && size >= 2)
    for (int i = 0; i < size - 1; i += 2)
      if (regno + i >= REG_24
	  && memo.knows (regno + i, 2))
	{
	  const int16_t value16 = memo[regno + i] + 256 * memo[regno + i + 1];
	  const int16_t lo16 = val >> (8 * i);
	  const int16_t delta = lo16 - value16;
	  const uint8_t lo8 = val >> (8 * i);
	  const uint8_t hi8 = val >> (8 * i + 8);
	  if (IN_RANGE (delta, -63, 63)
	      && lo8 != memo[regno + i]
	      && hi8 != memo[regno + i + 1])
	    {
	      ps.add (ply_t { regno + i, 2, SS_PLUS, delta, 1, 2 });
	    }
	}

  // Find 1-reg plies.  In an optimal sequence, each 1-reg ply will decrease
  // the Hamming distance.  Thus we only have to consider plies that set
  // one of the target bytes to the target value VAL.  Start with the
  // high registers since that is the canonical order when two plies commute.

  for (int i = size - 1; i >= 0; --i)
    {
      const uint8_t val8 = val >> (8 * i);

      // Nothing to do for this byte when its value is already as desired.
      if (memo.have_value (regno + i, 1, val8))
	continue;

      // LDI or CLR.
      if (regno + i >= REG_16 || val8 == 0)
	ps.add (ply_t { regno + i, 1, SET, val8, 1 }, prev, memo,
		maybe_set_some);

      // We only may need to MOV non-zero values since there is CLR,
      // and only when there is no LDI.
      if (val8 != 0
	  && regno + i < REG_16)
	{
	  // MOV where the source register is one of the target regs.
	  for (int j = 0; j < size; ++j)
	    if (j != i)
	      if (memo.have_value (regno + j, 1, val8))
		ps.add (ply_t { regno + i, 1, REG, regno + j, 1 });

	  // MOV where the source register is not a target reg.
	  // FIXME: ticks.
	  for (int j = FIRST_GPR; j < REG_32; ++j)
	    if (! IN_RANGE (j, regno, regno + size - 1))
	      if (memo.have_value (j, 1, val8))
		ps.add (ply_t { regno + i, 1, REG, j, 1 });

	  // LDI + MOV.
	  if (regno + i < REG_16 && val8 != 0)
	    {
	      ply_t p { regno + i, 1, SET, val8, 2 };
	      p.scratch = ii.m_scratch;
	      ps.add (p, prev, memo, maybe_set_some);
	    }
	}
    }

  // Arithmetic like INC, DEC or ASHIFT.
  for (int i = size - 1; i >= 0; --i)
    if (bbinfo_t::use_arith_p
	&& regno + i < REG_16
	&& memo.knows (regno + i))
      {
	const uint8_t y = val >> (8 * i);
	const uint8_t x = memo[regno + i];
	rtx_code code;

	if (y == 0 || y == x)
	  continue;

	// INC, DEC, SWAP, LSL, NEG, ...
	if (UNKNOWN != (code = find_arith (y, x)))
	  {
	    ps.add (ply_t { regno + i, 1, code, x /* dummy */, 1 });
	    continue;
	  }

	// ADD, AND, ...
	for (int r = FIRST_GPR; r < REG_32; ++r)
	  if (r != regno + i
	      && memo.knows (r)
	      && memo[r] != 0
	      && UNKNOWN != (code = find_arith2 (y, x, memo[r])))
	    {
	      ps.add (ply_t { regno + i, 1, code, r, 1 });
	    }

	if (size < 2 || size > 4)
	  continue;

	// SET + BLD
	if ((x & y) == x && popcount_hwi (x ^ y) == 1)
	  ps.add (ply_t { regno + i, 1, MOD, x ^ y, 1 },
		  prev, memo, maybe_set_some);

	// CLT + BLD
	if ((x & y) == y && popcount_hwi (x ^ y) == 1)
	  ps.add (ply_t { regno + i, 1, MOD, x ^ y ^ 0xff, 1 },
		  prev, memo, maybe_set_some);
      }

  if (bbinfo_t::use_arith_p
      // For 8-byte values, don't use ply_t's with only a partial reduction
      // of the hamming distance.
      && size <= 4)
    {
      // Find MOVW with a Hamming delta of 1, then 0.
      ps.add_plies_movw (regno, size, val, 1, memo);
      ps.add_plies_movw (regno, size, val, 0, memo);
    }

  plies_t::max_n_plies = std::max (plies_t::max_n_plies, ps.n_plies);
}


// Try to combine two 8-bit insns PREV and CURR that (effectively)
// are REG = CONST_INT to one 16-bit such insn.  Returns true on success.
bool
insninfo_t::combine (const insninfo_t &prev, const insninfo_t &curr)
{
  if (prev.m_size == 1 && curr.m_size == 1
      && prev.m_regno == (1 ^ curr.m_regno)
      && curr.m_code == CONST_INT
      && prev.m_code == CONST_INT)
    {
      m_regno = curr.m_regno & ~1;
      m_code = CONST_INT;
      m_size = 2;
      m_scratch = std::max (curr.m_scratch, prev.m_scratch);
      m_isrc = m_regno == prev.m_regno
	? (uint8_t) prev.m_isrc + 256 * (uint8_t) curr.m_isrc
	: (uint8_t) curr.m_isrc + 256 * (uint8_t) prev.m_isrc;

      return true;
    }

  return false;
}


// Return the cost (in terms of words) of the respective mov<mode> insn.
// This can be used as an upper bound for the ply_t's cost.
int
insninfo_t::cost () const
{
  if (m_code != CONST_INT)
    return m_size;

  if (m_regno >= REG_16 || m_isrc == 0)
    return m_size
      // MOVW can save one instruction.
      - (AVR_HAVE_MOVW && m_size == 4 && (uint32_t) m_isrc % 0x10001 == 0);

  // LDI + MOV to a lower reg.
  if (m_scratch && m_size == 1)
    return 2;

  if (m_size == 8)
    {
      int len = m_size;
      for (int i = 0; i < m_size; ++i)
	len += m_regno + i < REG_16 && (0xff & (m_isrc >> (8 * i))) != 0;
      return len;
    }

  // All other cases are complicated.  Ask the output oracle.
  const machine_mode mode = size_to_mode (m_size);
  rtx xscratch = m_scratch ? all_regs_rtx[m_scratch] : NULL_RTX;
  rtx xop[] = { gen_rtx_REG (mode, m_regno), gen_int_mode (m_isrc, mode) };
  int len;
  if (m_size == 4)
    output_reload_insisf (xop, xscratch, &len);
  else
    output_reload_in_const (xop, xscratch, &len, false);

  return len;
}

// Emit the according REG = REG-or-CONST_INT insn.  Returns 1 or aborts
// when the insn is not of that form.
int
insninfo_t::emit_insn () const
{
  int n_insns = 0;

  machine_mode mode = size_to_mode (m_size);
  rtx xsrc = NULL_RTX;
  rtx xscratch = NULL_RTX;

  gcc_assert (m_size > 0);

  switch (m_code)
    {
    default:
      gcc_unreachable();

    case CONST_INT:
      xsrc = gen_int_mode (m_isrc, mode);
      if (m_scratch && m_regno < REG_16)
	xscratch = gen_rtx_REG (QImode, m_scratch);
      break;

    case REG:
      gcc_assert (gpr_regno_p (m_rsrc, m_size));
      if (m_regno != m_rsrc)
	xsrc = gen_rtx_REG (mode, m_rsrc);
      break;
    }

  if (xsrc)
    {
      rtx dest = gen_rtx_REG (mode, m_regno);
      emit_valid_move_clobbercc (dest, xsrc, xscratch);
      n_insns += 1;
    }

  return n_insns;
}


// Entering a basic block means combining known register values from
// all incoming BBs.
void
bbinfo_t::enter ()
{
  avr_dump ("\n;; Entering [bb %d]\n", bb->index);

  gcc_assert (! done);

  edge e;
  edge_iterator ei;
  gprmask_t pred_known_mask = ~0u;
  bbinfo_t *bbi = nullptr;

  // A quick iteration over all predecessors / incoming edges to reveal
  // whether this BB is worth a closer look.
  FOR_EACH_EDGE (e, ei, bb->preds)
    {
      basic_block pred = e->src;
      bbi = & bb_info[pred->index];

      pred_known_mask &= bbi->regs.known;

      if (dump_file)
	{
	  avr_dump (";; [bb %d] <- [bb %d] ", e->dest->index, e->src->index);
	  if (bbi->done)
	    bbi->regs.dump ();
	  else
	    avr_dump (" (unknown)\n");
	}
    }

  // Only if all predecessors have already been handled, we can
  // have known values as we are entering the current BB.
  if (pred_known_mask != 0
      && bbi != nullptr)
    {
      // Initialize current BB info from BI, an arbitrary predecessor.

      regs = bbi->regs;

      // Coalesce the output values from all predecessing BBs.  At the
      // start of the current BB, a value is only known if it is known
      // in *all* predecessors and *all* these values are the same.
      FOR_EACH_EDGE (e, ei, bb->preds)
	{
	  regs.coalesce (bb_info[e->src->index].regs);
	}
    }

  if (dump_file)
    {
      avr_dump (";; [bb %d] known at start: ", bb->index);
      if (regs.known)
	regs.dump ();
      else
	avr_dump (" (none)\n");
      avr_dump ("\n");
    }
}


void
bbinfo_t::leave ()
{
  done = true;

  if (dump_file)
    fprintf (dump_file, ";; Leaving [bb %d]\n\n", bb->index);
}


/* Initialize according to INSN which is a 1-byte single_set that's
   (effectively) a reg = reg or reg = const move.  INSN may be the result
   of the current pass's optimization, e.g. something like INC R2 where R2
   has a known content.  MEMO is the state prior to INSN.  Only CONST
   cases are recorded; plus cases that are non-trivial for example when
   an XOR decays to a move.  */

bool
insninfo_t::init1 (insn_optimize_data_t &iod, int max_size,
		   const char *purpose = "")
{
  m_size = 0;
  m_insn = iod.insn;
  m_old_code = UNKNOWN;
  iod.unused = false;

  if (! iod.insn
      || ! (m_set = single_set_with_scratch (iod.insn, m_scratch)))
    return false;

  rtx dest = SET_DEST (m_set);
  machine_mode mode = GET_MODE (dest);
  const int n_bytes = GET_MODE_SIZE (mode);
  max_size = std::min (max_size, FUSE_MOVE_MAX_MODESIZE);

  if (! REG_P (dest)
      || END_REGNO (dest) > REG_32
      || n_bytes > max_size)
    return false;

  // Omit insns that (explicitly) touch fixed GPRs in any way.
  using elt0_getter_HRS = elt0_getter<HARD_REG_SET, HARD_REG_ELT_TYPE>;
  HARD_REG_SET hregs;
  CLEAR_HARD_REG_SET (hregs);
  find_all_hard_regs (PATTERN (iod.insn), & hregs);
  if (memento_t::fixed_regs_mask & (gprmask_t) elt0_getter_HRS::get (hregs))
    {
      avr_dump (";; %sinit1 has fixed GPRs\n", purpose);
      return false;
    }

  if ((iod.unused = find_reg_note (iod.insn, REG_UNUSED, dest)))
    return false;

  m_src = SET_SRC (m_set);
  m_regno = REGNO (dest);
  const rtx_code src_code = GET_CODE (m_src);

  m_ai = absint_t::explore (m_src, iod.regs, mode);

  if (m_ai.popcount ())
    {
      if (m_ai.end_knows (CONST_INT) >= n_bytes)
	{
	  m_code = CONST_INT;
	  m_old_code = CONSTANT_P (m_src) ? UNKNOWN : src_code;
	  m_isrc = m_ai.get_value (n_bytes);
	  m_size = n_bytes;
	}
      else if (! REG_P (m_src)
	       && n_bytes == 1
	       && m_ai.end_knows (REG) >= n_bytes)
	{
	  m_code = REG;
	  m_old_code = src_code;
	  m_rsrc = m_ai[0].regno ();
	  m_size = n_bytes;
	}
      else if (n_bytes == 1)
	{
	  absint_byte_t &aib = m_new_src;
	  aib = m_ai[0].find_alternative_binary (iod.regs);

	  if (aib.arity () == 2
	      && aib.arg (0).regno == m_regno)
	    {
	      m_old_code = src_code;
	      m_code = aib.get_code ();
	      m_size = n_bytes;
	    }
	}
      else if (n_bytes >= 2
	       && m_ai.end_knows (VALUE) >= n_bytes)
	{
	  m_code = src_code;
	  m_size = n_bytes;
	}

      if (dump_file && m_size != 0)
	{
	  avr_dump (";; %sinit1 (%C", purpose,
		    m_old_code ? m_old_code : m_code);
	  if (m_old_code)
	    avr_dump ("-> %C", m_code);
	  avr_dump (") insn %d to R%d[%d] := %C:%m = ", INSN_UID (iod.insn),
		    m_regno, n_bytes, src_code, mode);

	  m_ai.dump (dest);

	  if (dump_flags & TDF_FOLDING)
	    avr_dump ("\n");
	}
    }

  return m_size != 0;
}


// The private worker for .apply_insn().
void
memento_t::apply_insn1 (rtx_insn *insn, bool unused)
{
  gcc_assert (NONDEBUG_INSN_P (insn));

  if (INSN_CODE (insn) == CODE_FOR_set_some)
    {
      // This insn only sets some selected bytes of register $3 of
      // modesize $4.  If non-0, then $1 is a QImode scratch d-reg with
      // a known value of $2.

      const auto &xop = recog_data.operand;
      extract_insn (insn);
      gcc_assert (recog_data.n_operands == 7);
      gcc_assert (set_some_operation (xop[0], VOIDmode));

      const rtx &xscratch = xop[1];
      const rtx &xscratch_value = xop[2];
      const int sets_start = 5;

      for (int i = sets_start; i < XVECLEN (xop[0], 0); ++i)
	{
	  rtx xset = XVECEXP (xop[0], 0, i);
	  avr_dump (";; set_some %r = %r\n", XEXP (xset, 0), XEXP (xset, 1));
	  set_values (XEXP (xset, 0), XEXP (xset, 1));
	}

      if (REG_P (xscratch))
	{
	  avr_dump (";; set_some %r = %r restore\n", xscratch, xscratch_value);
	  set_values (xscratch, xscratch_value);
	}

      return;
    } // CODE_FOR_set_some

  memento_t mold = *this;

  // When insn changes a register in whatever way, set it to "unknown".

  HARD_REG_SET rset;
  find_all_hard_reg_sets (insn, &rset, true /* implicit */);
  (*this) &= ~rset;

  rtx set = single_set (insn);
  rtx dest;

  if (! set
      || ! REG_P (dest = SET_DEST (set))
      || END_REGNO (dest) > REG_32
      || (regmask (dest) & memento_t::fixed_regs_mask))
    return;

  rtx src = SET_SRC (set);
  const rtx_code src_code = GET_CODE (src);
  const machine_mode mode = GET_MODE (dest);
  const int n_bytes = GET_MODE_SIZE (mode);

  // Insns that are too complicated or have a poor yield.
  // Just record which regs are clobberd / changed.
  if (n_bytes > FUSE_MOVE_MAX_MODESIZE
      || MEM_P (src)
      || (REG_P (src) && END_REGNO (src) > REG_32))
    {
      // Comparisons may clobber the compared reg when it is unused after.
      if (src_code == COMPARE
	  && REG_P (XEXP (src, 0))
	  && CONSTANT_P (XEXP (src, 1)))
	{
	  rtx reg = XEXP (src, 0);
	  for (unsigned r = REGNO (reg); r < END_REGNO (reg); ++r)
	    set_unknown (r);
	}
      return;
    }

  if (unused)
    return;

  // Simulate the effect of some selected insns that are likely to produce
  // or propagate known values.

  // Get an abstract representation of src.  Bytes may be unknown,
  // known to equal some 8-bit compile-time constant (CTC) value,
  // or are known to equal some 8-bit register.
  // TODO: Currently, only the ai[].val8 knowledge ist used.
  //       What's the best way to make use of ai[].regno ?

  absint_t ai = absint_t::explore (src, mold, mode);

  if (ai.popcount ())
    {
      avr_dump (";; apply_insn %d R%d[%d] := %C:%m = ", INSN_UID (insn),
		REGNO (dest), n_bytes, src_code, mode);
      ai.dump ();

      for (int i = 0; i < n_bytes; ++i)
	if (ai[i].can (CONST_INT))
	  set_value (i + REGNO (dest), ai[i].val8 ());
    }
}


void
memento_t::apply (const ply_t &p)
{
  if (p.is_movw ())
    {
      copy_value (p.regno, p.arg);
      copy_value (p.regno + 1, p.arg + 1);
    }
  else if (p.is_adiw ())
    {
      int val = p.arg + values[p.regno] + 256 * values[1 + p.regno];
      set_value (p.regno, val);
      set_value (p.regno + 1, val >> 8);
    }
  else if (p.size == 1)
    {
      int x = values[p.regno];
      int y = values[p.arg];

      switch (p.code)
	{
	default:
	  gcc_unreachable ();
	  break;

	case REG:
	  copy_value (p.regno, p.arg);
	  break;

	case SET:
	  set_value (p.regno, p.arg);
	  if (p.scratch >= REG_16)
	    set_unknown (p.scratch);
	  break;

	case MOD: // BLD
	  gcc_assert (knows (p.regno));
	  if (popcount_hwi (p.arg) == 1)
	    values[p.regno] |= p.arg;
	  else if (popcount_hwi (p.arg) == 7)
	    values[p.regno] &= p.arg;
	  else
	    gcc_unreachable ();
	  break;

#define DO_ARITH(n_args, code, expr)					\
	  case code:							\
	    gcc_assert (knows (p.regno));				\
	    if (n_args == 2)						\
	      gcc_assert (knows (p.arg));				\
	    set_value (p.regno, expr);					\
	    break

	  DO_ARITH (1, NEG, -x);
	  DO_ARITH (1, NOT, ~x);
	  DO_ARITH (1, PRE_INC, x + 1);
	  DO_ARITH (1, PRE_DEC, x - 1);
	  DO_ARITH (1, ROTATE, (x << 4) | (x >> 4));
	  DO_ARITH (1, ASHIFT, x << 1);
	  DO_ARITH (1, LSHIFTRT, x >> 1);
	  DO_ARITH (1, ASHIFTRT, (x >> 1) | (x & 0x80));

	  DO_ARITH (2, AND, x & y);
	  DO_ARITH (2, IOR, x | y);
	  DO_ARITH (2, XOR, x ^ y);
	  DO_ARITH (2, PLUS, x + y);
	  DO_ARITH (2, MINUS, x - y);
#undef DO_ARITH
	}
    } // size == 1
  else
    gcc_unreachable ();
}


// Try to find a sequence of ply_t's that represent a II.m_regno = II.m_isrc
// insn that sets a reg to a compile-time constant, and that is more
// efficient than just a move insn.  (When try_split_any_p is on, then
// solutions that perform equal to a move insn are also allowed).
// MEMO0 is the GPR state before II runs.  A solution has been found
// when .fpd->solution has at least one entry.  LEN specifies the
// depth of recursion, which works on the LEN-th ply_t.
void
bbinfo_t::find_plies (int len, const insninfo_t &ii, const memento_t &memo0)
{
  if (len > fpd->n_best_plys)
    return;

  memento_t memo = memo0;
  bool ply_applied_p = false;

  //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  const bool extra = dump_file && (dump_flags & TDF_FOLDING);

  if (extra)
    {
      fprintf (dump_file, ";; #%d (HAM=%d): get_plies R%d[%d] = ", len,
	       ii.hamming (fpd->regs0), ii.m_regno, ii.m_size);
      fprintf (dump_file, "0x%0*" PRIx64 "\n",
	       2 * ii.m_size, ii.m_isrc & size_to_mask (ii.m_size));
    }

  plies_t &ps = fpd->plies[len - 1];

  const ply_t *const prev = len >= 2 ? fpd->ply_stack[len - 2] : nullptr;
  const ply_t *const prev2 = len >= 3 ? fpd->ply_stack[len - 3] : nullptr;

  bbinfo_t::get_plies (ps, ii, memo0, prev);

#define NEXT(reason)					\
  do {							\
    if (extra)						\
      fprintf (dump_file, ";; cont=%s\n", reason);	\
    goto next;						\
  } while (0)

  for (int ip = 0; ip < ps.n_plies; ++ip)
    {
      const ply_t &p = ps.plies[ip];

      fpd->ply_stack[len - 1] = &p;

      if (0)
	next: continue;

      if (extra)
	ply_t::dump_plys (dump_file, len, 1, fpd->ply_stack + len - 1, memo0);

      // A MOVW with a Hamming distance of < 2 requires more plys.
      if (p.is_movw () && len + (2 - p.dhamming) > fpd->n_best_plys)
	NEXT ("movw.plys");

      if (len >= 2)
	{
	  // Destroying (parts of) the results of the previous ply
	  // won't yield an optimal sequence.
	  if (p.overrides (prev))
	    NEXT ("overrides");

	  // When two plys are independent of each other, then only
	  // investigate sequences that operate on the higher reg first.
	  // This canonicalization reduces the number of candidates,
	  if (p.commutes_with (prev, ii.m_scratch)
	      && p.regno > prev->regno)
	    NEXT ("noncanonic");

	  // Two subsequent BLDs touching the same register.
	  if (p.is_bld ()
	      && prev->is_bld ()
	      && p.changes_result_of (prev))
	    NEXT ("2bld");

	  // When there is a BLD, then at least 2 of the same kind
	  // shall occur in a row.
	  if (prev->is_bld ()
	      && ! p.is_bld ()
	      && (len == 2
		  || (prev->is_setbld () && ! prev2->is_setbld ())
		  || (prev->is_cltbld () && ! prev2->is_cltbld ())))
	    NEXT ("1bld");
	}

      // The hamming delta of a MOVW may be less than 2, namely 0 or 1.
      // When the latter is the case, then a reasonable sequence must
      // modify the result of the MOVW.
      if (len >= 2
	  && prev->is_movw ()
	  && prev->dhamming == 1
	  && ! p.changes_result_of (prev))
	NEXT ("movw.dh=1");

      if (len >= 3
	  && prev2->is_movw ()
	  && prev2->dhamming == 0
	  && ! p.changes_result_of (prev2))
	NEXT ("movw.dh=0");

      // When setting an n-byte destination, then at most n/2 MOVWs
      // will occur in an optimal sequence.
      int n_movw = 0;
      for (int i = 0; i < len; ++i)
	n_movw += fpd->ply_stack[i]->is_movw ();
      if (n_movw > ii.m_size / 2)
	NEXT ("movws");

      if (ply_applied_p)
	memo = memo0;

      memo.apply (p);

      ply_applied_p = true;

      // Calculate the cost of the sequence we have so far.  Scale by some
      // factor so that we can express that ADIW is more expensive than MOVW
      // because it is slower, but without defeating MOVW.
      const int SCALE = 4;

      int penal = 0;
      int cost = SCALE * 0;

      bool movw_p = 0;
      for (int i = 0; i < len; ++i)
	{
	  bool adiw_p = fpd->ply_stack[i]->is_adiw ();
	  cost += SCALE * fpd->ply_stack[i]->cost + adiw_p;
	  penal += adiw_p;
	  movw_p |= fpd->ply_stack[i]->is_movw ();
	}
      penal += movw_p;

      const int hamm = ii.hamming (memo);

      // The current Hamming distance yields a lower bound of how many
      // plys are still required.  Consider that future cost already now.
      int future_cost = AVR_HAVE_MOVW || (AVR_HAVE_ADIW && ii.m_regno >= REG_22)
	? (1 + hamm) / 2
	: hamm;

      // Similarly, when MOVW doesn't decrease the Hamming distance by 2,
      // then we know that at least 2 - dhamming plys must follow in the
      // future.  (MOVW + ADIW will not occur.)
      if (p.is_movw ())
	future_cost = std::max (future_cost, 2 - p.dhamming);

      if (extra && future_cost)
	avr_dump (";; future cost = %d, dh=%d\n", future_cost, hamm);

      cost += SCALE * future_cost;

      bool profitable = (cost < SCALE * fpd->max_ply_cost
			 || (bbinfo_t::try_split_any_p
			     && cost / SCALE <= fpd->max_ply_cost
			     && cost / SCALE == fpd->movmode_cost));
      if (! profitable)
	{
	  if (extra)
	    avr_dump (";; cont=cost %d+%d/%d\n", cost / SCALE, penal, SCALE);
	  continue;
	}

      if (hamm)
	{
	  // Go down that rabbit hole.
	  gcc_assert (ply_applied_p);
	  bbinfo_t::find_plies (1 + len, ii, memo);
	  continue;
	}

      // Found a solution that's better than everything so far.

      // Reduce the upper cost bound according to the found solution.
      // No future solution will be more expensive.
      fpd->max_ply_cost = cost / SCALE;

      fpd->solution = plies_t (len, fpd->ply_stack);

      if (dump_file)
	{
	  avr_dump (";; #%d FOUND COST = %d%s\n", len, cost / SCALE,
		    penal ? " with penalty" : "");
	  ply_t::dump_plys (dump_file, 0, len, fpd->ply_stack, fpd->regs0);
	  if (extra)
	    avr_dump (";; END\n");
	}
    } // for ply_t's

#undef NEXT
}


// Run .find_plies() and return true when .fpd->solution is a sequence of ply_t's
// that represents II, a REG = CONST insn.  MEMO is the GPR state prior to II.
bool
bbinfo_t::run_find_plies (const insninfo_t &ii, const memento_t &memo) const
{
  fpd->solution.reset ();
  fpd->regs0 = memo;
  fpd->n_get_plies = 0;

  const int hamm = ii.hamming (memo);

  if (hamm == 0)
    {
      avr_dump (";; Found redundant insn %d\n", INSN_UID (ii.m_insn));
      return true;
    }

  // Upper bound (in words) for any solution that's better than mov<mode>.
  // Will be decreased by find plies as it finds better solutions.
  fpd->movmode_cost = ii.cost ();
  fpd->max_ply_cost = fpd->movmode_cost;

  // With a non-zero Hamming distance, this insn will require at least one
  // instruction.  When the upper bound for required instructions is that
  // small, then the current insn is good enough.
  if (fpd->max_ply_cost <= 1)
    return false;

  fpd->n_best_plys = ii.n_best_plys (hamm);
  gcc_assert (fpd->n_best_plys <= N_BEST_PLYS);

  if (dump_file)
    {
      const uint64_t mask = size_to_mask (ii.m_size);
      fprintf (dump_file, ";; find_plies R%d[%d] = 0x%0*" PRIx64,
	       ii.m_regno, ii.m_size, 2 * ii.m_size, ii.m_isrc & mask);
      if (ii.m_scratch)
	fprintf (dump_file, ", scratch=r%d", ii.m_scratch);
      memo.dump ("\n;; regs%s\n");
    }

  avr_dump (";; mov<mode> cost = %d\n", fpd->max_ply_cost);
  avr_dump (";; max plys = %d\n", fpd->n_best_plys);
  ply_t::n_ply_ts = 0;

  find_plies (1, ii, memo);

  avr_dump (";; get_plies called %d times\n", fpd->n_get_plies);
  avr_dump (";; n_ply_ts = %d\n", ply_t::n_ply_ts);
  ply_t::max_n_ply_ts = std::max (ply_t::max_n_ply_ts, ply_t::n_ply_ts);

  return fpd->solution.n_plies != 0;
}


// Try to fuse two 1-byte insns .prev and .curr to one 2-byte insn (MOVW).
// Returns true on success, and sets .n_new_insns, .ignore_mask etc.
bool
optimize_data_t::try_fuse (bbinfo_t *bbi)
{
  insninfo_t comb;

  if (! prev.ii.m_size
      || ! curr.ii.m_size
      || ! comb.combine (prev.ii, curr.ii))
    return false;

  avr_dump (";; Working on fuse of insn %d + insn %d = 0x%04x\n",
	    INSN_UID (prev.insn), INSN_UID (curr.insn),
	    (unsigned) comb.m_isrc);

  bool found = bbi->run_find_plies (comb, prev.regs);
  if (found)
    {
      avr_dump (";; Found fuse of insns %d and %d\n",
		INSN_UID (prev.insn), INSN_UID (curr.insn));

      n_new_insns = bbinfo_t::fpd->solution.emit_insns (comb, prev.regs);
      delete_prev_p = true;

      if (prev.ii.m_scratch)
	ignore_mask |= regmask (prev.ii.m_scratch, 1);
      if (curr.ii.m_scratch)
	ignore_mask |= regmask (curr.ii.m_scratch, 1);
      ignore_mask &= ~regmask (comb.m_regno, comb.m_size);
    }

  return found;
}


// Try to replace an arithmetic 1-byte insn by a reg-reg move.
// Returns true on success, and sets .n_new_insns etc.
bool
optimize_data_t::try_simplify (bbinfo_t *)
{
  if (curr.ii.m_size == 1
      && curr.ii.m_old_code != REG
      && curr.ii.m_code == REG)
    {
      avr_dump (";; Found simplify of insn %d\n", INSN_UID (curr.insn));

      n_new_insns = curr.ii.emit_insn ();

      return true;
    }

  return false;
}


// Try to replace XEXP (*, 1) of a binary operation by a cheaper expression.
// Returns true on success; sets .n_new_insns, .ignore_mask, .delete_prev_p.
bool
optimize_data_t::try_bin_arg1 (bbinfo_t *)
{
  if (curr.ii.m_size != 1
      || curr.ii.m_new_src.arity () != 2
      || curr.unused)
    return false;

  avr_dump (";; Working on bin_arg1 insn %d\n", INSN_UID (curr.insn));

  gcc_assert (curr.ii.m_src && BINARY_P (curr.ii.m_src));
  rtx xarg1_old = XEXP (curr.ii.m_src, 1);

  const absint_byte_t &aib = curr.ii.m_new_src;
  const absint_val_t &arg0 = aib.arg (0);
  const absint_val_t &arg1 = aib.arg (1);
  const absint_val_t &arg1_old = curr.ii.m_ai[0].arg (1);

  rtx src = NULL_RTX;

  if (CONSTANT_P (xarg1_old))
    {
      // Sometimes, we allow expensive constants as 2nd operand like
      // in  R2 += 2  which produces two INCs.  When we have the
      // constant handy in a reg, then use that instead of the constant.
      const rtx_code code = aib.get_code ();
      gcc_assert (arg1.val8 == (INTVAL (xarg1_old) & 0xff));

      if (AVRasm::constant_cost (code, arg0.regno, arg1.val8) > 1)
	  src = aib.to_rtx ();
    }
  else if (REG_P (xarg1_old)
	   && dead_or_set_p (curr.insn, xarg1_old))
    {
      src = aib.to_rtx ();

      // The 2nd operand is a reg with a known content that dies
      // at the current insn.  Chances are high that the register
      // holds a reload value only used by the current insn.
      if (prev.ii.m_size == 1
	  && rtx_equal_p (xarg1_old, SET_DEST (prev.ii.m_set))
	  && CONSTANT_P (prev.ii.m_src))
	{
	  avr_dump (";; Found dying reload insn %d\n", INSN_UID (prev.insn));

	  delete_prev_p = true;
	  ignore_mask = regmask (arg1_old.regno, 1);
	}
    }

  if (src)
    {
      rtx dest = SET_DEST (curr.ii.m_set);

      avr_dump (";; Found bin_arg1 for insn %d: ", INSN_UID (curr.insn));
      avr_dump ("%C:%m %r", curr.ii.m_code, GET_MODE (dest), xarg1_old);
      aib.dump (" = %s\n");

      emit_valid_move_clobbercc (dest, src);
      n_new_insns = 1;
    }

  return src != NULL_RTX;
}


// Try to replace a REG = CONST insn by a cheaper sequence.
// Returns true on success, and sets .n_new_insns, .ignore_mask etc.
bool
optimize_data_t::try_split_ldi (bbinfo_t *bbi)
{
  if (! curr.ii.m_size
      || curr.unused
      || curr.ii.m_code != CONST_INT
      || (! bbinfo_t::try_split_any_p
	  // Finding plys will only ever succeed when there are
	  // regs with a known value.
	  && ! (curr.regs.known
		|| (AVR_HAVE_MOVW
		    && curr.ii.m_regno < REG_16 && curr.ii.m_size == 4))))
    return false;

  avr_dump (";; Working on split_ldi insn %d\n", INSN_UID (curr.insn));

  bool found = bbi->run_find_plies (curr.ii, curr.regs);
  if (found)
    {
      avr_dump (";; Found split for ldi insn %d\n", INSN_UID (curr.insn));

      n_new_insns = bbinfo_t::fpd->solution.emit_insns (curr.ii, curr.regs);

      if (curr.ii.m_scratch)
	ignore_mask = regmask (curr.ii.m_scratch, 1);
    }

  return found;
}


// Helper for try_split_any().
bool
optimize_data_t::fail (const char *reason)
{
  n_new_insns = -1;

  if (dump_file)
    fprintf (dump_file, ";; Giving up split_any: %s\n", reason);

  return false;
}


// Helper for try_split_any().
rtx_insn *
optimize_data_t::emit_and_apply_move (memento_t &memo, rtx dest, rtx src)
{
  rtx_insn *insn = emit_valid_move_clobbercc (dest, src);
  n_new_insns += 1;
  memo.apply_insn (insn, false);

  return insn;
}


// Set X0 and X1 so that they are operands valid for a andqi3, iorqi3, xorqi3
// or addqi3 insn with destination R_DEST.  The method loads X1 to
// a scratch reg as needed and records the GPR effect in IOD.regs.
// EXTRA_COST are extra costs in units of words of insns that cost more
// than one instruction.  This is a helper for try_split_any().
bool
optimize_data_t
    ::get_2ary_operands (rtx_code &code, const absint_byte_t &aib,
			 insn_optimize_data_t &iod, int r_dest,
			 absint_val_t &x0, absint_val_t &x1, int &extra_cost)
{
  if (code != IOR && code != AND && code != XOR && code != PLUS)
    return fail ("2ary: unknown code");

  x0 = aib.arg (0);
  x1 = aib.arg (1);

  if (! x0.knows_regno ()
      || x1.clueless ())
    return fail ("2ary: clueless");

  int val8 = x1.val8;
  int val8_cost = val8 < 0 ? 100 : AVRasm::constant_cost (code, r_dest, val8);

  if (x0.regno == r_dest
      && (x1.knows_regno ()
	  || val8_cost <= 1))
    {
      if (code == XOR
	  && val8 == 0x80
	  && x0.regno >= REG_16)
	{
	  // xorxi3 can only "r,0,r".
	  // x0 ^ 0x80  <=>  x0 - 0x80.
	  x1.regno = 0;
	  code = MINUS;
	}
      return true;
    }

  const bool and_1_bit = code == AND && popcount_hwi (val8) == 1;
  // andqi3 has a "r,r,Cb1" alternative where Cb1 has exactly 1 bit set.
  // This can accommodate bytes of higher AND Cb<N> alternatives.
  if (x0.regno != r_dest)
    {
      if (and_1_bit)
	{
	  extra_cost += 1 + (r_dest < REG_16);
	  return true;
	}
      else if (x1.regno == r_dest)
	{
	  std::swap (x0, x1);
	  return true;
	}
      return fail ("2ary is a 3-operand insn");
    }

  // Now we have:
  // 1)  r_dest = x0.regno, and
  // 2)  x1 is val8, and
  // 3)  x1 costs 2.

  const bool needs_scratch_p = select<bool>()
    : code == XOR ? true
    : code == AND ? popcount_hwi (val8) != 7
    : code == IOR ? popcount_hwi (val8) != 1
    : code == PLUS ? IN_RANGE (val8, 3, 0xff - 3)
    : bad_case<bool> ();

  const int r_val8 = iod.regs.regno_with_value (val8, 0 /* excludes: none */);
  if (r_val8)
    {
      // Found a reg that already holds the constant.
      x1.val8 = -1;
      x1.regno = r_val8;
      return true;
    }
  else if (iod.ii.m_scratch)
    {
      // Using the insn's scratch reg.
      rtx xdst = gen_rtx_REG (QImode, iod.ii.m_scratch);
      rtx xsrc = gen_int_mode (x1.val8, QImode);
      emit_and_apply_move (iod.regs, xdst, xsrc);

      x1.regno = iod.ii.m_scratch;
      x1.val8 = -1;

      return true;
    }
  else if (! needs_scratch_p)
    {
      // Some constants (1 and -1) can be loaded without a scratch.
      extra_cost += 1;
      return true;
    }
  else if (and_1_bit)
    {
      // This can always fall back to BST + CLR + BLD, but may be cheaper.
      extra_cost += 1 + (r_dest < REG_16);
      return true;
    }

  return fail ("2ary: expensive constant");
}


static inline bool
any_shift_p (rtx_code code)
{
  return code == LSHIFTRT || code == ASHIFTRT || code == ASHIFT;
}

// Try to split .curr into a sequence of 1-byte insns.
// Returns true on success.  Sets .n_new_insns and .ignore_mask.
bool
optimize_data_t::try_split_any (bbinfo_t *)
{
  if (curr.ii.m_size < 2
      // Constants are split by split_ldi.
      || CONSTANT_P (curr.ii.m_src)
      // Splitting requires knowledge about what to do with each byte.
      || curr.ii.m_ai.end_knows (VALUE) < curr.ii.m_size)
    return false;

  avr_dump (";; Working on split_any %C:%m insn %d\n", curr.ii.m_code,
	    GET_MODE (SET_DEST (curr.ii.m_set)), INSN_UID (curr.insn));

  const insninfo_t &ii = curr.ii;
  const int n_bytes = ii.m_size;
  int extra_cost = 0;
  int binop_cost = -1;

  // For plain AND, IOR, XOR get the current cost in units of words.
  if (BINARY_P (curr.ii.m_src))
    {
      const rtx_code code = curr.ii.m_code;
      if ((code == IOR || code == AND || code == XOR)
	  && REG_P (XEXP (curr.ii.m_src, 0))
	  && CONSTANT_P (XEXP (curr.ii.m_src, 1)))
	{
	  binop_cost = get_attr_length (curr.insn);
	  avr_dump (";; Competing against %C:%m cost = %d\n", code,
		    GET_MODE (curr.ii.m_src), binop_cost);
	}
    }

  // Step 1: Work out conflicts and which sign extends to perform.

  const gprmask_t regs_dest = regmask (ii.m_regno, n_bytes);
  int r_sign = 0;
  gprmask_t regs_signs = 0;
  bool has_lsl = false;
  bool has_lsr = false;

  for (int i = 0; i < n_bytes; ++i)
    {
      const absint_byte_t &aib = ii.m_ai[i];
      const int r_dest = ii.m_regno + i;
      const gprmask_t regs_src = aib.reg_mask ();

      // When only regs to the right are used, or only regs to the left
      // are used, then there's no conflict like it is arising for rotates.
      // For now, only implement conflict-free splits.
      has_lsl |= has_bits_in (regs_src & regs_dest, 0, r_dest - 1);
      has_lsr |= has_bits_in (regs_src & regs_dest, r_dest + 1, 31);
      if (has_lsl && has_lsr)
	return fail ("has both << and >>");

      if (aib.get_code () == SIGN_EXTEND)
	{
	  const absint_val_t x0 = aib.arg (0);
	  if (! r_sign)
	    r_sign = x0.regno;
	  else if (r_sign != x0.regno)
	    return fail ("too many signs");

	  // Signs are handled below after all the other bytes.
	  regs_signs |= regmask (r_dest, 1);
	}
    }

  // Step 2: Work on the individual bytes and emit according insns.

  n_new_insns = 0;
  memento_t memo = curr.regs;

  const int step = has_lsl ? -1 : 1;
  const int istart = step == 1 ? 0 : n_bytes - 1;
  const int iend = step == 1 ? n_bytes : -1;

  for (int i = istart; i != iend; i += step)
    {
      const absint_byte_t &aib = ii.m_ai[i];
      const int r_dest = ii.m_regno + i;
      rtx_code code = aib.get_code ();
      rtx xsrc = NULL_RTX;
      rtx xdest = gen_rtx_REG (QImode, r_dest);

      if (code == SET)
	{
	  const int r_src = aib.regno (false);
	  const int val8 = aib.val8 (false);
	  int r16;

	  // A no-op...
	  if (r_dest == r_src)
	    continue;
	  // ...or an existing 16-bit constant...
	  else if (AVR_HAVE_MOVW
		   && i + step != iend
		   // Next is not a no-op.
		   && ii.m_ai[i + step].regno (false) != r_dest + step
		   // Eligible for MOVW.
		   && r_dest + step == (r_dest ^ 1)
		   && r_dest % 2 == i % 2
		   && (r16 = ii.m_ai.reg16_with_value (i, i + step, memo)))
	    {
	      xdest = gen_rtx_REG (HImode, r_dest & ~1);
	      xsrc = gen_rtx_REG (HImode, r16);
	      i += step;
	    }
	  // ...or a cheap constant...
	  else if (val8 >= 0
		   && AVRasm::constant_cost (SET, r_dest, val8) <= 1)
	    xsrc = gen_int_mode (val8, QImode);
	  // ...or a reg-reg move...
	  else if (r_src)
	    xsrc = gen_rtx_REG (QImode, r_src);
	  // ...or a costly constant that already exists in some reg...
	  else if (memo.regno_with_value (val8, 0 /* excludes: none */))
	    xsrc = gen_rtx_REG (QImode, memo.regno_with_value (val8, 0));
	  // ...or a costly constant loaded into curr.insn's scratch reg...
	  else if (ii.m_scratch)
	    {
	      rtx xscratch = gen_rtx_REG (QImode, ii.m_scratch);
	      rtx xval8 = gen_int_mode (val8, QImode);
	      emit_and_apply_move (memo, xscratch, xval8);
	      xsrc = xscratch;
	    }
	  // ...or a costly constant (1 or -1) that doesn't need a scratch.
	  else if (! AVRasm::ldi_needs_scratch (r_dest, val8))
	    {
	      extra_cost += 1;
	      xsrc = gen_int_mode (val8, QImode);
	    }
	  else
	    return fail ("expensive val8");
	} // SET
      else if (aib.arity () == 1)
	{
	  if (aib.get_code () == SIGN_EXTEND)
	    // Signs are handled after all the others.
	    continue;
	  else
	    {
	      const absint_val_t x0 = aib.arg (0);
	      rtx xop0 = gen_rtx_REG (QImode, x0.regno);
	      xsrc = gen_rtx_fmt_e (code, QImode, xop0);
	    }
	} // unary
      else if (aib.arity () == 2)
	{
	  absint_val_t x0;
	  absint_val_t x1;
	  insn_optimize_data_t iod (memo);
	  iod.ii = curr.ii;

	  if (! get_2ary_operands (code, aib, iod, r_dest, x0, x1, extra_cost))
	    return false;
	  rtx xop0 = gen_rtx_REG (QImode, x0.regno);
	  rtx xop1 = x1.knows_val8 ()
	    ? gen_int_mode (x1.val8, QImode)
	    : gen_rtx_REG (QImode, x1.regno);

	  xsrc = gen_rtx_fmt_ee (code, QImode, xop0, xop1);
	} // binary

      if (! xsrc)
	return fail ("no source found");

      if (r_sign
	  && (regmask (xdest) & regmask (r_sign, 1)))
	return fail ("clobbered r_sign");

      emit_and_apply_move (memo, xdest, xsrc);
    }

  // Step 3: Emit insns for sign extend.
  // No more need to track memo beyond this point.

  if (! emit_signs (r_sign, regs_signs))
    return false;

  if (binop_cost >= 0)
    {
      avr_dump (";; Expected cost: %d + %d\n", n_new_insns, extra_cost);
      if (n_new_insns + extra_cost > binop_cost)
	return fail ("too expensive");
    }

  if (ii.m_scratch)
    ignore_mask = regmask (ii.m_scratch, 1);

  return true;
}


// A helper for try_split_any() above.
// Emit sign extends from R_MSB.7 to all regs in REGS_SIGNS.
bool
optimize_data_t::emit_signs (const int r_msb, gprmask_t regs_signs)
{
  if (! regs_signs)
    return true;
  else if (! r_msb)
    return fail ("fatal: no r_msb given");

  // Pick an arbitrary reg from the sign destinations when the source
  // isn't one of the signs.
  const int r_signs = regs_signs & regmask (r_msb, 1)
    ? r_msb
    : ctz_hwi (regs_signs);

  // Set all bits in r_signs according to the sign of r_msb using the
  // r,r,C07 alternative of ashrqi3.
  rtx xsrc = gen_rtx_fmt_ee (ASHIFTRT, QImode,
			     gen_rtx_REG (QImode, r_msb), GEN_INT (7));
  emit_valid_move_clobbercc (gen_rtx_REG (QImode, r_signs), xsrc);
  regs_signs &= ~regmask (r_signs, 1);

  // Set up a 16-bit sign register if possible.
  int r16_signs = 0;
  if (regs_signs & regmask (r_signs ^ 1, 1))
    {
      emit_move_mask (r_signs ^ 1, r_signs, 1, regs_signs);
      r16_signs = r_signs & ~1;
    }

  // Handle all 16-bit signs regs provided MOVW.
  if (AVR_HAVE_MOVW)
    for (int r = FIRST_GPR; r < REG_32; r += 2)
      {
	const gprmask_t m = regmask (r, 2);
	if ((m & regs_signs) == m)
	  {
	    if (r16_signs)
	      emit_move_mask (r, r16_signs, 2, regs_signs);
	    else
	      {
		emit_move_mask (r + 0, r_signs, 1, regs_signs);
		emit_move_mask (r + 1, r_signs, 1, regs_signs);
		r16_signs = r;
	      }
	  }
      }

  // Handle all remaining signs.
  while (regs_signs)
    emit_move_mask (ctz_hwi (regs_signs), r_signs, 1, regs_signs);

  return true;
}

// Helper for the method above.  Move N_BYTES registers from R_SRC to R_DST,
// keeping track of which regs are still to be done in MASK.
void
optimize_data_t::emit_move_mask (int r_dst, int r_src, int n_bytes,
				 gprmask_t &mask)
{
  const gprmask_t mask_dst = regmask (r_dst, n_bytes);
  const gprmask_t mask_src = regmask (r_src, n_bytes);
  gcc_assert ((mask_dst & mask) == mask_dst);
  gcc_assert ((mask_src & mask) == 0);
  rtx xdst = gen_rtx_REG (size_to_mode (n_bytes), r_dst);
  rtx xsrc = gen_rtx_REG (size_to_mode (n_bytes), r_src);
  emit_valid_move_clobbercc (xdst, xsrc);
  n_new_insns += 1;
  mask &= ~mask_dst;
}


void
bbinfo_t::optimize_one_block (bool &changed)
{
  memento_t prev_regs;

  rtx_insn *insn = next_nondebug_insn_bb (bb, BB_HEAD (bb));

  for (rtx_insn *next_insn; insn; insn = next_insn)
    {
      next_insn = next_nondebug_insn_bb (bb, insn);

      avr_dump ("\n;; Working on insn %d\n%r\n\n", INSN_UID (insn), insn);

      optimize_data_t od (prev_regs, regs);

      od.prev.insn = prev_nondebug_insn_bb (bb, insn);
      od.curr.insn = insn;

      od.prev.ii.init1 (od.prev, 1, "IIprev ");
      od.curr.ii.init1 (od.curr, 8, "IIcurr ");

      start_sequence ();

      bool found = ((bbinfo_t::try_fuse_p && od.try_fuse (this))
		    || (bbinfo_t::try_bin_arg1_p && od.try_bin_arg1 (this))
		    || (bbinfo_t::try_simplify_p && od.try_simplify (this))
		    || (bbinfo_t::try_split_ldi_p && od.try_split_ldi (this))
		    || (bbinfo_t::try_split_any_p && od.try_split_any (this)));

      rtx_insn *new_insns = get_insns ();
      end_sequence ();

      gcc_assert (found == (od.n_new_insns >= 0));

      ++tick;

      // This insn will become the previous one in the next loop iteration.
      // Just used in dumps.
      rtx_insn *new_curr_insn;

      if (! found)
	{
	  // Nothing changed.
	  avr_dump (";; Keeping old route.\n");
	  gcc_assert (! od.delete_prev_p);

	  prev_regs = regs;
	  regs.apply_insn (insn, false);

	  new_curr_insn = insn;
	}
      else
	{
	  // We have new_insns.
	  changed = true;

	  if (dump_file)
	    {
	      avr_dump ("\n;; EMIT %d new insn%s replacing ",
			od.n_new_insns, "s" + (od.n_new_insns == 1));
	      if (od.delete_prev_p)
		avr_dump ("insn %d and ", INSN_UID (od.prev.insn));
	      avr_dump ("insn %d, delete_prev=%d:\n%L\n", INSN_UID (insn),
			od.delete_prev_p, new_insns);
	    }

	  new_curr_insn = od.emit_sequence (bb, new_insns);
	} // found

      if (dump_file && new_curr_insn)
	{
	  avr_dump ("\n");

	  const int d = regs.distance_to (prev_regs);
	  if (d || new_curr_insn != insn)
	    avr_dump (";; %d regs changed state:\n", d);

	  if (new_curr_insn != insn)
	    {
	      avr_dump (";; Befor insn %d", INSN_UID (new_curr_insn));
	      prev_regs.dump ();
	    }

	  avr_dump (";; After insn %d", INSN_UID (new_curr_insn));
	  regs.dump ();
	}
    } // for BB insns
}


void
bbinfo_t::optimize_one_function (function *func)
{
  bbinfo_t::fpd = XNEW (bbinfo_t::find_plies_data_t);
  bbinfo_t::bb_info = XCNEWVEC (bbinfo_t, last_basic_block_for_fn (func));
  int *post_order = XNEWVEC (int, n_basic_blocks_for_fn (func));

  plies_t::max_n_plies = 0;

  using elt0_getter_HRS = elt0_getter<HARD_REG_SET, HARD_REG_ELT_TYPE>;
  memento_t::fixed_regs_mask = (gprmask_t) elt0_getter_HRS::get (fixed_reg_set);

  // Option -mfuse-move=<0,23> provides a 3:2:2:2 mixed radix value:
  // -mfuse-move= 0 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 20 1 2 3  Digit
  // fuse           1   1   1   1   1    1   1   1   1   1    1   1      0
  // bin_arg1         1 1     1 1      1 1     1 1     1 1      1 1      1
  // split_any            1 1 1 1          1 1 1 1          1 1 1 1      2
  // split_ldi                    1 1  1 1 1 1 1 1 1 1 1 1  1 1 1 1      3
  // use arith                                     1 1 1 1  1 1 1 1      3

  // Which optimization(s) to perform.
  bbinfo_t::try_fuse_p = avr_fuse_move & 0x1;      // Digit 0 in [0, 1].
  bbinfo_t::try_bin_arg1_p = avr_fuse_move & 0x2;  // Digit 1 in [0, 1].
  bbinfo_t::try_split_any_p = avr_fuse_move & 0x4; // Digit 2 in [0, 1].
  bbinfo_t::try_split_ldi_p = avr_fuse_move >> 3;       // Digit 3 in [0, 2].
  bbinfo_t::use_arith_p = (avr_fuse_move >> 3) >= 2;    // Digit 3 in [0, 2].
  bbinfo_t::use_set_some_p = bbinfo_t::try_split_ldi_p; // Digit 3 in [0, 2].
  bbinfo_t::try_simplify_p = avr_fuse_move != 0;

  // Topologically sort BBs from last to first.

  const int n_post_order = post_order_compute (post_order, false, false);
  bool changed = false;

  // Traverse the BBs from first to last in order to increase the chance
  // that register values from all incoming edges are known.

  for (int n = n_post_order - 1; n >= 0; --n)
    {
      basic_block bb = BASIC_BLOCK_FOR_FN (func, post_order[n]);

      bbinfo_t::bb_info[bb->index].bb = bb;
      bbinfo_t::bb_info[bb->index].enter ();
      bbinfo_t::bb_info[bb->index].optimize_one_block (changed);
      bbinfo_t::bb_info[bb->index].leave ();
    }

  if (plies_t::max_n_plies)
    avr_dump (";; max_n_plies=%d\n", (int) plies_t::max_n_plies);

  if (changed)
    {
      df_note_add_problem ();
      df_analyze ();
    }

  XDELETEVEC (post_order);
  XDELETEVEC (bbinfo_t::bb_info);
  XDELETE (bbinfo_t::fpd);
}

} // anonymous namespace


namespace
{


//////////////////////////////////////////////////////////////////////////////
// Try to replace 2 cbranch insns with 1 comparison and 2 branches.

static const pass_data avr_pass_data_ifelse =
{
  RTL_PASS,      // type
  "",            // name (will be patched)
  OPTGROUP_NONE, // optinfo_flags
  TV_DF_SCAN,    // tv_id
  0,             // properties_required
  0,             // properties_provided
  0,             // properties_destroyed
  0,             // todo_flags_start
  TODO_df_finish | TODO_df_verify // todo_flags_finish
};

class avr_pass_ifelse : public rtl_opt_pass
{
public:
  avr_pass_ifelse (gcc::context *ctxt, const char *name)
    : rtl_opt_pass (avr_pass_data_ifelse, ctxt)
  {
    this->name = name;
  }

  bool gate (function *) final override
  {
    return optimize > 0;
  }

  unsigned int execute (function *func) final override;
}; // avr_pass_ifelse


/* Return TRUE iff comparison code CODE is explicitly signed.  */

static bool
avr_strict_signed_p (rtx_code code)
{
  return code == GT || code == GE || code == LT || code == LE;
}


/* Return TRUE iff comparison code CODE is explicitly unsigned.  */

static bool
avr_strict_unsigned_p (rtx_code code)
{
  return code == GTU || code == GEU || code == LTU || code == LEU;
}

#include "config/avr/ranges.h"

/* Suppose the inputs represent a code like

      if (x <CMP1> XVAL1)  goto way1;
      if (x <CMP2> XVAL2)  goto way2;
      way3:;

   with two integer mode comparisons where XVAL1 and XVAL2 are CONST_INT.
   When this can be rewritten in the form

      if (x <cond1> xval)  goto way1;
      if (x <cond2> xval)  goto way2;
      way3:;

  then set CMP1 = cond1, CMP2 = cond2, and return xval.  Else return NULL_RTX.
  When SWAPT is returned true, then way1 and way2 must be swapped.
  When the incomping SWAPT is false, the outgoing one will be false, too.  */

static rtx
avr_2comparisons_rhs (rtx_code &cmp1, rtx xval1,
		      rtx_code &cmp2, rtx xval2,
		      machine_mode mode, bool &swapt)
{
  const bool may_swapt = swapt;
  swapt = false;

  //////////////////////////////////////////////////////////////////
  // Step 0: Decide about signedness, map xval1/2 to the range
  //         of [un]signed machine mode.

  const bool signed1_p = avr_strict_signed_p (cmp1);
  const bool signed2_p = avr_strict_signed_p (cmp2);
  const bool unsigned1_p = avr_strict_unsigned_p (cmp1);
  const bool unsigned2_p = avr_strict_unsigned_p (cmp2);
  const bool signed_p = signed1_p || signed2_p;
  bool unsigned_p = unsigned1_p || unsigned2_p;

  using T = Ranges::scalar_type;
  T val1 = INTVAL (xval1);
  T val2 = INTVAL (xval2);

  if (signed_p + unsigned_p > 1)
    {
      // Don't go down that rabbit hole.  When the RHSs are the
      // same, we can still save one comparison.
      return val1 == val2 ? xval1 : NULL_RTX;
    }

  // Decide about signedness.  When no explicit signedness is present,
  // then cases that are close to the unsigned boundary like  EQ 0, EQ 1
  // can also be optimized.
  if (unsigned_p
      || (! signed_p && IN_RANGE (val1, -2, 2)))
    {
      unsigned_p = true;
      val1 = UINTVAL (xval1) & GET_MODE_MASK (mode);
      val2 = UINTVAL (xval2) & GET_MODE_MASK (mode);
    }

  // No way we can decompose the domain in a usable manner when the
  // RHSes are too far apart.
  if (! IN_RANGE (val1 - val2, -2, 2))
    return NULL_RTX;

  //////////////////////////////////////////////////////////////////
  // Step 1: Represent the input conditions as truth Ranges.  This
  //         establishes a decomposition / coloring of the domain.

  Ranges dom = Ranges::NBitsRanges (GET_MODE_BITSIZE (mode), unsigned_p,
				    Ranges::ALL);
  Ranges r[4] = { dom, dom.truth (cmp1, val1), dom.truth (cmp2, val2), dom };

  // r[1] shadows r[2] shadows r[3].  r[0] is just for nice indices.
  r[3].minus (r[2]);
  r[3].minus (r[1]);
  r[2].minus (r[1]);

  //////////////////////////////////////////////////////////////////
  // Step 2: Filter for cases where the domain decomposes into three
  //         intervals:  One to the left, one to the right, and one
  //         in the middle where the latter holds exactly one value.

  for (int i = 1; i <= 3; ++i)
    {
      // Keep track of which Ranges is which.
      r[i].tag = i;

      gcc_assert (r[i].check ());

      // Filter for proper intervals.  Also return for the empty set,
      // since cases where [m_min, m_max] decomposes into two intervals
      // or less have been sorted out by the generic optimizers already,
      // and hence should not be seen here.  And more than two intervals
      // at a time cannot be optimized of course.
      if (r[i].size () != 1)
	return NULL_RTX;
    }

  // Bubble-sort the three intervals such that:
  // [1] is the left interval, i.e. the one taken by LT[U].
  // [2] is the middle interval, i.e. the one taken by EQ.
  // [3] is the right interval, i.e. the one taken by GT[U].
  Ranges::sort2 (r[1], r[3]);
  Ranges::sort2 (r[2], r[3]);
  Ranges::sort2 (r[1], r[2]);

  if (dump_file)
    fprintf (dump_file,
	     ";; Decomposed: .%d=[%ld, %ld] .%d=[%ld, %ld] .%d=[%ld, %ld]\n",
	     r[1].tag, (long) r[1].ranges[0].lo, (long) r[1].ranges[0].hi,
	     r[2].tag, (long) r[2].ranges[0].lo, (long) r[2].ranges[0].hi,
	     r[3].tag, (long) r[3].ranges[0].lo, (long) r[3].ranges[0].hi);

  // EQ / NE can handle only one value.
  if (r[2].cardinality (0) != 1)
    return NULL_RTX;

  // Success! This is the sought for xval.
  const T val = r[2].ranges[0].lo;

  //////////////////////////////////////////////////////////////////
  // Step 3: Work out which label gets which condition, trying to
  //         avoid the expensive codes GT[U] and LE[U] if possible.
  //         Avoiding expensive codes is always possible when labels
  //         way1 and way2 may be swapped.

  // The xx1 ways have an expensive GT for cmp1 which can be avoided
  // by swapping way1 with way2.
  swapt = may_swapt && r[3].tag == 1;
  if (swapt)
    std::swap (r[3], r[2].tag == 2 ? r[2] : r[1]);

  // 6 = 3! ways to assign LT, EQ, GT to the three labels.
  const int way = 100 * r[1].tag + 10 * r[2].tag + r[3].tag;

  if (dump_file)
    fprintf (dump_file, ";; Success: unsigned=%d, swapt=%d, way=%d, rhs=%ld\n",
	     unsigned_p, swapt, way, (long) val);

#define WAY(w, c1, c2)					\
  case w:						\
    cmp1 = unsigned_p ? unsigned_condition (c1) : c1;	\
    cmp2 = unsigned_p ? unsigned_condition (c2) : c2;	\
    break;

  switch (way)
    {
    default:
      gcc_unreachable();

      // cmp1 gets the LT, avoid difficult branches for cmp2.
      WAY (123, LT, EQ);
      WAY (132, LT, NE);

      // cmp1 gets the EQ, avoid difficult branches for cmp2.
      WAY (213, EQ, LT);
      WAY (312, EQ, GE);

      // cmp1 gets the difficult GT, unavoidable as we may not swap way1/2.
      WAY (231, GT, NE);
      WAY (321, GT, EQ);
    }

#undef WAY

  return gen_int_mode (val, mode);
}


/* A helper for the next method.  Suppose we have two conditional branches
   with REG and CONST_INT operands

      if (reg <cond1> xval1) goto label1;
      if (reg <cond2> xval2) goto label2;

   If the second comparison is redundant and there are codes <cmp1>
   and <cmp2> such that the sequence can be performed as

      REG_CC = compare (reg, xval);
      if (REG_CC <cmp1> 0) goto label1;
      if (REG_CC <cmp2> 0) goto label2;

   then set COND1 to cmp1, COND2 to cmp2, SWAPT to true when the branch
   targets have to be swapped, and return XVAL.  Otherwise, return NULL_RTX.
   This function may clobber COND1 and COND2 even when it returns NULL_RTX.

   REVERSE_COND1 can be set to reverse condition COND1.  This is useful
   when the second comparison does not follow the first one, but is
   located after label1 like in:

      if (reg <cond1> xval1) goto label1;
      ...
      label1:
      if (reg <cond2> xval2) goto label2;

   In such a case we cannot swap the labels, and we may end up with a
   difficult branch -- though one comparison can still be optimized out.
   Getting rid of such difficult branches would require to reorder blocks. */

static rtx
avr_redundant_compare (rtx xreg1, rtx_code &cond1, rtx xval1,
		       rtx xreg2, rtx_code &cond2, rtx xval2,
		       bool reverse_cond1, bool &swapt)
{
  // Make sure we have two REG <cond> CONST_INT comparisons with the same reg.
  if (! rtx_equal_p (xreg1, xreg2)
      || ! CONST_INT_P (xval1)
      || ! CONST_INT_P (xval2))
    return NULL_RTX;

  if (reverse_cond1)
    cond1 = reverse_condition (cond1);

  // Allow swapping label1 <-> label2 only when ! reverse_cond1.
  swapt = ! reverse_cond1;
  rtx_code c1 = cond1;
  rtx_code c2 = cond2;
  rtx xval = avr_2comparisons_rhs (c1, xval1,
				   c2, xval2, GET_MODE (xreg1), swapt);
  if (! xval)
    return NULL_RTX;

  if (dump_file)
    {
      rtx_code a1 = reverse_cond1 ? reverse_condition (cond1) : cond1;
      rtx_code b1 = reverse_cond1 ? reverse_condition (c1) : c1;
      const char *s_rev1 = reverse_cond1 ? " reverse_cond1" : "";
      avr_dump (";; cond1: %C %r%s\n", a1, xval1, s_rev1);
      avr_dump (";; cond2: %C %r\n", cond2, xval2);
      avr_dump (";; => %C %d\n", b1, (int) INTVAL (xval));
      avr_dump (";; => %C %d\n", c2, (int) INTVAL (xval));
    }

  cond1 = c1;
  cond2 = c2;

  return xval;
}


/* Similar to the function above, but assume that

      if (xreg1 <cond1> xval1) goto label1;
      if (xreg2 <cond2> xval2) goto label2;

   are two subsequent REG-REG comparisons.  When this can be represented as

      REG_CC = compare (reg, xval);
      if (REG_CC <cmp1> 0) goto label1;
      if (REG_CC <cmp2> 0) goto label2;

   then set XREG1 to reg, COND1 and COND2 accordingly, and return xval.
   Otherwise, return NULL_RTX.  This optmization can be performed
   when { xreg1, xval1 } and { xreg2, xval2 } are equal as sets.
   It can be done in such a way that no difficult branches occur.  */

static rtx
avr_redundant_compare_regs (rtx &xreg1, rtx_code &cond1, rtx &xval1,
			    rtx &xreg2, rtx_code &cond2, rtx &xval2,
			    bool reverse_cond1)
{
  bool swapped;

  if (! REG_P (xval1))
    return NULL_RTX;
  else if (rtx_equal_p (xreg1, xreg2)
	   && rtx_equal_p (xval1, xval2))
    swapped = false;
  else if (rtx_equal_p (xreg1, xval2)
	   && rtx_equal_p (xreg2, xval1))
    swapped = true;
  else
    return NULL_RTX;

  // Found a redundant REG-REG comparison.  Assume that the incoming
  // representation has been canonicalized by CANONICALIZE_COMPARISON.
  // We can always represent this using only one comparison and in such
  // a way that no difficult branches are required.

  if (dump_file)
    {
      const char *s_rev1 = reverse_cond1 ? " reverse_cond1" : "";
      avr_dump (";; %r %C %r%s\n", xreg1, cond1, xval1, s_rev1);
      avr_dump (";; %r %C %r\n", xreg2, cond2, xval2);
    }

  if (reverse_cond1)
    cond1 = reverse_condition (cond1);

  if (swapped)
    {
      if (cond1 == EQ || cond1 == NE)
	{
	  avr_dump (";; case #21\n");
	  std::swap (xreg1, xval1);
	}
      else
	{
	  std::swap (xreg2, xval2);
	  cond2 = swap_condition (cond2);

	  // The swap may have introduced a difficult comparison.
	  // In order to get of it, only a few cases need extra care.
	  if ((cond1 == LT && cond2 == GT)
	      || (cond1 == LTU && cond2 == GTU))
	    {
	      avr_dump (";; case #22\n");
	      cond2 = NE;
	    }
	  else
	    avr_dump (";; case #23\n");
	}
    }
  else
    avr_dump (";; case #20\n");

  return xval1;
}


/* INSN1 and INSN2 are two cbranch insns for the same integer mode.
   When FOLLOW_LABEL1 is false, then INSN2 is located in the fallthrough
   path of INSN1.  When FOLLOW_LABEL1 is true, then INSN2 is located at
   the true edge of INSN1, INSN2 is preceded by a barrier, and no other
   edge leads to the basic block of INSN2.

   Try to replace INSN1 and INSN2 by a compare insn and two branch insns.
   When such a replacement has been performed, then return the insn where the
   caller should continue scanning the insn stream.  Else, return nullptr.  */

static rtx_insn *
avr_optimize_2ifelse (rtx_jump_insn *insn1,
		      rtx_jump_insn *insn2, bool follow_label1)
{
  avr_dump (";; Investigating jump_insn %d and jump_insn %d.\n",
	    INSN_UID (insn1), INSN_UID (insn2));

  // Extract the operands of the insns:
  // $0 = comparison operator ($1, $2)
  // $1 = reg
  // $2 = reg or const_int
  // $3 = code_label
  // $4 = optional SCRATCH for HI, PSI, SI cases.

  const auto &op = recog_data.operand;

  extract_insn (insn1);
  rtx xop1[5] = { op[0], op[1], op[2], op[3], op[4] };
  int n_operands = recog_data.n_operands;

  extract_insn (insn2);
  rtx xop2[5] = { op[0], op[1], op[2], op[3], op[4] };

  rtx_code code1 = GET_CODE (xop1[0]);
  rtx_code code2 = GET_CODE (xop2[0]);
  bool swap_targets = false;

  // Search redundant REG-REG comparison.
  rtx xval = avr_redundant_compare_regs (xop1[1], code1, xop1[2],
					 xop2[1], code2, xop2[2],
					 follow_label1);

  // Search redundant REG-CONST_INT comparison.
  if (! xval)
    xval = avr_redundant_compare (xop1[1], code1, xop1[2],
				  xop2[1], code2, xop2[2],
				  follow_label1, swap_targets);
  if (! xval)
    {
      avr_dump (";; Nothing found for jump_insn %d and jump_insn %d.\n",
		INSN_UID (insn1), INSN_UID (insn2));
      return nullptr;
    }

  if (follow_label1)
    code1 = reverse_condition (code1);

  //////////////////////////////////////////////////////
  // Found a replacement.

  if (dump_file)
    {
      avr_dump (";; => %C %r\n", code1, xval);
      avr_dump (";; => %C %r\n", code2, xval);

      fprintf (dump_file, "\n;; Found chain of jump_insn %d and"
	       " jump_insn %d, follow_label1=%d:\n",
	       INSN_UID (insn1), INSN_UID (insn2), follow_label1);
      print_rtl_single (dump_file, PATTERN (insn1));
      print_rtl_single (dump_file, PATTERN (insn2));
    }

  rtx_insn *next_insn
    = next_nonnote_nondebug_insn (follow_label1 ? insn1 : insn2);

  // Pop the new branch conditions and the new comparison.
  // Prematurely split into compare + branch so that we can drop
  // the 2nd comparison.  The following pass, split2, splits all
  // insns for REG_CC, and it should still work as usual even when
  // there are already some REG_CC insns around.

  rtx xcond1 = gen_rtx_fmt_ee (code1, VOIDmode, cc_reg_rtx, const0_rtx);
  rtx xcond2 = gen_rtx_fmt_ee (code2, VOIDmode, cc_reg_rtx, const0_rtx);
  rtx xpat1 = gen_branch (xop1[3], xcond1);
  rtx xpat2 = gen_branch (xop2[3], xcond2);
  rtx xcompare = NULL_RTX;
  machine_mode mode = GET_MODE (xop1[1]);

  if (mode == QImode)
    {
      gcc_assert (n_operands == 4);
      xcompare = gen_cmpqi3 (xop1[1], xval);
    }
  else
    {
      gcc_assert (n_operands == 5);
      rtx scratch = GET_CODE (xop1[4]) == SCRATCH ? xop2[4] : xop1[4];
      rtx (*gen_cmp)(rtx,rtx,rtx)
	= mode == HImode  ? gen_gen_comparehi
	: mode == PSImode ? gen_gen_comparepsi
	: gen_gen_comparesi; // SImode
      xcompare = gen_cmp (xop1[1], xval, scratch);
    }

  // Emit that stuff.

  rtx_insn *cmp = emit_insn_before (xcompare, insn1);
  rtx_jump_insn *branch1 = emit_jump_insn_after (xpat1, insn1);
  rtx_jump_insn *branch2 = emit_jump_insn_after (xpat2, insn2);

  JUMP_LABEL (branch1) = xop1[3];
  JUMP_LABEL (branch2) = xop2[3];
  // delete_insn() decrements LABEL_NUSES when deleting a JUMP_INSN,
  // but when we pop a new JUMP_INSN, do it by hand.
  ++LABEL_NUSES (xop1[3]);
  ++LABEL_NUSES (xop2[3]);

  delete_insn (insn1);
  delete_insn (insn2);

  if (swap_targets)
    {
      gcc_assert (! follow_label1);

      basic_block to1 = BLOCK_FOR_INSN (xop1[3]);
      basic_block to2 = BLOCK_FOR_INSN (xop2[3]);
      edge e1 = find_edge (BLOCK_FOR_INSN (branch1), to1);
      edge e2 = find_edge (BLOCK_FOR_INSN (branch2), to2);
      gcc_assert (e1);
      gcc_assert (e2);
      redirect_edge_and_branch (e1, to2);
      redirect_edge_and_branch (e2, to1);
    }

  // As a side effect, also recog the new insns.
  gcc_assert (valid_insn_p (cmp));
  gcc_assert (valid_insn_p (branch1));
  gcc_assert (valid_insn_p (branch2));

  return next_insn;
}


/* Sequences like

      SREG = compare (reg, 1 + val);
	  if (SREG >= 0)  goto label1;
      SREG = compare (reg, val);
	  if (SREG == 0)  goto label2;

   can be optimized to

      SREG = compare (reg, val);
	  if (SREG == 0)  goto label2;
	  if (SREG >= 0)  goto label1;

   Almost all cases where one of the comparisons is redundant can
   be transformed in such a way that only one comparison is required
   and no difficult branches are needed.  */

unsigned int
avr_pass_ifelse::execute (function *)
{
  rtx_insn *next_insn;

  for (rtx_insn *insn = get_insns(); insn; insn = next_insn)
    {
      next_insn = next_nonnote_nondebug_insn (insn);

      if (! next_insn)
	break;

      // Search for two cbranch insns.  The first one is a cbranch.
      // Filter for "cbranch<mode>4_insn" with mode in QI, HI, PSI, SI.

      if (! JUMP_P (insn))
	continue;

      int icode1 = recog_memoized (insn);

      if (icode1 != CODE_FOR_cbranchqi4_insn
	  && icode1 != CODE_FOR_cbranchhi4_insn
	  && icode1 != CODE_FOR_cbranchpsi4_insn
	  && icode1 != CODE_FOR_cbranchsi4_insn)
	continue;

      rtx_jump_insn *insn1 = as_a<rtx_jump_insn *> (insn);

      // jmp[0]: We can optimize cbranches that follow cbranch insn1.
      rtx_insn *jmp[2] = { next_insn, nullptr };

      // jmp[1]: A cbranch following the label of cbranch insn1.
      if (LABEL_NUSES (JUMP_LABEL (insn1)) == 1)
	{
	  rtx_insn *code_label1 = JUMP_LABEL_AS_INSN (insn1);
	  rtx_insn *barrier = prev_nonnote_nondebug_insn (code_label1);

	  // When the target label of insn1 is used exactly once and is
	  // not a fallthrough, i.e. is preceded by a barrier, then
	  // consider the insn following that label.
	  if (barrier && BARRIER_P (barrier))
	    jmp[1] = next_nonnote_nondebug_insn (code_label1);
      }

      // With almost certainty, only one of the two possible jumps can
      // be optimized with insn1, but it's hard to tell which one a priori.
      // Just try both.  In the unlikely case where both could be optimized,
      // prefer jmp[0] because eliminating difficult branches is impeded
      // by following label1.

      for (int j = 0; j < 2; ++j)
	if (jmp[j] && JUMP_P (jmp[j])
	    && recog_memoized (jmp[j]) == icode1)
	  {
	    rtx_insn *next
	      = avr_optimize_2ifelse (insn1, as_a<rtx_jump_insn *> (jmp[j]),
				      j == 1 /* follow_label1 */);
	    if (next)
	      {
		next_insn = next;
		break;
	      }
	  }

    } // loop insns

  return 0;
}



//////////////////////////////////////////////////////////////////////////////
// Optimize results of the casesi expander for modes < SImode.

static const pass_data avr_pass_data_casesi =
{
  RTL_PASS,      // type
  "",            // name (will be patched)
  OPTGROUP_NONE, // optinfo_flags
  TV_DF_SCAN,    // tv_id
  0,             // properties_required
  0,             // properties_provided
  0,             // properties_destroyed
  0,             // todo_flags_start
  0              // todo_flags_finish
};

class avr_pass_casesi : public rtl_opt_pass
{
public:
  avr_pass_casesi (gcc::context *ctxt, const char *name)
    : rtl_opt_pass (avr_pass_data_casesi, ctxt)
  {
    this->name = name;
  }

  bool gate (function *) final override
  {
    return optimize > 0;
  }

  unsigned int execute (function *) final override;
}; // avr_pass_casesi


/* Make one parallel insn with all the patterns from insns i[0]..i[5].  */

static rtx_insn *
avr_parallel_insn_from_insns (rtx_insn *i[5])
{
  rtvec vec = gen_rtvec (5, PATTERN (i[0]), PATTERN (i[1]), PATTERN (i[2]),
			 PATTERN (i[3]), PATTERN (i[4]));
  start_sequence();
  emit (gen_rtx_PARALLEL (VOIDmode, vec));
  rtx_insn *insn = get_insns();
  end_sequence();

  return insn;
}


/* Return true if we see an insn stream generated by casesi expander together
   with an extension to SImode of the switch value.

   If this is the case, fill in the insns from casesi to INSNS[1..5] and
   the SImode extension to INSNS[0].  Moreover, extract the operands of
   pattern casesi_<mode>_sequence forged from the sequence to recog_data.  */

static bool
avr_is_casesi_sequence (basic_block bb, rtx_insn *insn, rtx_insn *insns[5])
{
  rtx set_4, set_0;

  /* A first and quick test for a casesi sequences.  As a side effect of
     the test, harvest respective insns to INSNS[0..4].  */

  if (!(JUMP_P (insns[4] = insn)
	// casesi is the only insn that comes up with UNSPEC_INDEX_JMP,
	// hence the following test ensures that we are actually dealing
	// with code from casesi.
	&& (set_4 = single_set (insns[4]))
	&& UNSPEC == GET_CODE (SET_SRC (set_4))
	&& UNSPEC_INDEX_JMP == XINT (SET_SRC (set_4), 1)

	&& (insns[3] = prev_real_insn (insns[4]))
	&& (insns[2] = prev_real_insn (insns[3]))
	&& (insns[1] = prev_real_insn (insns[2]))

	// Insn prior to casesi.
	&& (insns[0] = prev_real_insn (insns[1]))
	&& (set_0 = single_set (insns[0]))
	&& extend_operator (SET_SRC (set_0), SImode)))
    {
      return false;
    }

  if (dump_file)
    {
      fprintf (dump_file, ";; Sequence from casesi in "
	       "[bb %d]:\n\n", bb->index);
      for (int i = 0; i < 5; i++)
	print_rtl_single (dump_file, insns[i]);
    }

  /* We have to deal with quite some operands.  Extracting them by hand
     would be tedious, therefore wrap the insn patterns into a parallel,
     run recog against it and then use insn extract to get the operands. */

  rtx_insn *xinsn = avr_parallel_insn_from_insns (insns);

  INSN_CODE (xinsn) = recog (PATTERN (xinsn), xinsn, NULL /* num_clobbers */);

  /* Failing to recognize means that someone changed the casesi expander or
     that some passes prior to this one performed some unexpected changes.
     Gracefully drop such situations instead of aborting.  */

  if (INSN_CODE (xinsn) < 0)
    {
      if (dump_file)
	fprintf (dump_file, ";; Sequence not recognized, giving up.\n\n");

      return false;
    }

  gcc_assert (CODE_FOR_casesi_qi_sequence == INSN_CODE (xinsn)
	      || CODE_FOR_casesi_hi_sequence == INSN_CODE (xinsn));

  extract_insn (xinsn);

  // Assert on the anatomy of xinsn's operands we are going to work with.

  gcc_assert (recog_data.n_operands == 12);
  gcc_assert (recog_data.n_dups == 3);

  if (dump_file)
    {
      fprintf (dump_file, ";; Operands extracted:\n");
      for (int i = 0; i < recog_data.n_operands; i++)
	avr_fdump (dump_file, ";; $%d = %r\n", i, recog_data.operand[i]);
      fprintf (dump_file, "\n");
    }

  return true;
}


/* INSNS[1..4] is a sequence as generated by casesi and INSNS[0] is an
   extension of an 8-bit or 16-bit integer to SImode.  XOP contains the
   operands of INSNS as extracted by insn_extract from pattern
   casesi_<mode>_sequence:

      $0: SImode reg switch value as result of $10.
      $1: Negative of smallest index in switch.
      $2: Number of entries in switch.
      $3: Label to table.
      $4: Label if out-of-bounds.
      $5: $0 + $1.
      $6: 3-byte PC: subreg:HI ($5) + label_ref ($3)
	  2-byte PC: subreg:HI ($5)
      $7: HI reg index into table (Z or pseudo)
      $8: Z or scratch:HI (to be clobbered)
      $9: R24 or const0_rtx (to be clobbered)
      $10: Extension to SImode of an 8-bit or 16-bit integer register $11.
      $11: QImode or HImode register input of $10.

   Try to optimize this sequence, i.e. use the original HImode / QImode
   switch value instead of SImode.  */

static void
avr_optimize_casesi (rtx_insn *insns[5], rtx *xop)
{
  // Original mode of the switch value; this is QImode or HImode.
  machine_mode mode = GET_MODE (xop[11]);

  // How the original switch value was extended to SImode; this is
  // SIGN_EXTEND or ZERO_EXTEND.
  rtx_code code = GET_CODE (xop[10]);

  // Lower index, upper index (plus one) and range of case calues.
  HOST_WIDE_INT low_idx = -INTVAL (xop[1]);
  HOST_WIDE_INT num_idx = INTVAL (xop[2]);
  HOST_WIDE_INT hig_idx = low_idx + num_idx;

  // Maximum ranges of (un)signed QImode resp. HImode.
  unsigned umax = QImode == mode ? 0xff : 0xffff;
  int imax = QImode == mode ? 0x7f : 0x7fff;
  int imin = -imax - 1;

  // Testing the case range and whether it fits into the range of the
  // (un)signed mode.  This test should actually always pass because it
  // makes no sense to have case values outside the mode range.  Notice
  // that case labels which are unreachable because they are outside the
  // mode of the switch value (e.g. "case -1" for uint8_t) have already
  // been thrown away by the middle-end.

  if (SIGN_EXTEND == code
      && low_idx >= imin
      && hig_idx <= imax)
    {
      // ok
    }
  else if (ZERO_EXTEND == code
	   && low_idx >= 0
	   && (unsigned) hig_idx <= umax)
    {
      // ok
    }
  else
    {
      if (dump_file)
	fprintf (dump_file, ";; Case ranges too big, giving up.\n\n");
      return;
    }

  // Do normalization of switch value $10 and out-of-bound check in its
  // original mode instead of in SImode.  Use a newly created pseudo.
  // This will replace insns[1..2].

  start_sequence();

  rtx reg = copy_to_mode_reg (mode, xop[11]);

  rtx (*gen_add)(rtx,rtx,rtx) = QImode == mode ? gen_addqi3 : gen_addhi3;
  rtx (*gen_cbranch)(rtx,rtx,rtx,rtx)
    = QImode == mode ? gen_cbranchqi4 : gen_cbranchhi4;

  emit_insn (gen_add (reg, reg, gen_int_mode (-low_idx, mode)));
  rtx op0 = reg; rtx op1 = gen_int_mode (num_idx, mode);
  rtx labelref = copy_rtx (xop[4]);
  rtx xbranch = gen_cbranch (gen_rtx_fmt_ee (GTU, VOIDmode, op0, op1),
			     op0, op1, labelref);
  rtx_insn *cbranch = emit_jump_insn (xbranch);
  JUMP_LABEL (cbranch) = xop[4];
  ++LABEL_NUSES (xop[4]);

  rtx_insn *seq1 = get_insns();
  rtx_insn *last1 = get_last_insn();
  end_sequence();

  emit_insn_after (seq1, insns[2]);

  // After the out-of-bounds test and corresponding branch, use a
  // 16-bit index.  If QImode is used, extend it to HImode first.
  // This will replace insns[4].

  start_sequence();

  if (QImode == mode)
    reg = force_reg (HImode, gen_rtx_fmt_e (code, HImode, reg));

  rtx pat_4 = AVR_3_BYTE_PC
    ? gen_movhi (xop[7], reg)
    : gen_addhi3 (xop[7], reg, gen_rtx_LABEL_REF (VOIDmode, xop[3]));

  emit_insn (pat_4);

  rtx_insn *seq2 = get_insns();
  rtx_insn *last2 = get_last_insn();
  end_sequence();

  emit_insn_after (seq2, insns[3]);

  if (dump_file)
    {
      fprintf (dump_file, ";; New insns: ");

      for (rtx_insn *insn = seq1; ; insn = NEXT_INSN (insn))
	{
	  fprintf (dump_file, "%d, ", INSN_UID (insn));
	  if (insn == last1)
	    break;
	}
      for (rtx_insn *insn = seq2; ; insn = NEXT_INSN (insn))
	{
	  fprintf (dump_file, "%d%s", INSN_UID (insn),
		   insn == last2 ? ".\n\n" : ", ");
	  if (insn == last2)
	    break;
	}

      fprintf (dump_file, ";; Deleting insns: %d, %d, %d.\n\n",
	       INSN_UID (insns[1]), INSN_UID (insns[2]), INSN_UID (insns[3]));
    }

  // Pseudodelete the SImode and subreg of SImode insns.  We don't care
  // about the extension insns[0]: Its result is now unused and other
  // passes will clean it up.

  SET_INSN_DELETED (insns[1]);
  SET_INSN_DELETED (insns[2]);
  SET_INSN_DELETED (insns[3]);
}


unsigned int
avr_pass_casesi::execute (function *func)
{
  basic_block bb;

  FOR_EACH_BB_FN (bb, func)
    {
      rtx_insn *insn, *insns[5];

      FOR_BB_INSNS (bb, insn)
	{
	  if (avr_is_casesi_sequence (bb, insn, insns))
	    {
	      avr_optimize_casesi (insns, recog_data.operand);
	    }
	}
    }

  return 0;
}

} // anonymous namespace

/* Perform some extra checks on operands of casesi_<mode>_sequence.
   Not all operand dependencies can be described by means of predicates.
   This function performs left over checks and should always return true.
   Returning false means that someone changed the casesi expander but did
   not adjust casesi_<mode>_sequence.  */

bool
avr_casei_sequence_check_operands (rtx *xop)
{
  rtx sub_5 = NULL_RTX;

  if (AVR_HAVE_EIJMP_EICALL
      // The last clobber op of the tablejump.
      && xop[9] == all_regs_rtx[REG_24])
    {
      // $6 is: (subreg:SI ($5) 0)
      sub_5 = xop[6];
    }

  if (!AVR_HAVE_EIJMP_EICALL
      // $6 is: (plus:HI (subreg:SI ($5) 0)
      //		 (label_ref ($3)))
      && PLUS == GET_CODE (xop[6])
      && LABEL_REF == GET_CODE (XEXP (xop[6], 1))
      && rtx_equal_p (xop[3], XEXP (XEXP (xop[6], 1), 0))
      // The last clobber op of the tablejump.
      && xop[9] == const0_rtx)
    {
      sub_5 = XEXP (xop[6], 0);
    }

  if (sub_5
      && SUBREG_P (sub_5)
      && SUBREG_BYTE (sub_5) == 0
      && rtx_equal_p (xop[5], SUBREG_REG (sub_5)))
    return true;

  if (dump_file)
    fprintf (dump_file, "\n;; Failed condition for casesi_<mode>_sequence\n\n");

  return false;
}

namespace
{


//////////////////////////////////////////////////////////////////////////////
// Find more POST_INC and PRE_DEC cases.

static const pass_data avr_pass_data_fuse_add =
{
  RTL_PASS,	    // type
  "",		    // name (will be patched)
  OPTGROUP_NONE,    // optinfo_flags
  TV_MACH_DEP,	    // tv_id
  0,		    // properties_required
  0,		    // properties_provided
  0,		    // properties_destroyed
  0,		    // todo_flags_start
  TODO_df_finish    // todo_flags_finish
};

class avr_pass_fuse_add : public rtl_opt_pass
{
public:
  avr_pass_fuse_add (gcc::context *ctxt, const char *name)
    : rtl_opt_pass (avr_pass_data_fuse_add, ctxt)
  {
    this->name = name;
  }

  // Cloning is required because we are running one instance of the pass
  // before peephole2. and a second one after cprop_hardreg.
  opt_pass * clone () final override
  {
    return make_avr_pass_fuse_add (m_ctxt);
  }

  bool gate (function *) final override
  {
    return optimize && avr_fuse_add > 0;
  }

  unsigned int execute (function *) final override;

  struct Some_Insn
  {
    rtx_insn *insn = nullptr;
    rtx dest, src;
    bool valid () const { return insn != nullptr; }
    void set_deleted ()
    {
      gcc_assert (insn);
      SET_INSN_DELETED (insn);
      insn = nullptr;
    }
  };

  // If .insn is not NULL, then this is a  reg:HI += const_int
  // of an address register.
  struct Add_Insn : Some_Insn
  {
    rtx addend;
    int regno;
    Add_Insn () {}
    Add_Insn (rtx_insn *insn);
  };

  // If .insn is not NULL, then this sets an address register
  // to a constant value.
  struct Ldi_Insn : Some_Insn
  {
    int regno;
    Ldi_Insn () {}
    Ldi_Insn (rtx_insn *insn);
  };

  // If .insn is not NULL, then this is a load or store insn where the
  // address is REG or POST_INC with an address register.
  struct Mem_Insn : Some_Insn
  {
    rtx reg_or_0, mem, addr, addr_reg;
    int addr_regno;
    rtx_code addr_code;
    machine_mode mode;
    addr_space_t addr_space;
    bool store_p, volatile_p;
    Mem_Insn () {}
    Mem_Insn (rtx_insn *insn);
  };

  rtx_insn *fuse_ldi_add (Ldi_Insn &prev_ldi, Add_Insn &add);
  rtx_insn *fuse_add_add (Add_Insn &prev_add, Add_Insn &add);
  rtx_insn *fuse_add_mem (Add_Insn &prev_add, Mem_Insn &mem);
  rtx_insn *fuse_mem_add (Mem_Insn &prev_mem, Add_Insn &add);
}; // avr_pass_fuse_add


/* Describe properties of AVR's indirect load and store instructions
   LD, LDD, ST, STD, LPM, ELPM depending on register number, volatility etc.
   Rules for "volatile" accesses are:

	 | Xmega	   |  non-Xmega
   ------+-----------------+----------------
   load  | read LSB first  | read LSB first
   store | write LSB first | write MSB first
*/

struct AVR_LdSt_Props
{
  bool has_postinc, has_predec, has_ldd;
  // The insn printers will use POST_INC or PRE_DEC addressing, no matter
  // what adressing modes we are feeding into them.
  bool want_postinc, want_predec;

  AVR_LdSt_Props (int regno, bool store_p, bool volatile_p, addr_space_t as)
  {
    bool generic_p = ADDR_SPACE_GENERIC_P (as);
    bool flashx_p = ! generic_p && as != ADDR_SPACE_MEMX;
    has_postinc = generic_p || (flashx_p && regno == REG_Z);
    has_predec = generic_p;
    has_ldd = ! AVR_TINY && generic_p && (regno == REG_Y || regno == REG_Z);
    want_predec  = volatile_p && generic_p && ! AVR_XMEGA && store_p;
    want_postinc = volatile_p && generic_p && (AVR_XMEGA || ! store_p);
    want_postinc |= flashx_p && regno == REG_Z;
  }

  AVR_LdSt_Props (const avr_pass_fuse_add::Mem_Insn &m)
    : AVR_LdSt_Props (m.addr_regno, m.store_p, m.volatile_p, m.addr_space)
  {
    gcc_assert (m.valid ());
  }
};


/* Emit a single_set that clobbers REG_CC.  */

static rtx_insn *
emit_move_ccc (rtx dest, rtx src)
{
  return emit_insn (gen_gen_move_clobbercc (dest, src));
}


/* Emit a single_set that clobbers REG_CC after insn AFTER.  */

static rtx_insn *
emit_move_ccc_after (rtx dest, rtx src, rtx_insn *after)
{
  return emit_insn_after (gen_gen_move_clobbercc (dest, src), after);
}

static bool
reg_seen_between_p (const_rtx reg, const rtx_insn *from, const rtx_insn *to)
{
  return (reg_used_between_p (reg, from, to)
	  || reg_set_between_p (reg, from, to));
}


static void
avr_maybe_adjust_cfa (rtx_insn *insn, rtx reg, int addend)
{
  if (addend
      && frame_pointer_needed
      && REGNO (reg) == FRAME_POINTER_REGNUM
      && avr_fuse_add == 3)
    {
      rtx plus = plus_constant (Pmode, reg, addend);
      RTX_FRAME_RELATED_P (insn) = 1;
      add_reg_note (insn, REG_CFA_ADJUST_CFA, gen_rtx_SET (reg, plus));
    }
}


// If successful, this represents a SET of a pointer register to a constant.
avr_pass_fuse_add::Ldi_Insn::Ldi_Insn (rtx_insn *insn)
{
  rtx set = single_set (insn);
  if (!set)
    return;

  src = SET_SRC (set);
  dest = SET_DEST (set);

  if (REG_P (dest)
      && GET_MODE (dest) == Pmode
      && IN_RANGE (regno = REGNO (dest), REG_X, REG_Z)
      && CONSTANT_P (src))
    {
      this->insn = insn;
    }
}

// If successful, this represents a PLUS with CONST_INT of a pointer
// register X, Y or Z.  Otherwise, the object is not valid().
avr_pass_fuse_add::Add_Insn::Add_Insn (rtx_insn *insn)
{
  rtx set = single_set (insn);
  if (!set)
    return;

  src = SET_SRC (set);
  dest = SET_DEST (set);
  if (REG_P (dest)
      // We are only interested in PLUSes that change address regs.
      && GET_MODE (dest) == Pmode
      && IN_RANGE (regno = REGNO (dest), REG_X, REG_Z)
      && PLUS == GET_CODE (src)
      && rtx_equal_p (XEXP (src, 0), dest)
      && CONST_INT_P (XEXP (src, 1)))
    {
      // This is reg:HI += const_int.
      addend = XEXP (src, 1);
      this->insn = insn;
    }
}

// If successful, this represents a load or store insn where the addressing
// mode uses pointer register X, Y or Z.  Otherwise, the object is not valid().
avr_pass_fuse_add::avr_pass_fuse_add::Mem_Insn::Mem_Insn (rtx_insn *insn)
{
  rtx set = single_set (insn);
  if (!set)
    return;

  src = SET_SRC (set);
  dest = SET_DEST (set);
  mode = GET_MODE (dest);

  if (MEM_P (dest)
      && (REG_P (src) || src == CONST0_RTX (mode)))
    {
      reg_or_0 = src;
      mem = dest;
    }
  else if (REG_P (dest) && MEM_P (src))
    {
      reg_or_0 = dest;
      mem = src;
    }
  else
    return;

  if (avr_mem_memx_p (mem)
      || avr_load_libgcc_p (mem))
    return;

  addr = XEXP (mem, 0);
  addr_code = GET_CODE (addr);

  if (addr_code == REG)
    addr_reg = addr;
  else if (addr_code == POST_INC || addr_code == PRE_DEC)
    addr_reg = XEXP (addr, 0);
  else
    return;

  addr_regno = REGNO (addr_reg);

  if (avr_fuse_add == 2
      && frame_pointer_needed
      && addr_regno == FRAME_POINTER_REGNUM)
    MEM_VOLATILE_P (mem) = 0;

  if (reg_overlap_mentioned_p (reg_or_0, addr) // Can handle CONSTANT_P.
      || addr_regno > REG_Z
      || avr_mem_memx_p (mem)
      // The following optimizations only handle REG and POST_INC,
      // so that's all what we allow here.
      || (addr_code != REG && addr_code != POST_INC))
    return;

  addr_space = MEM_ADDR_SPACE (mem);
  volatile_p = MEM_VOLATILE_P (mem);
  store_p = MEM_P (dest);

  // Turn this "valid".
  this->insn = insn;
}

/* Try to combine a Ldi insn with a PLUS CONST_INT addend to one Ldi insn.
   If LDI is valid, then it precedes ADD in the same block.
   When a replacement is found, a new insn is emitted and the old insns
   are pseudo-deleted.  The returned insn is the point where the calling
   scanner should continue.  When no replacement is found, nullptr is
   returned and nothing changed.  */

rtx_insn *
avr_pass_fuse_add::fuse_ldi_add (Ldi_Insn &ldi, Add_Insn &add)
{
  if (! ldi.valid ()
      || reg_seen_between_p (ldi.dest, ldi.insn, add.insn))
    {
      // If something is between the Ldi and the current insn, we can
      // set the Ldi invalid to speed future scans.
      return ldi.insn = nullptr;
    }

  // Found a Ldi with const and a PLUS insns in the same BB,
  // and with no interfering insns between them.

  // Emit new Ldi with the sum of the original offsets after the old Ldi.
  rtx xval = plus_constant (Pmode, ldi.src, INTVAL (add.addend));

  rtx_insn *insn = emit_move_ccc_after (ldi.dest, xval, ldi.insn);
  avr_dump (";; new Ldi[%d] insn %d after %d: R%d = %r\n\n", ldi.regno,
	    INSN_UID (insn), INSN_UID (ldi.insn), ldi.regno, xval);

  rtx_insn *next = NEXT_INSN (add.insn);
  ldi.set_deleted ();
  add.set_deleted ();

  return next;
}

/* Try to combine two PLUS insns with CONST_INT addend to one such insn.
   If PREV_ADD is valid, then it precedes ADD in the same basic block.
   When a replacement is found, a new insn is emitted and the old insns
   are pseudo-deleted.  The returned insn is the point where the calling
   scanner should continue.  When no replacement is found, nullptr is
   returned and nothing changed.  */

rtx_insn *
avr_pass_fuse_add::fuse_add_add (Add_Insn &prev_add, Add_Insn &add)
{
  if (! prev_add.valid ()
      || reg_seen_between_p (add.dest, prev_add.insn, add.insn))
    {
      // If something is between the previous Add and the current insn,
      // we can set the previous Add invalid to speed future scans.
      return prev_add.insn = nullptr;
    }

  // Found two PLUS insns in the same BB, and with no interfering
  // insns between them.
  rtx plus = plus_constant (Pmode, add.src, INTVAL (prev_add.addend));

  rtx_insn *next;
  if (REG_P (plus))
    {
      avr_dump (";; Add[%d] from %d annihilates %d\n\n", add.regno,
		INSN_UID (prev_add.insn), INSN_UID (add.insn));
      next = NEXT_INSN (add.insn);
    }
  else
    {
      // Emit after the current insn, so that it will be picked
      // up as next valid Add insn.
      next = emit_move_ccc_after (add.dest, plus, add.insn);
      avr_dump (";; #1 new Add[%d] insn %d after %d: R%d += %d\n\n",
		add.regno, INSN_UID (next), INSN_UID (add.insn),
		add.regno, (int) INTVAL (XEXP (plus, 1)));
      gcc_assert (GET_CODE (plus) == PLUS);
    }

  add.set_deleted ();
  prev_add.set_deleted ();

  return next;
}

/* Try to combine a PLUS of the address register with a load or store insn.
   If ADD is valid, then it precedes MEM in the same basic block.
   When a replacement is found, a new insn is emitted and the old insns
   are pseudo-deleted.  The returned insn is the point where the calling
   scanner should continue.  When no replacement is found, nullptr is
   returned and nothing changed.  */

rtx_insn *
avr_pass_fuse_add::fuse_add_mem (Add_Insn &add, Mem_Insn &mem)
{
  if (! add.valid ()
      || reg_seen_between_p (add.dest, add.insn, mem.insn))
    {
      // If something is between the Add and the current insn, we can
      // set the Add invalid to speed future scans.
      return add.insn = nullptr;
    }

  AVR_LdSt_Props ap { mem };

  int msize = GET_MODE_SIZE (mem.mode);

  // The mem insn really wants PRE_DEC.
  bool case1 = ((mem.addr_code == REG || mem.addr_code == POST_INC)
		&& msize > 1 && ap.want_predec && ! ap.has_ldd);

  // The offset can be consumed by a PRE_DEC.
  bool case2 = (- INTVAL (add.addend) == msize
		&& (mem.addr_code == REG || mem.addr_code == POST_INC)
		&& ap.has_predec && ! ap.want_postinc);

  if (! case1 && ! case2)
    return nullptr;

  // Change from REG or POST_INC to PRE_DEC.
  rtx xmem = change_address (mem.mem, mem.mode,
			     gen_rtx_PRE_DEC (Pmode, mem.addr_reg));
  rtx dest = mem.store_p ? xmem : mem.reg_or_0;
  rtx src  = mem.store_p ? mem.reg_or_0 : xmem;

  rtx_insn *next = emit_move_ccc_after (dest, src, mem.insn);
  add_reg_note (next, REG_INC, mem.addr_reg);
  avr_dump (";; new Mem[%d] insn %d after %d: %r = %r\n\n", mem.addr_regno,
	    INSN_UID (next), INSN_UID (mem.insn), dest, src);

  // Changing REG or POST_INC -> PRE_DEC means that the addend before
  // the memory access must be increased by the size of the access,
  rtx plus = plus_constant (Pmode, add.src, msize);
  if (! REG_P (plus))
    {
      rtx_insn *insn = emit_move_ccc_after (add.dest, plus, add.insn);
      avr_dump (";; #2 new Add[%d] insn %d after %d: R%d += %d\n\n",
		add.regno, INSN_UID (insn), INSN_UID (add.insn),
		add.regno, (int) INTVAL (XEXP (plus, 1)));
      gcc_assert (GET_CODE (plus) == PLUS);
    }
  else
    avr_dump (";; Add[%d] insn %d consumed into %d\n\n",
	      add.regno, INSN_UID (add.insn), INSN_UID (next));

  // Changing POST_INC -> PRE_DEC means that the addend after the mem has to be
  // the size of the access.  The hope is that this new add insn may be unused.
  if (mem.addr_code == POST_INC)
    {
      plus = plus_constant (Pmode, add.dest, msize);
      rtx_insn *next2 = emit_move_ccc_after (add.dest, plus, next);
      avr_dump (";; #3 new Add[%d] insn %d after %d: R%d += %d\n\n", add.regno,
		INSN_UID (next2), INSN_UID (next), add.regno, msize);
      next = next2;
    }

  add.set_deleted ();
  mem.set_deleted ();

  return next;
}

/* Try to combine a load or store insn with a PLUS of the address register.
   If MEM is valid, then it precedes ADD in the same basic block.
   When a replacement is found, a new insn is emitted and the old insns
   are pseudo-deleted.  The returned insn is the point where the calling
   scanner should continue.  When no replacement is found, nullptr is
   returned and nothing changed.  */

rtx_insn *
avr_pass_fuse_add::fuse_mem_add (Mem_Insn &mem, Add_Insn &add)
{
  if (! mem.valid ()
      || reg_seen_between_p (add.dest, mem.insn, add.insn))
    {
      // If something is between the Mem and the current insn, we can
      // set the Mem invalid to speed future scans.
      return mem.insn = nullptr;
    }

  AVR_LdSt_Props ap { mem };

  int msize = GET_MODE_SIZE (mem.mode);

  // The add insn can be consumed by a POST_INC.
  bool case1 = (mem.addr_code == REG
		&& INTVAL (add.addend) == msize
		&& ap.has_postinc && ! ap.want_predec);

  // There are cases where even a partial consumption of the offset is better.
  // This are the cases where no LD+offset addressing is available, because
  // the address register is obviously used after the mem insn, and a mem insn
  // with REG addressing mode will have to restore the address.
  bool case2 = (mem.addr_code == REG
		&& msize > 1 && ap.want_postinc && ! ap.has_ldd);

  if (! case1 && ! case2)
    return nullptr;

  // Change addressing mode from REG to POST_INC.
  rtx xmem = change_address (mem.mem, mem.mode,
			     gen_rtx_POST_INC (Pmode, mem.addr_reg));
  rtx dest = mem.store_p ? xmem : mem.reg_or_0;
  rtx src  = mem.store_p ? mem.reg_or_0 : xmem;

  rtx_insn *insn = emit_move_ccc_after (dest, src, mem.insn);
  add_reg_note (insn, REG_INC, mem.addr_reg);
  avr_dump (";; new Mem[%d] insn %d after %d: %r = %r\n\n", add.regno,
	    INSN_UID (insn), INSN_UID (mem.insn), dest, src);

  rtx_insn *next = NEXT_INSN (add.insn);

  // Changing REG -> POST_INC means that the post addend must be
  // decreased by the size of the access.
  rtx plus = plus_constant (Pmode, add.src, -msize);
  if (! REG_P (plus))
    {
      next = emit_move_ccc_after (mem.addr_reg, plus, add.insn);
      avr_dump (";; #4 new Add[%d] insn %d after %d: R%d += %d\n\n",
		add.regno, INSN_UID (next), INSN_UID (add.insn),
		add.regno, (int) INTVAL (XEXP (plus, 1)));
      gcc_assert (GET_CODE (plus) == PLUS);
    }
  else
    avr_dump (";; Add[%d] insn %d consumed into %d\n\n",
	      add.regno, INSN_UID (add.insn), INSN_UID (insn));

  add.set_deleted ();
  mem.set_deleted ();

  return next;
}

/* Try to post-reload combine PLUS with CONST_INt of pointer registers with:
   - Sets to a constant address.
   - PLUS insn of that kind.
   - Indirect loads and stores.
   In almost all cases, combine opportunities arise from the preparation
   done by `avr_split_fake_addressing_move', but in some rare cases combinations
   are found for the ordinary cores, too.
      As we consider at most one Mem insn per try, there may still be missed
   optimizations like  POST_INC + PLUS + POST_INC  might be performed
   as  PRE_DEC + PRE_DEC  for two adjacent locations.  */

unsigned int
avr_pass_fuse_add::execute (function *func)
{
  df_note_add_problem ();
  df_analyze ();

  int n_add = 0, n_mem = 0, n_ldi = 0;
  basic_block bb;

  FOR_EACH_BB_FN (bb, func)
    {
      Ldi_Insn prev_ldi_insns[REG_32];
      Add_Insn prev_add_insns[REG_32];
      Mem_Insn prev_mem_insns[REG_32];
      rtx_insn *insn, *curr;

      avr_dump ("\n;; basic block %d\n\n", bb->index);

      FOR_BB_INSNS_SAFE (bb, insn, curr)
	{
	  rtx_insn *next = nullptr;
	  Ldi_Insn ldi_insn { insn };
	  Add_Insn add_insn { insn };
	  Mem_Insn mem_insn { insn };

	  if (add_insn.valid ())
	    {
	      // Found reg:HI += const_int
	      avr_dump (";; insn %d: Add[%d]: R%d += %d\n\n",
			INSN_UID (add_insn.insn), add_insn.regno,
			add_insn.regno, (int) INTVAL (add_insn.addend));
	      Ldi_Insn &prev_ldi_insn = prev_ldi_insns[add_insn.regno];
	      Add_Insn &prev_add_insn = prev_add_insns[add_insn.regno];
	      Mem_Insn &prev_mem_insn = prev_mem_insns[add_insn.regno];
	      if ((next = fuse_ldi_add (prev_ldi_insn, add_insn)))
		curr = next, n_ldi += 1;
	      else if ((next = fuse_add_add (prev_add_insn, add_insn)))
		curr = next, n_add += 1;
	      else if ((next = fuse_mem_add (prev_mem_insn, add_insn)))
		curr = next, n_mem += 1;
	      else
		prev_add_insn = add_insn;
	    }
	  else if (mem_insn.valid ())
	    {
	      int addr_regno = REGNO (mem_insn.addr_reg);
	      avr_dump (";; insn %d: Mem[%d]: %r = %r\n\n",
			INSN_UID (mem_insn.insn), addr_regno,
			mem_insn.dest, mem_insn.src);
	      Add_Insn &prev_add_insn = prev_add_insns[addr_regno];
	      if ((next = fuse_add_mem (prev_add_insn, mem_insn)))
		curr = next, n_mem += 1;
	      else
		prev_mem_insns[addr_regno] = mem_insn;
	    }
	  else if (ldi_insn.valid ())
	    {
	      if (! CONST_INT_P (ldi_insn.src))
		avr_dump (";; insn %d: Ldi[%d]: R%d = %r\n\n",
			  INSN_UID (ldi_insn.insn), ldi_insn.regno,
			  ldi_insn.regno, ldi_insn.src);
	      prev_ldi_insns[ldi_insn.regno] = ldi_insn;
	    }
	} // for insns
    } // for BBs

  avr_dump (";; Function %f: Found %d changes: %d ldi, %d add, %d mem.\n",
	    n_ldi + n_add + n_mem, n_ldi, n_add, n_mem);

  return 0;
}



//////////////////////////////////////////////////////////////////////////////
// Determine whether an ISR may use the __gcc_isr pseudo-instruction.

static const pass_data avr_pass_data_pre_proep =
{
  RTL_PASS,	    // type
  "",		    // name (will be patched)
  OPTGROUP_NONE,    // optinfo_flags
  TV_DF_SCAN,	    // tv_id
  0,		    // properties_required
  0,		    // properties_provided
  0,		    // properties_destroyed
  0,		    // todo_flags_start
  0		    // todo_flags_finish
};

class avr_pass_pre_proep : public rtl_opt_pass
{
public:
  avr_pass_pre_proep (gcc::context *ctxt, const char *name)
    : rtl_opt_pass (avr_pass_data_pre_proep, ctxt)
  {
    this->name = name;
  }

  void compute_maybe_gasisr (function *);

  unsigned int execute (function *fun) final override
  {
    if (avr_gasisr_prologues
	// Whether this function is an ISR worth scanning at all.
	&& !fun->machine->is_no_gccisr
	&& (fun->machine->is_interrupt
	    || fun->machine->is_signal)
	&& !cfun->machine->is_naked
	// Paranoia: Non-local gotos and labels that might escape.
	&& !cfun->calls_setjmp
	&& !cfun->has_nonlocal_label
	&& !cfun->has_forced_label_in_static)
      {
	compute_maybe_gasisr (fun);
      }

    return 0;
  }

}; // avr_pass_pre_proep


/* Set fun->machine->gasisr.maybe provided we don't find anything that
   prohibits GAS generating parts of ISR prologues / epilogues for us.  */

void
avr_pass_pre_proep::compute_maybe_gasisr (function *fun)
{
  // Don't use BB iterators so that we see JUMP_TABLE_DATA.

  for (rtx_insn *insn = get_insns (); insn; insn = NEXT_INSN (insn))
    {
      // Transparent calls always use [R]CALL and are filtered out by GAS.
      // ISRs don't use -mcall-prologues, hence what remains to be filtered
      // out are open coded (tail) calls.

      if (CALL_P (insn))
	return;

      // __tablejump2__ clobbers something and is targeted by JMP so
      // that GAS won't see its usage.

      if (AVR_HAVE_JMP_CALL
	  && JUMP_TABLE_DATA_P (insn))
	return;

      // Non-local gotos not seen in *FUN.

      if (JUMP_P (insn)
	  && find_reg_note (insn, REG_NON_LOCAL_GOTO, NULL_RTX))
	return;
    }

  fun->machine->gasisr.maybe = 1;
}



//////////////////////////////////////////////////////////////////////////////
// Late recomputation of notes so we can use `reg_unused_after()' and friends.

static const pass_data avr_pass_data_recompute_notes =
{
  RTL_PASS,      // type
  "",            // name (will be patched)
  OPTGROUP_NONE, // optinfo_flags
  TV_DF_SCAN,    // tv_id
  0,             // properties_required
  0,             // properties_provided
  0,             // properties_destroyed
  0,             // todo_flags_start
  TODO_df_finish | TODO_df_verify // todo_flags_finish
};

class avr_pass_recompute_notes : public rtl_opt_pass
{
public:
  avr_pass_recompute_notes (gcc::context *ctxt, const char *name)
    : rtl_opt_pass (avr_pass_data_recompute_notes, ctxt)
  {
    this->name = name;
  }

  unsigned int execute (function *) final override
  {
    df_note_add_problem ();
    df_analyze ();

    return 0;
  }
}; // avr_pass_recompute_notes

} // anonymous namespace



//////////////////////////////////////////////////////////////////////////////
// Function visible and used outside this module.

/* During reload, we allow much more addresses than Reduced Tiny actually
   supports.  Split them after reload in order to get closer to the
   core's capabilities.  This sets the stage for pass .avr-fuse-add.  */

bool
avr_split_fake_addressing_move (rtx_insn * /*insn*/, rtx *xop)
{
  bool store_p = false;
  rtx mem, reg_or_0;

  if (REG_P (xop[0]) && MEM_P (xop[1]))
    {
      reg_or_0 = xop[0];
      mem = xop[1];
    }
  else if (MEM_P (xop[0])
	   && (REG_P (xop[1])
	       || xop[1] == CONST0_RTX (GET_MODE (xop[0]))))
    {
      mem = xop[0];
      reg_or_0 = xop[1];
      store_p = true;
    }
  else
    return false;

  machine_mode mode = GET_MODE (mem);
  rtx base, addr = XEXP (mem, 0);
  rtx_code addr_code = GET_CODE (addr);

  if (REG_P (reg_or_0)
      && reg_overlap_mentioned_p (reg_or_0, addr))
    return false;
  else if (addr_code == PLUS || addr_code == PRE_DEC || addr_code == POST_INC)
    base = XEXP (addr, 0);
  else if (addr_code == REG)
    base = addr;
  else
    return false;

  if (REGNO (base) > REG_Z)
    return false;

  if (! AVR_TINY
      // Only keep base registers that can't do PLUS addressing.
      && ((REGNO (base) != REG_X
	   && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (mem)))
	  || avr_load_libgcc_p (mem)
	  || avr_mem_memx_p (mem)))
    return false;

  bool volatile_p = MEM_VOLATILE_P (mem);
  bool mem_volatile_p = false;
  if (frame_pointer_needed
      && REGNO (base) == FRAME_POINTER_REGNUM)
    {
      if (avr_fuse_add < 2
	  // Be a projection (we always split PLUS).
	  || (avr_fuse_add == 2 && volatile_p && addr_code != PLUS))
	return false;

      // Changing the frame pointer locally may confuse later passes
      // like .dse2 which don't track changes of FP, not even when
      // respective CFA notes are present.  An example is pr22141-1.c.
      if (avr_fuse_add == 2)
	mem_volatile_p = true;
    }

  rtx_code new_code = UNKNOWN;
  HOST_WIDE_INT add = 0, sub = 0;
  int msize = GET_MODE_SIZE (mode);

  AVR_LdSt_Props ap { (int) REGNO (base), store_p, volatile_p,
		      ADDR_SPACE_GENERIC };

  switch (addr_code)
    {
    default:
      return false;

    case PLUS:
      add = INTVAL (XEXP (addr, 1));
      if (msize == 1)
	{
	  new_code = REG;
	  sub = -add;
	}
      else if (ap.want_predec)
	{
	  // volatile stores prefer PRE_DEC (MSB first)
	  sub = -add;
	  add += msize;
	  new_code = PRE_DEC;
	}
      else
	{
	  new_code = POST_INC;
	  sub = -add - msize;
	}
      break;

    case POST_INC:
      // volatile stores prefer PRE_DEC (MSB first)
      if (msize > 1 && ap.want_predec)
	{
	  add = msize;
	  new_code = PRE_DEC;
	  sub = msize;
	  break;
	}
      return false;

    case PRE_DEC:
      // volatile loads prefer POST_INC (LSB first)
      if (msize > 1 && ap.want_postinc)
	{
	  add = -msize;
	  new_code = POST_INC;
	  sub = -msize;
	  break;
	}
      return false;

    case REG:
      if (msize == 1)
	return false;

      if (ap.want_predec)
	{
	  add = msize;
	  new_code = PRE_DEC;
	  sub = 0;
	}
      else
	{
	  add = 0;
	  new_code = POST_INC;
	  sub = -msize;
	}
      break;
    } // switch addr_code

  rtx_insn *insn;

  if (add)
    {
      insn = emit_move_ccc (base, plus_constant (Pmode, base, add));
      avr_maybe_adjust_cfa (insn, base, add);
    }

  rtx new_addr = new_code == REG
    ? base
    : gen_rtx_fmt_e (new_code, Pmode, base);

  rtx new_mem = change_address (mem, mode, new_addr);
  if (mem_volatile_p)
    MEM_VOLATILE_P (new_mem) = 1;

  insn = emit_move_ccc (store_p ? new_mem : reg_or_0,
			store_p ? reg_or_0 : new_mem);
  if (auto_inc_p (new_addr))
    {
      add_reg_note (insn, REG_INC, base);
      int off = new_code == POST_INC ? msize : -msize;
      avr_maybe_adjust_cfa (insn, base, off);
    }

  if (sub)
    {
      insn = emit_move_ccc (base, plus_constant (Pmode, base, sub));
      avr_maybe_adjust_cfa (insn, base, sub);
    }

  return true;
}



// Functions  make_<pass-name> (gcc::context*)  where <pass-name> is
// according to the pass declaration in avr-passes.def.  GCC's pass
// manager uses these function to create the respective pass object.

// Optimize results of the casesi expander for modes < SImode.

rtl_opt_pass *
make_avr_pass_casesi (gcc::context *ctxt)
{
  return new avr_pass_casesi (ctxt, "avr-casesi");
}

// Try to replace 2 cbranch insns with 1 comparison and 2 branches.

rtl_opt_pass *
make_avr_pass_ifelse (gcc::context *ctxt)
{
  return new avr_pass_ifelse (ctxt, "avr-ifelse");
}

// Determine whether an ISR may use the __gcc_isr pseudo-instruction.

rtl_opt_pass *
make_avr_pass_pre_proep (gcc::context *ctxt)
{
  return new avr_pass_pre_proep (ctxt, "avr-pre-proep");
}

// Find more POST_INC and PRE_DEC cases.

rtl_opt_pass *
make_avr_pass_fuse_add (gcc::context *ctxt)
{
  return new avr_pass_fuse_add (ctxt, "avr-fuse-add");
}

// Late recomputation of notes so we can use `reg_unused_after()' and friends.

rtl_opt_pass *
make_avr_pass_recompute_notes (gcc::context *ctxt)
{
  return new avr_pass_recompute_notes (ctxt, "avr-notes-free-cfg");
}

// Optimize moves after reload.

rtl_opt_pass *
make_avr_pass_fuse_move (gcc::context *ctxt)
{
  return new avr_pass_fuse_move (ctxt, "avr-fuse-move");
}