aboutsummaryrefslogtreecommitdiffstats
path: root/gcc-4.4.0/gcc/tree-ssa-lrs.c
blob: bd5050a4e8f6d8f69df16a0d4bbb6ced4d5de7b3 (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
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
/* Live Range Shrinking Optimizations to reduce register pressure. 
   Copyright (C) 2009 Free Software Foundation, Inc.
   Contributed by Xinliang (David) Li davidxl@google.com

This file is part of GCC.

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

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

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

#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
#include "errors.h"
#include "ggc.h"
#include "tree.h"
#include "basic-block.h"
#include "diagnostic.h"
#include "tree-inline.h"
#include "tree-flow.h"
#include "gimple.h"
#include "tree-dump.h"
#include "timevar.h"
#include "tree-iterator.h"
#include "tree-pass.h"
#include "alloc-pool.h"
#include "vec.h"
#include "langhooks.h"
#include "pointer-set.h"
#include "cfgloop.h"
#include "flags.h"
#include "params.h"
#include "dbgcnt.h"
#include "tm_p.h"
#include "regs.h"
#include "ira-int.h"


/* Live range shrink transformation (LRS)

   High register pressures can result from many optimizations 
   aggressive function inlining, loop unrolling, loop fusion,
   strength reduction, expression reassociation, partitial 
   redundancy elimination, etc. The following example illustrates
   how expression reassociation can increase register pressure.

   x = ...
   y = ...
   a = x + y;
   u = ...
   b = a + u;
   v = ...
   c = b + v;
   w = ...
   d = c + w; (1)

 ==> After reasociation:

    x = ...
    y = ...
    u = ...
    v = ...
    w = ...
    a = y + x;
    b = a + v;
    c = b + u;
    d = c + w;

    Assuming only d is live across the statement (1), the max
    simultaneous live variables is 2 for the orignal code
    sequence (thus requires only 2 registers), while after
    reassocation, the number of regisers required is 5.

    The live range shrinking optimization tries to move uses
    as close as possible to its definitions if the number of
    overlapping live ranges can be reduced after the code motion.
    The techniques implemented in this pass include the following:

    1) Large expression reassociation: The objective of the 
       reassication is to enable more live range shrinking code
       motions which are otherwise blocked due to data dependences.
       See comments for function 'do_lrs_reassoc' for detailed 
       descriptions.
    2) Upward code motion: scheduling last uses of LRs can shrink
       and potential reduce the number of overlapping live ranges.
       See comments for function 'get_reg_pressure_change_if_swapped'
       for details. 
    3) Expression tree scheduling: this is a downward code motion pass.
       The objective of this transformation is to make sure the subtrees
       of an expression tree are evaluated sequentially --- i.e., the
       evaluation of one subtree won't start until the previous subtree
       is complete. The evaluation order of the subtrees are determined 
       by the number of temporary registers needed. 
    4) Multi-use expression downward code motion: the objective is move
       the definition downward as close as possible to its uses  when 
       the downward motion does not cause the live range of the RHS LRs 
       to be lengthened. 

    The LRS optimization is composed of the following steps:

    1) Perform loop recognition;

    2) Process all phi statements in the function to map ssa names (
       gimple regs) to reg allocs (i.e., pseudo registers)

    3) walk through the function, and identify LRS regions (innermost
       loops or large BBs in  acyclic regions). For each region larger
       than a threshold.

         3.1) perform live use reference data flow analysis;
         3.2) compute register pressure for the region. If the pressure
              is high;
         3.3) perform a round of expression reassociation to enable more
              LRS opportunities;
         3.4) perform upward code motion to shrink live ranges;
         3.5) perform virtual variable reaching definition data flow analysis;
         3.6) perform downward code motion including subtree scheduling to
              shrink live ranges.
         3.7) perform downward code motion of expression tree with multiple uses.
*/


/* Canonicalized statement used in live range shrinking.  */

typedef struct lrs_stmt
{
  gimple  stmt;
  tree    def;
  tree  **uses;
  size_t  num_uses;
} *lrs_stmt_p;

/* The expression tree representation used in
   live range shinking (downward motion phase). The
   tree is formed by following single use/single def
   chain.  */

typedef struct lrs_tree
{
  /* The gimple statement that defines the root node
     of the expression tree.  */
  gimple root;
  /* The vector of gimple statements that define the inner
     nodes of the expression tree. The statements are in
     the scheduled order in the IL.  */
  VEC(gimple, heap) *tree_nodes;
  /* The vector of values that assoicated with leaf nodes
     of the expression tree.  */
  VEC(tree, heap) *leaf_nodes;
  /* The number of leaf nodes (ssa names) that are not live
     across the root gimple statement.  */
  int num_leaves_not_live_across;
  /* The number of leaves that are live across the root.  */
  int num_leaves_live_across;
  /* The max number of temporary LRs that are needed to evaluate
     this expression tree.  */
  int num_temp_lrs;
} *lrs_tree_p;


typedef tree *treep;
DEF_VEC_P(treep);
DEF_VEC_ALLOC_P(treep, heap);

/* Tree representation for expression trees that are candidates
   for reassociation enabling opportunities for live range shrinking.  */

typedef struct lrs_reassoc_tree
{
  gimple root;
  VEC(gimple, heap) *inner_nodes;
  VEC(tree, heap) *leaf_operands;
  VEC(treep, heap) *use_ref_slots;
  enum tree_code opc;
} *lrs_reassoc_tree_p;

DEF_VEC_P(lrs_reassoc_tree_p);
DEF_VEC_ALLOC_P(lrs_reassoc_tree_p, heap);

/* Enum for register classes.  The partition is an approximation
   at high level and is target independent.  */

enum lrs_reg_class
{
  /* general register class.  */
  lrc_gr,
  /* floating pointer register class.  */
  lrc_fr,
  lrc_num
};

/* The equivalent class of ssa names that need to be allocated 
   to the same register.  */

typedef struct reg_alloc
{
  /* Members (ssa names) of the reg_alloc.  */
  VEC(tree, heap) *members;
  /* reg alloc parent to which this reg alloc is joined.  */
  struct reg_alloc *parent;
} *reg_alloc_p;

/* This data structure is used to represent the code region
   that is selected for live range shrinking transformation. 
   In this implementation, only two types of regions are 
   supported: 1) inner most natural loop; 2) single basic block
   with size larger than a threshold.  */

typedef struct lrs_region
{
  /* The single entry to the region.  */
  basic_block entry;
  /* The array of exit bbs in the region. An exit bb is a bb 
     in the region with an out edge leaving the region.  */
  basic_block *exits;
  /* The region body as an array of basic block pointers. The
     index of the BB in the array is the id of the BB in the 
     region.  */
  basic_block *body;
  size_t num_bbs;
  size_t num_exits;
  size_t num_stmts;
  /* The width of the bitvector for the use-ref data flow problem.  */
  size_t bitvec_width;
  /* The size (in the number of gimple statements) of the largest
     bb in the region.  */
  size_t max_bb_size;

  /* The mapping from bb address to region index.  */
  struct pointer_map_t *bb_to_idx_map;

  /* Bitvector (live use-ref problem) arrays. Each 
     array is indexed by the region id of a BB.  */
  sbitmap *bb_use_ref_out_sets;
  sbitmap *bb_use_ref_in_sets;
  sbitmap *bb_use_ref_kill_sets;
  sbitmap *bb_use_ref_gen_sets;

  /* The array of bitvectors representing use-refs that are live
     across gimple statements. The array is indexed by gimple 
     statement id in the region. The gimple statement id is stored
     in the uid field of the gimple statement.  */
  sbitmap *across_stmt_use_ref_sets;
  /* The array of normalized statements. The array is indexed by
     the gimple statement id in the region.  */
  lrs_stmt_p normalized_stmts;

  /* The vector of ssa names that referenced in the region.  */
  VEC(tree, heap) *refed_names;
  /* The set of ssa names that are live (referenced) out of region.  */
  sbitmap region_live_out_names;

  /* The map from SSA names to position range in bitvectors.  */
  struct pointer_map_t *bit_range_map;
  /* The map from SSA names to the bitmask associated with name uses.  */
  struct pointer_map_t *def_bitmask_map;
  /* The map from use references to bit positions in bitvectors  .*/
  struct pointer_map_t *bit_pos_map;

  /* The vector of virtual variables that are referenced in the region.  */
  VEC(tree, heap) *refed_vvars;
  /* The vector of ssa names of the virtual variables.  */
  VEC(tree, heap) *refed_vvar_names;
  /* The map from virtual variable names to the bitvector (
     reaching def problem) bit positions.  */
  struct pointer_map_t *vname_bit_pos_map;
  /* The map from virtual variables to the bitvector (
     reaching def problem) bit position ranges.  */
  struct pointer_map_t *vvar_bit_range_map;

  /* Bitvector (reaching def problem) arrays. Each 
     array is indexed by the region id of a BB.  */
  sbitmap *bb_rd_in_sets;
  sbitmap *bb_rd_out_sets;
  sbitmap *bb_rd_kill_sets;
  sbitmap *bb_rd_gen_sets;

  /* Reach def bitvector array for gimple statements
     in the region.  */
  sbitmap *stmt_rd_sets;
  /* Map from a gimple statement to a lrs_tree that is
     rooted at the statement.  */
  struct pointer_map_t *gimple_to_lrs_tree_map;
  /* Memory pool for lrs_tree objects.  */
  alloc_pool lrs_tree_pool;

  /* Vector of created lrs_reassoc_tree  */
  VEC(lrs_reassoc_tree_p, heap) *lrs_reassoc_trees;
  /* Memory pool for lrs_reassoc_tree objects.  */
  alloc_pool lrs_reassoc_tree_pool;

  /* The number of available registers for each register
     class.  */
  size_t available_regs[lrc_num];
  /* Computed register pressure for this region.  */
  size_t reg_pressure[lrc_num];
  /* Computed register pressure for each BB in the region.  */
  size_t *bb_reg_pressures[lrc_num];

} *lrs_region_p;


#define REG_CLASS_MAP(rc) ira_class_translate[(rc)]

/* Statement order hashmap. The order information
   is used in rank compuation and code motion.  */
static struct pointer_map_t *stmt_order = NULL;
/* The map from ssa namees to the indices of the
   reg allocs.  */
static struct pointer_map_t *reg_alloc_map = NULL;
/* The array of reg allocs.  */
static VEC(tree, heap) **reg_allocs = NULL;
static size_t num_reg_allocs = 0;
/* The map from ssa names to the associated reg allocs.  */
static struct pointer_map_t *tmp_reg_alloc_map = NULL;
static alloc_pool tmp_reg_alloc_pool = NULL;
static size_t reg_pressure_control_min_bb_size = 0;

struct pending_negates
{
  VEC(tree, heap) *opnds_to_negate;
  VEC(gimple, heap) *stmts_to_fixup;
};
static struct pending_negates pending_negates = {NULL, NULL};

static size_t get_stmt_order (const_gimple);
static void destroy_region (lrs_region_p region);
static void dump_reg_allocs (FILE *file);
static void print_lrs_tree (FILE *file, lrs_tree_p);
static void print_lrs_reassoc_tree (FILE *file, lrs_reassoc_tree_p);

/* A helper function to determine if a code motion phase is needed
   for BB after expression reassociation to reduce register pressure.
   1. It returns false if --param ctrl-repre=0;
   2. It returns true if --param ctrl-regpre=2;
   3. If the parameter value is 1 (the default), it is at the
   compiler's discretion. Currently, it returns true only when largest 
   BB (in REGION)'s size is larger than a given threshold.  */

static int
need_control_reg_pressure (lrs_region_p region)
{
  int control_reg_pressure;

  control_reg_pressure = PARAM_VALUE (PARAM_CONTROL_REG_PRESSURE);
  if (control_reg_pressure == 2)
    return 2;
  if (control_reg_pressure == 1
      && (region->max_bb_size
          > reg_pressure_control_min_bb_size))
    return 1;
  else
    return 0;
}

/* The function checks command line control parameters and returns
   true if upward code motion transformation is enabled. It returns
   false otherwise.  */

static inline bool
do_upward_motion (void)
{
  int code_motion_mode;
  code_motion_mode 
      = PARAM_VALUE (PARAM_REG_PRESSURE_CONTROL_MODE);
  return !!(code_motion_mode & 1);
}

/* The function checks command line control parameters and returns
   true if downward code motion transformation is enabled. It returns
   false otherwise.  */

static inline bool
do_downward_motion (void)
{
  int code_motion_mode;
  code_motion_mode 
      = PARAM_VALUE (PARAM_REG_PRESSURE_CONTROL_MODE);
  return !!(code_motion_mode & 0x2);
}

/* The function checks command line control parameters and returns
   true if reassociation transformation is enabled. It returns
   false otherwise.  */

static inline bool
do_reassoc (void)
{
  int code_motion_mode;
  code_motion_mode 
      = PARAM_VALUE (PARAM_REG_PRESSURE_CONTROL_MODE);
  return !!(code_motion_mode & 0x4);
}

/* The function returns the size of the basic block BB in terms
   of the number of gimple statements in it.  */

static size_t
get_bb_size (basic_block bb)
{
  gimple_stmt_iterator gsi_last;

  gsi_last = gsi_last_bb (bb);
  if (gsi_end_p (gsi_last))
    return 0;

  return get_stmt_order (gsi_stmt (gsi_last));
}

/* The function returns the unique id of the gimple STMT.  */

static inline int
get_stmt_idx_in_region (gimple stmt)
{
  return gimple_uid (stmt);
}

/* The function returns true of BB is included in REGION. If it
   returns true, *IDX is set to the region id of BB in REGION.  */

static bool
get_bb_index_in_region (basic_block bb, lrs_region_p region, int *idx)
{
  void **slot;

  /* pointer map will always return a slot for NULL key.  */
  if (bb == NULL)
    return false;
  slot = pointer_map_contains (region->bb_to_idx_map, bb);
  if (!slot)
    return false;

  *idx = (int) (size_t) *slot;
  return true;
}

/* The function returns the normalized lrs stmt for STMT in REGION.  */

static inline lrs_stmt_p
get_normalized_gimple_stmt (gimple stmt, lrs_region_p region)
{
  int id = get_stmt_idx_in_region (stmt);
  return &region->normalized_stmts[id];
}

/* The tree walk callback function for collecting use refs. *OP
   is the tree node being processed, and *DATA is the pointer to
   the vector of collected uses.  */

static tree
collect_use (tree *op,
             int *unused ATTRIBUTE_UNUSED,
             void *data)
{
  VEC(treep, heap) **stmt_uses = (VEC(treep, heap) **)data;

  if (TREE_CODE (*op) == SSA_NAME && is_gimple_reg (*op))
      VEC_safe_push (treep, heap, *stmt_uses, op);

  return 0;
}

/* The function normalizes STMT into NORM_STMT.  */

static void
normalize_gimple_stmt (gimple stmt, lrs_stmt_p norm_stmt)
{
  size_t i, n;
  VEC(treep, heap) *stmt_uses = NULL;
  tree lhs;

  norm_stmt->stmt = stmt;
  norm_stmt->uses = NULL;
  norm_stmt->def = NULL; 
  norm_stmt->num_uses = 0;

  if (gimple_code (stmt) != GIMPLE_PHI)
    {
      lhs = (gimple_num_ops (stmt)? gimple_op (stmt, 0) : 0);
      if (lhs && is_gimple_reg (lhs) && TREE_CODE (lhs) == SSA_NAME)
        norm_stmt->def = lhs;

      n = gimple_num_ops (stmt);
      for (i = 1; i < n; i++)
        {
          tree *op = gimple_op_ptr (stmt, i);
          if (!op)
            continue;
          walk_tree_without_duplicates (op, collect_use, &stmt_uses);
        }
    }
  else
    {
      lhs = gimple_phi_result (stmt);
      if (is_gimple_reg (lhs))
        {
          norm_stmt->def = lhs;

          n = gimple_phi_num_args (stmt);
          for (i = 0; i < n; i++)
            {
              tree *arg = gimple_phi_arg_def_ptr (stmt, i);
              if (TREE_CODE (*arg) == SSA_NAME && is_gimple_reg (*arg))
                VEC_safe_push (treep, heap, stmt_uses, arg);
            }
        }
    }

  n = norm_stmt->num_uses = VEC_length (treep, stmt_uses);
  if (n)
    {
      norm_stmt->uses = XNEWVEC (treep, n);
      memcpy (norm_stmt->uses, VEC_address (treep, stmt_uses),
              n * sizeof(treep));
    }
  VEC_free (treep, heap, stmt_uses);
}

/* The function normalizes all statements in REGION.  */

static void
normalize_gimple_stmts (lrs_region_p region)
{
  size_t i;

  region->normalized_stmts = XNEWVEC (struct lrs_stmt, region->num_stmts);

  for (i = 0; i < region->num_bbs; i++)
    {
      basic_block bb;
      gimple_stmt_iterator gsi;

      bb = region->body[i];

      for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
        {
          int sidx;
          gimple stmt = gsi_stmt (gsi);
          sidx = get_stmt_idx_in_region(stmt);
          normalize_gimple_stmt (stmt, &region->normalized_stmts[sidx]);
        }
      for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
        {
          int sidx;
          gimple stmt = gsi_stmt (gsi);
          sidx = get_stmt_idx_in_region(stmt);
          normalize_gimple_stmt (stmt, &region->normalized_stmts[sidx]);
        }
    }
}

/* The function initializes data members of REGION.  */

static void
init_region (lrs_region_p region)
{
  region->entry = NULL;
  region->exits = NULL;
  region->body = NULL;
  region->num_bbs = 0;
  region->num_exits = 0;
  region->max_bb_size = 0;
  region->num_stmts = 0;
  region->bitvec_width = 0;
  region->bb_to_idx_map = NULL;
  region->bb_use_ref_out_sets = NULL;
  region->bb_use_ref_in_sets = NULL;
  region->bb_use_ref_kill_sets = NULL;
  region->bb_use_ref_gen_sets = NULL;
  region->across_stmt_use_ref_sets = NULL;
  region->normalized_stmts = NULL;
  region->refed_names = NULL;
  region->refed_vvars = NULL;
  region->refed_vvar_names = NULL;
  region->region_live_out_names = NULL;
  region->bit_range_map = NULL;
  region->def_bitmask_map = NULL;
  region->bit_pos_map = NULL;
  region->vname_bit_pos_map = NULL;
  region->vvar_bit_range_map = NULL;
  region->gimple_to_lrs_tree_map = NULL;
  region->lrs_tree_pool = NULL;
  region->lrs_reassoc_tree_pool = NULL;
  region->lrs_reassoc_trees = NULL;
  region->bb_rd_out_sets = NULL;
  region->bb_rd_in_sets = NULL;
  region->bb_rd_kill_sets = NULL;
  region->bb_rd_gen_sets = NULL;
  region->stmt_rd_sets = NULL;
  region->bb_reg_pressures[lrc_gr] = NULL;
  region->bb_reg_pressures[lrc_fr] = NULL;
}

/* The function returns true if BB is the head of a candidate
   region for live range shrinking optimization. Only two types
   of candidates are considered: inner most loop and single BB
   larger than a threshold and that is not in an inner loop. If
   BB is the region head of a loop, *THE_LOOP is set to that loop.  */

static bool
is_region_head (basic_block bb, struct loop **the_loop)
{
  struct loop *loop;

  loop = bb->loop_father;

  if (loop_depth (loop) >= 1 && !loop->inner)
    {
      if (loop->header == bb)
        {
          *the_loop = loop;
          return true;
        }
      else
        return false;
    }
  else
    {
      *the_loop = NULL;
      if (get_bb_size (bb)
          > (size_t) PARAM_VALUE (PARAM_REG_PRESSURE_MIN_BB_FACTOR))
        return true;
      else
        return false;
    }
}

/* The function contructs and returns the pointer to the region
   if BB is a region head.  */

static lrs_region_p
form_region (basic_block bb)
{
  struct loop *loop = NULL;
  struct lrs_region *region;
  size_t i, s = 0, nstmts;

  if (!is_region_head (bb, &loop))
    return NULL;

  region = XNEW (struct lrs_region);
  init_region (region);
  if (loop)
    {
      region->entry = loop->header;
      region->num_bbs = loop->num_nodes;
      region->body = get_loop_body (loop);
    }
  else
    {
      region->entry = bb;
      region->num_bbs = 1;
      region->body = XNEW (basic_block);
      region->body[0] = bb;
    }

  region->bb_to_idx_map = pointer_map_create ();
  s = 5;
  region->exits = XNEWVEC (basic_block, s);
  region->num_exits = 0;
  nstmts = 0;

  for (i = 0; i < region->num_bbs; i++)
    {
      edge e;
      edge_iterator ei;
      basic_block bb;
      gimple_stmt_iterator gsi;
      void **slot;
      size_t sz;

      bb = region->body[i];
      slot = pointer_map_insert (region->bb_to_idx_map, bb);
      *slot = (void*) i;
      sz = get_bb_size (bb);
      if (sz > region->max_bb_size)
        region->max_bb_size = sz;

      for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
        {
          gimple stmt = gsi_stmt (gsi);
          gimple_set_uid (stmt, nstmts++);
        }

      for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
        {
          gimple stmt = gsi_stmt (gsi);
          gimple_set_uid (stmt, nstmts++);
        }

      if (loop)
        {
          FOR_EACH_EDGE (e, ei, bb->succs)
            {
              if (!flow_bb_inside_loop_p (loop, e->dest))
                {
                  if (region->num_exits >= s)
                    {
                      s += s;
                      region->exits
                          = XRESIZEVEC (basic_block, region->exits, s);
                    }
                  region->exits[region->num_exits] = bb;
                  region->num_exits++;
                }
            }
        }
      else
        {
          region->exits = XNEW (basic_block);
          region->exits[0] = bb;
          region->num_exits = 1;
        }
    }

  if (!need_control_reg_pressure (region))
    {
      if (dump_file)
        fprintf (dump_file,
                 "region (Entry BB# %d) is skipped: "
                 "region max bb size = %d, min_size = %d!\n,",
                 region->entry->index, region->max_bb_size,
                 (int)reg_pressure_control_min_bb_size);
      destroy_region (region);
      return NULL;
    }

  region->num_stmts = nstmts;
  normalize_gimple_stmts (region);

  return region;
}

/* The function is used as the callback method in pointer map
   traversal. It destroys the bitmasks in the bitmask map.  */

static bool
destroy_def_bitmask (const void *key ATTRIBUTE_UNUSED, 
                     void **value,
                     void *data ATTRIBUTE_UNUSED)
{
  sbitmap mask;

  gcc_assert (*value);
  mask = (sbitmap)*value;
  sbitmap_free (mask);
  return true;
}

/* The function is used to destroy the definition bitmask map in REGION.  */

static inline void
destroy_def_bitmask_map (lrs_region_p region)
{
  pointer_map_traverse (region->def_bitmask_map, 
                        destroy_def_bitmask,  NULL);
  pointer_map_destroy (region->def_bitmask_map);
}

/* The function detroys normalized statements in REGION.  */

static void
destroy_normalized_stmts (lrs_region_p region)
{
  size_t i;

  for (i = 0; i < region->num_stmts; i++)
  {
    lrs_stmt_p nstmt = &region->normalized_stmts[i];
    if (nstmt->uses)
      free (nstmt->uses);
  }

  free (region->normalized_stmts);
}

/* The function is the callback function in the pointer map traversal
   to destroy the lrs trees.  */

static bool
destroy_lrs_tree (const void *key ATTRIBUTE_UNUSED, void **value,
                  void *data ATTRIBUTE_UNUSED)
{
  lrs_tree_p lrs_tree;

  lrs_tree = (lrs_tree_p)*value;
  VEC_free (gimple, heap, lrs_tree->tree_nodes);
  VEC_free (tree, heap, lrs_tree->leaf_nodes);
  return true;
}

/* The function is used to destroy the lrs tree map and the lrs tree
   memory pool in REGION.  */

static void
destroy_lrs_tree_pool (lrs_region_p region)
{
  if (region->gimple_to_lrs_tree_map)
    {
      pointer_map_traverse (region->gimple_to_lrs_tree_map,
                            destroy_lrs_tree, NULL);
      pointer_map_destroy (region->gimple_to_lrs_tree_map);
    }
  if (region->lrs_tree_pool)
    free_alloc_pool (region->lrs_tree_pool);
}

/* The function is used to destroy a lrs_reassoc_tree REASSOC_TREE.  */

static void
destroy_lrs_reassoc_tree (lrs_reassoc_tree_p reassoc_tree)
{
  VEC_free (gimple, heap, reassoc_tree->inner_nodes);
  VEC_free (tree, heap, reassoc_tree->leaf_operands);
  VEC_free (treep, heap, reassoc_tree->use_ref_slots);
}

/* The function destroys the memory pool for lrs reassociation trees.  */

static void
destroy_lrs_reassoc_tree_pool (lrs_region_p region)
{
  if (region->lrs_reassoc_trees)
    {
      int i = 0;
      int n = VEC_length (lrs_reassoc_tree_p, region->lrs_reassoc_trees);
      for (i = 0; i < n; i++)
	destroy_lrs_reassoc_tree (VEC_index (lrs_reassoc_tree_p,
					     region->lrs_reassoc_trees, i));
      gcc_assert (region->lrs_reassoc_tree_pool);
      free_alloc_pool (region->lrs_reassoc_tree_pool);
    }
}

/* The function reclaims memory used for live range shrinking for REGION.  */

static void
destroy_region (lrs_region_p region)
{
  size_t i;
  if (region->exits)
    free (region->exits);
  if (region->body)
    free (region->body);
  if (region->def_bitmask_map)
    destroy_def_bitmask_map (region);
  if (region->bit_range_map)
    pointer_map_destroy (region->bit_range_map);
  if (region->bit_pos_map)
    pointer_map_destroy (region->bit_pos_map);
  if (region->vname_bit_pos_map)
    pointer_map_destroy (region->vname_bit_pos_map);
  if (region->vvar_bit_range_map)
    pointer_map_destroy (region->vvar_bit_range_map);
  if (region->bb_to_idx_map)
    pointer_map_destroy (region->bb_to_idx_map);
  if (region->bb_use_ref_out_sets)
    sbitmap_vector_free (region->bb_use_ref_out_sets);
  if (region->bb_use_ref_in_sets)
    sbitmap_vector_free (region->bb_use_ref_in_sets);
  if (region->bb_use_ref_gen_sets)
    sbitmap_vector_free (region->bb_use_ref_gen_sets);
  if (region->bb_use_ref_kill_sets)
    sbitmap_vector_free (region->bb_use_ref_kill_sets);
  if (region->across_stmt_use_ref_sets)
    sbitmap_vector_free (region->across_stmt_use_ref_sets);
  if (region->region_live_out_names)
    sbitmap_free (region->region_live_out_names);
  if (region->refed_names)
    VEC_free (tree, heap, region->refed_names);
  if (region->refed_vvars)
    VEC_free (tree, heap, region->refed_vvars);
  if (region->refed_vvar_names)
    VEC_free (tree, heap, region->refed_vvar_names);
  if (region->bb_rd_out_sets)
    sbitmap_vector_free (region->bb_rd_out_sets);
  if (region->bb_rd_in_sets)
    sbitmap_vector_free (region->bb_rd_in_sets);
  if (region->bb_rd_gen_sets)
    sbitmap_vector_free (region->bb_rd_gen_sets);
  if (region->bb_rd_kill_sets)
    sbitmap_vector_free (region->bb_rd_kill_sets);
  if (region->stmt_rd_sets)
    sbitmap_vector_free (region->stmt_rd_sets);
  if (region->bb_reg_pressures[0])
    {
      for (i = 0; i < lrc_num; i++)
        free (region->bb_reg_pressures[i]);
    }
  destroy_normalized_stmts (region);

  destroy_lrs_tree_pool (region);
  destroy_lrs_reassoc_tree_pool (region);

  VEC_free (tree, heap, pending_negates.opnds_to_negate);
  VEC_free (gimple, heap, pending_negates.stmts_to_fixup);
  pending_negates.opnds_to_negate = NULL;
  pending_negates.stmts_to_fixup = NULL;

  free (region);
}

/* The function returns the root node for a union of
   reg_alloc nodes for which RA is a member.  */

static reg_alloc_p
get_reg_alloc_root (reg_alloc_p ra)
{
  reg_alloc_p ra1 = ra, ra2;

  while (ra->parent)
    ra = ra->parent;

  while (ra1->parent)
    {
      ra2 = ra1->parent;
      ra1->parent = ra;
      ra1 = ra2;
    }
  return ra;
}

/* The function joins two reg_alloc nodes RA1 and RA2, and
   returns the root node of the union.  */

static reg_alloc_p
reg_alloc_union (reg_alloc_p ra1, reg_alloc_p ra2)
{
  ra1 = get_reg_alloc_root (ra1);
  ra2 = get_reg_alloc_root (ra2);

  if (ra1 == ra2)
    return ra1;

  ra2->parent = ra1;

  return ra1;
}

/* The function joins a SSA name NM into one register allocation RA.  */

static inline void
join_reg_alloc (reg_alloc_p ra, tree nm)
{
  void **slot;
  VEC_safe_push (tree, heap, ra->members, nm);

  slot = pointer_map_insert (tmp_reg_alloc_map, nm);
  gcc_assert (!*slot);
  *slot = ra;
}

/* The function returns the representative reg_alloc node for ssa name NM.  */

static reg_alloc_p
find_reg_alloc (tree nm)
{
  void **slot;
  reg_alloc_p ra;

  slot = pointer_map_contains (tmp_reg_alloc_map, nm);
  if (!slot)
    return NULL;

  gcc_assert (*slot);
  ra = (reg_alloc_p) *slot;
  return get_reg_alloc_root (ra);
}

/* The function checks if ssa name NM has a register allocation allocated. 
   If not, it will be created and inserted into the map. The reg_alloc node
   found/created is then returned.  */

static reg_alloc_p
find_or_create_reg_alloc (tree nm)
{
  void **slot;
  reg_alloc_p ra;

  slot = pointer_map_insert (tmp_reg_alloc_map, nm);

  if (*slot)
    return get_reg_alloc_root ((reg_alloc_p)*slot);

  ra = (reg_alloc_p) pool_alloc (tmp_reg_alloc_pool);
  ra->members = NULL;
  ra->parent = NULL;
  VEC_safe_push (tree, heap, ra->members, nm);
  *slot = ra;
  return ra;
}

/* The function processes a PHI node and joins the reg_alloc
   of the phi arguments.  */

static void
compute_reg_alloc (gimple phi)
{
  size_t n, i;

  tree res = gimple_phi_result (phi);
  reg_alloc_p ra = find_or_create_reg_alloc (res);

  n = gimple_phi_num_args (phi);
  for (i = 0; i < n; i++)
    {
      reg_alloc_p arg_ra = 0;
      tree arg = gimple_phi_arg_def (phi, i);

      if (TREE_CODE (arg) != SSA_NAME)
        continue;

      arg_ra = find_reg_alloc (arg);
      if (arg_ra)
        ra = reg_alloc_union (ra, arg_ra);
      else
        join_reg_alloc (ra, arg);
    }
}

/* The function is used to process all phi statements
   in basic block BB for reg_alloc creation.  */

static void
compute_reg_allocs (basic_block bb)
{
  gimple_stmt_iterator gsi;

  for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
    {
      tree res;
      gimple phi = gsi_stmt (gsi);

      res = gimple_phi_result (phi);
      if (!is_gimple_reg (res))
        continue;

      compute_reg_alloc (phi);
    }
}

/* The function is used to move members (ssa names) of 
   a reg_alloc node to the root node of the union. The 
   member array of the transferred node is then destroyed.  */

static bool
finalize_ra_mem (const void *key ATTRIBUTE_UNUSED, void **value,
                  void *data )
{
  reg_alloc_p ra = 0;
  reg_alloc_p real_ra = 0;
  size_t i, n;
  int *c;

  c = (int *) data;

  gcc_assert (*value);
  ra = (reg_alloc_p) *value;
  if (!ra->members)
    return true;

  real_ra = get_reg_alloc_root (ra);
  if (real_ra == ra)
    {
      (*c)++ ;
      return true;
    }

  n = VEC_length (tree, ra->members);

  for (i = 0; i < n; i++)
    VEC_safe_push (tree, heap, real_ra->members,
                   VEC_index (tree, ra->members, i));

  VEC_free (tree, heap, ra->members);
  ra->members = 0;

  return true;
}

/* The function maps ssa names to their associated reg_alloc number.  */

static bool
finalize_ra_map (const void *key ATTRIBUTE_UNUSED, void **value,
                  void *data ATTRIBUTE_UNUSED)
{
  reg_alloc_p ra = 0;
  reg_alloc_p real_ra = 0;
  size_t i, n;

  gcc_assert (*value);
  ra = (reg_alloc_p) *value;

  real_ra = get_reg_alloc_root (ra);

  if (!real_ra->members)
    return true;


  n = VEC_length (tree, real_ra->members);

  for (i = 0; i < n; i++)
    {
      void **slot = pointer_map_insert (reg_alloc_map, 
                                        VEC_index (tree, real_ra->members, i));
      *slot = (void *)(size_t) num_reg_allocs;
    }

  reg_allocs[num_reg_allocs++] = real_ra->members;

  real_ra->members = 0;

  return true;
}

/* The function returns the reg_alloc number/id for ssa
   name NM. It returns -1 if the NM does not map to any
   reg_alloc.  */

static int
get_reg_alloc_id (const tree nm)
{
  void **slot;
  slot = pointer_map_contains (reg_alloc_map, nm);
  if (!slot)
    return -1;
  return (int) *slot;
}

/* The function destroys the temporary reg_alloc map
   used in reg_alloc computation.  */

static void
destroy_tmp_reg_alloc_map (void)
{
  pointer_map_destroy (tmp_reg_alloc_map);
  free_alloc_pool (tmp_reg_alloc_pool);
  tmp_reg_alloc_map = 0;
  tmp_reg_alloc_pool = 0;
}

/* The function converts the temporary reg_alloc map
   into the final ssa name to reg_alloc id map, and 
   destroyes the temporary map.  */

static void
finalize_reg_allocs (void)
{
  int sz = 0;
  pointer_map_traverse (tmp_reg_alloc_map,
                        finalize_ra_mem, &sz);
  reg_allocs = XNEWVEC (VEC(tree, heap)*, sz);
  num_reg_allocs = 0;
  pointer_map_traverse (tmp_reg_alloc_map,
                        finalize_ra_map, NULL);
  destroy_tmp_reg_alloc_map ();
}

/* In use-ref data flow problem, each bit position
   in the bit vector corresponds to a unique 
   reference (read) of a ssa name. All references
   of the same ssa name are allocated contiguously
   in the bitvector, so one ssa name has an associated
   bit position range. This function sets the bit range
   information for ssa name NM in a map. FIRST is the first
   bit position, LAST is the last (included) position. REGION
   is the lrs region.  */

static void
set_def_bit_range (int first, int last,
                   tree nm, lrs_region_p region)
{
  void **slot;
  long range;

  gcc_assert ((first & 0xffff) == first && (last & 0xffff) == last);
  slot = pointer_map_insert (region->bit_range_map, nm);
  range = (first << 16) + last;
  *slot = (void *)range;
}

/* The function retrieves the range of bit position for ssa name NM.
   The first bit position is returned in *FIRST, and the last position
   is in *LAST. REGION is the lrs region.  */

static void
get_def_bit_range (size_t *first, size_t *last,
                   tree nm, lrs_region_p region)
{
  void **slot;
  long range;

  slot = pointer_map_contains (region->bit_range_map, nm);
  range = (long) *slot;
  *first = ((range >> 16) & 0xffff);
  *last = (range & 0xffff);
}

/* The function is used to set the bitmask associated with a ssa name NM. 
   A bitmask has 1 bit set in bit range [first, last] which is computed
   from get_def_bit_range. NM is the ssa name. FIRST and LAST are the first
   and last bit position of the bit range. NUM_BITS is the total width of 
   the bit vector. REGION is the lrs region.  */

static void
set_def_bitmask (tree nm, size_t first, size_t last,
                 size_t num_bits, lrs_region_p region)
{
  void **slot;
  sbitmap bitmask;
  size_t i;

  bitmask = sbitmap_alloc (num_bits);
  sbitmap_zero (bitmask);

  for (i = first; i <= last; i++)
    SET_BIT (bitmask, i);

  slot = pointer_map_insert (region->def_bitmask_map, nm);
  *slot = (void*) bitmask;
}

/* The function returns the bitmask for ssa name NM in REGION.  */

static sbitmap
get_def_bitmask (tree nm, lrs_region_p region)
{
  void **slot;

  slot = pointer_map_contains (region->def_bitmask_map, nm);
  gcc_assert (slot && *slot);
  return (sbitmap) *slot;
}

/* The function returns the register class for a ssa name NM.  */

static inline enum lrs_reg_class
get_nm_reg_class (tree nm)
{
  if (FLOAT_TYPE_P (TREE_TYPE (nm)))
    return lrc_fr;
  return lrc_gr;
}

/* The function maps a use ref USE to a bit position POS in REGION.  */

static void
set_use_ref_bit_pos (tree *use, int pos, lrs_region_p region)
{
  void **slot;

  slot = pointer_map_insert (region->bit_pos_map, use);
  *slot = (void*) pos;
}

/* The function returns the bit position for use ref USE in REGION.  */

static int
get_use_ref_bit_pos (tree *use, lrs_region_p region)
{
  void **slot;

  slot = pointer_map_contains (region->bit_pos_map, use);
  gcc_assert (slot);

  return (int)*slot;
}

/* The function returns the live reference set at the program point 
   immediately after gimple statement STMT. REGION is the lrs region.  */

static inline sbitmap
get_across_stmt_use_ref_set (gimple stmt, lrs_region_p region)
{
  int stmt_idx = get_stmt_idx_in_region (stmt);
  return region->across_stmt_use_ref_sets[stmt_idx];
}

/* The function returns the GEN set of use references for the 
   basic block whose id is BB_RIDX in region REGION.  */

static inline sbitmap
get_bb_use_ref_gen_set (int bb_ridx, lrs_region_p region)
{
  return region->bb_use_ref_gen_sets[bb_ridx];
}

/* The function returns the KILL set of use references for the 
   basic block whose id is BB_RIDX in region REGION.  */

static inline sbitmap
get_bb_use_ref_kill_set (int bb_ridx, lrs_region_p region)
{
  return region->bb_use_ref_kill_sets[bb_ridx];
}

/* The function returns the IN set of use references for the 
   basic block whose id is BB_RIDX in region REGION.  */

static inline sbitmap
get_bb_use_ref_in_set (int bb_ridx, lrs_region_p region)
{
  return region->bb_use_ref_in_sets[bb_ridx];
}

/* The function returns the OUT set of use references for the 
   basic block whose id is BB_RIDX in region REGION.  */

static inline sbitmap
get_bb_use_ref_out_set (int bb_ridx, lrs_region_p region)
{
  return region->bb_use_ref_out_sets[bb_ridx];
}

/* The function merges the use ref IN set of
   basic block DEST_BB_RID into the OUT set of bb
   SRC_BB_RID in REGION.  */

static void
merge_from_in_set_ur (int src_bb_rid, int dest_bb_rid, 
                      lrs_region_p region)
{
  sbitmap dest_in_set, src_out_set;

  dest_in_set = get_bb_use_ref_in_set (dest_bb_rid, region);
  src_out_set = get_bb_use_ref_out_set (src_bb_rid, region);

  sbitmap_a_or_b (src_out_set, src_out_set, dest_in_set);
}

/* The function applies the transfer function on bb BB_RID 
   in region REGION for the live use ref data flow problem.  */

static bool
apply_use_ref_trans_func (int bb_rid, lrs_region_p region)
{
  sbitmap in, out, gen, kill;

  in = get_bb_use_ref_in_set (bb_rid, region);
  out = get_bb_use_ref_out_set (bb_rid, region);
  gen = get_bb_use_ref_gen_set (bb_rid, region);
  kill = get_bb_use_ref_kill_set (bb_rid, region);

  return sbitmap_a_or_b_and_c_compl_cg (in, gen, out, kill);
}

/* The function processes statement STMT and updates GEN set GEN_SET
   and KILL set  KILL_SET in REGION.  */

static void
update_use_ref_gen_kill_sets (gimple stmt, 
                              sbitmap gen_set,
                              sbitmap kill_set,
                              lrs_region_p region)
{
  int i, n;
  tree lhs = 0;
  lrs_stmt_p norm_stmt;

  norm_stmt = get_normalized_gimple_stmt (stmt, region);
  lhs = norm_stmt->def;
  if (lhs)
    {
      sbitmap def_bit_mask;

      def_bit_mask = get_def_bitmask (lhs, region);
      if (kill_set)
        sbitmap_a_or_b (kill_set, kill_set, def_bit_mask);
      sbitmap_a_and_not_b (gen_set, gen_set, def_bit_mask);
    }

  n = norm_stmt->num_uses;
  for (i = 0; i < n; i++)
    {
      int bit_pos;
      tree *op = norm_stmt->uses[i];
      bit_pos = get_use_ref_bit_pos (op, region);
      SET_BIT (gen_set, bit_pos);
    }
}

/* The function processes gimple statement STMT in REGION, and 
   updates the use ref set USE_REF_SET to produce the use ref set
   for the program point just before STMT.  */

static inline void
update_use_ref_set_by_stmt (gimple stmt, sbitmap use_ref_set,
                            lrs_region_p region)
{
  update_use_ref_gen_kill_sets (stmt, use_ref_set, NULL, region);
}

/* The function updates the use ref set GEN_SET by the use in phi node
   PHI via edge E. REGION is the lrs REGION.  */

static void
update_use_ref_gen_set_by_phi (gimple phi, edge e,
                               sbitmap gen_set,
                               lrs_region_p region)
{
    tree res;
    tree *arg;

    res = gimple_phi_result (phi);
    if (!is_gimple_reg (res))
      return;

    arg = PHI_ARG_DEF_PTR_FROM_EDGE (phi, e)->use;
    if (TREE_CODE (*arg) == SSA_NAME)
      SET_BIT (gen_set, get_use_ref_bit_pos (arg, region));
}

/* The function updates the gen or use set (GEN_SET) and 
   the kill set KILL_SET from PHI's definition.  REGION is
   the lrs region.  */

static void
update_use_ref_gen_kill_sets_by_phi (gimple phi,
                                     sbitmap gen_set,
                                     sbitmap kill_set,
                                     lrs_region_p region)
{
    tree res;
    sbitmap def_bit_mask;

    res = gimple_phi_result (phi);
    if (!is_gimple_reg (res))
      return;

    def_bit_mask = get_def_bitmask (res, region);
    if (kill_set)
      sbitmap_a_or_b (kill_set, kill_set, def_bit_mask);
    sbitmap_a_and_not_b (gen_set, gen_set, def_bit_mask);
}

/* This is a wrapper function of update_use_ref_gen_kill_sets_by_phi
   that updates the use ref set USE_REF_SET. PHI is tne phi node, and
   REGION is the lrs region.  */

static inline void
update_use_ref_set_by_phi (gimple phi, sbitmap use_ref_set,
                           lrs_region_p region)
{
  update_use_ref_gen_kill_sets_by_phi (phi, use_ref_set, NULL, region);
}

/* The function computes the gen/kill sets of basic block BB with id
   BB_RIDX for the live use ref problem. REGION is the lrs region.  */

static void
compute_use_ref_gen_kill_set (basic_block bb, int bb_ridx, 
                              lrs_region_p region)
{
  gimple_stmt_iterator gsi;
  sbitmap gen_set, kill_set;
  edge_iterator ei;
  edge e;

  gen_set = get_bb_use_ref_gen_set (bb_ridx, region);
  kill_set = get_bb_use_ref_kill_set (bb_ridx, region);

  /* Firstly check phi uses from succesor bbs.  Treating the use in
     a phi operand to be in the source block of the incoming edge is
     more precise (considering the case some of the operands are constant
     propagated.  */

  FOR_EACH_EDGE (e, ei, bb->succs)
    {
      int didx;
      basic_block dest = e->dest;

      if (get_bb_index_in_region (dest, region, &didx))
        {
          for (gsi = gsi_start_phis (dest); !gsi_end_p (gsi); gsi_next (&gsi))
            {
              gimple phi = gsi_stmt (gsi);
              update_use_ref_gen_set_by_phi (phi, e, gen_set, region);
            }
        }
    }

  for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi); gsi_prev (&gsi))
    {
      gimple stmt = gsi_stmt (gsi);
      update_use_ref_gen_kill_sets (stmt, gen_set, kill_set, region);
    }

  /* Now the phis -- the order of traversing does not matter.  */

  for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
    {
      gimple phi = gsi_stmt (gsi);
      update_use_ref_gen_kill_sets_by_phi (phi, gen_set, 
                                           kill_set, region);
    }
}

