2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2016-2017 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 two 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.
189 struct isl_context
*context
;
190 struct isl_partial_sol
*partial
;
191 void (*add
)(struct isl_sol
*sol
,
192 __isl_take isl_basic_set
*dom
, __isl_take isl_multi_aff
*ma
);
193 void (*add_empty
)(struct isl_sol
*sol
, struct isl_basic_set
*bset
);
194 void (*free
)(struct isl_sol
*sol
);
195 struct isl_sol_callback dec_level
;
198 static void sol_free(struct isl_sol
*sol
)
200 struct isl_partial_sol
*partial
, *next
;
203 for (partial
= sol
->partial
; partial
; partial
= next
) {
204 next
= partial
->next
;
205 isl_basic_set_free(partial
->dom
);
206 isl_multi_aff_free(partial
->ma
);
209 isl_space_free(sol
->space
);
211 sol
->context
->op
->free(sol
->context
);
216 /* Push a partial solution represented by a domain and function "ma"
217 * onto the stack of partial solutions.
218 * If "ma" is NULL, then "dom" represents a part of the domain
221 static void sol_push_sol(struct isl_sol
*sol
,
222 __isl_take isl_basic_set
*dom
, __isl_take isl_multi_aff
*ma
)
224 struct isl_partial_sol
*partial
;
226 if (sol
->error
|| !dom
)
229 partial
= isl_alloc_type(dom
->ctx
, struct isl_partial_sol
);
233 partial
->level
= sol
->level
;
236 partial
->next
= sol
->partial
;
238 sol
->partial
= partial
;
242 isl_basic_set_free(dom
);
243 isl_multi_aff_free(ma
);
247 /* Check that the final columns of "M", starting at "first", are zero.
249 static isl_stat
check_final_columns_are_zero(__isl_keep isl_mat
*M
,
256 rows
= isl_mat_rows(M
);
257 cols
= isl_mat_cols(M
);
258 if (rows
< 0 || cols
< 0)
259 return isl_stat_error
;
261 for (i
= 0; i
< rows
; ++i
)
262 if (isl_seq_first_non_zero(M
->row
[i
] + first
, n
) != -1)
263 isl_die(isl_mat_get_ctx(M
), isl_error_internal
,
264 "final columns should be zero",
265 return isl_stat_error
);
269 /* Set the affine expressions in "ma" according to the rows in "M", which
270 * are defined over the local space "ls".
271 * The matrix "M" may have extra (zero) columns beyond the number
272 * of variables in "ls".
274 static __isl_give isl_multi_aff
*set_from_affine_matrix(
275 __isl_take isl_multi_aff
*ma
, __isl_take isl_local_space
*ls
,
276 __isl_take isl_mat
*M
)
282 dim
= isl_local_space_dim(ls
, isl_dim_all
);
283 if (!ma
|| dim
< 0 || !M
)
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
)
336 n_div
= isl_basic_set_dim(dom
, isl_dim_div
);
339 n_known
= n_div
- sol
->context
->n_unknown
;
341 ma
= isl_multi_aff_alloc(isl_space_copy(sol
->space
));
342 ls
= isl_basic_set_get_local_space(dom
);
343 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
,
344 n_known
, n_div
- n_known
);
345 ma
= set_from_affine_matrix(ma
, ls
, M
);
348 dom
= isl_basic_set_free(dom
);
349 sol_push_sol(sol
, dom
, ma
);
352 isl_basic_set_free(dom
);
354 sol_push_sol(sol
, NULL
, NULL
);
357 /* Pop one partial solution from the partial solution stack and
358 * pass it on to sol->add or sol->add_empty.
360 static void sol_pop_one(struct isl_sol
*sol
)
362 struct isl_partial_sol
*partial
;
364 partial
= sol
->partial
;
365 sol
->partial
= partial
->next
;
368 sol
->add(sol
, partial
->dom
, partial
->ma
);
370 sol
->add_empty(sol
, partial
->dom
);
374 /* Return a fresh copy of the domain represented by the context tableau.
376 static struct isl_basic_set
*sol_domain(struct isl_sol
*sol
)
378 struct isl_basic_set
*bset
;
383 bset
= isl_basic_set_dup(sol
->context
->op
->peek_basic_set(sol
->context
));
384 bset
= isl_basic_set_update_from_tab(bset
,
385 sol
->context
->op
->peek_tab(sol
->context
));
390 /* Check whether two partial solutions have the same affine expressions.
392 static isl_bool
same_solution(struct isl_partial_sol
*s1
,
393 struct isl_partial_sol
*s2
)
395 if (!s1
->ma
!= !s2
->ma
)
396 return isl_bool_false
;
398 return isl_bool_true
;
400 return isl_multi_aff_plain_is_equal(s1
->ma
, s2
->ma
);
403 /* Swap the initial two partial solutions in "sol".
407 * sol->partial = p1; p1->next = p2; p2->next = p3
411 * sol->partial = p2; p2->next = p1; p1->next = p3
413 static void swap_initial(struct isl_sol
*sol
)
415 struct isl_partial_sol
*partial
;
417 partial
= sol
->partial
;
418 sol
->partial
= partial
->next
;
419 partial
->next
= partial
->next
->next
;
420 sol
->partial
->next
= partial
;
423 /* Combine the initial two partial solution of "sol" into
424 * a partial solution with the current context domain of "sol" and
425 * the function description of the second partial solution in the list.
426 * The level of the new partial solution is set to the current level.
428 * That is, the first two partial solutions (D1,M1) and (D2,M2) are
429 * replaced by (D,M2), where D is the domain of "sol", which is assumed
430 * to be the union of D1 and D2, while M1 is assumed to be equal to M2
433 static isl_stat
combine_initial_into_second(struct isl_sol
*sol
)
435 struct isl_partial_sol
*partial
;
438 partial
= sol
->partial
;
440 bset
= sol_domain(sol
);
441 isl_basic_set_free(partial
->next
->dom
);
442 partial
->next
->dom
= bset
;
443 partial
->next
->level
= sol
->level
;
446 return isl_stat_error
;
448 sol
->partial
= partial
->next
;
449 isl_basic_set_free(partial
->dom
);
450 isl_multi_aff_free(partial
->ma
);
456 /* Are "ma1" and "ma2" equal to each other on "dom"?
458 * Combine "ma1" and "ma2" with "dom" and check if the results are the same.
459 * "dom" may have existentially quantified variables. Eliminate them first
460 * as otherwise they would have to be eliminated twice, in a more complicated
463 static isl_bool
equal_on_domain(__isl_keep isl_multi_aff
*ma1
,
464 __isl_keep isl_multi_aff
*ma2
, __isl_keep isl_basic_set
*dom
)
467 isl_pw_multi_aff
*pma1
, *pma2
;
470 set
= isl_basic_set_compute_divs(isl_basic_set_copy(dom
));
471 pma1
= isl_pw_multi_aff_alloc(isl_set_copy(set
),
472 isl_multi_aff_copy(ma1
));
473 pma2
= isl_pw_multi_aff_alloc(set
, isl_multi_aff_copy(ma2
));
474 equal
= isl_pw_multi_aff_is_equal(pma1
, pma2
);
475 isl_pw_multi_aff_free(pma1
);
476 isl_pw_multi_aff_free(pma2
);
481 /* The initial two partial solutions of "sol" are known to be at
483 * If they represent the same solution (on different parts of the domain),
484 * then combine them into a single solution at the current level.
485 * Otherwise, pop them both.
487 * Even if the two partial solution are not obviously the same,
488 * one may still be a simplification of the other over its own domain.
489 * Also check if the two sets of affine functions are equal when
490 * restricted to one of the domains. If so, combine the two
491 * using the set of affine functions on the other domain.
492 * That is, for two partial solutions (D1,M1) and (D2,M2),
493 * if M1 = M2 on D1, then the pair of partial solutions can
494 * be replaced by (D1+D2,M2) and similarly when M1 = M2 on D2.
496 static isl_stat
combine_initial_if_equal(struct isl_sol
*sol
)
498 struct isl_partial_sol
*partial
;
501 partial
= sol
->partial
;
503 same
= same_solution(partial
, partial
->next
);
505 return isl_stat_error
;
507 return combine_initial_into_second(sol
);
508 if (partial
->ma
&& partial
->next
->ma
) {
509 same
= equal_on_domain(partial
->ma
, partial
->next
->ma
,
512 return isl_stat_error
;
514 return combine_initial_into_second(sol
);
515 same
= equal_on_domain(partial
->ma
, partial
->next
->ma
,
519 return combine_initial_into_second(sol
);
529 /* Pop all solutions from the partial solution stack that were pushed onto
530 * the stack at levels that are deeper than the current level.
531 * If the two topmost elements on the stack have the same level
532 * and represent the same solution, then their domains are combined.
533 * This combined domain is the same as the current context domain
534 * as sol_pop is called each time we move back to a higher level.
535 * If the outer level (0) has been reached, then all partial solutions
536 * at the current level are also popped off.
538 static void sol_pop(struct isl_sol
*sol
)
540 struct isl_partial_sol
*partial
;
545 partial
= sol
->partial
;
549 if (partial
->level
== 0 && sol
->level
== 0) {
550 for (partial
= sol
->partial
; partial
; partial
= sol
->partial
)
555 if (partial
->level
<= sol
->level
)
558 if (partial
->next
&& partial
->next
->level
== partial
->level
) {
559 if (combine_initial_if_equal(sol
) < 0)
564 if (sol
->level
== 0) {
565 for (partial
= sol
->partial
; partial
; partial
= sol
->partial
)
571 error
: sol
->error
= 1;
574 static void sol_dec_level(struct isl_sol
*sol
)
584 static isl_stat
sol_dec_level_wrap(struct isl_tab_callback
*cb
)
586 struct isl_sol_callback
*callback
= (struct isl_sol_callback
*)cb
;
588 sol_dec_level(callback
->sol
);
590 return callback
->sol
->error
? isl_stat_error
: isl_stat_ok
;
593 /* Move down to next level and push callback onto context tableau
594 * to decrease the level again when it gets rolled back across
595 * the current state. That is, dec_level will be called with
596 * the context tableau in the same state as it is when inc_level
599 static void sol_inc_level(struct isl_sol
*sol
)
607 tab
= sol
->context
->op
->peek_tab(sol
->context
);
608 if (isl_tab_push_callback(tab
, &sol
->dec_level
.callback
) < 0)
612 static void scale_rows(struct isl_mat
*mat
, isl_int m
, int n_row
)
616 if (isl_int_is_one(m
))
619 for (i
= 0; i
< n_row
; ++i
)
620 isl_seq_scale(mat
->row
[i
], mat
->row
[i
], m
, mat
->n_col
);
623 /* Add the solution identified by the tableau and the context tableau.
625 * The layout of the variables is as follows.
626 * tab->n_var is equal to the total number of variables in the input
627 * map (including divs that were copied from the context)
628 * + the number of extra divs constructed
629 * Of these, the first tab->n_param and the last tab->n_div variables
630 * correspond to the variables in the context, i.e.,
631 * tab->n_param + tab->n_div = context_tab->n_var
632 * tab->n_param is equal to the number of parameters and input
633 * dimensions in the input map
634 * tab->n_div is equal to the number of divs in the context
636 * If there is no solution, then call add_empty with a basic set
637 * that corresponds to the context tableau. (If add_empty is NULL,
640 * If there is a solution, then first construct a matrix that maps
641 * all dimensions of the context to the output variables, i.e.,
642 * the output dimensions in the input map.
643 * The divs in the input map (if any) that do not correspond to any
644 * div in the context do not appear in the solution.
645 * The algorithm will make sure that they have an integer value,
646 * but these values themselves are of no interest.
647 * We have to be careful not to drop or rearrange any divs in the
648 * context because that would change the meaning of the matrix.
650 * To extract the value of the output variables, it should be noted
651 * that we always use a big parameter M in the main tableau and so
652 * the variable stored in this tableau is not an output variable x itself, but
653 * x' = M + x (in case of minimization)
655 * x' = M - x (in case of maximization)
656 * If x' appears in a column, then its optimal value is zero,
657 * which means that the optimal value of x is an unbounded number
658 * (-M for minimization and M for maximization).
659 * We currently assume that the output dimensions in the original map
660 * are bounded, so this cannot occur.
661 * Similarly, when x' appears in a row, then the coefficient of M in that
662 * row is necessarily 1.
663 * If the row in the tableau represents
664 * d x' = c + d M + e(y)
665 * then, in case of minimization, the corresponding row in the matrix
668 * with a d = m, the (updated) common denominator of the matrix.
669 * In case of maximization, the row will be
672 static void sol_add(struct isl_sol
*sol
, struct isl_tab
*tab
)
674 struct isl_basic_set
*bset
= NULL
;
675 struct isl_mat
*mat
= NULL
;
680 if (sol
->error
|| !tab
)
683 if (tab
->empty
&& !sol
->add_empty
)
685 if (sol
->context
->op
->is_empty(sol
->context
))
688 bset
= sol_domain(sol
);
691 sol_push_sol(sol
, bset
, NULL
);
697 mat
= isl_mat_alloc(tab
->mat
->ctx
, 1 + sol
->n_out
,
698 1 + tab
->n_param
+ tab
->n_div
);
704 isl_seq_clr(mat
->row
[0] + 1, mat
->n_col
- 1);
705 isl_int_set_si(mat
->row
[0][0], 1);
706 for (row
= 0; row
< sol
->n_out
; ++row
) {
707 int i
= tab
->n_param
+ row
;
710 isl_seq_clr(mat
->row
[1 + row
], mat
->n_col
);
711 if (!tab
->var
[i
].is_row
) {
713 isl_die(mat
->ctx
, isl_error_invalid
,
714 "unbounded optimum", goto error2
);
718 r
= tab
->var
[i
].index
;
720 isl_int_ne(tab
->mat
->row
[r
][2], tab
->mat
->row
[r
][0]))
721 isl_die(mat
->ctx
, isl_error_invalid
,
722 "unbounded optimum", goto error2
);
723 isl_int_gcd(m
, mat
->row
[0][0], tab
->mat
->row
[r
][0]);
724 isl_int_divexact(m
, tab
->mat
->row
[r
][0], m
);
725 scale_rows(mat
, m
, 1 + row
);
726 isl_int_divexact(m
, mat
->row
[0][0], tab
->mat
->row
[r
][0]);
727 isl_int_mul(mat
->row
[1 + row
][0], m
, tab
->mat
->row
[r
][1]);
728 for (j
= 0; j
< tab
->n_param
; ++j
) {
730 if (tab
->var
[j
].is_row
)
732 col
= tab
->var
[j
].index
;
733 isl_int_mul(mat
->row
[1 + row
][1 + j
], m
,
734 tab
->mat
->row
[r
][off
+ col
]);
736 for (j
= 0; j
< tab
->n_div
; ++j
) {
738 if (tab
->var
[tab
->n_var
- tab
->n_div
+j
].is_row
)
740 col
= tab
->var
[tab
->n_var
- tab
->n_div
+j
].index
;
741 isl_int_mul(mat
->row
[1 + row
][1 + tab
->n_param
+ j
], m
,
742 tab
->mat
->row
[r
][off
+ col
]);
745 isl_seq_neg(mat
->row
[1 + row
], mat
->row
[1 + row
],
751 sol_push_sol_mat(sol
, bset
, mat
);
756 isl_basic_set_free(bset
);
764 struct isl_set
*empty
;
767 static void sol_map_free(struct isl_sol
*sol
)
769 struct isl_sol_map
*sol_map
= (struct isl_sol_map
*) sol
;
770 isl_map_free(sol_map
->map
);
771 isl_set_free(sol_map
->empty
);
774 /* This function is called for parts of the context where there is
775 * no solution, with "bset" corresponding to the context tableau.
776 * Simply add the basic set to the set "empty".
778 static void sol_map_add_empty(struct isl_sol_map
*sol
,
779 struct isl_basic_set
*bset
)
781 if (!bset
|| !sol
->empty
)
784 sol
->empty
= isl_set_grow(sol
->empty
, 1);
785 bset
= isl_basic_set_simplify(bset
);
786 bset
= isl_basic_set_finalize(bset
);
787 sol
->empty
= isl_set_add_basic_set(sol
->empty
, isl_basic_set_copy(bset
));
790 isl_basic_set_free(bset
);
793 isl_basic_set_free(bset
);
797 static void sol_map_add_empty_wrap(struct isl_sol
*sol
,
798 struct isl_basic_set
*bset
)
800 sol_map_add_empty((struct isl_sol_map
*)sol
, bset
);
803 /* Given a basic set "dom" that represents the context and a tuple of
804 * affine expressions "ma" defined over this domain, construct a basic map
805 * that expresses this function on the domain.
807 static void sol_map_add(struct isl_sol_map
*sol
,
808 __isl_take isl_basic_set
*dom
, __isl_take isl_multi_aff
*ma
)
812 if (sol
->sol
.error
|| !dom
|| !ma
)
815 bmap
= isl_basic_map_from_multi_aff2(ma
, sol
->sol
.rational
);
816 bmap
= isl_basic_map_intersect_domain(bmap
, dom
);
817 sol
->map
= isl_map_grow(sol
->map
, 1);
818 sol
->map
= isl_map_add_basic_map(sol
->map
, bmap
);
823 isl_basic_set_free(dom
);
824 isl_multi_aff_free(ma
);
828 static void sol_map_add_wrap(struct isl_sol
*sol
,
829 __isl_take isl_basic_set
*dom
, __isl_take isl_multi_aff
*ma
)
831 sol_map_add((struct isl_sol_map
*)sol
, dom
, ma
);
835 /* Store the "parametric constant" of row "row" of tableau "tab" in "line",
836 * i.e., the constant term and the coefficients of all variables that
837 * appear in the context tableau.
838 * Note that the coefficient of the big parameter M is NOT copied.
839 * The context tableau may not have a big parameter and even when it
840 * does, it is a different big parameter.
842 static void get_row_parameter_line(struct isl_tab
*tab
, int row
, isl_int
*line
)
845 unsigned off
= 2 + tab
->M
;
847 isl_int_set(line
[0], tab
->mat
->row
[row
][1]);
848 for (i
= 0; i
< tab
->n_param
; ++i
) {
849 if (tab
->var
[i
].is_row
)
850 isl_int_set_si(line
[1 + i
], 0);
852 int col
= tab
->var
[i
].index
;
853 isl_int_set(line
[1 + i
], tab
->mat
->row
[row
][off
+ col
]);
856 for (i
= 0; i
< tab
->n_div
; ++i
) {
857 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
858 isl_int_set_si(line
[1 + tab
->n_param
+ i
], 0);
860 int col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
861 isl_int_set(line
[1 + tab
->n_param
+ i
],
862 tab
->mat
->row
[row
][off
+ col
]);
867 /* Check if rows "row1" and "row2" have identical "parametric constants",
868 * as explained above.
869 * In this case, we also insist that the coefficients of the big parameter
870 * be the same as the values of the constants will only be the same
871 * if these coefficients are also the same.
873 static int identical_parameter_line(struct isl_tab
*tab
, int row1
, int row2
)
876 unsigned off
= 2 + tab
->M
;
878 if (isl_int_ne(tab
->mat
->row
[row1
][1], tab
->mat
->row
[row2
][1]))
881 if (tab
->M
&& isl_int_ne(tab
->mat
->row
[row1
][2],
882 tab
->mat
->row
[row2
][2]))
885 for (i
= 0; i
< tab
->n_param
+ tab
->n_div
; ++i
) {
886 int pos
= i
< tab
->n_param
? i
:
887 tab
->n_var
- tab
->n_div
+ i
- tab
->n_param
;
890 if (tab
->var
[pos
].is_row
)
892 col
= tab
->var
[pos
].index
;
893 if (isl_int_ne(tab
->mat
->row
[row1
][off
+ col
],
894 tab
->mat
->row
[row2
][off
+ col
]))
900 /* Return an inequality that expresses that the "parametric constant"
901 * should be non-negative.
902 * This function is only called when the coefficient of the big parameter
905 static struct isl_vec
*get_row_parameter_ineq(struct isl_tab
*tab
, int row
)
907 struct isl_vec
*ineq
;
909 ineq
= isl_vec_alloc(tab
->mat
->ctx
, 1 + tab
->n_param
+ tab
->n_div
);
913 get_row_parameter_line(tab
, row
, ineq
->el
);
915 ineq
= isl_vec_normalize(ineq
);
920 /* Normalize a div expression of the form
922 * [(g*f(x) + c)/(g * m)]
924 * with c the constant term and f(x) the remaining coefficients, to
928 static void normalize_div(__isl_keep isl_vec
*div
)
930 isl_ctx
*ctx
= isl_vec_get_ctx(div
);
931 int len
= div
->size
- 2;
933 isl_seq_gcd(div
->el
+ 2, len
, &ctx
->normalize_gcd
);
934 isl_int_gcd(ctx
->normalize_gcd
, ctx
->normalize_gcd
, div
->el
[0]);
936 if (isl_int_is_one(ctx
->normalize_gcd
))
939 isl_int_divexact(div
->el
[0], div
->el
[0], ctx
->normalize_gcd
);
940 isl_int_fdiv_q(div
->el
[1], div
->el
[1], ctx
->normalize_gcd
);
941 isl_seq_scale_down(div
->el
+ 2, div
->el
+ 2, ctx
->normalize_gcd
, len
);
944 /* Return an integer division for use in a parametric cut based
946 * In particular, let the parametric constant of the row be
950 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
951 * The div returned is equal to
953 * floor(\sum_i {-a_i} y_i) = floor((\sum_i (-a_i mod d) y_i)/d)
955 static struct isl_vec
*get_row_parameter_div(struct isl_tab
*tab
, int row
)
959 div
= isl_vec_alloc(tab
->mat
->ctx
, 1 + 1 + tab
->n_param
+ tab
->n_div
);
963 isl_int_set(div
->el
[0], tab
->mat
->row
[row
][0]);
964 get_row_parameter_line(tab
, row
, div
->el
+ 1);
965 isl_seq_neg(div
->el
+ 1, div
->el
+ 1, div
->size
- 1);
967 isl_seq_fdiv_r(div
->el
+ 1, div
->el
+ 1, div
->el
[0], div
->size
- 1);
972 /* Return an integer division for use in transferring an integrality constraint
974 * In particular, let the parametric constant of the row be
978 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
979 * The the returned div is equal to
981 * floor(\sum_i {a_i} y_i) = floor((\sum_i (a_i mod d) y_i)/d)
983 static struct isl_vec
*get_row_split_div(struct isl_tab
*tab
, int row
)
987 div
= isl_vec_alloc(tab
->mat
->ctx
, 1 + 1 + tab
->n_param
+ tab
->n_div
);
991 isl_int_set(div
->el
[0], tab
->mat
->row
[row
][0]);
992 get_row_parameter_line(tab
, row
, div
->el
+ 1);
994 isl_seq_fdiv_r(div
->el
+ 1, div
->el
+ 1, div
->el
[0], div
->size
- 1);
999 /* Construct and return an inequality that expresses an upper bound
1001 * In particular, if the div is given by
1005 * then the inequality expresses
1009 static __isl_give isl_vec
*ineq_for_div(__isl_keep isl_basic_set
*bset
,
1014 struct isl_vec
*ineq
;
1016 total
= isl_basic_set_dim(bset
, isl_dim_all
);
1020 div_pos
= 1 + total
- bset
->n_div
+ div
;
1022 ineq
= isl_vec_alloc(bset
->ctx
, 1 + total
);
1026 isl_seq_cpy(ineq
->el
, bset
->div
[div
] + 1, 1 + total
);
1027 isl_int_neg(ineq
->el
[div_pos
], bset
->div
[div
][0]);
1031 /* Given a row in the tableau and a div that was created
1032 * using get_row_split_div and that has been constrained to equality, i.e.,
1034 * d = floor(\sum_i {a_i} y_i) = \sum_i {a_i} y_i
1036 * replace the expression "\sum_i {a_i} y_i" in the row by d,
1037 * i.e., we subtract "\sum_i {a_i} y_i" and add 1 d.
1038 * The coefficients of the non-parameters in the tableau have been
1039 * verified to be integral. We can therefore simply replace coefficient b
1040 * by floor(b). For the coefficients of the parameters we have
1041 * floor(a_i) = a_i - {a_i}, while for the other coefficients, we have
1044 static struct isl_tab
*set_row_cst_to_div(struct isl_tab
*tab
, int row
, int div
)
1046 isl_seq_fdiv_q(tab
->mat
->row
[row
] + 1, tab
->mat
->row
[row
] + 1,
1047 tab
->mat
->row
[row
][0], 1 + tab
->M
+ tab
->n_col
);
1049 isl_int_set_si(tab
->mat
->row
[row
][0], 1);
1051 if (tab
->var
[tab
->n_var
- tab
->n_div
+ div
].is_row
) {
1052 int drow
= tab
->var
[tab
->n_var
- tab
->n_div
+ div
].index
;
1054 isl_assert(tab
->mat
->ctx
,
1055 isl_int_is_one(tab
->mat
->row
[drow
][0]), goto error
);
1056 isl_seq_combine(tab
->mat
->row
[row
] + 1,
1057 tab
->mat
->ctx
->one
, tab
->mat
->row
[row
] + 1,
1058 tab
->mat
->ctx
->one
, tab
->mat
->row
[drow
] + 1,
1059 1 + tab
->M
+ tab
->n_col
);
1061 int dcol
= tab
->var
[tab
->n_var
- tab
->n_div
+ div
].index
;
1063 isl_int_add_ui(tab
->mat
->row
[row
][2 + tab
->M
+ dcol
],
1064 tab
->mat
->row
[row
][2 + tab
->M
+ dcol
], 1);
1073 /* Check if the (parametric) constant of the given row is obviously
1074 * negative, meaning that we don't need to consult the context tableau.
1075 * If there is a big parameter and its coefficient is non-zero,
1076 * then this coefficient determines the outcome.
1077 * Otherwise, we check whether the constant is negative and
1078 * all non-zero coefficients of parameters are negative and
1079 * belong to non-negative parameters.
1081 static int is_obviously_neg(struct isl_tab
*tab
, int row
)
1085 unsigned off
= 2 + tab
->M
;
1088 if (isl_int_is_pos(tab
->mat
->row
[row
][2]))
1090 if (isl_int_is_neg(tab
->mat
->row
[row
][2]))
1094 if (isl_int_is_nonneg(tab
->mat
->row
[row
][1]))
1096 for (i
= 0; i
< tab
->n_param
; ++i
) {
1097 /* Eliminated parameter */
1098 if (tab
->var
[i
].is_row
)
1100 col
= tab
->var
[i
].index
;
1101 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
1103 if (!tab
->var
[i
].is_nonneg
)
1105 if (isl_int_is_pos(tab
->mat
->row
[row
][off
+ col
]))
1108 for (i
= 0; i
< tab
->n_div
; ++i
) {
1109 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
1111 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
1112 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
1114 if (!tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_nonneg
)
1116 if (isl_int_is_pos(tab
->mat
->row
[row
][off
+ col
]))
1122 /* Check if the (parametric) constant of the given row is obviously
1123 * non-negative, meaning that we don't need to consult the context tableau.
1124 * If there is a big parameter and its coefficient is non-zero,
1125 * then this coefficient determines the outcome.
1126 * Otherwise, we check whether the constant is non-negative and
1127 * all non-zero coefficients of parameters are positive and
1128 * belong to non-negative parameters.
1130 static int is_obviously_nonneg(struct isl_tab
*tab
, int row
)
1134 unsigned off
= 2 + tab
->M
;
1137 if (isl_int_is_pos(tab
->mat
->row
[row
][2]))
1139 if (isl_int_is_neg(tab
->mat
->row
[row
][2]))
1143 if (isl_int_is_neg(tab
->mat
->row
[row
][1]))
1145 for (i
= 0; i
< tab
->n_param
; ++i
) {
1146 /* Eliminated parameter */
1147 if (tab
->var
[i
].is_row
)
1149 col
= tab
->var
[i
].index
;
1150 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
1152 if (!tab
->var
[i
].is_nonneg
)
1154 if (isl_int_is_neg(tab
->mat
->row
[row
][off
+ col
]))
1157 for (i
= 0; i
< tab
->n_div
; ++i
) {
1158 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
1160 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
1161 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
1163 if (!tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_nonneg
)
1165 if (isl_int_is_neg(tab
->mat
->row
[row
][off
+ col
]))
1171 /* Given a row r and two columns, return the column that would
1172 * lead to the lexicographically smallest increment in the sample
1173 * solution when leaving the basis in favor of the row.
1174 * Pivoting with column c will increment the sample value by a non-negative
1175 * constant times a_{V,c}/a_{r,c}, with a_{V,c} the elements of column c
1176 * corresponding to the non-parametric variables.
1177 * If variable v appears in a column c_v, then a_{v,c} = 1 iff c = c_v,
1178 * with all other entries in this virtual row equal to zero.
1179 * If variable v appears in a row, then a_{v,c} is the element in column c
1182 * Let v be the first variable with a_{v,c1}/a_{r,c1} != a_{v,c2}/a_{r,c2}.
1183 * Then if a_{v,c1}/a_{r,c1} < a_{v,c2}/a_{r,c2}, i.e.,
1184 * a_{v,c2} a_{r,c1} - a_{v,c1} a_{r,c2} > 0, c1 results in the minimal
1185 * increment. Otherwise, it's c2.
1187 static int lexmin_col_pair(struct isl_tab
*tab
,
1188 int row
, int col1
, int col2
, isl_int tmp
)
1193 tr
= tab
->mat
->row
[row
] + 2 + tab
->M
;
1195 for (i
= tab
->n_param
; i
< tab
->n_var
- tab
->n_div
; ++i
) {
1199 if (!tab
->var
[i
].is_row
) {
1200 if (tab
->var
[i
].index
== col1
)
1202 if (tab
->var
[i
].index
== col2
)
1207 if (tab
->var
[i
].index
== row
)
1210 r
= tab
->mat
->row
[tab
->var
[i
].index
] + 2 + tab
->M
;
1211 s1
= isl_int_sgn(r
[col1
]);
1212 s2
= isl_int_sgn(r
[col2
]);
1213 if (s1
== 0 && s2
== 0)
1220 isl_int_mul(tmp
, r
[col2
], tr
[col1
]);
1221 isl_int_submul(tmp
, r
[col1
], tr
[col2
]);
1222 if (isl_int_is_pos(tmp
))
1224 if (isl_int_is_neg(tmp
))
1230 /* Does the index into the tab->var or tab->con array "index"
1231 * correspond to a variable in the context tableau?
1232 * In particular, it needs to be an index into the tab->var array and
1233 * it needs to refer to either one of the first tab->n_param variables or
1234 * one of the last tab->n_div variables.
1236 static int is_parameter_var(struct isl_tab
*tab
, int index
)
1240 if (index
< tab
->n_param
)
1242 if (index
>= tab
->n_var
- tab
->n_div
)
1247 /* Does column "col" of "tab" refer to a variable in the context tableau?
1249 static int col_is_parameter_var(struct isl_tab
*tab
, int col
)
1251 return is_parameter_var(tab
, tab
->col_var
[col
]);
1254 /* Does row "row" of "tab" refer to a variable in the context tableau?
1256 static int row_is_parameter_var(struct isl_tab
*tab
, int row
)
1258 return is_parameter_var(tab
, tab
->row_var
[row
]);
1261 /* Given a row in the tableau, find and return the column that would
1262 * result in the lexicographically smallest, but positive, increment
1263 * in the sample point.
1264 * If there is no such column, then return tab->n_col.
1265 * If anything goes wrong, return -1.
1267 static int lexmin_pivot_col(struct isl_tab
*tab
, int row
)
1270 int col
= tab
->n_col
;
1274 tr
= tab
->mat
->row
[row
] + 2 + tab
->M
;
1278 for (j
= tab
->n_dead
; j
< tab
->n_col
; ++j
) {
1279 if (col_is_parameter_var(tab
, j
))
1282 if (!isl_int_is_pos(tr
[j
]))
1285 if (col
== tab
->n_col
)
1288 col
= lexmin_col_pair(tab
, row
, col
, j
, tmp
);
1289 isl_assert(tab
->mat
->ctx
, col
>= 0, goto error
);
1299 /* Return the first known violated constraint, i.e., a non-negative
1300 * constraint that currently has an either obviously negative value
1301 * or a previously determined to be negative value.
1303 * If any constraint has a negative coefficient for the big parameter,
1304 * if any, then we return one of these first.
1306 static int first_neg(struct isl_tab
*tab
)
1311 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
1312 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
1314 if (!isl_int_is_neg(tab
->mat
->row
[row
][2]))
1317 tab
->row_sign
[row
] = isl_tab_row_neg
;
1320 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
1321 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
1323 if (tab
->row_sign
) {
1324 if (tab
->row_sign
[row
] == 0 &&
1325 is_obviously_neg(tab
, row
))
1326 tab
->row_sign
[row
] = isl_tab_row_neg
;
1327 if (tab
->row_sign
[row
] != isl_tab_row_neg
)
1329 } else if (!is_obviously_neg(tab
, row
))
1336 /* Check whether the invariant that all columns are lexico-positive
1337 * is satisfied. This function is not called from the current code
1338 * but is useful during debugging.
1340 static void check_lexpos(struct isl_tab
*tab
) __attribute__ ((unused
));
1341 static void check_lexpos(struct isl_tab
*tab
)
1343 unsigned off
= 2 + tab
->M
;
1348 for (col
= tab
->n_dead
; col
< tab
->n_col
; ++col
) {
1349 if (col_is_parameter_var(tab
, col
))
1351 for (var
= tab
->n_param
; var
< tab
->n_var
- tab
->n_div
; ++var
) {
1352 if (!tab
->var
[var
].is_row
) {
1353 if (tab
->var
[var
].index
== col
)
1358 row
= tab
->var
[var
].index
;
1359 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
1361 if (isl_int_is_pos(tab
->mat
->row
[row
][off
+ col
]))
1363 fprintf(stderr
, "lexneg column %d (row %d)\n",
1366 if (var
>= tab
->n_var
- tab
->n_div
)
1367 fprintf(stderr
, "zero column %d\n", col
);
1371 /* Report to the caller that the given constraint is part of an encountered
1374 static int report_conflicting_constraint(struct isl_tab
*tab
, int con
)
1376 return tab
->conflict(con
, tab
->conflict_user
);
1379 /* Given a conflicting row in the tableau, report all constraints
1380 * involved in the row to the caller. That is, the row itself
1381 * (if it represents a constraint) and all constraint columns with
1382 * non-zero (and therefore negative) coefficients.
1384 static int report_conflict(struct isl_tab
*tab
, int row
)
1392 if (tab
->row_var
[row
] < 0 &&
1393 report_conflicting_constraint(tab
, ~tab
->row_var
[row
]) < 0)
1396 tr
= tab
->mat
->row
[row
] + 2 + tab
->M
;
1398 for (j
= tab
->n_dead
; j
< tab
->n_col
; ++j
) {
1399 if (col_is_parameter_var(tab
, j
))
1402 if (!isl_int_is_neg(tr
[j
]))
1405 if (tab
->col_var
[j
] < 0 &&
1406 report_conflicting_constraint(tab
, ~tab
->col_var
[j
]) < 0)
1413 /* Resolve all known or obviously violated constraints through pivoting.
1414 * In particular, as long as we can find any violated constraint, we
1415 * look for a pivoting column that would result in the lexicographically
1416 * smallest increment in the sample point. If there is no such column
1417 * then the tableau is infeasible.
1419 static int restore_lexmin(struct isl_tab
*tab
) WARN_UNUSED
;
1420 static int restore_lexmin(struct isl_tab
*tab
)
1428 while ((row
= first_neg(tab
)) != -1) {
1429 col
= lexmin_pivot_col(tab
, row
);
1430 if (col
>= tab
->n_col
) {
1431 if (report_conflict(tab
, row
) < 0)
1433 if (isl_tab_mark_empty(tab
) < 0)
1439 if (isl_tab_pivot(tab
, row
, col
) < 0)
1445 /* Given a row that represents an equality, look for an appropriate
1447 * In particular, if there are any non-zero coefficients among
1448 * the non-parameter variables, then we take the last of these
1449 * variables. Eliminating this variable in terms of the other
1450 * variables and/or parameters does not influence the property
1451 * that all column in the initial tableau are lexicographically
1452 * positive. The row corresponding to the eliminated variable
1453 * will only have non-zero entries below the diagonal of the
1454 * initial tableau. That is, we transform
1460 * If there is no such non-parameter variable, then we are dealing with
1461 * pure parameter equality and we pick any parameter with coefficient 1 or -1
1462 * for elimination. This will ensure that the eliminated parameter
1463 * always has an integer value whenever all the other parameters are integral.
1464 * If there is no such parameter then we return -1.
1466 static int last_var_col_or_int_par_col(struct isl_tab
*tab
, int row
)
1468 unsigned off
= 2 + tab
->M
;
1471 for (i
= tab
->n_var
- tab
->n_div
- 1; i
>= 0 && i
>= tab
->n_param
; --i
) {
1473 if (tab
->var
[i
].is_row
)
1475 col
= tab
->var
[i
].index
;
1476 if (col
<= tab
->n_dead
)
1478 if (!isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
1481 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
1482 if (isl_int_is_one(tab
->mat
->row
[row
][off
+ i
]))
1484 if (isl_int_is_negone(tab
->mat
->row
[row
][off
+ i
]))
1490 /* Add an equality that is known to be valid to the tableau.
1491 * We first check if we can eliminate a variable or a parameter.
1492 * If not, we add the equality as two inequalities.
1493 * In this case, the equality was a pure parameter equality and there
1494 * is no need to resolve any constraint violations.
1496 * This function assumes that at least two more rows and at least
1497 * two more elements in the constraint array are available in the tableau.
1499 static struct isl_tab
*add_lexmin_valid_eq(struct isl_tab
*tab
, isl_int
*eq
)
1506 r
= isl_tab_add_row(tab
, eq
);
1510 r
= tab
->con
[r
].index
;
1511 i
= last_var_col_or_int_par_col(tab
, r
);
1513 tab
->con
[r
].is_nonneg
= 1;
1514 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
1516 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
1517 r
= isl_tab_add_row(tab
, eq
);
1520 tab
->con
[r
].is_nonneg
= 1;
1521 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
1524 if (isl_tab_pivot(tab
, r
, i
) < 0)
1526 if (isl_tab_kill_col(tab
, i
) < 0)
1537 /* Check if the given row is a pure constant.
1539 static int is_constant(struct isl_tab
*tab
, int row
)
1541 unsigned off
= 2 + tab
->M
;
1543 return isl_seq_first_non_zero(tab
->mat
->row
[row
] + off
+ tab
->n_dead
,
1544 tab
->n_col
- tab
->n_dead
) == -1;
1547 /* Is the given row a parametric constant?
1548 * That is, does it only involve variables that also appear in the context?
1550 static int is_parametric_constant(struct isl_tab
*tab
, int row
)
1552 unsigned off
= 2 + tab
->M
;
1555 for (col
= tab
->n_dead
; col
< tab
->n_col
; ++col
) {
1556 if (col_is_parameter_var(tab
, col
))
1558 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
1566 /* Add an equality that may or may not be valid to the tableau.
1567 * If the resulting row is a pure constant, then it must be zero.
1568 * Otherwise, the resulting tableau is empty.
1570 * If the row is not a pure constant, then we add two inequalities,
1571 * each time checking that they can be satisfied.
1572 * In the end we try to use one of the two constraints to eliminate
1575 * This function assumes that at least two more rows and at least
1576 * two more elements in the constraint array are available in the tableau.
1578 static int add_lexmin_eq(struct isl_tab
*tab
, isl_int
*eq
) WARN_UNUSED
;
1579 static int add_lexmin_eq(struct isl_tab
*tab
, isl_int
*eq
)
1583 struct isl_tab_undo
*snap
;
1587 snap
= isl_tab_snap(tab
);
1588 r1
= isl_tab_add_row(tab
, eq
);
1591 tab
->con
[r1
].is_nonneg
= 1;
1592 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r1
]) < 0)
1595 row
= tab
->con
[r1
].index
;
1596 if (is_constant(tab
, row
)) {
1597 if (!isl_int_is_zero(tab
->mat
->row
[row
][1]) ||
1598 (tab
->M
&& !isl_int_is_zero(tab
->mat
->row
[row
][2]))) {
1599 if (isl_tab_mark_empty(tab
) < 0)
1603 if (isl_tab_rollback(tab
, snap
) < 0)
1608 if (restore_lexmin(tab
) < 0)
1613 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
1615 r2
= isl_tab_add_row(tab
, eq
);
1618 tab
->con
[r2
].is_nonneg
= 1;
1619 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r2
]) < 0)
1622 if (restore_lexmin(tab
) < 0)
1627 if (!tab
->con
[r1
].is_row
) {
1628 if (isl_tab_kill_col(tab
, tab
->con
[r1
].index
) < 0)
1630 } else if (!tab
->con
[r2
].is_row
) {
1631 if (isl_tab_kill_col(tab
, tab
->con
[r2
].index
) < 0)
1636 tab
->bmap
= isl_basic_map_add_ineq(tab
->bmap
, eq
);
1637 if (isl_tab_push(tab
, isl_tab_undo_bmap_ineq
) < 0)
1639 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
1640 tab
->bmap
= isl_basic_map_add_ineq(tab
->bmap
, eq
);
1641 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
1642 if (isl_tab_push(tab
, isl_tab_undo_bmap_ineq
) < 0)
1651 /* Add an inequality to the tableau, resolving violations using
1654 * This function assumes that at least one more row and at least
1655 * one more element in the constraint array are available in the tableau.
1657 static struct isl_tab
*add_lexmin_ineq(struct isl_tab
*tab
, isl_int
*ineq
)
1664 tab
->bmap
= isl_basic_map_add_ineq(tab
->bmap
, ineq
);
1665 if (isl_tab_push(tab
, isl_tab_undo_bmap_ineq
) < 0)
1670 r
= isl_tab_add_row(tab
, ineq
);
1673 tab
->con
[r
].is_nonneg
= 1;
1674 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
1676 if (isl_tab_row_is_redundant(tab
, tab
->con
[r
].index
)) {
1677 if (isl_tab_mark_redundant(tab
, tab
->con
[r
].index
) < 0)
1682 if (restore_lexmin(tab
) < 0)
1684 if (!tab
->empty
&& tab
->con
[r
].is_row
&&
1685 isl_tab_row_is_redundant(tab
, tab
->con
[r
].index
))
1686 if (isl_tab_mark_redundant(tab
, tab
->con
[r
].index
) < 0)
1694 /* Check if the coefficients of the parameters are all integral.
1696 static int integer_parameter(struct isl_tab
*tab
, int row
)
1700 unsigned off
= 2 + tab
->M
;
1702 for (i
= 0; i
< tab
->n_param
; ++i
) {
1703 /* Eliminated parameter */
1704 if (tab
->var
[i
].is_row
)
1706 col
= tab
->var
[i
].index
;
1707 if (!isl_int_is_divisible_by(tab
->mat
->row
[row
][off
+ col
],
1708 tab
->mat
->row
[row
][0]))
1711 for (i
= 0; i
< tab
->n_div
; ++i
) {
1712 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
1714 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
1715 if (!isl_int_is_divisible_by(tab
->mat
->row
[row
][off
+ col
],
1716 tab
->mat
->row
[row
][0]))
1722 /* Check if the coefficients of the non-parameter variables are all integral.
1724 static int integer_variable(struct isl_tab
*tab
, int row
)
1727 unsigned off
= 2 + tab
->M
;
1729 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
1730 if (col_is_parameter_var(tab
, i
))
1732 if (!isl_int_is_divisible_by(tab
->mat
->row
[row
][off
+ i
],
1733 tab
->mat
->row
[row
][0]))
1739 /* Check if the constant term is integral.
1741 static int integer_constant(struct isl_tab
*tab
, int row
)
1743 return isl_int_is_divisible_by(tab
->mat
->row
[row
][1],
1744 tab
->mat
->row
[row
][0]);
1747 #define I_CST 1 << 0
1748 #define I_PAR 1 << 1
1749 #define I_VAR 1 << 2
1751 /* Check for next (non-parameter) variable after "var" (first if var == -1)
1752 * that is non-integer and therefore requires a cut and return
1753 * the index of the variable.
1754 * For parametric tableaus, there are three parts in a row,
1755 * the constant, the coefficients of the parameters and the rest.
1756 * For each part, we check whether the coefficients in that part
1757 * are all integral and if so, set the corresponding flag in *f.
1758 * If the constant and the parameter part are integral, then the
1759 * current sample value is integral and no cut is required
1760 * (irrespective of whether the variable part is integral).
1762 static int next_non_integer_var(struct isl_tab
*tab
, int var
, int *f
)
1764 var
= var
< 0 ? tab
->n_param
: var
+ 1;
1766 for (; var
< tab
->n_var
- tab
->n_div
; ++var
) {
1769 if (!tab
->var
[var
].is_row
)
1771 row
= tab
->var
[var
].index
;
1772 if (integer_constant(tab
, row
))
1773 ISL_FL_SET(flags
, I_CST
);
1774 if (integer_parameter(tab
, row
))
1775 ISL_FL_SET(flags
, I_PAR
);
1776 if (ISL_FL_ISSET(flags
, I_CST
) && ISL_FL_ISSET(flags
, I_PAR
))
1778 if (integer_variable(tab
, row
))
1779 ISL_FL_SET(flags
, I_VAR
);
1786 /* Check for first (non-parameter) variable that is non-integer and
1787 * therefore requires a cut and return the corresponding row.
1788 * For parametric tableaus, there are three parts in a row,
1789 * the constant, the coefficients of the parameters and the rest.
1790 * For each part, we check whether the coefficients in that part
1791 * are all integral and if so, set the corresponding flag in *f.
1792 * If the constant and the parameter part are integral, then the
1793 * current sample value is integral and no cut is required
1794 * (irrespective of whether the variable part is integral).
1796 static int first_non_integer_row(struct isl_tab
*tab
, int *f
)
1798 int var
= next_non_integer_var(tab
, -1, f
);
1800 return var
< 0 ? -1 : tab
->var
[var
].index
;
1803 /* Add a (non-parametric) cut to cut away the non-integral sample
1804 * value of the given row.
1806 * If the row is given by
1808 * m r = f + \sum_i a_i y_i
1812 * c = - {-f/m} + \sum_i {a_i/m} y_i >= 0
1814 * The big parameter, if any, is ignored, since it is assumed to be big
1815 * enough to be divisible by any integer.
1816 * If the tableau is actually a parametric tableau, then this function
1817 * is only called when all coefficients of the parameters are integral.
1818 * The cut therefore has zero coefficients for the parameters.
1820 * The current value is known to be negative, so row_sign, if it
1821 * exists, is set accordingly.
1823 * Return the row of the cut or -1.
1825 static int add_cut(struct isl_tab
*tab
, int row
)
1830 unsigned off
= 2 + tab
->M
;
1832 if (isl_tab_extend_cons(tab
, 1) < 0)
1834 r
= isl_tab_allocate_con(tab
);
1838 r_row
= tab
->mat
->row
[tab
->con
[r
].index
];
1839 isl_int_set(r_row
[0], tab
->mat
->row
[row
][0]);
1840 isl_int_neg(r_row
[1], tab
->mat
->row
[row
][1]);
1841 isl_int_fdiv_r(r_row
[1], r_row
[1], tab
->mat
->row
[row
][0]);
1842 isl_int_neg(r_row
[1], r_row
[1]);
1844 isl_int_set_si(r_row
[2], 0);
1845 for (i
= 0; i
< tab
->n_col
; ++i
)
1846 isl_int_fdiv_r(r_row
[off
+ i
],
1847 tab
->mat
->row
[row
][off
+ i
], tab
->mat
->row
[row
][0]);
1849 tab
->con
[r
].is_nonneg
= 1;
1850 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
1853 tab
->row_sign
[tab
->con
[r
].index
] = isl_tab_row_neg
;
1855 return tab
->con
[r
].index
;
1861 /* Given a non-parametric tableau, add cuts until an integer
1862 * sample point is obtained or until the tableau is determined
1863 * to be integer infeasible.
1864 * As long as there is any non-integer value in the sample point,
1865 * we add appropriate cuts, if possible, for each of these
1866 * non-integer values and then resolve the violated
1867 * cut constraints using restore_lexmin.
1868 * If one of the corresponding rows is equal to an integral
1869 * combination of variables/constraints plus a non-integral constant,
1870 * then there is no way to obtain an integer point and we return
1871 * a tableau that is marked empty.
1872 * The parameter cutting_strategy controls the strategy used when adding cuts
1873 * to remove non-integer points. CUT_ALL adds all possible cuts
1874 * before continuing the search. CUT_ONE adds only one cut at a time.
1876 static struct isl_tab
*cut_to_integer_lexmin(struct isl_tab
*tab
,
1877 int cutting_strategy
)
1888 while ((var
= next_non_integer_var(tab
, -1, &flags
)) != -1) {
1890 if (ISL_FL_ISSET(flags
, I_VAR
)) {
1891 if (isl_tab_mark_empty(tab
) < 0)
1895 row
= tab
->var
[var
].index
;
1896 row
= add_cut(tab
, row
);
1899 if (cutting_strategy
== CUT_ONE
)
1901 } while ((var
= next_non_integer_var(tab
, var
, &flags
)) != -1);
1902 if (restore_lexmin(tab
) < 0)
1913 /* Check whether all the currently active samples also satisfy the inequality
1914 * "ineq" (treated as an equality if eq is set).
1915 * Remove those samples that do not.
1917 static struct isl_tab
*check_samples(struct isl_tab
*tab
, isl_int
*ineq
, int eq
)
1925 isl_assert(tab
->mat
->ctx
, tab
->bmap
, goto error
);
1926 isl_assert(tab
->mat
->ctx
, tab
->samples
, goto error
);
1927 isl_assert(tab
->mat
->ctx
, tab
->samples
->n_col
== 1 + tab
->n_var
, goto error
);
1930 for (i
= tab
->n_outside
; i
< tab
->n_sample
; ++i
) {
1932 isl_seq_inner_product(ineq
, tab
->samples
->row
[i
],
1933 1 + tab
->n_var
, &v
);
1934 sgn
= isl_int_sgn(v
);
1935 if (eq
? (sgn
== 0) : (sgn
>= 0))
1937 tab
= isl_tab_drop_sample(tab
, i
);
1949 /* Check whether the sample value of the tableau is finite,
1950 * i.e., either the tableau does not use a big parameter, or
1951 * all values of the variables are equal to the big parameter plus
1952 * some constant. This constant is the actual sample value.
1954 static int sample_is_finite(struct isl_tab
*tab
)
1961 for (i
= 0; i
< tab
->n_var
; ++i
) {
1963 if (!tab
->var
[i
].is_row
)
1965 row
= tab
->var
[i
].index
;
1966 if (isl_int_ne(tab
->mat
->row
[row
][0], tab
->mat
->row
[row
][2]))
1972 /* Check if the context tableau of sol has any integer points.
1973 * Leave tab in empty state if no integer point can be found.
1974 * If an integer point can be found and if moreover it is finite,
1975 * then it is added to the list of sample values.
1977 * This function is only called when none of the currently active sample
1978 * values satisfies the most recently added constraint.
1980 static struct isl_tab
*check_integer_feasible(struct isl_tab
*tab
)
1982 struct isl_tab_undo
*snap
;
1987 snap
= isl_tab_snap(tab
);
1988 if (isl_tab_push_basis(tab
) < 0)
1991 tab
= cut_to_integer_lexmin(tab
, CUT_ALL
);
1995 if (!tab
->empty
&& sample_is_finite(tab
)) {
1996 struct isl_vec
*sample
;
1998 sample
= isl_tab_get_sample_value(tab
);
2000 if (isl_tab_add_sample(tab
, sample
) < 0)
2004 if (!tab
->empty
&& isl_tab_rollback(tab
, snap
) < 0)
2013 /* Check if any of the currently active sample values satisfies
2014 * the inequality "ineq" (an equality if eq is set).
2016 static int tab_has_valid_sample(struct isl_tab
*tab
, isl_int
*ineq
, int eq
)
2024 isl_assert(tab
->mat
->ctx
, tab
->bmap
, return -1);
2025 isl_assert(tab
->mat
->ctx
, tab
->samples
, return -1);
2026 isl_assert(tab
->mat
->ctx
, tab
->samples
->n_col
== 1 + tab
->n_var
, return -1);
2029 for (i
= tab
->n_outside
; i
< tab
->n_sample
; ++i
) {
2031 isl_seq_inner_product(ineq
, tab
->samples
->row
[i
],
2032 1 + tab
->n_var
, &v
);
2033 sgn
= isl_int_sgn(v
);
2034 if (eq
? (sgn
== 0) : (sgn
>= 0))
2039 return i
< tab
->n_sample
;
2042 /* Insert a div specified by "div" to the tableau "tab" at position "pos" and
2043 * return isl_bool_true if the div is obviously non-negative.
2045 static isl_bool
context_tab_insert_div(struct isl_tab
*tab
, int pos
,
2046 __isl_keep isl_vec
*div
,
2047 isl_stat (*add_ineq
)(void *user
, isl_int
*), void *user
)
2051 struct isl_mat
*samples
;
2054 r
= isl_tab_insert_div(tab
, pos
, div
, add_ineq
, user
);
2056 return isl_bool_error
;
2057 nonneg
= tab
->var
[r
].is_nonneg
;
2058 tab
->var
[r
].frozen
= 1;
2060 samples
= isl_mat_extend(tab
->samples
,
2061 tab
->n_sample
, 1 + tab
->n_var
);
2062 tab
->samples
= samples
;
2064 return isl_bool_error
;
2065 for (i
= tab
->n_outside
; i
< samples
->n_row
; ++i
) {
2066 isl_seq_inner_product(div
->el
+ 1, samples
->row
[i
],
2067 div
->size
- 1, &samples
->row
[i
][samples
->n_col
- 1]);
2068 isl_int_fdiv_q(samples
->row
[i
][samples
->n_col
- 1],
2069 samples
->row
[i
][samples
->n_col
- 1], div
->el
[0]);
2071 tab
->samples
= isl_mat_move_cols(tab
->samples
, 1 + pos
,
2072 1 + tab
->n_var
- 1, 1);
2074 return isl_bool_error
;
2076 return isl_bool_ok(nonneg
);
2079 /* Add a div specified by "div" to both the main tableau and
2080 * the context tableau. In case of the main tableau, we only
2081 * need to add an extra div. In the context tableau, we also
2082 * need to express the meaning of the div.
2083 * Return the index of the div or -1 if anything went wrong.
2085 * The new integer division is added before any unknown integer
2086 * divisions in the context to ensure that it does not get
2087 * equated to some linear combination involving unknown integer
2090 static int add_div(struct isl_tab
*tab
, struct isl_context
*context
,
2091 __isl_keep isl_vec
*div
)
2096 struct isl_tab
*context_tab
= context
->op
->peek_tab(context
);
2098 if (!tab
|| !context_tab
)
2101 pos
= context_tab
->n_var
- context
->n_unknown
;
2102 if ((nonneg
= context
->op
->insert_div(context
, pos
, div
)) < 0)
2105 if (!context
->op
->is_ok(context
))
2108 pos
= tab
->n_var
- context
->n_unknown
;
2109 if (isl_tab_extend_vars(tab
, 1) < 0)
2111 r
= isl_tab_insert_var(tab
, pos
);
2115 tab
->var
[r
].is_nonneg
= 1;
2116 tab
->var
[r
].frozen
= 1;
2119 return tab
->n_div
- 1 - context
->n_unknown
;
2121 context
->op
->invalidate(context
);
2125 /* Return the position of the integer division that is equal to div/denom
2126 * if there is one. Otherwise, return a position beyond the integer divisions.
2128 static int find_div(struct isl_tab
*tab
, isl_int
*div
, isl_int denom
)
2131 isl_size total
= isl_basic_map_dim(tab
->bmap
, isl_dim_all
);
2134 n_div
= isl_basic_map_dim(tab
->bmap
, isl_dim_div
);
2135 if (total
< 0 || n_div
< 0)
2137 for (i
= 0; i
< n_div
; ++i
) {
2138 if (isl_int_ne(tab
->bmap
->div
[i
][0], denom
))
2140 if (!isl_seq_eq(tab
->bmap
->div
[i
] + 1, div
, 1 + total
))
2147 /* Return the index of a div that corresponds to "div".
2148 * We first check if we already have such a div and if not, we create one.
2150 static int get_div(struct isl_tab
*tab
, struct isl_context
*context
,
2151 struct isl_vec
*div
)
2154 struct isl_tab
*context_tab
= context
->op
->peek_tab(context
);
2160 n_div
= isl_basic_map_dim(context_tab
->bmap
, isl_dim_div
);
2161 d
= find_div(context_tab
, div
->el
+ 1, div
->el
[0]);
2167 return add_div(tab
, context
, div
);
2170 /* Add a parametric cut to cut away the non-integral sample value
2172 * Let a_i be the coefficients of the constant term and the parameters
2173 * and let b_i be the coefficients of the variables or constraints
2174 * in basis of the tableau.
2175 * Let q be the div q = floor(\sum_i {-a_i} y_i).
2177 * The cut is expressed as
2179 * c = \sum_i -{-a_i} y_i + \sum_i {b_i} x_i + q >= 0
2181 * If q did not already exist in the context tableau, then it is added first.
2182 * If q is in a column of the main tableau then the "+ q" can be accomplished
2183 * by setting the corresponding entry to the denominator of the constraint.
2184 * If q happens to be in a row of the main tableau, then the corresponding
2185 * row needs to be added instead (taking care of the denominators).
2186 * Note that this is very unlikely, but perhaps not entirely impossible.
2188 * The current value of the cut is known to be negative (or at least
2189 * non-positive), so row_sign is set accordingly.
2191 * Return the row of the cut or -1.
2193 static int add_parametric_cut(struct isl_tab
*tab
, int row
,
2194 struct isl_context
*context
)
2196 struct isl_vec
*div
;
2203 unsigned off
= 2 + tab
->M
;
2208 div
= get_row_parameter_div(tab
, row
);
2212 n
= tab
->n_div
- context
->n_unknown
;
2213 d
= context
->op
->get_div(context
, tab
, div
);
2218 if (isl_tab_extend_cons(tab
, 1) < 0)
2220 r
= isl_tab_allocate_con(tab
);
2224 r_row
= tab
->mat
->row
[tab
->con
[r
].index
];
2225 isl_int_set(r_row
[0], tab
->mat
->row
[row
][0]);
2226 isl_int_neg(r_row
[1], tab
->mat
->row
[row
][1]);
2227 isl_int_fdiv_r(r_row
[1], r_row
[1], tab
->mat
->row
[row
][0]);
2228 isl_int_neg(r_row
[1], r_row
[1]);
2230 isl_int_set_si(r_row
[2], 0);
2231 for (i
= 0; i
< tab
->n_param
; ++i
) {
2232 if (tab
->var
[i
].is_row
)
2234 col
= tab
->var
[i
].index
;
2235 isl_int_neg(r_row
[off
+ col
], tab
->mat
->row
[row
][off
+ col
]);
2236 isl_int_fdiv_r(r_row
[off
+ col
], r_row
[off
+ col
],
2237 tab
->mat
->row
[row
][0]);
2238 isl_int_neg(r_row
[off
+ col
], r_row
[off
+ col
]);
2240 for (i
= 0; i
< tab
->n_div
; ++i
) {
2241 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
2243 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
2244 isl_int_neg(r_row
[off
+ col
], tab
->mat
->row
[row
][off
+ col
]);
2245 isl_int_fdiv_r(r_row
[off
+ col
], r_row
[off
+ col
],
2246 tab
->mat
->row
[row
][0]);
2247 isl_int_neg(r_row
[off
+ col
], r_row
[off
+ col
]);
2249 for (i
= 0; i
< tab
->n_col
; ++i
) {
2250 if (tab
->col_var
[i
] >= 0 &&
2251 (tab
->col_var
[i
] < tab
->n_param
||
2252 tab
->col_var
[i
] >= tab
->n_var
- tab
->n_div
))
2254 isl_int_fdiv_r(r_row
[off
+ i
],
2255 tab
->mat
->row
[row
][off
+ i
], tab
->mat
->row
[row
][0]);
2257 if (tab
->var
[tab
->n_var
- tab
->n_div
+ d
].is_row
) {
2259 int d_row
= tab
->var
[tab
->n_var
- tab
->n_div
+ d
].index
;
2261 isl_int_gcd(gcd
, tab
->mat
->row
[d_row
][0], r_row
[0]);
2262 isl_int_divexact(r_row
[0], r_row
[0], gcd
);
2263 isl_int_divexact(gcd
, tab
->mat
->row
[d_row
][0], gcd
);
2264 isl_seq_combine(r_row
+ 1, gcd
, r_row
+ 1,
2265 r_row
[0], tab
->mat
->row
[d_row
] + 1,
2266 off
- 1 + tab
->n_col
);
2267 isl_int_mul(r_row
[0], r_row
[0], tab
->mat
->row
[d_row
][0]);
2270 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ d
].index
;
2271 isl_int_set(r_row
[off
+ col
], tab
->mat
->row
[row
][0]);
2274 tab
->con
[r
].is_nonneg
= 1;
2275 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
2278 tab
->row_sign
[tab
->con
[r
].index
] = isl_tab_row_neg
;
2280 row
= tab
->con
[r
].index
;
2282 if (d
>= n
&& context
->op
->detect_equalities(context
, tab
) < 0)
2288 /* Construct a tableau for bmap that can be used for computing
2289 * the lexicographic minimum (or maximum) of bmap.
2290 * If not NULL, then dom is the domain where the minimum
2291 * should be computed. In this case, we set up a parametric
2292 * tableau with row signs (initialized to "unknown").
2293 * If M is set, then the tableau will use a big parameter.
2294 * If max is set, then a maximum should be computed instead of a minimum.
2295 * This means that for each variable x, the tableau will contain the variable
2296 * x' = M - x, rather than x' = M + x. This in turn means that the coefficient
2297 * of the variables in all constraints are negated prior to adding them
2300 static __isl_give
struct isl_tab
*tab_for_lexmin(__isl_keep isl_basic_map
*bmap
,
2301 __isl_keep isl_basic_set
*dom
, unsigned M
, int max
)
2304 struct isl_tab
*tab
;
2309 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
2312 tab
= isl_tab_alloc(bmap
->ctx
, 2 * bmap
->n_eq
+ bmap
->n_ineq
+ 1,
2317 tab
->rational
= ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
2320 dom_total
= isl_basic_set_dim(dom
, isl_dim_all
);
2323 tab
->n_param
= dom_total
- dom
->n_div
;
2324 tab
->n_div
= dom
->n_div
;
2325 tab
->row_sign
= isl_calloc_array(bmap
->ctx
,
2326 enum isl_tab_row_sign
, tab
->mat
->n_row
);
2327 if (tab
->mat
->n_row
&& !tab
->row_sign
)
2330 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
)) {
2331 if (isl_tab_mark_empty(tab
) < 0)
2336 for (i
= tab
->n_param
; i
< tab
->n_var
- tab
->n_div
; ++i
) {
2337 tab
->var
[i
].is_nonneg
= 1;
2338 tab
->var
[i
].frozen
= 1;
2340 o_var
= 1 + tab
->n_param
;
2341 n_var
= tab
->n_var
- tab
->n_param
- tab
->n_div
;
2342 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
2344 isl_seq_neg(bmap
->eq
[i
] + o_var
,
2345 bmap
->eq
[i
] + o_var
, n_var
);
2346 tab
= add_lexmin_valid_eq(tab
, bmap
->eq
[i
]);
2348 isl_seq_neg(bmap
->eq
[i
] + o_var
,
2349 bmap
->eq
[i
] + o_var
, n_var
);
2350 if (!tab
|| tab
->empty
)
2353 if (bmap
->n_eq
&& restore_lexmin(tab
) < 0)
2355 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
2357 isl_seq_neg(bmap
->ineq
[i
] + o_var
,
2358 bmap
->ineq
[i
] + o_var
, n_var
);
2359 tab
= add_lexmin_ineq(tab
, bmap
->ineq
[i
]);
2361 isl_seq_neg(bmap
->ineq
[i
] + o_var
,
2362 bmap
->ineq
[i
] + o_var
, n_var
);
2363 if (!tab
|| tab
->empty
)
2372 /* Given a main tableau where more than one row requires a split,
2373 * determine and return the "best" row to split on.
2375 * If any of the rows requiring a split only involves
2376 * variables that also appear in the context tableau,
2377 * then the negative part is guaranteed not to have a solution.
2378 * It is therefore best to split on any of these rows first.
2381 * given two rows in the main tableau, if the inequality corresponding
2382 * to the first row is redundant with respect to that of the second row
2383 * in the current tableau, then it is better to split on the second row,
2384 * since in the positive part, both rows will be positive.
2385 * (In the negative part a pivot will have to be performed and just about
2386 * anything can happen to the sign of the other row.)
2388 * As a simple heuristic, we therefore select the row that makes the most
2389 * of the other rows redundant.
2391 * Perhaps it would also be useful to look at the number of constraints
2392 * that conflict with any given constraint.
2394 * best is the best row so far (-1 when we have not found any row yet).
2395 * best_r is the number of other rows made redundant by row best.
2396 * When best is still -1, bset_r is meaningless, but it is initialized
2397 * to some arbitrary value (0) anyway. Without this redundant initialization
2398 * valgrind may warn about uninitialized memory accesses when isl
2399 * is compiled with some versions of gcc.
2401 static int best_split(struct isl_tab
*tab
, struct isl_tab
*context_tab
)
2403 struct isl_tab_undo
*snap
;
2409 if (isl_tab_extend_cons(context_tab
, 2) < 0)
2412 snap
= isl_tab_snap(context_tab
);
2414 for (split
= tab
->n_redundant
; split
< tab
->n_row
; ++split
) {
2415 struct isl_tab_undo
*snap2
;
2416 struct isl_vec
*ineq
= NULL
;
2420 if (!isl_tab_var_from_row(tab
, split
)->is_nonneg
)
2422 if (tab
->row_sign
[split
] != isl_tab_row_any
)
2425 if (is_parametric_constant(tab
, split
))
2428 ineq
= get_row_parameter_ineq(tab
, split
);
2431 ok
= isl_tab_add_ineq(context_tab
, ineq
->el
) >= 0;
2436 snap2
= isl_tab_snap(context_tab
);
2438 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
2439 struct isl_tab_var
*var
;
2443 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
2445 if (tab
->row_sign
[row
] != isl_tab_row_any
)
2448 ineq
= get_row_parameter_ineq(tab
, row
);
2451 ok
= isl_tab_add_ineq(context_tab
, ineq
->el
) >= 0;
2455 var
= &context_tab
->con
[context_tab
->n_con
- 1];
2456 if (!context_tab
->empty
&&
2457 !isl_tab_min_at_most_neg_one(context_tab
, var
))
2459 if (isl_tab_rollback(context_tab
, snap2
) < 0)
2462 if (best
== -1 || r
> best_r
) {
2466 if (isl_tab_rollback(context_tab
, snap
) < 0)
2473 static struct isl_basic_set
*context_lex_peek_basic_set(
2474 struct isl_context
*context
)
2476 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2479 return isl_tab_peek_bset(clex
->tab
);
2482 static struct isl_tab
*context_lex_peek_tab(struct isl_context
*context
)
2484 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2488 static void context_lex_add_eq(struct isl_context
*context
, isl_int
*eq
,
2489 int check
, int update
)
2491 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2492 if (isl_tab_extend_cons(clex
->tab
, 2) < 0)
2494 if (add_lexmin_eq(clex
->tab
, eq
) < 0)
2497 int v
= tab_has_valid_sample(clex
->tab
, eq
, 1);
2501 clex
->tab
= check_integer_feasible(clex
->tab
);
2504 clex
->tab
= check_samples(clex
->tab
, eq
, 1);
2507 isl_tab_free(clex
->tab
);
2511 static void context_lex_add_ineq(struct isl_context
*context
, isl_int
*ineq
,
2512 int check
, int update
)
2514 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2515 if (isl_tab_extend_cons(clex
->tab
, 1) < 0)
2517 clex
->tab
= add_lexmin_ineq(clex
->tab
, ineq
);
2519 int v
= tab_has_valid_sample(clex
->tab
, ineq
, 0);
2523 clex
->tab
= check_integer_feasible(clex
->tab
);
2526 clex
->tab
= check_samples(clex
->tab
, ineq
, 0);
2529 isl_tab_free(clex
->tab
);
2533 static isl_stat
context_lex_add_ineq_wrap(void *user
, isl_int
*ineq
)
2535 struct isl_context
*context
= (struct isl_context
*)user
;
2536 context_lex_add_ineq(context
, ineq
, 0, 0);
2537 return context
->op
->is_ok(context
) ? isl_stat_ok
: isl_stat_error
;
2540 /* Check which signs can be obtained by "ineq" on all the currently
2541 * active sample values. See row_sign for more information.
2543 static enum isl_tab_row_sign
tab_ineq_sign(struct isl_tab
*tab
, isl_int
*ineq
,
2549 enum isl_tab_row_sign res
= isl_tab_row_unknown
;
2551 isl_assert(tab
->mat
->ctx
, tab
->samples
, return isl_tab_row_unknown
);
2552 isl_assert(tab
->mat
->ctx
, tab
->samples
->n_col
== 1 + tab
->n_var
,
2553 return isl_tab_row_unknown
);
2556 for (i
= tab
->n_outside
; i
< tab
->n_sample
; ++i
) {
2557 isl_seq_inner_product(tab
->samples
->row
[i
], ineq
,
2558 1 + tab
->n_var
, &tmp
);
2559 sgn
= isl_int_sgn(tmp
);
2560 if (sgn
> 0 || (sgn
== 0 && strict
)) {
2561 if (res
== isl_tab_row_unknown
)
2562 res
= isl_tab_row_pos
;
2563 if (res
== isl_tab_row_neg
)
2564 res
= isl_tab_row_any
;
2567 if (res
== isl_tab_row_unknown
)
2568 res
= isl_tab_row_neg
;
2569 if (res
== isl_tab_row_pos
)
2570 res
= isl_tab_row_any
;
2572 if (res
== isl_tab_row_any
)
2580 static enum isl_tab_row_sign
context_lex_ineq_sign(struct isl_context
*context
,
2581 isl_int
*ineq
, int strict
)
2583 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2584 return tab_ineq_sign(clex
->tab
, ineq
, strict
);
2587 /* Check whether "ineq" can be added to the tableau without rendering
2590 static int context_lex_test_ineq(struct isl_context
*context
, isl_int
*ineq
)
2592 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2593 struct isl_tab_undo
*snap
;
2599 if (isl_tab_extend_cons(clex
->tab
, 1) < 0)
2602 snap
= isl_tab_snap(clex
->tab
);
2603 if (isl_tab_push_basis(clex
->tab
) < 0)
2605 clex
->tab
= add_lexmin_ineq(clex
->tab
, ineq
);
2606 clex
->tab
= check_integer_feasible(clex
->tab
);
2609 feasible
= !clex
->tab
->empty
;
2610 if (isl_tab_rollback(clex
->tab
, snap
) < 0)
2616 static int context_lex_get_div(struct isl_context
*context
, struct isl_tab
*tab
,
2617 struct isl_vec
*div
)
2619 return get_div(tab
, context
, div
);
2622 /* Insert a div specified by "div" to the context tableau at position "pos" and
2623 * return isl_bool_true if the div is obviously non-negative.
2624 * context_tab_add_div will always return isl_bool_true, because all variables
2625 * in a isl_context_lex tableau are non-negative.
2626 * However, if we are using a big parameter in the context, then this only
2627 * reflects the non-negativity of the variable used to _encode_ the
2628 * div, i.e., div' = M + div, so we can't draw any conclusions.
2630 static isl_bool
context_lex_insert_div(struct isl_context
*context
, int pos
,
2631 __isl_keep isl_vec
*div
)
2633 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2635 nonneg
= context_tab_insert_div(clex
->tab
, pos
, div
,
2636 context_lex_add_ineq_wrap
, context
);
2638 return isl_bool_error
;
2640 return isl_bool_false
;
2644 static int context_lex_detect_equalities(struct isl_context
*context
,
2645 struct isl_tab
*tab
)
2650 static int context_lex_best_split(struct isl_context
*context
,
2651 struct isl_tab
*tab
)
2653 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2654 struct isl_tab_undo
*snap
;
2657 snap
= isl_tab_snap(clex
->tab
);
2658 if (isl_tab_push_basis(clex
->tab
) < 0)
2660 r
= best_split(tab
, clex
->tab
);
2662 if (r
>= 0 && isl_tab_rollback(clex
->tab
, snap
) < 0)
2668 static int context_lex_is_empty(struct isl_context
*context
)
2670 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2673 return clex
->tab
->empty
;
2676 static void *context_lex_save(struct isl_context
*context
)
2678 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2679 struct isl_tab_undo
*snap
;
2681 snap
= isl_tab_snap(clex
->tab
);
2682 if (isl_tab_push_basis(clex
->tab
) < 0)
2684 if (isl_tab_save_samples(clex
->tab
) < 0)
2690 static void context_lex_restore(struct isl_context
*context
, void *save
)
2692 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2693 if (isl_tab_rollback(clex
->tab
, (struct isl_tab_undo
*)save
) < 0) {
2694 isl_tab_free(clex
->tab
);
2699 static void context_lex_discard(void *save
)
2703 static int context_lex_is_ok(struct isl_context
*context
)
2705 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2709 /* For each variable in the context tableau, check if the variable can
2710 * only attain non-negative values. If so, mark the parameter as non-negative
2711 * in the main tableau. This allows for a more direct identification of some
2712 * cases of violated constraints.
2714 static struct isl_tab
*tab_detect_nonnegative_parameters(struct isl_tab
*tab
,
2715 struct isl_tab
*context_tab
)
2718 struct isl_tab_undo
*snap
;
2719 struct isl_vec
*ineq
= NULL
;
2720 struct isl_tab_var
*var
;
2723 if (context_tab
->n_var
== 0)
2726 ineq
= isl_vec_alloc(tab
->mat
->ctx
, 1 + context_tab
->n_var
);
2730 if (isl_tab_extend_cons(context_tab
, 1) < 0)
2733 snap
= isl_tab_snap(context_tab
);
2736 isl_seq_clr(ineq
->el
, ineq
->size
);
2737 for (i
= 0; i
< context_tab
->n_var
; ++i
) {
2738 isl_int_set_si(ineq
->el
[1 + i
], 1);
2739 if (isl_tab_add_ineq(context_tab
, ineq
->el
) < 0)
2741 var
= &context_tab
->con
[context_tab
->n_con
- 1];
2742 if (!context_tab
->empty
&&
2743 !isl_tab_min_at_most_neg_one(context_tab
, var
)) {
2745 if (i
>= tab
->n_param
)
2746 j
= i
- tab
->n_param
+ tab
->n_var
- tab
->n_div
;
2747 tab
->var
[j
].is_nonneg
= 1;
2750 isl_int_set_si(ineq
->el
[1 + i
], 0);
2751 if (isl_tab_rollback(context_tab
, snap
) < 0)
2755 if (context_tab
->M
&& n
== context_tab
->n_var
) {
2756 context_tab
->mat
= isl_mat_drop_cols(context_tab
->mat
, 2, 1);
2768 static struct isl_tab
*context_lex_detect_nonnegative_parameters(
2769 struct isl_context
*context
, struct isl_tab
*tab
)
2771 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2772 struct isl_tab_undo
*snap
;
2777 snap
= isl_tab_snap(clex
->tab
);
2778 if (isl_tab_push_basis(clex
->tab
) < 0)
2781 tab
= tab_detect_nonnegative_parameters(tab
, clex
->tab
);
2783 if (isl_tab_rollback(clex
->tab
, snap
) < 0)
2792 static void context_lex_invalidate(struct isl_context
*context
)
2794 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2795 isl_tab_free(clex
->tab
);
2799 static __isl_null
struct isl_context
*context_lex_free(
2800 struct isl_context
*context
)
2802 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2803 isl_tab_free(clex
->tab
);
2809 struct isl_context_op isl_context_lex_op
= {
2810 context_lex_detect_nonnegative_parameters
,
2811 context_lex_peek_basic_set
,
2812 context_lex_peek_tab
,
2814 context_lex_add_ineq
,
2815 context_lex_ineq_sign
,
2816 context_lex_test_ineq
,
2817 context_lex_get_div
,
2818 context_lex_insert_div
,
2819 context_lex_detect_equalities
,
2820 context_lex_best_split
,
2821 context_lex_is_empty
,
2824 context_lex_restore
,
2825 context_lex_discard
,
2826 context_lex_invalidate
,
2830 static struct isl_tab
*context_tab_for_lexmin(__isl_take isl_basic_set
*bset
)
2832 struct isl_tab
*tab
;
2836 tab
= tab_for_lexmin(bset_to_bmap(bset
), NULL
, 1, 0);
2837 if (isl_tab_track_bset(tab
, bset
) < 0)
2839 tab
= isl_tab_init_samples(tab
);
2846 static struct isl_context
*isl_context_lex_alloc(struct isl_basic_set
*dom
)
2848 struct isl_context_lex
*clex
;
2853 clex
= isl_alloc_type(dom
->ctx
, struct isl_context_lex
);
2857 clex
->context
.op
= &isl_context_lex_op
;
2859 clex
->tab
= context_tab_for_lexmin(isl_basic_set_copy(dom
));
2860 if (restore_lexmin(clex
->tab
) < 0)
2862 clex
->tab
= check_integer_feasible(clex
->tab
);
2866 return &clex
->context
;
2868 clex
->context
.op
->free(&clex
->context
);
2872 /* Representation of the context when using generalized basis reduction.
2874 * "shifted" contains the offsets of the unit hypercubes that lie inside the
2875 * context. Any rational point in "shifted" can therefore be rounded
2876 * up to an integer point in the context.
2877 * If the context is constrained by any equality, then "shifted" is not used
2878 * as it would be empty.
2880 struct isl_context_gbr
{
2881 struct isl_context context
;
2882 struct isl_tab
*tab
;
2883 struct isl_tab
*shifted
;
2884 struct isl_tab
*cone
;
2887 static struct isl_tab
*context_gbr_detect_nonnegative_parameters(
2888 struct isl_context
*context
, struct isl_tab
*tab
)
2890 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2893 return tab_detect_nonnegative_parameters(tab
, cgbr
->tab
);
2896 static struct isl_basic_set
*context_gbr_peek_basic_set(
2897 struct isl_context
*context
)
2899 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2902 return isl_tab_peek_bset(cgbr
->tab
);
2905 static struct isl_tab
*context_gbr_peek_tab(struct isl_context
*context
)
2907 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2911 /* Initialize the "shifted" tableau of the context, which
2912 * contains the constraints of the original tableau shifted
2913 * by the sum of all negative coefficients. This ensures
2914 * that any rational point in the shifted tableau can
2915 * be rounded up to yield an integer point in the original tableau.
2917 static void gbr_init_shifted(struct isl_context_gbr
*cgbr
)
2920 struct isl_vec
*cst
;
2921 struct isl_basic_set
*bset
= isl_tab_peek_bset(cgbr
->tab
);
2922 isl_size dim
= isl_basic_set_dim(bset
, isl_dim_all
);
2926 cst
= isl_vec_alloc(cgbr
->tab
->mat
->ctx
, bset
->n_ineq
);
2930 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
2931 isl_int_set(cst
->el
[i
], bset
->ineq
[i
][0]);
2932 for (j
= 0; j
< dim
; ++j
) {
2933 if (!isl_int_is_neg(bset
->ineq
[i
][1 + j
]))
2935 isl_int_add(bset
->ineq
[i
][0], bset
->ineq
[i
][0],
2936 bset
->ineq
[i
][1 + j
]);
2940 cgbr
->shifted
= isl_tab_from_basic_set(bset
, 0);
2942 for (i
= 0; i
< bset
->n_ineq
; ++i
)
2943 isl_int_set(bset
->ineq
[i
][0], cst
->el
[i
]);
2948 /* Check if the shifted tableau is non-empty, and if so
2949 * use the sample point to construct an integer point
2950 * of the context tableau.
2952 static struct isl_vec
*gbr_get_shifted_sample(struct isl_context_gbr
*cgbr
)
2954 struct isl_vec
*sample
;
2957 gbr_init_shifted(cgbr
);
2960 if (cgbr
->shifted
->empty
)
2961 return isl_vec_alloc(cgbr
->tab
->mat
->ctx
, 0);
2963 sample
= isl_tab_get_sample_value(cgbr
->shifted
);
2964 sample
= isl_vec_ceil(sample
);
2969 static __isl_give isl_basic_set
*drop_constant_terms(
2970 __isl_take isl_basic_set
*bset
)
2977 for (i
= 0; i
< bset
->n_eq
; ++i
)
2978 isl_int_set_si(bset
->eq
[i
][0], 0);
2980 for (i
= 0; i
< bset
->n_ineq
; ++i
)
2981 isl_int_set_si(bset
->ineq
[i
][0], 0);
2986 static int use_shifted(struct isl_context_gbr
*cgbr
)
2990 return cgbr
->tab
->bmap
->n_eq
== 0 && cgbr
->tab
->bmap
->n_div
== 0;
2993 static struct isl_vec
*gbr_get_sample(struct isl_context_gbr
*cgbr
)
2995 struct isl_basic_set
*bset
;
2996 struct isl_basic_set
*cone
;
2998 if (isl_tab_sample_is_integer(cgbr
->tab
))
2999 return isl_tab_get_sample_value(cgbr
->tab
);
3001 if (use_shifted(cgbr
)) {
3002 struct isl_vec
*sample
;
3004 sample
= gbr_get_shifted_sample(cgbr
);
3005 if (!sample
|| sample
->size
> 0)
3008 isl_vec_free(sample
);
3012 bset
= isl_tab_peek_bset(cgbr
->tab
);
3013 cgbr
->cone
= isl_tab_from_recession_cone(bset
, 0);
3016 if (isl_tab_track_bset(cgbr
->cone
,
3017 isl_basic_set_copy(bset
)) < 0)
3020 if (isl_tab_detect_implicit_equalities(cgbr
->cone
) < 0)
3023 if (cgbr
->cone
->n_dead
== cgbr
->cone
->n_col
) {
3024 struct isl_vec
*sample
;
3025 struct isl_tab_undo
*snap
;
3027 if (cgbr
->tab
->basis
) {
3028 if (cgbr
->tab
->basis
->n_col
!= 1 + cgbr
->tab
->n_var
) {
3029 isl_mat_free(cgbr
->tab
->basis
);
3030 cgbr
->tab
->basis
= NULL
;
3032 cgbr
->tab
->n_zero
= 0;
3033 cgbr
->tab
->n_unbounded
= 0;
3036 snap
= isl_tab_snap(cgbr
->tab
);
3038 sample
= isl_tab_sample(cgbr
->tab
);
3040 if (!sample
|| isl_tab_rollback(cgbr
->tab
, snap
) < 0) {
3041 isl_vec_free(sample
);
3048 cone
= isl_basic_set_dup(isl_tab_peek_bset(cgbr
->cone
));
3049 cone
= drop_constant_terms(cone
);
3050 cone
= isl_basic_set_update_from_tab(cone
, cgbr
->cone
);
3051 cone
= isl_basic_set_underlying_set(cone
);
3052 cone
= isl_basic_set_gauss(cone
, NULL
);
3054 bset
= isl_basic_set_dup(isl_tab_peek_bset(cgbr
->tab
));
3055 bset
= isl_basic_set_update_from_tab(bset
, cgbr
->tab
);
3056 bset
= isl_basic_set_underlying_set(bset
);
3057 bset
= isl_basic_set_gauss(bset
, NULL
);
3059 return isl_basic_set_sample_with_cone(bset
, cone
);
3062 static void check_gbr_integer_feasible(struct isl_context_gbr
*cgbr
)
3064 struct isl_vec
*sample
;
3069 if (cgbr
->tab
->empty
)
3072 sample
= gbr_get_sample(cgbr
);
3076 if (sample
->size
== 0) {
3077 isl_vec_free(sample
);
3078 if (isl_tab_mark_empty(cgbr
->tab
) < 0)
3083 if (isl_tab_add_sample(cgbr
->tab
, sample
) < 0)
3088 isl_tab_free(cgbr
->tab
);
3092 static struct isl_tab
*add_gbr_eq(struct isl_tab
*tab
, isl_int
*eq
)
3097 if (isl_tab_extend_cons(tab
, 2) < 0)
3100 if (isl_tab_add_eq(tab
, eq
) < 0)
3109 /* Add the equality described by "eq" to the context.
3110 * If "check" is set, then we check if the context is empty after
3111 * adding the equality.
3112 * If "update" is set, then we check if the samples are still valid.
3114 * We do not explicitly add shifted copies of the equality to
3115 * cgbr->shifted since they would conflict with each other.
3116 * Instead, we directly mark cgbr->shifted empty.
3118 static void context_gbr_add_eq(struct isl_context
*context
, isl_int
*eq
,
3119 int check
, int update
)
3121 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3123 cgbr
->tab
= add_gbr_eq(cgbr
->tab
, eq
);
3125 if (cgbr
->shifted
&& !cgbr
->shifted
->empty
&& use_shifted(cgbr
)) {
3126 if (isl_tab_mark_empty(cgbr
->shifted
) < 0)
3130 if (cgbr
->cone
&& cgbr
->cone
->n_col
!= cgbr
->cone
->n_dead
) {
3131 if (isl_tab_extend_cons(cgbr
->cone
, 2) < 0)
3133 if (isl_tab_add_eq(cgbr
->cone
, eq
) < 0)
3138 int v
= tab_has_valid_sample(cgbr
->tab
, eq
, 1);
3142 check_gbr_integer_feasible(cgbr
);
3145 cgbr
->tab
= check_samples(cgbr
->tab
, eq
, 1);
3148 isl_tab_free(cgbr
->tab
);
3152 static void add_gbr_ineq(struct isl_context_gbr
*cgbr
, isl_int
*ineq
)
3157 if (isl_tab_extend_cons(cgbr
->tab
, 1) < 0)
3160 if (isl_tab_add_ineq(cgbr
->tab
, ineq
) < 0)
3163 if (cgbr
->shifted
&& !cgbr
->shifted
->empty
&& use_shifted(cgbr
)) {
3166 dim
= isl_basic_map_dim(cgbr
->tab
->bmap
, isl_dim_all
);
3170 if (isl_tab_extend_cons(cgbr
->shifted
, 1) < 0)
3173 for (i
= 0; i
< dim
; ++i
) {
3174 if (!isl_int_is_neg(ineq
[1 + i
]))
3176 isl_int_add(ineq
[0], ineq
[0], ineq
[1 + i
]);
3179 if (isl_tab_add_ineq(cgbr
->shifted
, ineq
) < 0)
3182 for (i
= 0; i
< dim
; ++i
) {
3183 if (!isl_int_is_neg(ineq
[1 + i
]))
3185 isl_int_sub(ineq
[0], ineq
[0], ineq
[1 + i
]);
3189 if (cgbr
->cone
&& cgbr
->cone
->n_col
!= cgbr
->cone
->n_dead
) {
3190 if (isl_tab_extend_cons(cgbr
->cone
, 1) < 0)
3192 if (isl_tab_add_ineq(cgbr
->cone
, ineq
) < 0)
3198 isl_tab_free(cgbr
->tab
);
3202 static void context_gbr_add_ineq(struct isl_context
*context
, isl_int
*ineq
,
3203 int check
, int update
)
3205 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3207 add_gbr_ineq(cgbr
, ineq
);
3212 int v
= tab_has_valid_sample(cgbr
->tab
, ineq
, 0);
3216 check_gbr_integer_feasible(cgbr
);
3219 cgbr
->tab
= check_samples(cgbr
->tab
, ineq
, 0);
3222 isl_tab_free(cgbr
->tab
);
3226 static isl_stat
context_gbr_add_ineq_wrap(void *user
, isl_int
*ineq
)
3228 struct isl_context
*context
= (struct isl_context
*)user
;
3229 context_gbr_add_ineq(context
, ineq
, 0, 0);
3230 return context
->op
->is_ok(context
) ? isl_stat_ok
: isl_stat_error
;
3233 static enum isl_tab_row_sign
context_gbr_ineq_sign(struct isl_context
*context
,
3234 isl_int
*ineq
, int strict
)
3236 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3237 return tab_ineq_sign(cgbr
->tab
, ineq
, strict
);
3240 /* Check whether "ineq" can be added to the tableau without rendering
3243 static int context_gbr_test_ineq(struct isl_context
*context
, isl_int
*ineq
)
3245 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3246 struct isl_tab_undo
*snap
;
3247 struct isl_tab_undo
*shifted_snap
= NULL
;
3248 struct isl_tab_undo
*cone_snap
= NULL
;
3254 if (isl_tab_extend_cons(cgbr
->tab
, 1) < 0)
3257 snap
= isl_tab_snap(cgbr
->tab
);
3259 shifted_snap
= isl_tab_snap(cgbr
->shifted
);
3261 cone_snap
= isl_tab_snap(cgbr
->cone
);
3262 add_gbr_ineq(cgbr
, ineq
);
3263 check_gbr_integer_feasible(cgbr
);
3266 feasible
= !cgbr
->tab
->empty
;
3267 if (isl_tab_rollback(cgbr
->tab
, snap
) < 0)
3270 if (isl_tab_rollback(cgbr
->shifted
, shifted_snap
))
3272 } else if (cgbr
->shifted
) {
3273 isl_tab_free(cgbr
->shifted
);
3274 cgbr
->shifted
= NULL
;
3277 if (isl_tab_rollback(cgbr
->cone
, cone_snap
))
3279 } else if (cgbr
->cone
) {
3280 isl_tab_free(cgbr
->cone
);
3287 /* Return the column of the last of the variables associated to
3288 * a column that has a non-zero coefficient.
3289 * This function is called in a context where only coefficients
3290 * of parameters or divs can be non-zero.
3292 static int last_non_zero_var_col(struct isl_tab
*tab
, isl_int
*p
)
3297 if (tab
->n_var
== 0)
3300 for (i
= tab
->n_var
- 1; i
>= 0; --i
) {
3301 if (i
>= tab
->n_param
&& i
< tab
->n_var
- tab
->n_div
)
3303 if (tab
->var
[i
].is_row
)
3305 col
= tab
->var
[i
].index
;
3306 if (!isl_int_is_zero(p
[col
]))
3313 /* Look through all the recently added equalities in the context
3314 * to see if we can propagate any of them to the main tableau.
3316 * The newly added equalities in the context are encoded as pairs
3317 * of inequalities starting at inequality "first".
3319 * We tentatively add each of these equalities to the main tableau
3320 * and if this happens to result in a row with a final coefficient
3321 * that is one or negative one, we use it to kill a column
3322 * in the main tableau. Otherwise, we discard the tentatively
3324 * This tentative addition of equality constraints turns
3325 * on the undo facility of the tableau. Turn it off again
3326 * at the end, assuming it was turned off to begin with.
3328 * Return 0 on success and -1 on failure.
3330 static int propagate_equalities(struct isl_context_gbr
*cgbr
,
3331 struct isl_tab
*tab
, unsigned first
)
3334 struct isl_vec
*eq
= NULL
;
3335 isl_bool needs_undo
;
3337 needs_undo
= isl_tab_need_undo(tab
);
3340 eq
= isl_vec_alloc(tab
->mat
->ctx
, 1 + tab
->n_var
);
3344 if (isl_tab_extend_cons(tab
, (cgbr
->tab
->bmap
->n_ineq
- first
)/2) < 0)
3347 isl_seq_clr(eq
->el
+ 1 + tab
->n_param
,
3348 tab
->n_var
- tab
->n_param
- tab
->n_div
);
3349 for (i
= first
; i
< cgbr
->tab
->bmap
->n_ineq
; i
+= 2) {
3352 struct isl_tab_undo
*snap
;
3353 snap
= isl_tab_snap(tab
);
3355 isl_seq_cpy(eq
->el
, cgbr
->tab
->bmap
->ineq
[i
], 1 + tab
->n_param
);
3356 isl_seq_cpy(eq
->el
+ 1 + tab
->n_var
- tab
->n_div
,
3357 cgbr
->tab
->bmap
->ineq
[i
] + 1 + tab
->n_param
,
3360 r
= isl_tab_add_row(tab
, eq
->el
);
3363 r
= tab
->con
[r
].index
;
3364 j
= last_non_zero_var_col(tab
, tab
->mat
->row
[r
] + 2 + tab
->M
);
3365 if (j
< 0 || j
< tab
->n_dead
||
3366 !isl_int_is_one(tab
->mat
->row
[r
][0]) ||
3367 (!isl_int_is_one(tab
->mat
->row
[r
][2 + tab
->M
+ j
]) &&
3368 !isl_int_is_negone(tab
->mat
->row
[r
][2 + tab
->M
+ j
]))) {
3369 if (isl_tab_rollback(tab
, snap
) < 0)
3373 if (isl_tab_pivot(tab
, r
, j
) < 0)
3375 if (isl_tab_kill_col(tab
, j
) < 0)
3378 if (restore_lexmin(tab
) < 0)
3383 isl_tab_clear_undo(tab
);
3389 isl_tab_free(cgbr
->tab
);
3394 static int context_gbr_detect_equalities(struct isl_context
*context
,
3395 struct isl_tab
*tab
)
3397 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3401 struct isl_basic_set
*bset
= isl_tab_peek_bset(cgbr
->tab
);
3402 cgbr
->cone
= isl_tab_from_recession_cone(bset
, 0);
3405 if (isl_tab_track_bset(cgbr
->cone
,
3406 isl_basic_set_copy(bset
)) < 0)
3409 if (isl_tab_detect_implicit_equalities(cgbr
->cone
) < 0)
3412 n_ineq
= cgbr
->tab
->bmap
->n_ineq
;
3413 cgbr
->tab
= isl_tab_detect_equalities(cgbr
->tab
, cgbr
->cone
);
3416 if (cgbr
->tab
->bmap
->n_ineq
> n_ineq
&&
3417 propagate_equalities(cgbr
, tab
, n_ineq
) < 0)
3422 isl_tab_free(cgbr
->tab
);
3427 static int context_gbr_get_div(struct isl_context
*context
, struct isl_tab
*tab
,
3428 struct isl_vec
*div
)
3430 return get_div(tab
, context
, div
);
3433 static isl_bool
context_gbr_insert_div(struct isl_context
*context
, int pos
,
3434 __isl_keep isl_vec
*div
)
3436 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3441 n_div
= isl_basic_map_dim(cgbr
->cone
->bmap
, isl_dim_div
);
3443 return isl_bool_error
;
3444 o_div
= cgbr
->cone
->n_var
- n_div
;
3446 if (isl_tab_extend_cons(cgbr
->cone
, 3) < 0)
3447 return isl_bool_error
;
3448 if (isl_tab_extend_vars(cgbr
->cone
, 1) < 0)
3449 return isl_bool_error
;
3450 if ((r
= isl_tab_insert_var(cgbr
->cone
, pos
)) <0)
3451 return isl_bool_error
;
3453 cgbr
->cone
->bmap
= isl_basic_map_insert_div(cgbr
->cone
->bmap
,
3455 if (!cgbr
->cone
->bmap
)
3456 return isl_bool_error
;
3457 if (isl_tab_push_var(cgbr
->cone
, isl_tab_undo_bmap_div
,
3458 &cgbr
->cone
->var
[r
]) < 0)
3459 return isl_bool_error
;
3461 return context_tab_insert_div(cgbr
->tab
, pos
, div
,
3462 context_gbr_add_ineq_wrap
, context
);
3465 static int context_gbr_best_split(struct isl_context
*context
,
3466 struct isl_tab
*tab
)
3468 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3469 struct isl_tab_undo
*snap
;
3472 snap
= isl_tab_snap(cgbr
->tab
);
3473 r
= best_split(tab
, cgbr
->tab
);
3475 if (r
>= 0 && isl_tab_rollback(cgbr
->tab
, snap
) < 0)
3481 static int context_gbr_is_empty(struct isl_context
*context
)
3483 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3486 return cgbr
->tab
->empty
;
3489 struct isl_gbr_tab_undo
{
3490 struct isl_tab_undo
*tab_snap
;
3491 struct isl_tab_undo
*shifted_snap
;
3492 struct isl_tab_undo
*cone_snap
;
3495 static void *context_gbr_save(struct isl_context
*context
)
3497 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3498 struct isl_gbr_tab_undo
*snap
;
3503 snap
= isl_alloc_type(cgbr
->tab
->mat
->ctx
, struct isl_gbr_tab_undo
);
3507 snap
->tab_snap
= isl_tab_snap(cgbr
->tab
);
3508 if (isl_tab_save_samples(cgbr
->tab
) < 0)
3512 snap
->shifted_snap
= isl_tab_snap(cgbr
->shifted
);
3514 snap
->shifted_snap
= NULL
;
3517 snap
->cone_snap
= isl_tab_snap(cgbr
->cone
);
3519 snap
->cone_snap
= NULL
;
3527 static void context_gbr_restore(struct isl_context
*context
, void *save
)
3529 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3530 struct isl_gbr_tab_undo
*snap
= (struct isl_gbr_tab_undo
*)save
;
3533 if (isl_tab_rollback(cgbr
->tab
, snap
->tab_snap
) < 0)
3536 if (snap
->shifted_snap
) {
3537 if (isl_tab_rollback(cgbr
->shifted
, snap
->shifted_snap
) < 0)
3539 } else if (cgbr
->shifted
) {
3540 isl_tab_free(cgbr
->shifted
);
3541 cgbr
->shifted
= NULL
;
3544 if (snap
->cone_snap
) {
3545 if (isl_tab_rollback(cgbr
->cone
, snap
->cone_snap
) < 0)
3547 } else if (cgbr
->cone
) {
3548 isl_tab_free(cgbr
->cone
);
3557 isl_tab_free(cgbr
->tab
);
3561 static void context_gbr_discard(void *save
)
3563 struct isl_gbr_tab_undo
*snap
= (struct isl_gbr_tab_undo
*)save
;
3567 static int context_gbr_is_ok(struct isl_context
*context
)
3569 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3573 static void context_gbr_invalidate(struct isl_context
*context
)
3575 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3576 isl_tab_free(cgbr
->tab
);
3580 static __isl_null
struct isl_context
*context_gbr_free(
3581 struct isl_context
*context
)
3583 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3584 isl_tab_free(cgbr
->tab
);
3585 isl_tab_free(cgbr
->shifted
);
3586 isl_tab_free(cgbr
->cone
);
3592 struct isl_context_op isl_context_gbr_op
= {
3593 context_gbr_detect_nonnegative_parameters
,
3594 context_gbr_peek_basic_set
,
3595 context_gbr_peek_tab
,
3597 context_gbr_add_ineq
,
3598 context_gbr_ineq_sign
,
3599 context_gbr_test_ineq
,
3600 context_gbr_get_div
,
3601 context_gbr_insert_div
,
3602 context_gbr_detect_equalities
,
3603 context_gbr_best_split
,
3604 context_gbr_is_empty
,
3607 context_gbr_restore
,
3608 context_gbr_discard
,
3609 context_gbr_invalidate
,
3613 static struct isl_context
*isl_context_gbr_alloc(__isl_keep isl_basic_set
*dom
)
3615 struct isl_context_gbr
*cgbr
;
3620 cgbr
= isl_calloc_type(dom
->ctx
, struct isl_context_gbr
);
3624 cgbr
->context
.op
= &isl_context_gbr_op
;
3626 cgbr
->shifted
= NULL
;
3628 cgbr
->tab
= isl_tab_from_basic_set(dom
, 1);
3629 cgbr
->tab
= isl_tab_init_samples(cgbr
->tab
);
3632 check_gbr_integer_feasible(cgbr
);
3634 return &cgbr
->context
;
3636 cgbr
->context
.op
->free(&cgbr
->context
);
3640 /* Allocate a context corresponding to "dom".
3641 * The representation specific fields are initialized by
3642 * isl_context_lex_alloc or isl_context_gbr_alloc.
3643 * The shared "n_unknown" field is initialized to the number
3644 * of final unknown integer divisions in "dom".
3646 static struct isl_context
*isl_context_alloc(__isl_keep isl_basic_set
*dom
)
3648 struct isl_context
*context
;
3655 if (dom
->ctx
->opt
->context
== ISL_CONTEXT_LEXMIN
)
3656 context
= isl_context_lex_alloc(dom
);
3658 context
= isl_context_gbr_alloc(dom
);
3663 first
= isl_basic_set_first_unknown_div(dom
);
3664 n_div
= isl_basic_set_dim(dom
, isl_dim_div
);
3665 if (first
< 0 || n_div
< 0)
3666 return context
->op
->free(context
);
3667 context
->n_unknown
= n_div
- first
;
3672 /* Initialize some common fields of "sol", which keeps track
3673 * of the solution of an optimization problem on "bmap" over
3675 * If "max" is set, then a maximization problem is being solved, rather than
3676 * a minimization problem, which means that the variables in the
3677 * tableau have value "M - x" rather than "M + x".
3679 static isl_stat
sol_init(struct isl_sol
*sol
, __isl_keep isl_basic_map
*bmap
,
3680 __isl_keep isl_basic_set
*dom
, int max
)
3682 sol
->rational
= ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
3683 sol
->dec_level
.callback
.run
= &sol_dec_level_wrap
;
3684 sol
->dec_level
.sol
= sol
;
3686 sol
->n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
3687 sol
->space
= isl_basic_map_get_space(bmap
);
3689 sol
->context
= isl_context_alloc(dom
);
3690 if (sol
->n_out
< 0 || !sol
->space
|| !sol
->context
)
3691 return isl_stat_error
;
3696 /* Construct an isl_sol_map structure for accumulating the solution.
3697 * If track_empty is set, then we also keep track of the parts
3698 * of the context where there is no solution.
3699 * If max is set, then we are solving a maximization, rather than
3700 * a minimization problem, which means that the variables in the
3701 * tableau have value "M - x" rather than "M + x".
3703 static struct isl_sol
*sol_map_init(__isl_keep isl_basic_map
*bmap
,
3704 __isl_take isl_basic_set
*dom
, int track_empty
, int max
)
3706 struct isl_sol_map
*sol_map
= NULL
;
3712 sol_map
= isl_calloc_type(bmap
->ctx
, struct isl_sol_map
);
3716 sol_map
->sol
.free
= &sol_map_free
;
3717 if (sol_init(&sol_map
->sol
, bmap
, dom
, max
) < 0)
3719 sol_map
->sol
.add
= &sol_map_add_wrap
;
3720 sol_map
->sol
.add_empty
= track_empty
? &sol_map_add_empty_wrap
: NULL
;
3721 space
= isl_space_copy(sol_map
->sol
.space
);
3722 sol_map
->map
= isl_map_alloc_space(space
, 1, ISL_MAP_DISJOINT
);
3727 sol_map
->empty
= isl_set_alloc_space(isl_basic_set_get_space(dom
),
3728 1, ISL_SET_DISJOINT
);
3729 if (!sol_map
->empty
)
3733 isl_basic_set_free(dom
);
3734 return &sol_map
->sol
;
3736 isl_basic_set_free(dom
);
3737 sol_free(&sol_map
->sol
);
3741 /* Check whether all coefficients of (non-parameter) variables
3742 * are non-positive, meaning that no pivots can be performed on the row.
3744 static int is_critical(struct isl_tab
*tab
, int row
)
3747 unsigned off
= 2 + tab
->M
;
3749 for (j
= tab
->n_dead
; j
< tab
->n_col
; ++j
) {
3750 if (col_is_parameter_var(tab
, j
))
3753 if (isl_int_is_pos(tab
->mat
->row
[row
][off
+ j
]))
3760 /* Check whether the inequality represented by vec is strict over the integers,
3761 * i.e., there are no integer values satisfying the constraint with
3762 * equality. This happens if the gcd of the coefficients is not a divisor
3763 * of the constant term. If so, scale the constraint down by the gcd
3764 * of the coefficients.
3766 static int is_strict(struct isl_vec
*vec
)
3772 isl_seq_gcd(vec
->el
+ 1, vec
->size
- 1, &gcd
);
3773 if (!isl_int_is_one(gcd
)) {
3774 strict
= !isl_int_is_divisible_by(vec
->el
[0], gcd
);
3775 isl_int_fdiv_q(vec
->el
[0], vec
->el
[0], gcd
);
3776 isl_seq_scale_down(vec
->el
+ 1, vec
->el
+ 1, gcd
, vec
->size
-1);
3783 /* Determine the sign of the given row of the main tableau.
3784 * The result is one of
3785 * isl_tab_row_pos: always non-negative; no pivot needed
3786 * isl_tab_row_neg: always non-positive; pivot
3787 * isl_tab_row_any: can be both positive and negative; split
3789 * We first handle some simple cases
3790 * - the row sign may be known already
3791 * - the row may be obviously non-negative
3792 * - the parametric constant may be equal to that of another row
3793 * for which we know the sign. This sign will be either "pos" or
3794 * "any". If it had been "neg" then we would have pivoted before.
3796 * If none of these cases hold, we check the value of the row for each
3797 * of the currently active samples. Based on the signs of these values
3798 * we make an initial determination of the sign of the row.
3800 * all zero -> unk(nown)
3801 * all non-negative -> pos
3802 * all non-positive -> neg
3803 * both negative and positive -> all
3805 * If we end up with "all", we are done.
3806 * Otherwise, we perform a check for positive and/or negative
3807 * values as follows.
3809 * samples neg unk pos
3815 * There is no special sign for "zero", because we can usually treat zero
3816 * as either non-negative or non-positive, whatever works out best.
3817 * However, if the row is "critical", meaning that pivoting is impossible
3818 * then we don't want to limp zero with the non-positive case, because
3819 * then we we would lose the solution for those values of the parameters
3820 * where the value of the row is zero. Instead, we treat 0 as non-negative
3821 * ensuring a split if the row can attain both zero and negative values.
3822 * The same happens when the original constraint was one that could not
3823 * be satisfied with equality by any integer values of the parameters.
3824 * In this case, we normalize the constraint, but then a value of zero
3825 * for the normalized constraint is actually a positive value for the
3826 * original constraint, so again we need to treat zero as non-negative.
3827 * In both these cases, we have the following decision tree instead:
3829 * all non-negative -> pos
3830 * all negative -> neg
3831 * both negative and non-negative -> all
3839 static enum isl_tab_row_sign
row_sign(struct isl_tab
*tab
,
3840 struct isl_sol
*sol
, int row
)
3842 struct isl_vec
*ineq
= NULL
;
3843 enum isl_tab_row_sign res
= isl_tab_row_unknown
;
3848 if (tab
->row_sign
[row
] != isl_tab_row_unknown
)
3849 return tab
->row_sign
[row
];
3850 if (is_obviously_nonneg(tab
, row
))
3851 return isl_tab_row_pos
;
3852 for (row2
= tab
->n_redundant
; row2
< tab
->n_row
; ++row2
) {
3853 if (tab
->row_sign
[row2
] == isl_tab_row_unknown
)
3855 if (identical_parameter_line(tab
, row
, row2
))
3856 return tab
->row_sign
[row2
];
3859 critical
= is_critical(tab
, row
);
3861 ineq
= get_row_parameter_ineq(tab
, row
);
3865 strict
= is_strict(ineq
);
3867 res
= sol
->context
->op
->ineq_sign(sol
->context
, ineq
->el
,
3868 critical
|| strict
);
3870 if (res
== isl_tab_row_unknown
|| res
== isl_tab_row_pos
) {
3871 /* test for negative values */
3873 isl_seq_neg(ineq
->el
, ineq
->el
, ineq
->size
);
3874 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3876 feasible
= sol
->context
->op
->test_ineq(sol
->context
, ineq
->el
);
3880 res
= isl_tab_row_pos
;
3882 res
= (res
== isl_tab_row_unknown
) ? isl_tab_row_neg
3884 if (res
== isl_tab_row_neg
) {
3885 isl_seq_neg(ineq
->el
, ineq
->el
, ineq
->size
);
3886 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3890 if (res
== isl_tab_row_neg
) {
3891 /* test for positive values */
3893 if (!critical
&& !strict
)
3894 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3896 feasible
= sol
->context
->op
->test_ineq(sol
->context
, ineq
->el
);
3900 res
= isl_tab_row_any
;
3907 return isl_tab_row_unknown
;
3910 static void find_solutions(struct isl_sol
*sol
, struct isl_tab
*tab
);
3912 /* Find solutions for values of the parameters that satisfy the given
3915 * We currently take a snapshot of the context tableau that is reset
3916 * when we return from this function, while we make a copy of the main
3917 * tableau, leaving the original main tableau untouched.
3918 * These are fairly arbitrary choices. Making a copy also of the context
3919 * tableau would obviate the need to undo any changes made to it later,
3920 * while taking a snapshot of the main tableau could reduce memory usage.
3921 * If we were to switch to taking a snapshot of the main tableau,
3922 * we would have to keep in mind that we need to save the row signs
3923 * and that we need to do this before saving the current basis
3924 * such that the basis has been restore before we restore the row signs.
3926 static void find_in_pos(struct isl_sol
*sol
, struct isl_tab
*tab
, isl_int
*ineq
)
3932 saved
= sol
->context
->op
->save(sol
->context
);
3934 tab
= isl_tab_dup(tab
);
3938 sol
->context
->op
->add_ineq(sol
->context
, ineq
, 0, 1);
3940 find_solutions(sol
, tab
);
3943 sol
->context
->op
->restore(sol
->context
, saved
);
3945 sol
->context
->op
->discard(saved
);
3951 /* Record the absence of solutions for those values of the parameters
3952 * that do not satisfy the given inequality with equality.
3954 static void no_sol_in_strict(struct isl_sol
*sol
,
3955 struct isl_tab
*tab
, struct isl_vec
*ineq
)
3960 if (!sol
->context
|| sol
->error
)
3962 saved
= sol
->context
->op
->save(sol
->context
);
3964 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3966 sol
->context
->op
->add_ineq(sol
->context
, ineq
->el
, 1, 0);
3975 isl_int_add_ui(ineq
->el
[0], ineq
->el
[0], 1);
3977 sol
->context
->op
->restore(sol
->context
, saved
);
3983 /* Reset all row variables that are marked to have a sign that may
3984 * be both positive and negative to have an unknown sign.
3986 static void reset_any_to_unknown(struct isl_tab
*tab
)
3990 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
3991 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
3993 if (tab
->row_sign
[row
] == isl_tab_row_any
)
3994 tab
->row_sign
[row
] = isl_tab_row_unknown
;
3998 /* Compute the lexicographic minimum of the set represented by the main
3999 * tableau "tab" within the context "sol->context_tab".
4000 * On entry the sample value of the main tableau is lexicographically
4001 * less than or equal to this lexicographic minimum.
4002 * Pivots are performed until a feasible point is found, which is then
4003 * necessarily equal to the minimum, or until the tableau is found to
4004 * be infeasible. Some pivots may need to be performed for only some
4005 * feasible values of the context tableau. If so, the context tableau
4006 * is split into a part where the pivot is needed and a part where it is not.
4008 * Whenever we enter the main loop, the main tableau is such that no
4009 * "obvious" pivots need to be performed on it, where "obvious" means
4010 * that the given row can be seen to be negative without looking at
4011 * the context tableau. In particular, for non-parametric problems,
4012 * no pivots need to be performed on the main tableau.
4013 * The caller of find_solutions is responsible for making this property
4014 * hold prior to the first iteration of the loop, while restore_lexmin
4015 * is called before every other iteration.
4017 * Inside the main loop, we first examine the signs of the rows of
4018 * the main tableau within the context of the context tableau.
4019 * If we find a row that is always non-positive for all values of
4020 * the parameters satisfying the context tableau and negative for at
4021 * least one value of the parameters, we perform the appropriate pivot
4022 * and start over. An exception is the case where no pivot can be
4023 * performed on the row. In this case, we require that the sign of
4024 * the row is negative for all values of the parameters (rather than just
4025 * non-positive). This special case is handled inside row_sign, which
4026 * will say that the row can have any sign if it determines that it can
4027 * attain both negative and zero values.
4029 * If we can't find a row that always requires a pivot, but we can find
4030 * one or more rows that require a pivot for some values of the parameters
4031 * (i.e., the row can attain both positive and negative signs), then we split
4032 * the context tableau into two parts, one where we force the sign to be
4033 * non-negative and one where we force is to be negative.
4034 * The non-negative part is handled by a recursive call (through find_in_pos).
4035 * Upon returning from this call, we continue with the negative part and
4036 * perform the required pivot.
4038 * If no such rows can be found, all rows are non-negative and we have
4039 * found a (rational) feasible point. If we only wanted a rational point
4041 * Otherwise, we check if all values of the sample point of the tableau
4042 * are integral for the variables. If so, we have found the minimal
4043 * integral point and we are done.
4044 * If the sample point is not integral, then we need to make a distinction
4045 * based on whether the constant term is non-integral or the coefficients
4046 * of the parameters. Furthermore, in order to decide how to handle
4047 * the non-integrality, we also need to know whether the coefficients
4048 * of the other columns in the tableau are integral. This leads
4049 * to the following table. The first two rows do not correspond
4050 * to a non-integral sample point and are only mentioned for completeness.
4052 * constant parameters other
4055 * int int rat | -> no problem
4057 * rat int int -> fail
4059 * rat int rat -> cut
4062 * rat rat rat | -> parametric cut
4065 * rat rat int | -> split context
4067 * If the parametric constant is completely integral, then there is nothing
4068 * to be done. If the constant term is non-integral, but all the other
4069 * coefficient are integral, then there is nothing that can be done
4070 * and the tableau has no integral solution.
4071 * If, on the other hand, one or more of the other columns have rational
4072 * coefficients, but the parameter coefficients are all integral, then
4073 * we can perform a regular (non-parametric) cut.
4074 * Finally, if there is any parameter coefficient that is non-integral,
4075 * then we need to involve the context tableau. There are two cases here.
4076 * If at least one other column has a rational coefficient, then we
4077 * can perform a parametric cut in the main tableau by adding a new
4078 * integer division in the context tableau.
4079 * If all other columns have integral coefficients, then we need to
4080 * enforce that the rational combination of parameters (c + \sum a_i y_i)/m
4081 * is always integral. We do this by introducing an integer division
4082 * q = floor((c + \sum a_i y_i)/m) and stipulating that its argument should
4083 * always be integral in the context tableau, i.e., m q = c + \sum a_i y_i.
4084 * Since q is expressed in the tableau as
4085 * c + \sum a_i y_i - m q >= 0
4086 * -c - \sum a_i y_i + m q + m - 1 >= 0
4087 * it is sufficient to add the inequality
4088 * -c - \sum a_i y_i + m q >= 0
4089 * In the part of the context where this inequality does not hold, the
4090 * main tableau is marked as being empty.
4092 static void find_solutions(struct isl_sol
*sol
, struct isl_tab
*tab
)
4094 struct isl_context
*context
;
4097 if (!tab
|| sol
->error
)
4100 context
= sol
->context
;
4104 if (context
->op
->is_empty(context
))
4107 for (r
= 0; r
>= 0 && tab
&& !tab
->empty
; r
= restore_lexmin(tab
)) {
4110 enum isl_tab_row_sign sgn
;
4114 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
4115 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
4117 sgn
= row_sign(tab
, sol
, row
);
4120 tab
->row_sign
[row
] = sgn
;
4121 if (sgn
== isl_tab_row_any
)
4123 if (sgn
== isl_tab_row_any
&& split
== -1)
4125 if (sgn
== isl_tab_row_neg
)
4128 if (row
< tab
->n_row
)
4131 struct isl_vec
*ineq
;
4133 split
= context
->op
->best_split(context
, tab
);
4136 ineq
= get_row_parameter_ineq(tab
, split
);
4140 reset_any_to_unknown(tab
);
4141 tab
->row_sign
[split
] = isl_tab_row_pos
;
4143 find_in_pos(sol
, tab
, ineq
->el
);
4144 tab
->row_sign
[split
] = isl_tab_row_neg
;
4145 isl_seq_neg(ineq
->el
, ineq
->el
, ineq
->size
);
4146 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
4148 context
->op
->add_ineq(context
, ineq
->el
, 0, 1);
4156 row
= first_non_integer_row(tab
, &flags
);
4159 if (ISL_FL_ISSET(flags
, I_PAR
)) {
4160 if (ISL_FL_ISSET(flags
, I_VAR
)) {
4161 if (isl_tab_mark_empty(tab
) < 0)
4165 row
= add_cut(tab
, row
);
4166 } else if (ISL_FL_ISSET(flags
, I_VAR
)) {
4167 struct isl_vec
*div
;
4168 struct isl_vec
*ineq
;
4170 div
= get_row_split_div(tab
, row
);
4173 d
= context
->op
->get_div(context
, tab
, div
);
4177 ineq
= ineq_for_div(context
->op
->peek_basic_set(context
), d
);
4181 no_sol_in_strict(sol
, tab
, ineq
);
4182 isl_seq_neg(ineq
->el
, ineq
->el
, ineq
->size
);
4183 context
->op
->add_ineq(context
, ineq
->el
, 1, 1);
4185 if (sol
->error
|| !context
->op
->is_ok(context
))
4187 tab
= set_row_cst_to_div(tab
, row
, d
);
4188 if (context
->op
->is_empty(context
))
4191 row
= add_parametric_cut(tab
, row
, context
);
4206 /* Does "sol" contain a pair of partial solutions that could potentially
4209 * We currently only check that "sol" is not in an error state
4210 * and that there are at least two partial solutions of which the final two
4211 * are defined at the same level.
4213 static int sol_has_mergeable_solutions(struct isl_sol
*sol
)
4219 if (!sol
->partial
->next
)
4221 return sol
->partial
->level
== sol
->partial
->next
->level
;
4224 /* Compute the lexicographic minimum of the set represented by the main
4225 * tableau "tab" within the context "sol->context_tab".
4227 * As a preprocessing step, we first transfer all the purely parametric
4228 * equalities from the main tableau to the context tableau, i.e.,
4229 * parameters that have been pivoted to a row.
4230 * These equalities are ignored by the main algorithm, because the
4231 * corresponding rows may not be marked as being non-negative.
4232 * In parts of the context where the added equality does not hold,
4233 * the main tableau is marked as being empty.
4235 * Before we embark on the actual computation, we save a copy
4236 * of the context. When we return, we check if there are any
4237 * partial solutions that can potentially be merged. If so,
4238 * we perform a rollback to the initial state of the context.
4239 * The merging of partial solutions happens inside calls to
4240 * sol_dec_level that are pushed onto the undo stack of the context.
4241 * If there are no partial solutions that can potentially be merged
4242 * then the rollback is skipped as it would just be wasted effort.
4244 static void find_solutions_main(struct isl_sol
*sol
, struct isl_tab
*tab
)
4254 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
4258 if (!row_is_parameter_var(tab
, row
))
4260 if (tab
->row_var
[row
] < tab
->n_param
)
4261 p
= tab
->row_var
[row
];
4263 p
= tab
->row_var
[row
]
4264 + tab
->n_param
- (tab
->n_var
- tab
->n_div
);
4266 eq
= isl_vec_alloc(tab
->mat
->ctx
, 1+tab
->n_param
+tab
->n_div
);
4269 get_row_parameter_line(tab
, row
, eq
->el
);
4270 isl_int_neg(eq
->el
[1 + p
], tab
->mat
->row
[row
][0]);
4271 eq
= isl_vec_normalize(eq
);
4274 no_sol_in_strict(sol
, tab
, eq
);
4276 isl_seq_neg(eq
->el
, eq
->el
, eq
->size
);
4278 no_sol_in_strict(sol
, tab
, eq
);
4279 isl_seq_neg(eq
->el
, eq
->el
, eq
->size
);
4281 sol
->context
->op
->add_eq(sol
->context
, eq
->el
, 1, 1);
4285 if (isl_tab_mark_redundant(tab
, row
) < 0)
4288 if (sol
->context
->op
->is_empty(sol
->context
))
4291 row
= tab
->n_redundant
- 1;
4294 saved
= sol
->context
->op
->save(sol
->context
);
4296 find_solutions(sol
, tab
);
4298 if (sol_has_mergeable_solutions(sol
))
4299 sol
->context
->op
->restore(sol
->context
, saved
);
4301 sol
->context
->op
->discard(saved
);
4312 /* Check if integer division "div" of "dom" also occurs in "bmap".
4313 * If so, return its position within the divs.
4314 * Otherwise, return a position beyond the integer divisions.
4316 static int find_context_div(__isl_keep isl_basic_map
*bmap
,
4317 __isl_keep isl_basic_set
*dom
, unsigned div
)
4320 isl_size b_v_div
, d_v_div
;
4323 b_v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
4324 d_v_div
= isl_basic_set_var_offset(dom
, isl_dim_div
);
4325 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4326 if (b_v_div
< 0 || d_v_div
< 0 || n_div
< 0)
4329 if (isl_int_is_zero(dom
->div
[div
][0]))
4331 if (isl_seq_first_non_zero(dom
->div
[div
] + 2 + d_v_div
,
4335 for (i
= 0; i
< n_div
; ++i
) {
4336 if (isl_int_is_zero(bmap
->div
[i
][0]))
4338 if (isl_seq_first_non_zero(bmap
->div
[i
] + 2 + d_v_div
,
4339 (b_v_div
- d_v_div
) + n_div
) != -1)
4341 if (isl_seq_eq(bmap
->div
[i
], dom
->div
[div
], 2 + d_v_div
))
4347 /* The correspondence between the variables in the main tableau,
4348 * the context tableau, and the input map and domain is as follows.
4349 * The first n_param and the last n_div variables of the main tableau
4350 * form the variables of the context tableau.
4351 * In the basic map, these n_param variables correspond to the
4352 * parameters and the input dimensions. In the domain, they correspond
4353 * to the parameters and the set dimensions.
4354 * The n_div variables correspond to the integer divisions in the domain.
4355 * To ensure that everything lines up, we may need to copy some of the
4356 * integer divisions of the domain to the map. These have to be placed
4357 * in the same order as those in the context and they have to be placed
4358 * after any other integer divisions that the map may have.
4359 * This function performs the required reordering.
4361 static __isl_give isl_basic_map
*align_context_divs(
4362 __isl_take isl_basic_map
*bmap
, __isl_keep isl_basic_set
*dom
)
4367 unsigned bmap_n_div
;
4369 bmap_n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4371 for (i
= 0; i
< dom
->n_div
; ++i
) {
4374 pos
= find_context_div(bmap
, dom
, i
);
4376 return isl_basic_map_free(bmap
);
4377 if (pos
< bmap_n_div
)
4380 other
= bmap_n_div
- common
;
4381 if (dom
->n_div
- common
> 0) {
4382 bmap
= isl_basic_map_cow(bmap
);
4383 bmap
= isl_basic_map_extend(bmap
, dom
->n_div
- common
, 0, 0);
4387 for (i
= 0; i
< dom
->n_div
; ++i
) {
4388 int pos
= find_context_div(bmap
, dom
, i
);
4390 bmap
= isl_basic_map_free(bmap
);
4391 if (pos
>= bmap_n_div
) {
4392 pos
= isl_basic_map_alloc_div(bmap
);
4395 isl_int_set_si(bmap
->div
[pos
][0], 0);
4398 if (pos
!= other
+ i
)
4399 bmap
= isl_basic_map_swap_div(bmap
, pos
, other
+ i
);
4403 isl_basic_map_free(bmap
);
4407 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
4408 * some obvious symmetries.
4410 * We make sure the divs in the domain are properly ordered,
4411 * because they will be added one by one in the given order
4412 * during the construction of the solution map.
4413 * Furthermore, make sure that the known integer divisions
4414 * appear before any unknown integer division because the solution
4415 * may depend on the known integer divisions, while anything that
4416 * depends on any variable starting from the first unknown integer
4417 * division is ignored in sol_pma_add.
4419 static struct isl_sol
*basic_map_partial_lexopt_base_sol(
4420 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
4421 __isl_give isl_set
**empty
, int max
,
4422 struct isl_sol
*(*init
)(__isl_keep isl_basic_map
*bmap
,
4423 __isl_take isl_basic_set
*dom
, int track_empty
, int max
))
4425 struct isl_tab
*tab
;
4426 struct isl_sol
*sol
= NULL
;
4427 struct isl_context
*context
;
4430 dom
= isl_basic_set_sort_divs(dom
);
4431 bmap
= align_context_divs(bmap
, dom
);
4433 sol
= init(bmap
, dom
, !!empty
, max
);
4437 context
= sol
->context
;
4438 if (isl_basic_set_plain_is_empty(context
->op
->peek_basic_set(context
)))
4440 else if (isl_basic_map_plain_is_empty(bmap
)) {
4443 isl_basic_set_copy(context
->op
->peek_basic_set(context
)));
4445 tab
= tab_for_lexmin(bmap
,
4446 context
->op
->peek_basic_set(context
), 1, max
);
4447 tab
= context
->op
->detect_nonnegative_parameters(context
, tab
);
4448 find_solutions_main(sol
, tab
);
4453 isl_basic_map_free(bmap
);
4457 isl_basic_map_free(bmap
);
4461 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
4462 * some obvious symmetries.
4464 * We call basic_map_partial_lexopt_base_sol and extract the results.
4466 static __isl_give isl_map
*basic_map_partial_lexopt_base(
4467 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
4468 __isl_give isl_set
**empty
, int max
)
4470 isl_map
*result
= NULL
;
4471 struct isl_sol
*sol
;
4472 struct isl_sol_map
*sol_map
;
4474 sol
= basic_map_partial_lexopt_base_sol(bmap
, dom
, empty
, max
,
4478 sol_map
= (struct isl_sol_map
*) sol
;
4480 result
= isl_map_copy(sol_map
->map
);
4482 *empty
= isl_set_copy(sol_map
->empty
);
4483 sol_free(&sol_map
->sol
);
4487 /* Return a count of the number of occurrences of the "n" first
4488 * variables in the inequality constraints of "bmap".
4490 static __isl_give
int *count_occurrences(__isl_keep isl_basic_map
*bmap
,
4499 ctx
= isl_basic_map_get_ctx(bmap
);
4500 occurrences
= isl_calloc_array(ctx
, int, n
);
4504 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
4505 for (j
= 0; j
< n
; ++j
) {
4506 if (!isl_int_is_zero(bmap
->ineq
[i
][1 + j
]))
4514 /* Do all of the "n" variables with non-zero coefficients in "c"
4515 * occur in exactly a single constraint.
4516 * "occurrences" is an array of length "n" containing the number
4517 * of occurrences of each of the variables in the inequality constraints.
4519 static int single_occurrence(int n
, isl_int
*c
, int *occurrences
)
4523 for (i
= 0; i
< n
; ++i
) {
4524 if (isl_int_is_zero(c
[i
]))
4526 if (occurrences
[i
] != 1)
4533 /* Do all of the "n" initial variables that occur in inequality constraint
4534 * "ineq" of "bmap" only occur in that constraint?
4536 static int all_single_occurrence(__isl_keep isl_basic_map
*bmap
, int ineq
,
4541 for (i
= 0; i
< n
; ++i
) {
4542 if (isl_int_is_zero(bmap
->ineq
[ineq
][1 + i
]))
4544 for (j
= 0; j
< bmap
->n_ineq
; ++j
) {
4547 if (!isl_int_is_zero(bmap
->ineq
[j
][1 + i
]))
4555 /* Structure used during detection of parallel constraints.
4556 * n_in: number of "input" variables: isl_dim_param + isl_dim_in
4557 * n_out: number of "output" variables: isl_dim_out + isl_dim_div
4558 * val: the coefficients of the output variables
4560 struct isl_constraint_equal_info
{
4566 /* Check whether the coefficients of the output variables
4567 * of the constraint in "entry" are equal to info->val.
4569 static isl_bool
constraint_equal(const void *entry
, const void *val
)
4571 isl_int
**row
= (isl_int
**)entry
;
4572 const struct isl_constraint_equal_info
*info
= val
;
4575 eq
= isl_seq_eq((*row
) + 1 + info
->n_in
, info
->val
, info
->n_out
);
4576 return isl_bool_ok(eq
);
4579 /* Check whether "bmap" has a pair of constraints that have
4580 * the same coefficients for the output variables.
4581 * Note that the coefficients of the existentially quantified
4582 * variables need to be zero since the existentially quantified
4583 * of the result are usually not the same as those of the input.
4584 * Furthermore, check that each of the input variables that occur
4585 * in those constraints does not occur in any other constraint.
4586 * If so, return true and return the row indices of the two constraints
4587 * in *first and *second.
4589 static isl_bool
parallel_constraints(__isl_keep isl_basic_map
*bmap
,
4590 int *first
, int *second
)
4594 int *occurrences
= NULL
;
4595 struct isl_hash_table
*table
= NULL
;
4596 struct isl_hash_table_entry
*entry
;
4597 struct isl_constraint_equal_info info
;
4598 isl_size nparam
, n_in
, n_out
, n_div
;
4600 ctx
= isl_basic_map_get_ctx(bmap
);
4601 table
= isl_hash_table_alloc(ctx
, bmap
->n_ineq
);
4605 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4606 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4607 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4608 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4609 if (nparam
< 0 || n_in
< 0 || n_out
< 0 || n_div
< 0)
4611 info
.n_in
= nparam
+ n_in
;
4612 occurrences
= count_occurrences(bmap
, info
.n_in
);
4613 if (info
.n_in
&& !occurrences
)
4615 info
.n_out
= n_out
+ n_div
;
4616 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
4619 info
.val
= bmap
->ineq
[i
] + 1 + info
.n_in
;
4620 if (isl_seq_first_non_zero(info
.val
, n_out
) < 0)
4622 if (isl_seq_first_non_zero(info
.val
+ n_out
, n_div
) >= 0)
4624 if (!single_occurrence(info
.n_in
, bmap
->ineq
[i
] + 1,
4627 hash
= isl_seq_get_hash(info
.val
, info
.n_out
);
4628 entry
= isl_hash_table_find(ctx
, table
, hash
,
4629 constraint_equal
, &info
, 1);
4634 entry
->data
= &bmap
->ineq
[i
];
4637 if (i
< bmap
->n_ineq
) {
4638 *first
= ((isl_int
**)entry
->data
) - bmap
->ineq
;
4642 isl_hash_table_free(ctx
, table
);
4645 return isl_bool_ok(i
< bmap
->n_ineq
);
4647 isl_hash_table_free(ctx
, table
);
4649 return isl_bool_error
;
4652 /* Given a set of upper bounds in "var", add constraints to "bset"
4653 * that make the i-th bound smallest.
4655 * In particular, if there are n bounds b_i, then add the constraints
4657 * b_i <= b_j for j > i
4658 * b_i < b_j for j < i
4660 static __isl_give isl_basic_set
*select_minimum(__isl_take isl_basic_set
*bset
,
4661 __isl_keep isl_mat
*var
, int i
)
4666 ctx
= isl_mat_get_ctx(var
);
4668 for (j
= 0; j
< var
->n_row
; ++j
) {
4671 k
= isl_basic_set_alloc_inequality(bset
);
4674 isl_seq_combine(bset
->ineq
[k
], ctx
->one
, var
->row
[j
],
4675 ctx
->negone
, var
->row
[i
], var
->n_col
);
4676 isl_int_set_si(bset
->ineq
[k
][var
->n_col
], 0);
4678 isl_int_sub_ui(bset
->ineq
[k
][0], bset
->ineq
[k
][0], 1);
4681 bset
= isl_basic_set_finalize(bset
);
4685 isl_basic_set_free(bset
);
4689 /* Given a set of upper bounds on the last "input" variable m,
4690 * construct a set that assigns the minimal upper bound to m, i.e.,
4691 * construct a set that divides the space into cells where one
4692 * of the upper bounds is smaller than all the others and assign
4693 * this upper bound to m.
4695 * In particular, if there are n bounds b_i, then the result
4696 * consists of n basic sets, each one of the form
4699 * b_i <= b_j for j > i
4700 * b_i < b_j for j < i
4702 static __isl_give isl_set
*set_minimum(__isl_take isl_space
*space
,
4703 __isl_take isl_mat
*var
)
4706 isl_basic_set
*bset
= NULL
;
4707 isl_set
*set
= NULL
;
4712 set
= isl_set_alloc_space(isl_space_copy(space
),
4713 var
->n_row
, ISL_SET_DISJOINT
);
4715 for (i
= 0; i
< var
->n_row
; ++i
) {
4716 bset
= isl_basic_set_alloc_space(isl_space_copy(space
), 0,
4718 k
= isl_basic_set_alloc_equality(bset
);
4721 isl_seq_cpy(bset
->eq
[k
], var
->row
[i
], var
->n_col
);
4722 isl_int_set_si(bset
->eq
[k
][var
->n_col
], -1);
4723 bset
= select_minimum(bset
, var
, i
);
4724 set
= isl_set_add_basic_set(set
, bset
);
4727 isl_space_free(space
);
4731 isl_basic_set_free(bset
);
4733 isl_space_free(space
);
4738 /* Given that the last input variable of "bmap" represents the minimum
4739 * of the bounds in "cst", check whether we need to split the domain
4740 * based on which bound attains the minimum.
4742 * A split is needed when the minimum appears in an integer division
4743 * or in an equality. Otherwise, it is only needed if it appears in
4744 * an upper bound that is different from the upper bounds on which it
4747 static isl_bool
need_split_basic_map(__isl_keep isl_basic_map
*bmap
,
4748 __isl_keep isl_mat
*cst
)
4754 pos
= cst
->n_col
- 1;
4755 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4757 return isl_bool_error
;
4759 for (i
= 0; i
< bmap
->n_div
; ++i
)
4760 if (!isl_int_is_zero(bmap
->div
[i
][2 + pos
]))
4761 return isl_bool_true
;
4763 for (i
= 0; i
< bmap
->n_eq
; ++i
)
4764 if (!isl_int_is_zero(bmap
->eq
[i
][1 + pos
]))
4765 return isl_bool_true
;
4767 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
4768 if (isl_int_is_nonneg(bmap
->ineq
[i
][1 + pos
]))
4770 if (!isl_int_is_negone(bmap
->ineq
[i
][1 + pos
]))
4771 return isl_bool_true
;
4772 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + pos
+ 1,
4773 total
- pos
- 1) >= 0)
4774 return isl_bool_true
;
4776 for (j
= 0; j
< cst
->n_row
; ++j
)
4777 if (isl_seq_eq(bmap
->ineq
[i
], cst
->row
[j
], cst
->n_col
))
4779 if (j
>= cst
->n_row
)
4780 return isl_bool_true
;
4783 return isl_bool_false
;
4786 /* Given that the last set variable of "bset" represents the minimum
4787 * of the bounds in "cst", check whether we need to split the domain
4788 * based on which bound attains the minimum.
4790 * We simply call need_split_basic_map here. This is safe because
4791 * the position of the minimum is computed from "cst" and not
4794 static isl_bool
need_split_basic_set(__isl_keep isl_basic_set
*bset
,
4795 __isl_keep isl_mat
*cst
)
4797 return need_split_basic_map(bset_to_bmap(bset
), cst
);
4800 /* Given that the last set variable of "set" represents the minimum
4801 * of the bounds in "cst", check whether we need to split the domain
4802 * based on which bound attains the minimum.
4804 static isl_bool
need_split_set(__isl_keep isl_set
*set
, __isl_keep isl_mat
*cst
)
4808 for (i
= 0; i
< set
->n
; ++i
) {
4811 split
= need_split_basic_set(set
->p
[i
], cst
);
4812 if (split
< 0 || split
)
4816 return isl_bool_false
;
4819 /* Given a map of which the last input variable is the minimum
4820 * of the bounds in "cst", split each basic set in the set
4821 * in pieces where one of the bounds is (strictly) smaller than the others.
4822 * This subdivision is given in "min_expr".
4823 * The variable is subsequently projected out.
4825 * We only do the split when it is needed.
4826 * For example if the last input variable m = min(a,b) and the only
4827 * constraints in the given basic set are lower bounds on m,
4828 * i.e., l <= m = min(a,b), then we can simply project out m
4829 * to obtain l <= a and l <= b, without having to split on whether
4830 * m is equal to a or b.
4832 static __isl_give isl_map
*split_domain(__isl_take isl_map
*opt
,
4833 __isl_take isl_set
*min_expr
, __isl_take isl_mat
*cst
)
4840 n_in
= isl_map_dim(opt
, isl_dim_in
);
4841 if (n_in
< 0 || !min_expr
|| !cst
)
4844 space
= isl_map_get_space(opt
);
4845 space
= isl_space_drop_dims(space
, isl_dim_in
, n_in
- 1, 1);
4846 res
= isl_map_empty(space
);
4848 for (i
= 0; i
< opt
->n
; ++i
) {
4852 map
= isl_map_from_basic_map(isl_basic_map_copy(opt
->p
[i
]));
4853 split
= need_split_basic_map(opt
->p
[i
], cst
);
4855 map
= isl_map_free(map
);
4857 map
= isl_map_intersect_domain(map
,
4858 isl_set_copy(min_expr
));
4859 map
= isl_map_remove_dims(map
, isl_dim_in
, n_in
- 1, 1);
4861 res
= isl_map_union_disjoint(res
, map
);
4865 isl_set_free(min_expr
);
4870 isl_set_free(min_expr
);
4875 /* Given a set of which the last set variable is the minimum
4876 * of the bounds in "cst", split each basic set in the set
4877 * in pieces where one of the bounds is (strictly) smaller than the others.
4878 * This subdivision is given in "min_expr".
4879 * The variable is subsequently projected out.
4881 static __isl_give isl_set
*split(__isl_take isl_set
*empty
,
4882 __isl_take isl_set
*min_expr
, __isl_take isl_mat
*cst
)
4886 map
= isl_map_from_domain(empty
);
4887 map
= split_domain(map
, min_expr
, cst
);
4888 empty
= isl_map_domain(map
);
4893 static __isl_give isl_map
*basic_map_partial_lexopt(
4894 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
4895 __isl_give isl_set
**empty
, int max
);
4897 /* This function is called from basic_map_partial_lexopt_symm.
4898 * The last variable of "bmap" and "dom" corresponds to the minimum
4899 * of the bounds in "cst". "map_space" is the space of the original
4900 * input relation (of basic_map_partial_lexopt_symm) and "set_space"
4901 * is the space of the original domain.
4903 * We recursively call basic_map_partial_lexopt and then plug in
4904 * the definition of the minimum in the result.
4906 static __isl_give isl_map
*basic_map_partial_lexopt_symm_core(
4907 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
4908 __isl_give isl_set
**empty
, int max
, __isl_take isl_mat
*cst
,
4909 __isl_take isl_space
*map_space
, __isl_take isl_space
*set_space
)
4914 min_expr
= set_minimum(isl_basic_set_get_space(dom
), isl_mat_copy(cst
));
4916 opt
= basic_map_partial_lexopt(bmap
, dom
, empty
, max
);
4919 *empty
= split(*empty
,
4920 isl_set_copy(min_expr
), isl_mat_copy(cst
));
4921 *empty
= isl_set_reset_space(*empty
, set_space
);
4924 opt
= split_domain(opt
, min_expr
, cst
);
4925 opt
= isl_map_reset_space(opt
, map_space
);
4930 /* Extract a domain from "bmap" for the purpose of computing
4931 * a lexicographic optimum.
4933 * This function is only called when the caller wants to compute a full
4934 * lexicographic optimum, i.e., without specifying a domain. In this case,
4935 * the caller is not interested in the part of the domain space where
4936 * there is no solution and the domain can be initialized to those constraints
4937 * of "bmap" that only involve the parameters and the input dimensions.
4938 * This relieves the parametric programming engine from detecting those
4939 * inequalities and transferring them to the context. More importantly,
4940 * it ensures that those inequalities are transferred first and not
4941 * intermixed with inequalities that actually split the domain.
4943 * If the caller does not require the absence of existentially quantified
4944 * variables in the result (i.e., if ISL_OPT_QE is not set in "flags"),
4945 * then the actual domain of "bmap" can be used. This ensures that
4946 * the domain does not need to be split at all just to separate out
4947 * pieces of the domain that do not have a solution from piece that do.
4948 * This domain cannot be used in general because it may involve
4949 * (unknown) existentially quantified variables which will then also
4950 * appear in the solution.
4952 static __isl_give isl_basic_set
*extract_domain(__isl_keep isl_basic_map
*bmap
,
4958 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4959 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4960 if (n_div
< 0 || n_out
< 0)
4962 bmap
= isl_basic_map_copy(bmap
);
4963 if (ISL_FL_ISSET(flags
, ISL_OPT_QE
)) {
4964 bmap
= isl_basic_map_drop_constraints_involving_dims(bmap
,
4965 isl_dim_div
, 0, n_div
);
4966 bmap
= isl_basic_map_drop_constraints_involving_dims(bmap
,
4967 isl_dim_out
, 0, n_out
);
4969 return isl_basic_map_domain(bmap
);
4973 #define TYPE isl_map
4976 #include "isl_tab_lexopt_templ.c"
4978 /* Extract the subsequence of the sample value of "tab"
4979 * starting at "pos" and of length "len".
4981 static __isl_give isl_vec
*extract_sample_sequence(struct isl_tab
*tab
,
4988 ctx
= isl_tab_get_ctx(tab
);
4989 v
= isl_vec_alloc(ctx
, len
);
4992 for (i
= 0; i
< len
; ++i
) {
4993 if (!tab
->var
[pos
+ i
].is_row
) {
4994 isl_int_set_si(v
->el
[i
], 0);
4998 row
= tab
->var
[pos
+ i
].index
;
4999 isl_int_divexact(v
->el
[i
], tab
->mat
->row
[row
][1],
5000 tab
->mat
->row
[row
][0]);
5007 /* Check if the sequence of variables starting at "pos"
5008 * represents a trivial solution according to "trivial".
5009 * That is, is the result of applying "trivial" to this sequence
5010 * equal to the zero vector?
5012 static isl_bool
region_is_trivial(struct isl_tab
*tab
, int pos
,
5013 __isl_keep isl_mat
*trivial
)
5017 isl_bool is_trivial
;
5019 n
= isl_mat_rows(trivial
);
5021 return isl_bool_error
;
5024 return isl_bool_false
;
5026 len
= isl_mat_cols(trivial
);
5028 return isl_bool_error
;
5029 v
= extract_sample_sequence(tab
, pos
, len
);
5030 v
= isl_mat_vec_product(isl_mat_copy(trivial
), v
);
5031 is_trivial
= isl_vec_is_zero(v
);
5037 /* Global internal data for isl_tab_basic_set_non_trivial_lexmin.
5039 * "n_op" is the number of initial coordinates to optimize,
5040 * as passed to isl_tab_basic_set_non_trivial_lexmin.
5041 * "region" is the "n_region"-sized array of regions passed
5042 * to isl_tab_basic_set_non_trivial_lexmin.
5044 * "tab" is the tableau that corresponds to the ILP problem.
5045 * "local" is an array of local data structure, one for each
5046 * (potential) level of the backtracking procedure of
5047 * isl_tab_basic_set_non_trivial_lexmin.
5048 * "v" is a pre-allocated vector that can be used for adding
5049 * constraints to the tableau.
5051 * "sol" contains the best solution found so far.
5052 * It is initialized to a vector of size zero.
5054 struct isl_lexmin_data
{
5057 struct isl_trivial_region
*region
;
5059 struct isl_tab
*tab
;
5060 struct isl_local_region
*local
;
5066 /* Return the index of the first trivial region, "n_region" if all regions
5067 * are non-trivial or -1 in case of error.
5069 static int first_trivial_region(struct isl_lexmin_data
*data
)
5073 for (i
= 0; i
< data
->n_region
; ++i
) {
5075 trivial
= region_is_trivial(data
->tab
, data
->region
[i
].pos
,
5076 data
->region
[i
].trivial
);
5083 return data
->n_region
;
5086 /* Check if the solution is optimal, i.e., whether the first
5087 * n_op entries are zero.
5089 static int is_optimal(__isl_keep isl_vec
*sol
, int n_op
)
5093 for (i
= 0; i
< n_op
; ++i
)
5094 if (!isl_int_is_zero(sol
->el
[1 + i
]))
5099 /* Add constraints to "tab" that ensure that any solution is significantly
5100 * better than that represented by "sol". That is, find the first
5101 * relevant (within first n_op) non-zero coefficient and force it (along
5102 * with all previous coefficients) to be zero.
5103 * If the solution is already optimal (all relevant coefficients are zero),
5104 * then just mark the table as empty.
5105 * "n_zero" is the number of coefficients that have been forced zero
5106 * by previous calls to this function at the same level.
5107 * Return the updated number of forced zero coefficients or -1 on error.
5109 * This function assumes that at least 2 * (n_op - n_zero) more rows and
5110 * at least 2 * (n_op - n_zero) more elements in the constraint array
5111 * are available in the tableau.
5113 static int force_better_solution(struct isl_tab
*tab
,
5114 __isl_keep isl_vec
*sol
, int n_op
, int n_zero
)
5123 for (i
= n_zero
; i
< n_op
; ++i
)
5124 if (!isl_int_is_zero(sol
->el
[1 + i
]))
5128 if (isl_tab_mark_empty(tab
) < 0)
5133 ctx
= isl_vec_get_ctx(sol
);
5134 v
= isl_vec_alloc(ctx
, 1 + tab
->n_var
);
5139 for (; i
>= n_zero
; --i
) {
5141 isl_int_set_si(v
->el
[1 + i
], -1);
5142 if (add_lexmin_eq(tab
, v
->el
) < 0)
5153 /* Fix triviality direction "dir" of the given region to zero.
5155 * This function assumes that at least two more rows and at least
5156 * two more elements in the constraint array are available in the tableau.
5158 static isl_stat
fix_zero(struct isl_tab
*tab
, struct isl_trivial_region
*region
,
5159 int dir
, struct isl_lexmin_data
*data
)
5163 data
->v
= isl_vec_clr(data
->v
);
5165 return isl_stat_error
;
5166 len
= isl_mat_cols(region
->trivial
);
5168 return isl_stat_error
;
5169 isl_seq_cpy(data
->v
->el
+ 1 + region
->pos
, region
->trivial
->row
[dir
],
5171 if (add_lexmin_eq(tab
, data
->v
->el
) < 0)
5172 return isl_stat_error
;
5177 /* This function selects case "side" for non-triviality region "region",
5178 * assuming all the equality constraints have been imposed already.
5179 * In particular, the triviality direction side/2 is made positive
5180 * if side is even and made negative if side is odd.
5182 * This function assumes that at least one more row and at least
5183 * one more element in the constraint array are available in the tableau.
5185 static struct isl_tab
*pos_neg(struct isl_tab
*tab
,
5186 struct isl_trivial_region
*region
,
5187 int side
, struct isl_lexmin_data
*data
)
5191 data
->v
= isl_vec_clr(data
->v
);
5194 isl_int_set_si(data
->v
->el
[0], -1);
5195 len
= isl_mat_cols(region
->trivial
);
5199 isl_seq_cpy(data
->v
->el
+ 1 + region
->pos
,
5200 region
->trivial
->row
[side
/ 2], len
);
5202 isl_seq_neg(data
->v
->el
+ 1 + region
->pos
,
5203 region
->trivial
->row
[side
/ 2], len
);
5204 return add_lexmin_ineq(tab
, data
->v
->el
);
5210 /* Local data at each level of the backtracking procedure of
5211 * isl_tab_basic_set_non_trivial_lexmin.
5213 * "update" is set if a solution has been found in the current case
5214 * of this level, such that a better solution needs to be enforced
5216 * "n_zero" is the number of initial coordinates that have already
5217 * been forced to be zero at this level.
5218 * "region" is the non-triviality region considered at this level.
5219 * "side" is the index of the current case at this level.
5220 * "n" is the number of triviality directions.
5221 * "snap" is a snapshot of the tableau holding a state that needs
5222 * to be satisfied by all subsequent cases.
5224 struct isl_local_region
{
5230 struct isl_tab_undo
*snap
;
5233 /* Initialize the global data structure "data" used while solving
5234 * the ILP problem "bset".
5236 static isl_stat
init_lexmin_data(struct isl_lexmin_data
*data
,
5237 __isl_keep isl_basic_set
*bset
)
5241 ctx
= isl_basic_set_get_ctx(bset
);
5243 data
->tab
= tab_for_lexmin(bset
, NULL
, 0, 0);
5245 return isl_stat_error
;
5247 data
->v
= isl_vec_alloc(ctx
, 1 + data
->tab
->n_var
);
5249 return isl_stat_error
;
5250 data
->local
= isl_calloc_array(ctx
, struct isl_local_region
,
5252 if (data
->n_region
&& !data
->local
)
5253 return isl_stat_error
;
5255 data
->sol
= isl_vec_alloc(ctx
, 0);
5260 /* Mark all outer levels as requiring a better solution
5261 * in the next cases.
5263 static void update_outer_levels(struct isl_lexmin_data
*data
, int level
)
5267 for (i
= 0; i
< level
; ++i
)
5268 data
->local
[i
].update
= 1;
5271 /* Initialize "local" to refer to region "region" and
5272 * to initiate processing at this level.
5274 static isl_stat
init_local_region(struct isl_local_region
*local
, int region
,
5275 struct isl_lexmin_data
*data
)
5277 isl_size n
= isl_mat_rows(data
->region
[region
].trivial
);
5280 return isl_stat_error
;
5282 local
->region
= region
;
5290 /* What to do next after entering a level of the backtracking procedure.
5292 * error: some error has occurred; abort
5293 * done: an optimal solution has been found; stop search
5294 * backtrack: backtrack to the previous level
5295 * handle: add the constraints for the current level and
5296 * move to the next level
5299 isl_next_error
= -1,
5305 /* Have all cases of the current region been considered?
5306 * If there are n directions, then there are 2n cases.
5308 * The constraints in the current tableau are imposed
5309 * in all subsequent cases. This means that if the current
5310 * tableau is empty, then none of those cases should be considered
5311 * anymore and all cases have effectively been considered.
5313 static int finished_all_cases(struct isl_local_region
*local
,
5314 struct isl_lexmin_data
*data
)
5316 if (data
->tab
->empty
)
5318 return local
->side
>= 2 * local
->n
;
5321 /* Enter level "level" of the backtracking search and figure out
5322 * what to do next. "init" is set if the level was entered
5323 * from a higher level and needs to be initialized.
5324 * Otherwise, the level is entered as a result of backtracking and
5325 * the tableau needs to be restored to a position that can
5326 * be used for the next case at this level.
5327 * The snapshot is assumed to have been saved in the previous case,
5328 * before the constraints specific to that case were added.
5330 * In the initialization case, the local region is initialized
5331 * to point to the first violated region.
5332 * If the constraints of all regions are satisfied by the current
5333 * sample of the tableau, then tell the caller to continue looking
5334 * for a better solution or to stop searching if an optimal solution
5337 * If the tableau is empty or if all cases at the current level
5338 * have been considered, then the caller needs to backtrack as well.
5340 static enum isl_next
enter_level(int level
, int init
,
5341 struct isl_lexmin_data
*data
)
5343 struct isl_local_region
*local
= &data
->local
[level
];
5348 data
->tab
= cut_to_integer_lexmin(data
->tab
, CUT_ONE
);
5350 return isl_next_error
;
5351 if (data
->tab
->empty
)
5352 return isl_next_backtrack
;
5353 r
= first_trivial_region(data
);
5355 return isl_next_error
;
5356 if (r
== data
->n_region
) {
5357 update_outer_levels(data
, level
);
5358 isl_vec_free(data
->sol
);
5359 data
->sol
= isl_tab_get_sample_value(data
->tab
);
5361 return isl_next_error
;
5362 if (is_optimal(data
->sol
, data
->n_op
))
5363 return isl_next_done
;
5364 return isl_next_backtrack
;
5366 if (level
>= data
->n_region
)
5367 isl_die(isl_vec_get_ctx(data
->v
), isl_error_internal
,
5368 "nesting level too deep",
5369 return isl_next_error
);
5370 if (init_local_region(local
, r
, data
) < 0)
5371 return isl_next_error
;
5372 if (isl_tab_extend_cons(data
->tab
,
5373 2 * local
->n
+ 2 * data
->n_op
) < 0)
5374 return isl_next_error
;
5376 if (isl_tab_rollback(data
->tab
, local
->snap
) < 0)
5377 return isl_next_error
;
5380 if (finished_all_cases(local
, data
))
5381 return isl_next_backtrack
;
5382 return isl_next_handle
;
5385 /* If a solution has been found in the previous case at this level
5386 * (marked by local->update being set), then add constraints
5387 * that enforce a better solution in the present and all following cases.
5388 * The constraints only need to be imposed once because they are
5389 * included in the snapshot (taken in pick_side) that will be used in
5392 static isl_stat
better_next_side(struct isl_local_region
*local
,
5393 struct isl_lexmin_data
*data
)
5398 local
->n_zero
= force_better_solution(data
->tab
,
5399 data
->sol
, data
->n_op
, local
->n_zero
);
5400 if (local
->n_zero
< 0)
5401 return isl_stat_error
;
5408 /* Add constraints to data->tab that select the current case (local->side)
5409 * at the current level.
5411 * If the linear combinations v should not be zero, then the cases are
5414 * v_0 = 0 and v_1 >= 1
5415 * v_0 = 0 and v_1 <= -1
5416 * v_0 = 0 and v_1 = 0 and v_2 >= 1
5417 * v_0 = 0 and v_1 = 0 and v_2 <= -1
5421 * A snapshot is taken after the equality constraint (if any) has been added
5422 * such that the next case can start off from this position.
5423 * The rollback to this position is performed in enter_level.
5425 static isl_stat
pick_side(struct isl_local_region
*local
,
5426 struct isl_lexmin_data
*data
)
5428 struct isl_trivial_region
*region
;
5431 region
= &data
->region
[local
->region
];
5433 base
= 2 * (side
/2);
5435 if (side
== base
&& base
>= 2 &&
5436 fix_zero(data
->tab
, region
, base
/ 2 - 1, data
) < 0)
5437 return isl_stat_error
;
5439 local
->snap
= isl_tab_snap(data
->tab
);
5440 if (isl_tab_push_basis(data
->tab
) < 0)
5441 return isl_stat_error
;
5443 data
->tab
= pos_neg(data
->tab
, region
, side
, data
);
5445 return isl_stat_error
;
5449 /* Free the memory associated to "data".
5451 static void clear_lexmin_data(struct isl_lexmin_data
*data
)
5454 isl_vec_free(data
->v
);
5455 isl_tab_free(data
->tab
);
5458 /* Return the lexicographically smallest non-trivial solution of the
5459 * given ILP problem.
5461 * All variables are assumed to be non-negative.
5463 * n_op is the number of initial coordinates to optimize.
5464 * That is, once a solution has been found, we will only continue looking
5465 * for solutions that result in significantly better values for those
5466 * initial coordinates. That is, we only continue looking for solutions
5467 * that increase the number of initial zeros in this sequence.
5469 * A solution is non-trivial, if it is non-trivial on each of the
5470 * specified regions. Each region represents a sequence of
5471 * triviality directions on a sequence of variables that starts
5472 * at a given position. A solution is non-trivial on such a region if
5473 * at least one of the triviality directions is non-zero
5474 * on that sequence of variables.
5476 * Whenever a conflict is encountered, all constraints involved are
5477 * reported to the caller through a call to "conflict".
5479 * We perform a simple branch-and-bound backtracking search.
5480 * Each level in the search represents an initially trivial region
5481 * that is forced to be non-trivial.
5482 * At each level we consider 2 * n cases, where n
5483 * is the number of triviality directions.
5484 * In terms of those n directions v_i, we consider the cases
5487 * v_0 = 0 and v_1 >= 1
5488 * v_0 = 0 and v_1 <= -1
5489 * v_0 = 0 and v_1 = 0 and v_2 >= 1
5490 * v_0 = 0 and v_1 = 0 and v_2 <= -1
5494 __isl_give isl_vec
*isl_tab_basic_set_non_trivial_lexmin(
5495 __isl_take isl_basic_set
*bset
, int n_op
, int n_region
,
5496 struct isl_trivial_region
*region
,
5497 int (*conflict
)(int con
, void *user
), void *user
)
5499 struct isl_lexmin_data data
= { n_op
, n_region
, region
};
5505 if (init_lexmin_data(&data
, bset
) < 0)
5507 data
.tab
->conflict
= conflict
;
5508 data
.tab
->conflict_user
= user
;
5513 while (level
>= 0) {
5515 struct isl_local_region
*local
= &data
.local
[level
];
5517 next
= enter_level(level
, init
, &data
);
5520 if (next
== isl_next_done
)
5522 if (next
== isl_next_backtrack
) {
5528 if (better_next_side(local
, &data
) < 0)
5530 if (pick_side(local
, &data
) < 0)
5538 clear_lexmin_data(&data
);
5539 isl_basic_set_free(bset
);
5543 clear_lexmin_data(&data
);
5544 isl_basic_set_free(bset
);
5545 isl_vec_free(data
.sol
);
5549 /* Wrapper for a tableau that is used for computing
5550 * the lexicographically smallest rational point of a non-negative set.
5551 * This point is represented by the sample value of "tab",
5552 * unless "tab" is empty.
5554 struct isl_tab_lexmin
{
5556 struct isl_tab
*tab
;
5559 /* Free "tl" and return NULL.
5561 __isl_null isl_tab_lexmin
*isl_tab_lexmin_free(__isl_take isl_tab_lexmin
*tl
)
5565 isl_ctx_deref(tl
->ctx
);
5566 isl_tab_free(tl
->tab
);
5572 /* Construct an isl_tab_lexmin for computing
5573 * the lexicographically smallest rational point in "bset",
5574 * assuming that all variables are non-negative.
5576 __isl_give isl_tab_lexmin
*isl_tab_lexmin_from_basic_set(
5577 __isl_take isl_basic_set
*bset
)
5585 ctx
= isl_basic_set_get_ctx(bset
);
5586 tl
= isl_calloc_type(ctx
, struct isl_tab_lexmin
);
5591 tl
->tab
= tab_for_lexmin(bset
, NULL
, 0, 0);
5592 isl_basic_set_free(bset
);
5594 return isl_tab_lexmin_free(tl
);
5597 isl_basic_set_free(bset
);
5598 isl_tab_lexmin_free(tl
);
5602 /* Return the dimension of the set represented by "tl".
5604 int isl_tab_lexmin_dim(__isl_keep isl_tab_lexmin
*tl
)
5606 return tl
? tl
->tab
->n_var
: -1;
5609 /* Add the equality with coefficients "eq" to "tl", updating the optimal
5610 * solution if needed.
5611 * The equality is added as two opposite inequality constraints.
5613 __isl_give isl_tab_lexmin
*isl_tab_lexmin_add_eq(__isl_take isl_tab_lexmin
*tl
,
5619 return isl_tab_lexmin_free(tl
);
5621 if (isl_tab_extend_cons(tl
->tab
, 2) < 0)
5622 return isl_tab_lexmin_free(tl
);
5623 n_var
= tl
->tab
->n_var
;
5624 isl_seq_neg(eq
, eq
, 1 + n_var
);
5625 tl
->tab
= add_lexmin_ineq(tl
->tab
, eq
);
5626 isl_seq_neg(eq
, eq
, 1 + n_var
);
5627 tl
->tab
= add_lexmin_ineq(tl
->tab
, eq
);
5630 return isl_tab_lexmin_free(tl
);
5635 /* Add cuts to "tl" until the sample value reaches an integer value or
5636 * until the result becomes empty.
5638 __isl_give isl_tab_lexmin
*isl_tab_lexmin_cut_to_integer(
5639 __isl_take isl_tab_lexmin
*tl
)
5643 tl
->tab
= cut_to_integer_lexmin(tl
->tab
, CUT_ONE
);
5645 return isl_tab_lexmin_free(tl
);
5649 /* Return the lexicographically smallest rational point in the basic set
5650 * from which "tl" was constructed.
5651 * If the original input was empty, then return a zero-length vector.
5653 __isl_give isl_vec
*isl_tab_lexmin_get_solution(__isl_keep isl_tab_lexmin
*tl
)
5658 return isl_vec_alloc(tl
->ctx
, 0);
5660 return isl_tab_get_sample_value(tl
->tab
);
5663 struct isl_sol_pma
{
5665 isl_pw_multi_aff
*pma
;
5669 static void sol_pma_free(struct isl_sol
*sol
)
5671 struct isl_sol_pma
*sol_pma
= (struct isl_sol_pma
*) sol
;
5672 isl_pw_multi_aff_free(sol_pma
->pma
);
5673 isl_set_free(sol_pma
->empty
);
5676 /* This function is called for parts of the context where there is
5677 * no solution, with "bset" corresponding to the context tableau.
5678 * Simply add the basic set to the set "empty".
5680 static void sol_pma_add_empty(struct isl_sol_pma
*sol
,
5681 __isl_take isl_basic_set
*bset
)
5683 if (!bset
|| !sol
->empty
)
5686 sol
->empty
= isl_set_grow(sol
->empty
, 1);
5687 bset
= isl_basic_set_simplify(bset
);
5688 bset
= isl_basic_set_finalize(bset
);
5689 sol
->empty
= isl_set_add_basic_set(sol
->empty
, bset
);
5694 isl_basic_set_free(bset
);
5698 /* Given a basic set "dom" that represents the context and a tuple of
5699 * affine expressions "maff" defined over this domain, construct
5700 * an isl_pw_multi_aff with a single cell corresponding to "dom" and
5701 * the affine expressions in "maff".
5703 static void sol_pma_add(struct isl_sol_pma
*sol
,
5704 __isl_take isl_basic_set
*dom
, __isl_take isl_multi_aff
*maff
)
5706 isl_pw_multi_aff
*pma
;
5708 dom
= isl_basic_set_simplify(dom
);
5709 dom
= isl_basic_set_finalize(dom
);
5710 pma
= isl_pw_multi_aff_alloc(isl_set_from_basic_set(dom
), maff
);
5711 sol
->pma
= isl_pw_multi_aff_add_disjoint(sol
->pma
, pma
);
5716 static void sol_pma_add_empty_wrap(struct isl_sol
*sol
,
5717 __isl_take isl_basic_set
*bset
)
5719 sol_pma_add_empty((struct isl_sol_pma
*)sol
, bset
);
5722 static void sol_pma_add_wrap(struct isl_sol
*sol
,
5723 __isl_take isl_basic_set
*dom
, __isl_take isl_multi_aff
*ma
)
5725 sol_pma_add((struct isl_sol_pma
*)sol
, dom
, ma
);
5728 /* Construct an isl_sol_pma structure for accumulating the solution.
5729 * If track_empty is set, then we also keep track of the parts
5730 * of the context where there is no solution.
5731 * If max is set, then we are solving a maximization, rather than
5732 * a minimization problem, which means that the variables in the
5733 * tableau have value "M - x" rather than "M + x".
5735 static struct isl_sol
*sol_pma_init(__isl_keep isl_basic_map
*bmap
,
5736 __isl_take isl_basic_set
*dom
, int track_empty
, int max
)
5738 struct isl_sol_pma
*sol_pma
= NULL
;
5744 sol_pma
= isl_calloc_type(bmap
->ctx
, struct isl_sol_pma
);
5748 sol_pma
->sol
.free
= &sol_pma_free
;
5749 if (sol_init(&sol_pma
->sol
, bmap
, dom
, max
) < 0)
5751 sol_pma
->sol
.add
= &sol_pma_add_wrap
;
5752 sol_pma
->sol
.add_empty
= track_empty
? &sol_pma_add_empty_wrap
: NULL
;
5753 space
= isl_space_copy(sol_pma
->sol
.space
);
5754 sol_pma
->pma
= isl_pw_multi_aff_empty(space
);
5759 sol_pma
->empty
= isl_set_alloc_space(isl_basic_set_get_space(dom
),
5760 1, ISL_SET_DISJOINT
);
5761 if (!sol_pma
->empty
)
5765 isl_basic_set_free(dom
);
5766 return &sol_pma
->sol
;
5768 isl_basic_set_free(dom
);
5769 sol_free(&sol_pma
->sol
);
5773 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
5774 * some obvious symmetries.
5776 * We call basic_map_partial_lexopt_base_sol and extract the results.
5778 static __isl_give isl_pw_multi_aff
*basic_map_partial_lexopt_base_pw_multi_aff(
5779 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5780 __isl_give isl_set
**empty
, int max
)
5782 isl_pw_multi_aff
*result
= NULL
;
5783 struct isl_sol
*sol
;
5784 struct isl_sol_pma
*sol_pma
;
5786 sol
= basic_map_partial_lexopt_base_sol(bmap
, dom
, empty
, max
,
5790 sol_pma
= (struct isl_sol_pma
*) sol
;
5792 result
= isl_pw_multi_aff_copy(sol_pma
->pma
);
5794 *empty
= isl_set_copy(sol_pma
->empty
);
5795 sol_free(&sol_pma
->sol
);
5799 /* Given that the last input variable of "maff" represents the minimum
5800 * of some bounds, check whether we need to plug in the expression
5803 * In particular, check if the last input variable appears in any
5804 * of the expressions in "maff".
5806 static isl_bool
need_substitution(__isl_keep isl_multi_aff
*maff
)
5812 n_in
= isl_multi_aff_dim(maff
, isl_dim_in
);
5814 return isl_bool_error
;
5817 for (i
= 0; i
< maff
->n
; ++i
) {
5820 involves
= isl_aff_involves_dims(maff
->u
.p
[i
],
5821 isl_dim_in
, pos
, 1);
5822 if (involves
< 0 || involves
)
5826 return isl_bool_false
;
5829 /* Given a set of upper bounds on the last "input" variable m,
5830 * construct a piecewise affine expression that selects
5831 * the minimal upper bound to m, i.e.,
5832 * divide the space into cells where one
5833 * of the upper bounds is smaller than all the others and select
5834 * this upper bound on that cell.
5836 * In particular, if there are n bounds b_i, then the result
5837 * consists of n cell, each one of the form
5839 * b_i <= b_j for j > i
5840 * b_i < b_j for j < i
5842 * The affine expression on this cell is
5846 static __isl_give isl_pw_aff
*set_minimum_pa(__isl_take isl_space
*space
,
5847 __isl_take isl_mat
*var
)
5850 isl_aff
*aff
= NULL
;
5851 isl_basic_set
*bset
= NULL
;
5852 isl_pw_aff
*paff
= NULL
;
5853 isl_space
*pw_space
;
5854 isl_local_space
*ls
= NULL
;
5859 ls
= isl_local_space_from_space(isl_space_copy(space
));
5860 pw_space
= isl_space_copy(space
);
5861 pw_space
= isl_space_from_domain(pw_space
);
5862 pw_space
= isl_space_add_dims(pw_space
, isl_dim_out
, 1);
5863 paff
= isl_pw_aff_alloc_size(pw_space
, var
->n_row
);
5865 for (i
= 0; i
< var
->n_row
; ++i
) {
5868 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
5869 bset
= isl_basic_set_alloc_space(isl_space_copy(space
), 0,
5873 isl_int_set_si(aff
->v
->el
[0], 1);
5874 isl_seq_cpy(aff
->v
->el
+ 1, var
->row
[i
], var
->n_col
);
5875 isl_int_set_si(aff
->v
->el
[1 + var
->n_col
], 0);
5876 bset
= select_minimum(bset
, var
, i
);
5877 paff_i
= isl_pw_aff_alloc(isl_set_from_basic_set(bset
), aff
);
5878 paff
= isl_pw_aff_add_disjoint(paff
, paff_i
);
5881 isl_local_space_free(ls
);
5882 isl_space_free(space
);
5887 isl_basic_set_free(bset
);
5888 isl_pw_aff_free(paff
);
5889 isl_local_space_free(ls
);
5890 isl_space_free(space
);
5895 /* Given a piecewise multi-affine expression of which the last input variable
5896 * is the minimum of the bounds in "cst", plug in the value of the minimum.
5897 * This minimum expression is given in "min_expr_pa".
5898 * The set "min_expr" contains the same information, but in the form of a set.
5899 * The variable is subsequently projected out.
5901 * The implementation is similar to those of "split" and "split_domain".
5902 * If the variable appears in a given expression, then minimum expression
5903 * is plugged in. Otherwise, if the variable appears in the constraints
5904 * and a split is required, then the domain is split. Otherwise, no split
5907 static __isl_give isl_pw_multi_aff
*split_domain_pma(
5908 __isl_take isl_pw_multi_aff
*opt
, __isl_take isl_pw_aff
*min_expr_pa
,
5909 __isl_take isl_set
*min_expr
, __isl_take isl_mat
*cst
)
5914 isl_pw_multi_aff
*res
;
5916 if (!opt
|| !min_expr
|| !cst
)
5919 n_in
= isl_pw_multi_aff_dim(opt
, isl_dim_in
);
5922 space
= isl_pw_multi_aff_get_space(opt
);
5923 space
= isl_space_drop_dims(space
, isl_dim_in
, n_in
- 1, 1);
5924 res
= isl_pw_multi_aff_empty(space
);
5926 for (i
= 0; i
< opt
->n
; ++i
) {
5928 isl_pw_multi_aff
*pma
;
5930 pma
= isl_pw_multi_aff_alloc(isl_set_copy(opt
->p
[i
].set
),
5931 isl_multi_aff_copy(opt
->p
[i
].maff
));
5932 subs
= need_substitution(opt
->p
[i
].maff
);
5934 pma
= isl_pw_multi_aff_free(pma
);
5936 pma
= isl_pw_multi_aff_substitute(pma
,
5937 n_in
- 1, min_expr_pa
);
5940 split
= need_split_set(opt
->p
[i
].set
, cst
);
5942 pma
= isl_pw_multi_aff_free(pma
);
5944 pma
= isl_pw_multi_aff_intersect_domain(pma
,
5945 isl_set_copy(min_expr
));
5947 pma
= isl_pw_multi_aff_project_out(pma
,
5948 isl_dim_in
, n_in
- 1, 1);
5950 res
= isl_pw_multi_aff_add_disjoint(res
, pma
);
5953 isl_pw_multi_aff_free(opt
);
5954 isl_pw_aff_free(min_expr_pa
);
5955 isl_set_free(min_expr
);
5959 isl_pw_multi_aff_free(opt
);
5960 isl_pw_aff_free(min_expr_pa
);
5961 isl_set_free(min_expr
);
5966 static __isl_give isl_pw_multi_aff
*basic_map_partial_lexopt_pw_multi_aff(
5967 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5968 __isl_give isl_set
**empty
, int max
);
5970 /* This function is called from basic_map_partial_lexopt_symm.
5971 * The last variable of "bmap" and "dom" corresponds to the minimum
5972 * of the bounds in "cst". "map_space" is the space of the original
5973 * input relation (of basic_map_partial_lexopt_symm) and "set_space"
5974 * is the space of the original domain.
5976 * We recursively call basic_map_partial_lexopt and then plug in
5977 * the definition of the minimum in the result.
5979 static __isl_give isl_pw_multi_aff
*
5980 basic_map_partial_lexopt_symm_core_pw_multi_aff(
5981 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5982 __isl_give isl_set
**empty
, int max
, __isl_take isl_mat
*cst
,
5983 __isl_take isl_space
*map_space
, __isl_take isl_space
*set_space
)
5985 isl_pw_multi_aff
*opt
;
5986 isl_pw_aff
*min_expr_pa
;
5989 min_expr
= set_minimum(isl_basic_set_get_space(dom
), isl_mat_copy(cst
));
5990 min_expr_pa
= set_minimum_pa(isl_basic_set_get_space(dom
),
5993 opt
= basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, empty
, max
);
5996 *empty
= split(*empty
,
5997 isl_set_copy(min_expr
), isl_mat_copy(cst
));
5998 *empty
= isl_set_reset_space(*empty
, set_space
);
6001 opt
= split_domain_pma(opt
, min_expr_pa
, min_expr
, cst
);
6002 opt
= isl_pw_multi_aff_reset_space(opt
, map_space
);
6008 #define TYPE isl_pw_multi_aff
6010 #define SUFFIX _pw_multi_aff
6011 #include "isl_tab_lexopt_templ.c"