isl_constraint.c 36.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 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
/*
 * Copyright 2008-2009 Katholieke Universiteit Leuven
 * Copyright 2010      INRIA Saclay
 *
 * Use of this software is governed by the MIT license
 *
 * Written by Sven Verdoolaege, K.U.Leuven, Departement
 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France 
 */

#include <isl_map_private.h>
#include <isl_constraint_private.h>
#include <isl_space_private.h>
#include <isl_seq.h>
#include <isl_aff_private.h>
#include <isl_local_space_private.h>
#include <isl_val_private.h>
#include <isl_vec_private.h>

#include <bset_to_bmap.c>
#include <bset_from_bmap.c>

#undef BASE
#define BASE constraint

#include <isl_list_templ.c>

isl_ctx *isl_constraint_get_ctx(__isl_keep isl_constraint *c)
{
	return c ? isl_local_space_get_ctx(c->ls) : NULL;
}

static unsigned n(struct isl_constraint *c, enum isl_dim_type type)
{
	return isl_local_space_dim(c->ls, type);
}

static unsigned offset(struct isl_constraint *c, enum isl_dim_type type)
{
	return isl_local_space_offset(c->ls, type);
}

static unsigned basic_map_offset(__isl_keep isl_basic_map *bmap,
							enum isl_dim_type type)
{
	return type == isl_dim_div ? 1 + isl_space_dim(bmap->dim, isl_dim_all)
				   : 1 + isl_space_offset(bmap->dim, type);
}

static unsigned basic_set_offset(struct isl_basic_set *bset,
							enum isl_dim_type type)
{
	isl_space *dim = bset->dim;
	switch (type) {
	case isl_dim_param:	return 1;
	case isl_dim_in:	return 1 + dim->nparam;
	case isl_dim_out:	return 1 + dim->nparam + dim->n_in;
	case isl_dim_div:	return 1 + dim->nparam + dim->n_in + dim->n_out;
	default:		return 0;
	}
}

__isl_give isl_constraint *isl_constraint_alloc_vec(int eq,
	__isl_take isl_local_space *ls, __isl_take isl_vec *v)
{
	isl_constraint *constraint;

	if (!ls || !v)
		goto error;

	constraint = isl_alloc_type(isl_vec_get_ctx(v), isl_constraint);
	if (!constraint)
		goto error;

	constraint->ref = 1;
	constraint->eq = eq;
	constraint->ls = ls;
	constraint->v = v;

	return constraint;
error:
	isl_local_space_free(ls);
	isl_vec_free(v);
	return NULL;
}

__isl_give isl_constraint *isl_constraint_alloc(int eq,
	__isl_take isl_local_space *ls)
{
	isl_ctx *ctx;
	isl_vec *v;

	if (!ls)
		return NULL;

	ctx = isl_local_space_get_ctx(ls);
	v = isl_vec_alloc(ctx, 1 + isl_local_space_dim(ls, isl_dim_all));
	v = isl_vec_clr(v);
	return isl_constraint_alloc_vec(eq, ls, v);
}

struct isl_constraint *isl_basic_map_constraint(struct isl_basic_map *bmap,
	isl_int **line)
{
	int eq;
	isl_ctx *ctx;
	isl_vec *v;
	isl_local_space *ls = NULL;
	isl_constraint *constraint;

	if (!bmap || !line)
		goto error;

	eq = line >= bmap->eq;

	ctx = isl_basic_map_get_ctx(bmap);
	ls = isl_basic_map_get_local_space(bmap);
	v = isl_vec_alloc(ctx, 1 + isl_local_space_dim(ls, isl_dim_all));
	if (!v)
		goto error;
	isl_seq_cpy(v->el, line[0], v->size);
	constraint = isl_constraint_alloc_vec(eq, ls, v);

	isl_basic_map_free(bmap);
	return constraint;
error:
	isl_local_space_free(ls);
	isl_basic_map_free(bmap);
	return NULL;
}

struct isl_constraint *isl_basic_set_constraint(struct isl_basic_set *bset,
	isl_int **line)
{
	return isl_basic_map_constraint(bset_to_bmap(bset), line);
}

__isl_give isl_constraint *isl_constraint_alloc_equality(
	__isl_take isl_local_space *ls)
{
	return isl_constraint_alloc(1, ls);
}

__isl_give isl_constraint *isl_constraint_alloc_inequality(
	__isl_take isl_local_space *ls)
{
	return isl_constraint_alloc(0, ls);
}

struct isl_constraint *isl_constraint_dup(struct isl_constraint *c)
{
	if (!c)
		return NULL;

	return isl_constraint_alloc_vec(c->eq, isl_local_space_copy(c->ls),
						isl_vec_copy(c->v));
}

struct isl_constraint *isl_constraint_cow(struct isl_constraint *c)
{
	if (!c)
		return NULL;

	if (c->ref == 1)
		return c;
	c->ref--;
	return isl_constraint_dup(c);
}

struct isl_constraint *isl_constraint_copy(struct isl_constraint *constraint)
{
	if (!constraint)
		return NULL;

	constraint->ref++;
	return constraint;
}

__isl_null isl_constraint *isl_constraint_free(__isl_take isl_constraint *c)
{
	if (!c)
		return NULL;

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

	isl_local_space_free(c->ls);
	isl_vec_free(c->v);
	free(c);

	return NULL;
}

/* Return the number of constraints in "bmap", i.e., the
 * number of times isl_basic_map_foreach_constraint will
 * call the callback.
 */
int isl_basic_map_n_constraint(__isl_keep isl_basic_map *bmap)
{
	if (!bmap)
		return -1;

	return bmap->n_eq + bmap->n_ineq;
}

