isl_union_templ.c 32 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
/*
 * Copyright 2010      INRIA Saclay
 * Copyright 2013      Ecole Normale Superieure
 *
 * Use of this software is governed by the MIT license
 *
 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
 * 91893 Orsay, France 
 * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
 */

#undef TYPE
#define TYPE	UNION
static
#include "has_single_reference_templ.c"

__isl_give UNION *FN(UNION,cow)(__isl_take UNION *u);

isl_ctx *FN(UNION,get_ctx)(__isl_keep UNION *u)
{
	return u ? u->space->ctx : NULL;
}

/* Return the space of "u".
 */
static __isl_keep isl_space *FN(UNION,peek_space)(__isl_keep UNION *u)
{
	if (!u)
		return NULL;
	return u->space;
}

/* Return a copy of the space of "u".
 */
__isl_give isl_space *FN(UNION,get_space)(__isl_keep UNION *u)
{
	return isl_space_copy(FN(UNION,peek_space)(u));
}

/* Return the number of parameters of "u", where "type"
 * is required to be set to isl_dim_param.
 */
isl_size FN(UNION,dim)(__isl_keep UNION *u, enum isl_dim_type type)
{
	if (!u)
		return isl_size_error;

	if (type != isl_dim_param)
		isl_die(FN(UNION,get_ctx)(u), isl_error_invalid,
			"can only reference parameters", return isl_size_error);

	return isl_space_dim(u->space, type);
}

/* Return the position of the parameter with the given name
 * in "u".
 * Return -1 if no such dimension can be found.
 */
int FN(UNION,find_dim_by_name)(__isl_keep UNION *u, enum isl_dim_type type,
	const char *name)
{
	if (!u)
		return -1;
	return isl_space_find_dim_by_name(u->space, type, name);
}

#include "opt_type.h"

static __isl_give UNION *FN(UNION,alloc)(__isl_take isl_space *space
	OPT_TYPE_PARAM, int size)
{
	UNION *u;

	space = isl_space_params(space);
	if (!space)
		return NULL;

	u = isl_calloc_type(space->ctx, UNION);
	if (!u)
		goto error;

	u->ref = 1;
	OPT_SET_TYPE(u->, type);
	u->space = space;
	if (isl_hash_table_init(space->ctx, &u->table, size) < 0)
		return FN(UNION,free)(u);

	return u;
error:
	isl_space_free(space);
	return NULL;
}

/* Create an empty/zero union without specifying any parameters.
 */
__isl_give UNION *FN(FN(UNION,ZERO),ctx)(isl_ctx *ctx OPT_TYPE_PARAM)
{
	isl_space *space;

	space = isl_space_unit(ctx);
	return FN(FN(UNION,ZERO),space)(space OPT_TYPE_ARG(NO_LOC));
}

__isl_give UNION *FN(FN(UNION,ZERO),space)(__isl_take isl_space *space
	OPT_TYPE_PARAM)
{
	return FN(UNION,alloc)(space OPT_TYPE_ARG(NO_LOC), 16);
}

/* This is an alternative name for the function above.
 */
__isl_give UNION *FN(UNION,ZERO)(__isl_take isl_space *space OPT_TYPE_PARAM)
{
	return FN(FN(UNION,ZERO),space)(space OPT_TYPE_ARG(NO_LOC));
}

__isl_give UNION *FN(UNION,copy)(__isl_keep UNION *u)
{
	if (!u)
		return NULL;

	u->ref++;
	return u;
}

/* Extract the element of "u" living in "space" (ignoring parameters).
 *
 * Return the ZERO element if "u" does not contain any element
 * living in "space".
 */
__isl_give PART *FN(FN(UNION,extract),BASE)(__isl_keep UNION *u,
	__isl_take isl_space *space)
{
	struct isl_hash_table_entry *entry;

	space = isl_space_replace_params(space, FN(UNION,peek_space)(u));

	entry = FN(UNION,find_part_entry)(u, space, 0);
	if (!entry)
		goto error;
	if (entry == isl_hash_table_entry_none)
		return FN(PART,ZERO)(space OPT_TYPE_ARG(u->));
	isl_space_free(space);
	return FN(PART,copy)(entry->data);
error:
	isl_space_free(space);
	return NULL;
}

/* Add "part" to "u".
 * If "disjoint" is set, then "u" is not allowed to already have
 * a part that is defined over a domain that overlaps with the domain
 * of "part".
 * Otherwise, compute the union sum of "part" and the part in "u"
 * defined on the same space.
 */
