isl_ast_build.c 68.6 KB
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
/*
 * Copyright 2012-2013 Ecole Normale Superieure
 * Copyright 2014      INRIA Rocquencourt
 *
 * Use of this software is governed by the MIT license
 *
 * Written by Sven Verdoolaege,
 * 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/id.h>
#include <isl/val.h>
#include <isl/space.h>
#include <isl/map.h>
#include <isl/aff.h>
#include <isl/constraint.h>
#include <isl/map.h>
#include <isl/union_set.h>
#include <isl/union_map.h>
#include <isl_ast_build_private.h>
#include <isl_ast_private.h>
#include <isl_config.h>

/* Construct a map that isolates the current dimension.
 *
 * Essentially, the current dimension of "set" is moved to the single output
 * dimension in the result, with the current dimension in the domain replaced
 * by an unconstrained variable.
 */
__isl_give isl_map *isl_ast_build_map_to_iterator(
	__isl_keep isl_ast_build *build, __isl_take isl_set *set)
{
	isl_map *map;

	map = isl_map_from_domain(set);
	map = isl_map_add_dims(map, isl_dim_out, 1);

	if (!build)
		return isl_map_free(map);

	map = isl_map_equate(map, isl_dim_in, build->depth, isl_dim_out, 0);
	map = isl_map_eliminate(map, isl_dim_in, build->depth, 1);

	return map;
}

/* Initialize the information derived during the AST generation to default
 * values for a schedule domain in "space".
 *
 * We also check that the remaining fields are not NULL so that
 * the calling functions don't have to perform this test.
 */
static __isl_give isl_ast_build *isl_ast_build_init_derived(
	__isl_take isl_ast_build *build, __isl_take isl_space *space)
{
	isl_ctx *ctx;
	isl_vec *strides;
	isl_size dim;

	build = isl_ast_build_cow(build);
	if (!build || !build->domain)
		goto error;

	ctx = isl_ast_build_get_ctx(build);
	dim = isl_space_dim(space, isl_dim_set);
	if (dim < 0)
		goto error;
	strides = isl_vec_alloc(ctx, dim);
	strides = isl_vec_set_si(strides, 1);

	isl_vec_free(build->strides);
	build->strides = strides;

	space = isl_space_map_from_set(space);
	isl_multi_aff_free(build->offsets);
	build->offsets = isl_multi_aff_zero(isl_space_copy(space));
	isl_multi_aff_free(build->values);
	build->values = isl_multi_aff_identity(isl_space_copy(space));
	isl_multi_aff_free(build->internal2input);
	build->internal2input = isl_multi_aff_identity(space);

	if (!build->iterators || !build->domain || !build->generated ||
	    !build->pending || !build->values || !build->internal2input ||
	    !build->strides || !build->offsets || !build->options)
		return isl_ast_build_free(build);

	return build;
error:
	isl_space_free(space);
	return isl_ast_build_free(build);
}

/* Return an isl_id called "c%d", with "%d" set to "i".
 * If an isl_id with such a name already appears among the parameters
 * in build->domain, then adjust the name to "c%d_%d".
 */
static __isl_give isl_id *generate_name(isl_ctx *ctx, int i,
	__isl_keep isl_ast_build *build)
{
	int j;
	char name[23];
	isl_set *dom = build->domain;

	snprintf(name, sizeof(name), "c%d", i);
	j = 0;
	while (isl_set_find_dim_by_name(dom, isl_dim_param, name) >= 0)
		snprintf(name, sizeof(name), "c%d_%d", i, j++);
	return isl_id_alloc(ctx, name, NULL);
}

/* Create an isl_ast_build with "set" as domain.
 *
 * The input set is usually a parameter domain, but we currently allow it to
 * be any kind of set.  We set the domain of the returned isl_ast_build
 * to "set" and initialize all the other fields to default values.
 */
__isl_give isl_ast_build *isl_ast_build_from_context(__isl_take isl_set *set)
{
	int i;
	isl_size n;
	isl_ctx *ctx;
	isl_space *space;
	isl_ast_build *build;

	set = isl_set_compute_divs(set);
	n = isl_set_dim(set, isl_dim_set);
	if (n < 0)
		goto error;

	ctx = isl_set_get_ctx(set);

	build = isl_calloc_type(ctx, isl_ast_build);
	if (!build)
		goto error;

	build->ref = 1;
	build->domain = set;
	build->generated = isl_set_copy(build->domain);
	build->pending = isl_set_universe(isl_set_get_space(build->domain));
	build->options = isl_union_map_empty(isl_space_params_alloc(ctx, 0));
	build->depth = n;
	build->iterators = isl_id_list_alloc(ctx, n);
	for (i = 0; i < n; ++i) {
		isl_id *id;
		if (isl_set_has_dim_id(set, isl_dim_set, i))
			id = isl_set_get_dim_id(set, isl_dim_set, i);
		else
			id = generate_name(ctx, i, build);
		build->iterators = isl_id_list_add(build->iterators, id);
	}
	space = isl_set_get_space(set);
	if (isl_space_is_params(space))
		space = isl_space_set_from_params(space);

	return isl_ast_build_init_derived(build, space);
error:
	isl_set_free(set);
	return NULL;
}

/* Create an isl_ast_build with a universe (parametric) context.
 */
__isl_give isl_ast_build *isl_ast_build_alloc(isl_ctx *ctx)
{
	isl_space *space;
	isl_set *context;

	space = isl_space_params_alloc(ctx, 0);
	context = isl_set_universe(space);

	return isl_ast_build_from_context(context);
}

__isl_give isl_ast_build *isl_ast_build_copy(__isl_keep isl_ast_build *build)
{
	if (!build)
		return NULL;

	build->ref++;
	return build;
}

__isl_give isl_ast_build *isl_ast_build_dup(__isl_keep isl_ast_build *build)
{
	isl_ctx *ctx;
	isl_ast_build *dup;

	if (!build)
		return NULL;

	ctx = isl_ast_build_get_ctx(build);
	dup = isl_calloc_type(ctx, isl_ast_build);
	if (!dup)
		return NULL;

	dup->ref = 1;
	dup->outer_pos = build->outer_pos;
	dup->depth = build->depth;
	dup->iterators = isl_id_list_copy(build->iterators);
	dup->domain = isl_set_copy(build->domain);
	dup->generated = isl_set_copy(build->generated);
	dup->pending = isl_set_copy(build->pending);
	dup->values = isl_multi_aff_copy(build->values);
	dup->internal2input = isl_multi_aff_copy(build->internal2input);
	dup->value = isl_pw_aff_copy(build->value);
	dup->strides = isl_vec_copy(build->strides);
	dup->offsets = isl_multi_aff_copy(build->offsets);
	dup->executed = isl_union_map_copy(build->executed);
	dup->single_valued = build->single_valued;
	dup->options = isl_union_map_copy(build->options);
	dup->at_each_domain = build->at_each_domain;
	dup->at_each_domain_user = build->at_each_domain_user;
	dup->before_each_for = build->before_each_for;
	dup->before_each_for_user = build->before_each_for_user;
	dup->after_each_for = build->after_each_for;
	dup->after_each_for_user = build->after_each_for_user;
	dup->before_each_mark = build->before_each_mark;
	dup->before_each_mark_user = build->before_each_mark_user;
	dup->after_each_mark = build->after_each_mark;
	dup->after_each_mark_user = build->after_each_mark_user;
	dup->create_leaf = build->create_leaf;
	dup->create_leaf_user = build->create_leaf_user;
	dup->node = isl_schedule_node_copy(build->node);
	if (build->loop_type) {
		int i;

		dup->n = build->n;
		dup->loop_type = isl_alloc_array(ctx,
						enum isl_ast_loop_type, dup->n);
		if (dup->n && !dup->loop_type)
			return isl_ast_build_free(dup);
		for (i = 0; i < dup->n; ++i)
			dup->loop_type[i] = build->loop_type[i];
	}

	if (!dup->iterators || !dup->domain || !dup->generated ||
	    !dup->pending || !dup->values ||
	    !dup->strides || !dup->offsets || !dup->options ||
	    (build->internal2input && !dup->internal2input) ||
	    (build->executed && !dup->executed) ||
	    (build->value && !dup->value) ||
	    (build->node && !dup->node))
		return isl_ast_build_free(dup);

	return dup;
}

/* Align the parameters of "build" to those of "model", introducing
 * additional parameters if needed.
 */
__isl_give isl_ast_build *isl_ast_build_align_params(
	__isl_take isl_ast_build *build, __isl_take isl_space *model)
{
	build = isl_ast_build_cow(build);
	if (!build)
		goto error;

	build->domain = isl_set_align_params(build->domain,
						isl_space_copy(model));
	build->generated = isl_set_align_params(build->generated,
						isl_space_copy(model));
	build->pending = isl_set_align_params(build->pending,
						isl_space_copy(model));
	build->values = isl_multi_aff_align_params(build->values,
						isl_space_copy(model));
	build->offsets = isl_multi_aff_align_params(build->offsets,
						isl_space_copy(model));
	build->options = isl_union_map_align_params(build->options,
						isl_space_copy(model));
	if (build->internal2input) {
		build->internal2input =
			isl_multi_aff_align_params(build->internal2input,
						model);
		if (!build->internal2input)
			return isl_ast_build_free(build);
	} else {
		isl_space_free(model);
	}

	if (!build->domain || !build->values || !build->offsets ||
	    !build->options)
		return isl_ast_build_free(build);

	return build;
error:
	isl_space_free(model);
	return NULL;
}

