2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
5 * Use of this software is governed by the GNU LGPLv2.1 license
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
13 #include <isl_ctx_private.h>
14 #include "isl_map_private.h"
17 #include "isl_sample.h"
18 #include <isl_mat_private.h>
21 * The implementation of parametric integer linear programming in this file
22 * was inspired by the paper "Parametric Integer Programming" and the
23 * report "Solving systems of affine (in)equalities" by Paul Feautrier
26 * The strategy used for obtaining a feasible solution is different
27 * from the one used in isl_tab.c. In particular, in isl_tab.c,
28 * upon finding a constraint that is not yet satisfied, we pivot
29 * in a row that increases the constant term of the row holding the
30 * constraint, making sure the sample solution remains feasible
31 * for all the constraints it already satisfied.
32 * Here, we always pivot in the row holding the constraint,
33 * choosing a column that induces the lexicographically smallest
34 * increment to the sample solution.
36 * By starting out from a sample value that is lexicographically
37 * smaller than any integer point in the problem space, the first
38 * feasible integer sample point we find will also be the lexicographically
39 * smallest. If all variables can be assumed to be non-negative,
40 * then the initial sample value may be chosen equal to zero.
41 * However, we will not make this assumption. Instead, we apply
42 * the "big parameter" trick. Any variable x is then not directly
43 * used in the tableau, but instead it is represented by another
44 * variable x' = M + x, where M is an arbitrarily large (positive)
45 * value. x' is therefore always non-negative, whatever the value of x.
46 * Taking as initial sample value x' = 0 corresponds to x = -M,
47 * which is always smaller than any possible value of x.
49 * The big parameter trick is used in the main tableau and
50 * also in the context tableau if isl_context_lex is used.
51 * In this case, each tableaus has its own big parameter.
52 * Before doing any real work, we check if all the parameters
53 * happen to be non-negative. If so, we drop the column corresponding
54 * to M from the initial context tableau.
55 * If isl_context_gbr is used, then the big parameter trick is only
56 * used in the main tableau.
60 struct isl_context_op
{
61 /* detect nonnegative parameters in context and mark them in tab */
62 struct isl_tab
*(*detect_nonnegative_parameters
)(
63 struct isl_context
*context
, struct isl_tab
*tab
);
64 /* return temporary reference to basic set representation of context */
65 struct isl_basic_set
*(*peek_basic_set
)(struct isl_context
*context
);
66 /* return temporary reference to tableau representation of context */
67 struct isl_tab
*(*peek_tab
)(struct isl_context
*context
);
68 /* add equality; check is 1 if eq may not be valid;
69 * update is 1 if we may want to call ineq_sign on context later.
71 void (*add_eq
)(struct isl_context
*context
, isl_int
*eq
,
72 int check
, int update
);
73 /* add inequality; check is 1 if ineq may not be valid;
74 * update is 1 if we may want to call ineq_sign on context later.
76 void (*add_ineq
)(struct isl_context
*context
, isl_int
*ineq
,
77 int check
, int update
);
78 /* check sign of ineq based on previous information.
79 * strict is 1 if saturation should be treated as a positive sign.
81 enum isl_tab_row_sign (*ineq_sign
)(struct isl_context
*context
,
82 isl_int
*ineq
, int strict
);
83 /* check if inequality maintains feasibility */
84 int (*test_ineq
)(struct isl_context
*context
, isl_int
*ineq
);
85 /* return index of a div that corresponds to "div" */
86 int (*get_div
)(struct isl_context
*context
, struct isl_tab
*tab
,
88 /* add div "div" to context and return non-negativity */
89 int (*add_div
)(struct isl_context
*context
, struct isl_vec
*div
);
90 int (*detect_equalities
)(struct isl_context
*context
,
92 /* return row index of "best" split */
93 int (*best_split
)(struct isl_context
*context
, struct isl_tab
*tab
);
94 /* check if context has already been determined to be empty */
95 int (*is_empty
)(struct isl_context
*context
);
96 /* check if context is still usable */
97 int (*is_ok
)(struct isl_context
*context
);
98 /* save a copy/snapshot of context */
99 void *(*save
)(struct isl_context
*context
);
100 /* restore saved context */
101 void (*restore
)(struct isl_context
*context
, void *);
102 /* invalidate context */
103 void (*invalidate
)(struct isl_context
*context
);
105 void (*free
)(struct isl_context
*context
);
109 struct isl_context_op
*op
;
112 struct isl_context_lex
{
113 struct isl_context context
;
117 struct isl_partial_sol
{
119 struct isl_basic_set
*dom
;
122 struct isl_partial_sol
*next
;
126 struct isl_sol_callback
{
127 struct isl_tab_callback callback
;
131 /* isl_sol is an interface for constructing a solution to
132 * a parametric integer linear programming problem.
133 * Every time the algorithm reaches a state where a solution
134 * can be read off from the tableau (including cases where the tableau
135 * is empty), the function "add" is called on the isl_sol passed
136 * to find_solutions_main.
138 * The context tableau is owned by isl_sol and is updated incrementally.
140 * There are currently two implementations of this interface,
141 * isl_sol_map, which simply collects the solutions in an isl_map
142 * and (optionally) the parts of the context where there is no solution
144 * isl_sol_for, which calls a user-defined function for each part of
153 struct isl_context
*context
;
154 struct isl_partial_sol
*partial
;
155 void (*add
)(struct isl_sol
*sol
,
156 struct isl_basic_set
*dom
, struct isl_mat
*M
);
157 void (*add_empty
)(struct isl_sol
*sol
, struct isl_basic_set
*bset
);
158 void (*free
)(struct isl_sol
*sol
);
159 struct isl_sol_callback dec_level
;
162 static void sol_free(struct isl_sol
*sol
)
164 struct isl_partial_sol
*partial
, *next
;
167 for (partial
= sol
->partial
; partial
; partial
= next
) {
168 next
= partial
->next
;
169 isl_basic_set_free(partial
->dom
);
170 isl_mat_free(partial
->M
);
176 /* Push a partial solution represented by a domain and mapping M
177 * onto the stack of partial solutions.
179 static void sol_push_sol(struct isl_sol
*sol
,
180 struct isl_basic_set
*dom
, struct isl_mat
*M
)
182 struct isl_partial_sol
*partial
;
184 if (sol
->error
|| !dom
)
187 partial
= isl_alloc_type(dom
->ctx
, struct isl_partial_sol
);
191 partial
->level
= sol
->level
;
194 partial
->next
= sol
->partial
;
196 sol
->partial
= partial
;
200 isl_basic_set_free(dom
);
204 /* Pop one partial solution from the partial solution stack and
205 * pass it on to sol->add or sol->add_empty.
207 static void sol_pop_one(struct isl_sol
*sol
)
209 struct isl_partial_sol
*partial
;
211 partial
= sol
->partial
;
212 sol
->partial
= partial
->next
;
215 sol
->add(sol
, partial
->dom
, partial
->M
);
217 sol
->add_empty(sol
, partial
->dom
);
221 /* Return a fresh copy of the domain represented by the context tableau.
223 static struct isl_basic_set
*sol_domain(struct isl_sol
*sol
)
225 struct isl_basic_set
*bset
;
230 bset
= isl_basic_set_dup(sol
->context
->op
->peek_basic_set(sol
->context
));
231 bset
= isl_basic_set_update_from_tab(bset
,
232 sol
->context
->op
->peek_tab(sol
->context
));
237 /* Check whether two partial solutions have the same mapping, where n_div
238 * is the number of divs that the two partial solutions have in common.
240 static int same_solution(struct isl_partial_sol
*s1
, struct isl_partial_sol
*s2
,
246 if (!s1
->M
!= !s2
->M
)
251 dim
= isl_basic_set_total_dim(s1
->dom
) - s1
->dom
->n_div
;
253 for (i
= 0; i
< s1
->M
->n_row
; ++i
) {
254 if (isl_seq_first_non_zero(s1
->M
->row
[i
]+1+dim
+n_div
,
255 s1
->M
->n_col
-1-dim
-n_div
) != -1)
257 if (isl_seq_first_non_zero(s2
->M
->row
[i
]+1+dim
+n_div
,
258 s2
->M
->n_col
-1-dim
-n_div
) != -1)
260 if (!isl_seq_eq(s1
->M
->row
[i
], s2
->M
->row
[i
], 1+dim
+n_div
))
266 /* Pop all solutions from the partial solution stack that were pushed onto
267 * the stack at levels that are deeper than the current level.
268 * If the two topmost elements on the stack have the same level
269 * and represent the same solution, then their domains are combined.
270 * This combined domain is the same as the current context domain
271 * as sol_pop is called each time we move back to a higher level.
273 static void sol_pop(struct isl_sol
*sol
)
275 struct isl_partial_sol
*partial
;
281 if (sol
->level
== 0) {
282 for (partial
= sol
->partial
; partial
; partial
= sol
->partial
)
287 partial
= sol
->partial
;
291 if (partial
->level
<= sol
->level
)
294 if (partial
->next
&& partial
->next
->level
== partial
->level
) {
295 n_div
= isl_basic_set_dim(
296 sol
->context
->op
->peek_basic_set(sol
->context
),
299 if (!same_solution(partial
, partial
->next
, n_div
)) {
303 struct isl_basic_set
*bset
;
305 bset
= sol_domain(sol
);
307 isl_basic_set_free(partial
->next
->dom
);
308 partial
->next
->dom
= bset
;
309 partial
->next
->level
= sol
->level
;
311 sol
->partial
= partial
->next
;
312 isl_basic_set_free(partial
->dom
);
313 isl_mat_free(partial
->M
);
320 static void sol_dec_level(struct isl_sol
*sol
)
330 static int sol_dec_level_wrap(struct isl_tab_callback
*cb
)
332 struct isl_sol_callback
*callback
= (struct isl_sol_callback
*)cb
;
334 sol_dec_level(callback
->sol
);
336 return callback
->sol
->error
? -1 : 0;
339 /* Move down to next level and push callback onto context tableau
340 * to decrease the level again when it gets rolled back across
341 * the current state. That is, dec_level will be called with
342 * the context tableau in the same state as it is when inc_level
345 static void sol_inc_level(struct isl_sol
*sol
)
353 tab
= sol
->context
->op
->peek_tab(sol
->context
);
354 if (isl_tab_push_callback(tab
, &sol
->dec_level
.callback
) < 0)
358 static void scale_rows(struct isl_mat
*mat
, isl_int m
, int n_row
)
362 if (isl_int_is_one(m
))
365 for (i
= 0; i
< n_row
; ++i
)
366 isl_seq_scale(mat
->row
[i
], mat
->row
[i
], m
, mat
->n_col
);
369 /* Add the solution identified by the tableau and the context tableau.
371 * The layout of the variables is as follows.
372 * tab->n_var is equal to the total number of variables in the input
373 * map (including divs that were copied from the context)
374 * + the number of extra divs constructed
375 * Of these, the first tab->n_param and the last tab->n_div variables
376 * correspond to the variables in the context, i.e.,
377 * tab->n_param + tab->n_div = context_tab->n_var
378 * tab->n_param is equal to the number of parameters and input
379 * dimensions in the input map
380 * tab->n_div is equal to the number of divs in the context
382 * If there is no solution, then call add_empty with a basic set
383 * that corresponds to the context tableau. (If add_empty is NULL,
386 * If there is a solution, then first construct a matrix that maps
387 * all dimensions of the context to the output variables, i.e.,
388 * the output dimensions in the input map.
389 * The divs in the input map (if any) that do not correspond to any
390 * div in the context do not appear in the solution.
391 * The algorithm will make sure that they have an integer value,
392 * but these values themselves are of no interest.
393 * We have to be careful not to drop or rearrange any divs in the
394 * context because that would change the meaning of the matrix.
396 * To extract the value of the output variables, it should be noted
397 * that we always use a big parameter M in the main tableau and so
398 * the variable stored in this tableau is not an output variable x itself, but
399 * x' = M + x (in case of minimization)
401 * x' = M - x (in case of maximization)
402 * If x' appears in a column, then its optimal value is zero,
403 * which means that the optimal value of x is an unbounded number
404 * (-M for minimization and M for maximization).
405 * We currently assume that the output dimensions in the original map
406 * are bounded, so this cannot occur.
407 * Similarly, when x' appears in a row, then the coefficient of M in that
408 * row is necessarily 1.
409 * If the row in the tableau represents
410 * d x' = c + d M + e(y)
411 * then, in case of minimization, the corresponding row in the matrix
414 * with a d = m, the (updated) common denominator of the matrix.
415 * In case of maximization, the row will be
418 static void sol_add(struct isl_sol
*sol
, struct isl_tab
*tab
)
420 struct isl_basic_set
*bset
= NULL
;
421 struct isl_mat
*mat
= NULL
;
426 if (sol
->error
|| !tab
)
429 if (tab
->empty
&& !sol
->add_empty
)
432 bset
= sol_domain(sol
);
435 sol_push_sol(sol
, bset
, NULL
);
441 mat
= isl_mat_alloc(tab
->mat
->ctx
, 1 + sol
->n_out
,
442 1 + tab
->n_param
+ tab
->n_div
);
448 isl_seq_clr(mat
->row
[0] + 1, mat
->n_col
- 1);
449 isl_int_set_si(mat
->row
[0][0], 1);
450 for (row
= 0; row
< sol
->n_out
; ++row
) {
451 int i
= tab
->n_param
+ row
;
454 isl_seq_clr(mat
->row
[1 + row
], mat
->n_col
);
455 if (!tab
->var
[i
].is_row
) {
457 isl_die(mat
->ctx
, isl_error_invalid
,
458 "unbounded optimum", goto error2
);
462 r
= tab
->var
[i
].index
;
464 isl_int_ne(tab
->mat
->row
[r
][2], tab
->mat
->row
[r
][0]))
465 isl_die(mat
->ctx
, isl_error_invalid
,
466 "unbounded optimum", goto error2
);
467 isl_int_gcd(m
, mat
->row
[0][0], tab
->mat
->row
[r
][0]);
468 isl_int_divexact(m
, tab
->mat
->row
[r
][0], m
);
469 scale_rows(mat
, m
, 1 + row
);
470 isl_int_divexact(m
, mat
->row
[0][0], tab
->mat
->row
[r
][0]);
471 isl_int_mul(mat
->row
[1 + row
][0], m
, tab
->mat
->row
[r
][1]);
472 for (j
= 0; j
< tab
->n_param
; ++j
) {
474 if (tab
->var
[j
].is_row
)
476 col
= tab
->var
[j
].index
;
477 isl_int_mul(mat
->row
[1 + row
][1 + j
], m
,
478 tab
->mat
->row
[r
][off
+ col
]);
480 for (j
= 0; j
< tab
->n_div
; ++j
) {
482 if (tab
->var
[tab
->n_var
- tab
->n_div
+j
].is_row
)
484 col
= tab
->var
[tab
->n_var
- tab
->n_div
+j
].index
;
485 isl_int_mul(mat
->row
[1 + row
][1 + tab
->n_param
+ j
], m
,
486 tab
->mat
->row
[r
][off
+ col
]);
489 isl_seq_neg(mat
->row
[1 + row
], mat
->row
[1 + row
],
495 sol_push_sol(sol
, bset
, mat
);
500 isl_basic_set_free(bset
);
508 struct isl_set
*empty
;
511 static void sol_map_free(struct isl_sol_map
*sol_map
)
515 if (sol_map
->sol
.context
)
516 sol_map
->sol
.context
->op
->free(sol_map
->sol
.context
);
517 isl_map_free(sol_map
->map
);
518 isl_set_free(sol_map
->empty
);
522 static void sol_map_free_wrap(struct isl_sol
*sol
)
524 sol_map_free((struct isl_sol_map
*)sol
);
527 /* This function is called for parts of the context where there is
528 * no solution, with "bset" corresponding to the context tableau.
529 * Simply add the basic set to the set "empty".
531 static void sol_map_add_empty(struct isl_sol_map
*sol
,
532 struct isl_basic_set
*bset
)
536 isl_assert(bset
->ctx
, sol
->empty
, goto error
);
538 sol
->empty
= isl_set_grow(sol
->empty
, 1);
539 bset
= isl_basic_set_simplify(bset
);
540 bset
= isl_basic_set_finalize(bset
);
541 sol
->empty
= isl_set_add_basic_set(sol
->empty
, isl_basic_set_copy(bset
));
544 isl_basic_set_free(bset
);
547 isl_basic_set_free(bset
);
551 static void sol_map_add_empty_wrap(struct isl_sol
*sol
,
552 struct isl_basic_set
*bset
)
554 sol_map_add_empty((struct isl_sol_map
*)sol
, bset
);
557 /* Add bset to sol's empty, but only if we are actually collecting
560 static void sol_map_add_empty_if_needed(struct isl_sol_map
*sol
,
561 struct isl_basic_set
*bset
)
564 sol_map_add_empty(sol
, bset
);
566 isl_basic_set_free(bset
);
569 /* Given a basic map "dom" that represents the context and an affine
570 * matrix "M" that maps the dimensions of the context to the
571 * output variables, construct a basic map with the same parameters
572 * and divs as the context, the dimensions of the context as input
573 * dimensions and a number of output dimensions that is equal to
574 * the number of output dimensions in the input map.
576 * The constraints and divs of the context are simply copied
577 * from "dom". For each row
581 * is added, with d the common denominator of M.
583 static void sol_map_add(struct isl_sol_map
*sol
,
584 struct isl_basic_set
*dom
, struct isl_mat
*M
)
587 struct isl_basic_map
*bmap
= NULL
;
588 isl_basic_set
*context_bset
;
596 if (sol
->sol
.error
|| !dom
|| !M
)
599 n_out
= sol
->sol
.n_out
;
600 n_eq
= dom
->n_eq
+ n_out
;
601 n_ineq
= dom
->n_ineq
;
603 nparam
= isl_basic_set_total_dim(dom
) - n_div
;
604 total
= isl_map_dim(sol
->map
, isl_dim_all
);
605 bmap
= isl_basic_map_alloc_dim(isl_map_get_dim(sol
->map
),
606 n_div
, n_eq
, 2 * n_div
+ n_ineq
);
609 if (sol
->sol
.rational
)
610 ISL_F_SET(bmap
, ISL_BASIC_MAP_RATIONAL
);
611 for (i
= 0; i
< dom
->n_div
; ++i
) {
612 int k
= isl_basic_map_alloc_div(bmap
);
615 isl_seq_cpy(bmap
->div
[k
], dom
->div
[i
], 1 + 1 + nparam
);
616 isl_seq_clr(bmap
->div
[k
] + 1 + 1 + nparam
, total
- nparam
);
617 isl_seq_cpy(bmap
->div
[k
] + 1 + 1 + total
,
618 dom
->div
[i
] + 1 + 1 + nparam
, i
);
620 for (i
= 0; i
< dom
->n_eq
; ++i
) {
621 int k
= isl_basic_map_alloc_equality(bmap
);
624 isl_seq_cpy(bmap
->eq
[k
], dom
->eq
[i
], 1 + nparam
);
625 isl_seq_clr(bmap
->eq
[k
] + 1 + nparam
, total
- nparam
);
626 isl_seq_cpy(bmap
->eq
[k
] + 1 + total
,
627 dom
->eq
[i
] + 1 + nparam
, n_div
);
629 for (i
= 0; i
< dom
->n_ineq
; ++i
) {
630 int k
= isl_basic_map_alloc_inequality(bmap
);
633 isl_seq_cpy(bmap
->ineq
[k
], dom
->ineq
[i
], 1 + nparam
);
634 isl_seq_clr(bmap
->ineq
[k
] + 1 + nparam
, total
- nparam
);
635 isl_seq_cpy(bmap
->ineq
[k
] + 1 + total
,
636 dom
->ineq
[i
] + 1 + nparam
, n_div
);
638 for (i
= 0; i
< M
->n_row
- 1; ++i
) {
639 int k
= isl_basic_map_alloc_equality(bmap
);
642 isl_seq_cpy(bmap
->eq
[k
], M
->row
[1 + i
], 1 + nparam
);
643 isl_seq_clr(bmap
->eq
[k
] + 1 + nparam
, n_out
);
644 isl_int_neg(bmap
->eq
[k
][1 + nparam
+ i
], M
->row
[0][0]);
645 isl_seq_cpy(bmap
->eq
[k
] + 1 + nparam
+ n_out
,
646 M
->row
[1 + i
] + 1 + nparam
, n_div
);
648 bmap
= isl_basic_map_simplify(bmap
);
649 bmap
= isl_basic_map_finalize(bmap
);
650 sol
->map
= isl_map_grow(sol
->map
, 1);
651 sol
->map
= isl_map_add_basic_map(sol
->map
, bmap
);
654 isl_basic_set_free(dom
);
658 isl_basic_set_free(dom
);
660 isl_basic_map_free(bmap
);
664 static void sol_map_add_wrap(struct isl_sol
*sol
,
665 struct isl_basic_set
*dom
, struct isl_mat
*M
)
667 sol_map_add((struct isl_sol_map
*)sol
, dom
, M
);
671 /* Store the "parametric constant" of row "row" of tableau "tab" in "line",
672 * i.e., the constant term and the coefficients of all variables that
673 * appear in the context tableau.
674 * Note that the coefficient of the big parameter M is NOT copied.
675 * The context tableau may not have a big parameter and even when it
676 * does, it is a different big parameter.
678 static void get_row_parameter_line(struct isl_tab
*tab
, int row
, isl_int
*line
)
681 unsigned off
= 2 + tab
->M
;
683 isl_int_set(line
[0], tab
->mat
->row
[row
][1]);
684 for (i
= 0; i
< tab
->n_param
; ++i
) {
685 if (tab
->var
[i
].is_row
)
686 isl_int_set_si(line
[1 + i
], 0);
688 int col
= tab
->var
[i
].index
;
689 isl_int_set(line
[1 + i
], tab
->mat
->row
[row
][off
+ col
]);
692 for (i
= 0; i
< tab
->n_div
; ++i
) {
693 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
694 isl_int_set_si(line
[1 + tab
->n_param
+ i
], 0);
696 int col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
697 isl_int_set(line
[1 + tab
->n_param
+ i
],
698 tab
->mat
->row
[row
][off
+ col
]);
703 /* Check if rows "row1" and "row2" have identical "parametric constants",
704 * as explained above.
705 * In this case, we also insist that the coefficients of the big parameter
706 * be the same as the values of the constants will only be the same
707 * if these coefficients are also the same.
709 static int identical_parameter_line(struct isl_tab
*tab
, int row1
, int row2
)
712 unsigned off
= 2 + tab
->M
;
714 if (isl_int_ne(tab
->mat
->row
[row1
][1], tab
->mat
->row
[row2
][1]))
717 if (tab
->M
&& isl_int_ne(tab
->mat
->row
[row1
][2],
718 tab
->mat
->row
[row2
][2]))
721 for (i
= 0; i
< tab
->n_param
+ tab
->n_div
; ++i
) {
722 int pos
= i
< tab
->n_param
? i
:
723 tab
->n_var
- tab
->n_div
+ i
- tab
->n_param
;
726 if (tab
->var
[pos
].is_row
)
728 col
= tab
->var
[pos
].index
;
729 if (isl_int_ne(tab
->mat
->row
[row1
][off
+ col
],
730 tab
->mat
->row
[row2
][off
+ col
]))
736 /* Return an inequality that expresses that the "parametric constant"
737 * should be non-negative.
738 * This function is only called when the coefficient of the big parameter
741 static struct isl_vec
*get_row_parameter_ineq(struct isl_tab
*tab
, int row
)
743 struct isl_vec
*ineq
;
745 ineq
= isl_vec_alloc(tab
->mat
->ctx
, 1 + tab
->n_param
+ tab
->n_div
);
749 get_row_parameter_line(tab
, row
, ineq
->el
);
751 ineq
= isl_vec_normalize(ineq
);
756 /* Return a integer division for use in a parametric cut based on the given row.
757 * In particular, let the parametric constant of the row be
761 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
762 * The div returned is equal to
764 * floor(\sum_i {-a_i} y_i) = floor((\sum_i (-a_i mod d) y_i)/d)
766 static struct isl_vec
*get_row_parameter_div(struct isl_tab
*tab
, int row
)
770 div
= isl_vec_alloc(tab
->mat
->ctx
, 1 + 1 + tab
->n_param
+ tab
->n_div
);
774 isl_int_set(div
->el
[0], tab
->mat
->row
[row
][0]);
775 get_row_parameter_line(tab
, row
, div
->el
+ 1);
776 div
= isl_vec_normalize(div
);
777 isl_seq_neg(div
->el
+ 1, div
->el
+ 1, div
->size
- 1);
778 isl_seq_fdiv_r(div
->el
+ 1, div
->el
+ 1, div
->el
[0], div
->size
- 1);
783 /* Return a integer division for use in transferring an integrality constraint
785 * In particular, let the parametric constant of the row be
789 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
790 * The the returned div is equal to
792 * floor(\sum_i {a_i} y_i) = floor((\sum_i (a_i mod d) y_i)/d)
794 static struct isl_vec
*get_row_split_div(struct isl_tab
*tab
, int row
)
798 div
= isl_vec_alloc(tab
->mat
->ctx
, 1 + 1 + tab
->n_param
+ tab
->n_div
);
802 isl_int_set(div
->el
[0], tab
->mat
->row
[row
][0]);
803 get_row_parameter_line(tab
, row
, div
->el
+ 1);
804 div
= isl_vec_normalize(div
);
805 isl_seq_fdiv_r(div
->el
+ 1, div
->el
+ 1, div
->el
[0], div
->size
- 1);
810 /* Construct and return an inequality that expresses an upper bound
812 * In particular, if the div is given by
816 * then the inequality expresses
820 static struct isl_vec
*ineq_for_div(struct isl_basic_set
*bset
, unsigned div
)
824 struct isl_vec
*ineq
;
829 total
= isl_basic_set_total_dim(bset
);
830 div_pos
= 1 + total
- bset
->n_div
+ div
;
832 ineq
= isl_vec_alloc(bset
->ctx
, 1 + total
);
836 isl_seq_cpy(ineq
->el
, bset
->div
[div
] + 1, 1 + total
);
837 isl_int_neg(ineq
->el
[div_pos
], bset
->div
[div
][0]);
841 /* Given a row in the tableau and a div that was created
842 * using get_row_split_div and that been constrained to equality, i.e.,
844 * d = floor(\sum_i {a_i} y_i) = \sum_i {a_i} y_i
846 * replace the expression "\sum_i {a_i} y_i" in the row by d,
847 * i.e., we subtract "\sum_i {a_i} y_i" and add 1 d.
848 * The coefficients of the non-parameters in the tableau have been
849 * verified to be integral. We can therefore simply replace coefficient b
850 * by floor(b). For the coefficients of the parameters we have
851 * floor(a_i) = a_i - {a_i}, while for the other coefficients, we have
854 static struct isl_tab
*set_row_cst_to_div(struct isl_tab
*tab
, int row
, int div
)
856 isl_seq_fdiv_q(tab
->mat
->row
[row
] + 1, tab
->mat
->row
[row
] + 1,
857 tab
->mat
->row
[row
][0], 1 + tab
->M
+ tab
->n_col
);
859 isl_int_set_si(tab
->mat
->row
[row
][0], 1);
861 if (tab
->var
[tab
->n_var
- tab
->n_div
+ div
].is_row
) {
862 int drow
= tab
->var
[tab
->n_var
- tab
->n_div
+ div
].index
;
864 isl_assert(tab
->mat
->ctx
,
865 isl_int_is_one(tab
->mat
->row
[drow
][0]), goto error
);
866 isl_seq_combine(tab
->mat
->row
[row
] + 1,
867 tab
->mat
->ctx
->one
, tab
->mat
->row
[row
] + 1,
868 tab
->mat
->ctx
->one
, tab
->mat
->row
[drow
] + 1,
869 1 + tab
->M
+ tab
->n_col
);
871 int dcol
= tab
->var
[tab
->n_var
- tab
->n_div
+ div
].index
;
873 isl_int_set_si(tab
->mat
->row
[row
][2 + tab
->M
+ dcol
], 1);
882 /* Check if the (parametric) constant of the given row is obviously
883 * negative, meaning that we don't need to consult the context tableau.
884 * If there is a big parameter and its coefficient is non-zero,
885 * then this coefficient determines the outcome.
886 * Otherwise, we check whether the constant is negative and
887 * all non-zero coefficients of parameters are negative and
888 * belong to non-negative parameters.
890 static int is_obviously_neg(struct isl_tab
*tab
, int row
)
894 unsigned off
= 2 + tab
->M
;
897 if (isl_int_is_pos(tab
->mat
->row
[row
][2]))
899 if (isl_int_is_neg(tab
->mat
->row
[row
][2]))
903 if (isl_int_is_nonneg(tab
->mat
->row
[row
][1]))
905 for (i
= 0; i
< tab
->n_param
; ++i
) {
906 /* Eliminated parameter */
907 if (tab
->var
[i
].is_row
)
909 col
= tab
->var
[i
].index
;
910 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
912 if (!tab
->var
[i
].is_nonneg
)
914 if (isl_int_is_pos(tab
->mat
->row
[row
][off
+ col
]))
917 for (i
= 0; i
< tab
->n_div
; ++i
) {
918 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
920 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
921 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
923 if (!tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_nonneg
)
925 if (isl_int_is_pos(tab
->mat
->row
[row
][off
+ col
]))
931 /* Check if the (parametric) constant of the given row is obviously
932 * non-negative, meaning that we don't need to consult the context tableau.
933 * If there is a big parameter and its coefficient is non-zero,
934 * then this coefficient determines the outcome.
935 * Otherwise, we check whether the constant is non-negative and
936 * all non-zero coefficients of parameters are positive and
937 * belong to non-negative parameters.
939 static int is_obviously_nonneg(struct isl_tab
*tab
, int row
)
943 unsigned off
= 2 + tab
->M
;
946 if (isl_int_is_pos(tab
->mat
->row
[row
][2]))
948 if (isl_int_is_neg(tab
->mat
->row
[row
][2]))
952 if (isl_int_is_neg(tab
->mat
->row
[row
][1]))
954 for (i
= 0; i
< tab
->n_param
; ++i
) {
955 /* Eliminated parameter */
956 if (tab
->var
[i
].is_row
)
958 col
= tab
->var
[i
].index
;
959 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
961 if (!tab
->var
[i
].is_nonneg
)
963 if (isl_int_is_neg(tab
->mat
->row
[row
][off
+ col
]))
966 for (i
= 0; i
< tab
->n_div
; ++i
) {
967 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
969 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
970 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
972 if (!tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_nonneg
)
974 if (isl_int_is_neg(tab
->mat
->row
[row
][off
+ col
]))
980 /* Given a row r and two columns, return the column that would
981 * lead to the lexicographically smallest increment in the sample
982 * solution when leaving the basis in favor of the row.
983 * Pivoting with column c will increment the sample value by a non-negative
984 * constant times a_{V,c}/a_{r,c}, with a_{V,c} the elements of column c
985 * corresponding to the non-parametric variables.
986 * If variable v appears in a column c_v, the a_{v,c} = 1 iff c = c_v,
987 * with all other entries in this virtual row equal to zero.
988 * If variable v appears in a row, then a_{v,c} is the element in column c
991 * Let v be the first variable with a_{v,c1}/a_{r,c1} != a_{v,c2}/a_{r,c2}.
992 * Then if a_{v,c1}/a_{r,c1} < a_{v,c2}/a_{r,c2}, i.e.,
993 * a_{v,c2} a_{r,c1} - a_{v,c1} a_{r,c2} > 0, c1 results in the minimal
994 * increment. Otherwise, it's c2.
996 static int lexmin_col_pair(struct isl_tab
*tab
,
997 int row
, int col1
, int col2
, isl_int tmp
)
1002 tr
= tab
->mat
->row
[row
] + 2 + tab
->M
;
1004 for (i
= tab
->n_param
; i
< tab
->n_var
- tab
->n_div
; ++i
) {
1008 if (!tab
->var
[i
].is_row
) {
1009 if (tab
->var
[i
].index
== col1
)
1011 if (tab
->var
[i
].index
== col2
)
1016 if (tab
->var
[i
].index
== row
)
1019 r
= tab
->mat
->row
[tab
->var
[i
].index
] + 2 + tab
->M
;
1020 s1
= isl_int_sgn(r
[col1
]);
1021 s2
= isl_int_sgn(r
[col2
]);
1022 if (s1
== 0 && s2
== 0)
1029 isl_int_mul(tmp
, r
[col2
], tr
[col1
]);
1030 isl_int_submul(tmp
, r
[col1
], tr
[col2
]);
1031 if (isl_int_is_pos(tmp
))
1033 if (isl_int_is_neg(tmp
))
1039 /* Given a row in the tableau, find and return the column that would
1040 * result in the lexicographically smallest, but positive, increment
1041 * in the sample point.
1042 * If there is no such column, then return tab->n_col.
1043 * If anything goes wrong, return -1.
1045 static int lexmin_pivot_col(struct isl_tab
*tab
, int row
)
1048 int col
= tab
->n_col
;
1052 tr
= tab
->mat
->row
[row
] + 2 + tab
->M
;
1056 for (j
= tab
->n_dead
; j
< tab
->n_col
; ++j
) {
1057 if (tab
->col_var
[j
] >= 0 &&
1058 (tab
->col_var
[j
] < tab
->n_param
||
1059 tab
->col_var
[j
] >= tab
->n_var
- tab
->n_div
))
1062 if (!isl_int_is_pos(tr
[j
]))
1065 if (col
== tab
->n_col
)
1068 col
= lexmin_col_pair(tab
, row
, col
, j
, tmp
);
1069 isl_assert(tab
->mat
->ctx
, col
>= 0, goto error
);
1079 /* Return the first known violated constraint, i.e., a non-negative
1080 * constraint that currently has an either obviously negative value
1081 * or a previously determined to be negative value.
1083 * If any constraint has a negative coefficient for the big parameter,
1084 * if any, then we return one of these first.
1086 static int first_neg(struct isl_tab
*tab
)
1091 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
1092 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
1094 if (!isl_int_is_neg(tab
->mat
->row
[row
][2]))
1097 tab
->row_sign
[row
] = isl_tab_row_neg
;
1100 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
1101 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
1103 if (tab
->row_sign
) {
1104 if (tab
->row_sign
[row
] == 0 &&
1105 is_obviously_neg(tab
, row
))
1106 tab
->row_sign
[row
] = isl_tab_row_neg
;
1107 if (tab
->row_sign
[row
] != isl_tab_row_neg
)
1109 } else if (!is_obviously_neg(tab
, row
))
1116 /* Check whether the invariant that all columns are lexico-positive
1117 * is satisfied. This function is not called from the current code
1118 * but is useful during debugging.
1120 static void check_lexpos(struct isl_tab
*tab
)
1122 unsigned off
= 2 + tab
->M
;
1127 for (col
= tab
->n_dead
; col
< tab
->n_col
; ++col
) {
1128 if (tab
->col_var
[col
] >= 0 &&
1129 (tab
->col_var
[col
] < tab
->n_param
||
1130 tab
->col_var
[col
] >= tab
->n_var
- tab
->n_div
))
1132 for (var
= tab
->n_param
; var
< tab
->n_var
- tab
->n_div
; ++var
) {
1133 if (!tab
->var
[var
].is_row
) {
1134 if (tab
->var
[var
].index
== col
)
1139 row
= tab
->var
[var
].index
;
1140 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
1142 if (isl_int_is_pos(tab
->mat
->row
[row
][off
+ col
]))
1144 fprintf(stderr
, "lexneg column %d (row %d)\n",
1147 if (var
>= tab
->n_var
- tab
->n_div
)
1148 fprintf(stderr
, "zero column %d\n", col
);
1152 /* Resolve all known or obviously violated constraints through pivoting.
1153 * In particular, as long as we can find any violated constraint, we
1154 * look for a pivoting column that would result in the lexicographically
1155 * smallest increment in the sample point. If there is no such column
1156 * then the tableau is infeasible.
1158 static struct isl_tab
*restore_lexmin(struct isl_tab
*tab
) WARN_UNUSED
;
1159 static struct isl_tab
*restore_lexmin(struct isl_tab
*tab
)
1167 while ((row
= first_neg(tab
)) != -1) {
1168 col
= lexmin_pivot_col(tab
, row
);
1169 if (col
>= tab
->n_col
) {
1170 if (isl_tab_mark_empty(tab
) < 0)
1176 if (isl_tab_pivot(tab
, row
, col
) < 0)
1185 /* Given a row that represents an equality, look for an appropriate
1187 * In particular, if there are any non-zero coefficients among
1188 * the non-parameter variables, then we take the last of these
1189 * variables. Eliminating this variable in terms of the other
1190 * variables and/or parameters does not influence the property
1191 * that all column in the initial tableau are lexicographically
1192 * positive. The row corresponding to the eliminated variable
1193 * will only have non-zero entries below the diagonal of the
1194 * initial tableau. That is, we transform
1200 * If there is no such non-parameter variable, then we are dealing with
1201 * pure parameter equality and we pick any parameter with coefficient 1 or -1
1202 * for elimination. This will ensure that the eliminated parameter
1203 * always has an integer value whenever all the other parameters are integral.
1204 * If there is no such parameter then we return -1.
1206 static int last_var_col_or_int_par_col(struct isl_tab
*tab
, int row
)
1208 unsigned off
= 2 + tab
->M
;
1211 for (i
= tab
->n_var
- tab
->n_div
- 1; i
>= 0 && i
>= tab
->n_param
; --i
) {
1213 if (tab
->var
[i
].is_row
)
1215 col
= tab
->var
[i
].index
;
1216 if (col
<= tab
->n_dead
)
1218 if (!isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
1221 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
1222 if (isl_int_is_one(tab
->mat
->row
[row
][off
+ i
]))
1224 if (isl_int_is_negone(tab
->mat
->row
[row
][off
+ i
]))
1230 /* Add an equality that is known to be valid to the tableau.
1231 * We first check if we can eliminate a variable or a parameter.
1232 * If not, we add the equality as two inequalities.
1233 * In this case, the equality was a pure parameter equality and there
1234 * is no need to resolve any constraint violations.
1236 static struct isl_tab
*add_lexmin_valid_eq(struct isl_tab
*tab
, isl_int
*eq
)
1243 r
= isl_tab_add_row(tab
, eq
);
1247 r
= tab
->con
[r
].index
;
1248 i
= last_var_col_or_int_par_col(tab
, r
);
1250 tab
->con
[r
].is_nonneg
= 1;
1251 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
1253 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
1254 r
= isl_tab_add_row(tab
, eq
);
1257 tab
->con
[r
].is_nonneg
= 1;
1258 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
1261 if (isl_tab_pivot(tab
, r
, i
) < 0)
1263 if (isl_tab_kill_col(tab
, i
) < 0)
1274 /* Check if the given row is a pure constant.
1276 static int is_constant(struct isl_tab
*tab
, int row
)
1278 unsigned off
= 2 + tab
->M
;
1280 return isl_seq_first_non_zero(tab
->mat
->row
[row
] + off
+ tab
->n_dead
,
1281 tab
->n_col
- tab
->n_dead
) == -1;
1284 /* Add an equality that may or may not be valid to the tableau.
1285 * If the resulting row is a pure constant, then it must be zero.
1286 * Otherwise, the resulting tableau is empty.
1288 * If the row is not a pure constant, then we add two inequalities,
1289 * each time checking that they can be satisfied.
1290 * In the end we try to use one of the two constraints to eliminate
1293 static struct isl_tab
*add_lexmin_eq(struct isl_tab
*tab
, isl_int
*eq
) WARN_UNUSED
;
1294 static struct isl_tab
*add_lexmin_eq(struct isl_tab
*tab
, isl_int
*eq
)
1298 struct isl_tab_undo
*snap
;
1302 snap
= isl_tab_snap(tab
);
1303 r1
= isl_tab_add_row(tab
, eq
);
1306 tab
->con
[r1
].is_nonneg
= 1;
1307 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r1
]) < 0)
1310 row
= tab
->con
[r1
].index
;
1311 if (is_constant(tab
, row
)) {
1312 if (!isl_int_is_zero(tab
->mat
->row
[row
][1]) ||
1313 (tab
->M
&& !isl_int_is_zero(tab
->mat
->row
[row
][2]))) {
1314 if (isl_tab_mark_empty(tab
) < 0)
1318 if (isl_tab_rollback(tab
, snap
) < 0)
1323 tab
= restore_lexmin(tab
);
1324 if (!tab
|| tab
->empty
)
1327 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
1329 r2
= isl_tab_add_row(tab
, eq
);
1332 tab
->con
[r2
].is_nonneg
= 1;
1333 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r2
]) < 0)
1336 tab
= restore_lexmin(tab
);
1337 if (!tab
|| tab
->empty
)
1340 if (!tab
->con
[r1
].is_row
) {
1341 if (isl_tab_kill_col(tab
, tab
->con
[r1
].index
) < 0)
1343 } else if (!tab
->con
[r2
].is_row
) {
1344 if (isl_tab_kill_col(tab
, tab
->con
[r2
].index
) < 0)
1349 tab
->bmap
= isl_basic_map_add_ineq(tab
->bmap
, eq
);
1350 if (isl_tab_push(tab
, isl_tab_undo_bmap_ineq
) < 0)
1352 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
1353 tab
->bmap
= isl_basic_map_add_ineq(tab
->bmap
, eq
);
1354 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
1355 if (isl_tab_push(tab
, isl_tab_undo_bmap_ineq
) < 0)
1367 /* Add an inequality to the tableau, resolving violations using
1370 static struct isl_tab
*add_lexmin_ineq(struct isl_tab
*tab
, isl_int
*ineq
)
1377 tab
->bmap
= isl_basic_map_add_ineq(tab
->bmap
, ineq
);
1378 if (isl_tab_push(tab
, isl_tab_undo_bmap_ineq
) < 0)
1383 r
= isl_tab_add_row(tab
, ineq
);
1386 tab
->con
[r
].is_nonneg
= 1;
1387 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
1389 if (isl_tab_row_is_redundant(tab
, tab
->con
[r
].index
)) {
1390 if (isl_tab_mark_redundant(tab
, tab
->con
[r
].index
) < 0)
1395 tab
= restore_lexmin(tab
);
1396 if (tab
&& !tab
->empty
&& tab
->con
[r
].is_row
&&
1397 isl_tab_row_is_redundant(tab
, tab
->con
[r
].index
))
1398 if (isl_tab_mark_redundant(tab
, tab
->con
[r
].index
) < 0)
1406 /* Check if the coefficients of the parameters are all integral.
1408 static int integer_parameter(struct isl_tab
*tab
, int row
)
1412 unsigned off
= 2 + tab
->M
;
1414 for (i
= 0; i
< tab
->n_param
; ++i
) {
1415 /* Eliminated parameter */
1416 if (tab
->var
[i
].is_row
)
1418 col
= tab
->var
[i
].index
;
1419 if (!isl_int_is_divisible_by(tab
->mat
->row
[row
][off
+ col
],
1420 tab
->mat
->row
[row
][0]))
1423 for (i
= 0; i
< tab
->n_div
; ++i
) {
1424 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
1426 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
1427 if (!isl_int_is_divisible_by(tab
->mat
->row
[row
][off
+ col
],
1428 tab
->mat
->row
[row
][0]))
1434 /* Check if the coefficients of the non-parameter variables are all integral.
1436 static int integer_variable(struct isl_tab
*tab
, int row
)
1439 unsigned off
= 2 + tab
->M
;
1441 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
1442 if (tab
->col_var
[i
] >= 0 &&
1443 (tab
->col_var
[i
] < tab
->n_param
||
1444 tab
->col_var
[i
] >= tab
->n_var
- tab
->n_div
))
1446 if (!isl_int_is_divisible_by(tab
->mat
->row
[row
][off
+ i
],
1447 tab
->mat
->row
[row
][0]))
1453 /* Check if the constant term is integral.
1455 static int integer_constant(struct isl_tab
*tab
, int row
)
1457 return isl_int_is_divisible_by(tab
->mat
->row
[row
][1],
1458 tab
->mat
->row
[row
][0]);
1461 #define I_CST 1 << 0
1462 #define I_PAR 1 << 1
1463 #define I_VAR 1 << 2
1465 /* Check for next (non-parameter) variable after "var" (first if var == -1)
1466 * that is non-integer and therefore requires a cut and return
1467 * the index of the variable.
1468 * For parametric tableaus, there are three parts in a row,
1469 * the constant, the coefficients of the parameters and the rest.
1470 * For each part, we check whether the coefficients in that part
1471 * are all integral and if so, set the corresponding flag in *f.
1472 * If the constant and the parameter part are integral, then the
1473 * current sample value is integral and no cut is required
1474 * (irrespective of whether the variable part is integral).
1476 static int next_non_integer_var(struct isl_tab
*tab
, int var
, int *f
)
1478 var
= var
< 0 ? tab
->n_param
: var
+ 1;
1480 for (; var
< tab
->n_var
- tab
->n_div
; ++var
) {
1483 if (!tab
->var
[var
].is_row
)
1485 row
= tab
->var
[var
].index
;
1486 if (integer_constant(tab
, row
))
1487 ISL_FL_SET(flags
, I_CST
);
1488 if (integer_parameter(tab
, row
))
1489 ISL_FL_SET(flags
, I_PAR
);
1490 if (ISL_FL_ISSET(flags
, I_CST
) && ISL_FL_ISSET(flags
, I_PAR
))
1492 if (integer_variable(tab
, row
))
1493 ISL_FL_SET(flags
, I_VAR
);
1500 /* Check for first (non-parameter) variable that is non-integer and
1501 * therefore requires a cut and return the corresponding row.
1502 * For parametric tableaus, there are three parts in a row,
1503 * the constant, the coefficients of the parameters and the rest.
1504 * For each part, we check whether the coefficients in that part
1505 * are all integral and if so, set the corresponding flag in *f.
1506 * If the constant and the parameter part are integral, then the
1507 * current sample value is integral and no cut is required
1508 * (irrespective of whether the variable part is integral).
1510 static int first_non_integer_row(struct isl_tab
*tab
, int *f
)
1512 int var
= next_non_integer_var(tab
, -1, f
);
1514 return var
< 0 ? -1 : tab
->var
[var
].index
;
1517 /* Add a (non-parametric) cut to cut away the non-integral sample
1518 * value of the given row.
1520 * If the row is given by
1522 * m r = f + \sum_i a_i y_i
1526 * c = - {-f/m} + \sum_i {a_i/m} y_i >= 0
1528 * The big parameter, if any, is ignored, since it is assumed to be big
1529 * enough to be divisible by any integer.
1530 * If the tableau is actually a parametric tableau, then this function
1531 * is only called when all coefficients of the parameters are integral.
1532 * The cut therefore has zero coefficients for the parameters.
1534 * The current value is known to be negative, so row_sign, if it
1535 * exists, is set accordingly.
1537 * Return the row of the cut or -1.
1539 static int add_cut(struct isl_tab
*tab
, int row
)
1544 unsigned off
= 2 + tab
->M
;
1546 if (isl_tab_extend_cons(tab
, 1) < 0)
1548 r
= isl_tab_allocate_con(tab
);
1552 r_row
= tab
->mat
->row
[tab
->con
[r
].index
];
1553 isl_int_set(r_row
[0], tab
->mat
->row
[row
][0]);
1554 isl_int_neg(r_row
[1], tab
->mat
->row
[row
][1]);
1555 isl_int_fdiv_r(r_row
[1], r_row
[1], tab
->mat
->row
[row
][0]);
1556 isl_int_neg(r_row
[1], r_row
[1]);
1558 isl_int_set_si(r_row
[2], 0);
1559 for (i
= 0; i
< tab
->n_col
; ++i
)
1560 isl_int_fdiv_r(r_row
[off
+ i
],
1561 tab
->mat
->row
[row
][off
+ i
], tab
->mat
->row
[row
][0]);
1563 tab
->con
[r
].is_nonneg
= 1;
1564 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
1567 tab
->row_sign
[tab
->con
[r
].index
] = isl_tab_row_neg
;
1569 return tab
->con
[r
].index
;
1572 /* Given a non-parametric tableau, add cuts until an integer
1573 * sample point is obtained or until the tableau is determined
1574 * to be integer infeasible.
1575 * As long as there is any non-integer value in the sample point,
1576 * we add appropriate cuts, if possible, for each of these
1577 * non-integer values and then resolve the violated
1578 * cut constraints using restore_lexmin.
1579 * If one of the corresponding rows is equal to an integral
1580 * combination of variables/constraints plus a non-integral constant,
1581 * then there is no way to obtain an integer point and we return
1582 * a tableau that is marked empty.
1584 static struct isl_tab
*cut_to_integer_lexmin(struct isl_tab
*tab
)
1595 while ((var
= next_non_integer_var(tab
, -1, &flags
)) != -1) {
1597 if (ISL_FL_ISSET(flags
, I_VAR
)) {
1598 if (isl_tab_mark_empty(tab
) < 0)
1602 row
= tab
->var
[var
].index
;
1603 row
= add_cut(tab
, row
);
1606 } while ((var
= next_non_integer_var(tab
, var
, &flags
)) != -1);
1607 tab
= restore_lexmin(tab
);
1608 if (!tab
|| tab
->empty
)
1617 /* Check whether all the currently active samples also satisfy the inequality
1618 * "ineq" (treated as an equality if eq is set).
1619 * Remove those samples that do not.
1621 static struct isl_tab
*check_samples(struct isl_tab
*tab
, isl_int
*ineq
, int eq
)
1629 isl_assert(tab
->mat
->ctx
, tab
->bmap
, goto error
);
1630 isl_assert(tab
->mat
->ctx
, tab
->samples
, goto error
);
1631 isl_assert(tab
->mat
->ctx
, tab
->samples
->n_col
== 1 + tab
->n_var
, goto error
);
1634 for (i
= tab
->n_outside
; i
< tab
->n_sample
; ++i
) {
1636 isl_seq_inner_product(ineq
, tab
->samples
->row
[i
],
1637 1 + tab
->n_var
, &v
);
1638 sgn
= isl_int_sgn(v
);
1639 if (eq
? (sgn
== 0) : (sgn
>= 0))
1641 tab
= isl_tab_drop_sample(tab
, i
);
1653 /* Check whether the sample value of the tableau is finite,
1654 * i.e., either the tableau does not use a big parameter, or
1655 * all values of the variables are equal to the big parameter plus
1656 * some constant. This constant is the actual sample value.
1658 static int sample_is_finite(struct isl_tab
*tab
)
1665 for (i
= 0; i
< tab
->n_var
; ++i
) {
1667 if (!tab
->var
[i
].is_row
)
1669 row
= tab
->var
[i
].index
;
1670 if (isl_int_ne(tab
->mat
->row
[row
][0], tab
->mat
->row
[row
][2]))
1676 /* Check if the context tableau of sol has any integer points.
1677 * Leave tab in empty state if no integer point can be found.
1678 * If an integer point can be found and if moreover it is finite,
1679 * then it is added to the list of sample values.
1681 * This function is only called when none of the currently active sample
1682 * values satisfies the most recently added constraint.
1684 static struct isl_tab
*check_integer_feasible(struct isl_tab
*tab
)
1686 struct isl_tab_undo
*snap
;
1692 snap
= isl_tab_snap(tab
);
1693 if (isl_tab_push_basis(tab
) < 0)
1696 tab
= cut_to_integer_lexmin(tab
);
1700 if (!tab
->empty
&& sample_is_finite(tab
)) {
1701 struct isl_vec
*sample
;
1703 sample
= isl_tab_get_sample_value(tab
);
1705 tab
= isl_tab_add_sample(tab
, sample
);
1708 if (!tab
->empty
&& isl_tab_rollback(tab
, snap
) < 0)
1717 /* Check if any of the currently active sample values satisfies
1718 * the inequality "ineq" (an equality if eq is set).
1720 static int tab_has_valid_sample(struct isl_tab
*tab
, isl_int
*ineq
, int eq
)
1728 isl_assert(tab
->mat
->ctx
, tab
->bmap
, return -1);
1729 isl_assert(tab
->mat
->ctx
, tab
->samples
, return -1);
1730 isl_assert(tab
->mat
->ctx
, tab
->samples
->n_col
== 1 + tab
->n_var
, return -1);
1733 for (i
= tab
->n_outside
; i
< tab
->n_sample
; ++i
) {
1735 isl_seq_inner_product(ineq
, tab
->samples
->row
[i
],
1736 1 + tab
->n_var
, &v
);
1737 sgn
= isl_int_sgn(v
);
1738 if (eq
? (sgn
== 0) : (sgn
>= 0))
1743 return i
< tab
->n_sample
;
1746 /* Add a div specified by "div" to the tableau "tab" and return
1747 * 1 if the div is obviously non-negative.
1749 static int context_tab_add_div(struct isl_tab
*tab
, struct isl_vec
*div
,
1750 int (*add_ineq
)(void *user
, isl_int
*), void *user
)
1754 struct isl_mat
*samples
;
1757 r
= isl_tab_add_div(tab
, div
, add_ineq
, user
);
1760 nonneg
= tab
->var
[r
].is_nonneg
;
1761 tab
->var
[r
].frozen
= 1;
1763 samples
= isl_mat_extend(tab
->samples
,
1764 tab
->n_sample
, 1 + tab
->n_var
);
1765 tab
->samples
= samples
;
1768 for (i
= tab
->n_outside
; i
< samples
->n_row
; ++i
) {
1769 isl_seq_inner_product(div
->el
+ 1, samples
->row
[i
],
1770 div
->size
- 1, &samples
->row
[i
][samples
->n_col
- 1]);
1771 isl_int_fdiv_q(samples
->row
[i
][samples
->n_col
- 1],
1772 samples
->row
[i
][samples
->n_col
- 1], div
->el
[0]);
1778 /* Add a div specified by "div" to both the main tableau and
1779 * the context tableau. In case of the main tableau, we only
1780 * need to add an extra div. In the context tableau, we also
1781 * need to express the meaning of the div.
1782 * Return the index of the div or -1 if anything went wrong.
1784 static int add_div(struct isl_tab
*tab
, struct isl_context
*context
,
1785 struct isl_vec
*div
)
1790 if ((nonneg
= context
->op
->add_div(context
, div
)) < 0)
1793 if (!context
->op
->is_ok(context
))
1796 if (isl_tab_extend_vars(tab
, 1) < 0)
1798 r
= isl_tab_allocate_var(tab
);
1802 tab
->var
[r
].is_nonneg
= 1;
1803 tab
->var
[r
].frozen
= 1;
1806 return tab
->n_div
- 1;
1808 context
->op
->invalidate(context
);
1812 static int find_div(struct isl_tab
*tab
, isl_int
*div
, isl_int denom
)
1815 unsigned total
= isl_basic_map_total_dim(tab
->bmap
);
1817 for (i
= 0; i
< tab
->bmap
->n_div
; ++i
) {
1818 if (isl_int_ne(tab
->bmap
->div
[i
][0], denom
))
1820 if (!isl_seq_eq(tab
->bmap
->div
[i
] + 1, div
, 1 + total
))
1827 /* Return the index of a div that corresponds to "div".
1828 * We first check if we already have such a div and if not, we create one.
1830 static int get_div(struct isl_tab
*tab
, struct isl_context
*context
,
1831 struct isl_vec
*div
)
1834 struct isl_tab
*context_tab
= context
->op
->peek_tab(context
);
1839 d
= find_div(context_tab
, div
->el
+ 1, div
->el
[0]);
1843 return add_div(tab
, context
, div
);
1846 /* Add a parametric cut to cut away the non-integral sample value
1848 * Let a_i be the coefficients of the constant term and the parameters
1849 * and let b_i be the coefficients of the variables or constraints
1850 * in basis of the tableau.
1851 * Let q be the div q = floor(\sum_i {-a_i} y_i).
1853 * The cut is expressed as
1855 * c = \sum_i -{-a_i} y_i + \sum_i {b_i} x_i + q >= 0
1857 * If q did not already exist in the context tableau, then it is added first.
1858 * If q is in a column of the main tableau then the "+ q" can be accomplished
1859 * by setting the corresponding entry to the denominator of the constraint.
1860 * If q happens to be in a row of the main tableau, then the corresponding
1861 * row needs to be added instead (taking care of the denominators).
1862 * Note that this is very unlikely, but perhaps not entirely impossible.
1864 * The current value of the cut is known to be negative (or at least
1865 * non-positive), so row_sign is set accordingly.
1867 * Return the row of the cut or -1.
1869 static int add_parametric_cut(struct isl_tab
*tab
, int row
,
1870 struct isl_context
*context
)
1872 struct isl_vec
*div
;
1879 unsigned off
= 2 + tab
->M
;
1884 div
= get_row_parameter_div(tab
, row
);
1889 d
= context
->op
->get_div(context
, tab
, div
);
1893 if (isl_tab_extend_cons(tab
, 1) < 0)
1895 r
= isl_tab_allocate_con(tab
);
1899 r_row
= tab
->mat
->row
[tab
->con
[r
].index
];
1900 isl_int_set(r_row
[0], tab
->mat
->row
[row
][0]);
1901 isl_int_neg(r_row
[1], tab
->mat
->row
[row
][1]);
1902 isl_int_fdiv_r(r_row
[1], r_row
[1], tab
->mat
->row
[row
][0]);
1903 isl_int_neg(r_row
[1], r_row
[1]);
1905 isl_int_set_si(r_row
[2], 0);
1906 for (i
= 0; i
< tab
->n_param
; ++i
) {
1907 if (tab
->var
[i
].is_row
)
1909 col
= tab
->var
[i
].index
;
1910 isl_int_neg(r_row
[off
+ col
], tab
->mat
->row
[row
][off
+ col
]);
1911 isl_int_fdiv_r(r_row
[off
+ col
], r_row
[off
+ col
],
1912 tab
->mat
->row
[row
][0]);
1913 isl_int_neg(r_row
[off
+ col
], r_row
[off
+ col
]);
1915 for (i
= 0; i
< tab
->n_div
; ++i
) {
1916 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
1918 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
1919 isl_int_neg(r_row
[off
+ col
], tab
->mat
->row
[row
][off
+ col
]);
1920 isl_int_fdiv_r(r_row
[off
+ col
], r_row
[off
+ col
],
1921 tab
->mat
->row
[row
][0]);
1922 isl_int_neg(r_row
[off
+ col
], r_row
[off
+ col
]);
1924 for (i
= 0; i
< tab
->n_col
; ++i
) {
1925 if (tab
->col_var
[i
] >= 0 &&
1926 (tab
->col_var
[i
] < tab
->n_param
||
1927 tab
->col_var
[i
] >= tab
->n_var
- tab
->n_div
))
1929 isl_int_fdiv_r(r_row
[off
+ i
],
1930 tab
->mat
->row
[row
][off
+ i
], tab
->mat
->row
[row
][0]);
1932 if (tab
->var
[tab
->n_var
- tab
->n_div
+ d
].is_row
) {
1934 int d_row
= tab
->var
[tab
->n_var
- tab
->n_div
+ d
].index
;
1936 isl_int_gcd(gcd
, tab
->mat
->row
[d_row
][0], r_row
[0]);
1937 isl_int_divexact(r_row
[0], r_row
[0], gcd
);
1938 isl_int_divexact(gcd
, tab
->mat
->row
[d_row
][0], gcd
);
1939 isl_seq_combine(r_row
+ 1, gcd
, r_row
+ 1,
1940 r_row
[0], tab
->mat
->row
[d_row
] + 1,
1941 off
- 1 + tab
->n_col
);
1942 isl_int_mul(r_row
[0], r_row
[0], tab
->mat
->row
[d_row
][0]);
1945 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ d
].index
;
1946 isl_int_set(r_row
[off
+ col
], tab
->mat
->row
[row
][0]);
1949 tab
->con
[r
].is_nonneg
= 1;
1950 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
1953 tab
->row_sign
[tab
->con
[r
].index
] = isl_tab_row_neg
;
1957 row
= tab
->con
[r
].index
;
1959 if (d
>= n
&& context
->op
->detect_equalities(context
, tab
) < 0)
1965 /* Construct a tableau for bmap that can be used for computing
1966 * the lexicographic minimum (or maximum) of bmap.
1967 * If not NULL, then dom is the domain where the minimum
1968 * should be computed. In this case, we set up a parametric
1969 * tableau with row signs (initialized to "unknown").
1970 * If M is set, then the tableau will use a big parameter.
1971 * If max is set, then a maximum should be computed instead of a minimum.
1972 * This means that for each variable x, the tableau will contain the variable
1973 * x' = M - x, rather than x' = M + x. This in turn means that the coefficient
1974 * of the variables in all constraints are negated prior to adding them
1977 static struct isl_tab
*tab_for_lexmin(struct isl_basic_map
*bmap
,
1978 struct isl_basic_set
*dom
, unsigned M
, int max
)
1981 struct isl_tab
*tab
;
1983 tab
= isl_tab_alloc(bmap
->ctx
, 2 * bmap
->n_eq
+ bmap
->n_ineq
+ 1,
1984 isl_basic_map_total_dim(bmap
), M
);
1988 tab
->rational
= ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
1990 tab
->n_param
= isl_basic_set_total_dim(dom
) - dom
->n_div
;
1991 tab
->n_div
= dom
->n_div
;
1992 tab
->row_sign
= isl_calloc_array(bmap
->ctx
,
1993 enum isl_tab_row_sign
, tab
->mat
->n_row
);
1997 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
)) {
1998 if (isl_tab_mark_empty(tab
) < 0)
2003 for (i
= tab
->n_param
; i
< tab
->n_var
- tab
->n_div
; ++i
) {
2004 tab
->var
[i
].is_nonneg
= 1;
2005 tab
->var
[i
].frozen
= 1;
2007 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
2009 isl_seq_neg(bmap
->eq
[i
] + 1 + tab
->n_param
,
2010 bmap
->eq
[i
] + 1 + tab
->n_param
,
2011 tab
->n_var
- tab
->n_param
- tab
->n_div
);
2012 tab
= add_lexmin_valid_eq(tab
, bmap
->eq
[i
]);
2014 isl_seq_neg(bmap
->eq
[i
] + 1 + tab
->n_param
,
2015 bmap
->eq
[i
] + 1 + tab
->n_param
,
2016 tab
->n_var
- tab
->n_param
- tab
->n_div
);
2017 if (!tab
|| tab
->empty
)
2021 tab
= restore_lexmin(tab
);
2022 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
2024 isl_seq_neg(bmap
->ineq
[i
] + 1 + tab
->n_param
,
2025 bmap
->ineq
[i
] + 1 + tab
->n_param
,
2026 tab
->n_var
- tab
->n_param
- tab
->n_div
);
2027 tab
= add_lexmin_ineq(tab
, bmap
->ineq
[i
]);
2029 isl_seq_neg(bmap
->ineq
[i
] + 1 + tab
->n_param
,
2030 bmap
->ineq
[i
] + 1 + tab
->n_param
,
2031 tab
->n_var
- tab
->n_param
- tab
->n_div
);
2032 if (!tab
|| tab
->empty
)
2041 /* Given a main tableau where more than one row requires a split,
2042 * determine and return the "best" row to split on.
2044 * Given two rows in the main tableau, if the inequality corresponding
2045 * to the first row is redundant with respect to that of the second row
2046 * in the current tableau, then it is better to split on the second row,
2047 * since in the positive part, both row will be positive.
2048 * (In the negative part a pivot will have to be performed and just about
2049 * anything can happen to the sign of the other row.)
2051 * As a simple heuristic, we therefore select the row that makes the most
2052 * of the other rows redundant.
2054 * Perhaps it would also be useful to look at the number of constraints
2055 * that conflict with any given constraint.
2057 static int best_split(struct isl_tab
*tab
, struct isl_tab
*context_tab
)
2059 struct isl_tab_undo
*snap
;
2065 if (isl_tab_extend_cons(context_tab
, 2) < 0)
2068 snap
= isl_tab_snap(context_tab
);
2070 for (split
= tab
->n_redundant
; split
< tab
->n_row
; ++split
) {
2071 struct isl_tab_undo
*snap2
;
2072 struct isl_vec
*ineq
= NULL
;
2076 if (!isl_tab_var_from_row(tab
, split
)->is_nonneg
)
2078 if (tab
->row_sign
[split
] != isl_tab_row_any
)
2081 ineq
= get_row_parameter_ineq(tab
, split
);
2084 ok
= isl_tab_add_ineq(context_tab
, ineq
->el
) >= 0;
2089 snap2
= isl_tab_snap(context_tab
);
2091 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
2092 struct isl_tab_var
*var
;
2096 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
2098 if (tab
->row_sign
[row
] != isl_tab_row_any
)
2101 ineq
= get_row_parameter_ineq(tab
, row
);
2104 ok
= isl_tab_add_ineq(context_tab
, ineq
->el
) >= 0;
2108 var
= &context_tab
->con
[context_tab
->n_con
- 1];
2109 if (!context_tab
->empty
&&
2110 !isl_tab_min_at_most_neg_one(context_tab
, var
))
2112 if (isl_tab_rollback(context_tab
, snap2
) < 0)
2115 if (best
== -1 || r
> best_r
) {
2119 if (isl_tab_rollback(context_tab
, snap
) < 0)
2126 static struct isl_basic_set
*context_lex_peek_basic_set(
2127 struct isl_context
*context
)
2129 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2132 return isl_tab_peek_bset(clex
->tab
);
2135 static struct isl_tab
*context_lex_peek_tab(struct isl_context
*context
)
2137 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2141 static void context_lex_extend(struct isl_context
*context
, int n
)
2143 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2146 if (isl_tab_extend_cons(clex
->tab
, n
) >= 0)
2148 isl_tab_free(clex
->tab
);
2152 static void context_lex_add_eq(struct isl_context
*context
, isl_int
*eq
,
2153 int check
, int update
)
2155 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2156 if (isl_tab_extend_cons(clex
->tab
, 2) < 0)
2158 clex
->tab
= add_lexmin_eq(clex
->tab
, eq
);
2160 int v
= tab_has_valid_sample(clex
->tab
, eq
, 1);
2164 clex
->tab
= check_integer_feasible(clex
->tab
);
2167 clex
->tab
= check_samples(clex
->tab
, eq
, 1);
2170 isl_tab_free(clex
->tab
);
2174 static void context_lex_add_ineq(struct isl_context
*context
, isl_int
*ineq
,
2175 int check
, int update
)
2177 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2178 if (isl_tab_extend_cons(clex
->tab
, 1) < 0)
2180 clex
->tab
= add_lexmin_ineq(clex
->tab
, ineq
);
2182 int v
= tab_has_valid_sample(clex
->tab
, ineq
, 0);
2186 clex
->tab
= check_integer_feasible(clex
->tab
);
2189 clex
->tab
= check_samples(clex
->tab
, ineq
, 0);
2192 isl_tab_free(clex
->tab
);
2196 static int context_lex_add_ineq_wrap(void *user
, isl_int
*ineq
)
2198 struct isl_context
*context
= (struct isl_context
*)user
;
2199 context_lex_add_ineq(context
, ineq
, 0, 0);
2200 return context
->op
->is_ok(context
) ? 0 : -1;
2203 /* Check which signs can be obtained by "ineq" on all the currently
2204 * active sample values. See row_sign for more information.
2206 static enum isl_tab_row_sign
tab_ineq_sign(struct isl_tab
*tab
, isl_int
*ineq
,
2212 enum isl_tab_row_sign res
= isl_tab_row_unknown
;
2214 isl_assert(tab
->mat
->ctx
, tab
->samples
, return isl_tab_row_unknown
);
2215 isl_assert(tab
->mat
->ctx
, tab
->samples
->n_col
== 1 + tab
->n_var
,
2216 return isl_tab_row_unknown
);
2219 for (i
= tab
->n_outside
; i
< tab
->n_sample
; ++i
) {
2220 isl_seq_inner_product(tab
->samples
->row
[i
], ineq
,
2221 1 + tab
->n_var
, &tmp
);
2222 sgn
= isl_int_sgn(tmp
);
2223 if (sgn
> 0 || (sgn
== 0 && strict
)) {
2224 if (res
== isl_tab_row_unknown
)
2225 res
= isl_tab_row_pos
;
2226 if (res
== isl_tab_row_neg
)
2227 res
= isl_tab_row_any
;
2230 if (res
== isl_tab_row_unknown
)
2231 res
= isl_tab_row_neg
;
2232 if (res
== isl_tab_row_pos
)
2233 res
= isl_tab_row_any
;
2235 if (res
== isl_tab_row_any
)
2243 static enum isl_tab_row_sign
context_lex_ineq_sign(struct isl_context
*context
,
2244 isl_int
*ineq
, int strict
)
2246 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2247 return tab_ineq_sign(clex
->tab
, ineq
, strict
);
2250 /* Check whether "ineq" can be added to the tableau without rendering
2253 static int context_lex_test_ineq(struct isl_context
*context
, isl_int
*ineq
)
2255 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2256 struct isl_tab_undo
*snap
;
2262 if (isl_tab_extend_cons(clex
->tab
, 1) < 0)
2265 snap
= isl_tab_snap(clex
->tab
);
2266 if (isl_tab_push_basis(clex
->tab
) < 0)
2268 clex
->tab
= add_lexmin_ineq(clex
->tab
, ineq
);
2269 clex
->tab
= check_integer_feasible(clex
->tab
);
2272 feasible
= !clex
->tab
->empty
;
2273 if (isl_tab_rollback(clex
->tab
, snap
) < 0)
2279 static int context_lex_get_div(struct isl_context
*context
, struct isl_tab
*tab
,
2280 struct isl_vec
*div
)
2282 return get_div(tab
, context
, div
);
2285 /* Add a div specified by "div" to the context tableau and return
2286 * 1 if the div is obviously non-negative.
2287 * context_tab_add_div will always return 1, because all variables
2288 * in a isl_context_lex tableau are non-negative.
2289 * However, if we are using a big parameter in the context, then this only
2290 * reflects the non-negativity of the variable used to _encode_ the
2291 * div, i.e., div' = M + div, so we can't draw any conclusions.
2293 static int context_lex_add_div(struct isl_context
*context
, struct isl_vec
*div
)
2295 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2297 nonneg
= context_tab_add_div(clex
->tab
, div
,
2298 context_lex_add_ineq_wrap
, context
);
2306 static int context_lex_detect_equalities(struct isl_context
*context
,
2307 struct isl_tab
*tab
)
2312 static int context_lex_best_split(struct isl_context
*context
,
2313 struct isl_tab
*tab
)
2315 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2316 struct isl_tab_undo
*snap
;
2319 snap
= isl_tab_snap(clex
->tab
);
2320 if (isl_tab_push_basis(clex
->tab
) < 0)
2322 r
= best_split(tab
, clex
->tab
);
2324 if (r
>= 0 && isl_tab_rollback(clex
->tab
, snap
) < 0)
2330 static int context_lex_is_empty(struct isl_context
*context
)
2332 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2335 return clex
->tab
->empty
;
2338 static void *context_lex_save(struct isl_context
*context
)
2340 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2341 struct isl_tab_undo
*snap
;
2343 snap
= isl_tab_snap(clex
->tab
);
2344 if (isl_tab_push_basis(clex
->tab
) < 0)
2346 if (isl_tab_save_samples(clex
->tab
) < 0)
2352 static void context_lex_restore(struct isl_context
*context
, void *save
)
2354 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2355 if (isl_tab_rollback(clex
->tab
, (struct isl_tab_undo
*)save
) < 0) {
2356 isl_tab_free(clex
->tab
);
2361 static int context_lex_is_ok(struct isl_context
*context
)
2363 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2367 /* For each variable in the context tableau, check if the variable can
2368 * only attain non-negative values. If so, mark the parameter as non-negative
2369 * in the main tableau. This allows for a more direct identification of some
2370 * cases of violated constraints.
2372 static struct isl_tab
*tab_detect_nonnegative_parameters(struct isl_tab
*tab
,
2373 struct isl_tab
*context_tab
)
2376 struct isl_tab_undo
*snap
;
2377 struct isl_vec
*ineq
= NULL
;
2378 struct isl_tab_var
*var
;
2381 if (context_tab
->n_var
== 0)
2384 ineq
= isl_vec_alloc(tab
->mat
->ctx
, 1 + context_tab
->n_var
);
2388 if (isl_tab_extend_cons(context_tab
, 1) < 0)
2391 snap
= isl_tab_snap(context_tab
);
2394 isl_seq_clr(ineq
->el
, ineq
->size
);
2395 for (i
= 0; i
< context_tab
->n_var
; ++i
) {
2396 isl_int_set_si(ineq
->el
[1 + i
], 1);
2397 if (isl_tab_add_ineq(context_tab
, ineq
->el
) < 0)
2399 var
= &context_tab
->con
[context_tab
->n_con
- 1];
2400 if (!context_tab
->empty
&&
2401 !isl_tab_min_at_most_neg_one(context_tab
, var
)) {
2403 if (i
>= tab
->n_param
)
2404 j
= i
- tab
->n_param
+ tab
->n_var
- tab
->n_div
;
2405 tab
->var
[j
].is_nonneg
= 1;
2408 isl_int_set_si(ineq
->el
[1 + i
], 0);
2409 if (isl_tab_rollback(context_tab
, snap
) < 0)
2413 if (context_tab
->M
&& n
== context_tab
->n_var
) {
2414 context_tab
->mat
= isl_mat_drop_cols(context_tab
->mat
, 2, 1);
2426 static struct isl_tab
*context_lex_detect_nonnegative_parameters(
2427 struct isl_context
*context
, struct isl_tab
*tab
)
2429 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2430 struct isl_tab_undo
*snap
;
2435 snap
= isl_tab_snap(clex
->tab
);
2436 if (isl_tab_push_basis(clex
->tab
) < 0)
2439 tab
= tab_detect_nonnegative_parameters(tab
, clex
->tab
);
2441 if (isl_tab_rollback(clex
->tab
, snap
) < 0)
2450 static void context_lex_invalidate(struct isl_context
*context
)
2452 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2453 isl_tab_free(clex
->tab
);
2457 static void context_lex_free(struct isl_context
*context
)
2459 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2460 isl_tab_free(clex
->tab
);
2464 struct isl_context_op isl_context_lex_op
= {
2465 context_lex_detect_nonnegative_parameters
,
2466 context_lex_peek_basic_set
,
2467 context_lex_peek_tab
,
2469 context_lex_add_ineq
,
2470 context_lex_ineq_sign
,
2471 context_lex_test_ineq
,
2472 context_lex_get_div
,
2473 context_lex_add_div
,
2474 context_lex_detect_equalities
,
2475 context_lex_best_split
,
2476 context_lex_is_empty
,
2479 context_lex_restore
,
2480 context_lex_invalidate
,
2484 static struct isl_tab
*context_tab_for_lexmin(struct isl_basic_set
*bset
)
2486 struct isl_tab
*tab
;
2488 bset
= isl_basic_set_cow(bset
);
2491 tab
= tab_for_lexmin((struct isl_basic_map
*)bset
, NULL
, 1, 0);
2494 if (isl_tab_track_bset(tab
, bset
) < 0)
2496 tab
= isl_tab_init_samples(tab
);
2499 isl_basic_set_free(bset
);
2503 static struct isl_context
*isl_context_lex_alloc(struct isl_basic_set
*dom
)
2505 struct isl_context_lex
*clex
;
2510 clex
= isl_alloc_type(dom
->ctx
, struct isl_context_lex
);
2514 clex
->context
.op
= &isl_context_lex_op
;
2516 clex
->tab
= context_tab_for_lexmin(isl_basic_set_copy(dom
));
2517 clex
->tab
= restore_lexmin(clex
->tab
);
2518 clex
->tab
= check_integer_feasible(clex
->tab
);
2522 return &clex
->context
;
2524 clex
->context
.op
->free(&clex
->context
);
2528 struct isl_context_gbr
{
2529 struct isl_context context
;
2530 struct isl_tab
*tab
;
2531 struct isl_tab
*shifted
;
2532 struct isl_tab
*cone
;
2535 static struct isl_tab
*context_gbr_detect_nonnegative_parameters(
2536 struct isl_context
*context
, struct isl_tab
*tab
)
2538 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2541 return tab_detect_nonnegative_parameters(tab
, cgbr
->tab
);
2544 static struct isl_basic_set
*context_gbr_peek_basic_set(
2545 struct isl_context
*context
)
2547 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2550 return isl_tab_peek_bset(cgbr
->tab
);
2553 static struct isl_tab
*context_gbr_peek_tab(struct isl_context
*context
)
2555 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2559 /* Initialize the "shifted" tableau of the context, which
2560 * contains the constraints of the original tableau shifted
2561 * by the sum of all negative coefficients. This ensures
2562 * that any rational point in the shifted tableau can
2563 * be rounded up to yield an integer point in the original tableau.
2565 static void gbr_init_shifted(struct isl_context_gbr
*cgbr
)
2568 struct isl_vec
*cst
;
2569 struct isl_basic_set
*bset
= isl_tab_peek_bset(cgbr
->tab
);
2570 unsigned dim
= isl_basic_set_total_dim(bset
);
2572 cst
= isl_vec_alloc(cgbr
->tab
->mat
->ctx
, bset
->n_ineq
);
2576 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
2577 isl_int_set(cst
->el
[i
], bset
->ineq
[i
][0]);
2578 for (j
= 0; j
< dim
; ++j
) {
2579 if (!isl_int_is_neg(bset
->ineq
[i
][1 + j
]))
2581 isl_int_add(bset
->ineq
[i
][0], bset
->ineq
[i
][0],
2582 bset
->ineq
[i
][1 + j
]);
2586 cgbr
->shifted
= isl_tab_from_basic_set(bset
);
2588 for (i
= 0; i
< bset
->n_ineq
; ++i
)
2589 isl_int_set(bset
->ineq
[i
][0], cst
->el
[i
]);
2594 /* Check if the shifted tableau is non-empty, and if so
2595 * use the sample point to construct an integer point
2596 * of the context tableau.
2598 static struct isl_vec
*gbr_get_shifted_sample(struct isl_context_gbr
*cgbr
)
2600 struct isl_vec
*sample
;
2603 gbr_init_shifted(cgbr
);
2606 if (cgbr
->shifted
->empty
)
2607 return isl_vec_alloc(cgbr
->tab
->mat
->ctx
, 0);
2609 sample
= isl_tab_get_sample_value(cgbr
->shifted
);
2610 sample
= isl_vec_ceil(sample
);
2615 static struct isl_basic_set
*drop_constant_terms(struct isl_basic_set
*bset
)
2622 for (i
= 0; i
< bset
->n_eq
; ++i
)
2623 isl_int_set_si(bset
->eq
[i
][0], 0);
2625 for (i
= 0; i
< bset
->n_ineq
; ++i
)
2626 isl_int_set_si(bset
->ineq
[i
][0], 0);
2631 static int use_shifted(struct isl_context_gbr
*cgbr
)
2633 return cgbr
->tab
->bmap
->n_eq
== 0 && cgbr
->tab
->bmap
->n_div
== 0;
2636 static struct isl_vec
*gbr_get_sample(struct isl_context_gbr
*cgbr
)
2638 struct isl_basic_set
*bset
;
2639 struct isl_basic_set
*cone
;
2641 if (isl_tab_sample_is_integer(cgbr
->tab
))
2642 return isl_tab_get_sample_value(cgbr
->tab
);
2644 if (use_shifted(cgbr
)) {
2645 struct isl_vec
*sample
;
2647 sample
= gbr_get_shifted_sample(cgbr
);
2648 if (!sample
|| sample
->size
> 0)
2651 isl_vec_free(sample
);
2655 bset
= isl_tab_peek_bset(cgbr
->tab
);
2656 cgbr
->cone
= isl_tab_from_recession_cone(bset
, 0);
2659 if (isl_tab_track_bset(cgbr
->cone
, isl_basic_set_dup(bset
)) < 0)
2662 if (isl_tab_detect_implicit_equalities(cgbr
->cone
) < 0)
2665 if (cgbr
->cone
->n_dead
== cgbr
->cone
->n_col
) {
2666 struct isl_vec
*sample
;
2667 struct isl_tab_undo
*snap
;
2669 if (cgbr
->tab
->basis
) {
2670 if (cgbr
->tab
->basis
->n_col
!= 1 + cgbr
->tab
->n_var
) {
2671 isl_mat_free(cgbr
->tab
->basis
);
2672 cgbr
->tab
->basis
= NULL
;
2674 cgbr
->tab
->n_zero
= 0;
2675 cgbr
->tab
->n_unbounded
= 0;
2678 snap
= isl_tab_snap(cgbr
->tab
);
2680 sample
= isl_tab_sample(cgbr
->tab
);
2682 if (isl_tab_rollback(cgbr
->tab
, snap
) < 0) {
2683 isl_vec_free(sample
);
2690 cone
= isl_basic_set_dup(isl_tab_peek_bset(cgbr
->cone
));
2691 cone
= drop_constant_terms(cone
);
2692 cone
= isl_basic_set_update_from_tab(cone
, cgbr
->cone
);
2693 cone
= isl_basic_set_underlying_set(cone
);
2694 cone
= isl_basic_set_gauss(cone
, NULL
);
2696 bset
= isl_basic_set_dup(isl_tab_peek_bset(cgbr
->tab
));
2697 bset
= isl_basic_set_update_from_tab(bset
, cgbr
->tab
);
2698 bset
= isl_basic_set_underlying_set(bset
);
2699 bset
= isl_basic_set_gauss(bset
, NULL
);
2701 return isl_basic_set_sample_with_cone(bset
, cone
);
2704 static void check_gbr_integer_feasible(struct isl_context_gbr
*cgbr
)
2706 struct isl_vec
*sample
;
2711 if (cgbr
->tab
->empty
)
2714 sample
= gbr_get_sample(cgbr
);
2718 if (sample
->size
== 0) {
2719 isl_vec_free(sample
);
2720 if (isl_tab_mark_empty(cgbr
->tab
) < 0)
2725 cgbr
->tab
= isl_tab_add_sample(cgbr
->tab
, sample
);
2729 isl_tab_free(cgbr
->tab
);
2733 static struct isl_tab
*add_gbr_eq(struct isl_tab
*tab
, isl_int
*eq
)
2740 if (isl_tab_extend_cons(tab
, 2) < 0)
2743 if (isl_tab_add_eq(tab
, eq
) < 0)
2752 static void context_gbr_add_eq(struct isl_context
*context
, isl_int
*eq
,
2753 int check
, int update
)
2755 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2757 cgbr
->tab
= add_gbr_eq(cgbr
->tab
, eq
);
2759 if (cgbr
->cone
&& cgbr
->cone
->n_col
!= cgbr
->cone
->n_dead
) {
2760 if (isl_tab_extend_cons(cgbr
->cone
, 2) < 0)
2762 if (isl_tab_add_eq(cgbr
->cone
, eq
) < 0)
2767 int v
= tab_has_valid_sample(cgbr
->tab
, eq
, 1);
2771 check_gbr_integer_feasible(cgbr
);
2774 cgbr
->tab
= check_samples(cgbr
->tab
, eq
, 1);
2777 isl_tab_free(cgbr
->tab
);
2781 static void add_gbr_ineq(struct isl_context_gbr
*cgbr
, isl_int
*ineq
)
2786 if (isl_tab_extend_cons(cgbr
->tab
, 1) < 0)
2789 if (isl_tab_add_ineq(cgbr
->tab
, ineq
) < 0)
2792 if (cgbr
->shifted
&& !cgbr
->shifted
->empty
&& use_shifted(cgbr
)) {
2795 dim
= isl_basic_map_total_dim(cgbr
->tab
->bmap
);
2797 if (isl_tab_extend_cons(cgbr
->shifted
, 1) < 0)
2800 for (i
= 0; i
< dim
; ++i
) {
2801 if (!isl_int_is_neg(ineq
[1 + i
]))
2803 isl_int_add(ineq
[0], ineq
[0], ineq
[1 + i
]);
2806 if (isl_tab_add_ineq(cgbr
->shifted
, ineq
) < 0)
2809 for (i
= 0; i
< dim
; ++i
) {
2810 if (!isl_int_is_neg(ineq
[1 + i
]))
2812 isl_int_sub(ineq
[0], ineq
[0], ineq
[1 + i
]);
2816 if (cgbr
->cone
&& cgbr
->cone
->n_col
!= cgbr
->cone
->n_dead
) {
2817 if (isl_tab_extend_cons(cgbr
->cone
, 1) < 0)
2819 if (isl_tab_add_ineq(cgbr
->cone
, ineq
) < 0)
2825 isl_tab_free(cgbr
->tab
);
2829 static void context_gbr_add_ineq(struct isl_context
*context
, isl_int
*ineq
,
2830 int check
, int update
)
2832 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2834 add_gbr_ineq(cgbr
, ineq
);
2839 int v
= tab_has_valid_sample(cgbr
->tab
, ineq
, 0);
2843 check_gbr_integer_feasible(cgbr
);
2846 cgbr
->tab
= check_samples(cgbr
->tab
, ineq
, 0);
2849 isl_tab_free(cgbr
->tab
);
2853 static int context_gbr_add_ineq_wrap(void *user
, isl_int
*ineq
)
2855 struct isl_context
*context
= (struct isl_context
*)user
;
2856 context_gbr_add_ineq(context
, ineq
, 0, 0);
2857 return context
->op
->is_ok(context
) ? 0 : -1;
2860 static enum isl_tab_row_sign
context_gbr_ineq_sign(struct isl_context
*context
,
2861 isl_int
*ineq
, int strict
)
2863 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2864 return tab_ineq_sign(cgbr
->tab
, ineq
, strict
);
2867 /* Check whether "ineq" can be added to the tableau without rendering
2870 static int context_gbr_test_ineq(struct isl_context
*context
, isl_int
*ineq
)
2872 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2873 struct isl_tab_undo
*snap
;
2874 struct isl_tab_undo
*shifted_snap
= NULL
;
2875 struct isl_tab_undo
*cone_snap
= NULL
;
2881 if (isl_tab_extend_cons(cgbr
->tab
, 1) < 0)
2884 snap
= isl_tab_snap(cgbr
->tab
);
2886 shifted_snap
= isl_tab_snap(cgbr
->shifted
);
2888 cone_snap
= isl_tab_snap(cgbr
->cone
);
2889 add_gbr_ineq(cgbr
, ineq
);
2890 check_gbr_integer_feasible(cgbr
);
2893 feasible
= !cgbr
->tab
->empty
;
2894 if (isl_tab_rollback(cgbr
->tab
, snap
) < 0)
2897 if (isl_tab_rollback(cgbr
->shifted
, shifted_snap
))
2899 } else if (cgbr
->shifted
) {
2900 isl_tab_free(cgbr
->shifted
);
2901 cgbr
->shifted
= NULL
;
2904 if (isl_tab_rollback(cgbr
->cone
, cone_snap
))
2906 } else if (cgbr
->cone
) {
2907 isl_tab_free(cgbr
->cone
);
2914 /* Return the column of the last of the variables associated to
2915 * a column that has a non-zero coefficient.
2916 * This function is called in a context where only coefficients
2917 * of parameters or divs can be non-zero.
2919 static int last_non_zero_var_col(struct isl_tab
*tab
, isl_int
*p
)
2923 unsigned dim
= tab
->n_var
- tab
->n_param
- tab
->n_div
;
2925 if (tab
->n_var
== 0)
2928 for (i
= tab
->n_var
- 1; i
>= 0; --i
) {
2929 if (i
>= tab
->n_param
&& i
< tab
->n_var
- tab
->n_div
)
2931 if (tab
->var
[i
].is_row
)
2933 col
= tab
->var
[i
].index
;
2934 if (!isl_int_is_zero(p
[col
]))
2941 /* Look through all the recently added equalities in the context
2942 * to see if we can propagate any of them to the main tableau.
2944 * The newly added equalities in the context are encoded as pairs
2945 * of inequalities starting at inequality "first".
2947 * We tentatively add each of these equalities to the main tableau
2948 * and if this happens to result in a row with a final coefficient
2949 * that is one or negative one, we use it to kill a column
2950 * in the main tableau. Otherwise, we discard the tentatively
2953 static void propagate_equalities(struct isl_context_gbr
*cgbr
,
2954 struct isl_tab
*tab
, unsigned first
)
2957 struct isl_vec
*eq
= NULL
;
2959 eq
= isl_vec_alloc(tab
->mat
->ctx
, 1 + tab
->n_var
);
2963 if (isl_tab_extend_cons(tab
, (cgbr
->tab
->bmap
->n_ineq
- first
)/2) < 0)
2966 isl_seq_clr(eq
->el
+ 1 + tab
->n_param
,
2967 tab
->n_var
- tab
->n_param
- tab
->n_div
);
2968 for (i
= first
; i
< cgbr
->tab
->bmap
->n_ineq
; i
+= 2) {
2971 struct isl_tab_undo
*snap
;
2972 snap
= isl_tab_snap(tab
);
2974 isl_seq_cpy(eq
->el
, cgbr
->tab
->bmap
->ineq
[i
], 1 + tab
->n_param
);
2975 isl_seq_cpy(eq
->el
+ 1 + tab
->n_var
- tab
->n_div
,
2976 cgbr
->tab
->bmap
->ineq
[i
] + 1 + tab
->n_param
,
2979 r
= isl_tab_add_row(tab
, eq
->el
);
2982 r
= tab
->con
[r
].index
;
2983 j
= last_non_zero_var_col(tab
, tab
->mat
->row
[r
] + 2 + tab
->M
);
2984 if (j
< 0 || j
< tab
->n_dead
||
2985 !isl_int_is_one(tab
->mat
->row
[r
][0]) ||
2986 (!isl_int_is_one(tab
->mat
->row
[r
][2 + tab
->M
+ j
]) &&
2987 !isl_int_is_negone(tab
->mat
->row
[r
][2 + tab
->M
+ j
]))) {
2988 if (isl_tab_rollback(tab
, snap
) < 0)
2992 if (isl_tab_pivot(tab
, r
, j
) < 0)
2994 if (isl_tab_kill_col(tab
, j
) < 0)
2997 tab
= restore_lexmin(tab
);
3005 isl_tab_free(cgbr
->tab
);
3009 static int context_gbr_detect_equalities(struct isl_context
*context
,
3010 struct isl_tab
*tab
)
3012 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3013 struct isl_ctx
*ctx
;
3015 enum isl_lp_result res
;
3018 ctx
= cgbr
->tab
->mat
->ctx
;
3021 struct isl_basic_set
*bset
= isl_tab_peek_bset(cgbr
->tab
);
3022 cgbr
->cone
= isl_tab_from_recession_cone(bset
, 0);
3025 if (isl_tab_track_bset(cgbr
->cone
, isl_basic_set_dup(bset
)) < 0)
3028 if (isl_tab_detect_implicit_equalities(cgbr
->cone
) < 0)
3031 n_ineq
= cgbr
->tab
->bmap
->n_ineq
;
3032 cgbr
->tab
= isl_tab_detect_equalities(cgbr
->tab
, cgbr
->cone
);
3033 if (cgbr
->tab
&& cgbr
->tab
->bmap
->n_ineq
> n_ineq
)
3034 propagate_equalities(cgbr
, tab
, n_ineq
);
3038 isl_tab_free(cgbr
->tab
);
3043 static int context_gbr_get_div(struct isl_context
*context
, struct isl_tab
*tab
,
3044 struct isl_vec
*div
)
3046 return get_div(tab
, context
, div
);
3049 static int context_gbr_add_div(struct isl_context
*context
, struct isl_vec
*div
)
3051 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3055 if (isl_tab_extend_cons(cgbr
->cone
, 3) < 0)
3057 if (isl_tab_extend_vars(cgbr
->cone
, 1) < 0)
3059 if (isl_tab_allocate_var(cgbr
->cone
) <0)
3062 cgbr
->cone
->bmap
= isl_basic_map_extend_dim(cgbr
->cone
->bmap
,
3063 isl_basic_map_get_dim(cgbr
->cone
->bmap
), 1, 0, 2);
3064 k
= isl_basic_map_alloc_div(cgbr
->cone
->bmap
);
3067 isl_seq_cpy(cgbr
->cone
->bmap
->div
[k
], div
->el
, div
->size
);
3068 if (isl_tab_push(cgbr
->cone
, isl_tab_undo_bmap_div
) < 0)
3071 return context_tab_add_div(cgbr
->tab
, div
,
3072 context_gbr_add_ineq_wrap
, context
);
3075 static int context_gbr_best_split(struct isl_context
*context
,
3076 struct isl_tab
*tab
)
3078 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3079 struct isl_tab_undo
*snap
;
3082 snap
= isl_tab_snap(cgbr
->tab
);
3083 r
= best_split(tab
, cgbr
->tab
);
3085 if (r
>= 0 && isl_tab_rollback(cgbr
->tab
, snap
) < 0)
3091 static int context_gbr_is_empty(struct isl_context
*context
)
3093 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3096 return cgbr
->tab
->empty
;
3099 struct isl_gbr_tab_undo
{
3100 struct isl_tab_undo
*tab_snap
;
3101 struct isl_tab_undo
*shifted_snap
;
3102 struct isl_tab_undo
*cone_snap
;
3105 static void *context_gbr_save(struct isl_context
*context
)
3107 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3108 struct isl_gbr_tab_undo
*snap
;
3110 snap
= isl_alloc_type(cgbr
->tab
->mat
->ctx
, struct isl_gbr_tab_undo
);
3114 snap
->tab_snap
= isl_tab_snap(cgbr
->tab
);
3115 if (isl_tab_save_samples(cgbr
->tab
) < 0)
3119 snap
->shifted_snap
= isl_tab_snap(cgbr
->shifted
);
3121 snap
->shifted_snap
= NULL
;
3124 snap
->cone_snap
= isl_tab_snap(cgbr
->cone
);
3126 snap
->cone_snap
= NULL
;
3134 static void context_gbr_restore(struct isl_context
*context
, void *save
)
3136 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3137 struct isl_gbr_tab_undo
*snap
= (struct isl_gbr_tab_undo
*)save
;
3140 if (isl_tab_rollback(cgbr
->tab
, snap
->tab_snap
) < 0) {
3141 isl_tab_free(cgbr
->tab
);
3145 if (snap
->shifted_snap
) {
3146 if (isl_tab_rollback(cgbr
->shifted
, snap
->shifted_snap
) < 0)
3148 } else if (cgbr
->shifted
) {
3149 isl_tab_free(cgbr
->shifted
);
3150 cgbr
->shifted
= NULL
;
3153 if (snap
->cone_snap
) {
3154 if (isl_tab_rollback(cgbr
->cone
, snap
->cone_snap
) < 0)
3156 } else if (cgbr
->cone
) {
3157 isl_tab_free(cgbr
->cone
);
3166 isl_tab_free(cgbr
->tab
);
3170 static int context_gbr_is_ok(struct isl_context
*context
)
3172 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3176 static void context_gbr_invalidate(struct isl_context
*context
)
3178 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3179 isl_tab_free(cgbr
->tab
);
3183 static void context_gbr_free(struct isl_context
*context
)
3185 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3186 isl_tab_free(cgbr
->tab
);
3187 isl_tab_free(cgbr
->shifted
);
3188 isl_tab_free(cgbr
->cone
);
3192 struct isl_context_op isl_context_gbr_op
= {
3193 context_gbr_detect_nonnegative_parameters
,
3194 context_gbr_peek_basic_set
,
3195 context_gbr_peek_tab
,
3197 context_gbr_add_ineq
,
3198 context_gbr_ineq_sign
,
3199 context_gbr_test_ineq
,
3200 context_gbr_get_div
,
3201 context_gbr_add_div
,
3202 context_gbr_detect_equalities
,
3203 context_gbr_best_split
,
3204 context_gbr_is_empty
,
3207 context_gbr_restore
,
3208 context_gbr_invalidate
,
3212 static struct isl_context
*isl_context_gbr_alloc(struct isl_basic_set
*dom
)
3214 struct isl_context_gbr
*cgbr
;
3219 cgbr
= isl_calloc_type(dom
->ctx
, struct isl_context_gbr
);
3223 cgbr
->context
.op
= &isl_context_gbr_op
;
3225 cgbr
->shifted
= NULL
;
3227 cgbr
->tab
= isl_tab_from_basic_set(dom
);
3228 cgbr
->tab
= isl_tab_init_samples(cgbr
->tab
);
3231 if (isl_tab_track_bset(cgbr
->tab
,
3232 isl_basic_set_cow(isl_basic_set_copy(dom
))) < 0)
3234 check_gbr_integer_feasible(cgbr
);
3236 return &cgbr
->context
;
3238 cgbr
->context
.op
->free(&cgbr
->context
);
3242 static struct isl_context
*isl_context_alloc(struct isl_basic_set
*dom
)
3247 if (dom
->ctx
->opt
->context
== ISL_CONTEXT_LEXMIN
)
3248 return isl_context_lex_alloc(dom
);
3250 return isl_context_gbr_alloc(dom
);
3253 /* Construct an isl_sol_map structure for accumulating the solution.
3254 * If track_empty is set, then we also keep track of the parts
3255 * of the context where there is no solution.
3256 * If max is set, then we are solving a maximization, rather than
3257 * a minimization problem, which means that the variables in the
3258 * tableau have value "M - x" rather than "M + x".
3260 static struct isl_sol_map
*sol_map_init(struct isl_basic_map
*bmap
,
3261 struct isl_basic_set
*dom
, int track_empty
, int max
)
3263 struct isl_sol_map
*sol_map
= NULL
;
3268 sol_map
= isl_calloc_type(bmap
->ctx
, struct isl_sol_map
);
3272 sol_map
->sol
.rational
= ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
3273 sol_map
->sol
.dec_level
.callback
.run
= &sol_dec_level_wrap
;
3274 sol_map
->sol
.dec_level
.sol
= &sol_map
->sol
;
3275 sol_map
->sol
.max
= max
;
3276 sol_map
->sol
.n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
3277 sol_map
->sol
.add
= &sol_map_add_wrap
;
3278 sol_map
->sol
.add_empty
= track_empty
? &sol_map_add_empty_wrap
: NULL
;
3279 sol_map
->sol
.free
= &sol_map_free_wrap
;
3280 sol_map
->map
= isl_map_alloc_dim(isl_basic_map_get_dim(bmap
), 1,
3285 sol_map
->sol
.context
= isl_context_alloc(dom
);
3286 if (!sol_map
->sol
.context
)
3290 sol_map
->empty
= isl_set_alloc_dim(isl_basic_set_get_dim(dom
),
3291 1, ISL_SET_DISJOINT
);
3292 if (!sol_map
->empty
)
3296 isl_basic_set_free(dom
);
3299 isl_basic_set_free(dom
);
3300 sol_map_free(sol_map
);
3304 /* Check whether all coefficients of (non-parameter) variables
3305 * are non-positive, meaning that no pivots can be performed on the row.
3307 static int is_critical(struct isl_tab
*tab
, int row
)
3310 unsigned off
= 2 + tab
->M
;
3312 for (j
= tab
->n_dead
; j
< tab
->n_col
; ++j
) {
3313 if (tab
->col_var
[j
] >= 0 &&
3314 (tab
->col_var
[j
] < tab
->n_param
||
3315 tab
->col_var
[j
] >= tab
->n_var
- tab
->n_div
))
3318 if (isl_int_is_pos(tab
->mat
->row
[row
][off
+ j
]))
3325 /* Check whether the inequality represented by vec is strict over the integers,
3326 * i.e., there are no integer values satisfying the constraint with
3327 * equality. This happens if the gcd of the coefficients is not a divisor
3328 * of the constant term. If so, scale the constraint down by the gcd
3329 * of the coefficients.
3331 static int is_strict(struct isl_vec
*vec
)
3337 isl_seq_gcd(vec
->el
+ 1, vec
->size
- 1, &gcd
);
3338 if (!isl_int_is_one(gcd
)) {
3339 strict
= !isl_int_is_divisible_by(vec
->el
[0], gcd
);
3340 isl_int_fdiv_q(vec
->el
[0], vec
->el
[0], gcd
);
3341 isl_seq_scale_down(vec
->el
+ 1, vec
->el
+ 1, gcd
, vec
->size
-1);
3348 /* Determine the sign of the given row of the main tableau.
3349 * The result is one of
3350 * isl_tab_row_pos: always non-negative; no pivot needed
3351 * isl_tab_row_neg: always non-positive; pivot
3352 * isl_tab_row_any: can be both positive and negative; split
3354 * We first handle some simple cases
3355 * - the row sign may be known already
3356 * - the row may be obviously non-negative
3357 * - the parametric constant may be equal to that of another row
3358 * for which we know the sign. This sign will be either "pos" or
3359 * "any". If it had been "neg" then we would have pivoted before.
3361 * If none of these cases hold, we check the value of the row for each
3362 * of the currently active samples. Based on the signs of these values
3363 * we make an initial determination of the sign of the row.
3365 * all zero -> unk(nown)
3366 * all non-negative -> pos
3367 * all non-positive -> neg
3368 * both negative and positive -> all
3370 * If we end up with "all", we are done.
3371 * Otherwise, we perform a check for positive and/or negative
3372 * values as follows.
3374 * samples neg unk pos
3380 * There is no special sign for "zero", because we can usually treat zero
3381 * as either non-negative or non-positive, whatever works out best.
3382 * However, if the row is "critical", meaning that pivoting is impossible
3383 * then we don't want to limp zero with the non-positive case, because
3384 * then we we would lose the solution for those values of the parameters
3385 * where the value of the row is zero. Instead, we treat 0 as non-negative
3386 * ensuring a split if the row can attain both zero and negative values.
3387 * The same happens when the original constraint was one that could not
3388 * be satisfied with equality by any integer values of the parameters.
3389 * In this case, we normalize the constraint, but then a value of zero
3390 * for the normalized constraint is actually a positive value for the
3391 * original constraint, so again we need to treat zero as non-negative.
3392 * In both these cases, we have the following decision tree instead:
3394 * all non-negative -> pos
3395 * all negative -> neg
3396 * both negative and non-negative -> all
3404 static enum isl_tab_row_sign
row_sign(struct isl_tab
*tab
,
3405 struct isl_sol
*sol
, int row
)
3407 struct isl_vec
*ineq
= NULL
;
3408 enum isl_tab_row_sign res
= isl_tab_row_unknown
;
3413 if (tab
->row_sign
[row
] != isl_tab_row_unknown
)
3414 return tab
->row_sign
[row
];
3415 if (is_obviously_nonneg(tab
, row
))
3416 return isl_tab_row_pos
;
3417 for (row2
= tab
->n_redundant
; row2
< tab
->n_row
; ++row2
) {
3418 if (tab
->row_sign
[row2
] == isl_tab_row_unknown
)
3420 if (identical_parameter_line(tab
, row
, row2
))
3421 return tab
->row_sign
[row2
];
3424 critical
= is_critical(tab
, row
);
3426 ineq
= get_row_parameter_ineq(tab
, row
);
3430 strict
= is_strict(ineq
);
3432 res
= sol
->context
->op
->ineq_sign(sol
->context
, ineq
->el
,
3433 critical
|| strict
);
3435 if (res
== isl_tab_row_unknown
|| res
== isl_tab_row_pos
) {
3436 /* test for negative values */
3438 isl_seq_neg(ineq
->el
, ineq
->el
, ineq
->size
);
3439 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3441 feasible
= sol
->context
->op
->test_ineq(sol
->context
, ineq
->el
);
3445 res
= isl_tab_row_pos
;
3447 res
= (res
== isl_tab_row_unknown
) ? isl_tab_row_neg
3449 if (res
== isl_tab_row_neg
) {
3450 isl_seq_neg(ineq
->el
, ineq
->el
, ineq
->size
);
3451 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3455 if (res
== isl_tab_row_neg
) {
3456 /* test for positive values */
3458 if (!critical
&& !strict
)
3459 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3461 feasible
= sol
->context
->op
->test_ineq(sol
->context
, ineq
->el
);
3465 res
= isl_tab_row_any
;
3472 return isl_tab_row_unknown
;
3475 static void find_solutions(struct isl_sol
*sol
, struct isl_tab
*tab
);
3477 /* Find solutions for values of the parameters that satisfy the given
3480 * We currently take a snapshot of the context tableau that is reset
3481 * when we return from this function, while we make a copy of the main
3482 * tableau, leaving the original main tableau untouched.
3483 * These are fairly arbitrary choices. Making a copy also of the context
3484 * tableau would obviate the need to undo any changes made to it later,
3485 * while taking a snapshot of the main tableau could reduce memory usage.
3486 * If we were to switch to taking a snapshot of the main tableau,
3487 * we would have to keep in mind that we need to save the row signs
3488 * and that we need to do this before saving the current basis
3489 * such that the basis has been restore before we restore the row signs.
3491 static void find_in_pos(struct isl_sol
*sol
, struct isl_tab
*tab
, isl_int
*ineq
)
3497 saved
= sol
->context
->op
->save(sol
->context
);
3499 tab
= isl_tab_dup(tab
);
3503 sol
->context
->op
->add_ineq(sol
->context
, ineq
, 0, 1);
3505 find_solutions(sol
, tab
);
3508 sol
->context
->op
->restore(sol
->context
, saved
);
3514 /* Record the absence of solutions for those values of the parameters
3515 * that do not satisfy the given inequality with equality.
3517 static void no_sol_in_strict(struct isl_sol
*sol
,
3518 struct isl_tab
*tab
, struct isl_vec
*ineq
)
3523 if (!sol
->context
|| sol
->error
)
3525 saved
= sol
->context
->op
->save(sol
->context
);
3527 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3529 sol
->context
->op
->add_ineq(sol
->context
, ineq
->el
, 1, 0);
3538 isl_int_add_ui(ineq
->el
[0], ineq
->el
[0], 1);
3540 sol
->context
->op
->restore(sol
->context
, saved
);
3546 /* Compute the lexicographic minimum of the set represented by the main
3547 * tableau "tab" within the context "sol->context_tab".
3548 * On entry the sample value of the main tableau is lexicographically
3549 * less than or equal to this lexicographic minimum.
3550 * Pivots are performed until a feasible point is found, which is then
3551 * necessarily equal to the minimum, or until the tableau is found to
3552 * be infeasible. Some pivots may need to be performed for only some
3553 * feasible values of the context tableau. If so, the context tableau
3554 * is split into a part where the pivot is needed and a part where it is not.
3556 * Whenever we enter the main loop, the main tableau is such that no
3557 * "obvious" pivots need to be performed on it, where "obvious" means
3558 * that the given row can be seen to be negative without looking at
3559 * the context tableau. In particular, for non-parametric problems,
3560 * no pivots need to be performed on the main tableau.
3561 * The caller of find_solutions is responsible for making this property
3562 * hold prior to the first iteration of the loop, while restore_lexmin
3563 * is called before every other iteration.
3565 * Inside the main loop, we first examine the signs of the rows of
3566 * the main tableau within the context of the context tableau.
3567 * If we find a row that is always non-positive for all values of
3568 * the parameters satisfying the context tableau and negative for at
3569 * least one value of the parameters, we perform the appropriate pivot
3570 * and start over. An exception is the case where no pivot can be
3571 * performed on the row. In this case, we require that the sign of
3572 * the row is negative for all values of the parameters (rather than just
3573 * non-positive). This special case is handled inside row_sign, which
3574 * will say that the row can have any sign if it determines that it can
3575 * attain both negative and zero values.
3577 * If we can't find a row that always requires a pivot, but we can find
3578 * one or more rows that require a pivot for some values of the parameters
3579 * (i.e., the row can attain both positive and negative signs), then we split
3580 * the context tableau into two parts, one where we force the sign to be
3581 * non-negative and one where we force is to be negative.
3582 * The non-negative part is handled by a recursive call (through find_in_pos).
3583 * Upon returning from this call, we continue with the negative part and
3584 * perform the required pivot.
3586 * If no such rows can be found, all rows are non-negative and we have
3587 * found a (rational) feasible point. If we only wanted a rational point
3589 * Otherwise, we check if all values of the sample point of the tableau
3590 * are integral for the variables. If so, we have found the minimal
3591 * integral point and we are done.
3592 * If the sample point is not integral, then we need to make a distinction
3593 * based on whether the constant term is non-integral or the coefficients
3594 * of the parameters. Furthermore, in order to decide how to handle
3595 * the non-integrality, we also need to know whether the coefficients
3596 * of the other columns in the tableau are integral. This leads
3597 * to the following table. The first two rows do not correspond
3598 * to a non-integral sample point and are only mentioned for completeness.
3600 * constant parameters other
3603 * int int rat | -> no problem
3605 * rat int int -> fail
3607 * rat int rat -> cut
3610 * rat rat rat | -> parametric cut
3613 * rat rat int | -> split context
3615 * If the parametric constant is completely integral, then there is nothing
3616 * to be done. If the constant term is non-integral, but all the other
3617 * coefficient are integral, then there is nothing that can be done
3618 * and the tableau has no integral solution.
3619 * If, on the other hand, one or more of the other columns have rational
3620 * coefficients, but the parameter coefficients are all integral, then
3621 * we can perform a regular (non-parametric) cut.
3622 * Finally, if there is any parameter coefficient that is non-integral,
3623 * then we need to involve the context tableau. There are two cases here.
3624 * If at least one other column has a rational coefficient, then we
3625 * can perform a parametric cut in the main tableau by adding a new
3626 * integer division in the context tableau.
3627 * If all other columns have integral coefficients, then we need to
3628 * enforce that the rational combination of parameters (c + \sum a_i y_i)/m
3629 * is always integral. We do this by introducing an integer division
3630 * q = floor((c + \sum a_i y_i)/m) and stipulating that its argument should
3631 * always be integral in the context tableau, i.e., m q = c + \sum a_i y_i.
3632 * Since q is expressed in the tableau as
3633 * c + \sum a_i y_i - m q >= 0
3634 * -c - \sum a_i y_i + m q + m - 1 >= 0
3635 * it is sufficient to add the inequality
3636 * -c - \sum a_i y_i + m q >= 0
3637 * In the part of the context where this inequality does not hold, the
3638 * main tableau is marked as being empty.
3640 static void find_solutions(struct isl_sol
*sol
, struct isl_tab
*tab
)
3642 struct isl_context
*context
;
3644 if (!tab
|| sol
->error
)
3647 context
= sol
->context
;
3651 if (context
->op
->is_empty(context
))
3654 for (; tab
&& !tab
->empty
; tab
= restore_lexmin(tab
)) {
3657 enum isl_tab_row_sign sgn
;
3661 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
3662 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
3664 sgn
= row_sign(tab
, sol
, row
);
3667 tab
->row_sign
[row
] = sgn
;
3668 if (sgn
== isl_tab_row_any
)
3670 if (sgn
== isl_tab_row_any
&& split
== -1)
3672 if (sgn
== isl_tab_row_neg
)
3675 if (row
< tab
->n_row
)
3678 struct isl_vec
*ineq
;
3680 split
= context
->op
->best_split(context
, tab
);
3683 ineq
= get_row_parameter_ineq(tab
, split
);
3687 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
3688 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
3690 if (tab
->row_sign
[row
] == isl_tab_row_any
)
3691 tab
->row_sign
[row
] = isl_tab_row_unknown
;
3693 tab
->row_sign
[split
] = isl_tab_row_pos
;
3695 find_in_pos(sol
, tab
, ineq
->el
);
3696 tab
->row_sign
[split
] = isl_tab_row_neg
;
3698 isl_seq_neg(ineq
->el
, ineq
->el
, ineq
->size
);
3699 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3701 context
->op
->add_ineq(context
, ineq
->el
, 0, 1);
3709 row
= first_non_integer_row(tab
, &flags
);
3712 if (ISL_FL_ISSET(flags
, I_PAR
)) {
3713 if (ISL_FL_ISSET(flags
, I_VAR
)) {
3714 if (isl_tab_mark_empty(tab
) < 0)
3718 row
= add_cut(tab
, row
);
3719 } else if (ISL_FL_ISSET(flags
, I_VAR
)) {
3720 struct isl_vec
*div
;
3721 struct isl_vec
*ineq
;
3723 div
= get_row_split_div(tab
, row
);
3726 d
= context
->op
->get_div(context
, tab
, div
);
3730 ineq
= ineq_for_div(context
->op
->peek_basic_set(context
), d
);
3734 no_sol_in_strict(sol
, tab
, ineq
);
3735 isl_seq_neg(ineq
->el
, ineq
->el
, ineq
->size
);
3736 context
->op
->add_ineq(context
, ineq
->el
, 1, 1);
3738 if (sol
->error
|| !context
->op
->is_ok(context
))
3740 tab
= set_row_cst_to_div(tab
, row
, d
);
3741 if (context
->op
->is_empty(context
))
3744 row
= add_parametric_cut(tab
, row
, context
);
3757 /* Compute the lexicographic minimum of the set represented by the main
3758 * tableau "tab" within the context "sol->context_tab".
3760 * As a preprocessing step, we first transfer all the purely parametric
3761 * equalities from the main tableau to the context tableau, i.e.,
3762 * parameters that have been pivoted to a row.
3763 * These equalities are ignored by the main algorithm, because the
3764 * corresponding rows may not be marked as being non-negative.
3765 * In parts of the context where the added equality does not hold,
3766 * the main tableau is marked as being empty.
3768 static void find_solutions_main(struct isl_sol
*sol
, struct isl_tab
*tab
)
3777 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
3781 if (tab
->row_var
[row
] < 0)
3783 if (tab
->row_var
[row
] >= tab
->n_param
&&
3784 tab
->row_var
[row
] < tab
->n_var
- tab
->n_div
)
3786 if (tab
->row_var
[row
] < tab
->n_param
)
3787 p
= tab
->row_var
[row
];
3789 p
= tab
->row_var
[row
]
3790 + tab
->n_param
- (tab
->n_var
- tab
->n_div
);
3792 eq
= isl_vec_alloc(tab
->mat
->ctx
, 1+tab
->n_param
+tab
->n_div
);
3795 get_row_parameter_line(tab
, row
, eq
->el
);
3796 isl_int_neg(eq
->el
[1 + p
], tab
->mat
->row
[row
][0]);
3797 eq
= isl_vec_normalize(eq
);
3800 no_sol_in_strict(sol
, tab
, eq
);
3802 isl_seq_neg(eq
->el
, eq
->el
, eq
->size
);
3804 no_sol_in_strict(sol
, tab
, eq
);
3805 isl_seq_neg(eq
->el
, eq
->el
, eq
->size
);
3807 sol
->context
->op
->add_eq(sol
->context
, eq
->el
, 1, 1);
3811 if (isl_tab_mark_redundant(tab
, row
) < 0)
3814 if (sol
->context
->op
->is_empty(sol
->context
))
3817 row
= tab
->n_redundant
- 1;
3820 find_solutions(sol
, tab
);
3831 static void sol_map_find_solutions(struct isl_sol_map
*sol_map
,
3832 struct isl_tab
*tab
)
3834 find_solutions_main(&sol_map
->sol
, tab
);
3837 /* Check if integer division "div" of "dom" also occurs in "bmap".
3838 * If so, return its position within the divs.
3839 * If not, return -1.
3841 static int find_context_div(struct isl_basic_map
*bmap
,
3842 struct isl_basic_set
*dom
, unsigned div
)
3845 unsigned b_dim
= isl_dim_total(bmap
->dim
);
3846 unsigned d_dim
= isl_dim_total(dom
->dim
);
3848 if (isl_int_is_zero(dom
->div
[div
][0]))
3850 if (isl_seq_first_non_zero(dom
->div
[div
] + 2 + d_dim
, dom
->n_div
) != -1)
3853 for (i
= 0; i
< bmap
->n_div
; ++i
) {
3854 if (isl_int_is_zero(bmap
->div
[i
][0]))
3856 if (isl_seq_first_non_zero(bmap
->div
[i
] + 2 + d_dim
,
3857 (b_dim
- d_dim
) + bmap
->n_div
) != -1)
3859 if (isl_seq_eq(bmap
->div
[i
], dom
->div
[div
], 2 + d_dim
))
3865 /* The correspondence between the variables in the main tableau,
3866 * the context tableau, and the input map and domain is as follows.
3867 * The first n_param and the last n_div variables of the main tableau
3868 * form the variables of the context tableau.
3869 * In the basic map, these n_param variables correspond to the
3870 * parameters and the input dimensions. In the domain, they correspond
3871 * to the parameters and the set dimensions.
3872 * The n_div variables correspond to the integer divisions in the domain.
3873 * To ensure that everything lines up, we may need to copy some of the
3874 * integer divisions of the domain to the map. These have to be placed
3875 * in the same order as those in the context and they have to be placed
3876 * after any other integer divisions that the map may have.
3877 * This function performs the required reordering.
3879 static struct isl_basic_map
*align_context_divs(struct isl_basic_map
*bmap
,
3880 struct isl_basic_set
*dom
)
3886 for (i
= 0; i
< dom
->n_div
; ++i
)
3887 if (find_context_div(bmap
, dom
, i
) != -1)
3889 other
= bmap
->n_div
- common
;
3890 if (dom
->n_div
- common
> 0) {
3891 bmap
= isl_basic_map_extend_dim(bmap
, isl_dim_copy(bmap
->dim
),
3892 dom
->n_div
- common
, 0, 0);
3896 for (i
= 0; i
< dom
->n_div
; ++i
) {
3897 int pos
= find_context_div(bmap
, dom
, i
);
3899 pos
= isl_basic_map_alloc_div(bmap
);
3902 isl_int_set_si(bmap
->div
[pos
][0], 0);
3904 if (pos
!= other
+ i
)
3905 isl_basic_map_swap_div(bmap
, pos
, other
+ i
);
3909 isl_basic_map_free(bmap
);
3913 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
3914 * some obvious symmetries.
3916 * We make sure the divs in the domain are properly ordered,
3917 * because they will be added one by one in the given order
3918 * during the construction of the solution map.
3920 static __isl_give isl_map
*basic_map_partial_lexopt_base(
3921 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
3922 __isl_give isl_set
**empty
, int max
)
3924 isl_map
*result
= NULL
;
3925 struct isl_tab
*tab
;
3926 struct isl_sol_map
*sol_map
= NULL
;
3927 struct isl_context
*context
;
3930 dom
= isl_basic_set_order_divs(dom
);
3931 bmap
= align_context_divs(bmap
, dom
);
3933 sol_map
= sol_map_init(bmap
, dom
, !!empty
, max
);
3937 context
= sol_map
->sol
.context
;
3938 if (isl_basic_set_fast_is_empty(context
->op
->peek_basic_set(context
)))
3940 else if (isl_basic_map_fast_is_empty(bmap
))
3941 sol_map_add_empty_if_needed(sol_map
,
3942 isl_basic_set_copy(context
->op
->peek_basic_set(context
)));
3944 tab
= tab_for_lexmin(bmap
,
3945 context
->op
->peek_basic_set(context
), 1, max
);
3946 tab
= context
->op
->detect_nonnegative_parameters(context
, tab
);
3947 sol_map_find_solutions(sol_map
, tab
);
3949 if (sol_map
->sol
.error
)
3952 result
= isl_map_copy(sol_map
->map
);
3954 *empty
= isl_set_copy(sol_map
->empty
);
3955 sol_free(&sol_map
->sol
);
3956 isl_basic_map_free(bmap
);
3959 sol_free(&sol_map
->sol
);
3960 isl_basic_map_free(bmap
);
3964 /* Structure used during detection of parallel constraints.
3965 * n_in: number of "input" variables: isl_dim_param + isl_dim_in
3966 * n_out: number of "output" variables: isl_dim_out + isl_dim_div
3967 * val: the coefficients of the output variables
3969 struct isl_constraint_equal_info
{
3970 isl_basic_map
*bmap
;
3976 /* Check whether the coefficients of the output variables
3977 * of the constraint in "entry" are equal to info->val.
3979 static int constraint_equal(const void *entry
, const void *val
)
3981 isl_int
**row
= (isl_int
**)entry
;
3982 const struct isl_constraint_equal_info
*info
= val
;
3984 return isl_seq_eq((*row
) + 1 + info
->n_in
, info
->val
, info
->n_out
);
3987 /* Check whether "bmap" has a pair of constraints that have
3988 * the same coefficients for the output variables.
3989 * Note that the coefficients of the existentially quantified
3990 * variables need to be zero since the existentially quantified
3991 * of the result are usually not the same as those of the input.
3992 * the isl_dim_out and isl_dim_div dimensions.
3993 * If so, return 1 and return the row indices of the two constraints
3994 * in *first and *second.
3996 static int parallel_constraints(__isl_keep isl_basic_map
*bmap
,
3997 int *first
, int *second
)
4000 isl_ctx
*ctx
= isl_basic_map_get_ctx(bmap
);
4001 struct isl_hash_table
*table
= NULL
;
4002 struct isl_hash_table_entry
*entry
;
4003 struct isl_constraint_equal_info info
;
4007 ctx
= isl_basic_map_get_ctx(bmap
);
4008 table
= isl_hash_table_alloc(ctx
, bmap
->n_ineq
);
4012 info
.n_in
= isl_basic_map_dim(bmap
, isl_dim_param
) +
4013 isl_basic_map_dim(bmap
, isl_dim_in
);
4015 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4016 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4017 info
.n_out
= n_out
+ n_div
;
4018 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
4021 info
.val
= bmap
->ineq
[i
] + 1 + info
.n_in
;
4022 if (isl_seq_first_non_zero(info
.val
, n_out
) < 0)
4024 if (isl_seq_first_non_zero(info
.val
+ n_out
, n_div
) >= 0)
4026 hash
= isl_seq_get_hash(info
.val
, info
.n_out
);
4027 entry
= isl_hash_table_find(ctx
, table
, hash
,
4028 constraint_equal
, &info
, 1);
4033 entry
->data
= &bmap
->ineq
[i
];
4036 if (i
< bmap
->n_ineq
) {
4037 *first
= ((isl_int
**)entry
->data
) - bmap
->ineq
;
4041 isl_hash_table_free(ctx
, table
);
4043 return i
< bmap
->n_ineq
;
4045 isl_hash_table_free(ctx
, table
);
4049 /* Given a set of upper bounds on the last "input" variable m,
4050 * construct a set that assigns the minimal upper bound to m, i.e.,
4051 * construct a set that divides the space into cells where one
4052 * of the upper bounds is smaller than all the others and assign
4053 * this upper bound to m.
4055 * In particular, if there are n bounds b_i, then the result
4056 * consists of n basic sets, each one of the form
4059 * b_i <= b_j for j > i
4060 * b_i < b_j for j < i
4062 static __isl_give isl_set
*set_minimum(__isl_take isl_dim
*dim
,
4063 __isl_take isl_mat
*var
)
4066 isl_basic_set
*bset
= NULL
;
4068 isl_set
*set
= NULL
;
4073 ctx
= isl_dim_get_ctx(dim
);
4074 set
= isl_set_alloc_dim(isl_dim_copy(dim
),
4075 var
->n_row
, ISL_SET_DISJOINT
);
4077 for (i
= 0; i
< var
->n_row
; ++i
) {
4078 bset
= isl_basic_set_alloc_dim(isl_dim_copy(dim
), 0,
4080 k
= isl_basic_set_alloc_equality(bset
);
4083 isl_seq_cpy(bset
->eq
[k
], var
->row
[i
], var
->n_col
);
4084 isl_int_set_si(bset
->eq
[k
][var
->n_col
], -1);
4085 for (j
= 0; j
< var
->n_row
; ++j
) {
4088 k
= isl_basic_set_alloc_inequality(bset
);
4091 isl_seq_combine(bset
->ineq
[k
], ctx
->one
, var
->row
[j
],
4092 ctx
->negone
, var
->row
[i
],
4094 isl_int_set_si(bset
->ineq
[k
][var
->n_col
], 0);
4096 isl_int_sub_ui(bset
->ineq
[k
][0],
4097 bset
->ineq
[k
][0], 1);
4099 bset
= isl_basic_set_finalize(bset
);
4100 set
= isl_set_add_basic_set(set
, bset
);
4107 isl_basic_set_free(bset
);
4114 /* Given that the last input variable of "bmap" represents the minimum
4115 * of the bounds in "cst", check whether we need to split the domain
4116 * based on which bound attains the minimum.
4118 * A split is needed when the minimum appears in an integer division
4119 * or in an equality. Otherwise, it is only needed if it appears in
4120 * an upper bound that is different from the upper bounds on which it
4123 static int need_split_map(__isl_keep isl_basic_map
*bmap
,
4124 __isl_keep isl_mat
*cst
)
4130 pos
= cst
->n_col
- 1;
4131 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4133 for (i
= 0; i
< bmap
->n_div
; ++i
)
4134 if (!isl_int_is_zero(bmap
->div
[i
][2 + pos
]))
4137 for (i
= 0; i
< bmap
->n_eq
; ++i
)
4138 if (!isl_int_is_zero(bmap
->eq
[i
][1 + pos
]))
4141 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
4142 if (isl_int_is_nonneg(bmap
->ineq
[i
][1 + pos
]))
4144 if (!isl_int_is_negone(bmap
->ineq
[i
][1 + pos
]))
4146 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + pos
+ 1,
4147 total
- pos
- 1) >= 0)
4150 for (j
= 0; j
< cst
->n_row
; ++j
)
4151 if (isl_seq_eq(bmap
->ineq
[i
], cst
->row
[j
], cst
->n_col
))
4153 if (j
>= cst
->n_row
)
4160 static int need_split_set(__isl_keep isl_basic_set
*bset
,
4161 __isl_keep isl_mat
*cst
)
4163 return need_split_map((isl_basic_map
*)bset
, cst
);
4166 /* Given a set of which the last set variable is the minimum
4167 * of the bounds in "cst", split each basic set in the set
4168 * in pieces where one of the bounds is (strictly) smaller than the others.
4169 * This subdivision is given in "min_expr".
4170 * The variable is subsequently projected out.
4172 * We only do the split when it is needed.
4173 * For example if the last input variable m = min(a,b) and the only
4174 * constraints in the given basic set are lower bounds on m,
4175 * i.e., l <= m = min(a,b), then we can simply project out m
4176 * to obtain l <= a and l <= b, without having to split on whether
4177 * m is equal to a or b.
4179 static __isl_give isl_set
*split(__isl_take isl_set
*empty
,
4180 __isl_take isl_set
*min_expr
, __isl_take isl_mat
*cst
)
4187 if (!empty
|| !min_expr
|| !cst
)
4190 n_in
= isl_set_dim(empty
, isl_dim_set
);
4191 dim
= isl_set_get_dim(empty
);
4192 dim
= isl_dim_drop(dim
, isl_dim_set
, n_in
- 1, 1);
4193 res
= isl_set_empty(dim
);
4195 for (i
= 0; i
< empty
->n
; ++i
) {
4198 set
= isl_set_from_basic_set(isl_basic_set_copy(empty
->p
[i
]));
4199 if (need_split_set(empty
->p
[i
], cst
))
4200 set
= isl_set_intersect(set
, isl_set_copy(min_expr
));
4201 set
= isl_set_remove_dims(set
, isl_dim_set
, n_in
- 1, 1);
4203 res
= isl_set_union_disjoint(res
, set
);
4206 isl_set_free(empty
);
4207 isl_set_free(min_expr
);
4211 isl_set_free(empty
);
4212 isl_set_free(min_expr
);
4217 /* Given a map of which the last input variable is the minimum
4218 * of the bounds in "cst", split each basic set in the set
4219 * in pieces where one of the bounds is (strictly) smaller than the others.
4220 * This subdivision is given in "min_expr".
4221 * The variable is subsequently projected out.
4223 * The implementation is essentially the same as that of "split".
4225 static __isl_give isl_map
*split_domain(__isl_take isl_map
*opt
,
4226 __isl_take isl_set
*min_expr
, __isl_take isl_mat
*cst
)
4233 if (!opt
|| !min_expr
|| !cst
)
4236 n_in
= isl_map_dim(opt
, isl_dim_in
);
4237 dim
= isl_map_get_dim(opt
);
4238 dim
= isl_dim_drop(dim
, isl_dim_in
, n_in
- 1, 1);
4239 res
= isl_map_empty(dim
);
4241 for (i
= 0; i
< opt
->n
; ++i
) {
4244 map
= isl_map_from_basic_map(isl_basic_map_copy(opt
->p
[i
]));
4245 if (need_split_map(opt
->p
[i
], cst
))
4246 map
= isl_map_intersect_domain(map
,
4247 isl_set_copy(min_expr
));
4248 map
= isl_map_remove_dims(map
, isl_dim_in
, n_in
- 1, 1);
4250 res
= isl_map_union_disjoint(res
, map
);
4254 isl_set_free(min_expr
);
4259 isl_set_free(min_expr
);
4264 static __isl_give isl_map
*basic_map_partial_lexopt(
4265 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
4266 __isl_give isl_set
**empty
, int max
);
4268 /* Given a basic map with at least two parallel constraints (as found
4269 * by the function parallel_constraints), first look for more constraints
4270 * parallel to the two constraint and replace the found list of parallel
4271 * constraints by a single constraint with as "input" part the minimum
4272 * of the input parts of the list of constraints. Then, recursively call
4273 * basic_map_partial_lexopt (possibly finding more parallel constraints)
4274 * and plug in the definition of the minimum in the result.
4276 * More specifically, given a set of constraints
4280 * Replace this set by a single constraint
4284 * with u a new parameter with constraints
4288 * Any solution to the new system is also a solution for the original system
4291 * a x >= -u >= -b_i(p)
4293 * Moreover, m = min_i(b_i(p)) satisfies the constraints on u and can
4294 * therefore be plugged into the solution.
4296 static __isl_give isl_map
*basic_map_partial_lexopt_symm(
4297 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
4298 __isl_give isl_set
**empty
, int max
, int first
, int second
)
4302 unsigned n_in
, n_out
, n_div
;
4304 isl_vec
*var
= NULL
;
4305 isl_mat
*cst
= NULL
;
4308 isl_dim
*map_dim
, *set_dim
;
4310 map_dim
= isl_basic_map_get_dim(bmap
);
4311 set_dim
= empty
? isl_basic_set_get_dim(dom
) : NULL
;
4313 n_in
= isl_basic_map_dim(bmap
, isl_dim_param
) +
4314 isl_basic_map_dim(bmap
, isl_dim_in
);
4315 n_out
= isl_basic_map_dim(bmap
, isl_dim_all
) - n_in
;
4317 ctx
= isl_basic_map_get_ctx(bmap
);
4318 list
= isl_alloc_array(ctx
, int, bmap
->n_ineq
);
4319 var
= isl_vec_alloc(ctx
, n_out
);
4325 isl_seq_cpy(var
->el
, bmap
->ineq
[first
] + 1 + n_in
, n_out
);
4326 for (i
= second
+ 1, n
= 2; i
< bmap
->n_ineq
; ++i
) {
4327 if (isl_seq_eq(var
->el
, bmap
->ineq
[i
] + 1 + n_in
, n_out
))
4331 cst
= isl_mat_alloc(ctx
, n
, 1 + n_in
);
4335 for (i
= 0; i
< n
; ++i
)
4336 isl_seq_cpy(cst
->row
[i
], bmap
->ineq
[list
[i
]], 1 + n_in
);
4338 bmap
= isl_basic_map_cow(bmap
);
4341 for (i
= n
- 1; i
>= 0; --i
)
4342 if (isl_basic_map_drop_inequality(bmap
, list
[i
]) < 0)
4345 bmap
= isl_basic_map_add(bmap
, isl_dim_in
, 1);
4346 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
4347 k
= isl_basic_map_alloc_inequality(bmap
);
4350 isl_seq_clr(bmap
->ineq
[k
], 1 + n_in
);
4351 isl_int_set_si(bmap
->ineq
[k
][1 + n_in
], 1);
4352 isl_seq_cpy(bmap
->ineq
[k
] + 1 + n_in
+ 1, var
->el
, n_out
);
4353 bmap
= isl_basic_map_finalize(bmap
);
4355 n_div
= isl_basic_set_dim(dom
, isl_dim_div
);
4356 dom
= isl_basic_set_add(dom
, isl_dim_set
, 1);
4357 dom
= isl_basic_set_extend_constraints(dom
, 0, n
);
4358 for (i
= 0; i
< n
; ++i
) {
4359 k
= isl_basic_set_alloc_inequality(dom
);
4362 isl_seq_cpy(dom
->ineq
[k
], cst
->row
[i
], 1 + n_in
);
4363 isl_int_set_si(dom
->ineq
[k
][1 + n_in
], -1);
4364 isl_seq_clr(dom
->ineq
[k
] + 1 + n_in
+ 1, n_div
);
4367 min_expr
= set_minimum(isl_basic_set_get_dim(dom
), isl_mat_copy(cst
));
4372 opt
= basic_map_partial_lexopt(bmap
, dom
, empty
, max
);
4375 *empty
= split(*empty
,
4376 isl_set_copy(min_expr
), isl_mat_copy(cst
));
4377 *empty
= isl_set_reset_dim(*empty
, set_dim
);
4380 opt
= split_domain(opt
, min_expr
, cst
);
4381 opt
= isl_map_reset_dim(opt
, map_dim
);
4385 isl_dim_free(map_dim
);
4386 isl_dim_free(set_dim
);
4390 isl_basic_set_free(dom
);
4391 isl_basic_map_free(bmap
);
4395 /* Recursive part of isl_tab_basic_map_partial_lexopt, after detecting
4396 * equalities and removing redundant constraints.
4398 * We first check if there are any parallel constraints (left).
4399 * If not, we are in the base case.
4400 * If there are parallel constraints, we replace them by a single
4401 * constraint in basic_map_partial_lexopt_symm and then call
4402 * this function recursively to look for more parallel constraints.
4404 static __isl_give isl_map
*basic_map_partial_lexopt(
4405 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
4406 __isl_give isl_set
**empty
, int max
)
4414 if (bmap
->ctx
->opt
->pip_symmetry
)
4415 par
= parallel_constraints(bmap
, &first
, &second
);
4419 return basic_map_partial_lexopt_base(bmap
, dom
, empty
, max
);
4421 return basic_map_partial_lexopt_symm(bmap
, dom
, empty
, max
,
4424 isl_basic_set_free(dom
);
4425 isl_basic_map_free(bmap
);
4429 /* Compute the lexicographic minimum (or maximum if "max" is set)
4430 * of "bmap" over the domain "dom" and return the result as a map.
4431 * If "empty" is not NULL, then *empty is assigned a set that
4432 * contains those parts of the domain where there is no solution.
4433 * If "bmap" is marked as rational (ISL_BASIC_MAP_RATIONAL),
4434 * then we compute the rational optimum. Otherwise, we compute
4435 * the integral optimum.
4437 * We perform some preprocessing. As the PILP solver does not
4438 * handle implicit equalities very well, we first make sure all
4439 * the equalities are explicitly available.
4441 * We also add context constraints to the basic map and remove
4442 * redundant constraints. This is only needed because of the
4443 * way we handle simple symmetries. In particular, we currently look
4444 * for symmetries on the constraints, before we set up the main tableau.
4445 * It is then no good to look for symmetries on possibly redundant constraints.
4447 struct isl_map
*isl_tab_basic_map_partial_lexopt(
4448 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
4449 struct isl_set
**empty
, int max
)
4456 isl_assert(bmap
->ctx
,
4457 isl_basic_map_compatible_domain(bmap
, dom
), goto error
);
4459 if (isl_basic_set_dim(dom
, isl_dim_all
) == 0)
4460 return basic_map_partial_lexopt(bmap
, dom
, empty
, max
);
4462 bmap
= isl_basic_map_intersect_domain(bmap
, isl_basic_set_copy(dom
));
4463 bmap
= isl_basic_map_detect_equalities(bmap
);
4464 bmap
= isl_basic_map_remove_redundancies(bmap
);
4466 return basic_map_partial_lexopt(bmap
, dom
, empty
, max
);
4468 isl_basic_set_free(dom
);
4469 isl_basic_map_free(bmap
);
4473 struct isl_sol_for
{
4475 int (*fn
)(__isl_take isl_basic_set
*dom
,
4476 __isl_take isl_mat
*map
, void *user
);
4480 static void sol_for_free(struct isl_sol_for
*sol_for
)
4482 if (sol_for
->sol
.context
)
4483 sol_for
->sol
.context
->op
->free(sol_for
->sol
.context
);
4487 static void sol_for_free_wrap(struct isl_sol
*sol
)
4489 sol_for_free((struct isl_sol_for
*)sol
);
4492 /* Add the solution identified by the tableau and the context tableau.
4494 * See documentation of sol_add for more details.
4496 * Instead of constructing a basic map, this function calls a user
4497 * defined function with the current context as a basic set and
4498 * an affine matrix representing the relation between the input and output.
4499 * The number of rows in this matrix is equal to one plus the number
4500 * of output variables. The number of columns is equal to one plus
4501 * the total dimension of the context, i.e., the number of parameters,
4502 * input variables and divs. Since some of the columns in the matrix
4503 * may refer to the divs, the basic set is not simplified.
4504 * (Simplification may reorder or remove divs.)
4506 static void sol_for_add(struct isl_sol_for
*sol
,
4507 struct isl_basic_set
*dom
, struct isl_mat
*M
)
4509 if (sol
->sol
.error
|| !dom
|| !M
)
4512 dom
= isl_basic_set_simplify(dom
);
4513 dom
= isl_basic_set_finalize(dom
);
4515 if (sol
->fn(isl_basic_set_copy(dom
), isl_mat_copy(M
), sol
->user
) < 0)
4518 isl_basic_set_free(dom
);
4522 isl_basic_set_free(dom
);
4527 static void sol_for_add_wrap(struct isl_sol
*sol
,
4528 struct isl_basic_set
*dom
, struct isl_mat
*M
)
4530 sol_for_add((struct isl_sol_for
*)sol
, dom
, M
);
4533 static struct isl_sol_for
*sol_for_init(struct isl_basic_map
*bmap
, int max
,
4534 int (*fn
)(__isl_take isl_basic_set
*dom
, __isl_take isl_mat
*map
,
4538 struct isl_sol_for
*sol_for
= NULL
;
4539 struct isl_dim
*dom_dim
;
4540 struct isl_basic_set
*dom
= NULL
;
4542 sol_for
= isl_calloc_type(bmap
->ctx
, struct isl_sol_for
);
4546 dom_dim
= isl_dim_domain(isl_dim_copy(bmap
->dim
));
4547 dom
= isl_basic_set_universe(dom_dim
);
4549 sol_for
->sol
.rational
= ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
4550 sol_for
->sol
.dec_level
.callback
.run
= &sol_dec_level_wrap
;
4551 sol_for
->sol
.dec_level
.sol
= &sol_for
->sol
;
4553 sol_for
->user
= user
;
4554 sol_for
->sol
.max
= max
;
4555 sol_for
->sol
.n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4556 sol_for
->sol
.add
= &sol_for_add_wrap
;
4557 sol_for
->sol
.add_empty
= NULL
;
4558 sol_for
->sol
.free
= &sol_for_free_wrap
;
4560 sol_for
->sol
.context
= isl_context_alloc(dom
);
4561 if (!sol_for
->sol
.context
)
4564 isl_basic_set_free(dom
);
4567 isl_basic_set_free(dom
);
4568 sol_for_free(sol_for
);
4572 static void sol_for_find_solutions(struct isl_sol_for
*sol_for
,
4573 struct isl_tab
*tab
)
4575 find_solutions_main(&sol_for
->sol
, tab
);
4578 int isl_basic_map_foreach_lexopt(__isl_keep isl_basic_map
*bmap
, int max
,
4579 int (*fn
)(__isl_take isl_basic_set
*dom
, __isl_take isl_mat
*map
,
4583 struct isl_sol_for
*sol_for
= NULL
;
4585 bmap
= isl_basic_map_copy(bmap
);
4589 bmap
= isl_basic_map_detect_equalities(bmap
);
4590 sol_for
= sol_for_init(bmap
, max
, fn
, user
);
4592 if (isl_basic_map_fast_is_empty(bmap
))
4595 struct isl_tab
*tab
;
4596 struct isl_context
*context
= sol_for
->sol
.context
;
4597 tab
= tab_for_lexmin(bmap
,
4598 context
->op
->peek_basic_set(context
), 1, max
);
4599 tab
= context
->op
->detect_nonnegative_parameters(context
, tab
);
4600 sol_for_find_solutions(sol_for
, tab
);
4601 if (sol_for
->sol
.error
)
4605 sol_free(&sol_for
->sol
);
4606 isl_basic_map_free(bmap
);
4609 sol_free(&sol_for
->sol
);
4610 isl_basic_map_free(bmap
);
4614 int isl_basic_map_foreach_lexmin(__isl_keep isl_basic_map
*bmap
,
4615 int (*fn
)(__isl_take isl_basic_set
*dom
, __isl_take isl_mat
*map
,
4619 return isl_basic_map_foreach_lexopt(bmap
, 0, fn
, user
);
4622 int isl_basic_map_foreach_lexmax(__isl_keep isl_basic_map
*bmap
,
4623 int (*fn
)(__isl_take isl_basic_set
*dom
, __isl_take isl_mat
*map
,
4627 return isl_basic_map_foreach_lexopt(bmap
, 1, fn
, user
);