static __isl_give UNION *FN(UNION,add_part_generic)(__isl_take UNION *u,
	__isl_take PART *part, int disjoint)
{
	int empty;
	struct isl_hash_table_entry *entry;

	if (!part)
		goto error;

	empty = FN(PART,IS_ZERO)(part);
	if (empty < 0)
		goto error;
	if (empty) {
		FN(PART,free)(part);
		return u;
	}

	u = FN(UNION,align_params)(u, FN(PART,get_space)(part));
	part = FN(PART,align_params)(part, FN(UNION,get_space)(u));

	u = FN(UNION,cow)(u);

	if (!u)
		goto error;

	if (FN(UNION,check_disjoint_domain_other)(u, part) < 0)
		goto error;
	entry = FN(UNION,find_part_entry)(u, part->dim, 1);
	if (!entry)
		goto error;

	if (!entry->data)
		entry->data = part;
	else {
		if (disjoint &&
		    FN(UNION,check_disjoint_domain)(entry->data, part) < 0)
			goto error;
		entry->data = FN(PART,union_add_)(entry->data,
						FN(PART,copy)(part));
		if (!entry->data)
			goto error;
		empty = FN(PART,IS_ZERO)(part);
		if (empty < 0)
			goto error;
		if (empty)
			u = FN(UNION,remove_part_entry)(u, entry);
		FN(PART,free)(part);
	}

	return u;
error:
	FN(PART,free)(part);
	FN(UNION,free)(u);
	return NULL;
}

/* Add "part" to "u", where "u" is assumed not to already have
 * a part that is defined on the same space as "part".
 */
__isl_give UNION *FN(FN(UNION,add),BASE)(__isl_take UNION *u,
	__isl_take PART *part)
{
	return FN(UNION,add_part_generic)(u, part, 1);
}

/* Allocate a UNION with the same type (if any) and the same size as "u" and
 * with space "space".
 */
static __isl_give UNION *FN(UNION,alloc_same_size_on_space)(__isl_keep UNION *u,
	__isl_take isl_space *space)
{
	if (!u)
		goto error;
	return FN(UNION,alloc)(space OPT_TYPE_ARG(u->), u->table.n);
error:
	isl_space_free(space);
	return NULL;
}

/* Allocate a UNION with the same space, the same type (if any) and
 * the same size as "u".
 */
static __isl_give UNION *FN(UNION,alloc_same_size)(__isl_keep UNION *u)
{
	return FN(UNION,alloc_same_size_on_space)(u, FN(UNION,get_space)(u));
}

/* Data structure that specifies how isl_union_*_transform
 * should modify the base expressions in the union expression.
 *
 * If "inplace" is set, then the base expression in the input union
 * are modified in place.  This means that "fn" should not
 * change the meaning of the union or that the union only
 * has a single reference.
 * If "space" is not NULL, then a new union is created in this space.
 * If "filter" is not NULL, then only the base expressions that satisfy "filter"
 * are taken into account.
 * "fn" is applied to each entry in the input.
 * "fn_user" is passed as the second argument to "fn".
 */
S(UNION,transform_control) {
	int inplace;
	isl_space *space;
	isl_bool (*filter)(__isl_keep PART *part);
	__isl_give PART *(*fn)(__isl_take PART *part, void *user);
	void *fn_user;
};

/* Internal data structure for isl_union_*_transform_space.
 * "control" specifies how the base expressions should be modified.
 * "res" collects the results (if control->inplace is not set).
 */
S(UNION,transform_data)
{
	S(UNION,transform_control) *control;
	UNION *res;
};

/* Apply control->fn to "part" and add the result to data->res or
 * place it back into the input union if control->inplace is set.
 */
static isl_stat FN(UNION,transform_entry)(void **entry, void *user)
{
	S(UNION,transform_data) *data = (S(UNION,transform_data) *)user;
	PART *part = *entry;

	if (data->control->filter) {
		isl_bool handle;

		handle = data->control->filter(part);
		if (handle < 0)
			return isl_stat_error;
		if (!handle)
			return isl_stat_ok;
	}

	if (!data->control->inplace)
		part = FN(PART,copy)(part);
	part = data->control->fn(part, data->control->fn_user);
	if (data->control->inplace)
		*entry = part;
	else
		data->res = FN(FN(UNION,add),BASE)(data->res, part);
	if (!part || !data->res)
		return isl_stat_error;

	return isl_stat_ok;
}

/* Return a UNION that is obtained by modifying "u" according to "control".
 */
static __isl_give UNION *FN(UNION,transform)(__isl_take UNION *u,
	S(UNION,transform_control) *control)
{
	S(UNION,transform_data) data = { control };
	isl_space *space;

	if (control->inplace) {
		data.res = u;
	} else {
		if (control->space)
			space = isl_space_copy(control->space);
		else
			space = FN(UNION,get_space)(u);
		data.res = FN(UNION,alloc_same_size_on_space)(u, space);
	}
	if (FN(UNION,foreach_inplace)(u, &FN(UNION,transform_entry), &data) < 0)
		data.res = FN(UNION,free)(data.res);
	if (!control->inplace)
		FN(UNION,free)(u);
	return data.res;
}

/* Return a UNION living in "space" that is otherwise obtained by modifying "u"
 * according to "control".
 */
static __isl_give UNION *FN(UNION,transform_space)(__isl_take UNION *u,
	__isl_take isl_space *space, S(UNION,transform_control) *control)
{
	if (!space)
		return FN(UNION,free)(u);
	control->space = space;
	u = FN(UNION,transform)(u, control);
	isl_space_free(space);
	return u;
}

