2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2016 Sven Verdoolaege
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, K.U.Leuven, Departement
9 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
11 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
14 #include <isl_ctx_private.h>
15 #include "isl_map_private.h"
18 #include "isl_sample.h"
19 #include <isl_mat_private.h>
20 #include <isl_vec_private.h>
21 #include <isl_aff_private.h>
22 #include <isl_constraint_private.h>
23 #include <isl_options_private.h>
24 #include <isl_config.h>
26 #include <bset_to_bmap.c>
29 * The implementation of parametric integer linear programming in this file
30 * was inspired by the paper "Parametric Integer Programming" and the
31 * report "Solving systems of affine (in)equalities" by Paul Feautrier
34 * The strategy used for obtaining a feasible solution is different
35 * from the one used in isl_tab.c. In particular, in isl_tab.c,
36 * upon finding a constraint that is not yet satisfied, we pivot
37 * in a row that increases the constant term of the row holding the
38 * constraint, making sure the sample solution remains feasible
39 * for all the constraints it already satisfied.
40 * Here, we always pivot in the row holding the constraint,
41 * choosing a column that induces the lexicographically smallest
42 * increment to the sample solution.
44 * By starting out from a sample value that is lexicographically
45 * smaller than any integer point in the problem space, the first
46 * feasible integer sample point we find will also be the lexicographically
47 * smallest. If all variables can be assumed to be non-negative,
48 * then the initial sample value may be chosen equal to zero.
49 * However, we will not make this assumption. Instead, we apply
50 * the "big parameter" trick. Any variable x is then not directly
51 * used in the tableau, but instead it is represented by another
52 * variable x' = M + x, where M is an arbitrarily large (positive)
53 * value. x' is therefore always non-negative, whatever the value of x.
54 * Taking as initial sample value x' = 0 corresponds to x = -M,
55 * which is always smaller than any possible value of x.
57 * The big parameter trick is used in the main tableau and
58 * also in the context tableau if isl_context_lex is used.
59 * In this case, each tableaus has its own big parameter.
60 * Before doing any real work, we check if all the parameters
61 * happen to be non-negative. If so, we drop the column corresponding
62 * to M from the initial context tableau.
63 * If isl_context_gbr is used, then the big parameter trick is only
64 * used in the main tableau.
68 struct isl_context_op
{
69 /* detect nonnegative parameters in context and mark them in tab */
70 struct isl_tab
*(*detect_nonnegative_parameters
)(
71 struct isl_context
*context
, struct isl_tab
*tab
);
72 /* return temporary reference to basic set representation of context */
73 struct isl_basic_set
*(*peek_basic_set
)(struct isl_context
*context
);
74 /* return temporary reference to tableau representation of context */
75 struct isl_tab
*(*peek_tab
)(struct isl_context
*context
);
76 /* add equality; check is 1 if eq may not be valid;
77 * update is 1 if we may want to call ineq_sign on context later.
79 void (*add_eq
)(struct isl_context
*context
, isl_int
*eq
,
80 int check
, int update
);
81 /* add inequality; check is 1 if ineq may not be valid;
82 * update is 1 if we may want to call ineq_sign on context later.
84 void (*add_ineq
)(struct isl_context
*context
, isl_int
*ineq
,
85 int check
, int update
);
86 /* check sign of ineq based on previous information.
87 * strict is 1 if saturation should be treated as a positive sign.
89 enum isl_tab_row_sign (*ineq_sign
)(struct isl_context
*context
,
90 isl_int
*ineq
, int strict
);
91 /* check if inequality maintains feasibility */
92 int (*test_ineq
)(struct isl_context
*context
, isl_int
*ineq
);
93 /* return index of a div that corresponds to "div" */
94 int (*get_div
)(struct isl_context
*context
, struct isl_tab
*tab
,
96 /* insert div "div" to context at "pos" and return non-negativity */
97 isl_bool (*insert_div
)(struct isl_context
*context
, int pos
,
98 __isl_keep isl_vec
*div
);
99 int (*detect_equalities
)(struct isl_context
*context
,
100 struct isl_tab
*tab
);
101 /* return row index of "best" split */
102 int (*best_split
)(struct isl_context
*context
, struct isl_tab
*tab
);
103 /* check if context has already been determined to be empty */
104 int (*is_empty
)(struct isl_context
*context
);
105 /* check if context is still usable */
106 int (*is_ok
)(struct isl_context
*context
);
107 /* save a copy/snapshot of context */
108 void *(*save
)(struct isl_context
*context
);
109 /* restore saved context */
110 void (*restore
)(struct isl_context
*context
, void *);
111 /* discard saved context */
112 void (*discard
)(void *);
113 /* invalidate context */
114 void (*invalidate
)(struct isl_context
*context
);
116 __isl_null
struct isl_context
*(*free
)(struct isl_context
*context
);
119 /* Shared parts of context representation.
121 * "n_unknown" is the number of final unknown integer divisions
122 * in the input domain.
125 struct isl_context_op
*op
;
129 struct isl_context_lex
{
130 struct isl_context context
;
134 /* A stack (linked list) of solutions of subtrees of the search space.
136 * "ma" describes the solution as a function of "dom".
137 * In particular, the domain space of "ma" is equal to the space of "dom".
139 * If "ma" is NULL, then there is no solution on "dom".
141 struct isl_partial_sol
{
143 struct isl_basic_set
*dom
;
146 struct isl_partial_sol
*next
;
150 struct isl_sol_callback
{
151 struct isl_tab_callback callback
;
155 /* isl_sol is an interface for constructing a solution to
156 * a parametric integer linear programming problem.
157 * Every time the algorithm reaches a state where a solution
158 * can be read off from the tableau, the function "add" is called
159 * on the isl_sol passed to find_solutions_main. In a state where
160 * the tableau is empty, "add_empty" is called instead.
161 * "free" is called to free the implementation specific fields, if any.
163 * "error" is set if some error has occurred. This flag invalidates
164 * the remainder of the data structure.
165 * If "rational" is set, then a rational optimization is being performed.
166 * "level" is the current level in the tree with nodes for each
167 * split in the context.
168 * If "max" is set, then a maximization problem is being solved, rather than
169 * a minimization problem, which means that the variables in the
170 * tableau have value "M - x" rather than "M + x".
171 * "n_out" is the number of output dimensions in the input.
172 * "space" is the space in which the solution (and also the input) lives.
174 * The context tableau is owned by isl_sol and is updated incrementally.
176 * There are currently three implementations of this interface,
177 * isl_sol_map, which simply collects the solutions in an isl_map
178 * and (optionally) the parts of the context where there is no solution
180 * isl_sol_pma, which collects an isl_pw_multi_aff instead, and
181 * isl_sol_for, which calls a user-defined function for each part of
191 struct isl_context
*context
;
192 struct isl_partial_sol
*partial
;
193 void (*add
)(struct isl_sol
*sol
,
194 __isl_take isl_basic_set
*dom
, __isl_take isl_multi_aff
*ma
);
195 void (*add_empty
)(struct isl_sol
*sol
, struct isl_basic_set
*bset
);
196 void (*free
)(struct isl_sol
*sol
);
197 struct isl_sol_callback dec_level
;
200 static void sol_free(struct isl_sol
*sol
)
202 struct isl_partial_sol
*partial
, *next
;
205 for (partial
= sol
->partial
; partial
; partial
= next
) {
206 next
= partial
->next
;
207 isl_basic_set_free(partial
->dom
);
208 isl_multi_aff_free(partial
->ma
);
211 isl_space_free(sol
->space
);
213 sol
->context
->op
->free(sol
->context
);
218 /* Push a partial solution represented by a domain and function "ma"
219 * onto the stack of partial solutions.
220 * If "ma" is NULL, then "dom" represents a part of the domain
223 static void sol_push_sol(struct isl_sol
*sol
,
224 __isl_take isl_basic_set
*dom
, __isl_take isl_multi_aff
*ma
)
226 struct isl_partial_sol
*partial
;
228 if (sol
->error
|| !dom
)
231 partial
= isl_alloc_type(dom
->ctx
, struct isl_partial_sol
);
235 partial
->level
= sol
->level
;
238 partial
->next
= sol
->partial
;
240 sol
->partial
= partial
;
244 isl_basic_set_free(dom
);
245 isl_multi_aff_free(ma
);
249 /* Check that the final columns of "M", starting at "first", are zero.
251 static isl_stat
check_final_columns_are_zero(__isl_keep isl_mat
*M
,
255 unsigned rows
, cols
, n
;
258 return isl_stat_error
;
259 rows
= isl_mat_rows(M
);
260 cols
= isl_mat_cols(M
);
262 for (i
= 0; i
< rows
; ++i
)
263 if (isl_seq_first_non_zero(M
->row
[i
] + first
, n
) != -1)
264 isl_die(isl_mat_get_ctx(M
), isl_error_internal
,
265 "final columns should be zero",
266 return isl_stat_error
);
270 /* Set the affine expressions in "ma" according to the rows in "M", which
271 * are defined over the local space "ls".
272 * The matrix "M" may have extra (zero) columns beyond the number
273 * of variables in "ls".
275 static __isl_give isl_multi_aff
*set_from_affine_matrix(
276 __isl_take isl_multi_aff
*ma
, __isl_take isl_local_space
*ls
,
277 __isl_take isl_mat
*M
)
282 if (!ma
|| !ls
|| !M
)
285 dim
= isl_local_space_dim(ls
, isl_dim_all
);
286 if (check_final_columns_are_zero(M
, 1 + dim
) < 0)
288 for (i
= 1; i
< M
->n_row
; ++i
) {
289 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
291 isl_int_set(aff
->v
->el
[0], M
->row
[0][0]);
292 isl_seq_cpy(aff
->v
->el
+ 1, M
->row
[i
], 1 + dim
);
294 aff
= isl_aff_normalize(aff
);
295 ma
= isl_multi_aff_set_aff(ma
, i
- 1, aff
);
297 isl_local_space_free(ls
);
302 isl_local_space_free(ls
);
304 isl_multi_aff_free(ma
);
308 /* Push a partial solution represented by a domain and mapping M
309 * onto the stack of partial solutions.
311 * The affine matrix "M" maps the dimensions of the context
312 * to the output variables. Convert it into an isl_multi_aff and
313 * then call sol_push_sol.
315 * Note that the description of the initial context may have involved
316 * existentially quantified variables, in which case they also appear
317 * in "dom". These need to be removed before creating the affine
318 * expression because an affine expression cannot be defined in terms
319 * of existentially quantified variables without a known representation.
320 * Since newly added integer divisions are inserted before these
321 * existentially quantified variables, they are still in the final
322 * positions and the corresponding final columns of "M" are zero
323 * because align_context_divs adds the existentially quantified
324 * variables of the context to the main tableau without any constraints and
325 * any equality constraints that are added later on can only serve
326 * to eliminate these existentially quantified variables.
328 static void sol_push_sol_mat(struct isl_sol
*sol
,
329 __isl_take isl_basic_set
*dom
, __isl_take isl_mat
*M
)
335 n_div
= isl_basic_set_dim(dom
, isl_dim_div
);
336 n_known
= n_div
- sol
->context
->n_unknown
;
338 ma
= isl_multi_aff_alloc(isl_space_copy(sol
->space
));
339 ls
= isl_basic_set_get_local_space(dom
);
340 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
,
341 n_known
, n_div
- n_known
);
342 ma
= set_from_affine_matrix(ma
, ls
, M
);
345 dom
= isl_basic_set_free(dom
);
346 sol_push_sol(sol
, dom
, ma
);
349 /* Pop one partial solution from the partial solution stack and
350 * pass it on to sol->add or sol->add_empty.
352 static void sol_pop_one(struct isl_sol
*sol
)
354 struct isl_partial_sol
*partial
;
356 partial
= sol
->partial
;
357 sol
->partial
= partial
->next
;
360 sol
->add(sol
, partial
->dom
, partial
->ma
);
362 sol
->add_empty(sol
, partial
->dom
);
366 /* Return a fresh copy of the domain represented by the context tableau.
368 static struct isl_basic_set
*sol_domain(struct isl_sol
*sol
)
370 struct isl_basic_set
*bset
;
375 bset
= isl_basic_set_dup(sol
->context
->op
->peek_basic_set(sol
->context
));
376 bset
= isl_basic_set_update_from_tab(bset
,
377 sol
->context
->op
->peek_tab(sol
->context
));
382 /* Check whether two partial solutions have the same affine expressions.
384 static isl_bool
same_solution(struct isl_partial_sol
*s1
,
385 struct isl_partial_sol
*s2
)
387 if (!s1
->ma
!= !s2
->ma
)
388 return isl_bool_false
;
390 return isl_bool_true
;
392 return isl_multi_aff_plain_is_equal(s1
->ma
, s2
->ma
);
395 /* Swap the initial two partial solutions in "sol".
399 * sol->partial = p1; p1->next = p2; p2->next = p3
403 * sol->partial = p2; p2->next = p1; p1->next = p3
405 static void swap_initial(struct isl_sol
*sol
)
407 struct isl_partial_sol
*partial
;
409 partial
= sol
->partial
;
410 sol
->partial
= partial
->next
;
411 partial
->next
= partial
->next
->next
;
412 sol
->partial
->next
= partial
;
415 /* Combine the initial two partial solution of "sol" into
416 * a partial solution with the current context domain of "sol" and
417 * the function description of the second partial solution in the list.
418 * The level of the new partial solution is set to the current level.
420 * That is, the first two partial solutions (D1,M1) and (D2,M2) are
421 * replaced by (D,M2), where D is the domain of "sol", which is assumed
422 * to be the union of D1 and D2, while M1 is assumed to be equal to M2
425 static isl_stat
combine_initial_into_second(struct isl_sol
*sol
)
427 struct isl_partial_sol
*partial
;
430 partial
= sol
->partial
;
432 bset
= sol_domain(sol
);
433 isl_basic_set_free(partial
->next
->dom
);
434 partial
->next
->dom
= bset
;
435 partial
->next
->level
= sol
->level
;
438 return isl_stat_error
;
440 sol
->partial
= partial
->next
;
441 isl_basic_set_free(partial
->dom
);
442 isl_multi_aff_free(partial
->ma
);
448 /* Are "ma1" and "ma2" equal to each other on "dom"?
450 * Combine "ma1" and "ma2" with "dom" and check if the results are the same.
451 * "dom" may have existentially quantified variables. Eliminate them first
452 * as otherwise they would have to be eliminated twice, in a more complicated
455 static isl_bool
equal_on_domain(__isl_keep isl_multi_aff
*ma1
,
456 __isl_keep isl_multi_aff
*ma2
, __isl_keep isl_basic_set
*dom
)
459 isl_pw_multi_aff
*pma1
, *pma2
;
462 set
= isl_basic_set_compute_divs(isl_basic_set_copy(dom
));
463 pma1
= isl_pw_multi_aff_alloc(isl_set_copy(set
),
464 isl_multi_aff_copy(ma1
));
465 pma2
= isl_pw_multi_aff_alloc(set
, isl_multi_aff_copy(ma2
));
466 equal
= isl_pw_multi_aff_is_equal(pma1
, pma2
);
467 isl_pw_multi_aff_free(pma1
);
468 isl_pw_multi_aff_free(pma2
);
473 /* The initial two partial solutions of "sol" are known to be at
475 * If they represent the same solution (on different parts of the domain),
476 * then combine them into a single solution at the current level.
477 * Otherwise, pop them both.
479 * Even if the two partial solution are not obviously the same,
480 * one may still be a simplification of the other over its own domain.
481 * Also check if the two sets of affine functions are equal when
482 * restricted to one of the domains. If so, combine the two
483 * using the set of affine functions on the other domain.
484 * That is, for two partial solutions (D1,M1) and (D2,M2),
485 * if M1 = M2 on D1, then the pair of partial solutions can
486 * be replaced by (D1+D2,M2) and similarly when M1 = M2 on D2.
488 static isl_stat
combine_initial_if_equal(struct isl_sol
*sol
)
490 struct isl_partial_sol
*partial
;
493 partial
= sol
->partial
;
495 same
= same_solution(partial
, partial
->next
);
497 return isl_stat_error
;
499 return combine_initial_into_second(sol
);
500 if (partial
->ma
&& partial
->next
->ma
) {
501 same
= equal_on_domain(partial
->ma
, partial
->next
->ma
,
504 return isl_stat_error
;
506 return combine_initial_into_second(sol
);
507 same
= equal_on_domain(partial
->ma
, partial
->next
->ma
,
511 return combine_initial_into_second(sol
);
521 /* Pop all solutions from the partial solution stack that were pushed onto
522 * the stack at levels that are deeper than the current level.
523 * If the two topmost elements on the stack have the same level
524 * and represent the same solution, then their domains are combined.
525 * This combined domain is the same as the current context domain
526 * as sol_pop is called each time we move back to a higher level.
527 * If the outer level (0) has been reached, then all partial solutions
528 * at the current level are also popped off.
530 static void sol_pop(struct isl_sol
*sol
)
532 struct isl_partial_sol
*partial
;
537 partial
= sol
->partial
;
541 if (partial
->level
== 0 && sol
->level
== 0) {
542 for (partial
= sol
->partial
; partial
; partial
= sol
->partial
)
547 if (partial
->level
<= sol
->level
)
550 if (partial
->next
&& partial
->next
->level
== partial
->level
) {
551 if (combine_initial_if_equal(sol
) < 0)
556 if (sol
->level
== 0) {
557 for (partial
= sol
->partial
; partial
; partial
= sol
->partial
)
563 error
: sol
->error
= 1;
566 static void sol_dec_level(struct isl_sol
*sol
)
576 static isl_stat
sol_dec_level_wrap(struct isl_tab_callback
*cb
)
578 struct isl_sol_callback
*callback
= (struct isl_sol_callback
*)cb
;
580 sol_dec_level(callback
->sol
);
582 return callback
->sol
->error
? isl_stat_error
: isl_stat_ok
;
585 /* Move down to next level and push callback onto context tableau
586 * to decrease the level again when it gets rolled back across
587 * the current state. That is, dec_level will be called with
588 * the context tableau in the same state as it is when inc_level
591 static void sol_inc_level(struct isl_sol
*sol
)
599 tab
= sol
->context
->op
->peek_tab(sol
->context
);
600 if (isl_tab_push_callback(tab
, &sol
->dec_level
.callback
) < 0)
604 static void scale_rows(struct isl_mat
*mat
, isl_int m
, int n_row
)
608 if (isl_int_is_one(m
))
611 for (i
= 0; i
< n_row
; ++i
)
612 isl_seq_scale(mat
->row
[i
], mat
->row
[i
], m
, mat
->n_col
);
615 /* Add the solution identified by the tableau and the context tableau.
617 * The layout of the variables is as follows.
618 * tab->n_var is equal to the total number of variables in the input
619 * map (including divs that were copied from the context)
620 * + the number of extra divs constructed
621 * Of these, the first tab->n_param and the last tab->n_div variables
622 * correspond to the variables in the context, i.e.,
623 * tab->n_param + tab->n_div = context_tab->n_var
624 * tab->n_param is equal to the number of parameters and input
625 * dimensions in the input map
626 * tab->n_div is equal to the number of divs in the context
628 * If there is no solution, then call add_empty with a basic set
629 * that corresponds to the context tableau. (If add_empty is NULL,
632 * If there is a solution, then first construct a matrix that maps
633 * all dimensions of the context to the output variables, i.e.,
634 * the output dimensions in the input map.
635 * The divs in the input map (if any) that do not correspond to any
636 * div in the context do not appear in the solution.
637 * The algorithm will make sure that they have an integer value,
638 * but these values themselves are of no interest.
639 * We have to be careful not to drop or rearrange any divs in the
640 * context because that would change the meaning of the matrix.
642 * To extract the value of the output variables, it should be noted
643 * that we always use a big parameter M in the main tableau and so
644 * the variable stored in this tableau is not an output variable x itself, but
645 * x' = M + x (in case of minimization)
647 * x' = M - x (in case of maximization)
648 * If x' appears in a column, then its optimal value is zero,
649 * which means that the optimal value of x is an unbounded number
650 * (-M for minimization and M for maximization).
651 * We currently assume that the output dimensions in the original map
652 * are bounded, so this cannot occur.
653 * Similarly, when x' appears in a row, then the coefficient of M in that
654 * row is necessarily 1.
655 * If the row in the tableau represents
656 * d x' = c + d M + e(y)
657 * then, in case of minimization, the corresponding row in the matrix
660 * with a d = m, the (updated) common denominator of the matrix.
661 * In case of maximization, the row will be
664 static void sol_add(struct isl_sol
*sol
, struct isl_tab
*tab
)
666 struct isl_basic_set
*bset
= NULL
;
667 struct isl_mat
*mat
= NULL
;
672 if (sol
->error
|| !tab
)
675 if (tab
->empty
&& !sol
->add_empty
)
677 if (sol
->context
->op
->is_empty(sol
->context
))
680 bset
= sol_domain(sol
);
683 sol_push_sol(sol
, bset
, NULL
);
689 mat
= isl_mat_alloc(tab
->mat
->ctx
, 1 + sol
->n_out
,
690 1 + tab
->n_param
+ tab
->n_div
);
696 isl_seq_clr(mat
->row
[0] + 1, mat
->n_col
- 1);
697 isl_int_set_si(mat
->row
[0][0], 1);
698 for (row
= 0; row
< sol
->n_out
; ++row
) {
699 int i
= tab
->n_param
+ row
;
702 isl_seq_clr(mat
->row
[1 + row
], mat
->n_col
);
703 if (!tab
->var
[i
].is_row
) {
705 isl_die(mat
->ctx
, isl_error_invalid
,
706 "unbounded optimum", goto error2
);
710 r
= tab
->var
[i
].index
;
712 isl_int_ne(tab
->mat
->row
[r
][2], tab
->mat
->row
[r
][0]))
713 isl_die(mat
->ctx
, isl_error_invalid
,
714 "unbounded optimum", goto error2
);
715 isl_int_gcd(m
, mat
->row
[0][0], tab
->mat
->row
[r
][0]);
716 isl_int_divexact(m
, tab
->mat
->row
[r
][0], m
);
717 scale_rows(mat
, m
, 1 + row
);
718 isl_int_divexact(m
, mat
->row
[0][0], tab
->mat
->row
[r
][0]);
719 isl_int_mul(mat
->row
[1 + row
][0], m
, tab
->mat
->row
[r
][1]);
720 for (j
= 0; j
< tab
->n_param
; ++j
) {
722 if (tab
->var
[j
].is_row
)
724 col
= tab
->var
[j
].index
;
725 isl_int_mul(mat
->row
[1 + row
][1 + j
], m
,
726 tab
->mat
->row
[r
][off
+ col
]);
728 for (j
= 0; j
< tab
->n_div
; ++j
) {
730 if (tab
->var
[tab
->n_var
- tab
->n_div
+j
].is_row
)
732 col
= tab
->var
[tab
->n_var
- tab
->n_div
+j
].index
;
733 isl_int_mul(mat
->row
[1 + row
][1 + tab
->n_param
+ j
], m
,
734 tab
->mat
->row
[r
][off
+ col
]);
737 isl_seq_neg(mat
->row
[1 + row
], mat
->row
[1 + row
],
743 sol_push_sol_mat(sol
, bset
, mat
);
748 isl_basic_set_free(bset
);
756 struct isl_set
*empty
;
759 static void sol_map_free(struct isl_sol
*sol
)
761 struct isl_sol_map
*sol_map
= (struct isl_sol_map
*) sol
;
762 isl_map_free(sol_map
->map
);
763 isl_set_free(sol_map
->empty
);
766 /* This function is called for parts of the context where there is
767 * no solution, with "bset" corresponding to the context tableau.
768 * Simply add the basic set to the set "empty".
770 static void sol_map_add_empty(struct isl_sol_map
*sol
,
771 struct isl_basic_set
*bset
)
773 if (!bset
|| !sol
->empty
)
776 sol
->empty
= isl_set_grow(sol
->empty
, 1);
777 bset
= isl_basic_set_simplify(bset
);
778 bset
= isl_basic_set_finalize(bset
);
779 sol
->empty
= isl_set_add_basic_set(sol
->empty
, isl_basic_set_copy(bset
));
782 isl_basic_set_free(bset
);
785 isl_basic_set_free(bset
);
789 static void sol_map_add_empty_wrap(struct isl_sol
*sol
,
790 struct isl_basic_set
*bset
)
792 sol_map_add_empty((struct isl_sol_map
*)sol
, bset
);
795 /* Given a basic set "dom" that represents the context and a tuple of
796 * affine expressions "ma" defined over this domain, construct a basic map
797 * that expresses this function on the domain.
799 static void sol_map_add(struct isl_sol_map
*sol
,
800 __isl_take isl_basic_set
*dom
, __isl_take isl_multi_aff
*ma
)
804 if (sol
->sol
.error
|| !dom
|| !ma
)
807 bmap
= isl_basic_map_from_multi_aff2(ma
, sol
->sol
.rational
);
808 bmap
= isl_basic_map_intersect_domain(bmap
, dom
);
809 sol
->map
= isl_map_grow(sol
->map
, 1);
810 sol
->map
= isl_map_add_basic_map(sol
->map
, bmap
);
815 isl_basic_set_free(dom
);
816 isl_multi_aff_free(ma
);
820 static void sol_map_add_wrap(struct isl_sol
*sol
,
821 __isl_take isl_basic_set
*dom
, __isl_take isl_multi_aff
*ma
)
823 sol_map_add((struct isl_sol_map
*)sol
, dom
, ma
);
827 /* Store the "parametric constant" of row "row" of tableau "tab" in "line",
828 * i.e., the constant term and the coefficients of all variables that
829 * appear in the context tableau.
830 * Note that the coefficient of the big parameter M is NOT copied.
831 * The context tableau may not have a big parameter and even when it
832 * does, it is a different big parameter.
834 static void get_row_parameter_line(struct isl_tab
*tab
, int row
, isl_int
*line
)
837 unsigned off
= 2 + tab
->M
;
839 isl_int_set(line
[0], tab
->mat
->row
[row
][1]);
840 for (i
= 0; i
< tab
->n_param
; ++i
) {
841 if (tab
->var
[i
].is_row
)
842 isl_int_set_si(line
[1 + i
], 0);
844 int col
= tab
->var
[i
].index
;
845 isl_int_set(line
[1 + i
], tab
->mat
->row
[row
][off
+ col
]);
848 for (i
= 0; i
< tab
->n_div
; ++i
) {
849 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
850 isl_int_set_si(line
[1 + tab
->n_param
+ i
], 0);
852 int col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
853 isl_int_set(line
[1 + tab
->n_param
+ i
],
854 tab
->mat
->row
[row
][off
+ col
]);
859 /* Check if rows "row1" and "row2" have identical "parametric constants",
860 * as explained above.
861 * In this case, we also insist that the coefficients of the big parameter
862 * be the same as the values of the constants will only be the same
863 * if these coefficients are also the same.
865 static int identical_parameter_line(struct isl_tab
*tab
, int row1
, int row2
)
868 unsigned off
= 2 + tab
->M
;
870 if (isl_int_ne(tab
->mat
->row
[row1
][1], tab
->mat
->row
[row2
][1]))
873 if (tab
->M
&& isl_int_ne(tab
->mat
->row
[row1
][2],
874 tab
->mat
->row
[row2
][2]))
877 for (i
= 0; i
< tab
->n_param
+ tab
->n_div
; ++i
) {
878 int pos
= i
< tab
->n_param
? i
:
879 tab
->n_var
- tab
->n_div
+ i
- tab
->n_param
;
882 if (tab
->var
[pos
].is_row
)
884 col
= tab
->var
[pos
].index
;
885 if (isl_int_ne(tab
->mat
->row
[row1
][off
+ col
],
886 tab
->mat
->row
[row2
][off
+ col
]))
892 /* Return an inequality that expresses that the "parametric constant"
893 * should be non-negative.
894 * This function is only called when the coefficient of the big parameter
897 static struct isl_vec
*get_row_parameter_ineq(struct isl_tab
*tab
, int row
)
899 struct isl_vec
*ineq
;
901 ineq
= isl_vec_alloc(tab
->mat
->ctx
, 1 + tab
->n_param
+ tab
->n_div
);
905 get_row_parameter_line(tab
, row
, ineq
->el
);
907 ineq
= isl_vec_normalize(ineq
);
912 /* Normalize a div expression of the form
914 * [(g*f(x) + c)/(g * m)]
916 * with c the constant term and f(x) the remaining coefficients, to
920 static void normalize_div(__isl_keep isl_vec
*div
)
922 isl_ctx
*ctx
= isl_vec_get_ctx(div
);
923 int len
= div
->size
- 2;
925 isl_seq_gcd(div
->el
+ 2, len
, &ctx
->normalize_gcd
);
926 isl_int_gcd(ctx
->normalize_gcd
, ctx
->normalize_gcd
, div
->el
[0]);
928 if (isl_int_is_one(ctx
->normalize_gcd
))
931 isl_int_divexact(div
->el
[0], div
->el
[0], ctx
->normalize_gcd
);
932 isl_int_fdiv_q(div
->el
[1], div
->el
[1], ctx
->normalize_gcd
);
933 isl_seq_scale_down(div
->el
+ 2, div
->el
+ 2, ctx
->normalize_gcd
, len
);
936 /* Return an integer division for use in a parametric cut based
938 * In particular, let the parametric constant of the row be
942 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
943 * The div returned is equal to
945 * floor(\sum_i {-a_i} y_i) = floor((\sum_i (-a_i mod d) y_i)/d)
947 static struct isl_vec
*get_row_parameter_div(struct isl_tab
*tab
, int row
)
951 div
= isl_vec_alloc(tab
->mat
->ctx
, 1 + 1 + tab
->n_param
+ tab
->n_div
);
955 isl_int_set(div
->el
[0], tab
->mat
->row
[row
][0]);
956 get_row_parameter_line(tab
, row
, div
->el
+ 1);
957 isl_seq_neg(div
->el
+ 1, div
->el
+ 1, div
->size
- 1);
959 isl_seq_fdiv_r(div
->el
+ 1, div
->el
+ 1, div
->el
[0], div
->size
- 1);
964 /* Return an integer division for use in transferring an integrality constraint
966 * In particular, let the parametric constant of the row be
970 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
971 * The the returned div is equal to
973 * floor(\sum_i {a_i} y_i) = floor((\sum_i (a_i mod d) y_i)/d)
975 static struct isl_vec
*get_row_split_div(struct isl_tab
*tab
, int row
)
979 div
= isl_vec_alloc(tab
->mat
->ctx
, 1 + 1 + tab
->n_param
+ tab
->n_div
);
983 isl_int_set(div
->el
[0], tab
->mat
->row
[row
][0]);
984 get_row_parameter_line(tab
, row
, div
->el
+ 1);
986 isl_seq_fdiv_r(div
->el
+ 1, div
->el
+ 1, div
->el
[0], div
->size
- 1);
991 /* Construct and return an inequality that expresses an upper bound
993 * In particular, if the div is given by
997 * then the inequality expresses
1001 static struct isl_vec
*ineq_for_div(struct isl_basic_set
*bset
, unsigned div
)
1005 struct isl_vec
*ineq
;
1010 total
= isl_basic_set_total_dim(bset
);
1011 div_pos
= 1 + total
- bset
->n_div
+ div
;
1013 ineq
= isl_vec_alloc(bset
->ctx
, 1 + total
);
1017 isl_seq_cpy(ineq
->el
, bset
->div
[div
] + 1, 1 + total
);
1018 isl_int_neg(ineq
->el
[div_pos
], bset
->div
[div
][0]);
1022 /* Given a row in the tableau and a div that was created
1023 * using get_row_split_div and that has been constrained to equality, i.e.,
1025 * d = floor(\sum_i {a_i} y_i) = \sum_i {a_i} y_i
1027 * replace the expression "\sum_i {a_i} y_i" in the row by d,
1028 * i.e., we subtract "\sum_i {a_i} y_i" and add 1 d.
1029 * The coefficients of the non-parameters in the tableau have been
1030 * verified to be integral. We can therefore simply replace coefficient b
1031 * by floor(b). For the coefficients of the parameters we have
1032 * floor(a_i) = a_i - {a_i}, while for the other coefficients, we have
1035 static struct isl_tab
*set_row_cst_to_div(struct isl_tab
*tab
, int row
, int div
)
1037 isl_seq_fdiv_q(tab
->mat
->row
[row
] + 1, tab
->mat
->row
[row
] + 1,
1038 tab
->mat
->row
[row
][0], 1 + tab
->M
+ tab
->n_col
);
1040 isl_int_set_si(tab
->mat
->row
[row
][0], 1);
1042 if (tab
->var
[tab
->n_var
- tab
->n_div
+ div
].is_row
) {
1043 int drow
= tab
->var
[tab
->n_var
- tab
->n_div
+ div
].index
;
1045 isl_assert(tab
->mat
->ctx
,
1046 isl_int_is_one(tab
->mat
->row
[drow
][0]), goto error
);
1047 isl_seq_combine(tab
->mat
->row
[row
] + 1,
1048 tab
->mat
->ctx
->one
, tab
->mat
->row
[row
] + 1,
1049 tab
->mat
->ctx
->one
, tab
->mat
->row
[drow
] + 1,
1050 1 + tab
->M
+ tab
->n_col
);
1052 int dcol
= tab
->var
[tab
->n_var
- tab
->n_div
+ div
].index
;
1054 isl_int_add_ui(tab
->mat
->row
[row
][2 + tab
->M
+ dcol
],
1055 tab
->mat
->row
[row
][2 + tab
->M
+ dcol
], 1);
1064 /* Check if the (parametric) constant of the given row is obviously
1065 * negative, meaning that we don't need to consult the context tableau.
1066 * If there is a big parameter and its coefficient is non-zero,
1067 * then this coefficient determines the outcome.
1068 * Otherwise, we check whether the constant is negative and
1069 * all non-zero coefficients of parameters are negative and
1070 * belong to non-negative parameters.
1072 static int is_obviously_neg(struct isl_tab
*tab
, int row
)
1076 unsigned off
= 2 + tab
->M
;
1079 if (isl_int_is_pos(tab
->mat
->row
[row
][2]))
1081 if (isl_int_is_neg(tab
->mat
->row
[row
][2]))
1085 if (isl_int_is_nonneg(tab
->mat
->row
[row
][1]))
1087 for (i
= 0; i
< tab
->n_param
; ++i
) {
1088 /* Eliminated parameter */
1089 if (tab
->var
[i
].is_row
)
1091 col
= tab
->var
[i
].index
;
1092 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
1094 if (!tab
->var
[i
].is_nonneg
)
1096 if (isl_int_is_pos(tab
->mat
->row
[row
][off
+ col
]))
1099 for (i
= 0; i
< tab
->n_div
; ++i
) {
1100 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
1102 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
1103 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
1105 if (!tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_nonneg
)
1107 if (isl_int_is_pos(tab
->mat
->row
[row
][off
+ col
]))
1113 /* Check if the (parametric) constant of the given row is obviously
1114 * non-negative, meaning that we don't need to consult the context tableau.
1115 * If there is a big parameter and its coefficient is non-zero,
1116 * then this coefficient determines the outcome.
1117 * Otherwise, we check whether the constant is non-negative and
1118 * all non-zero coefficients of parameters are positive and
1119 * belong to non-negative parameters.
1121 static int is_obviously_nonneg(struct isl_tab
*tab
, int row
)
1125 unsigned off
= 2 + tab
->M
;
1128 if (isl_int_is_pos(tab
->mat
->row
[row
][2]))
1130 if (isl_int_is_neg(tab
->mat
->row
[row
][2]))
1134 if (isl_int_is_neg(tab
->mat
->row
[row
][1]))
1136 for (i
= 0; i
< tab
->n_param
; ++i
) {
1137 /* Eliminated parameter */
1138 if (tab
->var
[i
].is_row
)
1140 col
= tab
->var
[i
].index
;
1141 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
1143 if (!tab
->var
[i
].is_nonneg
)
1145 if (isl_int_is_neg(tab
->mat
->row
[row
][off
+ col
]))
1148 for (i
= 0; i
< tab
->n_div
; ++i
) {
1149 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
1151 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
1152 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
1154 if (!tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_nonneg
)
1156 if (isl_int_is_neg(tab
->mat
->row
[row
][off
+ col
]))
1162 /* Given a row r and two columns, return the column that would
1163 * lead to the lexicographically smallest increment in the sample
1164 * solution when leaving the basis in favor of the row.
1165 * Pivoting with column c will increment the sample value by a non-negative
1166 * constant times a_{V,c}/a_{r,c}, with a_{V,c} the elements of column c
1167 * corresponding to the non-parametric variables.
1168 * If variable v appears in a column c_v, the a_{v,c} = 1 iff c = c_v,
1169 * with all other entries in this virtual row equal to zero.
1170 * If variable v appears in a row, then a_{v,c} is the element in column c
1173 * Let v be the first variable with a_{v,c1}/a_{r,c1} != a_{v,c2}/a_{r,c2}.
1174 * Then if a_{v,c1}/a_{r,c1} < a_{v,c2}/a_{r,c2}, i.e.,
1175 * a_{v,c2} a_{r,c1} - a_{v,c1} a_{r,c2} > 0, c1 results in the minimal
1176 * increment. Otherwise, it's c2.
1178 static int lexmin_col_pair(struct isl_tab
*tab
,
1179 int row
, int col1
, int col2
, isl_int tmp
)
1184 tr
= tab
->mat
->row
[row
] + 2 + tab
->M
;
1186 for (i
= tab
->n_param
; i
< tab
->n_var
- tab
->n_div
; ++i
) {
1190 if (!tab
->var
[i
].is_row
) {
1191 if (tab
->var
[i
].index
== col1
)
1193 if (tab
->var
[i
].index
== col2
)
1198 if (tab
->var
[i
].index
== row
)
1201 r
= tab
->mat
->row
[tab
->var
[i
].index
] + 2 + tab
->M
;
1202 s1
= isl_int_sgn(r
[col1
]);
1203 s2
= isl_int_sgn(r
[col2
]);
1204 if (s1
== 0 && s2
== 0)
1211 isl_int_mul(tmp
, r
[col2
], tr
[col1
]);
1212 isl_int_submul(tmp
, r
[col1
], tr
[col2
]);
1213 if (isl_int_is_pos(tmp
))
1215 if (isl_int_is_neg(tmp
))
1221 /* Given a row in the tableau, find and return the column that would
1222 * result in the lexicographically smallest, but positive, increment
1223 * in the sample point.
1224 * If there is no such column, then return tab->n_col.
1225 * If anything goes wrong, return -1.
1227 static int lexmin_pivot_col(struct isl_tab
*tab
, int row
)
1230 int col
= tab
->n_col
;
1234 tr
= tab
->mat
->row
[row
] + 2 + tab
->M
;
1238 for (j
= tab
->n_dead
; j
< tab
->n_col
; ++j
) {
1239 if (tab
->col_var
[j
] >= 0 &&
1240 (tab
->col_var
[j
] < tab
->n_param
||
1241 tab
->col_var
[j
] >= tab
->n_var
- tab
->n_div
))
1244 if (!isl_int_is_pos(tr
[j
]))
1247 if (col
== tab
->n_col
)
1250 col
= lexmin_col_pair(tab
, row
, col
, j
, tmp
);
1251 isl_assert(tab
->mat
->ctx
, col
>= 0, goto error
);
1261 /* Return the first known violated constraint, i.e., a non-negative
1262 * constraint that currently has an either obviously negative value
1263 * or a previously determined to be negative value.
1265 * If any constraint has a negative coefficient for the big parameter,
1266 * if any, then we return one of these first.
1268 static int first_neg(struct isl_tab
*tab
)
1273 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
1274 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
1276 if (!isl_int_is_neg(tab
->mat
->row
[row
][2]))
1279 tab
->row_sign
[row
] = isl_tab_row_neg
;
1282 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
1283 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
1285 if (tab
->row_sign
) {
1286 if (tab
->row_sign
[row
] == 0 &&
1287 is_obviously_neg(tab
, row
))
1288 tab
->row_sign
[row
] = isl_tab_row_neg
;
1289 if (tab
->row_sign
[row
] != isl_tab_row_neg
)
1291 } else if (!is_obviously_neg(tab
, row
))
1298 /* Check whether the invariant that all columns are lexico-positive
1299 * is satisfied. This function is not called from the current code
1300 * but is useful during debugging.
1302 static void check_lexpos(struct isl_tab
*tab
) __attribute__ ((unused
));
1303 static void check_lexpos(struct isl_tab
*tab
)
1305 unsigned off
= 2 + tab
->M
;
1310 for (col
= tab
->n_dead
; col
< tab
->n_col
; ++col
) {
1311 if (tab
->col_var
[col
] >= 0 &&
1312 (tab
->col_var
[col
] < tab
->n_param
||
1313 tab
->col_var
[col
] >= tab
->n_var
- tab
->n_div
))
1315 for (var
= tab
->n_param
; var
< tab
->n_var
- tab
->n_div
; ++var
) {
1316 if (!tab
->var
[var
].is_row
) {
1317 if (tab
->var
[var
].index
== col
)
1322 row
= tab
->var
[var
].index
;
1323 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
1325 if (isl_int_is_pos(tab
->mat
->row
[row
][off
+ col
]))
1327 fprintf(stderr
, "lexneg column %d (row %d)\n",
1330 if (var
>= tab
->n_var
- tab
->n_div
)
1331 fprintf(stderr
, "zero column %d\n", col
);
1335 /* Report to the caller that the given constraint is part of an encountered
1338 static int report_conflicting_constraint(struct isl_tab
*tab
, int con
)
1340 return tab
->conflict(con
, tab
->conflict_user
);
1343 /* Given a conflicting row in the tableau, report all constraints
1344 * involved in the row to the caller. That is, the row itself
1345 * (if it represents a constraint) and all constraint columns with
1346 * non-zero (and therefore negative) coefficients.
1348 static int report_conflict(struct isl_tab
*tab
, int row
)
1356 if (tab
->row_var
[row
] < 0 &&
1357 report_conflicting_constraint(tab
, ~tab
->row_var
[row
]) < 0)
1360 tr
= tab
->mat
->row
[row
] + 2 + tab
->M
;
1362 for (j
= tab
->n_dead
; j
< tab
->n_col
; ++j
) {
1363 if (tab
->col_var
[j
] >= 0 &&
1364 (tab
->col_var
[j
] < tab
->n_param
||
1365 tab
->col_var
[j
] >= tab
->n_var
- tab
->n_div
))
1368 if (!isl_int_is_neg(tr
[j
]))
1371 if (tab
->col_var
[j
] < 0 &&
1372 report_conflicting_constraint(tab
, ~tab
->col_var
[j
]) < 0)
1379 /* Resolve all known or obviously violated constraints through pivoting.
1380 * In particular, as long as we can find any violated constraint, we
1381 * look for a pivoting column that would result in the lexicographically
1382 * smallest increment in the sample point. If there is no such column
1383 * then the tableau is infeasible.
1385 static int restore_lexmin(struct isl_tab
*tab
) WARN_UNUSED
;
1386 static int restore_lexmin(struct isl_tab
*tab
)
1394 while ((row
= first_neg(tab
)) != -1) {
1395 col
= lexmin_pivot_col(tab
, row
);
1396 if (col
>= tab
->n_col
) {
1397 if (report_conflict(tab
, row
) < 0)
1399 if (isl_tab_mark_empty(tab
) < 0)
1405 if (isl_tab_pivot(tab
, row
, col
) < 0)
1411 /* Given a row that represents an equality, look for an appropriate
1413 * In particular, if there are any non-zero coefficients among
1414 * the non-parameter variables, then we take the last of these
1415 * variables. Eliminating this variable in terms of the other
1416 * variables and/or parameters does not influence the property
1417 * that all column in the initial tableau are lexicographically
1418 * positive. The row corresponding to the eliminated variable
1419 * will only have non-zero entries below the diagonal of the
1420 * initial tableau. That is, we transform
1426 * If there is no such non-parameter variable, then we are dealing with
1427 * pure parameter equality and we pick any parameter with coefficient 1 or -1
1428 * for elimination. This will ensure that the eliminated parameter
1429 * always has an integer value whenever all the other parameters are integral.
1430 * If there is no such parameter then we return -1.
1432 static int last_var_col_or_int_par_col(struct isl_tab
*tab
, int row
)
1434 unsigned off
= 2 + tab
->M
;
1437 for (i
= tab
->n_var
- tab
->n_div
- 1; i
>= 0 && i
>= tab
->n_param
; --i
) {
1439 if (tab
->var
[i
].is_row
)
1441 col
= tab
->var
[i
].index
;
1442 if (col
<= tab
->n_dead
)
1444 if (!isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
1447 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
1448 if (isl_int_is_one(tab
->mat
->row
[row
][off
+ i
]))
1450 if (isl_int_is_negone(tab
->mat
->row
[row
][off
+ i
]))
1456 /* Add an equality that is known to be valid to the tableau.
1457 * We first check if we can eliminate a variable or a parameter.
1458 * If not, we add the equality as two inequalities.
1459 * In this case, the equality was a pure parameter equality and there
1460 * is no need to resolve any constraint violations.
1462 * This function assumes that at least two more rows and at least
1463 * two more elements in the constraint array are available in the tableau.
1465 static struct isl_tab
*add_lexmin_valid_eq(struct isl_tab
*tab
, isl_int
*eq
)
1472 r
= isl_tab_add_row(tab
, eq
);
1476 r
= tab
->con
[r
].index
;
1477 i
= last_var_col_or_int_par_col(tab
, r
);
1479 tab
->con
[r
].is_nonneg
= 1;
1480 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
1482 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
1483 r
= isl_tab_add_row(tab
, eq
);
1486 tab
->con
[r
].is_nonneg
= 1;
1487 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
1490 if (isl_tab_pivot(tab
, r
, i
) < 0)
1492 if (isl_tab_kill_col(tab
, i
) < 0)
1503 /* Check if the given row is a pure constant.
1505 static int is_constant(struct isl_tab
*tab
, int row
)
1507 unsigned off
= 2 + tab
->M
;
1509 return isl_seq_first_non_zero(tab
->mat
->row
[row
] + off
+ tab
->n_dead
,
1510 tab
->n_col
- tab
->n_dead
) == -1;
1513 /* Add an equality that may or may not be valid to the tableau.
1514 * If the resulting row is a pure constant, then it must be zero.
1515 * Otherwise, the resulting tableau is empty.
1517 * If the row is not a pure constant, then we add two inequalities,
1518 * each time checking that they can be satisfied.
1519 * In the end we try to use one of the two constraints to eliminate
1522 * This function assumes that at least two more rows and at least
1523 * two more elements in the constraint array are available in the tableau.
1525 static int add_lexmin_eq(struct isl_tab
*tab
, isl_int
*eq
) WARN_UNUSED
;
1526 static int add_lexmin_eq(struct isl_tab
*tab
, isl_int
*eq
)
1530 struct isl_tab_undo
*snap
;
1534 snap
= isl_tab_snap(tab
);
1535 r1
= isl_tab_add_row(tab
, eq
);
1538 tab
->con
[r1
].is_nonneg
= 1;
1539 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r1
]) < 0)
1542 row
= tab
->con
[r1
].index
;
1543 if (is_constant(tab
, row
)) {
1544 if (!isl_int_is_zero(tab
->mat
->row
[row
][1]) ||
1545 (tab
->M
&& !isl_int_is_zero(tab
->mat
->row
[row
][2]))) {
1546 if (isl_tab_mark_empty(tab
) < 0)
1550 if (isl_tab_rollback(tab
, snap
) < 0)
1555 if (restore_lexmin(tab
) < 0)
1560 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
1562 r2
= isl_tab_add_row(tab
, eq
);
1565 tab
->con
[r2
].is_nonneg
= 1;
1566 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r2
]) < 0)
1569 if (restore_lexmin(tab
) < 0)
1574 if (!tab
->con
[r1
].is_row
) {
1575 if (isl_tab_kill_col(tab
, tab
->con
[r1
].index
) < 0)
1577 } else if (!tab
->con
[r2
].is_row
) {
1578 if (isl_tab_kill_col(tab
, tab
->con
[r2
].index
) < 0)
1583 tab
->bmap
= isl_basic_map_add_ineq(tab
->bmap
, eq
);
1584 if (isl_tab_push(tab
, isl_tab_undo_bmap_ineq
) < 0)
1586 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
1587 tab
->bmap
= isl_basic_map_add_ineq(tab
->bmap
, eq
);
1588 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
1589 if (isl_tab_push(tab
, isl_tab_undo_bmap_ineq
) < 0)
1598 /* Add an inequality to the tableau, resolving violations using
1601 * This function assumes that at least one more row and at least
1602 * one more element in the constraint array are available in the tableau.
1604 static struct isl_tab
*add_lexmin_ineq(struct isl_tab
*tab
, isl_int
*ineq
)
1611 tab
->bmap
= isl_basic_map_add_ineq(tab
->bmap
, ineq
);
1612 if (isl_tab_push(tab
, isl_tab_undo_bmap_ineq
) < 0)
1617 r
= isl_tab_add_row(tab
, ineq
);
1620 tab
->con
[r
].is_nonneg
= 1;
1621 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
1623 if (isl_tab_row_is_redundant(tab
, tab
->con
[r
].index
)) {
1624 if (isl_tab_mark_redundant(tab
, tab
->con
[r
].index
) < 0)
1629 if (restore_lexmin(tab
) < 0)
1631 if (!tab
->empty
&& tab
->con
[r
].is_row
&&
1632 isl_tab_row_is_redundant(tab
, tab
->con
[r
].index
))
1633 if (isl_tab_mark_redundant(tab
, tab
->con
[r
].index
) < 0)
1641 /* Check if the coefficients of the parameters are all integral.
1643 static int integer_parameter(struct isl_tab
*tab
, int row
)
1647 unsigned off
= 2 + tab
->M
;
1649 for (i
= 0; i
< tab
->n_param
; ++i
) {
1650 /* Eliminated parameter */
1651 if (tab
->var
[i
].is_row
)
1653 col
= tab
->var
[i
].index
;
1654 if (!isl_int_is_divisible_by(tab
->mat
->row
[row
][off
+ col
],
1655 tab
->mat
->row
[row
][0]))
1658 for (i
= 0; i
< tab
->n_div
; ++i
) {
1659 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
1661 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
1662 if (!isl_int_is_divisible_by(tab
->mat
->row
[row
][off
+ col
],
1663 tab
->mat
->row
[row
][0]))
1669 /* Check if the coefficients of the non-parameter variables are all integral.
1671 static int integer_variable(struct isl_tab
*tab
, int row
)
1674 unsigned off
= 2 + tab
->M
;
1676 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
1677 if (tab
->col_var
[i
] >= 0 &&
1678 (tab
->col_var
[i
] < tab
->n_param
||
1679 tab
->col_var
[i
] >= tab
->n_var
- tab
->n_div
))
1681 if (!isl_int_is_divisible_by(tab
->mat
->row
[row
][off
+ i
],
1682 tab
->mat
->row
[row
][0]))
1688 /* Check if the constant term is integral.
1690 static int integer_constant(struct isl_tab
*tab
, int row
)
1692 return isl_int_is_divisible_by(tab
->mat
->row
[row
][1],
1693 tab
->mat
->row
[row
][0]);
1696 #define I_CST 1 << 0
1697 #define I_PAR 1 << 1
1698 #define I_VAR 1 << 2
1700 /* Check for next (non-parameter) variable after "var" (first if var == -1)
1701 * that is non-integer and therefore requires a cut and return
1702 * the index of the variable.
1703 * For parametric tableaus, there are three parts in a row,
1704 * the constant, the coefficients of the parameters and the rest.
1705 * For each part, we check whether the coefficients in that part
1706 * are all integral and if so, set the corresponding flag in *f.
1707 * If the constant and the parameter part are integral, then the
1708 * current sample value is integral and no cut is required
1709 * (irrespective of whether the variable part is integral).
1711 static int next_non_integer_var(struct isl_tab
*tab
, int var
, int *f
)
1713 var
= var
< 0 ? tab
->n_param
: var
+ 1;
1715 for (; var
< tab
->n_var
- tab
->n_div
; ++var
) {
1718 if (!tab
->var
[var
].is_row
)
1720 row
= tab
->var
[var
].index
;
1721 if (integer_constant(tab
, row
))
1722 ISL_FL_SET(flags
, I_CST
);
1723 if (integer_parameter(tab
, row
))
1724 ISL_FL_SET(flags
, I_PAR
);
1725 if (ISL_FL_ISSET(flags
, I_CST
) && ISL_FL_ISSET(flags
, I_PAR
))
1727 if (integer_variable(tab
, row
))
1728 ISL_FL_SET(flags
, I_VAR
);
1735 /* Check for first (non-parameter) variable that is non-integer and
1736 * therefore requires a cut and return the corresponding row.
1737 * For parametric tableaus, there are three parts in a row,
1738 * the constant, the coefficients of the parameters and the rest.
1739 * For each part, we check whether the coefficients in that part
1740 * are all integral and if so, set the corresponding flag in *f.
1741 * If the constant and the parameter part are integral, then the
1742 * current sample value is integral and no cut is required
1743 * (irrespective of whether the variable part is integral).
1745 static int first_non_integer_row(struct isl_tab
*tab
, int *f
)
1747 int var
= next_non_integer_var(tab
, -1, f
);
1749 return var
< 0 ? -1 : tab
->var
[var
].index
;
1752 /* Add a (non-parametric) cut to cut away the non-integral sample
1753 * value of the given row.
1755 * If the row is given by
1757 * m r = f + \sum_i a_i y_i
1761 * c = - {-f/m} + \sum_i {a_i/m} y_i >= 0
1763 * The big parameter, if any, is ignored, since it is assumed to be big
1764 * enough to be divisible by any integer.
1765 * If the tableau is actually a parametric tableau, then this function
1766 * is only called when all coefficients of the parameters are integral.
1767 * The cut therefore has zero coefficients for the parameters.
1769 * The current value is known to be negative, so row_sign, if it
1770 * exists, is set accordingly.
1772 * Return the row of the cut or -1.
1774 static int add_cut(struct isl_tab
*tab
, int row
)
1779 unsigned off
= 2 + tab
->M
;
1781 if (isl_tab_extend_cons(tab
, 1) < 0)
1783 r
= isl_tab_allocate_con(tab
);
1787 r_row
= tab
->mat
->row
[tab
->con
[r
].index
];
1788 isl_int_set(r_row
[0], tab
->mat
->row
[row
][0]);
1789 isl_int_neg(r_row
[1], tab
->mat
->row
[row
][1]);
1790 isl_int_fdiv_r(r_row
[1], r_row
[1], tab
->mat
->row
[row
][0]);
1791 isl_int_neg(r_row
[1], r_row
[1]);
1793 isl_int_set_si(r_row
[2], 0);
1794 for (i
= 0; i
< tab
->n_col
; ++i
)
1795 isl_int_fdiv_r(r_row
[off
+ i
],
1796 tab
->mat
->row
[row
][off
+ i
], tab
->mat
->row
[row
][0]);
1798 tab
->con
[r
].is_nonneg
= 1;
1799 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
1802 tab
->row_sign
[tab
->con
[r
].index
] = isl_tab_row_neg
;
1804 return tab
->con
[r
].index
;
1810 /* Given a non-parametric tableau, add cuts until an integer
1811 * sample point is obtained or until the tableau is determined
1812 * to be integer infeasible.
1813 * As long as there is any non-integer value in the sample point,
1814 * we add appropriate cuts, if possible, for each of these
1815 * non-integer values and then resolve the violated
1816 * cut constraints using restore_lexmin.
1817 * If one of the corresponding rows is equal to an integral
1818 * combination of variables/constraints plus a non-integral constant,
1819 * then there is no way to obtain an integer point and we return
1820 * a tableau that is marked empty.
1821 * The parameter cutting_strategy controls the strategy used when adding cuts
1822 * to remove non-integer points. CUT_ALL adds all possible cuts
1823 * before continuing the search. CUT_ONE adds only one cut at a time.
1825 static struct isl_tab
*cut_to_integer_lexmin(struct isl_tab
*tab
,
1826 int cutting_strategy
)
1837 while ((var
= next_non_integer_var(tab
, -1, &flags
)) != -1) {
1839 if (ISL_FL_ISSET(flags
, I_VAR
)) {
1840 if (isl_tab_mark_empty(tab
) < 0)
1844 row
= tab
->var
[var
].index
;
1845 row
= add_cut(tab
, row
);
1848 if (cutting_strategy
== CUT_ONE
)
1850 } while ((var
= next_non_integer_var(tab
, var
, &flags
)) != -1);
1851 if (restore_lexmin(tab
) < 0)
1862 /* Check whether all the currently active samples also satisfy the inequality
1863 * "ineq" (treated as an equality if eq is set).
1864 * Remove those samples that do not.
1866 static struct isl_tab
*check_samples(struct isl_tab
*tab
, isl_int
*ineq
, int eq
)
1874 isl_assert(tab
->mat
->ctx
, tab
->bmap
, goto error
);
1875 isl_assert(tab
->mat
->ctx
, tab
->samples
, goto error
);
1876 isl_assert(tab
->mat
->ctx
, tab
->samples
->n_col
== 1 + tab
->n_var
, goto error
);
1879 for (i
= tab
->n_outside
; i
< tab
->n_sample
; ++i
) {
1881 isl_seq_inner_product(ineq
, tab
->samples
->row
[i
],
1882 1 + tab
->n_var
, &v
);
1883 sgn
= isl_int_sgn(v
);
1884 if (eq
? (sgn
== 0) : (sgn
>= 0))
1886 tab
= isl_tab_drop_sample(tab
, i
);
1898 /* Check whether the sample value of the tableau is finite,
1899 * i.e., either the tableau does not use a big parameter, or
1900 * all values of the variables are equal to the big parameter plus
1901 * some constant. This constant is the actual sample value.
1903 static int sample_is_finite(struct isl_tab
*tab
)
1910 for (i
= 0; i
< tab
->n_var
; ++i
) {
1912 if (!tab
->var
[i
].is_row
)
1914 row
= tab
->var
[i
].index
;
1915 if (isl_int_ne(tab
->mat
->row
[row
][0], tab
->mat
->row
[row
][2]))
1921 /* Check if the context tableau of sol has any integer points.
1922 * Leave tab in empty state if no integer point can be found.
1923 * If an integer point can be found and if moreover it is finite,
1924 * then it is added to the list of sample values.
1926 * This function is only called when none of the currently active sample
1927 * values satisfies the most recently added constraint.
1929 static struct isl_tab
*check_integer_feasible(struct isl_tab
*tab
)
1931 struct isl_tab_undo
*snap
;
1936 snap
= isl_tab_snap(tab
);
1937 if (isl_tab_push_basis(tab
) < 0)
1940 tab
= cut_to_integer_lexmin(tab
, CUT_ALL
);
1944 if (!tab
->empty
&& sample_is_finite(tab
)) {
1945 struct isl_vec
*sample
;
1947 sample
= isl_tab_get_sample_value(tab
);
1949 if (isl_tab_add_sample(tab
, sample
) < 0)
1953 if (!tab
->empty
&& isl_tab_rollback(tab
, snap
) < 0)
1962 /* Check if any of the currently active sample values satisfies
1963 * the inequality "ineq" (an equality if eq is set).
1965 static int tab_has_valid_sample(struct isl_tab
*tab
, isl_int
*ineq
, int eq
)
1973 isl_assert(tab
->mat
->ctx
, tab
->bmap
, return -1);
1974 isl_assert(tab
->mat
->ctx
, tab
->samples
, return -1);
1975 isl_assert(tab
->mat
->ctx
, tab
->samples
->n_col
== 1 + tab
->n_var
, return -1);
1978 for (i
= tab
->n_outside
; i
< tab
->n_sample
; ++i
) {
1980 isl_seq_inner_product(ineq
, tab
->samples
->row
[i
],
1981 1 + tab
->n_var
, &v
);
1982 sgn
= isl_int_sgn(v
);
1983 if (eq
? (sgn
== 0) : (sgn
>= 0))
1988 return i
< tab
->n_sample
;
1991 /* Insert a div specified by "div" to the tableau "tab" at position "pos" and
1992 * return isl_bool_true if the div is obviously non-negative.
1994 static isl_bool
context_tab_insert_div(struct isl_tab
*tab
, int pos
,
1995 __isl_keep isl_vec
*div
,
1996 isl_stat (*add_ineq
)(void *user
, isl_int
*), void *user
)
2000 struct isl_mat
*samples
;
2003 r
= isl_tab_insert_div(tab
, pos
, div
, add_ineq
, user
);
2005 return isl_bool_error
;
2006 nonneg
= tab
->var
[r
].is_nonneg
;
2007 tab
->var
[r
].frozen
= 1;
2009 samples
= isl_mat_extend(tab
->samples
,
2010 tab
->n_sample
, 1 + tab
->n_var
);
2011 tab
->samples
= samples
;
2013 return isl_bool_error
;
2014 for (i
= tab
->n_outside
; i
< samples
->n_row
; ++i
) {
2015 isl_seq_inner_product(div
->el
+ 1, samples
->row
[i
],
2016 div
->size
- 1, &samples
->row
[i
][samples
->n_col
- 1]);
2017 isl_int_fdiv_q(samples
->row
[i
][samples
->n_col
- 1],
2018 samples
->row
[i
][samples
->n_col
- 1], div
->el
[0]);
2020 tab
->samples
= isl_mat_move_cols(tab
->samples
, 1 + pos
,
2021 1 + tab
->n_var
- 1, 1);
2023 return isl_bool_error
;
2028 /* Add a div specified by "div" to both the main tableau and
2029 * the context tableau. In case of the main tableau, we only
2030 * need to add an extra div. In the context tableau, we also
2031 * need to express the meaning of the div.
2032 * Return the index of the div or -1 if anything went wrong.
2034 * The new integer division is added before any unknown integer
2035 * divisions in the context to ensure that it does not get
2036 * equated to some linear combination involving unknown integer
2039 static int add_div(struct isl_tab
*tab
, struct isl_context
*context
,
2040 __isl_keep isl_vec
*div
)
2045 struct isl_tab
*context_tab
= context
->op
->peek_tab(context
);
2047 if (!tab
|| !context_tab
)
2050 pos
= context_tab
->n_var
- context
->n_unknown
;
2051 if ((nonneg
= context
->op
->insert_div(context
, pos
, div
)) < 0)
2054 if (!context
->op
->is_ok(context
))
2057 pos
= tab
->n_var
- context
->n_unknown
;
2058 if (isl_tab_extend_vars(tab
, 1) < 0)
2060 r
= isl_tab_insert_var(tab
, pos
);
2064 tab
->var
[r
].is_nonneg
= 1;
2065 tab
->var
[r
].frozen
= 1;
2068 return tab
->n_div
- 1 - context
->n_unknown
;
2070 context
->op
->invalidate(context
);
2074 static int find_div(struct isl_tab
*tab
, isl_int
*div
, isl_int denom
)
2077 unsigned total
= isl_basic_map_total_dim(tab
->bmap
);
2079 for (i
= 0; i
< tab
->bmap
->n_div
; ++i
) {
2080 if (isl_int_ne(tab
->bmap
->div
[i
][0], denom
))
2082 if (!isl_seq_eq(tab
->bmap
->div
[i
] + 1, div
, 1 + total
))
2089 /* Return the index of a div that corresponds to "div".
2090 * We first check if we already have such a div and if not, we create one.
2092 static int get_div(struct isl_tab
*tab
, struct isl_context
*context
,
2093 struct isl_vec
*div
)
2096 struct isl_tab
*context_tab
= context
->op
->peek_tab(context
);
2101 d
= find_div(context_tab
, div
->el
+ 1, div
->el
[0]);
2105 return add_div(tab
, context
, div
);
2108 /* Add a parametric cut to cut away the non-integral sample value
2110 * Let a_i be the coefficients of the constant term and the parameters
2111 * and let b_i be the coefficients of the variables or constraints
2112 * in basis of the tableau.
2113 * Let q be the div q = floor(\sum_i {-a_i} y_i).
2115 * The cut is expressed as
2117 * c = \sum_i -{-a_i} y_i + \sum_i {b_i} x_i + q >= 0
2119 * If q did not already exist in the context tableau, then it is added first.
2120 * If q is in a column of the main tableau then the "+ q" can be accomplished
2121 * by setting the corresponding entry to the denominator of the constraint.
2122 * If q happens to be in a row of the main tableau, then the corresponding
2123 * row needs to be added instead (taking care of the denominators).
2124 * Note that this is very unlikely, but perhaps not entirely impossible.
2126 * The current value of the cut is known to be negative (or at least
2127 * non-positive), so row_sign is set accordingly.
2129 * Return the row of the cut or -1.
2131 static int add_parametric_cut(struct isl_tab
*tab
, int row
,
2132 struct isl_context
*context
)
2134 struct isl_vec
*div
;
2141 unsigned off
= 2 + tab
->M
;
2146 div
= get_row_parameter_div(tab
, row
);
2150 n
= tab
->n_div
- context
->n_unknown
;
2151 d
= context
->op
->get_div(context
, tab
, div
);
2156 if (isl_tab_extend_cons(tab
, 1) < 0)
2158 r
= isl_tab_allocate_con(tab
);
2162 r_row
= tab
->mat
->row
[tab
->con
[r
].index
];
2163 isl_int_set(r_row
[0], tab
->mat
->row
[row
][0]);
2164 isl_int_neg(r_row
[1], tab
->mat
->row
[row
][1]);
2165 isl_int_fdiv_r(r_row
[1], r_row
[1], tab
->mat
->row
[row
][0]);
2166 isl_int_neg(r_row
[1], r_row
[1]);
2168 isl_int_set_si(r_row
[2], 0);
2169 for (i
= 0; i
< tab
->n_param
; ++i
) {
2170 if (tab
->var
[i
].is_row
)
2172 col
= tab
->var
[i
].index
;
2173 isl_int_neg(r_row
[off
+ col
], tab
->mat
->row
[row
][off
+ col
]);
2174 isl_int_fdiv_r(r_row
[off
+ col
], r_row
[off
+ col
],
2175 tab
->mat
->row
[row
][0]);
2176 isl_int_neg(r_row
[off
+ col
], r_row
[off
+ col
]);
2178 for (i
= 0; i
< tab
->n_div
; ++i
) {
2179 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
2181 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
2182 isl_int_neg(r_row
[off
+ col
], tab
->mat
->row
[row
][off
+ col
]);
2183 isl_int_fdiv_r(r_row
[off
+ col
], r_row
[off
+ col
],
2184 tab
->mat
->row
[row
][0]);
2185 isl_int_neg(r_row
[off
+ col
], r_row
[off
+ col
]);
2187 for (i
= 0; i
< tab
->n_col
; ++i
) {
2188 if (tab
->col_var
[i
] >= 0 &&
2189 (tab
->col_var
[i
] < tab
->n_param
||
2190 tab
->col_var
[i
] >= tab
->n_var
- tab
->n_div
))
2192 isl_int_fdiv_r(r_row
[off
+ i
],
2193 tab
->mat
->row
[row
][off
+ i
], tab
->mat
->row
[row
][0]);
2195 if (tab
->var
[tab
->n_var
- tab
->n_div
+ d
].is_row
) {
2197 int d_row
= tab
->var
[tab
->n_var
- tab
->n_div
+ d
].index
;
2199 isl_int_gcd(gcd
, tab
->mat
->row
[d_row
][0], r_row
[0]);
2200 isl_int_divexact(r_row
[0], r_row
[0], gcd
);
2201 isl_int_divexact(gcd
, tab
->mat
->row
[d_row
][0], gcd
);
2202 isl_seq_combine(r_row
+ 1, gcd
, r_row
+ 1,
2203 r_row
[0], tab
->mat
->row
[d_row
] + 1,
2204 off
- 1 + tab
->n_col
);
2205 isl_int_mul(r_row
[0], r_row
[0], tab
->mat
->row
[d_row
][0]);
2208 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ d
].index
;
2209 isl_int_set(r_row
[off
+ col
], tab
->mat
->row
[row
][0]);
2212 tab
->con
[r
].is_nonneg
= 1;
2213 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
2216 tab
->row_sign
[tab
->con
[r
].index
] = isl_tab_row_neg
;
2218 row
= tab
->con
[r
].index
;
2220 if (d
>= n
&& context
->op
->detect_equalities(context
, tab
) < 0)
2226 /* Construct a tableau for bmap that can be used for computing
2227 * the lexicographic minimum (or maximum) of bmap.
2228 * If not NULL, then dom is the domain where the minimum
2229 * should be computed. In this case, we set up a parametric
2230 * tableau with row signs (initialized to "unknown").
2231 * If M is set, then the tableau will use a big parameter.
2232 * If max is set, then a maximum should be computed instead of a minimum.
2233 * This means that for each variable x, the tableau will contain the variable
2234 * x' = M - x, rather than x' = M + x. This in turn means that the coefficient
2235 * of the variables in all constraints are negated prior to adding them
2238 static struct isl_tab
*tab_for_lexmin(struct isl_basic_map
*bmap
,
2239 struct isl_basic_set
*dom
, unsigned M
, int max
)
2242 struct isl_tab
*tab
;
2246 tab
= isl_tab_alloc(bmap
->ctx
, 2 * bmap
->n_eq
+ bmap
->n_ineq
+ 1,
2247 isl_basic_map_total_dim(bmap
), M
);
2251 tab
->rational
= ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
2253 tab
->n_param
= isl_basic_set_total_dim(dom
) - dom
->n_div
;
2254 tab
->n_div
= dom
->n_div
;
2255 tab
->row_sign
= isl_calloc_array(bmap
->ctx
,
2256 enum isl_tab_row_sign
, tab
->mat
->n_row
);
2257 if (tab
->mat
->n_row
&& !tab
->row_sign
)
2260 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
)) {
2261 if (isl_tab_mark_empty(tab
) < 0)
2266 for (i
= tab
->n_param
; i
< tab
->n_var
- tab
->n_div
; ++i
) {
2267 tab
->var
[i
].is_nonneg
= 1;
2268 tab
->var
[i
].frozen
= 1;
2270 o_var
= 1 + tab
->n_param
;
2271 n_var
= tab
->n_var
- tab
->n_param
- tab
->n_div
;
2272 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
2274 isl_seq_neg(bmap
->eq
[i
] + o_var
,
2275 bmap
->eq
[i
] + o_var
, n_var
);
2276 tab
= add_lexmin_valid_eq(tab
, bmap
->eq
[i
]);
2278 isl_seq_neg(bmap
->eq
[i
] + o_var
,
2279 bmap
->eq
[i
] + o_var
, n_var
);
2280 if (!tab
|| tab
->empty
)
2283 if (bmap
->n_eq
&& restore_lexmin(tab
) < 0)
2285 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
2287 isl_seq_neg(bmap
->ineq
[i
] + o_var
,
2288 bmap
->ineq
[i
] + o_var
, n_var
);
2289 tab
= add_lexmin_ineq(tab
, bmap
->ineq
[i
]);
2291 isl_seq_neg(bmap
->ineq
[i
] + o_var
,
2292 bmap
->ineq
[i
] + o_var
, n_var
);
2293 if (!tab
|| tab
->empty
)
2302 /* Given a main tableau where more than one row requires a split,
2303 * determine and return the "best" row to split on.
2305 * Given two rows in the main tableau, if the inequality corresponding
2306 * to the first row is redundant with respect to that of the second row
2307 * in the current tableau, then it is better to split on the second row,
2308 * since in the positive part, both rows will be positive.
2309 * (In the negative part a pivot will have to be performed and just about
2310 * anything can happen to the sign of the other row.)
2312 * As a simple heuristic, we therefore select the row that makes the most
2313 * of the other rows redundant.
2315 * Perhaps it would also be useful to look at the number of constraints
2316 * that conflict with any given constraint.
2318 * best is the best row so far (-1 when we have not found any row yet).
2319 * best_r is the number of other rows made redundant by row best.
2320 * When best is still -1, bset_r is meaningless, but it is initialized
2321 * to some arbitrary value (0) anyway. Without this redundant initialization
2322 * valgrind may warn about uninitialized memory accesses when isl
2323 * is compiled with some versions of gcc.
2325 static int best_split(struct isl_tab
*tab
, struct isl_tab
*context_tab
)
2327 struct isl_tab_undo
*snap
;
2333 if (isl_tab_extend_cons(context_tab
, 2) < 0)
2336 snap
= isl_tab_snap(context_tab
);
2338 for (split
= tab
->n_redundant
; split
< tab
->n_row
; ++split
) {
2339 struct isl_tab_undo
*snap2
;
2340 struct isl_vec
*ineq
= NULL
;
2344 if (!isl_tab_var_from_row(tab
, split
)->is_nonneg
)
2346 if (tab
->row_sign
[split
] != isl_tab_row_any
)
2349 ineq
= get_row_parameter_ineq(tab
, split
);
2352 ok
= isl_tab_add_ineq(context_tab
, ineq
->el
) >= 0;
2357 snap2
= isl_tab_snap(context_tab
);
2359 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
2360 struct isl_tab_var
*var
;
2364 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
2366 if (tab
->row_sign
[row
] != isl_tab_row_any
)
2369 ineq
= get_row_parameter_ineq(tab
, row
);
2372 ok
= isl_tab_add_ineq(context_tab
, ineq
->el
) >= 0;
2376 var
= &context_tab
->con
[context_tab
->n_con
- 1];
2377 if (!context_tab
->empty
&&
2378 !isl_tab_min_at_most_neg_one(context_tab
, var
))
2380 if (isl_tab_rollback(context_tab
, snap2
) < 0)
2383 if (best
== -1 || r
> best_r
) {
2387 if (isl_tab_rollback(context_tab
, snap
) < 0)
2394 static struct isl_basic_set
*context_lex_peek_basic_set(
2395 struct isl_context
*context
)
2397 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2400 return isl_tab_peek_bset(clex
->tab
);
2403 static struct isl_tab
*context_lex_peek_tab(struct isl_context
*context
)
2405 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2409 static void context_lex_add_eq(struct isl_context
*context
, isl_int
*eq
,
2410 int check
, int update
)
2412 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2413 if (isl_tab_extend_cons(clex
->tab
, 2) < 0)
2415 if (add_lexmin_eq(clex
->tab
, eq
) < 0)
2418 int v
= tab_has_valid_sample(clex
->tab
, eq
, 1);
2422 clex
->tab
= check_integer_feasible(clex
->tab
);
2425 clex
->tab
= check_samples(clex
->tab
, eq
, 1);
2428 isl_tab_free(clex
->tab
);
2432 static void context_lex_add_ineq(struct isl_context
*context
, isl_int
*ineq
,
2433 int check
, int update
)
2435 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2436 if (isl_tab_extend_cons(clex
->tab
, 1) < 0)
2438 clex
->tab
= add_lexmin_ineq(clex
->tab
, ineq
);
2440 int v
= tab_has_valid_sample(clex
->tab
, ineq
, 0);
2444 clex
->tab
= check_integer_feasible(clex
->tab
);
2447 clex
->tab
= check_samples(clex
->tab
, ineq
, 0);
2450 isl_tab_free(clex
->tab
);
2454 static isl_stat
context_lex_add_ineq_wrap(void *user
, isl_int
*ineq
)
2456 struct isl_context
*context
= (struct isl_context
*)user
;
2457 context_lex_add_ineq(context
, ineq
, 0, 0);
2458 return context
->op
->is_ok(context
) ? isl_stat_ok
: isl_stat_error
;
2461 /* Check which signs can be obtained by "ineq" on all the currently
2462 * active sample values. See row_sign for more information.
2464 static enum isl_tab_row_sign
tab_ineq_sign(struct isl_tab
*tab
, isl_int
*ineq
,
2470 enum isl_tab_row_sign res
= isl_tab_row_unknown
;
2472 isl_assert(tab
->mat
->ctx
, tab
->samples
, return isl_tab_row_unknown
);
2473 isl_assert(tab
->mat
->ctx
, tab
->samples
->n_col
== 1 + tab
->n_var
,
2474 return isl_tab_row_unknown
);
2477 for (i
= tab
->n_outside
; i
< tab
->n_sample
; ++i
) {
2478 isl_seq_inner_product(tab
->samples
->row
[i
], ineq
,
2479 1 + tab
->n_var
, &tmp
);
2480 sgn
= isl_int_sgn(tmp
);
2481 if (sgn
> 0 || (sgn
== 0 && strict
)) {
2482 if (res
== isl_tab_row_unknown
)
2483 res
= isl_tab_row_pos
;
2484 if (res
== isl_tab_row_neg
)
2485 res
= isl_tab_row_any
;
2488 if (res
== isl_tab_row_unknown
)
2489 res
= isl_tab_row_neg
;
2490 if (res
== isl_tab_row_pos
)
2491 res
= isl_tab_row_any
;
2493 if (res
== isl_tab_row_any
)
2501 static enum isl_tab_row_sign
context_lex_ineq_sign(struct isl_context
*context
,
2502 isl_int
*ineq
, int strict
)
2504 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2505 return tab_ineq_sign(clex
->tab
, ineq
, strict
);
2508 /* Check whether "ineq" can be added to the tableau without rendering
2511 static int context_lex_test_ineq(struct isl_context
*context
, isl_int
*ineq
)
2513 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2514 struct isl_tab_undo
*snap
;
2520 if (isl_tab_extend_cons(clex
->tab
, 1) < 0)
2523 snap
= isl_tab_snap(clex
->tab
);
2524 if (isl_tab_push_basis(clex
->tab
) < 0)
2526 clex
->tab
= add_lexmin_ineq(clex
->tab
, ineq
);
2527 clex
->tab
= check_integer_feasible(clex
->tab
);
2530 feasible
= !clex
->tab
->empty
;
2531 if (isl_tab_rollback(clex
->tab
, snap
) < 0)
2537 static int context_lex_get_div(struct isl_context
*context
, struct isl_tab
*tab
,
2538 struct isl_vec
*div
)
2540 return get_div(tab
, context
, div
);
2543 /* Insert a div specified by "div" to the context tableau at position "pos" and
2544 * return isl_bool_true if the div is obviously non-negative.
2545 * context_tab_add_div will always return isl_bool_true, because all variables
2546 * in a isl_context_lex tableau are non-negative.
2547 * However, if we are using a big parameter in the context, then this only
2548 * reflects the non-negativity of the variable used to _encode_ the
2549 * div, i.e., div' = M + div, so we can't draw any conclusions.
2551 static isl_bool
context_lex_insert_div(struct isl_context
*context
, int pos
,
2552 __isl_keep isl_vec
*div
)
2554 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2556 nonneg
= context_tab_insert_div(clex
->tab
, pos
, div
,
2557 context_lex_add_ineq_wrap
, context
);
2559 return isl_bool_error
;
2561 return isl_bool_false
;
2565 static int context_lex_detect_equalities(struct isl_context
*context
,
2566 struct isl_tab
*tab
)
2571 static int context_lex_best_split(struct isl_context
*context
,
2572 struct isl_tab
*tab
)
2574 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2575 struct isl_tab_undo
*snap
;
2578 snap
= isl_tab_snap(clex
->tab
);
2579 if (isl_tab_push_basis(clex
->tab
) < 0)
2581 r
= best_split(tab
, clex
->tab
);
2583 if (r
>= 0 && isl_tab_rollback(clex
->tab
, snap
) < 0)
2589 static int context_lex_is_empty(struct isl_context
*context
)
2591 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2594 return clex
->tab
->empty
;
2597 static void *context_lex_save(struct isl_context
*context
)
2599 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2600 struct isl_tab_undo
*snap
;
2602 snap
= isl_tab_snap(clex
->tab
);
2603 if (isl_tab_push_basis(clex
->tab
) < 0)
2605 if (isl_tab_save_samples(clex
->tab
) < 0)
2611 static void context_lex_restore(struct isl_context
*context
, void *save
)
2613 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2614 if (isl_tab_rollback(clex
->tab
, (struct isl_tab_undo
*)save
) < 0) {
2615 isl_tab_free(clex
->tab
);
2620 static void context_lex_discard(void *save
)
2624 static int context_lex_is_ok(struct isl_context
*context
)
2626 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2630 /* For each variable in the context tableau, check if the variable can
2631 * only attain non-negative values. If so, mark the parameter as non-negative
2632 * in the main tableau. This allows for a more direct identification of some
2633 * cases of violated constraints.
2635 static struct isl_tab
*tab_detect_nonnegative_parameters(struct isl_tab
*tab
,
2636 struct isl_tab
*context_tab
)
2639 struct isl_tab_undo
*snap
;
2640 struct isl_vec
*ineq
= NULL
;
2641 struct isl_tab_var
*var
;
2644 if (context_tab
->n_var
== 0)
2647 ineq
= isl_vec_alloc(tab
->mat
->ctx
, 1 + context_tab
->n_var
);
2651 if (isl_tab_extend_cons(context_tab
, 1) < 0)
2654 snap
= isl_tab_snap(context_tab
);
2657 isl_seq_clr(ineq
->el
, ineq
->size
);
2658 for (i
= 0; i
< context_tab
->n_var
; ++i
) {
2659 isl_int_set_si(ineq
->el
[1 + i
], 1);
2660 if (isl_tab_add_ineq(context_tab
, ineq
->el
) < 0)
2662 var
= &context_tab
->con
[context_tab
->n_con
- 1];
2663 if (!context_tab
->empty
&&
2664 !isl_tab_min_at_most_neg_one(context_tab
, var
)) {
2666 if (i
>= tab
->n_param
)
2667 j
= i
- tab
->n_param
+ tab
->n_var
- tab
->n_div
;
2668 tab
->var
[j
].is_nonneg
= 1;
2671 isl_int_set_si(ineq
->el
[1 + i
], 0);
2672 if (isl_tab_rollback(context_tab
, snap
) < 0)
2676 if (context_tab
->M
&& n
== context_tab
->n_var
) {
2677 context_tab
->mat
= isl_mat_drop_cols(context_tab
->mat
, 2, 1);
2689 static struct isl_tab
*context_lex_detect_nonnegative_parameters(
2690 struct isl_context
*context
, struct isl_tab
*tab
)
2692 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2693 struct isl_tab_undo
*snap
;
2698 snap
= isl_tab_snap(clex
->tab
);
2699 if (isl_tab_push_basis(clex
->tab
) < 0)
2702 tab
= tab_detect_nonnegative_parameters(tab
, clex
->tab
);
2704 if (isl_tab_rollback(clex
->tab
, snap
) < 0)
2713 static void context_lex_invalidate(struct isl_context
*context
)
2715 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2716 isl_tab_free(clex
->tab
);
2720 static __isl_null
struct isl_context
*context_lex_free(
2721 struct isl_context
*context
)
2723 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2724 isl_tab_free(clex
->tab
);
2730 struct isl_context_op isl_context_lex_op
= {
2731 context_lex_detect_nonnegative_parameters
,
2732 context_lex_peek_basic_set
,
2733 context_lex_peek_tab
,
2735 context_lex_add_ineq
,
2736 context_lex_ineq_sign
,
2737 context_lex_test_ineq
,
2738 context_lex_get_div
,
2739 context_lex_insert_div
,
2740 context_lex_detect_equalities
,
2741 context_lex_best_split
,
2742 context_lex_is_empty
,
2745 context_lex_restore
,
2746 context_lex_discard
,
2747 context_lex_invalidate
,
2751 static struct isl_tab
*context_tab_for_lexmin(struct isl_basic_set
*bset
)
2753 struct isl_tab
*tab
;
2757 tab
= tab_for_lexmin(bset_to_bmap(bset
), NULL
, 1, 0);
2758 if (isl_tab_track_bset(tab
, bset
) < 0)
2760 tab
= isl_tab_init_samples(tab
);
2767 static struct isl_context
*isl_context_lex_alloc(struct isl_basic_set
*dom
)
2769 struct isl_context_lex
*clex
;
2774 clex
= isl_alloc_type(dom
->ctx
, struct isl_context_lex
);
2778 clex
->context
.op
= &isl_context_lex_op
;
2780 clex
->tab
= context_tab_for_lexmin(isl_basic_set_copy(dom
));
2781 if (restore_lexmin(clex
->tab
) < 0)
2783 clex
->tab
= check_integer_feasible(clex
->tab
);
2787 return &clex
->context
;
2789 clex
->context
.op
->free(&clex
->context
);
2793 /* Representation of the context when using generalized basis reduction.
2795 * "shifted" contains the offsets of the unit hypercubes that lie inside the
2796 * context. Any rational point in "shifted" can therefore be rounded
2797 * up to an integer point in the context.
2798 * If the context is constrained by any equality, then "shifted" is not used
2799 * as it would be empty.
2801 struct isl_context_gbr
{
2802 struct isl_context context
;
2803 struct isl_tab
*tab
;
2804 struct isl_tab
*shifted
;
2805 struct isl_tab
*cone
;
2808 static struct isl_tab
*context_gbr_detect_nonnegative_parameters(
2809 struct isl_context
*context
, struct isl_tab
*tab
)
2811 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2814 return tab_detect_nonnegative_parameters(tab
, cgbr
->tab
);
2817 static struct isl_basic_set
*context_gbr_peek_basic_set(
2818 struct isl_context
*context
)
2820 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2823 return isl_tab_peek_bset(cgbr
->tab
);
2826 static struct isl_tab
*context_gbr_peek_tab(struct isl_context
*context
)
2828 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2832 /* Initialize the "shifted" tableau of the context, which
2833 * contains the constraints of the original tableau shifted
2834 * by the sum of all negative coefficients. This ensures
2835 * that any rational point in the shifted tableau can
2836 * be rounded up to yield an integer point in the original tableau.
2838 static void gbr_init_shifted(struct isl_context_gbr
*cgbr
)
2841 struct isl_vec
*cst
;
2842 struct isl_basic_set
*bset
= isl_tab_peek_bset(cgbr
->tab
);
2843 unsigned dim
= isl_basic_set_total_dim(bset
);
2845 cst
= isl_vec_alloc(cgbr
->tab
->mat
->ctx
, bset
->n_ineq
);
2849 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
2850 isl_int_set(cst
->el
[i
], bset
->ineq
[i
][0]);
2851 for (j
= 0; j
< dim
; ++j
) {
2852 if (!isl_int_is_neg(bset
->ineq
[i
][1 + j
]))
2854 isl_int_add(bset
->ineq
[i
][0], bset
->ineq
[i
][0],
2855 bset
->ineq
[i
][1 + j
]);
2859 cgbr
->shifted
= isl_tab_from_basic_set(bset
, 0);
2861 for (i
= 0; i
< bset
->n_ineq
; ++i
)
2862 isl_int_set(bset
->ineq
[i
][0], cst
->el
[i
]);
2867 /* Check if the shifted tableau is non-empty, and if so
2868 * use the sample point to construct an integer point
2869 * of the context tableau.
2871 static struct isl_vec
*gbr_get_shifted_sample(struct isl_context_gbr
*cgbr
)
2873 struct isl_vec
*sample
;
2876 gbr_init_shifted(cgbr
);
2879 if (cgbr
->shifted
->empty
)
2880 return isl_vec_alloc(cgbr
->tab
->mat
->ctx
, 0);
2882 sample
= isl_tab_get_sample_value(cgbr
->shifted
);
2883 sample
= isl_vec_ceil(sample
);
2888 static struct isl_basic_set
*drop_constant_terms(struct isl_basic_set
*bset
)
2895 for (i
= 0; i
< bset
->n_eq
; ++i
)
2896 isl_int_set_si(bset
->eq
[i
][0], 0);
2898 for (i
= 0; i
< bset
->n_ineq
; ++i
)
2899 isl_int_set_si(bset
->ineq
[i
][0], 0);
2904 static int use_shifted(struct isl_context_gbr
*cgbr
)
2908 return cgbr
->tab
->bmap
->n_eq
== 0 && cgbr
->tab
->bmap
->n_div
== 0;
2911 static struct isl_vec
*gbr_get_sample(struct isl_context_gbr
*cgbr
)
2913 struct isl_basic_set
*bset
;
2914 struct isl_basic_set
*cone
;
2916 if (isl_tab_sample_is_integer(cgbr
->tab
))
2917 return isl_tab_get_sample_value(cgbr
->tab
);
2919 if (use_shifted(cgbr
)) {
2920 struct isl_vec
*sample
;
2922 sample
= gbr_get_shifted_sample(cgbr
);
2923 if (!sample
|| sample
->size
> 0)
2926 isl_vec_free(sample
);
2930 bset
= isl_tab_peek_bset(cgbr
->tab
);
2931 cgbr
->cone
= isl_tab_from_recession_cone(bset
, 0);
2934 if (isl_tab_track_bset(cgbr
->cone
,
2935 isl_basic_set_copy(bset
)) < 0)
2938 if (isl_tab_detect_implicit_equalities(cgbr
->cone
) < 0)
2941 if (cgbr
->cone
->n_dead
== cgbr
->cone
->n_col
) {
2942 struct isl_vec
*sample
;
2943 struct isl_tab_undo
*snap
;
2945 if (cgbr
->tab
->basis
) {
2946 if (cgbr
->tab
->basis
->n_col
!= 1 + cgbr
->tab
->n_var
) {
2947 isl_mat_free(cgbr
->tab
->basis
);
2948 cgbr
->tab
->basis
= NULL
;
2950 cgbr
->tab
->n_zero
= 0;
2951 cgbr
->tab
->n_unbounded
= 0;
2954 snap
= isl_tab_snap(cgbr
->tab
);
2956 sample
= isl_tab_sample(cgbr
->tab
);
2958 if (!sample
|| isl_tab_rollback(cgbr
->tab
, snap
) < 0) {
2959 isl_vec_free(sample
);
2966 cone
= isl_basic_set_dup(isl_tab_peek_bset(cgbr
->cone
));
2967 cone
= drop_constant_terms(cone
);
2968 cone
= isl_basic_set_update_from_tab(cone
, cgbr
->cone
);
2969 cone
= isl_basic_set_underlying_set(cone
);
2970 cone
= isl_basic_set_gauss(cone
, NULL
);
2972 bset
= isl_basic_set_dup(isl_tab_peek_bset(cgbr
->tab
));
2973 bset
= isl_basic_set_update_from_tab(bset
, cgbr
->tab
);
2974 bset
= isl_basic_set_underlying_set(bset
);
2975 bset
= isl_basic_set_gauss(bset
, NULL
);
2977 return isl_basic_set_sample_with_cone(bset
, cone
);
2980 static void check_gbr_integer_feasible(struct isl_context_gbr
*cgbr
)
2982 struct isl_vec
*sample
;
2987 if (cgbr
->tab
->empty
)
2990 sample
= gbr_get_sample(cgbr
);
2994 if (sample
->size
== 0) {
2995 isl_vec_free(sample
);
2996 if (isl_tab_mark_empty(cgbr
->tab
) < 0)
3001 if (isl_tab_add_sample(cgbr
->tab
, sample
) < 0)
3006 isl_tab_free(cgbr
->tab
);
3010 static struct isl_tab
*add_gbr_eq(struct isl_tab
*tab
, isl_int
*eq
)
3015 if (isl_tab_extend_cons(tab
, 2) < 0)
3018 if (isl_tab_add_eq(tab
, eq
) < 0)
3027 /* Add the equality described by "eq" to the context.
3028 * If "check" is set, then we check if the context is empty after
3029 * adding the equality.
3030 * If "update" is set, then we check if the samples are still valid.
3032 * We do not explicitly add shifted copies of the equality to
3033 * cgbr->shifted since they would conflict with each other.
3034 * Instead, we directly mark cgbr->shifted empty.
3036 static void context_gbr_add_eq(struct isl_context
*context
, isl_int
*eq
,
3037 int check
, int update
)
3039 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3041 cgbr
->tab
= add_gbr_eq(cgbr
->tab
, eq
);
3043 if (cgbr
->shifted
&& !cgbr
->shifted
->empty
&& use_shifted(cgbr
)) {
3044 if (isl_tab_mark_empty(cgbr
->shifted
) < 0)
3048 if (cgbr
->cone
&& cgbr
->cone
->n_col
!= cgbr
->cone
->n_dead
) {
3049 if (isl_tab_extend_cons(cgbr
->cone
, 2) < 0)
3051 if (isl_tab_add_eq(cgbr
->cone
, eq
) < 0)
3056 int v
= tab_has_valid_sample(cgbr
->tab
, eq
, 1);
3060 check_gbr_integer_feasible(cgbr
);
3063 cgbr
->tab
= check_samples(cgbr
->tab
, eq
, 1);
3066 isl_tab_free(cgbr
->tab
);
3070 static void add_gbr_ineq(struct isl_context_gbr
*cgbr
, isl_int
*ineq
)
3075 if (isl_tab_extend_cons(cgbr
->tab
, 1) < 0)
3078 if (isl_tab_add_ineq(cgbr
->tab
, ineq
) < 0)
3081 if (cgbr
->shifted
&& !cgbr
->shifted
->empty
&& use_shifted(cgbr
)) {
3084 dim
= isl_basic_map_total_dim(cgbr
->tab
->bmap
);
3086 if (isl_tab_extend_cons(cgbr
->shifted
, 1) < 0)
3089 for (i
= 0; i
< dim
; ++i
) {
3090 if (!isl_int_is_neg(ineq
[1 + i
]))
3092 isl_int_add(ineq
[0], ineq
[0], ineq
[1 + i
]);
3095 if (isl_tab_add_ineq(cgbr
->shifted
, ineq
) < 0)
3098 for (i
= 0; i
< dim
; ++i
) {
3099 if (!isl_int_is_neg(ineq
[1 + i
]))
3101 isl_int_sub(ineq
[0], ineq
[0], ineq
[1 + i
]);
3105 if (cgbr
->cone
&& cgbr
->cone
->n_col
!= cgbr
->cone
->n_dead
) {
3106 if (isl_tab_extend_cons(cgbr
->cone
, 1) < 0)
3108 if (isl_tab_add_ineq(cgbr
->cone
, ineq
) < 0)
3114 isl_tab_free(cgbr
->tab
);
3118 static void context_gbr_add_ineq(struct isl_context
*context
, isl_int
*ineq
,
3119 int check
, int update
)
3121 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3123 add_gbr_ineq(cgbr
, ineq
);
3128 int v
= tab_has_valid_sample(cgbr
->tab
, ineq
, 0);
3132 check_gbr_integer_feasible(cgbr
);
3135 cgbr
->tab
= check_samples(cgbr
->tab
, ineq
, 0);
3138 isl_tab_free(cgbr
->tab
);
3142 static isl_stat
context_gbr_add_ineq_wrap(void *user
, isl_int
*ineq
)
3144 struct isl_context
*context
= (struct isl_context
*)user
;
3145 context_gbr_add_ineq(context
, ineq
, 0, 0);
3146 return context
->op
->is_ok(context
) ? isl_stat_ok
: isl_stat_error
;
3149 static enum isl_tab_row_sign
context_gbr_ineq_sign(struct isl_context
*context
,
3150 isl_int
*ineq
, int strict
)
3152 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3153 return tab_ineq_sign(cgbr
->tab
, ineq
, strict
);
3156 /* Check whether "ineq" can be added to the tableau without rendering
3159 static int context_gbr_test_ineq(struct isl_context
*context
, isl_int
*ineq
)
3161 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3162 struct isl_tab_undo
*snap
;
3163 struct isl_tab_undo
*shifted_snap
= NULL
;
3164 struct isl_tab_undo
*cone_snap
= NULL
;
3170 if (isl_tab_extend_cons(cgbr
->tab
, 1) < 0)
3173 snap
= isl_tab_snap(cgbr
->tab
);
3175 shifted_snap
= isl_tab_snap(cgbr
->shifted
);
3177 cone_snap
= isl_tab_snap(cgbr
->cone
);
3178 add_gbr_ineq(cgbr
, ineq
);
3179 check_gbr_integer_feasible(cgbr
);
3182 feasible
= !cgbr
->tab
->empty
;
3183 if (isl_tab_rollback(cgbr
->tab
, snap
) < 0)
3186 if (isl_tab_rollback(cgbr
->shifted
, shifted_snap
))
3188 } else if (cgbr
->shifted
) {
3189 isl_tab_free(cgbr
->shifted
);
3190 cgbr
->shifted
= NULL
;
3193 if (isl_tab_rollback(cgbr
->cone
, cone_snap
))
3195 } else if (cgbr
->cone
) {
3196 isl_tab_free(cgbr
->cone
);
3203 /* Return the column of the last of the variables associated to
3204 * a column that has a non-zero coefficient.
3205 * This function is called in a context where only coefficients
3206 * of parameters or divs can be non-zero.
3208 static int last_non_zero_var_col(struct isl_tab
*tab
, isl_int
*p
)
3213 if (tab
->n_var
== 0)
3216 for (i
= tab
->n_var
- 1; i
>= 0; --i
) {
3217 if (i
>= tab
->n_param
&& i
< tab
->n_var
- tab
->n_div
)
3219 if (tab
->var
[i
].is_row
)
3221 col
= tab
->var
[i
].index
;
3222 if (!isl_int_is_zero(p
[col
]))
3229 /* Look through all the recently added equalities in the context
3230 * to see if we can propagate any of them to the main tableau.
3232 * The newly added equalities in the context are encoded as pairs
3233 * of inequalities starting at inequality "first".
3235 * We tentatively add each of these equalities to the main tableau
3236 * and if this happens to result in a row with a final coefficient
3237 * that is one or negative one, we use it to kill a column
3238 * in the main tableau. Otherwise, we discard the tentatively
3240 * This tentative addition of equality constraints turns
3241 * on the undo facility of the tableau. Turn it off again
3242 * at the end, assuming it was turned off to begin with.
3244 * Return 0 on success and -1 on failure.
3246 static int propagate_equalities(struct isl_context_gbr
*cgbr
,
3247 struct isl_tab
*tab
, unsigned first
)
3250 struct isl_vec
*eq
= NULL
;
3251 isl_bool needs_undo
;
3253 needs_undo
= isl_tab_need_undo(tab
);
3256 eq
= isl_vec_alloc(tab
->mat
->ctx
, 1 + tab
->n_var
);
3260 if (isl_tab_extend_cons(tab
, (cgbr
->tab
->bmap
->n_ineq
- first
)/2) < 0)
3263 isl_seq_clr(eq
->el
+ 1 + tab
->n_param
,
3264 tab
->n_var
- tab
->n_param
- tab
->n_div
);
3265 for (i
= first
; i
< cgbr
->tab
->bmap
->n_ineq
; i
+= 2) {
3268 struct isl_tab_undo
*snap
;
3269 snap
= isl_tab_snap(tab
);
3271 isl_seq_cpy(eq
->el
, cgbr
->tab
->bmap
->ineq
[i
], 1 + tab
->n_param
);
3272 isl_seq_cpy(eq
->el
+ 1 + tab
->n_var
- tab
->n_div
,
3273 cgbr
->tab
->bmap
->ineq
[i
] + 1 + tab
->n_param
,
3276 r
= isl_tab_add_row(tab
, eq
->el
);
3279 r
= tab
->con
[r
].index
;
3280 j
= last_non_zero_var_col(tab
, tab
->mat
->row
[r
] + 2 + tab
->M
);
3281 if (j
< 0 || j
< tab
->n_dead
||
3282 !isl_int_is_one(tab
->mat
->row
[r
][0]) ||
3283 (!isl_int_is_one(tab
->mat
->row
[r
][2 + tab
->M
+ j
]) &&
3284 !isl_int_is_negone(tab
->mat
->row
[r
][2 + tab
->M
+ j
]))) {
3285 if (isl_tab_rollback(tab
, snap
) < 0)
3289 if (isl_tab_pivot(tab
, r
, j
) < 0)
3291 if (isl_tab_kill_col(tab
, j
) < 0)
3294 if (restore_lexmin(tab
) < 0)
3299 isl_tab_clear_undo(tab
);
3305 isl_tab_free(cgbr
->tab
);
3310 static int context_gbr_detect_equalities(struct isl_context
*context
,
3311 struct isl_tab
*tab
)
3313 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3317 struct isl_basic_set
*bset
= isl_tab_peek_bset(cgbr
->tab
);
3318 cgbr
->cone
= isl_tab_from_recession_cone(bset
, 0);
3321 if (isl_tab_track_bset(cgbr
->cone
,
3322 isl_basic_set_copy(bset
)) < 0)
3325 if (isl_tab_detect_implicit_equalities(cgbr
->cone
) < 0)
3328 n_ineq
= cgbr
->tab
->bmap
->n_ineq
;
3329 cgbr
->tab
= isl_tab_detect_equalities(cgbr
->tab
, cgbr
->cone
);
3332 if (cgbr
->tab
->bmap
->n_ineq
> n_ineq
&&
3333 propagate_equalities(cgbr
, tab
, n_ineq
) < 0)
3338 isl_tab_free(cgbr
->tab
);
3343 static int context_gbr_get_div(struct isl_context
*context
, struct isl_tab
*tab
,
3344 struct isl_vec
*div
)
3346 return get_div(tab
, context
, div
);
3349 static isl_bool
context_gbr_insert_div(struct isl_context
*context
, int pos
,
3350 __isl_keep isl_vec
*div
)
3352 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3354 int r
, n_div
, o_div
;
3356 n_div
= isl_basic_map_dim(cgbr
->cone
->bmap
, isl_dim_div
);
3357 o_div
= cgbr
->cone
->n_var
- n_div
;
3359 if (isl_tab_extend_cons(cgbr
->cone
, 3) < 0)
3360 return isl_bool_error
;
3361 if (isl_tab_extend_vars(cgbr
->cone
, 1) < 0)
3362 return isl_bool_error
;
3363 if ((r
= isl_tab_insert_var(cgbr
->cone
, pos
)) <0)
3364 return isl_bool_error
;
3366 cgbr
->cone
->bmap
= isl_basic_map_insert_div(cgbr
->cone
->bmap
,
3368 if (!cgbr
->cone
->bmap
)
3369 return isl_bool_error
;
3370 if (isl_tab_push_var(cgbr
->cone
, isl_tab_undo_bmap_div
,
3371 &cgbr
->cone
->var
[r
]) < 0)
3372 return isl_bool_error
;
3374 return context_tab_insert_div(cgbr
->tab
, pos
, div
,
3375 context_gbr_add_ineq_wrap
, context
);
3378 static int context_gbr_best_split(struct isl_context
*context
,
3379 struct isl_tab
*tab
)
3381 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3382 struct isl_tab_undo
*snap
;
3385 snap
= isl_tab_snap(cgbr
->tab
);
3386 r
= best_split(tab
, cgbr
->tab
);
3388 if (r
>= 0 && isl_tab_rollback(cgbr
->tab
, snap
) < 0)
3394 static int context_gbr_is_empty(struct isl_context
*context
)
3396 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3399 return cgbr
->tab
->empty
;
3402 struct isl_gbr_tab_undo
{
3403 struct isl_tab_undo
*tab_snap
;
3404 struct isl_tab_undo
*shifted_snap
;
3405 struct isl_tab_undo
*cone_snap
;
3408 static void *context_gbr_save(struct isl_context
*context
)
3410 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3411 struct isl_gbr_tab_undo
*snap
;
3416 snap
= isl_alloc_type(cgbr
->tab
->mat
->ctx
, struct isl_gbr_tab_undo
);
3420 snap
->tab_snap
= isl_tab_snap(cgbr
->tab
);
3421 if (isl_tab_save_samples(cgbr
->tab
) < 0)
3425 snap
->shifted_snap
= isl_tab_snap(cgbr
->shifted
);
3427 snap
->shifted_snap
= NULL
;
3430 snap
->cone_snap
= isl_tab_snap(cgbr
->cone
);
3432 snap
->cone_snap
= NULL
;
3440 static void context_gbr_restore(struct isl_context
*context
, void *save
)
3442 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3443 struct isl_gbr_tab_undo
*snap
= (struct isl_gbr_tab_undo
*)save
;
3446 if (isl_tab_rollback(cgbr
->tab
, snap
->tab_snap
) < 0)
3449 if (snap
->shifted_snap
) {
3450 if (isl_tab_rollback(cgbr
->shifted
, snap
->shifted_snap
) < 0)
3452 } else if (cgbr
->shifted
) {
3453 isl_tab_free(cgbr
->shifted
);
3454 cgbr
->shifted
= NULL
;
3457 if (snap
->cone_snap
) {
3458 if (isl_tab_rollback(cgbr
->cone
, snap
->cone_snap
) < 0)
3460 } else if (cgbr
->cone
) {
3461 isl_tab_free(cgbr
->cone
);
3470 isl_tab_free(cgbr
->tab
);
3474 static void context_gbr_discard(void *save
)
3476 struct isl_gbr_tab_undo
*snap
= (struct isl_gbr_tab_undo
*)save
;
3480 static int context_gbr_is_ok(struct isl_context
*context
)
3482 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3486 static void context_gbr_invalidate(struct isl_context
*context
)
3488 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3489 isl_tab_free(cgbr
->tab
);
3493 static __isl_null
struct isl_context
*context_gbr_free(
3494 struct isl_context
*context
)
3496 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3497 isl_tab_free(cgbr
->tab
);
3498 isl_tab_free(cgbr
->shifted
);
3499 isl_tab_free(cgbr
->cone
);
3505 struct isl_context_op isl_context_gbr_op
= {
3506 context_gbr_detect_nonnegative_parameters
,
3507 context_gbr_peek_basic_set
,
3508 context_gbr_peek_tab
,
3510 context_gbr_add_ineq
,
3511 context_gbr_ineq_sign
,
3512 context_gbr_test_ineq
,
3513 context_gbr_get_div
,
3514 context_gbr_insert_div
,
3515 context_gbr_detect_equalities
,
3516 context_gbr_best_split
,
3517 context_gbr_is_empty
,
3520 context_gbr_restore
,
3521 context_gbr_discard
,
3522 context_gbr_invalidate
,
3526 static struct isl_context
*isl_context_gbr_alloc(__isl_keep isl_basic_set
*dom
)
3528 struct isl_context_gbr
*cgbr
;
3533 cgbr
= isl_calloc_type(dom
->ctx
, struct isl_context_gbr
);
3537 cgbr
->context
.op
= &isl_context_gbr_op
;
3539 cgbr
->shifted
= NULL
;
3541 cgbr
->tab
= isl_tab_from_basic_set(dom
, 1);
3542 cgbr
->tab
= isl_tab_init_samples(cgbr
->tab
);
3545 check_gbr_integer_feasible(cgbr
);
3547 return &cgbr
->context
;
3549 cgbr
->context
.op
->free(&cgbr
->context
);
3553 /* Allocate a context corresponding to "dom".
3554 * The representation specific fields are initialized by
3555 * isl_context_lex_alloc or isl_context_gbr_alloc.
3556 * The shared "n_unknown" field is initialized to the number
3557 * of final unknown integer divisions in "dom".
3559 static struct isl_context
*isl_context_alloc(__isl_keep isl_basic_set
*dom
)
3561 struct isl_context
*context
;
3567 if (dom
->ctx
->opt
->context
== ISL_CONTEXT_LEXMIN
)
3568 context
= isl_context_lex_alloc(dom
);
3570 context
= isl_context_gbr_alloc(dom
);
3575 first
= isl_basic_set_first_unknown_div(dom
);
3577 return context
->op
->free(context
);
3578 context
->n_unknown
= isl_basic_set_dim(dom
, isl_dim_div
) - first
;
3583 /* Initialize some common fields of "sol", which keeps track
3584 * of the solution of an optimization problem on "bmap" over
3586 * If "max" is set, then a maximization problem is being solved, rather than
3587 * a minimization problem, which means that the variables in the
3588 * tableau have value "M - x" rather than "M + x".
3590 static isl_stat
sol_init(struct isl_sol
*sol
, __isl_keep isl_basic_map
*bmap
,
3591 __isl_keep isl_basic_set
*dom
, int max
)
3593 sol
->rational
= ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
3594 sol
->dec_level
.callback
.run
= &sol_dec_level_wrap
;
3595 sol
->dec_level
.sol
= sol
;
3597 sol
->n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
3598 sol
->space
= isl_basic_map_get_space(bmap
);
3600 sol
->context
= isl_context_alloc(dom
);
3601 if (!sol
->space
|| !sol
->context
)
3602 return isl_stat_error
;
3607 /* Construct an isl_sol_map structure for accumulating the solution.
3608 * If track_empty is set, then we also keep track of the parts
3609 * of the context where there is no solution.
3610 * If max is set, then we are solving a maximization, rather than
3611 * a minimization problem, which means that the variables in the
3612 * tableau have value "M - x" rather than "M + x".
3614 static struct isl_sol
*sol_map_init(__isl_keep isl_basic_map
*bmap
,
3615 __isl_take isl_basic_set
*dom
, int track_empty
, int max
)
3617 struct isl_sol_map
*sol_map
= NULL
;
3623 sol_map
= isl_calloc_type(bmap
->ctx
, struct isl_sol_map
);
3627 sol_map
->sol
.free
= &sol_map_free
;
3628 if (sol_init(&sol_map
->sol
, bmap
, dom
, max
) < 0)
3630 sol_map
->sol
.add
= &sol_map_add_wrap
;
3631 sol_map
->sol
.add_empty
= track_empty
? &sol_map_add_empty_wrap
: NULL
;
3632 space
= isl_space_copy(sol_map
->sol
.space
);
3633 sol_map
->map
= isl_map_alloc_space(space
, 1, ISL_MAP_DISJOINT
);
3638 sol_map
->empty
= isl_set_alloc_space(isl_basic_set_get_space(dom
),
3639 1, ISL_SET_DISJOINT
);
3640 if (!sol_map
->empty
)
3644 isl_basic_set_free(dom
);
3645 return &sol_map
->sol
;
3647 isl_basic_set_free(dom
);
3648 sol_free(&sol_map
->sol
);
3652 /* Check whether all coefficients of (non-parameter) variables
3653 * are non-positive, meaning that no pivots can be performed on the row.
3655 static int is_critical(struct isl_tab
*tab
, int row
)
3658 unsigned off
= 2 + tab
->M
;
3660 for (j
= tab
->n_dead
; j
< tab
->n_col
; ++j
) {
3661 if (tab
->col_var
[j
] >= 0 &&
3662 (tab
->col_var
[j
] < tab
->n_param
||
3663 tab
->col_var
[j
] >= tab
->n_var
- tab
->n_div
))
3666 if (isl_int_is_pos(tab
->mat
->row
[row
][off
+ j
]))
3673 /* Check whether the inequality represented by vec is strict over the integers,
3674 * i.e., there are no integer values satisfying the constraint with
3675 * equality. This happens if the gcd of the coefficients is not a divisor
3676 * of the constant term. If so, scale the constraint down by the gcd
3677 * of the coefficients.
3679 static int is_strict(struct isl_vec
*vec
)
3685 isl_seq_gcd(vec
->el
+ 1, vec
->size
- 1, &gcd
);
3686 if (!isl_int_is_one(gcd
)) {
3687 strict
= !isl_int_is_divisible_by(vec
->el
[0], gcd
);
3688 isl_int_fdiv_q(vec
->el
[0], vec
->el
[0], gcd
);
3689 isl_seq_scale_down(vec
->el
+ 1, vec
->el
+ 1, gcd
, vec
->size
-1);
3696 /* Determine the sign of the given row of the main tableau.
3697 * The result is one of
3698 * isl_tab_row_pos: always non-negative; no pivot needed
3699 * isl_tab_row_neg: always non-positive; pivot
3700 * isl_tab_row_any: can be both positive and negative; split
3702 * We first handle some simple cases
3703 * - the row sign may be known already
3704 * - the row may be obviously non-negative
3705 * - the parametric constant may be equal to that of another row
3706 * for which we know the sign. This sign will be either "pos" or
3707 * "any". If it had been "neg" then we would have pivoted before.
3709 * If none of these cases hold, we check the value of the row for each
3710 * of the currently active samples. Based on the signs of these values
3711 * we make an initial determination of the sign of the row.
3713 * all zero -> unk(nown)
3714 * all non-negative -> pos
3715 * all non-positive -> neg
3716 * both negative and positive -> all
3718 * If we end up with "all", we are done.
3719 * Otherwise, we perform a check for positive and/or negative
3720 * values as follows.
3722 * samples neg unk pos
3728 * There is no special sign for "zero", because we can usually treat zero
3729 * as either non-negative or non-positive, whatever works out best.
3730 * However, if the row is "critical", meaning that pivoting is impossible
3731 * then we don't want to limp zero with the non-positive case, because
3732 * then we we would lose the solution for those values of the parameters
3733 * where the value of the row is zero. Instead, we treat 0 as non-negative
3734 * ensuring a split if the row can attain both zero and negative values.
3735 * The same happens when the original constraint was one that could not
3736 * be satisfied with equality by any integer values of the parameters.
3737 * In this case, we normalize the constraint, but then a value of zero
3738 * for the normalized constraint is actually a positive value for the
3739 * original constraint, so again we need to treat zero as non-negative.
3740 * In both these cases, we have the following decision tree instead:
3742 * all non-negative -> pos
3743 * all negative -> neg
3744 * both negative and non-negative -> all
3752 static enum isl_tab_row_sign
row_sign(struct isl_tab
*tab
,
3753 struct isl_sol
*sol
, int row
)
3755 struct isl_vec
*ineq
= NULL
;
3756 enum isl_tab_row_sign res
= isl_tab_row_unknown
;
3761 if (tab
->row_sign
[row
] != isl_tab_row_unknown
)
3762 return tab
->row_sign
[row
];
3763 if (is_obviously_nonneg(tab
, row
))
3764 return isl_tab_row_pos
;
3765 for (row2
= tab
->n_redundant
; row2
< tab
->n_row
; ++row2
) {
3766 if (tab
->row_sign
[row2
] == isl_tab_row_unknown
)
3768 if (identical_parameter_line(tab
, row
, row2
))
3769 return tab
->row_sign
[row2
];
3772 critical
= is_critical(tab
, row
);
3774 ineq
= get_row_parameter_ineq(tab
, row
);
3778 strict
= is_strict(ineq
);
3780 res
= sol
->context
->op
->ineq_sign(sol
->context
, ineq
->el
,
3781 critical
|| strict
);
3783 if (res
== isl_tab_row_unknown
|| res
== isl_tab_row_pos
) {
3784 /* test for negative values */
3786 isl_seq_neg(ineq
->el
, ineq
->el
, ineq
->size
);
3787 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3789 feasible
= sol
->context
->op
->test_ineq(sol
->context
, ineq
->el
);
3793 res
= isl_tab_row_pos
;
3795 res
= (res
== isl_tab_row_unknown
) ? isl_tab_row_neg
3797 if (res
== isl_tab_row_neg
) {
3798 isl_seq_neg(ineq
->el
, ineq
->el
, ineq
->size
);
3799 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3803 if (res
== isl_tab_row_neg
) {
3804 /* test for positive values */
3806 if (!critical
&& !strict
)
3807 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3809 feasible
= sol
->context
->op
->test_ineq(sol
->context
, ineq
->el
);
3813 res
= isl_tab_row_any
;
3820 return isl_tab_row_unknown
;
3823 static void find_solutions(struct isl_sol
*sol
, struct isl_tab
*tab
);
3825 /* Find solutions for values of the parameters that satisfy the given
3828 * We currently take a snapshot of the context tableau that is reset
3829 * when we return from this function, while we make a copy of the main
3830 * tableau, leaving the original main tableau untouched.
3831 * These are fairly arbitrary choices. Making a copy also of the context
3832 * tableau would obviate the need to undo any changes made to it later,
3833 * while taking a snapshot of the main tableau could reduce memory usage.
3834 * If we were to switch to taking a snapshot of the main tableau,
3835 * we would have to keep in mind that we need to save the row signs
3836 * and that we need to do this before saving the current basis
3837 * such that the basis has been restore before we restore the row signs.
3839 static void find_in_pos(struct isl_sol
*sol
, struct isl_tab
*tab
, isl_int
*ineq
)
3845 saved
= sol
->context
->op
->save(sol
->context
);
3847 tab
= isl_tab_dup(tab
);
3851 sol
->context
->op
->add_ineq(sol
->context
, ineq
, 0, 1);
3853 find_solutions(sol
, tab
);
3856 sol
->context
->op
->restore(sol
->context
, saved
);
3858 sol
->context
->op
->discard(saved
);
3864 /* Record the absence of solutions for those values of the parameters
3865 * that do not satisfy the given inequality with equality.
3867 static void no_sol_in_strict(struct isl_sol
*sol
,
3868 struct isl_tab
*tab
, struct isl_vec
*ineq
)
3873 if (!sol
->context
|| sol
->error
)
3875 saved
= sol
->context
->op
->save(sol
->context
);
3877 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3879 sol
->context
->op
->add_ineq(sol
->context
, ineq
->el
, 1, 0);
3888 isl_int_add_ui(ineq
->el
[0], ineq
->el
[0], 1);
3890 sol
->context
->op
->restore(sol
->context
, saved
);
3896 /* Reset all row variables that are marked to have a sign that may
3897 * be both positive and negative to have an unknown sign.
3899 static void reset_any_to_unknown(struct isl_tab
*tab
)
3903 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
3904 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
3906 if (tab
->row_sign
[row
] == isl_tab_row_any
)
3907 tab
->row_sign
[row
] = isl_tab_row_unknown
;
3911 /* Compute the lexicographic minimum of the set represented by the main
3912 * tableau "tab" within the context "sol->context_tab".
3913 * On entry the sample value of the main tableau is lexicographically
3914 * less than or equal to this lexicographic minimum.
3915 * Pivots are performed until a feasible point is found, which is then
3916 * necessarily equal to the minimum, or until the tableau is found to
3917 * be infeasible. Some pivots may need to be performed for only some
3918 * feasible values of the context tableau. If so, the context tableau
3919 * is split into a part where the pivot is needed and a part where it is not.
3921 * Whenever we enter the main loop, the main tableau is such that no
3922 * "obvious" pivots need to be performed on it, where "obvious" means
3923 * that the given row can be seen to be negative without looking at
3924 * the context tableau. In particular, for non-parametric problems,
3925 * no pivots need to be performed on the main tableau.
3926 * The caller of find_solutions is responsible for making this property
3927 * hold prior to the first iteration of the loop, while restore_lexmin
3928 * is called before every other iteration.
3930 * Inside the main loop, we first examine the signs of the rows of
3931 * the main tableau within the context of the context tableau.
3932 * If we find a row that is always non-positive for all values of
3933 * the parameters satisfying the context tableau and negative for at
3934 * least one value of the parameters, we perform the appropriate pivot
3935 * and start over. An exception is the case where no pivot can be
3936 * performed on the row. In this case, we require that the sign of
3937 * the row is negative for all values of the parameters (rather than just
3938 * non-positive). This special case is handled inside row_sign, which
3939 * will say that the row can have any sign if it determines that it can
3940 * attain both negative and zero values.
3942 * If we can't find a row that always requires a pivot, but we can find
3943 * one or more rows that require a pivot for some values of the parameters
3944 * (i.e., the row can attain both positive and negative signs), then we split
3945 * the context tableau into two parts, one where we force the sign to be
3946 * non-negative and one where we force is to be negative.
3947 * The non-negative part is handled by a recursive call (through find_in_pos).
3948 * Upon returning from this call, we continue with the negative part and
3949 * perform the required pivot.
3951 * If no such rows can be found, all rows are non-negative and we have
3952 * found a (rational) feasible point. If we only wanted a rational point
3954 * Otherwise, we check if all values of the sample point of the tableau
3955 * are integral for the variables. If so, we have found the minimal
3956 * integral point and we are done.
3957 * If the sample point is not integral, then we need to make a distinction
3958 * based on whether the constant term is non-integral or the coefficients
3959 * of the parameters. Furthermore, in order to decide how to handle
3960 * the non-integrality, we also need to know whether the coefficients
3961 * of the other columns in the tableau are integral. This leads
3962 * to the following table. The first two rows do not correspond
3963 * to a non-integral sample point and are only mentioned for completeness.
3965 * constant parameters other
3968 * int int rat | -> no problem
3970 * rat int int -> fail
3972 * rat int rat -> cut
3975 * rat rat rat | -> parametric cut
3978 * rat rat int | -> split context
3980 * If the parametric constant is completely integral, then there is nothing
3981 * to be done. If the constant term is non-integral, but all the other
3982 * coefficient are integral, then there is nothing that can be done
3983 * and the tableau has no integral solution.
3984 * If, on the other hand, one or more of the other columns have rational
3985 * coefficients, but the parameter coefficients are all integral, then
3986 * we can perform a regular (non-parametric) cut.
3987 * Finally, if there is any parameter coefficient that is non-integral,
3988 * then we need to involve the context tableau. There are two cases here.
3989 * If at least one other column has a rational coefficient, then we
3990 * can perform a parametric cut in the main tableau by adding a new
3991 * integer division in the context tableau.
3992 * If all other columns have integral coefficients, then we need to
3993 * enforce that the rational combination of parameters (c + \sum a_i y_i)/m
3994 * is always integral. We do this by introducing an integer division
3995 * q = floor((c + \sum a_i y_i)/m) and stipulating that its argument should
3996 * always be integral in the context tableau, i.e., m q = c + \sum a_i y_i.
3997 * Since q is expressed in the tableau as
3998 * c + \sum a_i y_i - m q >= 0
3999 * -c - \sum a_i y_i + m q + m - 1 >= 0
4000 * it is sufficient to add the inequality
4001 * -c - \sum a_i y_i + m q >= 0
4002 * In the part of the context where this inequality does not hold, the
4003 * main tableau is marked as being empty.
4005 static void find_solutions(struct isl_sol
*sol
, struct isl_tab
*tab
)
4007 struct isl_context
*context
;
4010 if (!tab
|| sol
->error
)
4013 context
= sol
->context
;
4017 if (context
->op
->is_empty(context
))
4020 for (r
= 0; r
>= 0 && tab
&& !tab
->empty
; r
= restore_lexmin(tab
)) {
4023 enum isl_tab_row_sign sgn
;
4027 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
4028 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
4030 sgn
= row_sign(tab
, sol
, row
);
4033 tab
->row_sign
[row
] = sgn
;
4034 if (sgn
== isl_tab_row_any
)
4036 if (sgn
== isl_tab_row_any
&& split
== -1)
4038 if (sgn
== isl_tab_row_neg
)
4041 if (row
< tab
->n_row
)
4044 struct isl_vec
*ineq
;
4046 split
= context
->op
->best_split(context
, tab
);
4049 ineq
= get_row_parameter_ineq(tab
, split
);
4053 reset_any_to_unknown(tab
);
4054 tab
->row_sign
[split
] = isl_tab_row_pos
;
4056 find_in_pos(sol
, tab
, ineq
->el
);
4057 tab
->row_sign
[split
] = isl_tab_row_neg
;
4058 isl_seq_neg(ineq
->el
, ineq
->el
, ineq
->size
);
4059 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
4061 context
->op
->add_ineq(context
, ineq
->el
, 0, 1);
4069 row
= first_non_integer_row(tab
, &flags
);
4072 if (ISL_FL_ISSET(flags
, I_PAR
)) {
4073 if (ISL_FL_ISSET(flags
, I_VAR
)) {
4074 if (isl_tab_mark_empty(tab
) < 0)
4078 row
= add_cut(tab
, row
);
4079 } else if (ISL_FL_ISSET(flags
, I_VAR
)) {
4080 struct isl_vec
*div
;
4081 struct isl_vec
*ineq
;
4083 div
= get_row_split_div(tab
, row
);
4086 d
= context
->op
->get_div(context
, tab
, div
);
4090 ineq
= ineq_for_div(context
->op
->peek_basic_set(context
), d
);
4094 no_sol_in_strict(sol
, tab
, ineq
);
4095 isl_seq_neg(ineq
->el
, ineq
->el
, ineq
->size
);
4096 context
->op
->add_ineq(context
, ineq
->el
, 1, 1);
4098 if (sol
->error
|| !context
->op
->is_ok(context
))
4100 tab
= set_row_cst_to_div(tab
, row
, d
);
4101 if (context
->op
->is_empty(context
))
4104 row
= add_parametric_cut(tab
, row
, context
);
4119 /* Does "sol" contain a pair of partial solutions that could potentially
4122 * We currently only check that "sol" is not in an error state
4123 * and that there are at least two partial solutions of which the final two
4124 * are defined at the same level.
4126 static int sol_has_mergeable_solutions(struct isl_sol
*sol
)
4132 if (!sol
->partial
->next
)
4134 return sol
->partial
->level
== sol
->partial
->next
->level
;
4137 /* Compute the lexicographic minimum of the set represented by the main
4138 * tableau "tab" within the context "sol->context_tab".
4140 * As a preprocessing step, we first transfer all the purely parametric
4141 * equalities from the main tableau to the context tableau, i.e.,
4142 * parameters that have been pivoted to a row.
4143 * These equalities are ignored by the main algorithm, because the
4144 * corresponding rows may not be marked as being non-negative.
4145 * In parts of the context where the added equality does not hold,
4146 * the main tableau is marked as being empty.
4148 * Before we embark on the actual computation, we save a copy
4149 * of the context. When we return, we check if there are any
4150 * partial solutions that can potentially be merged. If so,
4151 * we perform a rollback to the initial state of the context.
4152 * The merging of partial solutions happens inside calls to
4153 * sol_dec_level that are pushed onto the undo stack of the context.
4154 * If there are no partial solutions that can potentially be merged
4155 * then the rollback is skipped as it would just be wasted effort.
4157 static void find_solutions_main(struct isl_sol
*sol
, struct isl_tab
*tab
)
4167 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
4171 if (tab
->row_var
[row
] < 0)
4173 if (tab
->row_var
[row
] >= tab
->n_param
&&
4174 tab
->row_var
[row
] < tab
->n_var
- tab
->n_div
)
4176 if (tab
->row_var
[row
] < tab
->n_param
)
4177 p
= tab
->row_var
[row
];
4179 p
= tab
->row_var
[row
]
4180 + tab
->n_param
- (tab
->n_var
- tab
->n_div
);
4182 eq
= isl_vec_alloc(tab
->mat
->ctx
, 1+tab
->n_param
+tab
->n_div
);
4185 get_row_parameter_line(tab
, row
, eq
->el
);
4186 isl_int_neg(eq
->el
[1 + p
], tab
->mat
->row
[row
][0]);
4187 eq
= isl_vec_normalize(eq
);
4190 no_sol_in_strict(sol
, tab
, eq
);
4192 isl_seq_neg(eq
->el
, eq
->el
, eq
->size
);
4194 no_sol_in_strict(sol
, tab
, eq
);
4195 isl_seq_neg(eq
->el
, eq
->el
, eq
->size
);
4197 sol
->context
->op
->add_eq(sol
->context
, eq
->el
, 1, 1);
4201 if (isl_tab_mark_redundant(tab
, row
) < 0)
4204 if (sol
->context
->op
->is_empty(sol
->context
))
4207 row
= tab
->n_redundant
- 1;
4210 saved
= sol
->context
->op
->save(sol
->context
);
4212 find_solutions(sol
, tab
);
4214 if (sol_has_mergeable_solutions(sol
))
4215 sol
->context
->op
->restore(sol
->context
, saved
);
4217 sol
->context
->op
->discard(saved
);
4228 /* Check if integer division "div" of "dom" also occurs in "bmap".
4229 * If so, return its position within the divs.
4230 * If not, return -1.
4232 static int find_context_div(struct isl_basic_map
*bmap
,
4233 struct isl_basic_set
*dom
, unsigned div
)
4236 unsigned b_dim
= isl_space_dim(bmap
->dim
, isl_dim_all
);
4237 unsigned d_dim
= isl_space_dim(dom
->dim
, isl_dim_all
);
4239 if (isl_int_is_zero(dom
->div
[div
][0]))
4241 if (isl_seq_first_non_zero(dom
->div
[div
] + 2 + d_dim
, dom
->n_div
) != -1)
4244 for (i
= 0; i
< bmap
->n_div
; ++i
) {
4245 if (isl_int_is_zero(bmap
->div
[i
][0]))
4247 if (isl_seq_first_non_zero(bmap
->div
[i
] + 2 + d_dim
,
4248 (b_dim
- d_dim
) + bmap
->n_div
) != -1)
4250 if (isl_seq_eq(bmap
->div
[i
], dom
->div
[div
], 2 + d_dim
))
4256 /* The correspondence between the variables in the main tableau,
4257 * the context tableau, and the input map and domain is as follows.
4258 * The first n_param and the last n_div variables of the main tableau
4259 * form the variables of the context tableau.
4260 * In the basic map, these n_param variables correspond to the
4261 * parameters and the input dimensions. In the domain, they correspond
4262 * to the parameters and the set dimensions.
4263 * The n_div variables correspond to the integer divisions in the domain.
4264 * To ensure that everything lines up, we may need to copy some of the
4265 * integer divisions of the domain to the map. These have to be placed
4266 * in the same order as those in the context and they have to be placed
4267 * after any other integer divisions that the map may have.
4268 * This function performs the required reordering.
4270 static struct isl_basic_map
*align_context_divs(struct isl_basic_map
*bmap
,
4271 struct isl_basic_set
*dom
)
4277 for (i
= 0; i
< dom
->n_div
; ++i
)
4278 if (find_context_div(bmap
, dom
, i
) != -1)
4280 other
= bmap
->n_div
- common
;
4281 if (dom
->n_div
- common
> 0) {
4282 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
4283 dom
->n_div
- common
, 0, 0);
4287 for (i
= 0; i
< dom
->n_div
; ++i
) {
4288 int pos
= find_context_div(bmap
, dom
, i
);
4290 pos
= isl_basic_map_alloc_div(bmap
);
4293 isl_int_set_si(bmap
->div
[pos
][0], 0);
4295 if (pos
!= other
+ i
)
4296 isl_basic_map_swap_div(bmap
, pos
, other
+ i
);
4300 isl_basic_map_free(bmap
);
4304 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
4305 * some obvious symmetries.
4307 * We make sure the divs in the domain are properly ordered,
4308 * because they will be added one by one in the given order
4309 * during the construction of the solution map.
4310 * Furthermore, make sure that the known integer divisions
4311 * appear before any unknown integer division because the solution
4312 * may depend on the known integer divisions, while anything that
4313 * depends on any variable starting from the first unknown integer
4314 * division is ignored in sol_pma_add.
4316 static struct isl_sol
*basic_map_partial_lexopt_base_sol(
4317 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
4318 __isl_give isl_set
**empty
, int max
,
4319 struct isl_sol
*(*init
)(__isl_keep isl_basic_map
*bmap
,
4320 __isl_take isl_basic_set
*dom
, int track_empty
, int max
))
4322 struct isl_tab
*tab
;
4323 struct isl_sol
*sol
= NULL
;
4324 struct isl_context
*context
;
4327 dom
= isl_basic_set_sort_divs(dom
);
4328 bmap
= align_context_divs(bmap
, dom
);
4330 sol
= init(bmap
, dom
, !!empty
, max
);
4334 context
= sol
->context
;
4335 if (isl_basic_set_plain_is_empty(context
->op
->peek_basic_set(context
)))
4337 else if (isl_basic_map_plain_is_empty(bmap
)) {
4340 isl_basic_set_copy(context
->op
->peek_basic_set(context
)));
4342 tab
= tab_for_lexmin(bmap
,
4343 context
->op
->peek_basic_set(context
), 1, max
);
4344 tab
= context
->op
->detect_nonnegative_parameters(context
, tab
);
4345 find_solutions_main(sol
, tab
);
4350 isl_basic_map_free(bmap
);
4354 isl_basic_map_free(bmap
);
4358 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
4359 * some obvious symmetries.
4361 * We call basic_map_partial_lexopt_base_sol and extract the results.
4363 static __isl_give isl_map
*basic_map_partial_lexopt_base(
4364 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
4365 __isl_give isl_set
**empty
, int max
)
4367 isl_map
*result
= NULL
;
4368 struct isl_sol
*sol
;
4369 struct isl_sol_map
*sol_map
;
4371 sol
= basic_map_partial_lexopt_base_sol(bmap
, dom
, empty
, max
,
4375 sol_map
= (struct isl_sol_map
*) sol
;
4377 result
= isl_map_copy(sol_map
->map
);
4379 *empty
= isl_set_copy(sol_map
->empty
);
4380 sol_free(&sol_map
->sol
);
4384 /* Return a count of the number of occurrences of the "n" first
4385 * variables in the inequality constraints of "bmap".
4387 static __isl_give
int *count_occurrences(__isl_keep isl_basic_map
*bmap
,
4396 ctx
= isl_basic_map_get_ctx(bmap
);
4397 occurrences
= isl_calloc_array(ctx
, int, n
);
4401 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
4402 for (j
= 0; j
< n
; ++j
) {
4403 if (!isl_int_is_zero(bmap
->ineq
[i
][1 + j
]))
4411 /* Do all of the "n" variables with non-zero coefficients in "c"
4412 * occur in exactly a single constraint.
4413 * "occurrences" is an array of length "n" containing the number
4414 * of occurrences of each of the variables in the inequality constraints.
4416 static int single_occurrence(int n
, isl_int
*c
, int *occurrences
)
4420 for (i
= 0; i
< n
; ++i
) {
4421 if (isl_int_is_zero(c
[i
]))
4423 if (occurrences
[i
] != 1)
4430 /* Do all of the "n" initial variables that occur in inequality constraint
4431 * "ineq" of "bmap" only occur in that constraint?
4433 static int all_single_occurrence(__isl_keep isl_basic_map
*bmap
, int ineq
,
4438 for (i
= 0; i
< n
; ++i
) {
4439 if (isl_int_is_zero(bmap
->ineq
[ineq
][1 + i
]))
4441 for (j
= 0; j
< bmap
->n_ineq
; ++j
) {
4444 if (!isl_int_is_zero(bmap
->ineq
[j
][1 + i
]))
4452 /* Structure used during detection of parallel constraints.
4453 * n_in: number of "input" variables: isl_dim_param + isl_dim_in
4454 * n_out: number of "output" variables: isl_dim_out + isl_dim_div
4455 * val: the coefficients of the output variables
4457 struct isl_constraint_equal_info
{
4458 isl_basic_map
*bmap
;
4464 /* Check whether the coefficients of the output variables
4465 * of the constraint in "entry" are equal to info->val.
4467 static int constraint_equal(const void *entry
, const void *val
)
4469 isl_int
**row
= (isl_int
**)entry
;
4470 const struct isl_constraint_equal_info
*info
= val
;
4472 return isl_seq_eq((*row
) + 1 + info
->n_in
, info
->val
, info
->n_out
);
4475 /* Check whether "bmap" has a pair of constraints that have
4476 * the same coefficients for the output variables.
4477 * Note that the coefficients of the existentially quantified
4478 * variables need to be zero since the existentially quantified
4479 * of the result are usually not the same as those of the input.
4480 * Furthermore, check that each of the input variables that occur
4481 * in those constraints does not occur in any other constraint.
4482 * If so, return true and return the row indices of the two constraints
4483 * in *first and *second.
4485 static isl_bool
parallel_constraints(__isl_keep isl_basic_map
*bmap
,
4486 int *first
, int *second
)
4490 int *occurrences
= NULL
;
4491 struct isl_hash_table
*table
= NULL
;
4492 struct isl_hash_table_entry
*entry
;
4493 struct isl_constraint_equal_info info
;
4497 ctx
= isl_basic_map_get_ctx(bmap
);
4498 table
= isl_hash_table_alloc(ctx
, bmap
->n_ineq
);
4502 info
.n_in
= isl_basic_map_dim(bmap
, isl_dim_param
) +
4503 isl_basic_map_dim(bmap
, isl_dim_in
);
4504 occurrences
= count_occurrences(bmap
, info
.n_in
);
4505 if (info
.n_in
&& !occurrences
)
4508 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4509 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4510 info
.n_out
= n_out
+ n_div
;
4511 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
4514 info
.val
= bmap
->ineq
[i
] + 1 + info
.n_in
;
4515 if (isl_seq_first_non_zero(info
.val
, n_out
) < 0)
4517 if (isl_seq_first_non_zero(info
.val
+ n_out
, n_div
) >= 0)
4519 if (!single_occurrence(info
.n_in
, bmap
->ineq
[i
] + 1,
4522 hash
= isl_seq_get_hash(info
.val
, info
.n_out
);
4523 entry
= isl_hash_table_find(ctx
, table
, hash
,
4524 constraint_equal
, &info
, 1);
4529 entry
->data
= &bmap
->ineq
[i
];
4532 if (i
< bmap
->n_ineq
) {
4533 *first
= ((isl_int
**)entry
->data
) - bmap
->ineq
;
4537 isl_hash_table_free(ctx
, table
);
4540 return i
< bmap
->n_ineq
;
4542 isl_hash_table_free(ctx
, table
);
4544 return isl_bool_error
;
4547 /* Given a set of upper bounds in "var", add constraints to "bset"
4548 * that make the i-th bound smallest.
4550 * In particular, if there are n bounds b_i, then add the constraints
4552 * b_i <= b_j for j > i
4553 * b_i < b_j for j < i
4555 static __isl_give isl_basic_set
*select_minimum(__isl_take isl_basic_set
*bset
,
4556 __isl_keep isl_mat
*var
, int i
)
4561 ctx
= isl_mat_get_ctx(var
);
4563 for (j
= 0; j
< var
->n_row
; ++j
) {
4566 k
= isl_basic_set_alloc_inequality(bset
);
4569 isl_seq_combine(bset
->ineq
[k
], ctx
->one
, var
->row
[j
],
4570 ctx
->negone
, var
->row
[i
], var
->n_col
);
4571 isl_int_set_si(bset
->ineq
[k
][var
->n_col
], 0);
4573 isl_int_sub_ui(bset
->ineq
[k
][0], bset
->ineq
[k
][0], 1);
4576 bset
= isl_basic_set_finalize(bset
);
4580 isl_basic_set_free(bset
);
4584 /* Given a set of upper bounds on the last "input" variable m,
4585 * construct a set that assigns the minimal upper bound to m, i.e.,
4586 * construct a set that divides the space into cells where one
4587 * of the upper bounds is smaller than all the others and assign
4588 * this upper bound to m.
4590 * In particular, if there are n bounds b_i, then the result
4591 * consists of n basic sets, each one of the form
4594 * b_i <= b_j for j > i
4595 * b_i < b_j for j < i
4597 static __isl_give isl_set
*set_minimum(__isl_take isl_space
*dim
,
4598 __isl_take isl_mat
*var
)
4601 isl_basic_set
*bset
= NULL
;
4602 isl_set
*set
= NULL
;
4607 set
= isl_set_alloc_space(isl_space_copy(dim
),
4608 var
->n_row
, ISL_SET_DISJOINT
);
4610 for (i
= 0; i
< var
->n_row
; ++i
) {
4611 bset
= isl_basic_set_alloc_space(isl_space_copy(dim
), 0,
4613 k
= isl_basic_set_alloc_equality(bset
);
4616 isl_seq_cpy(bset
->eq
[k
], var
->row
[i
], var
->n_col
);
4617 isl_int_set_si(bset
->eq
[k
][var
->n_col
], -1);
4618 bset
= select_minimum(bset
, var
, i
);
4619 set
= isl_set_add_basic_set(set
, bset
);
4622 isl_space_free(dim
);
4626 isl_basic_set_free(bset
);
4628 isl_space_free(dim
);
4633 /* Given that the last input variable of "bmap" represents the minimum
4634 * of the bounds in "cst", check whether we need to split the domain
4635 * based on which bound attains the minimum.
4637 * A split is needed when the minimum appears in an integer division
4638 * or in an equality. Otherwise, it is only needed if it appears in
4639 * an upper bound that is different from the upper bounds on which it
4642 static isl_bool
need_split_basic_map(__isl_keep isl_basic_map
*bmap
,
4643 __isl_keep isl_mat
*cst
)
4649 pos
= cst
->n_col
- 1;
4650 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4652 for (i
= 0; i
< bmap
->n_div
; ++i
)
4653 if (!isl_int_is_zero(bmap
->div
[i
][2 + pos
]))
4654 return isl_bool_true
;
4656 for (i
= 0; i
< bmap
->n_eq
; ++i
)
4657 if (!isl_int_is_zero(bmap
->eq
[i
][1 + pos
]))
4658 return isl_bool_true
;
4660 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
4661 if (isl_int_is_nonneg(bmap
->ineq
[i
][1 + pos
]))
4663 if (!isl_int_is_negone(bmap
->ineq
[i
][1 + pos
]))
4664 return isl_bool_true
;
4665 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + pos
+ 1,
4666 total
- pos
- 1) >= 0)
4667 return isl_bool_true
;
4669 for (j
= 0; j
< cst
->n_row
; ++j
)
4670 if (isl_seq_eq(bmap
->ineq
[i
], cst
->row
[j
], cst
->n_col
))
4672 if (j
>= cst
->n_row
)
4673 return isl_bool_true
;
4676 return isl_bool_false
;
4679 /* Given that the last set variable of "bset" represents the minimum
4680 * of the bounds in "cst", check whether we need to split the domain
4681 * based on which bound attains the minimum.
4683 * We simply call need_split_basic_map here. This is safe because
4684 * the position of the minimum is computed from "cst" and not
4687 static isl_bool
need_split_basic_set(__isl_keep isl_basic_set
*bset
,
4688 __isl_keep isl_mat
*cst
)
4690 return need_split_basic_map(bset_to_bmap(bset
), cst
);
4693 /* Given that the last set variable of "set" represents the minimum
4694 * of the bounds in "cst", check whether we need to split the domain
4695 * based on which bound attains the minimum.
4697 static isl_bool
need_split_set(__isl_keep isl_set
*set
, __isl_keep isl_mat
*cst
)
4701 for (i
= 0; i
< set
->n
; ++i
) {
4704 split
= need_split_basic_set(set
->p
[i
], cst
);
4705 if (split
< 0 || split
)
4709 return isl_bool_false
;
4712 /* Given a set of which the last set variable is the minimum
4713 * of the bounds in "cst", split each basic set in the set
4714 * in pieces where one of the bounds is (strictly) smaller than the others.
4715 * This subdivision is given in "min_expr".
4716 * The variable is subsequently projected out.
4718 * We only do the split when it is needed.
4719 * For example if the last input variable m = min(a,b) and the only
4720 * constraints in the given basic set are lower bounds on m,
4721 * i.e., l <= m = min(a,b), then we can simply project out m
4722 * to obtain l <= a and l <= b, without having to split on whether
4723 * m is equal to a or b.
4725 static __isl_give isl_set
*split(__isl_take isl_set
*empty
,
4726 __isl_take isl_set
*min_expr
, __isl_take isl_mat
*cst
)
4733 if (!empty
|| !min_expr
|| !cst
)
4736 n_in
= isl_set_dim(empty
, isl_dim_set
);
4737 dim
= isl_set_get_space(empty
);
4738 dim
= isl_space_drop_dims(dim
, isl_dim_set
, n_in
- 1, 1);
4739 res
= isl_set_empty(dim
);
4741 for (i
= 0; i
< empty
->n
; ++i
) {
4745 set
= isl_set_from_basic_set(isl_basic_set_copy(empty
->p
[i
]));
4746 split
= need_split_basic_set(empty
->p
[i
], cst
);
4748 set
= isl_set_free(set
);
4750 set
= isl_set_intersect(set
, isl_set_copy(min_expr
));
4751 set
= isl_set_remove_dims(set
, isl_dim_set
, n_in
- 1, 1);
4753 res
= isl_set_union_disjoint(res
, set
);
4756 isl_set_free(empty
);
4757 isl_set_free(min_expr
);
4761 isl_set_free(empty
);
4762 isl_set_free(min_expr
);
4767 /* Given a map of which the last input variable is the minimum
4768 * of the bounds in "cst", split each basic set in the set
4769 * in pieces where one of the bounds is (strictly) smaller than the others.
4770 * This subdivision is given in "min_expr".
4771 * The variable is subsequently projected out.
4773 * The implementation is essentially the same as that of "split".
4775 static __isl_give isl_map
*split_domain(__isl_take isl_map
*opt
,
4776 __isl_take isl_set
*min_expr
, __isl_take isl_mat
*cst
)
4783 if (!opt
|| !min_expr
|| !cst
)
4786 n_in
= isl_map_dim(opt
, isl_dim_in
);
4787 dim
= isl_map_get_space(opt
);
4788 dim
= isl_space_drop_dims(dim
, isl_dim_in
, n_in
- 1, 1);
4789 res
= isl_map_empty(dim
);
4791 for (i
= 0; i
< opt
->n
; ++i
) {
4795 map
= isl_map_from_basic_map(isl_basic_map_copy(opt
->p
[i
]));
4796 split
= need_split_basic_map(opt
->p
[i
], cst
);
4798 map
= isl_map_free(map
);
4800 map
= isl_map_intersect_domain(map
,
4801 isl_set_copy(min_expr
));
4802 map
= isl_map_remove_dims(map
, isl_dim_in
, n_in
- 1, 1);
4804 res
= isl_map_union_disjoint(res
, map
);
4808 isl_set_free(min_expr
);
4813 isl_set_free(min_expr
);
4818 static __isl_give isl_map
*basic_map_partial_lexopt(
4819 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
4820 __isl_give isl_set
**empty
, int max
);
4822 /* This function is called from basic_map_partial_lexopt_symm.
4823 * The last variable of "bmap" and "dom" corresponds to the minimum
4824 * of the bounds in "cst". "map_space" is the space of the original
4825 * input relation (of basic_map_partial_lexopt_symm) and "set_space"
4826 * is the space of the original domain.
4828 * We recursively call basic_map_partial_lexopt and then plug in
4829 * the definition of the minimum in the result.
4831 static __isl_give isl_map
*basic_map_partial_lexopt_symm_core(
4832 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
4833 __isl_give isl_set
**empty
, int max
, __isl_take isl_mat
*cst
,
4834 __isl_take isl_space
*map_space
, __isl_take isl_space
*set_space
)
4839 min_expr
= set_minimum(isl_basic_set_get_space(dom
), isl_mat_copy(cst
));
4841 opt
= basic_map_partial_lexopt(bmap
, dom
, empty
, max
);
4844 *empty
= split(*empty
,
4845 isl_set_copy(min_expr
), isl_mat_copy(cst
));
4846 *empty
= isl_set_reset_space(*empty
, set_space
);
4849 opt
= split_domain(opt
, min_expr
, cst
);
4850 opt
= isl_map_reset_space(opt
, map_space
);
4855 /* Extract a domain from "bmap" for the purpose of computing
4856 * a lexicographic optimum.
4858 * This function is only called when the caller wants to compute a full
4859 * lexicographic optimum, i.e., without specifying a domain. In this case,
4860 * the caller is not interested in the part of the domain space where
4861 * there is no solution and the domain can be initialized to those constraints
4862 * of "bmap" that only involve the parameters and the input dimensions.
4863 * This relieves the parametric programming engine from detecting those
4864 * inequalities and transferring them to the context. More importantly,
4865 * it ensures that those inequalities are transferred first and not
4866 * intermixed with inequalities that actually split the domain.
4868 * If the caller does not require the absence of existentially quantified
4869 * variables in the result (i.e., if ISL_OPT_QE is not set in "flags"),
4870 * then the actual domain of "bmap" can be used. This ensures that
4871 * the domain does not need to be split at all just to separate out
4872 * pieces of the domain that do not have a solution from piece that do.
4873 * This domain cannot be used in general because it may involve
4874 * (unknown) existentially quantified variables which will then also
4875 * appear in the solution.
4877 static __isl_give isl_basic_set
*extract_domain(__isl_keep isl_basic_map
*bmap
,
4883 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4884 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4885 bmap
= isl_basic_map_copy(bmap
);
4886 if (ISL_FL_ISSET(flags
, ISL_OPT_QE
)) {
4887 bmap
= isl_basic_map_drop_constraints_involving_dims(bmap
,
4888 isl_dim_div
, 0, n_div
);
4889 bmap
= isl_basic_map_drop_constraints_involving_dims(bmap
,
4890 isl_dim_out
, 0, n_out
);
4892 return isl_basic_map_domain(bmap
);
4896 #define TYPE isl_map
4899 #include "isl_tab_lexopt_templ.c"
4901 struct isl_sol_for
{
4903 isl_stat (*fn
)(__isl_take isl_basic_set
*dom
,
4904 __isl_take isl_aff_list
*list
, void *user
);
4908 static void sol_for_free(struct isl_sol
*sol
)
4912 /* Add the solution identified by the tableau and the context tableau.
4913 * In particular, "dom" represents the context and "ma" expresses
4914 * the solution on that context.
4916 * See documentation of sol_add for more details.
4918 * Instead of constructing a basic map, this function calls a user
4919 * defined function with the current context as a basic set and
4920 * a list of affine expressions representing the relation between
4921 * the input and output. The space over which the affine expressions
4922 * are defined is the same as that of the domain. The number of
4923 * affine expressions in the list is equal to the number of output variables.
4925 static void sol_for_add(struct isl_sol_for
*sol
,
4926 __isl_take isl_basic_set
*dom
, __isl_take isl_multi_aff
*ma
)
4933 if (sol
->sol
.error
|| !dom
|| !ma
)
4936 ctx
= isl_basic_set_get_ctx(dom
);
4937 n
= isl_multi_aff_dim(ma
, isl_dim_out
);
4938 list
= isl_aff_list_alloc(ctx
, n
);
4939 for (i
= 0; i
< n
; ++i
) {
4940 aff
= isl_multi_aff_get_aff(ma
, i
);
4941 list
= isl_aff_list_add(list
, aff
);
4944 dom
= isl_basic_set_finalize(dom
);
4946 if (sol
->fn(isl_basic_set_copy(dom
), list
, sol
->user
) < 0)
4949 isl_basic_set_free(dom
);
4950 isl_multi_aff_free(ma
);
4953 isl_basic_set_free(dom
);
4954 isl_multi_aff_free(ma
);
4958 static void sol_for_add_wrap(struct isl_sol
*sol
,
4959 __isl_take isl_basic_set
*dom
, __isl_take isl_multi_aff
*ma
)
4961 sol_for_add((struct isl_sol_for
*)sol
, dom
, ma
);
4964 static struct isl_sol_for
*sol_for_init(__isl_keep isl_basic_map
*bmap
, int max
,
4965 isl_stat (*fn
)(__isl_take isl_basic_set
*dom
,
4966 __isl_take isl_aff_list
*list
, void *user
),
4969 struct isl_sol_for
*sol_for
= NULL
;
4971 struct isl_basic_set
*dom
= NULL
;
4973 sol_for
= isl_calloc_type(bmap
->ctx
, struct isl_sol_for
);
4977 dom_dim
= isl_space_domain(isl_space_copy(bmap
->dim
));
4978 dom
= isl_basic_set_universe(dom_dim
);
4980 sol_for
->sol
.free
= &sol_for_free
;
4981 if (sol_init(&sol_for
->sol
, bmap
, dom
, max
) < 0)
4984 sol_for
->user
= user
;
4985 sol_for
->sol
.add
= &sol_for_add_wrap
;
4986 sol_for
->sol
.add_empty
= NULL
;
4988 isl_basic_set_free(dom
);
4991 isl_basic_set_free(dom
);
4992 sol_free(&sol_for
->sol
);
4996 static void sol_for_find_solutions(struct isl_sol_for
*sol_for
,
4997 struct isl_tab
*tab
)
4999 find_solutions_main(&sol_for
->sol
, tab
);
5002 isl_stat
isl_basic_map_foreach_lexopt(__isl_keep isl_basic_map
*bmap
, int max
,
5003 isl_stat (*fn
)(__isl_take isl_basic_set
*dom
,
5004 __isl_take isl_aff_list
*list
, void *user
),
5007 struct isl_sol_for
*sol_for
= NULL
;
5009 bmap
= isl_basic_map_copy(bmap
);
5010 bmap
= isl_basic_map_detect_equalities(bmap
);
5012 return isl_stat_error
;
5014 sol_for
= sol_for_init(bmap
, max
, fn
, user
);
5018 if (isl_basic_map_plain_is_empty(bmap
))
5021 struct isl_tab
*tab
;
5022 struct isl_context
*context
= sol_for
->sol
.context
;
5023 tab
= tab_for_lexmin(bmap
,
5024 context
->op
->peek_basic_set(context
), 1, max
);
5025 tab
= context
->op
->detect_nonnegative_parameters(context
, tab
);
5026 sol_for_find_solutions(sol_for
, tab
);
5027 if (sol_for
->sol
.error
)
5031 sol_free(&sol_for
->sol
);
5032 isl_basic_map_free(bmap
);
5035 sol_free(&sol_for
->sol
);
5036 isl_basic_map_free(bmap
);
5037 return isl_stat_error
;
5040 isl_stat
isl_basic_set_foreach_lexopt(__isl_keep isl_basic_set
*bset
, int max
,
5041 isl_stat (*fn
)(__isl_take isl_basic_set
*dom
,
5042 __isl_take isl_aff_list
*list
, void *user
),
5045 return isl_basic_map_foreach_lexopt(bset
, max
, fn
, user
);
5048 /* Check if the given sequence of len variables starting at pos
5049 * represents a trivial (i.e., zero) solution.
5050 * The variables are assumed to be non-negative and to come in pairs,
5051 * with each pair representing a variable of unrestricted sign.
5052 * The solution is trivial if each such pair in the sequence consists
5053 * of two identical values, meaning that the variable being represented
5056 static int region_is_trivial(struct isl_tab
*tab
, int pos
, int len
)
5063 for (i
= 0; i
< len
; i
+= 2) {
5067 neg_row
= tab
->var
[pos
+ i
].is_row
?
5068 tab
->var
[pos
+ i
].index
: -1;
5069 pos_row
= tab
->var
[pos
+ i
+ 1].is_row
?
5070 tab
->var
[pos
+ i
+ 1].index
: -1;
5073 isl_int_is_zero(tab
->mat
->row
[neg_row
][1])) &&
5075 isl_int_is_zero(tab
->mat
->row
[pos_row
][1])))
5078 if (neg_row
< 0 || pos_row
< 0)
5080 if (isl_int_ne(tab
->mat
->row
[neg_row
][1],
5081 tab
->mat
->row
[pos_row
][1]))
5088 /* Return the index of the first trivial region or -1 if all regions
5091 static int first_trivial_region(struct isl_tab
*tab
,
5092 int n_region
, struct isl_region
*region
)
5096 for (i
= 0; i
< n_region
; ++i
) {
5097 if (region_is_trivial(tab
, region
[i
].pos
, region
[i
].len
))
5104 /* Check if the solution is optimal, i.e., whether the first
5105 * n_op entries are zero.
5107 static int is_optimal(__isl_keep isl_vec
*sol
, int n_op
)
5111 for (i
= 0; i
< n_op
; ++i
)
5112 if (!isl_int_is_zero(sol
->el
[1 + i
]))
5117 /* Add constraints to "tab" that ensure that any solution is significantly
5118 * better than that represented by "sol". That is, find the first
5119 * relevant (within first n_op) non-zero coefficient and force it (along
5120 * with all previous coefficients) to be zero.
5121 * If the solution is already optimal (all relevant coefficients are zero),
5122 * then just mark the table as empty.
5124 * This function assumes that at least 2 * n_op more rows and at least
5125 * 2 * n_op more elements in the constraint array are available in the tableau.
5127 static int force_better_solution(struct isl_tab
*tab
,
5128 __isl_keep isl_vec
*sol
, int n_op
)
5137 for (i
= 0; i
< n_op
; ++i
)
5138 if (!isl_int_is_zero(sol
->el
[1 + i
]))
5142 if (isl_tab_mark_empty(tab
) < 0)
5147 ctx
= isl_vec_get_ctx(sol
);
5148 v
= isl_vec_alloc(ctx
, 1 + tab
->n_var
);
5152 for (; i
>= 0; --i
) {
5154 isl_int_set_si(v
->el
[1 + i
], -1);
5155 if (add_lexmin_eq(tab
, v
->el
) < 0)
5166 struct isl_trivial
{
5170 struct isl_tab_undo
*snap
;
5173 /* Return the lexicographically smallest non-trivial solution of the
5174 * given ILP problem.
5176 * All variables are assumed to be non-negative.
5178 * n_op is the number of initial coordinates to optimize.
5179 * That is, once a solution has been found, we will only continue looking
5180 * for solution that result in significantly better values for those
5181 * initial coordinates. That is, we only continue looking for solutions
5182 * that increase the number of initial zeros in this sequence.
5184 * A solution is non-trivial, if it is non-trivial on each of the
5185 * specified regions. Each region represents a sequence of pairs
5186 * of variables. A solution is non-trivial on such a region if
5187 * at least one of these pairs consists of different values, i.e.,
5188 * such that the non-negative variable represented by the pair is non-zero.
5190 * Whenever a conflict is encountered, all constraints involved are
5191 * reported to the caller through a call to "conflict".
5193 * We perform a simple branch-and-bound backtracking search.
5194 * Each level in the search represents initially trivial region that is forced
5195 * to be non-trivial.
5196 * At each level we consider n cases, where n is the length of the region.
5197 * In terms of the n/2 variables of unrestricted signs being encoded by
5198 * the region, we consider the cases
5201 * x_0 = 0 and x_1 >= 1
5202 * x_0 = 0 and x_1 <= -1
5203 * x_0 = 0 and x_1 = 0 and x_2 >= 1
5204 * x_0 = 0 and x_1 = 0 and x_2 <= -1
5206 * The cases are considered in this order, assuming that each pair
5207 * x_i_a x_i_b represents the value x_i_b - x_i_a.
5208 * That is, x_0 >= 1 is enforced by adding the constraint
5209 * x_0_b - x_0_a >= 1
5211 __isl_give isl_vec
*isl_tab_basic_set_non_trivial_lexmin(
5212 __isl_take isl_basic_set
*bset
, int n_op
, int n_region
,
5213 struct isl_region
*region
,
5214 int (*conflict
)(int con
, void *user
), void *user
)
5220 isl_vec
*sol
= NULL
;
5221 struct isl_tab
*tab
;
5222 struct isl_trivial
*triv
= NULL
;
5228 ctx
= isl_basic_set_get_ctx(bset
);
5229 sol
= isl_vec_alloc(ctx
, 0);
5231 tab
= tab_for_lexmin(bset
, NULL
, 0, 0);
5234 tab
->conflict
= conflict
;
5235 tab
->conflict_user
= user
;
5237 v
= isl_vec_alloc(ctx
, 1 + tab
->n_var
);
5238 triv
= isl_calloc_array(ctx
, struct isl_trivial
, n_region
);
5239 if (!v
|| (n_region
&& !triv
))
5245 while (level
>= 0) {
5249 tab
= cut_to_integer_lexmin(tab
, CUT_ONE
);
5254 r
= first_trivial_region(tab
, n_region
, region
);
5256 for (i
= 0; i
< level
; ++i
)
5259 sol
= isl_tab_get_sample_value(tab
);
5262 if (is_optimal(sol
, n_op
))
5266 if (level
>= n_region
)
5267 isl_die(ctx
, isl_error_internal
,
5268 "nesting level too deep", goto error
);
5269 if (isl_tab_extend_cons(tab
,
5270 2 * region
[r
].len
+ 2 * n_op
) < 0)
5272 triv
[level
].region
= r
;
5273 triv
[level
].side
= 0;
5276 r
= triv
[level
].region
;
5277 side
= triv
[level
].side
;
5278 base
= 2 * (side
/2);
5280 if (side
>= region
[r
].len
) {
5285 if (isl_tab_rollback(tab
, triv
[level
].snap
) < 0)
5290 if (triv
[level
].update
) {
5291 if (force_better_solution(tab
, sol
, n_op
) < 0)
5293 triv
[level
].update
= 0;
5296 if (side
== base
&& base
>= 2) {
5297 for (j
= base
- 2; j
< base
; ++j
) {
5299 isl_int_set_si(v
->el
[1 + region
[r
].pos
+ j
], 1);
5300 if (add_lexmin_eq(tab
, v
->el
) < 0)
5305 triv
[level
].snap
= isl_tab_snap(tab
);
5306 if (isl_tab_push_basis(tab
) < 0)
5310 isl_int_set_si(v
->el
[0], -1);
5311 isl_int_set_si(v
->el
[1 + region
[r
].pos
+ side
], -1);
5312 isl_int_set_si(v
->el
[1 + region
[r
].pos
+ (side
^ 1)], 1);
5313 tab
= add_lexmin_ineq(tab
, v
->el
);
5323 isl_basic_set_free(bset
);
5330 isl_basic_set_free(bset
);
5335 /* Wrapper for a tableau that is used for computing
5336 * the lexicographically smallest rational point of a non-negative set.
5337 * This point is represented by the sample value of "tab",
5338 * unless "tab" is empty.
5340 struct isl_tab_lexmin
{
5342 struct isl_tab
*tab
;
5345 /* Free "tl" and return NULL.
5347 __isl_null isl_tab_lexmin
*isl_tab_lexmin_free(__isl_take isl_tab_lexmin
*tl
)
5351 isl_ctx_deref(tl
->ctx
);
5352 isl_tab_free(tl
->tab
);
5358 /* Construct an isl_tab_lexmin for computing
5359 * the lexicographically smallest rational point in "bset",
5360 * assuming that all variables are non-negative.
5362 __isl_give isl_tab_lexmin
*isl_tab_lexmin_from_basic_set(
5363 __isl_take isl_basic_set
*bset
)
5371 ctx
= isl_basic_set_get_ctx(bset
);
5372 tl
= isl_calloc_type(ctx
, struct isl_tab_lexmin
);
5377 tl
->tab
= tab_for_lexmin(bset
, NULL
, 0, 0);
5378 isl_basic_set_free(bset
);
5380 return isl_tab_lexmin_free(tl
);
5383 isl_basic_set_free(bset
);
5384 isl_tab_lexmin_free(tl
);
5388 /* Return the dimension of the set represented by "tl".
5390 int isl_tab_lexmin_dim(__isl_keep isl_tab_lexmin
*tl
)
5392 return tl
? tl
->tab
->n_var
: -1;
5395 /* Add the equality with coefficients "eq" to "tl", updating the optimal
5396 * solution if needed.
5397 * The equality is added as two opposite inequality constraints.
5399 __isl_give isl_tab_lexmin
*isl_tab_lexmin_add_eq(__isl_take isl_tab_lexmin
*tl
,
5405 return isl_tab_lexmin_free(tl
);
5407 if (isl_tab_extend_cons(tl
->tab
, 2) < 0)
5408 return isl_tab_lexmin_free(tl
);
5409 n_var
= tl
->tab
->n_var
;
5410 isl_seq_neg(eq
, eq
, 1 + n_var
);
5411 tl
->tab
= add_lexmin_ineq(tl
->tab
, eq
);
5412 isl_seq_neg(eq
, eq
, 1 + n_var
);
5413 tl
->tab
= add_lexmin_ineq(tl
->tab
, eq
);
5416 return isl_tab_lexmin_free(tl
);
5421 /* Return the lexicographically smallest rational point in the basic set
5422 * from which "tl" was constructed.
5423 * If the original input was empty, then return a zero-length vector.
5425 __isl_give isl_vec
*isl_tab_lexmin_get_solution(__isl_keep isl_tab_lexmin
*tl
)
5430 return isl_vec_alloc(tl
->ctx
, 0);
5432 return isl_tab_get_sample_value(tl
->tab
);
5435 /* Return the lexicographically smallest rational point in "bset",
5436 * assuming that all variables are non-negative.
5437 * If "bset" is empty, then return a zero-length vector.
5439 __isl_give isl_vec
*isl_tab_basic_set_non_neg_lexmin(
5440 __isl_take isl_basic_set
*bset
)
5445 tl
= isl_tab_lexmin_from_basic_set(bset
);
5446 sol
= isl_tab_lexmin_get_solution(tl
);
5447 isl_tab_lexmin_free(tl
);
5451 struct isl_sol_pma
{
5453 isl_pw_multi_aff
*pma
;
5457 static void sol_pma_free(struct isl_sol
*sol
)
5459 struct isl_sol_pma
*sol_pma
= (struct isl_sol_pma
*) sol
;
5460 isl_pw_multi_aff_free(sol_pma
->pma
);
5461 isl_set_free(sol_pma
->empty
);
5464 /* This function is called for parts of the context where there is
5465 * no solution, with "bset" corresponding to the context tableau.
5466 * Simply add the basic set to the set "empty".
5468 static void sol_pma_add_empty(struct isl_sol_pma
*sol
,
5469 __isl_take isl_basic_set
*bset
)
5471 if (!bset
|| !sol
->empty
)
5474 sol
->empty
= isl_set_grow(sol
->empty
, 1);
5475 bset
= isl_basic_set_simplify(bset
);
5476 bset
= isl_basic_set_finalize(bset
);
5477 sol
->empty
= isl_set_add_basic_set(sol
->empty
, bset
);
5482 isl_basic_set_free(bset
);
5486 /* Given a basic set "dom" that represents the context and a tuple of
5487 * affine expressions "maff" defined over this domain, construct
5488 * an isl_pw_multi_aff with a single cell corresponding to "dom" and
5489 * the affine expressions in "maff".
5491 static void sol_pma_add(struct isl_sol_pma
*sol
,
5492 __isl_take isl_basic_set
*dom
, __isl_take isl_multi_aff
*maff
)
5494 isl_pw_multi_aff
*pma
;
5496 dom
= isl_basic_set_simplify(dom
);
5497 dom
= isl_basic_set_finalize(dom
);
5498 pma
= isl_pw_multi_aff_alloc(isl_set_from_basic_set(dom
), maff
);
5499 sol
->pma
= isl_pw_multi_aff_add_disjoint(sol
->pma
, pma
);
5504 static void sol_pma_add_empty_wrap(struct isl_sol
*sol
,
5505 __isl_take isl_basic_set
*bset
)
5507 sol_pma_add_empty((struct isl_sol_pma
*)sol
, bset
);
5510 static void sol_pma_add_wrap(struct isl_sol
*sol
,
5511 __isl_take isl_basic_set
*dom
, __isl_take isl_multi_aff
*ma
)
5513 sol_pma_add((struct isl_sol_pma
*)sol
, dom
, ma
);
5516 /* Construct an isl_sol_pma structure for accumulating the solution.
5517 * If track_empty is set, then we also keep track of the parts
5518 * of the context where there is no solution.
5519 * If max is set, then we are solving a maximization, rather than
5520 * a minimization problem, which means that the variables in the
5521 * tableau have value "M - x" rather than "M + x".
5523 static struct isl_sol
*sol_pma_init(__isl_keep isl_basic_map
*bmap
,
5524 __isl_take isl_basic_set
*dom
, int track_empty
, int max
)
5526 struct isl_sol_pma
*sol_pma
= NULL
;
5532 sol_pma
= isl_calloc_type(bmap
->ctx
, struct isl_sol_pma
);
5536 sol_pma
->sol
.free
= &sol_pma_free
;
5537 if (sol_init(&sol_pma
->sol
, bmap
, dom
, max
) < 0)
5539 sol_pma
->sol
.add
= &sol_pma_add_wrap
;
5540 sol_pma
->sol
.add_empty
= track_empty
? &sol_pma_add_empty_wrap
: NULL
;
5541 space
= isl_space_copy(sol_pma
->sol
.space
);
5542 sol_pma
->pma
= isl_pw_multi_aff_empty(space
);
5547 sol_pma
->empty
= isl_set_alloc_space(isl_basic_set_get_space(dom
),
5548 1, ISL_SET_DISJOINT
);
5549 if (!sol_pma
->empty
)
5553 isl_basic_set_free(dom
);
5554 return &sol_pma
->sol
;
5556 isl_basic_set_free(dom
);
5557 sol_free(&sol_pma
->sol
);
5561 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
5562 * some obvious symmetries.
5564 * We call basic_map_partial_lexopt_base_sol and extract the results.
5566 static __isl_give isl_pw_multi_aff
*basic_map_partial_lexopt_base_pw_multi_aff(
5567 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5568 __isl_give isl_set
**empty
, int max
)
5570 isl_pw_multi_aff
*result
= NULL
;
5571 struct isl_sol
*sol
;
5572 struct isl_sol_pma
*sol_pma
;
5574 sol
= basic_map_partial_lexopt_base_sol(bmap
, dom
, empty
, max
,
5578 sol_pma
= (struct isl_sol_pma
*) sol
;
5580 result
= isl_pw_multi_aff_copy(sol_pma
->pma
);
5582 *empty
= isl_set_copy(sol_pma
->empty
);
5583 sol_free(&sol_pma
->sol
);
5587 /* Given that the last input variable of "maff" represents the minimum
5588 * of some bounds, check whether we need to plug in the expression
5591 * In particular, check if the last input variable appears in any
5592 * of the expressions in "maff".
5594 static int need_substitution(__isl_keep isl_multi_aff
*maff
)
5599 pos
= isl_multi_aff_dim(maff
, isl_dim_in
) - 1;
5601 for (i
= 0; i
< maff
->n
; ++i
)
5602 if (isl_aff_involves_dims(maff
->p
[i
], isl_dim_in
, pos
, 1))
5608 /* Given a set of upper bounds on the last "input" variable m,
5609 * construct a piecewise affine expression that selects
5610 * the minimal upper bound to m, i.e.,
5611 * divide the space into cells where one
5612 * of the upper bounds is smaller than all the others and select
5613 * this upper bound on that cell.
5615 * In particular, if there are n bounds b_i, then the result
5616 * consists of n cell, each one of the form
5618 * b_i <= b_j for j > i
5619 * b_i < b_j for j < i
5621 * The affine expression on this cell is
5625 static __isl_give isl_pw_aff
*set_minimum_pa(__isl_take isl_space
*space
,
5626 __isl_take isl_mat
*var
)
5629 isl_aff
*aff
= NULL
;
5630 isl_basic_set
*bset
= NULL
;
5631 isl_pw_aff
*paff
= NULL
;
5632 isl_space
*pw_space
;
5633 isl_local_space
*ls
= NULL
;
5638 ls
= isl_local_space_from_space(isl_space_copy(space
));
5639 pw_space
= isl_space_copy(space
);
5640 pw_space
= isl_space_from_domain(pw_space
);
5641 pw_space
= isl_space_add_dims(pw_space
, isl_dim_out
, 1);
5642 paff
= isl_pw_aff_alloc_size(pw_space
, var
->n_row
);
5644 for (i
= 0; i
< var
->n_row
; ++i
) {
5647 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
5648 bset
= isl_basic_set_alloc_space(isl_space_copy(space
), 0,
5652 isl_int_set_si(aff
->v
->el
[0], 1);
5653 isl_seq_cpy(aff
->v
->el
+ 1, var
->row
[i
], var
->n_col
);
5654 isl_int_set_si(aff
->v
->el
[1 + var
->n_col
], 0);
5655 bset
= select_minimum(bset
, var
, i
);
5656 paff_i
= isl_pw_aff_alloc(isl_set_from_basic_set(bset
), aff
);
5657 paff
= isl_pw_aff_add_disjoint(paff
, paff_i
);
5660 isl_local_space_free(ls
);
5661 isl_space_free(space
);
5666 isl_basic_set_free(bset
);
5667 isl_pw_aff_free(paff
);
5668 isl_local_space_free(ls
);
5669 isl_space_free(space
);
5674 /* Given a piecewise multi-affine expression of which the last input variable
5675 * is the minimum of the bounds in "cst", plug in the value of the minimum.
5676 * This minimum expression is given in "min_expr_pa".
5677 * The set "min_expr" contains the same information, but in the form of a set.
5678 * The variable is subsequently projected out.
5680 * The implementation is similar to those of "split" and "split_domain".
5681 * If the variable appears in a given expression, then minimum expression
5682 * is plugged in. Otherwise, if the variable appears in the constraints
5683 * and a split is required, then the domain is split. Otherwise, no split
5686 static __isl_give isl_pw_multi_aff
*split_domain_pma(
5687 __isl_take isl_pw_multi_aff
*opt
, __isl_take isl_pw_aff
*min_expr_pa
,
5688 __isl_take isl_set
*min_expr
, __isl_take isl_mat
*cst
)
5693 isl_pw_multi_aff
*res
;
5695 if (!opt
|| !min_expr
|| !cst
)
5698 n_in
= isl_pw_multi_aff_dim(opt
, isl_dim_in
);
5699 space
= isl_pw_multi_aff_get_space(opt
);
5700 space
= isl_space_drop_dims(space
, isl_dim_in
, n_in
- 1, 1);
5701 res
= isl_pw_multi_aff_empty(space
);
5703 for (i
= 0; i
< opt
->n
; ++i
) {
5704 isl_pw_multi_aff
*pma
;
5706 pma
= isl_pw_multi_aff_alloc(isl_set_copy(opt
->p
[i
].set
),
5707 isl_multi_aff_copy(opt
->p
[i
].maff
));
5708 if (need_substitution(opt
->p
[i
].maff
))
5709 pma
= isl_pw_multi_aff_substitute(pma
,
5710 isl_dim_in
, n_in
- 1, min_expr_pa
);
5713 split
= need_split_set(opt
->p
[i
].set
, cst
);
5715 pma
= isl_pw_multi_aff_free(pma
);
5717 pma
= isl_pw_multi_aff_intersect_domain(pma
,
5718 isl_set_copy(min_expr
));
5720 pma
= isl_pw_multi_aff_project_out(pma
,
5721 isl_dim_in
, n_in
- 1, 1);
5723 res
= isl_pw_multi_aff_add_disjoint(res
, pma
);
5726 isl_pw_multi_aff_free(opt
);
5727 isl_pw_aff_free(min_expr_pa
);
5728 isl_set_free(min_expr
);
5732 isl_pw_multi_aff_free(opt
);
5733 isl_pw_aff_free(min_expr_pa
);
5734 isl_set_free(min_expr
);
5739 static __isl_give isl_pw_multi_aff
*basic_map_partial_lexopt_pw_multi_aff(
5740 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5741 __isl_give isl_set
**empty
, int max
);
5743 /* This function is called from basic_map_partial_lexopt_symm.
5744 * The last variable of "bmap" and "dom" corresponds to the minimum
5745 * of the bounds in "cst". "map_space" is the space of the original
5746 * input relation (of basic_map_partial_lexopt_symm) and "set_space"
5747 * is the space of the original domain.
5749 * We recursively call basic_map_partial_lexopt and then plug in
5750 * the definition of the minimum in the result.
5752 static __isl_give isl_pw_multi_aff
*
5753 basic_map_partial_lexopt_symm_core_pw_multi_aff(
5754 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5755 __isl_give isl_set
**empty
, int max
, __isl_take isl_mat
*cst
,
5756 __isl_take isl_space
*map_space
, __isl_take isl_space
*set_space
)
5758 isl_pw_multi_aff
*opt
;
5759 isl_pw_aff
*min_expr_pa
;
5762 min_expr
= set_minimum(isl_basic_set_get_space(dom
), isl_mat_copy(cst
));
5763 min_expr_pa
= set_minimum_pa(isl_basic_set_get_space(dom
),
5766 opt
= basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, empty
, max
);
5769 *empty
= split(*empty
,
5770 isl_set_copy(min_expr
), isl_mat_copy(cst
));
5771 *empty
= isl_set_reset_space(*empty
, set_space
);
5774 opt
= split_domain_pma(opt
, min_expr_pa
, min_expr
, cst
);
5775 opt
= isl_pw_multi_aff_reset_space(opt
, map_space
);
5781 #define TYPE isl_pw_multi_aff
5783 #define SUFFIX _pw_multi_aff
5784 #include "isl_tab_lexopt_templ.c"