isl_schedule_band.c 33.5 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
/*
 * Copyright 2013-2014 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 <string.h>
#include <isl/val.h>
#include <isl/space.h>
#include <isl/map.h>
#include <isl/schedule_node.h>
#include <isl_schedule_band.h>
#include <isl_schedule_private.h>

isl_ctx *isl_schedule_band_get_ctx(__isl_keep isl_schedule_band *band)
{
	return band ? isl_multi_union_pw_aff_get_ctx(band->mupa) : NULL;
}

/* Return a new uninitialized isl_schedule_band.
 */
static __isl_give isl_schedule_band *isl_schedule_band_alloc(isl_ctx *ctx)
{
	isl_schedule_band *band;

	band = isl_calloc_type(ctx, isl_schedule_band);
	if (!band)
		return NULL;

	band->ref = 1;

	return band;
}

/* Return a new isl_schedule_band with partial schedule "mupa".
 * First replace "mupa" by its greatest integer part to ensure
 * that the schedule is always integral.
 * The band is not marked permutable, the dimensions are not
 * marked coincident and the AST build options are empty.
 * Since there are no build options, the node is not anchored.
 */
__isl_give isl_schedule_band *isl_schedule_band_from_multi_union_pw_aff(
	__isl_take isl_multi_union_pw_aff *mupa)
{
	isl_size dim;
	isl_ctx *ctx;
	isl_schedule_band *band;
	isl_space *space;

	mupa = isl_multi_union_pw_aff_floor(mupa);
	dim = isl_multi_union_pw_aff_size(mupa);
	if (dim < 0)
		goto error;
	ctx = isl_multi_union_pw_aff_get_ctx(mupa);
	band = isl_schedule_band_alloc(ctx);
	if (!band)
		goto error;

	band->n = dim;
	band->coincident = isl_calloc_array(ctx, int, band->n);
	band->mupa = mupa;
	space = isl_space_params_alloc(ctx, 0);
	band->ast_build_options = isl_union_set_empty(space);
	band->anchored = 0;

	if ((band->n && !band->coincident) || !band->ast_build_options)
		return isl_schedule_band_free(band);

	return band;
error:
	isl_multi_union_pw_aff_free(mupa);
	return NULL;
}

/* Create a duplicate of the given isl_schedule_band.
 */
__isl_give isl_schedule_band *isl_schedule_band_dup(
	__isl_keep isl_schedule_band *band)
{
	int i;
	isl_ctx *ctx;
	isl_schedule_band *dup;

	if (!band)
		return NULL;

	ctx = isl_schedule_band_get_ctx(band);
	dup = isl_schedule_band_alloc(ctx);
	if (!dup)
		return NULL;

	dup->n = band->n;
	dup->coincident = isl_alloc_array(ctx, int, band->n);
	if (band->n && !dup->coincident)
		return isl_schedule_band_free(dup);

	for (i = 0; i < band->n; ++i)
		dup->coincident[i] = band->coincident[i];
	dup->permutable = band->permutable;

	dup->mupa = isl_multi_union_pw_aff_copy(band->mupa);
	dup->ast_build_options = isl_union_set_copy(band->ast_build_options);
	if (!dup->mupa || !dup->ast_build_options)
		return isl_schedule_band_free(dup);

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

	return dup;
}

/* Return an isl_schedule_band that is equal to "band" and that has only
 * a single reference.
 */
__isl_give isl_schedule_band *isl_schedule_band_cow(
	__isl_take isl_schedule_band *band)
{
	if (!band)
		return NULL;

	if (band->ref == 1)
		return band;
	band->ref--;
	return isl_schedule_band_dup(band);
}

/* Return a new reference to "band".
 */
__isl_give isl_schedule_band *isl_schedule_band_copy(
	__isl_keep isl_schedule_band *band)
{
	if (!band)
		return NULL;

	band->ref++;
	return band;
}

/* Free a reference to "band" and return NULL.
 */
__isl_null isl_schedule_band *isl_schedule_band_free(
	__isl_take isl_schedule_band *band)
{
	if (!band)
		return NULL;

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

	isl_multi_union_pw_aff_free(band->mupa);
	isl_union_set_free(band->ast_build_options);
	free(band->loop_type);
	free(band->isolate_loop_type);
	free(band->coincident);
	free(band);

	return NULL;
}

/* Are "band1" and "band2" obviously equal?
 */