/* Return the number of constraints in "bset", i.e., the
 * number of times isl_basic_set_foreach_constraint will
 * call the callback.
 */
int isl_basic_set_n_constraint(__isl_keep isl_basic_set *bset)
{
	return isl_basic_map_n_constraint(bset);
}

isl_stat isl_basic_map_foreach_constraint(__isl_keep isl_basic_map *bmap,
	isl_stat (*fn)(__isl_take isl_constraint *c, void *user), void *user)
{
	int i;
	struct isl_constraint *c;

	if (!bmap)
		return isl_stat_error;

	isl_assert(bmap->ctx, ISL_F_ISSET(bmap, ISL_BASIC_MAP_FINAL),
			return isl_stat_error);

	for (i = 0; i < bmap->n_eq; ++i) {
		c = isl_basic_map_constraint(isl_basic_map_copy(bmap),
						&bmap->eq[i]);
		if (!c)
			return isl_stat_error;
		if (fn(c, user) < 0)
			return isl_stat_error;
	}

	for (i = 0; i < bmap->n_ineq; ++i) {
		c = isl_basic_map_constraint(isl_basic_map_copy(bmap),
						&bmap->ineq[i]);
		if (!c)
			return isl_stat_error;
		if (fn(c, user) < 0)
			return isl_stat_error;
	}

	return isl_stat_ok;
}

isl_stat isl_basic_set_foreach_constraint(__isl_keep isl_basic_set *bset,
	isl_stat (*fn)(__isl_take isl_constraint *c, void *user), void *user)
{
	return isl_basic_map_foreach_constraint(bset_to_bmap(bset), fn, user);
}

/* Add the constraint to the list that "user" points to, if it is not
 * a div constraint.
 */
static isl_stat collect_constraint(__isl_take isl_constraint *constraint,
	void *user)
{
	isl_constraint_list **list = user;

	if (isl_constraint_is_div_constraint(constraint))
		isl_constraint_free(constraint);
	else
		*list = isl_constraint_list_add(*list, constraint);

	return isl_stat_ok;
}

/* Return a list of constraints that, when combined, are equivalent
 * to "bmap".  The input is required to have only known divs.
 *
 * There is no need to include the div constraints as they are
 * implied by the div expressions.
 */
__isl_give isl_constraint_list *isl_basic_map_get_constraint_list(
	__isl_keep isl_basic_map *bmap)
{
	int n;
	int known;
	isl_ctx *ctx;
	isl_constraint_list *list;

	known = isl_basic_map_divs_known(bmap);
	if (known < 0)
		return NULL;
	ctx = isl_basic_map_get_ctx(bmap);
	if (!known)
		isl_die(ctx, isl_error_invalid,
			"input involves unknown divs", return NULL);

	n = isl_basic_map_n_constraint(bmap);
	list = isl_constraint_list_alloc(ctx, n);
	if (isl_basic_map_foreach_constraint(bmap,
					    &collect_constraint, &list) < 0)
		list = isl_constraint_list_free(list);

	return list;
}

/* Return a list of constraints that, when combined, are equivalent
 * to "bset".  The input is required to have only known divs.
 */
__isl_give isl_constraint_list *isl_basic_set_get_constraint_list(
	__isl_keep isl_basic_set *bset)
{
	return isl_basic_map_get_constraint_list(bset);
}

int isl_constraint_is_equal(struct isl_constraint *constraint1,
	struct isl_constraint *constraint2)
{
	int equal;

	if (!constraint1 || !constraint2)
		return 0;
	if (constraint1->eq != constraint2->eq)
		return 0;
	equal = isl_local_space_is_equal(constraint1->ls, constraint2->ls);
	if (equal < 0 || !equal)
		return equal;
	return isl_vec_is_equal(constraint1->v, constraint2->v);
}

struct isl_basic_map *isl_basic_map_add_constraint(
	struct isl_basic_map *bmap, struct isl_constraint *constraint)
{
	isl_ctx *ctx;
	isl_space *dim;
	int equal_space;

	if (!bmap || !constraint)
		goto error;

	ctx = isl_constraint_get_ctx(constraint);
	dim = isl_constraint_get_space(constraint);
	equal_space = isl_space_is_equal(bmap->dim, dim);
	isl_space_free(dim);
	isl_assert(ctx, equal_space, goto error);

	bmap = isl_basic_map_intersect(bmap,
				isl_basic_map_from_constraint(constraint));
	return bmap;
error:
	isl_basic_map_free(bmap);
	isl_constraint_free(constraint);
	return NULL;
}

struct isl_basic_set *isl_basic_set_add_constraint(
	struct isl_basic_set *bset, struct isl_constraint *constraint)
{
	return bset_from_bmap(isl_basic_map_add_constraint(bset_to_bmap(bset),
							    constraint));
}

__isl_give isl_map *isl_map_add_constraint(__isl_take isl_map *map,
	__isl_take isl_constraint *constraint)
{
	isl_basic_map *bmap;

	bmap = isl_basic_map_from_constraint(constraint);
	map = isl_map_intersect(map, isl_map_from_basic_map(bmap));

	return map;
}

__isl_give isl_set *isl_set_add_constraint(__isl_take isl_set *set,
	__isl_take isl_constraint *constraint)
{
	return isl_map_add_constraint(set, constraint);
}

__isl_give isl_space *isl_constraint_get_space(
	__isl_keep isl_constraint *constraint)
{
	return constraint ? isl_local_space_get_space(constraint->ls) : NULL;
}

__isl_give isl_local_space *isl_constraint_get_local_space(
	__isl_keep isl_constraint *constraint)
{
	return constraint ? isl_local_space_copy(constraint->ls) : NULL;
}

