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>
26 #include <bset_to_bmap.c>
29 * The implementation of parametric integer linear programming in this file
30 * was inspired by the paper "Parametric Integer Programming" and the
31 * report "Solving systems of affine (in)equalities" by Paul Feautrier
34 * The strategy used for obtaining a feasible solution is different
35 * from the one used in isl_tab.c. In particular, in isl_tab.c,
36 * upon finding a constraint that is not yet satisfied, we pivot
37 * in a row that increases the constant term of the row holding the
38 * constraint, making sure the sample solution remains feasible
39 * for all the constraints it already satisfied.
40 * Here, we always pivot in the row holding the constraint,
41 * choosing a column that induces the lexicographically smallest
42 * increment to the sample solution.
44 * By starting out from a sample value that is lexicographically
45 * smaller than any integer point in the problem space, the first
46 * feasible integer sample point we find will also be the lexicographically
47 * smallest. If all variables can be assumed to be non-negative,
48 * then the initial sample value may be chosen equal to zero.
49 * However, we will not make this assumption. Instead, we apply
50 * the "big parameter" trick. Any variable x is then not directly
51 * used in the tableau, but instead it is represented by another
52 * variable x' = M + x, where M is an arbitrarily large (positive)
53 * value. x' is therefore always non-negative, whatever the value of x.
54 * Taking as initial sample value x' = 0 corresponds to x = -M,
55 * which is always smaller than any possible value of x.
57 * The big parameter trick is used in the main tableau and
58 * also in the context tableau if isl_context_lex is used.
59 * In this case, each tableaus has its own big parameter.
60 * Before doing any real work, we check if all the parameters
61 * happen to be non-negative. If so, we drop the column corresponding
62 * to M from the initial context tableau.
63 * If isl_context_gbr is used, then the big parameter trick is only
64 * used in the main tableau.
68 struct isl_context_op
{
69 /* detect nonnegative parameters in context and mark them in tab */
70 struct isl_tab
*(*detect_nonnegative_parameters
)(
71 struct isl_context
*context
, struct isl_tab
*tab
);
72 /* return temporary reference to basic set representation of context */
73 struct isl_basic_set
*(*peek_basic_set
)(struct isl_context
*context
);
74 /* return temporary reference to tableau representation of context */
75 struct isl_tab
*(*peek_tab
)(struct isl_context
*context
);
76 /* add equality; check is 1 if eq may not be valid;
77 * update is 1 if we may want to call ineq_sign on context later.
79 void (*add_eq
)(struct isl_context
*context
, isl_int
*eq
,
80 int check
, int update
);
81 /* add inequality; check is 1 if ineq may not be valid;
82 * update is 1 if we may want to call ineq_sign on context later.
84 void (*add_ineq
)(struct isl_context
*context
, isl_int
*ineq
,
85 int check
, int update
);
86 /* check sign of ineq based on previous information.
87 * strict is 1 if saturation should be treated as a positive sign.
89 enum isl_tab_row_sign (*ineq_sign
)(struct isl_context
*context
,
90 isl_int
*ineq
, int strict
);
91 /* check if inequality maintains feasibility */
92 int (*test_ineq
)(struct isl_context
*context
, isl_int
*ineq
);
93 /* return index of a div that corresponds to "div" */
94 int (*get_div
)(struct isl_context
*context
, struct isl_tab
*tab
,
96 /* insert div "div" to context at "pos" and return non-negativity */
97 isl_bool (*insert_div
)(struct isl_context
*context
, int pos
,
98 __isl_keep isl_vec
*div
);
99 int (*detect_equalities
)(struct isl_context
*context
,
100 struct isl_tab
*tab
);
101 /* return row index of "best" split */
102 int (*best_split
)(struct isl_context
*context
, struct isl_tab
*tab
);
103 /* check if context has already been determined to be empty */
104 int (*is_empty
)(struct isl_context
*context
);
105 /* check if context is still usable */
106 int (*is_ok
)(struct isl_context
*context
);
107 /* save a copy/snapshot of context */
108 void *(*save
)(struct isl_context
*context
);
109 /* restore saved context */
110 void (*restore
)(struct isl_context
*context
, void *);
111 /* discard saved context */
112 void (*discard
)(void *);
113 /* invalidate context */
114 void (*invalidate
)(struct isl_context
*context
);
116 __isl_null
struct isl_context
*(*free
)(struct isl_context
*context
);
119 /* Shared parts of context representation.
121 * "n_unknown" is the number of final unknown integer divisions
122 * in the input domain.
125 struct isl_context_op
*op
;
129 struct isl_context_lex
{
130 struct isl_context context
;
134 /* A stack (linked list) of solutions of subtrees of the search space.
136 * "M" describes the solution in terms of the dimensions of "dom".
137 * The number of columns of "M" is one more than the total number
138 * of dimensions of "dom".
140 * If "M" is NULL, then there is no solution on "dom".
142 struct isl_partial_sol
{
144 struct isl_basic_set
*dom
;
147 struct isl_partial_sol
*next
;
151 struct isl_sol_callback
{
152 struct isl_tab_callback callback
;
156 /* isl_sol is an interface for constructing a solution to
157 * a parametric integer linear programming problem.
158 * Every time the algorithm reaches a state where a solution
159 * can be read off from the tableau (including cases where the tableau
160 * is empty), the function "add" is called on the isl_sol passed
161 * to find_solutions_main.
163 * The context tableau is owned by isl_sol and is updated incrementally.
165 * There are currently two implementations of this interface,
166 * isl_sol_map, which simply collects the solutions in an isl_map
167 * and (optionally) the parts of the context where there is no solution
169 * isl_sol_for, which calls a user-defined function for each part of
178 struct isl_context
*context
;
179 struct isl_partial_sol
*partial
;
180 void (*add
)(struct isl_sol
*sol
,
181 struct isl_basic_set
*dom
, struct isl_mat
*M
);
182 void (*add_empty
)(struct isl_sol
*sol
, struct isl_basic_set
*bset
);
183 void (*free
)(struct isl_sol
*sol
);
184 struct isl_sol_callback dec_level
;
187 static void sol_free(struct isl_sol
*sol
)
189 struct isl_partial_sol
*partial
, *next
;
192 for (partial
= sol
->partial
; partial
; partial
= next
) {
193 next
= partial
->next
;
194 isl_basic_set_free(partial
->dom
);
195 isl_mat_free(partial
->M
);
201 /* Push a partial solution represented by a domain and mapping M
202 * onto the stack of partial solutions.
204 static void sol_push_sol(struct isl_sol
*sol
,
205 struct isl_basic_set
*dom
, struct isl_mat
*M
)
207 struct isl_partial_sol
*partial
;
209 if (sol
->error
|| !dom
)
212 partial
= isl_alloc_type(dom
->ctx
, struct isl_partial_sol
);
216 partial
->level
= sol
->level
;
219 partial
->next
= sol
->partial
;
221 sol
->partial
= partial
;
225 isl_basic_set_free(dom
);
230 /* Pop one partial solution from the partial solution stack and
231 * pass it on to sol->add or sol->add_empty.
233 static void sol_pop_one(struct isl_sol
*sol
)
235 struct isl_partial_sol
*partial
;
237 partial
= sol
->partial
;
238 sol
->partial
= partial
->next
;
241 sol
->add(sol
, partial
->dom
, partial
->M
);
243 sol
->add_empty(sol
, partial
->dom
);
247 /* Return a fresh copy of the domain represented by the context tableau.
249 static struct isl_basic_set
*sol_domain(struct isl_sol
*sol
)
251 struct isl_basic_set
*bset
;
256 bset
= isl_basic_set_dup(sol
->context
->op
->peek_basic_set(sol
->context
));
257 bset
= isl_basic_set_update_from_tab(bset
,
258 sol
->context
->op
->peek_tab(sol
->context
));
263 /* Check whether two partial solutions have the same mapping, where n_div
264 * is the number of divs that the two partial solutions have in common.
266 static int same_solution(struct isl_partial_sol
*s1
, struct isl_partial_sol
*s2
,
272 if (!s1
->M
!= !s2
->M
)
277 dim
= isl_basic_set_total_dim(s1
->dom
) - s1
->dom
->n_div
;
279 for (i
= 0; i
< s1
->M
->n_row
; ++i
) {
280 if (isl_seq_first_non_zero(s1
->M
->row
[i
]+1+dim
+n_div
,
281 s1
->M
->n_col
-1-dim
-n_div
) != -1)
283 if (isl_seq_first_non_zero(s2
->M
->row
[i
]+1+dim
+n_div
,
284 s2
->M
->n_col
-1-dim
-n_div
) != -1)
286 if (!isl_seq_eq(s1
->M
->row
[i
], s2
->M
->row
[i
], 1+dim
+n_div
))
292 /* Pop all solutions from the partial solution stack that were pushed onto
293 * the stack at levels that are deeper than the current level.
294 * If the two topmost elements on the stack have the same level
295 * and represent the same solution, then their domains are combined.
296 * This combined domain is the same as the current context domain
297 * as sol_pop is called each time we move back to a higher level.
298 * If the outer level (0) has been reached, then all partial solutions
299 * at the current level are also popped off.
301 static void sol_pop(struct isl_sol
*sol
)
303 struct isl_partial_sol
*partial
;
309 partial
= sol
->partial
;
313 if (partial
->level
== 0 && sol
->level
== 0) {
314 for (partial
= sol
->partial
; partial
; partial
= sol
->partial
)
319 if (partial
->level
<= sol
->level
)
322 if (partial
->next
&& partial
->next
->level
== partial
->level
) {
323 n_div
= isl_basic_set_dim(
324 sol
->context
->op
->peek_basic_set(sol
->context
),
327 if (!same_solution(partial
, partial
->next
, n_div
)) {
331 struct isl_basic_set
*bset
;
335 n
= isl_basic_set_dim(partial
->next
->dom
, isl_dim_div
);
337 bset
= sol_domain(sol
);
338 isl_basic_set_free(partial
->next
->dom
);
339 partial
->next
->dom
= bset
;
340 M
= partial
->next
->M
;
342 M
= isl_mat_drop_cols(M
, M
->n_col
- n
, n
);
343 partial
->next
->M
= M
;
347 partial
->next
->level
= sol
->level
;
352 sol
->partial
= partial
->next
;
353 isl_basic_set_free(partial
->dom
);
354 isl_mat_free(partial
->M
);
360 if (sol
->level
== 0) {
361 for (partial
= sol
->partial
; partial
; partial
= sol
->partial
)
367 error
: sol
->error
= 1;
370 static void sol_dec_level(struct isl_sol
*sol
)
380 static int sol_dec_level_wrap(struct isl_tab_callback
*cb
)
382 struct isl_sol_callback
*callback
= (struct isl_sol_callback
*)cb
;
384 sol_dec_level(callback
->sol
);
386 return callback
->sol
->error
? -1 : 0;
389 /* Move down to next level and push callback onto context tableau
390 * to decrease the level again when it gets rolled back across
391 * the current state. That is, dec_level will be called with
392 * the context tableau in the same state as it is when inc_level
395 static void sol_inc_level(struct isl_sol
*sol
)
403 tab
= sol
->context
->op
->peek_tab(sol
->context
);
404 if (isl_tab_push_callback(tab
, &sol
->dec_level
.callback
) < 0)
408 static void scale_rows(struct isl_mat
*mat
, isl_int m
, int n_row
)
412 if (isl_int_is_one(m
))
415 for (i
= 0; i
< n_row
; ++i
)
416 isl_seq_scale(mat
->row
[i
], mat
->row
[i
], m
, mat
->n_col
);
419 /* Add the solution identified by the tableau and the context tableau.
421 * The layout of the variables is as follows.
422 * tab->n_var is equal to the total number of variables in the input
423 * map (including divs that were copied from the context)
424 * + the number of extra divs constructed
425 * Of these, the first tab->n_param and the last tab->n_div variables
426 * correspond to the variables in the context, i.e.,
427 * tab->n_param + tab->n_div = context_tab->n_var
428 * tab->n_param is equal to the number of parameters and input
429 * dimensions in the input map
430 * tab->n_div is equal to the number of divs in the context
432 * If there is no solution, then call add_empty with a basic set
433 * that corresponds to the context tableau. (If add_empty is NULL,
436 * If there is a solution, then first construct a matrix that maps
437 * all dimensions of the context to the output variables, i.e.,
438 * the output dimensions in the input map.
439 * The divs in the input map (if any) that do not correspond to any
440 * div in the context do not appear in the solution.
441 * The algorithm will make sure that they have an integer value,
442 * but these values themselves are of no interest.
443 * We have to be careful not to drop or rearrange any divs in the
444 * context because that would change the meaning of the matrix.
446 * To extract the value of the output variables, it should be noted
447 * that we always use a big parameter M in the main tableau and so
448 * the variable stored in this tableau is not an output variable x itself, but
449 * x' = M + x (in case of minimization)
451 * x' = M - x (in case of maximization)
452 * If x' appears in a column, then its optimal value is zero,
453 * which means that the optimal value of x is an unbounded number
454 * (-M for minimization and M for maximization).
455 * We currently assume that the output dimensions in the original map
456 * are bounded, so this cannot occur.
457 * Similarly, when x' appears in a row, then the coefficient of M in that
458 * row is necessarily 1.
459 * If the row in the tableau represents
460 * d x' = c + d M + e(y)
461 * then, in case of minimization, the corresponding row in the matrix
464 * with a d = m, the (updated) common denominator of the matrix.
465 * In case of maximization, the row will be
468 static void sol_add(struct isl_sol
*sol
, struct isl_tab
*tab
)
470 struct isl_basic_set
*bset
= NULL
;
471 struct isl_mat
*mat
= NULL
;
476 if (sol
->error
|| !tab
)
479 if (tab
->empty
&& !sol
->add_empty
)
481 if (sol
->context
->op
->is_empty(sol
->context
))
484 bset
= sol_domain(sol
);
487 sol_push_sol(sol
, bset
, NULL
);
493 mat
= isl_mat_alloc(tab
->mat
->ctx
, 1 + sol
->n_out
,
494 1 + tab
->n_param
+ tab
->n_div
);
500 isl_seq_clr(mat
->row
[0] + 1, mat
->n_col
- 1);
501 isl_int_set_si(mat
->row
[0][0], 1);
502 for (row
= 0; row
< sol
->n_out
; ++row
) {
503 int i
= tab
->n_param
+ row
;
506 isl_seq_clr(mat
->row
[1 + row
], mat
->n_col
);
507 if (!tab
->var
[i
].is_row
) {
509 isl_die(mat
->ctx
, isl_error_invalid
,
510 "unbounded optimum", goto error2
);
514 r
= tab
->var
[i
].index
;
516 isl_int_ne(tab
->mat
->row
[r
][2], tab
->mat
->row
[r
][0]))
517 isl_die(mat
->ctx
, isl_error_invalid
,
518 "unbounded optimum", goto error2
);
519 isl_int_gcd(m
, mat
->row
[0][0], tab
->mat
->row
[r
][0]);
520 isl_int_divexact(m
, tab
->mat
->row
[r
][0], m
);
521 scale_rows(mat
, m
, 1 + row
);
522 isl_int_divexact(m
, mat
->row
[0][0], tab
->mat
->row
[r
][0]);
523 isl_int_mul(mat
->row
[1 + row
][0], m
, tab
->mat
->row
[r
][1]);
524 for (j
= 0; j
< tab
->n_param
; ++j
) {
526 if (tab
->var
[j
].is_row
)
528 col
= tab
->var
[j
].index
;
529 isl_int_mul(mat
->row
[1 + row
][1 + j
], m
,
530 tab
->mat
->row
[r
][off
+ col
]);
532 for (j
= 0; j
< tab
->n_div
; ++j
) {
534 if (tab
->var
[tab
->n_var
- tab
->n_div
+j
].is_row
)
536 col
= tab
->var
[tab
->n_var
- tab
->n_div
+j
].index
;
537 isl_int_mul(mat
->row
[1 + row
][1 + tab
->n_param
+ j
], m
,
538 tab
->mat
->row
[r
][off
+ col
]);
541 isl_seq_neg(mat
->row
[1 + row
], mat
->row
[1 + row
],
547 sol_push_sol(sol
, bset
, mat
);
552 isl_basic_set_free(bset
);
560 struct isl_set
*empty
;
563 static void sol_map_free(struct isl_sol_map
*sol_map
)
567 if (sol_map
->sol
.context
)
568 sol_map
->sol
.context
->op
->free(sol_map
->sol
.context
);
569 isl_map_free(sol_map
->map
);
570 isl_set_free(sol_map
->empty
);
574 static void sol_map_free_wrap(struct isl_sol
*sol
)
576 sol_map_free((struct isl_sol_map
*)sol
);
579 /* This function is called for parts of the context where there is
580 * no solution, with "bset" corresponding to the context tableau.
581 * Simply add the basic set to the set "empty".
583 static void sol_map_add_empty(struct isl_sol_map
*sol
,
584 struct isl_basic_set
*bset
)
586 if (!bset
|| !sol
->empty
)
589 sol
->empty
= isl_set_grow(sol
->empty
, 1);
590 bset
= isl_basic_set_simplify(bset
);
591 bset
= isl_basic_set_finalize(bset
);
592 sol
->empty
= isl_set_add_basic_set(sol
->empty
, isl_basic_set_copy(bset
));
595 isl_basic_set_free(bset
);
598 isl_basic_set_free(bset
);
602 static void sol_map_add_empty_wrap(struct isl_sol
*sol
,
603 struct isl_basic_set
*bset
)
605 sol_map_add_empty((struct isl_sol_map
*)sol
, bset
);
608 /* Given a basic set "dom" that represents the context and an affine
609 * matrix "M" that maps the dimensions of the context to the
610 * output variables, construct a basic map with the same parameters
611 * and divs as the context, the dimensions of the context as input
612 * dimensions and a number of output dimensions that is equal to
613 * the number of output dimensions in the input map.
615 * The constraints and divs of the context are simply copied
616 * from "dom". For each row
620 * is added, with d the common denominator of M.
622 static void sol_map_add(struct isl_sol_map
*sol
,
623 struct isl_basic_set
*dom
, struct isl_mat
*M
)
626 struct isl_basic_map
*bmap
= NULL
;
634 if (sol
->sol
.error
|| !dom
|| !M
)
637 n_out
= sol
->sol
.n_out
;
638 n_eq
= dom
->n_eq
+ n_out
;
639 n_ineq
= dom
->n_ineq
;
641 nparam
= isl_basic_set_total_dim(dom
) - n_div
;
642 total
= isl_map_dim(sol
->map
, isl_dim_all
);
643 bmap
= isl_basic_map_alloc_space(isl_map_get_space(sol
->map
),
644 n_div
, n_eq
, 2 * n_div
+ n_ineq
);
647 if (sol
->sol
.rational
)
648 ISL_F_SET(bmap
, ISL_BASIC_MAP_RATIONAL
);
649 for (i
= 0; i
< dom
->n_div
; ++i
) {
650 int k
= isl_basic_map_alloc_div(bmap
);
653 isl_seq_cpy(bmap
->div
[k
], dom
->div
[i
], 1 + 1 + nparam
);
654 isl_seq_clr(bmap
->div
[k
] + 1 + 1 + nparam
, total
- nparam
);
655 isl_seq_cpy(bmap
->div
[k
] + 1 + 1 + total
,
656 dom
->div
[i
] + 1 + 1 + nparam
, i
);
658 for (i
= 0; i
< dom
->n_eq
; ++i
) {
659 int k
= isl_basic_map_alloc_equality(bmap
);
662 isl_seq_cpy(bmap
->eq
[k
], dom
->eq
[i
], 1 + nparam
);
663 isl_seq_clr(bmap
->eq
[k
] + 1 + nparam
, total
- nparam
);
664 isl_seq_cpy(bmap
->eq
[k
] + 1 + total
,
665 dom
->eq
[i
] + 1 + nparam
, n_div
);
667 for (i
= 0; i
< dom
->n_ineq
; ++i
) {
668 int k
= isl_basic_map_alloc_inequality(bmap
);
671 isl_seq_cpy(bmap
->ineq
[k
], dom
->ineq
[i
], 1 + nparam
);
672 isl_seq_clr(bmap
->ineq
[k
] + 1 + nparam
, total
- nparam
);
673 isl_seq_cpy(bmap
->ineq
[k
] + 1 + total
,
674 dom
->ineq
[i
] + 1 + nparam
, n_div
);
676 for (i
= 0; i
< M
->n_row
- 1; ++i
) {
677 int k
= isl_basic_map_alloc_equality(bmap
);
680 isl_seq_cpy(bmap
->eq
[k
], M
->row
[1 + i
], 1 + nparam
);
681 isl_seq_clr(bmap
->eq
[k
] + 1 + nparam
, n_out
);
682 isl_int_neg(bmap
->eq
[k
][1 + nparam
+ i
], M
->row
[0][0]);
683 isl_seq_cpy(bmap
->eq
[k
] + 1 + nparam
+ n_out
,
684 M
->row
[1 + i
] + 1 + nparam
, n_div
);
686 bmap
= isl_basic_map_simplify(bmap
);
687 bmap
= isl_basic_map_finalize(bmap
);
688 sol
->map
= isl_map_grow(sol
->map
, 1);
689 sol
->map
= isl_map_add_basic_map(sol
->map
, bmap
);
690 isl_basic_set_free(dom
);
696 isl_basic_set_free(dom
);
698 isl_basic_map_free(bmap
);
702 static void sol_map_add_wrap(struct isl_sol
*sol
,
703 struct isl_basic_set
*dom
, struct isl_mat
*M
)
705 sol_map_add((struct isl_sol_map
*)sol
, dom
, M
);
709 /* Store the "parametric constant" of row "row" of tableau "tab" in "line",
710 * i.e., the constant term and the coefficients of all variables that
711 * appear in the context tableau.
712 * Note that the coefficient of the big parameter M is NOT copied.
713 * The context tableau may not have a big parameter and even when it
714 * does, it is a different big parameter.
716 static void get_row_parameter_line(struct isl_tab
*tab
, int row
, isl_int
*line
)
719 unsigned off
= 2 + tab
->M
;
721 isl_int_set(line
[0], tab
->mat
->row
[row
][1]);
722 for (i
= 0; i
< tab
->n_param
; ++i
) {
723 if (tab
->var
[i
].is_row
)
724 isl_int_set_si(line
[1 + i
], 0);
726 int col
= tab
->var
[i
].index
;
727 isl_int_set(line
[1 + i
], tab
->mat
->row
[row
][off
+ col
]);
730 for (i
= 0; i
< tab
->n_div
; ++i
) {
731 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
732 isl_int_set_si(line
[1 + tab
->n_param
+ i
], 0);
734 int col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
735 isl_int_set(line
[1 + tab
->n_param
+ i
],
736 tab
->mat
->row
[row
][off
+ col
]);
741 /* Check if rows "row1" and "row2" have identical "parametric constants",
742 * as explained above.
743 * In this case, we also insist that the coefficients of the big parameter
744 * be the same as the values of the constants will only be the same
745 * if these coefficients are also the same.
747 static int identical_parameter_line(struct isl_tab
*tab
, int row1
, int row2
)
750 unsigned off
= 2 + tab
->M
;
752 if (isl_int_ne(tab
->mat
->row
[row1
][1], tab
->mat
->row
[row2
][1]))
755 if (tab
->M
&& isl_int_ne(tab
->mat
->row
[row1
][2],
756 tab
->mat
->row
[row2
][2]))
759 for (i
= 0; i
< tab
->n_param
+ tab
->n_div
; ++i
) {
760 int pos
= i
< tab
->n_param
? i
:
761 tab
->n_var
- tab
->n_div
+ i
- tab
->n_param
;
764 if (tab
->var
[pos
].is_row
)
766 col
= tab
->var
[pos
].index
;
767 if (isl_int_ne(tab
->mat
->row
[row1
][off
+ col
],
768 tab
->mat
->row
[row2
][off
+ col
]))
774 /* Return an inequality that expresses that the "parametric constant"
775 * should be non-negative.
776 * This function is only called when the coefficient of the big parameter
779 static struct isl_vec
*get_row_parameter_ineq(struct isl_tab
*tab
, int row
)
781 struct isl_vec
*ineq
;
783 ineq
= isl_vec_alloc(tab
->mat
->ctx
, 1 + tab
->n_param
+ tab
->n_div
);
787 get_row_parameter_line(tab
, row
, ineq
->el
);
789 ineq
= isl_vec_normalize(ineq
);
794 /* Normalize a div expression of the form
796 * [(g*f(x) + c)/(g * m)]
798 * with c the constant term and f(x) the remaining coefficients, to
802 static void normalize_div(__isl_keep isl_vec
*div
)
804 isl_ctx
*ctx
= isl_vec_get_ctx(div
);
805 int len
= div
->size
- 2;
807 isl_seq_gcd(div
->el
+ 2, len
, &ctx
->normalize_gcd
);
808 isl_int_gcd(ctx
->normalize_gcd
, ctx
->normalize_gcd
, div
->el
[0]);
810 if (isl_int_is_one(ctx
->normalize_gcd
))
813 isl_int_divexact(div
->el
[0], div
->el
[0], ctx
->normalize_gcd
);
814 isl_int_fdiv_q(div
->el
[1], div
->el
[1], ctx
->normalize_gcd
);
815 isl_seq_scale_down(div
->el
+ 2, div
->el
+ 2, ctx
->normalize_gcd
, len
);
818 /* Return an integer division for use in a parametric cut based
820 * In particular, let the parametric constant of the row be
824 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
825 * The div returned is equal to
827 * floor(\sum_i {-a_i} y_i) = floor((\sum_i (-a_i mod d) y_i)/d)
829 static struct isl_vec
*get_row_parameter_div(struct isl_tab
*tab
, int row
)
833 div
= isl_vec_alloc(tab
->mat
->ctx
, 1 + 1 + tab
->n_param
+ tab
->n_div
);
837 isl_int_set(div
->el
[0], tab
->mat
->row
[row
][0]);
838 get_row_parameter_line(tab
, row
, div
->el
+ 1);
839 isl_seq_neg(div
->el
+ 1, div
->el
+ 1, div
->size
- 1);
841 isl_seq_fdiv_r(div
->el
+ 1, div
->el
+ 1, div
->el
[0], div
->size
- 1);
846 /* Return an integer division for use in transferring an integrality constraint
848 * In particular, let the parametric constant of the row be
852 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
853 * The the returned div is equal to
855 * floor(\sum_i {a_i} y_i) = floor((\sum_i (a_i mod d) y_i)/d)
857 static struct isl_vec
*get_row_split_div(struct isl_tab
*tab
, int row
)
861 div
= isl_vec_alloc(tab
->mat
->ctx
, 1 + 1 + tab
->n_param
+ tab
->n_div
);
865 isl_int_set(div
->el
[0], tab
->mat
->row
[row
][0]);
866 get_row_parameter_line(tab
, row
, div
->el
+ 1);
868 isl_seq_fdiv_r(div
->el
+ 1, div
->el
+ 1, div
->el
[0], div
->size
- 1);
873 /* Construct and return an inequality that expresses an upper bound
875 * In particular, if the div is given by
879 * then the inequality expresses
883 static struct isl_vec
*ineq_for_div(struct isl_basic_set
*bset
, unsigned div
)
887 struct isl_vec
*ineq
;
892 total
= isl_basic_set_total_dim(bset
);
893 div_pos
= 1 + total
- bset
->n_div
+ div
;
895 ineq
= isl_vec_alloc(bset
->ctx
, 1 + total
);
899 isl_seq_cpy(ineq
->el
, bset
->div
[div
] + 1, 1 + total
);
900 isl_int_neg(ineq
->el
[div_pos
], bset
->div
[div
][0]);
904 /* Given a row in the tableau and a div that was created
905 * using get_row_split_div and that has been constrained to equality, i.e.,
907 * d = floor(\sum_i {a_i} y_i) = \sum_i {a_i} y_i
909 * replace the expression "\sum_i {a_i} y_i" in the row by d,
910 * i.e., we subtract "\sum_i {a_i} y_i" and add 1 d.
911 * The coefficients of the non-parameters in the tableau have been
912 * verified to be integral. We can therefore simply replace coefficient b
913 * by floor(b). For the coefficients of the parameters we have
914 * floor(a_i) = a_i - {a_i}, while for the other coefficients, we have
917 static struct isl_tab
*set_row_cst_to_div(struct isl_tab
*tab
, int row
, int div
)
919 isl_seq_fdiv_q(tab
->mat
->row
[row
] + 1, tab
->mat
->row
[row
] + 1,
920 tab
->mat
->row
[row
][0], 1 + tab
->M
+ tab
->n_col
);
922 isl_int_set_si(tab
->mat
->row
[row
][0], 1);
924 if (tab
->var
[tab
->n_var
- tab
->n_div
+ div
].is_row
) {
925 int drow
= tab
->var
[tab
->n_var
- tab
->n_div
+ div
].index
;
927 isl_assert(tab
->mat
->ctx
,
928 isl_int_is_one(tab
->mat
->row
[drow
][0]), goto error
);
929 isl_seq_combine(tab
->mat
->row
[row
] + 1,
930 tab
->mat
->ctx
->one
, tab
->mat
->row
[row
] + 1,
931 tab
->mat
->ctx
->one
, tab
->mat
->row
[drow
] + 1,
932 1 + tab
->M
+ tab
->n_col
);
934 int dcol
= tab
->var
[tab
->n_var
- tab
->n_div
+ div
].index
;
936 isl_int_add_ui(tab
->mat
->row
[row
][2 + tab
->M
+ dcol
],
937 tab
->mat
->row
[row
][2 + tab
->M
+ dcol
], 1);
946 /* Check if the (parametric) constant of the given row is obviously
947 * negative, meaning that we don't need to consult the context tableau.
948 * If there is a big parameter and its coefficient is non-zero,
949 * then this coefficient determines the outcome.
950 * Otherwise, we check whether the constant is negative and
951 * all non-zero coefficients of parameters are negative and
952 * belong to non-negative parameters.
954 static int is_obviously_neg(struct isl_tab
*tab
, int row
)
958 unsigned off
= 2 + tab
->M
;
961 if (isl_int_is_pos(tab
->mat
->row
[row
][2]))
963 if (isl_int_is_neg(tab
->mat
->row
[row
][2]))
967 if (isl_int_is_nonneg(tab
->mat
->row
[row
][1]))
969 for (i
= 0; i
< tab
->n_param
; ++i
) {
970 /* Eliminated parameter */
971 if (tab
->var
[i
].is_row
)
973 col
= tab
->var
[i
].index
;
974 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
976 if (!tab
->var
[i
].is_nonneg
)
978 if (isl_int_is_pos(tab
->mat
->row
[row
][off
+ col
]))
981 for (i
= 0; i
< tab
->n_div
; ++i
) {
982 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
984 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
985 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
987 if (!tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_nonneg
)
989 if (isl_int_is_pos(tab
->mat
->row
[row
][off
+ col
]))
995 /* Check if the (parametric) constant of the given row is obviously
996 * non-negative, meaning that we don't need to consult the context tableau.
997 * If there is a big parameter and its coefficient is non-zero,
998 * then this coefficient determines the outcome.
999 * Otherwise, we check whether the constant is non-negative and
1000 * all non-zero coefficients of parameters are positive and
1001 * belong to non-negative parameters.
1003 static int is_obviously_nonneg(struct isl_tab
*tab
, int row
)
1007 unsigned off
= 2 + tab
->M
;
1010 if (isl_int_is_pos(tab
->mat
->row
[row
][2]))
1012 if (isl_int_is_neg(tab
->mat
->row
[row
][2]))
1016 if (isl_int_is_neg(tab
->mat
->row
[row
][1]))
1018 for (i
= 0; i
< tab
->n_param
; ++i
) {
1019 /* Eliminated parameter */
1020 if (tab
->var
[i
].is_row
)
1022 col
= tab
->var
[i
].index
;
1023 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
1025 if (!tab
->var
[i
].is_nonneg
)
1027 if (isl_int_is_neg(tab
->mat
->row
[row
][off
+ col
]))
1030 for (i
= 0; i
< tab
->n_div
; ++i
) {
1031 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
1033 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
1034 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
1036 if (!tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_nonneg
)
1038 if (isl_int_is_neg(tab
->mat
->row
[row
][off
+ col
]))
1044 /* Given a row r and two columns, return the column that would
1045 * lead to the lexicographically smallest increment in the sample
1046 * solution when leaving the basis in favor of the row.
1047 * Pivoting with column c will increment the sample value by a non-negative
1048 * constant times a_{V,c}/a_{r,c}, with a_{V,c} the elements of column c
1049 * corresponding to the non-parametric variables.
1050 * If variable v appears in a column c_v, the a_{v,c} = 1 iff c = c_v,
1051 * with all other entries in this virtual row equal to zero.
1052 * If variable v appears in a row, then a_{v,c} is the element in column c
1055 * Let v be the first variable with a_{v,c1}/a_{r,c1} != a_{v,c2}/a_{r,c2}.
1056 * Then if a_{v,c1}/a_{r,c1} < a_{v,c2}/a_{r,c2}, i.e.,
1057 * a_{v,c2} a_{r,c1} - a_{v,c1} a_{r,c2} > 0, c1 results in the minimal
1058 * increment. Otherwise, it's c2.
1060 static int lexmin_col_pair(struct isl_tab
*tab
,
1061 int row
, int col1
, int col2
, isl_int tmp
)
1066 tr
= tab
->mat
->row
[row
] + 2 + tab
->M
;
1068 for (i
= tab
->n_param
; i
< tab
->n_var
- tab
->n_div
; ++i
) {
1072 if (!tab
->var
[i
].is_row
) {
1073 if (tab
->var
[i
].index
== col1
)
1075 if (tab
->var
[i
].index
== col2
)
1080 if (tab
->var
[i
].index
== row
)
1083 r
= tab
->mat
->row
[tab
->var
[i
].index
] + 2 + tab
->M
;
1084 s1
= isl_int_sgn(r
[col1
]);
1085 s2
= isl_int_sgn(r
[col2
]);
1086 if (s1
== 0 && s2
== 0)
1093 isl_int_mul(tmp
, r
[col2
], tr
[col1
]);
1094 isl_int_submul(tmp
, r
[col1
], tr
[col2
]);
1095 if (isl_int_is_pos(tmp
))
1097 if (isl_int_is_neg(tmp
))
1103 /* Given a row in the tableau, find and return the column that would
1104 * result in the lexicographically smallest, but positive, increment
1105 * in the sample point.
1106 * If there is no such column, then return tab->n_col.
1107 * If anything goes wrong, return -1.
1109 static int lexmin_pivot_col(struct isl_tab
*tab
, int row
)
1112 int col
= tab
->n_col
;
1116 tr
= tab
->mat
->row
[row
] + 2 + tab
->M
;
1120 for (j
= tab
->n_dead
; j
< tab
->n_col
; ++j
) {
1121 if (tab
->col_var
[j
] >= 0 &&
1122 (tab
->col_var
[j
] < tab
->n_param
||
1123 tab
->col_var
[j
] >= tab
->n_var
- tab
->n_div
))
1126 if (!isl_int_is_pos(tr
[j
]))
1129 if (col
== tab
->n_col
)
1132 col
= lexmin_col_pair(tab
, row
, col
, j
, tmp
);
1133 isl_assert(tab
->mat
->ctx
, col
>= 0, goto error
);
1143 /* Return the first known violated constraint, i.e., a non-negative
1144 * constraint that currently has an either obviously negative value
1145 * or a previously determined to be negative value.
1147 * If any constraint has a negative coefficient for the big parameter,
1148 * if any, then we return one of these first.
1150 static int first_neg(struct isl_tab
*tab
)
1155 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
1156 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
1158 if (!isl_int_is_neg(tab
->mat
->row
[row
][2]))
1161 tab
->row_sign
[row
] = isl_tab_row_neg
;
1164 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
1165 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
1167 if (tab
->row_sign
) {
1168 if (tab
->row_sign
[row
] == 0 &&
1169 is_obviously_neg(tab
, row
))
1170 tab
->row_sign
[row
] = isl_tab_row_neg
;
1171 if (tab
->row_sign
[row
] != isl_tab_row_neg
)
1173 } else if (!is_obviously_neg(tab
, row
))
1180 /* Check whether the invariant that all columns are lexico-positive
1181 * is satisfied. This function is not called from the current code
1182 * but is useful during debugging.
1184 static void check_lexpos(struct isl_tab
*tab
) __attribute__ ((unused
));
1185 static void check_lexpos(struct isl_tab
*tab
)
1187 unsigned off
= 2 + tab
->M
;
1192 for (col
= tab
->n_dead
; col
< tab
->n_col
; ++col
) {
1193 if (tab
->col_var
[col
] >= 0 &&
1194 (tab
->col_var
[col
] < tab
->n_param
||
1195 tab
->col_var
[col
] >= tab
->n_var
- tab
->n_div
))
1197 for (var
= tab
->n_param
; var
< tab
->n_var
- tab
->n_div
; ++var
) {
1198 if (!tab
->var
[var
].is_row
) {
1199 if (tab
->var
[var
].index
== col
)
1204 row
= tab
->var
[var
].index
;
1205 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
1207 if (isl_int_is_pos(tab
->mat
->row
[row
][off
+ col
]))
1209 fprintf(stderr
, "lexneg column %d (row %d)\n",
1212 if (var
>= tab
->n_var
- tab
->n_div
)
1213 fprintf(stderr
, "zero column %d\n", col
);
1217 /* Report to the caller that the given constraint is part of an encountered
1220 static int report_conflicting_constraint(struct isl_tab
*tab
, int con
)
1222 return tab
->conflict(con
, tab
->conflict_user
);
1225 /* Given a conflicting row in the tableau, report all constraints
1226 * involved in the row to the caller. That is, the row itself
1227 * (if it represents a constraint) and all constraint columns with
1228 * non-zero (and therefore negative) coefficients.
1230 static int report_conflict(struct isl_tab
*tab
, int row
)
1238 if (tab
->row_var
[row
] < 0 &&
1239 report_conflicting_constraint(tab
, ~tab
->row_var
[row
]) < 0)
1242 tr
= tab
->mat
->row
[row
] + 2 + tab
->M
;
1244 for (j
= tab
->n_dead
; j
< tab
->n_col
; ++j
) {
1245 if (tab
->col_var
[j
] >= 0 &&
1246 (tab
->col_var
[j
] < tab
->n_param
||
1247 tab
->col_var
[j
] >= tab
->n_var
- tab
->n_div
))
1250 if (!isl_int_is_neg(tr
[j
]))
1253 if (tab
->col_var
[j
] < 0 &&
1254 report_conflicting_constraint(tab
, ~tab
->col_var
[j
]) < 0)
1261 /* Resolve all known or obviously violated constraints through pivoting.
1262 * In particular, as long as we can find any violated constraint, we
1263 * look for a pivoting column that would result in the lexicographically
1264 * smallest increment in the sample point. If there is no such column
1265 * then the tableau is infeasible.
1267 static int restore_lexmin(struct isl_tab
*tab
) WARN_UNUSED
;
1268 static int restore_lexmin(struct isl_tab
*tab
)
1276 while ((row
= first_neg(tab
)) != -1) {
1277 col
= lexmin_pivot_col(tab
, row
);
1278 if (col
>= tab
->n_col
) {
1279 if (report_conflict(tab
, row
) < 0)
1281 if (isl_tab_mark_empty(tab
) < 0)
1287 if (isl_tab_pivot(tab
, row
, col
) < 0)
1293 /* Given a row that represents an equality, look for an appropriate
1295 * In particular, if there are any non-zero coefficients among
1296 * the non-parameter variables, then we take the last of these
1297 * variables. Eliminating this variable in terms of the other
1298 * variables and/or parameters does not influence the property
1299 * that all column in the initial tableau are lexicographically
1300 * positive. The row corresponding to the eliminated variable
1301 * will only have non-zero entries below the diagonal of the
1302 * initial tableau. That is, we transform
1308 * If there is no such non-parameter variable, then we are dealing with
1309 * pure parameter equality and we pick any parameter with coefficient 1 or -1
1310 * for elimination. This will ensure that the eliminated parameter
1311 * always has an integer value whenever all the other parameters are integral.
1312 * If there is no such parameter then we return -1.
1314 static int last_var_col_or_int_par_col(struct isl_tab
*tab
, int row
)
1316 unsigned off
= 2 + tab
->M
;
1319 for (i
= tab
->n_var
- tab
->n_div
- 1; i
>= 0 && i
>= tab
->n_param
; --i
) {
1321 if (tab
->var
[i
].is_row
)
1323 col
= tab
->var
[i
].index
;
1324 if (col
<= tab
->n_dead
)
1326 if (!isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
1329 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
1330 if (isl_int_is_one(tab
->mat
->row
[row
][off
+ i
]))
1332 if (isl_int_is_negone(tab
->mat
->row
[row
][off
+ i
]))
1338 /* Add an equality that is known to be valid to the tableau.
1339 * We first check if we can eliminate a variable or a parameter.
1340 * If not, we add the equality as two inequalities.
1341 * In this case, the equality was a pure parameter equality and there
1342 * is no need to resolve any constraint violations.
1344 * This function assumes that at least two more rows and at least
1345 * two more elements in the constraint array are available in the tableau.
1347 static struct isl_tab
*add_lexmin_valid_eq(struct isl_tab
*tab
, isl_int
*eq
)
1354 r
= isl_tab_add_row(tab
, eq
);
1358 r
= tab
->con
[r
].index
;
1359 i
= last_var_col_or_int_par_col(tab
, r
);
1361 tab
->con
[r
].is_nonneg
= 1;
1362 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
1364 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
1365 r
= isl_tab_add_row(tab
, eq
);
1368 tab
->con
[r
].is_nonneg
= 1;
1369 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
1372 if (isl_tab_pivot(tab
, r
, i
) < 0)
1374 if (isl_tab_kill_col(tab
, i
) < 0)
1385 /* Check if the given row is a pure constant.
1387 static int is_constant(struct isl_tab
*tab
, int row
)
1389 unsigned off
= 2 + tab
->M
;
1391 return isl_seq_first_non_zero(tab
->mat
->row
[row
] + off
+ tab
->n_dead
,
1392 tab
->n_col
- tab
->n_dead
) == -1;
1395 /* Add an equality that may or may not be valid to the tableau.
1396 * If the resulting row is a pure constant, then it must be zero.
1397 * Otherwise, the resulting tableau is empty.
1399 * If the row is not a pure constant, then we add two inequalities,
1400 * each time checking that they can be satisfied.
1401 * In the end we try to use one of the two constraints to eliminate
1404 * This function assumes that at least two more rows and at least
1405 * two more elements in the constraint array are available in the tableau.
1407 static int add_lexmin_eq(struct isl_tab
*tab
, isl_int
*eq
) WARN_UNUSED
;
1408 static int add_lexmin_eq(struct isl_tab
*tab
, isl_int
*eq
)
1412 struct isl_tab_undo
*snap
;
1416 snap
= isl_tab_snap(tab
);
1417 r1
= isl_tab_add_row(tab
, eq
);
1420 tab
->con
[r1
].is_nonneg
= 1;
1421 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r1
]) < 0)
1424 row
= tab
->con
[r1
].index
;
1425 if (is_constant(tab
, row
)) {
1426 if (!isl_int_is_zero(tab
->mat
->row
[row
][1]) ||
1427 (tab
->M
&& !isl_int_is_zero(tab
->mat
->row
[row
][2]))) {
1428 if (isl_tab_mark_empty(tab
) < 0)
1432 if (isl_tab_rollback(tab
, snap
) < 0)
1437 if (restore_lexmin(tab
) < 0)
1442 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
1444 r2
= isl_tab_add_row(tab
, eq
);
1447 tab
->con
[r2
].is_nonneg
= 1;
1448 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r2
]) < 0)
1451 if (restore_lexmin(tab
) < 0)
1456 if (!tab
->con
[r1
].is_row
) {
1457 if (isl_tab_kill_col(tab
, tab
->con
[r1
].index
) < 0)
1459 } else if (!tab
->con
[r2
].is_row
) {
1460 if (isl_tab_kill_col(tab
, tab
->con
[r2
].index
) < 0)
1465 tab
->bmap
= isl_basic_map_add_ineq(tab
->bmap
, eq
);
1466 if (isl_tab_push(tab
, isl_tab_undo_bmap_ineq
) < 0)
1468 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
1469 tab
->bmap
= isl_basic_map_add_ineq(tab
->bmap
, eq
);
1470 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
1471 if (isl_tab_push(tab
, isl_tab_undo_bmap_ineq
) < 0)
1480 /* Add an inequality to the tableau, resolving violations using
1483 * This function assumes that at least one more row and at least
1484 * one more element in the constraint array are available in the tableau.
1486 static struct isl_tab
*add_lexmin_ineq(struct isl_tab
*tab
, isl_int
*ineq
)
1493 tab
->bmap
= isl_basic_map_add_ineq(tab
->bmap
, ineq
);
1494 if (isl_tab_push(tab
, isl_tab_undo_bmap_ineq
) < 0)
1499 r
= isl_tab_add_row(tab
, ineq
);
1502 tab
->con
[r
].is_nonneg
= 1;
1503 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
1505 if (isl_tab_row_is_redundant(tab
, tab
->con
[r
].index
)) {
1506 if (isl_tab_mark_redundant(tab
, tab
->con
[r
].index
) < 0)
1511 if (restore_lexmin(tab
) < 0)
1513 if (!tab
->empty
&& tab
->con
[r
].is_row
&&
1514 isl_tab_row_is_redundant(tab
, tab
->con
[r
].index
))
1515 if (isl_tab_mark_redundant(tab
, tab
->con
[r
].index
) < 0)
1523 /* Check if the coefficients of the parameters are all integral.
1525 static int integer_parameter(struct isl_tab
*tab
, int row
)
1529 unsigned off
= 2 + tab
->M
;
1531 for (i
= 0; i
< tab
->n_param
; ++i
) {
1532 /* Eliminated parameter */
1533 if (tab
->var
[i
].is_row
)
1535 col
= tab
->var
[i
].index
;
1536 if (!isl_int_is_divisible_by(tab
->mat
->row
[row
][off
+ col
],
1537 tab
->mat
->row
[row
][0]))
1540 for (i
= 0; i
< tab
->n_div
; ++i
) {
1541 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
1543 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
1544 if (!isl_int_is_divisible_by(tab
->mat
->row
[row
][off
+ col
],
1545 tab
->mat
->row
[row
][0]))
1551 /* Check if the coefficients of the non-parameter variables are all integral.
1553 static int integer_variable(struct isl_tab
*tab
, int row
)
1556 unsigned off
= 2 + tab
->M
;
1558 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
1559 if (tab
->col_var
[i
] >= 0 &&
1560 (tab
->col_var
[i
] < tab
->n_param
||
1561 tab
->col_var
[i
] >= tab
->n_var
- tab
->n_div
))
1563 if (!isl_int_is_divisible_by(tab
->mat
->row
[row
][off
+ i
],
1564 tab
->mat
->row
[row
][0]))
1570 /* Check if the constant term is integral.
1572 static int integer_constant(struct isl_tab
*tab
, int row
)
1574 return isl_int_is_divisible_by(tab
->mat
->row
[row
][1],
1575 tab
->mat
->row
[row
][0]);
1578 #define I_CST 1 << 0
1579 #define I_PAR 1 << 1
1580 #define I_VAR 1 << 2
1582 /* Check for next (non-parameter) variable after "var" (first if var == -1)
1583 * that is non-integer and therefore requires a cut and return
1584 * the index of the variable.
1585 * For parametric tableaus, there are three parts in a row,
1586 * the constant, the coefficients of the parameters and the rest.
1587 * For each part, we check whether the coefficients in that part
1588 * are all integral and if so, set the corresponding flag in *f.
1589 * If the constant and the parameter part are integral, then the
1590 * current sample value is integral and no cut is required
1591 * (irrespective of whether the variable part is integral).
1593 static int next_non_integer_var(struct isl_tab
*tab
, int var
, int *f
)
1595 var
= var
< 0 ? tab
->n_param
: var
+ 1;
1597 for (; var
< tab
->n_var
- tab
->n_div
; ++var
) {
1600 if (!tab
->var
[var
].is_row
)
1602 row
= tab
->var
[var
].index
;
1603 if (integer_constant(tab
, row
))
1604 ISL_FL_SET(flags
, I_CST
);
1605 if (integer_parameter(tab
, row
))
1606 ISL_FL_SET(flags
, I_PAR
);
1607 if (ISL_FL_ISSET(flags
, I_CST
) && ISL_FL_ISSET(flags
, I_PAR
))
1609 if (integer_variable(tab
, row
))
1610 ISL_FL_SET(flags
, I_VAR
);
1617 /* Check for first (non-parameter) variable that is non-integer and
1618 * therefore requires a cut and return the corresponding row.
1619 * For parametric tableaus, there are three parts in a row,
1620 * the constant, the coefficients of the parameters and the rest.
1621 * For each part, we check whether the coefficients in that part
1622 * are all integral and if so, set the corresponding flag in *f.
1623 * If the constant and the parameter part are integral, then the
1624 * current sample value is integral and no cut is required
1625 * (irrespective of whether the variable part is integral).
1627 static int first_non_integer_row(struct isl_tab
*tab
, int *f
)
1629 int var
= next_non_integer_var(tab
, -1, f
);
1631 return var
< 0 ? -1 : tab
->var
[var
].index
;
1634 /* Add a (non-parametric) cut to cut away the non-integral sample
1635 * value of the given row.
1637 * If the row is given by
1639 * m r = f + \sum_i a_i y_i
1643 * c = - {-f/m} + \sum_i {a_i/m} y_i >= 0
1645 * The big parameter, if any, is ignored, since it is assumed to be big
1646 * enough to be divisible by any integer.
1647 * If the tableau is actually a parametric tableau, then this function
1648 * is only called when all coefficients of the parameters are integral.
1649 * The cut therefore has zero coefficients for the parameters.
1651 * The current value is known to be negative, so row_sign, if it
1652 * exists, is set accordingly.
1654 * Return the row of the cut or -1.
1656 static int add_cut(struct isl_tab
*tab
, int row
)
1661 unsigned off
= 2 + tab
->M
;
1663 if (isl_tab_extend_cons(tab
, 1) < 0)
1665 r
= isl_tab_allocate_con(tab
);
1669 r_row
= tab
->mat
->row
[tab
->con
[r
].index
];
1670 isl_int_set(r_row
[0], tab
->mat
->row
[row
][0]);
1671 isl_int_neg(r_row
[1], tab
->mat
->row
[row
][1]);
1672 isl_int_fdiv_r(r_row
[1], r_row
[1], tab
->mat
->row
[row
][0]);
1673 isl_int_neg(r_row
[1], r_row
[1]);
1675 isl_int_set_si(r_row
[2], 0);
1676 for (i
= 0; i
< tab
->n_col
; ++i
)
1677 isl_int_fdiv_r(r_row
[off
+ i
],
1678 tab
->mat
->row
[row
][off
+ i
], tab
->mat
->row
[row
][0]);
1680 tab
->con
[r
].is_nonneg
= 1;
1681 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
1684 tab
->row_sign
[tab
->con
[r
].index
] = isl_tab_row_neg
;
1686 return tab
->con
[r
].index
;
1692 /* Given a non-parametric tableau, add cuts until an integer
1693 * sample point is obtained or until the tableau is determined
1694 * to be integer infeasible.
1695 * As long as there is any non-integer value in the sample point,
1696 * we add appropriate cuts, if possible, for each of these
1697 * non-integer values and then resolve the violated
1698 * cut constraints using restore_lexmin.
1699 * If one of the corresponding rows is equal to an integral
1700 * combination of variables/constraints plus a non-integral constant,
1701 * then there is no way to obtain an integer point and we return
1702 * a tableau that is marked empty.
1703 * The parameter cutting_strategy controls the strategy used when adding cuts
1704 * to remove non-integer points. CUT_ALL adds all possible cuts
1705 * before continuing the search. CUT_ONE adds only one cut at a time.
1707 static struct isl_tab
*cut_to_integer_lexmin(struct isl_tab
*tab
,
1708 int cutting_strategy
)
1719 while ((var
= next_non_integer_var(tab
, -1, &flags
)) != -1) {
1721 if (ISL_FL_ISSET(flags
, I_VAR
)) {
1722 if (isl_tab_mark_empty(tab
) < 0)
1726 row
= tab
->var
[var
].index
;
1727 row
= add_cut(tab
, row
);
1730 if (cutting_strategy
== CUT_ONE
)
1732 } while ((var
= next_non_integer_var(tab
, var
, &flags
)) != -1);
1733 if (restore_lexmin(tab
) < 0)
1744 /* Check whether all the currently active samples also satisfy the inequality
1745 * "ineq" (treated as an equality if eq is set).
1746 * Remove those samples that do not.
1748 static struct isl_tab
*check_samples(struct isl_tab
*tab
, isl_int
*ineq
, int eq
)
1756 isl_assert(tab
->mat
->ctx
, tab
->bmap
, goto error
);
1757 isl_assert(tab
->mat
->ctx
, tab
->samples
, goto error
);
1758 isl_assert(tab
->mat
->ctx
, tab
->samples
->n_col
== 1 + tab
->n_var
, goto error
);
1761 for (i
= tab
->n_outside
; i
< tab
->n_sample
; ++i
) {
1763 isl_seq_inner_product(ineq
, tab
->samples
->row
[i
],
1764 1 + tab
->n_var
, &v
);
1765 sgn
= isl_int_sgn(v
);
1766 if (eq
? (sgn
== 0) : (sgn
>= 0))
1768 tab
= isl_tab_drop_sample(tab
, i
);
1780 /* Check whether the sample value of the tableau is finite,
1781 * i.e., either the tableau does not use a big parameter, or
1782 * all values of the variables are equal to the big parameter plus
1783 * some constant. This constant is the actual sample value.
1785 static int sample_is_finite(struct isl_tab
*tab
)
1792 for (i
= 0; i
< tab
->n_var
; ++i
) {
1794 if (!tab
->var
[i
].is_row
)
1796 row
= tab
->var
[i
].index
;
1797 if (isl_int_ne(tab
->mat
->row
[row
][0], tab
->mat
->row
[row
][2]))
1803 /* Check if the context tableau of sol has any integer points.
1804 * Leave tab in empty state if no integer point can be found.
1805 * If an integer point can be found and if moreover it is finite,
1806 * then it is added to the list of sample values.
1808 * This function is only called when none of the currently active sample
1809 * values satisfies the most recently added constraint.
1811 static struct isl_tab
*check_integer_feasible(struct isl_tab
*tab
)
1813 struct isl_tab_undo
*snap
;
1818 snap
= isl_tab_snap(tab
);
1819 if (isl_tab_push_basis(tab
) < 0)
1822 tab
= cut_to_integer_lexmin(tab
, CUT_ALL
);
1826 if (!tab
->empty
&& sample_is_finite(tab
)) {
1827 struct isl_vec
*sample
;
1829 sample
= isl_tab_get_sample_value(tab
);
1831 if (isl_tab_add_sample(tab
, sample
) < 0)
1835 if (!tab
->empty
&& isl_tab_rollback(tab
, snap
) < 0)
1844 /* Check if any of the currently active sample values satisfies
1845 * the inequality "ineq" (an equality if eq is set).
1847 static int tab_has_valid_sample(struct isl_tab
*tab
, isl_int
*ineq
, int eq
)
1855 isl_assert(tab
->mat
->ctx
, tab
->bmap
, return -1);
1856 isl_assert(tab
->mat
->ctx
, tab
->samples
, return -1);
1857 isl_assert(tab
->mat
->ctx
, tab
->samples
->n_col
== 1 + tab
->n_var
, return -1);
1860 for (i
= tab
->n_outside
; i
< tab
->n_sample
; ++i
) {
1862 isl_seq_inner_product(ineq
, tab
->samples
->row
[i
],
1863 1 + tab
->n_var
, &v
);
1864 sgn
= isl_int_sgn(v
);
1865 if (eq
? (sgn
== 0) : (sgn
>= 0))
1870 return i
< tab
->n_sample
;
1873 /* Insert a div specified by "div" to the tableau "tab" at position "pos" and
1874 * return isl_bool_true if the div is obviously non-negative.
1876 static isl_bool
context_tab_insert_div(struct isl_tab
*tab
, int pos
,
1877 __isl_keep isl_vec
*div
,
1878 int (*add_ineq
)(void *user
, isl_int
*), void *user
)
1882 struct isl_mat
*samples
;
1885 r
= isl_tab_insert_div(tab
, pos
, div
, add_ineq
, user
);
1887 return isl_bool_error
;
1888 nonneg
= tab
->var
[r
].is_nonneg
;
1889 tab
->var
[r
].frozen
= 1;
1891 samples
= isl_mat_extend(tab
->samples
,
1892 tab
->n_sample
, 1 + tab
->n_var
);
1893 tab
->samples
= samples
;
1895 return isl_bool_error
;
1896 for (i
= tab
->n_outside
; i
< samples
->n_row
; ++i
) {
1897 isl_seq_inner_product(div
->el
+ 1, samples
->row
[i
],
1898 div
->size
- 1, &samples
->row
[i
][samples
->n_col
- 1]);
1899 isl_int_fdiv_q(samples
->row
[i
][samples
->n_col
- 1],
1900 samples
->row
[i
][samples
->n_col
- 1], div
->el
[0]);
1902 tab
->samples
= isl_mat_move_cols(tab
->samples
, 1 + pos
,
1903 1 + tab
->n_var
- 1, 1);
1905 return isl_bool_error
;
1910 /* Add a div specified by "div" to both the main tableau and
1911 * the context tableau. In case of the main tableau, we only
1912 * need to add an extra div. In the context tableau, we also
1913 * need to express the meaning of the div.
1914 * Return the index of the div or -1 if anything went wrong.
1916 * The new integer division is added before any unknown integer
1917 * divisions in the context to ensure that it does not get
1918 * equated to some linear combination involving unknown integer
1921 static int add_div(struct isl_tab
*tab
, struct isl_context
*context
,
1922 __isl_keep isl_vec
*div
)
1927 struct isl_tab
*context_tab
= context
->op
->peek_tab(context
);
1929 if (!tab
|| !context_tab
)
1932 pos
= context_tab
->n_var
- context
->n_unknown
;
1933 if ((nonneg
= context
->op
->insert_div(context
, pos
, div
)) < 0)
1936 if (!context
->op
->is_ok(context
))
1939 pos
= tab
->n_var
- context
->n_unknown
;
1940 if (isl_tab_extend_vars(tab
, 1) < 0)
1942 r
= isl_tab_insert_var(tab
, pos
);
1946 tab
->var
[r
].is_nonneg
= 1;
1947 tab
->var
[r
].frozen
= 1;
1950 return tab
->n_div
- 1 - context
->n_unknown
;
1952 context
->op
->invalidate(context
);
1956 static int find_div(struct isl_tab
*tab
, isl_int
*div
, isl_int denom
)
1959 unsigned total
= isl_basic_map_total_dim(tab
->bmap
);
1961 for (i
= 0; i
< tab
->bmap
->n_div
; ++i
) {
1962 if (isl_int_ne(tab
->bmap
->div
[i
][0], denom
))
1964 if (!isl_seq_eq(tab
->bmap
->div
[i
] + 1, div
, 1 + total
))
1971 /* Return the index of a div that corresponds to "div".
1972 * We first check if we already have such a div and if not, we create one.
1974 static int get_div(struct isl_tab
*tab
, struct isl_context
*context
,
1975 struct isl_vec
*div
)
1978 struct isl_tab
*context_tab
= context
->op
->peek_tab(context
);
1983 d
= find_div(context_tab
, div
->el
+ 1, div
->el
[0]);
1987 return add_div(tab
, context
, div
);
1990 /* Add a parametric cut to cut away the non-integral sample value
1992 * Let a_i be the coefficients of the constant term and the parameters
1993 * and let b_i be the coefficients of the variables or constraints
1994 * in basis of the tableau.
1995 * Let q be the div q = floor(\sum_i {-a_i} y_i).
1997 * The cut is expressed as
1999 * c = \sum_i -{-a_i} y_i + \sum_i {b_i} x_i + q >= 0
2001 * If q did not already exist in the context tableau, then it is added first.
2002 * If q is in a column of the main tableau then the "+ q" can be accomplished
2003 * by setting the corresponding entry to the denominator of the constraint.
2004 * If q happens to be in a row of the main tableau, then the corresponding
2005 * row needs to be added instead (taking care of the denominators).
2006 * Note that this is very unlikely, but perhaps not entirely impossible.
2008 * The current value of the cut is known to be negative (or at least
2009 * non-positive), so row_sign is set accordingly.
2011 * Return the row of the cut or -1.
2013 static int add_parametric_cut(struct isl_tab
*tab
, int row
,
2014 struct isl_context
*context
)
2016 struct isl_vec
*div
;
2023 unsigned off
= 2 + tab
->M
;
2028 div
= get_row_parameter_div(tab
, row
);
2032 n
= tab
->n_div
- context
->n_unknown
;
2033 d
= context
->op
->get_div(context
, tab
, div
);
2038 if (isl_tab_extend_cons(tab
, 1) < 0)
2040 r
= isl_tab_allocate_con(tab
);
2044 r_row
= tab
->mat
->row
[tab
->con
[r
].index
];
2045 isl_int_set(r_row
[0], tab
->mat
->row
[row
][0]);
2046 isl_int_neg(r_row
[1], tab
->mat
->row
[row
][1]);
2047 isl_int_fdiv_r(r_row
[1], r_row
[1], tab
->mat
->row
[row
][0]);
2048 isl_int_neg(r_row
[1], r_row
[1]);
2050 isl_int_set_si(r_row
[2], 0);
2051 for (i
= 0; i
< tab
->n_param
; ++i
) {
2052 if (tab
->var
[i
].is_row
)
2054 col
= tab
->var
[i
].index
;
2055 isl_int_neg(r_row
[off
+ col
], tab
->mat
->row
[row
][off
+ col
]);
2056 isl_int_fdiv_r(r_row
[off
+ col
], r_row
[off
+ col
],
2057 tab
->mat
->row
[row
][0]);
2058 isl_int_neg(r_row
[off
+ col
], r_row
[off
+ col
]);
2060 for (i
= 0; i
< tab
->n_div
; ++i
) {
2061 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
2063 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
2064 isl_int_neg(r_row
[off
+ col
], tab
->mat
->row
[row
][off
+ col
]);
2065 isl_int_fdiv_r(r_row
[off
+ col
], r_row
[off
+ col
],
2066 tab
->mat
->row
[row
][0]);
2067 isl_int_neg(r_row
[off
+ col
], r_row
[off
+ col
]);
2069 for (i
= 0; i
< tab
->n_col
; ++i
) {
2070 if (tab
->col_var
[i
] >= 0 &&
2071 (tab
->col_var
[i
] < tab
->n_param
||
2072 tab
->col_var
[i
] >= tab
->n_var
- tab
->n_div
))
2074 isl_int_fdiv_r(r_row
[off
+ i
],
2075 tab
->mat
->row
[row
][off
+ i
], tab
->mat
->row
[row
][0]);
2077 if (tab
->var
[tab
->n_var
- tab
->n_div
+ d
].is_row
) {
2079 int d_row
= tab
->var
[tab
->n_var
- tab
->n_div
+ d
].index
;
2081 isl_int_gcd(gcd
, tab
->mat
->row
[d_row
][0], r_row
[0]);
2082 isl_int_divexact(r_row
[0], r_row
[0], gcd
);
2083 isl_int_divexact(gcd
, tab
->mat
->row
[d_row
][0], gcd
);
2084 isl_seq_combine(r_row
+ 1, gcd
, r_row
+ 1,
2085 r_row
[0], tab
->mat
->row
[d_row
] + 1,
2086 off
- 1 + tab
->n_col
);
2087 isl_int_mul(r_row
[0], r_row
[0], tab
->mat
->row
[d_row
][0]);
2090 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ d
].index
;
2091 isl_int_set(r_row
[off
+ col
], tab
->mat
->row
[row
][0]);
2094 tab
->con
[r
].is_nonneg
= 1;
2095 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
2098 tab
->row_sign
[tab
->con
[r
].index
] = isl_tab_row_neg
;
2100 row
= tab
->con
[r
].index
;
2102 if (d
>= n
&& context
->op
->detect_equalities(context
, tab
) < 0)
2108 /* Construct a tableau for bmap that can be used for computing
2109 * the lexicographic minimum (or maximum) of bmap.
2110 * If not NULL, then dom is the domain where the minimum
2111 * should be computed. In this case, we set up a parametric
2112 * tableau with row signs (initialized to "unknown").
2113 * If M is set, then the tableau will use a big parameter.
2114 * If max is set, then a maximum should be computed instead of a minimum.
2115 * This means that for each variable x, the tableau will contain the variable
2116 * x' = M - x, rather than x' = M + x. This in turn means that the coefficient
2117 * of the variables in all constraints are negated prior to adding them
2120 static struct isl_tab
*tab_for_lexmin(struct isl_basic_map
*bmap
,
2121 struct isl_basic_set
*dom
, unsigned M
, int max
)
2124 struct isl_tab
*tab
;
2128 tab
= isl_tab_alloc(bmap
->ctx
, 2 * bmap
->n_eq
+ bmap
->n_ineq
+ 1,
2129 isl_basic_map_total_dim(bmap
), M
);
2133 tab
->rational
= ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
2135 tab
->n_param
= isl_basic_set_total_dim(dom
) - dom
->n_div
;
2136 tab
->n_div
= dom
->n_div
;
2137 tab
->row_sign
= isl_calloc_array(bmap
->ctx
,
2138 enum isl_tab_row_sign
, tab
->mat
->n_row
);
2139 if (tab
->mat
->n_row
&& !tab
->row_sign
)
2142 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
)) {
2143 if (isl_tab_mark_empty(tab
) < 0)
2148 for (i
= tab
->n_param
; i
< tab
->n_var
- tab
->n_div
; ++i
) {
2149 tab
->var
[i
].is_nonneg
= 1;
2150 tab
->var
[i
].frozen
= 1;
2152 o_var
= 1 + tab
->n_param
;
2153 n_var
= tab
->n_var
- tab
->n_param
- tab
->n_div
;
2154 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
2156 isl_seq_neg(bmap
->eq
[i
] + o_var
,
2157 bmap
->eq
[i
] + o_var
, n_var
);
2158 tab
= add_lexmin_valid_eq(tab
, bmap
->eq
[i
]);
2160 isl_seq_neg(bmap
->eq
[i
] + o_var
,
2161 bmap
->eq
[i
] + o_var
, n_var
);
2162 if (!tab
|| tab
->empty
)
2165 if (bmap
->n_eq
&& restore_lexmin(tab
) < 0)
2167 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
2169 isl_seq_neg(bmap
->ineq
[i
] + o_var
,
2170 bmap
->ineq
[i
] + o_var
, n_var
);
2171 tab
= add_lexmin_ineq(tab
, bmap
->ineq
[i
]);
2173 isl_seq_neg(bmap
->ineq
[i
] + o_var
,
2174 bmap
->ineq
[i
] + o_var
, n_var
);
2175 if (!tab
|| tab
->empty
)
2184 /* Given a main tableau where more than one row requires a split,
2185 * determine and return the "best" row to split on.
2187 * Given two rows in the main tableau, if the inequality corresponding
2188 * to the first row is redundant with respect to that of the second row
2189 * in the current tableau, then it is better to split on the second row,
2190 * since in the positive part, both rows will be positive.
2191 * (In the negative part a pivot will have to be performed and just about
2192 * anything can happen to the sign of the other row.)
2194 * As a simple heuristic, we therefore select the row that makes the most
2195 * of the other rows redundant.
2197 * Perhaps it would also be useful to look at the number of constraints
2198 * that conflict with any given constraint.
2200 * best is the best row so far (-1 when we have not found any row yet).
2201 * best_r is the number of other rows made redundant by row best.
2202 * When best is still -1, bset_r is meaningless, but it is initialized
2203 * to some arbitrary value (0) anyway. Without this redundant initialization
2204 * valgrind may warn about uninitialized memory accesses when isl
2205 * is compiled with some versions of gcc.
2207 static int best_split(struct isl_tab
*tab
, struct isl_tab
*context_tab
)
2209 struct isl_tab_undo
*snap
;
2215 if (isl_tab_extend_cons(context_tab
, 2) < 0)
2218 snap
= isl_tab_snap(context_tab
);
2220 for (split
= tab
->n_redundant
; split
< tab
->n_row
; ++split
) {
2221 struct isl_tab_undo
*snap2
;
2222 struct isl_vec
*ineq
= NULL
;
2226 if (!isl_tab_var_from_row(tab
, split
)->is_nonneg
)
2228 if (tab
->row_sign
[split
] != isl_tab_row_any
)
2231 ineq
= get_row_parameter_ineq(tab
, split
);
2234 ok
= isl_tab_add_ineq(context_tab
, ineq
->el
) >= 0;
2239 snap2
= isl_tab_snap(context_tab
);
2241 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
2242 struct isl_tab_var
*var
;
2246 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
2248 if (tab
->row_sign
[row
] != isl_tab_row_any
)
2251 ineq
= get_row_parameter_ineq(tab
, row
);
2254 ok
= isl_tab_add_ineq(context_tab
, ineq
->el
) >= 0;
2258 var
= &context_tab
->con
[context_tab
->n_con
- 1];
2259 if (!context_tab
->empty
&&
2260 !isl_tab_min_at_most_neg_one(context_tab
, var
))
2262 if (isl_tab_rollback(context_tab
, snap2
) < 0)
2265 if (best
== -1 || r
> best_r
) {
2269 if (isl_tab_rollback(context_tab
, snap
) < 0)
2276 static struct isl_basic_set
*context_lex_peek_basic_set(
2277 struct isl_context
*context
)
2279 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2282 return isl_tab_peek_bset(clex
->tab
);
2285 static struct isl_tab
*context_lex_peek_tab(struct isl_context
*context
)
2287 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2291 static void context_lex_add_eq(struct isl_context
*context
, isl_int
*eq
,
2292 int check
, int update
)
2294 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2295 if (isl_tab_extend_cons(clex
->tab
, 2) < 0)
2297 if (add_lexmin_eq(clex
->tab
, eq
) < 0)
2300 int v
= tab_has_valid_sample(clex
->tab
, eq
, 1);
2304 clex
->tab
= check_integer_feasible(clex
->tab
);
2307 clex
->tab
= check_samples(clex
->tab
, eq
, 1);
2310 isl_tab_free(clex
->tab
);
2314 static void context_lex_add_ineq(struct isl_context
*context
, isl_int
*ineq
,
2315 int check
, int update
)
2317 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2318 if (isl_tab_extend_cons(clex
->tab
, 1) < 0)
2320 clex
->tab
= add_lexmin_ineq(clex
->tab
, ineq
);
2322 int v
= tab_has_valid_sample(clex
->tab
, ineq
, 0);
2326 clex
->tab
= check_integer_feasible(clex
->tab
);
2329 clex
->tab
= check_samples(clex
->tab
, ineq
, 0);
2332 isl_tab_free(clex
->tab
);
2336 static int context_lex_add_ineq_wrap(void *user
, isl_int
*ineq
)
2338 struct isl_context
*context
= (struct isl_context
*)user
;
2339 context_lex_add_ineq(context
, ineq
, 0, 0);
2340 return context
->op
->is_ok(context
) ? 0 : -1;
2343 /* Check which signs can be obtained by "ineq" on all the currently
2344 * active sample values. See row_sign for more information.
2346 static enum isl_tab_row_sign
tab_ineq_sign(struct isl_tab
*tab
, isl_int
*ineq
,
2352 enum isl_tab_row_sign res
= isl_tab_row_unknown
;
2354 isl_assert(tab
->mat
->ctx
, tab
->samples
, return isl_tab_row_unknown
);
2355 isl_assert(tab
->mat
->ctx
, tab
->samples
->n_col
== 1 + tab
->n_var
,
2356 return isl_tab_row_unknown
);
2359 for (i
= tab
->n_outside
; i
< tab
->n_sample
; ++i
) {
2360 isl_seq_inner_product(tab
->samples
->row
[i
], ineq
,
2361 1 + tab
->n_var
, &tmp
);
2362 sgn
= isl_int_sgn(tmp
);
2363 if (sgn
> 0 || (sgn
== 0 && strict
)) {
2364 if (res
== isl_tab_row_unknown
)
2365 res
= isl_tab_row_pos
;
2366 if (res
== isl_tab_row_neg
)
2367 res
= isl_tab_row_any
;
2370 if (res
== isl_tab_row_unknown
)
2371 res
= isl_tab_row_neg
;
2372 if (res
== isl_tab_row_pos
)
2373 res
= isl_tab_row_any
;
2375 if (res
== isl_tab_row_any
)
2383 static enum isl_tab_row_sign
context_lex_ineq_sign(struct isl_context
*context
,
2384 isl_int
*ineq
, int strict
)
2386 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2387 return tab_ineq_sign(clex
->tab
, ineq
, strict
);
2390 /* Check whether "ineq" can be added to the tableau without rendering
2393 static int context_lex_test_ineq(struct isl_context
*context
, isl_int
*ineq
)
2395 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2396 struct isl_tab_undo
*snap
;
2402 if (isl_tab_extend_cons(clex
->tab
, 1) < 0)
2405 snap
= isl_tab_snap(clex
->tab
);
2406 if (isl_tab_push_basis(clex
->tab
) < 0)
2408 clex
->tab
= add_lexmin_ineq(clex
->tab
, ineq
);
2409 clex
->tab
= check_integer_feasible(clex
->tab
);
2412 feasible
= !clex
->tab
->empty
;
2413 if (isl_tab_rollback(clex
->tab
, snap
) < 0)
2419 static int context_lex_get_div(struct isl_context
*context
, struct isl_tab
*tab
,
2420 struct isl_vec
*div
)
2422 return get_div(tab
, context
, div
);
2425 /* Insert a div specified by "div" to the context tableau at position "pos" and
2426 * return isl_bool_true if the div is obviously non-negative.
2427 * context_tab_add_div will always return isl_bool_true, because all variables
2428 * in a isl_context_lex tableau are non-negative.
2429 * However, if we are using a big parameter in the context, then this only
2430 * reflects the non-negativity of the variable used to _encode_ the
2431 * div, i.e., div' = M + div, so we can't draw any conclusions.
2433 static isl_bool
context_lex_insert_div(struct isl_context
*context
, int pos
,
2434 __isl_keep isl_vec
*div
)
2436 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2438 nonneg
= context_tab_insert_div(clex
->tab
, pos
, div
,
2439 context_lex_add_ineq_wrap
, context
);
2441 return isl_bool_error
;
2443 return isl_bool_false
;
2447 static int context_lex_detect_equalities(struct isl_context
*context
,
2448 struct isl_tab
*tab
)
2453 static int context_lex_best_split(struct isl_context
*context
,
2454 struct isl_tab
*tab
)
2456 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2457 struct isl_tab_undo
*snap
;
2460 snap
= isl_tab_snap(clex
->tab
);
2461 if (isl_tab_push_basis(clex
->tab
) < 0)
2463 r
= best_split(tab
, clex
->tab
);
2465 if (r
>= 0 && isl_tab_rollback(clex
->tab
, snap
) < 0)
2471 static int context_lex_is_empty(struct isl_context
*context
)
2473 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2476 return clex
->tab
->empty
;
2479 static void *context_lex_save(struct isl_context
*context
)
2481 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2482 struct isl_tab_undo
*snap
;
2484 snap
= isl_tab_snap(clex
->tab
);
2485 if (isl_tab_push_basis(clex
->tab
) < 0)
2487 if (isl_tab_save_samples(clex
->tab
) < 0)
2493 static void context_lex_restore(struct isl_context
*context
, void *save
)
2495 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2496 if (isl_tab_rollback(clex
->tab
, (struct isl_tab_undo
*)save
) < 0) {
2497 isl_tab_free(clex
->tab
);
2502 static void context_lex_discard(void *save
)
2506 static int context_lex_is_ok(struct isl_context
*context
)
2508 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2512 /* For each variable in the context tableau, check if the variable can
2513 * only attain non-negative values. If so, mark the parameter as non-negative
2514 * in the main tableau. This allows for a more direct identification of some
2515 * cases of violated constraints.
2517 static struct isl_tab
*tab_detect_nonnegative_parameters(struct isl_tab
*tab
,
2518 struct isl_tab
*context_tab
)
2521 struct isl_tab_undo
*snap
;
2522 struct isl_vec
*ineq
= NULL
;
2523 struct isl_tab_var
*var
;
2526 if (context_tab
->n_var
== 0)
2529 ineq
= isl_vec_alloc(tab
->mat
->ctx
, 1 + context_tab
->n_var
);
2533 if (isl_tab_extend_cons(context_tab
, 1) < 0)
2536 snap
= isl_tab_snap(context_tab
);
2539 isl_seq_clr(ineq
->el
, ineq
->size
);
2540 for (i
= 0; i
< context_tab
->n_var
; ++i
) {
2541 isl_int_set_si(ineq
->el
[1 + i
], 1);
2542 if (isl_tab_add_ineq(context_tab
, ineq
->el
) < 0)
2544 var
= &context_tab
->con
[context_tab
->n_con
- 1];
2545 if (!context_tab
->empty
&&
2546 !isl_tab_min_at_most_neg_one(context_tab
, var
)) {
2548 if (i
>= tab
->n_param
)
2549 j
= i
- tab
->n_param
+ tab
->n_var
- tab
->n_div
;
2550 tab
->var
[j
].is_nonneg
= 1;
2553 isl_int_set_si(ineq
->el
[1 + i
], 0);
2554 if (isl_tab_rollback(context_tab
, snap
) < 0)
2558 if (context_tab
->M
&& n
== context_tab
->n_var
) {
2559 context_tab
->mat
= isl_mat_drop_cols(context_tab
->mat
, 2, 1);
2571 static struct isl_tab
*context_lex_detect_nonnegative_parameters(
2572 struct isl_context
*context
, struct isl_tab
*tab
)
2574 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2575 struct isl_tab_undo
*snap
;
2580 snap
= isl_tab_snap(clex
->tab
);
2581 if (isl_tab_push_basis(clex
->tab
) < 0)
2584 tab
= tab_detect_nonnegative_parameters(tab
, clex
->tab
);
2586 if (isl_tab_rollback(clex
->tab
, snap
) < 0)
2595 static void context_lex_invalidate(struct isl_context
*context
)
2597 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2598 isl_tab_free(clex
->tab
);
2602 static __isl_null
struct isl_context
*context_lex_free(
2603 struct isl_context
*context
)
2605 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2606 isl_tab_free(clex
->tab
);
2612 struct isl_context_op isl_context_lex_op
= {
2613 context_lex_detect_nonnegative_parameters
,
2614 context_lex_peek_basic_set
,
2615 context_lex_peek_tab
,
2617 context_lex_add_ineq
,
2618 context_lex_ineq_sign
,
2619 context_lex_test_ineq
,
2620 context_lex_get_div
,
2621 context_lex_insert_div
,
2622 context_lex_detect_equalities
,
2623 context_lex_best_split
,
2624 context_lex_is_empty
,
2627 context_lex_restore
,
2628 context_lex_discard
,
2629 context_lex_invalidate
,
2633 static struct isl_tab
*context_tab_for_lexmin(struct isl_basic_set
*bset
)
2635 struct isl_tab
*tab
;
2639 tab
= tab_for_lexmin(bset_to_bmap(bset
), NULL
, 1, 0);
2640 if (isl_tab_track_bset(tab
, bset
) < 0)
2642 tab
= isl_tab_init_samples(tab
);
2649 static struct isl_context
*isl_context_lex_alloc(struct isl_basic_set
*dom
)
2651 struct isl_context_lex
*clex
;
2656 clex
= isl_alloc_type(dom
->ctx
, struct isl_context_lex
);
2660 clex
->context
.op
= &isl_context_lex_op
;
2662 clex
->tab
= context_tab_for_lexmin(isl_basic_set_copy(dom
));
2663 if (restore_lexmin(clex
->tab
) < 0)
2665 clex
->tab
= check_integer_feasible(clex
->tab
);
2669 return &clex
->context
;
2671 clex
->context
.op
->free(&clex
->context
);
2675 /* Representation of the context when using generalized basis reduction.
2677 * "shifted" contains the offsets of the unit hypercubes that lie inside the
2678 * context. Any rational point in "shifted" can therefore be rounded
2679 * up to an integer point in the context.
2680 * If the context is constrained by any equality, then "shifted" is not used
2681 * as it would be empty.
2683 struct isl_context_gbr
{
2684 struct isl_context context
;
2685 struct isl_tab
*tab
;
2686 struct isl_tab
*shifted
;
2687 struct isl_tab
*cone
;
2690 static struct isl_tab
*context_gbr_detect_nonnegative_parameters(
2691 struct isl_context
*context
, struct isl_tab
*tab
)
2693 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2696 return tab_detect_nonnegative_parameters(tab
, cgbr
->tab
);
2699 static struct isl_basic_set
*context_gbr_peek_basic_set(
2700 struct isl_context
*context
)
2702 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2705 return isl_tab_peek_bset(cgbr
->tab
);
2708 static struct isl_tab
*context_gbr_peek_tab(struct isl_context
*context
)
2710 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2714 /* Initialize the "shifted" tableau of the context, which
2715 * contains the constraints of the original tableau shifted
2716 * by the sum of all negative coefficients. This ensures
2717 * that any rational point in the shifted tableau can
2718 * be rounded up to yield an integer point in the original tableau.
2720 static void gbr_init_shifted(struct isl_context_gbr
*cgbr
)
2723 struct isl_vec
*cst
;
2724 struct isl_basic_set
*bset
= isl_tab_peek_bset(cgbr
->tab
);
2725 unsigned dim
= isl_basic_set_total_dim(bset
);
2727 cst
= isl_vec_alloc(cgbr
->tab
->mat
->ctx
, bset
->n_ineq
);
2731 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
2732 isl_int_set(cst
->el
[i
], bset
->ineq
[i
][0]);
2733 for (j
= 0; j
< dim
; ++j
) {
2734 if (!isl_int_is_neg(bset
->ineq
[i
][1 + j
]))
2736 isl_int_add(bset
->ineq
[i
][0], bset
->ineq
[i
][0],
2737 bset
->ineq
[i
][1 + j
]);
2741 cgbr
->shifted
= isl_tab_from_basic_set(bset
, 0);
2743 for (i
= 0; i
< bset
->n_ineq
; ++i
)
2744 isl_int_set(bset
->ineq
[i
][0], cst
->el
[i
]);
2749 /* Check if the shifted tableau is non-empty, and if so
2750 * use the sample point to construct an integer point
2751 * of the context tableau.
2753 static struct isl_vec
*gbr_get_shifted_sample(struct isl_context_gbr
*cgbr
)
2755 struct isl_vec
*sample
;
2758 gbr_init_shifted(cgbr
);
2761 if (cgbr
->shifted
->empty
)
2762 return isl_vec_alloc(cgbr
->tab
->mat
->ctx
, 0);
2764 sample
= isl_tab_get_sample_value(cgbr
->shifted
);
2765 sample
= isl_vec_ceil(sample
);
2770 static struct isl_basic_set
*drop_constant_terms(struct isl_basic_set
*bset
)
2777 for (i
= 0; i
< bset
->n_eq
; ++i
)
2778 isl_int_set_si(bset
->eq
[i
][0], 0);
2780 for (i
= 0; i
< bset
->n_ineq
; ++i
)
2781 isl_int_set_si(bset
->ineq
[i
][0], 0);
2786 static int use_shifted(struct isl_context_gbr
*cgbr
)
2790 return cgbr
->tab
->bmap
->n_eq
== 0 && cgbr
->tab
->bmap
->n_div
== 0;
2793 static struct isl_vec
*gbr_get_sample(struct isl_context_gbr
*cgbr
)
2795 struct isl_basic_set
*bset
;
2796 struct isl_basic_set
*cone
;
2798 if (isl_tab_sample_is_integer(cgbr
->tab
))
2799 return isl_tab_get_sample_value(cgbr
->tab
);
2801 if (use_shifted(cgbr
)) {
2802 struct isl_vec
*sample
;
2804 sample
= gbr_get_shifted_sample(cgbr
);
2805 if (!sample
|| sample
->size
> 0)
2808 isl_vec_free(sample
);
2812 bset
= isl_tab_peek_bset(cgbr
->tab
);
2813 cgbr
->cone
= isl_tab_from_recession_cone(bset
, 0);
2816 if (isl_tab_track_bset(cgbr
->cone
,
2817 isl_basic_set_copy(bset
)) < 0)
2820 if (isl_tab_detect_implicit_equalities(cgbr
->cone
) < 0)
2823 if (cgbr
->cone
->n_dead
== cgbr
->cone
->n_col
) {
2824 struct isl_vec
*sample
;
2825 struct isl_tab_undo
*snap
;
2827 if (cgbr
->tab
->basis
) {
2828 if (cgbr
->tab
->basis
->n_col
!= 1 + cgbr
->tab
->n_var
) {
2829 isl_mat_free(cgbr
->tab
->basis
);
2830 cgbr
->tab
->basis
= NULL
;
2832 cgbr
->tab
->n_zero
= 0;
2833 cgbr
->tab
->n_unbounded
= 0;
2836 snap
= isl_tab_snap(cgbr
->tab
);
2838 sample
= isl_tab_sample(cgbr
->tab
);
2840 if (!sample
|| isl_tab_rollback(cgbr
->tab
, snap
) < 0) {
2841 isl_vec_free(sample
);
2848 cone
= isl_basic_set_dup(isl_tab_peek_bset(cgbr
->cone
));
2849 cone
= drop_constant_terms(cone
);
2850 cone
= isl_basic_set_update_from_tab(cone
, cgbr
->cone
);
2851 cone
= isl_basic_set_underlying_set(cone
);
2852 cone
= isl_basic_set_gauss(cone
, NULL
);
2854 bset
= isl_basic_set_dup(isl_tab_peek_bset(cgbr
->tab
));
2855 bset
= isl_basic_set_update_from_tab(bset
, cgbr
->tab
);
2856 bset
= isl_basic_set_underlying_set(bset
);
2857 bset
= isl_basic_set_gauss(bset
, NULL
);
2859 return isl_basic_set_sample_with_cone(bset
, cone
);
2862 static void check_gbr_integer_feasible(struct isl_context_gbr
*cgbr
)
2864 struct isl_vec
*sample
;
2869 if (cgbr
->tab
->empty
)
2872 sample
= gbr_get_sample(cgbr
);
2876 if (sample
->size
== 0) {
2877 isl_vec_free(sample
);
2878 if (isl_tab_mark_empty(cgbr
->tab
) < 0)
2883 if (isl_tab_add_sample(cgbr
->tab
, sample
) < 0)
2888 isl_tab_free(cgbr
->tab
);
2892 static struct isl_tab
*add_gbr_eq(struct isl_tab
*tab
, isl_int
*eq
)
2897 if (isl_tab_extend_cons(tab
, 2) < 0)
2900 if (isl_tab_add_eq(tab
, eq
) < 0)
2909 /* Add the equality described by "eq" to the context.
2910 * If "check" is set, then we check if the context is empty after
2911 * adding the equality.
2912 * If "update" is set, then we check if the samples are still valid.
2914 * We do not explicitly add shifted copies of the equality to
2915 * cgbr->shifted since they would conflict with each other.
2916 * Instead, we directly mark cgbr->shifted empty.
2918 static void context_gbr_add_eq(struct isl_context
*context
, isl_int
*eq
,
2919 int check
, int update
)
2921 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2923 cgbr
->tab
= add_gbr_eq(cgbr
->tab
, eq
);
2925 if (cgbr
->shifted
&& !cgbr
->shifted
->empty
&& use_shifted(cgbr
)) {
2926 if (isl_tab_mark_empty(cgbr
->shifted
) < 0)
2930 if (cgbr
->cone
&& cgbr
->cone
->n_col
!= cgbr
->cone
->n_dead
) {
2931 if (isl_tab_extend_cons(cgbr
->cone
, 2) < 0)
2933 if (isl_tab_add_eq(cgbr
->cone
, eq
) < 0)
2938 int v
= tab_has_valid_sample(cgbr
->tab
, eq
, 1);
2942 check_gbr_integer_feasible(cgbr
);
2945 cgbr
->tab
= check_samples(cgbr
->tab
, eq
, 1);
2948 isl_tab_free(cgbr
->tab
);
2952 static void add_gbr_ineq(struct isl_context_gbr
*cgbr
, isl_int
*ineq
)
2957 if (isl_tab_extend_cons(cgbr
->tab
, 1) < 0)
2960 if (isl_tab_add_ineq(cgbr
->tab
, ineq
) < 0)
2963 if (cgbr
->shifted
&& !cgbr
->shifted
->empty
&& use_shifted(cgbr
)) {
2966 dim
= isl_basic_map_total_dim(cgbr
->tab
->bmap
);
2968 if (isl_tab_extend_cons(cgbr
->shifted
, 1) < 0)
2971 for (i
= 0; i
< dim
; ++i
) {
2972 if (!isl_int_is_neg(ineq
[1 + i
]))
2974 isl_int_add(ineq
[0], ineq
[0], ineq
[1 + i
]);
2977 if (isl_tab_add_ineq(cgbr
->shifted
, ineq
) < 0)
2980 for (i
= 0; i
< dim
; ++i
) {
2981 if (!isl_int_is_neg(ineq
[1 + i
]))
2983 isl_int_sub(ineq
[0], ineq
[0], ineq
[1 + i
]);
2987 if (cgbr
->cone
&& cgbr
->cone
->n_col
!= cgbr
->cone
->n_dead
) {
2988 if (isl_tab_extend_cons(cgbr
->cone
, 1) < 0)
2990 if (isl_tab_add_ineq(cgbr
->cone
, ineq
) < 0)
2996 isl_tab_free(cgbr
->tab
);
3000 static void context_gbr_add_ineq(struct isl_context
*context
, isl_int
*ineq
,
3001 int check
, int update
)
3003 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3005 add_gbr_ineq(cgbr
, ineq
);
3010 int v
= tab_has_valid_sample(cgbr
->tab
, ineq
, 0);
3014 check_gbr_integer_feasible(cgbr
);
3017 cgbr
->tab
= check_samples(cgbr
->tab
, ineq
, 0);
3020 isl_tab_free(cgbr
->tab
);
3024 static int context_gbr_add_ineq_wrap(void *user
, isl_int
*ineq
)
3026 struct isl_context
*context
= (struct isl_context
*)user
;
3027 context_gbr_add_ineq(context
, ineq
, 0, 0);
3028 return context
->op
->is_ok(context
) ? 0 : -1;
3031 static enum isl_tab_row_sign
context_gbr_ineq_sign(struct isl_context
*context
,
3032 isl_int
*ineq
, int strict
)
3034 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3035 return tab_ineq_sign(cgbr
->tab
, ineq
, strict
);
3038 /* Check whether "ineq" can be added to the tableau without rendering
3041 static int context_gbr_test_ineq(struct isl_context
*context
, isl_int
*ineq
)
3043 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3044 struct isl_tab_undo
*snap
;
3045 struct isl_tab_undo
*shifted_snap
= NULL
;
3046 struct isl_tab_undo
*cone_snap
= NULL
;
3052 if (isl_tab_extend_cons(cgbr
->tab
, 1) < 0)
3055 snap
= isl_tab_snap(cgbr
->tab
);
3057 shifted_snap
= isl_tab_snap(cgbr
->shifted
);
3059 cone_snap
= isl_tab_snap(cgbr
->cone
);
3060 add_gbr_ineq(cgbr
, ineq
);
3061 check_gbr_integer_feasible(cgbr
);
3064 feasible
= !cgbr
->tab
->empty
;
3065 if (isl_tab_rollback(cgbr
->tab
, snap
) < 0)
3068 if (isl_tab_rollback(cgbr
->shifted
, shifted_snap
))
3070 } else if (cgbr
->shifted
) {
3071 isl_tab_free(cgbr
->shifted
);
3072 cgbr
->shifted
= NULL
;
3075 if (isl_tab_rollback(cgbr
->cone
, cone_snap
))
3077 } else if (cgbr
->cone
) {
3078 isl_tab_free(cgbr
->cone
);
3085 /* Return the column of the last of the variables associated to
3086 * a column that has a non-zero coefficient.
3087 * This function is called in a context where only coefficients
3088 * of parameters or divs can be non-zero.
3090 static int last_non_zero_var_col(struct isl_tab
*tab
, isl_int
*p
)
3095 if (tab
->n_var
== 0)
3098 for (i
= tab
->n_var
- 1; i
>= 0; --i
) {
3099 if (i
>= tab
->n_param
&& i
< tab
->n_var
- tab
->n_div
)
3101 if (tab
->var
[i
].is_row
)
3103 col
= tab
->var
[i
].index
;
3104 if (!isl_int_is_zero(p
[col
]))
3111 /* Look through all the recently added equalities in the context
3112 * to see if we can propagate any of them to the main tableau.
3114 * The newly added equalities in the context are encoded as pairs
3115 * of inequalities starting at inequality "first".
3117 * We tentatively add each of these equalities to the main tableau
3118 * and if this happens to result in a row with a final coefficient
3119 * that is one or negative one, we use it to kill a column
3120 * in the main tableau. Otherwise, we discard the tentatively
3122 * This tentative addition of equality constraints turns
3123 * on the undo facility of the tableau. Turn it off again
3124 * at the end, assuming it was turned off to begin with.
3126 * Return 0 on success and -1 on failure.
3128 static int propagate_equalities(struct isl_context_gbr
*cgbr
,
3129 struct isl_tab
*tab
, unsigned first
)
3132 struct isl_vec
*eq
= NULL
;
3133 isl_bool needs_undo
;
3135 needs_undo
= isl_tab_need_undo(tab
);
3138 eq
= isl_vec_alloc(tab
->mat
->ctx
, 1 + tab
->n_var
);
3142 if (isl_tab_extend_cons(tab
, (cgbr
->tab
->bmap
->n_ineq
- first
)/2) < 0)
3145 isl_seq_clr(eq
->el
+ 1 + tab
->n_param
,
3146 tab
->n_var
- tab
->n_param
- tab
->n_div
);
3147 for (i
= first
; i
< cgbr
->tab
->bmap
->n_ineq
; i
+= 2) {
3150 struct isl_tab_undo
*snap
;
3151 snap
= isl_tab_snap(tab
);
3153 isl_seq_cpy(eq
->el
, cgbr
->tab
->bmap
->ineq
[i
], 1 + tab
->n_param
);
3154 isl_seq_cpy(eq
->el
+ 1 + tab
->n_var
- tab
->n_div
,
3155 cgbr
->tab
->bmap
->ineq
[i
] + 1 + tab
->n_param
,
3158 r
= isl_tab_add_row(tab
, eq
->el
);
3161 r
= tab
->con
[r
].index
;
3162 j
= last_non_zero_var_col(tab
, tab
->mat
->row
[r
] + 2 + tab
->M
);
3163 if (j
< 0 || j
< tab
->n_dead
||
3164 !isl_int_is_one(tab
->mat
->row
[r
][0]) ||
3165 (!isl_int_is_one(tab
->mat
->row
[r
][2 + tab
->M
+ j
]) &&
3166 !isl_int_is_negone(tab
->mat
->row
[r
][2 + tab
->M
+ j
]))) {
3167 if (isl_tab_rollback(tab
, snap
) < 0)
3171 if (isl_tab_pivot(tab
, r
, j
) < 0)
3173 if (isl_tab_kill_col(tab
, j
) < 0)
3176 if (restore_lexmin(tab
) < 0)
3181 isl_tab_clear_undo(tab
);
3187 isl_tab_free(cgbr
->tab
);
3192 static int context_gbr_detect_equalities(struct isl_context
*context
,
3193 struct isl_tab
*tab
)
3195 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3199 struct isl_basic_set
*bset
= isl_tab_peek_bset(cgbr
->tab
);
3200 cgbr
->cone
= isl_tab_from_recession_cone(bset
, 0);
3203 if (isl_tab_track_bset(cgbr
->cone
,
3204 isl_basic_set_copy(bset
)) < 0)
3207 if (isl_tab_detect_implicit_equalities(cgbr
->cone
) < 0)
3210 n_ineq
= cgbr
->tab
->bmap
->n_ineq
;
3211 cgbr
->tab
= isl_tab_detect_equalities(cgbr
->tab
, cgbr
->cone
);
3214 if (cgbr
->tab
->bmap
->n_ineq
> n_ineq
&&
3215 propagate_equalities(cgbr
, tab
, n_ineq
) < 0)
3220 isl_tab_free(cgbr
->tab
);
3225 static int context_gbr_get_div(struct isl_context
*context
, struct isl_tab
*tab
,
3226 struct isl_vec
*div
)
3228 return get_div(tab
, context
, div
);
3231 static isl_bool
context_gbr_insert_div(struct isl_context
*context
, int pos
,
3232 __isl_keep isl_vec
*div
)
3234 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3236 int r
, n_div
, o_div
;
3238 n_div
= isl_basic_map_dim(cgbr
->cone
->bmap
, isl_dim_div
);
3239 o_div
= cgbr
->cone
->n_var
- n_div
;
3241 if (isl_tab_extend_cons(cgbr
->cone
, 3) < 0)
3242 return isl_bool_error
;
3243 if (isl_tab_extend_vars(cgbr
->cone
, 1) < 0)
3244 return isl_bool_error
;
3245 if ((r
= isl_tab_insert_var(cgbr
->cone
, pos
)) <0)
3246 return isl_bool_error
;
3248 cgbr
->cone
->bmap
= isl_basic_map_insert_div(cgbr
->cone
->bmap
,
3250 if (!cgbr
->cone
->bmap
)
3251 return isl_bool_error
;
3252 if (isl_tab_push_var(cgbr
->cone
, isl_tab_undo_bmap_div
,
3253 &cgbr
->cone
->var
[r
]) < 0)
3254 return isl_bool_error
;
3256 return context_tab_insert_div(cgbr
->tab
, pos
, div
,
3257 context_gbr_add_ineq_wrap
, context
);
3260 static int context_gbr_best_split(struct isl_context
*context
,
3261 struct isl_tab
*tab
)
3263 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3264 struct isl_tab_undo
*snap
;
3267 snap
= isl_tab_snap(cgbr
->tab
);
3268 r
= best_split(tab
, cgbr
->tab
);
3270 if (r
>= 0 && isl_tab_rollback(cgbr
->tab
, snap
) < 0)
3276 static int context_gbr_is_empty(struct isl_context
*context
)
3278 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3281 return cgbr
->tab
->empty
;
3284 struct isl_gbr_tab_undo
{
3285 struct isl_tab_undo
*tab_snap
;
3286 struct isl_tab_undo
*shifted_snap
;
3287 struct isl_tab_undo
*cone_snap
;
3290 static void *context_gbr_save(struct isl_context
*context
)
3292 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3293 struct isl_gbr_tab_undo
*snap
;
3298 snap
= isl_alloc_type(cgbr
->tab
->mat
->ctx
, struct isl_gbr_tab_undo
);
3302 snap
->tab_snap
= isl_tab_snap(cgbr
->tab
);
3303 if (isl_tab_save_samples(cgbr
->tab
) < 0)
3307 snap
->shifted_snap
= isl_tab_snap(cgbr
->shifted
);
3309 snap
->shifted_snap
= NULL
;
3312 snap
->cone_snap
= isl_tab_snap(cgbr
->cone
);
3314 snap
->cone_snap
= NULL
;
3322 static void context_gbr_restore(struct isl_context
*context
, void *save
)
3324 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3325 struct isl_gbr_tab_undo
*snap
= (struct isl_gbr_tab_undo
*)save
;
3328 if (isl_tab_rollback(cgbr
->tab
, snap
->tab_snap
) < 0)
3331 if (snap
->shifted_snap
) {
3332 if (isl_tab_rollback(cgbr
->shifted
, snap
->shifted_snap
) < 0)
3334 } else if (cgbr
->shifted
) {
3335 isl_tab_free(cgbr
->shifted
);
3336 cgbr
->shifted
= NULL
;
3339 if (snap
->cone_snap
) {
3340 if (isl_tab_rollback(cgbr
->cone
, snap
->cone_snap
) < 0)
3342 } else if (cgbr
->cone
) {
3343 isl_tab_free(cgbr
->cone
);
3352 isl_tab_free(cgbr
->tab
);
3356 static void context_gbr_discard(void *save
)
3358 struct isl_gbr_tab_undo
*snap
= (struct isl_gbr_tab_undo
*)save
;
3362 static int context_gbr_is_ok(struct isl_context
*context
)
3364 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3368 static void context_gbr_invalidate(struct isl_context
*context
)
3370 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3371 isl_tab_free(cgbr
->tab
);
3375 static __isl_null
struct isl_context
*context_gbr_free(
3376 struct isl_context
*context
)
3378 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3379 isl_tab_free(cgbr
->tab
);
3380 isl_tab_free(cgbr
->shifted
);
3381 isl_tab_free(cgbr
->cone
);
3387 struct isl_context_op isl_context_gbr_op
= {
3388 context_gbr_detect_nonnegative_parameters
,
3389 context_gbr_peek_basic_set
,
3390 context_gbr_peek_tab
,
3392 context_gbr_add_ineq
,
3393 context_gbr_ineq_sign
,
3394 context_gbr_test_ineq
,
3395 context_gbr_get_div
,
3396 context_gbr_insert_div
,
3397 context_gbr_detect_equalities
,
3398 context_gbr_best_split
,
3399 context_gbr_is_empty
,
3402 context_gbr_restore
,
3403 context_gbr_discard
,
3404 context_gbr_invalidate
,
3408 static struct isl_context
*isl_context_gbr_alloc(__isl_keep isl_basic_set
*dom
)
3410 struct isl_context_gbr
*cgbr
;
3415 cgbr
= isl_calloc_type(dom
->ctx
, struct isl_context_gbr
);
3419 cgbr
->context
.op
= &isl_context_gbr_op
;
3421 cgbr
->shifted
= NULL
;
3423 cgbr
->tab
= isl_tab_from_basic_set(dom
, 1);
3424 cgbr
->tab
= isl_tab_init_samples(cgbr
->tab
);
3427 check_gbr_integer_feasible(cgbr
);
3429 return &cgbr
->context
;
3431 cgbr
->context
.op
->free(&cgbr
->context
);
3435 /* Allocate a context corresponding to "dom".
3436 * The representation specific fields are initialized by
3437 * isl_context_lex_alloc or isl_context_gbr_alloc.
3438 * The shared "n_unknown" field is initialized to the number
3439 * of final unknown integer divisions in "dom".
3441 static struct isl_context
*isl_context_alloc(__isl_keep isl_basic_set
*dom
)
3443 struct isl_context
*context
;
3449 if (dom
->ctx
->opt
->context
== ISL_CONTEXT_LEXMIN
)
3450 context
= isl_context_lex_alloc(dom
);
3452 context
= isl_context_gbr_alloc(dom
);
3457 first
= isl_basic_set_first_unknown_div(dom
);
3459 return context
->op
->free(context
);
3460 context
->n_unknown
= isl_basic_set_dim(dom
, isl_dim_div
) - first
;
3465 /* Construct an isl_sol_map structure for accumulating the solution.
3466 * If track_empty is set, then we also keep track of the parts
3467 * of the context where there is no solution.
3468 * If max is set, then we are solving a maximization, rather than
3469 * a minimization problem, which means that the variables in the
3470 * tableau have value "M - x" rather than "M + x".
3472 static struct isl_sol
*sol_map_init(struct isl_basic_map
*bmap
,
3473 struct isl_basic_set
*dom
, int track_empty
, int max
)
3475 struct isl_sol_map
*sol_map
= NULL
;
3480 sol_map
= isl_calloc_type(bmap
->ctx
, struct isl_sol_map
);
3484 sol_map
->sol
.rational
= ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
3485 sol_map
->sol
.dec_level
.callback
.run
= &sol_dec_level_wrap
;
3486 sol_map
->sol
.dec_level
.sol
= &sol_map
->sol
;
3487 sol_map
->sol
.max
= max
;
3488 sol_map
->sol
.n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
3489 sol_map
->sol
.add
= &sol_map_add_wrap
;
3490 sol_map
->sol
.add_empty
= track_empty
? &sol_map_add_empty_wrap
: NULL
;
3491 sol_map
->sol
.free
= &sol_map_free_wrap
;
3492 sol_map
->map
= isl_map_alloc_space(isl_basic_map_get_space(bmap
), 1,
3497 sol_map
->sol
.context
= isl_context_alloc(dom
);
3498 if (!sol_map
->sol
.context
)
3502 sol_map
->empty
= isl_set_alloc_space(isl_basic_set_get_space(dom
),
3503 1, ISL_SET_DISJOINT
);
3504 if (!sol_map
->empty
)
3508 isl_basic_set_free(dom
);
3509 return &sol_map
->sol
;
3511 isl_basic_set_free(dom
);
3512 sol_map_free(sol_map
);
3516 /* Check whether all coefficients of (non-parameter) variables
3517 * are non-positive, meaning that no pivots can be performed on the row.
3519 static int is_critical(struct isl_tab
*tab
, int row
)
3522 unsigned off
= 2 + tab
->M
;
3524 for (j
= tab
->n_dead
; j
< tab
->n_col
; ++j
) {
3525 if (tab
->col_var
[j
] >= 0 &&
3526 (tab
->col_var
[j
] < tab
->n_param
||
3527 tab
->col_var
[j
] >= tab
->n_var
- tab
->n_div
))
3530 if (isl_int_is_pos(tab
->mat
->row
[row
][off
+ j
]))
3537 /* Check whether the inequality represented by vec is strict over the integers,
3538 * i.e., there are no integer values satisfying the constraint with
3539 * equality. This happens if the gcd of the coefficients is not a divisor
3540 * of the constant term. If so, scale the constraint down by the gcd
3541 * of the coefficients.
3543 static int is_strict(struct isl_vec
*vec
)
3549 isl_seq_gcd(vec
->el
+ 1, vec
->size
- 1, &gcd
);
3550 if (!isl_int_is_one(gcd
)) {
3551 strict
= !isl_int_is_divisible_by(vec
->el
[0], gcd
);
3552 isl_int_fdiv_q(vec
->el
[0], vec
->el
[0], gcd
);
3553 isl_seq_scale_down(vec
->el
+ 1, vec
->el
+ 1, gcd
, vec
->size
-1);
3560 /* Determine the sign of the given row of the main tableau.
3561 * The result is one of
3562 * isl_tab_row_pos: always non-negative; no pivot needed
3563 * isl_tab_row_neg: always non-positive; pivot
3564 * isl_tab_row_any: can be both positive and negative; split
3566 * We first handle some simple cases
3567 * - the row sign may be known already
3568 * - the row may be obviously non-negative
3569 * - the parametric constant may be equal to that of another row
3570 * for which we know the sign. This sign will be either "pos" or
3571 * "any". If it had been "neg" then we would have pivoted before.
3573 * If none of these cases hold, we check the value of the row for each
3574 * of the currently active samples. Based on the signs of these values
3575 * we make an initial determination of the sign of the row.
3577 * all zero -> unk(nown)
3578 * all non-negative -> pos
3579 * all non-positive -> neg
3580 * both negative and positive -> all
3582 * If we end up with "all", we are done.
3583 * Otherwise, we perform a check for positive and/or negative
3584 * values as follows.
3586 * samples neg unk pos
3592 * There is no special sign for "zero", because we can usually treat zero
3593 * as either non-negative or non-positive, whatever works out best.
3594 * However, if the row is "critical", meaning that pivoting is impossible
3595 * then we don't want to limp zero with the non-positive case, because
3596 * then we we would lose the solution for those values of the parameters
3597 * where the value of the row is zero. Instead, we treat 0 as non-negative
3598 * ensuring a split if the row can attain both zero and negative values.
3599 * The same happens when the original constraint was one that could not
3600 * be satisfied with equality by any integer values of the parameters.
3601 * In this case, we normalize the constraint, but then a value of zero
3602 * for the normalized constraint is actually a positive value for the
3603 * original constraint, so again we need to treat zero as non-negative.
3604 * In both these cases, we have the following decision tree instead:
3606 * all non-negative -> pos
3607 * all negative -> neg
3608 * both negative and non-negative -> all
3616 static enum isl_tab_row_sign
row_sign(struct isl_tab
*tab
,
3617 struct isl_sol
*sol
, int row
)
3619 struct isl_vec
*ineq
= NULL
;
3620 enum isl_tab_row_sign res
= isl_tab_row_unknown
;
3625 if (tab
->row_sign
[row
] != isl_tab_row_unknown
)
3626 return tab
->row_sign
[row
];
3627 if (is_obviously_nonneg(tab
, row
))
3628 return isl_tab_row_pos
;
3629 for (row2
= tab
->n_redundant
; row2
< tab
->n_row
; ++row2
) {
3630 if (tab
->row_sign
[row2
] == isl_tab_row_unknown
)
3632 if (identical_parameter_line(tab
, row
, row2
))
3633 return tab
->row_sign
[row2
];
3636 critical
= is_critical(tab
, row
);
3638 ineq
= get_row_parameter_ineq(tab
, row
);
3642 strict
= is_strict(ineq
);
3644 res
= sol
->context
->op
->ineq_sign(sol
->context
, ineq
->el
,
3645 critical
|| strict
);
3647 if (res
== isl_tab_row_unknown
|| res
== isl_tab_row_pos
) {
3648 /* test for negative values */
3650 isl_seq_neg(ineq
->el
, ineq
->el
, ineq
->size
);
3651 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3653 feasible
= sol
->context
->op
->test_ineq(sol
->context
, ineq
->el
);
3657 res
= isl_tab_row_pos
;
3659 res
= (res
== isl_tab_row_unknown
) ? isl_tab_row_neg
3661 if (res
== isl_tab_row_neg
) {
3662 isl_seq_neg(ineq
->el
, ineq
->el
, ineq
->size
);
3663 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3667 if (res
== isl_tab_row_neg
) {
3668 /* test for positive values */
3670 if (!critical
&& !strict
)
3671 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3673 feasible
= sol
->context
->op
->test_ineq(sol
->context
, ineq
->el
);
3677 res
= isl_tab_row_any
;
3684 return isl_tab_row_unknown
;
3687 static void find_solutions(struct isl_sol
*sol
, struct isl_tab
*tab
);
3689 /* Find solutions for values of the parameters that satisfy the given
3692 * We currently take a snapshot of the context tableau that is reset
3693 * when we return from this function, while we make a copy of the main
3694 * tableau, leaving the original main tableau untouched.
3695 * These are fairly arbitrary choices. Making a copy also of the context
3696 * tableau would obviate the need to undo any changes made to it later,
3697 * while taking a snapshot of the main tableau could reduce memory usage.
3698 * If we were to switch to taking a snapshot of the main tableau,
3699 * we would have to keep in mind that we need to save the row signs
3700 * and that we need to do this before saving the current basis
3701 * such that the basis has been restore before we restore the row signs.
3703 static void find_in_pos(struct isl_sol
*sol
, struct isl_tab
*tab
, isl_int
*ineq
)
3709 saved
= sol
->context
->op
->save(sol
->context
);
3711 tab
= isl_tab_dup(tab
);
3715 sol
->context
->op
->add_ineq(sol
->context
, ineq
, 0, 1);
3717 find_solutions(sol
, tab
);
3720 sol
->context
->op
->restore(sol
->context
, saved
);
3722 sol
->context
->op
->discard(saved
);
3728 /* Record the absence of solutions for those values of the parameters
3729 * that do not satisfy the given inequality with equality.
3731 static void no_sol_in_strict(struct isl_sol
*sol
,
3732 struct isl_tab
*tab
, struct isl_vec
*ineq
)
3737 if (!sol
->context
|| sol
->error
)
3739 saved
= sol
->context
->op
->save(sol
->context
);
3741 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3743 sol
->context
->op
->add_ineq(sol
->context
, ineq
->el
, 1, 0);
3752 isl_int_add_ui(ineq
->el
[0], ineq
->el
[0], 1);
3754 sol
->context
->op
->restore(sol
->context
, saved
);
3760 /* Reset all row variables that are marked to have a sign that may
3761 * be both positive and negative to have an unknown sign.
3763 static void reset_any_to_unknown(struct isl_tab
*tab
)
3767 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
3768 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
3770 if (tab
->row_sign
[row
] == isl_tab_row_any
)
3771 tab
->row_sign
[row
] = isl_tab_row_unknown
;
3775 /* Compute the lexicographic minimum of the set represented by the main
3776 * tableau "tab" within the context "sol->context_tab".
3777 * On entry the sample value of the main tableau is lexicographically
3778 * less than or equal to this lexicographic minimum.
3779 * Pivots are performed until a feasible point is found, which is then
3780 * necessarily equal to the minimum, or until the tableau is found to
3781 * be infeasible. Some pivots may need to be performed for only some
3782 * feasible values of the context tableau. If so, the context tableau
3783 * is split into a part where the pivot is needed and a part where it is not.
3785 * Whenever we enter the main loop, the main tableau is such that no
3786 * "obvious" pivots need to be performed on it, where "obvious" means
3787 * that the given row can be seen to be negative without looking at
3788 * the context tableau. In particular, for non-parametric problems,
3789 * no pivots need to be performed on the main tableau.
3790 * The caller of find_solutions is responsible for making this property
3791 * hold prior to the first iteration of the loop, while restore_lexmin
3792 * is called before every other iteration.
3794 * Inside the main loop, we first examine the signs of the rows of
3795 * the main tableau within the context of the context tableau.
3796 * If we find a row that is always non-positive for all values of
3797 * the parameters satisfying the context tableau and negative for at
3798 * least one value of the parameters, we perform the appropriate pivot
3799 * and start over. An exception is the case where no pivot can be
3800 * performed on the row. In this case, we require that the sign of
3801 * the row is negative for all values of the parameters (rather than just
3802 * non-positive). This special case is handled inside row_sign, which
3803 * will say that the row can have any sign if it determines that it can
3804 * attain both negative and zero values.
3806 * If we can't find a row that always requires a pivot, but we can find
3807 * one or more rows that require a pivot for some values of the parameters
3808 * (i.e., the row can attain both positive and negative signs), then we split
3809 * the context tableau into two parts, one where we force the sign to be
3810 * non-negative and one where we force is to be negative.
3811 * The non-negative part is handled by a recursive call (through find_in_pos).
3812 * Upon returning from this call, we continue with the negative part and
3813 * perform the required pivot.
3815 * If no such rows can be found, all rows are non-negative and we have
3816 * found a (rational) feasible point. If we only wanted a rational point
3818 * Otherwise, we check if all values of the sample point of the tableau
3819 * are integral for the variables. If so, we have found the minimal
3820 * integral point and we are done.
3821 * If the sample point is not integral, then we need to make a distinction
3822 * based on whether the constant term is non-integral or the coefficients
3823 * of the parameters. Furthermore, in order to decide how to handle
3824 * the non-integrality, we also need to know whether the coefficients
3825 * of the other columns in the tableau are integral. This leads
3826 * to the following table. The first two rows do not correspond
3827 * to a non-integral sample point and are only mentioned for completeness.
3829 * constant parameters other
3832 * int int rat | -> no problem
3834 * rat int int -> fail
3836 * rat int rat -> cut
3839 * rat rat rat | -> parametric cut
3842 * rat rat int | -> split context
3844 * If the parametric constant is completely integral, then there is nothing
3845 * to be done. If the constant term is non-integral, but all the other
3846 * coefficient are integral, then there is nothing that can be done
3847 * and the tableau has no integral solution.
3848 * If, on the other hand, one or more of the other columns have rational
3849 * coefficients, but the parameter coefficients are all integral, then
3850 * we can perform a regular (non-parametric) cut.
3851 * Finally, if there is any parameter coefficient that is non-integral,
3852 * then we need to involve the context tableau. There are two cases here.
3853 * If at least one other column has a rational coefficient, then we
3854 * can perform a parametric cut in the main tableau by adding a new
3855 * integer division in the context tableau.
3856 * If all other columns have integral coefficients, then we need to
3857 * enforce that the rational combination of parameters (c + \sum a_i y_i)/m
3858 * is always integral. We do this by introducing an integer division
3859 * q = floor((c + \sum a_i y_i)/m) and stipulating that its argument should
3860 * always be integral in the context tableau, i.e., m q = c + \sum a_i y_i.
3861 * Since q is expressed in the tableau as
3862 * c + \sum a_i y_i - m q >= 0
3863 * -c - \sum a_i y_i + m q + m - 1 >= 0
3864 * it is sufficient to add the inequality
3865 * -c - \sum a_i y_i + m q >= 0
3866 * In the part of the context where this inequality does not hold, the
3867 * main tableau is marked as being empty.
3869 static void find_solutions(struct isl_sol
*sol
, struct isl_tab
*tab
)
3871 struct isl_context
*context
;
3874 if (!tab
|| sol
->error
)
3877 context
= sol
->context
;
3881 if (context
->op
->is_empty(context
))
3884 for (r
= 0; r
>= 0 && tab
&& !tab
->empty
; r
= restore_lexmin(tab
)) {
3887 enum isl_tab_row_sign sgn
;
3891 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
3892 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
3894 sgn
= row_sign(tab
, sol
, row
);
3897 tab
->row_sign
[row
] = sgn
;
3898 if (sgn
== isl_tab_row_any
)
3900 if (sgn
== isl_tab_row_any
&& split
== -1)
3902 if (sgn
== isl_tab_row_neg
)
3905 if (row
< tab
->n_row
)
3908 struct isl_vec
*ineq
;
3910 split
= context
->op
->best_split(context
, tab
);
3913 ineq
= get_row_parameter_ineq(tab
, split
);
3917 reset_any_to_unknown(tab
);
3918 tab
->row_sign
[split
] = isl_tab_row_pos
;
3920 find_in_pos(sol
, tab
, ineq
->el
);
3921 tab
->row_sign
[split
] = isl_tab_row_neg
;
3922 isl_seq_neg(ineq
->el
, ineq
->el
, ineq
->size
);
3923 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3925 context
->op
->add_ineq(context
, ineq
->el
, 0, 1);
3933 row
= first_non_integer_row(tab
, &flags
);
3936 if (ISL_FL_ISSET(flags
, I_PAR
)) {
3937 if (ISL_FL_ISSET(flags
, I_VAR
)) {
3938 if (isl_tab_mark_empty(tab
) < 0)
3942 row
= add_cut(tab
, row
);
3943 } else if (ISL_FL_ISSET(flags
, I_VAR
)) {
3944 struct isl_vec
*div
;
3945 struct isl_vec
*ineq
;
3947 div
= get_row_split_div(tab
, row
);
3950 d
= context
->op
->get_div(context
, tab
, div
);
3954 ineq
= ineq_for_div(context
->op
->peek_basic_set(context
), d
);
3958 no_sol_in_strict(sol
, tab
, ineq
);
3959 isl_seq_neg(ineq
->el
, ineq
->el
, ineq
->size
);
3960 context
->op
->add_ineq(context
, ineq
->el
, 1, 1);
3962 if (sol
->error
|| !context
->op
->is_ok(context
))
3964 tab
= set_row_cst_to_div(tab
, row
, d
);
3965 if (context
->op
->is_empty(context
))
3968 row
= add_parametric_cut(tab
, row
, context
);
3983 /* Does "sol" contain a pair of partial solutions that could potentially
3986 * We currently only check that "sol" is not in an error state
3987 * and that there are at least two partial solutions of which the final two
3988 * are defined at the same level.
3990 static int sol_has_mergeable_solutions(struct isl_sol
*sol
)
3996 if (!sol
->partial
->next
)
3998 return sol
->partial
->level
== sol
->partial
->next
->level
;
4001 /* Compute the lexicographic minimum of the set represented by the main
4002 * tableau "tab" within the context "sol->context_tab".
4004 * As a preprocessing step, we first transfer all the purely parametric
4005 * equalities from the main tableau to the context tableau, i.e.,
4006 * parameters that have been pivoted to a row.
4007 * These equalities are ignored by the main algorithm, because the
4008 * corresponding rows may not be marked as being non-negative.
4009 * In parts of the context where the added equality does not hold,
4010 * the main tableau is marked as being empty.
4012 * Before we embark on the actual computation, we save a copy
4013 * of the context. When we return, we check if there are any
4014 * partial solutions that can potentially be merged. If so,
4015 * we perform a rollback to the initial state of the context.
4016 * The merging of partial solutions happens inside calls to
4017 * sol_dec_level that are pushed onto the undo stack of the context.
4018 * If there are no partial solutions that can potentially be merged
4019 * then the rollback is skipped as it would just be wasted effort.
4021 static void find_solutions_main(struct isl_sol
*sol
, struct isl_tab
*tab
)
4031 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
4035 if (tab
->row_var
[row
] < 0)
4037 if (tab
->row_var
[row
] >= tab
->n_param
&&
4038 tab
->row_var
[row
] < tab
->n_var
- tab
->n_div
)
4040 if (tab
->row_var
[row
] < tab
->n_param
)
4041 p
= tab
->row_var
[row
];
4043 p
= tab
->row_var
[row
]
4044 + tab
->n_param
- (tab
->n_var
- tab
->n_div
);
4046 eq
= isl_vec_alloc(tab
->mat
->ctx
, 1+tab
->n_param
+tab
->n_div
);
4049 get_row_parameter_line(tab
, row
, eq
->el
);
4050 isl_int_neg(eq
->el
[1 + p
], tab
->mat
->row
[row
][0]);
4051 eq
= isl_vec_normalize(eq
);
4054 no_sol_in_strict(sol
, tab
, eq
);
4056 isl_seq_neg(eq
->el
, eq
->el
, eq
->size
);
4058 no_sol_in_strict(sol
, tab
, eq
);
4059 isl_seq_neg(eq
->el
, eq
->el
, eq
->size
);
4061 sol
->context
->op
->add_eq(sol
->context
, eq
->el
, 1, 1);
4065 if (isl_tab_mark_redundant(tab
, row
) < 0)
4068 if (sol
->context
->op
->is_empty(sol
->context
))
4071 row
= tab
->n_redundant
- 1;
4074 saved
= sol
->context
->op
->save(sol
->context
);
4076 find_solutions(sol
, tab
);
4078 if (sol_has_mergeable_solutions(sol
))
4079 sol
->context
->op
->restore(sol
->context
, saved
);
4081 sol
->context
->op
->discard(saved
);
4092 /* Check if integer division "div" of "dom" also occurs in "bmap".
4093 * If so, return its position within the divs.
4094 * If not, return -1.
4096 static int find_context_div(struct isl_basic_map
*bmap
,
4097 struct isl_basic_set
*dom
, unsigned div
)
4100 unsigned b_dim
= isl_space_dim(bmap
->dim
, isl_dim_all
);
4101 unsigned d_dim
= isl_space_dim(dom
->dim
, isl_dim_all
);
4103 if (isl_int_is_zero(dom
->div
[div
][0]))
4105 if (isl_seq_first_non_zero(dom
->div
[div
] + 2 + d_dim
, dom
->n_div
) != -1)
4108 for (i
= 0; i
< bmap
->n_div
; ++i
) {
4109 if (isl_int_is_zero(bmap
->div
[i
][0]))
4111 if (isl_seq_first_non_zero(bmap
->div
[i
] + 2 + d_dim
,
4112 (b_dim
- d_dim
) + bmap
->n_div
) != -1)
4114 if (isl_seq_eq(bmap
->div
[i
], dom
->div
[div
], 2 + d_dim
))
4120 /* The correspondence between the variables in the main tableau,
4121 * the context tableau, and the input map and domain is as follows.
4122 * The first n_param and the last n_div variables of the main tableau
4123 * form the variables of the context tableau.
4124 * In the basic map, these n_param variables correspond to the
4125 * parameters and the input dimensions. In the domain, they correspond
4126 * to the parameters and the set dimensions.
4127 * The n_div variables correspond to the integer divisions in the domain.
4128 * To ensure that everything lines up, we may need to copy some of the
4129 * integer divisions of the domain to the map. These have to be placed
4130 * in the same order as those in the context and they have to be placed
4131 * after any other integer divisions that the map may have.
4132 * This function performs the required reordering.
4134 static struct isl_basic_map
*align_context_divs(struct isl_basic_map
*bmap
,
4135 struct isl_basic_set
*dom
)
4141 for (i
= 0; i
< dom
->n_div
; ++i
)
4142 if (find_context_div(bmap
, dom
, i
) != -1)
4144 other
= bmap
->n_div
- common
;
4145 if (dom
->n_div
- common
> 0) {
4146 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
4147 dom
->n_div
- common
, 0, 0);
4151 for (i
= 0; i
< dom
->n_div
; ++i
) {
4152 int pos
= find_context_div(bmap
, dom
, i
);
4154 pos
= isl_basic_map_alloc_div(bmap
);
4157 isl_int_set_si(bmap
->div
[pos
][0], 0);
4159 if (pos
!= other
+ i
)
4160 isl_basic_map_swap_div(bmap
, pos
, other
+ i
);
4164 isl_basic_map_free(bmap
);
4168 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
4169 * some obvious symmetries.
4171 * We make sure the divs in the domain are properly ordered,
4172 * because they will be added one by one in the given order
4173 * during the construction of the solution map.
4174 * Furthermore, make sure that the known integer divisions
4175 * appear before any unknown integer division because the solution
4176 * may depend on the known integer divisions, while anything that
4177 * depends on any variable starting from the first unknown integer
4178 * division is ignored in sol_pma_add.
4180 static struct isl_sol
*basic_map_partial_lexopt_base_sol(
4181 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
4182 __isl_give isl_set
**empty
, int max
,
4183 struct isl_sol
*(*init
)(__isl_keep isl_basic_map
*bmap
,
4184 __isl_take isl_basic_set
*dom
, int track_empty
, int max
))
4186 struct isl_tab
*tab
;
4187 struct isl_sol
*sol
= NULL
;
4188 struct isl_context
*context
;
4191 dom
= isl_basic_set_sort_divs(dom
);
4192 bmap
= align_context_divs(bmap
, dom
);
4194 sol
= init(bmap
, dom
, !!empty
, max
);
4198 context
= sol
->context
;
4199 if (isl_basic_set_plain_is_empty(context
->op
->peek_basic_set(context
)))
4201 else if (isl_basic_map_plain_is_empty(bmap
)) {
4204 isl_basic_set_copy(context
->op
->peek_basic_set(context
)));
4206 tab
= tab_for_lexmin(bmap
,
4207 context
->op
->peek_basic_set(context
), 1, max
);
4208 tab
= context
->op
->detect_nonnegative_parameters(context
, tab
);
4209 find_solutions_main(sol
, tab
);
4214 isl_basic_map_free(bmap
);
4218 isl_basic_map_free(bmap
);
4222 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
4223 * some obvious symmetries.
4225 * We call basic_map_partial_lexopt_base_sol and extract the results.
4227 static __isl_give isl_map
*basic_map_partial_lexopt_base(
4228 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
4229 __isl_give isl_set
**empty
, int max
)
4231 isl_map
*result
= NULL
;
4232 struct isl_sol
*sol
;
4233 struct isl_sol_map
*sol_map
;
4235 sol
= basic_map_partial_lexopt_base_sol(bmap
, dom
, empty
, max
,
4239 sol_map
= (struct isl_sol_map
*) sol
;
4241 result
= isl_map_copy(sol_map
->map
);
4243 *empty
= isl_set_copy(sol_map
->empty
);
4244 sol_free(&sol_map
->sol
);
4248 /* Return a count of the number of occurrences of the "n" first
4249 * variables in the inequality constraints of "bmap".
4251 static __isl_give
int *count_occurrences(__isl_keep isl_basic_map
*bmap
,
4260 ctx
= isl_basic_map_get_ctx(bmap
);
4261 occurrences
= isl_calloc_array(ctx
, int, n
);
4265 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
4266 for (j
= 0; j
< n
; ++j
) {
4267 if (!isl_int_is_zero(bmap
->ineq
[i
][1 + j
]))
4275 /* Do all of the "n" variables with non-zero coefficients in "c"
4276 * occur in exactly a single constraint.
4277 * "occurrences" is an array of length "n" containing the number
4278 * of occurrences of each of the variables in the inequality constraints.
4280 static int single_occurrence(int n
, isl_int
*c
, int *occurrences
)
4284 for (i
= 0; i
< n
; ++i
) {
4285 if (isl_int_is_zero(c
[i
]))
4287 if (occurrences
[i
] != 1)
4294 /* Do all of the "n" initial variables that occur in inequality constraint
4295 * "ineq" of "bmap" only occur in that constraint?
4297 static int all_single_occurrence(__isl_keep isl_basic_map
*bmap
, int ineq
,
4302 for (i
= 0; i
< n
; ++i
) {
4303 if (isl_int_is_zero(bmap
->ineq
[ineq
][1 + i
]))
4305 for (j
= 0; j
< bmap
->n_ineq
; ++j
) {
4308 if (!isl_int_is_zero(bmap
->ineq
[j
][1 + i
]))
4316 /* Structure used during detection of parallel constraints.
4317 * n_in: number of "input" variables: isl_dim_param + isl_dim_in
4318 * n_out: number of "output" variables: isl_dim_out + isl_dim_div
4319 * val: the coefficients of the output variables
4321 struct isl_constraint_equal_info
{
4322 isl_basic_map
*bmap
;
4328 /* Check whether the coefficients of the output variables
4329 * of the constraint in "entry" are equal to info->val.
4331 static int constraint_equal(const void *entry
, const void *val
)
4333 isl_int
**row
= (isl_int
**)entry
;
4334 const struct isl_constraint_equal_info
*info
= val
;
4336 return isl_seq_eq((*row
) + 1 + info
->n_in
, info
->val
, info
->n_out
);
4339 /* Check whether "bmap" has a pair of constraints that have
4340 * the same coefficients for the output variables.
4341 * Note that the coefficients of the existentially quantified
4342 * variables need to be zero since the existentially quantified
4343 * of the result are usually not the same as those of the input.
4344 * Furthermore, check that each of the input variables that occur
4345 * in those constraints does not occur in any other constraint.
4346 * If so, return 1 and return the row indices of the two constraints
4347 * in *first and *second.
4349 static int parallel_constraints(__isl_keep isl_basic_map
*bmap
,
4350 int *first
, int *second
)
4354 int *occurrences
= NULL
;
4355 struct isl_hash_table
*table
= NULL
;
4356 struct isl_hash_table_entry
*entry
;
4357 struct isl_constraint_equal_info info
;
4361 ctx
= isl_basic_map_get_ctx(bmap
);
4362 table
= isl_hash_table_alloc(ctx
, bmap
->n_ineq
);
4366 info
.n_in
= isl_basic_map_dim(bmap
, isl_dim_param
) +
4367 isl_basic_map_dim(bmap
, isl_dim_in
);
4368 occurrences
= count_occurrences(bmap
, info
.n_in
);
4369 if (info
.n_in
&& !occurrences
)
4372 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4373 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4374 info
.n_out
= n_out
+ n_div
;
4375 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
4378 info
.val
= bmap
->ineq
[i
] + 1 + info
.n_in
;
4379 if (isl_seq_first_non_zero(info
.val
, n_out
) < 0)
4381 if (isl_seq_first_non_zero(info
.val
+ n_out
, n_div
) >= 0)
4383 if (!single_occurrence(info
.n_in
, bmap
->ineq
[i
] + 1,
4386 hash
= isl_seq_get_hash(info
.val
, info
.n_out
);
4387 entry
= isl_hash_table_find(ctx
, table
, hash
,
4388 constraint_equal
, &info
, 1);
4393 entry
->data
= &bmap
->ineq
[i
];
4396 if (i
< bmap
->n_ineq
) {
4397 *first
= ((isl_int
**)entry
->data
) - bmap
->ineq
;
4401 isl_hash_table_free(ctx
, table
);
4404 return i
< bmap
->n_ineq
;
4406 isl_hash_table_free(ctx
, table
);
4411 /* Given a set of upper bounds in "var", add constraints to "bset"
4412 * that make the i-th bound smallest.
4414 * In particular, if there are n bounds b_i, then add the constraints
4416 * b_i <= b_j for j > i
4417 * b_i < b_j for j < i
4419 static __isl_give isl_basic_set
*select_minimum(__isl_take isl_basic_set
*bset
,
4420 __isl_keep isl_mat
*var
, int i
)
4425 ctx
= isl_mat_get_ctx(var
);
4427 for (j
= 0; j
< var
->n_row
; ++j
) {
4430 k
= isl_basic_set_alloc_inequality(bset
);
4433 isl_seq_combine(bset
->ineq
[k
], ctx
->one
, var
->row
[j
],
4434 ctx
->negone
, var
->row
[i
], var
->n_col
);
4435 isl_int_set_si(bset
->ineq
[k
][var
->n_col
], 0);
4437 isl_int_sub_ui(bset
->ineq
[k
][0], bset
->ineq
[k
][0], 1);
4440 bset
= isl_basic_set_finalize(bset
);
4444 isl_basic_set_free(bset
);
4448 /* Given a set of upper bounds on the last "input" variable m,
4449 * construct a set that assigns the minimal upper bound to m, i.e.,
4450 * construct a set that divides the space into cells where one
4451 * of the upper bounds is smaller than all the others and assign
4452 * this upper bound to m.
4454 * In particular, if there are n bounds b_i, then the result
4455 * consists of n basic sets, each one of the form
4458 * b_i <= b_j for j > i
4459 * b_i < b_j for j < i
4461 static __isl_give isl_set
*set_minimum(__isl_take isl_space
*dim
,
4462 __isl_take isl_mat
*var
)
4465 isl_basic_set
*bset
= NULL
;
4466 isl_set
*set
= NULL
;
4471 set
= isl_set_alloc_space(isl_space_copy(dim
),
4472 var
->n_row
, ISL_SET_DISJOINT
);
4474 for (i
= 0; i
< var
->n_row
; ++i
) {
4475 bset
= isl_basic_set_alloc_space(isl_space_copy(dim
), 0,
4477 k
= isl_basic_set_alloc_equality(bset
);
4480 isl_seq_cpy(bset
->eq
[k
], var
->row
[i
], var
->n_col
);
4481 isl_int_set_si(bset
->eq
[k
][var
->n_col
], -1);
4482 bset
= select_minimum(bset
, var
, i
);
4483 set
= isl_set_add_basic_set(set
, bset
);
4486 isl_space_free(dim
);
4490 isl_basic_set_free(bset
);
4492 isl_space_free(dim
);
4497 /* Given that the last input variable of "bmap" represents the minimum
4498 * of the bounds in "cst", check whether we need to split the domain
4499 * based on which bound attains the minimum.
4501 * A split is needed when the minimum appears in an integer division
4502 * or in an equality. Otherwise, it is only needed if it appears in
4503 * an upper bound that is different from the upper bounds on which it
4506 static int need_split_basic_map(__isl_keep isl_basic_map
*bmap
,
4507 __isl_keep isl_mat
*cst
)
4513 pos
= cst
->n_col
- 1;
4514 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4516 for (i
= 0; i
< bmap
->n_div
; ++i
)
4517 if (!isl_int_is_zero(bmap
->div
[i
][2 + pos
]))
4520 for (i
= 0; i
< bmap
->n_eq
; ++i
)
4521 if (!isl_int_is_zero(bmap
->eq
[i
][1 + pos
]))
4524 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
4525 if (isl_int_is_nonneg(bmap
->ineq
[i
][1 + pos
]))
4527 if (!isl_int_is_negone(bmap
->ineq
[i
][1 + pos
]))
4529 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + pos
+ 1,
4530 total
- pos
- 1) >= 0)
4533 for (j
= 0; j
< cst
->n_row
; ++j
)
4534 if (isl_seq_eq(bmap
->ineq
[i
], cst
->row
[j
], cst
->n_col
))
4536 if (j
>= cst
->n_row
)
4543 /* Given that the last set variable of "bset" represents the minimum
4544 * of the bounds in "cst", check whether we need to split the domain
4545 * based on which bound attains the minimum.
4547 * We simply call need_split_basic_map here. This is safe because
4548 * the position of the minimum is computed from "cst" and not
4551 static int need_split_basic_set(__isl_keep isl_basic_set
*bset
,
4552 __isl_keep isl_mat
*cst
)
4554 return need_split_basic_map(bset_to_bmap(bset
), cst
);
4557 /* Given that the last set variable of "set" represents the minimum
4558 * of the bounds in "cst", check whether we need to split the domain
4559 * based on which bound attains the minimum.
4561 static int need_split_set(__isl_keep isl_set
*set
, __isl_keep isl_mat
*cst
)
4565 for (i
= 0; i
< set
->n
; ++i
)
4566 if (need_split_basic_set(set
->p
[i
], cst
))
4572 /* Given a set of which the last set variable is the minimum
4573 * of the bounds in "cst", split each basic set in the set
4574 * in pieces where one of the bounds is (strictly) smaller than the others.
4575 * This subdivision is given in "min_expr".
4576 * The variable is subsequently projected out.
4578 * We only do the split when it is needed.
4579 * For example if the last input variable m = min(a,b) and the only
4580 * constraints in the given basic set are lower bounds on m,
4581 * i.e., l <= m = min(a,b), then we can simply project out m
4582 * to obtain l <= a and l <= b, without having to split on whether
4583 * m is equal to a or b.
4585 static __isl_give isl_set
*split(__isl_take isl_set
*empty
,
4586 __isl_take isl_set
*min_expr
, __isl_take isl_mat
*cst
)
4593 if (!empty
|| !min_expr
|| !cst
)
4596 n_in
= isl_set_dim(empty
, isl_dim_set
);
4597 dim
= isl_set_get_space(empty
);
4598 dim
= isl_space_drop_dims(dim
, isl_dim_set
, n_in
- 1, 1);
4599 res
= isl_set_empty(dim
);
4601 for (i
= 0; i
< empty
->n
; ++i
) {
4604 set
= isl_set_from_basic_set(isl_basic_set_copy(empty
->p
[i
]));
4605 if (need_split_basic_set(empty
->p
[i
], cst
))
4606 set
= isl_set_intersect(set
, isl_set_copy(min_expr
));
4607 set
= isl_set_remove_dims(set
, isl_dim_set
, n_in
- 1, 1);
4609 res
= isl_set_union_disjoint(res
, set
);
4612 isl_set_free(empty
);
4613 isl_set_free(min_expr
);
4617 isl_set_free(empty
);
4618 isl_set_free(min_expr
);
4623 /* Given a map of which the last input variable is the minimum
4624 * of the bounds in "cst", split each basic set in the set
4625 * in pieces where one of the bounds is (strictly) smaller than the others.
4626 * This subdivision is given in "min_expr".
4627 * The variable is subsequently projected out.
4629 * The implementation is essentially the same as that of "split".
4631 static __isl_give isl_map
*split_domain(__isl_take isl_map
*opt
,
4632 __isl_take isl_set
*min_expr
, __isl_take isl_mat
*cst
)
4639 if (!opt
|| !min_expr
|| !cst
)
4642 n_in
= isl_map_dim(opt
, isl_dim_in
);
4643 dim
= isl_map_get_space(opt
);
4644 dim
= isl_space_drop_dims(dim
, isl_dim_in
, n_in
- 1, 1);
4645 res
= isl_map_empty(dim
);
4647 for (i
= 0; i
< opt
->n
; ++i
) {
4650 map
= isl_map_from_basic_map(isl_basic_map_copy(opt
->p
[i
]));
4651 if (need_split_basic_map(opt
->p
[i
], cst
))
4652 map
= isl_map_intersect_domain(map
,
4653 isl_set_copy(min_expr
));
4654 map
= isl_map_remove_dims(map
, isl_dim_in
, n_in
- 1, 1);
4656 res
= isl_map_union_disjoint(res
, map
);
4660 isl_set_free(min_expr
);
4665 isl_set_free(min_expr
);
4670 static __isl_give isl_map
*basic_map_partial_lexopt(
4671 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
4672 __isl_give isl_set
**empty
, int max
);
4674 /* This function is called from basic_map_partial_lexopt_symm.
4675 * The last variable of "bmap" and "dom" corresponds to the minimum
4676 * of the bounds in "cst". "map_space" is the space of the original
4677 * input relation (of basic_map_partial_lexopt_symm) and "set_space"
4678 * is the space of the original domain.
4680 * We recursively call basic_map_partial_lexopt and then plug in
4681 * the definition of the minimum in the result.
4683 static __isl_give isl_map
*basic_map_partial_lexopt_symm_core(
4684 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
4685 __isl_give isl_set
**empty
, int max
, __isl_take isl_mat
*cst
,
4686 __isl_take isl_space
*map_space
, __isl_take isl_space
*set_space
)
4691 min_expr
= set_minimum(isl_basic_set_get_space(dom
), isl_mat_copy(cst
));
4693 opt
= basic_map_partial_lexopt(bmap
, dom
, empty
, max
);
4696 *empty
= split(*empty
,
4697 isl_set_copy(min_expr
), isl_mat_copy(cst
));
4698 *empty
= isl_set_reset_space(*empty
, set_space
);
4701 opt
= split_domain(opt
, min_expr
, cst
);
4702 opt
= isl_map_reset_space(opt
, map_space
);
4707 /* Extract a domain from "bmap" for the purpose of computing
4708 * a lexicographic optimum.
4710 * This function is only called when the caller wants to compute a full
4711 * lexicographic optimum, i.e., without specifying a domain. In this case,
4712 * the caller is not interested in the part of the domain space where
4713 * there is no solution and the domain can be initialized to those constraints
4714 * of "bmap" that only involve the parameters and the input dimensions.
4715 * This relieves the parametric programming engine from detecting those
4716 * inequalities and transferring them to the context. More importantly,
4717 * it ensures that those inequalities are transferred first and not
4718 * intermixed with inequalities that actually split the domain.
4720 * If the caller does not require the absence of existentially quantified
4721 * variables in the result (i.e., if ISL_OPT_QE is not set in "flags"),
4722 * then the actual domain of "bmap" can be used. This ensures that
4723 * the domain does not need to be split at all just to separate out
4724 * pieces of the domain that do not have a solution from piece that do.
4725 * This domain cannot be used in general because it may involve
4726 * (unknown) existentially quantified variables which will then also
4727 * appear in the solution.
4729 static __isl_give isl_basic_set
*extract_domain(__isl_keep isl_basic_map
*bmap
,
4735 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4736 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4737 bmap
= isl_basic_map_copy(bmap
);
4738 if (ISL_FL_ISSET(flags
, ISL_OPT_QE
)) {
4739 bmap
= isl_basic_map_drop_constraints_involving_dims(bmap
,
4740 isl_dim_div
, 0, n_div
);
4741 bmap
= isl_basic_map_drop_constraints_involving_dims(bmap
,
4742 isl_dim_out
, 0, n_out
);
4744 return isl_basic_map_domain(bmap
);
4748 #define TYPE isl_map
4751 #include "isl_tab_lexopt_templ.c"
4753 struct isl_sol_for
{
4755 int (*fn
)(__isl_take isl_basic_set
*dom
,
4756 __isl_take isl_aff_list
*list
, void *user
);
4760 static void sol_for_free(struct isl_sol_for
*sol_for
)
4764 if (sol_for
->sol
.context
)
4765 sol_for
->sol
.context
->op
->free(sol_for
->sol
.context
);
4769 static void sol_for_free_wrap(struct isl_sol
*sol
)
4771 sol_for_free((struct isl_sol_for
*)sol
);
4774 /* Add the solution identified by the tableau and the context tableau.
4776 * See documentation of sol_add for more details.
4778 * Instead of constructing a basic map, this function calls a user
4779 * defined function with the current context as a basic set and
4780 * a list of affine expressions representing the relation between
4781 * the input and output. The space over which the affine expressions
4782 * are defined is the same as that of the domain. The number of
4783 * affine expressions in the list is equal to the number of output variables.
4785 static void sol_for_add(struct isl_sol_for
*sol
,
4786 struct isl_basic_set
*dom
, struct isl_mat
*M
)
4790 isl_local_space
*ls
;
4794 if (sol
->sol
.error
|| !dom
|| !M
)
4797 ctx
= isl_basic_set_get_ctx(dom
);
4798 ls
= isl_basic_set_get_local_space(dom
);
4799 list
= isl_aff_list_alloc(ctx
, M
->n_row
- 1);
4800 for (i
= 1; i
< M
->n_row
; ++i
) {
4801 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
4803 isl_int_set(aff
->v
->el
[0], M
->row
[0][0]);
4804 isl_seq_cpy(aff
->v
->el
+ 1, M
->row
[i
], M
->n_col
);
4806 aff
= isl_aff_normalize(aff
);
4807 list
= isl_aff_list_add(list
, aff
);
4809 isl_local_space_free(ls
);
4811 dom
= isl_basic_set_finalize(dom
);
4813 if (sol
->fn(isl_basic_set_copy(dom
), list
, sol
->user
) < 0)
4816 isl_basic_set_free(dom
);
4820 isl_basic_set_free(dom
);
4825 static void sol_for_add_wrap(struct isl_sol
*sol
,
4826 struct isl_basic_set
*dom
, struct isl_mat
*M
)
4828 sol_for_add((struct isl_sol_for
*)sol
, dom
, M
);
4831 static struct isl_sol_for
*sol_for_init(struct isl_basic_map
*bmap
, int max
,
4832 int (*fn
)(__isl_take isl_basic_set
*dom
, __isl_take isl_aff_list
*list
,
4836 struct isl_sol_for
*sol_for
= NULL
;
4838 struct isl_basic_set
*dom
= NULL
;
4840 sol_for
= isl_calloc_type(bmap
->ctx
, struct isl_sol_for
);
4844 dom_dim
= isl_space_domain(isl_space_copy(bmap
->dim
));
4845 dom
= isl_basic_set_universe(dom_dim
);
4847 sol_for
->sol
.rational
= ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
4848 sol_for
->sol
.dec_level
.callback
.run
= &sol_dec_level_wrap
;
4849 sol_for
->sol
.dec_level
.sol
= &sol_for
->sol
;
4851 sol_for
->user
= user
;
4852 sol_for
->sol
.max
= max
;
4853 sol_for
->sol
.n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4854 sol_for
->sol
.add
= &sol_for_add_wrap
;
4855 sol_for
->sol
.add_empty
= NULL
;
4856 sol_for
->sol
.free
= &sol_for_free_wrap
;
4858 sol_for
->sol
.context
= isl_context_alloc(dom
);
4859 if (!sol_for
->sol
.context
)
4862 isl_basic_set_free(dom
);
4865 isl_basic_set_free(dom
);
4866 sol_for_free(sol_for
);
4870 static void sol_for_find_solutions(struct isl_sol_for
*sol_for
,
4871 struct isl_tab
*tab
)
4873 find_solutions_main(&sol_for
->sol
, tab
);
4876 int isl_basic_map_foreach_lexopt(__isl_keep isl_basic_map
*bmap
, int max
,
4877 int (*fn
)(__isl_take isl_basic_set
*dom
, __isl_take isl_aff_list
*list
,
4881 struct isl_sol_for
*sol_for
= NULL
;
4883 bmap
= isl_basic_map_copy(bmap
);
4884 bmap
= isl_basic_map_detect_equalities(bmap
);
4888 sol_for
= sol_for_init(bmap
, max
, fn
, user
);
4892 if (isl_basic_map_plain_is_empty(bmap
))
4895 struct isl_tab
*tab
;
4896 struct isl_context
*context
= sol_for
->sol
.context
;
4897 tab
= tab_for_lexmin(bmap
,
4898 context
->op
->peek_basic_set(context
), 1, max
);
4899 tab
= context
->op
->detect_nonnegative_parameters(context
, tab
);
4900 sol_for_find_solutions(sol_for
, tab
);
4901 if (sol_for
->sol
.error
)
4905 sol_free(&sol_for
->sol
);
4906 isl_basic_map_free(bmap
);
4909 sol_free(&sol_for
->sol
);
4910 isl_basic_map_free(bmap
);
4914 int isl_basic_set_foreach_lexopt(__isl_keep isl_basic_set
*bset
, int max
,
4915 int (*fn
)(__isl_take isl_basic_set
*dom
, __isl_take isl_aff_list
*list
,
4919 return isl_basic_map_foreach_lexopt(bset
, max
, fn
, user
);
4922 /* Check if the given sequence of len variables starting at pos
4923 * represents a trivial (i.e., zero) solution.
4924 * The variables are assumed to be non-negative and to come in pairs,
4925 * with each pair representing a variable of unrestricted sign.
4926 * The solution is trivial if each such pair in the sequence consists
4927 * of two identical values, meaning that the variable being represented
4930 static int region_is_trivial(struct isl_tab
*tab
, int pos
, int len
)
4937 for (i
= 0; i
< len
; i
+= 2) {
4941 neg_row
= tab
->var
[pos
+ i
].is_row
?
4942 tab
->var
[pos
+ i
].index
: -1;
4943 pos_row
= tab
->var
[pos
+ i
+ 1].is_row
?
4944 tab
->var
[pos
+ i
+ 1].index
: -1;
4947 isl_int_is_zero(tab
->mat
->row
[neg_row
][1])) &&
4949 isl_int_is_zero(tab
->mat
->row
[pos_row
][1])))
4952 if (neg_row
< 0 || pos_row
< 0)
4954 if (isl_int_ne(tab
->mat
->row
[neg_row
][1],
4955 tab
->mat
->row
[pos_row
][1]))
4962 /* Return the index of the first trivial region or -1 if all regions
4965 static int first_trivial_region(struct isl_tab
*tab
,
4966 int n_region
, struct isl_region
*region
)
4970 for (i
= 0; i
< n_region
; ++i
) {
4971 if (region_is_trivial(tab
, region
[i
].pos
, region
[i
].len
))
4978 /* Check if the solution is optimal, i.e., whether the first
4979 * n_op entries are zero.
4981 static int is_optimal(__isl_keep isl_vec
*sol
, int n_op
)
4985 for (i
= 0; i
< n_op
; ++i
)
4986 if (!isl_int_is_zero(sol
->el
[1 + i
]))
4991 /* Add constraints to "tab" that ensure that any solution is significantly
4992 * better than that represented by "sol". That is, find the first
4993 * relevant (within first n_op) non-zero coefficient and force it (along
4994 * with all previous coefficients) to be zero.
4995 * If the solution is already optimal (all relevant coefficients are zero),
4996 * then just mark the table as empty.
4997 * "n_zero" is the number of coefficients that have been forced zero
4998 * by previous calls to this function at the same level.
4999 * Return the updated number of forced zero coefficients or -1 on error.
5001 * This function assumes that at least 2 * (n_op - n_zero) more rows and
5002 * at least 2 * (n_op - n_zero) more elements in the constraint array
5003 * are available in the tableau.
5005 static int force_better_solution(struct isl_tab
*tab
,
5006 __isl_keep isl_vec
*sol
, int n_op
, int n_zero
)
5015 for (i
= n_zero
; i
< n_op
; ++i
)
5016 if (!isl_int_is_zero(sol
->el
[1 + i
]))
5020 if (isl_tab_mark_empty(tab
) < 0)
5025 ctx
= isl_vec_get_ctx(sol
);
5026 v
= isl_vec_alloc(ctx
, 1 + tab
->n_var
);
5031 for (; i
>= n_zero
; --i
) {
5033 isl_int_set_si(v
->el
[1 + i
], -1);
5034 if (add_lexmin_eq(tab
, v
->el
) < 0)
5045 /* Local data at each level of the backtracking procedure of
5046 * isl_tab_basic_set_non_trivial_lexmin.
5048 * "n_zero" is the number of initial coordinates that have already
5049 * been forced to be zero at this level.
5051 struct isl_trivial
{
5056 struct isl_tab_undo
*snap
;
5059 /* Return the lexicographically smallest non-trivial solution of the
5060 * given ILP problem.
5062 * All variables are assumed to be non-negative.
5064 * n_op is the number of initial coordinates to optimize.
5065 * That is, once a solution has been found, we will only continue looking
5066 * for solution that result in significantly better values for those
5067 * initial coordinates. That is, we only continue looking for solutions
5068 * that increase the number of initial zeros in this sequence.
5070 * A solution is non-trivial, if it is non-trivial on each of the
5071 * specified regions. Each region represents a sequence of pairs
5072 * of variables. A solution is non-trivial on such a region if
5073 * at least one of these pairs consists of different values, i.e.,
5074 * such that the non-negative variable represented by the pair is non-zero.
5076 * Whenever a conflict is encountered, all constraints involved are
5077 * reported to the caller through a call to "conflict".
5079 * We perform a simple branch-and-bound backtracking search.
5080 * Each level in the search represents initially trivial region that is forced
5081 * to be non-trivial.
5082 * At each level we consider n cases, where n is the length of the region.
5083 * In terms of the n/2 variables of unrestricted signs being encoded by
5084 * the region, we consider the cases
5087 * x_0 = 0 and x_1 >= 1
5088 * x_0 = 0 and x_1 <= -1
5089 * x_0 = 0 and x_1 = 0 and x_2 >= 1
5090 * x_0 = 0 and x_1 = 0 and x_2 <= -1
5092 * The cases are considered in this order, assuming that each pair
5093 * x_i_a x_i_b represents the value x_i_b - x_i_a.
5094 * That is, x_0 >= 1 is enforced by adding the constraint
5095 * x_0_b - x_0_a >= 1
5097 __isl_give isl_vec
*isl_tab_basic_set_non_trivial_lexmin(
5098 __isl_take isl_basic_set
*bset
, int n_op
, int n_region
,
5099 struct isl_region
*region
,
5100 int (*conflict
)(int con
, void *user
), void *user
)
5106 isl_vec
*sol
= NULL
;
5107 struct isl_tab
*tab
;
5108 struct isl_trivial
*triv
= NULL
;
5114 ctx
= isl_basic_set_get_ctx(bset
);
5115 sol
= isl_vec_alloc(ctx
, 0);
5117 tab
= tab_for_lexmin(bset
, NULL
, 0, 0);
5120 tab
->conflict
= conflict
;
5121 tab
->conflict_user
= user
;
5123 v
= isl_vec_alloc(ctx
, 1 + tab
->n_var
);
5124 triv
= isl_calloc_array(ctx
, struct isl_trivial
, n_region
);
5125 if (!v
|| (n_region
&& !triv
))
5131 while (level
>= 0) {
5135 tab
= cut_to_integer_lexmin(tab
, CUT_ONE
);
5140 r
= first_trivial_region(tab
, n_region
, region
);
5142 for (i
= 0; i
< level
; ++i
)
5145 sol
= isl_tab_get_sample_value(tab
);
5148 if (is_optimal(sol
, n_op
))
5152 if (level
>= n_region
)
5153 isl_die(ctx
, isl_error_internal
,
5154 "nesting level too deep", goto error
);
5155 if (isl_tab_extend_cons(tab
,
5156 2 * region
[r
].len
+ 2 * n_op
) < 0)
5158 triv
[level
].region
= r
;
5159 triv
[level
].side
= 0;
5160 triv
[level
].update
= 0;
5161 triv
[level
].n_zero
= 0;
5164 r
= triv
[level
].region
;
5165 side
= triv
[level
].side
;
5166 base
= 2 * (side
/2);
5168 if (side
>= region
[r
].len
) {
5173 if (isl_tab_rollback(tab
, triv
[level
].snap
) < 0)
5178 if (triv
[level
].update
) {
5179 triv
[level
].n_zero
= force_better_solution(tab
, sol
,
5180 n_op
, triv
[level
].n_zero
);
5181 if (triv
[level
].n_zero
< 0)
5183 triv
[level
].update
= 0;
5186 if (side
== base
&& base
>= 2) {
5187 for (j
= base
- 2; j
< base
; ++j
) {
5189 isl_int_set_si(v
->el
[1 + region
[r
].pos
+ j
], 1);
5190 if (add_lexmin_eq(tab
, v
->el
) < 0)
5195 triv
[level
].snap
= isl_tab_snap(tab
);
5196 if (isl_tab_push_basis(tab
) < 0)
5200 isl_int_set_si(v
->el
[0], -1);
5201 isl_int_set_si(v
->el
[1 + region
[r
].pos
+ side
], -1);
5202 isl_int_set_si(v
->el
[1 + region
[r
].pos
+ (side
^ 1)], 1);
5203 tab
= add_lexmin_ineq(tab
, v
->el
);
5213 isl_basic_set_free(bset
);
5220 isl_basic_set_free(bset
);
5225 /* Wrapper for a tableau that is used for computing
5226 * the lexicographically smallest rational point of a non-negative set.
5227 * This point is represented by the sample value of "tab",
5228 * unless "tab" is empty.
5230 struct isl_tab_lexmin
{
5232 struct isl_tab
*tab
;
5235 /* Free "tl" and return NULL.
5237 __isl_null isl_tab_lexmin
*isl_tab_lexmin_free(__isl_take isl_tab_lexmin
*tl
)
5241 isl_ctx_deref(tl
->ctx
);
5242 isl_tab_free(tl
->tab
);
5248 /* Construct an isl_tab_lexmin for computing
5249 * the lexicographically smallest rational point in "bset",
5250 * assuming that all variables are non-negative.
5252 __isl_give isl_tab_lexmin
*isl_tab_lexmin_from_basic_set(
5253 __isl_take isl_basic_set
*bset
)
5261 ctx
= isl_basic_set_get_ctx(bset
);
5262 tl
= isl_calloc_type(ctx
, struct isl_tab_lexmin
);
5267 tl
->tab
= tab_for_lexmin(bset
, NULL
, 0, 0);
5268 isl_basic_set_free(bset
);
5270 return isl_tab_lexmin_free(tl
);
5273 isl_basic_set_free(bset
);
5274 isl_tab_lexmin_free(tl
);
5278 /* Return the dimension of the set represented by "tl".
5280 int isl_tab_lexmin_dim(__isl_keep isl_tab_lexmin
*tl
)
5282 return tl
? tl
->tab
->n_var
: -1;
5285 /* Add the equality with coefficients "eq" to "tl", updating the optimal
5286 * solution if needed.
5287 * The equality is added as two opposite inequality constraints.
5289 __isl_give isl_tab_lexmin
*isl_tab_lexmin_add_eq(__isl_take isl_tab_lexmin
*tl
,
5295 return isl_tab_lexmin_free(tl
);
5297 if (isl_tab_extend_cons(tl
->tab
, 2) < 0)
5298 return isl_tab_lexmin_free(tl
);
5299 n_var
= tl
->tab
->n_var
;
5300 isl_seq_neg(eq
, eq
, 1 + n_var
);
5301 tl
->tab
= add_lexmin_ineq(tl
->tab
, eq
);
5302 isl_seq_neg(eq
, eq
, 1 + n_var
);
5303 tl
->tab
= add_lexmin_ineq(tl
->tab
, eq
);
5306 return isl_tab_lexmin_free(tl
);
5311 /* Return the lexicographically smallest rational point in the basic set
5312 * from which "tl" was constructed.
5313 * If the original input was empty, then return a zero-length vector.
5315 __isl_give isl_vec
*isl_tab_lexmin_get_solution(__isl_keep isl_tab_lexmin
*tl
)
5320 return isl_vec_alloc(tl
->ctx
, 0);
5322 return isl_tab_get_sample_value(tl
->tab
);
5325 /* Return the lexicographically smallest rational point in "bset",
5326 * assuming that all variables are non-negative.
5327 * If "bset" is empty, then return a zero-length vector.
5329 __isl_give isl_vec
*isl_tab_basic_set_non_neg_lexmin(
5330 __isl_take isl_basic_set
*bset
)
5335 tl
= isl_tab_lexmin_from_basic_set(bset
);
5336 sol
= isl_tab_lexmin_get_solution(tl
);
5337 isl_tab_lexmin_free(tl
);
5341 struct isl_sol_pma
{
5343 isl_pw_multi_aff
*pma
;
5347 static void sol_pma_free(struct isl_sol_pma
*sol_pma
)
5351 if (sol_pma
->sol
.context
)
5352 sol_pma
->sol
.context
->op
->free(sol_pma
->sol
.context
);
5353 isl_pw_multi_aff_free(sol_pma
->pma
);
5354 isl_set_free(sol_pma
->empty
);
5358 /* This function is called for parts of the context where there is
5359 * no solution, with "bset" corresponding to the context tableau.
5360 * Simply add the basic set to the set "empty".
5362 static void sol_pma_add_empty(struct isl_sol_pma
*sol
,
5363 __isl_take isl_basic_set
*bset
)
5365 if (!bset
|| !sol
->empty
)
5368 sol
->empty
= isl_set_grow(sol
->empty
, 1);
5369 bset
= isl_basic_set_simplify(bset
);
5370 bset
= isl_basic_set_finalize(bset
);
5371 sol
->empty
= isl_set_add_basic_set(sol
->empty
, bset
);
5376 isl_basic_set_free(bset
);
5380 /* Check that the final columns of "M", starting at "first", are zero.
5382 static isl_stat
check_final_columns_are_zero(__isl_keep isl_mat
*M
,
5386 unsigned rows
, cols
, n
;
5389 return isl_stat_error
;
5390 rows
= isl_mat_rows(M
);
5391 cols
= isl_mat_cols(M
);
5393 for (i
= 0; i
< rows
; ++i
)
5394 if (isl_seq_first_non_zero(M
->row
[i
] + first
, n
) != -1)
5395 isl_die(isl_mat_get_ctx(M
), isl_error_internal
,
5396 "final columns should be zero",
5397 return isl_stat_error
);
5401 /* Set the affine expressions in "ma" according to the rows in "M", which
5402 * are defined over the local space "ls".
5403 * The matrix "M" may have extra (zero) columns beyond the number
5404 * of variables in "ls".
5406 static __isl_give isl_multi_aff
*set_from_affine_matrix(
5407 __isl_take isl_multi_aff
*ma
, __isl_take isl_local_space
*ls
,
5408 __isl_take isl_mat
*M
)
5413 if (!ma
|| !ls
|| !M
)
5416 dim
= isl_local_space_dim(ls
, isl_dim_all
);
5417 if (check_final_columns_are_zero(M
, 1 + dim
) < 0)
5419 for (i
= 1; i
< M
->n_row
; ++i
) {
5420 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
5422 isl_int_set(aff
->v
->el
[0], M
->row
[0][0]);
5423 isl_seq_cpy(aff
->v
->el
+ 1, M
->row
[i
], 1 + dim
);
5425 aff
= isl_aff_normalize(aff
);
5426 ma
= isl_multi_aff_set_aff(ma
, i
- 1, aff
);
5428 isl_local_space_free(ls
);
5433 isl_local_space_free(ls
);
5435 isl_multi_aff_free(ma
);
5439 /* Given a basic set "dom" that represents the context and an affine
5440 * matrix "M" that maps the dimensions of the context to the
5441 * output variables, construct an isl_pw_multi_aff with a single
5442 * cell corresponding to "dom" and affine expressions copied from "M".
5444 * Note that the description of the initial context may have involved
5445 * existentially quantified variables, in which case they also appear
5446 * in "dom". These need to be removed before creating the affine
5447 * expression because an affine expression cannot be defined in terms
5448 * of existentially quantified variables without a known representation.
5449 * Since newly added integer divisions are inserted before these
5450 * existentially quantified variables, they are still in the final
5451 * positions and the corresponding final columns of "M" are zero
5452 * because align_context_divs adds the existentially quantified
5453 * variables of the context to the main tableau without any constraints and
5454 * any equality constraints that are added later on can only serve
5455 * to eliminate these existentially quantified variables.
5457 static void sol_pma_add(struct isl_sol_pma
*sol
,
5458 __isl_take isl_basic_set
*dom
, __isl_take isl_mat
*M
)
5460 isl_local_space
*ls
;
5461 isl_multi_aff
*maff
;
5462 isl_pw_multi_aff
*pma
;
5465 n_div
= isl_basic_set_dim(dom
, isl_dim_div
);
5466 n_known
= n_div
- sol
->sol
.context
->n_unknown
;
5468 maff
= isl_multi_aff_alloc(isl_pw_multi_aff_get_space(sol
->pma
));
5469 ls
= isl_basic_set_get_local_space(dom
);
5470 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
,
5471 n_known
, n_div
- n_known
);
5472 maff
= set_from_affine_matrix(maff
, ls
, M
);
5473 dom
= isl_basic_set_simplify(dom
);
5474 dom
= isl_basic_set_finalize(dom
);
5475 pma
= isl_pw_multi_aff_alloc(isl_set_from_basic_set(dom
), maff
);
5476 sol
->pma
= isl_pw_multi_aff_add_disjoint(sol
->pma
, pma
);
5481 static void sol_pma_free_wrap(struct isl_sol
*sol
)
5483 sol_pma_free((struct isl_sol_pma
*)sol
);
5486 static void sol_pma_add_empty_wrap(struct isl_sol
*sol
,
5487 __isl_take isl_basic_set
*bset
)
5489 sol_pma_add_empty((struct isl_sol_pma
*)sol
, bset
);
5492 static void sol_pma_add_wrap(struct isl_sol
*sol
,
5493 __isl_take isl_basic_set
*dom
, __isl_take isl_mat
*M
)
5495 sol_pma_add((struct isl_sol_pma
*)sol
, dom
, M
);
5498 /* Construct an isl_sol_pma structure for accumulating the solution.
5499 * If track_empty is set, then we also keep track of the parts
5500 * of the context where there is no solution.
5501 * If max is set, then we are solving a maximization, rather than
5502 * a minimization problem, which means that the variables in the
5503 * tableau have value "M - x" rather than "M + x".
5505 static struct isl_sol
*sol_pma_init(__isl_keep isl_basic_map
*bmap
,
5506 __isl_take isl_basic_set
*dom
, int track_empty
, int max
)
5508 struct isl_sol_pma
*sol_pma
= NULL
;
5513 sol_pma
= isl_calloc_type(bmap
->ctx
, struct isl_sol_pma
);
5517 sol_pma
->sol
.rational
= ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
5518 sol_pma
->sol
.dec_level
.callback
.run
= &sol_dec_level_wrap
;
5519 sol_pma
->sol
.dec_level
.sol
= &sol_pma
->sol
;
5520 sol_pma
->sol
.max
= max
;
5521 sol_pma
->sol
.n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5522 sol_pma
->sol
.add
= &sol_pma_add_wrap
;
5523 sol_pma
->sol
.add_empty
= track_empty
? &sol_pma_add_empty_wrap
: NULL
;
5524 sol_pma
->sol
.free
= &sol_pma_free_wrap
;
5525 sol_pma
->pma
= isl_pw_multi_aff_empty(isl_basic_map_get_space(bmap
));
5529 sol_pma
->sol
.context
= isl_context_alloc(dom
);
5530 if (!sol_pma
->sol
.context
)
5534 sol_pma
->empty
= isl_set_alloc_space(isl_basic_set_get_space(dom
),
5535 1, ISL_SET_DISJOINT
);
5536 if (!sol_pma
->empty
)
5540 isl_basic_set_free(dom
);
5541 return &sol_pma
->sol
;
5543 isl_basic_set_free(dom
);
5544 sol_pma_free(sol_pma
);
5548 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
5549 * some obvious symmetries.
5551 * We call basic_map_partial_lexopt_base_sol and extract the results.
5553 static __isl_give isl_pw_multi_aff
*basic_map_partial_lexopt_base_pw_multi_aff(
5554 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5555 __isl_give isl_set
**empty
, int max
)
5557 isl_pw_multi_aff
*result
= NULL
;
5558 struct isl_sol
*sol
;
5559 struct isl_sol_pma
*sol_pma
;
5561 sol
= basic_map_partial_lexopt_base_sol(bmap
, dom
, empty
, max
,
5565 sol_pma
= (struct isl_sol_pma
*) sol
;
5567 result
= isl_pw_multi_aff_copy(sol_pma
->pma
);
5569 *empty
= isl_set_copy(sol_pma
->empty
);
5570 sol_free(&sol_pma
->sol
);
5574 /* Given that the last input variable of "maff" represents the minimum
5575 * of some bounds, check whether we need to plug in the expression
5578 * In particular, check if the last input variable appears in any
5579 * of the expressions in "maff".
5581 static int need_substitution(__isl_keep isl_multi_aff
*maff
)
5586 pos
= isl_multi_aff_dim(maff
, isl_dim_in
) - 1;
5588 for (i
= 0; i
< maff
->n
; ++i
)
5589 if (isl_aff_involves_dims(maff
->p
[i
], isl_dim_in
, pos
, 1))
5595 /* Given a set of upper bounds on the last "input" variable m,
5596 * construct a piecewise affine expression that selects
5597 * the minimal upper bound to m, i.e.,
5598 * divide the space into cells where one
5599 * of the upper bounds is smaller than all the others and select
5600 * this upper bound on that cell.
5602 * In particular, if there are n bounds b_i, then the result
5603 * consists of n cell, each one of the form
5605 * b_i <= b_j for j > i
5606 * b_i < b_j for j < i
5608 * The affine expression on this cell is
5612 static __isl_give isl_pw_aff
*set_minimum_pa(__isl_take isl_space
*space
,
5613 __isl_take isl_mat
*var
)
5616 isl_aff
*aff
= NULL
;
5617 isl_basic_set
*bset
= NULL
;
5618 isl_pw_aff
*paff
= NULL
;
5619 isl_space
*pw_space
;
5620 isl_local_space
*ls
= NULL
;
5625 ls
= isl_local_space_from_space(isl_space_copy(space
));
5626 pw_space
= isl_space_copy(space
);
5627 pw_space
= isl_space_from_domain(pw_space
);
5628 pw_space
= isl_space_add_dims(pw_space
, isl_dim_out
, 1);
5629 paff
= isl_pw_aff_alloc_size(pw_space
, var
->n_row
);
5631 for (i
= 0; i
< var
->n_row
; ++i
) {
5634 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
5635 bset
= isl_basic_set_alloc_space(isl_space_copy(space
), 0,
5639 isl_int_set_si(aff
->v
->el
[0], 1);
5640 isl_seq_cpy(aff
->v
->el
+ 1, var
->row
[i
], var
->n_col
);
5641 isl_int_set_si(aff
->v
->el
[1 + var
->n_col
], 0);
5642 bset
= select_minimum(bset
, var
, i
);
5643 paff_i
= isl_pw_aff_alloc(isl_set_from_basic_set(bset
), aff
);
5644 paff
= isl_pw_aff_add_disjoint(paff
, paff_i
);
5647 isl_local_space_free(ls
);
5648 isl_space_free(space
);
5653 isl_basic_set_free(bset
);
5654 isl_pw_aff_free(paff
);
5655 isl_local_space_free(ls
);
5656 isl_space_free(space
);
5661 /* Given a piecewise multi-affine expression of which the last input variable
5662 * is the minimum of the bounds in "cst", plug in the value of the minimum.
5663 * This minimum expression is given in "min_expr_pa".
5664 * The set "min_expr" contains the same information, but in the form of a set.
5665 * The variable is subsequently projected out.
5667 * The implementation is similar to those of "split" and "split_domain".
5668 * If the variable appears in a given expression, then minimum expression
5669 * is plugged in. Otherwise, if the variable appears in the constraints
5670 * and a split is required, then the domain is split. Otherwise, no split
5673 static __isl_give isl_pw_multi_aff
*split_domain_pma(
5674 __isl_take isl_pw_multi_aff
*opt
, __isl_take isl_pw_aff
*min_expr_pa
,
5675 __isl_take isl_set
*min_expr
, __isl_take isl_mat
*cst
)
5680 isl_pw_multi_aff
*res
;
5682 if (!opt
|| !min_expr
|| !cst
)
5685 n_in
= isl_pw_multi_aff_dim(opt
, isl_dim_in
);
5686 space
= isl_pw_multi_aff_get_space(opt
);
5687 space
= isl_space_drop_dims(space
, isl_dim_in
, n_in
- 1, 1);
5688 res
= isl_pw_multi_aff_empty(space
);
5690 for (i
= 0; i
< opt
->n
; ++i
) {
5691 isl_pw_multi_aff
*pma
;
5693 pma
= isl_pw_multi_aff_alloc(isl_set_copy(opt
->p
[i
].set
),
5694 isl_multi_aff_copy(opt
->p
[i
].maff
));
5695 if (need_substitution(opt
->p
[i
].maff
))
5696 pma
= isl_pw_multi_aff_substitute(pma
,
5697 isl_dim_in
, n_in
- 1, min_expr_pa
);
5698 else if (need_split_set(opt
->p
[i
].set
, cst
))
5699 pma
= isl_pw_multi_aff_intersect_domain(pma
,
5700 isl_set_copy(min_expr
));
5701 pma
= isl_pw_multi_aff_project_out(pma
,
5702 isl_dim_in
, n_in
- 1, 1);
5704 res
= isl_pw_multi_aff_add_disjoint(res
, pma
);
5707 isl_pw_multi_aff_free(opt
);
5708 isl_pw_aff_free(min_expr_pa
);
5709 isl_set_free(min_expr
);
5713 isl_pw_multi_aff_free(opt
);
5714 isl_pw_aff_free(min_expr_pa
);
5715 isl_set_free(min_expr
);
5720 static __isl_give isl_pw_multi_aff
*basic_map_partial_lexopt_pw_multi_aff(
5721 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5722 __isl_give isl_set
**empty
, int max
);
5724 /* This function is called from basic_map_partial_lexopt_symm.
5725 * The last variable of "bmap" and "dom" corresponds to the minimum
5726 * of the bounds in "cst". "map_space" is the space of the original
5727 * input relation (of basic_map_partial_lexopt_symm) and "set_space"
5728 * is the space of the original domain.
5730 * We recursively call basic_map_partial_lexopt and then plug in
5731 * the definition of the minimum in the result.
5733 static __isl_give isl_pw_multi_aff
*
5734 basic_map_partial_lexopt_symm_core_pw_multi_aff(
5735 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5736 __isl_give isl_set
**empty
, int max
, __isl_take isl_mat
*cst
,
5737 __isl_take isl_space
*map_space
, __isl_take isl_space
*set_space
)
5739 isl_pw_multi_aff
*opt
;
5740 isl_pw_aff
*min_expr_pa
;
5743 min_expr
= set_minimum(isl_basic_set_get_space(dom
), isl_mat_copy(cst
));
5744 min_expr_pa
= set_minimum_pa(isl_basic_set_get_space(dom
),
5747 opt
= basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, empty
, max
);
5750 *empty
= split(*empty
,
5751 isl_set_copy(min_expr
), isl_mat_copy(cst
));
5752 *empty
= isl_set_reset_space(*empty
, set_space
);
5755 opt
= split_domain_pma(opt
, min_expr_pa
, min_expr
, cst
);
5756 opt
= isl_pw_multi_aff_reset_space(opt
, map_space
);
5762 #define TYPE isl_pw_multi_aff
5764 #define SUFFIX _pw_multi_aff
5765 #include "isl_tab_lexopt_templ.c"