1 #include "isl_map_private.h"
5 * The implementation of parametric integer linear programming in this file
6 * was inspired by the paper "Parametric Integer Programming" and the
7 * report "Solving systems of affine (in)equalities" by Paul Feautrier
10 * The strategy used for obtaining a feasible solution is different
11 * from the one used in isl_tab.c. In particular, in isl_tab.c,
12 * upon finding a constraint that is not yet satisfied, we pivot
13 * in a row that increases the constant term of row holding the
14 * constraint, making sure the sample solution remains feasible
15 * for all the constraints it already satisfied.
16 * Here, we always pivot in the row holding the constraint,
17 * choosing a column that induces the lexicographically smallest
18 * increment to the sample solution.
20 * By starting out from a sample value that is lexicographically
21 * smaller than any integer point in the problem space, the first
22 * feasible integer sample point we find will also be the lexicographically
23 * smallest. If all variables can be assumed to be non-negative,
24 * then the initial sample value may be chosen equal to zero.
25 * However, we will not make this assumption. Instead, we apply
26 * the "big parameter" trick. Any variable x is then not directly
27 * used in the tableau, but instead it its represented by another
28 * variable x' = M + x, where M is an arbitrarily large (positive)
29 * value. x' is therefore always non-negative, whatever the value of x.
30 * Taking as initial smaple value x' = 0 corresponds to x = -M,
31 * which is always smaller than any possible value of x.
33 * We use the big parameter trick both in the main tableau and
34 * the context tableau, each of course having its own big parameter.
35 * Before doing any real work, we check if all the parameters
36 * happen to be non-negative. If so, we drop the column corresponding
37 * to M from the initial context tableau.
40 /* isl_sol is an interface for constructing a solution to
41 * a parametric integer linear programming problem.
42 * Every time the algorithm reaches a state where a solution
43 * can be read off from the tableau (including cases where the tableau
44 * is empty), the function "add" is called on the isl_sol passed
45 * to find_solutions_main.
47 * The context tableau is owned by isl_sol and is updated incrementally.
49 * There is currently only one implementation of this interface,
50 * isl_sol_map, which simply collects the solutions in an isl_map
51 * and (optionally) the parts of the context where there is no solution
55 struct isl_tab
*context_tab
;
56 struct isl_sol
*(*add
)(struct isl_sol
*sol
, struct isl_tab
*tab
);
57 void (*free
)(struct isl_sol
*sol
);
60 static void sol_free(struct isl_sol
*sol
)
70 struct isl_set
*empty
;
74 static void sol_map_free(struct isl_sol_map
*sol_map
)
76 isl_tab_free(sol_map
->sol
.context_tab
);
77 isl_map_free(sol_map
->map
);
78 isl_set_free(sol_map
->empty
);
82 static void sol_map_free_wrap(struct isl_sol
*sol
)
84 sol_map_free((struct isl_sol_map
*)sol
);
87 static struct isl_sol_map
*add_empty(struct isl_sol_map
*sol
)
89 struct isl_basic_set
*bset
;
93 sol
->empty
= isl_set_grow(sol
->empty
, 1);
94 bset
= isl_basic_set_copy(sol
->sol
.context_tab
->bset
);
95 bset
= isl_basic_set_simplify(bset
);
96 bset
= isl_basic_set_finalize(bset
);
97 sol
->empty
= isl_set_add(sol
->empty
, bset
);
106 /* Add the solution identified by the tableau and the context tableau.
108 * The layout of the variables is as follows.
109 * tab->n_var is equal to the total number of variables in the input
110 * map (including divs that were copied from the context)
111 * + the number of extra divs constructed
112 * Of these, the first tab->n_param and the last tab->n_div variables
113 * correspond to the variables in the context, i.e.,
114 tab->n_param + tab->n_div = context_tab->n_var
115 * tab->n_param is equal to the number of parameters and input
116 * dimensions in the input map
117 * tab->n_div is equal to the number of divs in the context
119 * If there is no solution, then the basic set corresponding to the
120 * context tableau is added to the set "empty".
122 * Otherwise, a basic map is constructed with the same parameters
123 * and divs as the context, the dimensions of the context as input
124 * dimensions and a number of output dimensions that is equal to
125 * the number of output dimensions in the input map.
126 * The divs in the input map (if any) that do not correspond to any
127 * div in the context do not appear in the solution.
128 * The algorithm will make sure that they have an integer value,
129 * but these values themselves are of no interest.
131 * The constraints and divs of the context are simply copied
132 * fron context_tab->bset.
133 * To extract the value of the output variables, it should be noted
134 * that we always use a big parameter M and so the variable stored
135 * in the tableau is not an output variable x itself, but
136 * x' = M + x (in case of minimization)
138 * x' = M - x (in case of maximization)
139 * If x' appears in a column, then its optimal value is zero,
140 * which means that the optimal value of x is an unbounded number
141 * (-M for minimization and M for maximization).
142 * We currently assume that the output dimensions in the original map
143 * are bounded, so this cannot occur.
144 * Similarly, when x' appears in a row, then the coefficient of M in that
145 * row is necessarily 1.
146 * If the row represents
147 * d x' = c + d M + e(y)
148 * then, in case of minimization, an equality
149 * c + e(y) - d x' = 0
150 * is added, and in case of maximization,
151 * c + e(y) + d x' = 0
153 static struct isl_sol_map
*sol_map_add(struct isl_sol_map
*sol
,
157 struct isl_basic_map
*bmap
= NULL
;
158 struct isl_tab
*context_tab
;
171 return add_empty(sol
);
173 context_tab
= sol
->sol
.context_tab
;
175 n_out
= isl_map_dim(sol
->map
, isl_dim_out
);
176 n_eq
= context_tab
->bset
->n_eq
+ n_out
;
177 n_ineq
= context_tab
->bset
->n_ineq
;
178 nparam
= tab
->n_param
;
179 total
= isl_map_dim(sol
->map
, isl_dim_all
);
180 bmap
= isl_basic_map_alloc_dim(isl_map_get_dim(sol
->map
),
181 tab
->n_div
, n_eq
, 2 * tab
->n_div
+ n_ineq
);
186 ISL_F_SET(bmap
, ISL_BASIC_MAP_RATIONAL
);
187 for (i
= 0; i
< context_tab
->bset
->n_div
; ++i
) {
188 int k
= isl_basic_map_alloc_div(bmap
);
191 isl_seq_cpy(bmap
->div
[k
],
192 context_tab
->bset
->div
[i
], 1 + 1 + nparam
);
193 isl_seq_clr(bmap
->div
[k
] + 1 + 1 + nparam
, total
- nparam
);
194 isl_seq_cpy(bmap
->div
[k
] + 1 + 1 + total
,
195 context_tab
->bset
->div
[i
] + 1 + 1 + nparam
, i
);
197 for (i
= 0; i
< context_tab
->bset
->n_eq
; ++i
) {
198 int k
= isl_basic_map_alloc_equality(bmap
);
201 isl_seq_cpy(bmap
->eq
[k
], context_tab
->bset
->eq
[i
], 1 + nparam
);
202 isl_seq_clr(bmap
->eq
[k
] + 1 + nparam
, total
- nparam
);
203 isl_seq_cpy(bmap
->eq
[k
] + 1 + total
,
204 context_tab
->bset
->eq
[i
] + 1 + nparam
, n_div
);
206 for (i
= 0; i
< context_tab
->bset
->n_ineq
; ++i
) {
207 int k
= isl_basic_map_alloc_inequality(bmap
);
210 isl_seq_cpy(bmap
->ineq
[k
],
211 context_tab
->bset
->ineq
[i
], 1 + nparam
);
212 isl_seq_clr(bmap
->ineq
[k
] + 1 + nparam
, total
- nparam
);
213 isl_seq_cpy(bmap
->ineq
[k
] + 1 + total
,
214 context_tab
->bset
->ineq
[i
] + 1 + nparam
, n_div
);
216 for (i
= tab
->n_param
; i
< total
; ++i
) {
217 int k
= isl_basic_map_alloc_equality(bmap
);
220 isl_seq_clr(bmap
->eq
[k
] + 1, isl_basic_map_total_dim(bmap
));
221 if (!tab
->var
[i
].is_row
) {
223 isl_assert(bmap
->ctx
, !tab
->M
, goto error
);
224 isl_int_set_si(bmap
->eq
[k
][0], 0);
226 isl_int_set_si(bmap
->eq
[k
][1 + i
], 1);
228 isl_int_set_si(bmap
->eq
[k
][1 + i
], -1);
231 row
= tab
->var
[i
].index
;
234 isl_assert(bmap
->ctx
,
235 isl_int_eq(tab
->mat
->row
[row
][2],
236 tab
->mat
->row
[row
][0]),
238 isl_int_set(bmap
->eq
[k
][0], tab
->mat
->row
[row
][1]);
239 for (j
= 0; j
< tab
->n_param
; ++j
) {
241 if (tab
->var
[j
].is_row
)
243 col
= tab
->var
[j
].index
;
244 isl_int_set(bmap
->eq
[k
][1 + j
],
245 tab
->mat
->row
[row
][off
+ col
]);
247 for (j
= 0; j
< tab
->n_div
; ++j
) {
249 if (tab
->var
[tab
->n_var
- tab
->n_div
+j
].is_row
)
251 col
= tab
->var
[tab
->n_var
- tab
->n_div
+j
].index
;
252 isl_int_set(bmap
->eq
[k
][1 + total
+ j
],
253 tab
->mat
->row
[row
][off
+ col
]);
256 isl_int_set(bmap
->eq
[k
][1 + i
],
257 tab
->mat
->row
[row
][0]);
259 isl_int_neg(bmap
->eq
[k
][1 + i
],
260 tab
->mat
->row
[row
][0]);
263 bmap
= isl_basic_map_gauss(bmap
, NULL
);
264 bmap
= isl_basic_map_normalize_constraints(bmap
);
265 bmap
= isl_basic_map_finalize(bmap
);
266 sol
->map
= isl_map_grow(sol
->map
, 1);
267 sol
->map
= isl_map_add(sol
->map
, bmap
);
272 isl_basic_map_free(bmap
);
277 static struct isl_sol
*sol_map_add_wrap(struct isl_sol
*sol
,
280 return (struct isl_sol
*)sol_map_add((struct isl_sol_map
*)sol
, tab
);
284 static struct isl_basic_set
*isl_basic_set_add_ineq(struct isl_basic_set
*bset
,
289 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
292 k
= isl_basic_set_alloc_inequality(bset
);
295 isl_seq_cpy(bset
->ineq
[k
], ineq
, 1 + isl_basic_set_total_dim(bset
));
298 isl_basic_set_free(bset
);
302 static struct isl_basic_set
*isl_basic_set_add_eq(struct isl_basic_set
*bset
,
307 bset
= isl_basic_set_extend_constraints(bset
, 1, 0);
310 k
= isl_basic_set_alloc_equality(bset
);
313 isl_seq_cpy(bset
->eq
[k
], eq
, 1 + isl_basic_set_total_dim(bset
));
316 isl_basic_set_free(bset
);
321 /* Store the "parametric constant" of row "row" of tableau "tab" in "line",
322 * i.e., the constant term and the coefficients of all variables that
323 * appear in the context tableau.
324 * Note that the coefficient of the big parameter M is NOT copied.
325 * The context tableau may not have a big parameter and even when it
326 * does, it is a different big parameter.
328 static void get_row_parameter_line(struct isl_tab
*tab
, int row
, isl_int
*line
)
331 unsigned off
= 2 + tab
->M
;
333 isl_int_set(line
[0], tab
->mat
->row
[row
][1]);
334 for (i
= 0; i
< tab
->n_param
; ++i
) {
335 if (tab
->var
[i
].is_row
)
336 isl_int_set_si(line
[1 + i
], 0);
338 int col
= tab
->var
[i
].index
;
339 isl_int_set(line
[1 + i
], tab
->mat
->row
[row
][off
+ col
]);
342 for (i
= 0; i
< tab
->n_div
; ++i
) {
343 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
344 isl_int_set_si(line
[1 + tab
->n_param
+ i
], 0);
346 int col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
347 isl_int_set(line
[1 + tab
->n_param
+ i
],
348 tab
->mat
->row
[row
][off
+ col
]);
353 /* Check if rows "row1" and "row2" have identical "parametric constants",
354 * as explained above.
355 * In this case, we also insist that the coefficients of the big parameter
356 * be the same as the values of the constants will only be the same
357 * if these coefficients are also the same.
359 static int identical_parameter_line(struct isl_tab
*tab
, int row1
, int row2
)
362 unsigned off
= 2 + tab
->M
;
364 if (isl_int_ne(tab
->mat
->row
[row1
][1], tab
->mat
->row
[row2
][1]))
367 if (tab
->M
&& isl_int_ne(tab
->mat
->row
[row1
][2],
368 tab
->mat
->row
[row2
][2]))
371 for (i
= 0; i
< tab
->n_param
+ tab
->n_div
; ++i
) {
372 int pos
= i
< tab
->n_param
? i
:
373 tab
->n_var
- tab
->n_div
+ i
- tab
->n_param
;
376 if (tab
->var
[pos
].is_row
)
378 col
= tab
->var
[pos
].index
;
379 if (isl_int_ne(tab
->mat
->row
[row1
][off
+ col
],
380 tab
->mat
->row
[row2
][off
+ col
]))
386 /* Return an inequality that expresses that the "parametric constant"
387 * should be non-negative.
388 * This function is only called when the coefficient of the big parameter
391 static struct isl_vec
*get_row_parameter_ineq(struct isl_tab
*tab
, int row
)
393 struct isl_vec
*ineq
;
395 ineq
= isl_vec_alloc(tab
->mat
->ctx
, 1 + tab
->n_param
+ tab
->n_div
);
399 get_row_parameter_line(tab
, row
, ineq
->el
);
401 isl_seq_normalize(ineq
->el
, ineq
->size
);
406 /* Return a integer division for use in a parametric cut based on the given row.
407 * In particular, let the parametric constant of the row be
411 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
412 * The div returned is equal to
414 * floor(\sum_i {-a_i} y_i) = floor((\sum_i (-a_i mod d) y_i)/d)
416 static struct isl_vec
*get_row_parameter_div(struct isl_tab
*tab
, int row
)
420 div
= isl_vec_alloc(tab
->mat
->ctx
, 1 + 1 + tab
->n_param
+ tab
->n_div
);
424 isl_int_set(div
->el
[0], tab
->mat
->row
[row
][0]);
425 get_row_parameter_line(tab
, row
, div
->el
+ 1);
426 isl_seq_normalize(div
->el
, div
->size
);
427 isl_seq_neg(div
->el
+ 1, div
->el
+ 1, div
->size
- 1);
428 isl_seq_fdiv_r(div
->el
+ 1, div
->el
+ 1, div
->el
[0], div
->size
- 1);
433 /* Return a integer division for use in transferring an integrality constraint
435 * In particular, let the parametric constant of the row be
439 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
440 * The the returned div is equal to
442 * floor(\sum_i {a_i} y_i) = floor((\sum_i (a_i mod d) y_i)/d)
444 static struct isl_vec
*get_row_split_div(struct isl_tab
*tab
, int row
)
448 div
= isl_vec_alloc(tab
->mat
->ctx
, 1 + 1 + tab
->n_param
+ tab
->n_div
);
452 isl_int_set(div
->el
[0], tab
->mat
->row
[row
][0]);
453 get_row_parameter_line(tab
, row
, div
->el
+ 1);
454 isl_seq_normalize(div
->el
, div
->size
);
455 isl_seq_fdiv_r(div
->el
+ 1, div
->el
+ 1, div
->el
[0], div
->size
- 1);
460 /* Construct and return an inequality that expresses an upper bound
462 * In particular, if the div is given by
466 * then the inequality expresses
470 static struct isl_vec
*ineq_for_div(struct isl_basic_set
*bset
, unsigned div
)
474 struct isl_vec
*ineq
;
476 total
= isl_basic_set_total_dim(bset
);
477 div_pos
= 1 + total
- bset
->n_div
+ div
;
479 ineq
= isl_vec_alloc(bset
->ctx
, 1 + total
);
483 isl_seq_cpy(ineq
->el
, bset
->div
[div
] + 1, 1 + total
);
484 isl_int_neg(ineq
->el
[div_pos
], bset
->div
[div
][0]);
488 /* Given a row in the tableau and a div that was created
489 * using get_row_split_div and that been constrained to equality, i.e.,
491 * d = floor(\sum_i {a_i} y_i) = \sum_i {a_i} y_i
493 * replace the expression "\sum_i {a_i} y_i" in the row by d,
494 * i.e., we subtract "\sum_i {a_i} y_i" and add 1 d.
495 * The coefficients of the non-parameters in the tableau have been
496 * verified to be integral. We can therefore simply replace coefficient b
497 * by floor(b). For the coefficients of the parameters we have
498 * floor(a_i) = a_i - {a_i}, while for the other coefficients, we have
501 static struct isl_tab
*set_row_cst_to_div(struct isl_tab
*tab
, int row
, int div
)
505 unsigned off
= 2 + tab
->M
;
507 isl_seq_fdiv_q(tab
->mat
->row
[row
] + 1, tab
->mat
->row
[row
] + 1,
508 tab
->mat
->row
[row
][0], 1 + tab
->M
+ tab
->n_col
);
510 isl_int_set_si(tab
->mat
->row
[row
][0], 1);
512 isl_assert(tab
->mat
->ctx
,
513 !tab
->var
[tab
->n_var
- tab
->n_div
+ div
].is_row
, goto error
);
515 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ div
].index
;
516 isl_int_set_si(tab
->mat
->row
[row
][off
+ col
], 1);
524 /* Check if the (parametric) constant of the given row is obviously
525 * negative, meaning that we don't need to consult the context tableau.
526 * If there is a big parameter and its coefficient is non-zero,
527 * then this coefficient determines the outcome.
528 * Otherwise, we check whether the constant is negative and
529 * all non-zero coefficients of parameters are negative and
530 * belong to non-negative parameters.
532 static int is_obviously_neg(struct isl_tab
*tab
, int row
)
536 unsigned off
= 2 + tab
->M
;
539 if (isl_int_is_pos(tab
->mat
->row
[row
][2]))
541 if (isl_int_is_neg(tab
->mat
->row
[row
][2]))
545 if (isl_int_is_nonneg(tab
->mat
->row
[row
][1]))
547 for (i
= 0; i
< tab
->n_param
; ++i
) {
548 /* Eliminated parameter */
549 if (tab
->var
[i
].is_row
)
551 col
= tab
->var
[i
].index
;
552 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
554 if (!tab
->var
[i
].is_nonneg
)
556 if (isl_int_is_pos(tab
->mat
->row
[row
][off
+ col
]))
559 for (i
= 0; i
< tab
->n_div
; ++i
) {
560 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
562 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
563 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
565 if (!tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_nonneg
)
567 if (isl_int_is_pos(tab
->mat
->row
[row
][off
+ col
]))
573 /* Check if the (parametric) constant of the given row is obviously
574 * non-negative, meaning that we don't need to consult the context tableau.
575 * If there is a big parameter and its coefficient is non-zero,
576 * then this coefficient determines the outcome.
577 * Otherwise, we check whether the constant is non-negative and
578 * all non-zero coefficients of parameters are positive and
579 * belong to non-negative parameters.
581 static int is_obviously_nonneg(struct isl_tab
*tab
, int row
)
585 unsigned off
= 2 + tab
->M
;
588 if (isl_int_is_pos(tab
->mat
->row
[row
][2]))
590 if (isl_int_is_neg(tab
->mat
->row
[row
][2]))
594 if (isl_int_is_neg(tab
->mat
->row
[row
][1]))
596 for (i
= 0; i
< tab
->n_param
; ++i
) {
597 /* Eliminated parameter */
598 if (tab
->var
[i
].is_row
)
600 col
= tab
->var
[i
].index
;
601 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
603 if (!tab
->var
[i
].is_nonneg
)
605 if (isl_int_is_neg(tab
->mat
->row
[row
][off
+ col
]))
608 for (i
= 0; i
< tab
->n_div
; ++i
) {
609 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
611 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
612 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
614 if (!tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_nonneg
)
616 if (isl_int_is_neg(tab
->mat
->row
[row
][off
+ col
]))
622 /* Given a row r and two columns, return the column that would
623 * lead to the lexicographically smallest increment in the sample
624 * solution when leaving the basis in favor of the row.
625 * Pivoting with column c will increment the sample value by a non-negative
626 * constant times a_{V,c}/a_{r,c}, with a_{V,c} the elements of column c
627 * corresponding to the non-parametric variables.
628 * If variable v appears in a column c_v, the a_{v,c} = 1 iff c = c_v,
629 * with all other entries in this virtual row equal to zero.
630 * If variable v appears in a row, then a_{v,c} is the element in column c
633 * Let v be the first variable with a_{v,c1}/a_{r,c1} != a_{v,c2}/a_{r,c2}.
634 * Then if a_{v,c1}/a_{r,c1} < a_{v,c2}/a_{r,c2}, i.e.,
635 * a_{v,c2} a_{r,c1} - a_{v,c1} a_{r,c2} > 0, c1 results in the minimal
636 * increment. Otherwise, it's c2.
638 static int lexmin_col_pair(struct isl_tab
*tab
,
639 int row
, int col1
, int col2
, isl_int tmp
)
644 tr
= tab
->mat
->row
[row
] + 2 + tab
->M
;
646 for (i
= tab
->n_param
; i
< tab
->n_var
- tab
->n_div
; ++i
) {
650 if (!tab
->var
[i
].is_row
) {
651 if (tab
->var
[i
].index
== col1
)
653 if (tab
->var
[i
].index
== col2
)
658 if (tab
->var
[i
].index
== row
)
661 r
= tab
->mat
->row
[tab
->var
[i
].index
] + 2 + tab
->M
;
662 s1
= isl_int_sgn(r
[col1
]);
663 s2
= isl_int_sgn(r
[col2
]);
664 if (s1
== 0 && s2
== 0)
671 isl_int_mul(tmp
, r
[col2
], tr
[col1
]);
672 isl_int_submul(tmp
, r
[col1
], tr
[col2
]);
673 if (isl_int_is_pos(tmp
))
675 if (isl_int_is_neg(tmp
))
681 /* Given a row in the tableau, find and return the column that would
682 * result in the lexicographically smallest, but positive, increment
683 * in the sample point.
684 * If there is no such column, then return tab->n_col.
685 * If anything goes wrong, return -1.
687 static int lexmin_pivot_col(struct isl_tab
*tab
, int row
)
690 int col
= tab
->n_col
;
694 tr
= tab
->mat
->row
[row
] + 2 + tab
->M
;
698 for (j
= tab
->n_dead
; j
< tab
->n_col
; ++j
) {
699 if (tab
->col_var
[j
] >= 0 &&
700 (tab
->col_var
[j
] < tab
->n_param
||
701 tab
->col_var
[j
] >= tab
->n_var
- tab
->n_div
))
704 if (!isl_int_is_pos(tr
[j
]))
707 if (col
== tab
->n_col
)
710 col
= lexmin_col_pair(tab
, row
, col
, j
, tmp
);
711 isl_assert(tab
->mat
->ctx
, col
>= 0, goto error
);
721 /* Return the first known violated constraint, i.e., a non-negative
722 * contraint that currently has an either obviously negative value
723 * or a previously determined to be negative value.
725 * If any constraint has a negative coefficient for the big parameter,
726 * if any, then we return one of these first.
728 static int first_neg(struct isl_tab
*tab
)
733 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
734 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
736 if (isl_int_is_neg(tab
->mat
->row
[row
][2]))
739 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
740 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
743 if (tab
->row_sign
[row
] == 0 &&
744 is_obviously_neg(tab
, row
))
745 tab
->row_sign
[row
] = isl_tab_row_neg
;
746 if (tab
->row_sign
[row
] != isl_tab_row_neg
)
748 } else if (!is_obviously_neg(tab
, row
))
755 /* Resolve all known or obviously violated constraints through pivoting.
756 * In particular, as long as we can find any violated constraint, we
757 * look for a pivoting column that would result in the lexicographicallly
758 * smallest increment in the sample point. If there is no such column
759 * then the tableau is infeasible.
761 static struct isl_tab
*restore_lexmin(struct isl_tab
*tab
)
769 while ((row
= first_neg(tab
)) != -1) {
770 col
= lexmin_pivot_col(tab
, row
);
771 if (col
>= tab
->n_col
)
772 return isl_tab_mark_empty(tab
);
775 isl_tab_pivot(tab
, row
, col
);
783 /* Given a row that represents an equality, look for an appropriate
785 * In particular, if there are any non-zero coefficients among
786 * the non-parameter variables, then we take the last of these
787 * variables. Eliminating this variable in terms of the other
788 * variables and/or parameters does not influence the property
789 * that all column in the initial tableau are lexicographically
790 * positive. The row corresponding to the eliminated variable
791 * will only have non-zero entries below the diagonal of the
792 * initial tableau. That is, we transform
798 * If there is no such non-parameter variable, then we are dealing with
799 * pure parameter equality and we pick any parameter with coefficient 1 or -1
800 * for elimination. This will ensure that the eliminated parameter
801 * always has an integer value whenever all the other parameters are integral.
802 * If there is no such parameter then we return -1.
804 static int last_var_col_or_int_par_col(struct isl_tab
*tab
, int row
)
806 unsigned off
= 2 + tab
->M
;
809 for (i
= tab
->n_var
- tab
->n_div
- 1; i
>= 0 && i
>= tab
->n_param
; --i
) {
811 if (tab
->var
[i
].is_row
)
813 col
= tab
->var
[i
].index
;
814 if (col
<= tab
->n_dead
)
816 if (!isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
819 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
820 if (isl_int_is_one(tab
->mat
->row
[row
][off
+ i
]))
822 if (isl_int_is_negone(tab
->mat
->row
[row
][off
+ i
]))
828 /* Add an equality that is known to be valid to the tableau.
829 * We first check if we can eliminate a variable or a parameter.
830 * If not, we add the equality as two inequalities.
831 * In this case, the equality was a pure parameter equality and there
832 * is no need to resolve any constraint violations.
834 static struct isl_tab
*add_lexmin_valid_eq(struct isl_tab
*tab
, isl_int
*eq
)
841 r
= isl_tab_add_row(tab
, eq
);
845 r
= tab
->con
[r
].index
;
846 i
= last_var_col_or_int_par_col(tab
, r
);
848 tab
->con
[r
].is_nonneg
= 1;
849 isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]);
850 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
851 r
= isl_tab_add_row(tab
, eq
);
854 tab
->con
[r
].is_nonneg
= 1;
855 isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]);
857 isl_tab_pivot(tab
, r
, i
);
858 isl_tab_kill_col(tab
, i
);
861 tab
= restore_lexmin(tab
);
870 /* Check if the given row is a pure constant.
872 static int is_constant(struct isl_tab
*tab
, int row
)
874 unsigned off
= 2 + tab
->M
;
876 return isl_seq_first_non_zero(tab
->mat
->row
[row
] + off
+ tab
->n_dead
,
877 tab
->n_col
- tab
->n_dead
) == -1;
880 /* Add an equality that may or may not be valid to the tableau.
881 * If the resulting row is a pure constant, then it must be zero.
882 * Otherwise, the resulting tableau is empty.
884 * If the row is not a pure constant, then we add two inequalities,
885 * each time checking that they can be satisfied.
886 * In the end we try to use one of the two constraints to eliminate
889 static struct isl_tab
*add_lexmin_eq(struct isl_tab
*tab
, isl_int
*eq
)
898 tab
->bset
= isl_basic_set_add_eq(tab
->bset
, eq
);
899 isl_tab_push(tab
, isl_tab_undo_bset_eq
);
903 r1
= isl_tab_add_row(tab
, eq
);
906 tab
->con
[r1
].is_nonneg
= 1;
907 isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r1
]);
909 row
= tab
->con
[r1
].index
;
910 if (is_constant(tab
, row
)) {
911 if (!isl_int_is_zero(tab
->mat
->row
[row
][1]) ||
912 (tab
->M
&& !isl_int_is_zero(tab
->mat
->row
[row
][2])))
913 return isl_tab_mark_empty(tab
);
917 tab
= restore_lexmin(tab
);
918 if (!tab
|| tab
->empty
)
921 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
923 r2
= isl_tab_add_row(tab
, eq
);
926 tab
->con
[r2
].is_nonneg
= 1;
927 isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r2
]);
929 tab
= restore_lexmin(tab
);
930 if (!tab
|| tab
->empty
)
933 if (!tab
->con
[r1
].is_row
)
934 isl_tab_kill_col(tab
, tab
->con
[r1
].index
);
935 else if (!tab
->con
[r2
].is_row
)
936 isl_tab_kill_col(tab
, tab
->con
[r2
].index
);
937 else if (isl_int_is_zero(tab
->mat
->row
[tab
->con
[r1
].index
][1])) {
938 unsigned off
= 2 + tab
->M
;
940 int row
= tab
->con
[r1
].index
;
941 i
= isl_seq_first_non_zero(tab
->mat
->row
[row
]+off
+tab
->n_dead
,
942 tab
->n_col
- tab
->n_dead
);
944 isl_tab_pivot(tab
, row
, tab
->n_dead
+ i
);
945 isl_tab_kill_col(tab
, tab
->n_dead
+ i
);
955 /* Add an inequality to the tableau, resolving violations using
958 static struct isl_tab
*add_lexmin_ineq(struct isl_tab
*tab
, isl_int
*ineq
)
966 tab
->bset
= isl_basic_set_add_ineq(tab
->bset
, ineq
);
967 isl_tab_push(tab
, isl_tab_undo_bset_ineq
);
971 r
= isl_tab_add_row(tab
, ineq
);
974 tab
->con
[r
].is_nonneg
= 1;
975 isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]);
976 if (isl_tab_row_is_redundant(tab
, tab
->con
[r
].index
)) {
977 isl_tab_mark_redundant(tab
, tab
->con
[r
].index
);
981 tab
= restore_lexmin(tab
);
982 if (tab
&& !tab
->empty
&& tab
->con
[r
].is_row
&&
983 isl_tab_row_is_redundant(tab
, tab
->con
[r
].index
))
984 isl_tab_mark_redundant(tab
, tab
->con
[r
].index
);
991 /* Check if the coefficients of the parameters are all integral.
993 static int integer_parameter(struct isl_tab
*tab
, int row
)
997 unsigned off
= 2 + tab
->M
;
999 for (i
= 0; i
< tab
->n_param
; ++i
) {
1000 /* Eliminated parameter */
1001 if (tab
->var
[i
].is_row
)
1003 col
= tab
->var
[i
].index
;
1004 if (!isl_int_is_divisible_by(tab
->mat
->row
[row
][off
+ col
],
1005 tab
->mat
->row
[row
][0]))
1008 for (i
= 0; i
< tab
->n_div
; ++i
) {
1009 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
1011 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
1012 if (!isl_int_is_divisible_by(tab
->mat
->row
[row
][off
+ col
],
1013 tab
->mat
->row
[row
][0]))
1019 /* Check if the coefficients of the non-parameter variables are all integral.
1021 static int integer_variable(struct isl_tab
*tab
, int row
)
1024 unsigned off
= 2 + tab
->M
;
1026 for (i
= 0; i
< tab
->n_col
; ++i
) {
1027 if (tab
->col_var
[i
] >= 0 &&
1028 (tab
->col_var
[i
] < tab
->n_param
||
1029 tab
->col_var
[i
] >= tab
->n_var
- tab
->n_div
))
1031 if (!isl_int_is_divisible_by(tab
->mat
->row
[row
][off
+ i
],
1032 tab
->mat
->row
[row
][0]))
1038 /* Check if the constant term is integral.
1040 static int integer_constant(struct isl_tab
*tab
, int row
)
1042 return isl_int_is_divisible_by(tab
->mat
->row
[row
][1],
1043 tab
->mat
->row
[row
][0]);
1046 #define I_CST 1 << 0
1047 #define I_PAR 1 << 1
1048 #define I_VAR 1 << 2
1050 /* Check for first (non-parameter) variable that is non-integer and
1051 * therefore requires a cut.
1052 * For parametric tableaus, there are three parts in a row,
1053 * the constant, the coefficients of the parameters and the rest.
1054 * For each part, we check whether the coefficients in that part
1055 * are all integral and if so, set the corresponding flag in *f.
1056 * If the constant and the parameter part are integral, then the
1057 * current sample value is integral and no cut is required
1058 * (irrespective of whether the variable part is integral).
1060 static int first_non_integer(struct isl_tab
*tab
, int *f
)
1064 for (i
= tab
->n_param
; i
< tab
->n_var
- tab
->n_div
; ++i
) {
1067 if (!tab
->var
[i
].is_row
)
1069 row
= tab
->var
[i
].index
;
1070 if (integer_constant(tab
, row
))
1071 ISL_FL_SET(flags
, I_CST
);
1072 if (integer_parameter(tab
, row
))
1073 ISL_FL_SET(flags
, I_PAR
);
1074 if (ISL_FL_ISSET(flags
, I_CST
) && ISL_FL_ISSET(flags
, I_PAR
))
1076 if (integer_variable(tab
, row
))
1077 ISL_FL_SET(flags
, I_VAR
);
1084 /* Add a (non-parametric) cut to cut away the non-integral sample
1085 * value of the given row.
1087 * If the row is given by
1089 * m r = f + \sum_i a_i y_i
1093 * c = - {-f/m} + \sum_i {a_i/m} y_i >= 0
1095 * The big parameter, if any, is ignored, since it is assumed to be big
1096 * enough to be divisible by any integer.
1097 * If the tableau is actually a parametric tableau, then this function
1098 * is only called when all coefficients of the parameters are integral.
1099 * The cut therefore has zero coefficients for the parameters.
1101 * The current value is known to be negative, so row_sign, if it
1102 * exists, is set accordingly.
1104 * Return the row of the cut or -1.
1106 static int add_cut(struct isl_tab
*tab
, int row
)
1111 unsigned off
= 2 + tab
->M
;
1113 if (isl_tab_extend_cons(tab
, 1) < 0)
1115 r
= isl_tab_allocate_con(tab
);
1119 r_row
= tab
->mat
->row
[tab
->con
[r
].index
];
1120 isl_int_set(r_row
[0], tab
->mat
->row
[row
][0]);
1121 isl_int_neg(r_row
[1], tab
->mat
->row
[row
][1]);
1122 isl_int_fdiv_r(r_row
[1], r_row
[1], tab
->mat
->row
[row
][0]);
1123 isl_int_neg(r_row
[1], r_row
[1]);
1125 isl_int_set_si(r_row
[2], 0);
1126 for (i
= 0; i
< tab
->n_col
; ++i
)
1127 isl_int_fdiv_r(r_row
[off
+ i
],
1128 tab
->mat
->row
[row
][off
+ i
], tab
->mat
->row
[row
][0]);
1130 tab
->con
[r
].is_nonneg
= 1;
1131 isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]);
1133 tab
->row_sign
[tab
->con
[r
].index
] = isl_tab_row_neg
;
1135 return tab
->con
[r
].index
;
1138 /* Given a non-parametric tableau, add cuts until an integer
1139 * sample point is obtained or until the tableau is determined
1140 * to be integer infeasible.
1141 * As long as there is any non-integer value in the sample point,
1142 * we add an appropriate cut, if possible and resolve the violated
1143 * cut constraint using restore_lexmin.
1144 * If one of the corresponding rows is equal to an integral
1145 * combination of variables/constraints plus a non-integral constant,
1146 * then there is no way to obtain an integer point an we return
1147 * a tableau that is marked empty.
1149 static struct isl_tab
*cut_to_integer_lexmin(struct isl_tab
*tab
)
1159 while ((row
= first_non_integer(tab
, &flags
)) != -1) {
1160 if (ISL_FL_ISSET(flags
, I_VAR
))
1161 return isl_tab_mark_empty(tab
);
1162 row
= add_cut(tab
, row
);
1165 tab
= restore_lexmin(tab
);
1166 if (!tab
|| tab
->empty
)
1175 static struct isl_tab
*drop_sample(struct isl_tab
*tab
, int s
)
1177 if (s
!= tab
->n_outside
)
1178 isl_mat_swap_rows(tab
->samples
, tab
->n_outside
, s
);
1180 isl_tab_push(tab
, isl_tab_undo_drop_sample
);
1185 /* Check whether all the currently active samples also satisfy the inequality
1186 * "ineq" (treated as an equality if eq is set).
1187 * Remove those samples that do not.
1189 static struct isl_tab
*check_samples(struct isl_tab
*tab
, isl_int
*ineq
, int eq
)
1197 isl_assert(tab
->mat
->ctx
, tab
->bset
, goto error
);
1198 isl_assert(tab
->mat
->ctx
, tab
->samples
, goto error
);
1199 isl_assert(tab
->mat
->ctx
, tab
->samples
->n_col
== 1 + tab
->n_var
, goto error
);
1202 for (i
= tab
->n_outside
; i
< tab
->n_sample
; ++i
) {
1204 isl_seq_inner_product(ineq
, tab
->samples
->row
[i
],
1205 1 + tab
->n_var
, &v
);
1206 sgn
= isl_int_sgn(v
);
1207 if (eq
? (sgn
== 0) : (sgn
>= 0))
1209 tab
= drop_sample(tab
, i
);
1218 /* Check whether the sample value of the tableau is finite,
1219 * i.e., either the tableau does not use a big parameter, or
1220 * all values of the variables are equal to the big parameter plus
1221 * some constant. This constant is the actual sample value.
1223 int sample_is_finite(struct isl_tab
*tab
)
1230 for (i
= 0; i
< tab
->n_var
; ++i
) {
1232 if (!tab
->var
[i
].is_row
)
1234 row
= tab
->var
[i
].index
;
1235 if (isl_int_ne(tab
->mat
->row
[row
][0], tab
->mat
->row
[row
][2]))
1241 /* Move to an integer point in the tableau and if such a point can be found
1242 * and if moreover it is finite, then add it to the list of sample values.
1243 * As a side effect, the tableau will be marked empty if no integer point
1246 * This function is only called when none of the currently active sample
1247 * values satisfies the most recently added constraint.
1249 static struct isl_tab
*check_integer_feasible(struct isl_tab
*tab
)
1251 struct isl_tab_undo
*snap
;
1256 snap
= isl_tab_snap(tab
);
1257 isl_tab_push_basis(tab
);
1259 tab
= cut_to_integer_lexmin(tab
);
1261 if (tab
&& !tab
->empty
&& sample_is_finite(tab
)) {
1262 struct isl_vec
*sample
;
1264 tab
->samples
= isl_mat_extend(tab
->samples
,
1265 tab
->n_sample
+ 1, tab
->samples
->n_col
);
1269 sample
= isl_tab_get_sample_value(tab
);
1272 isl_seq_cpy(tab
->samples
->row
[tab
->n_sample
],
1273 sample
->el
, sample
->size
);
1274 isl_vec_free(sample
);
1278 if (isl_tab_rollback(tab
, snap
) < 0)
1287 /* First check if any of the currently active sample values satisfies
1288 * the inequality "ineq" (an equality if eq is set).
1289 * If not, continue with check_integer_feasible.
1291 static struct isl_tab
*check_sample_or_integer_feasible(struct isl_tab
*tab
,
1292 isl_int
*ineq
, int eq
)
1300 isl_assert(tab
->mat
->ctx
, tab
->bset
, goto error
);
1301 isl_assert(tab
->mat
->ctx
, tab
->samples
, goto error
);
1302 isl_assert(tab
->mat
->ctx
, tab
->samples
->n_col
== 1 + tab
->n_var
, goto error
);
1305 for (i
= tab
->n_outside
; i
< tab
->n_sample
; ++i
) {
1307 isl_seq_inner_product(ineq
, tab
->samples
->row
[i
],
1308 1 + tab
->n_var
, &v
);
1309 sgn
= isl_int_sgn(v
);
1310 if (eq
? (sgn
== 0) : (sgn
>= 0))
1315 if (i
< tab
->n_sample
)
1318 return check_integer_feasible(tab
);
1321 /* For a div d = floor(f/m), add the constraints
1324 * -(f-(m-1)) + m d >= 0
1326 * Note that the second constraint is the negation of
1330 static struct isl_tab
*add_div_constraints(struct isl_tab
*tab
, unsigned div
)
1335 struct isl_vec
*ineq
;
1340 total
= isl_basic_set_total_dim(tab
->bset
);
1341 div_pos
= 1 + total
- tab
->bset
->n_div
+ div
;
1343 ineq
= ineq_for_div(tab
->bset
, div
);
1347 tab
= add_lexmin_ineq(tab
, ineq
->el
);
1349 isl_seq_neg(ineq
->el
, tab
->bset
->div
[div
] + 1, 1 + total
);
1350 isl_int_set(ineq
->el
[div_pos
], tab
->bset
->div
[div
][0]);
1351 isl_int_add(ineq
->el
[0], ineq
->el
[0], ineq
->el
[div_pos
]);
1352 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
1353 tab
= add_lexmin_ineq(tab
, ineq
->el
);
1363 /* Add a div specified by "div" to both the main tableau and
1364 * the context tableau. In case of the main tableau, we only
1365 * need to add an extra div. In the context tableau, we also
1366 * need to express the meaning of the div.
1367 * Return the index of the div or -1 if anything went wrong.
1369 static int add_div(struct isl_tab
*tab
, struct isl_tab
**context_tab
,
1370 struct isl_vec
*div
)
1375 struct isl_mat
*samples
;
1377 if (isl_tab_extend_vars(*context_tab
, 1) < 0)
1379 r
= isl_tab_allocate_var(*context_tab
);
1382 (*context_tab
)->var
[r
].is_nonneg
= 1;
1383 (*context_tab
)->var
[r
].frozen
= 1;
1385 samples
= isl_mat_extend((*context_tab
)->samples
,
1386 (*context_tab
)->n_sample
, 1 + (*context_tab
)->n_var
);
1387 (*context_tab
)->samples
= samples
;
1390 for (i
= (*context_tab
)->n_outside
; i
< samples
->n_row
; ++i
) {
1391 isl_seq_inner_product(div
->el
+ 1, samples
->row
[i
],
1392 div
->size
- 1, &samples
->row
[i
][samples
->n_col
- 1]);
1393 isl_int_fdiv_q(samples
->row
[i
][samples
->n_col
- 1],
1394 samples
->row
[i
][samples
->n_col
- 1], div
->el
[0]);
1397 (*context_tab
)->bset
= isl_basic_set_extend_dim((*context_tab
)->bset
,
1398 isl_basic_set_get_dim((*context_tab
)->bset
), 1, 0, 2);
1399 k
= isl_basic_set_alloc_div((*context_tab
)->bset
);
1402 isl_seq_cpy((*context_tab
)->bset
->div
[k
], div
->el
, div
->size
);
1403 isl_tab_push((*context_tab
), isl_tab_undo_bset_div
);
1404 *context_tab
= add_div_constraints(*context_tab
, k
);
1408 if (isl_tab_extend_vars(tab
, 1) < 0)
1410 r
= isl_tab_allocate_var(tab
);
1413 if (!(*context_tab
)->M
)
1414 tab
->var
[r
].is_nonneg
= 1;
1415 tab
->var
[r
].frozen
= 1;
1418 return tab
->n_div
- 1;
1420 isl_tab_free(*context_tab
);
1421 *context_tab
= NULL
;
1425 static int find_div(struct isl_tab
*tab
, isl_int
*div
, isl_int denom
)
1428 unsigned total
= isl_basic_set_total_dim(tab
->bset
);
1430 for (i
= 0; i
< tab
->bset
->n_div
; ++i
) {
1431 if (isl_int_ne(tab
->bset
->div
[i
][0], denom
))
1433 if (!isl_seq_eq(tab
->bset
->div
[i
] + 1, div
, total
))
1440 /* Return the index of a div that corresponds to "div".
1441 * We first check if we already have such a div and if not, we create one.
1443 static int get_div(struct isl_tab
*tab
, struct isl_tab
**context_tab
,
1444 struct isl_vec
*div
)
1448 d
= find_div(*context_tab
, div
->el
+ 1, div
->el
[0]);
1452 return add_div(tab
, context_tab
, div
);
1455 /* Add a parametric cut to cut away the non-integral sample value
1457 * Let a_i be the coefficients of the constant term and the parameters
1458 * and let b_i be the coefficients of the variables or constraints
1459 * in basis of the tableau.
1460 * Let q be the div q = floor(\sum_i {-a_i} y_i).
1462 * The cut is expressed as
1464 * c = \sum_i -{-a_i} y_i + \sum_i {b_i} x_i + q >= 0
1466 * If q did not already exist in the context tableau, then it is added first.
1467 * If q is in a column of the main tableau then the "+ q" can be accomplished
1468 * by setting the corresponding entry to the denominator of the constraint.
1469 * If q happens to be in a row of the main tableau, then the corresponding
1470 * row needs to be added instead (taking care of the denominators).
1471 * Note that this is very unlikely, but perhaps not entirely impossible.
1473 * The current value of the cut is known to be negative (or at least
1474 * non-positive), so row_sign is set accordingly.
1476 * Return the row of the cut or -1.
1478 static int add_parametric_cut(struct isl_tab
*tab
, int row
,
1479 struct isl_tab
**context_tab
)
1481 struct isl_vec
*div
;
1487 unsigned off
= 2 + tab
->M
;
1492 if (isl_tab_extend_cons(*context_tab
, 3) < 0)
1495 div
= get_row_parameter_div(tab
, row
);
1499 d
= get_div(tab
, context_tab
, div
);
1503 if (isl_tab_extend_cons(tab
, 1) < 0)
1505 r
= isl_tab_allocate_con(tab
);
1509 r_row
= tab
->mat
->row
[tab
->con
[r
].index
];
1510 isl_int_set(r_row
[0], tab
->mat
->row
[row
][0]);
1511 isl_int_neg(r_row
[1], tab
->mat
->row
[row
][1]);
1512 isl_int_fdiv_r(r_row
[1], r_row
[1], tab
->mat
->row
[row
][0]);
1513 isl_int_neg(r_row
[1], r_row
[1]);
1515 isl_int_set_si(r_row
[2], 0);
1516 for (i
= 0; i
< tab
->n_param
; ++i
) {
1517 if (tab
->var
[i
].is_row
)
1519 col
= tab
->var
[i
].index
;
1520 isl_int_neg(r_row
[off
+ col
], tab
->mat
->row
[row
][off
+ col
]);
1521 isl_int_fdiv_r(r_row
[off
+ col
], r_row
[off
+ col
],
1522 tab
->mat
->row
[row
][0]);
1523 isl_int_neg(r_row
[off
+ col
], r_row
[off
+ col
]);
1525 for (i
= 0; i
< tab
->n_div
; ++i
) {
1526 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
1528 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
1529 isl_int_neg(r_row
[off
+ col
], tab
->mat
->row
[row
][off
+ col
]);
1530 isl_int_fdiv_r(r_row
[off
+ col
], r_row
[off
+ col
],
1531 tab
->mat
->row
[row
][0]);
1532 isl_int_neg(r_row
[off
+ col
], r_row
[off
+ col
]);
1534 for (i
= 0; i
< tab
->n_col
; ++i
) {
1535 if (tab
->col_var
[i
] >= 0 &&
1536 (tab
->col_var
[i
] < tab
->n_param
||
1537 tab
->col_var
[i
] >= tab
->n_var
- tab
->n_div
))
1539 isl_int_fdiv_r(r_row
[off
+ i
],
1540 tab
->mat
->row
[row
][off
+ i
], tab
->mat
->row
[row
][0]);
1542 if (tab
->var
[tab
->n_var
- tab
->n_div
+ d
].is_row
) {
1544 int d_row
= tab
->var
[tab
->n_var
- tab
->n_div
+ d
].index
;
1546 isl_int_gcd(gcd
, tab
->mat
->row
[d_row
][0], r_row
[0]);
1547 isl_int_divexact(r_row
[0], r_row
[0], gcd
);
1548 isl_int_divexact(gcd
, tab
->mat
->row
[d_row
][0], gcd
);
1549 isl_seq_combine(r_row
+ 1, gcd
, r_row
+ 1,
1550 r_row
[0], tab
->mat
->row
[d_row
] + 1,
1551 off
- 1 + tab
->n_col
);
1552 isl_int_mul(r_row
[0], r_row
[0], tab
->mat
->row
[d_row
][0]);
1555 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ d
].index
;
1556 isl_int_set(r_row
[off
+ col
], tab
->mat
->row
[row
][0]);
1559 tab
->con
[r
].is_nonneg
= 1;
1560 isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]);
1562 tab
->row_sign
[tab
->con
[r
].index
] = isl_tab_row_neg
;
1566 return tab
->con
[r
].index
;
1568 isl_tab_free(*context_tab
);
1569 *context_tab
= NULL
;
1573 /* Construct a tableau for bmap that can be used for computing
1574 * the lexicographic minimum (or maximum) of bmap.
1575 * If not NULL, then dom is the domain where the minimum
1576 * should be computed. In this case, we set up a parametric
1577 * tableau with row signs (initialized to "unknown").
1578 * If M is set, then the tableau will use a big parameter.
1579 * If max is set, then a maximum should be computed instead of a minimum.
1580 * This means that for each variable x, the tableau will contain the variable
1581 * x' = M - x, rather than x' = M + x. This in turn means that the coefficient
1582 * of the variables in all constraints are negated prior to adding them
1585 static struct isl_tab
*tab_for_lexmin(struct isl_basic_map
*bmap
,
1586 struct isl_basic_set
*dom
, unsigned M
, int max
)
1589 struct isl_tab
*tab
;
1591 tab
= isl_tab_alloc(bmap
->ctx
, 2 * bmap
->n_eq
+ bmap
->n_ineq
+ 1,
1592 isl_basic_map_total_dim(bmap
), M
);
1596 tab
->rational
= ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
1598 tab
->n_param
= isl_basic_set_total_dim(dom
) - dom
->n_div
;
1599 tab
->n_div
= dom
->n_div
;
1600 tab
->row_sign
= isl_calloc_array(bmap
->ctx
,
1601 enum isl_tab_row_sign
, tab
->mat
->n_row
);
1605 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
1606 return isl_tab_mark_empty(tab
);
1608 for (i
= tab
->n_param
; i
< tab
->n_var
- tab
->n_div
; ++i
) {
1609 tab
->var
[i
].is_nonneg
= 1;
1610 tab
->var
[i
].frozen
= 1;
1612 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1614 isl_seq_neg(bmap
->eq
[i
] + 1 + tab
->n_param
,
1615 bmap
->eq
[i
] + 1 + tab
->n_param
,
1616 tab
->n_var
- tab
->n_param
- tab
->n_div
);
1617 tab
= add_lexmin_valid_eq(tab
, bmap
->eq
[i
]);
1619 isl_seq_neg(bmap
->eq
[i
] + 1 + tab
->n_param
,
1620 bmap
->eq
[i
] + 1 + tab
->n_param
,
1621 tab
->n_var
- tab
->n_param
- tab
->n_div
);
1622 if (!tab
|| tab
->empty
)
1625 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
1627 isl_seq_neg(bmap
->ineq
[i
] + 1 + tab
->n_param
,
1628 bmap
->ineq
[i
] + 1 + tab
->n_param
,
1629 tab
->n_var
- tab
->n_param
- tab
->n_div
);
1630 tab
= add_lexmin_ineq(tab
, bmap
->ineq
[i
]);
1632 isl_seq_neg(bmap
->ineq
[i
] + 1 + tab
->n_param
,
1633 bmap
->ineq
[i
] + 1 + tab
->n_param
,
1634 tab
->n_var
- tab
->n_param
- tab
->n_div
);
1635 if (!tab
|| tab
->empty
)
1644 static struct isl_tab
*context_tab_for_lexmin(struct isl_basic_set
*bset
)
1646 struct isl_tab
*tab
;
1648 bset
= isl_basic_set_cow(bset
);
1651 tab
= tab_for_lexmin((struct isl_basic_map
*)bset
, NULL
, 1, 0);
1657 tab
->samples
= isl_mat_alloc(bset
->ctx
, 1, 1 + tab
->n_var
);
1662 isl_basic_set_free(bset
);
1666 /* Construct an isl_sol_map structure for accumulating the solution.
1667 * If track_empty is set, then we also keep track of the parts
1668 * of the context where there is no solution.
1669 * If max is set, then we are solving a maximization, rather than
1670 * a minimization problem, which means that the variables in the
1671 * tableau have value "M - x" rather than "M + x".
1673 static struct isl_sol_map
*sol_map_init(struct isl_basic_map
*bmap
,
1674 struct isl_basic_set
*dom
, int track_empty
, int max
)
1676 struct isl_sol_map
*sol_map
;
1677 struct isl_tab
*context_tab
;
1679 sol_map
= isl_calloc_type(bset
->ctx
, struct isl_sol_map
);
1684 sol_map
->sol
.add
= &sol_map_add_wrap
;
1685 sol_map
->sol
.free
= &sol_map_free_wrap
;
1686 sol_map
->map
= isl_map_alloc_dim(isl_basic_map_get_dim(bmap
), 1,
1691 context_tab
= context_tab_for_lexmin(isl_basic_set_copy(dom
));
1692 context_tab
= restore_lexmin(context_tab
);
1693 context_tab
= check_integer_feasible(context_tab
);
1696 sol_map
->sol
.context_tab
= context_tab
;
1699 sol_map
->empty
= isl_set_alloc_dim(isl_basic_set_get_dim(dom
),
1700 1, ISL_SET_DISJOINT
);
1701 if (!sol_map
->empty
)
1705 isl_basic_set_free(dom
);
1708 isl_basic_set_free(dom
);
1709 sol_map_free(sol_map
);
1713 /* For each variable in the context tableau, check if the variable can
1714 * only attain non-negative values. If so, mark the parameter as non-negative
1715 * in the main tableau. This allows for a more direct identification of some
1716 * cases of violated constraints.
1718 static struct isl_tab
*tab_detect_nonnegative_parameters(struct isl_tab
*tab
,
1719 struct isl_tab
*context_tab
)
1722 struct isl_tab_undo
*snap
, *snap2
;
1723 struct isl_vec
*ineq
= NULL
;
1724 struct isl_tab_var
*var
;
1727 if (context_tab
->n_var
== 0)
1730 ineq
= isl_vec_alloc(tab
->mat
->ctx
, 1 + context_tab
->n_var
);
1734 if (isl_tab_extend_cons(context_tab
, 1) < 0)
1737 snap
= isl_tab_snap(context_tab
);
1738 isl_tab_push_basis(context_tab
);
1740 snap2
= isl_tab_snap(context_tab
);
1743 isl_seq_clr(ineq
->el
, ineq
->size
);
1744 for (i
= 0; i
< context_tab
->n_var
; ++i
) {
1745 isl_int_set_si(ineq
->el
[1 + i
], 1);
1746 context_tab
= isl_tab_add_ineq(context_tab
, ineq
->el
);
1747 var
= &context_tab
->con
[context_tab
->n_con
- 1];
1748 if (!context_tab
->empty
&&
1749 !isl_tab_min_at_most_neg_one(context_tab
, var
)) {
1751 if (i
>= tab
->n_param
)
1752 j
= i
- tab
->n_param
+ tab
->n_var
- tab
->n_div
;
1753 tab
->var
[j
].is_nonneg
= 1;
1756 isl_int_set_si(ineq
->el
[1 + i
], 0);
1757 if (isl_tab_rollback(context_tab
, snap2
) < 0)
1761 if (isl_tab_rollback(context_tab
, snap
) < 0)
1764 if (n
== context_tab
->n_var
) {
1765 context_tab
->mat
= isl_mat_drop_cols(context_tab
->mat
, 2, 1);
1777 /* Check whether all coefficients of (non-parameter) variables
1778 * are non-positive, meaning that no pivots can be performed on the row.
1780 static int is_critical(struct isl_tab
*tab
, int row
)
1783 unsigned off
= 2 + tab
->M
;
1785 for (j
= tab
->n_dead
; j
< tab
->n_col
; ++j
) {
1786 if (tab
->col_var
[j
] >= 0 &&
1787 (tab
->col_var
[j
] < tab
->n_param
||
1788 tab
->col_var
[j
] >= tab
->n_var
- tab
->n_div
))
1791 if (isl_int_is_pos(tab
->mat
->row
[row
][off
+ j
]))
1798 /* Check whether the inequality represented by vec is strict over the integers,
1799 * i.e., there are no integer values satisfying the constraint with
1800 * equality. This happens if the gcd of the coefficients is not a divisor
1801 * of the constant term. If so, scale the constraint down by the gcd
1802 * of the coefficients.
1804 static int is_strict(struct isl_vec
*vec
)
1810 isl_seq_gcd(vec
->el
+ 1, vec
->size
- 1, &gcd
);
1811 if (!isl_int_is_one(gcd
)) {
1812 strict
= !isl_int_is_divisible_by(vec
->el
[0], gcd
);
1813 isl_int_fdiv_q(vec
->el
[0], vec
->el
[0], gcd
);
1814 isl_seq_scale_down(vec
->el
+ 1, vec
->el
+ 1, gcd
, vec
->size
-1);
1821 /* Determine the sign of the given row of the main tableau.
1822 * The result is one of
1823 * isl_tab_row_pos: always non-negative; no pivot needed
1824 * isl_tab_row_neg: always non-positive; pivot
1825 * isl_tab_row_any: can be both positive and negative; split
1827 * We first handle some simple cases
1828 * - the row sign may be known already
1829 * - the row may be obviously non-negative
1830 * - the parametric constant may be equal to that of another row
1831 * for which we know the sign. This sign will be either "pos" or
1832 * "any". If it had been "neg" then we would have pivoted before.
1834 * If none of these cases hold, we check the value of the row for each
1835 * of the currently active samples. Based on the signs of these values
1836 * we make an initial determination of the sign of the row.
1838 * all zero -> unk(nown)
1839 * all non-negative -> pos
1840 * all non-positive -> neg
1841 * both negative and positive -> all
1843 * If we end up with "all", we are done.
1844 * Otherwise, we perform a check for positive and/or negative
1845 * values as follows.
1847 * samples neg unk pos
1853 * There is no special sign for "zero", because we can usually treat zero
1854 * as either non-negative or non-positive, whatever works out best.
1855 * However, if the row is "critical", meaning that pivoting is impossible
1856 * then we don't want to limp zero with the non-positive case, because
1857 * then we we would lose the solution for those values of the parameters
1858 * where the value of the row is zero. Instead, we treat 0 as non-negative
1859 * ensuring a split if the row can attain both zero and negative values.
1860 * The same happens when the original constraint was one that could not
1861 * be satisfied with equality by any integer values of the parameters.
1862 * In this case, we normalize the constraint, but then a value of zero
1863 * for the normalized constraint is actually a positive value for the
1864 * original constraint, so again we need to treat zero as non-negative.
1865 * In both these cases, we have the following decision tree instead:
1867 * all non-negative -> pos
1868 * all negative -> neg
1869 * both negative and non-negative -> all
1877 static int row_sign(struct isl_tab
*tab
, struct isl_tab
*context_tab
, int row
)
1880 struct isl_tab_undo
*snap
= NULL
;
1881 struct isl_vec
*ineq
= NULL
;
1882 int res
= isl_tab_row_unknown
;
1891 if (tab
->row_sign
[row
] != isl_tab_row_unknown
)
1892 return tab
->row_sign
[row
];
1893 if (is_obviously_nonneg(tab
, row
))
1894 return isl_tab_row_pos
;
1895 for (row2
= tab
->n_redundant
; row2
< tab
->n_row
; ++row2
) {
1896 if (tab
->row_sign
[row2
] == isl_tab_row_unknown
)
1898 if (identical_parameter_line(tab
, row
, row2
))
1899 return tab
->row_sign
[row2
];
1902 critical
= is_critical(tab
, row
);
1904 isl_assert(tab
->mat
->ctx
, context_tab
->samples
, goto error
);
1905 isl_assert(tab
->mat
->ctx
, context_tab
->samples
->n_col
== 1 + context_tab
->n_var
, goto error
);
1907 ineq
= get_row_parameter_ineq(tab
, row
);
1911 strict
= is_strict(ineq
);
1914 for (i
= context_tab
->n_outside
; i
< context_tab
->n_sample
; ++i
) {
1915 isl_seq_inner_product(context_tab
->samples
->row
[i
], ineq
->el
,
1917 sgn
= isl_int_sgn(tmp
);
1918 if (sgn
> 0 || (sgn
== 0 && (critical
|| strict
))) {
1919 if (res
== isl_tab_row_unknown
)
1920 res
= isl_tab_row_pos
;
1921 if (res
== isl_tab_row_neg
)
1922 res
= isl_tab_row_any
;
1925 if (res
== isl_tab_row_unknown
)
1926 res
= isl_tab_row_neg
;
1927 if (res
== isl_tab_row_pos
)
1928 res
= isl_tab_row_any
;
1930 if (res
== isl_tab_row_any
)
1935 if (res
!= isl_tab_row_any
) {
1936 if (isl_tab_extend_cons(context_tab
, 1) < 0)
1939 snap
= isl_tab_snap(context_tab
);
1940 isl_tab_push_basis(context_tab
);
1943 if (res
== isl_tab_row_unknown
|| res
== isl_tab_row_pos
) {
1944 /* test for negative values */
1945 isl_seq_neg(ineq
->el
, ineq
->el
, ineq
->size
);
1946 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
1948 isl_tab_push_basis(context_tab
);
1949 context_tab
= add_lexmin_ineq(context_tab
, ineq
->el
);
1950 context_tab
= check_integer_feasible(context_tab
);
1953 if (context_tab
->empty
)
1954 res
= isl_tab_row_pos
;
1956 res
= (res
== isl_tab_row_unknown
) ? isl_tab_row_neg
1958 if (isl_tab_rollback(context_tab
, snap
) < 0)
1961 if (res
== isl_tab_row_neg
) {
1962 isl_seq_neg(ineq
->el
, ineq
->el
, ineq
->size
);
1963 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
1967 if (res
== isl_tab_row_neg
) {
1968 /* test for positive values */
1969 if (!critical
&& !strict
)
1970 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
1972 isl_tab_push_basis(context_tab
);
1973 context_tab
= add_lexmin_ineq(context_tab
, ineq
->el
);
1974 context_tab
= check_integer_feasible(context_tab
);
1977 if (!context_tab
->empty
)
1978 res
= isl_tab_row_any
;
1979 if (isl_tab_rollback(context_tab
, snap
) < 0)
1990 static struct isl_sol
*find_solutions(struct isl_sol
*sol
, struct isl_tab
*tab
);
1992 /* Find solutions for values of the parameters that satisfy the given
1995 * We currently take a snapshot of the context tableau that is reset
1996 * when we return from this function, while we make a copy of the main
1997 * tableau, leaving the original main tableau untouched.
1998 * These are fairly arbitrary choices. Making a copy also of the context
1999 * tableau would obviate the need to undo any changes made to it later,
2000 * while taking a snapshot of the main tableau could reduce memory usage.
2001 * If we were to switch to taking a snapshot of the main tableau,
2002 * we would have to keep in mind that we need to save the row signs
2003 * and that we need to do this before saving the current basis
2004 * such that the basis has been restore before we restore the row signs.
2006 static struct isl_sol
*find_in_pos(struct isl_sol
*sol
,
2007 struct isl_tab
*tab
, isl_int
*ineq
)
2009 struct isl_tab_undo
*snap
;
2011 snap
= isl_tab_snap(sol
->context_tab
);
2012 isl_tab_push_basis(sol
->context_tab
);
2013 if (isl_tab_extend_cons(sol
->context_tab
, 1) < 0)
2016 tab
= isl_tab_dup(tab
);
2020 sol
->context_tab
= add_lexmin_ineq(sol
->context_tab
, ineq
);
2021 sol
->context_tab
= check_samples(sol
->context_tab
, ineq
, 0);
2023 sol
= find_solutions(sol
, tab
);
2025 isl_tab_rollback(sol
->context_tab
, snap
);
2028 isl_tab_rollback(sol
->context_tab
, snap
);
2033 /* Record the absence of solutions for those values of the parameters
2034 * that do not satisfy the given inequality with equality.
2036 static struct isl_sol
*no_sol_in_strict(struct isl_sol
*sol
,
2037 struct isl_tab
*tab
, struct isl_vec
*ineq
)
2040 struct isl_tab_undo
*snap
;
2041 snap
= isl_tab_snap(sol
->context_tab
);
2042 isl_tab_push_basis(sol
->context_tab
);
2043 if (isl_tab_extend_cons(sol
->context_tab
, 1) < 0)
2046 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
2048 sol
->context_tab
= add_lexmin_ineq(sol
->context_tab
, ineq
->el
);
2049 sol
->context_tab
= check_sample_or_integer_feasible(sol
->context_tab
,
2054 sol
= sol
->add(sol
, tab
);
2057 isl_int_add_ui(ineq
->el
[0], ineq
->el
[0], 1);
2059 if (isl_tab_rollback(sol
->context_tab
, snap
) < 0)
2067 /* Given a main tableau where more than one row requires a split,
2068 * determine and return the "best" row to split on.
2070 * Given two rows in the main tableau, if the inequality corresponding
2071 * to the first row is redundant with respect to that of the second row
2072 * in the current tableau, then it is better to split on the second row,
2073 * since in the positive part, both row will be positive.
2074 * (In the negative part a pivot will have to be performed and just about
2075 * anything can happen to the sign of the other row.)
2077 * As a simple heuristic, we therefore select the row that makes the most
2078 * of the other rows redundant.
2080 * Perhaps it would also be useful to look at the number of constraints
2081 * that conflict with any given constraint.
2083 static int best_split(struct isl_tab
*tab
, struct isl_tab
*context_tab
)
2085 struct isl_tab_undo
*snap
, *snap2
;
2091 if (isl_tab_extend_cons(context_tab
, 2) < 0)
2094 snap
= isl_tab_snap(context_tab
);
2095 isl_tab_push_basis(context_tab
);
2096 snap2
= isl_tab_snap(context_tab
);
2098 for (split
= tab
->n_redundant
; split
< tab
->n_row
; ++split
) {
2099 struct isl_tab_undo
*snap3
;
2100 struct isl_vec
*ineq
= NULL
;
2103 if (!isl_tab_var_from_row(tab
, split
)->is_nonneg
)
2105 if (tab
->row_sign
[split
] != isl_tab_row_any
)
2108 ineq
= get_row_parameter_ineq(tab
, split
);
2111 context_tab
= isl_tab_add_ineq(context_tab
, ineq
->el
);
2114 snap3
= isl_tab_snap(context_tab
);
2116 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
2117 struct isl_tab_var
*var
;
2121 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
2123 if (tab
->row_sign
[row
] != isl_tab_row_any
)
2126 ineq
= get_row_parameter_ineq(tab
, row
);
2129 context_tab
= isl_tab_add_ineq(context_tab
, ineq
->el
);
2131 var
= &context_tab
->con
[context_tab
->n_con
- 1];
2132 if (!context_tab
->empty
&&
2133 !isl_tab_min_at_most_neg_one(context_tab
, var
))
2135 if (isl_tab_rollback(context_tab
, snap3
) < 0)
2138 if (best
== -1 || r
> best_r
) {
2142 if (isl_tab_rollback(context_tab
, snap2
) < 0)
2146 if (isl_tab_rollback(context_tab
, snap
) < 0)
2152 /* Compute the lexicographic minimum of the set represented by the main
2153 * tableau "tab" within the context "sol->context_tab".
2154 * On entry the sample value of the main tableau is lexicographically
2155 * less than or equal to this lexicographic minimum.
2156 * Pivots are performed until a feasible point is found, which is then
2157 * necessarily equal to the minimum, or until the tableau is found to
2158 * be infeasible. Some pivots may need to be performed for only some
2159 * feasible values of the context tableau. If so, the context tableau
2160 * is split into a part where the pivot is needed and a part where it is not.
2162 * Whenever we enter the main loop, the main tableau is such that no
2163 * "obvious" pivots need to be performed on it, where "obvious" means
2164 * that the given row can be seen to be negative without looking at
2165 * the context tableau. In particular, for non-parametric problems,
2166 * no pivots need to be performed on the main tableau.
2167 * The caller of find_solutions is responsible for making this property
2168 * hold prior to the first iteration of the loop, while restore_lexmin
2169 * is called before every other iteration.
2171 * Inside the main loop, we first examine the signs of the rows of
2172 * the main tableau within the context of the context tableau.
2173 * If we find a row that is always non-positive for all values of
2174 * the parameters satisfying the context tableau and negative for at
2175 * least one value of the parameters, we perform the appropriate pivot
2176 * and start over. An exception is the case where no pivot can be
2177 * performed on the row. In this case, we require that the sign of
2178 * the row is negative for all values of the parameters (rather than just
2179 * non-positive). This special case is handled inside row_sign, which
2180 * will say that the row can have any sign if it determines that it can
2181 * attain both negative and zero values.
2183 * If we can't find a row that always requires a pivot, but we can find
2184 * one or more rows that require a pivot for some values of the parameters
2185 * (i.e., the row can attain both positive and negative signs), then we split
2186 * the context tableau into two parts, one where we force the sign to be
2187 * non-negative and one where we force is to be negative.
2188 * The non-negative part is handled by a recursive call (through find_in_pos).
2189 * Upon returning from this call, we continue with the negative part and
2190 * perform the required pivot.
2192 * If no such rows can be found, all rows are non-negative and we have
2193 * found a (rational) feasible point. If we only wanted a rational point
2195 * Otherwise, we check if all values of the sample point of the tableau
2196 * are integral for the variables. If so, we have found the minimal
2197 * integral point and we are done.
2198 * If the sample point is not integral, then we need to make a distinction
2199 * based on whether the constant term is non-integral or the coefficients
2200 * of the parameters. Furthermore, in order to decide how to handle
2201 * the non-integrality, we also need to know whether the coefficients
2202 * of the other columns in the tableau are integral. This leads
2203 * to the following table. The first two rows do not correspond
2204 * to a non-integral sample point and are only mentioned for completeness.
2206 * constant parameters other
2209 * int int rat | -> no problem
2211 * rat int int -> fail
2213 * rat int rat -> cut
2216 * rat rat rat | -> parametric cut
2219 * rat rat int | -> split context
2221 * If the parametric constant is completely integral, then there is nothing
2222 * to be done. If the constant term is non-integral, but all the other
2223 * coefficient are integral, then there is nothing that can be done
2224 * and the tableau has no integral solution.
2225 * If, on the other hand, one or more of the other columns have rational
2226 * coeffcients, but the parameter coefficients are all integral, then
2227 * we can perform a regular (non-parametric) cut.
2228 * Finally, if there is any parameter coefficient that is non-integral,
2229 * then we need to involve the context tableau. There are two cases here.
2230 * If at least one other column has a rational coefficient, then we
2231 * can perform a parametric cut in the main tableau by adding a new
2232 * integer division in the context tableau.
2233 * If all other columns have integral coefficients, then we need to
2234 * enforce that the rational combination of parameters (c + \sum a_i y_i)/m
2235 * is always integral. We do this by introducing an integer division
2236 * q = floor((c + \sum a_i y_i)/m) and stipulating that its argument should
2237 * always be integral in the context tableau, i.e., m q = c + \sum a_i y_i.
2238 * Since q is expressed in the tableau as
2239 * c + \sum a_i y_i - m q >= 0
2240 * -c - \sum a_i y_i + m q + m - 1 >= 0
2241 * it is sufficient to add the inequality
2242 * -c - \sum a_i y_i + m q >= 0
2243 * In the part of the context where this inequality does not hold, the
2244 * main tableau is marked as being empty.
2246 static struct isl_sol
*find_solutions(struct isl_sol
*sol
, struct isl_tab
*tab
)
2248 struct isl_tab
**context_tab
;
2253 context_tab
= &sol
->context_tab
;
2257 if ((*context_tab
)->empty
)
2260 for (; tab
&& !tab
->empty
; tab
= restore_lexmin(tab
)) {
2267 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
2268 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
2270 sgn
= row_sign(tab
, *context_tab
, row
);
2273 tab
->row_sign
[row
] = sgn
;
2274 if (sgn
== isl_tab_row_any
)
2276 if (sgn
== isl_tab_row_any
&& split
== -1)
2278 if (sgn
== isl_tab_row_neg
)
2281 if (row
< tab
->n_row
)
2284 struct isl_vec
*ineq
;
2286 split
= best_split(tab
, *context_tab
);
2289 ineq
= get_row_parameter_ineq(tab
, split
);
2293 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
2294 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
2296 if (tab
->row_sign
[row
] == isl_tab_row_any
)
2297 tab
->row_sign
[row
] = isl_tab_row_unknown
;
2299 tab
->row_sign
[split
] = isl_tab_row_pos
;
2300 sol
= find_in_pos(sol
, tab
, ineq
->el
);
2301 tab
->row_sign
[split
] = isl_tab_row_neg
;
2303 isl_seq_neg(ineq
->el
, ineq
->el
, ineq
->size
);
2304 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
2305 *context_tab
= add_lexmin_ineq(*context_tab
, ineq
->el
);
2306 *context_tab
= check_samples(*context_tab
, ineq
->el
, 0);
2314 row
= first_non_integer(tab
, &flags
);
2317 if (ISL_FL_ISSET(flags
, I_PAR
)) {
2318 if (ISL_FL_ISSET(flags
, I_VAR
)) {
2319 tab
= isl_tab_mark_empty(tab
);
2322 row
= add_cut(tab
, row
);
2323 } else if (ISL_FL_ISSET(flags
, I_VAR
)) {
2324 struct isl_vec
*div
;
2325 struct isl_vec
*ineq
;
2327 if (isl_tab_extend_cons(*context_tab
, 3) < 0)
2329 div
= get_row_split_div(tab
, row
);
2332 d
= get_div(tab
, context_tab
, div
);
2336 ineq
= ineq_for_div((*context_tab
)->bset
, d
);
2337 sol
= no_sol_in_strict(sol
, tab
, ineq
);
2338 isl_seq_neg(ineq
->el
, ineq
->el
, ineq
->size
);
2339 *context_tab
= add_lexmin_ineq(*context_tab
, ineq
->el
);
2340 *context_tab
= check_samples(*context_tab
, ineq
->el
, 0);
2344 tab
= set_row_cst_to_div(tab
, row
, d
);
2346 row
= add_parametric_cut(tab
, row
, context_tab
);
2351 sol
= sol
->add(sol
, tab
);
2360 /* Compute the lexicographic minimum of the set represented by the main
2361 * tableau "tab" within the context "sol->context_tab".
2363 * As a preprocessing step, we first transfer all the purely parametric
2364 * equalities from the main tableau to the context tableau, i.e.,
2365 * parameters that have been pivoted to a row.
2366 * These equalities are ignored by the main algorithm, because the
2367 * corresponding rows may not be marked as being non-negative.
2368 * In parts of the context where the added equality does not hold,
2369 * the main tableau is marked as being empty.
2371 static struct isl_sol
*find_solutions_main(struct isl_sol
*sol
,
2372 struct isl_tab
*tab
)
2376 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
2380 if (tab
->row_var
[row
] < 0)
2382 if (tab
->row_var
[row
] >= tab
->n_param
&&
2383 tab
->row_var
[row
] < tab
->n_var
- tab
->n_div
)
2385 if (tab
->row_var
[row
] < tab
->n_param
)
2386 p
= tab
->row_var
[row
];
2388 p
= tab
->row_var
[row
]
2389 + tab
->n_param
- (tab
->n_var
- tab
->n_div
);
2391 if (isl_tab_extend_cons(sol
->context_tab
, 2) < 0)
2394 eq
= isl_vec_alloc(tab
->mat
->ctx
, 1+tab
->n_param
+tab
->n_div
);
2395 get_row_parameter_line(tab
, row
, eq
->el
);
2396 isl_int_neg(eq
->el
[1 + p
], tab
->mat
->row
[row
][0]);
2397 isl_seq_normalize(eq
->el
, eq
->size
);
2399 sol
= no_sol_in_strict(sol
, tab
, eq
);
2401 isl_seq_neg(eq
->el
, eq
->el
, eq
->size
);
2402 sol
= no_sol_in_strict(sol
, tab
, eq
);
2403 isl_seq_neg(eq
->el
, eq
->el
, eq
->size
);
2405 sol
->context_tab
= add_lexmin_eq(sol
->context_tab
, eq
->el
);
2406 sol
->context_tab
= check_sample_or_integer_feasible(
2407 sol
->context_tab
, eq
->el
, 1);
2408 sol
->context_tab
= check_samples(sol
->context_tab
, eq
->el
, 1);
2412 isl_tab_mark_redundant(tab
, row
);
2414 if (!sol
->context_tab
)
2416 if (sol
->context_tab
->empty
)
2419 row
= tab
->n_redundant
- 1;
2422 return find_solutions(sol
, tab
);
2429 static struct isl_sol_map
*sol_map_find_solutions(struct isl_sol_map
*sol_map
,
2430 struct isl_tab
*tab
)
2432 return (struct isl_sol_map
*)find_solutions_main(&sol_map
->sol
, tab
);
2435 /* Check if integer division "div" of "dom" also occurs in "bmap".
2436 * If so, return its position within the divs.
2437 * If not, return -1.
2439 static int find_context_div(struct isl_basic_map
*bmap
,
2440 struct isl_basic_set
*dom
, unsigned div
)
2443 unsigned b_dim
= isl_dim_total(bmap
->dim
);
2444 unsigned d_dim
= isl_dim_total(dom
->dim
);
2446 if (isl_int_is_zero(dom
->div
[div
][0]))
2448 if (isl_seq_first_non_zero(dom
->div
[div
] + 2 + d_dim
, dom
->n_div
) != -1)
2451 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2452 if (isl_int_is_zero(bmap
->div
[i
][0]))
2454 if (isl_seq_first_non_zero(bmap
->div
[i
] + 2 + d_dim
,
2455 (b_dim
- d_dim
) + bmap
->n_div
) != -1)
2457 if (isl_seq_eq(bmap
->div
[i
], dom
->div
[div
], 2 + d_dim
))
2463 /* The correspondence between the variables in the main tableau,
2464 * the context tableau, and the input map and domain is as follows.
2465 * The first n_param and the last n_div variables of the main tableau
2466 * form the variables of the context tableau.
2467 * In the basic map, these n_param variables correspond to the
2468 * parameters and the input dimensions. In the domain, they correspond
2469 * to the parameters and the set dimensions.
2470 * The n_div variables correspond to the integer divisions in the domain.
2471 * To ensure that everything lines up, we may need to copy some of the
2472 * integer divisions of the domain to the map. These have to be placed
2473 * in the same order as those in the context and they have to be placed
2474 * after any other integer divisions that the map may have.
2475 * This function performs the required reordering.
2477 static struct isl_basic_map
*align_context_divs(struct isl_basic_map
*bmap
,
2478 struct isl_basic_set
*dom
)
2484 for (i
= 0; i
< dom
->n_div
; ++i
)
2485 if (find_context_div(bmap
, dom
, i
) != -1)
2487 other
= bmap
->n_div
- common
;
2488 if (dom
->n_div
- common
> 0) {
2489 bmap
= isl_basic_map_extend_dim(bmap
, isl_dim_copy(bmap
->dim
),
2490 dom
->n_div
- common
, 0, 0);
2494 for (i
= 0; i
< dom
->n_div
; ++i
) {
2495 int pos
= find_context_div(bmap
, dom
, i
);
2497 pos
= isl_basic_map_alloc_div(bmap
);
2500 isl_int_set_si(bmap
->div
[pos
][0], 0);
2502 if (pos
!= other
+ i
)
2503 isl_basic_map_swap_div(bmap
, pos
, other
+ i
);
2507 isl_basic_map_free(bmap
);
2511 /* Compute the lexicographic minimum (or maximum if "max" is set)
2512 * of "bmap" over the domain "dom" and return the result as a map.
2513 * If "empty" is not NULL, then *empty is assigned a set that
2514 * contains those parts of the domain where there is no solution.
2515 * If "bmap" is marked as rational (ISL_BASIC_MAP_RATIONAL),
2516 * then we compute the rational optimum. Otherwise, we compute
2517 * the integral optimum.
2519 * We perform some preprocessing. As the PILP solver does not
2520 * handle implicit equalities very well, we first make sure all
2521 * the equalities are explicitly available.
2522 * We also make sure the divs in the domain are properly order,
2523 * because they will be added one by one in the given order
2524 * during the construction of the solution map.
2526 struct isl_map
*isl_tab_basic_map_partial_lexopt(
2527 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
2528 struct isl_set
**empty
, int max
)
2530 struct isl_tab
*tab
;
2531 struct isl_map
*result
= NULL
;
2532 struct isl_sol_map
*sol_map
= NULL
;
2539 isl_assert(bmap
->ctx
,
2540 isl_basic_map_compatible_domain(bmap
, dom
), goto error
);
2542 bmap
= isl_basic_map_detect_equalities(bmap
);
2545 dom
= isl_basic_set_order_divs(dom
);
2546 bmap
= align_context_divs(bmap
, dom
);
2548 sol_map
= sol_map_init(bmap
, dom
, !!empty
, max
);
2552 if (isl_basic_set_fast_is_empty(sol_map
->sol
.context_tab
->bset
))
2554 else if (isl_basic_map_fast_is_empty(bmap
))
2555 sol_map
= add_empty(sol_map
);
2557 tab
= tab_for_lexmin(bmap
,
2558 sol_map
->sol
.context_tab
->bset
, 1, max
);
2559 tab
= tab_detect_nonnegative_parameters(tab
,
2560 sol_map
->sol
.context_tab
);
2561 sol_map
= sol_map_find_solutions(sol_map
, tab
);
2566 result
= isl_map_copy(sol_map
->map
);
2568 *empty
= isl_set_copy(sol_map
->empty
);
2569 sol_map_free(sol_map
);
2570 isl_basic_map_free(bmap
);
2573 sol_map_free(sol_map
);
2574 isl_basic_map_free(bmap
);