int isl_constraint_dim(struct isl_constraint *constraint,
	enum isl_dim_type type)
{
	if (!constraint)
		return -1;
	return n(constraint, type);
}

isl_bool isl_constraint_involves_dims(__isl_keep isl_constraint *constraint,
	enum isl_dim_type type, unsigned first, unsigned n)
{
	int i;
	isl_ctx *ctx;
	int *active = NULL;
	isl_bool involves = isl_bool_false;

	if (!constraint)
		return isl_bool_error;
	if (n == 0)
		return isl_bool_false;

	ctx = isl_constraint_get_ctx(constraint);
	if (first + n > isl_constraint_dim(constraint, type))
		isl_die(ctx, isl_error_invalid,
			"range out of bounds", return isl_bool_error);

	active = isl_local_space_get_active(constraint->ls,
					    constraint->v->el + 1);
	if (!active)
		goto error;

	first += isl_local_space_offset(constraint->ls, type) - 1;
	for (i = 0; i < n; ++i)
		if (active[first + i]) {
			involves = isl_bool_true;
			break;
		}

	free(active);

	return involves;
error:
	free(active);
	return isl_bool_error;
}

/* Does the given constraint represent a lower bound on the given
 * dimension?
 */
isl_bool isl_constraint_is_lower_bound(__isl_keep isl_constraint *constraint,
	enum isl_dim_type type, unsigned pos)
{
	if (!constraint)
		return isl_bool_error;

	if (pos >= isl_local_space_dim(constraint->ls, type))
		isl_die(isl_constraint_get_ctx(constraint), isl_error_invalid,
			"position out of bounds", return isl_bool_error);

	pos += isl_local_space_offset(constraint->ls, type);
	return isl_int_is_pos(constraint->v->el[pos]);
}

/* Does the given constraint represent an upper bound on the given
 * dimension?
 */
isl_bool isl_constraint_is_upper_bound(__isl_keep isl_constraint *constraint,
	enum isl_dim_type type, unsigned pos)
{
	if (!constraint)
		return isl_bool_error;

	if (pos >= isl_local_space_dim(constraint->ls, type))
		isl_die(isl_constraint_get_ctx(constraint), isl_error_invalid,
			"position out of bounds", return isl_bool_error);

	pos += isl_local_space_offset(constraint->ls, type);
	return isl_int_is_neg(constraint->v->el[pos]);
}

const char *isl_constraint_get_dim_name(__isl_keep isl_constraint *constraint,
	enum isl_dim_type type, unsigned pos)
{
	return constraint ?
	    isl_local_space_get_dim_name(constraint->ls, type, pos) : NULL;
}

void isl_constraint_get_constant(__isl_keep isl_constraint *constraint,
	isl_int *v)
{
	if (!constraint)
		return;
	isl_int_set(*v, constraint->v->el[0]);
}

/* Return the constant term of "constraint".
 */
__isl_give isl_val *isl_constraint_get_constant_val(
	__isl_keep isl_constraint *constraint)
{
	isl_ctx *ctx;

	if (!constraint)
		return NULL;

	ctx = isl_constraint_get_ctx(constraint);
	return isl_val_int_from_isl_int(ctx, constraint->v->el[0]);
}

void isl_constraint_get_coefficient(struct isl_constraint *constraint,
	enum isl_dim_type type, int pos, isl_int *v)
{
	if (!constraint)
		return;

	if (pos >= isl_local_space_dim(constraint->ls, type))
		isl_die(constraint->v->ctx, isl_error_invalid,
			"position out of bounds", return);

	pos += isl_local_space_offset(constraint->ls, type);
	isl_int_set(*v, constraint->v->el[pos]);
}

/* Return the coefficient of the variable of type "type" at position "pos"
 * of "constraint".
 */
__isl_give isl_val *isl_constraint_get_coefficient_val(
	__isl_keep isl_constraint *constraint, enum isl_dim_type type, int pos)
{
	isl_ctx *ctx;

	if (!constraint)
		return NULL;

	ctx = isl_constraint_get_ctx(constraint);
	if (pos < 0 || pos >= isl_local_space_dim(constraint->ls, type))
		isl_die(ctx, isl_error_invalid,
			"position out of bounds", return NULL);

	pos += isl_local_space_offset(constraint->ls, type);
	return isl_val_int_from_isl_int(ctx, constraint->v->el[pos]);
}

__isl_give isl_aff *isl_constraint_get_div(__isl_keep isl_constraint *constraint,
	int pos)
{
	if (!constraint)
		return NULL;

	return isl_local_space_get_div(constraint->ls, pos);
}

__isl_give isl_constraint *isl_constraint_set_constant(
	__isl_take isl_constraint *constraint, isl_int v)
{
	constraint = isl_constraint_cow(constraint);
	if (!constraint)
		return NULL;

	constraint->v = isl_vec_cow(constraint->v);
	if (!constraint->v)
		return isl_constraint_free(constraint);

	isl_int_set(constraint->v->el[0], v);
	return constraint;
}

/* Replace the constant term of "constraint" by "v".
 */
__isl_give isl_constraint *isl_constraint_set_constant_val(
	__isl_take isl_constraint *constraint, __isl_take isl_val *v)
{
	constraint = isl_constraint_cow(constraint);
	if (!constraint || !v)
		goto error;
	if (!isl_val_is_int(v))
		isl_die(isl_constraint_get_ctx(constraint), isl_error_invalid,
			"expecting integer value", goto error);
	constraint->v = isl_vec_set_element_val(constraint->v, 0, v);
	if (!constraint->v)
		constraint = isl_constraint_free(constraint);
	return constraint;
error:
	isl_val_free(v);
	return isl_constraint_free(constraint);
}