isl_bool isl_schedule_band_plain_is_equal(__isl_keep isl_schedule_band *band1,
	__isl_keep isl_schedule_band *band2)
{
	int i;
	isl_bool equal;

	if (!band1 || !band2)
		return isl_bool_error;
	if (band1 == band2)
		return isl_bool_true;

	if (band1->n != band2->n)
		return isl_bool_false;
	for (i = 0; i < band1->n; ++i)
		if (band1->coincident[i] != band2->coincident[i])
			return isl_bool_false;
	if (band1->permutable != band2->permutable)
		return isl_bool_false;

	equal = isl_multi_union_pw_aff_plain_is_equal(band1->mupa, band2->mupa);
	if (equal < 0 || !equal)
		return equal;

	if (!band1->loop_type != !band2->loop_type)
		return isl_bool_false;
	if (band1->loop_type)
		for (i = 0; i < band1->n; ++i)
			if (band1->loop_type[i] != band2->loop_type[i])
				return isl_bool_false;

	if (!band1->isolate_loop_type != !band2->isolate_loop_type)
		return isl_bool_false;
	if (band1->isolate_loop_type)
		for (i = 0; i < band1->n; ++i)
			if (band1->isolate_loop_type[i] !=
						band2->isolate_loop_type[i])
				return isl_bool_false;

	return isl_union_set_is_equal(band1->ast_build_options,
					band2->ast_build_options);
}

/* Return the number of scheduling dimensions in the band.
 */
isl_size isl_schedule_band_n_member(__isl_keep isl_schedule_band *band)
{
	return band ? band->n : isl_size_error;
}

/* Is the given scheduling dimension coincident within the band and
 * with respect to the coincidence constraints?
 */
isl_bool isl_schedule_band_member_get_coincident(
	__isl_keep isl_schedule_band *band, int pos)
{
	if (!band)
		return isl_bool_error;

	if (pos < 0 || pos >= band->n)
		isl_die(isl_schedule_band_get_ctx(band), isl_error_invalid,
			"invalid member position", return isl_bool_error);

	return isl_bool_ok(band->coincident[pos]);
}

/* Mark the given scheduling dimension as being coincident or not
 * according to "coincident".
 */
__isl_give isl_schedule_band *isl_schedule_band_member_set_coincident(
	__isl_take isl_schedule_band *band, int pos, int coincident)
{
	if (!band)
		return NULL;
	if (isl_schedule_band_member_get_coincident(band, pos) == coincident)
		return band;
	band = isl_schedule_band_cow(band);
	if (!band)
		return NULL;

	if (pos < 0 || pos >= band->n)
		isl_die(isl_schedule_band_get_ctx(band), isl_error_invalid,
			"invalid member position",
			return isl_schedule_band_free(band));

	band->coincident[pos] = coincident;

	return band;
}

/* Is the schedule band mark permutable?
 */
isl_bool isl_schedule_band_get_permutable(__isl_keep isl_schedule_band *band)
{
	if (!band)
		return isl_bool_error;
	return isl_bool_ok(band->permutable);
}

/* Mark the schedule band permutable or not according to "permutable"?
 */
__isl_give isl_schedule_band *isl_schedule_band_set_permutable(
	__isl_take isl_schedule_band *band, int permutable)
{
	if (!band)
		return NULL;
	if (band->permutable == permutable)
		return band;
	band = isl_schedule_band_cow(band);
	if (!band)
		return NULL;

	band->permutable = permutable;

	return band;
}

/* Is the band node "node" anchored?  That is, does it reference
 * the outer band nodes?
 */
int isl_schedule_band_is_anchored(__isl_keep isl_schedule_band *band)
{
	return band ? band->anchored : -1;
}

/* Return the schedule space of the band.
 */
__isl_give isl_space *isl_schedule_band_get_space(
	__isl_keep isl_schedule_band *band)
{
	if (!band)
		return NULL;
	return isl_multi_union_pw_aff_get_space(band->mupa);
}

/* Intersect the domain of the band schedule of "band" with "domain".
 */
__isl_give isl_schedule_band *isl_schedule_band_intersect_domain(
	__isl_take isl_schedule_band *band, __isl_take isl_union_set *domain)
{
	band = isl_schedule_band_cow(band);
	if (!band || !domain)
		goto error;

	band->mupa = isl_multi_union_pw_aff_intersect_domain(band->mupa,
								domain);
	if (!band->mupa)
		return isl_schedule_band_free(band);

	return band;
error:
	isl_schedule_band_free(band);
	isl_union_set_free(domain);
	return NULL;
}

/* Return the schedule of the band in isolation.
 */
__isl_give isl_multi_union_pw_aff *isl_schedule_band_get_partial_schedule(
	__isl_keep isl_schedule_band *band)
{
	return band ? isl_multi_union_pw_aff_copy(band->mupa) : NULL;
}

/* Replace the schedule of "band" by "schedule".
 */
__isl_give isl_schedule_band *isl_schedule_band_set_partial_schedule(
	__isl_take isl_schedule_band *band,
	__isl_take isl_multi_union_pw_aff *schedule)
{
	band = isl_schedule_band_cow(band);
	if (!band || !schedule)
		goto error;

	isl_multi_union_pw_aff_free(band->mupa);
	band->mupa = schedule;

	return band;
error:
	isl_schedule_band_free(band);
	isl_multi_union_pw_aff_free(schedule);
	return NULL;
}

