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 __isl_take isl_basic_set
*dom
, __isl_take 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 __isl_take isl_basic_set
*dom
, __isl_take 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 isl_bool
same_solution(struct isl_partial_sol
*s1
,
267 struct isl_partial_sol
*s2
, unsigned n_div
)
272 if (!s1
->M
!= !s2
->M
)
273 return isl_bool_false
;
275 return isl_bool_true
;
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)
282 return isl_bool_false
;
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)
285 return isl_bool_false
;
286 if (!isl_seq_eq(s1
->M
->row
[i
], s2
->M
->row
[i
], 1+dim
+n_div
))
287 return isl_bool_false
;
289 return isl_bool_true
;
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
) {
324 n_div
= isl_basic_set_dim(
325 sol
->context
->op
->peek_basic_set(sol
->context
),
328 same
= same_solution(partial
, partial
->next
, n_div
);
335 struct isl_basic_set
*bset
;
339 n
= isl_basic_set_dim(partial
->next
->dom
, isl_dim_div
);
341 bset
= sol_domain(sol
);
342 isl_basic_set_free(partial
->next
->dom
);
343 partial
->next
->dom
= bset
;
344 M
= partial
->next
->M
;
346 M
= isl_mat_drop_cols(M
, M
->n_col
- n
, n
);
347 partial
->next
->M
= M
;
351 partial
->next
->level
= sol
->level
;
356 sol
->partial
= partial
->next
;
357 isl_basic_set_free(partial
->dom
);
358 isl_mat_free(partial
->M
);
364 if (sol
->level
== 0) {
365 for (partial
= sol
->partial
; partial
; partial
= sol
->partial
)
371 error
: sol
->error
= 1;
374 static void sol_dec_level(struct isl_sol
*sol
)
384 static int sol_dec_level_wrap(struct isl_tab_callback
*cb
)
386 struct isl_sol_callback
*callback
= (struct isl_sol_callback
*)cb
;
388 sol_dec_level(callback
->sol
);
390 return callback
->sol
->error
? -1 : 0;
393 /* Move down to next level and push callback onto context tableau
394 * to decrease the level again when it gets rolled back across
395 * the current state. That is, dec_level will be called with
396 * the context tableau in the same state as it is when inc_level
399 static void sol_inc_level(struct isl_sol
*sol
)
407 tab
= sol
->context
->op
->peek_tab(sol
->context
);
408 if (isl_tab_push_callback(tab
, &sol
->dec_level
.callback
) < 0)
412 static void scale_rows(struct isl_mat
*mat
, isl_int m
, int n_row
)
416 if (isl_int_is_one(m
))
419 for (i
= 0; i
< n_row
; ++i
)
420 isl_seq_scale(mat
->row
[i
], mat
->row
[i
], m
, mat
->n_col
);
423 /* Add the solution identified by the tableau and the context tableau.
425 * The layout of the variables is as follows.
426 * tab->n_var is equal to the total number of variables in the input
427 * map (including divs that were copied from the context)
428 * + the number of extra divs constructed
429 * Of these, the first tab->n_param and the last tab->n_div variables
430 * correspond to the variables in the context, i.e.,
431 * tab->n_param + tab->n_div = context_tab->n_var
432 * tab->n_param is equal to the number of parameters and input
433 * dimensions in the input map
434 * tab->n_div is equal to the number of divs in the context
436 * If there is no solution, then call add_empty with a basic set
437 * that corresponds to the context tableau. (If add_empty is NULL,
440 * If there is a solution, then first construct a matrix that maps
441 * all dimensions of the context to the output variables, i.e.,
442 * the output dimensions in the input map.
443 * The divs in the input map (if any) that do not correspond to any
444 * div in the context do not appear in the solution.
445 * The algorithm will make sure that they have an integer value,
446 * but these values themselves are of no interest.
447 * We have to be careful not to drop or rearrange any divs in the
448 * context because that would change the meaning of the matrix.
450 * To extract the value of the output variables, it should be noted
451 * that we always use a big parameter M in the main tableau and so
452 * the variable stored in this tableau is not an output variable x itself, but
453 * x' = M + x (in case of minimization)
455 * x' = M - x (in case of maximization)
456 * If x' appears in a column, then its optimal value is zero,
457 * which means that the optimal value of x is an unbounded number
458 * (-M for minimization and M for maximization).
459 * We currently assume that the output dimensions in the original map
460 * are bounded, so this cannot occur.
461 * Similarly, when x' appears in a row, then the coefficient of M in that
462 * row is necessarily 1.
463 * If the row in the tableau represents
464 * d x' = c + d M + e(y)
465 * then, in case of minimization, the corresponding row in the matrix
468 * with a d = m, the (updated) common denominator of the matrix.
469 * In case of maximization, the row will be
472 static void sol_add(struct isl_sol
*sol
, struct isl_tab
*tab
)
474 struct isl_basic_set
*bset
= NULL
;
475 struct isl_mat
*mat
= NULL
;
480 if (sol
->error
|| !tab
)
483 if (tab
->empty
&& !sol
->add_empty
)
485 if (sol
->context
->op
->is_empty(sol
->context
))
488 bset
= sol_domain(sol
);
491 sol_push_sol(sol
, bset
, NULL
);
497 mat
= isl_mat_alloc(tab
->mat
->ctx
, 1 + sol
->n_out
,
498 1 + tab
->n_param
+ tab
->n_div
);
504 isl_seq_clr(mat
->row
[0] + 1, mat
->n_col
- 1);
505 isl_int_set_si(mat
->row
[0][0], 1);
506 for (row
= 0; row
< sol
->n_out
; ++row
) {
507 int i
= tab
->n_param
+ row
;
510 isl_seq_clr(mat
->row
[1 + row
], mat
->n_col
);
511 if (!tab
->var
[i
].is_row
) {
513 isl_die(mat
->ctx
, isl_error_invalid
,
514 "unbounded optimum", goto error2
);
518 r
= tab
->var
[i
].index
;
520 isl_int_ne(tab
->mat
->row
[r
][2], tab
->mat
->row
[r
][0]))
521 isl_die(mat
->ctx
, isl_error_invalid
,
522 "unbounded optimum", goto error2
);
523 isl_int_gcd(m
, mat
->row
[0][0], tab
->mat
->row
[r
][0]);
524 isl_int_divexact(m
, tab
->mat
->row
[r
][0], m
);
525 scale_rows(mat
, m
, 1 + row
);
526 isl_int_divexact(m
, mat
->row
[0][0], tab
->mat
->row
[r
][0]);
527 isl_int_mul(mat
->row
[1 + row
][0], m
, tab
->mat
->row
[r
][1]);
528 for (j
= 0; j
< tab
->n_param
; ++j
) {
530 if (tab
->var
[j
].is_row
)
532 col
= tab
->var
[j
].index
;
533 isl_int_mul(mat
->row
[1 + row
][1 + j
], m
,
534 tab
->mat
->row
[r
][off
+ col
]);
536 for (j
= 0; j
< tab
->n_div
; ++j
) {
538 if (tab
->var
[tab
->n_var
- tab
->n_div
+j
].is_row
)
540 col
= tab
->var
[tab
->n_var
- tab
->n_div
+j
].index
;
541 isl_int_mul(mat
->row
[1 + row
][1 + tab
->n_param
+ j
], m
,
542 tab
->mat
->row
[r
][off
+ col
]);
545 isl_seq_neg(mat
->row
[1 + row
], mat
->row
[1 + row
],
551 sol_push_sol(sol
, bset
, mat
);
556 isl_basic_set_free(bset
);
564 struct isl_set
*empty
;
567 static void sol_map_free(struct isl_sol_map
*sol_map
)
571 if (sol_map
->sol
.context
)
572 sol_map
->sol
.context
->op
->free(sol_map
->sol
.context
);
573 isl_map_free(sol_map
->map
);
574 isl_set_free(sol_map
->empty
);
578 static void sol_map_free_wrap(struct isl_sol
*sol
)
580 sol_map_free((struct isl_sol_map
*)sol
);
583 /* This function is called for parts of the context where there is
584 * no solution, with "bset" corresponding to the context tableau.
585 * Simply add the basic set to the set "empty".
587 static void sol_map_add_empty(struct isl_sol_map
*sol
,
588 struct isl_basic_set
*bset
)
590 if (!bset
|| !sol
->empty
)
593 sol
->empty
= isl_set_grow(sol
->empty
, 1);
594 bset
= isl_basic_set_simplify(bset
);
595 bset
= isl_basic_set_finalize(bset
);
596 sol
->empty
= isl_set_add_basic_set(sol
->empty
, isl_basic_set_copy(bset
));
599 isl_basic_set_free(bset
);
602 isl_basic_set_free(bset
);
606 static void sol_map_add_empty_wrap(struct isl_sol
*sol
,
607 struct isl_basic_set
*bset
)
609 sol_map_add_empty((struct isl_sol_map
*)sol
, bset
);
612 /* Given a basic set "dom" that represents the context and an affine
613 * matrix "M" that maps the dimensions of the context to the
614 * output variables, construct a basic map with the same parameters
615 * and divs as the context, the dimensions of the context as input
616 * dimensions and a number of output dimensions that is equal to
617 * the number of output dimensions in the input map.
619 * The constraints and divs of the context are simply copied
620 * from "dom". For each row
624 * is added, with d the common denominator of M.
626 static void sol_map_add(struct isl_sol_map
*sol
,
627 __isl_take isl_basic_set
*dom
, __isl_take isl_mat
*M
)
630 struct isl_basic_map
*bmap
= NULL
;
638 if (sol
->sol
.error
|| !dom
|| !M
)
641 n_out
= sol
->sol
.n_out
;
642 n_eq
= dom
->n_eq
+ n_out
;
643 n_ineq
= dom
->n_ineq
;
645 nparam
= isl_basic_set_total_dim(dom
) - n_div
;
646 total
= isl_map_dim(sol
->map
, isl_dim_all
);
647 bmap
= isl_basic_map_alloc_space(isl_map_get_space(sol
->map
),
648 n_div
, n_eq
, 2 * n_div
+ n_ineq
);
651 if (sol
->sol
.rational
)
652 ISL_F_SET(bmap
, ISL_BASIC_MAP_RATIONAL
);
653 for (i
= 0; i
< dom
->n_div
; ++i
) {
654 int k
= isl_basic_map_alloc_div(bmap
);
657 isl_seq_cpy(bmap
->div
[k
], dom
->div
[i
], 1 + 1 + nparam
);
658 isl_seq_clr(bmap
->div
[k
] + 1 + 1 + nparam
, total
- nparam
);
659 isl_seq_cpy(bmap
->div
[k
] + 1 + 1 + total
,
660 dom
->div
[i
] + 1 + 1 + nparam
, i
);
662 for (i
= 0; i
< dom
->n_eq
; ++i
) {
663 int k
= isl_basic_map_alloc_equality(bmap
);
666 isl_seq_cpy(bmap
->eq
[k
], dom
->eq
[i
], 1 + nparam
);
667 isl_seq_clr(bmap
->eq
[k
] + 1 + nparam
, total
- nparam
);
668 isl_seq_cpy(bmap
->eq
[k
] + 1 + total
,
669 dom
->eq
[i
] + 1 + nparam
, n_div
);
671 for (i
= 0; i
< dom
->n_ineq
; ++i
) {
672 int k
= isl_basic_map_alloc_inequality(bmap
);
675 isl_seq_cpy(bmap
->ineq
[k
], dom
->ineq
[i
], 1 + nparam
);
676 isl_seq_clr(bmap
->ineq
[k
] + 1 + nparam
, total
- nparam
);
677 isl_seq_cpy(bmap
->ineq
[k
] + 1 + total
,
678 dom
->ineq
[i
] + 1 + nparam
, n_div
);
680 for (i
= 0; i
< M
->n_row
- 1; ++i
) {
681 int k
= isl_basic_map_alloc_equality(bmap
);
684 isl_seq_cpy(bmap
->eq
[k
], M
->row
[1 + i
], 1 + nparam
);
685 isl_seq_clr(bmap
->eq
[k
] + 1 + nparam
, n_out
);
686 isl_int_neg(bmap
->eq
[k
][1 + nparam
+ i
], M
->row
[0][0]);
687 isl_seq_cpy(bmap
->eq
[k
] + 1 + nparam
+ n_out
,
688 M
->row
[1 + i
] + 1 + nparam
, n_div
);
690 bmap
= isl_basic_map_simplify(bmap
);
691 bmap
= isl_basic_map_finalize(bmap
);
692 sol
->map
= isl_map_grow(sol
->map
, 1);
693 sol
->map
= isl_map_add_basic_map(sol
->map
, bmap
);
694 isl_basic_set_free(dom
);
700 isl_basic_set_free(dom
);
702 isl_basic_map_free(bmap
);
706 static void sol_map_add_wrap(struct isl_sol
*sol
,
707 __isl_take isl_basic_set
*dom
, __isl_take isl_mat
*M
)
709 sol_map_add((struct isl_sol_map
*)sol
, dom
, M
);
713 /* Store the "parametric constant" of row "row" of tableau "tab" in "line",
714 * i.e., the constant term and the coefficients of all variables that
715 * appear in the context tableau.
716 * Note that the coefficient of the big parameter M is NOT copied.
717 * The context tableau may not have a big parameter and even when it
718 * does, it is a different big parameter.
720 static void get_row_parameter_line(struct isl_tab
*tab
, int row
, isl_int
*line
)
723 unsigned off
= 2 + tab
->M
;
725 isl_int_set(line
[0], tab
->mat
->row
[row
][1]);
726 for (i
= 0; i
< tab
->n_param
; ++i
) {
727 if (tab
->var
[i
].is_row
)
728 isl_int_set_si(line
[1 + i
], 0);
730 int col
= tab
->var
[i
].index
;
731 isl_int_set(line
[1 + i
], tab
->mat
->row
[row
][off
+ col
]);
734 for (i
= 0; i
< tab
->n_div
; ++i
) {
735 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
736 isl_int_set_si(line
[1 + tab
->n_param
+ i
], 0);
738 int col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
739 isl_int_set(line
[1 + tab
->n_param
+ i
],
740 tab
->mat
->row
[row
][off
+ col
]);
745 /* Check if rows "row1" and "row2" have identical "parametric constants",
746 * as explained above.
747 * In this case, we also insist that the coefficients of the big parameter
748 * be the same as the values of the constants will only be the same
749 * if these coefficients are also the same.
751 static int identical_parameter_line(struct isl_tab
*tab
, int row1
, int row2
)
754 unsigned off
= 2 + tab
->M
;
756 if (isl_int_ne(tab
->mat
->row
[row1
][1], tab
->mat
->row
[row2
][1]))
759 if (tab
->M
&& isl_int_ne(tab
->mat
->row
[row1
][2],
760 tab
->mat
->row
[row2
][2]))
763 for (i
= 0; i
< tab
->n_param
+ tab
->n_div
; ++i
) {
764 int pos
= i
< tab
->n_param
? i
:
765 tab
->n_var
- tab
->n_div
+ i
- tab
->n_param
;
768 if (tab
->var
[pos
].is_row
)
770 col
= tab
->var
[pos
].index
;
771 if (isl_int_ne(tab
->mat
->row
[row1
][off
+ col
],
772 tab
->mat
->row
[row2
][off
+ col
]))
778 /* Return an inequality that expresses that the "parametric constant"
779 * should be non-negative.
780 * This function is only called when the coefficient of the big parameter
783 static struct isl_vec
*get_row_parameter_ineq(struct isl_tab
*tab
, int row
)
785 struct isl_vec
*ineq
;
787 ineq
= isl_vec_alloc(tab
->mat
->ctx
, 1 + tab
->n_param
+ tab
->n_div
);
791 get_row_parameter_line(tab
, row
, ineq
->el
);
793 ineq
= isl_vec_normalize(ineq
);
798 /* Normalize a div expression of the form
800 * [(g*f(x) + c)/(g * m)]
802 * with c the constant term and f(x) the remaining coefficients, to
806 static void normalize_div(__isl_keep isl_vec
*div
)
808 isl_ctx
*ctx
= isl_vec_get_ctx(div
);
809 int len
= div
->size
- 2;
811 isl_seq_gcd(div
->el
+ 2, len
, &ctx
->normalize_gcd
);
812 isl_int_gcd(ctx
->normalize_gcd
, ctx
->normalize_gcd
, div
->el
[0]);
814 if (isl_int_is_one(ctx
->normalize_gcd
))
817 isl_int_divexact(div
->el
[0], div
->el
[0], ctx
->normalize_gcd
);
818 isl_int_fdiv_q(div
->el
[1], div
->el
[1], ctx
->normalize_gcd
);
819 isl_seq_scale_down(div
->el
+ 2, div
->el
+ 2, ctx
->normalize_gcd
, len
);
822 /* Return an integer division for use in a parametric cut based
824 * In particular, let the parametric constant of the row be
828 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
829 * The div returned is equal to
831 * floor(\sum_i {-a_i} y_i) = floor((\sum_i (-a_i mod d) y_i)/d)
833 static struct isl_vec
*get_row_parameter_div(struct isl_tab
*tab
, int row
)
837 div
= isl_vec_alloc(tab
->mat
->ctx
, 1 + 1 + tab
->n_param
+ tab
->n_div
);
841 isl_int_set(div
->el
[0], tab
->mat
->row
[row
][0]);
842 get_row_parameter_line(tab
, row
, div
->el
+ 1);
843 isl_seq_neg(div
->el
+ 1, div
->el
+ 1, div
->size
- 1);
845 isl_seq_fdiv_r(div
->el
+ 1, div
->el
+ 1, div
->el
[0], div
->size
- 1);
850 /* Return an integer division for use in transferring an integrality constraint
852 * In particular, let the parametric constant of the row be
856 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
857 * The the returned div is equal to
859 * floor(\sum_i {a_i} y_i) = floor((\sum_i (a_i mod d) y_i)/d)
861 static struct isl_vec
*get_row_split_div(struct isl_tab
*tab
, int row
)
865 div
= isl_vec_alloc(tab
->mat
->ctx
, 1 + 1 + tab
->n_param
+ tab
->n_div
);
869 isl_int_set(div
->el
[0], tab
->mat
->row
[row
][0]);
870 get_row_parameter_line(tab
, row
, div
->el
+ 1);
872 isl_seq_fdiv_r(div
->el
+ 1, div
->el
+ 1, div
->el
[0], div
->size
- 1);
877 /* Construct and return an inequality that expresses an upper bound
879 * In particular, if the div is given by
883 * then the inequality expresses
887 static struct isl_vec
*ineq_for_div(struct isl_basic_set
*bset
, unsigned div
)
891 struct isl_vec
*ineq
;
896 total
= isl_basic_set_total_dim(bset
);
897 div_pos
= 1 + total
- bset
->n_div
+ div
;
899 ineq
= isl_vec_alloc(bset
->ctx
, 1 + total
);
903 isl_seq_cpy(ineq
->el
, bset
->div
[div
] + 1, 1 + total
);
904 isl_int_neg(ineq
->el
[div_pos
], bset
->div
[div
][0]);
908 /* Given a row in the tableau and a div that was created
909 * using get_row_split_div and that has been constrained to equality, i.e.,
911 * d = floor(\sum_i {a_i} y_i) = \sum_i {a_i} y_i
913 * replace the expression "\sum_i {a_i} y_i" in the row by d,
914 * i.e., we subtract "\sum_i {a_i} y_i" and add 1 d.
915 * The coefficients of the non-parameters in the tableau have been
916 * verified to be integral. We can therefore simply replace coefficient b
917 * by floor(b). For the coefficients of the parameters we have
918 * floor(a_i) = a_i - {a_i}, while for the other coefficients, we have
921 static struct isl_tab
*set_row_cst_to_div(struct isl_tab
*tab
, int row
, int div
)
923 isl_seq_fdiv_q(tab
->mat
->row
[row
] + 1, tab
->mat
->row
[row
] + 1,
924 tab
->mat
->row
[row
][0], 1 + tab
->M
+ tab
->n_col
);
926 isl_int_set_si(tab
->mat
->row
[row
][0], 1);
928 if (tab
->var
[tab
->n_var
- tab
->n_div
+ div
].is_row
) {
929 int drow
= tab
->var
[tab
->n_var
- tab
->n_div
+ div
].index
;
931 isl_assert(tab
->mat
->ctx
,
932 isl_int_is_one(tab
->mat
->row
[drow
][0]), goto error
);
933 isl_seq_combine(tab
->mat
->row
[row
] + 1,
934 tab
->mat
->ctx
->one
, tab
->mat
->row
[row
] + 1,
935 tab
->mat
->ctx
->one
, tab
->mat
->row
[drow
] + 1,
936 1 + tab
->M
+ tab
->n_col
);
938 int dcol
= tab
->var
[tab
->n_var
- tab
->n_div
+ div
].index
;
940 isl_int_add_ui(tab
->mat
->row
[row
][2 + tab
->M
+ dcol
],
941 tab
->mat
->row
[row
][2 + tab
->M
+ dcol
], 1);
950 /* Check if the (parametric) constant of the given row is obviously
951 * negative, meaning that we don't need to consult the context tableau.
952 * If there is a big parameter and its coefficient is non-zero,
953 * then this coefficient determines the outcome.
954 * Otherwise, we check whether the constant is negative and
955 * all non-zero coefficients of parameters are negative and
956 * belong to non-negative parameters.
958 static int is_obviously_neg(struct isl_tab
*tab
, int row
)
962 unsigned off
= 2 + tab
->M
;
965 if (isl_int_is_pos(tab
->mat
->row
[row
][2]))
967 if (isl_int_is_neg(tab
->mat
->row
[row
][2]))
971 if (isl_int_is_nonneg(tab
->mat
->row
[row
][1]))
973 for (i
= 0; i
< tab
->n_param
; ++i
) {
974 /* Eliminated parameter */
975 if (tab
->var
[i
].is_row
)
977 col
= tab
->var
[i
].index
;
978 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
980 if (!tab
->var
[i
].is_nonneg
)
982 if (isl_int_is_pos(tab
->mat
->row
[row
][off
+ col
]))
985 for (i
= 0; i
< tab
->n_div
; ++i
) {
986 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
988 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
989 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
991 if (!tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_nonneg
)
993 if (isl_int_is_pos(tab
->mat
->row
[row
][off
+ col
]))
999 /* Check if the (parametric) constant of the given row is obviously
1000 * non-negative, meaning that we don't need to consult the context tableau.
1001 * If there is a big parameter and its coefficient is non-zero,
1002 * then this coefficient determines the outcome.
1003 * Otherwise, we check whether the constant is non-negative and
1004 * all non-zero coefficients of parameters are positive and
1005 * belong to non-negative parameters.
1007 static int is_obviously_nonneg(struct isl_tab
*tab
, int row
)
1011 unsigned off
= 2 + tab
->M
;
1014 if (isl_int_is_pos(tab
->mat
->row
[row
][2]))
1016 if (isl_int_is_neg(tab
->mat
->row
[row
][2]))
1020 if (isl_int_is_neg(tab
->mat
->row
[row
][1]))
1022 for (i
= 0; i
< tab
->n_param
; ++i
) {
1023 /* Eliminated parameter */
1024 if (tab
->var
[i
].is_row
)
1026 col
= tab
->var
[i
].index
;
1027 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
1029 if (!tab
->var
[i
].is_nonneg
)
1031 if (isl_int_is_neg(tab
->mat
->row
[row
][off
+ col
]))
1034 for (i
= 0; i
< tab
->n_div
; ++i
) {
1035 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
1037 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
1038 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
1040 if (!tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_nonneg
)
1042 if (isl_int_is_neg(tab
->mat
->row
[row
][off
+ col
]))
1048 /* Given a row r and two columns, return the column that would
1049 * lead to the lexicographically smallest increment in the sample
1050 * solution when leaving the basis in favor of the row.
1051 * Pivoting with column c will increment the sample value by a non-negative
1052 * constant times a_{V,c}/a_{r,c}, with a_{V,c} the elements of column c
1053 * corresponding to the non-parametric variables.
1054 * If variable v appears in a column c_v, the a_{v,c} = 1 iff c = c_v,
1055 * with all other entries in this virtual row equal to zero.
1056 * If variable v appears in a row, then a_{v,c} is the element in column c
1059 * Let v be the first variable with a_{v,c1}/a_{r,c1} != a_{v,c2}/a_{r,c2}.
1060 * Then if a_{v,c1}/a_{r,c1} < a_{v,c2}/a_{r,c2}, i.e.,
1061 * a_{v,c2} a_{r,c1} - a_{v,c1} a_{r,c2} > 0, c1 results in the minimal
1062 * increment. Otherwise, it's c2.
1064 static int lexmin_col_pair(struct isl_tab
*tab
,
1065 int row
, int col1
, int col2
, isl_int tmp
)
1070 tr
= tab
->mat
->row
[row
] + 2 + tab
->M
;
1072 for (i
= tab
->n_param
; i
< tab
->n_var
- tab
->n_div
; ++i
) {
1076 if (!tab
->var
[i
].is_row
) {
1077 if (tab
->var
[i
].index
== col1
)
1079 if (tab
->var
[i
].index
== col2
)
1084 if (tab
->var
[i
].index
== row
)
1087 r
= tab
->mat
->row
[tab
->var
[i
].index
] + 2 + tab
->M
;
1088 s1
= isl_int_sgn(r
[col1
]);
1089 s2
= isl_int_sgn(r
[col2
]);
1090 if (s1
== 0 && s2
== 0)
1097 isl_int_mul(tmp
, r
[col2
], tr
[col1
]);
1098 isl_int_submul(tmp
, r
[col1
], tr
[col2
]);
1099 if (isl_int_is_pos(tmp
))
1101 if (isl_int_is_neg(tmp
))
1107 /* Given a row in the tableau, find and return the column that would
1108 * result in the lexicographically smallest, but positive, increment
1109 * in the sample point.
1110 * If there is no such column, then return tab->n_col.
1111 * If anything goes wrong, return -1.
1113 static int lexmin_pivot_col(struct isl_tab
*tab
, int row
)
1116 int col
= tab
->n_col
;
1120 tr
= tab
->mat
->row
[row
] + 2 + tab
->M
;
1124 for (j
= tab
->n_dead
; j
< tab
->n_col
; ++j
) {
1125 if (tab
->col_var
[j
] >= 0 &&
1126 (tab
->col_var
[j
] < tab
->n_param
||
1127 tab
->col_var
[j
] >= tab
->n_var
- tab
->n_div
))
1130 if (!isl_int_is_pos(tr
[j
]))
1133 if (col
== tab
->n_col
)
1136 col
= lexmin_col_pair(tab
, row
, col
, j
, tmp
);
1137 isl_assert(tab
->mat
->ctx
, col
>= 0, goto error
);
1147 /* Return the first known violated constraint, i.e., a non-negative
1148 * constraint that currently has an either obviously negative value
1149 * or a previously determined to be negative value.
1151 * If any constraint has a negative coefficient for the big parameter,
1152 * if any, then we return one of these first.
1154 static int first_neg(struct isl_tab
*tab
)
1159 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
1160 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
1162 if (!isl_int_is_neg(tab
->mat
->row
[row
][2]))
1165 tab
->row_sign
[row
] = isl_tab_row_neg
;
1168 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
1169 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
1171 if (tab
->row_sign
) {
1172 if (tab
->row_sign
[row
] == 0 &&
1173 is_obviously_neg(tab
, row
))
1174 tab
->row_sign
[row
] = isl_tab_row_neg
;
1175 if (tab
->row_sign
[row
] != isl_tab_row_neg
)
1177 } else if (!is_obviously_neg(tab
, row
))
1184 /* Check whether the invariant that all columns are lexico-positive
1185 * is satisfied. This function is not called from the current code
1186 * but is useful during debugging.
1188 static void check_lexpos(struct isl_tab
*tab
) __attribute__ ((unused
));
1189 static void check_lexpos(struct isl_tab
*tab
)
1191 unsigned off
= 2 + tab
->M
;
1196 for (col
= tab
->n_dead
; col
< tab
->n_col
; ++col
) {
1197 if (tab
->col_var
[col
] >= 0 &&
1198 (tab
->col_var
[col
] < tab
->n_param
||
1199 tab
->col_var
[col
] >= tab
->n_var
- tab
->n_div
))
1201 for (var
= tab
->n_param
; var
< tab
->n_var
- tab
->n_div
; ++var
) {
1202 if (!tab
->var
[var
].is_row
) {
1203 if (tab
->var
[var
].index
== col
)
1208 row
= tab
->var
[var
].index
;
1209 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
1211 if (isl_int_is_pos(tab
->mat
->row
[row
][off
+ col
]))
1213 fprintf(stderr
, "lexneg column %d (row %d)\n",
1216 if (var
>= tab
->n_var
- tab
->n_div
)
1217 fprintf(stderr
, "zero column %d\n", col
);
1221 /* Report to the caller that the given constraint is part of an encountered
1224 static int report_conflicting_constraint(struct isl_tab
*tab
, int con
)
1226 return tab
->conflict(con
, tab
->conflict_user
);
1229 /* Given a conflicting row in the tableau, report all constraints
1230 * involved in the row to the caller. That is, the row itself
1231 * (if it represents a constraint) and all constraint columns with
1232 * non-zero (and therefore negative) coefficients.
1234 static int report_conflict(struct isl_tab
*tab
, int row
)
1242 if (tab
->row_var
[row
] < 0 &&
1243 report_conflicting_constraint(tab
, ~tab
->row_var
[row
]) < 0)
1246 tr
= tab
->mat
->row
[row
] + 2 + tab
->M
;
1248 for (j
= tab
->n_dead
; j
< tab
->n_col
; ++j
) {
1249 if (tab
->col_var
[j
] >= 0 &&
1250 (tab
->col_var
[j
] < tab
->n_param
||
1251 tab
->col_var
[j
] >= tab
->n_var
- tab
->n_div
))
1254 if (!isl_int_is_neg(tr
[j
]))
1257 if (tab
->col_var
[j
] < 0 &&
1258 report_conflicting_constraint(tab
, ~tab
->col_var
[j
]) < 0)
1265 /* Resolve all known or obviously violated constraints through pivoting.
1266 * In particular, as long as we can find any violated constraint, we
1267 * look for a pivoting column that would result in the lexicographically
1268 * smallest increment in the sample point. If there is no such column
1269 * then the tableau is infeasible.
1271 static int restore_lexmin(struct isl_tab
*tab
) WARN_UNUSED
;
1272 static int restore_lexmin(struct isl_tab
*tab
)
1280 while ((row
= first_neg(tab
)) != -1) {
1281 col
= lexmin_pivot_col(tab
, row
);
1282 if (col
>= tab
->n_col
) {
1283 if (report_conflict(tab
, row
) < 0)
1285 if (isl_tab_mark_empty(tab
) < 0)
1291 if (isl_tab_pivot(tab
, row
, col
) < 0)
1297 /* Given a row that represents an equality, look for an appropriate
1299 * In particular, if there are any non-zero coefficients among
1300 * the non-parameter variables, then we take the last of these
1301 * variables. Eliminating this variable in terms of the other
1302 * variables and/or parameters does not influence the property
1303 * that all column in the initial tableau are lexicographically
1304 * positive. The row corresponding to the eliminated variable
1305 * will only have non-zero entries below the diagonal of the
1306 * initial tableau. That is, we transform
1312 * If there is no such non-parameter variable, then we are dealing with
1313 * pure parameter equality and we pick any parameter with coefficient 1 or -1
1314 * for elimination. This will ensure that the eliminated parameter
1315 * always has an integer value whenever all the other parameters are integral.
1316 * If there is no such parameter then we return -1.
1318 static int last_var_col_or_int_par_col(struct isl_tab
*tab
, int row
)
1320 unsigned off
= 2 + tab
->M
;
1323 for (i
= tab
->n_var
- tab
->n_div
- 1; i
>= 0 && i
>= tab
->n_param
; --i
) {
1325 if (tab
->var
[i
].is_row
)
1327 col
= tab
->var
[i
].index
;
1328 if (col
<= tab
->n_dead
)
1330 if (!isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
1333 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
1334 if (isl_int_is_one(tab
->mat
->row
[row
][off
+ i
]))
1336 if (isl_int_is_negone(tab
->mat
->row
[row
][off
+ i
]))
1342 /* Add an equality that is known to be valid to the tableau.
1343 * We first check if we can eliminate a variable or a parameter.
1344 * If not, we add the equality as two inequalities.
1345 * In this case, the equality was a pure parameter equality and there
1346 * is no need to resolve any constraint violations.
1348 * This function assumes that at least two more rows and at least
1349 * two more elements in the constraint array are available in the tableau.
1351 static struct isl_tab
*add_lexmin_valid_eq(struct isl_tab
*tab
, isl_int
*eq
)
1358 r
= isl_tab_add_row(tab
, eq
);
1362 r
= tab
->con
[r
].index
;
1363 i
= last_var_col_or_int_par_col(tab
, r
);
1365 tab
->con
[r
].is_nonneg
= 1;
1366 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
1368 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
1369 r
= isl_tab_add_row(tab
, eq
);
1372 tab
->con
[r
].is_nonneg
= 1;
1373 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
1376 if (isl_tab_pivot(tab
, r
, i
) < 0)
1378 if (isl_tab_kill_col(tab
, i
) < 0)
1389 /* Check if the given row is a pure constant.
1391 static int is_constant(struct isl_tab
*tab
, int row
)
1393 unsigned off
= 2 + tab
->M
;
1395 return isl_seq_first_non_zero(tab
->mat
->row
[row
] + off
+ tab
->n_dead
,
1396 tab
->n_col
- tab
->n_dead
) == -1;
1399 /* Add an equality that may or may not be valid to the tableau.
1400 * If the resulting row is a pure constant, then it must be zero.
1401 * Otherwise, the resulting tableau is empty.
1403 * If the row is not a pure constant, then we add two inequalities,
1404 * each time checking that they can be satisfied.
1405 * In the end we try to use one of the two constraints to eliminate
1408 * This function assumes that at least two more rows and at least
1409 * two more elements in the constraint array are available in the tableau.
1411 static int add_lexmin_eq(struct isl_tab
*tab
, isl_int
*eq
) WARN_UNUSED
;
1412 static int add_lexmin_eq(struct isl_tab
*tab
, isl_int
*eq
)
1416 struct isl_tab_undo
*snap
;
1420 snap
= isl_tab_snap(tab
);
1421 r1
= isl_tab_add_row(tab
, eq
);
1424 tab
->con
[r1
].is_nonneg
= 1;
1425 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r1
]) < 0)
1428 row
= tab
->con
[r1
].index
;
1429 if (is_constant(tab
, row
)) {
1430 if (!isl_int_is_zero(tab
->mat
->row
[row
][1]) ||
1431 (tab
->M
&& !isl_int_is_zero(tab
->mat
->row
[row
][2]))) {
1432 if (isl_tab_mark_empty(tab
) < 0)
1436 if (isl_tab_rollback(tab
, snap
) < 0)
1441 if (restore_lexmin(tab
) < 0)
1446 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
1448 r2
= isl_tab_add_row(tab
, eq
);
1451 tab
->con
[r2
].is_nonneg
= 1;
1452 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r2
]) < 0)
1455 if (restore_lexmin(tab
) < 0)
1460 if (!tab
->con
[r1
].is_row
) {
1461 if (isl_tab_kill_col(tab
, tab
->con
[r1
].index
) < 0)
1463 } else if (!tab
->con
[r2
].is_row
) {
1464 if (isl_tab_kill_col(tab
, tab
->con
[r2
].index
) < 0)
1469 tab
->bmap
= isl_basic_map_add_ineq(tab
->bmap
, eq
);
1470 if (isl_tab_push(tab
, isl_tab_undo_bmap_ineq
) < 0)
1472 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
1473 tab
->bmap
= isl_basic_map_add_ineq(tab
->bmap
, eq
);
1474 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
1475 if (isl_tab_push(tab
, isl_tab_undo_bmap_ineq
) < 0)
1484 /* Add an inequality to the tableau, resolving violations using
1487 * This function assumes that at least one more row and at least
1488 * one more element in the constraint array are available in the tableau.
1490 static struct isl_tab
*add_lexmin_ineq(struct isl_tab
*tab
, isl_int
*ineq
)
1497 tab
->bmap
= isl_basic_map_add_ineq(tab
->bmap
, ineq
);
1498 if (isl_tab_push(tab
, isl_tab_undo_bmap_ineq
) < 0)
1503 r
= isl_tab_add_row(tab
, ineq
);
1506 tab
->con
[r
].is_nonneg
= 1;
1507 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
1509 if (isl_tab_row_is_redundant(tab
, tab
->con
[r
].index
)) {
1510 if (isl_tab_mark_redundant(tab
, tab
->con
[r
].index
) < 0)
1515 if (restore_lexmin(tab
) < 0)
1517 if (!tab
->empty
&& tab
->con
[r
].is_row
&&
1518 isl_tab_row_is_redundant(tab
, tab
->con
[r
].index
))
1519 if (isl_tab_mark_redundant(tab
, tab
->con
[r
].index
) < 0)
1527 /* Check if the coefficients of the parameters are all integral.
1529 static int integer_parameter(struct isl_tab
*tab
, int row
)
1533 unsigned off
= 2 + tab
->M
;
1535 for (i
= 0; i
< tab
->n_param
; ++i
) {
1536 /* Eliminated parameter */
1537 if (tab
->var
[i
].is_row
)
1539 col
= tab
->var
[i
].index
;
1540 if (!isl_int_is_divisible_by(tab
->mat
->row
[row
][off
+ col
],
1541 tab
->mat
->row
[row
][0]))
1544 for (i
= 0; i
< tab
->n_div
; ++i
) {
1545 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
1547 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
1548 if (!isl_int_is_divisible_by(tab
->mat
->row
[row
][off
+ col
],
1549 tab
->mat
->row
[row
][0]))
1555 /* Check if the coefficients of the non-parameter variables are all integral.
1557 static int integer_variable(struct isl_tab
*tab
, int row
)
1560 unsigned off
= 2 + tab
->M
;
1562 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
1563 if (tab
->col_var
[i
] >= 0 &&
1564 (tab
->col_var
[i
] < tab
->n_param
||
1565 tab
->col_var
[i
] >= tab
->n_var
- tab
->n_div
))
1567 if (!isl_int_is_divisible_by(tab
->mat
->row
[row
][off
+ i
],
1568 tab
->mat
->row
[row
][0]))
1574 /* Check if the constant term is integral.
1576 static int integer_constant(struct isl_tab
*tab
, int row
)
1578 return isl_int_is_divisible_by(tab
->mat
->row
[row
][1],
1579 tab
->mat
->row
[row
][0]);
1582 #define I_CST 1 << 0
1583 #define I_PAR 1 << 1
1584 #define I_VAR 1 << 2
1586 /* Check for next (non-parameter) variable after "var" (first if var == -1)
1587 * that is non-integer and therefore requires a cut and return
1588 * the index of the variable.
1589 * For parametric tableaus, there are three parts in a row,
1590 * the constant, the coefficients of the parameters and the rest.
1591 * For each part, we check whether the coefficients in that part
1592 * are all integral and if so, set the corresponding flag in *f.
1593 * If the constant and the parameter part are integral, then the
1594 * current sample value is integral and no cut is required
1595 * (irrespective of whether the variable part is integral).
1597 static int next_non_integer_var(struct isl_tab
*tab
, int var
, int *f
)
1599 var
= var
< 0 ? tab
->n_param
: var
+ 1;
1601 for (; var
< tab
->n_var
- tab
->n_div
; ++var
) {
1604 if (!tab
->var
[var
].is_row
)
1606 row
= tab
->var
[var
].index
;
1607 if (integer_constant(tab
, row
))
1608 ISL_FL_SET(flags
, I_CST
);
1609 if (integer_parameter(tab
, row
))
1610 ISL_FL_SET(flags
, I_PAR
);
1611 if (ISL_FL_ISSET(flags
, I_CST
) && ISL_FL_ISSET(flags
, I_PAR
))
1613 if (integer_variable(tab
, row
))
1614 ISL_FL_SET(flags
, I_VAR
);
1621 /* Check for first (non-parameter) variable that is non-integer and
1622 * therefore requires a cut and return the corresponding row.
1623 * For parametric tableaus, there are three parts in a row,
1624 * the constant, the coefficients of the parameters and the rest.
1625 * For each part, we check whether the coefficients in that part
1626 * are all integral and if so, set the corresponding flag in *f.
1627 * If the constant and the parameter part are integral, then the
1628 * current sample value is integral and no cut is required
1629 * (irrespective of whether the variable part is integral).
1631 static int first_non_integer_row(struct isl_tab
*tab
, int *f
)
1633 int var
= next_non_integer_var(tab
, -1, f
);
1635 return var
< 0 ? -1 : tab
->var
[var
].index
;
1638 /* Add a (non-parametric) cut to cut away the non-integral sample
1639 * value of the given row.
1641 * If the row is given by
1643 * m r = f + \sum_i a_i y_i
1647 * c = - {-f/m} + \sum_i {a_i/m} y_i >= 0
1649 * The big parameter, if any, is ignored, since it is assumed to be big
1650 * enough to be divisible by any integer.
1651 * If the tableau is actually a parametric tableau, then this function
1652 * is only called when all coefficients of the parameters are integral.
1653 * The cut therefore has zero coefficients for the parameters.
1655 * The current value is known to be negative, so row_sign, if it
1656 * exists, is set accordingly.
1658 * Return the row of the cut or -1.
1660 static int add_cut(struct isl_tab
*tab
, int row
)
1665 unsigned off
= 2 + tab
->M
;
1667 if (isl_tab_extend_cons(tab
, 1) < 0)
1669 r
= isl_tab_allocate_con(tab
);
1673 r_row
= tab
->mat
->row
[tab
->con
[r
].index
];
1674 isl_int_set(r_row
[0], tab
->mat
->row
[row
][0]);
1675 isl_int_neg(r_row
[1], tab
->mat
->row
[row
][1]);
1676 isl_int_fdiv_r(r_row
[1], r_row
[1], tab
->mat
->row
[row
][0]);
1677 isl_int_neg(r_row
[1], r_row
[1]);
1679 isl_int_set_si(r_row
[2], 0);
1680 for (i
= 0; i
< tab
->n_col
; ++i
)
1681 isl_int_fdiv_r(r_row
[off
+ i
],
1682 tab
->mat
->row
[row
][off
+ i
], tab
->mat
->row
[row
][0]);
1684 tab
->con
[r
].is_nonneg
= 1;
1685 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
1688 tab
->row_sign
[tab
->con
[r
].index
] = isl_tab_row_neg
;
1690 return tab
->con
[r
].index
;
1696 /* Given a non-parametric tableau, add cuts until an integer
1697 * sample point is obtained or until the tableau is determined
1698 * to be integer infeasible.
1699 * As long as there is any non-integer value in the sample point,
1700 * we add appropriate cuts, if possible, for each of these
1701 * non-integer values and then resolve the violated
1702 * cut constraints using restore_lexmin.
1703 * If one of the corresponding rows is equal to an integral
1704 * combination of variables/constraints plus a non-integral constant,
1705 * then there is no way to obtain an integer point and we return
1706 * a tableau that is marked empty.
1707 * The parameter cutting_strategy controls the strategy used when adding cuts
1708 * to remove non-integer points. CUT_ALL adds all possible cuts
1709 * before continuing the search. CUT_ONE adds only one cut at a time.
1711 static struct isl_tab
*cut_to_integer_lexmin(struct isl_tab
*tab
,
1712 int cutting_strategy
)
1723 while ((var
= next_non_integer_var(tab
, -1, &flags
)) != -1) {
1725 if (ISL_FL_ISSET(flags
, I_VAR
)) {
1726 if (isl_tab_mark_empty(tab
) < 0)
1730 row
= tab
->var
[var
].index
;
1731 row
= add_cut(tab
, row
);
1734 if (cutting_strategy
== CUT_ONE
)
1736 } while ((var
= next_non_integer_var(tab
, var
, &flags
)) != -1);
1737 if (restore_lexmin(tab
) < 0)
1748 /* Check whether all the currently active samples also satisfy the inequality
1749 * "ineq" (treated as an equality if eq is set).
1750 * Remove those samples that do not.
1752 static struct isl_tab
*check_samples(struct isl_tab
*tab
, isl_int
*ineq
, int eq
)
1760 isl_assert(tab
->mat
->ctx
, tab
->bmap
, goto error
);
1761 isl_assert(tab
->mat
->ctx
, tab
->samples
, goto error
);
1762 isl_assert(tab
->mat
->ctx
, tab
->samples
->n_col
== 1 + tab
->n_var
, goto error
);
1765 for (i
= tab
->n_outside
; i
< tab
->n_sample
; ++i
) {
1767 isl_seq_inner_product(ineq
, tab
->samples
->row
[i
],
1768 1 + tab
->n_var
, &v
);
1769 sgn
= isl_int_sgn(v
);
1770 if (eq
? (sgn
== 0) : (sgn
>= 0))
1772 tab
= isl_tab_drop_sample(tab
, i
);
1784 /* Check whether the sample value of the tableau is finite,
1785 * i.e., either the tableau does not use a big parameter, or
1786 * all values of the variables are equal to the big parameter plus
1787 * some constant. This constant is the actual sample value.
1789 static int sample_is_finite(struct isl_tab
*tab
)
1796 for (i
= 0; i
< tab
->n_var
; ++i
) {
1798 if (!tab
->var
[i
].is_row
)
1800 row
= tab
->var
[i
].index
;
1801 if (isl_int_ne(tab
->mat
->row
[row
][0], tab
->mat
->row
[row
][2]))
1807 /* Check if the context tableau of sol has any integer points.
1808 * Leave tab in empty state if no integer point can be found.
1809 * If an integer point can be found and if moreover it is finite,
1810 * then it is added to the list of sample values.
1812 * This function is only called when none of the currently active sample
1813 * values satisfies the most recently added constraint.
1815 static struct isl_tab
*check_integer_feasible(struct isl_tab
*tab
)
1817 struct isl_tab_undo
*snap
;
1822 snap
= isl_tab_snap(tab
);
1823 if (isl_tab_push_basis(tab
) < 0)
1826 tab
= cut_to_integer_lexmin(tab
, CUT_ALL
);
1830 if (!tab
->empty
&& sample_is_finite(tab
)) {
1831 struct isl_vec
*sample
;
1833 sample
= isl_tab_get_sample_value(tab
);
1835 if (isl_tab_add_sample(tab
, sample
) < 0)
1839 if (!tab
->empty
&& isl_tab_rollback(tab
, snap
) < 0)
1848 /* Check if any of the currently active sample values satisfies
1849 * the inequality "ineq" (an equality if eq is set).
1851 static int tab_has_valid_sample(struct isl_tab
*tab
, isl_int
*ineq
, int eq
)
1859 isl_assert(tab
->mat
->ctx
, tab
->bmap
, return -1);
1860 isl_assert(tab
->mat
->ctx
, tab
->samples
, return -1);
1861 isl_assert(tab
->mat
->ctx
, tab
->samples
->n_col
== 1 + tab
->n_var
, return -1);
1864 for (i
= tab
->n_outside
; i
< tab
->n_sample
; ++i
) {
1866 isl_seq_inner_product(ineq
, tab
->samples
->row
[i
],
1867 1 + tab
->n_var
, &v
);
1868 sgn
= isl_int_sgn(v
);
1869 if (eq
? (sgn
== 0) : (sgn
>= 0))
1874 return i
< tab
->n_sample
;
1877 /* Insert a div specified by "div" to the tableau "tab" at position "pos" and
1878 * return isl_bool_true if the div is obviously non-negative.
1880 static isl_bool
context_tab_insert_div(struct isl_tab
*tab
, int pos
,
1881 __isl_keep isl_vec
*div
,
1882 int (*add_ineq
)(void *user
, isl_int
*), void *user
)
1886 struct isl_mat
*samples
;
1889 r
= isl_tab_insert_div(tab
, pos
, div
, add_ineq
, user
);
1891 return isl_bool_error
;
1892 nonneg
= tab
->var
[r
].is_nonneg
;
1893 tab
->var
[r
].frozen
= 1;
1895 samples
= isl_mat_extend(tab
->samples
,
1896 tab
->n_sample
, 1 + tab
->n_var
);
1897 tab
->samples
= samples
;
1899 return isl_bool_error
;
1900 for (i
= tab
->n_outside
; i
< samples
->n_row
; ++i
) {
1901 isl_seq_inner_product(div
->el
+ 1, samples
->row
[i
],
1902 div
->size
- 1, &samples
->row
[i
][samples
->n_col
- 1]);
1903 isl_int_fdiv_q(samples
->row
[i
][samples
->n_col
- 1],
1904 samples
->row
[i
][samples
->n_col
- 1], div
->el
[0]);
1906 tab
->samples
= isl_mat_move_cols(tab
->samples
, 1 + pos
,
1907 1 + tab
->n_var
- 1, 1);
1909 return isl_bool_error
;
1914 /* Add a div specified by "div" to both the main tableau and
1915 * the context tableau. In case of the main tableau, we only
1916 * need to add an extra div. In the context tableau, we also
1917 * need to express the meaning of the div.
1918 * Return the index of the div or -1 if anything went wrong.
1920 * The new integer division is added before any unknown integer
1921 * divisions in the context to ensure that it does not get
1922 * equated to some linear combination involving unknown integer
1925 static int add_div(struct isl_tab
*tab
, struct isl_context
*context
,
1926 __isl_keep isl_vec
*div
)
1931 struct isl_tab
*context_tab
= context
->op
->peek_tab(context
);
1933 if (!tab
|| !context_tab
)
1936 pos
= context_tab
->n_var
- context
->n_unknown
;
1937 if ((nonneg
= context
->op
->insert_div(context
, pos
, div
)) < 0)
1940 if (!context
->op
->is_ok(context
))
1943 pos
= tab
->n_var
- context
->n_unknown
;
1944 if (isl_tab_extend_vars(tab
, 1) < 0)
1946 r
= isl_tab_insert_var(tab
, pos
);
1950 tab
->var
[r
].is_nonneg
= 1;
1951 tab
->var
[r
].frozen
= 1;
1954 return tab
->n_div
- 1 - context
->n_unknown
;
1956 context
->op
->invalidate(context
);
1960 static int find_div(struct isl_tab
*tab
, isl_int
*div
, isl_int denom
)
1963 unsigned total
= isl_basic_map_total_dim(tab
->bmap
);
1965 for (i
= 0; i
< tab
->bmap
->n_div
; ++i
) {
1966 if (isl_int_ne(tab
->bmap
->div
[i
][0], denom
))
1968 if (!isl_seq_eq(tab
->bmap
->div
[i
] + 1, div
, 1 + total
))
1975 /* Return the index of a div that corresponds to "div".
1976 * We first check if we already have such a div and if not, we create one.
1978 static int get_div(struct isl_tab
*tab
, struct isl_context
*context
,
1979 struct isl_vec
*div
)
1982 struct isl_tab
*context_tab
= context
->op
->peek_tab(context
);
1987 d
= find_div(context_tab
, div
->el
+ 1, div
->el
[0]);
1991 return add_div(tab
, context
, div
);
1994 /* Add a parametric cut to cut away the non-integral sample value
1996 * Let a_i be the coefficients of the constant term and the parameters
1997 * and let b_i be the coefficients of the variables or constraints
1998 * in basis of the tableau.
1999 * Let q be the div q = floor(\sum_i {-a_i} y_i).
2001 * The cut is expressed as
2003 * c = \sum_i -{-a_i} y_i + \sum_i {b_i} x_i + q >= 0
2005 * If q did not already exist in the context tableau, then it is added first.
2006 * If q is in a column of the main tableau then the "+ q" can be accomplished
2007 * by setting the corresponding entry to the denominator of the constraint.
2008 * If q happens to be in a row of the main tableau, then the corresponding
2009 * row needs to be added instead (taking care of the denominators).
2010 * Note that this is very unlikely, but perhaps not entirely impossible.
2012 * The current value of the cut is known to be negative (or at least
2013 * non-positive), so row_sign is set accordingly.
2015 * Return the row of the cut or -1.
2017 static int add_parametric_cut(struct isl_tab
*tab
, int row
,
2018 struct isl_context
*context
)
2020 struct isl_vec
*div
;
2027 unsigned off
= 2 + tab
->M
;
2032 div
= get_row_parameter_div(tab
, row
);
2036 n
= tab
->n_div
- context
->n_unknown
;
2037 d
= context
->op
->get_div(context
, tab
, div
);
2042 if (isl_tab_extend_cons(tab
, 1) < 0)
2044 r
= isl_tab_allocate_con(tab
);
2048 r_row
= tab
->mat
->row
[tab
->con
[r
].index
];
2049 isl_int_set(r_row
[0], tab
->mat
->row
[row
][0]);
2050 isl_int_neg(r_row
[1], tab
->mat
->row
[row
][1]);
2051 isl_int_fdiv_r(r_row
[1], r_row
[1], tab
->mat
->row
[row
][0]);
2052 isl_int_neg(r_row
[1], r_row
[1]);
2054 isl_int_set_si(r_row
[2], 0);
2055 for (i
= 0; i
< tab
->n_param
; ++i
) {
2056 if (tab
->var
[i
].is_row
)
2058 col
= tab
->var
[i
].index
;
2059 isl_int_neg(r_row
[off
+ col
], tab
->mat
->row
[row
][off
+ col
]);
2060 isl_int_fdiv_r(r_row
[off
+ col
], r_row
[off
+ col
],
2061 tab
->mat
->row
[row
][0]);
2062 isl_int_neg(r_row
[off
+ col
], r_row
[off
+ col
]);
2064 for (i
= 0; i
< tab
->n_div
; ++i
) {
2065 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
2067 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
2068 isl_int_neg(r_row
[off
+ col
], tab
->mat
->row
[row
][off
+ col
]);
2069 isl_int_fdiv_r(r_row
[off
+ col
], r_row
[off
+ col
],
2070 tab
->mat
->row
[row
][0]);
2071 isl_int_neg(r_row
[off
+ col
], r_row
[off
+ col
]);
2073 for (i
= 0; i
< tab
->n_col
; ++i
) {
2074 if (tab
->col_var
[i
] >= 0 &&
2075 (tab
->col_var
[i
] < tab
->n_param
||
2076 tab
->col_var
[i
] >= tab
->n_var
- tab
->n_div
))
2078 isl_int_fdiv_r(r_row
[off
+ i
],
2079 tab
->mat
->row
[row
][off
+ i
], tab
->mat
->row
[row
][0]);
2081 if (tab
->var
[tab
->n_var
- tab
->n_div
+ d
].is_row
) {
2083 int d_row
= tab
->var
[tab
->n_var
- tab
->n_div
+ d
].index
;
2085 isl_int_gcd(gcd
, tab
->mat
->row
[d_row
][0], r_row
[0]);
2086 isl_int_divexact(r_row
[0], r_row
[0], gcd
);
2087 isl_int_divexact(gcd
, tab
->mat
->row
[d_row
][0], gcd
);
2088 isl_seq_combine(r_row
+ 1, gcd
, r_row
+ 1,
2089 r_row
[0], tab
->mat
->row
[d_row
] + 1,
2090 off
- 1 + tab
->n_col
);
2091 isl_int_mul(r_row
[0], r_row
[0], tab
->mat
->row
[d_row
][0]);
2094 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ d
].index
;
2095 isl_int_set(r_row
[off
+ col
], tab
->mat
->row
[row
][0]);
2098 tab
->con
[r
].is_nonneg
= 1;
2099 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
2102 tab
->row_sign
[tab
->con
[r
].index
] = isl_tab_row_neg
;
2104 row
= tab
->con
[r
].index
;
2106 if (d
>= n
&& context
->op
->detect_equalities(context
, tab
) < 0)
2112 /* Construct a tableau for bmap that can be used for computing
2113 * the lexicographic minimum (or maximum) of bmap.
2114 * If not NULL, then dom is the domain where the minimum
2115 * should be computed. In this case, we set up a parametric
2116 * tableau with row signs (initialized to "unknown").
2117 * If M is set, then the tableau will use a big parameter.
2118 * If max is set, then a maximum should be computed instead of a minimum.
2119 * This means that for each variable x, the tableau will contain the variable
2120 * x' = M - x, rather than x' = M + x. This in turn means that the coefficient
2121 * of the variables in all constraints are negated prior to adding them
2124 static struct isl_tab
*tab_for_lexmin(struct isl_basic_map
*bmap
,
2125 struct isl_basic_set
*dom
, unsigned M
, int max
)
2128 struct isl_tab
*tab
;
2132 tab
= isl_tab_alloc(bmap
->ctx
, 2 * bmap
->n_eq
+ bmap
->n_ineq
+ 1,
2133 isl_basic_map_total_dim(bmap
), M
);
2137 tab
->rational
= ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
2139 tab
->n_param
= isl_basic_set_total_dim(dom
) - dom
->n_div
;
2140 tab
->n_div
= dom
->n_div
;
2141 tab
->row_sign
= isl_calloc_array(bmap
->ctx
,
2142 enum isl_tab_row_sign
, tab
->mat
->n_row
);
2143 if (tab
->mat
->n_row
&& !tab
->row_sign
)
2146 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
)) {
2147 if (isl_tab_mark_empty(tab
) < 0)
2152 for (i
= tab
->n_param
; i
< tab
->n_var
- tab
->n_div
; ++i
) {
2153 tab
->var
[i
].is_nonneg
= 1;
2154 tab
->var
[i
].frozen
= 1;
2156 o_var
= 1 + tab
->n_param
;
2157 n_var
= tab
->n_var
- tab
->n_param
- tab
->n_div
;
2158 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
2160 isl_seq_neg(bmap
->eq
[i
] + o_var
,
2161 bmap
->eq
[i
] + o_var
, n_var
);
2162 tab
= add_lexmin_valid_eq(tab
, bmap
->eq
[i
]);
2164 isl_seq_neg(bmap
->eq
[i
] + o_var
,
2165 bmap
->eq
[i
] + o_var
, n_var
);
2166 if (!tab
|| tab
->empty
)
2169 if (bmap
->n_eq
&& restore_lexmin(tab
) < 0)
2171 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
2173 isl_seq_neg(bmap
->ineq
[i
] + o_var
,
2174 bmap
->ineq
[i
] + o_var
, n_var
);
2175 tab
= add_lexmin_ineq(tab
, bmap
->ineq
[i
]);
2177 isl_seq_neg(bmap
->ineq
[i
] + o_var
,
2178 bmap
->ineq
[i
] + o_var
, n_var
);
2179 if (!tab
|| tab
->empty
)
2188 /* Given a main tableau where more than one row requires a split,
2189 * determine and return the "best" row to split on.
2191 * Given two rows in the main tableau, if the inequality corresponding
2192 * to the first row is redundant with respect to that of the second row
2193 * in the current tableau, then it is better to split on the second row,
2194 * since in the positive part, both rows will be positive.
2195 * (In the negative part a pivot will have to be performed and just about
2196 * anything can happen to the sign of the other row.)
2198 * As a simple heuristic, we therefore select the row that makes the most
2199 * of the other rows redundant.
2201 * Perhaps it would also be useful to look at the number of constraints
2202 * that conflict with any given constraint.
2204 * best is the best row so far (-1 when we have not found any row yet).
2205 * best_r is the number of other rows made redundant by row best.
2206 * When best is still -1, bset_r is meaningless, but it is initialized
2207 * to some arbitrary value (0) anyway. Without this redundant initialization
2208 * valgrind may warn about uninitialized memory accesses when isl
2209 * is compiled with some versions of gcc.
2211 static int best_split(struct isl_tab
*tab
, struct isl_tab
*context_tab
)
2213 struct isl_tab_undo
*snap
;
2219 if (isl_tab_extend_cons(context_tab
, 2) < 0)
2222 snap
= isl_tab_snap(context_tab
);
2224 for (split
= tab
->n_redundant
; split
< tab
->n_row
; ++split
) {
2225 struct isl_tab_undo
*snap2
;
2226 struct isl_vec
*ineq
= NULL
;
2230 if (!isl_tab_var_from_row(tab
, split
)->is_nonneg
)
2232 if (tab
->row_sign
[split
] != isl_tab_row_any
)
2235 ineq
= get_row_parameter_ineq(tab
, split
);
2238 ok
= isl_tab_add_ineq(context_tab
, ineq
->el
) >= 0;
2243 snap2
= isl_tab_snap(context_tab
);
2245 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
2246 struct isl_tab_var
*var
;
2250 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
2252 if (tab
->row_sign
[row
] != isl_tab_row_any
)
2255 ineq
= get_row_parameter_ineq(tab
, row
);
2258 ok
= isl_tab_add_ineq(context_tab
, ineq
->el
) >= 0;
2262 var
= &context_tab
->con
[context_tab
->n_con
- 1];
2263 if (!context_tab
->empty
&&
2264 !isl_tab_min_at_most_neg_one(context_tab
, var
))
2266 if (isl_tab_rollback(context_tab
, snap2
) < 0)
2269 if (best
== -1 || r
> best_r
) {
2273 if (isl_tab_rollback(context_tab
, snap
) < 0)
2280 static struct isl_basic_set
*context_lex_peek_basic_set(
2281 struct isl_context
*context
)
2283 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2286 return isl_tab_peek_bset(clex
->tab
);
2289 static struct isl_tab
*context_lex_peek_tab(struct isl_context
*context
)
2291 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2295 static void context_lex_add_eq(struct isl_context
*context
, isl_int
*eq
,
2296 int check
, int update
)
2298 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2299 if (isl_tab_extend_cons(clex
->tab
, 2) < 0)
2301 if (add_lexmin_eq(clex
->tab
, eq
) < 0)
2304 int v
= tab_has_valid_sample(clex
->tab
, eq
, 1);
2308 clex
->tab
= check_integer_feasible(clex
->tab
);
2311 clex
->tab
= check_samples(clex
->tab
, eq
, 1);
2314 isl_tab_free(clex
->tab
);
2318 static void context_lex_add_ineq(struct isl_context
*context
, isl_int
*ineq
,
2319 int check
, int update
)
2321 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2322 if (isl_tab_extend_cons(clex
->tab
, 1) < 0)
2324 clex
->tab
= add_lexmin_ineq(clex
->tab
, ineq
);
2326 int v
= tab_has_valid_sample(clex
->tab
, ineq
, 0);
2330 clex
->tab
= check_integer_feasible(clex
->tab
);
2333 clex
->tab
= check_samples(clex
->tab
, ineq
, 0);
2336 isl_tab_free(clex
->tab
);
2340 static int context_lex_add_ineq_wrap(void *user
, isl_int
*ineq
)
2342 struct isl_context
*context
= (struct isl_context
*)user
;
2343 context_lex_add_ineq(context
, ineq
, 0, 0);
2344 return context
->op
->is_ok(context
) ? 0 : -1;
2347 /* Check which signs can be obtained by "ineq" on all the currently
2348 * active sample values. See row_sign for more information.
2350 static enum isl_tab_row_sign
tab_ineq_sign(struct isl_tab
*tab
, isl_int
*ineq
,
2356 enum isl_tab_row_sign res
= isl_tab_row_unknown
;
2358 isl_assert(tab
->mat
->ctx
, tab
->samples
, return isl_tab_row_unknown
);
2359 isl_assert(tab
->mat
->ctx
, tab
->samples
->n_col
== 1 + tab
->n_var
,
2360 return isl_tab_row_unknown
);
2363 for (i
= tab
->n_outside
; i
< tab
->n_sample
; ++i
) {
2364 isl_seq_inner_product(tab
->samples
->row
[i
], ineq
,
2365 1 + tab
->n_var
, &tmp
);
2366 sgn
= isl_int_sgn(tmp
);
2367 if (sgn
> 0 || (sgn
== 0 && strict
)) {
2368 if (res
== isl_tab_row_unknown
)
2369 res
= isl_tab_row_pos
;
2370 if (res
== isl_tab_row_neg
)
2371 res
= isl_tab_row_any
;
2374 if (res
== isl_tab_row_unknown
)
2375 res
= isl_tab_row_neg
;
2376 if (res
== isl_tab_row_pos
)
2377 res
= isl_tab_row_any
;
2379 if (res
== isl_tab_row_any
)
2387 static enum isl_tab_row_sign
context_lex_ineq_sign(struct isl_context
*context
,
2388 isl_int
*ineq
, int strict
)
2390 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2391 return tab_ineq_sign(clex
->tab
, ineq
, strict
);
2394 /* Check whether "ineq" can be added to the tableau without rendering
2397 static int context_lex_test_ineq(struct isl_context
*context
, isl_int
*ineq
)
2399 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2400 struct isl_tab_undo
*snap
;
2406 if (isl_tab_extend_cons(clex
->tab
, 1) < 0)
2409 snap
= isl_tab_snap(clex
->tab
);
2410 if (isl_tab_push_basis(clex
->tab
) < 0)
2412 clex
->tab
= add_lexmin_ineq(clex
->tab
, ineq
);
2413 clex
->tab
= check_integer_feasible(clex
->tab
);
2416 feasible
= !clex
->tab
->empty
;
2417 if (isl_tab_rollback(clex
->tab
, snap
) < 0)
2423 static int context_lex_get_div(struct isl_context
*context
, struct isl_tab
*tab
,
2424 struct isl_vec
*div
)
2426 return get_div(tab
, context
, div
);
2429 /* Insert a div specified by "div" to the context tableau at position "pos" and
2430 * return isl_bool_true if the div is obviously non-negative.
2431 * context_tab_add_div will always return isl_bool_true, because all variables
2432 * in a isl_context_lex tableau are non-negative.
2433 * However, if we are using a big parameter in the context, then this only
2434 * reflects the non-negativity of the variable used to _encode_ the
2435 * div, i.e., div' = M + div, so we can't draw any conclusions.
2437 static isl_bool
context_lex_insert_div(struct isl_context
*context
, int pos
,
2438 __isl_keep isl_vec
*div
)
2440 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2442 nonneg
= context_tab_insert_div(clex
->tab
, pos
, div
,
2443 context_lex_add_ineq_wrap
, context
);
2445 return isl_bool_error
;
2447 return isl_bool_false
;
2451 static int context_lex_detect_equalities(struct isl_context
*context
,
2452 struct isl_tab
*tab
)
2457 static int context_lex_best_split(struct isl_context
*context
,
2458 struct isl_tab
*tab
)
2460 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2461 struct isl_tab_undo
*snap
;
2464 snap
= isl_tab_snap(clex
->tab
);
2465 if (isl_tab_push_basis(clex
->tab
) < 0)
2467 r
= best_split(tab
, clex
->tab
);
2469 if (r
>= 0 && isl_tab_rollback(clex
->tab
, snap
) < 0)
2475 static int context_lex_is_empty(struct isl_context
*context
)
2477 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2480 return clex
->tab
->empty
;
2483 static void *context_lex_save(struct isl_context
*context
)
2485 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2486 struct isl_tab_undo
*snap
;
2488 snap
= isl_tab_snap(clex
->tab
);
2489 if (isl_tab_push_basis(clex
->tab
) < 0)
2491 if (isl_tab_save_samples(clex
->tab
) < 0)
2497 static void context_lex_restore(struct isl_context
*context
, void *save
)
2499 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2500 if (isl_tab_rollback(clex
->tab
, (struct isl_tab_undo
*)save
) < 0) {
2501 isl_tab_free(clex
->tab
);
2506 static void context_lex_discard(void *save
)
2510 static int context_lex_is_ok(struct isl_context
*context
)
2512 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2516 /* For each variable in the context tableau, check if the variable can
2517 * only attain non-negative values. If so, mark the parameter as non-negative
2518 * in the main tableau. This allows for a more direct identification of some
2519 * cases of violated constraints.
2521 static struct isl_tab
*tab_detect_nonnegative_parameters(struct isl_tab
*tab
,
2522 struct isl_tab
*context_tab
)
2525 struct isl_tab_undo
*snap
;
2526 struct isl_vec
*ineq
= NULL
;
2527 struct isl_tab_var
*var
;
2530 if (context_tab
->n_var
== 0)
2533 ineq
= isl_vec_alloc(tab
->mat
->ctx
, 1 + context_tab
->n_var
);
2537 if (isl_tab_extend_cons(context_tab
, 1) < 0)
2540 snap
= isl_tab_snap(context_tab
);
2543 isl_seq_clr(ineq
->el
, ineq
->size
);
2544 for (i
= 0; i
< context_tab
->n_var
; ++i
) {
2545 isl_int_set_si(ineq
->el
[1 + i
], 1);
2546 if (isl_tab_add_ineq(context_tab
, ineq
->el
) < 0)
2548 var
= &context_tab
->con
[context_tab
->n_con
- 1];
2549 if (!context_tab
->empty
&&
2550 !isl_tab_min_at_most_neg_one(context_tab
, var
)) {
2552 if (i
>= tab
->n_param
)
2553 j
= i
- tab
->n_param
+ tab
->n_var
- tab
->n_div
;
2554 tab
->var
[j
].is_nonneg
= 1;
2557 isl_int_set_si(ineq
->el
[1 + i
], 0);
2558 if (isl_tab_rollback(context_tab
, snap
) < 0)
2562 if (context_tab
->M
&& n
== context_tab
->n_var
) {
2563 context_tab
->mat
= isl_mat_drop_cols(context_tab
->mat
, 2, 1);
2575 static struct isl_tab
*context_lex_detect_nonnegative_parameters(
2576 struct isl_context
*context
, struct isl_tab
*tab
)
2578 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2579 struct isl_tab_undo
*snap
;
2584 snap
= isl_tab_snap(clex
->tab
);
2585 if (isl_tab_push_basis(clex
->tab
) < 0)
2588 tab
= tab_detect_nonnegative_parameters(tab
, clex
->tab
);
2590 if (isl_tab_rollback(clex
->tab
, snap
) < 0)
2599 static void context_lex_invalidate(struct isl_context
*context
)
2601 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2602 isl_tab_free(clex
->tab
);
2606 static __isl_null
struct isl_context
*context_lex_free(
2607 struct isl_context
*context
)
2609 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2610 isl_tab_free(clex
->tab
);
2616 struct isl_context_op isl_context_lex_op
= {
2617 context_lex_detect_nonnegative_parameters
,
2618 context_lex_peek_basic_set
,
2619 context_lex_peek_tab
,
2621 context_lex_add_ineq
,
2622 context_lex_ineq_sign
,
2623 context_lex_test_ineq
,
2624 context_lex_get_div
,
2625 context_lex_insert_div
,
2626 context_lex_detect_equalities
,
2627 context_lex_best_split
,
2628 context_lex_is_empty
,
2631 context_lex_restore
,
2632 context_lex_discard
,
2633 context_lex_invalidate
,
2637 static struct isl_tab
*context_tab_for_lexmin(struct isl_basic_set
*bset
)
2639 struct isl_tab
*tab
;
2643 tab
= tab_for_lexmin(bset_to_bmap(bset
), NULL
, 1, 0);
2646 if (isl_tab_track_bset(tab
, bset
) < 0)
2648 tab
= isl_tab_init_samples(tab
);
2651 isl_basic_set_free(bset
);
2655 static struct isl_context
*isl_context_lex_alloc(struct isl_basic_set
*dom
)
2657 struct isl_context_lex
*clex
;
2662 clex
= isl_alloc_type(dom
->ctx
, struct isl_context_lex
);
2666 clex
->context
.op
= &isl_context_lex_op
;
2668 clex
->tab
= context_tab_for_lexmin(isl_basic_set_copy(dom
));
2669 if (restore_lexmin(clex
->tab
) < 0)
2671 clex
->tab
= check_integer_feasible(clex
->tab
);
2675 return &clex
->context
;
2677 clex
->context
.op
->free(&clex
->context
);
2681 /* Representation of the context when using generalized basis reduction.
2683 * "shifted" contains the offsets of the unit hypercubes that lie inside the
2684 * context. Any rational point in "shifted" can therefore be rounded
2685 * up to an integer point in the context.
2686 * If the context is constrained by any equality, then "shifted" is not used
2687 * as it would be empty.
2689 struct isl_context_gbr
{
2690 struct isl_context context
;
2691 struct isl_tab
*tab
;
2692 struct isl_tab
*shifted
;
2693 struct isl_tab
*cone
;
2696 static struct isl_tab
*context_gbr_detect_nonnegative_parameters(
2697 struct isl_context
*context
, struct isl_tab
*tab
)
2699 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2702 return tab_detect_nonnegative_parameters(tab
, cgbr
->tab
);
2705 static struct isl_basic_set
*context_gbr_peek_basic_set(
2706 struct isl_context
*context
)
2708 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2711 return isl_tab_peek_bset(cgbr
->tab
);
2714 static struct isl_tab
*context_gbr_peek_tab(struct isl_context
*context
)
2716 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2720 /* Initialize the "shifted" tableau of the context, which
2721 * contains the constraints of the original tableau shifted
2722 * by the sum of all negative coefficients. This ensures
2723 * that any rational point in the shifted tableau can
2724 * be rounded up to yield an integer point in the original tableau.
2726 static void gbr_init_shifted(struct isl_context_gbr
*cgbr
)
2729 struct isl_vec
*cst
;
2730 struct isl_basic_set
*bset
= isl_tab_peek_bset(cgbr
->tab
);
2731 unsigned dim
= isl_basic_set_total_dim(bset
);
2733 cst
= isl_vec_alloc(cgbr
->tab
->mat
->ctx
, bset
->n_ineq
);
2737 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
2738 isl_int_set(cst
->el
[i
], bset
->ineq
[i
][0]);
2739 for (j
= 0; j
< dim
; ++j
) {
2740 if (!isl_int_is_neg(bset
->ineq
[i
][1 + j
]))
2742 isl_int_add(bset
->ineq
[i
][0], bset
->ineq
[i
][0],
2743 bset
->ineq
[i
][1 + j
]);
2747 cgbr
->shifted
= isl_tab_from_basic_set(bset
, 0);
2749 for (i
= 0; i
< bset
->n_ineq
; ++i
)
2750 isl_int_set(bset
->ineq
[i
][0], cst
->el
[i
]);
2755 /* Check if the shifted tableau is non-empty, and if so
2756 * use the sample point to construct an integer point
2757 * of the context tableau.
2759 static struct isl_vec
*gbr_get_shifted_sample(struct isl_context_gbr
*cgbr
)
2761 struct isl_vec
*sample
;
2764 gbr_init_shifted(cgbr
);
2767 if (cgbr
->shifted
->empty
)
2768 return isl_vec_alloc(cgbr
->tab
->mat
->ctx
, 0);
2770 sample
= isl_tab_get_sample_value(cgbr
->shifted
);
2771 sample
= isl_vec_ceil(sample
);
2776 static struct isl_basic_set
*drop_constant_terms(struct isl_basic_set
*bset
)
2783 for (i
= 0; i
< bset
->n_eq
; ++i
)
2784 isl_int_set_si(bset
->eq
[i
][0], 0);
2786 for (i
= 0; i
< bset
->n_ineq
; ++i
)
2787 isl_int_set_si(bset
->ineq
[i
][0], 0);
2792 static int use_shifted(struct isl_context_gbr
*cgbr
)
2796 return cgbr
->tab
->bmap
->n_eq
== 0 && cgbr
->tab
->bmap
->n_div
== 0;
2799 static struct isl_vec
*gbr_get_sample(struct isl_context_gbr
*cgbr
)
2801 struct isl_basic_set
*bset
;
2802 struct isl_basic_set
*cone
;
2804 if (isl_tab_sample_is_integer(cgbr
->tab
))
2805 return isl_tab_get_sample_value(cgbr
->tab
);
2807 if (use_shifted(cgbr
)) {
2808 struct isl_vec
*sample
;
2810 sample
= gbr_get_shifted_sample(cgbr
);
2811 if (!sample
|| sample
->size
> 0)
2814 isl_vec_free(sample
);
2818 bset
= isl_tab_peek_bset(cgbr
->tab
);
2819 cgbr
->cone
= isl_tab_from_recession_cone(bset
, 0);
2822 if (isl_tab_track_bset(cgbr
->cone
,
2823 isl_basic_set_copy(bset
)) < 0)
2826 if (isl_tab_detect_implicit_equalities(cgbr
->cone
) < 0)
2829 if (cgbr
->cone
->n_dead
== cgbr
->cone
->n_col
) {
2830 struct isl_vec
*sample
;
2831 struct isl_tab_undo
*snap
;
2833 if (cgbr
->tab
->basis
) {
2834 if (cgbr
->tab
->basis
->n_col
!= 1 + cgbr
->tab
->n_var
) {
2835 isl_mat_free(cgbr
->tab
->basis
);
2836 cgbr
->tab
->basis
= NULL
;
2838 cgbr
->tab
->n_zero
= 0;
2839 cgbr
->tab
->n_unbounded
= 0;
2842 snap
= isl_tab_snap(cgbr
->tab
);
2844 sample
= isl_tab_sample(cgbr
->tab
);
2846 if (!sample
|| isl_tab_rollback(cgbr
->tab
, snap
) < 0) {
2847 isl_vec_free(sample
);
2854 cone
= isl_basic_set_dup(isl_tab_peek_bset(cgbr
->cone
));
2855 cone
= drop_constant_terms(cone
);
2856 cone
= isl_basic_set_update_from_tab(cone
, cgbr
->cone
);
2857 cone
= isl_basic_set_underlying_set(cone
);
2858 cone
= isl_basic_set_gauss(cone
, NULL
);
2860 bset
= isl_basic_set_dup(isl_tab_peek_bset(cgbr
->tab
));
2861 bset
= isl_basic_set_update_from_tab(bset
, cgbr
->tab
);
2862 bset
= isl_basic_set_underlying_set(bset
);
2863 bset
= isl_basic_set_gauss(bset
, NULL
);
2865 return isl_basic_set_sample_with_cone(bset
, cone
);
2868 static void check_gbr_integer_feasible(struct isl_context_gbr
*cgbr
)
2870 struct isl_vec
*sample
;
2875 if (cgbr
->tab
->empty
)
2878 sample
= gbr_get_sample(cgbr
);
2882 if (sample
->size
== 0) {
2883 isl_vec_free(sample
);
2884 if (isl_tab_mark_empty(cgbr
->tab
) < 0)
2889 if (isl_tab_add_sample(cgbr
->tab
, sample
) < 0)
2894 isl_tab_free(cgbr
->tab
);
2898 static struct isl_tab
*add_gbr_eq(struct isl_tab
*tab
, isl_int
*eq
)
2903 if (isl_tab_extend_cons(tab
, 2) < 0)
2906 if (isl_tab_add_eq(tab
, eq
) < 0)
2915 /* Add the equality described by "eq" to the context.
2916 * If "check" is set, then we check if the context is empty after
2917 * adding the equality.
2918 * If "update" is set, then we check if the samples are still valid.
2920 * We do not explicitly add shifted copies of the equality to
2921 * cgbr->shifted since they would conflict with each other.
2922 * Instead, we directly mark cgbr->shifted empty.
2924 static void context_gbr_add_eq(struct isl_context
*context
, isl_int
*eq
,
2925 int check
, int update
)
2927 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2929 cgbr
->tab
= add_gbr_eq(cgbr
->tab
, eq
);
2931 if (cgbr
->shifted
&& !cgbr
->shifted
->empty
&& use_shifted(cgbr
)) {
2932 if (isl_tab_mark_empty(cgbr
->shifted
) < 0)
2936 if (cgbr
->cone
&& cgbr
->cone
->n_col
!= cgbr
->cone
->n_dead
) {
2937 if (isl_tab_extend_cons(cgbr
->cone
, 2) < 0)
2939 if (isl_tab_add_eq(cgbr
->cone
, eq
) < 0)
2944 int v
= tab_has_valid_sample(cgbr
->tab
, eq
, 1);
2948 check_gbr_integer_feasible(cgbr
);
2951 cgbr
->tab
= check_samples(cgbr
->tab
, eq
, 1);
2954 isl_tab_free(cgbr
->tab
);
2958 static void add_gbr_ineq(struct isl_context_gbr
*cgbr
, isl_int
*ineq
)
2963 if (isl_tab_extend_cons(cgbr
->tab
, 1) < 0)
2966 if (isl_tab_add_ineq(cgbr
->tab
, ineq
) < 0)
2969 if (cgbr
->shifted
&& !cgbr
->shifted
->empty
&& use_shifted(cgbr
)) {
2972 dim
= isl_basic_map_total_dim(cgbr
->tab
->bmap
);
2974 if (isl_tab_extend_cons(cgbr
->shifted
, 1) < 0)
2977 for (i
= 0; i
< dim
; ++i
) {
2978 if (!isl_int_is_neg(ineq
[1 + i
]))
2980 isl_int_add(ineq
[0], ineq
[0], ineq
[1 + i
]);
2983 if (isl_tab_add_ineq(cgbr
->shifted
, ineq
) < 0)
2986 for (i
= 0; i
< dim
; ++i
) {
2987 if (!isl_int_is_neg(ineq
[1 + i
]))
2989 isl_int_sub(ineq
[0], ineq
[0], ineq
[1 + i
]);
2993 if (cgbr
->cone
&& cgbr
->cone
->n_col
!= cgbr
->cone
->n_dead
) {
2994 if (isl_tab_extend_cons(cgbr
->cone
, 1) < 0)
2996 if (isl_tab_add_ineq(cgbr
->cone
, ineq
) < 0)
3002 isl_tab_free(cgbr
->tab
);
3006 static void context_gbr_add_ineq(struct isl_context
*context
, isl_int
*ineq
,
3007 int check
, int update
)
3009 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3011 add_gbr_ineq(cgbr
, ineq
);
3016 int v
= tab_has_valid_sample(cgbr
->tab
, ineq
, 0);
3020 check_gbr_integer_feasible(cgbr
);
3023 cgbr
->tab
= check_samples(cgbr
->tab
, ineq
, 0);
3026 isl_tab_free(cgbr
->tab
);
3030 static int context_gbr_add_ineq_wrap(void *user
, isl_int
*ineq
)
3032 struct isl_context
*context
= (struct isl_context
*)user
;
3033 context_gbr_add_ineq(context
, ineq
, 0, 0);
3034 return context
->op
->is_ok(context
) ? 0 : -1;
3037 static enum isl_tab_row_sign
context_gbr_ineq_sign(struct isl_context
*context
,
3038 isl_int
*ineq
, int strict
)
3040 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3041 return tab_ineq_sign(cgbr
->tab
, ineq
, strict
);
3044 /* Check whether "ineq" can be added to the tableau without rendering
3047 static int context_gbr_test_ineq(struct isl_context
*context
, isl_int
*ineq
)
3049 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3050 struct isl_tab_undo
*snap
;
3051 struct isl_tab_undo
*shifted_snap
= NULL
;
3052 struct isl_tab_undo
*cone_snap
= NULL
;
3058 if (isl_tab_extend_cons(cgbr
->tab
, 1) < 0)
3061 snap
= isl_tab_snap(cgbr
->tab
);
3063 shifted_snap
= isl_tab_snap(cgbr
->shifted
);
3065 cone_snap
= isl_tab_snap(cgbr
->cone
);
3066 add_gbr_ineq(cgbr
, ineq
);
3067 check_gbr_integer_feasible(cgbr
);
3070 feasible
= !cgbr
->tab
->empty
;
3071 if (isl_tab_rollback(cgbr
->tab
, snap
) < 0)
3074 if (isl_tab_rollback(cgbr
->shifted
, shifted_snap
))
3076 } else if (cgbr
->shifted
) {
3077 isl_tab_free(cgbr
->shifted
);
3078 cgbr
->shifted
= NULL
;
3081 if (isl_tab_rollback(cgbr
->cone
, cone_snap
))
3083 } else if (cgbr
->cone
) {
3084 isl_tab_free(cgbr
->cone
);
3091 /* Return the column of the last of the variables associated to
3092 * a column that has a non-zero coefficient.
3093 * This function is called in a context where only coefficients
3094 * of parameters or divs can be non-zero.
3096 static int last_non_zero_var_col(struct isl_tab
*tab
, isl_int
*p
)
3101 if (tab
->n_var
== 0)
3104 for (i
= tab
->n_var
- 1; i
>= 0; --i
) {
3105 if (i
>= tab
->n_param
&& i
< tab
->n_var
- tab
->n_div
)
3107 if (tab
->var
[i
].is_row
)
3109 col
= tab
->var
[i
].index
;
3110 if (!isl_int_is_zero(p
[col
]))
3117 /* Look through all the recently added equalities in the context
3118 * to see if we can propagate any of them to the main tableau.
3120 * The newly added equalities in the context are encoded as pairs
3121 * of inequalities starting at inequality "first".
3123 * We tentatively add each of these equalities to the main tableau
3124 * and if this happens to result in a row with a final coefficient
3125 * that is one or negative one, we use it to kill a column
3126 * in the main tableau. Otherwise, we discard the tentatively
3128 * This tentative addition of equality constraints turns
3129 * on the undo facility of the tableau. Turn it off again
3130 * at the end, assuming it was turned off to begin with.
3132 * Return 0 on success and -1 on failure.
3134 static int propagate_equalities(struct isl_context_gbr
*cgbr
,
3135 struct isl_tab
*tab
, unsigned first
)
3138 struct isl_vec
*eq
= NULL
;
3139 isl_bool needs_undo
;
3141 needs_undo
= isl_tab_need_undo(tab
);
3144 eq
= isl_vec_alloc(tab
->mat
->ctx
, 1 + tab
->n_var
);
3148 if (isl_tab_extend_cons(tab
, (cgbr
->tab
->bmap
->n_ineq
- first
)/2) < 0)
3151 isl_seq_clr(eq
->el
+ 1 + tab
->n_param
,
3152 tab
->n_var
- tab
->n_param
- tab
->n_div
);
3153 for (i
= first
; i
< cgbr
->tab
->bmap
->n_ineq
; i
+= 2) {
3156 struct isl_tab_undo
*snap
;
3157 snap
= isl_tab_snap(tab
);
3159 isl_seq_cpy(eq
->el
, cgbr
->tab
->bmap
->ineq
[i
], 1 + tab
->n_param
);
3160 isl_seq_cpy(eq
->el
+ 1 + tab
->n_var
- tab
->n_div
,
3161 cgbr
->tab
->bmap
->ineq
[i
] + 1 + tab
->n_param
,
3164 r
= isl_tab_add_row(tab
, eq
->el
);
3167 r
= tab
->con
[r
].index
;
3168 j
= last_non_zero_var_col(tab
, tab
->mat
->row
[r
] + 2 + tab
->M
);
3169 if (j
< 0 || j
< tab
->n_dead
||
3170 !isl_int_is_one(tab
->mat
->row
[r
][0]) ||
3171 (!isl_int_is_one(tab
->mat
->row
[r
][2 + tab
->M
+ j
]) &&
3172 !isl_int_is_negone(tab
->mat
->row
[r
][2 + tab
->M
+ j
]))) {
3173 if (isl_tab_rollback(tab
, snap
) < 0)
3177 if (isl_tab_pivot(tab
, r
, j
) < 0)
3179 if (isl_tab_kill_col(tab
, j
) < 0)
3182 if (restore_lexmin(tab
) < 0)
3187 isl_tab_clear_undo(tab
);
3193 isl_tab_free(cgbr
->tab
);
3198 static int context_gbr_detect_equalities(struct isl_context
*context
,
3199 struct isl_tab
*tab
)
3201 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3205 struct isl_basic_set
*bset
= isl_tab_peek_bset(cgbr
->tab
);
3206 cgbr
->cone
= isl_tab_from_recession_cone(bset
, 0);
3209 if (isl_tab_track_bset(cgbr
->cone
,
3210 isl_basic_set_copy(bset
)) < 0)
3213 if (isl_tab_detect_implicit_equalities(cgbr
->cone
) < 0)
3216 n_ineq
= cgbr
->tab
->bmap
->n_ineq
;
3217 cgbr
->tab
= isl_tab_detect_equalities(cgbr
->tab
, cgbr
->cone
);
3220 if (cgbr
->tab
->bmap
->n_ineq
> n_ineq
&&
3221 propagate_equalities(cgbr
, tab
, n_ineq
) < 0)
3226 isl_tab_free(cgbr
->tab
);
3231 static int context_gbr_get_div(struct isl_context
*context
, struct isl_tab
*tab
,
3232 struct isl_vec
*div
)
3234 return get_div(tab
, context
, div
);
3237 static isl_bool
context_gbr_insert_div(struct isl_context
*context
, int pos
,
3238 __isl_keep isl_vec
*div
)
3240 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3242 int r
, n_div
, o_div
;
3244 n_div
= isl_basic_map_dim(cgbr
->cone
->bmap
, isl_dim_div
);
3245 o_div
= cgbr
->cone
->n_var
- n_div
;
3247 if (isl_tab_extend_cons(cgbr
->cone
, 3) < 0)
3248 return isl_bool_error
;
3249 if (isl_tab_extend_vars(cgbr
->cone
, 1) < 0)
3250 return isl_bool_error
;
3251 if ((r
= isl_tab_insert_var(cgbr
->cone
, pos
)) <0)
3252 return isl_bool_error
;
3254 cgbr
->cone
->bmap
= isl_basic_map_insert_div(cgbr
->cone
->bmap
,
3256 if (!cgbr
->cone
->bmap
)
3257 return isl_bool_error
;
3258 if (isl_tab_push_var(cgbr
->cone
, isl_tab_undo_bmap_div
,
3259 &cgbr
->cone
->var
[r
]) < 0)
3260 return isl_bool_error
;
3262 return context_tab_insert_div(cgbr
->tab
, pos
, div
,
3263 context_gbr_add_ineq_wrap
, context
);
3266 static int context_gbr_best_split(struct isl_context
*context
,
3267 struct isl_tab
*tab
)
3269 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3270 struct isl_tab_undo
*snap
;
3273 snap
= isl_tab_snap(cgbr
->tab
);
3274 r
= best_split(tab
, cgbr
->tab
);
3276 if (r
>= 0 && isl_tab_rollback(cgbr
->tab
, snap
) < 0)
3282 static int context_gbr_is_empty(struct isl_context
*context
)
3284 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3287 return cgbr
->tab
->empty
;
3290 struct isl_gbr_tab_undo
{
3291 struct isl_tab_undo
*tab_snap
;
3292 struct isl_tab_undo
*shifted_snap
;
3293 struct isl_tab_undo
*cone_snap
;
3296 static void *context_gbr_save(struct isl_context
*context
)
3298 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3299 struct isl_gbr_tab_undo
*snap
;
3304 snap
= isl_alloc_type(cgbr
->tab
->mat
->ctx
, struct isl_gbr_tab_undo
);
3308 snap
->tab_snap
= isl_tab_snap(cgbr
->tab
);
3309 if (isl_tab_save_samples(cgbr
->tab
) < 0)
3313 snap
->shifted_snap
= isl_tab_snap(cgbr
->shifted
);
3315 snap
->shifted_snap
= NULL
;
3318 snap
->cone_snap
= isl_tab_snap(cgbr
->cone
);
3320 snap
->cone_snap
= NULL
;
3328 static void context_gbr_restore(struct isl_context
*context
, void *save
)
3330 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3331 struct isl_gbr_tab_undo
*snap
= (struct isl_gbr_tab_undo
*)save
;
3334 if (isl_tab_rollback(cgbr
->tab
, snap
->tab_snap
) < 0)
3337 if (snap
->shifted_snap
) {
3338 if (isl_tab_rollback(cgbr
->shifted
, snap
->shifted_snap
) < 0)
3340 } else if (cgbr
->shifted
) {
3341 isl_tab_free(cgbr
->shifted
);
3342 cgbr
->shifted
= NULL
;
3345 if (snap
->cone_snap
) {
3346 if (isl_tab_rollback(cgbr
->cone
, snap
->cone_snap
) < 0)
3348 } else if (cgbr
->cone
) {
3349 isl_tab_free(cgbr
->cone
);
3358 isl_tab_free(cgbr
->tab
);
3362 static void context_gbr_discard(void *save
)
3364 struct isl_gbr_tab_undo
*snap
= (struct isl_gbr_tab_undo
*)save
;
3368 static int context_gbr_is_ok(struct isl_context
*context
)
3370 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3374 static void context_gbr_invalidate(struct isl_context
*context
)
3376 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3377 isl_tab_free(cgbr
->tab
);
3381 static __isl_null
struct isl_context
*context_gbr_free(
3382 struct isl_context
*context
)
3384 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3385 isl_tab_free(cgbr
->tab
);
3386 isl_tab_free(cgbr
->shifted
);
3387 isl_tab_free(cgbr
->cone
);
3393 struct isl_context_op isl_context_gbr_op
= {
3394 context_gbr_detect_nonnegative_parameters
,
3395 context_gbr_peek_basic_set
,
3396 context_gbr_peek_tab
,
3398 context_gbr_add_ineq
,
3399 context_gbr_ineq_sign
,
3400 context_gbr_test_ineq
,
3401 context_gbr_get_div
,
3402 context_gbr_insert_div
,
3403 context_gbr_detect_equalities
,
3404 context_gbr_best_split
,
3405 context_gbr_is_empty
,
3408 context_gbr_restore
,
3409 context_gbr_discard
,
3410 context_gbr_invalidate
,
3414 static struct isl_context
*isl_context_gbr_alloc(__isl_keep isl_basic_set
*dom
)
3416 struct isl_context_gbr
*cgbr
;
3421 cgbr
= isl_calloc_type(dom
->ctx
, struct isl_context_gbr
);
3425 cgbr
->context
.op
= &isl_context_gbr_op
;
3427 cgbr
->shifted
= NULL
;
3429 cgbr
->tab
= isl_tab_from_basic_set(dom
, 1);
3430 cgbr
->tab
= isl_tab_init_samples(cgbr
->tab
);
3433 check_gbr_integer_feasible(cgbr
);
3435 return &cgbr
->context
;
3437 cgbr
->context
.op
->free(&cgbr
->context
);
3441 /* Allocate a context corresponding to "dom".
3442 * The representation specific fields are initialized by
3443 * isl_context_lex_alloc or isl_context_gbr_alloc.
3444 * The shared "n_unknown" field is initialized to the number
3445 * of final unknown integer divisions in "dom".
3447 static struct isl_context
*isl_context_alloc(__isl_keep isl_basic_set
*dom
)
3449 struct isl_context
*context
;
3455 if (dom
->ctx
->opt
->context
== ISL_CONTEXT_LEXMIN
)
3456 context
= isl_context_lex_alloc(dom
);
3458 context
= isl_context_gbr_alloc(dom
);
3463 first
= isl_basic_set_first_unknown_div(dom
);
3465 return context
->op
->free(context
);
3466 context
->n_unknown
= isl_basic_set_dim(dom
, isl_dim_div
) - first
;
3471 /* Construct an isl_sol_map structure for accumulating the solution.
3472 * If track_empty is set, then we also keep track of the parts
3473 * of the context where there is no solution.
3474 * If max is set, then we are solving a maximization, rather than
3475 * a minimization problem, which means that the variables in the
3476 * tableau have value "M - x" rather than "M + x".
3478 static struct isl_sol
*sol_map_init(__isl_keep isl_basic_map
*bmap
,
3479 __isl_take isl_basic_set
*dom
, int track_empty
, int max
)
3481 struct isl_sol_map
*sol_map
= NULL
;
3486 sol_map
= isl_calloc_type(bmap
->ctx
, struct isl_sol_map
);
3490 sol_map
->sol
.rational
= ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
3491 sol_map
->sol
.dec_level
.callback
.run
= &sol_dec_level_wrap
;
3492 sol_map
->sol
.dec_level
.sol
= &sol_map
->sol
;
3493 sol_map
->sol
.max
= max
;
3494 sol_map
->sol
.n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
3495 sol_map
->sol
.add
= &sol_map_add_wrap
;
3496 sol_map
->sol
.add_empty
= track_empty
? &sol_map_add_empty_wrap
: NULL
;
3497 sol_map
->sol
.free
= &sol_map_free_wrap
;
3498 sol_map
->map
= isl_map_alloc_space(isl_basic_map_get_space(bmap
), 1,
3503 sol_map
->sol
.context
= isl_context_alloc(dom
);
3504 if (!sol_map
->sol
.context
)
3508 sol_map
->empty
= isl_set_alloc_space(isl_basic_set_get_space(dom
),
3509 1, ISL_SET_DISJOINT
);
3510 if (!sol_map
->empty
)
3514 isl_basic_set_free(dom
);
3515 return &sol_map
->sol
;
3517 isl_basic_set_free(dom
);
3518 sol_map_free(sol_map
);
3522 /* Check whether all coefficients of (non-parameter) variables
3523 * are non-positive, meaning that no pivots can be performed on the row.
3525 static int is_critical(struct isl_tab
*tab
, int row
)
3528 unsigned off
= 2 + tab
->M
;
3530 for (j
= tab
->n_dead
; j
< tab
->n_col
; ++j
) {
3531 if (tab
->col_var
[j
] >= 0 &&
3532 (tab
->col_var
[j
] < tab
->n_param
||
3533 tab
->col_var
[j
] >= tab
->n_var
- tab
->n_div
))
3536 if (isl_int_is_pos(tab
->mat
->row
[row
][off
+ j
]))
3543 /* Check whether the inequality represented by vec is strict over the integers,
3544 * i.e., there are no integer values satisfying the constraint with
3545 * equality. This happens if the gcd of the coefficients is not a divisor
3546 * of the constant term. If so, scale the constraint down by the gcd
3547 * of the coefficients.
3549 static int is_strict(struct isl_vec
*vec
)
3555 isl_seq_gcd(vec
->el
+ 1, vec
->size
- 1, &gcd
);
3556 if (!isl_int_is_one(gcd
)) {
3557 strict
= !isl_int_is_divisible_by(vec
->el
[0], gcd
);
3558 isl_int_fdiv_q(vec
->el
[0], vec
->el
[0], gcd
);
3559 isl_seq_scale_down(vec
->el
+ 1, vec
->el
+ 1, gcd
, vec
->size
-1);
3566 /* Determine the sign of the given row of the main tableau.
3567 * The result is one of
3568 * isl_tab_row_pos: always non-negative; no pivot needed
3569 * isl_tab_row_neg: always non-positive; pivot
3570 * isl_tab_row_any: can be both positive and negative; split
3572 * We first handle some simple cases
3573 * - the row sign may be known already
3574 * - the row may be obviously non-negative
3575 * - the parametric constant may be equal to that of another row
3576 * for which we know the sign. This sign will be either "pos" or
3577 * "any". If it had been "neg" then we would have pivoted before.
3579 * If none of these cases hold, we check the value of the row for each
3580 * of the currently active samples. Based on the signs of these values
3581 * we make an initial determination of the sign of the row.
3583 * all zero -> unk(nown)
3584 * all non-negative -> pos
3585 * all non-positive -> neg
3586 * both negative and positive -> all
3588 * If we end up with "all", we are done.
3589 * Otherwise, we perform a check for positive and/or negative
3590 * values as follows.
3592 * samples neg unk pos
3598 * There is no special sign for "zero", because we can usually treat zero
3599 * as either non-negative or non-positive, whatever works out best.
3600 * However, if the row is "critical", meaning that pivoting is impossible
3601 * then we don't want to limp zero with the non-positive case, because
3602 * then we we would lose the solution for those values of the parameters
3603 * where the value of the row is zero. Instead, we treat 0 as non-negative
3604 * ensuring a split if the row can attain both zero and negative values.
3605 * The same happens when the original constraint was one that could not
3606 * be satisfied with equality by any integer values of the parameters.
3607 * In this case, we normalize the constraint, but then a value of zero
3608 * for the normalized constraint is actually a positive value for the
3609 * original constraint, so again we need to treat zero as non-negative.
3610 * In both these cases, we have the following decision tree instead:
3612 * all non-negative -> pos
3613 * all negative -> neg
3614 * both negative and non-negative -> all
3622 static enum isl_tab_row_sign
row_sign(struct isl_tab
*tab
,
3623 struct isl_sol
*sol
, int row
)
3625 struct isl_vec
*ineq
= NULL
;
3626 enum isl_tab_row_sign res
= isl_tab_row_unknown
;
3631 if (tab
->row_sign
[row
] != isl_tab_row_unknown
)
3632 return tab
->row_sign
[row
];
3633 if (is_obviously_nonneg(tab
, row
))
3634 return isl_tab_row_pos
;
3635 for (row2
= tab
->n_redundant
; row2
< tab
->n_row
; ++row2
) {
3636 if (tab
->row_sign
[row2
] == isl_tab_row_unknown
)
3638 if (identical_parameter_line(tab
, row
, row2
))
3639 return tab
->row_sign
[row2
];
3642 critical
= is_critical(tab
, row
);
3644 ineq
= get_row_parameter_ineq(tab
, row
);
3648 strict
= is_strict(ineq
);
3650 res
= sol
->context
->op
->ineq_sign(sol
->context
, ineq
->el
,
3651 critical
|| strict
);
3653 if (res
== isl_tab_row_unknown
|| res
== isl_tab_row_pos
) {
3654 /* test for negative values */
3656 isl_seq_neg(ineq
->el
, ineq
->el
, ineq
->size
);
3657 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3659 feasible
= sol
->context
->op
->test_ineq(sol
->context
, ineq
->el
);
3663 res
= isl_tab_row_pos
;
3665 res
= (res
== isl_tab_row_unknown
) ? isl_tab_row_neg
3667 if (res
== isl_tab_row_neg
) {
3668 isl_seq_neg(ineq
->el
, ineq
->el
, ineq
->size
);
3669 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3673 if (res
== isl_tab_row_neg
) {
3674 /* test for positive values */
3676 if (!critical
&& !strict
)
3677 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3679 feasible
= sol
->context
->op
->test_ineq(sol
->context
, ineq
->el
);
3683 res
= isl_tab_row_any
;
3690 return isl_tab_row_unknown
;
3693 static void find_solutions(struct isl_sol
*sol
, struct isl_tab
*tab
);
3695 /* Find solutions for values of the parameters that satisfy the given
3698 * We currently take a snapshot of the context tableau that is reset
3699 * when we return from this function, while we make a copy of the main
3700 * tableau, leaving the original main tableau untouched.
3701 * These are fairly arbitrary choices. Making a copy also of the context
3702 * tableau would obviate the need to undo any changes made to it later,
3703 * while taking a snapshot of the main tableau could reduce memory usage.
3704 * If we were to switch to taking a snapshot of the main tableau,
3705 * we would have to keep in mind that we need to save the row signs
3706 * and that we need to do this before saving the current basis
3707 * such that the basis has been restore before we restore the row signs.
3709 static void find_in_pos(struct isl_sol
*sol
, struct isl_tab
*tab
, isl_int
*ineq
)
3715 saved
= sol
->context
->op
->save(sol
->context
);
3717 tab
= isl_tab_dup(tab
);
3721 sol
->context
->op
->add_ineq(sol
->context
, ineq
, 0, 1);
3723 find_solutions(sol
, tab
);
3726 sol
->context
->op
->restore(sol
->context
, saved
);
3728 sol
->context
->op
->discard(saved
);
3734 /* Record the absence of solutions for those values of the parameters
3735 * that do not satisfy the given inequality with equality.
3737 static void no_sol_in_strict(struct isl_sol
*sol
,
3738 struct isl_tab
*tab
, struct isl_vec
*ineq
)
3743 if (!sol
->context
|| sol
->error
)
3745 saved
= sol
->context
->op
->save(sol
->context
);
3747 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3749 sol
->context
->op
->add_ineq(sol
->context
, ineq
->el
, 1, 0);
3758 isl_int_add_ui(ineq
->el
[0], ineq
->el
[0], 1);
3760 sol
->context
->op
->restore(sol
->context
, saved
);
3766 /* Reset all row variables that are marked to have a sign that may
3767 * be both positive and negative to have an unknown sign.
3769 static void reset_any_to_unknown(struct isl_tab
*tab
)
3773 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
3774 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
3776 if (tab
->row_sign
[row
] == isl_tab_row_any
)
3777 tab
->row_sign
[row
] = isl_tab_row_unknown
;
3781 /* Compute the lexicographic minimum of the set represented by the main
3782 * tableau "tab" within the context "sol->context_tab".
3783 * On entry the sample value of the main tableau is lexicographically
3784 * less than or equal to this lexicographic minimum.
3785 * Pivots are performed until a feasible point is found, which is then
3786 * necessarily equal to the minimum, or until the tableau is found to
3787 * be infeasible. Some pivots may need to be performed for only some
3788 * feasible values of the context tableau. If so, the context tableau
3789 * is split into a part where the pivot is needed and a part where it is not.
3791 * Whenever we enter the main loop, the main tableau is such that no
3792 * "obvious" pivots need to be performed on it, where "obvious" means
3793 * that the given row can be seen to be negative without looking at
3794 * the context tableau. In particular, for non-parametric problems,
3795 * no pivots need to be performed on the main tableau.
3796 * The caller of find_solutions is responsible for making this property
3797 * hold prior to the first iteration of the loop, while restore_lexmin
3798 * is called before every other iteration.
3800 * Inside the main loop, we first examine the signs of the rows of
3801 * the main tableau within the context of the context tableau.
3802 * If we find a row that is always non-positive for all values of
3803 * the parameters satisfying the context tableau and negative for at
3804 * least one value of the parameters, we perform the appropriate pivot
3805 * and start over. An exception is the case where no pivot can be
3806 * performed on the row. In this case, we require that the sign of
3807 * the row is negative for all values of the parameters (rather than just
3808 * non-positive). This special case is handled inside row_sign, which
3809 * will say that the row can have any sign if it determines that it can
3810 * attain both negative and zero values.
3812 * If we can't find a row that always requires a pivot, but we can find
3813 * one or more rows that require a pivot for some values of the parameters
3814 * (i.e., the row can attain both positive and negative signs), then we split
3815 * the context tableau into two parts, one where we force the sign to be
3816 * non-negative and one where we force is to be negative.
3817 * The non-negative part is handled by a recursive call (through find_in_pos).
3818 * Upon returning from this call, we continue with the negative part and
3819 * perform the required pivot.
3821 * If no such rows can be found, all rows are non-negative and we have
3822 * found a (rational) feasible point. If we only wanted a rational point
3824 * Otherwise, we check if all values of the sample point of the tableau
3825 * are integral for the variables. If so, we have found the minimal
3826 * integral point and we are done.
3827 * If the sample point is not integral, then we need to make a distinction
3828 * based on whether the constant term is non-integral or the coefficients
3829 * of the parameters. Furthermore, in order to decide how to handle
3830 * the non-integrality, we also need to know whether the coefficients
3831 * of the other columns in the tableau are integral. This leads
3832 * to the following table. The first two rows do not correspond
3833 * to a non-integral sample point and are only mentioned for completeness.
3835 * constant parameters other
3838 * int int rat | -> no problem
3840 * rat int int -> fail
3842 * rat int rat -> cut
3845 * rat rat rat | -> parametric cut
3848 * rat rat int | -> split context
3850 * If the parametric constant is completely integral, then there is nothing
3851 * to be done. If the constant term is non-integral, but all the other
3852 * coefficient are integral, then there is nothing that can be done
3853 * and the tableau has no integral solution.
3854 * If, on the other hand, one or more of the other columns have rational
3855 * coefficients, but the parameter coefficients are all integral, then
3856 * we can perform a regular (non-parametric) cut.
3857 * Finally, if there is any parameter coefficient that is non-integral,
3858 * then we need to involve the context tableau. There are two cases here.
3859 * If at least one other column has a rational coefficient, then we
3860 * can perform a parametric cut in the main tableau by adding a new
3861 * integer division in the context tableau.
3862 * If all other columns have integral coefficients, then we need to
3863 * enforce that the rational combination of parameters (c + \sum a_i y_i)/m
3864 * is always integral. We do this by introducing an integer division
3865 * q = floor((c + \sum a_i y_i)/m) and stipulating that its argument should
3866 * always be integral in the context tableau, i.e., m q = c + \sum a_i y_i.
3867 * Since q is expressed in the tableau as
3868 * c + \sum a_i y_i - m q >= 0
3869 * -c - \sum a_i y_i + m q + m - 1 >= 0
3870 * it is sufficient to add the inequality
3871 * -c - \sum a_i y_i + m q >= 0
3872 * In the part of the context where this inequality does not hold, the
3873 * main tableau is marked as being empty.
3875 static void find_solutions(struct isl_sol
*sol
, struct isl_tab
*tab
)
3877 struct isl_context
*context
;
3880 if (!tab
|| sol
->error
)
3883 context
= sol
->context
;
3887 if (context
->op
->is_empty(context
))
3890 for (r
= 0; r
>= 0 && tab
&& !tab
->empty
; r
= restore_lexmin(tab
)) {
3893 enum isl_tab_row_sign sgn
;
3897 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
3898 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
3900 sgn
= row_sign(tab
, sol
, row
);
3903 tab
->row_sign
[row
] = sgn
;
3904 if (sgn
== isl_tab_row_any
)
3906 if (sgn
== isl_tab_row_any
&& split
== -1)
3908 if (sgn
== isl_tab_row_neg
)
3911 if (row
< tab
->n_row
)
3914 struct isl_vec
*ineq
;
3916 split
= context
->op
->best_split(context
, tab
);
3919 ineq
= get_row_parameter_ineq(tab
, split
);
3923 reset_any_to_unknown(tab
);
3924 tab
->row_sign
[split
] = isl_tab_row_pos
;
3926 find_in_pos(sol
, tab
, ineq
->el
);
3927 tab
->row_sign
[split
] = isl_tab_row_neg
;
3928 isl_seq_neg(ineq
->el
, ineq
->el
, ineq
->size
);
3929 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3931 context
->op
->add_ineq(context
, ineq
->el
, 0, 1);
3939 row
= first_non_integer_row(tab
, &flags
);
3942 if (ISL_FL_ISSET(flags
, I_PAR
)) {
3943 if (ISL_FL_ISSET(flags
, I_VAR
)) {
3944 if (isl_tab_mark_empty(tab
) < 0)
3948 row
= add_cut(tab
, row
);
3949 } else if (ISL_FL_ISSET(flags
, I_VAR
)) {
3950 struct isl_vec
*div
;
3951 struct isl_vec
*ineq
;
3953 div
= get_row_split_div(tab
, row
);
3956 d
= context
->op
->get_div(context
, tab
, div
);
3960 ineq
= ineq_for_div(context
->op
->peek_basic_set(context
), d
);
3964 no_sol_in_strict(sol
, tab
, ineq
);
3965 isl_seq_neg(ineq
->el
, ineq
->el
, ineq
->size
);
3966 context
->op
->add_ineq(context
, ineq
->el
, 1, 1);
3968 if (sol
->error
|| !context
->op
->is_ok(context
))
3970 tab
= set_row_cst_to_div(tab
, row
, d
);
3971 if (context
->op
->is_empty(context
))
3974 row
= add_parametric_cut(tab
, row
, context
);
3989 /* Does "sol" contain a pair of partial solutions that could potentially
3992 * We currently only check that "sol" is not in an error state
3993 * and that there are at least two partial solutions of which the final two
3994 * are defined at the same level.
3996 static int sol_has_mergeable_solutions(struct isl_sol
*sol
)
4002 if (!sol
->partial
->next
)
4004 return sol
->partial
->level
== sol
->partial
->next
->level
;
4007 /* Compute the lexicographic minimum of the set represented by the main
4008 * tableau "tab" within the context "sol->context_tab".
4010 * As a preprocessing step, we first transfer all the purely parametric
4011 * equalities from the main tableau to the context tableau, i.e.,
4012 * parameters that have been pivoted to a row.
4013 * These equalities are ignored by the main algorithm, because the
4014 * corresponding rows may not be marked as being non-negative.
4015 * In parts of the context where the added equality does not hold,
4016 * the main tableau is marked as being empty.
4018 * Before we embark on the actual computation, we save a copy
4019 * of the context. When we return, we check if there are any
4020 * partial solutions that can potentially be merged. If so,
4021 * we perform a rollback to the initial state of the context.
4022 * The merging of partial solutions happens inside calls to
4023 * sol_dec_level that are pushed onto the undo stack of the context.
4024 * If there are no partial solutions that can potentially be merged
4025 * then the rollback is skipped as it would just be wasted effort.
4027 static void find_solutions_main(struct isl_sol
*sol
, struct isl_tab
*tab
)
4037 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
4041 if (tab
->row_var
[row
] < 0)
4043 if (tab
->row_var
[row
] >= tab
->n_param
&&
4044 tab
->row_var
[row
] < tab
->n_var
- tab
->n_div
)
4046 if (tab
->row_var
[row
] < tab
->n_param
)
4047 p
= tab
->row_var
[row
];
4049 p
= tab
->row_var
[row
]
4050 + tab
->n_param
- (tab
->n_var
- tab
->n_div
);
4052 eq
= isl_vec_alloc(tab
->mat
->ctx
, 1+tab
->n_param
+tab
->n_div
);
4055 get_row_parameter_line(tab
, row
, eq
->el
);
4056 isl_int_neg(eq
->el
[1 + p
], tab
->mat
->row
[row
][0]);
4057 eq
= isl_vec_normalize(eq
);
4060 no_sol_in_strict(sol
, tab
, eq
);
4062 isl_seq_neg(eq
->el
, eq
->el
, eq
->size
);
4064 no_sol_in_strict(sol
, tab
, eq
);
4065 isl_seq_neg(eq
->el
, eq
->el
, eq
->size
);
4067 sol
->context
->op
->add_eq(sol
->context
, eq
->el
, 1, 1);
4071 if (isl_tab_mark_redundant(tab
, row
) < 0)
4074 if (sol
->context
->op
->is_empty(sol
->context
))
4077 row
= tab
->n_redundant
- 1;
4080 saved
= sol
->context
->op
->save(sol
->context
);
4082 find_solutions(sol
, tab
);
4084 if (sol_has_mergeable_solutions(sol
))
4085 sol
->context
->op
->restore(sol
->context
, saved
);
4087 sol
->context
->op
->discard(saved
);
4098 /* Check if integer division "div" of "dom" also occurs in "bmap".
4099 * If so, return its position within the divs.
4100 * If not, return -1.
4102 static int find_context_div(struct isl_basic_map
*bmap
,
4103 struct isl_basic_set
*dom
, unsigned div
)
4106 unsigned b_dim
= isl_space_dim(bmap
->dim
, isl_dim_all
);
4107 unsigned d_dim
= isl_space_dim(dom
->dim
, isl_dim_all
);
4109 if (isl_int_is_zero(dom
->div
[div
][0]))
4111 if (isl_seq_first_non_zero(dom
->div
[div
] + 2 + d_dim
, dom
->n_div
) != -1)
4114 for (i
= 0; i
< bmap
->n_div
; ++i
) {
4115 if (isl_int_is_zero(bmap
->div
[i
][0]))
4117 if (isl_seq_first_non_zero(bmap
->div
[i
] + 2 + d_dim
,
4118 (b_dim
- d_dim
) + bmap
->n_div
) != -1)
4120 if (isl_seq_eq(bmap
->div
[i
], dom
->div
[div
], 2 + d_dim
))
4126 /* The correspondence between the variables in the main tableau,
4127 * the context tableau, and the input map and domain is as follows.
4128 * The first n_param and the last n_div variables of the main tableau
4129 * form the variables of the context tableau.
4130 * In the basic map, these n_param variables correspond to the
4131 * parameters and the input dimensions. In the domain, they correspond
4132 * to the parameters and the set dimensions.
4133 * The n_div variables correspond to the integer divisions in the domain.
4134 * To ensure that everything lines up, we may need to copy some of the
4135 * integer divisions of the domain to the map. These have to be placed
4136 * in the same order as those in the context and they have to be placed
4137 * after any other integer divisions that the map may have.
4138 * This function performs the required reordering.
4140 static struct isl_basic_map
*align_context_divs(struct isl_basic_map
*bmap
,
4141 struct isl_basic_set
*dom
)
4147 for (i
= 0; i
< dom
->n_div
; ++i
)
4148 if (find_context_div(bmap
, dom
, i
) != -1)
4150 other
= bmap
->n_div
- common
;
4151 if (dom
->n_div
- common
> 0) {
4152 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
4153 dom
->n_div
- common
, 0, 0);
4157 for (i
= 0; i
< dom
->n_div
; ++i
) {
4158 int pos
= find_context_div(bmap
, dom
, i
);
4160 pos
= isl_basic_map_alloc_div(bmap
);
4163 isl_int_set_si(bmap
->div
[pos
][0], 0);
4165 if (pos
!= other
+ i
)
4166 isl_basic_map_swap_div(bmap
, pos
, other
+ i
);
4170 isl_basic_map_free(bmap
);
4174 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
4175 * some obvious symmetries.
4177 * We make sure the divs in the domain are properly ordered,
4178 * because they will be added one by one in the given order
4179 * during the construction of the solution map.
4180 * Furthermore, make sure that the known integer divisions
4181 * appear before any unknown integer division because the solution
4182 * may depend on the known integer divisions, while anything that
4183 * depends on any variable starting from the first unknown integer
4184 * division is ignored in sol_pma_add.
4186 static struct isl_sol
*basic_map_partial_lexopt_base_sol(
4187 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
4188 __isl_give isl_set
**empty
, int max
,
4189 struct isl_sol
*(*init
)(__isl_keep isl_basic_map
*bmap
,
4190 __isl_take isl_basic_set
*dom
, int track_empty
, int max
))
4192 struct isl_tab
*tab
;
4193 struct isl_sol
*sol
= NULL
;
4194 struct isl_context
*context
;
4197 dom
= isl_basic_set_sort_divs(dom
);
4198 bmap
= align_context_divs(bmap
, dom
);
4200 sol
= init(bmap
, dom
, !!empty
, max
);
4204 context
= sol
->context
;
4205 if (isl_basic_set_plain_is_empty(context
->op
->peek_basic_set(context
)))
4207 else if (isl_basic_map_plain_is_empty(bmap
)) {
4210 isl_basic_set_copy(context
->op
->peek_basic_set(context
)));
4212 tab
= tab_for_lexmin(bmap
,
4213 context
->op
->peek_basic_set(context
), 1, max
);
4214 tab
= context
->op
->detect_nonnegative_parameters(context
, tab
);
4215 find_solutions_main(sol
, tab
);
4220 isl_basic_map_free(bmap
);
4224 isl_basic_map_free(bmap
);
4228 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
4229 * some obvious symmetries.
4231 * We call basic_map_partial_lexopt_base_sol and extract the results.
4233 static __isl_give isl_map
*basic_map_partial_lexopt_base(
4234 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
4235 __isl_give isl_set
**empty
, int max
)
4237 isl_map
*result
= NULL
;
4238 struct isl_sol
*sol
;
4239 struct isl_sol_map
*sol_map
;
4241 sol
= basic_map_partial_lexopt_base_sol(bmap
, dom
, empty
, max
,
4245 sol_map
= (struct isl_sol_map
*) sol
;
4247 result
= isl_map_copy(sol_map
->map
);
4249 *empty
= isl_set_copy(sol_map
->empty
);
4250 sol_free(&sol_map
->sol
);
4254 /* Return a count of the number of occurrences of the "n" first
4255 * variables in the inequality constraints of "bmap".
4257 static __isl_give
int *count_occurrences(__isl_keep isl_basic_map
*bmap
,
4266 ctx
= isl_basic_map_get_ctx(bmap
);
4267 occurrences
= isl_calloc_array(ctx
, int, n
);
4271 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
4272 for (j
= 0; j
< n
; ++j
) {
4273 if (!isl_int_is_zero(bmap
->ineq
[i
][1 + j
]))
4281 /* Do all of the "n" variables with non-zero coefficients in "c"
4282 * occur in exactly a single constraint.
4283 * "occurrences" is an array of length "n" containing the number
4284 * of occurrences of each of the variables in the inequality constraints.
4286 static int single_occurrence(int n
, isl_int
*c
, int *occurrences
)
4290 for (i
= 0; i
< n
; ++i
) {
4291 if (isl_int_is_zero(c
[i
]))
4293 if (occurrences
[i
] != 1)
4300 /* Do all of the "n" initial variables that occur in inequality constraint
4301 * "ineq" of "bmap" only occur in that constraint?
4303 static int all_single_occurrence(__isl_keep isl_basic_map
*bmap
, int ineq
,
4308 for (i
= 0; i
< n
; ++i
) {
4309 if (isl_int_is_zero(bmap
->ineq
[ineq
][1 + i
]))
4311 for (j
= 0; j
< bmap
->n_ineq
; ++j
) {
4314 if (!isl_int_is_zero(bmap
->ineq
[j
][1 + i
]))
4322 /* Structure used during detection of parallel constraints.
4323 * n_in: number of "input" variables: isl_dim_param + isl_dim_in
4324 * n_out: number of "output" variables: isl_dim_out + isl_dim_div
4325 * val: the coefficients of the output variables
4327 struct isl_constraint_equal_info
{
4328 isl_basic_map
*bmap
;
4334 /* Check whether the coefficients of the output variables
4335 * of the constraint in "entry" are equal to info->val.
4337 static int constraint_equal(const void *entry
, const void *val
)
4339 isl_int
**row
= (isl_int
**)entry
;
4340 const struct isl_constraint_equal_info
*info
= val
;
4342 return isl_seq_eq((*row
) + 1 + info
->n_in
, info
->val
, info
->n_out
);
4345 /* Check whether "bmap" has a pair of constraints that have
4346 * the same coefficients for the output variables.
4347 * Note that the coefficients of the existentially quantified
4348 * variables need to be zero since the existentially quantified
4349 * of the result are usually not the same as those of the input.
4350 * Furthermore, check that each of the input variables that occur
4351 * in those constraints does not occur in any other constraint.
4352 * If so, return 1 and return the row indices of the two constraints
4353 * in *first and *second.
4355 static int parallel_constraints(__isl_keep isl_basic_map
*bmap
,
4356 int *first
, int *second
)
4360 int *occurrences
= NULL
;
4361 struct isl_hash_table
*table
= NULL
;
4362 struct isl_hash_table_entry
*entry
;
4363 struct isl_constraint_equal_info info
;
4367 ctx
= isl_basic_map_get_ctx(bmap
);
4368 table
= isl_hash_table_alloc(ctx
, bmap
->n_ineq
);
4372 info
.n_in
= isl_basic_map_dim(bmap
, isl_dim_param
) +
4373 isl_basic_map_dim(bmap
, isl_dim_in
);
4374 occurrences
= count_occurrences(bmap
, info
.n_in
);
4375 if (info
.n_in
&& !occurrences
)
4378 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4379 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4380 info
.n_out
= n_out
+ n_div
;
4381 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
4384 info
.val
= bmap
->ineq
[i
] + 1 + info
.n_in
;
4385 if (isl_seq_first_non_zero(info
.val
, n_out
) < 0)
4387 if (isl_seq_first_non_zero(info
.val
+ n_out
, n_div
) >= 0)
4389 if (!single_occurrence(info
.n_in
, bmap
->ineq
[i
] + 1,
4392 hash
= isl_seq_get_hash(info
.val
, info
.n_out
);
4393 entry
= isl_hash_table_find(ctx
, table
, hash
,
4394 constraint_equal
, &info
, 1);
4399 entry
->data
= &bmap
->ineq
[i
];
4402 if (i
< bmap
->n_ineq
) {
4403 *first
= ((isl_int
**)entry
->data
) - bmap
->ineq
;
4407 isl_hash_table_free(ctx
, table
);
4410 return i
< bmap
->n_ineq
;
4412 isl_hash_table_free(ctx
, table
);
4417 /* Given a set of upper bounds in "var", add constraints to "bset"
4418 * that make the i-th bound smallest.
4420 * In particular, if there are n bounds b_i, then add the constraints
4422 * b_i <= b_j for j > i
4423 * b_i < b_j for j < i
4425 static __isl_give isl_basic_set
*select_minimum(__isl_take isl_basic_set
*bset
,
4426 __isl_keep isl_mat
*var
, int i
)
4431 ctx
= isl_mat_get_ctx(var
);
4433 for (j
= 0; j
< var
->n_row
; ++j
) {
4436 k
= isl_basic_set_alloc_inequality(bset
);
4439 isl_seq_combine(bset
->ineq
[k
], ctx
->one
, var
->row
[j
],
4440 ctx
->negone
, var
->row
[i
], var
->n_col
);
4441 isl_int_set_si(bset
->ineq
[k
][var
->n_col
], 0);
4443 isl_int_sub_ui(bset
->ineq
[k
][0], bset
->ineq
[k
][0], 1);
4446 bset
= isl_basic_set_finalize(bset
);
4450 isl_basic_set_free(bset
);
4454 /* Given a set of upper bounds on the last "input" variable m,
4455 * construct a set that assigns the minimal upper bound to m, i.e.,
4456 * construct a set that divides the space into cells where one
4457 * of the upper bounds is smaller than all the others and assign
4458 * this upper bound to m.
4460 * In particular, if there are n bounds b_i, then the result
4461 * consists of n basic sets, each one of the form
4464 * b_i <= b_j for j > i
4465 * b_i < b_j for j < i
4467 static __isl_give isl_set
*set_minimum(__isl_take isl_space
*dim
,
4468 __isl_take isl_mat
*var
)
4471 isl_basic_set
*bset
= NULL
;
4472 isl_set
*set
= NULL
;
4477 set
= isl_set_alloc_space(isl_space_copy(dim
),
4478 var
->n_row
, ISL_SET_DISJOINT
);
4480 for (i
= 0; i
< var
->n_row
; ++i
) {
4481 bset
= isl_basic_set_alloc_space(isl_space_copy(dim
), 0,
4483 k
= isl_basic_set_alloc_equality(bset
);
4486 isl_seq_cpy(bset
->eq
[k
], var
->row
[i
], var
->n_col
);
4487 isl_int_set_si(bset
->eq
[k
][var
->n_col
], -1);
4488 bset
= select_minimum(bset
, var
, i
);
4489 set
= isl_set_add_basic_set(set
, bset
);
4492 isl_space_free(dim
);
4496 isl_basic_set_free(bset
);
4498 isl_space_free(dim
);
4503 /* Given that the last input variable of "bmap" represents the minimum
4504 * of the bounds in "cst", check whether we need to split the domain
4505 * based on which bound attains the minimum.
4507 * A split is needed when the minimum appears in an integer division
4508 * or in an equality. Otherwise, it is only needed if it appears in
4509 * an upper bound that is different from the upper bounds on which it
4512 static int need_split_basic_map(__isl_keep isl_basic_map
*bmap
,
4513 __isl_keep isl_mat
*cst
)
4519 pos
= cst
->n_col
- 1;
4520 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4522 for (i
= 0; i
< bmap
->n_div
; ++i
)
4523 if (!isl_int_is_zero(bmap
->div
[i
][2 + pos
]))
4526 for (i
= 0; i
< bmap
->n_eq
; ++i
)
4527 if (!isl_int_is_zero(bmap
->eq
[i
][1 + pos
]))
4530 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
4531 if (isl_int_is_nonneg(bmap
->ineq
[i
][1 + pos
]))
4533 if (!isl_int_is_negone(bmap
->ineq
[i
][1 + pos
]))
4535 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + pos
+ 1,
4536 total
- pos
- 1) >= 0)
4539 for (j
= 0; j
< cst
->n_row
; ++j
)
4540 if (isl_seq_eq(bmap
->ineq
[i
], cst
->row
[j
], cst
->n_col
))
4542 if (j
>= cst
->n_row
)
4549 /* Given that the last set variable of "bset" represents the minimum
4550 * of the bounds in "cst", check whether we need to split the domain
4551 * based on which bound attains the minimum.
4553 * We simply call need_split_basic_map here. This is safe because
4554 * the position of the minimum is computed from "cst" and not
4557 static int need_split_basic_set(__isl_keep isl_basic_set
*bset
,
4558 __isl_keep isl_mat
*cst
)
4560 return need_split_basic_map(bset_to_bmap(bset
), cst
);
4563 /* Given that the last set variable of "set" represents the minimum
4564 * of the bounds in "cst", check whether we need to split the domain
4565 * based on which bound attains the minimum.
4567 static int need_split_set(__isl_keep isl_set
*set
, __isl_keep isl_mat
*cst
)
4571 for (i
= 0; i
< set
->n
; ++i
)
4572 if (need_split_basic_set(set
->p
[i
], cst
))
4578 /* Given a set of which the last set variable is the minimum
4579 * of the bounds in "cst", split each basic set in the set
4580 * in pieces where one of the bounds is (strictly) smaller than the others.
4581 * This subdivision is given in "min_expr".
4582 * The variable is subsequently projected out.
4584 * We only do the split when it is needed.
4585 * For example if the last input variable m = min(a,b) and the only
4586 * constraints in the given basic set are lower bounds on m,
4587 * i.e., l <= m = min(a,b), then we can simply project out m
4588 * to obtain l <= a and l <= b, without having to split on whether
4589 * m is equal to a or b.
4591 static __isl_give isl_set
*split(__isl_take isl_set
*empty
,
4592 __isl_take isl_set
*min_expr
, __isl_take isl_mat
*cst
)
4599 if (!empty
|| !min_expr
|| !cst
)
4602 n_in
= isl_set_dim(empty
, isl_dim_set
);
4603 dim
= isl_set_get_space(empty
);
4604 dim
= isl_space_drop_dims(dim
, isl_dim_set
, n_in
- 1, 1);
4605 res
= isl_set_empty(dim
);
4607 for (i
= 0; i
< empty
->n
; ++i
) {
4610 set
= isl_set_from_basic_set(isl_basic_set_copy(empty
->p
[i
]));
4611 if (need_split_basic_set(empty
->p
[i
], cst
))
4612 set
= isl_set_intersect(set
, isl_set_copy(min_expr
));
4613 set
= isl_set_remove_dims(set
, isl_dim_set
, n_in
- 1, 1);
4615 res
= isl_set_union_disjoint(res
, set
);
4618 isl_set_free(empty
);
4619 isl_set_free(min_expr
);
4623 isl_set_free(empty
);
4624 isl_set_free(min_expr
);
4629 /* Given a map of which the last input variable is the minimum
4630 * of the bounds in "cst", split each basic set in the set
4631 * in pieces where one of the bounds is (strictly) smaller than the others.
4632 * This subdivision is given in "min_expr".
4633 * The variable is subsequently projected out.
4635 * The implementation is essentially the same as that of "split".
4637 static __isl_give isl_map
*split_domain(__isl_take isl_map
*opt
,
4638 __isl_take isl_set
*min_expr
, __isl_take isl_mat
*cst
)
4645 if (!opt
|| !min_expr
|| !cst
)
4648 n_in
= isl_map_dim(opt
, isl_dim_in
);
4649 dim
= isl_map_get_space(opt
);
4650 dim
= isl_space_drop_dims(dim
, isl_dim_in
, n_in
- 1, 1);
4651 res
= isl_map_empty(dim
);
4653 for (i
= 0; i
< opt
->n
; ++i
) {
4656 map
= isl_map_from_basic_map(isl_basic_map_copy(opt
->p
[i
]));
4657 if (need_split_basic_map(opt
->p
[i
], cst
))
4658 map
= isl_map_intersect_domain(map
,
4659 isl_set_copy(min_expr
));
4660 map
= isl_map_remove_dims(map
, isl_dim_in
, n_in
- 1, 1);
4662 res
= isl_map_union_disjoint(res
, map
);
4666 isl_set_free(min_expr
);
4671 isl_set_free(min_expr
);
4676 static __isl_give isl_map
*basic_map_partial_lexopt(
4677 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
4678 __isl_give isl_set
**empty
, int max
);
4680 /* This function is called from basic_map_partial_lexopt_symm.
4681 * The last variable of "bmap" and "dom" corresponds to the minimum
4682 * of the bounds in "cst". "map_space" is the space of the original
4683 * input relation (of basic_map_partial_lexopt_symm) and "set_space"
4684 * is the space of the original domain.
4686 * We recursively call basic_map_partial_lexopt and then plug in
4687 * the definition of the minimum in the result.
4689 static __isl_give isl_map
*basic_map_partial_lexopt_symm_core(
4690 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
4691 __isl_give isl_set
**empty
, int max
, __isl_take isl_mat
*cst
,
4692 __isl_take isl_space
*map_space
, __isl_take isl_space
*set_space
)
4697 min_expr
= set_minimum(isl_basic_set_get_space(dom
), isl_mat_copy(cst
));
4699 opt
= basic_map_partial_lexopt(bmap
, dom
, empty
, max
);
4702 *empty
= split(*empty
,
4703 isl_set_copy(min_expr
), isl_mat_copy(cst
));
4704 *empty
= isl_set_reset_space(*empty
, set_space
);
4707 opt
= split_domain(opt
, min_expr
, cst
);
4708 opt
= isl_map_reset_space(opt
, map_space
);
4713 /* Extract a domain from "bmap" for the purpose of computing
4714 * a lexicographic optimum.
4716 * This function is only called when the caller wants to compute a full
4717 * lexicographic optimum, i.e., without specifying a domain. In this case,
4718 * the caller is not interested in the part of the domain space where
4719 * there is no solution and the domain can be initialized to those constraints
4720 * of "bmap" that only involve the parameters and the input dimensions.
4721 * This relieves the parametric programming engine from detecting those
4722 * inequalities and transferring them to the context. More importantly,
4723 * it ensures that those inequalities are transferred first and not
4724 * intermixed with inequalities that actually split the domain.
4726 * If the caller does not require the absence of existentially quantified
4727 * variables in the result (i.e., if ISL_OPT_QE is not set in "flags"),
4728 * then the actual domain of "bmap" can be used. This ensures that
4729 * the domain does not need to be split at all just to separate out
4730 * pieces of the domain that do not have a solution from piece that do.
4731 * This domain cannot be used in general because it may involve
4732 * (unknown) existentially quantified variables which will then also
4733 * appear in the solution.
4735 static __isl_give isl_basic_set
*extract_domain(__isl_keep isl_basic_map
*bmap
,
4741 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4742 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4743 bmap
= isl_basic_map_copy(bmap
);
4744 if (ISL_FL_ISSET(flags
, ISL_OPT_QE
)) {
4745 bmap
= isl_basic_map_drop_constraints_involving_dims(bmap
,
4746 isl_dim_div
, 0, n_div
);
4747 bmap
= isl_basic_map_drop_constraints_involving_dims(bmap
,
4748 isl_dim_out
, 0, n_out
);
4750 return isl_basic_map_domain(bmap
);
4754 #define TYPE isl_map
4757 #include "isl_tab_lexopt_templ.c"
4759 struct isl_sol_for
{
4761 int (*fn
)(__isl_take isl_basic_set
*dom
,
4762 __isl_take isl_aff_list
*list
, void *user
);
4766 static void sol_for_free(struct isl_sol_for
*sol_for
)
4770 if (sol_for
->sol
.context
)
4771 sol_for
->sol
.context
->op
->free(sol_for
->sol
.context
);
4775 static void sol_for_free_wrap(struct isl_sol
*sol
)
4777 sol_for_free((struct isl_sol_for
*)sol
);
4780 /* Add the solution identified by the tableau and the context tableau.
4782 * See documentation of sol_add for more details.
4784 * Instead of constructing a basic map, this function calls a user
4785 * defined function with the current context as a basic set and
4786 * a list of affine expressions representing the relation between
4787 * the input and output. The space over which the affine expressions
4788 * are defined is the same as that of the domain. The number of
4789 * affine expressions in the list is equal to the number of output variables.
4791 static void sol_for_add(struct isl_sol_for
*sol
,
4792 __isl_take isl_basic_set
*dom
, __isl_take isl_mat
*M
)
4796 isl_local_space
*ls
;
4800 if (sol
->sol
.error
|| !dom
|| !M
)
4803 ctx
= isl_basic_set_get_ctx(dom
);
4804 ls
= isl_basic_set_get_local_space(dom
);
4805 list
= isl_aff_list_alloc(ctx
, M
->n_row
- 1);
4806 for (i
= 1; i
< M
->n_row
; ++i
) {
4807 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
4809 isl_int_set(aff
->v
->el
[0], M
->row
[0][0]);
4810 isl_seq_cpy(aff
->v
->el
+ 1, M
->row
[i
], M
->n_col
);
4812 aff
= isl_aff_normalize(aff
);
4813 list
= isl_aff_list_add(list
, aff
);
4815 isl_local_space_free(ls
);
4817 dom
= isl_basic_set_finalize(dom
);
4819 if (sol
->fn(isl_basic_set_copy(dom
), list
, sol
->user
) < 0)
4822 isl_basic_set_free(dom
);
4826 isl_basic_set_free(dom
);
4831 static void sol_for_add_wrap(struct isl_sol
*sol
,
4832 __isl_take isl_basic_set
*dom
, __isl_take isl_mat
*M
)
4834 sol_for_add((struct isl_sol_for
*)sol
, dom
, M
);
4837 static struct isl_sol_for
*sol_for_init(__isl_keep isl_basic_map
*bmap
, int max
,
4838 int (*fn
)(__isl_take isl_basic_set
*dom
, __isl_take isl_aff_list
*list
,
4842 struct isl_sol_for
*sol_for
= NULL
;
4844 struct isl_basic_set
*dom
= NULL
;
4846 sol_for
= isl_calloc_type(bmap
->ctx
, struct isl_sol_for
);
4850 dom_dim
= isl_space_domain(isl_space_copy(bmap
->dim
));
4851 dom
= isl_basic_set_universe(dom_dim
);
4853 sol_for
->sol
.rational
= ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
4854 sol_for
->sol
.dec_level
.callback
.run
= &sol_dec_level_wrap
;
4855 sol_for
->sol
.dec_level
.sol
= &sol_for
->sol
;
4857 sol_for
->user
= user
;
4858 sol_for
->sol
.max
= max
;
4859 sol_for
->sol
.n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4860 sol_for
->sol
.add
= &sol_for_add_wrap
;
4861 sol_for
->sol
.add_empty
= NULL
;
4862 sol_for
->sol
.free
= &sol_for_free_wrap
;
4864 sol_for
->sol
.context
= isl_context_alloc(dom
);
4865 if (!sol_for
->sol
.context
)
4868 isl_basic_set_free(dom
);
4871 isl_basic_set_free(dom
);
4872 sol_for_free(sol_for
);
4876 static void sol_for_find_solutions(struct isl_sol_for
*sol_for
,
4877 struct isl_tab
*tab
)
4879 find_solutions_main(&sol_for
->sol
, tab
);
4882 int isl_basic_map_foreach_lexopt(__isl_keep isl_basic_map
*bmap
, int max
,
4883 int (*fn
)(__isl_take isl_basic_set
*dom
, __isl_take isl_aff_list
*list
,
4887 struct isl_sol_for
*sol_for
= NULL
;
4889 bmap
= isl_basic_map_copy(bmap
);
4890 bmap
= isl_basic_map_detect_equalities(bmap
);
4894 sol_for
= sol_for_init(bmap
, max
, fn
, user
);
4898 if (isl_basic_map_plain_is_empty(bmap
))
4901 struct isl_tab
*tab
;
4902 struct isl_context
*context
= sol_for
->sol
.context
;
4903 tab
= tab_for_lexmin(bmap
,
4904 context
->op
->peek_basic_set(context
), 1, max
);
4905 tab
= context
->op
->detect_nonnegative_parameters(context
, tab
);
4906 sol_for_find_solutions(sol_for
, tab
);
4907 if (sol_for
->sol
.error
)
4911 sol_free(&sol_for
->sol
);
4912 isl_basic_map_free(bmap
);
4915 sol_free(&sol_for
->sol
);
4916 isl_basic_map_free(bmap
);
4920 int isl_basic_set_foreach_lexopt(__isl_keep isl_basic_set
*bset
, int max
,
4921 int (*fn
)(__isl_take isl_basic_set
*dom
, __isl_take isl_aff_list
*list
,
4925 return isl_basic_map_foreach_lexopt(bset
, max
, fn
, user
);
4928 /* Check if the given sequence of len variables starting at pos
4929 * represents a trivial (i.e., zero) solution.
4930 * The variables are assumed to be non-negative and to come in pairs,
4931 * with each pair representing a variable of unrestricted sign.
4932 * The solution is trivial if each such pair in the sequence consists
4933 * of two identical values, meaning that the variable being represented
4936 static int region_is_trivial(struct isl_tab
*tab
, int pos
, int len
)
4943 for (i
= 0; i
< len
; i
+= 2) {
4947 neg_row
= tab
->var
[pos
+ i
].is_row
?
4948 tab
->var
[pos
+ i
].index
: -1;
4949 pos_row
= tab
->var
[pos
+ i
+ 1].is_row
?
4950 tab
->var
[pos
+ i
+ 1].index
: -1;
4953 isl_int_is_zero(tab
->mat
->row
[neg_row
][1])) &&
4955 isl_int_is_zero(tab
->mat
->row
[pos_row
][1])))
4958 if (neg_row
< 0 || pos_row
< 0)
4960 if (isl_int_ne(tab
->mat
->row
[neg_row
][1],
4961 tab
->mat
->row
[pos_row
][1]))
4968 /* Return the index of the first trivial region or -1 if all regions
4971 static int first_trivial_region(struct isl_tab
*tab
,
4972 int n_region
, struct isl_region
*region
)
4976 for (i
= 0; i
< n_region
; ++i
) {
4977 if (region_is_trivial(tab
, region
[i
].pos
, region
[i
].len
))
4984 /* Check if the solution is optimal, i.e., whether the first
4985 * n_op entries are zero.
4987 static int is_optimal(__isl_keep isl_vec
*sol
, int n_op
)
4991 for (i
= 0; i
< n_op
; ++i
)
4992 if (!isl_int_is_zero(sol
->el
[1 + i
]))
4997 /* Add constraints to "tab" that ensure that any solution is significantly
4998 * better than that represented by "sol". That is, find the first
4999 * relevant (within first n_op) non-zero coefficient and force it (along
5000 * with all previous coefficients) to be zero.
5001 * If the solution is already optimal (all relevant coefficients are zero),
5002 * then just mark the table as empty.
5004 * This function assumes that at least 2 * n_op more rows and at least
5005 * 2 * n_op more elements in the constraint array are available in the tableau.
5007 static int force_better_solution(struct isl_tab
*tab
,
5008 __isl_keep isl_vec
*sol
, int n_op
)
5017 for (i
= 0; i
< n_op
; ++i
)
5018 if (!isl_int_is_zero(sol
->el
[1 + i
]))
5022 if (isl_tab_mark_empty(tab
) < 0)
5027 ctx
= isl_vec_get_ctx(sol
);
5028 v
= isl_vec_alloc(ctx
, 1 + tab
->n_var
);
5032 for (; i
>= 0; --i
) {
5034 isl_int_set_si(v
->el
[1 + i
], -1);
5035 if (add_lexmin_eq(tab
, v
->el
) < 0)
5046 struct isl_trivial
{
5050 struct isl_tab_undo
*snap
;
5053 /* Return the lexicographically smallest non-trivial solution of the
5054 * given ILP problem.
5056 * All variables are assumed to be non-negative.
5058 * n_op is the number of initial coordinates to optimize.
5059 * That is, once a solution has been found, we will only continue looking
5060 * for solution that result in significantly better values for those
5061 * initial coordinates. That is, we only continue looking for solutions
5062 * that increase the number of initial zeros in this sequence.
5064 * A solution is non-trivial, if it is non-trivial on each of the
5065 * specified regions. Each region represents a sequence of pairs
5066 * of variables. A solution is non-trivial on such a region if
5067 * at least one of these pairs consists of different values, i.e.,
5068 * such that the non-negative variable represented by the pair is non-zero.
5070 * Whenever a conflict is encountered, all constraints involved are
5071 * reported to the caller through a call to "conflict".
5073 * We perform a simple branch-and-bound backtracking search.
5074 * Each level in the search represents initially trivial region that is forced
5075 * to be non-trivial.
5076 * At each level we consider n cases, where n is the length of the region.
5077 * In terms of the n/2 variables of unrestricted signs being encoded by
5078 * the region, we consider the cases
5081 * x_0 = 0 and x_1 >= 1
5082 * x_0 = 0 and x_1 <= -1
5083 * x_0 = 0 and x_1 = 0 and x_2 >= 1
5084 * x_0 = 0 and x_1 = 0 and x_2 <= -1
5086 * The cases are considered in this order, assuming that each pair
5087 * x_i_a x_i_b represents the value x_i_b - x_i_a.
5088 * That is, x_0 >= 1 is enforced by adding the constraint
5089 * x_0_b - x_0_a >= 1
5091 __isl_give isl_vec
*isl_tab_basic_set_non_trivial_lexmin(
5092 __isl_take isl_basic_set
*bset
, int n_op
, int n_region
,
5093 struct isl_region
*region
,
5094 int (*conflict
)(int con
, void *user
), void *user
)
5100 isl_vec
*sol
= NULL
;
5101 struct isl_tab
*tab
;
5102 struct isl_trivial
*triv
= NULL
;
5108 ctx
= isl_basic_set_get_ctx(bset
);
5109 sol
= isl_vec_alloc(ctx
, 0);
5111 tab
= tab_for_lexmin(bset
, NULL
, 0, 0);
5114 tab
->conflict
= conflict
;
5115 tab
->conflict_user
= user
;
5117 v
= isl_vec_alloc(ctx
, 1 + tab
->n_var
);
5118 triv
= isl_calloc_array(ctx
, struct isl_trivial
, n_region
);
5119 if (!v
|| (n_region
&& !triv
))
5125 while (level
>= 0) {
5129 tab
= cut_to_integer_lexmin(tab
, CUT_ONE
);
5134 r
= first_trivial_region(tab
, n_region
, region
);
5136 for (i
= 0; i
< level
; ++i
)
5139 sol
= isl_tab_get_sample_value(tab
);
5142 if (is_optimal(sol
, n_op
))
5146 if (level
>= n_region
)
5147 isl_die(ctx
, isl_error_internal
,
5148 "nesting level too deep", goto error
);
5149 if (isl_tab_extend_cons(tab
,
5150 2 * region
[r
].len
+ 2 * n_op
) < 0)
5152 triv
[level
].region
= r
;
5153 triv
[level
].side
= 0;
5156 r
= triv
[level
].region
;
5157 side
= triv
[level
].side
;
5158 base
= 2 * (side
/2);
5160 if (side
>= region
[r
].len
) {
5165 if (isl_tab_rollback(tab
, triv
[level
].snap
) < 0)
5170 if (triv
[level
].update
) {
5171 if (force_better_solution(tab
, sol
, n_op
) < 0)
5173 triv
[level
].update
= 0;
5176 if (side
== base
&& base
>= 2) {
5177 for (j
= base
- 2; j
< base
; ++j
) {
5179 isl_int_set_si(v
->el
[1 + region
[r
].pos
+ j
], 1);
5180 if (add_lexmin_eq(tab
, v
->el
) < 0)
5185 triv
[level
].snap
= isl_tab_snap(tab
);
5186 if (isl_tab_push_basis(tab
) < 0)
5190 isl_int_set_si(v
->el
[0], -1);
5191 isl_int_set_si(v
->el
[1 + region
[r
].pos
+ side
], -1);
5192 isl_int_set_si(v
->el
[1 + region
[r
].pos
+ (side
^ 1)], 1);
5193 tab
= add_lexmin_ineq(tab
, v
->el
);
5203 isl_basic_set_free(bset
);
5210 isl_basic_set_free(bset
);
5215 /* Wrapper for a tableau that is used for computing
5216 * the lexicographically smallest rational point of a non-negative set.
5217 * This point is represented by the sample value of "tab",
5218 * unless "tab" is empty.
5220 struct isl_tab_lexmin
{
5222 struct isl_tab
*tab
;
5225 /* Free "tl" and return NULL.
5227 __isl_null isl_tab_lexmin
*isl_tab_lexmin_free(__isl_take isl_tab_lexmin
*tl
)
5231 isl_ctx_deref(tl
->ctx
);
5232 isl_tab_free(tl
->tab
);
5238 /* Construct an isl_tab_lexmin for computing
5239 * the lexicographically smallest rational point in "bset",
5240 * assuming that all variables are non-negative.
5242 __isl_give isl_tab_lexmin
*isl_tab_lexmin_from_basic_set(
5243 __isl_take isl_basic_set
*bset
)
5251 ctx
= isl_basic_set_get_ctx(bset
);
5252 tl
= isl_calloc_type(ctx
, struct isl_tab_lexmin
);
5257 tl
->tab
= tab_for_lexmin(bset
, NULL
, 0, 0);
5258 isl_basic_set_free(bset
);
5260 return isl_tab_lexmin_free(tl
);
5263 isl_basic_set_free(bset
);
5264 isl_tab_lexmin_free(tl
);
5268 /* Return the dimension of the set represented by "tl".
5270 int isl_tab_lexmin_dim(__isl_keep isl_tab_lexmin
*tl
)
5272 return tl
? tl
->tab
->n_var
: -1;
5275 /* Add the equality with coefficients "eq" to "tl", updating the optimal
5276 * solution if needed.
5277 * The equality is added as two opposite inequality constraints.
5279 __isl_give isl_tab_lexmin
*isl_tab_lexmin_add_eq(__isl_take isl_tab_lexmin
*tl
,
5285 return isl_tab_lexmin_free(tl
);
5287 if (isl_tab_extend_cons(tl
->tab
, 2) < 0)
5288 return isl_tab_lexmin_free(tl
);
5289 n_var
= tl
->tab
->n_var
;
5290 isl_seq_neg(eq
, eq
, 1 + n_var
);
5291 tl
->tab
= add_lexmin_ineq(tl
->tab
, eq
);
5292 isl_seq_neg(eq
, eq
, 1 + n_var
);
5293 tl
->tab
= add_lexmin_ineq(tl
->tab
, eq
);
5296 return isl_tab_lexmin_free(tl
);
5301 /* Return the lexicographically smallest rational point in the basic set
5302 * from which "tl" was constructed.
5303 * If the original input was empty, then return a zero-length vector.
5305 __isl_give isl_vec
*isl_tab_lexmin_get_solution(__isl_keep isl_tab_lexmin
*tl
)
5310 return isl_vec_alloc(tl
->ctx
, 0);
5312 return isl_tab_get_sample_value(tl
->tab
);
5315 /* Return the lexicographically smallest rational point in "bset",
5316 * assuming that all variables are non-negative.
5317 * If "bset" is empty, then return a zero-length vector.
5319 __isl_give isl_vec
*isl_tab_basic_set_non_neg_lexmin(
5320 __isl_take isl_basic_set
*bset
)
5325 tl
= isl_tab_lexmin_from_basic_set(bset
);
5326 sol
= isl_tab_lexmin_get_solution(tl
);
5327 isl_tab_lexmin_free(tl
);
5331 struct isl_sol_pma
{
5333 isl_pw_multi_aff
*pma
;
5337 static void sol_pma_free(struct isl_sol_pma
*sol_pma
)
5341 if (sol_pma
->sol
.context
)
5342 sol_pma
->sol
.context
->op
->free(sol_pma
->sol
.context
);
5343 isl_pw_multi_aff_free(sol_pma
->pma
);
5344 isl_set_free(sol_pma
->empty
);
5348 /* This function is called for parts of the context where there is
5349 * no solution, with "bset" corresponding to the context tableau.
5350 * Simply add the basic set to the set "empty".
5352 static void sol_pma_add_empty(struct isl_sol_pma
*sol
,
5353 __isl_take isl_basic_set
*bset
)
5355 if (!bset
|| !sol
->empty
)
5358 sol
->empty
= isl_set_grow(sol
->empty
, 1);
5359 bset
= isl_basic_set_simplify(bset
);
5360 bset
= isl_basic_set_finalize(bset
);
5361 sol
->empty
= isl_set_add_basic_set(sol
->empty
, bset
);
5366 isl_basic_set_free(bset
);
5370 /* Check that the final columns of "M", starting at "first", are zero.
5372 static isl_stat
check_final_columns_are_zero(__isl_keep isl_mat
*M
,
5376 unsigned rows
, cols
, n
;
5379 return isl_stat_error
;
5380 rows
= isl_mat_rows(M
);
5381 cols
= isl_mat_cols(M
);
5383 for (i
= 0; i
< rows
; ++i
)
5384 if (isl_seq_first_non_zero(M
->row
[i
] + first
, n
) != -1)
5385 isl_die(isl_mat_get_ctx(M
), isl_error_internal
,
5386 "final columns should be zero",
5387 return isl_stat_error
);
5391 /* Set the affine expressions in "ma" according to the rows in "M", which
5392 * are defined over the local space "ls".
5393 * The matrix "M" may have extra (zero) columns beyond the number
5394 * of variables in "ls".
5396 static __isl_give isl_multi_aff
*set_from_affine_matrix(
5397 __isl_take isl_multi_aff
*ma
, __isl_take isl_local_space
*ls
,
5398 __isl_take isl_mat
*M
)
5403 if (!ma
|| !ls
|| !M
)
5406 dim
= isl_local_space_dim(ls
, isl_dim_all
);
5407 if (check_final_columns_are_zero(M
, 1 + dim
) < 0)
5409 for (i
= 1; i
< M
->n_row
; ++i
) {
5410 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
5412 isl_int_set(aff
->v
->el
[0], M
->row
[0][0]);
5413 isl_seq_cpy(aff
->v
->el
+ 1, M
->row
[i
], 1 + dim
);
5415 aff
= isl_aff_normalize(aff
);
5416 ma
= isl_multi_aff_set_aff(ma
, i
- 1, aff
);
5418 isl_local_space_free(ls
);
5423 isl_local_space_free(ls
);
5425 isl_multi_aff_free(ma
);
5429 /* Given a basic set "dom" that represents the context and an affine
5430 * matrix "M" that maps the dimensions of the context to the
5431 * output variables, construct an isl_pw_multi_aff with a single
5432 * cell corresponding to "dom" and affine expressions copied from "M".
5434 * Note that the description of the initial context may have involved
5435 * existentially quantified variables, in which case they also appear
5436 * in "dom". These need to be removed before creating the affine
5437 * expression because an affine expression cannot be defined in terms
5438 * of existentially quantified variables without a known representation.
5439 * Since newly added integer divisions are inserted before these
5440 * existentially quantified variables, they are still in the final
5441 * positions and the corresponding final columns of "M" are zero
5442 * because align_context_divs adds the existentially quantified
5443 * variables of the context to the main tableau without any constraints and
5444 * any equality constraints that are added later on can only serve
5445 * to eliminate these existentially quantified variables.
5447 static void sol_pma_add(struct isl_sol_pma
*sol
,
5448 __isl_take isl_basic_set
*dom
, __isl_take isl_mat
*M
)
5450 isl_local_space
*ls
;
5451 isl_multi_aff
*maff
;
5452 isl_pw_multi_aff
*pma
;
5455 n_div
= isl_basic_set_dim(dom
, isl_dim_div
);
5456 n_known
= n_div
- sol
->sol
.context
->n_unknown
;
5458 maff
= isl_multi_aff_alloc(isl_pw_multi_aff_get_space(sol
->pma
));
5459 ls
= isl_basic_set_get_local_space(dom
);
5460 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
,
5461 n_known
, n_div
- n_known
);
5462 maff
= set_from_affine_matrix(maff
, ls
, M
);
5463 dom
= isl_basic_set_simplify(dom
);
5464 dom
= isl_basic_set_finalize(dom
);
5465 pma
= isl_pw_multi_aff_alloc(isl_set_from_basic_set(dom
), maff
);
5466 sol
->pma
= isl_pw_multi_aff_add_disjoint(sol
->pma
, pma
);
5471 static void sol_pma_free_wrap(struct isl_sol
*sol
)
5473 sol_pma_free((struct isl_sol_pma
*)sol
);
5476 static void sol_pma_add_empty_wrap(struct isl_sol
*sol
,
5477 __isl_take isl_basic_set
*bset
)
5479 sol_pma_add_empty((struct isl_sol_pma
*)sol
, bset
);
5482 static void sol_pma_add_wrap(struct isl_sol
*sol
,
5483 __isl_take isl_basic_set
*dom
, __isl_take isl_mat
*M
)
5485 sol_pma_add((struct isl_sol_pma
*)sol
, dom
, M
);
5488 /* Construct an isl_sol_pma structure for accumulating the solution.
5489 * If track_empty is set, then we also keep track of the parts
5490 * of the context where there is no solution.
5491 * If max is set, then we are solving a maximization, rather than
5492 * a minimization problem, which means that the variables in the
5493 * tableau have value "M - x" rather than "M + x".
5495 static struct isl_sol
*sol_pma_init(__isl_keep isl_basic_map
*bmap
,
5496 __isl_take isl_basic_set
*dom
, int track_empty
, int max
)
5498 struct isl_sol_pma
*sol_pma
= NULL
;
5503 sol_pma
= isl_calloc_type(bmap
->ctx
, struct isl_sol_pma
);
5507 sol_pma
->sol
.rational
= ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
5508 sol_pma
->sol
.dec_level
.callback
.run
= &sol_dec_level_wrap
;
5509 sol_pma
->sol
.dec_level
.sol
= &sol_pma
->sol
;
5510 sol_pma
->sol
.max
= max
;
5511 sol_pma
->sol
.n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5512 sol_pma
->sol
.add
= &sol_pma_add_wrap
;
5513 sol_pma
->sol
.add_empty
= track_empty
? &sol_pma_add_empty_wrap
: NULL
;
5514 sol_pma
->sol
.free
= &sol_pma_free_wrap
;
5515 sol_pma
->pma
= isl_pw_multi_aff_empty(isl_basic_map_get_space(bmap
));
5519 sol_pma
->sol
.context
= isl_context_alloc(dom
);
5520 if (!sol_pma
->sol
.context
)
5524 sol_pma
->empty
= isl_set_alloc_space(isl_basic_set_get_space(dom
),
5525 1, ISL_SET_DISJOINT
);
5526 if (!sol_pma
->empty
)
5530 isl_basic_set_free(dom
);
5531 return &sol_pma
->sol
;
5533 isl_basic_set_free(dom
);
5534 sol_pma_free(sol_pma
);
5538 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
5539 * some obvious symmetries.
5541 * We call basic_map_partial_lexopt_base_sol and extract the results.
5543 static __isl_give isl_pw_multi_aff
*basic_map_partial_lexopt_base_pw_multi_aff(
5544 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5545 __isl_give isl_set
**empty
, int max
)
5547 isl_pw_multi_aff
*result
= NULL
;
5548 struct isl_sol
*sol
;
5549 struct isl_sol_pma
*sol_pma
;
5551 sol
= basic_map_partial_lexopt_base_sol(bmap
, dom
, empty
, max
,
5555 sol_pma
= (struct isl_sol_pma
*) sol
;
5557 result
= isl_pw_multi_aff_copy(sol_pma
->pma
);
5559 *empty
= isl_set_copy(sol_pma
->empty
);
5560 sol_free(&sol_pma
->sol
);
5564 /* Given that the last input variable of "maff" represents the minimum
5565 * of some bounds, check whether we need to plug in the expression
5568 * In particular, check if the last input variable appears in any
5569 * of the expressions in "maff".
5571 static int need_substitution(__isl_keep isl_multi_aff
*maff
)
5576 pos
= isl_multi_aff_dim(maff
, isl_dim_in
) - 1;
5578 for (i
= 0; i
< maff
->n
; ++i
)
5579 if (isl_aff_involves_dims(maff
->p
[i
], isl_dim_in
, pos
, 1))
5585 /* Given a set of upper bounds on the last "input" variable m,
5586 * construct a piecewise affine expression that selects
5587 * the minimal upper bound to m, i.e.,
5588 * divide the space into cells where one
5589 * of the upper bounds is smaller than all the others and select
5590 * this upper bound on that cell.
5592 * In particular, if there are n bounds b_i, then the result
5593 * consists of n cell, each one of the form
5595 * b_i <= b_j for j > i
5596 * b_i < b_j for j < i
5598 * The affine expression on this cell is
5602 static __isl_give isl_pw_aff
*set_minimum_pa(__isl_take isl_space
*space
,
5603 __isl_take isl_mat
*var
)
5606 isl_aff
*aff
= NULL
;
5607 isl_basic_set
*bset
= NULL
;
5608 isl_pw_aff
*paff
= NULL
;
5609 isl_space
*pw_space
;
5610 isl_local_space
*ls
= NULL
;
5615 ls
= isl_local_space_from_space(isl_space_copy(space
));
5616 pw_space
= isl_space_copy(space
);
5617 pw_space
= isl_space_from_domain(pw_space
);
5618 pw_space
= isl_space_add_dims(pw_space
, isl_dim_out
, 1);
5619 paff
= isl_pw_aff_alloc_size(pw_space
, var
->n_row
);
5621 for (i
= 0; i
< var
->n_row
; ++i
) {
5624 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
5625 bset
= isl_basic_set_alloc_space(isl_space_copy(space
), 0,
5629 isl_int_set_si(aff
->v
->el
[0], 1);
5630 isl_seq_cpy(aff
->v
->el
+ 1, var
->row
[i
], var
->n_col
);
5631 isl_int_set_si(aff
->v
->el
[1 + var
->n_col
], 0);
5632 bset
= select_minimum(bset
, var
, i
);
5633 paff_i
= isl_pw_aff_alloc(isl_set_from_basic_set(bset
), aff
);
5634 paff
= isl_pw_aff_add_disjoint(paff
, paff_i
);
5637 isl_local_space_free(ls
);
5638 isl_space_free(space
);
5643 isl_basic_set_free(bset
);
5644 isl_pw_aff_free(paff
);
5645 isl_local_space_free(ls
);
5646 isl_space_free(space
);
5651 /* Given a piecewise multi-affine expression of which the last input variable
5652 * is the minimum of the bounds in "cst", plug in the value of the minimum.
5653 * This minimum expression is given in "min_expr_pa".
5654 * The set "min_expr" contains the same information, but in the form of a set.
5655 * The variable is subsequently projected out.
5657 * The implementation is similar to those of "split" and "split_domain".
5658 * If the variable appears in a given expression, then minimum expression
5659 * is plugged in. Otherwise, if the variable appears in the constraints
5660 * and a split is required, then the domain is split. Otherwise, no split
5663 static __isl_give isl_pw_multi_aff
*split_domain_pma(
5664 __isl_take isl_pw_multi_aff
*opt
, __isl_take isl_pw_aff
*min_expr_pa
,
5665 __isl_take isl_set
*min_expr
, __isl_take isl_mat
*cst
)
5670 isl_pw_multi_aff
*res
;
5672 if (!opt
|| !min_expr
|| !cst
)
5675 n_in
= isl_pw_multi_aff_dim(opt
, isl_dim_in
);
5676 space
= isl_pw_multi_aff_get_space(opt
);
5677 space
= isl_space_drop_dims(space
, isl_dim_in
, n_in
- 1, 1);
5678 res
= isl_pw_multi_aff_empty(space
);
5680 for (i
= 0; i
< opt
->n
; ++i
) {
5681 isl_pw_multi_aff
*pma
;
5683 pma
= isl_pw_multi_aff_alloc(isl_set_copy(opt
->p
[i
].set
),
5684 isl_multi_aff_copy(opt
->p
[i
].maff
));
5685 if (need_substitution(opt
->p
[i
].maff
))
5686 pma
= isl_pw_multi_aff_substitute(pma
,
5687 isl_dim_in
, n_in
- 1, min_expr_pa
);
5688 else if (need_split_set(opt
->p
[i
].set
, cst
))
5689 pma
= isl_pw_multi_aff_intersect_domain(pma
,
5690 isl_set_copy(min_expr
));
5691 pma
= isl_pw_multi_aff_project_out(pma
,
5692 isl_dim_in
, n_in
- 1, 1);
5694 res
= isl_pw_multi_aff_add_disjoint(res
, pma
);
5697 isl_pw_multi_aff_free(opt
);
5698 isl_pw_aff_free(min_expr_pa
);
5699 isl_set_free(min_expr
);
5703 isl_pw_multi_aff_free(opt
);
5704 isl_pw_aff_free(min_expr_pa
);
5705 isl_set_free(min_expr
);
5710 static __isl_give isl_pw_multi_aff
*basic_map_partial_lexopt_pw_multi_aff(
5711 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5712 __isl_give isl_set
**empty
, int max
);
5714 /* This function is called from basic_map_partial_lexopt_symm.
5715 * The last variable of "bmap" and "dom" corresponds to the minimum
5716 * of the bounds in "cst". "map_space" is the space of the original
5717 * input relation (of basic_map_partial_lexopt_symm) and "set_space"
5718 * is the space of the original domain.
5720 * We recursively call basic_map_partial_lexopt and then plug in
5721 * the definition of the minimum in the result.
5723 static __isl_give isl_pw_multi_aff
*
5724 basic_map_partial_lexopt_symm_core_pw_multi_aff(
5725 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5726 __isl_give isl_set
**empty
, int max
, __isl_take isl_mat
*cst
,
5727 __isl_take isl_space
*map_space
, __isl_take isl_space
*set_space
)
5729 isl_pw_multi_aff
*opt
;
5730 isl_pw_aff
*min_expr_pa
;
5733 min_expr
= set_minimum(isl_basic_set_get_space(dom
), isl_mat_copy(cst
));
5734 min_expr_pa
= set_minimum_pa(isl_basic_set_get_space(dom
),
5737 opt
= basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, empty
, max
);
5740 *empty
= split(*empty
,
5741 isl_set_copy(min_expr
), isl_mat_copy(cst
));
5742 *empty
= isl_set_reset_space(*empty
, set_space
);
5745 opt
= split_domain_pma(opt
, min_expr_pa
, min_expr
, cst
);
5746 opt
= isl_pw_multi_aff_reset_space(opt
, map_space
);
5752 #define TYPE isl_pw_multi_aff
5754 #define SUFFIX _pw_multi_aff
5755 #include "isl_tab_lexopt_templ.c"