isl_tab_pip.c: use_shifted: avoid invalid access on error path
[platform/upstream/isl.git] / isl_tab_pip.c
1 /*
2  * Copyright 2008-2009 Katholieke Universiteit Leuven
3  * Copyright 2010      INRIA Saclay
4  *
5  * Use of this software is governed by the MIT license
6  *
7  * Written by Sven Verdoolaege, K.U.Leuven, Departement
8  * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9  * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
10  * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France 
11  */
12
13 #include <isl_ctx_private.h>
14 #include "isl_map_private.h"
15 #include <isl/seq.h>
16 #include "isl_tab.h"
17 #include "isl_sample.h"
18 #include <isl_mat_private.h>
19 #include <isl_aff_private.h>
20 #include <isl_options_private.h>
21 #include <isl_config.h>
22
23 /*
24  * The implementation of parametric integer linear programming in this file
25  * was inspired by the paper "Parametric Integer Programming" and the
26  * report "Solving systems of affine (in)equalities" by Paul Feautrier
27  * (and others).
28  *
29  * The strategy used for obtaining a feasible solution is different
30  * from the one used in isl_tab.c.  In particular, in isl_tab.c,
31  * upon finding a constraint that is not yet satisfied, we pivot
32  * in a row that increases the constant term of the row holding the
33  * constraint, making sure the sample solution remains feasible
34  * for all the constraints it already satisfied.
35  * Here, we always pivot in the row holding the constraint,
36  * choosing a column that induces the lexicographically smallest
37  * increment to the sample solution.
38  *
39  * By starting out from a sample value that is lexicographically
40  * smaller than any integer point in the problem space, the first
41  * feasible integer sample point we find will also be the lexicographically
42  * smallest.  If all variables can be assumed to be non-negative,
43  * then the initial sample value may be chosen equal to zero.
44  * However, we will not make this assumption.  Instead, we apply
45  * the "big parameter" trick.  Any variable x is then not directly
46  * used in the tableau, but instead it is represented by another
47  * variable x' = M + x, where M is an arbitrarily large (positive)
48  * value.  x' is therefore always non-negative, whatever the value of x.
49  * Taking as initial sample value x' = 0 corresponds to x = -M,
50  * which is always smaller than any possible value of x.
51  *
52  * The big parameter trick is used in the main tableau and
53  * also in the context tableau if isl_context_lex is used.
54  * In this case, each tableaus has its own big parameter.
55  * Before doing any real work, we check if all the parameters
56  * happen to be non-negative.  If so, we drop the column corresponding
57  * to M from the initial context tableau.
58  * If isl_context_gbr is used, then the big parameter trick is only
59  * used in the main tableau.
60  */
61
62 struct isl_context;
63 struct isl_context_op {
64         /* detect nonnegative parameters in context and mark them in tab */
65         struct isl_tab *(*detect_nonnegative_parameters)(
66                         struct isl_context *context, struct isl_tab *tab);
67         /* return temporary reference to basic set representation of context */
68         struct isl_basic_set *(*peek_basic_set)(struct isl_context *context);
69         /* return temporary reference to tableau representation of context */
70         struct isl_tab *(*peek_tab)(struct isl_context *context);
71         /* add equality; check is 1 if eq may not be valid;
72          * update is 1 if we may want to call ineq_sign on context later.
73          */
74         void (*add_eq)(struct isl_context *context, isl_int *eq,
75                         int check, int update);
76         /* add inequality; check is 1 if ineq may not be valid;
77          * update is 1 if we may want to call ineq_sign on context later.
78          */
79         void (*add_ineq)(struct isl_context *context, isl_int *ineq,
80                         int check, int update);
81         /* check sign of ineq based on previous information.
82          * strict is 1 if saturation should be treated as a positive sign.
83          */
84         enum isl_tab_row_sign (*ineq_sign)(struct isl_context *context,
85                         isl_int *ineq, int strict);
86         /* check if inequality maintains feasibility */
87         int (*test_ineq)(struct isl_context *context, isl_int *ineq);
88         /* return index of a div that corresponds to "div" */
89         int (*get_div)(struct isl_context *context, struct isl_tab *tab,
90                         struct isl_vec *div);
91         /* add div "div" to context and return non-negativity */
92         int (*add_div)(struct isl_context *context, struct isl_vec *div);
93         int (*detect_equalities)(struct isl_context *context,
94                         struct isl_tab *tab);
95         /* return row index of "best" split */
96         int (*best_split)(struct isl_context *context, struct isl_tab *tab);
97         /* check if context has already been determined to be empty */
98         int (*is_empty)(struct isl_context *context);
99         /* check if context is still usable */
100         int (*is_ok)(struct isl_context *context);
101         /* save a copy/snapshot of context */
102         void *(*save)(struct isl_context *context);
103         /* restore saved context */
104         void (*restore)(struct isl_context *context, void *);
105         /* discard saved context */
106         void (*discard)(void *);
107         /* invalidate context */
108         void (*invalidate)(struct isl_context *context);
109         /* free context */
110         void (*free)(struct isl_context *context);
111 };
112
113 struct isl_context {
114         struct isl_context_op *op;
115 };
116
117 struct isl_context_lex {
118         struct isl_context context;
119         struct isl_tab *tab;
120 };
121
122 /* A stack (linked list) of solutions of subtrees of the search space.
123  *
124  * "M" describes the solution in terms of the dimensions of "dom".
125  * The number of columns of "M" is one more than the total number
126  * of dimensions of "dom".
127  *
128  * If "M" is NULL, then there is no solution on "dom".
129  */
130 struct isl_partial_sol {
131         int level;
132         struct isl_basic_set *dom;
133         struct isl_mat *M;
134
135         struct isl_partial_sol *next;
136 };
137
138 struct isl_sol;
139 struct isl_sol_callback {
140         struct isl_tab_callback callback;
141         struct isl_sol *sol;
142 };
143
144 /* isl_sol is an interface for constructing a solution to
145  * a parametric integer linear programming problem.
146  * Every time the algorithm reaches a state where a solution
147  * can be read off from the tableau (including cases where the tableau
148  * is empty), the function "add" is called on the isl_sol passed
149  * to find_solutions_main.
150  *
151  * The context tableau is owned by isl_sol and is updated incrementally.
152  *
153  * There are currently two implementations of this interface,
154  * isl_sol_map, which simply collects the solutions in an isl_map
155  * and (optionally) the parts of the context where there is no solution
156  * in an isl_set, and
157  * isl_sol_for, which calls a user-defined function for each part of
158  * the solution.
159  */
160 struct isl_sol {
161         int error;
162         int rational;
163         int level;
164         int max;
165         int n_out;
166         struct isl_context *context;
167         struct isl_partial_sol *partial;
168         void (*add)(struct isl_sol *sol,
169                             struct isl_basic_set *dom, struct isl_mat *M);
170         void (*add_empty)(struct isl_sol *sol, struct isl_basic_set *bset);
171         void (*free)(struct isl_sol *sol);
172         struct isl_sol_callback dec_level;
173 };
174
175 static void sol_free(struct isl_sol *sol)
176 {
177         struct isl_partial_sol *partial, *next;
178         if (!sol)
179                 return;
180         for (partial = sol->partial; partial; partial = next) {
181                 next = partial->next;
182                 isl_basic_set_free(partial->dom);
183                 isl_mat_free(partial->M);
184                 free(partial);
185         }
186         sol->free(sol);
187 }
188
189 /* Push a partial solution represented by a domain and mapping M
190  * onto the stack of partial solutions.
191  */
192 static void sol_push_sol(struct isl_sol *sol,
193         struct isl_basic_set *dom, struct isl_mat *M)
194 {
195         struct isl_partial_sol *partial;
196
197         if (sol->error || !dom)
198                 goto error;
199
200         partial = isl_alloc_type(dom->ctx, struct isl_partial_sol);
201         if (!partial)
202                 goto error;
203
204         partial->level = sol->level;
205         partial->dom = dom;
206         partial->M = M;
207         partial->next = sol->partial;
208
209         sol->partial = partial;
210
211         return;
212 error:
213         isl_basic_set_free(dom);
214         isl_mat_free(M);
215         sol->error = 1;
216 }
217
218 /* Pop one partial solution from the partial solution stack and
219  * pass it on to sol->add or sol->add_empty.
220  */
221 static void sol_pop_one(struct isl_sol *sol)
222 {
223         struct isl_partial_sol *partial;
224
225         partial = sol->partial;
226         sol->partial = partial->next;
227
228         if (partial->M)
229                 sol->add(sol, partial->dom, partial->M);
230         else
231                 sol->add_empty(sol, partial->dom);
232         free(partial);
233 }
234
235 /* Return a fresh copy of the domain represented by the context tableau.
236  */
237 static struct isl_basic_set *sol_domain(struct isl_sol *sol)
238 {
239         struct isl_basic_set *bset;
240
241         if (sol->error)
242                 return NULL;
243
244         bset = isl_basic_set_dup(sol->context->op->peek_basic_set(sol->context));
245         bset = isl_basic_set_update_from_tab(bset,
246                         sol->context->op->peek_tab(sol->context));
247
248         return bset;
249 }
250
251 /* Check whether two partial solutions have the same mapping, where n_div
252  * is the number of divs that the two partial solutions have in common.
253  */
254 static int same_solution(struct isl_partial_sol *s1, struct isl_partial_sol *s2,
255         unsigned n_div)
256 {
257         int i;
258         unsigned dim;
259
260         if (!s1->M != !s2->M)
261                 return 0;
262         if (!s1->M)
263                 return 1;
264
265         dim = isl_basic_set_total_dim(s1->dom) - s1->dom->n_div;
266
267         for (i = 0; i < s1->M->n_row; ++i) {
268                 if (isl_seq_first_non_zero(s1->M->row[i]+1+dim+n_div,
269                                             s1->M->n_col-1-dim-n_div) != -1)
270                         return 0;
271                 if (isl_seq_first_non_zero(s2->M->row[i]+1+dim+n_div,
272                                             s2->M->n_col-1-dim-n_div) != -1)
273                         return 0;
274                 if (!isl_seq_eq(s1->M->row[i], s2->M->row[i], 1+dim+n_div))
275                         return 0;
276         }
277         return 1;
278 }
279
280 /* Pop all solutions from the partial solution stack that were pushed onto
281  * the stack at levels that are deeper than the current level.
282  * If the two topmost elements on the stack have the same level
283  * and represent the same solution, then their domains are combined.
284  * This combined domain is the same as the current context domain
285  * as sol_pop is called each time we move back to a higher level.
286  */
287 static void sol_pop(struct isl_sol *sol)
288 {
289         struct isl_partial_sol *partial;
290         unsigned n_div;
291
292         if (sol->error)
293                 return;
294
295         if (sol->level == 0) {
296                 for (partial = sol->partial; partial; partial = sol->partial)
297                         sol_pop_one(sol);
298                 return;
299         }
300
301         partial = sol->partial;
302         if (!partial)
303                 return;
304
305         if (partial->level <= sol->level)
306                 return;
307
308         if (partial->next && partial->next->level == partial->level) {
309                 n_div = isl_basic_set_dim(
310                                 sol->context->op->peek_basic_set(sol->context),
311                                 isl_dim_div);
312
313                 if (!same_solution(partial, partial->next, n_div)) {
314                         sol_pop_one(sol);
315                         sol_pop_one(sol);
316                 } else {
317                         struct isl_basic_set *bset;
318                         isl_mat *M;
319                         unsigned n;
320
321                         n = isl_basic_set_dim(partial->next->dom, isl_dim_div);
322                         n -= n_div;
323                         bset = sol_domain(sol);
324                         isl_basic_set_free(partial->next->dom);
325                         partial->next->dom = bset;
326                         M = partial->next->M;
327                         if (M) {
328                                 M = isl_mat_drop_cols(M, M->n_col - n, n);
329                                 partial->next->M = M;
330                                 if (!M)
331                                         goto error;
332                         }
333                         partial->next->level = sol->level;
334
335                         if (!bset)
336                                 goto error;
337
338                         sol->partial = partial->next;
339                         isl_basic_set_free(partial->dom);
340                         isl_mat_free(partial->M);
341                         free(partial);
342                 }
343         } else
344                 sol_pop_one(sol);
345
346         if (0)
347 error:          sol->error = 1;
348 }
349
350 static void sol_dec_level(struct isl_sol *sol)
351 {
352         if (sol->error)
353                 return;
354
355         sol->level--;
356
357         sol_pop(sol);
358 }
359
360 static int sol_dec_level_wrap(struct isl_tab_callback *cb)
361 {
362         struct isl_sol_callback *callback = (struct isl_sol_callback *)cb;
363
364         sol_dec_level(callback->sol);
365
366         return callback->sol->error ? -1 : 0;
367 }
368
369 /* Move down to next level and push callback onto context tableau
370  * to decrease the level again when it gets rolled back across
371  * the current state.  That is, dec_level will be called with
372  * the context tableau in the same state as it is when inc_level
373  * is called.
374  */
375 static void sol_inc_level(struct isl_sol *sol)
376 {
377         struct isl_tab *tab;
378
379         if (sol->error)
380                 return;
381
382         sol->level++;
383         tab = sol->context->op->peek_tab(sol->context);
384         if (isl_tab_push_callback(tab, &sol->dec_level.callback) < 0)
385                 sol->error = 1;
386 }
387
388 static void scale_rows(struct isl_mat *mat, isl_int m, int n_row)
389 {
390         int i;
391
392         if (isl_int_is_one(m))
393                 return;
394
395         for (i = 0; i < n_row; ++i)
396                 isl_seq_scale(mat->row[i], mat->row[i], m, mat->n_col);
397 }
398
399 /* Add the solution identified by the tableau and the context tableau.
400  *
401  * The layout of the variables is as follows.
402  *      tab->n_var is equal to the total number of variables in the input
403  *                      map (including divs that were copied from the context)
404  *                      + the number of extra divs constructed
405  *      Of these, the first tab->n_param and the last tab->n_div variables
406  *      correspond to the variables in the context, i.e.,
407  *              tab->n_param + tab->n_div = context_tab->n_var
408  *      tab->n_param is equal to the number of parameters and input
409  *                      dimensions in the input map
410  *      tab->n_div is equal to the number of divs in the context
411  *
412  * If there is no solution, then call add_empty with a basic set
413  * that corresponds to the context tableau.  (If add_empty is NULL,
414  * then do nothing).
415  *
416  * If there is a solution, then first construct a matrix that maps
417  * all dimensions of the context to the output variables, i.e.,
418  * the output dimensions in the input map.
419  * The divs in the input map (if any) that do not correspond to any
420  * div in the context do not appear in the solution.
421  * The algorithm will make sure that they have an integer value,
422  * but these values themselves are of no interest.
423  * We have to be careful not to drop or rearrange any divs in the
424  * context because that would change the meaning of the matrix.
425  *
426  * To extract the value of the output variables, it should be noted
427  * that we always use a big parameter M in the main tableau and so
428  * the variable stored in this tableau is not an output variable x itself, but
429  *      x' = M + x (in case of minimization)
430  * or
431  *      x' = M - x (in case of maximization)
432  * If x' appears in a column, then its optimal value is zero,
433  * which means that the optimal value of x is an unbounded number
434  * (-M for minimization and M for maximization).
435  * We currently assume that the output dimensions in the original map
436  * are bounded, so this cannot occur.
437  * Similarly, when x' appears in a row, then the coefficient of M in that
438  * row is necessarily 1.
439  * If the row in the tableau represents
440  *      d x' = c + d M + e(y)
441  * then, in case of minimization, the corresponding row in the matrix
442  * will be
443  *      a c + a e(y)
444  * with a d = m, the (updated) common denominator of the matrix.
445  * In case of maximization, the row will be
446  *      -a c - a e(y)
447  */
448 static void sol_add(struct isl_sol *sol, struct isl_tab *tab)
449 {
450         struct isl_basic_set *bset = NULL;
451         struct isl_mat *mat = NULL;
452         unsigned off;
453         int row;
454         isl_int m;
455
456         if (sol->error || !tab)
457                 goto error;
458
459         if (tab->empty && !sol->add_empty)
460                 return;
461         if (sol->context->op->is_empty(sol->context))
462                 return;
463
464         bset = sol_domain(sol);
465
466         if (tab->empty) {
467                 sol_push_sol(sol, bset, NULL);
468                 return;
469         }
470
471         off = 2 + tab->M;
472
473         mat = isl_mat_alloc(tab->mat->ctx, 1 + sol->n_out,
474                                             1 + tab->n_param + tab->n_div);
475         if (!mat)
476                 goto error;
477
478         isl_int_init(m);
479
480         isl_seq_clr(mat->row[0] + 1, mat->n_col - 1);
481         isl_int_set_si(mat->row[0][0], 1);
482         for (row = 0; row < sol->n_out; ++row) {
483                 int i = tab->n_param + row;
484                 int r, j;
485
486                 isl_seq_clr(mat->row[1 + row], mat->n_col);
487                 if (!tab->var[i].is_row) {
488                         if (tab->M)
489                                 isl_die(mat->ctx, isl_error_invalid,
490                                         "unbounded optimum", goto error2);
491                         continue;
492                 }
493
494                 r = tab->var[i].index;
495                 if (tab->M &&
496                     isl_int_ne(tab->mat->row[r][2], tab->mat->row[r][0]))
497                         isl_die(mat->ctx, isl_error_invalid,
498                                 "unbounded optimum", goto error2);
499                 isl_int_gcd(m, mat->row[0][0], tab->mat->row[r][0]);
500                 isl_int_divexact(m, tab->mat->row[r][0], m);
501                 scale_rows(mat, m, 1 + row);
502                 isl_int_divexact(m, mat->row[0][0], tab->mat->row[r][0]);
503                 isl_int_mul(mat->row[1 + row][0], m, tab->mat->row[r][1]);
504                 for (j = 0; j < tab->n_param; ++j) {
505                         int col;
506                         if (tab->var[j].is_row)
507                                 continue;
508                         col = tab->var[j].index;
509                         isl_int_mul(mat->row[1 + row][1 + j], m,
510                                     tab->mat->row[r][off + col]);
511                 }
512                 for (j = 0; j < tab->n_div; ++j) {
513                         int col;
514                         if (tab->var[tab->n_var - tab->n_div+j].is_row)
515                                 continue;
516                         col = tab->var[tab->n_var - tab->n_div+j].index;
517                         isl_int_mul(mat->row[1 + row][1 + tab->n_param + j], m,
518                                     tab->mat->row[r][off + col]);
519                 }
520                 if (sol->max)
521                         isl_seq_neg(mat->row[1 + row], mat->row[1 + row],
522                                     mat->n_col);
523         }
524
525         isl_int_clear(m);
526
527         sol_push_sol(sol, bset, mat);
528         return;
529 error2:
530         isl_int_clear(m);
531 error:
532         isl_basic_set_free(bset);
533         isl_mat_free(mat);
534         sol->error = 1;
535 }
536
537 struct isl_sol_map {
538         struct isl_sol  sol;
539         struct isl_map  *map;
540         struct isl_set  *empty;
541 };
542
543 static void sol_map_free(struct isl_sol_map *sol_map)
544 {
545         if (!sol_map)
546                 return;
547         if (sol_map->sol.context)
548                 sol_map->sol.context->op->free(sol_map->sol.context);
549         isl_map_free(sol_map->map);
550         isl_set_free(sol_map->empty);
551         free(sol_map);
552 }
553
554 static void sol_map_free_wrap(struct isl_sol *sol)
555 {
556         sol_map_free((struct isl_sol_map *)sol);
557 }
558
559 /* This function is called for parts of the context where there is
560  * no solution, with "bset" corresponding to the context tableau.
561  * Simply add the basic set to the set "empty".
562  */
563 static void sol_map_add_empty(struct isl_sol_map *sol,
564         struct isl_basic_set *bset)
565 {
566         if (!bset)
567                 goto error;
568         isl_assert(bset->ctx, sol->empty, goto error);
569
570         sol->empty = isl_set_grow(sol->empty, 1);
571         bset = isl_basic_set_simplify(bset);
572         bset = isl_basic_set_finalize(bset);
573         sol->empty = isl_set_add_basic_set(sol->empty, isl_basic_set_copy(bset));
574         if (!sol->empty)
575                 goto error;
576         isl_basic_set_free(bset);
577         return;
578 error:
579         isl_basic_set_free(bset);
580         sol->sol.error = 1;
581 }
582
583 static void sol_map_add_empty_wrap(struct isl_sol *sol,
584         struct isl_basic_set *bset)
585 {
586         sol_map_add_empty((struct isl_sol_map *)sol, bset);
587 }
588
589 /* Given a basic map "dom" that represents the context and an affine
590  * matrix "M" that maps the dimensions of the context to the
591  * output variables, construct a basic map with the same parameters
592  * and divs as the context, the dimensions of the context as input
593  * dimensions and a number of output dimensions that is equal to
594  * the number of output dimensions in the input map.
595  *
596  * The constraints and divs of the context are simply copied
597  * from "dom".  For each row
598  *      x = c + e(y)
599  * an equality
600  *      c + e(y) - d x = 0
601  * is added, with d the common denominator of M.
602  */
603 static void sol_map_add(struct isl_sol_map *sol,
604         struct isl_basic_set *dom, struct isl_mat *M)
605 {
606         int i;
607         struct isl_basic_map *bmap = NULL;
608         unsigned n_eq;
609         unsigned n_ineq;
610         unsigned nparam;
611         unsigned total;
612         unsigned n_div;
613         unsigned n_out;
614
615         if (sol->sol.error || !dom || !M)
616                 goto error;
617
618         n_out = sol->sol.n_out;
619         n_eq = dom->n_eq + n_out;
620         n_ineq = dom->n_ineq;
621         n_div = dom->n_div;
622         nparam = isl_basic_set_total_dim(dom) - n_div;
623         total = isl_map_dim(sol->map, isl_dim_all);
624         bmap = isl_basic_map_alloc_space(isl_map_get_space(sol->map),
625                                         n_div, n_eq, 2 * n_div + n_ineq);
626         if (!bmap)
627                 goto error;
628         if (sol->sol.rational)
629                 ISL_F_SET(bmap, ISL_BASIC_MAP_RATIONAL);
630         for (i = 0; i < dom->n_div; ++i) {
631                 int k = isl_basic_map_alloc_div(bmap);
632                 if (k < 0)
633                         goto error;
634                 isl_seq_cpy(bmap->div[k], dom->div[i], 1 + 1 + nparam);
635                 isl_seq_clr(bmap->div[k] + 1 + 1 + nparam, total - nparam);
636                 isl_seq_cpy(bmap->div[k] + 1 + 1 + total,
637                             dom->div[i] + 1 + 1 + nparam, i);
638         }
639         for (i = 0; i < dom->n_eq; ++i) {
640                 int k = isl_basic_map_alloc_equality(bmap);
641                 if (k < 0)
642                         goto error;
643                 isl_seq_cpy(bmap->eq[k], dom->eq[i], 1 + nparam);
644                 isl_seq_clr(bmap->eq[k] + 1 + nparam, total - nparam);
645                 isl_seq_cpy(bmap->eq[k] + 1 + total,
646                             dom->eq[i] + 1 + nparam, n_div);
647         }
648         for (i = 0; i < dom->n_ineq; ++i) {
649                 int k = isl_basic_map_alloc_inequality(bmap);
650                 if (k < 0)
651                         goto error;
652                 isl_seq_cpy(bmap->ineq[k], dom->ineq[i], 1 + nparam);
653                 isl_seq_clr(bmap->ineq[k] + 1 + nparam, total - nparam);
654                 isl_seq_cpy(bmap->ineq[k] + 1 + total,
655                         dom->ineq[i] + 1 + nparam, n_div);
656         }
657         for (i = 0; i < M->n_row - 1; ++i) {
658                 int k = isl_basic_map_alloc_equality(bmap);
659                 if (k < 0)
660                         goto error;
661                 isl_seq_cpy(bmap->eq[k], M->row[1 + i], 1 + nparam);
662                 isl_seq_clr(bmap->eq[k] + 1 + nparam, n_out);
663                 isl_int_neg(bmap->eq[k][1 + nparam + i], M->row[0][0]);
664                 isl_seq_cpy(bmap->eq[k] + 1 + nparam + n_out,
665                             M->row[1 + i] + 1 + nparam, n_div);
666         }
667         bmap = isl_basic_map_simplify(bmap);
668         bmap = isl_basic_map_finalize(bmap);
669         sol->map = isl_map_grow(sol->map, 1);
670         sol->map = isl_map_add_basic_map(sol->map, bmap);
671         isl_basic_set_free(dom);
672         isl_mat_free(M);
673         if (!sol->map)
674                 sol->sol.error = 1;
675         return;
676 error:
677         isl_basic_set_free(dom);
678         isl_mat_free(M);
679         isl_basic_map_free(bmap);
680         sol->sol.error = 1;
681 }
682
683 static void sol_map_add_wrap(struct isl_sol *sol,
684         struct isl_basic_set *dom, struct isl_mat *M)
685 {
686         sol_map_add((struct isl_sol_map *)sol, dom, M);
687 }
688
689
690 /* Store the "parametric constant" of row "row" of tableau "tab" in "line",
691  * i.e., the constant term and the coefficients of all variables that
692  * appear in the context tableau.
693  * Note that the coefficient of the big parameter M is NOT copied.
694  * The context tableau may not have a big parameter and even when it
695  * does, it is a different big parameter.
696  */
697 static void get_row_parameter_line(struct isl_tab *tab, int row, isl_int *line)
698 {
699         int i;
700         unsigned off = 2 + tab->M;
701
702         isl_int_set(line[0], tab->mat->row[row][1]);
703         for (i = 0; i < tab->n_param; ++i) {
704                 if (tab->var[i].is_row)
705                         isl_int_set_si(line[1 + i], 0);
706                 else {
707                         int col = tab->var[i].index;
708                         isl_int_set(line[1 + i], tab->mat->row[row][off + col]);
709                 }
710         }
711         for (i = 0; i < tab->n_div; ++i) {
712                 if (tab->var[tab->n_var - tab->n_div + i].is_row)
713                         isl_int_set_si(line[1 + tab->n_param + i], 0);
714                 else {
715                         int col = tab->var[tab->n_var - tab->n_div + i].index;
716                         isl_int_set(line[1 + tab->n_param + i],
717                                     tab->mat->row[row][off + col]);
718                 }
719         }
720 }
721
722 /* Check if rows "row1" and "row2" have identical "parametric constants",
723  * as explained above.
724  * In this case, we also insist that the coefficients of the big parameter
725  * be the same as the values of the constants will only be the same
726  * if these coefficients are also the same.
727  */
728 static int identical_parameter_line(struct isl_tab *tab, int row1, int row2)
729 {
730         int i;
731         unsigned off = 2 + tab->M;
732
733         if (isl_int_ne(tab->mat->row[row1][1], tab->mat->row[row2][1]))
734                 return 0;
735
736         if (tab->M && isl_int_ne(tab->mat->row[row1][2],
737                                  tab->mat->row[row2][2]))
738                 return 0;
739
740         for (i = 0; i < tab->n_param + tab->n_div; ++i) {
741                 int pos = i < tab->n_param ? i :
742                         tab->n_var - tab->n_div + i - tab->n_param;
743                 int col;
744
745                 if (tab->var[pos].is_row)
746                         continue;
747                 col = tab->var[pos].index;
748                 if (isl_int_ne(tab->mat->row[row1][off + col],
749                                tab->mat->row[row2][off + col]))
750                         return 0;
751         }
752         return 1;
753 }
754
755 /* Return an inequality that expresses that the "parametric constant"
756  * should be non-negative.
757  * This function is only called when the coefficient of the big parameter
758  * is equal to zero.
759  */
760 static struct isl_vec *get_row_parameter_ineq(struct isl_tab *tab, int row)
761 {
762         struct isl_vec *ineq;
763
764         ineq = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_param + tab->n_div);
765         if (!ineq)
766                 return NULL;
767
768         get_row_parameter_line(tab, row, ineq->el);
769         if (ineq)
770                 ineq = isl_vec_normalize(ineq);
771
772         return ineq;
773 }
774
775 /* Normalize a div expression of the form
776  *
777  *      [(g*f(x) + c)/(g * m)]
778  *
779  * with c the constant term and f(x) the remaining coefficients, to
780  *
781  *      [(f(x) + [c/g])/m]
782  */
783 static void normalize_div(__isl_keep isl_vec *div)
784 {
785         isl_ctx *ctx = isl_vec_get_ctx(div);
786         int len = div->size - 2;
787
788         isl_seq_gcd(div->el + 2, len, &ctx->normalize_gcd);
789         isl_int_gcd(ctx->normalize_gcd, ctx->normalize_gcd, div->el[0]);
790
791         if (isl_int_is_one(ctx->normalize_gcd))
792                 return;
793
794         isl_int_divexact(div->el[0], div->el[0], ctx->normalize_gcd);
795         isl_int_fdiv_q(div->el[1], div->el[1], ctx->normalize_gcd);
796         isl_seq_scale_down(div->el + 2, div->el + 2, ctx->normalize_gcd, len);
797 }
798
799 /* Return a integer division for use in a parametric cut based on the given row.
800  * In particular, let the parametric constant of the row be
801  *
802  *              \sum_i a_i y_i
803  *
804  * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
805  * The div returned is equal to
806  *
807  *              floor(\sum_i {-a_i} y_i) = floor((\sum_i (-a_i mod d) y_i)/d)
808  */
809 static struct isl_vec *get_row_parameter_div(struct isl_tab *tab, int row)
810 {
811         struct isl_vec *div;
812
813         div = isl_vec_alloc(tab->mat->ctx, 1 + 1 + tab->n_param + tab->n_div);
814         if (!div)
815                 return NULL;
816
817         isl_int_set(div->el[0], tab->mat->row[row][0]);
818         get_row_parameter_line(tab, row, div->el + 1);
819         isl_seq_neg(div->el + 1, div->el + 1, div->size - 1);
820         normalize_div(div);
821         isl_seq_fdiv_r(div->el + 1, div->el + 1, div->el[0], div->size - 1);
822
823         return div;
824 }
825
826 /* Return a integer division for use in transferring an integrality constraint
827  * to the context.
828  * In particular, let the parametric constant of the row be
829  *
830  *              \sum_i a_i y_i
831  *
832  * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
833  * The the returned div is equal to
834  *
835  *              floor(\sum_i {a_i} y_i) = floor((\sum_i (a_i mod d) y_i)/d)
836  */
837 static struct isl_vec *get_row_split_div(struct isl_tab *tab, int row)
838 {
839         struct isl_vec *div;
840
841         div = isl_vec_alloc(tab->mat->ctx, 1 + 1 + tab->n_param + tab->n_div);
842         if (!div)
843                 return NULL;
844
845         isl_int_set(div->el[0], tab->mat->row[row][0]);
846         get_row_parameter_line(tab, row, div->el + 1);
847         normalize_div(div);
848         isl_seq_fdiv_r(div->el + 1, div->el + 1, div->el[0], div->size - 1);
849
850         return div;
851 }
852
853 /* Construct and return an inequality that expresses an upper bound
854  * on the given div.
855  * In particular, if the div is given by
856  *
857  *      d = floor(e/m)
858  *
859  * then the inequality expresses
860  *
861  *      m d <= e
862  */
863 static struct isl_vec *ineq_for_div(struct isl_basic_set *bset, unsigned div)
864 {
865         unsigned total;
866         unsigned div_pos;
867         struct isl_vec *ineq;
868
869         if (!bset)
870                 return NULL;
871
872         total = isl_basic_set_total_dim(bset);
873         div_pos = 1 + total - bset->n_div + div;
874
875         ineq = isl_vec_alloc(bset->ctx, 1 + total);
876         if (!ineq)
877                 return NULL;
878
879         isl_seq_cpy(ineq->el, bset->div[div] + 1, 1 + total);
880         isl_int_neg(ineq->el[div_pos], bset->div[div][0]);
881         return ineq;
882 }
883
884 /* Given a row in the tableau and a div that was created
885  * using get_row_split_div and that has been constrained to equality, i.e.,
886  *
887  *              d = floor(\sum_i {a_i} y_i) = \sum_i {a_i} y_i
888  *
889  * replace the expression "\sum_i {a_i} y_i" in the row by d,
890  * i.e., we subtract "\sum_i {a_i} y_i" and add 1 d.
891  * The coefficients of the non-parameters in the tableau have been
892  * verified to be integral.  We can therefore simply replace coefficient b
893  * by floor(b).  For the coefficients of the parameters we have
894  * floor(a_i) = a_i - {a_i}, while for the other coefficients, we have
895  * floor(b) = b.
896  */
897 static struct isl_tab *set_row_cst_to_div(struct isl_tab *tab, int row, int div)
898 {
899         isl_seq_fdiv_q(tab->mat->row[row] + 1, tab->mat->row[row] + 1,
900                         tab->mat->row[row][0], 1 + tab->M + tab->n_col);
901
902         isl_int_set_si(tab->mat->row[row][0], 1);
903
904         if (tab->var[tab->n_var - tab->n_div + div].is_row) {
905                 int drow = tab->var[tab->n_var - tab->n_div + div].index;
906
907                 isl_assert(tab->mat->ctx,
908                         isl_int_is_one(tab->mat->row[drow][0]), goto error);
909                 isl_seq_combine(tab->mat->row[row] + 1,
910                         tab->mat->ctx->one, tab->mat->row[row] + 1,
911                         tab->mat->ctx->one, tab->mat->row[drow] + 1,
912                         1 + tab->M + tab->n_col);
913         } else {
914                 int dcol = tab->var[tab->n_var - tab->n_div + div].index;
915
916                 isl_int_add_ui(tab->mat->row[row][2 + tab->M + dcol],
917                                 tab->mat->row[row][2 + tab->M + dcol], 1);
918         }
919
920         return tab;
921 error:
922         isl_tab_free(tab);
923         return NULL;
924 }
925
926 /* Check if the (parametric) constant of the given row is obviously
927  * negative, meaning that we don't need to consult the context tableau.
928  * If there is a big parameter and its coefficient is non-zero,
929  * then this coefficient determines the outcome.
930  * Otherwise, we check whether the constant is negative and
931  * all non-zero coefficients of parameters are negative and
932  * belong to non-negative parameters.
933  */
934 static int is_obviously_neg(struct isl_tab *tab, int row)
935 {
936         int i;
937         int col;
938         unsigned off = 2 + tab->M;
939
940         if (tab->M) {
941                 if (isl_int_is_pos(tab->mat->row[row][2]))
942                         return 0;
943                 if (isl_int_is_neg(tab->mat->row[row][2]))
944                         return 1;
945         }
946
947         if (isl_int_is_nonneg(tab->mat->row[row][1]))
948                 return 0;
949         for (i = 0; i < tab->n_param; ++i) {
950                 /* Eliminated parameter */
951                 if (tab->var[i].is_row)
952                         continue;
953                 col = tab->var[i].index;
954                 if (isl_int_is_zero(tab->mat->row[row][off + col]))
955                         continue;
956                 if (!tab->var[i].is_nonneg)
957                         return 0;
958                 if (isl_int_is_pos(tab->mat->row[row][off + col]))
959                         return 0;
960         }
961         for (i = 0; i < tab->n_div; ++i) {
962                 if (tab->var[tab->n_var - tab->n_div + i].is_row)
963                         continue;
964                 col = tab->var[tab->n_var - tab->n_div + i].index;
965                 if (isl_int_is_zero(tab->mat->row[row][off + col]))
966                         continue;
967                 if (!tab->var[tab->n_var - tab->n_div + i].is_nonneg)
968                         return 0;
969                 if (isl_int_is_pos(tab->mat->row[row][off + col]))
970                         return 0;
971         }
972         return 1;
973 }
974
975 /* Check if the (parametric) constant of the given row is obviously
976  * non-negative, meaning that we don't need to consult the context tableau.
977  * If there is a big parameter and its coefficient is non-zero,
978  * then this coefficient determines the outcome.
979  * Otherwise, we check whether the constant is non-negative and
980  * all non-zero coefficients of parameters are positive and
981  * belong to non-negative parameters.
982  */
983 static int is_obviously_nonneg(struct isl_tab *tab, int row)
984 {
985         int i;
986         int col;
987         unsigned off = 2 + tab->M;
988
989         if (tab->M) {
990                 if (isl_int_is_pos(tab->mat->row[row][2]))
991                         return 1;
992                 if (isl_int_is_neg(tab->mat->row[row][2]))
993                         return 0;
994         }
995
996         if (isl_int_is_neg(tab->mat->row[row][1]))
997                 return 0;
998         for (i = 0; i < tab->n_param; ++i) {
999                 /* Eliminated parameter */
1000                 if (tab->var[i].is_row)
1001                         continue;
1002                 col = tab->var[i].index;
1003                 if (isl_int_is_zero(tab->mat->row[row][off + col]))
1004                         continue;
1005                 if (!tab->var[i].is_nonneg)
1006                         return 0;
1007                 if (isl_int_is_neg(tab->mat->row[row][off + col]))
1008                         return 0;
1009         }
1010         for (i = 0; i < tab->n_div; ++i) {
1011                 if (tab->var[tab->n_var - tab->n_div + i].is_row)
1012                         continue;
1013                 col = tab->var[tab->n_var - tab->n_div + i].index;
1014                 if (isl_int_is_zero(tab->mat->row[row][off + col]))
1015                         continue;
1016                 if (!tab->var[tab->n_var - tab->n_div + i].is_nonneg)
1017                         return 0;
1018                 if (isl_int_is_neg(tab->mat->row[row][off + col]))
1019                         return 0;
1020         }
1021         return 1;
1022 }
1023
1024 /* Given a row r and two columns, return the column that would
1025  * lead to the lexicographically smallest increment in the sample
1026  * solution when leaving the basis in favor of the row.
1027  * Pivoting with column c will increment the sample value by a non-negative
1028  * constant times a_{V,c}/a_{r,c}, with a_{V,c} the elements of column c
1029  * corresponding to the non-parametric variables.
1030  * If variable v appears in a column c_v, the a_{v,c} = 1 iff c = c_v,
1031  * with all other entries in this virtual row equal to zero.
1032  * If variable v appears in a row, then a_{v,c} is the element in column c
1033  * of that row.
1034  *
1035  * Let v be the first variable with a_{v,c1}/a_{r,c1} != a_{v,c2}/a_{r,c2}.
1036  * Then if a_{v,c1}/a_{r,c1} < a_{v,c2}/a_{r,c2}, i.e.,
1037  * a_{v,c2} a_{r,c1} - a_{v,c1} a_{r,c2} > 0, c1 results in the minimal
1038  * increment.  Otherwise, it's c2.
1039  */
1040 static int lexmin_col_pair(struct isl_tab *tab,
1041         int row, int col1, int col2, isl_int tmp)
1042 {
1043         int i;
1044         isl_int *tr;
1045
1046         tr = tab->mat->row[row] + 2 + tab->M;
1047
1048         for (i = tab->n_param; i < tab->n_var - tab->n_div; ++i) {
1049                 int s1, s2;
1050                 isl_int *r;
1051
1052                 if (!tab->var[i].is_row) {
1053                         if (tab->var[i].index == col1)
1054                                 return col2;
1055                         if (tab->var[i].index == col2)
1056                                 return col1;
1057                         continue;
1058                 }
1059
1060                 if (tab->var[i].index == row)
1061                         continue;
1062
1063                 r = tab->mat->row[tab->var[i].index] + 2 + tab->M;
1064                 s1 = isl_int_sgn(r[col1]);
1065                 s2 = isl_int_sgn(r[col2]);
1066                 if (s1 == 0 && s2 == 0)
1067                         continue;
1068                 if (s1 < s2)
1069                         return col1;
1070                 if (s2 < s1)
1071                         return col2;
1072
1073                 isl_int_mul(tmp, r[col2], tr[col1]);
1074                 isl_int_submul(tmp, r[col1], tr[col2]);
1075                 if (isl_int_is_pos(tmp))
1076                         return col1;
1077                 if (isl_int_is_neg(tmp))
1078                         return col2;
1079         }
1080         return -1;
1081 }
1082
1083 /* Given a row in the tableau, find and return the column that would
1084  * result in the lexicographically smallest, but positive, increment
1085  * in the sample point.
1086  * If there is no such column, then return tab->n_col.
1087  * If anything goes wrong, return -1.
1088  */
1089 static int lexmin_pivot_col(struct isl_tab *tab, int row)
1090 {
1091         int j;
1092         int col = tab->n_col;
1093         isl_int *tr;
1094         isl_int tmp;
1095
1096         tr = tab->mat->row[row] + 2 + tab->M;
1097
1098         isl_int_init(tmp);
1099
1100         for (j = tab->n_dead; j < tab->n_col; ++j) {
1101                 if (tab->col_var[j] >= 0 &&
1102                     (tab->col_var[j] < tab->n_param  ||
1103                     tab->col_var[j] >= tab->n_var - tab->n_div))
1104                         continue;
1105
1106                 if (!isl_int_is_pos(tr[j]))
1107                         continue;
1108
1109                 if (col == tab->n_col)
1110                         col = j;
1111                 else
1112                         col = lexmin_col_pair(tab, row, col, j, tmp);
1113                 isl_assert(tab->mat->ctx, col >= 0, goto error);
1114         }
1115
1116         isl_int_clear(tmp);
1117         return col;
1118 error:
1119         isl_int_clear(tmp);
1120         return -1;
1121 }
1122
1123 /* Return the first known violated constraint, i.e., a non-negative
1124  * constraint that currently has an either obviously negative value
1125  * or a previously determined to be negative value.
1126  *
1127  * If any constraint has a negative coefficient for the big parameter,
1128  * if any, then we return one of these first.
1129  */
1130 static int first_neg(struct isl_tab *tab)
1131 {
1132         int row;
1133
1134         if (tab->M)
1135                 for (row = tab->n_redundant; row < tab->n_row; ++row) {
1136                         if (!isl_tab_var_from_row(tab, row)->is_nonneg)
1137                                 continue;
1138                         if (!isl_int_is_neg(tab->mat->row[row][2]))
1139                                 continue;
1140                         if (tab->row_sign)
1141                                 tab->row_sign[row] = isl_tab_row_neg;
1142                         return row;
1143                 }
1144         for (row = tab->n_redundant; row < tab->n_row; ++row) {
1145                 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
1146                         continue;
1147                 if (tab->row_sign) {
1148                         if (tab->row_sign[row] == 0 &&
1149                             is_obviously_neg(tab, row))
1150                                 tab->row_sign[row] = isl_tab_row_neg;
1151                         if (tab->row_sign[row] != isl_tab_row_neg)
1152                                 continue;
1153                 } else if (!is_obviously_neg(tab, row))
1154                         continue;
1155                 return row;
1156         }
1157         return -1;
1158 }
1159
1160 /* Check whether the invariant that all columns are lexico-positive
1161  * is satisfied.  This function is not called from the current code
1162  * but is useful during debugging.
1163  */
1164 static void check_lexpos(struct isl_tab *tab) __attribute__ ((unused));
1165 static void check_lexpos(struct isl_tab *tab)
1166 {
1167         unsigned off = 2 + tab->M;
1168         int col;
1169         int var;
1170         int row;
1171
1172         for (col = tab->n_dead; col < tab->n_col; ++col) {
1173                 if (tab->col_var[col] >= 0 &&
1174                     (tab->col_var[col] < tab->n_param ||
1175                      tab->col_var[col] >= tab->n_var - tab->n_div))
1176                         continue;
1177                 for (var = tab->n_param; var < tab->n_var - tab->n_div; ++var) {
1178                         if (!tab->var[var].is_row) {
1179                                 if (tab->var[var].index == col)
1180                                         break;
1181                                 else
1182                                         continue;
1183                         }
1184                         row = tab->var[var].index;
1185                         if (isl_int_is_zero(tab->mat->row[row][off + col]))
1186                                 continue;
1187                         if (isl_int_is_pos(tab->mat->row[row][off + col]))
1188                                 break;
1189                         fprintf(stderr, "lexneg column %d (row %d)\n",
1190                                 col, row);
1191                 }
1192                 if (var >= tab->n_var - tab->n_div)
1193                         fprintf(stderr, "zero column %d\n", col);
1194         }
1195 }
1196
1197 /* Report to the caller that the given constraint is part of an encountered
1198  * conflict.
1199  */
1200 static int report_conflicting_constraint(struct isl_tab *tab, int con)
1201 {
1202         return tab->conflict(con, tab->conflict_user);
1203 }
1204
1205 /* Given a conflicting row in the tableau, report all constraints
1206  * involved in the row to the caller.  That is, the row itself
1207  * (if it represents a constraint) and all constraint columns with
1208  * non-zero (and therefore negative) coefficients.
1209  */
1210 static int report_conflict(struct isl_tab *tab, int row)
1211 {
1212         int j;
1213         isl_int *tr;
1214
1215         if (!tab->conflict)
1216                 return 0;
1217
1218         if (tab->row_var[row] < 0 &&
1219             report_conflicting_constraint(tab, ~tab->row_var[row]) < 0)
1220                 return -1;
1221
1222         tr = tab->mat->row[row] + 2 + tab->M;
1223
1224         for (j = tab->n_dead; j < tab->n_col; ++j) {
1225                 if (tab->col_var[j] >= 0 &&
1226                     (tab->col_var[j] < tab->n_param  ||
1227                     tab->col_var[j] >= tab->n_var - tab->n_div))
1228                         continue;
1229
1230                 if (!isl_int_is_neg(tr[j]))
1231                         continue;
1232
1233                 if (tab->col_var[j] < 0 &&
1234                     report_conflicting_constraint(tab, ~tab->col_var[j]) < 0)
1235                         return -1;
1236         }
1237
1238         return 0;
1239 }
1240
1241 /* Resolve all known or obviously violated constraints through pivoting.
1242  * In particular, as long as we can find any violated constraint, we
1243  * look for a pivoting column that would result in the lexicographically
1244  * smallest increment in the sample point.  If there is no such column
1245  * then the tableau is infeasible.
1246  */
1247 static int restore_lexmin(struct isl_tab *tab) WARN_UNUSED;
1248 static int restore_lexmin(struct isl_tab *tab)
1249 {
1250         int row, col;
1251
1252         if (!tab)
1253                 return -1;
1254         if (tab->empty)
1255                 return 0;
1256         while ((row = first_neg(tab)) != -1) {
1257                 col = lexmin_pivot_col(tab, row);
1258                 if (col >= tab->n_col) {
1259                         if (report_conflict(tab, row) < 0)
1260                                 return -1;
1261                         if (isl_tab_mark_empty(tab) < 0)
1262                                 return -1;
1263                         return 0;
1264                 }
1265                 if (col < 0)
1266                         return -1;
1267                 if (isl_tab_pivot(tab, row, col) < 0)
1268                         return -1;
1269         }
1270         return 0;
1271 }
1272
1273 /* Given a row that represents an equality, look for an appropriate
1274  * pivoting column.
1275  * In particular, if there are any non-zero coefficients among
1276  * the non-parameter variables, then we take the last of these
1277  * variables.  Eliminating this variable in terms of the other
1278  * variables and/or parameters does not influence the property
1279  * that all column in the initial tableau are lexicographically
1280  * positive.  The row corresponding to the eliminated variable
1281  * will only have non-zero entries below the diagonal of the
1282  * initial tableau.  That is, we transform
1283  *
1284  *              I                               I
1285  *                1             into            a
1286  *                  I                             I
1287  *
1288  * If there is no such non-parameter variable, then we are dealing with
1289  * pure parameter equality and we pick any parameter with coefficient 1 or -1
1290  * for elimination.  This will ensure that the eliminated parameter
1291  * always has an integer value whenever all the other parameters are integral.
1292  * If there is no such parameter then we return -1.
1293  */
1294 static int last_var_col_or_int_par_col(struct isl_tab *tab, int row)
1295 {
1296         unsigned off = 2 + tab->M;
1297         int i;
1298
1299         for (i = tab->n_var - tab->n_div - 1; i >= 0 && i >= tab->n_param; --i) {
1300                 int col;
1301                 if (tab->var[i].is_row)
1302                         continue;
1303                 col = tab->var[i].index;
1304                 if (col <= tab->n_dead)
1305                         continue;
1306                 if (!isl_int_is_zero(tab->mat->row[row][off + col]))
1307                         return col;
1308         }
1309         for (i = tab->n_dead; i < tab->n_col; ++i) {
1310                 if (isl_int_is_one(tab->mat->row[row][off + i]))
1311                         return i;
1312                 if (isl_int_is_negone(tab->mat->row[row][off + i]))
1313                         return i;
1314         }
1315         return -1;
1316 }
1317
1318 /* Add an equality that is known to be valid to the tableau.
1319  * We first check if we can eliminate a variable or a parameter.
1320  * If not, we add the equality as two inequalities.
1321  * In this case, the equality was a pure parameter equality and there
1322  * is no need to resolve any constraint violations.
1323  */
1324 static struct isl_tab *add_lexmin_valid_eq(struct isl_tab *tab, isl_int *eq)
1325 {
1326         int i;
1327         int r;
1328
1329         if (!tab)
1330                 return NULL;
1331         r = isl_tab_add_row(tab, eq);
1332         if (r < 0)
1333                 goto error;
1334
1335         r = tab->con[r].index;
1336         i = last_var_col_or_int_par_col(tab, r);
1337         if (i < 0) {
1338                 tab->con[r].is_nonneg = 1;
1339                 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1340                         goto error;
1341                 isl_seq_neg(eq, eq, 1 + tab->n_var);
1342                 r = isl_tab_add_row(tab, eq);
1343                 if (r < 0)
1344                         goto error;
1345                 tab->con[r].is_nonneg = 1;
1346                 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1347                         goto error;
1348         } else {
1349                 if (isl_tab_pivot(tab, r, i) < 0)
1350                         goto error;
1351                 if (isl_tab_kill_col(tab, i) < 0)
1352                         goto error;
1353                 tab->n_eq++;
1354         }
1355
1356         return tab;
1357 error:
1358         isl_tab_free(tab);
1359         return NULL;
1360 }
1361
1362 /* Check if the given row is a pure constant.
1363  */
1364 static int is_constant(struct isl_tab *tab, int row)
1365 {
1366         unsigned off = 2 + tab->M;
1367
1368         return isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
1369                                         tab->n_col - tab->n_dead) == -1;
1370 }
1371
1372 /* Add an equality that may or may not be valid to the tableau.
1373  * If the resulting row is a pure constant, then it must be zero.
1374  * Otherwise, the resulting tableau is empty.
1375  *
1376  * If the row is not a pure constant, then we add two inequalities,
1377  * each time checking that they can be satisfied.
1378  * In the end we try to use one of the two constraints to eliminate
1379  * a column.
1380  */
1381 static int add_lexmin_eq(struct isl_tab *tab, isl_int *eq) WARN_UNUSED;
1382 static int add_lexmin_eq(struct isl_tab *tab, isl_int *eq)
1383 {
1384         int r1, r2;
1385         int row;
1386         struct isl_tab_undo *snap;
1387
1388         if (!tab)
1389                 return -1;
1390         snap = isl_tab_snap(tab);
1391         r1 = isl_tab_add_row(tab, eq);
1392         if (r1 < 0)
1393                 return -1;
1394         tab->con[r1].is_nonneg = 1;
1395         if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r1]) < 0)
1396                 return -1;
1397
1398         row = tab->con[r1].index;
1399         if (is_constant(tab, row)) {
1400                 if (!isl_int_is_zero(tab->mat->row[row][1]) ||
1401                     (tab->M && !isl_int_is_zero(tab->mat->row[row][2]))) {
1402                         if (isl_tab_mark_empty(tab) < 0)
1403                                 return -1;
1404                         return 0;
1405                 }
1406                 if (isl_tab_rollback(tab, snap) < 0)
1407                         return -1;
1408                 return 0;
1409         }
1410
1411         if (restore_lexmin(tab) < 0)
1412                 return -1;
1413         if (tab->empty)
1414                 return 0;
1415
1416         isl_seq_neg(eq, eq, 1 + tab->n_var);
1417
1418         r2 = isl_tab_add_row(tab, eq);
1419         if (r2 < 0)
1420                 return -1;
1421         tab->con[r2].is_nonneg = 1;
1422         if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r2]) < 0)
1423                 return -1;
1424
1425         if (restore_lexmin(tab) < 0)
1426                 return -1;
1427         if (tab->empty)
1428                 return 0;
1429
1430         if (!tab->con[r1].is_row) {
1431                 if (isl_tab_kill_col(tab, tab->con[r1].index) < 0)
1432                         return -1;
1433         } else if (!tab->con[r2].is_row) {
1434                 if (isl_tab_kill_col(tab, tab->con[r2].index) < 0)
1435                         return -1;
1436         }
1437
1438         if (tab->bmap) {
1439                 tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
1440                 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1441                         return -1;
1442                 isl_seq_neg(eq, eq, 1 + tab->n_var);
1443                 tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
1444                 isl_seq_neg(eq, eq, 1 + tab->n_var);
1445                 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1446                         return -1;
1447                 if (!tab->bmap)
1448                         return -1;
1449         }
1450
1451         return 0;
1452 }
1453
1454 /* Add an inequality to the tableau, resolving violations using
1455  * restore_lexmin.
1456  */
1457 static struct isl_tab *add_lexmin_ineq(struct isl_tab *tab, isl_int *ineq)
1458 {
1459         int r;
1460
1461         if (!tab)
1462                 return NULL;
1463         if (tab->bmap) {
1464                 tab->bmap = isl_basic_map_add_ineq(tab->bmap, ineq);
1465                 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1466                         goto error;
1467                 if (!tab->bmap)
1468                         goto error;
1469         }
1470         r = isl_tab_add_row(tab, ineq);
1471         if (r < 0)
1472                 goto error;
1473         tab->con[r].is_nonneg = 1;
1474         if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1475                 goto error;
1476         if (isl_tab_row_is_redundant(tab, tab->con[r].index)) {
1477                 if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1478                         goto error;
1479                 return tab;
1480         }
1481
1482         if (restore_lexmin(tab) < 0)
1483                 goto error;
1484         if (!tab->empty && tab->con[r].is_row &&
1485                  isl_tab_row_is_redundant(tab, tab->con[r].index))
1486                 if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1487                         goto error;
1488         return tab;
1489 error:
1490         isl_tab_free(tab);
1491         return NULL;
1492 }
1493
1494 /* Check if the coefficients of the parameters are all integral.
1495  */
1496 static int integer_parameter(struct isl_tab *tab, int row)
1497 {
1498         int i;
1499         int col;
1500         unsigned off = 2 + tab->M;
1501
1502         for (i = 0; i < tab->n_param; ++i) {
1503                 /* Eliminated parameter */
1504                 if (tab->var[i].is_row)
1505                         continue;
1506                 col = tab->var[i].index;
1507                 if (!isl_int_is_divisible_by(tab->mat->row[row][off + col],
1508                                                 tab->mat->row[row][0]))
1509                         return 0;
1510         }
1511         for (i = 0; i < tab->n_div; ++i) {
1512                 if (tab->var[tab->n_var - tab->n_div + i].is_row)
1513                         continue;
1514                 col = tab->var[tab->n_var - tab->n_div + i].index;
1515                 if (!isl_int_is_divisible_by(tab->mat->row[row][off + col],
1516                                                 tab->mat->row[row][0]))
1517                         return 0;
1518         }
1519         return 1;
1520 }
1521
1522 /* Check if the coefficients of the non-parameter variables are all integral.
1523  */
1524 static int integer_variable(struct isl_tab *tab, int row)
1525 {
1526         int i;
1527         unsigned off = 2 + tab->M;
1528
1529         for (i = tab->n_dead; i < tab->n_col; ++i) {
1530                 if (tab->col_var[i] >= 0 &&
1531                     (tab->col_var[i] < tab->n_param ||
1532                      tab->col_var[i] >= tab->n_var - tab->n_div))
1533                         continue;
1534                 if (!isl_int_is_divisible_by(tab->mat->row[row][off + i],
1535                                                 tab->mat->row[row][0]))
1536                         return 0;
1537         }
1538         return 1;
1539 }
1540
1541 /* Check if the constant term is integral.
1542  */
1543 static int integer_constant(struct isl_tab *tab, int row)
1544 {
1545         return isl_int_is_divisible_by(tab->mat->row[row][1],
1546                                         tab->mat->row[row][0]);
1547 }
1548
1549 #define I_CST   1 << 0
1550 #define I_PAR   1 << 1
1551 #define I_VAR   1 << 2
1552
1553 /* Check for next (non-parameter) variable after "var" (first if var == -1)
1554  * that is non-integer and therefore requires a cut and return
1555  * the index of the variable.
1556  * For parametric tableaus, there are three parts in a row,
1557  * the constant, the coefficients of the parameters and the rest.
1558  * For each part, we check whether the coefficients in that part
1559  * are all integral and if so, set the corresponding flag in *f.
1560  * If the constant and the parameter part are integral, then the
1561  * current sample value is integral and no cut is required
1562  * (irrespective of whether the variable part is integral).
1563  */
1564 static int next_non_integer_var(struct isl_tab *tab, int var, int *f)
1565 {
1566         var = var < 0 ? tab->n_param : var + 1;
1567
1568         for (; var < tab->n_var - tab->n_div; ++var) {
1569                 int flags = 0;
1570                 int row;
1571                 if (!tab->var[var].is_row)
1572                         continue;
1573                 row = tab->var[var].index;
1574                 if (integer_constant(tab, row))
1575                         ISL_FL_SET(flags, I_CST);
1576                 if (integer_parameter(tab, row))
1577                         ISL_FL_SET(flags, I_PAR);
1578                 if (ISL_FL_ISSET(flags, I_CST) && ISL_FL_ISSET(flags, I_PAR))
1579                         continue;
1580                 if (integer_variable(tab, row))
1581                         ISL_FL_SET(flags, I_VAR);
1582                 *f = flags;
1583                 return var;
1584         }
1585         return -1;
1586 }
1587
1588 /* Check for first (non-parameter) variable that is non-integer and
1589  * therefore requires a cut and return the corresponding row.
1590  * For parametric tableaus, there are three parts in a row,
1591  * the constant, the coefficients of the parameters and the rest.
1592  * For each part, we check whether the coefficients in that part
1593  * are all integral and if so, set the corresponding flag in *f.
1594  * If the constant and the parameter part are integral, then the
1595  * current sample value is integral and no cut is required
1596  * (irrespective of whether the variable part is integral).
1597  */
1598 static int first_non_integer_row(struct isl_tab *tab, int *f)
1599 {
1600         int var = next_non_integer_var(tab, -1, f);
1601
1602         return var < 0 ? -1 : tab->var[var].index;
1603 }
1604
1605 /* Add a (non-parametric) cut to cut away the non-integral sample
1606  * value of the given row.
1607  *
1608  * If the row is given by
1609  *
1610  *      m r = f + \sum_i a_i y_i
1611  *
1612  * then the cut is
1613  *
1614  *      c = - {-f/m} + \sum_i {a_i/m} y_i >= 0
1615  *
1616  * The big parameter, if any, is ignored, since it is assumed to be big
1617  * enough to be divisible by any integer.
1618  * If the tableau is actually a parametric tableau, then this function
1619  * is only called when all coefficients of the parameters are integral.
1620  * The cut therefore has zero coefficients for the parameters.
1621  *
1622  * The current value is known to be negative, so row_sign, if it
1623  * exists, is set accordingly.
1624  *
1625  * Return the row of the cut or -1.
1626  */
1627 static int add_cut(struct isl_tab *tab, int row)
1628 {
1629         int i;
1630         int r;
1631         isl_int *r_row;
1632         unsigned off = 2 + tab->M;
1633
1634         if (isl_tab_extend_cons(tab, 1) < 0)
1635                 return -1;
1636         r = isl_tab_allocate_con(tab);
1637         if (r < 0)
1638                 return -1;
1639
1640         r_row = tab->mat->row[tab->con[r].index];
1641         isl_int_set(r_row[0], tab->mat->row[row][0]);
1642         isl_int_neg(r_row[1], tab->mat->row[row][1]);
1643         isl_int_fdiv_r(r_row[1], r_row[1], tab->mat->row[row][0]);
1644         isl_int_neg(r_row[1], r_row[1]);
1645         if (tab->M)
1646                 isl_int_set_si(r_row[2], 0);
1647         for (i = 0; i < tab->n_col; ++i)
1648                 isl_int_fdiv_r(r_row[off + i],
1649                         tab->mat->row[row][off + i], tab->mat->row[row][0]);
1650
1651         tab->con[r].is_nonneg = 1;
1652         if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1653                 return -1;
1654         if (tab->row_sign)
1655                 tab->row_sign[tab->con[r].index] = isl_tab_row_neg;
1656
1657         return tab->con[r].index;
1658 }
1659
1660 #define CUT_ALL 1
1661 #define CUT_ONE 0
1662
1663 /* Given a non-parametric tableau, add cuts until an integer
1664  * sample point is obtained or until the tableau is determined
1665  * to be integer infeasible.
1666  * As long as there is any non-integer value in the sample point,
1667  * we add appropriate cuts, if possible, for each of these
1668  * non-integer values and then resolve the violated
1669  * cut constraints using restore_lexmin.
1670  * If one of the corresponding rows is equal to an integral
1671  * combination of variables/constraints plus a non-integral constant,
1672  * then there is no way to obtain an integer point and we return
1673  * a tableau that is marked empty.
1674  * The parameter cutting_strategy controls the strategy used when adding cuts
1675  * to remove non-integer points. CUT_ALL adds all possible cuts
1676  * before continuing the search. CUT_ONE adds only one cut at a time.
1677  */
1678 static struct isl_tab *cut_to_integer_lexmin(struct isl_tab *tab,
1679         int cutting_strategy)
1680 {
1681         int var;
1682         int row;
1683         int flags;
1684
1685         if (!tab)
1686                 return NULL;
1687         if (tab->empty)
1688                 return tab;
1689
1690         while ((var = next_non_integer_var(tab, -1, &flags)) != -1) {
1691                 do {
1692                         if (ISL_FL_ISSET(flags, I_VAR)) {
1693                                 if (isl_tab_mark_empty(tab) < 0)
1694                                         goto error;
1695                                 return tab;
1696                         }
1697                         row = tab->var[var].index;
1698                         row = add_cut(tab, row);
1699                         if (row < 0)
1700                                 goto error;
1701                         if (cutting_strategy == CUT_ONE)
1702                                 break;
1703                 } while ((var = next_non_integer_var(tab, var, &flags)) != -1);
1704                 if (restore_lexmin(tab) < 0)
1705                         goto error;
1706                 if (tab->empty)
1707                         break;
1708         }
1709         return tab;
1710 error:
1711         isl_tab_free(tab);
1712         return NULL;
1713 }
1714
1715 /* Check whether all the currently active samples also satisfy the inequality
1716  * "ineq" (treated as an equality if eq is set).
1717  * Remove those samples that do not.
1718  */
1719 static struct isl_tab *check_samples(struct isl_tab *tab, isl_int *ineq, int eq)
1720 {
1721         int i;
1722         isl_int v;
1723
1724         if (!tab)
1725                 return NULL;
1726
1727         isl_assert(tab->mat->ctx, tab->bmap, goto error);
1728         isl_assert(tab->mat->ctx, tab->samples, goto error);
1729         isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var, goto error);
1730
1731         isl_int_init(v);
1732         for (i = tab->n_outside; i < tab->n_sample; ++i) {
1733                 int sgn;
1734                 isl_seq_inner_product(ineq, tab->samples->row[i],
1735                                         1 + tab->n_var, &v);
1736                 sgn = isl_int_sgn(v);
1737                 if (eq ? (sgn == 0) : (sgn >= 0))
1738                         continue;
1739                 tab = isl_tab_drop_sample(tab, i);
1740                 if (!tab)
1741                         break;
1742         }
1743         isl_int_clear(v);
1744
1745         return tab;
1746 error:
1747         isl_tab_free(tab);
1748         return NULL;
1749 }
1750
1751 /* Check whether the sample value of the tableau is finite,
1752  * i.e., either the tableau does not use a big parameter, or
1753  * all values of the variables are equal to the big parameter plus
1754  * some constant.  This constant is the actual sample value.
1755  */
1756 static int sample_is_finite(struct isl_tab *tab)
1757 {
1758         int i;
1759
1760         if (!tab->M)
1761                 return 1;
1762
1763         for (i = 0; i < tab->n_var; ++i) {
1764                 int row;
1765                 if (!tab->var[i].is_row)
1766                         return 0;
1767                 row = tab->var[i].index;
1768                 if (isl_int_ne(tab->mat->row[row][0], tab->mat->row[row][2]))
1769                         return 0;
1770         }
1771         return 1;
1772 }
1773
1774 /* Check if the context tableau of sol has any integer points.
1775  * Leave tab in empty state if no integer point can be found.
1776  * If an integer point can be found and if moreover it is finite,
1777  * then it is added to the list of sample values.
1778  *
1779  * This function is only called when none of the currently active sample
1780  * values satisfies the most recently added constraint.
1781  */
1782 static struct isl_tab *check_integer_feasible(struct isl_tab *tab)
1783 {
1784         struct isl_tab_undo *snap;
1785
1786         if (!tab)
1787                 return NULL;
1788
1789         snap = isl_tab_snap(tab);
1790         if (isl_tab_push_basis(tab) < 0)
1791                 goto error;
1792
1793         tab = cut_to_integer_lexmin(tab, CUT_ALL);
1794         if (!tab)
1795                 goto error;
1796
1797         if (!tab->empty && sample_is_finite(tab)) {
1798                 struct isl_vec *sample;
1799
1800                 sample = isl_tab_get_sample_value(tab);
1801
1802                 tab = isl_tab_add_sample(tab, sample);
1803         }
1804
1805         if (!tab->empty && isl_tab_rollback(tab, snap) < 0)
1806                 goto error;
1807
1808         return tab;
1809 error:
1810         isl_tab_free(tab);
1811         return NULL;
1812 }
1813
1814 /* Check if any of the currently active sample values satisfies
1815  * the inequality "ineq" (an equality if eq is set).
1816  */
1817 static int tab_has_valid_sample(struct isl_tab *tab, isl_int *ineq, int eq)
1818 {
1819         int i;
1820         isl_int v;
1821
1822         if (!tab)
1823                 return -1;
1824
1825         isl_assert(tab->mat->ctx, tab->bmap, return -1);
1826         isl_assert(tab->mat->ctx, tab->samples, return -1);
1827         isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var, return -1);
1828
1829         isl_int_init(v);
1830         for (i = tab->n_outside; i < tab->n_sample; ++i) {
1831                 int sgn;
1832                 isl_seq_inner_product(ineq, tab->samples->row[i],
1833                                         1 + tab->n_var, &v);
1834                 sgn = isl_int_sgn(v);
1835                 if (eq ? (sgn == 0) : (sgn >= 0))
1836                         break;
1837         }
1838         isl_int_clear(v);
1839
1840         return i < tab->n_sample;
1841 }
1842
1843 /* Add a div specified by "div" to the tableau "tab" and return
1844  * 1 if the div is obviously non-negative.
1845  */
1846 static int context_tab_add_div(struct isl_tab *tab, struct isl_vec *div,
1847         int (*add_ineq)(void *user, isl_int *), void *user)
1848 {
1849         int i;
1850         int r;
1851         struct isl_mat *samples;
1852         int nonneg;
1853
1854         r = isl_tab_add_div(tab, div, add_ineq, user);
1855         if (r < 0)
1856                 return -1;
1857         nonneg = tab->var[r].is_nonneg;
1858         tab->var[r].frozen = 1;
1859
1860         samples = isl_mat_extend(tab->samples,
1861                         tab->n_sample, 1 + tab->n_var);
1862         tab->samples = samples;
1863         if (!samples)
1864                 return -1;
1865         for (i = tab->n_outside; i < samples->n_row; ++i) {
1866                 isl_seq_inner_product(div->el + 1, samples->row[i],
1867                         div->size - 1, &samples->row[i][samples->n_col - 1]);
1868                 isl_int_fdiv_q(samples->row[i][samples->n_col - 1],
1869                                samples->row[i][samples->n_col - 1], div->el[0]);
1870         }
1871
1872         return nonneg;
1873 }
1874
1875 /* Add a div specified by "div" to both the main tableau and
1876  * the context tableau.  In case of the main tableau, we only
1877  * need to add an extra div.  In the context tableau, we also
1878  * need to express the meaning of the div.
1879  * Return the index of the div or -1 if anything went wrong.
1880  */
1881 static int add_div(struct isl_tab *tab, struct isl_context *context,
1882         struct isl_vec *div)
1883 {
1884         int r;
1885         int nonneg;
1886
1887         if ((nonneg = context->op->add_div(context, div)) < 0)
1888                 goto error;
1889
1890         if (!context->op->is_ok(context))
1891                 goto error;
1892
1893         if (isl_tab_extend_vars(tab, 1) < 0)
1894                 goto error;
1895         r = isl_tab_allocate_var(tab);
1896         if (r < 0)
1897                 goto error;
1898         if (nonneg)
1899                 tab->var[r].is_nonneg = 1;
1900         tab->var[r].frozen = 1;
1901         tab->n_div++;
1902
1903         return tab->n_div - 1;
1904 error:
1905         context->op->invalidate(context);
1906         return -1;
1907 }
1908
1909 static int find_div(struct isl_tab *tab, isl_int *div, isl_int denom)
1910 {
1911         int i;
1912         unsigned total = isl_basic_map_total_dim(tab->bmap);
1913
1914         for (i = 0; i < tab->bmap->n_div; ++i) {
1915                 if (isl_int_ne(tab->bmap->div[i][0], denom))
1916                         continue;
1917                 if (!isl_seq_eq(tab->bmap->div[i] + 1, div, 1 + total))
1918                         continue;
1919                 return i;
1920         }
1921         return -1;
1922 }
1923
1924 /* Return the index of a div that corresponds to "div".
1925  * We first check if we already have such a div and if not, we create one.
1926  */
1927 static int get_div(struct isl_tab *tab, struct isl_context *context,
1928         struct isl_vec *div)
1929 {
1930         int d;
1931         struct isl_tab *context_tab = context->op->peek_tab(context);
1932
1933         if (!context_tab)
1934                 return -1;
1935
1936         d = find_div(context_tab, div->el + 1, div->el[0]);
1937         if (d != -1)
1938                 return d;
1939
1940         return add_div(tab, context, div);
1941 }
1942
1943 /* Add a parametric cut to cut away the non-integral sample value
1944  * of the give row.
1945  * Let a_i be the coefficients of the constant term and the parameters
1946  * and let b_i be the coefficients of the variables or constraints
1947  * in basis of the tableau.
1948  * Let q be the div q = floor(\sum_i {-a_i} y_i).
1949  *
1950  * The cut is expressed as
1951  *
1952  *      c = \sum_i -{-a_i} y_i + \sum_i {b_i} x_i + q >= 0
1953  *
1954  * If q did not already exist in the context tableau, then it is added first.
1955  * If q is in a column of the main tableau then the "+ q" can be accomplished
1956  * by setting the corresponding entry to the denominator of the constraint.
1957  * If q happens to be in a row of the main tableau, then the corresponding
1958  * row needs to be added instead (taking care of the denominators).
1959  * Note that this is very unlikely, but perhaps not entirely impossible.
1960  *
1961  * The current value of the cut is known to be negative (or at least
1962  * non-positive), so row_sign is set accordingly.
1963  *
1964  * Return the row of the cut or -1.
1965  */
1966 static int add_parametric_cut(struct isl_tab *tab, int row,
1967         struct isl_context *context)
1968 {
1969         struct isl_vec *div;
1970         int d;
1971         int i;
1972         int r;
1973         isl_int *r_row;
1974         int col;
1975         int n;
1976         unsigned off = 2 + tab->M;
1977
1978         if (!context)
1979                 return -1;
1980
1981         div = get_row_parameter_div(tab, row);
1982         if (!div)
1983                 return -1;
1984
1985         n = tab->n_div;
1986         d = context->op->get_div(context, tab, div);
1987         isl_vec_free(div);
1988         if (d < 0)
1989                 return -1;
1990
1991         if (isl_tab_extend_cons(tab, 1) < 0)
1992                 return -1;
1993         r = isl_tab_allocate_con(tab);
1994         if (r < 0)
1995                 return -1;
1996
1997         r_row = tab->mat->row[tab->con[r].index];
1998         isl_int_set(r_row[0], tab->mat->row[row][0]);
1999         isl_int_neg(r_row[1], tab->mat->row[row][1]);
2000         isl_int_fdiv_r(r_row[1], r_row[1], tab->mat->row[row][0]);
2001         isl_int_neg(r_row[1], r_row[1]);
2002         if (tab->M)
2003                 isl_int_set_si(r_row[2], 0);
2004         for (i = 0; i < tab->n_param; ++i) {
2005                 if (tab->var[i].is_row)
2006                         continue;
2007                 col = tab->var[i].index;
2008                 isl_int_neg(r_row[off + col], tab->mat->row[row][off + col]);
2009                 isl_int_fdiv_r(r_row[off + col], r_row[off + col],
2010                                 tab->mat->row[row][0]);
2011                 isl_int_neg(r_row[off + col], r_row[off + col]);
2012         }
2013         for (i = 0; i < tab->n_div; ++i) {
2014                 if (tab->var[tab->n_var - tab->n_div + i].is_row)
2015                         continue;
2016                 col = tab->var[tab->n_var - tab->n_div + i].index;
2017                 isl_int_neg(r_row[off + col], tab->mat->row[row][off + col]);
2018                 isl_int_fdiv_r(r_row[off + col], r_row[off + col],
2019                                 tab->mat->row[row][0]);
2020                 isl_int_neg(r_row[off + col], r_row[off + col]);
2021         }
2022         for (i = 0; i < tab->n_col; ++i) {
2023                 if (tab->col_var[i] >= 0 &&
2024                     (tab->col_var[i] < tab->n_param ||
2025                      tab->col_var[i] >= tab->n_var - tab->n_div))
2026                         continue;
2027                 isl_int_fdiv_r(r_row[off + i],
2028                         tab->mat->row[row][off + i], tab->mat->row[row][0]);
2029         }
2030         if (tab->var[tab->n_var - tab->n_div + d].is_row) {
2031                 isl_int gcd;
2032                 int d_row = tab->var[tab->n_var - tab->n_div + d].index;
2033                 isl_int_init(gcd);
2034                 isl_int_gcd(gcd, tab->mat->row[d_row][0], r_row[0]);
2035                 isl_int_divexact(r_row[0], r_row[0], gcd);
2036                 isl_int_divexact(gcd, tab->mat->row[d_row][0], gcd);
2037                 isl_seq_combine(r_row + 1, gcd, r_row + 1,
2038                                 r_row[0], tab->mat->row[d_row] + 1,
2039                                 off - 1 + tab->n_col);
2040                 isl_int_mul(r_row[0], r_row[0], tab->mat->row[d_row][0]);
2041                 isl_int_clear(gcd);
2042         } else {
2043                 col = tab->var[tab->n_var - tab->n_div + d].index;
2044                 isl_int_set(r_row[off + col], tab->mat->row[row][0]);
2045         }
2046
2047         tab->con[r].is_nonneg = 1;
2048         if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
2049                 return -1;
2050         if (tab->row_sign)
2051                 tab->row_sign[tab->con[r].index] = isl_tab_row_neg;
2052
2053         row = tab->con[r].index;
2054
2055         if (d >= n && context->op->detect_equalities(context, tab) < 0)
2056                 return -1;
2057
2058         return row;
2059 }
2060
2061 /* Construct a tableau for bmap that can be used for computing
2062  * the lexicographic minimum (or maximum) of bmap.
2063  * If not NULL, then dom is the domain where the minimum
2064  * should be computed.  In this case, we set up a parametric
2065  * tableau with row signs (initialized to "unknown").
2066  * If M is set, then the tableau will use a big parameter.
2067  * If max is set, then a maximum should be computed instead of a minimum.
2068  * This means that for each variable x, the tableau will contain the variable
2069  * x' = M - x, rather than x' = M + x.  This in turn means that the coefficient
2070  * of the variables in all constraints are negated prior to adding them
2071  * to the tableau.
2072  */
2073 static struct isl_tab *tab_for_lexmin(struct isl_basic_map *bmap,
2074         struct isl_basic_set *dom, unsigned M, int max)
2075 {
2076         int i;
2077         struct isl_tab *tab;
2078
2079         tab = isl_tab_alloc(bmap->ctx, 2 * bmap->n_eq + bmap->n_ineq + 1,
2080                             isl_basic_map_total_dim(bmap), M);
2081         if (!tab)
2082                 return NULL;
2083
2084         tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
2085         if (dom) {
2086                 tab->n_param = isl_basic_set_total_dim(dom) - dom->n_div;
2087                 tab->n_div = dom->n_div;
2088                 tab->row_sign = isl_calloc_array(bmap->ctx,
2089                                         enum isl_tab_row_sign, tab->mat->n_row);
2090                 if (!tab->row_sign)
2091                         goto error;
2092         }
2093         if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)) {
2094                 if (isl_tab_mark_empty(tab) < 0)
2095                         goto error;
2096                 return tab;
2097         }
2098
2099         for (i = tab->n_param; i < tab->n_var - tab->n_div; ++i) {
2100                 tab->var[i].is_nonneg = 1;
2101                 tab->var[i].frozen = 1;
2102         }
2103         for (i = 0; i < bmap->n_eq; ++i) {
2104                 if (max)
2105                         isl_seq_neg(bmap->eq[i] + 1 + tab->n_param,
2106                                     bmap->eq[i] + 1 + tab->n_param,
2107                                     tab->n_var - tab->n_param - tab->n_div);
2108                 tab = add_lexmin_valid_eq(tab, bmap->eq[i]);
2109                 if (max)
2110                         isl_seq_neg(bmap->eq[i] + 1 + tab->n_param,
2111                                     bmap->eq[i] + 1 + tab->n_param,
2112                                     tab->n_var - tab->n_param - tab->n_div);
2113                 if (!tab || tab->empty)
2114                         return tab;
2115         }
2116         if (bmap->n_eq && restore_lexmin(tab) < 0)
2117                 goto error;
2118         for (i = 0; i < bmap->n_ineq; ++i) {
2119                 if (max)
2120                         isl_seq_neg(bmap->ineq[i] + 1 + tab->n_param,
2121                                     bmap->ineq[i] + 1 + tab->n_param,
2122                                     tab->n_var - tab->n_param - tab->n_div);
2123                 tab = add_lexmin_ineq(tab, bmap->ineq[i]);
2124                 if (max)
2125                         isl_seq_neg(bmap->ineq[i] + 1 + tab->n_param,
2126                                     bmap->ineq[i] + 1 + tab->n_param,
2127                                     tab->n_var - tab->n_param - tab->n_div);
2128                 if (!tab || tab->empty)
2129                         return tab;
2130         }
2131         return tab;
2132 error:
2133         isl_tab_free(tab);
2134         return NULL;
2135 }
2136
2137 /* Given a main tableau where more than one row requires a split,
2138  * determine and return the "best" row to split on.
2139  *
2140  * Given two rows in the main tableau, if the inequality corresponding
2141  * to the first row is redundant with respect to that of the second row
2142  * in the current tableau, then it is better to split on the second row,
2143  * since in the positive part, both row will be positive.
2144  * (In the negative part a pivot will have to be performed and just about
2145  * anything can happen to the sign of the other row.)
2146  *
2147  * As a simple heuristic, we therefore select the row that makes the most
2148  * of the other rows redundant.
2149  *
2150  * Perhaps it would also be useful to look at the number of constraints
2151  * that conflict with any given constraint.
2152  */
2153 static int best_split(struct isl_tab *tab, struct isl_tab *context_tab)
2154 {
2155         struct isl_tab_undo *snap;
2156         int split;
2157         int row;
2158         int best = -1;
2159         int best_r;
2160
2161         if (isl_tab_extend_cons(context_tab, 2) < 0)
2162                 return -1;
2163
2164         snap = isl_tab_snap(context_tab);
2165
2166         for (split = tab->n_redundant; split < tab->n_row; ++split) {
2167                 struct isl_tab_undo *snap2;
2168                 struct isl_vec *ineq = NULL;
2169                 int r = 0;
2170                 int ok;
2171
2172                 if (!isl_tab_var_from_row(tab, split)->is_nonneg)
2173                         continue;
2174                 if (tab->row_sign[split] != isl_tab_row_any)
2175                         continue;
2176
2177                 ineq = get_row_parameter_ineq(tab, split);
2178                 if (!ineq)
2179                         return -1;
2180                 ok = isl_tab_add_ineq(context_tab, ineq->el) >= 0;
2181                 isl_vec_free(ineq);
2182                 if (!ok)
2183                         return -1;
2184
2185                 snap2 = isl_tab_snap(context_tab);
2186
2187                 for (row = tab->n_redundant; row < tab->n_row; ++row) {
2188                         struct isl_tab_var *var;
2189
2190                         if (row == split)
2191                                 continue;
2192                         if (!isl_tab_var_from_row(tab, row)->is_nonneg)
2193                                 continue;
2194                         if (tab->row_sign[row] != isl_tab_row_any)
2195                                 continue;
2196
2197                         ineq = get_row_parameter_ineq(tab, row);
2198                         if (!ineq)
2199                                 return -1;
2200                         ok = isl_tab_add_ineq(context_tab, ineq->el) >= 0;
2201                         isl_vec_free(ineq);
2202                         if (!ok)
2203                                 return -1;
2204                         var = &context_tab->con[context_tab->n_con - 1];
2205                         if (!context_tab->empty &&
2206                             !isl_tab_min_at_most_neg_one(context_tab, var))
2207                                 r++;
2208                         if (isl_tab_rollback(context_tab, snap2) < 0)
2209                                 return -1;
2210                 }
2211                 if (best == -1 || r > best_r) {
2212                         best = split;
2213                         best_r = r;
2214                 }
2215                 if (isl_tab_rollback(context_tab, snap) < 0)
2216                         return -1;
2217         }
2218
2219         return best;
2220 }
2221
2222 static struct isl_basic_set *context_lex_peek_basic_set(
2223         struct isl_context *context)
2224 {
2225         struct isl_context_lex *clex = (struct isl_context_lex *)context;
2226         if (!clex->tab)
2227                 return NULL;
2228         return isl_tab_peek_bset(clex->tab);
2229 }
2230
2231 static struct isl_tab *context_lex_peek_tab(struct isl_context *context)
2232 {
2233         struct isl_context_lex *clex = (struct isl_context_lex *)context;
2234         return clex->tab;
2235 }
2236
2237 static void context_lex_add_eq(struct isl_context *context, isl_int *eq,
2238                 int check, int update)
2239 {
2240         struct isl_context_lex *clex = (struct isl_context_lex *)context;
2241         if (isl_tab_extend_cons(clex->tab, 2) < 0)
2242                 goto error;
2243         if (add_lexmin_eq(clex->tab, eq) < 0)
2244                 goto error;
2245         if (check) {
2246                 int v = tab_has_valid_sample(clex->tab, eq, 1);
2247                 if (v < 0)
2248                         goto error;
2249                 if (!v)
2250                         clex->tab = check_integer_feasible(clex->tab);
2251         }
2252         if (update)
2253                 clex->tab = check_samples(clex->tab, eq, 1);
2254         return;
2255 error:
2256         isl_tab_free(clex->tab);
2257         clex->tab = NULL;
2258 }
2259
2260 static void context_lex_add_ineq(struct isl_context *context, isl_int *ineq,
2261                 int check, int update)
2262 {
2263         struct isl_context_lex *clex = (struct isl_context_lex *)context;
2264         if (isl_tab_extend_cons(clex->tab, 1) < 0)
2265                 goto error;
2266         clex->tab = add_lexmin_ineq(clex->tab, ineq);
2267         if (check) {
2268                 int v = tab_has_valid_sample(clex->tab, ineq, 0);
2269                 if (v < 0)
2270                         goto error;
2271                 if (!v)
2272                         clex->tab = check_integer_feasible(clex->tab);
2273         }
2274         if (update)
2275                 clex->tab = check_samples(clex->tab, ineq, 0);
2276         return;
2277 error:
2278         isl_tab_free(clex->tab);
2279         clex->tab = NULL;
2280 }
2281
2282 static int context_lex_add_ineq_wrap(void *user, isl_int *ineq)
2283 {
2284         struct isl_context *context = (struct isl_context *)user;
2285         context_lex_add_ineq(context, ineq, 0, 0);
2286         return context->op->is_ok(context) ? 0 : -1;
2287 }
2288
2289 /* Check which signs can be obtained by "ineq" on all the currently
2290  * active sample values.  See row_sign for more information.
2291  */
2292 static enum isl_tab_row_sign tab_ineq_sign(struct isl_tab *tab, isl_int *ineq,
2293         int strict)
2294 {
2295         int i;
2296         int sgn;
2297         isl_int tmp;
2298         enum isl_tab_row_sign res = isl_tab_row_unknown;
2299
2300         isl_assert(tab->mat->ctx, tab->samples, return isl_tab_row_unknown);
2301         isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var,
2302                         return isl_tab_row_unknown);
2303
2304         isl_int_init(tmp);
2305         for (i = tab->n_outside; i < tab->n_sample; ++i) {
2306                 isl_seq_inner_product(tab->samples->row[i], ineq,
2307                                         1 + tab->n_var, &tmp);
2308                 sgn = isl_int_sgn(tmp);
2309                 if (sgn > 0 || (sgn == 0 && strict)) {
2310                         if (res == isl_tab_row_unknown)
2311                                 res = isl_tab_row_pos;
2312                         if (res == isl_tab_row_neg)
2313                                 res = isl_tab_row_any;
2314                 }
2315                 if (sgn < 0) {
2316                         if (res == isl_tab_row_unknown)
2317                                 res = isl_tab_row_neg;
2318                         if (res == isl_tab_row_pos)
2319                                 res = isl_tab_row_any;
2320                 }
2321                 if (res == isl_tab_row_any)
2322                         break;
2323         }
2324         isl_int_clear(tmp);
2325
2326         return res;
2327 }
2328
2329 static enum isl_tab_row_sign context_lex_ineq_sign(struct isl_context *context,
2330                         isl_int *ineq, int strict)
2331 {
2332         struct isl_context_lex *clex = (struct isl_context_lex *)context;
2333         return tab_ineq_sign(clex->tab, ineq, strict);
2334 }
2335
2336 /* Check whether "ineq" can be added to the tableau without rendering
2337  * it infeasible.
2338  */
2339 static int context_lex_test_ineq(struct isl_context *context, isl_int *ineq)
2340 {
2341         struct isl_context_lex *clex = (struct isl_context_lex *)context;
2342         struct isl_tab_undo *snap;
2343         int feasible;
2344
2345         if (!clex->tab)
2346                 return -1;
2347
2348         if (isl_tab_extend_cons(clex->tab, 1) < 0)
2349                 return -1;
2350
2351         snap = isl_tab_snap(clex->tab);
2352         if (isl_tab_push_basis(clex->tab) < 0)
2353                 return -1;
2354         clex->tab = add_lexmin_ineq(clex->tab, ineq);
2355         clex->tab = check_integer_feasible(clex->tab);
2356         if (!clex->tab)
2357                 return -1;
2358         feasible = !clex->tab->empty;
2359         if (isl_tab_rollback(clex->tab, snap) < 0)
2360                 return -1;
2361
2362         return feasible;
2363 }
2364
2365 static int context_lex_get_div(struct isl_context *context, struct isl_tab *tab,
2366                 struct isl_vec *div)
2367 {
2368         return get_div(tab, context, div);
2369 }
2370
2371 /* Add a div specified by "div" to the context tableau and return
2372  * 1 if the div is obviously non-negative.
2373  * context_tab_add_div will always return 1, because all variables
2374  * in a isl_context_lex tableau are non-negative.
2375  * However, if we are using a big parameter in the context, then this only
2376  * reflects the non-negativity of the variable used to _encode_ the
2377  * div, i.e., div' = M + div, so we can't draw any conclusions.
2378  */
2379 static int context_lex_add_div(struct isl_context *context, struct isl_vec *div)
2380 {
2381         struct isl_context_lex *clex = (struct isl_context_lex *)context;
2382         int nonneg;
2383         nonneg = context_tab_add_div(clex->tab, div,
2384                                         context_lex_add_ineq_wrap, context);
2385         if (nonneg < 0)
2386                 return -1;
2387         if (clex->tab->M)
2388                 return 0;
2389         return nonneg;
2390 }
2391
2392 static int context_lex_detect_equalities(struct isl_context *context,
2393                 struct isl_tab *tab)
2394 {
2395         return 0;
2396 }
2397
2398 static int context_lex_best_split(struct isl_context *context,
2399                 struct isl_tab *tab)
2400 {
2401         struct isl_context_lex *clex = (struct isl_context_lex *)context;
2402         struct isl_tab_undo *snap;
2403         int r;
2404
2405         snap = isl_tab_snap(clex->tab);
2406         if (isl_tab_push_basis(clex->tab) < 0)
2407                 return -1;
2408         r = best_split(tab, clex->tab);
2409
2410         if (r >= 0 && isl_tab_rollback(clex->tab, snap) < 0)
2411                 return -1;
2412
2413         return r;
2414 }
2415
2416 static int context_lex_is_empty(struct isl_context *context)
2417 {
2418         struct isl_context_lex *clex = (struct isl_context_lex *)context;
2419         if (!clex->tab)
2420                 return -1;
2421         return clex->tab->empty;
2422 }
2423
2424 static void *context_lex_save(struct isl_context *context)
2425 {
2426         struct isl_context_lex *clex = (struct isl_context_lex *)context;
2427         struct isl_tab_undo *snap;
2428
2429         snap = isl_tab_snap(clex->tab);
2430         if (isl_tab_push_basis(clex->tab) < 0)
2431                 return NULL;
2432         if (isl_tab_save_samples(clex->tab) < 0)
2433                 return NULL;
2434
2435         return snap;
2436 }
2437
2438 static void context_lex_restore(struct isl_context *context, void *save)
2439 {
2440         struct isl_context_lex *clex = (struct isl_context_lex *)context;
2441         if (isl_tab_rollback(clex->tab, (struct isl_tab_undo *)save) < 0) {
2442                 isl_tab_free(clex->tab);
2443                 clex->tab = NULL;
2444         }
2445 }
2446
2447 static void context_lex_discard(void *save)
2448 {
2449 }
2450
2451 static int context_lex_is_ok(struct isl_context *context)
2452 {
2453         struct isl_context_lex *clex = (struct isl_context_lex *)context;
2454         return !!clex->tab;
2455 }
2456
2457 /* For each variable in the context tableau, check if the variable can
2458  * only attain non-negative values.  If so, mark the parameter as non-negative
2459  * in the main tableau.  This allows for a more direct identification of some
2460  * cases of violated constraints.
2461  */
2462 static struct isl_tab *tab_detect_nonnegative_parameters(struct isl_tab *tab,
2463         struct isl_tab *context_tab)
2464 {
2465         int i;
2466         struct isl_tab_undo *snap;
2467         struct isl_vec *ineq = NULL;
2468         struct isl_tab_var *var;
2469         int n;
2470
2471         if (context_tab->n_var == 0)
2472                 return tab;
2473
2474         ineq = isl_vec_alloc(tab->mat->ctx, 1 + context_tab->n_var);
2475         if (!ineq)
2476                 goto error;
2477
2478         if (isl_tab_extend_cons(context_tab, 1) < 0)
2479                 goto error;
2480
2481         snap = isl_tab_snap(context_tab);
2482
2483         n = 0;
2484         isl_seq_clr(ineq->el, ineq->size);
2485         for (i = 0; i < context_tab->n_var; ++i) {
2486                 isl_int_set_si(ineq->el[1 + i], 1);
2487                 if (isl_tab_add_ineq(context_tab, ineq->el) < 0)
2488                         goto error;
2489                 var = &context_tab->con[context_tab->n_con - 1];
2490                 if (!context_tab->empty &&
2491                     !isl_tab_min_at_most_neg_one(context_tab, var)) {
2492                         int j = i;
2493                         if (i >= tab->n_param)
2494                                 j = i - tab->n_param + tab->n_var - tab->n_div;
2495                         tab->var[j].is_nonneg = 1;
2496                         n++;
2497                 }
2498                 isl_int_set_si(ineq->el[1 + i], 0);
2499                 if (isl_tab_rollback(context_tab, snap) < 0)
2500                         goto error;
2501         }
2502
2503         if (context_tab->M && n == context_tab->n_var) {
2504                 context_tab->mat = isl_mat_drop_cols(context_tab->mat, 2, 1);
2505                 context_tab->M = 0;
2506         }
2507
2508         isl_vec_free(ineq);
2509         return tab;
2510 error:
2511         isl_vec_free(ineq);
2512         isl_tab_free(tab);
2513         return NULL;
2514 }
2515
2516 static struct isl_tab *context_lex_detect_nonnegative_parameters(
2517         struct isl_context *context, struct isl_tab *tab)
2518 {
2519         struct isl_context_lex *clex = (struct isl_context_lex *)context;
2520         struct isl_tab_undo *snap;
2521
2522         if (!tab)
2523                 return NULL;
2524
2525         snap = isl_tab_snap(clex->tab);
2526         if (isl_tab_push_basis(clex->tab) < 0)
2527                 goto error;
2528
2529         tab = tab_detect_nonnegative_parameters(tab, clex->tab);
2530
2531         if (isl_tab_rollback(clex->tab, snap) < 0)
2532                 goto error;
2533
2534         return tab;
2535 error:
2536         isl_tab_free(tab);
2537         return NULL;
2538 }
2539
2540 static void context_lex_invalidate(struct isl_context *context)
2541 {
2542         struct isl_context_lex *clex = (struct isl_context_lex *)context;
2543         isl_tab_free(clex->tab);
2544         clex->tab = NULL;
2545 }
2546
2547 static void context_lex_free(struct isl_context *context)
2548 {
2549         struct isl_context_lex *clex = (struct isl_context_lex *)context;
2550         isl_tab_free(clex->tab);
2551         free(clex);
2552 }
2553
2554 struct isl_context_op isl_context_lex_op = {
2555         context_lex_detect_nonnegative_parameters,
2556         context_lex_peek_basic_set,
2557         context_lex_peek_tab,
2558         context_lex_add_eq,
2559         context_lex_add_ineq,
2560         context_lex_ineq_sign,
2561         context_lex_test_ineq,
2562         context_lex_get_div,
2563         context_lex_add_div,
2564         context_lex_detect_equalities,
2565         context_lex_best_split,
2566         context_lex_is_empty,
2567         context_lex_is_ok,
2568         context_lex_save,
2569         context_lex_restore,
2570         context_lex_discard,
2571         context_lex_invalidate,
2572         context_lex_free,
2573 };
2574
2575 static struct isl_tab *context_tab_for_lexmin(struct isl_basic_set *bset)
2576 {
2577         struct isl_tab *tab;
2578
2579         if (!bset)
2580                 return NULL;
2581         tab = tab_for_lexmin((struct isl_basic_map *)bset, NULL, 1, 0);
2582         if (!tab)
2583                 goto error;
2584         if (isl_tab_track_bset(tab, bset) < 0)
2585                 goto error;
2586         tab = isl_tab_init_samples(tab);
2587         return tab;
2588 error:
2589         isl_basic_set_free(bset);
2590         return NULL;
2591 }
2592
2593 static struct isl_context *isl_context_lex_alloc(struct isl_basic_set *dom)
2594 {
2595         struct isl_context_lex *clex;
2596
2597         if (!dom)
2598                 return NULL;
2599
2600         clex = isl_alloc_type(dom->ctx, struct isl_context_lex);
2601         if (!clex)
2602                 return NULL;
2603
2604         clex->context.op = &isl_context_lex_op;
2605
2606         clex->tab = context_tab_for_lexmin(isl_basic_set_copy(dom));
2607         if (restore_lexmin(clex->tab) < 0)
2608                 goto error;
2609         clex->tab = check_integer_feasible(clex->tab);
2610         if (!clex->tab)
2611                 goto error;
2612
2613         return &clex->context;
2614 error:
2615         clex->context.op->free(&clex->context);
2616         return NULL;
2617 }
2618
2619 /* Representation of the context when using generalized basis reduction.
2620  *
2621  * "shifted" contains the offsets of the unit hypercubes that lie inside the
2622  * context.  Any rational point in "shifted" can therefore be rounded
2623  * up to an integer point in the context.
2624  * If the context is constrained by any equality, then "shifted" is not used
2625  * as it would be empty.
2626  */
2627 struct isl_context_gbr {
2628         struct isl_context context;
2629         struct isl_tab *tab;
2630         struct isl_tab *shifted;
2631         struct isl_tab *cone;
2632 };
2633
2634 static struct isl_tab *context_gbr_detect_nonnegative_parameters(
2635         struct isl_context *context, struct isl_tab *tab)
2636 {
2637         struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2638         if (!tab)
2639                 return NULL;
2640         return tab_detect_nonnegative_parameters(tab, cgbr->tab);
2641 }
2642
2643 static struct isl_basic_set *context_gbr_peek_basic_set(
2644         struct isl_context *context)
2645 {
2646         struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2647         if (!cgbr->tab)
2648                 return NULL;
2649         return isl_tab_peek_bset(cgbr->tab);
2650 }
2651
2652 static struct isl_tab *context_gbr_peek_tab(struct isl_context *context)
2653 {
2654         struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2655         return cgbr->tab;
2656 }
2657
2658 /* Initialize the "shifted" tableau of the context, which
2659  * contains the constraints of the original tableau shifted
2660  * by the sum of all negative coefficients.  This ensures
2661  * that any rational point in the shifted tableau can
2662  * be rounded up to yield an integer point in the original tableau.
2663  */
2664 static void gbr_init_shifted(struct isl_context_gbr *cgbr)
2665 {
2666         int i, j;
2667         struct isl_vec *cst;
2668         struct isl_basic_set *bset = isl_tab_peek_bset(cgbr->tab);
2669         unsigned dim = isl_basic_set_total_dim(bset);
2670
2671         cst = isl_vec_alloc(cgbr->tab->mat->ctx, bset->n_ineq);
2672         if (!cst)
2673                 return;
2674
2675         for (i = 0; i < bset->n_ineq; ++i) {
2676                 isl_int_set(cst->el[i], bset->ineq[i][0]);
2677                 for (j = 0; j < dim; ++j) {
2678                         if (!isl_int_is_neg(bset->ineq[i][1 + j]))
2679                                 continue;
2680                         isl_int_add(bset->ineq[i][0], bset->ineq[i][0],
2681                                     bset->ineq[i][1 + j]);
2682                 }
2683         }
2684
2685         cgbr->shifted = isl_tab_from_basic_set(bset, 0);
2686
2687         for (i = 0; i < bset->n_ineq; ++i)
2688                 isl_int_set(bset->ineq[i][0], cst->el[i]);
2689
2690         isl_vec_free(cst);
2691 }
2692
2693 /* Check if the shifted tableau is non-empty, and if so
2694  * use the sample point to construct an integer point
2695  * of the context tableau.
2696  */
2697 static struct isl_vec *gbr_get_shifted_sample(struct isl_context_gbr *cgbr)
2698 {
2699         struct isl_vec *sample;
2700
2701         if (!cgbr->shifted)
2702                 gbr_init_shifted(cgbr);
2703         if (!cgbr->shifted)
2704                 return NULL;
2705         if (cgbr->shifted->empty)
2706                 return isl_vec_alloc(cgbr->tab->mat->ctx, 0);
2707
2708         sample = isl_tab_get_sample_value(cgbr->shifted);
2709         sample = isl_vec_ceil(sample);
2710
2711         return sample;
2712 }
2713
2714 static struct isl_basic_set *drop_constant_terms(struct isl_basic_set *bset)
2715 {
2716         int i;
2717
2718         if (!bset)
2719                 return NULL;
2720
2721         for (i = 0; i < bset->n_eq; ++i)
2722                 isl_int_set_si(bset->eq[i][0], 0);
2723
2724         for (i = 0; i < bset->n_ineq; ++i)
2725                 isl_int_set_si(bset->ineq[i][0], 0);
2726
2727         return bset;
2728 }
2729
2730 static int use_shifted(struct isl_context_gbr *cgbr)
2731 {
2732         if (!cgbr->tab)
2733                 return 0;
2734         return cgbr->tab->bmap->n_eq == 0 && cgbr->tab->bmap->n_div == 0;
2735 }
2736
2737 static struct isl_vec *gbr_get_sample(struct isl_context_gbr *cgbr)
2738 {
2739         struct isl_basic_set *bset;
2740         struct isl_basic_set *cone;
2741
2742         if (isl_tab_sample_is_integer(cgbr->tab))
2743                 return isl_tab_get_sample_value(cgbr->tab);
2744
2745         if (use_shifted(cgbr)) {
2746                 struct isl_vec *sample;
2747
2748                 sample = gbr_get_shifted_sample(cgbr);
2749                 if (!sample || sample->size > 0)
2750                         return sample;
2751
2752                 isl_vec_free(sample);
2753         }
2754
2755         if (!cgbr->cone) {
2756                 bset = isl_tab_peek_bset(cgbr->tab);
2757                 cgbr->cone = isl_tab_from_recession_cone(bset, 0);
2758                 if (!cgbr->cone)
2759                         return NULL;
2760                 if (isl_tab_track_bset(cgbr->cone,
2761                                         isl_basic_set_copy(bset)) < 0)
2762                         return NULL;
2763         }
2764         if (isl_tab_detect_implicit_equalities(cgbr->cone) < 0)
2765                 return NULL;
2766
2767         if (cgbr->cone->n_dead == cgbr->cone->n_col) {
2768                 struct isl_vec *sample;
2769                 struct isl_tab_undo *snap;
2770
2771                 if (cgbr->tab->basis) {
2772                         if (cgbr->tab->basis->n_col != 1 + cgbr->tab->n_var) {
2773                                 isl_mat_free(cgbr->tab->basis);
2774                                 cgbr->tab->basis = NULL;
2775                         }
2776                         cgbr->tab->n_zero = 0;
2777                         cgbr->tab->n_unbounded = 0;
2778                 }
2779
2780                 snap = isl_tab_snap(cgbr->tab);
2781
2782                 sample = isl_tab_sample(cgbr->tab);
2783
2784                 if (isl_tab_rollback(cgbr->tab, snap) < 0) {
2785                         isl_vec_free(sample);
2786                         return NULL;
2787                 }
2788
2789                 return sample;
2790         }
2791
2792         cone = isl_basic_set_dup(isl_tab_peek_bset(cgbr->cone));
2793         cone = drop_constant_terms(cone);
2794         cone = isl_basic_set_update_from_tab(cone, cgbr->cone);
2795         cone = isl_basic_set_underlying_set(cone);
2796         cone = isl_basic_set_gauss(cone, NULL);
2797
2798         bset = isl_basic_set_dup(isl_tab_peek_bset(cgbr->tab));
2799         bset = isl_basic_set_update_from_tab(bset, cgbr->tab);
2800         bset = isl_basic_set_underlying_set(bset);
2801         bset = isl_basic_set_gauss(bset, NULL);
2802
2803         return isl_basic_set_sample_with_cone(bset, cone);
2804 }
2805
2806 static void check_gbr_integer_feasible(struct isl_context_gbr *cgbr)
2807 {
2808         struct isl_vec *sample;
2809
2810         if (!cgbr->tab)
2811                 return;
2812
2813         if (cgbr->tab->empty)
2814                 return;
2815
2816         sample = gbr_get_sample(cgbr);
2817         if (!sample)
2818                 goto error;
2819
2820         if (sample->size == 0) {
2821                 isl_vec_free(sample);
2822                 if (isl_tab_mark_empty(cgbr->tab) < 0)
2823                         goto error;
2824                 return;
2825         }
2826
2827         cgbr->tab = isl_tab_add_sample(cgbr->tab, sample);
2828
2829         return;
2830 error:
2831         isl_tab_free(cgbr->tab);
2832         cgbr->tab = NULL;
2833 }
2834
2835 static struct isl_tab *add_gbr_eq(struct isl_tab *tab, isl_int *eq)
2836 {
2837         if (!tab)
2838                 return NULL;
2839
2840         if (isl_tab_extend_cons(tab, 2) < 0)
2841                 goto error;
2842
2843         if (isl_tab_add_eq(tab, eq) < 0)
2844                 goto error;
2845
2846         return tab;
2847 error:
2848         isl_tab_free(tab);
2849         return NULL;
2850 }
2851
2852 /* Add the equality described by "eq" to the context.
2853  * If "check" is set, then we check if the context is empty after
2854  * adding the equality.
2855  * If "update" is set, then we check if the samples are still valid.
2856  *
2857  * We do not explicitly add shifted copies of the equality to
2858  * cgbr->shifted since they would conflict with each other.
2859  * Instead, we directly mark cgbr->shifted empty.
2860  */
2861 static void context_gbr_add_eq(struct isl_context *context, isl_int *eq,
2862                 int check, int update)
2863 {
2864         struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2865
2866         cgbr->tab = add_gbr_eq(cgbr->tab, eq);
2867
2868         if (cgbr->shifted && !cgbr->shifted->empty && use_shifted(cgbr)) {
2869                 if (isl_tab_mark_empty(cgbr->shifted) < 0)
2870                         goto error;
2871         }
2872
2873         if (cgbr->cone && cgbr->cone->n_col != cgbr->cone->n_dead) {
2874                 if (isl_tab_extend_cons(cgbr->cone, 2) < 0)
2875                         goto error;
2876                 if (isl_tab_add_eq(cgbr->cone, eq) < 0)
2877                         goto error;
2878         }
2879
2880         if (check) {
2881                 int v = tab_has_valid_sample(cgbr->tab, eq, 1);
2882                 if (v < 0)
2883                         goto error;
2884                 if (!v)
2885                         check_gbr_integer_feasible(cgbr);
2886         }
2887         if (update)
2888                 cgbr->tab = check_samples(cgbr->tab, eq, 1);
2889         return;
2890 error:
2891         isl_tab_free(cgbr->tab);
2892         cgbr->tab = NULL;
2893 }
2894
2895 static void add_gbr_ineq(struct isl_context_gbr *cgbr, isl_int *ineq)
2896 {
2897         if (!cgbr->tab)
2898                 return;
2899
2900         if (isl_tab_extend_cons(cgbr->tab, 1) < 0)
2901                 goto error;
2902
2903         if (isl_tab_add_ineq(cgbr->tab, ineq) < 0)
2904                 goto error;
2905
2906         if (cgbr->shifted && !cgbr->shifted->empty && use_shifted(cgbr)) {
2907                 int i;
2908                 unsigned dim;
2909                 dim = isl_basic_map_total_dim(cgbr->tab->bmap);
2910
2911                 if (isl_tab_extend_cons(cgbr->shifted, 1) < 0)
2912                         goto error;
2913
2914                 for (i = 0; i < dim; ++i) {
2915                         if (!isl_int_is_neg(ineq[1 + i]))
2916                                 continue;
2917                         isl_int_add(ineq[0], ineq[0], ineq[1 + i]);
2918                 }
2919
2920                 if (isl_tab_add_ineq(cgbr->shifted, ineq) < 0)
2921                         goto error;
2922
2923                 for (i = 0; i < dim; ++i) {
2924                         if (!isl_int_is_neg(ineq[1 + i]))
2925                                 continue;
2926                         isl_int_sub(ineq[0], ineq[0], ineq[1 + i]);
2927                 }
2928         }
2929
2930         if (cgbr->cone && cgbr->cone->n_col != cgbr->cone->n_dead) {
2931                 if (isl_tab_extend_cons(cgbr->cone, 1) < 0)
2932                         goto error;
2933                 if (isl_tab_add_ineq(cgbr->cone, ineq) < 0)
2934                         goto error;
2935         }
2936
2937         return;
2938 error:
2939         isl_tab_free(cgbr->tab);
2940         cgbr->tab = NULL;
2941 }
2942
2943 static void context_gbr_add_ineq(struct isl_context *context, isl_int *ineq,
2944                 int check, int update)
2945 {
2946         struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2947
2948         add_gbr_ineq(cgbr, ineq);
2949         if (!cgbr->tab)
2950                 return;
2951
2952         if (check) {
2953                 int v = tab_has_valid_sample(cgbr->tab, ineq, 0);
2954                 if (v < 0)
2955                         goto error;
2956                 if (!v)
2957                         check_gbr_integer_feasible(cgbr);
2958         }
2959         if (update)
2960                 cgbr->tab = check_samples(cgbr->tab, ineq, 0);
2961         return;
2962 error:
2963         isl_tab_free(cgbr->tab);
2964         cgbr->tab = NULL;
2965 }
2966
2967 static int context_gbr_add_ineq_wrap(void *user, isl_int *ineq)
2968 {
2969         struct isl_context *context = (struct isl_context *)user;
2970         context_gbr_add_ineq(context, ineq, 0, 0);
2971         return context->op->is_ok(context) ? 0 : -1;
2972 }
2973
2974 static enum isl_tab_row_sign context_gbr_ineq_sign(struct isl_context *context,
2975                         isl_int *ineq, int strict)
2976 {
2977         struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2978         return tab_ineq_sign(cgbr->tab, ineq, strict);
2979 }
2980
2981 /* Check whether "ineq" can be added to the tableau without rendering
2982  * it infeasible.
2983  */
2984 static int context_gbr_test_ineq(struct isl_context *context, isl_int *ineq)
2985 {
2986         struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2987         struct isl_tab_undo *snap;
2988         struct isl_tab_undo *shifted_snap = NULL;
2989         struct isl_tab_undo *cone_snap = NULL;
2990         int feasible;
2991
2992         if (!cgbr->tab)
2993                 return -1;
2994
2995         if (isl_tab_extend_cons(cgbr->tab, 1) < 0)
2996                 return -1;
2997
2998         snap = isl_tab_snap(cgbr->tab);
2999         if (cgbr->shifted)
3000                 shifted_snap = isl_tab_snap(cgbr->shifted);
3001         if (cgbr->cone)
3002                 cone_snap = isl_tab_snap(cgbr->cone);
3003         add_gbr_ineq(cgbr, ineq);
3004         check_gbr_integer_feasible(cgbr);
3005         if (!cgbr->tab)
3006                 return -1;
3007         feasible = !cgbr->tab->empty;
3008         if (isl_tab_rollback(cgbr->tab, snap) < 0)
3009                 return -1;
3010         if (shifted_snap) {
3011                 if (isl_tab_rollback(cgbr->shifted, shifted_snap))
3012                         return -1;
3013         } else if (cgbr->shifted) {
3014                 isl_tab_free(cgbr->shifted);
3015                 cgbr->shifted = NULL;
3016         }
3017         if (cone_snap) {
3018                 if (isl_tab_rollback(cgbr->cone, cone_snap))
3019                         return -1;
3020         } else if (cgbr->cone) {
3021                 isl_tab_free(cgbr->cone);
3022                 cgbr->cone = NULL;
3023         }
3024
3025         return feasible;
3026 }
3027
3028 /* Return the column of the last of the variables associated to
3029  * a column that has a non-zero coefficient.
3030  * This function is called in a context where only coefficients
3031  * of parameters or divs can be non-zero.
3032  */
3033 static int last_non_zero_var_col(struct isl_tab *tab, isl_int *p)
3034 {
3035         int i;
3036         int col;
3037
3038         if (tab->n_var == 0)
3039                 return -1;
3040
3041         for (i = tab->n_var - 1; i >= 0; --i) {
3042                 if (i >= tab->n_param && i < tab->n_var - tab->n_div)
3043                         continue;
3044                 if (tab->var[i].is_row)
3045                         continue;
3046                 col = tab->var[i].index;
3047                 if (!isl_int_is_zero(p[col]))
3048                         return col;
3049         }
3050
3051         return -1;
3052 }
3053
3054 /* Look through all the recently added equalities in the context
3055  * to see if we can propagate any of them to the main tableau.
3056  *
3057  * The newly added equalities in the context are encoded as pairs
3058  * of inequalities starting at inequality "first".
3059  *
3060  * We tentatively add each of these equalities to the main tableau
3061  * and if this happens to result in a row with a final coefficient
3062  * that is one or negative one, we use it to kill a column
3063  * in the main tableau.  Otherwise, we discard the tentatively
3064  * added row.
3065  */
3066 static void propagate_equalities(struct isl_context_gbr *cgbr,
3067         struct isl_tab *tab, unsigned first)
3068 {
3069         int i;
3070         struct isl_vec *eq = NULL;
3071
3072         eq = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
3073         if (!eq)
3074                 goto error;
3075
3076         if (isl_tab_extend_cons(tab, (cgbr->tab->bmap->n_ineq - first)/2) < 0)
3077                 goto error;
3078
3079         isl_seq_clr(eq->el + 1 + tab->n_param,
3080                     tab->n_var - tab->n_param - tab->n_div);
3081         for (i = first; i < cgbr->tab->bmap->n_ineq; i += 2) {
3082                 int j;
3083                 int r;
3084                 struct isl_tab_undo *snap;
3085                 snap = isl_tab_snap(tab);
3086
3087                 isl_seq_cpy(eq->el, cgbr->tab->bmap->ineq[i], 1 + tab->n_param);
3088                 isl_seq_cpy(eq->el + 1 + tab->n_var - tab->n_div,
3089                             cgbr->tab->bmap->ineq[i] + 1 + tab->n_param,
3090                             tab->n_div);
3091
3092                 r = isl_tab_add_row(tab, eq->el);
3093                 if (r < 0)
3094                         goto error;
3095                 r = tab->con[r].index;
3096                 j = last_non_zero_var_col(tab, tab->mat->row[r] + 2 + tab->M);
3097                 if (j < 0 || j < tab->n_dead ||
3098                     !isl_int_is_one(tab->mat->row[r][0]) ||
3099                     (!isl_int_is_one(tab->mat->row[r][2 + tab->M + j]) &&
3100                      !isl_int_is_negone(tab->mat->row[r][2 + tab->M + j]))) {
3101                         if (isl_tab_rollback(tab, snap) < 0)
3102                                 goto error;
3103                         continue;
3104                 }
3105                 if (isl_tab_pivot(tab, r, j) < 0)
3106                         goto error;
3107                 if (isl_tab_kill_col(tab, j) < 0)
3108                         goto error;
3109
3110                 if (restore_lexmin(tab) < 0)
3111                         goto error;
3112         }
3113
3114         isl_vec_free(eq);
3115
3116         return;
3117 error:
3118         isl_vec_free(eq);
3119         isl_tab_free(cgbr->tab);
3120         cgbr->tab = NULL;
3121 }
3122
3123 static int context_gbr_detect_equalities(struct isl_context *context,
3124         struct isl_tab *tab)
3125 {
3126         struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3127         struct isl_ctx *ctx;
3128         unsigned n_ineq;
3129
3130         ctx = cgbr->tab->mat->ctx;
3131
3132         if (!cgbr->cone) {
3133                 struct isl_basic_set *bset = isl_tab_peek_bset(cgbr->tab);
3134                 cgbr->cone = isl_tab_from_recession_cone(bset, 0);
3135                 if (!cgbr->cone)
3136                         goto error;
3137                 if (isl_tab_track_bset(cgbr->cone,
3138                                         isl_basic_set_copy(bset)) < 0)
3139                         goto error;
3140         }
3141         if (isl_tab_detect_implicit_equalities(cgbr->cone) < 0)
3142                 goto error;
3143
3144         n_ineq = cgbr->tab->bmap->n_ineq;
3145         cgbr->tab = isl_tab_detect_equalities(cgbr->tab, cgbr->cone);
3146         if (!cgbr->tab)
3147                 return -1;
3148         if (cgbr->tab->bmap->n_ineq > n_ineq)
3149                 propagate_equalities(cgbr, tab, n_ineq);
3150
3151         return 0;
3152 error:
3153         isl_tab_free(cgbr->tab);
3154         cgbr->tab = NULL;
3155         return -1;
3156 }
3157
3158 static int context_gbr_get_div(struct isl_context *context, struct isl_tab *tab,
3159                 struct isl_vec *div)
3160 {
3161         return get_div(tab, context, div);
3162 }
3163
3164 static int context_gbr_add_div(struct isl_context *context, struct isl_vec *div)
3165 {
3166         struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3167         if (cgbr->cone) {
3168                 int k;
3169
3170                 if (isl_tab_extend_cons(cgbr->cone, 3) < 0)
3171                         return -1;
3172                 if (isl_tab_extend_vars(cgbr->cone, 1) < 0)
3173                         return -1;
3174                 if (isl_tab_allocate_var(cgbr->cone) <0)
3175                         return -1;
3176
3177                 cgbr->cone->bmap = isl_basic_map_extend_space(cgbr->cone->bmap,
3178                         isl_basic_map_get_space(cgbr->cone->bmap), 1, 0, 2);
3179                 k = isl_basic_map_alloc_div(cgbr->cone->bmap);
3180                 if (k < 0)
3181                         return -1;
3182                 isl_seq_cpy(cgbr->cone->bmap->div[k], div->el, div->size);
3183                 if (isl_tab_push(cgbr->cone, isl_tab_undo_bmap_div) < 0)
3184                         return -1;
3185         }
3186         return context_tab_add_div(cgbr->tab, div,
3187                                         context_gbr_add_ineq_wrap, context);
3188 }
3189
3190 static int context_gbr_best_split(struct isl_context *context,
3191                 struct isl_tab *tab)
3192 {
3193         struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3194         struct isl_tab_undo *snap;
3195         int r;
3196
3197         snap = isl_tab_snap(cgbr->tab);
3198         r = best_split(tab, cgbr->tab);
3199
3200         if (r >= 0 && isl_tab_rollback(cgbr->tab, snap) < 0)
3201                 return -1;
3202
3203         return r;
3204 }
3205
3206 static int context_gbr_is_empty(struct isl_context *context)
3207 {
3208         struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3209         if (!cgbr->tab)
3210                 return -1;
3211         return cgbr->tab->empty;
3212 }
3213
3214 struct isl_gbr_tab_undo {
3215         struct isl_tab_undo *tab_snap;
3216         struct isl_tab_undo *shifted_snap;
3217         struct isl_tab_undo *cone_snap;
3218 };
3219
3220 static void *context_gbr_save(struct isl_context *context)
3221 {
3222         struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3223         struct isl_gbr_tab_undo *snap;
3224
3225         snap = isl_alloc_type(cgbr->tab->mat->ctx, struct isl_gbr_tab_undo);
3226         if (!snap)
3227                 return NULL;
3228
3229         snap->tab_snap = isl_tab_snap(cgbr->tab);
3230         if (isl_tab_save_samples(cgbr->tab) < 0)
3231                 goto error;
3232
3233         if (cgbr->shifted)
3234                 snap->shifted_snap = isl_tab_snap(cgbr->shifted);
3235         else
3236                 snap->shifted_snap = NULL;
3237
3238         if (cgbr->cone)
3239                 snap->cone_snap = isl_tab_snap(cgbr->cone);
3240         else
3241                 snap->cone_snap = NULL;
3242
3243         return snap;
3244 error:
3245         free(snap);
3246         return NULL;
3247 }
3248
3249 static void context_gbr_restore(struct isl_context *context, void *save)
3250 {
3251         struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3252         struct isl_gbr_tab_undo *snap = (struct isl_gbr_tab_undo *)save;
3253         if (!snap)
3254                 goto error;
3255         if (isl_tab_rollback(cgbr->tab, snap->tab_snap) < 0) {
3256                 isl_tab_free(cgbr->tab);
3257                 cgbr->tab = NULL;
3258         }
3259
3260         if (snap->shifted_snap) {
3261                 if (isl_tab_rollback(cgbr->shifted, snap->shifted_snap) < 0)
3262                         goto error;
3263         } else if (cgbr->shifted) {
3264                 isl_tab_free(cgbr->shifted);
3265                 cgbr->shifted = NULL;
3266         }
3267
3268         if (snap->cone_snap) {
3269                 if (isl_tab_rollback(cgbr->cone, snap->cone_snap) < 0)
3270                         goto error;
3271         } else if (cgbr->cone) {
3272                 isl_tab_free(cgbr->cone);
3273                 cgbr->cone = NULL;
3274         }
3275
3276         free(snap);
3277
3278         return;
3279 error:
3280         free(snap);
3281         isl_tab_free(cgbr->tab);
3282         cgbr->tab = NULL;
3283 }
3284
3285 static void context_gbr_discard(void *save)
3286 {
3287         struct isl_gbr_tab_undo *snap = (struct isl_gbr_tab_undo *)save;
3288         free(snap);
3289 }
3290
3291 static int context_gbr_is_ok(struct isl_context *context)
3292 {
3293         struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3294         return !!cgbr->tab;
3295 }
3296
3297 static void context_gbr_invalidate(struct isl_context *context)
3298 {
3299         struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3300         isl_tab_free(cgbr->tab);
3301         cgbr->tab = NULL;
3302 }
3303
3304 static void context_gbr_free(struct isl_context *context)
3305 {
3306         struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3307         isl_tab_free(cgbr->tab);
3308         isl_tab_free(cgbr->shifted);
3309         isl_tab_free(cgbr->cone);
3310         free(cgbr);
3311 }
3312
3313 struct isl_context_op isl_context_gbr_op = {
3314         context_gbr_detect_nonnegative_parameters,
3315         context_gbr_peek_basic_set,
3316         context_gbr_peek_tab,
3317         context_gbr_add_eq,
3318         context_gbr_add_ineq,
3319         context_gbr_ineq_sign,
3320         context_gbr_test_ineq,
3321         context_gbr_get_div,
3322         context_gbr_add_div,
3323         context_gbr_detect_equalities,
3324         context_gbr_best_split,
3325         context_gbr_is_empty,
3326         context_gbr_is_ok,
3327         context_gbr_save,
3328         context_gbr_restore,
3329         context_gbr_discard,
3330         context_gbr_invalidate,
3331         context_gbr_free,
3332 };
3333
3334 static struct isl_context *isl_context_gbr_alloc(struct isl_basic_set *dom)
3335 {
3336         struct isl_context_gbr *cgbr;
3337
3338         if (!dom)
3339                 return NULL;
3340
3341         cgbr = isl_calloc_type(dom->ctx, struct isl_context_gbr);
3342         if (!cgbr)
3343                 return NULL;
3344
3345         cgbr->context.op = &isl_context_gbr_op;
3346
3347         cgbr->shifted = NULL;
3348         cgbr->cone = NULL;
3349         cgbr->tab = isl_tab_from_basic_set(dom, 1);
3350         cgbr->tab = isl_tab_init_samples(cgbr->tab);
3351         if (!cgbr->tab)
3352                 goto error;
3353         check_gbr_integer_feasible(cgbr);
3354
3355         return &cgbr->context;
3356 error:
3357         cgbr->context.op->free(&cgbr->context);
3358         return NULL;
3359 }
3360
3361 static struct isl_context *isl_context_alloc(struct isl_basic_set *dom)
3362 {
3363         if (!dom)
3364                 return NULL;
3365
3366         if (dom->ctx->opt->context == ISL_CONTEXT_LEXMIN)
3367                 return isl_context_lex_alloc(dom);
3368         else
3369                 return isl_context_gbr_alloc(dom);
3370 }
3371
3372 /* Construct an isl_sol_map structure for accumulating the solution.
3373  * If track_empty is set, then we also keep track of the parts
3374  * of the context where there is no solution.
3375  * If max is set, then we are solving a maximization, rather than
3376  * a minimization problem, which means that the variables in the
3377  * tableau have value "M - x" rather than "M + x".
3378  */
3379 static struct isl_sol *sol_map_init(struct isl_basic_map *bmap,
3380         struct isl_basic_set *dom, int track_empty, int max)
3381 {
3382         struct isl_sol_map *sol_map = NULL;
3383
3384         if (!bmap)
3385                 goto error;
3386
3387         sol_map = isl_calloc_type(bmap->ctx, struct isl_sol_map);
3388         if (!sol_map)
3389                 goto error;
3390
3391         sol_map->sol.rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
3392         sol_map->sol.dec_level.callback.run = &sol_dec_level_wrap;
3393         sol_map->sol.dec_level.sol = &sol_map->sol;
3394         sol_map->sol.max = max;
3395         sol_map->sol.n_out = isl_basic_map_dim(bmap, isl_dim_out);
3396         sol_map->sol.add = &sol_map_add_wrap;
3397         sol_map->sol.add_empty = track_empty ? &sol_map_add_empty_wrap : NULL;
3398         sol_map->sol.free = &sol_map_free_wrap;
3399         sol_map->map = isl_map_alloc_space(isl_basic_map_get_space(bmap), 1,
3400                                             ISL_MAP_DISJOINT);
3401         if (!sol_map->map)
3402                 goto error;
3403
3404         sol_map->sol.context = isl_context_alloc(dom);
3405         if (!sol_map->sol.context)
3406                 goto error;
3407
3408         if (track_empty) {
3409                 sol_map->empty = isl_set_alloc_space(isl_basic_set_get_space(dom),
3410                                                         1, ISL_SET_DISJOINT);
3411                 if (!sol_map->empty)
3412                         goto error;
3413         }
3414
3415         isl_basic_set_free(dom);
3416         return &sol_map->sol;
3417 error:
3418         isl_basic_set_free(dom);
3419         sol_map_free(sol_map);
3420         return NULL;
3421 }
3422
3423 /* Check whether all coefficients of (non-parameter) variables
3424  * are non-positive, meaning that no pivots can be performed on the row.
3425  */
3426 static int is_critical(struct isl_tab *tab, int row)
3427 {
3428         int j;
3429         unsigned off = 2 + tab->M;
3430
3431         for (j = tab->n_dead; j < tab->n_col; ++j) {
3432                 if (tab->col_var[j] >= 0 &&
3433                     (tab->col_var[j] < tab->n_param  ||
3434                     tab->col_var[j] >= tab->n_var - tab->n_div))
3435                         continue;
3436
3437                 if (isl_int_is_pos(tab->mat->row[row][off + j]))
3438                         return 0;
3439         }
3440
3441         return 1;
3442 }
3443
3444 /* Check whether the inequality represented by vec is strict over the integers,
3445  * i.e., there are no integer values satisfying the constraint with
3446  * equality.  This happens if the gcd of the coefficients is not a divisor
3447  * of the constant term.  If so, scale the constraint down by the gcd
3448  * of the coefficients.
3449  */
3450 static int is_strict(struct isl_vec *vec)
3451 {
3452         isl_int gcd;
3453         int strict = 0;
3454
3455         isl_int_init(gcd);
3456         isl_seq_gcd(vec->el + 1, vec->size - 1, &gcd);
3457         if (!isl_int_is_one(gcd)) {
3458                 strict = !isl_int_is_divisible_by(vec->el[0], gcd);
3459                 isl_int_fdiv_q(vec->el[0], vec->el[0], gcd);
3460                 isl_seq_scale_down(vec->el + 1, vec->el + 1, gcd, vec->size-1);
3461         }
3462         isl_int_clear(gcd);
3463
3464         return strict;
3465 }
3466
3467 /* Determine the sign of the given row of the main tableau.
3468  * The result is one of
3469  *      isl_tab_row_pos: always non-negative; no pivot needed
3470  *      isl_tab_row_neg: always non-positive; pivot
3471  *      isl_tab_row_any: can be both positive and negative; split
3472  *
3473  * We first handle some simple cases
3474  *      - the row sign may be known already
3475  *      - the row may be obviously non-negative
3476  *      - the parametric constant may be equal to that of another row
3477  *        for which we know the sign.  This sign will be either "pos" or
3478  *        "any".  If it had been "neg" then we would have pivoted before.
3479  *
3480  * If none of these cases hold, we check the value of the row for each
3481  * of the currently active samples.  Based on the signs of these values
3482  * we make an initial determination of the sign of the row.
3483  *
3484  *      all zero                        ->      unk(nown)
3485  *      all non-negative                ->      pos
3486  *      all non-positive                ->      neg
3487  *      both negative and positive      ->      all
3488  *
3489  * If we end up with "all", we are done.
3490  * Otherwise, we perform a check for positive and/or negative
3491  * values as follows.
3492  *
3493  *      samples        neg             unk             pos
3494  *      <0 ?                        Y        N      Y        N
3495  *                                          pos    any      pos
3496  *      >0 ?         Y      N    Y     N
3497  *                  any    neg  any   neg
3498  *
3499  * There is no special sign for "zero", because we can usually treat zero
3500  * as either non-negative or non-positive, whatever works out best.
3501  * However, if the row is "critical", meaning that pivoting is impossible
3502  * then we don't want to limp zero with the non-positive case, because
3503  * then we we would lose the solution for those values of the parameters
3504  * where the value of the row is zero.  Instead, we treat 0 as non-negative
3505  * ensuring a split if the row can attain both zero and negative values.
3506  * The same happens when the original constraint was one that could not
3507  * be satisfied with equality by any integer values of the parameters.
3508  * In this case, we normalize the constraint, but then a value of zero
3509  * for the normalized constraint is actually a positive value for the
3510  * original constraint, so again we need to treat zero as non-negative.
3511  * In both these cases, we have the following decision tree instead:
3512  *
3513  *      all non-negative                ->      pos
3514  *      all negative                    ->      neg
3515  *      both negative and non-negative  ->      all
3516  *
3517  *      samples        neg                             pos
3518  *      <0 ?                                        Y        N
3519  *                                                 any      pos
3520  *      >=0 ?        Y      N
3521  *                  any    neg
3522  */
3523 static enum isl_tab_row_sign row_sign(struct isl_tab *tab,
3524         struct isl_sol *sol, int row)
3525 {
3526         struct isl_vec *ineq = NULL;
3527         enum isl_tab_row_sign res = isl_tab_row_unknown;
3528         int critical;
3529         int strict;
3530         int row2;
3531
3532         if (tab->row_sign[row] != isl_tab_row_unknown)
3533                 return tab->row_sign[row];
3534         if (is_obviously_nonneg(tab, row))
3535                 return isl_tab_row_pos;
3536         for (row2 = tab->n_redundant; row2 < tab->n_row; ++row2) {
3537                 if (tab->row_sign[row2] == isl_tab_row_unknown)
3538                         continue;
3539                 if (identical_parameter_line(tab, row, row2))
3540                         return tab->row_sign[row2];
3541         }
3542
3543         critical = is_critical(tab, row);
3544
3545         ineq = get_row_parameter_ineq(tab, row);
3546         if (!ineq)
3547                 goto error;
3548
3549         strict = is_strict(ineq);
3550
3551         res = sol->context->op->ineq_sign(sol->context, ineq->el,
3552                                           critical || strict);
3553
3554         if (res == isl_tab_row_unknown || res == isl_tab_row_pos) {
3555                 /* test for negative values */
3556                 int feasible;
3557                 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3558                 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3559
3560                 feasible = sol->context->op->test_ineq(sol->context, ineq->el);
3561                 if (feasible < 0)
3562                         goto error;
3563                 if (!feasible)
3564                         res = isl_tab_row_pos;
3565                 else
3566                         res = (res == isl_tab_row_unknown) ? isl_tab_row_neg
3567                                                            : isl_tab_row_any;
3568                 if (res == isl_tab_row_neg) {
3569                         isl_seq_neg(ineq->el, ineq->el, ineq->size);
3570                         isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3571                 }
3572         }
3573
3574         if (res == isl_tab_row_neg) {
3575                 /* test for positive values */
3576                 int feasible;
3577                 if (!critical && !strict)
3578                         isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3579
3580                 feasible = sol->context->op->test_ineq(sol->context, ineq->el);
3581                 if (feasible < 0)
3582                         goto error;
3583                 if (feasible)
3584                         res = isl_tab_row_any;
3585         }
3586
3587         isl_vec_free(ineq);
3588         return res;
3589 error:
3590         isl_vec_free(ineq);
3591         return isl_tab_row_unknown;
3592 }
3593
3594 static void find_solutions(struct isl_sol *sol, struct isl_tab *tab);
3595
3596 /* Find solutions for values of the parameters that satisfy the given
3597  * inequality.
3598  *
3599  * We currently take a snapshot of the context tableau that is reset
3600  * when we return from this function, while we make a copy of the main
3601  * tableau, leaving the original main tableau untouched.
3602  * These are fairly arbitrary choices.  Making a copy also of the context
3603  * tableau would obviate the need to undo any changes made to it later,
3604  * while taking a snapshot of the main tableau could reduce memory usage.
3605  * If we were to switch to taking a snapshot of the main tableau,
3606  * we would have to keep in mind that we need to save the row signs
3607  * and that we need to do this before saving the current basis
3608  * such that the basis has been restore before we restore the row signs.
3609  */
3610 static void find_in_pos(struct isl_sol *sol, struct isl_tab *tab, isl_int *ineq)
3611 {
3612         void *saved;
3613
3614         if (!sol->context)
3615                 goto error;
3616         saved = sol->context->op->save(sol->context);
3617
3618         tab = isl_tab_dup(tab);
3619         if (!tab)
3620                 goto error;
3621
3622         sol->context->op->add_ineq(sol->context, ineq, 0, 1);
3623
3624         find_solutions(sol, tab);
3625
3626         if (!sol->error)
3627                 sol->context->op->restore(sol->context, saved);
3628         else
3629                 sol->context->op->discard(saved);
3630         return;
3631 error:
3632         sol->error = 1;
3633 }
3634
3635 /* Record the absence of solutions for those values of the parameters
3636  * that do not satisfy the given inequality with equality.
3637  */
3638 static void no_sol_in_strict(struct isl_sol *sol,
3639         struct isl_tab *tab, struct isl_vec *ineq)
3640 {
3641         int empty;
3642         void *saved;
3643
3644         if (!sol->context || sol->error)
3645                 goto error;
3646         saved = sol->context->op->save(sol->context);
3647
3648         isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3649
3650         sol->context->op->add_ineq(sol->context, ineq->el, 1, 0);
3651         if (!sol->context)
3652                 goto error;
3653
3654         empty = tab->empty;
3655         tab->empty = 1;
3656         sol_add(sol, tab);
3657         tab->empty = empty;
3658
3659         isl_int_add_ui(ineq->el[0], ineq->el[0], 1);
3660
3661         sol->context->op->restore(sol->context, saved);
3662         return;
3663 error:
3664         sol->error = 1;
3665 }
3666
3667 /* Compute the lexicographic minimum of the set represented by the main
3668  * tableau "tab" within the context "sol->context_tab".
3669  * On entry the sample value of the main tableau is lexicographically
3670  * less than or equal to this lexicographic minimum.
3671  * Pivots are performed until a feasible point is found, which is then
3672  * necessarily equal to the minimum, or until the tableau is found to
3673  * be infeasible.  Some pivots may need to be performed for only some
3674  * feasible values of the context tableau.  If so, the context tableau
3675  * is split into a part where the pivot is needed and a part where it is not.
3676  *
3677  * Whenever we enter the main loop, the main tableau is such that no
3678  * "obvious" pivots need to be performed on it, where "obvious" means
3679  * that the given row can be seen to be negative without looking at
3680  * the context tableau.  In particular, for non-parametric problems,
3681  * no pivots need to be performed on the main tableau.
3682  * The caller of find_solutions is responsible for making this property
3683  * hold prior to the first iteration of the loop, while restore_lexmin
3684  * is called before every other iteration.
3685  *
3686  * Inside the main loop, we first examine the signs of the rows of
3687  * the main tableau within the context of the context tableau.
3688  * If we find a row that is always non-positive for all values of
3689  * the parameters satisfying the context tableau and negative for at
3690  * least one value of the parameters, we perform the appropriate pivot
3691  * and start over.  An exception is the case where no pivot can be
3692  * performed on the row.  In this case, we require that the sign of
3693  * the row is negative for all values of the parameters (rather than just
3694  * non-positive).  This special case is handled inside row_sign, which
3695  * will say that the row can have any sign if it determines that it can
3696  * attain both negative and zero values.
3697  *
3698  * If we can't find a row that always requires a pivot, but we can find
3699  * one or more rows that require a pivot for some values of the parameters
3700  * (i.e., the row can attain both positive and negative signs), then we split
3701  * the context tableau into two parts, one where we force the sign to be
3702  * non-negative and one where we force is to be negative.
3703  * The non-negative part is handled by a recursive call (through find_in_pos).
3704  * Upon returning from this call, we continue with the negative part and
3705  * perform the required pivot.
3706  *
3707  * If no such rows can be found, all rows are non-negative and we have
3708  * found a (rational) feasible point.  If we only wanted a rational point
3709  * then we are done.
3710  * Otherwise, we check if all values of the sample point of the tableau
3711  * are integral for the variables.  If so, we have found the minimal
3712  * integral point and we are done.
3713  * If the sample point is not integral, then we need to make a distinction
3714  * based on whether the constant term is non-integral or the coefficients
3715  * of the parameters.  Furthermore, in order to decide how to handle
3716  * the non-integrality, we also need to know whether the coefficients
3717  * of the other columns in the tableau are integral.  This leads
3718  * to the following table.  The first two rows do not correspond
3719  * to a non-integral sample point and are only mentioned for completeness.
3720  *
3721  *      constant        parameters      other
3722  *
3723  *      int             int             int     |
3724  *      int             int             rat     | -> no problem
3725  *
3726  *      rat             int             int       -> fail
3727  *
3728  *      rat             int             rat       -> cut
3729  *
3730  *      int             rat             rat     |
3731  *      rat             rat             rat     | -> parametric cut
3732  *
3733  *      int             rat             int     |
3734  *      rat             rat             int     | -> split context
3735  *
3736  * If the parametric constant is completely integral, then there is nothing
3737  * to be done.  If the constant term is non-integral, but all the other
3738  * coefficient are integral, then there is nothing that can be done
3739  * and the tableau has no integral solution.
3740  * If, on the other hand, one or more of the other columns have rational
3741  * coefficients, but the parameter coefficients are all integral, then
3742  * we can perform a regular (non-parametric) cut.
3743  * Finally, if there is any parameter coefficient that is non-integral,
3744  * then we need to involve the context tableau.  There are two cases here.
3745  * If at least one other column has a rational coefficient, then we
3746  * can perform a parametric cut in the main tableau by adding a new
3747  * integer division in the context tableau.
3748  * If all other columns have integral coefficients, then we need to
3749  * enforce that the rational combination of parameters (c + \sum a_i y_i)/m
3750  * is always integral.  We do this by introducing an integer division
3751  * q = floor((c + \sum a_i y_i)/m) and stipulating that its argument should
3752  * always be integral in the context tableau, i.e., m q = c + \sum a_i y_i.
3753  * Since q is expressed in the tableau as
3754  *      c + \sum a_i y_i - m q >= 0
3755  *      -c - \sum a_i y_i + m q + m - 1 >= 0
3756  * it is sufficient to add the inequality
3757  *      -c - \sum a_i y_i + m q >= 0
3758  * In the part of the context where this inequality does not hold, the
3759  * main tableau is marked as being empty.
3760  */
3761 static void find_solutions(struct isl_sol *sol, struct isl_tab *tab)
3762 {
3763         struct isl_context *context;
3764         int r;
3765
3766         if (!tab || sol->error)
3767                 goto error;
3768
3769         context = sol->context;
3770
3771         if (tab->empty)
3772                 goto done;
3773         if (context->op->is_empty(context))
3774                 goto done;
3775
3776         for (r = 0; r >= 0 && tab && !tab->empty; r = restore_lexmin(tab)) {
3777                 int flags;
3778                 int row;
3779                 enum isl_tab_row_sign sgn;
3780                 int split = -1;
3781                 int n_split = 0;
3782
3783                 for (row = tab->n_redundant; row < tab->n_row; ++row) {
3784                         if (!isl_tab_var_from_row(tab, row)->is_nonneg)
3785                                 continue;
3786                         sgn = row_sign(tab, sol, row);
3787                         if (!sgn)
3788                                 goto error;
3789                         tab->row_sign[row] = sgn;
3790                         if (sgn == isl_tab_row_any)
3791                                 n_split++;
3792                         if (sgn == isl_tab_row_any && split == -1)
3793                                 split = row;
3794                         if (sgn == isl_tab_row_neg)
3795                                 break;
3796                 }
3797                 if (row < tab->n_row)
3798                         continue;
3799                 if (split != -1) {
3800                         struct isl_vec *ineq;
3801                         if (n_split != 1)
3802                                 split = context->op->best_split(context, tab);
3803                         if (split < 0)
3804                                 goto error;
3805                         ineq = get_row_parameter_ineq(tab, split);
3806                         if (!ineq)
3807                                 goto error;
3808                         is_strict(ineq);
3809                         for (row = tab->n_redundant; row < tab->n_row; ++row) {
3810                                 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
3811                                         continue;
3812                                 if (tab->row_sign[row] == isl_tab_row_any)
3813                                         tab->row_sign[row] = isl_tab_row_unknown;
3814                         }
3815                         tab->row_sign[split] = isl_tab_row_pos;
3816                         sol_inc_level(sol);
3817                         find_in_pos(sol, tab, ineq->el);
3818                         tab->row_sign[split] = isl_tab_row_neg;
3819                         row = split;
3820                         isl_seq_neg(ineq->el, ineq->el, ineq->size);
3821                         isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3822                         if (!sol->error)
3823                                 context->op->add_ineq(context, ineq->el, 0, 1);
3824                         isl_vec_free(ineq);
3825                         if (sol->error)
3826                                 goto error;
3827                         continue;
3828                 }
3829                 if (tab->rational)
3830                         break;
3831                 row = first_non_integer_row(tab, &flags);
3832                 if (row < 0)
3833                         break;
3834                 if (ISL_FL_ISSET(flags, I_PAR)) {
3835                         if (ISL_FL_ISSET(flags, I_VAR)) {
3836                                 if (isl_tab_mark_empty(tab) < 0)
3837                                         goto error;
3838                                 break;
3839                         }
3840                         row = add_cut(tab, row);
3841                 } else if (ISL_FL_ISSET(flags, I_VAR)) {
3842                         struct isl_vec *div;
3843                         struct isl_vec *ineq;
3844                         int d;
3845                         div = get_row_split_div(tab, row);
3846                         if (!div)
3847                                 goto error;
3848                         d = context->op->get_div(context, tab, div);
3849                         isl_vec_free(div);
3850                         if (d < 0)
3851                                 goto error;
3852                         ineq = ineq_for_div(context->op->peek_basic_set(context), d);
3853                         if (!ineq)
3854                                 goto error;
3855                         sol_inc_level(sol);
3856                         no_sol_in_strict(sol, tab, ineq);
3857                         isl_seq_neg(ineq->el, ineq->el, ineq->size);
3858                         context->op->add_ineq(context, ineq->el, 1, 1);
3859                         isl_vec_free(ineq);
3860                         if (sol->error || !context->op->is_ok(context))
3861                                 goto error;
3862                         tab = set_row_cst_to_div(tab, row, d);
3863                         if (context->op->is_empty(context))
3864                                 break;
3865                 } else
3866                         row = add_parametric_cut(tab, row, context);
3867                 if (row < 0)
3868                         goto error;
3869         }
3870         if (r < 0)
3871                 goto error;
3872 done:
3873         sol_add(sol, tab);
3874         isl_tab_free(tab);
3875         return;
3876 error:
3877         isl_tab_free(tab);
3878         sol->error = 1;
3879 }
3880
3881 /* Compute the lexicographic minimum of the set represented by the main
3882  * tableau "tab" within the context "sol->context_tab".
3883  *
3884  * As a preprocessing step, we first transfer all the purely parametric
3885  * equalities from the main tableau to the context tableau, i.e.,
3886  * parameters that have been pivoted to a row.
3887  * These equalities are ignored by the main algorithm, because the
3888  * corresponding rows may not be marked as being non-negative.
3889  * In parts of the context where the added equality does not hold,
3890  * the main tableau is marked as being empty.
3891  */
3892 static void find_solutions_main(struct isl_sol *sol, struct isl_tab *tab)
3893 {
3894         int row;
3895
3896         if (!tab)
3897                 goto error;
3898
3899         sol->level = 0;
3900
3901         for (row = tab->n_redundant; row < tab->n_row; ++row) {
3902                 int p;
3903                 struct isl_vec *eq;
3904
3905                 if (tab->row_var[row] < 0)
3906                         continue;
3907                 if (tab->row_var[row] >= tab->n_param &&
3908                     tab->row_var[row] < tab->n_var - tab->n_div)
3909                         continue;
3910                 if (tab->row_var[row] < tab->n_param)
3911                         p = tab->row_var[row];
3912                 else
3913                         p = tab->row_var[row]
3914                                 + tab->n_param - (tab->n_var - tab->n_div);
3915
3916                 eq = isl_vec_alloc(tab->mat->ctx, 1+tab->n_param+tab->n_div);
3917                 if (!eq)
3918                         goto error;
3919                 get_row_parameter_line(tab, row, eq->el);
3920                 isl_int_neg(eq->el[1 + p], tab->mat->row[row][0]);
3921                 eq = isl_vec_normalize(eq);
3922
3923                 sol_inc_level(sol);
3924                 no_sol_in_strict(sol, tab, eq);
3925
3926                 isl_seq_neg(eq->el, eq->el, eq->size);
3927                 sol_inc_level(sol);
3928                 no_sol_in_strict(sol, tab, eq);
3929                 isl_seq_neg(eq->el, eq->el, eq->size);
3930
3931                 sol->context->op->add_eq(sol->context, eq->el, 1, 1);
3932
3933                 isl_vec_free(eq);
3934
3935                 if (isl_tab_mark_redundant(tab, row) < 0)
3936                         goto error;
3937
3938                 if (sol->context->op->is_empty(sol->context))
3939                         break;
3940
3941                 row = tab->n_redundant - 1;
3942         }
3943
3944         find_solutions(sol, tab);
3945
3946         sol->level = 0;
3947         sol_pop(sol);
3948
3949         return;
3950 error:
3951         isl_tab_free(tab);
3952         sol->error = 1;
3953 }
3954
3955 /* Check if integer division "div" of "dom" also occurs in "bmap".
3956  * If so, return its position within the divs.
3957  * If not, return -1.
3958  */
3959 static int find_context_div(struct isl_basic_map *bmap,
3960         struct isl_basic_set *dom, unsigned div)
3961 {
3962         int i;
3963         unsigned b_dim = isl_space_dim(bmap->dim, isl_dim_all);
3964         unsigned d_dim = isl_space_dim(dom->dim, isl_dim_all);
3965
3966         if (isl_int_is_zero(dom->div[div][0]))
3967                 return -1;
3968         if (isl_seq_first_non_zero(dom->div[div] + 2 + d_dim, dom->n_div) != -1)
3969                 return -1;
3970
3971         for (i = 0; i < bmap->n_div; ++i) {
3972                 if (isl_int_is_zero(bmap->div[i][0]))
3973                         continue;
3974                 if (isl_seq_first_non_zero(bmap->div[i] + 2 + d_dim,
3975                                            (b_dim - d_dim) + bmap->n_div) != -1)
3976                         continue;
3977                 if (isl_seq_eq(bmap->div[i], dom->div[div], 2 + d_dim))
3978                         return i;
3979         }
3980         return -1;
3981 }
3982
3983 /* The correspondence between the variables in the main tableau,
3984  * the context tableau, and the input map and domain is as follows.
3985  * The first n_param and the last n_div variables of the main tableau
3986  * form the variables of the context tableau.
3987  * In the basic map, these n_param variables correspond to the
3988  * parameters and the input dimensions.  In the domain, they correspond
3989  * to the parameters and the set dimensions.
3990  * The n_div variables correspond to the integer divisions in the domain.
3991  * To ensure that everything lines up, we may need to copy some of the
3992  * integer divisions of the domain to the map.  These have to be placed
3993  * in the same order as those in the context and they have to be placed
3994  * after any other integer divisions that the map may have.
3995  * This function performs the required reordering.
3996  */
3997 static struct isl_basic_map *align_context_divs(struct isl_basic_map *bmap,
3998         struct isl_basic_set *dom)
3999 {
4000         int i;
4001         int common = 0;
4002         int other;
4003
4004         for (i = 0; i < dom->n_div; ++i)
4005                 if (find_context_div(bmap, dom, i) != -1)
4006                         common++;
4007         other = bmap->n_div - common;
4008         if (dom->n_div - common > 0) {
4009                 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
4010                                 dom->n_div - common, 0, 0);
4011                 if (!bmap)
4012                         return NULL;
4013         }
4014         for (i = 0; i < dom->n_div; ++i) {
4015                 int pos = find_context_div(bmap, dom, i);
4016                 if (pos < 0) {
4017                         pos = isl_basic_map_alloc_div(bmap);
4018                         if (pos < 0)
4019                                 goto error;
4020                         isl_int_set_si(bmap->div[pos][0], 0);
4021                 }
4022                 if (pos != other + i)
4023                         isl_basic_map_swap_div(bmap, pos, other + i);
4024         }
4025         return bmap;
4026 error:
4027         isl_basic_map_free(bmap);
4028         return NULL;
4029 }
4030
4031 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
4032  * some obvious symmetries.
4033  *
4034  * We make sure the divs in the domain are properly ordered,
4035  * because they will be added one by one in the given order
4036  * during the construction of the solution map.
4037  */
4038 static struct isl_sol *basic_map_partial_lexopt_base(
4039         __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4040         __isl_give isl_set **empty, int max,
4041         struct isl_sol *(*init)(__isl_keep isl_basic_map *bmap,
4042                     __isl_take isl_basic_set *dom, int track_empty, int max))
4043 {
4044         struct isl_tab *tab;
4045         struct isl_sol *sol = NULL;
4046         struct isl_context *context;
4047
4048         if (dom->n_div) {
4049                 dom = isl_basic_set_order_divs(dom);
4050                 bmap = align_context_divs(bmap, dom);
4051         }
4052         sol = init(bmap, dom, !!empty, max);
4053         if (!sol)
4054                 goto error;
4055
4056         context = sol->context;
4057         if (isl_basic_set_plain_is_empty(context->op->peek_basic_set(context)))
4058                 /* nothing */;
4059         else if (isl_basic_map_plain_is_empty(bmap)) {
4060                 if (sol->add_empty)
4061                         sol->add_empty(sol,
4062                     isl_basic_set_copy(context->op->peek_basic_set(context)));
4063         } else {
4064                 tab = tab_for_lexmin(bmap,
4065                                     context->op->peek_basic_set(context), 1, max);
4066                 tab = context->op->detect_nonnegative_parameters(context, tab);
4067                 find_solutions_main(sol, tab);
4068         }
4069         if (sol->error)
4070                 goto error;
4071
4072         isl_basic_map_free(bmap);
4073         return sol;
4074 error:
4075         sol_free(sol);
4076         isl_basic_map_free(bmap);
4077         return NULL;
4078 }
4079
4080 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
4081  * some obvious symmetries.
4082  *
4083  * We call basic_map_partial_lexopt_base and extract the results.
4084  */
4085 static __isl_give isl_map *basic_map_partial_lexopt_base_map(
4086         __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4087         __isl_give isl_set **empty, int max)
4088 {
4089         isl_map *result = NULL;
4090         struct isl_sol *sol;
4091         struct isl_sol_map *sol_map;
4092
4093         sol = basic_map_partial_lexopt_base(bmap, dom, empty, max,
4094                                             &sol_map_init);
4095         if (!sol)
4096                 return NULL;
4097         sol_map = (struct isl_sol_map *) sol;
4098
4099         result = isl_map_copy(sol_map->map);
4100         if (empty)
4101                 *empty = isl_set_copy(sol_map->empty);
4102         sol_free(&sol_map->sol);
4103         return result;
4104 }
4105
4106 /* Structure used during detection of parallel constraints.
4107  * n_in: number of "input" variables: isl_dim_param + isl_dim_in
4108  * n_out: number of "output" variables: isl_dim_out + isl_dim_div
4109  * val: the coefficients of the output variables
4110  */
4111 struct isl_constraint_equal_info {
4112         isl_basic_map *bmap;
4113         unsigned n_in;
4114         unsigned n_out;
4115         isl_int *val;
4116 };
4117
4118 /* Check whether the coefficients of the output variables
4119  * of the constraint in "entry" are equal to info->val.
4120  */
4121 static int constraint_equal(const void *entry, const void *val)
4122 {
4123         isl_int **row = (isl_int **)entry;
4124         const struct isl_constraint_equal_info *info = val;
4125
4126         return isl_seq_eq((*row) + 1 + info->n_in, info->val, info->n_out);
4127 }
4128
4129 /* Check whether "bmap" has a pair of constraints that have
4130  * the same coefficients for the output variables.
4131  * Note that the coefficients of the existentially quantified
4132  * variables need to be zero since the existentially quantified
4133  * of the result are usually not the same as those of the input.
4134  * the isl_dim_out and isl_dim_div dimensions.
4135  * If so, return 1 and return the row indices of the two constraints
4136  * in *first and *second.
4137  */
4138 static int parallel_constraints(__isl_keep isl_basic_map *bmap,
4139         int *first, int *second)
4140 {
4141         int i;
4142         isl_ctx *ctx = isl_basic_map_get_ctx(bmap);
4143         struct isl_hash_table *table = NULL;
4144         struct isl_hash_table_entry *entry;
4145         struct isl_constraint_equal_info info;
4146         unsigned n_out;
4147         unsigned n_div;
4148
4149         ctx = isl_basic_map_get_ctx(bmap);
4150         table = isl_hash_table_alloc(ctx, bmap->n_ineq);
4151         if (!table)
4152                 goto error;
4153
4154         info.n_in = isl_basic_map_dim(bmap, isl_dim_param) +
4155                     isl_basic_map_dim(bmap, isl_dim_in);
4156         info.bmap = bmap;
4157         n_out = isl_basic_map_dim(bmap, isl_dim_out);
4158         n_div = isl_basic_map_dim(bmap, isl_dim_div);
4159         info.n_out = n_out + n_div;
4160         for (i = 0; i < bmap->n_ineq; ++i) {
4161                 uint32_t hash;
4162
4163                 info.val = bmap->ineq[i] + 1 + info.n_in;
4164                 if (isl_seq_first_non_zero(info.val, n_out) < 0)
4165                         continue;
4166                 if (isl_seq_first_non_zero(info.val + n_out, n_div) >= 0)
4167                         continue;
4168                 hash = isl_seq_get_hash(info.val, info.n_out);
4169                 entry = isl_hash_table_find(ctx, table, hash,
4170                                             constraint_equal, &info, 1);
4171                 if (!entry)
4172                         goto error;
4173                 if (entry->data)
4174                         break;
4175                 entry->data = &bmap->ineq[i];
4176         }
4177
4178         if (i < bmap->n_ineq) {
4179                 *first = ((isl_int **)entry->data) - bmap->ineq; 
4180                 *second = i;
4181         }
4182
4183         isl_hash_table_free(ctx, table);
4184
4185         return i < bmap->n_ineq;
4186 error:
4187         isl_hash_table_free(ctx, table);
4188         return -1;
4189 }
4190
4191 /* Given a set of upper bounds in "var", add constraints to "bset"
4192  * that make the i-th bound smallest.
4193  *
4194  * In particular, if there are n bounds b_i, then add the constraints
4195  *
4196  *      b_i <= b_j      for j > i
4197  *      b_i <  b_j      for j < i
4198  */
4199 static __isl_give isl_basic_set *select_minimum(__isl_take isl_basic_set *bset,
4200         __isl_keep isl_mat *var, int i)
4201 {
4202         isl_ctx *ctx;
4203         int j, k;
4204
4205         ctx = isl_mat_get_ctx(var);
4206
4207         for (j = 0; j < var->n_row; ++j) {
4208                 if (j == i)
4209                         continue;
4210                 k = isl_basic_set_alloc_inequality(bset);
4211                 if (k < 0)
4212                         goto error;
4213                 isl_seq_combine(bset->ineq[k], ctx->one, var->row[j],
4214                                 ctx->negone, var->row[i], var->n_col);
4215                 isl_int_set_si(bset->ineq[k][var->n_col], 0);
4216                 if (j < i)
4217                         isl_int_sub_ui(bset->ineq[k][0], bset->ineq[k][0], 1);
4218         }
4219
4220         bset = isl_basic_set_finalize(bset);
4221
4222         return bset;
4223 error:
4224         isl_basic_set_free(bset);
4225         return NULL;
4226 }
4227
4228 /* Given a set of upper bounds on the last "input" variable m,
4229  * construct a set that assigns the minimal upper bound to m, i.e.,
4230  * construct a set that divides the space into cells where one
4231  * of the upper bounds is smaller than all the others and assign
4232  * this upper bound to m.
4233  *
4234  * In particular, if there are n bounds b_i, then the result
4235  * consists of n basic sets, each one of the form
4236  *
4237  *      m = b_i
4238  *      b_i <= b_j      for j > i
4239  *      b_i <  b_j      for j < i
4240  */
4241 static __isl_give isl_set *set_minimum(__isl_take isl_space *dim,
4242         __isl_take isl_mat *var)
4243 {
4244         int i, k;
4245         isl_basic_set *bset = NULL;
4246         isl_ctx *ctx;
4247         isl_set *set = NULL;
4248
4249         if (!dim || !var)
4250                 goto error;
4251
4252         ctx = isl_space_get_ctx(dim);
4253         set = isl_set_alloc_space(isl_space_copy(dim),
4254                                 var->n_row, ISL_SET_DISJOINT);
4255
4256         for (i = 0; i < var->n_row; ++i) {
4257                 bset = isl_basic_set_alloc_space(isl_space_copy(dim), 0,
4258                                                1, var->n_row - 1);
4259                 k = isl_basic_set_alloc_equality(bset);
4260                 if (k < 0)
4261                         goto error;
4262                 isl_seq_cpy(bset->eq[k], var->row[i], var->n_col);
4263                 isl_int_set_si(bset->eq[k][var->n_col], -1);
4264                 bset = select_minimum(bset, var, i);
4265                 set = isl_set_add_basic_set(set, bset);
4266         }
4267
4268         isl_space_free(dim);
4269         isl_mat_free(var);
4270         return set;
4271 error:
4272         isl_basic_set_free(bset);
4273         isl_set_free(set);
4274         isl_space_free(dim);
4275         isl_mat_free(var);
4276         return NULL;
4277 }
4278
4279 /* Given that the last input variable of "bmap" represents the minimum
4280  * of the bounds in "cst", check whether we need to split the domain
4281  * based on which bound attains the minimum.
4282  *
4283  * A split is needed when the minimum appears in an integer division
4284  * or in an equality.  Otherwise, it is only needed if it appears in
4285  * an upper bound that is different from the upper bounds on which it
4286  * is defined.
4287  */
4288 static int need_split_basic_map(__isl_keep isl_basic_map *bmap,
4289         __isl_keep isl_mat *cst)
4290 {
4291         int i, j;
4292         unsigned total;
4293         unsigned pos;
4294
4295         pos = cst->n_col - 1;
4296         total = isl_basic_map_dim(bmap, isl_dim_all);
4297
4298         for (i = 0; i < bmap->n_div; ++i)
4299                 if (!isl_int_is_zero(bmap->div[i][2 + pos]))
4300                         return 1;
4301
4302         for (i = 0; i < bmap->n_eq; ++i)
4303                 if (!isl_int_is_zero(bmap->eq[i][1 + pos]))
4304                         return 1;
4305
4306         for (i = 0; i < bmap->n_ineq; ++i) {
4307                 if (isl_int_is_nonneg(bmap->ineq[i][1 + pos]))
4308                         continue;
4309                 if (!isl_int_is_negone(bmap->ineq[i][1 + pos]))
4310                         return 1;
4311                 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + pos + 1,
4312                                            total - pos - 1) >= 0)
4313                         return 1;
4314
4315                 for (j = 0; j < cst->n_row; ++j)
4316                         if (isl_seq_eq(bmap->ineq[i], cst->row[j], cst->n_col))
4317                                 break;
4318                 if (j >= cst->n_row)
4319                         return 1;
4320         }
4321
4322         return 0;
4323 }
4324
4325 /* Given that the last set variable of "bset" represents the minimum
4326  * of the bounds in "cst", check whether we need to split the domain
4327  * based on which bound attains the minimum.
4328  *
4329  * We simply call need_split_basic_map here.  This is safe because
4330  * the position of the minimum is computed from "cst" and not
4331  * from "bmap".
4332  */
4333 static int need_split_basic_set(__isl_keep isl_basic_set *bset,
4334         __isl_keep isl_mat *cst)
4335 {
4336         return need_split_basic_map((isl_basic_map *)bset, cst);
4337 }
4338
4339 /* Given that the last set variable of "set" represents the minimum
4340  * of the bounds in "cst", check whether we need to split the domain
4341  * based on which bound attains the minimum.
4342  */
4343 static int need_split_set(__isl_keep isl_set *set, __isl_keep isl_mat *cst)
4344 {
4345         int i;
4346
4347         for (i = 0; i < set->n; ++i)
4348                 if (need_split_basic_set(set->p[i], cst))
4349                         return 1;
4350
4351         return 0;
4352 }
4353
4354 /* Given a set of which the last set variable is the minimum
4355  * of the bounds in "cst", split each basic set in the set
4356  * in pieces where one of the bounds is (strictly) smaller than the others.
4357  * This subdivision is given in "min_expr".
4358  * The variable is subsequently projected out.
4359  *
4360  * We only do the split when it is needed.
4361  * For example if the last input variable m = min(a,b) and the only
4362  * constraints in the given basic set are lower bounds on m,
4363  * i.e., l <= m = min(a,b), then we can simply project out m
4364  * to obtain l <= a and l <= b, without having to split on whether
4365  * m is equal to a or b.
4366  */
4367 static __isl_give isl_set *split(__isl_take isl_set *empty,
4368         __isl_take isl_set *min_expr, __isl_take isl_mat *cst)
4369 {
4370         int n_in;
4371         int i;
4372         isl_space *dim;
4373         isl_set *res;
4374
4375         if (!empty || !min_expr || !cst)
4376                 goto error;
4377
4378         n_in = isl_set_dim(empty, isl_dim_set);
4379         dim = isl_set_get_space(empty);
4380         dim = isl_space_drop_dims(dim, isl_dim_set, n_in - 1, 1);
4381         res = isl_set_empty(dim);
4382
4383         for (i = 0; i < empty->n; ++i) {
4384                 isl_set *set;
4385
4386                 set = isl_set_from_basic_set(isl_basic_set_copy(empty->p[i]));
4387                 if (need_split_basic_set(empty->p[i], cst))
4388                         set = isl_set_intersect(set, isl_set_copy(min_expr));
4389                 set = isl_set_remove_dims(set, isl_dim_set, n_in - 1, 1);
4390
4391                 res = isl_set_union_disjoint(res, set);
4392         }
4393
4394         isl_set_free(empty);
4395         isl_set_free(min_expr);
4396         isl_mat_free(cst);
4397         return res;
4398 error:
4399         isl_set_free(empty);
4400         isl_set_free(min_expr);
4401         isl_mat_free(cst);
4402         return NULL;
4403 }
4404
4405 /* Given a map of which the last input variable is the minimum
4406  * of the bounds in "cst", split each basic set in the set
4407  * in pieces where one of the bounds is (strictly) smaller than the others.
4408  * This subdivision is given in "min_expr".
4409  * The variable is subsequently projected out.
4410  *
4411  * The implementation is essentially the same as that of "split".
4412  */
4413 static __isl_give isl_map *split_domain(__isl_take isl_map *opt,
4414         __isl_take isl_set *min_expr, __isl_take isl_mat *cst)
4415 {
4416         int n_in;
4417         int i;
4418         isl_space *dim;
4419         isl_map *res;
4420
4421         if (!opt || !min_expr || !cst)
4422                 goto error;
4423
4424         n_in = isl_map_dim(opt, isl_dim_in);
4425         dim = isl_map_get_space(opt);
4426         dim = isl_space_drop_dims(dim, isl_dim_in, n_in - 1, 1);
4427         res = isl_map_empty(dim);
4428
4429         for (i = 0; i < opt->n; ++i) {
4430                 isl_map *map;
4431
4432                 map = isl_map_from_basic_map(isl_basic_map_copy(opt->p[i]));
4433                 if (need_split_basic_map(opt->p[i], cst))
4434                         map = isl_map_intersect_domain(map,
4435                                                        isl_set_copy(min_expr));
4436                 map = isl_map_remove_dims(map, isl_dim_in, n_in - 1, 1);
4437
4438                 res = isl_map_union_disjoint(res, map);
4439         }
4440
4441         isl_map_free(opt);
4442         isl_set_free(min_expr);
4443         isl_mat_free(cst);
4444         return res;
4445 error:
4446         isl_map_free(opt);
4447         isl_set_free(min_expr);
4448         isl_mat_free(cst);
4449         return NULL;
4450 }
4451
4452 static __isl_give isl_map *basic_map_partial_lexopt(
4453         __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4454         __isl_give isl_set **empty, int max);
4455
4456 union isl_lex_res {
4457         void *p;
4458         isl_map *map;
4459         isl_pw_multi_aff *pma;
4460 };
4461
4462 /* This function is called from basic_map_partial_lexopt_symm.
4463  * The last variable of "bmap" and "dom" corresponds to the minimum
4464  * of the bounds in "cst".  "map_space" is the space of the original
4465  * input relation (of basic_map_partial_lexopt_symm) and "set_space"
4466  * is the space of the original domain.
4467  *
4468  * We recursively call basic_map_partial_lexopt and then plug in
4469  * the definition of the minimum in the result.
4470  */
4471 static __isl_give union isl_lex_res basic_map_partial_lexopt_symm_map_core(
4472         __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4473         __isl_give isl_set **empty, int max, __isl_take isl_mat *cst,
4474         __isl_take isl_space *map_space, __isl_take isl_space *set_space)
4475 {
4476         isl_map *opt;
4477         isl_set *min_expr;
4478         union isl_lex_res res;
4479
4480         min_expr = set_minimum(isl_basic_set_get_space(dom), isl_mat_copy(cst));
4481
4482         opt = basic_map_partial_lexopt(bmap, dom, empty, max);
4483
4484         if (empty) {
4485                 *empty = split(*empty,
4486                                isl_set_copy(min_expr), isl_mat_copy(cst));
4487                 *empty = isl_set_reset_space(*empty, set_space);
4488         }
4489
4490         opt = split_domain(opt, min_expr, cst);
4491         opt = isl_map_reset_space(opt, map_space);
4492
4493         res.map = opt;
4494         return res;
4495 }
4496
4497 /* Given a basic map with at least two parallel constraints (as found
4498  * by the function parallel_constraints), first look for more constraints
4499  * parallel to the two constraint and replace the found list of parallel
4500  * constraints by a single constraint with as "input" part the minimum
4501  * of the input parts of the list of constraints.  Then, recursively call
4502  * basic_map_partial_lexopt (possibly finding more parallel constraints)
4503  * and plug in the definition of the minimum in the result.
4504  *
4505  * More specifically, given a set of constraints
4506  *
4507  *      a x + b_i(p) >= 0
4508  *
4509  * Replace this set by a single constraint
4510  *
4511  *      a x + u >= 0
4512  *
4513  * with u a new parameter with constraints
4514  *
4515  *      u <= b_i(p)
4516  *
4517  * Any solution to the new system is also a solution for the original system
4518  * since
4519  *
4520  *      a x >= -u >= -b_i(p)
4521  *
4522  * Moreover, m = min_i(b_i(p)) satisfies the constraints on u and can
4523  * therefore be plugged into the solution.
4524  */
4525 static union isl_lex_res basic_map_partial_lexopt_symm(
4526         __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4527         __isl_give isl_set **empty, int max, int first, int second,
4528         __isl_give union isl_lex_res (*core)(__isl_take isl_basic_map *bmap,
4529                                             __isl_take isl_basic_set *dom,
4530                                             __isl_give isl_set **empty,
4531                                             int max, __isl_take isl_mat *cst,
4532                                             __isl_take isl_space *map_space,
4533                                             __isl_take isl_space *set_space))
4534 {
4535         int i, n, k;
4536         int *list = NULL;
4537         unsigned n_in, n_out, n_div;
4538         isl_ctx *ctx;
4539         isl_vec *var = NULL;
4540         isl_mat *cst = NULL;
4541         isl_space *map_space, *set_space;
4542         union isl_lex_res res;
4543
4544         map_space = isl_basic_map_get_space(bmap);
4545         set_space = empty ? isl_basic_set_get_space(dom) : NULL;
4546
4547         n_in = isl_basic_map_dim(bmap, isl_dim_param) +
4548                isl_basic_map_dim(bmap, isl_dim_in);
4549         n_out = isl_basic_map_dim(bmap, isl_dim_all) - n_in;
4550
4551         ctx = isl_basic_map_get_ctx(bmap);
4552         list = isl_alloc_array(ctx, int, bmap->n_ineq);
4553         var = isl_vec_alloc(ctx, n_out);
4554         if (!list || !var)
4555                 goto error;
4556
4557         list[0] = first;
4558         list[1] = second;
4559         isl_seq_cpy(var->el, bmap->ineq[first] + 1 + n_in, n_out);
4560         for (i = second + 1, n = 2; i < bmap->n_ineq; ++i) {
4561                 if (isl_seq_eq(var->el, bmap->ineq[i] + 1 + n_in, n_out))
4562                         list[n++] = i;
4563         }
4564
4565         cst = isl_mat_alloc(ctx, n, 1 + n_in);
4566         if (!cst)
4567                 goto error;
4568
4569         for (i = 0; i < n; ++i)
4570                 isl_seq_cpy(cst->row[i], bmap->ineq[list[i]], 1 + n_in);
4571
4572         bmap = isl_basic_map_cow(bmap);
4573         if (!bmap)
4574                 goto error;
4575         for (i = n - 1; i >= 0; --i)
4576                 if (isl_basic_map_drop_inequality(bmap, list[i]) < 0)
4577                         goto error;
4578
4579         bmap = isl_basic_map_add(bmap, isl_dim_in, 1);
4580         bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
4581         k = isl_basic_map_alloc_inequality(bmap);
4582         if (k < 0)
4583                 goto error;
4584         isl_seq_clr(bmap->ineq[k], 1 + n_in);
4585         isl_int_set_si(bmap->ineq[k][1 + n_in], 1);
4586         isl_seq_cpy(bmap->ineq[k] + 1 + n_in + 1, var->el, n_out);
4587         bmap = isl_basic_map_finalize(bmap);
4588
4589         n_div = isl_basic_set_dim(dom, isl_dim_div);
4590         dom = isl_basic_set_add_dims(dom, isl_dim_set, 1);
4591         dom = isl_basic_set_extend_constraints(dom, 0, n);
4592         for (i = 0; i < n; ++i) {
4593                 k = isl_basic_set_alloc_inequality(dom);
4594                 if (k < 0)
4595                         goto error;
4596                 isl_seq_cpy(dom->ineq[k], cst->row[i], 1 + n_in);
4597                 isl_int_set_si(dom->ineq[k][1 + n_in], -1);
4598                 isl_seq_clr(dom->ineq[k] + 1 + n_in + 1, n_div);
4599         }
4600
4601         isl_vec_free(var);
4602         free(list);
4603
4604         return core(bmap, dom, empty, max, cst, map_space, set_space);
4605 error:
4606         isl_space_free(map_space);
4607         isl_space_free(set_space);
4608         isl_mat_free(cst);
4609         isl_vec_free(var);
4610         free(list);
4611         isl_basic_set_free(dom);
4612         isl_basic_map_free(bmap);
4613         res.p = NULL;
4614         return res;
4615 }
4616
4617 static __isl_give isl_map *basic_map_partial_lexopt_symm_map(
4618         __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4619         __isl_give isl_set **empty, int max, int first, int second)
4620 {
4621         return basic_map_partial_lexopt_symm(bmap, dom, empty, max,
4622                     first, second, &basic_map_partial_lexopt_symm_map_core).map;
4623 }
4624
4625 /* Recursive part of isl_tab_basic_map_partial_lexopt, after detecting
4626  * equalities and removing redundant constraints.
4627  *
4628  * We first check if there are any parallel constraints (left).
4629  * If not, we are in the base case.
4630  * If there are parallel constraints, we replace them by a single
4631  * constraint in basic_map_partial_lexopt_symm and then call
4632  * this function recursively to look for more parallel constraints.
4633  */
4634 static __isl_give isl_map *basic_map_partial_lexopt(
4635         __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4636         __isl_give isl_set **empty, int max)
4637 {
4638         int par = 0;
4639         int first, second;
4640
4641         if (!bmap)
4642                 goto error;
4643
4644         if (bmap->ctx->opt->pip_symmetry)
4645                 par = parallel_constraints(bmap, &first, &second);
4646         if (par < 0)
4647                 goto error;
4648         if (!par)
4649                 return basic_map_partial_lexopt_base_map(bmap, dom, empty, max);
4650         
4651         return basic_map_partial_lexopt_symm_map(bmap, dom, empty, max,
4652                                                  first, second);
4653 error:
4654         isl_basic_set_free(dom);
4655         isl_basic_map_free(bmap);
4656         return NULL;
4657 }
4658
4659 /* Compute the lexicographic minimum (or maximum if "max" is set)
4660  * of "bmap" over the domain "dom" and return the result as a map.
4661  * If "empty" is not NULL, then *empty is assigned a set that
4662  * contains those parts of the domain where there is no solution.
4663  * If "bmap" is marked as rational (ISL_BASIC_MAP_RATIONAL),
4664  * then we compute the rational optimum.  Otherwise, we compute
4665  * the integral optimum.
4666  *
4667  * We perform some preprocessing.  As the PILP solver does not
4668  * handle implicit equalities very well, we first make sure all
4669  * the equalities are explicitly available.
4670  *
4671  * We also add context constraints to the basic map and remove
4672  * redundant constraints.  This is only needed because of the
4673  * way we handle simple symmetries.  In particular, we currently look
4674  * for symmetries on the constraints, before we set up the main tableau.
4675  * It is then no good to look for symmetries on possibly redundant constraints.
4676  */
4677 struct isl_map *isl_tab_basic_map_partial_lexopt(
4678                 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4679                 struct isl_set **empty, int max)
4680 {
4681         if (empty)
4682                 *empty = NULL;
4683         if (!bmap || !dom)
4684                 goto error;
4685
4686         isl_assert(bmap->ctx,
4687             isl_basic_map_compatible_domain(bmap, dom), goto error);
4688
4689         if (isl_basic_set_dim(dom, isl_dim_all) == 0)
4690                 return basic_map_partial_lexopt(bmap, dom, empty, max);
4691
4692         bmap = isl_basic_map_intersect_domain(bmap, isl_basic_set_copy(dom));
4693         bmap = isl_basic_map_detect_equalities(bmap);
4694         bmap = isl_basic_map_remove_redundancies(bmap);
4695
4696         return basic_map_partial_lexopt(bmap, dom, empty, max);
4697 error:
4698         isl_basic_set_free(dom);
4699         isl_basic_map_free(bmap);
4700         return NULL;
4701 }
4702
4703 struct isl_sol_for {
4704         struct isl_sol  sol;
4705         int             (*fn)(__isl_take isl_basic_set *dom,
4706                                 __isl_take isl_aff_list *list, void *user);
4707         void            *user;
4708 };
4709
4710 static void sol_for_free(struct isl_sol_for *sol_for)
4711 {
4712         if (sol_for->sol.context)
4713                 sol_for->sol.context->op->free(sol_for->sol.context);
4714         free(sol_for);
4715 }
4716
4717 static void sol_for_free_wrap(struct isl_sol *sol)
4718 {
4719         sol_for_free((struct isl_sol_for *)sol);
4720 }
4721
4722 /* Add the solution identified by the tableau and the context tableau.
4723  *
4724  * See documentation of sol_add for more details.
4725  *
4726  * Instead of constructing a basic map, this function calls a user
4727  * defined function with the current context as a basic set and
4728  * a list of affine expressions representing the relation between
4729  * the input and output.  The space over which the affine expressions
4730  * are defined is the same as that of the domain.  The number of
4731  * affine expressions in the list is equal to the number of output variables.
4732  */
4733 static void sol_for_add(struct isl_sol_for *sol,
4734         struct isl_basic_set *dom, struct isl_mat *M)
4735 {
4736         int i;
4737         isl_ctx *ctx;
4738         isl_local_space *ls;
4739         isl_aff *aff;
4740         isl_aff_list *list;
4741
4742         if (sol->sol.error || !dom || !M)
4743                 goto error;
4744
4745         ctx = isl_basic_set_get_ctx(dom);
4746         ls = isl_basic_set_get_local_space(dom);
4747         list = isl_aff_list_alloc(ctx, M->n_row - 1);
4748         for (i = 1; i < M->n_row; ++i) {
4749                 aff = isl_aff_alloc(isl_local_space_copy(ls));
4750                 if (aff) {
4751                         isl_int_set(aff->v->el[0], M->row[0][0]);
4752                         isl_seq_cpy(aff->v->el + 1, M->row[i], M->n_col);
4753                 }
4754                 aff = isl_aff_normalize(aff);
4755                 list = isl_aff_list_add(list, aff);
4756         }
4757         isl_local_space_free(ls);
4758
4759         dom = isl_basic_set_finalize(dom);
4760
4761         if (sol->fn(isl_basic_set_copy(dom), list, sol->user) < 0)
4762                 goto error;
4763
4764         isl_basic_set_free(dom);
4765         isl_mat_free(M);
4766         return;
4767 error:
4768         isl_basic_set_free(dom);
4769         isl_mat_free(M);
4770         sol->sol.error = 1;
4771 }
4772
4773 static void sol_for_add_wrap(struct isl_sol *sol,
4774         struct isl_basic_set *dom, struct isl_mat *M)
4775 {
4776         sol_for_add((struct isl_sol_for *)sol, dom, M);
4777 }
4778
4779 static struct isl_sol_for *sol_for_init(struct isl_basic_map *bmap, int max,
4780         int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_aff_list *list,
4781                   void *user),
4782         void *user)
4783 {
4784         struct isl_sol_for *sol_for = NULL;
4785         isl_space *dom_dim;
4786         struct isl_basic_set *dom = NULL;
4787
4788         sol_for = isl_calloc_type(bmap->ctx, struct isl_sol_for);
4789         if (!sol_for)
4790                 goto error;
4791
4792         dom_dim = isl_space_domain(isl_space_copy(bmap->dim));
4793         dom = isl_basic_set_universe(dom_dim);
4794
4795         sol_for->sol.rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
4796         sol_for->sol.dec_level.callback.run = &sol_dec_level_wrap;
4797         sol_for->sol.dec_level.sol = &sol_for->sol;
4798         sol_for->fn = fn;
4799         sol_for->user = user;
4800         sol_for->sol.max = max;
4801         sol_for->sol.n_out = isl_basic_map_dim(bmap, isl_dim_out);
4802         sol_for->sol.add = &sol_for_add_wrap;
4803         sol_for->sol.add_empty = NULL;
4804         sol_for->sol.free = &sol_for_free_wrap;
4805
4806         sol_for->sol.context = isl_context_alloc(dom);
4807         if (!sol_for->sol.context)
4808                 goto error;
4809
4810         isl_basic_set_free(dom);
4811         return sol_for;
4812 error:
4813         isl_basic_set_free(dom);
4814         sol_for_free(sol_for);
4815         return NULL;
4816 }
4817
4818 static void sol_for_find_solutions(struct isl_sol_for *sol_for,
4819         struct isl_tab *tab)
4820 {
4821         find_solutions_main(&sol_for->sol, tab);
4822 }
4823
4824 int isl_basic_map_foreach_lexopt(__isl_keep isl_basic_map *bmap, int max,
4825         int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_aff_list *list,
4826                   void *user),
4827         void *user)
4828 {
4829         struct isl_sol_for *sol_for = NULL;
4830
4831         bmap = isl_basic_map_copy(bmap);
4832         bmap = isl_basic_map_detect_equalities(bmap);
4833         if (!bmap)
4834                 return -1;
4835
4836         sol_for = sol_for_init(bmap, max, fn, user);
4837         if (!sol_for)
4838                 goto error;
4839
4840         if (isl_basic_map_plain_is_empty(bmap))
4841                 /* nothing */;
4842         else {
4843                 struct isl_tab *tab;
4844                 struct isl_context *context = sol_for->sol.context;
4845                 tab = tab_for_lexmin(bmap,
4846                                 context->op->peek_basic_set(context), 1, max);
4847                 tab = context->op->detect_nonnegative_parameters(context, tab);
4848                 sol_for_find_solutions(sol_for, tab);
4849                 if (sol_for->sol.error)
4850                         goto error;
4851         }
4852
4853         sol_free(&sol_for->sol);
4854         isl_basic_map_free(bmap);
4855         return 0;
4856 error:
4857         sol_free(&sol_for->sol);
4858         isl_basic_map_free(bmap);
4859         return -1;
4860 }
4861
4862 int isl_basic_set_foreach_lexopt(__isl_keep isl_basic_set *bset, int max,
4863         int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_aff_list *list,
4864                   void *user),
4865         void *user)
4866 {
4867         return isl_basic_map_foreach_lexopt(bset, max, fn, user);
4868 }
4869
4870 /* Check if the given sequence of len variables starting at pos
4871  * represents a trivial (i.e., zero) solution.
4872  * The variables are assumed to be non-negative and to come in pairs,
4873  * with each pair representing a variable of unrestricted sign.
4874  * The solution is trivial if each such pair in the sequence consists
4875  * of two identical values, meaning that the variable being represented
4876  * has value zero.
4877  */
4878 static int region_is_trivial(struct isl_tab *tab, int pos, int len)
4879 {
4880         int i;
4881
4882         if (len == 0)
4883                 return 0;
4884
4885         for (i = 0; i < len; i +=  2) {
4886                 int neg_row;
4887                 int pos_row;
4888
4889                 neg_row = tab->var[pos + i].is_row ?
4890                                 tab->var[pos + i].index : -1;
4891                 pos_row = tab->var[pos + i + 1].is_row ?
4892                                 tab->var[pos + i + 1].index : -1;
4893
4894                 if ((neg_row < 0 ||
4895                      isl_int_is_zero(tab->mat->row[neg_row][1])) &&
4896                     (pos_row < 0 ||
4897                      isl_int_is_zero(tab->mat->row[pos_row][1])))
4898                         continue;
4899
4900                 if (neg_row < 0 || pos_row < 0)
4901                         return 0;
4902                 if (isl_int_ne(tab->mat->row[neg_row][1],
4903                                tab->mat->row[pos_row][1]))
4904                         return 0;
4905         }
4906
4907         return 1;
4908 }
4909
4910 /* Return the index of the first trivial region or -1 if all regions
4911  * are non-trivial.
4912  */
4913 static int first_trivial_region(struct isl_tab *tab,
4914         int n_region, struct isl_region *region)
4915 {
4916         int i;
4917
4918         for (i = 0; i < n_region; ++i) {
4919                 if (region_is_trivial(tab, region[i].pos, region[i].len))
4920                         return i;
4921         }
4922
4923         return -1;
4924 }
4925
4926 /* Check if the solution is optimal, i.e., whether the first
4927  * n_op entries are zero.
4928  */
4929 static int is_optimal(__isl_keep isl_vec *sol, int n_op)
4930 {
4931         int i;
4932
4933         for (i = 0; i < n_op; ++i)
4934                 if (!isl_int_is_zero(sol->el[1 + i]))
4935                         return 0;
4936         return 1;
4937 }
4938
4939 /* Add constraints to "tab" that ensure that any solution is significantly
4940  * better that that represented by "sol".  That is, find the first
4941  * relevant (within first n_op) non-zero coefficient and force it (along
4942  * with all previous coefficients) to be zero.
4943  * If the solution is already optimal (all relevant coefficients are zero),
4944  * then just mark the table as empty.
4945  */
4946 static int force_better_solution(struct isl_tab *tab,
4947         __isl_keep isl_vec *sol, int n_op)
4948 {
4949         int i;
4950         isl_ctx *ctx;
4951         isl_vec *v = NULL;
4952
4953         if (!sol)
4954                 return -1;
4955
4956         for (i = 0; i < n_op; ++i)
4957                 if (!isl_int_is_zero(sol->el[1 + i]))
4958                         break;
4959
4960         if (i == n_op) {
4961                 if (isl_tab_mark_empty(tab) < 0)
4962                         return -1;
4963                 return 0;
4964         }
4965
4966         ctx = isl_vec_get_ctx(sol);
4967         v = isl_vec_alloc(ctx, 1 + tab->n_var);
4968         if (!v)
4969                 return -1;
4970
4971         for (; i >= 0; --i) {
4972                 v = isl_vec_clr(v);
4973                 isl_int_set_si(v->el[1 + i], -1);
4974                 if (add_lexmin_eq(tab, v->el) < 0)
4975                         goto error;
4976         }
4977
4978         isl_vec_free(v);
4979         return 0;
4980 error:
4981         isl_vec_free(v);
4982         return -1;
4983 }
4984
4985 struct isl_trivial {
4986         int update;
4987         int region;
4988         int side;
4989         struct isl_tab_undo *snap;
4990 };
4991
4992 /* Return the lexicographically smallest non-trivial solution of the
4993  * given ILP problem.
4994  *
4995  * All variables are assumed to be non-negative.
4996  *
4997  * n_op is the number of initial coordinates to optimize.
4998  * That is, once a solution has been found, we will only continue looking
4999  * for solution that result in significantly better values for those
5000  * initial coordinates.  That is, we only continue looking for solutions
5001  * that increase the number of initial zeros in this sequence.
5002  *
5003  * A solution is non-trivial, if it is non-trivial on each of the
5004  * specified regions.  Each region represents a sequence of pairs
5005  * of variables.  A solution is non-trivial on such a region if
5006  * at least one of these pairs consists of different values, i.e.,
5007  * such that the non-negative variable represented by the pair is non-zero.
5008  *
5009  * Whenever a conflict is encountered, all constraints involved are
5010  * reported to the caller through a call to "conflict".
5011  *
5012  * We perform a simple branch-and-bound backtracking search.
5013  * Each level in the search represents initially trivial region that is forced
5014  * to be non-trivial.
5015  * At each level we consider n cases, where n is the length of the region.
5016  * In terms of the n/2 variables of unrestricted signs being encoded by
5017  * the region, we consider the cases
5018  *      x_0 >= 1
5019  *      x_0 <= -1
5020  *      x_0 = 0 and x_1 >= 1
5021  *      x_0 = 0 and x_1 <= -1
5022  *      x_0 = 0 and x_1 = 0 and x_2 >= 1
5023  *      x_0 = 0 and x_1 = 0 and x_2 <= -1
5024  *      ...
5025  * The cases are considered in this order, assuming that each pair
5026  * x_i_a x_i_b represents the value x_i_b - x_i_a.
5027  * That is, x_0 >= 1 is enforced by adding the constraint
5028  *      x_0_b - x_0_a >= 1
5029  */
5030 __isl_give isl_vec *isl_tab_basic_set_non_trivial_lexmin(
5031         __isl_take isl_basic_set *bset, int n_op, int n_region,
5032         struct isl_region *region,
5033         int (*conflict)(int con, void *user), void *user)
5034 {
5035         int i, j;
5036         int r;
5037         isl_ctx *ctx;
5038         isl_vec *v = NULL;
5039         isl_vec *sol = NULL;
5040         struct isl_tab *tab;
5041         struct isl_trivial *triv = NULL;
5042         int level, init;
5043
5044         if (!bset)
5045                 return NULL;
5046
5047         ctx = isl_basic_set_get_ctx(bset);
5048         sol = isl_vec_alloc(ctx, 0);
5049
5050         tab = tab_for_lexmin(bset, NULL, 0, 0);
5051         if (!tab)
5052                 goto error;
5053         tab->conflict = conflict;
5054         tab->conflict_user = user;
5055
5056         v = isl_vec_alloc(ctx, 1 + tab->n_var);
5057         triv = isl_calloc_array(ctx, struct isl_trivial, n_region);
5058         if (!v || !triv)
5059                 goto error;
5060
5061         level = 0;
5062         init = 1;
5063
5064         while (level >= 0) {
5065                 int side, base;
5066
5067                 if (init) {
5068                         tab = cut_to_integer_lexmin(tab, CUT_ONE);
5069                         if (!tab)
5070                                 goto error;
5071                         if (tab->empty)
5072                                 goto backtrack;
5073                         r = first_trivial_region(tab, n_region, region);
5074                         if (r < 0) {
5075                                 for (i = 0; i < level; ++i)
5076                                         triv[i].update = 1;
5077                                 isl_vec_free(sol);
5078                                 sol = isl_tab_get_sample_value(tab);
5079                                 if (!sol)
5080                                         goto error;
5081                                 if (is_optimal(sol, n_op))
5082                                         break;
5083                                 goto backtrack;
5084                         }
5085                         if (level >= n_region)
5086                                 isl_die(ctx, isl_error_internal,
5087                                         "nesting level too deep", goto error);
5088                         if (isl_tab_extend_cons(tab,
5089                                             2 * region[r].len + 2 * n_op) < 0)
5090                                 goto error;
5091                         triv[level].region = r;
5092                         triv[level].side = 0;
5093                 }
5094
5095                 r = triv[level].region;
5096                 side = triv[level].side;
5097                 base = 2 * (side/2);
5098
5099                 if (side >= region[r].len) {
5100 backtrack:
5101                         level--;
5102                         init = 0;
5103                         if (level >= 0)
5104                                 if (isl_tab_rollback(tab, triv[level].snap) < 0)
5105                                         goto error;
5106                         continue;
5107                 }
5108
5109                 if (triv[level].update) {
5110                         if (force_better_solution(tab, sol, n_op) < 0)
5111                                 goto error;
5112                         triv[level].update = 0;
5113                 }
5114
5115                 if (side == base && base >= 2) {
5116                         for (j = base - 2; j < base; ++j) {
5117                                 v = isl_vec_clr(v);
5118                                 isl_int_set_si(v->el[1 + region[r].pos + j], 1);
5119                                 if (add_lexmin_eq(tab, v->el) < 0)
5120                                         goto error;
5121                         }
5122                 }
5123
5124                 triv[level].snap = isl_tab_snap(tab);
5125                 if (isl_tab_push_basis(tab) < 0)
5126                         goto error;
5127
5128                 v = isl_vec_clr(v);
5129                 isl_int_set_si(v->el[0], -1);
5130                 isl_int_set_si(v->el[1 + region[r].pos + side], -1);
5131                 isl_int_set_si(v->el[1 + region[r].pos + (side ^ 1)], 1);
5132                 tab = add_lexmin_ineq(tab, v->el);
5133
5134                 triv[level].side++;
5135                 level++;
5136                 init = 1;
5137         }
5138
5139         free(triv);
5140         isl_vec_free(v);
5141         isl_tab_free(tab);
5142         isl_basic_set_free(bset);
5143
5144         return sol;
5145 error:
5146         free(triv);
5147         isl_vec_free(v);
5148         isl_tab_free(tab);
5149         isl_basic_set_free(bset);
5150         isl_vec_free(sol);
5151         return NULL;
5152 }
5153
5154 /* Return the lexicographically smallest rational point in "bset",
5155  * assuming that all variables are non-negative.
5156  * If "bset" is empty, then return a zero-length vector.
5157  */
5158 __isl_give isl_vec *isl_tab_basic_set_non_neg_lexmin(
5159         __isl_take isl_basic_set *bset)
5160 {
5161         struct isl_tab *tab;
5162         isl_ctx *ctx = isl_basic_set_get_ctx(bset);
5163         isl_vec *sol;
5164
5165         if (!bset)
5166                 return NULL;
5167
5168         tab = tab_for_lexmin(bset, NULL, 0, 0);
5169         if (!tab)
5170                 goto error;
5171         if (tab->empty)
5172                 sol = isl_vec_alloc(ctx, 0);
5173         else
5174                 sol = isl_tab_get_sample_value(tab);
5175         isl_tab_free(tab);
5176         isl_basic_set_free(bset);
5177         return sol;
5178 error:
5179         isl_tab_free(tab);
5180         isl_basic_set_free(bset);
5181         return NULL;
5182 }
5183
5184 struct isl_sol_pma {
5185         struct isl_sol  sol;
5186         isl_pw_multi_aff *pma;
5187         isl_set *empty;
5188 };
5189
5190 static void sol_pma_free(struct isl_sol_pma *sol_pma)
5191 {
5192         if (!sol_pma)
5193                 return;
5194         if (sol_pma->sol.context)
5195                 sol_pma->sol.context->op->free(sol_pma->sol.context);
5196         isl_pw_multi_aff_free(sol_pma->pma);
5197         isl_set_free(sol_pma->empty);
5198         free(sol_pma);
5199 }
5200
5201 /* This function is called for parts of the context where there is
5202  * no solution, with "bset" corresponding to the context tableau.
5203  * Simply add the basic set to the set "empty".
5204  */
5205 static void sol_pma_add_empty(struct isl_sol_pma *sol,
5206         __isl_take isl_basic_set *bset)
5207 {
5208         if (!bset)
5209                 goto error;
5210         isl_assert(bset->ctx, sol->empty, goto error);
5211
5212         sol->empty = isl_set_grow(sol->empty, 1);
5213         bset = isl_basic_set_simplify(bset);
5214         bset = isl_basic_set_finalize(bset);
5215         sol->empty = isl_set_add_basic_set(sol->empty, bset);
5216         if (!sol->empty)
5217                 sol->sol.error = 1;
5218         return;
5219 error:
5220         isl_basic_set_free(bset);
5221         sol->sol.error = 1;
5222 }
5223
5224 /* Given a basic map "dom" that represents the context and an affine
5225  * matrix "M" that maps the dimensions of the context to the
5226  * output variables, construct an isl_pw_multi_aff with a single
5227  * cell corresponding to "dom" and affine expressions copied from "M".
5228  */
5229 static void sol_pma_add(struct isl_sol_pma *sol,
5230         __isl_take isl_basic_set *dom, __isl_take isl_mat *M)
5231 {
5232         int i;
5233         isl_local_space *ls;
5234         isl_aff *aff;
5235         isl_multi_aff *maff;
5236         isl_pw_multi_aff *pma;
5237
5238         maff = isl_multi_aff_alloc(isl_pw_multi_aff_get_space(sol->pma));
5239         ls = isl_basic_set_get_local_space(dom);
5240         for (i = 1; i < M->n_row; ++i) {
5241                 aff = isl_aff_alloc(isl_local_space_copy(ls));
5242                 if (aff) {
5243                         isl_int_set(aff->v->el[0], M->row[0][0]);
5244                         isl_seq_cpy(aff->v->el + 1, M->row[i], M->n_col);
5245                 }
5246                 aff = isl_aff_normalize(aff);
5247                 maff = isl_multi_aff_set_aff(maff, i - 1, aff);
5248         }
5249         isl_local_space_free(ls);
5250         isl_mat_free(M);
5251         dom = isl_basic_set_simplify(dom);
5252         dom = isl_basic_set_finalize(dom);
5253         pma = isl_pw_multi_aff_alloc(isl_set_from_basic_set(dom), maff);
5254         sol->pma = isl_pw_multi_aff_add_disjoint(sol->pma, pma);
5255         if (!sol->pma)
5256                 sol->sol.error = 1;
5257 }
5258
5259 static void sol_pma_free_wrap(struct isl_sol *sol)
5260 {
5261         sol_pma_free((struct isl_sol_pma *)sol);
5262 }
5263
5264 static void sol_pma_add_empty_wrap(struct isl_sol *sol,
5265         __isl_take isl_basic_set *bset)
5266 {
5267         sol_pma_add_empty((struct isl_sol_pma *)sol, bset);
5268 }
5269
5270 static void sol_pma_add_wrap(struct isl_sol *sol,
5271         __isl_take isl_basic_set *dom, __isl_take isl_mat *M)
5272 {
5273         sol_pma_add((struct isl_sol_pma *)sol, dom, M);
5274 }
5275
5276 /* Construct an isl_sol_pma structure for accumulating the solution.
5277  * If track_empty is set, then we also keep track of the parts
5278  * of the context where there is no solution.
5279  * If max is set, then we are solving a maximization, rather than
5280  * a minimization problem, which means that the variables in the
5281  * tableau have value "M - x" rather than "M + x".
5282  */
5283 static struct isl_sol *sol_pma_init(__isl_keep isl_basic_map *bmap,
5284         __isl_take isl_basic_set *dom, int track_empty, int max)
5285 {
5286         struct isl_sol_pma *sol_pma = NULL;
5287
5288         if (!bmap)
5289                 goto error;
5290
5291         sol_pma = isl_calloc_type(bmap->ctx, struct isl_sol_pma);
5292         if (!sol_pma)
5293                 goto error;
5294
5295         sol_pma->sol.rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
5296         sol_pma->sol.dec_level.callback.run = &sol_dec_level_wrap;
5297         sol_pma->sol.dec_level.sol = &sol_pma->sol;
5298         sol_pma->sol.max = max;
5299         sol_pma->sol.n_out = isl_basic_map_dim(bmap, isl_dim_out);
5300         sol_pma->sol.add = &sol_pma_add_wrap;
5301         sol_pma->sol.add_empty = track_empty ? &sol_pma_add_empty_wrap : NULL;
5302         sol_pma->sol.free = &sol_pma_free_wrap;
5303         sol_pma->pma = isl_pw_multi_aff_empty(isl_basic_map_get_space(bmap));
5304         if (!sol_pma->pma)
5305                 goto error;
5306
5307         sol_pma->sol.context = isl_context_alloc(dom);
5308         if (!sol_pma->sol.context)
5309                 goto error;
5310
5311         if (track_empty) {
5312                 sol_pma->empty = isl_set_alloc_space(isl_basic_set_get_space(dom),
5313                                                         1, ISL_SET_DISJOINT);
5314                 if (!sol_pma->empty)
5315                         goto error;
5316         }
5317
5318         isl_basic_set_free(dom);
5319         return &sol_pma->sol;
5320 error:
5321         isl_basic_set_free(dom);
5322         sol_pma_free(sol_pma);
5323         return NULL;
5324 }
5325
5326 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
5327  * some obvious symmetries.
5328  *
5329  * We call basic_map_partial_lexopt_base and extract the results.
5330  */
5331 static __isl_give isl_pw_multi_aff *basic_map_partial_lexopt_base_pma(
5332         __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5333         __isl_give isl_set **empty, int max)
5334 {
5335         isl_pw_multi_aff *result = NULL;
5336         struct isl_sol *sol;
5337         struct isl_sol_pma *sol_pma;
5338
5339         sol = basic_map_partial_lexopt_base(bmap, dom, empty, max,
5340                                             &sol_pma_init);
5341         if (!sol)
5342                 return NULL;
5343         sol_pma = (struct isl_sol_pma *) sol;
5344
5345         result = isl_pw_multi_aff_copy(sol_pma->pma);
5346         if (empty)
5347                 *empty = isl_set_copy(sol_pma->empty);
5348         sol_free(&sol_pma->sol);
5349         return result;
5350 }
5351
5352 /* Given that the last input variable of "maff" represents the minimum
5353  * of some bounds, check whether we need to plug in the expression
5354  * of the minimum.
5355  *
5356  * In particular, check if the last input variable appears in any
5357  * of the expressions in "maff".
5358  */
5359 static int need_substitution(__isl_keep isl_multi_aff *maff)
5360 {
5361         int i;
5362         unsigned pos;
5363
5364         pos = isl_multi_aff_dim(maff, isl_dim_in) - 1;
5365
5366         for (i = 0; i < maff->n; ++i)
5367                 if (isl_aff_involves_dims(maff->p[i], isl_dim_in, pos, 1))
5368                         return 1;
5369
5370         return 0;
5371 }
5372
5373 /* Given a set of upper bounds on the last "input" variable m,
5374  * construct a piecewise affine expression that selects
5375  * the minimal upper bound to m, i.e.,
5376  * divide the space into cells where one
5377  * of the upper bounds is smaller than all the others and select
5378  * this upper bound on that cell.
5379  *
5380  * In particular, if there are n bounds b_i, then the result
5381  * consists of n cell, each one of the form
5382  *
5383  *      b_i <= b_j      for j > i
5384  *      b_i <  b_j      for j < i
5385  *
5386  * The affine expression on this cell is
5387  *
5388  *      b_i
5389  */
5390 static __isl_give isl_pw_aff *set_minimum_pa(__isl_take isl_space *space,
5391         __isl_take isl_mat *var)
5392 {
5393         int i;
5394         isl_aff *aff = NULL;
5395         isl_basic_set *bset = NULL;
5396         isl_ctx *ctx;
5397         isl_pw_aff *paff = NULL;
5398         isl_space *pw_space;
5399         isl_local_space *ls = NULL;
5400
5401         if (!space || !var)
5402                 goto error;
5403
5404         ctx = isl_space_get_ctx(space);
5405         ls = isl_local_space_from_space(isl_space_copy(space));
5406         pw_space = isl_space_copy(space);
5407         pw_space = isl_space_from_domain(pw_space);
5408         pw_space = isl_space_add_dims(pw_space, isl_dim_out, 1);
5409         paff = isl_pw_aff_alloc_size(pw_space, var->n_row);
5410
5411         for (i = 0; i < var->n_row; ++i) {
5412                 isl_pw_aff *paff_i;
5413
5414                 aff = isl_aff_alloc(isl_local_space_copy(ls));
5415                 bset = isl_basic_set_alloc_space(isl_space_copy(space), 0,
5416                                                0, var->n_row - 1);
5417                 if (!aff || !bset)
5418                         goto error;
5419                 isl_int_set_si(aff->v->el[0], 1);
5420                 isl_seq_cpy(aff->v->el + 1, var->row[i], var->n_col);
5421                 isl_int_set_si(aff->v->el[1 + var->n_col], 0);
5422                 bset = select_minimum(bset, var, i);
5423                 paff_i = isl_pw_aff_alloc(isl_set_from_basic_set(bset), aff);
5424                 paff = isl_pw_aff_add_disjoint(paff, paff_i);
5425         }
5426
5427         isl_local_space_free(ls);
5428         isl_space_free(space);
5429         isl_mat_free(var);
5430         return paff;
5431 error:
5432         isl_aff_free(aff);
5433         isl_basic_set_free(bset);
5434         isl_pw_aff_free(paff);
5435         isl_local_space_free(ls);
5436         isl_space_free(space);
5437         isl_mat_free(var);
5438         return NULL;
5439 }
5440
5441 /* Given a piecewise multi-affine expression of which the last input variable
5442  * is the minimum of the bounds in "cst", plug in the value of the minimum.
5443  * This minimum expression is given in "min_expr_pa".
5444  * The set "min_expr" contains the same information, but in the form of a set.
5445  * The variable is subsequently projected out.
5446  *
5447  * The implementation is similar to those of "split" and "split_domain".
5448  * If the variable appears in a given expression, then minimum expression
5449  * is plugged in.  Otherwise, if the variable appears in the constraints
5450  * and a split is required, then the domain is split.  Otherwise, no split
5451  * is performed.
5452  */
5453 static __isl_give isl_pw_multi_aff *split_domain_pma(
5454         __isl_take isl_pw_multi_aff *opt, __isl_take isl_pw_aff *min_expr_pa,
5455         __isl_take isl_set *min_expr, __isl_take isl_mat *cst)
5456 {
5457         int n_in;
5458         int i;
5459         isl_space *space;
5460         isl_pw_multi_aff *res;
5461
5462         if (!opt || !min_expr || !cst)
5463                 goto error;
5464
5465         n_in = isl_pw_multi_aff_dim(opt, isl_dim_in);
5466         space = isl_pw_multi_aff_get_space(opt);
5467         space = isl_space_drop_dims(space, isl_dim_in, n_in - 1, 1);
5468         res = isl_pw_multi_aff_empty(space);
5469
5470         for (i = 0; i < opt->n; ++i) {
5471                 isl_pw_multi_aff *pma;
5472
5473                 pma = isl_pw_multi_aff_alloc(isl_set_copy(opt->p[i].set),
5474                                          isl_multi_aff_copy(opt->p[i].maff));
5475                 if (need_substitution(opt->p[i].maff))
5476                         pma = isl_pw_multi_aff_substitute(pma,
5477                                         isl_dim_in, n_in - 1, min_expr_pa);
5478                 else if (need_split_set(opt->p[i].set, cst))
5479                         pma = isl_pw_multi_aff_intersect_domain(pma,
5480                                                        isl_set_copy(min_expr));
5481                 pma = isl_pw_multi_aff_project_out(pma,
5482                                                     isl_dim_in, n_in - 1, 1);
5483
5484                 res = isl_pw_multi_aff_add_disjoint(res, pma);
5485         }
5486
5487         isl_pw_multi_aff_free(opt);
5488         isl_pw_aff_free(min_expr_pa);
5489         isl_set_free(min_expr);
5490         isl_mat_free(cst);
5491         return res;
5492 error:
5493         isl_pw_multi_aff_free(opt);
5494         isl_pw_aff_free(min_expr_pa);
5495         isl_set_free(min_expr);
5496         isl_mat_free(cst);
5497         return NULL;
5498 }
5499
5500 static __isl_give isl_pw_multi_aff *basic_map_partial_lexopt_pma(
5501         __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5502         __isl_give isl_set **empty, int max);
5503
5504 /* This function is called from basic_map_partial_lexopt_symm.
5505  * The last variable of "bmap" and "dom" corresponds to the minimum
5506  * of the bounds in "cst".  "map_space" is the space of the original
5507  * input relation (of basic_map_partial_lexopt_symm) and "set_space"
5508  * is the space of the original domain.
5509  *
5510  * We recursively call basic_map_partial_lexopt and then plug in
5511  * the definition of the minimum in the result.
5512  */
5513 static __isl_give union isl_lex_res basic_map_partial_lexopt_symm_pma_core(
5514         __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5515         __isl_give isl_set **empty, int max, __isl_take isl_mat *cst,
5516         __isl_take isl_space *map_space, __isl_take isl_space *set_space)
5517 {
5518         isl_pw_multi_aff *opt;
5519         isl_pw_aff *min_expr_pa;
5520         isl_set *min_expr;
5521         union isl_lex_res res;
5522
5523         min_expr = set_minimum(isl_basic_set_get_space(dom), isl_mat_copy(cst));
5524         min_expr_pa = set_minimum_pa(isl_basic_set_get_space(dom),
5525                                         isl_mat_copy(cst));
5526
5527         opt = basic_map_partial_lexopt_pma(bmap, dom, empty, max);
5528
5529         if (empty) {
5530                 *empty = split(*empty,
5531                                isl_set_copy(min_expr), isl_mat_copy(cst));
5532                 *empty = isl_set_reset_space(*empty, set_space);
5533         }
5534
5535         opt = split_domain_pma(opt, min_expr_pa, min_expr, cst);
5536         opt = isl_pw_multi_aff_reset_space(opt, map_space);
5537
5538         res.pma = opt;
5539         return res;
5540 }
5541
5542 static __isl_give isl_pw_multi_aff *basic_map_partial_lexopt_symm_pma(
5543         __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5544         __isl_give isl_set **empty, int max, int first, int second)
5545 {
5546         return basic_map_partial_lexopt_symm(bmap, dom, empty, max,
5547                     first, second, &basic_map_partial_lexopt_symm_pma_core).pma;
5548 }
5549
5550 /* Recursive part of isl_basic_map_partial_lexopt_pw_multi_aff, after detecting
5551  * equalities and removing redundant constraints.
5552  *
5553  * We first check if there are any parallel constraints (left).
5554  * If not, we are in the base case.
5555  * If there are parallel constraints, we replace them by a single
5556  * constraint in basic_map_partial_lexopt_symm_pma and then call
5557  * this function recursively to look for more parallel constraints.
5558  */
5559 static __isl_give isl_pw_multi_aff *basic_map_partial_lexopt_pma(
5560         __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5561         __isl_give isl_set **empty, int max)
5562 {
5563         int par = 0;
5564         int first, second;
5565
5566         if (!bmap)
5567                 goto error;
5568
5569         if (bmap->ctx->opt->pip_symmetry)
5570                 par = parallel_constraints(bmap, &first, &second);
5571         if (par < 0)
5572                 goto error;
5573         if (!par)
5574                 return basic_map_partial_lexopt_base_pma(bmap, dom, empty, max);
5575         
5576         return basic_map_partial_lexopt_symm_pma(bmap, dom, empty, max,
5577                                                  first, second);
5578 error:
5579         isl_basic_set_free(dom);
5580         isl_basic_map_free(bmap);
5581         return NULL;
5582 }
5583
5584 /* Compute the lexicographic minimum (or maximum if "max" is set)
5585  * of "bmap" over the domain "dom" and return the result as a piecewise
5586  * multi-affine expression.
5587  * If "empty" is not NULL, then *empty is assigned a set that
5588  * contains those parts of the domain where there is no solution.
5589  * If "bmap" is marked as rational (ISL_BASIC_MAP_RATIONAL),
5590  * then we compute the rational optimum.  Otherwise, we compute
5591  * the integral optimum.
5592  *
5593  * We perform some preprocessing.  As the PILP solver does not
5594  * handle implicit equalities very well, we first make sure all
5595  * the equalities are explicitly available.
5596  *
5597  * We also add context constraints to the basic map and remove
5598  * redundant constraints.  This is only needed because of the
5599  * way we handle simple symmetries.  In particular, we currently look
5600  * for symmetries on the constraints, before we set up the main tableau.
5601  * It is then no good to look for symmetries on possibly redundant constraints.
5602  */
5603 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexopt_pw_multi_aff(
5604         __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5605         __isl_give isl_set **empty, int max)
5606 {
5607         if (empty)
5608                 *empty = NULL;
5609         if (!bmap || !dom)
5610                 goto error;
5611
5612         isl_assert(bmap->ctx,
5613             isl_basic_map_compatible_domain(bmap, dom), goto error);
5614
5615         if (isl_basic_set_dim(dom, isl_dim_all) == 0)
5616                 return basic_map_partial_lexopt_pma(bmap, dom, empty, max);
5617
5618         bmap = isl_basic_map_intersect_domain(bmap, isl_basic_set_copy(dom));
5619         bmap = isl_basic_map_detect_equalities(bmap);
5620         bmap = isl_basic_map_remove_redundancies(bmap);
5621
5622         return basic_map_partial_lexopt_pma(bmap, dom, empty, max);
5623 error:
5624         isl_basic_set_free(dom);
5625         isl_basic_map_free(bmap);
5626         return NULL;
5627 }