/* The function updates the map from ssa names to set of their use references.
   OP is the use reference, *OP is the ssa name, MP is the map, and REFED_NAMES
   is a vector of referenced ssa names.  */

static void
add_use_ref (tree *op, struct pointer_map_t *mp,
             VEC(tree, heap) **refed_names)
{
  void **slot;
  slot = pointer_map_insert (mp, *op);
  if (!*slot)
    VEC_safe_push (tree, heap, *refed_names, *op);
  VEC_safe_push (treep, heap, *(VEC(treep, heap)**) slot, op);
}

/* The function is used as a callback for pointer_map traverasl. It is
   used to destroy the use ref vectors pointed by the pointer map's
   slots.  */

static bool
destroy_use_ref_vec (const void *key ATTRIBUTE_UNUSED, void **value,
                     void *data ATTRIBUTE_UNUSED)
{
  VEC(treep, heap) *use_refs;

  if (!*value)
    return true;
  use_refs = (VEC(treep, heap) *)*value;
  VEC_free (treep, heap, use_refs);
  return true;
}

/* The function collects the virtual variables (and their ssa names)
   referenced.  VOP is a tree that may be a ssa name. REFED_VVARS is 
   the vector of referenced virtual variables, and REFED_VVAR_NAMES is
   the vector of referenced virtual variable ssa names. VVAR_SET is
   the pointer set of referenced virtual variables, and VVAR_NAME_SET
   is the pointer set of the referenced virtual variable names.  */