__isl_give isl_constraint *isl_constraint_set_constant_si(
	__isl_take isl_constraint *constraint, int v)
{
	constraint = isl_constraint_cow(constraint);
	if (!constraint)
		return NULL;

	constraint->v = isl_vec_cow(constraint->v);
	if (!constraint->v)
		return isl_constraint_free(constraint);

	isl_int_set_si(constraint->v->el[0], v);
	return constraint;
}

__isl_give isl_constraint *isl_constraint_set_coefficient(
	__isl_take isl_constraint *constraint,
	enum isl_dim_type type, int pos, isl_int v)
{
	constraint = isl_constraint_cow(constraint);
	if (!constraint)
		return NULL;

	if (pos >= isl_local_space_dim(constraint->ls, type))
		isl_die(constraint->v->ctx, isl_error_invalid,
			"position out of bounds",
			return isl_constraint_free(constraint));

	constraint = isl_constraint_cow(constraint);
	if (!constraint)
		return NULL;

	constraint->v = isl_vec_cow(constraint->v);
	if (!constraint->v)
		return isl_constraint_free(constraint);

	pos += isl_local_space_offset(constraint->ls, type);
	isl_int_set(constraint->v->el[pos], v);

	return constraint;
}

/* Replace the coefficient of the variable of type "type" at position "pos"
 * of "constraint" by "v".
 */
__isl_give isl_constraint *isl_constraint_set_coefficient_val(
	__isl_take isl_constraint *constraint,
	enum isl_dim_type type, int pos, __isl_take isl_val *v)
{
	constraint = isl_constraint_cow(constraint);
	if (!constraint || !v)
		goto error;
	if (!isl_val_is_int(v))
		isl_die(isl_constraint_get_ctx(constraint), isl_error_invalid,
			"expecting integer value", goto error);

	if (pos >= isl_local_space_dim(constraint->ls, type))
		isl_die(isl_constraint_get_ctx(constraint), isl_error_invalid,
			"position out of bounds", goto error);

	pos += isl_local_space_offset(constraint->ls, type);
	constraint->v = isl_vec_set_element_val(constraint->v, pos, v);
	if (!constraint->v)
		constraint = isl_constraint_free(constraint);
	return constraint;
error:
	isl_val_free(v);
	return isl_constraint_free(constraint);
}

__isl_give isl_constraint *isl_constraint_set_coefficient_si(
	__isl_take isl_constraint *constraint,
	enum isl_dim_type type, int pos, int v)
{
	constraint = isl_constraint_cow(constraint);
	if (!constraint)
		return NULL;

	if (pos >= isl_local_space_dim(constraint->ls, type))
		isl_die(constraint->v->ctx, isl_error_invalid,
			"position out of bounds",
			return isl_constraint_free(constraint));

	constraint = isl_constraint_cow(constraint);
	if (!constraint)
		return NULL;

	constraint->v = isl_vec_cow(constraint->v);
	if (!constraint->v)
		return isl_constraint_free(constraint);

	pos += isl_local_space_offset(constraint->ls, type);
	isl_int_set_si(constraint->v->el[pos], v);

	return constraint;
}

struct isl_constraint *isl_constraint_negate(struct isl_constraint *constraint)
{
	isl_ctx *ctx;

	constraint = isl_constraint_cow(constraint);
	if (!constraint)
		return NULL;

	ctx = isl_constraint_get_ctx(constraint);
	if (isl_constraint_is_equality(constraint))
		isl_die(ctx, isl_error_invalid, "cannot negate equality",
			return isl_constraint_free(constraint));
	constraint->v = isl_vec_neg(constraint->v);
	constraint->v = isl_vec_cow(constraint->v);
	if (!constraint->v)
		return isl_constraint_free(constraint);
	isl_int_sub_ui(constraint->v->el[0], constraint->v->el[0], 1);
	return constraint;
}

isl_bool isl_constraint_is_equality(struct isl_constraint *constraint)
{
	if (!constraint)
		return isl_bool_error;
	return constraint->eq;
}

int isl_constraint_is_div_constraint(__isl_keep isl_constraint *constraint)
{
	int i;
	int n_div;

	if (!constraint)
		return -1;
	if (isl_constraint_is_equality(constraint))
		return 0;
	n_div = isl_constraint_dim(constraint, isl_dim_div);
	for (i = 0; i < n_div; ++i) {
		isl_bool is_div;
		is_div = isl_local_space_is_div_constraint(constraint->ls,
							constraint->v->el, i);
		if (is_div < 0 || is_div)
			return is_div;
	}

	return 0;
}

/* Is "constraint" an equality that corresponds to integer division "div"?
 *
 * That is, given an integer division of the form
 *
 *	a = floor((f + c)/m)
 *
 * is the equality of the form
 *
 *		-f + m d + c' = 0
 * ?
 * Note that the constant term is not checked explicitly, but given
 * that this is a valid equality constraint, the constant c' necessarily
 * has a value close to -c.
 */
isl_bool isl_constraint_is_div_equality(__isl_keep isl_constraint *constraint,
	unsigned div)
{
	isl_bool equality;

	equality = isl_constraint_is_equality(constraint);
	if (equality < 0 || !equality)
		return equality;
	return isl_local_space_is_div_equality(constraint->ls,
						constraint->v->el, div);
}

/* We manually set ISL_BASIC_SET_FINAL instead of calling
 * isl_basic_map_finalize because we want to keep the position
 * of the divs and we therefore do not want to throw away redundant divs.
 * This is arguably a bit fragile.
 */