/* Return the loop AST generation type for the band member of "band"
 * at position "pos".
 */
enum isl_ast_loop_type isl_schedule_band_member_get_ast_loop_type(
	__isl_keep isl_schedule_band *band, int pos)
{
	if (!band)
		return isl_ast_loop_error;

	if (pos < 0 || pos >= band->n)
		isl_die(isl_schedule_band_get_ctx(band), isl_error_invalid,
			"invalid member position", return isl_ast_loop_error);

	if (!band->loop_type)
		return isl_ast_loop_default;

	return band->loop_type[pos];
}

/* Set the loop AST generation type for the band member of "band"
 * at position "pos" to "type".
 */
__isl_give isl_schedule_band *isl_schedule_band_member_set_ast_loop_type(
	__isl_take isl_schedule_band *band, int pos,
	enum isl_ast_loop_type type)
{
	if (!band)
		return NULL;
	if (isl_schedule_band_member_get_ast_loop_type(band, pos) == type)
		return band;

	if (pos < 0 || pos >= band->n)
		isl_die(isl_schedule_band_get_ctx(band), isl_error_invalid,
			"invalid member position",
			return isl_schedule_band_free(band));

	band = isl_schedule_band_cow(band);
	if (!band)
		return isl_schedule_band_free(band);

	if (!band->loop_type) {
		isl_ctx *ctx;

		ctx = isl_schedule_band_get_ctx(band);
		band->loop_type = isl_calloc_array(ctx,
					    enum isl_ast_loop_type, band->n);
		if (band->n && !band->loop_type)
			return isl_schedule_band_free(band);
	}

	band->loop_type[pos] = type;

	return band;
}

/* Return the loop AST generation type for the band member of "band"
 * at position "pos" for the part that has been isolated by the isolate option.
 */
enum isl_ast_loop_type isl_schedule_band_member_get_isolate_ast_loop_type(
	__isl_keep isl_schedule_band *band, int pos)
{
	if (!band)
		return isl_ast_loop_error;

	if (pos < 0 || pos >= band->n)
		isl_die(isl_schedule_band_get_ctx(band), isl_error_invalid,
			"invalid member position", return isl_ast_loop_error);

	if (!band->isolate_loop_type)
		return isl_ast_loop_default;

	return band->isolate_loop_type[pos];
}

/* Set the loop AST generation type for the band member of "band"
 * at position "pos" to "type" for the part that has been isolated
 * by the isolate option.
 */
__isl_give isl_schedule_band *
isl_schedule_band_member_set_isolate_ast_loop_type(
	__isl_take isl_schedule_band *band, int pos,
	enum isl_ast_loop_type type)
{
	if (!band)
		return NULL;
	if (isl_schedule_band_member_get_isolate_ast_loop_type(band, pos) ==
									type)
		return band;

	if (pos < 0 || pos >= band->n)
		isl_die(isl_schedule_band_get_ctx(band), isl_error_invalid,
			"invalid member position",
			return isl_schedule_band_free(band));

	band = isl_schedule_band_cow(band);
	if (!band)
		return isl_schedule_band_free(band);

	if (!band->isolate_loop_type) {
		isl_ctx *ctx;

		ctx = isl_schedule_band_get_ctx(band);
		band->isolate_loop_type = isl_calloc_array(ctx,
					    enum isl_ast_loop_type, band->n);
		if (band->n && !band->isolate_loop_type)
			return isl_schedule_band_free(band);
	}

	band->isolate_loop_type[pos] = type;

	return band;
}

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

/* Given a parameter space "space", extend it to a set space
 *
 *	{ type[x] }
 *
 * or
 *
 *	{ [isolate[] -> type[x]] }
 *
 * depending on whether "isolate" is set.
 * These can be used to encode loop AST generation options of the given type.
 */
static __isl_give isl_space *loop_type_space(__isl_take isl_space *space,
	enum isl_ast_loop_type type, int isolate)
{
	const char *name;

	name = option_str[type];
	space = isl_space_set_from_params(space);
	space = isl_space_add_dims(space, isl_dim_set, 1);
	space = isl_space_set_tuple_name(space, isl_dim_set, name);
	if (!isolate)
		return space;
	space = isl_space_from_range(space);
	space = isl_space_set_tuple_name(space, isl_dim_in, "isolate");
	space = isl_space_wrap(space);

	return space;
}

/* Add encodings of the "n" loop AST generation options "type" to "options".
 * If "isolate" is set, then these options refer to the isolated part.
 *
 * In particular, for each sequence of consecutive identical types "t",
 * different from the default, add an option
 *
 *	{ t[x] : first <= x <= last }
 *
 * or
 *
 *	{ [isolate[] -> t[x]] : first <= x <= last }
 */