__isl_give isl_ast_build *isl_ast_build_cow(__isl_take isl_ast_build *build)
{
	if (!build)
		return NULL;

	if (build->ref == 1)
		return build;
	build->ref--;
	return isl_ast_build_dup(build);
}

__isl_null isl_ast_build *isl_ast_build_free(
	__isl_take isl_ast_build *build)
{
	if (!build)
		return NULL;

	if (--build->ref > 0)
		return NULL;

	isl_id_list_free(build->iterators);
	isl_set_free(build->domain);
	isl_set_free(build->generated);
	isl_set_free(build->pending);
	isl_multi_aff_free(build->values);
	isl_multi_aff_free(build->internal2input);
	isl_pw_aff_free(build->value);
	isl_vec_free(build->strides);
	isl_multi_aff_free(build->offsets);
	isl_multi_aff_free(build->schedule_map);
	isl_union_map_free(build->executed);
	isl_union_map_free(build->options);
	isl_schedule_node_free(build->node);
	free(build->loop_type);
	isl_set_free(build->isolated);

	free(build);

	return NULL;
}

isl_ctx *isl_ast_build_get_ctx(__isl_keep isl_ast_build *build)
{
	return build ? isl_set_get_ctx(build->domain) : NULL;
}

/* Replace build->options by "options".
 */
__isl_give isl_ast_build *isl_ast_build_set_options(
	__isl_take isl_ast_build *build, __isl_take isl_union_map *options)
{
	build = isl_ast_build_cow(build);

	if (!build || !options)
		goto error;

	isl_union_map_free(build->options);
	build->options = options;

	return build;
error:
	isl_union_map_free(options);
	return isl_ast_build_free(build);
}

/* Set the iterators for the next code generation.
 *
 * If we still have some iterators left from the previous code generation
 * (if any) or if iterators have already been set by a previous
 * call to this function, then we remove them first.
 */
__isl_give isl_ast_build *isl_ast_build_set_iterators(
	__isl_take isl_ast_build *build, __isl_take isl_id_list *iterators)
{
	isl_size dim, n_it;

	build = isl_ast_build_cow(build);
	if (!build)
		goto error;

	dim = isl_ast_build_dim(build, isl_dim_set);
	n_it = isl_id_list_n_id(build->iterators);
	if (dim < 0 || n_it < 0)
		goto error;
	if (n_it < dim)
		isl_die(isl_ast_build_get_ctx(build), isl_error_internal,
			"isl_ast_build in inconsistent state", goto error);
	if (n_it > dim)
		build->iterators = isl_id_list_drop(build->iterators,
							dim, n_it - dim);
	build->iterators = isl_id_list_concat(build->iterators, iterators);
	if (!build->iterators)
		return isl_ast_build_free(build);

	return build;
error:
	isl_id_list_free(iterators);
	return isl_ast_build_free(build);
}

/* Set the "at_each_domain" callback of "build" to "fn".
 */
__isl_give isl_ast_build *isl_ast_build_set_at_each_domain(
	__isl_take isl_ast_build *build,
	__isl_give isl_ast_node *(*fn)(__isl_take isl_ast_node *node,
		__isl_keep isl_ast_build *build, void *user), void *user)
{
	build = isl_ast_build_cow(build);

	if (!build)
		return NULL;

	build->at_each_domain = fn;
	build->at_each_domain_user = user;

	return build;
}

/* Set the "before_each_for" callback of "build" to "fn".
 */
__isl_give isl_ast_build *isl_ast_build_set_before_each_for(
	__isl_take isl_ast_build *build,
	__isl_give isl_id *(*fn)(__isl_keep isl_ast_build *build,
		void *user), void *user)
{
	build = isl_ast_build_cow(build);

	if (!build)
		return NULL;

	build->before_each_for = fn;
	build->before_each_for_user = user;

	return build;
}

/* Set the "after_each_for" callback of "build" to "fn".
 */
__isl_give isl_ast_build *isl_ast_build_set_after_each_for(
	__isl_take isl_ast_build *build,
	__isl_give isl_ast_node *(*fn)(__isl_take isl_ast_node *node,
		__isl_keep isl_ast_build *build, void *user), void *user)
{
	build = isl_ast_build_cow(build);

	if (!build)
		return NULL;

	build->after_each_for = fn;
	build->after_each_for_user = user;

	return build;
}

/* Set the "before_each_mark" callback of "build" to "fn".
 */
__isl_give isl_ast_build *isl_ast_build_set_before_each_mark(
	__isl_take isl_ast_build *build,
	isl_stat (*fn)(__isl_keep isl_id *mark, __isl_keep isl_ast_build *build,
		void *user), void *user)
{
	build = isl_ast_build_cow(build);

	if (!build)
		return NULL;

	build->before_each_mark = fn;
	build->before_each_mark_user = user;

	return build;
}

/* Set the "after_each_mark" callback of "build" to "fn".
 */
__isl_give isl_ast_build *isl_ast_build_set_after_each_mark(
	__isl_take isl_ast_build *build,
	__isl_give isl_ast_node *(*fn)(__isl_take isl_ast_node *node,
		__isl_keep isl_ast_build *build, void *user), void *user)
{
	build = isl_ast_build_cow(build);

	if (!build)
		return NULL;

	build->after_each_mark = fn;
	build->after_each_mark_user = user;

	return build;
}

/* Set the "create_leaf" callback of "build" to "fn".
 */
__isl_give isl_ast_build *isl_ast_build_set_create_leaf(
	__isl_take isl_ast_build *build,
	__isl_give isl_ast_node *(*fn)(__isl_take isl_ast_build *build,
		void *user), void *user)
{
	build = isl_ast_build_cow(build);

	if (!build)
		return NULL;

	build->create_leaf = fn;
	build->create_leaf_user = user;

	return build;
}

/* Clear all information that is specific to this code generation
 * and that is (probably) not meaningful to any nested code generation.
 */
__isl_give isl_ast_build *isl_ast_build_clear_local_info(
	__isl_take isl_ast_build *build)
{
	isl_space *space;

	build = isl_ast_build_cow(build);
	if (!build)
		return NULL;

	space = isl_union_map_get_space(build->options);
	isl_union_map_free(build->options);
	build->options = isl_union_map_empty(space);

	build->at_each_domain = NULL;
	build->at_each_domain_user = NULL;
	build->before_each_for = NULL;
	build->before_each_for_user = NULL;
	build->after_each_for = NULL;
	build->after_each_for_user = NULL;
	build->before_each_mark = NULL;
	build->before_each_mark_user = NULL;
	build->after_each_mark = NULL;
	build->after_each_mark_user = NULL;
	build->create_leaf = NULL;
	build->create_leaf_user = NULL;

	if (!build->options)
		return isl_ast_build_free(build);

	return build;
}

/* Have any loops been eliminated?
 * That is, do any of the original schedule dimensions have a fixed
 * value that has been substituted?
 */
static int any_eliminated(isl_ast_build *build)
{
	int i;

	for (i = 0; i < build->depth; ++i)
		if (isl_ast_build_has_affine_value(build, i))
			return 1;

	return 0;
}

/* Clear build->schedule_map.
 * This function should be called whenever anything that might affect
 * the result of isl_ast_build_get_schedule_map_multi_aff changes.
 * In particular, it should be called when the depth is changed or
 * when an iterator is determined to have a fixed value.
 */
static void isl_ast_build_reset_schedule_map(__isl_keep isl_ast_build *build)
{
	if (!build)
		return;
	isl_multi_aff_free(build->schedule_map);
	build->schedule_map = NULL;
}

/* Do we need a (non-trivial) schedule map?
 * That is, is the internal schedule space different from
 * the external schedule space?
 *
 * The internal and external schedule spaces are only the same
 * if code has been generated for the entire schedule and if none
 * of the loops have been eliminated.
 */
isl_bool isl_ast_build_need_schedule_map(__isl_keep isl_ast_build *build)
{
	isl_size dim;

	dim = isl_ast_build_dim(build, isl_dim_set);
	if (dim < 0)
		return isl_bool_error;
	return isl_bool_ok(build->depth != dim || any_eliminated(build));
}

/* Return a mapping from the internal schedule space to the external
 * schedule space in the form of an isl_multi_aff.
 * The internal schedule space originally corresponds to that of the
 * input schedule.  This may change during the code generation if
 * if isl_ast_build_insert_dim is ever called.
 * The external schedule space corresponds to the
 * loops that have been generated.
 *
 * Currently, the only difference between the internal schedule domain
 * and the external schedule domain is that some dimensions are projected
 * out in the external schedule domain.  In particular, the dimensions
 * for which no code has been generated yet and the dimensions that correspond
 * to eliminated loops.
 *
 * We cache a copy of the schedule_map in build->schedule_map.
 * The cache is cleared through isl_ast_build_reset_schedule_map
 * whenever anything changes that might affect the result of this function.
 */
__isl_give isl_multi_aff *isl_ast_build_get_schedule_map_multi_aff(
	__isl_keep isl_ast_build *build)
{
	isl_bool needs_map;
	isl_space *space;
	isl_multi_aff *ma;

	if (!build)
		return NULL;
	if (build->schedule_map)
		return isl_multi_aff_copy(build->schedule_map);
	needs_map = isl_ast_build_need_schedule_map(build);
	if (needs_map < 0)
		return NULL;

	space = isl_ast_build_get_space(build, 1);
	space = isl_space_map_from_set(space);
	ma = isl_multi_aff_identity(space);
	if (needs_map) {
		int i;
		isl_size dim = isl_ast_build_dim(build, isl_dim_set);

		if (dim < 0)
			ma = isl_multi_aff_free(ma);
		ma = isl_multi_aff_drop_dims(ma, isl_dim_out,
					build->depth, dim - build->depth);
		for (i = build->depth - 1; i >= 0; --i)
			if (isl_ast_build_has_affine_value(build, i))
				ma = isl_multi_aff_drop_dims(ma,
							isl_dim_out, i, 1);
	}

	build->schedule_map = ma;
	return isl_multi_aff_copy(build->schedule_map);
}

