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
|
/*
* Copyright 2008-2009 Katholieke Universiteit Leuven
* Copyright 2010 INRIA Saclay
* Copyright 2012-2013 Ecole Normale Superieure
* Copyright 2014 INRIA Rocquencourt
*
* Use of this software is governed by the MIT license
*
* Written by Sven Verdoolaege, K.U.Leuven, Departement
* Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
* and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
* ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
* and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
* and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
* B.P. 105 - 78153 Le Chesnay, France
*/
#include "isl_map_private.h"
#include <isl_seq.h>
#include <isl/options.h>
#include "isl_tab.h"
#include <isl_mat_private.h>
#include <isl_local_space_private.h>
#include <isl_vec_private.h>
#include <isl_aff_private.h>
#define STATUS_ERROR -1
#define STATUS_REDUNDANT 1
#define STATUS_VALID 2
#define STATUS_SEPARATE 3
#define STATUS_CUT 4
#define STATUS_ADJ_EQ 5
#define STATUS_ADJ_INEQ 6
static int status_in(isl_int *ineq, struct isl_tab *tab)
{
enum isl_ineq_type type = isl_tab_ineq_type(tab, ineq);
switch (type) {
default:
case isl_ineq_error: return STATUS_ERROR;
case isl_ineq_redundant: return STATUS_VALID;
case isl_ineq_separate: return STATUS_SEPARATE;
case isl_ineq_cut: return STATUS_CUT;
case isl_ineq_adj_eq: return STATUS_ADJ_EQ;
case isl_ineq_adj_ineq: return STATUS_ADJ_INEQ;
}
}
/* Compute the position of the equalities of basic map "bmap_i"
* with respect to the basic map represented by "tab_j".
* The resulting array has twice as many entries as the number
* of equalities corresponding to the two inequalties to which
* each equality corresponds.
*/
static int *eq_status_in(__isl_keep isl_basic_map *bmap_i,
struct isl_tab *tab_j)
{
int k, l;
int *eq = isl_calloc_array(bmap_i->ctx, int, 2 * bmap_i->n_eq);
unsigned dim;
if (!eq)
return NULL;
dim = isl_basic_map_total_dim(bmap_i);
for (k = 0; k < bmap_i->n_eq; ++k) {
for (l = 0; l < 2; ++l) {
isl_seq_neg(bmap_i->eq[k], bmap_i->eq[k], 1+dim);
eq[2 * k + l] = status_in(bmap_i->eq[k], tab_j);
if (eq[2 * k + l] == STATUS_ERROR)
goto error;
}
if (eq[2 * k] == STATUS_SEPARATE ||
eq[2 * k + 1] == STATUS_SEPARATE)
break;
}
return eq;
error:
free(eq);
return NULL;
}
/* Compute the position of the inequalities of basic map "bmap_i"
* (also represented by "tab_i", if not NULL) with respect to the basic map
* represented by "tab_j".
*/
static int *ineq_status_in(__isl_keep isl_basic_map *bmap_i,
struct isl_tab *tab_i, struct isl_tab *tab_j)
{
int k;
unsigned n_eq = bmap_i->n_eq;
int *ineq = isl_calloc_array(bmap_i->ctx, int, bmap_i->n_ineq);
if (!ineq)
return NULL;
for (k = 0; k < bmap_i->n_ineq; ++k) {
if (tab_i && isl_tab_is_redundant(tab_i, n_eq + k)) {
ineq[k] = STATUS_REDUNDANT;
continue;
}
ineq[k] = status_in(bmap_i->ineq[k], tab_j);
if (ineq[k] == STATUS_ERROR)
goto error;
if (ineq[k] == STATUS_SEPARATE)
break;
}
return ineq;
error:
free(ineq);
return NULL;
}
static int any(int *con, unsigned len, int status)
{
int i;
for (i = 0; i < len ; ++i)
if (con[i] == status)
return 1;
return 0;
}
static int count(int *con, unsigned len, int status)
{
int i;
int c = 0;
for (i = 0; i < len ; ++i)
if (con[i] == status)
c++;
return c;
}
static int all(int *con, unsigned len, int status)
{
int i;
for (i = 0; i < len ; ++i) {
if (con[i] == STATUS_REDUNDANT)
continue;
if (con[i] != status)
return 0;
}
return 1;
}
/* Internal information associated to a basic map in a map
* that is to be coalesced by isl_map_coalesce.
*
* "bmap" is the basic map itself (or NULL if "removed" is set)
* "tab" is the corresponding tableau (or NULL if "removed" is set)
* "hull_hash" identifies the affine space in which "bmap" lives.
* "removed" is set if this basic map has been removed from the map
* "simplify" is set if this basic map may have some unknown integer
* divisions that were not present in the input basic maps. The basic
* map should then be simplified such that we may be able to find
* a definition among the constraints.
*
* "eq" and "ineq" are only set if we are currently trying to coalesce
* this basic map with another basic map, in which case they represent
* the position of the inequalities of this basic map with respect to
* the other basic map. The number of elements in the "eq" array
* is twice the number of equalities in the "bmap", corresponding
* to the two inequalities that make up each equality.
*/
struct isl_coalesce_info {
isl_basic_map *bmap;
struct isl_tab *tab;
uint32_t hull_hash;
int removed;
int simplify;
int *eq;
int *ineq;
};
/* Compute the hash of the (apparent) affine hull of info->bmap (with
* the existentially quantified variables removed) and store it
* in info->hash.
*/
static int coalesce_info_set_hull_hash(struct isl_coalesce_info *info)
{
isl_basic_map *hull;
unsigned n_div;
hull = isl_basic_map_copy(info->bmap);
hull = isl_basic_map_plain_affine_hull(hull);
n_div = isl_basic_map_dim(hull, isl_dim_div);
hull = isl_basic_map_drop_constraints_involving_dims(hull,
isl_dim_div, 0, n_div);
info->hull_hash = isl_basic_map_get_hash(hull);
isl_basic_map_free(hull);
return hull ? 0 : -1;
}
/* Free all the allocated memory in an array
* of "n" isl_coalesce_info elements.
*/
static void clear_coalesce_info(int n, struct isl_coalesce_info *info)
{
int i;
if (!info)
return;
for (i = 0; i < n; ++i) {
isl_basic_map_free(info[i].bmap);
isl_tab_free(info[i].tab);
}
free(info);
}
/* Drop the basic map represented by "info".
* That is, clear the memory associated to the entry and
* mark it as having been removed.
*/
static void drop(struct isl_coalesce_info *info)
{
info->bmap = isl_basic_map_free(info->bmap);
isl_tab_free(info->tab);
info->tab = NULL;
info->removed = 1;
}
/* Exchange the information in "info1" with that in "info2".
*/
static void exchange(struct isl_coalesce_info *info1,
struct isl_coalesce_info *info2)
{
struct isl_coalesce_info info;
info = *info1;
*info1 = *info2;
*info2 = info;
}
/* This type represents the kind of change that has been performed
* while trying to coalesce two basic maps.
*
* isl_change_none: nothing was changed
* isl_change_drop_first: the first basic map was removed
* isl_change_drop_second: the second basic map was removed
* isl_change_fuse: the two basic maps were replaced by a new basic map.
*/
enum isl_change {
isl_change_error = -1,
isl_change_none = 0,
isl_change_drop_first,
isl_change_drop_second,
isl_change_fuse,
};
/* Update "change" based on an interchange of the first and the second
* basic map. That is, interchange isl_change_drop_first and
* isl_change_drop_second.
*/
static enum isl_change invert_change(enum isl_change change)
{
switch (change) {
case isl_change_error:
return isl_change_error;
case isl_change_none:
return isl_change_none;
case isl_change_drop_first:
return isl_change_drop_second;
case isl_change_drop_second:
return isl_change_drop_first;
case isl_change_fuse:
return isl_change_fuse;
}
return isl_change_error;
}
/* Add the valid constraints of the basic map represented by "info"
* to "bmap". "len" is the size of the constraints.
* If only one of the pair of inequalities that make up an equality
* is valid, then add that inequality.
*/
static __isl_give isl_basic_map *add_valid_constraints(
__isl_take isl_basic_map *bmap, struct isl_coalesce_info *info,
unsigned len)
{
int k, l;
if (!bmap)
return NULL;
for (k = 0; k < info->bmap->n_eq; ++k) {
if (info->eq[2 * k] == STATUS_VALID &&
info->eq[2 * k + 1] == STATUS_VALID) {
l = isl_basic_map_alloc_equality(bmap);
if (l < 0)
return isl_basic_map_free(bmap);
isl_seq_cpy(bmap->eq[l], info->bmap->eq[k], len);
} else if (info->eq[2 * k] == STATUS_VALID) {
l = isl_basic_map_alloc_inequality(bmap);
if (l < 0)
return isl_basic_map_free(bmap);
isl_seq_neg(bmap->ineq[l], info->bmap->eq[k], len);
} else if (info->eq[2 * k + 1] == STATUS_VALID) {
l = isl_basic_map_alloc_inequality(bmap);
if (l < 0)
return isl_basic_map_free(bmap);
isl_seq_cpy(bmap->ineq[l], info->bmap->eq[k], len);
}
}
for (k = 0; k < info->bmap->n_ineq; ++k) {
if (info->ineq[k] != STATUS_VALID)
continue;
l = isl_basic_map_alloc_inequality(bmap);
if (l < 0)
return isl_basic_map_free(bmap);
isl_seq_cpy(bmap->ineq[l], info->bmap->ineq[k], len);
}
return bmap;
}
/* Is "bmap" defined by a number of (non-redundant) constraints that
* is greater than the number of constraints of basic maps i and j combined?
* Equalities are counted as two inequalities.
*/
static int number_of_constraints_increases(int i, int j,
struct isl_coalesce_info *info,
__isl_keep isl_basic_map *bmap, struct isl_tab *tab)
{
int k, n_old, n_new;
n_old = 2 * info[i].bmap->n_eq + info[i].bmap->n_ineq;
n_old += 2 * info[j].bmap->n_eq + info[j].bmap->n_ineq;
n_new = 2 * bmap->n_eq;
for (k = 0; k < bmap->n_ineq; ++k)
if (!isl_tab_is_redundant(tab, bmap->n_eq + k))
++n_new;
return n_new > n_old;
}
/* Replace the pair of basic maps i and j by the basic map bounded
* by the valid constraints in both basic maps and the constraints
* in extra (if not NULL).
* Place the fused basic map in the position that is the smallest of i and j.
*
* If "detect_equalities" is set, then look for equalities encoded
* as pairs of inequalities.
* If "check_number" is set, then the original basic maps are only
* replaced if the total number of constraints does not increase.
* While the number of integer divisions in the two basic maps
* is assumed to be the same, the actual definitions may be different.
* We only copy the definition from one of the basic map if it is
* the same as that of the other basic map. Otherwise, we mark
* the integer division as unknown and schedule for the basic map
* to be simplified in an attempt to recover the integer division definition.
*/
static enum isl_change fuse(int i, int j, struct isl_coalesce_info *info,
__isl_keep isl_mat *extra, int detect_equalities, int check_number)
{
int k, l;
struct isl_basic_map *fused = NULL;
struct isl_tab *fused_tab = NULL;
unsigned total = isl_basic_map_total_dim(info[i].bmap);
unsigned extra_rows = extra ? extra->n_row : 0;
unsigned n_eq, n_ineq;
if (j < i)
return fuse(j, i, info, extra, detect_equalities, check_number);
n_eq = info[i].bmap->n_eq + info[j].bmap->n_eq;
n_ineq = info[i].bmap->n_ineq + info[j].bmap->n_ineq;
fused = isl_basic_map_alloc_space(isl_space_copy(info[i].bmap->dim),
info[i].bmap->n_div, n_eq, n_eq + n_ineq + extra_rows);
fused = add_valid_constraints(fused, &info[i], 1 + total);
fused = add_valid_constraints(fused, &info[j], 1 + total);
if (!fused)
goto error;
for (k = 0; k < info[i].bmap->n_div; ++k) {
int l = isl_basic_map_alloc_div(fused);
if (l < 0)
goto error;
if (isl_seq_eq(info[i].bmap->div[k], info[j].bmap->div[k],
1 + 1 + total)) {
isl_seq_cpy(fused->div[l], info[i].bmap->div[k],
1 + 1 + total);
} else {
isl_int_set_si(fused->div[l][0], 0);
info[i].simplify = 1;
}
}
for (k = 0; k < extra_rows; ++k) {
l = isl_basic_map_alloc_inequality(fused);
if (l < 0)
goto error;
isl_seq_cpy(fused->ineq[l], extra->row[k], 1 + total);
}
if (detect_equalities)
fused = isl_basic_map_detect_inequality_pairs(fused, NULL);
fused = isl_basic_map_gauss(fused, NULL);
ISL_F_SET(fused, ISL_BASIC_MAP_FINAL);
if (ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_RATIONAL) &&
ISL_F_ISSET(info[j].bmap, ISL_BASIC_MAP_RATIONAL))
ISL_F_SET(fused, ISL_BASIC_MAP_RATIONAL);
fused_tab = isl_tab_from_basic_map(fused, 0);
if (isl_tab_detect_redundant(fused_tab) < 0)
goto error;
if (check_number &&
number_of_constraints_increases(i, j, info, fused, fused_tab)) {
isl_tab_free(fused_tab);
isl_basic_map_free(fused);
return isl_change_none;
}
info[i].simplify |= info[j].simplify;
isl_basic_map_free(info[i].bmap);
info[i].bmap = fused;
isl_tab_free(info[i].tab);
info[i].tab = fused_tab;
drop(&info[j]);
return isl_change_fuse;
error:
isl_tab_free(fused_tab);
isl_basic_map_free(fused);
return isl_change_error;
}
/* Given a pair of basic maps i and j such that all constraints are either
* "valid" or "cut", check if the facets corresponding to the "cut"
* constraints of i lie entirely within basic map j.
* If so, replace the pair by the basic map consisting of the valid
* constraints in both basic maps.
* Checking whether the facet lies entirely within basic map j
* is performed by checking whether the constraints of basic map j
* are valid for the facet. These tests are performed on a rational
* tableau to avoid the theoretical possibility that a constraint
* that was considered to be a cut constraint for the entire basic map i
* happens to be considered to be a valid constraint for the facet,
* even though it cuts off the same rational points.
*
* To see that we are not introducing any extra points, call the
* two basic maps A and B and the resulting map U and let x
* be an element of U \setminus ( A \cup B ).
* A line connecting x with an element of A \cup B meets a facet F
* of either A or B. Assume it is a facet of B and let c_1 be
* the corresponding facet constraint. We have c_1(x) < 0 and
* so c_1 is a cut constraint. This implies that there is some
* (possibly rational) point x' satisfying the constraints of A
* and the opposite of c_1 as otherwise c_1 would have been marked
* valid for A. The line connecting x and x' meets a facet of A
* in a (possibly rational) point that also violates c_1, but this
* is impossible since all cut constraints of B are valid for all
* cut facets of A.
* In case F is a facet of A rather than B, then we can apply the
* above reasoning to find a facet of B separating x from A \cup B first.
*/
static enum isl_change check_facets(int i, int j,
struct isl_coalesce_info *info)
{
int k, l;
struct isl_tab_undo *snap, *snap2;
unsigned n_eq = info[i].bmap->n_eq;
snap = isl_tab_snap(info[i].tab);
if (isl_tab_mark_rational(info[i].tab) < 0)
return isl_change_error;
snap2 = isl_tab_snap(info[i].tab);
for (k = 0; k < info[i].bmap->n_ineq; ++k) {
if (info[i].ineq[k] != STATUS_CUT)
continue;
if (isl_tab_select_facet(info[i].tab, n_eq + k) < 0)
return isl_change_error;
for (l = 0; l < info[j].bmap->n_ineq; ++l) {
int stat;
if (info[j].ineq[l] != STATUS_CUT)
continue;
stat = status_in(info[j].bmap->ineq[l], info[i].tab);
if (stat < 0)
return isl_change_error;
if (stat != STATUS_VALID)
break;
}
if (isl_tab_rollback(info[i].tab, snap2) < 0)
return isl_change_error;
if (l < info[j].bmap->n_ineq)
break;
}
if (k < info[i].bmap->n_ineq) {
if (isl_tab_rollback(info[i].tab, snap) < 0)
return isl_change_error;
return isl_change_none;
}
return fuse(i, j, info, NULL, 0, 0);
}
/* Check if info->bmap contains the basic map represented
* by the tableau "tab".
* For each equality, we check both the constraint itself
* (as an inequality) and its negation. Make sure the
* equality is returned to its original state before returning.
*/
static int contains(struct isl_coalesce_info *info, struct isl_tab *tab)
{
int k;
unsigned dim;
isl_basic_map *bmap = info->bmap;
dim = isl_basic_map_total_dim(bmap);
for (k = 0; k < bmap->n_eq; ++k) {
int stat;
isl_seq_neg(bmap->eq[k], bmap->eq[k], 1 + dim);
stat = status_in(bmap->eq[k], tab);
isl_seq_neg(bmap->eq[k], bmap->eq[k], 1 + dim);
if (stat < 0)
return -1;
if (stat != STATUS_VALID)
return 0;
stat = status_in(bmap->eq[k], tab);
if (stat < 0)
return -1;
if (stat != STATUS_VALID)
return 0;
}
for (k = 0; k < bmap->n_ineq; ++k) {
int stat;
if (info->ineq[k] == STATUS_REDUNDANT)
continue;
stat = status_in(bmap->ineq[k], tab);
if (stat < 0)
return -1;
if (stat != STATUS_VALID)
return 0;
}
return 1;
}
/* Basic map "i" has an inequality (say "k") that is adjacent
* to some inequality of basic map "j". All the other inequalities
* are valid for "j".
* Check if basic map "j" forms an extension of basic map "i".
*
* Note that this function is only called if some of the equalities or
* inequalities of basic map "j" do cut basic map "i". The function is
* correct even if there are no such cut constraints, but in that case
* the additional checks performed by this function are overkill.
*
* In particular, we replace constraint k, say f >= 0, by constraint
* f <= -1, add the inequalities of "j" that are valid for "i"
* and check if the result is a subset of basic map "j".
* If so, then we know that this result is exactly equal to basic map "j"
* since all its constraints are valid for basic map "j".
* By combining the valid constraints of "i" (all equalities and all
* inequalities except "k") and the valid constraints of "j" we therefore
* obtain a basic map that is equal to their union.
* In this case, there is no need to perform a rollback of the tableau
* since it is going to be destroyed in fuse().
*
*
* |\__ |\__
* | \__ | \__
* | \_ => | \__
* |_______| _ |_________\
*
*
* |\ |\
* | \ | \
* | \ | \
* | | | \
* | ||\ => | \
* | || \ | \
* | || | | |
* |__||_/ |_____/
*/
static enum isl_change is_adj_ineq_extension(int i, int j,
struct isl_coalesce_info *info)
{
int k;
struct isl_tab_undo *snap;
unsigned n_eq = info[i].bmap->n_eq;
unsigned total = isl_basic_map_total_dim(info[i].bmap);
int r;
int super;
if (isl_tab_extend_cons(info[i].tab, 1 + info[j].bmap->n_ineq) < 0)
return isl_change_error;
for (k = 0; k < info[i].bmap->n_ineq; ++k)
if (info[i].ineq[k] == STATUS_ADJ_INEQ)
break;
if (k >= info[i].bmap->n_ineq)
isl_die(isl_basic_map_get_ctx(info[i].bmap), isl_error_internal,
"info[i].ineq should have exactly one STATUS_ADJ_INEQ",
return isl_change_error);
snap = isl_tab_snap(info[i].tab);
if (isl_tab_unrestrict(info[i].tab, n_eq + k) < 0)
return isl_change_error;
isl_seq_neg(info[i].bmap->ineq[k], info[i].bmap->ineq[k], 1 + total);
isl_int_sub_ui(info[i].bmap->ineq[k][0], info[i].bmap->ineq[k][0], 1);
r = isl_tab_add_ineq(info[i].tab, info[i].bmap->ineq[k]);
isl_seq_neg(info[i].bmap->ineq[k], info[i].bmap->ineq[k], 1 + total);
isl_int_sub_ui(info[i].bmap->ineq[k][0], info[i].bmap->ineq[k][0], 1);
if (r < 0)
return isl_change_error;
for (k = 0; k < info[j].bmap->n_ineq; ++k) {
if (info[j].ineq[k] != STATUS_VALID)
continue;
if (isl_tab_add_ineq(info[i].tab, info[j].bmap->ineq[k]) < 0)
return isl_change_error;
}
super = contains(&info[j], info[i].tab);
if (super < 0)
return isl_change_error;
if (super)
return fuse(i, j, info, NULL, 0, 0);
if (isl_tab_rollback(info[i].tab, snap) < 0)
return isl_change_error;
return isl_change_none;
}
/* Both basic maps have at least one inequality with and adjacent
* (but opposite) inequality in the other basic map.
* Check that there are no cut constraints and that there is only
* a single pair of adjacent inequalities.
* If so, we can replace the pair by a single basic map described
* by all but the pair of adjacent inequalities.
* Any additional points introduced lie strictly between the two
* adjacent hyperplanes and can therefore be integral.
*
* ____ _____
* / ||\ / \
* / || \ / \
* \ || \ => \ \
* \ || / \ /
* \___||_/ \_____/
*
* The test for a single pair of adjancent inequalities is important
* for avoiding the combination of two basic maps like the following
*
* /|
* / |
* /__|
* _____
* | |
* | |
* |___|
*
* If there are some cut constraints on one side, then we may
* still be able to fuse the two basic maps, but we need to perform
* some additional checks in is_adj_ineq_extension.
*/
static enum isl_change check_adj_ineq(int i, int j,
struct isl_coalesce_info *info)
{
int count_i, count_j;
int cut_i, cut_j;
count_i = count(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_INEQ);
count_j = count(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_INEQ);
if (count_i != 1 && count_j != 1)
return isl_change_none;
cut_i = any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT) ||
any(info[i].ineq, info[i].bmap->n_ineq, STATUS_CUT);
cut_j = any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_CUT) ||
any(info[j].ineq, info[j].bmap->n_ineq, STATUS_CUT);
if (!cut_i && !cut_j && count_i == 1 && count_j == 1)
return fuse(i, j, info, NULL, 0, 0);
if (count_i == 1 && !cut_i)
return is_adj_ineq_extension(i, j, info);
if (count_j == 1 && !cut_j)
return is_adj_ineq_extension(j, i, info);
return isl_change_none;
}
/* Basic map "i" has an inequality "k" that is adjacent to some equality
* of basic map "j". All the other inequalities are valid for "j".
* Check if basic map "j" forms an extension of basic map "i".
*
* In particular, we relax constraint "k", compute the corresponding
* facet and check whether it is included in the other basic map.
* If so, we know that relaxing the constraint extends the basic
* map with exactly the other basic map (we already know that this
* other basic map is included in the extension, because there
* were no "cut" inequalities in "i") and we can replace the
* two basic maps by this extension.
* Each integer division that does not have exactly the same
* definition in "i" and "j" is marked unknown and the basic map
* is scheduled to be simplified in an attempt to recover
* the integer division definition.
* Place this extension in the position that is the smallest of i and j.
* ____ _____
* / || / |
* / || / |
* \ || => \ |
* \ || \ |
* \___|| \____|
*/
static enum isl_change is_adj_eq_extension(int i, int j, int k,
struct isl_coalesce_info *info)
{
int change = isl_change_none;
int super;
struct isl_tab_undo *snap, *snap2;
unsigned n_eq = info[i].bmap->n_eq;
if (isl_tab_is_equality(info[i].tab, n_eq + k))
return isl_change_none;
snap = isl_tab_snap(info[i].tab);
if (isl_tab_relax(info[i].tab, n_eq + k) < 0)
return isl_change_error;
snap2 = isl_tab_snap(info[i].tab);
if (isl_tab_select_facet(info[i].tab, n_eq + k) < 0)
return isl_change_error;
super = contains(&info[j], info[i].tab);
if (super < 0)
return isl_change_error;
if (super) {
int l;
unsigned total;
if (isl_tab_rollback(info[i].tab, snap2) < 0)
return isl_change_error;
info[i].bmap = isl_basic_map_cow(info[i].bmap);
if (!info[i].bmap)
return isl_change_error;
total = isl_basic_map_total_dim(info[i].bmap);
for (l = 0; l < info[i].bmap->n_div; ++l)
if (!isl_seq_eq(info[i].bmap->div[l],
info[j].bmap->div[l], 1 + 1 + total)) {
isl_int_set_si(info[i].bmap->div[l][0], 0);
info[i].simplify = 1;
}
isl_int_add_ui(info[i].bmap->ineq[k][0],
info[i].bmap->ineq[k][0], 1);
ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_FINAL);
drop(&info[j]);
if (j < i)
exchange(&info[i], &info[j]);
change = isl_change_fuse;
} else
if (isl_tab_rollback(info[i].tab, snap) < 0)
return isl_change_error;
return change;
}
/* Data structure that keeps track of the wrapping constraints
* and of information to bound the coefficients of those constraints.
*
* bound is set if we want to apply a bound on the coefficients
* mat contains the wrapping constraints
* max is the bound on the coefficients (if bound is set)
*/
struct isl_wraps {
int bound;
isl_mat *mat;
isl_int max;
};
/* Update wraps->max to be greater than or equal to the coefficients
* in the equalities and inequalities of info->bmap that can be removed
* if we end up applying wrapping.
*/
static void wraps_update_max(struct isl_wraps *wraps,
struct isl_coalesce_info *info)
{
int k;
isl_int max_k;
unsigned total = isl_basic_map_total_dim(info->bmap);
isl_int_init(max_k);
for (k = 0; k < info->bmap->n_eq; ++k) {
if (info->eq[2 * k] == STATUS_VALID &&
info->eq[2 * k + 1] == STATUS_VALID)
continue;
isl_seq_abs_max(info->bmap->eq[k] + 1, total, &max_k);
if (isl_int_abs_gt(max_k, wraps->max))
isl_int_set(wraps->max, max_k);
}
for (k = 0; k < info->bmap->n_ineq; ++k) {
if (info->ineq[k] == STATUS_VALID ||
info->ineq[k] == STATUS_REDUNDANT)
continue;
isl_seq_abs_max(info->bmap->ineq[k] + 1, total, &max_k);
if (isl_int_abs_gt(max_k, wraps->max))
isl_int_set(wraps->max, max_k);
}
isl_int_clear(max_k);
}
/* Initialize the isl_wraps data structure.
* If we want to bound the coefficients of the wrapping constraints,
* we set wraps->max to the largest coefficient
* in the equalities and inequalities that can be removed if we end up
* applying wrapping.
*/
static void wraps_init(struct isl_wraps *wraps, __isl_take isl_mat *mat,
struct isl_coalesce_info *info, int i, int j)
{
isl_ctx *ctx;
wraps->bound = 0;
wraps->mat = mat;
if (!mat)
return;
ctx = isl_mat_get_ctx(mat);
wraps->bound = isl_options_get_coalesce_bounded_wrapping(ctx);
if (!wraps->bound)
return;
isl_int_init(wraps->max);
isl_int_set_si(wraps->max, 0);
wraps_update_max(wraps, &info[i]);
wraps_update_max(wraps, &info[j]);
}
/* Free the contents of the isl_wraps data structure.
*/
static void wraps_free(struct isl_wraps *wraps)
{
isl_mat_free(wraps->mat);
if (wraps->bound)
isl_int_clear(wraps->max);
}
/* Is the wrapping constraint in row "row" allowed?
*
* If wraps->bound is set, we check that none of the coefficients
* is greater than wraps->max.
*/
static int allow_wrap(struct isl_wraps *wraps, int row)
{
int i;
if (!wraps->bound)
return 1;
for (i = 1; i < wraps->mat->n_col; ++i)
if (isl_int_abs_gt(wraps->mat->row[row][i], wraps->max))
return 0;
return 1;
}
/* Wrap "ineq" (or its opposite if "negate" is set) around "bound"
* to include "set" and add the result in position "w" of "wraps".
* "len" is the total number of coefficients in "bound" and "ineq".
* Return 1 on success, 0 on failure and -1 on error.
* Wrapping can fail if the result of wrapping is equal to "bound"
* or if we want to bound the sizes of the coefficients and
* the wrapped constraint does not satisfy this bound.
*/
static int add_wrap(struct isl_wraps *wraps, int w, isl_int *bound,
isl_int *ineq, unsigned len, __isl_keep isl_set *set, int negate)
{
isl_seq_cpy(wraps->mat->row[w], bound, len);
if (negate) {
isl_seq_neg(wraps->mat->row[w + 1], ineq, len);
ineq = wraps->mat->row[w + 1];
}
if (!isl_set_wrap_facet(set, wraps->mat->row[w], ineq))
return -1;
if (isl_seq_eq(wraps->mat->row[w], bound, len))
return 0;
if (!allow_wrap(wraps, w))
return 0;
return 1;
}
/* For each constraint in info->bmap that is not redundant (as determined
* by info->tab) and that is not a valid constraint for the other basic map,
* wrap the constraint around "bound" such that it includes the whole
* set "set" and append the resulting constraint to "wraps".
* Note that the constraints that are valid for the other basic map
* will be added to the combined basic map by default, so there is
* no need to wrap them.
* The caller wrap_in_facets even relies on this function not wrapping
* any constraints that are already valid.
* "wraps" is assumed to have been pre-allocated to the appropriate size.
* wraps->n_row is the number of actual wrapped constraints that have
* been added.
* If any of the wrapping problems results in a constraint that is
* identical to "bound", then this means that "set" is unbounded in such
* way that no wrapping is possible. If this happens then wraps->n_row
* is reset to zero.
* Similarly, if we want to bound the coefficients of the wrapping
* constraints and a newly added wrapping constraint does not
* satisfy the bound, then wraps->n_row is also reset to zero.
*/
static int add_wraps(struct isl_wraps *wraps, struct isl_coalesce_info *info,
isl_int *bound, __isl_keep isl_set *set)
{
int l, m;
int w;
int added;
isl_basic_map *bmap = info->bmap;
unsigned len = 1 + isl_basic_map_total_dim(bmap);
w = wraps->mat->n_row;
for (l = 0; l < bmap->n_ineq; ++l) {
if (info->ineq[l] == STATUS_VALID ||
info->ineq[l] == STATUS_REDUNDANT)
continue;
if (isl_seq_is_neg(bound, bmap->ineq[l], len))
continue;
if (isl_seq_eq(bound, bmap->ineq[l], len))
continue;
if (isl_tab_is_redundant(info->tab, bmap->n_eq + l))
continue;
added = add_wrap(wraps, w, bound, bmap->ineq[l], len, set, 0);
if (added < 0)
return -1;
if (!added)
goto unbounded;
++w;
}
for (l = 0; l < bmap->n_eq; ++l) {
if (isl_seq_is_neg(bound, bmap->eq[l], len))
continue;
if (isl_seq_eq(bound, bmap->eq[l], len))
continue;
for (m = 0; m < 2; ++m) {
if (info->eq[2 * l + m] == STATUS_VALID)
continue;
added = add_wrap(wraps, w, bound, bmap->eq[l], len,
set, !m);
if (added < 0)
return -1;
if (!added)
goto unbounded;
++w;
}
}
wraps->mat->n_row = w;
return 0;
unbounded:
wraps->mat->n_row = 0;
return 0;
}
/* Check if the constraints in "wraps" from "first" until the last
* are all valid for the basic set represented by "tab".
* If not, wraps->n_row is set to zero.
*/
static int check_wraps(__isl_keep isl_mat *wraps, int first,
struct isl_tab *tab)
{
int i;
for (i = first; i < wraps->n_row; ++i) {
enum isl_ineq_type type;
type = isl_tab_ineq_type(tab, wraps->row[i]);
if (type == isl_ineq_error)
return -1;
if (type == isl_ineq_redundant)
continue;
wraps->n_row = 0;
return 0;
}
return 0;
}
/* Return a set that corresponds to the non-redundant constraints
* (as recorded in tab) of bmap.
*
* It's important to remove the redundant constraints as some
* of the other constraints may have been modified after the
* constraints were marked redundant.
* In particular, a constraint may have been relaxed.
* Redundant constraints are ignored when a constraint is relaxed
* and should therefore continue to be ignored ever after.
* Otherwise, the relaxation might be thwarted by some of
* these constraints.
*
* Update the underlying set to ensure that the dimension doesn't change.
* Otherwise the integer divisions could get dropped if the tab
* turns out to be empty.
*/
static __isl_give isl_set *set_from_updated_bmap(__isl_keep isl_basic_map *bmap,
struct isl_tab *tab)
{
isl_basic_set *bset;
bmap = isl_basic_map_copy(bmap);
bset = isl_basic_map_underlying_set(bmap);
bset = isl_basic_set_cow(bset);
bset = isl_basic_set_update_from_tab(bset, tab);
return isl_set_from_basic_set(bset);
}
/* Wrap the constraints of info->bmap that bound the facet defined
* by inequality "k" around (the opposite of) this inequality to
* include "set". "bound" may be used to store the negated inequality.
* Since the wrapped constraints are not guaranteed to contain the whole
* of info->bmap, we check them in check_wraps.
* If any of the wrapped constraints turn out to be invalid, then
* check_wraps will reset wrap->n_row to zero.
*/
static int add_wraps_around_facet(struct isl_wraps *wraps,
struct isl_coalesce_info *info, int k, isl_int *bound,
__isl_keep isl_set *set)
{
struct isl_tab_undo *snap;
int n;
unsigned total = isl_basic_map_total_dim(info->bmap);
snap = isl_tab_snap(info->tab);
if (isl_tab_select_facet(info->tab, info->bmap->n_eq + k) < 0)
return -1;
if (isl_tab_detect_redundant(info->tab) < 0)
return -1;
isl_seq_neg(bound, info->bmap->ineq[k], 1 + total);
n = wraps->mat->n_row;
if (add_wraps(wraps, info, bound, set) < 0)
return -1;
if (isl_tab_rollback(info->tab, snap) < 0)
return -1;
if (check_wraps(wraps->mat, n, info->tab) < 0)
return -1;
return 0;
}
/* Given a basic set i with a constraint k that is adjacent to
* basic set j, check if we can wrap
* both the facet corresponding to k (if "wrap_facet" is set) and basic map j
* (always) around their ridges to include the other set.
* If so, replace the pair of basic sets by their union.
*
* All constraints of i (except k) are assumed to be valid or
* cut constraints for j.
* Wrapping the cut constraints to include basic map j may result
* in constraints that are no longer valid of basic map i
* we have to check that the resulting wrapping constraints are valid for i.
* If "wrap_facet" is not set, then all constraints of i (except k)
* are assumed to be valid for j.
* ____ _____
* / | / \
* / || / |
* \ || => \ |
* \ || \ |
* \___|| \____|
*
*/
static enum isl_change can_wrap_in_facet(int i, int j, int k,
struct isl_coalesce_info *info, int wrap_facet)
{
enum isl_change change = isl_change_none;
struct isl_wraps wraps;
isl_ctx *ctx;
isl_mat *mat;
struct isl_set *set_i = NULL;
struct isl_set *set_j = NULL;
struct isl_vec *bound = NULL;
unsigned total = isl_basic_map_total_dim(info[i].bmap);
set_i = set_from_updated_bmap(info[i].bmap, info[i].tab);
set_j = set_from_updated_bmap(info[j].bmap, info[j].tab);
ctx = isl_basic_map_get_ctx(info[i].bmap);
mat = isl_mat_alloc(ctx, 2 * (info[i].bmap->n_eq + info[j].bmap->n_eq) +
info[i].bmap->n_ineq + info[j].bmap->n_ineq,
1 + total);
wraps_init(&wraps, mat, info, i, j);
bound = isl_vec_alloc(ctx, 1 + total);
if (!set_i || !set_j || !wraps.mat || !bound)
goto error;
isl_seq_cpy(bound->el, info[i].bmap->ineq[k], 1 + total);
isl_int_add_ui(bound->el[0], bound->el[0], 1);
isl_seq_cpy(wraps.mat->row[0], bound->el, 1 + total);
wraps.mat->n_row = 1;
if (add_wraps(&wraps, &info[j], bound->el, set_i) < 0)
goto error;
if (!wraps.mat->n_row)
goto unbounded;
if (wrap_facet) {
if (add_wraps_around_facet(&wraps, &info[i], k,
bound->el, set_j) < 0)
goto error;
if (!wraps.mat->n_row)
goto unbounded;
}
change = fuse(i, j, info, wraps.mat, 0, 0);
unbounded:
wraps_free(&wraps);
isl_set_free(set_i);
isl_set_free(set_j);
isl_vec_free(bound);
return change;
error:
wraps_free(&wraps);
isl_vec_free(bound);
isl_set_free(set_i);
isl_set_free(set_j);
return isl_change_error;
}
/* Given a pair of basic maps i and j such that j sticks out
* of i at n cut constraints, each time by at most one,
* try to compute wrapping constraints and replace the two
* basic maps by a single basic map.
* The other constraints of i are assumed to be valid for j.
*
* For each cut constraint t(x) >= 0 of i, we add the relaxed version
* t(x) + 1 >= 0, along with wrapping constraints for all constraints
* of basic map j that bound the part of basic map j that sticks out
* of the cut constraint.
* In particular, we first intersect basic map j with t(x) + 1 = 0.
* If the result is empty, then t(x) >= 0 was actually a valid constraint
* (with respect to the integer points), so we add t(x) >= 0 instead.
* Otherwise, we wrap the constraints of basic map j that are not
* redundant in this intersection and that are not already valid
* for basic map i over basic map i.
* Note that it is sufficient to wrap the constraints to include
* basic map i, because we will only wrap the constraints that do
* not include basic map i already. The wrapped constraint will
* therefore be more relaxed compared to the original constraint.
* Since the original constraint is valid for basic map j, so is
* the wrapped constraint.
*
* If any wrapping fails, i.e., if we cannot wrap to touch
* the union, then we give up.
* Otherwise, the pair of basic maps is replaced by their union.
*/
static enum isl_change wrap_in_facets(int i, int j, int *cuts, int n,
struct isl_coalesce_info *info)
{
enum isl_change change = isl_change_none;
struct isl_wraps wraps;
isl_ctx *ctx;
isl_mat *mat;
isl_set *set_i = NULL;
unsigned total = isl_basic_map_total_dim(info[i].bmap);
int max_wrap;
int k, w;
struct isl_tab_undo *snap;
if (isl_tab_extend_cons(info[j].tab, 1) < 0)
goto error;
max_wrap = 1 + 2 * info[j].bmap->n_eq + info[j].bmap->n_ineq;
max_wrap *= n;
set_i = set_from_updated_bmap(info[i].bmap, info[i].tab);
ctx = isl_basic_map_get_ctx(info[i].bmap);
mat = isl_mat_alloc(ctx, max_wrap, 1 + total);
wraps_init(&wraps, mat, info, i, j);
if (!set_i || !wraps.mat)
goto error;
snap = isl_tab_snap(info[j].tab);
wraps.mat->n_row = 0;
for (k = 0; k < n; ++k) {
w = wraps.mat->n_row++;
isl_seq_cpy(wraps.mat->row[w],
info[i].bmap->ineq[cuts[k]], 1 + total);
isl_int_add_ui(wraps.mat->row[w][0], wraps.mat->row[w][0], 1);
if (isl_tab_add_eq(info[j].tab, wraps.mat->row[w]) < 0)
goto error;
if (isl_tab_detect_redundant(info[j].tab) < 0)
goto error;
if (info[j].tab->empty)
isl_int_sub_ui(wraps.mat->row[w][0],
wraps.mat->row[w][0], 1);
else if (add_wraps(&wraps, &info[j],
wraps.mat->row[w], set_i) < 0)
goto error;
if (isl_tab_rollback(info[j].tab, snap) < 0)
goto error;
if (!wraps.mat->n_row)
break;
}
if (k == n)
change = fuse(i, j, info, wraps.mat, 0, 1);
wraps_free(&wraps);
isl_set_free(set_i);
return change;
error:
wraps_free(&wraps);
isl_set_free(set_i);
return isl_change_error;
}
/* Given two basic sets i and j such that i has no cut equalities,
* check if relaxing all the cut inequalities of i by one turns
* them into valid constraint for j and check if we can wrap in
* the bits that are sticking out.
* If so, replace the pair by their union.
*
* We first check if all relaxed cut inequalities of i are valid for j
* and then try to wrap in the intersections of the relaxed cut inequalities
* with j.
*
* During this wrapping, we consider the points of j that lie at a distance
* of exactly 1 from i. In particular, we ignore the points that lie in
* between this lower-dimensional space and the basic map i.
* We can therefore only apply this to integer maps.
* ____ _____
* / ___|_ / \
* / | | / |
* \ | | => \ |
* \|____| \ |
* \___| \____/
*
* _____ ______
* | ____|_ | \
* | | | | |
* | | | => | |
* |_| | | |
* |_____| \______|
*
* _______
* | |
* | |\ |
* | | \ |
* | | \ |
* | | \|
* | | \
* | |_____\
* | |
* |_______|
*
* Wrapping can fail if the result of wrapping one of the facets
* around its edges does not produce any new facet constraint.
* In particular, this happens when we try to wrap in unbounded sets.
*
* _______________________________________________________________________
* |
* | ___
* | | |
* |_| |_________________________________________________________________
* |___|
*
* The following is not an acceptable result of coalescing the above two
* sets as it includes extra integer points.
* _______________________________________________________________________
* |
* |
* |
* |
* \______________________________________________________________________
*/
static enum isl_change can_wrap_in_set(int i, int j,
struct isl_coalesce_info *info)
{
enum isl_change change = isl_change_none;
int k, m;
int n;
int *cuts = NULL;
isl_ctx *ctx;
if (ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_RATIONAL) ||
ISL_F_ISSET(info[j].bmap, ISL_BASIC_MAP_RATIONAL))
return isl_change_none;
n = count(info[i].ineq, info[i].bmap->n_ineq, STATUS_CUT);
if (n == 0)
return isl_change_none;
ctx = isl_basic_map_get_ctx(info[i].bmap);
cuts = isl_alloc_array(ctx, int, n);
if (!cuts)
return isl_change_error;
for (k = 0, m = 0; m < n; ++k) {
enum isl_ineq_type type;
if (info[i].ineq[k] != STATUS_CUT)
continue;
isl_int_add_ui(info[i].bmap->ineq[k][0],
info[i].bmap->ineq[k][0], 1);
type = isl_tab_ineq_type(info[j].tab, info[i].bmap->ineq[k]);
isl_int_sub_ui(info[i].bmap->ineq[k][0],
info[i].bmap->ineq[k][0], 1);
if (type == isl_ineq_error)
goto error;
if (type != isl_ineq_redundant)
break;
cuts[m] = k;
++m;
}
if (m == n)
change = wrap_in_facets(i, j, cuts, n, info);
free(cuts);
return change;
error:
free(cuts);
return isl_change_error;
}
/* Check if either i or j has only cut inequalities that can
* be used to wrap in (a facet of) the other basic set.
* if so, replace the pair by their union.
*/
static enum isl_change check_wrap(int i, int j, struct isl_coalesce_info *info)
{
enum isl_change change = isl_change_none;
if (!any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT))
change = can_wrap_in_set(i, j, info);
if (change != isl_change_none)
return change;
if (!any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_CUT))
change = can_wrap_in_set(j, i, info);
return change;
}
/* At least one of the basic maps has an equality that is adjacent
* to inequality. Make sure that only one of the basic maps has
* such an equality and that the other basic map has exactly one
* inequality adjacent to an equality.
* We call the basic map that has the inequality "i" and the basic
* map that has the equality "j".
* If "i" has any "cut" (in)equality, then relaxing the inequality
* by one would not result in a basic map that contains the other
* basic map. However, it may still be possible to wrap in the other
* basic map.
*/
static enum isl_change check_adj_eq(int i, int j,
struct isl_coalesce_info *info)
{
enum isl_change change = isl_change_none;
int k;
int any_cut;
if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_INEQ) &&
any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ADJ_INEQ))
/* ADJ EQ TOO MANY */
return isl_change_none;
if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_INEQ))
return check_adj_eq(j, i, info);
/* j has an equality adjacent to an inequality in i */
if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT))
return isl_change_none;
any_cut = any(info[i].ineq, info[i].bmap->n_ineq, STATUS_CUT);
if (count(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_EQ) != 1 ||
any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_EQ) ||
any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_INEQ) ||
any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_INEQ))
/* ADJ EQ TOO MANY */
return isl_change_none;
for (k = 0; k < info[i].bmap->n_ineq; ++k)
if (info[i].ineq[k] == STATUS_ADJ_EQ)
break;
if (!any_cut) {
change = is_adj_eq_extension(i, j, k, info);
if (change != isl_change_none)
return change;
}
change = can_wrap_in_facet(i, j, k, info, any_cut);
return change;
}
/* The two basic maps lie on adjacent hyperplanes. In particular,
* basic map "i" has an equality that lies parallel to basic map "j".
* Check if we can wrap the facets around the parallel hyperplanes
* to include the other set.
*
* We perform basically the same operations as can_wrap_in_facet,
* except that we don't need to select a facet of one of the sets.
* _
* \\ \\
* \\ => \\
* \ \|
*
* If there is more than one equality of "i" adjacent to an equality of "j",
* then the result will satisfy one or more equalities that are a linear
* combination of these equalities. These will be encoded as pairs
* of inequalities in the wrapping constraints and need to be made
* explicit.
*/
static enum isl_change check_eq_adj_eq(int i, int j,
struct isl_coalesce_info *info)
{
int k;
enum isl_change change = isl_change_none;
int detect_equalities = 0;
struct isl_wraps wraps;
isl_ctx *ctx;
isl_mat *mat;
struct isl_set *set_i = NULL;
struct isl_set *set_j = NULL;
struct isl_vec *bound = NULL;
unsigned total = isl_basic_map_total_dim(info[i].bmap);
if (count(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_EQ) != 1)
detect_equalities = 1;
for (k = 0; k < 2 * info[i].bmap->n_eq ; ++k)
if (info[i].eq[k] == STATUS_ADJ_EQ)
break;
set_i = set_from_updated_bmap(info[i].bmap, info[i].tab);
set_j = set_from_updated_bmap(info[j].bmap, info[j].tab);
ctx = isl_basic_map_get_ctx(info[i].bmap);
mat = isl_mat_alloc(ctx, 2 * (info[i].bmap->n_eq + info[j].bmap->n_eq) +
info[i].bmap->n_ineq + info[j].bmap->n_ineq,
1 + total);
wraps_init(&wraps, mat, info, i, j);
bound = isl_vec_alloc(ctx, 1 + total);
if (!set_i || !set_j || !wraps.mat || !bound)
goto error;
if (k % 2 == 0)
isl_seq_neg(bound->el, info[i].bmap->eq[k / 2], 1 + total);
else
isl_seq_cpy(bound->el, info[i].bmap->eq[k / 2], 1 + total);
isl_int_add_ui(bound->el[0], bound->el[0], 1);
isl_seq_cpy(wraps.mat->row[0], bound->el, 1 + total);
wraps.mat->n_row = 1;
if (add_wraps(&wraps, &info[j], bound->el, set_i) < 0)
goto error;
if (!wraps.mat->n_row)
goto unbounded;
isl_int_sub_ui(bound->el[0], bound->el[0], 1);
isl_seq_neg(bound->el, bound->el, 1 + total);
isl_seq_cpy(wraps.mat->row[wraps.mat->n_row], bound->el, 1 + total);
wraps.mat->n_row++;
if (add_wraps(&wraps, &info[i], bound->el, set_j) < 0)
goto error;
if (!wraps.mat->n_row)
goto unbounded;
change = fuse(i, j, info, wraps.mat, detect_equalities, 0);
if (0) {
error: change = isl_change_error;
}
unbounded:
wraps_free(&wraps);
isl_set_free(set_i);
isl_set_free(set_j);
isl_vec_free(bound);
return change;
}
/* Check if the union of the given pair of basic maps
* can be represented by a single basic map.
* If so, replace the pair by the single basic map and return
* isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
* Otherwise, return isl_change_none.
* The two basic maps are assumed to live in the same local space.
*
* We first check the effect of each constraint of one basic map
* on the other basic map.
* The constraint may be
* redundant the constraint is redundant in its own
* basic map and should be ignore and removed
* in the end
* valid all (integer) points of the other basic map
* satisfy the constraint
* separate no (integer) point of the other basic map
* satisfies the constraint
* cut some but not all points of the other basic map
* satisfy the constraint
* adj_eq the given constraint is adjacent (on the outside)
* to an equality of the other basic map
* adj_ineq the given constraint is adjacent (on the outside)
* to an inequality of the other basic map
*
* We consider seven cases in which we can replace the pair by a single
* basic map. We ignore all "redundant" constraints.
*
* 1. all constraints of one basic map are valid
* => the other basic map is a subset and can be removed
*
* 2. all constraints of both basic maps are either "valid" or "cut"
* and the facets corresponding to the "cut" constraints
* of one of the basic maps lies entirely inside the other basic map
* => the pair can be replaced by a basic map consisting
* of the valid constraints in both basic maps
*
* 3. there is a single pair of adjacent inequalities
* (all other constraints are "valid")
* => the pair can be replaced by a basic map consisting
* of the valid constraints in both basic maps
*
* 4. one basic map has a single adjacent inequality, while the other
* constraints are "valid". The other basic map has some
* "cut" constraints, but replacing the adjacent inequality by
* its opposite and adding the valid constraints of the other
* basic map results in a subset of the other basic map
* => the pair can be replaced by a basic map consisting
* of the valid constraints in both basic maps
*
* 5. there is a single adjacent pair of an inequality and an equality,
* the other constraints of the basic map containing the inequality are
* "valid". Moreover, if the inequality the basic map is relaxed
* and then turned into an equality, then resulting facet lies
* entirely inside the other basic map
* => the pair can be replaced by the basic map containing
* the inequality, with the inequality relaxed.
*
* 6. there is a single adjacent pair of an inequality and an equality,
* the other constraints of the basic map containing the inequality are
* "valid". Moreover, the facets corresponding to both
* the inequality and the equality can be wrapped around their
* ridges to include the other basic map
* => the pair can be replaced by a basic map consisting
* of the valid constraints in both basic maps together
* with all wrapping constraints
*
* 7. one of the basic maps extends beyond the other by at most one.
* Moreover, the facets corresponding to the cut constraints and
* the pieces of the other basic map at offset one from these cut
* constraints can be wrapped around their ridges to include
* the union of the two basic maps
* => the pair can be replaced by a basic map consisting
* of the valid constraints in both basic maps together
* with all wrapping constraints
*
* 8. the two basic maps live in adjacent hyperplanes. In principle
* such sets can always be combined through wrapping, but we impose
* that there is only one such pair, to avoid overeager coalescing.
*
* Throughout the computation, we maintain a collection of tableaus
* corresponding to the basic maps. When the basic maps are dropped
* or combined, the tableaus are modified accordingly.
*/
static enum isl_change coalesce_local_pair(int i, int j,
struct isl_coalesce_info *info)
{
enum isl_change change = isl_change_none;
info[i].eq = info[i].ineq = NULL;
info[j].eq = info[j].ineq = NULL;
info[i].eq = eq_status_in(info[i].bmap, info[j].tab);
if (info[i].bmap->n_eq && !info[i].eq)
goto error;
if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ERROR))
goto error;
if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_SEPARATE))
goto done;
info[j].eq = eq_status_in(info[j].bmap, info[i].tab);
if (info[j].bmap->n_eq && !info[j].eq)
goto error;
if (any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ERROR))
goto error;
if (any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_SEPARATE))
goto done;
info[i].ineq = ineq_status_in(info[i].bmap, info[i].tab, info[j].tab);
if (info[i].bmap->n_ineq && !info[i].ineq)
goto error;
if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ERROR))
goto error;
if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_SEPARATE))
goto done;
info[j].ineq = ineq_status_in(info[j].bmap, info[j].tab, info[i].tab);
if (info[j].bmap->n_ineq && !info[j].ineq)
goto error;
if (any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ERROR))
goto error;
if (any(info[j].ineq, info[j].bmap->n_ineq, STATUS_SEPARATE))
goto done;
if (all(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_VALID) &&
all(info[i].ineq, info[i].bmap->n_ineq, STATUS_VALID)) {
drop(&info[j]);
change = isl_change_drop_second;
} else if (all(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_VALID) &&
all(info[j].ineq, info[j].bmap->n_ineq, STATUS_VALID)) {
drop(&info[i]);
change = isl_change_drop_first;
} else if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_EQ)) {
change = check_eq_adj_eq(i, j, info);
} else if (any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ADJ_EQ)) {
change = check_eq_adj_eq(j, i, info);
} else if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_INEQ) ||
any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ADJ_INEQ)) {
change = check_adj_eq(i, j, info);
} else if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_EQ) ||
any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_EQ)) {
/* Can't happen */
/* BAD ADJ INEQ */
} else if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_INEQ) ||
any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_INEQ)) {
change = check_adj_ineq(i, j, info);
} else {
if (!any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT) &&
!any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_CUT))
change = check_facets(i, j, info);
if (change == isl_change_none)
change = check_wrap(i, j, info);
}
done:
free(info[i].eq);
free(info[j].eq);
free(info[i].ineq);
free(info[j].ineq);
return change;
error:
free(info[i].eq);
free(info[j].eq);
free(info[i].ineq);
free(info[j].ineq);
return isl_change_error;
}
/* Shift the integer division at position "div" of the basic map
* represented by "info" by "shift".
*
* That is, if the integer division has the form
*
* floor(f(x)/d)
*
* then replace it by
*
* floor((f(x) + shift * d)/d) - shift
*/
static int shift_div(struct isl_coalesce_info *info, int div, isl_int shift)
{
unsigned total;
info->bmap = isl_basic_map_shift_div(info->bmap, div, 0, shift);
if (!info->bmap)
return -1;
total = isl_basic_map_dim(info->bmap, isl_dim_all);
total -= isl_basic_map_dim(info->bmap, isl_dim_div);
if (isl_tab_shift_var(info->tab, total + div, shift) < 0)
return -1;
return 0;
}
/* Check if some of the divs in the basic map represented by "info1"
* are shifts of the corresponding divs in the basic map represented
* by "info2". If so, align them with those of "info2".
* Only do this if "info1" and "info2" have the same number
* of integer divisions.
*
* An integer division is considered to be a shift of another integer
* division if one is equal to the other plus a constant.
*
* In particular, for each pair of integer divisions, if both are known,
* have identical coefficients (apart from the constant term) and
* if the difference between the constant terms (taking into account
* the denominator) is an integer, then move the difference outside.
* That is, if one integer division is of the form
*
* floor((f(x) + c_1)/d)
*
* while the other is of the form
*
* floor((f(x) + c_2)/d)
*
* and n = (c_2 - c_1)/d is an integer, then replace the first
* integer division by
*
* floor((f(x) + c_1 + n * d)/d) - n = floor((f(x) + c_2)/d) - n
*/
static int harmonize_divs(struct isl_coalesce_info *info1,
struct isl_coalesce_info *info2)
{
int i;
int total;
if (!info1->bmap || !info2->bmap)
return -1;
if (info1->bmap->n_div != info2->bmap->n_div)
return 0;
if (info1->bmap->n_div == 0)
return 0;
total = isl_basic_map_total_dim(info1->bmap);
for (i = 0; i < info1->bmap->n_div; ++i) {
isl_int d;
int r = 0;
if (isl_int_is_zero(info1->bmap->div[i][0]) ||
isl_int_is_zero(info2->bmap->div[i][0]))
continue;
if (isl_int_ne(info1->bmap->div[i][0], info2->bmap->div[i][0]))
continue;
if (isl_int_eq(info1->bmap->div[i][1], info2->bmap->div[i][1]))
continue;
if (!isl_seq_eq(info1->bmap->div[i] + 2,
info2->bmap->div[i] + 2, total))
continue;
isl_int_init(d);
isl_int_sub(d, info2->bmap->div[i][1], info1->bmap->div[i][1]);
if (isl_int_is_divisible_by(d, info1->bmap->div[i][0])) {
isl_int_divexact(d, d, info1->bmap->div[i][0]);
r = shift_div(info1, i, d);
}
isl_int_clear(d);
if (r < 0)
return -1;
}
return 0;
}
/* Do the two basic maps live in the same local space, i.e.,
* do they have the same (known) divs?
* If either basic map has any unknown divs, then we can only assume
* that they do not live in the same local space.
*/
static int same_divs(__isl_keep isl_basic_map *bmap1,
__isl_keep isl_basic_map *bmap2)
{
int i;
int known;
int total;
if (!bmap1 || !bmap2)
return -1;
if (bmap1->n_div != bmap2->n_div)
return 0;
if (bmap1->n_div == 0)
return 1;
known = isl_basic_map_divs_known(bmap1);
if (known < 0 || !known)
return known;
known = isl_basic_map_divs_known(bmap2);
if (known < 0 || !known)
return known;
total = isl_basic_map_total_dim(bmap1);
for (i = 0; i < bmap1->n_div; ++i)
if (!isl_seq_eq(bmap1->div[i], bmap2->div[i], 2 + total))
return 0;
return 1;
}
/* Does "bmap" contain the basic map represented by the tableau "tab"
* after expanding the divs of "bmap" to match those of "tab"?
* The expansion is performed using the divs "div" and expansion "exp"
* computed by the caller.
* Then we check if all constraints of the expanded "bmap" are valid for "tab".
*/
static int contains_with_expanded_divs(__isl_keep isl_basic_map *bmap,
struct isl_tab *tab, __isl_keep isl_mat *div, int *exp)
{
int superset = 0;
int *eq_i = NULL;
int *ineq_i = NULL;
bmap = isl_basic_map_copy(bmap);
bmap = isl_basic_set_expand_divs(bmap, isl_mat_copy(div), exp);
if (!bmap)
goto error;
eq_i = eq_status_in(bmap, tab);
if (bmap->n_eq && !eq_i)
goto error;
if (any(eq_i, 2 * bmap->n_eq, STATUS_ERROR))
goto error;
if (any(eq_i, 2 * bmap->n_eq, STATUS_SEPARATE))
goto done;
ineq_i = ineq_status_in(bmap, NULL, tab);
if (bmap->n_ineq && !ineq_i)
goto error;
if (any(ineq_i, bmap->n_ineq, STATUS_ERROR))
goto error;
if (any(ineq_i, bmap->n_ineq, STATUS_SEPARATE))
goto done;
if (all(eq_i, 2 * bmap->n_eq, STATUS_VALID) &&
all(ineq_i, bmap->n_ineq, STATUS_VALID))
superset = 1;
done:
isl_basic_map_free(bmap);
free(eq_i);
free(ineq_i);
return superset;
error:
isl_basic_map_free(bmap);
free(eq_i);
free(ineq_i);
return -1;
}
/* Does "bmap_i" contain the basic map represented by "info_j"
* after aligning the divs of "bmap_i" to those of "info_j".
* Note that this can only succeed if the number of divs of "bmap_i"
* is smaller than (or equal to) the number of divs of "info_j".
*
* We first check if the divs of "bmap_i" are all known and form a subset
* of those of "bmap_j". If so, we pass control over to
* contains_with_expanded_divs.
*/
static int contains_after_aligning_divs(__isl_keep isl_basic_map *bmap_i,
struct isl_coalesce_info *info_j)
{
int known;
isl_mat *div_i, *div_j, *div;
int *exp1 = NULL;
int *exp2 = NULL;
isl_ctx *ctx;
int subset;
known = isl_basic_map_divs_known(bmap_i);
if (known < 0 || !known)
return known;
ctx = isl_basic_map_get_ctx(bmap_i);
div_i = isl_basic_map_get_divs(bmap_i);
div_j = isl_basic_map_get_divs(info_j->bmap);
if (!div_i || !div_j)
goto error;
exp1 = isl_alloc_array(ctx, int, div_i->n_row);
exp2 = isl_alloc_array(ctx, int, div_j->n_row);
if ((div_i->n_row && !exp1) || (div_j->n_row && !exp2))
goto error;
div = isl_merge_divs(div_i, div_j, exp1, exp2);
if (!div)
goto error;
if (div->n_row == div_j->n_row)
subset = contains_with_expanded_divs(bmap_i,
info_j->tab, div, exp1);
else
subset = 0;
isl_mat_free(div);
isl_mat_free(div_i);
isl_mat_free(div_j);
free(exp2);
free(exp1);
return subset;
error:
isl_mat_free(div_i);
isl_mat_free(div_j);
free(exp1);
free(exp2);
return -1;
}
/* Check if the basic map "j" is a subset of basic map "i",
* if "i" has fewer divs that "j".
* If so, remove basic map "j".
*
* If the two basic maps have the same number of divs, then
* they must necessarily be different. Otherwise, we would have
* called coalesce_local_pair. We therefore don't try anything
* in this case.
*/
static int coalesced_subset(int i, int j, struct isl_coalesce_info *info)
{
int superset;
if (info[i].bmap->n_div >= info[j].bmap->n_div)
return 0;
superset = contains_after_aligning_divs(info[i].bmap, &info[j]);
if (superset < 0)
return -1;
if (superset)
drop(&info[j]);
return superset;
}
/* Check if basic map "j" is a subset of basic map "i" after
* exploiting the extra equalities of "j" to simplify the divs of "i".
* If so, remove basic map "j".
*
* If "j" does not have any equalities or if they are the same
* as those of "i", then we cannot exploit them to simplify the divs.
* Similarly, if there are no divs in "i", then they cannot be simplified.
* If, on the other hand, the affine hulls of "i" and "j" do not intersect,
* then "j" cannot be a subset of "i".
*
* Otherwise, we intersect "i" with the affine hull of "j" and then
* check if "j" is a subset of the result after aligning the divs.
* If so, then "j" is definitely a subset of "i" and can be removed.
* Note that if after intersection with the affine hull of "j".
* "i" still has more divs than "j", then there is no way we can
* align the divs of "i" to those of "j".
*/
static int coalesced_subset_with_equalities(int i, int j,
struct isl_coalesce_info *info)
{
isl_basic_map *hull_i, *hull_j, *bmap_i;
int equal, empty, subset;
if (info[j].bmap->n_eq == 0)
return 0;
if (info[i].bmap->n_div == 0)
return 0;
hull_i = isl_basic_map_copy(info[i].bmap);
hull_i = isl_basic_map_plain_affine_hull(hull_i);
hull_j = isl_basic_map_copy(info[j].bmap);
hull_j = isl_basic_map_plain_affine_hull(hull_j);
hull_j = isl_basic_map_intersect(hull_j, isl_basic_map_copy(hull_i));
equal = isl_basic_map_plain_is_equal(hull_i, hull_j);
empty = isl_basic_map_plain_is_empty(hull_j);
isl_basic_map_free(hull_i);
if (equal < 0 || equal || empty < 0 || empty) {
isl_basic_map_free(hull_j);
return equal < 0 || empty < 0 ? -1 : 0;
}
bmap_i = isl_basic_map_copy(info[i].bmap);
bmap_i = isl_basic_map_intersect(bmap_i, hull_j);
if (!bmap_i)
return -1;
if (bmap_i->n_div > info[j].bmap->n_div) {
isl_basic_map_free(bmap_i);
return 0;
}
subset = contains_after_aligning_divs(bmap_i, &info[j]);
isl_basic_map_free(bmap_i);
if (subset < 0)
return -1;
if (subset)
drop(&info[j]);
return subset;
}
/* Check if one of the basic maps is a subset of the other and, if so,
* drop the subset.
* Note that we only perform any test if the number of divs is different
* in the two basic maps. In case the number of divs is the same,
* we have already established that the divs are different
* in the two basic maps.
* In particular, if the number of divs of basic map i is smaller than
* the number of divs of basic map j, then we check if j is a subset of i
* and vice versa.
*/
static enum isl_change check_coalesce_subset(int i, int j,
struct isl_coalesce_info *info)
{
int changed;
changed = coalesced_subset(i, j, info);
if (changed < 0 || changed)
return changed < 0 ? isl_change_error : isl_change_drop_second;
changed = coalesced_subset(j, i, info);
if (changed < 0 || changed)
return changed < 0 ? isl_change_error : isl_change_drop_first;
changed = coalesced_subset_with_equalities(i, j, info);
if (changed < 0 || changed)
return changed < 0 ? isl_change_error : isl_change_drop_second;
changed = coalesced_subset_with_equalities(j, i, info);
if (changed < 0 || changed)
return changed < 0 ? isl_change_error : isl_change_drop_first;
return isl_change_none;
}
/* Does "bmap" involve any divs that themselves refer to divs?
*/
static int has_nested_div(__isl_keep isl_basic_map *bmap)
{
int i;
unsigned total;
unsigned n_div;
total = isl_basic_map_dim(bmap, isl_dim_all);
n_div = isl_basic_map_dim(bmap, isl_dim_div);
total -= n_div;
for (i = 0; i < n_div; ++i)
if (isl_seq_first_non_zero(bmap->div[i] + 2 + total,
n_div) != -1)
return 1;
return 0;
}
/* Return a list of affine expressions, one for each integer division
* in "bmap_i". For each integer division that also appears in "bmap_j",
* the affine expression is set to NaN. The number of NaNs in the list
* is equal to the number of integer divisions in "bmap_j".
* For the other integer divisions of "bmap_i", the corresponding
* element in the list is a purely affine expression equal to the integer
* division in "hull".
* If no such list can be constructed, then the number of elements
* in the returned list is smaller than the number of integer divisions
* in "bmap_i".
*/
static __isl_give isl_aff_list *set_up_substitutions(
__isl_keep isl_basic_map *bmap_i, __isl_keep isl_basic_map *bmap_j,
__isl_take isl_basic_map *hull)
{
unsigned n_div_i, n_div_j, total;
isl_ctx *ctx;
isl_local_space *ls;
isl_basic_set *wrap_hull;
isl_aff *aff_nan;
isl_aff_list *list;
int i, j;
if (!hull)
return NULL;
ctx = isl_basic_map_get_ctx(hull);
n_div_i = isl_basic_map_dim(bmap_i, isl_dim_div);
n_div_j = isl_basic_map_dim(bmap_j, isl_dim_div);
total = isl_basic_map_total_dim(bmap_i) - n_div_i;
ls = isl_basic_map_get_local_space(bmap_i);
ls = isl_local_space_wrap(ls);
wrap_hull = isl_basic_map_wrap(hull);
aff_nan = isl_aff_nan_on_domain(isl_local_space_copy(ls));
list = isl_aff_list_alloc(ctx, n_div_i);
j = 0;
for (i = 0; i < n_div_i; ++i) {
isl_aff *aff;
if (j < n_div_j &&
isl_seq_eq(bmap_i->div[i], bmap_j->div[j], 2 + total)) {
++j;
list = isl_aff_list_add(list, isl_aff_copy(aff_nan));
continue;
}
if (n_div_i - i <= n_div_j - j)
break;
aff = isl_local_space_get_div(ls, i);
aff = isl_aff_substitute_equalities(aff,
isl_basic_set_copy(wrap_hull));
aff = isl_aff_floor(aff);
if (!aff)
goto error;
if (isl_aff_dim(aff, isl_dim_div) != 0) {
isl_aff_free(aff);
break;
}
list = isl_aff_list_add(list, aff);
}
isl_aff_free(aff_nan);
isl_local_space_free(ls);
isl_basic_set_free(wrap_hull);
return list;
error:
isl_aff_free(aff_nan);
isl_local_space_free(ls);
isl_basic_set_free(wrap_hull);
isl_aff_list_free(list);
return NULL;
}
/* Add variables to "tab" corresponding to the elements in "list"
* that are not set to NaN.
* "dim" is the offset in the variables of "tab" where we should
* start considering the elements in "list".
* When this function returns, the total number of variables in "tab"
* is equal to "dim" plus the number of elements in "list".
*/
static int add_sub_vars(struct isl_tab *tab, __isl_keep isl_aff_list *list,
int dim)
{
int i, n;
n = isl_aff_list_n_aff(list);
for (i = 0; i < n; ++i) {
int is_nan;
isl_aff *aff;
aff = isl_aff_list_get_aff(list, i);
is_nan = isl_aff_is_nan(aff);
isl_aff_free(aff);
if (is_nan < 0)
return -1;
if (!is_nan && isl_tab_insert_var(tab, dim + i) < 0)
return -1;
}
return 0;
}
/* For each element in "list" that is not set to NaN, fix the corresponding
* variable in "tab" to the purely affine expression defined by the element.
* "dim" is the offset in the variables of "tab" where we should
* start considering the elements in "list".
*/
static int add_sub_equalities(struct isl_tab *tab,
__isl_keep isl_aff_list *list, int dim)
{
int i, n;
isl_ctx *ctx;
isl_vec *sub;
isl_aff *aff;
n = isl_aff_list_n_aff(list);
ctx = isl_tab_get_ctx(tab);
sub = isl_vec_alloc(ctx, 1 + dim + n);
if (!sub)
return -1;
isl_seq_clr(sub->el + 1 + dim, n);
for (i = 0; i < n; ++i) {
aff = isl_aff_list_get_aff(list, i);
if (!aff)
goto error;
if (isl_aff_is_nan(aff)) {
isl_aff_free(aff);
continue;
}
isl_seq_cpy(sub->el, aff->v->el + 1, 1 + dim);
isl_int_neg(sub->el[1 + dim + i], aff->v->el[0]);
if (isl_tab_add_eq(tab, sub->el) < 0)
goto error;
isl_int_set_si(sub->el[1 + dim + i], 0);
isl_aff_free(aff);
}
isl_vec_free(sub);
return 0;
error:
isl_aff_free(aff);
isl_vec_free(sub);
return -1;
}
/* Add variables to info->tab corresponding to the elements in "list"
* that are not set to NaN. The value of the added variable
* is fixed to the purely affine expression defined by the element.
* "dim" is the offset in the variables of info->tab where we should
* start considering the elements in "list".
* When this function returns, the total number of variables in info->tab
* is equal to "dim" plus the number of elements in "list".
* Additionally, add the div constraints that have been added info->bmap
* after the tableau was constructed to info->tab. These constraints
* start at position "n_ineq" in info->bmap.
* The constraints need to be added to the tableau before
* the equalities assigning the purely affine expression
* because the position needs to match that in info->bmap.
* They are frozen because the corresponding added equality is a consequence
* of the two div constraints and the other equalities, meaning that
* the div constraints would otherwise get marked as redundant,
* while they are only redundant with respect to the extra equalities
* added to the tableau, which do not appear explicitly in the basic map.
*/
static int add_subs(struct isl_coalesce_info *info,
__isl_keep isl_aff_list *list, int dim, int n_ineq)
{
int i, extra_var, extra_con;
int n;
unsigned n_eq = info->bmap->n_eq;
if (!list)
return -1;
n = isl_aff_list_n_aff(list);
extra_var = n - (info->tab->n_var - dim);
extra_con = info->bmap->n_ineq - n_ineq;
if (isl_tab_extend_vars(info->tab, extra_var) < 0)
return -1;
if (isl_tab_extend_cons(info->tab, extra_con + 2 * extra_var) < 0)
return -1;
if (add_sub_vars(info->tab, list, dim) < 0)
return -1;
for (i = n_ineq; i < info->bmap->n_ineq; ++i) {
if (isl_tab_add_ineq(info->tab, info->bmap->ineq[i]) < 0)
return -1;
if (isl_tab_freeze_constraint(info->tab, n_eq + i) < 0)
return -1;
}
return add_sub_equalities(info->tab, list, dim);
}
/* Coalesce basic map "j" into basic map "i" after adding the extra integer
* divisions in "i" but not in "j" to basic map "j", with values
* specified by "list". The total number of elements in "list"
* is equal to the number of integer divisions in "i", while the number
* of NaN elements in the list is equal to the number of integer divisions
* in "j".
* Adding extra integer divisions to "j" through isl_basic_map_align_divs
* also adds the corresponding div constraints. These need to be added
* to the corresponding tableau as well in add_subs to maintain consistency.
*
* If no coalescing can be performed, then we need to revert basic map "j"
* to its original state. We do the same if basic map "i" gets dropped
* during the coalescing, even though this should not happen in practice
* since we have already checked for "j" being a subset of "i"
* before we reach this stage.
*/
static enum isl_change coalesce_with_subs(int i, int j,
struct isl_coalesce_info *info, __isl_keep isl_aff_list *list)
{
isl_basic_map *bmap_j;
struct isl_tab_undo *snap;
unsigned dim;
enum isl_change change;
int n_ineq;
bmap_j = isl_basic_map_copy(info[j].bmap);
n_ineq = info[j].bmap->n_ineq;
info[j].bmap = isl_basic_map_align_divs(info[j].bmap, info[i].bmap);
if (!info[j].bmap)
goto error;
snap = isl_tab_snap(info[j].tab);
dim = isl_basic_map_dim(bmap_j, isl_dim_all);
dim -= isl_basic_map_dim(bmap_j, isl_dim_div);
if (add_subs(&info[j], list, dim, n_ineq) < 0)
goto error;
change = coalesce_local_pair(i, j, info);
if (change != isl_change_none && change != isl_change_drop_first) {
isl_basic_map_free(bmap_j);
} else {
isl_basic_map_free(info[j].bmap);
info[j].bmap = bmap_j;
if (isl_tab_rollback(info[j].tab, snap) < 0)
return isl_change_error;
}
return change;
error:
isl_basic_map_free(bmap_j);
return isl_change_error;
}
/* Check if we can coalesce basic map "j" into basic map "i" after copying
* those extra integer divisions in "i" that can be simplified away
* using the extra equalities in "j".
* All divs are assumed to be known and not contain any nested divs.
*
* We first check if there are any extra equalities in "j" that we
* can exploit. Then we check if every integer division in "i"
* either already appears in "j" or can be simplified using the
* extra equalities to a purely affine expression.
* If these tests succeed, then we try to coalesce the two basic maps
* by introducing extra dimensions in "j" corresponding to
* the extra integer divsisions "i" fixed to the corresponding
* purely affine expression.
*/
static enum isl_change check_coalesce_into_eq(int i, int j,
struct isl_coalesce_info *info)
{
unsigned n_div_i, n_div_j;
isl_basic_map *hull_i, *hull_j;
int equal, empty;
isl_aff_list *list;
enum isl_change change;
n_div_i = isl_basic_map_dim(info[i].bmap, isl_dim_div);
n_div_j = isl_basic_map_dim(info[j].bmap, isl_dim_div);
if (n_div_i <= n_div_j)
return isl_change_none;
if (info[j].bmap->n_eq == 0)
return isl_change_none;
hull_i = isl_basic_map_copy(info[i].bmap);
hull_i = isl_basic_map_plain_affine_hull(hull_i);
hull_j = isl_basic_map_copy(info[j].bmap);
hull_j = isl_basic_map_plain_affine_hull(hull_j);
hull_j = isl_basic_map_intersect(hull_j, isl_basic_map_copy(hull_i));
equal = isl_basic_map_plain_is_equal(hull_i, hull_j);
empty = isl_basic_map_plain_is_empty(hull_j);
isl_basic_map_free(hull_i);
if (equal < 0 || empty < 0)
goto error;
if (equal || empty) {
isl_basic_map_free(hull_j);
return isl_change_none;
}
list = set_up_substitutions(info[i].bmap, info[j].bmap, hull_j);
if (!list)
return isl_change_error;
if (isl_aff_list_n_aff(list) < n_div_i)
change = isl_change_none;
else
change = coalesce_with_subs(i, j, info, list);
isl_aff_list_free(list);
return change;
error:
isl_basic_map_free(hull_j);
return isl_change_error;
}
/* Check if we can coalesce basic maps "i" and "j" after copying
* those extra integer divisions in one of the basic maps that can
* be simplified away using the extra equalities in the other basic map.
* We require all divs to be known in both basic maps.
* Furthermore, to simplify the comparison of div expressions,
* we do not allow any nested integer divisions.
*/
static enum isl_change check_coalesce_eq(int i, int j,
struct isl_coalesce_info *info)
{
int known, nested;
enum isl_change change;
known = isl_basic_map_divs_known(info[i].bmap);
if (known < 0 || !known)
return known < 0 ? isl_change_error : isl_change_none;
known = isl_basic_map_divs_known(info[j].bmap);
if (known < 0 || !known)
return known < 0 ? isl_change_error : isl_change_none;
nested = has_nested_div(info[i].bmap);
if (nested < 0 || nested)
return nested < 0 ? isl_change_error : isl_change_none;
nested = has_nested_div(info[j].bmap);
if (nested < 0 || nested)
return nested < 0 ? isl_change_error : isl_change_none;
change = check_coalesce_into_eq(i, j, info);
if (change != isl_change_none)
return change;
change = check_coalesce_into_eq(j, i, info);
if (change != isl_change_none)
return invert_change(change);
return isl_change_none;
}
/* Check if the union of the given pair of basic maps
* can be represented by a single basic map.
* If so, replace the pair by the single basic map and return
* isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
* Otherwise, return isl_change_none.
*
* We first check if the two basic maps live in the same local space,
* after aligning the divs that differ by only an integer constant.
* If so, we do the complete check. Otherwise, we check if they have
* the same number of integer divisions and can be coalesced, if one is
* an obvious subset of the other or if the extra integer divisions
* of one basic map can be simplified away using the extra equalities
* of the other basic map.
*/
static enum isl_change coalesce_pair(int i, int j,
struct isl_coalesce_info *info)
{
int same;
enum isl_change change;
if (harmonize_divs(&info[i], &info[j]) < 0)
return isl_change_error;
same = same_divs(info[i].bmap, info[j].bmap);
if (same < 0)
return isl_change_error;
if (same)
return coalesce_local_pair(i, j, info);
if (info[i].bmap->n_div == info[j].bmap->n_div) {
change = coalesce_local_pair(i, j, info);
if (change != isl_change_none)
return change;
}
change = check_coalesce_subset(i, j, info);
if (change != isl_change_none)
return change;
return check_coalesce_eq(i, j, info);
}
/* Return the maximum of "a" and "b".
*/
static int isl_max(int a, int b)
{
return a > b ? a : b;
}
/* Pairwise coalesce the basic maps in the range [start1, end1[ of "info"
* with those in the range [start2, end2[, skipping basic maps
* that have been removed (either before or within this function).
*
* For each basic map i in the first range, we check if it can be coalesced
* with respect to any previously considered basic map j in the second range.
* If i gets dropped (because it was a subset of some j), then
* we can move on to the next basic map.
* If j gets dropped, we need to continue checking against the other
* previously considered basic maps.
* If the two basic maps got fused, then we recheck the fused basic map
* against the previously considered basic maps, starting at i + 1
* (even if start2 is greater than i + 1).
*/
static int coalesce_range(isl_ctx *ctx, struct isl_coalesce_info *info,
int start1, int end1, int start2, int end2)
{
int i, j;
for (i = end1 - 1; i >= start1; --i) {
if (info[i].removed)
continue;
for (j = isl_max(i + 1, start2); j < end2; ++j) {
enum isl_change changed;
if (info[j].removed)
continue;
if (info[i].removed)
isl_die(ctx, isl_error_internal,
"basic map unexpectedly removed",
return -1);
changed = coalesce_pair(i, j, info);
switch (changed) {
case isl_change_error:
return -1;
case isl_change_none:
case isl_change_drop_second:
continue;
case isl_change_drop_first:
j = end2;
break;
case isl_change_fuse:
j = i;
break;
}
}
}
return 0;
}
/* Pairwise coalesce the basic maps described by the "n" elements of "info".
*
* We consider groups of basic maps that live in the same apparent
* affine hull and we first coalesce within such a group before we
* coalesce the elements in the group with elements of previously
* considered groups. If a fuse happens during the second phase,
* then we also reconsider the elements within the group.
*/
static int coalesce(isl_ctx *ctx, int n, struct isl_coalesce_info *info)
{
int start, end;
for (end = n; end > 0; end = start) {
start = end - 1;
while (start >= 1 &&
info[start - 1].hull_hash == info[start].hull_hash)
start--;
if (coalesce_range(ctx, info, start, end, start, end) < 0)
return -1;
if (coalesce_range(ctx, info, start, end, end, n) < 0)
return -1;
}
return 0;
}
/* Update the basic maps in "map" based on the information in "info".
* In particular, remove the basic maps that have been marked removed and
* update the others based on the information in the corresponding tableau.
* Since we detected implicit equalities without calling
* isl_basic_map_gauss, we need to do it now.
* Also call isl_basic_map_simplify if we may have lost the definition
* of one or more integer divisions.
*/
static __isl_give isl_map *update_basic_maps(__isl_take isl_map *map,
int n, struct isl_coalesce_info *info)
{
int i;
if (!map)
return NULL;
for (i = n - 1; i >= 0; --i) {
if (info[i].removed) {
isl_basic_map_free(map->p[i]);
if (i != map->n - 1)
map->p[i] = map->p[map->n - 1];
map->n--;
continue;
}
info[i].bmap = isl_basic_map_update_from_tab(info[i].bmap,
info[i].tab);
info[i].bmap = isl_basic_map_gauss(info[i].bmap, NULL);
if (info[i].simplify)
info[i].bmap = isl_basic_map_simplify(info[i].bmap);
info[i].bmap = isl_basic_map_finalize(info[i].bmap);
if (!info[i].bmap)
return isl_map_free(map);
ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_NO_IMPLICIT);
ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_NO_REDUNDANT);
isl_basic_map_free(map->p[i]);
map->p[i] = info[i].bmap;
info[i].bmap = NULL;
}
return map;
}
/* For each pair of basic maps in the map, check if the union of the two
* can be represented by a single basic map.
* If so, replace the pair by the single basic map and start over.
*
* We factor out any (hidden) common factor from the constraint
* coefficients to improve the detection of adjacent constraints.
*
* Since we are constructing the tableaus of the basic maps anyway,
* we exploit them to detect implicit equalities and redundant constraints.
* This also helps the coalescing as it can ignore the redundant constraints.
* In order to avoid confusion, we make all implicit equalities explicit
* in the basic maps. We don't call isl_basic_map_gauss, though,
* as that may affect the number of constraints.
* This means that we have to call isl_basic_map_gauss at the end
* of the computation (in update_basic_maps) to ensure that
* the basic maps are not left in an unexpected state.
* For each basic map, we also compute the hash of the apparent affine hull
* for use in coalesce.
*/
struct isl_map *isl_map_coalesce(struct isl_map *map)
{
int i;
unsigned n;
isl_ctx *ctx;
struct isl_coalesce_info *info = NULL;
map = isl_map_remove_empty_parts(map);
if (!map)
return NULL;
if (map->n <= 1)
return map;
ctx = isl_map_get_ctx(map);
map = isl_map_sort_divs(map);
map = isl_map_cow(map);
if (!map)
return NULL;
n = map->n;
info = isl_calloc_array(map->ctx, struct isl_coalesce_info, n);
if (!info)
goto error;
for (i = 0; i < map->n; ++i) {
map->p[i] = isl_basic_map_reduce_coefficients(map->p[i]);
if (!map->p[i])
goto error;
info[i].bmap = isl_basic_map_copy(map->p[i]);
info[i].tab = isl_tab_from_basic_map(info[i].bmap, 0);
if (!info[i].tab)
goto error;
if (!ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_NO_IMPLICIT))
if (isl_tab_detect_implicit_equalities(info[i].tab) < 0)
goto error;
info[i].bmap = isl_tab_make_equalities_explicit(info[i].tab,
info[i].bmap);
if (!info[i].bmap)
goto error;
if (!ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_NO_REDUNDANT))
if (isl_tab_detect_redundant(info[i].tab) < 0)
goto error;
if (coalesce_info_set_hull_hash(&info[i]) < 0)
goto error;
}
for (i = map->n - 1; i >= 0; --i)
if (info[i].tab->empty)
drop(&info[i]);
if (coalesce(ctx, n, info) < 0)
goto error;
map = update_basic_maps(map, n, info);
clear_coalesce_info(n, info);
return map;
error:
clear_coalesce_info(n, info);
isl_map_free(map);
return NULL;
}
/* For each pair of basic sets in the set, check if the union of the two
* can be represented by a single basic set.
* If so, replace the pair by the single basic set and start over.
*/
struct isl_set *isl_set_coalesce(struct isl_set *set)
{
return (struct isl_set *)isl_map_coalesce((struct isl_map *)set);
}
|