2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2016 Sven Verdoolaege
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, K.U.Leuven, Departement
9 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
11 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
14 #include <isl_ctx_private.h>
15 #include "isl_map_private.h"
18 #include "isl_sample.h"
19 #include <isl_mat_private.h>
20 #include <isl_vec_private.h>
21 #include <isl_aff_private.h>
22 #include <isl_constraint_private.h>
23 #include <isl_options_private.h>
24 #include <isl_config.h>
27 * The implementation of parametric integer linear programming in this file
28 * was inspired by the paper "Parametric Integer Programming" and the
29 * report "Solving systems of affine (in)equalities" by Paul Feautrier
32 * The strategy used for obtaining a feasible solution is different
33 * from the one used in isl_tab.c. In particular, in isl_tab.c,
34 * upon finding a constraint that is not yet satisfied, we pivot
35 * in a row that increases the constant term of the row holding the
36 * constraint, making sure the sample solution remains feasible
37 * for all the constraints it already satisfied.
38 * Here, we always pivot in the row holding the constraint,
39 * choosing a column that induces the lexicographically smallest
40 * increment to the sample solution.
42 * By starting out from a sample value that is lexicographically
43 * smaller than any integer point in the problem space, the first
44 * feasible integer sample point we find will also be the lexicographically
45 * smallest. If all variables can be assumed to be non-negative,
46 * then the initial sample value may be chosen equal to zero.
47 * However, we will not make this assumption. Instead, we apply
48 * the "big parameter" trick. Any variable x is then not directly
49 * used in the tableau, but instead it is represented by another
50 * variable x' = M + x, where M is an arbitrarily large (positive)
51 * value. x' is therefore always non-negative, whatever the value of x.
52 * Taking as initial sample value x' = 0 corresponds to x = -M,
53 * which is always smaller than any possible value of x.
55 * The big parameter trick is used in the main tableau and
56 * also in the context tableau if isl_context_lex is used.
57 * In this case, each tableaus has its own big parameter.
58 * Before doing any real work, we check if all the parameters
59 * happen to be non-negative. If so, we drop the column corresponding
60 * to M from the initial context tableau.
61 * If isl_context_gbr is used, then the big parameter trick is only
62 * used in the main tableau.
66 struct isl_context_op
{
67 /* detect nonnegative parameters in context and mark them in tab */
68 struct isl_tab
*(*detect_nonnegative_parameters
)(
69 struct isl_context
*context
, struct isl_tab
*tab
);
70 /* return temporary reference to basic set representation of context */
71 struct isl_basic_set
*(*peek_basic_set
)(struct isl_context
*context
);
72 /* return temporary reference to tableau representation of context */
73 struct isl_tab
*(*peek_tab
)(struct isl_context
*context
);
74 /* add equality; check is 1 if eq may not be valid;
75 * update is 1 if we may want to call ineq_sign on context later.
77 void (*add_eq
)(struct isl_context
*context
, isl_int
*eq
,
78 int check
, int update
);
79 /* add inequality; check is 1 if ineq may not be valid;
80 * update is 1 if we may want to call ineq_sign on context later.
82 void (*add_ineq
)(struct isl_context
*context
, isl_int
*ineq
,
83 int check
, int update
);
84 /* check sign of ineq based on previous information.
85 * strict is 1 if saturation should be treated as a positive sign.
87 enum isl_tab_row_sign (*ineq_sign
)(struct isl_context
*context
,
88 isl_int
*ineq
, int strict
);
89 /* check if inequality maintains feasibility */
90 int (*test_ineq
)(struct isl_context
*context
, isl_int
*ineq
);
91 /* return index of a div that corresponds to "div" */
92 int (*get_div
)(struct isl_context
*context
, struct isl_tab
*tab
,
94 /* insert div "div" to context at "pos" and return non-negativity */
95 isl_bool (*insert_div
)(struct isl_context
*context
, int pos
,
96 __isl_keep isl_vec
*div
);
97 int (*detect_equalities
)(struct isl_context
*context
,
99 /* return row index of "best" split */
100 int (*best_split
)(struct isl_context
*context
, struct isl_tab
*tab
);
101 /* check if context has already been determined to be empty */
102 int (*is_empty
)(struct isl_context
*context
);
103 /* check if context is still usable */
104 int (*is_ok
)(struct isl_context
*context
);
105 /* save a copy/snapshot of context */
106 void *(*save
)(struct isl_context
*context
);
107 /* restore saved context */
108 void (*restore
)(struct isl_context
*context
, void *);
109 /* discard saved context */
110 void (*discard
)(void *);
111 /* invalidate context */
112 void (*invalidate
)(struct isl_context
*context
);
114 __isl_null
struct isl_context
*(*free
)(struct isl_context
*context
);
117 /* Shared parts of context representation.
119 * "n_unknown" is the number of final unknown integer divisions
120 * in the input domain.
123 struct isl_context_op
*op
;
127 struct isl_context_lex
{
128 struct isl_context context
;
132 /* A stack (linked list) of solutions of subtrees of the search space.
134 * "M" describes the solution in terms of the dimensions of "dom".
135 * The number of columns of "M" is one more than the total number
136 * of dimensions of "dom".
138 * If "M" is NULL, then there is no solution on "dom".
140 struct isl_partial_sol
{
142 struct isl_basic_set
*dom
;
145 struct isl_partial_sol
*next
;
149 struct isl_sol_callback
{
150 struct isl_tab_callback callback
;
154 /* isl_sol is an interface for constructing a solution to
155 * a parametric integer linear programming problem.
156 * Every time the algorithm reaches a state where a solution
157 * can be read off from the tableau (including cases where the tableau
158 * is empty), the function "add" is called on the isl_sol passed
159 * to find_solutions_main.
161 * The context tableau is owned by isl_sol and is updated incrementally.
163 * There are currently two implementations of this interface,
164 * isl_sol_map, which simply collects the solutions in an isl_map
165 * and (optionally) the parts of the context where there is no solution
167 * isl_sol_for, which calls a user-defined function for each part of
176 struct isl_context
*context
;
177 struct isl_partial_sol
*partial
;
178 void (*add
)(struct isl_sol
*sol
,
179 struct isl_basic_set
*dom
, struct isl_mat
*M
);
180 void (*add_empty
)(struct isl_sol
*sol
, struct isl_basic_set
*bset
);
181 void (*free
)(struct isl_sol
*sol
);
182 struct isl_sol_callback dec_level
;
185 static void sol_free(struct isl_sol
*sol
)
187 struct isl_partial_sol
*partial
, *next
;
190 for (partial
= sol
->partial
; partial
; partial
= next
) {
191 next
= partial
->next
;
192 isl_basic_set_free(partial
->dom
);
193 isl_mat_free(partial
->M
);
199 /* Push a partial solution represented by a domain and mapping M
200 * onto the stack of partial solutions.
202 static void sol_push_sol(struct isl_sol
*sol
,
203 struct isl_basic_set
*dom
, struct isl_mat
*M
)
205 struct isl_partial_sol
*partial
;
207 if (sol
->error
|| !dom
)
210 partial
= isl_alloc_type(dom
->ctx
, struct isl_partial_sol
);
214 partial
->level
= sol
->level
;
217 partial
->next
= sol
->partial
;
219 sol
->partial
= partial
;
223 isl_basic_set_free(dom
);
228 /* Pop one partial solution from the partial solution stack and
229 * pass it on to sol->add or sol->add_empty.
231 static void sol_pop_one(struct isl_sol
*sol
)
233 struct isl_partial_sol
*partial
;
235 partial
= sol
->partial
;
236 sol
->partial
= partial
->next
;
239 sol
->add(sol
, partial
->dom
, partial
->M
);
241 sol
->add_empty(sol
, partial
->dom
);
245 /* Return a fresh copy of the domain represented by the context tableau.
247 static struct isl_basic_set
*sol_domain(struct isl_sol
*sol
)
249 struct isl_basic_set
*bset
;
254 bset
= isl_basic_set_dup(sol
->context
->op
->peek_basic_set(sol
->context
));
255 bset
= isl_basic_set_update_from_tab(bset
,
256 sol
->context
->op
->peek_tab(sol
->context
));
261 /* Check whether two partial solutions have the same mapping, where n_div
262 * is the number of divs that the two partial solutions have in common.
264 static int same_solution(struct isl_partial_sol
*s1
, struct isl_partial_sol
*s2
,
270 if (!s1
->M
!= !s2
->M
)
275 dim
= isl_basic_set_total_dim(s1
->dom
) - s1
->dom
->n_div
;
277 for (i
= 0; i
< s1
->M
->n_row
; ++i
) {
278 if (isl_seq_first_non_zero(s1
->M
->row
[i
]+1+dim
+n_div
,
279 s1
->M
->n_col
-1-dim
-n_div
) != -1)
281 if (isl_seq_first_non_zero(s2
->M
->row
[i
]+1+dim
+n_div
,
282 s2
->M
->n_col
-1-dim
-n_div
) != -1)
284 if (!isl_seq_eq(s1
->M
->row
[i
], s2
->M
->row
[i
], 1+dim
+n_div
))
290 /* Pop all solutions from the partial solution stack that were pushed onto
291 * the stack at levels that are deeper than the current level.
292 * If the two topmost elements on the stack have the same level
293 * and represent the same solution, then their domains are combined.
294 * This combined domain is the same as the current context domain
295 * as sol_pop is called each time we move back to a higher level.
296 * If the outer level (0) has been reached, then all partial solutions
297 * at the current level are also popped off.
299 static void sol_pop(struct isl_sol
*sol
)
301 struct isl_partial_sol
*partial
;
307 partial
= sol
->partial
;
311 if (partial
->level
== 0 && sol
->level
== 0) {
312 for (partial
= sol
->partial
; partial
; partial
= sol
->partial
)
317 if (partial
->level
<= sol
->level
)
320 if (partial
->next
&& partial
->next
->level
== partial
->level
) {
321 n_div
= isl_basic_set_dim(
322 sol
->context
->op
->peek_basic_set(sol
->context
),
325 if (!same_solution(partial
, partial
->next
, n_div
)) {
329 struct isl_basic_set
*bset
;
333 n
= isl_basic_set_dim(partial
->next
->dom
, isl_dim_div
);
335 bset
= sol_domain(sol
);
336 isl_basic_set_free(partial
->next
->dom
);
337 partial
->next
->dom
= bset
;
338 M
= partial
->next
->M
;
340 M
= isl_mat_drop_cols(M
, M
->n_col
- n
, n
);
341 partial
->next
->M
= M
;
345 partial
->next
->level
= sol
->level
;
350 sol
->partial
= partial
->next
;
351 isl_basic_set_free(partial
->dom
);
352 isl_mat_free(partial
->M
);
358 if (sol
->level
== 0) {
359 for (partial
= sol
->partial
; partial
; partial
= sol
->partial
)
365 error
: sol
->error
= 1;
368 static void sol_dec_level(struct isl_sol
*sol
)
378 static int sol_dec_level_wrap(struct isl_tab_callback
*cb
)
380 struct isl_sol_callback
*callback
= (struct isl_sol_callback
*)cb
;
382 sol_dec_level(callback
->sol
);
384 return callback
->sol
->error
? -1 : 0;
387 /* Move down to next level and push callback onto context tableau
388 * to decrease the level again when it gets rolled back across
389 * the current state. That is, dec_level will be called with
390 * the context tableau in the same state as it is when inc_level
393 static void sol_inc_level(struct isl_sol
*sol
)
401 tab
= sol
->context
->op
->peek_tab(sol
->context
);
402 if (isl_tab_push_callback(tab
, &sol
->dec_level
.callback
) < 0)
406 static void scale_rows(struct isl_mat
*mat
, isl_int m
, int n_row
)
410 if (isl_int_is_one(m
))
413 for (i
= 0; i
< n_row
; ++i
)
414 isl_seq_scale(mat
->row
[i
], mat
->row
[i
], m
, mat
->n_col
);
417 /* Add the solution identified by the tableau and the context tableau.
419 * The layout of the variables is as follows.
420 * tab->n_var is equal to the total number of variables in the input
421 * map (including divs that were copied from the context)
422 * + the number of extra divs constructed
423 * Of these, the first tab->n_param and the last tab->n_div variables
424 * correspond to the variables in the context, i.e.,
425 * tab->n_param + tab->n_div = context_tab->n_var
426 * tab->n_param is equal to the number of parameters and input
427 * dimensions in the input map
428 * tab->n_div is equal to the number of divs in the context
430 * If there is no solution, then call add_empty with a basic set
431 * that corresponds to the context tableau. (If add_empty is NULL,
434 * If there is a solution, then first construct a matrix that maps
435 * all dimensions of the context to the output variables, i.e.,
436 * the output dimensions in the input map.
437 * The divs in the input map (if any) that do not correspond to any
438 * div in the context do not appear in the solution.
439 * The algorithm will make sure that they have an integer value,
440 * but these values themselves are of no interest.
441 * We have to be careful not to drop or rearrange any divs in the
442 * context because that would change the meaning of the matrix.
444 * To extract the value of the output variables, it should be noted
445 * that we always use a big parameter M in the main tableau and so
446 * the variable stored in this tableau is not an output variable x itself, but
447 * x' = M + x (in case of minimization)
449 * x' = M - x (in case of maximization)
450 * If x' appears in a column, then its optimal value is zero,
451 * which means that the optimal value of x is an unbounded number
452 * (-M for minimization and M for maximization).
453 * We currently assume that the output dimensions in the original map
454 * are bounded, so this cannot occur.
455 * Similarly, when x' appears in a row, then the coefficient of M in that
456 * row is necessarily 1.
457 * If the row in the tableau represents
458 * d x' = c + d M + e(y)
459 * then, in case of minimization, the corresponding row in the matrix
462 * with a d = m, the (updated) common denominator of the matrix.
463 * In case of maximization, the row will be
466 static void sol_add(struct isl_sol
*sol
, struct isl_tab
*tab
)
468 struct isl_basic_set
*bset
= NULL
;
469 struct isl_mat
*mat
= NULL
;
474 if (sol
->error
|| !tab
)
477 if (tab
->empty
&& !sol
->add_empty
)
479 if (sol
->context
->op
->is_empty(sol
->context
))
482 bset
= sol_domain(sol
);
485 sol_push_sol(sol
, bset
, NULL
);
491 mat
= isl_mat_alloc(tab
->mat
->ctx
, 1 + sol
->n_out
,
492 1 + tab
->n_param
+ tab
->n_div
);
498 isl_seq_clr(mat
->row
[0] + 1, mat
->n_col
- 1);
499 isl_int_set_si(mat
->row
[0][0], 1);
500 for (row
= 0; row
< sol
->n_out
; ++row
) {
501 int i
= tab
->n_param
+ row
;
504 isl_seq_clr(mat
->row
[1 + row
], mat
->n_col
);
505 if (!tab
->var
[i
].is_row
) {
507 isl_die(mat
->ctx
, isl_error_invalid
,
508 "unbounded optimum", goto error2
);
512 r
= tab
->var
[i
].index
;
514 isl_int_ne(tab
->mat
->row
[r
][2], tab
->mat
->row
[r
][0]))
515 isl_die(mat
->ctx
, isl_error_invalid
,
516 "unbounded optimum", goto error2
);
517 isl_int_gcd(m
, mat
->row
[0][0], tab
->mat
->row
[r
][0]);
518 isl_int_divexact(m
, tab
->mat
->row
[r
][0], m
);
519 scale_rows(mat
, m
, 1 + row
);
520 isl_int_divexact(m
, mat
->row
[0][0], tab
->mat
->row
[r
][0]);
521 isl_int_mul(mat
->row
[1 + row
][0], m
, tab
->mat
->row
[r
][1]);
522 for (j
= 0; j
< tab
->n_param
; ++j
) {
524 if (tab
->var
[j
].is_row
)
526 col
= tab
->var
[j
].index
;
527 isl_int_mul(mat
->row
[1 + row
][1 + j
], m
,
528 tab
->mat
->row
[r
][off
+ col
]);
530 for (j
= 0; j
< tab
->n_div
; ++j
) {
532 if (tab
->var
[tab
->n_var
- tab
->n_div
+j
].is_row
)
534 col
= tab
->var
[tab
->n_var
- tab
->n_div
+j
].index
;
535 isl_int_mul(mat
->row
[1 + row
][1 + tab
->n_param
+ j
], m
,
536 tab
->mat
->row
[r
][off
+ col
]);
539 isl_seq_neg(mat
->row
[1 + row
], mat
->row
[1 + row
],
545 sol_push_sol(sol
, bset
, mat
);
550 isl_basic_set_free(bset
);
558 struct isl_set
*empty
;
561 static void sol_map_free(struct isl_sol_map
*sol_map
)
565 if (sol_map
->sol
.context
)
566 sol_map
->sol
.context
->op
->free(sol_map
->sol
.context
);
567 isl_map_free(sol_map
->map
);
568 isl_set_free(sol_map
->empty
);
572 static void sol_map_free_wrap(struct isl_sol
*sol
)
574 sol_map_free((struct isl_sol_map
*)sol
);
577 /* This function is called for parts of the context where there is
578 * no solution, with "bset" corresponding to the context tableau.
579 * Simply add the basic set to the set "empty".
581 static void sol_map_add_empty(struct isl_sol_map
*sol
,
582 struct isl_basic_set
*bset
)
584 if (!bset
|| !sol
->empty
)
587 sol
->empty
= isl_set_grow(sol
->empty
, 1);
588 bset
= isl_basic_set_simplify(bset
);
589 bset
= isl_basic_set_finalize(bset
);
590 sol
->empty
= isl_set_add_basic_set(sol
->empty
, isl_basic_set_copy(bset
));
593 isl_basic_set_free(bset
);
596 isl_basic_set_free(bset
);
600 static void sol_map_add_empty_wrap(struct isl_sol
*sol
,
601 struct isl_basic_set
*bset
)
603 sol_map_add_empty((struct isl_sol_map
*)sol
, bset
);
606 /* Given a basic map "dom" that represents the context and an affine
607 * matrix "M" that maps the dimensions of the context to the
608 * output variables, construct a basic map with the same parameters
609 * and divs as the context, the dimensions of the context as input
610 * dimensions and a number of output dimensions that is equal to
611 * the number of output dimensions in the input map.
613 * The constraints and divs of the context are simply copied
614 * from "dom". For each row
618 * is added, with d the common denominator of M.
620 static void sol_map_add(struct isl_sol_map
*sol
,
621 struct isl_basic_set
*dom
, struct isl_mat
*M
)
624 struct isl_basic_map
*bmap
= NULL
;
632 if (sol
->sol
.error
|| !dom
|| !M
)
635 n_out
= sol
->sol
.n_out
;
636 n_eq
= dom
->n_eq
+ n_out
;
637 n_ineq
= dom
->n_ineq
;
639 nparam
= isl_basic_set_total_dim(dom
) - n_div
;
640 total
= isl_map_dim(sol
->map
, isl_dim_all
);
641 bmap
= isl_basic_map_alloc_space(isl_map_get_space(sol
->map
),
642 n_div
, n_eq
, 2 * n_div
+ n_ineq
);
645 if (sol
->sol
.rational
)
646 ISL_F_SET(bmap
, ISL_BASIC_MAP_RATIONAL
);
647 for (i
= 0; i
< dom
->n_div
; ++i
) {
648 int k
= isl_basic_map_alloc_div(bmap
);
651 isl_seq_cpy(bmap
->div
[k
], dom
->div
[i
], 1 + 1 + nparam
);
652 isl_seq_clr(bmap
->div
[k
] + 1 + 1 + nparam
, total
- nparam
);
653 isl_seq_cpy(bmap
->div
[k
] + 1 + 1 + total
,
654 dom
->div
[i
] + 1 + 1 + nparam
, i
);
656 for (i
= 0; i
< dom
->n_eq
; ++i
) {
657 int k
= isl_basic_map_alloc_equality(bmap
);
660 isl_seq_cpy(bmap
->eq
[k
], dom
->eq
[i
], 1 + nparam
);
661 isl_seq_clr(bmap
->eq
[k
] + 1 + nparam
, total
- nparam
);
662 isl_seq_cpy(bmap
->eq
[k
] + 1 + total
,
663 dom
->eq
[i
] + 1 + nparam
, n_div
);
665 for (i
= 0; i
< dom
->n_ineq
; ++i
) {
666 int k
= isl_basic_map_alloc_inequality(bmap
);
669 isl_seq_cpy(bmap
->ineq
[k
], dom
->ineq
[i
], 1 + nparam
);
670 isl_seq_clr(bmap
->ineq
[k
] + 1 + nparam
, total
- nparam
);
671 isl_seq_cpy(bmap
->ineq
[k
] + 1 + total
,
672 dom
->ineq
[i
] + 1 + nparam
, n_div
);
674 for (i
= 0; i
< M
->n_row
- 1; ++i
) {
675 int k
= isl_basic_map_alloc_equality(bmap
);
678 isl_seq_cpy(bmap
->eq
[k
], M
->row
[1 + i
], 1 + nparam
);
679 isl_seq_clr(bmap
->eq
[k
] + 1 + nparam
, n_out
);
680 isl_int_neg(bmap
->eq
[k
][1 + nparam
+ i
], M
->row
[0][0]);
681 isl_seq_cpy(bmap
->eq
[k
] + 1 + nparam
+ n_out
,
682 M
->row
[1 + i
] + 1 + nparam
, n_div
);
684 bmap
= isl_basic_map_simplify(bmap
);
685 bmap
= isl_basic_map_finalize(bmap
);
686 sol
->map
= isl_map_grow(sol
->map
, 1);
687 sol
->map
= isl_map_add_basic_map(sol
->map
, bmap
);
688 isl_basic_set_free(dom
);
694 isl_basic_set_free(dom
);
696 isl_basic_map_free(bmap
);
700 static void sol_map_add_wrap(struct isl_sol
*sol
,
701 struct isl_basic_set
*dom
, struct isl_mat
*M
)
703 sol_map_add((struct isl_sol_map
*)sol
, dom
, M
);
707 /* Store the "parametric constant" of row "row" of tableau "tab" in "line",
708 * i.e., the constant term and the coefficients of all variables that
709 * appear in the context tableau.
710 * Note that the coefficient of the big parameter M is NOT copied.
711 * The context tableau may not have a big parameter and even when it
712 * does, it is a different big parameter.
714 static void get_row_parameter_line(struct isl_tab
*tab
, int row
, isl_int
*line
)
717 unsigned off
= 2 + tab
->M
;
719 isl_int_set(line
[0], tab
->mat
->row
[row
][1]);
720 for (i
= 0; i
< tab
->n_param
; ++i
) {
721 if (tab
->var
[i
].is_row
)
722 isl_int_set_si(line
[1 + i
], 0);
724 int col
= tab
->var
[i
].index
;
725 isl_int_set(line
[1 + i
], tab
->mat
->row
[row
][off
+ col
]);
728 for (i
= 0; i
< tab
->n_div
; ++i
) {
729 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
730 isl_int_set_si(line
[1 + tab
->n_param
+ i
], 0);
732 int col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
733 isl_int_set(line
[1 + tab
->n_param
+ i
],
734 tab
->mat
->row
[row
][off
+ col
]);
739 /* Check if rows "row1" and "row2" have identical "parametric constants",
740 * as explained above.
741 * In this case, we also insist that the coefficients of the big parameter
742 * be the same as the values of the constants will only be the same
743 * if these coefficients are also the same.
745 static int identical_parameter_line(struct isl_tab
*tab
, int row1
, int row2
)
748 unsigned off
= 2 + tab
->M
;
750 if (isl_int_ne(tab
->mat
->row
[row1
][1], tab
->mat
->row
[row2
][1]))
753 if (tab
->M
&& isl_int_ne(tab
->mat
->row
[row1
][2],
754 tab
->mat
->row
[row2
][2]))
757 for (i
= 0; i
< tab
->n_param
+ tab
->n_div
; ++i
) {
758 int pos
= i
< tab
->n_param
? i
:
759 tab
->n_var
- tab
->n_div
+ i
- tab
->n_param
;
762 if (tab
->var
[pos
].is_row
)
764 col
= tab
->var
[pos
].index
;
765 if (isl_int_ne(tab
->mat
->row
[row1
][off
+ col
],
766 tab
->mat
->row
[row2
][off
+ col
]))
772 /* Return an inequality that expresses that the "parametric constant"
773 * should be non-negative.
774 * This function is only called when the coefficient of the big parameter
777 static struct isl_vec
*get_row_parameter_ineq(struct isl_tab
*tab
, int row
)
779 struct isl_vec
*ineq
;
781 ineq
= isl_vec_alloc(tab
->mat
->ctx
, 1 + tab
->n_param
+ tab
->n_div
);
785 get_row_parameter_line(tab
, row
, ineq
->el
);
787 ineq
= isl_vec_normalize(ineq
);
792 /* Normalize a div expression of the form
794 * [(g*f(x) + c)/(g * m)]
796 * with c the constant term and f(x) the remaining coefficients, to
800 static void normalize_div(__isl_keep isl_vec
*div
)
802 isl_ctx
*ctx
= isl_vec_get_ctx(div
);
803 int len
= div
->size
- 2;
805 isl_seq_gcd(div
->el
+ 2, len
, &ctx
->normalize_gcd
);
806 isl_int_gcd(ctx
->normalize_gcd
, ctx
->normalize_gcd
, div
->el
[0]);
808 if (isl_int_is_one(ctx
->normalize_gcd
))
811 isl_int_divexact(div
->el
[0], div
->el
[0], ctx
->normalize_gcd
);
812 isl_int_fdiv_q(div
->el
[1], div
->el
[1], ctx
->normalize_gcd
);
813 isl_seq_scale_down(div
->el
+ 2, div
->el
+ 2, ctx
->normalize_gcd
, len
);
816 /* Return an integer division for use in a parametric cut based
818 * In particular, let the parametric constant of the row be
822 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
823 * The div returned is equal to
825 * floor(\sum_i {-a_i} y_i) = floor((\sum_i (-a_i mod d) y_i)/d)
827 static struct isl_vec
*get_row_parameter_div(struct isl_tab
*tab
, int row
)
831 div
= isl_vec_alloc(tab
->mat
->ctx
, 1 + 1 + tab
->n_param
+ tab
->n_div
);
835 isl_int_set(div
->el
[0], tab
->mat
->row
[row
][0]);
836 get_row_parameter_line(tab
, row
, div
->el
+ 1);
837 isl_seq_neg(div
->el
+ 1, div
->el
+ 1, div
->size
- 1);
839 isl_seq_fdiv_r(div
->el
+ 1, div
->el
+ 1, div
->el
[0], div
->size
- 1);
844 /* Return an integer division for use in transferring an integrality constraint
846 * In particular, let the parametric constant of the row be
850 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
851 * The the returned div is equal to
853 * floor(\sum_i {a_i} y_i) = floor((\sum_i (a_i mod d) y_i)/d)
855 static struct isl_vec
*get_row_split_div(struct isl_tab
*tab
, int row
)
859 div
= isl_vec_alloc(tab
->mat
->ctx
, 1 + 1 + tab
->n_param
+ tab
->n_div
);
863 isl_int_set(div
->el
[0], tab
->mat
->row
[row
][0]);
864 get_row_parameter_line(tab
, row
, div
->el
+ 1);
866 isl_seq_fdiv_r(div
->el
+ 1, div
->el
+ 1, div
->el
[0], div
->size
- 1);
871 /* Construct and return an inequality that expresses an upper bound
873 * In particular, if the div is given by
877 * then the inequality expresses
881 static struct isl_vec
*ineq_for_div(struct isl_basic_set
*bset
, unsigned div
)
885 struct isl_vec
*ineq
;
890 total
= isl_basic_set_total_dim(bset
);
891 div_pos
= 1 + total
- bset
->n_div
+ div
;
893 ineq
= isl_vec_alloc(bset
->ctx
, 1 + total
);
897 isl_seq_cpy(ineq
->el
, bset
->div
[div
] + 1, 1 + total
);
898 isl_int_neg(ineq
->el
[div_pos
], bset
->div
[div
][0]);
902 /* Given a row in the tableau and a div that was created
903 * using get_row_split_div and that has been constrained to equality, i.e.,
905 * d = floor(\sum_i {a_i} y_i) = \sum_i {a_i} y_i
907 * replace the expression "\sum_i {a_i} y_i" in the row by d,
908 * i.e., we subtract "\sum_i {a_i} y_i" and add 1 d.
909 * The coefficients of the non-parameters in the tableau have been
910 * verified to be integral. We can therefore simply replace coefficient b
911 * by floor(b). For the coefficients of the parameters we have
912 * floor(a_i) = a_i - {a_i}, while for the other coefficients, we have
915 static struct isl_tab
*set_row_cst_to_div(struct isl_tab
*tab
, int row
, int div
)
917 isl_seq_fdiv_q(tab
->mat
->row
[row
] + 1, tab
->mat
->row
[row
] + 1,
918 tab
->mat
->row
[row
][0], 1 + tab
->M
+ tab
->n_col
);
920 isl_int_set_si(tab
->mat
->row
[row
][0], 1);
922 if (tab
->var
[tab
->n_var
- tab
->n_div
+ div
].is_row
) {
923 int drow
= tab
->var
[tab
->n_var
- tab
->n_div
+ div
].index
;
925 isl_assert(tab
->mat
->ctx
,
926 isl_int_is_one(tab
->mat
->row
[drow
][0]), goto error
);
927 isl_seq_combine(tab
->mat
->row
[row
] + 1,
928 tab
->mat
->ctx
->one
, tab
->mat
->row
[row
] + 1,
929 tab
->mat
->ctx
->one
, tab
->mat
->row
[drow
] + 1,
930 1 + tab
->M
+ tab
->n_col
);
932 int dcol
= tab
->var
[tab
->n_var
- tab
->n_div
+ div
].index
;
934 isl_int_add_ui(tab
->mat
->row
[row
][2 + tab
->M
+ dcol
],
935 tab
->mat
->row
[row
][2 + tab
->M
+ dcol
], 1);
944 /* Check if the (parametric) constant of the given row is obviously
945 * negative, meaning that we don't need to consult the context tableau.
946 * If there is a big parameter and its coefficient is non-zero,
947 * then this coefficient determines the outcome.
948 * Otherwise, we check whether the constant is negative and
949 * all non-zero coefficients of parameters are negative and
950 * belong to non-negative parameters.
952 static int is_obviously_neg(struct isl_tab
*tab
, int row
)
956 unsigned off
= 2 + tab
->M
;
959 if (isl_int_is_pos(tab
->mat
->row
[row
][2]))
961 if (isl_int_is_neg(tab
->mat
->row
[row
][2]))
965 if (isl_int_is_nonneg(tab
->mat
->row
[row
][1]))
967 for (i
= 0; i
< tab
->n_param
; ++i
) {
968 /* Eliminated parameter */
969 if (tab
->var
[i
].is_row
)
971 col
= tab
->var
[i
].index
;
972 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
974 if (!tab
->var
[i
].is_nonneg
)
976 if (isl_int_is_pos(tab
->mat
->row
[row
][off
+ col
]))
979 for (i
= 0; i
< tab
->n_div
; ++i
) {
980 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
982 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
983 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
985 if (!tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_nonneg
)
987 if (isl_int_is_pos(tab
->mat
->row
[row
][off
+ col
]))
993 /* Check if the (parametric) constant of the given row is obviously
994 * non-negative, meaning that we don't need to consult the context tableau.
995 * If there is a big parameter and its coefficient is non-zero,
996 * then this coefficient determines the outcome.
997 * Otherwise, we check whether the constant is non-negative and
998 * all non-zero coefficients of parameters are positive and
999 * belong to non-negative parameters.
1001 static int is_obviously_nonneg(struct isl_tab
*tab
, int row
)
1005 unsigned off
= 2 + tab
->M
;
1008 if (isl_int_is_pos(tab
->mat
->row
[row
][2]))
1010 if (isl_int_is_neg(tab
->mat
->row
[row
][2]))
1014 if (isl_int_is_neg(tab
->mat
->row
[row
][1]))
1016 for (i
= 0; i
< tab
->n_param
; ++i
) {
1017 /* Eliminated parameter */
1018 if (tab
->var
[i
].is_row
)
1020 col
= tab
->var
[i
].index
;
1021 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
1023 if (!tab
->var
[i
].is_nonneg
)
1025 if (isl_int_is_neg(tab
->mat
->row
[row
][off
+ col
]))
1028 for (i
= 0; i
< tab
->n_div
; ++i
) {
1029 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
1031 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
1032 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
1034 if (!tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_nonneg
)
1036 if (isl_int_is_neg(tab
->mat
->row
[row
][off
+ col
]))
1042 /* Given a row r and two columns, return the column that would
1043 * lead to the lexicographically smallest increment in the sample
1044 * solution when leaving the basis in favor of the row.
1045 * Pivoting with column c will increment the sample value by a non-negative
1046 * constant times a_{V,c}/a_{r,c}, with a_{V,c} the elements of column c
1047 * corresponding to the non-parametric variables.
1048 * If variable v appears in a column c_v, the a_{v,c} = 1 iff c = c_v,
1049 * with all other entries in this virtual row equal to zero.
1050 * If variable v appears in a row, then a_{v,c} is the element in column c
1053 * Let v be the first variable with a_{v,c1}/a_{r,c1} != a_{v,c2}/a_{r,c2}.
1054 * Then if a_{v,c1}/a_{r,c1} < a_{v,c2}/a_{r,c2}, i.e.,
1055 * a_{v,c2} a_{r,c1} - a_{v,c1} a_{r,c2} > 0, c1 results in the minimal
1056 * increment. Otherwise, it's c2.
1058 static int lexmin_col_pair(struct isl_tab
*tab
,
1059 int row
, int col1
, int col2
, isl_int tmp
)
1064 tr
= tab
->mat
->row
[row
] + 2 + tab
->M
;
1066 for (i
= tab
->n_param
; i
< tab
->n_var
- tab
->n_div
; ++i
) {
1070 if (!tab
->var
[i
].is_row
) {
1071 if (tab
->var
[i
].index
== col1
)
1073 if (tab
->var
[i
].index
== col2
)
1078 if (tab
->var
[i
].index
== row
)
1081 r
= tab
->mat
->row
[tab
->var
[i
].index
] + 2 + tab
->M
;
1082 s1
= isl_int_sgn(r
[col1
]);
1083 s2
= isl_int_sgn(r
[col2
]);
1084 if (s1
== 0 && s2
== 0)
1091 isl_int_mul(tmp
, r
[col2
], tr
[col1
]);
1092 isl_int_submul(tmp
, r
[col1
], tr
[col2
]);
1093 if (isl_int_is_pos(tmp
))
1095 if (isl_int_is_neg(tmp
))
1101 /* Given a row in the tableau, find and return the column that would
1102 * result in the lexicographically smallest, but positive, increment
1103 * in the sample point.
1104 * If there is no such column, then return tab->n_col.
1105 * If anything goes wrong, return -1.
1107 static int lexmin_pivot_col(struct isl_tab
*tab
, int row
)
1110 int col
= tab
->n_col
;
1114 tr
= tab
->mat
->row
[row
] + 2 + tab
->M
;
1118 for (j
= tab
->n_dead
; j
< tab
->n_col
; ++j
) {
1119 if (tab
->col_var
[j
] >= 0 &&
1120 (tab
->col_var
[j
] < tab
->n_param
||
1121 tab
->col_var
[j
] >= tab
->n_var
- tab
->n_div
))
1124 if (!isl_int_is_pos(tr
[j
]))
1127 if (col
== tab
->n_col
)
1130 col
= lexmin_col_pair(tab
, row
, col
, j
, tmp
);
1131 isl_assert(tab
->mat
->ctx
, col
>= 0, goto error
);
1141 /* Return the first known violated constraint, i.e., a non-negative
1142 * constraint that currently has an either obviously negative value
1143 * or a previously determined to be negative value.
1145 * If any constraint has a negative coefficient for the big parameter,
1146 * if any, then we return one of these first.
1148 static int first_neg(struct isl_tab
*tab
)
1153 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
1154 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
1156 if (!isl_int_is_neg(tab
->mat
->row
[row
][2]))
1159 tab
->row_sign
[row
] = isl_tab_row_neg
;
1162 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
1163 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
1165 if (tab
->row_sign
) {
1166 if (tab
->row_sign
[row
] == 0 &&
1167 is_obviously_neg(tab
, row
))
1168 tab
->row_sign
[row
] = isl_tab_row_neg
;
1169 if (tab
->row_sign
[row
] != isl_tab_row_neg
)
1171 } else if (!is_obviously_neg(tab
, row
))
1178 /* Check whether the invariant that all columns are lexico-positive
1179 * is satisfied. This function is not called from the current code
1180 * but is useful during debugging.
1182 static void check_lexpos(struct isl_tab
*tab
) __attribute__ ((unused
));
1183 static void check_lexpos(struct isl_tab
*tab
)
1185 unsigned off
= 2 + tab
->M
;
1190 for (col
= tab
->n_dead
; col
< tab
->n_col
; ++col
) {
1191 if (tab
->col_var
[col
] >= 0 &&
1192 (tab
->col_var
[col
] < tab
->n_param
||
1193 tab
->col_var
[col
] >= tab
->n_var
- tab
->n_div
))
1195 for (var
= tab
->n_param
; var
< tab
->n_var
- tab
->n_div
; ++var
) {
1196 if (!tab
->var
[var
].is_row
) {
1197 if (tab
->var
[var
].index
== col
)
1202 row
= tab
->var
[var
].index
;
1203 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
1205 if (isl_int_is_pos(tab
->mat
->row
[row
][off
+ col
]))
1207 fprintf(stderr
, "lexneg column %d (row %d)\n",
1210 if (var
>= tab
->n_var
- tab
->n_div
)
1211 fprintf(stderr
, "zero column %d\n", col
);
1215 /* Report to the caller that the given constraint is part of an encountered
1218 static int report_conflicting_constraint(struct isl_tab
*tab
, int con
)
1220 return tab
->conflict(con
, tab
->conflict_user
);
1223 /* Given a conflicting row in the tableau, report all constraints
1224 * involved in the row to the caller. That is, the row itself
1225 * (if it represents a constraint) and all constraint columns with
1226 * non-zero (and therefore negative) coefficients.
1228 static int report_conflict(struct isl_tab
*tab
, int row
)
1236 if (tab
->row_var
[row
] < 0 &&
1237 report_conflicting_constraint(tab
, ~tab
->row_var
[row
]) < 0)
1240 tr
= tab
->mat
->row
[row
] + 2 + tab
->M
;
1242 for (j
= tab
->n_dead
; j
< tab
->n_col
; ++j
) {
1243 if (tab
->col_var
[j
] >= 0 &&
1244 (tab
->col_var
[j
] < tab
->n_param
||
1245 tab
->col_var
[j
] >= tab
->n_var
- tab
->n_div
))
1248 if (!isl_int_is_neg(tr
[j
]))
1251 if (tab
->col_var
[j
] < 0 &&
1252 report_conflicting_constraint(tab
, ~tab
->col_var
[j
]) < 0)
1259 /* Resolve all known or obviously violated constraints through pivoting.
1260 * In particular, as long as we can find any violated constraint, we
1261 * look for a pivoting column that would result in the lexicographically
1262 * smallest increment in the sample point. If there is no such column
1263 * then the tableau is infeasible.
1265 static int restore_lexmin(struct isl_tab
*tab
) WARN_UNUSED
;
1266 static int restore_lexmin(struct isl_tab
*tab
)
1274 while ((row
= first_neg(tab
)) != -1) {
1275 col
= lexmin_pivot_col(tab
, row
);
1276 if (col
>= tab
->n_col
) {
1277 if (report_conflict(tab
, row
) < 0)
1279 if (isl_tab_mark_empty(tab
) < 0)
1285 if (isl_tab_pivot(tab
, row
, col
) < 0)
1291 /* Given a row that represents an equality, look for an appropriate
1293 * In particular, if there are any non-zero coefficients among
1294 * the non-parameter variables, then we take the last of these
1295 * variables. Eliminating this variable in terms of the other
1296 * variables and/or parameters does not influence the property
1297 * that all column in the initial tableau are lexicographically
1298 * positive. The row corresponding to the eliminated variable
1299 * will only have non-zero entries below the diagonal of the
1300 * initial tableau. That is, we transform
1306 * If there is no such non-parameter variable, then we are dealing with
1307 * pure parameter equality and we pick any parameter with coefficient 1 or -1
1308 * for elimination. This will ensure that the eliminated parameter
1309 * always has an integer value whenever all the other parameters are integral.
1310 * If there is no such parameter then we return -1.
1312 static int last_var_col_or_int_par_col(struct isl_tab
*tab
, int row
)
1314 unsigned off
= 2 + tab
->M
;
1317 for (i
= tab
->n_var
- tab
->n_div
- 1; i
>= 0 && i
>= tab
->n_param
; --i
) {
1319 if (tab
->var
[i
].is_row
)
1321 col
= tab
->var
[i
].index
;
1322 if (col
<= tab
->n_dead
)
1324 if (!isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
1327 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
1328 if (isl_int_is_one(tab
->mat
->row
[row
][off
+ i
]))
1330 if (isl_int_is_negone(tab
->mat
->row
[row
][off
+ i
]))
1336 /* Add an equality that is known to be valid to the tableau.
1337 * We first check if we can eliminate a variable or a parameter.
1338 * If not, we add the equality as two inequalities.
1339 * In this case, the equality was a pure parameter equality and there
1340 * is no need to resolve any constraint violations.
1342 * This function assumes that at least two more rows and at least
1343 * two more elements in the constraint array are available in the tableau.
1345 static struct isl_tab
*add_lexmin_valid_eq(struct isl_tab
*tab
, isl_int
*eq
)
1352 r
= isl_tab_add_row(tab
, eq
);
1356 r
= tab
->con
[r
].index
;
1357 i
= last_var_col_or_int_par_col(tab
, r
);
1359 tab
->con
[r
].is_nonneg
= 1;
1360 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
1362 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
1363 r
= isl_tab_add_row(tab
, eq
);
1366 tab
->con
[r
].is_nonneg
= 1;
1367 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
1370 if (isl_tab_pivot(tab
, r
, i
) < 0)
1372 if (isl_tab_kill_col(tab
, i
) < 0)
1383 /* Check if the given row is a pure constant.
1385 static int is_constant(struct isl_tab
*tab
, int row
)
1387 unsigned off
= 2 + tab
->M
;
1389 return isl_seq_first_non_zero(tab
->mat
->row
[row
] + off
+ tab
->n_dead
,
1390 tab
->n_col
- tab
->n_dead
) == -1;
1393 /* Add an equality that may or may not be valid to the tableau.
1394 * If the resulting row is a pure constant, then it must be zero.
1395 * Otherwise, the resulting tableau is empty.
1397 * If the row is not a pure constant, then we add two inequalities,
1398 * each time checking that they can be satisfied.
1399 * In the end we try to use one of the two constraints to eliminate
1402 * This function assumes that at least two more rows and at least
1403 * two more elements in the constraint array are available in the tableau.
1405 static int add_lexmin_eq(struct isl_tab
*tab
, isl_int
*eq
) WARN_UNUSED
;
1406 static int add_lexmin_eq(struct isl_tab
*tab
, isl_int
*eq
)
1410 struct isl_tab_undo
*snap
;
1414 snap
= isl_tab_snap(tab
);
1415 r1
= isl_tab_add_row(tab
, eq
);
1418 tab
->con
[r1
].is_nonneg
= 1;
1419 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r1
]) < 0)
1422 row
= tab
->con
[r1
].index
;
1423 if (is_constant(tab
, row
)) {
1424 if (!isl_int_is_zero(tab
->mat
->row
[row
][1]) ||
1425 (tab
->M
&& !isl_int_is_zero(tab
->mat
->row
[row
][2]))) {
1426 if (isl_tab_mark_empty(tab
) < 0)
1430 if (isl_tab_rollback(tab
, snap
) < 0)
1435 if (restore_lexmin(tab
) < 0)
1440 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
1442 r2
= isl_tab_add_row(tab
, eq
);
1445 tab
->con
[r2
].is_nonneg
= 1;
1446 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r2
]) < 0)
1449 if (restore_lexmin(tab
) < 0)
1454 if (!tab
->con
[r1
].is_row
) {
1455 if (isl_tab_kill_col(tab
, tab
->con
[r1
].index
) < 0)
1457 } else if (!tab
->con
[r2
].is_row
) {
1458 if (isl_tab_kill_col(tab
, tab
->con
[r2
].index
) < 0)
1463 tab
->bmap
= isl_basic_map_add_ineq(tab
->bmap
, eq
);
1464 if (isl_tab_push(tab
, isl_tab_undo_bmap_ineq
) < 0)
1466 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
1467 tab
->bmap
= isl_basic_map_add_ineq(tab
->bmap
, eq
);
1468 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
1469 if (isl_tab_push(tab
, isl_tab_undo_bmap_ineq
) < 0)
1478 /* Add an inequality to the tableau, resolving violations using
1481 * This function assumes that at least one more row and at least
1482 * one more element in the constraint array are available in the tableau.
1484 static struct isl_tab
*add_lexmin_ineq(struct isl_tab
*tab
, isl_int
*ineq
)
1491 tab
->bmap
= isl_basic_map_add_ineq(tab
->bmap
, ineq
);
1492 if (isl_tab_push(tab
, isl_tab_undo_bmap_ineq
) < 0)
1497 r
= isl_tab_add_row(tab
, ineq
);
1500 tab
->con
[r
].is_nonneg
= 1;
1501 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
1503 if (isl_tab_row_is_redundant(tab
, tab
->con
[r
].index
)) {
1504 if (isl_tab_mark_redundant(tab
, tab
->con
[r
].index
) < 0)
1509 if (restore_lexmin(tab
) < 0)
1511 if (!tab
->empty
&& tab
->con
[r
].is_row
&&
1512 isl_tab_row_is_redundant(tab
, tab
->con
[r
].index
))
1513 if (isl_tab_mark_redundant(tab
, tab
->con
[r
].index
) < 0)
1521 /* Check if the coefficients of the parameters are all integral.
1523 static int integer_parameter(struct isl_tab
*tab
, int row
)
1527 unsigned off
= 2 + tab
->M
;
1529 for (i
= 0; i
< tab
->n_param
; ++i
) {
1530 /* Eliminated parameter */
1531 if (tab
->var
[i
].is_row
)
1533 col
= tab
->var
[i
].index
;
1534 if (!isl_int_is_divisible_by(tab
->mat
->row
[row
][off
+ col
],
1535 tab
->mat
->row
[row
][0]))
1538 for (i
= 0; i
< tab
->n_div
; ++i
) {
1539 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
1541 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
1542 if (!isl_int_is_divisible_by(tab
->mat
->row
[row
][off
+ col
],
1543 tab
->mat
->row
[row
][0]))
1549 /* Check if the coefficients of the non-parameter variables are all integral.
1551 static int integer_variable(struct isl_tab
*tab
, int row
)
1554 unsigned off
= 2 + tab
->M
;
1556 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
1557 if (tab
->col_var
[i
] >= 0 &&
1558 (tab
->col_var
[i
] < tab
->n_param
||
1559 tab
->col_var
[i
] >= tab
->n_var
- tab
->n_div
))
1561 if (!isl_int_is_divisible_by(tab
->mat
->row
[row
][off
+ i
],
1562 tab
->mat
->row
[row
][0]))
1568 /* Check if the constant term is integral.
1570 static int integer_constant(struct isl_tab
*tab
, int row
)
1572 return isl_int_is_divisible_by(tab
->mat
->row
[row
][1],
1573 tab
->mat
->row
[row
][0]);
1576 #define I_CST 1 << 0
1577 #define I_PAR 1 << 1
1578 #define I_VAR 1 << 2
1580 /* Check for next (non-parameter) variable after "var" (first if var == -1)
1581 * that is non-integer and therefore requires a cut and return
1582 * the index of the variable.
1583 * For parametric tableaus, there are three parts in a row,
1584 * the constant, the coefficients of the parameters and the rest.
1585 * For each part, we check whether the coefficients in that part
1586 * are all integral and if so, set the corresponding flag in *f.
1587 * If the constant and the parameter part are integral, then the
1588 * current sample value is integral and no cut is required
1589 * (irrespective of whether the variable part is integral).
1591 static int next_non_integer_var(struct isl_tab
*tab
, int var
, int *f
)
1593 var
= var
< 0 ? tab
->n_param
: var
+ 1;
1595 for (; var
< tab
->n_var
- tab
->n_div
; ++var
) {
1598 if (!tab
->var
[var
].is_row
)
1600 row
= tab
->var
[var
].index
;
1601 if (integer_constant(tab
, row
))
1602 ISL_FL_SET(flags
, I_CST
);
1603 if (integer_parameter(tab
, row
))
1604 ISL_FL_SET(flags
, I_PAR
);
1605 if (ISL_FL_ISSET(flags
, I_CST
) && ISL_FL_ISSET(flags
, I_PAR
))
1607 if (integer_variable(tab
, row
))
1608 ISL_FL_SET(flags
, I_VAR
);
1615 /* Check for first (non-parameter) variable that is non-integer and
1616 * therefore requires a cut and return the corresponding row.
1617 * For parametric tableaus, there are three parts in a row,
1618 * the constant, the coefficients of the parameters and the rest.
1619 * For each part, we check whether the coefficients in that part
1620 * are all integral and if so, set the corresponding flag in *f.
1621 * If the constant and the parameter part are integral, then the
1622 * current sample value is integral and no cut is required
1623 * (irrespective of whether the variable part is integral).
1625 static int first_non_integer_row(struct isl_tab
*tab
, int *f
)
1627 int var
= next_non_integer_var(tab
, -1, f
);
1629 return var
< 0 ? -1 : tab
->var
[var
].index
;
1632 /* Add a (non-parametric) cut to cut away the non-integral sample
1633 * value of the given row.
1635 * If the row is given by
1637 * m r = f + \sum_i a_i y_i
1641 * c = - {-f/m} + \sum_i {a_i/m} y_i >= 0
1643 * The big parameter, if any, is ignored, since it is assumed to be big
1644 * enough to be divisible by any integer.
1645 * If the tableau is actually a parametric tableau, then this function
1646 * is only called when all coefficients of the parameters are integral.
1647 * The cut therefore has zero coefficients for the parameters.
1649 * The current value is known to be negative, so row_sign, if it
1650 * exists, is set accordingly.
1652 * Return the row of the cut or -1.
1654 static int add_cut(struct isl_tab
*tab
, int row
)
1659 unsigned off
= 2 + tab
->M
;
1661 if (isl_tab_extend_cons(tab
, 1) < 0)
1663 r
= isl_tab_allocate_con(tab
);
1667 r_row
= tab
->mat
->row
[tab
->con
[r
].index
];
1668 isl_int_set(r_row
[0], tab
->mat
->row
[row
][0]);
1669 isl_int_neg(r_row
[1], tab
->mat
->row
[row
][1]);
1670 isl_int_fdiv_r(r_row
[1], r_row
[1], tab
->mat
->row
[row
][0]);
1671 isl_int_neg(r_row
[1], r_row
[1]);
1673 isl_int_set_si(r_row
[2], 0);
1674 for (i
= 0; i
< tab
->n_col
; ++i
)
1675 isl_int_fdiv_r(r_row
[off
+ i
],
1676 tab
->mat
->row
[row
][off
+ i
], tab
->mat
->row
[row
][0]);
1678 tab
->con
[r
].is_nonneg
= 1;
1679 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
1682 tab
->row_sign
[tab
->con
[r
].index
] = isl_tab_row_neg
;
1684 return tab
->con
[r
].index
;
1690 /* Given a non-parametric tableau, add cuts until an integer
1691 * sample point is obtained or until the tableau is determined
1692 * to be integer infeasible.
1693 * As long as there is any non-integer value in the sample point,
1694 * we add appropriate cuts, if possible, for each of these
1695 * non-integer values and then resolve the violated
1696 * cut constraints using restore_lexmin.
1697 * If one of the corresponding rows is equal to an integral
1698 * combination of variables/constraints plus a non-integral constant,
1699 * then there is no way to obtain an integer point and we return
1700 * a tableau that is marked empty.
1701 * The parameter cutting_strategy controls the strategy used when adding cuts
1702 * to remove non-integer points. CUT_ALL adds all possible cuts
1703 * before continuing the search. CUT_ONE adds only one cut at a time.
1705 static struct isl_tab
*cut_to_integer_lexmin(struct isl_tab
*tab
,
1706 int cutting_strategy
)
1717 while ((var
= next_non_integer_var(tab
, -1, &flags
)) != -1) {
1719 if (ISL_FL_ISSET(flags
, I_VAR
)) {
1720 if (isl_tab_mark_empty(tab
) < 0)
1724 row
= tab
->var
[var
].index
;
1725 row
= add_cut(tab
, row
);
1728 if (cutting_strategy
== CUT_ONE
)
1730 } while ((var
= next_non_integer_var(tab
, var
, &flags
)) != -1);
1731 if (restore_lexmin(tab
) < 0)
1742 /* Check whether all the currently active samples also satisfy the inequality
1743 * "ineq" (treated as an equality if eq is set).
1744 * Remove those samples that do not.
1746 static struct isl_tab
*check_samples(struct isl_tab
*tab
, isl_int
*ineq
, int eq
)
1754 isl_assert(tab
->mat
->ctx
, tab
->bmap
, goto error
);
1755 isl_assert(tab
->mat
->ctx
, tab
->samples
, goto error
);
1756 isl_assert(tab
->mat
->ctx
, tab
->samples
->n_col
== 1 + tab
->n_var
, goto error
);
1759 for (i
= tab
->n_outside
; i
< tab
->n_sample
; ++i
) {
1761 isl_seq_inner_product(ineq
, tab
->samples
->row
[i
],
1762 1 + tab
->n_var
, &v
);
1763 sgn
= isl_int_sgn(v
);
1764 if (eq
? (sgn
== 0) : (sgn
>= 0))
1766 tab
= isl_tab_drop_sample(tab
, i
);
1778 /* Check whether the sample value of the tableau is finite,
1779 * i.e., either the tableau does not use a big parameter, or
1780 * all values of the variables are equal to the big parameter plus
1781 * some constant. This constant is the actual sample value.
1783 static int sample_is_finite(struct isl_tab
*tab
)
1790 for (i
= 0; i
< tab
->n_var
; ++i
) {
1792 if (!tab
->var
[i
].is_row
)
1794 row
= tab
->var
[i
].index
;
1795 if (isl_int_ne(tab
->mat
->row
[row
][0], tab
->mat
->row
[row
][2]))
1801 /* Check if the context tableau of sol has any integer points.
1802 * Leave tab in empty state if no integer point can be found.
1803 * If an integer point can be found and if moreover it is finite,
1804 * then it is added to the list of sample values.
1806 * This function is only called when none of the currently active sample
1807 * values satisfies the most recently added constraint.
1809 static struct isl_tab
*check_integer_feasible(struct isl_tab
*tab
)
1811 struct isl_tab_undo
*snap
;
1816 snap
= isl_tab_snap(tab
);
1817 if (isl_tab_push_basis(tab
) < 0)
1820 tab
= cut_to_integer_lexmin(tab
, CUT_ALL
);
1824 if (!tab
->empty
&& sample_is_finite(tab
)) {
1825 struct isl_vec
*sample
;
1827 sample
= isl_tab_get_sample_value(tab
);
1829 if (isl_tab_add_sample(tab
, sample
) < 0)
1833 if (!tab
->empty
&& isl_tab_rollback(tab
, snap
) < 0)
1842 /* Check if any of the currently active sample values satisfies
1843 * the inequality "ineq" (an equality if eq is set).
1845 static int tab_has_valid_sample(struct isl_tab
*tab
, isl_int
*ineq
, int eq
)
1853 isl_assert(tab
->mat
->ctx
, tab
->bmap
, return -1);
1854 isl_assert(tab
->mat
->ctx
, tab
->samples
, return -1);
1855 isl_assert(tab
->mat
->ctx
, tab
->samples
->n_col
== 1 + tab
->n_var
, return -1);
1858 for (i
= tab
->n_outside
; i
< tab
->n_sample
; ++i
) {
1860 isl_seq_inner_product(ineq
, tab
->samples
->row
[i
],
1861 1 + tab
->n_var
, &v
);
1862 sgn
= isl_int_sgn(v
);
1863 if (eq
? (sgn
== 0) : (sgn
>= 0))
1868 return i
< tab
->n_sample
;
1871 /* Insert a div specified by "div" to the tableau "tab" at position "pos" and
1872 * return isl_bool_true if the div is obviously non-negative.
1874 static isl_bool
context_tab_insert_div(struct isl_tab
*tab
, int pos
,
1875 __isl_keep isl_vec
*div
,
1876 int (*add_ineq
)(void *user
, isl_int
*), void *user
)
1880 struct isl_mat
*samples
;
1883 r
= isl_tab_insert_div(tab
, pos
, div
, add_ineq
, user
);
1885 return isl_bool_error
;
1886 nonneg
= tab
->var
[r
].is_nonneg
;
1887 tab
->var
[r
].frozen
= 1;
1889 samples
= isl_mat_extend(tab
->samples
,
1890 tab
->n_sample
, 1 + tab
->n_var
);
1891 tab
->samples
= samples
;
1893 return isl_bool_error
;
1894 for (i
= tab
->n_outside
; i
< samples
->n_row
; ++i
) {
1895 isl_seq_inner_product(div
->el
+ 1, samples
->row
[i
],
1896 div
->size
- 1, &samples
->row
[i
][samples
->n_col
- 1]);
1897 isl_int_fdiv_q(samples
->row
[i
][samples
->n_col
- 1],
1898 samples
->row
[i
][samples
->n_col
- 1], div
->el
[0]);
1900 tab
->samples
= isl_mat_move_cols(tab
->samples
, 1 + pos
,
1901 1 + tab
->n_var
- 1, 1);
1903 return isl_bool_error
;
1908 /* Add a div specified by "div" to both the main tableau and
1909 * the context tableau. In case of the main tableau, we only
1910 * need to add an extra div. In the context tableau, we also
1911 * need to express the meaning of the div.
1912 * Return the index of the div or -1 if anything went wrong.
1914 * The new integer division is added before any unknown integer
1915 * divisions in the context to ensure that it does not get
1916 * equated to some linear combination involving unknown integer
1919 static int add_div(struct isl_tab
*tab
, struct isl_context
*context
,
1920 __isl_keep isl_vec
*div
)
1925 struct isl_tab
*context_tab
= context
->op
->peek_tab(context
);
1927 if (!tab
|| !context_tab
)
1930 pos
= context_tab
->n_var
- context
->n_unknown
;
1931 if ((nonneg
= context
->op
->insert_div(context
, pos
, div
)) < 0)
1934 if (!context
->op
->is_ok(context
))
1937 pos
= tab
->n_var
- context
->n_unknown
;
1938 if (isl_tab_extend_vars(tab
, 1) < 0)
1940 r
= isl_tab_insert_var(tab
, pos
);
1944 tab
->var
[r
].is_nonneg
= 1;
1945 tab
->var
[r
].frozen
= 1;
1948 return tab
->n_div
- 1 - context
->n_unknown
;
1950 context
->op
->invalidate(context
);
1954 static int find_div(struct isl_tab
*tab
, isl_int
*div
, isl_int denom
)
1957 unsigned total
= isl_basic_map_total_dim(tab
->bmap
);
1959 for (i
= 0; i
< tab
->bmap
->n_div
; ++i
) {
1960 if (isl_int_ne(tab
->bmap
->div
[i
][0], denom
))
1962 if (!isl_seq_eq(tab
->bmap
->div
[i
] + 1, div
, 1 + total
))
1969 /* Return the index of a div that corresponds to "div".
1970 * We first check if we already have such a div and if not, we create one.
1972 static int get_div(struct isl_tab
*tab
, struct isl_context
*context
,
1973 struct isl_vec
*div
)
1976 struct isl_tab
*context_tab
= context
->op
->peek_tab(context
);
1981 d
= find_div(context_tab
, div
->el
+ 1, div
->el
[0]);
1985 return add_div(tab
, context
, div
);
1988 /* Add a parametric cut to cut away the non-integral sample value
1990 * Let a_i be the coefficients of the constant term and the parameters
1991 * and let b_i be the coefficients of the variables or constraints
1992 * in basis of the tableau.
1993 * Let q be the div q = floor(\sum_i {-a_i} y_i).
1995 * The cut is expressed as
1997 * c = \sum_i -{-a_i} y_i + \sum_i {b_i} x_i + q >= 0
1999 * If q did not already exist in the context tableau, then it is added first.
2000 * If q is in a column of the main tableau then the "+ q" can be accomplished
2001 * by setting the corresponding entry to the denominator of the constraint.
2002 * If q happens to be in a row of the main tableau, then the corresponding
2003 * row needs to be added instead (taking care of the denominators).
2004 * Note that this is very unlikely, but perhaps not entirely impossible.
2006 * The current value of the cut is known to be negative (or at least
2007 * non-positive), so row_sign is set accordingly.
2009 * Return the row of the cut or -1.
2011 static int add_parametric_cut(struct isl_tab
*tab
, int row
,
2012 struct isl_context
*context
)
2014 struct isl_vec
*div
;
2021 unsigned off
= 2 + tab
->M
;
2026 div
= get_row_parameter_div(tab
, row
);
2030 n
= tab
->n_div
- context
->n_unknown
;
2031 d
= context
->op
->get_div(context
, tab
, div
);
2036 if (isl_tab_extend_cons(tab
, 1) < 0)
2038 r
= isl_tab_allocate_con(tab
);
2042 r_row
= tab
->mat
->row
[tab
->con
[r
].index
];
2043 isl_int_set(r_row
[0], tab
->mat
->row
[row
][0]);
2044 isl_int_neg(r_row
[1], tab
->mat
->row
[row
][1]);
2045 isl_int_fdiv_r(r_row
[1], r_row
[1], tab
->mat
->row
[row
][0]);
2046 isl_int_neg(r_row
[1], r_row
[1]);
2048 isl_int_set_si(r_row
[2], 0);
2049 for (i
= 0; i
< tab
->n_param
; ++i
) {
2050 if (tab
->var
[i
].is_row
)
2052 col
= tab
->var
[i
].index
;
2053 isl_int_neg(r_row
[off
+ col
], tab
->mat
->row
[row
][off
+ col
]);
2054 isl_int_fdiv_r(r_row
[off
+ col
], r_row
[off
+ col
],
2055 tab
->mat
->row
[row
][0]);
2056 isl_int_neg(r_row
[off
+ col
], r_row
[off
+ col
]);
2058 for (i
= 0; i
< tab
->n_div
; ++i
) {
2059 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
2061 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
2062 isl_int_neg(r_row
[off
+ col
], tab
->mat
->row
[row
][off
+ col
]);
2063 isl_int_fdiv_r(r_row
[off
+ col
], r_row
[off
+ col
],
2064 tab
->mat
->row
[row
][0]);
2065 isl_int_neg(r_row
[off
+ col
], r_row
[off
+ col
]);
2067 for (i
= 0; i
< tab
->n_col
; ++i
) {
2068 if (tab
->col_var
[i
] >= 0 &&
2069 (tab
->col_var
[i
] < tab
->n_param
||
2070 tab
->col_var
[i
] >= tab
->n_var
- tab
->n_div
))
2072 isl_int_fdiv_r(r_row
[off
+ i
],
2073 tab
->mat
->row
[row
][off
+ i
], tab
->mat
->row
[row
][0]);
2075 if (tab
->var
[tab
->n_var
- tab
->n_div
+ d
].is_row
) {
2077 int d_row
= tab
->var
[tab
->n_var
- tab
->n_div
+ d
].index
;
2079 isl_int_gcd(gcd
, tab
->mat
->row
[d_row
][0], r_row
[0]);
2080 isl_int_divexact(r_row
[0], r_row
[0], gcd
);
2081 isl_int_divexact(gcd
, tab
->mat
->row
[d_row
][0], gcd
);
2082 isl_seq_combine(r_row
+ 1, gcd
, r_row
+ 1,
2083 r_row
[0], tab
->mat
->row
[d_row
] + 1,
2084 off
- 1 + tab
->n_col
);
2085 isl_int_mul(r_row
[0], r_row
[0], tab
->mat
->row
[d_row
][0]);
2088 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ d
].index
;
2089 isl_int_set(r_row
[off
+ col
], tab
->mat
->row
[row
][0]);
2092 tab
->con
[r
].is_nonneg
= 1;
2093 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
2096 tab
->row_sign
[tab
->con
[r
].index
] = isl_tab_row_neg
;
2098 row
= tab
->con
[r
].index
;
2100 if (d
>= n
&& context
->op
->detect_equalities(context
, tab
) < 0)
2106 /* Construct a tableau for bmap that can be used for computing
2107 * the lexicographic minimum (or maximum) of bmap.
2108 * If not NULL, then dom is the domain where the minimum
2109 * should be computed. In this case, we set up a parametric
2110 * tableau with row signs (initialized to "unknown").
2111 * If M is set, then the tableau will use a big parameter.
2112 * If max is set, then a maximum should be computed instead of a minimum.
2113 * This means that for each variable x, the tableau will contain the variable
2114 * x' = M - x, rather than x' = M + x. This in turn means that the coefficient
2115 * of the variables in all constraints are negated prior to adding them
2118 static struct isl_tab
*tab_for_lexmin(struct isl_basic_map
*bmap
,
2119 struct isl_basic_set
*dom
, unsigned M
, int max
)
2122 struct isl_tab
*tab
;
2126 tab
= isl_tab_alloc(bmap
->ctx
, 2 * bmap
->n_eq
+ bmap
->n_ineq
+ 1,
2127 isl_basic_map_total_dim(bmap
), M
);
2131 tab
->rational
= ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
2133 tab
->n_param
= isl_basic_set_total_dim(dom
) - dom
->n_div
;
2134 tab
->n_div
= dom
->n_div
;
2135 tab
->row_sign
= isl_calloc_array(bmap
->ctx
,
2136 enum isl_tab_row_sign
, tab
->mat
->n_row
);
2137 if (tab
->mat
->n_row
&& !tab
->row_sign
)
2140 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
)) {
2141 if (isl_tab_mark_empty(tab
) < 0)
2146 for (i
= tab
->n_param
; i
< tab
->n_var
- tab
->n_div
; ++i
) {
2147 tab
->var
[i
].is_nonneg
= 1;
2148 tab
->var
[i
].frozen
= 1;
2150 o_var
= 1 + tab
->n_param
;
2151 n_var
= tab
->n_var
- tab
->n_param
- tab
->n_div
;
2152 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
2154 isl_seq_neg(bmap
->eq
[i
] + o_var
,
2155 bmap
->eq
[i
] + o_var
, n_var
);
2156 tab
= add_lexmin_valid_eq(tab
, bmap
->eq
[i
]);
2158 isl_seq_neg(bmap
->eq
[i
] + o_var
,
2159 bmap
->eq
[i
] + o_var
, n_var
);
2160 if (!tab
|| tab
->empty
)
2163 if (bmap
->n_eq
&& restore_lexmin(tab
) < 0)
2165 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
2167 isl_seq_neg(bmap
->ineq
[i
] + o_var
,
2168 bmap
->ineq
[i
] + o_var
, n_var
);
2169 tab
= add_lexmin_ineq(tab
, bmap
->ineq
[i
]);
2171 isl_seq_neg(bmap
->ineq
[i
] + o_var
,
2172 bmap
->ineq
[i
] + o_var
, n_var
);
2173 if (!tab
|| tab
->empty
)
2182 /* Given a main tableau where more than one row requires a split,
2183 * determine and return the "best" row to split on.
2185 * Given two rows in the main tableau, if the inequality corresponding
2186 * to the first row is redundant with respect to that of the second row
2187 * in the current tableau, then it is better to split on the second row,
2188 * since in the positive part, both rows will be positive.
2189 * (In the negative part a pivot will have to be performed and just about
2190 * anything can happen to the sign of the other row.)
2192 * As a simple heuristic, we therefore select the row that makes the most
2193 * of the other rows redundant.
2195 * Perhaps it would also be useful to look at the number of constraints
2196 * that conflict with any given constraint.
2198 * best is the best row so far (-1 when we have not found any row yet).
2199 * best_r is the number of other rows made redundant by row best.
2200 * When best is still -1, bset_r is meaningless, but it is initialized
2201 * to some arbitrary value (0) anyway. Without this redundant initialization
2202 * valgrind may warn about uninitialized memory accesses when isl
2203 * is compiled with some versions of gcc.
2205 static int best_split(struct isl_tab
*tab
, struct isl_tab
*context_tab
)
2207 struct isl_tab_undo
*snap
;
2213 if (isl_tab_extend_cons(context_tab
, 2) < 0)
2216 snap
= isl_tab_snap(context_tab
);
2218 for (split
= tab
->n_redundant
; split
< tab
->n_row
; ++split
) {
2219 struct isl_tab_undo
*snap2
;
2220 struct isl_vec
*ineq
= NULL
;
2224 if (!isl_tab_var_from_row(tab
, split
)->is_nonneg
)
2226 if (tab
->row_sign
[split
] != isl_tab_row_any
)
2229 ineq
= get_row_parameter_ineq(tab
, split
);
2232 ok
= isl_tab_add_ineq(context_tab
, ineq
->el
) >= 0;
2237 snap2
= isl_tab_snap(context_tab
);
2239 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
2240 struct isl_tab_var
*var
;
2244 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
2246 if (tab
->row_sign
[row
] != isl_tab_row_any
)
2249 ineq
= get_row_parameter_ineq(tab
, row
);
2252 ok
= isl_tab_add_ineq(context_tab
, ineq
->el
) >= 0;
2256 var
= &context_tab
->con
[context_tab
->n_con
- 1];
2257 if (!context_tab
->empty
&&
2258 !isl_tab_min_at_most_neg_one(context_tab
, var
))
2260 if (isl_tab_rollback(context_tab
, snap2
) < 0)
2263 if (best
== -1 || r
> best_r
) {
2267 if (isl_tab_rollback(context_tab
, snap
) < 0)
2274 static struct isl_basic_set
*context_lex_peek_basic_set(
2275 struct isl_context
*context
)
2277 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2280 return isl_tab_peek_bset(clex
->tab
);
2283 static struct isl_tab
*context_lex_peek_tab(struct isl_context
*context
)
2285 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2289 static void context_lex_add_eq(struct isl_context
*context
, isl_int
*eq
,
2290 int check
, int update
)
2292 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2293 if (isl_tab_extend_cons(clex
->tab
, 2) < 0)
2295 if (add_lexmin_eq(clex
->tab
, eq
) < 0)
2298 int v
= tab_has_valid_sample(clex
->tab
, eq
, 1);
2302 clex
->tab
= check_integer_feasible(clex
->tab
);
2305 clex
->tab
= check_samples(clex
->tab
, eq
, 1);
2308 isl_tab_free(clex
->tab
);
2312 static void context_lex_add_ineq(struct isl_context
*context
, isl_int
*ineq
,
2313 int check
, int update
)
2315 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2316 if (isl_tab_extend_cons(clex
->tab
, 1) < 0)
2318 clex
->tab
= add_lexmin_ineq(clex
->tab
, ineq
);
2320 int v
= tab_has_valid_sample(clex
->tab
, ineq
, 0);
2324 clex
->tab
= check_integer_feasible(clex
->tab
);
2327 clex
->tab
= check_samples(clex
->tab
, ineq
, 0);
2330 isl_tab_free(clex
->tab
);
2334 static int context_lex_add_ineq_wrap(void *user
, isl_int
*ineq
)
2336 struct isl_context
*context
= (struct isl_context
*)user
;
2337 context_lex_add_ineq(context
, ineq
, 0, 0);
2338 return context
->op
->is_ok(context
) ? 0 : -1;
2341 /* Check which signs can be obtained by "ineq" on all the currently
2342 * active sample values. See row_sign for more information.
2344 static enum isl_tab_row_sign
tab_ineq_sign(struct isl_tab
*tab
, isl_int
*ineq
,
2350 enum isl_tab_row_sign res
= isl_tab_row_unknown
;
2352 isl_assert(tab
->mat
->ctx
, tab
->samples
, return isl_tab_row_unknown
);
2353 isl_assert(tab
->mat
->ctx
, tab
->samples
->n_col
== 1 + tab
->n_var
,
2354 return isl_tab_row_unknown
);
2357 for (i
= tab
->n_outside
; i
< tab
->n_sample
; ++i
) {
2358 isl_seq_inner_product(tab
->samples
->row
[i
], ineq
,
2359 1 + tab
->n_var
, &tmp
);
2360 sgn
= isl_int_sgn(tmp
);
2361 if (sgn
> 0 || (sgn
== 0 && strict
)) {
2362 if (res
== isl_tab_row_unknown
)
2363 res
= isl_tab_row_pos
;
2364 if (res
== isl_tab_row_neg
)
2365 res
= isl_tab_row_any
;
2368 if (res
== isl_tab_row_unknown
)
2369 res
= isl_tab_row_neg
;
2370 if (res
== isl_tab_row_pos
)
2371 res
= isl_tab_row_any
;
2373 if (res
== isl_tab_row_any
)
2381 static enum isl_tab_row_sign
context_lex_ineq_sign(struct isl_context
*context
,
2382 isl_int
*ineq
, int strict
)
2384 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2385 return tab_ineq_sign(clex
->tab
, ineq
, strict
);
2388 /* Check whether "ineq" can be added to the tableau without rendering
2391 static int context_lex_test_ineq(struct isl_context
*context
, isl_int
*ineq
)
2393 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2394 struct isl_tab_undo
*snap
;
2400 if (isl_tab_extend_cons(clex
->tab
, 1) < 0)
2403 snap
= isl_tab_snap(clex
->tab
);
2404 if (isl_tab_push_basis(clex
->tab
) < 0)
2406 clex
->tab
= add_lexmin_ineq(clex
->tab
, ineq
);
2407 clex
->tab
= check_integer_feasible(clex
->tab
);
2410 feasible
= !clex
->tab
->empty
;
2411 if (isl_tab_rollback(clex
->tab
, snap
) < 0)
2417 static int context_lex_get_div(struct isl_context
*context
, struct isl_tab
*tab
,
2418 struct isl_vec
*div
)
2420 return get_div(tab
, context
, div
);
2423 /* Insert a div specified by "div" to the context tableau at position "pos" and
2424 * return isl_bool_true if the div is obviously non-negative.
2425 * context_tab_add_div will always return isl_bool_true, because all variables
2426 * in a isl_context_lex tableau are non-negative.
2427 * However, if we are using a big parameter in the context, then this only
2428 * reflects the non-negativity of the variable used to _encode_ the
2429 * div, i.e., div' = M + div, so we can't draw any conclusions.
2431 static isl_bool
context_lex_insert_div(struct isl_context
*context
, int pos
,
2432 __isl_keep isl_vec
*div
)
2434 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2436 nonneg
= context_tab_insert_div(clex
->tab
, pos
, div
,
2437 context_lex_add_ineq_wrap
, context
);
2439 return isl_bool_error
;
2441 return isl_bool_false
;
2445 static int context_lex_detect_equalities(struct isl_context
*context
,
2446 struct isl_tab
*tab
)
2451 static int context_lex_best_split(struct isl_context
*context
,
2452 struct isl_tab
*tab
)
2454 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2455 struct isl_tab_undo
*snap
;
2458 snap
= isl_tab_snap(clex
->tab
);
2459 if (isl_tab_push_basis(clex
->tab
) < 0)
2461 r
= best_split(tab
, clex
->tab
);
2463 if (r
>= 0 && isl_tab_rollback(clex
->tab
, snap
) < 0)
2469 static int context_lex_is_empty(struct isl_context
*context
)
2471 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2474 return clex
->tab
->empty
;
2477 static void *context_lex_save(struct isl_context
*context
)
2479 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2480 struct isl_tab_undo
*snap
;
2482 snap
= isl_tab_snap(clex
->tab
);
2483 if (isl_tab_push_basis(clex
->tab
) < 0)
2485 if (isl_tab_save_samples(clex
->tab
) < 0)
2491 static void context_lex_restore(struct isl_context
*context
, void *save
)
2493 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2494 if (isl_tab_rollback(clex
->tab
, (struct isl_tab_undo
*)save
) < 0) {
2495 isl_tab_free(clex
->tab
);
2500 static void context_lex_discard(void *save
)
2504 static int context_lex_is_ok(struct isl_context
*context
)
2506 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2510 /* For each variable in the context tableau, check if the variable can
2511 * only attain non-negative values. If so, mark the parameter as non-negative
2512 * in the main tableau. This allows for a more direct identification of some
2513 * cases of violated constraints.
2515 static struct isl_tab
*tab_detect_nonnegative_parameters(struct isl_tab
*tab
,
2516 struct isl_tab
*context_tab
)
2519 struct isl_tab_undo
*snap
;
2520 struct isl_vec
*ineq
= NULL
;
2521 struct isl_tab_var
*var
;
2524 if (context_tab
->n_var
== 0)
2527 ineq
= isl_vec_alloc(tab
->mat
->ctx
, 1 + context_tab
->n_var
);
2531 if (isl_tab_extend_cons(context_tab
, 1) < 0)
2534 snap
= isl_tab_snap(context_tab
);
2537 isl_seq_clr(ineq
->el
, ineq
->size
);
2538 for (i
= 0; i
< context_tab
->n_var
; ++i
) {
2539 isl_int_set_si(ineq
->el
[1 + i
], 1);
2540 if (isl_tab_add_ineq(context_tab
, ineq
->el
) < 0)
2542 var
= &context_tab
->con
[context_tab
->n_con
- 1];
2543 if (!context_tab
->empty
&&
2544 !isl_tab_min_at_most_neg_one(context_tab
, var
)) {
2546 if (i
>= tab
->n_param
)
2547 j
= i
- tab
->n_param
+ tab
->n_var
- tab
->n_div
;
2548 tab
->var
[j
].is_nonneg
= 1;
2551 isl_int_set_si(ineq
->el
[1 + i
], 0);
2552 if (isl_tab_rollback(context_tab
, snap
) < 0)
2556 if (context_tab
->M
&& n
== context_tab
->n_var
) {
2557 context_tab
->mat
= isl_mat_drop_cols(context_tab
->mat
, 2, 1);
2569 static struct isl_tab
*context_lex_detect_nonnegative_parameters(
2570 struct isl_context
*context
, struct isl_tab
*tab
)
2572 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2573 struct isl_tab_undo
*snap
;
2578 snap
= isl_tab_snap(clex
->tab
);
2579 if (isl_tab_push_basis(clex
->tab
) < 0)
2582 tab
= tab_detect_nonnegative_parameters(tab
, clex
->tab
);
2584 if (isl_tab_rollback(clex
->tab
, snap
) < 0)
2593 static void context_lex_invalidate(struct isl_context
*context
)
2595 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2596 isl_tab_free(clex
->tab
);
2600 static __isl_null
struct isl_context
*context_lex_free(
2601 struct isl_context
*context
)
2603 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2604 isl_tab_free(clex
->tab
);
2610 struct isl_context_op isl_context_lex_op
= {
2611 context_lex_detect_nonnegative_parameters
,
2612 context_lex_peek_basic_set
,
2613 context_lex_peek_tab
,
2615 context_lex_add_ineq
,
2616 context_lex_ineq_sign
,
2617 context_lex_test_ineq
,
2618 context_lex_get_div
,
2619 context_lex_insert_div
,
2620 context_lex_detect_equalities
,
2621 context_lex_best_split
,
2622 context_lex_is_empty
,
2625 context_lex_restore
,
2626 context_lex_discard
,
2627 context_lex_invalidate
,
2631 static struct isl_tab
*context_tab_for_lexmin(struct isl_basic_set
*bset
)
2633 struct isl_tab
*tab
;
2637 tab
= tab_for_lexmin((struct isl_basic_map
*)bset
, NULL
, 1, 0);
2640 if (isl_tab_track_bset(tab
, bset
) < 0)
2642 tab
= isl_tab_init_samples(tab
);
2645 isl_basic_set_free(bset
);
2649 static struct isl_context
*isl_context_lex_alloc(struct isl_basic_set
*dom
)
2651 struct isl_context_lex
*clex
;
2656 clex
= isl_alloc_type(dom
->ctx
, struct isl_context_lex
);
2660 clex
->context
.op
= &isl_context_lex_op
;
2662 clex
->tab
= context_tab_for_lexmin(isl_basic_set_copy(dom
));
2663 if (restore_lexmin(clex
->tab
) < 0)
2665 clex
->tab
= check_integer_feasible(clex
->tab
);
2669 return &clex
->context
;
2671 clex
->context
.op
->free(&clex
->context
);
2675 /* Representation of the context when using generalized basis reduction.
2677 * "shifted" contains the offsets of the unit hypercubes that lie inside the
2678 * context. Any rational point in "shifted" can therefore be rounded
2679 * up to an integer point in the context.
2680 * If the context is constrained by any equality, then "shifted" is not used
2681 * as it would be empty.
2683 struct isl_context_gbr
{
2684 struct isl_context context
;
2685 struct isl_tab
*tab
;
2686 struct isl_tab
*shifted
;
2687 struct isl_tab
*cone
;
2690 static struct isl_tab
*context_gbr_detect_nonnegative_parameters(
2691 struct isl_context
*context
, struct isl_tab
*tab
)
2693 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2696 return tab_detect_nonnegative_parameters(tab
, cgbr
->tab
);
2699 static struct isl_basic_set
*context_gbr_peek_basic_set(
2700 struct isl_context
*context
)
2702 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2705 return isl_tab_peek_bset(cgbr
->tab
);
2708 static struct isl_tab
*context_gbr_peek_tab(struct isl_context
*context
)
2710 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2714 /* Initialize the "shifted" tableau of the context, which
2715 * contains the constraints of the original tableau shifted
2716 * by the sum of all negative coefficients. This ensures
2717 * that any rational point in the shifted tableau can
2718 * be rounded up to yield an integer point in the original tableau.
2720 static void gbr_init_shifted(struct isl_context_gbr
*cgbr
)
2723 struct isl_vec
*cst
;
2724 struct isl_basic_set
*bset
= isl_tab_peek_bset(cgbr
->tab
);
2725 unsigned dim
= isl_basic_set_total_dim(bset
);
2727 cst
= isl_vec_alloc(cgbr
->tab
->mat
->ctx
, bset
->n_ineq
);
2731 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
2732 isl_int_set(cst
->el
[i
], bset
->ineq
[i
][0]);
2733 for (j
= 0; j
< dim
; ++j
) {
2734 if (!isl_int_is_neg(bset
->ineq
[i
][1 + j
]))
2736 isl_int_add(bset
->ineq
[i
][0], bset
->ineq
[i
][0],
2737 bset
->ineq
[i
][1 + j
]);
2741 cgbr
->shifted
= isl_tab_from_basic_set(bset
, 0);
2743 for (i
= 0; i
< bset
->n_ineq
; ++i
)
2744 isl_int_set(bset
->ineq
[i
][0], cst
->el
[i
]);
2749 /* Check if the shifted tableau is non-empty, and if so
2750 * use the sample point to construct an integer point
2751 * of the context tableau.
2753 static struct isl_vec
*gbr_get_shifted_sample(struct isl_context_gbr
*cgbr
)
2755 struct isl_vec
*sample
;
2758 gbr_init_shifted(cgbr
);
2761 if (cgbr
->shifted
->empty
)
2762 return isl_vec_alloc(cgbr
->tab
->mat
->ctx
, 0);
2764 sample
= isl_tab_get_sample_value(cgbr
->shifted
);
2765 sample
= isl_vec_ceil(sample
);
2770 static struct isl_basic_set
*drop_constant_terms(struct isl_basic_set
*bset
)
2777 for (i
= 0; i
< bset
->n_eq
; ++i
)
2778 isl_int_set_si(bset
->eq
[i
][0], 0);
2780 for (i
= 0; i
< bset
->n_ineq
; ++i
)
2781 isl_int_set_si(bset
->ineq
[i
][0], 0);
2786 static int use_shifted(struct isl_context_gbr
*cgbr
)
2790 return cgbr
->tab
->bmap
->n_eq
== 0 && cgbr
->tab
->bmap
->n_div
== 0;
2793 static struct isl_vec
*gbr_get_sample(struct isl_context_gbr
*cgbr
)
2795 struct isl_basic_set
*bset
;
2796 struct isl_basic_set
*cone
;
2798 if (isl_tab_sample_is_integer(cgbr
->tab
))
2799 return isl_tab_get_sample_value(cgbr
->tab
);
2801 if (use_shifted(cgbr
)) {
2802 struct isl_vec
*sample
;
2804 sample
= gbr_get_shifted_sample(cgbr
);
2805 if (!sample
|| sample
->size
> 0)
2808 isl_vec_free(sample
);
2812 bset
= isl_tab_peek_bset(cgbr
->tab
);
2813 cgbr
->cone
= isl_tab_from_recession_cone(bset
, 0);
2816 if (isl_tab_track_bset(cgbr
->cone
,
2817 isl_basic_set_copy(bset
)) < 0)
2820 if (isl_tab_detect_implicit_equalities(cgbr
->cone
) < 0)
2823 if (cgbr
->cone
->n_dead
== cgbr
->cone
->n_col
) {
2824 struct isl_vec
*sample
;
2825 struct isl_tab_undo
*snap
;
2827 if (cgbr
->tab
->basis
) {
2828 if (cgbr
->tab
->basis
->n_col
!= 1 + cgbr
->tab
->n_var
) {
2829 isl_mat_free(cgbr
->tab
->basis
);
2830 cgbr
->tab
->basis
= NULL
;
2832 cgbr
->tab
->n_zero
= 0;
2833 cgbr
->tab
->n_unbounded
= 0;
2836 snap
= isl_tab_snap(cgbr
->tab
);
2838 sample
= isl_tab_sample(cgbr
->tab
);
2840 if (!sample
|| isl_tab_rollback(cgbr
->tab
, snap
) < 0) {
2841 isl_vec_free(sample
);
2848 cone
= isl_basic_set_dup(isl_tab_peek_bset(cgbr
->cone
));
2849 cone
= drop_constant_terms(cone
);
2850 cone
= isl_basic_set_update_from_tab(cone
, cgbr
->cone
);
2851 cone
= isl_basic_set_underlying_set(cone
);
2852 cone
= isl_basic_set_gauss(cone
, NULL
);
2854 bset
= isl_basic_set_dup(isl_tab_peek_bset(cgbr
->tab
));
2855 bset
= isl_basic_set_update_from_tab(bset
, cgbr
->tab
);
2856 bset
= isl_basic_set_underlying_set(bset
);
2857 bset
= isl_basic_set_gauss(bset
, NULL
);
2859 return isl_basic_set_sample_with_cone(bset
, cone
);
2862 static void check_gbr_integer_feasible(struct isl_context_gbr
*cgbr
)
2864 struct isl_vec
*sample
;
2869 if (cgbr
->tab
->empty
)
2872 sample
= gbr_get_sample(cgbr
);
2876 if (sample
->size
== 0) {
2877 isl_vec_free(sample
);
2878 if (isl_tab_mark_empty(cgbr
->tab
) < 0)
2883 if (isl_tab_add_sample(cgbr
->tab
, sample
) < 0)
2888 isl_tab_free(cgbr
->tab
);
2892 static struct isl_tab
*add_gbr_eq(struct isl_tab
*tab
, isl_int
*eq
)
2897 if (isl_tab_extend_cons(tab
, 2) < 0)
2900 if (isl_tab_add_eq(tab
, eq
) < 0)
2909 /* Add the equality described by "eq" to the context.
2910 * If "check" is set, then we check if the context is empty after
2911 * adding the equality.
2912 * If "update" is set, then we check if the samples are still valid.
2914 * We do not explicitly add shifted copies of the equality to
2915 * cgbr->shifted since they would conflict with each other.
2916 * Instead, we directly mark cgbr->shifted empty.
2918 static void context_gbr_add_eq(struct isl_context
*context
, isl_int
*eq
,
2919 int check
, int update
)
2921 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2923 cgbr
->tab
= add_gbr_eq(cgbr
->tab
, eq
);
2925 if (cgbr
->shifted
&& !cgbr
->shifted
->empty
&& use_shifted(cgbr
)) {
2926 if (isl_tab_mark_empty(cgbr
->shifted
) < 0)
2930 if (cgbr
->cone
&& cgbr
->cone
->n_col
!= cgbr
->cone
->n_dead
) {
2931 if (isl_tab_extend_cons(cgbr
->cone
, 2) < 0)
2933 if (isl_tab_add_eq(cgbr
->cone
, eq
) < 0)
2938 int v
= tab_has_valid_sample(cgbr
->tab
, eq
, 1);
2942 check_gbr_integer_feasible(cgbr
);
2945 cgbr
->tab
= check_samples(cgbr
->tab
, eq
, 1);
2948 isl_tab_free(cgbr
->tab
);
2952 static void add_gbr_ineq(struct isl_context_gbr
*cgbr
, isl_int
*ineq
)
2957 if (isl_tab_extend_cons(cgbr
->tab
, 1) < 0)
2960 if (isl_tab_add_ineq(cgbr
->tab
, ineq
) < 0)
2963 if (cgbr
->shifted
&& !cgbr
->shifted
->empty
&& use_shifted(cgbr
)) {
2966 dim
= isl_basic_map_total_dim(cgbr
->tab
->bmap
);
2968 if (isl_tab_extend_cons(cgbr
->shifted
, 1) < 0)
2971 for (i
= 0; i
< dim
; ++i
) {
2972 if (!isl_int_is_neg(ineq
[1 + i
]))
2974 isl_int_add(ineq
[0], ineq
[0], ineq
[1 + i
]);
2977 if (isl_tab_add_ineq(cgbr
->shifted
, ineq
) < 0)
2980 for (i
= 0; i
< dim
; ++i
) {
2981 if (!isl_int_is_neg(ineq
[1 + i
]))
2983 isl_int_sub(ineq
[0], ineq
[0], ineq
[1 + i
]);
2987 if (cgbr
->cone
&& cgbr
->cone
->n_col
!= cgbr
->cone
->n_dead
) {
2988 if (isl_tab_extend_cons(cgbr
->cone
, 1) < 0)
2990 if (isl_tab_add_ineq(cgbr
->cone
, ineq
) < 0)
2996 isl_tab_free(cgbr
->tab
);
3000 static void context_gbr_add_ineq(struct isl_context
*context
, isl_int
*ineq
,
3001 int check
, int update
)
3003 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3005 add_gbr_ineq(cgbr
, ineq
);
3010 int v
= tab_has_valid_sample(cgbr
->tab
, ineq
, 0);
3014 check_gbr_integer_feasible(cgbr
);
3017 cgbr
->tab
= check_samples(cgbr
->tab
, ineq
, 0);
3020 isl_tab_free(cgbr
->tab
);
3024 static int context_gbr_add_ineq_wrap(void *user
, isl_int
*ineq
)
3026 struct isl_context
*context
= (struct isl_context
*)user
;
3027 context_gbr_add_ineq(context
, ineq
, 0, 0);
3028 return context
->op
->is_ok(context
) ? 0 : -1;
3031 static enum isl_tab_row_sign
context_gbr_ineq_sign(struct isl_context
*context
,
3032 isl_int
*ineq
, int strict
)
3034 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3035 return tab_ineq_sign(cgbr
->tab
, ineq
, strict
);
3038 /* Check whether "ineq" can be added to the tableau without rendering
3041 static int context_gbr_test_ineq(struct isl_context
*context
, isl_int
*ineq
)
3043 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3044 struct isl_tab_undo
*snap
;
3045 struct isl_tab_undo
*shifted_snap
= NULL
;
3046 struct isl_tab_undo
*cone_snap
= NULL
;
3052 if (isl_tab_extend_cons(cgbr
->tab
, 1) < 0)
3055 snap
= isl_tab_snap(cgbr
->tab
);
3057 shifted_snap
= isl_tab_snap(cgbr
->shifted
);
3059 cone_snap
= isl_tab_snap(cgbr
->cone
);
3060 add_gbr_ineq(cgbr
, ineq
);
3061 check_gbr_integer_feasible(cgbr
);
3064 feasible
= !cgbr
->tab
->empty
;
3065 if (isl_tab_rollback(cgbr
->tab
, snap
) < 0)
3068 if (isl_tab_rollback(cgbr
->shifted
, shifted_snap
))
3070 } else if (cgbr
->shifted
) {
3071 isl_tab_free(cgbr
->shifted
);
3072 cgbr
->shifted
= NULL
;
3075 if (isl_tab_rollback(cgbr
->cone
, cone_snap
))
3077 } else if (cgbr
->cone
) {
3078 isl_tab_free(cgbr
->cone
);
3085 /* Return the column of the last of the variables associated to
3086 * a column that has a non-zero coefficient.
3087 * This function is called in a context where only coefficients
3088 * of parameters or divs can be non-zero.
3090 static int last_non_zero_var_col(struct isl_tab
*tab
, isl_int
*p
)
3095 if (tab
->n_var
== 0)
3098 for (i
= tab
->n_var
- 1; i
>= 0; --i
) {
3099 if (i
>= tab
->n_param
&& i
< tab
->n_var
- tab
->n_div
)
3101 if (tab
->var
[i
].is_row
)
3103 col
= tab
->var
[i
].index
;
3104 if (!isl_int_is_zero(p
[col
]))
3111 /* Look through all the recently added equalities in the context
3112 * to see if we can propagate any of them to the main tableau.
3114 * The newly added equalities in the context are encoded as pairs
3115 * of inequalities starting at inequality "first".
3117 * We tentatively add each of these equalities to the main tableau
3118 * and if this happens to result in a row with a final coefficient
3119 * that is one or negative one, we use it to kill a column
3120 * in the main tableau. Otherwise, we discard the tentatively
3122 * This tentative addition of equality constraints turns
3123 * on the undo facility of the tableau. Turn it off again
3124 * at the end, assuming it was turned off to begin with.
3126 * Return 0 on success and -1 on failure.
3128 static int propagate_equalities(struct isl_context_gbr
*cgbr
,
3129 struct isl_tab
*tab
, unsigned first
)
3132 struct isl_vec
*eq
= NULL
;
3133 isl_bool needs_undo
;
3135 needs_undo
= isl_tab_need_undo(tab
);
3138 eq
= isl_vec_alloc(tab
->mat
->ctx
, 1 + tab
->n_var
);
3142 if (isl_tab_extend_cons(tab
, (cgbr
->tab
->bmap
->n_ineq
- first
)/2) < 0)
3145 isl_seq_clr(eq
->el
+ 1 + tab
->n_param
,
3146 tab
->n_var
- tab
->n_param
- tab
->n_div
);
3147 for (i
= first
; i
< cgbr
->tab
->bmap
->n_ineq
; i
+= 2) {
3150 struct isl_tab_undo
*snap
;
3151 snap
= isl_tab_snap(tab
);
3153 isl_seq_cpy(eq
->el
, cgbr
->tab
->bmap
->ineq
[i
], 1 + tab
->n_param
);
3154 isl_seq_cpy(eq
->el
+ 1 + tab
->n_var
- tab
->n_div
,
3155 cgbr
->tab
->bmap
->ineq
[i
] + 1 + tab
->n_param
,
3158 r
= isl_tab_add_row(tab
, eq
->el
);
3161 r
= tab
->con
[r
].index
;
3162 j
= last_non_zero_var_col(tab
, tab
->mat
->row
[r
] + 2 + tab
->M
);
3163 if (j
< 0 || j
< tab
->n_dead
||
3164 !isl_int_is_one(tab
->mat
->row
[r
][0]) ||
3165 (!isl_int_is_one(tab
->mat
->row
[r
][2 + tab
->M
+ j
]) &&
3166 !isl_int_is_negone(tab
->mat
->row
[r
][2 + tab
->M
+ j
]))) {
3167 if (isl_tab_rollback(tab
, snap
) < 0)
3171 if (isl_tab_pivot(tab
, r
, j
) < 0)
3173 if (isl_tab_kill_col(tab
, j
) < 0)
3176 if (restore_lexmin(tab
) < 0)
3181 isl_tab_clear_undo(tab
);
3187 isl_tab_free(cgbr
->tab
);
3192 static int context_gbr_detect_equalities(struct isl_context
*context
,
3193 struct isl_tab
*tab
)
3195 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3199 struct isl_basic_set
*bset
= isl_tab_peek_bset(cgbr
->tab
);
3200 cgbr
->cone
= isl_tab_from_recession_cone(bset
, 0);
3203 if (isl_tab_track_bset(cgbr
->cone
,
3204 isl_basic_set_copy(bset
)) < 0)
3207 if (isl_tab_detect_implicit_equalities(cgbr
->cone
) < 0)
3210 n_ineq
= cgbr
->tab
->bmap
->n_ineq
;
3211 cgbr
->tab
= isl_tab_detect_equalities(cgbr
->tab
, cgbr
->cone
);
3214 if (cgbr
->tab
->bmap
->n_ineq
> n_ineq
&&
3215 propagate_equalities(cgbr
, tab
, n_ineq
) < 0)
3220 isl_tab_free(cgbr
->tab
);
3225 static int context_gbr_get_div(struct isl_context
*context
, struct isl_tab
*tab
,
3226 struct isl_vec
*div
)
3228 return get_div(tab
, context
, div
);
3231 static isl_bool
context_gbr_insert_div(struct isl_context
*context
, int pos
,
3232 __isl_keep isl_vec
*div
)
3234 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3236 int r
, n_div
, o_div
;
3238 n_div
= isl_basic_map_dim(cgbr
->cone
->bmap
, isl_dim_div
);
3239 o_div
= cgbr
->cone
->n_var
- n_div
;
3241 if (isl_tab_extend_cons(cgbr
->cone
, 3) < 0)
3242 return isl_bool_error
;
3243 if (isl_tab_extend_vars(cgbr
->cone
, 1) < 0)
3244 return isl_bool_error
;
3245 if ((r
= isl_tab_insert_var(cgbr
->cone
, pos
)) <0)
3246 return isl_bool_error
;
3248 cgbr
->cone
->bmap
= isl_basic_map_insert_div(cgbr
->cone
->bmap
,
3250 if (!cgbr
->cone
->bmap
)
3251 return isl_bool_error
;
3252 if (isl_tab_push_var(cgbr
->cone
, isl_tab_undo_bmap_div
,
3253 &cgbr
->cone
->var
[r
]) < 0)
3254 return isl_bool_error
;
3256 return context_tab_insert_div(cgbr
->tab
, pos
, div
,
3257 context_gbr_add_ineq_wrap
, context
);
3260 static int context_gbr_best_split(struct isl_context
*context
,
3261 struct isl_tab
*tab
)
3263 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3264 struct isl_tab_undo
*snap
;
3267 snap
= isl_tab_snap(cgbr
->tab
);
3268 r
= best_split(tab
, cgbr
->tab
);
3270 if (r
>= 0 && isl_tab_rollback(cgbr
->tab
, snap
) < 0)
3276 static int context_gbr_is_empty(struct isl_context
*context
)
3278 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3281 return cgbr
->tab
->empty
;
3284 struct isl_gbr_tab_undo
{
3285 struct isl_tab_undo
*tab_snap
;
3286 struct isl_tab_undo
*shifted_snap
;
3287 struct isl_tab_undo
*cone_snap
;
3290 static void *context_gbr_save(struct isl_context
*context
)
3292 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3293 struct isl_gbr_tab_undo
*snap
;
3298 snap
= isl_alloc_type(cgbr
->tab
->mat
->ctx
, struct isl_gbr_tab_undo
);
3302 snap
->tab_snap
= isl_tab_snap(cgbr
->tab
);
3303 if (isl_tab_save_samples(cgbr
->tab
) < 0)
3307 snap
->shifted_snap
= isl_tab_snap(cgbr
->shifted
);
3309 snap
->shifted_snap
= NULL
;
3312 snap
->cone_snap
= isl_tab_snap(cgbr
->cone
);
3314 snap
->cone_snap
= NULL
;
3322 static void context_gbr_restore(struct isl_context
*context
, void *save
)
3324 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3325 struct isl_gbr_tab_undo
*snap
= (struct isl_gbr_tab_undo
*)save
;
3328 if (isl_tab_rollback(cgbr
->tab
, snap
->tab_snap
) < 0)
3331 if (snap
->shifted_snap
) {
3332 if (isl_tab_rollback(cgbr
->shifted
, snap
->shifted_snap
) < 0)
3334 } else if (cgbr
->shifted
) {
3335 isl_tab_free(cgbr
->shifted
);
3336 cgbr
->shifted
= NULL
;
3339 if (snap
->cone_snap
) {
3340 if (isl_tab_rollback(cgbr
->cone
, snap
->cone_snap
) < 0)
3342 } else if (cgbr
->cone
) {
3343 isl_tab_free(cgbr
->cone
);
3352 isl_tab_free(cgbr
->tab
);
3356 static void context_gbr_discard(void *save
)
3358 struct isl_gbr_tab_undo
*snap
= (struct isl_gbr_tab_undo
*)save
;
3362 static int context_gbr_is_ok(struct isl_context
*context
)
3364 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3368 static void context_gbr_invalidate(struct isl_context
*context
)
3370 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3371 isl_tab_free(cgbr
->tab
);
3375 static __isl_null
struct isl_context
*context_gbr_free(
3376 struct isl_context
*context
)
3378 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3379 isl_tab_free(cgbr
->tab
);
3380 isl_tab_free(cgbr
->shifted
);
3381 isl_tab_free(cgbr
->cone
);
3387 struct isl_context_op isl_context_gbr_op
= {
3388 context_gbr_detect_nonnegative_parameters
,
3389 context_gbr_peek_basic_set
,
3390 context_gbr_peek_tab
,
3392 context_gbr_add_ineq
,
3393 context_gbr_ineq_sign
,
3394 context_gbr_test_ineq
,
3395 context_gbr_get_div
,
3396 context_gbr_insert_div
,
3397 context_gbr_detect_equalities
,
3398 context_gbr_best_split
,
3399 context_gbr_is_empty
,
3402 context_gbr_restore
,
3403 context_gbr_discard
,
3404 context_gbr_invalidate
,
3408 static struct isl_context
*isl_context_gbr_alloc(__isl_keep isl_basic_set
*dom
)
3410 struct isl_context_gbr
*cgbr
;
3415 cgbr
= isl_calloc_type(dom
->ctx
, struct isl_context_gbr
);
3419 cgbr
->context
.op
= &isl_context_gbr_op
;
3421 cgbr
->shifted
= NULL
;
3423 cgbr
->tab
= isl_tab_from_basic_set(dom
, 1);
3424 cgbr
->tab
= isl_tab_init_samples(cgbr
->tab
);
3427 check_gbr_integer_feasible(cgbr
);
3429 return &cgbr
->context
;
3431 cgbr
->context
.op
->free(&cgbr
->context
);
3435 /* Allocate a context corresponding to "dom".
3436 * The representation specific fields are initialized by
3437 * isl_context_lex_alloc or isl_context_gbr_alloc.
3438 * The shared "n_unknown" field is initialized to the number
3439 * of final unknown integer divisions in "dom".
3441 static struct isl_context
*isl_context_alloc(__isl_keep isl_basic_set
*dom
)
3443 struct isl_context
*context
;
3449 if (dom
->ctx
->opt
->context
== ISL_CONTEXT_LEXMIN
)
3450 context
= isl_context_lex_alloc(dom
);
3452 context
= isl_context_gbr_alloc(dom
);
3457 first
= isl_basic_set_first_unknown_div(dom
);
3459 return context
->op
->free(context
);
3460 context
->n_unknown
= isl_basic_set_dim(dom
, isl_dim_div
) - first
;
3465 /* Construct an isl_sol_map structure for accumulating the solution.
3466 * If track_empty is set, then we also keep track of the parts
3467 * of the context where there is no solution.
3468 * If max is set, then we are solving a maximization, rather than
3469 * a minimization problem, which means that the variables in the
3470 * tableau have value "M - x" rather than "M + x".
3472 static struct isl_sol
*sol_map_init(struct isl_basic_map
*bmap
,
3473 struct isl_basic_set
*dom
, int track_empty
, int max
)
3475 struct isl_sol_map
*sol_map
= NULL
;
3480 sol_map
= isl_calloc_type(bmap
->ctx
, struct isl_sol_map
);
3484 sol_map
->sol
.rational
= ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
3485 sol_map
->sol
.dec_level
.callback
.run
= &sol_dec_level_wrap
;
3486 sol_map
->sol
.dec_level
.sol
= &sol_map
->sol
;
3487 sol_map
->sol
.max
= max
;
3488 sol_map
->sol
.n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
3489 sol_map
->sol
.add
= &sol_map_add_wrap
;
3490 sol_map
->sol
.add_empty
= track_empty
? &sol_map_add_empty_wrap
: NULL
;
3491 sol_map
->sol
.free
= &sol_map_free_wrap
;
3492 sol_map
->map
= isl_map_alloc_space(isl_basic_map_get_space(bmap
), 1,
3497 sol_map
->sol
.context
= isl_context_alloc(dom
);
3498 if (!sol_map
->sol
.context
)
3502 sol_map
->empty
= isl_set_alloc_space(isl_basic_set_get_space(dom
),
3503 1, ISL_SET_DISJOINT
);
3504 if (!sol_map
->empty
)
3508 isl_basic_set_free(dom
);
3509 return &sol_map
->sol
;
3511 isl_basic_set_free(dom
);
3512 sol_map_free(sol_map
);
3516 /* Check whether all coefficients of (non-parameter) variables
3517 * are non-positive, meaning that no pivots can be performed on the row.
3519 static int is_critical(struct isl_tab
*tab
, int row
)
3522 unsigned off
= 2 + tab
->M
;
3524 for (j
= tab
->n_dead
; j
< tab
->n_col
; ++j
) {
3525 if (tab
->col_var
[j
] >= 0 &&
3526 (tab
->col_var
[j
] < tab
->n_param
||
3527 tab
->col_var
[j
] >= tab
->n_var
- tab
->n_div
))
3530 if (isl_int_is_pos(tab
->mat
->row
[row
][off
+ j
]))
3537 /* Check whether the inequality represented by vec is strict over the integers,
3538 * i.e., there are no integer values satisfying the constraint with
3539 * equality. This happens if the gcd of the coefficients is not a divisor
3540 * of the constant term. If so, scale the constraint down by the gcd
3541 * of the coefficients.
3543 static int is_strict(struct isl_vec
*vec
)
3549 isl_seq_gcd(vec
->el
+ 1, vec
->size
- 1, &gcd
);
3550 if (!isl_int_is_one(gcd
)) {
3551 strict
= !isl_int_is_divisible_by(vec
->el
[0], gcd
);
3552 isl_int_fdiv_q(vec
->el
[0], vec
->el
[0], gcd
);
3553 isl_seq_scale_down(vec
->el
+ 1, vec
->el
+ 1, gcd
, vec
->size
-1);
3560 /* Determine the sign of the given row of the main tableau.
3561 * The result is one of
3562 * isl_tab_row_pos: always non-negative; no pivot needed
3563 * isl_tab_row_neg: always non-positive; pivot
3564 * isl_tab_row_any: can be both positive and negative; split
3566 * We first handle some simple cases
3567 * - the row sign may be known already
3568 * - the row may be obviously non-negative
3569 * - the parametric constant may be equal to that of another row
3570 * for which we know the sign. This sign will be either "pos" or
3571 * "any". If it had been "neg" then we would have pivoted before.
3573 * If none of these cases hold, we check the value of the row for each
3574 * of the currently active samples. Based on the signs of these values
3575 * we make an initial determination of the sign of the row.
3577 * all zero -> unk(nown)
3578 * all non-negative -> pos
3579 * all non-positive -> neg
3580 * both negative and positive -> all
3582 * If we end up with "all", we are done.
3583 * Otherwise, we perform a check for positive and/or negative
3584 * values as follows.
3586 * samples neg unk pos
3592 * There is no special sign for "zero", because we can usually treat zero
3593 * as either non-negative or non-positive, whatever works out best.
3594 * However, if the row is "critical", meaning that pivoting is impossible
3595 * then we don't want to limp zero with the non-positive case, because
3596 * then we we would lose the solution for those values of the parameters
3597 * where the value of the row is zero. Instead, we treat 0 as non-negative
3598 * ensuring a split if the row can attain both zero and negative values.
3599 * The same happens when the original constraint was one that could not
3600 * be satisfied with equality by any integer values of the parameters.
3601 * In this case, we normalize the constraint, but then a value of zero
3602 * for the normalized constraint is actually a positive value for the
3603 * original constraint, so again we need to treat zero as non-negative.
3604 * In both these cases, we have the following decision tree instead:
3606 * all non-negative -> pos
3607 * all negative -> neg
3608 * both negative and non-negative -> all
3616 static enum isl_tab_row_sign
row_sign(struct isl_tab
*tab
,
3617 struct isl_sol
*sol
, int row
)
3619 struct isl_vec
*ineq
= NULL
;
3620 enum isl_tab_row_sign res
= isl_tab_row_unknown
;
3625 if (tab
->row_sign
[row
] != isl_tab_row_unknown
)
3626 return tab
->row_sign
[row
];
3627 if (is_obviously_nonneg(tab
, row
))
3628 return isl_tab_row_pos
;
3629 for (row2
= tab
->n_redundant
; row2
< tab
->n_row
; ++row2
) {
3630 if (tab
->row_sign
[row2
] == isl_tab_row_unknown
)
3632 if (identical_parameter_line(tab
, row
, row2
))
3633 return tab
->row_sign
[row2
];
3636 critical
= is_critical(tab
, row
);
3638 ineq
= get_row_parameter_ineq(tab
, row
);
3642 strict
= is_strict(ineq
);
3644 res
= sol
->context
->op
->ineq_sign(sol
->context
, ineq
->el
,
3645 critical
|| strict
);
3647 if (res
== isl_tab_row_unknown
|| res
== isl_tab_row_pos
) {
3648 /* test for negative values */
3650 isl_seq_neg(ineq
->el
, ineq
->el
, ineq
->size
);
3651 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3653 feasible
= sol
->context
->op
->test_ineq(sol
->context
, ineq
->el
);
3657 res
= isl_tab_row_pos
;
3659 res
= (res
== isl_tab_row_unknown
) ? isl_tab_row_neg
3661 if (res
== isl_tab_row_neg
) {
3662 isl_seq_neg(ineq
->el
, ineq
->el
, ineq
->size
);
3663 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3667 if (res
== isl_tab_row_neg
) {
3668 /* test for positive values */
3670 if (!critical
&& !strict
)
3671 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3673 feasible
= sol
->context
->op
->test_ineq(sol
->context
, ineq
->el
);
3677 res
= isl_tab_row_any
;
3684 return isl_tab_row_unknown
;
3687 static void find_solutions(struct isl_sol
*sol
, struct isl_tab
*tab
);
3689 /* Find solutions for values of the parameters that satisfy the given
3692 * We currently take a snapshot of the context tableau that is reset
3693 * when we return from this function, while we make a copy of the main
3694 * tableau, leaving the original main tableau untouched.
3695 * These are fairly arbitrary choices. Making a copy also of the context
3696 * tableau would obviate the need to undo any changes made to it later,
3697 * while taking a snapshot of the main tableau could reduce memory usage.
3698 * If we were to switch to taking a snapshot of the main tableau,
3699 * we would have to keep in mind that we need to save the row signs
3700 * and that we need to do this before saving the current basis
3701 * such that the basis has been restore before we restore the row signs.
3703 static void find_in_pos(struct isl_sol
*sol
, struct isl_tab
*tab
, isl_int
*ineq
)
3709 saved
= sol
->context
->op
->save(sol
->context
);
3711 tab
= isl_tab_dup(tab
);
3715 sol
->context
->op
->add_ineq(sol
->context
, ineq
, 0, 1);
3717 find_solutions(sol
, tab
);
3720 sol
->context
->op
->restore(sol
->context
, saved
);
3722 sol
->context
->op
->discard(saved
);
3728 /* Record the absence of solutions for those values of the parameters
3729 * that do not satisfy the given inequality with equality.
3731 static void no_sol_in_strict(struct isl_sol
*sol
,
3732 struct isl_tab
*tab
, struct isl_vec
*ineq
)
3737 if (!sol
->context
|| sol
->error
)
3739 saved
= sol
->context
->op
->save(sol
->context
);
3741 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3743 sol
->context
->op
->add_ineq(sol
->context
, ineq
->el
, 1, 0);
3752 isl_int_add_ui(ineq
->el
[0], ineq
->el
[0], 1);
3754 sol
->context
->op
->restore(sol
->context
, saved
);
3760 /* Reset all row variables that are marked to have a sign that may
3761 * be both positive and negative to have an unknown sign.
3763 static void reset_any_to_unknown(struct isl_tab
*tab
)
3767 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
3768 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
3770 if (tab
->row_sign
[row
] == isl_tab_row_any
)
3771 tab
->row_sign
[row
] = isl_tab_row_unknown
;
3775 /* Compute the lexicographic minimum of the set represented by the main
3776 * tableau "tab" within the context "sol->context_tab".
3777 * On entry the sample value of the main tableau is lexicographically
3778 * less than or equal to this lexicographic minimum.
3779 * Pivots are performed until a feasible point is found, which is then
3780 * necessarily equal to the minimum, or until the tableau is found to
3781 * be infeasible. Some pivots may need to be performed for only some
3782 * feasible values of the context tableau. If so, the context tableau
3783 * is split into a part where the pivot is needed and a part where it is not.
3785 * Whenever we enter the main loop, the main tableau is such that no
3786 * "obvious" pivots need to be performed on it, where "obvious" means
3787 * that the given row can be seen to be negative without looking at
3788 * the context tableau. In particular, for non-parametric problems,
3789 * no pivots need to be performed on the main tableau.
3790 * The caller of find_solutions is responsible for making this property
3791 * hold prior to the first iteration of the loop, while restore_lexmin
3792 * is called before every other iteration.
3794 * Inside the main loop, we first examine the signs of the rows of
3795 * the main tableau within the context of the context tableau.
3796 * If we find a row that is always non-positive for all values of
3797 * the parameters satisfying the context tableau and negative for at
3798 * least one value of the parameters, we perform the appropriate pivot
3799 * and start over. An exception is the case where no pivot can be
3800 * performed on the row. In this case, we require that the sign of
3801 * the row is negative for all values of the parameters (rather than just
3802 * non-positive). This special case is handled inside row_sign, which
3803 * will say that the row can have any sign if it determines that it can
3804 * attain both negative and zero values.
3806 * If we can't find a row that always requires a pivot, but we can find
3807 * one or more rows that require a pivot for some values of the parameters
3808 * (i.e., the row can attain both positive and negative signs), then we split
3809 * the context tableau into two parts, one where we force the sign to be
3810 * non-negative and one where we force is to be negative.
3811 * The non-negative part is handled by a recursive call (through find_in_pos).
3812 * Upon returning from this call, we continue with the negative part and
3813 * perform the required pivot.
3815 * If no such rows can be found, all rows are non-negative and we have
3816 * found a (rational) feasible point. If we only wanted a rational point
3818 * Otherwise, we check if all values of the sample point of the tableau
3819 * are integral for the variables. If so, we have found the minimal
3820 * integral point and we are done.
3821 * If the sample point is not integral, then we need to make a distinction
3822 * based on whether the constant term is non-integral or the coefficients
3823 * of the parameters. Furthermore, in order to decide how to handle
3824 * the non-integrality, we also need to know whether the coefficients
3825 * of the other columns in the tableau are integral. This leads
3826 * to the following table. The first two rows do not correspond
3827 * to a non-integral sample point and are only mentioned for completeness.
3829 * constant parameters other
3832 * int int rat | -> no problem
3834 * rat int int -> fail
3836 * rat int rat -> cut
3839 * rat rat rat | -> parametric cut
3842 * rat rat int | -> split context
3844 * If the parametric constant is completely integral, then there is nothing
3845 * to be done. If the constant term is non-integral, but all the other
3846 * coefficient are integral, then there is nothing that can be done
3847 * and the tableau has no integral solution.
3848 * If, on the other hand, one or more of the other columns have rational
3849 * coefficients, but the parameter coefficients are all integral, then
3850 * we can perform a regular (non-parametric) cut.
3851 * Finally, if there is any parameter coefficient that is non-integral,
3852 * then we need to involve the context tableau. There are two cases here.
3853 * If at least one other column has a rational coefficient, then we
3854 * can perform a parametric cut in the main tableau by adding a new
3855 * integer division in the context tableau.
3856 * If all other columns have integral coefficients, then we need to
3857 * enforce that the rational combination of parameters (c + \sum a_i y_i)/m
3858 * is always integral. We do this by introducing an integer division
3859 * q = floor((c + \sum a_i y_i)/m) and stipulating that its argument should
3860 * always be integral in the context tableau, i.e., m q = c + \sum a_i y_i.
3861 * Since q is expressed in the tableau as
3862 * c + \sum a_i y_i - m q >= 0
3863 * -c - \sum a_i y_i + m q + m - 1 >= 0
3864 * it is sufficient to add the inequality
3865 * -c - \sum a_i y_i + m q >= 0
3866 * In the part of the context where this inequality does not hold, the
3867 * main tableau is marked as being empty.
3869 static void find_solutions(struct isl_sol
*sol
, struct isl_tab
*tab
)
3871 struct isl_context
*context
;
3874 if (!tab
|| sol
->error
)
3877 context
= sol
->context
;
3881 if (context
->op
->is_empty(context
))
3884 for (r
= 0; r
>= 0 && tab
&& !tab
->empty
; r
= restore_lexmin(tab
)) {
3887 enum isl_tab_row_sign sgn
;
3891 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
3892 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
3894 sgn
= row_sign(tab
, sol
, row
);
3897 tab
->row_sign
[row
] = sgn
;
3898 if (sgn
== isl_tab_row_any
)
3900 if (sgn
== isl_tab_row_any
&& split
== -1)
3902 if (sgn
== isl_tab_row_neg
)
3905 if (row
< tab
->n_row
)
3908 struct isl_vec
*ineq
;
3910 split
= context
->op
->best_split(context
, tab
);
3913 ineq
= get_row_parameter_ineq(tab
, split
);
3917 reset_any_to_unknown(tab
);
3918 tab
->row_sign
[split
] = isl_tab_row_pos
;
3920 find_in_pos(sol
, tab
, ineq
->el
);
3921 tab
->row_sign
[split
] = isl_tab_row_neg
;
3922 isl_seq_neg(ineq
->el
, ineq
->el
, ineq
->size
);
3923 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3925 context
->op
->add_ineq(context
, ineq
->el
, 0, 1);
3933 row
= first_non_integer_row(tab
, &flags
);
3936 if (ISL_FL_ISSET(flags
, I_PAR
)) {
3937 if (ISL_FL_ISSET(flags
, I_VAR
)) {
3938 if (isl_tab_mark_empty(tab
) < 0)
3942 row
= add_cut(tab
, row
);
3943 } else if (ISL_FL_ISSET(flags
, I_VAR
)) {
3944 struct isl_vec
*div
;
3945 struct isl_vec
*ineq
;
3947 div
= get_row_split_div(tab
, row
);
3950 d
= context
->op
->get_div(context
, tab
, div
);
3954 ineq
= ineq_for_div(context
->op
->peek_basic_set(context
), d
);
3958 no_sol_in_strict(sol
, tab
, ineq
);
3959 isl_seq_neg(ineq
->el
, ineq
->el
, ineq
->size
);
3960 context
->op
->add_ineq(context
, ineq
->el
, 1, 1);
3962 if (sol
->error
|| !context
->op
->is_ok(context
))
3964 tab
= set_row_cst_to_div(tab
, row
, d
);
3965 if (context
->op
->is_empty(context
))
3968 row
= add_parametric_cut(tab
, row
, context
);
3983 /* Does "sol" contain a pair of partial solutions that could potentially
3986 * We currently only check that "sol" is not in an error state
3987 * and that there are at least two partial solutions of which the final two
3988 * are defined at the same level.
3990 static int sol_has_mergeable_solutions(struct isl_sol
*sol
)
3996 if (!sol
->partial
->next
)
3998 return sol
->partial
->level
== sol
->partial
->next
->level
;
4001 /* Compute the lexicographic minimum of the set represented by the main
4002 * tableau "tab" within the context "sol->context_tab".
4004 * As a preprocessing step, we first transfer all the purely parametric
4005 * equalities from the main tableau to the context tableau, i.e.,
4006 * parameters that have been pivoted to a row.
4007 * These equalities are ignored by the main algorithm, because the
4008 * corresponding rows may not be marked as being non-negative.
4009 * In parts of the context where the added equality does not hold,
4010 * the main tableau is marked as being empty.
4012 * Before we embark on the actual computation, we save a copy
4013 * of the context. When we return, we check if there are any
4014 * partial solutions that can potentially be merged. If so,
4015 * we perform a rollback to the initial state of the context.
4016 * The merging of partial solutions happens inside calls to
4017 * sol_dec_level that are pushed onto the undo stack of the context.
4018 * If there are no partial solutions that can potentially be merged
4019 * then the rollback is skipped as it would just be wasted effort.
4021 static void find_solutions_main(struct isl_sol
*sol
, struct isl_tab
*tab
)
4031 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
4035 if (tab
->row_var
[row
] < 0)
4037 if (tab
->row_var
[row
] >= tab
->n_param
&&
4038 tab
->row_var
[row
] < tab
->n_var
- tab
->n_div
)
4040 if (tab
->row_var
[row
] < tab
->n_param
)
4041 p
= tab
->row_var
[row
];
4043 p
= tab
->row_var
[row
]
4044 + tab
->n_param
- (tab
->n_var
- tab
->n_div
);
4046 eq
= isl_vec_alloc(tab
->mat
->ctx
, 1+tab
->n_param
+tab
->n_div
);
4049 get_row_parameter_line(tab
, row
, eq
->el
);
4050 isl_int_neg(eq
->el
[1 + p
], tab
->mat
->row
[row
][0]);
4051 eq
= isl_vec_normalize(eq
);
4054 no_sol_in_strict(sol
, tab
, eq
);
4056 isl_seq_neg(eq
->el
, eq
->el
, eq
->size
);
4058 no_sol_in_strict(sol
, tab
, eq
);
4059 isl_seq_neg(eq
->el
, eq
->el
, eq
->size
);
4061 sol
->context
->op
->add_eq(sol
->context
, eq
->el
, 1, 1);
4065 if (isl_tab_mark_redundant(tab
, row
) < 0)
4068 if (sol
->context
->op
->is_empty(sol
->context
))
4071 row
= tab
->n_redundant
- 1;
4074 saved
= sol
->context
->op
->save(sol
->context
);
4076 find_solutions(sol
, tab
);
4078 if (sol_has_mergeable_solutions(sol
))
4079 sol
->context
->op
->restore(sol
->context
, saved
);
4081 sol
->context
->op
->discard(saved
);
4092 /* Check if integer division "div" of "dom" also occurs in "bmap".
4093 * If so, return its position within the divs.
4094 * If not, return -1.
4096 static int find_context_div(struct isl_basic_map
*bmap
,
4097 struct isl_basic_set
*dom
, unsigned div
)
4100 unsigned b_dim
= isl_space_dim(bmap
->dim
, isl_dim_all
);
4101 unsigned d_dim
= isl_space_dim(dom
->dim
, isl_dim_all
);
4103 if (isl_int_is_zero(dom
->div
[div
][0]))
4105 if (isl_seq_first_non_zero(dom
->div
[div
] + 2 + d_dim
, dom
->n_div
) != -1)
4108 for (i
= 0; i
< bmap
->n_div
; ++i
) {
4109 if (isl_int_is_zero(bmap
->div
[i
][0]))
4111 if (isl_seq_first_non_zero(bmap
->div
[i
] + 2 + d_dim
,
4112 (b_dim
- d_dim
) + bmap
->n_div
) != -1)
4114 if (isl_seq_eq(bmap
->div
[i
], dom
->div
[div
], 2 + d_dim
))
4120 /* The correspondence between the variables in the main tableau,
4121 * the context tableau, and the input map and domain is as follows.
4122 * The first n_param and the last n_div variables of the main tableau
4123 * form the variables of the context tableau.
4124 * In the basic map, these n_param variables correspond to the
4125 * parameters and the input dimensions. In the domain, they correspond
4126 * to the parameters and the set dimensions.
4127 * The n_div variables correspond to the integer divisions in the domain.
4128 * To ensure that everything lines up, we may need to copy some of the
4129 * integer divisions of the domain to the map. These have to be placed
4130 * in the same order as those in the context and they have to be placed
4131 * after any other integer divisions that the map may have.
4132 * This function performs the required reordering.
4134 static struct isl_basic_map
*align_context_divs(struct isl_basic_map
*bmap
,
4135 struct isl_basic_set
*dom
)
4141 for (i
= 0; i
< dom
->n_div
; ++i
)
4142 if (find_context_div(bmap
, dom
, i
) != -1)
4144 other
= bmap
->n_div
- common
;
4145 if (dom
->n_div
- common
> 0) {
4146 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
4147 dom
->n_div
- common
, 0, 0);
4151 for (i
= 0; i
< dom
->n_div
; ++i
) {
4152 int pos
= find_context_div(bmap
, dom
, i
);
4154 pos
= isl_basic_map_alloc_div(bmap
);
4157 isl_int_set_si(bmap
->div
[pos
][0], 0);
4159 if (pos
!= other
+ i
)
4160 isl_basic_map_swap_div(bmap
, pos
, other
+ i
);
4164 isl_basic_map_free(bmap
);
4168 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
4169 * some obvious symmetries.
4171 * We make sure the divs in the domain are properly ordered,
4172 * because they will be added one by one in the given order
4173 * during the construction of the solution map.
4175 static struct isl_sol
*basic_map_partial_lexopt_base_sol(
4176 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
4177 __isl_give isl_set
**empty
, int max
,
4178 struct isl_sol
*(*init
)(__isl_keep isl_basic_map
*bmap
,
4179 __isl_take isl_basic_set
*dom
, int track_empty
, int max
))
4181 struct isl_tab
*tab
;
4182 struct isl_sol
*sol
= NULL
;
4183 struct isl_context
*context
;
4186 dom
= isl_basic_set_order_divs(dom
);
4187 bmap
= align_context_divs(bmap
, dom
);
4189 sol
= init(bmap
, dom
, !!empty
, max
);
4193 context
= sol
->context
;
4194 if (isl_basic_set_plain_is_empty(context
->op
->peek_basic_set(context
)))
4196 else if (isl_basic_map_plain_is_empty(bmap
)) {
4199 isl_basic_set_copy(context
->op
->peek_basic_set(context
)));
4201 tab
= tab_for_lexmin(bmap
,
4202 context
->op
->peek_basic_set(context
), 1, max
);
4203 tab
= context
->op
->detect_nonnegative_parameters(context
, tab
);
4204 find_solutions_main(sol
, tab
);
4209 isl_basic_map_free(bmap
);
4213 isl_basic_map_free(bmap
);
4217 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
4218 * some obvious symmetries.
4220 * We call basic_map_partial_lexopt_base_sol and extract the results.
4222 static __isl_give isl_map
*basic_map_partial_lexopt_base(
4223 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
4224 __isl_give isl_set
**empty
, int max
)
4226 isl_map
*result
= NULL
;
4227 struct isl_sol
*sol
;
4228 struct isl_sol_map
*sol_map
;
4230 sol
= basic_map_partial_lexopt_base_sol(bmap
, dom
, empty
, max
,
4234 sol_map
= (struct isl_sol_map
*) sol
;
4236 result
= isl_map_copy(sol_map
->map
);
4238 *empty
= isl_set_copy(sol_map
->empty
);
4239 sol_free(&sol_map
->sol
);
4243 /* Return a count of the number of occurrences of the "n" first
4244 * variables in the inequality constraints of "bmap".
4246 static __isl_give
int *count_occurrences(__isl_keep isl_basic_map
*bmap
,
4255 ctx
= isl_basic_map_get_ctx(bmap
);
4256 occurrences
= isl_calloc_array(ctx
, int, n
);
4260 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
4261 for (j
= 0; j
< n
; ++j
) {
4262 if (!isl_int_is_zero(bmap
->ineq
[i
][1 + j
]))
4270 /* Do all of the "n" variables with non-zero coefficients in "c"
4271 * occur in exactly a single constraint.
4272 * "occurrences" is an array of length "n" containing the number
4273 * of occurrences of each of the variables in the inequality constraints.
4275 static int single_occurrence(int n
, isl_int
*c
, int *occurrences
)
4279 for (i
= 0; i
< n
; ++i
) {
4280 if (isl_int_is_zero(c
[i
]))
4282 if (occurrences
[i
] != 1)
4289 /* Do all of the "n" initial variables that occur in inequality constraint
4290 * "ineq" of "bmap" only occur in that constraint?
4292 static int all_single_occurrence(__isl_keep isl_basic_map
*bmap
, int ineq
,
4297 for (i
= 0; i
< n
; ++i
) {
4298 if (isl_int_is_zero(bmap
->ineq
[ineq
][1 + i
]))
4300 for (j
= 0; j
< bmap
->n_ineq
; ++j
) {
4303 if (!isl_int_is_zero(bmap
->ineq
[j
][1 + i
]))
4311 /* Structure used during detection of parallel constraints.
4312 * n_in: number of "input" variables: isl_dim_param + isl_dim_in
4313 * n_out: number of "output" variables: isl_dim_out + isl_dim_div
4314 * val: the coefficients of the output variables
4316 struct isl_constraint_equal_info
{
4317 isl_basic_map
*bmap
;
4323 /* Check whether the coefficients of the output variables
4324 * of the constraint in "entry" are equal to info->val.
4326 static int constraint_equal(const void *entry
, const void *val
)
4328 isl_int
**row
= (isl_int
**)entry
;
4329 const struct isl_constraint_equal_info
*info
= val
;
4331 return isl_seq_eq((*row
) + 1 + info
->n_in
, info
->val
, info
->n_out
);
4334 /* Check whether "bmap" has a pair of constraints that have
4335 * the same coefficients for the output variables.
4336 * Note that the coefficients of the existentially quantified
4337 * variables need to be zero since the existentially quantified
4338 * of the result are usually not the same as those of the input.
4339 * Furthermore, check that each of the input variables that occur
4340 * in those constraints does not occur in any other constraint.
4341 * If so, return 1 and return the row indices of the two constraints
4342 * in *first and *second.
4344 static int parallel_constraints(__isl_keep isl_basic_map
*bmap
,
4345 int *first
, int *second
)
4349 int *occurrences
= NULL
;
4350 struct isl_hash_table
*table
= NULL
;
4351 struct isl_hash_table_entry
*entry
;
4352 struct isl_constraint_equal_info info
;
4356 ctx
= isl_basic_map_get_ctx(bmap
);
4357 table
= isl_hash_table_alloc(ctx
, bmap
->n_ineq
);
4361 info
.n_in
= isl_basic_map_dim(bmap
, isl_dim_param
) +
4362 isl_basic_map_dim(bmap
, isl_dim_in
);
4363 occurrences
= count_occurrences(bmap
, info
.n_in
);
4364 if (info
.n_in
&& !occurrences
)
4367 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4368 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4369 info
.n_out
= n_out
+ n_div
;
4370 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
4373 info
.val
= bmap
->ineq
[i
] + 1 + info
.n_in
;
4374 if (isl_seq_first_non_zero(info
.val
, n_out
) < 0)
4376 if (isl_seq_first_non_zero(info
.val
+ n_out
, n_div
) >= 0)
4378 if (!single_occurrence(info
.n_in
, bmap
->ineq
[i
] + 1,
4381 hash
= isl_seq_get_hash(info
.val
, info
.n_out
);
4382 entry
= isl_hash_table_find(ctx
, table
, hash
,
4383 constraint_equal
, &info
, 1);
4388 entry
->data
= &bmap
->ineq
[i
];
4391 if (i
< bmap
->n_ineq
) {
4392 *first
= ((isl_int
**)entry
->data
) - bmap
->ineq
;
4396 isl_hash_table_free(ctx
, table
);
4399 return i
< bmap
->n_ineq
;
4401 isl_hash_table_free(ctx
, table
);
4406 /* Given a set of upper bounds in "var", add constraints to "bset"
4407 * that make the i-th bound smallest.
4409 * In particular, if there are n bounds b_i, then add the constraints
4411 * b_i <= b_j for j > i
4412 * b_i < b_j for j < i
4414 static __isl_give isl_basic_set
*select_minimum(__isl_take isl_basic_set
*bset
,
4415 __isl_keep isl_mat
*var
, int i
)
4420 ctx
= isl_mat_get_ctx(var
);
4422 for (j
= 0; j
< var
->n_row
; ++j
) {
4425 k
= isl_basic_set_alloc_inequality(bset
);
4428 isl_seq_combine(bset
->ineq
[k
], ctx
->one
, var
->row
[j
],
4429 ctx
->negone
, var
->row
[i
], var
->n_col
);
4430 isl_int_set_si(bset
->ineq
[k
][var
->n_col
], 0);
4432 isl_int_sub_ui(bset
->ineq
[k
][0], bset
->ineq
[k
][0], 1);
4435 bset
= isl_basic_set_finalize(bset
);
4439 isl_basic_set_free(bset
);
4443 /* Given a set of upper bounds on the last "input" variable m,
4444 * construct a set that assigns the minimal upper bound to m, i.e.,
4445 * construct a set that divides the space into cells where one
4446 * of the upper bounds is smaller than all the others and assign
4447 * this upper bound to m.
4449 * In particular, if there are n bounds b_i, then the result
4450 * consists of n basic sets, each one of the form
4453 * b_i <= b_j for j > i
4454 * b_i < b_j for j < i
4456 static __isl_give isl_set
*set_minimum(__isl_take isl_space
*dim
,
4457 __isl_take isl_mat
*var
)
4460 isl_basic_set
*bset
= NULL
;
4461 isl_set
*set
= NULL
;
4466 set
= isl_set_alloc_space(isl_space_copy(dim
),
4467 var
->n_row
, ISL_SET_DISJOINT
);
4469 for (i
= 0; i
< var
->n_row
; ++i
) {
4470 bset
= isl_basic_set_alloc_space(isl_space_copy(dim
), 0,
4472 k
= isl_basic_set_alloc_equality(bset
);
4475 isl_seq_cpy(bset
->eq
[k
], var
->row
[i
], var
->n_col
);
4476 isl_int_set_si(bset
->eq
[k
][var
->n_col
], -1);
4477 bset
= select_minimum(bset
, var
, i
);
4478 set
= isl_set_add_basic_set(set
, bset
);
4481 isl_space_free(dim
);
4485 isl_basic_set_free(bset
);
4487 isl_space_free(dim
);
4492 /* Given that the last input variable of "bmap" represents the minimum
4493 * of the bounds in "cst", check whether we need to split the domain
4494 * based on which bound attains the minimum.
4496 * A split is needed when the minimum appears in an integer division
4497 * or in an equality. Otherwise, it is only needed if it appears in
4498 * an upper bound that is different from the upper bounds on which it
4501 static int need_split_basic_map(__isl_keep isl_basic_map
*bmap
,
4502 __isl_keep isl_mat
*cst
)
4508 pos
= cst
->n_col
- 1;
4509 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4511 for (i
= 0; i
< bmap
->n_div
; ++i
)
4512 if (!isl_int_is_zero(bmap
->div
[i
][2 + pos
]))
4515 for (i
= 0; i
< bmap
->n_eq
; ++i
)
4516 if (!isl_int_is_zero(bmap
->eq
[i
][1 + pos
]))
4519 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
4520 if (isl_int_is_nonneg(bmap
->ineq
[i
][1 + pos
]))
4522 if (!isl_int_is_negone(bmap
->ineq
[i
][1 + pos
]))
4524 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + pos
+ 1,
4525 total
- pos
- 1) >= 0)
4528 for (j
= 0; j
< cst
->n_row
; ++j
)
4529 if (isl_seq_eq(bmap
->ineq
[i
], cst
->row
[j
], cst
->n_col
))
4531 if (j
>= cst
->n_row
)
4538 /* Given that the last set variable of "bset" represents the minimum
4539 * of the bounds in "cst", check whether we need to split the domain
4540 * based on which bound attains the minimum.
4542 * We simply call need_split_basic_map here. This is safe because
4543 * the position of the minimum is computed from "cst" and not
4546 static int need_split_basic_set(__isl_keep isl_basic_set
*bset
,
4547 __isl_keep isl_mat
*cst
)
4549 return need_split_basic_map((isl_basic_map
*)bset
, cst
);
4552 /* Given that the last set variable of "set" represents the minimum
4553 * of the bounds in "cst", check whether we need to split the domain
4554 * based on which bound attains the minimum.
4556 static int need_split_set(__isl_keep isl_set
*set
, __isl_keep isl_mat
*cst
)
4560 for (i
= 0; i
< set
->n
; ++i
)
4561 if (need_split_basic_set(set
->p
[i
], cst
))
4567 /* Given a set of which the last set variable is the minimum
4568 * of the bounds in "cst", split each basic set in the set
4569 * in pieces where one of the bounds is (strictly) smaller than the others.
4570 * This subdivision is given in "min_expr".
4571 * The variable is subsequently projected out.
4573 * We only do the split when it is needed.
4574 * For example if the last input variable m = min(a,b) and the only
4575 * constraints in the given basic set are lower bounds on m,
4576 * i.e., l <= m = min(a,b), then we can simply project out m
4577 * to obtain l <= a and l <= b, without having to split on whether
4578 * m is equal to a or b.
4580 static __isl_give isl_set
*split(__isl_take isl_set
*empty
,
4581 __isl_take isl_set
*min_expr
, __isl_take isl_mat
*cst
)
4588 if (!empty
|| !min_expr
|| !cst
)
4591 n_in
= isl_set_dim(empty
, isl_dim_set
);
4592 dim
= isl_set_get_space(empty
);
4593 dim
= isl_space_drop_dims(dim
, isl_dim_set
, n_in
- 1, 1);
4594 res
= isl_set_empty(dim
);
4596 for (i
= 0; i
< empty
->n
; ++i
) {
4599 set
= isl_set_from_basic_set(isl_basic_set_copy(empty
->p
[i
]));
4600 if (need_split_basic_set(empty
->p
[i
], cst
))
4601 set
= isl_set_intersect(set
, isl_set_copy(min_expr
));
4602 set
= isl_set_remove_dims(set
, isl_dim_set
, n_in
- 1, 1);
4604 res
= isl_set_union_disjoint(res
, set
);
4607 isl_set_free(empty
);
4608 isl_set_free(min_expr
);
4612 isl_set_free(empty
);
4613 isl_set_free(min_expr
);
4618 /* Given a map of which the last input variable is the minimum
4619 * of the bounds in "cst", split each basic set in the set
4620 * in pieces where one of the bounds is (strictly) smaller than the others.
4621 * This subdivision is given in "min_expr".
4622 * The variable is subsequently projected out.
4624 * The implementation is essentially the same as that of "split".
4626 static __isl_give isl_map
*split_domain(__isl_take isl_map
*opt
,
4627 __isl_take isl_set
*min_expr
, __isl_take isl_mat
*cst
)
4634 if (!opt
|| !min_expr
|| !cst
)
4637 n_in
= isl_map_dim(opt
, isl_dim_in
);
4638 dim
= isl_map_get_space(opt
);
4639 dim
= isl_space_drop_dims(dim
, isl_dim_in
, n_in
- 1, 1);
4640 res
= isl_map_empty(dim
);
4642 for (i
= 0; i
< opt
->n
; ++i
) {
4645 map
= isl_map_from_basic_map(isl_basic_map_copy(opt
->p
[i
]));
4646 if (need_split_basic_map(opt
->p
[i
], cst
))
4647 map
= isl_map_intersect_domain(map
,
4648 isl_set_copy(min_expr
));
4649 map
= isl_map_remove_dims(map
, isl_dim_in
, n_in
- 1, 1);
4651 res
= isl_map_union_disjoint(res
, map
);
4655 isl_set_free(min_expr
);
4660 isl_set_free(min_expr
);
4665 static __isl_give isl_map
*basic_map_partial_lexopt(
4666 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
4667 __isl_give isl_set
**empty
, int max
);
4669 /* This function is called from basic_map_partial_lexopt_symm.
4670 * The last variable of "bmap" and "dom" corresponds to the minimum
4671 * of the bounds in "cst". "map_space" is the space of the original
4672 * input relation (of basic_map_partial_lexopt_symm) and "set_space"
4673 * is the space of the original domain.
4675 * We recursively call basic_map_partial_lexopt and then plug in
4676 * the definition of the minimum in the result.
4678 static __isl_give isl_map
*basic_map_partial_lexopt_symm_core(
4679 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
4680 __isl_give isl_set
**empty
, int max
, __isl_take isl_mat
*cst
,
4681 __isl_take isl_space
*map_space
, __isl_take isl_space
*set_space
)
4686 min_expr
= set_minimum(isl_basic_set_get_space(dom
), isl_mat_copy(cst
));
4688 opt
= basic_map_partial_lexopt(bmap
, dom
, empty
, max
);
4691 *empty
= split(*empty
,
4692 isl_set_copy(min_expr
), isl_mat_copy(cst
));
4693 *empty
= isl_set_reset_space(*empty
, set_space
);
4696 opt
= split_domain(opt
, min_expr
, cst
);
4697 opt
= isl_map_reset_space(opt
, map_space
);
4702 /* Extract a domain from "bmap" for the purpose of computing
4703 * a lexicographic optimum.
4705 * This function is only called when the caller wants to compute a full
4706 * lexicographic optimum, i.e., without specifying a domain. In this case,
4707 * the caller is not interested in the part of the domain space where
4708 * there is no solution and the domain can be initialized to those constraints
4709 * of "bmap" that only involve the parameters and the input dimensions.
4710 * This relieves the parametric programming engine from detecting those
4711 * inequalities and transferring them to the context. More importantly,
4712 * it ensures that those inequalities are transferred first and not
4713 * intermixed with inequalities that actually split the domain.
4715 * If the caller does not require the absence of existentially quantified
4716 * variables in the result (i.e., if ISL_OPT_QE is not set in "flags"),
4717 * then the actual domain of "bmap" can be used. This ensures that
4718 * the domain does not need to be split at all just to separate out
4719 * pieces of the domain that do not have a solution from piece that do.
4720 * This domain cannot be used in general because it may involve
4721 * (unknown) existentially quantified variables which will then also
4722 * appear in the solution.
4724 static __isl_give isl_basic_set
*extract_domain(__isl_keep isl_basic_map
*bmap
,
4730 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4731 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4732 bmap
= isl_basic_map_copy(bmap
);
4733 if (ISL_FL_ISSET(flags
, ISL_OPT_QE
)) {
4734 bmap
= isl_basic_map_drop_constraints_involving_dims(bmap
,
4735 isl_dim_div
, 0, n_div
);
4736 bmap
= isl_basic_map_drop_constraints_involving_dims(bmap
,
4737 isl_dim_out
, 0, n_out
);
4739 return isl_basic_map_domain(bmap
);
4743 #define TYPE isl_map
4746 #include "isl_tab_lexopt_templ.c"
4748 struct isl_sol_for
{
4750 int (*fn
)(__isl_take isl_basic_set
*dom
,
4751 __isl_take isl_aff_list
*list
, void *user
);
4755 static void sol_for_free(struct isl_sol_for
*sol_for
)
4759 if (sol_for
->sol
.context
)
4760 sol_for
->sol
.context
->op
->free(sol_for
->sol
.context
);
4764 static void sol_for_free_wrap(struct isl_sol
*sol
)
4766 sol_for_free((struct isl_sol_for
*)sol
);
4769 /* Add the solution identified by the tableau and the context tableau.
4771 * See documentation of sol_add for more details.
4773 * Instead of constructing a basic map, this function calls a user
4774 * defined function with the current context as a basic set and
4775 * a list of affine expressions representing the relation between
4776 * the input and output. The space over which the affine expressions
4777 * are defined is the same as that of the domain. The number of
4778 * affine expressions in the list is equal to the number of output variables.
4780 static void sol_for_add(struct isl_sol_for
*sol
,
4781 struct isl_basic_set
*dom
, struct isl_mat
*M
)
4785 isl_local_space
*ls
;
4789 if (sol
->sol
.error
|| !dom
|| !M
)
4792 ctx
= isl_basic_set_get_ctx(dom
);
4793 ls
= isl_basic_set_get_local_space(dom
);
4794 list
= isl_aff_list_alloc(ctx
, M
->n_row
- 1);
4795 for (i
= 1; i
< M
->n_row
; ++i
) {
4796 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
4798 isl_int_set(aff
->v
->el
[0], M
->row
[0][0]);
4799 isl_seq_cpy(aff
->v
->el
+ 1, M
->row
[i
], M
->n_col
);
4801 aff
= isl_aff_normalize(aff
);
4802 list
= isl_aff_list_add(list
, aff
);
4804 isl_local_space_free(ls
);
4806 dom
= isl_basic_set_finalize(dom
);
4808 if (sol
->fn(isl_basic_set_copy(dom
), list
, sol
->user
) < 0)
4811 isl_basic_set_free(dom
);
4815 isl_basic_set_free(dom
);
4820 static void sol_for_add_wrap(struct isl_sol
*sol
,
4821 struct isl_basic_set
*dom
, struct isl_mat
*M
)
4823 sol_for_add((struct isl_sol_for
*)sol
, dom
, M
);
4826 static struct isl_sol_for
*sol_for_init(struct isl_basic_map
*bmap
, int max
,
4827 int (*fn
)(__isl_take isl_basic_set
*dom
, __isl_take isl_aff_list
*list
,
4831 struct isl_sol_for
*sol_for
= NULL
;
4833 struct isl_basic_set
*dom
= NULL
;
4835 sol_for
= isl_calloc_type(bmap
->ctx
, struct isl_sol_for
);
4839 dom_dim
= isl_space_domain(isl_space_copy(bmap
->dim
));
4840 dom
= isl_basic_set_universe(dom_dim
);
4842 sol_for
->sol
.rational
= ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
4843 sol_for
->sol
.dec_level
.callback
.run
= &sol_dec_level_wrap
;
4844 sol_for
->sol
.dec_level
.sol
= &sol_for
->sol
;
4846 sol_for
->user
= user
;
4847 sol_for
->sol
.max
= max
;
4848 sol_for
->sol
.n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4849 sol_for
->sol
.add
= &sol_for_add_wrap
;
4850 sol_for
->sol
.add_empty
= NULL
;
4851 sol_for
->sol
.free
= &sol_for_free_wrap
;
4853 sol_for
->sol
.context
= isl_context_alloc(dom
);
4854 if (!sol_for
->sol
.context
)
4857 isl_basic_set_free(dom
);
4860 isl_basic_set_free(dom
);
4861 sol_for_free(sol_for
);
4865 static void sol_for_find_solutions(struct isl_sol_for
*sol_for
,
4866 struct isl_tab
*tab
)
4868 find_solutions_main(&sol_for
->sol
, tab
);
4871 int isl_basic_map_foreach_lexopt(__isl_keep isl_basic_map
*bmap
, int max
,
4872 int (*fn
)(__isl_take isl_basic_set
*dom
, __isl_take isl_aff_list
*list
,
4876 struct isl_sol_for
*sol_for
= NULL
;
4878 bmap
= isl_basic_map_copy(bmap
);
4879 bmap
= isl_basic_map_detect_equalities(bmap
);
4883 sol_for
= sol_for_init(bmap
, max
, fn
, user
);
4887 if (isl_basic_map_plain_is_empty(bmap
))
4890 struct isl_tab
*tab
;
4891 struct isl_context
*context
= sol_for
->sol
.context
;
4892 tab
= tab_for_lexmin(bmap
,
4893 context
->op
->peek_basic_set(context
), 1, max
);
4894 tab
= context
->op
->detect_nonnegative_parameters(context
, tab
);
4895 sol_for_find_solutions(sol_for
, tab
);
4896 if (sol_for
->sol
.error
)
4900 sol_free(&sol_for
->sol
);
4901 isl_basic_map_free(bmap
);
4904 sol_free(&sol_for
->sol
);
4905 isl_basic_map_free(bmap
);
4909 int isl_basic_set_foreach_lexopt(__isl_keep isl_basic_set
*bset
, int max
,
4910 int (*fn
)(__isl_take isl_basic_set
*dom
, __isl_take isl_aff_list
*list
,
4914 return isl_basic_map_foreach_lexopt(bset
, max
, fn
, user
);
4917 /* Check if the given sequence of len variables starting at pos
4918 * represents a trivial (i.e., zero) solution.
4919 * The variables are assumed to be non-negative and to come in pairs,
4920 * with each pair representing a variable of unrestricted sign.
4921 * The solution is trivial if each such pair in the sequence consists
4922 * of two identical values, meaning that the variable being represented
4925 static int region_is_trivial(struct isl_tab
*tab
, int pos
, int len
)
4932 for (i
= 0; i
< len
; i
+= 2) {
4936 neg_row
= tab
->var
[pos
+ i
].is_row
?
4937 tab
->var
[pos
+ i
].index
: -1;
4938 pos_row
= tab
->var
[pos
+ i
+ 1].is_row
?
4939 tab
->var
[pos
+ i
+ 1].index
: -1;
4942 isl_int_is_zero(tab
->mat
->row
[neg_row
][1])) &&
4944 isl_int_is_zero(tab
->mat
->row
[pos_row
][1])))
4947 if (neg_row
< 0 || pos_row
< 0)
4949 if (isl_int_ne(tab
->mat
->row
[neg_row
][1],
4950 tab
->mat
->row
[pos_row
][1]))
4957 /* Return the index of the first trivial region or -1 if all regions
4960 static int first_trivial_region(struct isl_tab
*tab
,
4961 int n_region
, struct isl_region
*region
)
4965 for (i
= 0; i
< n_region
; ++i
) {
4966 if (region_is_trivial(tab
, region
[i
].pos
, region
[i
].len
))
4973 /* Check if the solution is optimal, i.e., whether the first
4974 * n_op entries are zero.
4976 static int is_optimal(__isl_keep isl_vec
*sol
, int n_op
)
4980 for (i
= 0; i
< n_op
; ++i
)
4981 if (!isl_int_is_zero(sol
->el
[1 + i
]))
4986 /* Add constraints to "tab" that ensure that any solution is significantly
4987 * better than that represented by "sol". That is, find the first
4988 * relevant (within first n_op) non-zero coefficient and force it (along
4989 * with all previous coefficients) to be zero.
4990 * If the solution is already optimal (all relevant coefficients are zero),
4991 * then just mark the table as empty.
4993 * This function assumes that at least 2 * n_op more rows and at least
4994 * 2 * n_op more elements in the constraint array are available in the tableau.
4996 static int force_better_solution(struct isl_tab
*tab
,
4997 __isl_keep isl_vec
*sol
, int n_op
)
5006 for (i
= 0; i
< n_op
; ++i
)
5007 if (!isl_int_is_zero(sol
->el
[1 + i
]))
5011 if (isl_tab_mark_empty(tab
) < 0)
5016 ctx
= isl_vec_get_ctx(sol
);
5017 v
= isl_vec_alloc(ctx
, 1 + tab
->n_var
);
5021 for (; i
>= 0; --i
) {
5023 isl_int_set_si(v
->el
[1 + i
], -1);
5024 if (add_lexmin_eq(tab
, v
->el
) < 0)
5035 struct isl_trivial
{
5039 struct isl_tab_undo
*snap
;
5042 /* Return the lexicographically smallest non-trivial solution of the
5043 * given ILP problem.
5045 * All variables are assumed to be non-negative.
5047 * n_op is the number of initial coordinates to optimize.
5048 * That is, once a solution has been found, we will only continue looking
5049 * for solution that result in significantly better values for those
5050 * initial coordinates. That is, we only continue looking for solutions
5051 * that increase the number of initial zeros in this sequence.
5053 * A solution is non-trivial, if it is non-trivial on each of the
5054 * specified regions. Each region represents a sequence of pairs
5055 * of variables. A solution is non-trivial on such a region if
5056 * at least one of these pairs consists of different values, i.e.,
5057 * such that the non-negative variable represented by the pair is non-zero.
5059 * Whenever a conflict is encountered, all constraints involved are
5060 * reported to the caller through a call to "conflict".
5062 * We perform a simple branch-and-bound backtracking search.
5063 * Each level in the search represents initially trivial region that is forced
5064 * to be non-trivial.
5065 * At each level we consider n cases, where n is the length of the region.
5066 * In terms of the n/2 variables of unrestricted signs being encoded by
5067 * the region, we consider the cases
5070 * x_0 = 0 and x_1 >= 1
5071 * x_0 = 0 and x_1 <= -1
5072 * x_0 = 0 and x_1 = 0 and x_2 >= 1
5073 * x_0 = 0 and x_1 = 0 and x_2 <= -1
5075 * The cases are considered in this order, assuming that each pair
5076 * x_i_a x_i_b represents the value x_i_b - x_i_a.
5077 * That is, x_0 >= 1 is enforced by adding the constraint
5078 * x_0_b - x_0_a >= 1
5080 __isl_give isl_vec
*isl_tab_basic_set_non_trivial_lexmin(
5081 __isl_take isl_basic_set
*bset
, int n_op
, int n_region
,
5082 struct isl_region
*region
,
5083 int (*conflict
)(int con
, void *user
), void *user
)
5089 isl_vec
*sol
= NULL
;
5090 struct isl_tab
*tab
;
5091 struct isl_trivial
*triv
= NULL
;
5097 ctx
= isl_basic_set_get_ctx(bset
);
5098 sol
= isl_vec_alloc(ctx
, 0);
5100 tab
= tab_for_lexmin(bset
, NULL
, 0, 0);
5103 tab
->conflict
= conflict
;
5104 tab
->conflict_user
= user
;
5106 v
= isl_vec_alloc(ctx
, 1 + tab
->n_var
);
5107 triv
= isl_calloc_array(ctx
, struct isl_trivial
, n_region
);
5108 if (!v
|| (n_region
&& !triv
))
5114 while (level
>= 0) {
5118 tab
= cut_to_integer_lexmin(tab
, CUT_ONE
);
5123 r
= first_trivial_region(tab
, n_region
, region
);
5125 for (i
= 0; i
< level
; ++i
)
5128 sol
= isl_tab_get_sample_value(tab
);
5131 if (is_optimal(sol
, n_op
))
5135 if (level
>= n_region
)
5136 isl_die(ctx
, isl_error_internal
,
5137 "nesting level too deep", goto error
);
5138 if (isl_tab_extend_cons(tab
,
5139 2 * region
[r
].len
+ 2 * n_op
) < 0)
5141 triv
[level
].region
= r
;
5142 triv
[level
].side
= 0;
5145 r
= triv
[level
].region
;
5146 side
= triv
[level
].side
;
5147 base
= 2 * (side
/2);
5149 if (side
>= region
[r
].len
) {
5154 if (isl_tab_rollback(tab
, triv
[level
].snap
) < 0)
5159 if (triv
[level
].update
) {
5160 if (force_better_solution(tab
, sol
, n_op
) < 0)
5162 triv
[level
].update
= 0;
5165 if (side
== base
&& base
>= 2) {
5166 for (j
= base
- 2; j
< base
; ++j
) {
5168 isl_int_set_si(v
->el
[1 + region
[r
].pos
+ j
], 1);
5169 if (add_lexmin_eq(tab
, v
->el
) < 0)
5174 triv
[level
].snap
= isl_tab_snap(tab
);
5175 if (isl_tab_push_basis(tab
) < 0)
5179 isl_int_set_si(v
->el
[0], -1);
5180 isl_int_set_si(v
->el
[1 + region
[r
].pos
+ side
], -1);
5181 isl_int_set_si(v
->el
[1 + region
[r
].pos
+ (side
^ 1)], 1);
5182 tab
= add_lexmin_ineq(tab
, v
->el
);
5192 isl_basic_set_free(bset
);
5199 isl_basic_set_free(bset
);
5204 /* Wrapper for a tableau that is used for computing
5205 * the lexicographically smallest rational point of a non-negative set.
5206 * This point is represented by the sample value of "tab",
5207 * unless "tab" is empty.
5209 struct isl_tab_lexmin
{
5211 struct isl_tab
*tab
;
5214 /* Free "tl" and return NULL.
5216 __isl_null isl_tab_lexmin
*isl_tab_lexmin_free(__isl_take isl_tab_lexmin
*tl
)
5220 isl_ctx_deref(tl
->ctx
);
5221 isl_tab_free(tl
->tab
);
5227 /* Construct an isl_tab_lexmin for computing
5228 * the lexicographically smallest rational point in "bset",
5229 * assuming that all variables are non-negative.
5231 __isl_give isl_tab_lexmin
*isl_tab_lexmin_from_basic_set(
5232 __isl_take isl_basic_set
*bset
)
5240 ctx
= isl_basic_set_get_ctx(bset
);
5241 tl
= isl_calloc_type(ctx
, struct isl_tab_lexmin
);
5246 tl
->tab
= tab_for_lexmin(bset
, NULL
, 0, 0);
5247 isl_basic_set_free(bset
);
5249 return isl_tab_lexmin_free(tl
);
5252 isl_basic_set_free(bset
);
5253 isl_tab_lexmin_free(tl
);
5257 /* Return the dimension of the set represented by "tl".
5259 int isl_tab_lexmin_dim(__isl_keep isl_tab_lexmin
*tl
)
5261 return tl
? tl
->tab
->n_var
: -1;
5264 /* Add the equality with coefficients "eq" to "tl", updating the optimal
5265 * solution if needed.
5266 * The equality is added as two opposite inequality constraints.
5268 __isl_give isl_tab_lexmin
*isl_tab_lexmin_add_eq(__isl_take isl_tab_lexmin
*tl
,
5274 return isl_tab_lexmin_free(tl
);
5276 if (isl_tab_extend_cons(tl
->tab
, 2) < 0)
5277 return isl_tab_lexmin_free(tl
);
5278 n_var
= tl
->tab
->n_var
;
5279 isl_seq_neg(eq
, eq
, 1 + n_var
);
5280 tl
->tab
= add_lexmin_ineq(tl
->tab
, eq
);
5281 isl_seq_neg(eq
, eq
, 1 + n_var
);
5282 tl
->tab
= add_lexmin_ineq(tl
->tab
, eq
);
5285 return isl_tab_lexmin_free(tl
);
5290 /* Return the lexicographically smallest rational point in the basic set
5291 * from which "tl" was constructed.
5292 * If the original input was empty, then return a zero-length vector.
5294 __isl_give isl_vec
*isl_tab_lexmin_get_solution(__isl_keep isl_tab_lexmin
*tl
)
5299 return isl_vec_alloc(tl
->ctx
, 0);
5301 return isl_tab_get_sample_value(tl
->tab
);
5304 /* Return the lexicographically smallest rational point in "bset",
5305 * assuming that all variables are non-negative.
5306 * If "bset" is empty, then return a zero-length vector.
5308 __isl_give isl_vec
*isl_tab_basic_set_non_neg_lexmin(
5309 __isl_take isl_basic_set
*bset
)
5314 tl
= isl_tab_lexmin_from_basic_set(bset
);
5315 sol
= isl_tab_lexmin_get_solution(tl
);
5316 isl_tab_lexmin_free(tl
);
5320 struct isl_sol_pma
{
5322 isl_pw_multi_aff
*pma
;
5326 static void sol_pma_free(struct isl_sol_pma
*sol_pma
)
5330 if (sol_pma
->sol
.context
)
5331 sol_pma
->sol
.context
->op
->free(sol_pma
->sol
.context
);
5332 isl_pw_multi_aff_free(sol_pma
->pma
);
5333 isl_set_free(sol_pma
->empty
);
5337 /* This function is called for parts of the context where there is
5338 * no solution, with "bset" corresponding to the context tableau.
5339 * Simply add the basic set to the set "empty".
5341 static void sol_pma_add_empty(struct isl_sol_pma
*sol
,
5342 __isl_take isl_basic_set
*bset
)
5344 if (!bset
|| !sol
->empty
)
5347 sol
->empty
= isl_set_grow(sol
->empty
, 1);
5348 bset
= isl_basic_set_simplify(bset
);
5349 bset
= isl_basic_set_finalize(bset
);
5350 sol
->empty
= isl_set_add_basic_set(sol
->empty
, bset
);
5355 isl_basic_set_free(bset
);
5359 /* Set the affine expressions in "ma" according to the rows in "M", which
5360 * are defined over the local space "ls".
5361 * The matrix "M" may have extra (zero) columns beyond the number
5362 * of variables in "ls".
5364 static __isl_give isl_multi_aff
*set_from_affine_matrix(
5365 __isl_take isl_multi_aff
*ma
, __isl_take isl_local_space
*ls
,
5366 __isl_take isl_mat
*M
)
5371 if (!ma
|| !ls
|| !M
)
5374 dim
= isl_local_space_dim(ls
, isl_dim_all
);
5375 for (i
= 1; i
< M
->n_row
; ++i
) {
5376 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
5378 isl_int_set(aff
->v
->el
[0], M
->row
[0][0]);
5379 isl_seq_cpy(aff
->v
->el
+ 1, M
->row
[i
], 1 + dim
);
5381 aff
= isl_aff_normalize(aff
);
5382 ma
= isl_multi_aff_set_aff(ma
, i
- 1, aff
);
5384 isl_local_space_free(ls
);
5389 isl_local_space_free(ls
);
5391 isl_multi_aff_free(ma
);
5395 /* Given a basic map "dom" that represents the context and an affine
5396 * matrix "M" that maps the dimensions of the context to the
5397 * output variables, construct an isl_pw_multi_aff with a single
5398 * cell corresponding to "dom" and affine expressions copied from "M".
5400 * Note that the description of the initial context may have involved
5401 * existentially quantified variables, in which case they also appear
5402 * in "dom". These need to be removed before creating the affine
5403 * expression because an affine expression cannot be defined in terms
5404 * Since newly added integer divisions are inserted before these
5405 * existentially quantified variables, they are still in the final
5406 * positions and the corresponding final columns "M" are zero
5407 * because align_context_divs adds the existentially quantified
5408 * variables of the context to the main tableau without any constraints and
5409 * any equality constraints that are added later on can only serve
5410 * to eliminate these existentially quantified variables.
5412 static void sol_pma_add(struct isl_sol_pma
*sol
,
5413 __isl_take isl_basic_set
*dom
, __isl_take isl_mat
*M
)
5415 isl_local_space
*ls
;
5416 isl_multi_aff
*maff
;
5417 isl_pw_multi_aff
*pma
;
5420 n_div
= isl_basic_set_dim(dom
, isl_dim_div
);
5421 n_known
= n_div
- sol
->sol
.context
->n_unknown
;
5423 maff
= isl_multi_aff_alloc(isl_pw_multi_aff_get_space(sol
->pma
));
5424 ls
= isl_basic_set_get_local_space(dom
);
5425 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
,
5426 n_known
, n_div
- n_known
);
5427 maff
= set_from_affine_matrix(maff
, ls
, M
);
5428 dom
= isl_basic_set_simplify(dom
);
5429 dom
= isl_basic_set_finalize(dom
);
5430 pma
= isl_pw_multi_aff_alloc(isl_set_from_basic_set(dom
), maff
);
5431 sol
->pma
= isl_pw_multi_aff_add_disjoint(sol
->pma
, pma
);
5436 static void sol_pma_free_wrap(struct isl_sol
*sol
)
5438 sol_pma_free((struct isl_sol_pma
*)sol
);
5441 static void sol_pma_add_empty_wrap(struct isl_sol
*sol
,
5442 __isl_take isl_basic_set
*bset
)
5444 sol_pma_add_empty((struct isl_sol_pma
*)sol
, bset
);
5447 static void sol_pma_add_wrap(struct isl_sol
*sol
,
5448 __isl_take isl_basic_set
*dom
, __isl_take isl_mat
*M
)
5450 sol_pma_add((struct isl_sol_pma
*)sol
, dom
, M
);
5453 /* Construct an isl_sol_pma structure for accumulating the solution.
5454 * If track_empty is set, then we also keep track of the parts
5455 * of the context where there is no solution.
5456 * If max is set, then we are solving a maximization, rather than
5457 * a minimization problem, which means that the variables in the
5458 * tableau have value "M - x" rather than "M + x".
5460 static struct isl_sol
*sol_pma_init(__isl_keep isl_basic_map
*bmap
,
5461 __isl_take isl_basic_set
*dom
, int track_empty
, int max
)
5463 struct isl_sol_pma
*sol_pma
= NULL
;
5468 sol_pma
= isl_calloc_type(bmap
->ctx
, struct isl_sol_pma
);
5472 sol_pma
->sol
.rational
= ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
5473 sol_pma
->sol
.dec_level
.callback
.run
= &sol_dec_level_wrap
;
5474 sol_pma
->sol
.dec_level
.sol
= &sol_pma
->sol
;
5475 sol_pma
->sol
.max
= max
;
5476 sol_pma
->sol
.n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5477 sol_pma
->sol
.add
= &sol_pma_add_wrap
;
5478 sol_pma
->sol
.add_empty
= track_empty
? &sol_pma_add_empty_wrap
: NULL
;
5479 sol_pma
->sol
.free
= &sol_pma_free_wrap
;
5480 sol_pma
->pma
= isl_pw_multi_aff_empty(isl_basic_map_get_space(bmap
));
5484 sol_pma
->sol
.context
= isl_context_alloc(dom
);
5485 if (!sol_pma
->sol
.context
)
5489 sol_pma
->empty
= isl_set_alloc_space(isl_basic_set_get_space(dom
),
5490 1, ISL_SET_DISJOINT
);
5491 if (!sol_pma
->empty
)
5495 isl_basic_set_free(dom
);
5496 return &sol_pma
->sol
;
5498 isl_basic_set_free(dom
);
5499 sol_pma_free(sol_pma
);
5503 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
5504 * some obvious symmetries.
5506 * We call basic_map_partial_lexopt_base_sol and extract the results.
5508 static __isl_give isl_pw_multi_aff
*basic_map_partial_lexopt_base_pw_multi_aff(
5509 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5510 __isl_give isl_set
**empty
, int max
)
5512 isl_pw_multi_aff
*result
= NULL
;
5513 struct isl_sol
*sol
;
5514 struct isl_sol_pma
*sol_pma
;
5516 sol
= basic_map_partial_lexopt_base_sol(bmap
, dom
, empty
, max
,
5520 sol_pma
= (struct isl_sol_pma
*) sol
;
5522 result
= isl_pw_multi_aff_copy(sol_pma
->pma
);
5524 *empty
= isl_set_copy(sol_pma
->empty
);
5525 sol_free(&sol_pma
->sol
);
5529 /* Given that the last input variable of "maff" represents the minimum
5530 * of some bounds, check whether we need to plug in the expression
5533 * In particular, check if the last input variable appears in any
5534 * of the expressions in "maff".
5536 static int need_substitution(__isl_keep isl_multi_aff
*maff
)
5541 pos
= isl_multi_aff_dim(maff
, isl_dim_in
) - 1;
5543 for (i
= 0; i
< maff
->n
; ++i
)
5544 if (isl_aff_involves_dims(maff
->p
[i
], isl_dim_in
, pos
, 1))
5550 /* Given a set of upper bounds on the last "input" variable m,
5551 * construct a piecewise affine expression that selects
5552 * the minimal upper bound to m, i.e.,
5553 * divide the space into cells where one
5554 * of the upper bounds is smaller than all the others and select
5555 * this upper bound on that cell.
5557 * In particular, if there are n bounds b_i, then the result
5558 * consists of n cell, each one of the form
5560 * b_i <= b_j for j > i
5561 * b_i < b_j for j < i
5563 * The affine expression on this cell is
5567 static __isl_give isl_pw_aff
*set_minimum_pa(__isl_take isl_space
*space
,
5568 __isl_take isl_mat
*var
)
5571 isl_aff
*aff
= NULL
;
5572 isl_basic_set
*bset
= NULL
;
5573 isl_pw_aff
*paff
= NULL
;
5574 isl_space
*pw_space
;
5575 isl_local_space
*ls
= NULL
;
5580 ls
= isl_local_space_from_space(isl_space_copy(space
));
5581 pw_space
= isl_space_copy(space
);
5582 pw_space
= isl_space_from_domain(pw_space
);
5583 pw_space
= isl_space_add_dims(pw_space
, isl_dim_out
, 1);
5584 paff
= isl_pw_aff_alloc_size(pw_space
, var
->n_row
);
5586 for (i
= 0; i
< var
->n_row
; ++i
) {
5589 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
5590 bset
= isl_basic_set_alloc_space(isl_space_copy(space
), 0,
5594 isl_int_set_si(aff
->v
->el
[0], 1);
5595 isl_seq_cpy(aff
->v
->el
+ 1, var
->row
[i
], var
->n_col
);
5596 isl_int_set_si(aff
->v
->el
[1 + var
->n_col
], 0);
5597 bset
= select_minimum(bset
, var
, i
);
5598 paff_i
= isl_pw_aff_alloc(isl_set_from_basic_set(bset
), aff
);
5599 paff
= isl_pw_aff_add_disjoint(paff
, paff_i
);
5602 isl_local_space_free(ls
);
5603 isl_space_free(space
);
5608 isl_basic_set_free(bset
);
5609 isl_pw_aff_free(paff
);
5610 isl_local_space_free(ls
);
5611 isl_space_free(space
);
5616 /* Given a piecewise multi-affine expression of which the last input variable
5617 * is the minimum of the bounds in "cst", plug in the value of the minimum.
5618 * This minimum expression is given in "min_expr_pa".
5619 * The set "min_expr" contains the same information, but in the form of a set.
5620 * The variable is subsequently projected out.
5622 * The implementation is similar to those of "split" and "split_domain".
5623 * If the variable appears in a given expression, then minimum expression
5624 * is plugged in. Otherwise, if the variable appears in the constraints
5625 * and a split is required, then the domain is split. Otherwise, no split
5628 static __isl_give isl_pw_multi_aff
*split_domain_pma(
5629 __isl_take isl_pw_multi_aff
*opt
, __isl_take isl_pw_aff
*min_expr_pa
,
5630 __isl_take isl_set
*min_expr
, __isl_take isl_mat
*cst
)
5635 isl_pw_multi_aff
*res
;
5637 if (!opt
|| !min_expr
|| !cst
)
5640 n_in
= isl_pw_multi_aff_dim(opt
, isl_dim_in
);
5641 space
= isl_pw_multi_aff_get_space(opt
);
5642 space
= isl_space_drop_dims(space
, isl_dim_in
, n_in
- 1, 1);
5643 res
= isl_pw_multi_aff_empty(space
);
5645 for (i
= 0; i
< opt
->n
; ++i
) {
5646 isl_pw_multi_aff
*pma
;
5648 pma
= isl_pw_multi_aff_alloc(isl_set_copy(opt
->p
[i
].set
),
5649 isl_multi_aff_copy(opt
->p
[i
].maff
));
5650 if (need_substitution(opt
->p
[i
].maff
))
5651 pma
= isl_pw_multi_aff_substitute(pma
,
5652 isl_dim_in
, n_in
- 1, min_expr_pa
);
5653 else if (need_split_set(opt
->p
[i
].set
, cst
))
5654 pma
= isl_pw_multi_aff_intersect_domain(pma
,
5655 isl_set_copy(min_expr
));
5656 pma
= isl_pw_multi_aff_project_out(pma
,
5657 isl_dim_in
, n_in
- 1, 1);
5659 res
= isl_pw_multi_aff_add_disjoint(res
, pma
);
5662 isl_pw_multi_aff_free(opt
);
5663 isl_pw_aff_free(min_expr_pa
);
5664 isl_set_free(min_expr
);
5668 isl_pw_multi_aff_free(opt
);
5669 isl_pw_aff_free(min_expr_pa
);
5670 isl_set_free(min_expr
);
5675 static __isl_give isl_pw_multi_aff
*basic_map_partial_lexopt_pw_multi_aff(
5676 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5677 __isl_give isl_set
**empty
, int max
);
5679 /* This function is called from basic_map_partial_lexopt_symm.
5680 * The last variable of "bmap" and "dom" corresponds to the minimum
5681 * of the bounds in "cst". "map_space" is the space of the original
5682 * input relation (of basic_map_partial_lexopt_symm) and "set_space"
5683 * is the space of the original domain.
5685 * We recursively call basic_map_partial_lexopt and then plug in
5686 * the definition of the minimum in the result.
5688 static __isl_give isl_pw_multi_aff
*
5689 basic_map_partial_lexopt_symm_core_pw_multi_aff(
5690 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5691 __isl_give isl_set
**empty
, int max
, __isl_take isl_mat
*cst
,
5692 __isl_take isl_space
*map_space
, __isl_take isl_space
*set_space
)
5694 isl_pw_multi_aff
*opt
;
5695 isl_pw_aff
*min_expr_pa
;
5698 min_expr
= set_minimum(isl_basic_set_get_space(dom
), isl_mat_copy(cst
));
5699 min_expr_pa
= set_minimum_pa(isl_basic_set_get_space(dom
),
5702 opt
= basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, empty
, max
);
5705 *empty
= split(*empty
,
5706 isl_set_copy(min_expr
), isl_mat_copy(cst
));
5707 *empty
= isl_set_reset_space(*empty
, set_space
);
5710 opt
= split_domain_pma(opt
, min_expr_pa
, min_expr
, cst
);
5711 opt
= isl_pw_multi_aff_reset_space(opt
, map_space
);
5717 #define TYPE isl_pw_multi_aff
5719 #define SUFFIX _pw_multi_aff
5720 #include "isl_tab_lexopt_templ.c"