/* Return a mapping from the internal schedule space to the external
 * schedule space in the form of an isl_map.
 */
__isl_give isl_map *isl_ast_build_get_schedule_map(
	__isl_keep isl_ast_build *build)
{
	isl_multi_aff *ma;

	ma = isl_ast_build_get_schedule_map_multi_aff(build);
	return isl_map_from_multi_aff(ma);
}

/* Return the position of the dimension in build->domain for which
 * an AST node is currently being generated.
 */
int isl_ast_build_get_depth(__isl_keep isl_ast_build *build)
{
	return build ? build->depth : -1;
}

/* Prepare for generating code for the next level.
 * In particular, increase the depth and reset any information
 * that is local to the current depth.
 */
__isl_give isl_ast_build *isl_ast_build_increase_depth(
	__isl_take isl_ast_build *build)
{
	build = isl_ast_build_cow(build);
	if (!build)
		return NULL;
	build->depth++;
	isl_ast_build_reset_schedule_map(build);
	build->value = isl_pw_aff_free(build->value);
	return build;
}

void isl_ast_build_dump(__isl_keep isl_ast_build *build)
{
	if (!build)
		return;

	fprintf(stderr, "domain: ");
	isl_set_dump(build->domain);
	fprintf(stderr, "generated: ");
	isl_set_dump(build->generated);
	fprintf(stderr, "pending: ");
	isl_set_dump(build->pending);
	fprintf(stderr, "iterators: ");
	isl_id_list_dump(build->iterators);
	fprintf(stderr, "values: ");
	isl_multi_aff_dump(build->values);
	if (build->value) {
		fprintf(stderr, "value: ");
		isl_pw_aff_dump(build->value);
	}
	fprintf(stderr, "strides: ");
	isl_vec_dump(build->strides);
	fprintf(stderr, "offsets: ");
	isl_multi_aff_dump(build->offsets);
	fprintf(stderr, "internal2input: ");
	isl_multi_aff_dump(build->internal2input);
}

/* Initialize "build" for AST construction in schedule space "space"
 * in the case that build->domain is a parameter set.
 *
 * build->iterators is assumed to have been updated already.
 */
static __isl_give isl_ast_build *isl_ast_build_init(
	__isl_take isl_ast_build *build, __isl_take isl_space *space)
{
	isl_set *set;

	build = isl_ast_build_cow(build);
	if (!build)
		goto error;

	set = isl_set_universe(isl_space_copy(space));
	build->domain = isl_set_intersect_params(isl_set_copy(set),
						    build->domain);
	build->pending = isl_set_intersect_params(isl_set_copy(set),
						    build->pending);
	build->generated = isl_set_intersect_params(set, build->generated);

	return isl_ast_build_init_derived(build, space);
error:
	isl_ast_build_free(build);
	isl_space_free(space);
	return NULL;
}

/* Assign "aff" to *user and return -1, effectively extracting
 * the first (and presumably only) affine expression in the isl_pw_aff
 * on which this function is used.
 */
static isl_stat extract_single_piece(__isl_take isl_set *set,
	__isl_take isl_aff *aff, void *user)
{
	isl_aff **p = user;

	*p = aff;
	isl_set_free(set);

	return isl_stat_error;
}

/* Intersect "set" with the stride constraint of "build", if any.
 */
static __isl_give isl_set *intersect_stride_constraint(__isl_take isl_set *set,
	__isl_keep isl_ast_build *build)
{
	isl_set *stride;

	if (!build)
		return isl_set_free(set);
	if (!isl_ast_build_has_stride(build, build->depth))
		return set;

	stride = isl_ast_build_get_stride_constraint(build);
	return isl_set_intersect(set, stride);
}

/* Check if the given bounds on the current dimension (together with
 * the stride constraint, if any) imply that
 * this current dimension attains only a single value (in terms of
 * parameters and outer dimensions).
 * If so, we record it in build->value.
 * If, moreover, this value can be represented as a single affine expression,
 * then we also update build->values, effectively marking the current
 * dimension as "eliminated".
 *
 * When computing the gist of the fixed value that can be represented
 * as a single affine expression, it is important to only take into
 * account the domain constraints in the original AST build and
 * not the domain of the affine expression itself.
 * Otherwise, a [i/3] is changed into a i/3 because we know that i
 * is a multiple of 3, but then we end up not expressing anywhere
 * in the context that i is a multiple of 3.
 */
static __isl_give isl_ast_build *update_values(
	__isl_take isl_ast_build *build, __isl_take isl_basic_set *bounds)
{
	isl_bool sv;
	isl_size n;
	isl_pw_multi_aff *pma;
	isl_aff *aff = NULL;
	isl_map *it_map;
	isl_set *set;

	set = isl_set_from_basic_set(bounds);
	set = isl_set_intersect(set, isl_set_copy(build->domain));
	set = intersect_stride_constraint(set, build);
	it_map = isl_ast_build_map_to_iterator(build, set);

	sv = isl_map_is_single_valued(it_map);
	if (sv < 0)
		build = isl_ast_build_free(build);
	if (!build || !sv) {
		isl_map_free(it_map);
		return build;
	}

	pma = isl_pw_multi_aff_from_map(it_map);
	build->value = isl_pw_multi_aff_get_pw_aff(pma, 0);
	build->value = isl_ast_build_compute_gist_pw_aff(build, build->value);
	build->value = isl_pw_aff_coalesce(build->value);
	isl_pw_multi_aff_free(pma);

	n = isl_pw_aff_n_piece(build->value);
	if (n < 0)
		return isl_ast_build_free(build);
	if (n != 1)
		return build;

	isl_pw_aff_foreach_piece(build->value, &extract_single_piece, &aff);

	build->values = isl_multi_aff_set_aff(build->values, build->depth, aff);
	if (!build->values)
		return isl_ast_build_free(build);
	isl_ast_build_reset_schedule_map(build);
	return build;
}

/* Update the AST build based on the given loop bounds for
 * the current dimension and the stride information available in the build.
 *
 * We first make sure that the bounds do not refer to any iterators
 * that have already been eliminated.
 * Then, we check if the bounds imply that the current iterator
 * has a fixed value.
 * If they do and if this fixed value can be expressed as a single
 * affine expression, we eliminate the iterators from the bounds.
 * Note that we cannot simply plug in this single value using
 * isl_basic_set_preimage_multi_aff as the single value may only
 * be defined on a subset of the domain.  Plugging in the value
 * would restrict the build domain to this subset, while this
 * restriction may not be reflected in the generated code.
 * Finally, we intersect build->domain with the updated bounds.
 * We also add the stride constraint unless we have been able
 * to find a fixed value expressed as a single affine expression.
 *
 * Note that the check for a fixed value in update_values requires
 * us to intersect the bounds with the current build domain.
 * When we intersect build->domain with the updated bounds in
 * the final step, we make sure that these updated bounds have
 * not been intersected with the old build->domain.
 * Otherwise, we would indirectly intersect the build domain with itself,
 * which can lead to inefficiencies, in particular if the build domain
 * contains any unknown divs.
 *
 * The pending and generated sets are not updated by this function to
 * match the updated domain.
 * The caller still needs to call isl_ast_build_set_pending_generated.
 */
__isl_give isl_ast_build *isl_ast_build_set_loop_bounds(
	__isl_take isl_ast_build *build, __isl_take isl_basic_set *bounds)
{
	isl_set *set;

	build = isl_ast_build_cow(build);
	if (!build)
		goto error;

	build = update_values(build, isl_basic_set_copy(bounds));
	if (!build)
		goto error;
	set = isl_set_from_basic_set(isl_basic_set_copy(bounds));
	if (isl_ast_build_has_affine_value(build, build->depth)) {
		set = isl_set_eliminate(set, isl_dim_set, build->depth, 1);
		set = isl_set_compute_divs(set);
		build->pending = isl_set_intersect(build->pending,
							isl_set_copy(set));
		build->domain = isl_set_intersect(build->domain, set);
	} else {
		build->domain = isl_set_intersect(build->domain, set);
		build = isl_ast_build_include_stride(build);
		if (!build)
			goto error;
	}
	isl_basic_set_free(bounds);

	if (!build->domain || !build->pending || !build->generated)
		return isl_ast_build_free(build);

	return build;
error:
	isl_ast_build_free(build);
	isl_basic_set_free(bounds);
	return NULL;
}

/* Update the pending and generated sets of "build" according to "bounds".
 * If the build has an affine value at the current depth,
 * then isl_ast_build_set_loop_bounds has already set the pending set.
 * Otherwise, do it here.
 */
__isl_give isl_ast_build *isl_ast_build_set_pending_generated(
	__isl_take isl_ast_build *build, __isl_take isl_basic_set *bounds)
{
	isl_basic_set *generated, *pending;

	if (!build)
		goto error;

	if (isl_ast_build_has_affine_value(build, build->depth)) {
		isl_basic_set_free(bounds);
		return build;
	}

	build = isl_ast_build_cow(build);
	if (!build)
		goto error;

	pending = isl_basic_set_copy(bounds);
	pending = isl_basic_set_drop_constraints_involving_dims(pending,
				isl_dim_set, build->depth, 1);
	build->pending = isl_set_intersect(build->pending,
				isl_set_from_basic_set(pending));
	generated = bounds;
	generated = isl_basic_set_drop_constraints_not_involving_dims(
			    generated, isl_dim_set, build->depth, 1);
	build->generated = isl_set_intersect(build->generated,
				isl_set_from_basic_set(generated));

	if (!build->pending || !build->generated)
		return isl_ast_build_free(build);

	return build;
error:
	isl_ast_build_free(build);
	isl_basic_set_free(bounds);
	return NULL;
}

