isl_assert: validate all arguments and fix up fallout
[platform/upstream/isl.git] / isl_tab_pip.c
1 #include "isl_map_private.h"
2 #include "isl_seq.h"
3 #include "isl_tab.h"
4
5 /*
6  * The implementation of parametric integer linear programming in this file
7  * was inspired by the paper "Parametric Integer Programming" and the
8  * report "Solving systems of affine (in)equalities" by Paul Feautrier
9  * (and others).
10  *
11  * The strategy used for obtaining a feasible solution is different
12  * from the one used in isl_tab.c.  In particular, in isl_tab.c,
13  * upon finding a constraint that is not yet satisfied, we pivot
14  * in a row that increases the constant term of row holding the
15  * constraint, making sure the sample solution remains feasible
16  * for all the constraints it already satisfied.
17  * Here, we always pivot in the row holding the constraint,
18  * choosing a column that induces the lexicographically smallest
19  * increment to the sample solution.
20  *
21  * By starting out from a sample value that is lexicographically
22  * smaller than any integer point in the problem space, the first
23  * feasible integer sample point we find will also be the lexicographically
24  * smallest.  If all variables can be assumed to be non-negative,
25  * then the initial sample value may be chosen equal to zero.
26  * However, we will not make this assumption.  Instead, we apply
27  * the "big parameter" trick.  Any variable x is then not directly
28  * used in the tableau, but instead it its represented by another
29  * variable x' = M + x, where M is an arbitrarily large (positive)
30  * value.  x' is therefore always non-negative, whatever the value of x.
31  * Taking as initial smaple value x' = 0 corresponds to x = -M,
32  * which is always smaller than any possible value of x.
33  *
34  * We use the big parameter trick both in the main tableau and
35  * the context tableau, each of course having its own big parameter.
36  * Before doing any real work, we check if all the parameters
37  * happen to be non-negative.  If so, we drop the column corresponding
38  * to M from the initial context tableau.
39  */
40
41 /* isl_sol is an interface for constructing a solution to
42  * a parametric integer linear programming problem.
43  * Every time the algorithm reaches a state where a solution
44  * can be read off from the tableau (including cases where the tableau
45  * is empty), the function "add" is called on the isl_sol passed
46  * to find_solutions_main.
47  *
48  * The context tableau is owned by isl_sol and is updated incrementally.
49  *
50  * There is currently only one implementation of this interface,
51  * isl_sol_map, which simply collects the solutions in an isl_map
52  * and (optionally) the parts of the context where there is no solution
53  * in an isl_set.
54  */
55 struct isl_sol {
56         struct isl_tab *context_tab;
57         struct isl_sol *(*add)(struct isl_sol *sol, struct isl_tab *tab);
58         void (*free)(struct isl_sol *sol);
59 };
60
61 static void sol_free(struct isl_sol *sol)
62 {
63         if (!sol)
64                 return;
65         sol->free(sol);
66 }
67
68 struct isl_sol_map {
69         struct isl_sol  sol;
70         struct isl_map  *map;
71         struct isl_set  *empty;
72         int             max;
73 };
74
75 static void sol_map_free(struct isl_sol_map *sol_map)
76 {
77         isl_tab_free(sol_map->sol.context_tab);
78         isl_map_free(sol_map->map);
79         isl_set_free(sol_map->empty);
80         free(sol_map);
81 }
82
83 static void sol_map_free_wrap(struct isl_sol *sol)
84 {
85         sol_map_free((struct isl_sol_map *)sol);
86 }
87
88 static struct isl_sol_map *add_empty(struct isl_sol_map *sol)
89 {
90         struct isl_basic_set *bset;
91
92         if (!sol->empty)
93                 return sol;
94         sol->empty = isl_set_grow(sol->empty, 1);
95         bset = isl_basic_set_copy(sol->sol.context_tab->bset);
96         bset = isl_basic_set_simplify(bset);
97         bset = isl_basic_set_finalize(bset);
98         sol->empty = isl_set_add(sol->empty, bset);
99         if (!sol->empty)
100                 goto error;
101         return sol;
102 error:
103         sol_map_free(sol);
104         return NULL;
105 }
106
107 /* Add the solution identified by the tableau and the context tableau.
108  *
109  * The layout of the variables is as follows.
110  *      tab->n_var is equal to the total number of variables in the input
111  *                      map (including divs that were copied from the context)
112  *                      + the number of extra divs constructed
113  *      Of these, the first tab->n_param and the last tab->n_div variables
114  *      correspond to the variables in the context, i.e.,
115                 tab->n_param + tab->n_div = context_tab->n_var
116  *      tab->n_param is equal to the number of parameters and input
117  *                      dimensions in the input map
118  *      tab->n_div is equal to the number of divs in the context
119  *
120  * If there is no solution, then the basic set corresponding to the
121  * context tableau is added to the set "empty".
122  *
123  * Otherwise, a basic map is constructed with the same parameters
124  * and divs as the context, the dimensions of the context as input
125  * dimensions and a number of output dimensions that is equal to
126  * the number of output dimensions in the input map.
127  * The divs in the input map (if any) that do not correspond to any
128  * div in the context do not appear in the solution.
129  * The algorithm will make sure that they have an integer value,
130  * but these values themselves are of no interest.
131  *
132  * The constraints and divs of the context are simply copied
133  * fron context_tab->bset.
134  * To extract the value of the output variables, it should be noted
135  * that we always use a big parameter M and so the variable stored
136  * in the tableau is not an output variable x itself, but
137  *      x' = M + x (in case of minimization)
138  * or
139  *      x' = M - x (in case of maximization)
140  * If x' appears in a column, then its optimal value is zero,
141  * which means that the optimal value of x is an unbounded number
142  * (-M for minimization and M for maximization).
143  * We currently assume that the output dimensions in the original map
144  * are bounded, so this cannot occur.
145  * Similarly, when x' appears in a row, then the coefficient of M in that
146  * row is necessarily 1.
147  * If the row represents
148  *      d x' = c + d M + e(y)
149  * then, in case of minimization, an equality
150  *      c + e(y) - d x' = 0
151  * is added, and in case of maximization,
152  *      c + e(y) + d x' = 0
153  */
154 static struct isl_sol_map *sol_map_add(struct isl_sol_map *sol,
155         struct isl_tab *tab)
156 {
157         int i;
158         struct isl_basic_map *bmap = NULL;
159         struct isl_tab *context_tab;
160         unsigned n_eq;
161         unsigned n_ineq;
162         unsigned nparam;
163         unsigned total;
164         unsigned n_div;
165         unsigned n_out;
166         unsigned off;
167
168         if (!sol || !tab)
169                 goto error;
170
171         if (tab->empty)
172                 return add_empty(sol);
173
174         context_tab = sol->sol.context_tab;
175         off = 2 + tab->M;
176         n_out = isl_map_dim(sol->map, isl_dim_out);
177         n_eq = context_tab->bset->n_eq + n_out;
178         n_ineq = context_tab->bset->n_ineq;
179         nparam = tab->n_param;
180         total = isl_map_dim(sol->map, isl_dim_all);
181         bmap = isl_basic_map_alloc_dim(isl_map_get_dim(sol->map),
182                                     tab->n_div, n_eq, 2 * tab->n_div + n_ineq);
183         if (!bmap)
184                 goto error;
185         n_div = tab->n_div;
186         if (tab->rational)
187                 ISL_F_SET(bmap, ISL_BASIC_MAP_RATIONAL);
188         for (i = 0; i < context_tab->bset->n_div; ++i) {
189                 int k = isl_basic_map_alloc_div(bmap);
190                 if (k < 0)
191                         goto error;
192                 isl_seq_cpy(bmap->div[k],
193                             context_tab->bset->div[i], 1 + 1 + nparam);
194                 isl_seq_clr(bmap->div[k] + 1 + 1 + nparam, total - nparam);
195                 isl_seq_cpy(bmap->div[k] + 1 + 1 + total,
196                             context_tab->bset->div[i] + 1 + 1 + nparam, i);
197         }
198         for (i = 0; i < context_tab->bset->n_eq; ++i) {
199                 int k = isl_basic_map_alloc_equality(bmap);
200                 if (k < 0)
201                         goto error;
202                 isl_seq_cpy(bmap->eq[k], context_tab->bset->eq[i], 1 + nparam);
203                 isl_seq_clr(bmap->eq[k] + 1 + nparam, total - nparam);
204                 isl_seq_cpy(bmap->eq[k] + 1 + total,
205                             context_tab->bset->eq[i] + 1 + nparam, n_div);
206         }
207         for (i = 0; i < context_tab->bset->n_ineq; ++i) {
208                 int k = isl_basic_map_alloc_inequality(bmap);
209                 if (k < 0)
210                         goto error;
211                 isl_seq_cpy(bmap->ineq[k],
212                         context_tab->bset->ineq[i], 1 + nparam);
213                 isl_seq_clr(bmap->ineq[k] + 1 + nparam, total - nparam);
214                 isl_seq_cpy(bmap->ineq[k] + 1 + total,
215                         context_tab->bset->ineq[i] + 1 + nparam, n_div);
216         }
217         for (i = tab->n_param; i < total; ++i) {
218                 int k = isl_basic_map_alloc_equality(bmap);
219                 if (k < 0)
220                         goto error;
221                 isl_seq_clr(bmap->eq[k] + 1, isl_basic_map_total_dim(bmap));
222                 if (!tab->var[i].is_row) {
223                         /* no unbounded */
224                         isl_assert(bmap->ctx, !tab->M, goto error);
225                         isl_int_set_si(bmap->eq[k][0], 0);
226                         if (sol->max)
227                                 isl_int_set_si(bmap->eq[k][1 + i], 1);
228                         else
229                                 isl_int_set_si(bmap->eq[k][1 + i], -1);
230                 } else {
231                         int row, j;
232                         row = tab->var[i].index;
233                         /* no unbounded */
234                         if (tab->M)
235                                 isl_assert(bmap->ctx,
236                                         isl_int_eq(tab->mat->row[row][2],
237                                                    tab->mat->row[row][0]),
238                                         goto error);
239                         isl_int_set(bmap->eq[k][0], tab->mat->row[row][1]);
240                         for (j = 0; j < tab->n_param; ++j) {
241                                 int col;
242                                 if (tab->var[j].is_row)
243                                         continue;
244                                 col = tab->var[j].index;
245                                 isl_int_set(bmap->eq[k][1 + j],
246                                             tab->mat->row[row][off + col]);
247                         }
248                         for (j = 0; j < tab->n_div; ++j) {
249                                 int col;
250                                 if (tab->var[tab->n_var - tab->n_div+j].is_row)
251                                         continue;
252                                 col = tab->var[tab->n_var - tab->n_div+j].index;
253                                 isl_int_set(bmap->eq[k][1 + total + j],
254                                             tab->mat->row[row][off + col]);
255                         }
256                         if (sol->max)
257                                 isl_int_set(bmap->eq[k][1 + i],
258                                             tab->mat->row[row][0]);
259                         else
260                                 isl_int_neg(bmap->eq[k][1 + i],
261                                             tab->mat->row[row][0]);
262                 }
263         }
264         bmap = isl_basic_map_gauss(bmap, NULL);
265         bmap = isl_basic_map_normalize_constraints(bmap);
266         bmap = isl_basic_map_finalize(bmap);
267         sol->map = isl_map_grow(sol->map, 1);
268         sol->map = isl_map_add(sol->map, bmap);
269         if (!sol->map)
270                 goto error;
271         return sol;
272 error:
273         isl_basic_map_free(bmap);
274         sol_free(&sol->sol);
275         return NULL;
276 }
277
278 static struct isl_sol *sol_map_add_wrap(struct isl_sol *sol,
279         struct isl_tab *tab)
280 {
281         return (struct isl_sol *)sol_map_add((struct isl_sol_map *)sol, tab);
282 }
283
284
285 static struct isl_basic_set *isl_basic_set_add_ineq(struct isl_basic_set *bset,
286         isl_int *ineq)
287 {
288         int k;
289
290         bset = isl_basic_set_extend_constraints(bset, 0, 1);
291         if (!bset)
292                 return NULL;
293         k = isl_basic_set_alloc_inequality(bset);
294         if (k < 0)
295                 goto error;
296         isl_seq_cpy(bset->ineq[k], ineq, 1 + isl_basic_set_total_dim(bset));
297         return bset;
298 error:
299         isl_basic_set_free(bset);
300         return NULL;
301 }
302
303 static struct isl_basic_set *isl_basic_set_add_eq(struct isl_basic_set *bset,
304         isl_int *eq)
305 {
306         int k;
307
308         bset = isl_basic_set_extend_constraints(bset, 1, 0);
309         if (!bset)
310                 return NULL;
311         k = isl_basic_set_alloc_equality(bset);
312         if (k < 0)
313                 goto error;
314         isl_seq_cpy(bset->eq[k], eq, 1 + isl_basic_set_total_dim(bset));
315         return bset;
316 error:
317         isl_basic_set_free(bset);
318         return NULL;
319 }
320
321
322 /* Store the "parametric constant" of row "row" of tableau "tab" in "line",
323  * i.e., the constant term and the coefficients of all variables that
324  * appear in the context tableau.
325  * Note that the coefficient of the big parameter M is NOT copied.
326  * The context tableau may not have a big parameter and even when it
327  * does, it is a different big parameter.
328  */
329 static void get_row_parameter_line(struct isl_tab *tab, int row, isl_int *line)
330 {
331         int i;
332         unsigned off = 2 + tab->M;
333
334         isl_int_set(line[0], tab->mat->row[row][1]);
335         for (i = 0; i < tab->n_param; ++i) {
336                 if (tab->var[i].is_row)
337                         isl_int_set_si(line[1 + i], 0);
338                 else {
339                         int col = tab->var[i].index;
340                         isl_int_set(line[1 + i], tab->mat->row[row][off + col]);
341                 }
342         }
343         for (i = 0; i < tab->n_div; ++i) {
344                 if (tab->var[tab->n_var - tab->n_div + i].is_row)
345                         isl_int_set_si(line[1 + tab->n_param + i], 0);
346                 else {
347                         int col = tab->var[tab->n_var - tab->n_div + i].index;
348                         isl_int_set(line[1 + tab->n_param + i],
349                                     tab->mat->row[row][off + col]);
350                 }
351         }
352 }
353
354 /* Check if rows "row1" and "row2" have identical "parametric constants",
355  * as explained above.
356  * In this case, we also insist that the coefficients of the big parameter
357  * be the same as the values of the constants will only be the same
358  * if these coefficients are also the same.
359  */
360 static int identical_parameter_line(struct isl_tab *tab, int row1, int row2)
361 {
362         int i;
363         unsigned off = 2 + tab->M;
364
365         if (isl_int_ne(tab->mat->row[row1][1], tab->mat->row[row2][1]))
366                 return 0;
367
368         if (tab->M && isl_int_ne(tab->mat->row[row1][2],
369                                  tab->mat->row[row2][2]))
370                 return 0;
371
372         for (i = 0; i < tab->n_param + tab->n_div; ++i) {
373                 int pos = i < tab->n_param ? i :
374                         tab->n_var - tab->n_div + i - tab->n_param;
375                 int col;
376
377                 if (tab->var[pos].is_row)
378                         continue;
379                 col = tab->var[pos].index;
380                 if (isl_int_ne(tab->mat->row[row1][off + col],
381                                tab->mat->row[row2][off + col]))
382                         return 0;
383         }
384         return 1;
385 }
386
387 /* Return an inequality that expresses that the "parametric constant"
388  * should be non-negative.
389  * This function is only called when the coefficient of the big parameter
390  * is equal to zero.
391  */
392 static struct isl_vec *get_row_parameter_ineq(struct isl_tab *tab, int row)
393 {
394         struct isl_vec *ineq;
395
396         ineq = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_param + tab->n_div);
397         if (!ineq)
398                 return NULL;
399
400         get_row_parameter_line(tab, row, ineq->el);
401         if (ineq)
402                 ineq = isl_vec_normalize(ineq);
403
404         return ineq;
405 }
406
407 /* Return a integer division for use in a parametric cut based on the given row.
408  * In particular, let the parametric constant of the row be
409  *
410  *              \sum_i a_i y_i
411  *
412  * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
413  * The div returned is equal to
414  *
415  *              floor(\sum_i {-a_i} y_i) = floor((\sum_i (-a_i mod d) y_i)/d)
416  */
417 static struct isl_vec *get_row_parameter_div(struct isl_tab *tab, int row)
418 {
419         struct isl_vec *div;
420
421         div = isl_vec_alloc(tab->mat->ctx, 1 + 1 + tab->n_param + tab->n_div);
422         if (!div)
423                 return NULL;
424
425         isl_int_set(div->el[0], tab->mat->row[row][0]);
426         get_row_parameter_line(tab, row, div->el + 1);
427         div = isl_vec_normalize(div);
428         isl_seq_neg(div->el + 1, div->el + 1, div->size - 1);
429         isl_seq_fdiv_r(div->el + 1, div->el + 1, div->el[0], div->size - 1);
430
431         return div;
432 }
433
434 /* Return a integer division for use in transferring an integrality constraint
435  * to the context.
436  * In particular, let the parametric constant of the row be
437  *
438  *              \sum_i a_i y_i
439  *
440  * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
441  * The the returned div is equal to
442  *
443  *              floor(\sum_i {a_i} y_i) = floor((\sum_i (a_i mod d) y_i)/d)
444  */
445 static struct isl_vec *get_row_split_div(struct isl_tab *tab, int row)
446 {
447         struct isl_vec *div;
448
449         div = isl_vec_alloc(tab->mat->ctx, 1 + 1 + tab->n_param + tab->n_div);
450         if (!div)
451                 return NULL;
452
453         isl_int_set(div->el[0], tab->mat->row[row][0]);
454         get_row_parameter_line(tab, row, div->el + 1);
455         div = isl_vec_normalize(div);
456         isl_seq_fdiv_r(div->el + 1, div->el + 1, div->el[0], div->size - 1);
457
458         return div;
459 }
460
461 /* Construct and return an inequality that expresses an upper bound
462  * on the given div.
463  * In particular, if the div is given by
464  *
465  *      d = floor(e/m)
466  *
467  * then the inequality expresses
468  *
469  *      m d <= e
470  */
471 static struct isl_vec *ineq_for_div(struct isl_basic_set *bset, unsigned div)
472 {
473         unsigned total;
474         unsigned div_pos;
475         struct isl_vec *ineq;
476
477         total = isl_basic_set_total_dim(bset);
478         div_pos = 1 + total - bset->n_div + div;
479
480         ineq = isl_vec_alloc(bset->ctx, 1 + total);
481         if (!ineq)
482                 return NULL;
483
484         isl_seq_cpy(ineq->el, bset->div[div] + 1, 1 + total);
485         isl_int_neg(ineq->el[div_pos], bset->div[div][0]);
486         return ineq;
487 }
488
489 /* Given a row in the tableau and a div that was created
490  * using get_row_split_div and that been constrained to equality, i.e.,
491  *
492  *              d = floor(\sum_i {a_i} y_i) = \sum_i {a_i} y_i
493  *
494  * replace the expression "\sum_i {a_i} y_i" in the row by d,
495  * i.e., we subtract "\sum_i {a_i} y_i" and add 1 d.
496  * The coefficients of the non-parameters in the tableau have been
497  * verified to be integral.  We can therefore simply replace coefficient b
498  * by floor(b).  For the coefficients of the parameters we have
499  * floor(a_i) = a_i - {a_i}, while for the other coefficients, we have
500  * floor(b) = b.
501  */
502 static struct isl_tab *set_row_cst_to_div(struct isl_tab *tab, int row, int div)
503 {
504         int i;
505         int col;
506         unsigned off = 2 + tab->M;
507
508         isl_seq_fdiv_q(tab->mat->row[row] + 1, tab->mat->row[row] + 1,
509                         tab->mat->row[row][0], 1 + tab->M + tab->n_col);
510
511         isl_int_set_si(tab->mat->row[row][0], 1);
512
513         isl_assert(tab->mat->ctx,
514                 !tab->var[tab->n_var - tab->n_div + div].is_row, goto error);
515
516         col = tab->var[tab->n_var - tab->n_div + div].index;
517         isl_int_set_si(tab->mat->row[row][off + col], 1);
518
519         return tab;
520 error:
521         isl_tab_free(tab);
522         return NULL;
523 }
524
525 /* Check if the (parametric) constant of the given row is obviously
526  * negative, meaning that we don't need to consult the context tableau.
527  * If there is a big parameter and its coefficient is non-zero,
528  * then this coefficient determines the outcome.
529  * Otherwise, we check whether the constant is negative and
530  * all non-zero coefficients of parameters are negative and
531  * belong to non-negative parameters.
532  */
533 static int is_obviously_neg(struct isl_tab *tab, int row)
534 {
535         int i;
536         int col;
537         unsigned off = 2 + tab->M;
538
539         if (tab->M) {
540                 if (isl_int_is_pos(tab->mat->row[row][2]))
541                         return 0;
542                 if (isl_int_is_neg(tab->mat->row[row][2]))
543                         return 1;
544         }
545
546         if (isl_int_is_nonneg(tab->mat->row[row][1]))
547                 return 0;
548         for (i = 0; i < tab->n_param; ++i) {
549                 /* Eliminated parameter */
550                 if (tab->var[i].is_row)
551                         continue;
552                 col = tab->var[i].index;
553                 if (isl_int_is_zero(tab->mat->row[row][off + col]))
554                         continue;
555                 if (!tab->var[i].is_nonneg)
556                         return 0;
557                 if (isl_int_is_pos(tab->mat->row[row][off + col]))
558                         return 0;
559         }
560         for (i = 0; i < tab->n_div; ++i) {
561                 if (tab->var[tab->n_var - tab->n_div + i].is_row)
562                         continue;
563                 col = tab->var[tab->n_var - tab->n_div + i].index;
564                 if (isl_int_is_zero(tab->mat->row[row][off + col]))
565                         continue;
566                 if (!tab->var[tab->n_var - tab->n_div + i].is_nonneg)
567                         return 0;
568                 if (isl_int_is_pos(tab->mat->row[row][off + col]))
569                         return 0;
570         }
571         return 1;
572 }
573
574 /* Check if the (parametric) constant of the given row is obviously
575  * non-negative, meaning that we don't need to consult the context tableau.
576  * If there is a big parameter and its coefficient is non-zero,
577  * then this coefficient determines the outcome.
578  * Otherwise, we check whether the constant is non-negative and
579  * all non-zero coefficients of parameters are positive and
580  * belong to non-negative parameters.
581  */
582 static int is_obviously_nonneg(struct isl_tab *tab, int row)
583 {
584         int i;
585         int col;
586         unsigned off = 2 + tab->M;
587
588         if (tab->M) {
589                 if (isl_int_is_pos(tab->mat->row[row][2]))
590                         return 1;
591                 if (isl_int_is_neg(tab->mat->row[row][2]))
592                         return 0;
593         }
594
595         if (isl_int_is_neg(tab->mat->row[row][1]))
596                 return 0;
597         for (i = 0; i < tab->n_param; ++i) {
598                 /* Eliminated parameter */
599                 if (tab->var[i].is_row)
600                         continue;
601                 col = tab->var[i].index;
602                 if (isl_int_is_zero(tab->mat->row[row][off + col]))
603                         continue;
604                 if (!tab->var[i].is_nonneg)
605                         return 0;
606                 if (isl_int_is_neg(tab->mat->row[row][off + col]))
607                         return 0;
608         }
609         for (i = 0; i < tab->n_div; ++i) {
610                 if (tab->var[tab->n_var - tab->n_div + i].is_row)
611                         continue;
612                 col = tab->var[tab->n_var - tab->n_div + i].index;
613                 if (isl_int_is_zero(tab->mat->row[row][off + col]))
614                         continue;
615                 if (!tab->var[tab->n_var - tab->n_div + i].is_nonneg)
616                         return 0;
617                 if (isl_int_is_neg(tab->mat->row[row][off + col]))
618                         return 0;
619         }
620         return 1;
621 }
622
623 /* Given a row r and two columns, return the column that would
624  * lead to the lexicographically smallest increment in the sample
625  * solution when leaving the basis in favor of the row.
626  * Pivoting with column c will increment the sample value by a non-negative
627  * constant times a_{V,c}/a_{r,c}, with a_{V,c} the elements of column c
628  * corresponding to the non-parametric variables.
629  * If variable v appears in a column c_v, the a_{v,c} = 1 iff c = c_v,
630  * with all other entries in this virtual row equal to zero.
631  * If variable v appears in a row, then a_{v,c} is the element in column c
632  * of that row.
633  *
634  * Let v be the first variable with a_{v,c1}/a_{r,c1} != a_{v,c2}/a_{r,c2}.
635  * Then if a_{v,c1}/a_{r,c1} < a_{v,c2}/a_{r,c2}, i.e.,
636  * a_{v,c2} a_{r,c1} - a_{v,c1} a_{r,c2} > 0, c1 results in the minimal
637  * increment.  Otherwise, it's c2.
638  */
639 static int lexmin_col_pair(struct isl_tab *tab,
640         int row, int col1, int col2, isl_int tmp)
641 {
642         int i;
643         isl_int *tr;
644
645         tr = tab->mat->row[row] + 2 + tab->M;
646
647         for (i = tab->n_param; i < tab->n_var - tab->n_div; ++i) {
648                 int s1, s2;
649                 isl_int *r;
650
651                 if (!tab->var[i].is_row) {
652                         if (tab->var[i].index == col1)
653                                 return col2;
654                         if (tab->var[i].index == col2)
655                                 return col1;
656                         continue;
657                 }
658
659                 if (tab->var[i].index == row)
660                         continue;
661
662                 r = tab->mat->row[tab->var[i].index] + 2 + tab->M;
663                 s1 = isl_int_sgn(r[col1]);
664                 s2 = isl_int_sgn(r[col2]);
665                 if (s1 == 0 && s2 == 0)
666                         continue;
667                 if (s1 < s2)
668                         return col1;
669                 if (s2 < s1)
670                         return col2;
671
672                 isl_int_mul(tmp, r[col2], tr[col1]);
673                 isl_int_submul(tmp, r[col1], tr[col2]);
674                 if (isl_int_is_pos(tmp))
675                         return col1;
676                 if (isl_int_is_neg(tmp))
677                         return col2;
678         }
679         return -1;
680 }
681
682 /* Given a row in the tableau, find and return the column that would
683  * result in the lexicographically smallest, but positive, increment
684  * in the sample point.
685  * If there is no such column, then return tab->n_col.
686  * If anything goes wrong, return -1.
687  */
688 static int lexmin_pivot_col(struct isl_tab *tab, int row)
689 {
690         int j;
691         int col = tab->n_col;
692         isl_int *tr;
693         isl_int tmp;
694
695         tr = tab->mat->row[row] + 2 + tab->M;
696
697         isl_int_init(tmp);
698
699         for (j = tab->n_dead; j < tab->n_col; ++j) {
700                 if (tab->col_var[j] >= 0 &&
701                     (tab->col_var[j] < tab->n_param  ||
702                     tab->col_var[j] >= tab->n_var - tab->n_div))
703                         continue;
704
705                 if (!isl_int_is_pos(tr[j]))
706                         continue;
707
708                 if (col == tab->n_col)
709                         col = j;
710                 else
711                         col = lexmin_col_pair(tab, row, col, j, tmp);
712                 isl_assert(tab->mat->ctx, col >= 0, goto error);
713         }
714
715         isl_int_clear(tmp);
716         return col;
717 error:
718         isl_int_clear(tmp);
719         return -1;
720 }
721
722 /* Return the first known violated constraint, i.e., a non-negative
723  * contraint that currently has an either obviously negative value
724  * or a previously determined to be negative value.
725  *
726  * If any constraint has a negative coefficient for the big parameter,
727  * if any, then we return one of these first.
728  */
729 static int first_neg(struct isl_tab *tab)
730 {
731         int row;
732
733         if (tab->M)
734                 for (row = tab->n_redundant; row < tab->n_row; ++row) {
735                         if (!isl_tab_var_from_row(tab, row)->is_nonneg)
736                                 continue;
737                         if (isl_int_is_neg(tab->mat->row[row][2]))
738                                 return row;
739                 }
740         for (row = tab->n_redundant; row < tab->n_row; ++row) {
741                 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
742                         continue;
743                 if (tab->row_sign) {
744                         if (tab->row_sign[row] == 0 &&
745                             is_obviously_neg(tab, row))
746                                 tab->row_sign[row] = isl_tab_row_neg;
747                         if (tab->row_sign[row] != isl_tab_row_neg)
748                                 continue;
749                 } else if (!is_obviously_neg(tab, row))
750                         continue;
751                 return row;
752         }
753         return -1;
754 }
755
756 /* Resolve all known or obviously violated constraints through pivoting.
757  * In particular, as long as we can find any violated constraint, we
758  * look for a pivoting column that would result in the lexicographicallly
759  * smallest increment in the sample point.  If there is no such column
760  * then the tableau is infeasible.
761  */
762 static struct isl_tab *restore_lexmin(struct isl_tab *tab)
763 {
764         int row, col;
765
766         if (!tab)
767                 return NULL;
768         if (tab->empty)
769                 return tab;
770         while ((row = first_neg(tab)) != -1) {
771                 col = lexmin_pivot_col(tab, row);
772                 if (col >= tab->n_col)
773                         return isl_tab_mark_empty(tab);
774                 if (col < 0)
775                         goto error;
776                 isl_tab_pivot(tab, row, col);
777         }
778         return tab;
779 error:
780         isl_tab_free(tab);
781         return NULL;
782 }
783
784 /* Given a row that represents an equality, look for an appropriate
785  * pivoting column.
786  * In particular, if there are any non-zero coefficients among
787  * the non-parameter variables, then we take the last of these
788  * variables.  Eliminating this variable in terms of the other
789  * variables and/or parameters does not influence the property
790  * that all column in the initial tableau are lexicographically
791  * positive.  The row corresponding to the eliminated variable
792  * will only have non-zero entries below the diagonal of the
793  * initial tableau.  That is, we transform
794  *
795  *              I                               I
796  *                1             into            a
797  *                  I                             I
798  *
799  * If there is no such non-parameter variable, then we are dealing with
800  * pure parameter equality and we pick any parameter with coefficient 1 or -1
801  * for elimination.  This will ensure that the eliminated parameter
802  * always has an integer value whenever all the other parameters are integral.
803  * If there is no such parameter then we return -1.
804  */
805 static int last_var_col_or_int_par_col(struct isl_tab *tab, int row)
806 {
807         unsigned off = 2 + tab->M;
808         int i;
809
810         for (i = tab->n_var - tab->n_div - 1; i >= 0 && i >= tab->n_param; --i) {
811                 int col;
812                 if (tab->var[i].is_row)
813                         continue;
814                 col = tab->var[i].index;
815                 if (col <= tab->n_dead)
816                         continue;
817                 if (!isl_int_is_zero(tab->mat->row[row][off + col]))
818                         return col;
819         }
820         for (i = tab->n_dead; i < tab->n_col; ++i) {
821                 if (isl_int_is_one(tab->mat->row[row][off + i]))
822                         return i;
823                 if (isl_int_is_negone(tab->mat->row[row][off + i]))
824                         return i;
825         }
826         return -1;
827 }
828
829 /* Add an equality that is known to be valid to the tableau.
830  * We first check if we can eliminate a variable or a parameter.
831  * If not, we add the equality as two inequalities.
832  * In this case, the equality was a pure parameter equality and there
833  * is no need to resolve any constraint violations.
834  */
835 static struct isl_tab *add_lexmin_valid_eq(struct isl_tab *tab, isl_int *eq)
836 {
837         int i;
838         int r;
839
840         if (!tab)
841                 return NULL;
842         r = isl_tab_add_row(tab, eq);
843         if (r < 0)
844                 goto error;
845
846         r = tab->con[r].index;
847         i = last_var_col_or_int_par_col(tab, r);
848         if (i < 0) {
849                 tab->con[r].is_nonneg = 1;
850                 isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]);
851                 isl_seq_neg(eq, eq, 1 + tab->n_var);
852                 r = isl_tab_add_row(tab, eq);
853                 if (r < 0)
854                         goto error;
855                 tab->con[r].is_nonneg = 1;
856                 isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]);
857         } else {
858                 isl_tab_pivot(tab, r, i);
859                 isl_tab_kill_col(tab, i);
860                 tab->n_eq++;
861
862                 tab = restore_lexmin(tab);
863         }
864
865         return tab;
866 error:
867         isl_tab_free(tab);
868         return NULL;
869 }
870
871 /* Check if the given row is a pure constant.
872  */
873 static int is_constant(struct isl_tab *tab, int row)
874 {
875         unsigned off = 2 + tab->M;
876
877         return isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
878                                         tab->n_col - tab->n_dead) == -1;
879 }
880
881 /* Add an equality that may or may not be valid to the tableau.
882  * If the resulting row is a pure constant, then it must be zero.
883  * Otherwise, the resulting tableau is empty.
884  *
885  * If the row is not a pure constant, then we add two inequalities,
886  * each time checking that they can be satisfied.
887  * In the end we try to use one of the two constraints to eliminate
888  * a column.
889  */
890 static struct isl_tab *add_lexmin_eq(struct isl_tab *tab, isl_int *eq)
891 {
892         int r1, r2;
893         int sgn;
894         int row;
895
896         if (!tab)
897                 return NULL;
898         if (tab->bset) {
899                 tab->bset = isl_basic_set_add_eq(tab->bset, eq);
900                 isl_tab_push(tab, isl_tab_undo_bset_eq);
901                 if (!tab->bset)
902                         goto error;
903         }
904         r1 = isl_tab_add_row(tab, eq);
905         if (r1 < 0)
906                 goto error;
907         tab->con[r1].is_nonneg = 1;
908         isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r1]);
909
910         row = tab->con[r1].index;
911         if (is_constant(tab, row)) {
912                 if (!isl_int_is_zero(tab->mat->row[row][1]) ||
913                     (tab->M && !isl_int_is_zero(tab->mat->row[row][2])))
914                         return isl_tab_mark_empty(tab);
915                 return tab;
916         }
917
918         tab = restore_lexmin(tab);
919         if (!tab || tab->empty)
920                 return tab;
921
922         isl_seq_neg(eq, eq, 1 + tab->n_var);
923
924         r2 = isl_tab_add_row(tab, eq);
925         if (r2 < 0)
926                 goto error;
927         tab->con[r2].is_nonneg = 1;
928         isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r2]);
929
930         tab = restore_lexmin(tab);
931         if (!tab || tab->empty)
932                 return tab;
933
934         if (!tab->con[r1].is_row)
935                 isl_tab_kill_col(tab, tab->con[r1].index);
936         else if (!tab->con[r2].is_row)
937                 isl_tab_kill_col(tab, tab->con[r2].index);
938         else if (isl_int_is_zero(tab->mat->row[tab->con[r1].index][1])) {
939                 unsigned off = 2 + tab->M;
940                 int i;
941                 int row = tab->con[r1].index;
942                 i = isl_seq_first_non_zero(tab->mat->row[row]+off+tab->n_dead,
943                                                 tab->n_col - tab->n_dead);
944                 if (i != -1) {
945                         isl_tab_pivot(tab, row, tab->n_dead + i);
946                         isl_tab_kill_col(tab, tab->n_dead + i);
947                 }
948         }
949
950         return tab;
951 error:
952         isl_tab_free(tab);
953         return NULL;
954 }
955
956 /* Add an inequality to the tableau, resolving violations using
957  * restore_lexmin.
958  */
959 static struct isl_tab *add_lexmin_ineq(struct isl_tab *tab, isl_int *ineq)
960 {
961         int r;
962         int sgn;
963
964         if (!tab)
965                 return NULL;
966         if (tab->bset) {
967                 tab->bset = isl_basic_set_add_ineq(tab->bset, ineq);
968                 isl_tab_push(tab, isl_tab_undo_bset_ineq);
969                 if (!tab->bset)
970                         goto error;
971         }
972         r = isl_tab_add_row(tab, ineq);
973         if (r < 0)
974                 goto error;
975         tab->con[r].is_nonneg = 1;
976         isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]);
977         if (isl_tab_row_is_redundant(tab, tab->con[r].index)) {
978                 isl_tab_mark_redundant(tab, tab->con[r].index);
979                 return tab;
980         }
981
982         tab = restore_lexmin(tab);
983         if (tab && !tab->empty && tab->con[r].is_row &&
984                  isl_tab_row_is_redundant(tab, tab->con[r].index))
985                 isl_tab_mark_redundant(tab, tab->con[r].index);
986         return tab;
987 error:
988         isl_tab_free(tab);
989         return NULL;
990 }
991
992 /* Check if the coefficients of the parameters are all integral.
993  */
994 static int integer_parameter(struct isl_tab *tab, int row)
995 {
996         int i;
997         int col;
998         unsigned off = 2 + tab->M;
999
1000         for (i = 0; i < tab->n_param; ++i) {
1001                 /* Eliminated parameter */
1002                 if (tab->var[i].is_row)
1003                         continue;
1004                 col = tab->var[i].index;
1005                 if (!isl_int_is_divisible_by(tab->mat->row[row][off + col],
1006                                                 tab->mat->row[row][0]))
1007                         return 0;
1008         }
1009         for (i = 0; i < tab->n_div; ++i) {
1010                 if (tab->var[tab->n_var - tab->n_div + i].is_row)
1011                         continue;
1012                 col = tab->var[tab->n_var - tab->n_div + i].index;
1013                 if (!isl_int_is_divisible_by(tab->mat->row[row][off + col],
1014                                                 tab->mat->row[row][0]))
1015                         return 0;
1016         }
1017         return 1;
1018 }
1019
1020 /* Check if the coefficients of the non-parameter variables are all integral.
1021  */
1022 static int integer_variable(struct isl_tab *tab, int row)
1023 {
1024         int i;
1025         unsigned off = 2 + tab->M;
1026
1027         for (i = 0; i < tab->n_col; ++i) {
1028                 if (tab->col_var[i] >= 0 &&
1029                     (tab->col_var[i] < tab->n_param ||
1030                      tab->col_var[i] >= tab->n_var - tab->n_div))
1031                         continue;
1032                 if (!isl_int_is_divisible_by(tab->mat->row[row][off + i],
1033                                                 tab->mat->row[row][0]))
1034                         return 0;
1035         }
1036         return 1;
1037 }
1038
1039 /* Check if the constant term is integral.
1040  */
1041 static int integer_constant(struct isl_tab *tab, int row)
1042 {
1043         return isl_int_is_divisible_by(tab->mat->row[row][1],
1044                                         tab->mat->row[row][0]);
1045 }
1046
1047 #define I_CST   1 << 0
1048 #define I_PAR   1 << 1
1049 #define I_VAR   1 << 2
1050
1051 /* Check for first (non-parameter) variable that is non-integer and
1052  * therefore requires a cut.
1053  * For parametric tableaus, there are three parts in a row,
1054  * the constant, the coefficients of the parameters and the rest.
1055  * For each part, we check whether the coefficients in that part
1056  * are all integral and if so, set the corresponding flag in *f.
1057  * If the constant and the parameter part are integral, then the
1058  * current sample value is integral and no cut is required
1059  * (irrespective of whether the variable part is integral).
1060  */
1061 static int first_non_integer(struct isl_tab *tab, int *f)
1062 {
1063         int i;
1064
1065         for (i = tab->n_param; i < tab->n_var - tab->n_div; ++i) {
1066                 int flags = 0;
1067                 int row;
1068                 if (!tab->var[i].is_row)
1069                         continue;
1070                 row = tab->var[i].index;
1071                 if (integer_constant(tab, row))
1072                         ISL_FL_SET(flags, I_CST);
1073                 if (integer_parameter(tab, row))
1074                         ISL_FL_SET(flags, I_PAR);
1075                 if (ISL_FL_ISSET(flags, I_CST) && ISL_FL_ISSET(flags, I_PAR))
1076                         continue;
1077                 if (integer_variable(tab, row))
1078                         ISL_FL_SET(flags, I_VAR);
1079                 *f = flags;
1080                 return row;
1081         }
1082         return -1;
1083 }
1084
1085 /* Add a (non-parametric) cut to cut away the non-integral sample
1086  * value of the given row.
1087  *
1088  * If the row is given by
1089  *
1090  *      m r = f + \sum_i a_i y_i
1091  *
1092  * then the cut is
1093  *
1094  *      c = - {-f/m} + \sum_i {a_i/m} y_i >= 0
1095  *
1096  * The big parameter, if any, is ignored, since it is assumed to be big
1097  * enough to be divisible by any integer.
1098  * If the tableau is actually a parametric tableau, then this function
1099  * is only called when all coefficients of the parameters are integral.
1100  * The cut therefore has zero coefficients for the parameters.
1101  *
1102  * The current value is known to be negative, so row_sign, if it
1103  * exists, is set accordingly.
1104  *
1105  * Return the row of the cut or -1.
1106  */
1107 static int add_cut(struct isl_tab *tab, int row)
1108 {
1109         int i;
1110         int r;
1111         isl_int *r_row;
1112         unsigned off = 2 + tab->M;
1113
1114         if (isl_tab_extend_cons(tab, 1) < 0)
1115                 return -1;
1116         r = isl_tab_allocate_con(tab);
1117         if (r < 0)
1118                 return -1;
1119
1120         r_row = tab->mat->row[tab->con[r].index];
1121         isl_int_set(r_row[0], tab->mat->row[row][0]);
1122         isl_int_neg(r_row[1], tab->mat->row[row][1]);
1123         isl_int_fdiv_r(r_row[1], r_row[1], tab->mat->row[row][0]);
1124         isl_int_neg(r_row[1], r_row[1]);
1125         if (tab->M)
1126                 isl_int_set_si(r_row[2], 0);
1127         for (i = 0; i < tab->n_col; ++i)
1128                 isl_int_fdiv_r(r_row[off + i],
1129                         tab->mat->row[row][off + i], tab->mat->row[row][0]);
1130
1131         tab->con[r].is_nonneg = 1;
1132         isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]);
1133         if (tab->row_sign)
1134                 tab->row_sign[tab->con[r].index] = isl_tab_row_neg;
1135
1136         return tab->con[r].index;
1137 }
1138
1139 /* Given a non-parametric tableau, add cuts until an integer
1140  * sample point is obtained or until the tableau is determined
1141  * to be integer infeasible.
1142  * As long as there is any non-integer value in the sample point,
1143  * we add an appropriate cut, if possible and resolve the violated
1144  * cut constraint using restore_lexmin.
1145  * If one of the corresponding rows is equal to an integral
1146  * combination of variables/constraints plus a non-integral constant,
1147  * then there is no way to obtain an integer point an we return
1148  * a tableau that is marked empty.
1149  */
1150 static struct isl_tab *cut_to_integer_lexmin(struct isl_tab *tab)
1151 {
1152         int row;
1153         int flags;
1154
1155         if (!tab)
1156                 return NULL;
1157         if (tab->empty)
1158                 return tab;
1159
1160         while ((row = first_non_integer(tab, &flags)) != -1) {
1161                 if (ISL_FL_ISSET(flags, I_VAR))
1162                         return isl_tab_mark_empty(tab);
1163                 row = add_cut(tab, row);
1164                 if (row < 0)
1165                         goto error;
1166                 tab = restore_lexmin(tab);
1167                 if (!tab || tab->empty)
1168                         break;
1169         }
1170         return tab;
1171 error:
1172         isl_tab_free(tab);
1173         return NULL;
1174 }
1175
1176 static struct isl_tab *drop_sample(struct isl_tab *tab, int s)
1177 {
1178         if (s != tab->n_outside)
1179                 isl_mat_swap_rows(tab->samples, tab->n_outside, s);
1180         tab->n_outside++;
1181         isl_tab_push(tab, isl_tab_undo_drop_sample);
1182
1183         return tab;
1184 }
1185
1186 /* Check whether all the currently active samples also satisfy the inequality
1187  * "ineq" (treated as an equality if eq is set).
1188  * Remove those samples that do not.
1189  */
1190 static struct isl_tab *check_samples(struct isl_tab *tab, isl_int *ineq, int eq)
1191 {
1192         int i;
1193         isl_int v;
1194
1195         if (!tab)
1196                 return NULL;
1197
1198         isl_assert(tab->mat->ctx, tab->bset, goto error);
1199         isl_assert(tab->mat->ctx, tab->samples, goto error);
1200         isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var, goto error);
1201
1202         isl_int_init(v);
1203         for (i = tab->n_outside; i < tab->n_sample; ++i) {
1204                 int sgn;
1205                 isl_seq_inner_product(ineq, tab->samples->row[i],
1206                                         1 + tab->n_var, &v);
1207                 sgn = isl_int_sgn(v);
1208                 if (eq ? (sgn == 0) : (sgn >= 0))
1209                         continue;
1210                 tab = drop_sample(tab, i);
1211                 if (!tab)
1212                         break;
1213         }
1214         isl_int_clear(v);
1215
1216         return tab;
1217 error:
1218         isl_tab_free(tab);
1219         return NULL;
1220 }
1221
1222 /* Check whether the sample value of the tableau is finite,
1223  * i.e., either the tableau does not use a big parameter, or
1224  * all values of the variables are equal to the big parameter plus
1225  * some constant.  This constant is the actual sample value.
1226  */
1227 int sample_is_finite(struct isl_tab *tab)
1228 {
1229         int i;
1230
1231         if (!tab->M)
1232                 return 1;
1233
1234         for (i = 0; i < tab->n_var; ++i) {
1235                 int row;
1236                 if (!tab->var[i].is_row)
1237                         return 0;
1238                 row = tab->var[i].index;
1239                 if (isl_int_ne(tab->mat->row[row][0], tab->mat->row[row][2]))
1240                         return 0;
1241         }
1242         return 1;
1243 }
1244
1245 /* Check if the context tableau of sol has any integer points.
1246  * Returns -1 if an error occurred.
1247  * If an integer point can be found and if moreover it is finite,
1248  * then it is added to the list of sample values.
1249  *
1250  * This function is only called when none of the currently active sample
1251  * values satisfies the most recently added constraint.
1252  */
1253 static int context_is_feasible(struct isl_sol *sol)
1254 {
1255         struct isl_tab_undo *snap;
1256         struct isl_tab *tab;
1257         int feasible;
1258
1259         if (!sol || !sol->context_tab)
1260                 return -1;
1261
1262         snap = isl_tab_snap(sol->context_tab);
1263         isl_tab_push_basis(sol->context_tab);
1264
1265         sol->context_tab = cut_to_integer_lexmin(sol->context_tab);
1266         if (!sol->context_tab)
1267                 goto error;
1268
1269         tab = sol->context_tab;
1270         if (!tab->empty && sample_is_finite(tab)) {
1271                 struct isl_vec *sample;
1272
1273                 tab->samples = isl_mat_extend(tab->samples,
1274                                         tab->n_sample + 1, tab->samples->n_col);
1275                 if (!tab->samples)
1276                         goto error;
1277
1278                 sample = isl_tab_get_sample_value(tab);
1279                 if (!sample)
1280                         goto error;
1281                 isl_seq_cpy(tab->samples->row[tab->n_sample],
1282                                 sample->el, sample->size);
1283                 isl_vec_free(sample);
1284                 tab->n_sample++;
1285         }
1286
1287         feasible = !sol->context_tab->empty;
1288         if (isl_tab_rollback(sol->context_tab, snap) < 0)
1289                 goto error;
1290
1291         return feasible;
1292 error:
1293         isl_tab_free(sol->context_tab);
1294         sol->context_tab = NULL;
1295         return -1;
1296 }
1297
1298 /* First check if any of the currently active sample values satisfies
1299  * the inequality "ineq" (an equality if eq is set).
1300  * If not, continue with check_integer_feasible.
1301  */
1302 static int context_valid_sample_or_feasible(struct isl_sol *sol,
1303         isl_int *ineq, int eq)
1304 {
1305         int i;
1306         isl_int v;
1307         struct isl_tab *tab;
1308
1309         if (!sol || !sol->context_tab)
1310                 return -1;
1311
1312         tab = sol->context_tab;
1313         isl_assert(tab->mat->ctx, tab->bset, return -1);
1314         isl_assert(tab->mat->ctx, tab->samples, return -1);
1315         isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var, return -1);
1316
1317         isl_int_init(v);
1318         for (i = tab->n_outside; i < tab->n_sample; ++i) {
1319                 int sgn;
1320                 isl_seq_inner_product(ineq, tab->samples->row[i],
1321                                         1 + tab->n_var, &v);
1322                 sgn = isl_int_sgn(v);
1323                 if (eq ? (sgn == 0) : (sgn >= 0))
1324                         break;
1325         }
1326         isl_int_clear(v);
1327
1328         if (i < tab->n_sample)
1329                 return 1;
1330
1331         return context_is_feasible(sol);
1332 }
1333
1334 /* For a div d = floor(f/m), add the constraints
1335  *
1336  *              f - m d >= 0
1337  *              -(f-(m-1)) + m d >= 0
1338  *
1339  * Note that the second constraint is the negation of
1340  *
1341  *              f - m d >= m
1342  */
1343 static struct isl_tab *add_div_constraints(struct isl_tab *tab, unsigned div)
1344 {
1345         int i, j;
1346         unsigned total;
1347         unsigned div_pos;
1348         struct isl_vec *ineq;
1349
1350         if (!tab)
1351                 return NULL;
1352
1353         total = isl_basic_set_total_dim(tab->bset);
1354         div_pos = 1 + total - tab->bset->n_div + div;
1355
1356         ineq = ineq_for_div(tab->bset, div);
1357         if (!ineq)
1358                 goto error;
1359
1360         tab = add_lexmin_ineq(tab, ineq->el);
1361
1362         isl_seq_neg(ineq->el, tab->bset->div[div] + 1, 1 + total);
1363         isl_int_set(ineq->el[div_pos], tab->bset->div[div][0]);
1364         isl_int_add(ineq->el[0], ineq->el[0], ineq->el[div_pos]);
1365         isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
1366         tab = add_lexmin_ineq(tab, ineq->el);
1367
1368         isl_vec_free(ineq);
1369
1370         return tab;
1371 error:
1372         isl_tab_free(tab);
1373         return NULL;
1374 }
1375
1376 /* Add a div specified by "div" to both the main tableau and
1377  * the context tableau.  In case of the main tableau, we only
1378  * need to add an extra div.  In the context tableau, we also
1379  * need to express the meaning of the div.
1380  * Return the index of the div or -1 if anything went wrong.
1381  */
1382 static int add_div(struct isl_tab *tab, struct isl_tab **context_tab,
1383         struct isl_vec *div)
1384 {
1385         int i;
1386         int r;
1387         int k;
1388         struct isl_mat *samples;
1389
1390         if (isl_tab_extend_vars(*context_tab, 1) < 0)
1391                 goto error;
1392         r = isl_tab_allocate_var(*context_tab);
1393         if (r < 0)
1394                 goto error;
1395         (*context_tab)->var[r].is_nonneg = 1;
1396         (*context_tab)->var[r].frozen = 1;
1397
1398         samples = isl_mat_extend((*context_tab)->samples,
1399                         (*context_tab)->n_sample, 1 + (*context_tab)->n_var);
1400         (*context_tab)->samples = samples;
1401         if (!samples)
1402                 goto error;
1403         for (i = (*context_tab)->n_outside; i < samples->n_row; ++i) {
1404                 isl_seq_inner_product(div->el + 1, samples->row[i],
1405                         div->size - 1, &samples->row[i][samples->n_col - 1]);
1406                 isl_int_fdiv_q(samples->row[i][samples->n_col - 1],
1407                                samples->row[i][samples->n_col - 1], div->el[0]);
1408         }
1409
1410         (*context_tab)->bset = isl_basic_set_extend_dim((*context_tab)->bset,
1411                 isl_basic_set_get_dim((*context_tab)->bset), 1, 0, 2);
1412         k = isl_basic_set_alloc_div((*context_tab)->bset);
1413         if (k < 0)
1414                 goto error;
1415         isl_seq_cpy((*context_tab)->bset->div[k], div->el, div->size);
1416         isl_tab_push((*context_tab), isl_tab_undo_bset_div);
1417         *context_tab = add_div_constraints(*context_tab, k);
1418         if (!*context_tab)
1419                 goto error;
1420
1421         if (isl_tab_extend_vars(tab, 1) < 0)
1422                 goto error;
1423         r = isl_tab_allocate_var(tab);
1424         if (r < 0)
1425                 goto error;
1426         if (!(*context_tab)->M)
1427                 tab->var[r].is_nonneg = 1;
1428         tab->var[r].frozen = 1;
1429         tab->n_div++;
1430
1431         return tab->n_div - 1;
1432 error:
1433         isl_tab_free(*context_tab);
1434         *context_tab = NULL;
1435         return -1;
1436 }
1437
1438 static int find_div(struct isl_tab *tab, isl_int *div, isl_int denom)
1439 {
1440         int i;
1441         unsigned total = isl_basic_set_total_dim(tab->bset);
1442
1443         for (i = 0; i < tab->bset->n_div; ++i) {
1444                 if (isl_int_ne(tab->bset->div[i][0], denom))
1445                         continue;
1446                 if (!isl_seq_eq(tab->bset->div[i] + 1, div, total))
1447                         continue;
1448                 return i;
1449         }
1450         return -1;
1451 }
1452
1453 /* Return the index of a div that corresponds to "div".
1454  * We first check if we already have such a div and if not, we create one.
1455  */
1456 static int get_div(struct isl_tab *tab, struct isl_tab **context_tab,
1457         struct isl_vec *div)
1458 {
1459         int d;
1460
1461         d = find_div(*context_tab, div->el + 1, div->el[0]);
1462         if (d != -1)
1463                 return d;
1464
1465         return add_div(tab, context_tab, div);
1466 }
1467
1468 /* Add a parametric cut to cut away the non-integral sample value
1469  * of the give row.
1470  * Let a_i be the coefficients of the constant term and the parameters
1471  * and let b_i be the coefficients of the variables or constraints
1472  * in basis of the tableau.
1473  * Let q be the div q = floor(\sum_i {-a_i} y_i).
1474  *
1475  * The cut is expressed as
1476  *
1477  *      c = \sum_i -{-a_i} y_i + \sum_i {b_i} x_i + q >= 0
1478  *
1479  * If q did not already exist in the context tableau, then it is added first.
1480  * If q is in a column of the main tableau then the "+ q" can be accomplished
1481  * by setting the corresponding entry to the denominator of the constraint.
1482  * If q happens to be in a row of the main tableau, then the corresponding
1483  * row needs to be added instead (taking care of the denominators).
1484  * Note that this is very unlikely, but perhaps not entirely impossible.
1485  *
1486  * The current value of the cut is known to be negative (or at least
1487  * non-positive), so row_sign is set accordingly.
1488  *
1489  * Return the row of the cut or -1.
1490  */
1491 static int add_parametric_cut(struct isl_tab *tab, int row,
1492         struct isl_tab **context_tab)
1493 {
1494         struct isl_vec *div;
1495         int d;
1496         int i;
1497         int r;
1498         isl_int *r_row;
1499         int col;
1500         unsigned off = 2 + tab->M;
1501
1502         if (!*context_tab)
1503                 goto error;
1504
1505         if (isl_tab_extend_cons(*context_tab, 3) < 0)
1506                 goto error;
1507
1508         div = get_row_parameter_div(tab, row);
1509         if (!div)
1510                 return -1;
1511
1512         d = get_div(tab, context_tab, div);
1513         if (d < 0)
1514                 goto error;
1515
1516         if (isl_tab_extend_cons(tab, 1) < 0)
1517                 return -1;
1518         r = isl_tab_allocate_con(tab);
1519         if (r < 0)
1520                 return -1;
1521
1522         r_row = tab->mat->row[tab->con[r].index];
1523         isl_int_set(r_row[0], tab->mat->row[row][0]);
1524         isl_int_neg(r_row[1], tab->mat->row[row][1]);
1525         isl_int_fdiv_r(r_row[1], r_row[1], tab->mat->row[row][0]);
1526         isl_int_neg(r_row[1], r_row[1]);
1527         if (tab->M)
1528                 isl_int_set_si(r_row[2], 0);
1529         for (i = 0; i < tab->n_param; ++i) {
1530                 if (tab->var[i].is_row)
1531                         continue;
1532                 col = tab->var[i].index;
1533                 isl_int_neg(r_row[off + col], tab->mat->row[row][off + col]);
1534                 isl_int_fdiv_r(r_row[off + col], r_row[off + col],
1535                                 tab->mat->row[row][0]);
1536                 isl_int_neg(r_row[off + col], r_row[off + col]);
1537         }
1538         for (i = 0; i < tab->n_div; ++i) {
1539                 if (tab->var[tab->n_var - tab->n_div + i].is_row)
1540                         continue;
1541                 col = tab->var[tab->n_var - tab->n_div + i].index;
1542                 isl_int_neg(r_row[off + col], tab->mat->row[row][off + col]);
1543                 isl_int_fdiv_r(r_row[off + col], r_row[off + col],
1544                                 tab->mat->row[row][0]);
1545                 isl_int_neg(r_row[off + col], r_row[off + col]);
1546         }
1547         for (i = 0; i < tab->n_col; ++i) {
1548                 if (tab->col_var[i] >= 0 &&
1549                     (tab->col_var[i] < tab->n_param ||
1550                      tab->col_var[i] >= tab->n_var - tab->n_div))
1551                         continue;
1552                 isl_int_fdiv_r(r_row[off + i],
1553                         tab->mat->row[row][off + i], tab->mat->row[row][0]);
1554         }
1555         if (tab->var[tab->n_var - tab->n_div + d].is_row) {
1556                 isl_int gcd;
1557                 int d_row = tab->var[tab->n_var - tab->n_div + d].index;
1558                 isl_int_init(gcd);
1559                 isl_int_gcd(gcd, tab->mat->row[d_row][0], r_row[0]);
1560                 isl_int_divexact(r_row[0], r_row[0], gcd);
1561                 isl_int_divexact(gcd, tab->mat->row[d_row][0], gcd);
1562                 isl_seq_combine(r_row + 1, gcd, r_row + 1,
1563                                 r_row[0], tab->mat->row[d_row] + 1,
1564                                 off - 1 + tab->n_col);
1565                 isl_int_mul(r_row[0], r_row[0], tab->mat->row[d_row][0]);
1566                 isl_int_clear(gcd);
1567         } else {
1568                 col = tab->var[tab->n_var - tab->n_div + d].index;
1569                 isl_int_set(r_row[off + col], tab->mat->row[row][0]);
1570         }
1571
1572         tab->con[r].is_nonneg = 1;
1573         isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]);
1574         if (tab->row_sign)
1575                 tab->row_sign[tab->con[r].index] = isl_tab_row_neg;
1576
1577         isl_vec_free(div);
1578
1579         return tab->con[r].index;
1580 error:
1581         isl_tab_free(*context_tab);
1582         *context_tab = NULL;
1583         return -1;
1584 }
1585
1586 /* Construct a tableau for bmap that can be used for computing
1587  * the lexicographic minimum (or maximum) of bmap.
1588  * If not NULL, then dom is the domain where the minimum
1589  * should be computed.  In this case, we set up a parametric
1590  * tableau with row signs (initialized to "unknown").
1591  * If M is set, then the tableau will use a big parameter.
1592  * If max is set, then a maximum should be computed instead of a minimum.
1593  * This means that for each variable x, the tableau will contain the variable
1594  * x' = M - x, rather than x' = M + x.  This in turn means that the coefficient
1595  * of the variables in all constraints are negated prior to adding them
1596  * to the tableau.
1597  */
1598 static struct isl_tab *tab_for_lexmin(struct isl_basic_map *bmap,
1599         struct isl_basic_set *dom, unsigned M, int max)
1600 {
1601         int i;
1602         struct isl_tab *tab;
1603
1604         tab = isl_tab_alloc(bmap->ctx, 2 * bmap->n_eq + bmap->n_ineq + 1,
1605                             isl_basic_map_total_dim(bmap), M);
1606         if (!tab)
1607                 return NULL;
1608
1609         tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
1610         if (dom) {
1611                 tab->n_param = isl_basic_set_total_dim(dom) - dom->n_div;
1612                 tab->n_div = dom->n_div;
1613                 tab->row_sign = isl_calloc_array(bmap->ctx,
1614                                         enum isl_tab_row_sign, tab->mat->n_row);
1615                 if (!tab->row_sign)
1616                         goto error;
1617         }
1618         if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
1619                 return isl_tab_mark_empty(tab);
1620
1621         for (i = tab->n_param; i < tab->n_var - tab->n_div; ++i) {
1622                 tab->var[i].is_nonneg = 1;
1623                 tab->var[i].frozen = 1;
1624         }
1625         for (i = 0; i < bmap->n_eq; ++i) {
1626                 if (max)
1627                         isl_seq_neg(bmap->eq[i] + 1 + tab->n_param,
1628                                     bmap->eq[i] + 1 + tab->n_param,
1629                                     tab->n_var - tab->n_param - tab->n_div);
1630                 tab = add_lexmin_valid_eq(tab, bmap->eq[i]);
1631                 if (max)
1632                         isl_seq_neg(bmap->eq[i] + 1 + tab->n_param,
1633                                     bmap->eq[i] + 1 + tab->n_param,
1634                                     tab->n_var - tab->n_param - tab->n_div);
1635                 if (!tab || tab->empty)
1636                         return tab;
1637         }
1638         for (i = 0; i < bmap->n_ineq; ++i) {
1639                 if (max)
1640                         isl_seq_neg(bmap->ineq[i] + 1 + tab->n_param,
1641                                     bmap->ineq[i] + 1 + tab->n_param,
1642                                     tab->n_var - tab->n_param - tab->n_div);
1643                 tab = add_lexmin_ineq(tab, bmap->ineq[i]);
1644                 if (max)
1645                         isl_seq_neg(bmap->ineq[i] + 1 + tab->n_param,
1646                                     bmap->ineq[i] + 1 + tab->n_param,
1647                                     tab->n_var - tab->n_param - tab->n_div);
1648                 if (!tab || tab->empty)
1649                         return tab;
1650         }
1651         return tab;
1652 error:
1653         isl_tab_free(tab);
1654         return NULL;
1655 }
1656
1657 static struct isl_tab *context_tab_for_lexmin(struct isl_basic_set *bset)
1658 {
1659         struct isl_tab *tab;
1660
1661         bset = isl_basic_set_cow(bset);
1662         if (!bset)
1663                 return NULL;
1664         tab = tab_for_lexmin((struct isl_basic_map *)bset, NULL, 1, 0);
1665         if (!tab)
1666                 goto error;
1667         tab->bset = bset;
1668         tab->n_sample = 0;
1669         tab->n_outside = 0;
1670         tab->samples = isl_mat_alloc(bset->ctx, 1, 1 + tab->n_var);
1671         if (!tab->samples)
1672                 goto error;
1673         return tab;
1674 error:
1675         isl_basic_set_free(bset);
1676         return NULL;
1677 }
1678
1679 /* Construct an isl_sol_map structure for accumulating the solution.
1680  * If track_empty is set, then we also keep track of the parts
1681  * of the context where there is no solution.
1682  * If max is set, then we are solving a maximization, rather than
1683  * a minimization problem, which means that the variables in the
1684  * tableau have value "M - x" rather than "M + x".
1685  */
1686 static struct isl_sol_map *sol_map_init(struct isl_basic_map *bmap,
1687         struct isl_basic_set *dom, int track_empty, int max)
1688 {
1689         struct isl_sol_map *sol_map;
1690         struct isl_tab *context_tab;
1691         int f;
1692
1693         sol_map = isl_calloc_type(bset->ctx, struct isl_sol_map);
1694         if (!sol_map)
1695                 goto error;
1696
1697         sol_map->max = max;
1698         sol_map->sol.add = &sol_map_add_wrap;
1699         sol_map->sol.free = &sol_map_free_wrap;
1700         sol_map->map = isl_map_alloc_dim(isl_basic_map_get_dim(bmap), 1,
1701                                             ISL_MAP_DISJOINT);
1702         if (!sol_map->map)
1703                 goto error;
1704
1705         context_tab = context_tab_for_lexmin(isl_basic_set_copy(dom));
1706         context_tab = restore_lexmin(context_tab);
1707         sol_map->sol.context_tab = context_tab;
1708         f = context_is_feasible(&sol_map->sol);
1709         if (f < 0)
1710                 goto error;
1711
1712         if (track_empty) {
1713                 sol_map->empty = isl_set_alloc_dim(isl_basic_set_get_dim(dom),
1714                                                         1, ISL_SET_DISJOINT);
1715                 if (!sol_map->empty)
1716                         goto error;
1717         }
1718
1719         isl_basic_set_free(dom);
1720         return sol_map;
1721 error:
1722         isl_basic_set_free(dom);
1723         sol_map_free(sol_map);
1724         return NULL;
1725 }
1726
1727 /* For each variable in the context tableau, check if the variable can
1728  * only attain non-negative values.  If so, mark the parameter as non-negative
1729  * in the main tableau.  This allows for a more direct identification of some
1730  * cases of violated constraints.
1731  */
1732 static struct isl_tab *tab_detect_nonnegative_parameters(struct isl_tab *tab,
1733         struct isl_tab *context_tab)
1734 {
1735         int i;
1736         struct isl_tab_undo *snap, *snap2;
1737         struct isl_vec *ineq = NULL;
1738         struct isl_tab_var *var;
1739         int n;
1740
1741         if (context_tab->n_var == 0)
1742                 return tab;
1743
1744         ineq = isl_vec_alloc(tab->mat->ctx, 1 + context_tab->n_var);
1745         if (!ineq)
1746                 goto error;
1747
1748         if (isl_tab_extend_cons(context_tab, 1) < 0)
1749                 goto error;
1750
1751         snap = isl_tab_snap(context_tab);
1752         isl_tab_push_basis(context_tab);
1753
1754         snap2 = isl_tab_snap(context_tab);
1755
1756         n = 0;
1757         isl_seq_clr(ineq->el, ineq->size);
1758         for (i = 0; i < context_tab->n_var; ++i) {
1759                 isl_int_set_si(ineq->el[1 + i], 1);
1760                 context_tab = isl_tab_add_ineq(context_tab, ineq->el);
1761                 var = &context_tab->con[context_tab->n_con - 1];
1762                 if (!context_tab->empty &&
1763                     !isl_tab_min_at_most_neg_one(context_tab, var)) {
1764                         int j = i;
1765                         if (i >= tab->n_param)
1766                                 j = i - tab->n_param + tab->n_var - tab->n_div;
1767                         tab->var[j].is_nonneg = 1;
1768                         n++;
1769                 }
1770                 isl_int_set_si(ineq->el[1 + i], 0);
1771                 if (isl_tab_rollback(context_tab, snap2) < 0)
1772                         goto error;
1773         }
1774
1775         if (isl_tab_rollback(context_tab, snap) < 0)
1776                 goto error;
1777
1778         if (n == context_tab->n_var) {
1779                 context_tab->mat = isl_mat_drop_cols(context_tab->mat, 2, 1);
1780                 context_tab->M = 0;
1781         }
1782
1783         isl_vec_free(ineq);
1784         return tab;
1785 error:
1786         isl_vec_free(ineq);
1787         isl_tab_free(tab);
1788         return NULL;
1789 }
1790
1791 /* Check whether all coefficients of (non-parameter) variables
1792  * are non-positive, meaning that no pivots can be performed on the row.
1793  */
1794 static int is_critical(struct isl_tab *tab, int row)
1795 {
1796         int j;
1797         unsigned off = 2 + tab->M;
1798
1799         for (j = tab->n_dead; j < tab->n_col; ++j) {
1800                 if (tab->col_var[j] >= 0 &&
1801                     (tab->col_var[j] < tab->n_param  ||
1802                     tab->col_var[j] >= tab->n_var - tab->n_div))
1803                         continue;
1804
1805                 if (isl_int_is_pos(tab->mat->row[row][off + j]))
1806                         return 0;
1807         }
1808
1809         return 1;
1810 }
1811
1812 /* Check whether the inequality represented by vec is strict over the integers,
1813  * i.e., there are no integer values satisfying the constraint with
1814  * equality.  This happens if the gcd of the coefficients is not a divisor
1815  * of the constant term.  If so, scale the constraint down by the gcd
1816  * of the coefficients.
1817  */
1818 static int is_strict(struct isl_vec *vec)
1819 {
1820         isl_int gcd;
1821         int strict = 0;
1822
1823         isl_int_init(gcd);
1824         isl_seq_gcd(vec->el + 1, vec->size - 1, &gcd);
1825         if (!isl_int_is_one(gcd)) {
1826                 strict = !isl_int_is_divisible_by(vec->el[0], gcd);
1827                 isl_int_fdiv_q(vec->el[0], vec->el[0], gcd);
1828                 isl_seq_scale_down(vec->el + 1, vec->el + 1, gcd, vec->size-1);
1829         }
1830         isl_int_clear(gcd);
1831
1832         return strict;
1833 }
1834
1835 /* Determine the sign of the given row of the main tableau.
1836  * The result is one of
1837  *      isl_tab_row_pos: always non-negative; no pivot needed
1838  *      isl_tab_row_neg: always non-positive; pivot
1839  *      isl_tab_row_any: can be both positive and negative; split
1840  *
1841  * We first handle some simple cases
1842  *      - the row sign may be known already
1843  *      - the row may be obviously non-negative
1844  *      - the parametric constant may be equal to that of another row
1845  *        for which we know the sign.  This sign will be either "pos" or
1846  *        "any".  If it had been "neg" then we would have pivoted before.
1847  *
1848  * If none of these cases hold, we check the value of the row for each
1849  * of the currently active samples.  Based on the signs of these values
1850  * we make an initial determination of the sign of the row.
1851  *
1852  *      all zero                        ->      unk(nown)
1853  *      all non-negative                ->      pos
1854  *      all non-positive                ->      neg
1855  *      both negative and positive      ->      all
1856  *
1857  * If we end up with "all", we are done.
1858  * Otherwise, we perform a check for positive and/or negative
1859  * values as follows.
1860  *
1861  *      samples        neg             unk             pos
1862  *      <0 ?                        Y        N      Y        N
1863  *                                          pos    any      pos
1864  *      >0 ?         Y      N    Y     N
1865  *                  any    neg  any   neg
1866  *
1867  * There is no special sign for "zero", because we can usually treat zero
1868  * as either non-negative or non-positive, whatever works out best.
1869  * However, if the row is "critical", meaning that pivoting is impossible
1870  * then we don't want to limp zero with the non-positive case, because
1871  * then we we would lose the solution for those values of the parameters
1872  * where the value of the row is zero.  Instead, we treat 0 as non-negative
1873  * ensuring a split if the row can attain both zero and negative values.
1874  * The same happens when the original constraint was one that could not
1875  * be satisfied with equality by any integer values of the parameters.
1876  * In this case, we normalize the constraint, but then a value of zero
1877  * for the normalized constraint is actually a positive value for the
1878  * original constraint, so again we need to treat zero as non-negative.
1879  * In both these cases, we have the following decision tree instead:
1880  *
1881  *      all non-negative                ->      pos
1882  *      all negative                    ->      neg
1883  *      both negative and non-negative  ->      all
1884  *
1885  *      samples        neg                             pos
1886  *      <0 ?                                        Y        N
1887  *                                                 any      pos
1888  *      >=0 ?        Y      N
1889  *                  any    neg
1890  */
1891 static int row_sign(struct isl_tab *tab, struct isl_sol *sol, int row)
1892 {
1893         int i;
1894         struct isl_tab_undo *snap = NULL;
1895         struct isl_vec *ineq = NULL;
1896         int res = isl_tab_row_unknown;
1897         int r;
1898         int context_row;
1899         int critical;
1900         int strict;
1901         int sgn;
1902         int row2;
1903         isl_int tmp;
1904         struct isl_tab *context_tab = sol->context_tab;
1905
1906         if (tab->row_sign[row] != isl_tab_row_unknown)
1907                 return tab->row_sign[row];
1908         if (is_obviously_nonneg(tab, row))
1909                 return isl_tab_row_pos;
1910         for (row2 = tab->n_redundant; row2 < tab->n_row; ++row2) {
1911                 if (tab->row_sign[row2] == isl_tab_row_unknown)
1912                         continue;
1913                 if (identical_parameter_line(tab, row, row2))
1914                         return tab->row_sign[row2];
1915         }
1916
1917         critical = is_critical(tab, row);
1918
1919         isl_assert(tab->mat->ctx, context_tab->samples, goto error);
1920         isl_assert(tab->mat->ctx, context_tab->samples->n_col == 1 + context_tab->n_var, goto error);
1921
1922         ineq = get_row_parameter_ineq(tab, row);
1923         if (!ineq)
1924                 goto error;
1925
1926         strict = is_strict(ineq);
1927
1928         isl_int_init(tmp);
1929         for (i = context_tab->n_outside; i < context_tab->n_sample; ++i) {
1930                 isl_seq_inner_product(context_tab->samples->row[i], ineq->el,
1931                                         ineq->size, &tmp);
1932                 sgn = isl_int_sgn(tmp);
1933                 if (sgn > 0 || (sgn == 0 && (critical || strict))) {
1934                         if (res == isl_tab_row_unknown)
1935                                 res = isl_tab_row_pos;
1936                         if (res == isl_tab_row_neg)
1937                                 res = isl_tab_row_any;
1938                 }
1939                 if (sgn < 0) {
1940                         if (res == isl_tab_row_unknown)
1941                                 res = isl_tab_row_neg;
1942                         if (res == isl_tab_row_pos)
1943                                 res = isl_tab_row_any;
1944                 }
1945                 if (res == isl_tab_row_any)
1946                         break;
1947         }
1948         isl_int_clear(tmp);
1949
1950         if (res != isl_tab_row_any) {
1951                 if (isl_tab_extend_cons(context_tab, 1) < 0)
1952                         goto error;
1953
1954                 snap = isl_tab_snap(context_tab);
1955                 isl_tab_push_basis(context_tab);
1956         }
1957
1958         if (res == isl_tab_row_unknown || res == isl_tab_row_pos) {
1959                 /* test for negative values */
1960                 int feasible;
1961                 isl_seq_neg(ineq->el, ineq->el, ineq->size);
1962                 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
1963
1964                 isl_tab_push_basis(context_tab);
1965                 sol->context_tab = add_lexmin_ineq(sol->context_tab, ineq->el);
1966                 feasible = context_is_feasible(sol);
1967                 if (feasible < 0)
1968                         goto error;
1969                 context_tab = sol->context_tab;
1970                 if (!feasible)
1971                         res = isl_tab_row_pos;
1972                 else
1973                         res = (res == isl_tab_row_unknown) ? isl_tab_row_neg
1974                                                            : isl_tab_row_any;
1975                 if (isl_tab_rollback(context_tab, snap) < 0)
1976                         goto error;
1977
1978                 if (res == isl_tab_row_neg) {
1979                         isl_seq_neg(ineq->el, ineq->el, ineq->size);
1980                         isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
1981                 }
1982         }
1983
1984         if (res == isl_tab_row_neg) {
1985                 /* test for positive values */
1986                 int feasible;
1987                 if (!critical && !strict)
1988                         isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
1989
1990                 isl_tab_push_basis(context_tab);
1991                 sol->context_tab = add_lexmin_ineq(sol->context_tab, ineq->el);
1992                 feasible = context_is_feasible(sol);
1993                 if (feasible < 0)
1994                         goto error;
1995                 context_tab = sol->context_tab;
1996                 if (feasible)
1997                         res = isl_tab_row_any;
1998                 if (isl_tab_rollback(context_tab, snap) < 0)
1999                         goto error;
2000         }
2001
2002         isl_vec_free(ineq);
2003         return res;
2004 error:
2005         isl_vec_free(ineq);
2006         return 0;
2007 }
2008
2009 static struct isl_sol *find_solutions(struct isl_sol *sol, struct isl_tab *tab);
2010
2011 /* Find solutions for values of the parameters that satisfy the given
2012  * inequality.
2013  *
2014  * We currently take a snapshot of the context tableau that is reset
2015  * when we return from this function, while we make a copy of the main
2016  * tableau, leaving the original main tableau untouched.
2017  * These are fairly arbitrary choices.  Making a copy also of the context
2018  * tableau would obviate the need to undo any changes made to it later,
2019  * while taking a snapshot of the main tableau could reduce memory usage.
2020  * If we were to switch to taking a snapshot of the main tableau,
2021  * we would have to keep in mind that we need to save the row signs
2022  * and that we need to do this before saving the current basis
2023  * such that the basis has been restore before we restore the row signs.
2024  */
2025 static struct isl_sol *find_in_pos(struct isl_sol *sol,
2026         struct isl_tab *tab, isl_int *ineq)
2027 {
2028         struct isl_tab_undo *snap;
2029
2030         snap = isl_tab_snap(sol->context_tab);
2031         isl_tab_push_basis(sol->context_tab);
2032         if (isl_tab_extend_cons(sol->context_tab, 1) < 0)
2033                 goto error;
2034
2035         tab = isl_tab_dup(tab);
2036         if (!tab)
2037                 goto error;
2038
2039         sol->context_tab = add_lexmin_ineq(sol->context_tab, ineq);
2040         sol->context_tab = check_samples(sol->context_tab, ineq, 0);
2041
2042         sol = find_solutions(sol, tab);
2043
2044         isl_tab_rollback(sol->context_tab, snap);
2045         return sol;
2046 error:
2047         isl_tab_rollback(sol->context_tab, snap);
2048         sol_free(sol);
2049         return NULL;
2050 }
2051
2052 /* Record the absence of solutions for those values of the parameters
2053  * that do not satisfy the given inequality with equality.
2054  */
2055 static struct isl_sol *no_sol_in_strict(struct isl_sol *sol,
2056         struct isl_tab *tab, struct isl_vec *ineq)
2057 {
2058         int empty;
2059         int f;
2060         struct isl_tab_undo *snap;
2061         snap = isl_tab_snap(sol->context_tab);
2062         isl_tab_push_basis(sol->context_tab);
2063         if (isl_tab_extend_cons(sol->context_tab, 1) < 0)
2064                 goto error;
2065
2066         isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
2067
2068         sol->context_tab = add_lexmin_ineq(sol->context_tab, ineq->el);
2069         f = context_valid_sample_or_feasible(sol, ineq->el, 0);
2070         if (f < 0)
2071                 goto error;
2072
2073         empty = tab->empty;
2074         tab->empty = 1;
2075         sol = sol->add(sol, tab);
2076         tab->empty = empty;
2077
2078         isl_int_add_ui(ineq->el[0], ineq->el[0], 1);
2079
2080         if (isl_tab_rollback(sol->context_tab, snap) < 0)
2081                 goto error;
2082         return sol;
2083 error:
2084         sol_free(sol);
2085         return NULL;
2086 }
2087
2088 /* Given a main tableau where more than one row requires a split,
2089  * determine and return the "best" row to split on.
2090  *
2091  * Given two rows in the main tableau, if the inequality corresponding
2092  * to the first row is redundant with respect to that of the second row
2093  * in the current tableau, then it is better to split on the second row,
2094  * since in the positive part, both row will be positive.
2095  * (In the negative part a pivot will have to be performed and just about
2096  * anything can happen to the sign of the other row.)
2097  *
2098  * As a simple heuristic, we therefore select the row that makes the most
2099  * of the other rows redundant.
2100  *
2101  * Perhaps it would also be useful to look at the number of constraints
2102  * that conflict with any given constraint.
2103  */
2104 static int best_split(struct isl_tab *tab, struct isl_tab *context_tab)
2105 {
2106         struct isl_tab_undo *snap, *snap2;
2107         int split;
2108         int row;
2109         int best = -1;
2110         int best_r;
2111
2112         if (isl_tab_extend_cons(context_tab, 2) < 0)
2113                 return -1;
2114
2115         snap = isl_tab_snap(context_tab);
2116         isl_tab_push_basis(context_tab);
2117         snap2 = isl_tab_snap(context_tab);
2118
2119         for (split = tab->n_redundant; split < tab->n_row; ++split) {
2120                 struct isl_tab_undo *snap3;
2121                 struct isl_vec *ineq = NULL;
2122                 int r = 0;
2123
2124                 if (!isl_tab_var_from_row(tab, split)->is_nonneg)
2125                         continue;
2126                 if (tab->row_sign[split] != isl_tab_row_any)
2127                         continue;
2128
2129                 ineq = get_row_parameter_ineq(tab, split);
2130                 if (!ineq)
2131                         return -1;
2132                 context_tab = isl_tab_add_ineq(context_tab, ineq->el);
2133                 isl_vec_free(ineq);
2134
2135                 snap3 = isl_tab_snap(context_tab);
2136
2137                 for (row = tab->n_redundant; row < tab->n_row; ++row) {
2138                         struct isl_tab_var *var;
2139
2140                         if (row == split)
2141                                 continue;
2142                         if (!isl_tab_var_from_row(tab, row)->is_nonneg)
2143                                 continue;
2144                         if (tab->row_sign[row] != isl_tab_row_any)
2145                                 continue;
2146
2147                         ineq = get_row_parameter_ineq(tab, row);
2148                         if (!ineq)
2149                                 return -1;
2150                         context_tab = isl_tab_add_ineq(context_tab, ineq->el);
2151                         isl_vec_free(ineq);
2152                         var = &context_tab->con[context_tab->n_con - 1];
2153                         if (!context_tab->empty &&
2154                             !isl_tab_min_at_most_neg_one(context_tab, var))
2155                                 r++;
2156                         if (isl_tab_rollback(context_tab, snap3) < 0)
2157                                 return -1;
2158                 }
2159                 if (best == -1 || r > best_r) {
2160                         best = split;
2161                         best_r = r;
2162                 }
2163                 if (isl_tab_rollback(context_tab, snap2) < 0)
2164                         return -1;
2165         }
2166
2167         if (isl_tab_rollback(context_tab, snap) < 0)
2168                 return -1;
2169
2170         return best;
2171 }
2172
2173 /* Compute the lexicographic minimum of the set represented by the main
2174  * tableau "tab" within the context "sol->context_tab".
2175  * On entry the sample value of the main tableau is lexicographically
2176  * less than or equal to this lexicographic minimum.
2177  * Pivots are performed until a feasible point is found, which is then
2178  * necessarily equal to the minimum, or until the tableau is found to
2179  * be infeasible.  Some pivots may need to be performed for only some
2180  * feasible values of the context tableau.  If so, the context tableau
2181  * is split into a part where the pivot is needed and a part where it is not.
2182  *
2183  * Whenever we enter the main loop, the main tableau is such that no
2184  * "obvious" pivots need to be performed on it, where "obvious" means
2185  * that the given row can be seen to be negative without looking at
2186  * the context tableau.  In particular, for non-parametric problems,
2187  * no pivots need to be performed on the main tableau.
2188  * The caller of find_solutions is responsible for making this property
2189  * hold prior to the first iteration of the loop, while restore_lexmin
2190  * is called before every other iteration.
2191  *
2192  * Inside the main loop, we first examine the signs of the rows of
2193  * the main tableau within the context of the context tableau.
2194  * If we find a row that is always non-positive for all values of
2195  * the parameters satisfying the context tableau and negative for at
2196  * least one value of the parameters, we perform the appropriate pivot
2197  * and start over.  An exception is the case where no pivot can be
2198  * performed on the row.  In this case, we require that the sign of
2199  * the row is negative for all values of the parameters (rather than just
2200  * non-positive).  This special case is handled inside row_sign, which
2201  * will say that the row can have any sign if it determines that it can
2202  * attain both negative and zero values.
2203  *
2204  * If we can't find a row that always requires a pivot, but we can find
2205  * one or more rows that require a pivot for some values of the parameters
2206  * (i.e., the row can attain both positive and negative signs), then we split
2207  * the context tableau into two parts, one where we force the sign to be
2208  * non-negative and one where we force is to be negative.
2209  * The non-negative part is handled by a recursive call (through find_in_pos).
2210  * Upon returning from this call, we continue with the negative part and
2211  * perform the required pivot.
2212  *
2213  * If no such rows can be found, all rows are non-negative and we have
2214  * found a (rational) feasible point.  If we only wanted a rational point
2215  * then we are done.
2216  * Otherwise, we check if all values of the sample point of the tableau
2217  * are integral for the variables.  If so, we have found the minimal
2218  * integral point and we are done.
2219  * If the sample point is not integral, then we need to make a distinction
2220  * based on whether the constant term is non-integral or the coefficients
2221  * of the parameters.  Furthermore, in order to decide how to handle
2222  * the non-integrality, we also need to know whether the coefficients
2223  * of the other columns in the tableau are integral.  This leads
2224  * to the following table.  The first two rows do not correspond
2225  * to a non-integral sample point and are only mentioned for completeness.
2226  *
2227  *      constant        parameters      other
2228  *
2229  *      int             int             int     |
2230  *      int             int             rat     | -> no problem
2231  *
2232  *      rat             int             int       -> fail
2233  *
2234  *      rat             int             rat       -> cut
2235  *
2236  *      int             rat             rat     |
2237  *      rat             rat             rat     | -> parametric cut
2238  *
2239  *      int             rat             int     |
2240  *      rat             rat             int     | -> split context
2241  *
2242  * If the parametric constant is completely integral, then there is nothing
2243  * to be done.  If the constant term is non-integral, but all the other
2244  * coefficient are integral, then there is nothing that can be done
2245  * and the tableau has no integral solution.
2246  * If, on the other hand, one or more of the other columns have rational
2247  * coeffcients, but the parameter coefficients are all integral, then
2248  * we can perform a regular (non-parametric) cut.
2249  * Finally, if there is any parameter coefficient that is non-integral,
2250  * then we need to involve the context tableau.  There are two cases here.
2251  * If at least one other column has a rational coefficient, then we
2252  * can perform a parametric cut in the main tableau by adding a new
2253  * integer division in the context tableau.
2254  * If all other columns have integral coefficients, then we need to
2255  * enforce that the rational combination of parameters (c + \sum a_i y_i)/m
2256  * is always integral.  We do this by introducing an integer division
2257  * q = floor((c + \sum a_i y_i)/m) and stipulating that its argument should
2258  * always be integral in the context tableau, i.e., m q = c + \sum a_i y_i.
2259  * Since q is expressed in the tableau as
2260  *      c + \sum a_i y_i - m q >= 0
2261  *      -c - \sum a_i y_i + m q + m - 1 >= 0
2262  * it is sufficient to add the inequality
2263  *      -c - \sum a_i y_i + m q >= 0
2264  * In the part of the context where this inequality does not hold, the
2265  * main tableau is marked as being empty.
2266  */
2267 static struct isl_sol *find_solutions(struct isl_sol *sol, struct isl_tab *tab)
2268 {
2269         struct isl_tab **context_tab;
2270
2271         if (!tab || !sol)
2272                 goto error;
2273
2274         context_tab = &sol->context_tab;
2275
2276         if (tab->empty)
2277                 goto done;
2278         if ((*context_tab)->empty)
2279                 goto done;
2280
2281         for (; tab && !tab->empty; tab = restore_lexmin(tab)) {
2282                 int flags;
2283                 int row;
2284                 int sgn;
2285                 int split = -1;
2286                 int n_split = 0;
2287
2288                 for (row = tab->n_redundant; row < tab->n_row; ++row) {
2289                         if (!isl_tab_var_from_row(tab, row)->is_nonneg)
2290                                 continue;
2291                         sgn = row_sign(tab, sol, row);
2292                         if (!sgn)
2293                                 goto error;
2294                         tab->row_sign[row] = sgn;
2295                         if (sgn == isl_tab_row_any)
2296                                 n_split++;
2297                         if (sgn == isl_tab_row_any && split == -1)
2298                                 split = row;
2299                         if (sgn == isl_tab_row_neg)
2300                                 break;
2301                 }
2302                 if (row < tab->n_row)
2303                         continue;
2304                 if (split != -1) {
2305                         struct isl_vec *ineq;
2306                         if (n_split != 1)
2307                                 split = best_split(tab, *context_tab);
2308                         if (split < 0)
2309                                 goto error;
2310                         ineq = get_row_parameter_ineq(tab, split);
2311                         if (!ineq)
2312                                 goto error;
2313                         is_strict(ineq);
2314                         for (row = tab->n_redundant; row < tab->n_row; ++row) {
2315                                 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
2316                                         continue;
2317                                 if (tab->row_sign[row] == isl_tab_row_any)
2318                                         tab->row_sign[row] = isl_tab_row_unknown;
2319                         }
2320                         tab->row_sign[split] = isl_tab_row_pos;
2321                         sol = find_in_pos(sol, tab, ineq->el);
2322                         tab->row_sign[split] = isl_tab_row_neg;
2323                         row = split;
2324                         isl_seq_neg(ineq->el, ineq->el, ineq->size);
2325                         isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
2326                         *context_tab = add_lexmin_ineq(*context_tab, ineq->el);
2327                         *context_tab = check_samples(*context_tab, ineq->el, 0);
2328                         isl_vec_free(ineq);
2329                         if (!sol)
2330                                 goto error;
2331                         continue;
2332                 }
2333                 if (tab->rational)
2334                         break;
2335                 row = first_non_integer(tab, &flags);
2336                 if (row < 0)
2337                         break;
2338                 if (ISL_FL_ISSET(flags, I_PAR)) {
2339                         if (ISL_FL_ISSET(flags, I_VAR)) {
2340                                 tab = isl_tab_mark_empty(tab);
2341                                 break;
2342                         }
2343                         row = add_cut(tab, row);
2344                 } else if (ISL_FL_ISSET(flags, I_VAR)) {
2345                         struct isl_vec *div;
2346                         struct isl_vec *ineq;
2347                         int d;
2348                         if (isl_tab_extend_cons(*context_tab, 3) < 0)
2349                                 goto error;
2350                         div = get_row_split_div(tab, row);
2351                         if (!div)
2352                                 goto error;
2353                         d = get_div(tab, context_tab, div);
2354                         isl_vec_free(div);
2355                         if (d < 0)
2356                                 goto error;
2357                         ineq = ineq_for_div((*context_tab)->bset, d);
2358                         sol = no_sol_in_strict(sol, tab, ineq);
2359                         isl_seq_neg(ineq->el, ineq->el, ineq->size);
2360                         *context_tab = add_lexmin_ineq(*context_tab, ineq->el);
2361                         *context_tab = check_samples(*context_tab, ineq->el, 0);
2362                         isl_vec_free(ineq);
2363                         if (!sol)
2364                                 goto error;
2365                         tab = set_row_cst_to_div(tab, row, d);
2366                 } else
2367                         row = add_parametric_cut(tab, row, context_tab);
2368                 if (row < 0)
2369                         goto error;
2370         }
2371 done:
2372         sol = sol->add(sol, tab);
2373         isl_tab_free(tab);
2374         return sol;
2375 error:
2376         isl_tab_free(tab);
2377         sol_free(sol);
2378         return NULL;
2379 }
2380
2381 /* Compute the lexicographic minimum of the set represented by the main
2382  * tableau "tab" within the context "sol->context_tab".
2383  *
2384  * As a preprocessing step, we first transfer all the purely parametric
2385  * equalities from the main tableau to the context tableau, i.e.,
2386  * parameters that have been pivoted to a row.
2387  * These equalities are ignored by the main algorithm, because the
2388  * corresponding rows may not be marked as being non-negative.
2389  * In parts of the context where the added equality does not hold,
2390  * the main tableau is marked as being empty.
2391  */
2392 static struct isl_sol *find_solutions_main(struct isl_sol *sol,
2393         struct isl_tab *tab)
2394 {
2395         int row;
2396
2397         for (row = tab->n_redundant; row < tab->n_row; ++row) {
2398                 int p;
2399                 struct isl_vec *eq;
2400
2401                 if (tab->row_var[row] < 0)
2402                         continue;
2403                 if (tab->row_var[row] >= tab->n_param &&
2404                     tab->row_var[row] < tab->n_var - tab->n_div)
2405                         continue;
2406                 if (tab->row_var[row] < tab->n_param)
2407                         p = tab->row_var[row];
2408                 else
2409                         p = tab->row_var[row]
2410                                 + tab->n_param - (tab->n_var - tab->n_div);
2411
2412                 if (isl_tab_extend_cons(sol->context_tab, 2) < 0)
2413                         goto error;
2414
2415                 eq = isl_vec_alloc(tab->mat->ctx, 1+tab->n_param+tab->n_div);
2416                 get_row_parameter_line(tab, row, eq->el);
2417                 isl_int_neg(eq->el[1 + p], tab->mat->row[row][0]);
2418                 eq = isl_vec_normalize(eq);
2419
2420                 sol = no_sol_in_strict(sol, tab, eq);
2421
2422                 isl_seq_neg(eq->el, eq->el, eq->size);
2423                 sol = no_sol_in_strict(sol, tab, eq);
2424                 isl_seq_neg(eq->el, eq->el, eq->size);
2425
2426                 sol->context_tab = add_lexmin_eq(sol->context_tab, eq->el);
2427                 context_valid_sample_or_feasible(sol, eq->el, 1);
2428                 sol->context_tab = check_samples(sol->context_tab, eq->el, 1);
2429
2430                 isl_vec_free(eq);
2431
2432                 isl_tab_mark_redundant(tab, row);
2433
2434                 if (!sol->context_tab)
2435                         goto error;
2436                 if (sol->context_tab->empty)
2437                         break;
2438
2439                 row = tab->n_redundant - 1;
2440         }
2441
2442         return find_solutions(sol, tab);
2443 error:
2444         isl_tab_free(tab);
2445         sol_free(sol);
2446         return NULL;
2447 }
2448
2449 static struct isl_sol_map *sol_map_find_solutions(struct isl_sol_map *sol_map,
2450         struct isl_tab *tab)
2451 {
2452         return (struct isl_sol_map *)find_solutions_main(&sol_map->sol, tab);
2453 }
2454
2455 /* Check if integer division "div" of "dom" also occurs in "bmap".
2456  * If so, return its position within the divs.
2457  * If not, return -1.
2458  */
2459 static int find_context_div(struct isl_basic_map *bmap,
2460         struct isl_basic_set *dom, unsigned div)
2461 {
2462         int i;
2463         unsigned b_dim = isl_dim_total(bmap->dim);
2464         unsigned d_dim = isl_dim_total(dom->dim);
2465
2466         if (isl_int_is_zero(dom->div[div][0]))
2467                 return -1;
2468         if (isl_seq_first_non_zero(dom->div[div] + 2 + d_dim, dom->n_div) != -1)
2469                 return -1;
2470
2471         for (i = 0; i < bmap->n_div; ++i) {
2472                 if (isl_int_is_zero(bmap->div[i][0]))
2473                         continue;
2474                 if (isl_seq_first_non_zero(bmap->div[i] + 2 + d_dim,
2475                                            (b_dim - d_dim) + bmap->n_div) != -1)
2476                         continue;
2477                 if (isl_seq_eq(bmap->div[i], dom->div[div], 2 + d_dim))
2478                         return i;
2479         }
2480         return -1;
2481 }
2482
2483 /* The correspondence between the variables in the main tableau,
2484  * the context tableau, and the input map and domain is as follows.
2485  * The first n_param and the last n_div variables of the main tableau
2486  * form the variables of the context tableau.
2487  * In the basic map, these n_param variables correspond to the
2488  * parameters and the input dimensions.  In the domain, they correspond
2489  * to the parameters and the set dimensions.
2490  * The n_div variables correspond to the integer divisions in the domain.
2491  * To ensure that everything lines up, we may need to copy some of the
2492  * integer divisions of the domain to the map.  These have to be placed
2493  * in the same order as those in the context and they have to be placed
2494  * after any other integer divisions that the map may have.
2495  * This function performs the required reordering.
2496  */
2497 static struct isl_basic_map *align_context_divs(struct isl_basic_map *bmap,
2498         struct isl_basic_set *dom)
2499 {
2500         int i;
2501         int common = 0;
2502         int other;
2503
2504         for (i = 0; i < dom->n_div; ++i)
2505                 if (find_context_div(bmap, dom, i) != -1)
2506                         common++;
2507         other = bmap->n_div - common;
2508         if (dom->n_div - common > 0) {
2509                 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
2510                                 dom->n_div - common, 0, 0);
2511                 if (!bmap)
2512                         return NULL;
2513         }
2514         for (i = 0; i < dom->n_div; ++i) {
2515                 int pos = find_context_div(bmap, dom, i);
2516                 if (pos < 0) {
2517                         pos = isl_basic_map_alloc_div(bmap);
2518                         if (pos < 0)
2519                                 goto error;
2520                         isl_int_set_si(bmap->div[pos][0], 0);
2521                 }
2522                 if (pos != other + i)
2523                         isl_basic_map_swap_div(bmap, pos, other + i);
2524         }
2525         return bmap;
2526 error:
2527         isl_basic_map_free(bmap);
2528         return NULL;
2529 }
2530
2531 /* Compute the lexicographic minimum (or maximum if "max" is set)
2532  * of "bmap" over the domain "dom" and return the result as a map.
2533  * If "empty" is not NULL, then *empty is assigned a set that
2534  * contains those parts of the domain where there is no solution.
2535  * If "bmap" is marked as rational (ISL_BASIC_MAP_RATIONAL),
2536  * then we compute the rational optimum.  Otherwise, we compute
2537  * the integral optimum.
2538  *
2539  * We perform some preprocessing.  As the PILP solver does not
2540  * handle implicit equalities very well, we first make sure all
2541  * the equalities are explicitly available.
2542  * We also make sure the divs in the domain are properly order,
2543  * because they will be added one by one in the given order
2544  * during the construction of the solution map.
2545  */
2546 struct isl_map *isl_tab_basic_map_partial_lexopt(
2547                 struct isl_basic_map *bmap, struct isl_basic_set *dom,
2548                 struct isl_set **empty, int max)
2549 {
2550         struct isl_tab *tab;
2551         struct isl_map *result = NULL;
2552         struct isl_sol_map *sol_map = NULL;
2553
2554         if (empty)
2555                 *empty = NULL;
2556         if (!bmap || !dom)
2557                 goto error;
2558
2559         isl_assert(bmap->ctx,
2560             isl_basic_map_compatible_domain(bmap, dom), goto error);
2561
2562         bmap = isl_basic_map_detect_equalities(bmap);
2563
2564         if (dom->n_div) {
2565                 dom = isl_basic_set_order_divs(dom);
2566                 bmap = align_context_divs(bmap, dom);
2567         }
2568         sol_map = sol_map_init(bmap, dom, !!empty, max);
2569         if (!sol_map)
2570                 goto error;
2571
2572         if (isl_basic_set_fast_is_empty(sol_map->sol.context_tab->bset))
2573                 /* nothing */;
2574         else if (isl_basic_map_fast_is_empty(bmap))
2575                 sol_map = add_empty(sol_map);
2576         else {
2577                 tab = tab_for_lexmin(bmap,
2578                                         sol_map->sol.context_tab->bset, 1, max);
2579                 tab = tab_detect_nonnegative_parameters(tab,
2580                                                 sol_map->sol.context_tab);
2581                 sol_map = sol_map_find_solutions(sol_map, tab);
2582                 if (!sol_map)
2583                         goto error;
2584         }
2585
2586         result = isl_map_copy(sol_map->map);
2587         if (empty)
2588                 *empty = isl_set_copy(sol_map->empty);
2589         sol_map_free(sol_map);
2590         isl_basic_map_free(bmap);
2591         return result;
2592 error:
2593         sol_map_free(sol_map);
2594         isl_basic_map_free(bmap);
2595         return NULL;
2596 }