static inline void
add_one_vop (tree vop,
             VEC(tree, heap) ** refed_vvars,
             VEC(tree, heap) ** refed_vvar_names,
             struct pointer_set_t *vvar_set,
             struct pointer_set_t *vvar_name_set)
{
  if (TREE_CODE (vop) == SSA_NAME)
    {
      tree vvar;
      if (!pointer_set_insert (vvar_name_set, vop))
        VEC_safe_push (tree, heap, *refed_vvar_names, vop);

      vvar = SSA_NAME_VAR (vop);

      if (!pointer_set_insert (vvar_set, vvar))
        VEC_safe_push (tree, heap, *refed_vvars, vvar);
    }
}

/* The function collects the virtual variables (and their ssa names)
   referenced.  NORM_STMT is the normalized statement. REFED_VVARS is 
   the vector of referenced virtual variables, and REFED_VVAR_NAMES is
   the vector of referenced virtual variable ssa names. VVAR_SET is
   the pointer set of referenced virtual variables, and VVAR_NAME_SET
   is the pointer set of the referenced virtual variable names.  */

static void
get_vop_operands (lrs_stmt_p norm_stmt,
                  VEC(tree, heap) ** refed_vvars,
                  VEC(tree, heap) ** refed_vvar_names,
                  struct pointer_set_t *vvar_set,
                  struct pointer_set_t *vvar_name_set)
{
  gimple stmt;

  stmt = norm_stmt->stmt;
  if (gimple_code (stmt) != GIMPLE_PHI)
    {
      struct voptype_d *vdefs;
      struct voptype_d *vuses;
      int i, n;

      vuses = gimple_vuse_ops (stmt);
      while (vuses)
        {
          n = VUSE_NUM (vuses);
          for (i = 0; i < n; i++)
            {
              tree vop = VUSE_OP (vuses, i);
              add_one_vop (vop, refed_vvars, refed_vvar_names,
                           vvar_set, vvar_name_set);
            }
          vuses = vuses->next;
        }

      vdefs = gimple_vdef_ops (stmt);
      while (vdefs)
        {
          tree vdef;

          vdef = VDEF_RESULT (vdefs);
          add_one_vop (vdef, refed_vvars, refed_vvar_names,
                       vvar_set, vvar_name_set);
          n = VDEF_NUM (vdefs);
          for (i = 0; i < n; i++)
            {
              tree vop = VDEF_OP (vdefs, i);
              add_one_vop (vop, refed_vvars, refed_vvar_names,
                           vvar_set, vvar_name_set);
            }
          vdefs = vdefs->next;
        }
    }
  else
    {
      int i, n;
      tree lhs;

      lhs = gimple_phi_result (stmt);
      if (!is_gimple_reg (lhs))
        {
          add_one_vop (lhs, refed_vvars, refed_vvar_names,
                       vvar_set, vvar_name_set);
        }

      n = gimple_phi_num_args (stmt);
      for (i = 0; i < n; i++)
        {
          tree arg = gimple_phi_arg_def (stmt, i);
          if (TREE_CODE (arg) == SSA_NAME && !is_gimple_reg (arg))
            add_one_vop (arg, refed_vvars, refed_vvar_names,
                         vvar_set, vvar_name_set);
        }
    }
}

/* The comparison function used in quick sorting of SSA names. The
   names are sorted according to the register allocation ids, or if
   not mapped to registers, their ssa name versions.  */

static int
refed_nm_cmp (const void *p1, const void *p2)
{
  int ra1, ra2;
  const tree *n1 = (const tree *)p1;
  const tree *n2 = (const tree *)p2;
  ra1 = get_reg_alloc_id (*n1);
  ra2 = get_reg_alloc_id (*n2);

  if (ra1 == -1 && ra2 == -1)
    return SSA_NAME_VERSION (*n2) - SSA_NAME_VERSION (*n1);

  if (ra2 == -1)
    return 1;
  if (ra1 == -1)
    return -1;

  return ra2 - ra1;
}

/* The function initializes the bit vectors used in the live
   use ref data flow problem for REGION.  */

static void
prepare_bitmaps (lrs_region_p region)
{
  size_t i, nr, k, bit_pos;
  VEC(tree, heap) *refed_names = 0;
  struct pointer_map_t *nm_to_use_ref_map = 0;
  sbitmap region_live_out_nms;
  struct pointer_set_t *vvar_set = 0, *vvar_name_set = 0;

  nm_to_use_ref_map = pointer_map_create ();
  vvar_set = pointer_set_create ();
  vvar_name_set = pointer_set_create ();

  for (i = 0; i < region->num_stmts; i++)
    {
      tree lhs;
      size_t j, n;
      lrs_stmt_p norm_stmt;

      norm_stmt = &region->normalized_stmts[i];
      lhs = norm_stmt->def;

      if (lhs)
        {
          void **slot;
          slot = pointer_map_insert (nm_to_use_ref_map, lhs);
          if (!*slot)
            {
              VEC_safe_push (tree, heap, refed_names, lhs);
              *slot = VEC_alloc (treep, heap, 1);
            }
        }

      n = norm_stmt->num_uses;
      for (j = 0; j < n; j++)
        {
          tree *op = norm_stmt->uses[j];
          add_use_ref (op, nm_to_use_ref_map, &refed_names);
        }

      get_vop_operands (norm_stmt, &region->refed_vvars, 
                        &region->refed_vvar_names, 
                        vvar_set, vvar_name_set);
    }

  nr = VEC_length (tree, refed_names);
  region_live_out_nms = sbitmap_alloc (nr);
  sbitmap_zero (region_live_out_nms);

  /* Sort the refed names.  */
  qsort (VEC_address (tree, refed_names), VEC_length (tree, refed_names),
	 sizeof (tree), refed_nm_cmp);

  for (i = 0; i < nr; i++)
    {
      use_operand_p use_p;
      gimple use_stmt;
      imm_use_iterator iter;
      tree nm = VEC_index (tree, refed_names, i);

      FOR_EACH_IMM_USE_FAST (use_p, iter, nm)
        {
          int bb_idx;

          use_stmt = use_p->loc.stmt;
          /* Conservatively consider  NM is live out of the region.  */
          if (!get_bb_index_in_region (gimple_bb (use_stmt),
                                       region, &bb_idx))
            SET_BIT (region_live_out_nms, i);
        }
    }

  region->bit_pos_map = pointer_map_create ();
  region->bit_range_map = pointer_map_create ();
  region->def_bitmask_map = pointer_map_create ();

  bit_pos = 0;
  for (i = 0; i < nr; i += k)
    {
      size_t width, j, m, rpos;
      void **slot;
      int ra_id;
      bool is_live_out = false;
      VEC(treep, heap) *uses = 0;
      tree nm = VEC_index (tree, refed_names, i);

      ra_id = get_reg_alloc_id (nm);

      /* First compute the number of num of names in
         the same class.  */
      k = 1;
      if (ra_id != -1)
        {
          while (i + k < nr)
            {
              tree nm2;
              nm2 = VEC_index (tree, refed_names, i + k);
              if (get_reg_alloc_id (nm2) != ra_id)
                break;
              k++;
            }
        }

      width = 0;
      rpos = 0;
      for (j = i; j < i + k; j++)
        {
          tree nm2;

          nm2 = VEC_index (tree, refed_names, j);
          uses = 0;
          slot = pointer_map_contains (nm_to_use_ref_map, nm2);
          if (*slot)
            uses = (VEC(treep, heap) *)*slot;
          width += VEC_length (treep, uses);

          if (TEST_BIT (region_live_out_nms, j))
            is_live_out = true;

          if (uses)
            for (m = 0; m < VEC_length (treep, uses); m++)
              {
                set_use_ref_bit_pos (VEC_index (treep, uses, m),
                                     bit_pos + rpos, region);
                rpos++;
              }
        }
      gcc_assert (rpos == width);

      if (is_live_out)
        width++;

      /* Reserve one bit for unused defs for simplicity.  */
      if (!width)
        width++;

      for (j = i; j < i + k; j++)
        {
          tree nm2 = VEC_index (tree, refed_names, j);
          set_def_bit_range (bit_pos, bit_pos + width - 1, nm2, region);
        }

      bit_pos += width;
    }

  region->bitvec_width = bit_pos;

  for (i = 0; i < nr; i++)
    {
      size_t first, last;
      tree nm = VEC_index (tree, refed_names, i);

      get_def_bit_range (&first, &last, nm, region);
      set_def_bitmask (nm, first, last,
                       region->bitvec_width, region);
    }

  region->bb_use_ref_out_sets = sbitmap_vector_alloc (region->num_bbs,
                                                      region->bitvec_width);
  sbitmap_vector_zero (region->bb_use_ref_out_sets, region->num_bbs);

  region->bb_use_ref_in_sets = sbitmap_vector_alloc (region->num_bbs,
                                                     region->bitvec_width);
  sbitmap_vector_zero (region->bb_use_ref_in_sets, region->num_bbs);

  region->bb_use_ref_gen_sets = sbitmap_vector_alloc (region->num_bbs,
                                                      region->bitvec_width);
  sbitmap_vector_zero (region->bb_use_ref_gen_sets, region->num_bbs);

  region->bb_use_ref_kill_sets = sbitmap_vector_alloc (region->num_bbs,
                                                       region->bitvec_width);
  sbitmap_vector_zero (region->bb_use_ref_kill_sets, region->num_bbs);

  region->across_stmt_use_ref_sets = sbitmap_vector_alloc (region->num_stmts,
                                                           region->bitvec_width);
  sbitmap_vector_zero (region->across_stmt_use_ref_sets, region->num_stmts);

  /* Now initialize the exit BB's live out use ref sets.  */
  nr = VEC_length (tree, refed_names);
  for (i = 0; i < nr; i++)
    {
      if (TEST_BIT (region_live_out_nms, i))
        {
          size_t j;
          size_t first, last;
          get_def_bit_range (&first, &last,
                             VEC_index (tree, refed_names, i),
                             region);

          for (j = 0; j < region->num_exits; j++)
            {
              int ridx = 0;
              get_bb_index_in_region (region->exits[j], region, &ridx);
              SET_BIT (region->bb_use_ref_out_sets[ridx], last);
            }
        }
    }

  region->region_live_out_names = region_live_out_nms;
  region->refed_names = refed_names;

  pointer_map_traverse(nm_to_use_ref_map, destroy_use_ref_vec, NULL);
  pointer_map_destroy (nm_to_use_ref_map);
  pointer_set_destroy (vvar_set);
  pointer_set_destroy (vvar_name_set);
}

/* From the solution set of the use ref data flow problem, this function
   computes the live use-ref sets for each program point after each statement
   in region REGION.  */

static void
compute_live_use_ref_set_for_stmts (lrs_region_p region)
{
  size_t i;
  basic_block bb;
  gimple_stmt_iterator gsi;
  sbitmap tmp_set;

  tmp_set = sbitmap_alloc (region->bitvec_width);
  for (i = 0; i < region->num_bbs; i++)
    {
      sbitmap bb_out_set;
      VEC(gimple, heap) *phis = 0;
      int j;
      edge_iterator ei;
      edge e;

      bb = region->body[i];
      bb_out_set = region->bb_use_ref_out_sets[i];
      sbitmap_copy (tmp_set, bb_out_set);

      /* Firstly check phi uses from succesor bbs.  */
      FOR_EACH_EDGE (e, ei, bb->succs)
        {
          int didx;
          basic_block dest = e->dest;

          if (get_bb_index_in_region (dest, region, &didx))
            {
              for (gsi = gsi_start_phis (dest); !gsi_end_p (gsi); gsi_next (&gsi))
                {
                  gimple phi = gsi_stmt (gsi);
                  update_use_ref_gen_set_by_phi (phi, e, tmp_set, region);
                }
            }
        }

      for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi); gsi_prev (&gsi))
        {
          int stmt_idx;
          gimple stmt = gsi_stmt (gsi);

          stmt_idx = get_stmt_idx_in_region (stmt);
          sbitmap_copy (region->across_stmt_use_ref_sets[stmt_idx],
                        tmp_set);
          update_use_ref_set_by_stmt (stmt, tmp_set, region);
        }

      for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
        VEC_safe_push (gimple, heap, phis, gsi_stmt (gsi));

      if (phis)
        for (j = VEC_length (gimple, phis) - 1; j >= 0; j--)
          {
            int stmt_id;
            gimple phi = VEC_index (gimple, phis, j);
            stmt_id = get_stmt_idx_in_region (phi);
            sbitmap_copy (region->across_stmt_use_ref_sets[stmt_id], tmp_set);
            update_use_ref_set_by_phi (phi, tmp_set, region);
          }
    }

  sbitmap_free (tmp_set);
}

/* The function returns true if the region REGION has high register
   pressure (general register class), i.e., max register required
   exceeds the number of registers available.  */

static inline bool
has_high_gr_reg_pressure (lrs_region_p region)
{
  return (region->available_regs[lrc_gr]
          <= region->reg_pressure[lrc_gr]);
}

/* The function returns true if the region REGION has high register
   pressure (float register class), i.e., max register required
   exceeds the number of registers available.  */

static inline bool
has_high_fr_reg_pressure (lrs_region_p region)
{
  return (region->available_regs[lrc_fr]
          <= region->reg_pressure[lrc_fr]
          && region->available_regs[lrc_fr] != 0);
}

/* The function returns true if the region REGION has high register
   pressure.  */

static inline bool
has_high_reg_pressure (lrs_region_p region)
{
  return (has_high_gr_reg_pressure (region)
          || has_high_fr_reg_pressure (region));
}


/* The function returns true if ssa name LR has any use reference bit
   set in bitvector BITVEC.  */

static inline bool
is_lr_live (tree lr, sbitmap bitvec, lrs_region_p region)
{
  size_t first, last;
  bool is_live;

  get_def_bit_range (&first, &last, lr, region);
  is_live = !sbitmap_range_empty_p (bitvec, first, last - first + 1);

  return is_live;
}

/* The function computes the number of LRs that have any use reference
   bit set in bitvector BITVEC in REGION.  The number of general register
   class LRs is set in *NGR and the number of float register class
   LRs is stored in *NFR.  */

static void
get_num_live_lrs (sbitmap bitvec, lrs_region_p region,
                  size_t *ngr, size_t *nfr)
{
  int i, n;
  int gr_pressure = 0;
  int fr_pressure = 0;

  n = VEC_length (tree, region->refed_names);

  for (i = 0; i < n; i++)
    {
      bool is_live;
      tree nm = VEC_index (tree, region->refed_names, i);

      is_live = is_lr_live (nm, bitvec, region);
      if (is_live)
        {
          if (get_nm_reg_class  (nm) == lrc_fr)
            fr_pressure++;
          else
            gr_pressure++;
        }
    }
  *ngr = gr_pressure;
  *nfr = fr_pressure;
}

/* The function computes register pressure for basic block BB (with id BB_RIDX)
   in region REGION.  */

static void
compute_reg_pressure_bb (basic_block bb, int bb_ridx, lrs_region_p region)
{

  gimple_stmt_iterator gsi;
  size_t bb_gr_pressure = 0;
  size_t bb_fr_pressure = 0;

  for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
    {
      int id;
      size_t ngr, nfr;
      gimple stmt = gsi_stmt (gsi);

      id = get_stmt_idx_in_region (stmt);
      get_num_live_lrs (region->across_stmt_use_ref_sets[id],
                        region, &ngr, &nfr);
      if (ngr > bb_gr_pressure)
        bb_gr_pressure = ngr;
      if (nfr > bb_fr_pressure)
        bb_fr_pressure = nfr;
    }

  for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
    {
      int id;
      size_t ngr, nfr;
      gimple stmt = gsi_stmt (gsi);

      id = get_stmt_idx_in_region (stmt);
      get_num_live_lrs (region->across_stmt_use_ref_sets[id],
                        region, &ngr, &nfr);
      if (ngr > bb_gr_pressure)
        bb_gr_pressure = ngr;
      if (nfr > bb_fr_pressure)
        bb_fr_pressure = nfr;
    }

  region->bb_reg_pressures[lrc_gr][bb_ridx] = bb_gr_pressure;
  region->bb_reg_pressures[lrc_fr][bb_ridx] = bb_fr_pressure;
  
  if (bb_gr_pressure > region->reg_pressure[lrc_gr])
    region->reg_pressure[lrc_gr] = bb_gr_pressure;
  if (bb_fr_pressure > region->reg_pressure[lrc_fr])
    region->reg_pressure[lrc_fr] = bb_fr_pressure;
}


/* The function computes register pressure for region REGION.  */

static void
compute_reg_pressure (lrs_region_p region)
{
  size_t i, j, nr;
  struct pointer_set_t *mode_set;
  VEC(int, heap) *refed_modes[lrc_num];

  nr = VEC_length (tree, region->refed_names);
  mode_set = pointer_set_create ();
  gcc_assert (lrc_num == 2);
  refed_modes[0] = NULL;
  refed_modes[1] = NULL;

  for (i = 0; i < nr; i++)
    {
      enum machine_mode mode;
      enum lrs_reg_class rc;
      tree nm = VEC_index (tree, region->refed_names, i);

      mode = TYPE_MODE (TREE_TYPE (nm));
      rc = get_nm_reg_class (nm);
      if (!pointer_set_insert (mode_set, (void *)(long) mode))
        VEC_safe_push (int, heap,
                       refed_modes[rc], (int) mode);
    }

  for (i = 0; i < lrc_num; i++)
    {
      size_t k;
      region->available_regs[i] = 0;

      for (k = 0; k < FIRST_PSEUDO_REGISTER; k++)
        {
          bool is_reg_ok = false;
          enum reg_class rc = REGNO_REG_CLASS (k);
          for (j = 0; j < VEC_length (int, refed_modes[i]); j++)
            {
              enum machine_mode mode;
              mode = (enum machine_mode) VEC_index (int, refed_modes[i], j);
              if (HARD_REGNO_MODE_OK (k, mode) && !fixed_regs[k]
                  && ((i == lrc_gr && REG_CLASS_MAP (rc) == GENERAL_REGS)
                      || (i == lrc_fr && REG_CLASS_MAP (rc) != GENERAL_REGS)))
                {
                  is_reg_ok = true;
                  break;
                }
            }
          if (is_reg_ok)
            region->available_regs[i]++;
        }
      region->reg_pressure[i] = 0;
      region->bb_reg_pressures[i] = XNEWVEC (size_t, region->num_bbs);
    }

  for (i = 0; i < region->num_bbs; i++)
    compute_reg_pressure_bb (region->body[i], i, region);

  pointer_set_destroy (mode_set);
  VEC_free (int, heap, refed_modes[0]);
  VEC_free (int, heap, refed_modes[1]);
}