/* Intersect build->domain with "set", where "set" is specified
 * in terms of the internal schedule domain.
 */
static __isl_give isl_ast_build *isl_ast_build_restrict_internal(
	__isl_take isl_ast_build *build, __isl_take isl_set *set)
{
	build = isl_ast_build_cow(build);
	if (!build)
		goto error;

	set = isl_set_compute_divs(set);
	build->domain = isl_set_intersect(build->domain, set);
	build->domain = isl_set_coalesce(build->domain);

	if (!build->domain)
		return isl_ast_build_free(build);

	return build;
error:
	isl_ast_build_free(build);
	isl_set_free(set);
	return NULL;
}

/* Intersect build->generated and build->domain with "set",
 * where "set" is specified in terms of the internal schedule domain.
 */
__isl_give isl_ast_build *isl_ast_build_restrict_generated(
	__isl_take isl_ast_build *build, __isl_take isl_set *set)
{
	set = isl_set_compute_divs(set);
	build = isl_ast_build_restrict_internal(build, isl_set_copy(set));
	build = isl_ast_build_cow(build);
	if (!build)
		goto error;

	build->generated = isl_set_intersect(build->generated, set);
	build->generated = isl_set_coalesce(build->generated);

	if (!build->generated)
		return isl_ast_build_free(build);

	return build;
error:
	isl_ast_build_free(build);
	isl_set_free(set);
	return NULL;
}

/* Replace the set of pending constraints by "guard", which is then
 * no longer considered as pending.
 * That is, add "guard" to the generated constraints and clear all pending
 * constraints, making the domain equal to the generated constraints.
 */
__isl_give isl_ast_build *isl_ast_build_replace_pending_by_guard(
	__isl_take isl_ast_build *build, __isl_take isl_set *guard)
{
	build = isl_ast_build_restrict_generated(build, guard);
	build = isl_ast_build_cow(build);
	if (!build)
		return NULL;

	isl_set_free(build->domain);
	build->domain = isl_set_copy(build->generated);
	isl_set_free(build->pending);
	build->pending = isl_set_universe(isl_set_get_space(build->domain));

	if (!build->pending)
		return isl_ast_build_free(build);

	return build;
}

/* Intersect build->domain with "set", where "set" is specified
 * in terms of the external schedule domain.
 */
__isl_give isl_ast_build *isl_ast_build_restrict(
	__isl_take isl_ast_build *build, __isl_take isl_set *set)
{
	isl_bool needs_map;

	if (isl_set_is_params(set))
		return isl_ast_build_restrict_generated(build, set);

	needs_map = isl_ast_build_need_schedule_map(build);
	if (needs_map < 0)
		goto error;
	if (needs_map) {
		isl_multi_aff *ma;
		ma = isl_ast_build_get_schedule_map_multi_aff(build);
		set = isl_set_preimage_multi_aff(set, ma);
	}
	return isl_ast_build_restrict_generated(build, set);
error:
	isl_ast_build_free(build);
	isl_set_free(set);
	return NULL;
}

/* Replace build->executed by "executed".
 */
__isl_give isl_ast_build *isl_ast_build_set_executed(
	__isl_take isl_ast_build *build, __isl_take isl_union_map *executed)
{
	build = isl_ast_build_cow(build);
	if (!build)
		goto error;

	isl_union_map_free(build->executed);
	build->executed = executed;

	return build;
error:
	isl_ast_build_free(build);
	isl_union_map_free(executed);
	return NULL;
}

/* Does "build" point to a band node?
 * That is, are we currently handling a band node inside a schedule tree?
 */
int isl_ast_build_has_schedule_node(__isl_keep isl_ast_build *build)
{
	if (!build)
		return -1;
	return build->node != NULL;
}

/* Return a copy of the band node that "build" refers to.
 */
__isl_give isl_schedule_node *isl_ast_build_get_schedule_node(
	__isl_keep isl_ast_build *build)
{
	if (!build)
		return NULL;
	return isl_schedule_node_copy(build->node);
}

/* Extract the loop AST generation types for the members of build->node
 * and store them in build->loop_type.
 */
static __isl_give isl_ast_build *extract_loop_types(
	__isl_take isl_ast_build *build)
{
	int i;
	isl_size n;
	isl_ctx *ctx;
	isl_schedule_node *node;

	if (!build)
		return NULL;
	n = isl_schedule_node_band_n_member(build->node);
	if (n < 0)
		return isl_ast_build_free(build);
	ctx = isl_ast_build_get_ctx(build);
	if (!build->node)
		isl_die(ctx, isl_error_internal, "missing AST node",
			return isl_ast_build_free(build));

	free(build->loop_type);
	build->n = n;
	build->loop_type = isl_alloc_array(ctx,
					    enum isl_ast_loop_type, build->n);
	if (build->n && !build->loop_type)
		return isl_ast_build_free(build);
	node = build->node;
	for (i = 0; i < build->n; ++i)
		build->loop_type[i] =
		    isl_schedule_node_band_member_get_ast_loop_type(node, i);

	return build;
}

/* Replace the band node that "build" refers to by "node" and
 * extract the corresponding loop AST generation types.
 */
__isl_give isl_ast_build *isl_ast_build_set_schedule_node(
	__isl_take isl_ast_build *build,
	__isl_take isl_schedule_node *node)
{
	build = isl_ast_build_cow(build);
	if (!build || !node)
		goto error;

	isl_schedule_node_free(build->node);
	build->node = node;

	build = extract_loop_types(build);

	return build;
error:
	isl_ast_build_free(build);
	isl_schedule_node_free(node);
	return NULL;
}

/* Remove any reference to a band node from "build".
 */
__isl_give isl_ast_build *isl_ast_build_reset_schedule_node(
	__isl_take isl_ast_build *build)
{
	build = isl_ast_build_cow(build);
	if (!build)
		return NULL;

	isl_schedule_node_free(build->node);
	build->node = NULL;

	return build;
}

/* Return a copy of the current schedule domain.
 */
__isl_give isl_set *isl_ast_build_get_domain(__isl_keep isl_ast_build *build)
{
	return build ? isl_set_copy(build->domain) : NULL;
}

/* Return a copy of the set of pending constraints.
 */
__isl_give isl_set *isl_ast_build_get_pending(
	__isl_keep isl_ast_build *build)
{
	return build ? isl_set_copy(build->pending) : NULL;
}

/* Return a copy of the set of generated constraints.
 */
__isl_give isl_set *isl_ast_build_get_generated(
	__isl_keep isl_ast_build *build)
{
	return build ? isl_set_copy(build->generated) : NULL;
}

/* Return a copy of the map from the internal schedule domain
 * to the original input schedule domain.
 */
__isl_give isl_multi_aff *isl_ast_build_get_internal2input(
	__isl_keep isl_ast_build *build)
{
	return build ? isl_multi_aff_copy(build->internal2input) : NULL;
}

/* Return the number of variables of the given type
 * in the (internal) schedule space.
 */
isl_size isl_ast_build_dim(__isl_keep isl_ast_build *build,
	enum isl_dim_type type)
{
	if (!build)
		return isl_size_error;
	return isl_set_dim(build->domain, type);
}

/* Return the (schedule) space of "build".
 *
 * If "internal" is set, then this space is the space of the internal
 * representation of the entire schedule, including those parts for
 * which no code has been generated yet.
 *
 * If "internal" is not set, then this space is the external representation
 * of the loops generated so far.
 */
__isl_give isl_space *isl_ast_build_get_space(__isl_keep isl_ast_build *build,
	int internal)
{
	int i;
	isl_size dim;
	isl_bool needs_map;
	isl_space *space;

	if (!build)
		return NULL;

	space = isl_set_get_space(build->domain);
	if (internal)
		return space;

	needs_map = isl_ast_build_need_schedule_map(build);
	if (needs_map < 0)
		return isl_space_free(space);
	if (!needs_map)
		return space;

	dim = isl_ast_build_dim(build, isl_dim_set);
	if (dim < 0)
		return isl_space_free(space);
	space = isl_space_drop_dims(space, isl_dim_set,
				    build->depth, dim - build->depth);
	for (i = build->depth - 1; i >= 0; --i) {
		isl_bool affine = isl_ast_build_has_affine_value(build, i);

		if (affine < 0)
			return isl_space_free(space);
		if (affine)
			space = isl_space_drop_dims(space, isl_dim_set, i, 1);
	}

	return space;
}

/* Return the external representation of the schedule space of "build",
 * i.e., a space with a dimension for each loop generated so far,
 * with the names of the dimensions set to the loop iterators.
 */
__isl_give isl_space *isl_ast_build_get_schedule_space(
	__isl_keep isl_ast_build *build)
{
	isl_space *space;
	int i, skip;

	if (!build)
		return NULL;

	space = isl_ast_build_get_space(build, 0);

	skip = 0;
	for (i = 0; i < build->depth; ++i) {
		isl_id *id;

		if (isl_ast_build_has_affine_value(build, i)) {
			skip++;
			continue;
		}

		id = isl_ast_build_get_iterator_id(build, i);
		space = isl_space_set_dim_id(space, isl_dim_set, i - skip, id);
	}

	return space;
}

