2 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Use of this software is governed by the GNU LGPLv2.1 license
6 * Written by Sven Verdoolaege, K.U.Leuven, Departement
7 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 #include "isl_map_private.h"
13 #include "isl_sample.h"
16 * The implementation of parametric integer linear programming in this file
17 * was inspired by the paper "Parametric Integer Programming" and the
18 * report "Solving systems of affine (in)equalities" by Paul Feautrier
21 * The strategy used for obtaining a feasible solution is different
22 * from the one used in isl_tab.c. In particular, in isl_tab.c,
23 * upon finding a constraint that is not yet satisfied, we pivot
24 * in a row that increases the constant term of row holding the
25 * constraint, making sure the sample solution remains feasible
26 * for all the constraints it already satisfied.
27 * Here, we always pivot in the row holding the constraint,
28 * choosing a column that induces the lexicographically smallest
29 * increment to the sample solution.
31 * By starting out from a sample value that is lexicographically
32 * smaller than any integer point in the problem space, the first
33 * feasible integer sample point we find will also be the lexicographically
34 * smallest. If all variables can be assumed to be non-negative,
35 * then the initial sample value may be chosen equal to zero.
36 * However, we will not make this assumption. Instead, we apply
37 * the "big parameter" trick. Any variable x is then not directly
38 * used in the tableau, but instead it its represented by another
39 * variable x' = M + x, where M is an arbitrarily large (positive)
40 * value. x' is therefore always non-negative, whatever the value of x.
41 * Taking as initial smaple value x' = 0 corresponds to x = -M,
42 * which is always smaller than any possible value of x.
44 * The big parameter trick is used in the main tableau and
45 * also in the context tableau if isl_context_lex is used.
46 * In this case, each tableaus has its own big parameter.
47 * Before doing any real work, we check if all the parameters
48 * happen to be non-negative. If so, we drop the column corresponding
49 * to M from the initial context tableau.
50 * If isl_context_gbr is used, then the big parameter trick is only
51 * used in the main tableau.
55 struct isl_context_op
{
56 /* detect nonnegative parameters in context and mark them in tab */
57 struct isl_tab
*(*detect_nonnegative_parameters
)(
58 struct isl_context
*context
, struct isl_tab
*tab
);
59 /* return temporary reference to basic set representation of context */
60 struct isl_basic_set
*(*peek_basic_set
)(struct isl_context
*context
);
61 /* return temporary reference to tableau representation of context */
62 struct isl_tab
*(*peek_tab
)(struct isl_context
*context
);
63 /* add equality; check is 1 if eq may not be valid;
64 * update is 1 if we may want to call ineq_sign on context later.
66 void (*add_eq
)(struct isl_context
*context
, isl_int
*eq
,
67 int check
, int update
);
68 /* add inequality; check is 1 if ineq may not be valid;
69 * update is 1 if we may want to call ineq_sign on context later.
71 void (*add_ineq
)(struct isl_context
*context
, isl_int
*ineq
,
72 int check
, int update
);
73 /* check sign of ineq based on previous information.
74 * strict is 1 if saturation should be treated as a positive sign.
76 enum isl_tab_row_sign (*ineq_sign
)(struct isl_context
*context
,
77 isl_int
*ineq
, int strict
);
78 /* check if inequality maintains feasibility */
79 int (*test_ineq
)(struct isl_context
*context
, isl_int
*ineq
);
80 /* return index of a div that corresponds to "div" */
81 int (*get_div
)(struct isl_context
*context
, struct isl_tab
*tab
,
83 /* add div "div" to context and return index and non-negativity */
84 int (*add_div
)(struct isl_context
*context
, struct isl_vec
*div
,
86 int (*detect_equalities
)(struct isl_context
*context
,
88 /* return row index of "best" split */
89 int (*best_split
)(struct isl_context
*context
, struct isl_tab
*tab
);
90 /* check if context has already been determined to be empty */
91 int (*is_empty
)(struct isl_context
*context
);
92 /* check if context is still usable */
93 int (*is_ok
)(struct isl_context
*context
);
94 /* save a copy/snapshot of context */
95 void *(*save
)(struct isl_context
*context
);
96 /* restore saved context */
97 void (*restore
)(struct isl_context
*context
, void *);
98 /* invalidate context */
99 void (*invalidate
)(struct isl_context
*context
);
101 void (*free
)(struct isl_context
*context
);
105 struct isl_context_op
*op
;
108 struct isl_context_lex
{
109 struct isl_context context
;
113 struct isl_partial_sol
{
115 struct isl_basic_set
*dom
;
118 struct isl_partial_sol
*next
;
122 struct isl_sol_callback
{
123 struct isl_tab_callback callback
;
127 /* isl_sol is an interface for constructing a solution to
128 * a parametric integer linear programming problem.
129 * Every time the algorithm reaches a state where a solution
130 * can be read off from the tableau (including cases where the tableau
131 * is empty), the function "add" is called on the isl_sol passed
132 * to find_solutions_main.
134 * The context tableau is owned by isl_sol and is updated incrementally.
136 * There are currently two implementations of this interface,
137 * isl_sol_map, which simply collects the solutions in an isl_map
138 * and (optionally) the parts of the context where there is no solution
140 * isl_sol_for, which calls a user-defined function for each part of
149 struct isl_context
*context
;
150 struct isl_partial_sol
*partial
;
151 void (*add
)(struct isl_sol
*sol
,
152 struct isl_basic_set
*dom
, struct isl_mat
*M
);
153 void (*add_empty
)(struct isl_sol
*sol
, struct isl_basic_set
*bset
);
154 void (*free
)(struct isl_sol
*sol
);
155 struct isl_sol_callback dec_level
;
158 static void sol_free(struct isl_sol
*sol
)
160 struct isl_partial_sol
*partial
, *next
;
163 for (partial
= sol
->partial
; partial
; partial
= next
) {
164 next
= partial
->next
;
165 isl_basic_set_free(partial
->dom
);
166 isl_mat_free(partial
->M
);
172 /* Push a partial solution represented by a domain and mapping M
173 * onto the stack of partial solutions.
175 static void sol_push_sol(struct isl_sol
*sol
,
176 struct isl_basic_set
*dom
, struct isl_mat
*M
)
178 struct isl_partial_sol
*partial
;
180 if (sol
->error
|| !dom
)
183 partial
= isl_alloc_type(dom
->ctx
, struct isl_partial_sol
);
187 partial
->level
= sol
->level
;
190 partial
->next
= sol
->partial
;
192 sol
->partial
= partial
;
196 isl_basic_set_free(dom
);
200 /* Pop one partial solution from the partial solution stack and
201 * pass it on to sol->add or sol->add_empty.
203 static void sol_pop_one(struct isl_sol
*sol
)
205 struct isl_partial_sol
*partial
;
207 partial
= sol
->partial
;
208 sol
->partial
= partial
->next
;
211 sol
->add(sol
, partial
->dom
, partial
->M
);
213 sol
->add_empty(sol
, partial
->dom
);
217 /* Return a fresh copy of the domain represented by the context tableau.
219 static struct isl_basic_set
*sol_domain(struct isl_sol
*sol
)
221 struct isl_basic_set
*bset
;
226 bset
= isl_basic_set_dup(sol
->context
->op
->peek_basic_set(sol
->context
));
227 bset
= isl_basic_set_update_from_tab(bset
,
228 sol
->context
->op
->peek_tab(sol
->context
));
233 /* Check whether two partial solutions have the same mapping, where n_div
234 * is the number of divs that the two partial solutions have in common.
236 static int same_solution(struct isl_partial_sol
*s1
, struct isl_partial_sol
*s2
,
242 if (!s1
->M
!= !s2
->M
)
247 dim
= isl_basic_set_total_dim(s1
->dom
) - s1
->dom
->n_div
;
249 for (i
= 0; i
< s1
->M
->n_row
; ++i
) {
250 if (isl_seq_first_non_zero(s1
->M
->row
[i
]+1+dim
+n_div
,
251 s1
->M
->n_col
-1-dim
-n_div
) != -1)
253 if (isl_seq_first_non_zero(s2
->M
->row
[i
]+1+dim
+n_div
,
254 s2
->M
->n_col
-1-dim
-n_div
) != -1)
256 if (!isl_seq_eq(s1
->M
->row
[i
], s2
->M
->row
[i
], 1+dim
+n_div
))
262 /* Pop all solutions from the partial solution stack that were pushed onto
263 * the stack at levels that are deeper than the current level.
264 * If the two topmost elements on the stack have the same level
265 * and represent the same solution, then their domains are combined.
266 * This combined domain is the same as the current context domain
267 * as sol_pop is called each time we move back to a higher level.
269 static void sol_pop(struct isl_sol
*sol
)
271 struct isl_partial_sol
*partial
;
277 if (sol
->level
== 0) {
278 for (partial
= sol
->partial
; partial
; partial
= sol
->partial
)
283 partial
= sol
->partial
;
287 if (partial
->level
<= sol
->level
)
290 if (partial
->next
&& partial
->next
->level
== partial
->level
) {
291 n_div
= isl_basic_set_dim(
292 sol
->context
->op
->peek_basic_set(sol
->context
),
295 if (!same_solution(partial
, partial
->next
, n_div
)) {
299 struct isl_basic_set
*bset
;
301 bset
= sol_domain(sol
);
303 isl_basic_set_free(partial
->next
->dom
);
304 partial
->next
->dom
= bset
;
305 partial
->next
->level
= sol
->level
;
307 sol
->partial
= partial
->next
;
308 isl_basic_set_free(partial
->dom
);
309 isl_mat_free(partial
->M
);
316 static void sol_dec_level(struct isl_sol
*sol
)
326 static int sol_dec_level_wrap(struct isl_tab_callback
*cb
)
328 struct isl_sol_callback
*callback
= (struct isl_sol_callback
*)cb
;
330 sol_dec_level(callback
->sol
);
332 return callback
->sol
->error
? -1 : 0;
335 /* Move down to next level and push callback onto context tableau
336 * to decrease the level again when it gets rolled back across
337 * the current state. That is, dec_level will be called with
338 * the context tableau in the same state as it is when inc_level
341 static void sol_inc_level(struct isl_sol
*sol
)
349 tab
= sol
->context
->op
->peek_tab(sol
->context
);
350 if (isl_tab_push_callback(tab
, &sol
->dec_level
.callback
) < 0)
354 static void scale_rows(struct isl_mat
*mat
, isl_int m
, int n_row
)
358 if (isl_int_is_one(m
))
361 for (i
= 0; i
< n_row
; ++i
)
362 isl_seq_scale(mat
->row
[i
], mat
->row
[i
], m
, mat
->n_col
);
365 /* Add the solution identified by the tableau and the context tableau.
367 * The layout of the variables is as follows.
368 * tab->n_var is equal to the total number of variables in the input
369 * map (including divs that were copied from the context)
370 * + the number of extra divs constructed
371 * Of these, the first tab->n_param and the last tab->n_div variables
372 * correspond to the variables in the context, i.e.,
373 * tab->n_param + tab->n_div = context_tab->n_var
374 * tab->n_param is equal to the number of parameters and input
375 * dimensions in the input map
376 * tab->n_div is equal to the number of divs in the context
378 * If there is no solution, then call add_empty with a basic set
379 * that corresponds to the context tableau. (If add_empty is NULL,
382 * If there is a solution, then first construct a matrix that maps
383 * all dimensions of the context to the output variables, i.e.,
384 * the output dimensions in the input map.
385 * The divs in the input map (if any) that do not correspond to any
386 * div in the context do not appear in the solution.
387 * The algorithm will make sure that they have an integer value,
388 * but these values themselves are of no interest.
389 * We have to be careful not to drop or rearrange any divs in the
390 * context because that would change the meaning of the matrix.
392 * To extract the value of the output variables, it should be noted
393 * that we always use a big parameter M in the main tableau and so
394 * the variable stored in this tableau is not an output variable x itself, but
395 * x' = M + x (in case of minimization)
397 * x' = M - x (in case of maximization)
398 * If x' appears in a column, then its optimal value is zero,
399 * which means that the optimal value of x is an unbounded number
400 * (-M for minimization and M for maximization).
401 * We currently assume that the output dimensions in the original map
402 * are bounded, so this cannot occur.
403 * Similarly, when x' appears in a row, then the coefficient of M in that
404 * row is necessarily 1.
405 * If the row in the tableau represents
406 * d x' = c + d M + e(y)
407 * then, in case of minimization, the corresponding row in the matrix
410 * with a d = m, the (updated) common denominator of the matrix.
411 * In case of maximization, the row will be
414 static void sol_add(struct isl_sol
*sol
, struct isl_tab
*tab
)
416 struct isl_basic_set
*bset
= NULL
;
417 struct isl_mat
*mat
= NULL
;
422 if (sol
->error
|| !tab
)
425 if (tab
->empty
&& !sol
->add_empty
)
428 bset
= sol_domain(sol
);
431 sol_push_sol(sol
, bset
, NULL
);
437 mat
= isl_mat_alloc(tab
->mat
->ctx
, 1 + sol
->n_out
,
438 1 + tab
->n_param
+ tab
->n_div
);
444 isl_seq_clr(mat
->row
[0] + 1, mat
->n_col
- 1);
445 isl_int_set_si(mat
->row
[0][0], 1);
446 for (row
= 0; row
< sol
->n_out
; ++row
) {
447 int i
= tab
->n_param
+ row
;
450 isl_seq_clr(mat
->row
[1 + row
], mat
->n_col
);
451 if (!tab
->var
[i
].is_row
) {
453 isl_assert(mat
->ctx
, !tab
->M
, goto error2
);
457 r
= tab
->var
[i
].index
;
460 isl_assert(mat
->ctx
, isl_int_eq(tab
->mat
->row
[r
][2],
461 tab
->mat
->row
[r
][0]),
463 isl_int_gcd(m
, mat
->row
[0][0], tab
->mat
->row
[r
][0]);
464 isl_int_divexact(m
, tab
->mat
->row
[r
][0], m
);
465 scale_rows(mat
, m
, 1 + row
);
466 isl_int_divexact(m
, mat
->row
[0][0], tab
->mat
->row
[r
][0]);
467 isl_int_mul(mat
->row
[1 + row
][0], m
, tab
->mat
->row
[r
][1]);
468 for (j
= 0; j
< tab
->n_param
; ++j
) {
470 if (tab
->var
[j
].is_row
)
472 col
= tab
->var
[j
].index
;
473 isl_int_mul(mat
->row
[1 + row
][1 + j
], m
,
474 tab
->mat
->row
[r
][off
+ col
]);
476 for (j
= 0; j
< tab
->n_div
; ++j
) {
478 if (tab
->var
[tab
->n_var
- tab
->n_div
+j
].is_row
)
480 col
= tab
->var
[tab
->n_var
- tab
->n_div
+j
].index
;
481 isl_int_mul(mat
->row
[1 + row
][1 + tab
->n_param
+ j
], m
,
482 tab
->mat
->row
[r
][off
+ col
]);
485 isl_seq_neg(mat
->row
[1 + row
], mat
->row
[1 + row
],
491 sol_push_sol(sol
, bset
, mat
);
496 isl_basic_set_free(bset
);
504 struct isl_set
*empty
;
507 static void sol_map_free(struct isl_sol_map
*sol_map
)
509 if (sol_map
->sol
.context
)
510 sol_map
->sol
.context
->op
->free(sol_map
->sol
.context
);
511 isl_map_free(sol_map
->map
);
512 isl_set_free(sol_map
->empty
);
516 static void sol_map_free_wrap(struct isl_sol
*sol
)
518 sol_map_free((struct isl_sol_map
*)sol
);
521 /* This function is called for parts of the context where there is
522 * no solution, with "bset" corresponding to the context tableau.
523 * Simply add the basic set to the set "empty".
525 static void sol_map_add_empty(struct isl_sol_map
*sol
,
526 struct isl_basic_set
*bset
)
530 isl_assert(bset
->ctx
, sol
->empty
, goto error
);
532 sol
->empty
= isl_set_grow(sol
->empty
, 1);
533 bset
= isl_basic_set_simplify(bset
);
534 bset
= isl_basic_set_finalize(bset
);
535 sol
->empty
= isl_set_add(sol
->empty
, isl_basic_set_copy(bset
));
538 isl_basic_set_free(bset
);
541 isl_basic_set_free(bset
);
545 static void sol_map_add_empty_wrap(struct isl_sol
*sol
,
546 struct isl_basic_set
*bset
)
548 sol_map_add_empty((struct isl_sol_map
*)sol
, bset
);
551 /* Given a basic map "dom" that represents the context and an affine
552 * matrix "M" that maps the dimensions of the context to the
553 * output variables, construct a basic map with the same parameters
554 * and divs as the context, the dimensions of the context as input
555 * dimensions and a number of output dimensions that is equal to
556 * the number of output dimensions in the input map.
558 * The constraints and divs of the context are simply copied
559 * from "dom". For each row
563 * is added, with d the common denominator of M.
565 static void sol_map_add(struct isl_sol_map
*sol
,
566 struct isl_basic_set
*dom
, struct isl_mat
*M
)
569 struct isl_basic_map
*bmap
= NULL
;
570 isl_basic_set
*context_bset
;
578 if (sol
->sol
.error
|| !dom
|| !M
)
581 n_out
= sol
->sol
.n_out
;
582 n_eq
= dom
->n_eq
+ n_out
;
583 n_ineq
= dom
->n_ineq
;
585 nparam
= isl_basic_set_total_dim(dom
) - n_div
;
586 total
= isl_map_dim(sol
->map
, isl_dim_all
);
587 bmap
= isl_basic_map_alloc_dim(isl_map_get_dim(sol
->map
),
588 n_div
, n_eq
, 2 * n_div
+ n_ineq
);
591 if (sol
->sol
.rational
)
592 ISL_F_SET(bmap
, ISL_BASIC_MAP_RATIONAL
);
593 for (i
= 0; i
< dom
->n_div
; ++i
) {
594 int k
= isl_basic_map_alloc_div(bmap
);
597 isl_seq_cpy(bmap
->div
[k
], dom
->div
[i
], 1 + 1 + nparam
);
598 isl_seq_clr(bmap
->div
[k
] + 1 + 1 + nparam
, total
- nparam
);
599 isl_seq_cpy(bmap
->div
[k
] + 1 + 1 + total
,
600 dom
->div
[i
] + 1 + 1 + nparam
, i
);
602 for (i
= 0; i
< dom
->n_eq
; ++i
) {
603 int k
= isl_basic_map_alloc_equality(bmap
);
606 isl_seq_cpy(bmap
->eq
[k
], dom
->eq
[i
], 1 + nparam
);
607 isl_seq_clr(bmap
->eq
[k
] + 1 + nparam
, total
- nparam
);
608 isl_seq_cpy(bmap
->eq
[k
] + 1 + total
,
609 dom
->eq
[i
] + 1 + nparam
, n_div
);
611 for (i
= 0; i
< dom
->n_ineq
; ++i
) {
612 int k
= isl_basic_map_alloc_inequality(bmap
);
615 isl_seq_cpy(bmap
->ineq
[k
], dom
->ineq
[i
], 1 + nparam
);
616 isl_seq_clr(bmap
->ineq
[k
] + 1 + nparam
, total
- nparam
);
617 isl_seq_cpy(bmap
->ineq
[k
] + 1 + total
,
618 dom
->ineq
[i
] + 1 + nparam
, n_div
);
620 for (i
= 0; i
< M
->n_row
- 1; ++i
) {
621 int k
= isl_basic_map_alloc_equality(bmap
);
624 isl_seq_cpy(bmap
->eq
[k
], M
->row
[1 + i
], 1 + nparam
);
625 isl_seq_clr(bmap
->eq
[k
] + 1 + nparam
, n_out
);
626 isl_int_neg(bmap
->eq
[k
][1 + nparam
+ i
], M
->row
[0][0]);
627 isl_seq_cpy(bmap
->eq
[k
] + 1 + nparam
+ n_out
,
628 M
->row
[1 + i
] + 1 + nparam
, n_div
);
630 bmap
= isl_basic_map_simplify(bmap
);
631 bmap
= isl_basic_map_finalize(bmap
);
632 sol
->map
= isl_map_grow(sol
->map
, 1);
633 sol
->map
= isl_map_add(sol
->map
, bmap
);
636 isl_basic_set_free(dom
);
640 isl_basic_set_free(dom
);
642 isl_basic_map_free(bmap
);
646 static void sol_map_add_wrap(struct isl_sol
*sol
,
647 struct isl_basic_set
*dom
, struct isl_mat
*M
)
649 sol_map_add((struct isl_sol_map
*)sol
, dom
, M
);
653 /* Store the "parametric constant" of row "row" of tableau "tab" in "line",
654 * i.e., the constant term and the coefficients of all variables that
655 * appear in the context tableau.
656 * Note that the coefficient of the big parameter M is NOT copied.
657 * The context tableau may not have a big parameter and even when it
658 * does, it is a different big parameter.
660 static void get_row_parameter_line(struct isl_tab
*tab
, int row
, isl_int
*line
)
663 unsigned off
= 2 + tab
->M
;
665 isl_int_set(line
[0], tab
->mat
->row
[row
][1]);
666 for (i
= 0; i
< tab
->n_param
; ++i
) {
667 if (tab
->var
[i
].is_row
)
668 isl_int_set_si(line
[1 + i
], 0);
670 int col
= tab
->var
[i
].index
;
671 isl_int_set(line
[1 + i
], tab
->mat
->row
[row
][off
+ col
]);
674 for (i
= 0; i
< tab
->n_div
; ++i
) {
675 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
676 isl_int_set_si(line
[1 + tab
->n_param
+ i
], 0);
678 int col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
679 isl_int_set(line
[1 + tab
->n_param
+ i
],
680 tab
->mat
->row
[row
][off
+ col
]);
685 /* Check if rows "row1" and "row2" have identical "parametric constants",
686 * as explained above.
687 * In this case, we also insist that the coefficients of the big parameter
688 * be the same as the values of the constants will only be the same
689 * if these coefficients are also the same.
691 static int identical_parameter_line(struct isl_tab
*tab
, int row1
, int row2
)
694 unsigned off
= 2 + tab
->M
;
696 if (isl_int_ne(tab
->mat
->row
[row1
][1], tab
->mat
->row
[row2
][1]))
699 if (tab
->M
&& isl_int_ne(tab
->mat
->row
[row1
][2],
700 tab
->mat
->row
[row2
][2]))
703 for (i
= 0; i
< tab
->n_param
+ tab
->n_div
; ++i
) {
704 int pos
= i
< tab
->n_param
? i
:
705 tab
->n_var
- tab
->n_div
+ i
- tab
->n_param
;
708 if (tab
->var
[pos
].is_row
)
710 col
= tab
->var
[pos
].index
;
711 if (isl_int_ne(tab
->mat
->row
[row1
][off
+ col
],
712 tab
->mat
->row
[row2
][off
+ col
]))
718 /* Return an inequality that expresses that the "parametric constant"
719 * should be non-negative.
720 * This function is only called when the coefficient of the big parameter
723 static struct isl_vec
*get_row_parameter_ineq(struct isl_tab
*tab
, int row
)
725 struct isl_vec
*ineq
;
727 ineq
= isl_vec_alloc(tab
->mat
->ctx
, 1 + tab
->n_param
+ tab
->n_div
);
731 get_row_parameter_line(tab
, row
, ineq
->el
);
733 ineq
= isl_vec_normalize(ineq
);
738 /* Return a integer division for use in a parametric cut based on the given row.
739 * In particular, let the parametric constant of the row be
743 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
744 * The div returned is equal to
746 * floor(\sum_i {-a_i} y_i) = floor((\sum_i (-a_i mod d) y_i)/d)
748 static struct isl_vec
*get_row_parameter_div(struct isl_tab
*tab
, int row
)
752 div
= isl_vec_alloc(tab
->mat
->ctx
, 1 + 1 + tab
->n_param
+ tab
->n_div
);
756 isl_int_set(div
->el
[0], tab
->mat
->row
[row
][0]);
757 get_row_parameter_line(tab
, row
, div
->el
+ 1);
758 div
= isl_vec_normalize(div
);
759 isl_seq_neg(div
->el
+ 1, div
->el
+ 1, div
->size
- 1);
760 isl_seq_fdiv_r(div
->el
+ 1, div
->el
+ 1, div
->el
[0], div
->size
- 1);
765 /* Return a integer division for use in transferring an integrality constraint
767 * In particular, let the parametric constant of the row be
771 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
772 * The the returned div is equal to
774 * floor(\sum_i {a_i} y_i) = floor((\sum_i (a_i mod d) y_i)/d)
776 static struct isl_vec
*get_row_split_div(struct isl_tab
*tab
, int row
)
780 div
= isl_vec_alloc(tab
->mat
->ctx
, 1 + 1 + tab
->n_param
+ tab
->n_div
);
784 isl_int_set(div
->el
[0], tab
->mat
->row
[row
][0]);
785 get_row_parameter_line(tab
, row
, div
->el
+ 1);
786 div
= isl_vec_normalize(div
);
787 isl_seq_fdiv_r(div
->el
+ 1, div
->el
+ 1, div
->el
[0], div
->size
- 1);
792 /* Construct and return an inequality that expresses an upper bound
794 * In particular, if the div is given by
798 * then the inequality expresses
802 static struct isl_vec
*ineq_for_div(struct isl_basic_set
*bset
, unsigned div
)
806 struct isl_vec
*ineq
;
811 total
= isl_basic_set_total_dim(bset
);
812 div_pos
= 1 + total
- bset
->n_div
+ div
;
814 ineq
= isl_vec_alloc(bset
->ctx
, 1 + total
);
818 isl_seq_cpy(ineq
->el
, bset
->div
[div
] + 1, 1 + total
);
819 isl_int_neg(ineq
->el
[div_pos
], bset
->div
[div
][0]);
823 /* Given a row in the tableau and a div that was created
824 * using get_row_split_div and that been constrained to equality, i.e.,
826 * d = floor(\sum_i {a_i} y_i) = \sum_i {a_i} y_i
828 * replace the expression "\sum_i {a_i} y_i" in the row by d,
829 * i.e., we subtract "\sum_i {a_i} y_i" and add 1 d.
830 * The coefficients of the non-parameters in the tableau have been
831 * verified to be integral. We can therefore simply replace coefficient b
832 * by floor(b). For the coefficients of the parameters we have
833 * floor(a_i) = a_i - {a_i}, while for the other coefficients, we have
836 static struct isl_tab
*set_row_cst_to_div(struct isl_tab
*tab
, int row
, int div
)
838 isl_seq_fdiv_q(tab
->mat
->row
[row
] + 1, tab
->mat
->row
[row
] + 1,
839 tab
->mat
->row
[row
][0], 1 + tab
->M
+ tab
->n_col
);
841 isl_int_set_si(tab
->mat
->row
[row
][0], 1);
843 if (tab
->var
[tab
->n_var
- tab
->n_div
+ div
].is_row
) {
844 int drow
= tab
->var
[tab
->n_var
- tab
->n_div
+ div
].index
;
846 isl_assert(tab
->mat
->ctx
,
847 isl_int_is_one(tab
->mat
->row
[drow
][0]), goto error
);
848 isl_seq_combine(tab
->mat
->row
[row
] + 1,
849 tab
->mat
->ctx
->one
, tab
->mat
->row
[row
] + 1,
850 tab
->mat
->ctx
->one
, tab
->mat
->row
[drow
] + 1,
851 1 + tab
->M
+ tab
->n_col
);
853 int dcol
= tab
->var
[tab
->n_var
- tab
->n_div
+ div
].index
;
855 isl_int_set_si(tab
->mat
->row
[row
][2 + tab
->M
+ dcol
], 1);
864 /* Check if the (parametric) constant of the given row is obviously
865 * negative, meaning that we don't need to consult the context tableau.
866 * If there is a big parameter and its coefficient is non-zero,
867 * then this coefficient determines the outcome.
868 * Otherwise, we check whether the constant is negative and
869 * all non-zero coefficients of parameters are negative and
870 * belong to non-negative parameters.
872 static int is_obviously_neg(struct isl_tab
*tab
, int row
)
876 unsigned off
= 2 + tab
->M
;
879 if (isl_int_is_pos(tab
->mat
->row
[row
][2]))
881 if (isl_int_is_neg(tab
->mat
->row
[row
][2]))
885 if (isl_int_is_nonneg(tab
->mat
->row
[row
][1]))
887 for (i
= 0; i
< tab
->n_param
; ++i
) {
888 /* Eliminated parameter */
889 if (tab
->var
[i
].is_row
)
891 col
= tab
->var
[i
].index
;
892 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
894 if (!tab
->var
[i
].is_nonneg
)
896 if (isl_int_is_pos(tab
->mat
->row
[row
][off
+ col
]))
899 for (i
= 0; i
< tab
->n_div
; ++i
) {
900 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
902 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
903 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
905 if (!tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_nonneg
)
907 if (isl_int_is_pos(tab
->mat
->row
[row
][off
+ col
]))
913 /* Check if the (parametric) constant of the given row is obviously
914 * non-negative, meaning that we don't need to consult the context tableau.
915 * If there is a big parameter and its coefficient is non-zero,
916 * then this coefficient determines the outcome.
917 * Otherwise, we check whether the constant is non-negative and
918 * all non-zero coefficients of parameters are positive and
919 * belong to non-negative parameters.
921 static int is_obviously_nonneg(struct isl_tab
*tab
, int row
)
925 unsigned off
= 2 + tab
->M
;
928 if (isl_int_is_pos(tab
->mat
->row
[row
][2]))
930 if (isl_int_is_neg(tab
->mat
->row
[row
][2]))
934 if (isl_int_is_neg(tab
->mat
->row
[row
][1]))
936 for (i
= 0; i
< tab
->n_param
; ++i
) {
937 /* Eliminated parameter */
938 if (tab
->var
[i
].is_row
)
940 col
= tab
->var
[i
].index
;
941 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
943 if (!tab
->var
[i
].is_nonneg
)
945 if (isl_int_is_neg(tab
->mat
->row
[row
][off
+ col
]))
948 for (i
= 0; i
< tab
->n_div
; ++i
) {
949 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
951 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
952 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
954 if (!tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_nonneg
)
956 if (isl_int_is_neg(tab
->mat
->row
[row
][off
+ col
]))
962 /* Given a row r and two columns, return the column that would
963 * lead to the lexicographically smallest increment in the sample
964 * solution when leaving the basis in favor of the row.
965 * Pivoting with column c will increment the sample value by a non-negative
966 * constant times a_{V,c}/a_{r,c}, with a_{V,c} the elements of column c
967 * corresponding to the non-parametric variables.
968 * If variable v appears in a column c_v, the a_{v,c} = 1 iff c = c_v,
969 * with all other entries in this virtual row equal to zero.
970 * If variable v appears in a row, then a_{v,c} is the element in column c
973 * Let v be the first variable with a_{v,c1}/a_{r,c1} != a_{v,c2}/a_{r,c2}.
974 * Then if a_{v,c1}/a_{r,c1} < a_{v,c2}/a_{r,c2}, i.e.,
975 * a_{v,c2} a_{r,c1} - a_{v,c1} a_{r,c2} > 0, c1 results in the minimal
976 * increment. Otherwise, it's c2.
978 static int lexmin_col_pair(struct isl_tab
*tab
,
979 int row
, int col1
, int col2
, isl_int tmp
)
984 tr
= tab
->mat
->row
[row
] + 2 + tab
->M
;
986 for (i
= tab
->n_param
; i
< tab
->n_var
- tab
->n_div
; ++i
) {
990 if (!tab
->var
[i
].is_row
) {
991 if (tab
->var
[i
].index
== col1
)
993 if (tab
->var
[i
].index
== col2
)
998 if (tab
->var
[i
].index
== row
)
1001 r
= tab
->mat
->row
[tab
->var
[i
].index
] + 2 + tab
->M
;
1002 s1
= isl_int_sgn(r
[col1
]);
1003 s2
= isl_int_sgn(r
[col2
]);
1004 if (s1
== 0 && s2
== 0)
1011 isl_int_mul(tmp
, r
[col2
], tr
[col1
]);
1012 isl_int_submul(tmp
, r
[col1
], tr
[col2
]);
1013 if (isl_int_is_pos(tmp
))
1015 if (isl_int_is_neg(tmp
))
1021 /* Given a row in the tableau, find and return the column that would
1022 * result in the lexicographically smallest, but positive, increment
1023 * in the sample point.
1024 * If there is no such column, then return tab->n_col.
1025 * If anything goes wrong, return -1.
1027 static int lexmin_pivot_col(struct isl_tab
*tab
, int row
)
1030 int col
= tab
->n_col
;
1034 tr
= tab
->mat
->row
[row
] + 2 + tab
->M
;
1038 for (j
= tab
->n_dead
; j
< tab
->n_col
; ++j
) {
1039 if (tab
->col_var
[j
] >= 0 &&
1040 (tab
->col_var
[j
] < tab
->n_param
||
1041 tab
->col_var
[j
] >= tab
->n_var
- tab
->n_div
))
1044 if (!isl_int_is_pos(tr
[j
]))
1047 if (col
== tab
->n_col
)
1050 col
= lexmin_col_pair(tab
, row
, col
, j
, tmp
);
1051 isl_assert(tab
->mat
->ctx
, col
>= 0, goto error
);
1061 /* Return the first known violated constraint, i.e., a non-negative
1062 * contraint that currently has an either obviously negative value
1063 * or a previously determined to be negative value.
1065 * If any constraint has a negative coefficient for the big parameter,
1066 * if any, then we return one of these first.
1068 static int first_neg(struct isl_tab
*tab
)
1073 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
1074 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
1076 if (isl_int_is_neg(tab
->mat
->row
[row
][2]))
1079 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
1080 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
1082 if (tab
->row_sign
) {
1083 if (tab
->row_sign
[row
] == 0 &&
1084 is_obviously_neg(tab
, row
))
1085 tab
->row_sign
[row
] = isl_tab_row_neg
;
1086 if (tab
->row_sign
[row
] != isl_tab_row_neg
)
1088 } else if (!is_obviously_neg(tab
, row
))
1095 /* Resolve all known or obviously violated constraints through pivoting.
1096 * In particular, as long as we can find any violated constraint, we
1097 * look for a pivoting column that would result in the lexicographicallly
1098 * smallest increment in the sample point. If there is no such column
1099 * then the tableau is infeasible.
1101 static struct isl_tab
*restore_lexmin(struct isl_tab
*tab
) WARN_UNUSED
;
1102 static struct isl_tab
*restore_lexmin(struct isl_tab
*tab
)
1110 while ((row
= first_neg(tab
)) != -1) {
1111 col
= lexmin_pivot_col(tab
, row
);
1112 if (col
>= tab
->n_col
) {
1113 if (isl_tab_mark_empty(tab
) < 0)
1119 if (isl_tab_pivot(tab
, row
, col
) < 0)
1128 /* Given a row that represents an equality, look for an appropriate
1130 * In particular, if there are any non-zero coefficients among
1131 * the non-parameter variables, then we take the last of these
1132 * variables. Eliminating this variable in terms of the other
1133 * variables and/or parameters does not influence the property
1134 * that all column in the initial tableau are lexicographically
1135 * positive. The row corresponding to the eliminated variable
1136 * will only have non-zero entries below the diagonal of the
1137 * initial tableau. That is, we transform
1143 * If there is no such non-parameter variable, then we are dealing with
1144 * pure parameter equality and we pick any parameter with coefficient 1 or -1
1145 * for elimination. This will ensure that the eliminated parameter
1146 * always has an integer value whenever all the other parameters are integral.
1147 * If there is no such parameter then we return -1.
1149 static int last_var_col_or_int_par_col(struct isl_tab
*tab
, int row
)
1151 unsigned off
= 2 + tab
->M
;
1154 for (i
= tab
->n_var
- tab
->n_div
- 1; i
>= 0 && i
>= tab
->n_param
; --i
) {
1156 if (tab
->var
[i
].is_row
)
1158 col
= tab
->var
[i
].index
;
1159 if (col
<= tab
->n_dead
)
1161 if (!isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
1164 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
1165 if (isl_int_is_one(tab
->mat
->row
[row
][off
+ i
]))
1167 if (isl_int_is_negone(tab
->mat
->row
[row
][off
+ i
]))
1173 /* Add an equality that is known to be valid to the tableau.
1174 * We first check if we can eliminate a variable or a parameter.
1175 * If not, we add the equality as two inequalities.
1176 * In this case, the equality was a pure parameter equality and there
1177 * is no need to resolve any constraint violations.
1179 static struct isl_tab
*add_lexmin_valid_eq(struct isl_tab
*tab
, isl_int
*eq
)
1186 r
= isl_tab_add_row(tab
, eq
);
1190 r
= tab
->con
[r
].index
;
1191 i
= last_var_col_or_int_par_col(tab
, r
);
1193 tab
->con
[r
].is_nonneg
= 1;
1194 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
1196 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
1197 r
= isl_tab_add_row(tab
, eq
);
1200 tab
->con
[r
].is_nonneg
= 1;
1201 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
1204 if (isl_tab_pivot(tab
, r
, i
) < 0)
1206 if (isl_tab_kill_col(tab
, i
) < 0)
1210 tab
= restore_lexmin(tab
);
1219 /* Check if the given row is a pure constant.
1221 static int is_constant(struct isl_tab
*tab
, int row
)
1223 unsigned off
= 2 + tab
->M
;
1225 return isl_seq_first_non_zero(tab
->mat
->row
[row
] + off
+ tab
->n_dead
,
1226 tab
->n_col
- tab
->n_dead
) == -1;
1229 /* Add an equality that may or may not be valid to the tableau.
1230 * If the resulting row is a pure constant, then it must be zero.
1231 * Otherwise, the resulting tableau is empty.
1233 * If the row is not a pure constant, then we add two inequalities,
1234 * each time checking that they can be satisfied.
1235 * In the end we try to use one of the two constraints to eliminate
1238 static struct isl_tab
*add_lexmin_eq(struct isl_tab
*tab
, isl_int
*eq
) WARN_UNUSED
;
1239 static struct isl_tab
*add_lexmin_eq(struct isl_tab
*tab
, isl_int
*eq
)
1243 struct isl_tab_undo
*snap
;
1247 snap
= isl_tab_snap(tab
);
1248 r1
= isl_tab_add_row(tab
, eq
);
1251 tab
->con
[r1
].is_nonneg
= 1;
1252 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r1
]) < 0)
1255 row
= tab
->con
[r1
].index
;
1256 if (is_constant(tab
, row
)) {
1257 if (!isl_int_is_zero(tab
->mat
->row
[row
][1]) ||
1258 (tab
->M
&& !isl_int_is_zero(tab
->mat
->row
[row
][2]))) {
1259 if (isl_tab_mark_empty(tab
) < 0)
1263 if (isl_tab_rollback(tab
, snap
) < 0)
1268 tab
= restore_lexmin(tab
);
1269 if (!tab
|| tab
->empty
)
1272 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
1274 r2
= isl_tab_add_row(tab
, eq
);
1277 tab
->con
[r2
].is_nonneg
= 1;
1278 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r2
]) < 0)
1281 tab
= restore_lexmin(tab
);
1282 if (!tab
|| tab
->empty
)
1285 if (!tab
->con
[r1
].is_row
) {
1286 if (isl_tab_kill_col(tab
, tab
->con
[r1
].index
) < 0)
1288 } else if (!tab
->con
[r2
].is_row
) {
1289 if (isl_tab_kill_col(tab
, tab
->con
[r2
].index
) < 0)
1291 } else if (isl_int_is_zero(tab
->mat
->row
[tab
->con
[r1
].index
][1])) {
1292 unsigned off
= 2 + tab
->M
;
1294 int row
= tab
->con
[r1
].index
;
1295 i
= isl_seq_first_non_zero(tab
->mat
->row
[row
]+off
+tab
->n_dead
,
1296 tab
->n_col
- tab
->n_dead
);
1298 if (isl_tab_pivot(tab
, row
, tab
->n_dead
+ i
) < 0)
1300 if (isl_tab_kill_col(tab
, tab
->n_dead
+ i
) < 0)
1306 tab
->bmap
= isl_basic_map_add_ineq(tab
->bmap
, eq
);
1307 if (isl_tab_push(tab
, isl_tab_undo_bmap_ineq
) < 0)
1309 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
1310 tab
->bmap
= isl_basic_map_add_ineq(tab
->bmap
, eq
);
1311 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
1312 if (isl_tab_push(tab
, isl_tab_undo_bmap_ineq
) < 0)
1324 /* Add an inequality to the tableau, resolving violations using
1327 static struct isl_tab
*add_lexmin_ineq(struct isl_tab
*tab
, isl_int
*ineq
)
1334 tab
->bmap
= isl_basic_map_add_ineq(tab
->bmap
, ineq
);
1335 if (isl_tab_push(tab
, isl_tab_undo_bmap_ineq
) < 0)
1340 r
= isl_tab_add_row(tab
, ineq
);
1343 tab
->con
[r
].is_nonneg
= 1;
1344 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
1346 if (isl_tab_row_is_redundant(tab
, tab
->con
[r
].index
)) {
1347 if (isl_tab_mark_redundant(tab
, tab
->con
[r
].index
) < 0)
1352 tab
= restore_lexmin(tab
);
1353 if (tab
&& !tab
->empty
&& tab
->con
[r
].is_row
&&
1354 isl_tab_row_is_redundant(tab
, tab
->con
[r
].index
))
1355 if (isl_tab_mark_redundant(tab
, tab
->con
[r
].index
) < 0)
1363 /* Check if the coefficients of the parameters are all integral.
1365 static int integer_parameter(struct isl_tab
*tab
, int row
)
1369 unsigned off
= 2 + tab
->M
;
1371 for (i
= 0; i
< tab
->n_param
; ++i
) {
1372 /* Eliminated parameter */
1373 if (tab
->var
[i
].is_row
)
1375 col
= tab
->var
[i
].index
;
1376 if (!isl_int_is_divisible_by(tab
->mat
->row
[row
][off
+ col
],
1377 tab
->mat
->row
[row
][0]))
1380 for (i
= 0; i
< tab
->n_div
; ++i
) {
1381 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
1383 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
1384 if (!isl_int_is_divisible_by(tab
->mat
->row
[row
][off
+ col
],
1385 tab
->mat
->row
[row
][0]))
1391 /* Check if the coefficients of the non-parameter variables are all integral.
1393 static int integer_variable(struct isl_tab
*tab
, int row
)
1396 unsigned off
= 2 + tab
->M
;
1398 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
1399 if (tab
->col_var
[i
] >= 0 &&
1400 (tab
->col_var
[i
] < tab
->n_param
||
1401 tab
->col_var
[i
] >= tab
->n_var
- tab
->n_div
))
1403 if (!isl_int_is_divisible_by(tab
->mat
->row
[row
][off
+ i
],
1404 tab
->mat
->row
[row
][0]))
1410 /* Check if the constant term is integral.
1412 static int integer_constant(struct isl_tab
*tab
, int row
)
1414 return isl_int_is_divisible_by(tab
->mat
->row
[row
][1],
1415 tab
->mat
->row
[row
][0]);
1418 #define I_CST 1 << 0
1419 #define I_PAR 1 << 1
1420 #define I_VAR 1 << 2
1422 /* Check for next (non-parameter) variable after "var" (first if var == -1)
1423 * that is non-integer and therefore requires a cut and return
1424 * the index of the variable.
1425 * For parametric tableaus, there are three parts in a row,
1426 * the constant, the coefficients of the parameters and the rest.
1427 * For each part, we check whether the coefficients in that part
1428 * are all integral and if so, set the corresponding flag in *f.
1429 * If the constant and the parameter part are integral, then the
1430 * current sample value is integral and no cut is required
1431 * (irrespective of whether the variable part is integral).
1433 static int next_non_integer_var(struct isl_tab
*tab
, int var
, int *f
)
1435 var
= var
< 0 ? tab
->n_param
: var
+ 1;
1437 for (; var
< tab
->n_var
- tab
->n_div
; ++var
) {
1440 if (!tab
->var
[var
].is_row
)
1442 row
= tab
->var
[var
].index
;
1443 if (integer_constant(tab
, row
))
1444 ISL_FL_SET(flags
, I_CST
);
1445 if (integer_parameter(tab
, row
))
1446 ISL_FL_SET(flags
, I_PAR
);
1447 if (ISL_FL_ISSET(flags
, I_CST
) && ISL_FL_ISSET(flags
, I_PAR
))
1449 if (integer_variable(tab
, row
))
1450 ISL_FL_SET(flags
, I_VAR
);
1457 /* Check for first (non-parameter) variable that is non-integer and
1458 * therefore requires a cut and return the corresponding row.
1459 * For parametric tableaus, there are three parts in a row,
1460 * the constant, the coefficients of the parameters and the rest.
1461 * For each part, we check whether the coefficients in that part
1462 * are all integral and if so, set the corresponding flag in *f.
1463 * If the constant and the parameter part are integral, then the
1464 * current sample value is integral and no cut is required
1465 * (irrespective of whether the variable part is integral).
1467 static int first_non_integer_row(struct isl_tab
*tab
, int *f
)
1469 int var
= next_non_integer_var(tab
, -1, f
);
1471 return var
< 0 ? -1 : tab
->var
[var
].index
;
1474 /* Add a (non-parametric) cut to cut away the non-integral sample
1475 * value of the given row.
1477 * If the row is given by
1479 * m r = f + \sum_i a_i y_i
1483 * c = - {-f/m} + \sum_i {a_i/m} y_i >= 0
1485 * The big parameter, if any, is ignored, since it is assumed to be big
1486 * enough to be divisible by any integer.
1487 * If the tableau is actually a parametric tableau, then this function
1488 * is only called when all coefficients of the parameters are integral.
1489 * The cut therefore has zero coefficients for the parameters.
1491 * The current value is known to be negative, so row_sign, if it
1492 * exists, is set accordingly.
1494 * Return the row of the cut or -1.
1496 static int add_cut(struct isl_tab
*tab
, int row
)
1501 unsigned off
= 2 + tab
->M
;
1503 if (isl_tab_extend_cons(tab
, 1) < 0)
1505 r
= isl_tab_allocate_con(tab
);
1509 r_row
= tab
->mat
->row
[tab
->con
[r
].index
];
1510 isl_int_set(r_row
[0], tab
->mat
->row
[row
][0]);
1511 isl_int_neg(r_row
[1], tab
->mat
->row
[row
][1]);
1512 isl_int_fdiv_r(r_row
[1], r_row
[1], tab
->mat
->row
[row
][0]);
1513 isl_int_neg(r_row
[1], r_row
[1]);
1515 isl_int_set_si(r_row
[2], 0);
1516 for (i
= 0; i
< tab
->n_col
; ++i
)
1517 isl_int_fdiv_r(r_row
[off
+ i
],
1518 tab
->mat
->row
[row
][off
+ i
], tab
->mat
->row
[row
][0]);
1520 tab
->con
[r
].is_nonneg
= 1;
1521 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
1524 tab
->row_sign
[tab
->con
[r
].index
] = isl_tab_row_neg
;
1526 return tab
->con
[r
].index
;
1529 /* Given a non-parametric tableau, add cuts until an integer
1530 * sample point is obtained or until the tableau is determined
1531 * to be integer infeasible.
1532 * As long as there is any non-integer value in the sample point,
1533 * we add appropriate cuts, if possible, for each of these
1534 * non-integer values and then resolve the violated
1535 * cut constraints using restore_lexmin.
1536 * If one of the corresponding rows is equal to an integral
1537 * combination of variables/constraints plus a non-integral constant,
1538 * then there is no way to obtain an integer point and we return
1539 * a tableau that is marked empty.
1541 static struct isl_tab
*cut_to_integer_lexmin(struct isl_tab
*tab
)
1552 while ((var
= next_non_integer_var(tab
, -1, &flags
)) != -1) {
1554 if (ISL_FL_ISSET(flags
, I_VAR
)) {
1555 if (isl_tab_mark_empty(tab
) < 0)
1559 row
= tab
->var
[var
].index
;
1560 row
= add_cut(tab
, row
);
1563 } while ((var
= next_non_integer_var(tab
, var
, &flags
)) != -1);
1564 tab
= restore_lexmin(tab
);
1565 if (!tab
|| tab
->empty
)
1574 /* Check whether all the currently active samples also satisfy the inequality
1575 * "ineq" (treated as an equality if eq is set).
1576 * Remove those samples that do not.
1578 static struct isl_tab
*check_samples(struct isl_tab
*tab
, isl_int
*ineq
, int eq
)
1586 isl_assert(tab
->mat
->ctx
, tab
->bmap
, goto error
);
1587 isl_assert(tab
->mat
->ctx
, tab
->samples
, goto error
);
1588 isl_assert(tab
->mat
->ctx
, tab
->samples
->n_col
== 1 + tab
->n_var
, goto error
);
1591 for (i
= tab
->n_outside
; i
< tab
->n_sample
; ++i
) {
1593 isl_seq_inner_product(ineq
, tab
->samples
->row
[i
],
1594 1 + tab
->n_var
, &v
);
1595 sgn
= isl_int_sgn(v
);
1596 if (eq
? (sgn
== 0) : (sgn
>= 0))
1598 tab
= isl_tab_drop_sample(tab
, i
);
1610 /* Check whether the sample value of the tableau is finite,
1611 * i.e., either the tableau does not use a big parameter, or
1612 * all values of the variables are equal to the big parameter plus
1613 * some constant. This constant is the actual sample value.
1615 static int sample_is_finite(struct isl_tab
*tab
)
1622 for (i
= 0; i
< tab
->n_var
; ++i
) {
1624 if (!tab
->var
[i
].is_row
)
1626 row
= tab
->var
[i
].index
;
1627 if (isl_int_ne(tab
->mat
->row
[row
][0], tab
->mat
->row
[row
][2]))
1633 /* Check if the context tableau of sol has any integer points.
1634 * Leave tab in empty state if no integer point can be found.
1635 * If an integer point can be found and if moreover it is finite,
1636 * then it is added to the list of sample values.
1638 * This function is only called when none of the currently active sample
1639 * values satisfies the most recently added constraint.
1641 static struct isl_tab
*check_integer_feasible(struct isl_tab
*tab
)
1643 struct isl_tab_undo
*snap
;
1649 snap
= isl_tab_snap(tab
);
1650 if (isl_tab_push_basis(tab
) < 0)
1653 tab
= cut_to_integer_lexmin(tab
);
1657 if (!tab
->empty
&& sample_is_finite(tab
)) {
1658 struct isl_vec
*sample
;
1660 sample
= isl_tab_get_sample_value(tab
);
1662 tab
= isl_tab_add_sample(tab
, sample
);
1665 if (!tab
->empty
&& isl_tab_rollback(tab
, snap
) < 0)
1674 /* Check if any of the currently active sample values satisfies
1675 * the inequality "ineq" (an equality if eq is set).
1677 static int tab_has_valid_sample(struct isl_tab
*tab
, isl_int
*ineq
, int eq
)
1685 isl_assert(tab
->mat
->ctx
, tab
->bmap
, return -1);
1686 isl_assert(tab
->mat
->ctx
, tab
->samples
, return -1);
1687 isl_assert(tab
->mat
->ctx
, tab
->samples
->n_col
== 1 + tab
->n_var
, return -1);
1690 for (i
= tab
->n_outside
; i
< tab
->n_sample
; ++i
) {
1692 isl_seq_inner_product(ineq
, tab
->samples
->row
[i
],
1693 1 + tab
->n_var
, &v
);
1694 sgn
= isl_int_sgn(v
);
1695 if (eq
? (sgn
== 0) : (sgn
>= 0))
1700 return i
< tab
->n_sample
;
1703 /* For a div d = floor(f/m), add the constraints
1706 * -(f-(m-1)) + m d >= 0
1708 * Note that the second constraint is the negation of
1712 static void add_div_constraints(struct isl_context
*context
, unsigned div
)
1716 struct isl_vec
*ineq
;
1717 struct isl_basic_set
*bset
;
1719 bset
= context
->op
->peek_basic_set(context
);
1723 total
= isl_basic_set_total_dim(bset
);
1724 div_pos
= 1 + total
- bset
->n_div
+ div
;
1726 ineq
= ineq_for_div(bset
, div
);
1730 context
->op
->add_ineq(context
, ineq
->el
, 0, 0);
1732 isl_seq_neg(ineq
->el
, bset
->div
[div
] + 1, 1 + total
);
1733 isl_int_set(ineq
->el
[div_pos
], bset
->div
[div
][0]);
1734 isl_int_add(ineq
->el
[0], ineq
->el
[0], ineq
->el
[div_pos
]);
1735 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
1737 context
->op
->add_ineq(context
, ineq
->el
, 0, 0);
1743 context
->op
->invalidate(context
);
1746 /* Add a div specifed by "div" to the tableau "tab" and return
1747 * the index of the new div. *nonneg is set to 1 if the div
1748 * is obviously non-negative.
1750 static int context_tab_add_div(struct isl_tab
*tab
, struct isl_vec
*div
,
1756 struct isl_mat
*samples
;
1758 for (i
= 0; i
< tab
->n_var
; ++i
) {
1759 if (isl_int_is_zero(div
->el
[2 + i
]))
1761 if (!tab
->var
[i
].is_nonneg
)
1764 *nonneg
= i
== tab
->n_var
;
1766 if (isl_tab_extend_cons(tab
, 3) < 0)
1768 if (isl_tab_extend_vars(tab
, 1) < 0)
1770 r
= isl_tab_allocate_var(tab
);
1774 tab
->var
[r
].is_nonneg
= 1;
1775 tab
->var
[r
].frozen
= 1;
1777 samples
= isl_mat_extend(tab
->samples
,
1778 tab
->n_sample
, 1 + tab
->n_var
);
1779 tab
->samples
= samples
;
1782 for (i
= tab
->n_outside
; i
< samples
->n_row
; ++i
) {
1783 isl_seq_inner_product(div
->el
+ 1, samples
->row
[i
],
1784 div
->size
- 1, &samples
->row
[i
][samples
->n_col
- 1]);
1785 isl_int_fdiv_q(samples
->row
[i
][samples
->n_col
- 1],
1786 samples
->row
[i
][samples
->n_col
- 1], div
->el
[0]);
1789 tab
->bmap
= isl_basic_map_extend_dim(tab
->bmap
,
1790 isl_basic_map_get_dim(tab
->bmap
), 1, 0, 2);
1791 k
= isl_basic_map_alloc_div(tab
->bmap
);
1794 isl_seq_cpy(tab
->bmap
->div
[k
], div
->el
, div
->size
);
1795 if (isl_tab_push(tab
, isl_tab_undo_bmap_div
) < 0)
1801 /* Add a div specified by "div" to both the main tableau and
1802 * the context tableau. In case of the main tableau, we only
1803 * need to add an extra div. In the context tableau, we also
1804 * need to express the meaning of the div.
1805 * Return the index of the div or -1 if anything went wrong.
1807 static int add_div(struct isl_tab
*tab
, struct isl_context
*context
,
1808 struct isl_vec
*div
)
1814 k
= context
->op
->add_div(context
, div
, &nonneg
);
1818 add_div_constraints(context
, k
);
1819 if (!context
->op
->is_ok(context
))
1822 if (isl_tab_extend_vars(tab
, 1) < 0)
1824 r
= isl_tab_allocate_var(tab
);
1828 tab
->var
[r
].is_nonneg
= 1;
1829 tab
->var
[r
].frozen
= 1;
1832 return tab
->n_div
- 1;
1834 context
->op
->invalidate(context
);
1838 static int find_div(struct isl_tab
*tab
, isl_int
*div
, isl_int denom
)
1841 unsigned total
= isl_basic_map_total_dim(tab
->bmap
);
1843 for (i
= 0; i
< tab
->bmap
->n_div
; ++i
) {
1844 if (isl_int_ne(tab
->bmap
->div
[i
][0], denom
))
1846 if (!isl_seq_eq(tab
->bmap
->div
[i
] + 1, div
, total
))
1853 /* Return the index of a div that corresponds to "div".
1854 * We first check if we already have such a div and if not, we create one.
1856 static int get_div(struct isl_tab
*tab
, struct isl_context
*context
,
1857 struct isl_vec
*div
)
1860 struct isl_tab
*context_tab
= context
->op
->peek_tab(context
);
1865 d
= find_div(context_tab
, div
->el
+ 1, div
->el
[0]);
1869 return add_div(tab
, context
, div
);
1872 /* Add a parametric cut to cut away the non-integral sample value
1874 * Let a_i be the coefficients of the constant term and the parameters
1875 * and let b_i be the coefficients of the variables or constraints
1876 * in basis of the tableau.
1877 * Let q be the div q = floor(\sum_i {-a_i} y_i).
1879 * The cut is expressed as
1881 * c = \sum_i -{-a_i} y_i + \sum_i {b_i} x_i + q >= 0
1883 * If q did not already exist in the context tableau, then it is added first.
1884 * If q is in a column of the main tableau then the "+ q" can be accomplished
1885 * by setting the corresponding entry to the denominator of the constraint.
1886 * If q happens to be in a row of the main tableau, then the corresponding
1887 * row needs to be added instead (taking care of the denominators).
1888 * Note that this is very unlikely, but perhaps not entirely impossible.
1890 * The current value of the cut is known to be negative (or at least
1891 * non-positive), so row_sign is set accordingly.
1893 * Return the row of the cut or -1.
1895 static int add_parametric_cut(struct isl_tab
*tab
, int row
,
1896 struct isl_context
*context
)
1898 struct isl_vec
*div
;
1905 unsigned off
= 2 + tab
->M
;
1910 div
= get_row_parameter_div(tab
, row
);
1915 d
= context
->op
->get_div(context
, tab
, div
);
1919 if (isl_tab_extend_cons(tab
, 1) < 0)
1921 r
= isl_tab_allocate_con(tab
);
1925 r_row
= tab
->mat
->row
[tab
->con
[r
].index
];
1926 isl_int_set(r_row
[0], tab
->mat
->row
[row
][0]);
1927 isl_int_neg(r_row
[1], tab
->mat
->row
[row
][1]);
1928 isl_int_fdiv_r(r_row
[1], r_row
[1], tab
->mat
->row
[row
][0]);
1929 isl_int_neg(r_row
[1], r_row
[1]);
1931 isl_int_set_si(r_row
[2], 0);
1932 for (i
= 0; i
< tab
->n_param
; ++i
) {
1933 if (tab
->var
[i
].is_row
)
1935 col
= tab
->var
[i
].index
;
1936 isl_int_neg(r_row
[off
+ col
], tab
->mat
->row
[row
][off
+ col
]);
1937 isl_int_fdiv_r(r_row
[off
+ col
], r_row
[off
+ col
],
1938 tab
->mat
->row
[row
][0]);
1939 isl_int_neg(r_row
[off
+ col
], r_row
[off
+ col
]);
1941 for (i
= 0; i
< tab
->n_div
; ++i
) {
1942 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
1944 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
1945 isl_int_neg(r_row
[off
+ col
], tab
->mat
->row
[row
][off
+ col
]);
1946 isl_int_fdiv_r(r_row
[off
+ col
], r_row
[off
+ col
],
1947 tab
->mat
->row
[row
][0]);
1948 isl_int_neg(r_row
[off
+ col
], r_row
[off
+ col
]);
1950 for (i
= 0; i
< tab
->n_col
; ++i
) {
1951 if (tab
->col_var
[i
] >= 0 &&
1952 (tab
->col_var
[i
] < tab
->n_param
||
1953 tab
->col_var
[i
] >= tab
->n_var
- tab
->n_div
))
1955 isl_int_fdiv_r(r_row
[off
+ i
],
1956 tab
->mat
->row
[row
][off
+ i
], tab
->mat
->row
[row
][0]);
1958 if (tab
->var
[tab
->n_var
- tab
->n_div
+ d
].is_row
) {
1960 int d_row
= tab
->var
[tab
->n_var
- tab
->n_div
+ d
].index
;
1962 isl_int_gcd(gcd
, tab
->mat
->row
[d_row
][0], r_row
[0]);
1963 isl_int_divexact(r_row
[0], r_row
[0], gcd
);
1964 isl_int_divexact(gcd
, tab
->mat
->row
[d_row
][0], gcd
);
1965 isl_seq_combine(r_row
+ 1, gcd
, r_row
+ 1,
1966 r_row
[0], tab
->mat
->row
[d_row
] + 1,
1967 off
- 1 + tab
->n_col
);
1968 isl_int_mul(r_row
[0], r_row
[0], tab
->mat
->row
[d_row
][0]);
1971 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ d
].index
;
1972 isl_int_set(r_row
[off
+ col
], tab
->mat
->row
[row
][0]);
1975 tab
->con
[r
].is_nonneg
= 1;
1976 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
1979 tab
->row_sign
[tab
->con
[r
].index
] = isl_tab_row_neg
;
1983 row
= tab
->con
[r
].index
;
1985 if (d
>= n
&& context
->op
->detect_equalities(context
, tab
) < 0)
1991 /* Construct a tableau for bmap that can be used for computing
1992 * the lexicographic minimum (or maximum) of bmap.
1993 * If not NULL, then dom is the domain where the minimum
1994 * should be computed. In this case, we set up a parametric
1995 * tableau with row signs (initialized to "unknown").
1996 * If M is set, then the tableau will use a big parameter.
1997 * If max is set, then a maximum should be computed instead of a minimum.
1998 * This means that for each variable x, the tableau will contain the variable
1999 * x' = M - x, rather than x' = M + x. This in turn means that the coefficient
2000 * of the variables in all constraints are negated prior to adding them
2003 static struct isl_tab
*tab_for_lexmin(struct isl_basic_map
*bmap
,
2004 struct isl_basic_set
*dom
, unsigned M
, int max
)
2007 struct isl_tab
*tab
;
2009 tab
= isl_tab_alloc(bmap
->ctx
, 2 * bmap
->n_eq
+ bmap
->n_ineq
+ 1,
2010 isl_basic_map_total_dim(bmap
), M
);
2014 tab
->rational
= ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
2016 tab
->n_param
= isl_basic_set_total_dim(dom
) - dom
->n_div
;
2017 tab
->n_div
= dom
->n_div
;
2018 tab
->row_sign
= isl_calloc_array(bmap
->ctx
,
2019 enum isl_tab_row_sign
, tab
->mat
->n_row
);
2023 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
)) {
2024 if (isl_tab_mark_empty(tab
) < 0)
2029 for (i
= tab
->n_param
; i
< tab
->n_var
- tab
->n_div
; ++i
) {
2030 tab
->var
[i
].is_nonneg
= 1;
2031 tab
->var
[i
].frozen
= 1;
2033 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
2035 isl_seq_neg(bmap
->eq
[i
] + 1 + tab
->n_param
,
2036 bmap
->eq
[i
] + 1 + tab
->n_param
,
2037 tab
->n_var
- tab
->n_param
- tab
->n_div
);
2038 tab
= add_lexmin_valid_eq(tab
, bmap
->eq
[i
]);
2040 isl_seq_neg(bmap
->eq
[i
] + 1 + tab
->n_param
,
2041 bmap
->eq
[i
] + 1 + tab
->n_param
,
2042 tab
->n_var
- tab
->n_param
- tab
->n_div
);
2043 if (!tab
|| tab
->empty
)
2046 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
2048 isl_seq_neg(bmap
->ineq
[i
] + 1 + tab
->n_param
,
2049 bmap
->ineq
[i
] + 1 + tab
->n_param
,
2050 tab
->n_var
- tab
->n_param
- tab
->n_div
);
2051 tab
= add_lexmin_ineq(tab
, bmap
->ineq
[i
]);
2053 isl_seq_neg(bmap
->ineq
[i
] + 1 + tab
->n_param
,
2054 bmap
->ineq
[i
] + 1 + tab
->n_param
,
2055 tab
->n_var
- tab
->n_param
- tab
->n_div
);
2056 if (!tab
|| tab
->empty
)
2065 /* Given a main tableau where more than one row requires a split,
2066 * determine and return the "best" row to split on.
2068 * Given two rows in the main tableau, if the inequality corresponding
2069 * to the first row is redundant with respect to that of the second row
2070 * in the current tableau, then it is better to split on the second row,
2071 * since in the positive part, both row will be positive.
2072 * (In the negative part a pivot will have to be performed and just about
2073 * anything can happen to the sign of the other row.)
2075 * As a simple heuristic, we therefore select the row that makes the most
2076 * of the other rows redundant.
2078 * Perhaps it would also be useful to look at the number of constraints
2079 * that conflict with any given constraint.
2081 static int best_split(struct isl_tab
*tab
, struct isl_tab
*context_tab
)
2083 struct isl_tab_undo
*snap
;
2089 if (isl_tab_extend_cons(context_tab
, 2) < 0)
2092 snap
= isl_tab_snap(context_tab
);
2094 for (split
= tab
->n_redundant
; split
< tab
->n_row
; ++split
) {
2095 struct isl_tab_undo
*snap2
;
2096 struct isl_vec
*ineq
= NULL
;
2100 if (!isl_tab_var_from_row(tab
, split
)->is_nonneg
)
2102 if (tab
->row_sign
[split
] != isl_tab_row_any
)
2105 ineq
= get_row_parameter_ineq(tab
, split
);
2108 ok
= isl_tab_add_ineq(context_tab
, ineq
->el
) >= 0;
2113 snap2
= isl_tab_snap(context_tab
);
2115 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
2116 struct isl_tab_var
*var
;
2120 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
2122 if (tab
->row_sign
[row
] != isl_tab_row_any
)
2125 ineq
= get_row_parameter_ineq(tab
, row
);
2128 ok
= isl_tab_add_ineq(context_tab
, ineq
->el
) >= 0;
2132 var
= &context_tab
->con
[context_tab
->n_con
- 1];
2133 if (!context_tab
->empty
&&
2134 !isl_tab_min_at_most_neg_one(context_tab
, var
))
2136 if (isl_tab_rollback(context_tab
, snap2
) < 0)
2139 if (best
== -1 || r
> best_r
) {
2143 if (isl_tab_rollback(context_tab
, snap
) < 0)
2150 static struct isl_basic_set
*context_lex_peek_basic_set(
2151 struct isl_context
*context
)
2153 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2156 return isl_tab_peek_bset(clex
->tab
);
2159 static struct isl_tab
*context_lex_peek_tab(struct isl_context
*context
)
2161 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2165 static void context_lex_extend(struct isl_context
*context
, int n
)
2167 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2170 if (isl_tab_extend_cons(clex
->tab
, n
) >= 0)
2172 isl_tab_free(clex
->tab
);
2176 static void context_lex_add_eq(struct isl_context
*context
, isl_int
*eq
,
2177 int check
, int update
)
2179 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2180 if (isl_tab_extend_cons(clex
->tab
, 2) < 0)
2182 clex
->tab
= add_lexmin_eq(clex
->tab
, eq
);
2184 int v
= tab_has_valid_sample(clex
->tab
, eq
, 1);
2188 clex
->tab
= check_integer_feasible(clex
->tab
);
2191 clex
->tab
= check_samples(clex
->tab
, eq
, 1);
2194 isl_tab_free(clex
->tab
);
2198 static void context_lex_add_ineq(struct isl_context
*context
, isl_int
*ineq
,
2199 int check
, int update
)
2201 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2202 if (isl_tab_extend_cons(clex
->tab
, 1) < 0)
2204 clex
->tab
= add_lexmin_ineq(clex
->tab
, ineq
);
2206 int v
= tab_has_valid_sample(clex
->tab
, ineq
, 0);
2210 clex
->tab
= check_integer_feasible(clex
->tab
);
2213 clex
->tab
= check_samples(clex
->tab
, ineq
, 0);
2216 isl_tab_free(clex
->tab
);
2220 /* Check which signs can be obtained by "ineq" on all the currently
2221 * active sample values. See row_sign for more information.
2223 static enum isl_tab_row_sign
tab_ineq_sign(struct isl_tab
*tab
, isl_int
*ineq
,
2229 int res
= isl_tab_row_unknown
;
2231 isl_assert(tab
->mat
->ctx
, tab
->samples
, return 0);
2232 isl_assert(tab
->mat
->ctx
, tab
->samples
->n_col
== 1 + tab
->n_var
, return 0);
2235 for (i
= tab
->n_outside
; i
< tab
->n_sample
; ++i
) {
2236 isl_seq_inner_product(tab
->samples
->row
[i
], ineq
,
2237 1 + tab
->n_var
, &tmp
);
2238 sgn
= isl_int_sgn(tmp
);
2239 if (sgn
> 0 || (sgn
== 0 && strict
)) {
2240 if (res
== isl_tab_row_unknown
)
2241 res
= isl_tab_row_pos
;
2242 if (res
== isl_tab_row_neg
)
2243 res
= isl_tab_row_any
;
2246 if (res
== isl_tab_row_unknown
)
2247 res
= isl_tab_row_neg
;
2248 if (res
== isl_tab_row_pos
)
2249 res
= isl_tab_row_any
;
2251 if (res
== isl_tab_row_any
)
2259 static enum isl_tab_row_sign
context_lex_ineq_sign(struct isl_context
*context
,
2260 isl_int
*ineq
, int strict
)
2262 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2263 return tab_ineq_sign(clex
->tab
, ineq
, strict
);
2266 /* Check whether "ineq" can be added to the tableau without rendering
2269 static int context_lex_test_ineq(struct isl_context
*context
, isl_int
*ineq
)
2271 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2272 struct isl_tab_undo
*snap
;
2278 if (isl_tab_extend_cons(clex
->tab
, 1) < 0)
2281 snap
= isl_tab_snap(clex
->tab
);
2282 if (isl_tab_push_basis(clex
->tab
) < 0)
2284 clex
->tab
= add_lexmin_ineq(clex
->tab
, ineq
);
2285 clex
->tab
= check_integer_feasible(clex
->tab
);
2288 feasible
= !clex
->tab
->empty
;
2289 if (isl_tab_rollback(clex
->tab
, snap
) < 0)
2295 static int context_lex_get_div(struct isl_context
*context
, struct isl_tab
*tab
,
2296 struct isl_vec
*div
)
2298 return get_div(tab
, context
, div
);
2301 static int context_lex_add_div(struct isl_context
*context
, struct isl_vec
*div
,
2304 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2305 return context_tab_add_div(clex
->tab
, div
, nonneg
);
2308 static int context_lex_detect_equalities(struct isl_context
*context
,
2309 struct isl_tab
*tab
)
2314 static int context_lex_best_split(struct isl_context
*context
,
2315 struct isl_tab
*tab
)
2317 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2318 struct isl_tab_undo
*snap
;
2321 snap
= isl_tab_snap(clex
->tab
);
2322 if (isl_tab_push_basis(clex
->tab
) < 0)
2324 r
= best_split(tab
, clex
->tab
);
2326 if (isl_tab_rollback(clex
->tab
, snap
) < 0)
2332 static int context_lex_is_empty(struct isl_context
*context
)
2334 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2337 return clex
->tab
->empty
;
2340 static void *context_lex_save(struct isl_context
*context
)
2342 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2343 struct isl_tab_undo
*snap
;
2345 snap
= isl_tab_snap(clex
->tab
);
2346 if (isl_tab_push_basis(clex
->tab
) < 0)
2348 if (isl_tab_save_samples(clex
->tab
) < 0)
2354 static void context_lex_restore(struct isl_context
*context
, void *save
)
2356 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2357 if (isl_tab_rollback(clex
->tab
, (struct isl_tab_undo
*)save
) < 0) {
2358 isl_tab_free(clex
->tab
);
2363 static int context_lex_is_ok(struct isl_context
*context
)
2365 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2369 /* For each variable in the context tableau, check if the variable can
2370 * only attain non-negative values. If so, mark the parameter as non-negative
2371 * in the main tableau. This allows for a more direct identification of some
2372 * cases of violated constraints.
2374 static struct isl_tab
*tab_detect_nonnegative_parameters(struct isl_tab
*tab
,
2375 struct isl_tab
*context_tab
)
2378 struct isl_tab_undo
*snap
;
2379 struct isl_vec
*ineq
= NULL
;
2380 struct isl_tab_var
*var
;
2383 if (context_tab
->n_var
== 0)
2386 ineq
= isl_vec_alloc(tab
->mat
->ctx
, 1 + context_tab
->n_var
);
2390 if (isl_tab_extend_cons(context_tab
, 1) < 0)
2393 snap
= isl_tab_snap(context_tab
);
2396 isl_seq_clr(ineq
->el
, ineq
->size
);
2397 for (i
= 0; i
< context_tab
->n_var
; ++i
) {
2398 isl_int_set_si(ineq
->el
[1 + i
], 1);
2399 if (isl_tab_add_ineq(context_tab
, ineq
->el
) < 0)
2401 var
= &context_tab
->con
[context_tab
->n_con
- 1];
2402 if (!context_tab
->empty
&&
2403 !isl_tab_min_at_most_neg_one(context_tab
, var
)) {
2405 if (i
>= tab
->n_param
)
2406 j
= i
- tab
->n_param
+ tab
->n_var
- tab
->n_div
;
2407 tab
->var
[j
].is_nonneg
= 1;
2410 isl_int_set_si(ineq
->el
[1 + i
], 0);
2411 if (isl_tab_rollback(context_tab
, snap
) < 0)
2415 if (context_tab
->M
&& n
== context_tab
->n_var
) {
2416 context_tab
->mat
= isl_mat_drop_cols(context_tab
->mat
, 2, 1);
2428 static struct isl_tab
*context_lex_detect_nonnegative_parameters(
2429 struct isl_context
*context
, struct isl_tab
*tab
)
2431 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2432 struct isl_tab_undo
*snap
;
2434 snap
= isl_tab_snap(clex
->tab
);
2435 if (isl_tab_push_basis(clex
->tab
) < 0)
2438 tab
= tab_detect_nonnegative_parameters(tab
, clex
->tab
);
2440 if (isl_tab_rollback(clex
->tab
, snap
) < 0)
2449 static void context_lex_invalidate(struct isl_context
*context
)
2451 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2452 isl_tab_free(clex
->tab
);
2456 static void context_lex_free(struct isl_context
*context
)
2458 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2459 isl_tab_free(clex
->tab
);
2463 struct isl_context_op isl_context_lex_op
= {
2464 context_lex_detect_nonnegative_parameters
,
2465 context_lex_peek_basic_set
,
2466 context_lex_peek_tab
,
2468 context_lex_add_ineq
,
2469 context_lex_ineq_sign
,
2470 context_lex_test_ineq
,
2471 context_lex_get_div
,
2472 context_lex_add_div
,
2473 context_lex_detect_equalities
,
2474 context_lex_best_split
,
2475 context_lex_is_empty
,
2478 context_lex_restore
,
2479 context_lex_invalidate
,
2483 static struct isl_tab
*context_tab_for_lexmin(struct isl_basic_set
*bset
)
2485 struct isl_tab
*tab
;
2487 bset
= isl_basic_set_cow(bset
);
2490 tab
= tab_for_lexmin((struct isl_basic_map
*)bset
, NULL
, 1, 0);
2493 if (isl_tab_track_bset(tab
, bset
) < 0)
2495 tab
= isl_tab_init_samples(tab
);
2498 isl_basic_set_free(bset
);
2502 static struct isl_context
*isl_context_lex_alloc(struct isl_basic_set
*dom
)
2504 struct isl_context_lex
*clex
;
2509 clex
= isl_alloc_type(dom
->ctx
, struct isl_context_lex
);
2513 clex
->context
.op
= &isl_context_lex_op
;
2515 clex
->tab
= context_tab_for_lexmin(isl_basic_set_copy(dom
));
2516 clex
->tab
= restore_lexmin(clex
->tab
);
2517 clex
->tab
= check_integer_feasible(clex
->tab
);
2521 return &clex
->context
;
2523 clex
->context
.op
->free(&clex
->context
);
2527 struct isl_context_gbr
{
2528 struct isl_context context
;
2529 struct isl_tab
*tab
;
2530 struct isl_tab
*shifted
;
2531 struct isl_tab
*cone
;
2534 static struct isl_tab
*context_gbr_detect_nonnegative_parameters(
2535 struct isl_context
*context
, struct isl_tab
*tab
)
2537 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2538 return tab_detect_nonnegative_parameters(tab
, cgbr
->tab
);
2541 static struct isl_basic_set
*context_gbr_peek_basic_set(
2542 struct isl_context
*context
)
2544 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2547 return isl_tab_peek_bset(cgbr
->tab
);
2550 static struct isl_tab
*context_gbr_peek_tab(struct isl_context
*context
)
2552 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2556 /* Initialize the "shifted" tableau of the context, which
2557 * contains the constraints of the original tableau shifted
2558 * by the sum of all negative coefficients. This ensures
2559 * that any rational point in the shifted tableau can
2560 * be rounded up to yield an integer point in the original tableau.
2562 static void gbr_init_shifted(struct isl_context_gbr
*cgbr
)
2565 struct isl_vec
*cst
;
2566 struct isl_basic_set
*bset
= isl_tab_peek_bset(cgbr
->tab
);
2567 unsigned dim
= isl_basic_set_total_dim(bset
);
2569 cst
= isl_vec_alloc(cgbr
->tab
->mat
->ctx
, bset
->n_ineq
);
2573 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
2574 isl_int_set(cst
->el
[i
], bset
->ineq
[i
][0]);
2575 for (j
= 0; j
< dim
; ++j
) {
2576 if (!isl_int_is_neg(bset
->ineq
[i
][1 + j
]))
2578 isl_int_add(bset
->ineq
[i
][0], bset
->ineq
[i
][0],
2579 bset
->ineq
[i
][1 + j
]);
2583 cgbr
->shifted
= isl_tab_from_basic_set(bset
);
2585 for (i
= 0; i
< bset
->n_ineq
; ++i
)
2586 isl_int_set(bset
->ineq
[i
][0], cst
->el
[i
]);
2591 /* Check if the shifted tableau is non-empty, and if so
2592 * use the sample point to construct an integer point
2593 * of the context tableau.
2595 static struct isl_vec
*gbr_get_shifted_sample(struct isl_context_gbr
*cgbr
)
2597 struct isl_vec
*sample
;
2600 gbr_init_shifted(cgbr
);
2603 if (cgbr
->shifted
->empty
)
2604 return isl_vec_alloc(cgbr
->tab
->mat
->ctx
, 0);
2606 sample
= isl_tab_get_sample_value(cgbr
->shifted
);
2607 sample
= isl_vec_ceil(sample
);
2612 static struct isl_basic_set
*drop_constant_terms(struct isl_basic_set
*bset
)
2619 for (i
= 0; i
< bset
->n_eq
; ++i
)
2620 isl_int_set_si(bset
->eq
[i
][0], 0);
2622 for (i
= 0; i
< bset
->n_ineq
; ++i
)
2623 isl_int_set_si(bset
->ineq
[i
][0], 0);
2628 static int use_shifted(struct isl_context_gbr
*cgbr
)
2630 return cgbr
->tab
->bmap
->n_eq
== 0 && cgbr
->tab
->bmap
->n_div
== 0;
2633 static struct isl_vec
*gbr_get_sample(struct isl_context_gbr
*cgbr
)
2635 struct isl_basic_set
*bset
;
2636 struct isl_basic_set
*cone
;
2638 if (isl_tab_sample_is_integer(cgbr
->tab
))
2639 return isl_tab_get_sample_value(cgbr
->tab
);
2641 if (use_shifted(cgbr
)) {
2642 struct isl_vec
*sample
;
2644 sample
= gbr_get_shifted_sample(cgbr
);
2645 if (!sample
|| sample
->size
> 0)
2648 isl_vec_free(sample
);
2652 bset
= isl_tab_peek_bset(cgbr
->tab
);
2653 cgbr
->cone
= isl_tab_from_recession_cone(bset
);
2656 if (isl_tab_track_bset(cgbr
->cone
, isl_basic_set_dup(bset
)) < 0)
2659 cgbr
->cone
= isl_tab_detect_implicit_equalities(cgbr
->cone
);
2663 if (cgbr
->cone
->n_dead
== cgbr
->cone
->n_col
) {
2664 struct isl_vec
*sample
;
2665 struct isl_tab_undo
*snap
;
2667 if (cgbr
->tab
->basis
) {
2668 if (cgbr
->tab
->basis
->n_col
!= 1 + cgbr
->tab
->n_var
) {
2669 isl_mat_free(cgbr
->tab
->basis
);
2670 cgbr
->tab
->basis
= NULL
;
2672 cgbr
->tab
->n_zero
= 0;
2673 cgbr
->tab
->n_unbounded
= 0;
2677 snap
= isl_tab_snap(cgbr
->tab
);
2679 sample
= isl_tab_sample(cgbr
->tab
);
2681 if (isl_tab_rollback(cgbr
->tab
, snap
) < 0) {
2682 isl_vec_free(sample
);
2689 cone
= isl_basic_set_dup(isl_tab_peek_bset(cgbr
->cone
));
2690 cone
= drop_constant_terms(cone
);
2691 cone
= isl_basic_set_update_from_tab(cone
, cgbr
->cone
);
2692 cone
= isl_basic_set_underlying_set(cone
);
2693 cone
= isl_basic_set_gauss(cone
, NULL
);
2695 bset
= isl_basic_set_dup(isl_tab_peek_bset(cgbr
->tab
));
2696 bset
= isl_basic_set_update_from_tab(bset
, cgbr
->tab
);
2697 bset
= isl_basic_set_underlying_set(bset
);
2698 bset
= isl_basic_set_gauss(bset
, NULL
);
2700 return isl_basic_set_sample_with_cone(bset
, cone
);
2703 static void check_gbr_integer_feasible(struct isl_context_gbr
*cgbr
)
2705 struct isl_vec
*sample
;
2710 if (cgbr
->tab
->empty
)
2713 sample
= gbr_get_sample(cgbr
);
2717 if (sample
->size
== 0) {
2718 isl_vec_free(sample
);
2719 if (isl_tab_mark_empty(cgbr
->tab
) < 0)
2724 cgbr
->tab
= isl_tab_add_sample(cgbr
->tab
, sample
);
2728 isl_tab_free(cgbr
->tab
);
2732 static struct isl_tab
*add_gbr_eq(struct isl_tab
*tab
, isl_int
*eq
)
2739 if (isl_tab_extend_cons(tab
, 2) < 0)
2742 tab
= isl_tab_add_eq(tab
, eq
);
2750 static void context_gbr_add_eq(struct isl_context
*context
, isl_int
*eq
,
2751 int check
, int update
)
2753 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2755 cgbr
->tab
= add_gbr_eq(cgbr
->tab
, eq
);
2757 if (cgbr
->cone
&& cgbr
->cone
->n_col
!= cgbr
->cone
->n_dead
) {
2758 if (isl_tab_extend_cons(cgbr
->cone
, 2) < 0)
2760 cgbr
->cone
= isl_tab_add_eq(cgbr
->cone
, eq
);
2764 int v
= tab_has_valid_sample(cgbr
->tab
, eq
, 1);
2768 check_gbr_integer_feasible(cgbr
);
2771 cgbr
->tab
= check_samples(cgbr
->tab
, eq
, 1);
2774 isl_tab_free(cgbr
->tab
);
2778 static void add_gbr_ineq(struct isl_context_gbr
*cgbr
, isl_int
*ineq
)
2783 if (isl_tab_extend_cons(cgbr
->tab
, 1) < 0)
2786 if (isl_tab_add_ineq(cgbr
->tab
, ineq
) < 0)
2789 if (cgbr
->shifted
&& !cgbr
->shifted
->empty
&& use_shifted(cgbr
)) {
2792 dim
= isl_basic_map_total_dim(cgbr
->tab
->bmap
);
2794 if (isl_tab_extend_cons(cgbr
->shifted
, 1) < 0)
2797 for (i
= 0; i
< dim
; ++i
) {
2798 if (!isl_int_is_neg(ineq
[1 + i
]))
2800 isl_int_add(ineq
[0], ineq
[0], ineq
[1 + i
]);
2803 if (isl_tab_add_ineq(cgbr
->shifted
, ineq
) < 0)
2806 for (i
= 0; i
< dim
; ++i
) {
2807 if (!isl_int_is_neg(ineq
[1 + i
]))
2809 isl_int_sub(ineq
[0], ineq
[0], ineq
[1 + i
]);
2813 if (cgbr
->cone
&& cgbr
->cone
->n_col
!= cgbr
->cone
->n_dead
) {
2814 if (isl_tab_extend_cons(cgbr
->cone
, 1) < 0)
2816 if (isl_tab_add_ineq(cgbr
->cone
, ineq
) < 0)
2822 isl_tab_free(cgbr
->tab
);
2826 static void context_gbr_add_ineq(struct isl_context
*context
, isl_int
*ineq
,
2827 int check
, int update
)
2829 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2831 add_gbr_ineq(cgbr
, ineq
);
2836 int v
= tab_has_valid_sample(cgbr
->tab
, ineq
, 0);
2840 check_gbr_integer_feasible(cgbr
);
2843 cgbr
->tab
= check_samples(cgbr
->tab
, ineq
, 0);
2846 isl_tab_free(cgbr
->tab
);
2850 static enum isl_tab_row_sign
context_gbr_ineq_sign(struct isl_context
*context
,
2851 isl_int
*ineq
, int strict
)
2853 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2854 return tab_ineq_sign(cgbr
->tab
, ineq
, strict
);
2857 /* Check whether "ineq" can be added to the tableau without rendering
2860 static int context_gbr_test_ineq(struct isl_context
*context
, isl_int
*ineq
)
2862 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2863 struct isl_tab_undo
*snap
;
2864 struct isl_tab_undo
*shifted_snap
= NULL
;
2865 struct isl_tab_undo
*cone_snap
= NULL
;
2871 if (isl_tab_extend_cons(cgbr
->tab
, 1) < 0)
2874 snap
= isl_tab_snap(cgbr
->tab
);
2876 shifted_snap
= isl_tab_snap(cgbr
->shifted
);
2878 cone_snap
= isl_tab_snap(cgbr
->cone
);
2879 add_gbr_ineq(cgbr
, ineq
);
2880 check_gbr_integer_feasible(cgbr
);
2883 feasible
= !cgbr
->tab
->empty
;
2884 if (isl_tab_rollback(cgbr
->tab
, snap
) < 0)
2887 if (isl_tab_rollback(cgbr
->shifted
, shifted_snap
))
2889 } else if (cgbr
->shifted
) {
2890 isl_tab_free(cgbr
->shifted
);
2891 cgbr
->shifted
= NULL
;
2894 if (isl_tab_rollback(cgbr
->cone
, cone_snap
))
2896 } else if (cgbr
->cone
) {
2897 isl_tab_free(cgbr
->cone
);
2904 /* Return the column of the last of the variables associated to
2905 * a column that has a non-zero coefficient.
2906 * This function is called in a context where only coefficients
2907 * of parameters or divs can be non-zero.
2909 static int last_non_zero_var_col(struct isl_tab
*tab
, isl_int
*p
)
2913 unsigned dim
= tab
->n_var
- tab
->n_param
- tab
->n_div
;
2915 if (tab
->n_var
== 0)
2918 for (i
= tab
->n_var
- 1; i
>= 0; --i
) {
2919 if (i
>= tab
->n_param
&& i
< tab
->n_var
- tab
->n_div
)
2921 if (tab
->var
[i
].is_row
)
2923 col
= tab
->var
[i
].index
;
2924 if (!isl_int_is_zero(p
[col
]))
2931 /* Look through all the recently added equalities in the context
2932 * to see if we can propagate any of them to the main tableau.
2934 * The newly added equalities in the context are encoded as pairs
2935 * of inequalities starting at inequality "first".
2937 * We tentatively add each of these equalities to the main tableau
2938 * and if this happens to result in a row with a final coefficient
2939 * that is one or negative one, we use it to kill a column
2940 * in the main tableau. Otherwise, we discard the tentatively
2943 static void propagate_equalities(struct isl_context_gbr
*cgbr
,
2944 struct isl_tab
*tab
, unsigned first
)
2947 struct isl_vec
*eq
= NULL
;
2949 eq
= isl_vec_alloc(tab
->mat
->ctx
, 1 + tab
->n_var
);
2953 if (isl_tab_extend_cons(tab
, (cgbr
->tab
->bmap
->n_ineq
- first
)/2) < 0)
2956 isl_seq_clr(eq
->el
+ 1 + tab
->n_param
,
2957 tab
->n_var
- tab
->n_param
- tab
->n_div
);
2958 for (i
= first
; i
< cgbr
->tab
->bmap
->n_ineq
; i
+= 2) {
2961 struct isl_tab_undo
*snap
;
2962 snap
= isl_tab_snap(tab
);
2964 isl_seq_cpy(eq
->el
, cgbr
->tab
->bmap
->ineq
[i
], 1 + tab
->n_param
);
2965 isl_seq_cpy(eq
->el
+ 1 + tab
->n_var
- tab
->n_div
,
2966 cgbr
->tab
->bmap
->ineq
[i
] + 1 + tab
->n_param
,
2969 r
= isl_tab_add_row(tab
, eq
->el
);
2972 r
= tab
->con
[r
].index
;
2973 j
= last_non_zero_var_col(tab
, tab
->mat
->row
[r
] + 2 + tab
->M
);
2974 if (j
< 0 || j
< tab
->n_dead
||
2975 !isl_int_is_one(tab
->mat
->row
[r
][0]) ||
2976 (!isl_int_is_one(tab
->mat
->row
[r
][2 + tab
->M
+ j
]) &&
2977 !isl_int_is_negone(tab
->mat
->row
[r
][2 + tab
->M
+ j
]))) {
2978 if (isl_tab_rollback(tab
, snap
) < 0)
2982 if (isl_tab_pivot(tab
, r
, j
) < 0)
2984 if (isl_tab_kill_col(tab
, j
) < 0)
2987 tab
= restore_lexmin(tab
);
2995 isl_tab_free(cgbr
->tab
);
2999 static int context_gbr_detect_equalities(struct isl_context
*context
,
3000 struct isl_tab
*tab
)
3002 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3003 struct isl_ctx
*ctx
;
3005 enum isl_lp_result res
;
3008 ctx
= cgbr
->tab
->mat
->ctx
;
3011 struct isl_basic_set
*bset
= isl_tab_peek_bset(cgbr
->tab
);
3012 cgbr
->cone
= isl_tab_from_recession_cone(bset
);
3015 if (isl_tab_track_bset(cgbr
->cone
, isl_basic_set_dup(bset
)) < 0)
3018 cgbr
->cone
= isl_tab_detect_implicit_equalities(cgbr
->cone
);
3020 n_ineq
= cgbr
->tab
->bmap
->n_ineq
;
3021 cgbr
->tab
= isl_tab_detect_equalities(cgbr
->tab
, cgbr
->cone
);
3022 if (cgbr
->tab
&& cgbr
->tab
->bmap
->n_ineq
> n_ineq
)
3023 propagate_equalities(cgbr
, tab
, n_ineq
);
3027 isl_tab_free(cgbr
->tab
);
3032 static int context_gbr_get_div(struct isl_context
*context
, struct isl_tab
*tab
,
3033 struct isl_vec
*div
)
3035 return get_div(tab
, context
, div
);
3038 static int context_gbr_add_div(struct isl_context
*context
, struct isl_vec
*div
,
3041 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3045 if (isl_tab_extend_cons(cgbr
->cone
, 3) < 0)
3047 if (isl_tab_extend_vars(cgbr
->cone
, 1) < 0)
3049 if (isl_tab_allocate_var(cgbr
->cone
) <0)
3052 cgbr
->cone
->bmap
= isl_basic_map_extend_dim(cgbr
->cone
->bmap
,
3053 isl_basic_map_get_dim(cgbr
->cone
->bmap
), 1, 0, 2);
3054 k
= isl_basic_map_alloc_div(cgbr
->cone
->bmap
);
3057 isl_seq_cpy(cgbr
->cone
->bmap
->div
[k
], div
->el
, div
->size
);
3058 if (isl_tab_push(cgbr
->cone
, isl_tab_undo_bmap_div
) < 0)
3061 return context_tab_add_div(cgbr
->tab
, div
, nonneg
);
3064 static int context_gbr_best_split(struct isl_context
*context
,
3065 struct isl_tab
*tab
)
3067 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3068 struct isl_tab_undo
*snap
;
3071 snap
= isl_tab_snap(cgbr
->tab
);
3072 r
= best_split(tab
, cgbr
->tab
);
3074 if (isl_tab_rollback(cgbr
->tab
, snap
) < 0)
3080 static int context_gbr_is_empty(struct isl_context
*context
)
3082 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3085 return cgbr
->tab
->empty
;
3088 struct isl_gbr_tab_undo
{
3089 struct isl_tab_undo
*tab_snap
;
3090 struct isl_tab_undo
*shifted_snap
;
3091 struct isl_tab_undo
*cone_snap
;
3094 static void *context_gbr_save(struct isl_context
*context
)
3096 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3097 struct isl_gbr_tab_undo
*snap
;
3099 snap
= isl_alloc_type(cgbr
->tab
->mat
->ctx
, struct isl_gbr_tab_undo
);
3103 snap
->tab_snap
= isl_tab_snap(cgbr
->tab
);
3104 if (isl_tab_save_samples(cgbr
->tab
) < 0)
3108 snap
->shifted_snap
= isl_tab_snap(cgbr
->shifted
);
3110 snap
->shifted_snap
= NULL
;
3113 snap
->cone_snap
= isl_tab_snap(cgbr
->cone
);
3115 snap
->cone_snap
= NULL
;
3123 static void context_gbr_restore(struct isl_context
*context
, void *save
)
3125 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3126 struct isl_gbr_tab_undo
*snap
= (struct isl_gbr_tab_undo
*)save
;
3129 if (isl_tab_rollback(cgbr
->tab
, snap
->tab_snap
) < 0) {
3130 isl_tab_free(cgbr
->tab
);
3134 if (snap
->shifted_snap
) {
3135 if (isl_tab_rollback(cgbr
->shifted
, snap
->shifted_snap
) < 0)
3137 } else if (cgbr
->shifted
) {
3138 isl_tab_free(cgbr
->shifted
);
3139 cgbr
->shifted
= NULL
;
3142 if (snap
->cone_snap
) {
3143 if (isl_tab_rollback(cgbr
->cone
, snap
->cone_snap
) < 0)
3145 } else if (cgbr
->cone
) {
3146 isl_tab_free(cgbr
->cone
);
3155 isl_tab_free(cgbr
->tab
);
3159 static int context_gbr_is_ok(struct isl_context
*context
)
3161 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3165 static void context_gbr_invalidate(struct isl_context
*context
)
3167 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3168 isl_tab_free(cgbr
->tab
);
3172 static void context_gbr_free(struct isl_context
*context
)
3174 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3175 isl_tab_free(cgbr
->tab
);
3176 isl_tab_free(cgbr
->shifted
);
3177 isl_tab_free(cgbr
->cone
);
3181 struct isl_context_op isl_context_gbr_op
= {
3182 context_gbr_detect_nonnegative_parameters
,
3183 context_gbr_peek_basic_set
,
3184 context_gbr_peek_tab
,
3186 context_gbr_add_ineq
,
3187 context_gbr_ineq_sign
,
3188 context_gbr_test_ineq
,
3189 context_gbr_get_div
,
3190 context_gbr_add_div
,
3191 context_gbr_detect_equalities
,
3192 context_gbr_best_split
,
3193 context_gbr_is_empty
,
3196 context_gbr_restore
,
3197 context_gbr_invalidate
,
3201 static struct isl_context
*isl_context_gbr_alloc(struct isl_basic_set
*dom
)
3203 struct isl_context_gbr
*cgbr
;
3208 cgbr
= isl_calloc_type(dom
->ctx
, struct isl_context_gbr
);
3212 cgbr
->context
.op
= &isl_context_gbr_op
;
3214 cgbr
->shifted
= NULL
;
3216 cgbr
->tab
= isl_tab_from_basic_set(dom
);
3217 cgbr
->tab
= isl_tab_init_samples(cgbr
->tab
);
3220 if (isl_tab_track_bset(cgbr
->tab
,
3221 isl_basic_set_cow(isl_basic_set_copy(dom
))) < 0)
3223 check_gbr_integer_feasible(cgbr
);
3225 return &cgbr
->context
;
3227 cgbr
->context
.op
->free(&cgbr
->context
);
3231 static struct isl_context
*isl_context_alloc(struct isl_basic_set
*dom
)
3236 if (dom
->ctx
->opt
->context
== ISL_CONTEXT_LEXMIN
)
3237 return isl_context_lex_alloc(dom
);
3239 return isl_context_gbr_alloc(dom
);
3242 /* Construct an isl_sol_map structure for accumulating the solution.
3243 * If track_empty is set, then we also keep track of the parts
3244 * of the context where there is no solution.
3245 * If max is set, then we are solving a maximization, rather than
3246 * a minimization problem, which means that the variables in the
3247 * tableau have value "M - x" rather than "M + x".
3249 static struct isl_sol_map
*sol_map_init(struct isl_basic_map
*bmap
,
3250 struct isl_basic_set
*dom
, int track_empty
, int max
)
3252 struct isl_sol_map
*sol_map
;
3254 sol_map
= isl_calloc_type(bset
->ctx
, struct isl_sol_map
);
3258 sol_map
->sol
.rational
= ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
3259 sol_map
->sol
.dec_level
.callback
.run
= &sol_dec_level_wrap
;
3260 sol_map
->sol
.dec_level
.sol
= &sol_map
->sol
;
3261 sol_map
->sol
.max
= max
;
3262 sol_map
->sol
.n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
3263 sol_map
->sol
.add
= &sol_map_add_wrap
;
3264 sol_map
->sol
.add_empty
= track_empty
? &sol_map_add_empty_wrap
: NULL
;
3265 sol_map
->sol
.free
= &sol_map_free_wrap
;
3266 sol_map
->map
= isl_map_alloc_dim(isl_basic_map_get_dim(bmap
), 1,
3271 sol_map
->sol
.context
= isl_context_alloc(dom
);
3272 if (!sol_map
->sol
.context
)
3276 sol_map
->empty
= isl_set_alloc_dim(isl_basic_set_get_dim(dom
),
3277 1, ISL_SET_DISJOINT
);
3278 if (!sol_map
->empty
)
3282 isl_basic_set_free(dom
);
3285 isl_basic_set_free(dom
);
3286 sol_map_free(sol_map
);
3290 /* Check whether all coefficients of (non-parameter) variables
3291 * are non-positive, meaning that no pivots can be performed on the row.
3293 static int is_critical(struct isl_tab
*tab
, int row
)
3296 unsigned off
= 2 + tab
->M
;
3298 for (j
= tab
->n_dead
; j
< tab
->n_col
; ++j
) {
3299 if (tab
->col_var
[j
] >= 0 &&
3300 (tab
->col_var
[j
] < tab
->n_param
||
3301 tab
->col_var
[j
] >= tab
->n_var
- tab
->n_div
))
3304 if (isl_int_is_pos(tab
->mat
->row
[row
][off
+ j
]))
3311 /* Check whether the inequality represented by vec is strict over the integers,
3312 * i.e., there are no integer values satisfying the constraint with
3313 * equality. This happens if the gcd of the coefficients is not a divisor
3314 * of the constant term. If so, scale the constraint down by the gcd
3315 * of the coefficients.
3317 static int is_strict(struct isl_vec
*vec
)
3323 isl_seq_gcd(vec
->el
+ 1, vec
->size
- 1, &gcd
);
3324 if (!isl_int_is_one(gcd
)) {
3325 strict
= !isl_int_is_divisible_by(vec
->el
[0], gcd
);
3326 isl_int_fdiv_q(vec
->el
[0], vec
->el
[0], gcd
);
3327 isl_seq_scale_down(vec
->el
+ 1, vec
->el
+ 1, gcd
, vec
->size
-1);
3334 /* Determine the sign of the given row of the main tableau.
3335 * The result is one of
3336 * isl_tab_row_pos: always non-negative; no pivot needed
3337 * isl_tab_row_neg: always non-positive; pivot
3338 * isl_tab_row_any: can be both positive and negative; split
3340 * We first handle some simple cases
3341 * - the row sign may be known already
3342 * - the row may be obviously non-negative
3343 * - the parametric constant may be equal to that of another row
3344 * for which we know the sign. This sign will be either "pos" or
3345 * "any". If it had been "neg" then we would have pivoted before.
3347 * If none of these cases hold, we check the value of the row for each
3348 * of the currently active samples. Based on the signs of these values
3349 * we make an initial determination of the sign of the row.
3351 * all zero -> unk(nown)
3352 * all non-negative -> pos
3353 * all non-positive -> neg
3354 * both negative and positive -> all
3356 * If we end up with "all", we are done.
3357 * Otherwise, we perform a check for positive and/or negative
3358 * values as follows.
3360 * samples neg unk pos
3366 * There is no special sign for "zero", because we can usually treat zero
3367 * as either non-negative or non-positive, whatever works out best.
3368 * However, if the row is "critical", meaning that pivoting is impossible
3369 * then we don't want to limp zero with the non-positive case, because
3370 * then we we would lose the solution for those values of the parameters
3371 * where the value of the row is zero. Instead, we treat 0 as non-negative
3372 * ensuring a split if the row can attain both zero and negative values.
3373 * The same happens when the original constraint was one that could not
3374 * be satisfied with equality by any integer values of the parameters.
3375 * In this case, we normalize the constraint, but then a value of zero
3376 * for the normalized constraint is actually a positive value for the
3377 * original constraint, so again we need to treat zero as non-negative.
3378 * In both these cases, we have the following decision tree instead:
3380 * all non-negative -> pos
3381 * all negative -> neg
3382 * both negative and non-negative -> all
3390 static enum isl_tab_row_sign
row_sign(struct isl_tab
*tab
,
3391 struct isl_sol
*sol
, int row
)
3393 struct isl_vec
*ineq
= NULL
;
3394 int res
= isl_tab_row_unknown
;
3399 if (tab
->row_sign
[row
] != isl_tab_row_unknown
)
3400 return tab
->row_sign
[row
];
3401 if (is_obviously_nonneg(tab
, row
))
3402 return isl_tab_row_pos
;
3403 for (row2
= tab
->n_redundant
; row2
< tab
->n_row
; ++row2
) {
3404 if (tab
->row_sign
[row2
] == isl_tab_row_unknown
)
3406 if (identical_parameter_line(tab
, row
, row2
))
3407 return tab
->row_sign
[row2
];
3410 critical
= is_critical(tab
, row
);
3412 ineq
= get_row_parameter_ineq(tab
, row
);
3416 strict
= is_strict(ineq
);
3418 res
= sol
->context
->op
->ineq_sign(sol
->context
, ineq
->el
,
3419 critical
|| strict
);
3421 if (res
== isl_tab_row_unknown
|| res
== isl_tab_row_pos
) {
3422 /* test for negative values */
3424 isl_seq_neg(ineq
->el
, ineq
->el
, ineq
->size
);
3425 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3427 feasible
= sol
->context
->op
->test_ineq(sol
->context
, ineq
->el
);
3431 res
= isl_tab_row_pos
;
3433 res
= (res
== isl_tab_row_unknown
) ? isl_tab_row_neg
3435 if (res
== isl_tab_row_neg
) {
3436 isl_seq_neg(ineq
->el
, ineq
->el
, ineq
->size
);
3437 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3441 if (res
== isl_tab_row_neg
) {
3442 /* test for positive values */
3444 if (!critical
&& !strict
)
3445 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3447 feasible
= sol
->context
->op
->test_ineq(sol
->context
, ineq
->el
);
3451 res
= isl_tab_row_any
;
3461 static void find_solutions(struct isl_sol
*sol
, struct isl_tab
*tab
);
3463 /* Find solutions for values of the parameters that satisfy the given
3466 * We currently take a snapshot of the context tableau that is reset
3467 * when we return from this function, while we make a copy of the main
3468 * tableau, leaving the original main tableau untouched.
3469 * These are fairly arbitrary choices. Making a copy also of the context
3470 * tableau would obviate the need to undo any changes made to it later,
3471 * while taking a snapshot of the main tableau could reduce memory usage.
3472 * If we were to switch to taking a snapshot of the main tableau,
3473 * we would have to keep in mind that we need to save the row signs
3474 * and that we need to do this before saving the current basis
3475 * such that the basis has been restore before we restore the row signs.
3477 static void find_in_pos(struct isl_sol
*sol
, struct isl_tab
*tab
, isl_int
*ineq
)
3483 saved
= sol
->context
->op
->save(sol
->context
);
3485 tab
= isl_tab_dup(tab
);
3489 sol
->context
->op
->add_ineq(sol
->context
, ineq
, 0, 1);
3491 find_solutions(sol
, tab
);
3493 sol
->context
->op
->restore(sol
->context
, saved
);
3499 /* Record the absence of solutions for those values of the parameters
3500 * that do not satisfy the given inequality with equality.
3502 static void no_sol_in_strict(struct isl_sol
*sol
,
3503 struct isl_tab
*tab
, struct isl_vec
*ineq
)
3510 saved
= sol
->context
->op
->save(sol
->context
);
3512 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3514 sol
->context
->op
->add_ineq(sol
->context
, ineq
->el
, 1, 0);
3523 isl_int_add_ui(ineq
->el
[0], ineq
->el
[0], 1);
3525 sol
->context
->op
->restore(sol
->context
, saved
);
3531 /* Compute the lexicographic minimum of the set represented by the main
3532 * tableau "tab" within the context "sol->context_tab".
3533 * On entry the sample value of the main tableau is lexicographically
3534 * less than or equal to this lexicographic minimum.
3535 * Pivots are performed until a feasible point is found, which is then
3536 * necessarily equal to the minimum, or until the tableau is found to
3537 * be infeasible. Some pivots may need to be performed for only some
3538 * feasible values of the context tableau. If so, the context tableau
3539 * is split into a part where the pivot is needed and a part where it is not.
3541 * Whenever we enter the main loop, the main tableau is such that no
3542 * "obvious" pivots need to be performed on it, where "obvious" means
3543 * that the given row can be seen to be negative without looking at
3544 * the context tableau. In particular, for non-parametric problems,
3545 * no pivots need to be performed on the main tableau.
3546 * The caller of find_solutions is responsible for making this property
3547 * hold prior to the first iteration of the loop, while restore_lexmin
3548 * is called before every other iteration.
3550 * Inside the main loop, we first examine the signs of the rows of
3551 * the main tableau within the context of the context tableau.
3552 * If we find a row that is always non-positive for all values of
3553 * the parameters satisfying the context tableau and negative for at
3554 * least one value of the parameters, we perform the appropriate pivot
3555 * and start over. An exception is the case where no pivot can be
3556 * performed on the row. In this case, we require that the sign of
3557 * the row is negative for all values of the parameters (rather than just
3558 * non-positive). This special case is handled inside row_sign, which
3559 * will say that the row can have any sign if it determines that it can
3560 * attain both negative and zero values.
3562 * If we can't find a row that always requires a pivot, but we can find
3563 * one or more rows that require a pivot for some values of the parameters
3564 * (i.e., the row can attain both positive and negative signs), then we split
3565 * the context tableau into two parts, one where we force the sign to be
3566 * non-negative and one where we force is to be negative.
3567 * The non-negative part is handled by a recursive call (through find_in_pos).
3568 * Upon returning from this call, we continue with the negative part and
3569 * perform the required pivot.
3571 * If no such rows can be found, all rows are non-negative and we have
3572 * found a (rational) feasible point. If we only wanted a rational point
3574 * Otherwise, we check if all values of the sample point of the tableau
3575 * are integral for the variables. If so, we have found the minimal
3576 * integral point and we are done.
3577 * If the sample point is not integral, then we need to make a distinction
3578 * based on whether the constant term is non-integral or the coefficients
3579 * of the parameters. Furthermore, in order to decide how to handle
3580 * the non-integrality, we also need to know whether the coefficients
3581 * of the other columns in the tableau are integral. This leads
3582 * to the following table. The first two rows do not correspond
3583 * to a non-integral sample point and are only mentioned for completeness.
3585 * constant parameters other
3588 * int int rat | -> no problem
3590 * rat int int -> fail
3592 * rat int rat -> cut
3595 * rat rat rat | -> parametric cut
3598 * rat rat int | -> split context
3600 * If the parametric constant is completely integral, then there is nothing
3601 * to be done. If the constant term is non-integral, but all the other
3602 * coefficient are integral, then there is nothing that can be done
3603 * and the tableau has no integral solution.
3604 * If, on the other hand, one or more of the other columns have rational
3605 * coeffcients, but the parameter coefficients are all integral, then
3606 * we can perform a regular (non-parametric) cut.
3607 * Finally, if there is any parameter coefficient that is non-integral,
3608 * then we need to involve the context tableau. There are two cases here.
3609 * If at least one other column has a rational coefficient, then we
3610 * can perform a parametric cut in the main tableau by adding a new
3611 * integer division in the context tableau.
3612 * If all other columns have integral coefficients, then we need to
3613 * enforce that the rational combination of parameters (c + \sum a_i y_i)/m
3614 * is always integral. We do this by introducing an integer division
3615 * q = floor((c + \sum a_i y_i)/m) and stipulating that its argument should
3616 * always be integral in the context tableau, i.e., m q = c + \sum a_i y_i.
3617 * Since q is expressed in the tableau as
3618 * c + \sum a_i y_i - m q >= 0
3619 * -c - \sum a_i y_i + m q + m - 1 >= 0
3620 * it is sufficient to add the inequality
3621 * -c - \sum a_i y_i + m q >= 0
3622 * In the part of the context where this inequality does not hold, the
3623 * main tableau is marked as being empty.
3625 static void find_solutions(struct isl_sol
*sol
, struct isl_tab
*tab
)
3627 struct isl_context
*context
;
3629 if (!tab
|| sol
->error
)
3632 context
= sol
->context
;
3636 if (context
->op
->is_empty(context
))
3639 for (; tab
&& !tab
->empty
; tab
= restore_lexmin(tab
)) {
3646 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
3647 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
3649 sgn
= row_sign(tab
, sol
, row
);
3652 tab
->row_sign
[row
] = sgn
;
3653 if (sgn
== isl_tab_row_any
)
3655 if (sgn
== isl_tab_row_any
&& split
== -1)
3657 if (sgn
== isl_tab_row_neg
)
3660 if (row
< tab
->n_row
)
3663 struct isl_vec
*ineq
;
3665 split
= context
->op
->best_split(context
, tab
);
3668 ineq
= get_row_parameter_ineq(tab
, split
);
3672 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
3673 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
3675 if (tab
->row_sign
[row
] == isl_tab_row_any
)
3676 tab
->row_sign
[row
] = isl_tab_row_unknown
;
3678 tab
->row_sign
[split
] = isl_tab_row_pos
;
3680 find_in_pos(sol
, tab
, ineq
->el
);
3681 tab
->row_sign
[split
] = isl_tab_row_neg
;
3683 isl_seq_neg(ineq
->el
, ineq
->el
, ineq
->size
);
3684 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3685 context
->op
->add_ineq(context
, ineq
->el
, 0, 1);
3693 row
= first_non_integer_row(tab
, &flags
);
3696 if (ISL_FL_ISSET(flags
, I_PAR
)) {
3697 if (ISL_FL_ISSET(flags
, I_VAR
)) {
3698 if (isl_tab_mark_empty(tab
) < 0)
3702 row
= add_cut(tab
, row
);
3703 } else if (ISL_FL_ISSET(flags
, I_VAR
)) {
3704 struct isl_vec
*div
;
3705 struct isl_vec
*ineq
;
3707 div
= get_row_split_div(tab
, row
);
3710 d
= context
->op
->get_div(context
, tab
, div
);
3714 ineq
= ineq_for_div(context
->op
->peek_basic_set(context
), d
);
3716 no_sol_in_strict(sol
, tab
, ineq
);
3717 isl_seq_neg(ineq
->el
, ineq
->el
, ineq
->size
);
3718 context
->op
->add_ineq(context
, ineq
->el
, 1, 1);
3720 if (sol
->error
|| !context
->op
->is_ok(context
))
3722 tab
= set_row_cst_to_div(tab
, row
, d
);
3724 row
= add_parametric_cut(tab
, row
, context
);
3737 /* Compute the lexicographic minimum of the set represented by the main
3738 * tableau "tab" within the context "sol->context_tab".
3740 * As a preprocessing step, we first transfer all the purely parametric
3741 * equalities from the main tableau to the context tableau, i.e.,
3742 * parameters that have been pivoted to a row.
3743 * These equalities are ignored by the main algorithm, because the
3744 * corresponding rows may not be marked as being non-negative.
3745 * In parts of the context where the added equality does not hold,
3746 * the main tableau is marked as being empty.
3748 static void find_solutions_main(struct isl_sol
*sol
, struct isl_tab
*tab
)
3754 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
3758 if (tab
->row_var
[row
] < 0)
3760 if (tab
->row_var
[row
] >= tab
->n_param
&&
3761 tab
->row_var
[row
] < tab
->n_var
- tab
->n_div
)
3763 if (tab
->row_var
[row
] < tab
->n_param
)
3764 p
= tab
->row_var
[row
];
3766 p
= tab
->row_var
[row
]
3767 + tab
->n_param
- (tab
->n_var
- tab
->n_div
);
3769 eq
= isl_vec_alloc(tab
->mat
->ctx
, 1+tab
->n_param
+tab
->n_div
);
3770 get_row_parameter_line(tab
, row
, eq
->el
);
3771 isl_int_neg(eq
->el
[1 + p
], tab
->mat
->row
[row
][0]);
3772 eq
= isl_vec_normalize(eq
);
3775 no_sol_in_strict(sol
, tab
, eq
);
3777 isl_seq_neg(eq
->el
, eq
->el
, eq
->size
);
3779 no_sol_in_strict(sol
, tab
, eq
);
3780 isl_seq_neg(eq
->el
, eq
->el
, eq
->size
);
3782 sol
->context
->op
->add_eq(sol
->context
, eq
->el
, 1, 1);
3786 if (isl_tab_mark_redundant(tab
, row
) < 0)
3789 if (sol
->context
->op
->is_empty(sol
->context
))
3792 row
= tab
->n_redundant
- 1;
3795 find_solutions(sol
, tab
);
3806 static void sol_map_find_solutions(struct isl_sol_map
*sol_map
,
3807 struct isl_tab
*tab
)
3809 find_solutions_main(&sol_map
->sol
, tab
);
3812 /* Check if integer division "div" of "dom" also occurs in "bmap".
3813 * If so, return its position within the divs.
3814 * If not, return -1.
3816 static int find_context_div(struct isl_basic_map
*bmap
,
3817 struct isl_basic_set
*dom
, unsigned div
)
3820 unsigned b_dim
= isl_dim_total(bmap
->dim
);
3821 unsigned d_dim
= isl_dim_total(dom
->dim
);
3823 if (isl_int_is_zero(dom
->div
[div
][0]))
3825 if (isl_seq_first_non_zero(dom
->div
[div
] + 2 + d_dim
, dom
->n_div
) != -1)
3828 for (i
= 0; i
< bmap
->n_div
; ++i
) {
3829 if (isl_int_is_zero(bmap
->div
[i
][0]))
3831 if (isl_seq_first_non_zero(bmap
->div
[i
] + 2 + d_dim
,
3832 (b_dim
- d_dim
) + bmap
->n_div
) != -1)
3834 if (isl_seq_eq(bmap
->div
[i
], dom
->div
[div
], 2 + d_dim
))
3840 /* The correspondence between the variables in the main tableau,
3841 * the context tableau, and the input map and domain is as follows.
3842 * The first n_param and the last n_div variables of the main tableau
3843 * form the variables of the context tableau.
3844 * In the basic map, these n_param variables correspond to the
3845 * parameters and the input dimensions. In the domain, they correspond
3846 * to the parameters and the set dimensions.
3847 * The n_div variables correspond to the integer divisions in the domain.
3848 * To ensure that everything lines up, we may need to copy some of the
3849 * integer divisions of the domain to the map. These have to be placed
3850 * in the same order as those in the context and they have to be placed
3851 * after any other integer divisions that the map may have.
3852 * This function performs the required reordering.
3854 static struct isl_basic_map
*align_context_divs(struct isl_basic_map
*bmap
,
3855 struct isl_basic_set
*dom
)
3861 for (i
= 0; i
< dom
->n_div
; ++i
)
3862 if (find_context_div(bmap
, dom
, i
) != -1)
3864 other
= bmap
->n_div
- common
;
3865 if (dom
->n_div
- common
> 0) {
3866 bmap
= isl_basic_map_extend_dim(bmap
, isl_dim_copy(bmap
->dim
),
3867 dom
->n_div
- common
, 0, 0);
3871 for (i
= 0; i
< dom
->n_div
; ++i
) {
3872 int pos
= find_context_div(bmap
, dom
, i
);
3874 pos
= isl_basic_map_alloc_div(bmap
);
3877 isl_int_set_si(bmap
->div
[pos
][0], 0);
3879 if (pos
!= other
+ i
)
3880 isl_basic_map_swap_div(bmap
, pos
, other
+ i
);
3884 isl_basic_map_free(bmap
);
3888 /* Compute the lexicographic minimum (or maximum if "max" is set)
3889 * of "bmap" over the domain "dom" and return the result as a map.
3890 * If "empty" is not NULL, then *empty is assigned a set that
3891 * contains those parts of the domain where there is no solution.
3892 * If "bmap" is marked as rational (ISL_BASIC_MAP_RATIONAL),
3893 * then we compute the rational optimum. Otherwise, we compute
3894 * the integral optimum.
3896 * We perform some preprocessing. As the PILP solver does not
3897 * handle implicit equalities very well, we first make sure all
3898 * the equalities are explicitly available.
3899 * We also make sure the divs in the domain are properly order,
3900 * because they will be added one by one in the given order
3901 * during the construction of the solution map.
3903 struct isl_map
*isl_tab_basic_map_partial_lexopt(
3904 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
3905 struct isl_set
**empty
, int max
)
3907 struct isl_tab
*tab
;
3908 struct isl_map
*result
= NULL
;
3909 struct isl_sol_map
*sol_map
= NULL
;
3910 struct isl_context
*context
;
3911 struct isl_basic_map
*eq
;
3918 isl_assert(bmap
->ctx
,
3919 isl_basic_map_compatible_domain(bmap
, dom
), goto error
);
3921 eq
= isl_basic_map_copy(bmap
);
3922 eq
= isl_basic_map_intersect_domain(eq
, isl_basic_set_copy(dom
));
3923 eq
= isl_basic_map_affine_hull(eq
);
3924 bmap
= isl_basic_map_intersect(bmap
, eq
);
3927 dom
= isl_basic_set_order_divs(dom
);
3928 bmap
= align_context_divs(bmap
, dom
);
3930 sol_map
= sol_map_init(bmap
, dom
, !!empty
, max
);
3934 context
= sol_map
->sol
.context
;
3935 if (isl_basic_set_fast_is_empty(context
->op
->peek_basic_set(context
)))
3937 else if (isl_basic_map_fast_is_empty(bmap
))
3938 sol_map_add_empty(sol_map
,
3939 isl_basic_set_dup(context
->op
->peek_basic_set(context
)));
3941 tab
= tab_for_lexmin(bmap
,
3942 context
->op
->peek_basic_set(context
), 1, max
);
3943 tab
= context
->op
->detect_nonnegative_parameters(context
, tab
);
3944 sol_map_find_solutions(sol_map
, tab
);
3946 if (sol_map
->sol
.error
)
3949 result
= isl_map_copy(sol_map
->map
);
3951 *empty
= isl_set_copy(sol_map
->empty
);
3952 sol_free(&sol_map
->sol
);
3953 isl_basic_map_free(bmap
);
3956 sol_free(&sol_map
->sol
);
3957 isl_basic_map_free(bmap
);
3961 struct isl_sol_for
{
3963 int (*fn
)(__isl_take isl_basic_set
*dom
,
3964 __isl_take isl_mat
*map
, void *user
);
3968 static void sol_for_free(struct isl_sol_for
*sol_for
)
3970 if (sol_for
->sol
.context
)
3971 sol_for
->sol
.context
->op
->free(sol_for
->sol
.context
);
3975 static void sol_for_free_wrap(struct isl_sol
*sol
)
3977 sol_for_free((struct isl_sol_for
*)sol
);
3980 /* Add the solution identified by the tableau and the context tableau.
3982 * See documentation of sol_add for more details.
3984 * Instead of constructing a basic map, this function calls a user
3985 * defined function with the current context as a basic set and
3986 * an affine matrix reprenting the relation between the input and output.
3987 * The number of rows in this matrix is equal to one plus the number
3988 * of output variables. The number of columns is equal to one plus
3989 * the total dimension of the context, i.e., the number of parameters,
3990 * input variables and divs. Since some of the columns in the matrix
3991 * may refer to the divs, the basic set is not simplified.
3992 * (Simplification may reorder or remove divs.)
3994 static void sol_for_add(struct isl_sol_for
*sol
,
3995 struct isl_basic_set
*dom
, struct isl_mat
*M
)
3997 if (sol
->sol
.error
|| !dom
|| !M
)
4000 dom
= isl_basic_set_simplify(dom
);
4001 dom
= isl_basic_set_finalize(dom
);
4003 if (sol
->fn(isl_basic_set_copy(dom
), isl_mat_copy(M
), sol
->user
) < 0)
4006 isl_basic_set_free(dom
);
4010 isl_basic_set_free(dom
);
4015 static void sol_for_add_wrap(struct isl_sol
*sol
,
4016 struct isl_basic_set
*dom
, struct isl_mat
*M
)
4018 sol_for_add((struct isl_sol_for
*)sol
, dom
, M
);
4021 static struct isl_sol_for
*sol_for_init(struct isl_basic_map
*bmap
, int max
,
4022 int (*fn
)(__isl_take isl_basic_set
*dom
, __isl_take isl_mat
*map
,
4026 struct isl_sol_for
*sol_for
= NULL
;
4027 struct isl_dim
*dom_dim
;
4028 struct isl_basic_set
*dom
= NULL
;
4030 sol_for
= isl_calloc_type(bset
->ctx
, struct isl_sol_for
);
4034 dom_dim
= isl_dim_domain(isl_dim_copy(bmap
->dim
));
4035 dom
= isl_basic_set_universe(dom_dim
);
4037 sol_for
->sol
.rational
= ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
4038 sol_for
->sol
.dec_level
.callback
.run
= &sol_dec_level_wrap
;
4039 sol_for
->sol
.dec_level
.sol
= &sol_for
->sol
;
4041 sol_for
->user
= user
;
4042 sol_for
->sol
.max
= max
;
4043 sol_for
->sol
.n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4044 sol_for
->sol
.add
= &sol_for_add_wrap
;
4045 sol_for
->sol
.add_empty
= NULL
;
4046 sol_for
->sol
.free
= &sol_for_free_wrap
;
4048 sol_for
->sol
.context
= isl_context_alloc(dom
);
4049 if (!sol_for
->sol
.context
)
4052 isl_basic_set_free(dom
);
4055 isl_basic_set_free(dom
);
4056 sol_for_free(sol_for
);
4060 static void sol_for_find_solutions(struct isl_sol_for
*sol_for
,
4061 struct isl_tab
*tab
)
4063 find_solutions_main(&sol_for
->sol
, tab
);
4066 int isl_basic_map_foreach_lexopt(__isl_keep isl_basic_map
*bmap
, int max
,
4067 int (*fn
)(__isl_take isl_basic_set
*dom
, __isl_take isl_mat
*map
,
4071 struct isl_sol_for
*sol_for
= NULL
;
4073 bmap
= isl_basic_map_copy(bmap
);
4077 bmap
= isl_basic_map_detect_equalities(bmap
);
4078 sol_for
= sol_for_init(bmap
, max
, fn
, user
);
4080 if (isl_basic_map_fast_is_empty(bmap
))
4083 struct isl_tab
*tab
;
4084 struct isl_context
*context
= sol_for
->sol
.context
;
4085 tab
= tab_for_lexmin(bmap
,
4086 context
->op
->peek_basic_set(context
), 1, max
);
4087 tab
= context
->op
->detect_nonnegative_parameters(context
, tab
);
4088 sol_for_find_solutions(sol_for
, tab
);
4089 if (sol_for
->sol
.error
)
4093 sol_free(&sol_for
->sol
);
4094 isl_basic_map_free(bmap
);
4097 sol_free(&sol_for
->sol
);
4098 isl_basic_map_free(bmap
);
4102 int isl_basic_map_foreach_lexmin(__isl_keep isl_basic_map
*bmap
,
4103 int (*fn
)(__isl_take isl_basic_set
*dom
, __isl_take isl_mat
*map
,
4107 return isl_basic_map_foreach_lexopt(bmap
, 0, fn
, user
);
4110 int isl_basic_map_foreach_lexmax(__isl_keep isl_basic_map
*bmap
,
4111 int (*fn
)(__isl_take isl_basic_set
*dom
, __isl_take isl_mat
*map
,
4115 return isl_basic_map_foreach_lexopt(bmap
, 1, fn
, user
);