/* The function performs initialization for use ref data flow
   analysis for region REGION.  */

static void
initialize_data_flow_ur (lrs_region_p region)
{
  size_t i;

  prepare_bitmaps (region);

  for (i = 0; i < region->num_bbs; i++)
    compute_use_ref_gen_kill_set (region->body[i], i, region);
}

static void dump_data_flow_result (lrs_region_p, const char *);

/* The function performs live use reference data flow analysis, which
   is a backward data flow problem similar to live variable analysis.  */

static void
perform_data_flow_ur (lrs_region_p region)
{
  sbitmap worklist;
  size_t i;

  initialize_data_flow_ur (region);

  worklist = sbitmap_alloc (region->num_bbs);
  sbitmap_zero (worklist);

  for (i = 0; i < region->num_exits; i++)
    {
      int ridx = 0;
      bool fd;

      fd = get_bb_index_in_region (region->exits[i], region, &ridx);
      gcc_assert (fd);
      SET_BIT (worklist, ridx);
    }

  while (!sbitmap_empty_p (worklist))
    {
      int bb_rid;
      basic_block bb;
      edge e;
      edge_iterator ei;

      bb_rid = sbitmap_first_set_bit (worklist);
      RESET_BIT (worklist, bb_rid);
      bb = region->body[bb_rid];

      FOR_EACH_EDGE (e, ei, bb->succs)
        {
          int didx;

          if (get_bb_index_in_region (e->dest, region, &didx))
            merge_from_in_set_ur (bb_rid, didx, region);
        }

      if (apply_use_ref_trans_func (bb_rid, region))
        {
          FOR_EACH_EDGE (e, ei, bb->preds)
            {
              int pidx;

              if (get_bb_index_in_region (e->src, region, &pidx))
                SET_BIT (worklist, pidx);
            }
        }
    }

  sbitmap_free (worklist);

  compute_live_use_ref_set_for_stmts (region);

  compute_reg_pressure (region);

  dump_data_flow_result (region, "Before code motion");
}

/* This is the comparison function used in sorting of virtual
   ssa names.  The purpose of the sorting is to put all ssa names
   from the same variable together.  */

static int
refed_vnm_cmp (const void *p1, const void *p2)
{
  tree v1, v2;
  const tree n1 = *(const tree *)p1;
  const tree n2 = *(const tree *)p2;

  v1 = SSA_NAME_VAR (n1);
  v2 = SSA_NAME_VAR (n2);

  if (v1 == v2)
    return SSA_NAME_VERSION (n2) - SSA_NAME_VERSION (n1);

  return DECL_UID (v2) - DECL_UID (v1);
}

/* The function maps virtual name VNM to a bit position POS.  */

static void
set_vnm_bit_pos (tree vnm, int pos, lrs_region_p region)
{
  void **slot;

  slot = pointer_map_insert (region->vname_bit_pos_map, vnm);
  *slot = (void *) pos;
}

/* The function returns the bit position of virtual name VNM.  */

static int
get_vnm_bit_pos (tree vnm, lrs_region_p region)
{
  void **slot;

  slot = pointer_map_contains (region->vname_bit_pos_map, vnm);
  gcc_assert (slot);

  return (int)(size_t)*slot;
}

/* The function maps virtual variable VVAR to bit position range
   [first, last].  */

static void
set_vvar_bit_range (int first, int last,
                    tree vvar, lrs_region_p region)
{
  void **slot;
  long range;

  slot = pointer_map_insert (region->vvar_bit_range_map, vvar);
  range = (first << 16) + last;
  *slot = (void *)range;
}

/* The function gets the bit position range for virtual variable
   VVAR.  The first position is stored in *FIRST, and last position
   is in *LAST.  */

static void
get_vvar_bit_range (size_t *first, size_t *last,
                    tree vvar, lrs_region_p region)
{
  void **slot;
  long range;

  slot = pointer_map_contains (region->vvar_bit_range_map, vvar);
  range = (long)(size_t) *slot;
  *first = ((range >> 16) & 0xffff);
  *last = (range & 0xffff);
}

/* The function computes the gen and kill sets for basic block BB
   with id BB_RIDX in region REGION.  The df problem is virtual 
   variable reaching definitions.  */

static void
compute_rd_gen_kill_set (basic_block bb, int bb_ridx, 
                         lrs_region_p region);

/* The function initializes the virtual variable reaching definition
   data flow problem for region REGION.  */

static void
initialize_data_flow_rd (lrs_region_p region)
{
  int i, n, nb, bit_first = 0, bit_last = -1, entry_rid;
  tree vvar; 

  region->vname_bit_pos_map = pointer_map_create ();
  region->vvar_bit_range_map = pointer_map_create ();
  vvar = NULL;
  qsort (VEC_address (tree, region->refed_vvar_names), 
         VEC_length (tree, region->refed_vvar_names),
	 sizeof (tree), refed_vnm_cmp);

  n = VEC_length (tree, region->refed_vvar_names);
  for (i = 0; i < n; i++)
    {
      tree cur_vvar;
      tree vnm = VEC_index (tree, region->refed_vvar_names, i);

      set_vnm_bit_pos (vnm, i, region);
      cur_vvar = SSA_NAME_VAR (vnm);
      if (cur_vvar != vvar)
        {
          if (vvar)
            set_vvar_bit_range (bit_first, bit_last,
                                vvar, region);
          bit_first = bit_last + 1; 
          vvar = cur_vvar;
        }
      bit_last = i;
    }
  if (vvar)
    set_vvar_bit_range (bit_first, bit_last,
                        vvar, region);

  region->bb_rd_out_sets = sbitmap_vector_alloc (region->num_bbs, n);
  sbitmap_vector_zero (region->bb_rd_out_sets, region->num_bbs);
  region->bb_rd_in_sets = sbitmap_vector_alloc (region->num_bbs, n);
  sbitmap_vector_zero (region->bb_rd_in_sets, region->num_bbs);
  region->bb_rd_gen_sets = sbitmap_vector_alloc (region->num_bbs, n); 
  sbitmap_vector_zero (region->bb_rd_gen_sets, region->num_bbs);
  region->bb_rd_kill_sets = sbitmap_vector_alloc (region->num_bbs, n);
  sbitmap_vector_zero (region->bb_rd_kill_sets, region->num_bbs);
  region->stmt_rd_sets = sbitmap_vector_alloc (region->num_stmts, n);
  sbitmap_vector_zero (region->stmt_rd_sets, region->num_stmts);

  nb = region->num_bbs;
  for (i = 0; i < nb; i++)
    compute_rd_gen_kill_set (region->body[i], i, region);

  get_bb_index_in_region (region->entry, region, &entry_rid);
  /* Now initialize the entry's in-set.  */
  for (i = 0; i < n; i++)
    {
      gimple def;
      basic_block bb;
      int rid;
      tree vnm = VEC_index (tree, region->refed_vvar_names, i);

      def = SSA_NAME_DEF_STMT (vnm);
      bb = gimple_bb (def);
      if (!get_bb_index_in_region (bb, region, &rid))
        SET_BIT (region->bb_rd_in_sets[entry_rid], i);
    }
}

/* The function returns the reaching def set before statement
   STMT in region REGION.  */

static inline sbitmap
get_stmt_rd_set (gimple stmt, lrs_region_p region)
{
  int stmt_idx = get_stmt_idx_in_region (stmt);
  return region->stmt_rd_sets[stmt_idx];
}

/* The function returns the reaching def GEN set for basic block
   BB_RIDX in region REGION.  */

static inline sbitmap
get_bb_rd_gen_set (int bb_ridx, lrs_region_p region)
{
  return region->bb_rd_gen_sets[bb_ridx];
}

/* The function returns the reaching def KILL  set for basic block
   BB_RIDX in region REGION.  */

static inline sbitmap
get_bb_rd_kill_set (int bb_ridx, lrs_region_p region)
{
  return region->bb_rd_kill_sets[bb_ridx];
}

/* The function returns the reaching def IN set for basic block
   BB_RIDX in region REGION.  */

static inline sbitmap
get_bb_rd_in_set (int bb_ridx, lrs_region_p region)
{
  return region->bb_rd_in_sets[bb_ridx];
}

/* The function returns the reaching def OUT set for basic block
   BB_RIDX in region REGION.  */

static inline sbitmap
get_bb_rd_out_set (int bb_ridx, lrs_region_p region)
{
  return region->bb_rd_out_sets[bb_ridx];
}

/* The function merges OUT set from source block SRC_BB_RID
   into the IN set of DEST_BB_RID in REGION.  The DF problem
   is the reaching virtual variable definition.  */ 

static void
merge_from_out_set_rd (int dest_bb_rid, int src_bb_rid, 
                       lrs_region_p region)
{
  sbitmap dest_in_set, src_out_set;

  dest_in_set = get_bb_rd_in_set (dest_bb_rid, region);
  src_out_set = get_bb_rd_out_set (src_bb_rid, region);

  sbitmap_a_or_b (dest_in_set, dest_in_set, src_out_set);
}

/* The function applies the transfer function (reaching def)
   for block BB_RID in REGION.  */

static bool
apply_rd_trans_func (int bb_rid, lrs_region_p region)
{
  sbitmap in, out, gen, kill;

  in = get_bb_rd_in_set (bb_rid, region);
  out = get_bb_rd_out_set (bb_rid, region);
  gen = get_bb_rd_gen_set (bb_rid, region);
  kill = get_bb_rd_kill_set (bb_rid, region);

  return sbitmap_a_or_b_and_c_compl_cg (out, gen, in, kill);
}

/* The function updates the GEN set GEN_SET and KILL set KILL_SET by
   processing statement STMT in region REGION.  */

static void
update_rd_gen_kill_sets (gimple stmt, sbitmap gen_set,
                         sbitmap kill_set,
                         lrs_region_p region)
{
  struct voptype_d *vdefs;

  vdefs = gimple_vdef_ops (stmt);
  while (vdefs)
    {
      tree vdef, vvar;
      size_t first, last, i, pos;

      /* The result of the reaching def analysis is used for
         checking violation of data dependency (anti-dependency)
         during downward code motion  -- not for the purpose of redundancy
         elimination or dead code elimination.  What we care about is whether
         there is most recent definition that can block the downward motion.
         For this reason, all may-def or partial defs of a virtual operand
         are considered a strong kill.  For instance:

         <example 1 start>

         *s = ...  (0)  {VDEF: vname_v1 }
         ...
         t = *s;   (1) statement to be moved {VUSE: vname_v1 }

         *p = .... (2)  {VDEF: vname_v2, VUSE: vname_v1 }

         ....
         // Since *p is a may def, both vname_v1 and vname_v2 
         // reach this program point, but we do not need to propagate
         // vname_v1 to this point in order to block the code motion of (1).
         x = t + y (3)  <-- check if (1) can be moved just before (3)

         <example 1 end>


         The reason of this handling is to allow more agressive code motion. 
         For instance, if (2) appears before (0), it is ok to move (1) before (3). 
         This can be archieve by strong kill.

         <example 2 start>

         *p = .... (2)  {VDEF: vname_v2 }

         *s = ...  (0)  {VDEF: vname_v1  VUSE: vname_v2 }
         ...
         t = *s;   (1) statement to be moved {VUSE: vname_v1 }
         ....
         // It is legal to move (1) just before (3).
         x = t + y (3)  <-- check if (1) can be moved just before (3)

         <example 2 end>

      */

      vdef = VDEF_RESULT (vdefs);
      vvar = SSA_NAME_VAR (vdef);
      get_vvar_bit_range (&first, &last, vvar, region);
      pos = get_vnm_bit_pos (vdef,  region);
      for (i = first; i <= last; i++)
        {
          RESET_BIT (gen_set, i);
          if (kill_set)
            SET_BIT (kill_set, i);
        }
      SET_BIT (gen_set, pos);
      if (kill_set)
        RESET_BIT (kill_set, pos);
      vdefs = vdefs->next;
    }
}

/* The function updates the GEN set GEN_SET and KILL set KILL_SET by
   processing phi node STMT in region REGION.  */

static void
update_rd_gen_kill_sets_by_phi (gimple stmt, sbitmap gen_set,
                                sbitmap kill_set,
                                lrs_region_p region)
{
  tree  vvar;
  size_t first, last, i, pos;
  tree lhs = gimple_phi_result (stmt);
  if (!is_gimple_reg (lhs) && TREE_CODE (lhs) == SSA_NAME)
    {
      vvar = SSA_NAME_VAR (lhs);
      get_vvar_bit_range (&first, &last, vvar, region);
      pos = get_vnm_bit_pos (lhs,  region);
      for (i = first; i <= last; i++)
        {
          RESET_BIT (gen_set, i);
          if (kill_set) 
            SET_BIT (kill_set, i);
        }
      SET_BIT (gen_set, pos);
      if (kill_set) 
        RESET_BIT (kill_set, pos);
    }
}

/* The function computes the GEN/KILL sets for basic block BB
   with id BB_RIDX in region REGION.  */

static void
compute_rd_gen_kill_set (basic_block bb, int bb_ridx, 
                         lrs_region_p region)
{
  gimple_stmt_iterator gsi;
  sbitmap gen_set, kill_set;

  gen_set = get_bb_rd_gen_set (bb_ridx, region);
  kill_set = get_bb_rd_kill_set (bb_ridx, region);

  for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
    {
      gimple phi = gsi_stmt (gsi);
      update_rd_gen_kill_sets_by_phi (phi, gen_set, 
                                      kill_set, region);
    }

  for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
    {
      gimple stmt = gsi_stmt (gsi);
      update_rd_gen_kill_sets (stmt, gen_set, kill_set, region);
    }
}

/* The function computes the reaching def sets for  program points
   before all statements in region REGION.  */

static void
compute_rd_set_for_stmts (lrs_region_p region)
{
  size_t i, n;
  basic_block bb;
  gimple_stmt_iterator gsi;
  sbitmap tmp_set;

  n = VEC_length (tree, region->refed_vvar_names);
  tmp_set = sbitmap_alloc (n);

  for (i = 0; i < region->num_bbs; i++)
    {
      sbitmap bb_in_set;

      bb = region->body[i];
      bb_in_set = region->bb_rd_in_sets[i];
      sbitmap_copy (tmp_set, bb_in_set);

      for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
        {
          int stmt_id;
          gimple phi = gsi_stmt (gsi);
          stmt_id = get_stmt_idx_in_region (phi);
          sbitmap_copy (region->stmt_rd_sets[stmt_id], tmp_set);
          update_rd_gen_kill_sets_by_phi (phi, tmp_set, NULL,  region);
        }


      for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
        {
          int stmt_idx;
          gimple stmt = gsi_stmt (gsi);

          stmt_idx = get_stmt_idx_in_region (stmt);
          sbitmap_copy (region->stmt_rd_sets[stmt_idx], tmp_set);
          update_rd_gen_kill_sets (stmt, tmp_set, NULL, region);
        }
    }
  sbitmap_free (tmp_set);
}

static void dump_data_flow_result_rd (lrs_region_p);

/* The function performs virtual variable reaching def data flow
   analysis for region REGION.  */

static void
perform_data_flow_rd (lrs_region_p region)
{
  int ridx;
  sbitmap worklist;
  bool fd;

  if (VEC_length (tree, region->refed_vvars) == 0)
    return;

  initialize_data_flow_rd (region);

  worklist = sbitmap_alloc (region->num_bbs);
  sbitmap_zero (worklist);

  fd = get_bb_index_in_region (region->entry, region, &ridx);
  gcc_assert (fd);
  SET_BIT (worklist, ridx);

  while (!sbitmap_empty_p (worklist))
    {
      int bb_rid;
      basic_block bb;
      edge e;
      edge_iterator ei;

      bb_rid = sbitmap_first_set_bit (worklist);
      RESET_BIT (worklist, bb_rid);
      bb = region->body[bb_rid];

      FOR_EACH_EDGE (e, ei, bb->preds)
        {
          int sidx;

          if (get_bb_index_in_region (e->src, region, &sidx))
            merge_from_out_set_rd (bb_rid, sidx, region);
        }

      if (apply_rd_trans_func (bb_rid, region))
        {
          FOR_EACH_EDGE (e, ei, bb->succs)
            {
              int pidx;

              if (get_bb_index_in_region (e->dest, region, &pidx))
                SET_BIT (worklist, pidx);
            }
        }
    }

  compute_rd_set_for_stmts (region);
  dump_data_flow_result_rd (region);

  sbitmap_free (worklist);
}

/* The function computes the order of statements in
   BB.  Phi nodes are not traversed, and they all
   have the default order of 0.  */

static void
compute_stmt_order (basic_block bb)
{
  gimple_stmt_iterator gsi;
  /* Order starts from one.  Zero is reserved for phis.  */
  size_t order = 1;

  for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
    {
      void **slot;
      gimple stmt = gsi_stmt (gsi);
      slot = pointer_map_insert (stmt_order, stmt);
      gcc_assert (!*slot);
      *slot = (void *) order;
      order++;
      gimple_set_visited (stmt, false);
    }
}

/* Returns the order of STMT in its current basic block.
   Returns 0 for phi nodes, and -1 if STMT is created
   after the stmt order map is created (to indicate
   that no order information is available).  */

static size_t
get_stmt_order (const_gimple stmt)
{
  void **slot;

  if (gimple_code (stmt) == GIMPLE_PHI)
    return 0;

  slot = pointer_map_contains (stmt_order, stmt);
  return slot ? (size_t) *slot : (size_t)-1U;
}

/* The function resets the order of STMT to NEW_ORDER after
   code motion.  NEW_ORDER for STMT is the order of the stmt
   it is inserted after/before.  */

static void
reset_stmt_order (const_gimple stmt, size_t new_order)
{
  void **slot = pointer_map_contains (stmt_order, stmt);
  if (!slot)
    /* New stmt can be created in factorization/undistribution.  */
    slot = pointer_map_insert (stmt_order, stmt);
  *slot = (void *) new_order;
}

/* This function checks if STMT1, which appears in the same
   basic block as STMT2, actually precedes STMT2 in the IL
   stream.  */

static bool
is_preceding_in_real_order (gimple stmt1, gimple stmt2)
{
  gimple_stmt_iterator gsi = gsi_for_stmt (stmt1);
  bool found = false;

  gcc_assert (gimple_code (stmt1) != GIMPLE_PHI);
  for (; !gsi_end_p (gsi); gsi_next (&gsi))
    {
      gimple stmt = gsi_stmt (gsi);
      if (stmt == stmt2)
        {
          found = true;
          break;
        }
    }

  return found;
}

/* Returns true if STMT1 dominates STMT2.  This query function
   is always invoked with STMT1 being the dependence predecessor
   and STMT2 being the successor.  When they are in the same basic
   block, their statement orders are used for determining dominance.
   Note that after code motion, statements that are moved get the
   new order from the statement of their insertion point.  As a result,
   two statements may have the same order in the same BB.  When
   dominance relationship is checked on such two statements, their
   real statement order needs to be examined which means traversing
   the statement list.  */

static bool
is_dominating (gimple stmt1, gimple stmt2)
{
  basic_block bb1 = gimple_bb (stmt1);
  basic_block bb2 = gimple_bb (stmt2);

  if (bb1 == bb2)
    {
      size_t stmt_order1 = get_stmt_order (stmt1);
      size_t stmt_order2 = get_stmt_order (stmt2);

      gcc_assert (stmt_order1 != (size_t)-1U
                  && stmt_order2 != (size_t)-1U);

      if (stmt_order1 == 0)
        {
          gcc_assert (gimple_code (stmt1) == GIMPLE_PHI);
          /* It does not matter if the other stmt is a phi
             or not -- as the code motion insertion point
             is guaranteed to be dominated by the phis.  */
          return true;
        }

      if (stmt_order1 == stmt_order2)
        /* Slow check which is rare.  */
        return is_preceding_in_real_order (stmt1, stmt2);
      else
        return stmt_order1 < stmt_order2;
    }
  else
    {
      bool dominates = false;
      dominates = dominated_by_p (CDI_DOMINATORS, bb2, bb1);
      return dominates;
    }
}

/* expression of the form:
    a - b - c - d - e
  allows ressociation of operands b, c, d, and e.
  This requires left tree expansion only.
*/

static bool
is_reassociable_minus_op (enum tree_code rhs_code, tree rhs1)
{
  gimple def1;
  if (rhs_code != MINUS_EXPR)
    return false;

  if (TREE_CODE (rhs1) != SSA_NAME || !has_single_use (rhs1))
    return false;

  def1 = SSA_NAME_DEF_STMT (rhs1);
  if (is_gimple_assign (def1)
      && gimple_assign_rhs_code (def1) != MINUS_EXPR)
    return false;

  return true;
}

/* The function returns true if the right hand side operands RHS1 and RHS2
   are reassociable w.r.t to opcode RHS_CODE.  LHS is the left hand side 
   of the gimple assignment.  */