/* Return the current schedule, as stored in build->executed, in terms
 * of the external schedule domain.
 */
__isl_give isl_union_map *isl_ast_build_get_schedule(
	__isl_keep isl_ast_build *build)
{
	isl_bool needs_map;
	isl_union_map *executed;
	isl_union_map *schedule;

	needs_map = isl_ast_build_need_schedule_map(build);
	if (needs_map < 0)
		return NULL;

	executed = isl_union_map_copy(build->executed);
	if (needs_map) {
		isl_map *proj = isl_ast_build_get_schedule_map(build);
		executed = isl_union_map_apply_domain(executed,
					isl_union_map_from_map(proj));
	}
	schedule = isl_union_map_reverse(executed);

	return schedule;
}

/* Return the iterator attached to the internal schedule dimension "pos".
 */
__isl_give isl_id *isl_ast_build_get_iterator_id(
	__isl_keep isl_ast_build *build, int pos)
{
	if (!build)
		return NULL;

	return isl_id_list_get_id(build->iterators, pos);
}

/* Set the stride and offset of the current dimension to the given
 * value and expression.
 */
static __isl_give isl_ast_build *set_stride(__isl_take isl_ast_build *build,
	__isl_take isl_val *stride, __isl_take isl_aff *offset)
{
	int pos;

	build = isl_ast_build_cow(build);
	if (!build || !stride || !offset)
		goto error;

	pos = build->depth;

	build->strides = isl_vec_set_element_val(build->strides, pos, stride);
	build->offsets = isl_multi_aff_set_aff(build->offsets, pos, offset);
	if (!build->strides || !build->offsets)
		return isl_ast_build_free(build);

	return build;
error:
	isl_val_free(stride);
	isl_aff_free(offset);
	return isl_ast_build_free(build);
}

/* Return a set expressing the stride constraint at the current depth.
 *
 * In particular, if the current iterator (i) is known to attain values
 *
 *	f + s a
 *
 * where f is the offset and s is the stride, then the returned set
 * expresses the constraint
 *
 *	(f - i) mod s = 0
 */
__isl_give isl_set *isl_ast_build_get_stride_constraint(
	__isl_keep isl_ast_build *build)
{
	isl_aff *aff;
	isl_set *set;
	isl_val *stride;
	int pos;

	if (!build)
		return NULL;

	pos = build->depth;

	if (!isl_ast_build_has_stride(build, pos))
		return isl_set_universe(isl_ast_build_get_space(build, 1));

	stride = isl_ast_build_get_stride(build, pos);
	aff = isl_ast_build_get_offset(build, pos);
	aff = isl_aff_add_coefficient_si(aff, isl_dim_in, pos, -1);
	aff = isl_aff_mod_val(aff, stride);
	set = isl_set_from_basic_set(isl_aff_zero_basic_set(aff));

	return set;
}

/* Return the expansion implied by the stride and offset at the current
 * depth.
 *
 * That is, return the mapping
 *
 *	[i_0, ..., i_{d-1}, i_d, i_{d+1}, ...]
 *		-> [i_0, ..., i_{d-1}, s * i_d + offset(i),  i_{d+1}, ...]
 *
 * where s is the stride at the current depth d and offset(i) is
 * the corresponding offset.
 */
__isl_give isl_multi_aff *isl_ast_build_get_stride_expansion(
	__isl_keep isl_ast_build *build)
{
	isl_space *space;
	isl_multi_aff *ma;
	int pos;
	isl_aff *aff, *offset;
	isl_val *stride;

	if (!build)
		return NULL;

	pos = isl_ast_build_get_depth(build);
	space = isl_ast_build_get_space(build, 1);
	space = isl_space_map_from_set(space);
	ma = isl_multi_aff_identity(space);

	if (!isl_ast_build_has_stride(build, pos))
		return ma;

	offset = isl_ast_build_get_offset(build, pos);
	stride = isl_ast_build_get_stride(build, pos);
	aff = isl_multi_aff_get_aff(ma, pos);
	aff = isl_aff_scale_val(aff, stride);
	aff = isl_aff_add(aff, offset);
	ma = isl_multi_aff_set_aff(ma, pos, aff);

	return ma;
}

/* Add constraints corresponding to any previously detected
 * stride on the current dimension to build->domain.
 */
__isl_give isl_ast_build *isl_ast_build_include_stride(
	__isl_take isl_ast_build *build)
{
	isl_set *set;

	if (!build)
		return NULL;
	if (!isl_ast_build_has_stride(build, build->depth))
		return build;
	build = isl_ast_build_cow(build);
	if (!build)
		return NULL;

	set = isl_ast_build_get_stride_constraint(build);

	build->domain = isl_set_intersect(build->domain, isl_set_copy(set));
	build->generated = isl_set_intersect(build->generated, set);
	if (!build->domain || !build->generated)
		return isl_ast_build_free(build);

	return build;
}

/* Check if the constraints in "set" imply any stride on the current
 * dimension and, if so, record the stride information in "build"
 * and return the updated "build".
 *
 * We assume that inner dimensions have been eliminated from "set"
 * by the caller.  This is needed because the common stride
 * may be imposed by different inner dimensions on different parts of
 * the domain.
 * The assumption ensures that the lower bound does not depend
 * on inner dimensions.
 */
__isl_give isl_ast_build *isl_ast_build_detect_strides(
	__isl_take isl_ast_build *build, __isl_take isl_set *set)
{
	int pos;
	isl_bool no_stride;
	isl_val *stride;
	isl_aff *offset;
	isl_stride_info *si;

	if (!build)
		goto error;

	pos = isl_ast_build_get_depth(build);
	si = isl_set_get_stride_info(set, pos);
	stride = isl_stride_info_get_stride(si);
	offset = isl_stride_info_get_offset(si);
	isl_stride_info_free(si);
	isl_set_free(set);

	no_stride = isl_val_is_one(stride);
	if (no_stride >= 0 && !no_stride)
		return set_stride(build, stride, offset);
	isl_val_free(stride);
	isl_aff_free(offset);
	if (no_stride < 0)
		return isl_ast_build_free(build);
	return build;
error:
	isl_set_free(set);
	return NULL;
}

struct isl_ast_build_involves_data {
	int depth;
	int involves;
};

/* Check if "map" involves the input dimension data->depth.
 */
static isl_stat involves_depth(__isl_take isl_map *map, void *user)
{
	struct isl_ast_build_involves_data *data = user;

	data->involves = isl_map_involves_dims(map, isl_dim_in, data->depth, 1);
	isl_map_free(map);

	if (data->involves < 0 || data->involves)
		return isl_stat_error;
	return isl_stat_ok;
}

/* Do any options depend on the value of the dimension at the current depth?
 */
int isl_ast_build_options_involve_depth(__isl_keep isl_ast_build *build)
{
	struct isl_ast_build_involves_data data;

	if (!build)
		return -1;

	data.depth = build->depth;
	data.involves = 0;

	if (isl_union_map_foreach_map(build->options,
					&involves_depth, &data) < 0) {
		if (data.involves < 0 || !data.involves)
			return -1;
	}

	return data.involves;
}

/* Construct the map
 *
 *	{ [i] -> [i] : i < pos; [i] -> [i + 1] : i >= pos }
 *
 * with "space" the parameter space of the constructed map.
 */
static __isl_give isl_map *construct_insertion_map(__isl_take isl_space *space,
	int pos)
{
	isl_constraint *c;
	isl_basic_map *bmap1, *bmap2;

	space = isl_space_set_from_params(space);
	space = isl_space_add_dims(space, isl_dim_set, 1);
	space = isl_space_map_from_set(space);
	c = isl_constraint_alloc_equality(isl_local_space_from_space(space));
	c = isl_constraint_set_coefficient_si(c, isl_dim_in, 0, 1);
	c = isl_constraint_set_coefficient_si(c, isl_dim_out, 0, -1);
	bmap1 = isl_basic_map_from_constraint(isl_constraint_copy(c));
	c = isl_constraint_set_constant_si(c, 1);
	bmap2 = isl_basic_map_from_constraint(c);

	bmap1 = isl_basic_map_upper_bound_si(bmap1, isl_dim_in, 0, pos - 1);
	bmap2 = isl_basic_map_lower_bound_si(bmap2, isl_dim_in, 0, pos);

	return isl_basic_map_union(bmap1, bmap2);
}

static const char *option_str[] = {
	[isl_ast_loop_atomic] = "atomic",
	[isl_ast_loop_unroll] = "unroll",
	[isl_ast_loop_separate] = "separate"
};

/* Update the "options" to reflect the insertion of a dimension
 * at position "pos" in the schedule domain space.
 * "space" is the original domain space before the insertion and
 * may be named and/or structured.
 *
 * The (relevant) input options all have "space" as domain, which
 * has to be mapped to the extended space.
 * The values of the ranges also refer to the schedule domain positions
 * and they therefore also need to be adjusted.  In particular, values
 * smaller than pos do not need to change, while values greater than or
 * equal to pos need to be incremented.
 * That is, we need to apply the following map.
 *
 *	{ atomic[i] -> atomic[i] : i < pos; [i] -> [i + 1] : i >= pos;
 *	  unroll[i] -> unroll[i] : i < pos; [i] -> [i + 1] : i >= pos;
 *	  separate[i] -> separate[i] : i < pos; [i] -> [i + 1] : i >= pos;
 *	  separation_class[[i] -> [c]]
 *		-> separation_class[[i] -> [c]] : i < pos;
 *	  separation_class[[i] -> [c]]
 *		-> separation_class[[i + 1] -> [c]] : i >= pos }
 */
