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