2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
5 * Use of this software is governed by the MIT license
7 * Written by Sven Verdoolaege, K.U.Leuven, Departement
8 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
10 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
13 #include <isl_ctx_private.h>
14 #include "isl_map_private.h"
17 #include "isl_sample.h"
18 #include <isl_mat_private.h>
19 #include <isl_aff_private.h>
20 #include <isl_options_private.h>
21 #include <isl_config.h>
24 * The implementation of parametric integer linear programming in this file
25 * was inspired by the paper "Parametric Integer Programming" and the
26 * report "Solving systems of affine (in)equalities" by Paul Feautrier
29 * The strategy used for obtaining a feasible solution is different
30 * from the one used in isl_tab.c. In particular, in isl_tab.c,
31 * upon finding a constraint that is not yet satisfied, we pivot
32 * in a row that increases the constant term of the row holding the
33 * constraint, making sure the sample solution remains feasible
34 * for all the constraints it already satisfied.
35 * Here, we always pivot in the row holding the constraint,
36 * choosing a column that induces the lexicographically smallest
37 * increment to the sample solution.
39 * By starting out from a sample value that is lexicographically
40 * smaller than any integer point in the problem space, the first
41 * feasible integer sample point we find will also be the lexicographically
42 * smallest. If all variables can be assumed to be non-negative,
43 * then the initial sample value may be chosen equal to zero.
44 * However, we will not make this assumption. Instead, we apply
45 * the "big parameter" trick. Any variable x is then not directly
46 * used in the tableau, but instead it is represented by another
47 * variable x' = M + x, where M is an arbitrarily large (positive)
48 * value. x' is therefore always non-negative, whatever the value of x.
49 * Taking as initial sample value x' = 0 corresponds to x = -M,
50 * which is always smaller than any possible value of x.
52 * The big parameter trick is used in the main tableau and
53 * also in the context tableau if isl_context_lex is used.
54 * In this case, each tableaus has its own big parameter.
55 * Before doing any real work, we check if all the parameters
56 * happen to be non-negative. If so, we drop the column corresponding
57 * to M from the initial context tableau.
58 * If isl_context_gbr is used, then the big parameter trick is only
59 * used in the main tableau.
63 struct isl_context_op
{
64 /* detect nonnegative parameters in context and mark them in tab */
65 struct isl_tab
*(*detect_nonnegative_parameters
)(
66 struct isl_context
*context
, struct isl_tab
*tab
);
67 /* return temporary reference to basic set representation of context */
68 struct isl_basic_set
*(*peek_basic_set
)(struct isl_context
*context
);
69 /* return temporary reference to tableau representation of context */
70 struct isl_tab
*(*peek_tab
)(struct isl_context
*context
);
71 /* add equality; check is 1 if eq may not be valid;
72 * update is 1 if we may want to call ineq_sign on context later.
74 void (*add_eq
)(struct isl_context
*context
, isl_int
*eq
,
75 int check
, int update
);
76 /* add inequality; check is 1 if ineq may not be valid;
77 * update is 1 if we may want to call ineq_sign on context later.
79 void (*add_ineq
)(struct isl_context
*context
, isl_int
*ineq
,
80 int check
, int update
);
81 /* check sign of ineq based on previous information.
82 * strict is 1 if saturation should be treated as a positive sign.
84 enum isl_tab_row_sign (*ineq_sign
)(struct isl_context
*context
,
85 isl_int
*ineq
, int strict
);
86 /* check if inequality maintains feasibility */
87 int (*test_ineq
)(struct isl_context
*context
, isl_int
*ineq
);
88 /* return index of a div that corresponds to "div" */
89 int (*get_div
)(struct isl_context
*context
, struct isl_tab
*tab
,
91 /* add div "div" to context and return non-negativity */
92 int (*add_div
)(struct isl_context
*context
, struct isl_vec
*div
);
93 int (*detect_equalities
)(struct isl_context
*context
,
95 /* return row index of "best" split */
96 int (*best_split
)(struct isl_context
*context
, struct isl_tab
*tab
);
97 /* check if context has already been determined to be empty */
98 int (*is_empty
)(struct isl_context
*context
);
99 /* check if context is still usable */
100 int (*is_ok
)(struct isl_context
*context
);
101 /* save a copy/snapshot of context */
102 void *(*save
)(struct isl_context
*context
);
103 /* restore saved context */
104 void (*restore
)(struct isl_context
*context
, void *);
105 /* discard saved context */
106 void (*discard
)(void *);
107 /* invalidate context */
108 void (*invalidate
)(struct isl_context
*context
);
110 void (*free
)(struct isl_context
*context
);
114 struct isl_context_op
*op
;
117 struct isl_context_lex
{
118 struct isl_context context
;
122 /* A stack (linked list) of solutions of subtrees of the search space.
124 * "M" describes the solution in terms of the dimensions of "dom".
125 * The number of columns of "M" is one more than the total number
126 * of dimensions of "dom".
128 struct isl_partial_sol
{
130 struct isl_basic_set
*dom
;
133 struct isl_partial_sol
*next
;
137 struct isl_sol_callback
{
138 struct isl_tab_callback callback
;
142 /* isl_sol is an interface for constructing a solution to
143 * a parametric integer linear programming problem.
144 * Every time the algorithm reaches a state where a solution
145 * can be read off from the tableau (including cases where the tableau
146 * is empty), the function "add" is called on the isl_sol passed
147 * to find_solutions_main.
149 * The context tableau is owned by isl_sol and is updated incrementally.
151 * There are currently two implementations of this interface,
152 * isl_sol_map, which simply collects the solutions in an isl_map
153 * and (optionally) the parts of the context where there is no solution
155 * isl_sol_for, which calls a user-defined function for each part of
164 struct isl_context
*context
;
165 struct isl_partial_sol
*partial
;
166 void (*add
)(struct isl_sol
*sol
,
167 struct isl_basic_set
*dom
, struct isl_mat
*M
);
168 void (*add_empty
)(struct isl_sol
*sol
, struct isl_basic_set
*bset
);
169 void (*free
)(struct isl_sol
*sol
);
170 struct isl_sol_callback dec_level
;
173 static void sol_free(struct isl_sol
*sol
)
175 struct isl_partial_sol
*partial
, *next
;
178 for (partial
= sol
->partial
; partial
; partial
= next
) {
179 next
= partial
->next
;
180 isl_basic_set_free(partial
->dom
);
181 isl_mat_free(partial
->M
);
187 /* Push a partial solution represented by a domain and mapping M
188 * onto the stack of partial solutions.
190 static void sol_push_sol(struct isl_sol
*sol
,
191 struct isl_basic_set
*dom
, struct isl_mat
*M
)
193 struct isl_partial_sol
*partial
;
195 if (sol
->error
|| !dom
)
198 partial
= isl_alloc_type(dom
->ctx
, struct isl_partial_sol
);
202 partial
->level
= sol
->level
;
205 partial
->next
= sol
->partial
;
207 sol
->partial
= partial
;
211 isl_basic_set_free(dom
);
216 /* Pop one partial solution from the partial solution stack and
217 * pass it on to sol->add or sol->add_empty.
219 static void sol_pop_one(struct isl_sol
*sol
)
221 struct isl_partial_sol
*partial
;
223 partial
= sol
->partial
;
224 sol
->partial
= partial
->next
;
227 sol
->add(sol
, partial
->dom
, partial
->M
);
229 sol
->add_empty(sol
, partial
->dom
);
233 /* Return a fresh copy of the domain represented by the context tableau.
235 static struct isl_basic_set
*sol_domain(struct isl_sol
*sol
)
237 struct isl_basic_set
*bset
;
242 bset
= isl_basic_set_dup(sol
->context
->op
->peek_basic_set(sol
->context
));
243 bset
= isl_basic_set_update_from_tab(bset
,
244 sol
->context
->op
->peek_tab(sol
->context
));
249 /* Check whether two partial solutions have the same mapping, where n_div
250 * is the number of divs that the two partial solutions have in common.
252 static int same_solution(struct isl_partial_sol
*s1
, struct isl_partial_sol
*s2
,
258 if (!s1
->M
!= !s2
->M
)
263 dim
= isl_basic_set_total_dim(s1
->dom
) - s1
->dom
->n_div
;
265 for (i
= 0; i
< s1
->M
->n_row
; ++i
) {
266 if (isl_seq_first_non_zero(s1
->M
->row
[i
]+1+dim
+n_div
,
267 s1
->M
->n_col
-1-dim
-n_div
) != -1)
269 if (isl_seq_first_non_zero(s2
->M
->row
[i
]+1+dim
+n_div
,
270 s2
->M
->n_col
-1-dim
-n_div
) != -1)
272 if (!isl_seq_eq(s1
->M
->row
[i
], s2
->M
->row
[i
], 1+dim
+n_div
))
278 /* Pop all solutions from the partial solution stack that were pushed onto
279 * the stack at levels that are deeper than the current level.
280 * If the two topmost elements on the stack have the same level
281 * and represent the same solution, then their domains are combined.
282 * This combined domain is the same as the current context domain
283 * as sol_pop is called each time we move back to a higher level.
285 static void sol_pop(struct isl_sol
*sol
)
287 struct isl_partial_sol
*partial
;
293 if (sol
->level
== 0) {
294 for (partial
= sol
->partial
; partial
; partial
= sol
->partial
)
299 partial
= sol
->partial
;
303 if (partial
->level
<= sol
->level
)
306 if (partial
->next
&& partial
->next
->level
== partial
->level
) {
307 n_div
= isl_basic_set_dim(
308 sol
->context
->op
->peek_basic_set(sol
->context
),
311 if (!same_solution(partial
, partial
->next
, n_div
)) {
315 struct isl_basic_set
*bset
;
319 n
= isl_basic_set_dim(partial
->next
->dom
, isl_dim_div
);
321 bset
= sol_domain(sol
);
322 isl_basic_set_free(partial
->next
->dom
);
323 partial
->next
->dom
= bset
;
324 M
= partial
->next
->M
;
325 M
= isl_mat_drop_cols(M
, M
->n_col
- n
, n
);
326 partial
->next
->M
= M
;
327 partial
->next
->level
= sol
->level
;
332 sol
->partial
= partial
->next
;
333 isl_basic_set_free(partial
->dom
);
334 isl_mat_free(partial
->M
);
341 error
: sol
->error
= 1;
344 static void sol_dec_level(struct isl_sol
*sol
)
354 static int sol_dec_level_wrap(struct isl_tab_callback
*cb
)
356 struct isl_sol_callback
*callback
= (struct isl_sol_callback
*)cb
;
358 sol_dec_level(callback
->sol
);
360 return callback
->sol
->error
? -1 : 0;
363 /* Move down to next level and push callback onto context tableau
364 * to decrease the level again when it gets rolled back across
365 * the current state. That is, dec_level will be called with
366 * the context tableau in the same state as it is when inc_level
369 static void sol_inc_level(struct isl_sol
*sol
)
377 tab
= sol
->context
->op
->peek_tab(sol
->context
);
378 if (isl_tab_push_callback(tab
, &sol
->dec_level
.callback
) < 0)
382 static void scale_rows(struct isl_mat
*mat
, isl_int m
, int n_row
)
386 if (isl_int_is_one(m
))
389 for (i
= 0; i
< n_row
; ++i
)
390 isl_seq_scale(mat
->row
[i
], mat
->row
[i
], m
, mat
->n_col
);
393 /* Add the solution identified by the tableau and the context tableau.
395 * The layout of the variables is as follows.
396 * tab->n_var is equal to the total number of variables in the input
397 * map (including divs that were copied from the context)
398 * + the number of extra divs constructed
399 * Of these, the first tab->n_param and the last tab->n_div variables
400 * correspond to the variables in the context, i.e.,
401 * tab->n_param + tab->n_div = context_tab->n_var
402 * tab->n_param is equal to the number of parameters and input
403 * dimensions in the input map
404 * tab->n_div is equal to the number of divs in the context
406 * If there is no solution, then call add_empty with a basic set
407 * that corresponds to the context tableau. (If add_empty is NULL,
410 * If there is a solution, then first construct a matrix that maps
411 * all dimensions of the context to the output variables, i.e.,
412 * the output dimensions in the input map.
413 * The divs in the input map (if any) that do not correspond to any
414 * div in the context do not appear in the solution.
415 * The algorithm will make sure that they have an integer value,
416 * but these values themselves are of no interest.
417 * We have to be careful not to drop or rearrange any divs in the
418 * context because that would change the meaning of the matrix.
420 * To extract the value of the output variables, it should be noted
421 * that we always use a big parameter M in the main tableau and so
422 * the variable stored in this tableau is not an output variable x itself, but
423 * x' = M + x (in case of minimization)
425 * x' = M - x (in case of maximization)
426 * If x' appears in a column, then its optimal value is zero,
427 * which means that the optimal value of x is an unbounded number
428 * (-M for minimization and M for maximization).
429 * We currently assume that the output dimensions in the original map
430 * are bounded, so this cannot occur.
431 * Similarly, when x' appears in a row, then the coefficient of M in that
432 * row is necessarily 1.
433 * If the row in the tableau represents
434 * d x' = c + d M + e(y)
435 * then, in case of minimization, the corresponding row in the matrix
438 * with a d = m, the (updated) common denominator of the matrix.
439 * In case of maximization, the row will be
442 static void sol_add(struct isl_sol
*sol
, struct isl_tab
*tab
)
444 struct isl_basic_set
*bset
= NULL
;
445 struct isl_mat
*mat
= NULL
;
450 if (sol
->error
|| !tab
)
453 if (tab
->empty
&& !sol
->add_empty
)
455 if (sol
->context
->op
->is_empty(sol
->context
))
458 bset
= sol_domain(sol
);
461 sol_push_sol(sol
, bset
, NULL
);
467 mat
= isl_mat_alloc(tab
->mat
->ctx
, 1 + sol
->n_out
,
468 1 + tab
->n_param
+ tab
->n_div
);
474 isl_seq_clr(mat
->row
[0] + 1, mat
->n_col
- 1);
475 isl_int_set_si(mat
->row
[0][0], 1);
476 for (row
= 0; row
< sol
->n_out
; ++row
) {
477 int i
= tab
->n_param
+ row
;
480 isl_seq_clr(mat
->row
[1 + row
], mat
->n_col
);
481 if (!tab
->var
[i
].is_row
) {
483 isl_die(mat
->ctx
, isl_error_invalid
,
484 "unbounded optimum", goto error2
);
488 r
= tab
->var
[i
].index
;
490 isl_int_ne(tab
->mat
->row
[r
][2], tab
->mat
->row
[r
][0]))
491 isl_die(mat
->ctx
, isl_error_invalid
,
492 "unbounded optimum", goto error2
);
493 isl_int_gcd(m
, mat
->row
[0][0], tab
->mat
->row
[r
][0]);
494 isl_int_divexact(m
, tab
->mat
->row
[r
][0], m
);
495 scale_rows(mat
, m
, 1 + row
);
496 isl_int_divexact(m
, mat
->row
[0][0], tab
->mat
->row
[r
][0]);
497 isl_int_mul(mat
->row
[1 + row
][0], m
, tab
->mat
->row
[r
][1]);
498 for (j
= 0; j
< tab
->n_param
; ++j
) {
500 if (tab
->var
[j
].is_row
)
502 col
= tab
->var
[j
].index
;
503 isl_int_mul(mat
->row
[1 + row
][1 + j
], m
,
504 tab
->mat
->row
[r
][off
+ col
]);
506 for (j
= 0; j
< tab
->n_div
; ++j
) {
508 if (tab
->var
[tab
->n_var
- tab
->n_div
+j
].is_row
)
510 col
= tab
->var
[tab
->n_var
- tab
->n_div
+j
].index
;
511 isl_int_mul(mat
->row
[1 + row
][1 + tab
->n_param
+ j
], m
,
512 tab
->mat
->row
[r
][off
+ col
]);
515 isl_seq_neg(mat
->row
[1 + row
], mat
->row
[1 + row
],
521 sol_push_sol(sol
, bset
, mat
);
526 isl_basic_set_free(bset
);
534 struct isl_set
*empty
;
537 static void sol_map_free(struct isl_sol_map
*sol_map
)
541 if (sol_map
->sol
.context
)
542 sol_map
->sol
.context
->op
->free(sol_map
->sol
.context
);
543 isl_map_free(sol_map
->map
);
544 isl_set_free(sol_map
->empty
);
548 static void sol_map_free_wrap(struct isl_sol
*sol
)
550 sol_map_free((struct isl_sol_map
*)sol
);
553 /* This function is called for parts of the context where there is
554 * no solution, with "bset" corresponding to the context tableau.
555 * Simply add the basic set to the set "empty".
557 static void sol_map_add_empty(struct isl_sol_map
*sol
,
558 struct isl_basic_set
*bset
)
562 isl_assert(bset
->ctx
, sol
->empty
, goto error
);
564 sol
->empty
= isl_set_grow(sol
->empty
, 1);
565 bset
= isl_basic_set_simplify(bset
);
566 bset
= isl_basic_set_finalize(bset
);
567 sol
->empty
= isl_set_add_basic_set(sol
->empty
, isl_basic_set_copy(bset
));
570 isl_basic_set_free(bset
);
573 isl_basic_set_free(bset
);
577 static void sol_map_add_empty_wrap(struct isl_sol
*sol
,
578 struct isl_basic_set
*bset
)
580 sol_map_add_empty((struct isl_sol_map
*)sol
, bset
);
583 /* Given a basic map "dom" that represents the context and an affine
584 * matrix "M" that maps the dimensions of the context to the
585 * output variables, construct a basic map with the same parameters
586 * and divs as the context, the dimensions of the context as input
587 * dimensions and a number of output dimensions that is equal to
588 * the number of output dimensions in the input map.
590 * The constraints and divs of the context are simply copied
591 * from "dom". For each row
595 * is added, with d the common denominator of M.
597 static void sol_map_add(struct isl_sol_map
*sol
,
598 struct isl_basic_set
*dom
, struct isl_mat
*M
)
601 struct isl_basic_map
*bmap
= NULL
;
609 if (sol
->sol
.error
|| !dom
|| !M
)
612 n_out
= sol
->sol
.n_out
;
613 n_eq
= dom
->n_eq
+ n_out
;
614 n_ineq
= dom
->n_ineq
;
616 nparam
= isl_basic_set_total_dim(dom
) - n_div
;
617 total
= isl_map_dim(sol
->map
, isl_dim_all
);
618 bmap
= isl_basic_map_alloc_space(isl_map_get_space(sol
->map
),
619 n_div
, n_eq
, 2 * n_div
+ n_ineq
);
622 if (sol
->sol
.rational
)
623 ISL_F_SET(bmap
, ISL_BASIC_MAP_RATIONAL
);
624 for (i
= 0; i
< dom
->n_div
; ++i
) {
625 int k
= isl_basic_map_alloc_div(bmap
);
628 isl_seq_cpy(bmap
->div
[k
], dom
->div
[i
], 1 + 1 + nparam
);
629 isl_seq_clr(bmap
->div
[k
] + 1 + 1 + nparam
, total
- nparam
);
630 isl_seq_cpy(bmap
->div
[k
] + 1 + 1 + total
,
631 dom
->div
[i
] + 1 + 1 + nparam
, i
);
633 for (i
= 0; i
< dom
->n_eq
; ++i
) {
634 int k
= isl_basic_map_alloc_equality(bmap
);
637 isl_seq_cpy(bmap
->eq
[k
], dom
->eq
[i
], 1 + nparam
);
638 isl_seq_clr(bmap
->eq
[k
] + 1 + nparam
, total
- nparam
);
639 isl_seq_cpy(bmap
->eq
[k
] + 1 + total
,
640 dom
->eq
[i
] + 1 + nparam
, n_div
);
642 for (i
= 0; i
< dom
->n_ineq
; ++i
) {
643 int k
= isl_basic_map_alloc_inequality(bmap
);
646 isl_seq_cpy(bmap
->ineq
[k
], dom
->ineq
[i
], 1 + nparam
);
647 isl_seq_clr(bmap
->ineq
[k
] + 1 + nparam
, total
- nparam
);
648 isl_seq_cpy(bmap
->ineq
[k
] + 1 + total
,
649 dom
->ineq
[i
] + 1 + nparam
, n_div
);
651 for (i
= 0; i
< M
->n_row
- 1; ++i
) {
652 int k
= isl_basic_map_alloc_equality(bmap
);
655 isl_seq_cpy(bmap
->eq
[k
], M
->row
[1 + i
], 1 + nparam
);
656 isl_seq_clr(bmap
->eq
[k
] + 1 + nparam
, n_out
);
657 isl_int_neg(bmap
->eq
[k
][1 + nparam
+ i
], M
->row
[0][0]);
658 isl_seq_cpy(bmap
->eq
[k
] + 1 + nparam
+ n_out
,
659 M
->row
[1 + i
] + 1 + nparam
, n_div
);
661 bmap
= isl_basic_map_simplify(bmap
);
662 bmap
= isl_basic_map_finalize(bmap
);
663 sol
->map
= isl_map_grow(sol
->map
, 1);
664 sol
->map
= isl_map_add_basic_map(sol
->map
, bmap
);
665 isl_basic_set_free(dom
);
671 isl_basic_set_free(dom
);
673 isl_basic_map_free(bmap
);
677 static void sol_map_add_wrap(struct isl_sol
*sol
,
678 struct isl_basic_set
*dom
, struct isl_mat
*M
)
680 sol_map_add((struct isl_sol_map
*)sol
, dom
, M
);
684 /* Store the "parametric constant" of row "row" of tableau "tab" in "line",
685 * i.e., the constant term and the coefficients of all variables that
686 * appear in the context tableau.
687 * Note that the coefficient of the big parameter M is NOT copied.
688 * The context tableau may not have a big parameter and even when it
689 * does, it is a different big parameter.
691 static void get_row_parameter_line(struct isl_tab
*tab
, int row
, isl_int
*line
)
694 unsigned off
= 2 + tab
->M
;
696 isl_int_set(line
[0], tab
->mat
->row
[row
][1]);
697 for (i
= 0; i
< tab
->n_param
; ++i
) {
698 if (tab
->var
[i
].is_row
)
699 isl_int_set_si(line
[1 + i
], 0);
701 int col
= tab
->var
[i
].index
;
702 isl_int_set(line
[1 + i
], tab
->mat
->row
[row
][off
+ col
]);
705 for (i
= 0; i
< tab
->n_div
; ++i
) {
706 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
707 isl_int_set_si(line
[1 + tab
->n_param
+ i
], 0);
709 int col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
710 isl_int_set(line
[1 + tab
->n_param
+ i
],
711 tab
->mat
->row
[row
][off
+ col
]);
716 /* Check if rows "row1" and "row2" have identical "parametric constants",
717 * as explained above.
718 * In this case, we also insist that the coefficients of the big parameter
719 * be the same as the values of the constants will only be the same
720 * if these coefficients are also the same.
722 static int identical_parameter_line(struct isl_tab
*tab
, int row1
, int row2
)
725 unsigned off
= 2 + tab
->M
;
727 if (isl_int_ne(tab
->mat
->row
[row1
][1], tab
->mat
->row
[row2
][1]))
730 if (tab
->M
&& isl_int_ne(tab
->mat
->row
[row1
][2],
731 tab
->mat
->row
[row2
][2]))
734 for (i
= 0; i
< tab
->n_param
+ tab
->n_div
; ++i
) {
735 int pos
= i
< tab
->n_param
? i
:
736 tab
->n_var
- tab
->n_div
+ i
- tab
->n_param
;
739 if (tab
->var
[pos
].is_row
)
741 col
= tab
->var
[pos
].index
;
742 if (isl_int_ne(tab
->mat
->row
[row1
][off
+ col
],
743 tab
->mat
->row
[row2
][off
+ col
]))
749 /* Return an inequality that expresses that the "parametric constant"
750 * should be non-negative.
751 * This function is only called when the coefficient of the big parameter
754 static struct isl_vec
*get_row_parameter_ineq(struct isl_tab
*tab
, int row
)
756 struct isl_vec
*ineq
;
758 ineq
= isl_vec_alloc(tab
->mat
->ctx
, 1 + tab
->n_param
+ tab
->n_div
);
762 get_row_parameter_line(tab
, row
, ineq
->el
);
764 ineq
= isl_vec_normalize(ineq
);
769 /* Normalize a div expression of the form
771 * [(g*f(x) + c)/(g * m)]
773 * with c the constant term and f(x) the remaining coefficients, to
777 static void normalize_div(__isl_keep isl_vec
*div
)
779 isl_ctx
*ctx
= isl_vec_get_ctx(div
);
780 int len
= div
->size
- 2;
782 isl_seq_gcd(div
->el
+ 2, len
, &ctx
->normalize_gcd
);
783 isl_int_gcd(ctx
->normalize_gcd
, ctx
->normalize_gcd
, div
->el
[0]);
785 if (isl_int_is_one(ctx
->normalize_gcd
))
788 isl_int_divexact(div
->el
[0], div
->el
[0], ctx
->normalize_gcd
);
789 isl_int_fdiv_q(div
->el
[1], div
->el
[1], ctx
->normalize_gcd
);
790 isl_seq_scale_down(div
->el
+ 2, div
->el
+ 2, ctx
->normalize_gcd
, len
);
793 /* Return a integer division for use in a parametric cut based on the given row.
794 * In particular, let the parametric constant of the row be
798 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
799 * The div returned is equal to
801 * floor(\sum_i {-a_i} y_i) = floor((\sum_i (-a_i mod d) y_i)/d)
803 static struct isl_vec
*get_row_parameter_div(struct isl_tab
*tab
, int row
)
807 div
= isl_vec_alloc(tab
->mat
->ctx
, 1 + 1 + tab
->n_param
+ tab
->n_div
);
811 isl_int_set(div
->el
[0], tab
->mat
->row
[row
][0]);
812 get_row_parameter_line(tab
, row
, div
->el
+ 1);
813 isl_seq_neg(div
->el
+ 1, div
->el
+ 1, div
->size
- 1);
815 isl_seq_fdiv_r(div
->el
+ 1, div
->el
+ 1, div
->el
[0], div
->size
- 1);
820 /* Return a integer division for use in transferring an integrality constraint
822 * In particular, let the parametric constant of the row be
826 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
827 * The the returned div is equal to
829 * floor(\sum_i {a_i} y_i) = floor((\sum_i (a_i mod d) y_i)/d)
831 static struct isl_vec
*get_row_split_div(struct isl_tab
*tab
, int row
)
835 div
= isl_vec_alloc(tab
->mat
->ctx
, 1 + 1 + tab
->n_param
+ tab
->n_div
);
839 isl_int_set(div
->el
[0], tab
->mat
->row
[row
][0]);
840 get_row_parameter_line(tab
, row
, div
->el
+ 1);
842 isl_seq_fdiv_r(div
->el
+ 1, div
->el
+ 1, div
->el
[0], div
->size
- 1);
847 /* Construct and return an inequality that expresses an upper bound
849 * In particular, if the div is given by
853 * then the inequality expresses
857 static struct isl_vec
*ineq_for_div(struct isl_basic_set
*bset
, unsigned div
)
861 struct isl_vec
*ineq
;
866 total
= isl_basic_set_total_dim(bset
);
867 div_pos
= 1 + total
- bset
->n_div
+ div
;
869 ineq
= isl_vec_alloc(bset
->ctx
, 1 + total
);
873 isl_seq_cpy(ineq
->el
, bset
->div
[div
] + 1, 1 + total
);
874 isl_int_neg(ineq
->el
[div_pos
], bset
->div
[div
][0]);
878 /* Given a row in the tableau and a div that was created
879 * using get_row_split_div and that has been constrained to equality, i.e.,
881 * d = floor(\sum_i {a_i} y_i) = \sum_i {a_i} y_i
883 * replace the expression "\sum_i {a_i} y_i" in the row by d,
884 * i.e., we subtract "\sum_i {a_i} y_i" and add 1 d.
885 * The coefficients of the non-parameters in the tableau have been
886 * verified to be integral. We can therefore simply replace coefficient b
887 * by floor(b). For the coefficients of the parameters we have
888 * floor(a_i) = a_i - {a_i}, while for the other coefficients, we have
891 static struct isl_tab
*set_row_cst_to_div(struct isl_tab
*tab
, int row
, int div
)
893 isl_seq_fdiv_q(tab
->mat
->row
[row
] + 1, tab
->mat
->row
[row
] + 1,
894 tab
->mat
->row
[row
][0], 1 + tab
->M
+ tab
->n_col
);
896 isl_int_set_si(tab
->mat
->row
[row
][0], 1);
898 if (tab
->var
[tab
->n_var
- tab
->n_div
+ div
].is_row
) {
899 int drow
= tab
->var
[tab
->n_var
- tab
->n_div
+ div
].index
;
901 isl_assert(tab
->mat
->ctx
,
902 isl_int_is_one(tab
->mat
->row
[drow
][0]), goto error
);
903 isl_seq_combine(tab
->mat
->row
[row
] + 1,
904 tab
->mat
->ctx
->one
, tab
->mat
->row
[row
] + 1,
905 tab
->mat
->ctx
->one
, tab
->mat
->row
[drow
] + 1,
906 1 + tab
->M
+ tab
->n_col
);
908 int dcol
= tab
->var
[tab
->n_var
- tab
->n_div
+ div
].index
;
910 isl_int_add_ui(tab
->mat
->row
[row
][2 + tab
->M
+ dcol
],
911 tab
->mat
->row
[row
][2 + tab
->M
+ dcol
], 1);
920 /* Check if the (parametric) constant of the given row is obviously
921 * negative, meaning that we don't need to consult the context tableau.
922 * If there is a big parameter and its coefficient is non-zero,
923 * then this coefficient determines the outcome.
924 * Otherwise, we check whether the constant is negative and
925 * all non-zero coefficients of parameters are negative and
926 * belong to non-negative parameters.
928 static int is_obviously_neg(struct isl_tab
*tab
, int row
)
932 unsigned off
= 2 + tab
->M
;
935 if (isl_int_is_pos(tab
->mat
->row
[row
][2]))
937 if (isl_int_is_neg(tab
->mat
->row
[row
][2]))
941 if (isl_int_is_nonneg(tab
->mat
->row
[row
][1]))
943 for (i
= 0; i
< tab
->n_param
; ++i
) {
944 /* Eliminated parameter */
945 if (tab
->var
[i
].is_row
)
947 col
= tab
->var
[i
].index
;
948 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
950 if (!tab
->var
[i
].is_nonneg
)
952 if (isl_int_is_pos(tab
->mat
->row
[row
][off
+ col
]))
955 for (i
= 0; i
< tab
->n_div
; ++i
) {
956 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
958 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
959 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
961 if (!tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_nonneg
)
963 if (isl_int_is_pos(tab
->mat
->row
[row
][off
+ col
]))
969 /* Check if the (parametric) constant of the given row is obviously
970 * non-negative, meaning that we don't need to consult the context tableau.
971 * If there is a big parameter and its coefficient is non-zero,
972 * then this coefficient determines the outcome.
973 * Otherwise, we check whether the constant is non-negative and
974 * all non-zero coefficients of parameters are positive and
975 * belong to non-negative parameters.
977 static int is_obviously_nonneg(struct isl_tab
*tab
, int row
)
981 unsigned off
= 2 + tab
->M
;
984 if (isl_int_is_pos(tab
->mat
->row
[row
][2]))
986 if (isl_int_is_neg(tab
->mat
->row
[row
][2]))
990 if (isl_int_is_neg(tab
->mat
->row
[row
][1]))
992 for (i
= 0; i
< tab
->n_param
; ++i
) {
993 /* Eliminated parameter */
994 if (tab
->var
[i
].is_row
)
996 col
= tab
->var
[i
].index
;
997 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
999 if (!tab
->var
[i
].is_nonneg
)
1001 if (isl_int_is_neg(tab
->mat
->row
[row
][off
+ col
]))
1004 for (i
= 0; i
< tab
->n_div
; ++i
) {
1005 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
1007 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
1008 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
1010 if (!tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_nonneg
)
1012 if (isl_int_is_neg(tab
->mat
->row
[row
][off
+ col
]))
1018 /* Given a row r and two columns, return the column that would
1019 * lead to the lexicographically smallest increment in the sample
1020 * solution when leaving the basis in favor of the row.
1021 * Pivoting with column c will increment the sample value by a non-negative
1022 * constant times a_{V,c}/a_{r,c}, with a_{V,c} the elements of column c
1023 * corresponding to the non-parametric variables.
1024 * If variable v appears in a column c_v, the a_{v,c} = 1 iff c = c_v,
1025 * with all other entries in this virtual row equal to zero.
1026 * If variable v appears in a row, then a_{v,c} is the element in column c
1029 * Let v be the first variable with a_{v,c1}/a_{r,c1} != a_{v,c2}/a_{r,c2}.
1030 * Then if a_{v,c1}/a_{r,c1} < a_{v,c2}/a_{r,c2}, i.e.,
1031 * a_{v,c2} a_{r,c1} - a_{v,c1} a_{r,c2} > 0, c1 results in the minimal
1032 * increment. Otherwise, it's c2.
1034 static int lexmin_col_pair(struct isl_tab
*tab
,
1035 int row
, int col1
, int col2
, isl_int tmp
)
1040 tr
= tab
->mat
->row
[row
] + 2 + tab
->M
;
1042 for (i
= tab
->n_param
; i
< tab
->n_var
- tab
->n_div
; ++i
) {
1046 if (!tab
->var
[i
].is_row
) {
1047 if (tab
->var
[i
].index
== col1
)
1049 if (tab
->var
[i
].index
== col2
)
1054 if (tab
->var
[i
].index
== row
)
1057 r
= tab
->mat
->row
[tab
->var
[i
].index
] + 2 + tab
->M
;
1058 s1
= isl_int_sgn(r
[col1
]);
1059 s2
= isl_int_sgn(r
[col2
]);
1060 if (s1
== 0 && s2
== 0)
1067 isl_int_mul(tmp
, r
[col2
], tr
[col1
]);
1068 isl_int_submul(tmp
, r
[col1
], tr
[col2
]);
1069 if (isl_int_is_pos(tmp
))
1071 if (isl_int_is_neg(tmp
))
1077 /* Given a row in the tableau, find and return the column that would
1078 * result in the lexicographically smallest, but positive, increment
1079 * in the sample point.
1080 * If there is no such column, then return tab->n_col.
1081 * If anything goes wrong, return -1.
1083 static int lexmin_pivot_col(struct isl_tab
*tab
, int row
)
1086 int col
= tab
->n_col
;
1090 tr
= tab
->mat
->row
[row
] + 2 + tab
->M
;
1094 for (j
= tab
->n_dead
; j
< tab
->n_col
; ++j
) {
1095 if (tab
->col_var
[j
] >= 0 &&
1096 (tab
->col_var
[j
] < tab
->n_param
||
1097 tab
->col_var
[j
] >= tab
->n_var
- tab
->n_div
))
1100 if (!isl_int_is_pos(tr
[j
]))
1103 if (col
== tab
->n_col
)
1106 col
= lexmin_col_pair(tab
, row
, col
, j
, tmp
);
1107 isl_assert(tab
->mat
->ctx
, col
>= 0, goto error
);
1117 /* Return the first known violated constraint, i.e., a non-negative
1118 * constraint that currently has an either obviously negative value
1119 * or a previously determined to be negative value.
1121 * If any constraint has a negative coefficient for the big parameter,
1122 * if any, then we return one of these first.
1124 static int first_neg(struct isl_tab
*tab
)
1129 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
1130 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
1132 if (!isl_int_is_neg(tab
->mat
->row
[row
][2]))
1135 tab
->row_sign
[row
] = isl_tab_row_neg
;
1138 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
1139 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
1141 if (tab
->row_sign
) {
1142 if (tab
->row_sign
[row
] == 0 &&
1143 is_obviously_neg(tab
, row
))
1144 tab
->row_sign
[row
] = isl_tab_row_neg
;
1145 if (tab
->row_sign
[row
] != isl_tab_row_neg
)
1147 } else if (!is_obviously_neg(tab
, row
))
1154 /* Check whether the invariant that all columns are lexico-positive
1155 * is satisfied. This function is not called from the current code
1156 * but is useful during debugging.
1158 static void check_lexpos(struct isl_tab
*tab
) __attribute__ ((unused
));
1159 static void check_lexpos(struct isl_tab
*tab
)
1161 unsigned off
= 2 + tab
->M
;
1166 for (col
= tab
->n_dead
; col
< tab
->n_col
; ++col
) {
1167 if (tab
->col_var
[col
] >= 0 &&
1168 (tab
->col_var
[col
] < tab
->n_param
||
1169 tab
->col_var
[col
] >= tab
->n_var
- tab
->n_div
))
1171 for (var
= tab
->n_param
; var
< tab
->n_var
- tab
->n_div
; ++var
) {
1172 if (!tab
->var
[var
].is_row
) {
1173 if (tab
->var
[var
].index
== col
)
1178 row
= tab
->var
[var
].index
;
1179 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
1181 if (isl_int_is_pos(tab
->mat
->row
[row
][off
+ col
]))
1183 fprintf(stderr
, "lexneg column %d (row %d)\n",
1186 if (var
>= tab
->n_var
- tab
->n_div
)
1187 fprintf(stderr
, "zero column %d\n", col
);
1191 /* Report to the caller that the given constraint is part of an encountered
1194 static int report_conflicting_constraint(struct isl_tab
*tab
, int con
)
1196 return tab
->conflict(con
, tab
->conflict_user
);
1199 /* Given a conflicting row in the tableau, report all constraints
1200 * involved in the row to the caller. That is, the row itself
1201 * (if it represents a constraint) and all constraint columns with
1202 * non-zero (and therefore negative) coefficients.
1204 static int report_conflict(struct isl_tab
*tab
, int row
)
1212 if (tab
->row_var
[row
] < 0 &&
1213 report_conflicting_constraint(tab
, ~tab
->row_var
[row
]) < 0)
1216 tr
= tab
->mat
->row
[row
] + 2 + tab
->M
;
1218 for (j
= tab
->n_dead
; j
< tab
->n_col
; ++j
) {
1219 if (tab
->col_var
[j
] >= 0 &&
1220 (tab
->col_var
[j
] < tab
->n_param
||
1221 tab
->col_var
[j
] >= tab
->n_var
- tab
->n_div
))
1224 if (!isl_int_is_neg(tr
[j
]))
1227 if (tab
->col_var
[j
] < 0 &&
1228 report_conflicting_constraint(tab
, ~tab
->col_var
[j
]) < 0)
1235 /* Resolve all known or obviously violated constraints through pivoting.
1236 * In particular, as long as we can find any violated constraint, we
1237 * look for a pivoting column that would result in the lexicographically
1238 * smallest increment in the sample point. If there is no such column
1239 * then the tableau is infeasible.
1241 static int restore_lexmin(struct isl_tab
*tab
) WARN_UNUSED
;
1242 static int restore_lexmin(struct isl_tab
*tab
)
1250 while ((row
= first_neg(tab
)) != -1) {
1251 col
= lexmin_pivot_col(tab
, row
);
1252 if (col
>= tab
->n_col
) {
1253 if (report_conflict(tab
, row
) < 0)
1255 if (isl_tab_mark_empty(tab
) < 0)
1261 if (isl_tab_pivot(tab
, row
, col
) < 0)
1267 /* Given a row that represents an equality, look for an appropriate
1269 * In particular, if there are any non-zero coefficients among
1270 * the non-parameter variables, then we take the last of these
1271 * variables. Eliminating this variable in terms of the other
1272 * variables and/or parameters does not influence the property
1273 * that all column in the initial tableau are lexicographically
1274 * positive. The row corresponding to the eliminated variable
1275 * will only have non-zero entries below the diagonal of the
1276 * initial tableau. That is, we transform
1282 * If there is no such non-parameter variable, then we are dealing with
1283 * pure parameter equality and we pick any parameter with coefficient 1 or -1
1284 * for elimination. This will ensure that the eliminated parameter
1285 * always has an integer value whenever all the other parameters are integral.
1286 * If there is no such parameter then we return -1.
1288 static int last_var_col_or_int_par_col(struct isl_tab
*tab
, int row
)
1290 unsigned off
= 2 + tab
->M
;
1293 for (i
= tab
->n_var
- tab
->n_div
- 1; i
>= 0 && i
>= tab
->n_param
; --i
) {
1295 if (tab
->var
[i
].is_row
)
1297 col
= tab
->var
[i
].index
;
1298 if (col
<= tab
->n_dead
)
1300 if (!isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
1303 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
1304 if (isl_int_is_one(tab
->mat
->row
[row
][off
+ i
]))
1306 if (isl_int_is_negone(tab
->mat
->row
[row
][off
+ i
]))
1312 /* Add an equality that is known to be valid to the tableau.
1313 * We first check if we can eliminate a variable or a parameter.
1314 * If not, we add the equality as two inequalities.
1315 * In this case, the equality was a pure parameter equality and there
1316 * is no need to resolve any constraint violations.
1318 static struct isl_tab
*add_lexmin_valid_eq(struct isl_tab
*tab
, isl_int
*eq
)
1325 r
= isl_tab_add_row(tab
, eq
);
1329 r
= tab
->con
[r
].index
;
1330 i
= last_var_col_or_int_par_col(tab
, r
);
1332 tab
->con
[r
].is_nonneg
= 1;
1333 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
1335 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
1336 r
= isl_tab_add_row(tab
, eq
);
1339 tab
->con
[r
].is_nonneg
= 1;
1340 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
1343 if (isl_tab_pivot(tab
, r
, i
) < 0)
1345 if (isl_tab_kill_col(tab
, i
) < 0)
1356 /* Check if the given row is a pure constant.
1358 static int is_constant(struct isl_tab
*tab
, int row
)
1360 unsigned off
= 2 + tab
->M
;
1362 return isl_seq_first_non_zero(tab
->mat
->row
[row
] + off
+ tab
->n_dead
,
1363 tab
->n_col
- tab
->n_dead
) == -1;
1366 /* Add an equality that may or may not be valid to the tableau.
1367 * If the resulting row is a pure constant, then it must be zero.
1368 * Otherwise, the resulting tableau is empty.
1370 * If the row is not a pure constant, then we add two inequalities,
1371 * each time checking that they can be satisfied.
1372 * In the end we try to use one of the two constraints to eliminate
1375 static int add_lexmin_eq(struct isl_tab
*tab
, isl_int
*eq
) WARN_UNUSED
;
1376 static int add_lexmin_eq(struct isl_tab
*tab
, isl_int
*eq
)
1380 struct isl_tab_undo
*snap
;
1384 snap
= isl_tab_snap(tab
);
1385 r1
= isl_tab_add_row(tab
, eq
);
1388 tab
->con
[r1
].is_nonneg
= 1;
1389 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r1
]) < 0)
1392 row
= tab
->con
[r1
].index
;
1393 if (is_constant(tab
, row
)) {
1394 if (!isl_int_is_zero(tab
->mat
->row
[row
][1]) ||
1395 (tab
->M
&& !isl_int_is_zero(tab
->mat
->row
[row
][2]))) {
1396 if (isl_tab_mark_empty(tab
) < 0)
1400 if (isl_tab_rollback(tab
, snap
) < 0)
1405 if (restore_lexmin(tab
) < 0)
1410 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
1412 r2
= isl_tab_add_row(tab
, eq
);
1415 tab
->con
[r2
].is_nonneg
= 1;
1416 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r2
]) < 0)
1419 if (restore_lexmin(tab
) < 0)
1424 if (!tab
->con
[r1
].is_row
) {
1425 if (isl_tab_kill_col(tab
, tab
->con
[r1
].index
) < 0)
1427 } else if (!tab
->con
[r2
].is_row
) {
1428 if (isl_tab_kill_col(tab
, tab
->con
[r2
].index
) < 0)
1433 tab
->bmap
= isl_basic_map_add_ineq(tab
->bmap
, eq
);
1434 if (isl_tab_push(tab
, isl_tab_undo_bmap_ineq
) < 0)
1436 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
1437 tab
->bmap
= isl_basic_map_add_ineq(tab
->bmap
, eq
);
1438 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
1439 if (isl_tab_push(tab
, isl_tab_undo_bmap_ineq
) < 0)
1448 /* Add an inequality to the tableau, resolving violations using
1451 static struct isl_tab
*add_lexmin_ineq(struct isl_tab
*tab
, isl_int
*ineq
)
1458 tab
->bmap
= isl_basic_map_add_ineq(tab
->bmap
, ineq
);
1459 if (isl_tab_push(tab
, isl_tab_undo_bmap_ineq
) < 0)
1464 r
= isl_tab_add_row(tab
, ineq
);
1467 tab
->con
[r
].is_nonneg
= 1;
1468 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
1470 if (isl_tab_row_is_redundant(tab
, tab
->con
[r
].index
)) {
1471 if (isl_tab_mark_redundant(tab
, tab
->con
[r
].index
) < 0)
1476 if (restore_lexmin(tab
) < 0)
1478 if (!tab
->empty
&& tab
->con
[r
].is_row
&&
1479 isl_tab_row_is_redundant(tab
, tab
->con
[r
].index
))
1480 if (isl_tab_mark_redundant(tab
, tab
->con
[r
].index
) < 0)
1488 /* Check if the coefficients of the parameters are all integral.
1490 static int integer_parameter(struct isl_tab
*tab
, int row
)
1494 unsigned off
= 2 + tab
->M
;
1496 for (i
= 0; i
< tab
->n_param
; ++i
) {
1497 /* Eliminated parameter */
1498 if (tab
->var
[i
].is_row
)
1500 col
= tab
->var
[i
].index
;
1501 if (!isl_int_is_divisible_by(tab
->mat
->row
[row
][off
+ col
],
1502 tab
->mat
->row
[row
][0]))
1505 for (i
= 0; i
< tab
->n_div
; ++i
) {
1506 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
1508 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
1509 if (!isl_int_is_divisible_by(tab
->mat
->row
[row
][off
+ col
],
1510 tab
->mat
->row
[row
][0]))
1516 /* Check if the coefficients of the non-parameter variables are all integral.
1518 static int integer_variable(struct isl_tab
*tab
, int row
)
1521 unsigned off
= 2 + tab
->M
;
1523 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
1524 if (tab
->col_var
[i
] >= 0 &&
1525 (tab
->col_var
[i
] < tab
->n_param
||
1526 tab
->col_var
[i
] >= tab
->n_var
- tab
->n_div
))
1528 if (!isl_int_is_divisible_by(tab
->mat
->row
[row
][off
+ i
],
1529 tab
->mat
->row
[row
][0]))
1535 /* Check if the constant term is integral.
1537 static int integer_constant(struct isl_tab
*tab
, int row
)
1539 return isl_int_is_divisible_by(tab
->mat
->row
[row
][1],
1540 tab
->mat
->row
[row
][0]);
1543 #define I_CST 1 << 0
1544 #define I_PAR 1 << 1
1545 #define I_VAR 1 << 2
1547 /* Check for next (non-parameter) variable after "var" (first if var == -1)
1548 * that is non-integer and therefore requires a cut and return
1549 * the index of the variable.
1550 * For parametric tableaus, there are three parts in a row,
1551 * the constant, the coefficients of the parameters and the rest.
1552 * For each part, we check whether the coefficients in that part
1553 * are all integral and if so, set the corresponding flag in *f.
1554 * If the constant and the parameter part are integral, then the
1555 * current sample value is integral and no cut is required
1556 * (irrespective of whether the variable part is integral).
1558 static int next_non_integer_var(struct isl_tab
*tab
, int var
, int *f
)
1560 var
= var
< 0 ? tab
->n_param
: var
+ 1;
1562 for (; var
< tab
->n_var
- tab
->n_div
; ++var
) {
1565 if (!tab
->var
[var
].is_row
)
1567 row
= tab
->var
[var
].index
;
1568 if (integer_constant(tab
, row
))
1569 ISL_FL_SET(flags
, I_CST
);
1570 if (integer_parameter(tab
, row
))
1571 ISL_FL_SET(flags
, I_PAR
);
1572 if (ISL_FL_ISSET(flags
, I_CST
) && ISL_FL_ISSET(flags
, I_PAR
))
1574 if (integer_variable(tab
, row
))
1575 ISL_FL_SET(flags
, I_VAR
);
1582 /* Check for first (non-parameter) variable that is non-integer and
1583 * therefore requires a cut and return the corresponding row.
1584 * For parametric tableaus, there are three parts in a row,
1585 * the constant, the coefficients of the parameters and the rest.
1586 * For each part, we check whether the coefficients in that part
1587 * are all integral and if so, set the corresponding flag in *f.
1588 * If the constant and the parameter part are integral, then the
1589 * current sample value is integral and no cut is required
1590 * (irrespective of whether the variable part is integral).
1592 static int first_non_integer_row(struct isl_tab
*tab
, int *f
)
1594 int var
= next_non_integer_var(tab
, -1, f
);
1596 return var
< 0 ? -1 : tab
->var
[var
].index
;
1599 /* Add a (non-parametric) cut to cut away the non-integral sample
1600 * value of the given row.
1602 * If the row is given by
1604 * m r = f + \sum_i a_i y_i
1608 * c = - {-f/m} + \sum_i {a_i/m} y_i >= 0
1610 * The big parameter, if any, is ignored, since it is assumed to be big
1611 * enough to be divisible by any integer.
1612 * If the tableau is actually a parametric tableau, then this function
1613 * is only called when all coefficients of the parameters are integral.
1614 * The cut therefore has zero coefficients for the parameters.
1616 * The current value is known to be negative, so row_sign, if it
1617 * exists, is set accordingly.
1619 * Return the row of the cut or -1.
1621 static int add_cut(struct isl_tab
*tab
, int row
)
1626 unsigned off
= 2 + tab
->M
;
1628 if (isl_tab_extend_cons(tab
, 1) < 0)
1630 r
= isl_tab_allocate_con(tab
);
1634 r_row
= tab
->mat
->row
[tab
->con
[r
].index
];
1635 isl_int_set(r_row
[0], tab
->mat
->row
[row
][0]);
1636 isl_int_neg(r_row
[1], tab
->mat
->row
[row
][1]);
1637 isl_int_fdiv_r(r_row
[1], r_row
[1], tab
->mat
->row
[row
][0]);
1638 isl_int_neg(r_row
[1], r_row
[1]);
1640 isl_int_set_si(r_row
[2], 0);
1641 for (i
= 0; i
< tab
->n_col
; ++i
)
1642 isl_int_fdiv_r(r_row
[off
+ i
],
1643 tab
->mat
->row
[row
][off
+ i
], tab
->mat
->row
[row
][0]);
1645 tab
->con
[r
].is_nonneg
= 1;
1646 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
1649 tab
->row_sign
[tab
->con
[r
].index
] = isl_tab_row_neg
;
1651 return tab
->con
[r
].index
;
1657 /* Given a non-parametric tableau, add cuts until an integer
1658 * sample point is obtained or until the tableau is determined
1659 * to be integer infeasible.
1660 * As long as there is any non-integer value in the sample point,
1661 * we add appropriate cuts, if possible, for each of these
1662 * non-integer values and then resolve the violated
1663 * cut constraints using restore_lexmin.
1664 * If one of the corresponding rows is equal to an integral
1665 * combination of variables/constraints plus a non-integral constant,
1666 * then there is no way to obtain an integer point and we return
1667 * a tableau that is marked empty.
1668 * The parameter cutting_strategy controls the strategy used when adding cuts
1669 * to remove non-integer points. CUT_ALL adds all possible cuts
1670 * before continuing the search. CUT_ONE adds only one cut at a time.
1672 static struct isl_tab
*cut_to_integer_lexmin(struct isl_tab
*tab
,
1673 int cutting_strategy
)
1684 while ((var
= next_non_integer_var(tab
, -1, &flags
)) != -1) {
1686 if (ISL_FL_ISSET(flags
, I_VAR
)) {
1687 if (isl_tab_mark_empty(tab
) < 0)
1691 row
= tab
->var
[var
].index
;
1692 row
= add_cut(tab
, row
);
1695 if (cutting_strategy
== CUT_ONE
)
1697 } while ((var
= next_non_integer_var(tab
, var
, &flags
)) != -1);
1698 if (restore_lexmin(tab
) < 0)
1709 /* Check whether all the currently active samples also satisfy the inequality
1710 * "ineq" (treated as an equality if eq is set).
1711 * Remove those samples that do not.
1713 static struct isl_tab
*check_samples(struct isl_tab
*tab
, isl_int
*ineq
, int eq
)
1721 isl_assert(tab
->mat
->ctx
, tab
->bmap
, goto error
);
1722 isl_assert(tab
->mat
->ctx
, tab
->samples
, goto error
);
1723 isl_assert(tab
->mat
->ctx
, tab
->samples
->n_col
== 1 + tab
->n_var
, goto error
);
1726 for (i
= tab
->n_outside
; i
< tab
->n_sample
; ++i
) {
1728 isl_seq_inner_product(ineq
, tab
->samples
->row
[i
],
1729 1 + tab
->n_var
, &v
);
1730 sgn
= isl_int_sgn(v
);
1731 if (eq
? (sgn
== 0) : (sgn
>= 0))
1733 tab
= isl_tab_drop_sample(tab
, i
);
1745 /* Check whether the sample value of the tableau is finite,
1746 * i.e., either the tableau does not use a big parameter, or
1747 * all values of the variables are equal to the big parameter plus
1748 * some constant. This constant is the actual sample value.
1750 static int sample_is_finite(struct isl_tab
*tab
)
1757 for (i
= 0; i
< tab
->n_var
; ++i
) {
1759 if (!tab
->var
[i
].is_row
)
1761 row
= tab
->var
[i
].index
;
1762 if (isl_int_ne(tab
->mat
->row
[row
][0], tab
->mat
->row
[row
][2]))
1768 /* Check if the context tableau of sol has any integer points.
1769 * Leave tab in empty state if no integer point can be found.
1770 * If an integer point can be found and if moreover it is finite,
1771 * then it is added to the list of sample values.
1773 * This function is only called when none of the currently active sample
1774 * values satisfies the most recently added constraint.
1776 static struct isl_tab
*check_integer_feasible(struct isl_tab
*tab
)
1778 struct isl_tab_undo
*snap
;
1783 snap
= isl_tab_snap(tab
);
1784 if (isl_tab_push_basis(tab
) < 0)
1787 tab
= cut_to_integer_lexmin(tab
, CUT_ALL
);
1791 if (!tab
->empty
&& sample_is_finite(tab
)) {
1792 struct isl_vec
*sample
;
1794 sample
= isl_tab_get_sample_value(tab
);
1796 tab
= isl_tab_add_sample(tab
, sample
);
1799 if (!tab
->empty
&& isl_tab_rollback(tab
, snap
) < 0)
1808 /* Check if any of the currently active sample values satisfies
1809 * the inequality "ineq" (an equality if eq is set).
1811 static int tab_has_valid_sample(struct isl_tab
*tab
, isl_int
*ineq
, int eq
)
1819 isl_assert(tab
->mat
->ctx
, tab
->bmap
, return -1);
1820 isl_assert(tab
->mat
->ctx
, tab
->samples
, return -1);
1821 isl_assert(tab
->mat
->ctx
, tab
->samples
->n_col
== 1 + tab
->n_var
, return -1);
1824 for (i
= tab
->n_outside
; i
< tab
->n_sample
; ++i
) {
1826 isl_seq_inner_product(ineq
, tab
->samples
->row
[i
],
1827 1 + tab
->n_var
, &v
);
1828 sgn
= isl_int_sgn(v
);
1829 if (eq
? (sgn
== 0) : (sgn
>= 0))
1834 return i
< tab
->n_sample
;
1837 /* Add a div specified by "div" to the tableau "tab" and return
1838 * 1 if the div is obviously non-negative.
1840 static int context_tab_add_div(struct isl_tab
*tab
, struct isl_vec
*div
,
1841 int (*add_ineq
)(void *user
, isl_int
*), void *user
)
1845 struct isl_mat
*samples
;
1848 r
= isl_tab_add_div(tab
, div
, add_ineq
, user
);
1851 nonneg
= tab
->var
[r
].is_nonneg
;
1852 tab
->var
[r
].frozen
= 1;
1854 samples
= isl_mat_extend(tab
->samples
,
1855 tab
->n_sample
, 1 + tab
->n_var
);
1856 tab
->samples
= samples
;
1859 for (i
= tab
->n_outside
; i
< samples
->n_row
; ++i
) {
1860 isl_seq_inner_product(div
->el
+ 1, samples
->row
[i
],
1861 div
->size
- 1, &samples
->row
[i
][samples
->n_col
- 1]);
1862 isl_int_fdiv_q(samples
->row
[i
][samples
->n_col
- 1],
1863 samples
->row
[i
][samples
->n_col
- 1], div
->el
[0]);
1869 /* Add a div specified by "div" to both the main tableau and
1870 * the context tableau. In case of the main tableau, we only
1871 * need to add an extra div. In the context tableau, we also
1872 * need to express the meaning of the div.
1873 * Return the index of the div or -1 if anything went wrong.
1875 static int add_div(struct isl_tab
*tab
, struct isl_context
*context
,
1876 struct isl_vec
*div
)
1881 if ((nonneg
= context
->op
->add_div(context
, div
)) < 0)
1884 if (!context
->op
->is_ok(context
))
1887 if (isl_tab_extend_vars(tab
, 1) < 0)
1889 r
= isl_tab_allocate_var(tab
);
1893 tab
->var
[r
].is_nonneg
= 1;
1894 tab
->var
[r
].frozen
= 1;
1897 return tab
->n_div
- 1;
1899 context
->op
->invalidate(context
);
1903 static int find_div(struct isl_tab
*tab
, isl_int
*div
, isl_int denom
)
1906 unsigned total
= isl_basic_map_total_dim(tab
->bmap
);
1908 for (i
= 0; i
< tab
->bmap
->n_div
; ++i
) {
1909 if (isl_int_ne(tab
->bmap
->div
[i
][0], denom
))
1911 if (!isl_seq_eq(tab
->bmap
->div
[i
] + 1, div
, 1 + total
))
1918 /* Return the index of a div that corresponds to "div".
1919 * We first check if we already have such a div and if not, we create one.
1921 static int get_div(struct isl_tab
*tab
, struct isl_context
*context
,
1922 struct isl_vec
*div
)
1925 struct isl_tab
*context_tab
= context
->op
->peek_tab(context
);
1930 d
= find_div(context_tab
, div
->el
+ 1, div
->el
[0]);
1934 return add_div(tab
, context
, div
);
1937 /* Add a parametric cut to cut away the non-integral sample value
1939 * Let a_i be the coefficients of the constant term and the parameters
1940 * and let b_i be the coefficients of the variables or constraints
1941 * in basis of the tableau.
1942 * Let q be the div q = floor(\sum_i {-a_i} y_i).
1944 * The cut is expressed as
1946 * c = \sum_i -{-a_i} y_i + \sum_i {b_i} x_i + q >= 0
1948 * If q did not already exist in the context tableau, then it is added first.
1949 * If q is in a column of the main tableau then the "+ q" can be accomplished
1950 * by setting the corresponding entry to the denominator of the constraint.
1951 * If q happens to be in a row of the main tableau, then the corresponding
1952 * row needs to be added instead (taking care of the denominators).
1953 * Note that this is very unlikely, but perhaps not entirely impossible.
1955 * The current value of the cut is known to be negative (or at least
1956 * non-positive), so row_sign is set accordingly.
1958 * Return the row of the cut or -1.
1960 static int add_parametric_cut(struct isl_tab
*tab
, int row
,
1961 struct isl_context
*context
)
1963 struct isl_vec
*div
;
1970 unsigned off
= 2 + tab
->M
;
1975 div
= get_row_parameter_div(tab
, row
);
1980 d
= context
->op
->get_div(context
, tab
, div
);
1985 if (isl_tab_extend_cons(tab
, 1) < 0)
1987 r
= isl_tab_allocate_con(tab
);
1991 r_row
= tab
->mat
->row
[tab
->con
[r
].index
];
1992 isl_int_set(r_row
[0], tab
->mat
->row
[row
][0]);
1993 isl_int_neg(r_row
[1], tab
->mat
->row
[row
][1]);
1994 isl_int_fdiv_r(r_row
[1], r_row
[1], tab
->mat
->row
[row
][0]);
1995 isl_int_neg(r_row
[1], r_row
[1]);
1997 isl_int_set_si(r_row
[2], 0);
1998 for (i
= 0; i
< tab
->n_param
; ++i
) {
1999 if (tab
->var
[i
].is_row
)
2001 col
= tab
->var
[i
].index
;
2002 isl_int_neg(r_row
[off
+ col
], tab
->mat
->row
[row
][off
+ col
]);
2003 isl_int_fdiv_r(r_row
[off
+ col
], r_row
[off
+ col
],
2004 tab
->mat
->row
[row
][0]);
2005 isl_int_neg(r_row
[off
+ col
], r_row
[off
+ col
]);
2007 for (i
= 0; i
< tab
->n_div
; ++i
) {
2008 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
2010 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
2011 isl_int_neg(r_row
[off
+ col
], tab
->mat
->row
[row
][off
+ col
]);
2012 isl_int_fdiv_r(r_row
[off
+ col
], r_row
[off
+ col
],
2013 tab
->mat
->row
[row
][0]);
2014 isl_int_neg(r_row
[off
+ col
], r_row
[off
+ col
]);
2016 for (i
= 0; i
< tab
->n_col
; ++i
) {
2017 if (tab
->col_var
[i
] >= 0 &&
2018 (tab
->col_var
[i
] < tab
->n_param
||
2019 tab
->col_var
[i
] >= tab
->n_var
- tab
->n_div
))
2021 isl_int_fdiv_r(r_row
[off
+ i
],
2022 tab
->mat
->row
[row
][off
+ i
], tab
->mat
->row
[row
][0]);
2024 if (tab
->var
[tab
->n_var
- tab
->n_div
+ d
].is_row
) {
2026 int d_row
= tab
->var
[tab
->n_var
- tab
->n_div
+ d
].index
;
2028 isl_int_gcd(gcd
, tab
->mat
->row
[d_row
][0], r_row
[0]);
2029 isl_int_divexact(r_row
[0], r_row
[0], gcd
);
2030 isl_int_divexact(gcd
, tab
->mat
->row
[d_row
][0], gcd
);
2031 isl_seq_combine(r_row
+ 1, gcd
, r_row
+ 1,
2032 r_row
[0], tab
->mat
->row
[d_row
] + 1,
2033 off
- 1 + tab
->n_col
);
2034 isl_int_mul(r_row
[0], r_row
[0], tab
->mat
->row
[d_row
][0]);
2037 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ d
].index
;
2038 isl_int_set(r_row
[off
+ col
], tab
->mat
->row
[row
][0]);
2041 tab
->con
[r
].is_nonneg
= 1;
2042 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
2045 tab
->row_sign
[tab
->con
[r
].index
] = isl_tab_row_neg
;
2047 row
= tab
->con
[r
].index
;
2049 if (d
>= n
&& context
->op
->detect_equalities(context
, tab
) < 0)
2055 /* Construct a tableau for bmap that can be used for computing
2056 * the lexicographic minimum (or maximum) of bmap.
2057 * If not NULL, then dom is the domain where the minimum
2058 * should be computed. In this case, we set up a parametric
2059 * tableau with row signs (initialized to "unknown").
2060 * If M is set, then the tableau will use a big parameter.
2061 * If max is set, then a maximum should be computed instead of a minimum.
2062 * This means that for each variable x, the tableau will contain the variable
2063 * x' = M - x, rather than x' = M + x. This in turn means that the coefficient
2064 * of the variables in all constraints are negated prior to adding them
2067 static struct isl_tab
*tab_for_lexmin(struct isl_basic_map
*bmap
,
2068 struct isl_basic_set
*dom
, unsigned M
, int max
)
2071 struct isl_tab
*tab
;
2073 tab
= isl_tab_alloc(bmap
->ctx
, 2 * bmap
->n_eq
+ bmap
->n_ineq
+ 1,
2074 isl_basic_map_total_dim(bmap
), M
);
2078 tab
->rational
= ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
2080 tab
->n_param
= isl_basic_set_total_dim(dom
) - dom
->n_div
;
2081 tab
->n_div
= dom
->n_div
;
2082 tab
->row_sign
= isl_calloc_array(bmap
->ctx
,
2083 enum isl_tab_row_sign
, tab
->mat
->n_row
);
2087 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
)) {
2088 if (isl_tab_mark_empty(tab
) < 0)
2093 for (i
= tab
->n_param
; i
< tab
->n_var
- tab
->n_div
; ++i
) {
2094 tab
->var
[i
].is_nonneg
= 1;
2095 tab
->var
[i
].frozen
= 1;
2097 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
2099 isl_seq_neg(bmap
->eq
[i
] + 1 + tab
->n_param
,
2100 bmap
->eq
[i
] + 1 + tab
->n_param
,
2101 tab
->n_var
- tab
->n_param
- tab
->n_div
);
2102 tab
= add_lexmin_valid_eq(tab
, bmap
->eq
[i
]);
2104 isl_seq_neg(bmap
->eq
[i
] + 1 + tab
->n_param
,
2105 bmap
->eq
[i
] + 1 + tab
->n_param
,
2106 tab
->n_var
- tab
->n_param
- tab
->n_div
);
2107 if (!tab
|| tab
->empty
)
2110 if (bmap
->n_eq
&& restore_lexmin(tab
) < 0)
2112 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
2114 isl_seq_neg(bmap
->ineq
[i
] + 1 + tab
->n_param
,
2115 bmap
->ineq
[i
] + 1 + tab
->n_param
,
2116 tab
->n_var
- tab
->n_param
- tab
->n_div
);
2117 tab
= add_lexmin_ineq(tab
, bmap
->ineq
[i
]);
2119 isl_seq_neg(bmap
->ineq
[i
] + 1 + tab
->n_param
,
2120 bmap
->ineq
[i
] + 1 + tab
->n_param
,
2121 tab
->n_var
- tab
->n_param
- tab
->n_div
);
2122 if (!tab
|| tab
->empty
)
2131 /* Given a main tableau where more than one row requires a split,
2132 * determine and return the "best" row to split on.
2134 * Given two rows in the main tableau, if the inequality corresponding
2135 * to the first row is redundant with respect to that of the second row
2136 * in the current tableau, then it is better to split on the second row,
2137 * since in the positive part, both row will be positive.
2138 * (In the negative part a pivot will have to be performed and just about
2139 * anything can happen to the sign of the other row.)
2141 * As a simple heuristic, we therefore select the row that makes the most
2142 * of the other rows redundant.
2144 * Perhaps it would also be useful to look at the number of constraints
2145 * that conflict with any given constraint.
2147 static int best_split(struct isl_tab
*tab
, struct isl_tab
*context_tab
)
2149 struct isl_tab_undo
*snap
;
2155 if (isl_tab_extend_cons(context_tab
, 2) < 0)
2158 snap
= isl_tab_snap(context_tab
);
2160 for (split
= tab
->n_redundant
; split
< tab
->n_row
; ++split
) {
2161 struct isl_tab_undo
*snap2
;
2162 struct isl_vec
*ineq
= NULL
;
2166 if (!isl_tab_var_from_row(tab
, split
)->is_nonneg
)
2168 if (tab
->row_sign
[split
] != isl_tab_row_any
)
2171 ineq
= get_row_parameter_ineq(tab
, split
);
2174 ok
= isl_tab_add_ineq(context_tab
, ineq
->el
) >= 0;
2179 snap2
= isl_tab_snap(context_tab
);
2181 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
2182 struct isl_tab_var
*var
;
2186 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
2188 if (tab
->row_sign
[row
] != isl_tab_row_any
)
2191 ineq
= get_row_parameter_ineq(tab
, row
);
2194 ok
= isl_tab_add_ineq(context_tab
, ineq
->el
) >= 0;
2198 var
= &context_tab
->con
[context_tab
->n_con
- 1];
2199 if (!context_tab
->empty
&&
2200 !isl_tab_min_at_most_neg_one(context_tab
, var
))
2202 if (isl_tab_rollback(context_tab
, snap2
) < 0)
2205 if (best
== -1 || r
> best_r
) {
2209 if (isl_tab_rollback(context_tab
, snap
) < 0)
2216 static struct isl_basic_set
*context_lex_peek_basic_set(
2217 struct isl_context
*context
)
2219 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2222 return isl_tab_peek_bset(clex
->tab
);
2225 static struct isl_tab
*context_lex_peek_tab(struct isl_context
*context
)
2227 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2231 static void context_lex_add_eq(struct isl_context
*context
, isl_int
*eq
,
2232 int check
, int update
)
2234 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2235 if (isl_tab_extend_cons(clex
->tab
, 2) < 0)
2237 if (add_lexmin_eq(clex
->tab
, eq
) < 0)
2240 int v
= tab_has_valid_sample(clex
->tab
, eq
, 1);
2244 clex
->tab
= check_integer_feasible(clex
->tab
);
2247 clex
->tab
= check_samples(clex
->tab
, eq
, 1);
2250 isl_tab_free(clex
->tab
);
2254 static void context_lex_add_ineq(struct isl_context
*context
, isl_int
*ineq
,
2255 int check
, int update
)
2257 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2258 if (isl_tab_extend_cons(clex
->tab
, 1) < 0)
2260 clex
->tab
= add_lexmin_ineq(clex
->tab
, ineq
);
2262 int v
= tab_has_valid_sample(clex
->tab
, ineq
, 0);
2266 clex
->tab
= check_integer_feasible(clex
->tab
);
2269 clex
->tab
= check_samples(clex
->tab
, ineq
, 0);
2272 isl_tab_free(clex
->tab
);
2276 static int context_lex_add_ineq_wrap(void *user
, isl_int
*ineq
)
2278 struct isl_context
*context
= (struct isl_context
*)user
;
2279 context_lex_add_ineq(context
, ineq
, 0, 0);
2280 return context
->op
->is_ok(context
) ? 0 : -1;
2283 /* Check which signs can be obtained by "ineq" on all the currently
2284 * active sample values. See row_sign for more information.
2286 static enum isl_tab_row_sign
tab_ineq_sign(struct isl_tab
*tab
, isl_int
*ineq
,
2292 enum isl_tab_row_sign res
= isl_tab_row_unknown
;
2294 isl_assert(tab
->mat
->ctx
, tab
->samples
, return isl_tab_row_unknown
);
2295 isl_assert(tab
->mat
->ctx
, tab
->samples
->n_col
== 1 + tab
->n_var
,
2296 return isl_tab_row_unknown
);
2299 for (i
= tab
->n_outside
; i
< tab
->n_sample
; ++i
) {
2300 isl_seq_inner_product(tab
->samples
->row
[i
], ineq
,
2301 1 + tab
->n_var
, &tmp
);
2302 sgn
= isl_int_sgn(tmp
);
2303 if (sgn
> 0 || (sgn
== 0 && strict
)) {
2304 if (res
== isl_tab_row_unknown
)
2305 res
= isl_tab_row_pos
;
2306 if (res
== isl_tab_row_neg
)
2307 res
= isl_tab_row_any
;
2310 if (res
== isl_tab_row_unknown
)
2311 res
= isl_tab_row_neg
;
2312 if (res
== isl_tab_row_pos
)
2313 res
= isl_tab_row_any
;
2315 if (res
== isl_tab_row_any
)
2323 static enum isl_tab_row_sign
context_lex_ineq_sign(struct isl_context
*context
,
2324 isl_int
*ineq
, int strict
)
2326 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2327 return tab_ineq_sign(clex
->tab
, ineq
, strict
);
2330 /* Check whether "ineq" can be added to the tableau without rendering
2333 static int context_lex_test_ineq(struct isl_context
*context
, isl_int
*ineq
)
2335 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2336 struct isl_tab_undo
*snap
;
2342 if (isl_tab_extend_cons(clex
->tab
, 1) < 0)
2345 snap
= isl_tab_snap(clex
->tab
);
2346 if (isl_tab_push_basis(clex
->tab
) < 0)
2348 clex
->tab
= add_lexmin_ineq(clex
->tab
, ineq
);
2349 clex
->tab
= check_integer_feasible(clex
->tab
);
2352 feasible
= !clex
->tab
->empty
;
2353 if (isl_tab_rollback(clex
->tab
, snap
) < 0)
2359 static int context_lex_get_div(struct isl_context
*context
, struct isl_tab
*tab
,
2360 struct isl_vec
*div
)
2362 return get_div(tab
, context
, div
);
2365 /* Add a div specified by "div" to the context tableau and return
2366 * 1 if the div is obviously non-negative.
2367 * context_tab_add_div will always return 1, because all variables
2368 * in a isl_context_lex tableau are non-negative.
2369 * However, if we are using a big parameter in the context, then this only
2370 * reflects the non-negativity of the variable used to _encode_ the
2371 * div, i.e., div' = M + div, so we can't draw any conclusions.
2373 static int context_lex_add_div(struct isl_context
*context
, struct isl_vec
*div
)
2375 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2377 nonneg
= context_tab_add_div(clex
->tab
, div
,
2378 context_lex_add_ineq_wrap
, context
);
2386 static int context_lex_detect_equalities(struct isl_context
*context
,
2387 struct isl_tab
*tab
)
2392 static int context_lex_best_split(struct isl_context
*context
,
2393 struct isl_tab
*tab
)
2395 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2396 struct isl_tab_undo
*snap
;
2399 snap
= isl_tab_snap(clex
->tab
);
2400 if (isl_tab_push_basis(clex
->tab
) < 0)
2402 r
= best_split(tab
, clex
->tab
);
2404 if (r
>= 0 && isl_tab_rollback(clex
->tab
, snap
) < 0)
2410 static int context_lex_is_empty(struct isl_context
*context
)
2412 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2415 return clex
->tab
->empty
;
2418 static void *context_lex_save(struct isl_context
*context
)
2420 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2421 struct isl_tab_undo
*snap
;
2423 snap
= isl_tab_snap(clex
->tab
);
2424 if (isl_tab_push_basis(clex
->tab
) < 0)
2426 if (isl_tab_save_samples(clex
->tab
) < 0)
2432 static void context_lex_restore(struct isl_context
*context
, void *save
)
2434 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2435 if (isl_tab_rollback(clex
->tab
, (struct isl_tab_undo
*)save
) < 0) {
2436 isl_tab_free(clex
->tab
);
2441 static void context_lex_discard(void *save
)
2445 static int context_lex_is_ok(struct isl_context
*context
)
2447 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2451 /* For each variable in the context tableau, check if the variable can
2452 * only attain non-negative values. If so, mark the parameter as non-negative
2453 * in the main tableau. This allows for a more direct identification of some
2454 * cases of violated constraints.
2456 static struct isl_tab
*tab_detect_nonnegative_parameters(struct isl_tab
*tab
,
2457 struct isl_tab
*context_tab
)
2460 struct isl_tab_undo
*snap
;
2461 struct isl_vec
*ineq
= NULL
;
2462 struct isl_tab_var
*var
;
2465 if (context_tab
->n_var
== 0)
2468 ineq
= isl_vec_alloc(tab
->mat
->ctx
, 1 + context_tab
->n_var
);
2472 if (isl_tab_extend_cons(context_tab
, 1) < 0)
2475 snap
= isl_tab_snap(context_tab
);
2478 isl_seq_clr(ineq
->el
, ineq
->size
);
2479 for (i
= 0; i
< context_tab
->n_var
; ++i
) {
2480 isl_int_set_si(ineq
->el
[1 + i
], 1);
2481 if (isl_tab_add_ineq(context_tab
, ineq
->el
) < 0)
2483 var
= &context_tab
->con
[context_tab
->n_con
- 1];
2484 if (!context_tab
->empty
&&
2485 !isl_tab_min_at_most_neg_one(context_tab
, var
)) {
2487 if (i
>= tab
->n_param
)
2488 j
= i
- tab
->n_param
+ tab
->n_var
- tab
->n_div
;
2489 tab
->var
[j
].is_nonneg
= 1;
2492 isl_int_set_si(ineq
->el
[1 + i
], 0);
2493 if (isl_tab_rollback(context_tab
, snap
) < 0)
2497 if (context_tab
->M
&& n
== context_tab
->n_var
) {
2498 context_tab
->mat
= isl_mat_drop_cols(context_tab
->mat
, 2, 1);
2510 static struct isl_tab
*context_lex_detect_nonnegative_parameters(
2511 struct isl_context
*context
, struct isl_tab
*tab
)
2513 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2514 struct isl_tab_undo
*snap
;
2519 snap
= isl_tab_snap(clex
->tab
);
2520 if (isl_tab_push_basis(clex
->tab
) < 0)
2523 tab
= tab_detect_nonnegative_parameters(tab
, clex
->tab
);
2525 if (isl_tab_rollback(clex
->tab
, snap
) < 0)
2534 static void context_lex_invalidate(struct isl_context
*context
)
2536 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2537 isl_tab_free(clex
->tab
);
2541 static void context_lex_free(struct isl_context
*context
)
2543 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2544 isl_tab_free(clex
->tab
);
2548 struct isl_context_op isl_context_lex_op
= {
2549 context_lex_detect_nonnegative_parameters
,
2550 context_lex_peek_basic_set
,
2551 context_lex_peek_tab
,
2553 context_lex_add_ineq
,
2554 context_lex_ineq_sign
,
2555 context_lex_test_ineq
,
2556 context_lex_get_div
,
2557 context_lex_add_div
,
2558 context_lex_detect_equalities
,
2559 context_lex_best_split
,
2560 context_lex_is_empty
,
2563 context_lex_restore
,
2564 context_lex_discard
,
2565 context_lex_invalidate
,
2569 static struct isl_tab
*context_tab_for_lexmin(struct isl_basic_set
*bset
)
2571 struct isl_tab
*tab
;
2575 tab
= tab_for_lexmin((struct isl_basic_map
*)bset
, NULL
, 1, 0);
2578 if (isl_tab_track_bset(tab
, bset
) < 0)
2580 tab
= isl_tab_init_samples(tab
);
2583 isl_basic_set_free(bset
);
2587 static struct isl_context
*isl_context_lex_alloc(struct isl_basic_set
*dom
)
2589 struct isl_context_lex
*clex
;
2594 clex
= isl_alloc_type(dom
->ctx
, struct isl_context_lex
);
2598 clex
->context
.op
= &isl_context_lex_op
;
2600 clex
->tab
= context_tab_for_lexmin(isl_basic_set_copy(dom
));
2601 if (restore_lexmin(clex
->tab
) < 0)
2603 clex
->tab
= check_integer_feasible(clex
->tab
);
2607 return &clex
->context
;
2609 clex
->context
.op
->free(&clex
->context
);
2613 /* Representation of the context when using generalized basis reduction.
2615 * "shifted" contains the offsets of the unit hypercubes that lie inside the
2616 * context. Any rational point in "shifted" can therefore be rounded
2617 * up to an integer point in the context.
2618 * If the context is constrained by any equality, then "shifted" is not used
2619 * as it would be empty.
2621 struct isl_context_gbr
{
2622 struct isl_context context
;
2623 struct isl_tab
*tab
;
2624 struct isl_tab
*shifted
;
2625 struct isl_tab
*cone
;
2628 static struct isl_tab
*context_gbr_detect_nonnegative_parameters(
2629 struct isl_context
*context
, struct isl_tab
*tab
)
2631 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2634 return tab_detect_nonnegative_parameters(tab
, cgbr
->tab
);
2637 static struct isl_basic_set
*context_gbr_peek_basic_set(
2638 struct isl_context
*context
)
2640 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2643 return isl_tab_peek_bset(cgbr
->tab
);
2646 static struct isl_tab
*context_gbr_peek_tab(struct isl_context
*context
)
2648 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2652 /* Initialize the "shifted" tableau of the context, which
2653 * contains the constraints of the original tableau shifted
2654 * by the sum of all negative coefficients. This ensures
2655 * that any rational point in the shifted tableau can
2656 * be rounded up to yield an integer point in the original tableau.
2658 static void gbr_init_shifted(struct isl_context_gbr
*cgbr
)
2661 struct isl_vec
*cst
;
2662 struct isl_basic_set
*bset
= isl_tab_peek_bset(cgbr
->tab
);
2663 unsigned dim
= isl_basic_set_total_dim(bset
);
2665 cst
= isl_vec_alloc(cgbr
->tab
->mat
->ctx
, bset
->n_ineq
);
2669 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
2670 isl_int_set(cst
->el
[i
], bset
->ineq
[i
][0]);
2671 for (j
= 0; j
< dim
; ++j
) {
2672 if (!isl_int_is_neg(bset
->ineq
[i
][1 + j
]))
2674 isl_int_add(bset
->ineq
[i
][0], bset
->ineq
[i
][0],
2675 bset
->ineq
[i
][1 + j
]);
2679 cgbr
->shifted
= isl_tab_from_basic_set(bset
, 0);
2681 for (i
= 0; i
< bset
->n_ineq
; ++i
)
2682 isl_int_set(bset
->ineq
[i
][0], cst
->el
[i
]);
2687 /* Check if the shifted tableau is non-empty, and if so
2688 * use the sample point to construct an integer point
2689 * of the context tableau.
2691 static struct isl_vec
*gbr_get_shifted_sample(struct isl_context_gbr
*cgbr
)
2693 struct isl_vec
*sample
;
2696 gbr_init_shifted(cgbr
);
2699 if (cgbr
->shifted
->empty
)
2700 return isl_vec_alloc(cgbr
->tab
->mat
->ctx
, 0);
2702 sample
= isl_tab_get_sample_value(cgbr
->shifted
);
2703 sample
= isl_vec_ceil(sample
);
2708 static struct isl_basic_set
*drop_constant_terms(struct isl_basic_set
*bset
)
2715 for (i
= 0; i
< bset
->n_eq
; ++i
)
2716 isl_int_set_si(bset
->eq
[i
][0], 0);
2718 for (i
= 0; i
< bset
->n_ineq
; ++i
)
2719 isl_int_set_si(bset
->ineq
[i
][0], 0);
2724 static int use_shifted(struct isl_context_gbr
*cgbr
)
2726 return cgbr
->tab
->bmap
->n_eq
== 0 && cgbr
->tab
->bmap
->n_div
== 0;
2729 static struct isl_vec
*gbr_get_sample(struct isl_context_gbr
*cgbr
)
2731 struct isl_basic_set
*bset
;
2732 struct isl_basic_set
*cone
;
2734 if (isl_tab_sample_is_integer(cgbr
->tab
))
2735 return isl_tab_get_sample_value(cgbr
->tab
);
2737 if (use_shifted(cgbr
)) {
2738 struct isl_vec
*sample
;
2740 sample
= gbr_get_shifted_sample(cgbr
);
2741 if (!sample
|| sample
->size
> 0)
2744 isl_vec_free(sample
);
2748 bset
= isl_tab_peek_bset(cgbr
->tab
);
2749 cgbr
->cone
= isl_tab_from_recession_cone(bset
, 0);
2752 if (isl_tab_track_bset(cgbr
->cone
,
2753 isl_basic_set_copy(bset
)) < 0)
2756 if (isl_tab_detect_implicit_equalities(cgbr
->cone
) < 0)
2759 if (cgbr
->cone
->n_dead
== cgbr
->cone
->n_col
) {
2760 struct isl_vec
*sample
;
2761 struct isl_tab_undo
*snap
;
2763 if (cgbr
->tab
->basis
) {
2764 if (cgbr
->tab
->basis
->n_col
!= 1 + cgbr
->tab
->n_var
) {
2765 isl_mat_free(cgbr
->tab
->basis
);
2766 cgbr
->tab
->basis
= NULL
;
2768 cgbr
->tab
->n_zero
= 0;
2769 cgbr
->tab
->n_unbounded
= 0;
2772 snap
= isl_tab_snap(cgbr
->tab
);
2774 sample
= isl_tab_sample(cgbr
->tab
);
2776 if (isl_tab_rollback(cgbr
->tab
, snap
) < 0) {
2777 isl_vec_free(sample
);
2784 cone
= isl_basic_set_dup(isl_tab_peek_bset(cgbr
->cone
));
2785 cone
= drop_constant_terms(cone
);
2786 cone
= isl_basic_set_update_from_tab(cone
, cgbr
->cone
);
2787 cone
= isl_basic_set_underlying_set(cone
);
2788 cone
= isl_basic_set_gauss(cone
, NULL
);
2790 bset
= isl_basic_set_dup(isl_tab_peek_bset(cgbr
->tab
));
2791 bset
= isl_basic_set_update_from_tab(bset
, cgbr
->tab
);
2792 bset
= isl_basic_set_underlying_set(bset
);
2793 bset
= isl_basic_set_gauss(bset
, NULL
);
2795 return isl_basic_set_sample_with_cone(bset
, cone
);
2798 static void check_gbr_integer_feasible(struct isl_context_gbr
*cgbr
)
2800 struct isl_vec
*sample
;
2805 if (cgbr
->tab
->empty
)
2808 sample
= gbr_get_sample(cgbr
);
2812 if (sample
->size
== 0) {
2813 isl_vec_free(sample
);
2814 if (isl_tab_mark_empty(cgbr
->tab
) < 0)
2819 cgbr
->tab
= isl_tab_add_sample(cgbr
->tab
, sample
);
2823 isl_tab_free(cgbr
->tab
);
2827 static struct isl_tab
*add_gbr_eq(struct isl_tab
*tab
, isl_int
*eq
)
2832 if (isl_tab_extend_cons(tab
, 2) < 0)
2835 if (isl_tab_add_eq(tab
, eq
) < 0)
2844 /* Add the equality described by "eq" to the context.
2845 * If "check" is set, then we check if the context is empty after
2846 * adding the equality.
2847 * If "update" is set, then we check if the samples are still valid.
2849 * We do not explicitly add shifted copies of the equality to
2850 * cgbr->shifted since they would conflict with each other.
2851 * Instead, we directly mark cgbr->shifted empty.
2853 static void context_gbr_add_eq(struct isl_context
*context
, isl_int
*eq
,
2854 int check
, int update
)
2856 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2858 cgbr
->tab
= add_gbr_eq(cgbr
->tab
, eq
);
2860 if (cgbr
->shifted
&& !cgbr
->shifted
->empty
&& use_shifted(cgbr
)) {
2861 if (isl_tab_mark_empty(cgbr
->shifted
) < 0)
2865 if (cgbr
->cone
&& cgbr
->cone
->n_col
!= cgbr
->cone
->n_dead
) {
2866 if (isl_tab_extend_cons(cgbr
->cone
, 2) < 0)
2868 if (isl_tab_add_eq(cgbr
->cone
, eq
) < 0)
2873 int v
= tab_has_valid_sample(cgbr
->tab
, eq
, 1);
2877 check_gbr_integer_feasible(cgbr
);
2880 cgbr
->tab
= check_samples(cgbr
->tab
, eq
, 1);
2883 isl_tab_free(cgbr
->tab
);
2887 static void add_gbr_ineq(struct isl_context_gbr
*cgbr
, isl_int
*ineq
)
2892 if (isl_tab_extend_cons(cgbr
->tab
, 1) < 0)
2895 if (isl_tab_add_ineq(cgbr
->tab
, ineq
) < 0)
2898 if (cgbr
->shifted
&& !cgbr
->shifted
->empty
&& use_shifted(cgbr
)) {
2901 dim
= isl_basic_map_total_dim(cgbr
->tab
->bmap
);
2903 if (isl_tab_extend_cons(cgbr
->shifted
, 1) < 0)
2906 for (i
= 0; i
< dim
; ++i
) {
2907 if (!isl_int_is_neg(ineq
[1 + i
]))
2909 isl_int_add(ineq
[0], ineq
[0], ineq
[1 + i
]);
2912 if (isl_tab_add_ineq(cgbr
->shifted
, ineq
) < 0)
2915 for (i
= 0; i
< dim
; ++i
) {
2916 if (!isl_int_is_neg(ineq
[1 + i
]))
2918 isl_int_sub(ineq
[0], ineq
[0], ineq
[1 + i
]);
2922 if (cgbr
->cone
&& cgbr
->cone
->n_col
!= cgbr
->cone
->n_dead
) {
2923 if (isl_tab_extend_cons(cgbr
->cone
, 1) < 0)
2925 if (isl_tab_add_ineq(cgbr
->cone
, ineq
) < 0)
2931 isl_tab_free(cgbr
->tab
);
2935 static void context_gbr_add_ineq(struct isl_context
*context
, isl_int
*ineq
,
2936 int check
, int update
)
2938 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2940 add_gbr_ineq(cgbr
, ineq
);
2945 int v
= tab_has_valid_sample(cgbr
->tab
, ineq
, 0);
2949 check_gbr_integer_feasible(cgbr
);
2952 cgbr
->tab
= check_samples(cgbr
->tab
, ineq
, 0);
2955 isl_tab_free(cgbr
->tab
);
2959 static int context_gbr_add_ineq_wrap(void *user
, isl_int
*ineq
)
2961 struct isl_context
*context
= (struct isl_context
*)user
;
2962 context_gbr_add_ineq(context
, ineq
, 0, 0);
2963 return context
->op
->is_ok(context
) ? 0 : -1;
2966 static enum isl_tab_row_sign
context_gbr_ineq_sign(struct isl_context
*context
,
2967 isl_int
*ineq
, int strict
)
2969 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2970 return tab_ineq_sign(cgbr
->tab
, ineq
, strict
);
2973 /* Check whether "ineq" can be added to the tableau without rendering
2976 static int context_gbr_test_ineq(struct isl_context
*context
, isl_int
*ineq
)
2978 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2979 struct isl_tab_undo
*snap
;
2980 struct isl_tab_undo
*shifted_snap
= NULL
;
2981 struct isl_tab_undo
*cone_snap
= NULL
;
2987 if (isl_tab_extend_cons(cgbr
->tab
, 1) < 0)
2990 snap
= isl_tab_snap(cgbr
->tab
);
2992 shifted_snap
= isl_tab_snap(cgbr
->shifted
);
2994 cone_snap
= isl_tab_snap(cgbr
->cone
);
2995 add_gbr_ineq(cgbr
, ineq
);
2996 check_gbr_integer_feasible(cgbr
);
2999 feasible
= !cgbr
->tab
->empty
;
3000 if (isl_tab_rollback(cgbr
->tab
, snap
) < 0)
3003 if (isl_tab_rollback(cgbr
->shifted
, shifted_snap
))
3005 } else if (cgbr
->shifted
) {
3006 isl_tab_free(cgbr
->shifted
);
3007 cgbr
->shifted
= NULL
;
3010 if (isl_tab_rollback(cgbr
->cone
, cone_snap
))
3012 } else if (cgbr
->cone
) {
3013 isl_tab_free(cgbr
->cone
);
3020 /* Return the column of the last of the variables associated to
3021 * a column that has a non-zero coefficient.
3022 * This function is called in a context where only coefficients
3023 * of parameters or divs can be non-zero.
3025 static int last_non_zero_var_col(struct isl_tab
*tab
, isl_int
*p
)
3030 if (tab
->n_var
== 0)
3033 for (i
= tab
->n_var
- 1; i
>= 0; --i
) {
3034 if (i
>= tab
->n_param
&& i
< tab
->n_var
- tab
->n_div
)
3036 if (tab
->var
[i
].is_row
)
3038 col
= tab
->var
[i
].index
;
3039 if (!isl_int_is_zero(p
[col
]))
3046 /* Look through all the recently added equalities in the context
3047 * to see if we can propagate any of them to the main tableau.
3049 * The newly added equalities in the context are encoded as pairs
3050 * of inequalities starting at inequality "first".
3052 * We tentatively add each of these equalities to the main tableau
3053 * and if this happens to result in a row with a final coefficient
3054 * that is one or negative one, we use it to kill a column
3055 * in the main tableau. Otherwise, we discard the tentatively
3058 static void propagate_equalities(struct isl_context_gbr
*cgbr
,
3059 struct isl_tab
*tab
, unsigned first
)
3062 struct isl_vec
*eq
= NULL
;
3064 eq
= isl_vec_alloc(tab
->mat
->ctx
, 1 + tab
->n_var
);
3068 if (isl_tab_extend_cons(tab
, (cgbr
->tab
->bmap
->n_ineq
- first
)/2) < 0)
3071 isl_seq_clr(eq
->el
+ 1 + tab
->n_param
,
3072 tab
->n_var
- tab
->n_param
- tab
->n_div
);
3073 for (i
= first
; i
< cgbr
->tab
->bmap
->n_ineq
; i
+= 2) {
3076 struct isl_tab_undo
*snap
;
3077 snap
= isl_tab_snap(tab
);
3079 isl_seq_cpy(eq
->el
, cgbr
->tab
->bmap
->ineq
[i
], 1 + tab
->n_param
);
3080 isl_seq_cpy(eq
->el
+ 1 + tab
->n_var
- tab
->n_div
,
3081 cgbr
->tab
->bmap
->ineq
[i
] + 1 + tab
->n_param
,
3084 r
= isl_tab_add_row(tab
, eq
->el
);
3087 r
= tab
->con
[r
].index
;
3088 j
= last_non_zero_var_col(tab
, tab
->mat
->row
[r
] + 2 + tab
->M
);
3089 if (j
< 0 || j
< tab
->n_dead
||
3090 !isl_int_is_one(tab
->mat
->row
[r
][0]) ||
3091 (!isl_int_is_one(tab
->mat
->row
[r
][2 + tab
->M
+ j
]) &&
3092 !isl_int_is_negone(tab
->mat
->row
[r
][2 + tab
->M
+ j
]))) {
3093 if (isl_tab_rollback(tab
, snap
) < 0)
3097 if (isl_tab_pivot(tab
, r
, j
) < 0)
3099 if (isl_tab_kill_col(tab
, j
) < 0)
3102 if (restore_lexmin(tab
) < 0)
3111 isl_tab_free(cgbr
->tab
);
3115 static int context_gbr_detect_equalities(struct isl_context
*context
,
3116 struct isl_tab
*tab
)
3118 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3119 struct isl_ctx
*ctx
;
3122 ctx
= cgbr
->tab
->mat
->ctx
;
3125 struct isl_basic_set
*bset
= isl_tab_peek_bset(cgbr
->tab
);
3126 cgbr
->cone
= isl_tab_from_recession_cone(bset
, 0);
3129 if (isl_tab_track_bset(cgbr
->cone
,
3130 isl_basic_set_copy(bset
)) < 0)
3133 if (isl_tab_detect_implicit_equalities(cgbr
->cone
) < 0)
3136 n_ineq
= cgbr
->tab
->bmap
->n_ineq
;
3137 cgbr
->tab
= isl_tab_detect_equalities(cgbr
->tab
, cgbr
->cone
);
3140 if (cgbr
->tab
->bmap
->n_ineq
> n_ineq
)
3141 propagate_equalities(cgbr
, tab
, n_ineq
);
3145 isl_tab_free(cgbr
->tab
);
3150 static int context_gbr_get_div(struct isl_context
*context
, struct isl_tab
*tab
,
3151 struct isl_vec
*div
)
3153 return get_div(tab
, context
, div
);
3156 static int context_gbr_add_div(struct isl_context
*context
, struct isl_vec
*div
)
3158 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3162 if (isl_tab_extend_cons(cgbr
->cone
, 3) < 0)
3164 if (isl_tab_extend_vars(cgbr
->cone
, 1) < 0)
3166 if (isl_tab_allocate_var(cgbr
->cone
) <0)
3169 cgbr
->cone
->bmap
= isl_basic_map_extend_space(cgbr
->cone
->bmap
,
3170 isl_basic_map_get_space(cgbr
->cone
->bmap
), 1, 0, 2);
3171 k
= isl_basic_map_alloc_div(cgbr
->cone
->bmap
);
3174 isl_seq_cpy(cgbr
->cone
->bmap
->div
[k
], div
->el
, div
->size
);
3175 if (isl_tab_push(cgbr
->cone
, isl_tab_undo_bmap_div
) < 0)
3178 return context_tab_add_div(cgbr
->tab
, div
,
3179 context_gbr_add_ineq_wrap
, context
);
3182 static int context_gbr_best_split(struct isl_context
*context
,
3183 struct isl_tab
*tab
)
3185 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3186 struct isl_tab_undo
*snap
;
3189 snap
= isl_tab_snap(cgbr
->tab
);
3190 r
= best_split(tab
, cgbr
->tab
);
3192 if (r
>= 0 && isl_tab_rollback(cgbr
->tab
, snap
) < 0)
3198 static int context_gbr_is_empty(struct isl_context
*context
)
3200 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3203 return cgbr
->tab
->empty
;
3206 struct isl_gbr_tab_undo
{
3207 struct isl_tab_undo
*tab_snap
;
3208 struct isl_tab_undo
*shifted_snap
;
3209 struct isl_tab_undo
*cone_snap
;
3212 static void *context_gbr_save(struct isl_context
*context
)
3214 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3215 struct isl_gbr_tab_undo
*snap
;
3217 snap
= isl_alloc_type(cgbr
->tab
->mat
->ctx
, struct isl_gbr_tab_undo
);
3221 snap
->tab_snap
= isl_tab_snap(cgbr
->tab
);
3222 if (isl_tab_save_samples(cgbr
->tab
) < 0)
3226 snap
->shifted_snap
= isl_tab_snap(cgbr
->shifted
);
3228 snap
->shifted_snap
= NULL
;
3231 snap
->cone_snap
= isl_tab_snap(cgbr
->cone
);
3233 snap
->cone_snap
= NULL
;
3241 static void context_gbr_restore(struct isl_context
*context
, void *save
)
3243 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3244 struct isl_gbr_tab_undo
*snap
= (struct isl_gbr_tab_undo
*)save
;
3247 if (isl_tab_rollback(cgbr
->tab
, snap
->tab_snap
) < 0) {
3248 isl_tab_free(cgbr
->tab
);
3252 if (snap
->shifted_snap
) {
3253 if (isl_tab_rollback(cgbr
->shifted
, snap
->shifted_snap
) < 0)
3255 } else if (cgbr
->shifted
) {
3256 isl_tab_free(cgbr
->shifted
);
3257 cgbr
->shifted
= NULL
;
3260 if (snap
->cone_snap
) {
3261 if (isl_tab_rollback(cgbr
->cone
, snap
->cone_snap
) < 0)
3263 } else if (cgbr
->cone
) {
3264 isl_tab_free(cgbr
->cone
);
3273 isl_tab_free(cgbr
->tab
);
3277 static void context_gbr_discard(void *save
)
3279 struct isl_gbr_tab_undo
*snap
= (struct isl_gbr_tab_undo
*)save
;
3283 static int context_gbr_is_ok(struct isl_context
*context
)
3285 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3289 static void context_gbr_invalidate(struct isl_context
*context
)
3291 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3292 isl_tab_free(cgbr
->tab
);
3296 static void context_gbr_free(struct isl_context
*context
)
3298 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3299 isl_tab_free(cgbr
->tab
);
3300 isl_tab_free(cgbr
->shifted
);
3301 isl_tab_free(cgbr
->cone
);
3305 struct isl_context_op isl_context_gbr_op
= {
3306 context_gbr_detect_nonnegative_parameters
,
3307 context_gbr_peek_basic_set
,
3308 context_gbr_peek_tab
,
3310 context_gbr_add_ineq
,
3311 context_gbr_ineq_sign
,
3312 context_gbr_test_ineq
,
3313 context_gbr_get_div
,
3314 context_gbr_add_div
,
3315 context_gbr_detect_equalities
,
3316 context_gbr_best_split
,
3317 context_gbr_is_empty
,
3320 context_gbr_restore
,
3321 context_gbr_discard
,
3322 context_gbr_invalidate
,
3326 static struct isl_context
*isl_context_gbr_alloc(struct isl_basic_set
*dom
)
3328 struct isl_context_gbr
*cgbr
;
3333 cgbr
= isl_calloc_type(dom
->ctx
, struct isl_context_gbr
);
3337 cgbr
->context
.op
= &isl_context_gbr_op
;
3339 cgbr
->shifted
= NULL
;
3341 cgbr
->tab
= isl_tab_from_basic_set(dom
, 1);
3342 cgbr
->tab
= isl_tab_init_samples(cgbr
->tab
);
3345 check_gbr_integer_feasible(cgbr
);
3347 return &cgbr
->context
;
3349 cgbr
->context
.op
->free(&cgbr
->context
);
3353 static struct isl_context
*isl_context_alloc(struct isl_basic_set
*dom
)
3358 if (dom
->ctx
->opt
->context
== ISL_CONTEXT_LEXMIN
)
3359 return isl_context_lex_alloc(dom
);
3361 return isl_context_gbr_alloc(dom
);
3364 /* Construct an isl_sol_map structure for accumulating the solution.
3365 * If track_empty is set, then we also keep track of the parts
3366 * of the context where there is no solution.
3367 * If max is set, then we are solving a maximization, rather than
3368 * a minimization problem, which means that the variables in the
3369 * tableau have value "M - x" rather than "M + x".
3371 static struct isl_sol
*sol_map_init(struct isl_basic_map
*bmap
,
3372 struct isl_basic_set
*dom
, int track_empty
, int max
)
3374 struct isl_sol_map
*sol_map
= NULL
;
3379 sol_map
= isl_calloc_type(bmap
->ctx
, struct isl_sol_map
);
3383 sol_map
->sol
.rational
= ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
3384 sol_map
->sol
.dec_level
.callback
.run
= &sol_dec_level_wrap
;
3385 sol_map
->sol
.dec_level
.sol
= &sol_map
->sol
;
3386 sol_map
->sol
.max
= max
;
3387 sol_map
->sol
.n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
3388 sol_map
->sol
.add
= &sol_map_add_wrap
;
3389 sol_map
->sol
.add_empty
= track_empty
? &sol_map_add_empty_wrap
: NULL
;
3390 sol_map
->sol
.free
= &sol_map_free_wrap
;
3391 sol_map
->map
= isl_map_alloc_space(isl_basic_map_get_space(bmap
), 1,
3396 sol_map
->sol
.context
= isl_context_alloc(dom
);
3397 if (!sol_map
->sol
.context
)
3401 sol_map
->empty
= isl_set_alloc_space(isl_basic_set_get_space(dom
),
3402 1, ISL_SET_DISJOINT
);
3403 if (!sol_map
->empty
)
3407 isl_basic_set_free(dom
);
3408 return &sol_map
->sol
;
3410 isl_basic_set_free(dom
);
3411 sol_map_free(sol_map
);
3415 /* Check whether all coefficients of (non-parameter) variables
3416 * are non-positive, meaning that no pivots can be performed on the row.
3418 static int is_critical(struct isl_tab
*tab
, int row
)
3421 unsigned off
= 2 + tab
->M
;
3423 for (j
= tab
->n_dead
; j
< tab
->n_col
; ++j
) {
3424 if (tab
->col_var
[j
] >= 0 &&
3425 (tab
->col_var
[j
] < tab
->n_param
||
3426 tab
->col_var
[j
] >= tab
->n_var
- tab
->n_div
))
3429 if (isl_int_is_pos(tab
->mat
->row
[row
][off
+ j
]))
3436 /* Check whether the inequality represented by vec is strict over the integers,
3437 * i.e., there are no integer values satisfying the constraint with
3438 * equality. This happens if the gcd of the coefficients is not a divisor
3439 * of the constant term. If so, scale the constraint down by the gcd
3440 * of the coefficients.
3442 static int is_strict(struct isl_vec
*vec
)
3448 isl_seq_gcd(vec
->el
+ 1, vec
->size
- 1, &gcd
);
3449 if (!isl_int_is_one(gcd
)) {
3450 strict
= !isl_int_is_divisible_by(vec
->el
[0], gcd
);
3451 isl_int_fdiv_q(vec
->el
[0], vec
->el
[0], gcd
);
3452 isl_seq_scale_down(vec
->el
+ 1, vec
->el
+ 1, gcd
, vec
->size
-1);
3459 /* Determine the sign of the given row of the main tableau.
3460 * The result is one of
3461 * isl_tab_row_pos: always non-negative; no pivot needed
3462 * isl_tab_row_neg: always non-positive; pivot
3463 * isl_tab_row_any: can be both positive and negative; split
3465 * We first handle some simple cases
3466 * - the row sign may be known already
3467 * - the row may be obviously non-negative
3468 * - the parametric constant may be equal to that of another row
3469 * for which we know the sign. This sign will be either "pos" or
3470 * "any". If it had been "neg" then we would have pivoted before.
3472 * If none of these cases hold, we check the value of the row for each
3473 * of the currently active samples. Based on the signs of these values
3474 * we make an initial determination of the sign of the row.
3476 * all zero -> unk(nown)
3477 * all non-negative -> pos
3478 * all non-positive -> neg
3479 * both negative and positive -> all
3481 * If we end up with "all", we are done.
3482 * Otherwise, we perform a check for positive and/or negative
3483 * values as follows.
3485 * samples neg unk pos
3491 * There is no special sign for "zero", because we can usually treat zero
3492 * as either non-negative or non-positive, whatever works out best.
3493 * However, if the row is "critical", meaning that pivoting is impossible
3494 * then we don't want to limp zero with the non-positive case, because
3495 * then we we would lose the solution for those values of the parameters
3496 * where the value of the row is zero. Instead, we treat 0 as non-negative
3497 * ensuring a split if the row can attain both zero and negative values.
3498 * The same happens when the original constraint was one that could not
3499 * be satisfied with equality by any integer values of the parameters.
3500 * In this case, we normalize the constraint, but then a value of zero
3501 * for the normalized constraint is actually a positive value for the
3502 * original constraint, so again we need to treat zero as non-negative.
3503 * In both these cases, we have the following decision tree instead:
3505 * all non-negative -> pos
3506 * all negative -> neg
3507 * both negative and non-negative -> all
3515 static enum isl_tab_row_sign
row_sign(struct isl_tab
*tab
,
3516 struct isl_sol
*sol
, int row
)
3518 struct isl_vec
*ineq
= NULL
;
3519 enum isl_tab_row_sign res
= isl_tab_row_unknown
;
3524 if (tab
->row_sign
[row
] != isl_tab_row_unknown
)
3525 return tab
->row_sign
[row
];
3526 if (is_obviously_nonneg(tab
, row
))
3527 return isl_tab_row_pos
;
3528 for (row2
= tab
->n_redundant
; row2
< tab
->n_row
; ++row2
) {
3529 if (tab
->row_sign
[row2
] == isl_tab_row_unknown
)
3531 if (identical_parameter_line(tab
, row
, row2
))
3532 return tab
->row_sign
[row2
];
3535 critical
= is_critical(tab
, row
);
3537 ineq
= get_row_parameter_ineq(tab
, row
);
3541 strict
= is_strict(ineq
);
3543 res
= sol
->context
->op
->ineq_sign(sol
->context
, ineq
->el
,
3544 critical
|| strict
);
3546 if (res
== isl_tab_row_unknown
|| res
== isl_tab_row_pos
) {
3547 /* test for negative values */
3549 isl_seq_neg(ineq
->el
, ineq
->el
, ineq
->size
);
3550 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3552 feasible
= sol
->context
->op
->test_ineq(sol
->context
, ineq
->el
);
3556 res
= isl_tab_row_pos
;
3558 res
= (res
== isl_tab_row_unknown
) ? isl_tab_row_neg
3560 if (res
== isl_tab_row_neg
) {
3561 isl_seq_neg(ineq
->el
, ineq
->el
, ineq
->size
);
3562 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3566 if (res
== isl_tab_row_neg
) {
3567 /* test for positive values */
3569 if (!critical
&& !strict
)
3570 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3572 feasible
= sol
->context
->op
->test_ineq(sol
->context
, ineq
->el
);
3576 res
= isl_tab_row_any
;
3583 return isl_tab_row_unknown
;
3586 static void find_solutions(struct isl_sol
*sol
, struct isl_tab
*tab
);
3588 /* Find solutions for values of the parameters that satisfy the given
3591 * We currently take a snapshot of the context tableau that is reset
3592 * when we return from this function, while we make a copy of the main
3593 * tableau, leaving the original main tableau untouched.
3594 * These are fairly arbitrary choices. Making a copy also of the context
3595 * tableau would obviate the need to undo any changes made to it later,
3596 * while taking a snapshot of the main tableau could reduce memory usage.
3597 * If we were to switch to taking a snapshot of the main tableau,
3598 * we would have to keep in mind that we need to save the row signs
3599 * and that we need to do this before saving the current basis
3600 * such that the basis has been restore before we restore the row signs.
3602 static void find_in_pos(struct isl_sol
*sol
, struct isl_tab
*tab
, isl_int
*ineq
)
3608 saved
= sol
->context
->op
->save(sol
->context
);
3610 tab
= isl_tab_dup(tab
);
3614 sol
->context
->op
->add_ineq(sol
->context
, ineq
, 0, 1);
3616 find_solutions(sol
, tab
);
3619 sol
->context
->op
->restore(sol
->context
, saved
);
3621 sol
->context
->op
->discard(saved
);
3627 /* Record the absence of solutions for those values of the parameters
3628 * that do not satisfy the given inequality with equality.
3630 static void no_sol_in_strict(struct isl_sol
*sol
,
3631 struct isl_tab
*tab
, struct isl_vec
*ineq
)
3636 if (!sol
->context
|| sol
->error
)
3638 saved
= sol
->context
->op
->save(sol
->context
);
3640 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3642 sol
->context
->op
->add_ineq(sol
->context
, ineq
->el
, 1, 0);
3651 isl_int_add_ui(ineq
->el
[0], ineq
->el
[0], 1);
3653 sol
->context
->op
->restore(sol
->context
, saved
);
3659 /* Compute the lexicographic minimum of the set represented by the main
3660 * tableau "tab" within the context "sol->context_tab".
3661 * On entry the sample value of the main tableau is lexicographically
3662 * less than or equal to this lexicographic minimum.
3663 * Pivots are performed until a feasible point is found, which is then
3664 * necessarily equal to the minimum, or until the tableau is found to
3665 * be infeasible. Some pivots may need to be performed for only some
3666 * feasible values of the context tableau. If so, the context tableau
3667 * is split into a part where the pivot is needed and a part where it is not.
3669 * Whenever we enter the main loop, the main tableau is such that no
3670 * "obvious" pivots need to be performed on it, where "obvious" means
3671 * that the given row can be seen to be negative without looking at
3672 * the context tableau. In particular, for non-parametric problems,
3673 * no pivots need to be performed on the main tableau.
3674 * The caller of find_solutions is responsible for making this property
3675 * hold prior to the first iteration of the loop, while restore_lexmin
3676 * is called before every other iteration.
3678 * Inside the main loop, we first examine the signs of the rows of
3679 * the main tableau within the context of the context tableau.
3680 * If we find a row that is always non-positive for all values of
3681 * the parameters satisfying the context tableau and negative for at
3682 * least one value of the parameters, we perform the appropriate pivot
3683 * and start over. An exception is the case where no pivot can be
3684 * performed on the row. In this case, we require that the sign of
3685 * the row is negative for all values of the parameters (rather than just
3686 * non-positive). This special case is handled inside row_sign, which
3687 * will say that the row can have any sign if it determines that it can
3688 * attain both negative and zero values.
3690 * If we can't find a row that always requires a pivot, but we can find
3691 * one or more rows that require a pivot for some values of the parameters
3692 * (i.e., the row can attain both positive and negative signs), then we split
3693 * the context tableau into two parts, one where we force the sign to be
3694 * non-negative and one where we force is to be negative.
3695 * The non-negative part is handled by a recursive call (through find_in_pos).
3696 * Upon returning from this call, we continue with the negative part and
3697 * perform the required pivot.
3699 * If no such rows can be found, all rows are non-negative and we have
3700 * found a (rational) feasible point. If we only wanted a rational point
3702 * Otherwise, we check if all values of the sample point of the tableau
3703 * are integral for the variables. If so, we have found the minimal
3704 * integral point and we are done.
3705 * If the sample point is not integral, then we need to make a distinction
3706 * based on whether the constant term is non-integral or the coefficients
3707 * of the parameters. Furthermore, in order to decide how to handle
3708 * the non-integrality, we also need to know whether the coefficients
3709 * of the other columns in the tableau are integral. This leads
3710 * to the following table. The first two rows do not correspond
3711 * to a non-integral sample point and are only mentioned for completeness.
3713 * constant parameters other
3716 * int int rat | -> no problem
3718 * rat int int -> fail
3720 * rat int rat -> cut
3723 * rat rat rat | -> parametric cut
3726 * rat rat int | -> split context
3728 * If the parametric constant is completely integral, then there is nothing
3729 * to be done. If the constant term is non-integral, but all the other
3730 * coefficient are integral, then there is nothing that can be done
3731 * and the tableau has no integral solution.
3732 * If, on the other hand, one or more of the other columns have rational
3733 * coefficients, but the parameter coefficients are all integral, then
3734 * we can perform a regular (non-parametric) cut.
3735 * Finally, if there is any parameter coefficient that is non-integral,
3736 * then we need to involve the context tableau. There are two cases here.
3737 * If at least one other column has a rational coefficient, then we
3738 * can perform a parametric cut in the main tableau by adding a new
3739 * integer division in the context tableau.
3740 * If all other columns have integral coefficients, then we need to
3741 * enforce that the rational combination of parameters (c + \sum a_i y_i)/m
3742 * is always integral. We do this by introducing an integer division
3743 * q = floor((c + \sum a_i y_i)/m) and stipulating that its argument should
3744 * always be integral in the context tableau, i.e., m q = c + \sum a_i y_i.
3745 * Since q is expressed in the tableau as
3746 * c + \sum a_i y_i - m q >= 0
3747 * -c - \sum a_i y_i + m q + m - 1 >= 0
3748 * it is sufficient to add the inequality
3749 * -c - \sum a_i y_i + m q >= 0
3750 * In the part of the context where this inequality does not hold, the
3751 * main tableau is marked as being empty.
3753 static void find_solutions(struct isl_sol
*sol
, struct isl_tab
*tab
)
3755 struct isl_context
*context
;
3758 if (!tab
|| sol
->error
)
3761 context
= sol
->context
;
3765 if (context
->op
->is_empty(context
))
3768 for (r
= 0; r
>= 0 && tab
&& !tab
->empty
; r
= restore_lexmin(tab
)) {
3771 enum isl_tab_row_sign sgn
;
3775 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
3776 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
3778 sgn
= row_sign(tab
, sol
, row
);
3781 tab
->row_sign
[row
] = sgn
;
3782 if (sgn
== isl_tab_row_any
)
3784 if (sgn
== isl_tab_row_any
&& split
== -1)
3786 if (sgn
== isl_tab_row_neg
)
3789 if (row
< tab
->n_row
)
3792 struct isl_vec
*ineq
;
3794 split
= context
->op
->best_split(context
, tab
);
3797 ineq
= get_row_parameter_ineq(tab
, split
);
3801 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
3802 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
3804 if (tab
->row_sign
[row
] == isl_tab_row_any
)
3805 tab
->row_sign
[row
] = isl_tab_row_unknown
;
3807 tab
->row_sign
[split
] = isl_tab_row_pos
;
3809 find_in_pos(sol
, tab
, ineq
->el
);
3810 tab
->row_sign
[split
] = isl_tab_row_neg
;
3812 isl_seq_neg(ineq
->el
, ineq
->el
, ineq
->size
);
3813 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3815 context
->op
->add_ineq(context
, ineq
->el
, 0, 1);
3823 row
= first_non_integer_row(tab
, &flags
);
3826 if (ISL_FL_ISSET(flags
, I_PAR
)) {
3827 if (ISL_FL_ISSET(flags
, I_VAR
)) {
3828 if (isl_tab_mark_empty(tab
) < 0)
3832 row
= add_cut(tab
, row
);
3833 } else if (ISL_FL_ISSET(flags
, I_VAR
)) {
3834 struct isl_vec
*div
;
3835 struct isl_vec
*ineq
;
3837 div
= get_row_split_div(tab
, row
);
3840 d
= context
->op
->get_div(context
, tab
, div
);
3844 ineq
= ineq_for_div(context
->op
->peek_basic_set(context
), d
);
3848 no_sol_in_strict(sol
, tab
, ineq
);
3849 isl_seq_neg(ineq
->el
, ineq
->el
, ineq
->size
);
3850 context
->op
->add_ineq(context
, ineq
->el
, 1, 1);
3852 if (sol
->error
|| !context
->op
->is_ok(context
))
3854 tab
= set_row_cst_to_div(tab
, row
, d
);
3855 if (context
->op
->is_empty(context
))
3858 row
= add_parametric_cut(tab
, row
, context
);
3873 /* Does "sol" contain a pair of partial solutions that could potentially
3876 * We currently only check that "sol" is not in an error state
3877 * and that there are at least two partial solutions of which the final two
3878 * are defined at the same level.
3880 static int sol_has_mergeable_solutions(struct isl_sol
*sol
)
3886 if (!sol
->partial
->next
)
3888 return sol
->partial
->level
== sol
->partial
->next
->level
;
3891 /* Compute the lexicographic minimum of the set represented by the main
3892 * tableau "tab" within the context "sol->context_tab".
3894 * As a preprocessing step, we first transfer all the purely parametric
3895 * equalities from the main tableau to the context tableau, i.e.,
3896 * parameters that have been pivoted to a row.
3897 * These equalities are ignored by the main algorithm, because the
3898 * corresponding rows may not be marked as being non-negative.
3899 * In parts of the context where the added equality does not hold,
3900 * the main tableau is marked as being empty.
3902 * Before we embark on the actual computation, we save a copy
3903 * of the context. When we return, we check if there are any
3904 * partial solutions that can potentially be merged. If so,
3905 * we perform a rollback to the initial state of the context.
3906 * The merging of partial solutions happens inside calls to
3907 * sol_dec_level that are pushed onto the undo stack of the context.
3908 * If there are no partial solutions that can potentially be merged
3909 * then the rollback is skipped as it would just be wasted effort.
3911 static void find_solutions_main(struct isl_sol
*sol
, struct isl_tab
*tab
)
3921 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
3925 if (tab
->row_var
[row
] < 0)
3927 if (tab
->row_var
[row
] >= tab
->n_param
&&
3928 tab
->row_var
[row
] < tab
->n_var
- tab
->n_div
)
3930 if (tab
->row_var
[row
] < tab
->n_param
)
3931 p
= tab
->row_var
[row
];
3933 p
= tab
->row_var
[row
]
3934 + tab
->n_param
- (tab
->n_var
- tab
->n_div
);
3936 eq
= isl_vec_alloc(tab
->mat
->ctx
, 1+tab
->n_param
+tab
->n_div
);
3939 get_row_parameter_line(tab
, row
, eq
->el
);
3940 isl_int_neg(eq
->el
[1 + p
], tab
->mat
->row
[row
][0]);
3941 eq
= isl_vec_normalize(eq
);
3944 no_sol_in_strict(sol
, tab
, eq
);
3946 isl_seq_neg(eq
->el
, eq
->el
, eq
->size
);
3948 no_sol_in_strict(sol
, tab
, eq
);
3949 isl_seq_neg(eq
->el
, eq
->el
, eq
->size
);
3951 sol
->context
->op
->add_eq(sol
->context
, eq
->el
, 1, 1);
3955 if (isl_tab_mark_redundant(tab
, row
) < 0)
3958 if (sol
->context
->op
->is_empty(sol
->context
))
3961 row
= tab
->n_redundant
- 1;
3964 saved
= sol
->context
->op
->save(sol
->context
);
3966 find_solutions(sol
, tab
);
3968 if (sol_has_mergeable_solutions(sol
))
3969 sol
->context
->op
->restore(sol
->context
, saved
);
3971 sol
->context
->op
->discard(saved
);
3982 /* Check if integer division "div" of "dom" also occurs in "bmap".
3983 * If so, return its position within the divs.
3984 * If not, return -1.
3986 static int find_context_div(struct isl_basic_map
*bmap
,
3987 struct isl_basic_set
*dom
, unsigned div
)
3990 unsigned b_dim
= isl_space_dim(bmap
->dim
, isl_dim_all
);
3991 unsigned d_dim
= isl_space_dim(dom
->dim
, isl_dim_all
);
3993 if (isl_int_is_zero(dom
->div
[div
][0]))
3995 if (isl_seq_first_non_zero(dom
->div
[div
] + 2 + d_dim
, dom
->n_div
) != -1)
3998 for (i
= 0; i
< bmap
->n_div
; ++i
) {
3999 if (isl_int_is_zero(bmap
->div
[i
][0]))
4001 if (isl_seq_first_non_zero(bmap
->div
[i
] + 2 + d_dim
,
4002 (b_dim
- d_dim
) + bmap
->n_div
) != -1)
4004 if (isl_seq_eq(bmap
->div
[i
], dom
->div
[div
], 2 + d_dim
))
4010 /* The correspondence between the variables in the main tableau,
4011 * the context tableau, and the input map and domain is as follows.
4012 * The first n_param and the last n_div variables of the main tableau
4013 * form the variables of the context tableau.
4014 * In the basic map, these n_param variables correspond to the
4015 * parameters and the input dimensions. In the domain, they correspond
4016 * to the parameters and the set dimensions.
4017 * The n_div variables correspond to the integer divisions in the domain.
4018 * To ensure that everything lines up, we may need to copy some of the
4019 * integer divisions of the domain to the map. These have to be placed
4020 * in the same order as those in the context and they have to be placed
4021 * after any other integer divisions that the map may have.
4022 * This function performs the required reordering.
4024 static struct isl_basic_map
*align_context_divs(struct isl_basic_map
*bmap
,
4025 struct isl_basic_set
*dom
)
4031 for (i
= 0; i
< dom
->n_div
; ++i
)
4032 if (find_context_div(bmap
, dom
, i
) != -1)
4034 other
= bmap
->n_div
- common
;
4035 if (dom
->n_div
- common
> 0) {
4036 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
4037 dom
->n_div
- common
, 0, 0);
4041 for (i
= 0; i
< dom
->n_div
; ++i
) {
4042 int pos
= find_context_div(bmap
, dom
, i
);
4044 pos
= isl_basic_map_alloc_div(bmap
);
4047 isl_int_set_si(bmap
->div
[pos
][0], 0);
4049 if (pos
!= other
+ i
)
4050 isl_basic_map_swap_div(bmap
, pos
, other
+ i
);
4054 isl_basic_map_free(bmap
);
4058 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
4059 * some obvious symmetries.
4061 * We make sure the divs in the domain are properly ordered,
4062 * because they will be added one by one in the given order
4063 * during the construction of the solution map.
4065 static struct isl_sol
*basic_map_partial_lexopt_base(
4066 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
4067 __isl_give isl_set
**empty
, int max
,
4068 struct isl_sol
*(*init
)(__isl_keep isl_basic_map
*bmap
,
4069 __isl_take isl_basic_set
*dom
, int track_empty
, int max
))
4071 struct isl_tab
*tab
;
4072 struct isl_sol
*sol
= NULL
;
4073 struct isl_context
*context
;
4076 dom
= isl_basic_set_order_divs(dom
);
4077 bmap
= align_context_divs(bmap
, dom
);
4079 sol
= init(bmap
, dom
, !!empty
, max
);
4083 context
= sol
->context
;
4084 if (isl_basic_set_plain_is_empty(context
->op
->peek_basic_set(context
)))
4086 else if (isl_basic_map_plain_is_empty(bmap
)) {
4089 isl_basic_set_copy(context
->op
->peek_basic_set(context
)));
4091 tab
= tab_for_lexmin(bmap
,
4092 context
->op
->peek_basic_set(context
), 1, max
);
4093 tab
= context
->op
->detect_nonnegative_parameters(context
, tab
);
4094 find_solutions_main(sol
, tab
);
4099 isl_basic_map_free(bmap
);
4103 isl_basic_map_free(bmap
);
4107 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
4108 * some obvious symmetries.
4110 * We call basic_map_partial_lexopt_base and extract the results.
4112 static __isl_give isl_map
*basic_map_partial_lexopt_base_map(
4113 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
4114 __isl_give isl_set
**empty
, int max
)
4116 isl_map
*result
= NULL
;
4117 struct isl_sol
*sol
;
4118 struct isl_sol_map
*sol_map
;
4120 sol
= basic_map_partial_lexopt_base(bmap
, dom
, empty
, max
,
4124 sol_map
= (struct isl_sol_map
*) sol
;
4126 result
= isl_map_copy(sol_map
->map
);
4128 *empty
= isl_set_copy(sol_map
->empty
);
4129 sol_free(&sol_map
->sol
);
4133 /* Structure used during detection of parallel constraints.
4134 * n_in: number of "input" variables: isl_dim_param + isl_dim_in
4135 * n_out: number of "output" variables: isl_dim_out + isl_dim_div
4136 * val: the coefficients of the output variables
4138 struct isl_constraint_equal_info
{
4139 isl_basic_map
*bmap
;
4145 /* Check whether the coefficients of the output variables
4146 * of the constraint in "entry" are equal to info->val.
4148 static int constraint_equal(const void *entry
, const void *val
)
4150 isl_int
**row
= (isl_int
**)entry
;
4151 const struct isl_constraint_equal_info
*info
= val
;
4153 return isl_seq_eq((*row
) + 1 + info
->n_in
, info
->val
, info
->n_out
);
4156 /* Check whether "bmap" has a pair of constraints that have
4157 * the same coefficients for the output variables.
4158 * Note that the coefficients of the existentially quantified
4159 * variables need to be zero since the existentially quantified
4160 * of the result are usually not the same as those of the input.
4161 * the isl_dim_out and isl_dim_div dimensions.
4162 * If so, return 1 and return the row indices of the two constraints
4163 * in *first and *second.
4165 static int parallel_constraints(__isl_keep isl_basic_map
*bmap
,
4166 int *first
, int *second
)
4169 isl_ctx
*ctx
= isl_basic_map_get_ctx(bmap
);
4170 struct isl_hash_table
*table
= NULL
;
4171 struct isl_hash_table_entry
*entry
;
4172 struct isl_constraint_equal_info info
;
4176 ctx
= isl_basic_map_get_ctx(bmap
);
4177 table
= isl_hash_table_alloc(ctx
, bmap
->n_ineq
);
4181 info
.n_in
= isl_basic_map_dim(bmap
, isl_dim_param
) +
4182 isl_basic_map_dim(bmap
, isl_dim_in
);
4184 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4185 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4186 info
.n_out
= n_out
+ n_div
;
4187 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
4190 info
.val
= bmap
->ineq
[i
] + 1 + info
.n_in
;
4191 if (isl_seq_first_non_zero(info
.val
, n_out
) < 0)
4193 if (isl_seq_first_non_zero(info
.val
+ n_out
, n_div
) >= 0)
4195 hash
= isl_seq_get_hash(info
.val
, info
.n_out
);
4196 entry
= isl_hash_table_find(ctx
, table
, hash
,
4197 constraint_equal
, &info
, 1);
4202 entry
->data
= &bmap
->ineq
[i
];
4205 if (i
< bmap
->n_ineq
) {
4206 *first
= ((isl_int
**)entry
->data
) - bmap
->ineq
;
4210 isl_hash_table_free(ctx
, table
);
4212 return i
< bmap
->n_ineq
;
4214 isl_hash_table_free(ctx
, table
);
4218 /* Given a set of upper bounds in "var", add constraints to "bset"
4219 * that make the i-th bound smallest.
4221 * In particular, if there are n bounds b_i, then add the constraints
4223 * b_i <= b_j for j > i
4224 * b_i < b_j for j < i
4226 static __isl_give isl_basic_set
*select_minimum(__isl_take isl_basic_set
*bset
,
4227 __isl_keep isl_mat
*var
, int i
)
4232 ctx
= isl_mat_get_ctx(var
);
4234 for (j
= 0; j
< var
->n_row
; ++j
) {
4237 k
= isl_basic_set_alloc_inequality(bset
);
4240 isl_seq_combine(bset
->ineq
[k
], ctx
->one
, var
->row
[j
],
4241 ctx
->negone
, var
->row
[i
], var
->n_col
);
4242 isl_int_set_si(bset
->ineq
[k
][var
->n_col
], 0);
4244 isl_int_sub_ui(bset
->ineq
[k
][0], bset
->ineq
[k
][0], 1);
4247 bset
= isl_basic_set_finalize(bset
);
4251 isl_basic_set_free(bset
);
4255 /* Given a set of upper bounds on the last "input" variable m,
4256 * construct a set that assigns the minimal upper bound to m, i.e.,
4257 * construct a set that divides the space into cells where one
4258 * of the upper bounds is smaller than all the others and assign
4259 * this upper bound to m.
4261 * In particular, if there are n bounds b_i, then the result
4262 * consists of n basic sets, each one of the form
4265 * b_i <= b_j for j > i
4266 * b_i < b_j for j < i
4268 static __isl_give isl_set
*set_minimum(__isl_take isl_space
*dim
,
4269 __isl_take isl_mat
*var
)
4272 isl_basic_set
*bset
= NULL
;
4274 isl_set
*set
= NULL
;
4279 ctx
= isl_space_get_ctx(dim
);
4280 set
= isl_set_alloc_space(isl_space_copy(dim
),
4281 var
->n_row
, ISL_SET_DISJOINT
);
4283 for (i
= 0; i
< var
->n_row
; ++i
) {
4284 bset
= isl_basic_set_alloc_space(isl_space_copy(dim
), 0,
4286 k
= isl_basic_set_alloc_equality(bset
);
4289 isl_seq_cpy(bset
->eq
[k
], var
->row
[i
], var
->n_col
);
4290 isl_int_set_si(bset
->eq
[k
][var
->n_col
], -1);
4291 bset
= select_minimum(bset
, var
, i
);
4292 set
= isl_set_add_basic_set(set
, bset
);
4295 isl_space_free(dim
);
4299 isl_basic_set_free(bset
);
4301 isl_space_free(dim
);
4306 /* Given that the last input variable of "bmap" represents the minimum
4307 * of the bounds in "cst", check whether we need to split the domain
4308 * based on which bound attains the minimum.
4310 * A split is needed when the minimum appears in an integer division
4311 * or in an equality. Otherwise, it is only needed if it appears in
4312 * an upper bound that is different from the upper bounds on which it
4315 static int need_split_basic_map(__isl_keep isl_basic_map
*bmap
,
4316 __isl_keep isl_mat
*cst
)
4322 pos
= cst
->n_col
- 1;
4323 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4325 for (i
= 0; i
< bmap
->n_div
; ++i
)
4326 if (!isl_int_is_zero(bmap
->div
[i
][2 + pos
]))
4329 for (i
= 0; i
< bmap
->n_eq
; ++i
)
4330 if (!isl_int_is_zero(bmap
->eq
[i
][1 + pos
]))
4333 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
4334 if (isl_int_is_nonneg(bmap
->ineq
[i
][1 + pos
]))
4336 if (!isl_int_is_negone(bmap
->ineq
[i
][1 + pos
]))
4338 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + pos
+ 1,
4339 total
- pos
- 1) >= 0)
4342 for (j
= 0; j
< cst
->n_row
; ++j
)
4343 if (isl_seq_eq(bmap
->ineq
[i
], cst
->row
[j
], cst
->n_col
))
4345 if (j
>= cst
->n_row
)
4352 /* Given that the last set variable of "bset" represents the minimum
4353 * of the bounds in "cst", check whether we need to split the domain
4354 * based on which bound attains the minimum.
4356 * We simply call need_split_basic_map here. This is safe because
4357 * the position of the minimum is computed from "cst" and not
4360 static int need_split_basic_set(__isl_keep isl_basic_set
*bset
,
4361 __isl_keep isl_mat
*cst
)
4363 return need_split_basic_map((isl_basic_map
*)bset
, cst
);
4366 /* Given that the last set variable of "set" represents the minimum
4367 * of the bounds in "cst", check whether we need to split the domain
4368 * based on which bound attains the minimum.
4370 static int need_split_set(__isl_keep isl_set
*set
, __isl_keep isl_mat
*cst
)
4374 for (i
= 0; i
< set
->n
; ++i
)
4375 if (need_split_basic_set(set
->p
[i
], cst
))
4381 /* Given a set of which the last set variable is the minimum
4382 * of the bounds in "cst", split each basic set in the set
4383 * in pieces where one of the bounds is (strictly) smaller than the others.
4384 * This subdivision is given in "min_expr".
4385 * The variable is subsequently projected out.
4387 * We only do the split when it is needed.
4388 * For example if the last input variable m = min(a,b) and the only
4389 * constraints in the given basic set are lower bounds on m,
4390 * i.e., l <= m = min(a,b), then we can simply project out m
4391 * to obtain l <= a and l <= b, without having to split on whether
4392 * m is equal to a or b.
4394 static __isl_give isl_set
*split(__isl_take isl_set
*empty
,
4395 __isl_take isl_set
*min_expr
, __isl_take isl_mat
*cst
)
4402 if (!empty
|| !min_expr
|| !cst
)
4405 n_in
= isl_set_dim(empty
, isl_dim_set
);
4406 dim
= isl_set_get_space(empty
);
4407 dim
= isl_space_drop_dims(dim
, isl_dim_set
, n_in
- 1, 1);
4408 res
= isl_set_empty(dim
);
4410 for (i
= 0; i
< empty
->n
; ++i
) {
4413 set
= isl_set_from_basic_set(isl_basic_set_copy(empty
->p
[i
]));
4414 if (need_split_basic_set(empty
->p
[i
], cst
))
4415 set
= isl_set_intersect(set
, isl_set_copy(min_expr
));
4416 set
= isl_set_remove_dims(set
, isl_dim_set
, n_in
- 1, 1);
4418 res
= isl_set_union_disjoint(res
, set
);
4421 isl_set_free(empty
);
4422 isl_set_free(min_expr
);
4426 isl_set_free(empty
);
4427 isl_set_free(min_expr
);
4432 /* Given a map of which the last input variable is the minimum
4433 * of the bounds in "cst", split each basic set in the set
4434 * in pieces where one of the bounds is (strictly) smaller than the others.
4435 * This subdivision is given in "min_expr".
4436 * The variable is subsequently projected out.
4438 * The implementation is essentially the same as that of "split".
4440 static __isl_give isl_map
*split_domain(__isl_take isl_map
*opt
,
4441 __isl_take isl_set
*min_expr
, __isl_take isl_mat
*cst
)
4448 if (!opt
|| !min_expr
|| !cst
)
4451 n_in
= isl_map_dim(opt
, isl_dim_in
);
4452 dim
= isl_map_get_space(opt
);
4453 dim
= isl_space_drop_dims(dim
, isl_dim_in
, n_in
- 1, 1);
4454 res
= isl_map_empty(dim
);
4456 for (i
= 0; i
< opt
->n
; ++i
) {
4459 map
= isl_map_from_basic_map(isl_basic_map_copy(opt
->p
[i
]));
4460 if (need_split_basic_map(opt
->p
[i
], cst
))
4461 map
= isl_map_intersect_domain(map
,
4462 isl_set_copy(min_expr
));
4463 map
= isl_map_remove_dims(map
, isl_dim_in
, n_in
- 1, 1);
4465 res
= isl_map_union_disjoint(res
, map
);
4469 isl_set_free(min_expr
);
4474 isl_set_free(min_expr
);
4479 static __isl_give isl_map
*basic_map_partial_lexopt(
4480 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
4481 __isl_give isl_set
**empty
, int max
);
4486 isl_pw_multi_aff
*pma
;
4489 /* This function is called from basic_map_partial_lexopt_symm.
4490 * The last variable of "bmap" and "dom" corresponds to the minimum
4491 * of the bounds in "cst". "map_space" is the space of the original
4492 * input relation (of basic_map_partial_lexopt_symm) and "set_space"
4493 * is the space of the original domain.
4495 * We recursively call basic_map_partial_lexopt and then plug in
4496 * the definition of the minimum in the result.
4498 static __isl_give
union isl_lex_res
basic_map_partial_lexopt_symm_map_core(
4499 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
4500 __isl_give isl_set
**empty
, int max
, __isl_take isl_mat
*cst
,
4501 __isl_take isl_space
*map_space
, __isl_take isl_space
*set_space
)
4505 union isl_lex_res res
;
4507 min_expr
= set_minimum(isl_basic_set_get_space(dom
), isl_mat_copy(cst
));
4509 opt
= basic_map_partial_lexopt(bmap
, dom
, empty
, max
);
4512 *empty
= split(*empty
,
4513 isl_set_copy(min_expr
), isl_mat_copy(cst
));
4514 *empty
= isl_set_reset_space(*empty
, set_space
);
4517 opt
= split_domain(opt
, min_expr
, cst
);
4518 opt
= isl_map_reset_space(opt
, map_space
);
4524 /* Given a basic map with at least two parallel constraints (as found
4525 * by the function parallel_constraints), first look for more constraints
4526 * parallel to the two constraint and replace the found list of parallel
4527 * constraints by a single constraint with as "input" part the minimum
4528 * of the input parts of the list of constraints. Then, recursively call
4529 * basic_map_partial_lexopt (possibly finding more parallel constraints)
4530 * and plug in the definition of the minimum in the result.
4532 * More specifically, given a set of constraints
4536 * Replace this set by a single constraint
4540 * with u a new parameter with constraints
4544 * Any solution to the new system is also a solution for the original system
4547 * a x >= -u >= -b_i(p)
4549 * Moreover, m = min_i(b_i(p)) satisfies the constraints on u and can
4550 * therefore be plugged into the solution.
4552 static union isl_lex_res
basic_map_partial_lexopt_symm(
4553 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
4554 __isl_give isl_set
**empty
, int max
, int first
, int second
,
4555 __isl_give
union isl_lex_res (*core
)(__isl_take isl_basic_map
*bmap
,
4556 __isl_take isl_basic_set
*dom
,
4557 __isl_give isl_set
**empty
,
4558 int max
, __isl_take isl_mat
*cst
,
4559 __isl_take isl_space
*map_space
,
4560 __isl_take isl_space
*set_space
))
4564 unsigned n_in
, n_out
, n_div
;
4566 isl_vec
*var
= NULL
;
4567 isl_mat
*cst
= NULL
;
4568 isl_space
*map_space
, *set_space
;
4569 union isl_lex_res res
;
4571 map_space
= isl_basic_map_get_space(bmap
);
4572 set_space
= empty
? isl_basic_set_get_space(dom
) : NULL
;
4574 n_in
= isl_basic_map_dim(bmap
, isl_dim_param
) +
4575 isl_basic_map_dim(bmap
, isl_dim_in
);
4576 n_out
= isl_basic_map_dim(bmap
, isl_dim_all
) - n_in
;
4578 ctx
= isl_basic_map_get_ctx(bmap
);
4579 list
= isl_alloc_array(ctx
, int, bmap
->n_ineq
);
4580 var
= isl_vec_alloc(ctx
, n_out
);
4586 isl_seq_cpy(var
->el
, bmap
->ineq
[first
] + 1 + n_in
, n_out
);
4587 for (i
= second
+ 1, n
= 2; i
< bmap
->n_ineq
; ++i
) {
4588 if (isl_seq_eq(var
->el
, bmap
->ineq
[i
] + 1 + n_in
, n_out
))
4592 cst
= isl_mat_alloc(ctx
, n
, 1 + n_in
);
4596 for (i
= 0; i
< n
; ++i
)
4597 isl_seq_cpy(cst
->row
[i
], bmap
->ineq
[list
[i
]], 1 + n_in
);
4599 bmap
= isl_basic_map_cow(bmap
);
4602 for (i
= n
- 1; i
>= 0; --i
)
4603 if (isl_basic_map_drop_inequality(bmap
, list
[i
]) < 0)
4606 bmap
= isl_basic_map_add(bmap
, isl_dim_in
, 1);
4607 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
4608 k
= isl_basic_map_alloc_inequality(bmap
);
4611 isl_seq_clr(bmap
->ineq
[k
], 1 + n_in
);
4612 isl_int_set_si(bmap
->ineq
[k
][1 + n_in
], 1);
4613 isl_seq_cpy(bmap
->ineq
[k
] + 1 + n_in
+ 1, var
->el
, n_out
);
4614 bmap
= isl_basic_map_finalize(bmap
);
4616 n_div
= isl_basic_set_dim(dom
, isl_dim_div
);
4617 dom
= isl_basic_set_add_dims(dom
, isl_dim_set
, 1);
4618 dom
= isl_basic_set_extend_constraints(dom
, 0, n
);
4619 for (i
= 0; i
< n
; ++i
) {
4620 k
= isl_basic_set_alloc_inequality(dom
);
4623 isl_seq_cpy(dom
->ineq
[k
], cst
->row
[i
], 1 + n_in
);
4624 isl_int_set_si(dom
->ineq
[k
][1 + n_in
], -1);
4625 isl_seq_clr(dom
->ineq
[k
] + 1 + n_in
+ 1, n_div
);
4631 return core(bmap
, dom
, empty
, max
, cst
, map_space
, set_space
);
4633 isl_space_free(map_space
);
4634 isl_space_free(set_space
);
4638 isl_basic_set_free(dom
);
4639 isl_basic_map_free(bmap
);
4644 static __isl_give isl_map
*basic_map_partial_lexopt_symm_map(
4645 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
4646 __isl_give isl_set
**empty
, int max
, int first
, int second
)
4648 return basic_map_partial_lexopt_symm(bmap
, dom
, empty
, max
,
4649 first
, second
, &basic_map_partial_lexopt_symm_map_core
).map
;
4652 /* Recursive part of isl_tab_basic_map_partial_lexopt, after detecting
4653 * equalities and removing redundant constraints.
4655 * We first check if there are any parallel constraints (left).
4656 * If not, we are in the base case.
4657 * If there are parallel constraints, we replace them by a single
4658 * constraint in basic_map_partial_lexopt_symm and then call
4659 * this function recursively to look for more parallel constraints.
4661 static __isl_give isl_map
*basic_map_partial_lexopt(
4662 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
4663 __isl_give isl_set
**empty
, int max
)
4671 if (bmap
->ctx
->opt
->pip_symmetry
)
4672 par
= parallel_constraints(bmap
, &first
, &second
);
4676 return basic_map_partial_lexopt_base_map(bmap
, dom
, empty
, max
);
4678 return basic_map_partial_lexopt_symm_map(bmap
, dom
, empty
, max
,
4681 isl_basic_set_free(dom
);
4682 isl_basic_map_free(bmap
);
4686 /* Compute the lexicographic minimum (or maximum if "max" is set)
4687 * of "bmap" over the domain "dom" and return the result as a map.
4688 * If "empty" is not NULL, then *empty is assigned a set that
4689 * contains those parts of the domain where there is no solution.
4690 * If "bmap" is marked as rational (ISL_BASIC_MAP_RATIONAL),
4691 * then we compute the rational optimum. Otherwise, we compute
4692 * the integral optimum.
4694 * We perform some preprocessing. As the PILP solver does not
4695 * handle implicit equalities very well, we first make sure all
4696 * the equalities are explicitly available.
4698 * We also add context constraints to the basic map and remove
4699 * redundant constraints. This is only needed because of the
4700 * way we handle simple symmetries. In particular, we currently look
4701 * for symmetries on the constraints, before we set up the main tableau.
4702 * It is then no good to look for symmetries on possibly redundant constraints.
4704 struct isl_map
*isl_tab_basic_map_partial_lexopt(
4705 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
4706 struct isl_set
**empty
, int max
)
4713 isl_assert(bmap
->ctx
,
4714 isl_basic_map_compatible_domain(bmap
, dom
), goto error
);
4716 if (isl_basic_set_dim(dom
, isl_dim_all
) == 0)
4717 return basic_map_partial_lexopt(bmap
, dom
, empty
, max
);
4719 bmap
= isl_basic_map_intersect_domain(bmap
, isl_basic_set_copy(dom
));
4720 bmap
= isl_basic_map_detect_equalities(bmap
);
4721 bmap
= isl_basic_map_remove_redundancies(bmap
);
4723 return basic_map_partial_lexopt(bmap
, dom
, empty
, max
);
4725 isl_basic_set_free(dom
);
4726 isl_basic_map_free(bmap
);
4730 struct isl_sol_for
{
4732 int (*fn
)(__isl_take isl_basic_set
*dom
,
4733 __isl_take isl_aff_list
*list
, void *user
);
4737 static void sol_for_free(struct isl_sol_for
*sol_for
)
4739 if (sol_for
->sol
.context
)
4740 sol_for
->sol
.context
->op
->free(sol_for
->sol
.context
);
4744 static void sol_for_free_wrap(struct isl_sol
*sol
)
4746 sol_for_free((struct isl_sol_for
*)sol
);
4749 /* Add the solution identified by the tableau and the context tableau.
4751 * See documentation of sol_add for more details.
4753 * Instead of constructing a basic map, this function calls a user
4754 * defined function with the current context as a basic set and
4755 * a list of affine expressions representing the relation between
4756 * the input and output. The space over which the affine expressions
4757 * are defined is the same as that of the domain. The number of
4758 * affine expressions in the list is equal to the number of output variables.
4760 static void sol_for_add(struct isl_sol_for
*sol
,
4761 struct isl_basic_set
*dom
, struct isl_mat
*M
)
4765 isl_local_space
*ls
;
4769 if (sol
->sol
.error
|| !dom
|| !M
)
4772 ctx
= isl_basic_set_get_ctx(dom
);
4773 ls
= isl_basic_set_get_local_space(dom
);
4774 list
= isl_aff_list_alloc(ctx
, M
->n_row
- 1);
4775 for (i
= 1; i
< M
->n_row
; ++i
) {
4776 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
4778 isl_int_set(aff
->v
->el
[0], M
->row
[0][0]);
4779 isl_seq_cpy(aff
->v
->el
+ 1, M
->row
[i
], M
->n_col
);
4781 aff
= isl_aff_normalize(aff
);
4782 list
= isl_aff_list_add(list
, aff
);
4784 isl_local_space_free(ls
);
4786 dom
= isl_basic_set_finalize(dom
);
4788 if (sol
->fn(isl_basic_set_copy(dom
), list
, sol
->user
) < 0)
4791 isl_basic_set_free(dom
);
4795 isl_basic_set_free(dom
);
4800 static void sol_for_add_wrap(struct isl_sol
*sol
,
4801 struct isl_basic_set
*dom
, struct isl_mat
*M
)
4803 sol_for_add((struct isl_sol_for
*)sol
, dom
, M
);
4806 static struct isl_sol_for
*sol_for_init(struct isl_basic_map
*bmap
, int max
,
4807 int (*fn
)(__isl_take isl_basic_set
*dom
, __isl_take isl_aff_list
*list
,
4811 struct isl_sol_for
*sol_for
= NULL
;
4813 struct isl_basic_set
*dom
= NULL
;
4815 sol_for
= isl_calloc_type(bmap
->ctx
, struct isl_sol_for
);
4819 dom_dim
= isl_space_domain(isl_space_copy(bmap
->dim
));
4820 dom
= isl_basic_set_universe(dom_dim
);
4822 sol_for
->sol
.rational
= ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
4823 sol_for
->sol
.dec_level
.callback
.run
= &sol_dec_level_wrap
;
4824 sol_for
->sol
.dec_level
.sol
= &sol_for
->sol
;
4826 sol_for
->user
= user
;
4827 sol_for
->sol
.max
= max
;
4828 sol_for
->sol
.n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4829 sol_for
->sol
.add
= &sol_for_add_wrap
;
4830 sol_for
->sol
.add_empty
= NULL
;
4831 sol_for
->sol
.free
= &sol_for_free_wrap
;
4833 sol_for
->sol
.context
= isl_context_alloc(dom
);
4834 if (!sol_for
->sol
.context
)
4837 isl_basic_set_free(dom
);
4840 isl_basic_set_free(dom
);
4841 sol_for_free(sol_for
);
4845 static void sol_for_find_solutions(struct isl_sol_for
*sol_for
,
4846 struct isl_tab
*tab
)
4848 find_solutions_main(&sol_for
->sol
, tab
);
4851 int isl_basic_map_foreach_lexopt(__isl_keep isl_basic_map
*bmap
, int max
,
4852 int (*fn
)(__isl_take isl_basic_set
*dom
, __isl_take isl_aff_list
*list
,
4856 struct isl_sol_for
*sol_for
= NULL
;
4858 bmap
= isl_basic_map_copy(bmap
);
4859 bmap
= isl_basic_map_detect_equalities(bmap
);
4863 sol_for
= sol_for_init(bmap
, max
, fn
, user
);
4867 if (isl_basic_map_plain_is_empty(bmap
))
4870 struct isl_tab
*tab
;
4871 struct isl_context
*context
= sol_for
->sol
.context
;
4872 tab
= tab_for_lexmin(bmap
,
4873 context
->op
->peek_basic_set(context
), 1, max
);
4874 tab
= context
->op
->detect_nonnegative_parameters(context
, tab
);
4875 sol_for_find_solutions(sol_for
, tab
);
4876 if (sol_for
->sol
.error
)
4880 sol_free(&sol_for
->sol
);
4881 isl_basic_map_free(bmap
);
4884 sol_free(&sol_for
->sol
);
4885 isl_basic_map_free(bmap
);
4889 int isl_basic_set_foreach_lexopt(__isl_keep isl_basic_set
*bset
, int max
,
4890 int (*fn
)(__isl_take isl_basic_set
*dom
, __isl_take isl_aff_list
*list
,
4894 return isl_basic_map_foreach_lexopt(bset
, max
, fn
, user
);
4897 /* Check if the given sequence of len variables starting at pos
4898 * represents a trivial (i.e., zero) solution.
4899 * The variables are assumed to be non-negative and to come in pairs,
4900 * with each pair representing a variable of unrestricted sign.
4901 * The solution is trivial if each such pair in the sequence consists
4902 * of two identical values, meaning that the variable being represented
4905 static int region_is_trivial(struct isl_tab
*tab
, int pos
, int len
)
4912 for (i
= 0; i
< len
; i
+= 2) {
4916 neg_row
= tab
->var
[pos
+ i
].is_row
?
4917 tab
->var
[pos
+ i
].index
: -1;
4918 pos_row
= tab
->var
[pos
+ i
+ 1].is_row
?
4919 tab
->var
[pos
+ i
+ 1].index
: -1;
4922 isl_int_is_zero(tab
->mat
->row
[neg_row
][1])) &&
4924 isl_int_is_zero(tab
->mat
->row
[pos_row
][1])))
4927 if (neg_row
< 0 || pos_row
< 0)
4929 if (isl_int_ne(tab
->mat
->row
[neg_row
][1],
4930 tab
->mat
->row
[pos_row
][1]))
4937 /* Return the index of the first trivial region or -1 if all regions
4940 static int first_trivial_region(struct isl_tab
*tab
,
4941 int n_region
, struct isl_region
*region
)
4945 for (i
= 0; i
< n_region
; ++i
) {
4946 if (region_is_trivial(tab
, region
[i
].pos
, region
[i
].len
))
4953 /* Check if the solution is optimal, i.e., whether the first
4954 * n_op entries are zero.
4956 static int is_optimal(__isl_keep isl_vec
*sol
, int n_op
)
4960 for (i
= 0; i
< n_op
; ++i
)
4961 if (!isl_int_is_zero(sol
->el
[1 + i
]))
4966 /* Add constraints to "tab" that ensure that any solution is significantly
4967 * better that that represented by "sol". That is, find the first
4968 * relevant (within first n_op) non-zero coefficient and force it (along
4969 * with all previous coefficients) to be zero.
4970 * If the solution is already optimal (all relevant coefficients are zero),
4971 * then just mark the table as empty.
4973 static int force_better_solution(struct isl_tab
*tab
,
4974 __isl_keep isl_vec
*sol
, int n_op
)
4983 for (i
= 0; i
< n_op
; ++i
)
4984 if (!isl_int_is_zero(sol
->el
[1 + i
]))
4988 if (isl_tab_mark_empty(tab
) < 0)
4993 ctx
= isl_vec_get_ctx(sol
);
4994 v
= isl_vec_alloc(ctx
, 1 + tab
->n_var
);
4998 for (; i
>= 0; --i
) {
5000 isl_int_set_si(v
->el
[1 + i
], -1);
5001 if (add_lexmin_eq(tab
, v
->el
) < 0)
5012 struct isl_trivial
{
5016 struct isl_tab_undo
*snap
;
5019 /* Return the lexicographically smallest non-trivial solution of the
5020 * given ILP problem.
5022 * All variables are assumed to be non-negative.
5024 * n_op is the number of initial coordinates to optimize.
5025 * That is, once a solution has been found, we will only continue looking
5026 * for solution that result in significantly better values for those
5027 * initial coordinates. That is, we only continue looking for solutions
5028 * that increase the number of initial zeros in this sequence.
5030 * A solution is non-trivial, if it is non-trivial on each of the
5031 * specified regions. Each region represents a sequence of pairs
5032 * of variables. A solution is non-trivial on such a region if
5033 * at least one of these pairs consists of different values, i.e.,
5034 * such that the non-negative variable represented by the pair is non-zero.
5036 * Whenever a conflict is encountered, all constraints involved are
5037 * reported to the caller through a call to "conflict".
5039 * We perform a simple branch-and-bound backtracking search.
5040 * Each level in the search represents initially trivial region that is forced
5041 * to be non-trivial.
5042 * At each level we consider n cases, where n is the length of the region.
5043 * In terms of the n/2 variables of unrestricted signs being encoded by
5044 * the region, we consider the cases
5047 * x_0 = 0 and x_1 >= 1
5048 * x_0 = 0 and x_1 <= -1
5049 * x_0 = 0 and x_1 = 0 and x_2 >= 1
5050 * x_0 = 0 and x_1 = 0 and x_2 <= -1
5052 * The cases are considered in this order, assuming that each pair
5053 * x_i_a x_i_b represents the value x_i_b - x_i_a.
5054 * That is, x_0 >= 1 is enforced by adding the constraint
5055 * x_0_b - x_0_a >= 1
5057 __isl_give isl_vec
*isl_tab_basic_set_non_trivial_lexmin(
5058 __isl_take isl_basic_set
*bset
, int n_op
, int n_region
,
5059 struct isl_region
*region
,
5060 int (*conflict
)(int con
, void *user
), void *user
)
5066 isl_vec
*sol
= NULL
;
5067 struct isl_tab
*tab
;
5068 struct isl_trivial
*triv
= NULL
;
5074 ctx
= isl_basic_set_get_ctx(bset
);
5075 sol
= isl_vec_alloc(ctx
, 0);
5077 tab
= tab_for_lexmin(bset
, NULL
, 0, 0);
5080 tab
->conflict
= conflict
;
5081 tab
->conflict_user
= user
;
5083 v
= isl_vec_alloc(ctx
, 1 + tab
->n_var
);
5084 triv
= isl_calloc_array(ctx
, struct isl_trivial
, n_region
);
5091 while (level
>= 0) {
5095 tab
= cut_to_integer_lexmin(tab
, CUT_ONE
);
5100 r
= first_trivial_region(tab
, n_region
, region
);
5102 for (i
= 0; i
< level
; ++i
)
5105 sol
= isl_tab_get_sample_value(tab
);
5108 if (is_optimal(sol
, n_op
))
5112 if (level
>= n_region
)
5113 isl_die(ctx
, isl_error_internal
,
5114 "nesting level too deep", goto error
);
5115 if (isl_tab_extend_cons(tab
,
5116 2 * region
[r
].len
+ 2 * n_op
) < 0)
5118 triv
[level
].region
= r
;
5119 triv
[level
].side
= 0;
5122 r
= triv
[level
].region
;
5123 side
= triv
[level
].side
;
5124 base
= 2 * (side
/2);
5126 if (side
>= region
[r
].len
) {
5131 if (isl_tab_rollback(tab
, triv
[level
].snap
) < 0)
5136 if (triv
[level
].update
) {
5137 if (force_better_solution(tab
, sol
, n_op
) < 0)
5139 triv
[level
].update
= 0;
5142 if (side
== base
&& base
>= 2) {
5143 for (j
= base
- 2; j
< base
; ++j
) {
5145 isl_int_set_si(v
->el
[1 + region
[r
].pos
+ j
], 1);
5146 if (add_lexmin_eq(tab
, v
->el
) < 0)
5151 triv
[level
].snap
= isl_tab_snap(tab
);
5152 if (isl_tab_push_basis(tab
) < 0)
5156 isl_int_set_si(v
->el
[0], -1);
5157 isl_int_set_si(v
->el
[1 + region
[r
].pos
+ side
], -1);
5158 isl_int_set_si(v
->el
[1 + region
[r
].pos
+ (side
^ 1)], 1);
5159 tab
= add_lexmin_ineq(tab
, v
->el
);
5169 isl_basic_set_free(bset
);
5176 isl_basic_set_free(bset
);
5181 /* Return the lexicographically smallest rational point in "bset",
5182 * assuming that all variables are non-negative.
5183 * If "bset" is empty, then return a zero-length vector.
5185 __isl_give isl_vec
*isl_tab_basic_set_non_neg_lexmin(
5186 __isl_take isl_basic_set
*bset
)
5188 struct isl_tab
*tab
;
5189 isl_ctx
*ctx
= isl_basic_set_get_ctx(bset
);
5195 tab
= tab_for_lexmin(bset
, NULL
, 0, 0);
5199 sol
= isl_vec_alloc(ctx
, 0);
5201 sol
= isl_tab_get_sample_value(tab
);
5203 isl_basic_set_free(bset
);
5207 isl_basic_set_free(bset
);
5211 struct isl_sol_pma
{
5213 isl_pw_multi_aff
*pma
;
5217 static void sol_pma_free(struct isl_sol_pma
*sol_pma
)
5221 if (sol_pma
->sol
.context
)
5222 sol_pma
->sol
.context
->op
->free(sol_pma
->sol
.context
);
5223 isl_pw_multi_aff_free(sol_pma
->pma
);
5224 isl_set_free(sol_pma
->empty
);
5228 /* This function is called for parts of the context where there is
5229 * no solution, with "bset" corresponding to the context tableau.
5230 * Simply add the basic set to the set "empty".
5232 static void sol_pma_add_empty(struct isl_sol_pma
*sol
,
5233 __isl_take isl_basic_set
*bset
)
5237 isl_assert(bset
->ctx
, sol
->empty
, goto error
);
5239 sol
->empty
= isl_set_grow(sol
->empty
, 1);
5240 bset
= isl_basic_set_simplify(bset
);
5241 bset
= isl_basic_set_finalize(bset
);
5242 sol
->empty
= isl_set_add_basic_set(sol
->empty
, bset
);
5247 isl_basic_set_free(bset
);
5251 /* Given a basic map "dom" that represents the context and an affine
5252 * matrix "M" that maps the dimensions of the context to the
5253 * output variables, construct an isl_pw_multi_aff with a single
5254 * cell corresponding to "dom" and affine expressions copied from "M".
5256 static void sol_pma_add(struct isl_sol_pma
*sol
,
5257 __isl_take isl_basic_set
*dom
, __isl_take isl_mat
*M
)
5260 isl_local_space
*ls
;
5262 isl_multi_aff
*maff
;
5263 isl_pw_multi_aff
*pma
;
5265 maff
= isl_multi_aff_alloc(isl_pw_multi_aff_get_space(sol
->pma
));
5266 ls
= isl_basic_set_get_local_space(dom
);
5267 for (i
= 1; i
< M
->n_row
; ++i
) {
5268 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
5270 isl_int_set(aff
->v
->el
[0], M
->row
[0][0]);
5271 isl_seq_cpy(aff
->v
->el
+ 1, M
->row
[i
], M
->n_col
);
5273 aff
= isl_aff_normalize(aff
);
5274 maff
= isl_multi_aff_set_aff(maff
, i
- 1, aff
);
5276 isl_local_space_free(ls
);
5278 dom
= isl_basic_set_simplify(dom
);
5279 dom
= isl_basic_set_finalize(dom
);
5280 pma
= isl_pw_multi_aff_alloc(isl_set_from_basic_set(dom
), maff
);
5281 sol
->pma
= isl_pw_multi_aff_add_disjoint(sol
->pma
, pma
);
5286 static void sol_pma_free_wrap(struct isl_sol
*sol
)
5288 sol_pma_free((struct isl_sol_pma
*)sol
);
5291 static void sol_pma_add_empty_wrap(struct isl_sol
*sol
,
5292 __isl_take isl_basic_set
*bset
)
5294 sol_pma_add_empty((struct isl_sol_pma
*)sol
, bset
);
5297 static void sol_pma_add_wrap(struct isl_sol
*sol
,
5298 __isl_take isl_basic_set
*dom
, __isl_take isl_mat
*M
)
5300 sol_pma_add((struct isl_sol_pma
*)sol
, dom
, M
);
5303 /* Construct an isl_sol_pma structure for accumulating the solution.
5304 * If track_empty is set, then we also keep track of the parts
5305 * of the context where there is no solution.
5306 * If max is set, then we are solving a maximization, rather than
5307 * a minimization problem, which means that the variables in the
5308 * tableau have value "M - x" rather than "M + x".
5310 static struct isl_sol
*sol_pma_init(__isl_keep isl_basic_map
*bmap
,
5311 __isl_take isl_basic_set
*dom
, int track_empty
, int max
)
5313 struct isl_sol_pma
*sol_pma
= NULL
;
5318 sol_pma
= isl_calloc_type(bmap
->ctx
, struct isl_sol_pma
);
5322 sol_pma
->sol
.rational
= ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
5323 sol_pma
->sol
.dec_level
.callback
.run
= &sol_dec_level_wrap
;
5324 sol_pma
->sol
.dec_level
.sol
= &sol_pma
->sol
;
5325 sol_pma
->sol
.max
= max
;
5326 sol_pma
->sol
.n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5327 sol_pma
->sol
.add
= &sol_pma_add_wrap
;
5328 sol_pma
->sol
.add_empty
= track_empty
? &sol_pma_add_empty_wrap
: NULL
;
5329 sol_pma
->sol
.free
= &sol_pma_free_wrap
;
5330 sol_pma
->pma
= isl_pw_multi_aff_empty(isl_basic_map_get_space(bmap
));
5334 sol_pma
->sol
.context
= isl_context_alloc(dom
);
5335 if (!sol_pma
->sol
.context
)
5339 sol_pma
->empty
= isl_set_alloc_space(isl_basic_set_get_space(dom
),
5340 1, ISL_SET_DISJOINT
);
5341 if (!sol_pma
->empty
)
5345 isl_basic_set_free(dom
);
5346 return &sol_pma
->sol
;
5348 isl_basic_set_free(dom
);
5349 sol_pma_free(sol_pma
);
5353 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
5354 * some obvious symmetries.
5356 * We call basic_map_partial_lexopt_base and extract the results.
5358 static __isl_give isl_pw_multi_aff
*basic_map_partial_lexopt_base_pma(
5359 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5360 __isl_give isl_set
**empty
, int max
)
5362 isl_pw_multi_aff
*result
= NULL
;
5363 struct isl_sol
*sol
;
5364 struct isl_sol_pma
*sol_pma
;
5366 sol
= basic_map_partial_lexopt_base(bmap
, dom
, empty
, max
,
5370 sol_pma
= (struct isl_sol_pma
*) sol
;
5372 result
= isl_pw_multi_aff_copy(sol_pma
->pma
);
5374 *empty
= isl_set_copy(sol_pma
->empty
);
5375 sol_free(&sol_pma
->sol
);
5379 /* Given that the last input variable of "maff" represents the minimum
5380 * of some bounds, check whether we need to plug in the expression
5383 * In particular, check if the last input variable appears in any
5384 * of the expressions in "maff".
5386 static int need_substitution(__isl_keep isl_multi_aff
*maff
)
5391 pos
= isl_multi_aff_dim(maff
, isl_dim_in
) - 1;
5393 for (i
= 0; i
< maff
->n
; ++i
)
5394 if (isl_aff_involves_dims(maff
->p
[i
], isl_dim_in
, pos
, 1))
5400 /* Given a set of upper bounds on the last "input" variable m,
5401 * construct a piecewise affine expression that selects
5402 * the minimal upper bound to m, i.e.,
5403 * divide the space into cells where one
5404 * of the upper bounds is smaller than all the others and select
5405 * this upper bound on that cell.
5407 * In particular, if there are n bounds b_i, then the result
5408 * consists of n cell, each one of the form
5410 * b_i <= b_j for j > i
5411 * b_i < b_j for j < i
5413 * The affine expression on this cell is
5417 static __isl_give isl_pw_aff
*set_minimum_pa(__isl_take isl_space
*space
,
5418 __isl_take isl_mat
*var
)
5421 isl_aff
*aff
= NULL
;
5422 isl_basic_set
*bset
= NULL
;
5424 isl_pw_aff
*paff
= NULL
;
5425 isl_space
*pw_space
;
5426 isl_local_space
*ls
= NULL
;
5431 ctx
= isl_space_get_ctx(space
);
5432 ls
= isl_local_space_from_space(isl_space_copy(space
));
5433 pw_space
= isl_space_copy(space
);
5434 pw_space
= isl_space_from_domain(pw_space
);
5435 pw_space
= isl_space_add_dims(pw_space
, isl_dim_out
, 1);
5436 paff
= isl_pw_aff_alloc_size(pw_space
, var
->n_row
);
5438 for (i
= 0; i
< var
->n_row
; ++i
) {
5441 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
5442 bset
= isl_basic_set_alloc_space(isl_space_copy(space
), 0,
5446 isl_int_set_si(aff
->v
->el
[0], 1);
5447 isl_seq_cpy(aff
->v
->el
+ 1, var
->row
[i
], var
->n_col
);
5448 isl_int_set_si(aff
->v
->el
[1 + var
->n_col
], 0);
5449 bset
= select_minimum(bset
, var
, i
);
5450 paff_i
= isl_pw_aff_alloc(isl_set_from_basic_set(bset
), aff
);
5451 paff
= isl_pw_aff_add_disjoint(paff
, paff_i
);
5454 isl_local_space_free(ls
);
5455 isl_space_free(space
);
5460 isl_basic_set_free(bset
);
5461 isl_pw_aff_free(paff
);
5462 isl_local_space_free(ls
);
5463 isl_space_free(space
);
5468 /* Given a piecewise multi-affine expression of which the last input variable
5469 * is the minimum of the bounds in "cst", plug in the value of the minimum.
5470 * This minimum expression is given in "min_expr_pa".
5471 * The set "min_expr" contains the same information, but in the form of a set.
5472 * The variable is subsequently projected out.
5474 * The implementation is similar to those of "split" and "split_domain".
5475 * If the variable appears in a given expression, then minimum expression
5476 * is plugged in. Otherwise, if the variable appears in the constraints
5477 * and a split is required, then the domain is split. Otherwise, no split
5480 static __isl_give isl_pw_multi_aff
*split_domain_pma(
5481 __isl_take isl_pw_multi_aff
*opt
, __isl_take isl_pw_aff
*min_expr_pa
,
5482 __isl_take isl_set
*min_expr
, __isl_take isl_mat
*cst
)
5487 isl_pw_multi_aff
*res
;
5489 if (!opt
|| !min_expr
|| !cst
)
5492 n_in
= isl_pw_multi_aff_dim(opt
, isl_dim_in
);
5493 space
= isl_pw_multi_aff_get_space(opt
);
5494 space
= isl_space_drop_dims(space
, isl_dim_in
, n_in
- 1, 1);
5495 res
= isl_pw_multi_aff_empty(space
);
5497 for (i
= 0; i
< opt
->n
; ++i
) {
5498 isl_pw_multi_aff
*pma
;
5500 pma
= isl_pw_multi_aff_alloc(isl_set_copy(opt
->p
[i
].set
),
5501 isl_multi_aff_copy(opt
->p
[i
].maff
));
5502 if (need_substitution(opt
->p
[i
].maff
))
5503 pma
= isl_pw_multi_aff_substitute(pma
,
5504 isl_dim_in
, n_in
- 1, min_expr_pa
);
5505 else if (need_split_set(opt
->p
[i
].set
, cst
))
5506 pma
= isl_pw_multi_aff_intersect_domain(pma
,
5507 isl_set_copy(min_expr
));
5508 pma
= isl_pw_multi_aff_project_out(pma
,
5509 isl_dim_in
, n_in
- 1, 1);
5511 res
= isl_pw_multi_aff_add_disjoint(res
, pma
);
5514 isl_pw_multi_aff_free(opt
);
5515 isl_pw_aff_free(min_expr_pa
);
5516 isl_set_free(min_expr
);
5520 isl_pw_multi_aff_free(opt
);
5521 isl_pw_aff_free(min_expr_pa
);
5522 isl_set_free(min_expr
);
5527 static __isl_give isl_pw_multi_aff
*basic_map_partial_lexopt_pma(
5528 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5529 __isl_give isl_set
**empty
, int max
);
5531 /* This function is called from basic_map_partial_lexopt_symm.
5532 * The last variable of "bmap" and "dom" corresponds to the minimum
5533 * of the bounds in "cst". "map_space" is the space of the original
5534 * input relation (of basic_map_partial_lexopt_symm) and "set_space"
5535 * is the space of the original domain.
5537 * We recursively call basic_map_partial_lexopt and then plug in
5538 * the definition of the minimum in the result.
5540 static __isl_give
union isl_lex_res
basic_map_partial_lexopt_symm_pma_core(
5541 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5542 __isl_give isl_set
**empty
, int max
, __isl_take isl_mat
*cst
,
5543 __isl_take isl_space
*map_space
, __isl_take isl_space
*set_space
)
5545 isl_pw_multi_aff
*opt
;
5546 isl_pw_aff
*min_expr_pa
;
5548 union isl_lex_res res
;
5550 min_expr
= set_minimum(isl_basic_set_get_space(dom
), isl_mat_copy(cst
));
5551 min_expr_pa
= set_minimum_pa(isl_basic_set_get_space(dom
),
5554 opt
= basic_map_partial_lexopt_pma(bmap
, dom
, empty
, max
);
5557 *empty
= split(*empty
,
5558 isl_set_copy(min_expr
), isl_mat_copy(cst
));
5559 *empty
= isl_set_reset_space(*empty
, set_space
);
5562 opt
= split_domain_pma(opt
, min_expr_pa
, min_expr
, cst
);
5563 opt
= isl_pw_multi_aff_reset_space(opt
, map_space
);
5569 static __isl_give isl_pw_multi_aff
*basic_map_partial_lexopt_symm_pma(
5570 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5571 __isl_give isl_set
**empty
, int max
, int first
, int second
)
5573 return basic_map_partial_lexopt_symm(bmap
, dom
, empty
, max
,
5574 first
, second
, &basic_map_partial_lexopt_symm_pma_core
).pma
;
5577 /* Recursive part of isl_basic_map_partial_lexopt_pw_multi_aff, after detecting
5578 * equalities and removing redundant constraints.
5580 * We first check if there are any parallel constraints (left).
5581 * If not, we are in the base case.
5582 * If there are parallel constraints, we replace them by a single
5583 * constraint in basic_map_partial_lexopt_symm_pma and then call
5584 * this function recursively to look for more parallel constraints.
5586 static __isl_give isl_pw_multi_aff
*basic_map_partial_lexopt_pma(
5587 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5588 __isl_give isl_set
**empty
, int max
)
5596 if (bmap
->ctx
->opt
->pip_symmetry
)
5597 par
= parallel_constraints(bmap
, &first
, &second
);
5601 return basic_map_partial_lexopt_base_pma(bmap
, dom
, empty
, max
);
5603 return basic_map_partial_lexopt_symm_pma(bmap
, dom
, empty
, max
,
5606 isl_basic_set_free(dom
);
5607 isl_basic_map_free(bmap
);
5611 /* Compute the lexicographic minimum (or maximum if "max" is set)
5612 * of "bmap" over the domain "dom" and return the result as a piecewise
5613 * multi-affine expression.
5614 * If "empty" is not NULL, then *empty is assigned a set that
5615 * contains those parts of the domain where there is no solution.
5616 * If "bmap" is marked as rational (ISL_BASIC_MAP_RATIONAL),
5617 * then we compute the rational optimum. Otherwise, we compute
5618 * the integral optimum.
5620 * We perform some preprocessing. As the PILP solver does not
5621 * handle implicit equalities very well, we first make sure all
5622 * the equalities are explicitly available.
5624 * We also add context constraints to the basic map and remove
5625 * redundant constraints. This is only needed because of the
5626 * way we handle simple symmetries. In particular, we currently look
5627 * for symmetries on the constraints, before we set up the main tableau.
5628 * It is then no good to look for symmetries on possibly redundant constraints.
5630 __isl_give isl_pw_multi_aff
*isl_basic_map_partial_lexopt_pw_multi_aff(
5631 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5632 __isl_give isl_set
**empty
, int max
)
5639 isl_assert(bmap
->ctx
,
5640 isl_basic_map_compatible_domain(bmap
, dom
), goto error
);
5642 if (isl_basic_set_dim(dom
, isl_dim_all
) == 0)
5643 return basic_map_partial_lexopt_pma(bmap
, dom
, empty
, max
);
5645 bmap
= isl_basic_map_intersect_domain(bmap
, isl_basic_set_copy(dom
));
5646 bmap
= isl_basic_map_detect_equalities(bmap
);
5647 bmap
= isl_basic_map_remove_redundancies(bmap
);
5649 return basic_map_partial_lexopt_pma(bmap
, dom
, empty
, max
);
5651 isl_basic_set_free(dom
);
5652 isl_basic_map_free(bmap
);