static inline bool
is_gimple_assign_rhs_reassociable (enum tree_code rhs_code,
				   tree lhs, tree rhs1, tree rhs2)
{

  if (!associative_tree_code (rhs_code)
      && !is_reassociable_minus_op (rhs_code, rhs1))
    return false;

  /* If associative-math we can do reassociation for
     non-integral types.  Or, we can do reassociation for
     non-saturating fixed-point types.  */

  if ((!INTEGRAL_TYPE_P (TREE_TYPE (lhs))
       || !INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
       || !INTEGRAL_TYPE_P (TREE_TYPE (rhs2)))
      && (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (lhs))
	  || !SCALAR_FLOAT_TYPE_P (TREE_TYPE(rhs1))
	  || !SCALAR_FLOAT_TYPE_P (TREE_TYPE(rhs2))
	  || !flag_associative_math)
      && (!NON_SAT_FIXED_POINT_TYPE_P (TREE_TYPE (lhs))
	  || !NON_SAT_FIXED_POINT_TYPE_P (TREE_TYPE(rhs1))
	  || !NON_SAT_FIXED_POINT_TYPE_P (TREE_TYPE(rhs2))))
    return false;

  return true;
}

/* The function expands the expression tree REASSOC_TREE by checking
   the tree operand at slot OPERAND_SLOT.  */

static void
expand_reassoc_tree (lrs_reassoc_tree_p reassoc_tree, treep operand_slot,
                     bool is_right_subtree, lrs_region_p region)
{
  gimple def_stmt;
  treep rhs1, rhs2;
  enum tree_code rhs_code;
  basic_block bb;
  int bb_rid;

  if (TREE_CODE (*operand_slot) != SSA_NAME)
    {
      VEC_safe_push (tree, heap, reassoc_tree->leaf_operands,
		     *operand_slot);
      VEC_safe_push (treep, heap, reassoc_tree->use_ref_slots,
		     operand_slot);
      return;
    }

  def_stmt = SSA_NAME_DEF_STMT (*operand_slot);
  if (!is_gimple_assign (def_stmt)
      || !has_single_use (*operand_slot))
    {
      VEC_safe_push (tree, heap, reassoc_tree->leaf_operands,
		     *operand_slot);
      VEC_safe_push (treep, heap, reassoc_tree->use_ref_slots,
		     operand_slot);
      return;
    }
  bb = gimple_bb (def_stmt);
  if (!get_bb_index_in_region (bb, region, &bb_rid))
    {
      VEC_safe_push (tree, heap, reassoc_tree->leaf_operands,
		     *operand_slot);
      VEC_safe_push (treep, heap, reassoc_tree->use_ref_slots,
		     operand_slot);
      return;
    }
  /* We limit the tree to one BB for now. When this is changed, we
     also need to change the logic in reassoc_tree_opnd_cmp.  */
  if (bb != gimple_bb (reassoc_tree->root))
    {
      VEC_safe_push (tree, heap, reassoc_tree->leaf_operands,
		     *operand_slot);
      VEC_safe_push (treep, heap, reassoc_tree->use_ref_slots,
		     operand_slot);
      return;
    }

  rhs_code = gimple_assign_rhs_code (def_stmt);
  if (rhs_code != reassoc_tree->opc ||
      (reassoc_tree->opc == MINUS_EXPR && is_right_subtree))
    {
      VEC_safe_push (tree, heap, reassoc_tree->leaf_operands,
		     *operand_slot);
      VEC_safe_push (treep, heap, reassoc_tree->use_ref_slots,
		     operand_slot);
      return;
    }

  rhs1 = gimple_assign_rhs1_ptr (def_stmt);
  rhs2 = gimple_assign_rhs2_ptr (def_stmt);
  gimple_set_visited (def_stmt, true);
  VEC_safe_push (gimple, heap, reassoc_tree->inner_nodes, def_stmt);

  expand_reassoc_tree (reassoc_tree, rhs1, false, region);
  expand_reassoc_tree (reassoc_tree, rhs2, true, region);
}

/* The function returns the expanded reassociation tree for expression
   rooted at ROOT_STMT.  RHS_CODE is the opcode of the gimple assignment
   right hand side.  RHS1 and RHS2 are the operand slots of ROOT_STMT.  */

static lrs_reassoc_tree_p
get_reassoc_tree(gimple root_stmt,
		 enum tree_code rhs_code,
		 treep rhs1, treep rhs2,
		 lrs_region_p region)
{
  lrs_reassoc_tree_p reassoc_tree;
  reassoc_tree
    = (lrs_reassoc_tree_p) pool_alloc (region->lrs_reassoc_tree_pool);
  VEC_safe_push (lrs_reassoc_tree_p, heap,
		 region->lrs_reassoc_trees, reassoc_tree);
  reassoc_tree->opc = rhs_code;
  reassoc_tree->root = root_stmt;
  reassoc_tree->inner_nodes = NULL;
  reassoc_tree->leaf_operands = NULL;
  reassoc_tree->use_ref_slots = NULL;
  VEC_safe_push (gimple, heap, reassoc_tree->inner_nodes,
		 root_stmt);
  gimple_set_visited (root_stmt, true);
  expand_reassoc_tree (reassoc_tree, rhs1, false, region);
  expand_reassoc_tree (reassoc_tree, rhs2, true, region);

  return reassoc_tree;
}

/* The comparison function is used in sorting reassociation tree's operands.
   The operands are sorted according to the order of their defining statements.  */

static basic_block cur_bb = NULL;
static int
reassoc_tree_opnd_cmp (const void *p1, const void *p2)
{
  tree opnd1, opnd2;
  enum tree_code tc1, tc2;

  opnd1 = *(const tree *) p1;
  opnd2 = *(const tree *) p2;
  tc1 = TREE_CODE (opnd1);
  tc2 = TREE_CODE (opnd2);
  if (tc1 != SSA_NAME && tc2 == SSA_NAME)
    return -1;
  else if (tc1 == SSA_NAME && tc2 != SSA_NAME)
    return 1;
  else if (tc1 != SSA_NAME && tc2 != SSA_NAME)
    return SSA_NAME_VERSION (opnd1) - SSA_NAME_VERSION (opnd2);
  else
    {
      gimple stmt1, stmt2;
      basic_block bb1, bb2;

      stmt1 = SSA_NAME_DEF_STMT (opnd1);
      stmt2 = SSA_NAME_DEF_STMT (opnd2);
      bb1 = gimple_bb (stmt1);
      bb2 = gimple_bb (stmt2);

      if (bb1 != cur_bb)
        return -1;
      if (bb2 != cur_bb)
        return 1;
      return get_stmt_order (stmt1) - get_stmt_order (stmt2);
    }
}

/* The comparison function is used in sorting of the internal nodes
   of the reassociation tree.  */

static int
reassoc_inner_node_cmp (const void *p1, const void *p2)
{
  gimple node1, node2;

  node1 = *(const gimple *) p1;
  node2 = *(const gimple *) p2;

  return get_stmt_order (node1) - get_stmt_order (node2);
}


/* The function inserts the use refs from STMT into use ref vector
    USE_REFS.  OPND_SET is a pointer set of use refs.  */

static inline void
append_cur_use_refs (gimple stmt, VEC(treep, heap) **use_refs,
		     struct pointer_set_t * opnd_set)
{
  treep use_ref;

  use_ref = gimple_assign_rhs1_ptr (stmt);
  if (pointer_set_contains (opnd_set, *use_ref))
    VEC_safe_push (treep, heap, *use_refs, use_ref);
  use_ref = gimple_assign_rhs2_ptr (stmt);
  if (pointer_set_contains (opnd_set, *use_ref))
    VEC_safe_push (treep, heap, *use_refs, use_ref);
}

/* The function clears or sets the bits in bitmap BITVEC
   associated with use references in vector USE_REFS.
   VAL is a flag.  When it is non zero, the bits are
   set to 1, otherwise the bits are cleared.  */

static inline void
reset_use_ref_bits (VEC(treep, heap) *use_refs, int val,
		    sbitmap bitvec, lrs_region_p region)
{
  size_t i, n;
  n = VEC_length (treep, use_refs);

  for (i = 0; i < n; i++)
    {
      treep use_ref = VEC_index (treep, use_refs, i);
      int bit_pos;
      if (TREE_CODE (*use_ref) != SSA_NAME)
	continue;
      bit_pos = get_use_ref_bit_pos (use_ref, region);
      if (val)
	SET_BIT (bitvec, bit_pos);
      else
	RESET_BIT (bitvec, bit_pos);
    }
}

/* After operand reassociation, this function is used to remap
   operand slot to the new bit positions associated with
   the old operand slot holding the same value.  REASSOC_TREE
   is the tree that is reassociated, NEW_BIT_POS_MAP is the map
   from operand value to bit position.  */

static void
remap_use_ref_bit_pos (lrs_reassoc_tree_p reassoc_tree,
		       struct pointer_map_t *new_bit_pos_map,
		       lrs_region_p region)
{
  size_t i, n, s = 0;

  n = VEC_length (treep, reassoc_tree->use_ref_slots);

  for (i = s; i < n; i++)
    {
      void **slot;
      tree opnd;
      treep use_ref_slot = VEC_index (treep, reassoc_tree->use_ref_slots, i);

      opnd =  *use_ref_slot;
      slot = pointer_map_contains (new_bit_pos_map, opnd);
      gcc_assert (slot);
      if ((size_t) *slot != (size_t) -1)
        {
          int bit_pos = (long) *slot;
          set_use_ref_bit_pos (use_ref_slot, bit_pos, region);
        }
    }
}

/* The function updates the use-ref data flow result after reassociating
   tree REASSOC_TREE.  OPND_SET is the pointer set of operands.
   NEW_BIT_POS_MAP is the map from operand value to bit position, and REGION
   is the lrs region.  */

static void
update_dataflow_ur_for_reassoc (lrs_reassoc_tree_p reassoc_tree,
				struct pointer_set_t *opnd_set,
				struct pointer_map_t *new_bit_pos_map,
				lrs_region_p region)
{
  gimple_stmt_iterator gsi;
  gimple cur_stmt, prev_stmt, prev_stmt_in_tree;
  size_t n, cur_stmt_idx;
  VEC(treep, heap) *cur_use_refs = NULL;

  /* Remap bit positions  */
  remap_use_ref_bit_pos (reassoc_tree, new_bit_pos_map, region);

  n = VEC_length (gimple, reassoc_tree->inner_nodes);
  cur_stmt_idx = n - 1;

  while (cur_stmt_idx > 0)
    {
      sbitmap use_ref_bvec;
      cur_stmt = VEC_index (gimple, reassoc_tree->inner_nodes, cur_stmt_idx);
      prev_stmt_in_tree
          = VEC_index (gimple, reassoc_tree->inner_nodes, cur_stmt_idx - 1);
      append_cur_use_refs (cur_stmt, &cur_use_refs, opnd_set);
      gsi = gsi_for_stmt (cur_stmt);
      do
        {
          gsi_prev (&gsi);
          prev_stmt = gsi_stmt (gsi);
          use_ref_bvec = get_across_stmt_use_ref_set (prev_stmt, region);
          reset_use_ref_bits (reassoc_tree->use_ref_slots, 0, use_ref_bvec, region);
          reset_use_ref_bits (cur_use_refs, 1, use_ref_bvec, region);
        } while (prev_stmt != prev_stmt_in_tree);

      cur_stmt_idx--;
    }
}

/* The function performs statement update after reassociation of STMT.  */

static void
update_gimple_stmt (gimple stmt, lrs_region_p region)
{
  lrs_stmt_p norm_stmt;

  norm_stmt = get_normalized_gimple_stmt (stmt, region);
  free (norm_stmt->uses);
  norm_stmt->num_uses = 0;
  normalize_gimple_stmt (stmt, norm_stmt);

  /* Now ssa update.  */
  update_stmt (stmt);
}


static void
negate_opnds (void)
{
  size_t n, i;
  if (!pending_negates.opnds_to_negate)
    return;

  gcc_assert (VEC_length (tree, pending_negates.opnds_to_negate)
              == VEC_length (gimple, pending_negates.stmts_to_fixup));

  n = VEC_length (tree, pending_negates.opnds_to_negate);

  for (i = 0; i < n; i++)
    {
      tree negate_result;
      gimple_stmt_iterator gsi;
      gimple insert_point;
      bool insert_before;

      tree opnd_to_negate
          = VEC_index (tree, pending_negates.opnds_to_negate, i);
      gimple stmt_to_fixup
          = VEC_index (gimple, pending_negates.stmts_to_fixup, i);
      gcc_assert (opnd_to_negate == gimple_assign_rhs1 (stmt_to_fixup));
      if (TREE_CODE (opnd_to_negate) != SSA_NAME
          || gimple_code (SSA_NAME_DEF_STMT (opnd_to_negate)) == GIMPLE_PHI)
        {
          insert_before = true;
          insert_point = stmt_to_fixup;
        }
      else
        {
          insert_before = false;
          insert_point = SSA_NAME_DEF_STMT (opnd_to_negate);
        }

      gsi = gsi_for_stmt (insert_point);
      negate_result
          = fold_build1 (NEGATE_EXPR, TREE_TYPE (opnd_to_negate), opnd_to_negate);
      negate_result
          = force_gimple_operand_gsi (&gsi, negate_result, true,
                                      NULL_TREE, insert_before, GSI_SAME_STMT);
      gimple_assign_set_rhs1 (stmt_to_fixup, negate_result);
      update_stmt (stmt_to_fixup);
    }
}

/* The function performs reassociation for expression tree REASSOC_TREE in
   REGION.  After reassociation, more freedom (no dependence violations) is 
   created for code motions to reduce overlapping live ranges.

   Example:

   Before reassociation -- upward code motion (closes to their defs in (1)
   (2) and (3) ) of (4), (5), and (6) are not possible due to dependence 
   violation.  Downward motion of (1), (2), (3) (closest to their uses) is either
   imposible due to possible dependance between (1)/(2)/(3), or not profitable due
   to increased live times of their rhs LRs.

     x  = ...  (1)
     y  = ...  (2)
     z  = ...  (3)

     u = z + 1; (4)
     v = u + y; (5)
     w = v + x; (6)

   After Reassociation:

    x = ...  (1)
    y = ...  (2)
    z = ...  (3)

    u = x + 1;
    v = u + y;
    w = v + z;


   This allows code motion to produce the following code sequence:

    x = ...
    u = x + 1;
    y = ...
    v = u + y;
    z = ...
    w = v + z;
*/

static void
do_lrs_reassoc (lrs_reassoc_tree_p reassoc_tree, lrs_region_p region)
{
  size_t num_operands;
  size_t num_inner_nodes;
  gimple stmt;
  tree opnd, neg_opnd = NULL;
  struct pointer_set_t *opnd_set;
  struct pointer_map_t *new_bpos_map = NULL;
  size_t i, j;
  size_t min_tree;

  num_operands = VEC_length (tree, reassoc_tree->leaf_operands);
  num_inner_nodes = VEC_length (gimple, reassoc_tree->inner_nodes) ;
  gcc_assert (num_inner_nodes + 1 == num_operands);
  min_tree = PARAM_VALUE (PARAM_REG_PRESSURE_MIN_TREE_TO_RESHAPE);

  if (num_operands < min_tree)
    return;

  if (reassoc_tree->opc == MINUS_EXPR)
    neg_opnd = VEC_index (tree, reassoc_tree->leaf_operands, 0);

  cur_bb = gimple_bb (reassoc_tree->root);
  /* Sort the leaf operands.  The operand slots array is not sorted.  */
  qsort (VEC_address (tree, reassoc_tree->leaf_operands), num_operands,
         sizeof (tree), reassoc_tree_opnd_cmp);
  cur_bb = NULL;

  /* Sort the internal tree nodes.  */
  qsort (VEC_address (gimple, reassoc_tree->inner_nodes), num_operands - 1,
         sizeof (gimple), reassoc_inner_node_cmp);

  gcc_assert (VEC_index (gimple, reassoc_tree->inner_nodes, num_operands - 2)
	      == reassoc_tree->root);

  /* Now collect the pointer set of leaf operands.  */
  opnd_set = pointer_set_create ();
  for (i = 0; i < num_operands; i++)
    {
      opnd = VEC_index (tree, reassoc_tree->leaf_operands, i);
      pointer_set_insert (opnd_set, opnd);
    }

  /* Map the operand value to the bit position of the corresponding use-ref
     bit position before reassociation transformation.  The mapping is used to
     remap the new operand slot's bit positions.  */

  new_bpos_map = pointer_map_create ();
  for (i = 0; i < num_operands; i++)
    {
      void **slot;
      treep use_ref;
      tree val;
      long bit_pos;

      use_ref = VEC_index (treep, reassoc_tree->use_ref_slots, i);
      val = *use_ref;
      slot = pointer_map_insert (new_bpos_map, val);
      bit_pos = ((TREE_CODE (val) == SSA_NAME)
		 ? get_use_ref_bit_pos (use_ref, region): -1) ;
      *slot = (void *)bit_pos;
    }

  if (dump_file && (dump_flags & TDF_DETAILS))
    {
      fprintf (dump_file, "REASSOCITING:\n");
      print_lrs_reassoc_tree (dump_file, reassoc_tree);
      fprintf (dump_file, "INTO\n");
    }

  /* Now reaasign the leaf operands to the leaf operand slots.  */
  j = 0;
  for (i = 0; i < num_inner_nodes; i++)
    {
      tree orig_opnd;
      bool need_update = false;
      bool need_neg = (neg_opnd != NULL);

      stmt = VEC_index (gimple, reassoc_tree->inner_nodes, i);
      orig_opnd = gimple_assign_rhs1 (stmt);
      if (pointer_set_contains (opnd_set, orig_opnd))
	{
	  opnd = VEC_index (tree, reassoc_tree->leaf_operands, j++);
	  if (opnd != orig_opnd)
	    {
	      gimple_assign_set_rhs1 (stmt, opnd);
	      need_update = true;

              /* Negation handling */
              /* It is a left linearized tree -- only the first
                 operand can be the left operand.  */
              gcc_assert (!need_neg || orig_opnd == neg_opnd);
              /* insert negating statement.  */
              if (need_neg)
                {
                  VEC_safe_push (tree, heap,
                                 pending_negates.opnds_to_negate, opnd);
                  VEC_safe_push (gimple, heap,
                                 pending_negates.stmts_to_fixup, stmt);
                }
	    }
	}
      orig_opnd = gimple_assign_rhs2 (stmt);
      if (pointer_set_contains (opnd_set, orig_opnd))
	{
	  opnd = VEC_index (tree, reassoc_tree->leaf_operands, j++);
	  if (opnd != orig_opnd)
	    {
	      gimple_assign_set_rhs2 (stmt, opnd);
	      need_update = true;

              if (need_neg && opnd == neg_opnd)
                gimple_assign_set_rhs_code (stmt, PLUS_EXPR);
	    }
	}
      if (need_update)
	update_gimple_stmt (stmt, region);
    }

  if (dump_file && (dump_flags & TDF_DETAILS))
    print_lrs_reassoc_tree (dump_file, reassoc_tree);

  gcc_assert (j == num_operands);

  update_dataflow_ur_for_reassoc (reassoc_tree, opnd_set, new_bpos_map, region);

  pointer_map_destroy (new_bpos_map);
  pointer_set_destroy (opnd_set);
}


/* The function performs reassociation on the expression
   tree rooted at statement STMT in REGION.  */

static void
do_lrs_shrink_by_reassoc (gimple stmt, lrs_region_p region)
{
  tree lhs, *rhs1, *rhs2;
  enum tree_code rhs_code;
  lrs_reassoc_tree_p reassoc_tree;

  if (gimple_visited_p (stmt))
    return;

  if (!is_gimple_assign (stmt))
    return;

  rhs_code = gimple_assign_rhs_code (stmt);

  if (get_gimple_rhs_class (rhs_code) != GIMPLE_BINARY_RHS)
    return;

  lhs = gimple_assign_lhs (stmt);
  rhs1 = gimple_assign_rhs1_ptr (stmt);
  rhs2 = gimple_assign_rhs2_ptr (stmt);

  if (!is_gimple_assign_rhs_reassociable (rhs_code,
					  lhs, *rhs1, *rhs2))
    return;

  reassoc_tree = get_reassoc_tree (stmt, rhs_code,
				   rhs1, rhs2, region);

  do_lrs_reassoc (reassoc_tree, region);
}


/* The function performs expression reassociation for 
   statements in REGION.  */

static void
shrink_lrs_reassociation (lrs_region_p region)
{
  size_t i;

  if (!do_reassoc ())
    return;

  region->lrs_reassoc_tree_pool
      = create_alloc_pool ("lrs linear tree pool",
                           sizeof (struct lrs_reassoc_tree), 50);

  for (i = 0; i < region->num_bbs; i++)
    {
      basic_block bb = region->body[i];
      gimple_stmt_iterator gsi; 
      for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi); gsi_prev (&gsi))
	{
	  gimple stmt = gsi_stmt (gsi);
	  do_lrs_shrink_by_reassoc (stmt, region);
	}
    }
}

/* The function returns true if STMT has float typed constant
   operand.  */

static inline bool
has_float_typed_const_operand (gimple stmt)
{
  unsigned i, n;

  n = gimple_num_ops (stmt);
  for (i = 1; i < n; i++)
    {
      tree op = gimple_op (stmt, i);
      if (is_gimple_constant (op) 
          && FLOAT_TYPE_P (TREE_TYPE (op)))
        return true;
    }
  return false;
}

/* The function returns true if STMT is selected as
   candidate for upward code motion.  */

static bool
is_upward_motion_candidate (gimple stmt)
{
  if (!is_gimple_assign (stmt) )
    return false;

  if (!ZERO_SSA_OPERANDS (stmt, SSA_OP_ALL_VIRTUALS))
    return false;

  if (has_float_typed_const_operand (stmt))
    return false;

  return true;
}


