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>
23 #include <bset_to_bmap.c>
24 #include <bset_from_bmap.c>
27 * The implementation of tableaus in this file was inspired by Section 8
28 * of David Detlefs, Greg Nelson and James B. Saxe, "Simplify: a theorem
29 * prover for program checking".
32 struct isl_tab
*isl_tab_alloc(struct isl_ctx
*ctx
,
33 unsigned n_row
, unsigned n_var
, unsigned M
)
39 tab
= isl_calloc_type(ctx
, struct isl_tab
);
42 tab
->mat
= isl_mat_alloc(ctx
, n_row
, off
+ n_var
);
45 tab
->var
= isl_alloc_array(ctx
, struct isl_tab_var
, n_var
);
46 if (n_var
&& !tab
->var
)
48 tab
->con
= isl_alloc_array(ctx
, struct isl_tab_var
, n_row
);
49 if (n_row
&& !tab
->con
)
51 tab
->col_var
= isl_alloc_array(ctx
, int, n_var
);
52 if (n_var
&& !tab
->col_var
)
54 tab
->row_var
= isl_alloc_array(ctx
, int, n_row
);
55 if (n_row
&& !tab
->row_var
)
57 for (i
= 0; i
< n_var
; ++i
) {
58 tab
->var
[i
].index
= i
;
59 tab
->var
[i
].is_row
= 0;
60 tab
->var
[i
].is_nonneg
= 0;
61 tab
->var
[i
].is_zero
= 0;
62 tab
->var
[i
].is_redundant
= 0;
63 tab
->var
[i
].frozen
= 0;
64 tab
->var
[i
].negated
= 0;
78 tab
->strict_redundant
= 0;
85 tab
->bottom
.type
= isl_tab_undo_bottom
;
86 tab
->bottom
.next
= NULL
;
87 tab
->top
= &tab
->bottom
;
99 isl_ctx
*isl_tab_get_ctx(struct isl_tab
*tab
)
101 return tab
? isl_mat_get_ctx(tab
->mat
) : NULL
;
104 int isl_tab_extend_cons(struct isl_tab
*tab
, unsigned n_new
)
113 if (tab
->max_con
< tab
->n_con
+ n_new
) {
114 struct isl_tab_var
*con
;
116 con
= isl_realloc_array(tab
->mat
->ctx
, tab
->con
,
117 struct isl_tab_var
, tab
->max_con
+ n_new
);
121 tab
->max_con
+= n_new
;
123 if (tab
->mat
->n_row
< tab
->n_row
+ n_new
) {
126 tab
->mat
= isl_mat_extend(tab
->mat
,
127 tab
->n_row
+ n_new
, off
+ tab
->n_col
);
130 row_var
= isl_realloc_array(tab
->mat
->ctx
, tab
->row_var
,
131 int, tab
->mat
->n_row
);
134 tab
->row_var
= row_var
;
136 enum isl_tab_row_sign
*s
;
137 s
= isl_realloc_array(tab
->mat
->ctx
, tab
->row_sign
,
138 enum isl_tab_row_sign
, tab
->mat
->n_row
);
147 /* Make room for at least n_new extra variables.
148 * Return -1 if anything went wrong.
150 int isl_tab_extend_vars(struct isl_tab
*tab
, unsigned n_new
)
152 struct isl_tab_var
*var
;
153 unsigned off
= 2 + tab
->M
;
155 if (tab
->max_var
< tab
->n_var
+ n_new
) {
156 var
= isl_realloc_array(tab
->mat
->ctx
, tab
->var
,
157 struct isl_tab_var
, tab
->n_var
+ n_new
);
161 tab
->max_var
= tab
->n_var
+ n_new
;
164 if (tab
->mat
->n_col
< off
+ tab
->n_col
+ n_new
) {
167 tab
->mat
= isl_mat_extend(tab
->mat
,
168 tab
->mat
->n_row
, off
+ tab
->n_col
+ n_new
);
171 p
= isl_realloc_array(tab
->mat
->ctx
, tab
->col_var
,
172 int, tab
->n_col
+ n_new
);
181 static void free_undo_record(struct isl_tab_undo
*undo
)
183 switch (undo
->type
) {
184 case isl_tab_undo_saved_basis
:
185 free(undo
->u
.col_var
);
192 static void free_undo(struct isl_tab
*tab
)
194 struct isl_tab_undo
*undo
, *next
;
196 for (undo
= tab
->top
; undo
&& undo
!= &tab
->bottom
; undo
= next
) {
198 free_undo_record(undo
);
203 void isl_tab_free(struct isl_tab
*tab
)
208 isl_mat_free(tab
->mat
);
209 isl_vec_free(tab
->dual
);
210 isl_basic_map_free(tab
->bmap
);
216 isl_mat_free(tab
->samples
);
217 free(tab
->sample_index
);
218 isl_mat_free(tab
->basis
);
222 struct isl_tab
*isl_tab_dup(struct isl_tab
*tab
)
232 dup
= isl_calloc_type(tab
->mat
->ctx
, struct isl_tab
);
235 dup
->mat
= isl_mat_dup(tab
->mat
);
238 dup
->var
= isl_alloc_array(tab
->mat
->ctx
, struct isl_tab_var
, tab
->max_var
);
239 if (tab
->max_var
&& !dup
->var
)
241 for (i
= 0; i
< tab
->n_var
; ++i
)
242 dup
->var
[i
] = tab
->var
[i
];
243 dup
->con
= isl_alloc_array(tab
->mat
->ctx
, struct isl_tab_var
, tab
->max_con
);
244 if (tab
->max_con
&& !dup
->con
)
246 for (i
= 0; i
< tab
->n_con
; ++i
)
247 dup
->con
[i
] = tab
->con
[i
];
248 dup
->col_var
= isl_alloc_array(tab
->mat
->ctx
, int, tab
->mat
->n_col
- off
);
249 if ((tab
->mat
->n_col
- off
) && !dup
->col_var
)
251 for (i
= 0; i
< tab
->n_col
; ++i
)
252 dup
->col_var
[i
] = tab
->col_var
[i
];
253 dup
->row_var
= isl_alloc_array(tab
->mat
->ctx
, int, tab
->mat
->n_row
);
254 if (tab
->mat
->n_row
&& !dup
->row_var
)
256 for (i
= 0; i
< tab
->n_row
; ++i
)
257 dup
->row_var
[i
] = tab
->row_var
[i
];
259 dup
->row_sign
= isl_alloc_array(tab
->mat
->ctx
, enum isl_tab_row_sign
,
261 if (tab
->mat
->n_row
&& !dup
->row_sign
)
263 for (i
= 0; i
< tab
->n_row
; ++i
)
264 dup
->row_sign
[i
] = tab
->row_sign
[i
];
267 dup
->samples
= isl_mat_dup(tab
->samples
);
270 dup
->sample_index
= isl_alloc_array(tab
->mat
->ctx
, int,
271 tab
->samples
->n_row
);
272 if (tab
->samples
->n_row
&& !dup
->sample_index
)
274 dup
->n_sample
= tab
->n_sample
;
275 dup
->n_outside
= tab
->n_outside
;
277 dup
->n_row
= tab
->n_row
;
278 dup
->n_con
= tab
->n_con
;
279 dup
->n_eq
= tab
->n_eq
;
280 dup
->max_con
= tab
->max_con
;
281 dup
->n_col
= tab
->n_col
;
282 dup
->n_var
= tab
->n_var
;
283 dup
->max_var
= tab
->max_var
;
284 dup
->n_param
= tab
->n_param
;
285 dup
->n_div
= tab
->n_div
;
286 dup
->n_dead
= tab
->n_dead
;
287 dup
->n_redundant
= tab
->n_redundant
;
288 dup
->rational
= tab
->rational
;
289 dup
->empty
= tab
->empty
;
290 dup
->strict_redundant
= 0;
294 tab
->cone
= tab
->cone
;
295 dup
->bottom
.type
= isl_tab_undo_bottom
;
296 dup
->bottom
.next
= NULL
;
297 dup
->top
= &dup
->bottom
;
299 dup
->n_zero
= tab
->n_zero
;
300 dup
->n_unbounded
= tab
->n_unbounded
;
301 dup
->basis
= isl_mat_dup(tab
->basis
);
309 /* Construct the coefficient matrix of the product tableau
311 * mat{1,2} is the coefficient matrix of tableau {1,2}
312 * row{1,2} is the number of rows in tableau {1,2}
313 * col{1,2} is the number of columns in tableau {1,2}
314 * off is the offset to the coefficient column (skipping the
315 * denominator, the constant term and the big parameter if any)
316 * r{1,2} is the number of redundant rows in tableau {1,2}
317 * d{1,2} is the number of dead columns in tableau {1,2}
319 * The order of the rows and columns in the result is as explained
320 * in isl_tab_product.
322 static struct isl_mat
*tab_mat_product(struct isl_mat
*mat1
,
323 struct isl_mat
*mat2
, unsigned row1
, unsigned row2
,
324 unsigned col1
, unsigned col2
,
325 unsigned off
, unsigned r1
, unsigned r2
, unsigned d1
, unsigned d2
)
328 struct isl_mat
*prod
;
331 prod
= isl_mat_alloc(mat1
->ctx
, mat1
->n_row
+ mat2
->n_row
,
337 for (i
= 0; i
< r1
; ++i
) {
338 isl_seq_cpy(prod
->row
[n
+ i
], mat1
->row
[i
], off
+ d1
);
339 isl_seq_clr(prod
->row
[n
+ i
] + off
+ d1
, d2
);
340 isl_seq_cpy(prod
->row
[n
+ i
] + off
+ d1
+ d2
,
341 mat1
->row
[i
] + off
+ d1
, col1
- d1
);
342 isl_seq_clr(prod
->row
[n
+ i
] + off
+ col1
+ d1
, col2
- d2
);
346 for (i
= 0; i
< r2
; ++i
) {
347 isl_seq_cpy(prod
->row
[n
+ i
], mat2
->row
[i
], off
);
348 isl_seq_clr(prod
->row
[n
+ i
] + off
, d1
);
349 isl_seq_cpy(prod
->row
[n
+ i
] + off
+ d1
,
350 mat2
->row
[i
] + off
, d2
);
351 isl_seq_clr(prod
->row
[n
+ i
] + off
+ d1
+ d2
, col1
- d1
);
352 isl_seq_cpy(prod
->row
[n
+ i
] + off
+ col1
+ d1
,
353 mat2
->row
[i
] + off
+ d2
, col2
- d2
);
357 for (i
= 0; i
< row1
- r1
; ++i
) {
358 isl_seq_cpy(prod
->row
[n
+ i
], mat1
->row
[r1
+ i
], off
+ d1
);
359 isl_seq_clr(prod
->row
[n
+ i
] + off
+ d1
, d2
);
360 isl_seq_cpy(prod
->row
[n
+ i
] + off
+ d1
+ d2
,
361 mat1
->row
[r1
+ i
] + off
+ d1
, col1
- d1
);
362 isl_seq_clr(prod
->row
[n
+ i
] + off
+ col1
+ d1
, col2
- d2
);
366 for (i
= 0; i
< row2
- r2
; ++i
) {
367 isl_seq_cpy(prod
->row
[n
+ i
], mat2
->row
[r2
+ i
], off
);
368 isl_seq_clr(prod
->row
[n
+ i
] + off
, d1
);
369 isl_seq_cpy(prod
->row
[n
+ i
] + off
+ d1
,
370 mat2
->row
[r2
+ i
] + off
, d2
);
371 isl_seq_clr(prod
->row
[n
+ i
] + off
+ d1
+ d2
, col1
- d1
);
372 isl_seq_cpy(prod
->row
[n
+ i
] + off
+ col1
+ d1
,
373 mat2
->row
[r2
+ i
] + off
+ d2
, col2
- d2
);
379 /* Update the row or column index of a variable that corresponds
380 * to a variable in the first input tableau.
382 static void update_index1(struct isl_tab_var
*var
,
383 unsigned r1
, unsigned r2
, unsigned d1
, unsigned d2
)
385 if (var
->index
== -1)
387 if (var
->is_row
&& var
->index
>= r1
)
389 if (!var
->is_row
&& var
->index
>= d1
)
393 /* Update the row or column index of a variable that corresponds
394 * to a variable in the second input tableau.
396 static void update_index2(struct isl_tab_var
*var
,
397 unsigned row1
, unsigned col1
,
398 unsigned r1
, unsigned r2
, unsigned d1
, unsigned d2
)
400 if (var
->index
== -1)
415 /* Create a tableau that represents the Cartesian product of the sets
416 * represented by tableaus tab1 and tab2.
417 * The order of the rows in the product is
418 * - redundant rows of tab1
419 * - redundant rows of tab2
420 * - non-redundant rows of tab1
421 * - non-redundant rows of tab2
422 * The order of the columns is
425 * - coefficient of big parameter, if any
426 * - dead columns of tab1
427 * - dead columns of tab2
428 * - live columns of tab1
429 * - live columns of tab2
430 * The order of the variables and the constraints is a concatenation
431 * of order in the two input tableaus.
433 struct isl_tab
*isl_tab_product(struct isl_tab
*tab1
, struct isl_tab
*tab2
)
436 struct isl_tab
*prod
;
438 unsigned r1
, r2
, d1
, d2
;
443 isl_assert(tab1
->mat
->ctx
, tab1
->M
== tab2
->M
, return NULL
);
444 isl_assert(tab1
->mat
->ctx
, tab1
->rational
== tab2
->rational
, return NULL
);
445 isl_assert(tab1
->mat
->ctx
, tab1
->cone
== tab2
->cone
, return NULL
);
446 isl_assert(tab1
->mat
->ctx
, !tab1
->row_sign
, return NULL
);
447 isl_assert(tab1
->mat
->ctx
, !tab2
->row_sign
, return NULL
);
448 isl_assert(tab1
->mat
->ctx
, tab1
->n_param
== 0, return NULL
);
449 isl_assert(tab1
->mat
->ctx
, tab2
->n_param
== 0, return NULL
);
450 isl_assert(tab1
->mat
->ctx
, tab1
->n_div
== 0, return NULL
);
451 isl_assert(tab1
->mat
->ctx
, tab2
->n_div
== 0, return NULL
);
454 r1
= tab1
->n_redundant
;
455 r2
= tab2
->n_redundant
;
458 prod
= isl_calloc_type(tab1
->mat
->ctx
, struct isl_tab
);
461 prod
->mat
= tab_mat_product(tab1
->mat
, tab2
->mat
,
462 tab1
->n_row
, tab2
->n_row
,
463 tab1
->n_col
, tab2
->n_col
, off
, r1
, r2
, d1
, d2
);
466 prod
->var
= isl_alloc_array(tab1
->mat
->ctx
, struct isl_tab_var
,
467 tab1
->max_var
+ tab2
->max_var
);
468 if ((tab1
->max_var
+ tab2
->max_var
) && !prod
->var
)
470 for (i
= 0; i
< tab1
->n_var
; ++i
) {
471 prod
->var
[i
] = tab1
->var
[i
];
472 update_index1(&prod
->var
[i
], r1
, r2
, d1
, d2
);
474 for (i
= 0; i
< tab2
->n_var
; ++i
) {
475 prod
->var
[tab1
->n_var
+ i
] = tab2
->var
[i
];
476 update_index2(&prod
->var
[tab1
->n_var
+ i
],
477 tab1
->n_row
, tab1
->n_col
,
480 prod
->con
= isl_alloc_array(tab1
->mat
->ctx
, struct isl_tab_var
,
481 tab1
->max_con
+ tab2
->max_con
);
482 if ((tab1
->max_con
+ tab2
->max_con
) && !prod
->con
)
484 for (i
= 0; i
< tab1
->n_con
; ++i
) {
485 prod
->con
[i
] = tab1
->con
[i
];
486 update_index1(&prod
->con
[i
], r1
, r2
, d1
, d2
);
488 for (i
= 0; i
< tab2
->n_con
; ++i
) {
489 prod
->con
[tab1
->n_con
+ i
] = tab2
->con
[i
];
490 update_index2(&prod
->con
[tab1
->n_con
+ i
],
491 tab1
->n_row
, tab1
->n_col
,
494 prod
->col_var
= isl_alloc_array(tab1
->mat
->ctx
, int,
495 tab1
->n_col
+ tab2
->n_col
);
496 if ((tab1
->n_col
+ tab2
->n_col
) && !prod
->col_var
)
498 for (i
= 0; i
< tab1
->n_col
; ++i
) {
499 int pos
= i
< d1
? i
: i
+ d2
;
500 prod
->col_var
[pos
] = tab1
->col_var
[i
];
502 for (i
= 0; i
< tab2
->n_col
; ++i
) {
503 int pos
= i
< d2
? d1
+ i
: tab1
->n_col
+ i
;
504 int t
= tab2
->col_var
[i
];
509 prod
->col_var
[pos
] = t
;
511 prod
->row_var
= isl_alloc_array(tab1
->mat
->ctx
, int,
512 tab1
->mat
->n_row
+ tab2
->mat
->n_row
);
513 if ((tab1
->mat
->n_row
+ tab2
->mat
->n_row
) && !prod
->row_var
)
515 for (i
= 0; i
< tab1
->n_row
; ++i
) {
516 int pos
= i
< r1
? i
: i
+ r2
;
517 prod
->row_var
[pos
] = tab1
->row_var
[i
];
519 for (i
= 0; i
< tab2
->n_row
; ++i
) {
520 int pos
= i
< r2
? r1
+ i
: tab1
->n_row
+ i
;
521 int t
= tab2
->row_var
[i
];
526 prod
->row_var
[pos
] = t
;
528 prod
->samples
= NULL
;
529 prod
->sample_index
= NULL
;
530 prod
->n_row
= tab1
->n_row
+ tab2
->n_row
;
531 prod
->n_con
= tab1
->n_con
+ tab2
->n_con
;
533 prod
->max_con
= tab1
->max_con
+ tab2
->max_con
;
534 prod
->n_col
= tab1
->n_col
+ tab2
->n_col
;
535 prod
->n_var
= tab1
->n_var
+ tab2
->n_var
;
536 prod
->max_var
= tab1
->max_var
+ tab2
->max_var
;
539 prod
->n_dead
= tab1
->n_dead
+ tab2
->n_dead
;
540 prod
->n_redundant
= tab1
->n_redundant
+ tab2
->n_redundant
;
541 prod
->rational
= tab1
->rational
;
542 prod
->empty
= tab1
->empty
|| tab2
->empty
;
543 prod
->strict_redundant
= tab1
->strict_redundant
|| tab2
->strict_redundant
;
547 prod
->cone
= tab1
->cone
;
548 prod
->bottom
.type
= isl_tab_undo_bottom
;
549 prod
->bottom
.next
= NULL
;
550 prod
->top
= &prod
->bottom
;
553 prod
->n_unbounded
= 0;
562 static struct isl_tab_var
*var_from_index(struct isl_tab
*tab
, int i
)
567 return &tab
->con
[~i
];
570 struct isl_tab_var
*isl_tab_var_from_row(struct isl_tab
*tab
, int i
)
572 return var_from_index(tab
, tab
->row_var
[i
]);
575 static struct isl_tab_var
*var_from_col(struct isl_tab
*tab
, int i
)
577 return var_from_index(tab
, tab
->col_var
[i
]);
580 /* Check if there are any upper bounds on column variable "var",
581 * i.e., non-negative rows where var appears with a negative coefficient.
582 * Return 1 if there are no such bounds.
584 static int max_is_manifestly_unbounded(struct isl_tab
*tab
,
585 struct isl_tab_var
*var
)
588 unsigned off
= 2 + tab
->M
;
592 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
593 if (!isl_int_is_neg(tab
->mat
->row
[i
][off
+ var
->index
]))
595 if (isl_tab_var_from_row(tab
, i
)->is_nonneg
)
601 /* Check if there are any lower bounds on column variable "var",
602 * i.e., non-negative rows where var appears with a positive coefficient.
603 * Return 1 if there are no such bounds.
605 static int min_is_manifestly_unbounded(struct isl_tab
*tab
,
606 struct isl_tab_var
*var
)
609 unsigned off
= 2 + tab
->M
;
613 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
614 if (!isl_int_is_pos(tab
->mat
->row
[i
][off
+ var
->index
]))
616 if (isl_tab_var_from_row(tab
, i
)->is_nonneg
)
622 static int row_cmp(struct isl_tab
*tab
, int r1
, int r2
, int c
, isl_int
*t
)
624 unsigned off
= 2 + tab
->M
;
628 isl_int_mul(*t
, tab
->mat
->row
[r1
][2], tab
->mat
->row
[r2
][off
+c
]);
629 isl_int_submul(*t
, tab
->mat
->row
[r2
][2], tab
->mat
->row
[r1
][off
+c
]);
634 isl_int_mul(*t
, tab
->mat
->row
[r1
][1], tab
->mat
->row
[r2
][off
+ c
]);
635 isl_int_submul(*t
, tab
->mat
->row
[r2
][1], tab
->mat
->row
[r1
][off
+ c
]);
636 return isl_int_sgn(*t
);
639 /* Given the index of a column "c", return the index of a row
640 * that can be used to pivot the column in, with either an increase
641 * (sgn > 0) or a decrease (sgn < 0) of the corresponding variable.
642 * If "var" is not NULL, then the row returned will be different from
643 * the one associated with "var".
645 * Each row in the tableau is of the form
647 * x_r = a_r0 + \sum_i a_ri x_i
649 * Only rows with x_r >= 0 and with the sign of a_ri opposite to "sgn"
650 * impose any limit on the increase or decrease in the value of x_c
651 * and this bound is equal to a_r0 / |a_rc|. We are therefore looking
652 * for the row with the smallest (most stringent) such bound.
653 * Note that the common denominator of each row drops out of the fraction.
654 * To check if row j has a smaller bound than row r, i.e.,
655 * a_j0 / |a_jc| < a_r0 / |a_rc| or a_j0 |a_rc| < a_r0 |a_jc|,
656 * we check if -sign(a_jc) (a_j0 a_rc - a_r0 a_jc) < 0,
657 * where -sign(a_jc) is equal to "sgn".
659 static int pivot_row(struct isl_tab
*tab
,
660 struct isl_tab_var
*var
, int sgn
, int c
)
664 unsigned off
= 2 + tab
->M
;
668 for (j
= tab
->n_redundant
; j
< tab
->n_row
; ++j
) {
669 if (var
&& j
== var
->index
)
671 if (!isl_tab_var_from_row(tab
, j
)->is_nonneg
)
673 if (sgn
* isl_int_sgn(tab
->mat
->row
[j
][off
+ c
]) >= 0)
679 tsgn
= sgn
* row_cmp(tab
, r
, j
, c
, &t
);
680 if (tsgn
< 0 || (tsgn
== 0 &&
681 tab
->row_var
[j
] < tab
->row_var
[r
]))
688 /* Find a pivot (row and col) that will increase (sgn > 0) or decrease
689 * (sgn < 0) the value of row variable var.
690 * If not NULL, then skip_var is a row variable that should be ignored
691 * while looking for a pivot row. It is usually equal to var.
693 * As the given row in the tableau is of the form
695 * x_r = a_r0 + \sum_i a_ri x_i
697 * we need to find a column such that the sign of a_ri is equal to "sgn"
698 * (such that an increase in x_i will have the desired effect) or a
699 * column with a variable that may attain negative values.
700 * If a_ri is positive, then we need to move x_i in the same direction
701 * to obtain the desired effect. Otherwise, x_i has to move in the
702 * opposite direction.
704 static void find_pivot(struct isl_tab
*tab
,
705 struct isl_tab_var
*var
, struct isl_tab_var
*skip_var
,
706 int sgn
, int *row
, int *col
)
713 isl_assert(tab
->mat
->ctx
, var
->is_row
, return);
714 tr
= tab
->mat
->row
[var
->index
] + 2 + tab
->M
;
717 for (j
= tab
->n_dead
; j
< tab
->n_col
; ++j
) {
718 if (isl_int_is_zero(tr
[j
]))
720 if (isl_int_sgn(tr
[j
]) != sgn
&&
721 var_from_col(tab
, j
)->is_nonneg
)
723 if (c
< 0 || tab
->col_var
[j
] < tab
->col_var
[c
])
729 sgn
*= isl_int_sgn(tr
[c
]);
730 r
= pivot_row(tab
, skip_var
, sgn
, c
);
731 *row
= r
< 0 ? var
->index
: r
;
735 /* Return 1 if row "row" represents an obviously redundant inequality.
737 * - it represents an inequality or a variable
738 * - that is the sum of a non-negative sample value and a positive
739 * combination of zero or more non-negative constraints.
741 int isl_tab_row_is_redundant(struct isl_tab
*tab
, int row
)
744 unsigned off
= 2 + tab
->M
;
746 if (tab
->row_var
[row
] < 0 && !isl_tab_var_from_row(tab
, row
)->is_nonneg
)
749 if (isl_int_is_neg(tab
->mat
->row
[row
][1]))
751 if (tab
->strict_redundant
&& isl_int_is_zero(tab
->mat
->row
[row
][1]))
753 if (tab
->M
&& isl_int_is_neg(tab
->mat
->row
[row
][2]))
756 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
757 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ i
]))
759 if (tab
->col_var
[i
] >= 0)
761 if (isl_int_is_neg(tab
->mat
->row
[row
][off
+ i
]))
763 if (!var_from_col(tab
, i
)->is_nonneg
)
769 static void swap_rows(struct isl_tab
*tab
, int row1
, int row2
)
772 enum isl_tab_row_sign s
;
774 t
= tab
->row_var
[row1
];
775 tab
->row_var
[row1
] = tab
->row_var
[row2
];
776 tab
->row_var
[row2
] = t
;
777 isl_tab_var_from_row(tab
, row1
)->index
= row1
;
778 isl_tab_var_from_row(tab
, row2
)->index
= row2
;
779 tab
->mat
= isl_mat_swap_rows(tab
->mat
, row1
, row2
);
783 s
= tab
->row_sign
[row1
];
784 tab
->row_sign
[row1
] = tab
->row_sign
[row2
];
785 tab
->row_sign
[row2
] = s
;
788 static int push_union(struct isl_tab
*tab
,
789 enum isl_tab_undo_type type
, union isl_tab_undo_val u
) WARN_UNUSED
;
790 static int push_union(struct isl_tab
*tab
,
791 enum isl_tab_undo_type type
, union isl_tab_undo_val u
)
793 struct isl_tab_undo
*undo
;
800 undo
= isl_alloc_type(tab
->mat
->ctx
, struct isl_tab_undo
);
805 undo
->next
= tab
->top
;
811 int isl_tab_push_var(struct isl_tab
*tab
,
812 enum isl_tab_undo_type type
, struct isl_tab_var
*var
)
814 union isl_tab_undo_val u
;
816 u
.var_index
= tab
->row_var
[var
->index
];
818 u
.var_index
= tab
->col_var
[var
->index
];
819 return push_union(tab
, type
, u
);
822 int isl_tab_push(struct isl_tab
*tab
, enum isl_tab_undo_type type
)
824 union isl_tab_undo_val u
= { 0 };
825 return push_union(tab
, type
, u
);
828 /* Push a record on the undo stack describing the current basic
829 * variables, so that the this state can be restored during rollback.
831 int isl_tab_push_basis(struct isl_tab
*tab
)
834 union isl_tab_undo_val u
;
836 u
.col_var
= isl_alloc_array(tab
->mat
->ctx
, int, tab
->n_col
);
837 if (tab
->n_col
&& !u
.col_var
)
839 for (i
= 0; i
< tab
->n_col
; ++i
)
840 u
.col_var
[i
] = tab
->col_var
[i
];
841 return push_union(tab
, isl_tab_undo_saved_basis
, u
);
844 int isl_tab_push_callback(struct isl_tab
*tab
, struct isl_tab_callback
*callback
)
846 union isl_tab_undo_val u
;
847 u
.callback
= callback
;
848 return push_union(tab
, isl_tab_undo_callback
, u
);
851 struct isl_tab
*isl_tab_init_samples(struct isl_tab
*tab
)
858 tab
->samples
= isl_mat_alloc(tab
->mat
->ctx
, 1, 1 + tab
->n_var
);
861 tab
->sample_index
= isl_alloc_array(tab
->mat
->ctx
, int, 1);
862 if (!tab
->sample_index
)
870 int isl_tab_add_sample(struct isl_tab
*tab
, __isl_take isl_vec
*sample
)
875 if (tab
->n_sample
+ 1 > tab
->samples
->n_row
) {
876 int *t
= isl_realloc_array(tab
->mat
->ctx
,
877 tab
->sample_index
, int, tab
->n_sample
+ 1);
880 tab
->sample_index
= t
;
883 tab
->samples
= isl_mat_extend(tab
->samples
,
884 tab
->n_sample
+ 1, tab
->samples
->n_col
);
888 isl_seq_cpy(tab
->samples
->row
[tab
->n_sample
], sample
->el
, sample
->size
);
889 isl_vec_free(sample
);
890 tab
->sample_index
[tab
->n_sample
] = tab
->n_sample
;
895 isl_vec_free(sample
);
899 struct isl_tab
*isl_tab_drop_sample(struct isl_tab
*tab
, int s
)
901 if (s
!= tab
->n_outside
) {
902 int t
= tab
->sample_index
[tab
->n_outside
];
903 tab
->sample_index
[tab
->n_outside
] = tab
->sample_index
[s
];
904 tab
->sample_index
[s
] = t
;
905 isl_mat_swap_rows(tab
->samples
, tab
->n_outside
, s
);
908 if (isl_tab_push(tab
, isl_tab_undo_drop_sample
) < 0) {
916 /* Record the current number of samples so that we can remove newer
917 * samples during a rollback.
919 int isl_tab_save_samples(struct isl_tab
*tab
)
921 union isl_tab_undo_val u
;
927 return push_union(tab
, isl_tab_undo_saved_samples
, u
);
930 /* Mark row with index "row" as being redundant.
931 * If we may need to undo the operation or if the row represents
932 * a variable of the original problem, the row is kept,
933 * but no longer considered when looking for a pivot row.
934 * Otherwise, the row is simply removed.
936 * The row may be interchanged with some other row. If it
937 * is interchanged with a later row, return 1. Otherwise return 0.
938 * If the rows are checked in order in the calling function,
939 * then a return value of 1 means that the row with the given
940 * row number may now contain a different row that hasn't been checked yet.
942 int isl_tab_mark_redundant(struct isl_tab
*tab
, int row
)
944 struct isl_tab_var
*var
= isl_tab_var_from_row(tab
, row
);
945 var
->is_redundant
= 1;
946 isl_assert(tab
->mat
->ctx
, row
>= tab
->n_redundant
, return -1);
947 if (tab
->preserve
|| tab
->need_undo
|| tab
->row_var
[row
] >= 0) {
948 if (tab
->row_var
[row
] >= 0 && !var
->is_nonneg
) {
950 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, var
) < 0)
953 if (row
!= tab
->n_redundant
)
954 swap_rows(tab
, row
, tab
->n_redundant
);
956 return isl_tab_push_var(tab
, isl_tab_undo_redundant
, var
);
958 if (row
!= tab
->n_row
- 1)
959 swap_rows(tab
, row
, tab
->n_row
- 1);
960 isl_tab_var_from_row(tab
, tab
->n_row
- 1)->index
= -1;
966 /* Mark "tab" as a rational tableau.
967 * If it wasn't marked as a rational tableau already and if we may
968 * need to undo changes, then arrange for the marking to be undone
971 int isl_tab_mark_rational(struct isl_tab
*tab
)
975 if (!tab
->rational
&& tab
->need_undo
)
976 if (isl_tab_push(tab
, isl_tab_undo_rational
) < 0)
982 int isl_tab_mark_empty(struct isl_tab
*tab
)
986 if (!tab
->empty
&& tab
->need_undo
)
987 if (isl_tab_push(tab
, isl_tab_undo_empty
) < 0)
993 int isl_tab_freeze_constraint(struct isl_tab
*tab
, int con
)
995 struct isl_tab_var
*var
;
1000 var
= &tab
->con
[con
];
1008 return isl_tab_push_var(tab
, isl_tab_undo_freeze
, var
);
1013 /* Update the rows signs after a pivot of "row" and "col", with "row_sgn"
1014 * the original sign of the pivot element.
1015 * We only keep track of row signs during PILP solving and in this case
1016 * we only pivot a row with negative sign (meaning the value is always
1017 * non-positive) using a positive pivot element.
1019 * For each row j, the new value of the parametric constant is equal to
1021 * a_j0 - a_jc a_r0/a_rc
1023 * where a_j0 is the original parametric constant, a_rc is the pivot element,
1024 * a_r0 is the parametric constant of the pivot row and a_jc is the
1025 * pivot column entry of the row j.
1026 * Since a_r0 is non-positive and a_rc is positive, the sign of row j
1027 * remains the same if a_jc has the same sign as the row j or if
1028 * a_jc is zero. In all other cases, we reset the sign to "unknown".
1030 static void update_row_sign(struct isl_tab
*tab
, int row
, int col
, int row_sgn
)
1033 struct isl_mat
*mat
= tab
->mat
;
1034 unsigned off
= 2 + tab
->M
;
1039 if (tab
->row_sign
[row
] == 0)
1041 isl_assert(mat
->ctx
, row_sgn
> 0, return);
1042 isl_assert(mat
->ctx
, tab
->row_sign
[row
] == isl_tab_row_neg
, return);
1043 tab
->row_sign
[row
] = isl_tab_row_pos
;
1044 for (i
= 0; i
< tab
->n_row
; ++i
) {
1048 s
= isl_int_sgn(mat
->row
[i
][off
+ col
]);
1051 if (!tab
->row_sign
[i
])
1053 if (s
< 0 && tab
->row_sign
[i
] == isl_tab_row_neg
)
1055 if (s
> 0 && tab
->row_sign
[i
] == isl_tab_row_pos
)
1057 tab
->row_sign
[i
] = isl_tab_row_unknown
;
1061 /* Given a row number "row" and a column number "col", pivot the tableau
1062 * such that the associated variables are interchanged.
1063 * The given row in the tableau expresses
1065 * x_r = a_r0 + \sum_i a_ri x_i
1069 * x_c = 1/a_rc x_r - a_r0/a_rc + sum_{i \ne r} -a_ri/a_rc
1071 * Substituting this equality into the other rows
1073 * x_j = a_j0 + \sum_i a_ji x_i
1075 * with a_jc \ne 0, we obtain
1077 * 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
1084 * where i is any other column and j is any other row,
1085 * is therefore transformed into
1087 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1088 * 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)
1090 * The transformation is performed along the following steps
1092 * d_r/n_rc n_ri/n_rc
1095 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
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| 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|)/(|n_rc| d_j)
1104 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1105 * n_jc/(|n_rc| d_j) (n_ji |n_rc| - s(n_rc)n_jc n_ri)/(|n_rc| d_j)
1107 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1108 * 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)
1111 int isl_tab_pivot(struct isl_tab
*tab
, int row
, int col
)
1117 struct isl_mat
*mat
= tab
->mat
;
1118 struct isl_tab_var
*var
;
1119 unsigned off
= 2 + tab
->M
;
1121 ctx
= isl_tab_get_ctx(tab
);
1122 if (isl_ctx_next_operation(ctx
) < 0)
1125 isl_int_swap(mat
->row
[row
][0], mat
->row
[row
][off
+ col
]);
1126 sgn
= isl_int_sgn(mat
->row
[row
][0]);
1128 isl_int_neg(mat
->row
[row
][0], mat
->row
[row
][0]);
1129 isl_int_neg(mat
->row
[row
][off
+ col
], mat
->row
[row
][off
+ col
]);
1131 for (j
= 0; j
< off
- 1 + tab
->n_col
; ++j
) {
1132 if (j
== off
- 1 + col
)
1134 isl_int_neg(mat
->row
[row
][1 + j
], mat
->row
[row
][1 + j
]);
1136 if (!isl_int_is_one(mat
->row
[row
][0]))
1137 isl_seq_normalize(mat
->ctx
, mat
->row
[row
], off
+ tab
->n_col
);
1138 for (i
= 0; i
< tab
->n_row
; ++i
) {
1141 if (isl_int_is_zero(mat
->row
[i
][off
+ col
]))
1143 isl_int_mul(mat
->row
[i
][0], mat
->row
[i
][0], mat
->row
[row
][0]);
1144 for (j
= 0; j
< off
- 1 + tab
->n_col
; ++j
) {
1145 if (j
== off
- 1 + col
)
1147 isl_int_mul(mat
->row
[i
][1 + j
],
1148 mat
->row
[i
][1 + j
], mat
->row
[row
][0]);
1149 isl_int_addmul(mat
->row
[i
][1 + j
],
1150 mat
->row
[i
][off
+ col
], mat
->row
[row
][1 + j
]);
1152 isl_int_mul(mat
->row
[i
][off
+ col
],
1153 mat
->row
[i
][off
+ col
], mat
->row
[row
][off
+ col
]);
1154 if (!isl_int_is_one(mat
->row
[i
][0]))
1155 isl_seq_normalize(mat
->ctx
, mat
->row
[i
], off
+ tab
->n_col
);
1157 t
= tab
->row_var
[row
];
1158 tab
->row_var
[row
] = tab
->col_var
[col
];
1159 tab
->col_var
[col
] = t
;
1160 var
= isl_tab_var_from_row(tab
, row
);
1163 var
= var_from_col(tab
, col
);
1166 update_row_sign(tab
, row
, col
, sgn
);
1169 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
1170 if (isl_int_is_zero(mat
->row
[i
][off
+ col
]))
1172 if (!isl_tab_var_from_row(tab
, i
)->frozen
&&
1173 isl_tab_row_is_redundant(tab
, i
)) {
1174 int redo
= isl_tab_mark_redundant(tab
, i
);
1184 /* If "var" represents a column variable, then pivot is up (sgn > 0)
1185 * or down (sgn < 0) to a row. The variable is assumed not to be
1186 * unbounded in the specified direction.
1187 * If sgn = 0, then the variable is unbounded in both directions,
1188 * and we pivot with any row we can find.
1190 static int to_row(struct isl_tab
*tab
, struct isl_tab_var
*var
, int sign
) WARN_UNUSED
;
1191 static int to_row(struct isl_tab
*tab
, struct isl_tab_var
*var
, int sign
)
1194 unsigned off
= 2 + tab
->M
;
1200 for (r
= tab
->n_redundant
; r
< tab
->n_row
; ++r
)
1201 if (!isl_int_is_zero(tab
->mat
->row
[r
][off
+var
->index
]))
1203 isl_assert(tab
->mat
->ctx
, r
< tab
->n_row
, return -1);
1205 r
= pivot_row(tab
, NULL
, sign
, var
->index
);
1206 isl_assert(tab
->mat
->ctx
, r
>= 0, return -1);
1209 return isl_tab_pivot(tab
, r
, var
->index
);
1212 /* Check whether all variables that are marked as non-negative
1213 * also have a non-negative sample value. This function is not
1214 * called from the current code but is useful during debugging.
1216 static void check_table(struct isl_tab
*tab
) __attribute__ ((unused
));
1217 static void check_table(struct isl_tab
*tab
)
1223 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
1224 struct isl_tab_var
*var
;
1225 var
= isl_tab_var_from_row(tab
, i
);
1226 if (!var
->is_nonneg
)
1229 isl_assert(tab
->mat
->ctx
,
1230 !isl_int_is_neg(tab
->mat
->row
[i
][2]), abort());
1231 if (isl_int_is_pos(tab
->mat
->row
[i
][2]))
1234 isl_assert(tab
->mat
->ctx
, !isl_int_is_neg(tab
->mat
->row
[i
][1]),
1239 /* Return the sign of the maximal value of "var".
1240 * If the sign is not negative, then on return from this function,
1241 * the sample value will also be non-negative.
1243 * If "var" is manifestly unbounded wrt positive values, we are done.
1244 * Otherwise, we pivot the variable up to a row if needed
1245 * Then we continue pivoting down until either
1246 * - no more down pivots can be performed
1247 * - the sample value is positive
1248 * - the variable is pivoted into a manifestly unbounded column
1250 static int sign_of_max(struct isl_tab
*tab
, struct isl_tab_var
*var
)
1254 if (max_is_manifestly_unbounded(tab
, var
))
1256 if (to_row(tab
, var
, 1) < 0)
1258 while (!isl_int_is_pos(tab
->mat
->row
[var
->index
][1])) {
1259 find_pivot(tab
, var
, var
, 1, &row
, &col
);
1261 return isl_int_sgn(tab
->mat
->row
[var
->index
][1]);
1262 if (isl_tab_pivot(tab
, row
, col
) < 0)
1264 if (!var
->is_row
) /* manifestly unbounded */
1270 int isl_tab_sign_of_max(struct isl_tab
*tab
, int con
)
1272 struct isl_tab_var
*var
;
1277 var
= &tab
->con
[con
];
1278 isl_assert(tab
->mat
->ctx
, !var
->is_redundant
, return -2);
1279 isl_assert(tab
->mat
->ctx
, !var
->is_zero
, return -2);
1281 return sign_of_max(tab
, var
);
1284 static int row_is_neg(struct isl_tab
*tab
, int row
)
1287 return isl_int_is_neg(tab
->mat
->row
[row
][1]);
1288 if (isl_int_is_pos(tab
->mat
->row
[row
][2]))
1290 if (isl_int_is_neg(tab
->mat
->row
[row
][2]))
1292 return isl_int_is_neg(tab
->mat
->row
[row
][1]);
1295 static int row_sgn(struct isl_tab
*tab
, int row
)
1298 return isl_int_sgn(tab
->mat
->row
[row
][1]);
1299 if (!isl_int_is_zero(tab
->mat
->row
[row
][2]))
1300 return isl_int_sgn(tab
->mat
->row
[row
][2]);
1302 return isl_int_sgn(tab
->mat
->row
[row
][1]);
1305 /* Perform pivots until the row variable "var" has a non-negative
1306 * sample value or until no more upward pivots can be performed.
1307 * Return the sign of the sample value after the pivots have been
1310 static int restore_row(struct isl_tab
*tab
, struct isl_tab_var
*var
)
1314 while (row_is_neg(tab
, var
->index
)) {
1315 find_pivot(tab
, var
, var
, 1, &row
, &col
);
1318 if (isl_tab_pivot(tab
, row
, col
) < 0)
1320 if (!var
->is_row
) /* manifestly unbounded */
1323 return row_sgn(tab
, var
->index
);
1326 /* Perform pivots until we are sure that the row variable "var"
1327 * can attain non-negative values. After return from this
1328 * function, "var" is still a row variable, but its sample
1329 * value may not be non-negative, even if the function returns 1.
1331 static int at_least_zero(struct isl_tab
*tab
, struct isl_tab_var
*var
)
1335 while (isl_int_is_neg(tab
->mat
->row
[var
->index
][1])) {
1336 find_pivot(tab
, var
, var
, 1, &row
, &col
);
1339 if (row
== var
->index
) /* manifestly unbounded */
1341 if (isl_tab_pivot(tab
, row
, col
) < 0)
1344 return !isl_int_is_neg(tab
->mat
->row
[var
->index
][1]);
1347 /* Return a negative value if "var" can attain negative values.
1348 * Return a non-negative value otherwise.
1350 * If "var" is manifestly unbounded wrt negative values, we are done.
1351 * Otherwise, if var is in a column, we can pivot it down to a row.
1352 * Then we continue pivoting down until either
1353 * - the pivot would result in a manifestly unbounded column
1354 * => we don't perform the pivot, but simply return -1
1355 * - no more down pivots can be performed
1356 * - the sample value is negative
1357 * If the sample value becomes negative and the variable is supposed
1358 * to be nonnegative, then we undo the last pivot.
1359 * However, if the last pivot has made the pivoting variable
1360 * obviously redundant, then it may have moved to another row.
1361 * In that case we look for upward pivots until we reach a non-negative
1364 static int sign_of_min(struct isl_tab
*tab
, struct isl_tab_var
*var
)
1367 struct isl_tab_var
*pivot_var
= NULL
;
1369 if (min_is_manifestly_unbounded(tab
, var
))
1373 row
= pivot_row(tab
, NULL
, -1, col
);
1374 pivot_var
= var_from_col(tab
, col
);
1375 if (isl_tab_pivot(tab
, row
, col
) < 0)
1377 if (var
->is_redundant
)
1379 if (isl_int_is_neg(tab
->mat
->row
[var
->index
][1])) {
1380 if (var
->is_nonneg
) {
1381 if (!pivot_var
->is_redundant
&&
1382 pivot_var
->index
== row
) {
1383 if (isl_tab_pivot(tab
, row
, col
) < 0)
1386 if (restore_row(tab
, var
) < -1)
1392 if (var
->is_redundant
)
1394 while (!isl_int_is_neg(tab
->mat
->row
[var
->index
][1])) {
1395 find_pivot(tab
, var
, var
, -1, &row
, &col
);
1396 if (row
== var
->index
)
1399 return isl_int_sgn(tab
->mat
->row
[var
->index
][1]);
1400 pivot_var
= var_from_col(tab
, col
);
1401 if (isl_tab_pivot(tab
, row
, col
) < 0)
1403 if (var
->is_redundant
)
1406 if (pivot_var
&& var
->is_nonneg
) {
1407 /* pivot back to non-negative value */
1408 if (!pivot_var
->is_redundant
&& pivot_var
->index
== row
) {
1409 if (isl_tab_pivot(tab
, row
, col
) < 0)
1412 if (restore_row(tab
, var
) < -1)
1418 static int row_at_most_neg_one(struct isl_tab
*tab
, int row
)
1421 if (isl_int_is_pos(tab
->mat
->row
[row
][2]))
1423 if (isl_int_is_neg(tab
->mat
->row
[row
][2]))
1426 return isl_int_is_neg(tab
->mat
->row
[row
][1]) &&
1427 isl_int_abs_ge(tab
->mat
->row
[row
][1],
1428 tab
->mat
->row
[row
][0]);
1431 /* Return 1 if "var" can attain values <= -1.
1432 * Return 0 otherwise.
1434 * If the variable "var" is supposed to be non-negative (is_nonneg is set),
1435 * then the sample value of "var" is assumed to be non-negative when the
1436 * the function is called. If 1 is returned then the constraint
1437 * is not redundant and the sample value is made non-negative again before
1438 * the function returns.
1440 int isl_tab_min_at_most_neg_one(struct isl_tab
*tab
, struct isl_tab_var
*var
)
1443 struct isl_tab_var
*pivot_var
;
1445 if (min_is_manifestly_unbounded(tab
, var
))
1449 row
= pivot_row(tab
, NULL
, -1, col
);
1450 pivot_var
= var_from_col(tab
, col
);
1451 if (isl_tab_pivot(tab
, row
, col
) < 0)
1453 if (var
->is_redundant
)
1455 if (row_at_most_neg_one(tab
, var
->index
)) {
1456 if (var
->is_nonneg
) {
1457 if (!pivot_var
->is_redundant
&&
1458 pivot_var
->index
== row
) {
1459 if (isl_tab_pivot(tab
, row
, col
) < 0)
1462 if (restore_row(tab
, var
) < -1)
1468 if (var
->is_redundant
)
1471 find_pivot(tab
, var
, var
, -1, &row
, &col
);
1472 if (row
== var
->index
) {
1473 if (var
->is_nonneg
&& restore_row(tab
, var
) < -1)
1479 pivot_var
= var_from_col(tab
, col
);
1480 if (isl_tab_pivot(tab
, row
, col
) < 0)
1482 if (var
->is_redundant
)
1484 } while (!row_at_most_neg_one(tab
, var
->index
));
1485 if (var
->is_nonneg
) {
1486 /* pivot back to non-negative value */
1487 if (!pivot_var
->is_redundant
&& pivot_var
->index
== row
)
1488 if (isl_tab_pivot(tab
, row
, col
) < 0)
1490 if (restore_row(tab
, var
) < -1)
1496 /* Return 1 if "var" can attain values >= 1.
1497 * Return 0 otherwise.
1499 static int at_least_one(struct isl_tab
*tab
, struct isl_tab_var
*var
)
1504 if (max_is_manifestly_unbounded(tab
, var
))
1506 if (to_row(tab
, var
, 1) < 0)
1508 r
= tab
->mat
->row
[var
->index
];
1509 while (isl_int_lt(r
[1], r
[0])) {
1510 find_pivot(tab
, var
, var
, 1, &row
, &col
);
1512 return isl_int_ge(r
[1], r
[0]);
1513 if (row
== var
->index
) /* manifestly unbounded */
1515 if (isl_tab_pivot(tab
, row
, col
) < 0)
1521 static void swap_cols(struct isl_tab
*tab
, int col1
, int col2
)
1524 unsigned off
= 2 + tab
->M
;
1525 t
= tab
->col_var
[col1
];
1526 tab
->col_var
[col1
] = tab
->col_var
[col2
];
1527 tab
->col_var
[col2
] = t
;
1528 var_from_col(tab
, col1
)->index
= col1
;
1529 var_from_col(tab
, col2
)->index
= col2
;
1530 tab
->mat
= isl_mat_swap_cols(tab
->mat
, off
+ col1
, off
+ col2
);
1533 /* Mark column with index "col" as representing a zero variable.
1534 * If we may need to undo the operation the column is kept,
1535 * but no longer considered.
1536 * Otherwise, the column is simply removed.
1538 * The column may be interchanged with some other column. If it
1539 * is interchanged with a later column, return 1. Otherwise return 0.
1540 * If the columns are checked in order in the calling function,
1541 * then a return value of 1 means that the column with the given
1542 * column number may now contain a different column that
1543 * hasn't been checked yet.
1545 int isl_tab_kill_col(struct isl_tab
*tab
, int col
)
1547 var_from_col(tab
, col
)->is_zero
= 1;
1548 if (tab
->need_undo
) {
1549 if (isl_tab_push_var(tab
, isl_tab_undo_zero
,
1550 var_from_col(tab
, col
)) < 0)
1552 if (col
!= tab
->n_dead
)
1553 swap_cols(tab
, col
, tab
->n_dead
);
1557 if (col
!= tab
->n_col
- 1)
1558 swap_cols(tab
, col
, tab
->n_col
- 1);
1559 var_from_col(tab
, tab
->n_col
- 1)->index
= -1;
1565 static int row_is_manifestly_non_integral(struct isl_tab
*tab
, int row
)
1567 unsigned off
= 2 + tab
->M
;
1569 if (tab
->M
&& !isl_int_eq(tab
->mat
->row
[row
][2],
1570 tab
->mat
->row
[row
][0]))
1572 if (isl_seq_first_non_zero(tab
->mat
->row
[row
] + off
+ tab
->n_dead
,
1573 tab
->n_col
- tab
->n_dead
) != -1)
1576 return !isl_int_is_divisible_by(tab
->mat
->row
[row
][1],
1577 tab
->mat
->row
[row
][0]);
1580 /* For integer tableaus, check if any of the coordinates are stuck
1581 * at a non-integral value.
1583 static int tab_is_manifestly_empty(struct isl_tab
*tab
)
1592 for (i
= 0; i
< tab
->n_var
; ++i
) {
1593 if (!tab
->var
[i
].is_row
)
1595 if (row_is_manifestly_non_integral(tab
, tab
->var
[i
].index
))
1602 /* Row variable "var" is non-negative and cannot attain any values
1603 * larger than zero. This means that the coefficients of the unrestricted
1604 * column variables are zero and that the coefficients of the non-negative
1605 * column variables are zero or negative.
1606 * Each of the non-negative variables with a negative coefficient can
1607 * then also be written as the negative sum of non-negative variables
1608 * and must therefore also be zero.
1610 * If "temp_var" is set, then "var" is a temporary variable that
1611 * will be removed after this function returns and for which
1612 * no information is recorded on the undo stack.
1613 * Do not add any undo records involving this variable in this case
1614 * since the variable will have been removed before any future undo
1615 * operations. Also avoid marking the variable as redundant,
1616 * since that either adds an undo record or needlessly removes the row
1617 * (the caller will take care of removing the row).
1619 static isl_stat
close_row(struct isl_tab
*tab
, struct isl_tab_var
*var
,
1620 int temp_var
) WARN_UNUSED
;
1621 static isl_stat
close_row(struct isl_tab
*tab
, struct isl_tab_var
*var
,
1625 struct isl_mat
*mat
= tab
->mat
;
1626 unsigned off
= 2 + tab
->M
;
1628 if (!var
->is_nonneg
)
1629 isl_die(isl_tab_get_ctx(tab
), isl_error_internal
,
1630 "expecting non-negative variable",
1631 return isl_stat_error
);
1633 if (!temp_var
&& tab
->need_undo
)
1634 if (isl_tab_push_var(tab
, isl_tab_undo_zero
, var
) < 0)
1635 return isl_stat_error
;
1636 for (j
= tab
->n_dead
; j
< tab
->n_col
; ++j
) {
1638 if (isl_int_is_zero(mat
->row
[var
->index
][off
+ j
]))
1640 if (isl_int_is_pos(mat
->row
[var
->index
][off
+ j
]))
1641 isl_die(isl_tab_get_ctx(tab
), isl_error_internal
,
1642 "row cannot have positive coefficients",
1643 return isl_stat_error
);
1644 recheck
= isl_tab_kill_col(tab
, j
);
1646 return isl_stat_error
;
1650 if (!temp_var
&& isl_tab_mark_redundant(tab
, var
->index
) < 0)
1651 return isl_stat_error
;
1652 if (tab_is_manifestly_empty(tab
) && isl_tab_mark_empty(tab
) < 0)
1653 return isl_stat_error
;
1657 /* Add a constraint to the tableau and allocate a row for it.
1658 * Return the index into the constraint array "con".
1660 * This function assumes that at least one more row and at least
1661 * one more element in the constraint array are available in the tableau.
1663 int isl_tab_allocate_con(struct isl_tab
*tab
)
1667 isl_assert(tab
->mat
->ctx
, tab
->n_row
< tab
->mat
->n_row
, return -1);
1668 isl_assert(tab
->mat
->ctx
, tab
->n_con
< tab
->max_con
, return -1);
1671 tab
->con
[r
].index
= tab
->n_row
;
1672 tab
->con
[r
].is_row
= 1;
1673 tab
->con
[r
].is_nonneg
= 0;
1674 tab
->con
[r
].is_zero
= 0;
1675 tab
->con
[r
].is_redundant
= 0;
1676 tab
->con
[r
].frozen
= 0;
1677 tab
->con
[r
].negated
= 0;
1678 tab
->row_var
[tab
->n_row
] = ~r
;
1682 if (isl_tab_push_var(tab
, isl_tab_undo_allocate
, &tab
->con
[r
]) < 0)
1688 /* Move the entries in tab->var up one position, starting at "first",
1689 * creating room for an extra entry at position "first".
1690 * Since some of the entries of tab->row_var and tab->col_var contain
1691 * indices into this array, they have to be updated accordingly.
1693 static int var_insert_entry(struct isl_tab
*tab
, int first
)
1697 if (tab
->n_var
>= tab
->max_var
)
1698 isl_die(isl_tab_get_ctx(tab
), isl_error_internal
,
1699 "not enough room for new variable", return -1);
1700 if (first
> tab
->n_var
)
1701 isl_die(isl_tab_get_ctx(tab
), isl_error_internal
,
1702 "invalid initial position", return -1);
1704 for (i
= tab
->n_var
- 1; i
>= first
; --i
) {
1705 tab
->var
[i
+ 1] = tab
->var
[i
];
1706 if (tab
->var
[i
+ 1].is_row
)
1707 tab
->row_var
[tab
->var
[i
+ 1].index
]++;
1709 tab
->col_var
[tab
->var
[i
+ 1].index
]++;
1717 /* Drop the entry at position "first" in tab->var, moving all
1718 * subsequent entries down.
1719 * Since some of the entries of tab->row_var and tab->col_var contain
1720 * indices into this array, they have to be updated accordingly.
1722 static int var_drop_entry(struct isl_tab
*tab
, int first
)
1726 if (first
>= tab
->n_var
)
1727 isl_die(isl_tab_get_ctx(tab
), isl_error_internal
,
1728 "invalid initial position", return -1);
1732 for (i
= first
; i
< tab
->n_var
; ++i
) {
1733 tab
->var
[i
] = tab
->var
[i
+ 1];
1734 if (tab
->var
[i
+ 1].is_row
)
1735 tab
->row_var
[tab
->var
[i
].index
]--;
1737 tab
->col_var
[tab
->var
[i
].index
]--;
1743 /* Add a variable to the tableau at position "r" and allocate a column for it.
1744 * Return the index into the variable array "var", i.e., "r",
1747 int isl_tab_insert_var(struct isl_tab
*tab
, int r
)
1750 unsigned off
= 2 + tab
->M
;
1752 isl_assert(tab
->mat
->ctx
, tab
->n_col
< tab
->mat
->n_col
, return -1);
1754 if (var_insert_entry(tab
, r
) < 0)
1757 tab
->var
[r
].index
= tab
->n_col
;
1758 tab
->var
[r
].is_row
= 0;
1759 tab
->var
[r
].is_nonneg
= 0;
1760 tab
->var
[r
].is_zero
= 0;
1761 tab
->var
[r
].is_redundant
= 0;
1762 tab
->var
[r
].frozen
= 0;
1763 tab
->var
[r
].negated
= 0;
1764 tab
->col_var
[tab
->n_col
] = r
;
1766 for (i
= 0; i
< tab
->n_row
; ++i
)
1767 isl_int_set_si(tab
->mat
->row
[i
][off
+ tab
->n_col
], 0);
1770 if (isl_tab_push_var(tab
, isl_tab_undo_allocate
, &tab
->var
[r
]) < 0)
1776 /* Add a variable to the tableau and allocate a column for it.
1777 * Return the index into the variable array "var".
1779 int isl_tab_allocate_var(struct isl_tab
*tab
)
1784 return isl_tab_insert_var(tab
, tab
->n_var
);
1787 /* Add a row to the tableau. The row is given as an affine combination
1788 * of the original variables and needs to be expressed in terms of the
1791 * This function assumes that at least one more row and at least
1792 * one more element in the constraint array are available in the tableau.
1794 * We add each term in turn.
1795 * If r = n/d_r is the current sum and we need to add k x, then
1796 * if x is a column variable, we increase the numerator of
1797 * this column by k d_r
1798 * if x = f/d_x is a row variable, then the new representation of r is
1800 * n k f d_x/g n + d_r/g k f m/d_r n + m/d_g k f
1801 * --- + --- = ------------------- = -------------------
1802 * d_r d_r d_r d_x/g m
1804 * with g the gcd of d_r and d_x and m the lcm of d_r and d_x.
1806 * If tab->M is set, then, internally, each variable x is represented
1807 * as x' - M. We then also need no subtract k d_r from the coefficient of M.
1809 int isl_tab_add_row(struct isl_tab
*tab
, isl_int
*line
)
1815 unsigned off
= 2 + tab
->M
;
1817 r
= isl_tab_allocate_con(tab
);
1823 row
= tab
->mat
->row
[tab
->con
[r
].index
];
1824 isl_int_set_si(row
[0], 1);
1825 isl_int_set(row
[1], line
[0]);
1826 isl_seq_clr(row
+ 2, tab
->M
+ tab
->n_col
);
1827 for (i
= 0; i
< tab
->n_var
; ++i
) {
1828 if (tab
->var
[i
].is_zero
)
1830 if (tab
->var
[i
].is_row
) {
1832 row
[0], tab
->mat
->row
[tab
->var
[i
].index
][0]);
1833 isl_int_swap(a
, row
[0]);
1834 isl_int_divexact(a
, row
[0], a
);
1836 row
[0], tab
->mat
->row
[tab
->var
[i
].index
][0]);
1837 isl_int_mul(b
, b
, line
[1 + i
]);
1838 isl_seq_combine(row
+ 1, a
, row
+ 1,
1839 b
, tab
->mat
->row
[tab
->var
[i
].index
] + 1,
1840 1 + tab
->M
+ tab
->n_col
);
1842 isl_int_addmul(row
[off
+ tab
->var
[i
].index
],
1843 line
[1 + i
], row
[0]);
1844 if (tab
->M
&& i
>= tab
->n_param
&& i
< tab
->n_var
- tab
->n_div
)
1845 isl_int_submul(row
[2], line
[1 + i
], row
[0]);
1847 isl_seq_normalize(tab
->mat
->ctx
, row
, off
+ tab
->n_col
);
1852 tab
->row_sign
[tab
->con
[r
].index
] = isl_tab_row_unknown
;
1857 static int drop_row(struct isl_tab
*tab
, int row
)
1859 isl_assert(tab
->mat
->ctx
, ~tab
->row_var
[row
] == tab
->n_con
- 1, return -1);
1860 if (row
!= tab
->n_row
- 1)
1861 swap_rows(tab
, row
, tab
->n_row
- 1);
1867 /* Drop the variable in column "col" along with the column.
1868 * The column is removed first because it may need to be moved
1869 * into the last position and this process requires
1870 * the contents of the col_var array in a state
1871 * before the removal of the variable.
1873 static int drop_col(struct isl_tab
*tab
, int col
)
1877 var
= tab
->col_var
[col
];
1878 if (col
!= tab
->n_col
- 1)
1879 swap_cols(tab
, col
, tab
->n_col
- 1);
1881 if (var_drop_entry(tab
, var
) < 0)
1886 /* Add inequality "ineq" and check if it conflicts with the
1887 * previously added constraints or if it is obviously redundant.
1889 * This function assumes that at least one more row and at least
1890 * one more element in the constraint array are available in the tableau.
1892 int isl_tab_add_ineq(struct isl_tab
*tab
, isl_int
*ineq
)
1901 struct isl_basic_map
*bmap
= tab
->bmap
;
1903 isl_assert(tab
->mat
->ctx
, tab
->n_eq
== bmap
->n_eq
, return -1);
1904 isl_assert(tab
->mat
->ctx
,
1905 tab
->n_con
== bmap
->n_eq
+ bmap
->n_ineq
, return -1);
1906 tab
->bmap
= isl_basic_map_add_ineq(tab
->bmap
, ineq
);
1907 if (isl_tab_push(tab
, isl_tab_undo_bmap_ineq
) < 0)
1914 isl_int_set_si(cst
, 0);
1915 isl_int_swap(ineq
[0], cst
);
1917 r
= isl_tab_add_row(tab
, ineq
);
1919 isl_int_swap(ineq
[0], cst
);
1924 tab
->con
[r
].is_nonneg
= 1;
1925 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
1927 if (isl_tab_row_is_redundant(tab
, tab
->con
[r
].index
)) {
1928 if (isl_tab_mark_redundant(tab
, tab
->con
[r
].index
) < 0)
1933 sgn
= restore_row(tab
, &tab
->con
[r
]);
1937 return isl_tab_mark_empty(tab
);
1938 if (tab
->con
[r
].is_row
&& isl_tab_row_is_redundant(tab
, tab
->con
[r
].index
))
1939 if (isl_tab_mark_redundant(tab
, tab
->con
[r
].index
) < 0)
1944 /* Pivot a non-negative variable down until it reaches the value zero
1945 * and then pivot the variable into a column position.
1947 static int to_col(struct isl_tab
*tab
, struct isl_tab_var
*var
) WARN_UNUSED
;
1948 static int to_col(struct isl_tab
*tab
, struct isl_tab_var
*var
)
1952 unsigned off
= 2 + tab
->M
;
1957 while (isl_int_is_pos(tab
->mat
->row
[var
->index
][1])) {
1958 find_pivot(tab
, var
, NULL
, -1, &row
, &col
);
1959 isl_assert(tab
->mat
->ctx
, row
!= -1, return -1);
1960 if (isl_tab_pivot(tab
, row
, col
) < 0)
1966 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
)
1967 if (!isl_int_is_zero(tab
->mat
->row
[var
->index
][off
+ i
]))
1970 isl_assert(tab
->mat
->ctx
, i
< tab
->n_col
, return -1);
1971 if (isl_tab_pivot(tab
, var
->index
, i
) < 0)
1977 /* We assume Gaussian elimination has been performed on the equalities.
1978 * The equalities can therefore never conflict.
1979 * Adding the equalities is currently only really useful for a later call
1980 * to isl_tab_ineq_type.
1982 * This function assumes that at least one more row and at least
1983 * one more element in the constraint array are available in the tableau.
1985 static struct isl_tab
*add_eq(struct isl_tab
*tab
, isl_int
*eq
)
1992 r
= isl_tab_add_row(tab
, eq
);
1996 r
= tab
->con
[r
].index
;
1997 i
= isl_seq_first_non_zero(tab
->mat
->row
[r
] + 2 + tab
->M
+ tab
->n_dead
,
1998 tab
->n_col
- tab
->n_dead
);
1999 isl_assert(tab
->mat
->ctx
, i
>= 0, goto error
);
2001 if (isl_tab_pivot(tab
, r
, i
) < 0)
2003 if (isl_tab_kill_col(tab
, i
) < 0)
2013 static int row_is_manifestly_zero(struct isl_tab
*tab
, int row
)
2015 unsigned off
= 2 + tab
->M
;
2017 if (!isl_int_is_zero(tab
->mat
->row
[row
][1]))
2019 if (tab
->M
&& !isl_int_is_zero(tab
->mat
->row
[row
][2]))
2021 return isl_seq_first_non_zero(tab
->mat
->row
[row
] + off
+ tab
->n_dead
,
2022 tab
->n_col
- tab
->n_dead
) == -1;
2025 /* Add an equality that is known to be valid for the given tableau.
2027 * This function assumes that at least one more row and at least
2028 * one more element in the constraint array are available in the tableau.
2030 int isl_tab_add_valid_eq(struct isl_tab
*tab
, isl_int
*eq
)
2032 struct isl_tab_var
*var
;
2037 r
= isl_tab_add_row(tab
, eq
);
2043 if (row_is_manifestly_zero(tab
, r
)) {
2045 if (isl_tab_mark_redundant(tab
, r
) < 0)
2050 if (isl_int_is_neg(tab
->mat
->row
[r
][1])) {
2051 isl_seq_neg(tab
->mat
->row
[r
] + 1, tab
->mat
->row
[r
] + 1,
2056 if (to_col(tab
, var
) < 0)
2059 if (isl_tab_kill_col(tab
, var
->index
) < 0)
2065 /* Add a zero row to "tab" and return the corresponding index
2066 * in the constraint array.
2068 * This function assumes that at least one more row and at least
2069 * one more element in the constraint array are available in the tableau.
2071 static int add_zero_row(struct isl_tab
*tab
)
2076 r
= isl_tab_allocate_con(tab
);
2080 row
= tab
->mat
->row
[tab
->con
[r
].index
];
2081 isl_seq_clr(row
+ 1, 1 + tab
->M
+ tab
->n_col
);
2082 isl_int_set_si(row
[0], 1);
2087 /* Add equality "eq" and check if it conflicts with the
2088 * previously added constraints or if it is obviously redundant.
2090 * This function assumes that at least one more row and at least
2091 * one more element in the constraint array are available in the tableau.
2092 * If tab->bmap is set, then two rows are needed instead of one.
2094 int isl_tab_add_eq(struct isl_tab
*tab
, isl_int
*eq
)
2096 struct isl_tab_undo
*snap
= NULL
;
2097 struct isl_tab_var
*var
;
2105 isl_assert(tab
->mat
->ctx
, !tab
->M
, return -1);
2108 snap
= isl_tab_snap(tab
);
2112 isl_int_set_si(cst
, 0);
2113 isl_int_swap(eq
[0], cst
);
2115 r
= isl_tab_add_row(tab
, eq
);
2117 isl_int_swap(eq
[0], cst
);
2125 if (row_is_manifestly_zero(tab
, row
)) {
2127 return isl_tab_rollback(tab
, snap
);
2128 return drop_row(tab
, row
);
2132 tab
->bmap
= isl_basic_map_add_ineq(tab
->bmap
, eq
);
2133 if (isl_tab_push(tab
, isl_tab_undo_bmap_ineq
) < 0)
2135 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
2136 tab
->bmap
= isl_basic_map_add_ineq(tab
->bmap
, eq
);
2137 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
2138 if (isl_tab_push(tab
, isl_tab_undo_bmap_ineq
) < 0)
2142 if (add_zero_row(tab
) < 0)
2146 sgn
= isl_int_sgn(tab
->mat
->row
[row
][1]);
2149 isl_seq_neg(tab
->mat
->row
[row
] + 1, tab
->mat
->row
[row
] + 1,
2156 sgn
= sign_of_max(tab
, var
);
2160 if (isl_tab_mark_empty(tab
) < 0)
2167 if (to_col(tab
, var
) < 0)
2170 if (isl_tab_kill_col(tab
, var
->index
) < 0)
2176 /* Construct and return an inequality that expresses an upper bound
2178 * In particular, if the div is given by
2182 * then the inequality expresses
2186 static struct isl_vec
*ineq_for_div(struct isl_basic_map
*bmap
, unsigned div
)
2190 struct isl_vec
*ineq
;
2195 total
= isl_basic_map_total_dim(bmap
);
2196 div_pos
= 1 + total
- bmap
->n_div
+ div
;
2198 ineq
= isl_vec_alloc(bmap
->ctx
, 1 + total
);
2202 isl_seq_cpy(ineq
->el
, bmap
->div
[div
] + 1, 1 + total
);
2203 isl_int_neg(ineq
->el
[div_pos
], bmap
->div
[div
][0]);
2207 /* For a div d = floor(f/m), add the constraints
2210 * -(f-(m-1)) + m d >= 0
2212 * Note that the second constraint is the negation of
2216 * If add_ineq is not NULL, then this function is used
2217 * instead of isl_tab_add_ineq to effectively add the inequalities.
2219 * This function assumes that at least two more rows and at least
2220 * two more elements in the constraint array are available in the tableau.
2222 static int add_div_constraints(struct isl_tab
*tab
, unsigned div
,
2223 int (*add_ineq
)(void *user
, isl_int
*), void *user
)
2227 struct isl_vec
*ineq
;
2229 total
= isl_basic_map_total_dim(tab
->bmap
);
2230 div_pos
= 1 + total
- tab
->bmap
->n_div
+ div
;
2232 ineq
= ineq_for_div(tab
->bmap
, div
);
2237 if (add_ineq(user
, ineq
->el
) < 0)
2240 if (isl_tab_add_ineq(tab
, ineq
->el
) < 0)
2244 isl_seq_neg(ineq
->el
, tab
->bmap
->div
[div
] + 1, 1 + total
);
2245 isl_int_set(ineq
->el
[div_pos
], tab
->bmap
->div
[div
][0]);
2246 isl_int_add(ineq
->el
[0], ineq
->el
[0], ineq
->el
[div_pos
]);
2247 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
2250 if (add_ineq(user
, ineq
->el
) < 0)
2253 if (isl_tab_add_ineq(tab
, ineq
->el
) < 0)
2265 /* Check whether the div described by "div" is obviously non-negative.
2266 * If we are using a big parameter, then we will encode the div
2267 * as div' = M + div, which is always non-negative.
2268 * Otherwise, we check whether div is a non-negative affine combination
2269 * of non-negative variables.
2271 static int div_is_nonneg(struct isl_tab
*tab
, __isl_keep isl_vec
*div
)
2278 if (isl_int_is_neg(div
->el
[1]))
2281 for (i
= 0; i
< tab
->n_var
; ++i
) {
2282 if (isl_int_is_neg(div
->el
[2 + i
]))
2284 if (isl_int_is_zero(div
->el
[2 + i
]))
2286 if (!tab
->var
[i
].is_nonneg
)
2293 /* Insert an extra div, prescribed by "div", to the tableau and
2294 * the associated bmap (which is assumed to be non-NULL).
2295 * The extra integer division is inserted at (tableau) position "pos".
2296 * Return "pos" or -1 if an error occurred.
2298 * If add_ineq is not NULL, then this function is used instead
2299 * of isl_tab_add_ineq to add the div constraints.
2300 * This complication is needed because the code in isl_tab_pip
2301 * wants to perform some extra processing when an inequality
2302 * is added to the tableau.
2304 int isl_tab_insert_div(struct isl_tab
*tab
, int pos
, __isl_keep isl_vec
*div
,
2305 int (*add_ineq
)(void *user
, isl_int
*), void *user
)
2314 if (div
->size
!= 1 + 1 + tab
->n_var
)
2315 isl_die(isl_tab_get_ctx(tab
), isl_error_invalid
,
2316 "unexpected size", return -1);
2318 isl_assert(tab
->mat
->ctx
, tab
->bmap
, return -1);
2319 n_div
= isl_basic_map_dim(tab
->bmap
, isl_dim_div
);
2320 o_div
= tab
->n_var
- n_div
;
2321 if (pos
< o_div
|| pos
> tab
->n_var
)
2322 isl_die(isl_tab_get_ctx(tab
), isl_error_invalid
,
2323 "invalid position", return -1);
2325 nonneg
= div_is_nonneg(tab
, div
);
2327 if (isl_tab_extend_cons(tab
, 3) < 0)
2329 if (isl_tab_extend_vars(tab
, 1) < 0)
2331 r
= isl_tab_insert_var(tab
, pos
);
2336 tab
->var
[r
].is_nonneg
= 1;
2338 tab
->bmap
= isl_basic_map_insert_div(tab
->bmap
, pos
- o_div
, div
);
2341 if (isl_tab_push_var(tab
, isl_tab_undo_bmap_div
, &tab
->var
[r
]) < 0)
2344 if (add_div_constraints(tab
, pos
- o_div
, add_ineq
, user
) < 0)
2350 /* Add an extra div, prescribed by "div", to the tableau and
2351 * the associated bmap (which is assumed to be non-NULL).
2353 int isl_tab_add_div(struct isl_tab
*tab
, __isl_keep isl_vec
*div
)
2357 return isl_tab_insert_div(tab
, tab
->n_var
, div
, NULL
, NULL
);
2360 /* If "track" is set, then we want to keep track of all constraints in tab
2361 * in its bmap field. This field is initialized from a copy of "bmap",
2362 * so we need to make sure that all constraints in "bmap" also appear
2363 * in the constructed tab.
2365 __isl_give
struct isl_tab
*isl_tab_from_basic_map(
2366 __isl_keep isl_basic_map
*bmap
, int track
)
2369 struct isl_tab
*tab
;
2373 tab
= isl_tab_alloc(bmap
->ctx
,
2374 isl_basic_map_total_dim(bmap
) + bmap
->n_ineq
+ 1,
2375 isl_basic_map_total_dim(bmap
), 0);
2378 tab
->preserve
= track
;
2379 tab
->rational
= ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
2380 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
)) {
2381 if (isl_tab_mark_empty(tab
) < 0)
2385 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
2386 tab
= add_eq(tab
, bmap
->eq
[i
]);
2390 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
2391 if (isl_tab_add_ineq(tab
, bmap
->ineq
[i
]) < 0)
2397 if (track
&& isl_tab_track_bmap(tab
, isl_basic_map_copy(bmap
)) < 0)
2405 __isl_give
struct isl_tab
*isl_tab_from_basic_set(
2406 __isl_keep isl_basic_set
*bset
, int track
)
2408 return isl_tab_from_basic_map(bset
, track
);
2411 /* Construct a tableau corresponding to the recession cone of "bset".
2413 struct isl_tab
*isl_tab_from_recession_cone(__isl_keep isl_basic_set
*bset
,
2418 struct isl_tab
*tab
;
2419 unsigned offset
= 0;
2424 offset
= isl_basic_set_dim(bset
, isl_dim_param
);
2425 tab
= isl_tab_alloc(bset
->ctx
, bset
->n_eq
+ bset
->n_ineq
,
2426 isl_basic_set_total_dim(bset
) - offset
, 0);
2429 tab
->rational
= ISL_F_ISSET(bset
, ISL_BASIC_SET_RATIONAL
);
2433 isl_int_set_si(cst
, 0);
2434 for (i
= 0; i
< bset
->n_eq
; ++i
) {
2435 isl_int_swap(bset
->eq
[i
][offset
], cst
);
2437 if (isl_tab_add_eq(tab
, bset
->eq
[i
] + offset
) < 0)
2440 tab
= add_eq(tab
, bset
->eq
[i
]);
2441 isl_int_swap(bset
->eq
[i
][offset
], cst
);
2445 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
2447 isl_int_swap(bset
->ineq
[i
][offset
], cst
);
2448 r
= isl_tab_add_row(tab
, bset
->ineq
[i
] + offset
);
2449 isl_int_swap(bset
->ineq
[i
][offset
], cst
);
2452 tab
->con
[r
].is_nonneg
= 1;
2453 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
2465 /* Assuming "tab" is the tableau of a cone, check if the cone is
2466 * bounded, i.e., if it is empty or only contains the origin.
2468 int isl_tab_cone_is_bounded(struct isl_tab
*tab
)
2476 if (tab
->n_dead
== tab
->n_col
)
2480 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
2481 struct isl_tab_var
*var
;
2483 var
= isl_tab_var_from_row(tab
, i
);
2484 if (!var
->is_nonneg
)
2486 sgn
= sign_of_max(tab
, var
);
2491 if (close_row(tab
, var
, 0) < 0)
2495 if (tab
->n_dead
== tab
->n_col
)
2497 if (i
== tab
->n_row
)
2502 int isl_tab_sample_is_integer(struct isl_tab
*tab
)
2509 for (i
= 0; i
< tab
->n_var
; ++i
) {
2511 if (!tab
->var
[i
].is_row
)
2513 row
= tab
->var
[i
].index
;
2514 if (!isl_int_is_divisible_by(tab
->mat
->row
[row
][1],
2515 tab
->mat
->row
[row
][0]))
2521 static struct isl_vec
*extract_integer_sample(struct isl_tab
*tab
)
2524 struct isl_vec
*vec
;
2526 vec
= isl_vec_alloc(tab
->mat
->ctx
, 1 + tab
->n_var
);
2530 isl_int_set_si(vec
->block
.data
[0], 1);
2531 for (i
= 0; i
< tab
->n_var
; ++i
) {
2532 if (!tab
->var
[i
].is_row
)
2533 isl_int_set_si(vec
->block
.data
[1 + i
], 0);
2535 int row
= tab
->var
[i
].index
;
2536 isl_int_divexact(vec
->block
.data
[1 + i
],
2537 tab
->mat
->row
[row
][1], tab
->mat
->row
[row
][0]);
2544 struct isl_vec
*isl_tab_get_sample_value(struct isl_tab
*tab
)
2547 struct isl_vec
*vec
;
2553 vec
= isl_vec_alloc(tab
->mat
->ctx
, 1 + tab
->n_var
);
2559 isl_int_set_si(vec
->block
.data
[0], 1);
2560 for (i
= 0; i
< tab
->n_var
; ++i
) {
2562 if (!tab
->var
[i
].is_row
) {
2563 isl_int_set_si(vec
->block
.data
[1 + i
], 0);
2566 row
= tab
->var
[i
].index
;
2567 isl_int_gcd(m
, vec
->block
.data
[0], tab
->mat
->row
[row
][0]);
2568 isl_int_divexact(m
, tab
->mat
->row
[row
][0], m
);
2569 isl_seq_scale(vec
->block
.data
, vec
->block
.data
, m
, 1 + i
);
2570 isl_int_divexact(m
, vec
->block
.data
[0], tab
->mat
->row
[row
][0]);
2571 isl_int_mul(vec
->block
.data
[1 + i
], m
, tab
->mat
->row
[row
][1]);
2573 vec
= isl_vec_normalize(vec
);
2579 /* Update "bmap" based on the results of the tableau "tab".
2580 * In particular, implicit equalities are made explicit, redundant constraints
2581 * are removed and if the sample value happens to be integer, it is stored
2582 * in "bmap" (unless "bmap" already had an integer sample).
2584 * The tableau is assumed to have been created from "bmap" using
2585 * isl_tab_from_basic_map.
2587 struct isl_basic_map
*isl_basic_map_update_from_tab(struct isl_basic_map
*bmap
,
2588 struct isl_tab
*tab
)
2600 bmap
= isl_basic_map_set_to_empty(bmap
);
2602 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
2603 if (isl_tab_is_equality(tab
, n_eq
+ i
))
2604 isl_basic_map_inequality_to_equality(bmap
, i
);
2605 else if (isl_tab_is_redundant(tab
, n_eq
+ i
))
2606 isl_basic_map_drop_inequality(bmap
, i
);
2608 if (bmap
->n_eq
!= n_eq
)
2609 bmap
= isl_basic_map_gauss(bmap
, NULL
);
2610 if (!tab
->rational
&&
2611 bmap
&& !bmap
->sample
&& isl_tab_sample_is_integer(tab
))
2612 bmap
->sample
= extract_integer_sample(tab
);
2616 struct isl_basic_set
*isl_basic_set_update_from_tab(struct isl_basic_set
*bset
,
2617 struct isl_tab
*tab
)
2619 return bset_from_bmap(isl_basic_map_update_from_tab(bset_to_bmap(bset
),
2623 /* Drop the last constraint added to "tab" in position "r".
2624 * The constraint is expected to have remained in a row.
2626 static isl_stat
drop_last_con_in_row(struct isl_tab
*tab
, int r
)
2628 if (!tab
->con
[r
].is_row
)
2629 isl_die(isl_tab_get_ctx(tab
), isl_error_internal
,
2630 "row unexpectedly moved to column",
2631 return isl_stat_error
);
2632 if (r
+ 1 != tab
->n_con
)
2633 isl_die(isl_tab_get_ctx(tab
), isl_error_internal
,
2634 "additional constraints added", return isl_stat_error
);
2635 if (drop_row(tab
, tab
->con
[r
].index
) < 0)
2636 return isl_stat_error
;
2641 /* Given a non-negative variable "var", temporarily add a new non-negative
2642 * variable that is the opposite of "var", ensuring that "var" can only attain
2643 * the value zero. The new variable is removed again before this function
2644 * returns. However, the effect of forcing "var" to be zero remains.
2645 * If var = n/d is a row variable, then the new variable = -n/d.
2646 * If var is a column variables, then the new variable = -var.
2647 * If the new variable cannot attain non-negative values, then
2648 * the resulting tableau is empty.
2649 * Otherwise, we know the value will be zero and we close the row.
2651 static isl_stat
cut_to_hyperplane(struct isl_tab
*tab
, struct isl_tab_var
*var
)
2656 unsigned off
= 2 + tab
->M
;
2660 if (var
->is_redundant
|| !var
->is_nonneg
)
2661 isl_die(isl_tab_get_ctx(tab
), isl_error_invalid
,
2662 "expecting non-redundant non-negative variable",
2663 return isl_stat_error
);
2665 if (isl_tab_extend_cons(tab
, 1) < 0)
2666 return isl_stat_error
;
2669 tab
->con
[r
].index
= tab
->n_row
;
2670 tab
->con
[r
].is_row
= 1;
2671 tab
->con
[r
].is_nonneg
= 0;
2672 tab
->con
[r
].is_zero
= 0;
2673 tab
->con
[r
].is_redundant
= 0;
2674 tab
->con
[r
].frozen
= 0;
2675 tab
->con
[r
].negated
= 0;
2676 tab
->row_var
[tab
->n_row
] = ~r
;
2677 row
= tab
->mat
->row
[tab
->n_row
];
2680 isl_int_set(row
[0], tab
->mat
->row
[var
->index
][0]);
2681 isl_seq_neg(row
+ 1,
2682 tab
->mat
->row
[var
->index
] + 1, 1 + tab
->n_col
);
2684 isl_int_set_si(row
[0], 1);
2685 isl_seq_clr(row
+ 1, 1 + tab
->n_col
);
2686 isl_int_set_si(row
[off
+ var
->index
], -1);
2692 sgn
= sign_of_max(tab
, &tab
->con
[r
]);
2694 return isl_stat_error
;
2696 if (drop_last_con_in_row(tab
, r
) < 0)
2697 return isl_stat_error
;
2698 if (isl_tab_mark_empty(tab
) < 0)
2699 return isl_stat_error
;
2702 tab
->con
[r
].is_nonneg
= 1;
2704 if (close_row(tab
, &tab
->con
[r
], 1) < 0)
2705 return isl_stat_error
;
2706 if (drop_last_con_in_row(tab
, r
) < 0)
2707 return isl_stat_error
;
2712 /* Given a tableau "tab" and an inequality constraint "con" of the tableau,
2713 * relax the inequality by one. That is, the inequality r >= 0 is replaced
2714 * by r' = r + 1 >= 0.
2715 * If r is a row variable, we simply increase the constant term by one
2716 * (taking into account the denominator).
2717 * If r is a column variable, then we need to modify each row that
2718 * refers to r = r' - 1 by substituting this equality, effectively
2719 * subtracting the coefficient of the column from the constant.
2720 * We should only do this if the minimum is manifestly unbounded,
2721 * however. Otherwise, we may end up with negative sample values
2722 * for non-negative variables.
2723 * So, if r is a column variable with a minimum that is not
2724 * manifestly unbounded, then we need to move it to a row.
2725 * However, the sample value of this row may be negative,
2726 * even after the relaxation, so we need to restore it.
2727 * We therefore prefer to pivot a column up to a row, if possible.
2729 int isl_tab_relax(struct isl_tab
*tab
, int con
)
2731 struct isl_tab_var
*var
;
2736 var
= &tab
->con
[con
];
2738 if (var
->is_row
&& (var
->index
< 0 || var
->index
< tab
->n_redundant
))
2739 isl_die(tab
->mat
->ctx
, isl_error_invalid
,
2740 "cannot relax redundant constraint", return -1);
2741 if (!var
->is_row
&& (var
->index
< 0 || var
->index
< tab
->n_dead
))
2742 isl_die(tab
->mat
->ctx
, isl_error_invalid
,
2743 "cannot relax dead constraint", return -1);
2745 if (!var
->is_row
&& !max_is_manifestly_unbounded(tab
, var
))
2746 if (to_row(tab
, var
, 1) < 0)
2748 if (!var
->is_row
&& !min_is_manifestly_unbounded(tab
, var
))
2749 if (to_row(tab
, var
, -1) < 0)
2753 isl_int_add(tab
->mat
->row
[var
->index
][1],
2754 tab
->mat
->row
[var
->index
][1], tab
->mat
->row
[var
->index
][0]);
2755 if (restore_row(tab
, var
) < 0)
2759 unsigned off
= 2 + tab
->M
;
2761 for (i
= 0; i
< tab
->n_row
; ++i
) {
2762 if (isl_int_is_zero(tab
->mat
->row
[i
][off
+ var
->index
]))
2764 isl_int_sub(tab
->mat
->row
[i
][1], tab
->mat
->row
[i
][1],
2765 tab
->mat
->row
[i
][off
+ var
->index
]);
2770 if (isl_tab_push_var(tab
, isl_tab_undo_relax
, var
) < 0)
2776 /* Replace the variable v at position "pos" in the tableau "tab"
2777 * by v' = v + shift.
2779 * If the variable is in a column, then we first check if we can
2780 * simply plug in v = v' - shift. The effect on a row with
2781 * coefficient f/d for variable v is that the constant term c/d
2782 * is replaced by (c - f * shift)/d. If shift is positive and
2783 * f is negative for each row that needs to remain non-negative,
2784 * then this is clearly safe. In other words, if the minimum of v
2785 * is manifestly unbounded, then we can keep v in a column position.
2786 * Otherwise, we can pivot it down to a row.
2787 * Similarly, if shift is negative, we need to check if the maximum
2788 * of is manifestly unbounded.
2790 * If the variable is in a row (from the start or after pivoting),
2791 * then the constant term c/d is replaced by (c + d * shift)/d.
2793 int isl_tab_shift_var(struct isl_tab
*tab
, int pos
, isl_int shift
)
2795 struct isl_tab_var
*var
;
2799 if (isl_int_is_zero(shift
))
2802 var
= &tab
->var
[pos
];
2804 if (isl_int_is_neg(shift
)) {
2805 if (!max_is_manifestly_unbounded(tab
, var
))
2806 if (to_row(tab
, var
, 1) < 0)
2809 if (!min_is_manifestly_unbounded(tab
, var
))
2810 if (to_row(tab
, var
, -1) < 0)
2816 isl_int_addmul(tab
->mat
->row
[var
->index
][1],
2817 shift
, tab
->mat
->row
[var
->index
][0]);
2820 unsigned off
= 2 + tab
->M
;
2822 for (i
= 0; i
< tab
->n_row
; ++i
) {
2823 if (isl_int_is_zero(tab
->mat
->row
[i
][off
+ var
->index
]))
2825 isl_int_submul(tab
->mat
->row
[i
][1],
2826 shift
, tab
->mat
->row
[i
][off
+ var
->index
]);
2834 /* Remove the sign constraint from constraint "con".
2836 * If the constraint variable was originally marked non-negative,
2837 * then we make sure we mark it non-negative again during rollback.
2839 int isl_tab_unrestrict(struct isl_tab
*tab
, int con
)
2841 struct isl_tab_var
*var
;
2846 var
= &tab
->con
[con
];
2847 if (!var
->is_nonneg
)
2851 if (isl_tab_push_var(tab
, isl_tab_undo_unrestrict
, var
) < 0)
2857 int isl_tab_select_facet(struct isl_tab
*tab
, int con
)
2862 return cut_to_hyperplane(tab
, &tab
->con
[con
]);
2865 static int may_be_equality(struct isl_tab
*tab
, int row
)
2867 return tab
->rational
? isl_int_is_zero(tab
->mat
->row
[row
][1])
2868 : isl_int_lt(tab
->mat
->row
[row
][1],
2869 tab
->mat
->row
[row
][0]);
2872 /* Check for (near) equalities among the constraints.
2873 * A constraint is an equality if it is non-negative and if
2874 * its maximal value is either
2875 * - zero (in case of rational tableaus), or
2876 * - strictly less than 1 (in case of integer tableaus)
2878 * We first mark all non-redundant and non-dead variables that
2879 * are not frozen and not obviously not an equality.
2880 * Then we iterate over all marked variables if they can attain
2881 * any values larger than zero or at least one.
2882 * If the maximal value is zero, we mark any column variables
2883 * that appear in the row as being zero and mark the row as being redundant.
2884 * Otherwise, if the maximal value is strictly less than one (and the
2885 * tableau is integer), then we restrict the value to being zero
2886 * by adding an opposite non-negative variable.
2888 int isl_tab_detect_implicit_equalities(struct isl_tab
*tab
)
2897 if (tab
->n_dead
== tab
->n_col
)
2901 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
2902 struct isl_tab_var
*var
= isl_tab_var_from_row(tab
, i
);
2903 var
->marked
= !var
->frozen
&& var
->is_nonneg
&&
2904 may_be_equality(tab
, i
);
2908 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
2909 struct isl_tab_var
*var
= var_from_col(tab
, i
);
2910 var
->marked
= !var
->frozen
&& var
->is_nonneg
;
2915 struct isl_tab_var
*var
;
2917 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
2918 var
= isl_tab_var_from_row(tab
, i
);
2922 if (i
== tab
->n_row
) {
2923 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
2924 var
= var_from_col(tab
, i
);
2928 if (i
== tab
->n_col
)
2933 sgn
= sign_of_max(tab
, var
);
2937 if (close_row(tab
, var
, 0) < 0)
2939 } else if (!tab
->rational
&& !at_least_one(tab
, var
)) {
2940 if (cut_to_hyperplane(tab
, var
) < 0)
2942 return isl_tab_detect_implicit_equalities(tab
);
2944 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
2945 var
= isl_tab_var_from_row(tab
, i
);
2948 if (may_be_equality(tab
, i
))
2958 /* Update the element of row_var or col_var that corresponds to
2959 * constraint tab->con[i] to a move from position "old" to position "i".
2961 static int update_con_after_move(struct isl_tab
*tab
, int i
, int old
)
2966 index
= tab
->con
[i
].index
;
2969 p
= tab
->con
[i
].is_row
? tab
->row_var
: tab
->col_var
;
2970 if (p
[index
] != ~old
)
2971 isl_die(tab
->mat
->ctx
, isl_error_internal
,
2972 "broken internal state", return -1);
2978 /* Rotate the "n" constraints starting at "first" to the right,
2979 * putting the last constraint in the position of the first constraint.
2981 static int rotate_constraints(struct isl_tab
*tab
, int first
, int n
)
2984 struct isl_tab_var var
;
2989 last
= first
+ n
- 1;
2990 var
= tab
->con
[last
];
2991 for (i
= last
; i
> first
; --i
) {
2992 tab
->con
[i
] = tab
->con
[i
- 1];
2993 if (update_con_after_move(tab
, i
, i
- 1) < 0)
2996 tab
->con
[first
] = var
;
2997 if (update_con_after_move(tab
, first
, last
) < 0)
3003 /* Make the equalities that are implicit in "bmap" but that have been
3004 * detected in the corresponding "tab" explicit in "bmap" and update
3005 * "tab" to reflect the new order of the constraints.
3007 * In particular, if inequality i is an implicit equality then
3008 * isl_basic_map_inequality_to_equality will move the inequality
3009 * in front of the other equality and it will move the last inequality
3010 * in the position of inequality i.
3011 * In the tableau, the inequalities of "bmap" are stored after the equalities
3012 * and so the original order
3014 * E E E E E A A A I B B B B L
3018 * I E E E E E A A A L B B B B
3020 * where I is the implicit equality, the E are equalities,
3021 * the A inequalities before I, the B inequalities after I and
3022 * L the last inequality.
3023 * We therefore need to rotate to the right two sets of constraints,
3024 * those up to and including I and those after I.
3026 * If "tab" contains any constraints that are not in "bmap" then they
3027 * appear after those in "bmap" and they should be left untouched.
3029 * Note that this function leaves "bmap" in a temporary state
3030 * as it does not call isl_basic_map_gauss. Calling this function
3031 * is the responsibility of the caller.
3033 __isl_give isl_basic_map
*isl_tab_make_equalities_explicit(struct isl_tab
*tab
,
3034 __isl_take isl_basic_map
*bmap
)
3039 return isl_basic_map_free(bmap
);
3043 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
3044 if (!isl_tab_is_equality(tab
, bmap
->n_eq
+ i
))
3046 isl_basic_map_inequality_to_equality(bmap
, i
);
3047 if (rotate_constraints(tab
, 0, tab
->n_eq
+ i
+ 1) < 0)
3048 return isl_basic_map_free(bmap
);
3049 if (rotate_constraints(tab
, tab
->n_eq
+ i
+ 1,
3050 bmap
->n_ineq
- i
) < 0)
3051 return isl_basic_map_free(bmap
);
3058 static int con_is_redundant(struct isl_tab
*tab
, struct isl_tab_var
*var
)
3062 if (tab
->rational
) {
3063 int sgn
= sign_of_min(tab
, var
);
3068 int irred
= isl_tab_min_at_most_neg_one(tab
, var
);
3075 /* Return an isl_tab_var that has been marked or NULL if no such
3076 * variable can be found.
3077 * The marked field has only been set for variables that
3078 * appear in non-redundant rows or non-dead columns.
3080 * Pick the last constraint variable that is marked and
3081 * that appears in either a non-redundant row or a non-dead columns.
3082 * Since the returned variable is tested for being a redundant constraint,
3083 * there is no need to return any tab variable that corresponds to a variable.
3085 static struct isl_tab_var
*select_marked(struct isl_tab
*tab
)
3088 struct isl_tab_var
*var
;
3090 for (i
= tab
->n_con
- 1; i
>= 0; --i
) {
3094 if (var
->is_row
&& var
->index
< tab
->n_redundant
)
3096 if (!var
->is_row
&& var
->index
< tab
->n_dead
)
3105 /* Check for (near) redundant constraints.
3106 * A constraint is redundant if it is non-negative and if
3107 * its minimal value (temporarily ignoring the non-negativity) is either
3108 * - zero (in case of rational tableaus), or
3109 * - strictly larger than -1 (in case of integer tableaus)
3111 * We first mark all non-redundant and non-dead variables that
3112 * are not frozen and not obviously negatively unbounded.
3113 * Then we iterate over all marked variables if they can attain
3114 * any values smaller than zero or at most negative one.
3115 * If not, we mark the row as being redundant (assuming it hasn't
3116 * been detected as being obviously redundant in the mean time).
3118 int isl_tab_detect_redundant(struct isl_tab
*tab
)
3127 if (tab
->n_redundant
== tab
->n_row
)
3131 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
3132 struct isl_tab_var
*var
= isl_tab_var_from_row(tab
, i
);
3133 var
->marked
= !var
->frozen
&& var
->is_nonneg
;
3137 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
3138 struct isl_tab_var
*var
= var_from_col(tab
, i
);
3139 var
->marked
= !var
->frozen
&& var
->is_nonneg
&&
3140 !min_is_manifestly_unbounded(tab
, var
);
3145 struct isl_tab_var
*var
;
3147 var
= select_marked(tab
);
3152 red
= con_is_redundant(tab
, var
);
3155 if (red
&& !var
->is_redundant
)
3156 if (isl_tab_mark_redundant(tab
, var
->index
) < 0)
3158 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
3159 var
= var_from_col(tab
, i
);
3162 if (!min_is_manifestly_unbounded(tab
, var
))
3172 int isl_tab_is_equality(struct isl_tab
*tab
, int con
)
3179 if (tab
->con
[con
].is_zero
)
3181 if (tab
->con
[con
].is_redundant
)
3183 if (!tab
->con
[con
].is_row
)
3184 return tab
->con
[con
].index
< tab
->n_dead
;
3186 row
= tab
->con
[con
].index
;
3189 return isl_int_is_zero(tab
->mat
->row
[row
][1]) &&
3190 (!tab
->M
|| isl_int_is_zero(tab
->mat
->row
[row
][2])) &&
3191 isl_seq_first_non_zero(tab
->mat
->row
[row
] + off
+ tab
->n_dead
,
3192 tab
->n_col
- tab
->n_dead
) == -1;
3195 /* Return the minimal value of the affine expression "f" with denominator
3196 * "denom" in *opt, *opt_denom, assuming the tableau is not empty and
3197 * the expression cannot attain arbitrarily small values.
3198 * If opt_denom is NULL, then *opt is rounded up to the nearest integer.
3199 * The return value reflects the nature of the result (empty, unbounded,
3200 * minimal value returned in *opt).
3202 * This function assumes that at least one more row and at least
3203 * one more element in the constraint array are available in the tableau.
3205 enum isl_lp_result
isl_tab_min(struct isl_tab
*tab
,
3206 isl_int
*f
, isl_int denom
, isl_int
*opt
, isl_int
*opt_denom
,
3210 enum isl_lp_result res
= isl_lp_ok
;
3211 struct isl_tab_var
*var
;
3212 struct isl_tab_undo
*snap
;
3215 return isl_lp_error
;
3218 return isl_lp_empty
;
3220 snap
= isl_tab_snap(tab
);
3221 r
= isl_tab_add_row(tab
, f
);
3223 return isl_lp_error
;
3227 find_pivot(tab
, var
, var
, -1, &row
, &col
);
3228 if (row
== var
->index
) {
3229 res
= isl_lp_unbounded
;
3234 if (isl_tab_pivot(tab
, row
, col
) < 0)
3235 return isl_lp_error
;
3237 isl_int_mul(tab
->mat
->row
[var
->index
][0],
3238 tab
->mat
->row
[var
->index
][0], denom
);
3239 if (ISL_FL_ISSET(flags
, ISL_TAB_SAVE_DUAL
)) {
3242 isl_vec_free(tab
->dual
);
3243 tab
->dual
= isl_vec_alloc(tab
->mat
->ctx
, 1 + tab
->n_con
);
3245 return isl_lp_error
;
3246 isl_int_set(tab
->dual
->el
[0], tab
->mat
->row
[var
->index
][0]);
3247 for (i
= 0; i
< tab
->n_con
; ++i
) {
3249 if (tab
->con
[i
].is_row
) {
3250 isl_int_set_si(tab
->dual
->el
[1 + i
], 0);
3253 pos
= 2 + tab
->M
+ tab
->con
[i
].index
;
3254 if (tab
->con
[i
].negated
)
3255 isl_int_neg(tab
->dual
->el
[1 + i
],
3256 tab
->mat
->row
[var
->index
][pos
]);
3258 isl_int_set(tab
->dual
->el
[1 + i
],
3259 tab
->mat
->row
[var
->index
][pos
]);
3262 if (opt
&& res
== isl_lp_ok
) {
3264 isl_int_set(*opt
, tab
->mat
->row
[var
->index
][1]);
3265 isl_int_set(*opt_denom
, tab
->mat
->row
[var
->index
][0]);
3267 isl_int_cdiv_q(*opt
, tab
->mat
->row
[var
->index
][1],
3268 tab
->mat
->row
[var
->index
][0]);
3270 if (isl_tab_rollback(tab
, snap
) < 0)
3271 return isl_lp_error
;
3275 /* Is the constraint at position "con" marked as being redundant?
3276 * If it is marked as representing an equality, then it is not
3277 * considered to be redundant.
3278 * Note that isl_tab_mark_redundant marks both the isl_tab_var as
3279 * redundant and moves the corresponding row into the first
3280 * tab->n_redundant positions (or removes the row, assigning it index -1),
3281 * so the final test is actually redundant itself.
3283 int isl_tab_is_redundant(struct isl_tab
*tab
, int con
)
3287 if (con
< 0 || con
>= tab
->n_con
)
3288 isl_die(isl_tab_get_ctx(tab
), isl_error_invalid
,
3289 "position out of bounds", return -1);
3290 if (tab
->con
[con
].is_zero
)
3292 if (tab
->con
[con
].is_redundant
)
3294 return tab
->con
[con
].is_row
&& tab
->con
[con
].index
< tab
->n_redundant
;
3297 /* Take a snapshot of the tableau that can be restored by a call to
3300 struct isl_tab_undo
*isl_tab_snap(struct isl_tab
*tab
)
3308 /* Does "tab" need to keep track of undo information?
3309 * That is, was a snapshot taken that may need to be restored?
3311 isl_bool
isl_tab_need_undo(struct isl_tab
*tab
)
3314 return isl_bool_error
;
3316 return tab
->need_undo
;
3319 /* Remove all tracking of undo information from "tab", invalidating
3320 * any snapshots that may have been taken of the tableau.
3321 * Since all snapshots have been invalidated, there is also
3322 * no need to start keeping track of undo information again.
3324 void isl_tab_clear_undo(struct isl_tab
*tab
)
3333 /* Undo the operation performed by isl_tab_relax.
3335 static int unrelax(struct isl_tab
*tab
, struct isl_tab_var
*var
) WARN_UNUSED
;
3336 static int unrelax(struct isl_tab
*tab
, struct isl_tab_var
*var
)
3338 unsigned off
= 2 + tab
->M
;
3340 if (!var
->is_row
&& !max_is_manifestly_unbounded(tab
, var
))
3341 if (to_row(tab
, var
, 1) < 0)
3345 isl_int_sub(tab
->mat
->row
[var
->index
][1],
3346 tab
->mat
->row
[var
->index
][1], tab
->mat
->row
[var
->index
][0]);
3347 if (var
->is_nonneg
) {
3348 int sgn
= restore_row(tab
, var
);
3349 isl_assert(tab
->mat
->ctx
, sgn
>= 0, return -1);
3354 for (i
= 0; i
< tab
->n_row
; ++i
) {
3355 if (isl_int_is_zero(tab
->mat
->row
[i
][off
+ var
->index
]))
3357 isl_int_add(tab
->mat
->row
[i
][1], tab
->mat
->row
[i
][1],
3358 tab
->mat
->row
[i
][off
+ var
->index
]);
3366 /* Undo the operation performed by isl_tab_unrestrict.
3368 * In particular, mark the variable as being non-negative and make
3369 * sure the sample value respects this constraint.
3371 static int ununrestrict(struct isl_tab
*tab
, struct isl_tab_var
*var
)
3375 if (var
->is_row
&& restore_row(tab
, var
) < -1)
3381 static int perform_undo_var(struct isl_tab
*tab
, struct isl_tab_undo
*undo
) WARN_UNUSED
;
3382 static int perform_undo_var(struct isl_tab
*tab
, struct isl_tab_undo
*undo
)
3384 struct isl_tab_var
*var
= var_from_index(tab
, undo
->u
.var_index
);
3385 switch (undo
->type
) {
3386 case isl_tab_undo_nonneg
:
3389 case isl_tab_undo_redundant
:
3390 var
->is_redundant
= 0;
3392 restore_row(tab
, isl_tab_var_from_row(tab
, tab
->n_redundant
));
3394 case isl_tab_undo_freeze
:
3397 case isl_tab_undo_zero
:
3402 case isl_tab_undo_allocate
:
3403 if (undo
->u
.var_index
>= 0) {
3404 isl_assert(tab
->mat
->ctx
, !var
->is_row
, return -1);
3405 return drop_col(tab
, var
->index
);
3408 if (!max_is_manifestly_unbounded(tab
, var
)) {
3409 if (to_row(tab
, var
, 1) < 0)
3411 } else if (!min_is_manifestly_unbounded(tab
, var
)) {
3412 if (to_row(tab
, var
, -1) < 0)
3415 if (to_row(tab
, var
, 0) < 0)
3418 return drop_row(tab
, var
->index
);
3419 case isl_tab_undo_relax
:
3420 return unrelax(tab
, var
);
3421 case isl_tab_undo_unrestrict
:
3422 return ununrestrict(tab
, var
);
3424 isl_die(tab
->mat
->ctx
, isl_error_internal
,
3425 "perform_undo_var called on invalid undo record",
3432 /* Undo the addition of an integer division to the basic map representation
3433 * of "tab" in position "pos".
3435 static isl_stat
drop_bmap_div(struct isl_tab
*tab
, int pos
)
3439 off
= tab
->n_var
- isl_basic_map_dim(tab
->bmap
, isl_dim_div
);
3440 if (isl_basic_map_drop_div(tab
->bmap
, pos
- off
) < 0)
3441 return isl_stat_error
;
3443 tab
->samples
= isl_mat_drop_cols(tab
->samples
, 1 + pos
, 1);
3445 return isl_stat_error
;
3451 /* Restore the tableau to the state where the basic variables
3452 * are those in "col_var".
3453 * We first construct a list of variables that are currently in
3454 * the basis, but shouldn't. Then we iterate over all variables
3455 * that should be in the basis and for each one that is currently
3456 * not in the basis, we exchange it with one of the elements of the
3457 * list constructed before.
3458 * We can always find an appropriate variable to pivot with because
3459 * the current basis is mapped to the old basis by a non-singular
3460 * matrix and so we can never end up with a zero row.
3462 static int restore_basis(struct isl_tab
*tab
, int *col_var
)
3466 int *extra
= NULL
; /* current columns that contain bad stuff */
3467 unsigned off
= 2 + tab
->M
;
3469 extra
= isl_alloc_array(tab
->mat
->ctx
, int, tab
->n_col
);
3470 if (tab
->n_col
&& !extra
)
3472 for (i
= 0; i
< tab
->n_col
; ++i
) {
3473 for (j
= 0; j
< tab
->n_col
; ++j
)
3474 if (tab
->col_var
[i
] == col_var
[j
])
3478 extra
[n_extra
++] = i
;
3480 for (i
= 0; i
< tab
->n_col
&& n_extra
> 0; ++i
) {
3481 struct isl_tab_var
*var
;
3484 for (j
= 0; j
< tab
->n_col
; ++j
)
3485 if (col_var
[i
] == tab
->col_var
[j
])
3489 var
= var_from_index(tab
, col_var
[i
]);
3491 for (j
= 0; j
< n_extra
; ++j
)
3492 if (!isl_int_is_zero(tab
->mat
->row
[row
][off
+extra
[j
]]))
3494 isl_assert(tab
->mat
->ctx
, j
< n_extra
, goto error
);
3495 if (isl_tab_pivot(tab
, row
, extra
[j
]) < 0)
3497 extra
[j
] = extra
[--n_extra
];
3507 /* Remove all samples with index n or greater, i.e., those samples
3508 * that were added since we saved this number of samples in
3509 * isl_tab_save_samples.
3511 static void drop_samples_since(struct isl_tab
*tab
, int n
)
3515 for (i
= tab
->n_sample
- 1; i
>= 0 && tab
->n_sample
> n
; --i
) {
3516 if (tab
->sample_index
[i
] < n
)
3519 if (i
!= tab
->n_sample
- 1) {
3520 int t
= tab
->sample_index
[tab
->n_sample
-1];
3521 tab
->sample_index
[tab
->n_sample
-1] = tab
->sample_index
[i
];
3522 tab
->sample_index
[i
] = t
;
3523 isl_mat_swap_rows(tab
->samples
, tab
->n_sample
-1, i
);
3529 static int perform_undo(struct isl_tab
*tab
, struct isl_tab_undo
*undo
) WARN_UNUSED
;
3530 static int perform_undo(struct isl_tab
*tab
, struct isl_tab_undo
*undo
)
3532 switch (undo
->type
) {
3533 case isl_tab_undo_rational
:
3536 case isl_tab_undo_empty
:
3539 case isl_tab_undo_nonneg
:
3540 case isl_tab_undo_redundant
:
3541 case isl_tab_undo_freeze
:
3542 case isl_tab_undo_zero
:
3543 case isl_tab_undo_allocate
:
3544 case isl_tab_undo_relax
:
3545 case isl_tab_undo_unrestrict
:
3546 return perform_undo_var(tab
, undo
);
3547 case isl_tab_undo_bmap_eq
:
3548 return isl_basic_map_free_equality(tab
->bmap
, 1);
3549 case isl_tab_undo_bmap_ineq
:
3550 return isl_basic_map_free_inequality(tab
->bmap
, 1);
3551 case isl_tab_undo_bmap_div
:
3552 return drop_bmap_div(tab
, undo
->u
.var_index
);
3553 case isl_tab_undo_saved_basis
:
3554 if (restore_basis(tab
, undo
->u
.col_var
) < 0)
3557 case isl_tab_undo_drop_sample
:
3560 case isl_tab_undo_saved_samples
:
3561 drop_samples_since(tab
, undo
->u
.n
);
3563 case isl_tab_undo_callback
:
3564 return undo
->u
.callback
->run(undo
->u
.callback
);
3566 isl_assert(tab
->mat
->ctx
, 0, return -1);
3571 /* Return the tableau to the state it was in when the snapshot "snap"
3574 int isl_tab_rollback(struct isl_tab
*tab
, struct isl_tab_undo
*snap
)
3576 struct isl_tab_undo
*undo
, *next
;
3582 for (undo
= tab
->top
; undo
&& undo
!= &tab
->bottom
; undo
= next
) {
3586 if (perform_undo(tab
, undo
) < 0) {
3592 free_undo_record(undo
);
3601 /* The given row "row" represents an inequality violated by all
3602 * points in the tableau. Check for some special cases of such
3603 * separating constraints.
3604 * In particular, if the row has been reduced to the constant -1,
3605 * then we know the inequality is adjacent (but opposite) to
3606 * an equality in the tableau.
3607 * If the row has been reduced to r = c*(-1 -r'), with r' an inequality
3608 * of the tableau and c a positive constant, then the inequality
3609 * is adjacent (but opposite) to the inequality r'.
3611 static enum isl_ineq_type
separation_type(struct isl_tab
*tab
, unsigned row
)
3614 unsigned off
= 2 + tab
->M
;
3617 return isl_ineq_separate
;
3619 if (!isl_int_is_one(tab
->mat
->row
[row
][0]))
3620 return isl_ineq_separate
;
3622 pos
= isl_seq_first_non_zero(tab
->mat
->row
[row
] + off
+ tab
->n_dead
,
3623 tab
->n_col
- tab
->n_dead
);
3625 if (isl_int_is_negone(tab
->mat
->row
[row
][1]))
3626 return isl_ineq_adj_eq
;
3628 return isl_ineq_separate
;
3631 if (!isl_int_eq(tab
->mat
->row
[row
][1],
3632 tab
->mat
->row
[row
][off
+ tab
->n_dead
+ pos
]))
3633 return isl_ineq_separate
;
3635 pos
= isl_seq_first_non_zero(
3636 tab
->mat
->row
[row
] + off
+ tab
->n_dead
+ pos
+ 1,
3637 tab
->n_col
- tab
->n_dead
- pos
- 1);
3639 return pos
== -1 ? isl_ineq_adj_ineq
: isl_ineq_separate
;
3642 /* Check the effect of inequality "ineq" on the tableau "tab".
3644 * isl_ineq_redundant: satisfied by all points in the tableau
3645 * isl_ineq_separate: satisfied by no point in the tableau
3646 * isl_ineq_cut: satisfied by some by not all points
3647 * isl_ineq_adj_eq: adjacent to an equality
3648 * isl_ineq_adj_ineq: adjacent to an inequality.
3650 enum isl_ineq_type
isl_tab_ineq_type(struct isl_tab
*tab
, isl_int
*ineq
)
3652 enum isl_ineq_type type
= isl_ineq_error
;
3653 struct isl_tab_undo
*snap
= NULL
;
3658 return isl_ineq_error
;
3660 if (isl_tab_extend_cons(tab
, 1) < 0)
3661 return isl_ineq_error
;
3663 snap
= isl_tab_snap(tab
);
3665 con
= isl_tab_add_row(tab
, ineq
);
3669 row
= tab
->con
[con
].index
;
3670 if (isl_tab_row_is_redundant(tab
, row
))
3671 type
= isl_ineq_redundant
;
3672 else if (isl_int_is_neg(tab
->mat
->row
[row
][1]) &&
3674 isl_int_abs_ge(tab
->mat
->row
[row
][1],
3675 tab
->mat
->row
[row
][0]))) {
3676 int nonneg
= at_least_zero(tab
, &tab
->con
[con
]);
3680 type
= isl_ineq_cut
;
3682 type
= separation_type(tab
, row
);
3684 int red
= con_is_redundant(tab
, &tab
->con
[con
]);
3688 type
= isl_ineq_cut
;
3690 type
= isl_ineq_redundant
;
3693 if (isl_tab_rollback(tab
, snap
))
3694 return isl_ineq_error
;
3697 return isl_ineq_error
;
3700 int isl_tab_track_bmap(struct isl_tab
*tab
, __isl_take isl_basic_map
*bmap
)
3702 bmap
= isl_basic_map_cow(bmap
);
3707 bmap
= isl_basic_map_set_to_empty(bmap
);
3714 isl_assert(tab
->mat
->ctx
, tab
->n_eq
== bmap
->n_eq
, goto error
);
3715 isl_assert(tab
->mat
->ctx
,
3716 tab
->n_con
== bmap
->n_eq
+ bmap
->n_ineq
, goto error
);
3722 isl_basic_map_free(bmap
);
3726 int isl_tab_track_bset(struct isl_tab
*tab
, __isl_take isl_basic_set
*bset
)
3728 return isl_tab_track_bmap(tab
, bset_to_bmap(bset
));
3731 __isl_keep isl_basic_set
*isl_tab_peek_bset(struct isl_tab
*tab
)
3736 return bset_from_bmap(tab
->bmap
);
3739 static void isl_tab_print_internal(__isl_keep
struct isl_tab
*tab
,
3740 FILE *out
, int indent
)
3746 fprintf(out
, "%*snull tab\n", indent
, "");
3749 fprintf(out
, "%*sn_redundant: %d, n_dead: %d", indent
, "",
3750 tab
->n_redundant
, tab
->n_dead
);
3752 fprintf(out
, ", rational");
3754 fprintf(out
, ", empty");
3756 fprintf(out
, "%*s[", indent
, "");
3757 for (i
= 0; i
< tab
->n_var
; ++i
) {
3759 fprintf(out
, (i
== tab
->n_param
||
3760 i
== tab
->n_var
- tab
->n_div
) ? "; "
3762 fprintf(out
, "%c%d%s", tab
->var
[i
].is_row
? 'r' : 'c',
3764 tab
->var
[i
].is_zero
? " [=0]" :
3765 tab
->var
[i
].is_redundant
? " [R]" : "");
3767 fprintf(out
, "]\n");
3768 fprintf(out
, "%*s[", indent
, "");
3769 for (i
= 0; i
< tab
->n_con
; ++i
) {
3772 fprintf(out
, "%c%d%s", tab
->con
[i
].is_row
? 'r' : 'c',
3774 tab
->con
[i
].is_zero
? " [=0]" :
3775 tab
->con
[i
].is_redundant
? " [R]" : "");
3777 fprintf(out
, "]\n");
3778 fprintf(out
, "%*s[", indent
, "");
3779 for (i
= 0; i
< tab
->n_row
; ++i
) {
3780 const char *sign
= "";
3783 if (tab
->row_sign
) {
3784 if (tab
->row_sign
[i
] == isl_tab_row_unknown
)
3786 else if (tab
->row_sign
[i
] == isl_tab_row_neg
)
3788 else if (tab
->row_sign
[i
] == isl_tab_row_pos
)
3793 fprintf(out
, "r%d: %d%s%s", i
, tab
->row_var
[i
],
3794 isl_tab_var_from_row(tab
, i
)->is_nonneg
? " [>=0]" : "", sign
);
3796 fprintf(out
, "]\n");
3797 fprintf(out
, "%*s[", indent
, "");
3798 for (i
= 0; i
< tab
->n_col
; ++i
) {
3801 fprintf(out
, "c%d: %d%s", i
, tab
->col_var
[i
],
3802 var_from_col(tab
, i
)->is_nonneg
? " [>=0]" : "");
3804 fprintf(out
, "]\n");
3805 r
= tab
->mat
->n_row
;
3806 tab
->mat
->n_row
= tab
->n_row
;
3807 c
= tab
->mat
->n_col
;
3808 tab
->mat
->n_col
= 2 + tab
->M
+ tab
->n_col
;
3809 isl_mat_print_internal(tab
->mat
, out
, indent
);
3810 tab
->mat
->n_row
= r
;
3811 tab
->mat
->n_col
= c
;
3813 isl_basic_map_print_internal(tab
->bmap
, out
, indent
);
3816 void isl_tab_dump(__isl_keep
struct isl_tab
*tab
)
3818 isl_tab_print_internal(tab
, stderr
, 0);