2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2013 Ecole Normale Superieure
5 * Use of this software is governed by the MIT license
7 * Written by Sven Verdoolaege, K.U.Leuven, Departement
8 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9 * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
12 #include <isl_ctx_private.h>
13 #include <isl_mat_private.h>
14 #include <isl_vec_private.h>
15 #include "isl_map_private.h"
18 #include <isl_config.h>
21 * The implementation of tableaus in this file was inspired by Section 8
22 * of David Detlefs, Greg Nelson and James B. Saxe, "Simplify: a theorem
23 * prover for program checking".
26 struct isl_tab
*isl_tab_alloc(struct isl_ctx
*ctx
,
27 unsigned n_row
, unsigned n_var
, unsigned M
)
33 tab
= isl_calloc_type(ctx
, struct isl_tab
);
36 tab
->mat
= isl_mat_alloc(ctx
, n_row
, off
+ n_var
);
39 tab
->var
= isl_alloc_array(ctx
, struct isl_tab_var
, n_var
);
42 tab
->con
= isl_alloc_array(ctx
, struct isl_tab_var
, n_row
);
45 tab
->col_var
= isl_alloc_array(ctx
, int, n_var
);
48 tab
->row_var
= isl_alloc_array(ctx
, int, n_row
);
51 for (i
= 0; i
< n_var
; ++i
) {
52 tab
->var
[i
].index
= i
;
53 tab
->var
[i
].is_row
= 0;
54 tab
->var
[i
].is_nonneg
= 0;
55 tab
->var
[i
].is_zero
= 0;
56 tab
->var
[i
].is_redundant
= 0;
57 tab
->var
[i
].frozen
= 0;
58 tab
->var
[i
].negated
= 0;
72 tab
->strict_redundant
= 0;
79 tab
->bottom
.type
= isl_tab_undo_bottom
;
80 tab
->bottom
.next
= NULL
;
81 tab
->top
= &tab
->bottom
;
93 isl_ctx
*isl_tab_get_ctx(struct isl_tab
*tab
)
95 return tab
? isl_mat_get_ctx(tab
->mat
) : NULL
;
98 int isl_tab_extend_cons(struct isl_tab
*tab
, unsigned n_new
)
107 if (tab
->max_con
< tab
->n_con
+ n_new
) {
108 struct isl_tab_var
*con
;
110 con
= isl_realloc_array(tab
->mat
->ctx
, tab
->con
,
111 struct isl_tab_var
, tab
->max_con
+ n_new
);
115 tab
->max_con
+= n_new
;
117 if (tab
->mat
->n_row
< tab
->n_row
+ n_new
) {
120 tab
->mat
= isl_mat_extend(tab
->mat
,
121 tab
->n_row
+ n_new
, off
+ tab
->n_col
);
124 row_var
= isl_realloc_array(tab
->mat
->ctx
, tab
->row_var
,
125 int, tab
->mat
->n_row
);
128 tab
->row_var
= row_var
;
130 enum isl_tab_row_sign
*s
;
131 s
= isl_realloc_array(tab
->mat
->ctx
, tab
->row_sign
,
132 enum isl_tab_row_sign
, tab
->mat
->n_row
);
141 /* Make room for at least n_new extra variables.
142 * Return -1 if anything went wrong.
144 int isl_tab_extend_vars(struct isl_tab
*tab
, unsigned n_new
)
146 struct isl_tab_var
*var
;
147 unsigned off
= 2 + tab
->M
;
149 if (tab
->max_var
< tab
->n_var
+ n_new
) {
150 var
= isl_realloc_array(tab
->mat
->ctx
, tab
->var
,
151 struct isl_tab_var
, tab
->n_var
+ n_new
);
155 tab
->max_var
+= n_new
;
158 if (tab
->mat
->n_col
< off
+ tab
->n_col
+ n_new
) {
161 tab
->mat
= isl_mat_extend(tab
->mat
,
162 tab
->mat
->n_row
, off
+ tab
->n_col
+ n_new
);
165 p
= isl_realloc_array(tab
->mat
->ctx
, tab
->col_var
,
166 int, tab
->n_col
+ n_new
);
175 struct isl_tab
*isl_tab_extend(struct isl_tab
*tab
, unsigned n_new
)
177 if (isl_tab_extend_cons(tab
, n_new
) >= 0)
184 static void free_undo_record(struct isl_tab_undo
*undo
)
186 switch (undo
->type
) {
187 case isl_tab_undo_saved_basis
:
188 free(undo
->u
.col_var
);
195 static void free_undo(struct isl_tab
*tab
)
197 struct isl_tab_undo
*undo
, *next
;
199 for (undo
= tab
->top
; undo
&& undo
!= &tab
->bottom
; undo
= next
) {
201 free_undo_record(undo
);
206 void isl_tab_free(struct isl_tab
*tab
)
211 isl_mat_free(tab
->mat
);
212 isl_vec_free(tab
->dual
);
213 isl_basic_map_free(tab
->bmap
);
219 isl_mat_free(tab
->samples
);
220 free(tab
->sample_index
);
221 isl_mat_free(tab
->basis
);
225 struct isl_tab
*isl_tab_dup(struct isl_tab
*tab
)
235 dup
= isl_calloc_type(tab
->mat
->ctx
, struct isl_tab
);
238 dup
->mat
= isl_mat_dup(tab
->mat
);
241 dup
->var
= isl_alloc_array(tab
->mat
->ctx
, struct isl_tab_var
, tab
->max_var
);
244 for (i
= 0; i
< tab
->n_var
; ++i
)
245 dup
->var
[i
] = tab
->var
[i
];
246 dup
->con
= isl_alloc_array(tab
->mat
->ctx
, struct isl_tab_var
, tab
->max_con
);
249 for (i
= 0; i
< tab
->n_con
; ++i
)
250 dup
->con
[i
] = tab
->con
[i
];
251 dup
->col_var
= isl_alloc_array(tab
->mat
->ctx
, int, tab
->mat
->n_col
- off
);
254 for (i
= 0; i
< tab
->n_col
; ++i
)
255 dup
->col_var
[i
] = tab
->col_var
[i
];
256 dup
->row_var
= isl_alloc_array(tab
->mat
->ctx
, int, tab
->mat
->n_row
);
259 for (i
= 0; i
< tab
->n_row
; ++i
)
260 dup
->row_var
[i
] = tab
->row_var
[i
];
262 dup
->row_sign
= isl_alloc_array(tab
->mat
->ctx
, enum isl_tab_row_sign
,
266 for (i
= 0; i
< tab
->n_row
; ++i
)
267 dup
->row_sign
[i
] = tab
->row_sign
[i
];
270 dup
->samples
= isl_mat_dup(tab
->samples
);
273 dup
->sample_index
= isl_alloc_array(tab
->mat
->ctx
, int,
274 tab
->samples
->n_row
);
275 if (!dup
->sample_index
)
277 dup
->n_sample
= tab
->n_sample
;
278 dup
->n_outside
= tab
->n_outside
;
280 dup
->n_row
= tab
->n_row
;
281 dup
->n_con
= tab
->n_con
;
282 dup
->n_eq
= tab
->n_eq
;
283 dup
->max_con
= tab
->max_con
;
284 dup
->n_col
= tab
->n_col
;
285 dup
->n_var
= tab
->n_var
;
286 dup
->max_var
= tab
->max_var
;
287 dup
->n_param
= tab
->n_param
;
288 dup
->n_div
= tab
->n_div
;
289 dup
->n_dead
= tab
->n_dead
;
290 dup
->n_redundant
= tab
->n_redundant
;
291 dup
->rational
= tab
->rational
;
292 dup
->empty
= tab
->empty
;
293 dup
->strict_redundant
= 0;
297 tab
->cone
= tab
->cone
;
298 dup
->bottom
.type
= isl_tab_undo_bottom
;
299 dup
->bottom
.next
= NULL
;
300 dup
->top
= &dup
->bottom
;
302 dup
->n_zero
= tab
->n_zero
;
303 dup
->n_unbounded
= tab
->n_unbounded
;
304 dup
->basis
= isl_mat_dup(tab
->basis
);
312 /* Construct the coefficient matrix of the product tableau
314 * mat{1,2} is the coefficient matrix of tableau {1,2}
315 * row{1,2} is the number of rows in tableau {1,2}
316 * col{1,2} is the number of columns in tableau {1,2}
317 * off is the offset to the coefficient column (skipping the
318 * denominator, the constant term and the big parameter if any)
319 * r{1,2} is the number of redundant rows in tableau {1,2}
320 * d{1,2} is the number of dead columns in tableau {1,2}
322 * The order of the rows and columns in the result is as explained
323 * in isl_tab_product.
325 static struct isl_mat
*tab_mat_product(struct isl_mat
*mat1
,
326 struct isl_mat
*mat2
, unsigned row1
, unsigned row2
,
327 unsigned col1
, unsigned col2
,
328 unsigned off
, unsigned r1
, unsigned r2
, unsigned d1
, unsigned d2
)
331 struct isl_mat
*prod
;
334 prod
= isl_mat_alloc(mat1
->ctx
, mat1
->n_row
+ mat2
->n_row
,
340 for (i
= 0; i
< r1
; ++i
) {
341 isl_seq_cpy(prod
->row
[n
+ i
], mat1
->row
[i
], off
+ d1
);
342 isl_seq_clr(prod
->row
[n
+ i
] + off
+ d1
, d2
);
343 isl_seq_cpy(prod
->row
[n
+ i
] + off
+ d1
+ d2
,
344 mat1
->row
[i
] + off
+ d1
, col1
- d1
);
345 isl_seq_clr(prod
->row
[n
+ i
] + off
+ col1
+ d1
, col2
- d2
);
349 for (i
= 0; i
< r2
; ++i
) {
350 isl_seq_cpy(prod
->row
[n
+ i
], mat2
->row
[i
], off
);
351 isl_seq_clr(prod
->row
[n
+ i
] + off
, d1
);
352 isl_seq_cpy(prod
->row
[n
+ i
] + off
+ d1
,
353 mat2
->row
[i
] + off
, d2
);
354 isl_seq_clr(prod
->row
[n
+ i
] + off
+ d1
+ d2
, col1
- d1
);
355 isl_seq_cpy(prod
->row
[n
+ i
] + off
+ col1
+ d1
,
356 mat2
->row
[i
] + off
+ d2
, col2
- d2
);
360 for (i
= 0; i
< row1
- r1
; ++i
) {
361 isl_seq_cpy(prod
->row
[n
+ i
], mat1
->row
[r1
+ i
], off
+ d1
);
362 isl_seq_clr(prod
->row
[n
+ i
] + off
+ d1
, d2
);
363 isl_seq_cpy(prod
->row
[n
+ i
] + off
+ d1
+ d2
,
364 mat1
->row
[r1
+ i
] + off
+ d1
, col1
- d1
);
365 isl_seq_clr(prod
->row
[n
+ i
] + off
+ col1
+ d1
, col2
- d2
);
369 for (i
= 0; i
< row2
- r2
; ++i
) {
370 isl_seq_cpy(prod
->row
[n
+ i
], mat2
->row
[r2
+ i
], off
);
371 isl_seq_clr(prod
->row
[n
+ i
] + off
, d1
);
372 isl_seq_cpy(prod
->row
[n
+ i
] + off
+ d1
,
373 mat2
->row
[r2
+ i
] + off
, d2
);
374 isl_seq_clr(prod
->row
[n
+ i
] + off
+ d1
+ d2
, col1
- d1
);
375 isl_seq_cpy(prod
->row
[n
+ i
] + off
+ col1
+ d1
,
376 mat2
->row
[r2
+ i
] + off
+ d2
, col2
- d2
);
382 /* Update the row or column index of a variable that corresponds
383 * to a variable in the first input tableau.
385 static void update_index1(struct isl_tab_var
*var
,
386 unsigned r1
, unsigned r2
, unsigned d1
, unsigned d2
)
388 if (var
->index
== -1)
390 if (var
->is_row
&& var
->index
>= r1
)
392 if (!var
->is_row
&& var
->index
>= d1
)
396 /* Update the row or column index of a variable that corresponds
397 * to a variable in the second input tableau.
399 static void update_index2(struct isl_tab_var
*var
,
400 unsigned row1
, unsigned col1
,
401 unsigned r1
, unsigned r2
, unsigned d1
, unsigned d2
)
403 if (var
->index
== -1)
418 /* Create a tableau that represents the Cartesian product of the sets
419 * represented by tableaus tab1 and tab2.
420 * The order of the rows in the product is
421 * - redundant rows of tab1
422 * - redundant rows of tab2
423 * - non-redundant rows of tab1
424 * - non-redundant rows of tab2
425 * The order of the columns is
428 * - coefficient of big parameter, if any
429 * - dead columns of tab1
430 * - dead columns of tab2
431 * - live columns of tab1
432 * - live columns of tab2
433 * The order of the variables and the constraints is a concatenation
434 * of order in the two input tableaus.
436 struct isl_tab
*isl_tab_product(struct isl_tab
*tab1
, struct isl_tab
*tab2
)
439 struct isl_tab
*prod
;
441 unsigned r1
, r2
, d1
, d2
;
446 isl_assert(tab1
->mat
->ctx
, tab1
->M
== tab2
->M
, return NULL
);
447 isl_assert(tab1
->mat
->ctx
, tab1
->rational
== tab2
->rational
, return NULL
);
448 isl_assert(tab1
->mat
->ctx
, tab1
->cone
== tab2
->cone
, return NULL
);
449 isl_assert(tab1
->mat
->ctx
, !tab1
->row_sign
, return NULL
);
450 isl_assert(tab1
->mat
->ctx
, !tab2
->row_sign
, return NULL
);
451 isl_assert(tab1
->mat
->ctx
, tab1
->n_param
== 0, return NULL
);
452 isl_assert(tab1
->mat
->ctx
, tab2
->n_param
== 0, return NULL
);
453 isl_assert(tab1
->mat
->ctx
, tab1
->n_div
== 0, return NULL
);
454 isl_assert(tab1
->mat
->ctx
, tab2
->n_div
== 0, return NULL
);
457 r1
= tab1
->n_redundant
;
458 r2
= tab2
->n_redundant
;
461 prod
= isl_calloc_type(tab1
->mat
->ctx
, struct isl_tab
);
464 prod
->mat
= tab_mat_product(tab1
->mat
, tab2
->mat
,
465 tab1
->n_row
, tab2
->n_row
,
466 tab1
->n_col
, tab2
->n_col
, off
, r1
, r2
, d1
, d2
);
469 prod
->var
= isl_alloc_array(tab1
->mat
->ctx
, struct isl_tab_var
,
470 tab1
->max_var
+ tab2
->max_var
);
473 for (i
= 0; i
< tab1
->n_var
; ++i
) {
474 prod
->var
[i
] = tab1
->var
[i
];
475 update_index1(&prod
->var
[i
], r1
, r2
, d1
, d2
);
477 for (i
= 0; i
< tab2
->n_var
; ++i
) {
478 prod
->var
[tab1
->n_var
+ i
] = tab2
->var
[i
];
479 update_index2(&prod
->var
[tab1
->n_var
+ i
],
480 tab1
->n_row
, tab1
->n_col
,
483 prod
->con
= isl_alloc_array(tab1
->mat
->ctx
, struct isl_tab_var
,
484 tab1
->max_con
+ tab2
->max_con
);
487 for (i
= 0; i
< tab1
->n_con
; ++i
) {
488 prod
->con
[i
] = tab1
->con
[i
];
489 update_index1(&prod
->con
[i
], r1
, r2
, d1
, d2
);
491 for (i
= 0; i
< tab2
->n_con
; ++i
) {
492 prod
->con
[tab1
->n_con
+ i
] = tab2
->con
[i
];
493 update_index2(&prod
->con
[tab1
->n_con
+ i
],
494 tab1
->n_row
, tab1
->n_col
,
497 prod
->col_var
= isl_alloc_array(tab1
->mat
->ctx
, int,
498 tab1
->n_col
+ tab2
->n_col
);
501 for (i
= 0; i
< tab1
->n_col
; ++i
) {
502 int pos
= i
< d1
? i
: i
+ d2
;
503 prod
->col_var
[pos
] = tab1
->col_var
[i
];
505 for (i
= 0; i
< tab2
->n_col
; ++i
) {
506 int pos
= i
< d2
? d1
+ i
: tab1
->n_col
+ i
;
507 int t
= tab2
->col_var
[i
];
512 prod
->col_var
[pos
] = t
;
514 prod
->row_var
= isl_alloc_array(tab1
->mat
->ctx
, int,
515 tab1
->mat
->n_row
+ tab2
->mat
->n_row
);
518 for (i
= 0; i
< tab1
->n_row
; ++i
) {
519 int pos
= i
< r1
? i
: i
+ r2
;
520 prod
->row_var
[pos
] = tab1
->row_var
[i
];
522 for (i
= 0; i
< tab2
->n_row
; ++i
) {
523 int pos
= i
< r2
? r1
+ i
: tab1
->n_row
+ i
;
524 int t
= tab2
->row_var
[i
];
529 prod
->row_var
[pos
] = t
;
531 prod
->samples
= NULL
;
532 prod
->sample_index
= NULL
;
533 prod
->n_row
= tab1
->n_row
+ tab2
->n_row
;
534 prod
->n_con
= tab1
->n_con
+ tab2
->n_con
;
536 prod
->max_con
= tab1
->max_con
+ tab2
->max_con
;
537 prod
->n_col
= tab1
->n_col
+ tab2
->n_col
;
538 prod
->n_var
= tab1
->n_var
+ tab2
->n_var
;
539 prod
->max_var
= tab1
->max_var
+ tab2
->max_var
;
542 prod
->n_dead
= tab1
->n_dead
+ tab2
->n_dead
;
543 prod
->n_redundant
= tab1
->n_redundant
+ tab2
->n_redundant
;
544 prod
->rational
= tab1
->rational
;
545 prod
->empty
= tab1
->empty
|| tab2
->empty
;
546 prod
->strict_redundant
= tab1
->strict_redundant
|| tab2
->strict_redundant
;
550 prod
->cone
= tab1
->cone
;
551 prod
->bottom
.type
= isl_tab_undo_bottom
;
552 prod
->bottom
.next
= NULL
;
553 prod
->top
= &prod
->bottom
;
556 prod
->n_unbounded
= 0;
565 static struct isl_tab_var
*var_from_index(struct isl_tab
*tab
, int i
)
570 return &tab
->con
[~i
];
573 struct isl_tab_var
*isl_tab_var_from_row(struct isl_tab
*tab
, int i
)
575 return var_from_index(tab
, tab
->row_var
[i
]);
578 static struct isl_tab_var
*var_from_col(struct isl_tab
*tab
, int i
)
580 return var_from_index(tab
, tab
->col_var
[i
]);
583 /* Check if there are any upper bounds on column variable "var",
584 * i.e., non-negative rows where var appears with a negative coefficient.
585 * Return 1 if there are no such bounds.
587 static int max_is_manifestly_unbounded(struct isl_tab
*tab
,
588 struct isl_tab_var
*var
)
591 unsigned off
= 2 + tab
->M
;
595 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
596 if (!isl_int_is_neg(tab
->mat
->row
[i
][off
+ var
->index
]))
598 if (isl_tab_var_from_row(tab
, i
)->is_nonneg
)
604 /* Check if there are any lower bounds on column variable "var",
605 * i.e., non-negative rows where var appears with a positive coefficient.
606 * Return 1 if there are no such bounds.
608 static int min_is_manifestly_unbounded(struct isl_tab
*tab
,
609 struct isl_tab_var
*var
)
612 unsigned off
= 2 + tab
->M
;
616 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
617 if (!isl_int_is_pos(tab
->mat
->row
[i
][off
+ var
->index
]))
619 if (isl_tab_var_from_row(tab
, i
)->is_nonneg
)
625 static int row_cmp(struct isl_tab
*tab
, int r1
, int r2
, int c
, isl_int t
)
627 unsigned off
= 2 + tab
->M
;
631 isl_int_mul(t
, tab
->mat
->row
[r1
][2], tab
->mat
->row
[r2
][off
+c
]);
632 isl_int_submul(t
, tab
->mat
->row
[r2
][2], tab
->mat
->row
[r1
][off
+c
]);
637 isl_int_mul(t
, tab
->mat
->row
[r1
][1], tab
->mat
->row
[r2
][off
+ c
]);
638 isl_int_submul(t
, tab
->mat
->row
[r2
][1], tab
->mat
->row
[r1
][off
+ c
]);
639 return isl_int_sgn(t
);
642 /* Given the index of a column "c", return the index of a row
643 * that can be used to pivot the column in, with either an increase
644 * (sgn > 0) or a decrease (sgn < 0) of the corresponding variable.
645 * If "var" is not NULL, then the row returned will be different from
646 * the one associated with "var".
648 * Each row in the tableau is of the form
650 * x_r = a_r0 + \sum_i a_ri x_i
652 * Only rows with x_r >= 0 and with the sign of a_ri opposite to "sgn"
653 * impose any limit on the increase or decrease in the value of x_c
654 * and this bound is equal to a_r0 / |a_rc|. We are therefore looking
655 * for the row with the smallest (most stringent) such bound.
656 * Note that the common denominator of each row drops out of the fraction.
657 * To check if row j has a smaller bound than row r, i.e.,
658 * a_j0 / |a_jc| < a_r0 / |a_rc| or a_j0 |a_rc| < a_r0 |a_jc|,
659 * we check if -sign(a_jc) (a_j0 a_rc - a_r0 a_jc) < 0,
660 * where -sign(a_jc) is equal to "sgn".
662 static int pivot_row(struct isl_tab
*tab
,
663 struct isl_tab_var
*var
, int sgn
, int c
)
667 unsigned off
= 2 + tab
->M
;
671 for (j
= tab
->n_redundant
; j
< tab
->n_row
; ++j
) {
672 if (var
&& j
== var
->index
)
674 if (!isl_tab_var_from_row(tab
, j
)->is_nonneg
)
676 if (sgn
* isl_int_sgn(tab
->mat
->row
[j
][off
+ c
]) >= 0)
682 tsgn
= sgn
* row_cmp(tab
, r
, j
, c
, t
);
683 if (tsgn
< 0 || (tsgn
== 0 &&
684 tab
->row_var
[j
] < tab
->row_var
[r
]))
691 /* Find a pivot (row and col) that will increase (sgn > 0) or decrease
692 * (sgn < 0) the value of row variable var.
693 * If not NULL, then skip_var is a row variable that should be ignored
694 * while looking for a pivot row. It is usually equal to var.
696 * As the given row in the tableau is of the form
698 * x_r = a_r0 + \sum_i a_ri x_i
700 * we need to find a column such that the sign of a_ri is equal to "sgn"
701 * (such that an increase in x_i will have the desired effect) or a
702 * column with a variable that may attain negative values.
703 * If a_ri is positive, then we need to move x_i in the same direction
704 * to obtain the desired effect. Otherwise, x_i has to move in the
705 * opposite direction.
707 static void find_pivot(struct isl_tab
*tab
,
708 struct isl_tab_var
*var
, struct isl_tab_var
*skip_var
,
709 int sgn
, int *row
, int *col
)
716 isl_assert(tab
->mat
->ctx
, var
->is_row
, return);
717 tr
= tab
->mat
->row
[var
->index
] + 2 + tab
->M
;
720 for (j
= tab
->n_dead
; j
< tab
->n_col
; ++j
) {
721 if (isl_int_is_zero(tr
[j
]))
723 if (isl_int_sgn(tr
[j
]) != sgn
&&
724 var_from_col(tab
, j
)->is_nonneg
)
726 if (c
< 0 || tab
->col_var
[j
] < tab
->col_var
[c
])
732 sgn
*= isl_int_sgn(tr
[c
]);
733 r
= pivot_row(tab
, skip_var
, sgn
, c
);
734 *row
= r
< 0 ? var
->index
: r
;
738 /* Return 1 if row "row" represents an obviously redundant inequality.
740 * - it represents an inequality or a variable
741 * - that is the sum of a non-negative sample value and a positive
742 * combination of zero or more non-negative constraints.
744 int isl_tab_row_is_redundant(struct isl_tab
*tab
, int row
)
747 unsigned off
= 2 + tab
->M
;
749 if (tab
->row_var
[row
] < 0 && !isl_tab_var_from_row(tab
, row
)->is_nonneg
)
752 if (isl_int_is_neg(tab
->mat
->row
[row
][1]))
754 if (tab
->strict_redundant
&& isl_int_is_zero(tab
->mat
->row
[row
][1]))
756 if (tab
->M
&& isl_int_is_neg(tab
->mat
->row
[row
][2]))
759 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
760 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ i
]))
762 if (tab
->col_var
[i
] >= 0)
764 if (isl_int_is_neg(tab
->mat
->row
[row
][off
+ i
]))
766 if (!var_from_col(tab
, i
)->is_nonneg
)
772 static void swap_rows(struct isl_tab
*tab
, int row1
, int row2
)
775 enum isl_tab_row_sign s
;
777 t
= tab
->row_var
[row1
];
778 tab
->row_var
[row1
] = tab
->row_var
[row2
];
779 tab
->row_var
[row2
] = t
;
780 isl_tab_var_from_row(tab
, row1
)->index
= row1
;
781 isl_tab_var_from_row(tab
, row2
)->index
= row2
;
782 tab
->mat
= isl_mat_swap_rows(tab
->mat
, row1
, row2
);
786 s
= tab
->row_sign
[row1
];
787 tab
->row_sign
[row1
] = tab
->row_sign
[row2
];
788 tab
->row_sign
[row2
] = s
;
791 static int push_union(struct isl_tab
*tab
,
792 enum isl_tab_undo_type type
, union isl_tab_undo_val u
) WARN_UNUSED
;
793 static int push_union(struct isl_tab
*tab
,
794 enum isl_tab_undo_type type
, union isl_tab_undo_val u
)
796 struct isl_tab_undo
*undo
;
803 undo
= isl_alloc_type(tab
->mat
->ctx
, struct isl_tab_undo
);
808 undo
->next
= tab
->top
;
814 int isl_tab_push_var(struct isl_tab
*tab
,
815 enum isl_tab_undo_type type
, struct isl_tab_var
*var
)
817 union isl_tab_undo_val u
;
819 u
.var_index
= tab
->row_var
[var
->index
];
821 u
.var_index
= tab
->col_var
[var
->index
];
822 return push_union(tab
, type
, u
);
825 int isl_tab_push(struct isl_tab
*tab
, enum isl_tab_undo_type type
)
827 union isl_tab_undo_val u
= { 0 };
828 return push_union(tab
, type
, u
);
831 /* Push a record on the undo stack describing the current basic
832 * variables, so that the this state can be restored during rollback.
834 int isl_tab_push_basis(struct isl_tab
*tab
)
837 union isl_tab_undo_val u
;
839 u
.col_var
= isl_alloc_array(tab
->mat
->ctx
, int, tab
->n_col
);
842 for (i
= 0; i
< tab
->n_col
; ++i
)
843 u
.col_var
[i
] = tab
->col_var
[i
];
844 return push_union(tab
, isl_tab_undo_saved_basis
, u
);
847 int isl_tab_push_callback(struct isl_tab
*tab
, struct isl_tab_callback
*callback
)
849 union isl_tab_undo_val u
;
850 u
.callback
= callback
;
851 return push_union(tab
, isl_tab_undo_callback
, u
);
854 struct isl_tab
*isl_tab_init_samples(struct isl_tab
*tab
)
861 tab
->samples
= isl_mat_alloc(tab
->mat
->ctx
, 1, 1 + tab
->n_var
);
864 tab
->sample_index
= isl_alloc_array(tab
->mat
->ctx
, int, 1);
865 if (!tab
->sample_index
)
873 struct isl_tab
*isl_tab_add_sample(struct isl_tab
*tab
,
874 __isl_take isl_vec
*sample
)
879 if (tab
->n_sample
+ 1 > tab
->samples
->n_row
) {
880 int *t
= isl_realloc_array(tab
->mat
->ctx
,
881 tab
->sample_index
, int, tab
->n_sample
+ 1);
884 tab
->sample_index
= t
;
887 tab
->samples
= isl_mat_extend(tab
->samples
,
888 tab
->n_sample
+ 1, tab
->samples
->n_col
);
892 isl_seq_cpy(tab
->samples
->row
[tab
->n_sample
], sample
->el
, sample
->size
);
893 isl_vec_free(sample
);
894 tab
->sample_index
[tab
->n_sample
] = tab
->n_sample
;
899 isl_vec_free(sample
);
904 struct isl_tab
*isl_tab_drop_sample(struct isl_tab
*tab
, int s
)
906 if (s
!= tab
->n_outside
) {
907 int t
= tab
->sample_index
[tab
->n_outside
];
908 tab
->sample_index
[tab
->n_outside
] = tab
->sample_index
[s
];
909 tab
->sample_index
[s
] = t
;
910 isl_mat_swap_rows(tab
->samples
, tab
->n_outside
, s
);
913 if (isl_tab_push(tab
, isl_tab_undo_drop_sample
) < 0) {
921 /* Record the current number of samples so that we can remove newer
922 * samples during a rollback.
924 int isl_tab_save_samples(struct isl_tab
*tab
)
926 union isl_tab_undo_val u
;
932 return push_union(tab
, isl_tab_undo_saved_samples
, u
);
935 /* Mark row with index "row" as being redundant.
936 * If we may need to undo the operation or if the row represents
937 * a variable of the original problem, the row is kept,
938 * but no longer considered when looking for a pivot row.
939 * Otherwise, the row is simply removed.
941 * The row may be interchanged with some other row. If it
942 * is interchanged with a later row, return 1. Otherwise return 0.
943 * If the rows are checked in order in the calling function,
944 * then a return value of 1 means that the row with the given
945 * row number may now contain a different row that hasn't been checked yet.
947 int isl_tab_mark_redundant(struct isl_tab
*tab
, int row
)
949 struct isl_tab_var
*var
= isl_tab_var_from_row(tab
, row
);
950 var
->is_redundant
= 1;
951 isl_assert(tab
->mat
->ctx
, row
>= tab
->n_redundant
, return -1);
952 if (tab
->preserve
|| tab
->need_undo
|| tab
->row_var
[row
] >= 0) {
953 if (tab
->row_var
[row
] >= 0 && !var
->is_nonneg
) {
955 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, var
) < 0)
958 if (row
!= tab
->n_redundant
)
959 swap_rows(tab
, row
, tab
->n_redundant
);
961 return isl_tab_push_var(tab
, isl_tab_undo_redundant
, var
);
963 if (row
!= tab
->n_row
- 1)
964 swap_rows(tab
, row
, tab
->n_row
- 1);
965 isl_tab_var_from_row(tab
, tab
->n_row
- 1)->index
= -1;
971 int isl_tab_mark_empty(struct isl_tab
*tab
)
975 if (!tab
->empty
&& tab
->need_undo
)
976 if (isl_tab_push(tab
, isl_tab_undo_empty
) < 0)
982 int isl_tab_freeze_constraint(struct isl_tab
*tab
, int con
)
984 struct isl_tab_var
*var
;
989 var
= &tab
->con
[con
];
997 return isl_tab_push_var(tab
, isl_tab_undo_freeze
, var
);
1002 /* Update the rows signs after a pivot of "row" and "col", with "row_sgn"
1003 * the original sign of the pivot element.
1004 * We only keep track of row signs during PILP solving and in this case
1005 * we only pivot a row with negative sign (meaning the value is always
1006 * non-positive) using a positive pivot element.
1008 * For each row j, the new value of the parametric constant is equal to
1010 * a_j0 - a_jc a_r0/a_rc
1012 * where a_j0 is the original parametric constant, a_rc is the pivot element,
1013 * a_r0 is the parametric constant of the pivot row and a_jc is the
1014 * pivot column entry of the row j.
1015 * Since a_r0 is non-positive and a_rc is positive, the sign of row j
1016 * remains the same if a_jc has the same sign as the row j or if
1017 * a_jc is zero. In all other cases, we reset the sign to "unknown".
1019 static void update_row_sign(struct isl_tab
*tab
, int row
, int col
, int row_sgn
)
1022 struct isl_mat
*mat
= tab
->mat
;
1023 unsigned off
= 2 + tab
->M
;
1028 if (tab
->row_sign
[row
] == 0)
1030 isl_assert(mat
->ctx
, row_sgn
> 0, return);
1031 isl_assert(mat
->ctx
, tab
->row_sign
[row
] == isl_tab_row_neg
, return);
1032 tab
->row_sign
[row
] = isl_tab_row_pos
;
1033 for (i
= 0; i
< tab
->n_row
; ++i
) {
1037 s
= isl_int_sgn(mat
->row
[i
][off
+ col
]);
1040 if (!tab
->row_sign
[i
])
1042 if (s
< 0 && tab
->row_sign
[i
] == isl_tab_row_neg
)
1044 if (s
> 0 && tab
->row_sign
[i
] == isl_tab_row_pos
)
1046 tab
->row_sign
[i
] = isl_tab_row_unknown
;
1050 /* Given a row number "row" and a column number "col", pivot the tableau
1051 * such that the associated variables are interchanged.
1052 * The given row in the tableau expresses
1054 * x_r = a_r0 + \sum_i a_ri x_i
1058 * x_c = 1/a_rc x_r - a_r0/a_rc + sum_{i \ne r} -a_ri/a_rc
1060 * Substituting this equality into the other rows
1062 * x_j = a_j0 + \sum_i a_ji x_i
1064 * with a_jc \ne 0, we obtain
1066 * 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
1073 * where i is any other column and j is any other row,
1074 * is therefore transformed into
1076 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1077 * 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)
1079 * The transformation is performed along the following steps
1081 * d_r/n_rc n_ri/n_rc
1084 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1087 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1088 * n_jc/(|n_rc| d_j) n_ji/(|n_rc| d_j)
1090 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1091 * n_jc/(|n_rc| d_j) (n_ji |n_rc|)/(|n_rc| d_j)
1093 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1094 * n_jc/(|n_rc| d_j) (n_ji |n_rc| - s(n_rc)n_jc n_ri)/(|n_rc| d_j)
1096 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1097 * 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)
1100 int isl_tab_pivot(struct isl_tab
*tab
, int row
, int col
)
1105 struct isl_mat
*mat
= tab
->mat
;
1106 struct isl_tab_var
*var
;
1107 unsigned off
= 2 + tab
->M
;
1109 if (tab
->mat
->ctx
->abort
) {
1110 isl_ctx_set_error(tab
->mat
->ctx
, isl_error_abort
);
1114 isl_int_swap(mat
->row
[row
][0], mat
->row
[row
][off
+ col
]);
1115 sgn
= isl_int_sgn(mat
->row
[row
][0]);
1117 isl_int_neg(mat
->row
[row
][0], mat
->row
[row
][0]);
1118 isl_int_neg(mat
->row
[row
][off
+ col
], mat
->row
[row
][off
+ col
]);
1120 for (j
= 0; j
< off
- 1 + tab
->n_col
; ++j
) {
1121 if (j
== off
- 1 + col
)
1123 isl_int_neg(mat
->row
[row
][1 + j
], mat
->row
[row
][1 + j
]);
1125 if (!isl_int_is_one(mat
->row
[row
][0]))
1126 isl_seq_normalize(mat
->ctx
, mat
->row
[row
], off
+ tab
->n_col
);
1127 for (i
= 0; i
< tab
->n_row
; ++i
) {
1130 if (isl_int_is_zero(mat
->row
[i
][off
+ col
]))
1132 isl_int_mul(mat
->row
[i
][0], mat
->row
[i
][0], mat
->row
[row
][0]);
1133 for (j
= 0; j
< off
- 1 + tab
->n_col
; ++j
) {
1134 if (j
== off
- 1 + col
)
1136 isl_int_mul(mat
->row
[i
][1 + j
],
1137 mat
->row
[i
][1 + j
], mat
->row
[row
][0]);
1138 isl_int_addmul(mat
->row
[i
][1 + j
],
1139 mat
->row
[i
][off
+ col
], mat
->row
[row
][1 + j
]);
1141 isl_int_mul(mat
->row
[i
][off
+ col
],
1142 mat
->row
[i
][off
+ col
], mat
->row
[row
][off
+ col
]);
1143 if (!isl_int_is_one(mat
->row
[i
][0]))
1144 isl_seq_normalize(mat
->ctx
, mat
->row
[i
], off
+ tab
->n_col
);
1146 t
= tab
->row_var
[row
];
1147 tab
->row_var
[row
] = tab
->col_var
[col
];
1148 tab
->col_var
[col
] = t
;
1149 var
= isl_tab_var_from_row(tab
, row
);
1152 var
= var_from_col(tab
, col
);
1155 update_row_sign(tab
, row
, col
, sgn
);
1158 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
1159 if (isl_int_is_zero(mat
->row
[i
][off
+ col
]))
1161 if (!isl_tab_var_from_row(tab
, i
)->frozen
&&
1162 isl_tab_row_is_redundant(tab
, i
)) {
1163 int redo
= isl_tab_mark_redundant(tab
, i
);
1173 /* If "var" represents a column variable, then pivot is up (sgn > 0)
1174 * or down (sgn < 0) to a row. The variable is assumed not to be
1175 * unbounded in the specified direction.
1176 * If sgn = 0, then the variable is unbounded in both directions,
1177 * and we pivot with any row we can find.
1179 static int to_row(struct isl_tab
*tab
, struct isl_tab_var
*var
, int sign
) WARN_UNUSED
;
1180 static int to_row(struct isl_tab
*tab
, struct isl_tab_var
*var
, int sign
)
1183 unsigned off
= 2 + tab
->M
;
1189 for (r
= tab
->n_redundant
; r
< tab
->n_row
; ++r
)
1190 if (!isl_int_is_zero(tab
->mat
->row
[r
][off
+var
->index
]))
1192 isl_assert(tab
->mat
->ctx
, r
< tab
->n_row
, return -1);
1194 r
= pivot_row(tab
, NULL
, sign
, var
->index
);
1195 isl_assert(tab
->mat
->ctx
, r
>= 0, return -1);
1198 return isl_tab_pivot(tab
, r
, var
->index
);
1201 /* Check whether all variables that are marked as non-negative
1202 * also have a non-negative sample value. This function is not
1203 * called from the current code but is useful during debugging.
1205 static void check_table(struct isl_tab
*tab
) __attribute__ ((unused
));
1206 static void check_table(struct isl_tab
*tab
)
1212 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
1213 struct isl_tab_var
*var
;
1214 var
= isl_tab_var_from_row(tab
, i
);
1215 if (!var
->is_nonneg
)
1218 isl_assert(tab
->mat
->ctx
,
1219 !isl_int_is_neg(tab
->mat
->row
[i
][2]), abort());
1220 if (isl_int_is_pos(tab
->mat
->row
[i
][2]))
1223 isl_assert(tab
->mat
->ctx
, !isl_int_is_neg(tab
->mat
->row
[i
][1]),
1228 /* Return the sign of the maximal value of "var".
1229 * If the sign is not negative, then on return from this function,
1230 * the sample value will also be non-negative.
1232 * If "var" is manifestly unbounded wrt positive values, we are done.
1233 * Otherwise, we pivot the variable up to a row if needed
1234 * Then we continue pivoting down until either
1235 * - no more down pivots can be performed
1236 * - the sample value is positive
1237 * - the variable is pivoted into a manifestly unbounded column
1239 static int sign_of_max(struct isl_tab
*tab
, struct isl_tab_var
*var
)
1243 if (max_is_manifestly_unbounded(tab
, var
))
1245 if (to_row(tab
, var
, 1) < 0)
1247 while (!isl_int_is_pos(tab
->mat
->row
[var
->index
][1])) {
1248 find_pivot(tab
, var
, var
, 1, &row
, &col
);
1250 return isl_int_sgn(tab
->mat
->row
[var
->index
][1]);
1251 if (isl_tab_pivot(tab
, row
, col
) < 0)
1253 if (!var
->is_row
) /* manifestly unbounded */
1259 int isl_tab_sign_of_max(struct isl_tab
*tab
, int con
)
1261 struct isl_tab_var
*var
;
1266 var
= &tab
->con
[con
];
1267 isl_assert(tab
->mat
->ctx
, !var
->is_redundant
, return -2);
1268 isl_assert(tab
->mat
->ctx
, !var
->is_zero
, return -2);
1270 return sign_of_max(tab
, var
);
1273 static int row_is_neg(struct isl_tab
*tab
, int row
)
1276 return isl_int_is_neg(tab
->mat
->row
[row
][1]);
1277 if (isl_int_is_pos(tab
->mat
->row
[row
][2]))
1279 if (isl_int_is_neg(tab
->mat
->row
[row
][2]))
1281 return isl_int_is_neg(tab
->mat
->row
[row
][1]);
1284 static int row_sgn(struct isl_tab
*tab
, int row
)
1287 return isl_int_sgn(tab
->mat
->row
[row
][1]);
1288 if (!isl_int_is_zero(tab
->mat
->row
[row
][2]))
1289 return isl_int_sgn(tab
->mat
->row
[row
][2]);
1291 return isl_int_sgn(tab
->mat
->row
[row
][1]);
1294 /* Perform pivots until the row variable "var" has a non-negative
1295 * sample value or until no more upward pivots can be performed.
1296 * Return the sign of the sample value after the pivots have been
1299 static int restore_row(struct isl_tab
*tab
, struct isl_tab_var
*var
)
1303 while (row_is_neg(tab
, var
->index
)) {
1304 find_pivot(tab
, var
, var
, 1, &row
, &col
);
1307 if (isl_tab_pivot(tab
, row
, col
) < 0)
1309 if (!var
->is_row
) /* manifestly unbounded */
1312 return row_sgn(tab
, var
->index
);
1315 /* Perform pivots until we are sure that the row variable "var"
1316 * can attain non-negative values. After return from this
1317 * function, "var" is still a row variable, but its sample
1318 * value may not be non-negative, even if the function returns 1.
1320 static int at_least_zero(struct isl_tab
*tab
, struct isl_tab_var
*var
)
1324 while (isl_int_is_neg(tab
->mat
->row
[var
->index
][1])) {
1325 find_pivot(tab
, var
, var
, 1, &row
, &col
);
1328 if (row
== var
->index
) /* manifestly unbounded */
1330 if (isl_tab_pivot(tab
, row
, col
) < 0)
1333 return !isl_int_is_neg(tab
->mat
->row
[var
->index
][1]);
1336 /* Return a negative value if "var" can attain negative values.
1337 * Return a non-negative value otherwise.
1339 * If "var" is manifestly unbounded wrt negative values, we are done.
1340 * Otherwise, if var is in a column, we can pivot it down to a row.
1341 * Then we continue pivoting down until either
1342 * - the pivot would result in a manifestly unbounded column
1343 * => we don't perform the pivot, but simply return -1
1344 * - no more down pivots can be performed
1345 * - the sample value is negative
1346 * If the sample value becomes negative and the variable is supposed
1347 * to be nonnegative, then we undo the last pivot.
1348 * However, if the last pivot has made the pivoting variable
1349 * obviously redundant, then it may have moved to another row.
1350 * In that case we look for upward pivots until we reach a non-negative
1353 static int sign_of_min(struct isl_tab
*tab
, struct isl_tab_var
*var
)
1356 struct isl_tab_var
*pivot_var
= NULL
;
1358 if (min_is_manifestly_unbounded(tab
, var
))
1362 row
= pivot_row(tab
, NULL
, -1, col
);
1363 pivot_var
= var_from_col(tab
, col
);
1364 if (isl_tab_pivot(tab
, row
, col
) < 0)
1366 if (var
->is_redundant
)
1368 if (isl_int_is_neg(tab
->mat
->row
[var
->index
][1])) {
1369 if (var
->is_nonneg
) {
1370 if (!pivot_var
->is_redundant
&&
1371 pivot_var
->index
== row
) {
1372 if (isl_tab_pivot(tab
, row
, col
) < 0)
1375 if (restore_row(tab
, var
) < -1)
1381 if (var
->is_redundant
)
1383 while (!isl_int_is_neg(tab
->mat
->row
[var
->index
][1])) {
1384 find_pivot(tab
, var
, var
, -1, &row
, &col
);
1385 if (row
== var
->index
)
1388 return isl_int_sgn(tab
->mat
->row
[var
->index
][1]);
1389 pivot_var
= var_from_col(tab
, col
);
1390 if (isl_tab_pivot(tab
, row
, col
) < 0)
1392 if (var
->is_redundant
)
1395 if (pivot_var
&& var
->is_nonneg
) {
1396 /* pivot back to non-negative value */
1397 if (!pivot_var
->is_redundant
&& pivot_var
->index
== row
) {
1398 if (isl_tab_pivot(tab
, row
, col
) < 0)
1401 if (restore_row(tab
, var
) < -1)
1407 static int row_at_most_neg_one(struct isl_tab
*tab
, int row
)
1410 if (isl_int_is_pos(tab
->mat
->row
[row
][2]))
1412 if (isl_int_is_neg(tab
->mat
->row
[row
][2]))
1415 return isl_int_is_neg(tab
->mat
->row
[row
][1]) &&
1416 isl_int_abs_ge(tab
->mat
->row
[row
][1],
1417 tab
->mat
->row
[row
][0]);
1420 /* Return 1 if "var" can attain values <= -1.
1421 * Return 0 otherwise.
1423 * The sample value of "var" is assumed to be non-negative when the
1424 * the function is called. If 1 is returned then the constraint
1425 * is not redundant and the sample value is made non-negative again before
1426 * the function returns.
1428 int isl_tab_min_at_most_neg_one(struct isl_tab
*tab
, struct isl_tab_var
*var
)
1431 struct isl_tab_var
*pivot_var
;
1433 if (min_is_manifestly_unbounded(tab
, var
))
1437 row
= pivot_row(tab
, NULL
, -1, col
);
1438 pivot_var
= var_from_col(tab
, col
);
1439 if (isl_tab_pivot(tab
, row
, col
) < 0)
1441 if (var
->is_redundant
)
1443 if (row_at_most_neg_one(tab
, var
->index
)) {
1444 if (var
->is_nonneg
) {
1445 if (!pivot_var
->is_redundant
&&
1446 pivot_var
->index
== row
) {
1447 if (isl_tab_pivot(tab
, row
, col
) < 0)
1450 if (restore_row(tab
, var
) < -1)
1456 if (var
->is_redundant
)
1459 find_pivot(tab
, var
, var
, -1, &row
, &col
);
1460 if (row
== var
->index
) {
1461 if (restore_row(tab
, var
) < -1)
1467 pivot_var
= var_from_col(tab
, col
);
1468 if (isl_tab_pivot(tab
, row
, col
) < 0)
1470 if (var
->is_redundant
)
1472 } while (!row_at_most_neg_one(tab
, var
->index
));
1473 if (var
->is_nonneg
) {
1474 /* pivot back to non-negative value */
1475 if (!pivot_var
->is_redundant
&& pivot_var
->index
== row
)
1476 if (isl_tab_pivot(tab
, row
, col
) < 0)
1478 if (restore_row(tab
, var
) < -1)
1484 /* Return 1 if "var" can attain values >= 1.
1485 * Return 0 otherwise.
1487 static int at_least_one(struct isl_tab
*tab
, struct isl_tab_var
*var
)
1492 if (max_is_manifestly_unbounded(tab
, var
))
1494 if (to_row(tab
, var
, 1) < 0)
1496 r
= tab
->mat
->row
[var
->index
];
1497 while (isl_int_lt(r
[1], r
[0])) {
1498 find_pivot(tab
, var
, var
, 1, &row
, &col
);
1500 return isl_int_ge(r
[1], r
[0]);
1501 if (row
== var
->index
) /* manifestly unbounded */
1503 if (isl_tab_pivot(tab
, row
, col
) < 0)
1509 static void swap_cols(struct isl_tab
*tab
, int col1
, int col2
)
1512 unsigned off
= 2 + tab
->M
;
1513 t
= tab
->col_var
[col1
];
1514 tab
->col_var
[col1
] = tab
->col_var
[col2
];
1515 tab
->col_var
[col2
] = t
;
1516 var_from_col(tab
, col1
)->index
= col1
;
1517 var_from_col(tab
, col2
)->index
= col2
;
1518 tab
->mat
= isl_mat_swap_cols(tab
->mat
, off
+ col1
, off
+ col2
);
1521 /* Mark column with index "col" as representing a zero variable.
1522 * If we may need to undo the operation the column is kept,
1523 * but no longer considered.
1524 * Otherwise, the column is simply removed.
1526 * The column may be interchanged with some other column. If it
1527 * is interchanged with a later column, return 1. Otherwise return 0.
1528 * If the columns are checked in order in the calling function,
1529 * then a return value of 1 means that the column with the given
1530 * column number may now contain a different column that
1531 * hasn't been checked yet.
1533 int isl_tab_kill_col(struct isl_tab
*tab
, int col
)
1535 var_from_col(tab
, col
)->is_zero
= 1;
1536 if (tab
->need_undo
) {
1537 if (isl_tab_push_var(tab
, isl_tab_undo_zero
,
1538 var_from_col(tab
, col
)) < 0)
1540 if (col
!= tab
->n_dead
)
1541 swap_cols(tab
, col
, tab
->n_dead
);
1545 if (col
!= tab
->n_col
- 1)
1546 swap_cols(tab
, col
, tab
->n_col
- 1);
1547 var_from_col(tab
, tab
->n_col
- 1)->index
= -1;
1553 static int row_is_manifestly_non_integral(struct isl_tab
*tab
, int row
)
1555 unsigned off
= 2 + tab
->M
;
1557 if (tab
->M
&& !isl_int_eq(tab
->mat
->row
[row
][2],
1558 tab
->mat
->row
[row
][0]))
1560 if (isl_seq_first_non_zero(tab
->mat
->row
[row
] + off
+ tab
->n_dead
,
1561 tab
->n_col
- tab
->n_dead
) != -1)
1564 return !isl_int_is_divisible_by(tab
->mat
->row
[row
][1],
1565 tab
->mat
->row
[row
][0]);
1568 /* For integer tableaus, check if any of the coordinates are stuck
1569 * at a non-integral value.
1571 static int tab_is_manifestly_empty(struct isl_tab
*tab
)
1580 for (i
= 0; i
< tab
->n_var
; ++i
) {
1581 if (!tab
->var
[i
].is_row
)
1583 if (row_is_manifestly_non_integral(tab
, tab
->var
[i
].index
))
1590 /* Row variable "var" is non-negative and cannot attain any values
1591 * larger than zero. This means that the coefficients of the unrestricted
1592 * column variables are zero and that the coefficients of the non-negative
1593 * column variables are zero or negative.
1594 * Each of the non-negative variables with a negative coefficient can
1595 * then also be written as the negative sum of non-negative variables
1596 * and must therefore also be zero.
1598 static int close_row(struct isl_tab
*tab
, struct isl_tab_var
*var
) WARN_UNUSED
;
1599 static int close_row(struct isl_tab
*tab
, struct isl_tab_var
*var
)
1602 struct isl_mat
*mat
= tab
->mat
;
1603 unsigned off
= 2 + tab
->M
;
1605 isl_assert(tab
->mat
->ctx
, var
->is_nonneg
, return -1);
1608 if (isl_tab_push_var(tab
, isl_tab_undo_zero
, var
) < 0)
1610 for (j
= tab
->n_dead
; j
< tab
->n_col
; ++j
) {
1612 if (isl_int_is_zero(mat
->row
[var
->index
][off
+ j
]))
1614 isl_assert(tab
->mat
->ctx
,
1615 isl_int_is_neg(mat
->row
[var
->index
][off
+ j
]), return -1);
1616 recheck
= isl_tab_kill_col(tab
, j
);
1622 if (isl_tab_mark_redundant(tab
, var
->index
) < 0)
1624 if (tab_is_manifestly_empty(tab
) && isl_tab_mark_empty(tab
) < 0)
1629 /* Add a constraint to the tableau and allocate a row for it.
1630 * Return the index into the constraint array "con".
1632 int isl_tab_allocate_con(struct isl_tab
*tab
)
1636 isl_assert(tab
->mat
->ctx
, tab
->n_row
< tab
->mat
->n_row
, return -1);
1637 isl_assert(tab
->mat
->ctx
, tab
->n_con
< tab
->max_con
, return -1);
1640 tab
->con
[r
].index
= tab
->n_row
;
1641 tab
->con
[r
].is_row
= 1;
1642 tab
->con
[r
].is_nonneg
= 0;
1643 tab
->con
[r
].is_zero
= 0;
1644 tab
->con
[r
].is_redundant
= 0;
1645 tab
->con
[r
].frozen
= 0;
1646 tab
->con
[r
].negated
= 0;
1647 tab
->row_var
[tab
->n_row
] = ~r
;
1651 if (isl_tab_push_var(tab
, isl_tab_undo_allocate
, &tab
->con
[r
]) < 0)
1657 /* Add a variable to the tableau and allocate a column for it.
1658 * Return the index into the variable array "var".
1660 int isl_tab_allocate_var(struct isl_tab
*tab
)
1664 unsigned off
= 2 + tab
->M
;
1666 isl_assert(tab
->mat
->ctx
, tab
->n_col
< tab
->mat
->n_col
, return -1);
1667 isl_assert(tab
->mat
->ctx
, tab
->n_var
< tab
->max_var
, return -1);
1670 tab
->var
[r
].index
= tab
->n_col
;
1671 tab
->var
[r
].is_row
= 0;
1672 tab
->var
[r
].is_nonneg
= 0;
1673 tab
->var
[r
].is_zero
= 0;
1674 tab
->var
[r
].is_redundant
= 0;
1675 tab
->var
[r
].frozen
= 0;
1676 tab
->var
[r
].negated
= 0;
1677 tab
->col_var
[tab
->n_col
] = r
;
1679 for (i
= 0; i
< tab
->n_row
; ++i
)
1680 isl_int_set_si(tab
->mat
->row
[i
][off
+ tab
->n_col
], 0);
1684 if (isl_tab_push_var(tab
, isl_tab_undo_allocate
, &tab
->var
[r
]) < 0)
1690 /* Add a row to the tableau. The row is given as an affine combination
1691 * of the original variables and needs to be expressed in terms of the
1694 * We add each term in turn.
1695 * If r = n/d_r is the current sum and we need to add k x, then
1696 * if x is a column variable, we increase the numerator of
1697 * this column by k d_r
1698 * if x = f/d_x is a row variable, then the new representation of r is
1700 * n k f d_x/g n + d_r/g k f m/d_r n + m/d_g k f
1701 * --- + --- = ------------------- = -------------------
1702 * d_r d_r d_r d_x/g m
1704 * with g the gcd of d_r and d_x and m the lcm of d_r and d_x.
1706 * If tab->M is set, then, internally, each variable x is represented
1707 * as x' - M. We then also need no subtract k d_r from the coefficient of M.
1709 int isl_tab_add_row(struct isl_tab
*tab
, isl_int
*line
)
1715 unsigned off
= 2 + tab
->M
;
1717 r
= isl_tab_allocate_con(tab
);
1723 row
= tab
->mat
->row
[tab
->con
[r
].index
];
1724 isl_int_set_si(row
[0], 1);
1725 isl_int_set(row
[1], line
[0]);
1726 isl_seq_clr(row
+ 2, tab
->M
+ tab
->n_col
);
1727 for (i
= 0; i
< tab
->n_var
; ++i
) {
1728 if (tab
->var
[i
].is_zero
)
1730 if (tab
->var
[i
].is_row
) {
1732 row
[0], tab
->mat
->row
[tab
->var
[i
].index
][0]);
1733 isl_int_swap(a
, row
[0]);
1734 isl_int_divexact(a
, row
[0], a
);
1736 row
[0], tab
->mat
->row
[tab
->var
[i
].index
][0]);
1737 isl_int_mul(b
, b
, line
[1 + i
]);
1738 isl_seq_combine(row
+ 1, a
, row
+ 1,
1739 b
, tab
->mat
->row
[tab
->var
[i
].index
] + 1,
1740 1 + tab
->M
+ tab
->n_col
);
1742 isl_int_addmul(row
[off
+ tab
->var
[i
].index
],
1743 line
[1 + i
], row
[0]);
1744 if (tab
->M
&& i
>= tab
->n_param
&& i
< tab
->n_var
- tab
->n_div
)
1745 isl_int_submul(row
[2], line
[1 + i
], row
[0]);
1747 isl_seq_normalize(tab
->mat
->ctx
, row
, off
+ tab
->n_col
);
1752 tab
->row_sign
[tab
->con
[r
].index
] = isl_tab_row_unknown
;
1757 static int drop_row(struct isl_tab
*tab
, int row
)
1759 isl_assert(tab
->mat
->ctx
, ~tab
->row_var
[row
] == tab
->n_con
- 1, return -1);
1760 if (row
!= tab
->n_row
- 1)
1761 swap_rows(tab
, row
, tab
->n_row
- 1);
1767 static int drop_col(struct isl_tab
*tab
, int col
)
1769 isl_assert(tab
->mat
->ctx
, tab
->col_var
[col
] == tab
->n_var
- 1, return -1);
1770 if (col
!= tab
->n_col
- 1)
1771 swap_cols(tab
, col
, tab
->n_col
- 1);
1777 /* Add inequality "ineq" and check if it conflicts with the
1778 * previously added constraints or if it is obviously redundant.
1780 int isl_tab_add_ineq(struct isl_tab
*tab
, isl_int
*ineq
)
1789 struct isl_basic_map
*bmap
= tab
->bmap
;
1791 isl_assert(tab
->mat
->ctx
, tab
->n_eq
== bmap
->n_eq
, return -1);
1792 isl_assert(tab
->mat
->ctx
,
1793 tab
->n_con
== bmap
->n_eq
+ bmap
->n_ineq
, return -1);
1794 tab
->bmap
= isl_basic_map_add_ineq(tab
->bmap
, ineq
);
1795 if (isl_tab_push(tab
, isl_tab_undo_bmap_ineq
) < 0)
1802 isl_int_swap(ineq
[0], cst
);
1804 r
= isl_tab_add_row(tab
, ineq
);
1806 isl_int_swap(ineq
[0], cst
);
1811 tab
->con
[r
].is_nonneg
= 1;
1812 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
1814 if (isl_tab_row_is_redundant(tab
, tab
->con
[r
].index
)) {
1815 if (isl_tab_mark_redundant(tab
, tab
->con
[r
].index
) < 0)
1820 sgn
= restore_row(tab
, &tab
->con
[r
]);
1824 return isl_tab_mark_empty(tab
);
1825 if (tab
->con
[r
].is_row
&& isl_tab_row_is_redundant(tab
, tab
->con
[r
].index
))
1826 if (isl_tab_mark_redundant(tab
, tab
->con
[r
].index
) < 0)
1831 /* Pivot a non-negative variable down until it reaches the value zero
1832 * and then pivot the variable into a column position.
1834 static int to_col(struct isl_tab
*tab
, struct isl_tab_var
*var
) WARN_UNUSED
;
1835 static int to_col(struct isl_tab
*tab
, struct isl_tab_var
*var
)
1839 unsigned off
= 2 + tab
->M
;
1844 while (isl_int_is_pos(tab
->mat
->row
[var
->index
][1])) {
1845 find_pivot(tab
, var
, NULL
, -1, &row
, &col
);
1846 isl_assert(tab
->mat
->ctx
, row
!= -1, return -1);
1847 if (isl_tab_pivot(tab
, row
, col
) < 0)
1853 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
)
1854 if (!isl_int_is_zero(tab
->mat
->row
[var
->index
][off
+ i
]))
1857 isl_assert(tab
->mat
->ctx
, i
< tab
->n_col
, return -1);
1858 if (isl_tab_pivot(tab
, var
->index
, i
) < 0)
1864 /* We assume Gaussian elimination has been performed on the equalities.
1865 * The equalities can therefore never conflict.
1866 * Adding the equalities is currently only really useful for a later call
1867 * to isl_tab_ineq_type.
1869 static struct isl_tab
*add_eq(struct isl_tab
*tab
, isl_int
*eq
)
1876 r
= isl_tab_add_row(tab
, eq
);
1880 r
= tab
->con
[r
].index
;
1881 i
= isl_seq_first_non_zero(tab
->mat
->row
[r
] + 2 + tab
->M
+ tab
->n_dead
,
1882 tab
->n_col
- tab
->n_dead
);
1883 isl_assert(tab
->mat
->ctx
, i
>= 0, goto error
);
1885 if (isl_tab_pivot(tab
, r
, i
) < 0)
1887 if (isl_tab_kill_col(tab
, i
) < 0)
1897 static int row_is_manifestly_zero(struct isl_tab
*tab
, int row
)
1899 unsigned off
= 2 + tab
->M
;
1901 if (!isl_int_is_zero(tab
->mat
->row
[row
][1]))
1903 if (tab
->M
&& !isl_int_is_zero(tab
->mat
->row
[row
][2]))
1905 return isl_seq_first_non_zero(tab
->mat
->row
[row
] + off
+ tab
->n_dead
,
1906 tab
->n_col
- tab
->n_dead
) == -1;
1909 /* Add an equality that is known to be valid for the given tableau.
1911 int isl_tab_add_valid_eq(struct isl_tab
*tab
, isl_int
*eq
)
1913 struct isl_tab_var
*var
;
1918 r
= isl_tab_add_row(tab
, eq
);
1924 if (row_is_manifestly_zero(tab
, r
)) {
1926 if (isl_tab_mark_redundant(tab
, r
) < 0)
1931 if (isl_int_is_neg(tab
->mat
->row
[r
][1])) {
1932 isl_seq_neg(tab
->mat
->row
[r
] + 1, tab
->mat
->row
[r
] + 1,
1937 if (to_col(tab
, var
) < 0)
1940 if (isl_tab_kill_col(tab
, var
->index
) < 0)
1946 static int add_zero_row(struct isl_tab
*tab
)
1951 r
= isl_tab_allocate_con(tab
);
1955 row
= tab
->mat
->row
[tab
->con
[r
].index
];
1956 isl_seq_clr(row
+ 1, 1 + tab
->M
+ tab
->n_col
);
1957 isl_int_set_si(row
[0], 1);
1962 /* Add equality "eq" and check if it conflicts with the
1963 * previously added constraints or if it is obviously redundant.
1965 int isl_tab_add_eq(struct isl_tab
*tab
, isl_int
*eq
)
1967 struct isl_tab_undo
*snap
= NULL
;
1968 struct isl_tab_var
*var
;
1976 isl_assert(tab
->mat
->ctx
, !tab
->M
, return -1);
1979 snap
= isl_tab_snap(tab
);
1983 isl_int_swap(eq
[0], cst
);
1985 r
= isl_tab_add_row(tab
, eq
);
1987 isl_int_swap(eq
[0], cst
);
1995 if (row_is_manifestly_zero(tab
, row
)) {
1997 if (isl_tab_rollback(tab
, snap
) < 0)
2005 tab
->bmap
= isl_basic_map_add_ineq(tab
->bmap
, eq
);
2006 if (isl_tab_push(tab
, isl_tab_undo_bmap_ineq
) < 0)
2008 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
2009 tab
->bmap
= isl_basic_map_add_ineq(tab
->bmap
, eq
);
2010 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
2011 if (isl_tab_push(tab
, isl_tab_undo_bmap_ineq
) < 0)
2015 if (add_zero_row(tab
) < 0)
2019 sgn
= isl_int_sgn(tab
->mat
->row
[row
][1]);
2022 isl_seq_neg(tab
->mat
->row
[row
] + 1, tab
->mat
->row
[row
] + 1,
2029 sgn
= sign_of_max(tab
, var
);
2033 if (isl_tab_mark_empty(tab
) < 0)
2040 if (to_col(tab
, var
) < 0)
2043 if (isl_tab_kill_col(tab
, var
->index
) < 0)
2049 /* Construct and return an inequality that expresses an upper bound
2051 * In particular, if the div is given by
2055 * then the inequality expresses
2059 static struct isl_vec
*ineq_for_div(struct isl_basic_map
*bmap
, unsigned div
)
2063 struct isl_vec
*ineq
;
2068 total
= isl_basic_map_total_dim(bmap
);
2069 div_pos
= 1 + total
- bmap
->n_div
+ div
;
2071 ineq
= isl_vec_alloc(bmap
->ctx
, 1 + total
);
2075 isl_seq_cpy(ineq
->el
, bmap
->div
[div
] + 1, 1 + total
);
2076 isl_int_neg(ineq
->el
[div_pos
], bmap
->div
[div
][0]);
2080 /* For a div d = floor(f/m), add the constraints
2083 * -(f-(m-1)) + m d >= 0
2085 * Note that the second constraint is the negation of
2089 * If add_ineq is not NULL, then this function is used
2090 * instead of isl_tab_add_ineq to effectively add the inequalities.
2092 static int add_div_constraints(struct isl_tab
*tab
, unsigned div
,
2093 int (*add_ineq
)(void *user
, isl_int
*), void *user
)
2097 struct isl_vec
*ineq
;
2099 total
= isl_basic_map_total_dim(tab
->bmap
);
2100 div_pos
= 1 + total
- tab
->bmap
->n_div
+ div
;
2102 ineq
= ineq_for_div(tab
->bmap
, div
);
2107 if (add_ineq(user
, ineq
->el
) < 0)
2110 if (isl_tab_add_ineq(tab
, ineq
->el
) < 0)
2114 isl_seq_neg(ineq
->el
, tab
->bmap
->div
[div
] + 1, 1 + total
);
2115 isl_int_set(ineq
->el
[div_pos
], tab
->bmap
->div
[div
][0]);
2116 isl_int_add(ineq
->el
[0], ineq
->el
[0], ineq
->el
[div_pos
]);
2117 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
2120 if (add_ineq(user
, ineq
->el
) < 0)
2123 if (isl_tab_add_ineq(tab
, ineq
->el
) < 0)
2135 /* Check whether the div described by "div" is obviously non-negative.
2136 * If we are using a big parameter, then we will encode the div
2137 * as div' = M + div, which is always non-negative.
2138 * Otherwise, we check whether div is a non-negative affine combination
2139 * of non-negative variables.
2141 static int div_is_nonneg(struct isl_tab
*tab
, __isl_keep isl_vec
*div
)
2148 if (isl_int_is_neg(div
->el
[1]))
2151 for (i
= 0; i
< tab
->n_var
; ++i
) {
2152 if (isl_int_is_neg(div
->el
[2 + i
]))
2154 if (isl_int_is_zero(div
->el
[2 + i
]))
2156 if (!tab
->var
[i
].is_nonneg
)
2163 /* Add an extra div, prescribed by "div" to the tableau and
2164 * the associated bmap (which is assumed to be non-NULL).
2166 * If add_ineq is not NULL, then this function is used instead
2167 * of isl_tab_add_ineq to add the div constraints.
2168 * This complication is needed because the code in isl_tab_pip
2169 * wants to perform some extra processing when an inequality
2170 * is added to the tableau.
2172 int isl_tab_add_div(struct isl_tab
*tab
, __isl_keep isl_vec
*div
,
2173 int (*add_ineq
)(void *user
, isl_int
*), void *user
)
2182 isl_assert(tab
->mat
->ctx
, tab
->bmap
, return -1);
2184 nonneg
= div_is_nonneg(tab
, div
);
2186 if (isl_tab_extend_cons(tab
, 3) < 0)
2188 if (isl_tab_extend_vars(tab
, 1) < 0)
2190 r
= isl_tab_allocate_var(tab
);
2195 tab
->var
[r
].is_nonneg
= 1;
2197 tab
->bmap
= isl_basic_map_extend_space(tab
->bmap
,
2198 isl_basic_map_get_space(tab
->bmap
), 1, 0, 2);
2199 k
= isl_basic_map_alloc_div(tab
->bmap
);
2202 isl_seq_cpy(tab
->bmap
->div
[k
], div
->el
, div
->size
);
2203 if (isl_tab_push(tab
, isl_tab_undo_bmap_div
) < 0)
2206 if (add_div_constraints(tab
, k
, add_ineq
, user
) < 0)
2212 /* If "track" is set, then we want to keep track of all constraints in tab
2213 * in its bmap field. This field is initialized from a copy of "bmap",
2214 * so we need to make sure that all constraints in "bmap" also appear
2215 * in the constructed tab.
2217 __isl_give
struct isl_tab
*isl_tab_from_basic_map(
2218 __isl_keep isl_basic_map
*bmap
, int track
)
2221 struct isl_tab
*tab
;
2225 tab
= isl_tab_alloc(bmap
->ctx
,
2226 isl_basic_map_total_dim(bmap
) + bmap
->n_ineq
+ 1,
2227 isl_basic_map_total_dim(bmap
), 0);
2230 tab
->preserve
= track
;
2231 tab
->rational
= ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
2232 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
)) {
2233 if (isl_tab_mark_empty(tab
) < 0)
2237 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
2238 tab
= add_eq(tab
, bmap
->eq
[i
]);
2242 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
2243 if (isl_tab_add_ineq(tab
, bmap
->ineq
[i
]) < 0)
2249 if (track
&& isl_tab_track_bmap(tab
, isl_basic_map_copy(bmap
)) < 0)
2257 __isl_give
struct isl_tab
*isl_tab_from_basic_set(
2258 __isl_keep isl_basic_set
*bset
, int track
)
2260 return isl_tab_from_basic_map(bset
, track
);
2263 /* Construct a tableau corresponding to the recession cone of "bset".
2265 struct isl_tab
*isl_tab_from_recession_cone(__isl_keep isl_basic_set
*bset
,
2270 struct isl_tab
*tab
;
2271 unsigned offset
= 0;
2276 offset
= isl_basic_set_dim(bset
, isl_dim_param
);
2277 tab
= isl_tab_alloc(bset
->ctx
, bset
->n_eq
+ bset
->n_ineq
,
2278 isl_basic_set_total_dim(bset
) - offset
, 0);
2281 tab
->rational
= ISL_F_ISSET(bset
, ISL_BASIC_SET_RATIONAL
);
2285 for (i
= 0; i
< bset
->n_eq
; ++i
) {
2286 isl_int_swap(bset
->eq
[i
][offset
], cst
);
2288 if (isl_tab_add_eq(tab
, bset
->eq
[i
] + offset
) < 0)
2291 tab
= add_eq(tab
, bset
->eq
[i
]);
2292 isl_int_swap(bset
->eq
[i
][offset
], cst
);
2296 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
2298 isl_int_swap(bset
->ineq
[i
][offset
], cst
);
2299 r
= isl_tab_add_row(tab
, bset
->ineq
[i
] + offset
);
2300 isl_int_swap(bset
->ineq
[i
][offset
], cst
);
2303 tab
->con
[r
].is_nonneg
= 1;
2304 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
2316 /* Assuming "tab" is the tableau of a cone, check if the cone is
2317 * bounded, i.e., if it is empty or only contains the origin.
2319 int isl_tab_cone_is_bounded(struct isl_tab
*tab
)
2327 if (tab
->n_dead
== tab
->n_col
)
2331 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
2332 struct isl_tab_var
*var
;
2334 var
= isl_tab_var_from_row(tab
, i
);
2335 if (!var
->is_nonneg
)
2337 sgn
= sign_of_max(tab
, var
);
2342 if (close_row(tab
, var
) < 0)
2346 if (tab
->n_dead
== tab
->n_col
)
2348 if (i
== tab
->n_row
)
2353 int isl_tab_sample_is_integer(struct isl_tab
*tab
)
2360 for (i
= 0; i
< tab
->n_var
; ++i
) {
2362 if (!tab
->var
[i
].is_row
)
2364 row
= tab
->var
[i
].index
;
2365 if (!isl_int_is_divisible_by(tab
->mat
->row
[row
][1],
2366 tab
->mat
->row
[row
][0]))
2372 static struct isl_vec
*extract_integer_sample(struct isl_tab
*tab
)
2375 struct isl_vec
*vec
;
2377 vec
= isl_vec_alloc(tab
->mat
->ctx
, 1 + tab
->n_var
);
2381 isl_int_set_si(vec
->block
.data
[0], 1);
2382 for (i
= 0; i
< tab
->n_var
; ++i
) {
2383 if (!tab
->var
[i
].is_row
)
2384 isl_int_set_si(vec
->block
.data
[1 + i
], 0);
2386 int row
= tab
->var
[i
].index
;
2387 isl_int_divexact(vec
->block
.data
[1 + i
],
2388 tab
->mat
->row
[row
][1], tab
->mat
->row
[row
][0]);
2395 struct isl_vec
*isl_tab_get_sample_value(struct isl_tab
*tab
)
2398 struct isl_vec
*vec
;
2404 vec
= isl_vec_alloc(tab
->mat
->ctx
, 1 + tab
->n_var
);
2410 isl_int_set_si(vec
->block
.data
[0], 1);
2411 for (i
= 0; i
< tab
->n_var
; ++i
) {
2413 if (!tab
->var
[i
].is_row
) {
2414 isl_int_set_si(vec
->block
.data
[1 + i
], 0);
2417 row
= tab
->var
[i
].index
;
2418 isl_int_gcd(m
, vec
->block
.data
[0], tab
->mat
->row
[row
][0]);
2419 isl_int_divexact(m
, tab
->mat
->row
[row
][0], m
);
2420 isl_seq_scale(vec
->block
.data
, vec
->block
.data
, m
, 1 + i
);
2421 isl_int_divexact(m
, vec
->block
.data
[0], tab
->mat
->row
[row
][0]);
2422 isl_int_mul(vec
->block
.data
[1 + i
], m
, tab
->mat
->row
[row
][1]);
2424 vec
= isl_vec_normalize(vec
);
2430 /* Update "bmap" based on the results of the tableau "tab".
2431 * In particular, implicit equalities are made explicit, redundant constraints
2432 * are removed and if the sample value happens to be integer, it is stored
2433 * in "bmap" (unless "bmap" already had an integer sample).
2435 * The tableau is assumed to have been created from "bmap" using
2436 * isl_tab_from_basic_map.
2438 struct isl_basic_map
*isl_basic_map_update_from_tab(struct isl_basic_map
*bmap
,
2439 struct isl_tab
*tab
)
2451 bmap
= isl_basic_map_set_to_empty(bmap
);
2453 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
2454 if (isl_tab_is_equality(tab
, n_eq
+ i
))
2455 isl_basic_map_inequality_to_equality(bmap
, i
);
2456 else if (isl_tab_is_redundant(tab
, n_eq
+ i
))
2457 isl_basic_map_drop_inequality(bmap
, i
);
2459 if (bmap
->n_eq
!= n_eq
)
2460 isl_basic_map_gauss(bmap
, NULL
);
2461 if (!tab
->rational
&&
2462 !bmap
->sample
&& isl_tab_sample_is_integer(tab
))
2463 bmap
->sample
= extract_integer_sample(tab
);
2467 struct isl_basic_set
*isl_basic_set_update_from_tab(struct isl_basic_set
*bset
,
2468 struct isl_tab
*tab
)
2470 return (struct isl_basic_set
*)isl_basic_map_update_from_tab(
2471 (struct isl_basic_map
*)bset
, tab
);
2474 /* Given a non-negative variable "var", add a new non-negative variable
2475 * that is the opposite of "var", ensuring that var can only attain the
2477 * If var = n/d is a row variable, then the new variable = -n/d.
2478 * If var is a column variables, then the new variable = -var.
2479 * If the new variable cannot attain non-negative values, then
2480 * the resulting tableau is empty.
2481 * Otherwise, we know the value will be zero and we close the row.
2483 static int cut_to_hyperplane(struct isl_tab
*tab
, struct isl_tab_var
*var
)
2488 unsigned off
= 2 + tab
->M
;
2492 isl_assert(tab
->mat
->ctx
, !var
->is_redundant
, return -1);
2493 isl_assert(tab
->mat
->ctx
, var
->is_nonneg
, return -1);
2495 if (isl_tab_extend_cons(tab
, 1) < 0)
2499 tab
->con
[r
].index
= tab
->n_row
;
2500 tab
->con
[r
].is_row
= 1;
2501 tab
->con
[r
].is_nonneg
= 0;
2502 tab
->con
[r
].is_zero
= 0;
2503 tab
->con
[r
].is_redundant
= 0;
2504 tab
->con
[r
].frozen
= 0;
2505 tab
->con
[r
].negated
= 0;
2506 tab
->row_var
[tab
->n_row
] = ~r
;
2507 row
= tab
->mat
->row
[tab
->n_row
];
2510 isl_int_set(row
[0], tab
->mat
->row
[var
->index
][0]);
2511 isl_seq_neg(row
+ 1,
2512 tab
->mat
->row
[var
->index
] + 1, 1 + tab
->n_col
);
2514 isl_int_set_si(row
[0], 1);
2515 isl_seq_clr(row
+ 1, 1 + tab
->n_col
);
2516 isl_int_set_si(row
[off
+ var
->index
], -1);
2521 if (isl_tab_push_var(tab
, isl_tab_undo_allocate
, &tab
->con
[r
]) < 0)
2524 sgn
= sign_of_max(tab
, &tab
->con
[r
]);
2528 if (isl_tab_mark_empty(tab
) < 0)
2532 tab
->con
[r
].is_nonneg
= 1;
2533 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
2536 if (close_row(tab
, &tab
->con
[r
]) < 0)
2542 /* Given a tableau "tab" and an inequality constraint "con" of the tableau,
2543 * relax the inequality by one. That is, the inequality r >= 0 is replaced
2544 * by r' = r + 1 >= 0.
2545 * If r is a row variable, we simply increase the constant term by one
2546 * (taking into account the denominator).
2547 * If r is a column variable, then we need to modify each row that
2548 * refers to r = r' - 1 by substituting this equality, effectively
2549 * subtracting the coefficient of the column from the constant.
2550 * We should only do this if the minimum is manifestly unbounded,
2551 * however. Otherwise, we may end up with negative sample values
2552 * for non-negative variables.
2553 * So, if r is a column variable with a minimum that is not
2554 * manifestly unbounded, then we need to move it to a row.
2555 * However, the sample value of this row may be negative,
2556 * even after the relaxation, so we need to restore it.
2557 * We therefore prefer to pivot a column up to a row, if possible.
2559 struct isl_tab
*isl_tab_relax(struct isl_tab
*tab
, int con
)
2561 struct isl_tab_var
*var
;
2562 unsigned off
= 2 + tab
->M
;
2567 var
= &tab
->con
[con
];
2569 if (var
->is_row
&& (var
->index
< 0 || var
->index
< tab
->n_redundant
))
2570 isl_die(tab
->mat
->ctx
, isl_error_invalid
,
2571 "cannot relax redundant constraint", goto error
);
2572 if (!var
->is_row
&& (var
->index
< 0 || var
->index
< tab
->n_dead
))
2573 isl_die(tab
->mat
->ctx
, isl_error_invalid
,
2574 "cannot relax dead constraint", goto error
);
2576 if (!var
->is_row
&& !max_is_manifestly_unbounded(tab
, var
))
2577 if (to_row(tab
, var
, 1) < 0)
2579 if (!var
->is_row
&& !min_is_manifestly_unbounded(tab
, var
))
2580 if (to_row(tab
, var
, -1) < 0)
2584 isl_int_add(tab
->mat
->row
[var
->index
][1],
2585 tab
->mat
->row
[var
->index
][1], tab
->mat
->row
[var
->index
][0]);
2586 if (restore_row(tab
, var
) < 0)
2591 for (i
= 0; i
< tab
->n_row
; ++i
) {
2592 if (isl_int_is_zero(tab
->mat
->row
[i
][off
+ var
->index
]))
2594 isl_int_sub(tab
->mat
->row
[i
][1], tab
->mat
->row
[i
][1],
2595 tab
->mat
->row
[i
][off
+ var
->index
]);
2600 if (isl_tab_push_var(tab
, isl_tab_undo_relax
, var
) < 0)
2609 /* Remove the sign constraint from constraint "con".
2611 * If the constraint variable was originally marked non-negative,
2612 * then we make sure we mark it non-negative again during rollback.
2614 int isl_tab_unrestrict(struct isl_tab
*tab
, int con
)
2616 struct isl_tab_var
*var
;
2621 var
= &tab
->con
[con
];
2622 if (!var
->is_nonneg
)
2626 if (isl_tab_push_var(tab
, isl_tab_undo_unrestrict
, var
) < 0)
2632 int isl_tab_select_facet(struct isl_tab
*tab
, int con
)
2637 return cut_to_hyperplane(tab
, &tab
->con
[con
]);
2640 static int may_be_equality(struct isl_tab
*tab
, int row
)
2642 return tab
->rational
? isl_int_is_zero(tab
->mat
->row
[row
][1])
2643 : isl_int_lt(tab
->mat
->row
[row
][1],
2644 tab
->mat
->row
[row
][0]);
2647 /* Check for (near) equalities among the constraints.
2648 * A constraint is an equality if it is non-negative and if
2649 * its maximal value is either
2650 * - zero (in case of rational tableaus), or
2651 * - strictly less than 1 (in case of integer tableaus)
2653 * We first mark all non-redundant and non-dead variables that
2654 * are not frozen and not obviously not an equality.
2655 * Then we iterate over all marked variables if they can attain
2656 * any values larger than zero or at least one.
2657 * If the maximal value is zero, we mark any column variables
2658 * that appear in the row as being zero and mark the row as being redundant.
2659 * Otherwise, if the maximal value is strictly less than one (and the
2660 * tableau is integer), then we restrict the value to being zero
2661 * by adding an opposite non-negative variable.
2663 int isl_tab_detect_implicit_equalities(struct isl_tab
*tab
)
2672 if (tab
->n_dead
== tab
->n_col
)
2676 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
2677 struct isl_tab_var
*var
= isl_tab_var_from_row(tab
, i
);
2678 var
->marked
= !var
->frozen
&& var
->is_nonneg
&&
2679 may_be_equality(tab
, i
);
2683 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
2684 struct isl_tab_var
*var
= var_from_col(tab
, i
);
2685 var
->marked
= !var
->frozen
&& var
->is_nonneg
;
2690 struct isl_tab_var
*var
;
2692 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
2693 var
= isl_tab_var_from_row(tab
, i
);
2697 if (i
== tab
->n_row
) {
2698 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
2699 var
= var_from_col(tab
, i
);
2703 if (i
== tab
->n_col
)
2708 sgn
= sign_of_max(tab
, var
);
2712 if (close_row(tab
, var
) < 0)
2714 } else if (!tab
->rational
&& !at_least_one(tab
, var
)) {
2715 if (cut_to_hyperplane(tab
, var
) < 0)
2717 return isl_tab_detect_implicit_equalities(tab
);
2719 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
2720 var
= isl_tab_var_from_row(tab
, i
);
2723 if (may_be_equality(tab
, i
))
2733 /* Update the element of row_var or col_var that corresponds to
2734 * constraint tab->con[i] to a move from position "old" to position "i".
2736 static int update_con_after_move(struct isl_tab
*tab
, int i
, int old
)
2741 index
= tab
->con
[i
].index
;
2744 p
= tab
->con
[i
].is_row
? tab
->row_var
: tab
->col_var
;
2745 if (p
[index
] != ~old
)
2746 isl_die(tab
->mat
->ctx
, isl_error_internal
,
2747 "broken internal state", return -1);
2753 /* Rotate the "n" constraints starting at "first" to the right,
2754 * putting the last constraint in the position of the first constraint.
2756 static int rotate_constraints(struct isl_tab
*tab
, int first
, int n
)
2759 struct isl_tab_var var
;
2764 last
= first
+ n
- 1;
2765 var
= tab
->con
[last
];
2766 for (i
= last
; i
> first
; --i
) {
2767 tab
->con
[i
] = tab
->con
[i
- 1];
2768 if (update_con_after_move(tab
, i
, i
- 1) < 0)
2771 tab
->con
[first
] = var
;
2772 if (update_con_after_move(tab
, first
, last
) < 0)
2778 /* Make the equalities that are implicit in "bmap" but that have been
2779 * detected in the corresponding "tab" explicit in "bmap" and update
2780 * "tab" to reflect the new order of the constraints.
2782 * In particular, if inequality i is an implicit equality then
2783 * isl_basic_map_inequality_to_equality will move the inequality
2784 * in front of the other equality and it will move the last inequality
2785 * in the position of inequality i.
2786 * In the tableau, the inequalities of "bmap" are stored after the equalities
2787 * and so the original order
2789 * E E E E E A A A I B B B B L
2793 * I E E E E E A A A L B B B B
2795 * where I is the implicit equality, the E are equalities,
2796 * the A inequalities before I, the B inequalities after I and
2797 * L the last inequality.
2798 * We therefore need to rotate to the right two sets of constraints,
2799 * those up to and including I and those after I.
2801 * If "tab" contains any constraints that are not in "bmap" then they
2802 * appear after those in "bmap" and they should be left untouched.
2804 * Note that this function leaves "bmap" in a temporary state
2805 * as it does not call isl_basic_map_gauss. Calling this function
2806 * is the responsibility of the caller.
2808 __isl_give isl_basic_map
*isl_tab_make_equalities_explicit(struct isl_tab
*tab
,
2809 __isl_take isl_basic_map
*bmap
)
2814 return isl_basic_map_free(bmap
);
2818 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
2819 if (!isl_tab_is_equality(tab
, bmap
->n_eq
+ i
))
2821 isl_basic_map_inequality_to_equality(bmap
, i
);
2822 if (rotate_constraints(tab
, 0, tab
->n_eq
+ i
+ 1) < 0)
2823 return isl_basic_map_free(bmap
);
2824 if (rotate_constraints(tab
, tab
->n_eq
+ i
+ 1,
2825 bmap
->n_ineq
- i
) < 0)
2826 return isl_basic_map_free(bmap
);
2833 static int con_is_redundant(struct isl_tab
*tab
, struct isl_tab_var
*var
)
2837 if (tab
->rational
) {
2838 int sgn
= sign_of_min(tab
, var
);
2843 int irred
= isl_tab_min_at_most_neg_one(tab
, var
);
2850 /* Check for (near) redundant constraints.
2851 * A constraint is redundant if it is non-negative and if
2852 * its minimal value (temporarily ignoring the non-negativity) is either
2853 * - zero (in case of rational tableaus), or
2854 * - strictly larger than -1 (in case of integer tableaus)
2856 * We first mark all non-redundant and non-dead variables that
2857 * are not frozen and not obviously negatively unbounded.
2858 * Then we iterate over all marked variables if they can attain
2859 * any values smaller than zero or at most negative one.
2860 * If not, we mark the row as being redundant (assuming it hasn't
2861 * been detected as being obviously redundant in the mean time).
2863 int isl_tab_detect_redundant(struct isl_tab
*tab
)
2872 if (tab
->n_redundant
== tab
->n_row
)
2876 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
2877 struct isl_tab_var
*var
= isl_tab_var_from_row(tab
, i
);
2878 var
->marked
= !var
->frozen
&& var
->is_nonneg
;
2882 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
2883 struct isl_tab_var
*var
= var_from_col(tab
, i
);
2884 var
->marked
= !var
->frozen
&& var
->is_nonneg
&&
2885 !min_is_manifestly_unbounded(tab
, var
);
2890 struct isl_tab_var
*var
;
2892 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
2893 var
= isl_tab_var_from_row(tab
, i
);
2897 if (i
== tab
->n_row
) {
2898 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
2899 var
= var_from_col(tab
, i
);
2903 if (i
== tab
->n_col
)
2908 red
= con_is_redundant(tab
, var
);
2911 if (red
&& !var
->is_redundant
)
2912 if (isl_tab_mark_redundant(tab
, var
->index
) < 0)
2914 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
2915 var
= var_from_col(tab
, i
);
2918 if (!min_is_manifestly_unbounded(tab
, var
))
2928 int isl_tab_is_equality(struct isl_tab
*tab
, int con
)
2935 if (tab
->con
[con
].is_zero
)
2937 if (tab
->con
[con
].is_redundant
)
2939 if (!tab
->con
[con
].is_row
)
2940 return tab
->con
[con
].index
< tab
->n_dead
;
2942 row
= tab
->con
[con
].index
;
2945 return isl_int_is_zero(tab
->mat
->row
[row
][1]) &&
2946 (!tab
->M
|| isl_int_is_zero(tab
->mat
->row
[row
][2])) &&
2947 isl_seq_first_non_zero(tab
->mat
->row
[row
] + off
+ tab
->n_dead
,
2948 tab
->n_col
- tab
->n_dead
) == -1;
2951 /* Return the minimal value of the affine expression "f" with denominator
2952 * "denom" in *opt, *opt_denom, assuming the tableau is not empty and
2953 * the expression cannot attain arbitrarily small values.
2954 * If opt_denom is NULL, then *opt is rounded up to the nearest integer.
2955 * The return value reflects the nature of the result (empty, unbounded,
2956 * minimal value returned in *opt).
2958 enum isl_lp_result
isl_tab_min(struct isl_tab
*tab
,
2959 isl_int
*f
, isl_int denom
, isl_int
*opt
, isl_int
*opt_denom
,
2963 enum isl_lp_result res
= isl_lp_ok
;
2964 struct isl_tab_var
*var
;
2965 struct isl_tab_undo
*snap
;
2968 return isl_lp_error
;
2971 return isl_lp_empty
;
2973 snap
= isl_tab_snap(tab
);
2974 r
= isl_tab_add_row(tab
, f
);
2976 return isl_lp_error
;
2980 find_pivot(tab
, var
, var
, -1, &row
, &col
);
2981 if (row
== var
->index
) {
2982 res
= isl_lp_unbounded
;
2987 if (isl_tab_pivot(tab
, row
, col
) < 0)
2988 return isl_lp_error
;
2990 isl_int_mul(tab
->mat
->row
[var
->index
][0],
2991 tab
->mat
->row
[var
->index
][0], denom
);
2992 if (ISL_FL_ISSET(flags
, ISL_TAB_SAVE_DUAL
)) {
2995 isl_vec_free(tab
->dual
);
2996 tab
->dual
= isl_vec_alloc(tab
->mat
->ctx
, 1 + tab
->n_con
);
2998 return isl_lp_error
;
2999 isl_int_set(tab
->dual
->el
[0], tab
->mat
->row
[var
->index
][0]);
3000 for (i
= 0; i
< tab
->n_con
; ++i
) {
3002 if (tab
->con
[i
].is_row
) {
3003 isl_int_set_si(tab
->dual
->el
[1 + i
], 0);
3006 pos
= 2 + tab
->M
+ tab
->con
[i
].index
;
3007 if (tab
->con
[i
].negated
)
3008 isl_int_neg(tab
->dual
->el
[1 + i
],
3009 tab
->mat
->row
[var
->index
][pos
]);
3011 isl_int_set(tab
->dual
->el
[1 + i
],
3012 tab
->mat
->row
[var
->index
][pos
]);
3015 if (opt
&& res
== isl_lp_ok
) {
3017 isl_int_set(*opt
, tab
->mat
->row
[var
->index
][1]);
3018 isl_int_set(*opt_denom
, tab
->mat
->row
[var
->index
][0]);
3020 isl_int_cdiv_q(*opt
, tab
->mat
->row
[var
->index
][1],
3021 tab
->mat
->row
[var
->index
][0]);
3023 if (isl_tab_rollback(tab
, snap
) < 0)
3024 return isl_lp_error
;
3028 int isl_tab_is_redundant(struct isl_tab
*tab
, int con
)
3032 if (tab
->con
[con
].is_zero
)
3034 if (tab
->con
[con
].is_redundant
)
3036 return tab
->con
[con
].is_row
&& tab
->con
[con
].index
< tab
->n_redundant
;
3039 /* Take a snapshot of the tableau that can be restored by s call to
3042 struct isl_tab_undo
*isl_tab_snap(struct isl_tab
*tab
)
3050 /* Undo the operation performed by isl_tab_relax.
3052 static int unrelax(struct isl_tab
*tab
, struct isl_tab_var
*var
) WARN_UNUSED
;
3053 static int unrelax(struct isl_tab
*tab
, struct isl_tab_var
*var
)
3055 unsigned off
= 2 + tab
->M
;
3057 if (!var
->is_row
&& !max_is_manifestly_unbounded(tab
, var
))
3058 if (to_row(tab
, var
, 1) < 0)
3062 isl_int_sub(tab
->mat
->row
[var
->index
][1],
3063 tab
->mat
->row
[var
->index
][1], tab
->mat
->row
[var
->index
][0]);
3064 if (var
->is_nonneg
) {
3065 int sgn
= restore_row(tab
, var
);
3066 isl_assert(tab
->mat
->ctx
, sgn
>= 0, return -1);
3071 for (i
= 0; i
< tab
->n_row
; ++i
) {
3072 if (isl_int_is_zero(tab
->mat
->row
[i
][off
+ var
->index
]))
3074 isl_int_add(tab
->mat
->row
[i
][1], tab
->mat
->row
[i
][1],
3075 tab
->mat
->row
[i
][off
+ var
->index
]);
3083 /* Undo the operation performed by isl_tab_unrestrict.
3085 * In particular, mark the variable as being non-negative and make
3086 * sure the sample value respects this constraint.
3088 static int ununrestrict(struct isl_tab
*tab
, struct isl_tab_var
*var
)
3092 if (var
->is_row
&& restore_row(tab
, var
) < -1)
3098 static int perform_undo_var(struct isl_tab
*tab
, struct isl_tab_undo
*undo
) WARN_UNUSED
;
3099 static int perform_undo_var(struct isl_tab
*tab
, struct isl_tab_undo
*undo
)
3101 struct isl_tab_var
*var
= var_from_index(tab
, undo
->u
.var_index
);
3102 switch (undo
->type
) {
3103 case isl_tab_undo_nonneg
:
3106 case isl_tab_undo_redundant
:
3107 var
->is_redundant
= 0;
3109 restore_row(tab
, isl_tab_var_from_row(tab
, tab
->n_redundant
));
3111 case isl_tab_undo_freeze
:
3114 case isl_tab_undo_zero
:
3119 case isl_tab_undo_allocate
:
3120 if (undo
->u
.var_index
>= 0) {
3121 isl_assert(tab
->mat
->ctx
, !var
->is_row
, return -1);
3122 drop_col(tab
, var
->index
);
3126 if (!max_is_manifestly_unbounded(tab
, var
)) {
3127 if (to_row(tab
, var
, 1) < 0)
3129 } else if (!min_is_manifestly_unbounded(tab
, var
)) {
3130 if (to_row(tab
, var
, -1) < 0)
3133 if (to_row(tab
, var
, 0) < 0)
3136 drop_row(tab
, var
->index
);
3138 case isl_tab_undo_relax
:
3139 return unrelax(tab
, var
);
3140 case isl_tab_undo_unrestrict
:
3141 return ununrestrict(tab
, var
);
3143 isl_die(tab
->mat
->ctx
, isl_error_internal
,
3144 "perform_undo_var called on invalid undo record",
3151 /* Restore the tableau to the state where the basic variables
3152 * are those in "col_var".
3153 * We first construct a list of variables that are currently in
3154 * the basis, but shouldn't. Then we iterate over all variables
3155 * that should be in the basis and for each one that is currently
3156 * not in the basis, we exchange it with one of the elements of the
3157 * list constructed before.
3158 * We can always find an appropriate variable to pivot with because
3159 * the current basis is mapped to the old basis by a non-singular
3160 * matrix and so we can never end up with a zero row.
3162 static int restore_basis(struct isl_tab
*tab
, int *col_var
)
3166 int *extra
= NULL
; /* current columns that contain bad stuff */
3167 unsigned off
= 2 + tab
->M
;
3169 extra
= isl_alloc_array(tab
->mat
->ctx
, int, tab
->n_col
);
3172 for (i
= 0; i
< tab
->n_col
; ++i
) {
3173 for (j
= 0; j
< tab
->n_col
; ++j
)
3174 if (tab
->col_var
[i
] == col_var
[j
])
3178 extra
[n_extra
++] = i
;
3180 for (i
= 0; i
< tab
->n_col
&& n_extra
> 0; ++i
) {
3181 struct isl_tab_var
*var
;
3184 for (j
= 0; j
< tab
->n_col
; ++j
)
3185 if (col_var
[i
] == tab
->col_var
[j
])
3189 var
= var_from_index(tab
, col_var
[i
]);
3191 for (j
= 0; j
< n_extra
; ++j
)
3192 if (!isl_int_is_zero(tab
->mat
->row
[row
][off
+extra
[j
]]))
3194 isl_assert(tab
->mat
->ctx
, j
< n_extra
, goto error
);
3195 if (isl_tab_pivot(tab
, row
, extra
[j
]) < 0)
3197 extra
[j
] = extra
[--n_extra
];
3207 /* Remove all samples with index n or greater, i.e., those samples
3208 * that were added since we saved this number of samples in
3209 * isl_tab_save_samples.
3211 static void drop_samples_since(struct isl_tab
*tab
, int n
)
3215 for (i
= tab
->n_sample
- 1; i
>= 0 && tab
->n_sample
> n
; --i
) {
3216 if (tab
->sample_index
[i
] < n
)
3219 if (i
!= tab
->n_sample
- 1) {
3220 int t
= tab
->sample_index
[tab
->n_sample
-1];
3221 tab
->sample_index
[tab
->n_sample
-1] = tab
->sample_index
[i
];
3222 tab
->sample_index
[i
] = t
;
3223 isl_mat_swap_rows(tab
->samples
, tab
->n_sample
-1, i
);
3229 static int perform_undo(struct isl_tab
*tab
, struct isl_tab_undo
*undo
) WARN_UNUSED
;
3230 static int perform_undo(struct isl_tab
*tab
, struct isl_tab_undo
*undo
)
3232 switch (undo
->type
) {
3233 case isl_tab_undo_empty
:
3236 case isl_tab_undo_nonneg
:
3237 case isl_tab_undo_redundant
:
3238 case isl_tab_undo_freeze
:
3239 case isl_tab_undo_zero
:
3240 case isl_tab_undo_allocate
:
3241 case isl_tab_undo_relax
:
3242 case isl_tab_undo_unrestrict
:
3243 return perform_undo_var(tab
, undo
);
3244 case isl_tab_undo_bmap_eq
:
3245 return isl_basic_map_free_equality(tab
->bmap
, 1);
3246 case isl_tab_undo_bmap_ineq
:
3247 return isl_basic_map_free_inequality(tab
->bmap
, 1);
3248 case isl_tab_undo_bmap_div
:
3249 if (isl_basic_map_free_div(tab
->bmap
, 1) < 0)
3252 tab
->samples
->n_col
--;
3254 case isl_tab_undo_saved_basis
:
3255 if (restore_basis(tab
, undo
->u
.col_var
) < 0)
3258 case isl_tab_undo_drop_sample
:
3261 case isl_tab_undo_saved_samples
:
3262 drop_samples_since(tab
, undo
->u
.n
);
3264 case isl_tab_undo_callback
:
3265 return undo
->u
.callback
->run(undo
->u
.callback
);
3267 isl_assert(tab
->mat
->ctx
, 0, return -1);
3272 /* Return the tableau to the state it was in when the snapshot "snap"
3275 int isl_tab_rollback(struct isl_tab
*tab
, struct isl_tab_undo
*snap
)
3277 struct isl_tab_undo
*undo
, *next
;
3283 for (undo
= tab
->top
; undo
&& undo
!= &tab
->bottom
; undo
= next
) {
3287 if (perform_undo(tab
, undo
) < 0) {
3293 free_undo_record(undo
);
3302 /* The given row "row" represents an inequality violated by all
3303 * points in the tableau. Check for some special cases of such
3304 * separating constraints.
3305 * In particular, if the row has been reduced to the constant -1,
3306 * then we know the inequality is adjacent (but opposite) to
3307 * an equality in the tableau.
3308 * If the row has been reduced to r = c*(-1 -r'), with r' an inequality
3309 * of the tableau and c a positive constant, then the inequality
3310 * is adjacent (but opposite) to the inequality r'.
3312 static enum isl_ineq_type
separation_type(struct isl_tab
*tab
, unsigned row
)
3315 unsigned off
= 2 + tab
->M
;
3318 return isl_ineq_separate
;
3320 if (!isl_int_is_one(tab
->mat
->row
[row
][0]))
3321 return isl_ineq_separate
;
3323 pos
= isl_seq_first_non_zero(tab
->mat
->row
[row
] + off
+ tab
->n_dead
,
3324 tab
->n_col
- tab
->n_dead
);
3326 if (isl_int_is_negone(tab
->mat
->row
[row
][1]))
3327 return isl_ineq_adj_eq
;
3329 return isl_ineq_separate
;
3332 if (!isl_int_eq(tab
->mat
->row
[row
][1],
3333 tab
->mat
->row
[row
][off
+ tab
->n_dead
+ pos
]))
3334 return isl_ineq_separate
;
3336 pos
= isl_seq_first_non_zero(
3337 tab
->mat
->row
[row
] + off
+ tab
->n_dead
+ pos
+ 1,
3338 tab
->n_col
- tab
->n_dead
- pos
- 1);
3340 return pos
== -1 ? isl_ineq_adj_ineq
: isl_ineq_separate
;
3343 /* Check the effect of inequality "ineq" on the tableau "tab".
3345 * isl_ineq_redundant: satisfied by all points in the tableau
3346 * isl_ineq_separate: satisfied by no point in the tableau
3347 * isl_ineq_cut: satisfied by some by not all points
3348 * isl_ineq_adj_eq: adjacent to an equality
3349 * isl_ineq_adj_ineq: adjacent to an inequality.
3351 enum isl_ineq_type
isl_tab_ineq_type(struct isl_tab
*tab
, isl_int
*ineq
)
3353 enum isl_ineq_type type
= isl_ineq_error
;
3354 struct isl_tab_undo
*snap
= NULL
;
3359 return isl_ineq_error
;
3361 if (isl_tab_extend_cons(tab
, 1) < 0)
3362 return isl_ineq_error
;
3364 snap
= isl_tab_snap(tab
);
3366 con
= isl_tab_add_row(tab
, ineq
);
3370 row
= tab
->con
[con
].index
;
3371 if (isl_tab_row_is_redundant(tab
, row
))
3372 type
= isl_ineq_redundant
;
3373 else if (isl_int_is_neg(tab
->mat
->row
[row
][1]) &&
3375 isl_int_abs_ge(tab
->mat
->row
[row
][1],
3376 tab
->mat
->row
[row
][0]))) {
3377 int nonneg
= at_least_zero(tab
, &tab
->con
[con
]);
3381 type
= isl_ineq_cut
;
3383 type
= separation_type(tab
, row
);
3385 int red
= con_is_redundant(tab
, &tab
->con
[con
]);
3389 type
= isl_ineq_cut
;
3391 type
= isl_ineq_redundant
;
3394 if (isl_tab_rollback(tab
, snap
))
3395 return isl_ineq_error
;
3398 return isl_ineq_error
;
3401 int isl_tab_track_bmap(struct isl_tab
*tab
, __isl_take isl_basic_map
*bmap
)
3403 bmap
= isl_basic_map_cow(bmap
);
3408 bmap
= isl_basic_map_set_to_empty(bmap
);
3415 isl_assert(tab
->mat
->ctx
, tab
->n_eq
== bmap
->n_eq
, goto error
);
3416 isl_assert(tab
->mat
->ctx
,
3417 tab
->n_con
== bmap
->n_eq
+ bmap
->n_ineq
, goto error
);
3423 isl_basic_map_free(bmap
);
3427 int isl_tab_track_bset(struct isl_tab
*tab
, __isl_take isl_basic_set
*bset
)
3429 return isl_tab_track_bmap(tab
, (isl_basic_map
*)bset
);
3432 __isl_keep isl_basic_set
*isl_tab_peek_bset(struct isl_tab
*tab
)
3437 return (isl_basic_set
*)tab
->bmap
;
3440 static void isl_tab_print_internal(__isl_keep
struct isl_tab
*tab
,
3441 FILE *out
, int indent
)
3447 fprintf(out
, "%*snull tab\n", indent
, "");
3450 fprintf(out
, "%*sn_redundant: %d, n_dead: %d", indent
, "",
3451 tab
->n_redundant
, tab
->n_dead
);
3453 fprintf(out
, ", rational");
3455 fprintf(out
, ", empty");
3457 fprintf(out
, "%*s[", indent
, "");
3458 for (i
= 0; i
< tab
->n_var
; ++i
) {
3460 fprintf(out
, (i
== tab
->n_param
||
3461 i
== tab
->n_var
- tab
->n_div
) ? "; "
3463 fprintf(out
, "%c%d%s", tab
->var
[i
].is_row
? 'r' : 'c',
3465 tab
->var
[i
].is_zero
? " [=0]" :
3466 tab
->var
[i
].is_redundant
? " [R]" : "");
3468 fprintf(out
, "]\n");
3469 fprintf(out
, "%*s[", indent
, "");
3470 for (i
= 0; i
< tab
->n_con
; ++i
) {
3473 fprintf(out
, "%c%d%s", tab
->con
[i
].is_row
? 'r' : 'c',
3475 tab
->con
[i
].is_zero
? " [=0]" :
3476 tab
->con
[i
].is_redundant
? " [R]" : "");
3478 fprintf(out
, "]\n");
3479 fprintf(out
, "%*s[", indent
, "");
3480 for (i
= 0; i
< tab
->n_row
; ++i
) {
3481 const char *sign
= "";
3484 if (tab
->row_sign
) {
3485 if (tab
->row_sign
[i
] == isl_tab_row_unknown
)
3487 else if (tab
->row_sign
[i
] == isl_tab_row_neg
)
3489 else if (tab
->row_sign
[i
] == isl_tab_row_pos
)
3494 fprintf(out
, "r%d: %d%s%s", i
, tab
->row_var
[i
],
3495 isl_tab_var_from_row(tab
, i
)->is_nonneg
? " [>=0]" : "", sign
);
3497 fprintf(out
, "]\n");
3498 fprintf(out
, "%*s[", indent
, "");
3499 for (i
= 0; i
< tab
->n_col
; ++i
) {
3502 fprintf(out
, "c%d: %d%s", i
, tab
->col_var
[i
],
3503 var_from_col(tab
, i
)->is_nonneg
? " [>=0]" : "");
3505 fprintf(out
, "]\n");
3506 r
= tab
->mat
->n_row
;
3507 tab
->mat
->n_row
= tab
->n_row
;
3508 c
= tab
->mat
->n_col
;
3509 tab
->mat
->n_col
= 2 + tab
->M
+ tab
->n_col
;
3510 isl_mat_print_internal(tab
->mat
, out
, indent
);
3511 tab
->mat
->n_row
= r
;
3512 tab
->mat
->n_col
= c
;
3514 isl_basic_map_print_internal(tab
->bmap
, out
, indent
);
3517 void isl_tab_dump(__isl_keep
struct isl_tab
*tab
)
3519 isl_tab_print_internal(tab
, stderr
, 0);