/* Update "u" by applying "fn" to each entry.
 * This operation is assumed not to change the number of entries nor
 * the spaces of the entries.
 *
 * If there is only one reference to "u", then change "u" inplace.
 * Otherwise, create a new UNION from "u" and discard the original.
 */
static __isl_give UNION *FN(UNION,transform_inplace)(__isl_take UNION *u,
	__isl_give PART *(*fn)(__isl_take PART *part, void *user), void *user)
{
	S(UNION,transform_control) control = { .fn = fn, .fn_user = user };
	isl_bool single_ref;

	single_ref = FN(UNION,has_single_reference)(u);
	if (single_ref < 0)
		return FN(UNION,free)(u);
	if (single_ref)
		control.inplace = 1;
	return FN(UNION,transform)(u, &control);
}

/* An isl_union_*_transform callback for use in isl_union_*_dup
 * that simply returns "part".
 */
static __isl_give PART *FN(UNION,copy_part)(__isl_take PART *part, void *user)
{
	return part;
}

__isl_give UNION *FN(UNION,dup)(__isl_keep UNION *u)
{
	S(UNION,transform_control) control = { .fn = &FN(UNION,copy_part) };

	u = FN(UNION,copy)(u);
	return FN(UNION,transform)(u, &control);
}

__isl_give UNION *FN(UNION,cow)(__isl_take UNION *u)
{
	if (!u)
		return NULL;

	if (u->ref == 1)
		return u;
	u->ref--;
	return FN(UNION,dup)(u);
}

__isl_null UNION *FN(UNION,free)(__isl_take UNION *u)
{
	if (!u)
		return NULL;

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

	isl_hash_table_foreach(u->space->ctx, &u->table,
				&FN(UNION,free_u_entry), NULL);
	isl_hash_table_clear(&u->table);
	isl_space_free(u->space);
	free(u);
	return NULL;
}

static __isl_give PART *FN(UNION,align_entry)(__isl_take PART *part, void *user)
{
	isl_reordering *exp = user;

	exp = isl_reordering_extend_space(isl_reordering_copy(exp),
				    FN(PART,get_domain_space)(part));
	return FN(PART,realign_domain)(part, exp);
}

/* Reorder the parameters of "u" according to the given reordering.
 */
static __isl_give UNION *FN(UNION,realign_domain)(__isl_take UNION *u,
	__isl_take isl_reordering *r)
{
	S(UNION,transform_control) control = {
		.fn = &FN(UNION,align_entry),
		.fn_user = r,
	};
	isl_space *space;

	if (!u || !r)
		goto error;

	space = isl_reordering_get_space(r);
	u = FN(UNION,transform_space)(u, space, &control);
	isl_reordering_free(r);
	return u;
error:
	FN(UNION,free)(u);
	isl_reordering_free(r);
	return NULL;
}

/* Align the parameters of "u" to those of "model".
 */
__isl_give UNION *FN(UNION,align_params)(__isl_take UNION *u,
	__isl_take isl_space *model)
{
	isl_bool equal_params;
	isl_reordering *r;

	if (!u || !model)
		goto error;

	equal_params = isl_space_has_equal_params(u->space, model);
	if (equal_params < 0)
		goto error;
	if (equal_params) {
		isl_space_free(model);
		return u;
	}

	r = isl_parameter_alignment_reordering(u->space, model);
	isl_space_free(model);

	return FN(UNION,realign_domain)(u, r);
error:
	isl_space_free(model);
	FN(UNION,free)(u);
	return NULL;
}

/* Add "part" to *u, taking the union sum if "u" already has
 * a part defined on the same space as "part".
 */
static isl_stat FN(UNION,union_add_part)(__isl_take PART *part, void *user)
{
	UNION **u = (UNION **)user;

	*u = FN(UNION,add_part_generic)(*u, part, 0);

	return isl_stat_ok;
}

/* Compute the sum of "u1" and "u2" on the union of their domains,
 * with the actual sum on the shared domain and
 * the defined expression on the symmetric difference of the domains.
 *
 * This is an internal function that is exposed under different
 * names depending on whether the base expressions have a zero default
 * value.
 * If they do, then this function is called "add".
 * Otherwise, it is called "union_add".
 */
static __isl_give UNION *FN(UNION,union_add_)(__isl_take UNION *u1,
	__isl_take UNION *u2)
{
	u1 = FN(UNION,align_params)(u1, FN(UNION,get_space)(u2));
	u2 = FN(UNION,align_params)(u2, FN(UNION,get_space)(u1));

	u1 = FN(UNION,cow)(u1);

	if (!u1 || !u2)
		goto error;

	if (FN(FN(UNION,foreach),BASE)(u2, &FN(UNION,union_add_part), &u1) < 0)
		goto error;

	FN(UNION,free)(u2);

	return u1;
error:
	FN(UNION,free)(u1);
	FN(UNION,free)(u2);
	return NULL;
}

