2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2013 Ecole Normale Superieure
4 * Copyright 2014 INRIA Rocquencourt
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, K.U.Leuven, Departement
9 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
11 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
12 * B.P. 105 - 78153 Le Chesnay, France
15 #include <isl_ctx_private.h>
16 #include <isl_mat_private.h>
17 #include <isl_vec_private.h>
18 #include "isl_map_private.h"
21 #include <isl_config.h>
24 * The implementation of tableaus in this file was inspired by Section 8
25 * of David Detlefs, Greg Nelson and James B. Saxe, "Simplify: a theorem
26 * prover for program checking".
29 struct isl_tab
*isl_tab_alloc(struct isl_ctx
*ctx
,
30 unsigned n_row
, unsigned n_var
, unsigned M
)
36 tab
= isl_calloc_type(ctx
, struct isl_tab
);
39 tab
->mat
= isl_mat_alloc(ctx
, n_row
, off
+ n_var
);
42 tab
->var
= isl_alloc_array(ctx
, struct isl_tab_var
, n_var
);
43 if (n_var
&& !tab
->var
)
45 tab
->con
= isl_alloc_array(ctx
, struct isl_tab_var
, n_row
);
46 if (n_row
&& !tab
->con
)
48 tab
->col_var
= isl_alloc_array(ctx
, int, n_var
);
49 if (n_var
&& !tab
->col_var
)
51 tab
->row_var
= isl_alloc_array(ctx
, int, n_row
);
52 if (n_row
&& !tab
->row_var
)
54 for (i
= 0; i
< n_var
; ++i
) {
55 tab
->var
[i
].index
= i
;
56 tab
->var
[i
].is_row
= 0;
57 tab
->var
[i
].is_nonneg
= 0;
58 tab
->var
[i
].is_zero
= 0;
59 tab
->var
[i
].is_redundant
= 0;
60 tab
->var
[i
].frozen
= 0;
61 tab
->var
[i
].negated
= 0;
75 tab
->strict_redundant
= 0;
82 tab
->bottom
.type
= isl_tab_undo_bottom
;
83 tab
->bottom
.next
= NULL
;
84 tab
->top
= &tab
->bottom
;
96 isl_ctx
*isl_tab_get_ctx(struct isl_tab
*tab
)
98 return tab
? isl_mat_get_ctx(tab
->mat
) : NULL
;
101 int isl_tab_extend_cons(struct isl_tab
*tab
, unsigned n_new
)
110 if (tab
->max_con
< tab
->n_con
+ n_new
) {
111 struct isl_tab_var
*con
;
113 con
= isl_realloc_array(tab
->mat
->ctx
, tab
->con
,
114 struct isl_tab_var
, tab
->max_con
+ n_new
);
118 tab
->max_con
+= n_new
;
120 if (tab
->mat
->n_row
< tab
->n_row
+ n_new
) {
123 tab
->mat
= isl_mat_extend(tab
->mat
,
124 tab
->n_row
+ n_new
, off
+ tab
->n_col
);
127 row_var
= isl_realloc_array(tab
->mat
->ctx
, tab
->row_var
,
128 int, tab
->mat
->n_row
);
131 tab
->row_var
= row_var
;
133 enum isl_tab_row_sign
*s
;
134 s
= isl_realloc_array(tab
->mat
->ctx
, tab
->row_sign
,
135 enum isl_tab_row_sign
, tab
->mat
->n_row
);
144 /* Make room for at least n_new extra variables.
145 * Return -1 if anything went wrong.
147 int isl_tab_extend_vars(struct isl_tab
*tab
, unsigned n_new
)
149 struct isl_tab_var
*var
;
150 unsigned off
= 2 + tab
->M
;
152 if (tab
->max_var
< tab
->n_var
+ n_new
) {
153 var
= isl_realloc_array(tab
->mat
->ctx
, tab
->var
,
154 struct isl_tab_var
, tab
->n_var
+ n_new
);
158 tab
->max_var
= tab
->n_var
+ n_new
;
161 if (tab
->mat
->n_col
< off
+ tab
->n_col
+ n_new
) {
164 tab
->mat
= isl_mat_extend(tab
->mat
,
165 tab
->mat
->n_row
, off
+ tab
->n_col
+ n_new
);
168 p
= isl_realloc_array(tab
->mat
->ctx
, tab
->col_var
,
169 int, tab
->n_col
+ n_new
);
178 static void free_undo_record(struct isl_tab_undo
*undo
)
180 switch (undo
->type
) {
181 case isl_tab_undo_saved_basis
:
182 free(undo
->u
.col_var
);
189 static void free_undo(struct isl_tab
*tab
)
191 struct isl_tab_undo
*undo
, *next
;
193 for (undo
= tab
->top
; undo
&& undo
!= &tab
->bottom
; undo
= next
) {
195 free_undo_record(undo
);
200 void isl_tab_free(struct isl_tab
*tab
)
205 isl_mat_free(tab
->mat
);
206 isl_vec_free(tab
->dual
);
207 isl_basic_map_free(tab
->bmap
);
213 isl_mat_free(tab
->samples
);
214 free(tab
->sample_index
);
215 isl_mat_free(tab
->basis
);
219 struct isl_tab
*isl_tab_dup(struct isl_tab
*tab
)
229 dup
= isl_calloc_type(tab
->mat
->ctx
, struct isl_tab
);
232 dup
->mat
= isl_mat_dup(tab
->mat
);
235 dup
->var
= isl_alloc_array(tab
->mat
->ctx
, struct isl_tab_var
, tab
->max_var
);
236 if (tab
->max_var
&& !dup
->var
)
238 for (i
= 0; i
< tab
->n_var
; ++i
)
239 dup
->var
[i
] = tab
->var
[i
];
240 dup
->con
= isl_alloc_array(tab
->mat
->ctx
, struct isl_tab_var
, tab
->max_con
);
241 if (tab
->max_con
&& !dup
->con
)
243 for (i
= 0; i
< tab
->n_con
; ++i
)
244 dup
->con
[i
] = tab
->con
[i
];
245 dup
->col_var
= isl_alloc_array(tab
->mat
->ctx
, int, tab
->mat
->n_col
- off
);
246 if ((tab
->mat
->n_col
- off
) && !dup
->col_var
)
248 for (i
= 0; i
< tab
->n_col
; ++i
)
249 dup
->col_var
[i
] = tab
->col_var
[i
];
250 dup
->row_var
= isl_alloc_array(tab
->mat
->ctx
, int, tab
->mat
->n_row
);
251 if (tab
->mat
->n_row
&& !dup
->row_var
)
253 for (i
= 0; i
< tab
->n_row
; ++i
)
254 dup
->row_var
[i
] = tab
->row_var
[i
];
256 dup
->row_sign
= isl_alloc_array(tab
->mat
->ctx
, enum isl_tab_row_sign
,
258 if (tab
->mat
->n_row
&& !dup
->row_sign
)
260 for (i
= 0; i
< tab
->n_row
; ++i
)
261 dup
->row_sign
[i
] = tab
->row_sign
[i
];
264 dup
->samples
= isl_mat_dup(tab
->samples
);
267 dup
->sample_index
= isl_alloc_array(tab
->mat
->ctx
, int,
268 tab
->samples
->n_row
);
269 if (tab
->samples
->n_row
&& !dup
->sample_index
)
271 dup
->n_sample
= tab
->n_sample
;
272 dup
->n_outside
= tab
->n_outside
;
274 dup
->n_row
= tab
->n_row
;
275 dup
->n_con
= tab
->n_con
;
276 dup
->n_eq
= tab
->n_eq
;
277 dup
->max_con
= tab
->max_con
;
278 dup
->n_col
= tab
->n_col
;
279 dup
->n_var
= tab
->n_var
;
280 dup
->max_var
= tab
->max_var
;
281 dup
->n_param
= tab
->n_param
;
282 dup
->n_div
= tab
->n_div
;
283 dup
->n_dead
= tab
->n_dead
;
284 dup
->n_redundant
= tab
->n_redundant
;
285 dup
->rational
= tab
->rational
;
286 dup
->empty
= tab
->empty
;
287 dup
->strict_redundant
= 0;
291 tab
->cone
= tab
->cone
;
292 dup
->bottom
.type
= isl_tab_undo_bottom
;
293 dup
->bottom
.next
= NULL
;
294 dup
->top
= &dup
->bottom
;
296 dup
->n_zero
= tab
->n_zero
;
297 dup
->n_unbounded
= tab
->n_unbounded
;
298 dup
->basis
= isl_mat_dup(tab
->basis
);
306 /* Construct the coefficient matrix of the product tableau
308 * mat{1,2} is the coefficient matrix of tableau {1,2}
309 * row{1,2} is the number of rows in tableau {1,2}
310 * col{1,2} is the number of columns in tableau {1,2}
311 * off is the offset to the coefficient column (skipping the
312 * denominator, the constant term and the big parameter if any)
313 * r{1,2} is the number of redundant rows in tableau {1,2}
314 * d{1,2} is the number of dead columns in tableau {1,2}
316 * The order of the rows and columns in the result is as explained
317 * in isl_tab_product.
319 static struct isl_mat
*tab_mat_product(struct isl_mat
*mat1
,
320 struct isl_mat
*mat2
, unsigned row1
, unsigned row2
,
321 unsigned col1
, unsigned col2
,
322 unsigned off
, unsigned r1
, unsigned r2
, unsigned d1
, unsigned d2
)
325 struct isl_mat
*prod
;
328 prod
= isl_mat_alloc(mat1
->ctx
, mat1
->n_row
+ mat2
->n_row
,
334 for (i
= 0; i
< r1
; ++i
) {
335 isl_seq_cpy(prod
->row
[n
+ i
], mat1
->row
[i
], off
+ d1
);
336 isl_seq_clr(prod
->row
[n
+ i
] + off
+ d1
, d2
);
337 isl_seq_cpy(prod
->row
[n
+ i
] + off
+ d1
+ d2
,
338 mat1
->row
[i
] + off
+ d1
, col1
- d1
);
339 isl_seq_clr(prod
->row
[n
+ i
] + off
+ col1
+ d1
, col2
- d2
);
343 for (i
= 0; i
< r2
; ++i
) {
344 isl_seq_cpy(prod
->row
[n
+ i
], mat2
->row
[i
], off
);
345 isl_seq_clr(prod
->row
[n
+ i
] + off
, d1
);
346 isl_seq_cpy(prod
->row
[n
+ i
] + off
+ d1
,
347 mat2
->row
[i
] + off
, d2
);
348 isl_seq_clr(prod
->row
[n
+ i
] + off
+ d1
+ d2
, col1
- d1
);
349 isl_seq_cpy(prod
->row
[n
+ i
] + off
+ col1
+ d1
,
350 mat2
->row
[i
] + off
+ d2
, col2
- d2
);
354 for (i
= 0; i
< row1
- r1
; ++i
) {
355 isl_seq_cpy(prod
->row
[n
+ i
], mat1
->row
[r1
+ i
], off
+ d1
);
356 isl_seq_clr(prod
->row
[n
+ i
] + off
+ d1
, d2
);
357 isl_seq_cpy(prod
->row
[n
+ i
] + off
+ d1
+ d2
,
358 mat1
->row
[r1
+ i
] + off
+ d1
, col1
- d1
);
359 isl_seq_clr(prod
->row
[n
+ i
] + off
+ col1
+ d1
, col2
- d2
);
363 for (i
= 0; i
< row2
- r2
; ++i
) {
364 isl_seq_cpy(prod
->row
[n
+ i
], mat2
->row
[r2
+ i
], off
);
365 isl_seq_clr(prod
->row
[n
+ i
] + off
, d1
);
366 isl_seq_cpy(prod
->row
[n
+ i
] + off
+ d1
,
367 mat2
->row
[r2
+ i
] + off
, d2
);
368 isl_seq_clr(prod
->row
[n
+ i
] + off
+ d1
+ d2
, col1
- d1
);
369 isl_seq_cpy(prod
->row
[n
+ i
] + off
+ col1
+ d1
,
370 mat2
->row
[r2
+ i
] + off
+ d2
, col2
- d2
);
376 /* Update the row or column index of a variable that corresponds
377 * to a variable in the first input tableau.
379 static void update_index1(struct isl_tab_var
*var
,
380 unsigned r1
, unsigned r2
, unsigned d1
, unsigned d2
)
382 if (var
->index
== -1)
384 if (var
->is_row
&& var
->index
>= r1
)
386 if (!var
->is_row
&& var
->index
>= d1
)
390 /* Update the row or column index of a variable that corresponds
391 * to a variable in the second input tableau.
393 static void update_index2(struct isl_tab_var
*var
,
394 unsigned row1
, unsigned col1
,
395 unsigned r1
, unsigned r2
, unsigned d1
, unsigned d2
)
397 if (var
->index
== -1)
412 /* Create a tableau that represents the Cartesian product of the sets
413 * represented by tableaus tab1 and tab2.
414 * The order of the rows in the product is
415 * - redundant rows of tab1
416 * - redundant rows of tab2
417 * - non-redundant rows of tab1
418 * - non-redundant rows of tab2
419 * The order of the columns is
422 * - coefficient of big parameter, if any
423 * - dead columns of tab1
424 * - dead columns of tab2
425 * - live columns of tab1
426 * - live columns of tab2
427 * The order of the variables and the constraints is a concatenation
428 * of order in the two input tableaus.
430 struct isl_tab
*isl_tab_product(struct isl_tab
*tab1
, struct isl_tab
*tab2
)
433 struct isl_tab
*prod
;
435 unsigned r1
, r2
, d1
, d2
;
440 isl_assert(tab1
->mat
->ctx
, tab1
->M
== tab2
->M
, return NULL
);
441 isl_assert(tab1
->mat
->ctx
, tab1
->rational
== tab2
->rational
, return NULL
);
442 isl_assert(tab1
->mat
->ctx
, tab1
->cone
== tab2
->cone
, return NULL
);
443 isl_assert(tab1
->mat
->ctx
, !tab1
->row_sign
, return NULL
);
444 isl_assert(tab1
->mat
->ctx
, !tab2
->row_sign
, return NULL
);
445 isl_assert(tab1
->mat
->ctx
, tab1
->n_param
== 0, return NULL
);
446 isl_assert(tab1
->mat
->ctx
, tab2
->n_param
== 0, return NULL
);
447 isl_assert(tab1
->mat
->ctx
, tab1
->n_div
== 0, return NULL
);
448 isl_assert(tab1
->mat
->ctx
, tab2
->n_div
== 0, return NULL
);
451 r1
= tab1
->n_redundant
;
452 r2
= tab2
->n_redundant
;
455 prod
= isl_calloc_type(tab1
->mat
->ctx
, struct isl_tab
);
458 prod
->mat
= tab_mat_product(tab1
->mat
, tab2
->mat
,
459 tab1
->n_row
, tab2
->n_row
,
460 tab1
->n_col
, tab2
->n_col
, off
, r1
, r2
, d1
, d2
);
463 prod
->var
= isl_alloc_array(tab1
->mat
->ctx
, struct isl_tab_var
,
464 tab1
->max_var
+ tab2
->max_var
);
465 if ((tab1
->max_var
+ tab2
->max_var
) && !prod
->var
)
467 for (i
= 0; i
< tab1
->n_var
; ++i
) {
468 prod
->var
[i
] = tab1
->var
[i
];
469 update_index1(&prod
->var
[i
], r1
, r2
, d1
, d2
);
471 for (i
= 0; i
< tab2
->n_var
; ++i
) {
472 prod
->var
[tab1
->n_var
+ i
] = tab2
->var
[i
];
473 update_index2(&prod
->var
[tab1
->n_var
+ i
],
474 tab1
->n_row
, tab1
->n_col
,
477 prod
->con
= isl_alloc_array(tab1
->mat
->ctx
, struct isl_tab_var
,
478 tab1
->max_con
+ tab2
->max_con
);
479 if ((tab1
->max_con
+ tab2
->max_con
) && !prod
->con
)
481 for (i
= 0; i
< tab1
->n_con
; ++i
) {
482 prod
->con
[i
] = tab1
->con
[i
];
483 update_index1(&prod
->con
[i
], r1
, r2
, d1
, d2
);
485 for (i
= 0; i
< tab2
->n_con
; ++i
) {
486 prod
->con
[tab1
->n_con
+ i
] = tab2
->con
[i
];
487 update_index2(&prod
->con
[tab1
->n_con
+ i
],
488 tab1
->n_row
, tab1
->n_col
,
491 prod
->col_var
= isl_alloc_array(tab1
->mat
->ctx
, int,
492 tab1
->n_col
+ tab2
->n_col
);
493 if ((tab1
->n_col
+ tab2
->n_col
) && !prod
->col_var
)
495 for (i
= 0; i
< tab1
->n_col
; ++i
) {
496 int pos
= i
< d1
? i
: i
+ d2
;
497 prod
->col_var
[pos
] = tab1
->col_var
[i
];
499 for (i
= 0; i
< tab2
->n_col
; ++i
) {
500 int pos
= i
< d2
? d1
+ i
: tab1
->n_col
+ i
;
501 int t
= tab2
->col_var
[i
];
506 prod
->col_var
[pos
] = t
;
508 prod
->row_var
= isl_alloc_array(tab1
->mat
->ctx
, int,
509 tab1
->mat
->n_row
+ tab2
->mat
->n_row
);
510 if ((tab1
->mat
->n_row
+ tab2
->mat
->n_row
) && !prod
->row_var
)
512 for (i
= 0; i
< tab1
->n_row
; ++i
) {
513 int pos
= i
< r1
? i
: i
+ r2
;
514 prod
->row_var
[pos
] = tab1
->row_var
[i
];
516 for (i
= 0; i
< tab2
->n_row
; ++i
) {
517 int pos
= i
< r2
? r1
+ i
: tab1
->n_row
+ i
;
518 int t
= tab2
->row_var
[i
];
523 prod
->row_var
[pos
] = t
;
525 prod
->samples
= NULL
;
526 prod
->sample_index
= NULL
;
527 prod
->n_row
= tab1
->n_row
+ tab2
->n_row
;
528 prod
->n_con
= tab1
->n_con
+ tab2
->n_con
;
530 prod
->max_con
= tab1
->max_con
+ tab2
->max_con
;
531 prod
->n_col
= tab1
->n_col
+ tab2
->n_col
;
532 prod
->n_var
= tab1
->n_var
+ tab2
->n_var
;
533 prod
->max_var
= tab1
->max_var
+ tab2
->max_var
;
536 prod
->n_dead
= tab1
->n_dead
+ tab2
->n_dead
;
537 prod
->n_redundant
= tab1
->n_redundant
+ tab2
->n_redundant
;
538 prod
->rational
= tab1
->rational
;
539 prod
->empty
= tab1
->empty
|| tab2
->empty
;
540 prod
->strict_redundant
= tab1
->strict_redundant
|| tab2
->strict_redundant
;
544 prod
->cone
= tab1
->cone
;
545 prod
->bottom
.type
= isl_tab_undo_bottom
;
546 prod
->bottom
.next
= NULL
;
547 prod
->top
= &prod
->bottom
;
550 prod
->n_unbounded
= 0;
559 static struct isl_tab_var
*var_from_index(struct isl_tab
*tab
, int i
)
564 return &tab
->con
[~i
];
567 struct isl_tab_var
*isl_tab_var_from_row(struct isl_tab
*tab
, int i
)
569 return var_from_index(tab
, tab
->row_var
[i
]);
572 static struct isl_tab_var
*var_from_col(struct isl_tab
*tab
, int i
)
574 return var_from_index(tab
, tab
->col_var
[i
]);
577 /* Check if there are any upper bounds on column variable "var",
578 * i.e., non-negative rows where var appears with a negative coefficient.
579 * Return 1 if there are no such bounds.
581 static int max_is_manifestly_unbounded(struct isl_tab
*tab
,
582 struct isl_tab_var
*var
)
585 unsigned off
= 2 + tab
->M
;
589 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
590 if (!isl_int_is_neg(tab
->mat
->row
[i
][off
+ var
->index
]))
592 if (isl_tab_var_from_row(tab
, i
)->is_nonneg
)
598 /* Check if there are any lower bounds on column variable "var",
599 * i.e., non-negative rows where var appears with a positive coefficient.
600 * Return 1 if there are no such bounds.
602 static int min_is_manifestly_unbounded(struct isl_tab
*tab
,
603 struct isl_tab_var
*var
)
606 unsigned off
= 2 + tab
->M
;
610 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
611 if (!isl_int_is_pos(tab
->mat
->row
[i
][off
+ var
->index
]))
613 if (isl_tab_var_from_row(tab
, i
)->is_nonneg
)
619 static int row_cmp(struct isl_tab
*tab
, int r1
, int r2
, int c
, isl_int
*t
)
621 unsigned off
= 2 + tab
->M
;
625 isl_int_mul(*t
, tab
->mat
->row
[r1
][2], tab
->mat
->row
[r2
][off
+c
]);
626 isl_int_submul(*t
, tab
->mat
->row
[r2
][2], tab
->mat
->row
[r1
][off
+c
]);
631 isl_int_mul(*t
, tab
->mat
->row
[r1
][1], tab
->mat
->row
[r2
][off
+ c
]);
632 isl_int_submul(*t
, tab
->mat
->row
[r2
][1], tab
->mat
->row
[r1
][off
+ c
]);
633 return isl_int_sgn(*t
);
636 /* Given the index of a column "c", return the index of a row
637 * that can be used to pivot the column in, with either an increase
638 * (sgn > 0) or a decrease (sgn < 0) of the corresponding variable.
639 * If "var" is not NULL, then the row returned will be different from
640 * the one associated with "var".
642 * Each row in the tableau is of the form
644 * x_r = a_r0 + \sum_i a_ri x_i
646 * Only rows with x_r >= 0 and with the sign of a_ri opposite to "sgn"
647 * impose any limit on the increase or decrease in the value of x_c
648 * and this bound is equal to a_r0 / |a_rc|. We are therefore looking
649 * for the row with the smallest (most stringent) such bound.
650 * Note that the common denominator of each row drops out of the fraction.
651 * To check if row j has a smaller bound than row r, i.e.,
652 * a_j0 / |a_jc| < a_r0 / |a_rc| or a_j0 |a_rc| < a_r0 |a_jc|,
653 * we check if -sign(a_jc) (a_j0 a_rc - a_r0 a_jc) < 0,
654 * where -sign(a_jc) is equal to "sgn".
656 static int pivot_row(struct isl_tab
*tab
,
657 struct isl_tab_var
*var
, int sgn
, int c
)
661 unsigned off
= 2 + tab
->M
;
665 for (j
= tab
->n_redundant
; j
< tab
->n_row
; ++j
) {
666 if (var
&& j
== var
->index
)
668 if (!isl_tab_var_from_row(tab
, j
)->is_nonneg
)
670 if (sgn
* isl_int_sgn(tab
->mat
->row
[j
][off
+ c
]) >= 0)
676 tsgn
= sgn
* row_cmp(tab
, r
, j
, c
, &t
);
677 if (tsgn
< 0 || (tsgn
== 0 &&
678 tab
->row_var
[j
] < tab
->row_var
[r
]))
685 /* Find a pivot (row and col) that will increase (sgn > 0) or decrease
686 * (sgn < 0) the value of row variable var.
687 * If not NULL, then skip_var is a row variable that should be ignored
688 * while looking for a pivot row. It is usually equal to var.
690 * As the given row in the tableau is of the form
692 * x_r = a_r0 + \sum_i a_ri x_i
694 * we need to find a column such that the sign of a_ri is equal to "sgn"
695 * (such that an increase in x_i will have the desired effect) or a
696 * column with a variable that may attain negative values.
697 * If a_ri is positive, then we need to move x_i in the same direction
698 * to obtain the desired effect. Otherwise, x_i has to move in the
699 * opposite direction.
701 static void find_pivot(struct isl_tab
*tab
,
702 struct isl_tab_var
*var
, struct isl_tab_var
*skip_var
,
703 int sgn
, int *row
, int *col
)
710 isl_assert(tab
->mat
->ctx
, var
->is_row
, return);
711 tr
= tab
->mat
->row
[var
->index
] + 2 + tab
->M
;
714 for (j
= tab
->n_dead
; j
< tab
->n_col
; ++j
) {
715 if (isl_int_is_zero(tr
[j
]))
717 if (isl_int_sgn(tr
[j
]) != sgn
&&
718 var_from_col(tab
, j
)->is_nonneg
)
720 if (c
< 0 || tab
->col_var
[j
] < tab
->col_var
[c
])
726 sgn
*= isl_int_sgn(tr
[c
]);
727 r
= pivot_row(tab
, skip_var
, sgn
, c
);
728 *row
= r
< 0 ? var
->index
: r
;
732 /* Return 1 if row "row" represents an obviously redundant inequality.
734 * - it represents an inequality or a variable
735 * - that is the sum of a non-negative sample value and a positive
736 * combination of zero or more non-negative constraints.
738 int isl_tab_row_is_redundant(struct isl_tab
*tab
, int row
)
741 unsigned off
= 2 + tab
->M
;
743 if (tab
->row_var
[row
] < 0 && !isl_tab_var_from_row(tab
, row
)->is_nonneg
)
746 if (isl_int_is_neg(tab
->mat
->row
[row
][1]))
748 if (tab
->strict_redundant
&& isl_int_is_zero(tab
->mat
->row
[row
][1]))
750 if (tab
->M
&& isl_int_is_neg(tab
->mat
->row
[row
][2]))
753 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
754 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ i
]))
756 if (tab
->col_var
[i
] >= 0)
758 if (isl_int_is_neg(tab
->mat
->row
[row
][off
+ i
]))
760 if (!var_from_col(tab
, i
)->is_nonneg
)
766 static void swap_rows(struct isl_tab
*tab
, int row1
, int row2
)
769 enum isl_tab_row_sign s
;
771 t
= tab
->row_var
[row1
];
772 tab
->row_var
[row1
] = tab
->row_var
[row2
];
773 tab
->row_var
[row2
] = t
;
774 isl_tab_var_from_row(tab
, row1
)->index
= row1
;
775 isl_tab_var_from_row(tab
, row2
)->index
= row2
;
776 tab
->mat
= isl_mat_swap_rows(tab
->mat
, row1
, row2
);
780 s
= tab
->row_sign
[row1
];
781 tab
->row_sign
[row1
] = tab
->row_sign
[row2
];
782 tab
->row_sign
[row2
] = s
;
785 static int push_union(struct isl_tab
*tab
,
786 enum isl_tab_undo_type type
, union isl_tab_undo_val u
) WARN_UNUSED
;
787 static int push_union(struct isl_tab
*tab
,
788 enum isl_tab_undo_type type
, union isl_tab_undo_val u
)
790 struct isl_tab_undo
*undo
;
797 undo
= isl_alloc_type(tab
->mat
->ctx
, struct isl_tab_undo
);
802 undo
->next
= tab
->top
;
808 int isl_tab_push_var(struct isl_tab
*tab
,
809 enum isl_tab_undo_type type
, struct isl_tab_var
*var
)
811 union isl_tab_undo_val u
;
813 u
.var_index
= tab
->row_var
[var
->index
];
815 u
.var_index
= tab
->col_var
[var
->index
];
816 return push_union(tab
, type
, u
);
819 int isl_tab_push(struct isl_tab
*tab
, enum isl_tab_undo_type type
)
821 union isl_tab_undo_val u
= { 0 };
822 return push_union(tab
, type
, u
);
825 /* Push a record on the undo stack describing the current basic
826 * variables, so that the this state can be restored during rollback.
828 int isl_tab_push_basis(struct isl_tab
*tab
)
831 union isl_tab_undo_val u
;
833 u
.col_var
= isl_alloc_array(tab
->mat
->ctx
, int, tab
->n_col
);
834 if (tab
->n_col
&& !u
.col_var
)
836 for (i
= 0; i
< tab
->n_col
; ++i
)
837 u
.col_var
[i
] = tab
->col_var
[i
];
838 return push_union(tab
, isl_tab_undo_saved_basis
, u
);
841 int isl_tab_push_callback(struct isl_tab
*tab
, struct isl_tab_callback
*callback
)
843 union isl_tab_undo_val u
;
844 u
.callback
= callback
;
845 return push_union(tab
, isl_tab_undo_callback
, u
);
848 struct isl_tab
*isl_tab_init_samples(struct isl_tab
*tab
)
855 tab
->samples
= isl_mat_alloc(tab
->mat
->ctx
, 1, 1 + tab
->n_var
);
858 tab
->sample_index
= isl_alloc_array(tab
->mat
->ctx
, int, 1);
859 if (!tab
->sample_index
)
867 int isl_tab_add_sample(struct isl_tab
*tab
, __isl_take isl_vec
*sample
)
872 if (tab
->n_sample
+ 1 > tab
->samples
->n_row
) {
873 int *t
= isl_realloc_array(tab
->mat
->ctx
,
874 tab
->sample_index
, int, tab
->n_sample
+ 1);
877 tab
->sample_index
= t
;
880 tab
->samples
= isl_mat_extend(tab
->samples
,
881 tab
->n_sample
+ 1, tab
->samples
->n_col
);
885 isl_seq_cpy(tab
->samples
->row
[tab
->n_sample
], sample
->el
, sample
->size
);
886 isl_vec_free(sample
);
887 tab
->sample_index
[tab
->n_sample
] = tab
->n_sample
;
892 isl_vec_free(sample
);
896 struct isl_tab
*isl_tab_drop_sample(struct isl_tab
*tab
, int s
)
898 if (s
!= tab
->n_outside
) {
899 int t
= tab
->sample_index
[tab
->n_outside
];
900 tab
->sample_index
[tab
->n_outside
] = tab
->sample_index
[s
];
901 tab
->sample_index
[s
] = t
;
902 isl_mat_swap_rows(tab
->samples
, tab
->n_outside
, s
);
905 if (isl_tab_push(tab
, isl_tab_undo_drop_sample
) < 0) {
913 /* Record the current number of samples so that we can remove newer
914 * samples during a rollback.
916 int isl_tab_save_samples(struct isl_tab
*tab
)
918 union isl_tab_undo_val u
;
924 return push_union(tab
, isl_tab_undo_saved_samples
, u
);
927 /* Mark row with index "row" as being redundant.
928 * If we may need to undo the operation or if the row represents
929 * a variable of the original problem, the row is kept,
930 * but no longer considered when looking for a pivot row.
931 * Otherwise, the row is simply removed.
933 * The row may be interchanged with some other row. If it
934 * is interchanged with a later row, return 1. Otherwise return 0.
935 * If the rows are checked in order in the calling function,
936 * then a return value of 1 means that the row with the given
937 * row number may now contain a different row that hasn't been checked yet.
939 int isl_tab_mark_redundant(struct isl_tab
*tab
, int row
)
941 struct isl_tab_var
*var
= isl_tab_var_from_row(tab
, row
);
942 var
->is_redundant
= 1;
943 isl_assert(tab
->mat
->ctx
, row
>= tab
->n_redundant
, return -1);
944 if (tab
->preserve
|| tab
->need_undo
|| tab
->row_var
[row
] >= 0) {
945 if (tab
->row_var
[row
] >= 0 && !var
->is_nonneg
) {
947 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, var
) < 0)
950 if (row
!= tab
->n_redundant
)
951 swap_rows(tab
, row
, tab
->n_redundant
);
953 return isl_tab_push_var(tab
, isl_tab_undo_redundant
, var
);
955 if (row
!= tab
->n_row
- 1)
956 swap_rows(tab
, row
, tab
->n_row
- 1);
957 isl_tab_var_from_row(tab
, tab
->n_row
- 1)->index
= -1;
963 /* Mark "tab" as a rational tableau.
964 * If it wasn't marked as a rational tableau already and if we may
965 * need to undo changes, then arrange for the marking to be undone
968 int isl_tab_mark_rational(struct isl_tab
*tab
)
972 if (!tab
->rational
&& tab
->need_undo
)
973 if (isl_tab_push(tab
, isl_tab_undo_rational
) < 0)
979 int isl_tab_mark_empty(struct isl_tab
*tab
)
983 if (!tab
->empty
&& tab
->need_undo
)
984 if (isl_tab_push(tab
, isl_tab_undo_empty
) < 0)
990 int isl_tab_freeze_constraint(struct isl_tab
*tab
, int con
)
992 struct isl_tab_var
*var
;
997 var
= &tab
->con
[con
];
1005 return isl_tab_push_var(tab
, isl_tab_undo_freeze
, var
);
1010 /* Update the rows signs after a pivot of "row" and "col", with "row_sgn"
1011 * the original sign of the pivot element.
1012 * We only keep track of row signs during PILP solving and in this case
1013 * we only pivot a row with negative sign (meaning the value is always
1014 * non-positive) using a positive pivot element.
1016 * For each row j, the new value of the parametric constant is equal to
1018 * a_j0 - a_jc a_r0/a_rc
1020 * where a_j0 is the original parametric constant, a_rc is the pivot element,
1021 * a_r0 is the parametric constant of the pivot row and a_jc is the
1022 * pivot column entry of the row j.
1023 * Since a_r0 is non-positive and a_rc is positive, the sign of row j
1024 * remains the same if a_jc has the same sign as the row j or if
1025 * a_jc is zero. In all other cases, we reset the sign to "unknown".
1027 static void update_row_sign(struct isl_tab
*tab
, int row
, int col
, int row_sgn
)
1030 struct isl_mat
*mat
= tab
->mat
;
1031 unsigned off
= 2 + tab
->M
;
1036 if (tab
->row_sign
[row
] == 0)
1038 isl_assert(mat
->ctx
, row_sgn
> 0, return);
1039 isl_assert(mat
->ctx
, tab
->row_sign
[row
] == isl_tab_row_neg
, return);
1040 tab
->row_sign
[row
] = isl_tab_row_pos
;
1041 for (i
= 0; i
< tab
->n_row
; ++i
) {
1045 s
= isl_int_sgn(mat
->row
[i
][off
+ col
]);
1048 if (!tab
->row_sign
[i
])
1050 if (s
< 0 && tab
->row_sign
[i
] == isl_tab_row_neg
)
1052 if (s
> 0 && tab
->row_sign
[i
] == isl_tab_row_pos
)
1054 tab
->row_sign
[i
] = isl_tab_row_unknown
;
1058 /* Given a row number "row" and a column number "col", pivot the tableau
1059 * such that the associated variables are interchanged.
1060 * The given row in the tableau expresses
1062 * x_r = a_r0 + \sum_i a_ri x_i
1066 * x_c = 1/a_rc x_r - a_r0/a_rc + sum_{i \ne r} -a_ri/a_rc
1068 * Substituting this equality into the other rows
1070 * x_j = a_j0 + \sum_i a_ji x_i
1072 * with a_jc \ne 0, we obtain
1074 * x_j = a_jc/a_rc x_r + a_j0 - a_jc a_r0/a_rc + sum a_ji - a_jc a_ri/a_rc
1081 * where i is any other column and j is any other row,
1082 * is therefore transformed into
1084 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1085 * s(n_rc)d_r n_jc/(|n_rc| d_j) (n_ji |n_rc| - s(n_rc)n_jc n_ri)/(|n_rc| d_j)
1087 * The transformation is performed along the following steps
1089 * d_r/n_rc n_ri/n_rc
1092 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1095 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1096 * n_jc/(|n_rc| d_j) n_ji/(|n_rc| d_j)
1098 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1099 * n_jc/(|n_rc| d_j) (n_ji |n_rc|)/(|n_rc| d_j)
1101 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1102 * n_jc/(|n_rc| d_j) (n_ji |n_rc| - s(n_rc)n_jc n_ri)/(|n_rc| d_j)
1104 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1105 * s(n_rc)d_r n_jc/(|n_rc| d_j) (n_ji |n_rc| - s(n_rc)n_jc n_ri)/(|n_rc| d_j)
1108 int isl_tab_pivot(struct isl_tab
*tab
, int row
, int col
)
1114 struct isl_mat
*mat
= tab
->mat
;
1115 struct isl_tab_var
*var
;
1116 unsigned off
= 2 + tab
->M
;
1118 ctx
= isl_tab_get_ctx(tab
);
1119 if (isl_ctx_next_operation(ctx
) < 0)
1122 isl_int_swap(mat
->row
[row
][0], mat
->row
[row
][off
+ col
]);
1123 sgn
= isl_int_sgn(mat
->row
[row
][0]);
1125 isl_int_neg(mat
->row
[row
][0], mat
->row
[row
][0]);
1126 isl_int_neg(mat
->row
[row
][off
+ col
], mat
->row
[row
][off
+ col
]);
1128 for (j
= 0; j
< off
- 1 + tab
->n_col
; ++j
) {
1129 if (j
== off
- 1 + col
)
1131 isl_int_neg(mat
->row
[row
][1 + j
], mat
->row
[row
][1 + j
]);
1133 if (!isl_int_is_one(mat
->row
[row
][0]))
1134 isl_seq_normalize(mat
->ctx
, mat
->row
[row
], off
+ tab
->n_col
);
1135 for (i
= 0; i
< tab
->n_row
; ++i
) {
1138 if (isl_int_is_zero(mat
->row
[i
][off
+ col
]))
1140 isl_int_mul(mat
->row
[i
][0], mat
->row
[i
][0], mat
->row
[row
][0]);
1141 for (j
= 0; j
< off
- 1 + tab
->n_col
; ++j
) {
1142 if (j
== off
- 1 + col
)
1144 isl_int_mul(mat
->row
[i
][1 + j
],
1145 mat
->row
[i
][1 + j
], mat
->row
[row
][0]);
1146 isl_int_addmul(mat
->row
[i
][1 + j
],
1147 mat
->row
[i
][off
+ col
], mat
->row
[row
][1 + j
]);
1149 isl_int_mul(mat
->row
[i
][off
+ col
],
1150 mat
->row
[i
][off
+ col
], mat
->row
[row
][off
+ col
]);
1151 if (!isl_int_is_one(mat
->row
[i
][0]))
1152 isl_seq_normalize(mat
->ctx
, mat
->row
[i
], off
+ tab
->n_col
);
1154 t
= tab
->row_var
[row
];
1155 tab
->row_var
[row
] = tab
->col_var
[col
];
1156 tab
->col_var
[col
] = t
;
1157 var
= isl_tab_var_from_row(tab
, row
);
1160 var
= var_from_col(tab
, col
);
1163 update_row_sign(tab
, row
, col
, sgn
);
1166 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
1167 if (isl_int_is_zero(mat
->row
[i
][off
+ col
]))
1169 if (!isl_tab_var_from_row(tab
, i
)->frozen
&&
1170 isl_tab_row_is_redundant(tab
, i
)) {
1171 int redo
= isl_tab_mark_redundant(tab
, i
);
1181 /* If "var" represents a column variable, then pivot is up (sgn > 0)
1182 * or down (sgn < 0) to a row. The variable is assumed not to be
1183 * unbounded in the specified direction.
1184 * If sgn = 0, then the variable is unbounded in both directions,
1185 * and we pivot with any row we can find.
1187 static int to_row(struct isl_tab
*tab
, struct isl_tab_var
*var
, int sign
) WARN_UNUSED
;
1188 static int to_row(struct isl_tab
*tab
, struct isl_tab_var
*var
, int sign
)
1191 unsigned off
= 2 + tab
->M
;
1197 for (r
= tab
->n_redundant
; r
< tab
->n_row
; ++r
)
1198 if (!isl_int_is_zero(tab
->mat
->row
[r
][off
+var
->index
]))
1200 isl_assert(tab
->mat
->ctx
, r
< tab
->n_row
, return -1);
1202 r
= pivot_row(tab
, NULL
, sign
, var
->index
);
1203 isl_assert(tab
->mat
->ctx
, r
>= 0, return -1);
1206 return isl_tab_pivot(tab
, r
, var
->index
);
1209 /* Check whether all variables that are marked as non-negative
1210 * also have a non-negative sample value. This function is not
1211 * called from the current code but is useful during debugging.
1213 static void check_table(struct isl_tab
*tab
) __attribute__ ((unused
));
1214 static void check_table(struct isl_tab
*tab
)
1220 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
1221 struct isl_tab_var
*var
;
1222 var
= isl_tab_var_from_row(tab
, i
);
1223 if (!var
->is_nonneg
)
1226 isl_assert(tab
->mat
->ctx
,
1227 !isl_int_is_neg(tab
->mat
->row
[i
][2]), abort());
1228 if (isl_int_is_pos(tab
->mat
->row
[i
][2]))
1231 isl_assert(tab
->mat
->ctx
, !isl_int_is_neg(tab
->mat
->row
[i
][1]),
1236 /* Return the sign of the maximal value of "var".
1237 * If the sign is not negative, then on return from this function,
1238 * the sample value will also be non-negative.
1240 * If "var" is manifestly unbounded wrt positive values, we are done.
1241 * Otherwise, we pivot the variable up to a row if needed
1242 * Then we continue pivoting down until either
1243 * - no more down pivots can be performed
1244 * - the sample value is positive
1245 * - the variable is pivoted into a manifestly unbounded column
1247 static int sign_of_max(struct isl_tab
*tab
, struct isl_tab_var
*var
)
1251 if (max_is_manifestly_unbounded(tab
, var
))
1253 if (to_row(tab
, var
, 1) < 0)
1255 while (!isl_int_is_pos(tab
->mat
->row
[var
->index
][1])) {
1256 find_pivot(tab
, var
, var
, 1, &row
, &col
);
1258 return isl_int_sgn(tab
->mat
->row
[var
->index
][1]);
1259 if (isl_tab_pivot(tab
, row
, col
) < 0)
1261 if (!var
->is_row
) /* manifestly unbounded */
1267 int isl_tab_sign_of_max(struct isl_tab
*tab
, int con
)
1269 struct isl_tab_var
*var
;
1274 var
= &tab
->con
[con
];
1275 isl_assert(tab
->mat
->ctx
, !var
->is_redundant
, return -2);
1276 isl_assert(tab
->mat
->ctx
, !var
->is_zero
, return -2);
1278 return sign_of_max(tab
, var
);
1281 static int row_is_neg(struct isl_tab
*tab
, int row
)
1284 return isl_int_is_neg(tab
->mat
->row
[row
][1]);
1285 if (isl_int_is_pos(tab
->mat
->row
[row
][2]))
1287 if (isl_int_is_neg(tab
->mat
->row
[row
][2]))
1289 return isl_int_is_neg(tab
->mat
->row
[row
][1]);
1292 static int row_sgn(struct isl_tab
*tab
, int row
)
1295 return isl_int_sgn(tab
->mat
->row
[row
][1]);
1296 if (!isl_int_is_zero(tab
->mat
->row
[row
][2]))
1297 return isl_int_sgn(tab
->mat
->row
[row
][2]);
1299 return isl_int_sgn(tab
->mat
->row
[row
][1]);
1302 /* Perform pivots until the row variable "var" has a non-negative
1303 * sample value or until no more upward pivots can be performed.
1304 * Return the sign of the sample value after the pivots have been
1307 static int restore_row(struct isl_tab
*tab
, struct isl_tab_var
*var
)
1311 while (row_is_neg(tab
, var
->index
)) {
1312 find_pivot(tab
, var
, var
, 1, &row
, &col
);
1315 if (isl_tab_pivot(tab
, row
, col
) < 0)
1317 if (!var
->is_row
) /* manifestly unbounded */
1320 return row_sgn(tab
, var
->index
);
1323 /* Perform pivots until we are sure that the row variable "var"
1324 * can attain non-negative values. After return from this
1325 * function, "var" is still a row variable, but its sample
1326 * value may not be non-negative, even if the function returns 1.
1328 static int at_least_zero(struct isl_tab
*tab
, struct isl_tab_var
*var
)
1332 while (isl_int_is_neg(tab
->mat
->row
[var
->index
][1])) {
1333 find_pivot(tab
, var
, var
, 1, &row
, &col
);
1336 if (row
== var
->index
) /* manifestly unbounded */
1338 if (isl_tab_pivot(tab
, row
, col
) < 0)
1341 return !isl_int_is_neg(tab
->mat
->row
[var
->index
][1]);
1344 /* Return a negative value if "var" can attain negative values.
1345 * Return a non-negative value otherwise.
1347 * If "var" is manifestly unbounded wrt negative values, we are done.
1348 * Otherwise, if var is in a column, we can pivot it down to a row.
1349 * Then we continue pivoting down until either
1350 * - the pivot would result in a manifestly unbounded column
1351 * => we don't perform the pivot, but simply return -1
1352 * - no more down pivots can be performed
1353 * - the sample value is negative
1354 * If the sample value becomes negative and the variable is supposed
1355 * to be nonnegative, then we undo the last pivot.
1356 * However, if the last pivot has made the pivoting variable
1357 * obviously redundant, then it may have moved to another row.
1358 * In that case we look for upward pivots until we reach a non-negative
1361 static int sign_of_min(struct isl_tab
*tab
, struct isl_tab_var
*var
)
1364 struct isl_tab_var
*pivot_var
= NULL
;
1366 if (min_is_manifestly_unbounded(tab
, var
))
1370 row
= pivot_row(tab
, NULL
, -1, col
);
1371 pivot_var
= var_from_col(tab
, col
);
1372 if (isl_tab_pivot(tab
, row
, col
) < 0)
1374 if (var
->is_redundant
)
1376 if (isl_int_is_neg(tab
->mat
->row
[var
->index
][1])) {
1377 if (var
->is_nonneg
) {
1378 if (!pivot_var
->is_redundant
&&
1379 pivot_var
->index
== row
) {
1380 if (isl_tab_pivot(tab
, row
, col
) < 0)
1383 if (restore_row(tab
, var
) < -1)
1389 if (var
->is_redundant
)
1391 while (!isl_int_is_neg(tab
->mat
->row
[var
->index
][1])) {
1392 find_pivot(tab
, var
, var
, -1, &row
, &col
);
1393 if (row
== var
->index
)
1396 return isl_int_sgn(tab
->mat
->row
[var
->index
][1]);
1397 pivot_var
= var_from_col(tab
, col
);
1398 if (isl_tab_pivot(tab
, row
, col
) < 0)
1400 if (var
->is_redundant
)
1403 if (pivot_var
&& var
->is_nonneg
) {
1404 /* pivot back to non-negative value */
1405 if (!pivot_var
->is_redundant
&& pivot_var
->index
== row
) {
1406 if (isl_tab_pivot(tab
, row
, col
) < 0)
1409 if (restore_row(tab
, var
) < -1)
1415 static int row_at_most_neg_one(struct isl_tab
*tab
, int row
)
1418 if (isl_int_is_pos(tab
->mat
->row
[row
][2]))
1420 if (isl_int_is_neg(tab
->mat
->row
[row
][2]))
1423 return isl_int_is_neg(tab
->mat
->row
[row
][1]) &&
1424 isl_int_abs_ge(tab
->mat
->row
[row
][1],
1425 tab
->mat
->row
[row
][0]);
1428 /* Return 1 if "var" can attain values <= -1.
1429 * Return 0 otherwise.
1431 * If the variable "var" is supposed to be non-negative (is_nonneg is set),
1432 * then the sample value of "var" is assumed to be non-negative when the
1433 * the function is called. If 1 is returned then the constraint
1434 * is not redundant and the sample value is made non-negative again before
1435 * the function returns.
1437 int isl_tab_min_at_most_neg_one(struct isl_tab
*tab
, struct isl_tab_var
*var
)
1440 struct isl_tab_var
*pivot_var
;
1442 if (min_is_manifestly_unbounded(tab
, var
))
1446 row
= pivot_row(tab
, NULL
, -1, col
);
1447 pivot_var
= var_from_col(tab
, col
);
1448 if (isl_tab_pivot(tab
, row
, col
) < 0)
1450 if (var
->is_redundant
)
1452 if (row_at_most_neg_one(tab
, var
->index
)) {
1453 if (var
->is_nonneg
) {
1454 if (!pivot_var
->is_redundant
&&
1455 pivot_var
->index
== row
) {
1456 if (isl_tab_pivot(tab
, row
, col
) < 0)
1459 if (restore_row(tab
, var
) < -1)
1465 if (var
->is_redundant
)
1468 find_pivot(tab
, var
, var
, -1, &row
, &col
);
1469 if (row
== var
->index
) {
1470 if (var
->is_nonneg
&& restore_row(tab
, var
) < -1)
1476 pivot_var
= var_from_col(tab
, col
);
1477 if (isl_tab_pivot(tab
, row
, col
) < 0)
1479 if (var
->is_redundant
)
1481 } while (!row_at_most_neg_one(tab
, var
->index
));
1482 if (var
->is_nonneg
) {
1483 /* pivot back to non-negative value */
1484 if (!pivot_var
->is_redundant
&& pivot_var
->index
== row
)
1485 if (isl_tab_pivot(tab
, row
, col
) < 0)
1487 if (restore_row(tab
, var
) < -1)
1493 /* Return 1 if "var" can attain values >= 1.
1494 * Return 0 otherwise.
1496 static int at_least_one(struct isl_tab
*tab
, struct isl_tab_var
*var
)
1501 if (max_is_manifestly_unbounded(tab
, var
))
1503 if (to_row(tab
, var
, 1) < 0)
1505 r
= tab
->mat
->row
[var
->index
];
1506 while (isl_int_lt(r
[1], r
[0])) {
1507 find_pivot(tab
, var
, var
, 1, &row
, &col
);
1509 return isl_int_ge(r
[1], r
[0]);
1510 if (row
== var
->index
) /* manifestly unbounded */
1512 if (isl_tab_pivot(tab
, row
, col
) < 0)
1518 static void swap_cols(struct isl_tab
*tab
, int col1
, int col2
)
1521 unsigned off
= 2 + tab
->M
;
1522 t
= tab
->col_var
[col1
];
1523 tab
->col_var
[col1
] = tab
->col_var
[col2
];
1524 tab
->col_var
[col2
] = t
;
1525 var_from_col(tab
, col1
)->index
= col1
;
1526 var_from_col(tab
, col2
)->index
= col2
;
1527 tab
->mat
= isl_mat_swap_cols(tab
->mat
, off
+ col1
, off
+ col2
);
1530 /* Mark column with index "col" as representing a zero variable.
1531 * If we may need to undo the operation the column is kept,
1532 * but no longer considered.
1533 * Otherwise, the column is simply removed.
1535 * The column may be interchanged with some other column. If it
1536 * is interchanged with a later column, return 1. Otherwise return 0.
1537 * If the columns are checked in order in the calling function,
1538 * then a return value of 1 means that the column with the given
1539 * column number may now contain a different column that
1540 * hasn't been checked yet.
1542 int isl_tab_kill_col(struct isl_tab
*tab
, int col
)
1544 var_from_col(tab
, col
)->is_zero
= 1;
1545 if (tab
->need_undo
) {
1546 if (isl_tab_push_var(tab
, isl_tab_undo_zero
,
1547 var_from_col(tab
, col
)) < 0)
1549 if (col
!= tab
->n_dead
)
1550 swap_cols(tab
, col
, tab
->n_dead
);
1554 if (col
!= tab
->n_col
- 1)
1555 swap_cols(tab
, col
, tab
->n_col
- 1);
1556 var_from_col(tab
, tab
->n_col
- 1)->index
= -1;
1562 static int row_is_manifestly_non_integral(struct isl_tab
*tab
, int row
)
1564 unsigned off
= 2 + tab
->M
;
1566 if (tab
->M
&& !isl_int_eq(tab
->mat
->row
[row
][2],
1567 tab
->mat
->row
[row
][0]))
1569 if (isl_seq_first_non_zero(tab
->mat
->row
[row
] + off
+ tab
->n_dead
,
1570 tab
->n_col
- tab
->n_dead
) != -1)
1573 return !isl_int_is_divisible_by(tab
->mat
->row
[row
][1],
1574 tab
->mat
->row
[row
][0]);
1577 /* For integer tableaus, check if any of the coordinates are stuck
1578 * at a non-integral value.
1580 static int tab_is_manifestly_empty(struct isl_tab
*tab
)
1589 for (i
= 0; i
< tab
->n_var
; ++i
) {
1590 if (!tab
->var
[i
].is_row
)
1592 if (row_is_manifestly_non_integral(tab
, tab
->var
[i
].index
))
1599 /* Row variable "var" is non-negative and cannot attain any values
1600 * larger than zero. This means that the coefficients of the unrestricted
1601 * column variables are zero and that the coefficients of the non-negative
1602 * column variables are zero or negative.
1603 * Each of the non-negative variables with a negative coefficient can
1604 * then also be written as the negative sum of non-negative variables
1605 * and must therefore also be zero.
1607 * If "temp_var" is set, then "var" is a temporary variable that
1608 * will be removed after this function returns and for which
1609 * no information is recorded on the undo stack.
1610 * Do not add any undo records involving this variable in this case
1611 * since the variable will have been removed before any future undo
1612 * operations. Also avoid marking the variable as redundant,
1613 * since that either adds an undo record or needlessly removes the row
1614 * (the caller will take care of removing the row).
1616 static isl_stat
close_row(struct isl_tab
*tab
, struct isl_tab_var
*var
,
1617 int temp_var
) WARN_UNUSED
;
1618 static isl_stat
close_row(struct isl_tab
*tab
, struct isl_tab_var
*var
,
1622 struct isl_mat
*mat
= tab
->mat
;
1623 unsigned off
= 2 + tab
->M
;
1625 if (!var
->is_nonneg
)
1626 isl_die(isl_tab_get_ctx(tab
), isl_error_internal
,
1627 "expecting non-negative variable",
1628 return isl_stat_error
);
1630 if (!temp_var
&& tab
->need_undo
)
1631 if (isl_tab_push_var(tab
, isl_tab_undo_zero
, var
) < 0)
1632 return isl_stat_error
;
1633 for (j
= tab
->n_dead
; j
< tab
->n_col
; ++j
) {
1635 if (isl_int_is_zero(mat
->row
[var
->index
][off
+ j
]))
1637 if (isl_int_is_pos(mat
->row
[var
->index
][off
+ j
]))
1638 isl_die(isl_tab_get_ctx(tab
), isl_error_internal
,
1639 "row cannot have positive coefficients",
1640 return isl_stat_error
);
1641 recheck
= isl_tab_kill_col(tab
, j
);
1643 return isl_stat_error
;
1647 if (!temp_var
&& isl_tab_mark_redundant(tab
, var
->index
) < 0)
1648 return isl_stat_error
;
1649 if (tab_is_manifestly_empty(tab
) && isl_tab_mark_empty(tab
) < 0)
1650 return isl_stat_error
;
1654 /* Add a constraint to the tableau and allocate a row for it.
1655 * Return the index into the constraint array "con".
1657 * This function assumes that at least one more row and at least
1658 * one more element in the constraint array are available in the tableau.
1660 int isl_tab_allocate_con(struct isl_tab
*tab
)
1664 isl_assert(tab
->mat
->ctx
, tab
->n_row
< tab
->mat
->n_row
, return -1);
1665 isl_assert(tab
->mat
->ctx
, tab
->n_con
< tab
->max_con
, return -1);
1668 tab
->con
[r
].index
= tab
->n_row
;
1669 tab
->con
[r
].is_row
= 1;
1670 tab
->con
[r
].is_nonneg
= 0;
1671 tab
->con
[r
].is_zero
= 0;
1672 tab
->con
[r
].is_redundant
= 0;
1673 tab
->con
[r
].frozen
= 0;
1674 tab
->con
[r
].negated
= 0;
1675 tab
->row_var
[tab
->n_row
] = ~r
;
1679 if (isl_tab_push_var(tab
, isl_tab_undo_allocate
, &tab
->con
[r
]) < 0)
1685 /* Move the entries in tab->var up one position, starting at "first",
1686 * creating room for an extra entry at position "first".
1687 * Since some of the entries of tab->row_var and tab->col_var contain
1688 * indices into this array, they have to be updated accordingly.
1690 static int var_insert_entry(struct isl_tab
*tab
, int first
)
1694 if (tab
->n_var
>= tab
->max_var
)
1695 isl_die(isl_tab_get_ctx(tab
), isl_error_internal
,
1696 "not enough room for new variable", return -1);
1697 if (first
> tab
->n_var
)
1698 isl_die(isl_tab_get_ctx(tab
), isl_error_internal
,
1699 "invalid initial position", return -1);
1701 for (i
= tab
->n_var
- 1; i
>= first
; --i
) {
1702 tab
->var
[i
+ 1] = tab
->var
[i
];
1703 if (tab
->var
[i
+ 1].is_row
)
1704 tab
->row_var
[tab
->var
[i
+ 1].index
]++;
1706 tab
->col_var
[tab
->var
[i
+ 1].index
]++;
1714 /* Drop the entry at position "first" in tab->var, moving all
1715 * subsequent entries down.
1716 * Since some of the entries of tab->row_var and tab->col_var contain
1717 * indices into this array, they have to be updated accordingly.
1719 static int var_drop_entry(struct isl_tab
*tab
, int first
)
1723 if (first
>= tab
->n_var
)
1724 isl_die(isl_tab_get_ctx(tab
), isl_error_internal
,
1725 "invalid initial position", return -1);
1729 for (i
= first
; i
< tab
->n_var
; ++i
) {
1730 tab
->var
[i
] = tab
->var
[i
+ 1];
1731 if (tab
->var
[i
+ 1].is_row
)
1732 tab
->row_var
[tab
->var
[i
].index
]--;
1734 tab
->col_var
[tab
->var
[i
].index
]--;
1740 /* Add a variable to the tableau at position "r" and allocate a column for it.
1741 * Return the index into the variable array "var", i.e., "r",
1744 int isl_tab_insert_var(struct isl_tab
*tab
, int r
)
1747 unsigned off
= 2 + tab
->M
;
1749 isl_assert(tab
->mat
->ctx
, tab
->n_col
< tab
->mat
->n_col
, return -1);
1751 if (var_insert_entry(tab
, r
) < 0)
1754 tab
->var
[r
].index
= tab
->n_col
;
1755 tab
->var
[r
].is_row
= 0;
1756 tab
->var
[r
].is_nonneg
= 0;
1757 tab
->var
[r
].is_zero
= 0;
1758 tab
->var
[r
].is_redundant
= 0;
1759 tab
->var
[r
].frozen
= 0;
1760 tab
->var
[r
].negated
= 0;
1761 tab
->col_var
[tab
->n_col
] = r
;
1763 for (i
= 0; i
< tab
->n_row
; ++i
)
1764 isl_int_set_si(tab
->mat
->row
[i
][off
+ tab
->n_col
], 0);
1767 if (isl_tab_push_var(tab
, isl_tab_undo_allocate
, &tab
->var
[r
]) < 0)
1773 /* Add a variable to the tableau and allocate a column for it.
1774 * Return the index into the variable array "var".
1776 int isl_tab_allocate_var(struct isl_tab
*tab
)
1781 return isl_tab_insert_var(tab
, tab
->n_var
);
1784 /* Add a row to the tableau. The row is given as an affine combination
1785 * of the original variables and needs to be expressed in terms of the
1788 * This function assumes that at least one more row and at least
1789 * one more element in the constraint array are available in the tableau.
1791 * We add each term in turn.
1792 * If r = n/d_r is the current sum and we need to add k x, then
1793 * if x is a column variable, we increase the numerator of
1794 * this column by k d_r
1795 * if x = f/d_x is a row variable, then the new representation of r is
1797 * n k f d_x/g n + d_r/g k f m/d_r n + m/d_g k f
1798 * --- + --- = ------------------- = -------------------
1799 * d_r d_r d_r d_x/g m
1801 * with g the gcd of d_r and d_x and m the lcm of d_r and d_x.
1803 * If tab->M is set, then, internally, each variable x is represented
1804 * as x' - M. We then also need no subtract k d_r from the coefficient of M.
1806 int isl_tab_add_row(struct isl_tab
*tab
, isl_int
*line
)
1812 unsigned off
= 2 + tab
->M
;
1814 r
= isl_tab_allocate_con(tab
);
1820 row
= tab
->mat
->row
[tab
->con
[r
].index
];
1821 isl_int_set_si(row
[0], 1);
1822 isl_int_set(row
[1], line
[0]);
1823 isl_seq_clr(row
+ 2, tab
->M
+ tab
->n_col
);
1824 for (i
= 0; i
< tab
->n_var
; ++i
) {
1825 if (tab
->var
[i
].is_zero
)
1827 if (tab
->var
[i
].is_row
) {
1829 row
[0], tab
->mat
->row
[tab
->var
[i
].index
][0]);
1830 isl_int_swap(a
, row
[0]);
1831 isl_int_divexact(a
, row
[0], a
);
1833 row
[0], tab
->mat
->row
[tab
->var
[i
].index
][0]);
1834 isl_int_mul(b
, b
, line
[1 + i
]);
1835 isl_seq_combine(row
+ 1, a
, row
+ 1,
1836 b
, tab
->mat
->row
[tab
->var
[i
].index
] + 1,
1837 1 + tab
->M
+ tab
->n_col
);
1839 isl_int_addmul(row
[off
+ tab
->var
[i
].index
],
1840 line
[1 + i
], row
[0]);
1841 if (tab
->M
&& i
>= tab
->n_param
&& i
< tab
->n_var
- tab
->n_div
)
1842 isl_int_submul(row
[2], line
[1 + i
], row
[0]);
1844 isl_seq_normalize(tab
->mat
->ctx
, row
, off
+ tab
->n_col
);
1849 tab
->row_sign
[tab
->con
[r
].index
] = isl_tab_row_unknown
;
1854 static int drop_row(struct isl_tab
*tab
, int row
)
1856 isl_assert(tab
->mat
->ctx
, ~tab
->row_var
[row
] == tab
->n_con
- 1, return -1);
1857 if (row
!= tab
->n_row
- 1)
1858 swap_rows(tab
, row
, tab
->n_row
- 1);
1864 /* Drop the variable in column "col" along with the column.
1865 * The column is removed first because it may need to be moved
1866 * into the last position and this process requires
1867 * the contents of the col_var array in a state
1868 * before the removal of the variable.
1870 static int drop_col(struct isl_tab
*tab
, int col
)
1874 var
= tab
->col_var
[col
];
1875 if (col
!= tab
->n_col
- 1)
1876 swap_cols(tab
, col
, tab
->n_col
- 1);
1878 if (var_drop_entry(tab
, var
) < 0)
1883 /* Add inequality "ineq" and check if it conflicts with the
1884 * previously added constraints or if it is obviously redundant.
1886 * This function assumes that at least one more row and at least
1887 * one more element in the constraint array are available in the tableau.
1889 int isl_tab_add_ineq(struct isl_tab
*tab
, isl_int
*ineq
)
1898 struct isl_basic_map
*bmap
= tab
->bmap
;
1900 isl_assert(tab
->mat
->ctx
, tab
->n_eq
== bmap
->n_eq
, return -1);
1901 isl_assert(tab
->mat
->ctx
,
1902 tab
->n_con
== bmap
->n_eq
+ bmap
->n_ineq
, return -1);
1903 tab
->bmap
= isl_basic_map_add_ineq(tab
->bmap
, ineq
);
1904 if (isl_tab_push(tab
, isl_tab_undo_bmap_ineq
) < 0)
1911 isl_int_set_si(cst
, 0);
1912 isl_int_swap(ineq
[0], cst
);
1914 r
= isl_tab_add_row(tab
, ineq
);
1916 isl_int_swap(ineq
[0], cst
);
1921 tab
->con
[r
].is_nonneg
= 1;
1922 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
1924 if (isl_tab_row_is_redundant(tab
, tab
->con
[r
].index
)) {
1925 if (isl_tab_mark_redundant(tab
, tab
->con
[r
].index
) < 0)
1930 sgn
= restore_row(tab
, &tab
->con
[r
]);
1934 return isl_tab_mark_empty(tab
);
1935 if (tab
->con
[r
].is_row
&& isl_tab_row_is_redundant(tab
, tab
->con
[r
].index
))
1936 if (isl_tab_mark_redundant(tab
, tab
->con
[r
].index
) < 0)
1941 /* Pivot a non-negative variable down until it reaches the value zero
1942 * and then pivot the variable into a column position.
1944 static int to_col(struct isl_tab
*tab
, struct isl_tab_var
*var
) WARN_UNUSED
;
1945 static int to_col(struct isl_tab
*tab
, struct isl_tab_var
*var
)
1949 unsigned off
= 2 + tab
->M
;
1954 while (isl_int_is_pos(tab
->mat
->row
[var
->index
][1])) {
1955 find_pivot(tab
, var
, NULL
, -1, &row
, &col
);
1956 isl_assert(tab
->mat
->ctx
, row
!= -1, return -1);
1957 if (isl_tab_pivot(tab
, row
, col
) < 0)
1963 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
)
1964 if (!isl_int_is_zero(tab
->mat
->row
[var
->index
][off
+ i
]))
1967 isl_assert(tab
->mat
->ctx
, i
< tab
->n_col
, return -1);
1968 if (isl_tab_pivot(tab
, var
->index
, i
) < 0)
1974 /* We assume Gaussian elimination has been performed on the equalities.
1975 * The equalities can therefore never conflict.
1976 * Adding the equalities is currently only really useful for a later call
1977 * to isl_tab_ineq_type.
1979 * This function assumes that at least one more row and at least
1980 * one more element in the constraint array are available in the tableau.
1982 static struct isl_tab
*add_eq(struct isl_tab
*tab
, isl_int
*eq
)
1989 r
= isl_tab_add_row(tab
, eq
);
1993 r
= tab
->con
[r
].index
;
1994 i
= isl_seq_first_non_zero(tab
->mat
->row
[r
] + 2 + tab
->M
+ tab
->n_dead
,
1995 tab
->n_col
- tab
->n_dead
);
1996 isl_assert(tab
->mat
->ctx
, i
>= 0, goto error
);
1998 if (isl_tab_pivot(tab
, r
, i
) < 0)
2000 if (isl_tab_kill_col(tab
, i
) < 0)
2010 static int row_is_manifestly_zero(struct isl_tab
*tab
, int row
)
2012 unsigned off
= 2 + tab
->M
;
2014 if (!isl_int_is_zero(tab
->mat
->row
[row
][1]))
2016 if (tab
->M
&& !isl_int_is_zero(tab
->mat
->row
[row
][2]))
2018 return isl_seq_first_non_zero(tab
->mat
->row
[row
] + off
+ tab
->n_dead
,
2019 tab
->n_col
- tab
->n_dead
) == -1;
2022 /* Add an equality that is known to be valid for the given tableau.
2024 * This function assumes that at least one more row and at least
2025 * one more element in the constraint array are available in the tableau.
2027 int isl_tab_add_valid_eq(struct isl_tab
*tab
, isl_int
*eq
)
2029 struct isl_tab_var
*var
;
2034 r
= isl_tab_add_row(tab
, eq
);
2040 if (row_is_manifestly_zero(tab
, r
)) {
2042 if (isl_tab_mark_redundant(tab
, r
) < 0)
2047 if (isl_int_is_neg(tab
->mat
->row
[r
][1])) {
2048 isl_seq_neg(tab
->mat
->row
[r
] + 1, tab
->mat
->row
[r
] + 1,
2053 if (to_col(tab
, var
) < 0)
2056 if (isl_tab_kill_col(tab
, var
->index
) < 0)
2062 /* Add a zero row to "tab" and return the corresponding index
2063 * in the constraint array.
2065 * This function assumes that at least one more row and at least
2066 * one more element in the constraint array are available in the tableau.
2068 static int add_zero_row(struct isl_tab
*tab
)
2073 r
= isl_tab_allocate_con(tab
);
2077 row
= tab
->mat
->row
[tab
->con
[r
].index
];
2078 isl_seq_clr(row
+ 1, 1 + tab
->M
+ tab
->n_col
);
2079 isl_int_set_si(row
[0], 1);
2084 /* Add equality "eq" and check if it conflicts with the
2085 * previously added constraints or if it is obviously redundant.
2087 * This function assumes that at least one more row and at least
2088 * one more element in the constraint array are available in the tableau.
2089 * If tab->bmap is set, then two rows are needed instead of one.
2091 int isl_tab_add_eq(struct isl_tab
*tab
, isl_int
*eq
)
2093 struct isl_tab_undo
*snap
= NULL
;
2094 struct isl_tab_var
*var
;
2102 isl_assert(tab
->mat
->ctx
, !tab
->M
, return -1);
2105 snap
= isl_tab_snap(tab
);
2109 isl_int_set_si(cst
, 0);
2110 isl_int_swap(eq
[0], cst
);
2112 r
= isl_tab_add_row(tab
, eq
);
2114 isl_int_swap(eq
[0], cst
);
2122 if (row_is_manifestly_zero(tab
, row
)) {
2124 return isl_tab_rollback(tab
, snap
);
2125 return drop_row(tab
, row
);
2129 tab
->bmap
= isl_basic_map_add_ineq(tab
->bmap
, eq
);
2130 if (isl_tab_push(tab
, isl_tab_undo_bmap_ineq
) < 0)
2132 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
2133 tab
->bmap
= isl_basic_map_add_ineq(tab
->bmap
, eq
);
2134 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
2135 if (isl_tab_push(tab
, isl_tab_undo_bmap_ineq
) < 0)
2139 if (add_zero_row(tab
) < 0)
2143 sgn
= isl_int_sgn(tab
->mat
->row
[row
][1]);
2146 isl_seq_neg(tab
->mat
->row
[row
] + 1, tab
->mat
->row
[row
] + 1,
2153 sgn
= sign_of_max(tab
, var
);
2157 if (isl_tab_mark_empty(tab
) < 0)
2164 if (to_col(tab
, var
) < 0)
2167 if (isl_tab_kill_col(tab
, var
->index
) < 0)
2173 /* Construct and return an inequality that expresses an upper bound
2175 * In particular, if the div is given by
2179 * then the inequality expresses
2183 static struct isl_vec
*ineq_for_div(struct isl_basic_map
*bmap
, unsigned div
)
2187 struct isl_vec
*ineq
;
2192 total
= isl_basic_map_total_dim(bmap
);
2193 div_pos
= 1 + total
- bmap
->n_div
+ div
;
2195 ineq
= isl_vec_alloc(bmap
->ctx
, 1 + total
);
2199 isl_seq_cpy(ineq
->el
, bmap
->div
[div
] + 1, 1 + total
);
2200 isl_int_neg(ineq
->el
[div_pos
], bmap
->div
[div
][0]);
2204 /* For a div d = floor(f/m), add the constraints
2207 * -(f-(m-1)) + m d >= 0
2209 * Note that the second constraint is the negation of
2213 * If add_ineq is not NULL, then this function is used
2214 * instead of isl_tab_add_ineq to effectively add the inequalities.
2216 * This function assumes that at least two more rows and at least
2217 * two more elements in the constraint array are available in the tableau.
2219 static int add_div_constraints(struct isl_tab
*tab
, unsigned div
,
2220 int (*add_ineq
)(void *user
, isl_int
*), void *user
)
2224 struct isl_vec
*ineq
;
2226 total
= isl_basic_map_total_dim(tab
->bmap
);
2227 div_pos
= 1 + total
- tab
->bmap
->n_div
+ div
;
2229 ineq
= ineq_for_div(tab
->bmap
, div
);
2234 if (add_ineq(user
, ineq
->el
) < 0)
2237 if (isl_tab_add_ineq(tab
, ineq
->el
) < 0)
2241 isl_seq_neg(ineq
->el
, tab
->bmap
->div
[div
] + 1, 1 + total
);
2242 isl_int_set(ineq
->el
[div_pos
], tab
->bmap
->div
[div
][0]);
2243 isl_int_add(ineq
->el
[0], ineq
->el
[0], ineq
->el
[div_pos
]);
2244 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
2247 if (add_ineq(user
, ineq
->el
) < 0)
2250 if (isl_tab_add_ineq(tab
, ineq
->el
) < 0)
2262 /* Check whether the div described by "div" is obviously non-negative.
2263 * If we are using a big parameter, then we will encode the div
2264 * as div' = M + div, which is always non-negative.
2265 * Otherwise, we check whether div is a non-negative affine combination
2266 * of non-negative variables.
2268 static int div_is_nonneg(struct isl_tab
*tab
, __isl_keep isl_vec
*div
)
2275 if (isl_int_is_neg(div
->el
[1]))
2278 for (i
= 0; i
< tab
->n_var
; ++i
) {
2279 if (isl_int_is_neg(div
->el
[2 + i
]))
2281 if (isl_int_is_zero(div
->el
[2 + i
]))
2283 if (!tab
->var
[i
].is_nonneg
)
2290 /* Insert an extra div, prescribed by "div", to the tableau and
2291 * the associated bmap (which is assumed to be non-NULL).
2292 * The extra integer division is inserted at (tableau) position "pos".
2293 * Return "pos" or -1 if an error occurred.
2295 * If add_ineq is not NULL, then this function is used instead
2296 * of isl_tab_add_ineq to add the div constraints.
2297 * This complication is needed because the code in isl_tab_pip
2298 * wants to perform some extra processing when an inequality
2299 * is added to the tableau.
2301 int isl_tab_insert_div(struct isl_tab
*tab
, int pos
, __isl_keep isl_vec
*div
,
2302 int (*add_ineq
)(void *user
, isl_int
*), void *user
)
2311 if (div
->size
!= 1 + 1 + tab
->n_var
)
2312 isl_die(isl_tab_get_ctx(tab
), isl_error_invalid
,
2313 "unexpected size", return -1);
2315 isl_assert(tab
->mat
->ctx
, tab
->bmap
, return -1);
2316 n_div
= isl_basic_map_dim(tab
->bmap
, isl_dim_div
);
2317 o_div
= tab
->n_var
- n_div
;
2318 if (pos
< o_div
|| pos
> tab
->n_var
)
2319 isl_die(isl_tab_get_ctx(tab
), isl_error_invalid
,
2320 "invalid position", return -1);
2322 nonneg
= div_is_nonneg(tab
, div
);
2324 if (isl_tab_extend_cons(tab
, 3) < 0)
2326 if (isl_tab_extend_vars(tab
, 1) < 0)
2328 r
= isl_tab_insert_var(tab
, pos
);
2333 tab
->var
[r
].is_nonneg
= 1;
2335 tab
->bmap
= isl_basic_map_insert_div(tab
->bmap
, pos
- o_div
, div
);
2338 if (isl_tab_push_var(tab
, isl_tab_undo_bmap_div
, &tab
->var
[r
]) < 0)
2341 if (add_div_constraints(tab
, pos
- o_div
, add_ineq
, user
) < 0)
2347 /* Add an extra div, prescribed by "div", to the tableau and
2348 * the associated bmap (which is assumed to be non-NULL).
2350 int isl_tab_add_div(struct isl_tab
*tab
, __isl_keep isl_vec
*div
)
2354 return isl_tab_insert_div(tab
, tab
->n_var
, div
, NULL
, NULL
);
2357 /* If "track" is set, then we want to keep track of all constraints in tab
2358 * in its bmap field. This field is initialized from a copy of "bmap",
2359 * so we need to make sure that all constraints in "bmap" also appear
2360 * in the constructed tab.
2362 __isl_give
struct isl_tab
*isl_tab_from_basic_map(
2363 __isl_keep isl_basic_map
*bmap
, int track
)
2366 struct isl_tab
*tab
;
2370 tab
= isl_tab_alloc(bmap
->ctx
,
2371 isl_basic_map_total_dim(bmap
) + bmap
->n_ineq
+ 1,
2372 isl_basic_map_total_dim(bmap
), 0);
2375 tab
->preserve
= track
;
2376 tab
->rational
= ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
2377 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
)) {
2378 if (isl_tab_mark_empty(tab
) < 0)
2382 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
2383 tab
= add_eq(tab
, bmap
->eq
[i
]);
2387 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
2388 if (isl_tab_add_ineq(tab
, bmap
->ineq
[i
]) < 0)
2394 if (track
&& isl_tab_track_bmap(tab
, isl_basic_map_copy(bmap
)) < 0)
2402 __isl_give
struct isl_tab
*isl_tab_from_basic_set(
2403 __isl_keep isl_basic_set
*bset
, int track
)
2405 return isl_tab_from_basic_map(bset
, track
);
2408 /* Construct a tableau corresponding to the recession cone of "bset".
2410 struct isl_tab
*isl_tab_from_recession_cone(__isl_keep isl_basic_set
*bset
,
2415 struct isl_tab
*tab
;
2416 unsigned offset
= 0;
2421 offset
= isl_basic_set_dim(bset
, isl_dim_param
);
2422 tab
= isl_tab_alloc(bset
->ctx
, bset
->n_eq
+ bset
->n_ineq
,
2423 isl_basic_set_total_dim(bset
) - offset
, 0);
2426 tab
->rational
= ISL_F_ISSET(bset
, ISL_BASIC_SET_RATIONAL
);
2430 isl_int_set_si(cst
, 0);
2431 for (i
= 0; i
< bset
->n_eq
; ++i
) {
2432 isl_int_swap(bset
->eq
[i
][offset
], cst
);
2434 if (isl_tab_add_eq(tab
, bset
->eq
[i
] + offset
) < 0)
2437 tab
= add_eq(tab
, bset
->eq
[i
]);
2438 isl_int_swap(bset
->eq
[i
][offset
], cst
);
2442 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
2444 isl_int_swap(bset
->ineq
[i
][offset
], cst
);
2445 r
= isl_tab_add_row(tab
, bset
->ineq
[i
] + offset
);
2446 isl_int_swap(bset
->ineq
[i
][offset
], cst
);
2449 tab
->con
[r
].is_nonneg
= 1;
2450 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
2462 /* Assuming "tab" is the tableau of a cone, check if the cone is
2463 * bounded, i.e., if it is empty or only contains the origin.
2465 int isl_tab_cone_is_bounded(struct isl_tab
*tab
)
2473 if (tab
->n_dead
== tab
->n_col
)
2477 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
2478 struct isl_tab_var
*var
;
2480 var
= isl_tab_var_from_row(tab
, i
);
2481 if (!var
->is_nonneg
)
2483 sgn
= sign_of_max(tab
, var
);
2488 if (close_row(tab
, var
, 0) < 0)
2492 if (tab
->n_dead
== tab
->n_col
)
2494 if (i
== tab
->n_row
)
2499 int isl_tab_sample_is_integer(struct isl_tab
*tab
)
2506 for (i
= 0; i
< tab
->n_var
; ++i
) {
2508 if (!tab
->var
[i
].is_row
)
2510 row
= tab
->var
[i
].index
;
2511 if (!isl_int_is_divisible_by(tab
->mat
->row
[row
][1],
2512 tab
->mat
->row
[row
][0]))
2518 static struct isl_vec
*extract_integer_sample(struct isl_tab
*tab
)
2521 struct isl_vec
*vec
;
2523 vec
= isl_vec_alloc(tab
->mat
->ctx
, 1 + tab
->n_var
);
2527 isl_int_set_si(vec
->block
.data
[0], 1);
2528 for (i
= 0; i
< tab
->n_var
; ++i
) {
2529 if (!tab
->var
[i
].is_row
)
2530 isl_int_set_si(vec
->block
.data
[1 + i
], 0);
2532 int row
= tab
->var
[i
].index
;
2533 isl_int_divexact(vec
->block
.data
[1 + i
],
2534 tab
->mat
->row
[row
][1], tab
->mat
->row
[row
][0]);
2541 struct isl_vec
*isl_tab_get_sample_value(struct isl_tab
*tab
)
2544 struct isl_vec
*vec
;
2550 vec
= isl_vec_alloc(tab
->mat
->ctx
, 1 + tab
->n_var
);
2556 isl_int_set_si(vec
->block
.data
[0], 1);
2557 for (i
= 0; i
< tab
->n_var
; ++i
) {
2559 if (!tab
->var
[i
].is_row
) {
2560 isl_int_set_si(vec
->block
.data
[1 + i
], 0);
2563 row
= tab
->var
[i
].index
;
2564 isl_int_gcd(m
, vec
->block
.data
[0], tab
->mat
->row
[row
][0]);
2565 isl_int_divexact(m
, tab
->mat
->row
[row
][0], m
);
2566 isl_seq_scale(vec
->block
.data
, vec
->block
.data
, m
, 1 + i
);
2567 isl_int_divexact(m
, vec
->block
.data
[0], tab
->mat
->row
[row
][0]);
2568 isl_int_mul(vec
->block
.data
[1 + i
], m
, tab
->mat
->row
[row
][1]);
2570 vec
= isl_vec_normalize(vec
);
2576 /* Update "bmap" based on the results of the tableau "tab".
2577 * In particular, implicit equalities are made explicit, redundant constraints
2578 * are removed and if the sample value happens to be integer, it is stored
2579 * in "bmap" (unless "bmap" already had an integer sample).
2581 * The tableau is assumed to have been created from "bmap" using
2582 * isl_tab_from_basic_map.
2584 struct isl_basic_map
*isl_basic_map_update_from_tab(struct isl_basic_map
*bmap
,
2585 struct isl_tab
*tab
)
2597 bmap
= isl_basic_map_set_to_empty(bmap
);
2599 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
2600 if (isl_tab_is_equality(tab
, n_eq
+ i
))
2601 isl_basic_map_inequality_to_equality(bmap
, i
);
2602 else if (isl_tab_is_redundant(tab
, n_eq
+ i
))
2603 isl_basic_map_drop_inequality(bmap
, i
);
2605 if (bmap
->n_eq
!= n_eq
)
2606 bmap
= isl_basic_map_gauss(bmap
, NULL
);
2607 if (!tab
->rational
&&
2608 bmap
&& !bmap
->sample
&& isl_tab_sample_is_integer(tab
))
2609 bmap
->sample
= extract_integer_sample(tab
);
2613 struct isl_basic_set
*isl_basic_set_update_from_tab(struct isl_basic_set
*bset
,
2614 struct isl_tab
*tab
)
2616 return (struct isl_basic_set
*)isl_basic_map_update_from_tab(
2617 (struct isl_basic_map
*)bset
, tab
);
2620 /* Drop the last constraint added to "tab" in position "r".
2621 * The constraint is expected to have remained in a row.
2623 static isl_stat
drop_last_con_in_row(struct isl_tab
*tab
, int r
)
2625 if (!tab
->con
[r
].is_row
)
2626 isl_die(isl_tab_get_ctx(tab
), isl_error_internal
,
2627 "row unexpectedly moved to column",
2628 return isl_stat_error
);
2629 if (r
+ 1 != tab
->n_con
)
2630 isl_die(isl_tab_get_ctx(tab
), isl_error_internal
,
2631 "additional constraints added", return isl_stat_error
);
2632 if (drop_row(tab
, tab
->con
[r
].index
) < 0)
2633 return isl_stat_error
;
2638 /* Given a non-negative variable "var", temporarily add a new non-negative
2639 * variable that is the opposite of "var", ensuring that "var" can only attain
2640 * the value zero. The new variable is removed again before this function
2641 * returns. However, the effect of forcing "var" to be zero remains.
2642 * If var = n/d is a row variable, then the new variable = -n/d.
2643 * If var is a column variables, then the new variable = -var.
2644 * If the new variable cannot attain non-negative values, then
2645 * the resulting tableau is empty.
2646 * Otherwise, we know the value will be zero and we close the row.
2648 static isl_stat
cut_to_hyperplane(struct isl_tab
*tab
, struct isl_tab_var
*var
)
2653 unsigned off
= 2 + tab
->M
;
2657 if (var
->is_redundant
|| !var
->is_nonneg
)
2658 isl_die(isl_tab_get_ctx(tab
), isl_error_invalid
,
2659 "expecting non-redundant non-negative variable",
2660 return isl_stat_error
);
2662 if (isl_tab_extend_cons(tab
, 1) < 0)
2663 return isl_stat_error
;
2666 tab
->con
[r
].index
= tab
->n_row
;
2667 tab
->con
[r
].is_row
= 1;
2668 tab
->con
[r
].is_nonneg
= 0;
2669 tab
->con
[r
].is_zero
= 0;
2670 tab
->con
[r
].is_redundant
= 0;
2671 tab
->con
[r
].frozen
= 0;
2672 tab
->con
[r
].negated
= 0;
2673 tab
->row_var
[tab
->n_row
] = ~r
;
2674 row
= tab
->mat
->row
[tab
->n_row
];
2677 isl_int_set(row
[0], tab
->mat
->row
[var
->index
][0]);
2678 isl_seq_neg(row
+ 1,
2679 tab
->mat
->row
[var
->index
] + 1, 1 + tab
->n_col
);
2681 isl_int_set_si(row
[0], 1);
2682 isl_seq_clr(row
+ 1, 1 + tab
->n_col
);
2683 isl_int_set_si(row
[off
+ var
->index
], -1);
2689 sgn
= sign_of_max(tab
, &tab
->con
[r
]);
2691 return isl_stat_error
;
2693 if (drop_last_con_in_row(tab
, r
) < 0)
2694 return isl_stat_error
;
2695 if (isl_tab_mark_empty(tab
) < 0)
2696 return isl_stat_error
;
2699 tab
->con
[r
].is_nonneg
= 1;
2701 if (close_row(tab
, &tab
->con
[r
], 1) < 0)
2702 return isl_stat_error
;
2703 if (drop_last_con_in_row(tab
, r
) < 0)
2704 return isl_stat_error
;
2709 /* Given a tableau "tab" and an inequality constraint "con" of the tableau,
2710 * relax the inequality by one. That is, the inequality r >= 0 is replaced
2711 * by r' = r + 1 >= 0.
2712 * If r is a row variable, we simply increase the constant term by one
2713 * (taking into account the denominator).
2714 * If r is a column variable, then we need to modify each row that
2715 * refers to r = r' - 1 by substituting this equality, effectively
2716 * subtracting the coefficient of the column from the constant.
2717 * We should only do this if the minimum is manifestly unbounded,
2718 * however. Otherwise, we may end up with negative sample values
2719 * for non-negative variables.
2720 * So, if r is a column variable with a minimum that is not
2721 * manifestly unbounded, then we need to move it to a row.
2722 * However, the sample value of this row may be negative,
2723 * even after the relaxation, so we need to restore it.
2724 * We therefore prefer to pivot a column up to a row, if possible.
2726 int isl_tab_relax(struct isl_tab
*tab
, int con
)
2728 struct isl_tab_var
*var
;
2733 var
= &tab
->con
[con
];
2735 if (var
->is_row
&& (var
->index
< 0 || var
->index
< tab
->n_redundant
))
2736 isl_die(tab
->mat
->ctx
, isl_error_invalid
,
2737 "cannot relax redundant constraint", return -1);
2738 if (!var
->is_row
&& (var
->index
< 0 || var
->index
< tab
->n_dead
))
2739 isl_die(tab
->mat
->ctx
, isl_error_invalid
,
2740 "cannot relax dead constraint", return -1);
2742 if (!var
->is_row
&& !max_is_manifestly_unbounded(tab
, var
))
2743 if (to_row(tab
, var
, 1) < 0)
2745 if (!var
->is_row
&& !min_is_manifestly_unbounded(tab
, var
))
2746 if (to_row(tab
, var
, -1) < 0)
2750 isl_int_add(tab
->mat
->row
[var
->index
][1],
2751 tab
->mat
->row
[var
->index
][1], tab
->mat
->row
[var
->index
][0]);
2752 if (restore_row(tab
, var
) < 0)
2756 unsigned off
= 2 + tab
->M
;
2758 for (i
= 0; i
< tab
->n_row
; ++i
) {
2759 if (isl_int_is_zero(tab
->mat
->row
[i
][off
+ var
->index
]))
2761 isl_int_sub(tab
->mat
->row
[i
][1], tab
->mat
->row
[i
][1],
2762 tab
->mat
->row
[i
][off
+ var
->index
]);
2767 if (isl_tab_push_var(tab
, isl_tab_undo_relax
, var
) < 0)
2773 /* Replace the variable v at position "pos" in the tableau "tab"
2774 * by v' = v + shift.
2776 * If the variable is in a column, then we first check if we can
2777 * simply plug in v = v' - shift. The effect on a row with
2778 * coefficient f/d for variable v is that the constant term c/d
2779 * is replaced by (c - f * shift)/d. If shift is positive and
2780 * f is negative for each row that needs to remain non-negative,
2781 * then this is clearly safe. In other words, if the minimum of v
2782 * is manifestly unbounded, then we can keep v in a column position.
2783 * Otherwise, we can pivot it down to a row.
2784 * Similarly, if shift is negative, we need to check if the maximum
2785 * of is manifestly unbounded.
2787 * If the variable is in a row (from the start or after pivoting),
2788 * then the constant term c/d is replaced by (c + d * shift)/d.
2790 int isl_tab_shift_var(struct isl_tab
*tab
, int pos
, isl_int shift
)
2792 struct isl_tab_var
*var
;
2796 if (isl_int_is_zero(shift
))
2799 var
= &tab
->var
[pos
];
2801 if (isl_int_is_neg(shift
)) {
2802 if (!max_is_manifestly_unbounded(tab
, var
))
2803 if (to_row(tab
, var
, 1) < 0)
2806 if (!min_is_manifestly_unbounded(tab
, var
))
2807 if (to_row(tab
, var
, -1) < 0)
2813 isl_int_addmul(tab
->mat
->row
[var
->index
][1],
2814 shift
, tab
->mat
->row
[var
->index
][0]);
2817 unsigned off
= 2 + tab
->M
;
2819 for (i
= 0; i
< tab
->n_row
; ++i
) {
2820 if (isl_int_is_zero(tab
->mat
->row
[i
][off
+ var
->index
]))
2822 isl_int_submul(tab
->mat
->row
[i
][1],
2823 shift
, tab
->mat
->row
[i
][off
+ var
->index
]);
2831 /* Remove the sign constraint from constraint "con".
2833 * If the constraint variable was originally marked non-negative,
2834 * then we make sure we mark it non-negative again during rollback.
2836 int isl_tab_unrestrict(struct isl_tab
*tab
, int con
)
2838 struct isl_tab_var
*var
;
2843 var
= &tab
->con
[con
];
2844 if (!var
->is_nonneg
)
2848 if (isl_tab_push_var(tab
, isl_tab_undo_unrestrict
, var
) < 0)
2854 int isl_tab_select_facet(struct isl_tab
*tab
, int con
)
2859 return cut_to_hyperplane(tab
, &tab
->con
[con
]);
2862 static int may_be_equality(struct isl_tab
*tab
, int row
)
2864 return tab
->rational
? isl_int_is_zero(tab
->mat
->row
[row
][1])
2865 : isl_int_lt(tab
->mat
->row
[row
][1],
2866 tab
->mat
->row
[row
][0]);
2869 /* Check for (near) equalities among the constraints.
2870 * A constraint is an equality if it is non-negative and if
2871 * its maximal value is either
2872 * - zero (in case of rational tableaus), or
2873 * - strictly less than 1 (in case of integer tableaus)
2875 * We first mark all non-redundant and non-dead variables that
2876 * are not frozen and not obviously not an equality.
2877 * Then we iterate over all marked variables if they can attain
2878 * any values larger than zero or at least one.
2879 * If the maximal value is zero, we mark any column variables
2880 * that appear in the row as being zero and mark the row as being redundant.
2881 * Otherwise, if the maximal value is strictly less than one (and the
2882 * tableau is integer), then we restrict the value to being zero
2883 * by adding an opposite non-negative variable.
2885 int isl_tab_detect_implicit_equalities(struct isl_tab
*tab
)
2894 if (tab
->n_dead
== tab
->n_col
)
2898 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
2899 struct isl_tab_var
*var
= isl_tab_var_from_row(tab
, i
);
2900 var
->marked
= !var
->frozen
&& var
->is_nonneg
&&
2901 may_be_equality(tab
, i
);
2905 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
2906 struct isl_tab_var
*var
= var_from_col(tab
, i
);
2907 var
->marked
= !var
->frozen
&& var
->is_nonneg
;
2912 struct isl_tab_var
*var
;
2914 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
2915 var
= isl_tab_var_from_row(tab
, i
);
2919 if (i
== tab
->n_row
) {
2920 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
2921 var
= var_from_col(tab
, i
);
2925 if (i
== tab
->n_col
)
2930 sgn
= sign_of_max(tab
, var
);
2934 if (close_row(tab
, var
, 0) < 0)
2936 } else if (!tab
->rational
&& !at_least_one(tab
, var
)) {
2937 if (cut_to_hyperplane(tab
, var
) < 0)
2939 return isl_tab_detect_implicit_equalities(tab
);
2941 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
2942 var
= isl_tab_var_from_row(tab
, i
);
2945 if (may_be_equality(tab
, i
))
2955 /* Update the element of row_var or col_var that corresponds to
2956 * constraint tab->con[i] to a move from position "old" to position "i".
2958 static int update_con_after_move(struct isl_tab
*tab
, int i
, int old
)
2963 index
= tab
->con
[i
].index
;
2966 p
= tab
->con
[i
].is_row
? tab
->row_var
: tab
->col_var
;
2967 if (p
[index
] != ~old
)
2968 isl_die(tab
->mat
->ctx
, isl_error_internal
,
2969 "broken internal state", return -1);
2975 /* Rotate the "n" constraints starting at "first" to the right,
2976 * putting the last constraint in the position of the first constraint.
2978 static int rotate_constraints(struct isl_tab
*tab
, int first
, int n
)
2981 struct isl_tab_var var
;
2986 last
= first
+ n
- 1;
2987 var
= tab
->con
[last
];
2988 for (i
= last
; i
> first
; --i
) {
2989 tab
->con
[i
] = tab
->con
[i
- 1];
2990 if (update_con_after_move(tab
, i
, i
- 1) < 0)
2993 tab
->con
[first
] = var
;
2994 if (update_con_after_move(tab
, first
, last
) < 0)
3000 /* Make the equalities that are implicit in "bmap" but that have been
3001 * detected in the corresponding "tab" explicit in "bmap" and update
3002 * "tab" to reflect the new order of the constraints.
3004 * In particular, if inequality i is an implicit equality then
3005 * isl_basic_map_inequality_to_equality will move the inequality
3006 * in front of the other equality and it will move the last inequality
3007 * in the position of inequality i.
3008 * In the tableau, the inequalities of "bmap" are stored after the equalities
3009 * and so the original order
3011 * E E E E E A A A I B B B B L
3015 * I E E E E E A A A L B B B B
3017 * where I is the implicit equality, the E are equalities,
3018 * the A inequalities before I, the B inequalities after I and
3019 * L the last inequality.
3020 * We therefore need to rotate to the right two sets of constraints,
3021 * those up to and including I and those after I.
3023 * If "tab" contains any constraints that are not in "bmap" then they
3024 * appear after those in "bmap" and they should be left untouched.
3026 * Note that this function leaves "bmap" in a temporary state
3027 * as it does not call isl_basic_map_gauss. Calling this function
3028 * is the responsibility of the caller.
3030 __isl_give isl_basic_map
*isl_tab_make_equalities_explicit(struct isl_tab
*tab
,
3031 __isl_take isl_basic_map
*bmap
)
3036 return isl_basic_map_free(bmap
);
3040 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
3041 if (!isl_tab_is_equality(tab
, bmap
->n_eq
+ i
))
3043 isl_basic_map_inequality_to_equality(bmap
, i
);
3044 if (rotate_constraints(tab
, 0, tab
->n_eq
+ i
+ 1) < 0)
3045 return isl_basic_map_free(bmap
);
3046 if (rotate_constraints(tab
, tab
->n_eq
+ i
+ 1,
3047 bmap
->n_ineq
- i
) < 0)
3048 return isl_basic_map_free(bmap
);
3055 static int con_is_redundant(struct isl_tab
*tab
, struct isl_tab_var
*var
)
3059 if (tab
->rational
) {
3060 int sgn
= sign_of_min(tab
, var
);
3065 int irred
= isl_tab_min_at_most_neg_one(tab
, var
);
3072 /* Return an isl_tab_var that has been marked or NULL if no such
3073 * variable can be found.
3074 * The marked field has only been set for variables that
3075 * appear in non-redundant rows or non-dead columns.
3077 * Pick the last constraint variable that is marked and
3078 * that appears in either a non-redundant row or a non-dead columns.
3079 * Since the returned variable is tested for being a redundant constraint,
3080 * there is no need to return any tab variable that corresponds to a variable.
3082 static struct isl_tab_var
*select_marked(struct isl_tab
*tab
)
3085 struct isl_tab_var
*var
;
3087 for (i
= tab
->n_con
- 1; i
>= 0; --i
) {
3091 if (var
->is_row
&& var
->index
< tab
->n_redundant
)
3093 if (!var
->is_row
&& var
->index
< tab
->n_dead
)
3102 /* Check for (near) redundant constraints.
3103 * A constraint is redundant if it is non-negative and if
3104 * its minimal value (temporarily ignoring the non-negativity) is either
3105 * - zero (in case of rational tableaus), or
3106 * - strictly larger than -1 (in case of integer tableaus)
3108 * We first mark all non-redundant and non-dead variables that
3109 * are not frozen and not obviously negatively unbounded.
3110 * Then we iterate over all marked variables if they can attain
3111 * any values smaller than zero or at most negative one.
3112 * If not, we mark the row as being redundant (assuming it hasn't
3113 * been detected as being obviously redundant in the mean time).
3115 int isl_tab_detect_redundant(struct isl_tab
*tab
)
3124 if (tab
->n_redundant
== tab
->n_row
)
3128 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
3129 struct isl_tab_var
*var
= isl_tab_var_from_row(tab
, i
);
3130 var
->marked
= !var
->frozen
&& var
->is_nonneg
;
3134 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
3135 struct isl_tab_var
*var
= var_from_col(tab
, i
);
3136 var
->marked
= !var
->frozen
&& var
->is_nonneg
&&
3137 !min_is_manifestly_unbounded(tab
, var
);
3142 struct isl_tab_var
*var
;
3144 var
= select_marked(tab
);
3149 red
= con_is_redundant(tab
, var
);
3152 if (red
&& !var
->is_redundant
)
3153 if (isl_tab_mark_redundant(tab
, var
->index
) < 0)
3155 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
3156 var
= var_from_col(tab
, i
);
3159 if (!min_is_manifestly_unbounded(tab
, var
))
3169 int isl_tab_is_equality(struct isl_tab
*tab
, int con
)
3176 if (tab
->con
[con
].is_zero
)
3178 if (tab
->con
[con
].is_redundant
)
3180 if (!tab
->con
[con
].is_row
)
3181 return tab
->con
[con
].index
< tab
->n_dead
;
3183 row
= tab
->con
[con
].index
;
3186 return isl_int_is_zero(tab
->mat
->row
[row
][1]) &&
3187 (!tab
->M
|| isl_int_is_zero(tab
->mat
->row
[row
][2])) &&
3188 isl_seq_first_non_zero(tab
->mat
->row
[row
] + off
+ tab
->n_dead
,
3189 tab
->n_col
- tab
->n_dead
) == -1;
3192 /* Return the minimal value of the affine expression "f" with denominator
3193 * "denom" in *opt, *opt_denom, assuming the tableau is not empty and
3194 * the expression cannot attain arbitrarily small values.
3195 * If opt_denom is NULL, then *opt is rounded up to the nearest integer.
3196 * The return value reflects the nature of the result (empty, unbounded,
3197 * minimal value returned in *opt).
3199 * This function assumes that at least one more row and at least
3200 * one more element in the constraint array are available in the tableau.
3202 enum isl_lp_result
isl_tab_min(struct isl_tab
*tab
,
3203 isl_int
*f
, isl_int denom
, isl_int
*opt
, isl_int
*opt_denom
,
3207 enum isl_lp_result res
= isl_lp_ok
;
3208 struct isl_tab_var
*var
;
3209 struct isl_tab_undo
*snap
;
3212 return isl_lp_error
;
3215 return isl_lp_empty
;
3217 snap
= isl_tab_snap(tab
);
3218 r
= isl_tab_add_row(tab
, f
);
3220 return isl_lp_error
;
3224 find_pivot(tab
, var
, var
, -1, &row
, &col
);
3225 if (row
== var
->index
) {
3226 res
= isl_lp_unbounded
;
3231 if (isl_tab_pivot(tab
, row
, col
) < 0)
3232 return isl_lp_error
;
3234 isl_int_mul(tab
->mat
->row
[var
->index
][0],
3235 tab
->mat
->row
[var
->index
][0], denom
);
3236 if (ISL_FL_ISSET(flags
, ISL_TAB_SAVE_DUAL
)) {
3239 isl_vec_free(tab
->dual
);
3240 tab
->dual
= isl_vec_alloc(tab
->mat
->ctx
, 1 + tab
->n_con
);
3242 return isl_lp_error
;
3243 isl_int_set(tab
->dual
->el
[0], tab
->mat
->row
[var
->index
][0]);
3244 for (i
= 0; i
< tab
->n_con
; ++i
) {
3246 if (tab
->con
[i
].is_row
) {
3247 isl_int_set_si(tab
->dual
->el
[1 + i
], 0);
3250 pos
= 2 + tab
->M
+ tab
->con
[i
].index
;
3251 if (tab
->con
[i
].negated
)
3252 isl_int_neg(tab
->dual
->el
[1 + i
],
3253 tab
->mat
->row
[var
->index
][pos
]);
3255 isl_int_set(tab
->dual
->el
[1 + i
],
3256 tab
->mat
->row
[var
->index
][pos
]);
3259 if (opt
&& res
== isl_lp_ok
) {
3261 isl_int_set(*opt
, tab
->mat
->row
[var
->index
][1]);
3262 isl_int_set(*opt_denom
, tab
->mat
->row
[var
->index
][0]);
3264 isl_int_cdiv_q(*opt
, tab
->mat
->row
[var
->index
][1],
3265 tab
->mat
->row
[var
->index
][0]);
3267 if (isl_tab_rollback(tab
, snap
) < 0)
3268 return isl_lp_error
;
3272 /* Is the constraint at position "con" marked as being redundant?
3273 * If it is marked as representing an equality, then it is not
3274 * considered to be redundant.
3275 * Note that isl_tab_mark_redundant marks both the isl_tab_var as
3276 * redundant and moves the corresponding row into the first
3277 * tab->n_redundant positions (or removes the row, assigning it index -1),
3278 * so the final test is actually redundant itself.
3280 int isl_tab_is_redundant(struct isl_tab
*tab
, int con
)
3284 if (con
< 0 || con
>= tab
->n_con
)
3285 isl_die(isl_tab_get_ctx(tab
), isl_error_invalid
,
3286 "position out of bounds", return -1);
3287 if (tab
->con
[con
].is_zero
)
3289 if (tab
->con
[con
].is_redundant
)
3291 return tab
->con
[con
].is_row
&& tab
->con
[con
].index
< tab
->n_redundant
;
3294 /* Take a snapshot of the tableau that can be restored by a call to
3297 struct isl_tab_undo
*isl_tab_snap(struct isl_tab
*tab
)
3305 /* Does "tab" need to keep track of undo information?
3306 * That is, was a snapshot taken that may need to be restored?
3308 isl_bool
isl_tab_need_undo(struct isl_tab
*tab
)
3311 return isl_bool_error
;
3313 return tab
->need_undo
;
3316 /* Remove all tracking of undo information from "tab", invalidating
3317 * any snapshots that may have been taken of the tableau.
3318 * Since all snapshots have been invalidated, there is also
3319 * no need to start keeping track of undo information again.
3321 void isl_tab_clear_undo(struct isl_tab
*tab
)
3330 /* Undo the operation performed by isl_tab_relax.
3332 static int unrelax(struct isl_tab
*tab
, struct isl_tab_var
*var
) WARN_UNUSED
;
3333 static int unrelax(struct isl_tab
*tab
, struct isl_tab_var
*var
)
3335 unsigned off
= 2 + tab
->M
;
3337 if (!var
->is_row
&& !max_is_manifestly_unbounded(tab
, var
))
3338 if (to_row(tab
, var
, 1) < 0)
3342 isl_int_sub(tab
->mat
->row
[var
->index
][1],
3343 tab
->mat
->row
[var
->index
][1], tab
->mat
->row
[var
->index
][0]);
3344 if (var
->is_nonneg
) {
3345 int sgn
= restore_row(tab
, var
);
3346 isl_assert(tab
->mat
->ctx
, sgn
>= 0, return -1);
3351 for (i
= 0; i
< tab
->n_row
; ++i
) {
3352 if (isl_int_is_zero(tab
->mat
->row
[i
][off
+ var
->index
]))
3354 isl_int_add(tab
->mat
->row
[i
][1], tab
->mat
->row
[i
][1],
3355 tab
->mat
->row
[i
][off
+ var
->index
]);
3363 /* Undo the operation performed by isl_tab_unrestrict.
3365 * In particular, mark the variable as being non-negative and make
3366 * sure the sample value respects this constraint.
3368 static int ununrestrict(struct isl_tab
*tab
, struct isl_tab_var
*var
)
3372 if (var
->is_row
&& restore_row(tab
, var
) < -1)
3378 static int perform_undo_var(struct isl_tab
*tab
, struct isl_tab_undo
*undo
) WARN_UNUSED
;
3379 static int perform_undo_var(struct isl_tab
*tab
, struct isl_tab_undo
*undo
)
3381 struct isl_tab_var
*var
= var_from_index(tab
, undo
->u
.var_index
);
3382 switch (undo
->type
) {
3383 case isl_tab_undo_nonneg
:
3386 case isl_tab_undo_redundant
:
3387 var
->is_redundant
= 0;
3389 restore_row(tab
, isl_tab_var_from_row(tab
, tab
->n_redundant
));
3391 case isl_tab_undo_freeze
:
3394 case isl_tab_undo_zero
:
3399 case isl_tab_undo_allocate
:
3400 if (undo
->u
.var_index
>= 0) {
3401 isl_assert(tab
->mat
->ctx
, !var
->is_row
, return -1);
3402 return drop_col(tab
, var
->index
);
3405 if (!max_is_manifestly_unbounded(tab
, var
)) {
3406 if (to_row(tab
, var
, 1) < 0)
3408 } else if (!min_is_manifestly_unbounded(tab
, var
)) {
3409 if (to_row(tab
, var
, -1) < 0)
3412 if (to_row(tab
, var
, 0) < 0)
3415 return drop_row(tab
, var
->index
);
3416 case isl_tab_undo_relax
:
3417 return unrelax(tab
, var
);
3418 case isl_tab_undo_unrestrict
:
3419 return ununrestrict(tab
, var
);
3421 isl_die(tab
->mat
->ctx
, isl_error_internal
,
3422 "perform_undo_var called on invalid undo record",
3429 /* Undo the addition of an integer division to the basic map representation
3430 * of "tab" in position "pos".
3432 static isl_stat
drop_bmap_div(struct isl_tab
*tab
, int pos
)
3436 off
= tab
->n_var
- isl_basic_map_dim(tab
->bmap
, isl_dim_div
);
3437 if (isl_basic_map_drop_div(tab
->bmap
, pos
- off
) < 0)
3438 return isl_stat_error
;
3440 tab
->samples
= isl_mat_drop_cols(tab
->samples
, 1 + pos
, 1);
3442 return isl_stat_error
;
3448 /* Restore the tableau to the state where the basic variables
3449 * are those in "col_var".
3450 * We first construct a list of variables that are currently in
3451 * the basis, but shouldn't. Then we iterate over all variables
3452 * that should be in the basis and for each one that is currently
3453 * not in the basis, we exchange it with one of the elements of the
3454 * list constructed before.
3455 * We can always find an appropriate variable to pivot with because
3456 * the current basis is mapped to the old basis by a non-singular
3457 * matrix and so we can never end up with a zero row.
3459 static int restore_basis(struct isl_tab
*tab
, int *col_var
)
3463 int *extra
= NULL
; /* current columns that contain bad stuff */
3464 unsigned off
= 2 + tab
->M
;
3466 extra
= isl_alloc_array(tab
->mat
->ctx
, int, tab
->n_col
);
3467 if (tab
->n_col
&& !extra
)
3469 for (i
= 0; i
< tab
->n_col
; ++i
) {
3470 for (j
= 0; j
< tab
->n_col
; ++j
)
3471 if (tab
->col_var
[i
] == col_var
[j
])
3475 extra
[n_extra
++] = i
;
3477 for (i
= 0; i
< tab
->n_col
&& n_extra
> 0; ++i
) {
3478 struct isl_tab_var
*var
;
3481 for (j
= 0; j
< tab
->n_col
; ++j
)
3482 if (col_var
[i
] == tab
->col_var
[j
])
3486 var
= var_from_index(tab
, col_var
[i
]);
3488 for (j
= 0; j
< n_extra
; ++j
)
3489 if (!isl_int_is_zero(tab
->mat
->row
[row
][off
+extra
[j
]]))
3491 isl_assert(tab
->mat
->ctx
, j
< n_extra
, goto error
);
3492 if (isl_tab_pivot(tab
, row
, extra
[j
]) < 0)
3494 extra
[j
] = extra
[--n_extra
];
3504 /* Remove all samples with index n or greater, i.e., those samples
3505 * that were added since we saved this number of samples in
3506 * isl_tab_save_samples.
3508 static void drop_samples_since(struct isl_tab
*tab
, int n
)
3512 for (i
= tab
->n_sample
- 1; i
>= 0 && tab
->n_sample
> n
; --i
) {
3513 if (tab
->sample_index
[i
] < n
)
3516 if (i
!= tab
->n_sample
- 1) {
3517 int t
= tab
->sample_index
[tab
->n_sample
-1];
3518 tab
->sample_index
[tab
->n_sample
-1] = tab
->sample_index
[i
];
3519 tab
->sample_index
[i
] = t
;
3520 isl_mat_swap_rows(tab
->samples
, tab
->n_sample
-1, i
);
3526 static int perform_undo(struct isl_tab
*tab
, struct isl_tab_undo
*undo
) WARN_UNUSED
;
3527 static int perform_undo(struct isl_tab
*tab
, struct isl_tab_undo
*undo
)
3529 switch (undo
->type
) {
3530 case isl_tab_undo_rational
:
3533 case isl_tab_undo_empty
:
3536 case isl_tab_undo_nonneg
:
3537 case isl_tab_undo_redundant
:
3538 case isl_tab_undo_freeze
:
3539 case isl_tab_undo_zero
:
3540 case isl_tab_undo_allocate
:
3541 case isl_tab_undo_relax
:
3542 case isl_tab_undo_unrestrict
:
3543 return perform_undo_var(tab
, undo
);
3544 case isl_tab_undo_bmap_eq
:
3545 return isl_basic_map_free_equality(tab
->bmap
, 1);
3546 case isl_tab_undo_bmap_ineq
:
3547 return isl_basic_map_free_inequality(tab
->bmap
, 1);
3548 case isl_tab_undo_bmap_div
:
3549 return drop_bmap_div(tab
, undo
->u
.var_index
);
3550 case isl_tab_undo_saved_basis
:
3551 if (restore_basis(tab
, undo
->u
.col_var
) < 0)
3554 case isl_tab_undo_drop_sample
:
3557 case isl_tab_undo_saved_samples
:
3558 drop_samples_since(tab
, undo
->u
.n
);
3560 case isl_tab_undo_callback
:
3561 return undo
->u
.callback
->run(undo
->u
.callback
);
3563 isl_assert(tab
->mat
->ctx
, 0, return -1);
3568 /* Return the tableau to the state it was in when the snapshot "snap"
3571 int isl_tab_rollback(struct isl_tab
*tab
, struct isl_tab_undo
*snap
)
3573 struct isl_tab_undo
*undo
, *next
;
3579 for (undo
= tab
->top
; undo
&& undo
!= &tab
->bottom
; undo
= next
) {
3583 if (perform_undo(tab
, undo
) < 0) {
3589 free_undo_record(undo
);
3598 /* The given row "row" represents an inequality violated by all
3599 * points in the tableau. Check for some special cases of such
3600 * separating constraints.
3601 * In particular, if the row has been reduced to the constant -1,
3602 * then we know the inequality is adjacent (but opposite) to
3603 * an equality in the tableau.
3604 * If the row has been reduced to r = c*(-1 -r'), with r' an inequality
3605 * of the tableau and c a positive constant, then the inequality
3606 * is adjacent (but opposite) to the inequality r'.
3608 static enum isl_ineq_type
separation_type(struct isl_tab
*tab
, unsigned row
)
3611 unsigned off
= 2 + tab
->M
;
3614 return isl_ineq_separate
;
3616 if (!isl_int_is_one(tab
->mat
->row
[row
][0]))
3617 return isl_ineq_separate
;
3619 pos
= isl_seq_first_non_zero(tab
->mat
->row
[row
] + off
+ tab
->n_dead
,
3620 tab
->n_col
- tab
->n_dead
);
3622 if (isl_int_is_negone(tab
->mat
->row
[row
][1]))
3623 return isl_ineq_adj_eq
;
3625 return isl_ineq_separate
;
3628 if (!isl_int_eq(tab
->mat
->row
[row
][1],
3629 tab
->mat
->row
[row
][off
+ tab
->n_dead
+ pos
]))
3630 return isl_ineq_separate
;
3632 pos
= isl_seq_first_non_zero(
3633 tab
->mat
->row
[row
] + off
+ tab
->n_dead
+ pos
+ 1,
3634 tab
->n_col
- tab
->n_dead
- pos
- 1);
3636 return pos
== -1 ? isl_ineq_adj_ineq
: isl_ineq_separate
;
3639 /* Check the effect of inequality "ineq" on the tableau "tab".
3641 * isl_ineq_redundant: satisfied by all points in the tableau
3642 * isl_ineq_separate: satisfied by no point in the tableau
3643 * isl_ineq_cut: satisfied by some by not all points
3644 * isl_ineq_adj_eq: adjacent to an equality
3645 * isl_ineq_adj_ineq: adjacent to an inequality.
3647 enum isl_ineq_type
isl_tab_ineq_type(struct isl_tab
*tab
, isl_int
*ineq
)
3649 enum isl_ineq_type type
= isl_ineq_error
;
3650 struct isl_tab_undo
*snap
= NULL
;
3655 return isl_ineq_error
;
3657 if (isl_tab_extend_cons(tab
, 1) < 0)
3658 return isl_ineq_error
;
3660 snap
= isl_tab_snap(tab
);
3662 con
= isl_tab_add_row(tab
, ineq
);
3666 row
= tab
->con
[con
].index
;
3667 if (isl_tab_row_is_redundant(tab
, row
))
3668 type
= isl_ineq_redundant
;
3669 else if (isl_int_is_neg(tab
->mat
->row
[row
][1]) &&
3671 isl_int_abs_ge(tab
->mat
->row
[row
][1],
3672 tab
->mat
->row
[row
][0]))) {
3673 int nonneg
= at_least_zero(tab
, &tab
->con
[con
]);
3677 type
= isl_ineq_cut
;
3679 type
= separation_type(tab
, row
);
3681 int red
= con_is_redundant(tab
, &tab
->con
[con
]);
3685 type
= isl_ineq_cut
;
3687 type
= isl_ineq_redundant
;
3690 if (isl_tab_rollback(tab
, snap
))
3691 return isl_ineq_error
;
3694 return isl_ineq_error
;
3697 int isl_tab_track_bmap(struct isl_tab
*tab
, __isl_take isl_basic_map
*bmap
)
3699 bmap
= isl_basic_map_cow(bmap
);
3704 bmap
= isl_basic_map_set_to_empty(bmap
);
3711 isl_assert(tab
->mat
->ctx
, tab
->n_eq
== bmap
->n_eq
, goto error
);
3712 isl_assert(tab
->mat
->ctx
,
3713 tab
->n_con
== bmap
->n_eq
+ bmap
->n_ineq
, goto error
);
3719 isl_basic_map_free(bmap
);
3723 int isl_tab_track_bset(struct isl_tab
*tab
, __isl_take isl_basic_set
*bset
)
3725 return isl_tab_track_bmap(tab
, (isl_basic_map
*)bset
);
3728 __isl_keep isl_basic_set
*isl_tab_peek_bset(struct isl_tab
*tab
)
3733 return (isl_basic_set
*)tab
->bmap
;
3736 static void isl_tab_print_internal(__isl_keep
struct isl_tab
*tab
,
3737 FILE *out
, int indent
)
3743 fprintf(out
, "%*snull tab\n", indent
, "");
3746 fprintf(out
, "%*sn_redundant: %d, n_dead: %d", indent
, "",
3747 tab
->n_redundant
, tab
->n_dead
);
3749 fprintf(out
, ", rational");
3751 fprintf(out
, ", empty");
3753 fprintf(out
, "%*s[", indent
, "");
3754 for (i
= 0; i
< tab
->n_var
; ++i
) {
3756 fprintf(out
, (i
== tab
->n_param
||
3757 i
== tab
->n_var
- tab
->n_div
) ? "; "
3759 fprintf(out
, "%c%d%s", tab
->var
[i
].is_row
? 'r' : 'c',
3761 tab
->var
[i
].is_zero
? " [=0]" :
3762 tab
->var
[i
].is_redundant
? " [R]" : "");
3764 fprintf(out
, "]\n");
3765 fprintf(out
, "%*s[", indent
, "");
3766 for (i
= 0; i
< tab
->n_con
; ++i
) {
3769 fprintf(out
, "%c%d%s", tab
->con
[i
].is_row
? 'r' : 'c',
3771 tab
->con
[i
].is_zero
? " [=0]" :
3772 tab
->con
[i
].is_redundant
? " [R]" : "");
3774 fprintf(out
, "]\n");
3775 fprintf(out
, "%*s[", indent
, "");
3776 for (i
= 0; i
< tab
->n_row
; ++i
) {
3777 const char *sign
= "";
3780 if (tab
->row_sign
) {
3781 if (tab
->row_sign
[i
] == isl_tab_row_unknown
)
3783 else if (tab
->row_sign
[i
] == isl_tab_row_neg
)
3785 else if (tab
->row_sign
[i
] == isl_tab_row_pos
)
3790 fprintf(out
, "r%d: %d%s%s", i
, tab
->row_var
[i
],
3791 isl_tab_var_from_row(tab
, i
)->is_nonneg
? " [>=0]" : "", sign
);
3793 fprintf(out
, "]\n");
3794 fprintf(out
, "%*s[", indent
, "");
3795 for (i
= 0; i
< tab
->n_col
; ++i
) {
3798 fprintf(out
, "c%d: %d%s", i
, tab
->col_var
[i
],
3799 var_from_col(tab
, i
)->is_nonneg
? " [>=0]" : "");
3801 fprintf(out
, "]\n");
3802 r
= tab
->mat
->n_row
;
3803 tab
->mat
->n_row
= tab
->n_row
;
3804 c
= tab
->mat
->n_col
;
3805 tab
->mat
->n_col
= 2 + tab
->M
+ tab
->n_col
;
3806 isl_mat_print_internal(tab
->mat
, out
, indent
);
3807 tab
->mat
->n_row
= r
;
3808 tab
->mat
->n_col
= c
;
3810 isl_basic_map_print_internal(tab
->bmap
, out
, indent
);
3813 void isl_tab_dump(__isl_keep
struct isl_tab
*tab
)
3815 isl_tab_print_internal(tab
, stderr
, 0);