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
|
/* Detection of Static Control Parts (SCoP) for Graphite.
Copyright (C) 2009-2017 Free Software Foundation, Inc.
Contributed by Sebastian Pop <sebastian.pop@amd.com> and
Tobias Grosser <grosser@fim.uni-passau.de>.
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GCC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. */
#define USES_ISL
#include "config.h"
#ifdef HAVE_isl
#include "system.h"
#include "coretypes.h"
#include "backend.h"
#include "cfghooks.h"
#include "domwalk.h"
#include "params.h"
#include "tree.h"
#include "gimple.h"
#include "ssa.h"
#include "fold-const.h"
#include "gimple-iterator.h"
#include "tree-cfg.h"
#include "tree-ssa-loop-manip.h"
#include "tree-ssa-loop-niter.h"
#include "tree-ssa-loop.h"
#include "tree-into-ssa.h"
#include "tree-ssa.h"
#include "cfgloop.h"
#include "tree-data-ref.h"
#include "tree-scalar-evolution.h"
#include "tree-pass.h"
#include "tree-ssa-propagate.h"
#include "gimple-pretty-print.h"
#include "graphite.h"
class debug_printer
{
private:
FILE *dump_file;
public:
void
set_dump_file (FILE *f)
{
gcc_assert (f);
dump_file = f;
}
friend debug_printer &
operator<< (debug_printer &output, int i)
{
fprintf (output.dump_file, "%d", i);
return output;
}
friend debug_printer &
operator<< (debug_printer &output, const char *s)
{
fprintf (output.dump_file, "%s", s);
return output;
}
} dp;
#define DEBUG_PRINT(args) do \
{ \
if (dump_file && (dump_flags & TDF_DETAILS)) { args; } \
} while (0);
/* Pretty print to FILE all the SCoPs in DOT format and mark them with
different colors. If there are not enough colors, paint the
remaining SCoPs in gray.
Special nodes:
- "*" after the node number denotes the entry of a SCoP,
- "#" after the node number denotes the exit of a SCoP,
- "()" around the node number denotes the entry or the
exit nodes of the SCOP. These are not part of SCoP. */
DEBUG_FUNCTION void
dot_all_sese (FILE *file, vec<sese_l>& scops)
{
/* Disable debugging while printing graph. */
dump_flags_t tmp_dump_flags = dump_flags;
dump_flags = TDF_NONE;
fprintf (file, "digraph all {\n");
basic_block bb;
FOR_ALL_BB_FN (bb, cfun)
{
int part_of_scop = false;
/* Use HTML for every bb label. So we are able to print bbs
which are part of two different SCoPs, with two different
background colors. */
fprintf (file, "%d [label=<\n <TABLE BORDER=\"0\" CELLBORDER=\"1\" ",
bb->index);
fprintf (file, "CELLSPACING=\"0\">\n");
/* Select color for SCoP. */
sese_l *region;
int i;
FOR_EACH_VEC_ELT (scops, i, region)
{
bool sese_in_region = bb_in_sese_p (bb, *region);
if (sese_in_region || (region->exit->dest == bb)
|| (region->entry->dest == bb))
{
const char *color;
switch (i % 17)
{
case 0: /* red */
color = "#e41a1c";
break;
case 1: /* blue */
color = "#377eb8";
break;
case 2: /* green */
color = "#4daf4a";
break;
case 3: /* purple */
color = "#984ea3";
break;
case 4: /* orange */
color = "#ff7f00";
break;
case 5: /* yellow */
color = "#ffff33";
break;
case 6: /* brown */
color = "#a65628";
break;
case 7: /* rose */
color = "#f781bf";
break;
case 8:
color = "#8dd3c7";
break;
case 9:
color = "#ffffb3";
break;
case 10:
color = "#bebada";
break;
case 11:
color = "#fb8072";
break;
case 12:
color = "#80b1d3";
break;
case 13:
color = "#fdb462";
break;
case 14:
color = "#b3de69";
break;
case 15:
color = "#fccde5";
break;
case 16:
color = "#bc80bd";
break;
default: /* gray */
color = "#999999";
}
fprintf (file, " <TR><TD WIDTH=\"50\" BGCOLOR=\"%s\">",
color);
if (!sese_in_region)
fprintf (file, " (");
if (bb == region->entry->dest && bb == region->exit->dest)
fprintf (file, " %d*# ", bb->index);
else if (bb == region->entry->dest)
fprintf (file, " %d* ", bb->index);
else if (bb == region->exit->dest)
fprintf (file, " %d# ", bb->index);
else
fprintf (file, " %d ", bb->index);
fprintf (file, "{lp_%d}", bb->loop_father->num);
if (!sese_in_region)
fprintf (file, ")");
fprintf (file, "</TD></TR>\n");
part_of_scop = true;
}
}
if (!part_of_scop)
{
fprintf (file, " <TR><TD WIDTH=\"50\" BGCOLOR=\"#ffffff\">");
fprintf (file, " %d {lp_%d} </TD></TR>\n", bb->index,
bb->loop_father->num);
}
fprintf (file, " </TABLE>>, shape=box, style=\"setlinewidth(0)\"]\n");
}
FOR_ALL_BB_FN (bb, cfun)
{
edge e;
edge_iterator ei;
FOR_EACH_EDGE (e, ei, bb->succs)
fprintf (file, "%d -> %d;\n", bb->index, e->dest->index);
}
fputs ("}\n\n", file);
/* Enable debugging again. */
dump_flags = tmp_dump_flags;
}
/* Display SCoP on stderr. */
DEBUG_FUNCTION void
dot_sese (sese_l& scop)
{
vec<sese_l> scops;
scops.create (1);
if (scop)
scops.safe_push (scop);
dot_all_sese (stderr, scops);
scops.release ();
}
DEBUG_FUNCTION void
dot_cfg ()
{
vec<sese_l> scops;
scops.create (1);
dot_all_sese (stderr, scops);
scops.release ();
}
/* Return true if BB is empty, contains only DEBUG_INSNs. */
static bool
trivially_empty_bb_p (basic_block bb)
{
gimple_stmt_iterator gsi;
for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
if (gimple_code (gsi_stmt (gsi)) != GIMPLE_DEBUG)
return false;
return true;
}
/* Returns true when P1 and P2 are close phis with the same
argument. */
static inline bool
same_close_phi_node (gphi *p1, gphi *p2)
{
return (types_compatible_p (TREE_TYPE (gimple_phi_result (p1)),
TREE_TYPE (gimple_phi_result (p2)))
&& operand_equal_p (gimple_phi_arg_def (p1, 0),
gimple_phi_arg_def (p2, 0), 0));
}
static void make_close_phi_nodes_unique (basic_block bb);
/* Remove the close phi node at GSI and replace its rhs with the rhs
of PHI. */
static void
remove_duplicate_close_phi (gphi *phi, gphi_iterator *gsi)
{
gimple *use_stmt;
use_operand_p use_p;
imm_use_iterator imm_iter;
tree res = gimple_phi_result (phi);
tree def = gimple_phi_result (gsi->phi ());
gcc_assert (same_close_phi_node (phi, gsi->phi ()));
FOR_EACH_IMM_USE_STMT (use_stmt, imm_iter, def)
{
FOR_EACH_IMM_USE_ON_STMT (use_p, imm_iter)
SET_USE (use_p, res);
update_stmt (use_stmt);
/* It is possible that we just created a duplicate close-phi
for an already-processed containing loop. Check for this
case and clean it up. */
if (gimple_code (use_stmt) == GIMPLE_PHI
&& gimple_phi_num_args (use_stmt) == 1)
make_close_phi_nodes_unique (gimple_bb (use_stmt));
}
remove_phi_node (gsi, true);
}
/* Removes all the close phi duplicates from BB. */
static void
make_close_phi_nodes_unique (basic_block bb)
{
gphi_iterator psi;
for (psi = gsi_start_phis (bb); !gsi_end_p (psi); gsi_next (&psi))
{
gphi_iterator gsi = psi;
gphi *phi = psi.phi ();
/* At this point, PHI should be a close phi in normal form. */
gcc_assert (gimple_phi_num_args (phi) == 1);
/* Iterate over the next phis and remove duplicates. */
gsi_next (&gsi);
while (!gsi_end_p (gsi))
if (same_close_phi_node (phi, gsi.phi ()))
remove_duplicate_close_phi (phi, &gsi);
else
gsi_next (&gsi);
}
}
/* Return true when NAME is defined in LOOP. */
static bool
defined_in_loop_p (tree name, loop_p loop)
{
gcc_assert (TREE_CODE (name) == SSA_NAME);
return loop == loop_containing_stmt (SSA_NAME_DEF_STMT (name));
}
/* Transforms LOOP to the canonical loop closed SSA form. */
static void
canonicalize_loop_closed_ssa (loop_p loop)
{
edge e = single_exit (loop);
basic_block bb;
if (!e || e->flags & EDGE_ABNORMAL)
return;
bb = e->dest;
if (single_pred_p (bb))
{
e = split_block_after_labels (bb);
DEBUG_PRINT (dp << "Splitting bb_" << bb->index << ".\n");
make_close_phi_nodes_unique (e->src);
}
else
{
gphi_iterator psi;
basic_block close = split_edge (e);
e = single_succ_edge (close);
DEBUG_PRINT (dp << "Splitting edge (" << e->src->index << ","
<< e->dest->index << ")\n");
for (psi = gsi_start_phis (bb); !gsi_end_p (psi); gsi_next (&psi))
{
gphi *phi = psi.phi ();
unsigned i;
for (i = 0; i < gimple_phi_num_args (phi); i++)
if (gimple_phi_arg_edge (phi, i) == e)
{
tree res, arg = gimple_phi_arg_def (phi, i);
use_operand_p use_p;
gphi *close_phi;
/* Only add close phi nodes for SSA_NAMEs defined in LOOP. */
if (TREE_CODE (arg) != SSA_NAME
|| !defined_in_loop_p (arg, loop))
continue;
close_phi = create_phi_node (NULL_TREE, close);
res = create_new_def_for (arg, close_phi,
gimple_phi_result_ptr (close_phi));
add_phi_arg (close_phi, arg,
gimple_phi_arg_edge (close_phi, 0),
UNKNOWN_LOCATION);
use_p = gimple_phi_arg_imm_use_ptr (phi, i);
replace_exp (use_p, res);
update_stmt (phi);
}
}
make_close_phi_nodes_unique (close);
}
/* The code above does not properly handle changes in the post dominance
information (yet). */
recompute_all_dominators ();
}
/* Converts the current loop closed SSA form to a canonical form
expected by the Graphite code generation.
The loop closed SSA form has the following invariant: a variable
defined in a loop that is used outside the loop appears only in the
phi nodes in the destination of the loop exit. These phi nodes are
called close phi nodes.
The canonical loop closed SSA form contains the extra invariants:
- when the loop contains only one exit, the close phi nodes contain
only one argument. That implies that the basic block that contains
the close phi nodes has only one predecessor, that is a basic block
in the loop.
- the basic block containing the close phi nodes does not contain
other statements.
- there exist only one phi node per definition in the loop.
*/
static void
canonicalize_loop_closed_ssa_form (void)
{
checking_verify_loop_closed_ssa (true);
loop_p loop;
FOR_EACH_LOOP (loop, 0)
canonicalize_loop_closed_ssa (loop);
rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
update_ssa (TODO_update_ssa);
checking_verify_loop_closed_ssa (true);
}
/* Can all ivs be represented by a signed integer?
As isl might generate negative values in its expressions, signed loop ivs
are required in the backend. */
static bool
loop_ivs_can_be_represented (loop_p loop)
{
unsigned type_long_long = TYPE_PRECISION (long_long_integer_type_node);
for (gphi_iterator psi = gsi_start_phis (loop->header); !gsi_end_p (psi);
gsi_next (&psi))
{
gphi *phi = psi.phi ();
tree res = PHI_RESULT (phi);
tree type = TREE_TYPE (res);
if (TYPE_UNSIGNED (type) && TYPE_PRECISION (type) >= type_long_long)
return false;
}
return true;
}
/* Returns a COND_EXPR statement when BB has a single predecessor, the
edge between BB and its predecessor is not a loop exit edge, and
the last statement of the single predecessor is a COND_EXPR. */
static gcond *
single_pred_cond_non_loop_exit (basic_block bb)
{
if (single_pred_p (bb))
{
edge e = single_pred_edge (bb);
basic_block pred = e->src;
gimple *stmt;
if (loop_depth (pred->loop_father) > loop_depth (bb->loop_father))
return NULL;
stmt = last_stmt (pred);
if (stmt && gimple_code (stmt) == GIMPLE_COND)
return as_a<gcond *> (stmt);
}
return NULL;
}
namespace
{
/* Build the maximal scop containing LOOPs and add it to SCOPS. */
class scop_detection
{
public:
scop_detection () : scops (vNULL) {}
~scop_detection ()
{
scops.release ();
}
/* A marker for invalid sese_l. */
static sese_l invalid_sese;
/* Return the SCOPS in this SCOP_DETECTION. */
vec<sese_l>
get_scops ()
{
return scops;
}
/* Return an sese_l around the LOOP. */
sese_l get_sese (loop_p loop);
/* Return the closest dominator with a single entry edge. In case of a
back-loop the back-edge is not counted. */
static edge get_nearest_dom_with_single_entry (basic_block dom);
/* Return the closest post-dominator with a single exit edge. In case of a
back-loop the back-edge is not counted. */
static edge get_nearest_pdom_with_single_exit (basic_block dom);
/* Merge scops at same loop depth and returns the new sese.
Returns a new SESE when merge was successful, INVALID_SESE otherwise. */
sese_l merge_sese (sese_l first, sese_l second) const;
/* Build scop outer->inner if possible. */
sese_l build_scop_depth (sese_l s, loop_p loop);
/* If loop and loop->next are valid scops, try to merge them. */
sese_l build_scop_breadth (sese_l s1, loop_p loop);
/* Return true when LOOP is a valid scop, that is a Static Control Part, a
region of code that can be represented in the polyhedral model. SCOP
defines the region we analyse. */
bool loop_is_valid_in_scop (loop_p loop, sese_l scop) const;
/* Return true when BEGIN is the preheader edge of a loop with a single exit
END. */
static bool region_has_one_loop (sese_l s);
/* Add to SCOPS a scop starting at SCOP_BEGIN and ending at SCOP_END. */
void add_scop (sese_l s);
/* Returns true if S1 subsumes/surrounds S2. */
static bool subsumes (sese_l s1, sese_l s2);
/* Remove a SCoP which is subsumed by S1. */
void remove_subscops (sese_l s1);
/* Returns true if S1 intersects with S2. Since we already know that S1 does
not subsume S2 or vice-versa, we only check for entry bbs. */
static bool intersects (sese_l s1, sese_l s2);
/* Remove one of the scops when it intersects with any other. */
void remove_intersecting_scops (sese_l s1);
/* Return true when the body of LOOP has statements that can be represented
as a valid scop. */
bool loop_body_is_valid_scop (loop_p loop, sese_l scop) const;
/* Return true when BB contains a harmful operation for a scop: that
can be a function call with side effects, the induction variables
are not linear with respect to SCOP, etc. The current open
scop should end before this statement. */
bool harmful_stmt_in_bb (sese_l scop, basic_block bb) const;
/* Return true when a statement in SCOP cannot be represented by Graphite.
The assumptions are that L1 dominates L2, and SCOP->entry dominates L1.
Limit the number of bbs between adjacent loops to
PARAM_SCOP_MAX_NUM_BBS_BETWEEN_LOOPS. */
bool harmful_loop_in_region (sese_l scop) const;
/* Return true only when STMT is simple enough for being handled by Graphite.
This depends on SCOP, as the parameters are initialized relatively to
this basic block, the linear functions are initialized based on the
outermost loop containing STMT inside the SCOP. BB is the place where we
try to evaluate the STMT. */
bool stmt_simple_for_scop_p (sese_l scop, gimple *stmt,
basic_block bb) const;
/* Something like "n * m" is not allowed. */
static bool graphite_can_represent_init (tree e);
/* Return true when SCEV can be represented in the polyhedral model.
An expression can be represented, if it can be expressed as an
affine expression. For loops (i, j) and parameters (m, n) all
affine expressions are of the form:
x1 * i + x2 * j + x3 * m + x4 * n + x5 * 1 where x1..x5 element of Z
1 i + 20 j + (-2) m + 25
Something like "i * n" or "n * m" is not allowed. */
static bool graphite_can_represent_scev (tree scev);
/* Return true when EXPR can be represented in the polyhedral model.
This means an expression can be represented, if it is linear with respect
to the loops and the strides are non parametric. LOOP is the place where
the expr will be evaluated. SCOP defines the region we analyse. */
static bool graphite_can_represent_expr (sese_l scop, loop_p loop,
tree expr);
/* Return true if the data references of STMT can be represented by Graphite.
We try to analyze the data references in a loop contained in the SCOP. */
static bool stmt_has_simple_data_refs_p (sese_l scop, gimple *stmt);
/* Remove the close phi node at GSI and replace its rhs with the rhs
of PHI. */
static void remove_duplicate_close_phi (gphi *phi, gphi_iterator *gsi);
/* Returns true when Graphite can represent LOOP in SCOP.
FIXME: For the moment, graphite cannot be used on loops that iterate using
induction variables that wrap. */
static bool can_represent_loop_1 (loop_p loop, sese_l scop);
/* Return true when all the loops within LOOP can be represented by
Graphite. */
static bool can_represent_loop (loop_p loop, sese_l scop);
/* Returns the number of pbbs that are in loops contained in SCOP. */
static int nb_pbbs_in_loops (scop_p scop);
static bool graphite_can_represent_stmt (sese_l, gimple *, basic_block);
private:
vec<sese_l> scops;
};
sese_l scop_detection::invalid_sese (NULL, NULL);
/* Return an sese_l around the LOOP. */
sese_l
scop_detection::get_sese (loop_p loop)
{
if (!loop)
return invalid_sese;
if (!loops_state_satisfies_p (LOOPS_HAVE_PREHEADERS))
return invalid_sese;
edge scop_end = single_exit (loop);
if (!scop_end)
return invalid_sese;
edge scop_begin = loop_preheader_edge (loop);
sese_l s (scop_begin, scop_end);
return s;
}
/* Return the closest dominator with a single entry edge. */
edge
scop_detection::get_nearest_dom_with_single_entry (basic_block dom)
{
if (!dom->preds)
return NULL;
/* If any of the dominators has two predecessors but one of them is a back
edge, then that basic block also qualifies as a dominator with single
entry. */
if (dom->preds->length () == 2)
{
/* If e1->src dominates e2->src then e1->src will also dominate dom. */
edge e1 = (*dom->preds)[0];
edge e2 = (*dom->preds)[1];
loop_p l = dom->loop_father;
loop_p l1 = e1->src->loop_father;
loop_p l2 = e2->src->loop_father;
if (l != l1 && l == l2
&& dominated_by_p (CDI_DOMINATORS, e2->src, e1->src))
return e1;
if (l != l2 && l == l1
&& dominated_by_p (CDI_DOMINATORS, e1->src, e2->src))
return e2;
}
while (dom->preds->length () != 1)
{
if (dom->preds->length () < 1)
return NULL;
dom = get_immediate_dominator (CDI_DOMINATORS, dom);
if (!dom->preds)
return NULL;
}
return (*dom->preds)[0];
}
/* Return the closest post-dominator with a single exit edge. In case of a
back-loop the back-edge is not counted. */
edge
scop_detection::get_nearest_pdom_with_single_exit (basic_block pdom)
{
if (!pdom->succs)
return NULL;
/* If any of the post-dominators has two successors but one of them is a back
edge, then that basic block also qualifies as a post-dominator with single
exit. */
if (pdom->succs->length () == 2)
{
/* If e1->dest post-dominates e2->dest then e1->dest will also
post-dominate pdom. */
edge e1 = (*pdom->succs)[0];
edge e2 = (*pdom->succs)[1];
loop_p l = pdom->loop_father;
loop_p l1 = e1->dest->loop_father;
loop_p l2 = e2->dest->loop_father;
if (l != l1 && l == l2
&& dominated_by_p (CDI_POST_DOMINATORS, e2->dest, e1->dest))
return e1;
if (l != l2 && l == l1
&& dominated_by_p (CDI_POST_DOMINATORS, e1->dest, e2->dest))
return e2;
}
while (pdom->succs->length () != 1)
{
if (pdom->succs->length () < 1)
return NULL;
pdom = get_immediate_dominator (CDI_POST_DOMINATORS, pdom);
if (!pdom->succs)
return NULL;
}
return (*pdom->succs)[0];
}
/* Merge scops at same loop depth and returns the new sese.
Returns a new SESE when merge was successful, INVALID_SESE otherwise. */
sese_l
scop_detection::merge_sese (sese_l first, sese_l second) const
{
/* In the trivial case first/second may be NULL. */
if (!first)
return second;
if (!second)
return first;
DEBUG_PRINT (dp << "[scop-detection] try merging sese s1: ";
print_sese (dump_file, first);
dp << "[scop-detection] try merging sese s2: ";
print_sese (dump_file, second));
/* Assumption: Both the sese's should be at the same loop depth or one scop
should subsume the other like in case of nested loops. */
/* Find the common dominators for entry,
and common post-dominators for the exit. */
basic_block dom = nearest_common_dominator (CDI_DOMINATORS,
get_entry_bb (first),
get_entry_bb (second));
edge entry = get_nearest_dom_with_single_entry (dom);
if (!entry || (entry->flags & EDGE_IRREDUCIBLE_LOOP))
return invalid_sese;
basic_block pdom = nearest_common_dominator (CDI_POST_DOMINATORS,
get_exit_bb (first),
get_exit_bb (second));
pdom = nearest_common_dominator (CDI_POST_DOMINATORS, dom, pdom);
edge exit = get_nearest_pdom_with_single_exit (pdom);
if (!exit || (exit->flags & EDGE_IRREDUCIBLE_LOOP))
return invalid_sese;
sese_l combined (entry, exit);
DEBUG_PRINT (dp << "[scop-detection] checking combined sese: ";
print_sese (dump_file, combined));
/* FIXME: We could iterate to find the dom which dominates pdom, and pdom
which post-dominates dom, until it stabilizes. Also, ENTRY->SRC and
EXIT->DEST should be in the same loop nest. */
if (!dominated_by_p (CDI_DOMINATORS, pdom, dom)
|| loop_depth (entry->src->loop_father)
!= loop_depth (exit->dest->loop_father))
return invalid_sese;
/* For now we just bail out when there is a loop exit in the region
that is not also the exit of the region. We could enlarge the
region to cover the loop that region exits to. See PR79977. */
if (loop_outer (entry->src->loop_father))
{
vec<edge> exits = get_loop_exit_edges (entry->src->loop_father);
for (unsigned i = 0; i < exits.length (); ++i)
{
if (exits[i] != exit
&& bb_in_region (exits[i]->src, entry->dest, exit->src))
{
DEBUG_PRINT (dp << "[scop-detection-fail] cannot merge seses.\n");
exits.release ();
return invalid_sese;
}
}
exits.release ();
}
/* For now we just want to bail out when exit does not post-dominate entry.
TODO: We might just add a basic_block at the exit to make exit
post-dominate entry (the entire region). */
if (!dominated_by_p (CDI_POST_DOMINATORS, get_entry_bb (combined),
get_exit_bb (combined))
|| !dominated_by_p (CDI_DOMINATORS, get_exit_bb (combined),
get_entry_bb (combined)))
{
DEBUG_PRINT (dp << "[scop-detection-fail] cannot merge seses.\n");
return invalid_sese;
}
/* FIXME: We should remove this piece of code once
canonicalize_loop_closed_ssa has been removed, because that function
adds a BB with single exit. */
if (!trivially_empty_bb_p (get_exit_bb (combined)))
{
/* Find the first empty succ (with single exit) of combined.exit. */
basic_block imm_succ = combined.exit->dest;
if (single_succ_p (imm_succ)
&& single_pred_p (imm_succ)
&& trivially_empty_bb_p (imm_succ))
combined.exit = single_succ_edge (imm_succ);
else
{
DEBUG_PRINT (dp << "[scop-detection-fail] Discarding SCoP because "
<< "no single exit (empty succ) for sese exit";
print_sese (dump_file, combined));
return invalid_sese;
}
}
/* Analyze all the BBs in new sese. */
if (harmful_loop_in_region (combined))
return invalid_sese;
DEBUG_PRINT (dp << "[merged-sese] s1: "; print_sese (dump_file, combined));
return combined;
}
/* Build scop outer->inner if possible. */
sese_l
scop_detection::build_scop_depth (sese_l s, loop_p loop)
{
if (!loop)
return s;
DEBUG_PRINT (dp << "[Depth loop_" << loop->num << "]\n");
s = build_scop_depth (s, loop->inner);
sese_l s2 = merge_sese (s, get_sese (loop));
if (!s2)
{
/* s might be a valid scop, so return it and start analyzing from the
adjacent loop. */
build_scop_depth (invalid_sese, loop->next);
return s;
}
if (!loop_is_valid_in_scop (loop, s2))
return build_scop_depth (invalid_sese, loop->next);
return build_scop_breadth (s2, loop);
}
/* If loop and loop->next are valid scops, try to merge them. */
sese_l
scop_detection::build_scop_breadth (sese_l s1, loop_p loop)
{
if (!loop)
return s1;
DEBUG_PRINT (dp << "[Breadth loop_" << loop->num << "]\n");
gcc_assert (s1);
loop_p l = loop;
sese_l s2 = build_scop_depth (invalid_sese, l->next);
if (!s2)
{
if (s1)
add_scop (s1);
return s1;
}
sese_l combined = merge_sese (s1, s2);
/* Combining adjacent loops may add unrelated loops into the
region so we have to check all sub-loops of the outer loop
that are in the combined region. */
if (combined)
for (l = loop_outer (loop)->inner; l; l = l->next)
if (bb_in_sese_p (l->header, combined)
&& ! loop_is_valid_in_scop (l, combined))
{
combined = invalid_sese;
break;
}
if (combined)
s1 = combined;
else
add_scop (s2);
if (s1)
add_scop (s1);
return s1;
}
/* Returns true when Graphite can represent LOOP in SCOP.
FIXME: For the moment, graphite cannot be used on loops that iterate using
induction variables that wrap. */
bool
scop_detection::can_represent_loop_1 (loop_p loop, sese_l scop)
{
tree niter;
struct tree_niter_desc niter_desc;
return single_exit (loop)
&& !(loop_preheader_edge (loop)->flags & EDGE_IRREDUCIBLE_LOOP)
&& number_of_iterations_exit (loop, single_exit (loop), &niter_desc, false)
&& niter_desc.control.no_overflow
&& (niter = number_of_latch_executions (loop))
&& !chrec_contains_undetermined (niter)
&& !chrec_contains_undetermined (scalar_evolution_in_region (scop,
loop, niter))
&& graphite_can_represent_expr (scop, loop, niter);
}
/* Return true when all the loops within LOOP can be represented by
Graphite. */
bool
scop_detection::can_represent_loop (loop_p loop, sese_l scop)
{
if (!can_represent_loop_1 (loop, scop))
return false;
if (loop->inner && !can_represent_loop (loop->inner, scop))
return false;
if (loop->next && !can_represent_loop (loop->next, scop))
return false;
return true;
}
/* Return true when LOOP is a valid scop, that is a Static Control Part, a
region of code that can be represented in the polyhedral model. SCOP
defines the region we analyse. */
bool
scop_detection::loop_is_valid_in_scop (loop_p loop, sese_l scop) const
{
if (!scop)
return false;
if (!optimize_loop_nest_for_speed_p (loop))
{
DEBUG_PRINT (dp << "[scop-detection-fail] loop_"
<< loop->num << " is not on a hot path.\n");
return false;
}
if (!can_represent_loop (loop, scop))
{
DEBUG_PRINT (dp << "[scop-detection-fail] cannot represent loop_"
<< loop->num << "\n");
return false;
}
if (loop_body_is_valid_scop (loop, scop))
{
DEBUG_PRINT (dp << "[valid-scop] loop_" << loop->num
<< " is a valid scop.\n");
return true;
}
return false;
}
/* Return true when BEGIN is the preheader edge of a loop with a single exit
END. */
bool
scop_detection::region_has_one_loop (sese_l s)
{
edge begin = s.entry;
edge end = s.exit;
/* Check for a single perfectly nested loop. */
if (begin->dest->loop_father->inner)
return false;
/* Otherwise, check whether we have adjacent loops. */
return begin->dest->loop_father == end->src->loop_father;
}
/* Add to SCOPS a scop starting at SCOP_BEGIN and ending at SCOP_END. */
void
scop_detection::add_scop (sese_l s)
{
gcc_assert (s);
/* Do not add scops with only one loop. */
if (region_has_one_loop (s))
{
DEBUG_PRINT (dp << "[scop-detection-fail] Discarding one loop SCoP: ";
print_sese (dump_file, s));
return;
}
if (get_exit_bb (s) == EXIT_BLOCK_PTR_FOR_FN (cfun))
{
DEBUG_PRINT (dp << "[scop-detection-fail] "
<< "Discarding SCoP exiting to return: ";
print_sese (dump_file, s));
return;
}
/* Remove all the scops which are subsumed by s. */
remove_subscops (s);
/* Remove intersecting scops. FIXME: It will be a good idea to keep
the non-intersecting part of the scop already in the list. */
remove_intersecting_scops (s);
scops.safe_push (s);
DEBUG_PRINT (dp << "[scop-detection] Adding SCoP: "; print_sese (dump_file, s));
}
/* Return true when a statement in SCOP cannot be represented by Graphite.
The assumptions are that L1 dominates L2, and SCOP->entry dominates L1.
Limit the number of bbs between adjacent loops to
PARAM_SCOP_MAX_NUM_BBS_BETWEEN_LOOPS. */
bool
scop_detection::harmful_loop_in_region (sese_l scop) const
{
basic_block exit_bb = get_exit_bb (scop);
basic_block entry_bb = get_entry_bb (scop);
DEBUG_PRINT (dp << "[checking-harmful-bbs] ";
print_sese (dump_file, scop));
gcc_assert (dominated_by_p (CDI_DOMINATORS, exit_bb, entry_bb));
auto_vec<basic_block> worklist;
auto_bitmap loops;
worklist.safe_push (entry_bb);
while (! worklist.is_empty ())
{
basic_block bb = worklist.pop ();
DEBUG_PRINT (dp << "Visiting bb_" << bb->index << "\n");
/* The basic block should not be part of an irreducible loop. */
if (bb->flags & BB_IRREDUCIBLE_LOOP)
return true;
/* Check for unstructured control flow: CFG not generated by structured
if-then-else. */
if (bb->succs->length () > 1)
{
edge e;
edge_iterator ei;
FOR_EACH_EDGE (e, ei, bb->succs)
if (!dominated_by_p (CDI_POST_DOMINATORS, bb, e->dest)
&& !dominated_by_p (CDI_DOMINATORS, e->dest, bb))
return true;
}
/* Collect all loops in the current region. */
loop_p loop = bb->loop_father;
if (loop_in_sese_p (loop, scop))
bitmap_set_bit (loops, loop->num);
else
{
/* We only check for harmful statements in basic blocks not part of
any loop fully contained in the scop: other bbs are checked below
in loop_is_valid_in_scop. */
if (harmful_stmt_in_bb (scop, bb))
return true;
}
if (bb != exit_bb)
for (basic_block dom = first_dom_son (CDI_DOMINATORS, bb);
dom;
dom = next_dom_son (CDI_DOMINATORS, dom))
worklist.safe_push (dom);
}
/* Go through all loops and check that they are still valid in the combined
scop. */
unsigned j;
bitmap_iterator bi;
EXECUTE_IF_SET_IN_BITMAP (loops, 0, j, bi)
{
loop_p loop = (*current_loops->larray)[j];
gcc_assert (loop->num == (int) j);
if (!loop_is_valid_in_scop (loop, scop))
return true;
}
return false;
}
/* Returns true if S1 subsumes/surrounds S2. */
bool
scop_detection::subsumes (sese_l s1, sese_l s2)
{
if (dominated_by_p (CDI_DOMINATORS, get_entry_bb (s2),
get_entry_bb (s1))
&& dominated_by_p (CDI_POST_DOMINATORS, s2.exit->dest,
s1.exit->dest))
return true;
return false;
}
/* Remove a SCoP which is subsumed by S1. */
void
scop_detection::remove_subscops (sese_l s1)
{
int j;
sese_l *s2;
FOR_EACH_VEC_ELT_REVERSE (scops, j, s2)
{
if (subsumes (s1, *s2))
{
DEBUG_PRINT (dp << "Removing sub-SCoP";
print_sese (dump_file, *s2));
scops.unordered_remove (j);
}
}
}
/* Returns true if S1 intersects with S2. Since we already know that S1 does
not subsume S2 or vice-versa, we only check for entry bbs. */
bool
scop_detection::intersects (sese_l s1, sese_l s2)
{
if (dominated_by_p (CDI_DOMINATORS, get_entry_bb (s2),
get_entry_bb (s1))
&& !dominated_by_p (CDI_DOMINATORS, get_entry_bb (s2),
get_exit_bb (s1)))
return true;
if ((s1.exit == s2.entry) || (s2.exit == s1.entry))
return true;
return false;
}
/* Remove one of the scops when it intersects with any other. */
void
scop_detection::remove_intersecting_scops (sese_l s1)
{
int j;
sese_l *s2;
FOR_EACH_VEC_ELT_REVERSE (scops, j, s2)
{
if (intersects (s1, *s2))
{
DEBUG_PRINT (dp << "Removing intersecting SCoP";
print_sese (dump_file, *s2);
dp << "Intersects with:";
print_sese (dump_file, s1));
scops.unordered_remove (j);
}
}
}
/* Something like "n * m" is not allowed. */
bool
scop_detection::graphite_can_represent_init (tree e)
{
switch (TREE_CODE (e))
{
case POLYNOMIAL_CHREC:
return graphite_can_represent_init (CHREC_LEFT (e))
&& graphite_can_represent_init (CHREC_RIGHT (e));
case MULT_EXPR:
if (chrec_contains_symbols (TREE_OPERAND (e, 0)))
return graphite_can_represent_init (TREE_OPERAND (e, 0))
&& tree_fits_shwi_p (TREE_OPERAND (e, 1));
else
return graphite_can_represent_init (TREE_OPERAND (e, 1))
&& tree_fits_shwi_p (TREE_OPERAND (e, 0));
case PLUS_EXPR:
case POINTER_PLUS_EXPR:
case MINUS_EXPR:
return graphite_can_represent_init (TREE_OPERAND (e, 0))
&& graphite_can_represent_init (TREE_OPERAND (e, 1));
case NEGATE_EXPR:
case BIT_NOT_EXPR:
CASE_CONVERT:
case NON_LVALUE_EXPR:
return graphite_can_represent_init (TREE_OPERAND (e, 0));
default:
break;
}
return true;
}
/* Return true when SCEV can be represented in the polyhedral model.
An expression can be represented, if it can be expressed as an
affine expression. For loops (i, j) and parameters (m, n) all
affine expressions are of the form:
x1 * i + x2 * j + x3 * m + x4 * n + x5 * 1 where x1..x5 element of Z
1 i + 20 j + (-2) m + 25
Something like "i * n" or "n * m" is not allowed. */
bool
scop_detection::graphite_can_represent_scev (tree scev)
{
if (chrec_contains_undetermined (scev))
return false;
/* We disable the handling of pointer types, because it’s currently not
supported by Graphite with the isl AST generator. SSA_NAME nodes are
the only nodes, which are disabled in case they are pointers to object
types, but this can be changed. */
if (POINTER_TYPE_P (TREE_TYPE (scev)) && TREE_CODE (scev) == SSA_NAME)
return false;
switch (TREE_CODE (scev))
{
case NEGATE_EXPR:
case BIT_NOT_EXPR:
CASE_CONVERT:
case NON_LVALUE_EXPR:
return graphite_can_represent_scev (TREE_OPERAND (scev, 0));
case PLUS_EXPR:
case POINTER_PLUS_EXPR:
case MINUS_EXPR:
return graphite_can_represent_scev (TREE_OPERAND (scev, 0))
&& graphite_can_represent_scev (TREE_OPERAND (scev, 1));
case MULT_EXPR:
return !CONVERT_EXPR_CODE_P (TREE_CODE (TREE_OPERAND (scev, 0)))
&& !CONVERT_EXPR_CODE_P (TREE_CODE (TREE_OPERAND (scev, 1)))
&& !(chrec_contains_symbols (TREE_OPERAND (scev, 0))
&& chrec_contains_symbols (TREE_OPERAND (scev, 1)))
&& graphite_can_represent_init (scev)
&& graphite_can_represent_scev (TREE_OPERAND (scev, 0))
&& graphite_can_represent_scev (TREE_OPERAND (scev, 1));
case POLYNOMIAL_CHREC:
/* Check for constant strides. With a non constant stride of
'n' we would have a value of 'iv * n'. Also check that the
initial value can represented: for example 'n * m' cannot be
represented. */
if (!evolution_function_right_is_integer_cst (scev)
|| !graphite_can_represent_init (scev))
return false;
return graphite_can_represent_scev (CHREC_LEFT (scev));
default:
break;
}
/* Only affine functions can be represented. */
if (tree_contains_chrecs (scev, NULL) || !scev_is_linear_expression (scev))
return false;
return true;
}
/* Return true when EXPR can be represented in the polyhedral model.
This means an expression can be represented, if it is linear with respect to
the loops and the strides are non parametric. LOOP is the place where the
expr will be evaluated. SCOP defines the region we analyse. */
bool
scop_detection::graphite_can_represent_expr (sese_l scop, loop_p loop,
tree expr)
{
tree scev = scalar_evolution_in_region (scop, loop, expr);
return graphite_can_represent_scev (scev);
}
/* Return true if the data references of STMT can be represented by Graphite.
We try to analyze the data references in a loop contained in the SCOP. */
bool
scop_detection::stmt_has_simple_data_refs_p (sese_l scop, gimple *stmt)
{
loop_p nest = outermost_loop_in_sese (scop, gimple_bb (stmt));
loop_p loop = loop_containing_stmt (stmt);
vec<data_reference_p> drs = vNULL;
graphite_find_data_references_in_stmt (nest, loop, stmt, &drs);
int j;
data_reference_p dr;
FOR_EACH_VEC_ELT (drs, j, dr)
{
int nb_subscripts = DR_NUM_DIMENSIONS (dr);
if (nb_subscripts < 1)
{
free_data_refs (drs);
return false;
}
tree ref = DR_REF (dr);
for (int i = nb_subscripts - 1; i >= 0; i--)
{
if (!graphite_can_represent_scev (DR_ACCESS_FN (dr, i))
|| (TREE_CODE (ref) != ARRAY_REF && TREE_CODE (ref) != MEM_REF
&& TREE_CODE (ref) != COMPONENT_REF))
{
free_data_refs (drs);
return false;
}
ref = TREE_OPERAND (ref, 0);
}
}
free_data_refs (drs);
return true;
}
/* GIMPLE_ASM and GIMPLE_CALL may embed arbitrary side effects.
Calls have side-effects, except those to const or pure
functions. */
static bool
stmt_has_side_effects (gimple *stmt)
{
if (gimple_has_volatile_ops (stmt)
|| (gimple_code (stmt) == GIMPLE_CALL
&& !(gimple_call_flags (stmt) & (ECF_CONST | ECF_PURE)))
|| (gimple_code (stmt) == GIMPLE_ASM))
{
DEBUG_PRINT (dp << "[scop-detection-fail] "
<< "Statement has side-effects:\n";
print_gimple_stmt (dump_file, stmt, 0, TDF_VOPS | TDF_MEMSYMS));
return true;
}
return false;
}
/* Returns true if STMT can be represented in polyhedral model. LABEL,
simple COND stmts, pure calls, and assignments can be repesented. */
bool
scop_detection::graphite_can_represent_stmt (sese_l scop, gimple *stmt,
basic_block bb)
{
loop_p loop = bb->loop_father;
switch (gimple_code (stmt))
{
case GIMPLE_LABEL:
return true;
case GIMPLE_COND:
{
/* We can handle all binary comparisons. Inequalities are
also supported as they can be represented with union of
polyhedra. */
enum tree_code code = gimple_cond_code (stmt);
if (!(code == LT_EXPR
|| code == GT_EXPR
|| code == LE_EXPR
|| code == GE_EXPR
|| code == EQ_EXPR
|| code == NE_EXPR))
{
DEBUG_PRINT (dp << "[scop-detection-fail] "
<< "Graphite cannot handle cond stmt:\n";
print_gimple_stmt (dump_file, stmt, 0,
TDF_VOPS | TDF_MEMSYMS));
return false;
}
for (unsigned i = 0; i < 2; ++i)
{
tree op = gimple_op (stmt, i);
if (!graphite_can_represent_expr (scop, loop, op)
/* We can only constrain on integer type. */
|| (TREE_CODE (TREE_TYPE (op)) != INTEGER_TYPE))
{
DEBUG_PRINT (dp << "[scop-detection-fail] "
<< "Graphite cannot represent stmt:\n";
print_gimple_stmt (dump_file, stmt, 0,
TDF_VOPS | TDF_MEMSYMS));
return false;
}
}
return true;
}
case GIMPLE_ASSIGN:
case GIMPLE_CALL:
return true;
default:
/* These nodes cut a new scope. */
DEBUG_PRINT (
dp << "[scop-detection-fail] "
<< "Gimple stmt not handled in Graphite:\n";
print_gimple_stmt (dump_file, stmt, 0, TDF_VOPS | TDF_MEMSYMS));
return false;
}
}
/* Return true only when STMT is simple enough for being handled by Graphite.
This depends on SCOP, as the parameters are initialized relatively to
this basic block, the linear functions are initialized based on the outermost
loop containing STMT inside the SCOP. BB is the place where we try to
evaluate the STMT. */
bool
scop_detection::stmt_simple_for_scop_p (sese_l scop, gimple *stmt,
basic_block bb) const
{
gcc_assert (scop);
if (is_gimple_debug (stmt))
return true;
if (stmt_has_side_effects (stmt))
return false;
if (!stmt_has_simple_data_refs_p (scop, stmt))
{
DEBUG_PRINT (dp << "[scop-detection-fail] "
<< "Graphite cannot handle data-refs in stmt:\n";
print_gimple_stmt (dump_file, stmt, 0, TDF_VOPS|TDF_MEMSYMS););
return false;
}
return graphite_can_represent_stmt (scop, stmt, bb);
}
/* Return true when BB contains a harmful operation for a scop: that
can be a function call with side effects, the induction variables
are not linear with respect to SCOP, etc. The current open
scop should end before this statement. */
bool
scop_detection::harmful_stmt_in_bb (sese_l scop, basic_block bb) const
{
gimple_stmt_iterator gsi;
for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
if (!stmt_simple_for_scop_p (scop, gsi_stmt (gsi), bb))
return true;
return false;
}
/* Return true when the body of LOOP has statements that can be represented as a
valid scop. */
bool
scop_detection::loop_body_is_valid_scop (loop_p loop, sese_l scop) const
{
if (!loop_ivs_can_be_represented (loop))
{
DEBUG_PRINT (dp << "[scop-detection-fail] loop_" << loop->num
<< "IV cannot be represented.\n");
return false;
}
if (!loop_nest_has_data_refs (loop))
{
DEBUG_PRINT (dp << "[scop-detection-fail] loop_" << loop->num
<< "does not have any data reference.\n");
return false;
}
basic_block *bbs = get_loop_body (loop);
for (unsigned i = 0; i < loop->num_nodes; i++)
{
basic_block bb = bbs[i];
if (harmful_stmt_in_bb (scop, bb))
{
free (bbs);
return false;
}
}
free (bbs);
if (loop->inner)
{
loop = loop->inner;
while (loop)
{
if (!loop_body_is_valid_scop (loop, scop))
return false;
loop = loop->next;
}
}
return true;
}
/* Returns the number of pbbs that are in loops contained in SCOP. */
int
scop_detection::nb_pbbs_in_loops (scop_p scop)
{
int i;
poly_bb_p pbb;
int res = 0;
FOR_EACH_VEC_ELT (scop->pbbs, i, pbb)
if (loop_in_sese_p (gbb_loop (PBB_BLACK_BOX (pbb)), scop->scop_info->region))
res++;
return res;
}
/* When parameter NAME is in REGION, returns its index in SESE_PARAMS.
Otherwise returns -1. */
static inline int
parameter_index_in_region_1 (tree name, sese_info_p region)
{
int i;
tree p;
gcc_assert (TREE_CODE (name) == SSA_NAME);
FOR_EACH_VEC_ELT (region->params, i, p)
if (p == name)
return i;
return -1;
}
/* When the parameter NAME is in REGION, returns its index in
SESE_PARAMS. Otherwise this function inserts NAME in SESE_PARAMS
and returns the index of NAME. */
static int
parameter_index_in_region (tree name, sese_info_p region)
{
int i;
gcc_assert (TREE_CODE (name) == SSA_NAME);
/* Cannot constrain on anything else than INTEGER_TYPE parameters. */
if (TREE_CODE (TREE_TYPE (name)) != INTEGER_TYPE)
return -1;
if (!invariant_in_sese_p_rec (name, region->region, NULL))
return -1;
i = parameter_index_in_region_1 (name, region);
if (i != -1)
return i;
i = region->params.length ();
region->params.safe_push (name);
return i;
}
/* In the context of sese S, scan the expression E and translate it to
a linear expression C. When parsing a symbolic multiplication, K
represents the constant multiplier of an expression containing
parameters. */
static void
scan_tree_for_params (sese_info_p s, tree e)
{
if (e == chrec_dont_know)
return;
switch (TREE_CODE (e))
{
case POLYNOMIAL_CHREC:
scan_tree_for_params (s, CHREC_LEFT (e));
break;
case MULT_EXPR:
if (chrec_contains_symbols (TREE_OPERAND (e, 0)))
scan_tree_for_params (s, TREE_OPERAND (e, 0));
else
scan_tree_for_params (s, TREE_OPERAND (e, 1));
break;
case PLUS_EXPR:
case POINTER_PLUS_EXPR:
case MINUS_EXPR:
scan_tree_for_params (s, TREE_OPERAND (e, 0));
scan_tree_for_params (s, TREE_OPERAND (e, 1));
break;
case NEGATE_EXPR:
case BIT_NOT_EXPR:
CASE_CONVERT:
case NON_LVALUE_EXPR:
scan_tree_for_params (s, TREE_OPERAND (e, 0));
break;
case SSA_NAME:
parameter_index_in_region (e, s);
break;
case INTEGER_CST:
case ADDR_EXPR:
case REAL_CST:
case COMPLEX_CST:
case VECTOR_CST:
break;
default:
gcc_unreachable ();
break;
}
}
/* Find parameters with respect to REGION in BB. We are looking in memory
access functions, conditions and loop bounds. */
static void
find_params_in_bb (sese_info_p region, gimple_poly_bb_p gbb)
{
/* Find parameters in the access functions of data references. */
int i;
data_reference_p dr;
FOR_EACH_VEC_ELT (GBB_DATA_REFS (gbb), i, dr)
for (unsigned j = 0; j < DR_NUM_DIMENSIONS (dr); j++)
scan_tree_for_params (region, DR_ACCESS_FN (dr, j));
/* Find parameters in conditional statements. */
gimple *stmt;
loop_p loop = GBB_BB (gbb)->loop_father;
FOR_EACH_VEC_ELT (GBB_CONDITIONS (gbb), i, stmt)
{
tree lhs = scalar_evolution_in_region (region->region, loop,
gimple_cond_lhs (stmt));
tree rhs = scalar_evolution_in_region (region->region, loop,
gimple_cond_rhs (stmt));
scan_tree_for_params (region, lhs);
scan_tree_for_params (region, rhs);
}
}
/* Record the parameters used in the SCOP. A variable is a parameter
in a scop if it does not vary during the execution of that scop. */
static void
find_scop_parameters (scop_p scop)
{
unsigned i;
sese_info_p region = scop->scop_info;
struct loop *loop;
/* Find the parameters used in the loop bounds. */
FOR_EACH_VEC_ELT (region->loop_nest, i, loop)
{
tree nb_iters = number_of_latch_executions (loop);
if (!chrec_contains_symbols (nb_iters))
continue;
nb_iters = scalar_evolution_in_region (region->region, loop, nb_iters);
scan_tree_for_params (region, nb_iters);
}
/* Find the parameters used in data accesses. */
poly_bb_p pbb;
FOR_EACH_VEC_ELT (scop->pbbs, i, pbb)
find_params_in_bb (region, PBB_BLACK_BOX (pbb));
int nbp = sese_nb_params (region);
scop_set_nb_params (scop, nbp);
}
/* Record DEF if it is used in other bbs different than DEF_BB in the SCOP. */
static void
build_cross_bb_scalars_def (scop_p scop, tree def, basic_block def_bb,
vec<tree> *writes)
{
if (!def || !is_gimple_reg (def))
return;
/* Do not gather scalar variables that can be analyzed by SCEV as they can be
generated out of the induction variables. */
if (scev_analyzable_p (def, scop->scop_info->region))
return;
gimple *use_stmt;
imm_use_iterator imm_iter;
FOR_EACH_IMM_USE_STMT (use_stmt, imm_iter, def)
if (def_bb != gimple_bb (use_stmt) && !is_gimple_debug (use_stmt))
{
writes->safe_push (def);
DEBUG_PRINT (dp << "Adding scalar write: ";
print_generic_expr (dump_file, def);
dp << "\nFrom stmt: ";
print_gimple_stmt (dump_file,
SSA_NAME_DEF_STMT (def), 0));
/* This is required by the FOR_EACH_IMM_USE_STMT when we want to break
before all the uses have been visited. */
BREAK_FROM_IMM_USE_STMT (imm_iter);
}
}
/* Record DEF if it is used in other bbs different than DEF_BB in the SCOP. */
static void
build_cross_bb_scalars_use (scop_p scop, tree use, gimple *use_stmt,
vec<scalar_use> *reads)
{
gcc_assert (use);
if (!is_gimple_reg (use))
return;
/* Do not gather scalar variables that can be analyzed by SCEV as they can be
generated out of the induction variables. */
if (scev_analyzable_p (use, scop->scop_info->region))
return;
gimple *def_stmt = SSA_NAME_DEF_STMT (use);
if (gimple_bb (def_stmt) != gimple_bb (use_stmt))
{
DEBUG_PRINT (dp << "Adding scalar read: ";
print_generic_expr (dump_file, use);
dp << "\nFrom stmt: ";
print_gimple_stmt (dump_file, use_stmt, 0));
reads->safe_push (std::make_pair (use_stmt, use));
}
}
/* Record all scalar variables that are defined and used in different BBs of the
SCOP. */
static void
graphite_find_cross_bb_scalar_vars (scop_p scop, gimple *stmt,
vec<scalar_use> *reads, vec<tree> *writes)
{
tree def;
if (gimple_code (stmt) == GIMPLE_ASSIGN)
def = gimple_assign_lhs (stmt);
else if (gimple_code (stmt) == GIMPLE_CALL)
def = gimple_call_lhs (stmt);
else if (gimple_code (stmt) == GIMPLE_PHI)
def = gimple_phi_result (stmt);
else
return;
build_cross_bb_scalars_def (scop, def, gimple_bb (stmt), writes);
ssa_op_iter iter;
use_operand_p use_p;
FOR_EACH_PHI_OR_STMT_USE (use_p, stmt, iter, SSA_OP_USE)
{
tree use = USE_FROM_PTR (use_p);
build_cross_bb_scalars_use (scop, use, stmt, reads);
}
}
/* Generates a polyhedral black box only if the bb contains interesting
information. */
static gimple_poly_bb_p
try_generate_gimple_bb (scop_p scop, basic_block bb)
{
vec<data_reference_p> drs = vNULL;
vec<tree> writes = vNULL;
vec<scalar_use> reads = vNULL;
sese_l region = scop->scop_info->region;
loop_p nest = outermost_loop_in_sese (region, bb);
loop_p loop = bb->loop_father;
if (!loop_in_sese_p (loop, region))
loop = nest;
for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
gsi_next (&gsi))
{
gimple *stmt = gsi_stmt (gsi);
if (is_gimple_debug (stmt))
continue;
graphite_find_data_references_in_stmt (nest, loop, stmt, &drs);
graphite_find_cross_bb_scalar_vars (scop, stmt, &reads, &writes);
}
for (gphi_iterator psi = gsi_start_phis (bb); !gsi_end_p (psi);
gsi_next (&psi))
if (!virtual_operand_p (gimple_phi_result (psi.phi ())))
graphite_find_cross_bb_scalar_vars (scop, psi.phi (), &reads, &writes);
if (drs.is_empty () && writes.is_empty () && reads.is_empty ())
return NULL;
return new_gimple_poly_bb (bb, drs, reads, writes);
}
/* Compute alias-sets for all data references in DRS. */
static void
build_alias_set (scop_p scop)
{
int num_vertices = scop->drs.length ();
struct graph *g = new_graph (num_vertices);
dr_info *dr1, *dr2;
int i, j;
int *all_vertices;
FOR_EACH_VEC_ELT (scop->drs, i, dr1)
for (j = i+1; scop->drs.iterate (j, &dr2); j++)
if (dr_may_alias_p (dr1->dr, dr2->dr, true))
{
add_edge (g, i, j);
add_edge (g, j, i);
}
all_vertices = XNEWVEC (int, num_vertices);
for (i = 0; i < num_vertices; i++)
all_vertices[i] = i;
graphds_dfs (g, all_vertices, num_vertices, NULL, true, NULL);
free (all_vertices);
for (i = 0; i < g->n_vertices; i++)
scop->drs[i].alias_set = g->vertices[i].component + 1;
free_graph (g);
}
/* Gather BBs and conditions for a SCOP. */
class gather_bbs : public dom_walker
{
public:
gather_bbs (cdi_direction, scop_p);
virtual edge before_dom_children (basic_block);
virtual void after_dom_children (basic_block);
private:
auto_vec<gimple *, 3> conditions, cases;
scop_p scop;
};
}
gather_bbs::gather_bbs (cdi_direction direction, scop_p scop)
: dom_walker (direction), scop (scop)
{
}
/* Record in execution order the loops fully contained in the region. */
static void
record_loop_in_sese (basic_block bb, sese_info_p region)
{
loop_p father = bb->loop_father;
if (loop_in_sese_p (father, region->region))
{
bool found = false;
loop_p loop0;
int j;
FOR_EACH_VEC_ELT (region->loop_nest, j, loop0)
if (father == loop0)
{
found = true;
break;
}
if (!found)
region->loop_nest.safe_push (father);
}
}
/* Call-back for dom_walk executed before visiting the dominated
blocks. */
edge
gather_bbs::before_dom_children (basic_block bb)
{
sese_info_p region = scop->scop_info;
if (!bb_in_sese_p (bb, region->region))
return NULL;
record_loop_in_sese (bb, region);
gcond *stmt = single_pred_cond_non_loop_exit (bb);
if (stmt)
{
edge e = single_pred_edge (bb);
conditions.safe_push (stmt);
if (e->flags & EDGE_TRUE_VALUE)
cases.safe_push (stmt);
else
cases.safe_push (NULL);
}
scop->scop_info->bbs.safe_push (bb);
gimple_poly_bb_p gbb = try_generate_gimple_bb (scop, bb);
if (!gbb)
return NULL;
GBB_CONDITIONS (gbb) = conditions.copy ();
GBB_CONDITION_CASES (gbb) = cases.copy ();
poly_bb_p pbb = new_poly_bb (scop, gbb);
scop->pbbs.safe_push (pbb);
int i;
data_reference_p dr;
FOR_EACH_VEC_ELT (gbb->data_refs, i, dr)
{
DEBUG_PRINT (dp << "Adding memory ";
if (dr->is_read)
dp << "read: ";
else
dp << "write: ";
print_generic_expr (dump_file, dr->ref);
dp << "\nFrom stmt: ";
print_gimple_stmt (dump_file, dr->stmt, 0));
scop->drs.safe_push (dr_info (dr, pbb));
}
return NULL;
}
/* Call-back for dom_walk executed after visiting the dominated
blocks. */
void
gather_bbs::after_dom_children (basic_block bb)
{
if (!bb_in_sese_p (bb, scop->scop_info->region))
return;
if (single_pred_cond_non_loop_exit (bb))
{
conditions.pop ();
cases.pop ();
}
}
/* Compute sth like an execution order, dominator order with first executing
edges that stay inside the current loop, delaying processing exit edges. */
static vec<unsigned> order;
static void
get_order (scop_p scop, basic_block bb, vec<unsigned> *order, unsigned *dfs_num)
{
if (! bb_in_sese_p (bb, scop->scop_info->region))
return;
(*order)[bb->index] = (*dfs_num)++;
for (basic_block son = first_dom_son (CDI_DOMINATORS, bb);
son;
son = next_dom_son (CDI_DOMINATORS, son))
if (flow_bb_inside_loop_p (bb->loop_father, son))
get_order (scop, son, order, dfs_num);
for (basic_block son = first_dom_son (CDI_DOMINATORS, bb);
son;
son = next_dom_son (CDI_DOMINATORS, son))
if (! flow_bb_inside_loop_p (bb->loop_father, son))
get_order (scop, son, order, dfs_num);
}
/* Helper for qsort, sorting after order above. */
static int
cmp_pbbs (const void *pa, const void *pb)
{
poly_bb_p bb1 = *((const poly_bb_p *)pa);
poly_bb_p bb2 = *((const poly_bb_p *)pb);
if (order[bb1->black_box->bb->index] < order[bb2->black_box->bb->index])
return -1;
else if (order[bb1->black_box->bb->index] > order[bb2->black_box->bb->index])
return 1;
else
return 0;
}
/* Find Static Control Parts (SCoP) in the current function and pushes
them to SCOPS. */
void
build_scops (vec<scop_p> *scops)
{
if (dump_file)
dp.set_dump_file (dump_file);
canonicalize_loop_closed_ssa_form ();
scop_detection sb;
sb.build_scop_depth (scop_detection::invalid_sese, current_loops->tree_root);
/* Now create scops from the lightweight SESEs. */
vec<sese_l> scops_l = sb.get_scops ();
int i;
sese_l *s;
FOR_EACH_VEC_ELT (scops_l, i, s)
{
scop_p scop = new_scop (s->entry, s->exit);
/* Record all basic blocks and their conditions in REGION. */
gather_bbs (CDI_DOMINATORS, scop).walk (s->entry->dest);
/* domwalk does not fulfil our code-generations constraints on the
order of pbb which is to produce sth like execution order, delaying
exection of loop exit edges. So compute such order and sort after
that. */
order.create (last_basic_block_for_fn (cfun));
order.quick_grow (last_basic_block_for_fn (cfun));
unsigned dfs_num = 0;
get_order (scop, s->entry->dest, &order, &dfs_num);
scop->pbbs.qsort (cmp_pbbs);
order.release ();
build_alias_set (scop);
/* Do not optimize a scop containing only PBBs that do not belong
to any loops. */
if (sb.nb_pbbs_in_loops (scop) == 0)
{
DEBUG_PRINT (dp << "[scop-detection-fail] no data references.\n");
free_scop (scop);
continue;
}
unsigned max_arrays = PARAM_VALUE (PARAM_GRAPHITE_MAX_ARRAYS_PER_SCOP);
if (scop->drs.length () >= max_arrays)
{
DEBUG_PRINT (dp << "[scop-detection-fail] too many data references: "
<< scop->drs.length ()
<< " is larger than --param graphite-max-arrays-per-scop="
<< max_arrays << ".\n");
free_scop (scop);
continue;
}
find_scop_parameters (scop);
graphite_dim_t max_dim = PARAM_VALUE (PARAM_GRAPHITE_MAX_NB_SCOP_PARAMS);
if (scop_nb_params (scop) > max_dim)
{
DEBUG_PRINT (dp << "[scop-detection-fail] too many parameters: "
<< scop_nb_params (scop)
<< " larger than --param graphite-max-nb-scop-params="
<< max_dim << ".\n");
free_scop (scop);
continue;
}
scops->safe_push (scop);
}
DEBUG_PRINT (dp << "number of SCoPs: " << (scops ? scops->length () : 0););
}
#endif /* HAVE_isl */
|