/* The function computes the number of LRs that are not live after
   statement NORM_STMT (with id STMT_ID) in REGION.  *NGR is the 
   number GRs that are not live across, and *NFR is the number of FRs 
   that are not live across.  The number of gr uses is in *NGR_USE, and
   *NFR_USE is the number of fr uses.  */

static void
get_num_lrs_not_live_across (lrs_stmt_p norm_stmt, int stmt_id,
                             sbitmap use_ref_set, lrs_region_p region,
                             int *ngr, int *nfr, int *ngr_use, int *nfr_use)
{
  size_t i;

  *ngr = 0;
  *nfr = 0;
  *ngr_use = 0;
  *nfr_use = 0;

  if (norm_stmt->num_uses == 0)
    return;

  if (use_ref_set == NULL)
    use_ref_set = region->across_stmt_use_ref_sets[stmt_id];

  for (i = 0; i < norm_stmt->num_uses; i++)
    {
      bool is_live_across = true;
      size_t first_bit, last_bit;
      tree *use = norm_stmt->uses[i];

      get_def_bit_range (&first_bit, &last_bit, *use, region);
      if (sbitmap_range_empty_p (use_ref_set, first_bit,
                                 last_bit - first_bit + 1))
        is_live_across = false;

      if (get_nm_reg_class (*use) == lrc_gr)
	{
	  (*ngr_use)++;
	  if (!is_live_across)
	    (*ngr)++;
	}
      else
	{
	  (*nfr_use)++;
	  if (!is_live_across)
	    (*nfr)++;
	}
    }
}

/* The function computes the number of GR defs (returned in *D_GR), 
   and the number of FR defs (in *D_FR) in statement NORM_STMT.  */

static inline void
get_num_lrs_defed_by (lrs_stmt_p norm_stmt, int *d_gr, int *d_fr)
{
  *d_gr = 0;
  *d_fr = 0;

  if (norm_stmt->def)
    {
      if (get_nm_reg_class (norm_stmt->def) == lrc_gr)
        (*d_gr)++;
      else
        (*d_fr)++;
    }
}

/* The function computes the register pressure changes in the program
   point between the STMT1 and STMT2 if two statements are swapped.
   STMT1 precedes STMT2 in the code stream.  The result will be stored
   into the output parameter *GR_CHANGE and *FR_CHANGE.  A positive value
   indicates regiter pressure reduction.  REGION is the code region where
   the optimization is performed.

   The formular for computing the register pressure change (reduction) is:

   reg_pressure_change (stmt1, stmt2, reg_class)
      = num_of_lrs_not_live_across (stmt2, reg_class)
        * num_lrs_defined (stmt1, reg_class)
      - num_of_lrs_not_live_across (stmtl, reg_class)
        * num_lrs_defined (stmt2, reg_class)


   More precisely, num_of_lrs_not_live_across (stmt1, reg_class) should
   actually be the number of LRs referenced in stmt1 that is not live across
   stmt2.  For instance:

   stmt1:  a = b    + c
   stmt2:  x = b(N) + y;

   The reference of b in stmt2 is the last reference -- so b is live across stmt1,
   but not live across stmt2.  However if stmt1 and stmt2 is swapped, new interference
   will be introduced between x and b.

   There are three program points related to the two statements that are to
   be swapped, and register pressure in the program points before and after
   the two statements do not change.

   In the following examples, LRs that do not live across the statement are
   marked with (N).  Program points are Pnn:, and live LRs are in curly braces.

   Example 1: register pressure is reduced after swapping:

   Before code motion
   ------------------

    P1: {w, v, y, z},    reg_pressure = 4
      u = w + v;
    P2: {u, w, v, y, z}, reg_pressure = 5
      x = y + z(N);
    P3: {u, x, w, v, y}, reg_pressure = 5

   After code motion
   ----------------- 

    P1: {w, v, y, z},    reg_pressure = 4
      x = y + z(N);
    P2: {x, y, w, v},    reg_pressure = 4
      u = w + v;
    P3: {u, x, w, v, y}, reg_pressure = 5



   Example 2: register pressure is reduced after swapping:

   Before code motion
   ------------------

    P1: {w, v, y, z},    reg_pressure = 4
      u = w + v;
    P2: {u, w, v, y, z}, reg_pressure = 5
      -- = y + z(N);
    P3: {u, w, v, y},    reg_pressure = 4

   After code motion
   ----------------- 

    P1: {w, v, y, z},    reg_pressure = 4
     -- = y + z(N);
    P2: { y, w, v},      reg_pressure = 3
      u = w + v;
    P3: {u, w, v, y},    reg_pressure = 4


   Example 3: register pressure does not change after swapping:

   Before code motion
   ------------------

    P1: {w, v, y, z},    reg_pressure = 4
      u = w(N) + v;
    P2: {u, v, y, z},    reg_pressure = 4
      x = y + z(N);
    P3: {x, u, v, y},    reg_pressure = 4

   After code motion
   ----------------- 

    P1: {w, v, y, z},    reg_pressure = 4
      x = y + z(N);
    P2: {x, y, w, v},    reg_pressure = 4
      u = w(N) + v;
    P3: {x, u, v, y},    reg_pressure = 4

   Example 4: register pressure is reduced after swapping:

   Before code motion
   ------------------

    P1: {w, v, y, z},    reg_pressure = 4
      u = w + v;
    P2: {u, w, v, y, z}, reg_pressure = 5
      x = y(N) + z(N);
    P3: {x, u, w, v},    reg_pressure = 4

   After code motion
   -----------------

    P1: {w, v, y, z},    reg_pressure = 4
      x = y(N) + z(N);
    P2: {x, w, v},       reg_pressure = 3
      u = w + v;
    P3: {x, u, w, v},    reg_pressure = 4

  Example 4: register pressure is increased after swapping:

   Before code motion
   ------------------

    P1: {w, v, y, z},    reg_pressure = 4
      -- = w(N) + v;
    P2: { v, y, z},      reg_pressure = 3
      x = y + z;
    P3: {x, v, y, z},    reg_pressure = 4

   After code motion
   -----------------

    P1: {w, v, y, z},    reg_pressure = 4
      x = y + z;
    P2: {x, y, z, w, v}, reg_pressure = 5
      - = w(N) + v;
    P3: {x, v, y, z},    reg_pressure = 4

*/

static void
get_reg_pressure_change_if_swapped (int stmt_id1, lrs_stmt_p norm_stmt1,
                                    int stmt_id2, lrs_stmt_p norm_stmt2,
                                    sbitmap stmt2_use_ref_set,
                                    lrs_region_p region,
                                    int *gr_change, int *fr_change)

{
  int non_live_across_gr1, non_live_across_fr1;
  int non_live_across_gr2, non_live_across_fr2;
  int ngr_def1, nfr_def1;
  int ngr_def2, nfr_def2;
  int ngr_use1, nfr_use1;
  int ngr_use2, nfr_use2;

  get_num_lrs_not_live_across (norm_stmt1, stmt_id1,
                               stmt2_use_ref_set, region,
                               &non_live_across_gr1,
                               &non_live_across_fr1,
			       &ngr_use1, &nfr_use1);

  get_num_lrs_not_live_across (norm_stmt2, stmt_id2,
                               stmt2_use_ref_set, region,
                               &non_live_across_gr2,
                               &non_live_across_fr2,
			       &ngr_use2, &nfr_use2);

  get_num_lrs_defed_by (norm_stmt1, &ngr_def1, &nfr_def1);
  get_num_lrs_defed_by (norm_stmt2, &ngr_def2, &nfr_def2);


  *gr_change = (non_live_across_gr2 * ngr_def1
                - non_live_across_gr1 * ngr_def2);

  *fr_change = (non_live_across_fr2 * nfr_def1
                - non_live_across_fr1 * nfr_def2);
}

/*  STMT is a candidate for updward code motion.  The function computes
   the earliest insertion point for the code motion.  EARLIEST_STMT is the
   earliest statement in the same bb where STMT can be moved across (inserted
   before).  *INSERT_POINT is the earliest possible statement STMT can be
   inserted before without increasing register pressure.
   If no such insertion point can be found, *INSERT_POINT  is set to NULL.  */

static void
compute_earliest_insertion_point (gimple stmt, gimple earliest_stmt,
                                  lrs_region_p region,
                                  gimple *insert_point)
{
  gimple_stmt_iterator gsi;
  gimple best_target_loc = NULL;
  gimple cur_stmt;
  int tot_reg_reduc = 0, max_reg_reduc = 0;
  int tot_gr_reduc = 0, max_gr_reduc = 0;
  int tot_fr_reduc = 0, max_fr_reduc = 0;
  sbitmap stmt_use_ref_set = NULL;
  int stmt_id;
  lrs_stmt_p norm_stmt;

  gsi = gsi_for_stmt (stmt);
  gsi_prev (&gsi);
  stmt_use_ref_set = sbitmap_alloc (region->bitvec_width);
  stmt_id = get_stmt_idx_in_region (stmt);
  norm_stmt = get_normalized_gimple_stmt (stmt, region);
  sbitmap_copy (stmt_use_ref_set, region->across_stmt_use_ref_sets[stmt_id]);

  do
  {
    int cur_stmt_id;
    lrs_stmt_p cur_norm_stmt;
    int gr_reg_cg = 0;
    int fr_reg_cg = 0;
    size_t i, n;

    cur_stmt = gsi_stmt (gsi);
    cur_stmt_id = get_stmt_idx_in_region (cur_stmt);
    cur_norm_stmt = get_normalized_gimple_stmt (cur_stmt, region);
    get_reg_pressure_change_if_swapped (cur_stmt_id, cur_norm_stmt,
                                        stmt_id, norm_stmt,
                                        stmt_use_ref_set, region,
                                        &gr_reg_cg, &fr_reg_cg);

    tot_reg_reduc += gr_reg_cg;
    tot_reg_reduc += fr_reg_cg;
    tot_gr_reduc += gr_reg_cg;
    tot_fr_reduc += fr_reg_cg;

    if (tot_reg_reduc >= max_reg_reduc)
      {
        max_reg_reduc = tot_reg_reduc;
        best_target_loc = cur_stmt;
      }

    if (tot_gr_reduc >= max_gr_reduc)
      max_gr_reduc = tot_gr_reduc;
    if (tot_fr_reduc >= max_fr_reduc)
      max_fr_reduc = tot_fr_reduc;

    /* Now update the use-ref set for stmt as if it is moved across (up)
       cur_stmt.  */

    n = cur_norm_stmt->num_uses;
    for (i = 0; i < n; i++)
      {
        int bit_pos;
        tree *use = cur_norm_stmt->uses[i];
        bit_pos = get_use_ref_bit_pos (use, region);
        SET_BIT (stmt_use_ref_set, bit_pos);
      }
    if (cur_norm_stmt->def)
      {
        size_t fbit, lbit;
        get_def_bit_range (&fbit, &lbit, cur_norm_stmt->def, region);
        for (i = fbit; i <= lbit; i++)
          RESET_BIT (stmt_use_ref_set, i);
      }

    gsi_prev (&gsi);
  } while (cur_stmt != earliest_stmt);

  if (max_reg_reduc > 0)
    *insert_point = best_target_loc;
  else
    *insert_point = NULL;

  sbitmap_free (stmt_use_ref_set);

  if (dump_file && (dump_flags & TDF_DETAILS))
    {
      fprintf (dump_file, "[CHECKING CODE MOTION]:\n");
      fprintf (dump_file, "\t[FROM] : ");
      print_gimple_stmt (dump_file, stmt, 0, 0);
      fprintf (dump_file, "\t[TO] : ");
      print_gimple_stmt (dump_file, earliest_stmt, 0, 0);
      fprintf (dump_file, "\t[RESULT]:\n");
      fprintf (dump_file, "\t[BEST TARGET] : ");
      if (*insert_point)
        print_gimple_stmt (dump_file, *insert_point, 0, 0);
      else
        fprintf (dump_file, "NO LOC \n");
      fprintf (dump_file, 
               "\tREG pressure reduction TOT : %d, MAX : %d\n",
               tot_reg_reduc, max_reg_reduc);
      fprintf (dump_file, 
               "\tGR pressure reduction TOT : %d, MAX : %d\n",
               tot_gr_reduc, max_gr_reduc);
      fprintf (dump_file, 
               "\tFR pressure reduction TOT : %d, MAX : %d\n",
               tot_fr_reduc, max_fr_reduc);

    }
}

/* The function computes the cost and savings in terms of
   register pressure reductions if STMT_TO_MOVE is moved to
   TARGET_LOC  (inserted before if IS_BEFORE is true and afer 
   if IS_BEFORE is false).  The cost of the upward code motion 
   is approximited by the total number of new interferences 
   that will be introduced and the savings is approximated by 
   the total number interferences that can be eliminated when 
   the code motion happens.  The function returns the gimple 
   statement before which STMT_TO_MOVE can be inserted or NULL
   if no profitable location can be found.  */

static gimple
check_upward_motion_profitability (gimple target_loc,
                                   gimple stmt_to_move,
                                   bool is_before,
                                   lrs_region_p region)
{
  gimple_stmt_iterator gsi;
  gimple adjusted_target_loc = NULL;

  basic_block bb = gimple_bb (target_loc);

  /* Only handle this for now.  */
  gcc_assert (gimple_bb (stmt_to_move) == bb);

  gsi = gsi_for_stmt (target_loc);
  if (!is_before)
    {
      gsi_next (&gsi);
      target_loc = gsi_stmt (gsi);
    }

  if (target_loc == stmt_to_move)
    return NULL;

  compute_earliest_insertion_point (stmt_to_move, target_loc,
                                    region, &adjusted_target_loc);

  return adjusted_target_loc;
}

/* This function adjusts the insertion point TARGET for
   the statement to be move ME and returns the new insertion
   point or NULL if no insertion point is appropriate.
   *INSERT_BEFORE is a flag indicating if the new insertion point
   is before or after returned gimple statement.  The client of
   this interface is responsible to set the initial value
   of the flag.  */

static gimple
check_move_target_loc (gimple target, gimple me, bool *insert_before)
{
  basic_block target_bb = 0;
  basic_block me_bb = 0;
  gimple_stmt_iterator gsi;
  gimple last_label = 0;

  /* Bail if the target stmt is a call with EH.  */
  if (stmt_ends_bb_p (target) && !*insert_before)
    return 0;

  if (gimple_code (target) == GIMPLE_PHI)
    {
      basic_block bb = gimple_bb (target);
      gsi = gsi_start_bb (bb);
      if (gsi_end_p (gsi))
        return 0;

      target = gsi_stmt (gsi);
      *insert_before = true;
    }
  else
    gsi = gsi_for_stmt (target);

  while (gimple_code (target) == GIMPLE_LABEL)
    {
      last_label = target;
      gsi_next (&gsi);
      if (gsi_end_p (gsi))
        break;
      target = gsi_stmt (gsi);
    }

  if (last_label)
    {
      *insert_before = false;
      target = last_label;
    }

  /* We do not really want to introduce redundancy nor do we need
     to do LIM here.  */

  target_bb = gimple_bb (target);
  me_bb = gimple_bb (me);

  if (target_bb->loop_father != me_bb->loop_father 
      || !dominated_by_p (CDI_POST_DOMINATORS, target_bb, me_bb))
    {
      *insert_before = true;
      return check_move_target_loc (gsi_stmt (gsi_start_bb (me_bb)),
                                    me, insert_before);
    }

  /* Do not handle this for now.  */
  if  (me_bb != target_bb)
    {
      *insert_before = true;
      return check_move_target_loc (gsi_stmt (gsi_start_bb (me_bb)),
                                    me, insert_before);
    }

  return target;
}

/* The function returns the defining statement for SSAVAR if
   it is dominated by CUR_LATEST.  Otherwise CUR_LATEST is returned.  */

static gimple
get_cur_latest_def (tree ssavar, gimple cur_latest)
{
  gimple cur_def = 0;
  cur_def = SSA_NAME_DEF_STMT (ssavar);

  if (cur_def && gimple_nop_p (cur_def))
    cur_def = 0;

  if (!cur_def)
    return cur_latest;

  if (!cur_latest)
    return cur_def;

  if (is_dominating (cur_latest, cur_def))
    return cur_def;

  return cur_latest;
}

/* The function returns the closest defining statement for 
   all the used rhs operands of STMT.  This function is only
   used by the upward code motion transformation in which
   statements with VUSES are not candidates, so VUSES are 
   not examined here.  See also is_upward_motion_candidate. 
   REGION is the code region being optimized.  */

static gimple
get_closest_def (gimple stmt, lrs_region_p region)
{
  int i, n;
  lrs_stmt_p norm_stmt;
  gimple cur_latest_def = 0;

  norm_stmt = get_normalized_gimple_stmt (stmt, region);

  n = norm_stmt->num_uses;
  for (i = 0; i < n; i++)
    {
      tree *op = norm_stmt->uses[i];
      cur_latest_def = get_cur_latest_def (*op, cur_latest_def);
    }

  if (!cur_latest_def)
    {
      gimple_stmt_iterator gsi0
          = gsi_start_bb (single_succ (ENTRY_BLOCK_PTR));
      cur_latest_def
          = gsi_end_p (gsi0)? 0 : gsi_stmt (gsi0);
    }

  return cur_latest_def;
}

/* The function updates the use-ref data flow information for all
   statements in the region enclosed by [FIRST_STMT_GSI, END_STMT_GSI)
   as well as the bitvector for the statement that is moved :
   MOVED_STMT_GSI.  The region is REGION.  */

static void
update_data_flow (gimple_stmt_iterator moved_stmt_gsi,
                  gimple_stmt_iterator first_stmt_gsi,
                  gimple_stmt_iterator end_stmt_gsi,
                  lrs_region_p region)
{
  gimple_stmt_iterator gsi;
  gimple_stmt_iterator gsi_prev_stmt;
  gimple moved_stmt;
  gimple first_stmt;
  gimple curr_stmt;
  gimple prev_stmt;
  gimple end_stmt;
  lrs_stmt_p norm_moved_stmt;
  sbitmap live_across_curr;
  sbitmap live_across_moved;
  sbitmap live_across_prev;
  size_t i, n, fbit, lbit;

  moved_stmt = gsi_stmt (moved_stmt_gsi);
  norm_moved_stmt = get_normalized_gimple_stmt (moved_stmt, region);
  n = norm_moved_stmt->num_uses;
  get_def_bit_range (&fbit, &lbit, norm_moved_stmt->def, region);
  live_across_moved
      = get_across_stmt_use_ref_set (moved_stmt, region);

  if (gsi_end_p (end_stmt_gsi))
    end_stmt = NULL;
  else
    end_stmt = gsi_stmt (end_stmt_gsi);

  gsi = first_stmt_gsi;
  curr_stmt = gsi_stmt (gsi);
  do
    {
      live_across_curr
          = get_across_stmt_use_ref_set (curr_stmt, region);
      for (i = 0; i < n; i++)
        {
          int bit_pos;
          tree *use = norm_moved_stmt->uses[i];
          bit_pos = get_use_ref_bit_pos (use, region);
          RESET_BIT (live_across_curr, bit_pos);
        }

      if (norm_moved_stmt->def)
        {
          for (i = fbit; i <= lbit; i++)
            SET_BIT (live_across_curr, i);
        }

      gsi_next (&gsi);
      curr_stmt = (gsi_end_p (gsi) ? NULL : gsi_stmt (gsi));

    } while (curr_stmt != end_stmt);

  /* Now update the live across set for the statement
     that is moved.  */

  gsi_prev_stmt = moved_stmt_gsi;
  gsi_prev (&gsi_prev_stmt);
  if (gsi_end_p (gsi_prev_stmt))
    prev_stmt = NULL;
  else
    prev_stmt = gsi_stmt (gsi_prev_stmt);
  if (prev_stmt)
    live_across_prev
        = get_across_stmt_use_ref_set (prev_stmt, region);
  else
    {
      int bidx;
      basic_block bb = gimple_bb (moved_stmt);
      get_bb_index_in_region (bb, region, &bidx);
      live_across_prev
          = get_bb_use_ref_in_set (bidx, region);
    }

  sbitmap_copy (live_across_moved, live_across_prev);

  /* Now the final adjustment  */

  for (i = 0; i < n; i++)
    {
      int bit_pos;
      tree *use = norm_moved_stmt->uses[i];
      bit_pos = get_use_ref_bit_pos (use, region);
      RESET_BIT (live_across_moved, bit_pos);
    }
  if (norm_moved_stmt->def)
    {
      for (i = fbit; i <= lbit; i++)
        SET_BIT (live_across_moved, i);
    }

  /* Now update the order for the statement that
     is just moved -- it gets the same order as the
     statement it is inserted after/before.  */
  first_stmt = gsi_stmt (first_stmt_gsi);
  reset_stmt_order (moved_stmt, get_stmt_order (first_stmt));
}

/* The function performs code motion for the statement
   pointed to by GSI_CM_STMT and returns the iterator to
   the next statement before the code motion.  */