__isl_give UNION *FN(FN(UNION,from),BASE)(__isl_take PART *part)
{
	isl_space *space;
	UNION *u;

	if (!part)
		return NULL;

	space = FN(PART,get_space)(part);
	space = isl_space_drop_dims(space, isl_dim_in, 0,
					isl_space_dim(space, isl_dim_in));
	space = isl_space_drop_dims(space, isl_dim_out, 0,
					isl_space_dim(space, isl_dim_out));
	u = FN(UNION,ZERO)(space OPT_TYPE_ARG(part->));
	u = FN(FN(UNION,add),BASE)(u, part);

	return u;
}

S(UNION,match_bin_data) {
	UNION *u2;
	UNION *res;
	__isl_give PART *(*fn)(__isl_take PART *, __isl_take PART *);
};

/* Check if data->u2 has an element living in the same space as "part".
 * If so, call data->fn on the two elements and add the result to
 * data->res.
 */
static isl_stat FN(UNION,match_bin_entry)(__isl_take PART *part, void *user)
{
	S(UNION,match_bin_data) *data = user;
	struct isl_hash_table_entry *entry2;
	isl_space *space;
	PART *part2;

	space = FN(PART,get_space)(part);
	entry2 = FN(UNION,find_part_entry)(data->u2, space, 0);
	isl_space_free(space);
	if (!entry2)
		goto error;
	if (entry2 == isl_hash_table_entry_none) {
		FN(PART,free)(part);
		return isl_stat_ok;
	}

	part2 = entry2->data;
	if (!isl_space_tuple_is_equal(part->dim, isl_dim_out,
					part2->dim, isl_dim_out))
		isl_die(FN(UNION,get_ctx)(data->u2), isl_error_invalid,
			"entries should have the same range space",
			goto error);

	part = data->fn(part, FN(PART, copy)(entry2->data));

	data->res = FN(FN(UNION,add),BASE)(data->res, part);
	if (!data->res)
		return isl_stat_error;

	return isl_stat_ok;
error:
	FN(PART,free)(part);
	return isl_stat_error;
}

/* This function is currently only used from isl_polynomial.c
 * and not from isl_fold.c.
 */
static __isl_give UNION *FN(UNION,match_bin_op)(__isl_take UNION *u1,
	__isl_take UNION *u2,
	__isl_give PART *(*fn)(__isl_take PART *, __isl_take PART *))
	__attribute__ ((unused));
/* For each pair of elements in "u1" and "u2" living in the same space,
 * call "fn" and collect the results.
 */
static __isl_give UNION *FN(UNION,match_bin_op)(__isl_take UNION *u1,
	__isl_take UNION *u2,
	__isl_give PART *(*fn)(__isl_take PART *, __isl_take PART *))
{
	S(UNION,match_bin_data) data = { NULL, NULL, fn };

	u1 = FN(UNION,align_params)(u1, FN(UNION,get_space)(u2));
	u2 = FN(UNION,align_params)(u2, FN(UNION,get_space)(u1));

	if (!u1 || !u2)
		goto error;

	data.u2 = u2;
	data.res = FN(UNION,alloc_same_size)(u1);
	if (FN(FN(UNION,foreach),BASE)(u1,
				    &FN(UNION,match_bin_entry), &data) < 0)
		goto error;

	FN(UNION,free)(u1);
	FN(UNION,free)(u2);
	return data.res;
error:
	FN(UNION,free)(u1);
	FN(UNION,free)(u2);
	FN(UNION,free)(data.res);
	return NULL;
}

/* Compute the sum of "u1" and "u2".
 *
 * If the base expressions have a default zero value, then the sum
 * is computed on the union of the domains of "u1" and "u2".
 * Otherwise, it is computed on their shared domains.
 */
__isl_give UNION *FN(UNION,add)(__isl_take UNION *u1, __isl_take UNION *u2)
{
#if DEFAULT_IS_ZERO
	return FN(UNION,union_add_)(u1, u2);
#else
	return FN(UNION,match_bin_op)(u1, u2, &FN(PART,add));
#endif
}

#ifndef NO_SUB
/* Subtract "u2" from "u1" and return the result.
 */
__isl_give UNION *FN(UNION,sub)(__isl_take UNION *u1, __isl_take UNION *u2)
{
	return FN(UNION,match_bin_op)(u1, u2, &FN(PART,sub));
}
#endif

S(UNION,any_set_data) {
	isl_set *set;
	__isl_give PW *(*fn)(__isl_take PW*, __isl_take isl_set*);
};

static __isl_give PART *FN(UNION,any_set_entry)(__isl_take PART *part,
	void *user)
{
	S(UNION,any_set_data) *data = user;

	return data->fn(part, isl_set_copy(data->set));
}

/* Update each element of "u" by calling "fn" on the element and "set".
 */
static __isl_give UNION *FN(UNION,any_set_op)(__isl_take UNION *u,
	__isl_take isl_set *set,
	__isl_give PW *(*fn)(__isl_take PW*, __isl_take isl_set*))
{
	S(UNION,any_set_data) data = { NULL, fn };
	S(UNION,transform_control) control = {
		.fn = &FN(UNION,any_set_entry),
		.fn_user = &data,
	};

	u = FN(UNION,align_params)(u, isl_set_get_space(set));
	set = isl_set_align_params(set, FN(UNION,get_space)(u));

	if (!u || !set)
		goto error;

	data.set = set;
	u = FN(UNION,transform)(u, &control);
	isl_set_free(set);
	return u;
error:
	FN(UNION,free)(u);
	isl_set_free(set);
	return NULL;
}

