2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
5 * Use of this software is governed by the MIT license
7 * Written by Sven Verdoolaege, K.U.Leuven, Departement
8 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
10 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
13 #include <isl_ctx_private.h>
14 #include "isl_map_private.h"
17 #include "isl_sample.h"
18 #include <isl_mat_private.h>
19 #include <isl_aff_private.h>
20 #include <isl_options_private.h>
21 #include <isl_config.h>
24 * The implementation of parametric integer linear programming in this file
25 * was inspired by the paper "Parametric Integer Programming" and the
26 * report "Solving systems of affine (in)equalities" by Paul Feautrier
29 * The strategy used for obtaining a feasible solution is different
30 * from the one used in isl_tab.c. In particular, in isl_tab.c,
31 * upon finding a constraint that is not yet satisfied, we pivot
32 * in a row that increases the constant term of the row holding the
33 * constraint, making sure the sample solution remains feasible
34 * for all the constraints it already satisfied.
35 * Here, we always pivot in the row holding the constraint,
36 * choosing a column that induces the lexicographically smallest
37 * increment to the sample solution.
39 * By starting out from a sample value that is lexicographically
40 * smaller than any integer point in the problem space, the first
41 * feasible integer sample point we find will also be the lexicographically
42 * smallest. If all variables can be assumed to be non-negative,
43 * then the initial sample value may be chosen equal to zero.
44 * However, we will not make this assumption. Instead, we apply
45 * the "big parameter" trick. Any variable x is then not directly
46 * used in the tableau, but instead it is represented by another
47 * variable x' = M + x, where M is an arbitrarily large (positive)
48 * value. x' is therefore always non-negative, whatever the value of x.
49 * Taking as initial sample value x' = 0 corresponds to x = -M,
50 * which is always smaller than any possible value of x.
52 * The big parameter trick is used in the main tableau and
53 * also in the context tableau if isl_context_lex is used.
54 * In this case, each tableaus has its own big parameter.
55 * Before doing any real work, we check if all the parameters
56 * happen to be non-negative. If so, we drop the column corresponding
57 * to M from the initial context tableau.
58 * If isl_context_gbr is used, then the big parameter trick is only
59 * used in the main tableau.
63 struct isl_context_op
{
64 /* detect nonnegative parameters in context and mark them in tab */
65 struct isl_tab
*(*detect_nonnegative_parameters
)(
66 struct isl_context
*context
, struct isl_tab
*tab
);
67 /* return temporary reference to basic set representation of context */
68 struct isl_basic_set
*(*peek_basic_set
)(struct isl_context
*context
);
69 /* return temporary reference to tableau representation of context */
70 struct isl_tab
*(*peek_tab
)(struct isl_context
*context
);
71 /* add equality; check is 1 if eq may not be valid;
72 * update is 1 if we may want to call ineq_sign on context later.
74 void (*add_eq
)(struct isl_context
*context
, isl_int
*eq
,
75 int check
, int update
);
76 /* add inequality; check is 1 if ineq may not be valid;
77 * update is 1 if we may want to call ineq_sign on context later.
79 void (*add_ineq
)(struct isl_context
*context
, isl_int
*ineq
,
80 int check
, int update
);
81 /* check sign of ineq based on previous information.
82 * strict is 1 if saturation should be treated as a positive sign.
84 enum isl_tab_row_sign (*ineq_sign
)(struct isl_context
*context
,
85 isl_int
*ineq
, int strict
);
86 /* check if inequality maintains feasibility */
87 int (*test_ineq
)(struct isl_context
*context
, isl_int
*ineq
);
88 /* return index of a div that corresponds to "div" */
89 int (*get_div
)(struct isl_context
*context
, struct isl_tab
*tab
,
91 /* add div "div" to context and return non-negativity */
92 int (*add_div
)(struct isl_context
*context
, struct isl_vec
*div
);
93 int (*detect_equalities
)(struct isl_context
*context
,
95 /* return row index of "best" split */
96 int (*best_split
)(struct isl_context
*context
, struct isl_tab
*tab
);
97 /* check if context has already been determined to be empty */
98 int (*is_empty
)(struct isl_context
*context
);
99 /* check if context is still usable */
100 int (*is_ok
)(struct isl_context
*context
);
101 /* save a copy/snapshot of context */
102 void *(*save
)(struct isl_context
*context
);
103 /* restore saved context */
104 void (*restore
)(struct isl_context
*context
, void *);
105 /* discard saved context */
106 void (*discard
)(void *);
107 /* invalidate context */
108 void (*invalidate
)(struct isl_context
*context
);
110 void (*free
)(struct isl_context
*context
);
114 struct isl_context_op
*op
;
117 struct isl_context_lex
{
118 struct isl_context context
;
122 struct isl_partial_sol
{
124 struct isl_basic_set
*dom
;
127 struct isl_partial_sol
*next
;
131 struct isl_sol_callback
{
132 struct isl_tab_callback callback
;
136 /* isl_sol is an interface for constructing a solution to
137 * a parametric integer linear programming problem.
138 * Every time the algorithm reaches a state where a solution
139 * can be read off from the tableau (including cases where the tableau
140 * is empty), the function "add" is called on the isl_sol passed
141 * to find_solutions_main.
143 * The context tableau is owned by isl_sol and is updated incrementally.
145 * There are currently two implementations of this interface,
146 * isl_sol_map, which simply collects the solutions in an isl_map
147 * and (optionally) the parts of the context where there is no solution
149 * isl_sol_for, which calls a user-defined function for each part of
158 struct isl_context
*context
;
159 struct isl_partial_sol
*partial
;
160 void (*add
)(struct isl_sol
*sol
,
161 struct isl_basic_set
*dom
, struct isl_mat
*M
);
162 void (*add_empty
)(struct isl_sol
*sol
, struct isl_basic_set
*bset
);
163 void (*free
)(struct isl_sol
*sol
);
164 struct isl_sol_callback dec_level
;
167 static void sol_free(struct isl_sol
*sol
)
169 struct isl_partial_sol
*partial
, *next
;
172 for (partial
= sol
->partial
; partial
; partial
= next
) {
173 next
= partial
->next
;
174 isl_basic_set_free(partial
->dom
);
175 isl_mat_free(partial
->M
);
181 /* Push a partial solution represented by a domain and mapping M
182 * onto the stack of partial solutions.
184 static void sol_push_sol(struct isl_sol
*sol
,
185 struct isl_basic_set
*dom
, struct isl_mat
*M
)
187 struct isl_partial_sol
*partial
;
189 if (sol
->error
|| !dom
)
192 partial
= isl_alloc_type(dom
->ctx
, struct isl_partial_sol
);
196 partial
->level
= sol
->level
;
199 partial
->next
= sol
->partial
;
201 sol
->partial
= partial
;
205 isl_basic_set_free(dom
);
210 /* Pop one partial solution from the partial solution stack and
211 * pass it on to sol->add or sol->add_empty.
213 static void sol_pop_one(struct isl_sol
*sol
)
215 struct isl_partial_sol
*partial
;
217 partial
= sol
->partial
;
218 sol
->partial
= partial
->next
;
221 sol
->add(sol
, partial
->dom
, partial
->M
);
223 sol
->add_empty(sol
, partial
->dom
);
227 /* Return a fresh copy of the domain represented by the context tableau.
229 static struct isl_basic_set
*sol_domain(struct isl_sol
*sol
)
231 struct isl_basic_set
*bset
;
236 bset
= isl_basic_set_dup(sol
->context
->op
->peek_basic_set(sol
->context
));
237 bset
= isl_basic_set_update_from_tab(bset
,
238 sol
->context
->op
->peek_tab(sol
->context
));
243 /* Check whether two partial solutions have the same mapping, where n_div
244 * is the number of divs that the two partial solutions have in common.
246 static int same_solution(struct isl_partial_sol
*s1
, struct isl_partial_sol
*s2
,
252 if (!s1
->M
!= !s2
->M
)
257 dim
= isl_basic_set_total_dim(s1
->dom
) - s1
->dom
->n_div
;
259 for (i
= 0; i
< s1
->M
->n_row
; ++i
) {
260 if (isl_seq_first_non_zero(s1
->M
->row
[i
]+1+dim
+n_div
,
261 s1
->M
->n_col
-1-dim
-n_div
) != -1)
263 if (isl_seq_first_non_zero(s2
->M
->row
[i
]+1+dim
+n_div
,
264 s2
->M
->n_col
-1-dim
-n_div
) != -1)
266 if (!isl_seq_eq(s1
->M
->row
[i
], s2
->M
->row
[i
], 1+dim
+n_div
))
272 /* Pop all solutions from the partial solution stack that were pushed onto
273 * the stack at levels that are deeper than the current level.
274 * If the two topmost elements on the stack have the same level
275 * and represent the same solution, then their domains are combined.
276 * This combined domain is the same as the current context domain
277 * as sol_pop is called each time we move back to a higher level.
279 static void sol_pop(struct isl_sol
*sol
)
281 struct isl_partial_sol
*partial
;
287 if (sol
->level
== 0) {
288 for (partial
= sol
->partial
; partial
; partial
= sol
->partial
)
293 partial
= sol
->partial
;
297 if (partial
->level
<= sol
->level
)
300 if (partial
->next
&& partial
->next
->level
== partial
->level
) {
301 n_div
= isl_basic_set_dim(
302 sol
->context
->op
->peek_basic_set(sol
->context
),
305 if (!same_solution(partial
, partial
->next
, n_div
)) {
309 struct isl_basic_set
*bset
;
311 bset
= sol_domain(sol
);
315 isl_basic_set_free(partial
->next
->dom
);
316 partial
->next
->dom
= bset
;
317 partial
->next
->level
= sol
->level
;
319 sol
->partial
= partial
->next
;
320 isl_basic_set_free(partial
->dom
);
321 isl_mat_free(partial
->M
);
328 error
: sol
->error
= 1;
331 static void sol_dec_level(struct isl_sol
*sol
)
341 static int sol_dec_level_wrap(struct isl_tab_callback
*cb
)
343 struct isl_sol_callback
*callback
= (struct isl_sol_callback
*)cb
;
345 sol_dec_level(callback
->sol
);
347 return callback
->sol
->error
? -1 : 0;
350 /* Move down to next level and push callback onto context tableau
351 * to decrease the level again when it gets rolled back across
352 * the current state. That is, dec_level will be called with
353 * the context tableau in the same state as it is when inc_level
356 static void sol_inc_level(struct isl_sol
*sol
)
364 tab
= sol
->context
->op
->peek_tab(sol
->context
);
365 if (isl_tab_push_callback(tab
, &sol
->dec_level
.callback
) < 0)
369 static void scale_rows(struct isl_mat
*mat
, isl_int m
, int n_row
)
373 if (isl_int_is_one(m
))
376 for (i
= 0; i
< n_row
; ++i
)
377 isl_seq_scale(mat
->row
[i
], mat
->row
[i
], m
, mat
->n_col
);
380 /* Add the solution identified by the tableau and the context tableau.
382 * The layout of the variables is as follows.
383 * tab->n_var is equal to the total number of variables in the input
384 * map (including divs that were copied from the context)
385 * + the number of extra divs constructed
386 * Of these, the first tab->n_param and the last tab->n_div variables
387 * correspond to the variables in the context, i.e.,
388 * tab->n_param + tab->n_div = context_tab->n_var
389 * tab->n_param is equal to the number of parameters and input
390 * dimensions in the input map
391 * tab->n_div is equal to the number of divs in the context
393 * If there is no solution, then call add_empty with a basic set
394 * that corresponds to the context tableau. (If add_empty is NULL,
397 * If there is a solution, then first construct a matrix that maps
398 * all dimensions of the context to the output variables, i.e.,
399 * the output dimensions in the input map.
400 * The divs in the input map (if any) that do not correspond to any
401 * div in the context do not appear in the solution.
402 * The algorithm will make sure that they have an integer value,
403 * but these values themselves are of no interest.
404 * We have to be careful not to drop or rearrange any divs in the
405 * context because that would change the meaning of the matrix.
407 * To extract the value of the output variables, it should be noted
408 * that we always use a big parameter M in the main tableau and so
409 * the variable stored in this tableau is not an output variable x itself, but
410 * x' = M + x (in case of minimization)
412 * x' = M - x (in case of maximization)
413 * If x' appears in a column, then its optimal value is zero,
414 * which means that the optimal value of x is an unbounded number
415 * (-M for minimization and M for maximization).
416 * We currently assume that the output dimensions in the original map
417 * are bounded, so this cannot occur.
418 * Similarly, when x' appears in a row, then the coefficient of M in that
419 * row is necessarily 1.
420 * If the row in the tableau represents
421 * d x' = c + d M + e(y)
422 * then, in case of minimization, the corresponding row in the matrix
425 * with a d = m, the (updated) common denominator of the matrix.
426 * In case of maximization, the row will be
429 static void sol_add(struct isl_sol
*sol
, struct isl_tab
*tab
)
431 struct isl_basic_set
*bset
= NULL
;
432 struct isl_mat
*mat
= NULL
;
437 if (sol
->error
|| !tab
)
440 if (tab
->empty
&& !sol
->add_empty
)
442 if (sol
->context
->op
->is_empty(sol
->context
))
445 bset
= sol_domain(sol
);
448 sol_push_sol(sol
, bset
, NULL
);
454 mat
= isl_mat_alloc(tab
->mat
->ctx
, 1 + sol
->n_out
,
455 1 + tab
->n_param
+ tab
->n_div
);
461 isl_seq_clr(mat
->row
[0] + 1, mat
->n_col
- 1);
462 isl_int_set_si(mat
->row
[0][0], 1);
463 for (row
= 0; row
< sol
->n_out
; ++row
) {
464 int i
= tab
->n_param
+ row
;
467 isl_seq_clr(mat
->row
[1 + row
], mat
->n_col
);
468 if (!tab
->var
[i
].is_row
) {
470 isl_die(mat
->ctx
, isl_error_invalid
,
471 "unbounded optimum", goto error2
);
475 r
= tab
->var
[i
].index
;
477 isl_int_ne(tab
->mat
->row
[r
][2], tab
->mat
->row
[r
][0]))
478 isl_die(mat
->ctx
, isl_error_invalid
,
479 "unbounded optimum", goto error2
);
480 isl_int_gcd(m
, mat
->row
[0][0], tab
->mat
->row
[r
][0]);
481 isl_int_divexact(m
, tab
->mat
->row
[r
][0], m
);
482 scale_rows(mat
, m
, 1 + row
);
483 isl_int_divexact(m
, mat
->row
[0][0], tab
->mat
->row
[r
][0]);
484 isl_int_mul(mat
->row
[1 + row
][0], m
, tab
->mat
->row
[r
][1]);
485 for (j
= 0; j
< tab
->n_param
; ++j
) {
487 if (tab
->var
[j
].is_row
)
489 col
= tab
->var
[j
].index
;
490 isl_int_mul(mat
->row
[1 + row
][1 + j
], m
,
491 tab
->mat
->row
[r
][off
+ col
]);
493 for (j
= 0; j
< tab
->n_div
; ++j
) {
495 if (tab
->var
[tab
->n_var
- tab
->n_div
+j
].is_row
)
497 col
= tab
->var
[tab
->n_var
- tab
->n_div
+j
].index
;
498 isl_int_mul(mat
->row
[1 + row
][1 + tab
->n_param
+ j
], m
,
499 tab
->mat
->row
[r
][off
+ col
]);
502 isl_seq_neg(mat
->row
[1 + row
], mat
->row
[1 + row
],
508 sol_push_sol(sol
, bset
, mat
);
513 isl_basic_set_free(bset
);
521 struct isl_set
*empty
;
524 static void sol_map_free(struct isl_sol_map
*sol_map
)
528 if (sol_map
->sol
.context
)
529 sol_map
->sol
.context
->op
->free(sol_map
->sol
.context
);
530 isl_map_free(sol_map
->map
);
531 isl_set_free(sol_map
->empty
);
535 static void sol_map_free_wrap(struct isl_sol
*sol
)
537 sol_map_free((struct isl_sol_map
*)sol
);
540 /* This function is called for parts of the context where there is
541 * no solution, with "bset" corresponding to the context tableau.
542 * Simply add the basic set to the set "empty".
544 static void sol_map_add_empty(struct isl_sol_map
*sol
,
545 struct isl_basic_set
*bset
)
549 isl_assert(bset
->ctx
, sol
->empty
, goto error
);
551 sol
->empty
= isl_set_grow(sol
->empty
, 1);
552 bset
= isl_basic_set_simplify(bset
);
553 bset
= isl_basic_set_finalize(bset
);
554 sol
->empty
= isl_set_add_basic_set(sol
->empty
, isl_basic_set_copy(bset
));
557 isl_basic_set_free(bset
);
560 isl_basic_set_free(bset
);
564 static void sol_map_add_empty_wrap(struct isl_sol
*sol
,
565 struct isl_basic_set
*bset
)
567 sol_map_add_empty((struct isl_sol_map
*)sol
, bset
);
570 /* Given a basic map "dom" that represents the context and an affine
571 * matrix "M" that maps the dimensions of the context to the
572 * output variables, construct a basic map with the same parameters
573 * and divs as the context, the dimensions of the context as input
574 * dimensions and a number of output dimensions that is equal to
575 * the number of output dimensions in the input map.
577 * The constraints and divs of the context are simply copied
578 * from "dom". For each row
582 * is added, with d the common denominator of M.
584 static void sol_map_add(struct isl_sol_map
*sol
,
585 struct isl_basic_set
*dom
, struct isl_mat
*M
)
588 struct isl_basic_map
*bmap
= NULL
;
596 if (sol
->sol
.error
|| !dom
|| !M
)
599 n_out
= sol
->sol
.n_out
;
600 n_eq
= dom
->n_eq
+ n_out
;
601 n_ineq
= dom
->n_ineq
;
603 nparam
= isl_basic_set_total_dim(dom
) - n_div
;
604 total
= isl_map_dim(sol
->map
, isl_dim_all
);
605 bmap
= isl_basic_map_alloc_space(isl_map_get_space(sol
->map
),
606 n_div
, n_eq
, 2 * n_div
+ n_ineq
);
609 if (sol
->sol
.rational
)
610 ISL_F_SET(bmap
, ISL_BASIC_MAP_RATIONAL
);
611 for (i
= 0; i
< dom
->n_div
; ++i
) {
612 int k
= isl_basic_map_alloc_div(bmap
);
615 isl_seq_cpy(bmap
->div
[k
], dom
->div
[i
], 1 + 1 + nparam
);
616 isl_seq_clr(bmap
->div
[k
] + 1 + 1 + nparam
, total
- nparam
);
617 isl_seq_cpy(bmap
->div
[k
] + 1 + 1 + total
,
618 dom
->div
[i
] + 1 + 1 + nparam
, i
);
620 for (i
= 0; i
< dom
->n_eq
; ++i
) {
621 int k
= isl_basic_map_alloc_equality(bmap
);
624 isl_seq_cpy(bmap
->eq
[k
], dom
->eq
[i
], 1 + nparam
);
625 isl_seq_clr(bmap
->eq
[k
] + 1 + nparam
, total
- nparam
);
626 isl_seq_cpy(bmap
->eq
[k
] + 1 + total
,
627 dom
->eq
[i
] + 1 + nparam
, n_div
);
629 for (i
= 0; i
< dom
->n_ineq
; ++i
) {
630 int k
= isl_basic_map_alloc_inequality(bmap
);
633 isl_seq_cpy(bmap
->ineq
[k
], dom
->ineq
[i
], 1 + nparam
);
634 isl_seq_clr(bmap
->ineq
[k
] + 1 + nparam
, total
- nparam
);
635 isl_seq_cpy(bmap
->ineq
[k
] + 1 + total
,
636 dom
->ineq
[i
] + 1 + nparam
, n_div
);
638 for (i
= 0; i
< M
->n_row
- 1; ++i
) {
639 int k
= isl_basic_map_alloc_equality(bmap
);
642 isl_seq_cpy(bmap
->eq
[k
], M
->row
[1 + i
], 1 + nparam
);
643 isl_seq_clr(bmap
->eq
[k
] + 1 + nparam
, n_out
);
644 isl_int_neg(bmap
->eq
[k
][1 + nparam
+ i
], M
->row
[0][0]);
645 isl_seq_cpy(bmap
->eq
[k
] + 1 + nparam
+ n_out
,
646 M
->row
[1 + i
] + 1 + nparam
, n_div
);
648 bmap
= isl_basic_map_simplify(bmap
);
649 bmap
= isl_basic_map_finalize(bmap
);
650 sol
->map
= isl_map_grow(sol
->map
, 1);
651 sol
->map
= isl_map_add_basic_map(sol
->map
, bmap
);
652 isl_basic_set_free(dom
);
658 isl_basic_set_free(dom
);
660 isl_basic_map_free(bmap
);
664 static void sol_map_add_wrap(struct isl_sol
*sol
,
665 struct isl_basic_set
*dom
, struct isl_mat
*M
)
667 sol_map_add((struct isl_sol_map
*)sol
, dom
, M
);
671 /* Store the "parametric constant" of row "row" of tableau "tab" in "line",
672 * i.e., the constant term and the coefficients of all variables that
673 * appear in the context tableau.
674 * Note that the coefficient of the big parameter M is NOT copied.
675 * The context tableau may not have a big parameter and even when it
676 * does, it is a different big parameter.
678 static void get_row_parameter_line(struct isl_tab
*tab
, int row
, isl_int
*line
)
681 unsigned off
= 2 + tab
->M
;
683 isl_int_set(line
[0], tab
->mat
->row
[row
][1]);
684 for (i
= 0; i
< tab
->n_param
; ++i
) {
685 if (tab
->var
[i
].is_row
)
686 isl_int_set_si(line
[1 + i
], 0);
688 int col
= tab
->var
[i
].index
;
689 isl_int_set(line
[1 + i
], tab
->mat
->row
[row
][off
+ col
]);
692 for (i
= 0; i
< tab
->n_div
; ++i
) {
693 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
694 isl_int_set_si(line
[1 + tab
->n_param
+ i
], 0);
696 int col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
697 isl_int_set(line
[1 + tab
->n_param
+ i
],
698 tab
->mat
->row
[row
][off
+ col
]);
703 /* Check if rows "row1" and "row2" have identical "parametric constants",
704 * as explained above.
705 * In this case, we also insist that the coefficients of the big parameter
706 * be the same as the values of the constants will only be the same
707 * if these coefficients are also the same.
709 static int identical_parameter_line(struct isl_tab
*tab
, int row1
, int row2
)
712 unsigned off
= 2 + tab
->M
;
714 if (isl_int_ne(tab
->mat
->row
[row1
][1], tab
->mat
->row
[row2
][1]))
717 if (tab
->M
&& isl_int_ne(tab
->mat
->row
[row1
][2],
718 tab
->mat
->row
[row2
][2]))
721 for (i
= 0; i
< tab
->n_param
+ tab
->n_div
; ++i
) {
722 int pos
= i
< tab
->n_param
? i
:
723 tab
->n_var
- tab
->n_div
+ i
- tab
->n_param
;
726 if (tab
->var
[pos
].is_row
)
728 col
= tab
->var
[pos
].index
;
729 if (isl_int_ne(tab
->mat
->row
[row1
][off
+ col
],
730 tab
->mat
->row
[row2
][off
+ col
]))
736 /* Return an inequality that expresses that the "parametric constant"
737 * should be non-negative.
738 * This function is only called when the coefficient of the big parameter
741 static struct isl_vec
*get_row_parameter_ineq(struct isl_tab
*tab
, int row
)
743 struct isl_vec
*ineq
;
745 ineq
= isl_vec_alloc(tab
->mat
->ctx
, 1 + tab
->n_param
+ tab
->n_div
);
749 get_row_parameter_line(tab
, row
, ineq
->el
);
751 ineq
= isl_vec_normalize(ineq
);
756 /* Normalize a div expression of the form
758 * [(g*f(x) + c)/(g * m)]
760 * with c the constant term and f(x) the remaining coefficients, to
764 static void normalize_div(__isl_keep isl_vec
*div
)
766 isl_ctx
*ctx
= isl_vec_get_ctx(div
);
767 int len
= div
->size
- 2;
769 isl_seq_gcd(div
->el
+ 2, len
, &ctx
->normalize_gcd
);
770 isl_int_gcd(ctx
->normalize_gcd
, ctx
->normalize_gcd
, div
->el
[0]);
772 if (isl_int_is_one(ctx
->normalize_gcd
))
775 isl_int_divexact(div
->el
[0], div
->el
[0], ctx
->normalize_gcd
);
776 isl_int_fdiv_q(div
->el
[1], div
->el
[1], ctx
->normalize_gcd
);
777 isl_seq_scale_down(div
->el
+ 2, div
->el
+ 2, ctx
->normalize_gcd
, len
);
780 /* Return a integer division for use in a parametric cut based on the given row.
781 * In particular, let the parametric constant of the row be
785 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
786 * The div returned is equal to
788 * floor(\sum_i {-a_i} y_i) = floor((\sum_i (-a_i mod d) y_i)/d)
790 static struct isl_vec
*get_row_parameter_div(struct isl_tab
*tab
, int row
)
794 div
= isl_vec_alloc(tab
->mat
->ctx
, 1 + 1 + tab
->n_param
+ tab
->n_div
);
798 isl_int_set(div
->el
[0], tab
->mat
->row
[row
][0]);
799 get_row_parameter_line(tab
, row
, div
->el
+ 1);
800 isl_seq_neg(div
->el
+ 1, div
->el
+ 1, div
->size
- 1);
802 isl_seq_fdiv_r(div
->el
+ 1, div
->el
+ 1, div
->el
[0], div
->size
- 1);
807 /* Return a integer division for use in transferring an integrality constraint
809 * In particular, let the parametric constant of the row be
813 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
814 * The the returned div is equal to
816 * floor(\sum_i {a_i} y_i) = floor((\sum_i (a_i mod d) y_i)/d)
818 static struct isl_vec
*get_row_split_div(struct isl_tab
*tab
, int row
)
822 div
= isl_vec_alloc(tab
->mat
->ctx
, 1 + 1 + tab
->n_param
+ tab
->n_div
);
826 isl_int_set(div
->el
[0], tab
->mat
->row
[row
][0]);
827 get_row_parameter_line(tab
, row
, div
->el
+ 1);
829 isl_seq_fdiv_r(div
->el
+ 1, div
->el
+ 1, div
->el
[0], div
->size
- 1);
834 /* Construct and return an inequality that expresses an upper bound
836 * In particular, if the div is given by
840 * then the inequality expresses
844 static struct isl_vec
*ineq_for_div(struct isl_basic_set
*bset
, unsigned div
)
848 struct isl_vec
*ineq
;
853 total
= isl_basic_set_total_dim(bset
);
854 div_pos
= 1 + total
- bset
->n_div
+ div
;
856 ineq
= isl_vec_alloc(bset
->ctx
, 1 + total
);
860 isl_seq_cpy(ineq
->el
, bset
->div
[div
] + 1, 1 + total
);
861 isl_int_neg(ineq
->el
[div_pos
], bset
->div
[div
][0]);
865 /* Given a row in the tableau and a div that was created
866 * using get_row_split_div and that has been constrained to equality, i.e.,
868 * d = floor(\sum_i {a_i} y_i) = \sum_i {a_i} y_i
870 * replace the expression "\sum_i {a_i} y_i" in the row by d,
871 * i.e., we subtract "\sum_i {a_i} y_i" and add 1 d.
872 * The coefficients of the non-parameters in the tableau have been
873 * verified to be integral. We can therefore simply replace coefficient b
874 * by floor(b). For the coefficients of the parameters we have
875 * floor(a_i) = a_i - {a_i}, while for the other coefficients, we have
878 static struct isl_tab
*set_row_cst_to_div(struct isl_tab
*tab
, int row
, int div
)
880 isl_seq_fdiv_q(tab
->mat
->row
[row
] + 1, tab
->mat
->row
[row
] + 1,
881 tab
->mat
->row
[row
][0], 1 + tab
->M
+ tab
->n_col
);
883 isl_int_set_si(tab
->mat
->row
[row
][0], 1);
885 if (tab
->var
[tab
->n_var
- tab
->n_div
+ div
].is_row
) {
886 int drow
= tab
->var
[tab
->n_var
- tab
->n_div
+ div
].index
;
888 isl_assert(tab
->mat
->ctx
,
889 isl_int_is_one(tab
->mat
->row
[drow
][0]), goto error
);
890 isl_seq_combine(tab
->mat
->row
[row
] + 1,
891 tab
->mat
->ctx
->one
, tab
->mat
->row
[row
] + 1,
892 tab
->mat
->ctx
->one
, tab
->mat
->row
[drow
] + 1,
893 1 + tab
->M
+ tab
->n_col
);
895 int dcol
= tab
->var
[tab
->n_var
- tab
->n_div
+ div
].index
;
897 isl_int_add_ui(tab
->mat
->row
[row
][2 + tab
->M
+ dcol
],
898 tab
->mat
->row
[row
][2 + tab
->M
+ dcol
], 1);
907 /* Check if the (parametric) constant of the given row is obviously
908 * negative, meaning that we don't need to consult the context tableau.
909 * If there is a big parameter and its coefficient is non-zero,
910 * then this coefficient determines the outcome.
911 * Otherwise, we check whether the constant is negative and
912 * all non-zero coefficients of parameters are negative and
913 * belong to non-negative parameters.
915 static int is_obviously_neg(struct isl_tab
*tab
, int row
)
919 unsigned off
= 2 + tab
->M
;
922 if (isl_int_is_pos(tab
->mat
->row
[row
][2]))
924 if (isl_int_is_neg(tab
->mat
->row
[row
][2]))
928 if (isl_int_is_nonneg(tab
->mat
->row
[row
][1]))
930 for (i
= 0; i
< tab
->n_param
; ++i
) {
931 /* Eliminated parameter */
932 if (tab
->var
[i
].is_row
)
934 col
= tab
->var
[i
].index
;
935 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
937 if (!tab
->var
[i
].is_nonneg
)
939 if (isl_int_is_pos(tab
->mat
->row
[row
][off
+ col
]))
942 for (i
= 0; i
< tab
->n_div
; ++i
) {
943 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
945 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
946 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
948 if (!tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_nonneg
)
950 if (isl_int_is_pos(tab
->mat
->row
[row
][off
+ col
]))
956 /* Check if the (parametric) constant of the given row is obviously
957 * non-negative, meaning that we don't need to consult the context tableau.
958 * If there is a big parameter and its coefficient is non-zero,
959 * then this coefficient determines the outcome.
960 * Otherwise, we check whether the constant is non-negative and
961 * all non-zero coefficients of parameters are positive and
962 * belong to non-negative parameters.
964 static int is_obviously_nonneg(struct isl_tab
*tab
, int row
)
968 unsigned off
= 2 + tab
->M
;
971 if (isl_int_is_pos(tab
->mat
->row
[row
][2]))
973 if (isl_int_is_neg(tab
->mat
->row
[row
][2]))
977 if (isl_int_is_neg(tab
->mat
->row
[row
][1]))
979 for (i
= 0; i
< tab
->n_param
; ++i
) {
980 /* Eliminated parameter */
981 if (tab
->var
[i
].is_row
)
983 col
= tab
->var
[i
].index
;
984 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
986 if (!tab
->var
[i
].is_nonneg
)
988 if (isl_int_is_neg(tab
->mat
->row
[row
][off
+ col
]))
991 for (i
= 0; i
< tab
->n_div
; ++i
) {
992 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
994 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
995 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
997 if (!tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_nonneg
)
999 if (isl_int_is_neg(tab
->mat
->row
[row
][off
+ col
]))
1005 /* Given a row r and two columns, return the column that would
1006 * lead to the lexicographically smallest increment in the sample
1007 * solution when leaving the basis in favor of the row.
1008 * Pivoting with column c will increment the sample value by a non-negative
1009 * constant times a_{V,c}/a_{r,c}, with a_{V,c} the elements of column c
1010 * corresponding to the non-parametric variables.
1011 * If variable v appears in a column c_v, the a_{v,c} = 1 iff c = c_v,
1012 * with all other entries in this virtual row equal to zero.
1013 * If variable v appears in a row, then a_{v,c} is the element in column c
1016 * Let v be the first variable with a_{v,c1}/a_{r,c1} != a_{v,c2}/a_{r,c2}.
1017 * Then if a_{v,c1}/a_{r,c1} < a_{v,c2}/a_{r,c2}, i.e.,
1018 * a_{v,c2} a_{r,c1} - a_{v,c1} a_{r,c2} > 0, c1 results in the minimal
1019 * increment. Otherwise, it's c2.
1021 static int lexmin_col_pair(struct isl_tab
*tab
,
1022 int row
, int col1
, int col2
, isl_int tmp
)
1027 tr
= tab
->mat
->row
[row
] + 2 + tab
->M
;
1029 for (i
= tab
->n_param
; i
< tab
->n_var
- tab
->n_div
; ++i
) {
1033 if (!tab
->var
[i
].is_row
) {
1034 if (tab
->var
[i
].index
== col1
)
1036 if (tab
->var
[i
].index
== col2
)
1041 if (tab
->var
[i
].index
== row
)
1044 r
= tab
->mat
->row
[tab
->var
[i
].index
] + 2 + tab
->M
;
1045 s1
= isl_int_sgn(r
[col1
]);
1046 s2
= isl_int_sgn(r
[col2
]);
1047 if (s1
== 0 && s2
== 0)
1054 isl_int_mul(tmp
, r
[col2
], tr
[col1
]);
1055 isl_int_submul(tmp
, r
[col1
], tr
[col2
]);
1056 if (isl_int_is_pos(tmp
))
1058 if (isl_int_is_neg(tmp
))
1064 /* Given a row in the tableau, find and return the column that would
1065 * result in the lexicographically smallest, but positive, increment
1066 * in the sample point.
1067 * If there is no such column, then return tab->n_col.
1068 * If anything goes wrong, return -1.
1070 static int lexmin_pivot_col(struct isl_tab
*tab
, int row
)
1073 int col
= tab
->n_col
;
1077 tr
= tab
->mat
->row
[row
] + 2 + tab
->M
;
1081 for (j
= tab
->n_dead
; j
< tab
->n_col
; ++j
) {
1082 if (tab
->col_var
[j
] >= 0 &&
1083 (tab
->col_var
[j
] < tab
->n_param
||
1084 tab
->col_var
[j
] >= tab
->n_var
- tab
->n_div
))
1087 if (!isl_int_is_pos(tr
[j
]))
1090 if (col
== tab
->n_col
)
1093 col
= lexmin_col_pair(tab
, row
, col
, j
, tmp
);
1094 isl_assert(tab
->mat
->ctx
, col
>= 0, goto error
);
1104 /* Return the first known violated constraint, i.e., a non-negative
1105 * constraint that currently has an either obviously negative value
1106 * or a previously determined to be negative value.
1108 * If any constraint has a negative coefficient for the big parameter,
1109 * if any, then we return one of these first.
1111 static int first_neg(struct isl_tab
*tab
)
1116 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
1117 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
1119 if (!isl_int_is_neg(tab
->mat
->row
[row
][2]))
1122 tab
->row_sign
[row
] = isl_tab_row_neg
;
1125 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
1126 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
1128 if (tab
->row_sign
) {
1129 if (tab
->row_sign
[row
] == 0 &&
1130 is_obviously_neg(tab
, row
))
1131 tab
->row_sign
[row
] = isl_tab_row_neg
;
1132 if (tab
->row_sign
[row
] != isl_tab_row_neg
)
1134 } else if (!is_obviously_neg(tab
, row
))
1141 /* Check whether the invariant that all columns are lexico-positive
1142 * is satisfied. This function is not called from the current code
1143 * but is useful during debugging.
1145 static void check_lexpos(struct isl_tab
*tab
) __attribute__ ((unused
));
1146 static void check_lexpos(struct isl_tab
*tab
)
1148 unsigned off
= 2 + tab
->M
;
1153 for (col
= tab
->n_dead
; col
< tab
->n_col
; ++col
) {
1154 if (tab
->col_var
[col
] >= 0 &&
1155 (tab
->col_var
[col
] < tab
->n_param
||
1156 tab
->col_var
[col
] >= tab
->n_var
- tab
->n_div
))
1158 for (var
= tab
->n_param
; var
< tab
->n_var
- tab
->n_div
; ++var
) {
1159 if (!tab
->var
[var
].is_row
) {
1160 if (tab
->var
[var
].index
== col
)
1165 row
= tab
->var
[var
].index
;
1166 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
1168 if (isl_int_is_pos(tab
->mat
->row
[row
][off
+ col
]))
1170 fprintf(stderr
, "lexneg column %d (row %d)\n",
1173 if (var
>= tab
->n_var
- tab
->n_div
)
1174 fprintf(stderr
, "zero column %d\n", col
);
1178 /* Report to the caller that the given constraint is part of an encountered
1181 static int report_conflicting_constraint(struct isl_tab
*tab
, int con
)
1183 return tab
->conflict(con
, tab
->conflict_user
);
1186 /* Given a conflicting row in the tableau, report all constraints
1187 * involved in the row to the caller. That is, the row itself
1188 * (if it represents a constraint) and all constraint columns with
1189 * non-zero (and therefore negative) coefficients.
1191 static int report_conflict(struct isl_tab
*tab
, int row
)
1199 if (tab
->row_var
[row
] < 0 &&
1200 report_conflicting_constraint(tab
, ~tab
->row_var
[row
]) < 0)
1203 tr
= tab
->mat
->row
[row
] + 2 + tab
->M
;
1205 for (j
= tab
->n_dead
; j
< tab
->n_col
; ++j
) {
1206 if (tab
->col_var
[j
] >= 0 &&
1207 (tab
->col_var
[j
] < tab
->n_param
||
1208 tab
->col_var
[j
] >= tab
->n_var
- tab
->n_div
))
1211 if (!isl_int_is_neg(tr
[j
]))
1214 if (tab
->col_var
[j
] < 0 &&
1215 report_conflicting_constraint(tab
, ~tab
->col_var
[j
]) < 0)
1222 /* Resolve all known or obviously violated constraints through pivoting.
1223 * In particular, as long as we can find any violated constraint, we
1224 * look for a pivoting column that would result in the lexicographically
1225 * smallest increment in the sample point. If there is no such column
1226 * then the tableau is infeasible.
1228 static int restore_lexmin(struct isl_tab
*tab
) WARN_UNUSED
;
1229 static int restore_lexmin(struct isl_tab
*tab
)
1237 while ((row
= first_neg(tab
)) != -1) {
1238 col
= lexmin_pivot_col(tab
, row
);
1239 if (col
>= tab
->n_col
) {
1240 if (report_conflict(tab
, row
) < 0)
1242 if (isl_tab_mark_empty(tab
) < 0)
1248 if (isl_tab_pivot(tab
, row
, col
) < 0)
1254 /* Given a row that represents an equality, look for an appropriate
1256 * In particular, if there are any non-zero coefficients among
1257 * the non-parameter variables, then we take the last of these
1258 * variables. Eliminating this variable in terms of the other
1259 * variables and/or parameters does not influence the property
1260 * that all column in the initial tableau are lexicographically
1261 * positive. The row corresponding to the eliminated variable
1262 * will only have non-zero entries below the diagonal of the
1263 * initial tableau. That is, we transform
1269 * If there is no such non-parameter variable, then we are dealing with
1270 * pure parameter equality and we pick any parameter with coefficient 1 or -1
1271 * for elimination. This will ensure that the eliminated parameter
1272 * always has an integer value whenever all the other parameters are integral.
1273 * If there is no such parameter then we return -1.
1275 static int last_var_col_or_int_par_col(struct isl_tab
*tab
, int row
)
1277 unsigned off
= 2 + tab
->M
;
1280 for (i
= tab
->n_var
- tab
->n_div
- 1; i
>= 0 && i
>= tab
->n_param
; --i
) {
1282 if (tab
->var
[i
].is_row
)
1284 col
= tab
->var
[i
].index
;
1285 if (col
<= tab
->n_dead
)
1287 if (!isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
1290 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
1291 if (isl_int_is_one(tab
->mat
->row
[row
][off
+ i
]))
1293 if (isl_int_is_negone(tab
->mat
->row
[row
][off
+ i
]))
1299 /* Add an equality that is known to be valid to the tableau.
1300 * We first check if we can eliminate a variable or a parameter.
1301 * If not, we add the equality as two inequalities.
1302 * In this case, the equality was a pure parameter equality and there
1303 * is no need to resolve any constraint violations.
1305 static struct isl_tab
*add_lexmin_valid_eq(struct isl_tab
*tab
, isl_int
*eq
)
1312 r
= isl_tab_add_row(tab
, eq
);
1316 r
= tab
->con
[r
].index
;
1317 i
= last_var_col_or_int_par_col(tab
, r
);
1319 tab
->con
[r
].is_nonneg
= 1;
1320 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
1322 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
1323 r
= isl_tab_add_row(tab
, eq
);
1326 tab
->con
[r
].is_nonneg
= 1;
1327 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
1330 if (isl_tab_pivot(tab
, r
, i
) < 0)
1332 if (isl_tab_kill_col(tab
, i
) < 0)
1343 /* Check if the given row is a pure constant.
1345 static int is_constant(struct isl_tab
*tab
, int row
)
1347 unsigned off
= 2 + tab
->M
;
1349 return isl_seq_first_non_zero(tab
->mat
->row
[row
] + off
+ tab
->n_dead
,
1350 tab
->n_col
- tab
->n_dead
) == -1;
1353 /* Add an equality that may or may not be valid to the tableau.
1354 * If the resulting row is a pure constant, then it must be zero.
1355 * Otherwise, the resulting tableau is empty.
1357 * If the row is not a pure constant, then we add two inequalities,
1358 * each time checking that they can be satisfied.
1359 * In the end we try to use one of the two constraints to eliminate
1362 static int add_lexmin_eq(struct isl_tab
*tab
, isl_int
*eq
) WARN_UNUSED
;
1363 static int add_lexmin_eq(struct isl_tab
*tab
, isl_int
*eq
)
1367 struct isl_tab_undo
*snap
;
1371 snap
= isl_tab_snap(tab
);
1372 r1
= isl_tab_add_row(tab
, eq
);
1375 tab
->con
[r1
].is_nonneg
= 1;
1376 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r1
]) < 0)
1379 row
= tab
->con
[r1
].index
;
1380 if (is_constant(tab
, row
)) {
1381 if (!isl_int_is_zero(tab
->mat
->row
[row
][1]) ||
1382 (tab
->M
&& !isl_int_is_zero(tab
->mat
->row
[row
][2]))) {
1383 if (isl_tab_mark_empty(tab
) < 0)
1387 if (isl_tab_rollback(tab
, snap
) < 0)
1392 if (restore_lexmin(tab
) < 0)
1397 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
1399 r2
= isl_tab_add_row(tab
, eq
);
1402 tab
->con
[r2
].is_nonneg
= 1;
1403 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r2
]) < 0)
1406 if (restore_lexmin(tab
) < 0)
1411 if (!tab
->con
[r1
].is_row
) {
1412 if (isl_tab_kill_col(tab
, tab
->con
[r1
].index
) < 0)
1414 } else if (!tab
->con
[r2
].is_row
) {
1415 if (isl_tab_kill_col(tab
, tab
->con
[r2
].index
) < 0)
1420 tab
->bmap
= isl_basic_map_add_ineq(tab
->bmap
, eq
);
1421 if (isl_tab_push(tab
, isl_tab_undo_bmap_ineq
) < 0)
1423 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
1424 tab
->bmap
= isl_basic_map_add_ineq(tab
->bmap
, eq
);
1425 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
1426 if (isl_tab_push(tab
, isl_tab_undo_bmap_ineq
) < 0)
1435 /* Add an inequality to the tableau, resolving violations using
1438 static struct isl_tab
*add_lexmin_ineq(struct isl_tab
*tab
, isl_int
*ineq
)
1445 tab
->bmap
= isl_basic_map_add_ineq(tab
->bmap
, ineq
);
1446 if (isl_tab_push(tab
, isl_tab_undo_bmap_ineq
) < 0)
1451 r
= isl_tab_add_row(tab
, ineq
);
1454 tab
->con
[r
].is_nonneg
= 1;
1455 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
1457 if (isl_tab_row_is_redundant(tab
, tab
->con
[r
].index
)) {
1458 if (isl_tab_mark_redundant(tab
, tab
->con
[r
].index
) < 0)
1463 if (restore_lexmin(tab
) < 0)
1465 if (!tab
->empty
&& tab
->con
[r
].is_row
&&
1466 isl_tab_row_is_redundant(tab
, tab
->con
[r
].index
))
1467 if (isl_tab_mark_redundant(tab
, tab
->con
[r
].index
) < 0)
1475 /* Check if the coefficients of the parameters are all integral.
1477 static int integer_parameter(struct isl_tab
*tab
, int row
)
1481 unsigned off
= 2 + tab
->M
;
1483 for (i
= 0; i
< tab
->n_param
; ++i
) {
1484 /* Eliminated parameter */
1485 if (tab
->var
[i
].is_row
)
1487 col
= tab
->var
[i
].index
;
1488 if (!isl_int_is_divisible_by(tab
->mat
->row
[row
][off
+ col
],
1489 tab
->mat
->row
[row
][0]))
1492 for (i
= 0; i
< tab
->n_div
; ++i
) {
1493 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
1495 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
1496 if (!isl_int_is_divisible_by(tab
->mat
->row
[row
][off
+ col
],
1497 tab
->mat
->row
[row
][0]))
1503 /* Check if the coefficients of the non-parameter variables are all integral.
1505 static int integer_variable(struct isl_tab
*tab
, int row
)
1508 unsigned off
= 2 + tab
->M
;
1510 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
1511 if (tab
->col_var
[i
] >= 0 &&
1512 (tab
->col_var
[i
] < tab
->n_param
||
1513 tab
->col_var
[i
] >= tab
->n_var
- tab
->n_div
))
1515 if (!isl_int_is_divisible_by(tab
->mat
->row
[row
][off
+ i
],
1516 tab
->mat
->row
[row
][0]))
1522 /* Check if the constant term is integral.
1524 static int integer_constant(struct isl_tab
*tab
, int row
)
1526 return isl_int_is_divisible_by(tab
->mat
->row
[row
][1],
1527 tab
->mat
->row
[row
][0]);
1530 #define I_CST 1 << 0
1531 #define I_PAR 1 << 1
1532 #define I_VAR 1 << 2
1534 /* Check for next (non-parameter) variable after "var" (first if var == -1)
1535 * that is non-integer and therefore requires a cut and return
1536 * the index of the variable.
1537 * For parametric tableaus, there are three parts in a row,
1538 * the constant, the coefficients of the parameters and the rest.
1539 * For each part, we check whether the coefficients in that part
1540 * are all integral and if so, set the corresponding flag in *f.
1541 * If the constant and the parameter part are integral, then the
1542 * current sample value is integral and no cut is required
1543 * (irrespective of whether the variable part is integral).
1545 static int next_non_integer_var(struct isl_tab
*tab
, int var
, int *f
)
1547 var
= var
< 0 ? tab
->n_param
: var
+ 1;
1549 for (; var
< tab
->n_var
- tab
->n_div
; ++var
) {
1552 if (!tab
->var
[var
].is_row
)
1554 row
= tab
->var
[var
].index
;
1555 if (integer_constant(tab
, row
))
1556 ISL_FL_SET(flags
, I_CST
);
1557 if (integer_parameter(tab
, row
))
1558 ISL_FL_SET(flags
, I_PAR
);
1559 if (ISL_FL_ISSET(flags
, I_CST
) && ISL_FL_ISSET(flags
, I_PAR
))
1561 if (integer_variable(tab
, row
))
1562 ISL_FL_SET(flags
, I_VAR
);
1569 /* Check for first (non-parameter) variable that is non-integer and
1570 * therefore requires a cut and return the corresponding row.
1571 * For parametric tableaus, there are three parts in a row,
1572 * the constant, the coefficients of the parameters and the rest.
1573 * For each part, we check whether the coefficients in that part
1574 * are all integral and if so, set the corresponding flag in *f.
1575 * If the constant and the parameter part are integral, then the
1576 * current sample value is integral and no cut is required
1577 * (irrespective of whether the variable part is integral).
1579 static int first_non_integer_row(struct isl_tab
*tab
, int *f
)
1581 int var
= next_non_integer_var(tab
, -1, f
);
1583 return var
< 0 ? -1 : tab
->var
[var
].index
;
1586 /* Add a (non-parametric) cut to cut away the non-integral sample
1587 * value of the given row.
1589 * If the row is given by
1591 * m r = f + \sum_i a_i y_i
1595 * c = - {-f/m} + \sum_i {a_i/m} y_i >= 0
1597 * The big parameter, if any, is ignored, since it is assumed to be big
1598 * enough to be divisible by any integer.
1599 * If the tableau is actually a parametric tableau, then this function
1600 * is only called when all coefficients of the parameters are integral.
1601 * The cut therefore has zero coefficients for the parameters.
1603 * The current value is known to be negative, so row_sign, if it
1604 * exists, is set accordingly.
1606 * Return the row of the cut or -1.
1608 static int add_cut(struct isl_tab
*tab
, int row
)
1613 unsigned off
= 2 + tab
->M
;
1615 if (isl_tab_extend_cons(tab
, 1) < 0)
1617 r
= isl_tab_allocate_con(tab
);
1621 r_row
= tab
->mat
->row
[tab
->con
[r
].index
];
1622 isl_int_set(r_row
[0], tab
->mat
->row
[row
][0]);
1623 isl_int_neg(r_row
[1], tab
->mat
->row
[row
][1]);
1624 isl_int_fdiv_r(r_row
[1], r_row
[1], tab
->mat
->row
[row
][0]);
1625 isl_int_neg(r_row
[1], r_row
[1]);
1627 isl_int_set_si(r_row
[2], 0);
1628 for (i
= 0; i
< tab
->n_col
; ++i
)
1629 isl_int_fdiv_r(r_row
[off
+ i
],
1630 tab
->mat
->row
[row
][off
+ i
], tab
->mat
->row
[row
][0]);
1632 tab
->con
[r
].is_nonneg
= 1;
1633 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
1636 tab
->row_sign
[tab
->con
[r
].index
] = isl_tab_row_neg
;
1638 return tab
->con
[r
].index
;
1644 /* Given a non-parametric tableau, add cuts until an integer
1645 * sample point is obtained or until the tableau is determined
1646 * to be integer infeasible.
1647 * As long as there is any non-integer value in the sample point,
1648 * we add appropriate cuts, if possible, for each of these
1649 * non-integer values and then resolve the violated
1650 * cut constraints using restore_lexmin.
1651 * If one of the corresponding rows is equal to an integral
1652 * combination of variables/constraints plus a non-integral constant,
1653 * then there is no way to obtain an integer point and we return
1654 * a tableau that is marked empty.
1655 * The parameter cutting_strategy controls the strategy used when adding cuts
1656 * to remove non-integer points. CUT_ALL adds all possible cuts
1657 * before continuing the search. CUT_ONE adds only one cut at a time.
1659 static struct isl_tab
*cut_to_integer_lexmin(struct isl_tab
*tab
,
1660 int cutting_strategy
)
1671 while ((var
= next_non_integer_var(tab
, -1, &flags
)) != -1) {
1673 if (ISL_FL_ISSET(flags
, I_VAR
)) {
1674 if (isl_tab_mark_empty(tab
) < 0)
1678 row
= tab
->var
[var
].index
;
1679 row
= add_cut(tab
, row
);
1682 if (cutting_strategy
== CUT_ONE
)
1684 } while ((var
= next_non_integer_var(tab
, var
, &flags
)) != -1);
1685 if (restore_lexmin(tab
) < 0)
1696 /* Check whether all the currently active samples also satisfy the inequality
1697 * "ineq" (treated as an equality if eq is set).
1698 * Remove those samples that do not.
1700 static struct isl_tab
*check_samples(struct isl_tab
*tab
, isl_int
*ineq
, int eq
)
1708 isl_assert(tab
->mat
->ctx
, tab
->bmap
, goto error
);
1709 isl_assert(tab
->mat
->ctx
, tab
->samples
, goto error
);
1710 isl_assert(tab
->mat
->ctx
, tab
->samples
->n_col
== 1 + tab
->n_var
, goto error
);
1713 for (i
= tab
->n_outside
; i
< tab
->n_sample
; ++i
) {
1715 isl_seq_inner_product(ineq
, tab
->samples
->row
[i
],
1716 1 + tab
->n_var
, &v
);
1717 sgn
= isl_int_sgn(v
);
1718 if (eq
? (sgn
== 0) : (sgn
>= 0))
1720 tab
= isl_tab_drop_sample(tab
, i
);
1732 /* Check whether the sample value of the tableau is finite,
1733 * i.e., either the tableau does not use a big parameter, or
1734 * all values of the variables are equal to the big parameter plus
1735 * some constant. This constant is the actual sample value.
1737 static int sample_is_finite(struct isl_tab
*tab
)
1744 for (i
= 0; i
< tab
->n_var
; ++i
) {
1746 if (!tab
->var
[i
].is_row
)
1748 row
= tab
->var
[i
].index
;
1749 if (isl_int_ne(tab
->mat
->row
[row
][0], tab
->mat
->row
[row
][2]))
1755 /* Check if the context tableau of sol has any integer points.
1756 * Leave tab in empty state if no integer point can be found.
1757 * If an integer point can be found and if moreover it is finite,
1758 * then it is added to the list of sample values.
1760 * This function is only called when none of the currently active sample
1761 * values satisfies the most recently added constraint.
1763 static struct isl_tab
*check_integer_feasible(struct isl_tab
*tab
)
1765 struct isl_tab_undo
*snap
;
1770 snap
= isl_tab_snap(tab
);
1771 if (isl_tab_push_basis(tab
) < 0)
1774 tab
= cut_to_integer_lexmin(tab
, CUT_ALL
);
1778 if (!tab
->empty
&& sample_is_finite(tab
)) {
1779 struct isl_vec
*sample
;
1781 sample
= isl_tab_get_sample_value(tab
);
1783 tab
= isl_tab_add_sample(tab
, sample
);
1786 if (!tab
->empty
&& isl_tab_rollback(tab
, snap
) < 0)
1795 /* Check if any of the currently active sample values satisfies
1796 * the inequality "ineq" (an equality if eq is set).
1798 static int tab_has_valid_sample(struct isl_tab
*tab
, isl_int
*ineq
, int eq
)
1806 isl_assert(tab
->mat
->ctx
, tab
->bmap
, return -1);
1807 isl_assert(tab
->mat
->ctx
, tab
->samples
, return -1);
1808 isl_assert(tab
->mat
->ctx
, tab
->samples
->n_col
== 1 + tab
->n_var
, return -1);
1811 for (i
= tab
->n_outside
; i
< tab
->n_sample
; ++i
) {
1813 isl_seq_inner_product(ineq
, tab
->samples
->row
[i
],
1814 1 + tab
->n_var
, &v
);
1815 sgn
= isl_int_sgn(v
);
1816 if (eq
? (sgn
== 0) : (sgn
>= 0))
1821 return i
< tab
->n_sample
;
1824 /* Add a div specified by "div" to the tableau "tab" and return
1825 * 1 if the div is obviously non-negative.
1827 static int context_tab_add_div(struct isl_tab
*tab
, struct isl_vec
*div
,
1828 int (*add_ineq
)(void *user
, isl_int
*), void *user
)
1832 struct isl_mat
*samples
;
1835 r
= isl_tab_add_div(tab
, div
, add_ineq
, user
);
1838 nonneg
= tab
->var
[r
].is_nonneg
;
1839 tab
->var
[r
].frozen
= 1;
1841 samples
= isl_mat_extend(tab
->samples
,
1842 tab
->n_sample
, 1 + tab
->n_var
);
1843 tab
->samples
= samples
;
1846 for (i
= tab
->n_outside
; i
< samples
->n_row
; ++i
) {
1847 isl_seq_inner_product(div
->el
+ 1, samples
->row
[i
],
1848 div
->size
- 1, &samples
->row
[i
][samples
->n_col
- 1]);
1849 isl_int_fdiv_q(samples
->row
[i
][samples
->n_col
- 1],
1850 samples
->row
[i
][samples
->n_col
- 1], div
->el
[0]);
1856 /* Add a div specified by "div" to both the main tableau and
1857 * the context tableau. In case of the main tableau, we only
1858 * need to add an extra div. In the context tableau, we also
1859 * need to express the meaning of the div.
1860 * Return the index of the div or -1 if anything went wrong.
1862 static int add_div(struct isl_tab
*tab
, struct isl_context
*context
,
1863 struct isl_vec
*div
)
1868 if ((nonneg
= context
->op
->add_div(context
, div
)) < 0)
1871 if (!context
->op
->is_ok(context
))
1874 if (isl_tab_extend_vars(tab
, 1) < 0)
1876 r
= isl_tab_allocate_var(tab
);
1880 tab
->var
[r
].is_nonneg
= 1;
1881 tab
->var
[r
].frozen
= 1;
1884 return tab
->n_div
- 1;
1886 context
->op
->invalidate(context
);
1890 static int find_div(struct isl_tab
*tab
, isl_int
*div
, isl_int denom
)
1893 unsigned total
= isl_basic_map_total_dim(tab
->bmap
);
1895 for (i
= 0; i
< tab
->bmap
->n_div
; ++i
) {
1896 if (isl_int_ne(tab
->bmap
->div
[i
][0], denom
))
1898 if (!isl_seq_eq(tab
->bmap
->div
[i
] + 1, div
, 1 + total
))
1905 /* Return the index of a div that corresponds to "div".
1906 * We first check if we already have such a div and if not, we create one.
1908 static int get_div(struct isl_tab
*tab
, struct isl_context
*context
,
1909 struct isl_vec
*div
)
1912 struct isl_tab
*context_tab
= context
->op
->peek_tab(context
);
1917 d
= find_div(context_tab
, div
->el
+ 1, div
->el
[0]);
1921 return add_div(tab
, context
, div
);
1924 /* Add a parametric cut to cut away the non-integral sample value
1926 * Let a_i be the coefficients of the constant term and the parameters
1927 * and let b_i be the coefficients of the variables or constraints
1928 * in basis of the tableau.
1929 * Let q be the div q = floor(\sum_i {-a_i} y_i).
1931 * The cut is expressed as
1933 * c = \sum_i -{-a_i} y_i + \sum_i {b_i} x_i + q >= 0
1935 * If q did not already exist in the context tableau, then it is added first.
1936 * If q is in a column of the main tableau then the "+ q" can be accomplished
1937 * by setting the corresponding entry to the denominator of the constraint.
1938 * If q happens to be in a row of the main tableau, then the corresponding
1939 * row needs to be added instead (taking care of the denominators).
1940 * Note that this is very unlikely, but perhaps not entirely impossible.
1942 * The current value of the cut is known to be negative (or at least
1943 * non-positive), so row_sign is set accordingly.
1945 * Return the row of the cut or -1.
1947 static int add_parametric_cut(struct isl_tab
*tab
, int row
,
1948 struct isl_context
*context
)
1950 struct isl_vec
*div
;
1957 unsigned off
= 2 + tab
->M
;
1962 div
= get_row_parameter_div(tab
, row
);
1967 d
= context
->op
->get_div(context
, tab
, div
);
1972 if (isl_tab_extend_cons(tab
, 1) < 0)
1974 r
= isl_tab_allocate_con(tab
);
1978 r_row
= tab
->mat
->row
[tab
->con
[r
].index
];
1979 isl_int_set(r_row
[0], tab
->mat
->row
[row
][0]);
1980 isl_int_neg(r_row
[1], tab
->mat
->row
[row
][1]);
1981 isl_int_fdiv_r(r_row
[1], r_row
[1], tab
->mat
->row
[row
][0]);
1982 isl_int_neg(r_row
[1], r_row
[1]);
1984 isl_int_set_si(r_row
[2], 0);
1985 for (i
= 0; i
< tab
->n_param
; ++i
) {
1986 if (tab
->var
[i
].is_row
)
1988 col
= tab
->var
[i
].index
;
1989 isl_int_neg(r_row
[off
+ col
], tab
->mat
->row
[row
][off
+ col
]);
1990 isl_int_fdiv_r(r_row
[off
+ col
], r_row
[off
+ col
],
1991 tab
->mat
->row
[row
][0]);
1992 isl_int_neg(r_row
[off
+ col
], r_row
[off
+ col
]);
1994 for (i
= 0; i
< tab
->n_div
; ++i
) {
1995 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
1997 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
1998 isl_int_neg(r_row
[off
+ col
], tab
->mat
->row
[row
][off
+ col
]);
1999 isl_int_fdiv_r(r_row
[off
+ col
], r_row
[off
+ col
],
2000 tab
->mat
->row
[row
][0]);
2001 isl_int_neg(r_row
[off
+ col
], r_row
[off
+ col
]);
2003 for (i
= 0; i
< tab
->n_col
; ++i
) {
2004 if (tab
->col_var
[i
] >= 0 &&
2005 (tab
->col_var
[i
] < tab
->n_param
||
2006 tab
->col_var
[i
] >= tab
->n_var
- tab
->n_div
))
2008 isl_int_fdiv_r(r_row
[off
+ i
],
2009 tab
->mat
->row
[row
][off
+ i
], tab
->mat
->row
[row
][0]);
2011 if (tab
->var
[tab
->n_var
- tab
->n_div
+ d
].is_row
) {
2013 int d_row
= tab
->var
[tab
->n_var
- tab
->n_div
+ d
].index
;
2015 isl_int_gcd(gcd
, tab
->mat
->row
[d_row
][0], r_row
[0]);
2016 isl_int_divexact(r_row
[0], r_row
[0], gcd
);
2017 isl_int_divexact(gcd
, tab
->mat
->row
[d_row
][0], gcd
);
2018 isl_seq_combine(r_row
+ 1, gcd
, r_row
+ 1,
2019 r_row
[0], tab
->mat
->row
[d_row
] + 1,
2020 off
- 1 + tab
->n_col
);
2021 isl_int_mul(r_row
[0], r_row
[0], tab
->mat
->row
[d_row
][0]);
2024 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ d
].index
;
2025 isl_int_set(r_row
[off
+ col
], tab
->mat
->row
[row
][0]);
2028 tab
->con
[r
].is_nonneg
= 1;
2029 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
2032 tab
->row_sign
[tab
->con
[r
].index
] = isl_tab_row_neg
;
2034 row
= tab
->con
[r
].index
;
2036 if (d
>= n
&& context
->op
->detect_equalities(context
, tab
) < 0)
2042 /* Construct a tableau for bmap that can be used for computing
2043 * the lexicographic minimum (or maximum) of bmap.
2044 * If not NULL, then dom is the domain where the minimum
2045 * should be computed. In this case, we set up a parametric
2046 * tableau with row signs (initialized to "unknown").
2047 * If M is set, then the tableau will use a big parameter.
2048 * If max is set, then a maximum should be computed instead of a minimum.
2049 * This means that for each variable x, the tableau will contain the variable
2050 * x' = M - x, rather than x' = M + x. This in turn means that the coefficient
2051 * of the variables in all constraints are negated prior to adding them
2054 static struct isl_tab
*tab_for_lexmin(struct isl_basic_map
*bmap
,
2055 struct isl_basic_set
*dom
, unsigned M
, int max
)
2058 struct isl_tab
*tab
;
2060 tab
= isl_tab_alloc(bmap
->ctx
, 2 * bmap
->n_eq
+ bmap
->n_ineq
+ 1,
2061 isl_basic_map_total_dim(bmap
), M
);
2065 tab
->rational
= ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
2067 tab
->n_param
= isl_basic_set_total_dim(dom
) - dom
->n_div
;
2068 tab
->n_div
= dom
->n_div
;
2069 tab
->row_sign
= isl_calloc_array(bmap
->ctx
,
2070 enum isl_tab_row_sign
, tab
->mat
->n_row
);
2074 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
)) {
2075 if (isl_tab_mark_empty(tab
) < 0)
2080 for (i
= tab
->n_param
; i
< tab
->n_var
- tab
->n_div
; ++i
) {
2081 tab
->var
[i
].is_nonneg
= 1;
2082 tab
->var
[i
].frozen
= 1;
2084 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
2086 isl_seq_neg(bmap
->eq
[i
] + 1 + tab
->n_param
,
2087 bmap
->eq
[i
] + 1 + tab
->n_param
,
2088 tab
->n_var
- tab
->n_param
- tab
->n_div
);
2089 tab
= add_lexmin_valid_eq(tab
, bmap
->eq
[i
]);
2091 isl_seq_neg(bmap
->eq
[i
] + 1 + tab
->n_param
,
2092 bmap
->eq
[i
] + 1 + tab
->n_param
,
2093 tab
->n_var
- tab
->n_param
- tab
->n_div
);
2094 if (!tab
|| tab
->empty
)
2097 if (bmap
->n_eq
&& restore_lexmin(tab
) < 0)
2099 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
2101 isl_seq_neg(bmap
->ineq
[i
] + 1 + tab
->n_param
,
2102 bmap
->ineq
[i
] + 1 + tab
->n_param
,
2103 tab
->n_var
- tab
->n_param
- tab
->n_div
);
2104 tab
= add_lexmin_ineq(tab
, bmap
->ineq
[i
]);
2106 isl_seq_neg(bmap
->ineq
[i
] + 1 + tab
->n_param
,
2107 bmap
->ineq
[i
] + 1 + tab
->n_param
,
2108 tab
->n_var
- tab
->n_param
- tab
->n_div
);
2109 if (!tab
|| tab
->empty
)
2118 /* Given a main tableau where more than one row requires a split,
2119 * determine and return the "best" row to split on.
2121 * Given two rows in the main tableau, if the inequality corresponding
2122 * to the first row is redundant with respect to that of the second row
2123 * in the current tableau, then it is better to split on the second row,
2124 * since in the positive part, both row will be positive.
2125 * (In the negative part a pivot will have to be performed and just about
2126 * anything can happen to the sign of the other row.)
2128 * As a simple heuristic, we therefore select the row that makes the most
2129 * of the other rows redundant.
2131 * Perhaps it would also be useful to look at the number of constraints
2132 * that conflict with any given constraint.
2134 static int best_split(struct isl_tab
*tab
, struct isl_tab
*context_tab
)
2136 struct isl_tab_undo
*snap
;
2142 if (isl_tab_extend_cons(context_tab
, 2) < 0)
2145 snap
= isl_tab_snap(context_tab
);
2147 for (split
= tab
->n_redundant
; split
< tab
->n_row
; ++split
) {
2148 struct isl_tab_undo
*snap2
;
2149 struct isl_vec
*ineq
= NULL
;
2153 if (!isl_tab_var_from_row(tab
, split
)->is_nonneg
)
2155 if (tab
->row_sign
[split
] != isl_tab_row_any
)
2158 ineq
= get_row_parameter_ineq(tab
, split
);
2161 ok
= isl_tab_add_ineq(context_tab
, ineq
->el
) >= 0;
2166 snap2
= isl_tab_snap(context_tab
);
2168 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
2169 struct isl_tab_var
*var
;
2173 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
2175 if (tab
->row_sign
[row
] != isl_tab_row_any
)
2178 ineq
= get_row_parameter_ineq(tab
, row
);
2181 ok
= isl_tab_add_ineq(context_tab
, ineq
->el
) >= 0;
2185 var
= &context_tab
->con
[context_tab
->n_con
- 1];
2186 if (!context_tab
->empty
&&
2187 !isl_tab_min_at_most_neg_one(context_tab
, var
))
2189 if (isl_tab_rollback(context_tab
, snap2
) < 0)
2192 if (best
== -1 || r
> best_r
) {
2196 if (isl_tab_rollback(context_tab
, snap
) < 0)
2203 static struct isl_basic_set
*context_lex_peek_basic_set(
2204 struct isl_context
*context
)
2206 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2209 return isl_tab_peek_bset(clex
->tab
);
2212 static struct isl_tab
*context_lex_peek_tab(struct isl_context
*context
)
2214 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2218 static void context_lex_add_eq(struct isl_context
*context
, isl_int
*eq
,
2219 int check
, int update
)
2221 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2222 if (isl_tab_extend_cons(clex
->tab
, 2) < 0)
2224 if (add_lexmin_eq(clex
->tab
, eq
) < 0)
2227 int v
= tab_has_valid_sample(clex
->tab
, eq
, 1);
2231 clex
->tab
= check_integer_feasible(clex
->tab
);
2234 clex
->tab
= check_samples(clex
->tab
, eq
, 1);
2237 isl_tab_free(clex
->tab
);
2241 static void context_lex_add_ineq(struct isl_context
*context
, isl_int
*ineq
,
2242 int check
, int update
)
2244 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2245 if (isl_tab_extend_cons(clex
->tab
, 1) < 0)
2247 clex
->tab
= add_lexmin_ineq(clex
->tab
, ineq
);
2249 int v
= tab_has_valid_sample(clex
->tab
, ineq
, 0);
2253 clex
->tab
= check_integer_feasible(clex
->tab
);
2256 clex
->tab
= check_samples(clex
->tab
, ineq
, 0);
2259 isl_tab_free(clex
->tab
);
2263 static int context_lex_add_ineq_wrap(void *user
, isl_int
*ineq
)
2265 struct isl_context
*context
= (struct isl_context
*)user
;
2266 context_lex_add_ineq(context
, ineq
, 0, 0);
2267 return context
->op
->is_ok(context
) ? 0 : -1;
2270 /* Check which signs can be obtained by "ineq" on all the currently
2271 * active sample values. See row_sign for more information.
2273 static enum isl_tab_row_sign
tab_ineq_sign(struct isl_tab
*tab
, isl_int
*ineq
,
2279 enum isl_tab_row_sign res
= isl_tab_row_unknown
;
2281 isl_assert(tab
->mat
->ctx
, tab
->samples
, return isl_tab_row_unknown
);
2282 isl_assert(tab
->mat
->ctx
, tab
->samples
->n_col
== 1 + tab
->n_var
,
2283 return isl_tab_row_unknown
);
2286 for (i
= tab
->n_outside
; i
< tab
->n_sample
; ++i
) {
2287 isl_seq_inner_product(tab
->samples
->row
[i
], ineq
,
2288 1 + tab
->n_var
, &tmp
);
2289 sgn
= isl_int_sgn(tmp
);
2290 if (sgn
> 0 || (sgn
== 0 && strict
)) {
2291 if (res
== isl_tab_row_unknown
)
2292 res
= isl_tab_row_pos
;
2293 if (res
== isl_tab_row_neg
)
2294 res
= isl_tab_row_any
;
2297 if (res
== isl_tab_row_unknown
)
2298 res
= isl_tab_row_neg
;
2299 if (res
== isl_tab_row_pos
)
2300 res
= isl_tab_row_any
;
2302 if (res
== isl_tab_row_any
)
2310 static enum isl_tab_row_sign
context_lex_ineq_sign(struct isl_context
*context
,
2311 isl_int
*ineq
, int strict
)
2313 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2314 return tab_ineq_sign(clex
->tab
, ineq
, strict
);
2317 /* Check whether "ineq" can be added to the tableau without rendering
2320 static int context_lex_test_ineq(struct isl_context
*context
, isl_int
*ineq
)
2322 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2323 struct isl_tab_undo
*snap
;
2329 if (isl_tab_extend_cons(clex
->tab
, 1) < 0)
2332 snap
= isl_tab_snap(clex
->tab
);
2333 if (isl_tab_push_basis(clex
->tab
) < 0)
2335 clex
->tab
= add_lexmin_ineq(clex
->tab
, ineq
);
2336 clex
->tab
= check_integer_feasible(clex
->tab
);
2339 feasible
= !clex
->tab
->empty
;
2340 if (isl_tab_rollback(clex
->tab
, snap
) < 0)
2346 static int context_lex_get_div(struct isl_context
*context
, struct isl_tab
*tab
,
2347 struct isl_vec
*div
)
2349 return get_div(tab
, context
, div
);
2352 /* Add a div specified by "div" to the context tableau and return
2353 * 1 if the div is obviously non-negative.
2354 * context_tab_add_div will always return 1, because all variables
2355 * in a isl_context_lex tableau are non-negative.
2356 * However, if we are using a big parameter in the context, then this only
2357 * reflects the non-negativity of the variable used to _encode_ the
2358 * div, i.e., div' = M + div, so we can't draw any conclusions.
2360 static int context_lex_add_div(struct isl_context
*context
, struct isl_vec
*div
)
2362 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2364 nonneg
= context_tab_add_div(clex
->tab
, div
,
2365 context_lex_add_ineq_wrap
, context
);
2373 static int context_lex_detect_equalities(struct isl_context
*context
,
2374 struct isl_tab
*tab
)
2379 static int context_lex_best_split(struct isl_context
*context
,
2380 struct isl_tab
*tab
)
2382 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2383 struct isl_tab_undo
*snap
;
2386 snap
= isl_tab_snap(clex
->tab
);
2387 if (isl_tab_push_basis(clex
->tab
) < 0)
2389 r
= best_split(tab
, clex
->tab
);
2391 if (r
>= 0 && isl_tab_rollback(clex
->tab
, snap
) < 0)
2397 static int context_lex_is_empty(struct isl_context
*context
)
2399 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2402 return clex
->tab
->empty
;
2405 static void *context_lex_save(struct isl_context
*context
)
2407 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2408 struct isl_tab_undo
*snap
;
2410 snap
= isl_tab_snap(clex
->tab
);
2411 if (isl_tab_push_basis(clex
->tab
) < 0)
2413 if (isl_tab_save_samples(clex
->tab
) < 0)
2419 static void context_lex_restore(struct isl_context
*context
, void *save
)
2421 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2422 if (isl_tab_rollback(clex
->tab
, (struct isl_tab_undo
*)save
) < 0) {
2423 isl_tab_free(clex
->tab
);
2428 static void context_lex_discard(void *save
)
2432 static int context_lex_is_ok(struct isl_context
*context
)
2434 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2438 /* For each variable in the context tableau, check if the variable can
2439 * only attain non-negative values. If so, mark the parameter as non-negative
2440 * in the main tableau. This allows for a more direct identification of some
2441 * cases of violated constraints.
2443 static struct isl_tab
*tab_detect_nonnegative_parameters(struct isl_tab
*tab
,
2444 struct isl_tab
*context_tab
)
2447 struct isl_tab_undo
*snap
;
2448 struct isl_vec
*ineq
= NULL
;
2449 struct isl_tab_var
*var
;
2452 if (context_tab
->n_var
== 0)
2455 ineq
= isl_vec_alloc(tab
->mat
->ctx
, 1 + context_tab
->n_var
);
2459 if (isl_tab_extend_cons(context_tab
, 1) < 0)
2462 snap
= isl_tab_snap(context_tab
);
2465 isl_seq_clr(ineq
->el
, ineq
->size
);
2466 for (i
= 0; i
< context_tab
->n_var
; ++i
) {
2467 isl_int_set_si(ineq
->el
[1 + i
], 1);
2468 if (isl_tab_add_ineq(context_tab
, ineq
->el
) < 0)
2470 var
= &context_tab
->con
[context_tab
->n_con
- 1];
2471 if (!context_tab
->empty
&&
2472 !isl_tab_min_at_most_neg_one(context_tab
, var
)) {
2474 if (i
>= tab
->n_param
)
2475 j
= i
- tab
->n_param
+ tab
->n_var
- tab
->n_div
;
2476 tab
->var
[j
].is_nonneg
= 1;
2479 isl_int_set_si(ineq
->el
[1 + i
], 0);
2480 if (isl_tab_rollback(context_tab
, snap
) < 0)
2484 if (context_tab
->M
&& n
== context_tab
->n_var
) {
2485 context_tab
->mat
= isl_mat_drop_cols(context_tab
->mat
, 2, 1);
2497 static struct isl_tab
*context_lex_detect_nonnegative_parameters(
2498 struct isl_context
*context
, struct isl_tab
*tab
)
2500 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2501 struct isl_tab_undo
*snap
;
2506 snap
= isl_tab_snap(clex
->tab
);
2507 if (isl_tab_push_basis(clex
->tab
) < 0)
2510 tab
= tab_detect_nonnegative_parameters(tab
, clex
->tab
);
2512 if (isl_tab_rollback(clex
->tab
, snap
) < 0)
2521 static void context_lex_invalidate(struct isl_context
*context
)
2523 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2524 isl_tab_free(clex
->tab
);
2528 static void context_lex_free(struct isl_context
*context
)
2530 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2531 isl_tab_free(clex
->tab
);
2535 struct isl_context_op isl_context_lex_op
= {
2536 context_lex_detect_nonnegative_parameters
,
2537 context_lex_peek_basic_set
,
2538 context_lex_peek_tab
,
2540 context_lex_add_ineq
,
2541 context_lex_ineq_sign
,
2542 context_lex_test_ineq
,
2543 context_lex_get_div
,
2544 context_lex_add_div
,
2545 context_lex_detect_equalities
,
2546 context_lex_best_split
,
2547 context_lex_is_empty
,
2550 context_lex_restore
,
2551 context_lex_discard
,
2552 context_lex_invalidate
,
2556 static struct isl_tab
*context_tab_for_lexmin(struct isl_basic_set
*bset
)
2558 struct isl_tab
*tab
;
2562 tab
= tab_for_lexmin((struct isl_basic_map
*)bset
, NULL
, 1, 0);
2565 if (isl_tab_track_bset(tab
, bset
) < 0)
2567 tab
= isl_tab_init_samples(tab
);
2570 isl_basic_set_free(bset
);
2574 static struct isl_context
*isl_context_lex_alloc(struct isl_basic_set
*dom
)
2576 struct isl_context_lex
*clex
;
2581 clex
= isl_alloc_type(dom
->ctx
, struct isl_context_lex
);
2585 clex
->context
.op
= &isl_context_lex_op
;
2587 clex
->tab
= context_tab_for_lexmin(isl_basic_set_copy(dom
));
2588 if (restore_lexmin(clex
->tab
) < 0)
2590 clex
->tab
= check_integer_feasible(clex
->tab
);
2594 return &clex
->context
;
2596 clex
->context
.op
->free(&clex
->context
);
2600 struct isl_context_gbr
{
2601 struct isl_context context
;
2602 struct isl_tab
*tab
;
2603 struct isl_tab
*shifted
;
2604 struct isl_tab
*cone
;
2607 static struct isl_tab
*context_gbr_detect_nonnegative_parameters(
2608 struct isl_context
*context
, struct isl_tab
*tab
)
2610 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2613 return tab_detect_nonnegative_parameters(tab
, cgbr
->tab
);
2616 static struct isl_basic_set
*context_gbr_peek_basic_set(
2617 struct isl_context
*context
)
2619 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2622 return isl_tab_peek_bset(cgbr
->tab
);
2625 static struct isl_tab
*context_gbr_peek_tab(struct isl_context
*context
)
2627 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2631 /* Initialize the "shifted" tableau of the context, which
2632 * contains the constraints of the original tableau shifted
2633 * by the sum of all negative coefficients. This ensures
2634 * that any rational point in the shifted tableau can
2635 * be rounded up to yield an integer point in the original tableau.
2637 static void gbr_init_shifted(struct isl_context_gbr
*cgbr
)
2640 struct isl_vec
*cst
;
2641 struct isl_basic_set
*bset
= isl_tab_peek_bset(cgbr
->tab
);
2642 unsigned dim
= isl_basic_set_total_dim(bset
);
2644 cst
= isl_vec_alloc(cgbr
->tab
->mat
->ctx
, bset
->n_ineq
);
2648 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
2649 isl_int_set(cst
->el
[i
], bset
->ineq
[i
][0]);
2650 for (j
= 0; j
< dim
; ++j
) {
2651 if (!isl_int_is_neg(bset
->ineq
[i
][1 + j
]))
2653 isl_int_add(bset
->ineq
[i
][0], bset
->ineq
[i
][0],
2654 bset
->ineq
[i
][1 + j
]);
2658 cgbr
->shifted
= isl_tab_from_basic_set(bset
, 0);
2660 for (i
= 0; i
< bset
->n_ineq
; ++i
)
2661 isl_int_set(bset
->ineq
[i
][0], cst
->el
[i
]);
2666 /* Check if the shifted tableau is non-empty, and if so
2667 * use the sample point to construct an integer point
2668 * of the context tableau.
2670 static struct isl_vec
*gbr_get_shifted_sample(struct isl_context_gbr
*cgbr
)
2672 struct isl_vec
*sample
;
2675 gbr_init_shifted(cgbr
);
2678 if (cgbr
->shifted
->empty
)
2679 return isl_vec_alloc(cgbr
->tab
->mat
->ctx
, 0);
2681 sample
= isl_tab_get_sample_value(cgbr
->shifted
);
2682 sample
= isl_vec_ceil(sample
);
2687 static struct isl_basic_set
*drop_constant_terms(struct isl_basic_set
*bset
)
2694 for (i
= 0; i
< bset
->n_eq
; ++i
)
2695 isl_int_set_si(bset
->eq
[i
][0], 0);
2697 for (i
= 0; i
< bset
->n_ineq
; ++i
)
2698 isl_int_set_si(bset
->ineq
[i
][0], 0);
2703 static int use_shifted(struct isl_context_gbr
*cgbr
)
2705 return cgbr
->tab
->bmap
->n_eq
== 0 && cgbr
->tab
->bmap
->n_div
== 0;
2708 static struct isl_vec
*gbr_get_sample(struct isl_context_gbr
*cgbr
)
2710 struct isl_basic_set
*bset
;
2711 struct isl_basic_set
*cone
;
2713 if (isl_tab_sample_is_integer(cgbr
->tab
))
2714 return isl_tab_get_sample_value(cgbr
->tab
);
2716 if (use_shifted(cgbr
)) {
2717 struct isl_vec
*sample
;
2719 sample
= gbr_get_shifted_sample(cgbr
);
2720 if (!sample
|| sample
->size
> 0)
2723 isl_vec_free(sample
);
2727 bset
= isl_tab_peek_bset(cgbr
->tab
);
2728 cgbr
->cone
= isl_tab_from_recession_cone(bset
, 0);
2731 if (isl_tab_track_bset(cgbr
->cone
,
2732 isl_basic_set_copy(bset
)) < 0)
2735 if (isl_tab_detect_implicit_equalities(cgbr
->cone
) < 0)
2738 if (cgbr
->cone
->n_dead
== cgbr
->cone
->n_col
) {
2739 struct isl_vec
*sample
;
2740 struct isl_tab_undo
*snap
;
2742 if (cgbr
->tab
->basis
) {
2743 if (cgbr
->tab
->basis
->n_col
!= 1 + cgbr
->tab
->n_var
) {
2744 isl_mat_free(cgbr
->tab
->basis
);
2745 cgbr
->tab
->basis
= NULL
;
2747 cgbr
->tab
->n_zero
= 0;
2748 cgbr
->tab
->n_unbounded
= 0;
2751 snap
= isl_tab_snap(cgbr
->tab
);
2753 sample
= isl_tab_sample(cgbr
->tab
);
2755 if (isl_tab_rollback(cgbr
->tab
, snap
) < 0) {
2756 isl_vec_free(sample
);
2763 cone
= isl_basic_set_dup(isl_tab_peek_bset(cgbr
->cone
));
2764 cone
= drop_constant_terms(cone
);
2765 cone
= isl_basic_set_update_from_tab(cone
, cgbr
->cone
);
2766 cone
= isl_basic_set_underlying_set(cone
);
2767 cone
= isl_basic_set_gauss(cone
, NULL
);
2769 bset
= isl_basic_set_dup(isl_tab_peek_bset(cgbr
->tab
));
2770 bset
= isl_basic_set_update_from_tab(bset
, cgbr
->tab
);
2771 bset
= isl_basic_set_underlying_set(bset
);
2772 bset
= isl_basic_set_gauss(bset
, NULL
);
2774 return isl_basic_set_sample_with_cone(bset
, cone
);
2777 static void check_gbr_integer_feasible(struct isl_context_gbr
*cgbr
)
2779 struct isl_vec
*sample
;
2784 if (cgbr
->tab
->empty
)
2787 sample
= gbr_get_sample(cgbr
);
2791 if (sample
->size
== 0) {
2792 isl_vec_free(sample
);
2793 if (isl_tab_mark_empty(cgbr
->tab
) < 0)
2798 cgbr
->tab
= isl_tab_add_sample(cgbr
->tab
, sample
);
2802 isl_tab_free(cgbr
->tab
);
2806 static struct isl_tab
*add_gbr_eq(struct isl_tab
*tab
, isl_int
*eq
)
2811 if (isl_tab_extend_cons(tab
, 2) < 0)
2814 if (isl_tab_add_eq(tab
, eq
) < 0)
2823 static void context_gbr_add_eq(struct isl_context
*context
, isl_int
*eq
,
2824 int check
, int update
)
2826 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2828 cgbr
->tab
= add_gbr_eq(cgbr
->tab
, eq
);
2830 if (cgbr
->cone
&& cgbr
->cone
->n_col
!= cgbr
->cone
->n_dead
) {
2831 if (isl_tab_extend_cons(cgbr
->cone
, 2) < 0)
2833 if (isl_tab_add_eq(cgbr
->cone
, eq
) < 0)
2838 int v
= tab_has_valid_sample(cgbr
->tab
, eq
, 1);
2842 check_gbr_integer_feasible(cgbr
);
2845 cgbr
->tab
= check_samples(cgbr
->tab
, eq
, 1);
2848 isl_tab_free(cgbr
->tab
);
2852 static void add_gbr_ineq(struct isl_context_gbr
*cgbr
, isl_int
*ineq
)
2857 if (isl_tab_extend_cons(cgbr
->tab
, 1) < 0)
2860 if (isl_tab_add_ineq(cgbr
->tab
, ineq
) < 0)
2863 if (cgbr
->shifted
&& !cgbr
->shifted
->empty
&& use_shifted(cgbr
)) {
2866 dim
= isl_basic_map_total_dim(cgbr
->tab
->bmap
);
2868 if (isl_tab_extend_cons(cgbr
->shifted
, 1) < 0)
2871 for (i
= 0; i
< dim
; ++i
) {
2872 if (!isl_int_is_neg(ineq
[1 + i
]))
2874 isl_int_add(ineq
[0], ineq
[0], ineq
[1 + i
]);
2877 if (isl_tab_add_ineq(cgbr
->shifted
, ineq
) < 0)
2880 for (i
= 0; i
< dim
; ++i
) {
2881 if (!isl_int_is_neg(ineq
[1 + i
]))
2883 isl_int_sub(ineq
[0], ineq
[0], ineq
[1 + i
]);
2887 if (cgbr
->cone
&& cgbr
->cone
->n_col
!= cgbr
->cone
->n_dead
) {
2888 if (isl_tab_extend_cons(cgbr
->cone
, 1) < 0)
2890 if (isl_tab_add_ineq(cgbr
->cone
, ineq
) < 0)
2896 isl_tab_free(cgbr
->tab
);
2900 static void context_gbr_add_ineq(struct isl_context
*context
, isl_int
*ineq
,
2901 int check
, int update
)
2903 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2905 add_gbr_ineq(cgbr
, ineq
);
2910 int v
= tab_has_valid_sample(cgbr
->tab
, ineq
, 0);
2914 check_gbr_integer_feasible(cgbr
);
2917 cgbr
->tab
= check_samples(cgbr
->tab
, ineq
, 0);
2920 isl_tab_free(cgbr
->tab
);
2924 static int context_gbr_add_ineq_wrap(void *user
, isl_int
*ineq
)
2926 struct isl_context
*context
= (struct isl_context
*)user
;
2927 context_gbr_add_ineq(context
, ineq
, 0, 0);
2928 return context
->op
->is_ok(context
) ? 0 : -1;
2931 static enum isl_tab_row_sign
context_gbr_ineq_sign(struct isl_context
*context
,
2932 isl_int
*ineq
, int strict
)
2934 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2935 return tab_ineq_sign(cgbr
->tab
, ineq
, strict
);
2938 /* Check whether "ineq" can be added to the tableau without rendering
2941 static int context_gbr_test_ineq(struct isl_context
*context
, isl_int
*ineq
)
2943 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2944 struct isl_tab_undo
*snap
;
2945 struct isl_tab_undo
*shifted_snap
= NULL
;
2946 struct isl_tab_undo
*cone_snap
= NULL
;
2952 if (isl_tab_extend_cons(cgbr
->tab
, 1) < 0)
2955 snap
= isl_tab_snap(cgbr
->tab
);
2957 shifted_snap
= isl_tab_snap(cgbr
->shifted
);
2959 cone_snap
= isl_tab_snap(cgbr
->cone
);
2960 add_gbr_ineq(cgbr
, ineq
);
2961 check_gbr_integer_feasible(cgbr
);
2964 feasible
= !cgbr
->tab
->empty
;
2965 if (isl_tab_rollback(cgbr
->tab
, snap
) < 0)
2968 if (isl_tab_rollback(cgbr
->shifted
, shifted_snap
))
2970 } else if (cgbr
->shifted
) {
2971 isl_tab_free(cgbr
->shifted
);
2972 cgbr
->shifted
= NULL
;
2975 if (isl_tab_rollback(cgbr
->cone
, cone_snap
))
2977 } else if (cgbr
->cone
) {
2978 isl_tab_free(cgbr
->cone
);
2985 /* Return the column of the last of the variables associated to
2986 * a column that has a non-zero coefficient.
2987 * This function is called in a context where only coefficients
2988 * of parameters or divs can be non-zero.
2990 static int last_non_zero_var_col(struct isl_tab
*tab
, isl_int
*p
)
2995 if (tab
->n_var
== 0)
2998 for (i
= tab
->n_var
- 1; i
>= 0; --i
) {
2999 if (i
>= tab
->n_param
&& i
< tab
->n_var
- tab
->n_div
)
3001 if (tab
->var
[i
].is_row
)
3003 col
= tab
->var
[i
].index
;
3004 if (!isl_int_is_zero(p
[col
]))
3011 /* Look through all the recently added equalities in the context
3012 * to see if we can propagate any of them to the main tableau.
3014 * The newly added equalities in the context are encoded as pairs
3015 * of inequalities starting at inequality "first".
3017 * We tentatively add each of these equalities to the main tableau
3018 * and if this happens to result in a row with a final coefficient
3019 * that is one or negative one, we use it to kill a column
3020 * in the main tableau. Otherwise, we discard the tentatively
3023 static void propagate_equalities(struct isl_context_gbr
*cgbr
,
3024 struct isl_tab
*tab
, unsigned first
)
3027 struct isl_vec
*eq
= NULL
;
3029 eq
= isl_vec_alloc(tab
->mat
->ctx
, 1 + tab
->n_var
);
3033 if (isl_tab_extend_cons(tab
, (cgbr
->tab
->bmap
->n_ineq
- first
)/2) < 0)
3036 isl_seq_clr(eq
->el
+ 1 + tab
->n_param
,
3037 tab
->n_var
- tab
->n_param
- tab
->n_div
);
3038 for (i
= first
; i
< cgbr
->tab
->bmap
->n_ineq
; i
+= 2) {
3041 struct isl_tab_undo
*snap
;
3042 snap
= isl_tab_snap(tab
);
3044 isl_seq_cpy(eq
->el
, cgbr
->tab
->bmap
->ineq
[i
], 1 + tab
->n_param
);
3045 isl_seq_cpy(eq
->el
+ 1 + tab
->n_var
- tab
->n_div
,
3046 cgbr
->tab
->bmap
->ineq
[i
] + 1 + tab
->n_param
,
3049 r
= isl_tab_add_row(tab
, eq
->el
);
3052 r
= tab
->con
[r
].index
;
3053 j
= last_non_zero_var_col(tab
, tab
->mat
->row
[r
] + 2 + tab
->M
);
3054 if (j
< 0 || j
< tab
->n_dead
||
3055 !isl_int_is_one(tab
->mat
->row
[r
][0]) ||
3056 (!isl_int_is_one(tab
->mat
->row
[r
][2 + tab
->M
+ j
]) &&
3057 !isl_int_is_negone(tab
->mat
->row
[r
][2 + tab
->M
+ j
]))) {
3058 if (isl_tab_rollback(tab
, snap
) < 0)
3062 if (isl_tab_pivot(tab
, r
, j
) < 0)
3064 if (isl_tab_kill_col(tab
, j
) < 0)
3067 if (restore_lexmin(tab
) < 0)
3076 isl_tab_free(cgbr
->tab
);
3080 static int context_gbr_detect_equalities(struct isl_context
*context
,
3081 struct isl_tab
*tab
)
3083 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3084 struct isl_ctx
*ctx
;
3087 ctx
= cgbr
->tab
->mat
->ctx
;
3090 struct isl_basic_set
*bset
= isl_tab_peek_bset(cgbr
->tab
);
3091 cgbr
->cone
= isl_tab_from_recession_cone(bset
, 0);
3094 if (isl_tab_track_bset(cgbr
->cone
,
3095 isl_basic_set_copy(bset
)) < 0)
3098 if (isl_tab_detect_implicit_equalities(cgbr
->cone
) < 0)
3101 n_ineq
= cgbr
->tab
->bmap
->n_ineq
;
3102 cgbr
->tab
= isl_tab_detect_equalities(cgbr
->tab
, cgbr
->cone
);
3103 if (cgbr
->tab
&& cgbr
->tab
->bmap
->n_ineq
> n_ineq
)
3104 propagate_equalities(cgbr
, tab
, n_ineq
);
3108 isl_tab_free(cgbr
->tab
);
3113 static int context_gbr_get_div(struct isl_context
*context
, struct isl_tab
*tab
,
3114 struct isl_vec
*div
)
3116 return get_div(tab
, context
, div
);
3119 static int context_gbr_add_div(struct isl_context
*context
, struct isl_vec
*div
)
3121 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3125 if (isl_tab_extend_cons(cgbr
->cone
, 3) < 0)
3127 if (isl_tab_extend_vars(cgbr
->cone
, 1) < 0)
3129 if (isl_tab_allocate_var(cgbr
->cone
) <0)
3132 cgbr
->cone
->bmap
= isl_basic_map_extend_space(cgbr
->cone
->bmap
,
3133 isl_basic_map_get_space(cgbr
->cone
->bmap
), 1, 0, 2);
3134 k
= isl_basic_map_alloc_div(cgbr
->cone
->bmap
);
3137 isl_seq_cpy(cgbr
->cone
->bmap
->div
[k
], div
->el
, div
->size
);
3138 if (isl_tab_push(cgbr
->cone
, isl_tab_undo_bmap_div
) < 0)
3141 return context_tab_add_div(cgbr
->tab
, div
,
3142 context_gbr_add_ineq_wrap
, context
);
3145 static int context_gbr_best_split(struct isl_context
*context
,
3146 struct isl_tab
*tab
)
3148 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3149 struct isl_tab_undo
*snap
;
3152 snap
= isl_tab_snap(cgbr
->tab
);
3153 r
= best_split(tab
, cgbr
->tab
);
3155 if (r
>= 0 && isl_tab_rollback(cgbr
->tab
, snap
) < 0)
3161 static int context_gbr_is_empty(struct isl_context
*context
)
3163 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3166 return cgbr
->tab
->empty
;
3169 struct isl_gbr_tab_undo
{
3170 struct isl_tab_undo
*tab_snap
;
3171 struct isl_tab_undo
*shifted_snap
;
3172 struct isl_tab_undo
*cone_snap
;
3175 static void *context_gbr_save(struct isl_context
*context
)
3177 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3178 struct isl_gbr_tab_undo
*snap
;
3180 snap
= isl_alloc_type(cgbr
->tab
->mat
->ctx
, struct isl_gbr_tab_undo
);
3184 snap
->tab_snap
= isl_tab_snap(cgbr
->tab
);
3185 if (isl_tab_save_samples(cgbr
->tab
) < 0)
3189 snap
->shifted_snap
= isl_tab_snap(cgbr
->shifted
);
3191 snap
->shifted_snap
= NULL
;
3194 snap
->cone_snap
= isl_tab_snap(cgbr
->cone
);
3196 snap
->cone_snap
= NULL
;
3204 static void context_gbr_restore(struct isl_context
*context
, void *save
)
3206 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3207 struct isl_gbr_tab_undo
*snap
= (struct isl_gbr_tab_undo
*)save
;
3210 if (isl_tab_rollback(cgbr
->tab
, snap
->tab_snap
) < 0) {
3211 isl_tab_free(cgbr
->tab
);
3215 if (snap
->shifted_snap
) {
3216 if (isl_tab_rollback(cgbr
->shifted
, snap
->shifted_snap
) < 0)
3218 } else if (cgbr
->shifted
) {
3219 isl_tab_free(cgbr
->shifted
);
3220 cgbr
->shifted
= NULL
;
3223 if (snap
->cone_snap
) {
3224 if (isl_tab_rollback(cgbr
->cone
, snap
->cone_snap
) < 0)
3226 } else if (cgbr
->cone
) {
3227 isl_tab_free(cgbr
->cone
);
3236 isl_tab_free(cgbr
->tab
);
3240 static void context_gbr_discard(void *save
)
3242 struct isl_gbr_tab_undo
*snap
= (struct isl_gbr_tab_undo
*)save
;
3246 static int context_gbr_is_ok(struct isl_context
*context
)
3248 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3252 static void context_gbr_invalidate(struct isl_context
*context
)
3254 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3255 isl_tab_free(cgbr
->tab
);
3259 static void context_gbr_free(struct isl_context
*context
)
3261 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3262 isl_tab_free(cgbr
->tab
);
3263 isl_tab_free(cgbr
->shifted
);
3264 isl_tab_free(cgbr
->cone
);
3268 struct isl_context_op isl_context_gbr_op
= {
3269 context_gbr_detect_nonnegative_parameters
,
3270 context_gbr_peek_basic_set
,
3271 context_gbr_peek_tab
,
3273 context_gbr_add_ineq
,
3274 context_gbr_ineq_sign
,
3275 context_gbr_test_ineq
,
3276 context_gbr_get_div
,
3277 context_gbr_add_div
,
3278 context_gbr_detect_equalities
,
3279 context_gbr_best_split
,
3280 context_gbr_is_empty
,
3283 context_gbr_restore
,
3284 context_gbr_discard
,
3285 context_gbr_invalidate
,
3289 static struct isl_context
*isl_context_gbr_alloc(struct isl_basic_set
*dom
)
3291 struct isl_context_gbr
*cgbr
;
3296 cgbr
= isl_calloc_type(dom
->ctx
, struct isl_context_gbr
);
3300 cgbr
->context
.op
= &isl_context_gbr_op
;
3302 cgbr
->shifted
= NULL
;
3304 cgbr
->tab
= isl_tab_from_basic_set(dom
, 1);
3305 cgbr
->tab
= isl_tab_init_samples(cgbr
->tab
);
3308 check_gbr_integer_feasible(cgbr
);
3310 return &cgbr
->context
;
3312 cgbr
->context
.op
->free(&cgbr
->context
);
3316 static struct isl_context
*isl_context_alloc(struct isl_basic_set
*dom
)
3321 if (dom
->ctx
->opt
->context
== ISL_CONTEXT_LEXMIN
)
3322 return isl_context_lex_alloc(dom
);
3324 return isl_context_gbr_alloc(dom
);
3327 /* Construct an isl_sol_map structure for accumulating the solution.
3328 * If track_empty is set, then we also keep track of the parts
3329 * of the context where there is no solution.
3330 * If max is set, then we are solving a maximization, rather than
3331 * a minimization problem, which means that the variables in the
3332 * tableau have value "M - x" rather than "M + x".
3334 static struct isl_sol
*sol_map_init(struct isl_basic_map
*bmap
,
3335 struct isl_basic_set
*dom
, int track_empty
, int max
)
3337 struct isl_sol_map
*sol_map
= NULL
;
3342 sol_map
= isl_calloc_type(bmap
->ctx
, struct isl_sol_map
);
3346 sol_map
->sol
.rational
= ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
3347 sol_map
->sol
.dec_level
.callback
.run
= &sol_dec_level_wrap
;
3348 sol_map
->sol
.dec_level
.sol
= &sol_map
->sol
;
3349 sol_map
->sol
.max
= max
;
3350 sol_map
->sol
.n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
3351 sol_map
->sol
.add
= &sol_map_add_wrap
;
3352 sol_map
->sol
.add_empty
= track_empty
? &sol_map_add_empty_wrap
: NULL
;
3353 sol_map
->sol
.free
= &sol_map_free_wrap
;
3354 sol_map
->map
= isl_map_alloc_space(isl_basic_map_get_space(bmap
), 1,
3359 sol_map
->sol
.context
= isl_context_alloc(dom
);
3360 if (!sol_map
->sol
.context
)
3364 sol_map
->empty
= isl_set_alloc_space(isl_basic_set_get_space(dom
),
3365 1, ISL_SET_DISJOINT
);
3366 if (!sol_map
->empty
)
3370 isl_basic_set_free(dom
);
3371 return &sol_map
->sol
;
3373 isl_basic_set_free(dom
);
3374 sol_map_free(sol_map
);
3378 /* Check whether all coefficients of (non-parameter) variables
3379 * are non-positive, meaning that no pivots can be performed on the row.
3381 static int is_critical(struct isl_tab
*tab
, int row
)
3384 unsigned off
= 2 + tab
->M
;
3386 for (j
= tab
->n_dead
; j
< tab
->n_col
; ++j
) {
3387 if (tab
->col_var
[j
] >= 0 &&
3388 (tab
->col_var
[j
] < tab
->n_param
||
3389 tab
->col_var
[j
] >= tab
->n_var
- tab
->n_div
))
3392 if (isl_int_is_pos(tab
->mat
->row
[row
][off
+ j
]))
3399 /* Check whether the inequality represented by vec is strict over the integers,
3400 * i.e., there are no integer values satisfying the constraint with
3401 * equality. This happens if the gcd of the coefficients is not a divisor
3402 * of the constant term. If so, scale the constraint down by the gcd
3403 * of the coefficients.
3405 static int is_strict(struct isl_vec
*vec
)
3411 isl_seq_gcd(vec
->el
+ 1, vec
->size
- 1, &gcd
);
3412 if (!isl_int_is_one(gcd
)) {
3413 strict
= !isl_int_is_divisible_by(vec
->el
[0], gcd
);
3414 isl_int_fdiv_q(vec
->el
[0], vec
->el
[0], gcd
);
3415 isl_seq_scale_down(vec
->el
+ 1, vec
->el
+ 1, gcd
, vec
->size
-1);
3422 /* Determine the sign of the given row of the main tableau.
3423 * The result is one of
3424 * isl_tab_row_pos: always non-negative; no pivot needed
3425 * isl_tab_row_neg: always non-positive; pivot
3426 * isl_tab_row_any: can be both positive and negative; split
3428 * We first handle some simple cases
3429 * - the row sign may be known already
3430 * - the row may be obviously non-negative
3431 * - the parametric constant may be equal to that of another row
3432 * for which we know the sign. This sign will be either "pos" or
3433 * "any". If it had been "neg" then we would have pivoted before.
3435 * If none of these cases hold, we check the value of the row for each
3436 * of the currently active samples. Based on the signs of these values
3437 * we make an initial determination of the sign of the row.
3439 * all zero -> unk(nown)
3440 * all non-negative -> pos
3441 * all non-positive -> neg
3442 * both negative and positive -> all
3444 * If we end up with "all", we are done.
3445 * Otherwise, we perform a check for positive and/or negative
3446 * values as follows.
3448 * samples neg unk pos
3454 * There is no special sign for "zero", because we can usually treat zero
3455 * as either non-negative or non-positive, whatever works out best.
3456 * However, if the row is "critical", meaning that pivoting is impossible
3457 * then we don't want to limp zero with the non-positive case, because
3458 * then we we would lose the solution for those values of the parameters
3459 * where the value of the row is zero. Instead, we treat 0 as non-negative
3460 * ensuring a split if the row can attain both zero and negative values.
3461 * The same happens when the original constraint was one that could not
3462 * be satisfied with equality by any integer values of the parameters.
3463 * In this case, we normalize the constraint, but then a value of zero
3464 * for the normalized constraint is actually a positive value for the
3465 * original constraint, so again we need to treat zero as non-negative.
3466 * In both these cases, we have the following decision tree instead:
3468 * all non-negative -> pos
3469 * all negative -> neg
3470 * both negative and non-negative -> all
3478 static enum isl_tab_row_sign
row_sign(struct isl_tab
*tab
,
3479 struct isl_sol
*sol
, int row
)
3481 struct isl_vec
*ineq
= NULL
;
3482 enum isl_tab_row_sign res
= isl_tab_row_unknown
;
3487 if (tab
->row_sign
[row
] != isl_tab_row_unknown
)
3488 return tab
->row_sign
[row
];
3489 if (is_obviously_nonneg(tab
, row
))
3490 return isl_tab_row_pos
;
3491 for (row2
= tab
->n_redundant
; row2
< tab
->n_row
; ++row2
) {
3492 if (tab
->row_sign
[row2
] == isl_tab_row_unknown
)
3494 if (identical_parameter_line(tab
, row
, row2
))
3495 return tab
->row_sign
[row2
];
3498 critical
= is_critical(tab
, row
);
3500 ineq
= get_row_parameter_ineq(tab
, row
);
3504 strict
= is_strict(ineq
);
3506 res
= sol
->context
->op
->ineq_sign(sol
->context
, ineq
->el
,
3507 critical
|| strict
);
3509 if (res
== isl_tab_row_unknown
|| res
== isl_tab_row_pos
) {
3510 /* test for negative values */
3512 isl_seq_neg(ineq
->el
, ineq
->el
, ineq
->size
);
3513 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3515 feasible
= sol
->context
->op
->test_ineq(sol
->context
, ineq
->el
);
3519 res
= isl_tab_row_pos
;
3521 res
= (res
== isl_tab_row_unknown
) ? isl_tab_row_neg
3523 if (res
== isl_tab_row_neg
) {
3524 isl_seq_neg(ineq
->el
, ineq
->el
, ineq
->size
);
3525 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3529 if (res
== isl_tab_row_neg
) {
3530 /* test for positive values */
3532 if (!critical
&& !strict
)
3533 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3535 feasible
= sol
->context
->op
->test_ineq(sol
->context
, ineq
->el
);
3539 res
= isl_tab_row_any
;
3546 return isl_tab_row_unknown
;
3549 static void find_solutions(struct isl_sol
*sol
, struct isl_tab
*tab
);
3551 /* Find solutions for values of the parameters that satisfy the given
3554 * We currently take a snapshot of the context tableau that is reset
3555 * when we return from this function, while we make a copy of the main
3556 * tableau, leaving the original main tableau untouched.
3557 * These are fairly arbitrary choices. Making a copy also of the context
3558 * tableau would obviate the need to undo any changes made to it later,
3559 * while taking a snapshot of the main tableau could reduce memory usage.
3560 * If we were to switch to taking a snapshot of the main tableau,
3561 * we would have to keep in mind that we need to save the row signs
3562 * and that we need to do this before saving the current basis
3563 * such that the basis has been restore before we restore the row signs.
3565 static void find_in_pos(struct isl_sol
*sol
, struct isl_tab
*tab
, isl_int
*ineq
)
3571 saved
= sol
->context
->op
->save(sol
->context
);
3573 tab
= isl_tab_dup(tab
);
3577 sol
->context
->op
->add_ineq(sol
->context
, ineq
, 0, 1);
3579 find_solutions(sol
, tab
);
3582 sol
->context
->op
->restore(sol
->context
, saved
);
3584 sol
->context
->op
->discard(saved
);
3590 /* Record the absence of solutions for those values of the parameters
3591 * that do not satisfy the given inequality with equality.
3593 static void no_sol_in_strict(struct isl_sol
*sol
,
3594 struct isl_tab
*tab
, struct isl_vec
*ineq
)
3599 if (!sol
->context
|| sol
->error
)
3601 saved
= sol
->context
->op
->save(sol
->context
);
3603 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3605 sol
->context
->op
->add_ineq(sol
->context
, ineq
->el
, 1, 0);
3614 isl_int_add_ui(ineq
->el
[0], ineq
->el
[0], 1);
3616 sol
->context
->op
->restore(sol
->context
, saved
);
3622 /* Compute the lexicographic minimum of the set represented by the main
3623 * tableau "tab" within the context "sol->context_tab".
3624 * On entry the sample value of the main tableau is lexicographically
3625 * less than or equal to this lexicographic minimum.
3626 * Pivots are performed until a feasible point is found, which is then
3627 * necessarily equal to the minimum, or until the tableau is found to
3628 * be infeasible. Some pivots may need to be performed for only some
3629 * feasible values of the context tableau. If so, the context tableau
3630 * is split into a part where the pivot is needed and a part where it is not.
3632 * Whenever we enter the main loop, the main tableau is such that no
3633 * "obvious" pivots need to be performed on it, where "obvious" means
3634 * that the given row can be seen to be negative without looking at
3635 * the context tableau. In particular, for non-parametric problems,
3636 * no pivots need to be performed on the main tableau.
3637 * The caller of find_solutions is responsible for making this property
3638 * hold prior to the first iteration of the loop, while restore_lexmin
3639 * is called before every other iteration.
3641 * Inside the main loop, we first examine the signs of the rows of
3642 * the main tableau within the context of the context tableau.
3643 * If we find a row that is always non-positive for all values of
3644 * the parameters satisfying the context tableau and negative for at
3645 * least one value of the parameters, we perform the appropriate pivot
3646 * and start over. An exception is the case where no pivot can be
3647 * performed on the row. In this case, we require that the sign of
3648 * the row is negative for all values of the parameters (rather than just
3649 * non-positive). This special case is handled inside row_sign, which
3650 * will say that the row can have any sign if it determines that it can
3651 * attain both negative and zero values.
3653 * If we can't find a row that always requires a pivot, but we can find
3654 * one or more rows that require a pivot for some values of the parameters
3655 * (i.e., the row can attain both positive and negative signs), then we split
3656 * the context tableau into two parts, one where we force the sign to be
3657 * non-negative and one where we force is to be negative.
3658 * The non-negative part is handled by a recursive call (through find_in_pos).
3659 * Upon returning from this call, we continue with the negative part and
3660 * perform the required pivot.
3662 * If no such rows can be found, all rows are non-negative and we have
3663 * found a (rational) feasible point. If we only wanted a rational point
3665 * Otherwise, we check if all values of the sample point of the tableau
3666 * are integral for the variables. If so, we have found the minimal
3667 * integral point and we are done.
3668 * If the sample point is not integral, then we need to make a distinction
3669 * based on whether the constant term is non-integral or the coefficients
3670 * of the parameters. Furthermore, in order to decide how to handle
3671 * the non-integrality, we also need to know whether the coefficients
3672 * of the other columns in the tableau are integral. This leads
3673 * to the following table. The first two rows do not correspond
3674 * to a non-integral sample point and are only mentioned for completeness.
3676 * constant parameters other
3679 * int int rat | -> no problem
3681 * rat int int -> fail
3683 * rat int rat -> cut
3686 * rat rat rat | -> parametric cut
3689 * rat rat int | -> split context
3691 * If the parametric constant is completely integral, then there is nothing
3692 * to be done. If the constant term is non-integral, but all the other
3693 * coefficient are integral, then there is nothing that can be done
3694 * and the tableau has no integral solution.
3695 * If, on the other hand, one or more of the other columns have rational
3696 * coefficients, but the parameter coefficients are all integral, then
3697 * we can perform a regular (non-parametric) cut.
3698 * Finally, if there is any parameter coefficient that is non-integral,
3699 * then we need to involve the context tableau. There are two cases here.
3700 * If at least one other column has a rational coefficient, then we
3701 * can perform a parametric cut in the main tableau by adding a new
3702 * integer division in the context tableau.
3703 * If all other columns have integral coefficients, then we need to
3704 * enforce that the rational combination of parameters (c + \sum a_i y_i)/m
3705 * is always integral. We do this by introducing an integer division
3706 * q = floor((c + \sum a_i y_i)/m) and stipulating that its argument should
3707 * always be integral in the context tableau, i.e., m q = c + \sum a_i y_i.
3708 * Since q is expressed in the tableau as
3709 * c + \sum a_i y_i - m q >= 0
3710 * -c - \sum a_i y_i + m q + m - 1 >= 0
3711 * it is sufficient to add the inequality
3712 * -c - \sum a_i y_i + m q >= 0
3713 * In the part of the context where this inequality does not hold, the
3714 * main tableau is marked as being empty.
3716 static void find_solutions(struct isl_sol
*sol
, struct isl_tab
*tab
)
3718 struct isl_context
*context
;
3721 if (!tab
|| sol
->error
)
3724 context
= sol
->context
;
3728 if (context
->op
->is_empty(context
))
3731 for (r
= 0; r
>= 0 && tab
&& !tab
->empty
; r
= restore_lexmin(tab
)) {
3734 enum isl_tab_row_sign sgn
;
3738 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
3739 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
3741 sgn
= row_sign(tab
, sol
, row
);
3744 tab
->row_sign
[row
] = sgn
;
3745 if (sgn
== isl_tab_row_any
)
3747 if (sgn
== isl_tab_row_any
&& split
== -1)
3749 if (sgn
== isl_tab_row_neg
)
3752 if (row
< tab
->n_row
)
3755 struct isl_vec
*ineq
;
3757 split
= context
->op
->best_split(context
, tab
);
3760 ineq
= get_row_parameter_ineq(tab
, split
);
3764 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
3765 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
3767 if (tab
->row_sign
[row
] == isl_tab_row_any
)
3768 tab
->row_sign
[row
] = isl_tab_row_unknown
;
3770 tab
->row_sign
[split
] = isl_tab_row_pos
;
3772 find_in_pos(sol
, tab
, ineq
->el
);
3773 tab
->row_sign
[split
] = isl_tab_row_neg
;
3775 isl_seq_neg(ineq
->el
, ineq
->el
, ineq
->size
);
3776 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3778 context
->op
->add_ineq(context
, ineq
->el
, 0, 1);
3786 row
= first_non_integer_row(tab
, &flags
);
3789 if (ISL_FL_ISSET(flags
, I_PAR
)) {
3790 if (ISL_FL_ISSET(flags
, I_VAR
)) {
3791 if (isl_tab_mark_empty(tab
) < 0)
3795 row
= add_cut(tab
, row
);
3796 } else if (ISL_FL_ISSET(flags
, I_VAR
)) {
3797 struct isl_vec
*div
;
3798 struct isl_vec
*ineq
;
3800 div
= get_row_split_div(tab
, row
);
3803 d
= context
->op
->get_div(context
, tab
, div
);
3807 ineq
= ineq_for_div(context
->op
->peek_basic_set(context
), d
);
3811 no_sol_in_strict(sol
, tab
, ineq
);
3812 isl_seq_neg(ineq
->el
, ineq
->el
, ineq
->size
);
3813 context
->op
->add_ineq(context
, ineq
->el
, 1, 1);
3815 if (sol
->error
|| !context
->op
->is_ok(context
))
3817 tab
= set_row_cst_to_div(tab
, row
, d
);
3818 if (context
->op
->is_empty(context
))
3821 row
= add_parametric_cut(tab
, row
, context
);
3836 /* Compute the lexicographic minimum of the set represented by the main
3837 * tableau "tab" within the context "sol->context_tab".
3839 * As a preprocessing step, we first transfer all the purely parametric
3840 * equalities from the main tableau to the context tableau, i.e.,
3841 * parameters that have been pivoted to a row.
3842 * These equalities are ignored by the main algorithm, because the
3843 * corresponding rows may not be marked as being non-negative.
3844 * In parts of the context where the added equality does not hold,
3845 * the main tableau is marked as being empty.
3847 static void find_solutions_main(struct isl_sol
*sol
, struct isl_tab
*tab
)
3856 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
3860 if (tab
->row_var
[row
] < 0)
3862 if (tab
->row_var
[row
] >= tab
->n_param
&&
3863 tab
->row_var
[row
] < tab
->n_var
- tab
->n_div
)
3865 if (tab
->row_var
[row
] < tab
->n_param
)
3866 p
= tab
->row_var
[row
];
3868 p
= tab
->row_var
[row
]
3869 + tab
->n_param
- (tab
->n_var
- tab
->n_div
);
3871 eq
= isl_vec_alloc(tab
->mat
->ctx
, 1+tab
->n_param
+tab
->n_div
);
3874 get_row_parameter_line(tab
, row
, eq
->el
);
3875 isl_int_neg(eq
->el
[1 + p
], tab
->mat
->row
[row
][0]);
3876 eq
= isl_vec_normalize(eq
);
3879 no_sol_in_strict(sol
, tab
, eq
);
3881 isl_seq_neg(eq
->el
, eq
->el
, eq
->size
);
3883 no_sol_in_strict(sol
, tab
, eq
);
3884 isl_seq_neg(eq
->el
, eq
->el
, eq
->size
);
3886 sol
->context
->op
->add_eq(sol
->context
, eq
->el
, 1, 1);
3890 if (isl_tab_mark_redundant(tab
, row
) < 0)
3893 if (sol
->context
->op
->is_empty(sol
->context
))
3896 row
= tab
->n_redundant
- 1;
3899 find_solutions(sol
, tab
);
3910 /* Check if integer division "div" of "dom" also occurs in "bmap".
3911 * If so, return its position within the divs.
3912 * If not, return -1.
3914 static int find_context_div(struct isl_basic_map
*bmap
,
3915 struct isl_basic_set
*dom
, unsigned div
)
3918 unsigned b_dim
= isl_space_dim(bmap
->dim
, isl_dim_all
);
3919 unsigned d_dim
= isl_space_dim(dom
->dim
, isl_dim_all
);
3921 if (isl_int_is_zero(dom
->div
[div
][0]))
3923 if (isl_seq_first_non_zero(dom
->div
[div
] + 2 + d_dim
, dom
->n_div
) != -1)
3926 for (i
= 0; i
< bmap
->n_div
; ++i
) {
3927 if (isl_int_is_zero(bmap
->div
[i
][0]))
3929 if (isl_seq_first_non_zero(bmap
->div
[i
] + 2 + d_dim
,
3930 (b_dim
- d_dim
) + bmap
->n_div
) != -1)
3932 if (isl_seq_eq(bmap
->div
[i
], dom
->div
[div
], 2 + d_dim
))
3938 /* The correspondence between the variables in the main tableau,
3939 * the context tableau, and the input map and domain is as follows.
3940 * The first n_param and the last n_div variables of the main tableau
3941 * form the variables of the context tableau.
3942 * In the basic map, these n_param variables correspond to the
3943 * parameters and the input dimensions. In the domain, they correspond
3944 * to the parameters and the set dimensions.
3945 * The n_div variables correspond to the integer divisions in the domain.
3946 * To ensure that everything lines up, we may need to copy some of the
3947 * integer divisions of the domain to the map. These have to be placed
3948 * in the same order as those in the context and they have to be placed
3949 * after any other integer divisions that the map may have.
3950 * This function performs the required reordering.
3952 static struct isl_basic_map
*align_context_divs(struct isl_basic_map
*bmap
,
3953 struct isl_basic_set
*dom
)
3959 for (i
= 0; i
< dom
->n_div
; ++i
)
3960 if (find_context_div(bmap
, dom
, i
) != -1)
3962 other
= bmap
->n_div
- common
;
3963 if (dom
->n_div
- common
> 0) {
3964 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
3965 dom
->n_div
- common
, 0, 0);
3969 for (i
= 0; i
< dom
->n_div
; ++i
) {
3970 int pos
= find_context_div(bmap
, dom
, i
);
3972 pos
= isl_basic_map_alloc_div(bmap
);
3975 isl_int_set_si(bmap
->div
[pos
][0], 0);
3977 if (pos
!= other
+ i
)
3978 isl_basic_map_swap_div(bmap
, pos
, other
+ i
);
3982 isl_basic_map_free(bmap
);
3986 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
3987 * some obvious symmetries.
3989 * We make sure the divs in the domain are properly ordered,
3990 * because they will be added one by one in the given order
3991 * during the construction of the solution map.
3993 static struct isl_sol
*basic_map_partial_lexopt_base(
3994 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
3995 __isl_give isl_set
**empty
, int max
,
3996 struct isl_sol
*(*init
)(__isl_keep isl_basic_map
*bmap
,
3997 __isl_take isl_basic_set
*dom
, int track_empty
, int max
))
3999 struct isl_tab
*tab
;
4000 struct isl_sol
*sol
= NULL
;
4001 struct isl_context
*context
;
4004 dom
= isl_basic_set_order_divs(dom
);
4005 bmap
= align_context_divs(bmap
, dom
);
4007 sol
= init(bmap
, dom
, !!empty
, max
);
4011 context
= sol
->context
;
4012 if (isl_basic_set_plain_is_empty(context
->op
->peek_basic_set(context
)))
4014 else if (isl_basic_map_plain_is_empty(bmap
)) {
4017 isl_basic_set_copy(context
->op
->peek_basic_set(context
)));
4019 tab
= tab_for_lexmin(bmap
,
4020 context
->op
->peek_basic_set(context
), 1, max
);
4021 tab
= context
->op
->detect_nonnegative_parameters(context
, tab
);
4022 find_solutions_main(sol
, tab
);
4027 isl_basic_map_free(bmap
);
4031 isl_basic_map_free(bmap
);
4035 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
4036 * some obvious symmetries.
4038 * We call basic_map_partial_lexopt_base and extract the results.
4040 static __isl_give isl_map
*basic_map_partial_lexopt_base_map(
4041 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
4042 __isl_give isl_set
**empty
, int max
)
4044 isl_map
*result
= NULL
;
4045 struct isl_sol
*sol
;
4046 struct isl_sol_map
*sol_map
;
4048 sol
= basic_map_partial_lexopt_base(bmap
, dom
, empty
, max
,
4052 sol_map
= (struct isl_sol_map
*) sol
;
4054 result
= isl_map_copy(sol_map
->map
);
4056 *empty
= isl_set_copy(sol_map
->empty
);
4057 sol_free(&sol_map
->sol
);
4061 /* Structure used during detection of parallel constraints.
4062 * n_in: number of "input" variables: isl_dim_param + isl_dim_in
4063 * n_out: number of "output" variables: isl_dim_out + isl_dim_div
4064 * val: the coefficients of the output variables
4066 struct isl_constraint_equal_info
{
4067 isl_basic_map
*bmap
;
4073 /* Check whether the coefficients of the output variables
4074 * of the constraint in "entry" are equal to info->val.
4076 static int constraint_equal(const void *entry
, const void *val
)
4078 isl_int
**row
= (isl_int
**)entry
;
4079 const struct isl_constraint_equal_info
*info
= val
;
4081 return isl_seq_eq((*row
) + 1 + info
->n_in
, info
->val
, info
->n_out
);
4084 /* Check whether "bmap" has a pair of constraints that have
4085 * the same coefficients for the output variables.
4086 * Note that the coefficients of the existentially quantified
4087 * variables need to be zero since the existentially quantified
4088 * of the result are usually not the same as those of the input.
4089 * the isl_dim_out and isl_dim_div dimensions.
4090 * If so, return 1 and return the row indices of the two constraints
4091 * in *first and *second.
4093 static int parallel_constraints(__isl_keep isl_basic_map
*bmap
,
4094 int *first
, int *second
)
4097 isl_ctx
*ctx
= isl_basic_map_get_ctx(bmap
);
4098 struct isl_hash_table
*table
= NULL
;
4099 struct isl_hash_table_entry
*entry
;
4100 struct isl_constraint_equal_info info
;
4104 ctx
= isl_basic_map_get_ctx(bmap
);
4105 table
= isl_hash_table_alloc(ctx
, bmap
->n_ineq
);
4109 info
.n_in
= isl_basic_map_dim(bmap
, isl_dim_param
) +
4110 isl_basic_map_dim(bmap
, isl_dim_in
);
4112 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4113 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4114 info
.n_out
= n_out
+ n_div
;
4115 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
4118 info
.val
= bmap
->ineq
[i
] + 1 + info
.n_in
;
4119 if (isl_seq_first_non_zero(info
.val
, n_out
) < 0)
4121 if (isl_seq_first_non_zero(info
.val
+ n_out
, n_div
) >= 0)
4123 hash
= isl_seq_get_hash(info
.val
, info
.n_out
);
4124 entry
= isl_hash_table_find(ctx
, table
, hash
,
4125 constraint_equal
, &info
, 1);
4130 entry
->data
= &bmap
->ineq
[i
];
4133 if (i
< bmap
->n_ineq
) {
4134 *first
= ((isl_int
**)entry
->data
) - bmap
->ineq
;
4138 isl_hash_table_free(ctx
, table
);
4140 return i
< bmap
->n_ineq
;
4142 isl_hash_table_free(ctx
, table
);
4146 /* Given a set of upper bounds in "var", add constraints to "bset"
4147 * that make the i-th bound smallest.
4149 * In particular, if there are n bounds b_i, then add the constraints
4151 * b_i <= b_j for j > i
4152 * b_i < b_j for j < i
4154 static __isl_give isl_basic_set
*select_minimum(__isl_take isl_basic_set
*bset
,
4155 __isl_keep isl_mat
*var
, int i
)
4160 ctx
= isl_mat_get_ctx(var
);
4162 for (j
= 0; j
< var
->n_row
; ++j
) {
4165 k
= isl_basic_set_alloc_inequality(bset
);
4168 isl_seq_combine(bset
->ineq
[k
], ctx
->one
, var
->row
[j
],
4169 ctx
->negone
, var
->row
[i
], var
->n_col
);
4170 isl_int_set_si(bset
->ineq
[k
][var
->n_col
], 0);
4172 isl_int_sub_ui(bset
->ineq
[k
][0], bset
->ineq
[k
][0], 1);
4175 bset
= isl_basic_set_finalize(bset
);
4179 isl_basic_set_free(bset
);
4183 /* Given a set of upper bounds on the last "input" variable m,
4184 * construct a set that assigns the minimal upper bound to m, i.e.,
4185 * construct a set that divides the space into cells where one
4186 * of the upper bounds is smaller than all the others and assign
4187 * this upper bound to m.
4189 * In particular, if there are n bounds b_i, then the result
4190 * consists of n basic sets, each one of the form
4193 * b_i <= b_j for j > i
4194 * b_i < b_j for j < i
4196 static __isl_give isl_set
*set_minimum(__isl_take isl_space
*dim
,
4197 __isl_take isl_mat
*var
)
4200 isl_basic_set
*bset
= NULL
;
4202 isl_set
*set
= NULL
;
4207 ctx
= isl_space_get_ctx(dim
);
4208 set
= isl_set_alloc_space(isl_space_copy(dim
),
4209 var
->n_row
, ISL_SET_DISJOINT
);
4211 for (i
= 0; i
< var
->n_row
; ++i
) {
4212 bset
= isl_basic_set_alloc_space(isl_space_copy(dim
), 0,
4214 k
= isl_basic_set_alloc_equality(bset
);
4217 isl_seq_cpy(bset
->eq
[k
], var
->row
[i
], var
->n_col
);
4218 isl_int_set_si(bset
->eq
[k
][var
->n_col
], -1);
4219 bset
= select_minimum(bset
, var
, i
);
4220 set
= isl_set_add_basic_set(set
, bset
);
4223 isl_space_free(dim
);
4227 isl_basic_set_free(bset
);
4229 isl_space_free(dim
);
4234 /* Given that the last input variable of "bmap" represents the minimum
4235 * of the bounds in "cst", check whether we need to split the domain
4236 * based on which bound attains the minimum.
4238 * A split is needed when the minimum appears in an integer division
4239 * or in an equality. Otherwise, it is only needed if it appears in
4240 * an upper bound that is different from the upper bounds on which it
4243 static int need_split_basic_map(__isl_keep isl_basic_map
*bmap
,
4244 __isl_keep isl_mat
*cst
)
4250 pos
= cst
->n_col
- 1;
4251 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4253 for (i
= 0; i
< bmap
->n_div
; ++i
)
4254 if (!isl_int_is_zero(bmap
->div
[i
][2 + pos
]))
4257 for (i
= 0; i
< bmap
->n_eq
; ++i
)
4258 if (!isl_int_is_zero(bmap
->eq
[i
][1 + pos
]))
4261 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
4262 if (isl_int_is_nonneg(bmap
->ineq
[i
][1 + pos
]))
4264 if (!isl_int_is_negone(bmap
->ineq
[i
][1 + pos
]))
4266 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + pos
+ 1,
4267 total
- pos
- 1) >= 0)
4270 for (j
= 0; j
< cst
->n_row
; ++j
)
4271 if (isl_seq_eq(bmap
->ineq
[i
], cst
->row
[j
], cst
->n_col
))
4273 if (j
>= cst
->n_row
)
4280 /* Given that the last set variable of "bset" represents the minimum
4281 * of the bounds in "cst", check whether we need to split the domain
4282 * based on which bound attains the minimum.
4284 * We simply call need_split_basic_map here. This is safe because
4285 * the position of the minimum is computed from "cst" and not
4288 static int need_split_basic_set(__isl_keep isl_basic_set
*bset
,
4289 __isl_keep isl_mat
*cst
)
4291 return need_split_basic_map((isl_basic_map
*)bset
, cst
);
4294 /* Given that the last set variable of "set" represents the minimum
4295 * of the bounds in "cst", check whether we need to split the domain
4296 * based on which bound attains the minimum.
4298 static int need_split_set(__isl_keep isl_set
*set
, __isl_keep isl_mat
*cst
)
4302 for (i
= 0; i
< set
->n
; ++i
)
4303 if (need_split_basic_set(set
->p
[i
], cst
))
4309 /* Given a set of which the last set variable is the minimum
4310 * of the bounds in "cst", split each basic set in the set
4311 * in pieces where one of the bounds is (strictly) smaller than the others.
4312 * This subdivision is given in "min_expr".
4313 * The variable is subsequently projected out.
4315 * We only do the split when it is needed.
4316 * For example if the last input variable m = min(a,b) and the only
4317 * constraints in the given basic set are lower bounds on m,
4318 * i.e., l <= m = min(a,b), then we can simply project out m
4319 * to obtain l <= a and l <= b, without having to split on whether
4320 * m is equal to a or b.
4322 static __isl_give isl_set
*split(__isl_take isl_set
*empty
,
4323 __isl_take isl_set
*min_expr
, __isl_take isl_mat
*cst
)
4330 if (!empty
|| !min_expr
|| !cst
)
4333 n_in
= isl_set_dim(empty
, isl_dim_set
);
4334 dim
= isl_set_get_space(empty
);
4335 dim
= isl_space_drop_dims(dim
, isl_dim_set
, n_in
- 1, 1);
4336 res
= isl_set_empty(dim
);
4338 for (i
= 0; i
< empty
->n
; ++i
) {
4341 set
= isl_set_from_basic_set(isl_basic_set_copy(empty
->p
[i
]));
4342 if (need_split_basic_set(empty
->p
[i
], cst
))
4343 set
= isl_set_intersect(set
, isl_set_copy(min_expr
));
4344 set
= isl_set_remove_dims(set
, isl_dim_set
, n_in
- 1, 1);
4346 res
= isl_set_union_disjoint(res
, set
);
4349 isl_set_free(empty
);
4350 isl_set_free(min_expr
);
4354 isl_set_free(empty
);
4355 isl_set_free(min_expr
);
4360 /* Given a map of which the last input variable is the minimum
4361 * of the bounds in "cst", split each basic set in the set
4362 * in pieces where one of the bounds is (strictly) smaller than the others.
4363 * This subdivision is given in "min_expr".
4364 * The variable is subsequently projected out.
4366 * The implementation is essentially the same as that of "split".
4368 static __isl_give isl_map
*split_domain(__isl_take isl_map
*opt
,
4369 __isl_take isl_set
*min_expr
, __isl_take isl_mat
*cst
)
4376 if (!opt
|| !min_expr
|| !cst
)
4379 n_in
= isl_map_dim(opt
, isl_dim_in
);
4380 dim
= isl_map_get_space(opt
);
4381 dim
= isl_space_drop_dims(dim
, isl_dim_in
, n_in
- 1, 1);
4382 res
= isl_map_empty(dim
);
4384 for (i
= 0; i
< opt
->n
; ++i
) {
4387 map
= isl_map_from_basic_map(isl_basic_map_copy(opt
->p
[i
]));
4388 if (need_split_basic_map(opt
->p
[i
], cst
))
4389 map
= isl_map_intersect_domain(map
,
4390 isl_set_copy(min_expr
));
4391 map
= isl_map_remove_dims(map
, isl_dim_in
, n_in
- 1, 1);
4393 res
= isl_map_union_disjoint(res
, map
);
4397 isl_set_free(min_expr
);
4402 isl_set_free(min_expr
);
4407 static __isl_give isl_map
*basic_map_partial_lexopt(
4408 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
4409 __isl_give isl_set
**empty
, int max
);
4414 isl_pw_multi_aff
*pma
;
4417 /* This function is called from basic_map_partial_lexopt_symm.
4418 * The last variable of "bmap" and "dom" corresponds to the minimum
4419 * of the bounds in "cst". "map_space" is the space of the original
4420 * input relation (of basic_map_partial_lexopt_symm) and "set_space"
4421 * is the space of the original domain.
4423 * We recursively call basic_map_partial_lexopt and then plug in
4424 * the definition of the minimum in the result.
4426 static __isl_give
union isl_lex_res
basic_map_partial_lexopt_symm_map_core(
4427 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
4428 __isl_give isl_set
**empty
, int max
, __isl_take isl_mat
*cst
,
4429 __isl_take isl_space
*map_space
, __isl_take isl_space
*set_space
)
4433 union isl_lex_res res
;
4435 min_expr
= set_minimum(isl_basic_set_get_space(dom
), isl_mat_copy(cst
));
4437 opt
= basic_map_partial_lexopt(bmap
, dom
, empty
, max
);
4440 *empty
= split(*empty
,
4441 isl_set_copy(min_expr
), isl_mat_copy(cst
));
4442 *empty
= isl_set_reset_space(*empty
, set_space
);
4445 opt
= split_domain(opt
, min_expr
, cst
);
4446 opt
= isl_map_reset_space(opt
, map_space
);
4452 /* Given a basic map with at least two parallel constraints (as found
4453 * by the function parallel_constraints), first look for more constraints
4454 * parallel to the two constraint and replace the found list of parallel
4455 * constraints by a single constraint with as "input" part the minimum
4456 * of the input parts of the list of constraints. Then, recursively call
4457 * basic_map_partial_lexopt (possibly finding more parallel constraints)
4458 * and plug in the definition of the minimum in the result.
4460 * More specifically, given a set of constraints
4464 * Replace this set by a single constraint
4468 * with u a new parameter with constraints
4472 * Any solution to the new system is also a solution for the original system
4475 * a x >= -u >= -b_i(p)
4477 * Moreover, m = min_i(b_i(p)) satisfies the constraints on u and can
4478 * therefore be plugged into the solution.
4480 static union isl_lex_res
basic_map_partial_lexopt_symm(
4481 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
4482 __isl_give isl_set
**empty
, int max
, int first
, int second
,
4483 __isl_give
union isl_lex_res (*core
)(__isl_take isl_basic_map
*bmap
,
4484 __isl_take isl_basic_set
*dom
,
4485 __isl_give isl_set
**empty
,
4486 int max
, __isl_take isl_mat
*cst
,
4487 __isl_take isl_space
*map_space
,
4488 __isl_take isl_space
*set_space
))
4492 unsigned n_in
, n_out
, n_div
;
4494 isl_vec
*var
= NULL
;
4495 isl_mat
*cst
= NULL
;
4496 isl_space
*map_space
, *set_space
;
4497 union isl_lex_res res
;
4499 map_space
= isl_basic_map_get_space(bmap
);
4500 set_space
= empty
? isl_basic_set_get_space(dom
) : NULL
;
4502 n_in
= isl_basic_map_dim(bmap
, isl_dim_param
) +
4503 isl_basic_map_dim(bmap
, isl_dim_in
);
4504 n_out
= isl_basic_map_dim(bmap
, isl_dim_all
) - n_in
;
4506 ctx
= isl_basic_map_get_ctx(bmap
);
4507 list
= isl_alloc_array(ctx
, int, bmap
->n_ineq
);
4508 var
= isl_vec_alloc(ctx
, n_out
);
4514 isl_seq_cpy(var
->el
, bmap
->ineq
[first
] + 1 + n_in
, n_out
);
4515 for (i
= second
+ 1, n
= 2; i
< bmap
->n_ineq
; ++i
) {
4516 if (isl_seq_eq(var
->el
, bmap
->ineq
[i
] + 1 + n_in
, n_out
))
4520 cst
= isl_mat_alloc(ctx
, n
, 1 + n_in
);
4524 for (i
= 0; i
< n
; ++i
)
4525 isl_seq_cpy(cst
->row
[i
], bmap
->ineq
[list
[i
]], 1 + n_in
);
4527 bmap
= isl_basic_map_cow(bmap
);
4530 for (i
= n
- 1; i
>= 0; --i
)
4531 if (isl_basic_map_drop_inequality(bmap
, list
[i
]) < 0)
4534 bmap
= isl_basic_map_add(bmap
, isl_dim_in
, 1);
4535 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
4536 k
= isl_basic_map_alloc_inequality(bmap
);
4539 isl_seq_clr(bmap
->ineq
[k
], 1 + n_in
);
4540 isl_int_set_si(bmap
->ineq
[k
][1 + n_in
], 1);
4541 isl_seq_cpy(bmap
->ineq
[k
] + 1 + n_in
+ 1, var
->el
, n_out
);
4542 bmap
= isl_basic_map_finalize(bmap
);
4544 n_div
= isl_basic_set_dim(dom
, isl_dim_div
);
4545 dom
= isl_basic_set_add_dims(dom
, isl_dim_set
, 1);
4546 dom
= isl_basic_set_extend_constraints(dom
, 0, n
);
4547 for (i
= 0; i
< n
; ++i
) {
4548 k
= isl_basic_set_alloc_inequality(dom
);
4551 isl_seq_cpy(dom
->ineq
[k
], cst
->row
[i
], 1 + n_in
);
4552 isl_int_set_si(dom
->ineq
[k
][1 + n_in
], -1);
4553 isl_seq_clr(dom
->ineq
[k
] + 1 + n_in
+ 1, n_div
);
4559 return core(bmap
, dom
, empty
, max
, cst
, map_space
, set_space
);
4561 isl_space_free(map_space
);
4562 isl_space_free(set_space
);
4566 isl_basic_set_free(dom
);
4567 isl_basic_map_free(bmap
);
4572 static __isl_give isl_map
*basic_map_partial_lexopt_symm_map(
4573 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
4574 __isl_give isl_set
**empty
, int max
, int first
, int second
)
4576 return basic_map_partial_lexopt_symm(bmap
, dom
, empty
, max
,
4577 first
, second
, &basic_map_partial_lexopt_symm_map_core
).map
;
4580 /* Recursive part of isl_tab_basic_map_partial_lexopt, after detecting
4581 * equalities and removing redundant constraints.
4583 * We first check if there are any parallel constraints (left).
4584 * If not, we are in the base case.
4585 * If there are parallel constraints, we replace them by a single
4586 * constraint in basic_map_partial_lexopt_symm and then call
4587 * this function recursively to look for more parallel constraints.
4589 static __isl_give isl_map
*basic_map_partial_lexopt(
4590 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
4591 __isl_give isl_set
**empty
, int max
)
4599 if (bmap
->ctx
->opt
->pip_symmetry
)
4600 par
= parallel_constraints(bmap
, &first
, &second
);
4604 return basic_map_partial_lexopt_base_map(bmap
, dom
, empty
, max
);
4606 return basic_map_partial_lexopt_symm_map(bmap
, dom
, empty
, max
,
4609 isl_basic_set_free(dom
);
4610 isl_basic_map_free(bmap
);
4614 /* Compute the lexicographic minimum (or maximum if "max" is set)
4615 * of "bmap" over the domain "dom" and return the result as a map.
4616 * If "empty" is not NULL, then *empty is assigned a set that
4617 * contains those parts of the domain where there is no solution.
4618 * If "bmap" is marked as rational (ISL_BASIC_MAP_RATIONAL),
4619 * then we compute the rational optimum. Otherwise, we compute
4620 * the integral optimum.
4622 * We perform some preprocessing. As the PILP solver does not
4623 * handle implicit equalities very well, we first make sure all
4624 * the equalities are explicitly available.
4626 * We also add context constraints to the basic map and remove
4627 * redundant constraints. This is only needed because of the
4628 * way we handle simple symmetries. In particular, we currently look
4629 * for symmetries on the constraints, before we set up the main tableau.
4630 * It is then no good to look for symmetries on possibly redundant constraints.
4632 struct isl_map
*isl_tab_basic_map_partial_lexopt(
4633 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
4634 struct isl_set
**empty
, int max
)
4641 isl_assert(bmap
->ctx
,
4642 isl_basic_map_compatible_domain(bmap
, dom
), goto error
);
4644 if (isl_basic_set_dim(dom
, isl_dim_all
) == 0)
4645 return basic_map_partial_lexopt(bmap
, dom
, empty
, max
);
4647 bmap
= isl_basic_map_intersect_domain(bmap
, isl_basic_set_copy(dom
));
4648 bmap
= isl_basic_map_detect_equalities(bmap
);
4649 bmap
= isl_basic_map_remove_redundancies(bmap
);
4651 return basic_map_partial_lexopt(bmap
, dom
, empty
, max
);
4653 isl_basic_set_free(dom
);
4654 isl_basic_map_free(bmap
);
4658 struct isl_sol_for
{
4660 int (*fn
)(__isl_take isl_basic_set
*dom
,
4661 __isl_take isl_aff_list
*list
, void *user
);
4665 static void sol_for_free(struct isl_sol_for
*sol_for
)
4667 if (sol_for
->sol
.context
)
4668 sol_for
->sol
.context
->op
->free(sol_for
->sol
.context
);
4672 static void sol_for_free_wrap(struct isl_sol
*sol
)
4674 sol_for_free((struct isl_sol_for
*)sol
);
4677 /* Add the solution identified by the tableau and the context tableau.
4679 * See documentation of sol_add for more details.
4681 * Instead of constructing a basic map, this function calls a user
4682 * defined function with the current context as a basic set and
4683 * a list of affine expressions representing the relation between
4684 * the input and output. The space over which the affine expressions
4685 * are defined is the same as that of the domain. The number of
4686 * affine expressions in the list is equal to the number of output variables.
4688 static void sol_for_add(struct isl_sol_for
*sol
,
4689 struct isl_basic_set
*dom
, struct isl_mat
*M
)
4693 isl_local_space
*ls
;
4697 if (sol
->sol
.error
|| !dom
|| !M
)
4700 ctx
= isl_basic_set_get_ctx(dom
);
4701 ls
= isl_basic_set_get_local_space(dom
);
4702 list
= isl_aff_list_alloc(ctx
, M
->n_row
- 1);
4703 for (i
= 1; i
< M
->n_row
; ++i
) {
4704 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
4706 isl_int_set(aff
->v
->el
[0], M
->row
[0][0]);
4707 isl_seq_cpy(aff
->v
->el
+ 1, M
->row
[i
], M
->n_col
);
4709 aff
= isl_aff_normalize(aff
);
4710 list
= isl_aff_list_add(list
, aff
);
4712 isl_local_space_free(ls
);
4714 dom
= isl_basic_set_finalize(dom
);
4716 if (sol
->fn(isl_basic_set_copy(dom
), list
, sol
->user
) < 0)
4719 isl_basic_set_free(dom
);
4723 isl_basic_set_free(dom
);
4728 static void sol_for_add_wrap(struct isl_sol
*sol
,
4729 struct isl_basic_set
*dom
, struct isl_mat
*M
)
4731 sol_for_add((struct isl_sol_for
*)sol
, dom
, M
);
4734 static struct isl_sol_for
*sol_for_init(struct isl_basic_map
*bmap
, int max
,
4735 int (*fn
)(__isl_take isl_basic_set
*dom
, __isl_take isl_aff_list
*list
,
4739 struct isl_sol_for
*sol_for
= NULL
;
4741 struct isl_basic_set
*dom
= NULL
;
4743 sol_for
= isl_calloc_type(bmap
->ctx
, struct isl_sol_for
);
4747 dom_dim
= isl_space_domain(isl_space_copy(bmap
->dim
));
4748 dom
= isl_basic_set_universe(dom_dim
);
4750 sol_for
->sol
.rational
= ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
4751 sol_for
->sol
.dec_level
.callback
.run
= &sol_dec_level_wrap
;
4752 sol_for
->sol
.dec_level
.sol
= &sol_for
->sol
;
4754 sol_for
->user
= user
;
4755 sol_for
->sol
.max
= max
;
4756 sol_for
->sol
.n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4757 sol_for
->sol
.add
= &sol_for_add_wrap
;
4758 sol_for
->sol
.add_empty
= NULL
;
4759 sol_for
->sol
.free
= &sol_for_free_wrap
;
4761 sol_for
->sol
.context
= isl_context_alloc(dom
);
4762 if (!sol_for
->sol
.context
)
4765 isl_basic_set_free(dom
);
4768 isl_basic_set_free(dom
);
4769 sol_for_free(sol_for
);
4773 static void sol_for_find_solutions(struct isl_sol_for
*sol_for
,
4774 struct isl_tab
*tab
)
4776 find_solutions_main(&sol_for
->sol
, tab
);
4779 int isl_basic_map_foreach_lexopt(__isl_keep isl_basic_map
*bmap
, int max
,
4780 int (*fn
)(__isl_take isl_basic_set
*dom
, __isl_take isl_aff_list
*list
,
4784 struct isl_sol_for
*sol_for
= NULL
;
4786 bmap
= isl_basic_map_copy(bmap
);
4787 bmap
= isl_basic_map_detect_equalities(bmap
);
4791 sol_for
= sol_for_init(bmap
, max
, fn
, user
);
4795 if (isl_basic_map_plain_is_empty(bmap
))
4798 struct isl_tab
*tab
;
4799 struct isl_context
*context
= sol_for
->sol
.context
;
4800 tab
= tab_for_lexmin(bmap
,
4801 context
->op
->peek_basic_set(context
), 1, max
);
4802 tab
= context
->op
->detect_nonnegative_parameters(context
, tab
);
4803 sol_for_find_solutions(sol_for
, tab
);
4804 if (sol_for
->sol
.error
)
4808 sol_free(&sol_for
->sol
);
4809 isl_basic_map_free(bmap
);
4812 sol_free(&sol_for
->sol
);
4813 isl_basic_map_free(bmap
);
4817 int isl_basic_set_foreach_lexopt(__isl_keep isl_basic_set
*bset
, int max
,
4818 int (*fn
)(__isl_take isl_basic_set
*dom
, __isl_take isl_aff_list
*list
,
4822 return isl_basic_map_foreach_lexopt(bset
, max
, fn
, user
);
4825 /* Check if the given sequence of len variables starting at pos
4826 * represents a trivial (i.e., zero) solution.
4827 * The variables are assumed to be non-negative and to come in pairs,
4828 * with each pair representing a variable of unrestricted sign.
4829 * The solution is trivial if each such pair in the sequence consists
4830 * of two identical values, meaning that the variable being represented
4833 static int region_is_trivial(struct isl_tab
*tab
, int pos
, int len
)
4840 for (i
= 0; i
< len
; i
+= 2) {
4844 neg_row
= tab
->var
[pos
+ i
].is_row
?
4845 tab
->var
[pos
+ i
].index
: -1;
4846 pos_row
= tab
->var
[pos
+ i
+ 1].is_row
?
4847 tab
->var
[pos
+ i
+ 1].index
: -1;
4850 isl_int_is_zero(tab
->mat
->row
[neg_row
][1])) &&
4852 isl_int_is_zero(tab
->mat
->row
[pos_row
][1])))
4855 if (neg_row
< 0 || pos_row
< 0)
4857 if (isl_int_ne(tab
->mat
->row
[neg_row
][1],
4858 tab
->mat
->row
[pos_row
][1]))
4865 /* Return the index of the first trivial region or -1 if all regions
4868 static int first_trivial_region(struct isl_tab
*tab
,
4869 int n_region
, struct isl_region
*region
)
4873 for (i
= 0; i
< n_region
; ++i
) {
4874 if (region_is_trivial(tab
, region
[i
].pos
, region
[i
].len
))
4881 /* Check if the solution is optimal, i.e., whether the first
4882 * n_op entries are zero.
4884 static int is_optimal(__isl_keep isl_vec
*sol
, int n_op
)
4888 for (i
= 0; i
< n_op
; ++i
)
4889 if (!isl_int_is_zero(sol
->el
[1 + i
]))
4894 /* Add constraints to "tab" that ensure that any solution is significantly
4895 * better that that represented by "sol". That is, find the first
4896 * relevant (within first n_op) non-zero coefficient and force it (along
4897 * with all previous coefficients) to be zero.
4898 * If the solution is already optimal (all relevant coefficients are zero),
4899 * then just mark the table as empty.
4901 static int force_better_solution(struct isl_tab
*tab
,
4902 __isl_keep isl_vec
*sol
, int n_op
)
4911 for (i
= 0; i
< n_op
; ++i
)
4912 if (!isl_int_is_zero(sol
->el
[1 + i
]))
4916 if (isl_tab_mark_empty(tab
) < 0)
4921 ctx
= isl_vec_get_ctx(sol
);
4922 v
= isl_vec_alloc(ctx
, 1 + tab
->n_var
);
4926 for (; i
>= 0; --i
) {
4928 isl_int_set_si(v
->el
[1 + i
], -1);
4929 if (add_lexmin_eq(tab
, v
->el
) < 0)
4940 struct isl_trivial
{
4944 struct isl_tab_undo
*snap
;
4947 /* Return the lexicographically smallest non-trivial solution of the
4948 * given ILP problem.
4950 * All variables are assumed to be non-negative.
4952 * n_op is the number of initial coordinates to optimize.
4953 * That is, once a solution has been found, we will only continue looking
4954 * for solution that result in significantly better values for those
4955 * initial coordinates. That is, we only continue looking for solutions
4956 * that increase the number of initial zeros in this sequence.
4958 * A solution is non-trivial, if it is non-trivial on each of the
4959 * specified regions. Each region represents a sequence of pairs
4960 * of variables. A solution is non-trivial on such a region if
4961 * at least one of these pairs consists of different values, i.e.,
4962 * such that the non-negative variable represented by the pair is non-zero.
4964 * Whenever a conflict is encountered, all constraints involved are
4965 * reported to the caller through a call to "conflict".
4967 * We perform a simple branch-and-bound backtracking search.
4968 * Each level in the search represents initially trivial region that is forced
4969 * to be non-trivial.
4970 * At each level we consider n cases, where n is the length of the region.
4971 * In terms of the n/2 variables of unrestricted signs being encoded by
4972 * the region, we consider the cases
4975 * x_0 = 0 and x_1 >= 1
4976 * x_0 = 0 and x_1 <= -1
4977 * x_0 = 0 and x_1 = 0 and x_2 >= 1
4978 * x_0 = 0 and x_1 = 0 and x_2 <= -1
4980 * The cases are considered in this order, assuming that each pair
4981 * x_i_a x_i_b represents the value x_i_b - x_i_a.
4982 * That is, x_0 >= 1 is enforced by adding the constraint
4983 * x_0_b - x_0_a >= 1
4985 __isl_give isl_vec
*isl_tab_basic_set_non_trivial_lexmin(
4986 __isl_take isl_basic_set
*bset
, int n_op
, int n_region
,
4987 struct isl_region
*region
,
4988 int (*conflict
)(int con
, void *user
), void *user
)
4994 isl_vec
*sol
= NULL
;
4995 struct isl_tab
*tab
;
4996 struct isl_trivial
*triv
= NULL
;
5002 ctx
= isl_basic_set_get_ctx(bset
);
5003 sol
= isl_vec_alloc(ctx
, 0);
5005 tab
= tab_for_lexmin(bset
, NULL
, 0, 0);
5008 tab
->conflict
= conflict
;
5009 tab
->conflict_user
= user
;
5011 v
= isl_vec_alloc(ctx
, 1 + tab
->n_var
);
5012 triv
= isl_calloc_array(ctx
, struct isl_trivial
, n_region
);
5019 while (level
>= 0) {
5023 tab
= cut_to_integer_lexmin(tab
, CUT_ONE
);
5028 r
= first_trivial_region(tab
, n_region
, region
);
5030 for (i
= 0; i
< level
; ++i
)
5033 sol
= isl_tab_get_sample_value(tab
);
5036 if (is_optimal(sol
, n_op
))
5040 if (level
>= n_region
)
5041 isl_die(ctx
, isl_error_internal
,
5042 "nesting level too deep", goto error
);
5043 if (isl_tab_extend_cons(tab
,
5044 2 * region
[r
].len
+ 2 * n_op
) < 0)
5046 triv
[level
].region
= r
;
5047 triv
[level
].side
= 0;
5050 r
= triv
[level
].region
;
5051 side
= triv
[level
].side
;
5052 base
= 2 * (side
/2);
5054 if (side
>= region
[r
].len
) {
5059 if (isl_tab_rollback(tab
, triv
[level
].snap
) < 0)
5064 if (triv
[level
].update
) {
5065 if (force_better_solution(tab
, sol
, n_op
) < 0)
5067 triv
[level
].update
= 0;
5070 if (side
== base
&& base
>= 2) {
5071 for (j
= base
- 2; j
< base
; ++j
) {
5073 isl_int_set_si(v
->el
[1 + region
[r
].pos
+ j
], 1);
5074 if (add_lexmin_eq(tab
, v
->el
) < 0)
5079 triv
[level
].snap
= isl_tab_snap(tab
);
5080 if (isl_tab_push_basis(tab
) < 0)
5084 isl_int_set_si(v
->el
[0], -1);
5085 isl_int_set_si(v
->el
[1 + region
[r
].pos
+ side
], -1);
5086 isl_int_set_si(v
->el
[1 + region
[r
].pos
+ (side
^ 1)], 1);
5087 tab
= add_lexmin_ineq(tab
, v
->el
);
5097 isl_basic_set_free(bset
);
5104 isl_basic_set_free(bset
);
5109 /* Return the lexicographically smallest rational point in "bset",
5110 * assuming that all variables are non-negative.
5111 * If "bset" is empty, then return a zero-length vector.
5113 __isl_give isl_vec
*isl_tab_basic_set_non_neg_lexmin(
5114 __isl_take isl_basic_set
*bset
)
5116 struct isl_tab
*tab
;
5117 isl_ctx
*ctx
= isl_basic_set_get_ctx(bset
);
5123 tab
= tab_for_lexmin(bset
, NULL
, 0, 0);
5127 sol
= isl_vec_alloc(ctx
, 0);
5129 sol
= isl_tab_get_sample_value(tab
);
5131 isl_basic_set_free(bset
);
5135 isl_basic_set_free(bset
);
5139 struct isl_sol_pma
{
5141 isl_pw_multi_aff
*pma
;
5145 static void sol_pma_free(struct isl_sol_pma
*sol_pma
)
5149 if (sol_pma
->sol
.context
)
5150 sol_pma
->sol
.context
->op
->free(sol_pma
->sol
.context
);
5151 isl_pw_multi_aff_free(sol_pma
->pma
);
5152 isl_set_free(sol_pma
->empty
);
5156 /* This function is called for parts of the context where there is
5157 * no solution, with "bset" corresponding to the context tableau.
5158 * Simply add the basic set to the set "empty".
5160 static void sol_pma_add_empty(struct isl_sol_pma
*sol
,
5161 __isl_take isl_basic_set
*bset
)
5165 isl_assert(bset
->ctx
, sol
->empty
, goto error
);
5167 sol
->empty
= isl_set_grow(sol
->empty
, 1);
5168 bset
= isl_basic_set_simplify(bset
);
5169 bset
= isl_basic_set_finalize(bset
);
5170 sol
->empty
= isl_set_add_basic_set(sol
->empty
, bset
);
5175 isl_basic_set_free(bset
);
5179 /* Given a basic map "dom" that represents the context and an affine
5180 * matrix "M" that maps the dimensions of the context to the
5181 * output variables, construct an isl_pw_multi_aff with a single
5182 * cell corresponding to "dom" and affine expressions copied from "M".
5184 static void sol_pma_add(struct isl_sol_pma
*sol
,
5185 __isl_take isl_basic_set
*dom
, __isl_take isl_mat
*M
)
5188 isl_local_space
*ls
;
5190 isl_multi_aff
*maff
;
5191 isl_pw_multi_aff
*pma
;
5193 maff
= isl_multi_aff_alloc(isl_pw_multi_aff_get_space(sol
->pma
));
5194 ls
= isl_basic_set_get_local_space(dom
);
5195 for (i
= 1; i
< M
->n_row
; ++i
) {
5196 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
5198 isl_int_set(aff
->v
->el
[0], M
->row
[0][0]);
5199 isl_seq_cpy(aff
->v
->el
+ 1, M
->row
[i
], M
->n_col
);
5201 aff
= isl_aff_normalize(aff
);
5202 maff
= isl_multi_aff_set_aff(maff
, i
- 1, aff
);
5204 isl_local_space_free(ls
);
5206 dom
= isl_basic_set_simplify(dom
);
5207 dom
= isl_basic_set_finalize(dom
);
5208 pma
= isl_pw_multi_aff_alloc(isl_set_from_basic_set(dom
), maff
);
5209 sol
->pma
= isl_pw_multi_aff_add_disjoint(sol
->pma
, pma
);
5214 static void sol_pma_free_wrap(struct isl_sol
*sol
)
5216 sol_pma_free((struct isl_sol_pma
*)sol
);
5219 static void sol_pma_add_empty_wrap(struct isl_sol
*sol
,
5220 __isl_take isl_basic_set
*bset
)
5222 sol_pma_add_empty((struct isl_sol_pma
*)sol
, bset
);
5225 static void sol_pma_add_wrap(struct isl_sol
*sol
,
5226 __isl_take isl_basic_set
*dom
, __isl_take isl_mat
*M
)
5228 sol_pma_add((struct isl_sol_pma
*)sol
, dom
, M
);
5231 /* Construct an isl_sol_pma structure for accumulating the solution.
5232 * If track_empty is set, then we also keep track of the parts
5233 * of the context where there is no solution.
5234 * If max is set, then we are solving a maximization, rather than
5235 * a minimization problem, which means that the variables in the
5236 * tableau have value "M - x" rather than "M + x".
5238 static struct isl_sol
*sol_pma_init(__isl_keep isl_basic_map
*bmap
,
5239 __isl_take isl_basic_set
*dom
, int track_empty
, int max
)
5241 struct isl_sol_pma
*sol_pma
= NULL
;
5246 sol_pma
= isl_calloc_type(bmap
->ctx
, struct isl_sol_pma
);
5250 sol_pma
->sol
.rational
= ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
5251 sol_pma
->sol
.dec_level
.callback
.run
= &sol_dec_level_wrap
;
5252 sol_pma
->sol
.dec_level
.sol
= &sol_pma
->sol
;
5253 sol_pma
->sol
.max
= max
;
5254 sol_pma
->sol
.n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5255 sol_pma
->sol
.add
= &sol_pma_add_wrap
;
5256 sol_pma
->sol
.add_empty
= track_empty
? &sol_pma_add_empty_wrap
: NULL
;
5257 sol_pma
->sol
.free
= &sol_pma_free_wrap
;
5258 sol_pma
->pma
= isl_pw_multi_aff_empty(isl_basic_map_get_space(bmap
));
5262 sol_pma
->sol
.context
= isl_context_alloc(dom
);
5263 if (!sol_pma
->sol
.context
)
5267 sol_pma
->empty
= isl_set_alloc_space(isl_basic_set_get_space(dom
),
5268 1, ISL_SET_DISJOINT
);
5269 if (!sol_pma
->empty
)
5273 isl_basic_set_free(dom
);
5274 return &sol_pma
->sol
;
5276 isl_basic_set_free(dom
);
5277 sol_pma_free(sol_pma
);
5281 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
5282 * some obvious symmetries.
5284 * We call basic_map_partial_lexopt_base and extract the results.
5286 static __isl_give isl_pw_multi_aff
*basic_map_partial_lexopt_base_pma(
5287 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5288 __isl_give isl_set
**empty
, int max
)
5290 isl_pw_multi_aff
*result
= NULL
;
5291 struct isl_sol
*sol
;
5292 struct isl_sol_pma
*sol_pma
;
5294 sol
= basic_map_partial_lexopt_base(bmap
, dom
, empty
, max
,
5298 sol_pma
= (struct isl_sol_pma
*) sol
;
5300 result
= isl_pw_multi_aff_copy(sol_pma
->pma
);
5302 *empty
= isl_set_copy(sol_pma
->empty
);
5303 sol_free(&sol_pma
->sol
);
5307 /* Given that the last input variable of "maff" represents the minimum
5308 * of some bounds, check whether we need to plug in the expression
5311 * In particular, check if the last input variable appears in any
5312 * of the expressions in "maff".
5314 static int need_substitution(__isl_keep isl_multi_aff
*maff
)
5319 pos
= isl_multi_aff_dim(maff
, isl_dim_in
) - 1;
5321 for (i
= 0; i
< maff
->n
; ++i
)
5322 if (isl_aff_involves_dims(maff
->p
[i
], isl_dim_in
, pos
, 1))
5328 /* Given a set of upper bounds on the last "input" variable m,
5329 * construct a piecewise affine expression that selects
5330 * the minimal upper bound to m, i.e.,
5331 * divide the space into cells where one
5332 * of the upper bounds is smaller than all the others and select
5333 * this upper bound on that cell.
5335 * In particular, if there are n bounds b_i, then the result
5336 * consists of n cell, each one of the form
5338 * b_i <= b_j for j > i
5339 * b_i < b_j for j < i
5341 * The affine expression on this cell is
5345 static __isl_give isl_pw_aff
*set_minimum_pa(__isl_take isl_space
*space
,
5346 __isl_take isl_mat
*var
)
5349 isl_aff
*aff
= NULL
;
5350 isl_basic_set
*bset
= NULL
;
5352 isl_pw_aff
*paff
= NULL
;
5353 isl_space
*pw_space
;
5354 isl_local_space
*ls
= NULL
;
5359 ctx
= isl_space_get_ctx(space
);
5360 ls
= isl_local_space_from_space(isl_space_copy(space
));
5361 pw_space
= isl_space_copy(space
);
5362 pw_space
= isl_space_from_domain(pw_space
);
5363 pw_space
= isl_space_add_dims(pw_space
, isl_dim_out
, 1);
5364 paff
= isl_pw_aff_alloc_size(pw_space
, var
->n_row
);
5366 for (i
= 0; i
< var
->n_row
; ++i
) {
5369 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
5370 bset
= isl_basic_set_alloc_space(isl_space_copy(space
), 0,
5374 isl_int_set_si(aff
->v
->el
[0], 1);
5375 isl_seq_cpy(aff
->v
->el
+ 1, var
->row
[i
], var
->n_col
);
5376 isl_int_set_si(aff
->v
->el
[1 + var
->n_col
], 0);
5377 bset
= select_minimum(bset
, var
, i
);
5378 paff_i
= isl_pw_aff_alloc(isl_set_from_basic_set(bset
), aff
);
5379 paff
= isl_pw_aff_add_disjoint(paff
, paff_i
);
5382 isl_local_space_free(ls
);
5383 isl_space_free(space
);
5388 isl_basic_set_free(bset
);
5389 isl_pw_aff_free(paff
);
5390 isl_local_space_free(ls
);
5391 isl_space_free(space
);
5396 /* Given a piecewise multi-affine expression of which the last input variable
5397 * is the minimum of the bounds in "cst", plug in the value of the minimum.
5398 * This minimum expression is given in "min_expr_pa".
5399 * The set "min_expr" contains the same information, but in the form of a set.
5400 * The variable is subsequently projected out.
5402 * The implementation is similar to those of "split" and "split_domain".
5403 * If the variable appears in a given expression, then minimum expression
5404 * is plugged in. Otherwise, if the variable appears in the constraints
5405 * and a split is required, then the domain is split. Otherwise, no split
5408 static __isl_give isl_pw_multi_aff
*split_domain_pma(
5409 __isl_take isl_pw_multi_aff
*opt
, __isl_take isl_pw_aff
*min_expr_pa
,
5410 __isl_take isl_set
*min_expr
, __isl_take isl_mat
*cst
)
5415 isl_pw_multi_aff
*res
;
5417 if (!opt
|| !min_expr
|| !cst
)
5420 n_in
= isl_pw_multi_aff_dim(opt
, isl_dim_in
);
5421 space
= isl_pw_multi_aff_get_space(opt
);
5422 space
= isl_space_drop_dims(space
, isl_dim_in
, n_in
- 1, 1);
5423 res
= isl_pw_multi_aff_empty(space
);
5425 for (i
= 0; i
< opt
->n
; ++i
) {
5426 isl_pw_multi_aff
*pma
;
5428 pma
= isl_pw_multi_aff_alloc(isl_set_copy(opt
->p
[i
].set
),
5429 isl_multi_aff_copy(opt
->p
[i
].maff
));
5430 if (need_substitution(opt
->p
[i
].maff
))
5431 pma
= isl_pw_multi_aff_substitute(pma
,
5432 isl_dim_in
, n_in
- 1, min_expr_pa
);
5433 else if (need_split_set(opt
->p
[i
].set
, cst
))
5434 pma
= isl_pw_multi_aff_intersect_domain(pma
,
5435 isl_set_copy(min_expr
));
5436 pma
= isl_pw_multi_aff_project_out(pma
,
5437 isl_dim_in
, n_in
- 1, 1);
5439 res
= isl_pw_multi_aff_add_disjoint(res
, pma
);
5442 isl_pw_multi_aff_free(opt
);
5443 isl_pw_aff_free(min_expr_pa
);
5444 isl_set_free(min_expr
);
5448 isl_pw_multi_aff_free(opt
);
5449 isl_pw_aff_free(min_expr_pa
);
5450 isl_set_free(min_expr
);
5455 static __isl_give isl_pw_multi_aff
*basic_map_partial_lexopt_pma(
5456 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5457 __isl_give isl_set
**empty
, int max
);
5459 /* This function is called from basic_map_partial_lexopt_symm.
5460 * The last variable of "bmap" and "dom" corresponds to the minimum
5461 * of the bounds in "cst". "map_space" is the space of the original
5462 * input relation (of basic_map_partial_lexopt_symm) and "set_space"
5463 * is the space of the original domain.
5465 * We recursively call basic_map_partial_lexopt and then plug in
5466 * the definition of the minimum in the result.
5468 static __isl_give
union isl_lex_res
basic_map_partial_lexopt_symm_pma_core(
5469 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5470 __isl_give isl_set
**empty
, int max
, __isl_take isl_mat
*cst
,
5471 __isl_take isl_space
*map_space
, __isl_take isl_space
*set_space
)
5473 isl_pw_multi_aff
*opt
;
5474 isl_pw_aff
*min_expr_pa
;
5476 union isl_lex_res res
;
5478 min_expr
= set_minimum(isl_basic_set_get_space(dom
), isl_mat_copy(cst
));
5479 min_expr_pa
= set_minimum_pa(isl_basic_set_get_space(dom
),
5482 opt
= basic_map_partial_lexopt_pma(bmap
, dom
, empty
, max
);
5485 *empty
= split(*empty
,
5486 isl_set_copy(min_expr
), isl_mat_copy(cst
));
5487 *empty
= isl_set_reset_space(*empty
, set_space
);
5490 opt
= split_domain_pma(opt
, min_expr_pa
, min_expr
, cst
);
5491 opt
= isl_pw_multi_aff_reset_space(opt
, map_space
);
5497 static __isl_give isl_pw_multi_aff
*basic_map_partial_lexopt_symm_pma(
5498 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5499 __isl_give isl_set
**empty
, int max
, int first
, int second
)
5501 return basic_map_partial_lexopt_symm(bmap
, dom
, empty
, max
,
5502 first
, second
, &basic_map_partial_lexopt_symm_pma_core
).pma
;
5505 /* Recursive part of isl_basic_map_partial_lexopt_pw_multi_aff, after detecting
5506 * equalities and removing redundant constraints.
5508 * We first check if there are any parallel constraints (left).
5509 * If not, we are in the base case.
5510 * If there are parallel constraints, we replace them by a single
5511 * constraint in basic_map_partial_lexopt_symm_pma and then call
5512 * this function recursively to look for more parallel constraints.
5514 static __isl_give isl_pw_multi_aff
*basic_map_partial_lexopt_pma(
5515 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5516 __isl_give isl_set
**empty
, int max
)
5524 if (bmap
->ctx
->opt
->pip_symmetry
)
5525 par
= parallel_constraints(bmap
, &first
, &second
);
5529 return basic_map_partial_lexopt_base_pma(bmap
, dom
, empty
, max
);
5531 return basic_map_partial_lexopt_symm_pma(bmap
, dom
, empty
, max
,
5534 isl_basic_set_free(dom
);
5535 isl_basic_map_free(bmap
);
5539 /* Compute the lexicographic minimum (or maximum if "max" is set)
5540 * of "bmap" over the domain "dom" and return the result as a piecewise
5541 * multi-affine expression.
5542 * If "empty" is not NULL, then *empty is assigned a set that
5543 * contains those parts of the domain where there is no solution.
5544 * If "bmap" is marked as rational (ISL_BASIC_MAP_RATIONAL),
5545 * then we compute the rational optimum. Otherwise, we compute
5546 * the integral optimum.
5548 * We perform some preprocessing. As the PILP solver does not
5549 * handle implicit equalities very well, we first make sure all
5550 * the equalities are explicitly available.
5552 * We also add context constraints to the basic map and remove
5553 * redundant constraints. This is only needed because of the
5554 * way we handle simple symmetries. In particular, we currently look
5555 * for symmetries on the constraints, before we set up the main tableau.
5556 * It is then no good to look for symmetries on possibly redundant constraints.
5558 __isl_give isl_pw_multi_aff
*isl_basic_map_partial_lexopt_pw_multi_aff(
5559 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5560 __isl_give isl_set
**empty
, int max
)
5567 isl_assert(bmap
->ctx
,
5568 isl_basic_map_compatible_domain(bmap
, dom
), goto error
);
5570 if (isl_basic_set_dim(dom
, isl_dim_all
) == 0)
5571 return basic_map_partial_lexopt_pma(bmap
, dom
, empty
, max
);
5573 bmap
= isl_basic_map_intersect_domain(bmap
, isl_basic_set_copy(dom
));
5574 bmap
= isl_basic_map_detect_equalities(bmap
);
5575 bmap
= isl_basic_map_remove_redundancies(bmap
);
5577 return basic_map_partial_lexopt_pma(bmap
, dom
, empty
, max
);
5579 isl_basic_set_free(dom
);
5580 isl_basic_map_free(bmap
);