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);
2642 if (isl_tab_track_bset(tab
, bset
) < 0)
2644 tab
= isl_tab_init_samples(tab
);
2647 isl_basic_set_free(bset
);
2651 static struct isl_context
*isl_context_lex_alloc(struct isl_basic_set
*dom
)
2653 struct isl_context_lex
*clex
;
2658 clex
= isl_alloc_type(dom
->ctx
, struct isl_context_lex
);
2662 clex
->context
.op
= &isl_context_lex_op
;
2664 clex
->tab
= context_tab_for_lexmin(isl_basic_set_copy(dom
));
2665 if (restore_lexmin(clex
->tab
) < 0)
2667 clex
->tab
= check_integer_feasible(clex
->tab
);
2671 return &clex
->context
;
2673 clex
->context
.op
->free(&clex
->context
);
2677 /* Representation of the context when using generalized basis reduction.
2679 * "shifted" contains the offsets of the unit hypercubes that lie inside the
2680 * context. Any rational point in "shifted" can therefore be rounded
2681 * up to an integer point in the context.
2682 * If the context is constrained by any equality, then "shifted" is not used
2683 * as it would be empty.
2685 struct isl_context_gbr
{
2686 struct isl_context context
;
2687 struct isl_tab
*tab
;
2688 struct isl_tab
*shifted
;
2689 struct isl_tab
*cone
;
2692 static struct isl_tab
*context_gbr_detect_nonnegative_parameters(
2693 struct isl_context
*context
, struct isl_tab
*tab
)
2695 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2698 return tab_detect_nonnegative_parameters(tab
, cgbr
->tab
);
2701 static struct isl_basic_set
*context_gbr_peek_basic_set(
2702 struct isl_context
*context
)
2704 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2707 return isl_tab_peek_bset(cgbr
->tab
);
2710 static struct isl_tab
*context_gbr_peek_tab(struct isl_context
*context
)
2712 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2716 /* Initialize the "shifted" tableau of the context, which
2717 * contains the constraints of the original tableau shifted
2718 * by the sum of all negative coefficients. This ensures
2719 * that any rational point in the shifted tableau can
2720 * be rounded up to yield an integer point in the original tableau.
2722 static void gbr_init_shifted(struct isl_context_gbr
*cgbr
)
2725 struct isl_vec
*cst
;
2726 struct isl_basic_set
*bset
= isl_tab_peek_bset(cgbr
->tab
);
2727 unsigned dim
= isl_basic_set_total_dim(bset
);
2729 cst
= isl_vec_alloc(cgbr
->tab
->mat
->ctx
, bset
->n_ineq
);
2733 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
2734 isl_int_set(cst
->el
[i
], bset
->ineq
[i
][0]);
2735 for (j
= 0; j
< dim
; ++j
) {
2736 if (!isl_int_is_neg(bset
->ineq
[i
][1 + j
]))
2738 isl_int_add(bset
->ineq
[i
][0], bset
->ineq
[i
][0],
2739 bset
->ineq
[i
][1 + j
]);
2743 cgbr
->shifted
= isl_tab_from_basic_set(bset
, 0);
2745 for (i
= 0; i
< bset
->n_ineq
; ++i
)
2746 isl_int_set(bset
->ineq
[i
][0], cst
->el
[i
]);
2751 /* Check if the shifted tableau is non-empty, and if so
2752 * use the sample point to construct an integer point
2753 * of the context tableau.
2755 static struct isl_vec
*gbr_get_shifted_sample(struct isl_context_gbr
*cgbr
)
2757 struct isl_vec
*sample
;
2760 gbr_init_shifted(cgbr
);
2763 if (cgbr
->shifted
->empty
)
2764 return isl_vec_alloc(cgbr
->tab
->mat
->ctx
, 0);
2766 sample
= isl_tab_get_sample_value(cgbr
->shifted
);
2767 sample
= isl_vec_ceil(sample
);
2772 static struct isl_basic_set
*drop_constant_terms(struct isl_basic_set
*bset
)
2779 for (i
= 0; i
< bset
->n_eq
; ++i
)
2780 isl_int_set_si(bset
->eq
[i
][0], 0);
2782 for (i
= 0; i
< bset
->n_ineq
; ++i
)
2783 isl_int_set_si(bset
->ineq
[i
][0], 0);
2788 static int use_shifted(struct isl_context_gbr
*cgbr
)
2792 return cgbr
->tab
->bmap
->n_eq
== 0 && cgbr
->tab
->bmap
->n_div
== 0;
2795 static struct isl_vec
*gbr_get_sample(struct isl_context_gbr
*cgbr
)
2797 struct isl_basic_set
*bset
;
2798 struct isl_basic_set
*cone
;
2800 if (isl_tab_sample_is_integer(cgbr
->tab
))
2801 return isl_tab_get_sample_value(cgbr
->tab
);
2803 if (use_shifted(cgbr
)) {
2804 struct isl_vec
*sample
;
2806 sample
= gbr_get_shifted_sample(cgbr
);
2807 if (!sample
|| sample
->size
> 0)
2810 isl_vec_free(sample
);
2814 bset
= isl_tab_peek_bset(cgbr
->tab
);
2815 cgbr
->cone
= isl_tab_from_recession_cone(bset
, 0);
2818 if (isl_tab_track_bset(cgbr
->cone
,
2819 isl_basic_set_copy(bset
)) < 0)
2822 if (isl_tab_detect_implicit_equalities(cgbr
->cone
) < 0)
2825 if (cgbr
->cone
->n_dead
== cgbr
->cone
->n_col
) {
2826 struct isl_vec
*sample
;
2827 struct isl_tab_undo
*snap
;
2829 if (cgbr
->tab
->basis
) {
2830 if (cgbr
->tab
->basis
->n_col
!= 1 + cgbr
->tab
->n_var
) {
2831 isl_mat_free(cgbr
->tab
->basis
);
2832 cgbr
->tab
->basis
= NULL
;
2834 cgbr
->tab
->n_zero
= 0;
2835 cgbr
->tab
->n_unbounded
= 0;
2838 snap
= isl_tab_snap(cgbr
->tab
);
2840 sample
= isl_tab_sample(cgbr
->tab
);
2842 if (!sample
|| isl_tab_rollback(cgbr
->tab
, snap
) < 0) {
2843 isl_vec_free(sample
);
2850 cone
= isl_basic_set_dup(isl_tab_peek_bset(cgbr
->cone
));
2851 cone
= drop_constant_terms(cone
);
2852 cone
= isl_basic_set_update_from_tab(cone
, cgbr
->cone
);
2853 cone
= isl_basic_set_underlying_set(cone
);
2854 cone
= isl_basic_set_gauss(cone
, NULL
);
2856 bset
= isl_basic_set_dup(isl_tab_peek_bset(cgbr
->tab
));
2857 bset
= isl_basic_set_update_from_tab(bset
, cgbr
->tab
);
2858 bset
= isl_basic_set_underlying_set(bset
);
2859 bset
= isl_basic_set_gauss(bset
, NULL
);
2861 return isl_basic_set_sample_with_cone(bset
, cone
);
2864 static void check_gbr_integer_feasible(struct isl_context_gbr
*cgbr
)
2866 struct isl_vec
*sample
;
2871 if (cgbr
->tab
->empty
)
2874 sample
= gbr_get_sample(cgbr
);
2878 if (sample
->size
== 0) {
2879 isl_vec_free(sample
);
2880 if (isl_tab_mark_empty(cgbr
->tab
) < 0)
2885 if (isl_tab_add_sample(cgbr
->tab
, sample
) < 0)
2890 isl_tab_free(cgbr
->tab
);
2894 static struct isl_tab
*add_gbr_eq(struct isl_tab
*tab
, isl_int
*eq
)
2899 if (isl_tab_extend_cons(tab
, 2) < 0)
2902 if (isl_tab_add_eq(tab
, eq
) < 0)
2911 /* Add the equality described by "eq" to the context.
2912 * If "check" is set, then we check if the context is empty after
2913 * adding the equality.
2914 * If "update" is set, then we check if the samples are still valid.
2916 * We do not explicitly add shifted copies of the equality to
2917 * cgbr->shifted since they would conflict with each other.
2918 * Instead, we directly mark cgbr->shifted empty.
2920 static void context_gbr_add_eq(struct isl_context
*context
, isl_int
*eq
,
2921 int check
, int update
)
2923 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2925 cgbr
->tab
= add_gbr_eq(cgbr
->tab
, eq
);
2927 if (cgbr
->shifted
&& !cgbr
->shifted
->empty
&& use_shifted(cgbr
)) {
2928 if (isl_tab_mark_empty(cgbr
->shifted
) < 0)
2932 if (cgbr
->cone
&& cgbr
->cone
->n_col
!= cgbr
->cone
->n_dead
) {
2933 if (isl_tab_extend_cons(cgbr
->cone
, 2) < 0)
2935 if (isl_tab_add_eq(cgbr
->cone
, eq
) < 0)
2940 int v
= tab_has_valid_sample(cgbr
->tab
, eq
, 1);
2944 check_gbr_integer_feasible(cgbr
);
2947 cgbr
->tab
= check_samples(cgbr
->tab
, eq
, 1);
2950 isl_tab_free(cgbr
->tab
);
2954 static void add_gbr_ineq(struct isl_context_gbr
*cgbr
, isl_int
*ineq
)
2959 if (isl_tab_extend_cons(cgbr
->tab
, 1) < 0)
2962 if (isl_tab_add_ineq(cgbr
->tab
, ineq
) < 0)
2965 if (cgbr
->shifted
&& !cgbr
->shifted
->empty
&& use_shifted(cgbr
)) {
2968 dim
= isl_basic_map_total_dim(cgbr
->tab
->bmap
);
2970 if (isl_tab_extend_cons(cgbr
->shifted
, 1) < 0)
2973 for (i
= 0; i
< dim
; ++i
) {
2974 if (!isl_int_is_neg(ineq
[1 + i
]))
2976 isl_int_add(ineq
[0], ineq
[0], ineq
[1 + i
]);
2979 if (isl_tab_add_ineq(cgbr
->shifted
, ineq
) < 0)
2982 for (i
= 0; i
< dim
; ++i
) {
2983 if (!isl_int_is_neg(ineq
[1 + i
]))
2985 isl_int_sub(ineq
[0], ineq
[0], ineq
[1 + i
]);
2989 if (cgbr
->cone
&& cgbr
->cone
->n_col
!= cgbr
->cone
->n_dead
) {
2990 if (isl_tab_extend_cons(cgbr
->cone
, 1) < 0)
2992 if (isl_tab_add_ineq(cgbr
->cone
, ineq
) < 0)
2998 isl_tab_free(cgbr
->tab
);
3002 static void context_gbr_add_ineq(struct isl_context
*context
, isl_int
*ineq
,
3003 int check
, int update
)
3005 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3007 add_gbr_ineq(cgbr
, ineq
);
3012 int v
= tab_has_valid_sample(cgbr
->tab
, ineq
, 0);
3016 check_gbr_integer_feasible(cgbr
);
3019 cgbr
->tab
= check_samples(cgbr
->tab
, ineq
, 0);
3022 isl_tab_free(cgbr
->tab
);
3026 static int context_gbr_add_ineq_wrap(void *user
, isl_int
*ineq
)
3028 struct isl_context
*context
= (struct isl_context
*)user
;
3029 context_gbr_add_ineq(context
, ineq
, 0, 0);
3030 return context
->op
->is_ok(context
) ? 0 : -1;
3033 static enum isl_tab_row_sign
context_gbr_ineq_sign(struct isl_context
*context
,
3034 isl_int
*ineq
, int strict
)
3036 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3037 return tab_ineq_sign(cgbr
->tab
, ineq
, strict
);
3040 /* Check whether "ineq" can be added to the tableau without rendering
3043 static int context_gbr_test_ineq(struct isl_context
*context
, isl_int
*ineq
)
3045 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3046 struct isl_tab_undo
*snap
;
3047 struct isl_tab_undo
*shifted_snap
= NULL
;
3048 struct isl_tab_undo
*cone_snap
= NULL
;
3054 if (isl_tab_extend_cons(cgbr
->tab
, 1) < 0)
3057 snap
= isl_tab_snap(cgbr
->tab
);
3059 shifted_snap
= isl_tab_snap(cgbr
->shifted
);
3061 cone_snap
= isl_tab_snap(cgbr
->cone
);
3062 add_gbr_ineq(cgbr
, ineq
);
3063 check_gbr_integer_feasible(cgbr
);
3066 feasible
= !cgbr
->tab
->empty
;
3067 if (isl_tab_rollback(cgbr
->tab
, snap
) < 0)
3070 if (isl_tab_rollback(cgbr
->shifted
, shifted_snap
))
3072 } else if (cgbr
->shifted
) {
3073 isl_tab_free(cgbr
->shifted
);
3074 cgbr
->shifted
= NULL
;
3077 if (isl_tab_rollback(cgbr
->cone
, cone_snap
))
3079 } else if (cgbr
->cone
) {
3080 isl_tab_free(cgbr
->cone
);
3087 /* Return the column of the last of the variables associated to
3088 * a column that has a non-zero coefficient.
3089 * This function is called in a context where only coefficients
3090 * of parameters or divs can be non-zero.
3092 static int last_non_zero_var_col(struct isl_tab
*tab
, isl_int
*p
)
3097 if (tab
->n_var
== 0)
3100 for (i
= tab
->n_var
- 1; i
>= 0; --i
) {
3101 if (i
>= tab
->n_param
&& i
< tab
->n_var
- tab
->n_div
)
3103 if (tab
->var
[i
].is_row
)
3105 col
= tab
->var
[i
].index
;
3106 if (!isl_int_is_zero(p
[col
]))
3113 /* Look through all the recently added equalities in the context
3114 * to see if we can propagate any of them to the main tableau.
3116 * The newly added equalities in the context are encoded as pairs
3117 * of inequalities starting at inequality "first".
3119 * We tentatively add each of these equalities to the main tableau
3120 * and if this happens to result in a row with a final coefficient
3121 * that is one or negative one, we use it to kill a column
3122 * in the main tableau. Otherwise, we discard the tentatively
3124 * This tentative addition of equality constraints turns
3125 * on the undo facility of the tableau. Turn it off again
3126 * at the end, assuming it was turned off to begin with.
3128 * Return 0 on success and -1 on failure.
3130 static int propagate_equalities(struct isl_context_gbr
*cgbr
,
3131 struct isl_tab
*tab
, unsigned first
)
3134 struct isl_vec
*eq
= NULL
;
3135 isl_bool needs_undo
;
3137 needs_undo
= isl_tab_need_undo(tab
);
3140 eq
= isl_vec_alloc(tab
->mat
->ctx
, 1 + tab
->n_var
);
3144 if (isl_tab_extend_cons(tab
, (cgbr
->tab
->bmap
->n_ineq
- first
)/2) < 0)
3147 isl_seq_clr(eq
->el
+ 1 + tab
->n_param
,
3148 tab
->n_var
- tab
->n_param
- tab
->n_div
);
3149 for (i
= first
; i
< cgbr
->tab
->bmap
->n_ineq
; i
+= 2) {
3152 struct isl_tab_undo
*snap
;
3153 snap
= isl_tab_snap(tab
);
3155 isl_seq_cpy(eq
->el
, cgbr
->tab
->bmap
->ineq
[i
], 1 + tab
->n_param
);
3156 isl_seq_cpy(eq
->el
+ 1 + tab
->n_var
- tab
->n_div
,
3157 cgbr
->tab
->bmap
->ineq
[i
] + 1 + tab
->n_param
,
3160 r
= isl_tab_add_row(tab
, eq
->el
);
3163 r
= tab
->con
[r
].index
;
3164 j
= last_non_zero_var_col(tab
, tab
->mat
->row
[r
] + 2 + tab
->M
);
3165 if (j
< 0 || j
< tab
->n_dead
||
3166 !isl_int_is_one(tab
->mat
->row
[r
][0]) ||
3167 (!isl_int_is_one(tab
->mat
->row
[r
][2 + tab
->M
+ j
]) &&
3168 !isl_int_is_negone(tab
->mat
->row
[r
][2 + tab
->M
+ j
]))) {
3169 if (isl_tab_rollback(tab
, snap
) < 0)
3173 if (isl_tab_pivot(tab
, r
, j
) < 0)
3175 if (isl_tab_kill_col(tab
, j
) < 0)
3178 if (restore_lexmin(tab
) < 0)
3183 isl_tab_clear_undo(tab
);
3189 isl_tab_free(cgbr
->tab
);
3194 static int context_gbr_detect_equalities(struct isl_context
*context
,
3195 struct isl_tab
*tab
)
3197 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3201 struct isl_basic_set
*bset
= isl_tab_peek_bset(cgbr
->tab
);
3202 cgbr
->cone
= isl_tab_from_recession_cone(bset
, 0);
3205 if (isl_tab_track_bset(cgbr
->cone
,
3206 isl_basic_set_copy(bset
)) < 0)
3209 if (isl_tab_detect_implicit_equalities(cgbr
->cone
) < 0)
3212 n_ineq
= cgbr
->tab
->bmap
->n_ineq
;
3213 cgbr
->tab
= isl_tab_detect_equalities(cgbr
->tab
, cgbr
->cone
);
3216 if (cgbr
->tab
->bmap
->n_ineq
> n_ineq
&&
3217 propagate_equalities(cgbr
, tab
, n_ineq
) < 0)
3222 isl_tab_free(cgbr
->tab
);
3227 static int context_gbr_get_div(struct isl_context
*context
, struct isl_tab
*tab
,
3228 struct isl_vec
*div
)
3230 return get_div(tab
, context
, div
);
3233 static isl_bool
context_gbr_insert_div(struct isl_context
*context
, int pos
,
3234 __isl_keep isl_vec
*div
)
3236 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3238 int r
, n_div
, o_div
;
3240 n_div
= isl_basic_map_dim(cgbr
->cone
->bmap
, isl_dim_div
);
3241 o_div
= cgbr
->cone
->n_var
- n_div
;
3243 if (isl_tab_extend_cons(cgbr
->cone
, 3) < 0)
3244 return isl_bool_error
;
3245 if (isl_tab_extend_vars(cgbr
->cone
, 1) < 0)
3246 return isl_bool_error
;
3247 if ((r
= isl_tab_insert_var(cgbr
->cone
, pos
)) <0)
3248 return isl_bool_error
;
3250 cgbr
->cone
->bmap
= isl_basic_map_insert_div(cgbr
->cone
->bmap
,
3252 if (!cgbr
->cone
->bmap
)
3253 return isl_bool_error
;
3254 if (isl_tab_push_var(cgbr
->cone
, isl_tab_undo_bmap_div
,
3255 &cgbr
->cone
->var
[r
]) < 0)
3256 return isl_bool_error
;
3258 return context_tab_insert_div(cgbr
->tab
, pos
, div
,
3259 context_gbr_add_ineq_wrap
, context
);
3262 static int context_gbr_best_split(struct isl_context
*context
,
3263 struct isl_tab
*tab
)
3265 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3266 struct isl_tab_undo
*snap
;
3269 snap
= isl_tab_snap(cgbr
->tab
);
3270 r
= best_split(tab
, cgbr
->tab
);
3272 if (r
>= 0 && isl_tab_rollback(cgbr
->tab
, snap
) < 0)
3278 static int context_gbr_is_empty(struct isl_context
*context
)
3280 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3283 return cgbr
->tab
->empty
;
3286 struct isl_gbr_tab_undo
{
3287 struct isl_tab_undo
*tab_snap
;
3288 struct isl_tab_undo
*shifted_snap
;
3289 struct isl_tab_undo
*cone_snap
;
3292 static void *context_gbr_save(struct isl_context
*context
)
3294 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3295 struct isl_gbr_tab_undo
*snap
;
3300 snap
= isl_alloc_type(cgbr
->tab
->mat
->ctx
, struct isl_gbr_tab_undo
);
3304 snap
->tab_snap
= isl_tab_snap(cgbr
->tab
);
3305 if (isl_tab_save_samples(cgbr
->tab
) < 0)
3309 snap
->shifted_snap
= isl_tab_snap(cgbr
->shifted
);
3311 snap
->shifted_snap
= NULL
;
3314 snap
->cone_snap
= isl_tab_snap(cgbr
->cone
);
3316 snap
->cone_snap
= NULL
;
3324 static void context_gbr_restore(struct isl_context
*context
, void *save
)
3326 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3327 struct isl_gbr_tab_undo
*snap
= (struct isl_gbr_tab_undo
*)save
;
3330 if (isl_tab_rollback(cgbr
->tab
, snap
->tab_snap
) < 0)
3333 if (snap
->shifted_snap
) {
3334 if (isl_tab_rollback(cgbr
->shifted
, snap
->shifted_snap
) < 0)
3336 } else if (cgbr
->shifted
) {
3337 isl_tab_free(cgbr
->shifted
);
3338 cgbr
->shifted
= NULL
;
3341 if (snap
->cone_snap
) {
3342 if (isl_tab_rollback(cgbr
->cone
, snap
->cone_snap
) < 0)
3344 } else if (cgbr
->cone
) {
3345 isl_tab_free(cgbr
->cone
);
3354 isl_tab_free(cgbr
->tab
);
3358 static void context_gbr_discard(void *save
)
3360 struct isl_gbr_tab_undo
*snap
= (struct isl_gbr_tab_undo
*)save
;
3364 static int context_gbr_is_ok(struct isl_context
*context
)
3366 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3370 static void context_gbr_invalidate(struct isl_context
*context
)
3372 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3373 isl_tab_free(cgbr
->tab
);
3377 static __isl_null
struct isl_context
*context_gbr_free(
3378 struct isl_context
*context
)
3380 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3381 isl_tab_free(cgbr
->tab
);
3382 isl_tab_free(cgbr
->shifted
);
3383 isl_tab_free(cgbr
->cone
);
3389 struct isl_context_op isl_context_gbr_op
= {
3390 context_gbr_detect_nonnegative_parameters
,
3391 context_gbr_peek_basic_set
,
3392 context_gbr_peek_tab
,
3394 context_gbr_add_ineq
,
3395 context_gbr_ineq_sign
,
3396 context_gbr_test_ineq
,
3397 context_gbr_get_div
,
3398 context_gbr_insert_div
,
3399 context_gbr_detect_equalities
,
3400 context_gbr_best_split
,
3401 context_gbr_is_empty
,
3404 context_gbr_restore
,
3405 context_gbr_discard
,
3406 context_gbr_invalidate
,
3410 static struct isl_context
*isl_context_gbr_alloc(__isl_keep isl_basic_set
*dom
)
3412 struct isl_context_gbr
*cgbr
;
3417 cgbr
= isl_calloc_type(dom
->ctx
, struct isl_context_gbr
);
3421 cgbr
->context
.op
= &isl_context_gbr_op
;
3423 cgbr
->shifted
= NULL
;
3425 cgbr
->tab
= isl_tab_from_basic_set(dom
, 1);
3426 cgbr
->tab
= isl_tab_init_samples(cgbr
->tab
);
3429 check_gbr_integer_feasible(cgbr
);
3431 return &cgbr
->context
;
3433 cgbr
->context
.op
->free(&cgbr
->context
);
3437 /* Allocate a context corresponding to "dom".
3438 * The representation specific fields are initialized by
3439 * isl_context_lex_alloc or isl_context_gbr_alloc.
3440 * The shared "n_unknown" field is initialized to the number
3441 * of final unknown integer divisions in "dom".
3443 static struct isl_context
*isl_context_alloc(__isl_keep isl_basic_set
*dom
)
3445 struct isl_context
*context
;
3451 if (dom
->ctx
->opt
->context
== ISL_CONTEXT_LEXMIN
)
3452 context
= isl_context_lex_alloc(dom
);
3454 context
= isl_context_gbr_alloc(dom
);
3459 first
= isl_basic_set_first_unknown_div(dom
);
3461 return context
->op
->free(context
);
3462 context
->n_unknown
= isl_basic_set_dim(dom
, isl_dim_div
) - first
;
3467 /* Construct an isl_sol_map structure for accumulating the solution.
3468 * If track_empty is set, then we also keep track of the parts
3469 * of the context where there is no solution.
3470 * If max is set, then we are solving a maximization, rather than
3471 * a minimization problem, which means that the variables in the
3472 * tableau have value "M - x" rather than "M + x".
3474 static struct isl_sol
*sol_map_init(struct isl_basic_map
*bmap
,
3475 struct isl_basic_set
*dom
, int track_empty
, int max
)
3477 struct isl_sol_map
*sol_map
= NULL
;
3482 sol_map
= isl_calloc_type(bmap
->ctx
, struct isl_sol_map
);
3486 sol_map
->sol
.rational
= ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
3487 sol_map
->sol
.dec_level
.callback
.run
= &sol_dec_level_wrap
;
3488 sol_map
->sol
.dec_level
.sol
= &sol_map
->sol
;
3489 sol_map
->sol
.max
= max
;
3490 sol_map
->sol
.n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
3491 sol_map
->sol
.add
= &sol_map_add_wrap
;
3492 sol_map
->sol
.add_empty
= track_empty
? &sol_map_add_empty_wrap
: NULL
;
3493 sol_map
->sol
.free
= &sol_map_free_wrap
;
3494 sol_map
->map
= isl_map_alloc_space(isl_basic_map_get_space(bmap
), 1,
3499 sol_map
->sol
.context
= isl_context_alloc(dom
);
3500 if (!sol_map
->sol
.context
)
3504 sol_map
->empty
= isl_set_alloc_space(isl_basic_set_get_space(dom
),
3505 1, ISL_SET_DISJOINT
);
3506 if (!sol_map
->empty
)
3510 isl_basic_set_free(dom
);
3511 return &sol_map
->sol
;
3513 isl_basic_set_free(dom
);
3514 sol_map_free(sol_map
);
3518 /* Check whether all coefficients of (non-parameter) variables
3519 * are non-positive, meaning that no pivots can be performed on the row.
3521 static int is_critical(struct isl_tab
*tab
, int row
)
3524 unsigned off
= 2 + tab
->M
;
3526 for (j
= tab
->n_dead
; j
< tab
->n_col
; ++j
) {
3527 if (tab
->col_var
[j
] >= 0 &&
3528 (tab
->col_var
[j
] < tab
->n_param
||
3529 tab
->col_var
[j
] >= tab
->n_var
- tab
->n_div
))
3532 if (isl_int_is_pos(tab
->mat
->row
[row
][off
+ j
]))
3539 /* Check whether the inequality represented by vec is strict over the integers,
3540 * i.e., there are no integer values satisfying the constraint with
3541 * equality. This happens if the gcd of the coefficients is not a divisor
3542 * of the constant term. If so, scale the constraint down by the gcd
3543 * of the coefficients.
3545 static int is_strict(struct isl_vec
*vec
)
3551 isl_seq_gcd(vec
->el
+ 1, vec
->size
- 1, &gcd
);
3552 if (!isl_int_is_one(gcd
)) {
3553 strict
= !isl_int_is_divisible_by(vec
->el
[0], gcd
);
3554 isl_int_fdiv_q(vec
->el
[0], vec
->el
[0], gcd
);
3555 isl_seq_scale_down(vec
->el
+ 1, vec
->el
+ 1, gcd
, vec
->size
-1);
3562 /* Determine the sign of the given row of the main tableau.
3563 * The result is one of
3564 * isl_tab_row_pos: always non-negative; no pivot needed
3565 * isl_tab_row_neg: always non-positive; pivot
3566 * isl_tab_row_any: can be both positive and negative; split
3568 * We first handle some simple cases
3569 * - the row sign may be known already
3570 * - the row may be obviously non-negative
3571 * - the parametric constant may be equal to that of another row
3572 * for which we know the sign. This sign will be either "pos" or
3573 * "any". If it had been "neg" then we would have pivoted before.
3575 * If none of these cases hold, we check the value of the row for each
3576 * of the currently active samples. Based on the signs of these values
3577 * we make an initial determination of the sign of the row.
3579 * all zero -> unk(nown)
3580 * all non-negative -> pos
3581 * all non-positive -> neg
3582 * both negative and positive -> all
3584 * If we end up with "all", we are done.
3585 * Otherwise, we perform a check for positive and/or negative
3586 * values as follows.
3588 * samples neg unk pos
3594 * There is no special sign for "zero", because we can usually treat zero
3595 * as either non-negative or non-positive, whatever works out best.
3596 * However, if the row is "critical", meaning that pivoting is impossible
3597 * then we don't want to limp zero with the non-positive case, because
3598 * then we we would lose the solution for those values of the parameters
3599 * where the value of the row is zero. Instead, we treat 0 as non-negative
3600 * ensuring a split if the row can attain both zero and negative values.
3601 * The same happens when the original constraint was one that could not
3602 * be satisfied with equality by any integer values of the parameters.
3603 * In this case, we normalize the constraint, but then a value of zero
3604 * for the normalized constraint is actually a positive value for the
3605 * original constraint, so again we need to treat zero as non-negative.
3606 * In both these cases, we have the following decision tree instead:
3608 * all non-negative -> pos
3609 * all negative -> neg
3610 * both negative and non-negative -> all
3618 static enum isl_tab_row_sign
row_sign(struct isl_tab
*tab
,
3619 struct isl_sol
*sol
, int row
)
3621 struct isl_vec
*ineq
= NULL
;
3622 enum isl_tab_row_sign res
= isl_tab_row_unknown
;
3627 if (tab
->row_sign
[row
] != isl_tab_row_unknown
)
3628 return tab
->row_sign
[row
];
3629 if (is_obviously_nonneg(tab
, row
))
3630 return isl_tab_row_pos
;
3631 for (row2
= tab
->n_redundant
; row2
< tab
->n_row
; ++row2
) {
3632 if (tab
->row_sign
[row2
] == isl_tab_row_unknown
)
3634 if (identical_parameter_line(tab
, row
, row2
))
3635 return tab
->row_sign
[row2
];
3638 critical
= is_critical(tab
, row
);
3640 ineq
= get_row_parameter_ineq(tab
, row
);
3644 strict
= is_strict(ineq
);
3646 res
= sol
->context
->op
->ineq_sign(sol
->context
, ineq
->el
,
3647 critical
|| strict
);
3649 if (res
== isl_tab_row_unknown
|| res
== isl_tab_row_pos
) {
3650 /* test for negative values */
3652 isl_seq_neg(ineq
->el
, ineq
->el
, ineq
->size
);
3653 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3655 feasible
= sol
->context
->op
->test_ineq(sol
->context
, ineq
->el
);
3659 res
= isl_tab_row_pos
;
3661 res
= (res
== isl_tab_row_unknown
) ? isl_tab_row_neg
3663 if (res
== isl_tab_row_neg
) {
3664 isl_seq_neg(ineq
->el
, ineq
->el
, ineq
->size
);
3665 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3669 if (res
== isl_tab_row_neg
) {
3670 /* test for positive values */
3672 if (!critical
&& !strict
)
3673 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3675 feasible
= sol
->context
->op
->test_ineq(sol
->context
, ineq
->el
);
3679 res
= isl_tab_row_any
;
3686 return isl_tab_row_unknown
;
3689 static void find_solutions(struct isl_sol
*sol
, struct isl_tab
*tab
);
3691 /* Find solutions for values of the parameters that satisfy the given
3694 * We currently take a snapshot of the context tableau that is reset
3695 * when we return from this function, while we make a copy of the main
3696 * tableau, leaving the original main tableau untouched.
3697 * These are fairly arbitrary choices. Making a copy also of the context
3698 * tableau would obviate the need to undo any changes made to it later,
3699 * while taking a snapshot of the main tableau could reduce memory usage.
3700 * If we were to switch to taking a snapshot of the main tableau,
3701 * we would have to keep in mind that we need to save the row signs
3702 * and that we need to do this before saving the current basis
3703 * such that the basis has been restore before we restore the row signs.
3705 static void find_in_pos(struct isl_sol
*sol
, struct isl_tab
*tab
, isl_int
*ineq
)
3711 saved
= sol
->context
->op
->save(sol
->context
);
3713 tab
= isl_tab_dup(tab
);
3717 sol
->context
->op
->add_ineq(sol
->context
, ineq
, 0, 1);
3719 find_solutions(sol
, tab
);
3722 sol
->context
->op
->restore(sol
->context
, saved
);
3724 sol
->context
->op
->discard(saved
);
3730 /* Record the absence of solutions for those values of the parameters
3731 * that do not satisfy the given inequality with equality.
3733 static void no_sol_in_strict(struct isl_sol
*sol
,
3734 struct isl_tab
*tab
, struct isl_vec
*ineq
)
3739 if (!sol
->context
|| sol
->error
)
3741 saved
= sol
->context
->op
->save(sol
->context
);
3743 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3745 sol
->context
->op
->add_ineq(sol
->context
, ineq
->el
, 1, 0);
3754 isl_int_add_ui(ineq
->el
[0], ineq
->el
[0], 1);
3756 sol
->context
->op
->restore(sol
->context
, saved
);
3762 /* Reset all row variables that are marked to have a sign that may
3763 * be both positive and negative to have an unknown sign.
3765 static void reset_any_to_unknown(struct isl_tab
*tab
)
3769 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
3770 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
3772 if (tab
->row_sign
[row
] == isl_tab_row_any
)
3773 tab
->row_sign
[row
] = isl_tab_row_unknown
;
3777 /* Compute the lexicographic minimum of the set represented by the main
3778 * tableau "tab" within the context "sol->context_tab".
3779 * On entry the sample value of the main tableau is lexicographically
3780 * less than or equal to this lexicographic minimum.
3781 * Pivots are performed until a feasible point is found, which is then
3782 * necessarily equal to the minimum, or until the tableau is found to
3783 * be infeasible. Some pivots may need to be performed for only some
3784 * feasible values of the context tableau. If so, the context tableau
3785 * is split into a part where the pivot is needed and a part where it is not.
3787 * Whenever we enter the main loop, the main tableau is such that no
3788 * "obvious" pivots need to be performed on it, where "obvious" means
3789 * that the given row can be seen to be negative without looking at
3790 * the context tableau. In particular, for non-parametric problems,
3791 * no pivots need to be performed on the main tableau.
3792 * The caller of find_solutions is responsible for making this property
3793 * hold prior to the first iteration of the loop, while restore_lexmin
3794 * is called before every other iteration.
3796 * Inside the main loop, we first examine the signs of the rows of
3797 * the main tableau within the context of the context tableau.
3798 * If we find a row that is always non-positive for all values of
3799 * the parameters satisfying the context tableau and negative for at
3800 * least one value of the parameters, we perform the appropriate pivot
3801 * and start over. An exception is the case where no pivot can be
3802 * performed on the row. In this case, we require that the sign of
3803 * the row is negative for all values of the parameters (rather than just
3804 * non-positive). This special case is handled inside row_sign, which
3805 * will say that the row can have any sign if it determines that it can
3806 * attain both negative and zero values.
3808 * If we can't find a row that always requires a pivot, but we can find
3809 * one or more rows that require a pivot for some values of the parameters
3810 * (i.e., the row can attain both positive and negative signs), then we split
3811 * the context tableau into two parts, one where we force the sign to be
3812 * non-negative and one where we force is to be negative.
3813 * The non-negative part is handled by a recursive call (through find_in_pos).
3814 * Upon returning from this call, we continue with the negative part and
3815 * perform the required pivot.
3817 * If no such rows can be found, all rows are non-negative and we have
3818 * found a (rational) feasible point. If we only wanted a rational point
3820 * Otherwise, we check if all values of the sample point of the tableau
3821 * are integral for the variables. If so, we have found the minimal
3822 * integral point and we are done.
3823 * If the sample point is not integral, then we need to make a distinction
3824 * based on whether the constant term is non-integral or the coefficients
3825 * of the parameters. Furthermore, in order to decide how to handle
3826 * the non-integrality, we also need to know whether the coefficients
3827 * of the other columns in the tableau are integral. This leads
3828 * to the following table. The first two rows do not correspond
3829 * to a non-integral sample point and are only mentioned for completeness.
3831 * constant parameters other
3834 * int int rat | -> no problem
3836 * rat int int -> fail
3838 * rat int rat -> cut
3841 * rat rat rat | -> parametric cut
3844 * rat rat int | -> split context
3846 * If the parametric constant is completely integral, then there is nothing
3847 * to be done. If the constant term is non-integral, but all the other
3848 * coefficient are integral, then there is nothing that can be done
3849 * and the tableau has no integral solution.
3850 * If, on the other hand, one or more of the other columns have rational
3851 * coefficients, but the parameter coefficients are all integral, then
3852 * we can perform a regular (non-parametric) cut.
3853 * Finally, if there is any parameter coefficient that is non-integral,
3854 * then we need to involve the context tableau. There are two cases here.
3855 * If at least one other column has a rational coefficient, then we
3856 * can perform a parametric cut in the main tableau by adding a new
3857 * integer division in the context tableau.
3858 * If all other columns have integral coefficients, then we need to
3859 * enforce that the rational combination of parameters (c + \sum a_i y_i)/m
3860 * is always integral. We do this by introducing an integer division
3861 * q = floor((c + \sum a_i y_i)/m) and stipulating that its argument should
3862 * always be integral in the context tableau, i.e., m q = c + \sum a_i y_i.
3863 * Since q is expressed in the tableau as
3864 * c + \sum a_i y_i - m q >= 0
3865 * -c - \sum a_i y_i + m q + m - 1 >= 0
3866 * it is sufficient to add the inequality
3867 * -c - \sum a_i y_i + m q >= 0
3868 * In the part of the context where this inequality does not hold, the
3869 * main tableau is marked as being empty.
3871 static void find_solutions(struct isl_sol
*sol
, struct isl_tab
*tab
)
3873 struct isl_context
*context
;
3876 if (!tab
|| sol
->error
)
3879 context
= sol
->context
;
3883 if (context
->op
->is_empty(context
))
3886 for (r
= 0; r
>= 0 && tab
&& !tab
->empty
; r
= restore_lexmin(tab
)) {
3889 enum isl_tab_row_sign sgn
;
3893 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
3894 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
3896 sgn
= row_sign(tab
, sol
, row
);
3899 tab
->row_sign
[row
] = sgn
;
3900 if (sgn
== isl_tab_row_any
)
3902 if (sgn
== isl_tab_row_any
&& split
== -1)
3904 if (sgn
== isl_tab_row_neg
)
3907 if (row
< tab
->n_row
)
3910 struct isl_vec
*ineq
;
3912 split
= context
->op
->best_split(context
, tab
);
3915 ineq
= get_row_parameter_ineq(tab
, split
);
3919 reset_any_to_unknown(tab
);
3920 tab
->row_sign
[split
] = isl_tab_row_pos
;
3922 find_in_pos(sol
, tab
, ineq
->el
);
3923 tab
->row_sign
[split
] = isl_tab_row_neg
;
3924 isl_seq_neg(ineq
->el
, ineq
->el
, ineq
->size
);
3925 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3927 context
->op
->add_ineq(context
, ineq
->el
, 0, 1);
3935 row
= first_non_integer_row(tab
, &flags
);
3938 if (ISL_FL_ISSET(flags
, I_PAR
)) {
3939 if (ISL_FL_ISSET(flags
, I_VAR
)) {
3940 if (isl_tab_mark_empty(tab
) < 0)
3944 row
= add_cut(tab
, row
);
3945 } else if (ISL_FL_ISSET(flags
, I_VAR
)) {
3946 struct isl_vec
*div
;
3947 struct isl_vec
*ineq
;
3949 div
= get_row_split_div(tab
, row
);
3952 d
= context
->op
->get_div(context
, tab
, div
);
3956 ineq
= ineq_for_div(context
->op
->peek_basic_set(context
), d
);
3960 no_sol_in_strict(sol
, tab
, ineq
);
3961 isl_seq_neg(ineq
->el
, ineq
->el
, ineq
->size
);
3962 context
->op
->add_ineq(context
, ineq
->el
, 1, 1);
3964 if (sol
->error
|| !context
->op
->is_ok(context
))
3966 tab
= set_row_cst_to_div(tab
, row
, d
);
3967 if (context
->op
->is_empty(context
))
3970 row
= add_parametric_cut(tab
, row
, context
);
3985 /* Does "sol" contain a pair of partial solutions that could potentially
3988 * We currently only check that "sol" is not in an error state
3989 * and that there are at least two partial solutions of which the final two
3990 * are defined at the same level.
3992 static int sol_has_mergeable_solutions(struct isl_sol
*sol
)
3998 if (!sol
->partial
->next
)
4000 return sol
->partial
->level
== sol
->partial
->next
->level
;
4003 /* Compute the lexicographic minimum of the set represented by the main
4004 * tableau "tab" within the context "sol->context_tab".
4006 * As a preprocessing step, we first transfer all the purely parametric
4007 * equalities from the main tableau to the context tableau, i.e.,
4008 * parameters that have been pivoted to a row.
4009 * These equalities are ignored by the main algorithm, because the
4010 * corresponding rows may not be marked as being non-negative.
4011 * In parts of the context where the added equality does not hold,
4012 * the main tableau is marked as being empty.
4014 * Before we embark on the actual computation, we save a copy
4015 * of the context. When we return, we check if there are any
4016 * partial solutions that can potentially be merged. If so,
4017 * we perform a rollback to the initial state of the context.
4018 * The merging of partial solutions happens inside calls to
4019 * sol_dec_level that are pushed onto the undo stack of the context.
4020 * If there are no partial solutions that can potentially be merged
4021 * then the rollback is skipped as it would just be wasted effort.
4023 static void find_solutions_main(struct isl_sol
*sol
, struct isl_tab
*tab
)
4033 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
4037 if (tab
->row_var
[row
] < 0)
4039 if (tab
->row_var
[row
] >= tab
->n_param
&&
4040 tab
->row_var
[row
] < tab
->n_var
- tab
->n_div
)
4042 if (tab
->row_var
[row
] < tab
->n_param
)
4043 p
= tab
->row_var
[row
];
4045 p
= tab
->row_var
[row
]
4046 + tab
->n_param
- (tab
->n_var
- tab
->n_div
);
4048 eq
= isl_vec_alloc(tab
->mat
->ctx
, 1+tab
->n_param
+tab
->n_div
);
4051 get_row_parameter_line(tab
, row
, eq
->el
);
4052 isl_int_neg(eq
->el
[1 + p
], tab
->mat
->row
[row
][0]);
4053 eq
= isl_vec_normalize(eq
);
4056 no_sol_in_strict(sol
, tab
, eq
);
4058 isl_seq_neg(eq
->el
, eq
->el
, eq
->size
);
4060 no_sol_in_strict(sol
, tab
, eq
);
4061 isl_seq_neg(eq
->el
, eq
->el
, eq
->size
);
4063 sol
->context
->op
->add_eq(sol
->context
, eq
->el
, 1, 1);
4067 if (isl_tab_mark_redundant(tab
, row
) < 0)
4070 if (sol
->context
->op
->is_empty(sol
->context
))
4073 row
= tab
->n_redundant
- 1;
4076 saved
= sol
->context
->op
->save(sol
->context
);
4078 find_solutions(sol
, tab
);
4080 if (sol_has_mergeable_solutions(sol
))
4081 sol
->context
->op
->restore(sol
->context
, saved
);
4083 sol
->context
->op
->discard(saved
);
4094 /* Check if integer division "div" of "dom" also occurs in "bmap".
4095 * If so, return its position within the divs.
4096 * If not, return -1.
4098 static int find_context_div(struct isl_basic_map
*bmap
,
4099 struct isl_basic_set
*dom
, unsigned div
)
4102 unsigned b_dim
= isl_space_dim(bmap
->dim
, isl_dim_all
);
4103 unsigned d_dim
= isl_space_dim(dom
->dim
, isl_dim_all
);
4105 if (isl_int_is_zero(dom
->div
[div
][0]))
4107 if (isl_seq_first_non_zero(dom
->div
[div
] + 2 + d_dim
, dom
->n_div
) != -1)
4110 for (i
= 0; i
< bmap
->n_div
; ++i
) {
4111 if (isl_int_is_zero(bmap
->div
[i
][0]))
4113 if (isl_seq_first_non_zero(bmap
->div
[i
] + 2 + d_dim
,
4114 (b_dim
- d_dim
) + bmap
->n_div
) != -1)
4116 if (isl_seq_eq(bmap
->div
[i
], dom
->div
[div
], 2 + d_dim
))
4122 /* The correspondence between the variables in the main tableau,
4123 * the context tableau, and the input map and domain is as follows.
4124 * The first n_param and the last n_div variables of the main tableau
4125 * form the variables of the context tableau.
4126 * In the basic map, these n_param variables correspond to the
4127 * parameters and the input dimensions. In the domain, they correspond
4128 * to the parameters and the set dimensions.
4129 * The n_div variables correspond to the integer divisions in the domain.
4130 * To ensure that everything lines up, we may need to copy some of the
4131 * integer divisions of the domain to the map. These have to be placed
4132 * in the same order as those in the context and they have to be placed
4133 * after any other integer divisions that the map may have.
4134 * This function performs the required reordering.
4136 static struct isl_basic_map
*align_context_divs(struct isl_basic_map
*bmap
,
4137 struct isl_basic_set
*dom
)
4143 for (i
= 0; i
< dom
->n_div
; ++i
)
4144 if (find_context_div(bmap
, dom
, i
) != -1)
4146 other
= bmap
->n_div
- common
;
4147 if (dom
->n_div
- common
> 0) {
4148 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
4149 dom
->n_div
- common
, 0, 0);
4153 for (i
= 0; i
< dom
->n_div
; ++i
) {
4154 int pos
= find_context_div(bmap
, dom
, i
);
4156 pos
= isl_basic_map_alloc_div(bmap
);
4159 isl_int_set_si(bmap
->div
[pos
][0], 0);
4161 if (pos
!= other
+ i
)
4162 isl_basic_map_swap_div(bmap
, pos
, other
+ i
);
4166 isl_basic_map_free(bmap
);
4170 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
4171 * some obvious symmetries.
4173 * We make sure the divs in the domain are properly ordered,
4174 * because they will be added one by one in the given order
4175 * during the construction of the solution map.
4176 * Furthermore, make sure that the known integer divisions
4177 * appear before any unknown integer division because the solution
4178 * may depend on the known integer divisions, while anything that
4179 * depends on any variable starting from the first unknown integer
4180 * division is ignored in sol_pma_add.
4182 static struct isl_sol
*basic_map_partial_lexopt_base_sol(
4183 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
4184 __isl_give isl_set
**empty
, int max
,
4185 struct isl_sol
*(*init
)(__isl_keep isl_basic_map
*bmap
,
4186 __isl_take isl_basic_set
*dom
, int track_empty
, int max
))
4188 struct isl_tab
*tab
;
4189 struct isl_sol
*sol
= NULL
;
4190 struct isl_context
*context
;
4193 dom
= isl_basic_set_sort_divs(dom
);
4194 bmap
= align_context_divs(bmap
, dom
);
4196 sol
= init(bmap
, dom
, !!empty
, max
);
4200 context
= sol
->context
;
4201 if (isl_basic_set_plain_is_empty(context
->op
->peek_basic_set(context
)))
4203 else if (isl_basic_map_plain_is_empty(bmap
)) {
4206 isl_basic_set_copy(context
->op
->peek_basic_set(context
)));
4208 tab
= tab_for_lexmin(bmap
,
4209 context
->op
->peek_basic_set(context
), 1, max
);
4210 tab
= context
->op
->detect_nonnegative_parameters(context
, tab
);
4211 find_solutions_main(sol
, tab
);
4216 isl_basic_map_free(bmap
);
4220 isl_basic_map_free(bmap
);
4224 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
4225 * some obvious symmetries.
4227 * We call basic_map_partial_lexopt_base_sol and extract the results.
4229 static __isl_give isl_map
*basic_map_partial_lexopt_base(
4230 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
4231 __isl_give isl_set
**empty
, int max
)
4233 isl_map
*result
= NULL
;
4234 struct isl_sol
*sol
;
4235 struct isl_sol_map
*sol_map
;
4237 sol
= basic_map_partial_lexopt_base_sol(bmap
, dom
, empty
, max
,
4241 sol_map
= (struct isl_sol_map
*) sol
;
4243 result
= isl_map_copy(sol_map
->map
);
4245 *empty
= isl_set_copy(sol_map
->empty
);
4246 sol_free(&sol_map
->sol
);
4250 /* Return a count of the number of occurrences of the "n" first
4251 * variables in the inequality constraints of "bmap".
4253 static __isl_give
int *count_occurrences(__isl_keep isl_basic_map
*bmap
,
4262 ctx
= isl_basic_map_get_ctx(bmap
);
4263 occurrences
= isl_calloc_array(ctx
, int, n
);
4267 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
4268 for (j
= 0; j
< n
; ++j
) {
4269 if (!isl_int_is_zero(bmap
->ineq
[i
][1 + j
]))
4277 /* Do all of the "n" variables with non-zero coefficients in "c"
4278 * occur in exactly a single constraint.
4279 * "occurrences" is an array of length "n" containing the number
4280 * of occurrences of each of the variables in the inequality constraints.
4282 static int single_occurrence(int n
, isl_int
*c
, int *occurrences
)
4286 for (i
= 0; i
< n
; ++i
) {
4287 if (isl_int_is_zero(c
[i
]))
4289 if (occurrences
[i
] != 1)
4296 /* Do all of the "n" initial variables that occur in inequality constraint
4297 * "ineq" of "bmap" only occur in that constraint?
4299 static int all_single_occurrence(__isl_keep isl_basic_map
*bmap
, int ineq
,
4304 for (i
= 0; i
< n
; ++i
) {
4305 if (isl_int_is_zero(bmap
->ineq
[ineq
][1 + i
]))
4307 for (j
= 0; j
< bmap
->n_ineq
; ++j
) {
4310 if (!isl_int_is_zero(bmap
->ineq
[j
][1 + i
]))
4318 /* Structure used during detection of parallel constraints.
4319 * n_in: number of "input" variables: isl_dim_param + isl_dim_in
4320 * n_out: number of "output" variables: isl_dim_out + isl_dim_div
4321 * val: the coefficients of the output variables
4323 struct isl_constraint_equal_info
{
4324 isl_basic_map
*bmap
;
4330 /* Check whether the coefficients of the output variables
4331 * of the constraint in "entry" are equal to info->val.
4333 static int constraint_equal(const void *entry
, const void *val
)
4335 isl_int
**row
= (isl_int
**)entry
;
4336 const struct isl_constraint_equal_info
*info
= val
;
4338 return isl_seq_eq((*row
) + 1 + info
->n_in
, info
->val
, info
->n_out
);
4341 /* Check whether "bmap" has a pair of constraints that have
4342 * the same coefficients for the output variables.
4343 * Note that the coefficients of the existentially quantified
4344 * variables need to be zero since the existentially quantified
4345 * of the result are usually not the same as those of the input.
4346 * Furthermore, check that each of the input variables that occur
4347 * in those constraints does not occur in any other constraint.
4348 * If so, return 1 and return the row indices of the two constraints
4349 * in *first and *second.
4351 static int parallel_constraints(__isl_keep isl_basic_map
*bmap
,
4352 int *first
, int *second
)
4356 int *occurrences
= NULL
;
4357 struct isl_hash_table
*table
= NULL
;
4358 struct isl_hash_table_entry
*entry
;
4359 struct isl_constraint_equal_info info
;
4363 ctx
= isl_basic_map_get_ctx(bmap
);
4364 table
= isl_hash_table_alloc(ctx
, bmap
->n_ineq
);
4368 info
.n_in
= isl_basic_map_dim(bmap
, isl_dim_param
) +
4369 isl_basic_map_dim(bmap
, isl_dim_in
);
4370 occurrences
= count_occurrences(bmap
, info
.n_in
);
4371 if (info
.n_in
&& !occurrences
)
4374 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4375 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4376 info
.n_out
= n_out
+ n_div
;
4377 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
4380 info
.val
= bmap
->ineq
[i
] + 1 + info
.n_in
;
4381 if (isl_seq_first_non_zero(info
.val
, n_out
) < 0)
4383 if (isl_seq_first_non_zero(info
.val
+ n_out
, n_div
) >= 0)
4385 if (!single_occurrence(info
.n_in
, bmap
->ineq
[i
] + 1,
4388 hash
= isl_seq_get_hash(info
.val
, info
.n_out
);
4389 entry
= isl_hash_table_find(ctx
, table
, hash
,
4390 constraint_equal
, &info
, 1);
4395 entry
->data
= &bmap
->ineq
[i
];
4398 if (i
< bmap
->n_ineq
) {
4399 *first
= ((isl_int
**)entry
->data
) - bmap
->ineq
;
4403 isl_hash_table_free(ctx
, table
);
4406 return i
< bmap
->n_ineq
;
4408 isl_hash_table_free(ctx
, table
);
4413 /* Given a set of upper bounds in "var", add constraints to "bset"
4414 * that make the i-th bound smallest.
4416 * In particular, if there are n bounds b_i, then add the constraints
4418 * b_i <= b_j for j > i
4419 * b_i < b_j for j < i
4421 static __isl_give isl_basic_set
*select_minimum(__isl_take isl_basic_set
*bset
,
4422 __isl_keep isl_mat
*var
, int i
)
4427 ctx
= isl_mat_get_ctx(var
);
4429 for (j
= 0; j
< var
->n_row
; ++j
) {
4432 k
= isl_basic_set_alloc_inequality(bset
);
4435 isl_seq_combine(bset
->ineq
[k
], ctx
->one
, var
->row
[j
],
4436 ctx
->negone
, var
->row
[i
], var
->n_col
);
4437 isl_int_set_si(bset
->ineq
[k
][var
->n_col
], 0);
4439 isl_int_sub_ui(bset
->ineq
[k
][0], bset
->ineq
[k
][0], 1);
4442 bset
= isl_basic_set_finalize(bset
);
4446 isl_basic_set_free(bset
);
4450 /* Given a set of upper bounds on the last "input" variable m,
4451 * construct a set that assigns the minimal upper bound to m, i.e.,
4452 * construct a set that divides the space into cells where one
4453 * of the upper bounds is smaller than all the others and assign
4454 * this upper bound to m.
4456 * In particular, if there are n bounds b_i, then the result
4457 * consists of n basic sets, each one of the form
4460 * b_i <= b_j for j > i
4461 * b_i < b_j for j < i
4463 static __isl_give isl_set
*set_minimum(__isl_take isl_space
*dim
,
4464 __isl_take isl_mat
*var
)
4467 isl_basic_set
*bset
= NULL
;
4468 isl_set
*set
= NULL
;
4473 set
= isl_set_alloc_space(isl_space_copy(dim
),
4474 var
->n_row
, ISL_SET_DISJOINT
);
4476 for (i
= 0; i
< var
->n_row
; ++i
) {
4477 bset
= isl_basic_set_alloc_space(isl_space_copy(dim
), 0,
4479 k
= isl_basic_set_alloc_equality(bset
);
4482 isl_seq_cpy(bset
->eq
[k
], var
->row
[i
], var
->n_col
);
4483 isl_int_set_si(bset
->eq
[k
][var
->n_col
], -1);
4484 bset
= select_minimum(bset
, var
, i
);
4485 set
= isl_set_add_basic_set(set
, bset
);
4488 isl_space_free(dim
);
4492 isl_basic_set_free(bset
);
4494 isl_space_free(dim
);
4499 /* Given that the last input variable of "bmap" represents the minimum
4500 * of the bounds in "cst", check whether we need to split the domain
4501 * based on which bound attains the minimum.
4503 * A split is needed when the minimum appears in an integer division
4504 * or in an equality. Otherwise, it is only needed if it appears in
4505 * an upper bound that is different from the upper bounds on which it
4508 static int need_split_basic_map(__isl_keep isl_basic_map
*bmap
,
4509 __isl_keep isl_mat
*cst
)
4515 pos
= cst
->n_col
- 1;
4516 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4518 for (i
= 0; i
< bmap
->n_div
; ++i
)
4519 if (!isl_int_is_zero(bmap
->div
[i
][2 + pos
]))
4522 for (i
= 0; i
< bmap
->n_eq
; ++i
)
4523 if (!isl_int_is_zero(bmap
->eq
[i
][1 + pos
]))
4526 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
4527 if (isl_int_is_nonneg(bmap
->ineq
[i
][1 + pos
]))
4529 if (!isl_int_is_negone(bmap
->ineq
[i
][1 + pos
]))
4531 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + pos
+ 1,
4532 total
- pos
- 1) >= 0)
4535 for (j
= 0; j
< cst
->n_row
; ++j
)
4536 if (isl_seq_eq(bmap
->ineq
[i
], cst
->row
[j
], cst
->n_col
))
4538 if (j
>= cst
->n_row
)
4545 /* Given that the last set variable of "bset" represents the minimum
4546 * of the bounds in "cst", check whether we need to split the domain
4547 * based on which bound attains the minimum.
4549 * We simply call need_split_basic_map here. This is safe because
4550 * the position of the minimum is computed from "cst" and not
4553 static int need_split_basic_set(__isl_keep isl_basic_set
*bset
,
4554 __isl_keep isl_mat
*cst
)
4556 return need_split_basic_map(bset_to_bmap(bset
), cst
);
4559 /* Given that the last set variable of "set" represents the minimum
4560 * of the bounds in "cst", check whether we need to split the domain
4561 * based on which bound attains the minimum.
4563 static int need_split_set(__isl_keep isl_set
*set
, __isl_keep isl_mat
*cst
)
4567 for (i
= 0; i
< set
->n
; ++i
)
4568 if (need_split_basic_set(set
->p
[i
], cst
))
4574 /* Given a set of which the last set variable is the minimum
4575 * of the bounds in "cst", split each basic set in the set
4576 * in pieces where one of the bounds is (strictly) smaller than the others.
4577 * This subdivision is given in "min_expr".
4578 * The variable is subsequently projected out.
4580 * We only do the split when it is needed.
4581 * For example if the last input variable m = min(a,b) and the only
4582 * constraints in the given basic set are lower bounds on m,
4583 * i.e., l <= m = min(a,b), then we can simply project out m
4584 * to obtain l <= a and l <= b, without having to split on whether
4585 * m is equal to a or b.
4587 static __isl_give isl_set
*split(__isl_take isl_set
*empty
,
4588 __isl_take isl_set
*min_expr
, __isl_take isl_mat
*cst
)
4595 if (!empty
|| !min_expr
|| !cst
)
4598 n_in
= isl_set_dim(empty
, isl_dim_set
);
4599 dim
= isl_set_get_space(empty
);
4600 dim
= isl_space_drop_dims(dim
, isl_dim_set
, n_in
- 1, 1);
4601 res
= isl_set_empty(dim
);
4603 for (i
= 0; i
< empty
->n
; ++i
) {
4606 set
= isl_set_from_basic_set(isl_basic_set_copy(empty
->p
[i
]));
4607 if (need_split_basic_set(empty
->p
[i
], cst
))
4608 set
= isl_set_intersect(set
, isl_set_copy(min_expr
));
4609 set
= isl_set_remove_dims(set
, isl_dim_set
, n_in
- 1, 1);
4611 res
= isl_set_union_disjoint(res
, set
);
4614 isl_set_free(empty
);
4615 isl_set_free(min_expr
);
4619 isl_set_free(empty
);
4620 isl_set_free(min_expr
);
4625 /* Given a map of which the last input variable is the minimum
4626 * of the bounds in "cst", split each basic set in the set
4627 * in pieces where one of the bounds is (strictly) smaller than the others.
4628 * This subdivision is given in "min_expr".
4629 * The variable is subsequently projected out.
4631 * The implementation is essentially the same as that of "split".
4633 static __isl_give isl_map
*split_domain(__isl_take isl_map
*opt
,
4634 __isl_take isl_set
*min_expr
, __isl_take isl_mat
*cst
)
4641 if (!opt
|| !min_expr
|| !cst
)
4644 n_in
= isl_map_dim(opt
, isl_dim_in
);
4645 dim
= isl_map_get_space(opt
);
4646 dim
= isl_space_drop_dims(dim
, isl_dim_in
, n_in
- 1, 1);
4647 res
= isl_map_empty(dim
);
4649 for (i
= 0; i
< opt
->n
; ++i
) {
4652 map
= isl_map_from_basic_map(isl_basic_map_copy(opt
->p
[i
]));
4653 if (need_split_basic_map(opt
->p
[i
], cst
))
4654 map
= isl_map_intersect_domain(map
,
4655 isl_set_copy(min_expr
));
4656 map
= isl_map_remove_dims(map
, isl_dim_in
, n_in
- 1, 1);
4658 res
= isl_map_union_disjoint(res
, map
);
4662 isl_set_free(min_expr
);
4667 isl_set_free(min_expr
);
4672 static __isl_give isl_map
*basic_map_partial_lexopt(
4673 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
4674 __isl_give isl_set
**empty
, int max
);
4676 /* This function is called from basic_map_partial_lexopt_symm.
4677 * The last variable of "bmap" and "dom" corresponds to the minimum
4678 * of the bounds in "cst". "map_space" is the space of the original
4679 * input relation (of basic_map_partial_lexopt_symm) and "set_space"
4680 * is the space of the original domain.
4682 * We recursively call basic_map_partial_lexopt and then plug in
4683 * the definition of the minimum in the result.
4685 static __isl_give isl_map
*basic_map_partial_lexopt_symm_core(
4686 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
4687 __isl_give isl_set
**empty
, int max
, __isl_take isl_mat
*cst
,
4688 __isl_take isl_space
*map_space
, __isl_take isl_space
*set_space
)
4693 min_expr
= set_minimum(isl_basic_set_get_space(dom
), isl_mat_copy(cst
));
4695 opt
= basic_map_partial_lexopt(bmap
, dom
, empty
, max
);
4698 *empty
= split(*empty
,
4699 isl_set_copy(min_expr
), isl_mat_copy(cst
));
4700 *empty
= isl_set_reset_space(*empty
, set_space
);
4703 opt
= split_domain(opt
, min_expr
, cst
);
4704 opt
= isl_map_reset_space(opt
, map_space
);
4709 /* Extract a domain from "bmap" for the purpose of computing
4710 * a lexicographic optimum.
4712 * This function is only called when the caller wants to compute a full
4713 * lexicographic optimum, i.e., without specifying a domain. In this case,
4714 * the caller is not interested in the part of the domain space where
4715 * there is no solution and the domain can be initialized to those constraints
4716 * of "bmap" that only involve the parameters and the input dimensions.
4717 * This relieves the parametric programming engine from detecting those
4718 * inequalities and transferring them to the context. More importantly,
4719 * it ensures that those inequalities are transferred first and not
4720 * intermixed with inequalities that actually split the domain.
4722 * If the caller does not require the absence of existentially quantified
4723 * variables in the result (i.e., if ISL_OPT_QE is not set in "flags"),
4724 * then the actual domain of "bmap" can be used. This ensures that
4725 * the domain does not need to be split at all just to separate out
4726 * pieces of the domain that do not have a solution from piece that do.
4727 * This domain cannot be used in general because it may involve
4728 * (unknown) existentially quantified variables which will then also
4729 * appear in the solution.
4731 static __isl_give isl_basic_set
*extract_domain(__isl_keep isl_basic_map
*bmap
,
4737 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4738 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4739 bmap
= isl_basic_map_copy(bmap
);
4740 if (ISL_FL_ISSET(flags
, ISL_OPT_QE
)) {
4741 bmap
= isl_basic_map_drop_constraints_involving_dims(bmap
,
4742 isl_dim_div
, 0, n_div
);
4743 bmap
= isl_basic_map_drop_constraints_involving_dims(bmap
,
4744 isl_dim_out
, 0, n_out
);
4746 return isl_basic_map_domain(bmap
);
4750 #define TYPE isl_map
4753 #include "isl_tab_lexopt_templ.c"
4755 struct isl_sol_for
{
4757 int (*fn
)(__isl_take isl_basic_set
*dom
,
4758 __isl_take isl_aff_list
*list
, void *user
);
4762 static void sol_for_free(struct isl_sol_for
*sol_for
)
4766 if (sol_for
->sol
.context
)
4767 sol_for
->sol
.context
->op
->free(sol_for
->sol
.context
);
4771 static void sol_for_free_wrap(struct isl_sol
*sol
)
4773 sol_for_free((struct isl_sol_for
*)sol
);
4776 /* Add the solution identified by the tableau and the context tableau.
4778 * See documentation of sol_add for more details.
4780 * Instead of constructing a basic map, this function calls a user
4781 * defined function with the current context as a basic set and
4782 * a list of affine expressions representing the relation between
4783 * the input and output. The space over which the affine expressions
4784 * are defined is the same as that of the domain. The number of
4785 * affine expressions in the list is equal to the number of output variables.
4787 static void sol_for_add(struct isl_sol_for
*sol
,
4788 struct isl_basic_set
*dom
, struct isl_mat
*M
)
4792 isl_local_space
*ls
;
4796 if (sol
->sol
.error
|| !dom
|| !M
)
4799 ctx
= isl_basic_set_get_ctx(dom
);
4800 ls
= isl_basic_set_get_local_space(dom
);
4801 list
= isl_aff_list_alloc(ctx
, M
->n_row
- 1);
4802 for (i
= 1; i
< M
->n_row
; ++i
) {
4803 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
4805 isl_int_set(aff
->v
->el
[0], M
->row
[0][0]);
4806 isl_seq_cpy(aff
->v
->el
+ 1, M
->row
[i
], M
->n_col
);
4808 aff
= isl_aff_normalize(aff
);
4809 list
= isl_aff_list_add(list
, aff
);
4811 isl_local_space_free(ls
);
4813 dom
= isl_basic_set_finalize(dom
);
4815 if (sol
->fn(isl_basic_set_copy(dom
), list
, sol
->user
) < 0)
4818 isl_basic_set_free(dom
);
4822 isl_basic_set_free(dom
);
4827 static void sol_for_add_wrap(struct isl_sol
*sol
,
4828 struct isl_basic_set
*dom
, struct isl_mat
*M
)
4830 sol_for_add((struct isl_sol_for
*)sol
, dom
, M
);
4833 static struct isl_sol_for
*sol_for_init(struct isl_basic_map
*bmap
, int max
,
4834 int (*fn
)(__isl_take isl_basic_set
*dom
, __isl_take isl_aff_list
*list
,
4838 struct isl_sol_for
*sol_for
= NULL
;
4840 struct isl_basic_set
*dom
= NULL
;
4842 sol_for
= isl_calloc_type(bmap
->ctx
, struct isl_sol_for
);
4846 dom_dim
= isl_space_domain(isl_space_copy(bmap
->dim
));
4847 dom
= isl_basic_set_universe(dom_dim
);
4849 sol_for
->sol
.rational
= ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
4850 sol_for
->sol
.dec_level
.callback
.run
= &sol_dec_level_wrap
;
4851 sol_for
->sol
.dec_level
.sol
= &sol_for
->sol
;
4853 sol_for
->user
= user
;
4854 sol_for
->sol
.max
= max
;
4855 sol_for
->sol
.n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4856 sol_for
->sol
.add
= &sol_for_add_wrap
;
4857 sol_for
->sol
.add_empty
= NULL
;
4858 sol_for
->sol
.free
= &sol_for_free_wrap
;
4860 sol_for
->sol
.context
= isl_context_alloc(dom
);
4861 if (!sol_for
->sol
.context
)
4864 isl_basic_set_free(dom
);
4867 isl_basic_set_free(dom
);
4868 sol_for_free(sol_for
);
4872 static void sol_for_find_solutions(struct isl_sol_for
*sol_for
,
4873 struct isl_tab
*tab
)
4875 find_solutions_main(&sol_for
->sol
, tab
);
4878 int isl_basic_map_foreach_lexopt(__isl_keep isl_basic_map
*bmap
, int max
,
4879 int (*fn
)(__isl_take isl_basic_set
*dom
, __isl_take isl_aff_list
*list
,
4883 struct isl_sol_for
*sol_for
= NULL
;
4885 bmap
= isl_basic_map_copy(bmap
);
4886 bmap
= isl_basic_map_detect_equalities(bmap
);
4890 sol_for
= sol_for_init(bmap
, max
, fn
, user
);
4894 if (isl_basic_map_plain_is_empty(bmap
))
4897 struct isl_tab
*tab
;
4898 struct isl_context
*context
= sol_for
->sol
.context
;
4899 tab
= tab_for_lexmin(bmap
,
4900 context
->op
->peek_basic_set(context
), 1, max
);
4901 tab
= context
->op
->detect_nonnegative_parameters(context
, tab
);
4902 sol_for_find_solutions(sol_for
, tab
);
4903 if (sol_for
->sol
.error
)
4907 sol_free(&sol_for
->sol
);
4908 isl_basic_map_free(bmap
);
4911 sol_free(&sol_for
->sol
);
4912 isl_basic_map_free(bmap
);
4916 int isl_basic_set_foreach_lexopt(__isl_keep isl_basic_set
*bset
, int max
,
4917 int (*fn
)(__isl_take isl_basic_set
*dom
, __isl_take isl_aff_list
*list
,
4921 return isl_basic_map_foreach_lexopt(bset
, max
, fn
, user
);
4924 /* Check if the given sequence of len variables starting at pos
4925 * represents a trivial (i.e., zero) solution.
4926 * The variables are assumed to be non-negative and to come in pairs,
4927 * with each pair representing a variable of unrestricted sign.
4928 * The solution is trivial if each such pair in the sequence consists
4929 * of two identical values, meaning that the variable being represented
4932 static int region_is_trivial(struct isl_tab
*tab
, int pos
, int len
)
4939 for (i
= 0; i
< len
; i
+= 2) {
4943 neg_row
= tab
->var
[pos
+ i
].is_row
?
4944 tab
->var
[pos
+ i
].index
: -1;
4945 pos_row
= tab
->var
[pos
+ i
+ 1].is_row
?
4946 tab
->var
[pos
+ i
+ 1].index
: -1;
4949 isl_int_is_zero(tab
->mat
->row
[neg_row
][1])) &&
4951 isl_int_is_zero(tab
->mat
->row
[pos_row
][1])))
4954 if (neg_row
< 0 || pos_row
< 0)
4956 if (isl_int_ne(tab
->mat
->row
[neg_row
][1],
4957 tab
->mat
->row
[pos_row
][1]))
4964 /* Return the index of the first trivial region or -1 if all regions
4967 static int first_trivial_region(struct isl_tab
*tab
,
4968 int n_region
, struct isl_region
*region
)
4972 for (i
= 0; i
< n_region
; ++i
) {
4973 if (region_is_trivial(tab
, region
[i
].pos
, region
[i
].len
))
4980 /* Check if the solution is optimal, i.e., whether the first
4981 * n_op entries are zero.
4983 static int is_optimal(__isl_keep isl_vec
*sol
, int n_op
)
4987 for (i
= 0; i
< n_op
; ++i
)
4988 if (!isl_int_is_zero(sol
->el
[1 + i
]))
4993 /* Add constraints to "tab" that ensure that any solution is significantly
4994 * better than that represented by "sol". That is, find the first
4995 * relevant (within first n_op) non-zero coefficient and force it (along
4996 * with all previous coefficients) to be zero.
4997 * If the solution is already optimal (all relevant coefficients are zero),
4998 * then just mark the table as empty.
5000 * This function assumes that at least 2 * n_op more rows and at least
5001 * 2 * n_op more elements in the constraint array are available in the tableau.
5003 static int force_better_solution(struct isl_tab
*tab
,
5004 __isl_keep isl_vec
*sol
, int n_op
)
5013 for (i
= 0; i
< n_op
; ++i
)
5014 if (!isl_int_is_zero(sol
->el
[1 + i
]))
5018 if (isl_tab_mark_empty(tab
) < 0)
5023 ctx
= isl_vec_get_ctx(sol
);
5024 v
= isl_vec_alloc(ctx
, 1 + tab
->n_var
);
5028 for (; i
>= 0; --i
) {
5030 isl_int_set_si(v
->el
[1 + i
], -1);
5031 if (add_lexmin_eq(tab
, v
->el
) < 0)
5042 struct isl_trivial
{
5046 struct isl_tab_undo
*snap
;
5049 /* Return the lexicographically smallest non-trivial solution of the
5050 * given ILP problem.
5052 * All variables are assumed to be non-negative.
5054 * n_op is the number of initial coordinates to optimize.
5055 * That is, once a solution has been found, we will only continue looking
5056 * for solution that result in significantly better values for those
5057 * initial coordinates. That is, we only continue looking for solutions
5058 * that increase the number of initial zeros in this sequence.
5060 * A solution is non-trivial, if it is non-trivial on each of the
5061 * specified regions. Each region represents a sequence of pairs
5062 * of variables. A solution is non-trivial on such a region if
5063 * at least one of these pairs consists of different values, i.e.,
5064 * such that the non-negative variable represented by the pair is non-zero.
5066 * Whenever a conflict is encountered, all constraints involved are
5067 * reported to the caller through a call to "conflict".
5069 * We perform a simple branch-and-bound backtracking search.
5070 * Each level in the search represents initially trivial region that is forced
5071 * to be non-trivial.
5072 * At each level we consider n cases, where n is the length of the region.
5073 * In terms of the n/2 variables of unrestricted signs being encoded by
5074 * the region, we consider the cases
5077 * x_0 = 0 and x_1 >= 1
5078 * x_0 = 0 and x_1 <= -1
5079 * x_0 = 0 and x_1 = 0 and x_2 >= 1
5080 * x_0 = 0 and x_1 = 0 and x_2 <= -1
5082 * The cases are considered in this order, assuming that each pair
5083 * x_i_a x_i_b represents the value x_i_b - x_i_a.
5084 * That is, x_0 >= 1 is enforced by adding the constraint
5085 * x_0_b - x_0_a >= 1
5087 __isl_give isl_vec
*isl_tab_basic_set_non_trivial_lexmin(
5088 __isl_take isl_basic_set
*bset
, int n_op
, int n_region
,
5089 struct isl_region
*region
,
5090 int (*conflict
)(int con
, void *user
), void *user
)
5096 isl_vec
*sol
= NULL
;
5097 struct isl_tab
*tab
;
5098 struct isl_trivial
*triv
= NULL
;
5104 ctx
= isl_basic_set_get_ctx(bset
);
5105 sol
= isl_vec_alloc(ctx
, 0);
5107 tab
= tab_for_lexmin(bset
, NULL
, 0, 0);
5110 tab
->conflict
= conflict
;
5111 tab
->conflict_user
= user
;
5113 v
= isl_vec_alloc(ctx
, 1 + tab
->n_var
);
5114 triv
= isl_calloc_array(ctx
, struct isl_trivial
, n_region
);
5115 if (!v
|| (n_region
&& !triv
))
5121 while (level
>= 0) {
5125 tab
= cut_to_integer_lexmin(tab
, CUT_ONE
);
5130 r
= first_trivial_region(tab
, n_region
, region
);
5132 for (i
= 0; i
< level
; ++i
)
5135 sol
= isl_tab_get_sample_value(tab
);
5138 if (is_optimal(sol
, n_op
))
5142 if (level
>= n_region
)
5143 isl_die(ctx
, isl_error_internal
,
5144 "nesting level too deep", goto error
);
5145 if (isl_tab_extend_cons(tab
,
5146 2 * region
[r
].len
+ 2 * n_op
) < 0)
5148 triv
[level
].region
= r
;
5149 triv
[level
].side
= 0;
5152 r
= triv
[level
].region
;
5153 side
= triv
[level
].side
;
5154 base
= 2 * (side
/2);
5156 if (side
>= region
[r
].len
) {
5161 if (isl_tab_rollback(tab
, triv
[level
].snap
) < 0)
5166 if (triv
[level
].update
) {
5167 if (force_better_solution(tab
, sol
, n_op
) < 0)
5169 triv
[level
].update
= 0;
5172 if (side
== base
&& base
>= 2) {
5173 for (j
= base
- 2; j
< base
; ++j
) {
5175 isl_int_set_si(v
->el
[1 + region
[r
].pos
+ j
], 1);
5176 if (add_lexmin_eq(tab
, v
->el
) < 0)
5181 triv
[level
].snap
= isl_tab_snap(tab
);
5182 if (isl_tab_push_basis(tab
) < 0)
5186 isl_int_set_si(v
->el
[0], -1);
5187 isl_int_set_si(v
->el
[1 + region
[r
].pos
+ side
], -1);
5188 isl_int_set_si(v
->el
[1 + region
[r
].pos
+ (side
^ 1)], 1);
5189 tab
= add_lexmin_ineq(tab
, v
->el
);
5199 isl_basic_set_free(bset
);
5206 isl_basic_set_free(bset
);
5211 /* Wrapper for a tableau that is used for computing
5212 * the lexicographically smallest rational point of a non-negative set.
5213 * This point is represented by the sample value of "tab",
5214 * unless "tab" is empty.
5216 struct isl_tab_lexmin
{
5218 struct isl_tab
*tab
;
5221 /* Free "tl" and return NULL.
5223 __isl_null isl_tab_lexmin
*isl_tab_lexmin_free(__isl_take isl_tab_lexmin
*tl
)
5227 isl_ctx_deref(tl
->ctx
);
5228 isl_tab_free(tl
->tab
);
5234 /* Construct an isl_tab_lexmin for computing
5235 * the lexicographically smallest rational point in "bset",
5236 * assuming that all variables are non-negative.
5238 __isl_give isl_tab_lexmin
*isl_tab_lexmin_from_basic_set(
5239 __isl_take isl_basic_set
*bset
)
5247 ctx
= isl_basic_set_get_ctx(bset
);
5248 tl
= isl_calloc_type(ctx
, struct isl_tab_lexmin
);
5253 tl
->tab
= tab_for_lexmin(bset
, NULL
, 0, 0);
5254 isl_basic_set_free(bset
);
5256 return isl_tab_lexmin_free(tl
);
5259 isl_basic_set_free(bset
);
5260 isl_tab_lexmin_free(tl
);
5264 /* Return the dimension of the set represented by "tl".
5266 int isl_tab_lexmin_dim(__isl_keep isl_tab_lexmin
*tl
)
5268 return tl
? tl
->tab
->n_var
: -1;
5271 /* Add the equality with coefficients "eq" to "tl", updating the optimal
5272 * solution if needed.
5273 * The equality is added as two opposite inequality constraints.
5275 __isl_give isl_tab_lexmin
*isl_tab_lexmin_add_eq(__isl_take isl_tab_lexmin
*tl
,
5281 return isl_tab_lexmin_free(tl
);
5283 if (isl_tab_extend_cons(tl
->tab
, 2) < 0)
5284 return isl_tab_lexmin_free(tl
);
5285 n_var
= tl
->tab
->n_var
;
5286 isl_seq_neg(eq
, eq
, 1 + n_var
);
5287 tl
->tab
= add_lexmin_ineq(tl
->tab
, eq
);
5288 isl_seq_neg(eq
, eq
, 1 + n_var
);
5289 tl
->tab
= add_lexmin_ineq(tl
->tab
, eq
);
5292 return isl_tab_lexmin_free(tl
);
5297 /* Return the lexicographically smallest rational point in the basic set
5298 * from which "tl" was constructed.
5299 * If the original input was empty, then return a zero-length vector.
5301 __isl_give isl_vec
*isl_tab_lexmin_get_solution(__isl_keep isl_tab_lexmin
*tl
)
5306 return isl_vec_alloc(tl
->ctx
, 0);
5308 return isl_tab_get_sample_value(tl
->tab
);
5311 /* Return the lexicographically smallest rational point in "bset",
5312 * assuming that all variables are non-negative.
5313 * If "bset" is empty, then return a zero-length vector.
5315 __isl_give isl_vec
*isl_tab_basic_set_non_neg_lexmin(
5316 __isl_take isl_basic_set
*bset
)
5321 tl
= isl_tab_lexmin_from_basic_set(bset
);
5322 sol
= isl_tab_lexmin_get_solution(tl
);
5323 isl_tab_lexmin_free(tl
);
5327 struct isl_sol_pma
{
5329 isl_pw_multi_aff
*pma
;
5333 static void sol_pma_free(struct isl_sol_pma
*sol_pma
)
5337 if (sol_pma
->sol
.context
)
5338 sol_pma
->sol
.context
->op
->free(sol_pma
->sol
.context
);
5339 isl_pw_multi_aff_free(sol_pma
->pma
);
5340 isl_set_free(sol_pma
->empty
);
5344 /* This function is called for parts of the context where there is
5345 * no solution, with "bset" corresponding to the context tableau.
5346 * Simply add the basic set to the set "empty".
5348 static void sol_pma_add_empty(struct isl_sol_pma
*sol
,
5349 __isl_take isl_basic_set
*bset
)
5351 if (!bset
|| !sol
->empty
)
5354 sol
->empty
= isl_set_grow(sol
->empty
, 1);
5355 bset
= isl_basic_set_simplify(bset
);
5356 bset
= isl_basic_set_finalize(bset
);
5357 sol
->empty
= isl_set_add_basic_set(sol
->empty
, bset
);
5362 isl_basic_set_free(bset
);
5366 /* Check that the final columns of "M", starting at "first", are zero.
5368 static isl_stat
check_final_columns_are_zero(__isl_keep isl_mat
*M
,
5372 unsigned rows
, cols
, n
;
5375 return isl_stat_error
;
5376 rows
= isl_mat_rows(M
);
5377 cols
= isl_mat_cols(M
);
5379 for (i
= 0; i
< rows
; ++i
)
5380 if (isl_seq_first_non_zero(M
->row
[i
] + first
, n
) != -1)
5381 isl_die(isl_mat_get_ctx(M
), isl_error_internal
,
5382 "final columns should be zero",
5383 return isl_stat_error
);
5387 /* Set the affine expressions in "ma" according to the rows in "M", which
5388 * are defined over the local space "ls".
5389 * The matrix "M" may have extra (zero) columns beyond the number
5390 * of variables in "ls".
5392 static __isl_give isl_multi_aff
*set_from_affine_matrix(
5393 __isl_take isl_multi_aff
*ma
, __isl_take isl_local_space
*ls
,
5394 __isl_take isl_mat
*M
)
5399 if (!ma
|| !ls
|| !M
)
5402 dim
= isl_local_space_dim(ls
, isl_dim_all
);
5403 if (check_final_columns_are_zero(M
, 1 + dim
) < 0)
5405 for (i
= 1; i
< M
->n_row
; ++i
) {
5406 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
5408 isl_int_set(aff
->v
->el
[0], M
->row
[0][0]);
5409 isl_seq_cpy(aff
->v
->el
+ 1, M
->row
[i
], 1 + dim
);
5411 aff
= isl_aff_normalize(aff
);
5412 ma
= isl_multi_aff_set_aff(ma
, i
- 1, aff
);
5414 isl_local_space_free(ls
);
5419 isl_local_space_free(ls
);
5421 isl_multi_aff_free(ma
);
5425 /* Given a basic set "dom" that represents the context and an affine
5426 * matrix "M" that maps the dimensions of the context to the
5427 * output variables, construct an isl_pw_multi_aff with a single
5428 * cell corresponding to "dom" and affine expressions copied from "M".
5430 * Note that the description of the initial context may have involved
5431 * existentially quantified variables, in which case they also appear
5432 * in "dom". These need to be removed before creating the affine
5433 * expression because an affine expression cannot be defined in terms
5434 * of existentially quantified variables without a known representation.
5435 * Since newly added integer divisions are inserted before these
5436 * existentially quantified variables, they are still in the final
5437 * positions and the corresponding final columns of "M" are zero
5438 * because align_context_divs adds the existentially quantified
5439 * variables of the context to the main tableau without any constraints and
5440 * any equality constraints that are added later on can only serve
5441 * to eliminate these existentially quantified variables.
5443 static void sol_pma_add(struct isl_sol_pma
*sol
,
5444 __isl_take isl_basic_set
*dom
, __isl_take isl_mat
*M
)
5446 isl_local_space
*ls
;
5447 isl_multi_aff
*maff
;
5448 isl_pw_multi_aff
*pma
;
5451 n_div
= isl_basic_set_dim(dom
, isl_dim_div
);
5452 n_known
= n_div
- sol
->sol
.context
->n_unknown
;
5454 maff
= isl_multi_aff_alloc(isl_pw_multi_aff_get_space(sol
->pma
));
5455 ls
= isl_basic_set_get_local_space(dom
);
5456 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
,
5457 n_known
, n_div
- n_known
);
5458 maff
= set_from_affine_matrix(maff
, ls
, M
);
5459 dom
= isl_basic_set_simplify(dom
);
5460 dom
= isl_basic_set_finalize(dom
);
5461 pma
= isl_pw_multi_aff_alloc(isl_set_from_basic_set(dom
), maff
);
5462 sol
->pma
= isl_pw_multi_aff_add_disjoint(sol
->pma
, pma
);
5467 static void sol_pma_free_wrap(struct isl_sol
*sol
)
5469 sol_pma_free((struct isl_sol_pma
*)sol
);
5472 static void sol_pma_add_empty_wrap(struct isl_sol
*sol
,
5473 __isl_take isl_basic_set
*bset
)
5475 sol_pma_add_empty((struct isl_sol_pma
*)sol
, bset
);
5478 static void sol_pma_add_wrap(struct isl_sol
*sol
,
5479 __isl_take isl_basic_set
*dom
, __isl_take isl_mat
*M
)
5481 sol_pma_add((struct isl_sol_pma
*)sol
, dom
, M
);
5484 /* Construct an isl_sol_pma structure for accumulating the solution.
5485 * If track_empty is set, then we also keep track of the parts
5486 * of the context where there is no solution.
5487 * If max is set, then we are solving a maximization, rather than
5488 * a minimization problem, which means that the variables in the
5489 * tableau have value "M - x" rather than "M + x".
5491 static struct isl_sol
*sol_pma_init(__isl_keep isl_basic_map
*bmap
,
5492 __isl_take isl_basic_set
*dom
, int track_empty
, int max
)
5494 struct isl_sol_pma
*sol_pma
= NULL
;
5499 sol_pma
= isl_calloc_type(bmap
->ctx
, struct isl_sol_pma
);
5503 sol_pma
->sol
.rational
= ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
5504 sol_pma
->sol
.dec_level
.callback
.run
= &sol_dec_level_wrap
;
5505 sol_pma
->sol
.dec_level
.sol
= &sol_pma
->sol
;
5506 sol_pma
->sol
.max
= max
;
5507 sol_pma
->sol
.n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5508 sol_pma
->sol
.add
= &sol_pma_add_wrap
;
5509 sol_pma
->sol
.add_empty
= track_empty
? &sol_pma_add_empty_wrap
: NULL
;
5510 sol_pma
->sol
.free
= &sol_pma_free_wrap
;
5511 sol_pma
->pma
= isl_pw_multi_aff_empty(isl_basic_map_get_space(bmap
));
5515 sol_pma
->sol
.context
= isl_context_alloc(dom
);
5516 if (!sol_pma
->sol
.context
)
5520 sol_pma
->empty
= isl_set_alloc_space(isl_basic_set_get_space(dom
),
5521 1, ISL_SET_DISJOINT
);
5522 if (!sol_pma
->empty
)
5526 isl_basic_set_free(dom
);
5527 return &sol_pma
->sol
;
5529 isl_basic_set_free(dom
);
5530 sol_pma_free(sol_pma
);
5534 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
5535 * some obvious symmetries.
5537 * We call basic_map_partial_lexopt_base_sol and extract the results.
5539 static __isl_give isl_pw_multi_aff
*basic_map_partial_lexopt_base_pw_multi_aff(
5540 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5541 __isl_give isl_set
**empty
, int max
)
5543 isl_pw_multi_aff
*result
= NULL
;
5544 struct isl_sol
*sol
;
5545 struct isl_sol_pma
*sol_pma
;
5547 sol
= basic_map_partial_lexopt_base_sol(bmap
, dom
, empty
, max
,
5551 sol_pma
= (struct isl_sol_pma
*) sol
;
5553 result
= isl_pw_multi_aff_copy(sol_pma
->pma
);
5555 *empty
= isl_set_copy(sol_pma
->empty
);
5556 sol_free(&sol_pma
->sol
);
5560 /* Given that the last input variable of "maff" represents the minimum
5561 * of some bounds, check whether we need to plug in the expression
5564 * In particular, check if the last input variable appears in any
5565 * of the expressions in "maff".
5567 static int need_substitution(__isl_keep isl_multi_aff
*maff
)
5572 pos
= isl_multi_aff_dim(maff
, isl_dim_in
) - 1;
5574 for (i
= 0; i
< maff
->n
; ++i
)
5575 if (isl_aff_involves_dims(maff
->p
[i
], isl_dim_in
, pos
, 1))
5581 /* Given a set of upper bounds on the last "input" variable m,
5582 * construct a piecewise affine expression that selects
5583 * the minimal upper bound to m, i.e.,
5584 * divide the space into cells where one
5585 * of the upper bounds is smaller than all the others and select
5586 * this upper bound on that cell.
5588 * In particular, if there are n bounds b_i, then the result
5589 * consists of n cell, each one of the form
5591 * b_i <= b_j for j > i
5592 * b_i < b_j for j < i
5594 * The affine expression on this cell is
5598 static __isl_give isl_pw_aff
*set_minimum_pa(__isl_take isl_space
*space
,
5599 __isl_take isl_mat
*var
)
5602 isl_aff
*aff
= NULL
;
5603 isl_basic_set
*bset
= NULL
;
5604 isl_pw_aff
*paff
= NULL
;
5605 isl_space
*pw_space
;
5606 isl_local_space
*ls
= NULL
;
5611 ls
= isl_local_space_from_space(isl_space_copy(space
));
5612 pw_space
= isl_space_copy(space
);
5613 pw_space
= isl_space_from_domain(pw_space
);
5614 pw_space
= isl_space_add_dims(pw_space
, isl_dim_out
, 1);
5615 paff
= isl_pw_aff_alloc_size(pw_space
, var
->n_row
);
5617 for (i
= 0; i
< var
->n_row
; ++i
) {
5620 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
5621 bset
= isl_basic_set_alloc_space(isl_space_copy(space
), 0,
5625 isl_int_set_si(aff
->v
->el
[0], 1);
5626 isl_seq_cpy(aff
->v
->el
+ 1, var
->row
[i
], var
->n_col
);
5627 isl_int_set_si(aff
->v
->el
[1 + var
->n_col
], 0);
5628 bset
= select_minimum(bset
, var
, i
);
5629 paff_i
= isl_pw_aff_alloc(isl_set_from_basic_set(bset
), aff
);
5630 paff
= isl_pw_aff_add_disjoint(paff
, paff_i
);
5633 isl_local_space_free(ls
);
5634 isl_space_free(space
);
5639 isl_basic_set_free(bset
);
5640 isl_pw_aff_free(paff
);
5641 isl_local_space_free(ls
);
5642 isl_space_free(space
);
5647 /* Given a piecewise multi-affine expression of which the last input variable
5648 * is the minimum of the bounds in "cst", plug in the value of the minimum.
5649 * This minimum expression is given in "min_expr_pa".
5650 * The set "min_expr" contains the same information, but in the form of a set.
5651 * The variable is subsequently projected out.
5653 * The implementation is similar to those of "split" and "split_domain".
5654 * If the variable appears in a given expression, then minimum expression
5655 * is plugged in. Otherwise, if the variable appears in the constraints
5656 * and a split is required, then the domain is split. Otherwise, no split
5659 static __isl_give isl_pw_multi_aff
*split_domain_pma(
5660 __isl_take isl_pw_multi_aff
*opt
, __isl_take isl_pw_aff
*min_expr_pa
,
5661 __isl_take isl_set
*min_expr
, __isl_take isl_mat
*cst
)
5666 isl_pw_multi_aff
*res
;
5668 if (!opt
|| !min_expr
|| !cst
)
5671 n_in
= isl_pw_multi_aff_dim(opt
, isl_dim_in
);
5672 space
= isl_pw_multi_aff_get_space(opt
);
5673 space
= isl_space_drop_dims(space
, isl_dim_in
, n_in
- 1, 1);
5674 res
= isl_pw_multi_aff_empty(space
);
5676 for (i
= 0; i
< opt
->n
; ++i
) {
5677 isl_pw_multi_aff
*pma
;
5679 pma
= isl_pw_multi_aff_alloc(isl_set_copy(opt
->p
[i
].set
),
5680 isl_multi_aff_copy(opt
->p
[i
].maff
));
5681 if (need_substitution(opt
->p
[i
].maff
))
5682 pma
= isl_pw_multi_aff_substitute(pma
,
5683 isl_dim_in
, n_in
- 1, min_expr_pa
);
5684 else if (need_split_set(opt
->p
[i
].set
, cst
))
5685 pma
= isl_pw_multi_aff_intersect_domain(pma
,
5686 isl_set_copy(min_expr
));
5687 pma
= isl_pw_multi_aff_project_out(pma
,
5688 isl_dim_in
, n_in
- 1, 1);
5690 res
= isl_pw_multi_aff_add_disjoint(res
, pma
);
5693 isl_pw_multi_aff_free(opt
);
5694 isl_pw_aff_free(min_expr_pa
);
5695 isl_set_free(min_expr
);
5699 isl_pw_multi_aff_free(opt
);
5700 isl_pw_aff_free(min_expr_pa
);
5701 isl_set_free(min_expr
);
5706 static __isl_give isl_pw_multi_aff
*basic_map_partial_lexopt_pw_multi_aff(
5707 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5708 __isl_give isl_set
**empty
, int max
);
5710 /* This function is called from basic_map_partial_lexopt_symm.
5711 * The last variable of "bmap" and "dom" corresponds to the minimum
5712 * of the bounds in "cst". "map_space" is the space of the original
5713 * input relation (of basic_map_partial_lexopt_symm) and "set_space"
5714 * is the space of the original domain.
5716 * We recursively call basic_map_partial_lexopt and then plug in
5717 * the definition of the minimum in the result.
5719 static __isl_give isl_pw_multi_aff
*
5720 basic_map_partial_lexopt_symm_core_pw_multi_aff(
5721 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5722 __isl_give isl_set
**empty
, int max
, __isl_take isl_mat
*cst
,
5723 __isl_take isl_space
*map_space
, __isl_take isl_space
*set_space
)
5725 isl_pw_multi_aff
*opt
;
5726 isl_pw_aff
*min_expr_pa
;
5729 min_expr
= set_minimum(isl_basic_set_get_space(dom
), isl_mat_copy(cst
));
5730 min_expr_pa
= set_minimum_pa(isl_basic_set_get_space(dom
),
5733 opt
= basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, empty
, max
);
5736 *empty
= split(*empty
,
5737 isl_set_copy(min_expr
), isl_mat_copy(cst
));
5738 *empty
= isl_set_reset_space(*empty
, set_space
);
5741 opt
= split_domain_pma(opt
, min_expr_pa
, min_expr
, cst
);
5742 opt
= isl_pw_multi_aff_reset_space(opt
, map_space
);
5748 #define TYPE isl_pw_multi_aff
5750 #define SUFFIX _pw_multi_aff
5751 #include "isl_tab_lexopt_templ.c"