static __isl_give isl_union_map *options_insert_dim(
	__isl_take isl_union_map *options, __isl_take isl_space *space, int pos)
{
	isl_map *map;
	isl_union_map *insertion;
	enum isl_ast_loop_type type;
	const char *name = "separation_class";

	space = isl_space_map_from_set(space);
	map = isl_map_identity(space);
	map = isl_map_insert_dims(map, isl_dim_out, pos, 1);
	options = isl_union_map_apply_domain(options,
						isl_union_map_from_map(map));

	if (!options)
		return NULL;

	map = construct_insertion_map(isl_union_map_get_space(options), pos);

	insertion = isl_union_map_empty(isl_union_map_get_space(options));

	for (type = isl_ast_loop_atomic;
	    type <= isl_ast_loop_separate; ++type) {
		isl_map *map_type = isl_map_copy(map);
		const char *name = option_str[type];
		map_type = isl_map_set_tuple_name(map_type, isl_dim_in, name);
		map_type = isl_map_set_tuple_name(map_type, isl_dim_out, name);
		insertion = isl_union_map_add_map(insertion, map_type);
	}

	map = isl_map_product(map, isl_map_identity(isl_map_get_space(map)));
	map = isl_map_set_tuple_name(map, isl_dim_in, name);
	map = isl_map_set_tuple_name(map, isl_dim_out, name);
	insertion = isl_union_map_add_map(insertion, map);

	options = isl_union_map_apply_range(options, insertion);

	return options;
}

/* If we are generating an AST from a schedule tree (build->node is set),
 * then update the loop AST generation types
 * to reflect the insertion of a dimension at (global) position "pos"
 * in the schedule domain space.
 * We do not need to adjust any isolate option since we would not be inserting
 * any dimensions if there were any isolate option.
 */
static __isl_give isl_ast_build *node_insert_dim(
	__isl_take isl_ast_build *build, int pos)
{
	int i;
	int local_pos;
	enum isl_ast_loop_type *loop_type;
	isl_ctx *ctx;

	build = isl_ast_build_cow(build);
	if (!build)
		return NULL;
	if (!build->node)
		return build;

	ctx = isl_ast_build_get_ctx(build);
	local_pos = pos - build->outer_pos;
	loop_type = isl_realloc_array(ctx, build->loop_type,
					enum isl_ast_loop_type, build->n + 1);
	if (!loop_type)
		return isl_ast_build_free(build);
	build->loop_type = loop_type;
	for (i = build->n - 1; i >= local_pos; --i)
		loop_type[i + 1] = loop_type[i];
	loop_type[local_pos] = isl_ast_loop_default;
	build->n++;

	return build;
}

/* Insert a single dimension in the schedule domain at position "pos".
 * The new dimension is given an isl_id with the empty string as name.
 *
 * The main difficulty is updating build->options to reflect the
 * extra dimension.  This is handled in options_insert_dim.
 *
 * Note that because of the dimension manipulations, the resulting
 * schedule domain space will always be unnamed and unstructured.
 * However, the original schedule domain space may be named and/or
 * structured, so we have to take this possibility into account
 * while performing the transformations.
 *
 * Since the inserted schedule dimension is used by the caller
 * to differentiate between different domain spaces, there is
 * no longer a uniform mapping from the internal schedule space
 * to the input schedule space.  The internal2input mapping is
 * therefore removed.
 */
__isl_give isl_ast_build *isl_ast_build_insert_dim(
	__isl_take isl_ast_build *build, int pos)
{
	isl_ctx *ctx;
	isl_space *space, *ma_space;
	isl_id *id;
	isl_multi_aff *ma;

	build = isl_ast_build_cow(build);
	if (!build)
		return NULL;

	ctx = isl_ast_build_get_ctx(build);
	id = isl_id_alloc(ctx, "", NULL);
	if (!build->node)
		space = isl_ast_build_get_space(build, 1);
	build->iterators = isl_id_list_insert(build->iterators, pos, id);
	build->domain = isl_set_insert_dims(build->domain,
						isl_dim_set, pos, 1);
	build->generated = isl_set_insert_dims(build->generated,
						isl_dim_set, pos, 1);
	build->pending = isl_set_insert_dims(build->pending,
						isl_dim_set, pos, 1);
	build->strides = isl_vec_insert_els(build->strides, pos, 1);
	build->strides = isl_vec_set_element_si(build->strides, pos, 1);
	ma_space = isl_space_params(isl_multi_aff_get_space(build->offsets));
	ma_space = isl_space_set_from_params(ma_space);
	ma_space = isl_space_add_dims(ma_space, isl_dim_set, 1);
	ma_space = isl_space_map_from_set(ma_space);
	ma = isl_multi_aff_zero(isl_space_copy(ma_space));
	build->offsets = isl_multi_aff_splice(build->offsets, pos, pos, ma);
	ma = isl_multi_aff_identity(ma_space);
	build->values = isl_multi_aff_splice(build->values, pos, pos, ma);
	if (!build->node)
		build->options = options_insert_dim(build->options, space, pos);
	build->internal2input = isl_multi_aff_free(build->internal2input);

	if (!build->iterators || !build->domain || !build->generated ||
	    !build->pending || !build->values ||
	    !build->strides || !build->offsets || !build->options)
		return isl_ast_build_free(build);

	build = node_insert_dim(build, pos);

	return build;
}

/* Scale down the current dimension by a factor of "m".
 * "umap" is an isl_union_map that implements the scaling down.
 * That is, it is of the form
 *
 *	{ [.... i ....] -> [.... i' ....] : i = m i' }
 *
 * This function is called right after the strides have been
 * detected, but before any constraints on the current dimension
 * have been included in build->domain.
 * We therefore only need to update stride, offset, the options and
 * the mapping from internal schedule space to the original schedule
 * space, if we are still keeping track of such a mapping.
 * The latter mapping is updated by plugging in
 * { [... i ...] -> [... m i ... ] }.
 */
__isl_give isl_ast_build *isl_ast_build_scale_down(
	__isl_take isl_ast_build *build, __isl_take isl_val *m,
	__isl_take isl_union_map *umap)
{
	isl_aff *aff;
	isl_val *v;
	int depth;

	build = isl_ast_build_cow(build);
	if (!build || !umap || !m)
		goto error;

	depth = build->depth;

	if (build->internal2input) {
		isl_space *space;
		isl_multi_aff *ma;
		isl_aff *aff;

		space = isl_multi_aff_get_space(build->internal2input);
		space = isl_space_map_from_set(isl_space_domain(space));
		ma = isl_multi_aff_identity(space);
		aff = isl_multi_aff_get_aff(ma, depth);
		aff = isl_aff_scale_val(aff, isl_val_copy(m));
		ma = isl_multi_aff_set_aff(ma, depth, aff);
		build->internal2input =
		    isl_multi_aff_pullback_multi_aff(build->internal2input, ma);
		if (!build->internal2input)
			goto error;
	}

	v = isl_vec_get_element_val(build->strides, depth);
	v = isl_val_div(v, isl_val_copy(m));
	build->strides = isl_vec_set_element_val(build->strides, depth, v);

	aff = isl_multi_aff_get_aff(build->offsets, depth);
	aff = isl_aff_scale_down_val(aff, m);
	build->offsets = isl_multi_aff_set_aff(build->offsets, depth, aff);
	build->options = isl_union_map_apply_domain(build->options, umap);
	if (!build->strides || !build->offsets || !build->options)
		return isl_ast_build_free(build);

	return build;
error:
	isl_val_free(m);
	isl_union_map_free(umap);
	return isl_ast_build_free(build);
}

/* Return a list of "n" isl_ids called "c%d", with "%d" starting at "first".
 * If an isl_id with such a name already appears among the parameters
 * in build->domain, then adjust the name to "c%d_%d".
 */
static __isl_give isl_id_list *generate_names(isl_ctx *ctx, int n, int first,
	__isl_keep isl_ast_build *build)
{
	int i;
	isl_id_list *names;

	names = isl_id_list_alloc(ctx, n);
	for (i = 0; i < n; ++i) {
		isl_id *id;

		id = generate_name(ctx, first + i, build);
		names = isl_id_list_add(names, id);
	}

	return names;
}

/* Embed "options" into the given isl_ast_build space.
 *
 * This function is called from within a nested call to
 * isl_ast_build_node_from_schedule_map.
 * "options" refers to the additional schedule,
 * while space refers to both the space of the outer isl_ast_build and
 * that of the additional schedule.
 * Specifically, space is of the form
 *
 *	[I -> S]
 *
 * while options lives in the space(s)
 *
 *	S -> *
 *
 * We compute
 *
 *	[I -> S] -> S
 *
 * and compose this with options, to obtain the new options
 * living in the space(s)
 *
 *	[I -> S] -> *
 */
static __isl_give isl_union_map *embed_options(
	__isl_take isl_union_map *options, __isl_take isl_space *space)
{
	isl_map *map;

	map = isl_map_universe(isl_space_unwrap(space));
	map = isl_map_range_map(map);

	options = isl_union_map_apply_range(
				isl_union_map_from_map(map), options);

	return options;
}

/* Update "build" for use in a (possibly nested) code generation.  That is,
 * extend "build" from an AST build on some domain O to an AST build
 * on domain [O -> S], with S corresponding to "space".
 * If the original domain is a parameter domain, then the new domain is
 * simply S.
 * "iterators" is a list of iterators for S, but the number of elements
 * may be smaller or greater than the number of set dimensions of S.
 * If "keep_iterators" is set, then any extra ids in build->iterators
 * are reused for S.  Otherwise, these extra ids are dropped.
 *
 * We first update build->outer_pos to the current depth.
 * This depth is zero in case this is the outermost code generation.
 *
 * We then add additional ids such that the number of iterators is at least
 * equal to the dimension of the new build domain.
 *
 * If the original domain is parametric, then we are constructing
 * an isl_ast_build for the outer code generation and we pass control
 * to isl_ast_build_init.
 *
 * Otherwise, we adjust the fields of "build" to include "space".
 */