static gimple_stmt_iterator
do_lr_shrink_by_use_hoisting (gimple_stmt_iterator gsi_cm_stmt, 
                              lrs_region_p region)
{
  gimple cm_stmt, earliest;
  gimple_stmt_iterator gsi_next_stmt;
  gimple_stmt_iterator gsi_earliest;

  bool insert_before = false;

  cm_stmt = gsi_stmt (gsi_cm_stmt);
  gsi_next_stmt = gsi_cm_stmt;
  gsi_next (&gsi_next_stmt);

  if (!is_upward_motion_candidate (cm_stmt))
    return gsi_next_stmt;

  earliest = get_closest_def (cm_stmt, region);

  if (!earliest)
    return gsi_next_stmt;

  insert_before = false;
  earliest = check_move_target_loc (earliest, cm_stmt, &insert_before);

  if (!earliest || earliest == cm_stmt)
    return gsi_next_stmt;

  earliest = check_upward_motion_profitability (earliest, cm_stmt, 
                                                insert_before, region);
  if (earliest == NULL)
    return gsi_next_stmt;

  if (!dbg_cnt (lrs))
    return gsi_next_stmt;

  gsi_earliest = gsi_for_stmt (earliest);

  /* The insertion point is adjusted by is_upwared_motion_beneficial
     such that it is before earliest.  */
  gsi_move_before (&gsi_cm_stmt, &gsi_earliest);

  update_data_flow (gsi_for_stmt (cm_stmt), 
                    gsi_for_stmt (earliest),
                    gsi_next_stmt, region);

  if (dump_file && (dump_flags & TDF_DETAILS))
    {
      fprintf (dump_file, "Moved UP\n");
      print_gimple_stmt (dump_file, cm_stmt, 0, 0);
      fprintf (dump_file, "just before \n");
      print_gimple_stmt (dump_file, earliest, 0, 0);
      fprintf (dump_file, "\n");
    }

  return gsi_next_stmt;
}


/* The function attempts to reduce the number of
   overlapping live ranges in BB by performing
   upward code motion for statements.  */

static void
shrink_lrs_up_motion (lrs_region_p region)
{
  size_t i;

  if (!do_upward_motion ())
    return;

  for (i = 0; i < region->num_bbs; i++)
    {
      basic_block bb = region->body[i];
      gimple_stmt_iterator gsi = gsi_start_bb (bb);
      while (!gsi_end_p (gsi))
        gsi = do_lr_shrink_by_use_hoisting (gsi, region);
    }

  dump_data_flow_result (region, "After upward motion");
}


/* This is the comparison function for sorting all uses
   of a definition.  The sorting is based on the order of 
   the use statements in the basic block.  Sorting is to 
   allow closest use to be easily found.  Note that the 
   BB of the use in a phi operand  is in the src bb of 
   the associatied edge.  */

static int
use_stmt_pos_cmp (const void *p1, const void *p2)
{
  basic_block b1, b2;
  gimple s1, s2, phi, s;
  use_operand_p phi_use;
  enum gimple_code c1, c2;
  int idx;
  edge use_edge;
  const use_operand_p u1 = *(const use_operand_p *)p1;
  const use_operand_p u2 = *(const use_operand_p *)p2;

  s1 = u1->loc.stmt;
  s2 = u2->loc.stmt;
  c1 = gimple_code (s1);
  c2 = gimple_code (s2);

  if (c1 != GIMPLE_PHI && c2 != GIMPLE_PHI)
    {
      if (is_dominating (s1, s2))
        return -1;
      else if (is_dominating (s2, s1))
        return 1;

      b1 = gimple_bb (s1);
      b2 = gimple_bb (s2);

      return b2->index - b1->index;
    }

  if (c1 == GIMPLE_PHI && c2 == GIMPLE_PHI)
    return gimple_bb (s2)->index - gimple_bb (s1)->index;

  if (c1 == GIMPLE_PHI)
    {
      phi = s1;
      phi_use = u1;
      s = s2;
    }
  else
    {
      phi = s2;
      phi_use = u2;
      s = s1;
    }

  idx = PHI_ARG_INDEX_FROM_USE (phi_use);
  use_edge = gimple_phi_arg_edge (phi, idx);

  b1 = gimple_bb (s);
  b2 = use_edge->src;

  if (b1 == b2 || dominated_by_p (CDI_DOMINATORS, b2, b1))
    {
      if (s == s1)
        return -1;
      else
        return 1;
    }

  return b2->index - b1->index;
}


DEF_VEC_P(use_operand_p);
DEF_VEC_ALLOC_P(use_operand_p, heap);

/* The function returns the closest use statement of STMT's
   definitions that also dominate all other uses.  STMT should 
   be a gimple assignment.  REGION is the code region under 
   optimization.  */

static gimple
get_closest_use (gimple stmt)
{
  int i, n;
  gimple closest_use = 0;
  use_operand_p use_p;
  imm_use_iterator iter;
  VEC(use_operand_p, heap) *uses = NULL;
  bool is_phi;
  basic_block bb1;

  tree nm = gimple_assign_lhs (stmt);

  FOR_EACH_IMM_USE_FAST (use_p, iter, nm)
    VEC_safe_push (use_operand_p, heap, uses, use_p);

  n = VEC_length (use_operand_p, uses);
  if (!n)
    return NULL;

  if (n == 1)
    return VEC_index (use_operand_p, uses, 0)->loc.stmt;

  qsort (VEC_address (use_operand_p, uses), n,
         sizeof (use_operand_p), use_stmt_pos_cmp);

  closest_use = VEC_index (use_operand_p, uses, 0)->loc.stmt;
  is_phi = (gimple_code (closest_use) == GIMPLE_PHI);
  if (is_phi && n > 1)
    return NULL;

  bb1 = gimple_bb (closest_use);
  for (i = 1; i < n; i++)
    {
      gimple cur_use_stmt;
      use_operand_p use = VEC_index (use_operand_p, uses, i);
      cur_use_stmt = use->loc.stmt;
      if (gimple_code (cur_use_stmt) == GIMPLE_PHI)
        {
          int idx;
          edge use_edge;

          idx = PHI_ARG_INDEX_FROM_USE (use);
          use_edge = gimple_phi_arg_edge (cur_use_stmt, idx);
          if (bb1 != use_edge->src 
              && !dominated_by_p (CDI_DOMINATORS, use_edge->src, bb1))
            return NULL;
        }
      else if (!is_dominating (closest_use, cur_use_stmt))
        return NULL;
    }

  return closest_use;
}

/* The function examine the downward code motion target location TARGET,
   and returns the adjusted location.  ME is the statement to be moved.
   If the insertion point is after the adjusted location, *INSERT_AFTER is
   set to true.  */

static gimple
check_down_motion_target_loc (gimple target, gimple me, 
                              bool *insert_after, lrs_region_p region)
{
  basic_block target_bb = 0;
  basic_block me_bb = 0;
  int target_bb_rid = -1;

  gcc_assert (gimple_code (target) != GIMPLE_LABEL);

  if (gimple_code (target) == GIMPLE_PHI)
    {
      /* we have to scan the phi operand to find the matching
         edge.  If multiple operands in the phi uses the same def, 
         then the phi would not be chosen as an insertion point --
         see get_closest_use.  */
      int i, n;
      bool found = false;
      tree def = gimple_assign_lhs (me);
      n = gimple_phi_num_args (target);
      for (i = 0; i < n; i++)
        {
          if (gimple_phi_arg_def (target, i) == def)
            {
              edge e;
              gimple_stmt_iterator target_gsi;
              found = true;
              e = gimple_phi_arg_edge (target, i);
              target_bb = e->src;
              target_gsi = gsi_last_bb (target_bb);
              *insert_after = true;
              target = gsi_stmt (target_gsi);
              break;
            }
        }
      gcc_assert (found);
      if (!target)
        return NULL;
      /* fall through for the rest the adjustment.  */
    }

  if (gimple_code (target) == GIMPLE_PHI 
      || gimple_code (target) == GIMPLE_LABEL)
    return NULL;

  /* move before the target stmt if it is a call with EH.  */
  if (stmt_ends_bb_p (target) && *insert_after)
    *insert_after = false;

  target_bb = gimple_bb (target);
  me_bb = gimple_bb (me);

  if (!get_bb_index_in_region (target_bb, region, &target_bb_rid)
      || !dominated_by_p (CDI_DOMINATORS, target_bb, me_bb))
    {
      *insert_after = true;
      return check_down_motion_target_loc (gsi_stmt (gsi_last_bb (me_bb)),
                                           me, insert_after, region);
    }

  return target;
}

/* Return the bitvector of reaching VDEFS at the program point
   before (when IS_AFTER is false) or after (when IS_AFTER is true)
   the TARGET_LOC statement.  */

static sbitmap
get_reaching_vdefs (gimple target_loc, bool is_after,
                    lrs_region_p region)
{
  sbitmap reaching_defs;

  if (!is_after)
    reaching_defs = get_stmt_rd_set (target_loc, region);
  else
    {
      gimple_stmt_iterator gsi = gsi_for_stmt (target_loc);
      gsi_next (&gsi);
      if (!gsi_end_p (gsi))
        {
          gimple nstmt = gsi_stmt (gsi);
          reaching_defs = get_stmt_rd_set (nstmt, region);
        }
      else
        {
          int rid;
          basic_block bb = gimple_bb (target_loc);
          get_bb_index_in_region (bb, region, &rid);
          reaching_defs = get_bb_rd_out_set (rid, region);
        }
    }
  return reaching_defs;
}

/* Stack layout in cfgexpand.c performs stack reuse/overlay on
   stack variables that do not conflict. However variable conflicit
   computation is not based on variable life time overlap analsysis,
   but on information of variable scopes -- a variable conflicts with
   another variable in the same scope or a nested scope. Two variables
   won't conflict if they are in different scopes not nested with each
   other. The assumption is that no optimization will introduce life time
   overlap for stack variables in different scopes.  Return true if
   STMT_TO_MOVE reference a stack variable that may be a candidate for
   stack reuse. */
static bool
reference_overlapable_stack_variable_p (gimple stmt_to_move)
{
  enum tree_code gc;
  tree var;

  gcc_assert (is_gimple_assign (stmt_to_move));
  gc = gimple_assign_rhs_code (stmt_to_move);
  /* We do not care about PARM_DECL as they are in the top level scope.
     Should probably also filter out top level local VAR_DECLS.  */
  if (gc != VAR_DECL)
    return false;

  var = gimple_assign_rhs1 (stmt_to_move);

  if (TREE_STATIC (var) || DECL_EXTERNAL (var))
    return false;

  if (DECL_ARTIFICIAL (var))
    return false;

  return true;
}

/* The function checks to see if there are possible violations 
   of anti-depedency (memory) with this move.  Returns true if 
   there is none.  TARGET_LOC is the statement before/after which 
   statement STMT_TO_MOVE is to be moved.  IS_AFTER is the flag.  If
   it is true, the insertion point is after TARGET LOC, otherwise
   it is before it.  */

static bool
is_down_motion_legal (gimple target_loc, gimple stmt_to_move,
                      bool is_after, lrs_region_p region)
{
  sbitmap reaching_defs;
  struct voptype_d *vuses;
  int i, n;

  gcc_assert (!gimple_vdef_ops (stmt_to_move));

  if (!(vuses = gimple_vuse_ops (stmt_to_move)))
    return true;

  if (reference_overlapable_stack_variable_p (stmt_to_move))
    return false;

  reaching_defs = get_reaching_vdefs (target_loc, is_after, region);

  while (vuses)
    {
      n = VUSE_NUM (vuses);
      for (i = 0; i < n; i++)
        {
          size_t first, last, j, bpos;
          tree vvar;
          tree vop = VUSE_OP (vuses, i);
          vvar = SSA_NAME_VAR (vop);
          bpos = get_vnm_bit_pos (vop, region);
          get_vvar_bit_range (&first, &last, vvar, region);
          for (j = first; j <= last; j++)
            {
              if (TEST_BIT (reaching_defs, j) && (j != bpos))
                return false;
            }
        }
      vuses = vuses->next;
    }
  return true;
}

/* The function returns true if all gimple statements defining
   inner nodes of the expression tree LRS_TREE can be legally
   moved to the target location TARGET_LOC.  IS_AFTER is a flag.
   if it is true, the target location is after TARGET_LOC, 
   otherwise it is before.  */

static bool
ok_to_sched_down (lrs_tree_p lrs_tree, gimple target_loc,
                  bool is_after, lrs_region_p region)
{
  int i = 0;
  int n = VEC_length (gimple, lrs_tree->tree_nodes);

  for (i = 0; i < n; i++)
    {
      gimple to_move = VEC_index (gimple, lrs_tree->tree_nodes, i);
      if (!is_down_motion_legal (target_loc, to_move,
                                 is_after, region))
        return false;
    }
  return true;
}

/* The function returns true if it is benefitial to move tree
   SUB_TREE downward.  */

static inline bool
profitable_to_sched_down (lrs_tree_p sub_tree)
{
  return (sub_tree->num_leaves_not_live_across  == 0);
}

/* For an expression tree LRS_TREE_TO_MOVE whose value has
   multiple uses, this function is used to examine if it is 
   profitable (overlapping live range reduction) move it 
   downward to target location TARGET_LOC.  It it is profitable,
   TARGET_LOC is returned, otherwise the function returns NULL. 
   IS_AFTER is a flag. If it is true, the target location is 
   after TARGET_LOC.  */

static gimple
check_down_motion_profitability (gimple target_loc,
                                 lrs_tree_p lrs_tree_to_move,
                                 bool is_after,
                                 lrs_region_p region)
{
  gimple_stmt_iterator gsi;
  sbitmap live_ur_set;
  int i, n;
  bool use_live_across_target = true;

  if (!profitable_to_sched_down (lrs_tree_to_move))
    return NULL;

  if (is_after)
      live_ur_set = get_across_stmt_use_ref_set (target_loc, region);
  else
    {
      gsi = gsi_for_stmt (target_loc);
      gsi_prev (&gsi);
      if (!gsi_end_p (gsi))
        {
          gimple pstmt = gsi_stmt (gsi);
          live_ur_set = get_across_stmt_use_ref_set (pstmt, region);
        }
      else
        {
          basic_block bb;
          int rid;
          bb = gimple_bb (target_loc);
          get_bb_index_in_region (bb, region, &rid);
          live_ur_set = get_bb_use_ref_in_set (rid, region);
        }
    }

  n = VEC_length (tree, lrs_tree_to_move->leaf_nodes);
  if (n == 0)
    return target_loc;

  for (i = 0; i < n; i++)
    {
      size_t first, last;
      tree use = VEC_index (tree, lrs_tree_to_move->leaf_nodes, i);
      get_def_bit_range (&first, &last, use, region);
      if (sbitmap_range_empty_p (live_ur_set, first, last - first + 1))
        {
          use_live_across_target = false;
          break;
        }
    }

  if (use_live_across_target)
    return target_loc;

  return NULL;
}

static lrs_tree_p
find_lrs_tree (gimple, lrs_region_p);

/* The function performs downward code motion on expression
   tree whose value have multiple uses.  The root of the tree
   is the gimple statement pointed to by the iteration
   GSI_CM_STMT.  */

static gimple_stmt_iterator
sink_multi_use_def (gimple_stmt_iterator gsi_cm_stmt,
                    lrs_region_p region)
{
  gimple cm_stmt, latest;
  gimple_stmt_iterator gsi_prev_stmt;
  gimple_stmt_iterator gsi_target;
  lrs_tree_p cm_stmt_lrs_tree;
  int j, k;
  bool insert_after = false;
  size_t target_order;
  sbitmap reaching_defs;

  cm_stmt = gsi_stmt (gsi_cm_stmt);
  gsi_prev_stmt = gsi_cm_stmt;
  gsi_prev (&gsi_prev_stmt);

  cm_stmt_lrs_tree = find_lrs_tree (cm_stmt, region);
  if (!cm_stmt_lrs_tree)
    return gsi_prev_stmt;

  /* Now adjust the previous statement to be the first
     of the statement group associated with CM_STMT_LRS_TREE  */
  gsi_prev_stmt 
      = gsi_for_stmt (VEC_index (gimple, cm_stmt_lrs_tree->tree_nodes, 0));
  gsi_prev (&gsi_prev_stmt);

  if (has_single_use (gimple_assign_lhs (cm_stmt)))
    return gsi_prev_stmt;

  if (has_float_typed_const_operand (cm_stmt))
    return gsi_prev_stmt;

  latest = get_closest_use (cm_stmt);

  if (!latest)
    return gsi_prev_stmt;

  insert_after = false;
  latest = check_down_motion_target_loc (latest, cm_stmt, 
                                         &insert_after, region);

  if (!latest || latest == cm_stmt)
    return gsi_prev_stmt;

  latest = check_down_motion_profitability (latest, cm_stmt_lrs_tree,
                                            insert_after, region);

  if (latest == NULL)
    return gsi_prev_stmt;

  if (!ok_to_sched_down (cm_stmt_lrs_tree, latest, insert_after, region))
    return gsi_prev_stmt;

  if (!dbg_cnt (lrs))
    return gsi_prev_stmt;

  gsi_target = gsi_for_stmt (latest);
  target_order = get_stmt_order (latest);
  reaching_defs
      = (region->stmt_rd_sets
         ? get_reaching_vdefs (latest, insert_after, region)
         : NULL);
  k = VEC_length (gimple, cm_stmt_lrs_tree->tree_nodes);
  for (j = 0; j < k; j++)
    {
      gimple_stmt_iterator gsi_cm;
      gimple inner_node
          = VEC_index (gimple, cm_stmt_lrs_tree->tree_nodes, j);
      int stmt_id;

      gsi_cm = gsi_for_stmt (inner_node);
      stmt_id = get_stmt_idx_in_region (inner_node);
      if (insert_after)
        {
          gsi_move_after (&gsi_cm, &gsi_target);
          gsi_target = gsi_for_stmt (inner_node);
        }
      else
        gsi_move_before (&gsi_cm, &gsi_target);
      reset_stmt_order (inner_node, target_order);
      if (reaching_defs)
        sbitmap_copy (region->stmt_rd_sets[stmt_id], reaching_defs);
    }

  if (dump_file && (dump_flags & TDF_DETAILS))
    {
      fprintf (dump_file, "MOVED (multiuse) DOWN\n");
      print_lrs_tree (dump_file, cm_stmt_lrs_tree);
      fprintf (dump_file, "just %s \n", insert_after? "after" : "before");
      print_gimple_stmt (dump_file, latest, 0, 0);
      fprintf (dump_file, "\n");
    }

  return gsi_prev_stmt;
}

/* The function expands and returns the expression tree rooted
   at statement ROOT.  */

static lrs_tree_p
create_lrs_tree (gimple root, lrs_region_p region)
{
  lrs_tree_p lrs_tree;
  void **slot;

  lrs_tree = (lrs_tree_p) pool_alloc (region->lrs_tree_pool);
  lrs_tree->root = root;
  lrs_tree->tree_nodes = NULL;
  lrs_tree->leaf_nodes = NULL;
  lrs_tree->num_leaves_not_live_across = 0;
  lrs_tree->num_leaves_live_across = 0;
  lrs_tree->num_temp_lrs = 0;

  slot = pointer_map_insert (region->gimple_to_lrs_tree_map, root);
  *slot = lrs_tree;

  return lrs_tree;
}

/* The function returns the lrs_tree created for gimple statement
   STMT which is the tree root.  */

static lrs_tree_p
find_lrs_tree (gimple stmt, lrs_region_p region)
{
  void **slot;

  slot = pointer_map_contains (region->gimple_to_lrs_tree_map, stmt);
  if (!slot)
    return NULL;

  return (lrs_tree_p)*slot;
}

/* The function computes the number of leaf nodes in LRS_TREE that are
   live across (i.e., no references to the same ssa name after ROOT) 
   statement ROOT.  */

static void
check_leaf_liveness (gimple root, lrs_tree_p lrs_tree,
                     lrs_region_p region)
{
  sbitmap live_ur_set;
  int i, n;

  live_ur_set = get_across_stmt_use_ref_set (root, region);

  n = VEC_length (tree, lrs_tree->leaf_nodes);

  for (i = 0; i < n; i++)
    {
      size_t first, last;
      tree leaf = VEC_index (tree, lrs_tree->leaf_nodes, i);
      get_def_bit_range (&first, &last, leaf, region);
      if (sbitmap_range_empty_p (live_ur_set, first, last - first + 1))
        lrs_tree->num_leaves_not_live_across++;
    }

  lrs_tree->num_leaves_live_across = n - lrs_tree->num_leaves_not_live_across;
}

/* The function returns true if STMT is a candidate to be scheduled
   down to basic block SCHED_BB in REGION.  */

static bool
is_down_sched_candidate (gimple stmt, basic_block sched_bb, 
                         lrs_region_p region)
{
  basic_block bb;
  if (!is_gimple_assign (stmt) )
    return false;

  if (!ZERO_SSA_OPERANDS (stmt, SSA_OP_VIRTUAL_DEFS))
    return false;

  if (TREE_CODE (gimple_assign_lhs (stmt)) != SSA_NAME)
    return false;

  bb = gimple_bb (stmt);

  /* can not do code motion across regions.  */
  if (bb->loop_father != sched_bb->loop_father)
    return false;

  if (region->num_bbs == 1 && bb != region->entry)
    return false;

  return true;
}

/* The downward code motion of a statement will shrink the life
   range for the LR that is defined by it, but it may also extend
   the life range of the used LRs if they do not live across the
   insertion point.  If the used LR DO live across the insertion
   point, it will be beneficial to do the code motion.  If they do
   not, this function will try to adjust the insertion point to
   the one they last live.

   Example:


   x = y + z; // statement to be moved

   ....

   a = b + c;

   // insertion point, y, and z live across this point

   t = x + r;

   u = y + 1;

   v = z + 1;

   Since y and z live across the insertion point, it is good to
   to do the code motion and shrink x's range without penalty.


   a = b + c;

   x = y + z; // statement  this is moved

   t = x + r;

   u = y + 1;

   v = z + 1;
*/

