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 /* invalidate context */
106 void (*invalidate
)(struct isl_context
*context
);
108 void (*free
)(struct isl_context
*context
);
112 struct isl_context_op
*op
;
115 struct isl_context_lex
{
116 struct isl_context context
;
120 struct isl_partial_sol
{
122 struct isl_basic_set
*dom
;
125 struct isl_partial_sol
*next
;
129 struct isl_sol_callback
{
130 struct isl_tab_callback callback
;
134 /* isl_sol is an interface for constructing a solution to
135 * a parametric integer linear programming problem.
136 * Every time the algorithm reaches a state where a solution
137 * can be read off from the tableau (including cases where the tableau
138 * is empty), the function "add" is called on the isl_sol passed
139 * to find_solutions_main.
141 * The context tableau is owned by isl_sol and is updated incrementally.
143 * There are currently two implementations of this interface,
144 * isl_sol_map, which simply collects the solutions in an isl_map
145 * and (optionally) the parts of the context where there is no solution
147 * isl_sol_for, which calls a user-defined function for each part of
156 struct isl_context
*context
;
157 struct isl_partial_sol
*partial
;
158 void (*add
)(struct isl_sol
*sol
,
159 struct isl_basic_set
*dom
, struct isl_mat
*M
);
160 void (*add_empty
)(struct isl_sol
*sol
, struct isl_basic_set
*bset
);
161 void (*free
)(struct isl_sol
*sol
);
162 struct isl_sol_callback dec_level
;
165 static void sol_free(struct isl_sol
*sol
)
167 struct isl_partial_sol
*partial
, *next
;
170 for (partial
= sol
->partial
; partial
; partial
= next
) {
171 next
= partial
->next
;
172 isl_basic_set_free(partial
->dom
);
173 isl_mat_free(partial
->M
);
179 /* Push a partial solution represented by a domain and mapping M
180 * onto the stack of partial solutions.
182 static void sol_push_sol(struct isl_sol
*sol
,
183 struct isl_basic_set
*dom
, struct isl_mat
*M
)
185 struct isl_partial_sol
*partial
;
187 if (sol
->error
|| !dom
)
190 partial
= isl_alloc_type(dom
->ctx
, struct isl_partial_sol
);
194 partial
->level
= sol
->level
;
197 partial
->next
= sol
->partial
;
199 sol
->partial
= partial
;
203 isl_basic_set_free(dom
);
207 /* Pop one partial solution from the partial solution stack and
208 * pass it on to sol->add or sol->add_empty.
210 static void sol_pop_one(struct isl_sol
*sol
)
212 struct isl_partial_sol
*partial
;
214 partial
= sol
->partial
;
215 sol
->partial
= partial
->next
;
218 sol
->add(sol
, partial
->dom
, partial
->M
);
220 sol
->add_empty(sol
, partial
->dom
);
224 /* Return a fresh copy of the domain represented by the context tableau.
226 static struct isl_basic_set
*sol_domain(struct isl_sol
*sol
)
228 struct isl_basic_set
*bset
;
233 bset
= isl_basic_set_dup(sol
->context
->op
->peek_basic_set(sol
->context
));
234 bset
= isl_basic_set_update_from_tab(bset
,
235 sol
->context
->op
->peek_tab(sol
->context
));
240 /* Check whether two partial solutions have the same mapping, where n_div
241 * is the number of divs that the two partial solutions have in common.
243 static int same_solution(struct isl_partial_sol
*s1
, struct isl_partial_sol
*s2
,
249 if (!s1
->M
!= !s2
->M
)
254 dim
= isl_basic_set_total_dim(s1
->dom
) - s1
->dom
->n_div
;
256 for (i
= 0; i
< s1
->M
->n_row
; ++i
) {
257 if (isl_seq_first_non_zero(s1
->M
->row
[i
]+1+dim
+n_div
,
258 s1
->M
->n_col
-1-dim
-n_div
) != -1)
260 if (isl_seq_first_non_zero(s2
->M
->row
[i
]+1+dim
+n_div
,
261 s2
->M
->n_col
-1-dim
-n_div
) != -1)
263 if (!isl_seq_eq(s1
->M
->row
[i
], s2
->M
->row
[i
], 1+dim
+n_div
))
269 /* Pop all solutions from the partial solution stack that were pushed onto
270 * the stack at levels that are deeper than the current level.
271 * If the two topmost elements on the stack have the same level
272 * and represent the same solution, then their domains are combined.
273 * This combined domain is the same as the current context domain
274 * as sol_pop is called each time we move back to a higher level.
276 static void sol_pop(struct isl_sol
*sol
)
278 struct isl_partial_sol
*partial
;
284 if (sol
->level
== 0) {
285 for (partial
= sol
->partial
; partial
; partial
= sol
->partial
)
290 partial
= sol
->partial
;
294 if (partial
->level
<= sol
->level
)
297 if (partial
->next
&& partial
->next
->level
== partial
->level
) {
298 n_div
= isl_basic_set_dim(
299 sol
->context
->op
->peek_basic_set(sol
->context
),
302 if (!same_solution(partial
, partial
->next
, n_div
)) {
306 struct isl_basic_set
*bset
;
308 bset
= sol_domain(sol
);
310 isl_basic_set_free(partial
->next
->dom
);
311 partial
->next
->dom
= bset
;
312 partial
->next
->level
= sol
->level
;
314 sol
->partial
= partial
->next
;
315 isl_basic_set_free(partial
->dom
);
316 isl_mat_free(partial
->M
);
323 static void sol_dec_level(struct isl_sol
*sol
)
333 static int sol_dec_level_wrap(struct isl_tab_callback
*cb
)
335 struct isl_sol_callback
*callback
= (struct isl_sol_callback
*)cb
;
337 sol_dec_level(callback
->sol
);
339 return callback
->sol
->error
? -1 : 0;
342 /* Move down to next level and push callback onto context tableau
343 * to decrease the level again when it gets rolled back across
344 * the current state. That is, dec_level will be called with
345 * the context tableau in the same state as it is when inc_level
348 static void sol_inc_level(struct isl_sol
*sol
)
356 tab
= sol
->context
->op
->peek_tab(sol
->context
);
357 if (isl_tab_push_callback(tab
, &sol
->dec_level
.callback
) < 0)
361 static void scale_rows(struct isl_mat
*mat
, isl_int m
, int n_row
)
365 if (isl_int_is_one(m
))
368 for (i
= 0; i
< n_row
; ++i
)
369 isl_seq_scale(mat
->row
[i
], mat
->row
[i
], m
, mat
->n_col
);
372 /* Add the solution identified by the tableau and the context tableau.
374 * The layout of the variables is as follows.
375 * tab->n_var is equal to the total number of variables in the input
376 * map (including divs that were copied from the context)
377 * + the number of extra divs constructed
378 * Of these, the first tab->n_param and the last tab->n_div variables
379 * correspond to the variables in the context, i.e.,
380 * tab->n_param + tab->n_div = context_tab->n_var
381 * tab->n_param is equal to the number of parameters and input
382 * dimensions in the input map
383 * tab->n_div is equal to the number of divs in the context
385 * If there is no solution, then call add_empty with a basic set
386 * that corresponds to the context tableau. (If add_empty is NULL,
389 * If there is a solution, then first construct a matrix that maps
390 * all dimensions of the context to the output variables, i.e.,
391 * the output dimensions in the input map.
392 * The divs in the input map (if any) that do not correspond to any
393 * div in the context do not appear in the solution.
394 * The algorithm will make sure that they have an integer value,
395 * but these values themselves are of no interest.
396 * We have to be careful not to drop or rearrange any divs in the
397 * context because that would change the meaning of the matrix.
399 * To extract the value of the output variables, it should be noted
400 * that we always use a big parameter M in the main tableau and so
401 * the variable stored in this tableau is not an output variable x itself, but
402 * x' = M + x (in case of minimization)
404 * x' = M - x (in case of maximization)
405 * If x' appears in a column, then its optimal value is zero,
406 * which means that the optimal value of x is an unbounded number
407 * (-M for minimization and M for maximization).
408 * We currently assume that the output dimensions in the original map
409 * are bounded, so this cannot occur.
410 * Similarly, when x' appears in a row, then the coefficient of M in that
411 * row is necessarily 1.
412 * If the row in the tableau represents
413 * d x' = c + d M + e(y)
414 * then, in case of minimization, the corresponding row in the matrix
417 * with a d = m, the (updated) common denominator of the matrix.
418 * In case of maximization, the row will be
421 static void sol_add(struct isl_sol
*sol
, struct isl_tab
*tab
)
423 struct isl_basic_set
*bset
= NULL
;
424 struct isl_mat
*mat
= NULL
;
429 if (sol
->error
|| !tab
)
432 if (tab
->empty
&& !sol
->add_empty
)
434 if (sol
->context
->op
->is_empty(sol
->context
))
437 bset
= sol_domain(sol
);
440 sol_push_sol(sol
, bset
, NULL
);
446 mat
= isl_mat_alloc(tab
->mat
->ctx
, 1 + sol
->n_out
,
447 1 + tab
->n_param
+ tab
->n_div
);
453 isl_seq_clr(mat
->row
[0] + 1, mat
->n_col
- 1);
454 isl_int_set_si(mat
->row
[0][0], 1);
455 for (row
= 0; row
< sol
->n_out
; ++row
) {
456 int i
= tab
->n_param
+ row
;
459 isl_seq_clr(mat
->row
[1 + row
], mat
->n_col
);
460 if (!tab
->var
[i
].is_row
) {
462 isl_die(mat
->ctx
, isl_error_invalid
,
463 "unbounded optimum", goto error2
);
467 r
= tab
->var
[i
].index
;
469 isl_int_ne(tab
->mat
->row
[r
][2], tab
->mat
->row
[r
][0]))
470 isl_die(mat
->ctx
, isl_error_invalid
,
471 "unbounded optimum", goto error2
);
472 isl_int_gcd(m
, mat
->row
[0][0], tab
->mat
->row
[r
][0]);
473 isl_int_divexact(m
, tab
->mat
->row
[r
][0], m
);
474 scale_rows(mat
, m
, 1 + row
);
475 isl_int_divexact(m
, mat
->row
[0][0], tab
->mat
->row
[r
][0]);
476 isl_int_mul(mat
->row
[1 + row
][0], m
, tab
->mat
->row
[r
][1]);
477 for (j
= 0; j
< tab
->n_param
; ++j
) {
479 if (tab
->var
[j
].is_row
)
481 col
= tab
->var
[j
].index
;
482 isl_int_mul(mat
->row
[1 + row
][1 + j
], m
,
483 tab
->mat
->row
[r
][off
+ col
]);
485 for (j
= 0; j
< tab
->n_div
; ++j
) {
487 if (tab
->var
[tab
->n_var
- tab
->n_div
+j
].is_row
)
489 col
= tab
->var
[tab
->n_var
- tab
->n_div
+j
].index
;
490 isl_int_mul(mat
->row
[1 + row
][1 + tab
->n_param
+ j
], m
,
491 tab
->mat
->row
[r
][off
+ col
]);
494 isl_seq_neg(mat
->row
[1 + row
], mat
->row
[1 + row
],
500 sol_push_sol(sol
, bset
, mat
);
505 isl_basic_set_free(bset
);
513 struct isl_set
*empty
;
516 static void sol_map_free(struct isl_sol_map
*sol_map
)
520 if (sol_map
->sol
.context
)
521 sol_map
->sol
.context
->op
->free(sol_map
->sol
.context
);
522 isl_map_free(sol_map
->map
);
523 isl_set_free(sol_map
->empty
);
527 static void sol_map_free_wrap(struct isl_sol
*sol
)
529 sol_map_free((struct isl_sol_map
*)sol
);
532 /* This function is called for parts of the context where there is
533 * no solution, with "bset" corresponding to the context tableau.
534 * Simply add the basic set to the set "empty".
536 static void sol_map_add_empty(struct isl_sol_map
*sol
,
537 struct isl_basic_set
*bset
)
541 isl_assert(bset
->ctx
, sol
->empty
, goto error
);
543 sol
->empty
= isl_set_grow(sol
->empty
, 1);
544 bset
= isl_basic_set_simplify(bset
);
545 bset
= isl_basic_set_finalize(bset
);
546 sol
->empty
= isl_set_add_basic_set(sol
->empty
, isl_basic_set_copy(bset
));
549 isl_basic_set_free(bset
);
552 isl_basic_set_free(bset
);
556 static void sol_map_add_empty_wrap(struct isl_sol
*sol
,
557 struct isl_basic_set
*bset
)
559 sol_map_add_empty((struct isl_sol_map
*)sol
, bset
);
562 /* Given a basic map "dom" that represents the context and an affine
563 * matrix "M" that maps the dimensions of the context to the
564 * output variables, construct a basic map with the same parameters
565 * and divs as the context, the dimensions of the context as input
566 * dimensions and a number of output dimensions that is equal to
567 * the number of output dimensions in the input map.
569 * The constraints and divs of the context are simply copied
570 * from "dom". For each row
574 * is added, with d the common denominator of M.
576 static void sol_map_add(struct isl_sol_map
*sol
,
577 struct isl_basic_set
*dom
, struct isl_mat
*M
)
580 struct isl_basic_map
*bmap
= NULL
;
588 if (sol
->sol
.error
|| !dom
|| !M
)
591 n_out
= sol
->sol
.n_out
;
592 n_eq
= dom
->n_eq
+ n_out
;
593 n_ineq
= dom
->n_ineq
;
595 nparam
= isl_basic_set_total_dim(dom
) - n_div
;
596 total
= isl_map_dim(sol
->map
, isl_dim_all
);
597 bmap
= isl_basic_map_alloc_space(isl_map_get_space(sol
->map
),
598 n_div
, n_eq
, 2 * n_div
+ n_ineq
);
601 if (sol
->sol
.rational
)
602 ISL_F_SET(bmap
, ISL_BASIC_MAP_RATIONAL
);
603 for (i
= 0; i
< dom
->n_div
; ++i
) {
604 int k
= isl_basic_map_alloc_div(bmap
);
607 isl_seq_cpy(bmap
->div
[k
], dom
->div
[i
], 1 + 1 + nparam
);
608 isl_seq_clr(bmap
->div
[k
] + 1 + 1 + nparam
, total
- nparam
);
609 isl_seq_cpy(bmap
->div
[k
] + 1 + 1 + total
,
610 dom
->div
[i
] + 1 + 1 + nparam
, i
);
612 for (i
= 0; i
< dom
->n_eq
; ++i
) {
613 int k
= isl_basic_map_alloc_equality(bmap
);
616 isl_seq_cpy(bmap
->eq
[k
], dom
->eq
[i
], 1 + nparam
);
617 isl_seq_clr(bmap
->eq
[k
] + 1 + nparam
, total
- nparam
);
618 isl_seq_cpy(bmap
->eq
[k
] + 1 + total
,
619 dom
->eq
[i
] + 1 + nparam
, n_div
);
621 for (i
= 0; i
< dom
->n_ineq
; ++i
) {
622 int k
= isl_basic_map_alloc_inequality(bmap
);
625 isl_seq_cpy(bmap
->ineq
[k
], dom
->ineq
[i
], 1 + nparam
);
626 isl_seq_clr(bmap
->ineq
[k
] + 1 + nparam
, total
- nparam
);
627 isl_seq_cpy(bmap
->ineq
[k
] + 1 + total
,
628 dom
->ineq
[i
] + 1 + nparam
, n_div
);
630 for (i
= 0; i
< M
->n_row
- 1; ++i
) {
631 int k
= isl_basic_map_alloc_equality(bmap
);
634 isl_seq_cpy(bmap
->eq
[k
], M
->row
[1 + i
], 1 + nparam
);
635 isl_seq_clr(bmap
->eq
[k
] + 1 + nparam
, n_out
);
636 isl_int_neg(bmap
->eq
[k
][1 + nparam
+ i
], M
->row
[0][0]);
637 isl_seq_cpy(bmap
->eq
[k
] + 1 + nparam
+ n_out
,
638 M
->row
[1 + i
] + 1 + nparam
, n_div
);
640 bmap
= isl_basic_map_simplify(bmap
);
641 bmap
= isl_basic_map_finalize(bmap
);
642 sol
->map
= isl_map_grow(sol
->map
, 1);
643 sol
->map
= isl_map_add_basic_map(sol
->map
, bmap
);
644 isl_basic_set_free(dom
);
650 isl_basic_set_free(dom
);
652 isl_basic_map_free(bmap
);
656 static void sol_map_add_wrap(struct isl_sol
*sol
,
657 struct isl_basic_set
*dom
, struct isl_mat
*M
)
659 sol_map_add((struct isl_sol_map
*)sol
, dom
, M
);
663 /* Store the "parametric constant" of row "row" of tableau "tab" in "line",
664 * i.e., the constant term and the coefficients of all variables that
665 * appear in the context tableau.
666 * Note that the coefficient of the big parameter M is NOT copied.
667 * The context tableau may not have a big parameter and even when it
668 * does, it is a different big parameter.
670 static void get_row_parameter_line(struct isl_tab
*tab
, int row
, isl_int
*line
)
673 unsigned off
= 2 + tab
->M
;
675 isl_int_set(line
[0], tab
->mat
->row
[row
][1]);
676 for (i
= 0; i
< tab
->n_param
; ++i
) {
677 if (tab
->var
[i
].is_row
)
678 isl_int_set_si(line
[1 + i
], 0);
680 int col
= tab
->var
[i
].index
;
681 isl_int_set(line
[1 + i
], tab
->mat
->row
[row
][off
+ col
]);
684 for (i
= 0; i
< tab
->n_div
; ++i
) {
685 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
686 isl_int_set_si(line
[1 + tab
->n_param
+ i
], 0);
688 int col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
689 isl_int_set(line
[1 + tab
->n_param
+ i
],
690 tab
->mat
->row
[row
][off
+ col
]);
695 /* Check if rows "row1" and "row2" have identical "parametric constants",
696 * as explained above.
697 * In this case, we also insist that the coefficients of the big parameter
698 * be the same as the values of the constants will only be the same
699 * if these coefficients are also the same.
701 static int identical_parameter_line(struct isl_tab
*tab
, int row1
, int row2
)
704 unsigned off
= 2 + tab
->M
;
706 if (isl_int_ne(tab
->mat
->row
[row1
][1], tab
->mat
->row
[row2
][1]))
709 if (tab
->M
&& isl_int_ne(tab
->mat
->row
[row1
][2],
710 tab
->mat
->row
[row2
][2]))
713 for (i
= 0; i
< tab
->n_param
+ tab
->n_div
; ++i
) {
714 int pos
= i
< tab
->n_param
? i
:
715 tab
->n_var
- tab
->n_div
+ i
- tab
->n_param
;
718 if (tab
->var
[pos
].is_row
)
720 col
= tab
->var
[pos
].index
;
721 if (isl_int_ne(tab
->mat
->row
[row1
][off
+ col
],
722 tab
->mat
->row
[row2
][off
+ col
]))
728 /* Return an inequality that expresses that the "parametric constant"
729 * should be non-negative.
730 * This function is only called when the coefficient of the big parameter
733 static struct isl_vec
*get_row_parameter_ineq(struct isl_tab
*tab
, int row
)
735 struct isl_vec
*ineq
;
737 ineq
= isl_vec_alloc(tab
->mat
->ctx
, 1 + tab
->n_param
+ tab
->n_div
);
741 get_row_parameter_line(tab
, row
, ineq
->el
);
743 ineq
= isl_vec_normalize(ineq
);
748 /* Normalize a div expression of the form
750 * [(g*f(x) + c)/(g * m)]
752 * with c the constant term and f(x) the remaining coefficients, to
756 static void normalize_div(__isl_keep isl_vec
*div
)
758 isl_ctx
*ctx
= isl_vec_get_ctx(div
);
759 int len
= div
->size
- 2;
761 isl_seq_gcd(div
->el
+ 2, len
, &ctx
->normalize_gcd
);
762 isl_int_gcd(ctx
->normalize_gcd
, ctx
->normalize_gcd
, div
->el
[0]);
764 if (isl_int_is_one(ctx
->normalize_gcd
))
767 isl_int_divexact(div
->el
[0], div
->el
[0], ctx
->normalize_gcd
);
768 isl_int_fdiv_q(div
->el
[1], div
->el
[1], ctx
->normalize_gcd
);
769 isl_seq_scale_down(div
->el
+ 2, div
->el
+ 2, ctx
->normalize_gcd
, len
);
772 /* Return a integer division for use in a parametric cut based on the given row.
773 * In particular, let the parametric constant of the row be
777 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
778 * The div returned is equal to
780 * floor(\sum_i {-a_i} y_i) = floor((\sum_i (-a_i mod d) y_i)/d)
782 static struct isl_vec
*get_row_parameter_div(struct isl_tab
*tab
, int row
)
786 div
= isl_vec_alloc(tab
->mat
->ctx
, 1 + 1 + tab
->n_param
+ tab
->n_div
);
790 isl_int_set(div
->el
[0], tab
->mat
->row
[row
][0]);
791 get_row_parameter_line(tab
, row
, div
->el
+ 1);
792 isl_seq_neg(div
->el
+ 1, div
->el
+ 1, div
->size
- 1);
794 isl_seq_fdiv_r(div
->el
+ 1, div
->el
+ 1, div
->el
[0], div
->size
- 1);
799 /* Return a integer division for use in transferring an integrality constraint
801 * In particular, let the parametric constant of the row be
805 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
806 * The the returned div is equal to
808 * floor(\sum_i {a_i} y_i) = floor((\sum_i (a_i mod d) y_i)/d)
810 static struct isl_vec
*get_row_split_div(struct isl_tab
*tab
, int row
)
814 div
= isl_vec_alloc(tab
->mat
->ctx
, 1 + 1 + tab
->n_param
+ tab
->n_div
);
818 isl_int_set(div
->el
[0], tab
->mat
->row
[row
][0]);
819 get_row_parameter_line(tab
, row
, div
->el
+ 1);
821 isl_seq_fdiv_r(div
->el
+ 1, div
->el
+ 1, div
->el
[0], div
->size
- 1);
826 /* Construct and return an inequality that expresses an upper bound
828 * In particular, if the div is given by
832 * then the inequality expresses
836 static struct isl_vec
*ineq_for_div(struct isl_basic_set
*bset
, unsigned div
)
840 struct isl_vec
*ineq
;
845 total
= isl_basic_set_total_dim(bset
);
846 div_pos
= 1 + total
- bset
->n_div
+ div
;
848 ineq
= isl_vec_alloc(bset
->ctx
, 1 + total
);
852 isl_seq_cpy(ineq
->el
, bset
->div
[div
] + 1, 1 + total
);
853 isl_int_neg(ineq
->el
[div_pos
], bset
->div
[div
][0]);
857 /* Given a row in the tableau and a div that was created
858 * using get_row_split_div and that has been constrained to equality, i.e.,
860 * d = floor(\sum_i {a_i} y_i) = \sum_i {a_i} y_i
862 * replace the expression "\sum_i {a_i} y_i" in the row by d,
863 * i.e., we subtract "\sum_i {a_i} y_i" and add 1 d.
864 * The coefficients of the non-parameters in the tableau have been
865 * verified to be integral. We can therefore simply replace coefficient b
866 * by floor(b). For the coefficients of the parameters we have
867 * floor(a_i) = a_i - {a_i}, while for the other coefficients, we have
870 static struct isl_tab
*set_row_cst_to_div(struct isl_tab
*tab
, int row
, int div
)
872 isl_seq_fdiv_q(tab
->mat
->row
[row
] + 1, tab
->mat
->row
[row
] + 1,
873 tab
->mat
->row
[row
][0], 1 + tab
->M
+ tab
->n_col
);
875 isl_int_set_si(tab
->mat
->row
[row
][0], 1);
877 if (tab
->var
[tab
->n_var
- tab
->n_div
+ div
].is_row
) {
878 int drow
= tab
->var
[tab
->n_var
- tab
->n_div
+ div
].index
;
880 isl_assert(tab
->mat
->ctx
,
881 isl_int_is_one(tab
->mat
->row
[drow
][0]), goto error
);
882 isl_seq_combine(tab
->mat
->row
[row
] + 1,
883 tab
->mat
->ctx
->one
, tab
->mat
->row
[row
] + 1,
884 tab
->mat
->ctx
->one
, tab
->mat
->row
[drow
] + 1,
885 1 + tab
->M
+ tab
->n_col
);
887 int dcol
= tab
->var
[tab
->n_var
- tab
->n_div
+ div
].index
;
889 isl_int_add_ui(tab
->mat
->row
[row
][2 + tab
->M
+ dcol
],
890 tab
->mat
->row
[row
][2 + tab
->M
+ dcol
], 1);
899 /* Check if the (parametric) constant of the given row is obviously
900 * negative, meaning that we don't need to consult the context tableau.
901 * If there is a big parameter and its coefficient is non-zero,
902 * then this coefficient determines the outcome.
903 * Otherwise, we check whether the constant is negative and
904 * all non-zero coefficients of parameters are negative and
905 * belong to non-negative parameters.
907 static int is_obviously_neg(struct isl_tab
*tab
, int row
)
911 unsigned off
= 2 + tab
->M
;
914 if (isl_int_is_pos(tab
->mat
->row
[row
][2]))
916 if (isl_int_is_neg(tab
->mat
->row
[row
][2]))
920 if (isl_int_is_nonneg(tab
->mat
->row
[row
][1]))
922 for (i
= 0; i
< tab
->n_param
; ++i
) {
923 /* Eliminated parameter */
924 if (tab
->var
[i
].is_row
)
926 col
= tab
->var
[i
].index
;
927 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
929 if (!tab
->var
[i
].is_nonneg
)
931 if (isl_int_is_pos(tab
->mat
->row
[row
][off
+ col
]))
934 for (i
= 0; i
< tab
->n_div
; ++i
) {
935 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
937 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
938 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
940 if (!tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_nonneg
)
942 if (isl_int_is_pos(tab
->mat
->row
[row
][off
+ col
]))
948 /* Check if the (parametric) constant of the given row is obviously
949 * non-negative, meaning that we don't need to consult the context tableau.
950 * If there is a big parameter and its coefficient is non-zero,
951 * then this coefficient determines the outcome.
952 * Otherwise, we check whether the constant is non-negative and
953 * all non-zero coefficients of parameters are positive and
954 * belong to non-negative parameters.
956 static int is_obviously_nonneg(struct isl_tab
*tab
, int row
)
960 unsigned off
= 2 + tab
->M
;
963 if (isl_int_is_pos(tab
->mat
->row
[row
][2]))
965 if (isl_int_is_neg(tab
->mat
->row
[row
][2]))
969 if (isl_int_is_neg(tab
->mat
->row
[row
][1]))
971 for (i
= 0; i
< tab
->n_param
; ++i
) {
972 /* Eliminated parameter */
973 if (tab
->var
[i
].is_row
)
975 col
= tab
->var
[i
].index
;
976 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
978 if (!tab
->var
[i
].is_nonneg
)
980 if (isl_int_is_neg(tab
->mat
->row
[row
][off
+ col
]))
983 for (i
= 0; i
< tab
->n_div
; ++i
) {
984 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
986 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
987 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
989 if (!tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_nonneg
)
991 if (isl_int_is_neg(tab
->mat
->row
[row
][off
+ col
]))
997 /* Given a row r and two columns, return the column that would
998 * lead to the lexicographically smallest increment in the sample
999 * solution when leaving the basis in favor of the row.
1000 * Pivoting with column c will increment the sample value by a non-negative
1001 * constant times a_{V,c}/a_{r,c}, with a_{V,c} the elements of column c
1002 * corresponding to the non-parametric variables.
1003 * If variable v appears in a column c_v, the a_{v,c} = 1 iff c = c_v,
1004 * with all other entries in this virtual row equal to zero.
1005 * If variable v appears in a row, then a_{v,c} is the element in column c
1008 * Let v be the first variable with a_{v,c1}/a_{r,c1} != a_{v,c2}/a_{r,c2}.
1009 * Then if a_{v,c1}/a_{r,c1} < a_{v,c2}/a_{r,c2}, i.e.,
1010 * a_{v,c2} a_{r,c1} - a_{v,c1} a_{r,c2} > 0, c1 results in the minimal
1011 * increment. Otherwise, it's c2.
1013 static int lexmin_col_pair(struct isl_tab
*tab
,
1014 int row
, int col1
, int col2
, isl_int tmp
)
1019 tr
= tab
->mat
->row
[row
] + 2 + tab
->M
;
1021 for (i
= tab
->n_param
; i
< tab
->n_var
- tab
->n_div
; ++i
) {
1025 if (!tab
->var
[i
].is_row
) {
1026 if (tab
->var
[i
].index
== col1
)
1028 if (tab
->var
[i
].index
== col2
)
1033 if (tab
->var
[i
].index
== row
)
1036 r
= tab
->mat
->row
[tab
->var
[i
].index
] + 2 + tab
->M
;
1037 s1
= isl_int_sgn(r
[col1
]);
1038 s2
= isl_int_sgn(r
[col2
]);
1039 if (s1
== 0 && s2
== 0)
1046 isl_int_mul(tmp
, r
[col2
], tr
[col1
]);
1047 isl_int_submul(tmp
, r
[col1
], tr
[col2
]);
1048 if (isl_int_is_pos(tmp
))
1050 if (isl_int_is_neg(tmp
))
1056 /* Given a row in the tableau, find and return the column that would
1057 * result in the lexicographically smallest, but positive, increment
1058 * in the sample point.
1059 * If there is no such column, then return tab->n_col.
1060 * If anything goes wrong, return -1.
1062 static int lexmin_pivot_col(struct isl_tab
*tab
, int row
)
1065 int col
= tab
->n_col
;
1069 tr
= tab
->mat
->row
[row
] + 2 + tab
->M
;
1073 for (j
= tab
->n_dead
; j
< tab
->n_col
; ++j
) {
1074 if (tab
->col_var
[j
] >= 0 &&
1075 (tab
->col_var
[j
] < tab
->n_param
||
1076 tab
->col_var
[j
] >= tab
->n_var
- tab
->n_div
))
1079 if (!isl_int_is_pos(tr
[j
]))
1082 if (col
== tab
->n_col
)
1085 col
= lexmin_col_pair(tab
, row
, col
, j
, tmp
);
1086 isl_assert(tab
->mat
->ctx
, col
>= 0, goto error
);
1096 /* Return the first known violated constraint, i.e., a non-negative
1097 * constraint that currently has an either obviously negative value
1098 * or a previously determined to be negative value.
1100 * If any constraint has a negative coefficient for the big parameter,
1101 * if any, then we return one of these first.
1103 static int first_neg(struct isl_tab
*tab
)
1108 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
1109 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
1111 if (!isl_int_is_neg(tab
->mat
->row
[row
][2]))
1114 tab
->row_sign
[row
] = isl_tab_row_neg
;
1117 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
1118 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
1120 if (tab
->row_sign
) {
1121 if (tab
->row_sign
[row
] == 0 &&
1122 is_obviously_neg(tab
, row
))
1123 tab
->row_sign
[row
] = isl_tab_row_neg
;
1124 if (tab
->row_sign
[row
] != isl_tab_row_neg
)
1126 } else if (!is_obviously_neg(tab
, row
))
1133 /* Check whether the invariant that all columns are lexico-positive
1134 * is satisfied. This function is not called from the current code
1135 * but is useful during debugging.
1137 static void check_lexpos(struct isl_tab
*tab
) __attribute__ ((unused
));
1138 static void check_lexpos(struct isl_tab
*tab
)
1140 unsigned off
= 2 + tab
->M
;
1145 for (col
= tab
->n_dead
; col
< tab
->n_col
; ++col
) {
1146 if (tab
->col_var
[col
] >= 0 &&
1147 (tab
->col_var
[col
] < tab
->n_param
||
1148 tab
->col_var
[col
] >= tab
->n_var
- tab
->n_div
))
1150 for (var
= tab
->n_param
; var
< tab
->n_var
- tab
->n_div
; ++var
) {
1151 if (!tab
->var
[var
].is_row
) {
1152 if (tab
->var
[var
].index
== col
)
1157 row
= tab
->var
[var
].index
;
1158 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
1160 if (isl_int_is_pos(tab
->mat
->row
[row
][off
+ col
]))
1162 fprintf(stderr
, "lexneg column %d (row %d)\n",
1165 if (var
>= tab
->n_var
- tab
->n_div
)
1166 fprintf(stderr
, "zero column %d\n", col
);
1170 /* Report to the caller that the given constraint is part of an encountered
1173 static int report_conflicting_constraint(struct isl_tab
*tab
, int con
)
1175 return tab
->conflict(con
, tab
->conflict_user
);
1178 /* Given a conflicting row in the tableau, report all constraints
1179 * involved in the row to the caller. That is, the row itself
1180 * (if it represents a constraint) and all constraint columns with
1181 * non-zero (and therefore negative) coefficients.
1183 static int report_conflict(struct isl_tab
*tab
, int row
)
1191 if (tab
->row_var
[row
] < 0 &&
1192 report_conflicting_constraint(tab
, ~tab
->row_var
[row
]) < 0)
1195 tr
= tab
->mat
->row
[row
] + 2 + tab
->M
;
1197 for (j
= tab
->n_dead
; j
< tab
->n_col
; ++j
) {
1198 if (tab
->col_var
[j
] >= 0 &&
1199 (tab
->col_var
[j
] < tab
->n_param
||
1200 tab
->col_var
[j
] >= tab
->n_var
- tab
->n_div
))
1203 if (!isl_int_is_neg(tr
[j
]))
1206 if (tab
->col_var
[j
] < 0 &&
1207 report_conflicting_constraint(tab
, ~tab
->col_var
[j
]) < 0)
1214 /* Resolve all known or obviously violated constraints through pivoting.
1215 * In particular, as long as we can find any violated constraint, we
1216 * look for a pivoting column that would result in the lexicographically
1217 * smallest increment in the sample point. If there is no such column
1218 * then the tableau is infeasible.
1220 static int restore_lexmin(struct isl_tab
*tab
) WARN_UNUSED
;
1221 static int restore_lexmin(struct isl_tab
*tab
)
1229 while ((row
= first_neg(tab
)) != -1) {
1230 col
= lexmin_pivot_col(tab
, row
);
1231 if (col
>= tab
->n_col
) {
1232 if (report_conflict(tab
, row
) < 0)
1234 if (isl_tab_mark_empty(tab
) < 0)
1240 if (isl_tab_pivot(tab
, row
, col
) < 0)
1246 /* Given a row that represents an equality, look for an appropriate
1248 * In particular, if there are any non-zero coefficients among
1249 * the non-parameter variables, then we take the last of these
1250 * variables. Eliminating this variable in terms of the other
1251 * variables and/or parameters does not influence the property
1252 * that all column in the initial tableau are lexicographically
1253 * positive. The row corresponding to the eliminated variable
1254 * will only have non-zero entries below the diagonal of the
1255 * initial tableau. That is, we transform
1261 * If there is no such non-parameter variable, then we are dealing with
1262 * pure parameter equality and we pick any parameter with coefficient 1 or -1
1263 * for elimination. This will ensure that the eliminated parameter
1264 * always has an integer value whenever all the other parameters are integral.
1265 * If there is no such parameter then we return -1.
1267 static int last_var_col_or_int_par_col(struct isl_tab
*tab
, int row
)
1269 unsigned off
= 2 + tab
->M
;
1272 for (i
= tab
->n_var
- tab
->n_div
- 1; i
>= 0 && i
>= tab
->n_param
; --i
) {
1274 if (tab
->var
[i
].is_row
)
1276 col
= tab
->var
[i
].index
;
1277 if (col
<= tab
->n_dead
)
1279 if (!isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
1282 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
1283 if (isl_int_is_one(tab
->mat
->row
[row
][off
+ i
]))
1285 if (isl_int_is_negone(tab
->mat
->row
[row
][off
+ i
]))
1291 /* Add an equality that is known to be valid to the tableau.
1292 * We first check if we can eliminate a variable or a parameter.
1293 * If not, we add the equality as two inequalities.
1294 * In this case, the equality was a pure parameter equality and there
1295 * is no need to resolve any constraint violations.
1297 static struct isl_tab
*add_lexmin_valid_eq(struct isl_tab
*tab
, isl_int
*eq
)
1304 r
= isl_tab_add_row(tab
, eq
);
1308 r
= tab
->con
[r
].index
;
1309 i
= last_var_col_or_int_par_col(tab
, r
);
1311 tab
->con
[r
].is_nonneg
= 1;
1312 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
1314 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
1315 r
= isl_tab_add_row(tab
, eq
);
1318 tab
->con
[r
].is_nonneg
= 1;
1319 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
1322 if (isl_tab_pivot(tab
, r
, i
) < 0)
1324 if (isl_tab_kill_col(tab
, i
) < 0)
1335 /* Check if the given row is a pure constant.
1337 static int is_constant(struct isl_tab
*tab
, int row
)
1339 unsigned off
= 2 + tab
->M
;
1341 return isl_seq_first_non_zero(tab
->mat
->row
[row
] + off
+ tab
->n_dead
,
1342 tab
->n_col
- tab
->n_dead
) == -1;
1345 /* Add an equality that may or may not be valid to the tableau.
1346 * If the resulting row is a pure constant, then it must be zero.
1347 * Otherwise, the resulting tableau is empty.
1349 * If the row is not a pure constant, then we add two inequalities,
1350 * each time checking that they can be satisfied.
1351 * In the end we try to use one of the two constraints to eliminate
1354 static int add_lexmin_eq(struct isl_tab
*tab
, isl_int
*eq
) WARN_UNUSED
;
1355 static int add_lexmin_eq(struct isl_tab
*tab
, isl_int
*eq
)
1359 struct isl_tab_undo
*snap
;
1363 snap
= isl_tab_snap(tab
);
1364 r1
= isl_tab_add_row(tab
, eq
);
1367 tab
->con
[r1
].is_nonneg
= 1;
1368 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r1
]) < 0)
1371 row
= tab
->con
[r1
].index
;
1372 if (is_constant(tab
, row
)) {
1373 if (!isl_int_is_zero(tab
->mat
->row
[row
][1]) ||
1374 (tab
->M
&& !isl_int_is_zero(tab
->mat
->row
[row
][2]))) {
1375 if (isl_tab_mark_empty(tab
) < 0)
1379 if (isl_tab_rollback(tab
, snap
) < 0)
1384 if (restore_lexmin(tab
) < 0)
1389 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
1391 r2
= isl_tab_add_row(tab
, eq
);
1394 tab
->con
[r2
].is_nonneg
= 1;
1395 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r2
]) < 0)
1398 if (restore_lexmin(tab
) < 0)
1403 if (!tab
->con
[r1
].is_row
) {
1404 if (isl_tab_kill_col(tab
, tab
->con
[r1
].index
) < 0)
1406 } else if (!tab
->con
[r2
].is_row
) {
1407 if (isl_tab_kill_col(tab
, tab
->con
[r2
].index
) < 0)
1412 tab
->bmap
= isl_basic_map_add_ineq(tab
->bmap
, eq
);
1413 if (isl_tab_push(tab
, isl_tab_undo_bmap_ineq
) < 0)
1415 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
1416 tab
->bmap
= isl_basic_map_add_ineq(tab
->bmap
, eq
);
1417 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
1418 if (isl_tab_push(tab
, isl_tab_undo_bmap_ineq
) < 0)
1427 /* Add an inequality to the tableau, resolving violations using
1430 static struct isl_tab
*add_lexmin_ineq(struct isl_tab
*tab
, isl_int
*ineq
)
1437 tab
->bmap
= isl_basic_map_add_ineq(tab
->bmap
, ineq
);
1438 if (isl_tab_push(tab
, isl_tab_undo_bmap_ineq
) < 0)
1443 r
= isl_tab_add_row(tab
, ineq
);
1446 tab
->con
[r
].is_nonneg
= 1;
1447 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
1449 if (isl_tab_row_is_redundant(tab
, tab
->con
[r
].index
)) {
1450 if (isl_tab_mark_redundant(tab
, tab
->con
[r
].index
) < 0)
1455 if (restore_lexmin(tab
) < 0)
1457 if (!tab
->empty
&& tab
->con
[r
].is_row
&&
1458 isl_tab_row_is_redundant(tab
, tab
->con
[r
].index
))
1459 if (isl_tab_mark_redundant(tab
, tab
->con
[r
].index
) < 0)
1467 /* Check if the coefficients of the parameters are all integral.
1469 static int integer_parameter(struct isl_tab
*tab
, int row
)
1473 unsigned off
= 2 + tab
->M
;
1475 for (i
= 0; i
< tab
->n_param
; ++i
) {
1476 /* Eliminated parameter */
1477 if (tab
->var
[i
].is_row
)
1479 col
= tab
->var
[i
].index
;
1480 if (!isl_int_is_divisible_by(tab
->mat
->row
[row
][off
+ col
],
1481 tab
->mat
->row
[row
][0]))
1484 for (i
= 0; i
< tab
->n_div
; ++i
) {
1485 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
1487 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
1488 if (!isl_int_is_divisible_by(tab
->mat
->row
[row
][off
+ col
],
1489 tab
->mat
->row
[row
][0]))
1495 /* Check if the coefficients of the non-parameter variables are all integral.
1497 static int integer_variable(struct isl_tab
*tab
, int row
)
1500 unsigned off
= 2 + tab
->M
;
1502 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
1503 if (tab
->col_var
[i
] >= 0 &&
1504 (tab
->col_var
[i
] < tab
->n_param
||
1505 tab
->col_var
[i
] >= tab
->n_var
- tab
->n_div
))
1507 if (!isl_int_is_divisible_by(tab
->mat
->row
[row
][off
+ i
],
1508 tab
->mat
->row
[row
][0]))
1514 /* Check if the constant term is integral.
1516 static int integer_constant(struct isl_tab
*tab
, int row
)
1518 return isl_int_is_divisible_by(tab
->mat
->row
[row
][1],
1519 tab
->mat
->row
[row
][0]);
1522 #define I_CST 1 << 0
1523 #define I_PAR 1 << 1
1524 #define I_VAR 1 << 2
1526 /* Check for next (non-parameter) variable after "var" (first if var == -1)
1527 * that is non-integer and therefore requires a cut and return
1528 * the index of the variable.
1529 * For parametric tableaus, there are three parts in a row,
1530 * the constant, the coefficients of the parameters and the rest.
1531 * For each part, we check whether the coefficients in that part
1532 * are all integral and if so, set the corresponding flag in *f.
1533 * If the constant and the parameter part are integral, then the
1534 * current sample value is integral and no cut is required
1535 * (irrespective of whether the variable part is integral).
1537 static int next_non_integer_var(struct isl_tab
*tab
, int var
, int *f
)
1539 var
= var
< 0 ? tab
->n_param
: var
+ 1;
1541 for (; var
< tab
->n_var
- tab
->n_div
; ++var
) {
1544 if (!tab
->var
[var
].is_row
)
1546 row
= tab
->var
[var
].index
;
1547 if (integer_constant(tab
, row
))
1548 ISL_FL_SET(flags
, I_CST
);
1549 if (integer_parameter(tab
, row
))
1550 ISL_FL_SET(flags
, I_PAR
);
1551 if (ISL_FL_ISSET(flags
, I_CST
) && ISL_FL_ISSET(flags
, I_PAR
))
1553 if (integer_variable(tab
, row
))
1554 ISL_FL_SET(flags
, I_VAR
);
1561 /* Check for first (non-parameter) variable that is non-integer and
1562 * therefore requires a cut and return the corresponding row.
1563 * For parametric tableaus, there are three parts in a row,
1564 * the constant, the coefficients of the parameters and the rest.
1565 * For each part, we check whether the coefficients in that part
1566 * are all integral and if so, set the corresponding flag in *f.
1567 * If the constant and the parameter part are integral, then the
1568 * current sample value is integral and no cut is required
1569 * (irrespective of whether the variable part is integral).
1571 static int first_non_integer_row(struct isl_tab
*tab
, int *f
)
1573 int var
= next_non_integer_var(tab
, -1, f
);
1575 return var
< 0 ? -1 : tab
->var
[var
].index
;
1578 /* Add a (non-parametric) cut to cut away the non-integral sample
1579 * value of the given row.
1581 * If the row is given by
1583 * m r = f + \sum_i a_i y_i
1587 * c = - {-f/m} + \sum_i {a_i/m} y_i >= 0
1589 * The big parameter, if any, is ignored, since it is assumed to be big
1590 * enough to be divisible by any integer.
1591 * If the tableau is actually a parametric tableau, then this function
1592 * is only called when all coefficients of the parameters are integral.
1593 * The cut therefore has zero coefficients for the parameters.
1595 * The current value is known to be negative, so row_sign, if it
1596 * exists, is set accordingly.
1598 * Return the row of the cut or -1.
1600 static int add_cut(struct isl_tab
*tab
, int row
)
1605 unsigned off
= 2 + tab
->M
;
1607 if (isl_tab_extend_cons(tab
, 1) < 0)
1609 r
= isl_tab_allocate_con(tab
);
1613 r_row
= tab
->mat
->row
[tab
->con
[r
].index
];
1614 isl_int_set(r_row
[0], tab
->mat
->row
[row
][0]);
1615 isl_int_neg(r_row
[1], tab
->mat
->row
[row
][1]);
1616 isl_int_fdiv_r(r_row
[1], r_row
[1], tab
->mat
->row
[row
][0]);
1617 isl_int_neg(r_row
[1], r_row
[1]);
1619 isl_int_set_si(r_row
[2], 0);
1620 for (i
= 0; i
< tab
->n_col
; ++i
)
1621 isl_int_fdiv_r(r_row
[off
+ i
],
1622 tab
->mat
->row
[row
][off
+ i
], tab
->mat
->row
[row
][0]);
1624 tab
->con
[r
].is_nonneg
= 1;
1625 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
1628 tab
->row_sign
[tab
->con
[r
].index
] = isl_tab_row_neg
;
1630 return tab
->con
[r
].index
;
1636 /* Given a non-parametric tableau, add cuts until an integer
1637 * sample point is obtained or until the tableau is determined
1638 * to be integer infeasible.
1639 * As long as there is any non-integer value in the sample point,
1640 * we add appropriate cuts, if possible, for each of these
1641 * non-integer values and then resolve the violated
1642 * cut constraints using restore_lexmin.
1643 * If one of the corresponding rows is equal to an integral
1644 * combination of variables/constraints plus a non-integral constant,
1645 * then there is no way to obtain an integer point and we return
1646 * a tableau that is marked empty.
1647 * The parameter cutting_strategy controls the strategy used when adding cuts
1648 * to remove non-integer points. CUT_ALL adds all possible cuts
1649 * before continuing the search. CUT_ONE adds only one cut at a time.
1651 static struct isl_tab
*cut_to_integer_lexmin(struct isl_tab
*tab
,
1652 int cutting_strategy
)
1663 while ((var
= next_non_integer_var(tab
, -1, &flags
)) != -1) {
1665 if (ISL_FL_ISSET(flags
, I_VAR
)) {
1666 if (isl_tab_mark_empty(tab
) < 0)
1670 row
= tab
->var
[var
].index
;
1671 row
= add_cut(tab
, row
);
1674 if (cutting_strategy
== CUT_ONE
)
1676 } while ((var
= next_non_integer_var(tab
, var
, &flags
)) != -1);
1677 if (restore_lexmin(tab
) < 0)
1688 /* Check whether all the currently active samples also satisfy the inequality
1689 * "ineq" (treated as an equality if eq is set).
1690 * Remove those samples that do not.
1692 static struct isl_tab
*check_samples(struct isl_tab
*tab
, isl_int
*ineq
, int eq
)
1700 isl_assert(tab
->mat
->ctx
, tab
->bmap
, goto error
);
1701 isl_assert(tab
->mat
->ctx
, tab
->samples
, goto error
);
1702 isl_assert(tab
->mat
->ctx
, tab
->samples
->n_col
== 1 + tab
->n_var
, goto error
);
1705 for (i
= tab
->n_outside
; i
< tab
->n_sample
; ++i
) {
1707 isl_seq_inner_product(ineq
, tab
->samples
->row
[i
],
1708 1 + tab
->n_var
, &v
);
1709 sgn
= isl_int_sgn(v
);
1710 if (eq
? (sgn
== 0) : (sgn
>= 0))
1712 tab
= isl_tab_drop_sample(tab
, i
);
1724 /* Check whether the sample value of the tableau is finite,
1725 * i.e., either the tableau does not use a big parameter, or
1726 * all values of the variables are equal to the big parameter plus
1727 * some constant. This constant is the actual sample value.
1729 static int sample_is_finite(struct isl_tab
*tab
)
1736 for (i
= 0; i
< tab
->n_var
; ++i
) {
1738 if (!tab
->var
[i
].is_row
)
1740 row
= tab
->var
[i
].index
;
1741 if (isl_int_ne(tab
->mat
->row
[row
][0], tab
->mat
->row
[row
][2]))
1747 /* Check if the context tableau of sol has any integer points.
1748 * Leave tab in empty state if no integer point can be found.
1749 * If an integer point can be found and if moreover it is finite,
1750 * then it is added to the list of sample values.
1752 * This function is only called when none of the currently active sample
1753 * values satisfies the most recently added constraint.
1755 static struct isl_tab
*check_integer_feasible(struct isl_tab
*tab
)
1757 struct isl_tab_undo
*snap
;
1762 snap
= isl_tab_snap(tab
);
1763 if (isl_tab_push_basis(tab
) < 0)
1766 tab
= cut_to_integer_lexmin(tab
, CUT_ALL
);
1770 if (!tab
->empty
&& sample_is_finite(tab
)) {
1771 struct isl_vec
*sample
;
1773 sample
= isl_tab_get_sample_value(tab
);
1775 tab
= isl_tab_add_sample(tab
, sample
);
1778 if (!tab
->empty
&& isl_tab_rollback(tab
, snap
) < 0)
1787 /* Check if any of the currently active sample values satisfies
1788 * the inequality "ineq" (an equality if eq is set).
1790 static int tab_has_valid_sample(struct isl_tab
*tab
, isl_int
*ineq
, int eq
)
1798 isl_assert(tab
->mat
->ctx
, tab
->bmap
, return -1);
1799 isl_assert(tab
->mat
->ctx
, tab
->samples
, return -1);
1800 isl_assert(tab
->mat
->ctx
, tab
->samples
->n_col
== 1 + tab
->n_var
, return -1);
1803 for (i
= tab
->n_outside
; i
< tab
->n_sample
; ++i
) {
1805 isl_seq_inner_product(ineq
, tab
->samples
->row
[i
],
1806 1 + tab
->n_var
, &v
);
1807 sgn
= isl_int_sgn(v
);
1808 if (eq
? (sgn
== 0) : (sgn
>= 0))
1813 return i
< tab
->n_sample
;
1816 /* Add a div specified by "div" to the tableau "tab" and return
1817 * 1 if the div is obviously non-negative.
1819 static int context_tab_add_div(struct isl_tab
*tab
, struct isl_vec
*div
,
1820 int (*add_ineq
)(void *user
, isl_int
*), void *user
)
1824 struct isl_mat
*samples
;
1827 r
= isl_tab_add_div(tab
, div
, add_ineq
, user
);
1830 nonneg
= tab
->var
[r
].is_nonneg
;
1831 tab
->var
[r
].frozen
= 1;
1833 samples
= isl_mat_extend(tab
->samples
,
1834 tab
->n_sample
, 1 + tab
->n_var
);
1835 tab
->samples
= samples
;
1838 for (i
= tab
->n_outside
; i
< samples
->n_row
; ++i
) {
1839 isl_seq_inner_product(div
->el
+ 1, samples
->row
[i
],
1840 div
->size
- 1, &samples
->row
[i
][samples
->n_col
- 1]);
1841 isl_int_fdiv_q(samples
->row
[i
][samples
->n_col
- 1],
1842 samples
->row
[i
][samples
->n_col
- 1], div
->el
[0]);
1848 /* Add a div specified by "div" to both the main tableau and
1849 * the context tableau. In case of the main tableau, we only
1850 * need to add an extra div. In the context tableau, we also
1851 * need to express the meaning of the div.
1852 * Return the index of the div or -1 if anything went wrong.
1854 static int add_div(struct isl_tab
*tab
, struct isl_context
*context
,
1855 struct isl_vec
*div
)
1860 if ((nonneg
= context
->op
->add_div(context
, div
)) < 0)
1863 if (!context
->op
->is_ok(context
))
1866 if (isl_tab_extend_vars(tab
, 1) < 0)
1868 r
= isl_tab_allocate_var(tab
);
1872 tab
->var
[r
].is_nonneg
= 1;
1873 tab
->var
[r
].frozen
= 1;
1876 return tab
->n_div
- 1;
1878 context
->op
->invalidate(context
);
1882 static int find_div(struct isl_tab
*tab
, isl_int
*div
, isl_int denom
)
1885 unsigned total
= isl_basic_map_total_dim(tab
->bmap
);
1887 for (i
= 0; i
< tab
->bmap
->n_div
; ++i
) {
1888 if (isl_int_ne(tab
->bmap
->div
[i
][0], denom
))
1890 if (!isl_seq_eq(tab
->bmap
->div
[i
] + 1, div
, 1 + total
))
1897 /* Return the index of a div that corresponds to "div".
1898 * We first check if we already have such a div and if not, we create one.
1900 static int get_div(struct isl_tab
*tab
, struct isl_context
*context
,
1901 struct isl_vec
*div
)
1904 struct isl_tab
*context_tab
= context
->op
->peek_tab(context
);
1909 d
= find_div(context_tab
, div
->el
+ 1, div
->el
[0]);
1913 return add_div(tab
, context
, div
);
1916 /* Add a parametric cut to cut away the non-integral sample value
1918 * Let a_i be the coefficients of the constant term and the parameters
1919 * and let b_i be the coefficients of the variables or constraints
1920 * in basis of the tableau.
1921 * Let q be the div q = floor(\sum_i {-a_i} y_i).
1923 * The cut is expressed as
1925 * c = \sum_i -{-a_i} y_i + \sum_i {b_i} x_i + q >= 0
1927 * If q did not already exist in the context tableau, then it is added first.
1928 * If q is in a column of the main tableau then the "+ q" can be accomplished
1929 * by setting the corresponding entry to the denominator of the constraint.
1930 * If q happens to be in a row of the main tableau, then the corresponding
1931 * row needs to be added instead (taking care of the denominators).
1932 * Note that this is very unlikely, but perhaps not entirely impossible.
1934 * The current value of the cut is known to be negative (or at least
1935 * non-positive), so row_sign is set accordingly.
1937 * Return the row of the cut or -1.
1939 static int add_parametric_cut(struct isl_tab
*tab
, int row
,
1940 struct isl_context
*context
)
1942 struct isl_vec
*div
;
1949 unsigned off
= 2 + tab
->M
;
1954 div
= get_row_parameter_div(tab
, row
);
1959 d
= context
->op
->get_div(context
, tab
, div
);
1963 if (isl_tab_extend_cons(tab
, 1) < 0)
1965 r
= isl_tab_allocate_con(tab
);
1969 r_row
= tab
->mat
->row
[tab
->con
[r
].index
];
1970 isl_int_set(r_row
[0], tab
->mat
->row
[row
][0]);
1971 isl_int_neg(r_row
[1], tab
->mat
->row
[row
][1]);
1972 isl_int_fdiv_r(r_row
[1], r_row
[1], tab
->mat
->row
[row
][0]);
1973 isl_int_neg(r_row
[1], r_row
[1]);
1975 isl_int_set_si(r_row
[2], 0);
1976 for (i
= 0; i
< tab
->n_param
; ++i
) {
1977 if (tab
->var
[i
].is_row
)
1979 col
= tab
->var
[i
].index
;
1980 isl_int_neg(r_row
[off
+ col
], tab
->mat
->row
[row
][off
+ col
]);
1981 isl_int_fdiv_r(r_row
[off
+ col
], r_row
[off
+ col
],
1982 tab
->mat
->row
[row
][0]);
1983 isl_int_neg(r_row
[off
+ col
], r_row
[off
+ col
]);
1985 for (i
= 0; i
< tab
->n_div
; ++i
) {
1986 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
1988 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ 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_col
; ++i
) {
1995 if (tab
->col_var
[i
] >= 0 &&
1996 (tab
->col_var
[i
] < tab
->n_param
||
1997 tab
->col_var
[i
] >= tab
->n_var
- tab
->n_div
))
1999 isl_int_fdiv_r(r_row
[off
+ i
],
2000 tab
->mat
->row
[row
][off
+ i
], tab
->mat
->row
[row
][0]);
2002 if (tab
->var
[tab
->n_var
- tab
->n_div
+ d
].is_row
) {
2004 int d_row
= tab
->var
[tab
->n_var
- tab
->n_div
+ d
].index
;
2006 isl_int_gcd(gcd
, tab
->mat
->row
[d_row
][0], r_row
[0]);
2007 isl_int_divexact(r_row
[0], r_row
[0], gcd
);
2008 isl_int_divexact(gcd
, tab
->mat
->row
[d_row
][0], gcd
);
2009 isl_seq_combine(r_row
+ 1, gcd
, r_row
+ 1,
2010 r_row
[0], tab
->mat
->row
[d_row
] + 1,
2011 off
- 1 + tab
->n_col
);
2012 isl_int_mul(r_row
[0], r_row
[0], tab
->mat
->row
[d_row
][0]);
2015 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ d
].index
;
2016 isl_int_set(r_row
[off
+ col
], tab
->mat
->row
[row
][0]);
2019 tab
->con
[r
].is_nonneg
= 1;
2020 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
2023 tab
->row_sign
[tab
->con
[r
].index
] = isl_tab_row_neg
;
2027 row
= tab
->con
[r
].index
;
2029 if (d
>= n
&& context
->op
->detect_equalities(context
, tab
) < 0)
2035 /* Construct a tableau for bmap that can be used for computing
2036 * the lexicographic minimum (or maximum) of bmap.
2037 * If not NULL, then dom is the domain where the minimum
2038 * should be computed. In this case, we set up a parametric
2039 * tableau with row signs (initialized to "unknown").
2040 * If M is set, then the tableau will use a big parameter.
2041 * If max is set, then a maximum should be computed instead of a minimum.
2042 * This means that for each variable x, the tableau will contain the variable
2043 * x' = M - x, rather than x' = M + x. This in turn means that the coefficient
2044 * of the variables in all constraints are negated prior to adding them
2047 static struct isl_tab
*tab_for_lexmin(struct isl_basic_map
*bmap
,
2048 struct isl_basic_set
*dom
, unsigned M
, int max
)
2051 struct isl_tab
*tab
;
2053 tab
= isl_tab_alloc(bmap
->ctx
, 2 * bmap
->n_eq
+ bmap
->n_ineq
+ 1,
2054 isl_basic_map_total_dim(bmap
), M
);
2058 tab
->rational
= ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
2060 tab
->n_param
= isl_basic_set_total_dim(dom
) - dom
->n_div
;
2061 tab
->n_div
= dom
->n_div
;
2062 tab
->row_sign
= isl_calloc_array(bmap
->ctx
,
2063 enum isl_tab_row_sign
, tab
->mat
->n_row
);
2067 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
)) {
2068 if (isl_tab_mark_empty(tab
) < 0)
2073 for (i
= tab
->n_param
; i
< tab
->n_var
- tab
->n_div
; ++i
) {
2074 tab
->var
[i
].is_nonneg
= 1;
2075 tab
->var
[i
].frozen
= 1;
2077 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
2079 isl_seq_neg(bmap
->eq
[i
] + 1 + tab
->n_param
,
2080 bmap
->eq
[i
] + 1 + tab
->n_param
,
2081 tab
->n_var
- tab
->n_param
- tab
->n_div
);
2082 tab
= add_lexmin_valid_eq(tab
, bmap
->eq
[i
]);
2084 isl_seq_neg(bmap
->eq
[i
] + 1 + tab
->n_param
,
2085 bmap
->eq
[i
] + 1 + tab
->n_param
,
2086 tab
->n_var
- tab
->n_param
- tab
->n_div
);
2087 if (!tab
|| tab
->empty
)
2090 if (bmap
->n_eq
&& restore_lexmin(tab
) < 0)
2092 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
2094 isl_seq_neg(bmap
->ineq
[i
] + 1 + tab
->n_param
,
2095 bmap
->ineq
[i
] + 1 + tab
->n_param
,
2096 tab
->n_var
- tab
->n_param
- tab
->n_div
);
2097 tab
= add_lexmin_ineq(tab
, bmap
->ineq
[i
]);
2099 isl_seq_neg(bmap
->ineq
[i
] + 1 + tab
->n_param
,
2100 bmap
->ineq
[i
] + 1 + tab
->n_param
,
2101 tab
->n_var
- tab
->n_param
- tab
->n_div
);
2102 if (!tab
|| tab
->empty
)
2111 /* Given a main tableau where more than one row requires a split,
2112 * determine and return the "best" row to split on.
2114 * Given two rows in the main tableau, if the inequality corresponding
2115 * to the first row is redundant with respect to that of the second row
2116 * in the current tableau, then it is better to split on the second row,
2117 * since in the positive part, both row will be positive.
2118 * (In the negative part a pivot will have to be performed and just about
2119 * anything can happen to the sign of the other row.)
2121 * As a simple heuristic, we therefore select the row that makes the most
2122 * of the other rows redundant.
2124 * Perhaps it would also be useful to look at the number of constraints
2125 * that conflict with any given constraint.
2127 static int best_split(struct isl_tab
*tab
, struct isl_tab
*context_tab
)
2129 struct isl_tab_undo
*snap
;
2135 if (isl_tab_extend_cons(context_tab
, 2) < 0)
2138 snap
= isl_tab_snap(context_tab
);
2140 for (split
= tab
->n_redundant
; split
< tab
->n_row
; ++split
) {
2141 struct isl_tab_undo
*snap2
;
2142 struct isl_vec
*ineq
= NULL
;
2146 if (!isl_tab_var_from_row(tab
, split
)->is_nonneg
)
2148 if (tab
->row_sign
[split
] != isl_tab_row_any
)
2151 ineq
= get_row_parameter_ineq(tab
, split
);
2154 ok
= isl_tab_add_ineq(context_tab
, ineq
->el
) >= 0;
2159 snap2
= isl_tab_snap(context_tab
);
2161 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
2162 struct isl_tab_var
*var
;
2166 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
2168 if (tab
->row_sign
[row
] != isl_tab_row_any
)
2171 ineq
= get_row_parameter_ineq(tab
, row
);
2174 ok
= isl_tab_add_ineq(context_tab
, ineq
->el
) >= 0;
2178 var
= &context_tab
->con
[context_tab
->n_con
- 1];
2179 if (!context_tab
->empty
&&
2180 !isl_tab_min_at_most_neg_one(context_tab
, var
))
2182 if (isl_tab_rollback(context_tab
, snap2
) < 0)
2185 if (best
== -1 || r
> best_r
) {
2189 if (isl_tab_rollback(context_tab
, snap
) < 0)
2196 static struct isl_basic_set
*context_lex_peek_basic_set(
2197 struct isl_context
*context
)
2199 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2202 return isl_tab_peek_bset(clex
->tab
);
2205 static struct isl_tab
*context_lex_peek_tab(struct isl_context
*context
)
2207 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2211 static void context_lex_add_eq(struct isl_context
*context
, isl_int
*eq
,
2212 int check
, int update
)
2214 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2215 if (isl_tab_extend_cons(clex
->tab
, 2) < 0)
2217 if (add_lexmin_eq(clex
->tab
, eq
) < 0)
2220 int v
= tab_has_valid_sample(clex
->tab
, eq
, 1);
2224 clex
->tab
= check_integer_feasible(clex
->tab
);
2227 clex
->tab
= check_samples(clex
->tab
, eq
, 1);
2230 isl_tab_free(clex
->tab
);
2234 static void context_lex_add_ineq(struct isl_context
*context
, isl_int
*ineq
,
2235 int check
, int update
)
2237 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2238 if (isl_tab_extend_cons(clex
->tab
, 1) < 0)
2240 clex
->tab
= add_lexmin_ineq(clex
->tab
, ineq
);
2242 int v
= tab_has_valid_sample(clex
->tab
, ineq
, 0);
2246 clex
->tab
= check_integer_feasible(clex
->tab
);
2249 clex
->tab
= check_samples(clex
->tab
, ineq
, 0);
2252 isl_tab_free(clex
->tab
);
2256 static int context_lex_add_ineq_wrap(void *user
, isl_int
*ineq
)
2258 struct isl_context
*context
= (struct isl_context
*)user
;
2259 context_lex_add_ineq(context
, ineq
, 0, 0);
2260 return context
->op
->is_ok(context
) ? 0 : -1;
2263 /* Check which signs can be obtained by "ineq" on all the currently
2264 * active sample values. See row_sign for more information.
2266 static enum isl_tab_row_sign
tab_ineq_sign(struct isl_tab
*tab
, isl_int
*ineq
,
2272 enum isl_tab_row_sign res
= isl_tab_row_unknown
;
2274 isl_assert(tab
->mat
->ctx
, tab
->samples
, return isl_tab_row_unknown
);
2275 isl_assert(tab
->mat
->ctx
, tab
->samples
->n_col
== 1 + tab
->n_var
,
2276 return isl_tab_row_unknown
);
2279 for (i
= tab
->n_outside
; i
< tab
->n_sample
; ++i
) {
2280 isl_seq_inner_product(tab
->samples
->row
[i
], ineq
,
2281 1 + tab
->n_var
, &tmp
);
2282 sgn
= isl_int_sgn(tmp
);
2283 if (sgn
> 0 || (sgn
== 0 && strict
)) {
2284 if (res
== isl_tab_row_unknown
)
2285 res
= isl_tab_row_pos
;
2286 if (res
== isl_tab_row_neg
)
2287 res
= isl_tab_row_any
;
2290 if (res
== isl_tab_row_unknown
)
2291 res
= isl_tab_row_neg
;
2292 if (res
== isl_tab_row_pos
)
2293 res
= isl_tab_row_any
;
2295 if (res
== isl_tab_row_any
)
2303 static enum isl_tab_row_sign
context_lex_ineq_sign(struct isl_context
*context
,
2304 isl_int
*ineq
, int strict
)
2306 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2307 return tab_ineq_sign(clex
->tab
, ineq
, strict
);
2310 /* Check whether "ineq" can be added to the tableau without rendering
2313 static int context_lex_test_ineq(struct isl_context
*context
, isl_int
*ineq
)
2315 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2316 struct isl_tab_undo
*snap
;
2322 if (isl_tab_extend_cons(clex
->tab
, 1) < 0)
2325 snap
= isl_tab_snap(clex
->tab
);
2326 if (isl_tab_push_basis(clex
->tab
) < 0)
2328 clex
->tab
= add_lexmin_ineq(clex
->tab
, ineq
);
2329 clex
->tab
= check_integer_feasible(clex
->tab
);
2332 feasible
= !clex
->tab
->empty
;
2333 if (isl_tab_rollback(clex
->tab
, snap
) < 0)
2339 static int context_lex_get_div(struct isl_context
*context
, struct isl_tab
*tab
,
2340 struct isl_vec
*div
)
2342 return get_div(tab
, context
, div
);
2345 /* Add a div specified by "div" to the context tableau and return
2346 * 1 if the div is obviously non-negative.
2347 * context_tab_add_div will always return 1, because all variables
2348 * in a isl_context_lex tableau are non-negative.
2349 * However, if we are using a big parameter in the context, then this only
2350 * reflects the non-negativity of the variable used to _encode_ the
2351 * div, i.e., div' = M + div, so we can't draw any conclusions.
2353 static int context_lex_add_div(struct isl_context
*context
, struct isl_vec
*div
)
2355 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2357 nonneg
= context_tab_add_div(clex
->tab
, div
,
2358 context_lex_add_ineq_wrap
, context
);
2366 static int context_lex_detect_equalities(struct isl_context
*context
,
2367 struct isl_tab
*tab
)
2372 static int context_lex_best_split(struct isl_context
*context
,
2373 struct isl_tab
*tab
)
2375 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2376 struct isl_tab_undo
*snap
;
2379 snap
= isl_tab_snap(clex
->tab
);
2380 if (isl_tab_push_basis(clex
->tab
) < 0)
2382 r
= best_split(tab
, clex
->tab
);
2384 if (r
>= 0 && isl_tab_rollback(clex
->tab
, snap
) < 0)
2390 static int context_lex_is_empty(struct isl_context
*context
)
2392 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2395 return clex
->tab
->empty
;
2398 static void *context_lex_save(struct isl_context
*context
)
2400 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2401 struct isl_tab_undo
*snap
;
2403 snap
= isl_tab_snap(clex
->tab
);
2404 if (isl_tab_push_basis(clex
->tab
) < 0)
2406 if (isl_tab_save_samples(clex
->tab
) < 0)
2412 static void context_lex_restore(struct isl_context
*context
, void *save
)
2414 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2415 if (isl_tab_rollback(clex
->tab
, (struct isl_tab_undo
*)save
) < 0) {
2416 isl_tab_free(clex
->tab
);
2421 static int context_lex_is_ok(struct isl_context
*context
)
2423 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2427 /* For each variable in the context tableau, check if the variable can
2428 * only attain non-negative values. If so, mark the parameter as non-negative
2429 * in the main tableau. This allows for a more direct identification of some
2430 * cases of violated constraints.
2432 static struct isl_tab
*tab_detect_nonnegative_parameters(struct isl_tab
*tab
,
2433 struct isl_tab
*context_tab
)
2436 struct isl_tab_undo
*snap
;
2437 struct isl_vec
*ineq
= NULL
;
2438 struct isl_tab_var
*var
;
2441 if (context_tab
->n_var
== 0)
2444 ineq
= isl_vec_alloc(tab
->mat
->ctx
, 1 + context_tab
->n_var
);
2448 if (isl_tab_extend_cons(context_tab
, 1) < 0)
2451 snap
= isl_tab_snap(context_tab
);
2454 isl_seq_clr(ineq
->el
, ineq
->size
);
2455 for (i
= 0; i
< context_tab
->n_var
; ++i
) {
2456 isl_int_set_si(ineq
->el
[1 + i
], 1);
2457 if (isl_tab_add_ineq(context_tab
, ineq
->el
) < 0)
2459 var
= &context_tab
->con
[context_tab
->n_con
- 1];
2460 if (!context_tab
->empty
&&
2461 !isl_tab_min_at_most_neg_one(context_tab
, var
)) {
2463 if (i
>= tab
->n_param
)
2464 j
= i
- tab
->n_param
+ tab
->n_var
- tab
->n_div
;
2465 tab
->var
[j
].is_nonneg
= 1;
2468 isl_int_set_si(ineq
->el
[1 + i
], 0);
2469 if (isl_tab_rollback(context_tab
, snap
) < 0)
2473 if (context_tab
->M
&& n
== context_tab
->n_var
) {
2474 context_tab
->mat
= isl_mat_drop_cols(context_tab
->mat
, 2, 1);
2486 static struct isl_tab
*context_lex_detect_nonnegative_parameters(
2487 struct isl_context
*context
, struct isl_tab
*tab
)
2489 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2490 struct isl_tab_undo
*snap
;
2495 snap
= isl_tab_snap(clex
->tab
);
2496 if (isl_tab_push_basis(clex
->tab
) < 0)
2499 tab
= tab_detect_nonnegative_parameters(tab
, clex
->tab
);
2501 if (isl_tab_rollback(clex
->tab
, snap
) < 0)
2510 static void context_lex_invalidate(struct isl_context
*context
)
2512 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2513 isl_tab_free(clex
->tab
);
2517 static void context_lex_free(struct isl_context
*context
)
2519 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2520 isl_tab_free(clex
->tab
);
2524 struct isl_context_op isl_context_lex_op
= {
2525 context_lex_detect_nonnegative_parameters
,
2526 context_lex_peek_basic_set
,
2527 context_lex_peek_tab
,
2529 context_lex_add_ineq
,
2530 context_lex_ineq_sign
,
2531 context_lex_test_ineq
,
2532 context_lex_get_div
,
2533 context_lex_add_div
,
2534 context_lex_detect_equalities
,
2535 context_lex_best_split
,
2536 context_lex_is_empty
,
2539 context_lex_restore
,
2540 context_lex_invalidate
,
2544 static struct isl_tab
*context_tab_for_lexmin(struct isl_basic_set
*bset
)
2546 struct isl_tab
*tab
;
2550 tab
= tab_for_lexmin((struct isl_basic_map
*)bset
, NULL
, 1, 0);
2553 if (isl_tab_track_bset(tab
, bset
) < 0)
2555 tab
= isl_tab_init_samples(tab
);
2558 isl_basic_set_free(bset
);
2562 static struct isl_context
*isl_context_lex_alloc(struct isl_basic_set
*dom
)
2564 struct isl_context_lex
*clex
;
2569 clex
= isl_alloc_type(dom
->ctx
, struct isl_context_lex
);
2573 clex
->context
.op
= &isl_context_lex_op
;
2575 clex
->tab
= context_tab_for_lexmin(isl_basic_set_copy(dom
));
2576 if (restore_lexmin(clex
->tab
) < 0)
2578 clex
->tab
= check_integer_feasible(clex
->tab
);
2582 return &clex
->context
;
2584 clex
->context
.op
->free(&clex
->context
);
2588 struct isl_context_gbr
{
2589 struct isl_context context
;
2590 struct isl_tab
*tab
;
2591 struct isl_tab
*shifted
;
2592 struct isl_tab
*cone
;
2595 static struct isl_tab
*context_gbr_detect_nonnegative_parameters(
2596 struct isl_context
*context
, struct isl_tab
*tab
)
2598 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2601 return tab_detect_nonnegative_parameters(tab
, cgbr
->tab
);
2604 static struct isl_basic_set
*context_gbr_peek_basic_set(
2605 struct isl_context
*context
)
2607 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2610 return isl_tab_peek_bset(cgbr
->tab
);
2613 static struct isl_tab
*context_gbr_peek_tab(struct isl_context
*context
)
2615 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2619 /* Initialize the "shifted" tableau of the context, which
2620 * contains the constraints of the original tableau shifted
2621 * by the sum of all negative coefficients. This ensures
2622 * that any rational point in the shifted tableau can
2623 * be rounded up to yield an integer point in the original tableau.
2625 static void gbr_init_shifted(struct isl_context_gbr
*cgbr
)
2628 struct isl_vec
*cst
;
2629 struct isl_basic_set
*bset
= isl_tab_peek_bset(cgbr
->tab
);
2630 unsigned dim
= isl_basic_set_total_dim(bset
);
2632 cst
= isl_vec_alloc(cgbr
->tab
->mat
->ctx
, bset
->n_ineq
);
2636 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
2637 isl_int_set(cst
->el
[i
], bset
->ineq
[i
][0]);
2638 for (j
= 0; j
< dim
; ++j
) {
2639 if (!isl_int_is_neg(bset
->ineq
[i
][1 + j
]))
2641 isl_int_add(bset
->ineq
[i
][0], bset
->ineq
[i
][0],
2642 bset
->ineq
[i
][1 + j
]);
2646 cgbr
->shifted
= isl_tab_from_basic_set(bset
, 0);
2648 for (i
= 0; i
< bset
->n_ineq
; ++i
)
2649 isl_int_set(bset
->ineq
[i
][0], cst
->el
[i
]);
2654 /* Check if the shifted tableau is non-empty, and if so
2655 * use the sample point to construct an integer point
2656 * of the context tableau.
2658 static struct isl_vec
*gbr_get_shifted_sample(struct isl_context_gbr
*cgbr
)
2660 struct isl_vec
*sample
;
2663 gbr_init_shifted(cgbr
);
2666 if (cgbr
->shifted
->empty
)
2667 return isl_vec_alloc(cgbr
->tab
->mat
->ctx
, 0);
2669 sample
= isl_tab_get_sample_value(cgbr
->shifted
);
2670 sample
= isl_vec_ceil(sample
);
2675 static struct isl_basic_set
*drop_constant_terms(struct isl_basic_set
*bset
)
2682 for (i
= 0; i
< bset
->n_eq
; ++i
)
2683 isl_int_set_si(bset
->eq
[i
][0], 0);
2685 for (i
= 0; i
< bset
->n_ineq
; ++i
)
2686 isl_int_set_si(bset
->ineq
[i
][0], 0);
2691 static int use_shifted(struct isl_context_gbr
*cgbr
)
2693 return cgbr
->tab
->bmap
->n_eq
== 0 && cgbr
->tab
->bmap
->n_div
== 0;
2696 static struct isl_vec
*gbr_get_sample(struct isl_context_gbr
*cgbr
)
2698 struct isl_basic_set
*bset
;
2699 struct isl_basic_set
*cone
;
2701 if (isl_tab_sample_is_integer(cgbr
->tab
))
2702 return isl_tab_get_sample_value(cgbr
->tab
);
2704 if (use_shifted(cgbr
)) {
2705 struct isl_vec
*sample
;
2707 sample
= gbr_get_shifted_sample(cgbr
);
2708 if (!sample
|| sample
->size
> 0)
2711 isl_vec_free(sample
);
2715 bset
= isl_tab_peek_bset(cgbr
->tab
);
2716 cgbr
->cone
= isl_tab_from_recession_cone(bset
, 0);
2719 if (isl_tab_track_bset(cgbr
->cone
,
2720 isl_basic_set_copy(bset
)) < 0)
2723 if (isl_tab_detect_implicit_equalities(cgbr
->cone
) < 0)
2726 if (cgbr
->cone
->n_dead
== cgbr
->cone
->n_col
) {
2727 struct isl_vec
*sample
;
2728 struct isl_tab_undo
*snap
;
2730 if (cgbr
->tab
->basis
) {
2731 if (cgbr
->tab
->basis
->n_col
!= 1 + cgbr
->tab
->n_var
) {
2732 isl_mat_free(cgbr
->tab
->basis
);
2733 cgbr
->tab
->basis
= NULL
;
2735 cgbr
->tab
->n_zero
= 0;
2736 cgbr
->tab
->n_unbounded
= 0;
2739 snap
= isl_tab_snap(cgbr
->tab
);
2741 sample
= isl_tab_sample(cgbr
->tab
);
2743 if (isl_tab_rollback(cgbr
->tab
, snap
) < 0) {
2744 isl_vec_free(sample
);
2751 cone
= isl_basic_set_dup(isl_tab_peek_bset(cgbr
->cone
));
2752 cone
= drop_constant_terms(cone
);
2753 cone
= isl_basic_set_update_from_tab(cone
, cgbr
->cone
);
2754 cone
= isl_basic_set_underlying_set(cone
);
2755 cone
= isl_basic_set_gauss(cone
, NULL
);
2757 bset
= isl_basic_set_dup(isl_tab_peek_bset(cgbr
->tab
));
2758 bset
= isl_basic_set_update_from_tab(bset
, cgbr
->tab
);
2759 bset
= isl_basic_set_underlying_set(bset
);
2760 bset
= isl_basic_set_gauss(bset
, NULL
);
2762 return isl_basic_set_sample_with_cone(bset
, cone
);
2765 static void check_gbr_integer_feasible(struct isl_context_gbr
*cgbr
)
2767 struct isl_vec
*sample
;
2772 if (cgbr
->tab
->empty
)
2775 sample
= gbr_get_sample(cgbr
);
2779 if (sample
->size
== 0) {
2780 isl_vec_free(sample
);
2781 if (isl_tab_mark_empty(cgbr
->tab
) < 0)
2786 cgbr
->tab
= isl_tab_add_sample(cgbr
->tab
, sample
);
2790 isl_tab_free(cgbr
->tab
);
2794 static struct isl_tab
*add_gbr_eq(struct isl_tab
*tab
, isl_int
*eq
)
2799 if (isl_tab_extend_cons(tab
, 2) < 0)
2802 if (isl_tab_add_eq(tab
, eq
) < 0)
2811 static void context_gbr_add_eq(struct isl_context
*context
, isl_int
*eq
,
2812 int check
, int update
)
2814 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2816 cgbr
->tab
= add_gbr_eq(cgbr
->tab
, eq
);
2818 if (cgbr
->cone
&& cgbr
->cone
->n_col
!= cgbr
->cone
->n_dead
) {
2819 if (isl_tab_extend_cons(cgbr
->cone
, 2) < 0)
2821 if (isl_tab_add_eq(cgbr
->cone
, eq
) < 0)
2826 int v
= tab_has_valid_sample(cgbr
->tab
, eq
, 1);
2830 check_gbr_integer_feasible(cgbr
);
2833 cgbr
->tab
= check_samples(cgbr
->tab
, eq
, 1);
2836 isl_tab_free(cgbr
->tab
);
2840 static void add_gbr_ineq(struct isl_context_gbr
*cgbr
, isl_int
*ineq
)
2845 if (isl_tab_extend_cons(cgbr
->tab
, 1) < 0)
2848 if (isl_tab_add_ineq(cgbr
->tab
, ineq
) < 0)
2851 if (cgbr
->shifted
&& !cgbr
->shifted
->empty
&& use_shifted(cgbr
)) {
2854 dim
= isl_basic_map_total_dim(cgbr
->tab
->bmap
);
2856 if (isl_tab_extend_cons(cgbr
->shifted
, 1) < 0)
2859 for (i
= 0; i
< dim
; ++i
) {
2860 if (!isl_int_is_neg(ineq
[1 + i
]))
2862 isl_int_add(ineq
[0], ineq
[0], ineq
[1 + i
]);
2865 if (isl_tab_add_ineq(cgbr
->shifted
, ineq
) < 0)
2868 for (i
= 0; i
< dim
; ++i
) {
2869 if (!isl_int_is_neg(ineq
[1 + i
]))
2871 isl_int_sub(ineq
[0], ineq
[0], ineq
[1 + i
]);
2875 if (cgbr
->cone
&& cgbr
->cone
->n_col
!= cgbr
->cone
->n_dead
) {
2876 if (isl_tab_extend_cons(cgbr
->cone
, 1) < 0)
2878 if (isl_tab_add_ineq(cgbr
->cone
, ineq
) < 0)
2884 isl_tab_free(cgbr
->tab
);
2888 static void context_gbr_add_ineq(struct isl_context
*context
, isl_int
*ineq
,
2889 int check
, int update
)
2891 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2893 add_gbr_ineq(cgbr
, ineq
);
2898 int v
= tab_has_valid_sample(cgbr
->tab
, ineq
, 0);
2902 check_gbr_integer_feasible(cgbr
);
2905 cgbr
->tab
= check_samples(cgbr
->tab
, ineq
, 0);
2908 isl_tab_free(cgbr
->tab
);
2912 static int context_gbr_add_ineq_wrap(void *user
, isl_int
*ineq
)
2914 struct isl_context
*context
= (struct isl_context
*)user
;
2915 context_gbr_add_ineq(context
, ineq
, 0, 0);
2916 return context
->op
->is_ok(context
) ? 0 : -1;
2919 static enum isl_tab_row_sign
context_gbr_ineq_sign(struct isl_context
*context
,
2920 isl_int
*ineq
, int strict
)
2922 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2923 return tab_ineq_sign(cgbr
->tab
, ineq
, strict
);
2926 /* Check whether "ineq" can be added to the tableau without rendering
2929 static int context_gbr_test_ineq(struct isl_context
*context
, isl_int
*ineq
)
2931 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2932 struct isl_tab_undo
*snap
;
2933 struct isl_tab_undo
*shifted_snap
= NULL
;
2934 struct isl_tab_undo
*cone_snap
= NULL
;
2940 if (isl_tab_extend_cons(cgbr
->tab
, 1) < 0)
2943 snap
= isl_tab_snap(cgbr
->tab
);
2945 shifted_snap
= isl_tab_snap(cgbr
->shifted
);
2947 cone_snap
= isl_tab_snap(cgbr
->cone
);
2948 add_gbr_ineq(cgbr
, ineq
);
2949 check_gbr_integer_feasible(cgbr
);
2952 feasible
= !cgbr
->tab
->empty
;
2953 if (isl_tab_rollback(cgbr
->tab
, snap
) < 0)
2956 if (isl_tab_rollback(cgbr
->shifted
, shifted_snap
))
2958 } else if (cgbr
->shifted
) {
2959 isl_tab_free(cgbr
->shifted
);
2960 cgbr
->shifted
= NULL
;
2963 if (isl_tab_rollback(cgbr
->cone
, cone_snap
))
2965 } else if (cgbr
->cone
) {
2966 isl_tab_free(cgbr
->cone
);
2973 /* Return the column of the last of the variables associated to
2974 * a column that has a non-zero coefficient.
2975 * This function is called in a context where only coefficients
2976 * of parameters or divs can be non-zero.
2978 static int last_non_zero_var_col(struct isl_tab
*tab
, isl_int
*p
)
2983 if (tab
->n_var
== 0)
2986 for (i
= tab
->n_var
- 1; i
>= 0; --i
) {
2987 if (i
>= tab
->n_param
&& i
< tab
->n_var
- tab
->n_div
)
2989 if (tab
->var
[i
].is_row
)
2991 col
= tab
->var
[i
].index
;
2992 if (!isl_int_is_zero(p
[col
]))
2999 /* Look through all the recently added equalities in the context
3000 * to see if we can propagate any of them to the main tableau.
3002 * The newly added equalities in the context are encoded as pairs
3003 * of inequalities starting at inequality "first".
3005 * We tentatively add each of these equalities to the main tableau
3006 * and if this happens to result in a row with a final coefficient
3007 * that is one or negative one, we use it to kill a column
3008 * in the main tableau. Otherwise, we discard the tentatively
3011 static void propagate_equalities(struct isl_context_gbr
*cgbr
,
3012 struct isl_tab
*tab
, unsigned first
)
3015 struct isl_vec
*eq
= NULL
;
3017 eq
= isl_vec_alloc(tab
->mat
->ctx
, 1 + tab
->n_var
);
3021 if (isl_tab_extend_cons(tab
, (cgbr
->tab
->bmap
->n_ineq
- first
)/2) < 0)
3024 isl_seq_clr(eq
->el
+ 1 + tab
->n_param
,
3025 tab
->n_var
- tab
->n_param
- tab
->n_div
);
3026 for (i
= first
; i
< cgbr
->tab
->bmap
->n_ineq
; i
+= 2) {
3029 struct isl_tab_undo
*snap
;
3030 snap
= isl_tab_snap(tab
);
3032 isl_seq_cpy(eq
->el
, cgbr
->tab
->bmap
->ineq
[i
], 1 + tab
->n_param
);
3033 isl_seq_cpy(eq
->el
+ 1 + tab
->n_var
- tab
->n_div
,
3034 cgbr
->tab
->bmap
->ineq
[i
] + 1 + tab
->n_param
,
3037 r
= isl_tab_add_row(tab
, eq
->el
);
3040 r
= tab
->con
[r
].index
;
3041 j
= last_non_zero_var_col(tab
, tab
->mat
->row
[r
] + 2 + tab
->M
);
3042 if (j
< 0 || j
< tab
->n_dead
||
3043 !isl_int_is_one(tab
->mat
->row
[r
][0]) ||
3044 (!isl_int_is_one(tab
->mat
->row
[r
][2 + tab
->M
+ j
]) &&
3045 !isl_int_is_negone(tab
->mat
->row
[r
][2 + tab
->M
+ j
]))) {
3046 if (isl_tab_rollback(tab
, snap
) < 0)
3050 if (isl_tab_pivot(tab
, r
, j
) < 0)
3052 if (isl_tab_kill_col(tab
, j
) < 0)
3055 if (restore_lexmin(tab
) < 0)
3064 isl_tab_free(cgbr
->tab
);
3068 static int context_gbr_detect_equalities(struct isl_context
*context
,
3069 struct isl_tab
*tab
)
3071 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3072 struct isl_ctx
*ctx
;
3075 ctx
= cgbr
->tab
->mat
->ctx
;
3078 struct isl_basic_set
*bset
= isl_tab_peek_bset(cgbr
->tab
);
3079 cgbr
->cone
= isl_tab_from_recession_cone(bset
, 0);
3082 if (isl_tab_track_bset(cgbr
->cone
,
3083 isl_basic_set_copy(bset
)) < 0)
3086 if (isl_tab_detect_implicit_equalities(cgbr
->cone
) < 0)
3089 n_ineq
= cgbr
->tab
->bmap
->n_ineq
;
3090 cgbr
->tab
= isl_tab_detect_equalities(cgbr
->tab
, cgbr
->cone
);
3091 if (cgbr
->tab
&& cgbr
->tab
->bmap
->n_ineq
> n_ineq
)
3092 propagate_equalities(cgbr
, tab
, n_ineq
);
3096 isl_tab_free(cgbr
->tab
);
3101 static int context_gbr_get_div(struct isl_context
*context
, struct isl_tab
*tab
,
3102 struct isl_vec
*div
)
3104 return get_div(tab
, context
, div
);
3107 static int context_gbr_add_div(struct isl_context
*context
, struct isl_vec
*div
)
3109 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3113 if (isl_tab_extend_cons(cgbr
->cone
, 3) < 0)
3115 if (isl_tab_extend_vars(cgbr
->cone
, 1) < 0)
3117 if (isl_tab_allocate_var(cgbr
->cone
) <0)
3120 cgbr
->cone
->bmap
= isl_basic_map_extend_space(cgbr
->cone
->bmap
,
3121 isl_basic_map_get_space(cgbr
->cone
->bmap
), 1, 0, 2);
3122 k
= isl_basic_map_alloc_div(cgbr
->cone
->bmap
);
3125 isl_seq_cpy(cgbr
->cone
->bmap
->div
[k
], div
->el
, div
->size
);
3126 if (isl_tab_push(cgbr
->cone
, isl_tab_undo_bmap_div
) < 0)
3129 return context_tab_add_div(cgbr
->tab
, div
,
3130 context_gbr_add_ineq_wrap
, context
);
3133 static int context_gbr_best_split(struct isl_context
*context
,
3134 struct isl_tab
*tab
)
3136 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3137 struct isl_tab_undo
*snap
;
3140 snap
= isl_tab_snap(cgbr
->tab
);
3141 r
= best_split(tab
, cgbr
->tab
);
3143 if (r
>= 0 && isl_tab_rollback(cgbr
->tab
, snap
) < 0)
3149 static int context_gbr_is_empty(struct isl_context
*context
)
3151 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3154 return cgbr
->tab
->empty
;
3157 struct isl_gbr_tab_undo
{
3158 struct isl_tab_undo
*tab_snap
;
3159 struct isl_tab_undo
*shifted_snap
;
3160 struct isl_tab_undo
*cone_snap
;
3163 static void *context_gbr_save(struct isl_context
*context
)
3165 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3166 struct isl_gbr_tab_undo
*snap
;
3168 snap
= isl_alloc_type(cgbr
->tab
->mat
->ctx
, struct isl_gbr_tab_undo
);
3172 snap
->tab_snap
= isl_tab_snap(cgbr
->tab
);
3173 if (isl_tab_save_samples(cgbr
->tab
) < 0)
3177 snap
->shifted_snap
= isl_tab_snap(cgbr
->shifted
);
3179 snap
->shifted_snap
= NULL
;
3182 snap
->cone_snap
= isl_tab_snap(cgbr
->cone
);
3184 snap
->cone_snap
= NULL
;
3192 static void context_gbr_restore(struct isl_context
*context
, void *save
)
3194 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3195 struct isl_gbr_tab_undo
*snap
= (struct isl_gbr_tab_undo
*)save
;
3198 if (isl_tab_rollback(cgbr
->tab
, snap
->tab_snap
) < 0) {
3199 isl_tab_free(cgbr
->tab
);
3203 if (snap
->shifted_snap
) {
3204 if (isl_tab_rollback(cgbr
->shifted
, snap
->shifted_snap
) < 0)
3206 } else if (cgbr
->shifted
) {
3207 isl_tab_free(cgbr
->shifted
);
3208 cgbr
->shifted
= NULL
;
3211 if (snap
->cone_snap
) {
3212 if (isl_tab_rollback(cgbr
->cone
, snap
->cone_snap
) < 0)
3214 } else if (cgbr
->cone
) {
3215 isl_tab_free(cgbr
->cone
);
3224 isl_tab_free(cgbr
->tab
);
3228 static int context_gbr_is_ok(struct isl_context
*context
)
3230 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3234 static void context_gbr_invalidate(struct isl_context
*context
)
3236 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3237 isl_tab_free(cgbr
->tab
);
3241 static void context_gbr_free(struct isl_context
*context
)
3243 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3244 isl_tab_free(cgbr
->tab
);
3245 isl_tab_free(cgbr
->shifted
);
3246 isl_tab_free(cgbr
->cone
);
3250 struct isl_context_op isl_context_gbr_op
= {
3251 context_gbr_detect_nonnegative_parameters
,
3252 context_gbr_peek_basic_set
,
3253 context_gbr_peek_tab
,
3255 context_gbr_add_ineq
,
3256 context_gbr_ineq_sign
,
3257 context_gbr_test_ineq
,
3258 context_gbr_get_div
,
3259 context_gbr_add_div
,
3260 context_gbr_detect_equalities
,
3261 context_gbr_best_split
,
3262 context_gbr_is_empty
,
3265 context_gbr_restore
,
3266 context_gbr_invalidate
,
3270 static struct isl_context
*isl_context_gbr_alloc(struct isl_basic_set
*dom
)
3272 struct isl_context_gbr
*cgbr
;
3277 cgbr
= isl_calloc_type(dom
->ctx
, struct isl_context_gbr
);
3281 cgbr
->context
.op
= &isl_context_gbr_op
;
3283 cgbr
->shifted
= NULL
;
3285 cgbr
->tab
= isl_tab_from_basic_set(dom
, 1);
3286 cgbr
->tab
= isl_tab_init_samples(cgbr
->tab
);
3289 check_gbr_integer_feasible(cgbr
);
3291 return &cgbr
->context
;
3293 cgbr
->context
.op
->free(&cgbr
->context
);
3297 static struct isl_context
*isl_context_alloc(struct isl_basic_set
*dom
)
3302 if (dom
->ctx
->opt
->context
== ISL_CONTEXT_LEXMIN
)
3303 return isl_context_lex_alloc(dom
);
3305 return isl_context_gbr_alloc(dom
);
3308 /* Construct an isl_sol_map structure for accumulating the solution.
3309 * If track_empty is set, then we also keep track of the parts
3310 * of the context where there is no solution.
3311 * If max is set, then we are solving a maximization, rather than
3312 * a minimization problem, which means that the variables in the
3313 * tableau have value "M - x" rather than "M + x".
3315 static struct isl_sol
*sol_map_init(struct isl_basic_map
*bmap
,
3316 struct isl_basic_set
*dom
, int track_empty
, int max
)
3318 struct isl_sol_map
*sol_map
= NULL
;
3323 sol_map
= isl_calloc_type(bmap
->ctx
, struct isl_sol_map
);
3327 sol_map
->sol
.rational
= ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
3328 sol_map
->sol
.dec_level
.callback
.run
= &sol_dec_level_wrap
;
3329 sol_map
->sol
.dec_level
.sol
= &sol_map
->sol
;
3330 sol_map
->sol
.max
= max
;
3331 sol_map
->sol
.n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
3332 sol_map
->sol
.add
= &sol_map_add_wrap
;
3333 sol_map
->sol
.add_empty
= track_empty
? &sol_map_add_empty_wrap
: NULL
;
3334 sol_map
->sol
.free
= &sol_map_free_wrap
;
3335 sol_map
->map
= isl_map_alloc_space(isl_basic_map_get_space(bmap
), 1,
3340 sol_map
->sol
.context
= isl_context_alloc(dom
);
3341 if (!sol_map
->sol
.context
)
3345 sol_map
->empty
= isl_set_alloc_space(isl_basic_set_get_space(dom
),
3346 1, ISL_SET_DISJOINT
);
3347 if (!sol_map
->empty
)
3351 isl_basic_set_free(dom
);
3352 return &sol_map
->sol
;
3354 isl_basic_set_free(dom
);
3355 sol_map_free(sol_map
);
3359 /* Check whether all coefficients of (non-parameter) variables
3360 * are non-positive, meaning that no pivots can be performed on the row.
3362 static int is_critical(struct isl_tab
*tab
, int row
)
3365 unsigned off
= 2 + tab
->M
;
3367 for (j
= tab
->n_dead
; j
< tab
->n_col
; ++j
) {
3368 if (tab
->col_var
[j
] >= 0 &&
3369 (tab
->col_var
[j
] < tab
->n_param
||
3370 tab
->col_var
[j
] >= tab
->n_var
- tab
->n_div
))
3373 if (isl_int_is_pos(tab
->mat
->row
[row
][off
+ j
]))
3380 /* Check whether the inequality represented by vec is strict over the integers,
3381 * i.e., there are no integer values satisfying the constraint with
3382 * equality. This happens if the gcd of the coefficients is not a divisor
3383 * of the constant term. If so, scale the constraint down by the gcd
3384 * of the coefficients.
3386 static int is_strict(struct isl_vec
*vec
)
3392 isl_seq_gcd(vec
->el
+ 1, vec
->size
- 1, &gcd
);
3393 if (!isl_int_is_one(gcd
)) {
3394 strict
= !isl_int_is_divisible_by(vec
->el
[0], gcd
);
3395 isl_int_fdiv_q(vec
->el
[0], vec
->el
[0], gcd
);
3396 isl_seq_scale_down(vec
->el
+ 1, vec
->el
+ 1, gcd
, vec
->size
-1);
3403 /* Determine the sign of the given row of the main tableau.
3404 * The result is one of
3405 * isl_tab_row_pos: always non-negative; no pivot needed
3406 * isl_tab_row_neg: always non-positive; pivot
3407 * isl_tab_row_any: can be both positive and negative; split
3409 * We first handle some simple cases
3410 * - the row sign may be known already
3411 * - the row may be obviously non-negative
3412 * - the parametric constant may be equal to that of another row
3413 * for which we know the sign. This sign will be either "pos" or
3414 * "any". If it had been "neg" then we would have pivoted before.
3416 * If none of these cases hold, we check the value of the row for each
3417 * of the currently active samples. Based on the signs of these values
3418 * we make an initial determination of the sign of the row.
3420 * all zero -> unk(nown)
3421 * all non-negative -> pos
3422 * all non-positive -> neg
3423 * both negative and positive -> all
3425 * If we end up with "all", we are done.
3426 * Otherwise, we perform a check for positive and/or negative
3427 * values as follows.
3429 * samples neg unk pos
3435 * There is no special sign for "zero", because we can usually treat zero
3436 * as either non-negative or non-positive, whatever works out best.
3437 * However, if the row is "critical", meaning that pivoting is impossible
3438 * then we don't want to limp zero with the non-positive case, because
3439 * then we we would lose the solution for those values of the parameters
3440 * where the value of the row is zero. Instead, we treat 0 as non-negative
3441 * ensuring a split if the row can attain both zero and negative values.
3442 * The same happens when the original constraint was one that could not
3443 * be satisfied with equality by any integer values of the parameters.
3444 * In this case, we normalize the constraint, but then a value of zero
3445 * for the normalized constraint is actually a positive value for the
3446 * original constraint, so again we need to treat zero as non-negative.
3447 * In both these cases, we have the following decision tree instead:
3449 * all non-negative -> pos
3450 * all negative -> neg
3451 * both negative and non-negative -> all
3459 static enum isl_tab_row_sign
row_sign(struct isl_tab
*tab
,
3460 struct isl_sol
*sol
, int row
)
3462 struct isl_vec
*ineq
= NULL
;
3463 enum isl_tab_row_sign res
= isl_tab_row_unknown
;
3468 if (tab
->row_sign
[row
] != isl_tab_row_unknown
)
3469 return tab
->row_sign
[row
];
3470 if (is_obviously_nonneg(tab
, row
))
3471 return isl_tab_row_pos
;
3472 for (row2
= tab
->n_redundant
; row2
< tab
->n_row
; ++row2
) {
3473 if (tab
->row_sign
[row2
] == isl_tab_row_unknown
)
3475 if (identical_parameter_line(tab
, row
, row2
))
3476 return tab
->row_sign
[row2
];
3479 critical
= is_critical(tab
, row
);
3481 ineq
= get_row_parameter_ineq(tab
, row
);
3485 strict
= is_strict(ineq
);
3487 res
= sol
->context
->op
->ineq_sign(sol
->context
, ineq
->el
,
3488 critical
|| strict
);
3490 if (res
== isl_tab_row_unknown
|| res
== isl_tab_row_pos
) {
3491 /* test for negative values */
3493 isl_seq_neg(ineq
->el
, ineq
->el
, ineq
->size
);
3494 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3496 feasible
= sol
->context
->op
->test_ineq(sol
->context
, ineq
->el
);
3500 res
= isl_tab_row_pos
;
3502 res
= (res
== isl_tab_row_unknown
) ? isl_tab_row_neg
3504 if (res
== isl_tab_row_neg
) {
3505 isl_seq_neg(ineq
->el
, ineq
->el
, ineq
->size
);
3506 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3510 if (res
== isl_tab_row_neg
) {
3511 /* test for positive values */
3513 if (!critical
&& !strict
)
3514 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3516 feasible
= sol
->context
->op
->test_ineq(sol
->context
, ineq
->el
);
3520 res
= isl_tab_row_any
;
3527 return isl_tab_row_unknown
;
3530 static void find_solutions(struct isl_sol
*sol
, struct isl_tab
*tab
);
3532 /* Find solutions for values of the parameters that satisfy the given
3535 * We currently take a snapshot of the context tableau that is reset
3536 * when we return from this function, while we make a copy of the main
3537 * tableau, leaving the original main tableau untouched.
3538 * These are fairly arbitrary choices. Making a copy also of the context
3539 * tableau would obviate the need to undo any changes made to it later,
3540 * while taking a snapshot of the main tableau could reduce memory usage.
3541 * If we were to switch to taking a snapshot of the main tableau,
3542 * we would have to keep in mind that we need to save the row signs
3543 * and that we need to do this before saving the current basis
3544 * such that the basis has been restore before we restore the row signs.
3546 static void find_in_pos(struct isl_sol
*sol
, struct isl_tab
*tab
, isl_int
*ineq
)
3552 saved
= sol
->context
->op
->save(sol
->context
);
3554 tab
= isl_tab_dup(tab
);
3558 sol
->context
->op
->add_ineq(sol
->context
, ineq
, 0, 1);
3560 find_solutions(sol
, tab
);
3563 sol
->context
->op
->restore(sol
->context
, saved
);
3569 /* Record the absence of solutions for those values of the parameters
3570 * that do not satisfy the given inequality with equality.
3572 static void no_sol_in_strict(struct isl_sol
*sol
,
3573 struct isl_tab
*tab
, struct isl_vec
*ineq
)
3578 if (!sol
->context
|| sol
->error
)
3580 saved
= sol
->context
->op
->save(sol
->context
);
3582 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3584 sol
->context
->op
->add_ineq(sol
->context
, ineq
->el
, 1, 0);
3593 isl_int_add_ui(ineq
->el
[0], ineq
->el
[0], 1);
3595 sol
->context
->op
->restore(sol
->context
, saved
);
3601 /* Compute the lexicographic minimum of the set represented by the main
3602 * tableau "tab" within the context "sol->context_tab".
3603 * On entry the sample value of the main tableau is lexicographically
3604 * less than or equal to this lexicographic minimum.
3605 * Pivots are performed until a feasible point is found, which is then
3606 * necessarily equal to the minimum, or until the tableau is found to
3607 * be infeasible. Some pivots may need to be performed for only some
3608 * feasible values of the context tableau. If so, the context tableau
3609 * is split into a part where the pivot is needed and a part where it is not.
3611 * Whenever we enter the main loop, the main tableau is such that no
3612 * "obvious" pivots need to be performed on it, where "obvious" means
3613 * that the given row can be seen to be negative without looking at
3614 * the context tableau. In particular, for non-parametric problems,
3615 * no pivots need to be performed on the main tableau.
3616 * The caller of find_solutions is responsible for making this property
3617 * hold prior to the first iteration of the loop, while restore_lexmin
3618 * is called before every other iteration.
3620 * Inside the main loop, we first examine the signs of the rows of
3621 * the main tableau within the context of the context tableau.
3622 * If we find a row that is always non-positive for all values of
3623 * the parameters satisfying the context tableau and negative for at
3624 * least one value of the parameters, we perform the appropriate pivot
3625 * and start over. An exception is the case where no pivot can be
3626 * performed on the row. In this case, we require that the sign of
3627 * the row is negative for all values of the parameters (rather than just
3628 * non-positive). This special case is handled inside row_sign, which
3629 * will say that the row can have any sign if it determines that it can
3630 * attain both negative and zero values.
3632 * If we can't find a row that always requires a pivot, but we can find
3633 * one or more rows that require a pivot for some values of the parameters
3634 * (i.e., the row can attain both positive and negative signs), then we split
3635 * the context tableau into two parts, one where we force the sign to be
3636 * non-negative and one where we force is to be negative.
3637 * The non-negative part is handled by a recursive call (through find_in_pos).
3638 * Upon returning from this call, we continue with the negative part and
3639 * perform the required pivot.
3641 * If no such rows can be found, all rows are non-negative and we have
3642 * found a (rational) feasible point. If we only wanted a rational point
3644 * Otherwise, we check if all values of the sample point of the tableau
3645 * are integral for the variables. If so, we have found the minimal
3646 * integral point and we are done.
3647 * If the sample point is not integral, then we need to make a distinction
3648 * based on whether the constant term is non-integral or the coefficients
3649 * of the parameters. Furthermore, in order to decide how to handle
3650 * the non-integrality, we also need to know whether the coefficients
3651 * of the other columns in the tableau are integral. This leads
3652 * to the following table. The first two rows do not correspond
3653 * to a non-integral sample point and are only mentioned for completeness.
3655 * constant parameters other
3658 * int int rat | -> no problem
3660 * rat int int -> fail
3662 * rat int rat -> cut
3665 * rat rat rat | -> parametric cut
3668 * rat rat int | -> split context
3670 * If the parametric constant is completely integral, then there is nothing
3671 * to be done. If the constant term is non-integral, but all the other
3672 * coefficient are integral, then there is nothing that can be done
3673 * and the tableau has no integral solution.
3674 * If, on the other hand, one or more of the other columns have rational
3675 * coefficients, but the parameter coefficients are all integral, then
3676 * we can perform a regular (non-parametric) cut.
3677 * Finally, if there is any parameter coefficient that is non-integral,
3678 * then we need to involve the context tableau. There are two cases here.
3679 * If at least one other column has a rational coefficient, then we
3680 * can perform a parametric cut in the main tableau by adding a new
3681 * integer division in the context tableau.
3682 * If all other columns have integral coefficients, then we need to
3683 * enforce that the rational combination of parameters (c + \sum a_i y_i)/m
3684 * is always integral. We do this by introducing an integer division
3685 * q = floor((c + \sum a_i y_i)/m) and stipulating that its argument should
3686 * always be integral in the context tableau, i.e., m q = c + \sum a_i y_i.
3687 * Since q is expressed in the tableau as
3688 * c + \sum a_i y_i - m q >= 0
3689 * -c - \sum a_i y_i + m q + m - 1 >= 0
3690 * it is sufficient to add the inequality
3691 * -c - \sum a_i y_i + m q >= 0
3692 * In the part of the context where this inequality does not hold, the
3693 * main tableau is marked as being empty.
3695 static void find_solutions(struct isl_sol
*sol
, struct isl_tab
*tab
)
3697 struct isl_context
*context
;
3700 if (!tab
|| sol
->error
)
3703 context
= sol
->context
;
3707 if (context
->op
->is_empty(context
))
3710 for (r
= 0; r
>= 0 && tab
&& !tab
->empty
; r
= restore_lexmin(tab
)) {
3713 enum isl_tab_row_sign sgn
;
3717 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
3718 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
3720 sgn
= row_sign(tab
, sol
, row
);
3723 tab
->row_sign
[row
] = sgn
;
3724 if (sgn
== isl_tab_row_any
)
3726 if (sgn
== isl_tab_row_any
&& split
== -1)
3728 if (sgn
== isl_tab_row_neg
)
3731 if (row
< tab
->n_row
)
3734 struct isl_vec
*ineq
;
3736 split
= context
->op
->best_split(context
, tab
);
3739 ineq
= get_row_parameter_ineq(tab
, split
);
3743 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
3744 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
3746 if (tab
->row_sign
[row
] == isl_tab_row_any
)
3747 tab
->row_sign
[row
] = isl_tab_row_unknown
;
3749 tab
->row_sign
[split
] = isl_tab_row_pos
;
3751 find_in_pos(sol
, tab
, ineq
->el
);
3752 tab
->row_sign
[split
] = isl_tab_row_neg
;
3754 isl_seq_neg(ineq
->el
, ineq
->el
, ineq
->size
);
3755 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3757 context
->op
->add_ineq(context
, ineq
->el
, 0, 1);
3765 row
= first_non_integer_row(tab
, &flags
);
3768 if (ISL_FL_ISSET(flags
, I_PAR
)) {
3769 if (ISL_FL_ISSET(flags
, I_VAR
)) {
3770 if (isl_tab_mark_empty(tab
) < 0)
3774 row
= add_cut(tab
, row
);
3775 } else if (ISL_FL_ISSET(flags
, I_VAR
)) {
3776 struct isl_vec
*div
;
3777 struct isl_vec
*ineq
;
3779 div
= get_row_split_div(tab
, row
);
3782 d
= context
->op
->get_div(context
, tab
, div
);
3786 ineq
= ineq_for_div(context
->op
->peek_basic_set(context
), d
);
3790 no_sol_in_strict(sol
, tab
, ineq
);
3791 isl_seq_neg(ineq
->el
, ineq
->el
, ineq
->size
);
3792 context
->op
->add_ineq(context
, ineq
->el
, 1, 1);
3794 if (sol
->error
|| !context
->op
->is_ok(context
))
3796 tab
= set_row_cst_to_div(tab
, row
, d
);
3797 if (context
->op
->is_empty(context
))
3800 row
= add_parametric_cut(tab
, row
, context
);
3815 /* Compute the lexicographic minimum of the set represented by the main
3816 * tableau "tab" within the context "sol->context_tab".
3818 * As a preprocessing step, we first transfer all the purely parametric
3819 * equalities from the main tableau to the context tableau, i.e.,
3820 * parameters that have been pivoted to a row.
3821 * These equalities are ignored by the main algorithm, because the
3822 * corresponding rows may not be marked as being non-negative.
3823 * In parts of the context where the added equality does not hold,
3824 * the main tableau is marked as being empty.
3826 static void find_solutions_main(struct isl_sol
*sol
, struct isl_tab
*tab
)
3835 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
3839 if (tab
->row_var
[row
] < 0)
3841 if (tab
->row_var
[row
] >= tab
->n_param
&&
3842 tab
->row_var
[row
] < tab
->n_var
- tab
->n_div
)
3844 if (tab
->row_var
[row
] < tab
->n_param
)
3845 p
= tab
->row_var
[row
];
3847 p
= tab
->row_var
[row
]
3848 + tab
->n_param
- (tab
->n_var
- tab
->n_div
);
3850 eq
= isl_vec_alloc(tab
->mat
->ctx
, 1+tab
->n_param
+tab
->n_div
);
3853 get_row_parameter_line(tab
, row
, eq
->el
);
3854 isl_int_neg(eq
->el
[1 + p
], tab
->mat
->row
[row
][0]);
3855 eq
= isl_vec_normalize(eq
);
3858 no_sol_in_strict(sol
, tab
, eq
);
3860 isl_seq_neg(eq
->el
, eq
->el
, eq
->size
);
3862 no_sol_in_strict(sol
, tab
, eq
);
3863 isl_seq_neg(eq
->el
, eq
->el
, eq
->size
);
3865 sol
->context
->op
->add_eq(sol
->context
, eq
->el
, 1, 1);
3869 if (isl_tab_mark_redundant(tab
, row
) < 0)
3872 if (sol
->context
->op
->is_empty(sol
->context
))
3875 row
= tab
->n_redundant
- 1;
3878 find_solutions(sol
, tab
);
3889 /* Check if integer division "div" of "dom" also occurs in "bmap".
3890 * If so, return its position within the divs.
3891 * If not, return -1.
3893 static int find_context_div(struct isl_basic_map
*bmap
,
3894 struct isl_basic_set
*dom
, unsigned div
)
3897 unsigned b_dim
= isl_space_dim(bmap
->dim
, isl_dim_all
);
3898 unsigned d_dim
= isl_space_dim(dom
->dim
, isl_dim_all
);
3900 if (isl_int_is_zero(dom
->div
[div
][0]))
3902 if (isl_seq_first_non_zero(dom
->div
[div
] + 2 + d_dim
, dom
->n_div
) != -1)
3905 for (i
= 0; i
< bmap
->n_div
; ++i
) {
3906 if (isl_int_is_zero(bmap
->div
[i
][0]))
3908 if (isl_seq_first_non_zero(bmap
->div
[i
] + 2 + d_dim
,
3909 (b_dim
- d_dim
) + bmap
->n_div
) != -1)
3911 if (isl_seq_eq(bmap
->div
[i
], dom
->div
[div
], 2 + d_dim
))
3917 /* The correspondence between the variables in the main tableau,
3918 * the context tableau, and the input map and domain is as follows.
3919 * The first n_param and the last n_div variables of the main tableau
3920 * form the variables of the context tableau.
3921 * In the basic map, these n_param variables correspond to the
3922 * parameters and the input dimensions. In the domain, they correspond
3923 * to the parameters and the set dimensions.
3924 * The n_div variables correspond to the integer divisions in the domain.
3925 * To ensure that everything lines up, we may need to copy some of the
3926 * integer divisions of the domain to the map. These have to be placed
3927 * in the same order as those in the context and they have to be placed
3928 * after any other integer divisions that the map may have.
3929 * This function performs the required reordering.
3931 static struct isl_basic_map
*align_context_divs(struct isl_basic_map
*bmap
,
3932 struct isl_basic_set
*dom
)
3938 for (i
= 0; i
< dom
->n_div
; ++i
)
3939 if (find_context_div(bmap
, dom
, i
) != -1)
3941 other
= bmap
->n_div
- common
;
3942 if (dom
->n_div
- common
> 0) {
3943 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
3944 dom
->n_div
- common
, 0, 0);
3948 for (i
= 0; i
< dom
->n_div
; ++i
) {
3949 int pos
= find_context_div(bmap
, dom
, i
);
3951 pos
= isl_basic_map_alloc_div(bmap
);
3954 isl_int_set_si(bmap
->div
[pos
][0], 0);
3956 if (pos
!= other
+ i
)
3957 isl_basic_map_swap_div(bmap
, pos
, other
+ i
);
3961 isl_basic_map_free(bmap
);
3965 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
3966 * some obvious symmetries.
3968 * We make sure the divs in the domain are properly ordered,
3969 * because they will be added one by one in the given order
3970 * during the construction of the solution map.
3972 static struct isl_sol
*basic_map_partial_lexopt_base(
3973 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
3974 __isl_give isl_set
**empty
, int max
,
3975 struct isl_sol
*(*init
)(__isl_keep isl_basic_map
*bmap
,
3976 __isl_take isl_basic_set
*dom
, int track_empty
, int max
))
3978 struct isl_tab
*tab
;
3979 struct isl_sol
*sol
= NULL
;
3980 struct isl_context
*context
;
3983 dom
= isl_basic_set_order_divs(dom
);
3984 bmap
= align_context_divs(bmap
, dom
);
3986 sol
= init(bmap
, dom
, !!empty
, max
);
3990 context
= sol
->context
;
3991 if (isl_basic_set_plain_is_empty(context
->op
->peek_basic_set(context
)))
3993 else if (isl_basic_map_plain_is_empty(bmap
)) {
3996 isl_basic_set_copy(context
->op
->peek_basic_set(context
)));
3998 tab
= tab_for_lexmin(bmap
,
3999 context
->op
->peek_basic_set(context
), 1, max
);
4000 tab
= context
->op
->detect_nonnegative_parameters(context
, tab
);
4001 find_solutions_main(sol
, tab
);
4006 isl_basic_map_free(bmap
);
4010 isl_basic_map_free(bmap
);
4014 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
4015 * some obvious symmetries.
4017 * We call basic_map_partial_lexopt_base and extract the results.
4019 static __isl_give isl_map
*basic_map_partial_lexopt_base_map(
4020 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
4021 __isl_give isl_set
**empty
, int max
)
4023 isl_map
*result
= NULL
;
4024 struct isl_sol
*sol
;
4025 struct isl_sol_map
*sol_map
;
4027 sol
= basic_map_partial_lexopt_base(bmap
, dom
, empty
, max
,
4031 sol_map
= (struct isl_sol_map
*) sol
;
4033 result
= isl_map_copy(sol_map
->map
);
4035 *empty
= isl_set_copy(sol_map
->empty
);
4036 sol_free(&sol_map
->sol
);
4040 /* Structure used during detection of parallel constraints.
4041 * n_in: number of "input" variables: isl_dim_param + isl_dim_in
4042 * n_out: number of "output" variables: isl_dim_out + isl_dim_div
4043 * val: the coefficients of the output variables
4045 struct isl_constraint_equal_info
{
4046 isl_basic_map
*bmap
;
4052 /* Check whether the coefficients of the output variables
4053 * of the constraint in "entry" are equal to info->val.
4055 static int constraint_equal(const void *entry
, const void *val
)
4057 isl_int
**row
= (isl_int
**)entry
;
4058 const struct isl_constraint_equal_info
*info
= val
;
4060 return isl_seq_eq((*row
) + 1 + info
->n_in
, info
->val
, info
->n_out
);
4063 /* Check whether "bmap" has a pair of constraints that have
4064 * the same coefficients for the output variables.
4065 * Note that the coefficients of the existentially quantified
4066 * variables need to be zero since the existentially quantified
4067 * of the result are usually not the same as those of the input.
4068 * the isl_dim_out and isl_dim_div dimensions.
4069 * If so, return 1 and return the row indices of the two constraints
4070 * in *first and *second.
4072 static int parallel_constraints(__isl_keep isl_basic_map
*bmap
,
4073 int *first
, int *second
)
4076 isl_ctx
*ctx
= isl_basic_map_get_ctx(bmap
);
4077 struct isl_hash_table
*table
= NULL
;
4078 struct isl_hash_table_entry
*entry
;
4079 struct isl_constraint_equal_info info
;
4083 ctx
= isl_basic_map_get_ctx(bmap
);
4084 table
= isl_hash_table_alloc(ctx
, bmap
->n_ineq
);
4088 info
.n_in
= isl_basic_map_dim(bmap
, isl_dim_param
) +
4089 isl_basic_map_dim(bmap
, isl_dim_in
);
4091 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4092 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4093 info
.n_out
= n_out
+ n_div
;
4094 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
4097 info
.val
= bmap
->ineq
[i
] + 1 + info
.n_in
;
4098 if (isl_seq_first_non_zero(info
.val
, n_out
) < 0)
4100 if (isl_seq_first_non_zero(info
.val
+ n_out
, n_div
) >= 0)
4102 hash
= isl_seq_get_hash(info
.val
, info
.n_out
);
4103 entry
= isl_hash_table_find(ctx
, table
, hash
,
4104 constraint_equal
, &info
, 1);
4109 entry
->data
= &bmap
->ineq
[i
];
4112 if (i
< bmap
->n_ineq
) {
4113 *first
= ((isl_int
**)entry
->data
) - bmap
->ineq
;
4117 isl_hash_table_free(ctx
, table
);
4119 return i
< bmap
->n_ineq
;
4121 isl_hash_table_free(ctx
, table
);
4125 /* Given a set of upper bounds in "var", add constraints to "bset"
4126 * that make the i-th bound smallest.
4128 * In particular, if there are n bounds b_i, then add the constraints
4130 * b_i <= b_j for j > i
4131 * b_i < b_j for j < i
4133 static __isl_give isl_basic_set
*select_minimum(__isl_take isl_basic_set
*bset
,
4134 __isl_keep isl_mat
*var
, int i
)
4139 ctx
= isl_mat_get_ctx(var
);
4141 for (j
= 0; j
< var
->n_row
; ++j
) {
4144 k
= isl_basic_set_alloc_inequality(bset
);
4147 isl_seq_combine(bset
->ineq
[k
], ctx
->one
, var
->row
[j
],
4148 ctx
->negone
, var
->row
[i
], var
->n_col
);
4149 isl_int_set_si(bset
->ineq
[k
][var
->n_col
], 0);
4151 isl_int_sub_ui(bset
->ineq
[k
][0], bset
->ineq
[k
][0], 1);
4154 bset
= isl_basic_set_finalize(bset
);
4158 isl_basic_set_free(bset
);
4162 /* Given a set of upper bounds on the last "input" variable m,
4163 * construct a set that assigns the minimal upper bound to m, i.e.,
4164 * construct a set that divides the space into cells where one
4165 * of the upper bounds is smaller than all the others and assign
4166 * this upper bound to m.
4168 * In particular, if there are n bounds b_i, then the result
4169 * consists of n basic sets, each one of the form
4172 * b_i <= b_j for j > i
4173 * b_i < b_j for j < i
4175 static __isl_give isl_set
*set_minimum(__isl_take isl_space
*dim
,
4176 __isl_take isl_mat
*var
)
4179 isl_basic_set
*bset
= NULL
;
4181 isl_set
*set
= NULL
;
4186 ctx
= isl_space_get_ctx(dim
);
4187 set
= isl_set_alloc_space(isl_space_copy(dim
),
4188 var
->n_row
, ISL_SET_DISJOINT
);
4190 for (i
= 0; i
< var
->n_row
; ++i
) {
4191 bset
= isl_basic_set_alloc_space(isl_space_copy(dim
), 0,
4193 k
= isl_basic_set_alloc_equality(bset
);
4196 isl_seq_cpy(bset
->eq
[k
], var
->row
[i
], var
->n_col
);
4197 isl_int_set_si(bset
->eq
[k
][var
->n_col
], -1);
4198 bset
= select_minimum(bset
, var
, i
);
4199 set
= isl_set_add_basic_set(set
, bset
);
4202 isl_space_free(dim
);
4206 isl_basic_set_free(bset
);
4208 isl_space_free(dim
);
4213 /* Given that the last input variable of "bmap" represents the minimum
4214 * of the bounds in "cst", check whether we need to split the domain
4215 * based on which bound attains the minimum.
4217 * A split is needed when the minimum appears in an integer division
4218 * or in an equality. Otherwise, it is only needed if it appears in
4219 * an upper bound that is different from the upper bounds on which it
4222 static int need_split_basic_map(__isl_keep isl_basic_map
*bmap
,
4223 __isl_keep isl_mat
*cst
)
4229 pos
= cst
->n_col
- 1;
4230 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4232 for (i
= 0; i
< bmap
->n_div
; ++i
)
4233 if (!isl_int_is_zero(bmap
->div
[i
][2 + pos
]))
4236 for (i
= 0; i
< bmap
->n_eq
; ++i
)
4237 if (!isl_int_is_zero(bmap
->eq
[i
][1 + pos
]))
4240 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
4241 if (isl_int_is_nonneg(bmap
->ineq
[i
][1 + pos
]))
4243 if (!isl_int_is_negone(bmap
->ineq
[i
][1 + pos
]))
4245 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + pos
+ 1,
4246 total
- pos
- 1) >= 0)
4249 for (j
= 0; j
< cst
->n_row
; ++j
)
4250 if (isl_seq_eq(bmap
->ineq
[i
], cst
->row
[j
], cst
->n_col
))
4252 if (j
>= cst
->n_row
)
4259 /* Given that the last set variable of "bset" represents the minimum
4260 * of the bounds in "cst", check whether we need to split the domain
4261 * based on which bound attains the minimum.
4263 * We simply call need_split_basic_map here. This is safe because
4264 * the position of the minimum is computed from "cst" and not
4267 static int need_split_basic_set(__isl_keep isl_basic_set
*bset
,
4268 __isl_keep isl_mat
*cst
)
4270 return need_split_basic_map((isl_basic_map
*)bset
, cst
);
4273 /* Given that the last set variable of "set" represents the minimum
4274 * of the bounds in "cst", check whether we need to split the domain
4275 * based on which bound attains the minimum.
4277 static int need_split_set(__isl_keep isl_set
*set
, __isl_keep isl_mat
*cst
)
4281 for (i
= 0; i
< set
->n
; ++i
)
4282 if (need_split_basic_set(set
->p
[i
], cst
))
4288 /* Given a set of which the last set variable is the minimum
4289 * of the bounds in "cst", split each basic set in the set
4290 * in pieces where one of the bounds is (strictly) smaller than the others.
4291 * This subdivision is given in "min_expr".
4292 * The variable is subsequently projected out.
4294 * We only do the split when it is needed.
4295 * For example if the last input variable m = min(a,b) and the only
4296 * constraints in the given basic set are lower bounds on m,
4297 * i.e., l <= m = min(a,b), then we can simply project out m
4298 * to obtain l <= a and l <= b, without having to split on whether
4299 * m is equal to a or b.
4301 static __isl_give isl_set
*split(__isl_take isl_set
*empty
,
4302 __isl_take isl_set
*min_expr
, __isl_take isl_mat
*cst
)
4309 if (!empty
|| !min_expr
|| !cst
)
4312 n_in
= isl_set_dim(empty
, isl_dim_set
);
4313 dim
= isl_set_get_space(empty
);
4314 dim
= isl_space_drop_dims(dim
, isl_dim_set
, n_in
- 1, 1);
4315 res
= isl_set_empty(dim
);
4317 for (i
= 0; i
< empty
->n
; ++i
) {
4320 set
= isl_set_from_basic_set(isl_basic_set_copy(empty
->p
[i
]));
4321 if (need_split_basic_set(empty
->p
[i
], cst
))
4322 set
= isl_set_intersect(set
, isl_set_copy(min_expr
));
4323 set
= isl_set_remove_dims(set
, isl_dim_set
, n_in
- 1, 1);
4325 res
= isl_set_union_disjoint(res
, set
);
4328 isl_set_free(empty
);
4329 isl_set_free(min_expr
);
4333 isl_set_free(empty
);
4334 isl_set_free(min_expr
);
4339 /* Given a map of which the last input variable is the minimum
4340 * of the bounds in "cst", split each basic set in the set
4341 * in pieces where one of the bounds is (strictly) smaller than the others.
4342 * This subdivision is given in "min_expr".
4343 * The variable is subsequently projected out.
4345 * The implementation is essentially the same as that of "split".
4347 static __isl_give isl_map
*split_domain(__isl_take isl_map
*opt
,
4348 __isl_take isl_set
*min_expr
, __isl_take isl_mat
*cst
)
4355 if (!opt
|| !min_expr
|| !cst
)
4358 n_in
= isl_map_dim(opt
, isl_dim_in
);
4359 dim
= isl_map_get_space(opt
);
4360 dim
= isl_space_drop_dims(dim
, isl_dim_in
, n_in
- 1, 1);
4361 res
= isl_map_empty(dim
);
4363 for (i
= 0; i
< opt
->n
; ++i
) {
4366 map
= isl_map_from_basic_map(isl_basic_map_copy(opt
->p
[i
]));
4367 if (need_split_basic_map(opt
->p
[i
], cst
))
4368 map
= isl_map_intersect_domain(map
,
4369 isl_set_copy(min_expr
));
4370 map
= isl_map_remove_dims(map
, isl_dim_in
, n_in
- 1, 1);
4372 res
= isl_map_union_disjoint(res
, map
);
4376 isl_set_free(min_expr
);
4381 isl_set_free(min_expr
);
4386 static __isl_give isl_map
*basic_map_partial_lexopt(
4387 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
4388 __isl_give isl_set
**empty
, int max
);
4393 isl_pw_multi_aff
*pma
;
4396 /* This function is called from basic_map_partial_lexopt_symm.
4397 * The last variable of "bmap" and "dom" corresponds to the minimum
4398 * of the bounds in "cst". "map_space" is the space of the original
4399 * input relation (of basic_map_partial_lexopt_symm) and "set_space"
4400 * is the space of the original domain.
4402 * We recursively call basic_map_partial_lexopt and then plug in
4403 * the definition of the minimum in the result.
4405 static __isl_give
union isl_lex_res
basic_map_partial_lexopt_symm_map_core(
4406 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
4407 __isl_give isl_set
**empty
, int max
, __isl_take isl_mat
*cst
,
4408 __isl_take isl_space
*map_space
, __isl_take isl_space
*set_space
)
4412 union isl_lex_res res
;
4414 min_expr
= set_minimum(isl_basic_set_get_space(dom
), isl_mat_copy(cst
));
4416 opt
= basic_map_partial_lexopt(bmap
, dom
, empty
, max
);
4419 *empty
= split(*empty
,
4420 isl_set_copy(min_expr
), isl_mat_copy(cst
));
4421 *empty
= isl_set_reset_space(*empty
, set_space
);
4424 opt
= split_domain(opt
, min_expr
, cst
);
4425 opt
= isl_map_reset_space(opt
, map_space
);
4431 /* Given a basic map with at least two parallel constraints (as found
4432 * by the function parallel_constraints), first look for more constraints
4433 * parallel to the two constraint and replace the found list of parallel
4434 * constraints by a single constraint with as "input" part the minimum
4435 * of the input parts of the list of constraints. Then, recursively call
4436 * basic_map_partial_lexopt (possibly finding more parallel constraints)
4437 * and plug in the definition of the minimum in the result.
4439 * More specifically, given a set of constraints
4443 * Replace this set by a single constraint
4447 * with u a new parameter with constraints
4451 * Any solution to the new system is also a solution for the original system
4454 * a x >= -u >= -b_i(p)
4456 * Moreover, m = min_i(b_i(p)) satisfies the constraints on u and can
4457 * therefore be plugged into the solution.
4459 static union isl_lex_res
basic_map_partial_lexopt_symm(
4460 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
4461 __isl_give isl_set
**empty
, int max
, int first
, int second
,
4462 __isl_give
union isl_lex_res (*core
)(__isl_take isl_basic_map
*bmap
,
4463 __isl_take isl_basic_set
*dom
,
4464 __isl_give isl_set
**empty
,
4465 int max
, __isl_take isl_mat
*cst
,
4466 __isl_take isl_space
*map_space
,
4467 __isl_take isl_space
*set_space
))
4471 unsigned n_in
, n_out
, n_div
;
4473 isl_vec
*var
= NULL
;
4474 isl_mat
*cst
= NULL
;
4475 isl_space
*map_space
, *set_space
;
4476 union isl_lex_res res
;
4478 map_space
= isl_basic_map_get_space(bmap
);
4479 set_space
= empty
? isl_basic_set_get_space(dom
) : NULL
;
4481 n_in
= isl_basic_map_dim(bmap
, isl_dim_param
) +
4482 isl_basic_map_dim(bmap
, isl_dim_in
);
4483 n_out
= isl_basic_map_dim(bmap
, isl_dim_all
) - n_in
;
4485 ctx
= isl_basic_map_get_ctx(bmap
);
4486 list
= isl_alloc_array(ctx
, int, bmap
->n_ineq
);
4487 var
= isl_vec_alloc(ctx
, n_out
);
4493 isl_seq_cpy(var
->el
, bmap
->ineq
[first
] + 1 + n_in
, n_out
);
4494 for (i
= second
+ 1, n
= 2; i
< bmap
->n_ineq
; ++i
) {
4495 if (isl_seq_eq(var
->el
, bmap
->ineq
[i
] + 1 + n_in
, n_out
))
4499 cst
= isl_mat_alloc(ctx
, n
, 1 + n_in
);
4503 for (i
= 0; i
< n
; ++i
)
4504 isl_seq_cpy(cst
->row
[i
], bmap
->ineq
[list
[i
]], 1 + n_in
);
4506 bmap
= isl_basic_map_cow(bmap
);
4509 for (i
= n
- 1; i
>= 0; --i
)
4510 if (isl_basic_map_drop_inequality(bmap
, list
[i
]) < 0)
4513 bmap
= isl_basic_map_add(bmap
, isl_dim_in
, 1);
4514 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
4515 k
= isl_basic_map_alloc_inequality(bmap
);
4518 isl_seq_clr(bmap
->ineq
[k
], 1 + n_in
);
4519 isl_int_set_si(bmap
->ineq
[k
][1 + n_in
], 1);
4520 isl_seq_cpy(bmap
->ineq
[k
] + 1 + n_in
+ 1, var
->el
, n_out
);
4521 bmap
= isl_basic_map_finalize(bmap
);
4523 n_div
= isl_basic_set_dim(dom
, isl_dim_div
);
4524 dom
= isl_basic_set_add_dims(dom
, isl_dim_set
, 1);
4525 dom
= isl_basic_set_extend_constraints(dom
, 0, n
);
4526 for (i
= 0; i
< n
; ++i
) {
4527 k
= isl_basic_set_alloc_inequality(dom
);
4530 isl_seq_cpy(dom
->ineq
[k
], cst
->row
[i
], 1 + n_in
);
4531 isl_int_set_si(dom
->ineq
[k
][1 + n_in
], -1);
4532 isl_seq_clr(dom
->ineq
[k
] + 1 + n_in
+ 1, n_div
);
4538 return core(bmap
, dom
, empty
, max
, cst
, map_space
, set_space
);
4540 isl_space_free(map_space
);
4541 isl_space_free(set_space
);
4545 isl_basic_set_free(dom
);
4546 isl_basic_map_free(bmap
);
4551 static __isl_give isl_map
*basic_map_partial_lexopt_symm_map(
4552 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
4553 __isl_give isl_set
**empty
, int max
, int first
, int second
)
4555 return basic_map_partial_lexopt_symm(bmap
, dom
, empty
, max
,
4556 first
, second
, &basic_map_partial_lexopt_symm_map_core
).map
;
4559 /* Recursive part of isl_tab_basic_map_partial_lexopt, after detecting
4560 * equalities and removing redundant constraints.
4562 * We first check if there are any parallel constraints (left).
4563 * If not, we are in the base case.
4564 * If there are parallel constraints, we replace them by a single
4565 * constraint in basic_map_partial_lexopt_symm and then call
4566 * this function recursively to look for more parallel constraints.
4568 static __isl_give isl_map
*basic_map_partial_lexopt(
4569 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
4570 __isl_give isl_set
**empty
, int max
)
4578 if (bmap
->ctx
->opt
->pip_symmetry
)
4579 par
= parallel_constraints(bmap
, &first
, &second
);
4583 return basic_map_partial_lexopt_base_map(bmap
, dom
, empty
, max
);
4585 return basic_map_partial_lexopt_symm_map(bmap
, dom
, empty
, max
,
4588 isl_basic_set_free(dom
);
4589 isl_basic_map_free(bmap
);
4593 /* Compute the lexicographic minimum (or maximum if "max" is set)
4594 * of "bmap" over the domain "dom" and return the result as a map.
4595 * If "empty" is not NULL, then *empty is assigned a set that
4596 * contains those parts of the domain where there is no solution.
4597 * If "bmap" is marked as rational (ISL_BASIC_MAP_RATIONAL),
4598 * then we compute the rational optimum. Otherwise, we compute
4599 * the integral optimum.
4601 * We perform some preprocessing. As the PILP solver does not
4602 * handle implicit equalities very well, we first make sure all
4603 * the equalities are explicitly available.
4605 * We also add context constraints to the basic map and remove
4606 * redundant constraints. This is only needed because of the
4607 * way we handle simple symmetries. In particular, we currently look
4608 * for symmetries on the constraints, before we set up the main tableau.
4609 * It is then no good to look for symmetries on possibly redundant constraints.
4611 struct isl_map
*isl_tab_basic_map_partial_lexopt(
4612 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
4613 struct isl_set
**empty
, int max
)
4620 isl_assert(bmap
->ctx
,
4621 isl_basic_map_compatible_domain(bmap
, dom
), goto error
);
4623 if (isl_basic_set_dim(dom
, isl_dim_all
) == 0)
4624 return basic_map_partial_lexopt(bmap
, dom
, empty
, max
);
4626 bmap
= isl_basic_map_intersect_domain(bmap
, isl_basic_set_copy(dom
));
4627 bmap
= isl_basic_map_detect_equalities(bmap
);
4628 bmap
= isl_basic_map_remove_redundancies(bmap
);
4630 return basic_map_partial_lexopt(bmap
, dom
, empty
, max
);
4632 isl_basic_set_free(dom
);
4633 isl_basic_map_free(bmap
);
4637 struct isl_sol_for
{
4639 int (*fn
)(__isl_take isl_basic_set
*dom
,
4640 __isl_take isl_aff_list
*list
, void *user
);
4644 static void sol_for_free(struct isl_sol_for
*sol_for
)
4646 if (sol_for
->sol
.context
)
4647 sol_for
->sol
.context
->op
->free(sol_for
->sol
.context
);
4651 static void sol_for_free_wrap(struct isl_sol
*sol
)
4653 sol_for_free((struct isl_sol_for
*)sol
);
4656 /* Add the solution identified by the tableau and the context tableau.
4658 * See documentation of sol_add for more details.
4660 * Instead of constructing a basic map, this function calls a user
4661 * defined function with the current context as a basic set and
4662 * a list of affine expressions representing the relation between
4663 * the input and output. The space over which the affine expressions
4664 * are defined is the same as that of the domain. The number of
4665 * affine expressions in the list is equal to the number of output variables.
4667 static void sol_for_add(struct isl_sol_for
*sol
,
4668 struct isl_basic_set
*dom
, struct isl_mat
*M
)
4672 isl_local_space
*ls
;
4676 if (sol
->sol
.error
|| !dom
|| !M
)
4679 ctx
= isl_basic_set_get_ctx(dom
);
4680 ls
= isl_basic_set_get_local_space(dom
);
4681 list
= isl_aff_list_alloc(ctx
, M
->n_row
- 1);
4682 for (i
= 1; i
< M
->n_row
; ++i
) {
4683 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
4685 isl_int_set(aff
->v
->el
[0], M
->row
[0][0]);
4686 isl_seq_cpy(aff
->v
->el
+ 1, M
->row
[i
], M
->n_col
);
4688 aff
= isl_aff_normalize(aff
);
4689 list
= isl_aff_list_add(list
, aff
);
4691 isl_local_space_free(ls
);
4693 dom
= isl_basic_set_finalize(dom
);
4695 if (sol
->fn(isl_basic_set_copy(dom
), list
, sol
->user
) < 0)
4698 isl_basic_set_free(dom
);
4702 isl_basic_set_free(dom
);
4707 static void sol_for_add_wrap(struct isl_sol
*sol
,
4708 struct isl_basic_set
*dom
, struct isl_mat
*M
)
4710 sol_for_add((struct isl_sol_for
*)sol
, dom
, M
);
4713 static struct isl_sol_for
*sol_for_init(struct isl_basic_map
*bmap
, int max
,
4714 int (*fn
)(__isl_take isl_basic_set
*dom
, __isl_take isl_aff_list
*list
,
4718 struct isl_sol_for
*sol_for
= NULL
;
4720 struct isl_basic_set
*dom
= NULL
;
4722 sol_for
= isl_calloc_type(bmap
->ctx
, struct isl_sol_for
);
4726 dom_dim
= isl_space_domain(isl_space_copy(bmap
->dim
));
4727 dom
= isl_basic_set_universe(dom_dim
);
4729 sol_for
->sol
.rational
= ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
4730 sol_for
->sol
.dec_level
.callback
.run
= &sol_dec_level_wrap
;
4731 sol_for
->sol
.dec_level
.sol
= &sol_for
->sol
;
4733 sol_for
->user
= user
;
4734 sol_for
->sol
.max
= max
;
4735 sol_for
->sol
.n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4736 sol_for
->sol
.add
= &sol_for_add_wrap
;
4737 sol_for
->sol
.add_empty
= NULL
;
4738 sol_for
->sol
.free
= &sol_for_free_wrap
;
4740 sol_for
->sol
.context
= isl_context_alloc(dom
);
4741 if (!sol_for
->sol
.context
)
4744 isl_basic_set_free(dom
);
4747 isl_basic_set_free(dom
);
4748 sol_for_free(sol_for
);
4752 static void sol_for_find_solutions(struct isl_sol_for
*sol_for
,
4753 struct isl_tab
*tab
)
4755 find_solutions_main(&sol_for
->sol
, tab
);
4758 int isl_basic_map_foreach_lexopt(__isl_keep isl_basic_map
*bmap
, int max
,
4759 int (*fn
)(__isl_take isl_basic_set
*dom
, __isl_take isl_aff_list
*list
,
4763 struct isl_sol_for
*sol_for
= NULL
;
4765 bmap
= isl_basic_map_copy(bmap
);
4769 bmap
= isl_basic_map_detect_equalities(bmap
);
4770 sol_for
= sol_for_init(bmap
, max
, fn
, user
);
4772 if (isl_basic_map_plain_is_empty(bmap
))
4775 struct isl_tab
*tab
;
4776 struct isl_context
*context
= sol_for
->sol
.context
;
4777 tab
= tab_for_lexmin(bmap
,
4778 context
->op
->peek_basic_set(context
), 1, max
);
4779 tab
= context
->op
->detect_nonnegative_parameters(context
, tab
);
4780 sol_for_find_solutions(sol_for
, tab
);
4781 if (sol_for
->sol
.error
)
4785 sol_free(&sol_for
->sol
);
4786 isl_basic_map_free(bmap
);
4789 sol_free(&sol_for
->sol
);
4790 isl_basic_map_free(bmap
);
4794 int isl_basic_set_foreach_lexopt(__isl_keep isl_basic_set
*bset
, int max
,
4795 int (*fn
)(__isl_take isl_basic_set
*dom
, __isl_take isl_aff_list
*list
,
4799 return isl_basic_map_foreach_lexopt(bset
, max
, fn
, user
);
4802 /* Check if the given sequence of len variables starting at pos
4803 * represents a trivial (i.e., zero) solution.
4804 * The variables are assumed to be non-negative and to come in pairs,
4805 * with each pair representing a variable of unrestricted sign.
4806 * The solution is trivial if each such pair in the sequence consists
4807 * of two identical values, meaning that the variable being represented
4810 static int region_is_trivial(struct isl_tab
*tab
, int pos
, int len
)
4817 for (i
= 0; i
< len
; i
+= 2) {
4821 neg_row
= tab
->var
[pos
+ i
].is_row
?
4822 tab
->var
[pos
+ i
].index
: -1;
4823 pos_row
= tab
->var
[pos
+ i
+ 1].is_row
?
4824 tab
->var
[pos
+ i
+ 1].index
: -1;
4827 isl_int_is_zero(tab
->mat
->row
[neg_row
][1])) &&
4829 isl_int_is_zero(tab
->mat
->row
[pos_row
][1])))
4832 if (neg_row
< 0 || pos_row
< 0)
4834 if (isl_int_ne(tab
->mat
->row
[neg_row
][1],
4835 tab
->mat
->row
[pos_row
][1]))
4842 /* Return the index of the first trivial region or -1 if all regions
4845 static int first_trivial_region(struct isl_tab
*tab
,
4846 int n_region
, struct isl_region
*region
)
4850 for (i
= 0; i
< n_region
; ++i
) {
4851 if (region_is_trivial(tab
, region
[i
].pos
, region
[i
].len
))
4858 /* Check if the solution is optimal, i.e., whether the first
4859 * n_op entries are zero.
4861 static int is_optimal(__isl_keep isl_vec
*sol
, int n_op
)
4865 for (i
= 0; i
< n_op
; ++i
)
4866 if (!isl_int_is_zero(sol
->el
[1 + i
]))
4871 /* Add constraints to "tab" that ensure that any solution is significantly
4872 * better that that represented by "sol". That is, find the first
4873 * relevant (within first n_op) non-zero coefficient and force it (along
4874 * with all previous coefficients) to be zero.
4875 * If the solution is already optimal (all relevant coefficients are zero),
4876 * then just mark the table as empty.
4878 static int force_better_solution(struct isl_tab
*tab
,
4879 __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
]))
4893 if (isl_tab_mark_empty(tab
) < 0)
4898 ctx
= isl_vec_get_ctx(sol
);
4899 v
= isl_vec_alloc(ctx
, 1 + tab
->n_var
);
4903 for (; i
>= 0; --i
) {
4905 isl_int_set_si(v
->el
[1 + i
], -1);
4906 if (add_lexmin_eq(tab
, v
->el
) < 0)
4917 struct isl_trivial
{
4921 struct isl_tab_undo
*snap
;
4924 /* Return the lexicographically smallest non-trivial solution of the
4925 * given ILP problem.
4927 * All variables are assumed to be non-negative.
4929 * n_op is the number of initial coordinates to optimize.
4930 * That is, once a solution has been found, we will only continue looking
4931 * for solution that result in significantly better values for those
4932 * initial coordinates. That is, we only continue looking for solutions
4933 * that increase the number of initial zeros in this sequence.
4935 * A solution is non-trivial, if it is non-trivial on each of the
4936 * specified regions. Each region represents a sequence of pairs
4937 * of variables. A solution is non-trivial on such a region if
4938 * at least one of these pairs consists of different values, i.e.,
4939 * such that the non-negative variable represented by the pair is non-zero.
4941 * Whenever a conflict is encountered, all constraints involved are
4942 * reported to the caller through a call to "conflict".
4944 * We perform a simple branch-and-bound backtracking search.
4945 * Each level in the search represents initially trivial region that is forced
4946 * to be non-trivial.
4947 * At each level we consider n cases, where n is the length of the region.
4948 * In terms of the n/2 variables of unrestricted signs being encoded by
4949 * the region, we consider the cases
4952 * x_0 = 0 and x_1 >= 1
4953 * x_0 = 0 and x_1 <= -1
4954 * x_0 = 0 and x_1 = 0 and x_2 >= 1
4955 * x_0 = 0 and x_1 = 0 and x_2 <= -1
4957 * The cases are considered in this order, assuming that each pair
4958 * x_i_a x_i_b represents the value x_i_b - x_i_a.
4959 * That is, x_0 >= 1 is enforced by adding the constraint
4960 * x_0_b - x_0_a >= 1
4962 __isl_give isl_vec
*isl_tab_basic_set_non_trivial_lexmin(
4963 __isl_take isl_basic_set
*bset
, int n_op
, int n_region
,
4964 struct isl_region
*region
,
4965 int (*conflict
)(int con
, void *user
), void *user
)
4969 isl_ctx
*ctx
= isl_basic_set_get_ctx(bset
);
4971 isl_vec
*sol
= isl_vec_alloc(ctx
, 0);
4972 struct isl_tab
*tab
;
4973 struct isl_trivial
*triv
= NULL
;
4976 tab
= tab_for_lexmin(bset
, NULL
, 0, 0);
4979 tab
->conflict
= conflict
;
4980 tab
->conflict_user
= user
;
4982 v
= isl_vec_alloc(ctx
, 1 + tab
->n_var
);
4983 triv
= isl_calloc_array(ctx
, struct isl_trivial
, n_region
);
4990 while (level
>= 0) {
4994 tab
= cut_to_integer_lexmin(tab
, CUT_ONE
);
4999 r
= first_trivial_region(tab
, n_region
, region
);
5001 for (i
= 0; i
< level
; ++i
)
5004 sol
= isl_tab_get_sample_value(tab
);
5007 if (is_optimal(sol
, n_op
))
5011 if (level
>= n_region
)
5012 isl_die(ctx
, isl_error_internal
,
5013 "nesting level too deep", goto error
);
5014 if (isl_tab_extend_cons(tab
,
5015 2 * region
[r
].len
+ 2 * n_op
) < 0)
5017 triv
[level
].region
= r
;
5018 triv
[level
].side
= 0;
5021 r
= triv
[level
].region
;
5022 side
= triv
[level
].side
;
5023 base
= 2 * (side
/2);
5025 if (side
>= region
[r
].len
) {
5030 if (isl_tab_rollback(tab
, triv
[level
].snap
) < 0)
5035 if (triv
[level
].update
) {
5036 if (force_better_solution(tab
, sol
, n_op
) < 0)
5038 triv
[level
].update
= 0;
5041 if (side
== base
&& base
>= 2) {
5042 for (j
= base
- 2; j
< base
; ++j
) {
5044 isl_int_set_si(v
->el
[1 + region
[r
].pos
+ j
], 1);
5045 if (add_lexmin_eq(tab
, v
->el
) < 0)
5050 triv
[level
].snap
= isl_tab_snap(tab
);
5051 if (isl_tab_push_basis(tab
) < 0)
5055 isl_int_set_si(v
->el
[0], -1);
5056 isl_int_set_si(v
->el
[1 + region
[r
].pos
+ side
], -1);
5057 isl_int_set_si(v
->el
[1 + region
[r
].pos
+ (side
^ 1)], 1);
5058 tab
= add_lexmin_ineq(tab
, v
->el
);
5068 isl_basic_set_free(bset
);
5075 isl_basic_set_free(bset
);
5080 /* Return the lexicographically smallest rational point in "bset",
5081 * assuming that all variables are non-negative.
5082 * If "bset" is empty, then return a zero-length vector.
5084 __isl_give isl_vec
*isl_tab_basic_set_non_neg_lexmin(
5085 __isl_take isl_basic_set
*bset
)
5087 struct isl_tab
*tab
;
5088 isl_ctx
*ctx
= isl_basic_set_get_ctx(bset
);
5091 tab
= tab_for_lexmin(bset
, NULL
, 0, 0);
5095 sol
= isl_vec_alloc(ctx
, 0);
5097 sol
= isl_tab_get_sample_value(tab
);
5099 isl_basic_set_free(bset
);
5103 isl_basic_set_free(bset
);
5107 struct isl_sol_pma
{
5109 isl_pw_multi_aff
*pma
;
5113 static void sol_pma_free(struct isl_sol_pma
*sol_pma
)
5117 if (sol_pma
->sol
.context
)
5118 sol_pma
->sol
.context
->op
->free(sol_pma
->sol
.context
);
5119 isl_pw_multi_aff_free(sol_pma
->pma
);
5120 isl_set_free(sol_pma
->empty
);
5124 /* This function is called for parts of the context where there is
5125 * no solution, with "bset" corresponding to the context tableau.
5126 * Simply add the basic set to the set "empty".
5128 static void sol_pma_add_empty(struct isl_sol_pma
*sol
,
5129 __isl_take isl_basic_set
*bset
)
5133 isl_assert(bset
->ctx
, sol
->empty
, goto error
);
5135 sol
->empty
= isl_set_grow(sol
->empty
, 1);
5136 bset
= isl_basic_set_simplify(bset
);
5137 bset
= isl_basic_set_finalize(bset
);
5138 sol
->empty
= isl_set_add_basic_set(sol
->empty
, bset
);
5143 isl_basic_set_free(bset
);
5147 /* Given a basic map "dom" that represents the context and an affine
5148 * matrix "M" that maps the dimensions of the context to the
5149 * output variables, construct an isl_pw_multi_aff with a single
5150 * cell corresponding to "dom" and affine expressions copied from "M".
5152 static void sol_pma_add(struct isl_sol_pma
*sol
,
5153 __isl_take isl_basic_set
*dom
, __isl_take isl_mat
*M
)
5156 isl_local_space
*ls
;
5158 isl_multi_aff
*maff
;
5159 isl_pw_multi_aff
*pma
;
5161 maff
= isl_multi_aff_alloc(isl_pw_multi_aff_get_space(sol
->pma
));
5162 ls
= isl_basic_set_get_local_space(dom
);
5163 for (i
= 1; i
< M
->n_row
; ++i
) {
5164 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
5166 isl_int_set(aff
->v
->el
[0], M
->row
[0][0]);
5167 isl_seq_cpy(aff
->v
->el
+ 1, M
->row
[i
], M
->n_col
);
5169 aff
= isl_aff_normalize(aff
);
5170 maff
= isl_multi_aff_set_aff(maff
, i
- 1, aff
);
5172 isl_local_space_free(ls
);
5174 dom
= isl_basic_set_simplify(dom
);
5175 dom
= isl_basic_set_finalize(dom
);
5176 pma
= isl_pw_multi_aff_alloc(isl_set_from_basic_set(dom
), maff
);
5177 sol
->pma
= isl_pw_multi_aff_add_disjoint(sol
->pma
, pma
);
5182 static void sol_pma_free_wrap(struct isl_sol
*sol
)
5184 sol_pma_free((struct isl_sol_pma
*)sol
);
5187 static void sol_pma_add_empty_wrap(struct isl_sol
*sol
,
5188 __isl_take isl_basic_set
*bset
)
5190 sol_pma_add_empty((struct isl_sol_pma
*)sol
, bset
);
5193 static void sol_pma_add_wrap(struct isl_sol
*sol
,
5194 __isl_take isl_basic_set
*dom
, __isl_take isl_mat
*M
)
5196 sol_pma_add((struct isl_sol_pma
*)sol
, dom
, M
);
5199 /* Construct an isl_sol_pma structure for accumulating the solution.
5200 * If track_empty is set, then we also keep track of the parts
5201 * of the context where there is no solution.
5202 * If max is set, then we are solving a maximization, rather than
5203 * a minimization problem, which means that the variables in the
5204 * tableau have value "M - x" rather than "M + x".
5206 static struct isl_sol
*sol_pma_init(__isl_keep isl_basic_map
*bmap
,
5207 __isl_take isl_basic_set
*dom
, int track_empty
, int max
)
5209 struct isl_sol_pma
*sol_pma
= NULL
;
5214 sol_pma
= isl_calloc_type(bmap
->ctx
, struct isl_sol_pma
);
5218 sol_pma
->sol
.rational
= ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
5219 sol_pma
->sol
.dec_level
.callback
.run
= &sol_dec_level_wrap
;
5220 sol_pma
->sol
.dec_level
.sol
= &sol_pma
->sol
;
5221 sol_pma
->sol
.max
= max
;
5222 sol_pma
->sol
.n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5223 sol_pma
->sol
.add
= &sol_pma_add_wrap
;
5224 sol_pma
->sol
.add_empty
= track_empty
? &sol_pma_add_empty_wrap
: NULL
;
5225 sol_pma
->sol
.free
= &sol_pma_free_wrap
;
5226 sol_pma
->pma
= isl_pw_multi_aff_empty(isl_basic_map_get_space(bmap
));
5230 sol_pma
->sol
.context
= isl_context_alloc(dom
);
5231 if (!sol_pma
->sol
.context
)
5235 sol_pma
->empty
= isl_set_alloc_space(isl_basic_set_get_space(dom
),
5236 1, ISL_SET_DISJOINT
);
5237 if (!sol_pma
->empty
)
5241 isl_basic_set_free(dom
);
5242 return &sol_pma
->sol
;
5244 isl_basic_set_free(dom
);
5245 sol_pma_free(sol_pma
);
5249 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
5250 * some obvious symmetries.
5252 * We call basic_map_partial_lexopt_base and extract the results.
5254 static __isl_give isl_pw_multi_aff
*basic_map_partial_lexopt_base_pma(
5255 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5256 __isl_give isl_set
**empty
, int max
)
5258 isl_pw_multi_aff
*result
= NULL
;
5259 struct isl_sol
*sol
;
5260 struct isl_sol_pma
*sol_pma
;
5262 sol
= basic_map_partial_lexopt_base(bmap
, dom
, empty
, max
,
5266 sol_pma
= (struct isl_sol_pma
*) sol
;
5268 result
= isl_pw_multi_aff_copy(sol_pma
->pma
);
5270 *empty
= isl_set_copy(sol_pma
->empty
);
5271 sol_free(&sol_pma
->sol
);
5275 /* Given that the last input variable of "maff" represents the minimum
5276 * of some bounds, check whether we need to plug in the expression
5279 * In particular, check if the last input variable appears in any
5280 * of the expressions in "maff".
5282 static int need_substitution(__isl_keep isl_multi_aff
*maff
)
5287 pos
= isl_multi_aff_dim(maff
, isl_dim_in
) - 1;
5289 for (i
= 0; i
< maff
->n
; ++i
)
5290 if (isl_aff_involves_dims(maff
->p
[i
], isl_dim_in
, pos
, 1))
5296 /* Given a set of upper bounds on the last "input" variable m,
5297 * construct a piecewise affine expression that selects
5298 * the minimal upper bound to m, i.e.,
5299 * divide the space into cells where one
5300 * of the upper bounds is smaller than all the others and select
5301 * this upper bound on that cell.
5303 * In particular, if there are n bounds b_i, then the result
5304 * consists of n cell, each one of the form
5306 * b_i <= b_j for j > i
5307 * b_i < b_j for j < i
5309 * The affine expression on this cell is
5313 static __isl_give isl_pw_aff
*set_minimum_pa(__isl_take isl_space
*space
,
5314 __isl_take isl_mat
*var
)
5317 isl_aff
*aff
= NULL
;
5318 isl_basic_set
*bset
= NULL
;
5320 isl_pw_aff
*paff
= NULL
;
5321 isl_space
*pw_space
;
5322 isl_local_space
*ls
= NULL
;
5327 ctx
= isl_space_get_ctx(space
);
5328 ls
= isl_local_space_from_space(isl_space_copy(space
));
5329 pw_space
= isl_space_copy(space
);
5330 pw_space
= isl_space_from_domain(pw_space
);
5331 pw_space
= isl_space_add_dims(pw_space
, isl_dim_out
, 1);
5332 paff
= isl_pw_aff_alloc_size(pw_space
, var
->n_row
);
5334 for (i
= 0; i
< var
->n_row
; ++i
) {
5337 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
5338 bset
= isl_basic_set_alloc_space(isl_space_copy(space
), 0,
5342 isl_int_set_si(aff
->v
->el
[0], 1);
5343 isl_seq_cpy(aff
->v
->el
+ 1, var
->row
[i
], var
->n_col
);
5344 isl_int_set_si(aff
->v
->el
[1 + var
->n_col
], 0);
5345 bset
= select_minimum(bset
, var
, i
);
5346 paff_i
= isl_pw_aff_alloc(isl_set_from_basic_set(bset
), aff
);
5347 paff
= isl_pw_aff_add_disjoint(paff
, paff_i
);
5350 isl_local_space_free(ls
);
5351 isl_space_free(space
);
5356 isl_basic_set_free(bset
);
5357 isl_pw_aff_free(paff
);
5358 isl_local_space_free(ls
);
5359 isl_space_free(space
);
5364 /* Given a piecewise multi-affine expression of which the last input variable
5365 * is the minimum of the bounds in "cst", plug in the value of the minimum.
5366 * This minimum expression is given in "min_expr_pa".
5367 * The set "min_expr" contains the same information, but in the form of a set.
5368 * The variable is subsequently projected out.
5370 * The implementation is similar to those of "split" and "split_domain".
5371 * If the variable appears in a given expression, then minimum expression
5372 * is plugged in. Otherwise, if the variable appears in the constraints
5373 * and a split is required, then the domain is split. Otherwise, no split
5376 static __isl_give isl_pw_multi_aff
*split_domain_pma(
5377 __isl_take isl_pw_multi_aff
*opt
, __isl_take isl_pw_aff
*min_expr_pa
,
5378 __isl_take isl_set
*min_expr
, __isl_take isl_mat
*cst
)
5383 isl_pw_multi_aff
*res
;
5385 if (!opt
|| !min_expr
|| !cst
)
5388 n_in
= isl_pw_multi_aff_dim(opt
, isl_dim_in
);
5389 space
= isl_pw_multi_aff_get_space(opt
);
5390 space
= isl_space_drop_dims(space
, isl_dim_in
, n_in
- 1, 1);
5391 res
= isl_pw_multi_aff_empty(space
);
5393 for (i
= 0; i
< opt
->n
; ++i
) {
5394 isl_pw_multi_aff
*pma
;
5396 pma
= isl_pw_multi_aff_alloc(isl_set_copy(opt
->p
[i
].set
),
5397 isl_multi_aff_copy(opt
->p
[i
].maff
));
5398 if (need_substitution(opt
->p
[i
].maff
))
5399 pma
= isl_pw_multi_aff_substitute(pma
,
5400 isl_dim_in
, n_in
- 1, min_expr_pa
);
5401 else if (need_split_set(opt
->p
[i
].set
, cst
))
5402 pma
= isl_pw_multi_aff_intersect_domain(pma
,
5403 isl_set_copy(min_expr
));
5404 pma
= isl_pw_multi_aff_project_out(pma
,
5405 isl_dim_in
, n_in
- 1, 1);
5407 res
= isl_pw_multi_aff_add_disjoint(res
, pma
);
5410 isl_pw_multi_aff_free(opt
);
5411 isl_pw_aff_free(min_expr_pa
);
5412 isl_set_free(min_expr
);
5416 isl_pw_multi_aff_free(opt
);
5417 isl_pw_aff_free(min_expr_pa
);
5418 isl_set_free(min_expr
);
5423 static __isl_give isl_pw_multi_aff
*basic_map_partial_lexopt_pma(
5424 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5425 __isl_give isl_set
**empty
, int max
);
5427 /* This function is called from basic_map_partial_lexopt_symm.
5428 * The last variable of "bmap" and "dom" corresponds to the minimum
5429 * of the bounds in "cst". "map_space" is the space of the original
5430 * input relation (of basic_map_partial_lexopt_symm) and "set_space"
5431 * is the space of the original domain.
5433 * We recursively call basic_map_partial_lexopt and then plug in
5434 * the definition of the minimum in the result.
5436 static __isl_give
union isl_lex_res
basic_map_partial_lexopt_symm_pma_core(
5437 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5438 __isl_give isl_set
**empty
, int max
, __isl_take isl_mat
*cst
,
5439 __isl_take isl_space
*map_space
, __isl_take isl_space
*set_space
)
5441 isl_pw_multi_aff
*opt
;
5442 isl_pw_aff
*min_expr_pa
;
5444 union isl_lex_res res
;
5446 min_expr
= set_minimum(isl_basic_set_get_space(dom
), isl_mat_copy(cst
));
5447 min_expr_pa
= set_minimum_pa(isl_basic_set_get_space(dom
),
5450 opt
= basic_map_partial_lexopt_pma(bmap
, dom
, empty
, max
);
5453 *empty
= split(*empty
,
5454 isl_set_copy(min_expr
), isl_mat_copy(cst
));
5455 *empty
= isl_set_reset_space(*empty
, set_space
);
5458 opt
= split_domain_pma(opt
, min_expr_pa
, min_expr
, cst
);
5459 opt
= isl_pw_multi_aff_reset_space(opt
, map_space
);
5465 static __isl_give isl_pw_multi_aff
*basic_map_partial_lexopt_symm_pma(
5466 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5467 __isl_give isl_set
**empty
, int max
, int first
, int second
)
5469 return basic_map_partial_lexopt_symm(bmap
, dom
, empty
, max
,
5470 first
, second
, &basic_map_partial_lexopt_symm_pma_core
).pma
;
5473 /* Recursive part of isl_basic_map_partial_lexopt_pw_multi_aff, after detecting
5474 * equalities and removing redundant constraints.
5476 * We first check if there are any parallel constraints (left).
5477 * If not, we are in the base case.
5478 * If there are parallel constraints, we replace them by a single
5479 * constraint in basic_map_partial_lexopt_symm_pma and then call
5480 * this function recursively to look for more parallel constraints.
5482 static __isl_give isl_pw_multi_aff
*basic_map_partial_lexopt_pma(
5483 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5484 __isl_give isl_set
**empty
, int max
)
5492 if (bmap
->ctx
->opt
->pip_symmetry
)
5493 par
= parallel_constraints(bmap
, &first
, &second
);
5497 return basic_map_partial_lexopt_base_pma(bmap
, dom
, empty
, max
);
5499 return basic_map_partial_lexopt_symm_pma(bmap
, dom
, empty
, max
,
5502 isl_basic_set_free(dom
);
5503 isl_basic_map_free(bmap
);
5507 /* Compute the lexicographic minimum (or maximum if "max" is set)
5508 * of "bmap" over the domain "dom" and return the result as a piecewise
5509 * multi-affine expression.
5510 * If "empty" is not NULL, then *empty is assigned a set that
5511 * contains those parts of the domain where there is no solution.
5512 * If "bmap" is marked as rational (ISL_BASIC_MAP_RATIONAL),
5513 * then we compute the rational optimum. Otherwise, we compute
5514 * the integral optimum.
5516 * We perform some preprocessing. As the PILP solver does not
5517 * handle implicit equalities very well, we first make sure all
5518 * the equalities are explicitly available.
5520 * We also add context constraints to the basic map and remove
5521 * redundant constraints. This is only needed because of the
5522 * way we handle simple symmetries. In particular, we currently look
5523 * for symmetries on the constraints, before we set up the main tableau.
5524 * It is then no good to look for symmetries on possibly redundant constraints.
5526 __isl_give isl_pw_multi_aff
*isl_basic_map_partial_lexopt_pw_multi_aff(
5527 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5528 __isl_give isl_set
**empty
, int max
)
5535 isl_assert(bmap
->ctx
,
5536 isl_basic_map_compatible_domain(bmap
, dom
), goto error
);
5538 if (isl_basic_set_dim(dom
, isl_dim_all
) == 0)
5539 return basic_map_partial_lexopt_pma(bmap
, dom
, empty
, max
);
5541 bmap
= isl_basic_map_intersect_domain(bmap
, isl_basic_set_copy(dom
));
5542 bmap
= isl_basic_map_detect_equalities(bmap
);
5543 bmap
= isl_basic_map_remove_redundancies(bmap
);
5545 return basic_map_partial_lexopt_pma(bmap
, dom
, empty
, max
);
5547 isl_basic_set_free(dom
);
5548 isl_basic_map_free(bmap
);