static __isl_give isl_union_set *add_loop_types(
	__isl_take isl_union_set *options, int n, enum isl_ast_loop_type *type,
	int isolate)
{
	int i;

	if (!type)
		return options;
	if (!options)
		return NULL;

	for (i = 0; i < n; ++i) {
		int first;
		isl_space *space;
		isl_set *option;

		if (type[i] == isl_ast_loop_default)
			continue;

		first = i;
		while (i + 1 < n && type[i + 1] == type[i])
			++i;

		space = isl_union_set_get_space(options);
		space = loop_type_space(space, type[i], isolate);
		option = isl_set_universe(space);
		option = isl_set_lower_bound_si(option, isl_dim_set, 0, first);
		option = isl_set_upper_bound_si(option, isl_dim_set, 0, i);
		options = isl_union_set_add_set(options, option);
	}

	return options;
}

/* Return the AST build options associated to "band".
 */
__isl_give isl_union_set *isl_schedule_band_get_ast_build_options(
	__isl_keep isl_schedule_band *band)
{
	isl_union_set *options;

	if (!band)
		return NULL;

	options = isl_union_set_copy(band->ast_build_options);
	options = add_loop_types(options, band->n, band->loop_type, 0);
	options = add_loop_types(options, band->n, band->isolate_loop_type, 1);

	return options;
}

/* Internal data structure for not().
 */
struct isl_not_data {
	isl_bool (*is)(__isl_keep isl_set *set);
};

/* Does "set" not satisfy data->is()?
 */
static isl_bool not(__isl_keep isl_set *set, void *user)
{
	struct isl_not_data *data = user;

	return isl_bool_not(data->is(set));
}

/* Does "uset" contain any set that satisfies "is"?
 * In other words, is it not the case that all of them do not satisfy "is"?
 */
static isl_bool has_any(__isl_keep isl_union_set *uset,
	isl_bool (*is)(__isl_keep isl_set *set))
{
	struct isl_not_data data = { is };

	return isl_bool_not(isl_union_set_every_set(uset, &not, &data));
}

/* Does "set" live in a space of the form
 *
 *	isolate[[...] -> [...]]
 *
 * ?
 */
static isl_bool is_isolate(__isl_keep isl_set *set)
{
	if (isl_set_has_tuple_name(set)) {
		const char *name;
		name = isl_set_get_tuple_name(set);
		if (isl_set_is_wrapping(set) && !strcmp(name, "isolate"))
			return isl_bool_true;
	}

	return isl_bool_false;
}

/* Does "options" include an option of the ofrm
 *
 *	isolate[[...] -> [...]]
 *
 * ?
 */
static isl_bool has_isolate_option(__isl_keep isl_union_set *options)
{
	return has_any(options, &is_isolate);
}

/* Does "set" encode a loop AST generation option?
 */
static isl_bool is_loop_type_option(__isl_keep isl_set *set)
{
	isl_size dim;

	dim = isl_set_dim(set, isl_dim_set);
	if (dim < 0)
		return isl_bool_error;
	if (dim == 1 && isl_set_has_tuple_name(set)) {
		const char *name;
		enum isl_ast_loop_type type;
		name = isl_set_get_tuple_name(set);
		for (type = isl_ast_loop_atomic;
		    type <= isl_ast_loop_separate; ++type) {
			if (strcmp(name, option_str[type]))
				continue;
			return isl_bool_true;
		}
	}

	return isl_bool_false;
}

/* Does "set" encode a loop AST generation option for the isolated part?
 * That is, is of the form
 *
 *	{ [isolate[] -> t[x]] }
 *
 * with t equal to "atomic", "unroll" or "separate"?
 */
static isl_bool is_isolate_loop_type_option(__isl_keep isl_set *set)
{
	const char *name;
	enum isl_ast_loop_type type;
	isl_map *map;

	if (!isl_set_is_wrapping(set))
		return isl_bool_false;
	map = isl_set_unwrap(isl_set_copy(set));
	if (!isl_map_has_tuple_name(map, isl_dim_in) ||
	    !isl_map_has_tuple_name(map, isl_dim_out)) {
		isl_map_free(map);
		return isl_bool_false;
	}
	name = isl_map_get_tuple_name(map, isl_dim_in);
	if (!strcmp(name, "isolate")) {
		name = isl_map_get_tuple_name(map, isl_dim_out);
		for (type = isl_ast_loop_atomic;
		    type <= isl_ast_loop_separate; ++type) {
			if (strcmp(name, option_str[type]))
				continue;
			isl_map_free(map);
			return isl_bool_true;
		}
	}
	isl_map_free(map);

	return isl_bool_false;
}

/* Does "options" encode any loop AST generation options
 * for the isolated part?
 */
static isl_bool has_isolate_loop_type_options(__isl_keep isl_union_set *options)
{
	return has_any(options, &is_isolate_loop_type_option);
}

/* Does "options" encode any loop AST generation options?
 */