__isl_give isl_basic_map *isl_basic_map_from_constraint(
	__isl_take isl_constraint *constraint)
{
	int k;
	isl_local_space *ls;
	struct isl_basic_map *bmap;
	isl_int *c;
	unsigned total;

	if (!constraint)
		return NULL;

	ls = isl_local_space_copy(constraint->ls);
	bmap = isl_basic_map_from_local_space(ls);
	bmap = isl_basic_map_extend_constraints(bmap, 1, 1);
	if (isl_constraint_is_equality(constraint)) {
		k = isl_basic_map_alloc_equality(bmap);
		if (k < 0)
			goto error;
		c = bmap->eq[k];
	}
	else {
		k = isl_basic_map_alloc_inequality(bmap);
		if (k < 0)
			goto error;
		c = bmap->ineq[k];
	}
	total = isl_basic_map_total_dim(bmap);
	isl_seq_cpy(c, constraint->v->el, 1 + total);
	isl_constraint_free(constraint);
	if (bmap)
		ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
	return bmap;
error:
	isl_constraint_free(constraint);
	isl_basic_map_free(bmap);
	return NULL;
}

__isl_give isl_basic_set *isl_basic_set_from_constraint(
	__isl_take isl_constraint *constraint)
{
	if (!constraint)
		return NULL;

	if (isl_constraint_dim(constraint, isl_dim_in) != 0)
		isl_die(isl_constraint_get_ctx(constraint), isl_error_invalid,
			"not a set constraint", goto error);
	return bset_from_bmap(isl_basic_map_from_constraint(constraint));
error:
	isl_constraint_free(constraint);
	return NULL;
}

/* Is the variable of "type" at position "pos" of "bmap" defined
 * in terms of earlier dimensions through an equality?
 *
 * If so, and if c is not NULL, then return a copy of this equality in *c.
 */
isl_bool isl_basic_map_has_defining_equality(
	__isl_keep isl_basic_map *bmap, enum isl_dim_type type, int pos,
	__isl_give isl_constraint **c)
{
	int i;
	unsigned offset;
	unsigned total;

	if (!bmap)
		return isl_bool_error;
	offset = basic_map_offset(bmap, type);
	total = isl_basic_map_total_dim(bmap);
	if (pos >= isl_basic_map_dim(bmap, type))
		isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
			"invalid position", return isl_bool_error);
	for (i = 0; i < bmap->n_eq; ++i) {
		if (isl_int_is_zero(bmap->eq[i][offset + pos]) ||
		    isl_seq_first_non_zero(bmap->eq[i]+offset+pos+1,
					   1+total-offset-pos-1) != -1)
			continue;
		if (c)
			*c = isl_basic_map_constraint(isl_basic_map_copy(bmap),
								&bmap->eq[i]);
		return isl_bool_true;
	}
	return isl_bool_false;
}

/* Is the variable of "type" at position "pos" of "bset" defined
 * in terms of earlier dimensions through an equality?
 *
 * If so, and if c is not NULL, then return a copy of this equality in *c.
 */
isl_bool isl_basic_set_has_defining_equality(
	__isl_keep isl_basic_set *bset, enum isl_dim_type type, int pos,
	__isl_give isl_constraint **c)
{
	return isl_basic_map_has_defining_equality(bset_to_bmap(bset),
						    type, pos, c);
}

isl_bool isl_basic_set_has_defining_inequalities(
	struct isl_basic_set *bset, enum isl_dim_type type, int pos,
	struct isl_constraint **lower,
	struct isl_constraint **upper)
{
	int i, j;
	unsigned offset;
	unsigned total;
	isl_int m;
	isl_int **lower_line, **upper_line;

	if (!bset)
		return isl_bool_error;
	offset = basic_set_offset(bset, type);
	total = isl_basic_set_total_dim(bset);
	if (pos >= isl_basic_set_dim(bset, type))
		isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
			"invalid position", return isl_bool_error);
	isl_int_init(m);
	for (i = 0; i < bset->n_ineq; ++i) {
		if (isl_int_is_zero(bset->ineq[i][offset + pos]))
			continue;
		if (isl_int_is_one(bset->ineq[i][offset + pos]))
			continue;
		if (isl_int_is_negone(bset->ineq[i][offset + pos]))
			continue;
		if (isl_seq_first_non_zero(bset->ineq[i]+offset+pos+1,
						1+total-offset-pos-1) != -1)
			continue;
		for (j = i + 1; j < bset->n_ineq; ++j) {
			if (!isl_seq_is_neg(bset->ineq[i]+1, bset->ineq[j]+1,
					    total))
				continue;
			isl_int_add(m, bset->ineq[i][0], bset->ineq[j][0]);
			if (isl_int_abs_ge(m, bset->ineq[i][offset+pos]))
				continue;

			if (isl_int_is_pos(bset->ineq[i][offset+pos])) {
				lower_line = &bset->ineq[i];
				upper_line = &bset->ineq[j];
			} else {
				lower_line = &bset->ineq[j];
				upper_line = &bset->ineq[i];
			}
			*lower = isl_basic_set_constraint(
					isl_basic_set_copy(bset), lower_line);
			*upper = isl_basic_set_constraint(
					isl_basic_set_copy(bset), upper_line);
			isl_int_clear(m);
			return isl_bool_true;
		}
	}
	*lower = NULL;
	*upper = NULL;
	isl_int_clear(m);
	return isl_bool_false;
}

/* Given two constraints "a" and "b" on the variable at position "abs_pos"
 * (in "a" and "b"), add a constraint to "bset" that ensures that the
 * bound implied by "a" is (strictly) larger than the bound implied by "b".
 *
 * If both constraints imply lower bounds, then this means that "a" is
 * active in the result.
 * If both constraints imply upper bounds, then this means that "b" is
 * active in the result.
 */
