isl_vertices.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624
  1. /*
  2. * Copyright 2010 INRIA Saclay
  3. *
  4. * Use of this software is governed by the MIT license
  5. *
  6. * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
  7. * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
  8. * 91893 Orsay, France
  9. */
  10. #include <isl_map_private.h>
  11. #include <isl_aff_private.h>
  12. #include <isl/set.h>
  13. #include <isl_seq.h>
  14. #include <isl_tab.h>
  15. #include <isl_space_private.h>
  16. #include <isl_morph.h>
  17. #include <isl_vertices_private.h>
  18. #include <isl_mat_private.h>
  19. #include <isl_vec_private.h>
  20. #define SELECTED 1
  21. #define DESELECTED -1
  22. #define UNSELECTED 0
  23. static __isl_give isl_vertices *compute_chambers(__isl_take isl_basic_set *bset,
  24. __isl_take isl_vertices *vertices);
  25. __isl_give isl_vertices *isl_vertices_copy(__isl_keep isl_vertices *vertices)
  26. {
  27. if (!vertices)
  28. return NULL;
  29. vertices->ref++;
  30. return vertices;
  31. }
  32. __isl_null isl_vertices *isl_vertices_free(__isl_take isl_vertices *vertices)
  33. {
  34. int i;
  35. if (!vertices)
  36. return NULL;
  37. if (--vertices->ref > 0)
  38. return NULL;
  39. for (i = 0; i < vertices->n_vertices; ++i) {
  40. isl_basic_set_free(vertices->v[i].vertex);
  41. isl_basic_set_free(vertices->v[i].dom);
  42. }
  43. free(vertices->v);
  44. for (i = 0; i < vertices->n_chambers; ++i) {
  45. free(vertices->c[i].vertices);
  46. isl_basic_set_free(vertices->c[i].dom);
  47. }
  48. free(vertices->c);
  49. isl_basic_set_free(vertices->bset);
  50. free(vertices);
  51. return NULL;
  52. }
  53. struct isl_vertex_list {
  54. struct isl_vertex v;
  55. struct isl_vertex_list *next;
  56. };
  57. static struct isl_vertex_list *free_vertex_list(struct isl_vertex_list *list)
  58. {
  59. struct isl_vertex_list *next;
  60. for (; list; list = next) {
  61. next = list->next;
  62. isl_basic_set_free(list->v.vertex);
  63. isl_basic_set_free(list->v.dom);
  64. free(list);
  65. }
  66. return NULL;
  67. }
  68. static __isl_give isl_vertices *vertices_from_list(__isl_keep isl_basic_set *bset,
  69. int n_vertices, struct isl_vertex_list *list)
  70. {
  71. int i;
  72. struct isl_vertex_list *next;
  73. isl_vertices *vertices;
  74. vertices = isl_calloc_type(bset->ctx, isl_vertices);
  75. if (!vertices)
  76. goto error;
  77. vertices->ref = 1;
  78. vertices->bset = isl_basic_set_copy(bset);
  79. vertices->v = isl_alloc_array(bset->ctx, struct isl_vertex, n_vertices);
  80. if (n_vertices && !vertices->v)
  81. goto error;
  82. vertices->n_vertices = n_vertices;
  83. for (i = 0; list; list = next, i++) {
  84. next = list->next;
  85. vertices->v[i] = list->v;
  86. free(list);
  87. }
  88. return vertices;
  89. error:
  90. isl_vertices_free(vertices);
  91. free_vertex_list(list);
  92. return NULL;
  93. }
  94. /* Prepend a vertex to the linked list "list" based on the equalities in "tab".
  95. * Return isl_bool_true if the vertex was actually added and
  96. * isl_bool_false otherwise.
  97. * In particular, vertices with a lower-dimensional activity domain are
  98. * not added to the list because they would not be included in any chamber.
  99. * Return isl_bool_error on error.
  100. */
  101. static isl_bool add_vertex(struct isl_vertex_list **list,
  102. __isl_keep isl_basic_set *bset, struct isl_tab *tab)
  103. {
  104. isl_size nvar;
  105. struct isl_vertex_list *v = NULL;
  106. if (isl_tab_detect_implicit_equalities(tab) < 0)
  107. return isl_bool_error;
  108. nvar = isl_basic_set_dim(bset, isl_dim_set);
  109. if (nvar < 0)
  110. return isl_bool_error;
  111. v = isl_calloc_type(tab->mat->ctx, struct isl_vertex_list);
  112. if (!v)
  113. goto error;
  114. v->v.vertex = isl_basic_set_copy(bset);
  115. v->v.vertex = isl_basic_set_cow(v->v.vertex);
  116. v->v.vertex = isl_basic_set_update_from_tab(v->v.vertex, tab);
  117. v->v.vertex = isl_basic_set_simplify(v->v.vertex);
  118. v->v.vertex = isl_basic_set_finalize(v->v.vertex);
  119. if (!v->v.vertex)
  120. goto error;
  121. isl_assert(bset->ctx, v->v.vertex->n_eq >= nvar, goto error);
  122. v->v.dom = isl_basic_set_copy(v->v.vertex);
  123. v->v.dom = isl_basic_set_params(v->v.dom);
  124. if (!v->v.dom)
  125. goto error;
  126. if (v->v.dom->n_eq > 0) {
  127. free_vertex_list(v);
  128. return isl_bool_false;
  129. }
  130. v->next = *list;
  131. *list = v;
  132. return isl_bool_true;
  133. error:
  134. free_vertex_list(v);
  135. return isl_bool_error;
  136. }
  137. /* Compute the parametric vertices and the chamber decomposition
  138. * of an empty parametric polytope.
  139. */
  140. static __isl_give isl_vertices *vertices_empty(__isl_keep isl_basic_set *bset)
  141. {
  142. isl_vertices *vertices;
  143. if (!bset)
  144. return NULL;
  145. vertices = isl_calloc_type(bset->ctx, isl_vertices);
  146. if (!vertices)
  147. return NULL;
  148. vertices->bset = isl_basic_set_copy(bset);
  149. vertices->ref = 1;
  150. vertices->n_vertices = 0;
  151. vertices->n_chambers = 0;
  152. return vertices;
  153. }
  154. /* Compute the parametric vertices and the chamber decomposition
  155. * of the parametric polytope defined using the same constraints
  156. * as "bset" in the 0D case.
  157. * There is exactly one 0D vertex and a single chamber containing
  158. * the vertex.
  159. */
  160. static __isl_give isl_vertices *vertices_0D(__isl_keep isl_basic_set *bset)
  161. {
  162. isl_vertices *vertices;
  163. if (!bset)
  164. return NULL;
  165. vertices = isl_calloc_type(bset->ctx, isl_vertices);
  166. if (!vertices)
  167. return NULL;
  168. vertices->ref = 1;
  169. vertices->bset = isl_basic_set_copy(bset);
  170. vertices->v = isl_calloc_array(bset->ctx, struct isl_vertex, 1);
  171. if (!vertices->v)
  172. goto error;
  173. vertices->n_vertices = 1;
  174. vertices->v[0].vertex = isl_basic_set_copy(bset);
  175. vertices->v[0].dom = isl_basic_set_params(isl_basic_set_copy(bset));
  176. if (!vertices->v[0].vertex || !vertices->v[0].dom)
  177. goto error;
  178. vertices->c = isl_calloc_array(bset->ctx, struct isl_chamber, 1);
  179. if (!vertices->c)
  180. goto error;
  181. vertices->n_chambers = 1;
  182. vertices->c[0].n_vertices = 1;
  183. vertices->c[0].vertices = isl_calloc_array(bset->ctx, int, 1);
  184. if (!vertices->c[0].vertices)
  185. goto error;
  186. vertices->c[0].dom = isl_basic_set_copy(vertices->v[0].dom);
  187. if (!vertices->c[0].dom)
  188. goto error;
  189. return vertices;
  190. error:
  191. isl_vertices_free(vertices);
  192. return NULL;
  193. }
  194. /* Is the row pointed to by "f" linearly independent of the "n" first
  195. * rows in "facets"?
  196. */
  197. static isl_bool is_independent(__isl_keep isl_mat *facets, int n, isl_int *f)
  198. {
  199. isl_size rank;
  200. if (isl_seq_first_non_zero(f, facets->n_col) < 0)
  201. return isl_bool_false;
  202. isl_seq_cpy(facets->row[n], f, facets->n_col);
  203. facets->n_row = n + 1;
  204. rank = isl_mat_rank(facets);
  205. if (rank < 0)
  206. return isl_bool_error;
  207. return isl_bool_ok(rank == n + 1);
  208. }
  209. /* Check whether we can select constraint "level", given the current selection
  210. * reflected by facets in "tab", the rows of "facets" and the earlier
  211. * "selected" elements of "selection".
  212. *
  213. * If the constraint is (strictly) redundant in the tableau, selecting it would
  214. * result in an empty tableau, so it can't be selected.
  215. * If the set variable part of the constraint is not linearly independent
  216. * of the set variable parts of the already selected constraints,
  217. * the constraint cannot be selected.
  218. * If selecting the constraint results in an empty tableau, the constraint
  219. * cannot be selected.
  220. * Finally, if selecting the constraint results in some explicitly
  221. * deselected constraints turning into equalities, then the corresponding
  222. * vertices have already been generated, so the constraint cannot be selected.
  223. */
  224. static isl_bool can_select(__isl_keep isl_basic_set *bset, int level,
  225. struct isl_tab *tab, __isl_keep isl_mat *facets, int selected,
  226. int *selection)
  227. {
  228. int i;
  229. isl_bool indep;
  230. unsigned ovar;
  231. struct isl_tab_undo *snap;
  232. if (isl_tab_is_redundant(tab, level))
  233. return isl_bool_false;
  234. ovar = isl_space_offset(bset->dim, isl_dim_set);
  235. indep = is_independent(facets, selected, bset->ineq[level] + 1 + ovar);
  236. if (indep < 0 || !indep)
  237. return indep;
  238. snap = isl_tab_snap(tab);
  239. if (isl_tab_select_facet(tab, level) < 0)
  240. return isl_bool_error;
  241. if (tab->empty) {
  242. if (isl_tab_rollback(tab, snap) < 0)
  243. return isl_bool_error;
  244. return isl_bool_false;
  245. }
  246. for (i = 0; i < level; ++i) {
  247. int sgn;
  248. if (selection[i] != DESELECTED)
  249. continue;
  250. if (isl_tab_is_equality(tab, i))
  251. sgn = 0;
  252. else if (isl_tab_is_redundant(tab, i))
  253. sgn = 1;
  254. else
  255. sgn = isl_tab_sign_of_max(tab, i);
  256. if (sgn < -1)
  257. return isl_bool_error;
  258. if (sgn <= 0) {
  259. if (isl_tab_rollback(tab, snap) < 0)
  260. return isl_bool_error;
  261. return isl_bool_false;
  262. }
  263. }
  264. return isl_bool_true;
  265. }
  266. /* Compute the parametric vertices and the chamber decomposition
  267. * of a parametric polytope that is not full-dimensional.
  268. *
  269. * Simply map the parametric polytope to a lower dimensional space
  270. * and map the resulting vertices back.
  271. */
  272. static __isl_give isl_vertices *lower_dim_vertices(
  273. __isl_take isl_basic_set *bset)
  274. {
  275. isl_morph *morph;
  276. isl_vertices *vertices;
  277. morph = isl_basic_set_full_compression(bset);
  278. bset = isl_morph_basic_set(isl_morph_copy(morph), bset);
  279. vertices = isl_basic_set_compute_vertices(bset);
  280. isl_basic_set_free(bset);
  281. morph = isl_morph_inverse(morph);
  282. vertices = isl_morph_vertices(morph, vertices);
  283. return vertices;
  284. }
  285. /* Compute the parametric vertices and the chamber decomposition
  286. * of a parametric polytope "bset" that is not full-dimensional.
  287. * Additionally, free both "copy" and "tab".
  288. */
  289. static __isl_give isl_vertices *lower_dim_vertices_free(
  290. __isl_take isl_basic_set *bset, __isl_take isl_basic_set *copy,
  291. struct isl_tab *tab)
  292. {
  293. isl_basic_set_free(copy);
  294. isl_tab_free(tab);
  295. return lower_dim_vertices(bset);
  296. }
  297. /* Detect implicit equality constraints in "bset" using the tableau
  298. * representation "tab".
  299. * Return a copy of "bset" with the implicit equality constraints
  300. * made explicit, leaving the original "bset" unmodified.
  301. */
  302. static __isl_give isl_basic_set *detect_implicit_equality_constraints(
  303. __isl_keep isl_basic_set *bset, struct isl_tab *tab)
  304. {
  305. if (isl_tab_detect_implicit_equalities(tab) < 0)
  306. return NULL;
  307. bset = isl_basic_set_copy(bset);
  308. bset = isl_basic_set_cow(bset);
  309. bset = isl_basic_set_update_from_tab(bset, tab);
  310. return bset;
  311. }
  312. /* Compute the parametric vertices and the chamber decomposition
  313. * of the parametric polytope defined using the same constraints
  314. * as "bset". "bset" is assumed to have no existentially quantified
  315. * variables.
  316. *
  317. * The vertices themselves are computed in a fairly simplistic way.
  318. * We simply run through all combinations of d constraints,
  319. * with d the number of set variables, and check if those d constraints
  320. * define a vertex. To avoid the generation of duplicate vertices,
  321. * which may happen if a vertex is defined by more than d constraints,
  322. * we make sure we only generate the vertex for the d constraints with
  323. * smallest index.
  324. *
  325. * Only potential vertices with a full-dimensional activity domain
  326. * are considered. However, if the input has (implicit) equality
  327. * constraints among the parameters, then activity domain
  328. * should be considered full-dimensional if it does not satisfy
  329. * any extra equality constraints beyond those of the input.
  330. * The implicit equality constraints of the input are therefore first detected.
  331. * If there are any, then the input is mapped to a lower dimensional space
  332. * such that the check for full-dimensional activity domains
  333. * can be performed with respect to a full-dimensional space.
  334. * Note that it is important to leave "bset" unmodified while detecting
  335. * equality constraints since the inequality constraints of "bset"
  336. * are assumed to correspond to those of the tableau.
  337. *
  338. * We set up a tableau and keep track of which facets have been
  339. * selected. The tableau is marked strict_redundant so that we can be
  340. * sure that any constraint that is marked redundant (and that is not
  341. * also marked zero) is not an equality.
  342. * If a constraint is marked DESELECTED, it means the constraint was
  343. * SELECTED before (in combination with the same selection of earlier
  344. * constraints). If such a deselected constraint turns out to be an
  345. * equality, then any vertex that may still be found with the current
  346. * selection has already been generated when the constraint was selected.
  347. * A constraint is marked UNSELECTED when there is no way selecting
  348. * the constraint could lead to a vertex (in combination with the current
  349. * selection of earlier constraints).
  350. *
  351. * The set variable coefficients of the selected constraints are stored
  352. * in the facets matrix.
  353. */
  354. __isl_give isl_vertices *isl_basic_set_compute_vertices(
  355. __isl_keep isl_basic_set *bset)
  356. {
  357. struct isl_tab *tab;
  358. int level;
  359. int init;
  360. isl_size n_eq;
  361. isl_size nvar;
  362. int *selection = NULL;
  363. int selected;
  364. struct isl_tab_undo **snap = NULL;
  365. isl_mat *facets = NULL;
  366. struct isl_vertex_list *list = NULL;
  367. int n_vertices = 0;
  368. isl_vertices *vertices;
  369. isl_basic_set *copy;
  370. isl_basic_set *test;
  371. if (!bset)
  372. return NULL;
  373. if (isl_basic_set_plain_is_empty(bset))
  374. return vertices_empty(bset);
  375. if (bset->n_eq != 0)
  376. return lower_dim_vertices(isl_basic_set_copy(bset));
  377. if (isl_basic_set_check_no_locals(bset) < 0)
  378. return NULL;
  379. nvar = isl_basic_set_dim(bset, isl_dim_set);
  380. if (nvar < 0)
  381. return NULL;
  382. if (nvar == 0)
  383. return vertices_0D(bset);
  384. copy = isl_basic_set_copy(bset);
  385. copy = isl_basic_set_set_rational(copy);
  386. if (!copy)
  387. return NULL;
  388. tab = isl_tab_from_basic_set(copy, 0);
  389. if (!tab)
  390. goto error;
  391. tab->strict_redundant = 1;
  392. if (tab->empty) {
  393. vertices = vertices_empty(copy);
  394. isl_basic_set_free(copy);
  395. isl_tab_free(tab);
  396. return vertices;
  397. }
  398. test = detect_implicit_equality_constraints(bset, tab);
  399. n_eq = isl_basic_set_n_equality(test);
  400. if (n_eq < 0)
  401. test = isl_basic_set_free(test);
  402. if (n_eq < 0 || n_eq > 0)
  403. return lower_dim_vertices_free(test, copy, tab);
  404. isl_basic_set_free(test);
  405. selection = isl_alloc_array(copy->ctx, int, copy->n_ineq);
  406. snap = isl_alloc_array(copy->ctx, struct isl_tab_undo *, copy->n_ineq);
  407. facets = isl_mat_alloc(copy->ctx, nvar, nvar);
  408. if ((copy->n_ineq && (!selection || !snap)) || !facets)
  409. goto error;
  410. level = 0;
  411. init = 1;
  412. selected = 0;
  413. while (level >= 0) {
  414. if (level >= copy->n_ineq ||
  415. (!init && selection[level] != SELECTED)) {
  416. --level;
  417. init = 0;
  418. continue;
  419. }
  420. if (init) {
  421. isl_bool ok;
  422. snap[level] = isl_tab_snap(tab);
  423. ok = can_select(copy, level, tab, facets, selected,
  424. selection);
  425. if (ok < 0)
  426. goto error;
  427. if (ok) {
  428. selection[level] = SELECTED;
  429. selected++;
  430. } else
  431. selection[level] = UNSELECTED;
  432. } else {
  433. selection[level] = DESELECTED;
  434. selected--;
  435. if (isl_tab_rollback(tab, snap[level]) < 0)
  436. goto error;
  437. }
  438. if (selected == nvar) {
  439. if (tab->n_dead == nvar) {
  440. isl_bool added = add_vertex(&list, copy, tab);
  441. if (added < 0)
  442. goto error;
  443. if (added)
  444. n_vertices++;
  445. }
  446. init = 0;
  447. continue;
  448. }
  449. ++level;
  450. init = 1;
  451. }
  452. isl_mat_free(facets);
  453. free(selection);
  454. free(snap);
  455. isl_tab_free(tab);
  456. vertices = vertices_from_list(copy, n_vertices, list);
  457. vertices = compute_chambers(copy, vertices);
  458. return vertices;
  459. error:
  460. free_vertex_list(list);
  461. isl_mat_free(facets);
  462. free(selection);
  463. free(snap);
  464. isl_tab_free(tab);
  465. isl_basic_set_free(copy);
  466. return NULL;
  467. }
  468. struct isl_chamber_list {
  469. struct isl_chamber c;
  470. struct isl_chamber_list *next;
  471. };
  472. static void free_chamber_list(struct isl_chamber_list *list)
  473. {
  474. struct isl_chamber_list *next;
  475. for (; list; list = next) {
  476. next = list->next;
  477. isl_basic_set_free(list->c.dom);
  478. free(list->c.vertices);
  479. free(list);
  480. }
  481. }
  482. /* Check whether the basic set "bset" is a superset of the basic set described
  483. * by "tab", i.e., check whether all constraints of "bset" are redundant.
  484. */
  485. static isl_bool bset_covers_tab(__isl_keep isl_basic_set *bset,
  486. struct isl_tab *tab)
  487. {
  488. int i;
  489. if (!bset || !tab)
  490. return isl_bool_error;
  491. for (i = 0; i < bset->n_ineq; ++i) {
  492. enum isl_ineq_type type = isl_tab_ineq_type(tab, bset->ineq[i]);
  493. switch (type) {
  494. case isl_ineq_error: return isl_bool_error;
  495. case isl_ineq_redundant: continue;
  496. default: return isl_bool_false;
  497. }
  498. }
  499. return isl_bool_true;
  500. }
  501. static __isl_give isl_vertices *vertices_add_chambers(
  502. __isl_take isl_vertices *vertices, int n_chambers,
  503. struct isl_chamber_list *list)
  504. {
  505. int i;
  506. isl_ctx *ctx;
  507. struct isl_chamber_list *next;
  508. ctx = isl_vertices_get_ctx(vertices);
  509. vertices->c = isl_alloc_array(ctx, struct isl_chamber, n_chambers);
  510. if (!vertices->c)
  511. goto error;
  512. vertices->n_chambers = n_chambers;
  513. for (i = 0; list; list = next, i++) {
  514. next = list->next;
  515. vertices->c[i] = list->c;
  516. free(list);
  517. }
  518. return vertices;
  519. error:
  520. isl_vertices_free(vertices);
  521. free_chamber_list(list);
  522. return NULL;
  523. }
  524. /* Can "tab" be intersected with "bset" without resulting in
  525. * a lower-dimensional set.
  526. * "bset" itself is assumed to be full-dimensional.
  527. */
  528. static isl_bool can_intersect(struct isl_tab *tab,
  529. __isl_keep isl_basic_set *bset)
  530. {
  531. int i;
  532. struct isl_tab_undo *snap;
  533. if (bset->n_eq > 0)
  534. isl_die(isl_basic_set_get_ctx(bset), isl_error_internal,
  535. "expecting full-dimensional input",
  536. return isl_bool_error);
  537. if (isl_tab_extend_cons(tab, bset->n_ineq) < 0)
  538. return isl_bool_error;
  539. snap = isl_tab_snap(tab);
  540. for (i = 0; i < bset->n_ineq; ++i) {
  541. enum isl_ineq_type type;
  542. type = isl_tab_ineq_type(tab, bset->ineq[i]);
  543. if (type < 0)
  544. return isl_bool_error;
  545. if (type == isl_ineq_redundant)
  546. continue;
  547. if (isl_tab_add_ineq(tab, bset->ineq[i]) < 0)
  548. return isl_bool_error;
  549. }
  550. if (isl_tab_detect_implicit_equalities(tab) < 0)
  551. return isl_bool_error;
  552. if (tab->n_dead) {
  553. if (isl_tab_rollback(tab, snap) < 0)
  554. return isl_bool_error;
  555. return isl_bool_false;
  556. }
  557. return isl_bool_true;
  558. }
  559. static int add_chamber(struct isl_chamber_list **list,
  560. __isl_keep isl_vertices *vertices, struct isl_tab *tab, int *selection)
  561. {
  562. int n_frozen;
  563. int i, j;
  564. int n_vertices = 0;
  565. struct isl_tab_undo *snap;
  566. struct isl_chamber_list *c = NULL;
  567. for (i = 0; i < vertices->n_vertices; ++i)
  568. if (selection[i])
  569. n_vertices++;
  570. snap = isl_tab_snap(tab);
  571. for (i = 0; i < tab->n_con && tab->con[i].frozen; ++i)
  572. tab->con[i].frozen = 0;
  573. n_frozen = i;
  574. if (isl_tab_detect_redundant(tab) < 0)
  575. return -1;
  576. c = isl_calloc_type(tab->mat->ctx, struct isl_chamber_list);
  577. if (!c)
  578. goto error;
  579. c->c.vertices = isl_alloc_array(tab->mat->ctx, int, n_vertices);
  580. if (n_vertices && !c->c.vertices)
  581. goto error;
  582. c->c.dom = isl_basic_set_copy(isl_tab_peek_bset(tab));
  583. c->c.dom = isl_basic_set_set_rational(c->c.dom);
  584. c->c.dom = isl_basic_set_cow(c->c.dom);
  585. c->c.dom = isl_basic_set_update_from_tab(c->c.dom, tab);
  586. c->c.dom = isl_basic_set_simplify(c->c.dom);
  587. c->c.dom = isl_basic_set_finalize(c->c.dom);
  588. if (!c->c.dom)
  589. goto error;
  590. c->c.n_vertices = n_vertices;
  591. for (i = 0, j = 0; i < vertices->n_vertices; ++i)
  592. if (selection[i]) {
  593. c->c.vertices[j] = i;
  594. j++;
  595. }
  596. c->next = *list;
  597. *list = c;
  598. for (i = 0; i < n_frozen; ++i)
  599. tab->con[i].frozen = 1;
  600. if (isl_tab_rollback(tab, snap) < 0)
  601. return -1;
  602. return 0;
  603. error:
  604. free_chamber_list(c);
  605. return -1;
  606. }
  607. struct isl_facet_todo {
  608. struct isl_tab *tab; /* A tableau representation of the facet */
  609. isl_basic_set *bset; /* A normalized basic set representation */
  610. isl_vec *constraint; /* Constraint pointing to the other side */
  611. struct isl_facet_todo *next;
  612. };
  613. static void free_todo(struct isl_facet_todo *todo)
  614. {
  615. while (todo) {
  616. struct isl_facet_todo *next = todo->next;
  617. isl_tab_free(todo->tab);
  618. isl_basic_set_free(todo->bset);
  619. isl_vec_free(todo->constraint);
  620. free(todo);
  621. todo = next;
  622. }
  623. }
  624. static struct isl_facet_todo *create_todo(struct isl_tab *tab, int con)
  625. {
  626. int i;
  627. int n_frozen;
  628. struct isl_tab_undo *snap;
  629. struct isl_facet_todo *todo;
  630. snap = isl_tab_snap(tab);
  631. for (i = 0; i < tab->n_con && tab->con[i].frozen; ++i)
  632. tab->con[i].frozen = 0;
  633. n_frozen = i;
  634. if (isl_tab_detect_redundant(tab) < 0)
  635. return NULL;
  636. todo = isl_calloc_type(tab->mat->ctx, struct isl_facet_todo);
  637. if (!todo)
  638. return NULL;
  639. todo->constraint = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
  640. if (!todo->constraint)
  641. goto error;
  642. isl_seq_neg(todo->constraint->el, tab->bmap->ineq[con], 1 + tab->n_var);
  643. todo->bset = isl_basic_set_copy(isl_tab_peek_bset(tab));
  644. todo->bset = isl_basic_set_set_rational(todo->bset);
  645. todo->bset = isl_basic_set_cow(todo->bset);
  646. todo->bset = isl_basic_set_update_from_tab(todo->bset, tab);
  647. todo->bset = isl_basic_set_simplify(todo->bset);
  648. todo->bset = isl_basic_set_sort_constraints(todo->bset);
  649. if (!todo->bset)
  650. goto error;
  651. ISL_F_SET(todo->bset, ISL_BASIC_SET_NO_REDUNDANT);
  652. todo->tab = isl_tab_dup(tab);
  653. if (!todo->tab)
  654. goto error;
  655. for (i = 0; i < n_frozen; ++i)
  656. tab->con[i].frozen = 1;
  657. if (isl_tab_rollback(tab, snap) < 0)
  658. goto error;
  659. return todo;
  660. error:
  661. free_todo(todo);
  662. return NULL;
  663. }
  664. /* Create todo items for all interior facets of the chamber represented
  665. * by "tab" and collect them in "next".
  666. */
  667. static int init_todo(struct isl_facet_todo **next, struct isl_tab *tab)
  668. {
  669. int i;
  670. struct isl_tab_undo *snap;
  671. struct isl_facet_todo *todo;
  672. snap = isl_tab_snap(tab);
  673. for (i = 0; i < tab->n_con; ++i) {
  674. if (tab->con[i].frozen)
  675. continue;
  676. if (tab->con[i].is_redundant)
  677. continue;
  678. if (isl_tab_select_facet(tab, i) < 0)
  679. return -1;
  680. todo = create_todo(tab, i);
  681. if (!todo)
  682. return -1;
  683. todo->next = *next;
  684. *next = todo;
  685. if (isl_tab_rollback(tab, snap) < 0)
  686. return -1;
  687. }
  688. return 0;
  689. }
  690. /* Does the linked list contain a todo item that is the opposite of "todo".
  691. * If so, return 1 and remove the opposite todo item.
  692. */
  693. static int has_opposite(struct isl_facet_todo *todo,
  694. struct isl_facet_todo **list)
  695. {
  696. for (; *list; list = &(*list)->next) {
  697. int eq;
  698. eq = isl_basic_set_plain_is_equal(todo->bset, (*list)->bset);
  699. if (eq < 0)
  700. return -1;
  701. if (!eq)
  702. continue;
  703. todo = *list;
  704. *list = todo->next;
  705. todo->next = NULL;
  706. free_todo(todo);
  707. return 1;
  708. }
  709. return 0;
  710. }
  711. /* Create todo items for all interior facets of the chamber represented
  712. * by "tab" and collect them in first->next, taking care to cancel
  713. * opposite todo items.
  714. */
  715. static int update_todo(struct isl_facet_todo *first, struct isl_tab *tab)
  716. {
  717. int i;
  718. struct isl_tab_undo *snap;
  719. struct isl_facet_todo *todo;
  720. snap = isl_tab_snap(tab);
  721. for (i = 0; i < tab->n_con; ++i) {
  722. int drop;
  723. if (tab->con[i].frozen)
  724. continue;
  725. if (tab->con[i].is_redundant)
  726. continue;
  727. if (isl_tab_select_facet(tab, i) < 0)
  728. return -1;
  729. todo = create_todo(tab, i);
  730. if (!todo)
  731. return -1;
  732. drop = has_opposite(todo, &first->next);
  733. if (drop < 0)
  734. return -1;
  735. if (drop)
  736. free_todo(todo);
  737. else {
  738. todo->next = first->next;
  739. first->next = todo;
  740. }
  741. if (isl_tab_rollback(tab, snap) < 0)
  742. return -1;
  743. }
  744. return 0;
  745. }
  746. /* Compute the chamber decomposition of the parametric polytope respresented
  747. * by "bset" given the parametric vertices and their activity domains.
  748. *
  749. * We are only interested in full-dimensional chambers.
  750. * Each of these chambers is the intersection of the activity domains of
  751. * one or more vertices and the union of all chambers is equal to the
  752. * projection of the entire parametric polytope onto the parameter space.
  753. *
  754. * We first create an initial chamber by intersecting as many activity
  755. * domains as possible without ending up with an empty or lower-dimensional
  756. * set. As a minor optimization, we only consider those activity domains
  757. * that contain some arbitrary point.
  758. *
  759. * For each of the interior facets of the chamber, we construct a todo item,
  760. * containing the facet and a constraint containing the other side of the facet,
  761. * for constructing the chamber on the other side.
  762. * While their are any todo items left, we pick a todo item and
  763. * create the required chamber by intersecting all activity domains
  764. * that contain the facet and have a full-dimensional intersection with
  765. * the other side of the facet. For each of the interior facets, we
  766. * again create todo items, taking care to cancel opposite todo items.
  767. */
  768. static __isl_give isl_vertices *compute_chambers(__isl_take isl_basic_set *bset,
  769. __isl_take isl_vertices *vertices)
  770. {
  771. int i;
  772. isl_ctx *ctx;
  773. isl_size n_eq;
  774. isl_vec *sample = NULL;
  775. struct isl_tab *tab = NULL;
  776. struct isl_tab_undo *snap;
  777. int *selection = NULL;
  778. int n_chambers = 0;
  779. struct isl_chamber_list *list = NULL;
  780. struct isl_facet_todo *todo = NULL;
  781. if (!bset || !vertices)
  782. goto error;
  783. ctx = isl_vertices_get_ctx(vertices);
  784. selection = isl_alloc_array(ctx, int, vertices->n_vertices);
  785. if (vertices->n_vertices && !selection)
  786. goto error;
  787. bset = isl_basic_set_params(bset);
  788. n_eq = isl_basic_set_n_equality(bset);
  789. if (n_eq < 0)
  790. goto error;
  791. if (n_eq > 0)
  792. isl_die(isl_basic_set_get_ctx(bset), isl_error_internal,
  793. "expecting full-dimensional input", goto error);
  794. tab = isl_tab_from_basic_set(bset, 1);
  795. if (!tab)
  796. goto error;
  797. for (i = 0; i < bset->n_ineq; ++i)
  798. if (isl_tab_freeze_constraint(tab, i) < 0)
  799. goto error;
  800. isl_basic_set_free(bset);
  801. snap = isl_tab_snap(tab);
  802. sample = isl_tab_get_sample_value(tab);
  803. for (i = 0; i < vertices->n_vertices; ++i) {
  804. selection[i] = isl_basic_set_contains(vertices->v[i].dom, sample);
  805. if (selection[i] < 0)
  806. goto error;
  807. if (!selection[i])
  808. continue;
  809. selection[i] = can_intersect(tab, vertices->v[i].dom);
  810. if (selection[i] < 0)
  811. goto error;
  812. }
  813. if (isl_tab_detect_redundant(tab) < 0)
  814. goto error;
  815. if (add_chamber(&list, vertices, tab, selection) < 0)
  816. goto error;
  817. n_chambers++;
  818. if (init_todo(&todo, tab) < 0)
  819. goto error;
  820. while (todo) {
  821. struct isl_facet_todo *next;
  822. if (isl_tab_rollback(tab, snap) < 0)
  823. goto error;
  824. if (isl_tab_add_ineq(tab, todo->constraint->el) < 0)
  825. goto error;
  826. if (isl_tab_freeze_constraint(tab, tab->n_con - 1) < 0)
  827. goto error;
  828. for (i = 0; i < vertices->n_vertices; ++i) {
  829. selection[i] = bset_covers_tab(vertices->v[i].dom,
  830. todo->tab);
  831. if (selection[i] < 0)
  832. goto error;
  833. if (!selection[i])
  834. continue;
  835. selection[i] = can_intersect(tab, vertices->v[i].dom);
  836. if (selection[i] < 0)
  837. goto error;
  838. }
  839. if (isl_tab_detect_redundant(tab) < 0)
  840. goto error;
  841. if (add_chamber(&list, vertices, tab, selection) < 0)
  842. goto error;
  843. n_chambers++;
  844. if (update_todo(todo, tab) < 0)
  845. goto error;
  846. next = todo->next;
  847. todo->next = NULL;
  848. free_todo(todo);
  849. todo = next;
  850. }
  851. isl_vec_free(sample);
  852. isl_tab_free(tab);
  853. free(selection);
  854. vertices = vertices_add_chambers(vertices, n_chambers, list);
  855. for (i = 0; vertices && i < vertices->n_vertices; ++i) {
  856. isl_basic_set_free(vertices->v[i].dom);
  857. vertices->v[i].dom = NULL;
  858. }
  859. return vertices;
  860. error:
  861. free_chamber_list(list);
  862. free_todo(todo);
  863. isl_vec_free(sample);
  864. isl_tab_free(tab);
  865. free(selection);
  866. if (!tab)
  867. isl_basic_set_free(bset);
  868. isl_vertices_free(vertices);
  869. return NULL;
  870. }
  871. isl_ctx *isl_vertex_get_ctx(__isl_keep isl_vertex *vertex)
  872. {
  873. return vertex ? isl_vertices_get_ctx(vertex->vertices) : NULL;
  874. }
  875. isl_size isl_vertex_get_id(__isl_keep isl_vertex *vertex)
  876. {
  877. return vertex ? vertex->id : isl_size_error;
  878. }
  879. /* Return the activity domain of the vertex "vertex".
  880. */
  881. __isl_give isl_basic_set *isl_vertex_get_domain(__isl_keep isl_vertex *vertex)
  882. {
  883. struct isl_vertex *v;
  884. if (!vertex)
  885. return NULL;
  886. v = &vertex->vertices->v[vertex->id];
  887. if (!v->dom) {
  888. v->dom = isl_basic_set_copy(v->vertex);
  889. v->dom = isl_basic_set_params(v->dom);
  890. v->dom = isl_basic_set_set_integral(v->dom);
  891. }
  892. return isl_basic_set_copy(v->dom);
  893. }
  894. /* Return a multiple quasi-affine expression describing the vertex "vertex"
  895. * in terms of the parameters,
  896. */
  897. __isl_give isl_multi_aff *isl_vertex_get_expr(__isl_keep isl_vertex *vertex)
  898. {
  899. struct isl_vertex *v;
  900. isl_basic_set *bset;
  901. if (!vertex)
  902. return NULL;
  903. v = &vertex->vertices->v[vertex->id];
  904. bset = isl_basic_set_copy(v->vertex);
  905. return isl_multi_aff_from_basic_set_equalities(bset);
  906. }
  907. static __isl_give isl_vertex *isl_vertex_alloc(__isl_take isl_vertices *vertices,
  908. int id)
  909. {
  910. isl_ctx *ctx;
  911. isl_vertex *vertex;
  912. if (!vertices)
  913. return NULL;
  914. ctx = isl_vertices_get_ctx(vertices);
  915. vertex = isl_alloc_type(ctx, isl_vertex);
  916. if (!vertex)
  917. goto error;
  918. vertex->vertices = vertices;
  919. vertex->id = id;
  920. return vertex;
  921. error:
  922. isl_vertices_free(vertices);
  923. return NULL;
  924. }
  925. __isl_null isl_vertex *isl_vertex_free(__isl_take isl_vertex *vertex)
  926. {
  927. if (!vertex)
  928. return NULL;
  929. isl_vertices_free(vertex->vertices);
  930. free(vertex);
  931. return NULL;
  932. }
  933. isl_ctx *isl_cell_get_ctx(__isl_keep isl_cell *cell)
  934. {
  935. return cell ? cell->dom->ctx : NULL;
  936. }
  937. __isl_give isl_basic_set *isl_cell_get_domain(__isl_keep isl_cell *cell)
  938. {
  939. return cell ? isl_basic_set_copy(cell->dom) : NULL;
  940. }
  941. static __isl_give isl_cell *isl_cell_alloc(__isl_take isl_vertices *vertices,
  942. __isl_take isl_basic_set *dom, int id)
  943. {
  944. int i;
  945. isl_cell *cell = NULL;
  946. if (!vertices || !dom)
  947. goto error;
  948. cell = isl_calloc_type(dom->ctx, isl_cell);
  949. if (!cell)
  950. goto error;
  951. cell->n_vertices = vertices->c[id].n_vertices;
  952. cell->ids = isl_alloc_array(dom->ctx, int, cell->n_vertices);
  953. if (cell->n_vertices && !cell->ids)
  954. goto error;
  955. for (i = 0; i < cell->n_vertices; ++i)
  956. cell->ids[i] = vertices->c[id].vertices[i];
  957. cell->vertices = vertices;
  958. cell->dom = dom;
  959. return cell;
  960. error:
  961. isl_cell_free(cell);
  962. isl_vertices_free(vertices);
  963. isl_basic_set_free(dom);
  964. return NULL;
  965. }
  966. __isl_null isl_cell *isl_cell_free(__isl_take isl_cell *cell)
  967. {
  968. if (!cell)
  969. return NULL;
  970. isl_vertices_free(cell->vertices);
  971. free(cell->ids);
  972. isl_basic_set_free(cell->dom);
  973. free(cell);
  974. return NULL;
  975. }
  976. /* Create a tableau of the cone obtained by first homogenizing the given
  977. * polytope and then making all inequalities strict by setting the
  978. * constant term to -1.
  979. */
  980. static struct isl_tab *tab_for_shifted_cone(__isl_keep isl_basic_set *bset)
  981. {
  982. int i;
  983. isl_vec *c = NULL;
  984. struct isl_tab *tab;
  985. isl_size total;
  986. total = isl_basic_set_dim(bset, isl_dim_all);
  987. if (total < 0)
  988. return NULL;
  989. tab = isl_tab_alloc(bset->ctx, bset->n_eq + bset->n_ineq + 1,
  990. 1 + total, 0);
  991. if (!tab)
  992. return NULL;
  993. tab->rational = ISL_F_ISSET(bset, ISL_BASIC_SET_RATIONAL);
  994. if (ISL_F_ISSET(bset, ISL_BASIC_MAP_EMPTY)) {
  995. if (isl_tab_mark_empty(tab) < 0)
  996. goto error;
  997. return tab;
  998. }
  999. c = isl_vec_alloc(bset->ctx, 1 + 1 + total);
  1000. if (!c)
  1001. goto error;
  1002. isl_int_set_si(c->el[0], 0);
  1003. for (i = 0; i < bset->n_eq; ++i) {
  1004. isl_seq_cpy(c->el + 1, bset->eq[i], c->size - 1);
  1005. if (isl_tab_add_eq(tab, c->el) < 0)
  1006. goto error;
  1007. }
  1008. isl_int_set_si(c->el[0], -1);
  1009. for (i = 0; i < bset->n_ineq; ++i) {
  1010. isl_seq_cpy(c->el + 1, bset->ineq[i], c->size - 1);
  1011. if (isl_tab_add_ineq(tab, c->el) < 0)
  1012. goto error;
  1013. if (tab->empty) {
  1014. isl_vec_free(c);
  1015. return tab;
  1016. }
  1017. }
  1018. isl_seq_clr(c->el + 1, c->size - 1);
  1019. isl_int_set_si(c->el[1], 1);
  1020. if (isl_tab_add_ineq(tab, c->el) < 0)
  1021. goto error;
  1022. isl_vec_free(c);
  1023. return tab;
  1024. error:
  1025. isl_vec_free(c);
  1026. isl_tab_free(tab);
  1027. return NULL;
  1028. }
  1029. /* Compute an interior point of "bset" by selecting an interior
  1030. * point in homogeneous space and projecting the point back down.
  1031. */
  1032. static __isl_give isl_vec *isl_basic_set_interior_point(
  1033. __isl_keep isl_basic_set *bset)
  1034. {
  1035. isl_vec *vec;
  1036. struct isl_tab *tab;
  1037. tab = tab_for_shifted_cone(bset);
  1038. vec = isl_tab_get_sample_value(tab);
  1039. isl_tab_free(tab);
  1040. if (!vec)
  1041. return NULL;
  1042. isl_seq_cpy(vec->el, vec->el + 1, vec->size - 1);
  1043. vec->size--;
  1044. return vec;
  1045. }
  1046. /* Call "fn" on all chambers of the parametric polytope with the shared
  1047. * facets of neighboring chambers only appearing in one of the chambers.
  1048. *
  1049. * We pick an interior point from one of the chambers and then make
  1050. * all constraints that do not satisfy this point strict.
  1051. * For constraints that saturate the interior point, the sign
  1052. * of the first non-zero coefficient is used to determine which
  1053. * of the two (internal) constraints should be tightened.
  1054. */
  1055. isl_stat isl_vertices_foreach_disjoint_cell(__isl_keep isl_vertices *vertices,
  1056. isl_stat (*fn)(__isl_take isl_cell *cell, void *user), void *user)
  1057. {
  1058. int i;
  1059. isl_vec *vec;
  1060. isl_cell *cell;
  1061. if (!vertices)
  1062. return isl_stat_error;
  1063. if (vertices->n_chambers == 0)
  1064. return isl_stat_ok;
  1065. if (vertices->n_chambers == 1) {
  1066. isl_basic_set *dom = isl_basic_set_copy(vertices->c[0].dom);
  1067. dom = isl_basic_set_set_integral(dom);
  1068. cell = isl_cell_alloc(isl_vertices_copy(vertices), dom, 0);
  1069. if (!cell)
  1070. return isl_stat_error;
  1071. return fn(cell, user);
  1072. }
  1073. vec = isl_basic_set_interior_point(vertices->c[0].dom);
  1074. if (!vec)
  1075. return isl_stat_error;
  1076. for (i = 0; i < vertices->n_chambers; ++i) {
  1077. int r;
  1078. isl_basic_set *dom = isl_basic_set_copy(vertices->c[i].dom);
  1079. if (i)
  1080. dom = isl_basic_set_tighten_outward(dom, vec);
  1081. dom = isl_basic_set_set_integral(dom);
  1082. cell = isl_cell_alloc(isl_vertices_copy(vertices), dom, i);
  1083. if (!cell)
  1084. goto error;
  1085. r = fn(cell, user);
  1086. if (r < 0)
  1087. goto error;
  1088. }
  1089. isl_vec_free(vec);
  1090. return isl_stat_ok;
  1091. error:
  1092. isl_vec_free(vec);
  1093. return isl_stat_error;
  1094. }
  1095. isl_stat isl_vertices_foreach_cell(__isl_keep isl_vertices *vertices,
  1096. isl_stat (*fn)(__isl_take isl_cell *cell, void *user), void *user)
  1097. {
  1098. int i;
  1099. isl_cell *cell;
  1100. if (!vertices)
  1101. return isl_stat_error;
  1102. if (vertices->n_chambers == 0)
  1103. return isl_stat_ok;
  1104. for (i = 0; i < vertices->n_chambers; ++i) {
  1105. isl_stat r;
  1106. isl_basic_set *dom = isl_basic_set_copy(vertices->c[i].dom);
  1107. cell = isl_cell_alloc(isl_vertices_copy(vertices), dom, i);
  1108. if (!cell)
  1109. return isl_stat_error;
  1110. r = fn(cell, user);
  1111. if (r < 0)
  1112. return isl_stat_error;
  1113. }
  1114. return isl_stat_ok;
  1115. }
  1116. isl_stat isl_vertices_foreach_vertex(__isl_keep isl_vertices *vertices,
  1117. isl_stat (*fn)(__isl_take isl_vertex *vertex, void *user), void *user)
  1118. {
  1119. int i;
  1120. isl_vertex *vertex;
  1121. if (!vertices)
  1122. return isl_stat_error;
  1123. if (vertices->n_vertices == 0)
  1124. return isl_stat_ok;
  1125. for (i = 0; i < vertices->n_vertices; ++i) {
  1126. isl_stat r;
  1127. vertex = isl_vertex_alloc(isl_vertices_copy(vertices), i);
  1128. if (!vertex)
  1129. return isl_stat_error;
  1130. r = fn(vertex, user);
  1131. if (r < 0)
  1132. return isl_stat_error;
  1133. }
  1134. return isl_stat_ok;
  1135. }
  1136. isl_stat isl_cell_foreach_vertex(__isl_keep isl_cell *cell,
  1137. isl_stat (*fn)(__isl_take isl_vertex *vertex, void *user), void *user)
  1138. {
  1139. int i;
  1140. isl_vertex *vertex;
  1141. if (!cell)
  1142. return isl_stat_error;
  1143. if (cell->n_vertices == 0)
  1144. return isl_stat_ok;
  1145. for (i = 0; i < cell->n_vertices; ++i) {
  1146. isl_stat r;
  1147. vertex = isl_vertex_alloc(isl_vertices_copy(cell->vertices),
  1148. cell->ids[i]);
  1149. if (!vertex)
  1150. return isl_stat_error;
  1151. r = fn(vertex, user);
  1152. if (r < 0)
  1153. return isl_stat_error;
  1154. }
  1155. return isl_stat_ok;
  1156. }
  1157. isl_ctx *isl_vertices_get_ctx(__isl_keep isl_vertices *vertices)
  1158. {
  1159. return vertices ? vertices->bset->ctx : NULL;
  1160. }
  1161. isl_size isl_vertices_get_n_vertices(__isl_keep isl_vertices *vertices)
  1162. {
  1163. return vertices ? vertices->n_vertices : isl_size_error;
  1164. }
  1165. __isl_give isl_vertices *isl_morph_vertices(__isl_take isl_morph *morph,
  1166. __isl_take isl_vertices *vertices)
  1167. {
  1168. int i;
  1169. isl_morph *param_morph = NULL;
  1170. if (!morph || !vertices)
  1171. goto error;
  1172. isl_assert(vertices->bset->ctx, vertices->ref == 1, goto error);
  1173. param_morph = isl_morph_copy(morph);
  1174. param_morph = isl_morph_dom_params(param_morph);
  1175. param_morph = isl_morph_ran_params(param_morph);
  1176. for (i = 0; i < vertices->n_vertices; ++i) {
  1177. vertices->v[i].dom = isl_morph_basic_set(
  1178. isl_morph_copy(param_morph), vertices->v[i].dom);
  1179. vertices->v[i].vertex = isl_morph_basic_set(
  1180. isl_morph_copy(morph), vertices->v[i].vertex);
  1181. if (!vertices->v[i].vertex)
  1182. goto error;
  1183. }
  1184. for (i = 0; i < vertices->n_chambers; ++i) {
  1185. vertices->c[i].dom = isl_morph_basic_set(
  1186. isl_morph_copy(param_morph), vertices->c[i].dom);
  1187. if (!vertices->c[i].dom)
  1188. goto error;
  1189. }
  1190. isl_morph_free(param_morph);
  1191. isl_morph_free(morph);
  1192. return vertices;
  1193. error:
  1194. isl_morph_free(param_morph);
  1195. isl_morph_free(morph);
  1196. isl_vertices_free(vertices);
  1197. return NULL;
  1198. }
  1199. /* Construct a simplex isl_cell spanned by the vertices with indices in
  1200. * "simplex_ids" and "other_ids" and call "fn" on this isl_cell.
  1201. */
  1202. static isl_stat call_on_simplex(__isl_keep isl_cell *cell,
  1203. int *simplex_ids, int n_simplex, int *other_ids, int n_other,
  1204. isl_stat (*fn)(__isl_take isl_cell *simplex, void *user), void *user)
  1205. {
  1206. int i;
  1207. isl_ctx *ctx;
  1208. struct isl_cell *simplex;
  1209. ctx = isl_cell_get_ctx(cell);
  1210. simplex = isl_calloc_type(ctx, struct isl_cell);
  1211. if (!simplex)
  1212. return isl_stat_error;
  1213. simplex->vertices = isl_vertices_copy(cell->vertices);
  1214. if (!simplex->vertices)
  1215. goto error;
  1216. simplex->dom = isl_basic_set_copy(cell->dom);
  1217. if (!simplex->dom)
  1218. goto error;
  1219. simplex->n_vertices = n_simplex + n_other;
  1220. simplex->ids = isl_alloc_array(ctx, int, simplex->n_vertices);
  1221. if (!simplex->ids)
  1222. goto error;
  1223. for (i = 0; i < n_simplex; ++i)
  1224. simplex->ids[i] = simplex_ids[i];
  1225. for (i = 0; i < n_other; ++i)
  1226. simplex->ids[n_simplex + i] = other_ids[i];
  1227. return fn(simplex, user);
  1228. error:
  1229. isl_cell_free(simplex);
  1230. return isl_stat_error;
  1231. }
  1232. /* Check whether the parametric vertex described by "vertex"
  1233. * lies on the facet corresponding to constraint "facet" of "bset".
  1234. * The isl_vec "v" is a temporary vector than can be used by this function.
  1235. *
  1236. * We eliminate the variables from the facet constraint using the
  1237. * equalities defining the vertex and check if the result is identical
  1238. * to zero.
  1239. *
  1240. * It would probably be better to keep track of the constraints defining
  1241. * a vertex during the vertex construction so that we could simply look
  1242. * it up here.
  1243. */
  1244. static int vertex_on_facet(__isl_keep isl_basic_set *vertex,
  1245. __isl_keep isl_basic_set *bset, int facet, __isl_keep isl_vec *v)
  1246. {
  1247. int i;
  1248. isl_int m;
  1249. isl_seq_cpy(v->el, bset->ineq[facet], v->size);
  1250. isl_int_init(m);
  1251. for (i = 0; i < vertex->n_eq; ++i) {
  1252. int k = isl_seq_last_non_zero(vertex->eq[i], v->size);
  1253. isl_seq_elim(v->el, vertex->eq[i], k, v->size, &m);
  1254. }
  1255. isl_int_clear(m);
  1256. return isl_seq_first_non_zero(v->el, v->size) == -1;
  1257. }
  1258. /* Triangulate the polytope spanned by the vertices with ids
  1259. * in "simplex_ids" and "other_ids" and call "fn" on each of
  1260. * the resulting simplices.
  1261. * If the input polytope is already a simplex, we simply call "fn".
  1262. * Otherwise, we pick a point from "other_ids" and add it to "simplex_ids".
  1263. * Then we consider each facet of "bset" that does not contain the point
  1264. * we just picked, but does contain some of the other points in "other_ids"
  1265. * and call ourselves recursively on the polytope spanned by the new
  1266. * "simplex_ids" and those points in "other_ids" that lie on the facet.
  1267. */
  1268. static isl_stat triangulate(__isl_keep isl_cell *cell, __isl_keep isl_vec *v,
  1269. int *simplex_ids, int n_simplex, int *other_ids, int n_other,
  1270. isl_stat (*fn)(__isl_take isl_cell *simplex, void *user), void *user)
  1271. {
  1272. int i, j, k;
  1273. isl_size d, nparam;
  1274. int *ids;
  1275. isl_ctx *ctx;
  1276. isl_basic_set *vertex;
  1277. isl_basic_set *bset;
  1278. ctx = isl_cell_get_ctx(cell);
  1279. d = isl_basic_set_dim(cell->vertices->bset, isl_dim_set);
  1280. nparam = isl_basic_set_dim(cell->vertices->bset, isl_dim_param);
  1281. if (d < 0 || nparam < 0)
  1282. return isl_stat_error;
  1283. if (n_simplex + n_other == d + 1)
  1284. return call_on_simplex(cell, simplex_ids, n_simplex,
  1285. other_ids, n_other, fn, user);
  1286. simplex_ids[n_simplex] = other_ids[0];
  1287. vertex = cell->vertices->v[other_ids[0]].vertex;
  1288. bset = cell->vertices->bset;
  1289. ids = isl_alloc_array(ctx, int, n_other - 1);
  1290. if (!ids)
  1291. goto error;
  1292. for (i = 0; i < bset->n_ineq; ++i) {
  1293. if (isl_seq_first_non_zero(bset->ineq[i] + 1 + nparam, d) == -1)
  1294. continue;
  1295. if (vertex_on_facet(vertex, bset, i, v))
  1296. continue;
  1297. for (j = 1, k = 0; j < n_other; ++j) {
  1298. isl_basic_set *ov;
  1299. ov = cell->vertices->v[other_ids[j]].vertex;
  1300. if (vertex_on_facet(ov, bset, i, v))
  1301. ids[k++] = other_ids[j];
  1302. }
  1303. if (k == 0)
  1304. continue;
  1305. if (triangulate(cell, v, simplex_ids, n_simplex + 1,
  1306. ids, k, fn, user) < 0)
  1307. goto error;
  1308. }
  1309. free(ids);
  1310. return isl_stat_ok;
  1311. error:
  1312. free(ids);
  1313. return isl_stat_error;
  1314. }
  1315. /* Triangulate the given cell and call "fn" on each of the resulting
  1316. * simplices.
  1317. */
  1318. isl_stat isl_cell_foreach_simplex(__isl_take isl_cell *cell,
  1319. isl_stat (*fn)(__isl_take isl_cell *simplex, void *user), void *user)
  1320. {
  1321. isl_size d, total;
  1322. isl_stat r;
  1323. isl_ctx *ctx;
  1324. isl_vec *v = NULL;
  1325. int *simplex_ids = NULL;
  1326. if (!cell)
  1327. return isl_stat_error;
  1328. d = isl_basic_set_dim(cell->vertices->bset, isl_dim_set);
  1329. total = isl_basic_set_dim(cell->vertices->bset, isl_dim_all);
  1330. if (d < 0 || total < 0)
  1331. return isl_stat_error;
  1332. if (cell->n_vertices == d + 1)
  1333. return fn(cell, user);
  1334. ctx = isl_cell_get_ctx(cell);
  1335. simplex_ids = isl_alloc_array(ctx, int, d + 1);
  1336. if (!simplex_ids)
  1337. goto error;
  1338. v = isl_vec_alloc(ctx, 1 + total);
  1339. if (!v)
  1340. goto error;
  1341. r = triangulate(cell, v, simplex_ids, 0,
  1342. cell->ids, cell->n_vertices, fn, user);
  1343. isl_vec_free(v);
  1344. free(simplex_ids);
  1345. isl_cell_free(cell);
  1346. return r;
  1347. error:
  1348. free(simplex_ids);
  1349. isl_vec_free(v);
  1350. isl_cell_free(cell);
  1351. return isl_stat_error;
  1352. }