static isl_bool has_loop_type_options(__isl_keep isl_union_set *options)
{
	return has_any(options, &is_loop_type_option);
}

/* Extract the loop AST generation type for the band member
 * at position "pos" from "options".
 * If "isolate" is set, then extract the loop types for the isolated part.
 */
static enum isl_ast_loop_type extract_loop_type(
	__isl_keep isl_union_set *options, int pos, int isolate)
{
	isl_ctx *ctx;
	enum isl_ast_loop_type type, res = isl_ast_loop_default;

	ctx = isl_union_set_get_ctx(options);
	for (type = isl_ast_loop_atomic;
	    type <= isl_ast_loop_separate; ++type) {
		isl_space *space;
		isl_set *option;
		int empty;

		space = isl_union_set_get_space(options);
		space = loop_type_space(space, type, isolate);
		option = isl_union_set_extract_set(options, space);
		option = isl_set_fix_si(option, isl_dim_set, 0, pos);
		empty = isl_set_is_empty(option);
		isl_set_free(option);

		if (empty < 0)
			return isl_ast_loop_error;
		if (empty)
			continue;
		if (res != isl_ast_loop_default)
			isl_die(ctx, isl_error_invalid,
				"conflicting loop type options",
				return isl_ast_loop_error);
		res = type;
	}

	return res;
}

/* Extract the loop AST generation types for the members of "band"
 * from "options" and store them in band->loop_type.
 * Return -1 on error.
 */
static int extract_loop_types(__isl_keep isl_schedule_band *band,
	__isl_keep isl_union_set *options)
{
	int i;

	if (!band->loop_type) {
		isl_ctx *ctx = isl_schedule_band_get_ctx(band);
		band->loop_type = isl_alloc_array(ctx,
					    enum isl_ast_loop_type, band->n);
		if (band->n && !band->loop_type)
			return -1;
	}
	for (i = 0; i < band->n; ++i) {
		band->loop_type[i] = extract_loop_type(options, i, 0);
		if (band->loop_type[i] == isl_ast_loop_error)
			return -1;
	}

	return 0;
}

/* Extract the loop AST generation types for the members of "band"
 * from "options" for the isolated part and
 * store them in band->isolate_loop_type.
 * Return -1 on error.
 */
static int extract_isolate_loop_types(__isl_keep isl_schedule_band *band,
	__isl_keep isl_union_set *options)
{
	int i;

	if (!band->isolate_loop_type) {
		isl_ctx *ctx = isl_schedule_band_get_ctx(band);
		band->isolate_loop_type = isl_alloc_array(ctx,
					    enum isl_ast_loop_type, band->n);
		if (band->n && !band->isolate_loop_type)
			return -1;
	}
	for (i = 0; i < band->n; ++i) {
		band->isolate_loop_type[i] = extract_loop_type(options, i, 1);
		if (band->isolate_loop_type[i] == isl_ast_loop_error)
			return -1;
	}

	return 0;
}

/* Construct universe sets of the spaces that encode loop AST generation
 * types (for the isolated part if "isolate" is set).  That is, construct
 *
 *	{ atomic[x]; separate[x]; unroll[x] }
 *
 * or
 *
 *	{ [isolate[] -> atomic[x]]; [isolate[] -> separate[x]];
 *	  [isolate[] -> unroll[x]] }
 */
static __isl_give isl_union_set *loop_types(__isl_take isl_space *space,
	int isolate)
{
	enum isl_ast_loop_type type;
	isl_union_set *types;

	types = isl_union_set_empty(space);
	for (type = isl_ast_loop_atomic;
	    type <= isl_ast_loop_separate; ++type) {
		isl_set *set;

		space = isl_union_set_get_space(types);
		space = loop_type_space(space, type, isolate);
		set = isl_set_universe(space);
		types = isl_union_set_add_set(types, set);
	}

	return types;
}

/* Remove all elements from spaces that encode loop AST generation types
 * from "options".
 */
static __isl_give isl_union_set *clear_loop_types(
	__isl_take isl_union_set *options)
{
	isl_union_set *types;

	types = loop_types(isl_union_set_get_space(options), 0);
	options = isl_union_set_subtract(options, types);

	return options;
}

/* Remove all elements from spaces that encode loop AST generation types
 * for the isolated part from "options".
 */
static __isl_give isl_union_set *clear_isolate_loop_types(
	__isl_take isl_union_set *options)
{
	isl_union_set *types;

	types = loop_types(isl_union_set_get_space(options), 1);
	options = isl_union_set_subtract(options, types);

	return options;
}

/* Replace the AST build options associated to "band" by "options".
 * If there are any loop AST generation type options, then they
 * are extracted and stored in band->loop_type.  Otherwise,
 * band->loop_type is removed to indicate that the default applies
 * to all members.  Similarly for the loop AST generation type options
 * for the isolated part, which are stored in band->isolate_loop_type.
 * The remaining options are stored in band->ast_build_options.
 *
 * Set anchored if the options include an isolate option since the
 * domain of the wrapped map references the outer band node schedules.
 */