/* Intersect the domain of "u" with the parameter domain "context".
 */
__isl_give UNION *FN(UNION,intersect_params)(__isl_take UNION *u,
	__isl_take isl_set *set)
{
	return FN(UNION,any_set_op)(u, set, &FN(PW,intersect_params));
}

/* Compute the gist of the domain of "u" with respect to
 * the parameter domain "context".
 */
__isl_give UNION *FN(UNION,gist_params)(__isl_take UNION *u,
	__isl_take isl_set *set)
{
	return FN(UNION,any_set_op)(u, set, &FN(PW,gist_params));
}

/* Data structure that specifies how isl_union_*_match_domain_op
 * should combine its arguments.
 *
 * If "filter" is not NULL, then only parts that pass the given
 * filter are considered for matching.
 * "fn" is applied to each part in the union and each corresponding
 * set in the union set, i.e., such that the set lives in the same space
 * as the domain of the part.
 * If "match_space" is not NULL, then the set extracted from the union set
 * does not live in the same space as the domain of the part,
 * but rather in the space that results from calling "match_space"
 * on this domain space.
 */
S(UNION,match_domain_control) {
	isl_bool (*filter)(__isl_keep PART *part);
	__isl_give isl_space *(*match_space)(__isl_take isl_space *space);
	__isl_give PW *(*fn)(__isl_take PW*, __isl_take isl_set*);
};

S(UNION,match_domain_data) {
	isl_union_set *uset;
	UNION *res;
	S(UNION,match_domain_control) *control;
};

static isl_bool FN(UNION,set_has_space)(const void *entry, const void *val)
{
	isl_set *set = (isl_set *)entry;
	isl_space *space = (isl_space *)val;

	return isl_space_is_equal(set->dim, space);
}

/* Find the set in data->uset that lives in the same space as the domain
 * of "part", apply data->fn to *entry and this set (if any), and add
 * the result to data->res.
 */
static isl_stat FN(UNION,match_domain_entry)(__isl_take PART *part, void *user)
{
	S(UNION,match_domain_data) *data = user;
	uint32_t hash;
	struct isl_hash_table_entry *entry2;
	isl_space *space, *uset_space;

	if (data->control->filter) {
		isl_bool pass = data->control->filter(part);
		if (pass < 0 || !pass) {
			FN(PART,free)(part);
			return pass < 0 ? isl_stat_error : isl_stat_ok;
		}
	}

	uset_space = isl_union_set_peek_space(data->uset);
	space = FN(PART,get_domain_space)(part);
	if (data->control->match_space)
		space = data->control->match_space(space);
	space = isl_space_replace_params(space, uset_space);
	hash = isl_space_get_hash(space);
	entry2 = isl_hash_table_find(data->uset->dim->ctx, &data->uset->table,
				     hash, &FN(UNION,set_has_space), space, 0);
	isl_space_free(space);
	if (!entry2 || entry2 == isl_hash_table_entry_none) {
		FN(PART,free)(part);
		return isl_stat_non_null(entry2);
	}

	part = data->control->fn(part, isl_set_copy(entry2->data));

	data->res = FN(FN(UNION,add),BASE)(data->res, part);
	if (!data->res)
		return isl_stat_error;

	return isl_stat_ok;
}

/* Combine "u" and "uset" according to "control"
 * and collect the results.
 */
static __isl_give UNION *FN(UNION,match_domain_op)(__isl_take UNION *u,
	__isl_take isl_union_set *uset, S(UNION,match_domain_control) *control)
{
	S(UNION,match_domain_data) data = { NULL, NULL, control };

	if (!u || !uset)
		goto error;

	data.uset = uset;
	data.res = FN(UNION,alloc_same_size)(u);
	if (FN(FN(UNION,foreach),BASE)(u,
				   &FN(UNION,match_domain_entry), &data) < 0)
		goto error;

	FN(UNION,free)(u);
	isl_union_set_free(uset);
	return data.res;
error:
	FN(UNION,free)(u);
	isl_union_set_free(uset);
	FN(UNION,free)(data.res);
	return NULL;
}

/* Intersect the domain of "u" with "uset".
 * If "uset" is a parameters domain, then intersect the parameter
 * domain of "u" with this set.
 */
__isl_give UNION *FN(UNION,intersect_domain)(__isl_take UNION *u,
	__isl_take isl_union_set *uset)
{
	S(UNION,match_domain_control) control = {
		.fn = &FN(PW,intersect_domain),
	};

	if (isl_union_set_is_params(uset))
		return FN(UNION,intersect_params)(u,
						isl_set_from_union_set(uset));
	return FN(UNION,match_domain_op)(u, uset, &control);
}

/* Is the domain of "pw" a wrapped relation?
 */