static lrs_tree_p
schedule_lrs_tree (gimple root, basic_block sched_bb, lrs_region_p region)
{
  lrs_tree_p lrs_tree = NULL;
  int i, n, num_leaves, nsub;
  lrs_tree_p subtrees[2];
  struct pointer_set_t *leaves;

  if (!is_down_sched_candidate (root, sched_bb, region))
    return NULL;

  lrs_tree = find_lrs_tree (root, region);

  /* Already scheduled  */
  if (lrs_tree)
    return lrs_tree;

  if (!dbg_cnt (lrs))
    return NULL;

  nsub = 0;
  num_leaves = 0;
  lrs_tree = create_lrs_tree (root, region);
  leaves = pointer_set_create ();

  n = gimple_num_ops (root);
  gcc_assert (n < 4);
  for (i = 1; i < n; i++)
    {
      gimple def_stmt;
      lrs_tree_p sub_tree;
      tree op = gimple_op (root, i);
      if (TREE_CODE (op) != SSA_NAME)
        continue;

      def_stmt = SSA_NAME_DEF_STMT (op);
      sub_tree = schedule_lrs_tree (def_stmt, sched_bb, region);
      if (!sub_tree || !has_single_use (op)
          || !ok_to_sched_down (sub_tree, root, false, region)
          || !profitable_to_sched_down (sub_tree))
        {
          if (!pointer_set_insert (leaves, op))
            VEC_safe_push (tree, heap, lrs_tree->leaf_nodes, op);
        }
      else
        {
          int j, k;
          subtrees[nsub++] = sub_tree;
          k = VEC_length (tree, sub_tree->leaf_nodes);
          /* copy leaf nodes  */
          for (j = 0; j < k; j++)
            {
              tree lv = VEC_index (tree, sub_tree->leaf_nodes, j);
              if (!pointer_set_insert (leaves, lv))
                VEC_safe_push (tree, heap, lrs_tree->leaf_nodes, lv);
            }
        }
     }

  if (nsub == 0)
    {
      int nu;
      lrs_stmt_p norm_stmt
          = get_normalized_gimple_stmt (root, region);
      nu = norm_stmt->num_uses;
      for (i = 0; i < nu; i++)
        {
          tree u = *(norm_stmt->uses[i]);
          if (!pointer_set_insert (leaves, u))
            VEC_safe_push (tree, heap, lrs_tree->leaf_nodes, u);
        }
    }

  /* Now compute the number of leaves that do not live across ROOT.  */
  check_leaf_liveness (root, lrs_tree, region);

  /* Subtrees that are scheduled now can be moved down closer to
     the root stmt.  */
  if (nsub == 0)
      lrs_tree->num_temp_lrs = 1;
  else
    {
      int i, j, k; 
      gimple_stmt_iterator target_gsi;
      gimple_stmt_iterator gsi;
      size_t target_order;
      sbitmap reaching_vdefs;

      /* To reduce the max number of temp register required, it is
         better to schedule the subtree with larger temp registers first.  */
      if (nsub == 2 
          && subtrees[0]->num_temp_lrs < subtrees[1]->num_temp_lrs)
        {
          lrs_tree_p first = subtrees[1];
          subtrees[1] = subtrees[0];
          subtrees[0] = first;;
        }

      target_gsi = gsi_for_stmt (root);
      target_order = get_stmt_order (root);
      reaching_vdefs
          = (region->stmt_rd_sets
             ? get_reaching_vdefs (root, false, region)
             : NULL);
      for (i = 0; i < nsub; i++)
        {
          lrs_tree_p sub_tree = subtrees[i];
          k = VEC_length (gimple, sub_tree->tree_nodes);
          for (j = 0; j < k; j++)
            {
              int stmt_id;
              gimple inner_node = VEC_index (gimple, sub_tree->tree_nodes, j);

              stmt_id = get_stmt_idx_in_region (inner_node);

              VEC_safe_push (gimple, heap, lrs_tree->tree_nodes, inner_node);
              gsi = gsi_for_stmt (inner_node);
              gsi_move_before (&gsi, &target_gsi);
              reset_stmt_order (inner_node, target_order);
              if (reaching_vdefs)
                sbitmap_copy (region->stmt_rd_sets[stmt_id], reaching_vdefs);
              if (dump_file && (dump_flags & TDF_DETAILS))
                {
                  fprintf (dump_file, "MOVED DOWN\n");
                  print_lrs_tree (dump_file, sub_tree);
                  fprintf (dump_file, "Before\n");
                  print_gimple_stmt (dump_file, root, 0, 0);
                  fprintf (dump_file, "\n");
                }
            }
        }
      lrs_tree->num_temp_lrs = subtrees[0]->num_temp_lrs;
      if (nsub == 2
          && subtrees[0]->num_temp_lrs == subtrees[1]->num_temp_lrs)
        lrs_tree->num_temp_lrs++;
    }

  VEC_safe_push (gimple, heap, lrs_tree->tree_nodes, root);
  pointer_set_destroy (leaves);
  return lrs_tree;
}

/* The function prepares for downward code motion
   transformation in region REGION.  */

static void
initialize_down_motion (lrs_region_p region)
{
  perform_data_flow_rd (region);
  region->gimple_to_lrs_tree_map
      = pointer_map_create ();
  region->lrs_tree_pool
      = create_alloc_pool ("lrs tree pool",
                           sizeof (struct lrs_tree), 50);
}

/* The function performs downward code motion in REGION
   to reduce overlapping live ranges.  */

static void
shrink_lrs_down_motion (lrs_region_p region)
{
  size_t i;

  if (!do_downward_motion ())
    return;

  initialize_down_motion (region);

  for (i = 0; i < region->num_bbs; i++)
    {
      basic_block bb = region->body[i];
      gimple_stmt_iterator gsi = gsi_start_bb (bb);
      while (!gsi_end_p (gsi))
        {
          schedule_lrs_tree (gsi_stmt (gsi), bb, region);
          gsi_next (&gsi);
        }
    }

  /* Now schedule all the lrs trees that have multiple uses.  */
  for (i = 0; i < region->num_bbs; i++)
    {
      basic_block bb = region->body[i];
      gimple_stmt_iterator gsi = gsi_last_bb (bb);
      while (!gsi_end_p (gsi))
        gsi = sink_multi_use_def (gsi, region);
    }

  dump_data_flow_result (region, "After downward motion");
}

/* The function prepares for the lrs shrinking transformation
   for the current function.  */

static void
init_lrs_shrink (void)
{
  basic_block bb;

  /* TODO -- share loop recog with the reassociation phase.  */
  loop_optimizer_init (AVOID_CFG_MODIFICATIONS);
  calculate_dominance_info (CDI_POST_DOMINATORS);
  stmt_order = pointer_map_create ();
  tmp_reg_alloc_pool
      = create_alloc_pool ("congruent class pool",
                           sizeof (struct reg_alloc), 50);
  tmp_reg_alloc_map = pointer_map_create ();
  reg_alloc_map = pointer_map_create ();
  tmp_reg_alloc_map = pointer_map_create ();
  FOR_EACH_BB (bb)
    {
      compute_stmt_order (bb);
      compute_reg_allocs (bb);
    }
  finalize_reg_allocs ();
  reg_pressure_control_min_bb_size
      = PARAM_VALUE (PARAM_REG_PRESSURE_MIN_BB_FACTOR)
      * target_avail_regs;
}

/* The function destroys the reg alloc map.  */

static void
destroy_reg_alloc_map (void)
{
  size_t i;

  for (i = 0; i < num_reg_allocs; i++)
    VEC_free (tree, heap, reg_allocs[i]);

  pointer_map_destroy (reg_alloc_map);
  reg_alloc_map = NULL;

  free (reg_allocs);
  reg_allocs = NULL;
}

/* The function finalizes function for lrs shrinking.  */

static void
fini_lrs_shrink (void)
{
  destroy_reg_alloc_map ();
  pointer_map_destroy (stmt_order);
  free_dominance_info (CDI_POST_DOMINATORS);
  loop_optimizer_finalize ();
}

/* Entry point for doing live range shrink transformation.  */

static void
do_lr_shrink (void)
{
  basic_block bb;
  FOR_EACH_BB (bb)
    {
      lrs_region_p region = form_region (bb);
      if (!region)
        continue;

      perform_data_flow_ur (region);

      if (!has_high_reg_pressure (region)
	  && need_control_reg_pressure (region) != 2)
	{
	  destroy_region (region);
	  continue;
	}

      shrink_lrs_reassociation (region);

      shrink_lrs_up_motion (region);

      shrink_lrs_down_motion (region);

      /* Now fixup negates if needed  */
      negate_opnds ();

      destroy_region (region);
    }
}

/* Gate and execute functions for live range shrinking.  */

static unsigned int
execute_lrs (void)
{
  init_lrs_shrink ();
  do_lr_shrink ();
  fini_lrs_shrink ();
  return 0;
}

static bool
gate_tree_ssa_lrs (void)
{
  return (flag_tree_lr_shrinking
          && (PARAM_VALUE (PARAM_CONTROL_REG_PRESSURE) != 0));
}

/* Print function for lrs_tree.  DUMP_FILE is the FILE pointer,
   LRS_REASSOC_TREE is the tree to be printed.  */

static void
print_lrs_reassoc_tree (FILE *dump_file, lrs_reassoc_tree_p reassoc_tree)
{
  int i, n;
  n = VEC_length (gimple, reassoc_tree->inner_nodes);
  fprintf (dump_file, "LRS_REASSOC_TREE {\n");
  for (i = 0; i < n; i++)
    {
      gimple stmt = VEC_index (gimple, reassoc_tree->inner_nodes, i);
      fprintf (dump_file, "\t");
      print_gimple_stmt (dump_file, stmt, 0, 0);
    }
  fprintf (dump_file,"}\n");
}

/* Print function for lrs_tree.  DUMP_FILE is the FILE pointer,
   LRS_TREE is the tree to be printed.  */

static void
print_lrs_tree (FILE *dump_file, lrs_tree_p lrs_tree)
{
  int i, n;
  n = VEC_length (gimple, lrs_tree->tree_nodes);
  fprintf (dump_file, "LRS_TREE {\n");
  for (i = 0; i < n; i++)
    {
      gimple stmt = VEC_index (gimple, lrs_tree->tree_nodes, i);
      fprintf (dump_file, "\t");
      print_gimple_stmt (dump_file, stmt, 0, 0);
    }
  fprintf (dump_file,"}\n");
}

/* The function dumps the ssa names referenced in REGION.  The output
   is dumped to DUMP_FILE.  */

static void
dump_refed_names (FILE *dump_file, lrs_region_p region)
{

  size_t i;
  bool is_first = true;
  fprintf (dump_file, "[Refed names]\n\t{ ");
  for (i = 0; i < VEC_length (tree, region->refed_names); i++)
    {
      tree nm;
      if (!is_first)
        fprintf (dump_file, ", ");
      else
        is_first = false;
      nm = VEC_index (tree, region->refed_names, i);
      print_generic_expr (dump_file, nm, 0); 
      fprintf (dump_file, "(%d)", get_reg_alloc_id (nm));
    }
  fprintf (dump_file, "}\n");
}

/* The function dumps the virtual variables referenced in REGION.  The output
   is dumped to DUMP_FILE.  */

static void
dump_refed_vvars (FILE *dump_file, lrs_region_p region)
{
  size_t i;
  bool is_first = true;
  fprintf (dump_file, "[Refed vvar names]\n\t{ ");
  for (i = 0; i < VEC_length (tree, region->refed_vvar_names); i++)
    {
      tree nm;
      if (!is_first)
        fprintf (dump_file, ", ");
      else
        is_first = false;
      nm = VEC_index (tree, region->refed_vvar_names, i);
      print_generic_expr (dump_file, nm, 0); 
    }
  fprintf (dump_file, "}\n");

  is_first = true;
  fprintf (dump_file, "[Refed vvars]\n\t{ ");
  for (i = 0; i < VEC_length (tree, region->refed_vvars); i++)
    {
      tree vvar;
      if (!is_first)
        fprintf (dump_file, ", ");
      else
        is_first = false;
      vvar = VEC_index (tree, region->refed_vvars, i);
      print_generic_expr (dump_file, vvar, 0); 
    }
  fprintf (dump_file, "}\n");
}

/* The function dumps the content of the bitvector BITVEC.  MAPPING is the
   mapping from bit position to ssa name.  Output is dumped to FILE.  */

static void
dump_use_ref_set (FILE *file, sbitmap bitvec, tree *mapping)
{
  size_t i = 0;
  sbitmap_iterator bi;
  bool first = true;

  fprintf (file, "{");
  EXECUTE_IF_SET_IN_SBITMAP (bitvec, 0, i, bi)
    {
      tree nm = mapping[i];
      if (!first)
        fprintf (file, ", ");
      else
        first = false;

      print_generic_expr (file, nm, 0);
      fprintf (file, "(%d)", i);
    }

  fprintf (file, "}");
}

/* The function computes and dumps register pressure associated with
   use-ref bit vector BITVEC in REGION.  FILE is the output file.  */

static void
dump_register_pressure (FILE *file, sbitmap bitvec,
                        lrs_region_p region)
{
  size_t gr_pressure = 0;
  size_t fr_pressure = 0;

  get_num_live_lrs (bitvec, region, &gr_pressure, &fr_pressure);

  fprintf (file, " \n\t[REG PRESSURE: gr = %d, fr = %d]", 
           gr_pressure, fr_pressure);
}

/* The function dumps the use-ref data flow analysis result for 
   basic block BB in REGION.  BB_RIDX is the basis block index in
   REGION, MAPPING is the mapping from bitvector position to ssa names,
   and FILE is the output file.  */

static void
dump_data_flow_bb (FILE *file, basic_block bb, int bb_ridx, 
                   tree *mapping, lrs_region_p region)
{
  gimple_stmt_iterator gsi;

  fprintf (file, "BB#  %d:\n", bb->index);
  fprintf (file, "\tIN :");
  dump_use_ref_set (file, region->bb_use_ref_in_sets[bb_ridx],
                    mapping);
  fprintf (file, "\n");
  fprintf (file, "\tOUT:");
  dump_use_ref_set (file, region->bb_use_ref_out_sets[bb_ridx],
                    mapping);
  fprintf (file, "\n");
  fprintf (file, "\tGEN:");
  dump_use_ref_set (file, region->bb_use_ref_gen_sets[bb_ridx],
                    mapping);
  fprintf (file, "\n");
  fprintf (file, "\tKILL:");
  dump_use_ref_set (file, region->bb_use_ref_kill_sets[bb_ridx],
                    mapping);
  fprintf (file, "\n");

  fprintf (file, "\tREG PRESSURE: gr = %d, fr = %d\n",
           region->bb_reg_pressures[lrc_gr][bb_ridx],
           region->bb_reg_pressures[lrc_fr][bb_ridx]);

  for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
    {
      int id;
      gimple stmt = gsi_stmt (gsi);

      id = get_stmt_idx_in_region (stmt);
      fprintf (file, "\t[PHI]: ");
      print_gimple_stmt (file, stmt, 0, 0);
      fprintf (file, "\t\t");
      dump_use_ref_set (file, region->across_stmt_use_ref_sets[id],
                        mapping);
      dump_register_pressure (file, region->across_stmt_use_ref_sets[id],
                              region);
      fprintf (file, "\n");
    }
  for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
    {
      int id;
      gimple stmt = gsi_stmt (gsi);

      id = get_stmt_idx_in_region (stmt);
      fprintf (file, "\t[STMT]: ");
      print_gimple_stmt (file, stmt, 0, 0);
      fprintf (file, "\t\t");
      dump_use_ref_set (file, region->across_stmt_use_ref_sets[id],
                        mapping);
      dump_register_pressure (file, region->across_stmt_use_ref_sets[id],
                              region);
      fprintf (file, "\n");
    }
}

/* The function dumps the use-ref data flow result for REGION.  PHASE is 
   the string of the dump phase.  */

static void
dump_data_flow_result (lrs_region_p region, const char* phase)
{
  size_t i, n;
  tree *bit_pos_to_tree_mapping = 0;

  if (!dump_file)
    return;

  fprintf (dump_file, "[Data Flow Result for region (head bb): %d:  PHASE: %s\n\n",
           region->entry->index, phase);

  dump_reg_allocs (dump_file);

  dump_refed_names (dump_file, region);
  dump_refed_vvars (dump_file, region);

  fprintf (dump_file, "\tREG PRESSURE: gr = %d, fr = %d\n",
           region->reg_pressure[lrc_gr],
           region->reg_pressure[lrc_fr]);

  fprintf (dump_file, "\tAVAIL REGS: gr = %d, fr = %d\n",
           region->available_regs[lrc_gr],
           region->available_regs[lrc_fr]);

  bit_pos_to_tree_mapping = XNEWVEC (tree, region->bitvec_width);
  n = VEC_length (tree, region->refed_names);
  for (i = 0; i < n; i++)
    {
      size_t first, last, j;
      tree nm = VEC_index (tree, region->refed_names, i);
      get_def_bit_range (&first, &last, nm, region);
      for (j = first; j <= last; j++)
        bit_pos_to_tree_mapping[j] = nm;
    }

  for (i = 0; i < region->num_bbs; i++)
    dump_data_flow_bb (dump_file, region->body[i], i,
                       bit_pos_to_tree_mapping, region);

  free (bit_pos_to_tree_mapping);
}

/* The functions dumps the reaching def bitvector
   BITVEC.  REFED_VNAMES is a map from bit positions
   to the virtual variable names.  */

static void
dump_rd_set (FILE *file, sbitmap bitvec, 
             VEC(tree, heap) *refed_vnames)
{
  size_t i = 0;
  sbitmap_iterator bi;
  bool first = true;

  fprintf (file, "{");
  EXECUTE_IF_SET_IN_SBITMAP (bitvec, 0, i, bi)
    {
      tree nm = VEC_index (tree, refed_vnames, i);
      if (!first)
        fprintf (file, ", ");
      else
        first = false;

      print_generic_expr (file, nm, 0);
      fprintf (file, "(%d)", i);
    }

  fprintf (file, "}");
}

/* The function dumps virtual variable reaching def data flow
   results for basic block BB (with id BB_RIDX) in REGION.  */

static void
dump_data_flow_bb_rd (FILE *file, basic_block bb, int bb_ridx, 
                      lrs_region_p region)
{
  gimple_stmt_iterator gsi;
  VEC(tree, heap) *refed_vnames = region->refed_vvar_names;

  fprintf (file, "BB#  %d:\n", bb->index);
  fprintf (file, "\tIN :");
  dump_rd_set (file, region->bb_rd_in_sets[bb_ridx], refed_vnames);
  fprintf (file, "\n");
  fprintf (file, "\tOUT:");
  dump_rd_set (file, region->bb_rd_out_sets[bb_ridx], refed_vnames);
  fprintf (file, "\n");
  fprintf (file, "\tGEN:");
  dump_rd_set (file, region->bb_rd_gen_sets[bb_ridx], refed_vnames);
  fprintf (file, "\n");
  fprintf (file, "\tKILL:");
  dump_rd_set (file, region->bb_rd_kill_sets[bb_ridx], refed_vnames);
  fprintf (file, "\n");

  for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
    {
      int id;
      gimple stmt = gsi_stmt (gsi);

      id = get_stmt_idx_in_region (stmt);
      fprintf (file, "\t[PHI]: ");
      print_gimple_stmt (file, stmt, 0, 0);
      fprintf (file, "\t\t");
      dump_rd_set (file, region->stmt_rd_sets[id], refed_vnames);
      fprintf (file, "\n");
    }
  for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
    {
      int id;
      gimple stmt = gsi_stmt (gsi);

      id = get_stmt_idx_in_region (stmt);
      fprintf (file, "\t[STMT]: ");
      print_gimple_stmt (file, stmt, 0, 0);
      fprintf (file, "\t\t");
      dump_rd_set (file, region->stmt_rd_sets[id], refed_vnames);
      fprintf (file, "\n");
    }
}

/* The function dumps virtual variable reaching def data
   flow result for region REGION.  */

static void
dump_data_flow_result_rd (lrs_region_p region)
{
  size_t i;

  if (!dump_file)
    return;

  fprintf (dump_file, "[Data Flow Result (Reach Def) for region (head bb): %d\n\n",
           region->entry->index);

  dump_refed_vvars (dump_file, region);

  for (i = 0; i < region->num_bbs; i++)
    dump_data_flow_bb_rd (dump_file, region->body[i], i, region);

}

/* The function dumps one reg alloc RA.  */
static bool
dump_ra (FILE *file, VEC(tree, heap) *ra)
{
  size_t i;

  fprintf (file, "\t{ ");
  for (i = 0; i < VEC_length (tree, ra); i++)
    {
      tree nm = VEC_index (tree, ra, i);
      print_generic_expr (file, nm, 0);
      fprintf (file, " ");
    }
  fprintf (file, "}\n");
  return true;
}

/* The function dumps reg allocs computed.  */

static void
dump_reg_allocs (FILE *file)
{
  size_t i;
  fprintf (file, "[Reg Alloc Congruent Classes]\n");

  for (i = 0; i < num_reg_allocs; i++)
    dump_ra (file, reg_allocs[i]);
}

struct gimple_opt_pass pass_lrs =
{
 {
  GIMPLE_PASS,
  "lrs",				/* name */
  gate_tree_ssa_lrs,     		/* gate */
  execute_lrs,		         	/* execute */
  NULL,					/* sub */
  NULL,					/* next */
  0,					/* static_pass_number */
  TV_TREE_LRS,	         		/* tv_id */
  PROP_cfg | PROP_ssa | PROP_alias,	/* properties_required */
  0,					/* properties_provided */
  0,					/* properties_destroyed */
  0,					/* todo_flags_start */
  TODO_dump_func | TODO_ggc_collect | TODO_verify_ssa /* todo_flags_finish */
 }
};