__isl_give isl_schedule_band *isl_schedule_band_set_ast_build_options(
	__isl_take isl_schedule_band *band, __isl_take isl_union_set *options)
{
	isl_bool has_isolate, has_loop_type, has_isolate_loop_type;

	band = isl_schedule_band_cow(band);
	if (!band || !options)
		goto error;
	has_isolate = has_isolate_option(options);
	if (has_isolate < 0)
		goto error;
	has_loop_type = has_loop_type_options(options);
	if (has_loop_type < 0)
		goto error;
	has_isolate_loop_type = has_isolate_loop_type_options(options);
	if (has_isolate_loop_type < 0)
		goto error;

	if (!has_loop_type) {
		free(band->loop_type);
		band->loop_type = NULL;
	} else {
		if (extract_loop_types(band, options) < 0)
			goto error;
		options = clear_loop_types(options);
		if (!options)
			goto error;
	}

	if (!has_isolate_loop_type) {
		free(band->isolate_loop_type);
		band->isolate_loop_type = NULL;
	} else {
		if (extract_isolate_loop_types(band, options) < 0)
			goto error;
		options = clear_isolate_loop_types(options);
		if (!options)
			goto error;
	}

	isl_union_set_free(band->ast_build_options);
	band->ast_build_options = options;
	band->anchored = has_isolate;

	return band;
error:
	isl_schedule_band_free(band);
	isl_union_set_free(options);
	return NULL;
}

/* Return the "isolate" option associated to "band", assuming
 * it at appears at schedule depth "depth".
 *
 * The isolate option is of the form
 *
 *	isolate[[flattened outer bands] -> band]
 */
__isl_give isl_set *isl_schedule_band_get_ast_isolate_option(
	__isl_keep isl_schedule_band *band, int depth)
{
	isl_space *space;
	isl_set *isolate;

	if (!band)
		return NULL;

	space = isl_schedule_band_get_space(band);
	space = isl_space_from_range(space);
	space = isl_space_add_dims(space, isl_dim_in, depth);
	space = isl_space_wrap(space);
	space = isl_space_set_tuple_name(space, isl_dim_set, "isolate");

	isolate = isl_union_set_extract_set(band->ast_build_options, space);

	return isolate;
}

/* Replace the option "drop" in the AST build options by "add".
 * That is, remove "drop" and add "add".
 */
__isl_give isl_schedule_band *isl_schedule_band_replace_ast_build_option(
	__isl_take isl_schedule_band *band, __isl_take isl_set *drop,
	__isl_take isl_set *add)
{
	isl_union_set *options;

	band = isl_schedule_band_cow(band);
	if (!band)
		goto error;

	options = band->ast_build_options;
	options = isl_union_set_subtract(options, isl_union_set_from_set(drop));
	options = isl_union_set_union(options, isl_union_set_from_set(add));
	band->ast_build_options = options;

	if (!band->ast_build_options)
		return isl_schedule_band_free(band);

	return band;
error:
	isl_schedule_band_free(band);
	isl_set_free(drop);
	isl_set_free(add);
	return NULL;
}

/* Multiply the partial schedule of "band" with the factors in "mv".
 * Replace the result by its greatest integer part to ensure
 * that the schedule is always integral.
 */
__isl_give isl_schedule_band *isl_schedule_band_scale(
	__isl_take isl_schedule_band *band, __isl_take isl_multi_val *mv)
{
	band = isl_schedule_band_cow(band);
	if (!band || !mv)
		goto error;
	band->mupa = isl_multi_union_pw_aff_scale_multi_val(band->mupa, mv);
	band->mupa = isl_multi_union_pw_aff_floor(band->mupa);
	if (!band->mupa)
		return isl_schedule_band_free(band);
	return band;
error:
	isl_schedule_band_free(band);
	isl_multi_val_free(mv);
	return NULL;
}

/* Divide the partial schedule of "band" by the factors in "mv".
 * Replace the result by its greatest integer part to ensure
 * that the schedule is always integral.
 */
__isl_give isl_schedule_band *isl_schedule_band_scale_down(
	__isl_take isl_schedule_band *band, __isl_take isl_multi_val *mv)
{
	band = isl_schedule_band_cow(band);
	if (!band || !mv)
		goto error;
	band->mupa = isl_multi_union_pw_aff_scale_down_multi_val(band->mupa,
								mv);
	band->mupa = isl_multi_union_pw_aff_floor(band->mupa);
	if (!band->mupa)
		return isl_schedule_band_free(band);
	return band;
error:
	isl_schedule_band_free(band);
	isl_multi_val_free(mv);
	return NULL;
}

/* Reduce the partial schedule of "band" modulo the factors in "mv".
 */