static isl_bool FN(PW,domain_is_wrapping)(__isl_keep PW *pw)
{
	return isl_space_domain_is_wrapping(FN(PW,peek_space)(pw));
}

/* Intersect the domain of the wrapped relation inside the domain of "u"
 * with "uset".
 */
__isl_give UNION *FN(UNION,intersect_domain_wrapped_domain)(__isl_take UNION *u,
	__isl_take isl_union_set *uset)
{
	S(UNION,match_domain_control) control = {
		.filter = &FN(PART,domain_is_wrapping),
		.match_space = &isl_space_factor_domain,
		.fn = &FN(PW,intersect_domain_wrapped_domain),
	};

	return FN(UNION,match_domain_op)(u, uset, &control);
}

/* Intersect the range of the wrapped relation inside the domain of "u"
 * with "uset".
 */
__isl_give UNION *FN(UNION,intersect_domain_wrapped_range)(__isl_take UNION *u,
	__isl_take isl_union_set *uset)
{
	S(UNION,match_domain_control) control = {
		.filter = &FN(PART,domain_is_wrapping),
		.match_space = &isl_space_factor_range,
		.fn = &FN(PW,intersect_domain_wrapped_range),
	};

	return FN(UNION,match_domain_op)(u, uset, &control);
}

/* Take the set (which may be empty) in data->uset that lives
 * in the same space as the domain of "pw", subtract it from the domain
 * of "part" and return the result.
 */
static __isl_give PART *FN(UNION,subtract_domain_entry)(__isl_take PART *part,
	void *user)
{
	isl_union_set *uset = user;
	isl_space *space;
	isl_set *set;

	space = FN(PART,get_domain_space)(part);
	set = isl_union_set_extract_set(uset, space);
	return FN(PART,subtract_domain)(part, set);
}

/* Subtract "uset" from the domain of "u".
 */
__isl_give UNION *FN(UNION,subtract_domain)(__isl_take UNION *u,
	__isl_take isl_union_set *uset)
{
	S(UNION,transform_control) control = {
		.fn = &FN(UNION,subtract_domain_entry),
		.fn_user = uset,
	};

	u = FN(UNION,transform)(u, &control);
	isl_union_set_free(uset);
	return u;
}

__isl_give UNION *FN(UNION,gist)(__isl_take UNION *u,
	__isl_take isl_union_set *uset)
{
	S(UNION,match_domain_control) control = {
		.fn = &FN(PW,gist),
	};

	if (isl_union_set_is_params(uset))
		return FN(UNION,gist_params)(u, isl_set_from_union_set(uset));
	return FN(UNION,match_domain_op)(u, uset, &control);
}

/* Coalesce an entry in a UNION.  Coalescing is performed in-place.
 * Since the UNION may have several references, the entry is only
 * replaced if the coalescing is successful.
 */
static isl_stat FN(UNION,coalesce_entry)(void **entry, void *user)
{
	PART **part_p = (PART **) entry;
	PART *part;

	part = FN(PART,copy)(*part_p);
	part = FN(PW,coalesce)(part);
	if (!part)
		return isl_stat_error;
	FN(PART,free)(*part_p);
	*part_p = part;

	return isl_stat_ok;
}

__isl_give UNION *FN(UNION,coalesce)(__isl_take UNION *u)
{
	if (FN(UNION,foreach_inplace)(u, &FN(UNION,coalesce_entry), NULL) < 0)
		goto error;

	return u;
error:
	FN(UNION,free)(u);
	return NULL;
}

static isl_stat FN(UNION,domain_entry)(__isl_take PART *part, void *user)
{
	isl_union_set **uset = (isl_union_set **)user;

	*uset = isl_union_set_add_set(*uset, FN(PART,domain)(part));

	return isl_stat_ok;
}

__isl_give isl_union_set *FN(UNION,domain)(__isl_take UNION *u)
{
	isl_union_set *uset;

	uset = isl_union_set_empty(FN(UNION,get_space)(u));
	if (FN(FN(UNION,foreach),BASE)(u, &FN(UNION,domain_entry), &uset) < 0)
		goto error;

	FN(UNION,free)(u);
	
	return uset;
error:
	isl_union_set_free(uset);
	FN(UNION,free)(u);
	return NULL;
}

#ifdef HAS_TYPE
/* Negate the type of "u".
 */
static __isl_give UNION *FN(UNION,negate_type)(__isl_take UNION *u)
{
	u = FN(UNION,cow)(u);
	if (!u)
		return NULL;
	u->type = isl_fold_type_negate(u->type);
	return u;
}
#else
/* Negate the type of "u".
 * Since "u" does not have a type, do nothing.
 */
static __isl_give UNION *FN(UNION,negate_type)(__isl_take UNION *u)
{
	return u;
}
#endif

/* Multiply "part" by the isl_val "user" and return the result.
 */
static __isl_give PART *FN(UNION,scale_val_entry)(__isl_take PART *part,
	void *user)
{
	isl_val *v = user;

	return FN(PART,scale_val)(part, isl_val_copy(v));
}

/* Multiply "u" by "v" and return the result.
 */
