aboutsummaryrefslogtreecommitdiff
path: root/gcc/tree-scalar-evolution.c
blob: 4fe1a86004b59c72f151a709b2e568248d7156f1 (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
/* Scalar evolution detector.
   Copyright (C) 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
   Contributed by Sebastian Pop <s.pop@laposte.net>

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/>.  */

/* 
   Description: 
   
   This pass analyzes the evolution of scalar variables in loop
   structures.  The algorithm is based on the SSA representation,
   and on the loop hierarchy tree.  This algorithm is not based on
   the notion of versions of a variable, as it was the case for the
   previous implementations of the scalar evolution algorithm, but
   it assumes that each defined name is unique.

   The notation used in this file is called "chains of recurrences",
   and has been proposed by Eugene Zima, Robert Van Engelen, and
   others for describing induction variables in programs.  For example
   "b -> {0, +, 2}_1" means that the scalar variable "b" is equal to 0
   when entering in the loop_1 and has a step 2 in this loop, in other
   words "for (b = 0; b < N; b+=2);".  Note that the coefficients of
   this chain of recurrence (or chrec [shrek]) can contain the name of
   other variables, in which case they are called parametric chrecs.
   For example, "b -> {a, +, 2}_1" means that the initial value of "b"
   is the value of "a".  In most of the cases these parametric chrecs
   are fully instantiated before their use because symbolic names can
   hide some difficult cases such as self-references described later
   (see the Fibonacci example).
   
   A short sketch of the algorithm is:
     
   Given a scalar variable to be analyzed, follow the SSA edge to
   its definition:
     
   - When the definition is a GIMPLE_MODIFY_STMT: if the right hand side
   (RHS) of the definition cannot be statically analyzed, the answer
   of the analyzer is: "don't know".  
   Otherwise, for all the variables that are not yet analyzed in the
   RHS, try to determine their evolution, and finally try to
   evaluate the operation of the RHS that gives the evolution
   function of the analyzed variable.

   - When the definition is a condition-phi-node: determine the
   evolution function for all the branches of the phi node, and
   finally merge these evolutions (see chrec_merge).

   - When the definition is a loop-phi-node: determine its initial
   condition, that is the SSA edge defined in an outer loop, and
   keep it symbolic.  Then determine the SSA edges that are defined
   in the body of the loop.  Follow the inner edges until ending on
   another loop-phi-node of the same analyzed loop.  If the reached
   loop-phi-node is not the starting loop-phi-node, then we keep
   this definition under a symbolic form.  If the reached
   loop-phi-node is the same as the starting one, then we compute a
   symbolic stride on the return path.  The result is then the
   symbolic chrec {initial_condition, +, symbolic_stride}_loop.

   Examples:
   
   Example 1: Illustration of the basic algorithm.
   
   | a = 3
   | loop_1
   |   b = phi (a, c)
   |   c = b + 1
   |   if (c > 10) exit_loop
   | endloop
   
   Suppose that we want to know the number of iterations of the
   loop_1.  The exit_loop is controlled by a COND_EXPR (c > 10).  We
   ask the scalar evolution analyzer two questions: what's the
   scalar evolution (scev) of "c", and what's the scev of "10".  For
   "10" the answer is "10" since it is a scalar constant.  For the
   scalar variable "c", it follows the SSA edge to its definition,
   "c = b + 1", and then asks again what's the scev of "b".
   Following the SSA edge, we end on a loop-phi-node "b = phi (a,
   c)", where the initial condition is "a", and the inner loop edge
   is "c".  The initial condition is kept under a symbolic form (it
   may be the case that the copy constant propagation has done its
   work and we end with the constant "3" as one of the edges of the
   loop-phi-node).  The update edge is followed to the end of the
   loop, and until reaching again the starting loop-phi-node: b -> c
   -> b.  At this point we have drawn a path from "b" to "b" from
   which we compute the stride in the loop: in this example it is
   "+1".  The resulting scev for "b" is "b -> {a, +, 1}_1".  Now
   that the scev for "b" is known, it is possible to compute the
   scev for "c", that is "c -> {a + 1, +, 1}_1".  In order to
   determine the number of iterations in the loop_1, we have to
   instantiate_parameters ({a + 1, +, 1}_1), that gives after some
   more analysis the scev {4, +, 1}_1, or in other words, this is
   the function "f (x) = x + 4", where x is the iteration count of
   the loop_1.  Now we have to solve the inequality "x + 4 > 10",
   and take the smallest iteration number for which the loop is
   exited: x = 7.  This loop runs from x = 0 to x = 7, and in total
   there are 8 iterations.  In terms of loop normalization, we have
   created a variable that is implicitly defined, "x" or just "_1",
   and all the other analyzed scalars of the loop are defined in
   function of this variable:
   
   a -> 3
   b -> {3, +, 1}_1
   c -> {4, +, 1}_1
     
   or in terms of a C program: 
     
   | a = 3
   | for (x = 0; x <= 7; x++)
   |   {
   |     b = x + 3
   |     c = x + 4
   |   }
     
   Example 2: Illustration of the algorithm on nested loops.
     
   | loop_1
   |   a = phi (1, b)
   |   c = a + 2
   |   loop_2  10 times
   |     b = phi (c, d)
   |     d = b + 3
   |   endloop
   | endloop
     
   For analyzing the scalar evolution of "a", the algorithm follows
   the SSA edge into the loop's body: "a -> b".  "b" is an inner
   loop-phi-node, and its analysis as in Example 1, gives: 
     
   b -> {c, +, 3}_2
   d -> {c + 3, +, 3}_2
     
   Following the SSA edge for the initial condition, we end on "c = a
   + 2", and then on the starting loop-phi-node "a".  From this point,
   the loop stride is computed: back on "c = a + 2" we get a "+2" in
   the loop_1, then on the loop-phi-node "b" we compute the overall
   effect of the inner loop that is "b = c + 30", and we get a "+30"
   in the loop_1.  That means that the overall stride in loop_1 is
   equal to "+32", and the result is: 
     
   a -> {1, +, 32}_1
   c -> {3, +, 32}_1
     
   Example 3: Higher degree polynomials.
     
   | loop_1
   |   a = phi (2, b)
   |   c = phi (5, d)
   |   b = a + 1
   |   d = c + a
   | endloop
     
   a -> {2, +, 1}_1
   b -> {3, +, 1}_1
   c -> {5, +, a}_1
   d -> {5 + a, +, a}_1
     
   instantiate_parameters ({5, +, a}_1) -> {5, +, 2, +, 1}_1
   instantiate_parameters ({5 + a, +, a}_1) -> {7, +, 3, +, 1}_1
     
   Example 4: Lucas, Fibonacci, or mixers in general.
     
   | loop_1
   |   a = phi (1, b)
   |   c = phi (3, d)
   |   b = c
   |   d = c + a
   | endloop
     
   a -> (1, c)_1
   c -> {3, +, a}_1
     
   The syntax "(1, c)_1" stands for a PEELED_CHREC that has the
   following semantics: during the first iteration of the loop_1, the
   variable contains the value 1, and then it contains the value "c".
   Note that this syntax is close to the syntax of the loop-phi-node:
   "a -> (1, c)_1" vs. "a = phi (1, c)".
     
   The symbolic chrec representation contains all the semantics of the
   original code.  What is more difficult is to use this information.
     
   Example 5: Flip-flops, or exchangers.
     
   | loop_1
   |   a = phi (1, b)
   |   c = phi (3, d)
   |   b = c
   |   d = a
   | endloop
     
   a -> (1, c)_1
   c -> (3, a)_1
     
   Based on these symbolic chrecs, it is possible to refine this
   information into the more precise PERIODIC_CHRECs: 
     
   a -> |1, 3|_1
   c -> |3, 1|_1
     
   This transformation is not yet implemented.
     
   Further readings:
   
   You can find a more detailed description of the algorithm in:
   http://icps.u-strasbg.fr/~pop/DEA_03_Pop.pdf
   http://icps.u-strasbg.fr/~pop/DEA_03_Pop.ps.gz.  But note that
   this is a preliminary report and some of the details of the
   algorithm have changed.  I'm working on a research report that
   updates the description of the algorithms to reflect the design
   choices used in this implementation.
     
   A set of slides show a high level overview of the algorithm and run
   an example through the scalar evolution analyzer:
   http://cri.ensmp.fr/~pop/gcc/mar04/slides.pdf

   The slides that I have presented at the GCC Summit'04 are available
   at: http://cri.ensmp.fr/~pop/gcc/20040604/gccsummit-lno-spop.pdf
*/

#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
#include "ggc.h"
#include "tree.h"
#include "real.h"

/* These RTL headers are needed for basic-block.h.  */
#include "rtl.h"
#include "basic-block.h"
#include "diagnostic.h"
#include "tree-flow.h"
#include "tree-dump.h"
#include "timevar.h"
#include "cfgloop.h"
#include "tree-chrec.h"
#include "tree-scalar-evolution.h"
#include "tree-pass.h"
#include "flags.h"
#include "params.h"

static tree analyze_scalar_evolution_1 (struct loop *, tree, tree);

/* The cached information about a ssa name VAR, claiming that inside LOOP,
   the value of VAR can be expressed as CHREC.  */

struct scev_info_str GTY(())
{
  tree var;
  tree chrec;
};

/* Counters for the scev database.  */
static unsigned nb_set_scev = 0;
static unsigned nb_get_scev = 0;

/* The following trees are unique elements.  Thus the comparison of
   another element to these elements should be done on the pointer to
   these trees, and not on their value.  */

/* The SSA_NAMEs that are not yet analyzed are qualified with NULL_TREE.  */
tree chrec_not_analyzed_yet;

/* Reserved to the cases where the analyzer has detected an
   undecidable property at compile time.  */
tree chrec_dont_know;

/* When the analyzer has detected that a property will never
   happen, then it qualifies it with chrec_known.  */
tree chrec_known;

static bitmap already_instantiated;

static GTY ((param_is (struct scev_info_str))) htab_t scalar_evolution_info;


/* Constructs a new SCEV_INFO_STR structure.  */

static inline struct scev_info_str *
new_scev_info_str (tree var)
{
  struct scev_info_str *res;
  
  res = GGC_NEW (struct scev_info_str);
  res->var = var;
  res->chrec = chrec_not_analyzed_yet;
  
  return res;
}

/* Computes a hash function for database element ELT.  */

static hashval_t
hash_scev_info (const void *elt)
{
  return SSA_NAME_VERSION (((const struct scev_info_str *) elt)->var);
}

/* Compares database elements E1 and E2.  */

static int
eq_scev_info (const void *e1, const void *e2)
{
  const struct scev_info_str *elt1 = (const struct scev_info_str *) e1;
  const struct scev_info_str *elt2 = (const struct scev_info_str *) e2;

  return elt1->var == elt2->var;
}

/* Deletes database element E.  */

static void
del_scev_info (void *e)
{
  ggc_free (e);
}

/* Get the index corresponding to VAR in the current LOOP.  If
   it's the first time we ask for this VAR, then we return
   chrec_not_analyzed_yet for this VAR and return its index.  */

static tree *
find_var_scev_info (tree var)
{
  struct scev_info_str *res;
  struct scev_info_str tmp;
  PTR *slot;

  tmp.var = var;
  slot = htab_find_slot (scalar_evolution_info, &tmp, INSERT);

  if (!*slot)
    *slot = new_scev_info_str (var);
  res = (struct scev_info_str *) *slot;

  return &res->chrec;
}

/* Return true when CHREC contains symbolic names defined in
   LOOP_NB.  */

bool 
chrec_contains_symbols_defined_in_loop (tree chrec, unsigned loop_nb)
{
  int i, n;

  if (chrec == NULL_TREE)
    return false;

  if (TREE_INVARIANT (chrec))
    return false;

  if (TREE_CODE (chrec) == VAR_DECL
      || TREE_CODE (chrec) == PARM_DECL
      || TREE_CODE (chrec) == FUNCTION_DECL
      || TREE_CODE (chrec) == LABEL_DECL
      || TREE_CODE (chrec) == RESULT_DECL
      || TREE_CODE (chrec) == FIELD_DECL)
    return true;

  if (TREE_CODE (chrec) == SSA_NAME)
    {
      tree def = SSA_NAME_DEF_STMT (chrec);
      struct loop *def_loop = loop_containing_stmt (def);
      struct loop *loop = get_loop (loop_nb);

      if (def_loop == NULL)
	return false;

      if (loop == def_loop || flow_loop_nested_p (loop, def_loop))
	return true;

      return false;
    }

  n = TREE_OPERAND_LENGTH (chrec);
  for (i = 0; i < n; i++)
    if (chrec_contains_symbols_defined_in_loop (TREE_OPERAND (chrec, i), 
						loop_nb))
      return true;
  return false;
}

/* Return true when PHI is a loop-phi-node.  */

static bool
loop_phi_node_p (tree phi)
{
  /* The implementation of this function is based on the following
     property: "all the loop-phi-nodes of a loop are contained in the
     loop's header basic block".  */

  return loop_containing_stmt (phi)->header == bb_for_stmt (phi);
}

/* Compute the scalar evolution for EVOLUTION_FN after crossing LOOP.
   In general, in the case of multivariate evolutions we want to get
   the evolution in different loops.  LOOP specifies the level for
   which to get the evolution.
   
   Example:
   
   | for (j = 0; j < 100; j++)
   |   {
   |     for (k = 0; k < 100; k++)
   |       {
   |         i = k + j;   - Here the value of i is a function of j, k. 
   |       }
   |      ... = i         - Here the value of i is a function of j. 
   |   }
   | ... = i              - Here the value of i is a scalar.  
   
   Example:  
   
   | i_0 = ...
   | loop_1 10 times
   |   i_1 = phi (i_0, i_2)
   |   i_2 = i_1 + 2
   | endloop
    
   This loop has the same effect as:
   LOOP_1 has the same effect as:
    
   | i_1 = i_0 + 20
   
   The overall effect of the loop, "i_0 + 20" in the previous example, 
   is obtained by passing in the parameters: LOOP = 1, 
   EVOLUTION_FN = {i_0, +, 2}_1.
*/
 
static tree 
compute_overall_effect_of_inner_loop (struct loop *loop, tree evolution_fn)
{
  bool val = false;

  if (evolution_fn == chrec_dont_know)
    return chrec_dont_know;

  else if (TREE_CODE (evolution_fn) == POLYNOMIAL_CHREC)
    {
      struct loop *inner_loop = get_chrec_loop (evolution_fn);

      if (inner_loop == loop
	  || flow_loop_nested_p (loop, inner_loop))
	{
	  tree nb_iter = number_of_latch_executions (inner_loop);

	  if (nb_iter == chrec_dont_know)
	    return chrec_dont_know;
	  else
	    {
	      tree res;

	      /* evolution_fn is the evolution function in LOOP.  Get
		 its value in the nb_iter-th iteration.  */
	      res = chrec_apply (inner_loop->num, evolution_fn, nb_iter);
	      
	      /* Continue the computation until ending on a parent of LOOP.  */
	      return compute_overall_effect_of_inner_loop (loop, res);
	    }
	}
      else
	return evolution_fn;
     }
  
  /* If the evolution function is an invariant, there is nothing to do.  */
  else if (no_evolution_in_loop_p (evolution_fn, loop->num, &val) && val)
    return evolution_fn;
  
  else
    return chrec_dont_know;
}

/* Determine whether the CHREC is always positive/negative.  If the expression
   cannot be statically analyzed, return false, otherwise set the answer into
   VALUE.  */

bool
chrec_is_positive (tree chrec, bool *value)
{
  bool value0, value1, value2;
  tree end_value, nb_iter;
  
  switch (TREE_CODE (chrec))
    {
    case POLYNOMIAL_CHREC:
      if (!chrec_is_positive (CHREC_LEFT (chrec), &value0)
	  || !chrec_is_positive (CHREC_RIGHT (chrec), &value1))
	return false;
     
      /* FIXME -- overflows.  */
      if (value0 == value1)
	{
	  *value = value0;
	  return true;
	}

      /* Otherwise the chrec is under the form: "{-197, +, 2}_1",
	 and the proof consists in showing that the sign never
	 changes during the execution of the loop, from 0 to
	 loop->nb_iterations.  */
      if (!evolution_function_is_affine_p (chrec))
	return false;

      nb_iter = number_of_latch_executions (get_chrec_loop (chrec));
      if (chrec_contains_undetermined (nb_iter))
	return false;

#if 0
      /* TODO -- If the test is after the exit, we may decrease the number of
	 iterations by one.  */
      if (after_exit)
	nb_iter = chrec_fold_minus (type, nb_iter, build_int_cst (type, 1));
#endif

      end_value = chrec_apply (CHREC_VARIABLE (chrec), chrec, nb_iter);
	      
      if (!chrec_is_positive (end_value, &value2))
	return false;
	
      *value = value0;
      return value0 == value1;
      
    case INTEGER_CST:
      *value = (tree_int_cst_sgn (chrec) == 1);
      return true;
      
    default:
      return false;
    }
}

/* Associate CHREC to SCALAR.  */

static void
set_scalar_evolution (tree scalar, tree chrec)
{
  tree *scalar_info;
 
  if (TREE_CODE (scalar) != SSA_NAME)
    return;

  scalar_info = find_var_scev_info (scalar);
  
  if (dump_file)
    {
      if (dump_flags & TDF_DETAILS)
	{
	  fprintf (dump_file, "(set_scalar_evolution \n");
	  fprintf (dump_file, "  (scalar = ");
	  print_generic_expr (dump_file, scalar, 0);
	  fprintf (dump_file, ")\n  (scalar_evolution = ");
	  print_generic_expr (dump_file, chrec, 0);
	  fprintf (dump_file, "))\n");
	}
      if (dump_flags & TDF_STATS)
	nb_set_scev++;
    }
  
  *scalar_info = chrec;
}

/* Retrieve the chrec associated to SCALAR in the LOOP.  */

static tree
get_scalar_evolution (tree scalar)
{
  tree res;
  
  if (dump_file)
    {
      if (dump_flags & TDF_DETAILS)
	{
	  fprintf (dump_file, "(get_scalar_evolution \n");
	  fprintf (dump_file, "  (scalar = ");
	  print_generic_expr (dump_file, scalar, 0);
	  fprintf (dump_file, ")\n");
	}
      if (dump_flags & TDF_STATS)
	nb_get_scev++;
    }
  
  switch (TREE_CODE (scalar))
    {
    case SSA_NAME:
      res = *find_var_scev_info (scalar);
      break;

    case REAL_CST:
    case INTEGER_CST:
      res = scalar;
      break;

    default:
      res = chrec_not_analyzed_yet;
      break;
    }
  
  if (dump_file && (dump_flags & TDF_DETAILS))
    {
      fprintf (dump_file, "  (scalar_evolution = ");
      print_generic_expr (dump_file, res, 0);
      fprintf (dump_file, "))\n");
    }
  
  return res;
}

/* Helper function for add_to_evolution.  Returns the evolution
   function for an assignment of the form "a = b + c", where "a" and
   "b" are on the strongly connected component.  CHREC_BEFORE is the
   information that we already have collected up to this point.
   TO_ADD is the evolution of "c".  
   
   When CHREC_BEFORE has an evolution part in LOOP_NB, add to this
   evolution the expression TO_ADD, otherwise construct an evolution
   part for this loop.  */

static tree
add_to_evolution_1 (unsigned loop_nb, tree chrec_before, tree to_add,
		    tree at_stmt)
{
  tree type, left, right;
  struct loop *loop = get_loop (loop_nb), *chloop;

  switch (TREE_CODE (chrec_before))
    {
    case POLYNOMIAL_CHREC:
      chloop = get_chrec_loop (chrec_before);
      if (chloop == loop
	  || flow_loop_nested_p (chloop, loop))
	{
	  unsigned var;

	  type = chrec_type (chrec_before);
	  
	  /* When there is no evolution part in this loop, build it.  */
	  if (chloop != loop)
	    {
	      var = loop_nb;
	      left = chrec_before;
	      right = SCALAR_FLOAT_TYPE_P (type)
		? build_real (type, dconst0)
		: build_int_cst (type, 0);
	    }
	  else
	    {
	      var = CHREC_VARIABLE (chrec_before);
	      left = CHREC_LEFT (chrec_before);
	      right = CHREC_RIGHT (chrec_before);
	    }

	  to_add = chrec_convert (type, to_add, at_stmt);
	  right = chrec_convert_rhs (type, right, at_stmt);
	  right = chrec_fold_plus (chrec_type (right), right, to_add);
	  return build_polynomial_chrec (var, left, right);
	}
      else
	{
	  gcc_assert (flow_loop_nested_p (loop, chloop));

	  /* Search the evolution in LOOP_NB.  */
	  left = add_to_evolution_1 (loop_nb, CHREC_LEFT (chrec_before),
				     to_add, at_stmt);
	  right = CHREC_RIGHT (chrec_before);
	  right = chrec_convert_rhs (chrec_type (left), right, at_stmt);
	  return build_polynomial_chrec (CHREC_VARIABLE (chrec_before),
					 left, right);
	}
      
    default:
      /* These nodes do not depend on a loop.  */
      if (chrec_before == chrec_dont_know)
	return chrec_dont_know;

      left = chrec_before;
      right = chrec_convert_rhs (chrec_type (left), to_add, at_stmt);
      return build_polynomial_chrec (loop_nb, left, right);
    }
}

/* Add TO_ADD to the evolution part of CHREC_BEFORE in the dimension
   of LOOP_NB.  
   
   Description (provided for completeness, for those who read code in
   a plane, and for my poor 62 bytes brain that would have forgotten
   all this in the next two or three months):
   
   The algorithm of translation of programs from the SSA representation
   into the chrecs syntax is based on a pattern matching.  After having
   reconstructed the overall tree expression for a loop, there are only
   two cases that can arise:
   
   1. a = loop-phi (init, a + expr)
   2. a = loop-phi (init, expr)
   
   where EXPR is either a scalar constant with respect to the analyzed
   loop (this is a degree 0 polynomial), or an expression containing
   other loop-phi definitions (these are higher degree polynomials).
   
   Examples:
   
   1. 
   | init = ...
   | loop_1
   |   a = phi (init, a + 5)
   | endloop
   
   2. 
   | inita = ...
   | initb = ...
   | loop_1
   |   a = phi (inita, 2 * b + 3)
   |   b = phi (initb, b + 1)
   | endloop
   
   For the first case, the semantics of the SSA representation is: 
   
   | a (x) = init + \sum_{j = 0}^{x - 1} expr (j)
   
   that is, there is a loop index "x" that determines the scalar value
   of the variable during the loop execution.  During the first
   iteration, the value is that of the initial condition INIT, while
   during the subsequent iterations, it is the sum of the initial
   condition with the sum of all the values of EXPR from the initial
   iteration to the before last considered iteration.  
   
   For the second case, the semantics of the SSA program is:
   
   | a (x) = init, if x = 0;
   |         expr (x - 1), otherwise.
   
   The second case corresponds to the PEELED_CHREC, whose syntax is
   close to the syntax of a loop-phi-node: 
   
   | phi (init, expr)  vs.  (init, expr)_x
   
   The proof of the translation algorithm for the first case is a
   proof by structural induction based on the degree of EXPR.  
   
   Degree 0:
   When EXPR is a constant with respect to the analyzed loop, or in
   other words when EXPR is a polynomial of degree 0, the evolution of
   the variable A in the loop is an affine function with an initial
   condition INIT, and a step EXPR.  In order to show this, we start
   from the semantics of the SSA representation:
   
   f (x) = init + \sum_{j = 0}^{x - 1} expr (j)
   
   and since "expr (j)" is a constant with respect to "j",
   
   f (x) = init + x * expr 
   
   Finally, based on the semantics of the pure sum chrecs, by
   identification we get the corresponding chrecs syntax:
   
   f (x) = init * \binom{x}{0} + expr * \binom{x}{1} 
   f (x) -> {init, +, expr}_x
   
   Higher degree:
   Suppose that EXPR is a polynomial of degree N with respect to the
   analyzed loop_x for which we have already determined that it is
   written under the chrecs syntax:
   
   | expr (x)  ->  {b_0, +, b_1, +, ..., +, b_{n-1}} (x)
   
   We start from the semantics of the SSA program:
   
   | f (x) = init + \sum_{j = 0}^{x - 1} expr (j)
   |
   | f (x) = init + \sum_{j = 0}^{x - 1} 
   |                (b_0 * \binom{j}{0} + ... + b_{n-1} * \binom{j}{n-1})
   |
   | f (x) = init + \sum_{j = 0}^{x - 1} 
   |                \sum_{k = 0}^{n - 1} (b_k * \binom{j}{k}) 
   |
   | f (x) = init + \sum_{k = 0}^{n - 1} 
   |                (b_k * \sum_{j = 0}^{x - 1} \binom{j}{k}) 
   |
   | f (x) = init + \sum_{k = 0}^{n - 1} 
   |                (b_k * \binom{x}{k + 1}) 
   |
   | f (x) = init + b_0 * \binom{x}{1} + ... 
   |              + b_{n-1} * \binom{x}{n} 
   |
   | f (x) = init * \binom{x}{0} + b_0 * \binom{x}{1} + ... 
   |                             + b_{n-1} * \binom{x}{n} 
   |
   
   And finally from the definition of the chrecs syntax, we identify:
   | f (x)  ->  {init, +, b_0, +, ..., +, b_{n-1}}_x 
   
   This shows the mechanism that stands behind the add_to_evolution
   function.  An important point is that the use of symbolic
   parameters avoids the need of an analysis schedule.
   
   Example:
   
   | inita = ...
   | initb = ...
   | loop_1 
   |   a = phi (inita, a + 2 + b)
   |   b = phi (initb, b + 1)
   | endloop
   
   When analyzing "a", the algorithm keeps "b" symbolically:
   
   | a  ->  {inita, +, 2 + b}_1
   
   Then, after instantiation, the analyzer ends on the evolution:
   
   | a  ->  {inita, +, 2 + initb, +, 1}_1

*/

static tree 
add_to_evolution (unsigned loop_nb, tree chrec_before, enum tree_code code,
		  tree to_add, tree at_stmt)
{
  tree type = chrec_type (to_add);
  tree res = NULL_TREE;
  
  if (to_add == NULL_TREE)
    return chrec_before;
  
  /* TO_ADD is either a scalar, or a parameter.  TO_ADD is not
     instantiated at this point.  */
  if (TREE_CODE (to_add) == POLYNOMIAL_CHREC)
    /* This should not happen.  */
    return chrec_dont_know;
  
  if (dump_file && (dump_flags & TDF_DETAILS))
    {
      fprintf (dump_file, "(add_to_evolution \n");
      fprintf (dump_file, "  (loop_nb = %d)\n", loop_nb);
      fprintf (dump_file, "  (chrec_before = ");
      print_generic_expr (dump_file, chrec_before, 0);
      fprintf (dump_file, ")\n  (to_add = ");
      print_generic_expr (dump_file, to_add, 0);
      fprintf (dump_file, ")\n");
    }

  if (code == MINUS_EXPR)
    to_add = chrec_fold_multiply (type, to_add, SCALAR_FLOAT_TYPE_P (type)
				  ? build_real (type, dconstm1)
				  : build_int_cst_type (type, -1));

  res = add_to_evolution_1 (loop_nb, chrec_before, to_add, at_stmt);

  if (dump_file && (dump_flags & TDF_DETAILS))
    {
      fprintf (dump_file, "  (res = ");
      print_generic_expr (dump_file, res, 0);
      fprintf (dump_file, "))\n");
    }

  return res;
}

/* Helper function.  */

static inline tree
set_nb_iterations_in_loop (struct loop *loop, 
			   tree res)
{
  if (dump_file && (dump_flags & TDF_DETAILS))
    {
      fprintf (dump_file, "  (set_nb_iterations_in_loop = ");
      print_generic_expr (dump_file, res, 0);
      fprintf (dump_file, "))\n");
    }
  
  loop->nb_iterations = res;
  return res;
}



/* This section selects the loops that will be good candidates for the
   scalar evolution analysis.  For the moment, greedily select all the
   loop nests we could analyze.  */

/* Return true when it is possible to analyze the condition expression
   EXPR.  */

static bool
analyzable_condition (tree expr)
{
  tree condition;
  
  if (TREE_CODE (expr) != COND_EXPR)
    return false;
  
  condition = TREE_OPERAND (expr, 0);
  
  switch (TREE_CODE (condition))
    {
    case SSA_NAME:
      return true;
      
    case LT_EXPR:
    case LE_EXPR:
    case GT_EXPR:
    case GE_EXPR:
    case EQ_EXPR:
    case NE_EXPR:
      return true;
      
    default:
      return false;
    }
  
  return false;
}

/* For a loop with a single exit edge, return the COND_EXPR that
   guards the exit edge.  If the expression is too difficult to
   analyze, then give up.  */

tree 
get_loop_exit_condition (struct loop *loop)
{
  tree res = NULL_TREE;
  edge exit_edge = single_exit (loop);
  
  if (dump_file && (dump_flags & TDF_DETAILS))
    fprintf (dump_file, "(get_loop_exit_condition \n  ");
  
  if (exit_edge)
    {
      tree expr;
      
      expr = last_stmt (exit_edge->src);
      if (analyzable_condition (expr))
	res = expr;
    }
  
  if (dump_file && (dump_flags & TDF_DETAILS))
    {
      print_generic_expr (dump_file, res, 0);
      fprintf (dump_file, ")\n");
    }
  
  return res;
}

/* Recursively determine and enqueue the exit conditions for a loop.  */

static void 
get_exit_conditions_rec (struct loop *loop, 
			 VEC(tree,heap) **exit_conditions)
{
  if (!loop)
    return;
  
  /* Recurse on the inner loops, then on the next (sibling) loops.  */
  get_exit_conditions_rec (loop->inner, exit_conditions);
  get_exit_conditions_rec (loop->next, exit_conditions);
  
  if (single_exit (loop))
    {
      tree loop_condition = get_loop_exit_condition (loop);
      
      if (loop_condition)
	VEC_safe_push (tree, heap, *exit_conditions, loop_condition);
    }
}

/* Select the candidate loop nests for the analysis.  This function
   initializes the EXIT_CONDITIONS array.  */

static void
select_loops_exit_conditions (VEC(tree,heap) **exit_conditions)
{
  struct loop *function_body = current_loops->tree_root;
  
  get_exit_conditions_rec (function_body->inner, exit_conditions);
}


/* Depth first search algorithm.  */

typedef enum t_bool {
  t_false,
  t_true,
  t_dont_know
} t_bool;


static t_bool follow_ssa_edge (struct loop *loop, tree, tree, tree *, int);

/* Follow the ssa edge into the right hand side RHS of an assignment.
   Return true if the strongly connected component has been found.  */

static t_bool
follow_ssa_edge_in_rhs (struct loop *loop, tree at_stmt, tree rhs, 
			tree halting_phi, tree *evolution_of_loop, int limit)
{
  t_bool res = t_false;
  tree rhs0, rhs1;
  tree type_rhs = TREE_TYPE (rhs);
  tree evol;
  enum tree_code code;
  
  /* The RHS is one of the following cases:
     - an SSA_NAME, 
     - an INTEGER_CST,
     - a PLUS_EXPR, 
     - a POINTER_PLUS_EXPR, 
     - a MINUS_EXPR,
     - an ASSERT_EXPR,
     - other cases are not yet handled.  */
  code = TREE_CODE (rhs);
  switch (code)
    {
    case NOP_EXPR:
      /* This assignment is under the form "a_1 = (cast) rhs.  */
      res = follow_ssa_edge_in_rhs (loop, at_stmt, TREE_OPERAND (rhs, 0),
				    halting_phi, evolution_of_loop, limit);
      *evolution_of_loop = chrec_convert (TREE_TYPE (rhs),
					  *evolution_of_loop, at_stmt);
      break;

    case INTEGER_CST:
      /* This assignment is under the form "a_1 = 7".  */
      res = t_false;
      break;
      
    case SSA_NAME:
      /* This assignment is under the form: "a_1 = b_2".  */
      res = follow_ssa_edge 
	(loop, SSA_NAME_DEF_STMT (rhs), halting_phi, evolution_of_loop, limit);
      break;
      
    case POINTER_PLUS_EXPR:
    case PLUS_EXPR:
      /* This case is under the form "rhs0 + rhs1".  */
      rhs0 = TREE_OPERAND (rhs, 0);
      rhs1 = TREE_OPERAND (rhs, 1);
      STRIP_TYPE_NOPS (rhs0);
      STRIP_TYPE_NOPS (rhs1);

      if (TREE_CODE (rhs0) == SSA_NAME)
	{
	  if (TREE_CODE (rhs1) == SSA_NAME)
	    {
	      /* Match an assignment under the form: 
		 "a = b + c".  */
      
	      /* We want only assignments of form "name + name" contribute to
		 LIMIT, as the other cases do not necessarily contribute to
		 the complexity of the expression.  */
	      limit++;

	      evol = *evolution_of_loop;
	      res = follow_ssa_edge 
		(loop, SSA_NAME_DEF_STMT (rhs0), halting_phi, 
		 &evol, limit);
	      
	      if (res == t_true)
		*evolution_of_loop = add_to_evolution 
		  (loop->num, 
		   chrec_convert (type_rhs, evol, at_stmt), 
		   code, rhs1, at_stmt);
	      
	      else if (res == t_false)
		{
		  res = follow_ssa_edge 
		    (loop, SSA_NAME_DEF_STMT (rhs1), halting_phi, 
		     evolution_of_loop, limit);
		  
		  if (res == t_true)
		    *evolution_of_loop = add_to_evolution 
		      (loop->num, 
		       chrec_convert (type_rhs, *evolution_of_loop, at_stmt), 
		       code, rhs0, at_stmt);

		  else if (res == t_dont_know)
		    *evolution_of_loop = chrec_dont_know;
		}

	      else if (res == t_dont_know)
		*evolution_of_loop = chrec_dont_know;
	    }
	  
	  else
	    {
	      /* Match an assignment under the form: 
		 "a = b + ...".  */
	      res = follow_ssa_edge 
		(loop, SSA_NAME_DEF_STMT (rhs0), halting_phi, 
		 evolution_of_loop, limit);
	      if (res == t_true)
		*evolution_of_loop = add_to_evolution 
		  (loop->num, chrec_convert (type_rhs, *evolution_of_loop,
					     at_stmt),
		   code, rhs1, at_stmt);

	      else if (res == t_dont_know)
		*evolution_of_loop = chrec_dont_know;
	    }
	}
      
      else if (TREE_CODE (rhs1) == SSA_NAME)
	{
	  /* Match an assignment under the form: 
	     "a = ... + c".  */
	  res = follow_ssa_edge 
	    (loop, SSA_NAME_DEF_STMT (rhs1), halting_phi, 
	     evolution_of_loop, limit);
	  if (res == t_true)
	    *evolution_of_loop = add_to_evolution 
	      (loop->num, chrec_convert (type_rhs, *evolution_of_loop,
					 at_stmt),
	       code, rhs0, at_stmt);

	  else if (res == t_dont_know)
	    *evolution_of_loop = chrec_dont_know;
	}

      else
	/* Otherwise, match an assignment under the form: 
	   "a = ... + ...".  */
	/* And there is nothing to do.  */
	res = t_false;
      
      break;
      
    case MINUS_EXPR:
      /* This case is under the form "opnd0 = rhs0 - rhs1".  */
      rhs0 = TREE_OPERAND (rhs, 0);
      rhs1 = TREE_OPERAND (rhs, 1);
      STRIP_TYPE_NOPS (rhs0);
      STRIP_TYPE_NOPS (rhs1);

      if (TREE_CODE (rhs0) == SSA_NAME)
	{
	  /* Match an assignment under the form: 
	     "a = b - ...".  */

	  /* We want only assignments of form "name - name" contribute to
	     LIMIT, as the other cases do not necessarily contribute to
	     the complexity of the expression.  */
	  if (TREE_CODE (rhs1) == SSA_NAME)
	    limit++;

	  res = follow_ssa_edge (loop, SSA_NAME_DEF_STMT (rhs0), halting_phi, 
				 evolution_of_loop, limit);
	  if (res == t_true)
	    *evolution_of_loop = add_to_evolution 
	      (loop->num, chrec_convert (type_rhs, *evolution_of_loop, at_stmt),
	       MINUS_EXPR, rhs1, at_stmt);

	  else if (res == t_dont_know)
	    *evolution_of_loop = chrec_dont_know;
	}
      else
	/* Otherwise, match an assignment under the form: 
	   "a = ... - ...".  */
	/* And there is nothing to do.  */
	res = t_false;
      
      break;
    
    case ASSERT_EXPR:
      {
	/* This assignment is of the form: "a_1 = ASSERT_EXPR <a_2, ...>"
	   It must be handled as a copy assignment of the form a_1 = a_2.  */
	tree op0 = ASSERT_EXPR_VAR (rhs);
	if (TREE_CODE (op0) == SSA_NAME)
	  res = follow_ssa_edge (loop, SSA_NAME_DEF_STMT (op0),
				 halting_phi, evolution_of_loop, limit);
	else
	  res = t_false;
	break;
      }


    default:
      res = t_false;
      break;
    }
  
  return res;
}

/* Checks whether the I-th argument of a PHI comes from a backedge.  */

static bool
backedge_phi_arg_p (tree phi, int i)
{
  edge e = PHI_ARG_EDGE (phi, i);

  /* We would in fact like to test EDGE_DFS_BACK here, but we do not care
     about updating it anywhere, and this should work as well most of the
     time.  */
  if (e->flags & EDGE_IRREDUCIBLE_LOOP)
    return true;

  return false;
}

/* Helper function for one branch of the condition-phi-node.  Return
   true if the strongly connected component has been found following
   this path.  */

static inline t_bool
follow_ssa_edge_in_condition_phi_branch (int i,
					 struct loop *loop, 
					 tree condition_phi, 
					 tree halting_phi,
					 tree *evolution_of_branch,
					 tree init_cond, int limit)
{
  tree branch = PHI_ARG_DEF (condition_phi, i);
  *evolution_of_branch = chrec_dont_know;

  /* Do not follow back edges (they must belong to an irreducible loop, which
     we really do not want to worry about).  */
  if (backedge_phi_arg_p (condition_phi, i))
    return t_false;

  if (TREE_CODE (branch) == SSA_NAME)
    {
      *evolution_of_branch = init_cond;
      return follow_ssa_edge (loop, SSA_NAME_DEF_STMT (branch), halting_phi, 
			      evolution_of_branch, limit);
    }

  /* This case occurs when one of the condition branches sets 
     the variable to a constant: i.e. a phi-node like
     "a_2 = PHI <a_7(5), 2(6)>;".  
	 
     FIXME:  This case have to be refined correctly: 
     in some cases it is possible to say something better than
     chrec_dont_know, for example using a wrap-around notation.  */
  return t_false;
}

/* This function merges the branches of a condition-phi-node in a
   loop.  */

static t_bool
follow_ssa_edge_in_condition_phi (struct loop *loop,
				  tree condition_phi, 
				  tree halting_phi, 
				  tree *evolution_of_loop, int limit)
{
  int i;
  tree init = *evolution_of_loop;
  tree evolution_of_branch;
  t_bool res = follow_ssa_edge_in_condition_phi_branch (0, loop, condition_phi,
							halting_phi,
							&evolution_of_branch,
							init, limit);
  if (res == t_false || res == t_dont_know)
    return res;

  *evolution_of_loop = evolution_of_branch;

  /* If the phi node is just a copy, do not increase the limit.  */
  if (PHI_NUM_ARGS (condition_phi) > 1)
    limit++;

  for (i = 1; i < PHI_NUM_ARGS (condition_phi); i++)
    {
      /* Quickly give up when the evolution of one of the branches is
	 not known.  */
      if (*evolution_of_loop == chrec_dont_know)
	return t_true;

      res = follow_ssa_edge_in_condition_phi_branch (i, loop, condition_phi,
						     halting_phi,
						     &evolution_of_branch,
						     init, limit);
      if (res == t_false || res == t_dont_know)
	return res;

      *evolution_of_loop = chrec_merge (*evolution_of_loop,
					evolution_of_branch);
    }
  
  return t_true;
}

/* Follow an SSA edge in an inner loop.  It computes the overall
   effect of the loop, and following the symbolic initial conditions,
   it follows the edges in the parent loop.  The inner loop is
   considered as a single statement.  */

static t_bool
follow_ssa_edge_inner_loop_phi (struct loop *outer_loop,
				tree loop_phi_node, 
				tree halting_phi,
				tree *evolution_of_loop, int limit)
{
  struct loop *loop = loop_containing_stmt (loop_phi_node);
  tree ev = analyze_scalar_evolution (loop, PHI_RESULT (loop_phi_node));

  /* Sometimes, the inner loop is too difficult to analyze, and the
     result of the analysis is a symbolic parameter.  */
  if (ev == PHI_RESULT (loop_phi_node))
    {
      t_bool res = t_false;
      int i;

      for (i = 0; i < PHI_NUM_ARGS (loop_phi_node); i++)
	{
	  tree arg = PHI_ARG_DEF (loop_phi_node, i);
	  basic_block bb;

	  /* Follow the edges that exit the inner loop.  */
	  bb = PHI_ARG_EDGE (loop_phi_node, i)->src;
	  if (!flow_bb_inside_loop_p (loop, bb))
	    res = follow_ssa_edge_in_rhs (outer_loop, loop_phi_node,
					  arg, halting_phi,
					  evolution_of_loop, limit);
	  if (res == t_true)
	    break;
	}

      /* If the path crosses this loop-phi, give up.  */
      if (res == t_true)
	*evolution_of_loop = chrec_dont_know;

      return res;
    }

  /* Otherwise, compute the overall effect of the inner loop.  */
  ev = compute_overall_effect_of_inner_loop (loop, ev);
  return follow_ssa_edge_in_rhs (outer_loop, loop_phi_node, ev, halting_phi,
				 evolution_of_loop, limit);
}

/* Follow an SSA edge from a loop-phi-node to itself, constructing a
   path that is analyzed on the return walk.  */

static t_bool
follow_ssa_edge (struct loop *loop, tree def, tree halting_phi,
		 tree *evolution_of_loop, int limit)
{
  struct loop *def_loop;
  
  if (TREE_CODE (def) == NOP_EXPR)
    return t_false;
  
  /* Give up if the path is longer than the MAX that we allow.  */
  if (limit > PARAM_VALUE (PARAM_SCEV_MAX_EXPR_SIZE))
    return t_dont_know;
  
  def_loop = loop_containing_stmt (def);
  
  switch (TREE_CODE (def))
    {
    case PHI_NODE:
      if (!loop_phi_node_p (def))
	/* DEF is a condition-phi-node.  Follow the branches, and
	   record their evolutions.  Finally, merge the collected
	   information and set the approximation to the main
	   variable.  */
	return follow_ssa_edge_in_condition_phi 
	  (loop, def, halting_phi, evolution_of_loop, limit);

      /* When the analyzed phi is the halting_phi, the
	 depth-first search is over: we have found a path from
	 the halting_phi to itself in the loop.  */
      if (def == halting_phi)
	return t_true;
	  
      /* Otherwise, the evolution of the HALTING_PHI depends
	 on the evolution of another loop-phi-node, i.e. the
	 evolution function is a higher degree polynomial.  */
      if (def_loop == loop)
	return t_false;
	  
      /* Inner loop.  */
      if (flow_loop_nested_p (loop, def_loop))
	return follow_ssa_edge_inner_loop_phi 
	  (loop, def, halting_phi, evolution_of_loop, limit + 1);

      /* Outer loop.  */
      return t_false;

    case GIMPLE_MODIFY_STMT:
      return follow_ssa_edge_in_rhs (loop, def,
				     GIMPLE_STMT_OPERAND (def, 1), 
				     halting_phi, 
				     evolution_of_loop, limit);
      
    default:
      /* At this level of abstraction, the program is just a set
	 of GIMPLE_MODIFY_STMTs and PHI_NODEs.  In principle there is no
	 other node to be handled.  */
      return t_false;
    }
}



/* Given a LOOP_PHI_NODE, this function determines the evolution
   function from LOOP_PHI_NODE to LOOP_PHI_NODE in the loop.  */

static tree
analyze_evolution_in_loop (tree loop_phi_node, 
			   tree init_cond)
{
  int i;
  tree evolution_function = chrec_not_analyzed_yet;
  struct loop *loop = loop_containing_stmt (loop_phi_node);
  basic_block bb;
  
  if (dump_file && (dump_flags & TDF_DETAILS))
    {
      fprintf (dump_file, "(analyze_evolution_in_loop \n");
      fprintf (dump_file, "  (loop_phi_node = ");
      print_generic_expr (dump_file, loop_phi_node, 0);
      fprintf (dump_file, ")\n");
    }
  
  for (i = 0; i < PHI_NUM_ARGS (loop_phi_node); i++)
    {
      tree arg = PHI_ARG_DEF (loop_phi_node, i);
      tree ssa_chain, ev_fn;
      t_bool res;

      /* Select the edges that enter the loop body.  */
      bb = PHI_ARG_EDGE (loop_phi_node, i)->src;
      if (!flow_bb_inside_loop_p (loop, bb))
	continue;
      
      if (TREE_CODE (arg) == SSA_NAME)
	{
	  ssa_chain = SSA_NAME_DEF_STMT (arg);

	  /* Pass in the initial condition to the follow edge function.  */
	  ev_fn = init_cond;
	  res = follow_ssa_edge (loop, ssa_chain, loop_phi_node, &ev_fn, 0);
	}
      else
	res = t_false;
	      
      /* When it is impossible to go back on the same
	 loop_phi_node by following the ssa edges, the
	 evolution is represented by a peeled chrec, i.e. the
	 first iteration, EV_FN has the value INIT_COND, then
	 all the other iterations it has the value of ARG.  
	 For the moment, PEELED_CHREC nodes are not built.  */
      if (res != t_true)
	ev_fn = chrec_dont_know;
      
      /* When there are multiple back edges of the loop (which in fact never
	 happens currently, but nevertheless), merge their evolutions.  */
      evolution_function = chrec_merge (evolution_function, ev_fn);
    }
  
  if (dump_file && (dump_flags & TDF_DETAILS))
    {
      fprintf (dump_file, "  (evolution_function = ");
      print_generic_expr (dump_file, evolution_function, 0);
      fprintf (dump_file, "))\n");
    }
  
  return evolution_function;
}

/* Given a loop-phi-node, return the initial conditions of the
   variable on entry of the loop.  When the CCP has propagated
   constants into the loop-phi-node, the initial condition is
   instantiated, otherwise the initial condition is kept symbolic.
   This analyzer does not analyze the evolution outside the current
   loop, and leaves this task to the on-demand tree reconstructor.  */

static tree 
analyze_initial_condition (tree loop_phi_node)
{
  int i;
  tree init_cond = chrec_not_analyzed_yet;
  struct loop *loop = bb_for_stmt (loop_phi_node)->loop_father;
  
  if (dump_file && (dump_flags & TDF_DETAILS))
    {
      fprintf (dump_file, "(analyze_initial_condition \n");
      fprintf (dump_file, "  (loop_phi_node = \n");
      print_generic_expr (dump_file, loop_phi_node, 0);
      fprintf (dump_file, ")\n");
    }
  
  for (i = 0; i < PHI_NUM_ARGS (loop_phi_node); i++)
    {
      tree branch = PHI_ARG_DEF (loop_phi_node, i);
      basic_block bb = PHI_ARG_EDGE (loop_phi_node, i)->src;
      
      /* When the branch is oriented to the loop's body, it does
     	 not contribute to the initial condition.  */
      if (flow_bb_inside_loop_p (loop, bb))
       	continue;

      if (init_cond == chrec_not_analyzed_yet)
	{
	  init_cond = branch;
	  continue;
	}

      if (TREE_CODE (branch) == SSA_NAME)
	{
	  init_cond = chrec_dont_know;
      	  break;
	}

      init_cond = chrec_merge (init_cond, branch);
    }

  /* Ooops -- a loop without an entry???  */
  if (init_cond == chrec_not_analyzed_yet)
    init_cond = chrec_dont_know;

  if (dump_file && (dump_flags & TDF_DETAILS))
    {
      fprintf (dump_file, "  (init_cond = ");
      print_generic_expr (dump_file, init_cond, 0);
      fprintf (dump_file, "))\n");
    }
  
  return init_cond;
}

/* Analyze the scalar evolution for LOOP_PHI_NODE.  */

static tree 
interpret_loop_phi (struct loop *loop, tree loop_phi_node)
{
  tree res;
  struct loop *phi_loop = loop_containing_stmt (loop_phi_node);
  tree init_cond;
  
  if (phi_loop != loop)
    {
      struct loop *subloop;
      tree evolution_fn = analyze_scalar_evolution
	(phi_loop, PHI_RESULT (loop_phi_node));

      /* Dive one level deeper.  */
      subloop = superloop_at_depth (phi_loop, loop_depth (loop) + 1);

      /* Interpret the subloop.  */
      res = compute_overall_effect_of_inner_loop (subloop, evolution_fn);
      return res;
    }

  /* Otherwise really interpret the loop phi.  */
  init_cond = analyze_initial_condition (loop_phi_node);
  res = analyze_evolution_in_loop (loop_phi_node, init_cond);

  return res;
}

/* This function merges the branches of a condition-phi-node,
   contained in the outermost loop, and whose arguments are already
   analyzed.  */

static tree
interpret_condition_phi (struct loop *loop, tree condition_phi)
{
  int i;
  tree res = chrec_not_analyzed_yet;
  
  for (i = 0; i < PHI_NUM_ARGS (condition_phi); i++)
    {
      tree branch_chrec;
      
      if (backedge_phi_arg_p (condition_phi, i))
	{
	  res = chrec_dont_know;
	  break;
	}

      branch_chrec = analyze_scalar_evolution
	(loop, PHI_ARG_DEF (condition_phi, i));
      
      res = chrec_merge (res, branch_chrec);
    }

  return res;
}

/* Interpret the right hand side of a GIMPLE_MODIFY_STMT OPND1.  If we didn't
   analyze this node before, follow the definitions until ending
   either on an analyzed GIMPLE_MODIFY_STMT, or on a loop-phi-node.  On the
   return path, this function propagates evolutions (ala constant copy
   propagation).  OPND1 is not a GIMPLE expression because we could
   analyze the effect of an inner loop: see interpret_loop_phi.  */

static tree
interpret_rhs_modify_stmt (struct loop *loop, tree at_stmt,
			   	  tree opnd1, tree type)
{
  tree res, opnd10, opnd11, chrec10, chrec11;

  if (is_gimple_min_invariant (opnd1))
    return chrec_convert (type, opnd1, at_stmt);

  switch (TREE_CODE (opnd1))
    {
    case POINTER_PLUS_EXPR:
      opnd10 = TREE_OPERAND (opnd1, 0);
      opnd11 = TREE_OPERAND (opnd1, 1);
      chrec10 = analyze_scalar_evolution (loop, opnd10);
      chrec11 = analyze_scalar_evolution (loop, opnd11);
      chrec10 = chrec_convert (type, chrec10, at_stmt);
      chrec11 = chrec_convert (sizetype, chrec11, at_stmt);
      res = chrec_fold_plus (type, chrec10, chrec11);
      break;

    case PLUS_EXPR:
      opnd10 = TREE_OPERAND (opnd1, 0);
      opnd11 = TREE_OPERAND (opnd1, 1);
      chrec10 = analyze_scalar_evolution (loop, opnd10);
      chrec11 = analyze_scalar_evolution (loop, opnd11);
      chrec10 = chrec_convert (type, chrec10, at_stmt);
      chrec11 = chrec_convert (type, chrec11, at_stmt);
      res = chrec_fold_plus (type, chrec10, chrec11);
      break;
      
    case MINUS_EXPR:
      opnd10 = TREE_OPERAND (opnd1, 0);
      opnd11 = TREE_OPERAND (opnd1, 1);
      chrec10 = analyze_scalar_evolution (loop, opnd10);
      chrec11 = analyze_scalar_evolution (loop, opnd11);
      chrec10 = chrec_convert (type, chrec10, at_stmt);
      chrec11 = chrec_convert (type, chrec11, at_stmt);
      res = chrec_fold_minus (type, chrec10, chrec11);
      break;

    case NEGATE_EXPR:
      opnd10 = TREE_OPERAND (opnd1, 0);
      chrec10 = analyze_scalar_evolution (loop, opnd10);
      chrec10 = chrec_convert (type, chrec10, at_stmt);
      /* TYPE may be integer, real or complex, so use fold_convert.  */
      res = chrec_fold_multiply (type, chrec10,
				 fold_convert (type, integer_minus_one_node));
      break;

    case MULT_EXPR:
      opnd10 = TREE_OPERAND (opnd1, 0);
      opnd11 = TREE_OPERAND (opnd1, 1);
      chrec10 = analyze_scalar_evolution (loop, opnd10);
      chrec11 = analyze_scalar_evolution (loop, opnd11);
      chrec10 = chrec_convert (type, chrec10, at_stmt);
      chrec11 = chrec_convert (type, chrec11, at_stmt);
      res = chrec_fold_multiply (type, chrec10, chrec11);
      break;
      
    case SSA_NAME:
      res = chrec_convert (type, analyze_scalar_evolution (loop, opnd1),
			   at_stmt);
      break;

    case ASSERT_EXPR:
      opnd10 = ASSERT_EXPR_VAR (opnd1);
      res = chrec_convert (type, analyze_scalar_evolution (loop, opnd10),
			   at_stmt);
      break;
      
    case NOP_EXPR:
    case CONVERT_EXPR:
      opnd10 = TREE_OPERAND (opnd1, 0);
      chrec10 = analyze_scalar_evolution (loop, opnd10);
      res = chrec_convert (type, chrec10, at_stmt);
      break;
      
    default:
      res = chrec_dont_know;
      break;
    }
  
  return res;
}



/* This section contains all the entry points: 
   - number_of_iterations_in_loop,
   - analyze_scalar_evolution,
   - instantiate_parameters.
*/

/* Compute and return the evolution function in WRTO_LOOP, the nearest
   common ancestor of DEF_LOOP and USE_LOOP.  */

static tree 
compute_scalar_evolution_in_loop (struct loop *wrto_loop, 
				  struct loop *def_loop, 
				  tree ev)
{
  tree res;
  if (def_loop == wrto_loop)
    return ev;

  def_loop = superloop_at_depth (def_loop, loop_depth (wrto_loop) + 1);
  res = compute_overall_effect_of_inner_loop (def_loop, ev);

  return analyze_scalar_evolution_1 (wrto_loop, res, chrec_not_analyzed_yet);
}

/* Helper recursive function.  */

static tree
analyze_scalar_evolution_1 (struct loop *loop, tree var, tree res)
{
  tree def, type = TREE_TYPE (var);
  basic_block bb;
  struct loop *def_loop;

  if (loop == NULL || TREE_CODE (type) == VECTOR_TYPE)
    return chrec_dont_know;

  if (TREE_CODE (var) != SSA_NAME)
    return interpret_rhs_modify_stmt (loop, NULL_TREE, var, type);

  def = SSA_NAME_DEF_STMT (var);
  bb = bb_for_stmt (def);
  def_loop = bb ? bb->loop_father : NULL;

  if (bb == NULL
      || !flow_bb_inside_loop_p (loop, bb))
    {
      /* Keep the symbolic form.  */
      res = var;
      goto set_and_end;
    }

  if (res != chrec_not_analyzed_yet)
    {
      if (loop != bb->loop_father)
	res = compute_scalar_evolution_in_loop 
	    (find_common_loop (loop, bb->loop_father), bb->loop_father, res);

      goto set_and_end;
    }

  if (loop != def_loop)
    {
      res = analyze_scalar_evolution_1 (def_loop, var, chrec_not_analyzed_yet);
      res = compute_scalar_evolution_in_loop (loop, def_loop, res);

      goto set_and_end;
    }

  switch (TREE_CODE (def))
    {
    case GIMPLE_MODIFY_STMT:
      res = interpret_rhs_modify_stmt (loop, def,
	  			       GIMPLE_STMT_OPERAND (def, 1), type);
      break;

    case PHI_NODE:
      if (loop_phi_node_p (def))
	res = interpret_loop_phi (loop, def);
      else
	res = interpret_condition_phi (loop, def);
      break;

    default:
      res = chrec_dont_know;
      break;
    }

 set_and_end:

  /* Keep the symbolic form.  */
  if (res == chrec_dont_know)
    res = var;

  if (loop == def_loop)
    set_scalar_evolution (var, res);

  return res;
}

/* Entry point for the scalar evolution analyzer.
   Analyzes and returns the scalar evolution of the ssa_name VAR.
   LOOP_NB is the identifier number of the loop in which the variable
   is used.
   
   Example of use: having a pointer VAR to a SSA_NAME node, STMT a
   pointer to the statement that uses this variable, in order to
   determine the evolution function of the variable, use the following
   calls:
   
   unsigned loop_nb = loop_containing_stmt (stmt)->num;
   tree chrec_with_symbols = analyze_scalar_evolution (loop_nb, var);
   tree chrec_instantiated = instantiate_parameters 
   (loop_nb, chrec_with_symbols);
*/

tree 
analyze_scalar_evolution (struct loop *loop, tree var)
{
  tree res;

  if (dump_file && (dump_flags & TDF_DETAILS))
    {
      fprintf (dump_file, "(analyze_scalar_evolution \n");
      fprintf (dump_file, "  (loop_nb = %d)\n", loop->num);
      fprintf (dump_file, "  (scalar = ");
      print_generic_expr (dump_file, var, 0);
      fprintf (dump_file, ")\n");
    }

  res = analyze_scalar_evolution_1 (loop, var, get_scalar_evolution (var));

  if (TREE_CODE (var) == SSA_NAME && res == chrec_dont_know)
    res = var;

  if (dump_file && (dump_flags & TDF_DETAILS))
    fprintf (dump_file, ")\n");

  return res;
}

/* Analyze scalar evolution of use of VERSION in USE_LOOP with respect to
   WRTO_LOOP (which should be a superloop of both USE_LOOP and definition
   of VERSION).

   FOLDED_CASTS is set to true if resolve_mixers used
   chrec_convert_aggressive (TODO -- not really, we are way too conservative
   at the moment in order to keep things simple).  */

static tree
analyze_scalar_evolution_in_loop (struct loop *wrto_loop, struct loop *use_loop,
				  tree version, bool *folded_casts)
{
  bool val = false;
  tree ev = version, tmp;

  if (folded_casts)
    *folded_casts = false;
  while (1)
    {
      tmp = analyze_scalar_evolution (use_loop, ev);
      ev = resolve_mixers (use_loop, tmp);

      if (folded_casts && tmp != ev)
	*folded_casts = true;

      if (use_loop == wrto_loop)
	return ev;

      /* If the value of the use changes in the inner loop, we cannot express
	 its value in the outer loop (we might try to return interval chrec,
	 but we do not have a user for it anyway)  */
      if (!no_evolution_in_loop_p (ev, use_loop->num, &val)
	  || !val)
	return chrec_dont_know;

      use_loop = loop_outer (use_loop);
    }
}

/* Returns instantiated value for VERSION in CACHE.  */

static tree
get_instantiated_value (htab_t cache, tree version)
{
  struct scev_info_str *info, pattern;
  
  pattern.var = version;
  info = (struct scev_info_str *) htab_find (cache, &pattern);

  if (info)
    return info->chrec;
  else
    return NULL_TREE;
}

/* Sets instantiated value for VERSION to VAL in CACHE.  */

static void
set_instantiated_value (htab_t cache, tree version, tree val)
{
  struct scev_info_str *info, pattern;
  PTR *slot;
  
  pattern.var = version;
  slot = htab_find_slot (cache, &pattern, INSERT);

  if (!*slot)
    *slot = new_scev_info_str (version);
  info = (struct scev_info_str *) *slot;
  info->chrec = val;
}

/* Return the closed_loop_phi node for VAR.  If there is none, return
   NULL_TREE.  */

static tree
loop_closed_phi_def (tree var)
{
  struct loop *loop;
  edge exit;
  tree phi;

  if (var == NULL_TREE
      || TREE_CODE (var) != SSA_NAME)
    return NULL_TREE;

  loop = loop_containing_stmt (SSA_NAME_DEF_STMT (var));
  exit = single_exit (loop);
  if (!exit)
    return NULL_TREE;

  for (phi = phi_nodes (exit->dest); phi; phi = PHI_CHAIN (phi))
    if (PHI_ARG_DEF_FROM_EDGE (phi, exit) == var)
      return PHI_RESULT (phi);

  return NULL_TREE;
}

/* Analyze all the parameters of the chrec that were left under a symbolic form,
   with respect to LOOP.  CHREC is the chrec to instantiate.  CACHE is the cache
   of already instantiated values.  FLAGS modify the way chrecs are
   instantiated.  SIZE_EXPR is used for computing the size of the expression to
   be instantiated, and to stop if it exceeds some limit.  */

/* Values for FLAGS.  */
enum
{
  INSERT_SUPERLOOP_CHRECS = 1,  /* Loop invariants are replaced with chrecs
				   in outer loops.  */
  FOLD_CONVERSIONS = 2		/* The conversions that may wrap in
				   signed/pointer type are folded, as long as the
				   value of the chrec is preserved.  */
};
  
static tree
instantiate_parameters_1 (struct loop *loop, tree chrec, int flags, htab_t cache,
			  int size_expr)
{
  tree res, op0, op1, op2;
  basic_block def_bb;
  struct loop *def_loop;
  tree type = chrec_type (chrec);

  /* Give up if the expression is larger than the MAX that we allow.  */
  if (size_expr++ > PARAM_VALUE (PARAM_SCEV_MAX_EXPR_SIZE))
    return chrec_dont_know;

  if (automatically_generated_chrec_p (chrec)
      || is_gimple_min_invariant (chrec))
    return chrec;

  switch (TREE_CODE (chrec))
    {
    case SSA_NAME:
      def_bb = bb_for_stmt (SSA_NAME_DEF_STMT (chrec));

      /* A parameter (or loop invariant and we do not want to include
	 evolutions in outer loops), nothing to do.  */
      if (!def_bb
	  || (!(flags & INSERT_SUPERLOOP_CHRECS)
	      && !flow_bb_inside_loop_p (loop, def_bb)))
	return chrec;

      /* We cache the value of instantiated variable to avoid exponential
	 time complexity due to reevaluations.  We also store the convenient
	 value in the cache in order to prevent infinite recursion -- we do
	 not want to instantiate the SSA_NAME if it is in a mixer
	 structure.  This is used for avoiding the instantiation of
	 recursively defined functions, such as: 

	 | a_2 -> {0, +, 1, +, a_2}_1  */

      res = get_instantiated_value (cache, chrec);
      if (res)
	return res;

      /* Store the convenient value for chrec in the structure.  If it
	 is defined outside of the loop, we may just leave it in symbolic
	 form, otherwise we need to admit that we do not know its behavior
	 inside the loop.  */
      res = !flow_bb_inside_loop_p (loop, def_bb) ? chrec : chrec_dont_know;
      set_instantiated_value (cache, chrec, res);

      /* To make things even more complicated, instantiate_parameters_1
	 calls analyze_scalar_evolution that may call # of iterations
	 analysis that may in turn call instantiate_parameters_1 again.
	 To prevent the infinite recursion, keep also the bitmap of
	 ssa names that are being instantiated globally.  */
      if (bitmap_bit_p (already_instantiated, SSA_NAME_VERSION (chrec)))
	return res;

      def_loop = find_common_loop (loop, def_bb->loop_father);

      /* If the analysis yields a parametric chrec, instantiate the
	 result again.  */
      bitmap_set_bit (already_instantiated, SSA_NAME_VERSION (chrec));
      res = analyze_scalar_evolution (def_loop, chrec);

      /* Don't instantiate loop-closed-ssa phi nodes.  */
      if (TREE_CODE (res) == SSA_NAME
	  && (loop_containing_stmt (SSA_NAME_DEF_STMT (res)) == NULL
	      || (loop_depth (loop_containing_stmt (SSA_NAME_DEF_STMT (res)))
		  > loop_depth (def_loop))))
	{
	  if (res == chrec)
	    res = loop_closed_phi_def (chrec);
	  else
	    res = chrec;

	  if (res == NULL_TREE)
	    res = chrec_dont_know;
	}

      else if (res != chrec_dont_know)
	res = instantiate_parameters_1 (loop, res, flags, cache, size_expr);

      bitmap_clear_bit (already_instantiated, SSA_NAME_VERSION (chrec));

      /* Store the correct value to the cache.  */
      set_instantiated_value (cache, chrec, res);
      return res;

    case POLYNOMIAL_CHREC:
      op0 = instantiate_parameters_1 (loop, CHREC_LEFT (chrec),
				      flags, cache, size_expr);
      if (op0 == chrec_dont_know)
	return chrec_dont_know;

      op1 = instantiate_parameters_1 (loop, CHREC_RIGHT (chrec),
				      flags, cache, size_expr);
      if (op1 == chrec_dont_know)
	return chrec_dont_know;

      if (CHREC_LEFT (chrec) != op0
	  || CHREC_RIGHT (chrec) != op1)
	{
	  op1 = chrec_convert_rhs (chrec_type (op0), op1, NULL_TREE);
	  chrec = build_polynomial_chrec (CHREC_VARIABLE (chrec), op0, op1);
	}
      return chrec;

    case POINTER_PLUS_EXPR:
    case PLUS_EXPR:
      op0 = instantiate_parameters_1 (loop, TREE_OPERAND (chrec, 0),
				      flags, cache, size_expr);
      if (op0 == chrec_dont_know)
	return chrec_dont_know;

      op1 = instantiate_parameters_1 (loop, TREE_OPERAND (chrec, 1),
				      flags, cache, size_expr);
      if (op1 == chrec_dont_know)
	return chrec_dont_know;

      if (TREE_OPERAND (chrec, 0) != op0
	  || TREE_OPERAND (chrec, 1) != op1)
	{
	  op0 = chrec_convert (type, op0, NULL_TREE);
	  op1 = chrec_convert_rhs (type, op1, NULL_TREE);
	  chrec = chrec_fold_plus (type, op0, op1);
	}
      return chrec;

    case MINUS_EXPR:
      op0 = instantiate_parameters_1 (loop, TREE_OPERAND (chrec, 0),
				      flags, cache, size_expr);
      if (op0 == chrec_dont_know)
	return chrec_dont_know;

      op1 = instantiate_parameters_1 (loop, TREE_OPERAND (chrec, 1),
				      flags, cache, size_expr);
      if (op1 == chrec_dont_know)
	return chrec_dont_know;

      if (TREE_OPERAND (chrec, 0) != op0
	  || TREE_OPERAND (chrec, 1) != op1)
	{
	  op0 = chrec_convert (type, op0, NULL_TREE);
	  op1 = chrec_convert (type, op1, NULL_TREE);
	  chrec = chrec_fold_minus (type, op0, op1);
	}
      return chrec;

    case MULT_EXPR:
      op0 = instantiate_parameters_1 (loop, TREE_OPERAND (chrec, 0),
				      flags, cache, size_expr);
      if (op0 == chrec_dont_know)
	return chrec_dont_know;

      op1 = instantiate_parameters_1 (loop, TREE_OPERAND (chrec, 1),
				      flags, cache, size_expr);
      if (op1 == chrec_dont_know)
	return chrec_dont_know;

      if (TREE_OPERAND (chrec, 0) != op0
	  || TREE_OPERAND (chrec, 1) != op1)
	{
	  op0 = chrec_convert (type, op0, NULL_TREE);
	  op1 = chrec_convert (type, op1, NULL_TREE);
	  chrec = chrec_fold_multiply (type, op0, op1);
	}
      return chrec;

    case NOP_EXPR:
    case CONVERT_EXPR:
    case NON_LVALUE_EXPR:
      op0 = instantiate_parameters_1 (loop, TREE_OPERAND (chrec, 0),
				      flags, cache, size_expr);
      if (op0 == chrec_dont_know)
        return chrec_dont_know;

      if (flags & FOLD_CONVERSIONS)
	{
	  tree tmp = chrec_convert_aggressive (TREE_TYPE (chrec), op0);
	  if (tmp)
	    return tmp;
	}

      if (op0 == TREE_OPERAND (chrec, 0))
	return chrec;

      /* If we used chrec_convert_aggressive, we can no longer assume that
	 signed chrecs do not overflow, as chrec_convert does, so avoid
         calling it in that case.  */
      if (flags & FOLD_CONVERSIONS)
	return fold_convert (TREE_TYPE (chrec), op0);

      return chrec_convert (TREE_TYPE (chrec), op0, NULL_TREE);

    case SCEV_NOT_KNOWN:
      return chrec_dont_know;

    case SCEV_KNOWN:
      return chrec_known;
                                     
    default:
      break;
    }

  gcc_assert (!VL_EXP_CLASS_P (chrec));
  switch (TREE_CODE_LENGTH (TREE_CODE (chrec)))
    {
    case 3:
      op0 = instantiate_parameters_1 (loop, TREE_OPERAND (chrec, 0),
				      flags, cache, size_expr);
      if (op0 == chrec_dont_know)
	return chrec_dont_know;

      op1 = instantiate_parameters_1 (loop, TREE_OPERAND (chrec, 1),
				      flags, cache, size_expr);
      if (op1 == chrec_dont_know)
	return chrec_dont_know;

      op2 = instantiate_parameters_1 (loop, TREE_OPERAND (chrec, 2),
				      flags, cache, size_expr);
      if (op2 == chrec_dont_know)
        return chrec_dont_know;

      if (op0 == TREE_OPERAND (chrec, 0)
	  && op1 == TREE_OPERAND (chrec, 1)
	  && op2 == TREE_OPERAND (chrec, 2))
	return chrec;

      return fold_build3 (TREE_CODE (chrec),
			  TREE_TYPE (chrec), op0, op1, op2);

    case 2:
      op0 = instantiate_parameters_1 (loop, TREE_OPERAND (chrec, 0),
				      flags, cache, size_expr);
      if (op0 == chrec_dont_know)
	return chrec_dont_know;

      op1 = instantiate_parameters_1 (loop, TREE_OPERAND (chrec, 1),
				      flags, cache, size_expr);
      if (op1 == chrec_dont_know)
        return chrec_dont_know;

      if (op0 == TREE_OPERAND (chrec, 0)
	  && op1 == TREE_OPERAND (chrec, 1))
	return chrec;
      return fold_build2 (TREE_CODE (chrec), TREE_TYPE (chrec), op0, op1);
	    
    case 1:
      op0 = instantiate_parameters_1 (loop, TREE_OPERAND (chrec, 0),
				      flags, cache, size_expr);
      if (op0 == chrec_dont_know)
        return chrec_dont_know;
      if (op0 == TREE_OPERAND (chrec, 0))
	return chrec;
      return fold_build1 (TREE_CODE (chrec), TREE_TYPE (chrec), op0);

    case 0:
      return chrec;

    default:
      break;
    }

  /* Too complicated to handle.  */
  return chrec_dont_know;
}

/* Analyze all the parameters of the chrec that were left under a
   symbolic form.  LOOP is the loop in which symbolic names have to
   be analyzed and instantiated.  */

tree
instantiate_parameters (struct loop *loop,
			tree chrec)
{
  tree res;
  htab_t cache = htab_create (10, hash_scev_info, eq_scev_info, del_scev_info);

  if (dump_file && (dump_flags & TDF_DETAILS))
    {
      fprintf (dump_file, "(instantiate_parameters \n");
      fprintf (dump_file, "  (loop_nb = %d)\n", loop->num);
      fprintf (dump_file, "  (chrec = ");
      print_generic_expr (dump_file, chrec, 0);
      fprintf (dump_file, ")\n");
    }
 
  res = instantiate_parameters_1 (loop, chrec, INSERT_SUPERLOOP_CHRECS, cache,
				  0);

  if (dump_file && (dump_flags & TDF_DETAILS))
    {
      fprintf (dump_file, "  (res = ");
      print_generic_expr (dump_file, res, 0);
      fprintf (dump_file, "))\n");
    }

  htab_delete (cache);
  
  return res;
}

/* Similar to instantiate_parameters, but does not introduce the
   evolutions in outer loops for LOOP invariants in CHREC, and does not
   care about causing overflows, as long as they do not affect value
   of an expression.  */

tree
resolve_mixers (struct loop *loop, tree chrec)
{
  htab_t cache = htab_create (10, hash_scev_info, eq_scev_info, del_scev_info);
  tree ret = instantiate_parameters_1 (loop, chrec, FOLD_CONVERSIONS, cache, 0);
  htab_delete (cache);
  return ret;
}

/* Entry point for the analysis of the number of iterations pass.  
   This function tries to safely approximate the number of iterations
   the loop will run.  When this property is not decidable at compile
   time, the result is chrec_dont_know.  Otherwise the result is
   a scalar or a symbolic parameter.
   
   Example of analysis: suppose that the loop has an exit condition:
   
   "if (b > 49) goto end_loop;"
   
   and that in a previous analysis we have determined that the
   variable 'b' has an evolution function:
   
   "EF = {23, +, 5}_2".  
   
   When we evaluate the function at the point 5, i.e. the value of the
   variable 'b' after 5 iterations in the loop, we have EF (5) = 48,
   and EF (6) = 53.  In this case the value of 'b' on exit is '53' and
   the loop body has been executed 6 times.  */

tree 
number_of_latch_executions (struct loop *loop)
{
  tree res, type;
  edge exit;
  struct tree_niter_desc niter_desc;

  /* Determine whether the number_of_iterations_in_loop has already
     been computed.  */
  res = loop->nb_iterations;
  if (res)
    return res;
  res = chrec_dont_know;

  if (dump_file && (dump_flags & TDF_DETAILS))
    fprintf (dump_file, "(number_of_iterations_in_loop\n");
  
  exit = single_exit (loop);
  if (!exit)
    goto end;

  if (!number_of_iterations_exit (loop, exit, &niter_desc, false))
    goto end;

  type = TREE_TYPE (niter_desc.niter);
  if (integer_nonzerop (niter_desc.may_be_zero))
    res = build_int_cst (type, 0);
  else if (integer_zerop (niter_desc.may_be_zero))
    res = niter_desc.niter;
  else
    res = chrec_dont_know;

end:
  return set_nb_iterations_in_loop (loop, res);
}

/* Returns the number of executions of the exit condition of LOOP,
   i.e., the number by one higher than number_of_latch_executions.
   Note that unline number_of_latch_executions, this number does
   not necessarily fit in the unsigned variant of the type of
   the control variable -- if the number of iterations is a constant,
   we return chrec_dont_know if adding one to number_of_latch_executions
   overflows; however, in case the number of iterations is symbolic
   expression, the caller is responsible for dealing with this
   the possible overflow.  */

tree 
number_of_exit_cond_executions (struct loop *loop)
{
  tree ret = number_of_latch_executions (loop);
  tree type = chrec_type (ret);

  if (chrec_contains_undetermined (ret))
    return ret;

  ret = chrec_fold_plus (type, ret, build_int_cst (type, 1));
  if (TREE_CODE (ret) == INTEGER_CST
      && TREE_OVERFLOW (ret))
    return chrec_dont_know;

  return ret;
}

/* One of the drivers for testing the scalar evolutions analysis.
   This function computes the number of iterations for all the loops
   from the EXIT_CONDITIONS array.  */

static void 
number_of_iterations_for_all_loops (VEC(tree,heap) **exit_conditions)
{
  unsigned int i;
  unsigned nb_chrec_dont_know_loops = 0;
  unsigned nb_static_loops = 0;
  tree cond;
  
  for (i = 0; VEC_iterate (tree, *exit_conditions, i, cond); i++)
    {
      tree res = number_of_latch_executions (loop_containing_stmt (cond));
      if (chrec_contains_undetermined (res))
	nb_chrec_dont_know_loops++;
      else
	nb_static_loops++;
    }
  
  if (dump_file)
    {
      fprintf (dump_file, "\n(\n");
      fprintf (dump_file, "-----------------------------------------\n");
      fprintf (dump_file, "%d\tnb_chrec_dont_know_loops\n", nb_chrec_dont_know_loops);
      fprintf (dump_file, "%d\tnb_static_loops\n", nb_static_loops);
      fprintf (dump_file, "%d\tnb_total_loops\n", number_of_loops ());
      fprintf (dump_file, "-----------------------------------------\n");
      fprintf (dump_file, ")\n\n");
      
      print_loop_ir (dump_file);
    }
}



/* Counters for the stats.  */

struct chrec_stats 
{
  unsigned nb_chrecs;
  unsigned nb_affine;
  unsigned nb_affine_multivar;
  unsigned nb_higher_poly;
  unsigned nb_chrec_dont_know;
  unsigned nb_undetermined;
};

/* Reset the counters.  */

static inline void
reset_chrecs_counters (struct chrec_stats *stats)
{
  stats->nb_chrecs = 0;
  stats->nb_affine = 0;
  stats->nb_affine_multivar = 0;
  stats->nb_higher_poly = 0;
  stats->nb_chrec_dont_know = 0;
  stats->nb_undetermined = 0;
}

/* Dump the contents of a CHREC_STATS structure.  */

static void
dump_chrecs_stats (FILE *file, struct chrec_stats *stats)
{
  fprintf (file, "\n(\n");
  fprintf (file, "-----------------------------------------\n");
  fprintf (file, "%d\taffine univariate chrecs\n", stats->nb_affine);
  fprintf (file, "%d\taffine multivariate chrecs\n", stats->nb_affine_multivar);
  fprintf (file, "%d\tdegree greater than 2 polynomials\n", 
	   stats->nb_higher_poly);
  fprintf (file, "%d\tchrec_dont_know chrecs\n", stats->nb_chrec_dont_know);
  fprintf (file, "-----------------------------------------\n");
  fprintf (file, "%d\ttotal chrecs\n", stats->nb_chrecs);
  fprintf (file, "%d\twith undetermined coefficients\n", 
	   stats->nb_undetermined);
  fprintf (file, "-----------------------------------------\n");
  fprintf (file, "%d\tchrecs in the scev database\n", 
	   (int) htab_elements (scalar_evolution_info));
  fprintf (file, "%d\tsets in the scev database\n", nb_set_scev);
  fprintf (file, "%d\tgets in the scev database\n", nb_get_scev);
  fprintf (file, "-----------------------------------------\n");
  fprintf (file, ")\n\n");
}

/* Gather statistics about CHREC.  */

static void
gather_chrec_stats (tree chrec, struct chrec_stats *stats)
{
  if (dump_file && (dump_flags & TDF_STATS))
    {
      fprintf (dump_file, "(classify_chrec ");
      print_generic_expr (dump_file, chrec, 0);
      fprintf (dump_file, "\n");
    }
  
  stats->nb_chrecs++;
  
  if (chrec == NULL_TREE)
    {
      stats->nb_undetermined++;
      return;
    }
  
  switch (TREE_CODE (chrec))
    {
    case POLYNOMIAL_CHREC:
      if (evolution_function_is_affine_p (chrec))
	{
	  if (dump_file && (dump_flags & TDF_STATS))
	    fprintf (dump_file, "  affine_univariate\n");
	  stats->nb_affine++;
	}
      else if (evolution_function_is_affine_multivariate_p (chrec, 0))
	{
	  if (dump_file && (dump_flags & TDF_STATS))
	    fprintf (dump_file, "  affine_multivariate\n");
	  stats->nb_affine_multivar++;
	}
      else
	{
	  if (dump_file && (dump_flags & TDF_STATS))
	    fprintf (dump_file, "  higher_degree_polynomial\n");
	  stats->nb_higher_poly++;
	}
      
      break;

    default:
      break;
    }
  
  if (chrec_contains_undetermined (chrec))
    {
      if (dump_file && (dump_flags & TDF_STATS))
	fprintf (dump_file, "  undetermined\n");
      stats->nb_undetermined++;
    }
  
  if (dump_file && (dump_flags & TDF_STATS))
    fprintf (dump_file, ")\n");
}

/* One of the drivers for testing the scalar evolutions analysis.
   This function analyzes the scalar evolution of all the scalars
   defined as loop phi nodes in one of the loops from the
   EXIT_CONDITIONS array.  
   
   TODO Optimization: A loop is in canonical form if it contains only
   a single scalar loop phi node.  All the other scalars that have an
   evolution in the loop are rewritten in function of this single
   index.  This allows the parallelization of the loop.  */

static void 
analyze_scalar_evolution_for_all_loop_phi_nodes (VEC(tree,heap) **exit_conditions)
{
  unsigned int i;
  struct chrec_stats stats;
  tree cond;
  
  reset_chrecs_counters (&stats);
  
  for (i = 0; VEC_iterate (tree, *exit_conditions, i, cond); i++)
    {
      struct loop *loop;
      basic_block bb;
      tree phi, chrec;
      
      loop = loop_containing_stmt (cond);
      bb = loop->header;
      
      for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
	if (is_gimple_reg (PHI_RESULT (phi)))
	  {
	    chrec = instantiate_parameters 
	      (loop, 
	       analyze_scalar_evolution (loop, PHI_RESULT (phi)));
	    
	    if (dump_file && (dump_flags & TDF_STATS))
	      gather_chrec_stats (chrec, &stats);
	  }
    }
  
  if (dump_file && (dump_flags & TDF_STATS))
    dump_chrecs_stats (dump_file, &stats);
}

/* Callback for htab_traverse, gathers information on chrecs in the
   hashtable.  */

static int
gather_stats_on_scev_database_1 (void **slot, void *stats)
{
  struct scev_info_str *entry = (struct scev_info_str *) *slot;

  gather_chrec_stats (entry->chrec, (struct chrec_stats *) stats);

  return 1;
}

/* Classify the chrecs of the whole database.  */

void 
gather_stats_on_scev_database (void)
{
  struct chrec_stats stats;
  
  if (!dump_file)
    return;
  
  reset_chrecs_counters (&stats);
 
  htab_traverse (scalar_evolution_info, gather_stats_on_scev_database_1,
		 &stats);

  dump_chrecs_stats (dump_file, &stats);
}



/* Initializer.  */

static void
initialize_scalar_evolutions_analyzer (void)
{
  /* The elements below are unique.  */
  if (chrec_dont_know == NULL_TREE)
    {
      chrec_not_analyzed_yet = NULL_TREE;
      chrec_dont_know = make_node (SCEV_NOT_KNOWN);
      chrec_known = make_node (SCEV_KNOWN);
      TREE_TYPE (chrec_dont_know) = void_type_node;
      TREE_TYPE (chrec_known) = void_type_node;
    }
}

/* Initialize the analysis of scalar evolutions for LOOPS.  */

void
scev_initialize (void)
{
  loop_iterator li;
  struct loop *loop;

  scalar_evolution_info = htab_create_alloc (100,
					     hash_scev_info,
					     eq_scev_info,
					     del_scev_info,
					     ggc_calloc,
					     ggc_free);
  already_instantiated = BITMAP_ALLOC (NULL);
  
  initialize_scalar_evolutions_analyzer ();

  FOR_EACH_LOOP (li, loop, 0)
    {
      loop->nb_iterations = NULL_TREE;
    }
}

/* Cleans up the information cached by the scalar evolutions analysis.  */

void
scev_reset (void)
{
  loop_iterator li;
  struct loop *loop;

  if (!scalar_evolution_info || !current_loops)
    return;

  htab_empty (scalar_evolution_info);
  FOR_EACH_LOOP (li, loop, 0)
    {
      loop->nb_iterations = NULL_TREE;
    }
}

/* Checks whether OP behaves as a simple affine iv of LOOP in STMT and returns
   its base and step in IV if possible.  If ALLOW_NONCONSTANT_STEP is true, we
   want step to be invariant in LOOP.  Otherwise we require it to be an
   integer constant.  IV->no_overflow is set to true if we are sure the iv cannot
   overflow (e.g.  because it is computed in signed arithmetics).  */

bool
simple_iv (struct loop *loop, tree stmt, tree op, affine_iv *iv,
	   bool allow_nonconstant_step)
{
  basic_block bb = bb_for_stmt (stmt);
  tree type, ev;
  bool folded_casts;

  iv->base = NULL_TREE;
  iv->step = NULL_TREE;
  iv->no_overflow = false;

  type = TREE_TYPE (op);
  if (TREE_CODE (type) != INTEGER_TYPE
      && TREE_CODE (type) != POINTER_TYPE)
    return false;

  ev = analyze_scalar_evolution_in_loop (loop, bb->loop_father, op,
					 &folded_casts);
  if (chrec_contains_undetermined (ev))
    return false;

  if (tree_does_not_contain_chrecs (ev)
      && !chrec_contains_symbols_defined_in_loop (ev, loop->num))
    {
      iv->base = ev;
      iv->step = build_int_cst (TREE_TYPE (ev), 0);
      iv->no_overflow = true;
      return true;
    }

  if (TREE_CODE (ev) != POLYNOMIAL_CHREC
      || CHREC_VARIABLE (ev) != (unsigned) loop->num)
    return false;

  iv->step = CHREC_RIGHT (ev);
  if (allow_nonconstant_step)
    {
      if (tree_contains_chrecs (iv->step, NULL)
	  || chrec_contains_symbols_defined_in_loop (iv->step, loop->num))
	return false;
    }
  else if (TREE_CODE (iv->step) != INTEGER_CST)
    return false;

  iv->base = CHREC_LEFT (ev);
  if (tree_contains_chrecs (iv->base, NULL)
      || chrec_contains_symbols_defined_in_loop (iv->base, loop->num))
    return false;

  iv->no_overflow = !folded_casts && TYPE_OVERFLOW_UNDEFINED (type);

  return true;
}

/* Runs the analysis of scalar evolutions.  */

void
scev_analysis (void)
{
  VEC(tree,heap) *exit_conditions;
  
  exit_conditions = VEC_alloc (tree, heap, 37);
  select_loops_exit_conditions (&exit_conditions);

  if (dump_file && (dump_flags & TDF_STATS))
    analyze_scalar_evolution_for_all_loop_phi_nodes (&exit_conditions);
  
  number_of_iterations_for_all_loops (&exit_conditions);
  VEC_free (tree, heap, exit_conditions);
}

/* Finalize the scalar evolution analysis.  */

void
scev_finalize (void)
{
  if (!scalar_evolution_info)
    return;
  htab_delete (scalar_evolution_info);
  BITMAP_FREE (already_instantiated);
  scalar_evolution_info = NULL;
}

/* Replace ssa names for that scev can prove they are constant by the
   appropriate constants.  Also perform final value replacement in loops,
   in case the replacement expressions are cheap.
   
   We only consider SSA names defined by phi nodes; rest is left to the
   ordinary constant propagation pass.  */

unsigned int
scev_const_prop (void)
{
  basic_block bb;
  tree name, phi, next_phi, type, ev;
  struct loop *loop, *ex_loop;
  bitmap ssa_names_to_remove = NULL;
  unsigned i;
  loop_iterator li;

  if (number_of_loops () <= 1)
    return 0;

  FOR_EACH_BB (bb)
    {
      loop = bb->loop_father;

      for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
	{
	  name = PHI_RESULT (phi);

	  if (!is_gimple_reg (name))
	    continue;

	  type = TREE_TYPE (name);

	  if (!POINTER_TYPE_P (type)
	      && !INTEGRAL_TYPE_P (type))
	    continue;

	  ev = resolve_mixers (loop, analyze_scalar_evolution (loop, name));
	  if (!is_gimple_min_invariant (ev)
	      || !may_propagate_copy (name, ev))
	    continue;

	  /* Replace the uses of the name.  */
	  if (name != ev)
	    replace_uses_by (name, ev);

	  if (!ssa_names_to_remove)
	    ssa_names_to_remove = BITMAP_ALLOC (NULL);
	  bitmap_set_bit (ssa_names_to_remove, SSA_NAME_VERSION (name));
	}
    }

  /* Remove the ssa names that were replaced by constants.  We do not
     remove them directly in the previous cycle, since this
     invalidates scev cache.  */
  if (ssa_names_to_remove)
    {
      bitmap_iterator bi;

      EXECUTE_IF_SET_IN_BITMAP (ssa_names_to_remove, 0, i, bi)
	{
	  name = ssa_name (i);
	  phi = SSA_NAME_DEF_STMT (name);

	  gcc_assert (TREE_CODE (phi) == PHI_NODE);
	  remove_phi_node (phi, NULL, true);
	}

      BITMAP_FREE (ssa_names_to_remove);
      scev_reset ();
    }

  /* Now the regular final value replacement.  */
  FOR_EACH_LOOP (li, loop, LI_FROM_INNERMOST)
    {
      edge exit;
      tree def, rslt, ass, niter;
      block_stmt_iterator bsi;

      /* If we do not know exact number of iterations of the loop, we cannot
	 replace the final value.  */
      exit = single_exit (loop);
      if (!exit)
	continue;

      niter = number_of_latch_executions (loop);
      /* We used to check here whether the computation of NITER is expensive,
	 and avoided final value elimination if that is the case.  The problem
	 is that it is hard to evaluate whether the expression is too
	 expensive, as we do not know what optimization opportunities the
	 the elimination of the final value may reveal.  Therefore, we now
	 eliminate the final values of induction variables unconditionally.  */
      if (niter == chrec_dont_know)
	continue;

      /* Ensure that it is possible to insert new statements somewhere.  */
      if (!single_pred_p (exit->dest))
	split_loop_exit_edge (exit);
      bsi = bsi_after_labels (exit->dest);

      ex_loop = superloop_at_depth (loop,
				    loop_depth (exit->dest->loop_father) + 1);

      for (phi = phi_nodes (exit->dest); phi; phi = next_phi)
	{
	  next_phi = PHI_CHAIN (phi);
	  rslt = PHI_RESULT (phi);
	  def = PHI_ARG_DEF_FROM_EDGE (phi, exit);
	  if (!is_gimple_reg (def))
	    continue;

	  if (!POINTER_TYPE_P (TREE_TYPE (def))
	      && !INTEGRAL_TYPE_P (TREE_TYPE (def)))
	    continue;

	  def = analyze_scalar_evolution_in_loop (ex_loop, loop, def, NULL);
	  def = compute_overall_effect_of_inner_loop (ex_loop, def);
	  if (!tree_does_not_contain_chrecs (def)
	      || chrec_contains_symbols_defined_in_loop (def, ex_loop->num)
	      /* Moving the computation from the loop may prolong life range
		 of some ssa names, which may cause problems if they appear
		 on abnormal edges.  */
	      || contains_abnormal_ssa_name_p (def))
	    continue;

	  /* Eliminate the PHI node and replace it by a computation outside
	     the loop.  */
	  def = unshare_expr (def);
	  remove_phi_node (phi, NULL_TREE, false);

	  ass = build_gimple_modify_stmt (rslt, NULL_TREE);
	  SSA_NAME_DEF_STMT (rslt) = ass;
	  {
	    block_stmt_iterator dest = bsi;
	    bsi_insert_before (&dest, ass, BSI_NEW_STMT);
	    def = force_gimple_operand_bsi (&dest, def, false, NULL_TREE,
					    true, BSI_SAME_STMT);
	  }
	  GIMPLE_STMT_OPERAND (ass, 1) = def;
	  update_stmt (ass);
	}
    }
  return 0;
}

#include "gt-tree-scalar-evolution.h"