__isl_give isl_schedule_band *isl_schedule_band_mod(
	__isl_take isl_schedule_band *band, __isl_take isl_multi_val *mv)
{
	band = isl_schedule_band_cow(band);
	if (!band || !mv)
		goto error;
	band->mupa = isl_multi_union_pw_aff_mod_multi_val(band->mupa, mv);
	if (!band->mupa)
		return isl_schedule_band_free(band);
	return band;
error:
	isl_schedule_band_free(band);
	isl_multi_val_free(mv);
	return NULL;
}

/* Shift the partial schedule of "band" by "shift" after checking
 * that the domain of the partial schedule would not be affected
 * by this shift.
 */
__isl_give isl_schedule_band *isl_schedule_band_shift(
	__isl_take isl_schedule_band *band,
	__isl_take isl_multi_union_pw_aff *shift)
{
	isl_union_set *dom1, *dom2;
	isl_bool subset;

	band = isl_schedule_band_cow(band);
	if (!band || !shift)
		goto error;
	dom1 = isl_multi_union_pw_aff_domain(
				isl_multi_union_pw_aff_copy(band->mupa));
	dom2 = isl_multi_union_pw_aff_domain(
				isl_multi_union_pw_aff_copy(shift));
	subset = isl_union_set_is_subset(dom1, dom2);
	isl_union_set_free(dom1);
	isl_union_set_free(dom2);
	if (subset < 0)
		goto error;
	if (!subset)
		isl_die(isl_schedule_band_get_ctx(band), isl_error_invalid,
			"domain of shift needs to include domain of "
			"partial schedule", goto error);
	band->mupa = isl_multi_union_pw_aff_add(band->mupa, shift);
	if (!band->mupa)
		return isl_schedule_band_free(band);
	return band;
error:
	isl_schedule_band_free(band);
	isl_multi_union_pw_aff_free(shift);
	return NULL;
}

/* Given the schedule of a band, construct the corresponding
 * schedule for the tile loops based on the given tile sizes
 * and return the result.
 *
 * If the scale tile loops options is set, then the tile loops
 * are scaled by the tile sizes.
 *
 * That is replace each schedule dimension "i" by either
 * "floor(i/s)" or "s * floor(i/s)".
 */
static isl_multi_union_pw_aff *isl_multi_union_pw_aff_tile(
	__isl_take isl_multi_union_pw_aff *sched,
	__isl_take isl_multi_val *sizes)
{
	isl_ctx *ctx;
	int i;
	isl_size n;
	isl_val *v;
	int scale;

	ctx = isl_multi_val_get_ctx(sizes);
	scale = isl_options_get_tile_scale_tile_loops(ctx);

	n = isl_multi_union_pw_aff_size(sched);
	if (n < 0)
		sched = isl_multi_union_pw_aff_free(sched);
	for (i = 0; i < n; ++i) {
		isl_union_pw_aff *upa;

		upa = isl_multi_union_pw_aff_get_union_pw_aff(sched, i);
		v = isl_multi_val_get_val(sizes, i);

		upa = isl_union_pw_aff_scale_down_val(upa, isl_val_copy(v));
		upa = isl_union_pw_aff_floor(upa);
		if (scale)
			upa = isl_union_pw_aff_scale_val(upa, isl_val_copy(v));
		isl_val_free(v);

		sched = isl_multi_union_pw_aff_set_union_pw_aff(sched, i, upa);
	}

	isl_multi_val_free(sizes);
	return sched;
}

/* Replace "band" by a band corresponding to the tile loops of a tiling
 * with the given tile sizes.
 */
__isl_give isl_schedule_band *isl_schedule_band_tile(
	__isl_take isl_schedule_band *band, __isl_take isl_multi_val *sizes)
{
	band = isl_schedule_band_cow(band);
	if (!band || !sizes)
		goto error;
	band->mupa = isl_multi_union_pw_aff_tile(band->mupa, sizes);
	if (!band->mupa)
		return isl_schedule_band_free(band);
	return band;
error:
	isl_schedule_band_free(band);
	isl_multi_val_free(sizes);
	return NULL;
}

/* Replace "band" by a band corresponding to the point loops of a tiling
 * with the given tile sizes.
 * "tile" is the corresponding tile loop band.
 *
 * If the shift point loops option is set, then the point loops
 * are shifted to start at zero.  That is, each schedule dimension "i"
 * is replaced by "i - s * floor(i/s)".
 * The expression "floor(i/s)" (or "s * floor(i/s)") is extracted from
 * the tile band.
 *
 * Otherwise, the band is left untouched.
 */