__isl_give isl_ast_build *isl_ast_build_product(
	__isl_take isl_ast_build *build, __isl_take isl_space *space)
{
	isl_ctx *ctx;
	isl_vec *strides;
	isl_set *set;
	isl_multi_aff *embedding;
	isl_size dim, space_dim, n_it;

	build = isl_ast_build_cow(build);
	if (!build)
		goto error;

	build->outer_pos = build->depth;

	ctx = isl_ast_build_get_ctx(build);
	dim = isl_ast_build_dim(build, isl_dim_set);
	space_dim = isl_space_dim(space, isl_dim_set);
	n_it = isl_id_list_n_id(build->iterators);
	if (dim < 0 || space_dim < 0 || n_it < 0)
		goto error;
	dim += space_dim;
	if (n_it < dim) {
		isl_id_list *l;
		l = generate_names(ctx, dim - n_it, n_it, build);
		build->iterators = isl_id_list_concat(build->iterators, l);
	}

	if (isl_set_is_params(build->domain))
		return isl_ast_build_init(build, space);

	set = isl_set_universe(isl_space_copy(space));
	build->domain = isl_set_product(build->domain, isl_set_copy(set));
	build->pending = isl_set_product(build->pending, isl_set_copy(set));
	build->generated = isl_set_product(build->generated, set);

	strides = isl_vec_alloc(ctx, space_dim);
	strides = isl_vec_set_si(strides, 1);
	build->strides = isl_vec_concat(build->strides, strides);

	space = isl_space_map_from_set(space);
	build->offsets = isl_multi_aff_align_params(build->offsets,
						    isl_space_copy(space));
	build->offsets = isl_multi_aff_product(build->offsets,
				isl_multi_aff_zero(isl_space_copy(space)));
	build->values = isl_multi_aff_align_params(build->values,
						    isl_space_copy(space));
	embedding = isl_multi_aff_identity(space);
	build->values = isl_multi_aff_product(build->values,
					isl_multi_aff_copy(embedding));
	if (build->internal2input) {
		build->internal2input =
			isl_multi_aff_product(build->internal2input, embedding);
		build->internal2input =
			isl_multi_aff_flatten_range(build->internal2input);
		if (!build->internal2input)
			return isl_ast_build_free(build);
	} else {
		isl_multi_aff_free(embedding);
	}

	space = isl_ast_build_get_space(build, 1);
	build->options = embed_options(build->options, space);

	if (!build->iterators || !build->domain || !build->generated ||
	    !build->pending || !build->values ||
	    !build->strides || !build->offsets || !build->options)
		return isl_ast_build_free(build);

	return build;
error:
	isl_ast_build_free(build);
	isl_space_free(space);
	return NULL;
}

/* Does "aff" only attain non-negative values over build->domain?
 * That is, does it not attain any negative values?
 */
int isl_ast_build_aff_is_nonneg(__isl_keep isl_ast_build *build,
	__isl_keep isl_aff *aff)
{
	isl_set *test;
	int empty;

	if (!build)
		return -1;

	aff = isl_aff_copy(aff);
	test = isl_set_from_basic_set(isl_aff_neg_basic_set(aff));
	test = isl_set_intersect(test, isl_set_copy(build->domain));
	empty = isl_set_is_empty(test);
	isl_set_free(test);

	return empty;
}

/* Does the dimension at (internal) position "pos" have a non-trivial stride?
 */
isl_bool isl_ast_build_has_stride(__isl_keep isl_ast_build *build, int pos)
{
	isl_val *v;
	isl_bool has_stride;

	if (!build)
		return isl_bool_error;

	v = isl_vec_get_element_val(build->strides, pos);
	has_stride = isl_bool_not(isl_val_is_one(v));
	isl_val_free(v);

	return has_stride;
}

/* Given that the dimension at position "pos" takes on values
 *
 *	f + s a
 *
 * with a an integer, return s through *stride.
 */
__isl_give isl_val *isl_ast_build_get_stride(__isl_keep isl_ast_build *build,
	int pos)
{
	if (!build)
		return NULL;

	return isl_vec_get_element_val(build->strides, pos);
}

/* Given that the dimension at position "pos" takes on values
 *
 *	f + s a
 *
 * with a an integer, return f.
 */
__isl_give isl_aff *isl_ast_build_get_offset(
	__isl_keep isl_ast_build *build, int pos)
{
	if (!build)
		return NULL;

	return isl_multi_aff_get_aff(build->offsets, pos);
}

/* Is the dimension at position "pos" known to attain only a single
 * value that, moreover, can be described by a single affine expression
 * in terms of the outer dimensions and parameters?
 *
 * If not, then the corresponding affine expression in build->values
 * is set to be equal to the same input dimension.
 * Otherwise, it is set to the requested expression in terms of
 * outer dimensions and parameters.
 */
isl_bool isl_ast_build_has_affine_value(__isl_keep isl_ast_build *build,
	int pos)
{
	isl_aff *aff;
	isl_bool involves;

	if (!build)
		return isl_bool_error;

	aff = isl_multi_aff_get_aff(build->values, pos);
	involves = isl_aff_involves_dims(aff, isl_dim_in, pos, 1);
	isl_aff_free(aff);

	return isl_bool_not(involves);
}

/* Plug in the known values (fixed affine expressions in terms of
 * parameters and outer loop iterators) of all loop iterators
 * in the domain of "umap".
 *
 * We simply precompose "umap" with build->values.
 */
__isl_give isl_union_map *isl_ast_build_substitute_values_union_map_domain(
	__isl_keep isl_ast_build *build, __isl_take isl_union_map *umap)
{
	isl_multi_aff *values;

	if (!build)
		return isl_union_map_free(umap);

	values = isl_multi_aff_copy(build->values);
	umap = isl_union_map_preimage_domain_multi_aff(umap, values);

	return umap;
}

/* Is the current dimension known to attain only a single value?
 */
int isl_ast_build_has_value(__isl_keep isl_ast_build *build)
{
	if (!build)
		return -1;

	return build->value != NULL;
}

/* Simplify the basic set "bset" based on what we know about
 * the iterators of already generated loops.
 *
 * "bset" is assumed to live in the (internal) schedule domain.
 */
__isl_give isl_basic_set *isl_ast_build_compute_gist_basic_set(
	__isl_keep isl_ast_build *build, __isl_take isl_basic_set *bset)
{
	if (!build)
		goto error;

	bset = isl_basic_set_preimage_multi_aff(bset,
					isl_multi_aff_copy(build->values));
	bset = isl_basic_set_gist(bset,
			isl_set_simple_hull(isl_set_copy(build->domain)));

	return bset;
error:
	isl_basic_set_free(bset);
	return NULL;
}

/* Simplify the set "set" based on what we know about
 * the iterators of already generated loops.
 *
 * "set" is assumed to live in the (internal) schedule domain.
 */
__isl_give isl_set *isl_ast_build_compute_gist(
	__isl_keep isl_ast_build *build, __isl_take isl_set *set)
{
	if (!build)
		goto error;

	if (!isl_set_is_params(set))
		set = isl_set_preimage_multi_aff(set,
					isl_multi_aff_copy(build->values));
	set = isl_set_gist(set, isl_set_copy(build->domain));

	return set;
error:
	isl_set_free(set);
	return NULL;
}

/* Include information about what we know about the iterators of
 * already generated loops to "set".
 *
 * We currently only plug in the known affine values of outer loop
 * iterators.
 * In principle we could also introduce equalities or even other
 * constraints implied by the intersection of "set" and build->domain.
 */
__isl_give isl_set *isl_ast_build_specialize(__isl_keep isl_ast_build *build,
	__isl_take isl_set *set)
{
	if (!build)
		return isl_set_free(set);

	return isl_set_preimage_multi_aff(set,
					isl_multi_aff_copy(build->values));
}

/* Plug in the known affine values of outer loop iterators in "bset".
 */
__isl_give isl_basic_set *isl_ast_build_specialize_basic_set(
	__isl_keep isl_ast_build *build, __isl_take isl_basic_set *bset)
{
	if (!build)
		return isl_basic_set_free(bset);

	return isl_basic_set_preimage_multi_aff(bset,
					isl_multi_aff_copy(build->values));
}

/* Simplify the map "map" based on what we know about
 * the iterators of already generated loops.
 *
 * The domain of "map" is assumed to live in the (internal) schedule domain.
 */
__isl_give isl_map *isl_ast_build_compute_gist_map_domain(
	__isl_keep isl_ast_build *build, __isl_take isl_map *map)
{
	if (!build)
		goto error;

	map = isl_map_gist_domain(map, isl_set_copy(build->domain));

	return map;
error:
	isl_map_free(map);
	return NULL;
}

/* Simplify the affine expression "aff" based on what we know about
 * the iterators of already generated loops.
 *
 * The domain of "aff" is assumed to live in the (internal) schedule domain.
 */
__isl_give isl_aff *isl_ast_build_compute_gist_aff(
	__isl_keep isl_ast_build *build, __isl_take isl_aff *aff)
{
	if (!build)
		goto error;

	aff = isl_aff_gist(aff, isl_set_copy(build->domain));

	return aff;
error:
	isl_aff_free(aff);
	return NULL;
}

