2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2016 Sven Verdoolaege
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, K.U.Leuven, Departement
9 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
11 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
14 #include <isl_ctx_private.h>
15 #include "isl_map_private.h"
18 #include "isl_sample.h"
19 #include <isl_mat_private.h>
20 #include <isl_vec_private.h>
21 #include <isl_aff_private.h>
22 #include <isl_constraint_private.h>
23 #include <isl_options_private.h>
24 #include <isl_config.h>
27 * The implementation of parametric integer linear programming in this file
28 * was inspired by the paper "Parametric Integer Programming" and the
29 * report "Solving systems of affine (in)equalities" by Paul Feautrier
32 * The strategy used for obtaining a feasible solution is different
33 * from the one used in isl_tab.c. In particular, in isl_tab.c,
34 * upon finding a constraint that is not yet satisfied, we pivot
35 * in a row that increases the constant term of the row holding the
36 * constraint, making sure the sample solution remains feasible
37 * for all the constraints it already satisfied.
38 * Here, we always pivot in the row holding the constraint,
39 * choosing a column that induces the lexicographically smallest
40 * increment to the sample solution.
42 * By starting out from a sample value that is lexicographically
43 * smaller than any integer point in the problem space, the first
44 * feasible integer sample point we find will also be the lexicographically
45 * smallest. If all variables can be assumed to be non-negative,
46 * then the initial sample value may be chosen equal to zero.
47 * However, we will not make this assumption. Instead, we apply
48 * the "big parameter" trick. Any variable x is then not directly
49 * used in the tableau, but instead it is represented by another
50 * variable x' = M + x, where M is an arbitrarily large (positive)
51 * value. x' is therefore always non-negative, whatever the value of x.
52 * Taking as initial sample value x' = 0 corresponds to x = -M,
53 * which is always smaller than any possible value of x.
55 * The big parameter trick is used in the main tableau and
56 * also in the context tableau if isl_context_lex is used.
57 * In this case, each tableaus has its own big parameter.
58 * Before doing any real work, we check if all the parameters
59 * happen to be non-negative. If so, we drop the column corresponding
60 * to M from the initial context tableau.
61 * If isl_context_gbr is used, then the big parameter trick is only
62 * used in the main tableau.
66 struct isl_context_op
{
67 /* detect nonnegative parameters in context and mark them in tab */
68 struct isl_tab
*(*detect_nonnegative_parameters
)(
69 struct isl_context
*context
, struct isl_tab
*tab
);
70 /* return temporary reference to basic set representation of context */
71 struct isl_basic_set
*(*peek_basic_set
)(struct isl_context
*context
);
72 /* return temporary reference to tableau representation of context */
73 struct isl_tab
*(*peek_tab
)(struct isl_context
*context
);
74 /* add equality; check is 1 if eq may not be valid;
75 * update is 1 if we may want to call ineq_sign on context later.
77 void (*add_eq
)(struct isl_context
*context
, isl_int
*eq
,
78 int check
, int update
);
79 /* add inequality; check is 1 if ineq may not be valid;
80 * update is 1 if we may want to call ineq_sign on context later.
82 void (*add_ineq
)(struct isl_context
*context
, isl_int
*ineq
,
83 int check
, int update
);
84 /* check sign of ineq based on previous information.
85 * strict is 1 if saturation should be treated as a positive sign.
87 enum isl_tab_row_sign (*ineq_sign
)(struct isl_context
*context
,
88 isl_int
*ineq
, int strict
);
89 /* check if inequality maintains feasibility */
90 int (*test_ineq
)(struct isl_context
*context
, isl_int
*ineq
);
91 /* return index of a div that corresponds to "div" */
92 int (*get_div
)(struct isl_context
*context
, struct isl_tab
*tab
,
94 /* add div "div" to context and return non-negativity */
95 isl_bool (*add_div
)(struct isl_context
*context
,
96 __isl_keep isl_vec
*div
);
97 int (*detect_equalities
)(struct isl_context
*context
,
99 /* return row index of "best" split */
100 int (*best_split
)(struct isl_context
*context
, struct isl_tab
*tab
);
101 /* check if context has already been determined to be empty */
102 int (*is_empty
)(struct isl_context
*context
);
103 /* check if context is still usable */
104 int (*is_ok
)(struct isl_context
*context
);
105 /* save a copy/snapshot of context */
106 void *(*save
)(struct isl_context
*context
);
107 /* restore saved context */
108 void (*restore
)(struct isl_context
*context
, void *);
109 /* discard saved context */
110 void (*discard
)(void *);
111 /* invalidate context */
112 void (*invalidate
)(struct isl_context
*context
);
114 void (*free
)(struct isl_context
*context
);
118 struct isl_context_op
*op
;
121 struct isl_context_lex
{
122 struct isl_context context
;
126 /* A stack (linked list) of solutions of subtrees of the search space.
128 * "M" describes the solution in terms of the dimensions of "dom".
129 * The number of columns of "M" is one more than the total number
130 * of dimensions of "dom".
132 * If "M" is NULL, then there is no solution on "dom".
134 struct isl_partial_sol
{
136 struct isl_basic_set
*dom
;
139 struct isl_partial_sol
*next
;
143 struct isl_sol_callback
{
144 struct isl_tab_callback callback
;
148 /* isl_sol is an interface for constructing a solution to
149 * a parametric integer linear programming problem.
150 * Every time the algorithm reaches a state where a solution
151 * can be read off from the tableau (including cases where the tableau
152 * is empty), the function "add" is called on the isl_sol passed
153 * to find_solutions_main.
155 * The context tableau is owned by isl_sol and is updated incrementally.
157 * There are currently two implementations of this interface,
158 * isl_sol_map, which simply collects the solutions in an isl_map
159 * and (optionally) the parts of the context where there is no solution
161 * isl_sol_for, which calls a user-defined function for each part of
170 struct isl_context
*context
;
171 struct isl_partial_sol
*partial
;
172 void (*add
)(struct isl_sol
*sol
,
173 struct isl_basic_set
*dom
, struct isl_mat
*M
);
174 void (*add_empty
)(struct isl_sol
*sol
, struct isl_basic_set
*bset
);
175 void (*free
)(struct isl_sol
*sol
);
176 struct isl_sol_callback dec_level
;
179 static void sol_free(struct isl_sol
*sol
)
181 struct isl_partial_sol
*partial
, *next
;
184 for (partial
= sol
->partial
; partial
; partial
= next
) {
185 next
= partial
->next
;
186 isl_basic_set_free(partial
->dom
);
187 isl_mat_free(partial
->M
);
193 /* Push a partial solution represented by a domain and mapping M
194 * onto the stack of partial solutions.
196 static void sol_push_sol(struct isl_sol
*sol
,
197 struct isl_basic_set
*dom
, struct isl_mat
*M
)
199 struct isl_partial_sol
*partial
;
201 if (sol
->error
|| !dom
)
204 partial
= isl_alloc_type(dom
->ctx
, struct isl_partial_sol
);
208 partial
->level
= sol
->level
;
211 partial
->next
= sol
->partial
;
213 sol
->partial
= partial
;
217 isl_basic_set_free(dom
);
222 /* Pop one partial solution from the partial solution stack and
223 * pass it on to sol->add or sol->add_empty.
225 static void sol_pop_one(struct isl_sol
*sol
)
227 struct isl_partial_sol
*partial
;
229 partial
= sol
->partial
;
230 sol
->partial
= partial
->next
;
233 sol
->add(sol
, partial
->dom
, partial
->M
);
235 sol
->add_empty(sol
, partial
->dom
);
239 /* Return a fresh copy of the domain represented by the context tableau.
241 static struct isl_basic_set
*sol_domain(struct isl_sol
*sol
)
243 struct isl_basic_set
*bset
;
248 bset
= isl_basic_set_dup(sol
->context
->op
->peek_basic_set(sol
->context
));
249 bset
= isl_basic_set_update_from_tab(bset
,
250 sol
->context
->op
->peek_tab(sol
->context
));
255 /* Check whether two partial solutions have the same mapping, where n_div
256 * is the number of divs that the two partial solutions have in common.
258 static int same_solution(struct isl_partial_sol
*s1
, struct isl_partial_sol
*s2
,
264 if (!s1
->M
!= !s2
->M
)
269 dim
= isl_basic_set_total_dim(s1
->dom
) - s1
->dom
->n_div
;
271 for (i
= 0; i
< s1
->M
->n_row
; ++i
) {
272 if (isl_seq_first_non_zero(s1
->M
->row
[i
]+1+dim
+n_div
,
273 s1
->M
->n_col
-1-dim
-n_div
) != -1)
275 if (isl_seq_first_non_zero(s2
->M
->row
[i
]+1+dim
+n_div
,
276 s2
->M
->n_col
-1-dim
-n_div
) != -1)
278 if (!isl_seq_eq(s1
->M
->row
[i
], s2
->M
->row
[i
], 1+dim
+n_div
))
284 /* Pop all solutions from the partial solution stack that were pushed onto
285 * the stack at levels that are deeper than the current level.
286 * If the two topmost elements on the stack have the same level
287 * and represent the same solution, then their domains are combined.
288 * This combined domain is the same as the current context domain
289 * as sol_pop is called each time we move back to a higher level.
290 * If the outer level (0) has been reached, then all partial solutions
291 * at the current level are also popped off.
293 static void sol_pop(struct isl_sol
*sol
)
295 struct isl_partial_sol
*partial
;
301 partial
= sol
->partial
;
305 if (partial
->level
== 0 && sol
->level
== 0) {
306 for (partial
= sol
->partial
; partial
; partial
= sol
->partial
)
311 if (partial
->level
<= sol
->level
)
314 if (partial
->next
&& partial
->next
->level
== partial
->level
) {
315 n_div
= isl_basic_set_dim(
316 sol
->context
->op
->peek_basic_set(sol
->context
),
319 if (!same_solution(partial
, partial
->next
, n_div
)) {
323 struct isl_basic_set
*bset
;
327 n
= isl_basic_set_dim(partial
->next
->dom
, isl_dim_div
);
329 bset
= sol_domain(sol
);
330 isl_basic_set_free(partial
->next
->dom
);
331 partial
->next
->dom
= bset
;
332 M
= partial
->next
->M
;
334 M
= isl_mat_drop_cols(M
, M
->n_col
- n
, n
);
335 partial
->next
->M
= M
;
339 partial
->next
->level
= sol
->level
;
344 sol
->partial
= partial
->next
;
345 isl_basic_set_free(partial
->dom
);
346 isl_mat_free(partial
->M
);
352 if (sol
->level
== 0) {
353 for (partial
= sol
->partial
; partial
; partial
= sol
->partial
)
359 error
: sol
->error
= 1;
362 static void sol_dec_level(struct isl_sol
*sol
)
372 static int sol_dec_level_wrap(struct isl_tab_callback
*cb
)
374 struct isl_sol_callback
*callback
= (struct isl_sol_callback
*)cb
;
376 sol_dec_level(callback
->sol
);
378 return callback
->sol
->error
? -1 : 0;
381 /* Move down to next level and push callback onto context tableau
382 * to decrease the level again when it gets rolled back across
383 * the current state. That is, dec_level will be called with
384 * the context tableau in the same state as it is when inc_level
387 static void sol_inc_level(struct isl_sol
*sol
)
395 tab
= sol
->context
->op
->peek_tab(sol
->context
);
396 if (isl_tab_push_callback(tab
, &sol
->dec_level
.callback
) < 0)
400 static void scale_rows(struct isl_mat
*mat
, isl_int m
, int n_row
)
404 if (isl_int_is_one(m
))
407 for (i
= 0; i
< n_row
; ++i
)
408 isl_seq_scale(mat
->row
[i
], mat
->row
[i
], m
, mat
->n_col
);
411 /* Add the solution identified by the tableau and the context tableau.
413 * The layout of the variables is as follows.
414 * tab->n_var is equal to the total number of variables in the input
415 * map (including divs that were copied from the context)
416 * + the number of extra divs constructed
417 * Of these, the first tab->n_param and the last tab->n_div variables
418 * correspond to the variables in the context, i.e.,
419 * tab->n_param + tab->n_div = context_tab->n_var
420 * tab->n_param is equal to the number of parameters and input
421 * dimensions in the input map
422 * tab->n_div is equal to the number of divs in the context
424 * If there is no solution, then call add_empty with a basic set
425 * that corresponds to the context tableau. (If add_empty is NULL,
428 * If there is a solution, then first construct a matrix that maps
429 * all dimensions of the context to the output variables, i.e.,
430 * the output dimensions in the input map.
431 * The divs in the input map (if any) that do not correspond to any
432 * div in the context do not appear in the solution.
433 * The algorithm will make sure that they have an integer value,
434 * but these values themselves are of no interest.
435 * We have to be careful not to drop or rearrange any divs in the
436 * context because that would change the meaning of the matrix.
438 * To extract the value of the output variables, it should be noted
439 * that we always use a big parameter M in the main tableau and so
440 * the variable stored in this tableau is not an output variable x itself, but
441 * x' = M + x (in case of minimization)
443 * x' = M - x (in case of maximization)
444 * If x' appears in a column, then its optimal value is zero,
445 * which means that the optimal value of x is an unbounded number
446 * (-M for minimization and M for maximization).
447 * We currently assume that the output dimensions in the original map
448 * are bounded, so this cannot occur.
449 * Similarly, when x' appears in a row, then the coefficient of M in that
450 * row is necessarily 1.
451 * If the row in the tableau represents
452 * d x' = c + d M + e(y)
453 * then, in case of minimization, the corresponding row in the matrix
456 * with a d = m, the (updated) common denominator of the matrix.
457 * In case of maximization, the row will be
460 static void sol_add(struct isl_sol
*sol
, struct isl_tab
*tab
)
462 struct isl_basic_set
*bset
= NULL
;
463 struct isl_mat
*mat
= NULL
;
468 if (sol
->error
|| !tab
)
471 if (tab
->empty
&& !sol
->add_empty
)
473 if (sol
->context
->op
->is_empty(sol
->context
))
476 bset
= sol_domain(sol
);
479 sol_push_sol(sol
, bset
, NULL
);
485 mat
= isl_mat_alloc(tab
->mat
->ctx
, 1 + sol
->n_out
,
486 1 + tab
->n_param
+ tab
->n_div
);
492 isl_seq_clr(mat
->row
[0] + 1, mat
->n_col
- 1);
493 isl_int_set_si(mat
->row
[0][0], 1);
494 for (row
= 0; row
< sol
->n_out
; ++row
) {
495 int i
= tab
->n_param
+ row
;
498 isl_seq_clr(mat
->row
[1 + row
], mat
->n_col
);
499 if (!tab
->var
[i
].is_row
) {
501 isl_die(mat
->ctx
, isl_error_invalid
,
502 "unbounded optimum", goto error2
);
506 r
= tab
->var
[i
].index
;
508 isl_int_ne(tab
->mat
->row
[r
][2], tab
->mat
->row
[r
][0]))
509 isl_die(mat
->ctx
, isl_error_invalid
,
510 "unbounded optimum", goto error2
);
511 isl_int_gcd(m
, mat
->row
[0][0], tab
->mat
->row
[r
][0]);
512 isl_int_divexact(m
, tab
->mat
->row
[r
][0], m
);
513 scale_rows(mat
, m
, 1 + row
);
514 isl_int_divexact(m
, mat
->row
[0][0], tab
->mat
->row
[r
][0]);
515 isl_int_mul(mat
->row
[1 + row
][0], m
, tab
->mat
->row
[r
][1]);
516 for (j
= 0; j
< tab
->n_param
; ++j
) {
518 if (tab
->var
[j
].is_row
)
520 col
= tab
->var
[j
].index
;
521 isl_int_mul(mat
->row
[1 + row
][1 + j
], m
,
522 tab
->mat
->row
[r
][off
+ col
]);
524 for (j
= 0; j
< tab
->n_div
; ++j
) {
526 if (tab
->var
[tab
->n_var
- tab
->n_div
+j
].is_row
)
528 col
= tab
->var
[tab
->n_var
- tab
->n_div
+j
].index
;
529 isl_int_mul(mat
->row
[1 + row
][1 + tab
->n_param
+ j
], m
,
530 tab
->mat
->row
[r
][off
+ col
]);
533 isl_seq_neg(mat
->row
[1 + row
], mat
->row
[1 + row
],
539 sol_push_sol(sol
, bset
, mat
);
544 isl_basic_set_free(bset
);
552 struct isl_set
*empty
;
555 static void sol_map_free(struct isl_sol_map
*sol_map
)
559 if (sol_map
->sol
.context
)
560 sol_map
->sol
.context
->op
->free(sol_map
->sol
.context
);
561 isl_map_free(sol_map
->map
);
562 isl_set_free(sol_map
->empty
);
566 static void sol_map_free_wrap(struct isl_sol
*sol
)
568 sol_map_free((struct isl_sol_map
*)sol
);
571 /* This function is called for parts of the context where there is
572 * no solution, with "bset" corresponding to the context tableau.
573 * Simply add the basic set to the set "empty".
575 static void sol_map_add_empty(struct isl_sol_map
*sol
,
576 struct isl_basic_set
*bset
)
578 if (!bset
|| !sol
->empty
)
581 sol
->empty
= isl_set_grow(sol
->empty
, 1);
582 bset
= isl_basic_set_simplify(bset
);
583 bset
= isl_basic_set_finalize(bset
);
584 sol
->empty
= isl_set_add_basic_set(sol
->empty
, isl_basic_set_copy(bset
));
587 isl_basic_set_free(bset
);
590 isl_basic_set_free(bset
);
594 static void sol_map_add_empty_wrap(struct isl_sol
*sol
,
595 struct isl_basic_set
*bset
)
597 sol_map_add_empty((struct isl_sol_map
*)sol
, bset
);
600 /* Given a basic map "dom" that represents the context and an affine
601 * matrix "M" that maps the dimensions of the context to the
602 * output variables, construct a basic map with the same parameters
603 * and divs as the context, the dimensions of the context as input
604 * dimensions and a number of output dimensions that is equal to
605 * the number of output dimensions in the input map.
607 * The constraints and divs of the context are simply copied
608 * from "dom". For each row
612 * is added, with d the common denominator of M.
614 static void sol_map_add(struct isl_sol_map
*sol
,
615 struct isl_basic_set
*dom
, struct isl_mat
*M
)
618 struct isl_basic_map
*bmap
= NULL
;
626 if (sol
->sol
.error
|| !dom
|| !M
)
629 n_out
= sol
->sol
.n_out
;
630 n_eq
= dom
->n_eq
+ n_out
;
631 n_ineq
= dom
->n_ineq
;
633 nparam
= isl_basic_set_total_dim(dom
) - n_div
;
634 total
= isl_map_dim(sol
->map
, isl_dim_all
);
635 bmap
= isl_basic_map_alloc_space(isl_map_get_space(sol
->map
),
636 n_div
, n_eq
, 2 * n_div
+ n_ineq
);
639 if (sol
->sol
.rational
)
640 ISL_F_SET(bmap
, ISL_BASIC_MAP_RATIONAL
);
641 for (i
= 0; i
< dom
->n_div
; ++i
) {
642 int k
= isl_basic_map_alloc_div(bmap
);
645 isl_seq_cpy(bmap
->div
[k
], dom
->div
[i
], 1 + 1 + nparam
);
646 isl_seq_clr(bmap
->div
[k
] + 1 + 1 + nparam
, total
- nparam
);
647 isl_seq_cpy(bmap
->div
[k
] + 1 + 1 + total
,
648 dom
->div
[i
] + 1 + 1 + nparam
, i
);
650 for (i
= 0; i
< dom
->n_eq
; ++i
) {
651 int k
= isl_basic_map_alloc_equality(bmap
);
654 isl_seq_cpy(bmap
->eq
[k
], dom
->eq
[i
], 1 + nparam
);
655 isl_seq_clr(bmap
->eq
[k
] + 1 + nparam
, total
- nparam
);
656 isl_seq_cpy(bmap
->eq
[k
] + 1 + total
,
657 dom
->eq
[i
] + 1 + nparam
, n_div
);
659 for (i
= 0; i
< dom
->n_ineq
; ++i
) {
660 int k
= isl_basic_map_alloc_inequality(bmap
);
663 isl_seq_cpy(bmap
->ineq
[k
], dom
->ineq
[i
], 1 + nparam
);
664 isl_seq_clr(bmap
->ineq
[k
] + 1 + nparam
, total
- nparam
);
665 isl_seq_cpy(bmap
->ineq
[k
] + 1 + total
,
666 dom
->ineq
[i
] + 1 + nparam
, n_div
);
668 for (i
= 0; i
< M
->n_row
- 1; ++i
) {
669 int k
= isl_basic_map_alloc_equality(bmap
);
672 isl_seq_cpy(bmap
->eq
[k
], M
->row
[1 + i
], 1 + nparam
);
673 isl_seq_clr(bmap
->eq
[k
] + 1 + nparam
, n_out
);
674 isl_int_neg(bmap
->eq
[k
][1 + nparam
+ i
], M
->row
[0][0]);
675 isl_seq_cpy(bmap
->eq
[k
] + 1 + nparam
+ n_out
,
676 M
->row
[1 + i
] + 1 + nparam
, n_div
);
678 bmap
= isl_basic_map_simplify(bmap
);
679 bmap
= isl_basic_map_finalize(bmap
);
680 sol
->map
= isl_map_grow(sol
->map
, 1);
681 sol
->map
= isl_map_add_basic_map(sol
->map
, bmap
);
682 isl_basic_set_free(dom
);
688 isl_basic_set_free(dom
);
690 isl_basic_map_free(bmap
);
694 static void sol_map_add_wrap(struct isl_sol
*sol
,
695 struct isl_basic_set
*dom
, struct isl_mat
*M
)
697 sol_map_add((struct isl_sol_map
*)sol
, dom
, M
);
701 /* Store the "parametric constant" of row "row" of tableau "tab" in "line",
702 * i.e., the constant term and the coefficients of all variables that
703 * appear in the context tableau.
704 * Note that the coefficient of the big parameter M is NOT copied.
705 * The context tableau may not have a big parameter and even when it
706 * does, it is a different big parameter.
708 static void get_row_parameter_line(struct isl_tab
*tab
, int row
, isl_int
*line
)
711 unsigned off
= 2 + tab
->M
;
713 isl_int_set(line
[0], tab
->mat
->row
[row
][1]);
714 for (i
= 0; i
< tab
->n_param
; ++i
) {
715 if (tab
->var
[i
].is_row
)
716 isl_int_set_si(line
[1 + i
], 0);
718 int col
= tab
->var
[i
].index
;
719 isl_int_set(line
[1 + i
], tab
->mat
->row
[row
][off
+ col
]);
722 for (i
= 0; i
< tab
->n_div
; ++i
) {
723 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
724 isl_int_set_si(line
[1 + tab
->n_param
+ i
], 0);
726 int col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
727 isl_int_set(line
[1 + tab
->n_param
+ i
],
728 tab
->mat
->row
[row
][off
+ col
]);
733 /* Check if rows "row1" and "row2" have identical "parametric constants",
734 * as explained above.
735 * In this case, we also insist that the coefficients of the big parameter
736 * be the same as the values of the constants will only be the same
737 * if these coefficients are also the same.
739 static int identical_parameter_line(struct isl_tab
*tab
, int row1
, int row2
)
742 unsigned off
= 2 + tab
->M
;
744 if (isl_int_ne(tab
->mat
->row
[row1
][1], tab
->mat
->row
[row2
][1]))
747 if (tab
->M
&& isl_int_ne(tab
->mat
->row
[row1
][2],
748 tab
->mat
->row
[row2
][2]))
751 for (i
= 0; i
< tab
->n_param
+ tab
->n_div
; ++i
) {
752 int pos
= i
< tab
->n_param
? i
:
753 tab
->n_var
- tab
->n_div
+ i
- tab
->n_param
;
756 if (tab
->var
[pos
].is_row
)
758 col
= tab
->var
[pos
].index
;
759 if (isl_int_ne(tab
->mat
->row
[row1
][off
+ col
],
760 tab
->mat
->row
[row2
][off
+ col
]))
766 /* Return an inequality that expresses that the "parametric constant"
767 * should be non-negative.
768 * This function is only called when the coefficient of the big parameter
771 static struct isl_vec
*get_row_parameter_ineq(struct isl_tab
*tab
, int row
)
773 struct isl_vec
*ineq
;
775 ineq
= isl_vec_alloc(tab
->mat
->ctx
, 1 + tab
->n_param
+ tab
->n_div
);
779 get_row_parameter_line(tab
, row
, ineq
->el
);
781 ineq
= isl_vec_normalize(ineq
);
786 /* Normalize a div expression of the form
788 * [(g*f(x) + c)/(g * m)]
790 * with c the constant term and f(x) the remaining coefficients, to
794 static void normalize_div(__isl_keep isl_vec
*div
)
796 isl_ctx
*ctx
= isl_vec_get_ctx(div
);
797 int len
= div
->size
- 2;
799 isl_seq_gcd(div
->el
+ 2, len
, &ctx
->normalize_gcd
);
800 isl_int_gcd(ctx
->normalize_gcd
, ctx
->normalize_gcd
, div
->el
[0]);
802 if (isl_int_is_one(ctx
->normalize_gcd
))
805 isl_int_divexact(div
->el
[0], div
->el
[0], ctx
->normalize_gcd
);
806 isl_int_fdiv_q(div
->el
[1], div
->el
[1], ctx
->normalize_gcd
);
807 isl_seq_scale_down(div
->el
+ 2, div
->el
+ 2, ctx
->normalize_gcd
, len
);
810 /* Return a integer division for use in a parametric cut based on the given row.
811 * In particular, let the parametric constant of the row be
815 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
816 * The div returned is equal to
818 * floor(\sum_i {-a_i} y_i) = floor((\sum_i (-a_i mod d) y_i)/d)
820 static struct isl_vec
*get_row_parameter_div(struct isl_tab
*tab
, int row
)
824 div
= isl_vec_alloc(tab
->mat
->ctx
, 1 + 1 + tab
->n_param
+ tab
->n_div
);
828 isl_int_set(div
->el
[0], tab
->mat
->row
[row
][0]);
829 get_row_parameter_line(tab
, row
, div
->el
+ 1);
830 isl_seq_neg(div
->el
+ 1, div
->el
+ 1, div
->size
- 1);
832 isl_seq_fdiv_r(div
->el
+ 1, div
->el
+ 1, div
->el
[0], div
->size
- 1);
837 /* Return a integer division for use in transferring an integrality constraint
839 * In particular, let the parametric constant of the row be
843 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
844 * The the returned div is equal to
846 * floor(\sum_i {a_i} y_i) = floor((\sum_i (a_i mod d) y_i)/d)
848 static struct isl_vec
*get_row_split_div(struct isl_tab
*tab
, int row
)
852 div
= isl_vec_alloc(tab
->mat
->ctx
, 1 + 1 + tab
->n_param
+ tab
->n_div
);
856 isl_int_set(div
->el
[0], tab
->mat
->row
[row
][0]);
857 get_row_parameter_line(tab
, row
, div
->el
+ 1);
859 isl_seq_fdiv_r(div
->el
+ 1, div
->el
+ 1, div
->el
[0], div
->size
- 1);
864 /* Construct and return an inequality that expresses an upper bound
866 * In particular, if the div is given by
870 * then the inequality expresses
874 static struct isl_vec
*ineq_for_div(struct isl_basic_set
*bset
, unsigned div
)
878 struct isl_vec
*ineq
;
883 total
= isl_basic_set_total_dim(bset
);
884 div_pos
= 1 + total
- bset
->n_div
+ div
;
886 ineq
= isl_vec_alloc(bset
->ctx
, 1 + total
);
890 isl_seq_cpy(ineq
->el
, bset
->div
[div
] + 1, 1 + total
);
891 isl_int_neg(ineq
->el
[div_pos
], bset
->div
[div
][0]);
895 /* Given a row in the tableau and a div that was created
896 * using get_row_split_div and that has been constrained to equality, i.e.,
898 * d = floor(\sum_i {a_i} y_i) = \sum_i {a_i} y_i
900 * replace the expression "\sum_i {a_i} y_i" in the row by d,
901 * i.e., we subtract "\sum_i {a_i} y_i" and add 1 d.
902 * The coefficients of the non-parameters in the tableau have been
903 * verified to be integral. We can therefore simply replace coefficient b
904 * by floor(b). For the coefficients of the parameters we have
905 * floor(a_i) = a_i - {a_i}, while for the other coefficients, we have
908 static struct isl_tab
*set_row_cst_to_div(struct isl_tab
*tab
, int row
, int div
)
910 isl_seq_fdiv_q(tab
->mat
->row
[row
] + 1, tab
->mat
->row
[row
] + 1,
911 tab
->mat
->row
[row
][0], 1 + tab
->M
+ tab
->n_col
);
913 isl_int_set_si(tab
->mat
->row
[row
][0], 1);
915 if (tab
->var
[tab
->n_var
- tab
->n_div
+ div
].is_row
) {
916 int drow
= tab
->var
[tab
->n_var
- tab
->n_div
+ div
].index
;
918 isl_assert(tab
->mat
->ctx
,
919 isl_int_is_one(tab
->mat
->row
[drow
][0]), goto error
);
920 isl_seq_combine(tab
->mat
->row
[row
] + 1,
921 tab
->mat
->ctx
->one
, tab
->mat
->row
[row
] + 1,
922 tab
->mat
->ctx
->one
, tab
->mat
->row
[drow
] + 1,
923 1 + tab
->M
+ tab
->n_col
);
925 int dcol
= tab
->var
[tab
->n_var
- tab
->n_div
+ div
].index
;
927 isl_int_add_ui(tab
->mat
->row
[row
][2 + tab
->M
+ dcol
],
928 tab
->mat
->row
[row
][2 + tab
->M
+ dcol
], 1);
937 /* Check if the (parametric) constant of the given row is obviously
938 * negative, meaning that we don't need to consult the context tableau.
939 * If there is a big parameter and its coefficient is non-zero,
940 * then this coefficient determines the outcome.
941 * Otherwise, we check whether the constant is negative and
942 * all non-zero coefficients of parameters are negative and
943 * belong to non-negative parameters.
945 static int is_obviously_neg(struct isl_tab
*tab
, int row
)
949 unsigned off
= 2 + tab
->M
;
952 if (isl_int_is_pos(tab
->mat
->row
[row
][2]))
954 if (isl_int_is_neg(tab
->mat
->row
[row
][2]))
958 if (isl_int_is_nonneg(tab
->mat
->row
[row
][1]))
960 for (i
= 0; i
< tab
->n_param
; ++i
) {
961 /* Eliminated parameter */
962 if (tab
->var
[i
].is_row
)
964 col
= tab
->var
[i
].index
;
965 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
967 if (!tab
->var
[i
].is_nonneg
)
969 if (isl_int_is_pos(tab
->mat
->row
[row
][off
+ col
]))
972 for (i
= 0; i
< tab
->n_div
; ++i
) {
973 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
975 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
976 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
978 if (!tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_nonneg
)
980 if (isl_int_is_pos(tab
->mat
->row
[row
][off
+ col
]))
986 /* Check if the (parametric) constant of the given row is obviously
987 * non-negative, meaning that we don't need to consult the context tableau.
988 * If there is a big parameter and its coefficient is non-zero,
989 * then this coefficient determines the outcome.
990 * Otherwise, we check whether the constant is non-negative and
991 * all non-zero coefficients of parameters are positive and
992 * belong to non-negative parameters.
994 static int is_obviously_nonneg(struct isl_tab
*tab
, int row
)
998 unsigned off
= 2 + tab
->M
;
1001 if (isl_int_is_pos(tab
->mat
->row
[row
][2]))
1003 if (isl_int_is_neg(tab
->mat
->row
[row
][2]))
1007 if (isl_int_is_neg(tab
->mat
->row
[row
][1]))
1009 for (i
= 0; i
< tab
->n_param
; ++i
) {
1010 /* Eliminated parameter */
1011 if (tab
->var
[i
].is_row
)
1013 col
= tab
->var
[i
].index
;
1014 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
1016 if (!tab
->var
[i
].is_nonneg
)
1018 if (isl_int_is_neg(tab
->mat
->row
[row
][off
+ col
]))
1021 for (i
= 0; i
< tab
->n_div
; ++i
) {
1022 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
1024 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
1025 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
1027 if (!tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_nonneg
)
1029 if (isl_int_is_neg(tab
->mat
->row
[row
][off
+ col
]))
1035 /* Given a row r and two columns, return the column that would
1036 * lead to the lexicographically smallest increment in the sample
1037 * solution when leaving the basis in favor of the row.
1038 * Pivoting with column c will increment the sample value by a non-negative
1039 * constant times a_{V,c}/a_{r,c}, with a_{V,c} the elements of column c
1040 * corresponding to the non-parametric variables.
1041 * If variable v appears in a column c_v, the a_{v,c} = 1 iff c = c_v,
1042 * with all other entries in this virtual row equal to zero.
1043 * If variable v appears in a row, then a_{v,c} is the element in column c
1046 * Let v be the first variable with a_{v,c1}/a_{r,c1} != a_{v,c2}/a_{r,c2}.
1047 * Then if a_{v,c1}/a_{r,c1} < a_{v,c2}/a_{r,c2}, i.e.,
1048 * a_{v,c2} a_{r,c1} - a_{v,c1} a_{r,c2} > 0, c1 results in the minimal
1049 * increment. Otherwise, it's c2.
1051 static int lexmin_col_pair(struct isl_tab
*tab
,
1052 int row
, int col1
, int col2
, isl_int tmp
)
1057 tr
= tab
->mat
->row
[row
] + 2 + tab
->M
;
1059 for (i
= tab
->n_param
; i
< tab
->n_var
- tab
->n_div
; ++i
) {
1063 if (!tab
->var
[i
].is_row
) {
1064 if (tab
->var
[i
].index
== col1
)
1066 if (tab
->var
[i
].index
== col2
)
1071 if (tab
->var
[i
].index
== row
)
1074 r
= tab
->mat
->row
[tab
->var
[i
].index
] + 2 + tab
->M
;
1075 s1
= isl_int_sgn(r
[col1
]);
1076 s2
= isl_int_sgn(r
[col2
]);
1077 if (s1
== 0 && s2
== 0)
1084 isl_int_mul(tmp
, r
[col2
], tr
[col1
]);
1085 isl_int_submul(tmp
, r
[col1
], tr
[col2
]);
1086 if (isl_int_is_pos(tmp
))
1088 if (isl_int_is_neg(tmp
))
1094 /* Given a row in the tableau, find and return the column that would
1095 * result in the lexicographically smallest, but positive, increment
1096 * in the sample point.
1097 * If there is no such column, then return tab->n_col.
1098 * If anything goes wrong, return -1.
1100 static int lexmin_pivot_col(struct isl_tab
*tab
, int row
)
1103 int col
= tab
->n_col
;
1107 tr
= tab
->mat
->row
[row
] + 2 + tab
->M
;
1111 for (j
= tab
->n_dead
; j
< tab
->n_col
; ++j
) {
1112 if (tab
->col_var
[j
] >= 0 &&
1113 (tab
->col_var
[j
] < tab
->n_param
||
1114 tab
->col_var
[j
] >= tab
->n_var
- tab
->n_div
))
1117 if (!isl_int_is_pos(tr
[j
]))
1120 if (col
== tab
->n_col
)
1123 col
= lexmin_col_pair(tab
, row
, col
, j
, tmp
);
1124 isl_assert(tab
->mat
->ctx
, col
>= 0, goto error
);
1134 /* Return the first known violated constraint, i.e., a non-negative
1135 * constraint that currently has an either obviously negative value
1136 * or a previously determined to be negative value.
1138 * If any constraint has a negative coefficient for the big parameter,
1139 * if any, then we return one of these first.
1141 static int first_neg(struct isl_tab
*tab
)
1146 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
1147 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
1149 if (!isl_int_is_neg(tab
->mat
->row
[row
][2]))
1152 tab
->row_sign
[row
] = isl_tab_row_neg
;
1155 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
1156 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
1158 if (tab
->row_sign
) {
1159 if (tab
->row_sign
[row
] == 0 &&
1160 is_obviously_neg(tab
, row
))
1161 tab
->row_sign
[row
] = isl_tab_row_neg
;
1162 if (tab
->row_sign
[row
] != isl_tab_row_neg
)
1164 } else if (!is_obviously_neg(tab
, row
))
1171 /* Check whether the invariant that all columns are lexico-positive
1172 * is satisfied. This function is not called from the current code
1173 * but is useful during debugging.
1175 static void check_lexpos(struct isl_tab
*tab
) __attribute__ ((unused
));
1176 static void check_lexpos(struct isl_tab
*tab
)
1178 unsigned off
= 2 + tab
->M
;
1183 for (col
= tab
->n_dead
; col
< tab
->n_col
; ++col
) {
1184 if (tab
->col_var
[col
] >= 0 &&
1185 (tab
->col_var
[col
] < tab
->n_param
||
1186 tab
->col_var
[col
] >= tab
->n_var
- tab
->n_div
))
1188 for (var
= tab
->n_param
; var
< tab
->n_var
- tab
->n_div
; ++var
) {
1189 if (!tab
->var
[var
].is_row
) {
1190 if (tab
->var
[var
].index
== col
)
1195 row
= tab
->var
[var
].index
;
1196 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
1198 if (isl_int_is_pos(tab
->mat
->row
[row
][off
+ col
]))
1200 fprintf(stderr
, "lexneg column %d (row %d)\n",
1203 if (var
>= tab
->n_var
- tab
->n_div
)
1204 fprintf(stderr
, "zero column %d\n", col
);
1208 /* Report to the caller that the given constraint is part of an encountered
1211 static int report_conflicting_constraint(struct isl_tab
*tab
, int con
)
1213 return tab
->conflict(con
, tab
->conflict_user
);
1216 /* Given a conflicting row in the tableau, report all constraints
1217 * involved in the row to the caller. That is, the row itself
1218 * (if it represents a constraint) and all constraint columns with
1219 * non-zero (and therefore negative) coefficients.
1221 static int report_conflict(struct isl_tab
*tab
, int row
)
1229 if (tab
->row_var
[row
] < 0 &&
1230 report_conflicting_constraint(tab
, ~tab
->row_var
[row
]) < 0)
1233 tr
= tab
->mat
->row
[row
] + 2 + tab
->M
;
1235 for (j
= tab
->n_dead
; j
< tab
->n_col
; ++j
) {
1236 if (tab
->col_var
[j
] >= 0 &&
1237 (tab
->col_var
[j
] < tab
->n_param
||
1238 tab
->col_var
[j
] >= tab
->n_var
- tab
->n_div
))
1241 if (!isl_int_is_neg(tr
[j
]))
1244 if (tab
->col_var
[j
] < 0 &&
1245 report_conflicting_constraint(tab
, ~tab
->col_var
[j
]) < 0)
1252 /* Resolve all known or obviously violated constraints through pivoting.
1253 * In particular, as long as we can find any violated constraint, we
1254 * look for a pivoting column that would result in the lexicographically
1255 * smallest increment in the sample point. If there is no such column
1256 * then the tableau is infeasible.
1258 static int restore_lexmin(struct isl_tab
*tab
) WARN_UNUSED
;
1259 static int restore_lexmin(struct isl_tab
*tab
)
1267 while ((row
= first_neg(tab
)) != -1) {
1268 col
= lexmin_pivot_col(tab
, row
);
1269 if (col
>= tab
->n_col
) {
1270 if (report_conflict(tab
, row
) < 0)
1272 if (isl_tab_mark_empty(tab
) < 0)
1278 if (isl_tab_pivot(tab
, row
, col
) < 0)
1284 /* Given a row that represents an equality, look for an appropriate
1286 * In particular, if there are any non-zero coefficients among
1287 * the non-parameter variables, then we take the last of these
1288 * variables. Eliminating this variable in terms of the other
1289 * variables and/or parameters does not influence the property
1290 * that all column in the initial tableau are lexicographically
1291 * positive. The row corresponding to the eliminated variable
1292 * will only have non-zero entries below the diagonal of the
1293 * initial tableau. That is, we transform
1299 * If there is no such non-parameter variable, then we are dealing with
1300 * pure parameter equality and we pick any parameter with coefficient 1 or -1
1301 * for elimination. This will ensure that the eliminated parameter
1302 * always has an integer value whenever all the other parameters are integral.
1303 * If there is no such parameter then we return -1.
1305 static int last_var_col_or_int_par_col(struct isl_tab
*tab
, int row
)
1307 unsigned off
= 2 + tab
->M
;
1310 for (i
= tab
->n_var
- tab
->n_div
- 1; i
>= 0 && i
>= tab
->n_param
; --i
) {
1312 if (tab
->var
[i
].is_row
)
1314 col
= tab
->var
[i
].index
;
1315 if (col
<= tab
->n_dead
)
1317 if (!isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
1320 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
1321 if (isl_int_is_one(tab
->mat
->row
[row
][off
+ i
]))
1323 if (isl_int_is_negone(tab
->mat
->row
[row
][off
+ i
]))
1329 /* Add an equality that is known to be valid to the tableau.
1330 * We first check if we can eliminate a variable or a parameter.
1331 * If not, we add the equality as two inequalities.
1332 * In this case, the equality was a pure parameter equality and there
1333 * is no need to resolve any constraint violations.
1335 * This function assumes that at least two more rows and at least
1336 * two more elements in the constraint array are available in the tableau.
1338 static struct isl_tab
*add_lexmin_valid_eq(struct isl_tab
*tab
, isl_int
*eq
)
1345 r
= isl_tab_add_row(tab
, eq
);
1349 r
= tab
->con
[r
].index
;
1350 i
= last_var_col_or_int_par_col(tab
, r
);
1352 tab
->con
[r
].is_nonneg
= 1;
1353 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
1355 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
1356 r
= isl_tab_add_row(tab
, eq
);
1359 tab
->con
[r
].is_nonneg
= 1;
1360 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
1363 if (isl_tab_pivot(tab
, r
, i
) < 0)
1365 if (isl_tab_kill_col(tab
, i
) < 0)
1376 /* Check if the given row is a pure constant.
1378 static int is_constant(struct isl_tab
*tab
, int row
)
1380 unsigned off
= 2 + tab
->M
;
1382 return isl_seq_first_non_zero(tab
->mat
->row
[row
] + off
+ tab
->n_dead
,
1383 tab
->n_col
- tab
->n_dead
) == -1;
1386 /* Add an equality that may or may not be valid to the tableau.
1387 * If the resulting row is a pure constant, then it must be zero.
1388 * Otherwise, the resulting tableau is empty.
1390 * If the row is not a pure constant, then we add two inequalities,
1391 * each time checking that they can be satisfied.
1392 * In the end we try to use one of the two constraints to eliminate
1395 * This function assumes that at least two more rows and at least
1396 * two more elements in the constraint array are available in the tableau.
1398 static int add_lexmin_eq(struct isl_tab
*tab
, isl_int
*eq
) WARN_UNUSED
;
1399 static int add_lexmin_eq(struct isl_tab
*tab
, isl_int
*eq
)
1403 struct isl_tab_undo
*snap
;
1407 snap
= isl_tab_snap(tab
);
1408 r1
= isl_tab_add_row(tab
, eq
);
1411 tab
->con
[r1
].is_nonneg
= 1;
1412 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r1
]) < 0)
1415 row
= tab
->con
[r1
].index
;
1416 if (is_constant(tab
, row
)) {
1417 if (!isl_int_is_zero(tab
->mat
->row
[row
][1]) ||
1418 (tab
->M
&& !isl_int_is_zero(tab
->mat
->row
[row
][2]))) {
1419 if (isl_tab_mark_empty(tab
) < 0)
1423 if (isl_tab_rollback(tab
, snap
) < 0)
1428 if (restore_lexmin(tab
) < 0)
1433 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
1435 r2
= isl_tab_add_row(tab
, eq
);
1438 tab
->con
[r2
].is_nonneg
= 1;
1439 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r2
]) < 0)
1442 if (restore_lexmin(tab
) < 0)
1447 if (!tab
->con
[r1
].is_row
) {
1448 if (isl_tab_kill_col(tab
, tab
->con
[r1
].index
) < 0)
1450 } else if (!tab
->con
[r2
].is_row
) {
1451 if (isl_tab_kill_col(tab
, tab
->con
[r2
].index
) < 0)
1456 tab
->bmap
= isl_basic_map_add_ineq(tab
->bmap
, eq
);
1457 if (isl_tab_push(tab
, isl_tab_undo_bmap_ineq
) < 0)
1459 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
1460 tab
->bmap
= isl_basic_map_add_ineq(tab
->bmap
, eq
);
1461 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
1462 if (isl_tab_push(tab
, isl_tab_undo_bmap_ineq
) < 0)
1471 /* Add an inequality to the tableau, resolving violations using
1474 * This function assumes that at least one more row and at least
1475 * one more element in the constraint array are available in the tableau.
1477 static struct isl_tab
*add_lexmin_ineq(struct isl_tab
*tab
, isl_int
*ineq
)
1484 tab
->bmap
= isl_basic_map_add_ineq(tab
->bmap
, ineq
);
1485 if (isl_tab_push(tab
, isl_tab_undo_bmap_ineq
) < 0)
1490 r
= isl_tab_add_row(tab
, ineq
);
1493 tab
->con
[r
].is_nonneg
= 1;
1494 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
1496 if (isl_tab_row_is_redundant(tab
, tab
->con
[r
].index
)) {
1497 if (isl_tab_mark_redundant(tab
, tab
->con
[r
].index
) < 0)
1502 if (restore_lexmin(tab
) < 0)
1504 if (!tab
->empty
&& tab
->con
[r
].is_row
&&
1505 isl_tab_row_is_redundant(tab
, tab
->con
[r
].index
))
1506 if (isl_tab_mark_redundant(tab
, tab
->con
[r
].index
) < 0)
1514 /* Check if the coefficients of the parameters are all integral.
1516 static int integer_parameter(struct isl_tab
*tab
, int row
)
1520 unsigned off
= 2 + tab
->M
;
1522 for (i
= 0; i
< tab
->n_param
; ++i
) {
1523 /* Eliminated parameter */
1524 if (tab
->var
[i
].is_row
)
1526 col
= tab
->var
[i
].index
;
1527 if (!isl_int_is_divisible_by(tab
->mat
->row
[row
][off
+ col
],
1528 tab
->mat
->row
[row
][0]))
1531 for (i
= 0; i
< tab
->n_div
; ++i
) {
1532 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
1534 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
1535 if (!isl_int_is_divisible_by(tab
->mat
->row
[row
][off
+ col
],
1536 tab
->mat
->row
[row
][0]))
1542 /* Check if the coefficients of the non-parameter variables are all integral.
1544 static int integer_variable(struct isl_tab
*tab
, int row
)
1547 unsigned off
= 2 + tab
->M
;
1549 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
1550 if (tab
->col_var
[i
] >= 0 &&
1551 (tab
->col_var
[i
] < tab
->n_param
||
1552 tab
->col_var
[i
] >= tab
->n_var
- tab
->n_div
))
1554 if (!isl_int_is_divisible_by(tab
->mat
->row
[row
][off
+ i
],
1555 tab
->mat
->row
[row
][0]))
1561 /* Check if the constant term is integral.
1563 static int integer_constant(struct isl_tab
*tab
, int row
)
1565 return isl_int_is_divisible_by(tab
->mat
->row
[row
][1],
1566 tab
->mat
->row
[row
][0]);
1569 #define I_CST 1 << 0
1570 #define I_PAR 1 << 1
1571 #define I_VAR 1 << 2
1573 /* Check for next (non-parameter) variable after "var" (first if var == -1)
1574 * that is non-integer and therefore requires a cut and return
1575 * the index of the variable.
1576 * For parametric tableaus, there are three parts in a row,
1577 * the constant, the coefficients of the parameters and the rest.
1578 * For each part, we check whether the coefficients in that part
1579 * are all integral and if so, set the corresponding flag in *f.
1580 * If the constant and the parameter part are integral, then the
1581 * current sample value is integral and no cut is required
1582 * (irrespective of whether the variable part is integral).
1584 static int next_non_integer_var(struct isl_tab
*tab
, int var
, int *f
)
1586 var
= var
< 0 ? tab
->n_param
: var
+ 1;
1588 for (; var
< tab
->n_var
- tab
->n_div
; ++var
) {
1591 if (!tab
->var
[var
].is_row
)
1593 row
= tab
->var
[var
].index
;
1594 if (integer_constant(tab
, row
))
1595 ISL_FL_SET(flags
, I_CST
);
1596 if (integer_parameter(tab
, row
))
1597 ISL_FL_SET(flags
, I_PAR
);
1598 if (ISL_FL_ISSET(flags
, I_CST
) && ISL_FL_ISSET(flags
, I_PAR
))
1600 if (integer_variable(tab
, row
))
1601 ISL_FL_SET(flags
, I_VAR
);
1608 /* Check for first (non-parameter) variable that is non-integer and
1609 * therefore requires a cut and return the corresponding row.
1610 * For parametric tableaus, there are three parts in a row,
1611 * the constant, the coefficients of the parameters and the rest.
1612 * For each part, we check whether the coefficients in that part
1613 * are all integral and if so, set the corresponding flag in *f.
1614 * If the constant and the parameter part are integral, then the
1615 * current sample value is integral and no cut is required
1616 * (irrespective of whether the variable part is integral).
1618 static int first_non_integer_row(struct isl_tab
*tab
, int *f
)
1620 int var
= next_non_integer_var(tab
, -1, f
);
1622 return var
< 0 ? -1 : tab
->var
[var
].index
;
1625 /* Add a (non-parametric) cut to cut away the non-integral sample
1626 * value of the given row.
1628 * If the row is given by
1630 * m r = f + \sum_i a_i y_i
1634 * c = - {-f/m} + \sum_i {a_i/m} y_i >= 0
1636 * The big parameter, if any, is ignored, since it is assumed to be big
1637 * enough to be divisible by any integer.
1638 * If the tableau is actually a parametric tableau, then this function
1639 * is only called when all coefficients of the parameters are integral.
1640 * The cut therefore has zero coefficients for the parameters.
1642 * The current value is known to be negative, so row_sign, if it
1643 * exists, is set accordingly.
1645 * Return the row of the cut or -1.
1647 static int add_cut(struct isl_tab
*tab
, int row
)
1652 unsigned off
= 2 + tab
->M
;
1654 if (isl_tab_extend_cons(tab
, 1) < 0)
1656 r
= isl_tab_allocate_con(tab
);
1660 r_row
= tab
->mat
->row
[tab
->con
[r
].index
];
1661 isl_int_set(r_row
[0], tab
->mat
->row
[row
][0]);
1662 isl_int_neg(r_row
[1], tab
->mat
->row
[row
][1]);
1663 isl_int_fdiv_r(r_row
[1], r_row
[1], tab
->mat
->row
[row
][0]);
1664 isl_int_neg(r_row
[1], r_row
[1]);
1666 isl_int_set_si(r_row
[2], 0);
1667 for (i
= 0; i
< tab
->n_col
; ++i
)
1668 isl_int_fdiv_r(r_row
[off
+ i
],
1669 tab
->mat
->row
[row
][off
+ i
], tab
->mat
->row
[row
][0]);
1671 tab
->con
[r
].is_nonneg
= 1;
1672 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
1675 tab
->row_sign
[tab
->con
[r
].index
] = isl_tab_row_neg
;
1677 return tab
->con
[r
].index
;
1683 /* Given a non-parametric tableau, add cuts until an integer
1684 * sample point is obtained or until the tableau is determined
1685 * to be integer infeasible.
1686 * As long as there is any non-integer value in the sample point,
1687 * we add appropriate cuts, if possible, for each of these
1688 * non-integer values and then resolve the violated
1689 * cut constraints using restore_lexmin.
1690 * If one of the corresponding rows is equal to an integral
1691 * combination of variables/constraints plus a non-integral constant,
1692 * then there is no way to obtain an integer point and we return
1693 * a tableau that is marked empty.
1694 * The parameter cutting_strategy controls the strategy used when adding cuts
1695 * to remove non-integer points. CUT_ALL adds all possible cuts
1696 * before continuing the search. CUT_ONE adds only one cut at a time.
1698 static struct isl_tab
*cut_to_integer_lexmin(struct isl_tab
*tab
,
1699 int cutting_strategy
)
1710 while ((var
= next_non_integer_var(tab
, -1, &flags
)) != -1) {
1712 if (ISL_FL_ISSET(flags
, I_VAR
)) {
1713 if (isl_tab_mark_empty(tab
) < 0)
1717 row
= tab
->var
[var
].index
;
1718 row
= add_cut(tab
, row
);
1721 if (cutting_strategy
== CUT_ONE
)
1723 } while ((var
= next_non_integer_var(tab
, var
, &flags
)) != -1);
1724 if (restore_lexmin(tab
) < 0)
1735 /* Check whether all the currently active samples also satisfy the inequality
1736 * "ineq" (treated as an equality if eq is set).
1737 * Remove those samples that do not.
1739 static struct isl_tab
*check_samples(struct isl_tab
*tab
, isl_int
*ineq
, int eq
)
1747 isl_assert(tab
->mat
->ctx
, tab
->bmap
, goto error
);
1748 isl_assert(tab
->mat
->ctx
, tab
->samples
, goto error
);
1749 isl_assert(tab
->mat
->ctx
, tab
->samples
->n_col
== 1 + tab
->n_var
, goto error
);
1752 for (i
= tab
->n_outside
; i
< tab
->n_sample
; ++i
) {
1754 isl_seq_inner_product(ineq
, tab
->samples
->row
[i
],
1755 1 + tab
->n_var
, &v
);
1756 sgn
= isl_int_sgn(v
);
1757 if (eq
? (sgn
== 0) : (sgn
>= 0))
1759 tab
= isl_tab_drop_sample(tab
, i
);
1771 /* Check whether the sample value of the tableau is finite,
1772 * i.e., either the tableau does not use a big parameter, or
1773 * all values of the variables are equal to the big parameter plus
1774 * some constant. This constant is the actual sample value.
1776 static int sample_is_finite(struct isl_tab
*tab
)
1783 for (i
= 0; i
< tab
->n_var
; ++i
) {
1785 if (!tab
->var
[i
].is_row
)
1787 row
= tab
->var
[i
].index
;
1788 if (isl_int_ne(tab
->mat
->row
[row
][0], tab
->mat
->row
[row
][2]))
1794 /* Check if the context tableau of sol has any integer points.
1795 * Leave tab in empty state if no integer point can be found.
1796 * If an integer point can be found and if moreover it is finite,
1797 * then it is added to the list of sample values.
1799 * This function is only called when none of the currently active sample
1800 * values satisfies the most recently added constraint.
1802 static struct isl_tab
*check_integer_feasible(struct isl_tab
*tab
)
1804 struct isl_tab_undo
*snap
;
1809 snap
= isl_tab_snap(tab
);
1810 if (isl_tab_push_basis(tab
) < 0)
1813 tab
= cut_to_integer_lexmin(tab
, CUT_ALL
);
1817 if (!tab
->empty
&& sample_is_finite(tab
)) {
1818 struct isl_vec
*sample
;
1820 sample
= isl_tab_get_sample_value(tab
);
1822 if (isl_tab_add_sample(tab
, sample
) < 0)
1826 if (!tab
->empty
&& isl_tab_rollback(tab
, snap
) < 0)
1835 /* Check if any of the currently active sample values satisfies
1836 * the inequality "ineq" (an equality if eq is set).
1838 static int tab_has_valid_sample(struct isl_tab
*tab
, isl_int
*ineq
, int eq
)
1846 isl_assert(tab
->mat
->ctx
, tab
->bmap
, return -1);
1847 isl_assert(tab
->mat
->ctx
, tab
->samples
, return -1);
1848 isl_assert(tab
->mat
->ctx
, tab
->samples
->n_col
== 1 + tab
->n_var
, return -1);
1851 for (i
= tab
->n_outside
; i
< tab
->n_sample
; ++i
) {
1853 isl_seq_inner_product(ineq
, tab
->samples
->row
[i
],
1854 1 + tab
->n_var
, &v
);
1855 sgn
= isl_int_sgn(v
);
1856 if (eq
? (sgn
== 0) : (sgn
>= 0))
1861 return i
< tab
->n_sample
;
1864 /* Add a div specified by "div" to the tableau "tab" and return
1865 * isl_bool_true if the div is obviously non-negative.
1867 static isl_bool
context_tab_add_div(struct isl_tab
*tab
,
1868 __isl_keep isl_vec
*div
,
1869 int (*add_ineq
)(void *user
, isl_int
*), void *user
)
1873 struct isl_mat
*samples
;
1876 r
= isl_tab_add_div(tab
, div
, add_ineq
, user
);
1878 return isl_bool_error
;
1879 nonneg
= tab
->var
[r
].is_nonneg
;
1880 tab
->var
[r
].frozen
= 1;
1882 samples
= isl_mat_extend(tab
->samples
,
1883 tab
->n_sample
, 1 + tab
->n_var
);
1884 tab
->samples
= samples
;
1886 return isl_bool_error
;
1887 for (i
= tab
->n_outside
; i
< samples
->n_row
; ++i
) {
1888 isl_seq_inner_product(div
->el
+ 1, samples
->row
[i
],
1889 div
->size
- 1, &samples
->row
[i
][samples
->n_col
- 1]);
1890 isl_int_fdiv_q(samples
->row
[i
][samples
->n_col
- 1],
1891 samples
->row
[i
][samples
->n_col
- 1], div
->el
[0]);
1897 /* Add a div specified by "div" to both the main tableau and
1898 * the context tableau. In case of the main tableau, we only
1899 * need to add an extra div. In the context tableau, we also
1900 * need to express the meaning of the div.
1901 * Return the index of the div or -1 if anything went wrong.
1903 static int add_div(struct isl_tab
*tab
, struct isl_context
*context
,
1904 __isl_keep isl_vec
*div
)
1909 if ((nonneg
= context
->op
->add_div(context
, div
)) < 0)
1912 if (!context
->op
->is_ok(context
))
1915 if (isl_tab_extend_vars(tab
, 1) < 0)
1917 r
= isl_tab_allocate_var(tab
);
1921 tab
->var
[r
].is_nonneg
= 1;
1922 tab
->var
[r
].frozen
= 1;
1925 return tab
->n_div
- 1;
1927 context
->op
->invalidate(context
);
1931 static int find_div(struct isl_tab
*tab
, isl_int
*div
, isl_int denom
)
1934 unsigned total
= isl_basic_map_total_dim(tab
->bmap
);
1936 for (i
= 0; i
< tab
->bmap
->n_div
; ++i
) {
1937 if (isl_int_ne(tab
->bmap
->div
[i
][0], denom
))
1939 if (!isl_seq_eq(tab
->bmap
->div
[i
] + 1, div
, 1 + total
))
1946 /* Return the index of a div that corresponds to "div".
1947 * We first check if we already have such a div and if not, we create one.
1949 static int get_div(struct isl_tab
*tab
, struct isl_context
*context
,
1950 struct isl_vec
*div
)
1953 struct isl_tab
*context_tab
= context
->op
->peek_tab(context
);
1958 d
= find_div(context_tab
, div
->el
+ 1, div
->el
[0]);
1962 return add_div(tab
, context
, div
);
1965 /* Add a parametric cut to cut away the non-integral sample value
1967 * Let a_i be the coefficients of the constant term and the parameters
1968 * and let b_i be the coefficients of the variables or constraints
1969 * in basis of the tableau.
1970 * Let q be the div q = floor(\sum_i {-a_i} y_i).
1972 * The cut is expressed as
1974 * c = \sum_i -{-a_i} y_i + \sum_i {b_i} x_i + q >= 0
1976 * If q did not already exist in the context tableau, then it is added first.
1977 * If q is in a column of the main tableau then the "+ q" can be accomplished
1978 * by setting the corresponding entry to the denominator of the constraint.
1979 * If q happens to be in a row of the main tableau, then the corresponding
1980 * row needs to be added instead (taking care of the denominators).
1981 * Note that this is very unlikely, but perhaps not entirely impossible.
1983 * The current value of the cut is known to be negative (or at least
1984 * non-positive), so row_sign is set accordingly.
1986 * Return the row of the cut or -1.
1988 static int add_parametric_cut(struct isl_tab
*tab
, int row
,
1989 struct isl_context
*context
)
1991 struct isl_vec
*div
;
1998 unsigned off
= 2 + tab
->M
;
2003 div
= get_row_parameter_div(tab
, row
);
2008 d
= context
->op
->get_div(context
, tab
, div
);
2013 if (isl_tab_extend_cons(tab
, 1) < 0)
2015 r
= isl_tab_allocate_con(tab
);
2019 r_row
= tab
->mat
->row
[tab
->con
[r
].index
];
2020 isl_int_set(r_row
[0], tab
->mat
->row
[row
][0]);
2021 isl_int_neg(r_row
[1], tab
->mat
->row
[row
][1]);
2022 isl_int_fdiv_r(r_row
[1], r_row
[1], tab
->mat
->row
[row
][0]);
2023 isl_int_neg(r_row
[1], r_row
[1]);
2025 isl_int_set_si(r_row
[2], 0);
2026 for (i
= 0; i
< tab
->n_param
; ++i
) {
2027 if (tab
->var
[i
].is_row
)
2029 col
= tab
->var
[i
].index
;
2030 isl_int_neg(r_row
[off
+ col
], tab
->mat
->row
[row
][off
+ col
]);
2031 isl_int_fdiv_r(r_row
[off
+ col
], r_row
[off
+ col
],
2032 tab
->mat
->row
[row
][0]);
2033 isl_int_neg(r_row
[off
+ col
], r_row
[off
+ col
]);
2035 for (i
= 0; i
< tab
->n_div
; ++i
) {
2036 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
2038 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
2039 isl_int_neg(r_row
[off
+ col
], tab
->mat
->row
[row
][off
+ col
]);
2040 isl_int_fdiv_r(r_row
[off
+ col
], r_row
[off
+ col
],
2041 tab
->mat
->row
[row
][0]);
2042 isl_int_neg(r_row
[off
+ col
], r_row
[off
+ col
]);
2044 for (i
= 0; i
< tab
->n_col
; ++i
) {
2045 if (tab
->col_var
[i
] >= 0 &&
2046 (tab
->col_var
[i
] < tab
->n_param
||
2047 tab
->col_var
[i
] >= tab
->n_var
- tab
->n_div
))
2049 isl_int_fdiv_r(r_row
[off
+ i
],
2050 tab
->mat
->row
[row
][off
+ i
], tab
->mat
->row
[row
][0]);
2052 if (tab
->var
[tab
->n_var
- tab
->n_div
+ d
].is_row
) {
2054 int d_row
= tab
->var
[tab
->n_var
- tab
->n_div
+ d
].index
;
2056 isl_int_gcd(gcd
, tab
->mat
->row
[d_row
][0], r_row
[0]);
2057 isl_int_divexact(r_row
[0], r_row
[0], gcd
);
2058 isl_int_divexact(gcd
, tab
->mat
->row
[d_row
][0], gcd
);
2059 isl_seq_combine(r_row
+ 1, gcd
, r_row
+ 1,
2060 r_row
[0], tab
->mat
->row
[d_row
] + 1,
2061 off
- 1 + tab
->n_col
);
2062 isl_int_mul(r_row
[0], r_row
[0], tab
->mat
->row
[d_row
][0]);
2065 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ d
].index
;
2066 isl_int_set(r_row
[off
+ col
], tab
->mat
->row
[row
][0]);
2069 tab
->con
[r
].is_nonneg
= 1;
2070 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
2073 tab
->row_sign
[tab
->con
[r
].index
] = isl_tab_row_neg
;
2075 row
= tab
->con
[r
].index
;
2077 if (d
>= n
&& context
->op
->detect_equalities(context
, tab
) < 0)
2083 /* Construct a tableau for bmap that can be used for computing
2084 * the lexicographic minimum (or maximum) of bmap.
2085 * If not NULL, then dom is the domain where the minimum
2086 * should be computed. In this case, we set up a parametric
2087 * tableau with row signs (initialized to "unknown").
2088 * If M is set, then the tableau will use a big parameter.
2089 * If max is set, then a maximum should be computed instead of a minimum.
2090 * This means that for each variable x, the tableau will contain the variable
2091 * x' = M - x, rather than x' = M + x. This in turn means that the coefficient
2092 * of the variables in all constraints are negated prior to adding them
2095 static struct isl_tab
*tab_for_lexmin(struct isl_basic_map
*bmap
,
2096 struct isl_basic_set
*dom
, unsigned M
, int max
)
2099 struct isl_tab
*tab
;
2103 tab
= isl_tab_alloc(bmap
->ctx
, 2 * bmap
->n_eq
+ bmap
->n_ineq
+ 1,
2104 isl_basic_map_total_dim(bmap
), M
);
2108 tab
->rational
= ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
2110 tab
->n_param
= isl_basic_set_total_dim(dom
) - dom
->n_div
;
2111 tab
->n_div
= dom
->n_div
;
2112 tab
->row_sign
= isl_calloc_array(bmap
->ctx
,
2113 enum isl_tab_row_sign
, tab
->mat
->n_row
);
2114 if (tab
->mat
->n_row
&& !tab
->row_sign
)
2117 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
)) {
2118 if (isl_tab_mark_empty(tab
) < 0)
2123 for (i
= tab
->n_param
; i
< tab
->n_var
- tab
->n_div
; ++i
) {
2124 tab
->var
[i
].is_nonneg
= 1;
2125 tab
->var
[i
].frozen
= 1;
2127 o_var
= 1 + tab
->n_param
;
2128 n_var
= tab
->n_var
- tab
->n_param
- tab
->n_div
;
2129 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
2131 isl_seq_neg(bmap
->eq
[i
] + o_var
,
2132 bmap
->eq
[i
] + o_var
, n_var
);
2133 tab
= add_lexmin_valid_eq(tab
, bmap
->eq
[i
]);
2135 isl_seq_neg(bmap
->eq
[i
] + o_var
,
2136 bmap
->eq
[i
] + o_var
, n_var
);
2137 if (!tab
|| tab
->empty
)
2140 if (bmap
->n_eq
&& restore_lexmin(tab
) < 0)
2142 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
2144 isl_seq_neg(bmap
->ineq
[i
] + o_var
,
2145 bmap
->ineq
[i
] + o_var
, n_var
);
2146 tab
= add_lexmin_ineq(tab
, bmap
->ineq
[i
]);
2148 isl_seq_neg(bmap
->ineq
[i
] + o_var
,
2149 bmap
->ineq
[i
] + o_var
, n_var
);
2150 if (!tab
|| tab
->empty
)
2159 /* Given a main tableau where more than one row requires a split,
2160 * determine and return the "best" row to split on.
2162 * Given two rows in the main tableau, if the inequality corresponding
2163 * to the first row is redundant with respect to that of the second row
2164 * in the current tableau, then it is better to split on the second row,
2165 * since in the positive part, both rows will be positive.
2166 * (In the negative part a pivot will have to be performed and just about
2167 * anything can happen to the sign of the other row.)
2169 * As a simple heuristic, we therefore select the row that makes the most
2170 * of the other rows redundant.
2172 * Perhaps it would also be useful to look at the number of constraints
2173 * that conflict with any given constraint.
2175 * best is the best row so far (-1 when we have not found any row yet).
2176 * best_r is the number of other rows made redundant by row best.
2177 * When best is still -1, bset_r is meaningless, but it is initialized
2178 * to some arbitrary value (0) anyway. Without this redundant initialization
2179 * valgrind may warn about uninitialized memory accesses when isl
2180 * is compiled with some versions of gcc.
2182 static int best_split(struct isl_tab
*tab
, struct isl_tab
*context_tab
)
2184 struct isl_tab_undo
*snap
;
2190 if (isl_tab_extend_cons(context_tab
, 2) < 0)
2193 snap
= isl_tab_snap(context_tab
);
2195 for (split
= tab
->n_redundant
; split
< tab
->n_row
; ++split
) {
2196 struct isl_tab_undo
*snap2
;
2197 struct isl_vec
*ineq
= NULL
;
2201 if (!isl_tab_var_from_row(tab
, split
)->is_nonneg
)
2203 if (tab
->row_sign
[split
] != isl_tab_row_any
)
2206 ineq
= get_row_parameter_ineq(tab
, split
);
2209 ok
= isl_tab_add_ineq(context_tab
, ineq
->el
) >= 0;
2214 snap2
= isl_tab_snap(context_tab
);
2216 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
2217 struct isl_tab_var
*var
;
2221 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
2223 if (tab
->row_sign
[row
] != isl_tab_row_any
)
2226 ineq
= get_row_parameter_ineq(tab
, row
);
2229 ok
= isl_tab_add_ineq(context_tab
, ineq
->el
) >= 0;
2233 var
= &context_tab
->con
[context_tab
->n_con
- 1];
2234 if (!context_tab
->empty
&&
2235 !isl_tab_min_at_most_neg_one(context_tab
, var
))
2237 if (isl_tab_rollback(context_tab
, snap2
) < 0)
2240 if (best
== -1 || r
> best_r
) {
2244 if (isl_tab_rollback(context_tab
, snap
) < 0)
2251 static struct isl_basic_set
*context_lex_peek_basic_set(
2252 struct isl_context
*context
)
2254 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2257 return isl_tab_peek_bset(clex
->tab
);
2260 static struct isl_tab
*context_lex_peek_tab(struct isl_context
*context
)
2262 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2266 static void context_lex_add_eq(struct isl_context
*context
, isl_int
*eq
,
2267 int check
, int update
)
2269 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2270 if (isl_tab_extend_cons(clex
->tab
, 2) < 0)
2272 if (add_lexmin_eq(clex
->tab
, eq
) < 0)
2275 int v
= tab_has_valid_sample(clex
->tab
, eq
, 1);
2279 clex
->tab
= check_integer_feasible(clex
->tab
);
2282 clex
->tab
= check_samples(clex
->tab
, eq
, 1);
2285 isl_tab_free(clex
->tab
);
2289 static void context_lex_add_ineq(struct isl_context
*context
, isl_int
*ineq
,
2290 int check
, int update
)
2292 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2293 if (isl_tab_extend_cons(clex
->tab
, 1) < 0)
2295 clex
->tab
= add_lexmin_ineq(clex
->tab
, ineq
);
2297 int v
= tab_has_valid_sample(clex
->tab
, ineq
, 0);
2301 clex
->tab
= check_integer_feasible(clex
->tab
);
2304 clex
->tab
= check_samples(clex
->tab
, ineq
, 0);
2307 isl_tab_free(clex
->tab
);
2311 static int context_lex_add_ineq_wrap(void *user
, isl_int
*ineq
)
2313 struct isl_context
*context
= (struct isl_context
*)user
;
2314 context_lex_add_ineq(context
, ineq
, 0, 0);
2315 return context
->op
->is_ok(context
) ? 0 : -1;
2318 /* Check which signs can be obtained by "ineq" on all the currently
2319 * active sample values. See row_sign for more information.
2321 static enum isl_tab_row_sign
tab_ineq_sign(struct isl_tab
*tab
, isl_int
*ineq
,
2327 enum isl_tab_row_sign res
= isl_tab_row_unknown
;
2329 isl_assert(tab
->mat
->ctx
, tab
->samples
, return isl_tab_row_unknown
);
2330 isl_assert(tab
->mat
->ctx
, tab
->samples
->n_col
== 1 + tab
->n_var
,
2331 return isl_tab_row_unknown
);
2334 for (i
= tab
->n_outside
; i
< tab
->n_sample
; ++i
) {
2335 isl_seq_inner_product(tab
->samples
->row
[i
], ineq
,
2336 1 + tab
->n_var
, &tmp
);
2337 sgn
= isl_int_sgn(tmp
);
2338 if (sgn
> 0 || (sgn
== 0 && strict
)) {
2339 if (res
== isl_tab_row_unknown
)
2340 res
= isl_tab_row_pos
;
2341 if (res
== isl_tab_row_neg
)
2342 res
= isl_tab_row_any
;
2345 if (res
== isl_tab_row_unknown
)
2346 res
= isl_tab_row_neg
;
2347 if (res
== isl_tab_row_pos
)
2348 res
= isl_tab_row_any
;
2350 if (res
== isl_tab_row_any
)
2358 static enum isl_tab_row_sign
context_lex_ineq_sign(struct isl_context
*context
,
2359 isl_int
*ineq
, int strict
)
2361 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2362 return tab_ineq_sign(clex
->tab
, ineq
, strict
);
2365 /* Check whether "ineq" can be added to the tableau without rendering
2368 static int context_lex_test_ineq(struct isl_context
*context
, isl_int
*ineq
)
2370 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2371 struct isl_tab_undo
*snap
;
2377 if (isl_tab_extend_cons(clex
->tab
, 1) < 0)
2380 snap
= isl_tab_snap(clex
->tab
);
2381 if (isl_tab_push_basis(clex
->tab
) < 0)
2383 clex
->tab
= add_lexmin_ineq(clex
->tab
, ineq
);
2384 clex
->tab
= check_integer_feasible(clex
->tab
);
2387 feasible
= !clex
->tab
->empty
;
2388 if (isl_tab_rollback(clex
->tab
, snap
) < 0)
2394 static int context_lex_get_div(struct isl_context
*context
, struct isl_tab
*tab
,
2395 struct isl_vec
*div
)
2397 return get_div(tab
, context
, div
);
2400 /* Add a div specified by "div" to the context tableau and return
2401 * isl_bool_true if the div is obviously non-negative.
2402 * context_tab_add_div will always return isl_bool_true, because all variables
2403 * in a isl_context_lex tableau are non-negative.
2404 * However, if we are using a big parameter in the context, then this only
2405 * reflects the non-negativity of the variable used to _encode_ the
2406 * div, i.e., div' = M + div, so we can't draw any conclusions.
2408 static isl_bool
context_lex_add_div(struct isl_context
*context
,
2409 __isl_keep isl_vec
*div
)
2411 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2413 nonneg
= context_tab_add_div(clex
->tab
, div
,
2414 context_lex_add_ineq_wrap
, context
);
2416 return isl_bool_error
;
2418 return isl_bool_false
;
2422 static int context_lex_detect_equalities(struct isl_context
*context
,
2423 struct isl_tab
*tab
)
2428 static int context_lex_best_split(struct isl_context
*context
,
2429 struct isl_tab
*tab
)
2431 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2432 struct isl_tab_undo
*snap
;
2435 snap
= isl_tab_snap(clex
->tab
);
2436 if (isl_tab_push_basis(clex
->tab
) < 0)
2438 r
= best_split(tab
, clex
->tab
);
2440 if (r
>= 0 && isl_tab_rollback(clex
->tab
, snap
) < 0)
2446 static int context_lex_is_empty(struct isl_context
*context
)
2448 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2451 return clex
->tab
->empty
;
2454 static void *context_lex_save(struct isl_context
*context
)
2456 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2457 struct isl_tab_undo
*snap
;
2459 snap
= isl_tab_snap(clex
->tab
);
2460 if (isl_tab_push_basis(clex
->tab
) < 0)
2462 if (isl_tab_save_samples(clex
->tab
) < 0)
2468 static void context_lex_restore(struct isl_context
*context
, void *save
)
2470 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2471 if (isl_tab_rollback(clex
->tab
, (struct isl_tab_undo
*)save
) < 0) {
2472 isl_tab_free(clex
->tab
);
2477 static void context_lex_discard(void *save
)
2481 static int context_lex_is_ok(struct isl_context
*context
)
2483 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2487 /* For each variable in the context tableau, check if the variable can
2488 * only attain non-negative values. If so, mark the parameter as non-negative
2489 * in the main tableau. This allows for a more direct identification of some
2490 * cases of violated constraints.
2492 static struct isl_tab
*tab_detect_nonnegative_parameters(struct isl_tab
*tab
,
2493 struct isl_tab
*context_tab
)
2496 struct isl_tab_undo
*snap
;
2497 struct isl_vec
*ineq
= NULL
;
2498 struct isl_tab_var
*var
;
2501 if (context_tab
->n_var
== 0)
2504 ineq
= isl_vec_alloc(tab
->mat
->ctx
, 1 + context_tab
->n_var
);
2508 if (isl_tab_extend_cons(context_tab
, 1) < 0)
2511 snap
= isl_tab_snap(context_tab
);
2514 isl_seq_clr(ineq
->el
, ineq
->size
);
2515 for (i
= 0; i
< context_tab
->n_var
; ++i
) {
2516 isl_int_set_si(ineq
->el
[1 + i
], 1);
2517 if (isl_tab_add_ineq(context_tab
, ineq
->el
) < 0)
2519 var
= &context_tab
->con
[context_tab
->n_con
- 1];
2520 if (!context_tab
->empty
&&
2521 !isl_tab_min_at_most_neg_one(context_tab
, var
)) {
2523 if (i
>= tab
->n_param
)
2524 j
= i
- tab
->n_param
+ tab
->n_var
- tab
->n_div
;
2525 tab
->var
[j
].is_nonneg
= 1;
2528 isl_int_set_si(ineq
->el
[1 + i
], 0);
2529 if (isl_tab_rollback(context_tab
, snap
) < 0)
2533 if (context_tab
->M
&& n
== context_tab
->n_var
) {
2534 context_tab
->mat
= isl_mat_drop_cols(context_tab
->mat
, 2, 1);
2546 static struct isl_tab
*context_lex_detect_nonnegative_parameters(
2547 struct isl_context
*context
, struct isl_tab
*tab
)
2549 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2550 struct isl_tab_undo
*snap
;
2555 snap
= isl_tab_snap(clex
->tab
);
2556 if (isl_tab_push_basis(clex
->tab
) < 0)
2559 tab
= tab_detect_nonnegative_parameters(tab
, clex
->tab
);
2561 if (isl_tab_rollback(clex
->tab
, snap
) < 0)
2570 static void context_lex_invalidate(struct isl_context
*context
)
2572 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2573 isl_tab_free(clex
->tab
);
2577 static void context_lex_free(struct isl_context
*context
)
2579 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2580 isl_tab_free(clex
->tab
);
2584 struct isl_context_op isl_context_lex_op
= {
2585 context_lex_detect_nonnegative_parameters
,
2586 context_lex_peek_basic_set
,
2587 context_lex_peek_tab
,
2589 context_lex_add_ineq
,
2590 context_lex_ineq_sign
,
2591 context_lex_test_ineq
,
2592 context_lex_get_div
,
2593 context_lex_add_div
,
2594 context_lex_detect_equalities
,
2595 context_lex_best_split
,
2596 context_lex_is_empty
,
2599 context_lex_restore
,
2600 context_lex_discard
,
2601 context_lex_invalidate
,
2605 static struct isl_tab
*context_tab_for_lexmin(struct isl_basic_set
*bset
)
2607 struct isl_tab
*tab
;
2611 tab
= tab_for_lexmin((struct isl_basic_map
*)bset
, NULL
, 1, 0);
2614 if (isl_tab_track_bset(tab
, bset
) < 0)
2616 tab
= isl_tab_init_samples(tab
);
2619 isl_basic_set_free(bset
);
2623 static struct isl_context
*isl_context_lex_alloc(struct isl_basic_set
*dom
)
2625 struct isl_context_lex
*clex
;
2630 clex
= isl_alloc_type(dom
->ctx
, struct isl_context_lex
);
2634 clex
->context
.op
= &isl_context_lex_op
;
2636 clex
->tab
= context_tab_for_lexmin(isl_basic_set_copy(dom
));
2637 if (restore_lexmin(clex
->tab
) < 0)
2639 clex
->tab
= check_integer_feasible(clex
->tab
);
2643 return &clex
->context
;
2645 clex
->context
.op
->free(&clex
->context
);
2649 /* Representation of the context when using generalized basis reduction.
2651 * "shifted" contains the offsets of the unit hypercubes that lie inside the
2652 * context. Any rational point in "shifted" can therefore be rounded
2653 * up to an integer point in the context.
2654 * If the context is constrained by any equality, then "shifted" is not used
2655 * as it would be empty.
2657 struct isl_context_gbr
{
2658 struct isl_context context
;
2659 struct isl_tab
*tab
;
2660 struct isl_tab
*shifted
;
2661 struct isl_tab
*cone
;
2664 static struct isl_tab
*context_gbr_detect_nonnegative_parameters(
2665 struct isl_context
*context
, struct isl_tab
*tab
)
2667 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2670 return tab_detect_nonnegative_parameters(tab
, cgbr
->tab
);
2673 static struct isl_basic_set
*context_gbr_peek_basic_set(
2674 struct isl_context
*context
)
2676 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2679 return isl_tab_peek_bset(cgbr
->tab
);
2682 static struct isl_tab
*context_gbr_peek_tab(struct isl_context
*context
)
2684 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2688 /* Initialize the "shifted" tableau of the context, which
2689 * contains the constraints of the original tableau shifted
2690 * by the sum of all negative coefficients. This ensures
2691 * that any rational point in the shifted tableau can
2692 * be rounded up to yield an integer point in the original tableau.
2694 static void gbr_init_shifted(struct isl_context_gbr
*cgbr
)
2697 struct isl_vec
*cst
;
2698 struct isl_basic_set
*bset
= isl_tab_peek_bset(cgbr
->tab
);
2699 unsigned dim
= isl_basic_set_total_dim(bset
);
2701 cst
= isl_vec_alloc(cgbr
->tab
->mat
->ctx
, bset
->n_ineq
);
2705 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
2706 isl_int_set(cst
->el
[i
], bset
->ineq
[i
][0]);
2707 for (j
= 0; j
< dim
; ++j
) {
2708 if (!isl_int_is_neg(bset
->ineq
[i
][1 + j
]))
2710 isl_int_add(bset
->ineq
[i
][0], bset
->ineq
[i
][0],
2711 bset
->ineq
[i
][1 + j
]);
2715 cgbr
->shifted
= isl_tab_from_basic_set(bset
, 0);
2717 for (i
= 0; i
< bset
->n_ineq
; ++i
)
2718 isl_int_set(bset
->ineq
[i
][0], cst
->el
[i
]);
2723 /* Check if the shifted tableau is non-empty, and if so
2724 * use the sample point to construct an integer point
2725 * of the context tableau.
2727 static struct isl_vec
*gbr_get_shifted_sample(struct isl_context_gbr
*cgbr
)
2729 struct isl_vec
*sample
;
2732 gbr_init_shifted(cgbr
);
2735 if (cgbr
->shifted
->empty
)
2736 return isl_vec_alloc(cgbr
->tab
->mat
->ctx
, 0);
2738 sample
= isl_tab_get_sample_value(cgbr
->shifted
);
2739 sample
= isl_vec_ceil(sample
);
2744 static struct isl_basic_set
*drop_constant_terms(struct isl_basic_set
*bset
)
2751 for (i
= 0; i
< bset
->n_eq
; ++i
)
2752 isl_int_set_si(bset
->eq
[i
][0], 0);
2754 for (i
= 0; i
< bset
->n_ineq
; ++i
)
2755 isl_int_set_si(bset
->ineq
[i
][0], 0);
2760 static int use_shifted(struct isl_context_gbr
*cgbr
)
2764 return cgbr
->tab
->bmap
->n_eq
== 0 && cgbr
->tab
->bmap
->n_div
== 0;
2767 static struct isl_vec
*gbr_get_sample(struct isl_context_gbr
*cgbr
)
2769 struct isl_basic_set
*bset
;
2770 struct isl_basic_set
*cone
;
2772 if (isl_tab_sample_is_integer(cgbr
->tab
))
2773 return isl_tab_get_sample_value(cgbr
->tab
);
2775 if (use_shifted(cgbr
)) {
2776 struct isl_vec
*sample
;
2778 sample
= gbr_get_shifted_sample(cgbr
);
2779 if (!sample
|| sample
->size
> 0)
2782 isl_vec_free(sample
);
2786 bset
= isl_tab_peek_bset(cgbr
->tab
);
2787 cgbr
->cone
= isl_tab_from_recession_cone(bset
, 0);
2790 if (isl_tab_track_bset(cgbr
->cone
,
2791 isl_basic_set_copy(bset
)) < 0)
2794 if (isl_tab_detect_implicit_equalities(cgbr
->cone
) < 0)
2797 if (cgbr
->cone
->n_dead
== cgbr
->cone
->n_col
) {
2798 struct isl_vec
*sample
;
2799 struct isl_tab_undo
*snap
;
2801 if (cgbr
->tab
->basis
) {
2802 if (cgbr
->tab
->basis
->n_col
!= 1 + cgbr
->tab
->n_var
) {
2803 isl_mat_free(cgbr
->tab
->basis
);
2804 cgbr
->tab
->basis
= NULL
;
2806 cgbr
->tab
->n_zero
= 0;
2807 cgbr
->tab
->n_unbounded
= 0;
2810 snap
= isl_tab_snap(cgbr
->tab
);
2812 sample
= isl_tab_sample(cgbr
->tab
);
2814 if (!sample
|| isl_tab_rollback(cgbr
->tab
, snap
) < 0) {
2815 isl_vec_free(sample
);
2822 cone
= isl_basic_set_dup(isl_tab_peek_bset(cgbr
->cone
));
2823 cone
= drop_constant_terms(cone
);
2824 cone
= isl_basic_set_update_from_tab(cone
, cgbr
->cone
);
2825 cone
= isl_basic_set_underlying_set(cone
);
2826 cone
= isl_basic_set_gauss(cone
, NULL
);
2828 bset
= isl_basic_set_dup(isl_tab_peek_bset(cgbr
->tab
));
2829 bset
= isl_basic_set_update_from_tab(bset
, cgbr
->tab
);
2830 bset
= isl_basic_set_underlying_set(bset
);
2831 bset
= isl_basic_set_gauss(bset
, NULL
);
2833 return isl_basic_set_sample_with_cone(bset
, cone
);
2836 static void check_gbr_integer_feasible(struct isl_context_gbr
*cgbr
)
2838 struct isl_vec
*sample
;
2843 if (cgbr
->tab
->empty
)
2846 sample
= gbr_get_sample(cgbr
);
2850 if (sample
->size
== 0) {
2851 isl_vec_free(sample
);
2852 if (isl_tab_mark_empty(cgbr
->tab
) < 0)
2857 if (isl_tab_add_sample(cgbr
->tab
, sample
) < 0)
2862 isl_tab_free(cgbr
->tab
);
2866 static struct isl_tab
*add_gbr_eq(struct isl_tab
*tab
, isl_int
*eq
)
2871 if (isl_tab_extend_cons(tab
, 2) < 0)
2874 if (isl_tab_add_eq(tab
, eq
) < 0)
2883 /* Add the equality described by "eq" to the context.
2884 * If "check" is set, then we check if the context is empty after
2885 * adding the equality.
2886 * If "update" is set, then we check if the samples are still valid.
2888 * We do not explicitly add shifted copies of the equality to
2889 * cgbr->shifted since they would conflict with each other.
2890 * Instead, we directly mark cgbr->shifted empty.
2892 static void context_gbr_add_eq(struct isl_context
*context
, isl_int
*eq
,
2893 int check
, int update
)
2895 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2897 cgbr
->tab
= add_gbr_eq(cgbr
->tab
, eq
);
2899 if (cgbr
->shifted
&& !cgbr
->shifted
->empty
&& use_shifted(cgbr
)) {
2900 if (isl_tab_mark_empty(cgbr
->shifted
) < 0)
2904 if (cgbr
->cone
&& cgbr
->cone
->n_col
!= cgbr
->cone
->n_dead
) {
2905 if (isl_tab_extend_cons(cgbr
->cone
, 2) < 0)
2907 if (isl_tab_add_eq(cgbr
->cone
, eq
) < 0)
2912 int v
= tab_has_valid_sample(cgbr
->tab
, eq
, 1);
2916 check_gbr_integer_feasible(cgbr
);
2919 cgbr
->tab
= check_samples(cgbr
->tab
, eq
, 1);
2922 isl_tab_free(cgbr
->tab
);
2926 static void add_gbr_ineq(struct isl_context_gbr
*cgbr
, isl_int
*ineq
)
2931 if (isl_tab_extend_cons(cgbr
->tab
, 1) < 0)
2934 if (isl_tab_add_ineq(cgbr
->tab
, ineq
) < 0)
2937 if (cgbr
->shifted
&& !cgbr
->shifted
->empty
&& use_shifted(cgbr
)) {
2940 dim
= isl_basic_map_total_dim(cgbr
->tab
->bmap
);
2942 if (isl_tab_extend_cons(cgbr
->shifted
, 1) < 0)
2945 for (i
= 0; i
< dim
; ++i
) {
2946 if (!isl_int_is_neg(ineq
[1 + i
]))
2948 isl_int_add(ineq
[0], ineq
[0], ineq
[1 + i
]);
2951 if (isl_tab_add_ineq(cgbr
->shifted
, ineq
) < 0)
2954 for (i
= 0; i
< dim
; ++i
) {
2955 if (!isl_int_is_neg(ineq
[1 + i
]))
2957 isl_int_sub(ineq
[0], ineq
[0], ineq
[1 + i
]);
2961 if (cgbr
->cone
&& cgbr
->cone
->n_col
!= cgbr
->cone
->n_dead
) {
2962 if (isl_tab_extend_cons(cgbr
->cone
, 1) < 0)
2964 if (isl_tab_add_ineq(cgbr
->cone
, ineq
) < 0)
2970 isl_tab_free(cgbr
->tab
);
2974 static void context_gbr_add_ineq(struct isl_context
*context
, isl_int
*ineq
,
2975 int check
, int update
)
2977 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2979 add_gbr_ineq(cgbr
, ineq
);
2984 int v
= tab_has_valid_sample(cgbr
->tab
, ineq
, 0);
2988 check_gbr_integer_feasible(cgbr
);
2991 cgbr
->tab
= check_samples(cgbr
->tab
, ineq
, 0);
2994 isl_tab_free(cgbr
->tab
);
2998 static int context_gbr_add_ineq_wrap(void *user
, isl_int
*ineq
)
3000 struct isl_context
*context
= (struct isl_context
*)user
;
3001 context_gbr_add_ineq(context
, ineq
, 0, 0);
3002 return context
->op
->is_ok(context
) ? 0 : -1;
3005 static enum isl_tab_row_sign
context_gbr_ineq_sign(struct isl_context
*context
,
3006 isl_int
*ineq
, int strict
)
3008 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3009 return tab_ineq_sign(cgbr
->tab
, ineq
, strict
);
3012 /* Check whether "ineq" can be added to the tableau without rendering
3015 static int context_gbr_test_ineq(struct isl_context
*context
, isl_int
*ineq
)
3017 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3018 struct isl_tab_undo
*snap
;
3019 struct isl_tab_undo
*shifted_snap
= NULL
;
3020 struct isl_tab_undo
*cone_snap
= NULL
;
3026 if (isl_tab_extend_cons(cgbr
->tab
, 1) < 0)
3029 snap
= isl_tab_snap(cgbr
->tab
);
3031 shifted_snap
= isl_tab_snap(cgbr
->shifted
);
3033 cone_snap
= isl_tab_snap(cgbr
->cone
);
3034 add_gbr_ineq(cgbr
, ineq
);
3035 check_gbr_integer_feasible(cgbr
);
3038 feasible
= !cgbr
->tab
->empty
;
3039 if (isl_tab_rollback(cgbr
->tab
, snap
) < 0)
3042 if (isl_tab_rollback(cgbr
->shifted
, shifted_snap
))
3044 } else if (cgbr
->shifted
) {
3045 isl_tab_free(cgbr
->shifted
);
3046 cgbr
->shifted
= NULL
;
3049 if (isl_tab_rollback(cgbr
->cone
, cone_snap
))
3051 } else if (cgbr
->cone
) {
3052 isl_tab_free(cgbr
->cone
);
3059 /* Return the column of the last of the variables associated to
3060 * a column that has a non-zero coefficient.
3061 * This function is called in a context where only coefficients
3062 * of parameters or divs can be non-zero.
3064 static int last_non_zero_var_col(struct isl_tab
*tab
, isl_int
*p
)
3069 if (tab
->n_var
== 0)
3072 for (i
= tab
->n_var
- 1; i
>= 0; --i
) {
3073 if (i
>= tab
->n_param
&& i
< tab
->n_var
- tab
->n_div
)
3075 if (tab
->var
[i
].is_row
)
3077 col
= tab
->var
[i
].index
;
3078 if (!isl_int_is_zero(p
[col
]))
3085 /* Look through all the recently added equalities in the context
3086 * to see if we can propagate any of them to the main tableau.
3088 * The newly added equalities in the context are encoded as pairs
3089 * of inequalities starting at inequality "first".
3091 * We tentatively add each of these equalities to the main tableau
3092 * and if this happens to result in a row with a final coefficient
3093 * that is one or negative one, we use it to kill a column
3094 * in the main tableau. Otherwise, we discard the tentatively
3096 * This tentative addition of equality constraints turns
3097 * on the undo facility of the tableau. Turn it off again
3098 * at the end, assuming it was turned off to begin with.
3100 * Return 0 on success and -1 on failure.
3102 static int propagate_equalities(struct isl_context_gbr
*cgbr
,
3103 struct isl_tab
*tab
, unsigned first
)
3106 struct isl_vec
*eq
= NULL
;
3107 isl_bool needs_undo
;
3109 needs_undo
= isl_tab_need_undo(tab
);
3112 eq
= isl_vec_alloc(tab
->mat
->ctx
, 1 + tab
->n_var
);
3116 if (isl_tab_extend_cons(tab
, (cgbr
->tab
->bmap
->n_ineq
- first
)/2) < 0)
3119 isl_seq_clr(eq
->el
+ 1 + tab
->n_param
,
3120 tab
->n_var
- tab
->n_param
- tab
->n_div
);
3121 for (i
= first
; i
< cgbr
->tab
->bmap
->n_ineq
; i
+= 2) {
3124 struct isl_tab_undo
*snap
;
3125 snap
= isl_tab_snap(tab
);
3127 isl_seq_cpy(eq
->el
, cgbr
->tab
->bmap
->ineq
[i
], 1 + tab
->n_param
);
3128 isl_seq_cpy(eq
->el
+ 1 + tab
->n_var
- tab
->n_div
,
3129 cgbr
->tab
->bmap
->ineq
[i
] + 1 + tab
->n_param
,
3132 r
= isl_tab_add_row(tab
, eq
->el
);
3135 r
= tab
->con
[r
].index
;
3136 j
= last_non_zero_var_col(tab
, tab
->mat
->row
[r
] + 2 + tab
->M
);
3137 if (j
< 0 || j
< tab
->n_dead
||
3138 !isl_int_is_one(tab
->mat
->row
[r
][0]) ||
3139 (!isl_int_is_one(tab
->mat
->row
[r
][2 + tab
->M
+ j
]) &&
3140 !isl_int_is_negone(tab
->mat
->row
[r
][2 + tab
->M
+ j
]))) {
3141 if (isl_tab_rollback(tab
, snap
) < 0)
3145 if (isl_tab_pivot(tab
, r
, j
) < 0)
3147 if (isl_tab_kill_col(tab
, j
) < 0)
3150 if (restore_lexmin(tab
) < 0)
3155 isl_tab_clear_undo(tab
);
3161 isl_tab_free(cgbr
->tab
);
3166 static int context_gbr_detect_equalities(struct isl_context
*context
,
3167 struct isl_tab
*tab
)
3169 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3173 struct isl_basic_set
*bset
= isl_tab_peek_bset(cgbr
->tab
);
3174 cgbr
->cone
= isl_tab_from_recession_cone(bset
, 0);
3177 if (isl_tab_track_bset(cgbr
->cone
,
3178 isl_basic_set_copy(bset
)) < 0)
3181 if (isl_tab_detect_implicit_equalities(cgbr
->cone
) < 0)
3184 n_ineq
= cgbr
->tab
->bmap
->n_ineq
;
3185 cgbr
->tab
= isl_tab_detect_equalities(cgbr
->tab
, cgbr
->cone
);
3188 if (cgbr
->tab
->bmap
->n_ineq
> n_ineq
&&
3189 propagate_equalities(cgbr
, tab
, n_ineq
) < 0)
3194 isl_tab_free(cgbr
->tab
);
3199 static int context_gbr_get_div(struct isl_context
*context
, struct isl_tab
*tab
,
3200 struct isl_vec
*div
)
3202 return get_div(tab
, context
, div
);
3205 static isl_bool
context_gbr_add_div(struct isl_context
*context
,
3206 __isl_keep isl_vec
*div
)
3208 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3212 if (isl_tab_extend_cons(cgbr
->cone
, 3) < 0)
3213 return isl_bool_error
;
3214 if (isl_tab_extend_vars(cgbr
->cone
, 1) < 0)
3215 return isl_bool_error
;
3216 if (isl_tab_allocate_var(cgbr
->cone
) <0)
3217 return isl_bool_error
;
3219 cgbr
->cone
->bmap
= isl_basic_map_extend_space(cgbr
->cone
->bmap
,
3220 isl_basic_map_get_space(cgbr
->cone
->bmap
), 1, 0, 2);
3221 k
= isl_basic_map_alloc_div(cgbr
->cone
->bmap
);
3223 return isl_bool_error
;
3224 isl_seq_cpy(cgbr
->cone
->bmap
->div
[k
], div
->el
, div
->size
);
3225 if (isl_tab_push(cgbr
->cone
, isl_tab_undo_bmap_div
) < 0)
3226 return isl_bool_error
;
3228 return context_tab_add_div(cgbr
->tab
, div
,
3229 context_gbr_add_ineq_wrap
, context
);
3232 static int context_gbr_best_split(struct isl_context
*context
,
3233 struct isl_tab
*tab
)
3235 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3236 struct isl_tab_undo
*snap
;
3239 snap
= isl_tab_snap(cgbr
->tab
);
3240 r
= best_split(tab
, cgbr
->tab
);
3242 if (r
>= 0 && isl_tab_rollback(cgbr
->tab
, snap
) < 0)
3248 static int context_gbr_is_empty(struct isl_context
*context
)
3250 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3253 return cgbr
->tab
->empty
;
3256 struct isl_gbr_tab_undo
{
3257 struct isl_tab_undo
*tab_snap
;
3258 struct isl_tab_undo
*shifted_snap
;
3259 struct isl_tab_undo
*cone_snap
;
3262 static void *context_gbr_save(struct isl_context
*context
)
3264 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3265 struct isl_gbr_tab_undo
*snap
;
3270 snap
= isl_alloc_type(cgbr
->tab
->mat
->ctx
, struct isl_gbr_tab_undo
);
3274 snap
->tab_snap
= isl_tab_snap(cgbr
->tab
);
3275 if (isl_tab_save_samples(cgbr
->tab
) < 0)
3279 snap
->shifted_snap
= isl_tab_snap(cgbr
->shifted
);
3281 snap
->shifted_snap
= NULL
;
3284 snap
->cone_snap
= isl_tab_snap(cgbr
->cone
);
3286 snap
->cone_snap
= NULL
;
3294 static void context_gbr_restore(struct isl_context
*context
, void *save
)
3296 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3297 struct isl_gbr_tab_undo
*snap
= (struct isl_gbr_tab_undo
*)save
;
3300 if (isl_tab_rollback(cgbr
->tab
, snap
->tab_snap
) < 0)
3303 if (snap
->shifted_snap
) {
3304 if (isl_tab_rollback(cgbr
->shifted
, snap
->shifted_snap
) < 0)
3306 } else if (cgbr
->shifted
) {
3307 isl_tab_free(cgbr
->shifted
);
3308 cgbr
->shifted
= NULL
;
3311 if (snap
->cone_snap
) {
3312 if (isl_tab_rollback(cgbr
->cone
, snap
->cone_snap
) < 0)
3314 } else if (cgbr
->cone
) {
3315 isl_tab_free(cgbr
->cone
);
3324 isl_tab_free(cgbr
->tab
);
3328 static void context_gbr_discard(void *save
)
3330 struct isl_gbr_tab_undo
*snap
= (struct isl_gbr_tab_undo
*)save
;
3334 static int context_gbr_is_ok(struct isl_context
*context
)
3336 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3340 static void context_gbr_invalidate(struct isl_context
*context
)
3342 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3343 isl_tab_free(cgbr
->tab
);
3347 static void context_gbr_free(struct isl_context
*context
)
3349 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3350 isl_tab_free(cgbr
->tab
);
3351 isl_tab_free(cgbr
->shifted
);
3352 isl_tab_free(cgbr
->cone
);
3356 struct isl_context_op isl_context_gbr_op
= {
3357 context_gbr_detect_nonnegative_parameters
,
3358 context_gbr_peek_basic_set
,
3359 context_gbr_peek_tab
,
3361 context_gbr_add_ineq
,
3362 context_gbr_ineq_sign
,
3363 context_gbr_test_ineq
,
3364 context_gbr_get_div
,
3365 context_gbr_add_div
,
3366 context_gbr_detect_equalities
,
3367 context_gbr_best_split
,
3368 context_gbr_is_empty
,
3371 context_gbr_restore
,
3372 context_gbr_discard
,
3373 context_gbr_invalidate
,
3377 static struct isl_context
*isl_context_gbr_alloc(__isl_keep isl_basic_set
*dom
)
3379 struct isl_context_gbr
*cgbr
;
3384 cgbr
= isl_calloc_type(dom
->ctx
, struct isl_context_gbr
);
3388 cgbr
->context
.op
= &isl_context_gbr_op
;
3390 cgbr
->shifted
= NULL
;
3392 cgbr
->tab
= isl_tab_from_basic_set(dom
, 1);
3393 cgbr
->tab
= isl_tab_init_samples(cgbr
->tab
);
3396 check_gbr_integer_feasible(cgbr
);
3398 return &cgbr
->context
;
3400 cgbr
->context
.op
->free(&cgbr
->context
);
3404 static struct isl_context
*isl_context_alloc(__isl_keep isl_basic_set
*dom
)
3409 if (dom
->ctx
->opt
->context
== ISL_CONTEXT_LEXMIN
)
3410 return isl_context_lex_alloc(dom
);
3412 return isl_context_gbr_alloc(dom
);
3415 /* Construct an isl_sol_map structure for accumulating the solution.
3416 * If track_empty is set, then we also keep track of the parts
3417 * of the context where there is no solution.
3418 * If max is set, then we are solving a maximization, rather than
3419 * a minimization problem, which means that the variables in the
3420 * tableau have value "M - x" rather than "M + x".
3422 static struct isl_sol
*sol_map_init(struct isl_basic_map
*bmap
,
3423 struct isl_basic_set
*dom
, int track_empty
, int max
)
3425 struct isl_sol_map
*sol_map
= NULL
;
3430 sol_map
= isl_calloc_type(bmap
->ctx
, struct isl_sol_map
);
3434 sol_map
->sol
.rational
= ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
3435 sol_map
->sol
.dec_level
.callback
.run
= &sol_dec_level_wrap
;
3436 sol_map
->sol
.dec_level
.sol
= &sol_map
->sol
;
3437 sol_map
->sol
.max
= max
;
3438 sol_map
->sol
.n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
3439 sol_map
->sol
.add
= &sol_map_add_wrap
;
3440 sol_map
->sol
.add_empty
= track_empty
? &sol_map_add_empty_wrap
: NULL
;
3441 sol_map
->sol
.free
= &sol_map_free_wrap
;
3442 sol_map
->map
= isl_map_alloc_space(isl_basic_map_get_space(bmap
), 1,
3447 sol_map
->sol
.context
= isl_context_alloc(dom
);
3448 if (!sol_map
->sol
.context
)
3452 sol_map
->empty
= isl_set_alloc_space(isl_basic_set_get_space(dom
),
3453 1, ISL_SET_DISJOINT
);
3454 if (!sol_map
->empty
)
3458 isl_basic_set_free(dom
);
3459 return &sol_map
->sol
;
3461 isl_basic_set_free(dom
);
3462 sol_map_free(sol_map
);
3466 /* Check whether all coefficients of (non-parameter) variables
3467 * are non-positive, meaning that no pivots can be performed on the row.
3469 static int is_critical(struct isl_tab
*tab
, int row
)
3472 unsigned off
= 2 + tab
->M
;
3474 for (j
= tab
->n_dead
; j
< tab
->n_col
; ++j
) {
3475 if (tab
->col_var
[j
] >= 0 &&
3476 (tab
->col_var
[j
] < tab
->n_param
||
3477 tab
->col_var
[j
] >= tab
->n_var
- tab
->n_div
))
3480 if (isl_int_is_pos(tab
->mat
->row
[row
][off
+ j
]))
3487 /* Check whether the inequality represented by vec is strict over the integers,
3488 * i.e., there are no integer values satisfying the constraint with
3489 * equality. This happens if the gcd of the coefficients is not a divisor
3490 * of the constant term. If so, scale the constraint down by the gcd
3491 * of the coefficients.
3493 static int is_strict(struct isl_vec
*vec
)
3499 isl_seq_gcd(vec
->el
+ 1, vec
->size
- 1, &gcd
);
3500 if (!isl_int_is_one(gcd
)) {
3501 strict
= !isl_int_is_divisible_by(vec
->el
[0], gcd
);
3502 isl_int_fdiv_q(vec
->el
[0], vec
->el
[0], gcd
);
3503 isl_seq_scale_down(vec
->el
+ 1, vec
->el
+ 1, gcd
, vec
->size
-1);
3510 /* Determine the sign of the given row of the main tableau.
3511 * The result is one of
3512 * isl_tab_row_pos: always non-negative; no pivot needed
3513 * isl_tab_row_neg: always non-positive; pivot
3514 * isl_tab_row_any: can be both positive and negative; split
3516 * We first handle some simple cases
3517 * - the row sign may be known already
3518 * - the row may be obviously non-negative
3519 * - the parametric constant may be equal to that of another row
3520 * for which we know the sign. This sign will be either "pos" or
3521 * "any". If it had been "neg" then we would have pivoted before.
3523 * If none of these cases hold, we check the value of the row for each
3524 * of the currently active samples. Based on the signs of these values
3525 * we make an initial determination of the sign of the row.
3527 * all zero -> unk(nown)
3528 * all non-negative -> pos
3529 * all non-positive -> neg
3530 * both negative and positive -> all
3532 * If we end up with "all", we are done.
3533 * Otherwise, we perform a check for positive and/or negative
3534 * values as follows.
3536 * samples neg unk pos
3542 * There is no special sign for "zero", because we can usually treat zero
3543 * as either non-negative or non-positive, whatever works out best.
3544 * However, if the row is "critical", meaning that pivoting is impossible
3545 * then we don't want to limp zero with the non-positive case, because
3546 * then we we would lose the solution for those values of the parameters
3547 * where the value of the row is zero. Instead, we treat 0 as non-negative
3548 * ensuring a split if the row can attain both zero and negative values.
3549 * The same happens when the original constraint was one that could not
3550 * be satisfied with equality by any integer values of the parameters.
3551 * In this case, we normalize the constraint, but then a value of zero
3552 * for the normalized constraint is actually a positive value for the
3553 * original constraint, so again we need to treat zero as non-negative.
3554 * In both these cases, we have the following decision tree instead:
3556 * all non-negative -> pos
3557 * all negative -> neg
3558 * both negative and non-negative -> all
3566 static enum isl_tab_row_sign
row_sign(struct isl_tab
*tab
,
3567 struct isl_sol
*sol
, int row
)
3569 struct isl_vec
*ineq
= NULL
;
3570 enum isl_tab_row_sign res
= isl_tab_row_unknown
;
3575 if (tab
->row_sign
[row
] != isl_tab_row_unknown
)
3576 return tab
->row_sign
[row
];
3577 if (is_obviously_nonneg(tab
, row
))
3578 return isl_tab_row_pos
;
3579 for (row2
= tab
->n_redundant
; row2
< tab
->n_row
; ++row2
) {
3580 if (tab
->row_sign
[row2
] == isl_tab_row_unknown
)
3582 if (identical_parameter_line(tab
, row
, row2
))
3583 return tab
->row_sign
[row2
];
3586 critical
= is_critical(tab
, row
);
3588 ineq
= get_row_parameter_ineq(tab
, row
);
3592 strict
= is_strict(ineq
);
3594 res
= sol
->context
->op
->ineq_sign(sol
->context
, ineq
->el
,
3595 critical
|| strict
);
3597 if (res
== isl_tab_row_unknown
|| res
== isl_tab_row_pos
) {
3598 /* test for negative values */
3600 isl_seq_neg(ineq
->el
, ineq
->el
, ineq
->size
);
3601 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3603 feasible
= sol
->context
->op
->test_ineq(sol
->context
, ineq
->el
);
3607 res
= isl_tab_row_pos
;
3609 res
= (res
== isl_tab_row_unknown
) ? isl_tab_row_neg
3611 if (res
== isl_tab_row_neg
) {
3612 isl_seq_neg(ineq
->el
, ineq
->el
, ineq
->size
);
3613 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3617 if (res
== isl_tab_row_neg
) {
3618 /* test for positive values */
3620 if (!critical
&& !strict
)
3621 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3623 feasible
= sol
->context
->op
->test_ineq(sol
->context
, ineq
->el
);
3627 res
= isl_tab_row_any
;
3634 return isl_tab_row_unknown
;
3637 static void find_solutions(struct isl_sol
*sol
, struct isl_tab
*tab
);
3639 /* Find solutions for values of the parameters that satisfy the given
3642 * We currently take a snapshot of the context tableau that is reset
3643 * when we return from this function, while we make a copy of the main
3644 * tableau, leaving the original main tableau untouched.
3645 * These are fairly arbitrary choices. Making a copy also of the context
3646 * tableau would obviate the need to undo any changes made to it later,
3647 * while taking a snapshot of the main tableau could reduce memory usage.
3648 * If we were to switch to taking a snapshot of the main tableau,
3649 * we would have to keep in mind that we need to save the row signs
3650 * and that we need to do this before saving the current basis
3651 * such that the basis has been restore before we restore the row signs.
3653 static void find_in_pos(struct isl_sol
*sol
, struct isl_tab
*tab
, isl_int
*ineq
)
3659 saved
= sol
->context
->op
->save(sol
->context
);
3661 tab
= isl_tab_dup(tab
);
3665 sol
->context
->op
->add_ineq(sol
->context
, ineq
, 0, 1);
3667 find_solutions(sol
, tab
);
3670 sol
->context
->op
->restore(sol
->context
, saved
);
3672 sol
->context
->op
->discard(saved
);
3678 /* Record the absence of solutions for those values of the parameters
3679 * that do not satisfy the given inequality with equality.
3681 static void no_sol_in_strict(struct isl_sol
*sol
,
3682 struct isl_tab
*tab
, struct isl_vec
*ineq
)
3687 if (!sol
->context
|| sol
->error
)
3689 saved
= sol
->context
->op
->save(sol
->context
);
3691 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3693 sol
->context
->op
->add_ineq(sol
->context
, ineq
->el
, 1, 0);
3702 isl_int_add_ui(ineq
->el
[0], ineq
->el
[0], 1);
3704 sol
->context
->op
->restore(sol
->context
, saved
);
3710 /* Reset all row variables that are marked to have a sign that may
3711 * be both positive and negative to have an unknown sign.
3713 static void reset_any_to_unknown(struct isl_tab
*tab
)
3717 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
3718 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
3720 if (tab
->row_sign
[row
] == isl_tab_row_any
)
3721 tab
->row_sign
[row
] = isl_tab_row_unknown
;
3725 /* Compute the lexicographic minimum of the set represented by the main
3726 * tableau "tab" within the context "sol->context_tab".
3727 * On entry the sample value of the main tableau is lexicographically
3728 * less than or equal to this lexicographic minimum.
3729 * Pivots are performed until a feasible point is found, which is then
3730 * necessarily equal to the minimum, or until the tableau is found to
3731 * be infeasible. Some pivots may need to be performed for only some
3732 * feasible values of the context tableau. If so, the context tableau
3733 * is split into a part where the pivot is needed and a part where it is not.
3735 * Whenever we enter the main loop, the main tableau is such that no
3736 * "obvious" pivots need to be performed on it, where "obvious" means
3737 * that the given row can be seen to be negative without looking at
3738 * the context tableau. In particular, for non-parametric problems,
3739 * no pivots need to be performed on the main tableau.
3740 * The caller of find_solutions is responsible for making this property
3741 * hold prior to the first iteration of the loop, while restore_lexmin
3742 * is called before every other iteration.
3744 * Inside the main loop, we first examine the signs of the rows of
3745 * the main tableau within the context of the context tableau.
3746 * If we find a row that is always non-positive for all values of
3747 * the parameters satisfying the context tableau and negative for at
3748 * least one value of the parameters, we perform the appropriate pivot
3749 * and start over. An exception is the case where no pivot can be
3750 * performed on the row. In this case, we require that the sign of
3751 * the row is negative for all values of the parameters (rather than just
3752 * non-positive). This special case is handled inside row_sign, which
3753 * will say that the row can have any sign if it determines that it can
3754 * attain both negative and zero values.
3756 * If we can't find a row that always requires a pivot, but we can find
3757 * one or more rows that require a pivot for some values of the parameters
3758 * (i.e., the row can attain both positive and negative signs), then we split
3759 * the context tableau into two parts, one where we force the sign to be
3760 * non-negative and one where we force is to be negative.
3761 * The non-negative part is handled by a recursive call (through find_in_pos).
3762 * Upon returning from this call, we continue with the negative part and
3763 * perform the required pivot.
3765 * If no such rows can be found, all rows are non-negative and we have
3766 * found a (rational) feasible point. If we only wanted a rational point
3768 * Otherwise, we check if all values of the sample point of the tableau
3769 * are integral for the variables. If so, we have found the minimal
3770 * integral point and we are done.
3771 * If the sample point is not integral, then we need to make a distinction
3772 * based on whether the constant term is non-integral or the coefficients
3773 * of the parameters. Furthermore, in order to decide how to handle
3774 * the non-integrality, we also need to know whether the coefficients
3775 * of the other columns in the tableau are integral. This leads
3776 * to the following table. The first two rows do not correspond
3777 * to a non-integral sample point and are only mentioned for completeness.
3779 * constant parameters other
3782 * int int rat | -> no problem
3784 * rat int int -> fail
3786 * rat int rat -> cut
3789 * rat rat rat | -> parametric cut
3792 * rat rat int | -> split context
3794 * If the parametric constant is completely integral, then there is nothing
3795 * to be done. If the constant term is non-integral, but all the other
3796 * coefficient are integral, then there is nothing that can be done
3797 * and the tableau has no integral solution.
3798 * If, on the other hand, one or more of the other columns have rational
3799 * coefficients, but the parameter coefficients are all integral, then
3800 * we can perform a regular (non-parametric) cut.
3801 * Finally, if there is any parameter coefficient that is non-integral,
3802 * then we need to involve the context tableau. There are two cases here.
3803 * If at least one other column has a rational coefficient, then we
3804 * can perform a parametric cut in the main tableau by adding a new
3805 * integer division in the context tableau.
3806 * If all other columns have integral coefficients, then we need to
3807 * enforce that the rational combination of parameters (c + \sum a_i y_i)/m
3808 * is always integral. We do this by introducing an integer division
3809 * q = floor((c + \sum a_i y_i)/m) and stipulating that its argument should
3810 * always be integral in the context tableau, i.e., m q = c + \sum a_i y_i.
3811 * Since q is expressed in the tableau as
3812 * c + \sum a_i y_i - m q >= 0
3813 * -c - \sum a_i y_i + m q + m - 1 >= 0
3814 * it is sufficient to add the inequality
3815 * -c - \sum a_i y_i + m q >= 0
3816 * In the part of the context where this inequality does not hold, the
3817 * main tableau is marked as being empty.
3819 static void find_solutions(struct isl_sol
*sol
, struct isl_tab
*tab
)
3821 struct isl_context
*context
;
3824 if (!tab
|| sol
->error
)
3827 context
= sol
->context
;
3831 if (context
->op
->is_empty(context
))
3834 for (r
= 0; r
>= 0 && tab
&& !tab
->empty
; r
= restore_lexmin(tab
)) {
3837 enum isl_tab_row_sign sgn
;
3841 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
3842 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
3844 sgn
= row_sign(tab
, sol
, row
);
3847 tab
->row_sign
[row
] = sgn
;
3848 if (sgn
== isl_tab_row_any
)
3850 if (sgn
== isl_tab_row_any
&& split
== -1)
3852 if (sgn
== isl_tab_row_neg
)
3855 if (row
< tab
->n_row
)
3858 struct isl_vec
*ineq
;
3860 split
= context
->op
->best_split(context
, tab
);
3863 ineq
= get_row_parameter_ineq(tab
, split
);
3867 reset_any_to_unknown(tab
);
3868 tab
->row_sign
[split
] = isl_tab_row_pos
;
3870 find_in_pos(sol
, tab
, ineq
->el
);
3871 tab
->row_sign
[split
] = isl_tab_row_neg
;
3872 isl_seq_neg(ineq
->el
, ineq
->el
, ineq
->size
);
3873 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3875 context
->op
->add_ineq(context
, ineq
->el
, 0, 1);
3883 row
= first_non_integer_row(tab
, &flags
);
3886 if (ISL_FL_ISSET(flags
, I_PAR
)) {
3887 if (ISL_FL_ISSET(flags
, I_VAR
)) {
3888 if (isl_tab_mark_empty(tab
) < 0)
3892 row
= add_cut(tab
, row
);
3893 } else if (ISL_FL_ISSET(flags
, I_VAR
)) {
3894 struct isl_vec
*div
;
3895 struct isl_vec
*ineq
;
3897 div
= get_row_split_div(tab
, row
);
3900 d
= context
->op
->get_div(context
, tab
, div
);
3904 ineq
= ineq_for_div(context
->op
->peek_basic_set(context
), d
);
3908 no_sol_in_strict(sol
, tab
, ineq
);
3909 isl_seq_neg(ineq
->el
, ineq
->el
, ineq
->size
);
3910 context
->op
->add_ineq(context
, ineq
->el
, 1, 1);
3912 if (sol
->error
|| !context
->op
->is_ok(context
))
3914 tab
= set_row_cst_to_div(tab
, row
, d
);
3915 if (context
->op
->is_empty(context
))
3918 row
= add_parametric_cut(tab
, row
, context
);
3933 /* Does "sol" contain a pair of partial solutions that could potentially
3936 * We currently only check that "sol" is not in an error state
3937 * and that there are at least two partial solutions of which the final two
3938 * are defined at the same level.
3940 static int sol_has_mergeable_solutions(struct isl_sol
*sol
)
3946 if (!sol
->partial
->next
)
3948 return sol
->partial
->level
== sol
->partial
->next
->level
;
3951 /* Compute the lexicographic minimum of the set represented by the main
3952 * tableau "tab" within the context "sol->context_tab".
3954 * As a preprocessing step, we first transfer all the purely parametric
3955 * equalities from the main tableau to the context tableau, i.e.,
3956 * parameters that have been pivoted to a row.
3957 * These equalities are ignored by the main algorithm, because the
3958 * corresponding rows may not be marked as being non-negative.
3959 * In parts of the context where the added equality does not hold,
3960 * the main tableau is marked as being empty.
3962 * Before we embark on the actual computation, we save a copy
3963 * of the context. When we return, we check if there are any
3964 * partial solutions that can potentially be merged. If so,
3965 * we perform a rollback to the initial state of the context.
3966 * The merging of partial solutions happens inside calls to
3967 * sol_dec_level that are pushed onto the undo stack of the context.
3968 * If there are no partial solutions that can potentially be merged
3969 * then the rollback is skipped as it would just be wasted effort.
3971 static void find_solutions_main(struct isl_sol
*sol
, struct isl_tab
*tab
)
3981 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
3985 if (tab
->row_var
[row
] < 0)
3987 if (tab
->row_var
[row
] >= tab
->n_param
&&
3988 tab
->row_var
[row
] < tab
->n_var
- tab
->n_div
)
3990 if (tab
->row_var
[row
] < tab
->n_param
)
3991 p
= tab
->row_var
[row
];
3993 p
= tab
->row_var
[row
]
3994 + tab
->n_param
- (tab
->n_var
- tab
->n_div
);
3996 eq
= isl_vec_alloc(tab
->mat
->ctx
, 1+tab
->n_param
+tab
->n_div
);
3999 get_row_parameter_line(tab
, row
, eq
->el
);
4000 isl_int_neg(eq
->el
[1 + p
], tab
->mat
->row
[row
][0]);
4001 eq
= isl_vec_normalize(eq
);
4004 no_sol_in_strict(sol
, tab
, eq
);
4006 isl_seq_neg(eq
->el
, eq
->el
, eq
->size
);
4008 no_sol_in_strict(sol
, tab
, eq
);
4009 isl_seq_neg(eq
->el
, eq
->el
, eq
->size
);
4011 sol
->context
->op
->add_eq(sol
->context
, eq
->el
, 1, 1);
4015 if (isl_tab_mark_redundant(tab
, row
) < 0)
4018 if (sol
->context
->op
->is_empty(sol
->context
))
4021 row
= tab
->n_redundant
- 1;
4024 saved
= sol
->context
->op
->save(sol
->context
);
4026 find_solutions(sol
, tab
);
4028 if (sol_has_mergeable_solutions(sol
))
4029 sol
->context
->op
->restore(sol
->context
, saved
);
4031 sol
->context
->op
->discard(saved
);
4042 /* Check if integer division "div" of "dom" also occurs in "bmap".
4043 * If so, return its position within the divs.
4044 * If not, return -1.
4046 static int find_context_div(struct isl_basic_map
*bmap
,
4047 struct isl_basic_set
*dom
, unsigned div
)
4050 unsigned b_dim
= isl_space_dim(bmap
->dim
, isl_dim_all
);
4051 unsigned d_dim
= isl_space_dim(dom
->dim
, isl_dim_all
);
4053 if (isl_int_is_zero(dom
->div
[div
][0]))
4055 if (isl_seq_first_non_zero(dom
->div
[div
] + 2 + d_dim
, dom
->n_div
) != -1)
4058 for (i
= 0; i
< bmap
->n_div
; ++i
) {
4059 if (isl_int_is_zero(bmap
->div
[i
][0]))
4061 if (isl_seq_first_non_zero(bmap
->div
[i
] + 2 + d_dim
,
4062 (b_dim
- d_dim
) + bmap
->n_div
) != -1)
4064 if (isl_seq_eq(bmap
->div
[i
], dom
->div
[div
], 2 + d_dim
))
4070 /* The correspondence between the variables in the main tableau,
4071 * the context tableau, and the input map and domain is as follows.
4072 * The first n_param and the last n_div variables of the main tableau
4073 * form the variables of the context tableau.
4074 * In the basic map, these n_param variables correspond to the
4075 * parameters and the input dimensions. In the domain, they correspond
4076 * to the parameters and the set dimensions.
4077 * The n_div variables correspond to the integer divisions in the domain.
4078 * To ensure that everything lines up, we may need to copy some of the
4079 * integer divisions of the domain to the map. These have to be placed
4080 * in the same order as those in the context and they have to be placed
4081 * after any other integer divisions that the map may have.
4082 * This function performs the required reordering.
4084 static struct isl_basic_map
*align_context_divs(struct isl_basic_map
*bmap
,
4085 struct isl_basic_set
*dom
)
4091 for (i
= 0; i
< dom
->n_div
; ++i
)
4092 if (find_context_div(bmap
, dom
, i
) != -1)
4094 other
= bmap
->n_div
- common
;
4095 if (dom
->n_div
- common
> 0) {
4096 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
4097 dom
->n_div
- common
, 0, 0);
4101 for (i
= 0; i
< dom
->n_div
; ++i
) {
4102 int pos
= find_context_div(bmap
, dom
, i
);
4104 pos
= isl_basic_map_alloc_div(bmap
);
4107 isl_int_set_si(bmap
->div
[pos
][0], 0);
4109 if (pos
!= other
+ i
)
4110 isl_basic_map_swap_div(bmap
, pos
, other
+ i
);
4114 isl_basic_map_free(bmap
);
4118 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
4119 * some obvious symmetries.
4121 * We make sure the divs in the domain are properly ordered,
4122 * because they will be added one by one in the given order
4123 * during the construction of the solution map.
4125 static struct isl_sol
*basic_map_partial_lexopt_base_sol(
4126 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
4127 __isl_give isl_set
**empty
, int max
,
4128 struct isl_sol
*(*init
)(__isl_keep isl_basic_map
*bmap
,
4129 __isl_take isl_basic_set
*dom
, int track_empty
, int max
))
4131 struct isl_tab
*tab
;
4132 struct isl_sol
*sol
= NULL
;
4133 struct isl_context
*context
;
4136 dom
= isl_basic_set_order_divs(dom
);
4137 bmap
= align_context_divs(bmap
, dom
);
4139 sol
= init(bmap
, dom
, !!empty
, max
);
4143 context
= sol
->context
;
4144 if (isl_basic_set_plain_is_empty(context
->op
->peek_basic_set(context
)))
4146 else if (isl_basic_map_plain_is_empty(bmap
)) {
4149 isl_basic_set_copy(context
->op
->peek_basic_set(context
)));
4151 tab
= tab_for_lexmin(bmap
,
4152 context
->op
->peek_basic_set(context
), 1, max
);
4153 tab
= context
->op
->detect_nonnegative_parameters(context
, tab
);
4154 find_solutions_main(sol
, tab
);
4159 isl_basic_map_free(bmap
);
4163 isl_basic_map_free(bmap
);
4167 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
4168 * some obvious symmetries.
4170 * We call basic_map_partial_lexopt_base_sol and extract the results.
4172 static __isl_give isl_map
*basic_map_partial_lexopt_base(
4173 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
4174 __isl_give isl_set
**empty
, int max
)
4176 isl_map
*result
= NULL
;
4177 struct isl_sol
*sol
;
4178 struct isl_sol_map
*sol_map
;
4180 sol
= basic_map_partial_lexopt_base_sol(bmap
, dom
, empty
, max
,
4184 sol_map
= (struct isl_sol_map
*) sol
;
4186 result
= isl_map_copy(sol_map
->map
);
4188 *empty
= isl_set_copy(sol_map
->empty
);
4189 sol_free(&sol_map
->sol
);
4193 /* Return a count of the number of occurrences of the "n" first
4194 * variables in the inequality constraints of "bmap".
4196 static __isl_give
int *count_occurrences(__isl_keep isl_basic_map
*bmap
,
4205 ctx
= isl_basic_map_get_ctx(bmap
);
4206 occurrences
= isl_calloc_array(ctx
, int, n
);
4210 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
4211 for (j
= 0; j
< n
; ++j
) {
4212 if (!isl_int_is_zero(bmap
->ineq
[i
][1 + j
]))
4220 /* Do all of the "n" variables with non-zero coefficients in "c"
4221 * occur in exactly a single constraint.
4222 * "occurrences" is an array of length "n" containing the number
4223 * of occurrences of each of the variables in the inequality constraints.
4225 static int single_occurrence(int n
, isl_int
*c
, int *occurrences
)
4229 for (i
= 0; i
< n
; ++i
) {
4230 if (isl_int_is_zero(c
[i
]))
4232 if (occurrences
[i
] != 1)
4239 /* Do all of the "n" initial variables that occur in inequality constraint
4240 * "ineq" of "bmap" only occur in that constraint?
4242 static int all_single_occurrence(__isl_keep isl_basic_map
*bmap
, int ineq
,
4247 for (i
= 0; i
< n
; ++i
) {
4248 if (isl_int_is_zero(bmap
->ineq
[ineq
][1 + i
]))
4250 for (j
= 0; j
< bmap
->n_ineq
; ++j
) {
4253 if (!isl_int_is_zero(bmap
->ineq
[j
][1 + i
]))
4261 /* Structure used during detection of parallel constraints.
4262 * n_in: number of "input" variables: isl_dim_param + isl_dim_in
4263 * n_out: number of "output" variables: isl_dim_out + isl_dim_div
4264 * val: the coefficients of the output variables
4266 struct isl_constraint_equal_info
{
4267 isl_basic_map
*bmap
;
4273 /* Check whether the coefficients of the output variables
4274 * of the constraint in "entry" are equal to info->val.
4276 static int constraint_equal(const void *entry
, const void *val
)
4278 isl_int
**row
= (isl_int
**)entry
;
4279 const struct isl_constraint_equal_info
*info
= val
;
4281 return isl_seq_eq((*row
) + 1 + info
->n_in
, info
->val
, info
->n_out
);
4284 /* Check whether "bmap" has a pair of constraints that have
4285 * the same coefficients for the output variables.
4286 * Note that the coefficients of the existentially quantified
4287 * variables need to be zero since the existentially quantified
4288 * of the result are usually not the same as those of the input.
4289 * Furthermore, check that each of the input variables that occur
4290 * in those constraints does not occur in any other constraint.
4291 * If so, return 1 and return the row indices of the two constraints
4292 * in *first and *second.
4294 static int parallel_constraints(__isl_keep isl_basic_map
*bmap
,
4295 int *first
, int *second
)
4299 int *occurrences
= NULL
;
4300 struct isl_hash_table
*table
= NULL
;
4301 struct isl_hash_table_entry
*entry
;
4302 struct isl_constraint_equal_info info
;
4306 ctx
= isl_basic_map_get_ctx(bmap
);
4307 table
= isl_hash_table_alloc(ctx
, bmap
->n_ineq
);
4311 info
.n_in
= isl_basic_map_dim(bmap
, isl_dim_param
) +
4312 isl_basic_map_dim(bmap
, isl_dim_in
);
4313 occurrences
= count_occurrences(bmap
, info
.n_in
);
4314 if (info
.n_in
&& !occurrences
)
4317 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4318 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4319 info
.n_out
= n_out
+ n_div
;
4320 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
4323 info
.val
= bmap
->ineq
[i
] + 1 + info
.n_in
;
4324 if (isl_seq_first_non_zero(info
.val
, n_out
) < 0)
4326 if (isl_seq_first_non_zero(info
.val
+ n_out
, n_div
) >= 0)
4328 if (!single_occurrence(info
.n_in
, bmap
->ineq
[i
] + 1,
4331 hash
= isl_seq_get_hash(info
.val
, info
.n_out
);
4332 entry
= isl_hash_table_find(ctx
, table
, hash
,
4333 constraint_equal
, &info
, 1);
4338 entry
->data
= &bmap
->ineq
[i
];
4341 if (i
< bmap
->n_ineq
) {
4342 *first
= ((isl_int
**)entry
->data
) - bmap
->ineq
;
4346 isl_hash_table_free(ctx
, table
);
4349 return i
< bmap
->n_ineq
;
4351 isl_hash_table_free(ctx
, table
);
4356 /* Given a set of upper bounds in "var", add constraints to "bset"
4357 * that make the i-th bound smallest.
4359 * In particular, if there are n bounds b_i, then add the constraints
4361 * b_i <= b_j for j > i
4362 * b_i < b_j for j < i
4364 static __isl_give isl_basic_set
*select_minimum(__isl_take isl_basic_set
*bset
,
4365 __isl_keep isl_mat
*var
, int i
)
4370 ctx
= isl_mat_get_ctx(var
);
4372 for (j
= 0; j
< var
->n_row
; ++j
) {
4375 k
= isl_basic_set_alloc_inequality(bset
);
4378 isl_seq_combine(bset
->ineq
[k
], ctx
->one
, var
->row
[j
],
4379 ctx
->negone
, var
->row
[i
], var
->n_col
);
4380 isl_int_set_si(bset
->ineq
[k
][var
->n_col
], 0);
4382 isl_int_sub_ui(bset
->ineq
[k
][0], bset
->ineq
[k
][0], 1);
4385 bset
= isl_basic_set_finalize(bset
);
4389 isl_basic_set_free(bset
);
4393 /* Given a set of upper bounds on the last "input" variable m,
4394 * construct a set that assigns the minimal upper bound to m, i.e.,
4395 * construct a set that divides the space into cells where one
4396 * of the upper bounds is smaller than all the others and assign
4397 * this upper bound to m.
4399 * In particular, if there are n bounds b_i, then the result
4400 * consists of n basic sets, each one of the form
4403 * b_i <= b_j for j > i
4404 * b_i < b_j for j < i
4406 static __isl_give isl_set
*set_minimum(__isl_take isl_space
*dim
,
4407 __isl_take isl_mat
*var
)
4410 isl_basic_set
*bset
= NULL
;
4411 isl_set
*set
= NULL
;
4416 set
= isl_set_alloc_space(isl_space_copy(dim
),
4417 var
->n_row
, ISL_SET_DISJOINT
);
4419 for (i
= 0; i
< var
->n_row
; ++i
) {
4420 bset
= isl_basic_set_alloc_space(isl_space_copy(dim
), 0,
4422 k
= isl_basic_set_alloc_equality(bset
);
4425 isl_seq_cpy(bset
->eq
[k
], var
->row
[i
], var
->n_col
);
4426 isl_int_set_si(bset
->eq
[k
][var
->n_col
], -1);
4427 bset
= select_minimum(bset
, var
, i
);
4428 set
= isl_set_add_basic_set(set
, bset
);
4431 isl_space_free(dim
);
4435 isl_basic_set_free(bset
);
4437 isl_space_free(dim
);
4442 /* Given that the last input variable of "bmap" represents the minimum
4443 * of the bounds in "cst", check whether we need to split the domain
4444 * based on which bound attains the minimum.
4446 * A split is needed when the minimum appears in an integer division
4447 * or in an equality. Otherwise, it is only needed if it appears in
4448 * an upper bound that is different from the upper bounds on which it
4451 static int need_split_basic_map(__isl_keep isl_basic_map
*bmap
,
4452 __isl_keep isl_mat
*cst
)
4458 pos
= cst
->n_col
- 1;
4459 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4461 for (i
= 0; i
< bmap
->n_div
; ++i
)
4462 if (!isl_int_is_zero(bmap
->div
[i
][2 + pos
]))
4465 for (i
= 0; i
< bmap
->n_eq
; ++i
)
4466 if (!isl_int_is_zero(bmap
->eq
[i
][1 + pos
]))
4469 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
4470 if (isl_int_is_nonneg(bmap
->ineq
[i
][1 + pos
]))
4472 if (!isl_int_is_negone(bmap
->ineq
[i
][1 + pos
]))
4474 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + pos
+ 1,
4475 total
- pos
- 1) >= 0)
4478 for (j
= 0; j
< cst
->n_row
; ++j
)
4479 if (isl_seq_eq(bmap
->ineq
[i
], cst
->row
[j
], cst
->n_col
))
4481 if (j
>= cst
->n_row
)
4488 /* Given that the last set variable of "bset" represents the minimum
4489 * of the bounds in "cst", check whether we need to split the domain
4490 * based on which bound attains the minimum.
4492 * We simply call need_split_basic_map here. This is safe because
4493 * the position of the minimum is computed from "cst" and not
4496 static int need_split_basic_set(__isl_keep isl_basic_set
*bset
,
4497 __isl_keep isl_mat
*cst
)
4499 return need_split_basic_map((isl_basic_map
*)bset
, cst
);
4502 /* Given that the last set variable of "set" represents the minimum
4503 * of the bounds in "cst", check whether we need to split the domain
4504 * based on which bound attains the minimum.
4506 static int need_split_set(__isl_keep isl_set
*set
, __isl_keep isl_mat
*cst
)
4510 for (i
= 0; i
< set
->n
; ++i
)
4511 if (need_split_basic_set(set
->p
[i
], cst
))
4517 /* Given a set of which the last set variable is the minimum
4518 * of the bounds in "cst", split each basic set in the set
4519 * in pieces where one of the bounds is (strictly) smaller than the others.
4520 * This subdivision is given in "min_expr".
4521 * The variable is subsequently projected out.
4523 * We only do the split when it is needed.
4524 * For example if the last input variable m = min(a,b) and the only
4525 * constraints in the given basic set are lower bounds on m,
4526 * i.e., l <= m = min(a,b), then we can simply project out m
4527 * to obtain l <= a and l <= b, without having to split on whether
4528 * m is equal to a or b.
4530 static __isl_give isl_set
*split(__isl_take isl_set
*empty
,
4531 __isl_take isl_set
*min_expr
, __isl_take isl_mat
*cst
)
4538 if (!empty
|| !min_expr
|| !cst
)
4541 n_in
= isl_set_dim(empty
, isl_dim_set
);
4542 dim
= isl_set_get_space(empty
);
4543 dim
= isl_space_drop_dims(dim
, isl_dim_set
, n_in
- 1, 1);
4544 res
= isl_set_empty(dim
);
4546 for (i
= 0; i
< empty
->n
; ++i
) {
4549 set
= isl_set_from_basic_set(isl_basic_set_copy(empty
->p
[i
]));
4550 if (need_split_basic_set(empty
->p
[i
], cst
))
4551 set
= isl_set_intersect(set
, isl_set_copy(min_expr
));
4552 set
= isl_set_remove_dims(set
, isl_dim_set
, n_in
- 1, 1);
4554 res
= isl_set_union_disjoint(res
, set
);
4557 isl_set_free(empty
);
4558 isl_set_free(min_expr
);
4562 isl_set_free(empty
);
4563 isl_set_free(min_expr
);
4568 /* Given a map of which the last input variable is the minimum
4569 * of the bounds in "cst", split each basic set in the set
4570 * in pieces where one of the bounds is (strictly) smaller than the others.
4571 * This subdivision is given in "min_expr".
4572 * The variable is subsequently projected out.
4574 * The implementation is essentially the same as that of "split".
4576 static __isl_give isl_map
*split_domain(__isl_take isl_map
*opt
,
4577 __isl_take isl_set
*min_expr
, __isl_take isl_mat
*cst
)
4584 if (!opt
|| !min_expr
|| !cst
)
4587 n_in
= isl_map_dim(opt
, isl_dim_in
);
4588 dim
= isl_map_get_space(opt
);
4589 dim
= isl_space_drop_dims(dim
, isl_dim_in
, n_in
- 1, 1);
4590 res
= isl_map_empty(dim
);
4592 for (i
= 0; i
< opt
->n
; ++i
) {
4595 map
= isl_map_from_basic_map(isl_basic_map_copy(opt
->p
[i
]));
4596 if (need_split_basic_map(opt
->p
[i
], cst
))
4597 map
= isl_map_intersect_domain(map
,
4598 isl_set_copy(min_expr
));
4599 map
= isl_map_remove_dims(map
, isl_dim_in
, n_in
- 1, 1);
4601 res
= isl_map_union_disjoint(res
, map
);
4605 isl_set_free(min_expr
);
4610 isl_set_free(min_expr
);
4615 static __isl_give isl_map
*basic_map_partial_lexopt(
4616 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
4617 __isl_give isl_set
**empty
, int max
);
4619 /* This function is called from basic_map_partial_lexopt_symm.
4620 * The last variable of "bmap" and "dom" corresponds to the minimum
4621 * of the bounds in "cst". "map_space" is the space of the original
4622 * input relation (of basic_map_partial_lexopt_symm) and "set_space"
4623 * is the space of the original domain.
4625 * We recursively call basic_map_partial_lexopt and then plug in
4626 * the definition of the minimum in the result.
4628 static __isl_give isl_map
*basic_map_partial_lexopt_symm_core(
4629 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
4630 __isl_give isl_set
**empty
, int max
, __isl_take isl_mat
*cst
,
4631 __isl_take isl_space
*map_space
, __isl_take isl_space
*set_space
)
4636 min_expr
= set_minimum(isl_basic_set_get_space(dom
), isl_mat_copy(cst
));
4638 opt
= basic_map_partial_lexopt(bmap
, dom
, empty
, max
);
4641 *empty
= split(*empty
,
4642 isl_set_copy(min_expr
), isl_mat_copy(cst
));
4643 *empty
= isl_set_reset_space(*empty
, set_space
);
4646 opt
= split_domain(opt
, min_expr
, cst
);
4647 opt
= isl_map_reset_space(opt
, map_space
);
4652 /* Extract a domain from "bmap" for the purpose of computing
4653 * a lexicographic optimum.
4655 * This function is only called when the caller wants to compute a full
4656 * lexicographic optimum, i.e., without specifying a domain. In this case,
4657 * the caller is not interested in the part of the domain space where
4658 * there is no solution and the domain can be initialized to those constraints
4659 * of "bmap" that only involve the parameters and the input dimensions.
4660 * This relieves the parametric programming engine from detecting those
4661 * inequalities and transferring them to the context. More importantly,
4662 * it ensures that those inequalities are transferred first and not
4663 * intermixed with inequalities that actually split the domain.
4665 * If the caller does not require the absence of existentially quantified
4666 * variables in the result (i.e., if ISL_OPT_QE is not set in "flags"),
4667 * then the actual domain of "bmap" can be used. This ensures that
4668 * the domain does not need to be split at all just to separate out
4669 * pieces of the domain that do not have a solution from piece that do.
4670 * This domain cannot be used in general because it may involve
4671 * (unknown) existentially quantified variables which will then also
4672 * appear in the solution.
4674 static __isl_give isl_basic_set
*extract_domain(__isl_keep isl_basic_map
*bmap
,
4680 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4681 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4682 bmap
= isl_basic_map_copy(bmap
);
4683 if (ISL_FL_ISSET(flags
, ISL_OPT_QE
)) {
4684 bmap
= isl_basic_map_drop_constraints_involving_dims(bmap
,
4685 isl_dim_div
, 0, n_div
);
4686 bmap
= isl_basic_map_drop_constraints_involving_dims(bmap
,
4687 isl_dim_out
, 0, n_out
);
4689 return isl_basic_map_domain(bmap
);
4693 #define TYPE isl_map
4696 #include "isl_tab_lexopt_templ.c"
4698 struct isl_sol_for
{
4700 int (*fn
)(__isl_take isl_basic_set
*dom
,
4701 __isl_take isl_aff_list
*list
, void *user
);
4705 static void sol_for_free(struct isl_sol_for
*sol_for
)
4709 if (sol_for
->sol
.context
)
4710 sol_for
->sol
.context
->op
->free(sol_for
->sol
.context
);
4714 static void sol_for_free_wrap(struct isl_sol
*sol
)
4716 sol_for_free((struct isl_sol_for
*)sol
);
4719 /* Add the solution identified by the tableau and the context tableau.
4721 * See documentation of sol_add for more details.
4723 * Instead of constructing a basic map, this function calls a user
4724 * defined function with the current context as a basic set and
4725 * a list of affine expressions representing the relation between
4726 * the input and output. The space over which the affine expressions
4727 * are defined is the same as that of the domain. The number of
4728 * affine expressions in the list is equal to the number of output variables.
4730 static void sol_for_add(struct isl_sol_for
*sol
,
4731 struct isl_basic_set
*dom
, struct isl_mat
*M
)
4735 isl_local_space
*ls
;
4739 if (sol
->sol
.error
|| !dom
|| !M
)
4742 ctx
= isl_basic_set_get_ctx(dom
);
4743 ls
= isl_basic_set_get_local_space(dom
);
4744 list
= isl_aff_list_alloc(ctx
, M
->n_row
- 1);
4745 for (i
= 1; i
< M
->n_row
; ++i
) {
4746 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
4748 isl_int_set(aff
->v
->el
[0], M
->row
[0][0]);
4749 isl_seq_cpy(aff
->v
->el
+ 1, M
->row
[i
], M
->n_col
);
4751 aff
= isl_aff_normalize(aff
);
4752 list
= isl_aff_list_add(list
, aff
);
4754 isl_local_space_free(ls
);
4756 dom
= isl_basic_set_finalize(dom
);
4758 if (sol
->fn(isl_basic_set_copy(dom
), list
, sol
->user
) < 0)
4761 isl_basic_set_free(dom
);
4765 isl_basic_set_free(dom
);
4770 static void sol_for_add_wrap(struct isl_sol
*sol
,
4771 struct isl_basic_set
*dom
, struct isl_mat
*M
)
4773 sol_for_add((struct isl_sol_for
*)sol
, dom
, M
);
4776 static struct isl_sol_for
*sol_for_init(struct isl_basic_map
*bmap
, int max
,
4777 int (*fn
)(__isl_take isl_basic_set
*dom
, __isl_take isl_aff_list
*list
,
4781 struct isl_sol_for
*sol_for
= NULL
;
4783 struct isl_basic_set
*dom
= NULL
;
4785 sol_for
= isl_calloc_type(bmap
->ctx
, struct isl_sol_for
);
4789 dom_dim
= isl_space_domain(isl_space_copy(bmap
->dim
));
4790 dom
= isl_basic_set_universe(dom_dim
);
4792 sol_for
->sol
.rational
= ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
4793 sol_for
->sol
.dec_level
.callback
.run
= &sol_dec_level_wrap
;
4794 sol_for
->sol
.dec_level
.sol
= &sol_for
->sol
;
4796 sol_for
->user
= user
;
4797 sol_for
->sol
.max
= max
;
4798 sol_for
->sol
.n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4799 sol_for
->sol
.add
= &sol_for_add_wrap
;
4800 sol_for
->sol
.add_empty
= NULL
;
4801 sol_for
->sol
.free
= &sol_for_free_wrap
;
4803 sol_for
->sol
.context
= isl_context_alloc(dom
);
4804 if (!sol_for
->sol
.context
)
4807 isl_basic_set_free(dom
);
4810 isl_basic_set_free(dom
);
4811 sol_for_free(sol_for
);
4815 static void sol_for_find_solutions(struct isl_sol_for
*sol_for
,
4816 struct isl_tab
*tab
)
4818 find_solutions_main(&sol_for
->sol
, tab
);
4821 int isl_basic_map_foreach_lexopt(__isl_keep isl_basic_map
*bmap
, int max
,
4822 int (*fn
)(__isl_take isl_basic_set
*dom
, __isl_take isl_aff_list
*list
,
4826 struct isl_sol_for
*sol_for
= NULL
;
4828 bmap
= isl_basic_map_copy(bmap
);
4829 bmap
= isl_basic_map_detect_equalities(bmap
);
4833 sol_for
= sol_for_init(bmap
, max
, fn
, user
);
4837 if (isl_basic_map_plain_is_empty(bmap
))
4840 struct isl_tab
*tab
;
4841 struct isl_context
*context
= sol_for
->sol
.context
;
4842 tab
= tab_for_lexmin(bmap
,
4843 context
->op
->peek_basic_set(context
), 1, max
);
4844 tab
= context
->op
->detect_nonnegative_parameters(context
, tab
);
4845 sol_for_find_solutions(sol_for
, tab
);
4846 if (sol_for
->sol
.error
)
4850 sol_free(&sol_for
->sol
);
4851 isl_basic_map_free(bmap
);
4854 sol_free(&sol_for
->sol
);
4855 isl_basic_map_free(bmap
);
4859 int isl_basic_set_foreach_lexopt(__isl_keep isl_basic_set
*bset
, int max
,
4860 int (*fn
)(__isl_take isl_basic_set
*dom
, __isl_take isl_aff_list
*list
,
4864 return isl_basic_map_foreach_lexopt(bset
, max
, fn
, user
);
4867 /* Check if the given sequence of len variables starting at pos
4868 * represents a trivial (i.e., zero) solution.
4869 * The variables are assumed to be non-negative and to come in pairs,
4870 * with each pair representing a variable of unrestricted sign.
4871 * The solution is trivial if each such pair in the sequence consists
4872 * of two identical values, meaning that the variable being represented
4875 static int region_is_trivial(struct isl_tab
*tab
, int pos
, int len
)
4882 for (i
= 0; i
< len
; i
+= 2) {
4886 neg_row
= tab
->var
[pos
+ i
].is_row
?
4887 tab
->var
[pos
+ i
].index
: -1;
4888 pos_row
= tab
->var
[pos
+ i
+ 1].is_row
?
4889 tab
->var
[pos
+ i
+ 1].index
: -1;
4892 isl_int_is_zero(tab
->mat
->row
[neg_row
][1])) &&
4894 isl_int_is_zero(tab
->mat
->row
[pos_row
][1])))
4897 if (neg_row
< 0 || pos_row
< 0)
4899 if (isl_int_ne(tab
->mat
->row
[neg_row
][1],
4900 tab
->mat
->row
[pos_row
][1]))
4907 /* Return the index of the first trivial region or -1 if all regions
4910 static int first_trivial_region(struct isl_tab
*tab
,
4911 int n_region
, struct isl_region
*region
)
4915 for (i
= 0; i
< n_region
; ++i
) {
4916 if (region_is_trivial(tab
, region
[i
].pos
, region
[i
].len
))
4923 /* Check if the solution is optimal, i.e., whether the first
4924 * n_op entries are zero.
4926 static int is_optimal(__isl_keep isl_vec
*sol
, int n_op
)
4930 for (i
= 0; i
< n_op
; ++i
)
4931 if (!isl_int_is_zero(sol
->el
[1 + i
]))
4936 /* Add constraints to "tab" that ensure that any solution is significantly
4937 * better than that represented by "sol". That is, find the first
4938 * relevant (within first n_op) non-zero coefficient and force it (along
4939 * with all previous coefficients) to be zero.
4940 * If the solution is already optimal (all relevant coefficients are zero),
4941 * then just mark the table as empty.
4943 * This function assumes that at least 2 * n_op more rows and at least
4944 * 2 * n_op more elements in the constraint array are available in the tableau.
4946 static int force_better_solution(struct isl_tab
*tab
,
4947 __isl_keep isl_vec
*sol
, int n_op
)
4956 for (i
= 0; i
< n_op
; ++i
)
4957 if (!isl_int_is_zero(sol
->el
[1 + i
]))
4961 if (isl_tab_mark_empty(tab
) < 0)
4966 ctx
= isl_vec_get_ctx(sol
);
4967 v
= isl_vec_alloc(ctx
, 1 + tab
->n_var
);
4971 for (; i
>= 0; --i
) {
4973 isl_int_set_si(v
->el
[1 + i
], -1);
4974 if (add_lexmin_eq(tab
, v
->el
) < 0)
4985 struct isl_trivial
{
4989 struct isl_tab_undo
*snap
;
4992 /* Return the lexicographically smallest non-trivial solution of the
4993 * given ILP problem.
4995 * All variables are assumed to be non-negative.
4997 * n_op is the number of initial coordinates to optimize.
4998 * That is, once a solution has been found, we will only continue looking
4999 * for solution that result in significantly better values for those
5000 * initial coordinates. That is, we only continue looking for solutions
5001 * that increase the number of initial zeros in this sequence.
5003 * A solution is non-trivial, if it is non-trivial on each of the
5004 * specified regions. Each region represents a sequence of pairs
5005 * of variables. A solution is non-trivial on such a region if
5006 * at least one of these pairs consists of different values, i.e.,
5007 * such that the non-negative variable represented by the pair is non-zero.
5009 * Whenever a conflict is encountered, all constraints involved are
5010 * reported to the caller through a call to "conflict".
5012 * We perform a simple branch-and-bound backtracking search.
5013 * Each level in the search represents initially trivial region that is forced
5014 * to be non-trivial.
5015 * At each level we consider n cases, where n is the length of the region.
5016 * In terms of the n/2 variables of unrestricted signs being encoded by
5017 * the region, we consider the cases
5020 * x_0 = 0 and x_1 >= 1
5021 * x_0 = 0 and x_1 <= -1
5022 * x_0 = 0 and x_1 = 0 and x_2 >= 1
5023 * x_0 = 0 and x_1 = 0 and x_2 <= -1
5025 * The cases are considered in this order, assuming that each pair
5026 * x_i_a x_i_b represents the value x_i_b - x_i_a.
5027 * That is, x_0 >= 1 is enforced by adding the constraint
5028 * x_0_b - x_0_a >= 1
5030 __isl_give isl_vec
*isl_tab_basic_set_non_trivial_lexmin(
5031 __isl_take isl_basic_set
*bset
, int n_op
, int n_region
,
5032 struct isl_region
*region
,
5033 int (*conflict
)(int con
, void *user
), void *user
)
5039 isl_vec
*sol
= NULL
;
5040 struct isl_tab
*tab
;
5041 struct isl_trivial
*triv
= NULL
;
5047 ctx
= isl_basic_set_get_ctx(bset
);
5048 sol
= isl_vec_alloc(ctx
, 0);
5050 tab
= tab_for_lexmin(bset
, NULL
, 0, 0);
5053 tab
->conflict
= conflict
;
5054 tab
->conflict_user
= user
;
5056 v
= isl_vec_alloc(ctx
, 1 + tab
->n_var
);
5057 triv
= isl_calloc_array(ctx
, struct isl_trivial
, n_region
);
5058 if (!v
|| (n_region
&& !triv
))
5064 while (level
>= 0) {
5068 tab
= cut_to_integer_lexmin(tab
, CUT_ONE
);
5073 r
= first_trivial_region(tab
, n_region
, region
);
5075 for (i
= 0; i
< level
; ++i
)
5078 sol
= isl_tab_get_sample_value(tab
);
5081 if (is_optimal(sol
, n_op
))
5085 if (level
>= n_region
)
5086 isl_die(ctx
, isl_error_internal
,
5087 "nesting level too deep", goto error
);
5088 if (isl_tab_extend_cons(tab
,
5089 2 * region
[r
].len
+ 2 * n_op
) < 0)
5091 triv
[level
].region
= r
;
5092 triv
[level
].side
= 0;
5095 r
= triv
[level
].region
;
5096 side
= triv
[level
].side
;
5097 base
= 2 * (side
/2);
5099 if (side
>= region
[r
].len
) {
5104 if (isl_tab_rollback(tab
, triv
[level
].snap
) < 0)
5109 if (triv
[level
].update
) {
5110 if (force_better_solution(tab
, sol
, n_op
) < 0)
5112 triv
[level
].update
= 0;
5115 if (side
== base
&& base
>= 2) {
5116 for (j
= base
- 2; j
< base
; ++j
) {
5118 isl_int_set_si(v
->el
[1 + region
[r
].pos
+ j
], 1);
5119 if (add_lexmin_eq(tab
, v
->el
) < 0)
5124 triv
[level
].snap
= isl_tab_snap(tab
);
5125 if (isl_tab_push_basis(tab
) < 0)
5129 isl_int_set_si(v
->el
[0], -1);
5130 isl_int_set_si(v
->el
[1 + region
[r
].pos
+ side
], -1);
5131 isl_int_set_si(v
->el
[1 + region
[r
].pos
+ (side
^ 1)], 1);
5132 tab
= add_lexmin_ineq(tab
, v
->el
);
5142 isl_basic_set_free(bset
);
5149 isl_basic_set_free(bset
);
5154 /* Wrapper for a tableau that is used for computing
5155 * the lexicographically smallest rational point of a non-negative set.
5156 * This point is represented by the sample value of "tab",
5157 * unless "tab" is empty.
5159 struct isl_tab_lexmin
{
5161 struct isl_tab
*tab
;
5164 /* Free "tl" and return NULL.
5166 __isl_null isl_tab_lexmin
*isl_tab_lexmin_free(__isl_take isl_tab_lexmin
*tl
)
5170 isl_ctx_deref(tl
->ctx
);
5171 isl_tab_free(tl
->tab
);
5177 /* Construct an isl_tab_lexmin for computing
5178 * the lexicographically smallest rational point in "bset",
5179 * assuming that all variables are non-negative.
5181 __isl_give isl_tab_lexmin
*isl_tab_lexmin_from_basic_set(
5182 __isl_take isl_basic_set
*bset
)
5190 ctx
= isl_basic_set_get_ctx(bset
);
5191 tl
= isl_calloc_type(ctx
, struct isl_tab_lexmin
);
5196 tl
->tab
= tab_for_lexmin(bset
, NULL
, 0, 0);
5197 isl_basic_set_free(bset
);
5199 return isl_tab_lexmin_free(tl
);
5202 isl_basic_set_free(bset
);
5203 isl_tab_lexmin_free(tl
);
5207 /* Return the dimension of the set represented by "tl".
5209 int isl_tab_lexmin_dim(__isl_keep isl_tab_lexmin
*tl
)
5211 return tl
? tl
->tab
->n_var
: -1;
5214 /* Add the equality with coefficients "eq" to "tl", updating the optimal
5215 * solution if needed.
5216 * The equality is added as two opposite inequality constraints.
5218 __isl_give isl_tab_lexmin
*isl_tab_lexmin_add_eq(__isl_take isl_tab_lexmin
*tl
,
5224 return isl_tab_lexmin_free(tl
);
5226 if (isl_tab_extend_cons(tl
->tab
, 2) < 0)
5227 return isl_tab_lexmin_free(tl
);
5228 n_var
= tl
->tab
->n_var
;
5229 isl_seq_neg(eq
, eq
, 1 + n_var
);
5230 tl
->tab
= add_lexmin_ineq(tl
->tab
, eq
);
5231 isl_seq_neg(eq
, eq
, 1 + n_var
);
5232 tl
->tab
= add_lexmin_ineq(tl
->tab
, eq
);
5235 return isl_tab_lexmin_free(tl
);
5240 /* Return the lexicographically smallest rational point in the basic set
5241 * from which "tl" was constructed.
5242 * If the original input was empty, then return a zero-length vector.
5244 __isl_give isl_vec
*isl_tab_lexmin_get_solution(__isl_keep isl_tab_lexmin
*tl
)
5249 return isl_vec_alloc(tl
->ctx
, 0);
5251 return isl_tab_get_sample_value(tl
->tab
);
5254 /* Return the lexicographically smallest rational point in "bset",
5255 * assuming that all variables are non-negative.
5256 * If "bset" is empty, then return a zero-length vector.
5258 __isl_give isl_vec
*isl_tab_basic_set_non_neg_lexmin(
5259 __isl_take isl_basic_set
*bset
)
5264 tl
= isl_tab_lexmin_from_basic_set(bset
);
5265 sol
= isl_tab_lexmin_get_solution(tl
);
5266 isl_tab_lexmin_free(tl
);
5270 struct isl_sol_pma
{
5272 isl_pw_multi_aff
*pma
;
5276 static void sol_pma_free(struct isl_sol_pma
*sol_pma
)
5280 if (sol_pma
->sol
.context
)
5281 sol_pma
->sol
.context
->op
->free(sol_pma
->sol
.context
);
5282 isl_pw_multi_aff_free(sol_pma
->pma
);
5283 isl_set_free(sol_pma
->empty
);
5287 /* This function is called for parts of the context where there is
5288 * no solution, with "bset" corresponding to the context tableau.
5289 * Simply add the basic set to the set "empty".
5291 static void sol_pma_add_empty(struct isl_sol_pma
*sol
,
5292 __isl_take isl_basic_set
*bset
)
5294 if (!bset
|| !sol
->empty
)
5297 sol
->empty
= isl_set_grow(sol
->empty
, 1);
5298 bset
= isl_basic_set_simplify(bset
);
5299 bset
= isl_basic_set_finalize(bset
);
5300 sol
->empty
= isl_set_add_basic_set(sol
->empty
, bset
);
5305 isl_basic_set_free(bset
);
5309 /* Return the equality constraint in "bset" that defines existentially
5310 * quantified variable "pos" in terms of earlier dimensions.
5311 * The equality constraint is guaranteed to exist by the caller.
5312 * If "c" is not NULL, then it is the result of a previous call
5313 * to this function for the same variable, so simply return the input "c"
5316 static __isl_give isl_constraint
*get_equality(__isl_keep isl_basic_set
*bset
,
5317 int pos
, __isl_take isl_constraint
*c
)
5323 r
= isl_basic_set_has_defining_equality(bset
, isl_dim_div
, pos
, &c
);
5327 isl_die(isl_basic_set_get_ctx(bset
), isl_error_internal
,
5328 "unexpected missing equality", return NULL
);
5332 /* Given a set "dom", of which only the first "n_known" existentially
5333 * quantified variables have a known explicit representation, and
5334 * a matrix "M", the rows of which are defined in terms of the dimensions
5335 * of "dom", eliminate all references to the existentially quantified
5336 * variables without a known explicit representation from "M"
5337 * by exploiting the equality constraints of "dom".
5339 * In particular, for each of those existentially quantified variables,
5340 * if there are non-zero entries in the corresponding column of "M",
5341 * then look for an equality constraint of "dom" that defines that variable
5342 * in terms of earlier variables and use it to clear the entries.
5344 * In particular, if the equality is of the form
5348 * while the matrix entry is b/d (with d the global denominator of "M"),
5349 * then first scale the matrix such that the entry becomes b'/d' with
5350 * b' a multiple of a. Do this by multiplying the entire matrix
5351 * by abs(a/gcd(a,b)). Then subtract the equality multiplied by b'/a
5352 * from the row of "M" to clear the entry.
5354 static __isl_give isl_mat
*eliminate_unknown_divs(__isl_take isl_mat
*M
,
5355 __isl_keep isl_basic_set
*dom
, int n_known
)
5357 int i
, j
, n_div
, off
;
5359 isl_constraint
*c
= NULL
;
5364 n_div
= isl_basic_set_dim(dom
, isl_dim_div
);
5365 off
= M
->n_col
- n_div
;
5368 for (i
= n_div
- 1; i
>= n_known
; --i
) {
5369 for (j
= 1; j
< M
->n_row
; ++j
) {
5370 if (isl_int_is_zero(M
->row
[j
][off
+ i
]))
5372 c
= get_equality(dom
, i
, c
);
5375 isl_int_gcd(t
, M
->row
[j
][off
+ i
], c
->v
->el
[off
+ i
]);
5376 isl_int_divexact(t
, c
->v
->el
[off
+ i
], t
);
5378 M
= isl_mat_scale(M
, t
);
5383 M
->row
[j
][off
+ i
], c
->v
->el
[off
+ i
]);
5384 isl_seq_submul(M
->row
[j
], t
, c
->v
->el
, M
->n_col
);
5386 c
= isl_constraint_free(c
);
5393 isl_constraint_free(c
);
5398 /* Return the index of the last known div of "bset" after "start" and
5399 * up to (but not including) "end".
5400 * Return "start" if there is no such known div.
5402 static int last_known_div_after(__isl_keep isl_basic_set
*bset
,
5405 for (end
= end
- 1; end
> start
; --end
) {
5406 if (isl_basic_set_div_is_known(bset
, end
))
5413 /* Set the affine expressions in "ma" according to the rows in "M", which
5414 * are defined over the local space "ls".
5415 * The matrix "M" may have extra (zero) columns beyond the number
5416 * of variables in "ls".
5418 static __isl_give isl_multi_aff
*set_from_affine_matrix(
5419 __isl_take isl_multi_aff
*ma
, __isl_take isl_local_space
*ls
,
5420 __isl_take isl_mat
*M
)
5425 if (!ma
|| !ls
|| !M
)
5428 dim
= isl_local_space_dim(ls
, isl_dim_all
);
5429 for (i
= 1; i
< M
->n_row
; ++i
) {
5430 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
5432 isl_int_set(aff
->v
->el
[0], M
->row
[0][0]);
5433 isl_seq_cpy(aff
->v
->el
+ 1, M
->row
[i
], 1 + dim
);
5435 aff
= isl_aff_normalize(aff
);
5436 ma
= isl_multi_aff_set_aff(ma
, i
- 1, aff
);
5438 isl_local_space_free(ls
);
5443 isl_local_space_free(ls
);
5445 isl_multi_aff_free(ma
);
5449 /* Given a basic map "dom" that represents the context and an affine
5450 * matrix "M" that maps the dimensions of the context to the
5451 * output variables, construct an isl_pw_multi_aff with a single
5452 * cell corresponding to "dom" and affine expressions copied from "M".
5454 * Note that the description of the initial context may have involved
5455 * existentially quantified variables, in which case they also appear
5456 * in "dom". These need to be removed before creating the affine
5457 * expression because an affine expression cannot be defined in terms
5458 * of existentially quantified variables without a known representation.
5459 * In particular, they are first moved to the end in both "dom" and "M" and
5460 * then ignored in "M". In principle, the final columns of "M"
5461 * (i.e., those that will be ignored) should be zero at this stage
5462 * because align_context_divs adds the existentially quantified
5463 * variables of the context to the main tableau without any constraints.
5464 * The computed minimal value can therefore not depend on these variables.
5465 * However, additional integer divisions that get added for parametric cuts
5466 * get added to the end and they may happen to be equal to some affine
5467 * expression involving the original existentially quantified variables.
5468 * These equality constraints are then propagated to the main tableau
5469 * such that the computed minimum can in fact depend on those existentially
5470 * quantified variables. This dependence can however be removed again
5471 * by exploiting the equality constraints in "dom".
5472 * eliminate_unknown_divs takes care of this.
5474 static void sol_pma_add(struct isl_sol_pma
*sol
,
5475 __isl_take isl_basic_set
*dom
, __isl_take isl_mat
*M
)
5477 isl_local_space
*ls
;
5478 isl_multi_aff
*maff
;
5479 isl_pw_multi_aff
*pma
;
5480 int n_div
, n_known
, end
, off
;
5482 n_div
= isl_basic_set_dim(dom
, isl_dim_div
);
5483 off
= M
->n_col
- n_div
;
5485 for (n_known
= 0; n_known
< end
; ++n_known
) {
5486 if (isl_basic_set_div_is_known(dom
, n_known
))
5488 end
= last_known_div_after(dom
, n_known
, end
);
5491 isl_basic_set_swap_div(dom
, n_known
, end
);
5492 M
= isl_mat_swap_cols(M
, off
+ n_known
, off
+ end
);
5494 dom
= isl_basic_set_gauss(dom
, NULL
);
5495 if (n_known
< n_div
)
5496 M
= eliminate_unknown_divs(M
, dom
, n_known
);
5498 maff
= isl_multi_aff_alloc(isl_pw_multi_aff_get_space(sol
->pma
));
5499 ls
= isl_basic_set_get_local_space(dom
);
5500 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
,
5501 n_known
, n_div
- n_known
);
5502 maff
= set_from_affine_matrix(maff
, ls
, M
);
5503 dom
= isl_basic_set_simplify(dom
);
5504 dom
= isl_basic_set_finalize(dom
);
5505 pma
= isl_pw_multi_aff_alloc(isl_set_from_basic_set(dom
), maff
);
5506 sol
->pma
= isl_pw_multi_aff_add_disjoint(sol
->pma
, pma
);
5511 static void sol_pma_free_wrap(struct isl_sol
*sol
)
5513 sol_pma_free((struct isl_sol_pma
*)sol
);
5516 static void sol_pma_add_empty_wrap(struct isl_sol
*sol
,
5517 __isl_take isl_basic_set
*bset
)
5519 sol_pma_add_empty((struct isl_sol_pma
*)sol
, bset
);
5522 static void sol_pma_add_wrap(struct isl_sol
*sol
,
5523 __isl_take isl_basic_set
*dom
, __isl_take isl_mat
*M
)
5525 sol_pma_add((struct isl_sol_pma
*)sol
, dom
, M
);
5528 /* Construct an isl_sol_pma structure for accumulating the solution.
5529 * If track_empty is set, then we also keep track of the parts
5530 * of the context where there is no solution.
5531 * If max is set, then we are solving a maximization, rather than
5532 * a minimization problem, which means that the variables in the
5533 * tableau have value "M - x" rather than "M + x".
5535 static struct isl_sol
*sol_pma_init(__isl_keep isl_basic_map
*bmap
,
5536 __isl_take isl_basic_set
*dom
, int track_empty
, int max
)
5538 struct isl_sol_pma
*sol_pma
= NULL
;
5543 sol_pma
= isl_calloc_type(bmap
->ctx
, struct isl_sol_pma
);
5547 sol_pma
->sol
.rational
= ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
5548 sol_pma
->sol
.dec_level
.callback
.run
= &sol_dec_level_wrap
;
5549 sol_pma
->sol
.dec_level
.sol
= &sol_pma
->sol
;
5550 sol_pma
->sol
.max
= max
;
5551 sol_pma
->sol
.n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5552 sol_pma
->sol
.add
= &sol_pma_add_wrap
;
5553 sol_pma
->sol
.add_empty
= track_empty
? &sol_pma_add_empty_wrap
: NULL
;
5554 sol_pma
->sol
.free
= &sol_pma_free_wrap
;
5555 sol_pma
->pma
= isl_pw_multi_aff_empty(isl_basic_map_get_space(bmap
));
5559 sol_pma
->sol
.context
= isl_context_alloc(dom
);
5560 if (!sol_pma
->sol
.context
)
5564 sol_pma
->empty
= isl_set_alloc_space(isl_basic_set_get_space(dom
),
5565 1, ISL_SET_DISJOINT
);
5566 if (!sol_pma
->empty
)
5570 isl_basic_set_free(dom
);
5571 return &sol_pma
->sol
;
5573 isl_basic_set_free(dom
);
5574 sol_pma_free(sol_pma
);
5578 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
5579 * some obvious symmetries.
5581 * We call basic_map_partial_lexopt_base_sol and extract the results.
5583 static __isl_give isl_pw_multi_aff
*basic_map_partial_lexopt_base_pw_multi_aff(
5584 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5585 __isl_give isl_set
**empty
, int max
)
5587 isl_pw_multi_aff
*result
= NULL
;
5588 struct isl_sol
*sol
;
5589 struct isl_sol_pma
*sol_pma
;
5591 sol
= basic_map_partial_lexopt_base_sol(bmap
, dom
, empty
, max
,
5595 sol_pma
= (struct isl_sol_pma
*) sol
;
5597 result
= isl_pw_multi_aff_copy(sol_pma
->pma
);
5599 *empty
= isl_set_copy(sol_pma
->empty
);
5600 sol_free(&sol_pma
->sol
);
5604 /* Given that the last input variable of "maff" represents the minimum
5605 * of some bounds, check whether we need to plug in the expression
5608 * In particular, check if the last input variable appears in any
5609 * of the expressions in "maff".
5611 static int need_substitution(__isl_keep isl_multi_aff
*maff
)
5616 pos
= isl_multi_aff_dim(maff
, isl_dim_in
) - 1;
5618 for (i
= 0; i
< maff
->n
; ++i
)
5619 if (isl_aff_involves_dims(maff
->p
[i
], isl_dim_in
, pos
, 1))
5625 /* Given a set of upper bounds on the last "input" variable m,
5626 * construct a piecewise affine expression that selects
5627 * the minimal upper bound to m, i.e.,
5628 * divide the space into cells where one
5629 * of the upper bounds is smaller than all the others and select
5630 * this upper bound on that cell.
5632 * In particular, if there are n bounds b_i, then the result
5633 * consists of n cell, each one of the form
5635 * b_i <= b_j for j > i
5636 * b_i < b_j for j < i
5638 * The affine expression on this cell is
5642 static __isl_give isl_pw_aff
*set_minimum_pa(__isl_take isl_space
*space
,
5643 __isl_take isl_mat
*var
)
5646 isl_aff
*aff
= NULL
;
5647 isl_basic_set
*bset
= NULL
;
5648 isl_pw_aff
*paff
= NULL
;
5649 isl_space
*pw_space
;
5650 isl_local_space
*ls
= NULL
;
5655 ls
= isl_local_space_from_space(isl_space_copy(space
));
5656 pw_space
= isl_space_copy(space
);
5657 pw_space
= isl_space_from_domain(pw_space
);
5658 pw_space
= isl_space_add_dims(pw_space
, isl_dim_out
, 1);
5659 paff
= isl_pw_aff_alloc_size(pw_space
, var
->n_row
);
5661 for (i
= 0; i
< var
->n_row
; ++i
) {
5664 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
5665 bset
= isl_basic_set_alloc_space(isl_space_copy(space
), 0,
5669 isl_int_set_si(aff
->v
->el
[0], 1);
5670 isl_seq_cpy(aff
->v
->el
+ 1, var
->row
[i
], var
->n_col
);
5671 isl_int_set_si(aff
->v
->el
[1 + var
->n_col
], 0);
5672 bset
= select_minimum(bset
, var
, i
);
5673 paff_i
= isl_pw_aff_alloc(isl_set_from_basic_set(bset
), aff
);
5674 paff
= isl_pw_aff_add_disjoint(paff
, paff_i
);
5677 isl_local_space_free(ls
);
5678 isl_space_free(space
);
5683 isl_basic_set_free(bset
);
5684 isl_pw_aff_free(paff
);
5685 isl_local_space_free(ls
);
5686 isl_space_free(space
);
5691 /* Given a piecewise multi-affine expression of which the last input variable
5692 * is the minimum of the bounds in "cst", plug in the value of the minimum.
5693 * This minimum expression is given in "min_expr_pa".
5694 * The set "min_expr" contains the same information, but in the form of a set.
5695 * The variable is subsequently projected out.
5697 * The implementation is similar to those of "split" and "split_domain".
5698 * If the variable appears in a given expression, then minimum expression
5699 * is plugged in. Otherwise, if the variable appears in the constraints
5700 * and a split is required, then the domain is split. Otherwise, no split
5703 static __isl_give isl_pw_multi_aff
*split_domain_pma(
5704 __isl_take isl_pw_multi_aff
*opt
, __isl_take isl_pw_aff
*min_expr_pa
,
5705 __isl_take isl_set
*min_expr
, __isl_take isl_mat
*cst
)
5710 isl_pw_multi_aff
*res
;
5712 if (!opt
|| !min_expr
|| !cst
)
5715 n_in
= isl_pw_multi_aff_dim(opt
, isl_dim_in
);
5716 space
= isl_pw_multi_aff_get_space(opt
);
5717 space
= isl_space_drop_dims(space
, isl_dim_in
, n_in
- 1, 1);
5718 res
= isl_pw_multi_aff_empty(space
);
5720 for (i
= 0; i
< opt
->n
; ++i
) {
5721 isl_pw_multi_aff
*pma
;
5723 pma
= isl_pw_multi_aff_alloc(isl_set_copy(opt
->p
[i
].set
),
5724 isl_multi_aff_copy(opt
->p
[i
].maff
));
5725 if (need_substitution(opt
->p
[i
].maff
))
5726 pma
= isl_pw_multi_aff_substitute(pma
,
5727 isl_dim_in
, n_in
- 1, min_expr_pa
);
5728 else if (need_split_set(opt
->p
[i
].set
, cst
))
5729 pma
= isl_pw_multi_aff_intersect_domain(pma
,
5730 isl_set_copy(min_expr
));
5731 pma
= isl_pw_multi_aff_project_out(pma
,
5732 isl_dim_in
, n_in
- 1, 1);
5734 res
= isl_pw_multi_aff_add_disjoint(res
, pma
);
5737 isl_pw_multi_aff_free(opt
);
5738 isl_pw_aff_free(min_expr_pa
);
5739 isl_set_free(min_expr
);
5743 isl_pw_multi_aff_free(opt
);
5744 isl_pw_aff_free(min_expr_pa
);
5745 isl_set_free(min_expr
);
5750 static __isl_give isl_pw_multi_aff
*basic_map_partial_lexopt_pw_multi_aff(
5751 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5752 __isl_give isl_set
**empty
, int max
);
5754 /* This function is called from basic_map_partial_lexopt_symm.
5755 * The last variable of "bmap" and "dom" corresponds to the minimum
5756 * of the bounds in "cst". "map_space" is the space of the original
5757 * input relation (of basic_map_partial_lexopt_symm) and "set_space"
5758 * is the space of the original domain.
5760 * We recursively call basic_map_partial_lexopt and then plug in
5761 * the definition of the minimum in the result.
5763 static __isl_give isl_pw_multi_aff
*
5764 basic_map_partial_lexopt_symm_core_pw_multi_aff(
5765 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5766 __isl_give isl_set
**empty
, int max
, __isl_take isl_mat
*cst
,
5767 __isl_take isl_space
*map_space
, __isl_take isl_space
*set_space
)
5769 isl_pw_multi_aff
*opt
;
5770 isl_pw_aff
*min_expr_pa
;
5773 min_expr
= set_minimum(isl_basic_set_get_space(dom
), isl_mat_copy(cst
));
5774 min_expr_pa
= set_minimum_pa(isl_basic_set_get_space(dom
),
5777 opt
= basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, empty
, max
);
5780 *empty
= split(*empty
,
5781 isl_set_copy(min_expr
), isl_mat_copy(cst
));
5782 *empty
= isl_set_reset_space(*empty
, set_space
);
5785 opt
= split_domain_pma(opt
, min_expr_pa
, min_expr
, cst
);
5786 opt
= isl_pw_multi_aff_reset_space(opt
, map_space
);
5792 #define TYPE isl_pw_multi_aff
5794 #define SUFFIX _pw_multi_aff
5795 #include "isl_tab_lexopt_templ.c"