__isl_give isl_schedule_band *isl_schedule_band_point(
	__isl_take isl_schedule_band *band, __isl_keep isl_schedule_band *tile,
	__isl_take isl_multi_val *sizes)
{
	isl_ctx *ctx;
	isl_multi_union_pw_aff *scaled;

	if (!band || !sizes)
		goto error;

	ctx = isl_schedule_band_get_ctx(band);
	if (!isl_options_get_tile_shift_point_loops(ctx)) {
		isl_multi_val_free(sizes);
		return band;
	}
	band = isl_schedule_band_cow(band);
	if (!band)
		goto error;

	scaled = isl_schedule_band_get_partial_schedule(tile);
	if (!isl_options_get_tile_scale_tile_loops(ctx))
		scaled = isl_multi_union_pw_aff_scale_multi_val(scaled, sizes);
	else
		isl_multi_val_free(sizes);
	band->mupa = isl_multi_union_pw_aff_sub(band->mupa, scaled);
	if (!band->mupa)
		return isl_schedule_band_free(band);
	return band;
error:
	isl_schedule_band_free(band);
	isl_multi_val_free(sizes);
	return NULL;
}

/* Drop the "n" dimensions starting at "pos" from "band".
 *
 * We apply the transformation even if "n" is zero to ensure consistent
 * behavior with respect to changes in the schedule space.
 *
 * The caller is responsible for updating the isolate option.
 */
__isl_give isl_schedule_band *isl_schedule_band_drop(
	__isl_take isl_schedule_band *band, int pos, int n)
{
	int i;

	if (pos < 0 || n < 0 || pos + n > band->n)
		isl_die(isl_schedule_band_get_ctx(band), isl_error_internal,
			"range out of bounds",
			return isl_schedule_band_free(band));

	band = isl_schedule_band_cow(band);
	if (!band)
		return NULL;

	band->mupa = isl_multi_union_pw_aff_drop_dims(band->mupa,
							isl_dim_set, pos, n);
	if (!band->mupa)
		return isl_schedule_band_free(band);

	for (i = pos + n; i < band->n; ++i)
		band->coincident[i - n] = band->coincident[i];
	if (band->loop_type)
		for (i = pos + n; i < band->n; ++i)
			band->loop_type[i - n] = band->loop_type[i];
	if (band->isolate_loop_type)
		for (i = pos + n; i < band->n; ++i)
			band->isolate_loop_type[i - n] =
						    band->isolate_loop_type[i];

	band->n -= n;

	return band;
}

/* Reset the user pointer on all identifiers of parameters and tuples
 * in "band".
 */
__isl_give isl_schedule_band *isl_schedule_band_reset_user(
	__isl_take isl_schedule_band *band)
{
	band = isl_schedule_band_cow(band);
	if (!band)
		return NULL;

	band->mupa = isl_multi_union_pw_aff_reset_user(band->mupa);
	band->ast_build_options =
		isl_union_set_reset_user(band->ast_build_options);
	if (!band->mupa || !band->ast_build_options)
		return isl_schedule_band_free(band);

	return band;
}

/* Align the parameters of "band" to those of "space".
 */
__isl_give isl_schedule_band *isl_schedule_band_align_params(
	__isl_take isl_schedule_band *band, __isl_take isl_space *space)
{
	band = isl_schedule_band_cow(band);
	if (!band || !space)
		goto error;

	band->mupa = isl_multi_union_pw_aff_align_params(band->mupa,
						isl_space_copy(space));
	band->ast_build_options =
		isl_union_set_align_params(band->ast_build_options, space);
	if (!band->mupa || !band->ast_build_options)
		return isl_schedule_band_free(band);

	return band;
error:
	isl_space_free(space);
	isl_schedule_band_free(band);
	return NULL;
}

/* Compute the pullback of "band" by the function represented by "upma".
 * In other words, plug in "upma" in the iteration domains of "band".
 */
__isl_give isl_schedule_band *isl_schedule_band_pullback_union_pw_multi_aff(
	__isl_take isl_schedule_band *band,
	__isl_take isl_union_pw_multi_aff *upma)
{
	band = isl_schedule_band_cow(band);
	if (!band || !upma)
		goto error;

	band->mupa =
		isl_multi_union_pw_aff_pullback_union_pw_multi_aff(band->mupa,
									upma);
	if (!band->mupa)
		return isl_schedule_band_free(band);

	return band;
error:
	isl_union_pw_multi_aff_free(upma);
	isl_schedule_band_free(band);
	return NULL;
}

/* Compute the gist of "band" with respect to "context".
 * In particular, compute the gist of the associated partial schedule.
 */
__isl_give isl_schedule_band *isl_schedule_band_gist(
	__isl_take isl_schedule_band *band, __isl_take isl_union_set *context)
{
	if (!band || !context)
		goto error;
	if (band->n == 0) {
		isl_union_set_free(context);
		return band;
	}
	band = isl_schedule_band_cow(band);
	if (!band)
		goto error;
	band->mupa = isl_multi_union_pw_aff_gist(band->mupa, context);
	if (!band->mupa)
		return isl_schedule_band_free(band);
	return band;
error:
	isl_union_set_free(context);
	isl_schedule_band_free(band);
	return NULL;
}