2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
5 * Use of this software is governed by the MIT license
7 * Written by Sven Verdoolaege, K.U.Leuven, Departement
8 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
10 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
13 #include <isl_ctx_private.h>
14 #include "isl_map_private.h"
17 #include "isl_sample.h"
18 #include <isl_mat_private.h>
19 #include <isl_aff_private.h>
20 #include <isl_options_private.h>
21 #include <isl_config.h>
24 * The implementation of parametric integer linear programming in this file
25 * was inspired by the paper "Parametric Integer Programming" and the
26 * report "Solving systems of affine (in)equalities" by Paul Feautrier
29 * The strategy used for obtaining a feasible solution is different
30 * from the one used in isl_tab.c. In particular, in isl_tab.c,
31 * upon finding a constraint that is not yet satisfied, we pivot
32 * in a row that increases the constant term of the row holding the
33 * constraint, making sure the sample solution remains feasible
34 * for all the constraints it already satisfied.
35 * Here, we always pivot in the row holding the constraint,
36 * choosing a column that induces the lexicographically smallest
37 * increment to the sample solution.
39 * By starting out from a sample value that is lexicographically
40 * smaller than any integer point in the problem space, the first
41 * feasible integer sample point we find will also be the lexicographically
42 * smallest. If all variables can be assumed to be non-negative,
43 * then the initial sample value may be chosen equal to zero.
44 * However, we will not make this assumption. Instead, we apply
45 * the "big parameter" trick. Any variable x is then not directly
46 * used in the tableau, but instead it is represented by another
47 * variable x' = M + x, where M is an arbitrarily large (positive)
48 * value. x' is therefore always non-negative, whatever the value of x.
49 * Taking as initial sample value x' = 0 corresponds to x = -M,
50 * which is always smaller than any possible value of x.
52 * The big parameter trick is used in the main tableau and
53 * also in the context tableau if isl_context_lex is used.
54 * In this case, each tableaus has its own big parameter.
55 * Before doing any real work, we check if all the parameters
56 * happen to be non-negative. If so, we drop the column corresponding
57 * to M from the initial context tableau.
58 * If isl_context_gbr is used, then the big parameter trick is only
59 * used in the main tableau.
63 struct isl_context_op
{
64 /* detect nonnegative parameters in context and mark them in tab */
65 struct isl_tab
*(*detect_nonnegative_parameters
)(
66 struct isl_context
*context
, struct isl_tab
*tab
);
67 /* return temporary reference to basic set representation of context */
68 struct isl_basic_set
*(*peek_basic_set
)(struct isl_context
*context
);
69 /* return temporary reference to tableau representation of context */
70 struct isl_tab
*(*peek_tab
)(struct isl_context
*context
);
71 /* add equality; check is 1 if eq may not be valid;
72 * update is 1 if we may want to call ineq_sign on context later.
74 void (*add_eq
)(struct isl_context
*context
, isl_int
*eq
,
75 int check
, int update
);
76 /* add inequality; check is 1 if ineq may not be valid;
77 * update is 1 if we may want to call ineq_sign on context later.
79 void (*add_ineq
)(struct isl_context
*context
, isl_int
*ineq
,
80 int check
, int update
);
81 /* check sign of ineq based on previous information.
82 * strict is 1 if saturation should be treated as a positive sign.
84 enum isl_tab_row_sign (*ineq_sign
)(struct isl_context
*context
,
85 isl_int
*ineq
, int strict
);
86 /* check if inequality maintains feasibility */
87 int (*test_ineq
)(struct isl_context
*context
, isl_int
*ineq
);
88 /* return index of a div that corresponds to "div" */
89 int (*get_div
)(struct isl_context
*context
, struct isl_tab
*tab
,
91 /* add div "div" to context and return non-negativity */
92 int (*add_div
)(struct isl_context
*context
, struct isl_vec
*div
);
93 int (*detect_equalities
)(struct isl_context
*context
,
95 /* return row index of "best" split */
96 int (*best_split
)(struct isl_context
*context
, struct isl_tab
*tab
);
97 /* check if context has already been determined to be empty */
98 int (*is_empty
)(struct isl_context
*context
);
99 /* check if context is still usable */
100 int (*is_ok
)(struct isl_context
*context
);
101 /* save a copy/snapshot of context */
102 void *(*save
)(struct isl_context
*context
);
103 /* restore saved context */
104 void (*restore
)(struct isl_context
*context
, void *);
105 /* discard saved context */
106 void (*discard
)(void *);
107 /* invalidate context */
108 void (*invalidate
)(struct isl_context
*context
);
110 void (*free
)(struct isl_context
*context
);
114 struct isl_context_op
*op
;
117 struct isl_context_lex
{
118 struct isl_context context
;
122 /* A stack (linked list) of solutions of subtrees of the search space.
124 * "M" describes the solution in terms of the dimensions of "dom".
125 * The number of columns of "M" is one more than the total number
126 * of dimensions of "dom".
128 * If "M" is NULL, then there is no solution on "dom".
130 struct isl_partial_sol
{
132 struct isl_basic_set
*dom
;
135 struct isl_partial_sol
*next
;
139 struct isl_sol_callback
{
140 struct isl_tab_callback callback
;
144 /* isl_sol is an interface for constructing a solution to
145 * a parametric integer linear programming problem.
146 * Every time the algorithm reaches a state where a solution
147 * can be read off from the tableau (including cases where the tableau
148 * is empty), the function "add" is called on the isl_sol passed
149 * to find_solutions_main.
151 * The context tableau is owned by isl_sol and is updated incrementally.
153 * There are currently two implementations of this interface,
154 * isl_sol_map, which simply collects the solutions in an isl_map
155 * and (optionally) the parts of the context where there is no solution
157 * isl_sol_for, which calls a user-defined function for each part of
166 struct isl_context
*context
;
167 struct isl_partial_sol
*partial
;
168 void (*add
)(struct isl_sol
*sol
,
169 struct isl_basic_set
*dom
, struct isl_mat
*M
);
170 void (*add_empty
)(struct isl_sol
*sol
, struct isl_basic_set
*bset
);
171 void (*free
)(struct isl_sol
*sol
);
172 struct isl_sol_callback dec_level
;
175 static void sol_free(struct isl_sol
*sol
)
177 struct isl_partial_sol
*partial
, *next
;
180 for (partial
= sol
->partial
; partial
; partial
= next
) {
181 next
= partial
->next
;
182 isl_basic_set_free(partial
->dom
);
183 isl_mat_free(partial
->M
);
189 /* Push a partial solution represented by a domain and mapping M
190 * onto the stack of partial solutions.
192 static void sol_push_sol(struct isl_sol
*sol
,
193 struct isl_basic_set
*dom
, struct isl_mat
*M
)
195 struct isl_partial_sol
*partial
;
197 if (sol
->error
|| !dom
)
200 partial
= isl_alloc_type(dom
->ctx
, struct isl_partial_sol
);
204 partial
->level
= sol
->level
;
207 partial
->next
= sol
->partial
;
209 sol
->partial
= partial
;
213 isl_basic_set_free(dom
);
218 /* Pop one partial solution from the partial solution stack and
219 * pass it on to sol->add or sol->add_empty.
221 static void sol_pop_one(struct isl_sol
*sol
)
223 struct isl_partial_sol
*partial
;
225 partial
= sol
->partial
;
226 sol
->partial
= partial
->next
;
229 sol
->add(sol
, partial
->dom
, partial
->M
);
231 sol
->add_empty(sol
, partial
->dom
);
235 /* Return a fresh copy of the domain represented by the context tableau.
237 static struct isl_basic_set
*sol_domain(struct isl_sol
*sol
)
239 struct isl_basic_set
*bset
;
244 bset
= isl_basic_set_dup(sol
->context
->op
->peek_basic_set(sol
->context
));
245 bset
= isl_basic_set_update_from_tab(bset
,
246 sol
->context
->op
->peek_tab(sol
->context
));
251 /* Check whether two partial solutions have the same mapping, where n_div
252 * is the number of divs that the two partial solutions have in common.
254 static int same_solution(struct isl_partial_sol
*s1
, struct isl_partial_sol
*s2
,
260 if (!s1
->M
!= !s2
->M
)
265 dim
= isl_basic_set_total_dim(s1
->dom
) - s1
->dom
->n_div
;
267 for (i
= 0; i
< s1
->M
->n_row
; ++i
) {
268 if (isl_seq_first_non_zero(s1
->M
->row
[i
]+1+dim
+n_div
,
269 s1
->M
->n_col
-1-dim
-n_div
) != -1)
271 if (isl_seq_first_non_zero(s2
->M
->row
[i
]+1+dim
+n_div
,
272 s2
->M
->n_col
-1-dim
-n_div
) != -1)
274 if (!isl_seq_eq(s1
->M
->row
[i
], s2
->M
->row
[i
], 1+dim
+n_div
))
280 /* Pop all solutions from the partial solution stack that were pushed onto
281 * the stack at levels that are deeper than the current level.
282 * If the two topmost elements on the stack have the same level
283 * and represent the same solution, then their domains are combined.
284 * This combined domain is the same as the current context domain
285 * as sol_pop is called each time we move back to a higher level.
287 static void sol_pop(struct isl_sol
*sol
)
289 struct isl_partial_sol
*partial
;
295 if (sol
->level
== 0) {
296 for (partial
= sol
->partial
; partial
; partial
= sol
->partial
)
301 partial
= sol
->partial
;
305 if (partial
->level
<= sol
->level
)
308 if (partial
->next
&& partial
->next
->level
== partial
->level
) {
309 n_div
= isl_basic_set_dim(
310 sol
->context
->op
->peek_basic_set(sol
->context
),
313 if (!same_solution(partial
, partial
->next
, n_div
)) {
317 struct isl_basic_set
*bset
;
321 n
= isl_basic_set_dim(partial
->next
->dom
, isl_dim_div
);
323 bset
= sol_domain(sol
);
324 isl_basic_set_free(partial
->next
->dom
);
325 partial
->next
->dom
= bset
;
326 M
= partial
->next
->M
;
328 M
= isl_mat_drop_cols(M
, M
->n_col
- n
, n
);
329 partial
->next
->M
= M
;
333 partial
->next
->level
= sol
->level
;
338 sol
->partial
= partial
->next
;
339 isl_basic_set_free(partial
->dom
);
340 isl_mat_free(partial
->M
);
347 error
: sol
->error
= 1;
350 static void sol_dec_level(struct isl_sol
*sol
)
360 static int sol_dec_level_wrap(struct isl_tab_callback
*cb
)
362 struct isl_sol_callback
*callback
= (struct isl_sol_callback
*)cb
;
364 sol_dec_level(callback
->sol
);
366 return callback
->sol
->error
? -1 : 0;
369 /* Move down to next level and push callback onto context tableau
370 * to decrease the level again when it gets rolled back across
371 * the current state. That is, dec_level will be called with
372 * the context tableau in the same state as it is when inc_level
375 static void sol_inc_level(struct isl_sol
*sol
)
383 tab
= sol
->context
->op
->peek_tab(sol
->context
);
384 if (isl_tab_push_callback(tab
, &sol
->dec_level
.callback
) < 0)
388 static void scale_rows(struct isl_mat
*mat
, isl_int m
, int n_row
)
392 if (isl_int_is_one(m
))
395 for (i
= 0; i
< n_row
; ++i
)
396 isl_seq_scale(mat
->row
[i
], mat
->row
[i
], m
, mat
->n_col
);
399 /* Add the solution identified by the tableau and the context tableau.
401 * The layout of the variables is as follows.
402 * tab->n_var is equal to the total number of variables in the input
403 * map (including divs that were copied from the context)
404 * + the number of extra divs constructed
405 * Of these, the first tab->n_param and the last tab->n_div variables
406 * correspond to the variables in the context, i.e.,
407 * tab->n_param + tab->n_div = context_tab->n_var
408 * tab->n_param is equal to the number of parameters and input
409 * dimensions in the input map
410 * tab->n_div is equal to the number of divs in the context
412 * If there is no solution, then call add_empty with a basic set
413 * that corresponds to the context tableau. (If add_empty is NULL,
416 * If there is a solution, then first construct a matrix that maps
417 * all dimensions of the context to the output variables, i.e.,
418 * the output dimensions in the input map.
419 * The divs in the input map (if any) that do not correspond to any
420 * div in the context do not appear in the solution.
421 * The algorithm will make sure that they have an integer value,
422 * but these values themselves are of no interest.
423 * We have to be careful not to drop or rearrange any divs in the
424 * context because that would change the meaning of the matrix.
426 * To extract the value of the output variables, it should be noted
427 * that we always use a big parameter M in the main tableau and so
428 * the variable stored in this tableau is not an output variable x itself, but
429 * x' = M + x (in case of minimization)
431 * x' = M - x (in case of maximization)
432 * If x' appears in a column, then its optimal value is zero,
433 * which means that the optimal value of x is an unbounded number
434 * (-M for minimization and M for maximization).
435 * We currently assume that the output dimensions in the original map
436 * are bounded, so this cannot occur.
437 * Similarly, when x' appears in a row, then the coefficient of M in that
438 * row is necessarily 1.
439 * If the row in the tableau represents
440 * d x' = c + d M + e(y)
441 * then, in case of minimization, the corresponding row in the matrix
444 * with a d = m, the (updated) common denominator of the matrix.
445 * In case of maximization, the row will be
448 static void sol_add(struct isl_sol
*sol
, struct isl_tab
*tab
)
450 struct isl_basic_set
*bset
= NULL
;
451 struct isl_mat
*mat
= NULL
;
456 if (sol
->error
|| !tab
)
459 if (tab
->empty
&& !sol
->add_empty
)
461 if (sol
->context
->op
->is_empty(sol
->context
))
464 bset
= sol_domain(sol
);
467 sol_push_sol(sol
, bset
, NULL
);
473 mat
= isl_mat_alloc(tab
->mat
->ctx
, 1 + sol
->n_out
,
474 1 + tab
->n_param
+ tab
->n_div
);
480 isl_seq_clr(mat
->row
[0] + 1, mat
->n_col
- 1);
481 isl_int_set_si(mat
->row
[0][0], 1);
482 for (row
= 0; row
< sol
->n_out
; ++row
) {
483 int i
= tab
->n_param
+ row
;
486 isl_seq_clr(mat
->row
[1 + row
], mat
->n_col
);
487 if (!tab
->var
[i
].is_row
) {
489 isl_die(mat
->ctx
, isl_error_invalid
,
490 "unbounded optimum", goto error2
);
494 r
= tab
->var
[i
].index
;
496 isl_int_ne(tab
->mat
->row
[r
][2], tab
->mat
->row
[r
][0]))
497 isl_die(mat
->ctx
, isl_error_invalid
,
498 "unbounded optimum", goto error2
);
499 isl_int_gcd(m
, mat
->row
[0][0], tab
->mat
->row
[r
][0]);
500 isl_int_divexact(m
, tab
->mat
->row
[r
][0], m
);
501 scale_rows(mat
, m
, 1 + row
);
502 isl_int_divexact(m
, mat
->row
[0][0], tab
->mat
->row
[r
][0]);
503 isl_int_mul(mat
->row
[1 + row
][0], m
, tab
->mat
->row
[r
][1]);
504 for (j
= 0; j
< tab
->n_param
; ++j
) {
506 if (tab
->var
[j
].is_row
)
508 col
= tab
->var
[j
].index
;
509 isl_int_mul(mat
->row
[1 + row
][1 + j
], m
,
510 tab
->mat
->row
[r
][off
+ col
]);
512 for (j
= 0; j
< tab
->n_div
; ++j
) {
514 if (tab
->var
[tab
->n_var
- tab
->n_div
+j
].is_row
)
516 col
= tab
->var
[tab
->n_var
- tab
->n_div
+j
].index
;
517 isl_int_mul(mat
->row
[1 + row
][1 + tab
->n_param
+ j
], m
,
518 tab
->mat
->row
[r
][off
+ col
]);
521 isl_seq_neg(mat
->row
[1 + row
], mat
->row
[1 + row
],
527 sol_push_sol(sol
, bset
, mat
);
532 isl_basic_set_free(bset
);
540 struct isl_set
*empty
;
543 static void sol_map_free(struct isl_sol_map
*sol_map
)
547 if (sol_map
->sol
.context
)
548 sol_map
->sol
.context
->op
->free(sol_map
->sol
.context
);
549 isl_map_free(sol_map
->map
);
550 isl_set_free(sol_map
->empty
);
554 static void sol_map_free_wrap(struct isl_sol
*sol
)
556 sol_map_free((struct isl_sol_map
*)sol
);
559 /* This function is called for parts of the context where there is
560 * no solution, with "bset" corresponding to the context tableau.
561 * Simply add the basic set to the set "empty".
563 static void sol_map_add_empty(struct isl_sol_map
*sol
,
564 struct isl_basic_set
*bset
)
568 isl_assert(bset
->ctx
, sol
->empty
, goto error
);
570 sol
->empty
= isl_set_grow(sol
->empty
, 1);
571 bset
= isl_basic_set_simplify(bset
);
572 bset
= isl_basic_set_finalize(bset
);
573 sol
->empty
= isl_set_add_basic_set(sol
->empty
, isl_basic_set_copy(bset
));
576 isl_basic_set_free(bset
);
579 isl_basic_set_free(bset
);
583 static void sol_map_add_empty_wrap(struct isl_sol
*sol
,
584 struct isl_basic_set
*bset
)
586 sol_map_add_empty((struct isl_sol_map
*)sol
, bset
);
589 /* Given a basic map "dom" that represents the context and an affine
590 * matrix "M" that maps the dimensions of the context to the
591 * output variables, construct a basic map with the same parameters
592 * and divs as the context, the dimensions of the context as input
593 * dimensions and a number of output dimensions that is equal to
594 * the number of output dimensions in the input map.
596 * The constraints and divs of the context are simply copied
597 * from "dom". For each row
601 * is added, with d the common denominator of M.
603 static void sol_map_add(struct isl_sol_map
*sol
,
604 struct isl_basic_set
*dom
, struct isl_mat
*M
)
607 struct isl_basic_map
*bmap
= NULL
;
615 if (sol
->sol
.error
|| !dom
|| !M
)
618 n_out
= sol
->sol
.n_out
;
619 n_eq
= dom
->n_eq
+ n_out
;
620 n_ineq
= dom
->n_ineq
;
622 nparam
= isl_basic_set_total_dim(dom
) - n_div
;
623 total
= isl_map_dim(sol
->map
, isl_dim_all
);
624 bmap
= isl_basic_map_alloc_space(isl_map_get_space(sol
->map
),
625 n_div
, n_eq
, 2 * n_div
+ n_ineq
);
628 if (sol
->sol
.rational
)
629 ISL_F_SET(bmap
, ISL_BASIC_MAP_RATIONAL
);
630 for (i
= 0; i
< dom
->n_div
; ++i
) {
631 int k
= isl_basic_map_alloc_div(bmap
);
634 isl_seq_cpy(bmap
->div
[k
], dom
->div
[i
], 1 + 1 + nparam
);
635 isl_seq_clr(bmap
->div
[k
] + 1 + 1 + nparam
, total
- nparam
);
636 isl_seq_cpy(bmap
->div
[k
] + 1 + 1 + total
,
637 dom
->div
[i
] + 1 + 1 + nparam
, i
);
639 for (i
= 0; i
< dom
->n_eq
; ++i
) {
640 int k
= isl_basic_map_alloc_equality(bmap
);
643 isl_seq_cpy(bmap
->eq
[k
], dom
->eq
[i
], 1 + nparam
);
644 isl_seq_clr(bmap
->eq
[k
] + 1 + nparam
, total
- nparam
);
645 isl_seq_cpy(bmap
->eq
[k
] + 1 + total
,
646 dom
->eq
[i
] + 1 + nparam
, n_div
);
648 for (i
= 0; i
< dom
->n_ineq
; ++i
) {
649 int k
= isl_basic_map_alloc_inequality(bmap
);
652 isl_seq_cpy(bmap
->ineq
[k
], dom
->ineq
[i
], 1 + nparam
);
653 isl_seq_clr(bmap
->ineq
[k
] + 1 + nparam
, total
- nparam
);
654 isl_seq_cpy(bmap
->ineq
[k
] + 1 + total
,
655 dom
->ineq
[i
] + 1 + nparam
, n_div
);
657 for (i
= 0; i
< M
->n_row
- 1; ++i
) {
658 int k
= isl_basic_map_alloc_equality(bmap
);
661 isl_seq_cpy(bmap
->eq
[k
], M
->row
[1 + i
], 1 + nparam
);
662 isl_seq_clr(bmap
->eq
[k
] + 1 + nparam
, n_out
);
663 isl_int_neg(bmap
->eq
[k
][1 + nparam
+ i
], M
->row
[0][0]);
664 isl_seq_cpy(bmap
->eq
[k
] + 1 + nparam
+ n_out
,
665 M
->row
[1 + i
] + 1 + nparam
, n_div
);
667 bmap
= isl_basic_map_simplify(bmap
);
668 bmap
= isl_basic_map_finalize(bmap
);
669 sol
->map
= isl_map_grow(sol
->map
, 1);
670 sol
->map
= isl_map_add_basic_map(sol
->map
, bmap
);
671 isl_basic_set_free(dom
);
677 isl_basic_set_free(dom
);
679 isl_basic_map_free(bmap
);
683 static void sol_map_add_wrap(struct isl_sol
*sol
,
684 struct isl_basic_set
*dom
, struct isl_mat
*M
)
686 sol_map_add((struct isl_sol_map
*)sol
, dom
, M
);
690 /* Store the "parametric constant" of row "row" of tableau "tab" in "line",
691 * i.e., the constant term and the coefficients of all variables that
692 * appear in the context tableau.
693 * Note that the coefficient of the big parameter M is NOT copied.
694 * The context tableau may not have a big parameter and even when it
695 * does, it is a different big parameter.
697 static void get_row_parameter_line(struct isl_tab
*tab
, int row
, isl_int
*line
)
700 unsigned off
= 2 + tab
->M
;
702 isl_int_set(line
[0], tab
->mat
->row
[row
][1]);
703 for (i
= 0; i
< tab
->n_param
; ++i
) {
704 if (tab
->var
[i
].is_row
)
705 isl_int_set_si(line
[1 + i
], 0);
707 int col
= tab
->var
[i
].index
;
708 isl_int_set(line
[1 + i
], tab
->mat
->row
[row
][off
+ col
]);
711 for (i
= 0; i
< tab
->n_div
; ++i
) {
712 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
713 isl_int_set_si(line
[1 + tab
->n_param
+ i
], 0);
715 int col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
716 isl_int_set(line
[1 + tab
->n_param
+ i
],
717 tab
->mat
->row
[row
][off
+ col
]);
722 /* Check if rows "row1" and "row2" have identical "parametric constants",
723 * as explained above.
724 * In this case, we also insist that the coefficients of the big parameter
725 * be the same as the values of the constants will only be the same
726 * if these coefficients are also the same.
728 static int identical_parameter_line(struct isl_tab
*tab
, int row1
, int row2
)
731 unsigned off
= 2 + tab
->M
;
733 if (isl_int_ne(tab
->mat
->row
[row1
][1], tab
->mat
->row
[row2
][1]))
736 if (tab
->M
&& isl_int_ne(tab
->mat
->row
[row1
][2],
737 tab
->mat
->row
[row2
][2]))
740 for (i
= 0; i
< tab
->n_param
+ tab
->n_div
; ++i
) {
741 int pos
= i
< tab
->n_param
? i
:
742 tab
->n_var
- tab
->n_div
+ i
- tab
->n_param
;
745 if (tab
->var
[pos
].is_row
)
747 col
= tab
->var
[pos
].index
;
748 if (isl_int_ne(tab
->mat
->row
[row1
][off
+ col
],
749 tab
->mat
->row
[row2
][off
+ col
]))
755 /* Return an inequality that expresses that the "parametric constant"
756 * should be non-negative.
757 * This function is only called when the coefficient of the big parameter
760 static struct isl_vec
*get_row_parameter_ineq(struct isl_tab
*tab
, int row
)
762 struct isl_vec
*ineq
;
764 ineq
= isl_vec_alloc(tab
->mat
->ctx
, 1 + tab
->n_param
+ tab
->n_div
);
768 get_row_parameter_line(tab
, row
, ineq
->el
);
770 ineq
= isl_vec_normalize(ineq
);
775 /* Normalize a div expression of the form
777 * [(g*f(x) + c)/(g * m)]
779 * with c the constant term and f(x) the remaining coefficients, to
783 static void normalize_div(__isl_keep isl_vec
*div
)
785 isl_ctx
*ctx
= isl_vec_get_ctx(div
);
786 int len
= div
->size
- 2;
788 isl_seq_gcd(div
->el
+ 2, len
, &ctx
->normalize_gcd
);
789 isl_int_gcd(ctx
->normalize_gcd
, ctx
->normalize_gcd
, div
->el
[0]);
791 if (isl_int_is_one(ctx
->normalize_gcd
))
794 isl_int_divexact(div
->el
[0], div
->el
[0], ctx
->normalize_gcd
);
795 isl_int_fdiv_q(div
->el
[1], div
->el
[1], ctx
->normalize_gcd
);
796 isl_seq_scale_down(div
->el
+ 2, div
->el
+ 2, ctx
->normalize_gcd
, len
);
799 /* Return a integer division for use in a parametric cut based on the given row.
800 * In particular, let the parametric constant of the row be
804 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
805 * The div returned is equal to
807 * floor(\sum_i {-a_i} y_i) = floor((\sum_i (-a_i mod d) y_i)/d)
809 static struct isl_vec
*get_row_parameter_div(struct isl_tab
*tab
, int row
)
813 div
= isl_vec_alloc(tab
->mat
->ctx
, 1 + 1 + tab
->n_param
+ tab
->n_div
);
817 isl_int_set(div
->el
[0], tab
->mat
->row
[row
][0]);
818 get_row_parameter_line(tab
, row
, div
->el
+ 1);
819 isl_seq_neg(div
->el
+ 1, div
->el
+ 1, div
->size
- 1);
821 isl_seq_fdiv_r(div
->el
+ 1, div
->el
+ 1, div
->el
[0], div
->size
- 1);
826 /* Return a integer division for use in transferring an integrality constraint
828 * In particular, let the parametric constant of the row be
832 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
833 * The the returned div is equal to
835 * floor(\sum_i {a_i} y_i) = floor((\sum_i (a_i mod d) y_i)/d)
837 static struct isl_vec
*get_row_split_div(struct isl_tab
*tab
, int row
)
841 div
= isl_vec_alloc(tab
->mat
->ctx
, 1 + 1 + tab
->n_param
+ tab
->n_div
);
845 isl_int_set(div
->el
[0], tab
->mat
->row
[row
][0]);
846 get_row_parameter_line(tab
, row
, div
->el
+ 1);
848 isl_seq_fdiv_r(div
->el
+ 1, div
->el
+ 1, div
->el
[0], div
->size
- 1);
853 /* Construct and return an inequality that expresses an upper bound
855 * In particular, if the div is given by
859 * then the inequality expresses
863 static struct isl_vec
*ineq_for_div(struct isl_basic_set
*bset
, unsigned div
)
867 struct isl_vec
*ineq
;
872 total
= isl_basic_set_total_dim(bset
);
873 div_pos
= 1 + total
- bset
->n_div
+ div
;
875 ineq
= isl_vec_alloc(bset
->ctx
, 1 + total
);
879 isl_seq_cpy(ineq
->el
, bset
->div
[div
] + 1, 1 + total
);
880 isl_int_neg(ineq
->el
[div_pos
], bset
->div
[div
][0]);
884 /* Given a row in the tableau and a div that was created
885 * using get_row_split_div and that has been constrained to equality, i.e.,
887 * d = floor(\sum_i {a_i} y_i) = \sum_i {a_i} y_i
889 * replace the expression "\sum_i {a_i} y_i" in the row by d,
890 * i.e., we subtract "\sum_i {a_i} y_i" and add 1 d.
891 * The coefficients of the non-parameters in the tableau have been
892 * verified to be integral. We can therefore simply replace coefficient b
893 * by floor(b). For the coefficients of the parameters we have
894 * floor(a_i) = a_i - {a_i}, while for the other coefficients, we have
897 static struct isl_tab
*set_row_cst_to_div(struct isl_tab
*tab
, int row
, int div
)
899 isl_seq_fdiv_q(tab
->mat
->row
[row
] + 1, tab
->mat
->row
[row
] + 1,
900 tab
->mat
->row
[row
][0], 1 + tab
->M
+ tab
->n_col
);
902 isl_int_set_si(tab
->mat
->row
[row
][0], 1);
904 if (tab
->var
[tab
->n_var
- tab
->n_div
+ div
].is_row
) {
905 int drow
= tab
->var
[tab
->n_var
- tab
->n_div
+ div
].index
;
907 isl_assert(tab
->mat
->ctx
,
908 isl_int_is_one(tab
->mat
->row
[drow
][0]), goto error
);
909 isl_seq_combine(tab
->mat
->row
[row
] + 1,
910 tab
->mat
->ctx
->one
, tab
->mat
->row
[row
] + 1,
911 tab
->mat
->ctx
->one
, tab
->mat
->row
[drow
] + 1,
912 1 + tab
->M
+ tab
->n_col
);
914 int dcol
= tab
->var
[tab
->n_var
- tab
->n_div
+ div
].index
;
916 isl_int_add_ui(tab
->mat
->row
[row
][2 + tab
->M
+ dcol
],
917 tab
->mat
->row
[row
][2 + tab
->M
+ dcol
], 1);
926 /* Check if the (parametric) constant of the given row is obviously
927 * negative, meaning that we don't need to consult the context tableau.
928 * If there is a big parameter and its coefficient is non-zero,
929 * then this coefficient determines the outcome.
930 * Otherwise, we check whether the constant is negative and
931 * all non-zero coefficients of parameters are negative and
932 * belong to non-negative parameters.
934 static int is_obviously_neg(struct isl_tab
*tab
, int row
)
938 unsigned off
= 2 + tab
->M
;
941 if (isl_int_is_pos(tab
->mat
->row
[row
][2]))
943 if (isl_int_is_neg(tab
->mat
->row
[row
][2]))
947 if (isl_int_is_nonneg(tab
->mat
->row
[row
][1]))
949 for (i
= 0; i
< tab
->n_param
; ++i
) {
950 /* Eliminated parameter */
951 if (tab
->var
[i
].is_row
)
953 col
= tab
->var
[i
].index
;
954 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
956 if (!tab
->var
[i
].is_nonneg
)
958 if (isl_int_is_pos(tab
->mat
->row
[row
][off
+ col
]))
961 for (i
= 0; i
< tab
->n_div
; ++i
) {
962 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
964 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
965 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
967 if (!tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_nonneg
)
969 if (isl_int_is_pos(tab
->mat
->row
[row
][off
+ col
]))
975 /* Check if the (parametric) constant of the given row is obviously
976 * non-negative, meaning that we don't need to consult the context tableau.
977 * If there is a big parameter and its coefficient is non-zero,
978 * then this coefficient determines the outcome.
979 * Otherwise, we check whether the constant is non-negative and
980 * all non-zero coefficients of parameters are positive and
981 * belong to non-negative parameters.
983 static int is_obviously_nonneg(struct isl_tab
*tab
, int row
)
987 unsigned off
= 2 + tab
->M
;
990 if (isl_int_is_pos(tab
->mat
->row
[row
][2]))
992 if (isl_int_is_neg(tab
->mat
->row
[row
][2]))
996 if (isl_int_is_neg(tab
->mat
->row
[row
][1]))
998 for (i
= 0; i
< tab
->n_param
; ++i
) {
999 /* Eliminated parameter */
1000 if (tab
->var
[i
].is_row
)
1002 col
= tab
->var
[i
].index
;
1003 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
1005 if (!tab
->var
[i
].is_nonneg
)
1007 if (isl_int_is_neg(tab
->mat
->row
[row
][off
+ col
]))
1010 for (i
= 0; i
< tab
->n_div
; ++i
) {
1011 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
1013 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
1014 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
1016 if (!tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_nonneg
)
1018 if (isl_int_is_neg(tab
->mat
->row
[row
][off
+ col
]))
1024 /* Given a row r and two columns, return the column that would
1025 * lead to the lexicographically smallest increment in the sample
1026 * solution when leaving the basis in favor of the row.
1027 * Pivoting with column c will increment the sample value by a non-negative
1028 * constant times a_{V,c}/a_{r,c}, with a_{V,c} the elements of column c
1029 * corresponding to the non-parametric variables.
1030 * If variable v appears in a column c_v, the a_{v,c} = 1 iff c = c_v,
1031 * with all other entries in this virtual row equal to zero.
1032 * If variable v appears in a row, then a_{v,c} is the element in column c
1035 * Let v be the first variable with a_{v,c1}/a_{r,c1} != a_{v,c2}/a_{r,c2}.
1036 * Then if a_{v,c1}/a_{r,c1} < a_{v,c2}/a_{r,c2}, i.e.,
1037 * a_{v,c2} a_{r,c1} - a_{v,c1} a_{r,c2} > 0, c1 results in the minimal
1038 * increment. Otherwise, it's c2.
1040 static int lexmin_col_pair(struct isl_tab
*tab
,
1041 int row
, int col1
, int col2
, isl_int tmp
)
1046 tr
= tab
->mat
->row
[row
] + 2 + tab
->M
;
1048 for (i
= tab
->n_param
; i
< tab
->n_var
- tab
->n_div
; ++i
) {
1052 if (!tab
->var
[i
].is_row
) {
1053 if (tab
->var
[i
].index
== col1
)
1055 if (tab
->var
[i
].index
== col2
)
1060 if (tab
->var
[i
].index
== row
)
1063 r
= tab
->mat
->row
[tab
->var
[i
].index
] + 2 + tab
->M
;
1064 s1
= isl_int_sgn(r
[col1
]);
1065 s2
= isl_int_sgn(r
[col2
]);
1066 if (s1
== 0 && s2
== 0)
1073 isl_int_mul(tmp
, r
[col2
], tr
[col1
]);
1074 isl_int_submul(tmp
, r
[col1
], tr
[col2
]);
1075 if (isl_int_is_pos(tmp
))
1077 if (isl_int_is_neg(tmp
))
1083 /* Given a row in the tableau, find and return the column that would
1084 * result in the lexicographically smallest, but positive, increment
1085 * in the sample point.
1086 * If there is no such column, then return tab->n_col.
1087 * If anything goes wrong, return -1.
1089 static int lexmin_pivot_col(struct isl_tab
*tab
, int row
)
1092 int col
= tab
->n_col
;
1096 tr
= tab
->mat
->row
[row
] + 2 + tab
->M
;
1100 for (j
= tab
->n_dead
; j
< tab
->n_col
; ++j
) {
1101 if (tab
->col_var
[j
] >= 0 &&
1102 (tab
->col_var
[j
] < tab
->n_param
||
1103 tab
->col_var
[j
] >= tab
->n_var
- tab
->n_div
))
1106 if (!isl_int_is_pos(tr
[j
]))
1109 if (col
== tab
->n_col
)
1112 col
= lexmin_col_pair(tab
, row
, col
, j
, tmp
);
1113 isl_assert(tab
->mat
->ctx
, col
>= 0, goto error
);
1123 /* Return the first known violated constraint, i.e., a non-negative
1124 * constraint that currently has an either obviously negative value
1125 * or a previously determined to be negative value.
1127 * If any constraint has a negative coefficient for the big parameter,
1128 * if any, then we return one of these first.
1130 static int first_neg(struct isl_tab
*tab
)
1135 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
1136 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
1138 if (!isl_int_is_neg(tab
->mat
->row
[row
][2]))
1141 tab
->row_sign
[row
] = isl_tab_row_neg
;
1144 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
1145 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
1147 if (tab
->row_sign
) {
1148 if (tab
->row_sign
[row
] == 0 &&
1149 is_obviously_neg(tab
, row
))
1150 tab
->row_sign
[row
] = isl_tab_row_neg
;
1151 if (tab
->row_sign
[row
] != isl_tab_row_neg
)
1153 } else if (!is_obviously_neg(tab
, row
))
1160 /* Check whether the invariant that all columns are lexico-positive
1161 * is satisfied. This function is not called from the current code
1162 * but is useful during debugging.
1164 static void check_lexpos(struct isl_tab
*tab
) __attribute__ ((unused
));
1165 static void check_lexpos(struct isl_tab
*tab
)
1167 unsigned off
= 2 + tab
->M
;
1172 for (col
= tab
->n_dead
; col
< tab
->n_col
; ++col
) {
1173 if (tab
->col_var
[col
] >= 0 &&
1174 (tab
->col_var
[col
] < tab
->n_param
||
1175 tab
->col_var
[col
] >= tab
->n_var
- tab
->n_div
))
1177 for (var
= tab
->n_param
; var
< tab
->n_var
- tab
->n_div
; ++var
) {
1178 if (!tab
->var
[var
].is_row
) {
1179 if (tab
->var
[var
].index
== col
)
1184 row
= tab
->var
[var
].index
;
1185 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
1187 if (isl_int_is_pos(tab
->mat
->row
[row
][off
+ col
]))
1189 fprintf(stderr
, "lexneg column %d (row %d)\n",
1192 if (var
>= tab
->n_var
- tab
->n_div
)
1193 fprintf(stderr
, "zero column %d\n", col
);
1197 /* Report to the caller that the given constraint is part of an encountered
1200 static int report_conflicting_constraint(struct isl_tab
*tab
, int con
)
1202 return tab
->conflict(con
, tab
->conflict_user
);
1205 /* Given a conflicting row in the tableau, report all constraints
1206 * involved in the row to the caller. That is, the row itself
1207 * (if it represents a constraint) and all constraint columns with
1208 * non-zero (and therefore negative) coefficients.
1210 static int report_conflict(struct isl_tab
*tab
, int row
)
1218 if (tab
->row_var
[row
] < 0 &&
1219 report_conflicting_constraint(tab
, ~tab
->row_var
[row
]) < 0)
1222 tr
= tab
->mat
->row
[row
] + 2 + tab
->M
;
1224 for (j
= tab
->n_dead
; j
< tab
->n_col
; ++j
) {
1225 if (tab
->col_var
[j
] >= 0 &&
1226 (tab
->col_var
[j
] < tab
->n_param
||
1227 tab
->col_var
[j
] >= tab
->n_var
- tab
->n_div
))
1230 if (!isl_int_is_neg(tr
[j
]))
1233 if (tab
->col_var
[j
] < 0 &&
1234 report_conflicting_constraint(tab
, ~tab
->col_var
[j
]) < 0)
1241 /* Resolve all known or obviously violated constraints through pivoting.
1242 * In particular, as long as we can find any violated constraint, we
1243 * look for a pivoting column that would result in the lexicographically
1244 * smallest increment in the sample point. If there is no such column
1245 * then the tableau is infeasible.
1247 static int restore_lexmin(struct isl_tab
*tab
) WARN_UNUSED
;
1248 static int restore_lexmin(struct isl_tab
*tab
)
1256 while ((row
= first_neg(tab
)) != -1) {
1257 col
= lexmin_pivot_col(tab
, row
);
1258 if (col
>= tab
->n_col
) {
1259 if (report_conflict(tab
, row
) < 0)
1261 if (isl_tab_mark_empty(tab
) < 0)
1267 if (isl_tab_pivot(tab
, row
, col
) < 0)
1273 /* Given a row that represents an equality, look for an appropriate
1275 * In particular, if there are any non-zero coefficients among
1276 * the non-parameter variables, then we take the last of these
1277 * variables. Eliminating this variable in terms of the other
1278 * variables and/or parameters does not influence the property
1279 * that all column in the initial tableau are lexicographically
1280 * positive. The row corresponding to the eliminated variable
1281 * will only have non-zero entries below the diagonal of the
1282 * initial tableau. That is, we transform
1288 * If there is no such non-parameter variable, then we are dealing with
1289 * pure parameter equality and we pick any parameter with coefficient 1 or -1
1290 * for elimination. This will ensure that the eliminated parameter
1291 * always has an integer value whenever all the other parameters are integral.
1292 * If there is no such parameter then we return -1.
1294 static int last_var_col_or_int_par_col(struct isl_tab
*tab
, int row
)
1296 unsigned off
= 2 + tab
->M
;
1299 for (i
= tab
->n_var
- tab
->n_div
- 1; i
>= 0 && i
>= tab
->n_param
; --i
) {
1301 if (tab
->var
[i
].is_row
)
1303 col
= tab
->var
[i
].index
;
1304 if (col
<= tab
->n_dead
)
1306 if (!isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
1309 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
1310 if (isl_int_is_one(tab
->mat
->row
[row
][off
+ i
]))
1312 if (isl_int_is_negone(tab
->mat
->row
[row
][off
+ i
]))
1318 /* Add an equality that is known to be valid to the tableau.
1319 * We first check if we can eliminate a variable or a parameter.
1320 * If not, we add the equality as two inequalities.
1321 * In this case, the equality was a pure parameter equality and there
1322 * is no need to resolve any constraint violations.
1324 static struct isl_tab
*add_lexmin_valid_eq(struct isl_tab
*tab
, isl_int
*eq
)
1331 r
= isl_tab_add_row(tab
, eq
);
1335 r
= tab
->con
[r
].index
;
1336 i
= last_var_col_or_int_par_col(tab
, r
);
1338 tab
->con
[r
].is_nonneg
= 1;
1339 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
1341 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
1342 r
= isl_tab_add_row(tab
, eq
);
1345 tab
->con
[r
].is_nonneg
= 1;
1346 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
1349 if (isl_tab_pivot(tab
, r
, i
) < 0)
1351 if (isl_tab_kill_col(tab
, i
) < 0)
1362 /* Check if the given row is a pure constant.
1364 static int is_constant(struct isl_tab
*tab
, int row
)
1366 unsigned off
= 2 + tab
->M
;
1368 return isl_seq_first_non_zero(tab
->mat
->row
[row
] + off
+ tab
->n_dead
,
1369 tab
->n_col
- tab
->n_dead
) == -1;
1372 /* Add an equality that may or may not be valid to the tableau.
1373 * If the resulting row is a pure constant, then it must be zero.
1374 * Otherwise, the resulting tableau is empty.
1376 * If the row is not a pure constant, then we add two inequalities,
1377 * each time checking that they can be satisfied.
1378 * In the end we try to use one of the two constraints to eliminate
1381 static int add_lexmin_eq(struct isl_tab
*tab
, isl_int
*eq
) WARN_UNUSED
;
1382 static int add_lexmin_eq(struct isl_tab
*tab
, isl_int
*eq
)
1386 struct isl_tab_undo
*snap
;
1390 snap
= isl_tab_snap(tab
);
1391 r1
= isl_tab_add_row(tab
, eq
);
1394 tab
->con
[r1
].is_nonneg
= 1;
1395 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r1
]) < 0)
1398 row
= tab
->con
[r1
].index
;
1399 if (is_constant(tab
, row
)) {
1400 if (!isl_int_is_zero(tab
->mat
->row
[row
][1]) ||
1401 (tab
->M
&& !isl_int_is_zero(tab
->mat
->row
[row
][2]))) {
1402 if (isl_tab_mark_empty(tab
) < 0)
1406 if (isl_tab_rollback(tab
, snap
) < 0)
1411 if (restore_lexmin(tab
) < 0)
1416 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
1418 r2
= isl_tab_add_row(tab
, eq
);
1421 tab
->con
[r2
].is_nonneg
= 1;
1422 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r2
]) < 0)
1425 if (restore_lexmin(tab
) < 0)
1430 if (!tab
->con
[r1
].is_row
) {
1431 if (isl_tab_kill_col(tab
, tab
->con
[r1
].index
) < 0)
1433 } else if (!tab
->con
[r2
].is_row
) {
1434 if (isl_tab_kill_col(tab
, tab
->con
[r2
].index
) < 0)
1439 tab
->bmap
= isl_basic_map_add_ineq(tab
->bmap
, eq
);
1440 if (isl_tab_push(tab
, isl_tab_undo_bmap_ineq
) < 0)
1442 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
1443 tab
->bmap
= isl_basic_map_add_ineq(tab
->bmap
, eq
);
1444 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
1445 if (isl_tab_push(tab
, isl_tab_undo_bmap_ineq
) < 0)
1454 /* Add an inequality to the tableau, resolving violations using
1457 static struct isl_tab
*add_lexmin_ineq(struct isl_tab
*tab
, isl_int
*ineq
)
1464 tab
->bmap
= isl_basic_map_add_ineq(tab
->bmap
, ineq
);
1465 if (isl_tab_push(tab
, isl_tab_undo_bmap_ineq
) < 0)
1470 r
= isl_tab_add_row(tab
, ineq
);
1473 tab
->con
[r
].is_nonneg
= 1;
1474 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
1476 if (isl_tab_row_is_redundant(tab
, tab
->con
[r
].index
)) {
1477 if (isl_tab_mark_redundant(tab
, tab
->con
[r
].index
) < 0)
1482 if (restore_lexmin(tab
) < 0)
1484 if (!tab
->empty
&& tab
->con
[r
].is_row
&&
1485 isl_tab_row_is_redundant(tab
, tab
->con
[r
].index
))
1486 if (isl_tab_mark_redundant(tab
, tab
->con
[r
].index
) < 0)
1494 /* Check if the coefficients of the parameters are all integral.
1496 static int integer_parameter(struct isl_tab
*tab
, int row
)
1500 unsigned off
= 2 + tab
->M
;
1502 for (i
= 0; i
< tab
->n_param
; ++i
) {
1503 /* Eliminated parameter */
1504 if (tab
->var
[i
].is_row
)
1506 col
= tab
->var
[i
].index
;
1507 if (!isl_int_is_divisible_by(tab
->mat
->row
[row
][off
+ col
],
1508 tab
->mat
->row
[row
][0]))
1511 for (i
= 0; i
< tab
->n_div
; ++i
) {
1512 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
1514 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
1515 if (!isl_int_is_divisible_by(tab
->mat
->row
[row
][off
+ col
],
1516 tab
->mat
->row
[row
][0]))
1522 /* Check if the coefficients of the non-parameter variables are all integral.
1524 static int integer_variable(struct isl_tab
*tab
, int row
)
1527 unsigned off
= 2 + tab
->M
;
1529 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
1530 if (tab
->col_var
[i
] >= 0 &&
1531 (tab
->col_var
[i
] < tab
->n_param
||
1532 tab
->col_var
[i
] >= tab
->n_var
- tab
->n_div
))
1534 if (!isl_int_is_divisible_by(tab
->mat
->row
[row
][off
+ i
],
1535 tab
->mat
->row
[row
][0]))
1541 /* Check if the constant term is integral.
1543 static int integer_constant(struct isl_tab
*tab
, int row
)
1545 return isl_int_is_divisible_by(tab
->mat
->row
[row
][1],
1546 tab
->mat
->row
[row
][0]);
1549 #define I_CST 1 << 0
1550 #define I_PAR 1 << 1
1551 #define I_VAR 1 << 2
1553 /* Check for next (non-parameter) variable after "var" (first if var == -1)
1554 * that is non-integer and therefore requires a cut and return
1555 * the index of the variable.
1556 * For parametric tableaus, there are three parts in a row,
1557 * the constant, the coefficients of the parameters and the rest.
1558 * For each part, we check whether the coefficients in that part
1559 * are all integral and if so, set the corresponding flag in *f.
1560 * If the constant and the parameter part are integral, then the
1561 * current sample value is integral and no cut is required
1562 * (irrespective of whether the variable part is integral).
1564 static int next_non_integer_var(struct isl_tab
*tab
, int var
, int *f
)
1566 var
= var
< 0 ? tab
->n_param
: var
+ 1;
1568 for (; var
< tab
->n_var
- tab
->n_div
; ++var
) {
1571 if (!tab
->var
[var
].is_row
)
1573 row
= tab
->var
[var
].index
;
1574 if (integer_constant(tab
, row
))
1575 ISL_FL_SET(flags
, I_CST
);
1576 if (integer_parameter(tab
, row
))
1577 ISL_FL_SET(flags
, I_PAR
);
1578 if (ISL_FL_ISSET(flags
, I_CST
) && ISL_FL_ISSET(flags
, I_PAR
))
1580 if (integer_variable(tab
, row
))
1581 ISL_FL_SET(flags
, I_VAR
);
1588 /* Check for first (non-parameter) variable that is non-integer and
1589 * therefore requires a cut and return the corresponding row.
1590 * For parametric tableaus, there are three parts in a row,
1591 * the constant, the coefficients of the parameters and the rest.
1592 * For each part, we check whether the coefficients in that part
1593 * are all integral and if so, set the corresponding flag in *f.
1594 * If the constant and the parameter part are integral, then the
1595 * current sample value is integral and no cut is required
1596 * (irrespective of whether the variable part is integral).
1598 static int first_non_integer_row(struct isl_tab
*tab
, int *f
)
1600 int var
= next_non_integer_var(tab
, -1, f
);
1602 return var
< 0 ? -1 : tab
->var
[var
].index
;
1605 /* Add a (non-parametric) cut to cut away the non-integral sample
1606 * value of the given row.
1608 * If the row is given by
1610 * m r = f + \sum_i a_i y_i
1614 * c = - {-f/m} + \sum_i {a_i/m} y_i >= 0
1616 * The big parameter, if any, is ignored, since it is assumed to be big
1617 * enough to be divisible by any integer.
1618 * If the tableau is actually a parametric tableau, then this function
1619 * is only called when all coefficients of the parameters are integral.
1620 * The cut therefore has zero coefficients for the parameters.
1622 * The current value is known to be negative, so row_sign, if it
1623 * exists, is set accordingly.
1625 * Return the row of the cut or -1.
1627 static int add_cut(struct isl_tab
*tab
, int row
)
1632 unsigned off
= 2 + tab
->M
;
1634 if (isl_tab_extend_cons(tab
, 1) < 0)
1636 r
= isl_tab_allocate_con(tab
);
1640 r_row
= tab
->mat
->row
[tab
->con
[r
].index
];
1641 isl_int_set(r_row
[0], tab
->mat
->row
[row
][0]);
1642 isl_int_neg(r_row
[1], tab
->mat
->row
[row
][1]);
1643 isl_int_fdiv_r(r_row
[1], r_row
[1], tab
->mat
->row
[row
][0]);
1644 isl_int_neg(r_row
[1], r_row
[1]);
1646 isl_int_set_si(r_row
[2], 0);
1647 for (i
= 0; i
< tab
->n_col
; ++i
)
1648 isl_int_fdiv_r(r_row
[off
+ i
],
1649 tab
->mat
->row
[row
][off
+ i
], tab
->mat
->row
[row
][0]);
1651 tab
->con
[r
].is_nonneg
= 1;
1652 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
1655 tab
->row_sign
[tab
->con
[r
].index
] = isl_tab_row_neg
;
1657 return tab
->con
[r
].index
;
1663 /* Given a non-parametric tableau, add cuts until an integer
1664 * sample point is obtained or until the tableau is determined
1665 * to be integer infeasible.
1666 * As long as there is any non-integer value in the sample point,
1667 * we add appropriate cuts, if possible, for each of these
1668 * non-integer values and then resolve the violated
1669 * cut constraints using restore_lexmin.
1670 * If one of the corresponding rows is equal to an integral
1671 * combination of variables/constraints plus a non-integral constant,
1672 * then there is no way to obtain an integer point and we return
1673 * a tableau that is marked empty.
1674 * The parameter cutting_strategy controls the strategy used when adding cuts
1675 * to remove non-integer points. CUT_ALL adds all possible cuts
1676 * before continuing the search. CUT_ONE adds only one cut at a time.
1678 static struct isl_tab
*cut_to_integer_lexmin(struct isl_tab
*tab
,
1679 int cutting_strategy
)
1690 while ((var
= next_non_integer_var(tab
, -1, &flags
)) != -1) {
1692 if (ISL_FL_ISSET(flags
, I_VAR
)) {
1693 if (isl_tab_mark_empty(tab
) < 0)
1697 row
= tab
->var
[var
].index
;
1698 row
= add_cut(tab
, row
);
1701 if (cutting_strategy
== CUT_ONE
)
1703 } while ((var
= next_non_integer_var(tab
, var
, &flags
)) != -1);
1704 if (restore_lexmin(tab
) < 0)
1715 /* Check whether all the currently active samples also satisfy the inequality
1716 * "ineq" (treated as an equality if eq is set).
1717 * Remove those samples that do not.
1719 static struct isl_tab
*check_samples(struct isl_tab
*tab
, isl_int
*ineq
, int eq
)
1727 isl_assert(tab
->mat
->ctx
, tab
->bmap
, goto error
);
1728 isl_assert(tab
->mat
->ctx
, tab
->samples
, goto error
);
1729 isl_assert(tab
->mat
->ctx
, tab
->samples
->n_col
== 1 + tab
->n_var
, goto error
);
1732 for (i
= tab
->n_outside
; i
< tab
->n_sample
; ++i
) {
1734 isl_seq_inner_product(ineq
, tab
->samples
->row
[i
],
1735 1 + tab
->n_var
, &v
);
1736 sgn
= isl_int_sgn(v
);
1737 if (eq
? (sgn
== 0) : (sgn
>= 0))
1739 tab
= isl_tab_drop_sample(tab
, i
);
1751 /* Check whether the sample value of the tableau is finite,
1752 * i.e., either the tableau does not use a big parameter, or
1753 * all values of the variables are equal to the big parameter plus
1754 * some constant. This constant is the actual sample value.
1756 static int sample_is_finite(struct isl_tab
*tab
)
1763 for (i
= 0; i
< tab
->n_var
; ++i
) {
1765 if (!tab
->var
[i
].is_row
)
1767 row
= tab
->var
[i
].index
;
1768 if (isl_int_ne(tab
->mat
->row
[row
][0], tab
->mat
->row
[row
][2]))
1774 /* Check if the context tableau of sol has any integer points.
1775 * Leave tab in empty state if no integer point can be found.
1776 * If an integer point can be found and if moreover it is finite,
1777 * then it is added to the list of sample values.
1779 * This function is only called when none of the currently active sample
1780 * values satisfies the most recently added constraint.
1782 static struct isl_tab
*check_integer_feasible(struct isl_tab
*tab
)
1784 struct isl_tab_undo
*snap
;
1789 snap
= isl_tab_snap(tab
);
1790 if (isl_tab_push_basis(tab
) < 0)
1793 tab
= cut_to_integer_lexmin(tab
, CUT_ALL
);
1797 if (!tab
->empty
&& sample_is_finite(tab
)) {
1798 struct isl_vec
*sample
;
1800 sample
= isl_tab_get_sample_value(tab
);
1802 tab
= isl_tab_add_sample(tab
, sample
);
1805 if (!tab
->empty
&& isl_tab_rollback(tab
, snap
) < 0)
1814 /* Check if any of the currently active sample values satisfies
1815 * the inequality "ineq" (an equality if eq is set).
1817 static int tab_has_valid_sample(struct isl_tab
*tab
, isl_int
*ineq
, int eq
)
1825 isl_assert(tab
->mat
->ctx
, tab
->bmap
, return -1);
1826 isl_assert(tab
->mat
->ctx
, tab
->samples
, return -1);
1827 isl_assert(tab
->mat
->ctx
, tab
->samples
->n_col
== 1 + tab
->n_var
, return -1);
1830 for (i
= tab
->n_outside
; i
< tab
->n_sample
; ++i
) {
1832 isl_seq_inner_product(ineq
, tab
->samples
->row
[i
],
1833 1 + tab
->n_var
, &v
);
1834 sgn
= isl_int_sgn(v
);
1835 if (eq
? (sgn
== 0) : (sgn
>= 0))
1840 return i
< tab
->n_sample
;
1843 /* Add a div specified by "div" to the tableau "tab" and return
1844 * 1 if the div is obviously non-negative.
1846 static int context_tab_add_div(struct isl_tab
*tab
, struct isl_vec
*div
,
1847 int (*add_ineq
)(void *user
, isl_int
*), void *user
)
1851 struct isl_mat
*samples
;
1854 r
= isl_tab_add_div(tab
, div
, add_ineq
, user
);
1857 nonneg
= tab
->var
[r
].is_nonneg
;
1858 tab
->var
[r
].frozen
= 1;
1860 samples
= isl_mat_extend(tab
->samples
,
1861 tab
->n_sample
, 1 + tab
->n_var
);
1862 tab
->samples
= samples
;
1865 for (i
= tab
->n_outside
; i
< samples
->n_row
; ++i
) {
1866 isl_seq_inner_product(div
->el
+ 1, samples
->row
[i
],
1867 div
->size
- 1, &samples
->row
[i
][samples
->n_col
- 1]);
1868 isl_int_fdiv_q(samples
->row
[i
][samples
->n_col
- 1],
1869 samples
->row
[i
][samples
->n_col
- 1], div
->el
[0]);
1875 /* Add a div specified by "div" to both the main tableau and
1876 * the context tableau. In case of the main tableau, we only
1877 * need to add an extra div. In the context tableau, we also
1878 * need to express the meaning of the div.
1879 * Return the index of the div or -1 if anything went wrong.
1881 static int add_div(struct isl_tab
*tab
, struct isl_context
*context
,
1882 struct isl_vec
*div
)
1887 if ((nonneg
= context
->op
->add_div(context
, div
)) < 0)
1890 if (!context
->op
->is_ok(context
))
1893 if (isl_tab_extend_vars(tab
, 1) < 0)
1895 r
= isl_tab_allocate_var(tab
);
1899 tab
->var
[r
].is_nonneg
= 1;
1900 tab
->var
[r
].frozen
= 1;
1903 return tab
->n_div
- 1;
1905 context
->op
->invalidate(context
);
1909 static int find_div(struct isl_tab
*tab
, isl_int
*div
, isl_int denom
)
1912 unsigned total
= isl_basic_map_total_dim(tab
->bmap
);
1914 for (i
= 0; i
< tab
->bmap
->n_div
; ++i
) {
1915 if (isl_int_ne(tab
->bmap
->div
[i
][0], denom
))
1917 if (!isl_seq_eq(tab
->bmap
->div
[i
] + 1, div
, 1 + total
))
1924 /* Return the index of a div that corresponds to "div".
1925 * We first check if we already have such a div and if not, we create one.
1927 static int get_div(struct isl_tab
*tab
, struct isl_context
*context
,
1928 struct isl_vec
*div
)
1931 struct isl_tab
*context_tab
= context
->op
->peek_tab(context
);
1936 d
= find_div(context_tab
, div
->el
+ 1, div
->el
[0]);
1940 return add_div(tab
, context
, div
);
1943 /* Add a parametric cut to cut away the non-integral sample value
1945 * Let a_i be the coefficients of the constant term and the parameters
1946 * and let b_i be the coefficients of the variables or constraints
1947 * in basis of the tableau.
1948 * Let q be the div q = floor(\sum_i {-a_i} y_i).
1950 * The cut is expressed as
1952 * c = \sum_i -{-a_i} y_i + \sum_i {b_i} x_i + q >= 0
1954 * If q did not already exist in the context tableau, then it is added first.
1955 * If q is in a column of the main tableau then the "+ q" can be accomplished
1956 * by setting the corresponding entry to the denominator of the constraint.
1957 * If q happens to be in a row of the main tableau, then the corresponding
1958 * row needs to be added instead (taking care of the denominators).
1959 * Note that this is very unlikely, but perhaps not entirely impossible.
1961 * The current value of the cut is known to be negative (or at least
1962 * non-positive), so row_sign is set accordingly.
1964 * Return the row of the cut or -1.
1966 static int add_parametric_cut(struct isl_tab
*tab
, int row
,
1967 struct isl_context
*context
)
1969 struct isl_vec
*div
;
1976 unsigned off
= 2 + tab
->M
;
1981 div
= get_row_parameter_div(tab
, row
);
1986 d
= context
->op
->get_div(context
, tab
, div
);
1991 if (isl_tab_extend_cons(tab
, 1) < 0)
1993 r
= isl_tab_allocate_con(tab
);
1997 r_row
= tab
->mat
->row
[tab
->con
[r
].index
];
1998 isl_int_set(r_row
[0], tab
->mat
->row
[row
][0]);
1999 isl_int_neg(r_row
[1], tab
->mat
->row
[row
][1]);
2000 isl_int_fdiv_r(r_row
[1], r_row
[1], tab
->mat
->row
[row
][0]);
2001 isl_int_neg(r_row
[1], r_row
[1]);
2003 isl_int_set_si(r_row
[2], 0);
2004 for (i
= 0; i
< tab
->n_param
; ++i
) {
2005 if (tab
->var
[i
].is_row
)
2007 col
= tab
->var
[i
].index
;
2008 isl_int_neg(r_row
[off
+ col
], tab
->mat
->row
[row
][off
+ col
]);
2009 isl_int_fdiv_r(r_row
[off
+ col
], r_row
[off
+ col
],
2010 tab
->mat
->row
[row
][0]);
2011 isl_int_neg(r_row
[off
+ col
], r_row
[off
+ col
]);
2013 for (i
= 0; i
< tab
->n_div
; ++i
) {
2014 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
2016 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
2017 isl_int_neg(r_row
[off
+ col
], tab
->mat
->row
[row
][off
+ col
]);
2018 isl_int_fdiv_r(r_row
[off
+ col
], r_row
[off
+ col
],
2019 tab
->mat
->row
[row
][0]);
2020 isl_int_neg(r_row
[off
+ col
], r_row
[off
+ col
]);
2022 for (i
= 0; i
< tab
->n_col
; ++i
) {
2023 if (tab
->col_var
[i
] >= 0 &&
2024 (tab
->col_var
[i
] < tab
->n_param
||
2025 tab
->col_var
[i
] >= tab
->n_var
- tab
->n_div
))
2027 isl_int_fdiv_r(r_row
[off
+ i
],
2028 tab
->mat
->row
[row
][off
+ i
], tab
->mat
->row
[row
][0]);
2030 if (tab
->var
[tab
->n_var
- tab
->n_div
+ d
].is_row
) {
2032 int d_row
= tab
->var
[tab
->n_var
- tab
->n_div
+ d
].index
;
2034 isl_int_gcd(gcd
, tab
->mat
->row
[d_row
][0], r_row
[0]);
2035 isl_int_divexact(r_row
[0], r_row
[0], gcd
);
2036 isl_int_divexact(gcd
, tab
->mat
->row
[d_row
][0], gcd
);
2037 isl_seq_combine(r_row
+ 1, gcd
, r_row
+ 1,
2038 r_row
[0], tab
->mat
->row
[d_row
] + 1,
2039 off
- 1 + tab
->n_col
);
2040 isl_int_mul(r_row
[0], r_row
[0], tab
->mat
->row
[d_row
][0]);
2043 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ d
].index
;
2044 isl_int_set(r_row
[off
+ col
], tab
->mat
->row
[row
][0]);
2047 tab
->con
[r
].is_nonneg
= 1;
2048 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
2051 tab
->row_sign
[tab
->con
[r
].index
] = isl_tab_row_neg
;
2053 row
= tab
->con
[r
].index
;
2055 if (d
>= n
&& context
->op
->detect_equalities(context
, tab
) < 0)
2061 /* Construct a tableau for bmap that can be used for computing
2062 * the lexicographic minimum (or maximum) of bmap.
2063 * If not NULL, then dom is the domain where the minimum
2064 * should be computed. In this case, we set up a parametric
2065 * tableau with row signs (initialized to "unknown").
2066 * If M is set, then the tableau will use a big parameter.
2067 * If max is set, then a maximum should be computed instead of a minimum.
2068 * This means that for each variable x, the tableau will contain the variable
2069 * x' = M - x, rather than x' = M + x. This in turn means that the coefficient
2070 * of the variables in all constraints are negated prior to adding them
2073 static struct isl_tab
*tab_for_lexmin(struct isl_basic_map
*bmap
,
2074 struct isl_basic_set
*dom
, unsigned M
, int max
)
2077 struct isl_tab
*tab
;
2079 tab
= isl_tab_alloc(bmap
->ctx
, 2 * bmap
->n_eq
+ bmap
->n_ineq
+ 1,
2080 isl_basic_map_total_dim(bmap
), M
);
2084 tab
->rational
= ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
2086 tab
->n_param
= isl_basic_set_total_dim(dom
) - dom
->n_div
;
2087 tab
->n_div
= dom
->n_div
;
2088 tab
->row_sign
= isl_calloc_array(bmap
->ctx
,
2089 enum isl_tab_row_sign
, tab
->mat
->n_row
);
2093 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
)) {
2094 if (isl_tab_mark_empty(tab
) < 0)
2099 for (i
= tab
->n_param
; i
< tab
->n_var
- tab
->n_div
; ++i
) {
2100 tab
->var
[i
].is_nonneg
= 1;
2101 tab
->var
[i
].frozen
= 1;
2103 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
2105 isl_seq_neg(bmap
->eq
[i
] + 1 + tab
->n_param
,
2106 bmap
->eq
[i
] + 1 + tab
->n_param
,
2107 tab
->n_var
- tab
->n_param
- tab
->n_div
);
2108 tab
= add_lexmin_valid_eq(tab
, bmap
->eq
[i
]);
2110 isl_seq_neg(bmap
->eq
[i
] + 1 + tab
->n_param
,
2111 bmap
->eq
[i
] + 1 + tab
->n_param
,
2112 tab
->n_var
- tab
->n_param
- tab
->n_div
);
2113 if (!tab
|| tab
->empty
)
2116 if (bmap
->n_eq
&& restore_lexmin(tab
) < 0)
2118 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
2120 isl_seq_neg(bmap
->ineq
[i
] + 1 + tab
->n_param
,
2121 bmap
->ineq
[i
] + 1 + tab
->n_param
,
2122 tab
->n_var
- tab
->n_param
- tab
->n_div
);
2123 tab
= add_lexmin_ineq(tab
, bmap
->ineq
[i
]);
2125 isl_seq_neg(bmap
->ineq
[i
] + 1 + tab
->n_param
,
2126 bmap
->ineq
[i
] + 1 + tab
->n_param
,
2127 tab
->n_var
- tab
->n_param
- tab
->n_div
);
2128 if (!tab
|| tab
->empty
)
2137 /* Given a main tableau where more than one row requires a split,
2138 * determine and return the "best" row to split on.
2140 * Given two rows in the main tableau, if the inequality corresponding
2141 * to the first row is redundant with respect to that of the second row
2142 * in the current tableau, then it is better to split on the second row,
2143 * since in the positive part, both row will be positive.
2144 * (In the negative part a pivot will have to be performed and just about
2145 * anything can happen to the sign of the other row.)
2147 * As a simple heuristic, we therefore select the row that makes the most
2148 * of the other rows redundant.
2150 * Perhaps it would also be useful to look at the number of constraints
2151 * that conflict with any given constraint.
2153 static int best_split(struct isl_tab
*tab
, struct isl_tab
*context_tab
)
2155 struct isl_tab_undo
*snap
;
2161 if (isl_tab_extend_cons(context_tab
, 2) < 0)
2164 snap
= isl_tab_snap(context_tab
);
2166 for (split
= tab
->n_redundant
; split
< tab
->n_row
; ++split
) {
2167 struct isl_tab_undo
*snap2
;
2168 struct isl_vec
*ineq
= NULL
;
2172 if (!isl_tab_var_from_row(tab
, split
)->is_nonneg
)
2174 if (tab
->row_sign
[split
] != isl_tab_row_any
)
2177 ineq
= get_row_parameter_ineq(tab
, split
);
2180 ok
= isl_tab_add_ineq(context_tab
, ineq
->el
) >= 0;
2185 snap2
= isl_tab_snap(context_tab
);
2187 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
2188 struct isl_tab_var
*var
;
2192 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
2194 if (tab
->row_sign
[row
] != isl_tab_row_any
)
2197 ineq
= get_row_parameter_ineq(tab
, row
);
2200 ok
= isl_tab_add_ineq(context_tab
, ineq
->el
) >= 0;
2204 var
= &context_tab
->con
[context_tab
->n_con
- 1];
2205 if (!context_tab
->empty
&&
2206 !isl_tab_min_at_most_neg_one(context_tab
, var
))
2208 if (isl_tab_rollback(context_tab
, snap2
) < 0)
2211 if (best
== -1 || r
> best_r
) {
2215 if (isl_tab_rollback(context_tab
, snap
) < 0)
2222 static struct isl_basic_set
*context_lex_peek_basic_set(
2223 struct isl_context
*context
)
2225 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2228 return isl_tab_peek_bset(clex
->tab
);
2231 static struct isl_tab
*context_lex_peek_tab(struct isl_context
*context
)
2233 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2237 static void context_lex_add_eq(struct isl_context
*context
, isl_int
*eq
,
2238 int check
, int update
)
2240 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2241 if (isl_tab_extend_cons(clex
->tab
, 2) < 0)
2243 if (add_lexmin_eq(clex
->tab
, eq
) < 0)
2246 int v
= tab_has_valid_sample(clex
->tab
, eq
, 1);
2250 clex
->tab
= check_integer_feasible(clex
->tab
);
2253 clex
->tab
= check_samples(clex
->tab
, eq
, 1);
2256 isl_tab_free(clex
->tab
);
2260 static void context_lex_add_ineq(struct isl_context
*context
, isl_int
*ineq
,
2261 int check
, int update
)
2263 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2264 if (isl_tab_extend_cons(clex
->tab
, 1) < 0)
2266 clex
->tab
= add_lexmin_ineq(clex
->tab
, ineq
);
2268 int v
= tab_has_valid_sample(clex
->tab
, ineq
, 0);
2272 clex
->tab
= check_integer_feasible(clex
->tab
);
2275 clex
->tab
= check_samples(clex
->tab
, ineq
, 0);
2278 isl_tab_free(clex
->tab
);
2282 static int context_lex_add_ineq_wrap(void *user
, isl_int
*ineq
)
2284 struct isl_context
*context
= (struct isl_context
*)user
;
2285 context_lex_add_ineq(context
, ineq
, 0, 0);
2286 return context
->op
->is_ok(context
) ? 0 : -1;
2289 /* Check which signs can be obtained by "ineq" on all the currently
2290 * active sample values. See row_sign for more information.
2292 static enum isl_tab_row_sign
tab_ineq_sign(struct isl_tab
*tab
, isl_int
*ineq
,
2298 enum isl_tab_row_sign res
= isl_tab_row_unknown
;
2300 isl_assert(tab
->mat
->ctx
, tab
->samples
, return isl_tab_row_unknown
);
2301 isl_assert(tab
->mat
->ctx
, tab
->samples
->n_col
== 1 + tab
->n_var
,
2302 return isl_tab_row_unknown
);
2305 for (i
= tab
->n_outside
; i
< tab
->n_sample
; ++i
) {
2306 isl_seq_inner_product(tab
->samples
->row
[i
], ineq
,
2307 1 + tab
->n_var
, &tmp
);
2308 sgn
= isl_int_sgn(tmp
);
2309 if (sgn
> 0 || (sgn
== 0 && strict
)) {
2310 if (res
== isl_tab_row_unknown
)
2311 res
= isl_tab_row_pos
;
2312 if (res
== isl_tab_row_neg
)
2313 res
= isl_tab_row_any
;
2316 if (res
== isl_tab_row_unknown
)
2317 res
= isl_tab_row_neg
;
2318 if (res
== isl_tab_row_pos
)
2319 res
= isl_tab_row_any
;
2321 if (res
== isl_tab_row_any
)
2329 static enum isl_tab_row_sign
context_lex_ineq_sign(struct isl_context
*context
,
2330 isl_int
*ineq
, int strict
)
2332 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2333 return tab_ineq_sign(clex
->tab
, ineq
, strict
);
2336 /* Check whether "ineq" can be added to the tableau without rendering
2339 static int context_lex_test_ineq(struct isl_context
*context
, isl_int
*ineq
)
2341 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2342 struct isl_tab_undo
*snap
;
2348 if (isl_tab_extend_cons(clex
->tab
, 1) < 0)
2351 snap
= isl_tab_snap(clex
->tab
);
2352 if (isl_tab_push_basis(clex
->tab
) < 0)
2354 clex
->tab
= add_lexmin_ineq(clex
->tab
, ineq
);
2355 clex
->tab
= check_integer_feasible(clex
->tab
);
2358 feasible
= !clex
->tab
->empty
;
2359 if (isl_tab_rollback(clex
->tab
, snap
) < 0)
2365 static int context_lex_get_div(struct isl_context
*context
, struct isl_tab
*tab
,
2366 struct isl_vec
*div
)
2368 return get_div(tab
, context
, div
);
2371 /* Add a div specified by "div" to the context tableau and return
2372 * 1 if the div is obviously non-negative.
2373 * context_tab_add_div will always return 1, because all variables
2374 * in a isl_context_lex tableau are non-negative.
2375 * However, if we are using a big parameter in the context, then this only
2376 * reflects the non-negativity of the variable used to _encode_ the
2377 * div, i.e., div' = M + div, so we can't draw any conclusions.
2379 static int context_lex_add_div(struct isl_context
*context
, struct isl_vec
*div
)
2381 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2383 nonneg
= context_tab_add_div(clex
->tab
, div
,
2384 context_lex_add_ineq_wrap
, context
);
2392 static int context_lex_detect_equalities(struct isl_context
*context
,
2393 struct isl_tab
*tab
)
2398 static int context_lex_best_split(struct isl_context
*context
,
2399 struct isl_tab
*tab
)
2401 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2402 struct isl_tab_undo
*snap
;
2405 snap
= isl_tab_snap(clex
->tab
);
2406 if (isl_tab_push_basis(clex
->tab
) < 0)
2408 r
= best_split(tab
, clex
->tab
);
2410 if (r
>= 0 && isl_tab_rollback(clex
->tab
, snap
) < 0)
2416 static int context_lex_is_empty(struct isl_context
*context
)
2418 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2421 return clex
->tab
->empty
;
2424 static void *context_lex_save(struct isl_context
*context
)
2426 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2427 struct isl_tab_undo
*snap
;
2429 snap
= isl_tab_snap(clex
->tab
);
2430 if (isl_tab_push_basis(clex
->tab
) < 0)
2432 if (isl_tab_save_samples(clex
->tab
) < 0)
2438 static void context_lex_restore(struct isl_context
*context
, void *save
)
2440 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2441 if (isl_tab_rollback(clex
->tab
, (struct isl_tab_undo
*)save
) < 0) {
2442 isl_tab_free(clex
->tab
);
2447 static void context_lex_discard(void *save
)
2451 static int context_lex_is_ok(struct isl_context
*context
)
2453 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2457 /* For each variable in the context tableau, check if the variable can
2458 * only attain non-negative values. If so, mark the parameter as non-negative
2459 * in the main tableau. This allows for a more direct identification of some
2460 * cases of violated constraints.
2462 static struct isl_tab
*tab_detect_nonnegative_parameters(struct isl_tab
*tab
,
2463 struct isl_tab
*context_tab
)
2466 struct isl_tab_undo
*snap
;
2467 struct isl_vec
*ineq
= NULL
;
2468 struct isl_tab_var
*var
;
2471 if (context_tab
->n_var
== 0)
2474 ineq
= isl_vec_alloc(tab
->mat
->ctx
, 1 + context_tab
->n_var
);
2478 if (isl_tab_extend_cons(context_tab
, 1) < 0)
2481 snap
= isl_tab_snap(context_tab
);
2484 isl_seq_clr(ineq
->el
, ineq
->size
);
2485 for (i
= 0; i
< context_tab
->n_var
; ++i
) {
2486 isl_int_set_si(ineq
->el
[1 + i
], 1);
2487 if (isl_tab_add_ineq(context_tab
, ineq
->el
) < 0)
2489 var
= &context_tab
->con
[context_tab
->n_con
- 1];
2490 if (!context_tab
->empty
&&
2491 !isl_tab_min_at_most_neg_one(context_tab
, var
)) {
2493 if (i
>= tab
->n_param
)
2494 j
= i
- tab
->n_param
+ tab
->n_var
- tab
->n_div
;
2495 tab
->var
[j
].is_nonneg
= 1;
2498 isl_int_set_si(ineq
->el
[1 + i
], 0);
2499 if (isl_tab_rollback(context_tab
, snap
) < 0)
2503 if (context_tab
->M
&& n
== context_tab
->n_var
) {
2504 context_tab
->mat
= isl_mat_drop_cols(context_tab
->mat
, 2, 1);
2516 static struct isl_tab
*context_lex_detect_nonnegative_parameters(
2517 struct isl_context
*context
, struct isl_tab
*tab
)
2519 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2520 struct isl_tab_undo
*snap
;
2525 snap
= isl_tab_snap(clex
->tab
);
2526 if (isl_tab_push_basis(clex
->tab
) < 0)
2529 tab
= tab_detect_nonnegative_parameters(tab
, clex
->tab
);
2531 if (isl_tab_rollback(clex
->tab
, snap
) < 0)
2540 static void context_lex_invalidate(struct isl_context
*context
)
2542 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2543 isl_tab_free(clex
->tab
);
2547 static void context_lex_free(struct isl_context
*context
)
2549 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2550 isl_tab_free(clex
->tab
);
2554 struct isl_context_op isl_context_lex_op
= {
2555 context_lex_detect_nonnegative_parameters
,
2556 context_lex_peek_basic_set
,
2557 context_lex_peek_tab
,
2559 context_lex_add_ineq
,
2560 context_lex_ineq_sign
,
2561 context_lex_test_ineq
,
2562 context_lex_get_div
,
2563 context_lex_add_div
,
2564 context_lex_detect_equalities
,
2565 context_lex_best_split
,
2566 context_lex_is_empty
,
2569 context_lex_restore
,
2570 context_lex_discard
,
2571 context_lex_invalidate
,
2575 static struct isl_tab
*context_tab_for_lexmin(struct isl_basic_set
*bset
)
2577 struct isl_tab
*tab
;
2581 tab
= tab_for_lexmin((struct isl_basic_map
*)bset
, NULL
, 1, 0);
2584 if (isl_tab_track_bset(tab
, bset
) < 0)
2586 tab
= isl_tab_init_samples(tab
);
2589 isl_basic_set_free(bset
);
2593 static struct isl_context
*isl_context_lex_alloc(struct isl_basic_set
*dom
)
2595 struct isl_context_lex
*clex
;
2600 clex
= isl_alloc_type(dom
->ctx
, struct isl_context_lex
);
2604 clex
->context
.op
= &isl_context_lex_op
;
2606 clex
->tab
= context_tab_for_lexmin(isl_basic_set_copy(dom
));
2607 if (restore_lexmin(clex
->tab
) < 0)
2609 clex
->tab
= check_integer_feasible(clex
->tab
);
2613 return &clex
->context
;
2615 clex
->context
.op
->free(&clex
->context
);
2619 /* Representation of the context when using generalized basis reduction.
2621 * "shifted" contains the offsets of the unit hypercubes that lie inside the
2622 * context. Any rational point in "shifted" can therefore be rounded
2623 * up to an integer point in the context.
2624 * If the context is constrained by any equality, then "shifted" is not used
2625 * as it would be empty.
2627 struct isl_context_gbr
{
2628 struct isl_context context
;
2629 struct isl_tab
*tab
;
2630 struct isl_tab
*shifted
;
2631 struct isl_tab
*cone
;
2634 static struct isl_tab
*context_gbr_detect_nonnegative_parameters(
2635 struct isl_context
*context
, struct isl_tab
*tab
)
2637 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2640 return tab_detect_nonnegative_parameters(tab
, cgbr
->tab
);
2643 static struct isl_basic_set
*context_gbr_peek_basic_set(
2644 struct isl_context
*context
)
2646 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2649 return isl_tab_peek_bset(cgbr
->tab
);
2652 static struct isl_tab
*context_gbr_peek_tab(struct isl_context
*context
)
2654 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2658 /* Initialize the "shifted" tableau of the context, which
2659 * contains the constraints of the original tableau shifted
2660 * by the sum of all negative coefficients. This ensures
2661 * that any rational point in the shifted tableau can
2662 * be rounded up to yield an integer point in the original tableau.
2664 static void gbr_init_shifted(struct isl_context_gbr
*cgbr
)
2667 struct isl_vec
*cst
;
2668 struct isl_basic_set
*bset
= isl_tab_peek_bset(cgbr
->tab
);
2669 unsigned dim
= isl_basic_set_total_dim(bset
);
2671 cst
= isl_vec_alloc(cgbr
->tab
->mat
->ctx
, bset
->n_ineq
);
2675 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
2676 isl_int_set(cst
->el
[i
], bset
->ineq
[i
][0]);
2677 for (j
= 0; j
< dim
; ++j
) {
2678 if (!isl_int_is_neg(bset
->ineq
[i
][1 + j
]))
2680 isl_int_add(bset
->ineq
[i
][0], bset
->ineq
[i
][0],
2681 bset
->ineq
[i
][1 + j
]);
2685 cgbr
->shifted
= isl_tab_from_basic_set(bset
, 0);
2687 for (i
= 0; i
< bset
->n_ineq
; ++i
)
2688 isl_int_set(bset
->ineq
[i
][0], cst
->el
[i
]);
2693 /* Check if the shifted tableau is non-empty, and if so
2694 * use the sample point to construct an integer point
2695 * of the context tableau.
2697 static struct isl_vec
*gbr_get_shifted_sample(struct isl_context_gbr
*cgbr
)
2699 struct isl_vec
*sample
;
2702 gbr_init_shifted(cgbr
);
2705 if (cgbr
->shifted
->empty
)
2706 return isl_vec_alloc(cgbr
->tab
->mat
->ctx
, 0);
2708 sample
= isl_tab_get_sample_value(cgbr
->shifted
);
2709 sample
= isl_vec_ceil(sample
);
2714 static struct isl_basic_set
*drop_constant_terms(struct isl_basic_set
*bset
)
2721 for (i
= 0; i
< bset
->n_eq
; ++i
)
2722 isl_int_set_si(bset
->eq
[i
][0], 0);
2724 for (i
= 0; i
< bset
->n_ineq
; ++i
)
2725 isl_int_set_si(bset
->ineq
[i
][0], 0);
2730 static int use_shifted(struct isl_context_gbr
*cgbr
)
2732 return cgbr
->tab
->bmap
->n_eq
== 0 && cgbr
->tab
->bmap
->n_div
== 0;
2735 static struct isl_vec
*gbr_get_sample(struct isl_context_gbr
*cgbr
)
2737 struct isl_basic_set
*bset
;
2738 struct isl_basic_set
*cone
;
2740 if (isl_tab_sample_is_integer(cgbr
->tab
))
2741 return isl_tab_get_sample_value(cgbr
->tab
);
2743 if (use_shifted(cgbr
)) {
2744 struct isl_vec
*sample
;
2746 sample
= gbr_get_shifted_sample(cgbr
);
2747 if (!sample
|| sample
->size
> 0)
2750 isl_vec_free(sample
);
2754 bset
= isl_tab_peek_bset(cgbr
->tab
);
2755 cgbr
->cone
= isl_tab_from_recession_cone(bset
, 0);
2758 if (isl_tab_track_bset(cgbr
->cone
,
2759 isl_basic_set_copy(bset
)) < 0)
2762 if (isl_tab_detect_implicit_equalities(cgbr
->cone
) < 0)
2765 if (cgbr
->cone
->n_dead
== cgbr
->cone
->n_col
) {
2766 struct isl_vec
*sample
;
2767 struct isl_tab_undo
*snap
;
2769 if (cgbr
->tab
->basis
) {
2770 if (cgbr
->tab
->basis
->n_col
!= 1 + cgbr
->tab
->n_var
) {
2771 isl_mat_free(cgbr
->tab
->basis
);
2772 cgbr
->tab
->basis
= NULL
;
2774 cgbr
->tab
->n_zero
= 0;
2775 cgbr
->tab
->n_unbounded
= 0;
2778 snap
= isl_tab_snap(cgbr
->tab
);
2780 sample
= isl_tab_sample(cgbr
->tab
);
2782 if (isl_tab_rollback(cgbr
->tab
, snap
) < 0) {
2783 isl_vec_free(sample
);
2790 cone
= isl_basic_set_dup(isl_tab_peek_bset(cgbr
->cone
));
2791 cone
= drop_constant_terms(cone
);
2792 cone
= isl_basic_set_update_from_tab(cone
, cgbr
->cone
);
2793 cone
= isl_basic_set_underlying_set(cone
);
2794 cone
= isl_basic_set_gauss(cone
, NULL
);
2796 bset
= isl_basic_set_dup(isl_tab_peek_bset(cgbr
->tab
));
2797 bset
= isl_basic_set_update_from_tab(bset
, cgbr
->tab
);
2798 bset
= isl_basic_set_underlying_set(bset
);
2799 bset
= isl_basic_set_gauss(bset
, NULL
);
2801 return isl_basic_set_sample_with_cone(bset
, cone
);
2804 static void check_gbr_integer_feasible(struct isl_context_gbr
*cgbr
)
2806 struct isl_vec
*sample
;
2811 if (cgbr
->tab
->empty
)
2814 sample
= gbr_get_sample(cgbr
);
2818 if (sample
->size
== 0) {
2819 isl_vec_free(sample
);
2820 if (isl_tab_mark_empty(cgbr
->tab
) < 0)
2825 cgbr
->tab
= isl_tab_add_sample(cgbr
->tab
, sample
);
2829 isl_tab_free(cgbr
->tab
);
2833 static struct isl_tab
*add_gbr_eq(struct isl_tab
*tab
, isl_int
*eq
)
2838 if (isl_tab_extend_cons(tab
, 2) < 0)
2841 if (isl_tab_add_eq(tab
, eq
) < 0)
2850 /* Add the equality described by "eq" to the context.
2851 * If "check" is set, then we check if the context is empty after
2852 * adding the equality.
2853 * If "update" is set, then we check if the samples are still valid.
2855 * We do not explicitly add shifted copies of the equality to
2856 * cgbr->shifted since they would conflict with each other.
2857 * Instead, we directly mark cgbr->shifted empty.
2859 static void context_gbr_add_eq(struct isl_context
*context
, isl_int
*eq
,
2860 int check
, int update
)
2862 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2864 cgbr
->tab
= add_gbr_eq(cgbr
->tab
, eq
);
2866 if (cgbr
->shifted
&& !cgbr
->shifted
->empty
&& use_shifted(cgbr
)) {
2867 if (isl_tab_mark_empty(cgbr
->shifted
) < 0)
2871 if (cgbr
->cone
&& cgbr
->cone
->n_col
!= cgbr
->cone
->n_dead
) {
2872 if (isl_tab_extend_cons(cgbr
->cone
, 2) < 0)
2874 if (isl_tab_add_eq(cgbr
->cone
, eq
) < 0)
2879 int v
= tab_has_valid_sample(cgbr
->tab
, eq
, 1);
2883 check_gbr_integer_feasible(cgbr
);
2886 cgbr
->tab
= check_samples(cgbr
->tab
, eq
, 1);
2889 isl_tab_free(cgbr
->tab
);
2893 static void add_gbr_ineq(struct isl_context_gbr
*cgbr
, isl_int
*ineq
)
2898 if (isl_tab_extend_cons(cgbr
->tab
, 1) < 0)
2901 if (isl_tab_add_ineq(cgbr
->tab
, ineq
) < 0)
2904 if (cgbr
->shifted
&& !cgbr
->shifted
->empty
&& use_shifted(cgbr
)) {
2907 dim
= isl_basic_map_total_dim(cgbr
->tab
->bmap
);
2909 if (isl_tab_extend_cons(cgbr
->shifted
, 1) < 0)
2912 for (i
= 0; i
< dim
; ++i
) {
2913 if (!isl_int_is_neg(ineq
[1 + i
]))
2915 isl_int_add(ineq
[0], ineq
[0], ineq
[1 + i
]);
2918 if (isl_tab_add_ineq(cgbr
->shifted
, ineq
) < 0)
2921 for (i
= 0; i
< dim
; ++i
) {
2922 if (!isl_int_is_neg(ineq
[1 + i
]))
2924 isl_int_sub(ineq
[0], ineq
[0], ineq
[1 + i
]);
2928 if (cgbr
->cone
&& cgbr
->cone
->n_col
!= cgbr
->cone
->n_dead
) {
2929 if (isl_tab_extend_cons(cgbr
->cone
, 1) < 0)
2931 if (isl_tab_add_ineq(cgbr
->cone
, ineq
) < 0)
2937 isl_tab_free(cgbr
->tab
);
2941 static void context_gbr_add_ineq(struct isl_context
*context
, isl_int
*ineq
,
2942 int check
, int update
)
2944 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2946 add_gbr_ineq(cgbr
, ineq
);
2951 int v
= tab_has_valid_sample(cgbr
->tab
, ineq
, 0);
2955 check_gbr_integer_feasible(cgbr
);
2958 cgbr
->tab
= check_samples(cgbr
->tab
, ineq
, 0);
2961 isl_tab_free(cgbr
->tab
);
2965 static int context_gbr_add_ineq_wrap(void *user
, isl_int
*ineq
)
2967 struct isl_context
*context
= (struct isl_context
*)user
;
2968 context_gbr_add_ineq(context
, ineq
, 0, 0);
2969 return context
->op
->is_ok(context
) ? 0 : -1;
2972 static enum isl_tab_row_sign
context_gbr_ineq_sign(struct isl_context
*context
,
2973 isl_int
*ineq
, int strict
)
2975 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2976 return tab_ineq_sign(cgbr
->tab
, ineq
, strict
);
2979 /* Check whether "ineq" can be added to the tableau without rendering
2982 static int context_gbr_test_ineq(struct isl_context
*context
, isl_int
*ineq
)
2984 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2985 struct isl_tab_undo
*snap
;
2986 struct isl_tab_undo
*shifted_snap
= NULL
;
2987 struct isl_tab_undo
*cone_snap
= NULL
;
2993 if (isl_tab_extend_cons(cgbr
->tab
, 1) < 0)
2996 snap
= isl_tab_snap(cgbr
->tab
);
2998 shifted_snap
= isl_tab_snap(cgbr
->shifted
);
3000 cone_snap
= isl_tab_snap(cgbr
->cone
);
3001 add_gbr_ineq(cgbr
, ineq
);
3002 check_gbr_integer_feasible(cgbr
);
3005 feasible
= !cgbr
->tab
->empty
;
3006 if (isl_tab_rollback(cgbr
->tab
, snap
) < 0)
3009 if (isl_tab_rollback(cgbr
->shifted
, shifted_snap
))
3011 } else if (cgbr
->shifted
) {
3012 isl_tab_free(cgbr
->shifted
);
3013 cgbr
->shifted
= NULL
;
3016 if (isl_tab_rollback(cgbr
->cone
, cone_snap
))
3018 } else if (cgbr
->cone
) {
3019 isl_tab_free(cgbr
->cone
);
3026 /* Return the column of the last of the variables associated to
3027 * a column that has a non-zero coefficient.
3028 * This function is called in a context where only coefficients
3029 * of parameters or divs can be non-zero.
3031 static int last_non_zero_var_col(struct isl_tab
*tab
, isl_int
*p
)
3036 if (tab
->n_var
== 0)
3039 for (i
= tab
->n_var
- 1; i
>= 0; --i
) {
3040 if (i
>= tab
->n_param
&& i
< tab
->n_var
- tab
->n_div
)
3042 if (tab
->var
[i
].is_row
)
3044 col
= tab
->var
[i
].index
;
3045 if (!isl_int_is_zero(p
[col
]))
3052 /* Look through all the recently added equalities in the context
3053 * to see if we can propagate any of them to the main tableau.
3055 * The newly added equalities in the context are encoded as pairs
3056 * of inequalities starting at inequality "first".
3058 * We tentatively add each of these equalities to the main tableau
3059 * and if this happens to result in a row with a final coefficient
3060 * that is one or negative one, we use it to kill a column
3061 * in the main tableau. Otherwise, we discard the tentatively
3064 static void propagate_equalities(struct isl_context_gbr
*cgbr
,
3065 struct isl_tab
*tab
, unsigned first
)
3068 struct isl_vec
*eq
= NULL
;
3070 eq
= isl_vec_alloc(tab
->mat
->ctx
, 1 + tab
->n_var
);
3074 if (isl_tab_extend_cons(tab
, (cgbr
->tab
->bmap
->n_ineq
- first
)/2) < 0)
3077 isl_seq_clr(eq
->el
+ 1 + tab
->n_param
,
3078 tab
->n_var
- tab
->n_param
- tab
->n_div
);
3079 for (i
= first
; i
< cgbr
->tab
->bmap
->n_ineq
; i
+= 2) {
3082 struct isl_tab_undo
*snap
;
3083 snap
= isl_tab_snap(tab
);
3085 isl_seq_cpy(eq
->el
, cgbr
->tab
->bmap
->ineq
[i
], 1 + tab
->n_param
);
3086 isl_seq_cpy(eq
->el
+ 1 + tab
->n_var
- tab
->n_div
,
3087 cgbr
->tab
->bmap
->ineq
[i
] + 1 + tab
->n_param
,
3090 r
= isl_tab_add_row(tab
, eq
->el
);
3093 r
= tab
->con
[r
].index
;
3094 j
= last_non_zero_var_col(tab
, tab
->mat
->row
[r
] + 2 + tab
->M
);
3095 if (j
< 0 || j
< tab
->n_dead
||
3096 !isl_int_is_one(tab
->mat
->row
[r
][0]) ||
3097 (!isl_int_is_one(tab
->mat
->row
[r
][2 + tab
->M
+ j
]) &&
3098 !isl_int_is_negone(tab
->mat
->row
[r
][2 + tab
->M
+ j
]))) {
3099 if (isl_tab_rollback(tab
, snap
) < 0)
3103 if (isl_tab_pivot(tab
, r
, j
) < 0)
3105 if (isl_tab_kill_col(tab
, j
) < 0)
3108 if (restore_lexmin(tab
) < 0)
3117 isl_tab_free(cgbr
->tab
);
3121 static int context_gbr_detect_equalities(struct isl_context
*context
,
3122 struct isl_tab
*tab
)
3124 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3125 struct isl_ctx
*ctx
;
3128 ctx
= cgbr
->tab
->mat
->ctx
;
3131 struct isl_basic_set
*bset
= isl_tab_peek_bset(cgbr
->tab
);
3132 cgbr
->cone
= isl_tab_from_recession_cone(bset
, 0);
3135 if (isl_tab_track_bset(cgbr
->cone
,
3136 isl_basic_set_copy(bset
)) < 0)
3139 if (isl_tab_detect_implicit_equalities(cgbr
->cone
) < 0)
3142 n_ineq
= cgbr
->tab
->bmap
->n_ineq
;
3143 cgbr
->tab
= isl_tab_detect_equalities(cgbr
->tab
, cgbr
->cone
);
3146 if (cgbr
->tab
->bmap
->n_ineq
> n_ineq
)
3147 propagate_equalities(cgbr
, tab
, n_ineq
);
3151 isl_tab_free(cgbr
->tab
);
3156 static int context_gbr_get_div(struct isl_context
*context
, struct isl_tab
*tab
,
3157 struct isl_vec
*div
)
3159 return get_div(tab
, context
, div
);
3162 static int context_gbr_add_div(struct isl_context
*context
, struct isl_vec
*div
)
3164 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3168 if (isl_tab_extend_cons(cgbr
->cone
, 3) < 0)
3170 if (isl_tab_extend_vars(cgbr
->cone
, 1) < 0)
3172 if (isl_tab_allocate_var(cgbr
->cone
) <0)
3175 cgbr
->cone
->bmap
= isl_basic_map_extend_space(cgbr
->cone
->bmap
,
3176 isl_basic_map_get_space(cgbr
->cone
->bmap
), 1, 0, 2);
3177 k
= isl_basic_map_alloc_div(cgbr
->cone
->bmap
);
3180 isl_seq_cpy(cgbr
->cone
->bmap
->div
[k
], div
->el
, div
->size
);
3181 if (isl_tab_push(cgbr
->cone
, isl_tab_undo_bmap_div
) < 0)
3184 return context_tab_add_div(cgbr
->tab
, div
,
3185 context_gbr_add_ineq_wrap
, context
);
3188 static int context_gbr_best_split(struct isl_context
*context
,
3189 struct isl_tab
*tab
)
3191 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3192 struct isl_tab_undo
*snap
;
3195 snap
= isl_tab_snap(cgbr
->tab
);
3196 r
= best_split(tab
, cgbr
->tab
);
3198 if (r
>= 0 && isl_tab_rollback(cgbr
->tab
, snap
) < 0)
3204 static int context_gbr_is_empty(struct isl_context
*context
)
3206 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3209 return cgbr
->tab
->empty
;
3212 struct isl_gbr_tab_undo
{
3213 struct isl_tab_undo
*tab_snap
;
3214 struct isl_tab_undo
*shifted_snap
;
3215 struct isl_tab_undo
*cone_snap
;
3218 static void *context_gbr_save(struct isl_context
*context
)
3220 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3221 struct isl_gbr_tab_undo
*snap
;
3223 snap
= isl_alloc_type(cgbr
->tab
->mat
->ctx
, struct isl_gbr_tab_undo
);
3227 snap
->tab_snap
= isl_tab_snap(cgbr
->tab
);
3228 if (isl_tab_save_samples(cgbr
->tab
) < 0)
3232 snap
->shifted_snap
= isl_tab_snap(cgbr
->shifted
);
3234 snap
->shifted_snap
= NULL
;
3237 snap
->cone_snap
= isl_tab_snap(cgbr
->cone
);
3239 snap
->cone_snap
= NULL
;
3247 static void context_gbr_restore(struct isl_context
*context
, void *save
)
3249 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3250 struct isl_gbr_tab_undo
*snap
= (struct isl_gbr_tab_undo
*)save
;
3253 if (isl_tab_rollback(cgbr
->tab
, snap
->tab_snap
) < 0) {
3254 isl_tab_free(cgbr
->tab
);
3258 if (snap
->shifted_snap
) {
3259 if (isl_tab_rollback(cgbr
->shifted
, snap
->shifted_snap
) < 0)
3261 } else if (cgbr
->shifted
) {
3262 isl_tab_free(cgbr
->shifted
);
3263 cgbr
->shifted
= NULL
;
3266 if (snap
->cone_snap
) {
3267 if (isl_tab_rollback(cgbr
->cone
, snap
->cone_snap
) < 0)
3269 } else if (cgbr
->cone
) {
3270 isl_tab_free(cgbr
->cone
);
3279 isl_tab_free(cgbr
->tab
);
3283 static void context_gbr_discard(void *save
)
3285 struct isl_gbr_tab_undo
*snap
= (struct isl_gbr_tab_undo
*)save
;
3289 static int context_gbr_is_ok(struct isl_context
*context
)
3291 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3295 static void context_gbr_invalidate(struct isl_context
*context
)
3297 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3298 isl_tab_free(cgbr
->tab
);
3302 static void context_gbr_free(struct isl_context
*context
)
3304 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3305 isl_tab_free(cgbr
->tab
);
3306 isl_tab_free(cgbr
->shifted
);
3307 isl_tab_free(cgbr
->cone
);
3311 struct isl_context_op isl_context_gbr_op
= {
3312 context_gbr_detect_nonnegative_parameters
,
3313 context_gbr_peek_basic_set
,
3314 context_gbr_peek_tab
,
3316 context_gbr_add_ineq
,
3317 context_gbr_ineq_sign
,
3318 context_gbr_test_ineq
,
3319 context_gbr_get_div
,
3320 context_gbr_add_div
,
3321 context_gbr_detect_equalities
,
3322 context_gbr_best_split
,
3323 context_gbr_is_empty
,
3326 context_gbr_restore
,
3327 context_gbr_discard
,
3328 context_gbr_invalidate
,
3332 static struct isl_context
*isl_context_gbr_alloc(struct isl_basic_set
*dom
)
3334 struct isl_context_gbr
*cgbr
;
3339 cgbr
= isl_calloc_type(dom
->ctx
, struct isl_context_gbr
);
3343 cgbr
->context
.op
= &isl_context_gbr_op
;
3345 cgbr
->shifted
= NULL
;
3347 cgbr
->tab
= isl_tab_from_basic_set(dom
, 1);
3348 cgbr
->tab
= isl_tab_init_samples(cgbr
->tab
);
3351 check_gbr_integer_feasible(cgbr
);
3353 return &cgbr
->context
;
3355 cgbr
->context
.op
->free(&cgbr
->context
);
3359 static struct isl_context
*isl_context_alloc(struct isl_basic_set
*dom
)
3364 if (dom
->ctx
->opt
->context
== ISL_CONTEXT_LEXMIN
)
3365 return isl_context_lex_alloc(dom
);
3367 return isl_context_gbr_alloc(dom
);
3370 /* Construct an isl_sol_map structure for accumulating the solution.
3371 * If track_empty is set, then we also keep track of the parts
3372 * of the context where there is no solution.
3373 * If max is set, then we are solving a maximization, rather than
3374 * a minimization problem, which means that the variables in the
3375 * tableau have value "M - x" rather than "M + x".
3377 static struct isl_sol
*sol_map_init(struct isl_basic_map
*bmap
,
3378 struct isl_basic_set
*dom
, int track_empty
, int max
)
3380 struct isl_sol_map
*sol_map
= NULL
;
3385 sol_map
= isl_calloc_type(bmap
->ctx
, struct isl_sol_map
);
3389 sol_map
->sol
.rational
= ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
3390 sol_map
->sol
.dec_level
.callback
.run
= &sol_dec_level_wrap
;
3391 sol_map
->sol
.dec_level
.sol
= &sol_map
->sol
;
3392 sol_map
->sol
.max
= max
;
3393 sol_map
->sol
.n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
3394 sol_map
->sol
.add
= &sol_map_add_wrap
;
3395 sol_map
->sol
.add_empty
= track_empty
? &sol_map_add_empty_wrap
: NULL
;
3396 sol_map
->sol
.free
= &sol_map_free_wrap
;
3397 sol_map
->map
= isl_map_alloc_space(isl_basic_map_get_space(bmap
), 1,
3402 sol_map
->sol
.context
= isl_context_alloc(dom
);
3403 if (!sol_map
->sol
.context
)
3407 sol_map
->empty
= isl_set_alloc_space(isl_basic_set_get_space(dom
),
3408 1, ISL_SET_DISJOINT
);
3409 if (!sol_map
->empty
)
3413 isl_basic_set_free(dom
);
3414 return &sol_map
->sol
;
3416 isl_basic_set_free(dom
);
3417 sol_map_free(sol_map
);
3421 /* Check whether all coefficients of (non-parameter) variables
3422 * are non-positive, meaning that no pivots can be performed on the row.
3424 static int is_critical(struct isl_tab
*tab
, int row
)
3427 unsigned off
= 2 + tab
->M
;
3429 for (j
= tab
->n_dead
; j
< tab
->n_col
; ++j
) {
3430 if (tab
->col_var
[j
] >= 0 &&
3431 (tab
->col_var
[j
] < tab
->n_param
||
3432 tab
->col_var
[j
] >= tab
->n_var
- tab
->n_div
))
3435 if (isl_int_is_pos(tab
->mat
->row
[row
][off
+ j
]))
3442 /* Check whether the inequality represented by vec is strict over the integers,
3443 * i.e., there are no integer values satisfying the constraint with
3444 * equality. This happens if the gcd of the coefficients is not a divisor
3445 * of the constant term. If so, scale the constraint down by the gcd
3446 * of the coefficients.
3448 static int is_strict(struct isl_vec
*vec
)
3454 isl_seq_gcd(vec
->el
+ 1, vec
->size
- 1, &gcd
);
3455 if (!isl_int_is_one(gcd
)) {
3456 strict
= !isl_int_is_divisible_by(vec
->el
[0], gcd
);
3457 isl_int_fdiv_q(vec
->el
[0], vec
->el
[0], gcd
);
3458 isl_seq_scale_down(vec
->el
+ 1, vec
->el
+ 1, gcd
, vec
->size
-1);
3465 /* Determine the sign of the given row of the main tableau.
3466 * The result is one of
3467 * isl_tab_row_pos: always non-negative; no pivot needed
3468 * isl_tab_row_neg: always non-positive; pivot
3469 * isl_tab_row_any: can be both positive and negative; split
3471 * We first handle some simple cases
3472 * - the row sign may be known already
3473 * - the row may be obviously non-negative
3474 * - the parametric constant may be equal to that of another row
3475 * for which we know the sign. This sign will be either "pos" or
3476 * "any". If it had been "neg" then we would have pivoted before.
3478 * If none of these cases hold, we check the value of the row for each
3479 * of the currently active samples. Based on the signs of these values
3480 * we make an initial determination of the sign of the row.
3482 * all zero -> unk(nown)
3483 * all non-negative -> pos
3484 * all non-positive -> neg
3485 * both negative and positive -> all
3487 * If we end up with "all", we are done.
3488 * Otherwise, we perform a check for positive and/or negative
3489 * values as follows.
3491 * samples neg unk pos
3497 * There is no special sign for "zero", because we can usually treat zero
3498 * as either non-negative or non-positive, whatever works out best.
3499 * However, if the row is "critical", meaning that pivoting is impossible
3500 * then we don't want to limp zero with the non-positive case, because
3501 * then we we would lose the solution for those values of the parameters
3502 * where the value of the row is zero. Instead, we treat 0 as non-negative
3503 * ensuring a split if the row can attain both zero and negative values.
3504 * The same happens when the original constraint was one that could not
3505 * be satisfied with equality by any integer values of the parameters.
3506 * In this case, we normalize the constraint, but then a value of zero
3507 * for the normalized constraint is actually a positive value for the
3508 * original constraint, so again we need to treat zero as non-negative.
3509 * In both these cases, we have the following decision tree instead:
3511 * all non-negative -> pos
3512 * all negative -> neg
3513 * both negative and non-negative -> all
3521 static enum isl_tab_row_sign
row_sign(struct isl_tab
*tab
,
3522 struct isl_sol
*sol
, int row
)
3524 struct isl_vec
*ineq
= NULL
;
3525 enum isl_tab_row_sign res
= isl_tab_row_unknown
;
3530 if (tab
->row_sign
[row
] != isl_tab_row_unknown
)
3531 return tab
->row_sign
[row
];
3532 if (is_obviously_nonneg(tab
, row
))
3533 return isl_tab_row_pos
;
3534 for (row2
= tab
->n_redundant
; row2
< tab
->n_row
; ++row2
) {
3535 if (tab
->row_sign
[row2
] == isl_tab_row_unknown
)
3537 if (identical_parameter_line(tab
, row
, row2
))
3538 return tab
->row_sign
[row2
];
3541 critical
= is_critical(tab
, row
);
3543 ineq
= get_row_parameter_ineq(tab
, row
);
3547 strict
= is_strict(ineq
);
3549 res
= sol
->context
->op
->ineq_sign(sol
->context
, ineq
->el
,
3550 critical
|| strict
);
3552 if (res
== isl_tab_row_unknown
|| res
== isl_tab_row_pos
) {
3553 /* test for negative values */
3555 isl_seq_neg(ineq
->el
, ineq
->el
, ineq
->size
);
3556 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3558 feasible
= sol
->context
->op
->test_ineq(sol
->context
, ineq
->el
);
3562 res
= isl_tab_row_pos
;
3564 res
= (res
== isl_tab_row_unknown
) ? isl_tab_row_neg
3566 if (res
== isl_tab_row_neg
) {
3567 isl_seq_neg(ineq
->el
, ineq
->el
, ineq
->size
);
3568 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3572 if (res
== isl_tab_row_neg
) {
3573 /* test for positive values */
3575 if (!critical
&& !strict
)
3576 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3578 feasible
= sol
->context
->op
->test_ineq(sol
->context
, ineq
->el
);
3582 res
= isl_tab_row_any
;
3589 return isl_tab_row_unknown
;
3592 static void find_solutions(struct isl_sol
*sol
, struct isl_tab
*tab
);
3594 /* Find solutions for values of the parameters that satisfy the given
3597 * We currently take a snapshot of the context tableau that is reset
3598 * when we return from this function, while we make a copy of the main
3599 * tableau, leaving the original main tableau untouched.
3600 * These are fairly arbitrary choices. Making a copy also of the context
3601 * tableau would obviate the need to undo any changes made to it later,
3602 * while taking a snapshot of the main tableau could reduce memory usage.
3603 * If we were to switch to taking a snapshot of the main tableau,
3604 * we would have to keep in mind that we need to save the row signs
3605 * and that we need to do this before saving the current basis
3606 * such that the basis has been restore before we restore the row signs.
3608 static void find_in_pos(struct isl_sol
*sol
, struct isl_tab
*tab
, isl_int
*ineq
)
3614 saved
= sol
->context
->op
->save(sol
->context
);
3616 tab
= isl_tab_dup(tab
);
3620 sol
->context
->op
->add_ineq(sol
->context
, ineq
, 0, 1);
3622 find_solutions(sol
, tab
);
3625 sol
->context
->op
->restore(sol
->context
, saved
);
3627 sol
->context
->op
->discard(saved
);
3633 /* Record the absence of solutions for those values of the parameters
3634 * that do not satisfy the given inequality with equality.
3636 static void no_sol_in_strict(struct isl_sol
*sol
,
3637 struct isl_tab
*tab
, struct isl_vec
*ineq
)
3642 if (!sol
->context
|| sol
->error
)
3644 saved
= sol
->context
->op
->save(sol
->context
);
3646 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3648 sol
->context
->op
->add_ineq(sol
->context
, ineq
->el
, 1, 0);
3657 isl_int_add_ui(ineq
->el
[0], ineq
->el
[0], 1);
3659 sol
->context
->op
->restore(sol
->context
, saved
);
3665 /* Compute the lexicographic minimum of the set represented by the main
3666 * tableau "tab" within the context "sol->context_tab".
3667 * On entry the sample value of the main tableau is lexicographically
3668 * less than or equal to this lexicographic minimum.
3669 * Pivots are performed until a feasible point is found, which is then
3670 * necessarily equal to the minimum, or until the tableau is found to
3671 * be infeasible. Some pivots may need to be performed for only some
3672 * feasible values of the context tableau. If so, the context tableau
3673 * is split into a part where the pivot is needed and a part where it is not.
3675 * Whenever we enter the main loop, the main tableau is such that no
3676 * "obvious" pivots need to be performed on it, where "obvious" means
3677 * that the given row can be seen to be negative without looking at
3678 * the context tableau. In particular, for non-parametric problems,
3679 * no pivots need to be performed on the main tableau.
3680 * The caller of find_solutions is responsible for making this property
3681 * hold prior to the first iteration of the loop, while restore_lexmin
3682 * is called before every other iteration.
3684 * Inside the main loop, we first examine the signs of the rows of
3685 * the main tableau within the context of the context tableau.
3686 * If we find a row that is always non-positive for all values of
3687 * the parameters satisfying the context tableau and negative for at
3688 * least one value of the parameters, we perform the appropriate pivot
3689 * and start over. An exception is the case where no pivot can be
3690 * performed on the row. In this case, we require that the sign of
3691 * the row is negative for all values of the parameters (rather than just
3692 * non-positive). This special case is handled inside row_sign, which
3693 * will say that the row can have any sign if it determines that it can
3694 * attain both negative and zero values.
3696 * If we can't find a row that always requires a pivot, but we can find
3697 * one or more rows that require a pivot for some values of the parameters
3698 * (i.e., the row can attain both positive and negative signs), then we split
3699 * the context tableau into two parts, one where we force the sign to be
3700 * non-negative and one where we force is to be negative.
3701 * The non-negative part is handled by a recursive call (through find_in_pos).
3702 * Upon returning from this call, we continue with the negative part and
3703 * perform the required pivot.
3705 * If no such rows can be found, all rows are non-negative and we have
3706 * found a (rational) feasible point. If we only wanted a rational point
3708 * Otherwise, we check if all values of the sample point of the tableau
3709 * are integral for the variables. If so, we have found the minimal
3710 * integral point and we are done.
3711 * If the sample point is not integral, then we need to make a distinction
3712 * based on whether the constant term is non-integral or the coefficients
3713 * of the parameters. Furthermore, in order to decide how to handle
3714 * the non-integrality, we also need to know whether the coefficients
3715 * of the other columns in the tableau are integral. This leads
3716 * to the following table. The first two rows do not correspond
3717 * to a non-integral sample point and are only mentioned for completeness.
3719 * constant parameters other
3722 * int int rat | -> no problem
3724 * rat int int -> fail
3726 * rat int rat -> cut
3729 * rat rat rat | -> parametric cut
3732 * rat rat int | -> split context
3734 * If the parametric constant is completely integral, then there is nothing
3735 * to be done. If the constant term is non-integral, but all the other
3736 * coefficient are integral, then there is nothing that can be done
3737 * and the tableau has no integral solution.
3738 * If, on the other hand, one or more of the other columns have rational
3739 * coefficients, but the parameter coefficients are all integral, then
3740 * we can perform a regular (non-parametric) cut.
3741 * Finally, if there is any parameter coefficient that is non-integral,
3742 * then we need to involve the context tableau. There are two cases here.
3743 * If at least one other column has a rational coefficient, then we
3744 * can perform a parametric cut in the main tableau by adding a new
3745 * integer division in the context tableau.
3746 * If all other columns have integral coefficients, then we need to
3747 * enforce that the rational combination of parameters (c + \sum a_i y_i)/m
3748 * is always integral. We do this by introducing an integer division
3749 * q = floor((c + \sum a_i y_i)/m) and stipulating that its argument should
3750 * always be integral in the context tableau, i.e., m q = c + \sum a_i y_i.
3751 * Since q is expressed in the tableau as
3752 * c + \sum a_i y_i - m q >= 0
3753 * -c - \sum a_i y_i + m q + m - 1 >= 0
3754 * it is sufficient to add the inequality
3755 * -c - \sum a_i y_i + m q >= 0
3756 * In the part of the context where this inequality does not hold, the
3757 * main tableau is marked as being empty.
3759 static void find_solutions(struct isl_sol
*sol
, struct isl_tab
*tab
)
3761 struct isl_context
*context
;
3764 if (!tab
|| sol
->error
)
3767 context
= sol
->context
;
3771 if (context
->op
->is_empty(context
))
3774 for (r
= 0; r
>= 0 && tab
&& !tab
->empty
; r
= restore_lexmin(tab
)) {
3777 enum isl_tab_row_sign sgn
;
3781 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
3782 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
3784 sgn
= row_sign(tab
, sol
, row
);
3787 tab
->row_sign
[row
] = sgn
;
3788 if (sgn
== isl_tab_row_any
)
3790 if (sgn
== isl_tab_row_any
&& split
== -1)
3792 if (sgn
== isl_tab_row_neg
)
3795 if (row
< tab
->n_row
)
3798 struct isl_vec
*ineq
;
3800 split
= context
->op
->best_split(context
, tab
);
3803 ineq
= get_row_parameter_ineq(tab
, split
);
3807 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
3808 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
3810 if (tab
->row_sign
[row
] == isl_tab_row_any
)
3811 tab
->row_sign
[row
] = isl_tab_row_unknown
;
3813 tab
->row_sign
[split
] = isl_tab_row_pos
;
3815 find_in_pos(sol
, tab
, ineq
->el
);
3816 tab
->row_sign
[split
] = isl_tab_row_neg
;
3818 isl_seq_neg(ineq
->el
, ineq
->el
, ineq
->size
);
3819 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3821 context
->op
->add_ineq(context
, ineq
->el
, 0, 1);
3829 row
= first_non_integer_row(tab
, &flags
);
3832 if (ISL_FL_ISSET(flags
, I_PAR
)) {
3833 if (ISL_FL_ISSET(flags
, I_VAR
)) {
3834 if (isl_tab_mark_empty(tab
) < 0)
3838 row
= add_cut(tab
, row
);
3839 } else if (ISL_FL_ISSET(flags
, I_VAR
)) {
3840 struct isl_vec
*div
;
3841 struct isl_vec
*ineq
;
3843 div
= get_row_split_div(tab
, row
);
3846 d
= context
->op
->get_div(context
, tab
, div
);
3850 ineq
= ineq_for_div(context
->op
->peek_basic_set(context
), d
);
3854 no_sol_in_strict(sol
, tab
, ineq
);
3855 isl_seq_neg(ineq
->el
, ineq
->el
, ineq
->size
);
3856 context
->op
->add_ineq(context
, ineq
->el
, 1, 1);
3858 if (sol
->error
|| !context
->op
->is_ok(context
))
3860 tab
= set_row_cst_to_div(tab
, row
, d
);
3861 if (context
->op
->is_empty(context
))
3864 row
= add_parametric_cut(tab
, row
, context
);
3879 /* Does "sol" contain a pair of partial solutions that could potentially
3882 * We currently only check that "sol" is not in an error state
3883 * and that there are at least two partial solutions of which the final two
3884 * are defined at the same level.
3886 static int sol_has_mergeable_solutions(struct isl_sol
*sol
)
3892 if (!sol
->partial
->next
)
3894 return sol
->partial
->level
== sol
->partial
->next
->level
;
3897 /* Compute the lexicographic minimum of the set represented by the main
3898 * tableau "tab" within the context "sol->context_tab".
3900 * As a preprocessing step, we first transfer all the purely parametric
3901 * equalities from the main tableau to the context tableau, i.e.,
3902 * parameters that have been pivoted to a row.
3903 * These equalities are ignored by the main algorithm, because the
3904 * corresponding rows may not be marked as being non-negative.
3905 * In parts of the context where the added equality does not hold,
3906 * the main tableau is marked as being empty.
3908 * Before we embark on the actual computation, we save a copy
3909 * of the context. When we return, we check if there are any
3910 * partial solutions that can potentially be merged. If so,
3911 * we perform a rollback to the initial state of the context.
3912 * The merging of partial solutions happens inside calls to
3913 * sol_dec_level that are pushed onto the undo stack of the context.
3914 * If there are no partial solutions that can potentially be merged
3915 * then the rollback is skipped as it would just be wasted effort.
3917 static void find_solutions_main(struct isl_sol
*sol
, struct isl_tab
*tab
)
3927 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
3931 if (tab
->row_var
[row
] < 0)
3933 if (tab
->row_var
[row
] >= tab
->n_param
&&
3934 tab
->row_var
[row
] < tab
->n_var
- tab
->n_div
)
3936 if (tab
->row_var
[row
] < tab
->n_param
)
3937 p
= tab
->row_var
[row
];
3939 p
= tab
->row_var
[row
]
3940 + tab
->n_param
- (tab
->n_var
- tab
->n_div
);
3942 eq
= isl_vec_alloc(tab
->mat
->ctx
, 1+tab
->n_param
+tab
->n_div
);
3945 get_row_parameter_line(tab
, row
, eq
->el
);
3946 isl_int_neg(eq
->el
[1 + p
], tab
->mat
->row
[row
][0]);
3947 eq
= isl_vec_normalize(eq
);
3950 no_sol_in_strict(sol
, tab
, eq
);
3952 isl_seq_neg(eq
->el
, eq
->el
, eq
->size
);
3954 no_sol_in_strict(sol
, tab
, eq
);
3955 isl_seq_neg(eq
->el
, eq
->el
, eq
->size
);
3957 sol
->context
->op
->add_eq(sol
->context
, eq
->el
, 1, 1);
3961 if (isl_tab_mark_redundant(tab
, row
) < 0)
3964 if (sol
->context
->op
->is_empty(sol
->context
))
3967 row
= tab
->n_redundant
- 1;
3970 saved
= sol
->context
->op
->save(sol
->context
);
3972 find_solutions(sol
, tab
);
3974 if (sol_has_mergeable_solutions(sol
))
3975 sol
->context
->op
->restore(sol
->context
, saved
);
3977 sol
->context
->op
->discard(saved
);
3988 /* Check if integer division "div" of "dom" also occurs in "bmap".
3989 * If so, return its position within the divs.
3990 * If not, return -1.
3992 static int find_context_div(struct isl_basic_map
*bmap
,
3993 struct isl_basic_set
*dom
, unsigned div
)
3996 unsigned b_dim
= isl_space_dim(bmap
->dim
, isl_dim_all
);
3997 unsigned d_dim
= isl_space_dim(dom
->dim
, isl_dim_all
);
3999 if (isl_int_is_zero(dom
->div
[div
][0]))
4001 if (isl_seq_first_non_zero(dom
->div
[div
] + 2 + d_dim
, dom
->n_div
) != -1)
4004 for (i
= 0; i
< bmap
->n_div
; ++i
) {
4005 if (isl_int_is_zero(bmap
->div
[i
][0]))
4007 if (isl_seq_first_non_zero(bmap
->div
[i
] + 2 + d_dim
,
4008 (b_dim
- d_dim
) + bmap
->n_div
) != -1)
4010 if (isl_seq_eq(bmap
->div
[i
], dom
->div
[div
], 2 + d_dim
))
4016 /* The correspondence between the variables in the main tableau,
4017 * the context tableau, and the input map and domain is as follows.
4018 * The first n_param and the last n_div variables of the main tableau
4019 * form the variables of the context tableau.
4020 * In the basic map, these n_param variables correspond to the
4021 * parameters and the input dimensions. In the domain, they correspond
4022 * to the parameters and the set dimensions.
4023 * The n_div variables correspond to the integer divisions in the domain.
4024 * To ensure that everything lines up, we may need to copy some of the
4025 * integer divisions of the domain to the map. These have to be placed
4026 * in the same order as those in the context and they have to be placed
4027 * after any other integer divisions that the map may have.
4028 * This function performs the required reordering.
4030 static struct isl_basic_map
*align_context_divs(struct isl_basic_map
*bmap
,
4031 struct isl_basic_set
*dom
)
4037 for (i
= 0; i
< dom
->n_div
; ++i
)
4038 if (find_context_div(bmap
, dom
, i
) != -1)
4040 other
= bmap
->n_div
- common
;
4041 if (dom
->n_div
- common
> 0) {
4042 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
4043 dom
->n_div
- common
, 0, 0);
4047 for (i
= 0; i
< dom
->n_div
; ++i
) {
4048 int pos
= find_context_div(bmap
, dom
, i
);
4050 pos
= isl_basic_map_alloc_div(bmap
);
4053 isl_int_set_si(bmap
->div
[pos
][0], 0);
4055 if (pos
!= other
+ i
)
4056 isl_basic_map_swap_div(bmap
, pos
, other
+ i
);
4060 isl_basic_map_free(bmap
);
4064 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
4065 * some obvious symmetries.
4067 * We make sure the divs in the domain are properly ordered,
4068 * because they will be added one by one in the given order
4069 * during the construction of the solution map.
4071 static struct isl_sol
*basic_map_partial_lexopt_base(
4072 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
4073 __isl_give isl_set
**empty
, int max
,
4074 struct isl_sol
*(*init
)(__isl_keep isl_basic_map
*bmap
,
4075 __isl_take isl_basic_set
*dom
, int track_empty
, int max
))
4077 struct isl_tab
*tab
;
4078 struct isl_sol
*sol
= NULL
;
4079 struct isl_context
*context
;
4082 dom
= isl_basic_set_order_divs(dom
);
4083 bmap
= align_context_divs(bmap
, dom
);
4085 sol
= init(bmap
, dom
, !!empty
, max
);
4089 context
= sol
->context
;
4090 if (isl_basic_set_plain_is_empty(context
->op
->peek_basic_set(context
)))
4092 else if (isl_basic_map_plain_is_empty(bmap
)) {
4095 isl_basic_set_copy(context
->op
->peek_basic_set(context
)));
4097 tab
= tab_for_lexmin(bmap
,
4098 context
->op
->peek_basic_set(context
), 1, max
);
4099 tab
= context
->op
->detect_nonnegative_parameters(context
, tab
);
4100 find_solutions_main(sol
, tab
);
4105 isl_basic_map_free(bmap
);
4109 isl_basic_map_free(bmap
);
4113 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
4114 * some obvious symmetries.
4116 * We call basic_map_partial_lexopt_base and extract the results.
4118 static __isl_give isl_map
*basic_map_partial_lexopt_base_map(
4119 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
4120 __isl_give isl_set
**empty
, int max
)
4122 isl_map
*result
= NULL
;
4123 struct isl_sol
*sol
;
4124 struct isl_sol_map
*sol_map
;
4126 sol
= basic_map_partial_lexopt_base(bmap
, dom
, empty
, max
,
4130 sol_map
= (struct isl_sol_map
*) sol
;
4132 result
= isl_map_copy(sol_map
->map
);
4134 *empty
= isl_set_copy(sol_map
->empty
);
4135 sol_free(&sol_map
->sol
);
4139 /* Structure used during detection of parallel constraints.
4140 * n_in: number of "input" variables: isl_dim_param + isl_dim_in
4141 * n_out: number of "output" variables: isl_dim_out + isl_dim_div
4142 * val: the coefficients of the output variables
4144 struct isl_constraint_equal_info
{
4145 isl_basic_map
*bmap
;
4151 /* Check whether the coefficients of the output variables
4152 * of the constraint in "entry" are equal to info->val.
4154 static int constraint_equal(const void *entry
, const void *val
)
4156 isl_int
**row
= (isl_int
**)entry
;
4157 const struct isl_constraint_equal_info
*info
= val
;
4159 return isl_seq_eq((*row
) + 1 + info
->n_in
, info
->val
, info
->n_out
);
4162 /* Check whether "bmap" has a pair of constraints that have
4163 * the same coefficients for the output variables.
4164 * Note that the coefficients of the existentially quantified
4165 * variables need to be zero since the existentially quantified
4166 * of the result are usually not the same as those of the input.
4167 * the isl_dim_out and isl_dim_div dimensions.
4168 * If so, return 1 and return the row indices of the two constraints
4169 * in *first and *second.
4171 static int parallel_constraints(__isl_keep isl_basic_map
*bmap
,
4172 int *first
, int *second
)
4175 isl_ctx
*ctx
= isl_basic_map_get_ctx(bmap
);
4176 struct isl_hash_table
*table
= NULL
;
4177 struct isl_hash_table_entry
*entry
;
4178 struct isl_constraint_equal_info info
;
4182 ctx
= isl_basic_map_get_ctx(bmap
);
4183 table
= isl_hash_table_alloc(ctx
, bmap
->n_ineq
);
4187 info
.n_in
= isl_basic_map_dim(bmap
, isl_dim_param
) +
4188 isl_basic_map_dim(bmap
, isl_dim_in
);
4190 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4191 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4192 info
.n_out
= n_out
+ n_div
;
4193 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
4196 info
.val
= bmap
->ineq
[i
] + 1 + info
.n_in
;
4197 if (isl_seq_first_non_zero(info
.val
, n_out
) < 0)
4199 if (isl_seq_first_non_zero(info
.val
+ n_out
, n_div
) >= 0)
4201 hash
= isl_seq_get_hash(info
.val
, info
.n_out
);
4202 entry
= isl_hash_table_find(ctx
, table
, hash
,
4203 constraint_equal
, &info
, 1);
4208 entry
->data
= &bmap
->ineq
[i
];
4211 if (i
< bmap
->n_ineq
) {
4212 *first
= ((isl_int
**)entry
->data
) - bmap
->ineq
;
4216 isl_hash_table_free(ctx
, table
);
4218 return i
< bmap
->n_ineq
;
4220 isl_hash_table_free(ctx
, table
);
4224 /* Given a set of upper bounds in "var", add constraints to "bset"
4225 * that make the i-th bound smallest.
4227 * In particular, if there are n bounds b_i, then add the constraints
4229 * b_i <= b_j for j > i
4230 * b_i < b_j for j < i
4232 static __isl_give isl_basic_set
*select_minimum(__isl_take isl_basic_set
*bset
,
4233 __isl_keep isl_mat
*var
, int i
)
4238 ctx
= isl_mat_get_ctx(var
);
4240 for (j
= 0; j
< var
->n_row
; ++j
) {
4243 k
= isl_basic_set_alloc_inequality(bset
);
4246 isl_seq_combine(bset
->ineq
[k
], ctx
->one
, var
->row
[j
],
4247 ctx
->negone
, var
->row
[i
], var
->n_col
);
4248 isl_int_set_si(bset
->ineq
[k
][var
->n_col
], 0);
4250 isl_int_sub_ui(bset
->ineq
[k
][0], bset
->ineq
[k
][0], 1);
4253 bset
= isl_basic_set_finalize(bset
);
4257 isl_basic_set_free(bset
);
4261 /* Given a set of upper bounds on the last "input" variable m,
4262 * construct a set that assigns the minimal upper bound to m, i.e.,
4263 * construct a set that divides the space into cells where one
4264 * of the upper bounds is smaller than all the others and assign
4265 * this upper bound to m.
4267 * In particular, if there are n bounds b_i, then the result
4268 * consists of n basic sets, each one of the form
4271 * b_i <= b_j for j > i
4272 * b_i < b_j for j < i
4274 static __isl_give isl_set
*set_minimum(__isl_take isl_space
*dim
,
4275 __isl_take isl_mat
*var
)
4278 isl_basic_set
*bset
= NULL
;
4280 isl_set
*set
= NULL
;
4285 ctx
= isl_space_get_ctx(dim
);
4286 set
= isl_set_alloc_space(isl_space_copy(dim
),
4287 var
->n_row
, ISL_SET_DISJOINT
);
4289 for (i
= 0; i
< var
->n_row
; ++i
) {
4290 bset
= isl_basic_set_alloc_space(isl_space_copy(dim
), 0,
4292 k
= isl_basic_set_alloc_equality(bset
);
4295 isl_seq_cpy(bset
->eq
[k
], var
->row
[i
], var
->n_col
);
4296 isl_int_set_si(bset
->eq
[k
][var
->n_col
], -1);
4297 bset
= select_minimum(bset
, var
, i
);
4298 set
= isl_set_add_basic_set(set
, bset
);
4301 isl_space_free(dim
);
4305 isl_basic_set_free(bset
);
4307 isl_space_free(dim
);
4312 /* Given that the last input variable of "bmap" represents the minimum
4313 * of the bounds in "cst", check whether we need to split the domain
4314 * based on which bound attains the minimum.
4316 * A split is needed when the minimum appears in an integer division
4317 * or in an equality. Otherwise, it is only needed if it appears in
4318 * an upper bound that is different from the upper bounds on which it
4321 static int need_split_basic_map(__isl_keep isl_basic_map
*bmap
,
4322 __isl_keep isl_mat
*cst
)
4328 pos
= cst
->n_col
- 1;
4329 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4331 for (i
= 0; i
< bmap
->n_div
; ++i
)
4332 if (!isl_int_is_zero(bmap
->div
[i
][2 + pos
]))
4335 for (i
= 0; i
< bmap
->n_eq
; ++i
)
4336 if (!isl_int_is_zero(bmap
->eq
[i
][1 + pos
]))
4339 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
4340 if (isl_int_is_nonneg(bmap
->ineq
[i
][1 + pos
]))
4342 if (!isl_int_is_negone(bmap
->ineq
[i
][1 + pos
]))
4344 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + pos
+ 1,
4345 total
- pos
- 1) >= 0)
4348 for (j
= 0; j
< cst
->n_row
; ++j
)
4349 if (isl_seq_eq(bmap
->ineq
[i
], cst
->row
[j
], cst
->n_col
))
4351 if (j
>= cst
->n_row
)
4358 /* Given that the last set variable of "bset" represents the minimum
4359 * of the bounds in "cst", check whether we need to split the domain
4360 * based on which bound attains the minimum.
4362 * We simply call need_split_basic_map here. This is safe because
4363 * the position of the minimum is computed from "cst" and not
4366 static int need_split_basic_set(__isl_keep isl_basic_set
*bset
,
4367 __isl_keep isl_mat
*cst
)
4369 return need_split_basic_map((isl_basic_map
*)bset
, cst
);
4372 /* Given that the last set variable of "set" represents the minimum
4373 * of the bounds in "cst", check whether we need to split the domain
4374 * based on which bound attains the minimum.
4376 static int need_split_set(__isl_keep isl_set
*set
, __isl_keep isl_mat
*cst
)
4380 for (i
= 0; i
< set
->n
; ++i
)
4381 if (need_split_basic_set(set
->p
[i
], cst
))
4387 /* Given a set of which the last set variable is the minimum
4388 * of the bounds in "cst", split each basic set in the set
4389 * in pieces where one of the bounds is (strictly) smaller than the others.
4390 * This subdivision is given in "min_expr".
4391 * The variable is subsequently projected out.
4393 * We only do the split when it is needed.
4394 * For example if the last input variable m = min(a,b) and the only
4395 * constraints in the given basic set are lower bounds on m,
4396 * i.e., l <= m = min(a,b), then we can simply project out m
4397 * to obtain l <= a and l <= b, without having to split on whether
4398 * m is equal to a or b.
4400 static __isl_give isl_set
*split(__isl_take isl_set
*empty
,
4401 __isl_take isl_set
*min_expr
, __isl_take isl_mat
*cst
)
4408 if (!empty
|| !min_expr
|| !cst
)
4411 n_in
= isl_set_dim(empty
, isl_dim_set
);
4412 dim
= isl_set_get_space(empty
);
4413 dim
= isl_space_drop_dims(dim
, isl_dim_set
, n_in
- 1, 1);
4414 res
= isl_set_empty(dim
);
4416 for (i
= 0; i
< empty
->n
; ++i
) {
4419 set
= isl_set_from_basic_set(isl_basic_set_copy(empty
->p
[i
]));
4420 if (need_split_basic_set(empty
->p
[i
], cst
))
4421 set
= isl_set_intersect(set
, isl_set_copy(min_expr
));
4422 set
= isl_set_remove_dims(set
, isl_dim_set
, n_in
- 1, 1);
4424 res
= isl_set_union_disjoint(res
, set
);
4427 isl_set_free(empty
);
4428 isl_set_free(min_expr
);
4432 isl_set_free(empty
);
4433 isl_set_free(min_expr
);
4438 /* Given a map of which the last input variable is the minimum
4439 * of the bounds in "cst", split each basic set in the set
4440 * in pieces where one of the bounds is (strictly) smaller than the others.
4441 * This subdivision is given in "min_expr".
4442 * The variable is subsequently projected out.
4444 * The implementation is essentially the same as that of "split".
4446 static __isl_give isl_map
*split_domain(__isl_take isl_map
*opt
,
4447 __isl_take isl_set
*min_expr
, __isl_take isl_mat
*cst
)
4454 if (!opt
|| !min_expr
|| !cst
)
4457 n_in
= isl_map_dim(opt
, isl_dim_in
);
4458 dim
= isl_map_get_space(opt
);
4459 dim
= isl_space_drop_dims(dim
, isl_dim_in
, n_in
- 1, 1);
4460 res
= isl_map_empty(dim
);
4462 for (i
= 0; i
< opt
->n
; ++i
) {
4465 map
= isl_map_from_basic_map(isl_basic_map_copy(opt
->p
[i
]));
4466 if (need_split_basic_map(opt
->p
[i
], cst
))
4467 map
= isl_map_intersect_domain(map
,
4468 isl_set_copy(min_expr
));
4469 map
= isl_map_remove_dims(map
, isl_dim_in
, n_in
- 1, 1);
4471 res
= isl_map_union_disjoint(res
, map
);
4475 isl_set_free(min_expr
);
4480 isl_set_free(min_expr
);
4485 static __isl_give isl_map
*basic_map_partial_lexopt(
4486 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
4487 __isl_give isl_set
**empty
, int max
);
4492 isl_pw_multi_aff
*pma
;
4495 /* This function is called from basic_map_partial_lexopt_symm.
4496 * The last variable of "bmap" and "dom" corresponds to the minimum
4497 * of the bounds in "cst". "map_space" is the space of the original
4498 * input relation (of basic_map_partial_lexopt_symm) and "set_space"
4499 * is the space of the original domain.
4501 * We recursively call basic_map_partial_lexopt and then plug in
4502 * the definition of the minimum in the result.
4504 static __isl_give
union isl_lex_res
basic_map_partial_lexopt_symm_map_core(
4505 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
4506 __isl_give isl_set
**empty
, int max
, __isl_take isl_mat
*cst
,
4507 __isl_take isl_space
*map_space
, __isl_take isl_space
*set_space
)
4511 union isl_lex_res res
;
4513 min_expr
= set_minimum(isl_basic_set_get_space(dom
), isl_mat_copy(cst
));
4515 opt
= basic_map_partial_lexopt(bmap
, dom
, empty
, max
);
4518 *empty
= split(*empty
,
4519 isl_set_copy(min_expr
), isl_mat_copy(cst
));
4520 *empty
= isl_set_reset_space(*empty
, set_space
);
4523 opt
= split_domain(opt
, min_expr
, cst
);
4524 opt
= isl_map_reset_space(opt
, map_space
);
4530 /* Given a basic map with at least two parallel constraints (as found
4531 * by the function parallel_constraints), first look for more constraints
4532 * parallel to the two constraint and replace the found list of parallel
4533 * constraints by a single constraint with as "input" part the minimum
4534 * of the input parts of the list of constraints. Then, recursively call
4535 * basic_map_partial_lexopt (possibly finding more parallel constraints)
4536 * and plug in the definition of the minimum in the result.
4538 * More specifically, given a set of constraints
4542 * Replace this set by a single constraint
4546 * with u a new parameter with constraints
4550 * Any solution to the new system is also a solution for the original system
4553 * a x >= -u >= -b_i(p)
4555 * Moreover, m = min_i(b_i(p)) satisfies the constraints on u and can
4556 * therefore be plugged into the solution.
4558 static union isl_lex_res
basic_map_partial_lexopt_symm(
4559 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
4560 __isl_give isl_set
**empty
, int max
, int first
, int second
,
4561 __isl_give
union isl_lex_res (*core
)(__isl_take isl_basic_map
*bmap
,
4562 __isl_take isl_basic_set
*dom
,
4563 __isl_give isl_set
**empty
,
4564 int max
, __isl_take isl_mat
*cst
,
4565 __isl_take isl_space
*map_space
,
4566 __isl_take isl_space
*set_space
))
4570 unsigned n_in
, n_out
, n_div
;
4572 isl_vec
*var
= NULL
;
4573 isl_mat
*cst
= NULL
;
4574 isl_space
*map_space
, *set_space
;
4575 union isl_lex_res res
;
4577 map_space
= isl_basic_map_get_space(bmap
);
4578 set_space
= empty
? isl_basic_set_get_space(dom
) : NULL
;
4580 n_in
= isl_basic_map_dim(bmap
, isl_dim_param
) +
4581 isl_basic_map_dim(bmap
, isl_dim_in
);
4582 n_out
= isl_basic_map_dim(bmap
, isl_dim_all
) - n_in
;
4584 ctx
= isl_basic_map_get_ctx(bmap
);
4585 list
= isl_alloc_array(ctx
, int, bmap
->n_ineq
);
4586 var
= isl_vec_alloc(ctx
, n_out
);
4592 isl_seq_cpy(var
->el
, bmap
->ineq
[first
] + 1 + n_in
, n_out
);
4593 for (i
= second
+ 1, n
= 2; i
< bmap
->n_ineq
; ++i
) {
4594 if (isl_seq_eq(var
->el
, bmap
->ineq
[i
] + 1 + n_in
, n_out
))
4598 cst
= isl_mat_alloc(ctx
, n
, 1 + n_in
);
4602 for (i
= 0; i
< n
; ++i
)
4603 isl_seq_cpy(cst
->row
[i
], bmap
->ineq
[list
[i
]], 1 + n_in
);
4605 bmap
= isl_basic_map_cow(bmap
);
4608 for (i
= n
- 1; i
>= 0; --i
)
4609 if (isl_basic_map_drop_inequality(bmap
, list
[i
]) < 0)
4612 bmap
= isl_basic_map_add(bmap
, isl_dim_in
, 1);
4613 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
4614 k
= isl_basic_map_alloc_inequality(bmap
);
4617 isl_seq_clr(bmap
->ineq
[k
], 1 + n_in
);
4618 isl_int_set_si(bmap
->ineq
[k
][1 + n_in
], 1);
4619 isl_seq_cpy(bmap
->ineq
[k
] + 1 + n_in
+ 1, var
->el
, n_out
);
4620 bmap
= isl_basic_map_finalize(bmap
);
4622 n_div
= isl_basic_set_dim(dom
, isl_dim_div
);
4623 dom
= isl_basic_set_add_dims(dom
, isl_dim_set
, 1);
4624 dom
= isl_basic_set_extend_constraints(dom
, 0, n
);
4625 for (i
= 0; i
< n
; ++i
) {
4626 k
= isl_basic_set_alloc_inequality(dom
);
4629 isl_seq_cpy(dom
->ineq
[k
], cst
->row
[i
], 1 + n_in
);
4630 isl_int_set_si(dom
->ineq
[k
][1 + n_in
], -1);
4631 isl_seq_clr(dom
->ineq
[k
] + 1 + n_in
+ 1, n_div
);
4637 return core(bmap
, dom
, empty
, max
, cst
, map_space
, set_space
);
4639 isl_space_free(map_space
);
4640 isl_space_free(set_space
);
4644 isl_basic_set_free(dom
);
4645 isl_basic_map_free(bmap
);
4650 static __isl_give isl_map
*basic_map_partial_lexopt_symm_map(
4651 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
4652 __isl_give isl_set
**empty
, int max
, int first
, int second
)
4654 return basic_map_partial_lexopt_symm(bmap
, dom
, empty
, max
,
4655 first
, second
, &basic_map_partial_lexopt_symm_map_core
).map
;
4658 /* Recursive part of isl_tab_basic_map_partial_lexopt, after detecting
4659 * equalities and removing redundant constraints.
4661 * We first check if there are any parallel constraints (left).
4662 * If not, we are in the base case.
4663 * If there are parallel constraints, we replace them by a single
4664 * constraint in basic_map_partial_lexopt_symm and then call
4665 * this function recursively to look for more parallel constraints.
4667 static __isl_give isl_map
*basic_map_partial_lexopt(
4668 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
4669 __isl_give isl_set
**empty
, int max
)
4677 if (bmap
->ctx
->opt
->pip_symmetry
)
4678 par
= parallel_constraints(bmap
, &first
, &second
);
4682 return basic_map_partial_lexopt_base_map(bmap
, dom
, empty
, max
);
4684 return basic_map_partial_lexopt_symm_map(bmap
, dom
, empty
, max
,
4687 isl_basic_set_free(dom
);
4688 isl_basic_map_free(bmap
);
4692 /* Compute the lexicographic minimum (or maximum if "max" is set)
4693 * of "bmap" over the domain "dom" and return the result as a map.
4694 * If "empty" is not NULL, then *empty is assigned a set that
4695 * contains those parts of the domain where there is no solution.
4696 * If "bmap" is marked as rational (ISL_BASIC_MAP_RATIONAL),
4697 * then we compute the rational optimum. Otherwise, we compute
4698 * the integral optimum.
4700 * We perform some preprocessing. As the PILP solver does not
4701 * handle implicit equalities very well, we first make sure all
4702 * the equalities are explicitly available.
4704 * We also add context constraints to the basic map and remove
4705 * redundant constraints. This is only needed because of the
4706 * way we handle simple symmetries. In particular, we currently look
4707 * for symmetries on the constraints, before we set up the main tableau.
4708 * It is then no good to look for symmetries on possibly redundant constraints.
4710 struct isl_map
*isl_tab_basic_map_partial_lexopt(
4711 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
4712 struct isl_set
**empty
, int max
)
4719 isl_assert(bmap
->ctx
,
4720 isl_basic_map_compatible_domain(bmap
, dom
), goto error
);
4722 if (isl_basic_set_dim(dom
, isl_dim_all
) == 0)
4723 return basic_map_partial_lexopt(bmap
, dom
, empty
, max
);
4725 bmap
= isl_basic_map_intersect_domain(bmap
, isl_basic_set_copy(dom
));
4726 bmap
= isl_basic_map_detect_equalities(bmap
);
4727 bmap
= isl_basic_map_remove_redundancies(bmap
);
4729 return basic_map_partial_lexopt(bmap
, dom
, empty
, max
);
4731 isl_basic_set_free(dom
);
4732 isl_basic_map_free(bmap
);
4736 struct isl_sol_for
{
4738 int (*fn
)(__isl_take isl_basic_set
*dom
,
4739 __isl_take isl_aff_list
*list
, void *user
);
4743 static void sol_for_free(struct isl_sol_for
*sol_for
)
4745 if (sol_for
->sol
.context
)
4746 sol_for
->sol
.context
->op
->free(sol_for
->sol
.context
);
4750 static void sol_for_free_wrap(struct isl_sol
*sol
)
4752 sol_for_free((struct isl_sol_for
*)sol
);
4755 /* Add the solution identified by the tableau and the context tableau.
4757 * See documentation of sol_add for more details.
4759 * Instead of constructing a basic map, this function calls a user
4760 * defined function with the current context as a basic set and
4761 * a list of affine expressions representing the relation between
4762 * the input and output. The space over which the affine expressions
4763 * are defined is the same as that of the domain. The number of
4764 * affine expressions in the list is equal to the number of output variables.
4766 static void sol_for_add(struct isl_sol_for
*sol
,
4767 struct isl_basic_set
*dom
, struct isl_mat
*M
)
4771 isl_local_space
*ls
;
4775 if (sol
->sol
.error
|| !dom
|| !M
)
4778 ctx
= isl_basic_set_get_ctx(dom
);
4779 ls
= isl_basic_set_get_local_space(dom
);
4780 list
= isl_aff_list_alloc(ctx
, M
->n_row
- 1);
4781 for (i
= 1; i
< M
->n_row
; ++i
) {
4782 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
4784 isl_int_set(aff
->v
->el
[0], M
->row
[0][0]);
4785 isl_seq_cpy(aff
->v
->el
+ 1, M
->row
[i
], M
->n_col
);
4787 aff
= isl_aff_normalize(aff
);
4788 list
= isl_aff_list_add(list
, aff
);
4790 isl_local_space_free(ls
);
4792 dom
= isl_basic_set_finalize(dom
);
4794 if (sol
->fn(isl_basic_set_copy(dom
), list
, sol
->user
) < 0)
4797 isl_basic_set_free(dom
);
4801 isl_basic_set_free(dom
);
4806 static void sol_for_add_wrap(struct isl_sol
*sol
,
4807 struct isl_basic_set
*dom
, struct isl_mat
*M
)
4809 sol_for_add((struct isl_sol_for
*)sol
, dom
, M
);
4812 static struct isl_sol_for
*sol_for_init(struct isl_basic_map
*bmap
, int max
,
4813 int (*fn
)(__isl_take isl_basic_set
*dom
, __isl_take isl_aff_list
*list
,
4817 struct isl_sol_for
*sol_for
= NULL
;
4819 struct isl_basic_set
*dom
= NULL
;
4821 sol_for
= isl_calloc_type(bmap
->ctx
, struct isl_sol_for
);
4825 dom_dim
= isl_space_domain(isl_space_copy(bmap
->dim
));
4826 dom
= isl_basic_set_universe(dom_dim
);
4828 sol_for
->sol
.rational
= ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
4829 sol_for
->sol
.dec_level
.callback
.run
= &sol_dec_level_wrap
;
4830 sol_for
->sol
.dec_level
.sol
= &sol_for
->sol
;
4832 sol_for
->user
= user
;
4833 sol_for
->sol
.max
= max
;
4834 sol_for
->sol
.n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4835 sol_for
->sol
.add
= &sol_for_add_wrap
;
4836 sol_for
->sol
.add_empty
= NULL
;
4837 sol_for
->sol
.free
= &sol_for_free_wrap
;
4839 sol_for
->sol
.context
= isl_context_alloc(dom
);
4840 if (!sol_for
->sol
.context
)
4843 isl_basic_set_free(dom
);
4846 isl_basic_set_free(dom
);
4847 sol_for_free(sol_for
);
4851 static void sol_for_find_solutions(struct isl_sol_for
*sol_for
,
4852 struct isl_tab
*tab
)
4854 find_solutions_main(&sol_for
->sol
, tab
);
4857 int isl_basic_map_foreach_lexopt(__isl_keep isl_basic_map
*bmap
, int max
,
4858 int (*fn
)(__isl_take isl_basic_set
*dom
, __isl_take isl_aff_list
*list
,
4862 struct isl_sol_for
*sol_for
= NULL
;
4864 bmap
= isl_basic_map_copy(bmap
);
4865 bmap
= isl_basic_map_detect_equalities(bmap
);
4869 sol_for
= sol_for_init(bmap
, max
, fn
, user
);
4873 if (isl_basic_map_plain_is_empty(bmap
))
4876 struct isl_tab
*tab
;
4877 struct isl_context
*context
= sol_for
->sol
.context
;
4878 tab
= tab_for_lexmin(bmap
,
4879 context
->op
->peek_basic_set(context
), 1, max
);
4880 tab
= context
->op
->detect_nonnegative_parameters(context
, tab
);
4881 sol_for_find_solutions(sol_for
, tab
);
4882 if (sol_for
->sol
.error
)
4886 sol_free(&sol_for
->sol
);
4887 isl_basic_map_free(bmap
);
4890 sol_free(&sol_for
->sol
);
4891 isl_basic_map_free(bmap
);
4895 int isl_basic_set_foreach_lexopt(__isl_keep isl_basic_set
*bset
, int max
,
4896 int (*fn
)(__isl_take isl_basic_set
*dom
, __isl_take isl_aff_list
*list
,
4900 return isl_basic_map_foreach_lexopt(bset
, max
, fn
, user
);
4903 /* Check if the given sequence of len variables starting at pos
4904 * represents a trivial (i.e., zero) solution.
4905 * The variables are assumed to be non-negative and to come in pairs,
4906 * with each pair representing a variable of unrestricted sign.
4907 * The solution is trivial if each such pair in the sequence consists
4908 * of two identical values, meaning that the variable being represented
4911 static int region_is_trivial(struct isl_tab
*tab
, int pos
, int len
)
4918 for (i
= 0; i
< len
; i
+= 2) {
4922 neg_row
= tab
->var
[pos
+ i
].is_row
?
4923 tab
->var
[pos
+ i
].index
: -1;
4924 pos_row
= tab
->var
[pos
+ i
+ 1].is_row
?
4925 tab
->var
[pos
+ i
+ 1].index
: -1;
4928 isl_int_is_zero(tab
->mat
->row
[neg_row
][1])) &&
4930 isl_int_is_zero(tab
->mat
->row
[pos_row
][1])))
4933 if (neg_row
< 0 || pos_row
< 0)
4935 if (isl_int_ne(tab
->mat
->row
[neg_row
][1],
4936 tab
->mat
->row
[pos_row
][1]))
4943 /* Return the index of the first trivial region or -1 if all regions
4946 static int first_trivial_region(struct isl_tab
*tab
,
4947 int n_region
, struct isl_region
*region
)
4951 for (i
= 0; i
< n_region
; ++i
) {
4952 if (region_is_trivial(tab
, region
[i
].pos
, region
[i
].len
))
4959 /* Check if the solution is optimal, i.e., whether the first
4960 * n_op entries are zero.
4962 static int is_optimal(__isl_keep isl_vec
*sol
, int n_op
)
4966 for (i
= 0; i
< n_op
; ++i
)
4967 if (!isl_int_is_zero(sol
->el
[1 + i
]))
4972 /* Add constraints to "tab" that ensure that any solution is significantly
4973 * better that that represented by "sol". That is, find the first
4974 * relevant (within first n_op) non-zero coefficient and force it (along
4975 * with all previous coefficients) to be zero.
4976 * If the solution is already optimal (all relevant coefficients are zero),
4977 * then just mark the table as empty.
4979 static int force_better_solution(struct isl_tab
*tab
,
4980 __isl_keep isl_vec
*sol
, int n_op
)
4989 for (i
= 0; i
< n_op
; ++i
)
4990 if (!isl_int_is_zero(sol
->el
[1 + i
]))
4994 if (isl_tab_mark_empty(tab
) < 0)
4999 ctx
= isl_vec_get_ctx(sol
);
5000 v
= isl_vec_alloc(ctx
, 1 + tab
->n_var
);
5004 for (; i
>= 0; --i
) {
5006 isl_int_set_si(v
->el
[1 + i
], -1);
5007 if (add_lexmin_eq(tab
, v
->el
) < 0)
5018 struct isl_trivial
{
5022 struct isl_tab_undo
*snap
;
5025 /* Return the lexicographically smallest non-trivial solution of the
5026 * given ILP problem.
5028 * All variables are assumed to be non-negative.
5030 * n_op is the number of initial coordinates to optimize.
5031 * That is, once a solution has been found, we will only continue looking
5032 * for solution that result in significantly better values for those
5033 * initial coordinates. That is, we only continue looking for solutions
5034 * that increase the number of initial zeros in this sequence.
5036 * A solution is non-trivial, if it is non-trivial on each of the
5037 * specified regions. Each region represents a sequence of pairs
5038 * of variables. A solution is non-trivial on such a region if
5039 * at least one of these pairs consists of different values, i.e.,
5040 * such that the non-negative variable represented by the pair is non-zero.
5042 * Whenever a conflict is encountered, all constraints involved are
5043 * reported to the caller through a call to "conflict".
5045 * We perform a simple branch-and-bound backtracking search.
5046 * Each level in the search represents initially trivial region that is forced
5047 * to be non-trivial.
5048 * At each level we consider n cases, where n is the length of the region.
5049 * In terms of the n/2 variables of unrestricted signs being encoded by
5050 * the region, we consider the cases
5053 * x_0 = 0 and x_1 >= 1
5054 * x_0 = 0 and x_1 <= -1
5055 * x_0 = 0 and x_1 = 0 and x_2 >= 1
5056 * x_0 = 0 and x_1 = 0 and x_2 <= -1
5058 * The cases are considered in this order, assuming that each pair
5059 * x_i_a x_i_b represents the value x_i_b - x_i_a.
5060 * That is, x_0 >= 1 is enforced by adding the constraint
5061 * x_0_b - x_0_a >= 1
5063 __isl_give isl_vec
*isl_tab_basic_set_non_trivial_lexmin(
5064 __isl_take isl_basic_set
*bset
, int n_op
, int n_region
,
5065 struct isl_region
*region
,
5066 int (*conflict
)(int con
, void *user
), void *user
)
5072 isl_vec
*sol
= NULL
;
5073 struct isl_tab
*tab
;
5074 struct isl_trivial
*triv
= NULL
;
5080 ctx
= isl_basic_set_get_ctx(bset
);
5081 sol
= isl_vec_alloc(ctx
, 0);
5083 tab
= tab_for_lexmin(bset
, NULL
, 0, 0);
5086 tab
->conflict
= conflict
;
5087 tab
->conflict_user
= user
;
5089 v
= isl_vec_alloc(ctx
, 1 + tab
->n_var
);
5090 triv
= isl_calloc_array(ctx
, struct isl_trivial
, n_region
);
5097 while (level
>= 0) {
5101 tab
= cut_to_integer_lexmin(tab
, CUT_ONE
);
5106 r
= first_trivial_region(tab
, n_region
, region
);
5108 for (i
= 0; i
< level
; ++i
)
5111 sol
= isl_tab_get_sample_value(tab
);
5114 if (is_optimal(sol
, n_op
))
5118 if (level
>= n_region
)
5119 isl_die(ctx
, isl_error_internal
,
5120 "nesting level too deep", goto error
);
5121 if (isl_tab_extend_cons(tab
,
5122 2 * region
[r
].len
+ 2 * n_op
) < 0)
5124 triv
[level
].region
= r
;
5125 triv
[level
].side
= 0;
5128 r
= triv
[level
].region
;
5129 side
= triv
[level
].side
;
5130 base
= 2 * (side
/2);
5132 if (side
>= region
[r
].len
) {
5137 if (isl_tab_rollback(tab
, triv
[level
].snap
) < 0)
5142 if (triv
[level
].update
) {
5143 if (force_better_solution(tab
, sol
, n_op
) < 0)
5145 triv
[level
].update
= 0;
5148 if (side
== base
&& base
>= 2) {
5149 for (j
= base
- 2; j
< base
; ++j
) {
5151 isl_int_set_si(v
->el
[1 + region
[r
].pos
+ j
], 1);
5152 if (add_lexmin_eq(tab
, v
->el
) < 0)
5157 triv
[level
].snap
= isl_tab_snap(tab
);
5158 if (isl_tab_push_basis(tab
) < 0)
5162 isl_int_set_si(v
->el
[0], -1);
5163 isl_int_set_si(v
->el
[1 + region
[r
].pos
+ side
], -1);
5164 isl_int_set_si(v
->el
[1 + region
[r
].pos
+ (side
^ 1)], 1);
5165 tab
= add_lexmin_ineq(tab
, v
->el
);
5175 isl_basic_set_free(bset
);
5182 isl_basic_set_free(bset
);
5187 /* Return the lexicographically smallest rational point in "bset",
5188 * assuming that all variables are non-negative.
5189 * If "bset" is empty, then return a zero-length vector.
5191 __isl_give isl_vec
*isl_tab_basic_set_non_neg_lexmin(
5192 __isl_take isl_basic_set
*bset
)
5194 struct isl_tab
*tab
;
5195 isl_ctx
*ctx
= isl_basic_set_get_ctx(bset
);
5201 tab
= tab_for_lexmin(bset
, NULL
, 0, 0);
5205 sol
= isl_vec_alloc(ctx
, 0);
5207 sol
= isl_tab_get_sample_value(tab
);
5209 isl_basic_set_free(bset
);
5213 isl_basic_set_free(bset
);
5217 struct isl_sol_pma
{
5219 isl_pw_multi_aff
*pma
;
5223 static void sol_pma_free(struct isl_sol_pma
*sol_pma
)
5227 if (sol_pma
->sol
.context
)
5228 sol_pma
->sol
.context
->op
->free(sol_pma
->sol
.context
);
5229 isl_pw_multi_aff_free(sol_pma
->pma
);
5230 isl_set_free(sol_pma
->empty
);
5234 /* This function is called for parts of the context where there is
5235 * no solution, with "bset" corresponding to the context tableau.
5236 * Simply add the basic set to the set "empty".
5238 static void sol_pma_add_empty(struct isl_sol_pma
*sol
,
5239 __isl_take isl_basic_set
*bset
)
5243 isl_assert(bset
->ctx
, sol
->empty
, goto error
);
5245 sol
->empty
= isl_set_grow(sol
->empty
, 1);
5246 bset
= isl_basic_set_simplify(bset
);
5247 bset
= isl_basic_set_finalize(bset
);
5248 sol
->empty
= isl_set_add_basic_set(sol
->empty
, bset
);
5253 isl_basic_set_free(bset
);
5257 /* Given a basic map "dom" that represents the context and an affine
5258 * matrix "M" that maps the dimensions of the context to the
5259 * output variables, construct an isl_pw_multi_aff with a single
5260 * cell corresponding to "dom" and affine expressions copied from "M".
5262 static void sol_pma_add(struct isl_sol_pma
*sol
,
5263 __isl_take isl_basic_set
*dom
, __isl_take isl_mat
*M
)
5266 isl_local_space
*ls
;
5268 isl_multi_aff
*maff
;
5269 isl_pw_multi_aff
*pma
;
5271 maff
= isl_multi_aff_alloc(isl_pw_multi_aff_get_space(sol
->pma
));
5272 ls
= isl_basic_set_get_local_space(dom
);
5273 for (i
= 1; i
< M
->n_row
; ++i
) {
5274 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
5276 isl_int_set(aff
->v
->el
[0], M
->row
[0][0]);
5277 isl_seq_cpy(aff
->v
->el
+ 1, M
->row
[i
], M
->n_col
);
5279 aff
= isl_aff_normalize(aff
);
5280 maff
= isl_multi_aff_set_aff(maff
, i
- 1, aff
);
5282 isl_local_space_free(ls
);
5284 dom
= isl_basic_set_simplify(dom
);
5285 dom
= isl_basic_set_finalize(dom
);
5286 pma
= isl_pw_multi_aff_alloc(isl_set_from_basic_set(dom
), maff
);
5287 sol
->pma
= isl_pw_multi_aff_add_disjoint(sol
->pma
, pma
);
5292 static void sol_pma_free_wrap(struct isl_sol
*sol
)
5294 sol_pma_free((struct isl_sol_pma
*)sol
);
5297 static void sol_pma_add_empty_wrap(struct isl_sol
*sol
,
5298 __isl_take isl_basic_set
*bset
)
5300 sol_pma_add_empty((struct isl_sol_pma
*)sol
, bset
);
5303 static void sol_pma_add_wrap(struct isl_sol
*sol
,
5304 __isl_take isl_basic_set
*dom
, __isl_take isl_mat
*M
)
5306 sol_pma_add((struct isl_sol_pma
*)sol
, dom
, M
);
5309 /* Construct an isl_sol_pma structure for accumulating the solution.
5310 * If track_empty is set, then we also keep track of the parts
5311 * of the context where there is no solution.
5312 * If max is set, then we are solving a maximization, rather than
5313 * a minimization problem, which means that the variables in the
5314 * tableau have value "M - x" rather than "M + x".
5316 static struct isl_sol
*sol_pma_init(__isl_keep isl_basic_map
*bmap
,
5317 __isl_take isl_basic_set
*dom
, int track_empty
, int max
)
5319 struct isl_sol_pma
*sol_pma
= NULL
;
5324 sol_pma
= isl_calloc_type(bmap
->ctx
, struct isl_sol_pma
);
5328 sol_pma
->sol
.rational
= ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
5329 sol_pma
->sol
.dec_level
.callback
.run
= &sol_dec_level_wrap
;
5330 sol_pma
->sol
.dec_level
.sol
= &sol_pma
->sol
;
5331 sol_pma
->sol
.max
= max
;
5332 sol_pma
->sol
.n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5333 sol_pma
->sol
.add
= &sol_pma_add_wrap
;
5334 sol_pma
->sol
.add_empty
= track_empty
? &sol_pma_add_empty_wrap
: NULL
;
5335 sol_pma
->sol
.free
= &sol_pma_free_wrap
;
5336 sol_pma
->pma
= isl_pw_multi_aff_empty(isl_basic_map_get_space(bmap
));
5340 sol_pma
->sol
.context
= isl_context_alloc(dom
);
5341 if (!sol_pma
->sol
.context
)
5345 sol_pma
->empty
= isl_set_alloc_space(isl_basic_set_get_space(dom
),
5346 1, ISL_SET_DISJOINT
);
5347 if (!sol_pma
->empty
)
5351 isl_basic_set_free(dom
);
5352 return &sol_pma
->sol
;
5354 isl_basic_set_free(dom
);
5355 sol_pma_free(sol_pma
);
5359 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
5360 * some obvious symmetries.
5362 * We call basic_map_partial_lexopt_base and extract the results.
5364 static __isl_give isl_pw_multi_aff
*basic_map_partial_lexopt_base_pma(
5365 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5366 __isl_give isl_set
**empty
, int max
)
5368 isl_pw_multi_aff
*result
= NULL
;
5369 struct isl_sol
*sol
;
5370 struct isl_sol_pma
*sol_pma
;
5372 sol
= basic_map_partial_lexopt_base(bmap
, dom
, empty
, max
,
5376 sol_pma
= (struct isl_sol_pma
*) sol
;
5378 result
= isl_pw_multi_aff_copy(sol_pma
->pma
);
5380 *empty
= isl_set_copy(sol_pma
->empty
);
5381 sol_free(&sol_pma
->sol
);
5385 /* Given that the last input variable of "maff" represents the minimum
5386 * of some bounds, check whether we need to plug in the expression
5389 * In particular, check if the last input variable appears in any
5390 * of the expressions in "maff".
5392 static int need_substitution(__isl_keep isl_multi_aff
*maff
)
5397 pos
= isl_multi_aff_dim(maff
, isl_dim_in
) - 1;
5399 for (i
= 0; i
< maff
->n
; ++i
)
5400 if (isl_aff_involves_dims(maff
->p
[i
], isl_dim_in
, pos
, 1))
5406 /* Given a set of upper bounds on the last "input" variable m,
5407 * construct a piecewise affine expression that selects
5408 * the minimal upper bound to m, i.e.,
5409 * divide the space into cells where one
5410 * of the upper bounds is smaller than all the others and select
5411 * this upper bound on that cell.
5413 * In particular, if there are n bounds b_i, then the result
5414 * consists of n cell, each one of the form
5416 * b_i <= b_j for j > i
5417 * b_i < b_j for j < i
5419 * The affine expression on this cell is
5423 static __isl_give isl_pw_aff
*set_minimum_pa(__isl_take isl_space
*space
,
5424 __isl_take isl_mat
*var
)
5427 isl_aff
*aff
= NULL
;
5428 isl_basic_set
*bset
= NULL
;
5430 isl_pw_aff
*paff
= NULL
;
5431 isl_space
*pw_space
;
5432 isl_local_space
*ls
= NULL
;
5437 ctx
= isl_space_get_ctx(space
);
5438 ls
= isl_local_space_from_space(isl_space_copy(space
));
5439 pw_space
= isl_space_copy(space
);
5440 pw_space
= isl_space_from_domain(pw_space
);
5441 pw_space
= isl_space_add_dims(pw_space
, isl_dim_out
, 1);
5442 paff
= isl_pw_aff_alloc_size(pw_space
, var
->n_row
);
5444 for (i
= 0; i
< var
->n_row
; ++i
) {
5447 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
5448 bset
= isl_basic_set_alloc_space(isl_space_copy(space
), 0,
5452 isl_int_set_si(aff
->v
->el
[0], 1);
5453 isl_seq_cpy(aff
->v
->el
+ 1, var
->row
[i
], var
->n_col
);
5454 isl_int_set_si(aff
->v
->el
[1 + var
->n_col
], 0);
5455 bset
= select_minimum(bset
, var
, i
);
5456 paff_i
= isl_pw_aff_alloc(isl_set_from_basic_set(bset
), aff
);
5457 paff
= isl_pw_aff_add_disjoint(paff
, paff_i
);
5460 isl_local_space_free(ls
);
5461 isl_space_free(space
);
5466 isl_basic_set_free(bset
);
5467 isl_pw_aff_free(paff
);
5468 isl_local_space_free(ls
);
5469 isl_space_free(space
);
5474 /* Given a piecewise multi-affine expression of which the last input variable
5475 * is the minimum of the bounds in "cst", plug in the value of the minimum.
5476 * This minimum expression is given in "min_expr_pa".
5477 * The set "min_expr" contains the same information, but in the form of a set.
5478 * The variable is subsequently projected out.
5480 * The implementation is similar to those of "split" and "split_domain".
5481 * If the variable appears in a given expression, then minimum expression
5482 * is plugged in. Otherwise, if the variable appears in the constraints
5483 * and a split is required, then the domain is split. Otherwise, no split
5486 static __isl_give isl_pw_multi_aff
*split_domain_pma(
5487 __isl_take isl_pw_multi_aff
*opt
, __isl_take isl_pw_aff
*min_expr_pa
,
5488 __isl_take isl_set
*min_expr
, __isl_take isl_mat
*cst
)
5493 isl_pw_multi_aff
*res
;
5495 if (!opt
|| !min_expr
|| !cst
)
5498 n_in
= isl_pw_multi_aff_dim(opt
, isl_dim_in
);
5499 space
= isl_pw_multi_aff_get_space(opt
);
5500 space
= isl_space_drop_dims(space
, isl_dim_in
, n_in
- 1, 1);
5501 res
= isl_pw_multi_aff_empty(space
);
5503 for (i
= 0; i
< opt
->n
; ++i
) {
5504 isl_pw_multi_aff
*pma
;
5506 pma
= isl_pw_multi_aff_alloc(isl_set_copy(opt
->p
[i
].set
),
5507 isl_multi_aff_copy(opt
->p
[i
].maff
));
5508 if (need_substitution(opt
->p
[i
].maff
))
5509 pma
= isl_pw_multi_aff_substitute(pma
,
5510 isl_dim_in
, n_in
- 1, min_expr_pa
);
5511 else if (need_split_set(opt
->p
[i
].set
, cst
))
5512 pma
= isl_pw_multi_aff_intersect_domain(pma
,
5513 isl_set_copy(min_expr
));
5514 pma
= isl_pw_multi_aff_project_out(pma
,
5515 isl_dim_in
, n_in
- 1, 1);
5517 res
= isl_pw_multi_aff_add_disjoint(res
, pma
);
5520 isl_pw_multi_aff_free(opt
);
5521 isl_pw_aff_free(min_expr_pa
);
5522 isl_set_free(min_expr
);
5526 isl_pw_multi_aff_free(opt
);
5527 isl_pw_aff_free(min_expr_pa
);
5528 isl_set_free(min_expr
);
5533 static __isl_give isl_pw_multi_aff
*basic_map_partial_lexopt_pma(
5534 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5535 __isl_give isl_set
**empty
, int max
);
5537 /* This function is called from basic_map_partial_lexopt_symm.
5538 * The last variable of "bmap" and "dom" corresponds to the minimum
5539 * of the bounds in "cst". "map_space" is the space of the original
5540 * input relation (of basic_map_partial_lexopt_symm) and "set_space"
5541 * is the space of the original domain.
5543 * We recursively call basic_map_partial_lexopt and then plug in
5544 * the definition of the minimum in the result.
5546 static __isl_give
union isl_lex_res
basic_map_partial_lexopt_symm_pma_core(
5547 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5548 __isl_give isl_set
**empty
, int max
, __isl_take isl_mat
*cst
,
5549 __isl_take isl_space
*map_space
, __isl_take isl_space
*set_space
)
5551 isl_pw_multi_aff
*opt
;
5552 isl_pw_aff
*min_expr_pa
;
5554 union isl_lex_res res
;
5556 min_expr
= set_minimum(isl_basic_set_get_space(dom
), isl_mat_copy(cst
));
5557 min_expr_pa
= set_minimum_pa(isl_basic_set_get_space(dom
),
5560 opt
= basic_map_partial_lexopt_pma(bmap
, dom
, empty
, max
);
5563 *empty
= split(*empty
,
5564 isl_set_copy(min_expr
), isl_mat_copy(cst
));
5565 *empty
= isl_set_reset_space(*empty
, set_space
);
5568 opt
= split_domain_pma(opt
, min_expr_pa
, min_expr
, cst
);
5569 opt
= isl_pw_multi_aff_reset_space(opt
, map_space
);
5575 static __isl_give isl_pw_multi_aff
*basic_map_partial_lexopt_symm_pma(
5576 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5577 __isl_give isl_set
**empty
, int max
, int first
, int second
)
5579 return basic_map_partial_lexopt_symm(bmap
, dom
, empty
, max
,
5580 first
, second
, &basic_map_partial_lexopt_symm_pma_core
).pma
;
5583 /* Recursive part of isl_basic_map_partial_lexopt_pw_multi_aff, after detecting
5584 * equalities and removing redundant constraints.
5586 * We first check if there are any parallel constraints (left).
5587 * If not, we are in the base case.
5588 * If there are parallel constraints, we replace them by a single
5589 * constraint in basic_map_partial_lexopt_symm_pma and then call
5590 * this function recursively to look for more parallel constraints.
5592 static __isl_give isl_pw_multi_aff
*basic_map_partial_lexopt_pma(
5593 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5594 __isl_give isl_set
**empty
, int max
)
5602 if (bmap
->ctx
->opt
->pip_symmetry
)
5603 par
= parallel_constraints(bmap
, &first
, &second
);
5607 return basic_map_partial_lexopt_base_pma(bmap
, dom
, empty
, max
);
5609 return basic_map_partial_lexopt_symm_pma(bmap
, dom
, empty
, max
,
5612 isl_basic_set_free(dom
);
5613 isl_basic_map_free(bmap
);
5617 /* Compute the lexicographic minimum (or maximum if "max" is set)
5618 * of "bmap" over the domain "dom" and return the result as a piecewise
5619 * multi-affine expression.
5620 * If "empty" is not NULL, then *empty is assigned a set that
5621 * contains those parts of the domain where there is no solution.
5622 * If "bmap" is marked as rational (ISL_BASIC_MAP_RATIONAL),
5623 * then we compute the rational optimum. Otherwise, we compute
5624 * the integral optimum.
5626 * We perform some preprocessing. As the PILP solver does not
5627 * handle implicit equalities very well, we first make sure all
5628 * the equalities are explicitly available.
5630 * We also add context constraints to the basic map and remove
5631 * redundant constraints. This is only needed because of the
5632 * way we handle simple symmetries. In particular, we currently look
5633 * for symmetries on the constraints, before we set up the main tableau.
5634 * It is then no good to look for symmetries on possibly redundant constraints.
5636 __isl_give isl_pw_multi_aff
*isl_basic_map_partial_lexopt_pw_multi_aff(
5637 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5638 __isl_give isl_set
**empty
, int max
)
5645 isl_assert(bmap
->ctx
,
5646 isl_basic_map_compatible_domain(bmap
, dom
), goto error
);
5648 if (isl_basic_set_dim(dom
, isl_dim_all
) == 0)
5649 return basic_map_partial_lexopt_pma(bmap
, dom
, empty
, max
);
5651 bmap
= isl_basic_map_intersect_domain(bmap
, isl_basic_set_copy(dom
));
5652 bmap
= isl_basic_map_detect_equalities(bmap
);
5653 bmap
= isl_basic_map_remove_redundancies(bmap
);
5655 return basic_map_partial_lexopt_pma(bmap
, dom
, empty
, max
);
5657 isl_basic_set_free(dom
);
5658 isl_basic_map_free(bmap
);