/* Simplify the piecewise affine expression "aff" based on what we know about
 * the iterators of already generated loops.
 *
 * The domain of "pa" is assumed to live in the (internal) schedule domain.
 */
__isl_give isl_pw_aff *isl_ast_build_compute_gist_pw_aff(
	__isl_keep isl_ast_build *build, __isl_take isl_pw_aff *pa)
{
	if (!build)
		goto error;

	if (!isl_set_is_params(build->domain))
		pa = isl_pw_aff_pullback_multi_aff(pa,
					isl_multi_aff_copy(build->values));
	pa = isl_pw_aff_gist(pa, isl_set_copy(build->domain));

	return pa;
error:
	isl_pw_aff_free(pa);
	return NULL;
}

/* Simplify the piecewise multi-affine expression "aff" based on what
 * we know about the iterators of already generated loops.
 *
 * The domain of "pma" is assumed to live in the (internal) schedule domain.
 */
__isl_give isl_pw_multi_aff *isl_ast_build_compute_gist_pw_multi_aff(
	__isl_keep isl_ast_build *build, __isl_take isl_pw_multi_aff *pma)
{
	if (!build)
		goto error;

	pma = isl_pw_multi_aff_pullback_multi_aff(pma,
					isl_multi_aff_copy(build->values));
	pma = isl_pw_multi_aff_gist(pma, isl_set_copy(build->domain));

	return pma;
error:
	isl_pw_multi_aff_free(pma);
	return NULL;
}

/* Extract the schedule domain of the given type from build->options
 * at the current depth.
 *
 * In particular, find the subset of build->options that is of
 * the following form
 *
 *	schedule_domain -> type[depth]
 *
 * and return the corresponding domain, after eliminating inner dimensions
 * and divs that depend on the current dimension.
 *
 * Note that the domain of build->options has been reformulated
 * in terms of the internal build space in embed_options,
 * but the position is still that within the current code generation.
 */
__isl_give isl_set *isl_ast_build_get_option_domain(
	__isl_keep isl_ast_build *build, enum isl_ast_loop_type type)
{
	const char *name;
	isl_space *space;
	isl_map *option;
	isl_set *domain;
	int local_pos;

	if (!build)
		return NULL;

	name = option_str[type];
	local_pos = build->depth - build->outer_pos;

	space = isl_ast_build_get_space(build, 1);
	space = isl_space_from_domain(space);
	space = isl_space_add_dims(space, isl_dim_out, 1);
	space = isl_space_set_tuple_name(space, isl_dim_out, name);

	option = isl_union_map_extract_map(build->options, space);
	option = isl_map_fix_si(option, isl_dim_out, 0, local_pos);

	domain = isl_map_domain(option);
	domain = isl_ast_build_eliminate(build, domain);

	return domain;
}

/* How does the user want the current schedule dimension to be generated?
 * These choices have been extracted from the schedule node
 * in extract_loop_types and stored in build->loop_type.
 * They have been updated to reflect any dimension insertion in
 * node_insert_dim.
 * Return isl_ast_domain_error on error.
 *
 * If "isolated" is set, then we get the loop AST generation type
 * directly from the band node since node_insert_dim cannot have been
 * called on a band with the isolate option.
 */
enum isl_ast_loop_type isl_ast_build_get_loop_type(
	__isl_keep isl_ast_build *build, int isolated)
{
	int local_pos;
	isl_ctx *ctx;

	if (!build)
		return isl_ast_loop_error;
	ctx = isl_ast_build_get_ctx(build);
	if (!build->node)
		isl_die(ctx, isl_error_internal,
			"only works for schedule tree based AST generation",
			return isl_ast_loop_error);

	local_pos = build->depth - build->outer_pos;
	if (!isolated)
		return build->loop_type[local_pos];
	return isl_schedule_node_band_member_get_isolate_ast_loop_type(
							build->node, local_pos);
}

/* Extract the isolated set from the isolate option, if any,
 * and store in the build.
 * If there is no isolate option, then the isolated set is
 * set to the empty set.
 *
 * The isolate option is of the form
 *
 *	isolate[[outer bands] -> current_band]
 *
 * We flatten this set and then map it back to the internal
 * schedule space.
 *
 * If we have already extracted the isolated set
 * or if internal2input is no longer set, then we do not
 * need to do anything.  In the latter case, we know
 * that the current band cannot have any isolate option.
 */
__isl_give isl_ast_build *isl_ast_build_extract_isolated(
	__isl_take isl_ast_build *build)
{
	isl_set *isolated;

	if (!build)
		return NULL;
	if (!build->internal2input)
		return build;
	if (build->isolated)
		return build;

	build = isl_ast_build_cow(build);
	if (!build)
		return NULL;

	isolated = isl_schedule_node_band_get_ast_isolate_option(build->node);
	isolated = isl_set_flatten(isolated);
	isolated = isl_set_preimage_multi_aff(isolated,
				    isl_multi_aff_copy(build->internal2input));

	build->isolated = isolated;
	if (!build->isolated)
		return isl_ast_build_free(build);

	return build;
}

/* Does "build" have a non-empty isolated set?
 *
 * The caller is assumed to have called isl_ast_build_extract_isolated first.
 */
int isl_ast_build_has_isolated(__isl_keep isl_ast_build *build)
{
	int empty;

	if (!build)
		return -1;
	if (!build->internal2input)
		return 0;
	if (!build->isolated)
		isl_die(isl_ast_build_get_ctx(build), isl_error_internal,
			"isolated set not extracted yet", return -1);

	empty = isl_set_plain_is_empty(build->isolated);
	return empty < 0 ? -1 : !empty;
}

/* Return a copy of the isolated set of "build".
 *
 * The caller is assume to have called isl_ast_build_has_isolated first,
 * with this function returning true.
 * In particular, this function should not be called if we are no
 * longer keeping track of internal2input (and there therefore could
 * not possibly be any isolated set).
 */
__isl_give isl_set *isl_ast_build_get_isolated(__isl_keep isl_ast_build *build)
{
	if (!build)
		return NULL;
	if (!build->internal2input)
		isl_die(isl_ast_build_get_ctx(build), isl_error_internal,
			"build cannot have isolated set", return NULL);

	return isl_set_copy(build->isolated);
}

/* Extract the separation class mapping at the current depth.
 *
 * In particular, find and return the subset of build->options that is of
 * the following form
 *
 *	schedule_domain -> separation_class[[depth] -> [class]]
 *
 * The caller is expected to eliminate inner dimensions from the domain.
 *
 * Note that the domain of build->options has been reformulated
 * in terms of the internal build space in embed_options,
 * but the position is still that within the current code generation.
 */
__isl_give isl_map *isl_ast_build_get_separation_class(
	__isl_keep isl_ast_build *build)
{
	isl_ctx *ctx;
	isl_space *space_sep, *space;
	isl_map *res;
	int local_pos;

	if (!build)
		return NULL;

	local_pos = build->depth - build->outer_pos;
	ctx = isl_ast_build_get_ctx(build);
	space_sep = isl_space_alloc(ctx, 0, 1, 1);
	space_sep = isl_space_wrap(space_sep);
	space_sep = isl_space_set_tuple_name(space_sep, isl_dim_set,
						"separation_class");
	space = isl_ast_build_get_space(build, 1);
	space_sep = isl_space_align_params(space_sep, isl_space_copy(space));
	space = isl_space_map_from_domain_and_range(space, space_sep);

	res = isl_union_map_extract_map(build->options, space);
	res = isl_map_fix_si(res, isl_dim_out, 0, local_pos);
	res = isl_map_coalesce(res);

	return res;
}

/* Eliminate dimensions inner to the current dimension.
 */
__isl_give isl_set *isl_ast_build_eliminate_inner(
	__isl_keep isl_ast_build *build, __isl_take isl_set *set)
{
	int dim;
	int depth;

	if (!build)
		return isl_set_free(set);

	dim = isl_set_dim(set, isl_dim_set);
	depth = build->depth;
	set = isl_set_detect_equalities(set);
	set = isl_set_eliminate(set, isl_dim_set, depth + 1, dim - (depth + 1));

	return set;
}

/* Eliminate unknown divs and divs that depend on the current dimension.
 *
 * Note that during the elimination of unknown divs, we may discover
 * an explicit representation of some other unknown divs, which may
 * depend on the current dimension.  We therefore need to eliminate
 * unknown divs first.
 */
__isl_give isl_set *isl_ast_build_eliminate_divs(
	__isl_keep isl_ast_build *build, __isl_take isl_set *set)
{
	int depth;

	if (!build)
		return isl_set_free(set);

	set = isl_set_remove_unknown_divs(set);
	depth = build->depth;
	set = isl_set_remove_divs_involving_dims(set, isl_dim_set, depth, 1);

	return set;
}

/* Eliminate dimensions inner to the current dimension as well as
 * unknown divs and divs that depend on the current dimension.
 * The result then consists only of constraints that are independent
 * of the current dimension and upper and lower bounds on the current
 * dimension.
 */
__isl_give isl_set *isl_ast_build_eliminate(
	__isl_keep isl_ast_build *build, __isl_take isl_set *domain)
{
	domain = isl_ast_build_eliminate_inner(build, domain);
	domain = isl_ast_build_eliminate_divs(build, domain);
	return domain;
}

/* Replace build->single_valued by "sv".
 */
__isl_give isl_ast_build *isl_ast_build_set_single_valued(
	__isl_take isl_ast_build *build, int sv)
{
	if (!build)
		return build;
	if (build->single_valued == sv)
		return build;
	build = isl_ast_build_cow(build);
	if (!build)
		return build;
	build->single_valued = sv;

	return build;
}