__isl_give UNION *FN(UNION,scale_val)(__isl_take UNION *u,
	__isl_take isl_val *v)
{
	if (!u || !v)
		goto error;
	if (isl_val_is_one(v)) {
		isl_val_free(v);
		return u;
	}

	if (DEFAULT_IS_ZERO && u && isl_val_is_zero(v)) {
		UNION *zero;
		isl_space *space = FN(UNION,get_space)(u);
		zero = FN(UNION,ZERO)(space OPT_TYPE_ARG(u->));
		FN(UNION,free)(u);
		isl_val_free(v);
		return zero;
	}

	if (!isl_val_is_rat(v))
		isl_die(isl_val_get_ctx(v), isl_error_invalid,
			"expecting rational factor", goto error);

	u = FN(UNION,transform_inplace)(u, &FN(UNION,scale_val_entry), v);
	if (isl_val_is_neg(v))
		u = FN(UNION,negate_type)(u);

	isl_val_free(v);
	return u;
error:
	isl_val_free(v);
	FN(UNION,free)(u);
	return NULL;
}

/* Divide "part" by the isl_val "user" and return the result.
 */
static __isl_give PART *FN(UNION,scale_down_val_entry)(__isl_take PART *part,
	void *user)
{
	isl_val *v = user;

	return FN(PART,scale_down_val)(part, isl_val_copy(v));
}

/* Divide "u" by "v" and return the result.
 */
__isl_give UNION *FN(UNION,scale_down_val)(__isl_take UNION *u,
	__isl_take isl_val *v)
{
	if (!u || !v)
		goto error;
	if (isl_val_is_one(v)) {
		isl_val_free(v);
		return u;
	}

	if (!isl_val_is_rat(v))
		isl_die(isl_val_get_ctx(v), isl_error_invalid,
			"expecting rational factor", goto error);
	if (isl_val_is_zero(v))
		isl_die(isl_val_get_ctx(v), isl_error_invalid,
			"cannot scale down by zero", goto error);

	u = FN(UNION,transform_inplace)(u, &FN(UNION,scale_down_val_entry), v);
	if (isl_val_is_neg(v))
		u = FN(UNION,negate_type)(u);

	isl_val_free(v);
	return u;
error:
	isl_val_free(v);
	FN(UNION,free)(u);
	return NULL;
}

S(UNION,plain_is_equal_data)
{
	UNION *u2;
	isl_bool is_equal;
};

static isl_stat FN(UNION,plain_is_equal_entry)(void **entry, void *user)
{
	S(UNION,plain_is_equal_data) *data = user;
	struct isl_hash_table_entry *entry2;
	PW *pw = *entry;

	entry2 = FN(UNION,find_part_entry)(data->u2, pw->dim, 0);
	if (!entry2 || entry2 == isl_hash_table_entry_none) {
		if (!entry2)
			data->is_equal = isl_bool_error;
		else
			data->is_equal = isl_bool_false;
		return isl_stat_error;
	}

	data->is_equal = FN(PW,plain_is_equal)(pw, entry2->data);
	if (data->is_equal < 0 || !data->is_equal)
		return isl_stat_error;

	return isl_stat_ok;
}

isl_bool FN(UNION,plain_is_equal)(__isl_keep UNION *u1, __isl_keep UNION *u2)
{
	S(UNION,plain_is_equal_data) data = { NULL, isl_bool_true };
	isl_size n1, n2;

	if (!u1 || !u2)
		return isl_bool_error;
	if (u1 == u2)
		return isl_bool_true;
	if (u1->table.n != u2->table.n)
		return isl_bool_false;
	n1 = FN(FN(UNION,n),BASE)(u1);
	n2 = FN(FN(UNION,n),BASE)(u2);
	if (n1 < 0 || n2 < 0)
		return isl_bool_error;
	if (n1 != n2)
		return isl_bool_false;

	u1 = FN(UNION,copy)(u1);
	u2 = FN(UNION,copy)(u2);
	u1 = FN(UNION,align_params)(u1, FN(UNION,get_space)(u2));
	u2 = FN(UNION,align_params)(u2, FN(UNION,get_space)(u1));
	if (!u1 || !u2)
		goto error;

	data.u2 = u2;
	if (FN(UNION,foreach_inplace)(u1,
			       &FN(UNION,plain_is_equal_entry), &data) < 0 &&
	    data.is_equal)
		goto error;

	FN(UNION,free)(u1);
	FN(UNION,free)(u2);

	return data.is_equal;
error:
	FN(UNION,free)(u1);
	FN(UNION,free)(u2);
	return isl_bool_error;
}

/* Check whether the element that "entry" points to involves any NaNs and
 * store the result in *nan.
 * Abort as soon as one such element has been found.
 */
static isl_stat FN(UNION,involves_nan_entry)(void **entry, void *user)
{
	isl_bool *nan = user;
	PW *pw = *entry;

	*nan = FN(PW,involves_nan)(pw);
	if (*nan < 0 || !nan)
		return isl_stat_error;

	return isl_stat_ok;
}

