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 /* add div "div" to context and return non-negativity */
95 int (*add_div
)(struct isl_context
*context
, __isl_keep isl_vec
*div
);
96 int (*detect_equalities
)(struct isl_context
*context
,
98 /* return row index of "best" split */
99 int (*best_split
)(struct isl_context
*context
, struct isl_tab
*tab
);
100 /* check if context has already been determined to be empty */
101 int (*is_empty
)(struct isl_context
*context
);
102 /* check if context is still usable */
103 int (*is_ok
)(struct isl_context
*context
);
104 /* save a copy/snapshot of context */
105 void *(*save
)(struct isl_context
*context
);
106 /* restore saved context */
107 void (*restore
)(struct isl_context
*context
, void *);
108 /* discard saved context */
109 void (*discard
)(void *);
110 /* invalidate context */
111 void (*invalidate
)(struct isl_context
*context
);
113 void (*free
)(struct isl_context
*context
);
117 struct isl_context_op
*op
;
120 struct isl_context_lex
{
121 struct isl_context context
;
125 /* A stack (linked list) of solutions of subtrees of the search space.
127 * "M" describes the solution in terms of the dimensions of "dom".
128 * The number of columns of "M" is one more than the total number
129 * of dimensions of "dom".
131 * If "M" is NULL, then there is no solution on "dom".
133 struct isl_partial_sol
{
135 struct isl_basic_set
*dom
;
138 struct isl_partial_sol
*next
;
142 struct isl_sol_callback
{
143 struct isl_tab_callback callback
;
147 /* isl_sol is an interface for constructing a solution to
148 * a parametric integer linear programming problem.
149 * Every time the algorithm reaches a state where a solution
150 * can be read off from the tableau (including cases where the tableau
151 * is empty), the function "add" is called on the isl_sol passed
152 * to find_solutions_main.
154 * The context tableau is owned by isl_sol and is updated incrementally.
156 * There are currently two implementations of this interface,
157 * isl_sol_map, which simply collects the solutions in an isl_map
158 * and (optionally) the parts of the context where there is no solution
160 * isl_sol_for, which calls a user-defined function for each part of
169 struct isl_context
*context
;
170 struct isl_partial_sol
*partial
;
171 void (*add
)(struct isl_sol
*sol
,
172 struct isl_basic_set
*dom
, struct isl_mat
*M
);
173 void (*add_empty
)(struct isl_sol
*sol
, struct isl_basic_set
*bset
);
174 void (*free
)(struct isl_sol
*sol
);
175 struct isl_sol_callback dec_level
;
178 static void sol_free(struct isl_sol
*sol
)
180 struct isl_partial_sol
*partial
, *next
;
183 for (partial
= sol
->partial
; partial
; partial
= next
) {
184 next
= partial
->next
;
185 isl_basic_set_free(partial
->dom
);
186 isl_mat_free(partial
->M
);
192 /* Push a partial solution represented by a domain and mapping M
193 * onto the stack of partial solutions.
195 static void sol_push_sol(struct isl_sol
*sol
,
196 struct isl_basic_set
*dom
, struct isl_mat
*M
)
198 struct isl_partial_sol
*partial
;
200 if (sol
->error
|| !dom
)
203 partial
= isl_alloc_type(dom
->ctx
, struct isl_partial_sol
);
207 partial
->level
= sol
->level
;
210 partial
->next
= sol
->partial
;
212 sol
->partial
= partial
;
216 isl_basic_set_free(dom
);
221 /* Pop one partial solution from the partial solution stack and
222 * pass it on to sol->add or sol->add_empty.
224 static void sol_pop_one(struct isl_sol
*sol
)
226 struct isl_partial_sol
*partial
;
228 partial
= sol
->partial
;
229 sol
->partial
= partial
->next
;
232 sol
->add(sol
, partial
->dom
, partial
->M
);
234 sol
->add_empty(sol
, partial
->dom
);
238 /* Return a fresh copy of the domain represented by the context tableau.
240 static struct isl_basic_set
*sol_domain(struct isl_sol
*sol
)
242 struct isl_basic_set
*bset
;
247 bset
= isl_basic_set_dup(sol
->context
->op
->peek_basic_set(sol
->context
));
248 bset
= isl_basic_set_update_from_tab(bset
,
249 sol
->context
->op
->peek_tab(sol
->context
));
254 /* Check whether two partial solutions have the same mapping, where n_div
255 * is the number of divs that the two partial solutions have in common.
257 static int same_solution(struct isl_partial_sol
*s1
, struct isl_partial_sol
*s2
,
263 if (!s1
->M
!= !s2
->M
)
268 dim
= isl_basic_set_total_dim(s1
->dom
) - s1
->dom
->n_div
;
270 for (i
= 0; i
< s1
->M
->n_row
; ++i
) {
271 if (isl_seq_first_non_zero(s1
->M
->row
[i
]+1+dim
+n_div
,
272 s1
->M
->n_col
-1-dim
-n_div
) != -1)
274 if (isl_seq_first_non_zero(s2
->M
->row
[i
]+1+dim
+n_div
,
275 s2
->M
->n_col
-1-dim
-n_div
) != -1)
277 if (!isl_seq_eq(s1
->M
->row
[i
], s2
->M
->row
[i
], 1+dim
+n_div
))
283 /* Pop all solutions from the partial solution stack that were pushed onto
284 * the stack at levels that are deeper than the current level.
285 * If the two topmost elements on the stack have the same level
286 * and represent the same solution, then their domains are combined.
287 * This combined domain is the same as the current context domain
288 * as sol_pop is called each time we move back to a higher level.
289 * If the outer level (0) has been reached, then all partial solutions
290 * at the current level are also popped off.
292 static void sol_pop(struct isl_sol
*sol
)
294 struct isl_partial_sol
*partial
;
300 partial
= sol
->partial
;
304 if (partial
->level
== 0 && sol
->level
== 0) {
305 for (partial
= sol
->partial
; partial
; partial
= sol
->partial
)
310 if (partial
->level
<= sol
->level
)
313 if (partial
->next
&& partial
->next
->level
== partial
->level
) {
314 n_div
= isl_basic_set_dim(
315 sol
->context
->op
->peek_basic_set(sol
->context
),
318 if (!same_solution(partial
, partial
->next
, n_div
)) {
322 struct isl_basic_set
*bset
;
326 n
= isl_basic_set_dim(partial
->next
->dom
, isl_dim_div
);
328 bset
= sol_domain(sol
);
329 isl_basic_set_free(partial
->next
->dom
);
330 partial
->next
->dom
= bset
;
331 M
= partial
->next
->M
;
333 M
= isl_mat_drop_cols(M
, M
->n_col
- n
, n
);
334 partial
->next
->M
= M
;
338 partial
->next
->level
= sol
->level
;
343 sol
->partial
= partial
->next
;
344 isl_basic_set_free(partial
->dom
);
345 isl_mat_free(partial
->M
);
351 if (sol
->level
== 0) {
352 for (partial
= sol
->partial
; partial
; partial
= sol
->partial
)
358 error
: sol
->error
= 1;
361 static void sol_dec_level(struct isl_sol
*sol
)
371 static int sol_dec_level_wrap(struct isl_tab_callback
*cb
)
373 struct isl_sol_callback
*callback
= (struct isl_sol_callback
*)cb
;
375 sol_dec_level(callback
->sol
);
377 return callback
->sol
->error
? -1 : 0;
380 /* Move down to next level and push callback onto context tableau
381 * to decrease the level again when it gets rolled back across
382 * the current state. That is, dec_level will be called with
383 * the context tableau in the same state as it is when inc_level
386 static void sol_inc_level(struct isl_sol
*sol
)
394 tab
= sol
->context
->op
->peek_tab(sol
->context
);
395 if (isl_tab_push_callback(tab
, &sol
->dec_level
.callback
) < 0)
399 static void scale_rows(struct isl_mat
*mat
, isl_int m
, int n_row
)
403 if (isl_int_is_one(m
))
406 for (i
= 0; i
< n_row
; ++i
)
407 isl_seq_scale(mat
->row
[i
], mat
->row
[i
], m
, mat
->n_col
);
410 /* Add the solution identified by the tableau and the context tableau.
412 * The layout of the variables is as follows.
413 * tab->n_var is equal to the total number of variables in the input
414 * map (including divs that were copied from the context)
415 * + the number of extra divs constructed
416 * Of these, the first tab->n_param and the last tab->n_div variables
417 * correspond to the variables in the context, i.e.,
418 * tab->n_param + tab->n_div = context_tab->n_var
419 * tab->n_param is equal to the number of parameters and input
420 * dimensions in the input map
421 * tab->n_div is equal to the number of divs in the context
423 * If there is no solution, then call add_empty with a basic set
424 * that corresponds to the context tableau. (If add_empty is NULL,
427 * If there is a solution, then first construct a matrix that maps
428 * all dimensions of the context to the output variables, i.e.,
429 * the output dimensions in the input map.
430 * The divs in the input map (if any) that do not correspond to any
431 * div in the context do not appear in the solution.
432 * The algorithm will make sure that they have an integer value,
433 * but these values themselves are of no interest.
434 * We have to be careful not to drop or rearrange any divs in the
435 * context because that would change the meaning of the matrix.
437 * To extract the value of the output variables, it should be noted
438 * that we always use a big parameter M in the main tableau and so
439 * the variable stored in this tableau is not an output variable x itself, but
440 * x' = M + x (in case of minimization)
442 * x' = M - x (in case of maximization)
443 * If x' appears in a column, then its optimal value is zero,
444 * which means that the optimal value of x is an unbounded number
445 * (-M for minimization and M for maximization).
446 * We currently assume that the output dimensions in the original map
447 * are bounded, so this cannot occur.
448 * Similarly, when x' appears in a row, then the coefficient of M in that
449 * row is necessarily 1.
450 * If the row in the tableau represents
451 * d x' = c + d M + e(y)
452 * then, in case of minimization, the corresponding row in the matrix
455 * with a d = m, the (updated) common denominator of the matrix.
456 * In case of maximization, the row will be
459 static void sol_add(struct isl_sol
*sol
, struct isl_tab
*tab
)
461 struct isl_basic_set
*bset
= NULL
;
462 struct isl_mat
*mat
= NULL
;
467 if (sol
->error
|| !tab
)
470 if (tab
->empty
&& !sol
->add_empty
)
472 if (sol
->context
->op
->is_empty(sol
->context
))
475 bset
= sol_domain(sol
);
478 sol_push_sol(sol
, bset
, NULL
);
484 mat
= isl_mat_alloc(tab
->mat
->ctx
, 1 + sol
->n_out
,
485 1 + tab
->n_param
+ tab
->n_div
);
491 isl_seq_clr(mat
->row
[0] + 1, mat
->n_col
- 1);
492 isl_int_set_si(mat
->row
[0][0], 1);
493 for (row
= 0; row
< sol
->n_out
; ++row
) {
494 int i
= tab
->n_param
+ row
;
497 isl_seq_clr(mat
->row
[1 + row
], mat
->n_col
);
498 if (!tab
->var
[i
].is_row
) {
500 isl_die(mat
->ctx
, isl_error_invalid
,
501 "unbounded optimum", goto error2
);
505 r
= tab
->var
[i
].index
;
507 isl_int_ne(tab
->mat
->row
[r
][2], tab
->mat
->row
[r
][0]))
508 isl_die(mat
->ctx
, isl_error_invalid
,
509 "unbounded optimum", goto error2
);
510 isl_int_gcd(m
, mat
->row
[0][0], tab
->mat
->row
[r
][0]);
511 isl_int_divexact(m
, tab
->mat
->row
[r
][0], m
);
512 scale_rows(mat
, m
, 1 + row
);
513 isl_int_divexact(m
, mat
->row
[0][0], tab
->mat
->row
[r
][0]);
514 isl_int_mul(mat
->row
[1 + row
][0], m
, tab
->mat
->row
[r
][1]);
515 for (j
= 0; j
< tab
->n_param
; ++j
) {
517 if (tab
->var
[j
].is_row
)
519 col
= tab
->var
[j
].index
;
520 isl_int_mul(mat
->row
[1 + row
][1 + j
], m
,
521 tab
->mat
->row
[r
][off
+ col
]);
523 for (j
= 0; j
< tab
->n_div
; ++j
) {
525 if (tab
->var
[tab
->n_var
- tab
->n_div
+j
].is_row
)
527 col
= tab
->var
[tab
->n_var
- tab
->n_div
+j
].index
;
528 isl_int_mul(mat
->row
[1 + row
][1 + tab
->n_param
+ j
], m
,
529 tab
->mat
->row
[r
][off
+ col
]);
532 isl_seq_neg(mat
->row
[1 + row
], mat
->row
[1 + row
],
538 sol_push_sol(sol
, bset
, mat
);
543 isl_basic_set_free(bset
);
551 struct isl_set
*empty
;
554 static void sol_map_free(struct isl_sol_map
*sol_map
)
558 if (sol_map
->sol
.context
)
559 sol_map
->sol
.context
->op
->free(sol_map
->sol
.context
);
560 isl_map_free(sol_map
->map
);
561 isl_set_free(sol_map
->empty
);
565 static void sol_map_free_wrap(struct isl_sol
*sol
)
567 sol_map_free((struct isl_sol_map
*)sol
);
570 /* This function is called for parts of the context where there is
571 * no solution, with "bset" corresponding to the context tableau.
572 * Simply add the basic set to the set "empty".
574 static void sol_map_add_empty(struct isl_sol_map
*sol
,
575 struct isl_basic_set
*bset
)
577 if (!bset
|| !sol
->empty
)
580 sol
->empty
= isl_set_grow(sol
->empty
, 1);
581 bset
= isl_basic_set_simplify(bset
);
582 bset
= isl_basic_set_finalize(bset
);
583 sol
->empty
= isl_set_add_basic_set(sol
->empty
, isl_basic_set_copy(bset
));
586 isl_basic_set_free(bset
);
589 isl_basic_set_free(bset
);
593 static void sol_map_add_empty_wrap(struct isl_sol
*sol
,
594 struct isl_basic_set
*bset
)
596 sol_map_add_empty((struct isl_sol_map
*)sol
, bset
);
599 /* Given a basic map "dom" that represents the context and an affine
600 * matrix "M" that maps the dimensions of the context to the
601 * output variables, construct a basic map with the same parameters
602 * and divs as the context, the dimensions of the context as input
603 * dimensions and a number of output dimensions that is equal to
604 * the number of output dimensions in the input map.
606 * The constraints and divs of the context are simply copied
607 * from "dom". For each row
611 * is added, with d the common denominator of M.
613 static void sol_map_add(struct isl_sol_map
*sol
,
614 struct isl_basic_set
*dom
, struct isl_mat
*M
)
617 struct isl_basic_map
*bmap
= NULL
;
625 if (sol
->sol
.error
|| !dom
|| !M
)
628 n_out
= sol
->sol
.n_out
;
629 n_eq
= dom
->n_eq
+ n_out
;
630 n_ineq
= dom
->n_ineq
;
632 nparam
= isl_basic_set_total_dim(dom
) - n_div
;
633 total
= isl_map_dim(sol
->map
, isl_dim_all
);
634 bmap
= isl_basic_map_alloc_space(isl_map_get_space(sol
->map
),
635 n_div
, n_eq
, 2 * n_div
+ n_ineq
);
638 if (sol
->sol
.rational
)
639 ISL_F_SET(bmap
, ISL_BASIC_MAP_RATIONAL
);
640 for (i
= 0; i
< dom
->n_div
; ++i
) {
641 int k
= isl_basic_map_alloc_div(bmap
);
644 isl_seq_cpy(bmap
->div
[k
], dom
->div
[i
], 1 + 1 + nparam
);
645 isl_seq_clr(bmap
->div
[k
] + 1 + 1 + nparam
, total
- nparam
);
646 isl_seq_cpy(bmap
->div
[k
] + 1 + 1 + total
,
647 dom
->div
[i
] + 1 + 1 + nparam
, i
);
649 for (i
= 0; i
< dom
->n_eq
; ++i
) {
650 int k
= isl_basic_map_alloc_equality(bmap
);
653 isl_seq_cpy(bmap
->eq
[k
], dom
->eq
[i
], 1 + nparam
);
654 isl_seq_clr(bmap
->eq
[k
] + 1 + nparam
, total
- nparam
);
655 isl_seq_cpy(bmap
->eq
[k
] + 1 + total
,
656 dom
->eq
[i
] + 1 + nparam
, n_div
);
658 for (i
= 0; i
< dom
->n_ineq
; ++i
) {
659 int k
= isl_basic_map_alloc_inequality(bmap
);
662 isl_seq_cpy(bmap
->ineq
[k
], dom
->ineq
[i
], 1 + nparam
);
663 isl_seq_clr(bmap
->ineq
[k
] + 1 + nparam
, total
- nparam
);
664 isl_seq_cpy(bmap
->ineq
[k
] + 1 + total
,
665 dom
->ineq
[i
] + 1 + nparam
, n_div
);
667 for (i
= 0; i
< M
->n_row
- 1; ++i
) {
668 int k
= isl_basic_map_alloc_equality(bmap
);
671 isl_seq_cpy(bmap
->eq
[k
], M
->row
[1 + i
], 1 + nparam
);
672 isl_seq_clr(bmap
->eq
[k
] + 1 + nparam
, n_out
);
673 isl_int_neg(bmap
->eq
[k
][1 + nparam
+ i
], M
->row
[0][0]);
674 isl_seq_cpy(bmap
->eq
[k
] + 1 + nparam
+ n_out
,
675 M
->row
[1 + i
] + 1 + nparam
, n_div
);
677 bmap
= isl_basic_map_simplify(bmap
);
678 bmap
= isl_basic_map_finalize(bmap
);
679 sol
->map
= isl_map_grow(sol
->map
, 1);
680 sol
->map
= isl_map_add_basic_map(sol
->map
, bmap
);
681 isl_basic_set_free(dom
);
687 isl_basic_set_free(dom
);
689 isl_basic_map_free(bmap
);
693 static void sol_map_add_wrap(struct isl_sol
*sol
,
694 struct isl_basic_set
*dom
, struct isl_mat
*M
)
696 sol_map_add((struct isl_sol_map
*)sol
, dom
, M
);
700 /* Store the "parametric constant" of row "row" of tableau "tab" in "line",
701 * i.e., the constant term and the coefficients of all variables that
702 * appear in the context tableau.
703 * Note that the coefficient of the big parameter M is NOT copied.
704 * The context tableau may not have a big parameter and even when it
705 * does, it is a different big parameter.
707 static void get_row_parameter_line(struct isl_tab
*tab
, int row
, isl_int
*line
)
710 unsigned off
= 2 + tab
->M
;
712 isl_int_set(line
[0], tab
->mat
->row
[row
][1]);
713 for (i
= 0; i
< tab
->n_param
; ++i
) {
714 if (tab
->var
[i
].is_row
)
715 isl_int_set_si(line
[1 + i
], 0);
717 int col
= tab
->var
[i
].index
;
718 isl_int_set(line
[1 + i
], tab
->mat
->row
[row
][off
+ col
]);
721 for (i
= 0; i
< tab
->n_div
; ++i
) {
722 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
723 isl_int_set_si(line
[1 + tab
->n_param
+ i
], 0);
725 int col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
726 isl_int_set(line
[1 + tab
->n_param
+ i
],
727 tab
->mat
->row
[row
][off
+ col
]);
732 /* Check if rows "row1" and "row2" have identical "parametric constants",
733 * as explained above.
734 * In this case, we also insist that the coefficients of the big parameter
735 * be the same as the values of the constants will only be the same
736 * if these coefficients are also the same.
738 static int identical_parameter_line(struct isl_tab
*tab
, int row1
, int row2
)
741 unsigned off
= 2 + tab
->M
;
743 if (isl_int_ne(tab
->mat
->row
[row1
][1], tab
->mat
->row
[row2
][1]))
746 if (tab
->M
&& isl_int_ne(tab
->mat
->row
[row1
][2],
747 tab
->mat
->row
[row2
][2]))
750 for (i
= 0; i
< tab
->n_param
+ tab
->n_div
; ++i
) {
751 int pos
= i
< tab
->n_param
? i
:
752 tab
->n_var
- tab
->n_div
+ i
- tab
->n_param
;
755 if (tab
->var
[pos
].is_row
)
757 col
= tab
->var
[pos
].index
;
758 if (isl_int_ne(tab
->mat
->row
[row1
][off
+ col
],
759 tab
->mat
->row
[row2
][off
+ col
]))
765 /* Return an inequality that expresses that the "parametric constant"
766 * should be non-negative.
767 * This function is only called when the coefficient of the big parameter
770 static struct isl_vec
*get_row_parameter_ineq(struct isl_tab
*tab
, int row
)
772 struct isl_vec
*ineq
;
774 ineq
= isl_vec_alloc(tab
->mat
->ctx
, 1 + tab
->n_param
+ tab
->n_div
);
778 get_row_parameter_line(tab
, row
, ineq
->el
);
780 ineq
= isl_vec_normalize(ineq
);
785 /* Normalize a div expression of the form
787 * [(g*f(x) + c)/(g * m)]
789 * with c the constant term and f(x) the remaining coefficients, to
793 static void normalize_div(__isl_keep isl_vec
*div
)
795 isl_ctx
*ctx
= isl_vec_get_ctx(div
);
796 int len
= div
->size
- 2;
798 isl_seq_gcd(div
->el
+ 2, len
, &ctx
->normalize_gcd
);
799 isl_int_gcd(ctx
->normalize_gcd
, ctx
->normalize_gcd
, div
->el
[0]);
801 if (isl_int_is_one(ctx
->normalize_gcd
))
804 isl_int_divexact(div
->el
[0], div
->el
[0], ctx
->normalize_gcd
);
805 isl_int_fdiv_q(div
->el
[1], div
->el
[1], ctx
->normalize_gcd
);
806 isl_seq_scale_down(div
->el
+ 2, div
->el
+ 2, ctx
->normalize_gcd
, len
);
809 /* Return a integer division for use in a parametric cut based on the given row.
810 * In particular, let the parametric constant of the row be
814 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
815 * The div returned is equal to
817 * floor(\sum_i {-a_i} y_i) = floor((\sum_i (-a_i mod d) y_i)/d)
819 static struct isl_vec
*get_row_parameter_div(struct isl_tab
*tab
, int row
)
823 div
= isl_vec_alloc(tab
->mat
->ctx
, 1 + 1 + tab
->n_param
+ tab
->n_div
);
827 isl_int_set(div
->el
[0], tab
->mat
->row
[row
][0]);
828 get_row_parameter_line(tab
, row
, div
->el
+ 1);
829 isl_seq_neg(div
->el
+ 1, div
->el
+ 1, div
->size
- 1);
831 isl_seq_fdiv_r(div
->el
+ 1, div
->el
+ 1, div
->el
[0], div
->size
- 1);
836 /* Return a integer division for use in transferring an integrality constraint
838 * In particular, let the parametric constant of the row be
842 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
843 * The the returned div is equal to
845 * floor(\sum_i {a_i} y_i) = floor((\sum_i (a_i mod d) y_i)/d)
847 static struct isl_vec
*get_row_split_div(struct isl_tab
*tab
, int row
)
851 div
= isl_vec_alloc(tab
->mat
->ctx
, 1 + 1 + tab
->n_param
+ tab
->n_div
);
855 isl_int_set(div
->el
[0], tab
->mat
->row
[row
][0]);
856 get_row_parameter_line(tab
, row
, div
->el
+ 1);
858 isl_seq_fdiv_r(div
->el
+ 1, div
->el
+ 1, div
->el
[0], div
->size
- 1);
863 /* Construct and return an inequality that expresses an upper bound
865 * In particular, if the div is given by
869 * then the inequality expresses
873 static struct isl_vec
*ineq_for_div(struct isl_basic_set
*bset
, unsigned div
)
877 struct isl_vec
*ineq
;
882 total
= isl_basic_set_total_dim(bset
);
883 div_pos
= 1 + total
- bset
->n_div
+ div
;
885 ineq
= isl_vec_alloc(bset
->ctx
, 1 + total
);
889 isl_seq_cpy(ineq
->el
, bset
->div
[div
] + 1, 1 + total
);
890 isl_int_neg(ineq
->el
[div_pos
], bset
->div
[div
][0]);
894 /* Given a row in the tableau and a div that was created
895 * using get_row_split_div and that has been constrained to equality, i.e.,
897 * d = floor(\sum_i {a_i} y_i) = \sum_i {a_i} y_i
899 * replace the expression "\sum_i {a_i} y_i" in the row by d,
900 * i.e., we subtract "\sum_i {a_i} y_i" and add 1 d.
901 * The coefficients of the non-parameters in the tableau have been
902 * verified to be integral. We can therefore simply replace coefficient b
903 * by floor(b). For the coefficients of the parameters we have
904 * floor(a_i) = a_i - {a_i}, while for the other coefficients, we have
907 static struct isl_tab
*set_row_cst_to_div(struct isl_tab
*tab
, int row
, int div
)
909 isl_seq_fdiv_q(tab
->mat
->row
[row
] + 1, tab
->mat
->row
[row
] + 1,
910 tab
->mat
->row
[row
][0], 1 + tab
->M
+ tab
->n_col
);
912 isl_int_set_si(tab
->mat
->row
[row
][0], 1);
914 if (tab
->var
[tab
->n_var
- tab
->n_div
+ div
].is_row
) {
915 int drow
= tab
->var
[tab
->n_var
- tab
->n_div
+ div
].index
;
917 isl_assert(tab
->mat
->ctx
,
918 isl_int_is_one(tab
->mat
->row
[drow
][0]), goto error
);
919 isl_seq_combine(tab
->mat
->row
[row
] + 1,
920 tab
->mat
->ctx
->one
, tab
->mat
->row
[row
] + 1,
921 tab
->mat
->ctx
->one
, tab
->mat
->row
[drow
] + 1,
922 1 + tab
->M
+ tab
->n_col
);
924 int dcol
= tab
->var
[tab
->n_var
- tab
->n_div
+ div
].index
;
926 isl_int_add_ui(tab
->mat
->row
[row
][2 + tab
->M
+ dcol
],
927 tab
->mat
->row
[row
][2 + tab
->M
+ dcol
], 1);
936 /* Check if the (parametric) constant of the given row is obviously
937 * negative, meaning that we don't need to consult the context tableau.
938 * If there is a big parameter and its coefficient is non-zero,
939 * then this coefficient determines the outcome.
940 * Otherwise, we check whether the constant is negative and
941 * all non-zero coefficients of parameters are negative and
942 * belong to non-negative parameters.
944 static int is_obviously_neg(struct isl_tab
*tab
, int row
)
948 unsigned off
= 2 + tab
->M
;
951 if (isl_int_is_pos(tab
->mat
->row
[row
][2]))
953 if (isl_int_is_neg(tab
->mat
->row
[row
][2]))
957 if (isl_int_is_nonneg(tab
->mat
->row
[row
][1]))
959 for (i
= 0; i
< tab
->n_param
; ++i
) {
960 /* Eliminated parameter */
961 if (tab
->var
[i
].is_row
)
963 col
= tab
->var
[i
].index
;
964 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
966 if (!tab
->var
[i
].is_nonneg
)
968 if (isl_int_is_pos(tab
->mat
->row
[row
][off
+ col
]))
971 for (i
= 0; i
< tab
->n_div
; ++i
) {
972 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
974 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
975 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
977 if (!tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_nonneg
)
979 if (isl_int_is_pos(tab
->mat
->row
[row
][off
+ col
]))
985 /* Check if the (parametric) constant of the given row is obviously
986 * non-negative, meaning that we don't need to consult the context tableau.
987 * If there is a big parameter and its coefficient is non-zero,
988 * then this coefficient determines the outcome.
989 * Otherwise, we check whether the constant is non-negative and
990 * all non-zero coefficients of parameters are positive and
991 * belong to non-negative parameters.
993 static int is_obviously_nonneg(struct isl_tab
*tab
, int row
)
997 unsigned off
= 2 + tab
->M
;
1000 if (isl_int_is_pos(tab
->mat
->row
[row
][2]))
1002 if (isl_int_is_neg(tab
->mat
->row
[row
][2]))
1006 if (isl_int_is_neg(tab
->mat
->row
[row
][1]))
1008 for (i
= 0; i
< tab
->n_param
; ++i
) {
1009 /* Eliminated parameter */
1010 if (tab
->var
[i
].is_row
)
1012 col
= tab
->var
[i
].index
;
1013 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
1015 if (!tab
->var
[i
].is_nonneg
)
1017 if (isl_int_is_neg(tab
->mat
->row
[row
][off
+ col
]))
1020 for (i
= 0; i
< tab
->n_div
; ++i
) {
1021 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
1023 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
1024 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
1026 if (!tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_nonneg
)
1028 if (isl_int_is_neg(tab
->mat
->row
[row
][off
+ col
]))
1034 /* Given a row r and two columns, return the column that would
1035 * lead to the lexicographically smallest increment in the sample
1036 * solution when leaving the basis in favor of the row.
1037 * Pivoting with column c will increment the sample value by a non-negative
1038 * constant times a_{V,c}/a_{r,c}, with a_{V,c} the elements of column c
1039 * corresponding to the non-parametric variables.
1040 * If variable v appears in a column c_v, the a_{v,c} = 1 iff c = c_v,
1041 * with all other entries in this virtual row equal to zero.
1042 * If variable v appears in a row, then a_{v,c} is the element in column c
1045 * Let v be the first variable with a_{v,c1}/a_{r,c1} != a_{v,c2}/a_{r,c2}.
1046 * Then if a_{v,c1}/a_{r,c1} < a_{v,c2}/a_{r,c2}, i.e.,
1047 * a_{v,c2} a_{r,c1} - a_{v,c1} a_{r,c2} > 0, c1 results in the minimal
1048 * increment. Otherwise, it's c2.
1050 static int lexmin_col_pair(struct isl_tab
*tab
,
1051 int row
, int col1
, int col2
, isl_int tmp
)
1056 tr
= tab
->mat
->row
[row
] + 2 + tab
->M
;
1058 for (i
= tab
->n_param
; i
< tab
->n_var
- tab
->n_div
; ++i
) {
1062 if (!tab
->var
[i
].is_row
) {
1063 if (tab
->var
[i
].index
== col1
)
1065 if (tab
->var
[i
].index
== col2
)
1070 if (tab
->var
[i
].index
== row
)
1073 r
= tab
->mat
->row
[tab
->var
[i
].index
] + 2 + tab
->M
;
1074 s1
= isl_int_sgn(r
[col1
]);
1075 s2
= isl_int_sgn(r
[col2
]);
1076 if (s1
== 0 && s2
== 0)
1083 isl_int_mul(tmp
, r
[col2
], tr
[col1
]);
1084 isl_int_submul(tmp
, r
[col1
], tr
[col2
]);
1085 if (isl_int_is_pos(tmp
))
1087 if (isl_int_is_neg(tmp
))
1093 /* Given a row in the tableau, find and return the column that would
1094 * result in the lexicographically smallest, but positive, increment
1095 * in the sample point.
1096 * If there is no such column, then return tab->n_col.
1097 * If anything goes wrong, return -1.
1099 static int lexmin_pivot_col(struct isl_tab
*tab
, int row
)
1102 int col
= tab
->n_col
;
1106 tr
= tab
->mat
->row
[row
] + 2 + tab
->M
;
1110 for (j
= tab
->n_dead
; j
< tab
->n_col
; ++j
) {
1111 if (tab
->col_var
[j
] >= 0 &&
1112 (tab
->col_var
[j
] < tab
->n_param
||
1113 tab
->col_var
[j
] >= tab
->n_var
- tab
->n_div
))
1116 if (!isl_int_is_pos(tr
[j
]))
1119 if (col
== tab
->n_col
)
1122 col
= lexmin_col_pair(tab
, row
, col
, j
, tmp
);
1123 isl_assert(tab
->mat
->ctx
, col
>= 0, goto error
);
1133 /* Return the first known violated constraint, i.e., a non-negative
1134 * constraint that currently has an either obviously negative value
1135 * or a previously determined to be negative value.
1137 * If any constraint has a negative coefficient for the big parameter,
1138 * if any, then we return one of these first.
1140 static int first_neg(struct isl_tab
*tab
)
1145 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
1146 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
1148 if (!isl_int_is_neg(tab
->mat
->row
[row
][2]))
1151 tab
->row_sign
[row
] = isl_tab_row_neg
;
1154 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
1155 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
1157 if (tab
->row_sign
) {
1158 if (tab
->row_sign
[row
] == 0 &&
1159 is_obviously_neg(tab
, row
))
1160 tab
->row_sign
[row
] = isl_tab_row_neg
;
1161 if (tab
->row_sign
[row
] != isl_tab_row_neg
)
1163 } else if (!is_obviously_neg(tab
, row
))
1170 /* Check whether the invariant that all columns are lexico-positive
1171 * is satisfied. This function is not called from the current code
1172 * but is useful during debugging.
1174 static void check_lexpos(struct isl_tab
*tab
) __attribute__ ((unused
));
1175 static void check_lexpos(struct isl_tab
*tab
)
1177 unsigned off
= 2 + tab
->M
;
1182 for (col
= tab
->n_dead
; col
< tab
->n_col
; ++col
) {
1183 if (tab
->col_var
[col
] >= 0 &&
1184 (tab
->col_var
[col
] < tab
->n_param
||
1185 tab
->col_var
[col
] >= tab
->n_var
- tab
->n_div
))
1187 for (var
= tab
->n_param
; var
< tab
->n_var
- tab
->n_div
; ++var
) {
1188 if (!tab
->var
[var
].is_row
) {
1189 if (tab
->var
[var
].index
== col
)
1194 row
= tab
->var
[var
].index
;
1195 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
1197 if (isl_int_is_pos(tab
->mat
->row
[row
][off
+ col
]))
1199 fprintf(stderr
, "lexneg column %d (row %d)\n",
1202 if (var
>= tab
->n_var
- tab
->n_div
)
1203 fprintf(stderr
, "zero column %d\n", col
);
1207 /* Report to the caller that the given constraint is part of an encountered
1210 static int report_conflicting_constraint(struct isl_tab
*tab
, int con
)
1212 return tab
->conflict(con
, tab
->conflict_user
);
1215 /* Given a conflicting row in the tableau, report all constraints
1216 * involved in the row to the caller. That is, the row itself
1217 * (if it represents a constraint) and all constraint columns with
1218 * non-zero (and therefore negative) coefficients.
1220 static int report_conflict(struct isl_tab
*tab
, int row
)
1228 if (tab
->row_var
[row
] < 0 &&
1229 report_conflicting_constraint(tab
, ~tab
->row_var
[row
]) < 0)
1232 tr
= tab
->mat
->row
[row
] + 2 + tab
->M
;
1234 for (j
= tab
->n_dead
; j
< tab
->n_col
; ++j
) {
1235 if (tab
->col_var
[j
] >= 0 &&
1236 (tab
->col_var
[j
] < tab
->n_param
||
1237 tab
->col_var
[j
] >= tab
->n_var
- tab
->n_div
))
1240 if (!isl_int_is_neg(tr
[j
]))
1243 if (tab
->col_var
[j
] < 0 &&
1244 report_conflicting_constraint(tab
, ~tab
->col_var
[j
]) < 0)
1251 /* Resolve all known or obviously violated constraints through pivoting.
1252 * In particular, as long as we can find any violated constraint, we
1253 * look for a pivoting column that would result in the lexicographically
1254 * smallest increment in the sample point. If there is no such column
1255 * then the tableau is infeasible.
1257 static int restore_lexmin(struct isl_tab
*tab
) WARN_UNUSED
;
1258 static int restore_lexmin(struct isl_tab
*tab
)
1266 while ((row
= first_neg(tab
)) != -1) {
1267 col
= lexmin_pivot_col(tab
, row
);
1268 if (col
>= tab
->n_col
) {
1269 if (report_conflict(tab
, row
) < 0)
1271 if (isl_tab_mark_empty(tab
) < 0)
1277 if (isl_tab_pivot(tab
, row
, col
) < 0)
1283 /* Given a row that represents an equality, look for an appropriate
1285 * In particular, if there are any non-zero coefficients among
1286 * the non-parameter variables, then we take the last of these
1287 * variables. Eliminating this variable in terms of the other
1288 * variables and/or parameters does not influence the property
1289 * that all column in the initial tableau are lexicographically
1290 * positive. The row corresponding to the eliminated variable
1291 * will only have non-zero entries below the diagonal of the
1292 * initial tableau. That is, we transform
1298 * If there is no such non-parameter variable, then we are dealing with
1299 * pure parameter equality and we pick any parameter with coefficient 1 or -1
1300 * for elimination. This will ensure that the eliminated parameter
1301 * always has an integer value whenever all the other parameters are integral.
1302 * If there is no such parameter then we return -1.
1304 static int last_var_col_or_int_par_col(struct isl_tab
*tab
, int row
)
1306 unsigned off
= 2 + tab
->M
;
1309 for (i
= tab
->n_var
- tab
->n_div
- 1; i
>= 0 && i
>= tab
->n_param
; --i
) {
1311 if (tab
->var
[i
].is_row
)
1313 col
= tab
->var
[i
].index
;
1314 if (col
<= tab
->n_dead
)
1316 if (!isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
1319 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
1320 if (isl_int_is_one(tab
->mat
->row
[row
][off
+ i
]))
1322 if (isl_int_is_negone(tab
->mat
->row
[row
][off
+ i
]))
1328 /* Add an equality that is known to be valid to the tableau.
1329 * We first check if we can eliminate a variable or a parameter.
1330 * If not, we add the equality as two inequalities.
1331 * In this case, the equality was a pure parameter equality and there
1332 * is no need to resolve any constraint violations.
1334 * This function assumes that at least two more rows and at least
1335 * two more elements in the constraint array are available in the tableau.
1337 static struct isl_tab
*add_lexmin_valid_eq(struct isl_tab
*tab
, isl_int
*eq
)
1344 r
= isl_tab_add_row(tab
, eq
);
1348 r
= tab
->con
[r
].index
;
1349 i
= last_var_col_or_int_par_col(tab
, r
);
1351 tab
->con
[r
].is_nonneg
= 1;
1352 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
1354 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
1355 r
= isl_tab_add_row(tab
, eq
);
1358 tab
->con
[r
].is_nonneg
= 1;
1359 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
1362 if (isl_tab_pivot(tab
, r
, i
) < 0)
1364 if (isl_tab_kill_col(tab
, i
) < 0)
1375 /* Check if the given row is a pure constant.
1377 static int is_constant(struct isl_tab
*tab
, int row
)
1379 unsigned off
= 2 + tab
->M
;
1381 return isl_seq_first_non_zero(tab
->mat
->row
[row
] + off
+ tab
->n_dead
,
1382 tab
->n_col
- tab
->n_dead
) == -1;
1385 /* Add an equality that may or may not be valid to the tableau.
1386 * If the resulting row is a pure constant, then it must be zero.
1387 * Otherwise, the resulting tableau is empty.
1389 * If the row is not a pure constant, then we add two inequalities,
1390 * each time checking that they can be satisfied.
1391 * In the end we try to use one of the two constraints to eliminate
1394 * This function assumes that at least two more rows and at least
1395 * two more elements in the constraint array are available in the tableau.
1397 static int add_lexmin_eq(struct isl_tab
*tab
, isl_int
*eq
) WARN_UNUSED
;
1398 static int add_lexmin_eq(struct isl_tab
*tab
, isl_int
*eq
)
1402 struct isl_tab_undo
*snap
;
1406 snap
= isl_tab_snap(tab
);
1407 r1
= isl_tab_add_row(tab
, eq
);
1410 tab
->con
[r1
].is_nonneg
= 1;
1411 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r1
]) < 0)
1414 row
= tab
->con
[r1
].index
;
1415 if (is_constant(tab
, row
)) {
1416 if (!isl_int_is_zero(tab
->mat
->row
[row
][1]) ||
1417 (tab
->M
&& !isl_int_is_zero(tab
->mat
->row
[row
][2]))) {
1418 if (isl_tab_mark_empty(tab
) < 0)
1422 if (isl_tab_rollback(tab
, snap
) < 0)
1427 if (restore_lexmin(tab
) < 0)
1432 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
1434 r2
= isl_tab_add_row(tab
, eq
);
1437 tab
->con
[r2
].is_nonneg
= 1;
1438 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r2
]) < 0)
1441 if (restore_lexmin(tab
) < 0)
1446 if (!tab
->con
[r1
].is_row
) {
1447 if (isl_tab_kill_col(tab
, tab
->con
[r1
].index
) < 0)
1449 } else if (!tab
->con
[r2
].is_row
) {
1450 if (isl_tab_kill_col(tab
, tab
->con
[r2
].index
) < 0)
1455 tab
->bmap
= isl_basic_map_add_ineq(tab
->bmap
, eq
);
1456 if (isl_tab_push(tab
, isl_tab_undo_bmap_ineq
) < 0)
1458 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
1459 tab
->bmap
= isl_basic_map_add_ineq(tab
->bmap
, eq
);
1460 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
1461 if (isl_tab_push(tab
, isl_tab_undo_bmap_ineq
) < 0)
1470 /* Add an inequality to the tableau, resolving violations using
1473 * This function assumes that at least one more row and at least
1474 * one more element in the constraint array are available in the tableau.
1476 static struct isl_tab
*add_lexmin_ineq(struct isl_tab
*tab
, isl_int
*ineq
)
1483 tab
->bmap
= isl_basic_map_add_ineq(tab
->bmap
, ineq
);
1484 if (isl_tab_push(tab
, isl_tab_undo_bmap_ineq
) < 0)
1489 r
= isl_tab_add_row(tab
, ineq
);
1492 tab
->con
[r
].is_nonneg
= 1;
1493 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
1495 if (isl_tab_row_is_redundant(tab
, tab
->con
[r
].index
)) {
1496 if (isl_tab_mark_redundant(tab
, tab
->con
[r
].index
) < 0)
1501 if (restore_lexmin(tab
) < 0)
1503 if (!tab
->empty
&& tab
->con
[r
].is_row
&&
1504 isl_tab_row_is_redundant(tab
, tab
->con
[r
].index
))
1505 if (isl_tab_mark_redundant(tab
, tab
->con
[r
].index
) < 0)
1513 /* Check if the coefficients of the parameters are all integral.
1515 static int integer_parameter(struct isl_tab
*tab
, int row
)
1519 unsigned off
= 2 + tab
->M
;
1521 for (i
= 0; i
< tab
->n_param
; ++i
) {
1522 /* Eliminated parameter */
1523 if (tab
->var
[i
].is_row
)
1525 col
= tab
->var
[i
].index
;
1526 if (!isl_int_is_divisible_by(tab
->mat
->row
[row
][off
+ col
],
1527 tab
->mat
->row
[row
][0]))
1530 for (i
= 0; i
< tab
->n_div
; ++i
) {
1531 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
1533 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
1534 if (!isl_int_is_divisible_by(tab
->mat
->row
[row
][off
+ col
],
1535 tab
->mat
->row
[row
][0]))
1541 /* Check if the coefficients of the non-parameter variables are all integral.
1543 static int integer_variable(struct isl_tab
*tab
, int row
)
1546 unsigned off
= 2 + tab
->M
;
1548 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
1549 if (tab
->col_var
[i
] >= 0 &&
1550 (tab
->col_var
[i
] < tab
->n_param
||
1551 tab
->col_var
[i
] >= tab
->n_var
- tab
->n_div
))
1553 if (!isl_int_is_divisible_by(tab
->mat
->row
[row
][off
+ i
],
1554 tab
->mat
->row
[row
][0]))
1560 /* Check if the constant term is integral.
1562 static int integer_constant(struct isl_tab
*tab
, int row
)
1564 return isl_int_is_divisible_by(tab
->mat
->row
[row
][1],
1565 tab
->mat
->row
[row
][0]);
1568 #define I_CST 1 << 0
1569 #define I_PAR 1 << 1
1570 #define I_VAR 1 << 2
1572 /* Check for next (non-parameter) variable after "var" (first if var == -1)
1573 * that is non-integer and therefore requires a cut and return
1574 * the index of the variable.
1575 * For parametric tableaus, there are three parts in a row,
1576 * the constant, the coefficients of the parameters and the rest.
1577 * For each part, we check whether the coefficients in that part
1578 * are all integral and if so, set the corresponding flag in *f.
1579 * If the constant and the parameter part are integral, then the
1580 * current sample value is integral and no cut is required
1581 * (irrespective of whether the variable part is integral).
1583 static int next_non_integer_var(struct isl_tab
*tab
, int var
, int *f
)
1585 var
= var
< 0 ? tab
->n_param
: var
+ 1;
1587 for (; var
< tab
->n_var
- tab
->n_div
; ++var
) {
1590 if (!tab
->var
[var
].is_row
)
1592 row
= tab
->var
[var
].index
;
1593 if (integer_constant(tab
, row
))
1594 ISL_FL_SET(flags
, I_CST
);
1595 if (integer_parameter(tab
, row
))
1596 ISL_FL_SET(flags
, I_PAR
);
1597 if (ISL_FL_ISSET(flags
, I_CST
) && ISL_FL_ISSET(flags
, I_PAR
))
1599 if (integer_variable(tab
, row
))
1600 ISL_FL_SET(flags
, I_VAR
);
1607 /* Check for first (non-parameter) variable that is non-integer and
1608 * therefore requires a cut and return the corresponding row.
1609 * For parametric tableaus, there are three parts in a row,
1610 * the constant, the coefficients of the parameters and the rest.
1611 * For each part, we check whether the coefficients in that part
1612 * are all integral and if so, set the corresponding flag in *f.
1613 * If the constant and the parameter part are integral, then the
1614 * current sample value is integral and no cut is required
1615 * (irrespective of whether the variable part is integral).
1617 static int first_non_integer_row(struct isl_tab
*tab
, int *f
)
1619 int var
= next_non_integer_var(tab
, -1, f
);
1621 return var
< 0 ? -1 : tab
->var
[var
].index
;
1624 /* Add a (non-parametric) cut to cut away the non-integral sample
1625 * value of the given row.
1627 * If the row is given by
1629 * m r = f + \sum_i a_i y_i
1633 * c = - {-f/m} + \sum_i {a_i/m} y_i >= 0
1635 * The big parameter, if any, is ignored, since it is assumed to be big
1636 * enough to be divisible by any integer.
1637 * If the tableau is actually a parametric tableau, then this function
1638 * is only called when all coefficients of the parameters are integral.
1639 * The cut therefore has zero coefficients for the parameters.
1641 * The current value is known to be negative, so row_sign, if it
1642 * exists, is set accordingly.
1644 * Return the row of the cut or -1.
1646 static int add_cut(struct isl_tab
*tab
, int row
)
1651 unsigned off
= 2 + tab
->M
;
1653 if (isl_tab_extend_cons(tab
, 1) < 0)
1655 r
= isl_tab_allocate_con(tab
);
1659 r_row
= tab
->mat
->row
[tab
->con
[r
].index
];
1660 isl_int_set(r_row
[0], tab
->mat
->row
[row
][0]);
1661 isl_int_neg(r_row
[1], tab
->mat
->row
[row
][1]);
1662 isl_int_fdiv_r(r_row
[1], r_row
[1], tab
->mat
->row
[row
][0]);
1663 isl_int_neg(r_row
[1], r_row
[1]);
1665 isl_int_set_si(r_row
[2], 0);
1666 for (i
= 0; i
< tab
->n_col
; ++i
)
1667 isl_int_fdiv_r(r_row
[off
+ i
],
1668 tab
->mat
->row
[row
][off
+ i
], tab
->mat
->row
[row
][0]);
1670 tab
->con
[r
].is_nonneg
= 1;
1671 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
1674 tab
->row_sign
[tab
->con
[r
].index
] = isl_tab_row_neg
;
1676 return tab
->con
[r
].index
;
1682 /* Given a non-parametric tableau, add cuts until an integer
1683 * sample point is obtained or until the tableau is determined
1684 * to be integer infeasible.
1685 * As long as there is any non-integer value in the sample point,
1686 * we add appropriate cuts, if possible, for each of these
1687 * non-integer values and then resolve the violated
1688 * cut constraints using restore_lexmin.
1689 * If one of the corresponding rows is equal to an integral
1690 * combination of variables/constraints plus a non-integral constant,
1691 * then there is no way to obtain an integer point and we return
1692 * a tableau that is marked empty.
1693 * The parameter cutting_strategy controls the strategy used when adding cuts
1694 * to remove non-integer points. CUT_ALL adds all possible cuts
1695 * before continuing the search. CUT_ONE adds only one cut at a time.
1697 static struct isl_tab
*cut_to_integer_lexmin(struct isl_tab
*tab
,
1698 int cutting_strategy
)
1709 while ((var
= next_non_integer_var(tab
, -1, &flags
)) != -1) {
1711 if (ISL_FL_ISSET(flags
, I_VAR
)) {
1712 if (isl_tab_mark_empty(tab
) < 0)
1716 row
= tab
->var
[var
].index
;
1717 row
= add_cut(tab
, row
);
1720 if (cutting_strategy
== CUT_ONE
)
1722 } while ((var
= next_non_integer_var(tab
, var
, &flags
)) != -1);
1723 if (restore_lexmin(tab
) < 0)
1734 /* Check whether all the currently active samples also satisfy the inequality
1735 * "ineq" (treated as an equality if eq is set).
1736 * Remove those samples that do not.
1738 static struct isl_tab
*check_samples(struct isl_tab
*tab
, isl_int
*ineq
, int eq
)
1746 isl_assert(tab
->mat
->ctx
, tab
->bmap
, goto error
);
1747 isl_assert(tab
->mat
->ctx
, tab
->samples
, goto error
);
1748 isl_assert(tab
->mat
->ctx
, tab
->samples
->n_col
== 1 + tab
->n_var
, goto error
);
1751 for (i
= tab
->n_outside
; i
< tab
->n_sample
; ++i
) {
1753 isl_seq_inner_product(ineq
, tab
->samples
->row
[i
],
1754 1 + tab
->n_var
, &v
);
1755 sgn
= isl_int_sgn(v
);
1756 if (eq
? (sgn
== 0) : (sgn
>= 0))
1758 tab
= isl_tab_drop_sample(tab
, i
);
1770 /* Check whether the sample value of the tableau is finite,
1771 * i.e., either the tableau does not use a big parameter, or
1772 * all values of the variables are equal to the big parameter plus
1773 * some constant. This constant is the actual sample value.
1775 static int sample_is_finite(struct isl_tab
*tab
)
1782 for (i
= 0; i
< tab
->n_var
; ++i
) {
1784 if (!tab
->var
[i
].is_row
)
1786 row
= tab
->var
[i
].index
;
1787 if (isl_int_ne(tab
->mat
->row
[row
][0], tab
->mat
->row
[row
][2]))
1793 /* Check if the context tableau of sol has any integer points.
1794 * Leave tab in empty state if no integer point can be found.
1795 * If an integer point can be found and if moreover it is finite,
1796 * then it is added to the list of sample values.
1798 * This function is only called when none of the currently active sample
1799 * values satisfies the most recently added constraint.
1801 static struct isl_tab
*check_integer_feasible(struct isl_tab
*tab
)
1803 struct isl_tab_undo
*snap
;
1808 snap
= isl_tab_snap(tab
);
1809 if (isl_tab_push_basis(tab
) < 0)
1812 tab
= cut_to_integer_lexmin(tab
, CUT_ALL
);
1816 if (!tab
->empty
&& sample_is_finite(tab
)) {
1817 struct isl_vec
*sample
;
1819 sample
= isl_tab_get_sample_value(tab
);
1821 if (isl_tab_add_sample(tab
, sample
) < 0)
1825 if (!tab
->empty
&& isl_tab_rollback(tab
, snap
) < 0)
1834 /* Check if any of the currently active sample values satisfies
1835 * the inequality "ineq" (an equality if eq is set).
1837 static int tab_has_valid_sample(struct isl_tab
*tab
, isl_int
*ineq
, int eq
)
1845 isl_assert(tab
->mat
->ctx
, tab
->bmap
, return -1);
1846 isl_assert(tab
->mat
->ctx
, tab
->samples
, return -1);
1847 isl_assert(tab
->mat
->ctx
, tab
->samples
->n_col
== 1 + tab
->n_var
, return -1);
1850 for (i
= tab
->n_outside
; i
< tab
->n_sample
; ++i
) {
1852 isl_seq_inner_product(ineq
, tab
->samples
->row
[i
],
1853 1 + tab
->n_var
, &v
);
1854 sgn
= isl_int_sgn(v
);
1855 if (eq
? (sgn
== 0) : (sgn
>= 0))
1860 return i
< tab
->n_sample
;
1863 /* Add a div specified by "div" to the tableau "tab" and return
1864 * 1 if the div is obviously non-negative.
1866 static int context_tab_add_div(struct isl_tab
*tab
, __isl_keep isl_vec
*div
,
1867 int (*add_ineq
)(void *user
, isl_int
*), void *user
)
1871 struct isl_mat
*samples
;
1874 r
= isl_tab_add_div(tab
, div
, add_ineq
, user
);
1877 nonneg
= tab
->var
[r
].is_nonneg
;
1878 tab
->var
[r
].frozen
= 1;
1880 samples
= isl_mat_extend(tab
->samples
,
1881 tab
->n_sample
, 1 + tab
->n_var
);
1882 tab
->samples
= samples
;
1885 for (i
= tab
->n_outside
; i
< samples
->n_row
; ++i
) {
1886 isl_seq_inner_product(div
->el
+ 1, samples
->row
[i
],
1887 div
->size
- 1, &samples
->row
[i
][samples
->n_col
- 1]);
1888 isl_int_fdiv_q(samples
->row
[i
][samples
->n_col
- 1],
1889 samples
->row
[i
][samples
->n_col
- 1], div
->el
[0]);
1895 /* Add a div specified by "div" to both the main tableau and
1896 * the context tableau. In case of the main tableau, we only
1897 * need to add an extra div. In the context tableau, we also
1898 * need to express the meaning of the div.
1899 * Return the index of the div or -1 if anything went wrong.
1901 static int add_div(struct isl_tab
*tab
, struct isl_context
*context
,
1902 struct isl_vec
*div
)
1907 if ((nonneg
= context
->op
->add_div(context
, div
)) < 0)
1910 if (!context
->op
->is_ok(context
))
1913 if (isl_tab_extend_vars(tab
, 1) < 0)
1915 r
= isl_tab_allocate_var(tab
);
1919 tab
->var
[r
].is_nonneg
= 1;
1920 tab
->var
[r
].frozen
= 1;
1923 return tab
->n_div
- 1;
1925 context
->op
->invalidate(context
);
1929 static int find_div(struct isl_tab
*tab
, isl_int
*div
, isl_int denom
)
1932 unsigned total
= isl_basic_map_total_dim(tab
->bmap
);
1934 for (i
= 0; i
< tab
->bmap
->n_div
; ++i
) {
1935 if (isl_int_ne(tab
->bmap
->div
[i
][0], denom
))
1937 if (!isl_seq_eq(tab
->bmap
->div
[i
] + 1, div
, 1 + total
))
1944 /* Return the index of a div that corresponds to "div".
1945 * We first check if we already have such a div and if not, we create one.
1947 static int get_div(struct isl_tab
*tab
, struct isl_context
*context
,
1948 struct isl_vec
*div
)
1951 struct isl_tab
*context_tab
= context
->op
->peek_tab(context
);
1956 d
= find_div(context_tab
, div
->el
+ 1, div
->el
[0]);
1960 return add_div(tab
, context
, div
);
1963 /* Add a parametric cut to cut away the non-integral sample value
1965 * Let a_i be the coefficients of the constant term and the parameters
1966 * and let b_i be the coefficients of the variables or constraints
1967 * in basis of the tableau.
1968 * Let q be the div q = floor(\sum_i {-a_i} y_i).
1970 * The cut is expressed as
1972 * c = \sum_i -{-a_i} y_i + \sum_i {b_i} x_i + q >= 0
1974 * If q did not already exist in the context tableau, then it is added first.
1975 * If q is in a column of the main tableau then the "+ q" can be accomplished
1976 * by setting the corresponding entry to the denominator of the constraint.
1977 * If q happens to be in a row of the main tableau, then the corresponding
1978 * row needs to be added instead (taking care of the denominators).
1979 * Note that this is very unlikely, but perhaps not entirely impossible.
1981 * The current value of the cut is known to be negative (or at least
1982 * non-positive), so row_sign is set accordingly.
1984 * Return the row of the cut or -1.
1986 static int add_parametric_cut(struct isl_tab
*tab
, int row
,
1987 struct isl_context
*context
)
1989 struct isl_vec
*div
;
1996 unsigned off
= 2 + tab
->M
;
2001 div
= get_row_parameter_div(tab
, row
);
2006 d
= context
->op
->get_div(context
, tab
, div
);
2011 if (isl_tab_extend_cons(tab
, 1) < 0)
2013 r
= isl_tab_allocate_con(tab
);
2017 r_row
= tab
->mat
->row
[tab
->con
[r
].index
];
2018 isl_int_set(r_row
[0], tab
->mat
->row
[row
][0]);
2019 isl_int_neg(r_row
[1], tab
->mat
->row
[row
][1]);
2020 isl_int_fdiv_r(r_row
[1], r_row
[1], tab
->mat
->row
[row
][0]);
2021 isl_int_neg(r_row
[1], r_row
[1]);
2023 isl_int_set_si(r_row
[2], 0);
2024 for (i
= 0; i
< tab
->n_param
; ++i
) {
2025 if (tab
->var
[i
].is_row
)
2027 col
= tab
->var
[i
].index
;
2028 isl_int_neg(r_row
[off
+ col
], tab
->mat
->row
[row
][off
+ col
]);
2029 isl_int_fdiv_r(r_row
[off
+ col
], r_row
[off
+ col
],
2030 tab
->mat
->row
[row
][0]);
2031 isl_int_neg(r_row
[off
+ col
], r_row
[off
+ col
]);
2033 for (i
= 0; i
< tab
->n_div
; ++i
) {
2034 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
2036 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
2037 isl_int_neg(r_row
[off
+ col
], tab
->mat
->row
[row
][off
+ col
]);
2038 isl_int_fdiv_r(r_row
[off
+ col
], r_row
[off
+ col
],
2039 tab
->mat
->row
[row
][0]);
2040 isl_int_neg(r_row
[off
+ col
], r_row
[off
+ col
]);
2042 for (i
= 0; i
< tab
->n_col
; ++i
) {
2043 if (tab
->col_var
[i
] >= 0 &&
2044 (tab
->col_var
[i
] < tab
->n_param
||
2045 tab
->col_var
[i
] >= tab
->n_var
- tab
->n_div
))
2047 isl_int_fdiv_r(r_row
[off
+ i
],
2048 tab
->mat
->row
[row
][off
+ i
], tab
->mat
->row
[row
][0]);
2050 if (tab
->var
[tab
->n_var
- tab
->n_div
+ d
].is_row
) {
2052 int d_row
= tab
->var
[tab
->n_var
- tab
->n_div
+ d
].index
;
2054 isl_int_gcd(gcd
, tab
->mat
->row
[d_row
][0], r_row
[0]);
2055 isl_int_divexact(r_row
[0], r_row
[0], gcd
);
2056 isl_int_divexact(gcd
, tab
->mat
->row
[d_row
][0], gcd
);
2057 isl_seq_combine(r_row
+ 1, gcd
, r_row
+ 1,
2058 r_row
[0], tab
->mat
->row
[d_row
] + 1,
2059 off
- 1 + tab
->n_col
);
2060 isl_int_mul(r_row
[0], r_row
[0], tab
->mat
->row
[d_row
][0]);
2063 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ d
].index
;
2064 isl_int_set(r_row
[off
+ col
], tab
->mat
->row
[row
][0]);
2067 tab
->con
[r
].is_nonneg
= 1;
2068 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
2071 tab
->row_sign
[tab
->con
[r
].index
] = isl_tab_row_neg
;
2073 row
= tab
->con
[r
].index
;
2075 if (d
>= n
&& context
->op
->detect_equalities(context
, tab
) < 0)
2081 /* Construct a tableau for bmap that can be used for computing
2082 * the lexicographic minimum (or maximum) of bmap.
2083 * If not NULL, then dom is the domain where the minimum
2084 * should be computed. In this case, we set up a parametric
2085 * tableau with row signs (initialized to "unknown").
2086 * If M is set, then the tableau will use a big parameter.
2087 * If max is set, then a maximum should be computed instead of a minimum.
2088 * This means that for each variable x, the tableau will contain the variable
2089 * x' = M - x, rather than x' = M + x. This in turn means that the coefficient
2090 * of the variables in all constraints are negated prior to adding them
2093 static struct isl_tab
*tab_for_lexmin(struct isl_basic_map
*bmap
,
2094 struct isl_basic_set
*dom
, unsigned M
, int max
)
2097 struct isl_tab
*tab
;
2101 tab
= isl_tab_alloc(bmap
->ctx
, 2 * bmap
->n_eq
+ bmap
->n_ineq
+ 1,
2102 isl_basic_map_total_dim(bmap
), M
);
2106 tab
->rational
= ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
2108 tab
->n_param
= isl_basic_set_total_dim(dom
) - dom
->n_div
;
2109 tab
->n_div
= dom
->n_div
;
2110 tab
->row_sign
= isl_calloc_array(bmap
->ctx
,
2111 enum isl_tab_row_sign
, tab
->mat
->n_row
);
2112 if (tab
->mat
->n_row
&& !tab
->row_sign
)
2115 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
)) {
2116 if (isl_tab_mark_empty(tab
) < 0)
2121 for (i
= tab
->n_param
; i
< tab
->n_var
- tab
->n_div
; ++i
) {
2122 tab
->var
[i
].is_nonneg
= 1;
2123 tab
->var
[i
].frozen
= 1;
2125 o_var
= 1 + tab
->n_param
;
2126 n_var
= tab
->n_var
- tab
->n_param
- tab
->n_div
;
2127 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
2129 isl_seq_neg(bmap
->eq
[i
] + o_var
,
2130 bmap
->eq
[i
] + o_var
, n_var
);
2131 tab
= add_lexmin_valid_eq(tab
, bmap
->eq
[i
]);
2133 isl_seq_neg(bmap
->eq
[i
] + o_var
,
2134 bmap
->eq
[i
] + o_var
, n_var
);
2135 if (!tab
|| tab
->empty
)
2138 if (bmap
->n_eq
&& restore_lexmin(tab
) < 0)
2140 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
2142 isl_seq_neg(bmap
->ineq
[i
] + o_var
,
2143 bmap
->ineq
[i
] + o_var
, n_var
);
2144 tab
= add_lexmin_ineq(tab
, bmap
->ineq
[i
]);
2146 isl_seq_neg(bmap
->ineq
[i
] + o_var
,
2147 bmap
->ineq
[i
] + o_var
, n_var
);
2148 if (!tab
|| tab
->empty
)
2157 /* Given a main tableau where more than one row requires a split,
2158 * determine and return the "best" row to split on.
2160 * Given two rows in the main tableau, if the inequality corresponding
2161 * to the first row is redundant with respect to that of the second row
2162 * in the current tableau, then it is better to split on the second row,
2163 * since in the positive part, both rows will be positive.
2164 * (In the negative part a pivot will have to be performed and just about
2165 * anything can happen to the sign of the other row.)
2167 * As a simple heuristic, we therefore select the row that makes the most
2168 * of the other rows redundant.
2170 * Perhaps it would also be useful to look at the number of constraints
2171 * that conflict with any given constraint.
2173 * best is the best row so far (-1 when we have not found any row yet).
2174 * best_r is the number of other rows made redundant by row best.
2175 * When best is still -1, bset_r is meaningless, but it is initialized
2176 * to some arbitrary value (0) anyway. Without this redundant initialization
2177 * valgrind may warn about uninitialized memory accesses when isl
2178 * is compiled with some versions of gcc.
2180 static int best_split(struct isl_tab
*tab
, struct isl_tab
*context_tab
)
2182 struct isl_tab_undo
*snap
;
2188 if (isl_tab_extend_cons(context_tab
, 2) < 0)
2191 snap
= isl_tab_snap(context_tab
);
2193 for (split
= tab
->n_redundant
; split
< tab
->n_row
; ++split
) {
2194 struct isl_tab_undo
*snap2
;
2195 struct isl_vec
*ineq
= NULL
;
2199 if (!isl_tab_var_from_row(tab
, split
)->is_nonneg
)
2201 if (tab
->row_sign
[split
] != isl_tab_row_any
)
2204 ineq
= get_row_parameter_ineq(tab
, split
);
2207 ok
= isl_tab_add_ineq(context_tab
, ineq
->el
) >= 0;
2212 snap2
= isl_tab_snap(context_tab
);
2214 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
2215 struct isl_tab_var
*var
;
2219 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
2221 if (tab
->row_sign
[row
] != isl_tab_row_any
)
2224 ineq
= get_row_parameter_ineq(tab
, row
);
2227 ok
= isl_tab_add_ineq(context_tab
, ineq
->el
) >= 0;
2231 var
= &context_tab
->con
[context_tab
->n_con
- 1];
2232 if (!context_tab
->empty
&&
2233 !isl_tab_min_at_most_neg_one(context_tab
, var
))
2235 if (isl_tab_rollback(context_tab
, snap2
) < 0)
2238 if (best
== -1 || r
> best_r
) {
2242 if (isl_tab_rollback(context_tab
, snap
) < 0)
2249 static struct isl_basic_set
*context_lex_peek_basic_set(
2250 struct isl_context
*context
)
2252 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2255 return isl_tab_peek_bset(clex
->tab
);
2258 static struct isl_tab
*context_lex_peek_tab(struct isl_context
*context
)
2260 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2264 static void context_lex_add_eq(struct isl_context
*context
, isl_int
*eq
,
2265 int check
, int update
)
2267 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2268 if (isl_tab_extend_cons(clex
->tab
, 2) < 0)
2270 if (add_lexmin_eq(clex
->tab
, eq
) < 0)
2273 int v
= tab_has_valid_sample(clex
->tab
, eq
, 1);
2277 clex
->tab
= check_integer_feasible(clex
->tab
);
2280 clex
->tab
= check_samples(clex
->tab
, eq
, 1);
2283 isl_tab_free(clex
->tab
);
2287 static void context_lex_add_ineq(struct isl_context
*context
, isl_int
*ineq
,
2288 int check
, int update
)
2290 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2291 if (isl_tab_extend_cons(clex
->tab
, 1) < 0)
2293 clex
->tab
= add_lexmin_ineq(clex
->tab
, ineq
);
2295 int v
= tab_has_valid_sample(clex
->tab
, ineq
, 0);
2299 clex
->tab
= check_integer_feasible(clex
->tab
);
2302 clex
->tab
= check_samples(clex
->tab
, ineq
, 0);
2305 isl_tab_free(clex
->tab
);
2309 static int context_lex_add_ineq_wrap(void *user
, isl_int
*ineq
)
2311 struct isl_context
*context
= (struct isl_context
*)user
;
2312 context_lex_add_ineq(context
, ineq
, 0, 0);
2313 return context
->op
->is_ok(context
) ? 0 : -1;
2316 /* Check which signs can be obtained by "ineq" on all the currently
2317 * active sample values. See row_sign for more information.
2319 static enum isl_tab_row_sign
tab_ineq_sign(struct isl_tab
*tab
, isl_int
*ineq
,
2325 enum isl_tab_row_sign res
= isl_tab_row_unknown
;
2327 isl_assert(tab
->mat
->ctx
, tab
->samples
, return isl_tab_row_unknown
);
2328 isl_assert(tab
->mat
->ctx
, tab
->samples
->n_col
== 1 + tab
->n_var
,
2329 return isl_tab_row_unknown
);
2332 for (i
= tab
->n_outside
; i
< tab
->n_sample
; ++i
) {
2333 isl_seq_inner_product(tab
->samples
->row
[i
], ineq
,
2334 1 + tab
->n_var
, &tmp
);
2335 sgn
= isl_int_sgn(tmp
);
2336 if (sgn
> 0 || (sgn
== 0 && strict
)) {
2337 if (res
== isl_tab_row_unknown
)
2338 res
= isl_tab_row_pos
;
2339 if (res
== isl_tab_row_neg
)
2340 res
= isl_tab_row_any
;
2343 if (res
== isl_tab_row_unknown
)
2344 res
= isl_tab_row_neg
;
2345 if (res
== isl_tab_row_pos
)
2346 res
= isl_tab_row_any
;
2348 if (res
== isl_tab_row_any
)
2356 static enum isl_tab_row_sign
context_lex_ineq_sign(struct isl_context
*context
,
2357 isl_int
*ineq
, int strict
)
2359 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2360 return tab_ineq_sign(clex
->tab
, ineq
, strict
);
2363 /* Check whether "ineq" can be added to the tableau without rendering
2366 static int context_lex_test_ineq(struct isl_context
*context
, isl_int
*ineq
)
2368 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2369 struct isl_tab_undo
*snap
;
2375 if (isl_tab_extend_cons(clex
->tab
, 1) < 0)
2378 snap
= isl_tab_snap(clex
->tab
);
2379 if (isl_tab_push_basis(clex
->tab
) < 0)
2381 clex
->tab
= add_lexmin_ineq(clex
->tab
, ineq
);
2382 clex
->tab
= check_integer_feasible(clex
->tab
);
2385 feasible
= !clex
->tab
->empty
;
2386 if (isl_tab_rollback(clex
->tab
, snap
) < 0)
2392 static int context_lex_get_div(struct isl_context
*context
, struct isl_tab
*tab
,
2393 struct isl_vec
*div
)
2395 return get_div(tab
, context
, div
);
2398 /* Add a div specified by "div" to the context tableau and return
2399 * 1 if the div is obviously non-negative.
2400 * context_tab_add_div will always return 1, because all variables
2401 * in a isl_context_lex tableau are non-negative.
2402 * However, if we are using a big parameter in the context, then this only
2403 * reflects the non-negativity of the variable used to _encode_ the
2404 * div, i.e., div' = M + div, so we can't draw any conclusions.
2406 static int context_lex_add_div(struct isl_context
*context
,
2407 __isl_keep isl_vec
*div
)
2409 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2411 nonneg
= context_tab_add_div(clex
->tab
, div
,
2412 context_lex_add_ineq_wrap
, context
);
2420 static int context_lex_detect_equalities(struct isl_context
*context
,
2421 struct isl_tab
*tab
)
2426 static int context_lex_best_split(struct isl_context
*context
,
2427 struct isl_tab
*tab
)
2429 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2430 struct isl_tab_undo
*snap
;
2433 snap
= isl_tab_snap(clex
->tab
);
2434 if (isl_tab_push_basis(clex
->tab
) < 0)
2436 r
= best_split(tab
, clex
->tab
);
2438 if (r
>= 0 && isl_tab_rollback(clex
->tab
, snap
) < 0)
2444 static int context_lex_is_empty(struct isl_context
*context
)
2446 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2449 return clex
->tab
->empty
;
2452 static void *context_lex_save(struct isl_context
*context
)
2454 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2455 struct isl_tab_undo
*snap
;
2457 snap
= isl_tab_snap(clex
->tab
);
2458 if (isl_tab_push_basis(clex
->tab
) < 0)
2460 if (isl_tab_save_samples(clex
->tab
) < 0)
2466 static void context_lex_restore(struct isl_context
*context
, void *save
)
2468 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2469 if (isl_tab_rollback(clex
->tab
, (struct isl_tab_undo
*)save
) < 0) {
2470 isl_tab_free(clex
->tab
);
2475 static void context_lex_discard(void *save
)
2479 static int context_lex_is_ok(struct isl_context
*context
)
2481 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2485 /* For each variable in the context tableau, check if the variable can
2486 * only attain non-negative values. If so, mark the parameter as non-negative
2487 * in the main tableau. This allows for a more direct identification of some
2488 * cases of violated constraints.
2490 static struct isl_tab
*tab_detect_nonnegative_parameters(struct isl_tab
*tab
,
2491 struct isl_tab
*context_tab
)
2494 struct isl_tab_undo
*snap
;
2495 struct isl_vec
*ineq
= NULL
;
2496 struct isl_tab_var
*var
;
2499 if (context_tab
->n_var
== 0)
2502 ineq
= isl_vec_alloc(tab
->mat
->ctx
, 1 + context_tab
->n_var
);
2506 if (isl_tab_extend_cons(context_tab
, 1) < 0)
2509 snap
= isl_tab_snap(context_tab
);
2512 isl_seq_clr(ineq
->el
, ineq
->size
);
2513 for (i
= 0; i
< context_tab
->n_var
; ++i
) {
2514 isl_int_set_si(ineq
->el
[1 + i
], 1);
2515 if (isl_tab_add_ineq(context_tab
, ineq
->el
) < 0)
2517 var
= &context_tab
->con
[context_tab
->n_con
- 1];
2518 if (!context_tab
->empty
&&
2519 !isl_tab_min_at_most_neg_one(context_tab
, var
)) {
2521 if (i
>= tab
->n_param
)
2522 j
= i
- tab
->n_param
+ tab
->n_var
- tab
->n_div
;
2523 tab
->var
[j
].is_nonneg
= 1;
2526 isl_int_set_si(ineq
->el
[1 + i
], 0);
2527 if (isl_tab_rollback(context_tab
, snap
) < 0)
2531 if (context_tab
->M
&& n
== context_tab
->n_var
) {
2532 context_tab
->mat
= isl_mat_drop_cols(context_tab
->mat
, 2, 1);
2544 static struct isl_tab
*context_lex_detect_nonnegative_parameters(
2545 struct isl_context
*context
, struct isl_tab
*tab
)
2547 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2548 struct isl_tab_undo
*snap
;
2553 snap
= isl_tab_snap(clex
->tab
);
2554 if (isl_tab_push_basis(clex
->tab
) < 0)
2557 tab
= tab_detect_nonnegative_parameters(tab
, clex
->tab
);
2559 if (isl_tab_rollback(clex
->tab
, snap
) < 0)
2568 static void context_lex_invalidate(struct isl_context
*context
)
2570 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2571 isl_tab_free(clex
->tab
);
2575 static void context_lex_free(struct isl_context
*context
)
2577 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2578 isl_tab_free(clex
->tab
);
2582 struct isl_context_op isl_context_lex_op
= {
2583 context_lex_detect_nonnegative_parameters
,
2584 context_lex_peek_basic_set
,
2585 context_lex_peek_tab
,
2587 context_lex_add_ineq
,
2588 context_lex_ineq_sign
,
2589 context_lex_test_ineq
,
2590 context_lex_get_div
,
2591 context_lex_add_div
,
2592 context_lex_detect_equalities
,
2593 context_lex_best_split
,
2594 context_lex_is_empty
,
2597 context_lex_restore
,
2598 context_lex_discard
,
2599 context_lex_invalidate
,
2603 static struct isl_tab
*context_tab_for_lexmin(struct isl_basic_set
*bset
)
2605 struct isl_tab
*tab
;
2609 tab
= tab_for_lexmin((struct isl_basic_map
*)bset
, NULL
, 1, 0);
2612 if (isl_tab_track_bset(tab
, bset
) < 0)
2614 tab
= isl_tab_init_samples(tab
);
2617 isl_basic_set_free(bset
);
2621 static struct isl_context
*isl_context_lex_alloc(struct isl_basic_set
*dom
)
2623 struct isl_context_lex
*clex
;
2628 clex
= isl_alloc_type(dom
->ctx
, struct isl_context_lex
);
2632 clex
->context
.op
= &isl_context_lex_op
;
2634 clex
->tab
= context_tab_for_lexmin(isl_basic_set_copy(dom
));
2635 if (restore_lexmin(clex
->tab
) < 0)
2637 clex
->tab
= check_integer_feasible(clex
->tab
);
2641 return &clex
->context
;
2643 clex
->context
.op
->free(&clex
->context
);
2647 /* Representation of the context when using generalized basis reduction.
2649 * "shifted" contains the offsets of the unit hypercubes that lie inside the
2650 * context. Any rational point in "shifted" can therefore be rounded
2651 * up to an integer point in the context.
2652 * If the context is constrained by any equality, then "shifted" is not used
2653 * as it would be empty.
2655 struct isl_context_gbr
{
2656 struct isl_context context
;
2657 struct isl_tab
*tab
;
2658 struct isl_tab
*shifted
;
2659 struct isl_tab
*cone
;
2662 static struct isl_tab
*context_gbr_detect_nonnegative_parameters(
2663 struct isl_context
*context
, struct isl_tab
*tab
)
2665 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2668 return tab_detect_nonnegative_parameters(tab
, cgbr
->tab
);
2671 static struct isl_basic_set
*context_gbr_peek_basic_set(
2672 struct isl_context
*context
)
2674 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2677 return isl_tab_peek_bset(cgbr
->tab
);
2680 static struct isl_tab
*context_gbr_peek_tab(struct isl_context
*context
)
2682 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2686 /* Initialize the "shifted" tableau of the context, which
2687 * contains the constraints of the original tableau shifted
2688 * by the sum of all negative coefficients. This ensures
2689 * that any rational point in the shifted tableau can
2690 * be rounded up to yield an integer point in the original tableau.
2692 static void gbr_init_shifted(struct isl_context_gbr
*cgbr
)
2695 struct isl_vec
*cst
;
2696 struct isl_basic_set
*bset
= isl_tab_peek_bset(cgbr
->tab
);
2697 unsigned dim
= isl_basic_set_total_dim(bset
);
2699 cst
= isl_vec_alloc(cgbr
->tab
->mat
->ctx
, bset
->n_ineq
);
2703 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
2704 isl_int_set(cst
->el
[i
], bset
->ineq
[i
][0]);
2705 for (j
= 0; j
< dim
; ++j
) {
2706 if (!isl_int_is_neg(bset
->ineq
[i
][1 + j
]))
2708 isl_int_add(bset
->ineq
[i
][0], bset
->ineq
[i
][0],
2709 bset
->ineq
[i
][1 + j
]);
2713 cgbr
->shifted
= isl_tab_from_basic_set(bset
, 0);
2715 for (i
= 0; i
< bset
->n_ineq
; ++i
)
2716 isl_int_set(bset
->ineq
[i
][0], cst
->el
[i
]);
2721 /* Check if the shifted tableau is non-empty, and if so
2722 * use the sample point to construct an integer point
2723 * of the context tableau.
2725 static struct isl_vec
*gbr_get_shifted_sample(struct isl_context_gbr
*cgbr
)
2727 struct isl_vec
*sample
;
2730 gbr_init_shifted(cgbr
);
2733 if (cgbr
->shifted
->empty
)
2734 return isl_vec_alloc(cgbr
->tab
->mat
->ctx
, 0);
2736 sample
= isl_tab_get_sample_value(cgbr
->shifted
);
2737 sample
= isl_vec_ceil(sample
);
2742 static struct isl_basic_set
*drop_constant_terms(struct isl_basic_set
*bset
)
2749 for (i
= 0; i
< bset
->n_eq
; ++i
)
2750 isl_int_set_si(bset
->eq
[i
][0], 0);
2752 for (i
= 0; i
< bset
->n_ineq
; ++i
)
2753 isl_int_set_si(bset
->ineq
[i
][0], 0);
2758 static int use_shifted(struct isl_context_gbr
*cgbr
)
2762 return cgbr
->tab
->bmap
->n_eq
== 0 && cgbr
->tab
->bmap
->n_div
== 0;
2765 static struct isl_vec
*gbr_get_sample(struct isl_context_gbr
*cgbr
)
2767 struct isl_basic_set
*bset
;
2768 struct isl_basic_set
*cone
;
2770 if (isl_tab_sample_is_integer(cgbr
->tab
))
2771 return isl_tab_get_sample_value(cgbr
->tab
);
2773 if (use_shifted(cgbr
)) {
2774 struct isl_vec
*sample
;
2776 sample
= gbr_get_shifted_sample(cgbr
);
2777 if (!sample
|| sample
->size
> 0)
2780 isl_vec_free(sample
);
2784 bset
= isl_tab_peek_bset(cgbr
->tab
);
2785 cgbr
->cone
= isl_tab_from_recession_cone(bset
, 0);
2788 if (isl_tab_track_bset(cgbr
->cone
,
2789 isl_basic_set_copy(bset
)) < 0)
2792 if (isl_tab_detect_implicit_equalities(cgbr
->cone
) < 0)
2795 if (cgbr
->cone
->n_dead
== cgbr
->cone
->n_col
) {
2796 struct isl_vec
*sample
;
2797 struct isl_tab_undo
*snap
;
2799 if (cgbr
->tab
->basis
) {
2800 if (cgbr
->tab
->basis
->n_col
!= 1 + cgbr
->tab
->n_var
) {
2801 isl_mat_free(cgbr
->tab
->basis
);
2802 cgbr
->tab
->basis
= NULL
;
2804 cgbr
->tab
->n_zero
= 0;
2805 cgbr
->tab
->n_unbounded
= 0;
2808 snap
= isl_tab_snap(cgbr
->tab
);
2810 sample
= isl_tab_sample(cgbr
->tab
);
2812 if (!sample
|| isl_tab_rollback(cgbr
->tab
, snap
) < 0) {
2813 isl_vec_free(sample
);
2820 cone
= isl_basic_set_dup(isl_tab_peek_bset(cgbr
->cone
));
2821 cone
= drop_constant_terms(cone
);
2822 cone
= isl_basic_set_update_from_tab(cone
, cgbr
->cone
);
2823 cone
= isl_basic_set_underlying_set(cone
);
2824 cone
= isl_basic_set_gauss(cone
, NULL
);
2826 bset
= isl_basic_set_dup(isl_tab_peek_bset(cgbr
->tab
));
2827 bset
= isl_basic_set_update_from_tab(bset
, cgbr
->tab
);
2828 bset
= isl_basic_set_underlying_set(bset
);
2829 bset
= isl_basic_set_gauss(bset
, NULL
);
2831 return isl_basic_set_sample_with_cone(bset
, cone
);
2834 static void check_gbr_integer_feasible(struct isl_context_gbr
*cgbr
)
2836 struct isl_vec
*sample
;
2841 if (cgbr
->tab
->empty
)
2844 sample
= gbr_get_sample(cgbr
);
2848 if (sample
->size
== 0) {
2849 isl_vec_free(sample
);
2850 if (isl_tab_mark_empty(cgbr
->tab
) < 0)
2855 if (isl_tab_add_sample(cgbr
->tab
, sample
) < 0)
2860 isl_tab_free(cgbr
->tab
);
2864 static struct isl_tab
*add_gbr_eq(struct isl_tab
*tab
, isl_int
*eq
)
2869 if (isl_tab_extend_cons(tab
, 2) < 0)
2872 if (isl_tab_add_eq(tab
, eq
) < 0)
2881 /* Add the equality described by "eq" to the context.
2882 * If "check" is set, then we check if the context is empty after
2883 * adding the equality.
2884 * If "update" is set, then we check if the samples are still valid.
2886 * We do not explicitly add shifted copies of the equality to
2887 * cgbr->shifted since they would conflict with each other.
2888 * Instead, we directly mark cgbr->shifted empty.
2890 static void context_gbr_add_eq(struct isl_context
*context
, isl_int
*eq
,
2891 int check
, int update
)
2893 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2895 cgbr
->tab
= add_gbr_eq(cgbr
->tab
, eq
);
2897 if (cgbr
->shifted
&& !cgbr
->shifted
->empty
&& use_shifted(cgbr
)) {
2898 if (isl_tab_mark_empty(cgbr
->shifted
) < 0)
2902 if (cgbr
->cone
&& cgbr
->cone
->n_col
!= cgbr
->cone
->n_dead
) {
2903 if (isl_tab_extend_cons(cgbr
->cone
, 2) < 0)
2905 if (isl_tab_add_eq(cgbr
->cone
, eq
) < 0)
2910 int v
= tab_has_valid_sample(cgbr
->tab
, eq
, 1);
2914 check_gbr_integer_feasible(cgbr
);
2917 cgbr
->tab
= check_samples(cgbr
->tab
, eq
, 1);
2920 isl_tab_free(cgbr
->tab
);
2924 static void add_gbr_ineq(struct isl_context_gbr
*cgbr
, isl_int
*ineq
)
2929 if (isl_tab_extend_cons(cgbr
->tab
, 1) < 0)
2932 if (isl_tab_add_ineq(cgbr
->tab
, ineq
) < 0)
2935 if (cgbr
->shifted
&& !cgbr
->shifted
->empty
&& use_shifted(cgbr
)) {
2938 dim
= isl_basic_map_total_dim(cgbr
->tab
->bmap
);
2940 if (isl_tab_extend_cons(cgbr
->shifted
, 1) < 0)
2943 for (i
= 0; i
< dim
; ++i
) {
2944 if (!isl_int_is_neg(ineq
[1 + i
]))
2946 isl_int_add(ineq
[0], ineq
[0], ineq
[1 + i
]);
2949 if (isl_tab_add_ineq(cgbr
->shifted
, ineq
) < 0)
2952 for (i
= 0; i
< dim
; ++i
) {
2953 if (!isl_int_is_neg(ineq
[1 + i
]))
2955 isl_int_sub(ineq
[0], ineq
[0], ineq
[1 + i
]);
2959 if (cgbr
->cone
&& cgbr
->cone
->n_col
!= cgbr
->cone
->n_dead
) {
2960 if (isl_tab_extend_cons(cgbr
->cone
, 1) < 0)
2962 if (isl_tab_add_ineq(cgbr
->cone
, ineq
) < 0)
2968 isl_tab_free(cgbr
->tab
);
2972 static void context_gbr_add_ineq(struct isl_context
*context
, isl_int
*ineq
,
2973 int check
, int update
)
2975 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2977 add_gbr_ineq(cgbr
, ineq
);
2982 int v
= tab_has_valid_sample(cgbr
->tab
, ineq
, 0);
2986 check_gbr_integer_feasible(cgbr
);
2989 cgbr
->tab
= check_samples(cgbr
->tab
, ineq
, 0);
2992 isl_tab_free(cgbr
->tab
);
2996 static int context_gbr_add_ineq_wrap(void *user
, isl_int
*ineq
)
2998 struct isl_context
*context
= (struct isl_context
*)user
;
2999 context_gbr_add_ineq(context
, ineq
, 0, 0);
3000 return context
->op
->is_ok(context
) ? 0 : -1;
3003 static enum isl_tab_row_sign
context_gbr_ineq_sign(struct isl_context
*context
,
3004 isl_int
*ineq
, int strict
)
3006 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3007 return tab_ineq_sign(cgbr
->tab
, ineq
, strict
);
3010 /* Check whether "ineq" can be added to the tableau without rendering
3013 static int context_gbr_test_ineq(struct isl_context
*context
, isl_int
*ineq
)
3015 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3016 struct isl_tab_undo
*snap
;
3017 struct isl_tab_undo
*shifted_snap
= NULL
;
3018 struct isl_tab_undo
*cone_snap
= NULL
;
3024 if (isl_tab_extend_cons(cgbr
->tab
, 1) < 0)
3027 snap
= isl_tab_snap(cgbr
->tab
);
3029 shifted_snap
= isl_tab_snap(cgbr
->shifted
);
3031 cone_snap
= isl_tab_snap(cgbr
->cone
);
3032 add_gbr_ineq(cgbr
, ineq
);
3033 check_gbr_integer_feasible(cgbr
);
3036 feasible
= !cgbr
->tab
->empty
;
3037 if (isl_tab_rollback(cgbr
->tab
, snap
) < 0)
3040 if (isl_tab_rollback(cgbr
->shifted
, shifted_snap
))
3042 } else if (cgbr
->shifted
) {
3043 isl_tab_free(cgbr
->shifted
);
3044 cgbr
->shifted
= NULL
;
3047 if (isl_tab_rollback(cgbr
->cone
, cone_snap
))
3049 } else if (cgbr
->cone
) {
3050 isl_tab_free(cgbr
->cone
);
3057 /* Return the column of the last of the variables associated to
3058 * a column that has a non-zero coefficient.
3059 * This function is called in a context where only coefficients
3060 * of parameters or divs can be non-zero.
3062 static int last_non_zero_var_col(struct isl_tab
*tab
, isl_int
*p
)
3067 if (tab
->n_var
== 0)
3070 for (i
= tab
->n_var
- 1; i
>= 0; --i
) {
3071 if (i
>= tab
->n_param
&& i
< tab
->n_var
- tab
->n_div
)
3073 if (tab
->var
[i
].is_row
)
3075 col
= tab
->var
[i
].index
;
3076 if (!isl_int_is_zero(p
[col
]))
3083 /* Look through all the recently added equalities in the context
3084 * to see if we can propagate any of them to the main tableau.
3086 * The newly added equalities in the context are encoded as pairs
3087 * of inequalities starting at inequality "first".
3089 * We tentatively add each of these equalities to the main tableau
3090 * and if this happens to result in a row with a final coefficient
3091 * that is one or negative one, we use it to kill a column
3092 * in the main tableau. Otherwise, we discard the tentatively
3094 * This tentative addition of equality constraints turns
3095 * on the undo facility of the tableau. Turn it off again
3096 * at the end, assuming it was turned off to begin with.
3098 * Return 0 on success and -1 on failure.
3100 static int propagate_equalities(struct isl_context_gbr
*cgbr
,
3101 struct isl_tab
*tab
, unsigned first
)
3104 struct isl_vec
*eq
= NULL
;
3105 isl_bool needs_undo
;
3107 needs_undo
= isl_tab_need_undo(tab
);
3110 eq
= isl_vec_alloc(tab
->mat
->ctx
, 1 + tab
->n_var
);
3114 if (isl_tab_extend_cons(tab
, (cgbr
->tab
->bmap
->n_ineq
- first
)/2) < 0)
3117 isl_seq_clr(eq
->el
+ 1 + tab
->n_param
,
3118 tab
->n_var
- tab
->n_param
- tab
->n_div
);
3119 for (i
= first
; i
< cgbr
->tab
->bmap
->n_ineq
; i
+= 2) {
3122 struct isl_tab_undo
*snap
;
3123 snap
= isl_tab_snap(tab
);
3125 isl_seq_cpy(eq
->el
, cgbr
->tab
->bmap
->ineq
[i
], 1 + tab
->n_param
);
3126 isl_seq_cpy(eq
->el
+ 1 + tab
->n_var
- tab
->n_div
,
3127 cgbr
->tab
->bmap
->ineq
[i
] + 1 + tab
->n_param
,
3130 r
= isl_tab_add_row(tab
, eq
->el
);
3133 r
= tab
->con
[r
].index
;
3134 j
= last_non_zero_var_col(tab
, tab
->mat
->row
[r
] + 2 + tab
->M
);
3135 if (j
< 0 || j
< tab
->n_dead
||
3136 !isl_int_is_one(tab
->mat
->row
[r
][0]) ||
3137 (!isl_int_is_one(tab
->mat
->row
[r
][2 + tab
->M
+ j
]) &&
3138 !isl_int_is_negone(tab
->mat
->row
[r
][2 + tab
->M
+ j
]))) {
3139 if (isl_tab_rollback(tab
, snap
) < 0)
3143 if (isl_tab_pivot(tab
, r
, j
) < 0)
3145 if (isl_tab_kill_col(tab
, j
) < 0)
3148 if (restore_lexmin(tab
) < 0)
3153 isl_tab_clear_undo(tab
);
3159 isl_tab_free(cgbr
->tab
);
3164 static int context_gbr_detect_equalities(struct isl_context
*context
,
3165 struct isl_tab
*tab
)
3167 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3171 struct isl_basic_set
*bset
= isl_tab_peek_bset(cgbr
->tab
);
3172 cgbr
->cone
= isl_tab_from_recession_cone(bset
, 0);
3175 if (isl_tab_track_bset(cgbr
->cone
,
3176 isl_basic_set_copy(bset
)) < 0)
3179 if (isl_tab_detect_implicit_equalities(cgbr
->cone
) < 0)
3182 n_ineq
= cgbr
->tab
->bmap
->n_ineq
;
3183 cgbr
->tab
= isl_tab_detect_equalities(cgbr
->tab
, cgbr
->cone
);
3186 if (cgbr
->tab
->bmap
->n_ineq
> n_ineq
&&
3187 propagate_equalities(cgbr
, tab
, n_ineq
) < 0)
3192 isl_tab_free(cgbr
->tab
);
3197 static int context_gbr_get_div(struct isl_context
*context
, struct isl_tab
*tab
,
3198 struct isl_vec
*div
)
3200 return get_div(tab
, context
, div
);
3203 static int context_gbr_add_div(struct isl_context
*context
,
3204 __isl_keep isl_vec
*div
)
3206 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3210 if (isl_tab_extend_cons(cgbr
->cone
, 3) < 0)
3212 if (isl_tab_extend_vars(cgbr
->cone
, 1) < 0)
3214 if (isl_tab_allocate_var(cgbr
->cone
) <0)
3217 cgbr
->cone
->bmap
= isl_basic_map_extend_space(cgbr
->cone
->bmap
,
3218 isl_basic_map_get_space(cgbr
->cone
->bmap
), 1, 0, 2);
3219 k
= isl_basic_map_alloc_div(cgbr
->cone
->bmap
);
3222 isl_seq_cpy(cgbr
->cone
->bmap
->div
[k
], div
->el
, div
->size
);
3223 if (isl_tab_push(cgbr
->cone
, isl_tab_undo_bmap_div
) < 0)
3226 return context_tab_add_div(cgbr
->tab
, div
,
3227 context_gbr_add_ineq_wrap
, context
);
3230 static int context_gbr_best_split(struct isl_context
*context
,
3231 struct isl_tab
*tab
)
3233 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3234 struct isl_tab_undo
*snap
;
3237 snap
= isl_tab_snap(cgbr
->tab
);
3238 r
= best_split(tab
, cgbr
->tab
);
3240 if (r
>= 0 && isl_tab_rollback(cgbr
->tab
, snap
) < 0)
3246 static int context_gbr_is_empty(struct isl_context
*context
)
3248 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3251 return cgbr
->tab
->empty
;
3254 struct isl_gbr_tab_undo
{
3255 struct isl_tab_undo
*tab_snap
;
3256 struct isl_tab_undo
*shifted_snap
;
3257 struct isl_tab_undo
*cone_snap
;
3260 static void *context_gbr_save(struct isl_context
*context
)
3262 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3263 struct isl_gbr_tab_undo
*snap
;
3268 snap
= isl_alloc_type(cgbr
->tab
->mat
->ctx
, struct isl_gbr_tab_undo
);
3272 snap
->tab_snap
= isl_tab_snap(cgbr
->tab
);
3273 if (isl_tab_save_samples(cgbr
->tab
) < 0)
3277 snap
->shifted_snap
= isl_tab_snap(cgbr
->shifted
);
3279 snap
->shifted_snap
= NULL
;
3282 snap
->cone_snap
= isl_tab_snap(cgbr
->cone
);
3284 snap
->cone_snap
= NULL
;
3292 static void context_gbr_restore(struct isl_context
*context
, void *save
)
3294 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3295 struct isl_gbr_tab_undo
*snap
= (struct isl_gbr_tab_undo
*)save
;
3298 if (isl_tab_rollback(cgbr
->tab
, snap
->tab_snap
) < 0)
3301 if (snap
->shifted_snap
) {
3302 if (isl_tab_rollback(cgbr
->shifted
, snap
->shifted_snap
) < 0)
3304 } else if (cgbr
->shifted
) {
3305 isl_tab_free(cgbr
->shifted
);
3306 cgbr
->shifted
= NULL
;
3309 if (snap
->cone_snap
) {
3310 if (isl_tab_rollback(cgbr
->cone
, snap
->cone_snap
) < 0)
3312 } else if (cgbr
->cone
) {
3313 isl_tab_free(cgbr
->cone
);
3322 isl_tab_free(cgbr
->tab
);
3326 static void context_gbr_discard(void *save
)
3328 struct isl_gbr_tab_undo
*snap
= (struct isl_gbr_tab_undo
*)save
;
3332 static int context_gbr_is_ok(struct isl_context
*context
)
3334 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3338 static void context_gbr_invalidate(struct isl_context
*context
)
3340 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3341 isl_tab_free(cgbr
->tab
);
3345 static void context_gbr_free(struct isl_context
*context
)
3347 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3348 isl_tab_free(cgbr
->tab
);
3349 isl_tab_free(cgbr
->shifted
);
3350 isl_tab_free(cgbr
->cone
);
3354 struct isl_context_op isl_context_gbr_op
= {
3355 context_gbr_detect_nonnegative_parameters
,
3356 context_gbr_peek_basic_set
,
3357 context_gbr_peek_tab
,
3359 context_gbr_add_ineq
,
3360 context_gbr_ineq_sign
,
3361 context_gbr_test_ineq
,
3362 context_gbr_get_div
,
3363 context_gbr_add_div
,
3364 context_gbr_detect_equalities
,
3365 context_gbr_best_split
,
3366 context_gbr_is_empty
,
3369 context_gbr_restore
,
3370 context_gbr_discard
,
3371 context_gbr_invalidate
,
3375 static struct isl_context
*isl_context_gbr_alloc(__isl_keep isl_basic_set
*dom
)
3377 struct isl_context_gbr
*cgbr
;
3382 cgbr
= isl_calloc_type(dom
->ctx
, struct isl_context_gbr
);
3386 cgbr
->context
.op
= &isl_context_gbr_op
;
3388 cgbr
->shifted
= NULL
;
3390 cgbr
->tab
= isl_tab_from_basic_set(dom
, 1);
3391 cgbr
->tab
= isl_tab_init_samples(cgbr
->tab
);
3394 check_gbr_integer_feasible(cgbr
);
3396 return &cgbr
->context
;
3398 cgbr
->context
.op
->free(&cgbr
->context
);
3402 static struct isl_context
*isl_context_alloc(__isl_keep isl_basic_set
*dom
)
3407 if (dom
->ctx
->opt
->context
== ISL_CONTEXT_LEXMIN
)
3408 return isl_context_lex_alloc(dom
);
3410 return isl_context_gbr_alloc(dom
);
3413 /* Construct an isl_sol_map structure for accumulating the solution.
3414 * If track_empty is set, then we also keep track of the parts
3415 * of the context where there is no solution.
3416 * If max is set, then we are solving a maximization, rather than
3417 * a minimization problem, which means that the variables in the
3418 * tableau have value "M - x" rather than "M + x".
3420 static struct isl_sol
*sol_map_init(struct isl_basic_map
*bmap
,
3421 struct isl_basic_set
*dom
, int track_empty
, int max
)
3423 struct isl_sol_map
*sol_map
= NULL
;
3428 sol_map
= isl_calloc_type(bmap
->ctx
, struct isl_sol_map
);
3432 sol_map
->sol
.rational
= ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
3433 sol_map
->sol
.dec_level
.callback
.run
= &sol_dec_level_wrap
;
3434 sol_map
->sol
.dec_level
.sol
= &sol_map
->sol
;
3435 sol_map
->sol
.max
= max
;
3436 sol_map
->sol
.n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
3437 sol_map
->sol
.add
= &sol_map_add_wrap
;
3438 sol_map
->sol
.add_empty
= track_empty
? &sol_map_add_empty_wrap
: NULL
;
3439 sol_map
->sol
.free
= &sol_map_free_wrap
;
3440 sol_map
->map
= isl_map_alloc_space(isl_basic_map_get_space(bmap
), 1,
3445 sol_map
->sol
.context
= isl_context_alloc(dom
);
3446 if (!sol_map
->sol
.context
)
3450 sol_map
->empty
= isl_set_alloc_space(isl_basic_set_get_space(dom
),
3451 1, ISL_SET_DISJOINT
);
3452 if (!sol_map
->empty
)
3456 isl_basic_set_free(dom
);
3457 return &sol_map
->sol
;
3459 isl_basic_set_free(dom
);
3460 sol_map_free(sol_map
);
3464 /* Check whether all coefficients of (non-parameter) variables
3465 * are non-positive, meaning that no pivots can be performed on the row.
3467 static int is_critical(struct isl_tab
*tab
, int row
)
3470 unsigned off
= 2 + tab
->M
;
3472 for (j
= tab
->n_dead
; j
< tab
->n_col
; ++j
) {
3473 if (tab
->col_var
[j
] >= 0 &&
3474 (tab
->col_var
[j
] < tab
->n_param
||
3475 tab
->col_var
[j
] >= tab
->n_var
- tab
->n_div
))
3478 if (isl_int_is_pos(tab
->mat
->row
[row
][off
+ j
]))
3485 /* Check whether the inequality represented by vec is strict over the integers,
3486 * i.e., there are no integer values satisfying the constraint with
3487 * equality. This happens if the gcd of the coefficients is not a divisor
3488 * of the constant term. If so, scale the constraint down by the gcd
3489 * of the coefficients.
3491 static int is_strict(struct isl_vec
*vec
)
3497 isl_seq_gcd(vec
->el
+ 1, vec
->size
- 1, &gcd
);
3498 if (!isl_int_is_one(gcd
)) {
3499 strict
= !isl_int_is_divisible_by(vec
->el
[0], gcd
);
3500 isl_int_fdiv_q(vec
->el
[0], vec
->el
[0], gcd
);
3501 isl_seq_scale_down(vec
->el
+ 1, vec
->el
+ 1, gcd
, vec
->size
-1);
3508 /* Determine the sign of the given row of the main tableau.
3509 * The result is one of
3510 * isl_tab_row_pos: always non-negative; no pivot needed
3511 * isl_tab_row_neg: always non-positive; pivot
3512 * isl_tab_row_any: can be both positive and negative; split
3514 * We first handle some simple cases
3515 * - the row sign may be known already
3516 * - the row may be obviously non-negative
3517 * - the parametric constant may be equal to that of another row
3518 * for which we know the sign. This sign will be either "pos" or
3519 * "any". If it had been "neg" then we would have pivoted before.
3521 * If none of these cases hold, we check the value of the row for each
3522 * of the currently active samples. Based on the signs of these values
3523 * we make an initial determination of the sign of the row.
3525 * all zero -> unk(nown)
3526 * all non-negative -> pos
3527 * all non-positive -> neg
3528 * both negative and positive -> all
3530 * If we end up with "all", we are done.
3531 * Otherwise, we perform a check for positive and/or negative
3532 * values as follows.
3534 * samples neg unk pos
3540 * There is no special sign for "zero", because we can usually treat zero
3541 * as either non-negative or non-positive, whatever works out best.
3542 * However, if the row is "critical", meaning that pivoting is impossible
3543 * then we don't want to limp zero with the non-positive case, because
3544 * then we we would lose the solution for those values of the parameters
3545 * where the value of the row is zero. Instead, we treat 0 as non-negative
3546 * ensuring a split if the row can attain both zero and negative values.
3547 * The same happens when the original constraint was one that could not
3548 * be satisfied with equality by any integer values of the parameters.
3549 * In this case, we normalize the constraint, but then a value of zero
3550 * for the normalized constraint is actually a positive value for the
3551 * original constraint, so again we need to treat zero as non-negative.
3552 * In both these cases, we have the following decision tree instead:
3554 * all non-negative -> pos
3555 * all negative -> neg
3556 * both negative and non-negative -> all
3564 static enum isl_tab_row_sign
row_sign(struct isl_tab
*tab
,
3565 struct isl_sol
*sol
, int row
)
3567 struct isl_vec
*ineq
= NULL
;
3568 enum isl_tab_row_sign res
= isl_tab_row_unknown
;
3573 if (tab
->row_sign
[row
] != isl_tab_row_unknown
)
3574 return tab
->row_sign
[row
];
3575 if (is_obviously_nonneg(tab
, row
))
3576 return isl_tab_row_pos
;
3577 for (row2
= tab
->n_redundant
; row2
< tab
->n_row
; ++row2
) {
3578 if (tab
->row_sign
[row2
] == isl_tab_row_unknown
)
3580 if (identical_parameter_line(tab
, row
, row2
))
3581 return tab
->row_sign
[row2
];
3584 critical
= is_critical(tab
, row
);
3586 ineq
= get_row_parameter_ineq(tab
, row
);
3590 strict
= is_strict(ineq
);
3592 res
= sol
->context
->op
->ineq_sign(sol
->context
, ineq
->el
,
3593 critical
|| strict
);
3595 if (res
== isl_tab_row_unknown
|| res
== isl_tab_row_pos
) {
3596 /* test for negative values */
3598 isl_seq_neg(ineq
->el
, ineq
->el
, ineq
->size
);
3599 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3601 feasible
= sol
->context
->op
->test_ineq(sol
->context
, ineq
->el
);
3605 res
= isl_tab_row_pos
;
3607 res
= (res
== isl_tab_row_unknown
) ? isl_tab_row_neg
3609 if (res
== isl_tab_row_neg
) {
3610 isl_seq_neg(ineq
->el
, ineq
->el
, ineq
->size
);
3611 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3615 if (res
== isl_tab_row_neg
) {
3616 /* test for positive values */
3618 if (!critical
&& !strict
)
3619 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3621 feasible
= sol
->context
->op
->test_ineq(sol
->context
, ineq
->el
);
3625 res
= isl_tab_row_any
;
3632 return isl_tab_row_unknown
;
3635 static void find_solutions(struct isl_sol
*sol
, struct isl_tab
*tab
);
3637 /* Find solutions for values of the parameters that satisfy the given
3640 * We currently take a snapshot of the context tableau that is reset
3641 * when we return from this function, while we make a copy of the main
3642 * tableau, leaving the original main tableau untouched.
3643 * These are fairly arbitrary choices. Making a copy also of the context
3644 * tableau would obviate the need to undo any changes made to it later,
3645 * while taking a snapshot of the main tableau could reduce memory usage.
3646 * If we were to switch to taking a snapshot of the main tableau,
3647 * we would have to keep in mind that we need to save the row signs
3648 * and that we need to do this before saving the current basis
3649 * such that the basis has been restore before we restore the row signs.
3651 static void find_in_pos(struct isl_sol
*sol
, struct isl_tab
*tab
, isl_int
*ineq
)
3657 saved
= sol
->context
->op
->save(sol
->context
);
3659 tab
= isl_tab_dup(tab
);
3663 sol
->context
->op
->add_ineq(sol
->context
, ineq
, 0, 1);
3665 find_solutions(sol
, tab
);
3668 sol
->context
->op
->restore(sol
->context
, saved
);
3670 sol
->context
->op
->discard(saved
);
3676 /* Record the absence of solutions for those values of the parameters
3677 * that do not satisfy the given inequality with equality.
3679 static void no_sol_in_strict(struct isl_sol
*sol
,
3680 struct isl_tab
*tab
, struct isl_vec
*ineq
)
3685 if (!sol
->context
|| sol
->error
)
3687 saved
= sol
->context
->op
->save(sol
->context
);
3689 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3691 sol
->context
->op
->add_ineq(sol
->context
, ineq
->el
, 1, 0);
3700 isl_int_add_ui(ineq
->el
[0], ineq
->el
[0], 1);
3702 sol
->context
->op
->restore(sol
->context
, saved
);
3708 /* Reset all row variables that are marked to have a sign that may
3709 * be both positive and negative to have an unknown sign.
3711 static void reset_any_to_unknown(struct isl_tab
*tab
)
3715 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
3716 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
3718 if (tab
->row_sign
[row
] == isl_tab_row_any
)
3719 tab
->row_sign
[row
] = isl_tab_row_unknown
;
3723 /* Compute the lexicographic minimum of the set represented by the main
3724 * tableau "tab" within the context "sol->context_tab".
3725 * On entry the sample value of the main tableau is lexicographically
3726 * less than or equal to this lexicographic minimum.
3727 * Pivots are performed until a feasible point is found, which is then
3728 * necessarily equal to the minimum, or until the tableau is found to
3729 * be infeasible. Some pivots may need to be performed for only some
3730 * feasible values of the context tableau. If so, the context tableau
3731 * is split into a part where the pivot is needed and a part where it is not.
3733 * Whenever we enter the main loop, the main tableau is such that no
3734 * "obvious" pivots need to be performed on it, where "obvious" means
3735 * that the given row can be seen to be negative without looking at
3736 * the context tableau. In particular, for non-parametric problems,
3737 * no pivots need to be performed on the main tableau.
3738 * The caller of find_solutions is responsible for making this property
3739 * hold prior to the first iteration of the loop, while restore_lexmin
3740 * is called before every other iteration.
3742 * Inside the main loop, we first examine the signs of the rows of
3743 * the main tableau within the context of the context tableau.
3744 * If we find a row that is always non-positive for all values of
3745 * the parameters satisfying the context tableau and negative for at
3746 * least one value of the parameters, we perform the appropriate pivot
3747 * and start over. An exception is the case where no pivot can be
3748 * performed on the row. In this case, we require that the sign of
3749 * the row is negative for all values of the parameters (rather than just
3750 * non-positive). This special case is handled inside row_sign, which
3751 * will say that the row can have any sign if it determines that it can
3752 * attain both negative and zero values.
3754 * If we can't find a row that always requires a pivot, but we can find
3755 * one or more rows that require a pivot for some values of the parameters
3756 * (i.e., the row can attain both positive and negative signs), then we split
3757 * the context tableau into two parts, one where we force the sign to be
3758 * non-negative and one where we force is to be negative.
3759 * The non-negative part is handled by a recursive call (through find_in_pos).
3760 * Upon returning from this call, we continue with the negative part and
3761 * perform the required pivot.
3763 * If no such rows can be found, all rows are non-negative and we have
3764 * found a (rational) feasible point. If we only wanted a rational point
3766 * Otherwise, we check if all values of the sample point of the tableau
3767 * are integral for the variables. If so, we have found the minimal
3768 * integral point and we are done.
3769 * If the sample point is not integral, then we need to make a distinction
3770 * based on whether the constant term is non-integral or the coefficients
3771 * of the parameters. Furthermore, in order to decide how to handle
3772 * the non-integrality, we also need to know whether the coefficients
3773 * of the other columns in the tableau are integral. This leads
3774 * to the following table. The first two rows do not correspond
3775 * to a non-integral sample point and are only mentioned for completeness.
3777 * constant parameters other
3780 * int int rat | -> no problem
3782 * rat int int -> fail
3784 * rat int rat -> cut
3787 * rat rat rat | -> parametric cut
3790 * rat rat int | -> split context
3792 * If the parametric constant is completely integral, then there is nothing
3793 * to be done. If the constant term is non-integral, but all the other
3794 * coefficient are integral, then there is nothing that can be done
3795 * and the tableau has no integral solution.
3796 * If, on the other hand, one or more of the other columns have rational
3797 * coefficients, but the parameter coefficients are all integral, then
3798 * we can perform a regular (non-parametric) cut.
3799 * Finally, if there is any parameter coefficient that is non-integral,
3800 * then we need to involve the context tableau. There are two cases here.
3801 * If at least one other column has a rational coefficient, then we
3802 * can perform a parametric cut in the main tableau by adding a new
3803 * integer division in the context tableau.
3804 * If all other columns have integral coefficients, then we need to
3805 * enforce that the rational combination of parameters (c + \sum a_i y_i)/m
3806 * is always integral. We do this by introducing an integer division
3807 * q = floor((c + \sum a_i y_i)/m) and stipulating that its argument should
3808 * always be integral in the context tableau, i.e., m q = c + \sum a_i y_i.
3809 * Since q is expressed in the tableau as
3810 * c + \sum a_i y_i - m q >= 0
3811 * -c - \sum a_i y_i + m q + m - 1 >= 0
3812 * it is sufficient to add the inequality
3813 * -c - \sum a_i y_i + m q >= 0
3814 * In the part of the context where this inequality does not hold, the
3815 * main tableau is marked as being empty.
3817 static void find_solutions(struct isl_sol
*sol
, struct isl_tab
*tab
)
3819 struct isl_context
*context
;
3822 if (!tab
|| sol
->error
)
3825 context
= sol
->context
;
3829 if (context
->op
->is_empty(context
))
3832 for (r
= 0; r
>= 0 && tab
&& !tab
->empty
; r
= restore_lexmin(tab
)) {
3835 enum isl_tab_row_sign sgn
;
3839 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
3840 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
3842 sgn
= row_sign(tab
, sol
, row
);
3845 tab
->row_sign
[row
] = sgn
;
3846 if (sgn
== isl_tab_row_any
)
3848 if (sgn
== isl_tab_row_any
&& split
== -1)
3850 if (sgn
== isl_tab_row_neg
)
3853 if (row
< tab
->n_row
)
3856 struct isl_vec
*ineq
;
3858 split
= context
->op
->best_split(context
, tab
);
3861 ineq
= get_row_parameter_ineq(tab
, split
);
3865 reset_any_to_unknown(tab
);
3866 tab
->row_sign
[split
] = isl_tab_row_pos
;
3868 find_in_pos(sol
, tab
, ineq
->el
);
3869 tab
->row_sign
[split
] = isl_tab_row_neg
;
3870 isl_seq_neg(ineq
->el
, ineq
->el
, ineq
->size
);
3871 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3873 context
->op
->add_ineq(context
, ineq
->el
, 0, 1);
3881 row
= first_non_integer_row(tab
, &flags
);
3884 if (ISL_FL_ISSET(flags
, I_PAR
)) {
3885 if (ISL_FL_ISSET(flags
, I_VAR
)) {
3886 if (isl_tab_mark_empty(tab
) < 0)
3890 row
= add_cut(tab
, row
);
3891 } else if (ISL_FL_ISSET(flags
, I_VAR
)) {
3892 struct isl_vec
*div
;
3893 struct isl_vec
*ineq
;
3895 div
= get_row_split_div(tab
, row
);
3898 d
= context
->op
->get_div(context
, tab
, div
);
3902 ineq
= ineq_for_div(context
->op
->peek_basic_set(context
), d
);
3906 no_sol_in_strict(sol
, tab
, ineq
);
3907 isl_seq_neg(ineq
->el
, ineq
->el
, ineq
->size
);
3908 context
->op
->add_ineq(context
, ineq
->el
, 1, 1);
3910 if (sol
->error
|| !context
->op
->is_ok(context
))
3912 tab
= set_row_cst_to_div(tab
, row
, d
);
3913 if (context
->op
->is_empty(context
))
3916 row
= add_parametric_cut(tab
, row
, context
);
3931 /* Does "sol" contain a pair of partial solutions that could potentially
3934 * We currently only check that "sol" is not in an error state
3935 * and that there are at least two partial solutions of which the final two
3936 * are defined at the same level.
3938 static int sol_has_mergeable_solutions(struct isl_sol
*sol
)
3944 if (!sol
->partial
->next
)
3946 return sol
->partial
->level
== sol
->partial
->next
->level
;
3949 /* Compute the lexicographic minimum of the set represented by the main
3950 * tableau "tab" within the context "sol->context_tab".
3952 * As a preprocessing step, we first transfer all the purely parametric
3953 * equalities from the main tableau to the context tableau, i.e.,
3954 * parameters that have been pivoted to a row.
3955 * These equalities are ignored by the main algorithm, because the
3956 * corresponding rows may not be marked as being non-negative.
3957 * In parts of the context where the added equality does not hold,
3958 * the main tableau is marked as being empty.
3960 * Before we embark on the actual computation, we save a copy
3961 * of the context. When we return, we check if there are any
3962 * partial solutions that can potentially be merged. If so,
3963 * we perform a rollback to the initial state of the context.
3964 * The merging of partial solutions happens inside calls to
3965 * sol_dec_level that are pushed onto the undo stack of the context.
3966 * If there are no partial solutions that can potentially be merged
3967 * then the rollback is skipped as it would just be wasted effort.
3969 static void find_solutions_main(struct isl_sol
*sol
, struct isl_tab
*tab
)
3979 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
3983 if (tab
->row_var
[row
] < 0)
3985 if (tab
->row_var
[row
] >= tab
->n_param
&&
3986 tab
->row_var
[row
] < tab
->n_var
- tab
->n_div
)
3988 if (tab
->row_var
[row
] < tab
->n_param
)
3989 p
= tab
->row_var
[row
];
3991 p
= tab
->row_var
[row
]
3992 + tab
->n_param
- (tab
->n_var
- tab
->n_div
);
3994 eq
= isl_vec_alloc(tab
->mat
->ctx
, 1+tab
->n_param
+tab
->n_div
);
3997 get_row_parameter_line(tab
, row
, eq
->el
);
3998 isl_int_neg(eq
->el
[1 + p
], tab
->mat
->row
[row
][0]);
3999 eq
= isl_vec_normalize(eq
);
4002 no_sol_in_strict(sol
, tab
, eq
);
4004 isl_seq_neg(eq
->el
, eq
->el
, eq
->size
);
4006 no_sol_in_strict(sol
, tab
, eq
);
4007 isl_seq_neg(eq
->el
, eq
->el
, eq
->size
);
4009 sol
->context
->op
->add_eq(sol
->context
, eq
->el
, 1, 1);
4013 if (isl_tab_mark_redundant(tab
, row
) < 0)
4016 if (sol
->context
->op
->is_empty(sol
->context
))
4019 row
= tab
->n_redundant
- 1;
4022 saved
= sol
->context
->op
->save(sol
->context
);
4024 find_solutions(sol
, tab
);
4026 if (sol_has_mergeable_solutions(sol
))
4027 sol
->context
->op
->restore(sol
->context
, saved
);
4029 sol
->context
->op
->discard(saved
);
4040 /* Check if integer division "div" of "dom" also occurs in "bmap".
4041 * If so, return its position within the divs.
4042 * If not, return -1.
4044 static int find_context_div(struct isl_basic_map
*bmap
,
4045 struct isl_basic_set
*dom
, unsigned div
)
4048 unsigned b_dim
= isl_space_dim(bmap
->dim
, isl_dim_all
);
4049 unsigned d_dim
= isl_space_dim(dom
->dim
, isl_dim_all
);
4051 if (isl_int_is_zero(dom
->div
[div
][0]))
4053 if (isl_seq_first_non_zero(dom
->div
[div
] + 2 + d_dim
, dom
->n_div
) != -1)
4056 for (i
= 0; i
< bmap
->n_div
; ++i
) {
4057 if (isl_int_is_zero(bmap
->div
[i
][0]))
4059 if (isl_seq_first_non_zero(bmap
->div
[i
] + 2 + d_dim
,
4060 (b_dim
- d_dim
) + bmap
->n_div
) != -1)
4062 if (isl_seq_eq(bmap
->div
[i
], dom
->div
[div
], 2 + d_dim
))
4068 /* The correspondence between the variables in the main tableau,
4069 * the context tableau, and the input map and domain is as follows.
4070 * The first n_param and the last n_div variables of the main tableau
4071 * form the variables of the context tableau.
4072 * In the basic map, these n_param variables correspond to the
4073 * parameters and the input dimensions. In the domain, they correspond
4074 * to the parameters and the set dimensions.
4075 * The n_div variables correspond to the integer divisions in the domain.
4076 * To ensure that everything lines up, we may need to copy some of the
4077 * integer divisions of the domain to the map. These have to be placed
4078 * in the same order as those in the context and they have to be placed
4079 * after any other integer divisions that the map may have.
4080 * This function performs the required reordering.
4082 static struct isl_basic_map
*align_context_divs(struct isl_basic_map
*bmap
,
4083 struct isl_basic_set
*dom
)
4089 for (i
= 0; i
< dom
->n_div
; ++i
)
4090 if (find_context_div(bmap
, dom
, i
) != -1)
4092 other
= bmap
->n_div
- common
;
4093 if (dom
->n_div
- common
> 0) {
4094 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
4095 dom
->n_div
- common
, 0, 0);
4099 for (i
= 0; i
< dom
->n_div
; ++i
) {
4100 int pos
= find_context_div(bmap
, dom
, i
);
4102 pos
= isl_basic_map_alloc_div(bmap
);
4105 isl_int_set_si(bmap
->div
[pos
][0], 0);
4107 if (pos
!= other
+ i
)
4108 isl_basic_map_swap_div(bmap
, pos
, other
+ i
);
4112 isl_basic_map_free(bmap
);
4116 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
4117 * some obvious symmetries.
4119 * We make sure the divs in the domain are properly ordered,
4120 * because they will be added one by one in the given order
4121 * during the construction of the solution map.
4123 static struct isl_sol
*basic_map_partial_lexopt_base_sol(
4124 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
4125 __isl_give isl_set
**empty
, int max
,
4126 struct isl_sol
*(*init
)(__isl_keep isl_basic_map
*bmap
,
4127 __isl_take isl_basic_set
*dom
, int track_empty
, int max
))
4129 struct isl_tab
*tab
;
4130 struct isl_sol
*sol
= NULL
;
4131 struct isl_context
*context
;
4134 dom
= isl_basic_set_order_divs(dom
);
4135 bmap
= align_context_divs(bmap
, dom
);
4137 sol
= init(bmap
, dom
, !!empty
, max
);
4141 context
= sol
->context
;
4142 if (isl_basic_set_plain_is_empty(context
->op
->peek_basic_set(context
)))
4144 else if (isl_basic_map_plain_is_empty(bmap
)) {
4147 isl_basic_set_copy(context
->op
->peek_basic_set(context
)));
4149 tab
= tab_for_lexmin(bmap
,
4150 context
->op
->peek_basic_set(context
), 1, max
);
4151 tab
= context
->op
->detect_nonnegative_parameters(context
, tab
);
4152 find_solutions_main(sol
, tab
);
4157 isl_basic_map_free(bmap
);
4161 isl_basic_map_free(bmap
);
4165 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
4166 * some obvious symmetries.
4168 * We call basic_map_partial_lexopt_base_sol and extract the results.
4170 static __isl_give isl_map
*basic_map_partial_lexopt_base(
4171 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
4172 __isl_give isl_set
**empty
, int max
)
4174 isl_map
*result
= NULL
;
4175 struct isl_sol
*sol
;
4176 struct isl_sol_map
*sol_map
;
4178 sol
= basic_map_partial_lexopt_base_sol(bmap
, dom
, empty
, max
,
4182 sol_map
= (struct isl_sol_map
*) sol
;
4184 result
= isl_map_copy(sol_map
->map
);
4186 *empty
= isl_set_copy(sol_map
->empty
);
4187 sol_free(&sol_map
->sol
);
4191 /* Return a count of the number of occurrences of the "n" first
4192 * variables in the inequality constraints of "bmap".
4194 static __isl_give
int *count_occurrences(__isl_keep isl_basic_map
*bmap
,
4203 ctx
= isl_basic_map_get_ctx(bmap
);
4204 occurrences
= isl_calloc_array(ctx
, int, n
);
4208 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
4209 for (j
= 0; j
< n
; ++j
) {
4210 if (!isl_int_is_zero(bmap
->ineq
[i
][1 + j
]))
4218 /* Do all of the "n" variables with non-zero coefficients in "c"
4219 * occur in exactly a single constraint.
4220 * "occurrences" is an array of length "n" containing the number
4221 * of occurrences of each of the variables in the inequality constraints.
4223 static int single_occurrence(int n
, isl_int
*c
, int *occurrences
)
4227 for (i
= 0; i
< n
; ++i
) {
4228 if (isl_int_is_zero(c
[i
]))
4230 if (occurrences
[i
] != 1)
4237 /* Do all of the "n" initial variables that occur in inequality constraint
4238 * "ineq" of "bmap" only occur in that constraint?
4240 static int all_single_occurrence(__isl_keep isl_basic_map
*bmap
, int ineq
,
4245 for (i
= 0; i
< n
; ++i
) {
4246 if (isl_int_is_zero(bmap
->ineq
[ineq
][1 + i
]))
4248 for (j
= 0; j
< bmap
->n_ineq
; ++j
) {
4251 if (!isl_int_is_zero(bmap
->ineq
[j
][1 + i
]))
4259 /* Structure used during detection of parallel constraints.
4260 * n_in: number of "input" variables: isl_dim_param + isl_dim_in
4261 * n_out: number of "output" variables: isl_dim_out + isl_dim_div
4262 * val: the coefficients of the output variables
4264 struct isl_constraint_equal_info
{
4265 isl_basic_map
*bmap
;
4271 /* Check whether the coefficients of the output variables
4272 * of the constraint in "entry" are equal to info->val.
4274 static int constraint_equal(const void *entry
, const void *val
)
4276 isl_int
**row
= (isl_int
**)entry
;
4277 const struct isl_constraint_equal_info
*info
= val
;
4279 return isl_seq_eq((*row
) + 1 + info
->n_in
, info
->val
, info
->n_out
);
4282 /* Check whether "bmap" has a pair of constraints that have
4283 * the same coefficients for the output variables.
4284 * Note that the coefficients of the existentially quantified
4285 * variables need to be zero since the existentially quantified
4286 * of the result are usually not the same as those of the input.
4287 * Furthermore, check that each of the input variables that occur
4288 * in those constraints does not occur in any other constraint.
4289 * If so, return 1 and return the row indices of the two constraints
4290 * in *first and *second.
4292 static int parallel_constraints(__isl_keep isl_basic_map
*bmap
,
4293 int *first
, int *second
)
4297 int *occurrences
= NULL
;
4298 struct isl_hash_table
*table
= NULL
;
4299 struct isl_hash_table_entry
*entry
;
4300 struct isl_constraint_equal_info info
;
4304 ctx
= isl_basic_map_get_ctx(bmap
);
4305 table
= isl_hash_table_alloc(ctx
, bmap
->n_ineq
);
4309 info
.n_in
= isl_basic_map_dim(bmap
, isl_dim_param
) +
4310 isl_basic_map_dim(bmap
, isl_dim_in
);
4311 occurrences
= count_occurrences(bmap
, info
.n_in
);
4312 if (info
.n_in
&& !occurrences
)
4315 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4316 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4317 info
.n_out
= n_out
+ n_div
;
4318 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
4321 info
.val
= bmap
->ineq
[i
] + 1 + info
.n_in
;
4322 if (isl_seq_first_non_zero(info
.val
, n_out
) < 0)
4324 if (isl_seq_first_non_zero(info
.val
+ n_out
, n_div
) >= 0)
4326 if (!single_occurrence(info
.n_in
, bmap
->ineq
[i
] + 1,
4329 hash
= isl_seq_get_hash(info
.val
, info
.n_out
);
4330 entry
= isl_hash_table_find(ctx
, table
, hash
,
4331 constraint_equal
, &info
, 1);
4336 entry
->data
= &bmap
->ineq
[i
];
4339 if (i
< bmap
->n_ineq
) {
4340 *first
= ((isl_int
**)entry
->data
) - bmap
->ineq
;
4344 isl_hash_table_free(ctx
, table
);
4347 return i
< bmap
->n_ineq
;
4349 isl_hash_table_free(ctx
, table
);
4354 /* Given a set of upper bounds in "var", add constraints to "bset"
4355 * that make the i-th bound smallest.
4357 * In particular, if there are n bounds b_i, then add the constraints
4359 * b_i <= b_j for j > i
4360 * b_i < b_j for j < i
4362 static __isl_give isl_basic_set
*select_minimum(__isl_take isl_basic_set
*bset
,
4363 __isl_keep isl_mat
*var
, int i
)
4368 ctx
= isl_mat_get_ctx(var
);
4370 for (j
= 0; j
< var
->n_row
; ++j
) {
4373 k
= isl_basic_set_alloc_inequality(bset
);
4376 isl_seq_combine(bset
->ineq
[k
], ctx
->one
, var
->row
[j
],
4377 ctx
->negone
, var
->row
[i
], var
->n_col
);
4378 isl_int_set_si(bset
->ineq
[k
][var
->n_col
], 0);
4380 isl_int_sub_ui(bset
->ineq
[k
][0], bset
->ineq
[k
][0], 1);
4383 bset
= isl_basic_set_finalize(bset
);
4387 isl_basic_set_free(bset
);
4391 /* Given a set of upper bounds on the last "input" variable m,
4392 * construct a set that assigns the minimal upper bound to m, i.e.,
4393 * construct a set that divides the space into cells where one
4394 * of the upper bounds is smaller than all the others and assign
4395 * this upper bound to m.
4397 * In particular, if there are n bounds b_i, then the result
4398 * consists of n basic sets, each one of the form
4401 * b_i <= b_j for j > i
4402 * b_i < b_j for j < i
4404 static __isl_give isl_set
*set_minimum(__isl_take isl_space
*dim
,
4405 __isl_take isl_mat
*var
)
4408 isl_basic_set
*bset
= NULL
;
4409 isl_set
*set
= NULL
;
4414 set
= isl_set_alloc_space(isl_space_copy(dim
),
4415 var
->n_row
, ISL_SET_DISJOINT
);
4417 for (i
= 0; i
< var
->n_row
; ++i
) {
4418 bset
= isl_basic_set_alloc_space(isl_space_copy(dim
), 0,
4420 k
= isl_basic_set_alloc_equality(bset
);
4423 isl_seq_cpy(bset
->eq
[k
], var
->row
[i
], var
->n_col
);
4424 isl_int_set_si(bset
->eq
[k
][var
->n_col
], -1);
4425 bset
= select_minimum(bset
, var
, i
);
4426 set
= isl_set_add_basic_set(set
, bset
);
4429 isl_space_free(dim
);
4433 isl_basic_set_free(bset
);
4435 isl_space_free(dim
);
4440 /* Given that the last input variable of "bmap" represents the minimum
4441 * of the bounds in "cst", check whether we need to split the domain
4442 * based on which bound attains the minimum.
4444 * A split is needed when the minimum appears in an integer division
4445 * or in an equality. Otherwise, it is only needed if it appears in
4446 * an upper bound that is different from the upper bounds on which it
4449 static int need_split_basic_map(__isl_keep isl_basic_map
*bmap
,
4450 __isl_keep isl_mat
*cst
)
4456 pos
= cst
->n_col
- 1;
4457 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4459 for (i
= 0; i
< bmap
->n_div
; ++i
)
4460 if (!isl_int_is_zero(bmap
->div
[i
][2 + pos
]))
4463 for (i
= 0; i
< bmap
->n_eq
; ++i
)
4464 if (!isl_int_is_zero(bmap
->eq
[i
][1 + pos
]))
4467 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
4468 if (isl_int_is_nonneg(bmap
->ineq
[i
][1 + pos
]))
4470 if (!isl_int_is_negone(bmap
->ineq
[i
][1 + pos
]))
4472 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + pos
+ 1,
4473 total
- pos
- 1) >= 0)
4476 for (j
= 0; j
< cst
->n_row
; ++j
)
4477 if (isl_seq_eq(bmap
->ineq
[i
], cst
->row
[j
], cst
->n_col
))
4479 if (j
>= cst
->n_row
)
4486 /* Given that the last set variable of "bset" represents the minimum
4487 * of the bounds in "cst", check whether we need to split the domain
4488 * based on which bound attains the minimum.
4490 * We simply call need_split_basic_map here. This is safe because
4491 * the position of the minimum is computed from "cst" and not
4494 static int need_split_basic_set(__isl_keep isl_basic_set
*bset
,
4495 __isl_keep isl_mat
*cst
)
4497 return need_split_basic_map((isl_basic_map
*)bset
, cst
);
4500 /* Given that the last set variable of "set" represents the minimum
4501 * of the bounds in "cst", check whether we need to split the domain
4502 * based on which bound attains the minimum.
4504 static int need_split_set(__isl_keep isl_set
*set
, __isl_keep isl_mat
*cst
)
4508 for (i
= 0; i
< set
->n
; ++i
)
4509 if (need_split_basic_set(set
->p
[i
], cst
))
4515 /* Given a set of which the last set variable is the minimum
4516 * of the bounds in "cst", split each basic set in the set
4517 * in pieces where one of the bounds is (strictly) smaller than the others.
4518 * This subdivision is given in "min_expr".
4519 * The variable is subsequently projected out.
4521 * We only do the split when it is needed.
4522 * For example if the last input variable m = min(a,b) and the only
4523 * constraints in the given basic set are lower bounds on m,
4524 * i.e., l <= m = min(a,b), then we can simply project out m
4525 * to obtain l <= a and l <= b, without having to split on whether
4526 * m is equal to a or b.
4528 static __isl_give isl_set
*split(__isl_take isl_set
*empty
,
4529 __isl_take isl_set
*min_expr
, __isl_take isl_mat
*cst
)
4536 if (!empty
|| !min_expr
|| !cst
)
4539 n_in
= isl_set_dim(empty
, isl_dim_set
);
4540 dim
= isl_set_get_space(empty
);
4541 dim
= isl_space_drop_dims(dim
, isl_dim_set
, n_in
- 1, 1);
4542 res
= isl_set_empty(dim
);
4544 for (i
= 0; i
< empty
->n
; ++i
) {
4547 set
= isl_set_from_basic_set(isl_basic_set_copy(empty
->p
[i
]));
4548 if (need_split_basic_set(empty
->p
[i
], cst
))
4549 set
= isl_set_intersect(set
, isl_set_copy(min_expr
));
4550 set
= isl_set_remove_dims(set
, isl_dim_set
, n_in
- 1, 1);
4552 res
= isl_set_union_disjoint(res
, set
);
4555 isl_set_free(empty
);
4556 isl_set_free(min_expr
);
4560 isl_set_free(empty
);
4561 isl_set_free(min_expr
);
4566 /* Given a map of which the last input variable is the minimum
4567 * of the bounds in "cst", split each basic set in the set
4568 * in pieces where one of the bounds is (strictly) smaller than the others.
4569 * This subdivision is given in "min_expr".
4570 * The variable is subsequently projected out.
4572 * The implementation is essentially the same as that of "split".
4574 static __isl_give isl_map
*split_domain(__isl_take isl_map
*opt
,
4575 __isl_take isl_set
*min_expr
, __isl_take isl_mat
*cst
)
4582 if (!opt
|| !min_expr
|| !cst
)
4585 n_in
= isl_map_dim(opt
, isl_dim_in
);
4586 dim
= isl_map_get_space(opt
);
4587 dim
= isl_space_drop_dims(dim
, isl_dim_in
, n_in
- 1, 1);
4588 res
= isl_map_empty(dim
);
4590 for (i
= 0; i
< opt
->n
; ++i
) {
4593 map
= isl_map_from_basic_map(isl_basic_map_copy(opt
->p
[i
]));
4594 if (need_split_basic_map(opt
->p
[i
], cst
))
4595 map
= isl_map_intersect_domain(map
,
4596 isl_set_copy(min_expr
));
4597 map
= isl_map_remove_dims(map
, isl_dim_in
, n_in
- 1, 1);
4599 res
= isl_map_union_disjoint(res
, map
);
4603 isl_set_free(min_expr
);
4608 isl_set_free(min_expr
);
4613 static __isl_give isl_map
*basic_map_partial_lexopt(
4614 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
4615 __isl_give isl_set
**empty
, int max
);
4617 /* This function is called from basic_map_partial_lexopt_symm.
4618 * The last variable of "bmap" and "dom" corresponds to the minimum
4619 * of the bounds in "cst". "map_space" is the space of the original
4620 * input relation (of basic_map_partial_lexopt_symm) and "set_space"
4621 * is the space of the original domain.
4623 * We recursively call basic_map_partial_lexopt and then plug in
4624 * the definition of the minimum in the result.
4626 static __isl_give isl_map
*basic_map_partial_lexopt_symm_core(
4627 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
4628 __isl_give isl_set
**empty
, int max
, __isl_take isl_mat
*cst
,
4629 __isl_take isl_space
*map_space
, __isl_take isl_space
*set_space
)
4634 min_expr
= set_minimum(isl_basic_set_get_space(dom
), isl_mat_copy(cst
));
4636 opt
= basic_map_partial_lexopt(bmap
, dom
, empty
, max
);
4639 *empty
= split(*empty
,
4640 isl_set_copy(min_expr
), isl_mat_copy(cst
));
4641 *empty
= isl_set_reset_space(*empty
, set_space
);
4644 opt
= split_domain(opt
, min_expr
, cst
);
4645 opt
= isl_map_reset_space(opt
, map_space
);
4650 /* Extract a domain from "bmap" for the purpose of computing
4651 * a lexicographic optimum.
4653 * This function is only called when the caller wants to compute a full
4654 * lexicographic optimum, i.e., without specifying a domain. In this case,
4655 * the caller is not interested in the part of the domain space where
4656 * there is no solution and the domain can be initialized to those constraints
4657 * of "bmap" that only involve the parameters and the input dimensions.
4658 * This relieves the parametric programming engine from detecting those
4659 * inequalities and transferring them to the context. More importantly,
4660 * it ensures that those inequalities are transferred first and not
4661 * intermixed with inequalities that actually split the domain.
4663 * If the caller does not require the absence of existentially quantified
4664 * variables in the result (i.e., if ISL_OPT_QE is not set in "flags"),
4665 * then the actual domain of "bmap" can be used. This ensures that
4666 * the domain does not need to be split at all just to separate out
4667 * pieces of the domain that do not have a solution from piece that do.
4668 * This domain cannot be used in general because it may involve
4669 * (unknown) existentially quantified variables which will then also
4670 * appear in the solution.
4672 static __isl_give isl_basic_set
*extract_domain(__isl_keep isl_basic_map
*bmap
,
4678 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4679 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4680 bmap
= isl_basic_map_copy(bmap
);
4681 if (ISL_FL_ISSET(flags
, ISL_OPT_QE
)) {
4682 bmap
= isl_basic_map_drop_constraints_involving_dims(bmap
,
4683 isl_dim_div
, 0, n_div
);
4684 bmap
= isl_basic_map_drop_constraints_involving_dims(bmap
,
4685 isl_dim_out
, 0, n_out
);
4687 return isl_basic_map_domain(bmap
);
4691 #define TYPE isl_map
4694 #include "isl_tab_lexopt_templ.c"
4696 struct isl_sol_for
{
4698 int (*fn
)(__isl_take isl_basic_set
*dom
,
4699 __isl_take isl_aff_list
*list
, void *user
);
4703 static void sol_for_free(struct isl_sol_for
*sol_for
)
4707 if (sol_for
->sol
.context
)
4708 sol_for
->sol
.context
->op
->free(sol_for
->sol
.context
);
4712 static void sol_for_free_wrap(struct isl_sol
*sol
)
4714 sol_for_free((struct isl_sol_for
*)sol
);
4717 /* Add the solution identified by the tableau and the context tableau.
4719 * See documentation of sol_add for more details.
4721 * Instead of constructing a basic map, this function calls a user
4722 * defined function with the current context as a basic set and
4723 * a list of affine expressions representing the relation between
4724 * the input and output. The space over which the affine expressions
4725 * are defined is the same as that of the domain. The number of
4726 * affine expressions in the list is equal to the number of output variables.
4728 static void sol_for_add(struct isl_sol_for
*sol
,
4729 struct isl_basic_set
*dom
, struct isl_mat
*M
)
4733 isl_local_space
*ls
;
4737 if (sol
->sol
.error
|| !dom
|| !M
)
4740 ctx
= isl_basic_set_get_ctx(dom
);
4741 ls
= isl_basic_set_get_local_space(dom
);
4742 list
= isl_aff_list_alloc(ctx
, M
->n_row
- 1);
4743 for (i
= 1; i
< M
->n_row
; ++i
) {
4744 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
4746 isl_int_set(aff
->v
->el
[0], M
->row
[0][0]);
4747 isl_seq_cpy(aff
->v
->el
+ 1, M
->row
[i
], M
->n_col
);
4749 aff
= isl_aff_normalize(aff
);
4750 list
= isl_aff_list_add(list
, aff
);
4752 isl_local_space_free(ls
);
4754 dom
= isl_basic_set_finalize(dom
);
4756 if (sol
->fn(isl_basic_set_copy(dom
), list
, sol
->user
) < 0)
4759 isl_basic_set_free(dom
);
4763 isl_basic_set_free(dom
);
4768 static void sol_for_add_wrap(struct isl_sol
*sol
,
4769 struct isl_basic_set
*dom
, struct isl_mat
*M
)
4771 sol_for_add((struct isl_sol_for
*)sol
, dom
, M
);
4774 static struct isl_sol_for
*sol_for_init(struct isl_basic_map
*bmap
, int max
,
4775 int (*fn
)(__isl_take isl_basic_set
*dom
, __isl_take isl_aff_list
*list
,
4779 struct isl_sol_for
*sol_for
= NULL
;
4781 struct isl_basic_set
*dom
= NULL
;
4783 sol_for
= isl_calloc_type(bmap
->ctx
, struct isl_sol_for
);
4787 dom_dim
= isl_space_domain(isl_space_copy(bmap
->dim
));
4788 dom
= isl_basic_set_universe(dom_dim
);
4790 sol_for
->sol
.rational
= ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
4791 sol_for
->sol
.dec_level
.callback
.run
= &sol_dec_level_wrap
;
4792 sol_for
->sol
.dec_level
.sol
= &sol_for
->sol
;
4794 sol_for
->user
= user
;
4795 sol_for
->sol
.max
= max
;
4796 sol_for
->sol
.n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4797 sol_for
->sol
.add
= &sol_for_add_wrap
;
4798 sol_for
->sol
.add_empty
= NULL
;
4799 sol_for
->sol
.free
= &sol_for_free_wrap
;
4801 sol_for
->sol
.context
= isl_context_alloc(dom
);
4802 if (!sol_for
->sol
.context
)
4805 isl_basic_set_free(dom
);
4808 isl_basic_set_free(dom
);
4809 sol_for_free(sol_for
);
4813 static void sol_for_find_solutions(struct isl_sol_for
*sol_for
,
4814 struct isl_tab
*tab
)
4816 find_solutions_main(&sol_for
->sol
, tab
);
4819 int isl_basic_map_foreach_lexopt(__isl_keep isl_basic_map
*bmap
, int max
,
4820 int (*fn
)(__isl_take isl_basic_set
*dom
, __isl_take isl_aff_list
*list
,
4824 struct isl_sol_for
*sol_for
= NULL
;
4826 bmap
= isl_basic_map_copy(bmap
);
4827 bmap
= isl_basic_map_detect_equalities(bmap
);
4831 sol_for
= sol_for_init(bmap
, max
, fn
, user
);
4835 if (isl_basic_map_plain_is_empty(bmap
))
4838 struct isl_tab
*tab
;
4839 struct isl_context
*context
= sol_for
->sol
.context
;
4840 tab
= tab_for_lexmin(bmap
,
4841 context
->op
->peek_basic_set(context
), 1, max
);
4842 tab
= context
->op
->detect_nonnegative_parameters(context
, tab
);
4843 sol_for_find_solutions(sol_for
, tab
);
4844 if (sol_for
->sol
.error
)
4848 sol_free(&sol_for
->sol
);
4849 isl_basic_map_free(bmap
);
4852 sol_free(&sol_for
->sol
);
4853 isl_basic_map_free(bmap
);
4857 int isl_basic_set_foreach_lexopt(__isl_keep isl_basic_set
*bset
, int max
,
4858 int (*fn
)(__isl_take isl_basic_set
*dom
, __isl_take isl_aff_list
*list
,
4862 return isl_basic_map_foreach_lexopt(bset
, max
, fn
, user
);
4865 /* Check if the given sequence of len variables starting at pos
4866 * represents a trivial (i.e., zero) solution.
4867 * The variables are assumed to be non-negative and to come in pairs,
4868 * with each pair representing a variable of unrestricted sign.
4869 * The solution is trivial if each such pair in the sequence consists
4870 * of two identical values, meaning that the variable being represented
4873 static int region_is_trivial(struct isl_tab
*tab
, int pos
, int len
)
4880 for (i
= 0; i
< len
; i
+= 2) {
4884 neg_row
= tab
->var
[pos
+ i
].is_row
?
4885 tab
->var
[pos
+ i
].index
: -1;
4886 pos_row
= tab
->var
[pos
+ i
+ 1].is_row
?
4887 tab
->var
[pos
+ i
+ 1].index
: -1;
4890 isl_int_is_zero(tab
->mat
->row
[neg_row
][1])) &&
4892 isl_int_is_zero(tab
->mat
->row
[pos_row
][1])))
4895 if (neg_row
< 0 || pos_row
< 0)
4897 if (isl_int_ne(tab
->mat
->row
[neg_row
][1],
4898 tab
->mat
->row
[pos_row
][1]))
4905 /* Return the index of the first trivial region or -1 if all regions
4908 static int first_trivial_region(struct isl_tab
*tab
,
4909 int n_region
, struct isl_region
*region
)
4913 for (i
= 0; i
< n_region
; ++i
) {
4914 if (region_is_trivial(tab
, region
[i
].pos
, region
[i
].len
))
4921 /* Check if the solution is optimal, i.e., whether the first
4922 * n_op entries are zero.
4924 static int is_optimal(__isl_keep isl_vec
*sol
, int n_op
)
4928 for (i
= 0; i
< n_op
; ++i
)
4929 if (!isl_int_is_zero(sol
->el
[1 + i
]))
4934 /* Add constraints to "tab" that ensure that any solution is significantly
4935 * better than that represented by "sol". That is, find the first
4936 * relevant (within first n_op) non-zero coefficient and force it (along
4937 * with all previous coefficients) to be zero.
4938 * If the solution is already optimal (all relevant coefficients are zero),
4939 * then just mark the table as empty.
4941 * This function assumes that at least 2 * n_op more rows and at least
4942 * 2 * n_op more elements in the constraint array are available in the tableau.
4944 static int force_better_solution(struct isl_tab
*tab
,
4945 __isl_keep isl_vec
*sol
, int n_op
)
4954 for (i
= 0; i
< n_op
; ++i
)
4955 if (!isl_int_is_zero(sol
->el
[1 + i
]))
4959 if (isl_tab_mark_empty(tab
) < 0)
4964 ctx
= isl_vec_get_ctx(sol
);
4965 v
= isl_vec_alloc(ctx
, 1 + tab
->n_var
);
4969 for (; i
>= 0; --i
) {
4971 isl_int_set_si(v
->el
[1 + i
], -1);
4972 if (add_lexmin_eq(tab
, v
->el
) < 0)
4983 struct isl_trivial
{
4987 struct isl_tab_undo
*snap
;
4990 /* Return the lexicographically smallest non-trivial solution of the
4991 * given ILP problem.
4993 * All variables are assumed to be non-negative.
4995 * n_op is the number of initial coordinates to optimize.
4996 * That is, once a solution has been found, we will only continue looking
4997 * for solution that result in significantly better values for those
4998 * initial coordinates. That is, we only continue looking for solutions
4999 * that increase the number of initial zeros in this sequence.
5001 * A solution is non-trivial, if it is non-trivial on each of the
5002 * specified regions. Each region represents a sequence of pairs
5003 * of variables. A solution is non-trivial on such a region if
5004 * at least one of these pairs consists of different values, i.e.,
5005 * such that the non-negative variable represented by the pair is non-zero.
5007 * Whenever a conflict is encountered, all constraints involved are
5008 * reported to the caller through a call to "conflict".
5010 * We perform a simple branch-and-bound backtracking search.
5011 * Each level in the search represents initially trivial region that is forced
5012 * to be non-trivial.
5013 * At each level we consider n cases, where n is the length of the region.
5014 * In terms of the n/2 variables of unrestricted signs being encoded by
5015 * the region, we consider the cases
5018 * x_0 = 0 and x_1 >= 1
5019 * x_0 = 0 and x_1 <= -1
5020 * x_0 = 0 and x_1 = 0 and x_2 >= 1
5021 * x_0 = 0 and x_1 = 0 and x_2 <= -1
5023 * The cases are considered in this order, assuming that each pair
5024 * x_i_a x_i_b represents the value x_i_b - x_i_a.
5025 * That is, x_0 >= 1 is enforced by adding the constraint
5026 * x_0_b - x_0_a >= 1
5028 __isl_give isl_vec
*isl_tab_basic_set_non_trivial_lexmin(
5029 __isl_take isl_basic_set
*bset
, int n_op
, int n_region
,
5030 struct isl_region
*region
,
5031 int (*conflict
)(int con
, void *user
), void *user
)
5037 isl_vec
*sol
= NULL
;
5038 struct isl_tab
*tab
;
5039 struct isl_trivial
*triv
= NULL
;
5045 ctx
= isl_basic_set_get_ctx(bset
);
5046 sol
= isl_vec_alloc(ctx
, 0);
5048 tab
= tab_for_lexmin(bset
, NULL
, 0, 0);
5051 tab
->conflict
= conflict
;
5052 tab
->conflict_user
= user
;
5054 v
= isl_vec_alloc(ctx
, 1 + tab
->n_var
);
5055 triv
= isl_calloc_array(ctx
, struct isl_trivial
, n_region
);
5056 if (!v
|| (n_region
&& !triv
))
5062 while (level
>= 0) {
5066 tab
= cut_to_integer_lexmin(tab
, CUT_ONE
);
5071 r
= first_trivial_region(tab
, n_region
, region
);
5073 for (i
= 0; i
< level
; ++i
)
5076 sol
= isl_tab_get_sample_value(tab
);
5079 if (is_optimal(sol
, n_op
))
5083 if (level
>= n_region
)
5084 isl_die(ctx
, isl_error_internal
,
5085 "nesting level too deep", goto error
);
5086 if (isl_tab_extend_cons(tab
,
5087 2 * region
[r
].len
+ 2 * n_op
) < 0)
5089 triv
[level
].region
= r
;
5090 triv
[level
].side
= 0;
5093 r
= triv
[level
].region
;
5094 side
= triv
[level
].side
;
5095 base
= 2 * (side
/2);
5097 if (side
>= region
[r
].len
) {
5102 if (isl_tab_rollback(tab
, triv
[level
].snap
) < 0)
5107 if (triv
[level
].update
) {
5108 if (force_better_solution(tab
, sol
, n_op
) < 0)
5110 triv
[level
].update
= 0;
5113 if (side
== base
&& base
>= 2) {
5114 for (j
= base
- 2; j
< base
; ++j
) {
5116 isl_int_set_si(v
->el
[1 + region
[r
].pos
+ j
], 1);
5117 if (add_lexmin_eq(tab
, v
->el
) < 0)
5122 triv
[level
].snap
= isl_tab_snap(tab
);
5123 if (isl_tab_push_basis(tab
) < 0)
5127 isl_int_set_si(v
->el
[0], -1);
5128 isl_int_set_si(v
->el
[1 + region
[r
].pos
+ side
], -1);
5129 isl_int_set_si(v
->el
[1 + region
[r
].pos
+ (side
^ 1)], 1);
5130 tab
= add_lexmin_ineq(tab
, v
->el
);
5140 isl_basic_set_free(bset
);
5147 isl_basic_set_free(bset
);
5152 /* Wrapper for a tableau that is used for computing
5153 * the lexicographically smallest rational point of a non-negative set.
5154 * This point is represented by the sample value of "tab",
5155 * unless "tab" is empty.
5157 struct isl_tab_lexmin
{
5159 struct isl_tab
*tab
;
5162 /* Free "tl" and return NULL.
5164 __isl_null isl_tab_lexmin
*isl_tab_lexmin_free(__isl_take isl_tab_lexmin
*tl
)
5168 isl_ctx_deref(tl
->ctx
);
5169 isl_tab_free(tl
->tab
);
5175 /* Construct an isl_tab_lexmin for computing
5176 * the lexicographically smallest rational point in "bset",
5177 * assuming that all variables are non-negative.
5179 __isl_give isl_tab_lexmin
*isl_tab_lexmin_from_basic_set(
5180 __isl_take isl_basic_set
*bset
)
5188 ctx
= isl_basic_set_get_ctx(bset
);
5189 tl
= isl_calloc_type(ctx
, struct isl_tab_lexmin
);
5194 tl
->tab
= tab_for_lexmin(bset
, NULL
, 0, 0);
5195 isl_basic_set_free(bset
);
5197 return isl_tab_lexmin_free(tl
);
5200 isl_basic_set_free(bset
);
5201 isl_tab_lexmin_free(tl
);
5205 /* Return the dimension of the set represented by "tl".
5207 int isl_tab_lexmin_dim(__isl_keep isl_tab_lexmin
*tl
)
5209 return tl
? tl
->tab
->n_var
: -1;
5212 /* Add the equality with coefficients "eq" to "tl", updating the optimal
5213 * solution if needed.
5214 * The equality is added as two opposite inequality constraints.
5216 __isl_give isl_tab_lexmin
*isl_tab_lexmin_add_eq(__isl_take isl_tab_lexmin
*tl
,
5222 return isl_tab_lexmin_free(tl
);
5224 if (isl_tab_extend_cons(tl
->tab
, 2) < 0)
5225 return isl_tab_lexmin_free(tl
);
5226 n_var
= tl
->tab
->n_var
;
5227 isl_seq_neg(eq
, eq
, 1 + n_var
);
5228 tl
->tab
= add_lexmin_ineq(tl
->tab
, eq
);
5229 isl_seq_neg(eq
, eq
, 1 + n_var
);
5230 tl
->tab
= add_lexmin_ineq(tl
->tab
, eq
);
5233 return isl_tab_lexmin_free(tl
);
5238 /* Return the lexicographically smallest rational point in the basic set
5239 * from which "tl" was constructed.
5240 * If the original input was empty, then return a zero-length vector.
5242 __isl_give isl_vec
*isl_tab_lexmin_get_solution(__isl_keep isl_tab_lexmin
*tl
)
5247 return isl_vec_alloc(tl
->ctx
, 0);
5249 return isl_tab_get_sample_value(tl
->tab
);
5252 /* Return the lexicographically smallest rational point in "bset",
5253 * assuming that all variables are non-negative.
5254 * If "bset" is empty, then return a zero-length vector.
5256 __isl_give isl_vec
*isl_tab_basic_set_non_neg_lexmin(
5257 __isl_take isl_basic_set
*bset
)
5262 tl
= isl_tab_lexmin_from_basic_set(bset
);
5263 sol
= isl_tab_lexmin_get_solution(tl
);
5264 isl_tab_lexmin_free(tl
);
5268 struct isl_sol_pma
{
5270 isl_pw_multi_aff
*pma
;
5274 static void sol_pma_free(struct isl_sol_pma
*sol_pma
)
5278 if (sol_pma
->sol
.context
)
5279 sol_pma
->sol
.context
->op
->free(sol_pma
->sol
.context
);
5280 isl_pw_multi_aff_free(sol_pma
->pma
);
5281 isl_set_free(sol_pma
->empty
);
5285 /* This function is called for parts of the context where there is
5286 * no solution, with "bset" corresponding to the context tableau.
5287 * Simply add the basic set to the set "empty".
5289 static void sol_pma_add_empty(struct isl_sol_pma
*sol
,
5290 __isl_take isl_basic_set
*bset
)
5292 if (!bset
|| !sol
->empty
)
5295 sol
->empty
= isl_set_grow(sol
->empty
, 1);
5296 bset
= isl_basic_set_simplify(bset
);
5297 bset
= isl_basic_set_finalize(bset
);
5298 sol
->empty
= isl_set_add_basic_set(sol
->empty
, bset
);
5303 isl_basic_set_free(bset
);
5307 /* Return the equality constraint in "bset" that defines existentially
5308 * quantified variable "pos" in terms of earlier dimensions.
5309 * The equality constraint is guaranteed to exist by the caller.
5310 * If "c" is not NULL, then it is the result of a previous call
5311 * to this function for the same variable, so simply return the input "c"
5314 static __isl_give isl_constraint
*get_equality(__isl_keep isl_basic_set
*bset
,
5315 int pos
, __isl_take isl_constraint
*c
)
5321 r
= isl_basic_set_has_defining_equality(bset
, isl_dim_div
, pos
, &c
);
5325 isl_die(isl_basic_set_get_ctx(bset
), isl_error_internal
,
5326 "unexpected missing equality", return NULL
);
5330 /* Given a set "dom", of which only the first "n_known" existentially
5331 * quantified variables have a known explicit representation, and
5332 * a matrix "M", the rows of which are defined in terms of the dimensions
5333 * of "dom", eliminate all references to the existentially quantified
5334 * variables without a known explicit representation from "M"
5335 * by exploiting the equality constraints of "dom".
5337 * In particular, for each of those existentially quantified variables,
5338 * if there are non-zero entries in the corresponding column of "M",
5339 * then look for an equality constraint of "dom" that defines that variable
5340 * in terms of earlier variables and use it to clear the entries.
5342 * In particular, if the equality is of the form
5346 * while the matrix entry is b/d (with d the global denominator of "M"),
5347 * then first scale the matrix such that the entry becomes b'/d' with
5348 * b' a multiple of a. Do this by multiplying the entire matrix
5349 * by abs(a/gcd(a,b)). Then subtract the equality multiplied by b'/a
5350 * from the row of "M" to clear the entry.
5352 static __isl_give isl_mat
*eliminate_unknown_divs(__isl_take isl_mat
*M
,
5353 __isl_keep isl_basic_set
*dom
, int n_known
)
5355 int i
, j
, n_div
, off
;
5357 isl_constraint
*c
= NULL
;
5362 n_div
= isl_basic_set_dim(dom
, isl_dim_div
);
5363 off
= M
->n_col
- n_div
;
5366 for (i
= n_div
- 1; i
>= n_known
; --i
) {
5367 for (j
= 1; j
< M
->n_row
; ++j
) {
5368 if (isl_int_is_zero(M
->row
[j
][off
+ i
]))
5370 c
= get_equality(dom
, i
, c
);
5373 isl_int_gcd(t
, M
->row
[j
][off
+ i
], c
->v
->el
[off
+ i
]);
5374 isl_int_divexact(t
, c
->v
->el
[off
+ i
], t
);
5376 M
= isl_mat_scale(M
, t
);
5381 M
->row
[j
][off
+ i
], c
->v
->el
[off
+ i
]);
5382 isl_seq_submul(M
->row
[j
], t
, c
->v
->el
, M
->n_col
);
5384 c
= isl_constraint_free(c
);
5391 isl_constraint_free(c
);
5396 /* Return the index of the last known div of "bset" after "start" and
5397 * up to (but not including) "end".
5398 * Return "start" if there is no such known div.
5400 static int last_known_div_after(__isl_keep isl_basic_set
*bset
,
5403 for (end
= end
- 1; end
> start
; --end
) {
5404 if (isl_basic_set_div_is_known(bset
, end
))
5411 /* Set the affine expressions in "ma" according to the rows in "M", which
5412 * are defined over the local space "ls".
5413 * The matrix "M" may have extra (zero) columns beyond the number
5414 * of variables in "ls".
5416 static __isl_give isl_multi_aff
*set_from_affine_matrix(
5417 __isl_take isl_multi_aff
*ma
, __isl_take isl_local_space
*ls
,
5418 __isl_take isl_mat
*M
)
5423 if (!ma
|| !ls
|| !M
)
5426 dim
= isl_local_space_dim(ls
, isl_dim_all
);
5427 for (i
= 1; i
< M
->n_row
; ++i
) {
5428 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
5430 isl_int_set(aff
->v
->el
[0], M
->row
[0][0]);
5431 isl_seq_cpy(aff
->v
->el
+ 1, M
->row
[i
], 1 + dim
);
5433 aff
= isl_aff_normalize(aff
);
5434 ma
= isl_multi_aff_set_aff(ma
, i
- 1, aff
);
5436 isl_local_space_free(ls
);
5441 isl_local_space_free(ls
);
5443 isl_multi_aff_free(ma
);
5447 /* Given a basic map "dom" that represents the context and an affine
5448 * matrix "M" that maps the dimensions of the context to the
5449 * output variables, construct an isl_pw_multi_aff with a single
5450 * cell corresponding to "dom" and affine expressions copied from "M".
5452 * Note that the description of the initial context may have involved
5453 * existentially quantified variables, in which case they also appear
5454 * in "dom". These need to be removed before creating the affine
5455 * expression because an affine expression cannot be defined in terms
5456 * of existentially quantified variables without a known representation.
5457 * In particular, they are first moved to the end in both "dom" and "M" and
5458 * then ignored in "M". In principle, the final columns of "M"
5459 * (i.e., those that will be ignored) should be zero at this stage
5460 * because align_context_divs adds the existentially quantified
5461 * variables of the context to the main tableau without any constraints.
5462 * The computed minimal value can therefore not depend on these variables.
5463 * However, additional integer divisions that get added for parametric cuts
5464 * get added to the end and they may happen to be equal to some affine
5465 * expression involving the original existentially quantified variables.
5466 * These equality constraints are then propagated to the main tableau
5467 * such that the computed minimum can in fact depend on those existentially
5468 * quantified variables. This dependence can however be removed again
5469 * by exploiting the equality constraints in "dom".
5470 * eliminate_unknown_divs takes care of this.
5472 static void sol_pma_add(struct isl_sol_pma
*sol
,
5473 __isl_take isl_basic_set
*dom
, __isl_take isl_mat
*M
)
5475 isl_local_space
*ls
;
5476 isl_multi_aff
*maff
;
5477 isl_pw_multi_aff
*pma
;
5478 int n_div
, n_known
, end
, off
;
5480 n_div
= isl_basic_set_dim(dom
, isl_dim_div
);
5481 off
= M
->n_col
- n_div
;
5483 for (n_known
= 0; n_known
< end
; ++n_known
) {
5484 if (isl_basic_set_div_is_known(dom
, n_known
))
5486 end
= last_known_div_after(dom
, n_known
, end
);
5489 isl_basic_set_swap_div(dom
, n_known
, end
);
5490 M
= isl_mat_swap_cols(M
, off
+ n_known
, off
+ end
);
5492 dom
= isl_basic_set_gauss(dom
, NULL
);
5493 if (n_known
< n_div
)
5494 M
= eliminate_unknown_divs(M
, dom
, n_known
);
5496 maff
= isl_multi_aff_alloc(isl_pw_multi_aff_get_space(sol
->pma
));
5497 ls
= isl_basic_set_get_local_space(dom
);
5498 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
,
5499 n_known
, n_div
- n_known
);
5500 maff
= set_from_affine_matrix(maff
, ls
, M
);
5501 dom
= isl_basic_set_simplify(dom
);
5502 dom
= isl_basic_set_finalize(dom
);
5503 pma
= isl_pw_multi_aff_alloc(isl_set_from_basic_set(dom
), maff
);
5504 sol
->pma
= isl_pw_multi_aff_add_disjoint(sol
->pma
, pma
);
5509 static void sol_pma_free_wrap(struct isl_sol
*sol
)
5511 sol_pma_free((struct isl_sol_pma
*)sol
);
5514 static void sol_pma_add_empty_wrap(struct isl_sol
*sol
,
5515 __isl_take isl_basic_set
*bset
)
5517 sol_pma_add_empty((struct isl_sol_pma
*)sol
, bset
);
5520 static void sol_pma_add_wrap(struct isl_sol
*sol
,
5521 __isl_take isl_basic_set
*dom
, __isl_take isl_mat
*M
)
5523 sol_pma_add((struct isl_sol_pma
*)sol
, dom
, M
);
5526 /* Construct an isl_sol_pma structure for accumulating the solution.
5527 * If track_empty is set, then we also keep track of the parts
5528 * of the context where there is no solution.
5529 * If max is set, then we are solving a maximization, rather than
5530 * a minimization problem, which means that the variables in the
5531 * tableau have value "M - x" rather than "M + x".
5533 static struct isl_sol
*sol_pma_init(__isl_keep isl_basic_map
*bmap
,
5534 __isl_take isl_basic_set
*dom
, int track_empty
, int max
)
5536 struct isl_sol_pma
*sol_pma
= NULL
;
5541 sol_pma
= isl_calloc_type(bmap
->ctx
, struct isl_sol_pma
);
5545 sol_pma
->sol
.rational
= ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
5546 sol_pma
->sol
.dec_level
.callback
.run
= &sol_dec_level_wrap
;
5547 sol_pma
->sol
.dec_level
.sol
= &sol_pma
->sol
;
5548 sol_pma
->sol
.max
= max
;
5549 sol_pma
->sol
.n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5550 sol_pma
->sol
.add
= &sol_pma_add_wrap
;
5551 sol_pma
->sol
.add_empty
= track_empty
? &sol_pma_add_empty_wrap
: NULL
;
5552 sol_pma
->sol
.free
= &sol_pma_free_wrap
;
5553 sol_pma
->pma
= isl_pw_multi_aff_empty(isl_basic_map_get_space(bmap
));
5557 sol_pma
->sol
.context
= isl_context_alloc(dom
);
5558 if (!sol_pma
->sol
.context
)
5562 sol_pma
->empty
= isl_set_alloc_space(isl_basic_set_get_space(dom
),
5563 1, ISL_SET_DISJOINT
);
5564 if (!sol_pma
->empty
)
5568 isl_basic_set_free(dom
);
5569 return &sol_pma
->sol
;
5571 isl_basic_set_free(dom
);
5572 sol_pma_free(sol_pma
);
5576 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
5577 * some obvious symmetries.
5579 * We call basic_map_partial_lexopt_base_sol and extract the results.
5581 static __isl_give isl_pw_multi_aff
*basic_map_partial_lexopt_base_pw_multi_aff(
5582 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5583 __isl_give isl_set
**empty
, int max
)
5585 isl_pw_multi_aff
*result
= NULL
;
5586 struct isl_sol
*sol
;
5587 struct isl_sol_pma
*sol_pma
;
5589 sol
= basic_map_partial_lexopt_base_sol(bmap
, dom
, empty
, max
,
5593 sol_pma
= (struct isl_sol_pma
*) sol
;
5595 result
= isl_pw_multi_aff_copy(sol_pma
->pma
);
5597 *empty
= isl_set_copy(sol_pma
->empty
);
5598 sol_free(&sol_pma
->sol
);
5602 /* Given that the last input variable of "maff" represents the minimum
5603 * of some bounds, check whether we need to plug in the expression
5606 * In particular, check if the last input variable appears in any
5607 * of the expressions in "maff".
5609 static int need_substitution(__isl_keep isl_multi_aff
*maff
)
5614 pos
= isl_multi_aff_dim(maff
, isl_dim_in
) - 1;
5616 for (i
= 0; i
< maff
->n
; ++i
)
5617 if (isl_aff_involves_dims(maff
->p
[i
], isl_dim_in
, pos
, 1))
5623 /* Given a set of upper bounds on the last "input" variable m,
5624 * construct a piecewise affine expression that selects
5625 * the minimal upper bound to m, i.e.,
5626 * divide the space into cells where one
5627 * of the upper bounds is smaller than all the others and select
5628 * this upper bound on that cell.
5630 * In particular, if there are n bounds b_i, then the result
5631 * consists of n cell, each one of the form
5633 * b_i <= b_j for j > i
5634 * b_i < b_j for j < i
5636 * The affine expression on this cell is
5640 static __isl_give isl_pw_aff
*set_minimum_pa(__isl_take isl_space
*space
,
5641 __isl_take isl_mat
*var
)
5644 isl_aff
*aff
= NULL
;
5645 isl_basic_set
*bset
= NULL
;
5646 isl_pw_aff
*paff
= NULL
;
5647 isl_space
*pw_space
;
5648 isl_local_space
*ls
= NULL
;
5653 ls
= isl_local_space_from_space(isl_space_copy(space
));
5654 pw_space
= isl_space_copy(space
);
5655 pw_space
= isl_space_from_domain(pw_space
);
5656 pw_space
= isl_space_add_dims(pw_space
, isl_dim_out
, 1);
5657 paff
= isl_pw_aff_alloc_size(pw_space
, var
->n_row
);
5659 for (i
= 0; i
< var
->n_row
; ++i
) {
5662 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
5663 bset
= isl_basic_set_alloc_space(isl_space_copy(space
), 0,
5667 isl_int_set_si(aff
->v
->el
[0], 1);
5668 isl_seq_cpy(aff
->v
->el
+ 1, var
->row
[i
], var
->n_col
);
5669 isl_int_set_si(aff
->v
->el
[1 + var
->n_col
], 0);
5670 bset
= select_minimum(bset
, var
, i
);
5671 paff_i
= isl_pw_aff_alloc(isl_set_from_basic_set(bset
), aff
);
5672 paff
= isl_pw_aff_add_disjoint(paff
, paff_i
);
5675 isl_local_space_free(ls
);
5676 isl_space_free(space
);
5681 isl_basic_set_free(bset
);
5682 isl_pw_aff_free(paff
);
5683 isl_local_space_free(ls
);
5684 isl_space_free(space
);
5689 /* Given a piecewise multi-affine expression of which the last input variable
5690 * is the minimum of the bounds in "cst", plug in the value of the minimum.
5691 * This minimum expression is given in "min_expr_pa".
5692 * The set "min_expr" contains the same information, but in the form of a set.
5693 * The variable is subsequently projected out.
5695 * The implementation is similar to those of "split" and "split_domain".
5696 * If the variable appears in a given expression, then minimum expression
5697 * is plugged in. Otherwise, if the variable appears in the constraints
5698 * and a split is required, then the domain is split. Otherwise, no split
5701 static __isl_give isl_pw_multi_aff
*split_domain_pma(
5702 __isl_take isl_pw_multi_aff
*opt
, __isl_take isl_pw_aff
*min_expr_pa
,
5703 __isl_take isl_set
*min_expr
, __isl_take isl_mat
*cst
)
5708 isl_pw_multi_aff
*res
;
5710 if (!opt
|| !min_expr
|| !cst
)
5713 n_in
= isl_pw_multi_aff_dim(opt
, isl_dim_in
);
5714 space
= isl_pw_multi_aff_get_space(opt
);
5715 space
= isl_space_drop_dims(space
, isl_dim_in
, n_in
- 1, 1);
5716 res
= isl_pw_multi_aff_empty(space
);
5718 for (i
= 0; i
< opt
->n
; ++i
) {
5719 isl_pw_multi_aff
*pma
;
5721 pma
= isl_pw_multi_aff_alloc(isl_set_copy(opt
->p
[i
].set
),
5722 isl_multi_aff_copy(opt
->p
[i
].maff
));
5723 if (need_substitution(opt
->p
[i
].maff
))
5724 pma
= isl_pw_multi_aff_substitute(pma
,
5725 isl_dim_in
, n_in
- 1, min_expr_pa
);
5726 else if (need_split_set(opt
->p
[i
].set
, cst
))
5727 pma
= isl_pw_multi_aff_intersect_domain(pma
,
5728 isl_set_copy(min_expr
));
5729 pma
= isl_pw_multi_aff_project_out(pma
,
5730 isl_dim_in
, n_in
- 1, 1);
5732 res
= isl_pw_multi_aff_add_disjoint(res
, pma
);
5735 isl_pw_multi_aff_free(opt
);
5736 isl_pw_aff_free(min_expr_pa
);
5737 isl_set_free(min_expr
);
5741 isl_pw_multi_aff_free(opt
);
5742 isl_pw_aff_free(min_expr_pa
);
5743 isl_set_free(min_expr
);
5748 static __isl_give isl_pw_multi_aff
*basic_map_partial_lexopt_pw_multi_aff(
5749 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5750 __isl_give isl_set
**empty
, int max
);
5752 /* This function is called from basic_map_partial_lexopt_symm.
5753 * The last variable of "bmap" and "dom" corresponds to the minimum
5754 * of the bounds in "cst". "map_space" is the space of the original
5755 * input relation (of basic_map_partial_lexopt_symm) and "set_space"
5756 * is the space of the original domain.
5758 * We recursively call basic_map_partial_lexopt and then plug in
5759 * the definition of the minimum in the result.
5761 static __isl_give isl_pw_multi_aff
*
5762 basic_map_partial_lexopt_symm_core_pw_multi_aff(
5763 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5764 __isl_give isl_set
**empty
, int max
, __isl_take isl_mat
*cst
,
5765 __isl_take isl_space
*map_space
, __isl_take isl_space
*set_space
)
5767 isl_pw_multi_aff
*opt
;
5768 isl_pw_aff
*min_expr_pa
;
5771 min_expr
= set_minimum(isl_basic_set_get_space(dom
), isl_mat_copy(cst
));
5772 min_expr_pa
= set_minimum_pa(isl_basic_set_get_space(dom
),
5775 opt
= basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, empty
, max
);
5778 *empty
= split(*empty
,
5779 isl_set_copy(min_expr
), isl_mat_copy(cst
));
5780 *empty
= isl_set_reset_space(*empty
, set_space
);
5783 opt
= split_domain_pma(opt
, min_expr_pa
, min_expr
, cst
);
5784 opt
= isl_pw_multi_aff_reset_space(opt
, map_space
);
5790 #define TYPE isl_pw_multi_aff
5792 #define SUFFIX _pw_multi_aff
5793 #include "isl_tab_lexopt_templ.c"