isl_constraint.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371
  1. /*
  2. * Copyright 2008-2009 Katholieke Universiteit Leuven
  3. * Copyright 2010 INRIA Saclay
  4. *
  5. * Use of this software is governed by the MIT license
  6. *
  7. * Written by Sven Verdoolaege, K.U.Leuven, Departement
  8. * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
  9. * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
  10. * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
  11. */
  12. #include <isl_map_private.h>
  13. #include <isl_constraint_private.h>
  14. #include <isl_space_private.h>
  15. #include <isl_seq.h>
  16. #include <isl_aff_private.h>
  17. #include <isl_local_space_private.h>
  18. #include <isl_val_private.h>
  19. #include <isl_vec_private.h>
  20. #include <bset_to_bmap.c>
  21. #include <bset_from_bmap.c>
  22. #undef EL_BASE
  23. #define EL_BASE constraint
  24. #include <isl_list_templ.c>
  25. isl_ctx *isl_constraint_get_ctx(__isl_keep isl_constraint *c)
  26. {
  27. return c ? isl_local_space_get_ctx(c->ls) : NULL;
  28. }
  29. static isl_size n(__isl_keep isl_constraint *c, enum isl_dim_type type)
  30. {
  31. return isl_local_space_dim(c->ls, type);
  32. }
  33. static unsigned offset(__isl_keep isl_constraint *c, enum isl_dim_type type)
  34. {
  35. return isl_local_space_offset(c->ls, type);
  36. }
  37. __isl_give isl_constraint *isl_constraint_alloc_vec(int eq,
  38. __isl_take isl_local_space *ls, __isl_take isl_vec *v)
  39. {
  40. isl_constraint *constraint;
  41. if (!ls || !v)
  42. goto error;
  43. constraint = isl_alloc_type(isl_vec_get_ctx(v), isl_constraint);
  44. if (!constraint)
  45. goto error;
  46. constraint->ref = 1;
  47. constraint->eq = eq;
  48. constraint->ls = ls;
  49. constraint->v = v;
  50. return constraint;
  51. error:
  52. isl_local_space_free(ls);
  53. isl_vec_free(v);
  54. return NULL;
  55. }
  56. __isl_give isl_constraint *isl_constraint_alloc(int eq,
  57. __isl_take isl_local_space *ls)
  58. {
  59. isl_size dim;
  60. isl_ctx *ctx;
  61. isl_vec *v;
  62. dim = isl_local_space_dim(ls, isl_dim_all);
  63. if (dim < 0)
  64. ls = isl_local_space_free(ls);
  65. if (!ls)
  66. return NULL;
  67. ctx = isl_local_space_get_ctx(ls);
  68. v = isl_vec_alloc(ctx, 1 + dim);
  69. v = isl_vec_clr(v);
  70. return isl_constraint_alloc_vec(eq, ls, v);
  71. }
  72. __isl_give isl_constraint *isl_basic_map_constraint(
  73. __isl_take isl_basic_map *bmap, isl_int **line)
  74. {
  75. int eq;
  76. isl_size dim;
  77. isl_ctx *ctx;
  78. isl_vec *v;
  79. isl_local_space *ls = NULL;
  80. isl_constraint *constraint;
  81. if (!bmap || !line)
  82. goto error;
  83. eq = line >= bmap->eq;
  84. ctx = isl_basic_map_get_ctx(bmap);
  85. ls = isl_basic_map_get_local_space(bmap);
  86. dim = isl_local_space_dim(ls, isl_dim_all);
  87. if (dim < 0)
  88. goto error;
  89. v = isl_vec_alloc(ctx, 1 + dim);
  90. if (!v)
  91. goto error;
  92. isl_seq_cpy(v->el, line[0], v->size);
  93. constraint = isl_constraint_alloc_vec(eq, ls, v);
  94. isl_basic_map_free(bmap);
  95. return constraint;
  96. error:
  97. isl_local_space_free(ls);
  98. isl_basic_map_free(bmap);
  99. return NULL;
  100. }
  101. __isl_give isl_constraint *isl_basic_set_constraint(
  102. __isl_take isl_basic_set *bset, isl_int **line)
  103. {
  104. return isl_basic_map_constraint(bset_to_bmap(bset), line);
  105. }
  106. __isl_give isl_constraint *isl_constraint_alloc_equality(
  107. __isl_take isl_local_space *ls)
  108. {
  109. return isl_constraint_alloc(1, ls);
  110. }
  111. __isl_give isl_constraint *isl_constraint_alloc_inequality(
  112. __isl_take isl_local_space *ls)
  113. {
  114. return isl_constraint_alloc(0, ls);
  115. }
  116. __isl_give isl_constraint *isl_constraint_dup(__isl_keep isl_constraint *c)
  117. {
  118. if (!c)
  119. return NULL;
  120. return isl_constraint_alloc_vec(c->eq, isl_local_space_copy(c->ls),
  121. isl_vec_copy(c->v));
  122. }
  123. __isl_give isl_constraint *isl_constraint_cow(__isl_take isl_constraint *c)
  124. {
  125. if (!c)
  126. return NULL;
  127. if (c->ref == 1)
  128. return c;
  129. c->ref--;
  130. return isl_constraint_dup(c);
  131. }
  132. __isl_give isl_constraint *isl_constraint_copy(
  133. __isl_keep isl_constraint *constraint)
  134. {
  135. if (!constraint)
  136. return NULL;
  137. constraint->ref++;
  138. return constraint;
  139. }
  140. __isl_null isl_constraint *isl_constraint_free(__isl_take isl_constraint *c)
  141. {
  142. if (!c)
  143. return NULL;
  144. if (--c->ref > 0)
  145. return NULL;
  146. isl_local_space_free(c->ls);
  147. isl_vec_free(c->v);
  148. free(c);
  149. return NULL;
  150. }
  151. /* Return the number of constraints in "bmap", i.e., the
  152. * number of times isl_basic_map_foreach_constraint will
  153. * call the callback.
  154. */
  155. isl_size isl_basic_map_n_constraint(__isl_keep isl_basic_map *bmap)
  156. {
  157. if (!bmap)
  158. return isl_size_error;
  159. return bmap->n_eq + bmap->n_ineq;
  160. }
  161. /* Return the number of constraints in "bset", i.e., the
  162. * number of times isl_basic_set_foreach_constraint will
  163. * call the callback.
  164. */
  165. isl_size isl_basic_set_n_constraint(__isl_keep isl_basic_set *bset)
  166. {
  167. return isl_basic_map_n_constraint(bset);
  168. }
  169. isl_stat isl_basic_map_foreach_constraint(__isl_keep isl_basic_map *bmap,
  170. isl_stat (*fn)(__isl_take isl_constraint *c, void *user), void *user)
  171. {
  172. int i;
  173. struct isl_constraint *c;
  174. if (!bmap)
  175. return isl_stat_error;
  176. isl_assert(bmap->ctx, ISL_F_ISSET(bmap, ISL_BASIC_MAP_FINAL),
  177. return isl_stat_error);
  178. for (i = 0; i < bmap->n_eq; ++i) {
  179. c = isl_basic_map_constraint(isl_basic_map_copy(bmap),
  180. &bmap->eq[i]);
  181. if (!c)
  182. return isl_stat_error;
  183. if (fn(c, user) < 0)
  184. return isl_stat_error;
  185. }
  186. for (i = 0; i < bmap->n_ineq; ++i) {
  187. c = isl_basic_map_constraint(isl_basic_map_copy(bmap),
  188. &bmap->ineq[i]);
  189. if (!c)
  190. return isl_stat_error;
  191. if (fn(c, user) < 0)
  192. return isl_stat_error;
  193. }
  194. return isl_stat_ok;
  195. }
  196. isl_stat isl_basic_set_foreach_constraint(__isl_keep isl_basic_set *bset,
  197. isl_stat (*fn)(__isl_take isl_constraint *c, void *user), void *user)
  198. {
  199. return isl_basic_map_foreach_constraint(bset_to_bmap(bset), fn, user);
  200. }
  201. /* Add the constraint to the list that "user" points to, if it is not
  202. * a div constraint.
  203. */
  204. static isl_stat collect_constraint(__isl_take isl_constraint *constraint,
  205. void *user)
  206. {
  207. isl_constraint_list **list = user;
  208. isl_bool is_div;
  209. is_div = isl_constraint_is_div_constraint(constraint);
  210. if (is_div < 0 || is_div)
  211. isl_constraint_free(constraint);
  212. else
  213. *list = isl_constraint_list_add(*list, constraint);
  214. return is_div < 0 ? isl_stat_error : isl_stat_ok;
  215. }
  216. /* Return a list of constraints that, when combined, are equivalent
  217. * to "bmap". The input is required to have only known divs.
  218. *
  219. * There is no need to include the div constraints as they are
  220. * implied by the div expressions.
  221. */
  222. __isl_give isl_constraint_list *isl_basic_map_get_constraint_list(
  223. __isl_keep isl_basic_map *bmap)
  224. {
  225. isl_size n;
  226. isl_bool known;
  227. isl_ctx *ctx;
  228. isl_constraint_list *list;
  229. known = isl_basic_map_divs_known(bmap);
  230. if (known < 0)
  231. return NULL;
  232. ctx = isl_basic_map_get_ctx(bmap);
  233. if (!known)
  234. isl_die(ctx, isl_error_invalid,
  235. "input involves unknown divs", return NULL);
  236. n = isl_basic_map_n_constraint(bmap);
  237. if (n < 0)
  238. return NULL;
  239. list = isl_constraint_list_alloc(ctx, n);
  240. if (isl_basic_map_foreach_constraint(bmap,
  241. &collect_constraint, &list) < 0)
  242. list = isl_constraint_list_free(list);
  243. return list;
  244. }
  245. /* Return a list of constraints that, when combined, are equivalent
  246. * to "bset". The input is required to have only known divs.
  247. */
  248. __isl_give isl_constraint_list *isl_basic_set_get_constraint_list(
  249. __isl_keep isl_basic_set *bset)
  250. {
  251. return isl_basic_map_get_constraint_list(bset);
  252. }
  253. int isl_constraint_is_equal(__isl_keep isl_constraint *constraint1,
  254. __isl_keep isl_constraint *constraint2)
  255. {
  256. int equal;
  257. if (!constraint1 || !constraint2)
  258. return 0;
  259. if (constraint1->eq != constraint2->eq)
  260. return 0;
  261. equal = isl_local_space_is_equal(constraint1->ls, constraint2->ls);
  262. if (equal < 0 || !equal)
  263. return equal;
  264. return isl_vec_is_equal(constraint1->v, constraint2->v);
  265. }
  266. __isl_give isl_basic_map *isl_basic_map_add_constraint(
  267. __isl_take isl_basic_map *bmap, __isl_take isl_constraint *constraint)
  268. {
  269. isl_ctx *ctx;
  270. isl_space *space;
  271. int equal_space;
  272. if (!bmap || !constraint)
  273. goto error;
  274. ctx = isl_constraint_get_ctx(constraint);
  275. space = isl_constraint_get_space(constraint);
  276. equal_space = isl_space_is_equal(bmap->dim, space);
  277. isl_space_free(space);
  278. isl_assert(ctx, equal_space, goto error);
  279. bmap = isl_basic_map_intersect(bmap,
  280. isl_basic_map_from_constraint(constraint));
  281. return bmap;
  282. error:
  283. isl_basic_map_free(bmap);
  284. isl_constraint_free(constraint);
  285. return NULL;
  286. }
  287. __isl_give isl_basic_set *isl_basic_set_add_constraint(
  288. __isl_take isl_basic_set *bset, __isl_take isl_constraint *constraint)
  289. {
  290. return bset_from_bmap(isl_basic_map_add_constraint(bset_to_bmap(bset),
  291. constraint));
  292. }
  293. __isl_give isl_map *isl_map_add_constraint(__isl_take isl_map *map,
  294. __isl_take isl_constraint *constraint)
  295. {
  296. isl_basic_map *bmap;
  297. bmap = isl_basic_map_from_constraint(constraint);
  298. map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
  299. return map;
  300. }
  301. __isl_give isl_set *isl_set_add_constraint(__isl_take isl_set *set,
  302. __isl_take isl_constraint *constraint)
  303. {
  304. return isl_map_add_constraint(set, constraint);
  305. }
  306. /* Return the space of "constraint".
  307. */
  308. static __isl_keep isl_space *isl_constraint_peek_space(
  309. __isl_keep isl_constraint *constraint)
  310. {
  311. return constraint ? isl_local_space_peek_space(constraint->ls) : NULL;
  312. }
  313. __isl_give isl_space *isl_constraint_get_space(
  314. __isl_keep isl_constraint *constraint)
  315. {
  316. return constraint ? isl_local_space_get_space(constraint->ls) : NULL;
  317. }
  318. __isl_give isl_local_space *isl_constraint_get_local_space(
  319. __isl_keep isl_constraint *constraint)
  320. {
  321. return constraint ? isl_local_space_copy(constraint->ls) : NULL;
  322. }
  323. isl_size isl_constraint_dim(__isl_keep isl_constraint *constraint,
  324. enum isl_dim_type type)
  325. {
  326. if (!constraint)
  327. return isl_size_error;
  328. return n(constraint, type);
  329. }
  330. #undef TYPE
  331. #define TYPE isl_constraint
  332. static
  333. #include "check_type_range_templ.c"
  334. isl_bool isl_constraint_involves_dims(__isl_keep isl_constraint *constraint,
  335. enum isl_dim_type type, unsigned first, unsigned n)
  336. {
  337. int i;
  338. int *active = NULL;
  339. isl_bool involves = isl_bool_false;
  340. if (!constraint)
  341. return isl_bool_error;
  342. if (n == 0)
  343. return isl_bool_false;
  344. if (isl_constraint_check_range(constraint, type, first, n) < 0)
  345. return isl_bool_error;
  346. active = isl_local_space_get_active(constraint->ls,
  347. constraint->v->el + 1);
  348. if (!active)
  349. goto error;
  350. first += isl_local_space_offset(constraint->ls, type) - 1;
  351. for (i = 0; i < n; ++i)
  352. if (active[first + i]) {
  353. involves = isl_bool_true;
  354. break;
  355. }
  356. free(active);
  357. return involves;
  358. error:
  359. free(active);
  360. return isl_bool_error;
  361. }
  362. /* Does the given constraint represent a lower bound on the given
  363. * dimension?
  364. */
  365. isl_bool isl_constraint_is_lower_bound(__isl_keep isl_constraint *constraint,
  366. enum isl_dim_type type, unsigned pos)
  367. {
  368. if (isl_constraint_check_range(constraint, type, pos, 1) < 0)
  369. return isl_bool_error;
  370. pos += isl_local_space_offset(constraint->ls, type);
  371. return isl_bool_ok(isl_int_is_pos(constraint->v->el[pos]));
  372. }
  373. /* Does the given constraint represent an upper bound on the given
  374. * dimension?
  375. */
  376. isl_bool isl_constraint_is_upper_bound(__isl_keep isl_constraint *constraint,
  377. enum isl_dim_type type, unsigned pos)
  378. {
  379. if (isl_constraint_check_range(constraint, type, pos, 1) < 0)
  380. return isl_bool_error;
  381. pos += isl_local_space_offset(constraint->ls, type);
  382. return isl_bool_ok(isl_int_is_neg(constraint->v->el[pos]));
  383. }
  384. const char *isl_constraint_get_dim_name(__isl_keep isl_constraint *constraint,
  385. enum isl_dim_type type, unsigned pos)
  386. {
  387. return constraint ?
  388. isl_local_space_get_dim_name(constraint->ls, type, pos) : NULL;
  389. }
  390. void isl_constraint_get_constant(__isl_keep isl_constraint *constraint,
  391. isl_int *v)
  392. {
  393. if (!constraint)
  394. return;
  395. isl_int_set(*v, constraint->v->el[0]);
  396. }
  397. /* Return the constant term of "constraint".
  398. */
  399. __isl_give isl_val *isl_constraint_get_constant_val(
  400. __isl_keep isl_constraint *constraint)
  401. {
  402. isl_ctx *ctx;
  403. if (!constraint)
  404. return NULL;
  405. ctx = isl_constraint_get_ctx(constraint);
  406. return isl_val_int_from_isl_int(ctx, constraint->v->el[0]);
  407. }
  408. void isl_constraint_get_coefficient(__isl_keep isl_constraint *constraint,
  409. enum isl_dim_type type, int pos, isl_int *v)
  410. {
  411. if (isl_constraint_check_range(constraint, type, pos, 1) < 0)
  412. return;
  413. pos += isl_local_space_offset(constraint->ls, type);
  414. isl_int_set(*v, constraint->v->el[pos]);
  415. }
  416. /* Return the coefficient of the variable of type "type" at position "pos"
  417. * of "constraint".
  418. */
  419. __isl_give isl_val *isl_constraint_get_coefficient_val(
  420. __isl_keep isl_constraint *constraint, enum isl_dim_type type, int pos)
  421. {
  422. isl_ctx *ctx;
  423. if (isl_constraint_check_range(constraint, type, pos, 1) < 0)
  424. return NULL;
  425. ctx = isl_constraint_get_ctx(constraint);
  426. pos += isl_local_space_offset(constraint->ls, type);
  427. return isl_val_int_from_isl_int(ctx, constraint->v->el[pos]);
  428. }
  429. __isl_give isl_aff *isl_constraint_get_div(__isl_keep isl_constraint *constraint,
  430. int pos)
  431. {
  432. if (!constraint)
  433. return NULL;
  434. return isl_local_space_get_div(constraint->ls, pos);
  435. }
  436. __isl_give isl_constraint *isl_constraint_set_constant(
  437. __isl_take isl_constraint *constraint, isl_int v)
  438. {
  439. constraint = isl_constraint_cow(constraint);
  440. if (!constraint)
  441. return NULL;
  442. constraint->v = isl_vec_cow(constraint->v);
  443. if (!constraint->v)
  444. return isl_constraint_free(constraint);
  445. isl_int_set(constraint->v->el[0], v);
  446. return constraint;
  447. }
  448. /* Replace the constant term of "constraint" by "v".
  449. */
  450. __isl_give isl_constraint *isl_constraint_set_constant_val(
  451. __isl_take isl_constraint *constraint, __isl_take isl_val *v)
  452. {
  453. constraint = isl_constraint_cow(constraint);
  454. if (!constraint || !v)
  455. goto error;
  456. if (!isl_val_is_int(v))
  457. isl_die(isl_constraint_get_ctx(constraint), isl_error_invalid,
  458. "expecting integer value", goto error);
  459. constraint->v = isl_vec_set_element_val(constraint->v, 0, v);
  460. if (!constraint->v)
  461. constraint = isl_constraint_free(constraint);
  462. return constraint;
  463. error:
  464. isl_val_free(v);
  465. return isl_constraint_free(constraint);
  466. }
  467. __isl_give isl_constraint *isl_constraint_set_constant_si(
  468. __isl_take isl_constraint *constraint, int v)
  469. {
  470. constraint = isl_constraint_cow(constraint);
  471. if (!constraint)
  472. return NULL;
  473. constraint->v = isl_vec_cow(constraint->v);
  474. if (!constraint->v)
  475. return isl_constraint_free(constraint);
  476. isl_int_set_si(constraint->v->el[0], v);
  477. return constraint;
  478. }
  479. /* Replace the coefficient of the variable of type "type" at position "pos"
  480. * of "constraint" by "v".
  481. */
  482. __isl_give isl_constraint *isl_constraint_set_coefficient_val(
  483. __isl_take isl_constraint *constraint,
  484. enum isl_dim_type type, int pos, __isl_take isl_val *v)
  485. {
  486. constraint = isl_constraint_cow(constraint);
  487. if (!constraint || !v)
  488. goto error;
  489. if (!isl_val_is_int(v))
  490. isl_die(isl_constraint_get_ctx(constraint), isl_error_invalid,
  491. "expecting integer value", goto error);
  492. if (isl_constraint_check_range(constraint, type, pos, 1) < 0)
  493. goto error;
  494. pos += isl_local_space_offset(constraint->ls, type);
  495. constraint->v = isl_vec_set_element_val(constraint->v, pos, v);
  496. if (!constraint->v)
  497. constraint = isl_constraint_free(constraint);
  498. return constraint;
  499. error:
  500. isl_val_free(v);
  501. return isl_constraint_free(constraint);
  502. }
  503. __isl_give isl_constraint *isl_constraint_set_coefficient_si(
  504. __isl_take isl_constraint *constraint,
  505. enum isl_dim_type type, int pos, int v)
  506. {
  507. constraint = isl_constraint_cow(constraint);
  508. if (isl_constraint_check_range(constraint, type, pos, 1) < 0)
  509. return isl_constraint_free(constraint);
  510. constraint->v = isl_vec_cow(constraint->v);
  511. if (!constraint->v)
  512. return isl_constraint_free(constraint);
  513. pos += isl_local_space_offset(constraint->ls, type);
  514. isl_int_set_si(constraint->v->el[pos], v);
  515. return constraint;
  516. }
  517. __isl_give isl_constraint *isl_constraint_negate(
  518. __isl_take isl_constraint *constraint)
  519. {
  520. isl_ctx *ctx;
  521. constraint = isl_constraint_cow(constraint);
  522. if (!constraint)
  523. return NULL;
  524. ctx = isl_constraint_get_ctx(constraint);
  525. if (isl_constraint_is_equality(constraint))
  526. isl_die(ctx, isl_error_invalid, "cannot negate equality",
  527. return isl_constraint_free(constraint));
  528. constraint->v = isl_vec_neg(constraint->v);
  529. constraint->v = isl_vec_cow(constraint->v);
  530. if (!constraint->v)
  531. return isl_constraint_free(constraint);
  532. isl_int_sub_ui(constraint->v->el[0], constraint->v->el[0], 1);
  533. return constraint;
  534. }
  535. isl_bool isl_constraint_is_equality(struct isl_constraint *constraint)
  536. {
  537. if (!constraint)
  538. return isl_bool_error;
  539. return isl_bool_ok(constraint->eq);
  540. }
  541. isl_bool isl_constraint_is_div_constraint(__isl_keep isl_constraint *constraint)
  542. {
  543. int i;
  544. isl_size n_div;
  545. if (!constraint)
  546. return isl_bool_error;
  547. if (isl_constraint_is_equality(constraint))
  548. return isl_bool_false;
  549. n_div = isl_constraint_dim(constraint, isl_dim_div);
  550. if (n_div < 0)
  551. return isl_bool_error;
  552. for (i = 0; i < n_div; ++i) {
  553. isl_bool is_div;
  554. is_div = isl_local_space_is_div_constraint(constraint->ls,
  555. constraint->v->el, i);
  556. if (is_div < 0 || is_div)
  557. return is_div;
  558. }
  559. return isl_bool_false;
  560. }
  561. /* Is "constraint" an equality that corresponds to integer division "div"?
  562. *
  563. * That is, given an integer division of the form
  564. *
  565. * a = floor((f + c)/m)
  566. *
  567. * is the equality of the form
  568. *
  569. * -f + m d + c' = 0
  570. * ?
  571. * Note that the constant term is not checked explicitly, but given
  572. * that this is a valid equality constraint, the constant c' necessarily
  573. * has a value close to -c.
  574. */
  575. isl_bool isl_constraint_is_div_equality(__isl_keep isl_constraint *constraint,
  576. unsigned div)
  577. {
  578. isl_bool equality;
  579. equality = isl_constraint_is_equality(constraint);
  580. if (equality < 0 || !equality)
  581. return equality;
  582. return isl_local_space_is_div_equality(constraint->ls,
  583. constraint->v->el, div);
  584. }
  585. /* We manually set ISL_BASIC_SET_FINAL instead of calling
  586. * isl_basic_map_finalize because we want to keep the position
  587. * of the divs and we therefore do not want to throw away redundant divs.
  588. * This is arguably a bit fragile.
  589. */
  590. __isl_give isl_basic_map *isl_basic_map_from_constraint(
  591. __isl_take isl_constraint *constraint)
  592. {
  593. int k;
  594. isl_local_space *ls;
  595. struct isl_basic_map *bmap;
  596. isl_int *c;
  597. isl_size total;
  598. if (!constraint)
  599. return NULL;
  600. ls = isl_local_space_copy(constraint->ls);
  601. bmap = isl_basic_map_from_local_space(ls);
  602. bmap = isl_basic_map_extend_constraints(bmap, 1, 1);
  603. if (isl_constraint_is_equality(constraint)) {
  604. k = isl_basic_map_alloc_equality(bmap);
  605. if (k < 0)
  606. goto error;
  607. c = bmap->eq[k];
  608. }
  609. else {
  610. k = isl_basic_map_alloc_inequality(bmap);
  611. if (k < 0)
  612. goto error;
  613. c = bmap->ineq[k];
  614. }
  615. total = isl_basic_map_dim(bmap, isl_dim_all);
  616. if (total < 0)
  617. goto error;
  618. isl_seq_cpy(c, constraint->v->el, 1 + total);
  619. isl_constraint_free(constraint);
  620. if (bmap)
  621. ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
  622. return bmap;
  623. error:
  624. isl_constraint_free(constraint);
  625. isl_basic_map_free(bmap);
  626. return NULL;
  627. }
  628. __isl_give isl_basic_set *isl_basic_set_from_constraint(
  629. __isl_take isl_constraint *constraint)
  630. {
  631. isl_space *space;
  632. space = isl_constraint_peek_space(constraint);
  633. if (isl_space_check_is_set(space) < 0)
  634. goto error;
  635. return bset_from_bmap(isl_basic_map_from_constraint(constraint));
  636. error:
  637. isl_constraint_free(constraint);
  638. return NULL;
  639. }
  640. /* Is the variable of "type" at position "pos" of "bmap" defined
  641. * in terms of earlier dimensions through an equality?
  642. *
  643. * If so, and if c is not NULL, then return a copy of this equality in *c.
  644. */
  645. isl_bool isl_basic_map_has_defining_equality(
  646. __isl_keep isl_basic_map *bmap, enum isl_dim_type type, int pos,
  647. __isl_give isl_constraint **c)
  648. {
  649. int i;
  650. unsigned offset;
  651. isl_size total;
  652. if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
  653. return isl_bool_error;
  654. offset = isl_basic_map_offset(bmap, type);
  655. total = isl_basic_map_dim(bmap, isl_dim_all);
  656. if (total < 0)
  657. return isl_bool_error;
  658. for (i = 0; i < bmap->n_eq; ++i) {
  659. if (isl_int_is_zero(bmap->eq[i][offset + pos]) ||
  660. isl_seq_first_non_zero(bmap->eq[i]+offset+pos+1,
  661. 1+total-offset-pos-1) != -1)
  662. continue;
  663. if (c)
  664. *c = isl_basic_map_constraint(isl_basic_map_copy(bmap),
  665. &bmap->eq[i]);
  666. return isl_bool_true;
  667. }
  668. return isl_bool_false;
  669. }
  670. /* Is the variable of "type" at position "pos" of "bset" defined
  671. * in terms of earlier dimensions through an equality?
  672. *
  673. * If so, and if c is not NULL, then return a copy of this equality in *c.
  674. */
  675. isl_bool isl_basic_set_has_defining_equality(
  676. __isl_keep isl_basic_set *bset, enum isl_dim_type type, int pos,
  677. __isl_give isl_constraint **c)
  678. {
  679. return isl_basic_map_has_defining_equality(bset_to_bmap(bset),
  680. type, pos, c);
  681. }
  682. isl_bool isl_basic_set_has_defining_inequalities(
  683. struct isl_basic_set *bset, enum isl_dim_type type, int pos,
  684. struct isl_constraint **lower,
  685. struct isl_constraint **upper)
  686. {
  687. int i, j;
  688. unsigned offset;
  689. isl_size total;
  690. isl_int m;
  691. isl_int **lower_line, **upper_line;
  692. if (isl_basic_set_check_range(bset, type, pos, 1) < 0)
  693. return isl_bool_error;
  694. offset = isl_basic_set_offset(bset, type);
  695. total = isl_basic_set_dim(bset, isl_dim_all);
  696. if (total < 0)
  697. return isl_bool_error;
  698. isl_int_init(m);
  699. for (i = 0; i < bset->n_ineq; ++i) {
  700. if (isl_int_is_zero(bset->ineq[i][offset + pos]))
  701. continue;
  702. if (isl_int_is_one(bset->ineq[i][offset + pos]))
  703. continue;
  704. if (isl_int_is_negone(bset->ineq[i][offset + pos]))
  705. continue;
  706. if (isl_seq_first_non_zero(bset->ineq[i]+offset+pos+1,
  707. 1+total-offset-pos-1) != -1)
  708. continue;
  709. for (j = i + 1; j < bset->n_ineq; ++j) {
  710. if (!isl_seq_is_neg(bset->ineq[i]+1, bset->ineq[j]+1,
  711. total))
  712. continue;
  713. isl_int_add(m, bset->ineq[i][0], bset->ineq[j][0]);
  714. if (isl_int_abs_ge(m, bset->ineq[i][offset+pos]))
  715. continue;
  716. if (isl_int_is_pos(bset->ineq[i][offset+pos])) {
  717. lower_line = &bset->ineq[i];
  718. upper_line = &bset->ineq[j];
  719. } else {
  720. lower_line = &bset->ineq[j];
  721. upper_line = &bset->ineq[i];
  722. }
  723. *lower = isl_basic_set_constraint(
  724. isl_basic_set_copy(bset), lower_line);
  725. *upper = isl_basic_set_constraint(
  726. isl_basic_set_copy(bset), upper_line);
  727. isl_int_clear(m);
  728. return isl_bool_true;
  729. }
  730. }
  731. *lower = NULL;
  732. *upper = NULL;
  733. isl_int_clear(m);
  734. return isl_bool_false;
  735. }
  736. /* Given two constraints "a" and "b" on the variable at position "abs_pos"
  737. * (in "a" and "b"), add a constraint to "bset" that ensures that the
  738. * bound implied by "a" is (strictly) larger than the bound implied by "b".
  739. *
  740. * If both constraints imply lower bounds, then this means that "a" is
  741. * active in the result.
  742. * If both constraints imply upper bounds, then this means that "b" is
  743. * active in the result.
  744. */
  745. static __isl_give isl_basic_set *add_larger_bound_constraint(
  746. __isl_take isl_basic_set *bset, isl_int *a, isl_int *b,
  747. unsigned abs_pos, int strict)
  748. {
  749. int k;
  750. isl_int t;
  751. isl_size total;
  752. total = isl_basic_set_dim(bset, isl_dim_all);
  753. if (total < 0)
  754. return isl_basic_set_free(bset);
  755. k = isl_basic_set_alloc_inequality(bset);
  756. if (k < 0)
  757. goto error;
  758. isl_int_init(t);
  759. isl_int_neg(t, b[1 + abs_pos]);
  760. isl_seq_combine(bset->ineq[k], t, a, a[1 + abs_pos], b, 1 + abs_pos);
  761. isl_seq_combine(bset->ineq[k] + 1 + abs_pos,
  762. t, a + 1 + abs_pos + 1, a[1 + abs_pos], b + 1 + abs_pos + 1,
  763. total - abs_pos);
  764. if (strict)
  765. isl_int_sub_ui(bset->ineq[k][0], bset->ineq[k][0], 1);
  766. isl_int_clear(t);
  767. return bset;
  768. error:
  769. isl_basic_set_free(bset);
  770. return NULL;
  771. }
  772. /* Add constraints to "context" that ensure that "u" is the smallest
  773. * (and therefore active) upper bound on "abs_pos" in "bset" and return
  774. * the resulting basic set.
  775. */
  776. static __isl_give isl_basic_set *set_smallest_upper_bound(
  777. __isl_keep isl_basic_set *context,
  778. __isl_keep isl_basic_set *bset, unsigned abs_pos, int n_upper, int u)
  779. {
  780. int j;
  781. context = isl_basic_set_copy(context);
  782. context = isl_basic_set_cow(context);
  783. context = isl_basic_set_extend_constraints(context, 0, n_upper - 1);
  784. for (j = 0; j < bset->n_ineq; ++j) {
  785. if (j == u)
  786. continue;
  787. if (!isl_int_is_neg(bset->ineq[j][1 + abs_pos]))
  788. continue;
  789. context = add_larger_bound_constraint(context,
  790. bset->ineq[j], bset->ineq[u], abs_pos, j > u);
  791. }
  792. context = isl_basic_set_simplify(context);
  793. context = isl_basic_set_finalize(context);
  794. return context;
  795. }
  796. /* Add constraints to "context" that ensure that "u" is the largest
  797. * (and therefore active) upper bound on "abs_pos" in "bset" and return
  798. * the resulting basic set.
  799. */
  800. static __isl_give isl_basic_set *set_largest_lower_bound(
  801. __isl_keep isl_basic_set *context,
  802. __isl_keep isl_basic_set *bset, unsigned abs_pos, int n_lower, int l)
  803. {
  804. int j;
  805. context = isl_basic_set_copy(context);
  806. context = isl_basic_set_cow(context);
  807. context = isl_basic_set_extend_constraints(context, 0, n_lower - 1);
  808. for (j = 0; j < bset->n_ineq; ++j) {
  809. if (j == l)
  810. continue;
  811. if (!isl_int_is_pos(bset->ineq[j][1 + abs_pos]))
  812. continue;
  813. context = add_larger_bound_constraint(context,
  814. bset->ineq[l], bset->ineq[j], abs_pos, j > l);
  815. }
  816. context = isl_basic_set_simplify(context);
  817. context = isl_basic_set_finalize(context);
  818. return context;
  819. }
  820. static isl_stat foreach_upper_bound(__isl_keep isl_basic_set *bset,
  821. enum isl_dim_type type, unsigned abs_pos,
  822. __isl_take isl_basic_set *context, int n_upper,
  823. isl_stat (*fn)(__isl_take isl_constraint *lower,
  824. __isl_take isl_constraint *upper,
  825. __isl_take isl_basic_set *bset, void *user), void *user)
  826. {
  827. isl_basic_set *context_i;
  828. isl_constraint *upper = NULL;
  829. int i;
  830. for (i = 0; i < bset->n_ineq; ++i) {
  831. if (isl_int_is_zero(bset->ineq[i][1 + abs_pos]))
  832. continue;
  833. context_i = set_smallest_upper_bound(context, bset,
  834. abs_pos, n_upper, i);
  835. if (isl_basic_set_is_empty(context_i)) {
  836. isl_basic_set_free(context_i);
  837. continue;
  838. }
  839. upper = isl_basic_set_constraint(isl_basic_set_copy(bset),
  840. &bset->ineq[i]);
  841. if (!upper || !context_i)
  842. goto error;
  843. if (fn(NULL, upper, context_i, user) < 0)
  844. break;
  845. }
  846. isl_basic_set_free(context);
  847. if (i < bset->n_ineq)
  848. return isl_stat_error;
  849. return isl_stat_ok;
  850. error:
  851. isl_constraint_free(upper);
  852. isl_basic_set_free(context_i);
  853. isl_basic_set_free(context);
  854. return isl_stat_error;
  855. }
  856. static isl_stat foreach_lower_bound(__isl_keep isl_basic_set *bset,
  857. enum isl_dim_type type, unsigned abs_pos,
  858. __isl_take isl_basic_set *context, int n_lower,
  859. isl_stat (*fn)(__isl_take isl_constraint *lower,
  860. __isl_take isl_constraint *upper,
  861. __isl_take isl_basic_set *bset, void *user), void *user)
  862. {
  863. isl_basic_set *context_i;
  864. isl_constraint *lower = NULL;
  865. int i;
  866. for (i = 0; i < bset->n_ineq; ++i) {
  867. if (isl_int_is_zero(bset->ineq[i][1 + abs_pos]))
  868. continue;
  869. context_i = set_largest_lower_bound(context, bset,
  870. abs_pos, n_lower, i);
  871. if (isl_basic_set_is_empty(context_i)) {
  872. isl_basic_set_free(context_i);
  873. continue;
  874. }
  875. lower = isl_basic_set_constraint(isl_basic_set_copy(bset),
  876. &bset->ineq[i]);
  877. if (!lower || !context_i)
  878. goto error;
  879. if (fn(lower, NULL, context_i, user) < 0)
  880. break;
  881. }
  882. isl_basic_set_free(context);
  883. if (i < bset->n_ineq)
  884. return isl_stat_error;
  885. return isl_stat_ok;
  886. error:
  887. isl_constraint_free(lower);
  888. isl_basic_set_free(context_i);
  889. isl_basic_set_free(context);
  890. return isl_stat_error;
  891. }
  892. static isl_stat foreach_bound_pair(__isl_keep isl_basic_set *bset,
  893. enum isl_dim_type type, unsigned abs_pos,
  894. __isl_take isl_basic_set *context, int n_lower, int n_upper,
  895. isl_stat (*fn)(__isl_take isl_constraint *lower,
  896. __isl_take isl_constraint *upper,
  897. __isl_take isl_basic_set *bset, void *user), void *user)
  898. {
  899. isl_basic_set *context_i, *context_j;
  900. isl_constraint *lower = NULL;
  901. isl_constraint *upper = NULL;
  902. int i, j;
  903. for (i = 0; i < bset->n_ineq; ++i) {
  904. if (!isl_int_is_pos(bset->ineq[i][1 + abs_pos]))
  905. continue;
  906. context_i = set_largest_lower_bound(context, bset,
  907. abs_pos, n_lower, i);
  908. if (isl_basic_set_is_empty(context_i)) {
  909. isl_basic_set_free(context_i);
  910. continue;
  911. }
  912. for (j = 0; j < bset->n_ineq; ++j) {
  913. if (!isl_int_is_neg(bset->ineq[j][1 + abs_pos]))
  914. continue;
  915. context_j = set_smallest_upper_bound(context_i, bset,
  916. abs_pos, n_upper, j);
  917. context_j = isl_basic_set_extend_constraints(context_j,
  918. 0, 1);
  919. context_j = add_larger_bound_constraint(context_j,
  920. bset->ineq[i], bset->ineq[j], abs_pos, 0);
  921. context_j = isl_basic_set_simplify(context_j);
  922. context_j = isl_basic_set_finalize(context_j);
  923. if (isl_basic_set_is_empty(context_j)) {
  924. isl_basic_set_free(context_j);
  925. continue;
  926. }
  927. lower = isl_basic_set_constraint(isl_basic_set_copy(bset),
  928. &bset->ineq[i]);
  929. upper = isl_basic_set_constraint(isl_basic_set_copy(bset),
  930. &bset->ineq[j]);
  931. if (!lower || !upper || !context_j)
  932. goto error;
  933. if (fn(lower, upper, context_j, user) < 0)
  934. break;
  935. }
  936. isl_basic_set_free(context_i);
  937. if (j < bset->n_ineq)
  938. break;
  939. }
  940. isl_basic_set_free(context);
  941. if (i < bset->n_ineq)
  942. return isl_stat_error;
  943. return isl_stat_ok;
  944. error:
  945. isl_constraint_free(lower);
  946. isl_constraint_free(upper);
  947. isl_basic_set_free(context_i);
  948. isl_basic_set_free(context_j);
  949. isl_basic_set_free(context);
  950. return isl_stat_error;
  951. }
  952. /* For each pair of lower and upper bounds on the variable "pos"
  953. * of type "type", call "fn" with these lower and upper bounds and the
  954. * set of constraints on the remaining variables where these bounds
  955. * are active, i.e., (stricly) larger/smaller than the other lower/upper bounds.
  956. *
  957. * If the designated variable is equal to an affine combination of the
  958. * other variables then fn is called with both lower and upper
  959. * set to the corresponding equality.
  960. *
  961. * If there is no lower (or upper) bound, then NULL is passed
  962. * as the corresponding bound.
  963. *
  964. * We first check if the variable is involved in any equality.
  965. * If not, we count the number of lower and upper bounds and
  966. * act accordingly.
  967. */
  968. isl_stat isl_basic_set_foreach_bound_pair(__isl_keep isl_basic_set *bset,
  969. enum isl_dim_type type, unsigned pos,
  970. isl_stat (*fn)(__isl_take isl_constraint *lower,
  971. __isl_take isl_constraint *upper,
  972. __isl_take isl_basic_set *bset, void *user), void *user)
  973. {
  974. int i;
  975. isl_constraint *lower = NULL;
  976. isl_constraint *upper = NULL;
  977. isl_basic_set *context = NULL;
  978. unsigned abs_pos;
  979. int n_lower, n_upper;
  980. isl_size off;
  981. if (isl_basic_set_check_range(bset, type, pos, 1) < 0)
  982. return isl_stat_error;
  983. isl_assert(bset->ctx, type == isl_dim_param || type == isl_dim_set,
  984. return isl_stat_error);
  985. off = isl_basic_set_var_offset(bset, type);
  986. if (off < 0)
  987. return isl_stat_error;
  988. abs_pos = off + pos;
  989. for (i = 0; i < bset->n_eq; ++i) {
  990. if (isl_int_is_zero(bset->eq[i][1 + abs_pos]))
  991. continue;
  992. lower = isl_basic_set_constraint(isl_basic_set_copy(bset),
  993. &bset->eq[i]);
  994. upper = isl_constraint_copy(lower);
  995. context = isl_basic_set_remove_dims(isl_basic_set_copy(bset),
  996. type, pos, 1);
  997. if (!lower || !upper || !context)
  998. goto error;
  999. return fn(lower, upper, context, user);
  1000. }
  1001. n_lower = 0;
  1002. n_upper = 0;
  1003. for (i = 0; i < bset->n_ineq; ++i) {
  1004. if (isl_int_is_pos(bset->ineq[i][1 + abs_pos]))
  1005. n_lower++;
  1006. else if (isl_int_is_neg(bset->ineq[i][1 + abs_pos]))
  1007. n_upper++;
  1008. }
  1009. context = isl_basic_set_copy(bset);
  1010. context = isl_basic_set_cow(context);
  1011. if (!context)
  1012. goto error;
  1013. for (i = context->n_ineq - 1; i >= 0; --i)
  1014. if (!isl_int_is_zero(context->ineq[i][1 + abs_pos]))
  1015. isl_basic_set_drop_inequality(context, i);
  1016. context = isl_basic_set_drop(context, type, pos, 1);
  1017. if (!n_lower && !n_upper)
  1018. return fn(NULL, NULL, context, user);
  1019. if (!n_lower)
  1020. return foreach_upper_bound(bset, type, abs_pos, context, n_upper,
  1021. fn, user);
  1022. if (!n_upper)
  1023. return foreach_lower_bound(bset, type, abs_pos, context, n_lower,
  1024. fn, user);
  1025. return foreach_bound_pair(bset, type, abs_pos, context, n_lower, n_upper,
  1026. fn, user);
  1027. error:
  1028. isl_constraint_free(lower);
  1029. isl_constraint_free(upper);
  1030. isl_basic_set_free(context);
  1031. return isl_stat_error;
  1032. }
  1033. __isl_give isl_aff *isl_constraint_get_bound(
  1034. __isl_keep isl_constraint *constraint, enum isl_dim_type type, int pos)
  1035. {
  1036. isl_space *space;
  1037. isl_aff *aff;
  1038. isl_ctx *ctx;
  1039. if (isl_constraint_check_range(constraint, type, pos, 1) < 0)
  1040. return NULL;
  1041. space = isl_constraint_peek_space(constraint);
  1042. if (isl_space_check_is_set(space) < 0)
  1043. return NULL;
  1044. ctx = isl_constraint_get_ctx(constraint);
  1045. pos += offset(constraint, type);
  1046. if (isl_int_is_zero(constraint->v->el[pos]))
  1047. isl_die(ctx, isl_error_invalid,
  1048. "constraint does not define a bound on given dimension",
  1049. return NULL);
  1050. aff = isl_aff_alloc(isl_local_space_copy(constraint->ls));
  1051. if (!aff)
  1052. return NULL;
  1053. if (isl_int_is_neg(constraint->v->el[pos]))
  1054. isl_seq_cpy(aff->v->el + 1, constraint->v->el, aff->v->size - 1);
  1055. else
  1056. isl_seq_neg(aff->v->el + 1, constraint->v->el, aff->v->size - 1);
  1057. isl_int_set_si(aff->v->el[1 + pos], 0);
  1058. isl_int_abs(aff->v->el[0], constraint->v->el[pos]);
  1059. aff = isl_aff_normalize(aff);
  1060. return aff;
  1061. }
  1062. /* For an inequality constraint
  1063. *
  1064. * f >= 0
  1065. *
  1066. * or an equality constraint
  1067. *
  1068. * f = 0
  1069. *
  1070. * return the affine expression f.
  1071. */
  1072. __isl_give isl_aff *isl_constraint_get_aff(
  1073. __isl_keep isl_constraint *constraint)
  1074. {
  1075. isl_aff *aff;
  1076. if (!constraint)
  1077. return NULL;
  1078. aff = isl_aff_alloc(isl_local_space_copy(constraint->ls));
  1079. if (!aff)
  1080. return NULL;
  1081. isl_seq_cpy(aff->v->el + 1, constraint->v->el, aff->v->size - 1);
  1082. isl_int_set_si(aff->v->el[0], 1);
  1083. return aff;
  1084. }
  1085. /* Construct an inequality (eq = 0) or equality (eq = 1) constraint from "aff".
  1086. * In particular, construct aff >= 0 or aff = 0.
  1087. *
  1088. * The denominator of "aff" can be ignored.
  1089. */
  1090. static __isl_give isl_constraint *isl_constraint_alloc_aff(int eq,
  1091. __isl_take isl_aff *aff)
  1092. {
  1093. isl_local_space *ls;
  1094. isl_vec *v;
  1095. if (!aff)
  1096. return NULL;
  1097. ls = isl_aff_get_domain_local_space(aff);
  1098. v = isl_vec_drop_els(isl_vec_copy(aff->v), 0, 1);
  1099. isl_aff_free(aff);
  1100. return isl_constraint_alloc_vec(eq, ls, v);
  1101. }
  1102. /* Construct an equality constraint equating the given affine expression
  1103. * to zero.
  1104. */
  1105. __isl_give isl_constraint *isl_equality_from_aff(__isl_take isl_aff *aff)
  1106. {
  1107. return isl_constraint_alloc_aff(1, aff);
  1108. }
  1109. /* Construct an inequality constraint enforcing the given affine expression
  1110. * to be non-negative.
  1111. */
  1112. __isl_give isl_constraint *isl_inequality_from_aff(__isl_take isl_aff *aff)
  1113. {
  1114. return isl_constraint_alloc_aff(0, aff);
  1115. }
  1116. /* Compare two isl_constraints.
  1117. *
  1118. * Return -1 if "c1" is "smaller" than "c2", 1 if "c1" is "greater"
  1119. * than "c2" and 0 if they are equal.
  1120. *
  1121. * The order is fairly arbitrary. We do consider constraints that only involve
  1122. * earlier dimensions as "smaller".
  1123. */
  1124. int isl_constraint_plain_cmp(__isl_keep isl_constraint *c1,
  1125. __isl_keep isl_constraint *c2)
  1126. {
  1127. int cmp;
  1128. int last1, last2;
  1129. if (c1 == c2)
  1130. return 0;
  1131. if (!c1)
  1132. return -1;
  1133. if (!c2)
  1134. return 1;
  1135. cmp = isl_local_space_cmp(c1->ls, c2->ls);
  1136. if (cmp != 0)
  1137. return cmp;
  1138. last1 = isl_seq_last_non_zero(c1->v->el + 1, c1->v->size - 1);
  1139. last2 = isl_seq_last_non_zero(c2->v->el + 1, c1->v->size - 1);
  1140. if (last1 != last2)
  1141. return last1 - last2;
  1142. return isl_seq_cmp(c1->v->el, c2->v->el, c1->v->size);
  1143. }
  1144. /* Compare two constraints based on their final (non-zero) coefficients.
  1145. * In particular, the constraint that involves later variables or
  1146. * that has a larger coefficient for a shared latest variable
  1147. * is considered "greater" than the other constraint.
  1148. *
  1149. * Return -1 if "c1" is "smaller" than "c2", 1 if "c1" is "greater"
  1150. * than "c2" and 0 if they are equal.
  1151. *
  1152. * If the constraints live in different local spaces, then we cannot
  1153. * really compare the constraints so we compare the local spaces instead.
  1154. */
  1155. int isl_constraint_cmp_last_non_zero(__isl_keep isl_constraint *c1,
  1156. __isl_keep isl_constraint *c2)
  1157. {
  1158. int cmp;
  1159. int last1, last2;
  1160. if (c1 == c2)
  1161. return 0;
  1162. if (!c1)
  1163. return -1;
  1164. if (!c2)
  1165. return 1;
  1166. cmp = isl_local_space_cmp(c1->ls, c2->ls);
  1167. if (cmp != 0)
  1168. return cmp;
  1169. last1 = isl_seq_last_non_zero(c1->v->el + 1, c1->v->size - 1);
  1170. last2 = isl_seq_last_non_zero(c2->v->el + 1, c1->v->size - 1);
  1171. if (last1 != last2)
  1172. return last1 - last2;
  1173. if (last1 == -1)
  1174. return 0;
  1175. return isl_int_abs_cmp(c1->v->el[1 + last1], c2->v->el[1 + last2]);
  1176. }