/* Does "u" involve any NaNs?
 */
isl_bool FN(UNION,involves_nan)(__isl_keep UNION *u)
{
	isl_bool nan = isl_bool_false;

	if (!u)
		return isl_bool_error;

	if (FN(UNION,foreach_inplace)(u,
				    &FN(UNION,involves_nan_entry), &nan) < 0 &&
	    !nan)
		return isl_bool_error;

	return nan;
}

/* Internal data structure for isl_union_*_drop_dims.
 * type, first and n are passed to isl_*_drop_dims.
 */
S(UNION,drop_dims_data) {
	enum isl_dim_type type;
	unsigned first;
	unsigned n;
};

/* Drop the parameters specified by "data" from "part" and return the result.
 */
static __isl_give PART *FN(UNION,drop_dims_entry)(__isl_take PART *part,
	void *user)
{
	S(UNION,drop_dims_data) *data = user;

	return FN(PART,drop_dims)(part, data->type, data->first, data->n);
}

/* Drop the specified parameters from "u".
 * That is, type is required to be isl_dim_param.
 */
__isl_give UNION *FN(UNION,drop_dims)( __isl_take UNION *u,
	enum isl_dim_type type, unsigned first, unsigned n)
{
	isl_space *space;
	S(UNION,drop_dims_data) data = { type, first, n };
	S(UNION,transform_control) control = {
		.fn = &FN(UNION,drop_dims_entry),
		.fn_user = &data,
	};

	if (!u)
		return NULL;

	if (type != isl_dim_param)
		isl_die(FN(UNION,get_ctx)(u), isl_error_invalid,
			"can only project out parameters",
			return FN(UNION,free)(u));

	space = FN(UNION,get_space)(u);
	space = isl_space_drop_dims(space, type, first, n);
	return FN(UNION,transform_space)(u, space, &control);
}

/* Internal data structure for isl_union_*_set_dim_name.
 * pos is the position of the parameter that needs to be renamed.
 * s is the new name.
 */
S(UNION,set_dim_name_data) {
	unsigned pos;
	const char *s;
};

/* Change the name of the parameter at position data->pos of "part" to data->s
 * and return the result.
 */
static __isl_give PART *FN(UNION,set_dim_name_entry)(__isl_take PART *part,
	void *user)
{
	S(UNION,set_dim_name_data) *data = user;

	return FN(PART,set_dim_name)(part, isl_dim_param, data->pos, data->s);
}

/* Change the name of the parameter at position "pos" to "s".
 * That is, type is required to be isl_dim_param.
 */
__isl_give UNION *FN(UNION,set_dim_name)(__isl_take UNION *u,
	enum isl_dim_type type, unsigned pos, const char *s)
{
	S(UNION,set_dim_name_data) data = { pos, s };
	S(UNION,transform_control) control = {
		.fn = &FN(UNION,set_dim_name_entry),
		.fn_user = &data,
	};
	isl_space *space;

	if (!u)
		return NULL;

	if (type != isl_dim_param)
		isl_die(FN(UNION,get_ctx)(u), isl_error_invalid,
			"can only set parameter names",
			return FN(UNION,free)(u));

	space = FN(UNION,get_space)(u);
	space = isl_space_set_dim_name(space, type, pos, s);
	return FN(UNION,transform_space)(u, space, &control);
}

/* Reset the user pointer on all identifiers of parameters and tuples
 * of the space of "part" and return the result.
 */
static __isl_give PART *FN(UNION,reset_user_entry)(__isl_take PART *part,
	void *user)
{
	return FN(PART,reset_user)(part);
}

/* Reset the user pointer on all identifiers of parameters and tuples
 * of the spaces of "u".
 */
__isl_give UNION *FN(UNION,reset_user)(__isl_take UNION *u)
{
	S(UNION,transform_control) control = {
		.fn = &FN(UNION,reset_user_entry),
	};
	isl_space *space;

	space = FN(UNION,get_space)(u);
	space = isl_space_reset_user(space);
	return FN(UNION,transform_space)(u, space, &control);
}

/* Add the base expression held by "entry" to "list".
 */
static isl_stat FN(UNION,add_to_list)(void **entry, void *user)
{
	PW *pw = *entry;
	LIST(PART) **list = user;

	*list = FN(LIST(PART),add)(*list, FN(PART,copy)(pw));
	if (!*list)
		return isl_stat_error;

	return isl_stat_ok;
}

/* Return a list containing all the base expressions in "u".
 *
 * First construct a list of the appropriate size and
 * then add all the elements.
 */
__isl_give LIST(PART) *FN(FN(UNION,get),LIST(BASE))(__isl_keep UNION *u)
{
	isl_size n;
	LIST(PART) *list;

	if (!u)
		return NULL;
	n = FN(FN(UNION,n),BASE)(u);
	if (n < 0)
		return NULL;
	list = FN(LIST(PART),alloc)(FN(UNION,get_ctx(u)), n);
	if (FN(UNION,foreach_inplace)(u, &FN(UNION,add_to_list), &list) < 0)
		return FN(LIST(PART),free)(list);

	return list;
}