static __isl_give isl_basic_set *add_larger_bound_constraint(
	__isl_take isl_basic_set *bset, isl_int *a, isl_int *b,
	unsigned abs_pos, int strict)
{
	int k;
	isl_int t;
	unsigned total;

	k = isl_basic_set_alloc_inequality(bset);
	if (k < 0)
		goto error;

	total = isl_basic_set_dim(bset, isl_dim_all);

	isl_int_init(t);
	isl_int_neg(t, b[1 + abs_pos]);

	isl_seq_combine(bset->ineq[k], t, a, a[1 + abs_pos], b, 1 + abs_pos);
	isl_seq_combine(bset->ineq[k] + 1 + abs_pos,
		t, a + 1 + abs_pos + 1, a[1 + abs_pos], b + 1 + abs_pos + 1,
		total - abs_pos);

	if (strict)
		isl_int_sub_ui(bset->ineq[k][0], bset->ineq[k][0], 1);

	isl_int_clear(t);

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

/* Add constraints to "context" that ensure that "u" is the smallest
 * (and therefore active) upper bound on "abs_pos" in "bset" and return
 * the resulting basic set.
 */
static __isl_give isl_basic_set *set_smallest_upper_bound(
	__isl_keep isl_basic_set *context,
	__isl_keep isl_basic_set *bset, unsigned abs_pos, int n_upper, int u)
{
	int j;

	context = isl_basic_set_copy(context);
	context = isl_basic_set_cow(context);

	context = isl_basic_set_extend_constraints(context, 0, n_upper - 1);

	for (j = 0; j < bset->n_ineq; ++j) {
		if (j == u)
			continue;
		if (!isl_int_is_neg(bset->ineq[j][1 + abs_pos]))
			continue;
		context = add_larger_bound_constraint(context,
			bset->ineq[j], bset->ineq[u], abs_pos, j > u);
	}

	context = isl_basic_set_simplify(context);
	context = isl_basic_set_finalize(context);

	return context;
}

/* Add constraints to "context" that ensure that "u" is the largest
 * (and therefore active) upper bound on "abs_pos" in "bset" and return
 * the resulting basic set.
 */
static __isl_give isl_basic_set *set_largest_lower_bound(
	__isl_keep isl_basic_set *context,
	__isl_keep isl_basic_set *bset, unsigned abs_pos, int n_lower, int l)
{
	int j;

	context = isl_basic_set_copy(context);
	context = isl_basic_set_cow(context);

	context = isl_basic_set_extend_constraints(context, 0, n_lower - 1);

	for (j = 0; j < bset->n_ineq; ++j) {
		if (j == l)
			continue;
		if (!isl_int_is_pos(bset->ineq[j][1 + abs_pos]))
			continue;
		context = add_larger_bound_constraint(context,
			bset->ineq[l], bset->ineq[j], abs_pos, j > l);
	}

	context = isl_basic_set_simplify(context);
	context = isl_basic_set_finalize(context);

	return context;
}

static isl_stat foreach_upper_bound(__isl_keep isl_basic_set *bset,
	enum isl_dim_type type, unsigned abs_pos,
	__isl_take isl_basic_set *context, int n_upper,
	isl_stat (*fn)(__isl_take isl_constraint *lower,
		  __isl_take isl_constraint *upper,
		  __isl_take isl_basic_set *bset, void *user), void *user)
{
	isl_basic_set *context_i;
	isl_constraint *upper = NULL;
	int i;

	for (i = 0; i < bset->n_ineq; ++i) {
		if (isl_int_is_zero(bset->ineq[i][1 + abs_pos]))
			continue;

		context_i = set_smallest_upper_bound(context, bset,
							abs_pos, n_upper, i);
		if (isl_basic_set_is_empty(context_i)) {
			isl_basic_set_free(context_i);
			continue;
		}
		upper = isl_basic_set_constraint(isl_basic_set_copy(bset),
						&bset->ineq[i]);
		if (!upper || !context_i)
			goto error;
		if (fn(NULL, upper, context_i, user) < 0)
			break;
	}

	isl_basic_set_free(context);

	if (i < bset->n_ineq)
		return isl_stat_error;

	return isl_stat_ok;
error:
	isl_constraint_free(upper);
	isl_basic_set_free(context_i);
	isl_basic_set_free(context);
	return isl_stat_error;
}

static isl_stat foreach_lower_bound(__isl_keep isl_basic_set *bset,
	enum isl_dim_type type, unsigned abs_pos,
	__isl_take isl_basic_set *context, int n_lower,
	isl_stat (*fn)(__isl_take isl_constraint *lower,
		  __isl_take isl_constraint *upper,
		  __isl_take isl_basic_set *bset, void *user), void *user)
{
	isl_basic_set *context_i;
	isl_constraint *lower = NULL;
	int i;

	for (i = 0; i < bset->n_ineq; ++i) {
		if (isl_int_is_zero(bset->ineq[i][1 + abs_pos]))
			continue;

		context_i = set_largest_lower_bound(context, bset,
							abs_pos, n_lower, i);
		if (isl_basic_set_is_empty(context_i)) {
			isl_basic_set_free(context_i);
			continue;
		}
		lower = isl_basic_set_constraint(isl_basic_set_copy(bset),
						&bset->ineq[i]);
		if (!lower || !context_i)
			goto error;
		if (fn(lower, NULL, context_i, user) < 0)
			break;
	}

	isl_basic_set_free(context);

	if (i < bset->n_ineq)
		return isl_stat_error;

	return isl_stat_ok;
error:
	isl_constraint_free(lower);
	isl_basic_set_free(context_i);
	isl_basic_set_free(context);
	return isl_stat_error;
}

static isl_stat foreach_bound_pair(__isl_keep isl_basic_set *bset,
	enum isl_dim_type type, unsigned abs_pos,
	__isl_take isl_basic_set *context, int n_lower, int n_upper,
	isl_stat (*fn)(__isl_take isl_constraint *lower,
		  __isl_take isl_constraint *upper,
		  __isl_take isl_basic_set *bset, void *user), void *user)
{
	isl_basic_set *context_i, *context_j;
	isl_constraint *lower = NULL;
	isl_constraint *upper = NULL;
	int i, j;

	for (i = 0; i < bset->n_ineq; ++i) {
		if (!isl_int_is_pos(bset->ineq[i][1 + abs_pos]))
			continue;

		context_i = set_largest_lower_bound(context, bset,
							abs_pos, n_lower, i);
		if (isl_basic_set_is_empty(context_i)) {
			isl_basic_set_free(context_i);
			continue;
		}

		for (j = 0; j < bset->n_ineq; ++j) {
			if (!isl_int_is_neg(bset->ineq[j][1 + abs_pos]))
				continue;

			context_j = set_smallest_upper_bound(context_i, bset,
							    abs_pos, n_upper, j);
			context_j = isl_basic_set_extend_constraints(context_j,
									0, 1);
			context_j = add_larger_bound_constraint(context_j,
				bset->ineq[i], bset->ineq[j], abs_pos, 0);
			context_j = isl_basic_set_simplify(context_j);
			context_j = isl_basic_set_finalize(context_j);
			if (isl_basic_set_is_empty(context_j)) {
				isl_basic_set_free(context_j);
				continue;
			}
			lower = isl_basic_set_constraint(isl_basic_set_copy(bset),
							&bset->ineq[i]);
			upper = isl_basic_set_constraint(isl_basic_set_copy(bset),
							&bset->ineq[j]);
			if (!lower || !upper || !context_j)
				goto error;
			if (fn(lower, upper, context_j, user) < 0)
				break;
		}

		isl_basic_set_free(context_i);

		if (j < bset->n_ineq)
			break;
	}

	isl_basic_set_free(context);

	if (i < bset->n_ineq)
		return isl_stat_error;

	return isl_stat_ok;
error:
	isl_constraint_free(lower);
	isl_constraint_free(upper);
	isl_basic_set_free(context_i);
	isl_basic_set_free(context_j);
	isl_basic_set_free(context);
	return isl_stat_error;
}

/* For each pair of lower and upper bounds on the variable "pos"
 * of type "type", call "fn" with these lower and upper bounds and the
 * set of constraints on the remaining variables where these bounds
 * are active, i.e., (stricly) larger/smaller than the other lower/upper bounds.
 *
 * If the designated variable is equal to an affine combination of the
 * other variables then fn is called with both lower and upper
 * set to the corresponding equality.
 *
 * If there is no lower (or upper) bound, then NULL is passed
 * as the corresponding bound.
 *
 * We first check if the variable is involved in any equality.
 * If not, we count the number of lower and upper bounds and
 * act accordingly.
 */
isl_stat isl_basic_set_foreach_bound_pair(__isl_keep isl_basic_set *bset,
	enum isl_dim_type type, unsigned pos,
	isl_stat (*fn)(__isl_take isl_constraint *lower,
		  __isl_take isl_constraint *upper,
		  __isl_take isl_basic_set *bset, void *user), void *user)
{
	int i;
	isl_constraint *lower = NULL;
	isl_constraint *upper = NULL;
	isl_basic_set *context = NULL;
	unsigned abs_pos;
	int n_lower, n_upper;

	if (!bset)
		return isl_stat_error;
	isl_assert(bset->ctx, pos < isl_basic_set_dim(bset, type),
		return isl_stat_error);
	isl_assert(bset->ctx, type == isl_dim_param || type == isl_dim_set,
		return isl_stat_error);

	abs_pos = pos;
	if (type == isl_dim_set)
		abs_pos += isl_basic_set_dim(bset, isl_dim_param);

	for (i = 0; i < bset->n_eq; ++i) {
		if (isl_int_is_zero(bset->eq[i][1 + abs_pos]))
			continue;

		lower = isl_basic_set_constraint(isl_basic_set_copy(bset),
						&bset->eq[i]);
		upper = isl_constraint_copy(lower);
		context = isl_basic_set_remove_dims(isl_basic_set_copy(bset),
					type, pos, 1);
		if (!lower || !upper || !context)
			goto error;
		return fn(lower, upper, context, user);
	}

	n_lower = 0;
	n_upper = 0;
	for (i = 0; i < bset->n_ineq; ++i) {
		if (isl_int_is_pos(bset->ineq[i][1 + abs_pos]))
			n_lower++;
		else if (isl_int_is_neg(bset->ineq[i][1 + abs_pos]))
			n_upper++;
	}

	context = isl_basic_set_copy(bset);
	context = isl_basic_set_cow(context);
	if (!context)
		goto error;
	for (i = context->n_ineq - 1; i >= 0; --i)
		if (!isl_int_is_zero(context->ineq[i][1 + abs_pos]))
			isl_basic_set_drop_inequality(context, i);

	context = isl_basic_set_drop(context, type, pos, 1);
	if (!n_lower && !n_upper)
		return fn(NULL, NULL, context, user);
	if (!n_lower)
		return foreach_upper_bound(bset, type, abs_pos, context, n_upper,
						fn, user);
	if (!n_upper)
		return foreach_lower_bound(bset, type, abs_pos, context, n_lower,
						fn, user);
	return foreach_bound_pair(bset, type, abs_pos, context, n_lower, n_upper,
					fn, user);
error:
	isl_constraint_free(lower);
	isl_constraint_free(upper);
	isl_basic_set_free(context);
	return isl_stat_error;
}

__isl_give isl_aff *isl_constraint_get_bound(
	__isl_keep isl_constraint *constraint, enum isl_dim_type type, int pos)
{
	isl_aff *aff;
	isl_ctx *ctx;

	if (!constraint)
		return NULL;
	ctx = isl_constraint_get_ctx(constraint);
	if (pos >= isl_constraint_dim(constraint, type))
		isl_die(ctx, isl_error_invalid,
			"index out of bounds", return NULL);
	if (isl_constraint_dim(constraint, isl_dim_in) != 0)
		isl_die(ctx, isl_error_invalid,
			"not a set constraint", return NULL);

	pos += offset(constraint, type);
	if (isl_int_is_zero(constraint->v->el[pos]))
		isl_die(ctx, isl_error_invalid,
			"constraint does not define a bound on given dimension",
			return NULL);

	aff = isl_aff_alloc(isl_local_space_copy(constraint->ls));
	if (!aff)
		return NULL;

	if (isl_int_is_neg(constraint->v->el[pos]))
		isl_seq_cpy(aff->v->el + 1, constraint->v->el, aff->v->size - 1);
	else
		isl_seq_neg(aff->v->el + 1, constraint->v->el, aff->v->size - 1);
	isl_int_set_si(aff->v->el[1 + pos], 0);
	isl_int_abs(aff->v->el[0], constraint->v->el[pos]);

	return aff;
}

/* For an inequality constraint
 *
 *	f >= 0
 *
 * or an equality constraint
 *
 *	f = 0
 *
 * return the affine expression f.
 */
__isl_give isl_aff *isl_constraint_get_aff(
	__isl_keep isl_constraint *constraint)
{
	isl_aff *aff;

	if (!constraint)
		return NULL;

	aff = isl_aff_alloc(isl_local_space_copy(constraint->ls));
	if (!aff)
		return NULL;

	isl_seq_cpy(aff->v->el + 1, constraint->v->el, aff->v->size - 1);
	isl_int_set_si(aff->v->el[0], 1);

	return aff;
}

/* Construct an inequality (eq = 0) or equality (eq = 1) constraint from "aff".
 * In particular, construct aff >= 0 or aff = 0.
 *
 * The denominator of "aff" can be ignored.
 */
static __isl_give isl_constraint *isl_constraint_alloc_aff(int eq,
	__isl_take isl_aff *aff)
{
	isl_local_space *ls;
	isl_vec *v;

	if (!aff)
		return NULL;
	ls = isl_aff_get_domain_local_space(aff);
	v = isl_vec_drop_els(isl_vec_copy(aff->v), 0, 1);
	isl_aff_free(aff);

	return isl_constraint_alloc_vec(eq, ls, v);
}

/* Construct an equality constraint equating the given affine expression
 * to zero.
 */
__isl_give isl_constraint *isl_equality_from_aff(__isl_take isl_aff *aff)
{
	return isl_constraint_alloc_aff(1, aff);
}

/* Construct an inequality constraint enforcing the given affine expression
 * to be non-negative.
 */
__isl_give isl_constraint *isl_inequality_from_aff(__isl_take isl_aff *aff)
{
	return isl_constraint_alloc_aff(0, aff);
}

/* Compare two isl_constraints.
 *
 * Return -1 if "c1" is "smaller" than "c2", 1 if "c1" is "greater"
 * than "c2" and 0 if they are equal.
 *
 * The order is fairly arbitrary.  We do consider constraints that only involve
 * earlier dimensions as "smaller".
 */
int isl_constraint_plain_cmp(__isl_keep isl_constraint *c1,
	__isl_keep isl_constraint *c2)
{
	int cmp;
	int last1, last2;

	if (c1 == c2)
		return 0;
	if (!c1)
		return -1;
	if (!c2)
		return 1;
	cmp = isl_local_space_cmp(c1->ls, c2->ls);
	if (cmp != 0)
		return cmp;

	last1 = isl_seq_last_non_zero(c1->v->el + 1, c1->v->size - 1);
	last2 = isl_seq_last_non_zero(c2->v->el + 1, c1->v->size - 1);
	if (last1 != last2)
		return last1 - last2;

	return isl_seq_cmp(c1->v->el, c2->v->el, c1->v->size);
}

/* Compare two constraints based on their final (non-zero) coefficients.
 * In particular, the constraint that involves later variables or
 * that has a larger coefficient for a shared latest variable
 * is considered "greater" than the other constraint.
 *
 * Return -1 if "c1" is "smaller" than "c2", 1 if "c1" is "greater"
 * than "c2" and 0 if they are equal.
 *
 * If the constraints live in different local spaces, then we cannot
 * really compare the constraints so we compare the local spaces instead.
 */
int isl_constraint_cmp_last_non_zero(__isl_keep isl_constraint *c1,
	__isl_keep isl_constraint *c2)
{
	int cmp;
	int last1, last2;

	if (c1 == c2)
		return 0;
	if (!c1)
		return -1;
	if (!c2)
		return 1;
	cmp = isl_local_space_cmp(c1->ls, c2->ls);
	if (cmp != 0)
		return cmp;

	last1 = isl_seq_last_non_zero(c1->v->el + 1, c1->v->size - 1);
	last2 = isl_seq_last_non_zero(c2->v->el + 1, c1->v->size - 1);
	if (last1 != last2)
		return last1 - last2;
	if (last1 == -1)
		return 0;
	return isl_int_abs_cmp(c1->v->el[1 + last1], c2->v->el[1 + last2]);
}