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_map_private.h"
17 #include <isl_config.h>
20 * The implementation of tableaus in this file was inspired by Section 8
21 * of David Detlefs, Greg Nelson and James B. Saxe, "Simplify: a theorem
22 * prover for program checking".
25 struct isl_tab
*isl_tab_alloc(struct isl_ctx
*ctx
,
26 unsigned n_row
, unsigned n_var
, unsigned M
)
32 tab
= isl_calloc_type(ctx
, struct isl_tab
);
35 tab
->mat
= isl_mat_alloc(ctx
, n_row
, off
+ n_var
);
38 tab
->var
= isl_alloc_array(ctx
, struct isl_tab_var
, n_var
);
39 if (n_var
&& !tab
->var
)
41 tab
->con
= isl_alloc_array(ctx
, struct isl_tab_var
, n_row
);
42 if (n_row
&& !tab
->con
)
44 tab
->col_var
= isl_alloc_array(ctx
, int, n_var
);
45 if (n_var
&& !tab
->col_var
)
47 tab
->row_var
= isl_alloc_array(ctx
, int, n_row
);
48 if (n_row
&& !tab
->row_var
)
50 for (i
= 0; i
< n_var
; ++i
) {
51 tab
->var
[i
].index
= i
;
52 tab
->var
[i
].is_row
= 0;
53 tab
->var
[i
].is_nonneg
= 0;
54 tab
->var
[i
].is_zero
= 0;
55 tab
->var
[i
].is_redundant
= 0;
56 tab
->var
[i
].frozen
= 0;
57 tab
->var
[i
].negated
= 0;
71 tab
->strict_redundant
= 0;
78 tab
->bottom
.type
= isl_tab_undo_bottom
;
79 tab
->bottom
.next
= NULL
;
80 tab
->top
= &tab
->bottom
;
92 isl_ctx
*isl_tab_get_ctx(struct isl_tab
*tab
)
94 return tab
? isl_mat_get_ctx(tab
->mat
) : NULL
;
97 int isl_tab_extend_cons(struct isl_tab
*tab
, unsigned n_new
)
106 if (tab
->max_con
< tab
->n_con
+ n_new
) {
107 struct isl_tab_var
*con
;
109 con
= isl_realloc_array(tab
->mat
->ctx
, tab
->con
,
110 struct isl_tab_var
, tab
->max_con
+ n_new
);
114 tab
->max_con
+= n_new
;
116 if (tab
->mat
->n_row
< tab
->n_row
+ n_new
) {
119 tab
->mat
= isl_mat_extend(tab
->mat
,
120 tab
->n_row
+ n_new
, off
+ tab
->n_col
);
123 row_var
= isl_realloc_array(tab
->mat
->ctx
, tab
->row_var
,
124 int, tab
->mat
->n_row
);
127 tab
->row_var
= row_var
;
129 enum isl_tab_row_sign
*s
;
130 s
= isl_realloc_array(tab
->mat
->ctx
, tab
->row_sign
,
131 enum isl_tab_row_sign
, tab
->mat
->n_row
);
140 /* Make room for at least n_new extra variables.
141 * Return -1 if anything went wrong.
143 int isl_tab_extend_vars(struct isl_tab
*tab
, unsigned n_new
)
145 struct isl_tab_var
*var
;
146 unsigned off
= 2 + tab
->M
;
148 if (tab
->max_var
< tab
->n_var
+ n_new
) {
149 var
= isl_realloc_array(tab
->mat
->ctx
, tab
->var
,
150 struct isl_tab_var
, tab
->n_var
+ n_new
);
154 tab
->max_var
+= n_new
;
157 if (tab
->mat
->n_col
< off
+ tab
->n_col
+ n_new
) {
160 tab
->mat
= isl_mat_extend(tab
->mat
,
161 tab
->mat
->n_row
, off
+ tab
->n_col
+ n_new
);
164 p
= isl_realloc_array(tab
->mat
->ctx
, tab
->col_var
,
165 int, tab
->n_col
+ n_new
);
174 struct isl_tab
*isl_tab_extend(struct isl_tab
*tab
, unsigned n_new
)
176 if (isl_tab_extend_cons(tab
, n_new
) >= 0)
183 static void free_undo_record(struct isl_tab_undo
*undo
)
185 switch (undo
->type
) {
186 case isl_tab_undo_saved_basis
:
187 free(undo
->u
.col_var
);
194 static void free_undo(struct isl_tab
*tab
)
196 struct isl_tab_undo
*undo
, *next
;
198 for (undo
= tab
->top
; undo
&& undo
!= &tab
->bottom
; undo
= next
) {
200 free_undo_record(undo
);
205 void isl_tab_free(struct isl_tab
*tab
)
210 isl_mat_free(tab
->mat
);
211 isl_vec_free(tab
->dual
);
212 isl_basic_map_free(tab
->bmap
);
218 isl_mat_free(tab
->samples
);
219 free(tab
->sample_index
);
220 isl_mat_free(tab
->basis
);
224 struct isl_tab
*isl_tab_dup(struct isl_tab
*tab
)
234 dup
= isl_calloc_type(tab
->mat
->ctx
, struct isl_tab
);
237 dup
->mat
= isl_mat_dup(tab
->mat
);
240 dup
->var
= isl_alloc_array(tab
->mat
->ctx
, struct isl_tab_var
, tab
->max_var
);
241 if (tab
->max_var
&& !dup
->var
)
243 for (i
= 0; i
< tab
->n_var
; ++i
)
244 dup
->var
[i
] = tab
->var
[i
];
245 dup
->con
= isl_alloc_array(tab
->mat
->ctx
, struct isl_tab_var
, tab
->max_con
);
246 if (tab
->max_con
&& !dup
->con
)
248 for (i
= 0; i
< tab
->n_con
; ++i
)
249 dup
->con
[i
] = tab
->con
[i
];
250 dup
->col_var
= isl_alloc_array(tab
->mat
->ctx
, int, tab
->mat
->n_col
- off
);
251 if ((tab
->mat
->n_col
- off
) && !dup
->col_var
)
253 for (i
= 0; i
< tab
->n_col
; ++i
)
254 dup
->col_var
[i
] = tab
->col_var
[i
];
255 dup
->row_var
= isl_alloc_array(tab
->mat
->ctx
, int, tab
->mat
->n_row
);
256 if (tab
->mat
->n_row
&& !dup
->row_var
)
258 for (i
= 0; i
< tab
->n_row
; ++i
)
259 dup
->row_var
[i
] = tab
->row_var
[i
];
261 dup
->row_sign
= isl_alloc_array(tab
->mat
->ctx
, enum isl_tab_row_sign
,
263 if (tab
->mat
->n_row
&& !dup
->row_sign
)
265 for (i
= 0; i
< tab
->n_row
; ++i
)
266 dup
->row_sign
[i
] = tab
->row_sign
[i
];
269 dup
->samples
= isl_mat_dup(tab
->samples
);
272 dup
->sample_index
= isl_alloc_array(tab
->mat
->ctx
, int,
273 tab
->samples
->n_row
);
274 if (tab
->samples
->n_row
&& !dup
->sample_index
)
276 dup
->n_sample
= tab
->n_sample
;
277 dup
->n_outside
= tab
->n_outside
;
279 dup
->n_row
= tab
->n_row
;
280 dup
->n_con
= tab
->n_con
;
281 dup
->n_eq
= tab
->n_eq
;
282 dup
->max_con
= tab
->max_con
;
283 dup
->n_col
= tab
->n_col
;
284 dup
->n_var
= tab
->n_var
;
285 dup
->max_var
= tab
->max_var
;
286 dup
->n_param
= tab
->n_param
;
287 dup
->n_div
= tab
->n_div
;
288 dup
->n_dead
= tab
->n_dead
;
289 dup
->n_redundant
= tab
->n_redundant
;
290 dup
->rational
= tab
->rational
;
291 dup
->empty
= tab
->empty
;
292 dup
->strict_redundant
= 0;
296 tab
->cone
= tab
->cone
;
297 dup
->bottom
.type
= isl_tab_undo_bottom
;
298 dup
->bottom
.next
= NULL
;
299 dup
->top
= &dup
->bottom
;
301 dup
->n_zero
= tab
->n_zero
;
302 dup
->n_unbounded
= tab
->n_unbounded
;
303 dup
->basis
= isl_mat_dup(tab
->basis
);
311 /* Construct the coefficient matrix of the product tableau
313 * mat{1,2} is the coefficient matrix of tableau {1,2}
314 * row{1,2} is the number of rows in tableau {1,2}
315 * col{1,2} is the number of columns in tableau {1,2}
316 * off is the offset to the coefficient column (skipping the
317 * denominator, the constant term and the big parameter if any)
318 * r{1,2} is the number of redundant rows in tableau {1,2}
319 * d{1,2} is the number of dead columns in tableau {1,2}
321 * The order of the rows and columns in the result is as explained
322 * in isl_tab_product.
324 static struct isl_mat
*tab_mat_product(struct isl_mat
*mat1
,
325 struct isl_mat
*mat2
, unsigned row1
, unsigned row2
,
326 unsigned col1
, unsigned col2
,
327 unsigned off
, unsigned r1
, unsigned r2
, unsigned d1
, unsigned d2
)
330 struct isl_mat
*prod
;
333 prod
= isl_mat_alloc(mat1
->ctx
, mat1
->n_row
+ mat2
->n_row
,
339 for (i
= 0; i
< r1
; ++i
) {
340 isl_seq_cpy(prod
->row
[n
+ i
], mat1
->row
[i
], off
+ d1
);
341 isl_seq_clr(prod
->row
[n
+ i
] + off
+ d1
, d2
);
342 isl_seq_cpy(prod
->row
[n
+ i
] + off
+ d1
+ d2
,
343 mat1
->row
[i
] + off
+ d1
, col1
- d1
);
344 isl_seq_clr(prod
->row
[n
+ i
] + off
+ col1
+ d1
, col2
- d2
);
348 for (i
= 0; i
< r2
; ++i
) {
349 isl_seq_cpy(prod
->row
[n
+ i
], mat2
->row
[i
], off
);
350 isl_seq_clr(prod
->row
[n
+ i
] + off
, d1
);
351 isl_seq_cpy(prod
->row
[n
+ i
] + off
+ d1
,
352 mat2
->row
[i
] + off
, d2
);
353 isl_seq_clr(prod
->row
[n
+ i
] + off
+ d1
+ d2
, col1
- d1
);
354 isl_seq_cpy(prod
->row
[n
+ i
] + off
+ col1
+ d1
,
355 mat2
->row
[i
] + off
+ d2
, col2
- d2
);
359 for (i
= 0; i
< row1
- r1
; ++i
) {
360 isl_seq_cpy(prod
->row
[n
+ i
], mat1
->row
[r1
+ i
], off
+ d1
);
361 isl_seq_clr(prod
->row
[n
+ i
] + off
+ d1
, d2
);
362 isl_seq_cpy(prod
->row
[n
+ i
] + off
+ d1
+ d2
,
363 mat1
->row
[r1
+ i
] + off
+ d1
, col1
- d1
);
364 isl_seq_clr(prod
->row
[n
+ i
] + off
+ col1
+ d1
, col2
- d2
);
368 for (i
= 0; i
< row2
- r2
; ++i
) {
369 isl_seq_cpy(prod
->row
[n
+ i
], mat2
->row
[r2
+ i
], off
);
370 isl_seq_clr(prod
->row
[n
+ i
] + off
, d1
);
371 isl_seq_cpy(prod
->row
[n
+ i
] + off
+ d1
,
372 mat2
->row
[r2
+ i
] + off
, d2
);
373 isl_seq_clr(prod
->row
[n
+ i
] + off
+ d1
+ d2
, col1
- d1
);
374 isl_seq_cpy(prod
->row
[n
+ i
] + off
+ col1
+ d1
,
375 mat2
->row
[r2
+ i
] + off
+ d2
, col2
- d2
);
381 /* Update the row or column index of a variable that corresponds
382 * to a variable in the first input tableau.
384 static void update_index1(struct isl_tab_var
*var
,
385 unsigned r1
, unsigned r2
, unsigned d1
, unsigned d2
)
387 if (var
->index
== -1)
389 if (var
->is_row
&& var
->index
>= r1
)
391 if (!var
->is_row
&& var
->index
>= d1
)
395 /* Update the row or column index of a variable that corresponds
396 * to a variable in the second input tableau.
398 static void update_index2(struct isl_tab_var
*var
,
399 unsigned row1
, unsigned col1
,
400 unsigned r1
, unsigned r2
, unsigned d1
, unsigned d2
)
402 if (var
->index
== -1)
417 /* Create a tableau that represents the Cartesian product of the sets
418 * represented by tableaus tab1 and tab2.
419 * The order of the rows in the product is
420 * - redundant rows of tab1
421 * - redundant rows of tab2
422 * - non-redundant rows of tab1
423 * - non-redundant rows of tab2
424 * The order of the columns is
427 * - coefficient of big parameter, if any
428 * - dead columns of tab1
429 * - dead columns of tab2
430 * - live columns of tab1
431 * - live columns of tab2
432 * The order of the variables and the constraints is a concatenation
433 * of order in the two input tableaus.
435 struct isl_tab
*isl_tab_product(struct isl_tab
*tab1
, struct isl_tab
*tab2
)
438 struct isl_tab
*prod
;
440 unsigned r1
, r2
, d1
, d2
;
445 isl_assert(tab1
->mat
->ctx
, tab1
->M
== tab2
->M
, return NULL
);
446 isl_assert(tab1
->mat
->ctx
, tab1
->rational
== tab2
->rational
, return NULL
);
447 isl_assert(tab1
->mat
->ctx
, tab1
->cone
== tab2
->cone
, return NULL
);
448 isl_assert(tab1
->mat
->ctx
, !tab1
->row_sign
, return NULL
);
449 isl_assert(tab1
->mat
->ctx
, !tab2
->row_sign
, return NULL
);
450 isl_assert(tab1
->mat
->ctx
, tab1
->n_param
== 0, return NULL
);
451 isl_assert(tab1
->mat
->ctx
, tab2
->n_param
== 0, return NULL
);
452 isl_assert(tab1
->mat
->ctx
, tab1
->n_div
== 0, return NULL
);
453 isl_assert(tab1
->mat
->ctx
, tab2
->n_div
== 0, return NULL
);
456 r1
= tab1
->n_redundant
;
457 r2
= tab2
->n_redundant
;
460 prod
= isl_calloc_type(tab1
->mat
->ctx
, struct isl_tab
);
463 prod
->mat
= tab_mat_product(tab1
->mat
, tab2
->mat
,
464 tab1
->n_row
, tab2
->n_row
,
465 tab1
->n_col
, tab2
->n_col
, off
, r1
, r2
, d1
, d2
);
468 prod
->var
= isl_alloc_array(tab1
->mat
->ctx
, struct isl_tab_var
,
469 tab1
->max_var
+ tab2
->max_var
);
470 if ((tab1
->max_var
+ tab2
->max_var
) && !prod
->var
)
472 for (i
= 0; i
< tab1
->n_var
; ++i
) {
473 prod
->var
[i
] = tab1
->var
[i
];
474 update_index1(&prod
->var
[i
], r1
, r2
, d1
, d2
);
476 for (i
= 0; i
< tab2
->n_var
; ++i
) {
477 prod
->var
[tab1
->n_var
+ i
] = tab2
->var
[i
];
478 update_index2(&prod
->var
[tab1
->n_var
+ i
],
479 tab1
->n_row
, tab1
->n_col
,
482 prod
->con
= isl_alloc_array(tab1
->mat
->ctx
, struct isl_tab_var
,
483 tab1
->max_con
+ tab2
->max_con
);
484 if ((tab1
->max_con
+ tab2
->max_con
) && !prod
->con
)
486 for (i
= 0; i
< tab1
->n_con
; ++i
) {
487 prod
->con
[i
] = tab1
->con
[i
];
488 update_index1(&prod
->con
[i
], r1
, r2
, d1
, d2
);
490 for (i
= 0; i
< tab2
->n_con
; ++i
) {
491 prod
->con
[tab1
->n_con
+ i
] = tab2
->con
[i
];
492 update_index2(&prod
->con
[tab1
->n_con
+ i
],
493 tab1
->n_row
, tab1
->n_col
,
496 prod
->col_var
= isl_alloc_array(tab1
->mat
->ctx
, int,
497 tab1
->n_col
+ tab2
->n_col
);
498 if ((tab1
->n_col
+ tab2
->n_col
) && !prod
->col_var
)
500 for (i
= 0; i
< tab1
->n_col
; ++i
) {
501 int pos
= i
< d1
? i
: i
+ d2
;
502 prod
->col_var
[pos
] = tab1
->col_var
[i
];
504 for (i
= 0; i
< tab2
->n_col
; ++i
) {
505 int pos
= i
< d2
? d1
+ i
: tab1
->n_col
+ i
;
506 int t
= tab2
->col_var
[i
];
511 prod
->col_var
[pos
] = t
;
513 prod
->row_var
= isl_alloc_array(tab1
->mat
->ctx
, int,
514 tab1
->mat
->n_row
+ tab2
->mat
->n_row
);
515 if ((tab1
->mat
->n_row
+ tab2
->mat
->n_row
) && !prod
->row_var
)
517 for (i
= 0; i
< tab1
->n_row
; ++i
) {
518 int pos
= i
< r1
? i
: i
+ r2
;
519 prod
->row_var
[pos
] = tab1
->row_var
[i
];
521 for (i
= 0; i
< tab2
->n_row
; ++i
) {
522 int pos
= i
< r2
? r1
+ i
: tab1
->n_row
+ i
;
523 int t
= tab2
->row_var
[i
];
528 prod
->row_var
[pos
] = t
;
530 prod
->samples
= NULL
;
531 prod
->sample_index
= NULL
;
532 prod
->n_row
= tab1
->n_row
+ tab2
->n_row
;
533 prod
->n_con
= tab1
->n_con
+ tab2
->n_con
;
535 prod
->max_con
= tab1
->max_con
+ tab2
->max_con
;
536 prod
->n_col
= tab1
->n_col
+ tab2
->n_col
;
537 prod
->n_var
= tab1
->n_var
+ tab2
->n_var
;
538 prod
->max_var
= tab1
->max_var
+ tab2
->max_var
;
541 prod
->n_dead
= tab1
->n_dead
+ tab2
->n_dead
;
542 prod
->n_redundant
= tab1
->n_redundant
+ tab2
->n_redundant
;
543 prod
->rational
= tab1
->rational
;
544 prod
->empty
= tab1
->empty
|| tab2
->empty
;
545 prod
->strict_redundant
= tab1
->strict_redundant
|| tab2
->strict_redundant
;
549 prod
->cone
= tab1
->cone
;
550 prod
->bottom
.type
= isl_tab_undo_bottom
;
551 prod
->bottom
.next
= NULL
;
552 prod
->top
= &prod
->bottom
;
555 prod
->n_unbounded
= 0;
564 static struct isl_tab_var
*var_from_index(struct isl_tab
*tab
, int i
)
569 return &tab
->con
[~i
];
572 struct isl_tab_var
*isl_tab_var_from_row(struct isl_tab
*tab
, int i
)
574 return var_from_index(tab
, tab
->row_var
[i
]);
577 static struct isl_tab_var
*var_from_col(struct isl_tab
*tab
, int i
)
579 return var_from_index(tab
, tab
->col_var
[i
]);
582 /* Check if there are any upper bounds on column variable "var",
583 * i.e., non-negative rows where var appears with a negative coefficient.
584 * Return 1 if there are no such bounds.
586 static int max_is_manifestly_unbounded(struct isl_tab
*tab
,
587 struct isl_tab_var
*var
)
590 unsigned off
= 2 + tab
->M
;
594 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
595 if (!isl_int_is_neg(tab
->mat
->row
[i
][off
+ var
->index
]))
597 if (isl_tab_var_from_row(tab
, i
)->is_nonneg
)
603 /* Check if there are any lower bounds on column variable "var",
604 * i.e., non-negative rows where var appears with a positive coefficient.
605 * Return 1 if there are no such bounds.
607 static int min_is_manifestly_unbounded(struct isl_tab
*tab
,
608 struct isl_tab_var
*var
)
611 unsigned off
= 2 + tab
->M
;
615 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
616 if (!isl_int_is_pos(tab
->mat
->row
[i
][off
+ var
->index
]))
618 if (isl_tab_var_from_row(tab
, i
)->is_nonneg
)
624 static int row_cmp(struct isl_tab
*tab
, int r1
, int r2
, int c
, isl_int t
)
626 unsigned off
= 2 + tab
->M
;
630 isl_int_mul(t
, tab
->mat
->row
[r1
][2], tab
->mat
->row
[r2
][off
+c
]);
631 isl_int_submul(t
, tab
->mat
->row
[r2
][2], tab
->mat
->row
[r1
][off
+c
]);
636 isl_int_mul(t
, tab
->mat
->row
[r1
][1], tab
->mat
->row
[r2
][off
+ c
]);
637 isl_int_submul(t
, tab
->mat
->row
[r2
][1], tab
->mat
->row
[r1
][off
+ c
]);
638 return isl_int_sgn(t
);
641 /* Given the index of a column "c", return the index of a row
642 * that can be used to pivot the column in, with either an increase
643 * (sgn > 0) or a decrease (sgn < 0) of the corresponding variable.
644 * If "var" is not NULL, then the row returned will be different from
645 * the one associated with "var".
647 * Each row in the tableau is of the form
649 * x_r = a_r0 + \sum_i a_ri x_i
651 * Only rows with x_r >= 0 and with the sign of a_ri opposite to "sgn"
652 * impose any limit on the increase or decrease in the value of x_c
653 * and this bound is equal to a_r0 / |a_rc|. We are therefore looking
654 * for the row with the smallest (most stringent) such bound.
655 * Note that the common denominator of each row drops out of the fraction.
656 * To check if row j has a smaller bound than row r, i.e.,
657 * a_j0 / |a_jc| < a_r0 / |a_rc| or a_j0 |a_rc| < a_r0 |a_jc|,
658 * we check if -sign(a_jc) (a_j0 a_rc - a_r0 a_jc) < 0,
659 * where -sign(a_jc) is equal to "sgn".
661 static int pivot_row(struct isl_tab
*tab
,
662 struct isl_tab_var
*var
, int sgn
, int c
)
666 unsigned off
= 2 + tab
->M
;
670 for (j
= tab
->n_redundant
; j
< tab
->n_row
; ++j
) {
671 if (var
&& j
== var
->index
)
673 if (!isl_tab_var_from_row(tab
, j
)->is_nonneg
)
675 if (sgn
* isl_int_sgn(tab
->mat
->row
[j
][off
+ c
]) >= 0)
681 tsgn
= sgn
* row_cmp(tab
, r
, j
, c
, t
);
682 if (tsgn
< 0 || (tsgn
== 0 &&
683 tab
->row_var
[j
] < tab
->row_var
[r
]))
690 /* Find a pivot (row and col) that will increase (sgn > 0) or decrease
691 * (sgn < 0) the value of row variable var.
692 * If not NULL, then skip_var is a row variable that should be ignored
693 * while looking for a pivot row. It is usually equal to var.
695 * As the given row in the tableau is of the form
697 * x_r = a_r0 + \sum_i a_ri x_i
699 * we need to find a column such that the sign of a_ri is equal to "sgn"
700 * (such that an increase in x_i will have the desired effect) or a
701 * column with a variable that may attain negative values.
702 * If a_ri is positive, then we need to move x_i in the same direction
703 * to obtain the desired effect. Otherwise, x_i has to move in the
704 * opposite direction.
706 static void find_pivot(struct isl_tab
*tab
,
707 struct isl_tab_var
*var
, struct isl_tab_var
*skip_var
,
708 int sgn
, int *row
, int *col
)
715 isl_assert(tab
->mat
->ctx
, var
->is_row
, return);
716 tr
= tab
->mat
->row
[var
->index
] + 2 + tab
->M
;
719 for (j
= tab
->n_dead
; j
< tab
->n_col
; ++j
) {
720 if (isl_int_is_zero(tr
[j
]))
722 if (isl_int_sgn(tr
[j
]) != sgn
&&
723 var_from_col(tab
, j
)->is_nonneg
)
725 if (c
< 0 || tab
->col_var
[j
] < tab
->col_var
[c
])
731 sgn
*= isl_int_sgn(tr
[c
]);
732 r
= pivot_row(tab
, skip_var
, sgn
, c
);
733 *row
= r
< 0 ? var
->index
: r
;
737 /* Return 1 if row "row" represents an obviously redundant inequality.
739 * - it represents an inequality or a variable
740 * - that is the sum of a non-negative sample value and a positive
741 * combination of zero or more non-negative constraints.
743 int isl_tab_row_is_redundant(struct isl_tab
*tab
, int row
)
746 unsigned off
= 2 + tab
->M
;
748 if (tab
->row_var
[row
] < 0 && !isl_tab_var_from_row(tab
, row
)->is_nonneg
)
751 if (isl_int_is_neg(tab
->mat
->row
[row
][1]))
753 if (tab
->strict_redundant
&& isl_int_is_zero(tab
->mat
->row
[row
][1]))
755 if (tab
->M
&& isl_int_is_neg(tab
->mat
->row
[row
][2]))
758 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
759 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ i
]))
761 if (tab
->col_var
[i
] >= 0)
763 if (isl_int_is_neg(tab
->mat
->row
[row
][off
+ i
]))
765 if (!var_from_col(tab
, i
)->is_nonneg
)
771 static void swap_rows(struct isl_tab
*tab
, int row1
, int row2
)
774 enum isl_tab_row_sign s
;
776 t
= tab
->row_var
[row1
];
777 tab
->row_var
[row1
] = tab
->row_var
[row2
];
778 tab
->row_var
[row2
] = t
;
779 isl_tab_var_from_row(tab
, row1
)->index
= row1
;
780 isl_tab_var_from_row(tab
, row2
)->index
= row2
;
781 tab
->mat
= isl_mat_swap_rows(tab
->mat
, row1
, row2
);
785 s
= tab
->row_sign
[row1
];
786 tab
->row_sign
[row1
] = tab
->row_sign
[row2
];
787 tab
->row_sign
[row2
] = s
;
790 static int push_union(struct isl_tab
*tab
,
791 enum isl_tab_undo_type type
, union isl_tab_undo_val u
) WARN_UNUSED
;
792 static int push_union(struct isl_tab
*tab
,
793 enum isl_tab_undo_type type
, union isl_tab_undo_val u
)
795 struct isl_tab_undo
*undo
;
802 undo
= isl_alloc_type(tab
->mat
->ctx
, struct isl_tab_undo
);
807 undo
->next
= tab
->top
;
813 int isl_tab_push_var(struct isl_tab
*tab
,
814 enum isl_tab_undo_type type
, struct isl_tab_var
*var
)
816 union isl_tab_undo_val u
;
818 u
.var_index
= tab
->row_var
[var
->index
];
820 u
.var_index
= tab
->col_var
[var
->index
];
821 return push_union(tab
, type
, u
);
824 int isl_tab_push(struct isl_tab
*tab
, enum isl_tab_undo_type type
)
826 union isl_tab_undo_val u
= { 0 };
827 return push_union(tab
, type
, u
);
830 /* Push a record on the undo stack describing the current basic
831 * variables, so that the this state can be restored during rollback.
833 int isl_tab_push_basis(struct isl_tab
*tab
)
836 union isl_tab_undo_val u
;
838 u
.col_var
= isl_alloc_array(tab
->mat
->ctx
, int, tab
->n_col
);
839 if (tab
->n_col
&& !u
.col_var
)
841 for (i
= 0; i
< tab
->n_col
; ++i
)
842 u
.col_var
[i
] = tab
->col_var
[i
];
843 return push_union(tab
, isl_tab_undo_saved_basis
, u
);
846 int isl_tab_push_callback(struct isl_tab
*tab
, struct isl_tab_callback
*callback
)
848 union isl_tab_undo_val u
;
849 u
.callback
= callback
;
850 return push_union(tab
, isl_tab_undo_callback
, u
);
853 struct isl_tab
*isl_tab_init_samples(struct isl_tab
*tab
)
860 tab
->samples
= isl_mat_alloc(tab
->mat
->ctx
, 1, 1 + tab
->n_var
);
863 tab
->sample_index
= isl_alloc_array(tab
->mat
->ctx
, int, 1);
864 if (!tab
->sample_index
)
872 int isl_tab_add_sample(struct isl_tab
*tab
, __isl_take isl_vec
*sample
)
877 if (tab
->n_sample
+ 1 > tab
->samples
->n_row
) {
878 int *t
= isl_realloc_array(tab
->mat
->ctx
,
879 tab
->sample_index
, int, tab
->n_sample
+ 1);
882 tab
->sample_index
= t
;
885 tab
->samples
= isl_mat_extend(tab
->samples
,
886 tab
->n_sample
+ 1, tab
->samples
->n_col
);
890 isl_seq_cpy(tab
->samples
->row
[tab
->n_sample
], sample
->el
, sample
->size
);
891 isl_vec_free(sample
);
892 tab
->sample_index
[tab
->n_sample
] = tab
->n_sample
;
897 isl_vec_free(sample
);
901 struct isl_tab
*isl_tab_drop_sample(struct isl_tab
*tab
, int s
)
903 if (s
!= tab
->n_outside
) {
904 int t
= tab
->sample_index
[tab
->n_outside
];
905 tab
->sample_index
[tab
->n_outside
] = tab
->sample_index
[s
];
906 tab
->sample_index
[s
] = t
;
907 isl_mat_swap_rows(tab
->samples
, tab
->n_outside
, s
);
910 if (isl_tab_push(tab
, isl_tab_undo_drop_sample
) < 0) {
918 /* Record the current number of samples so that we can remove newer
919 * samples during a rollback.
921 int isl_tab_save_samples(struct isl_tab
*tab
)
923 union isl_tab_undo_val u
;
929 return push_union(tab
, isl_tab_undo_saved_samples
, u
);
932 /* Mark row with index "row" as being redundant.
933 * If we may need to undo the operation or if the row represents
934 * a variable of the original problem, the row is kept,
935 * but no longer considered when looking for a pivot row.
936 * Otherwise, the row is simply removed.
938 * The row may be interchanged with some other row. If it
939 * is interchanged with a later row, return 1. Otherwise return 0.
940 * If the rows are checked in order in the calling function,
941 * then a return value of 1 means that the row with the given
942 * row number may now contain a different row that hasn't been checked yet.
944 int isl_tab_mark_redundant(struct isl_tab
*tab
, int row
)
946 struct isl_tab_var
*var
= isl_tab_var_from_row(tab
, row
);
947 var
->is_redundant
= 1;
948 isl_assert(tab
->mat
->ctx
, row
>= tab
->n_redundant
, return -1);
949 if (tab
->preserve
|| tab
->need_undo
|| tab
->row_var
[row
] >= 0) {
950 if (tab
->row_var
[row
] >= 0 && !var
->is_nonneg
) {
952 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, var
) < 0)
955 if (row
!= tab
->n_redundant
)
956 swap_rows(tab
, row
, tab
->n_redundant
);
958 return isl_tab_push_var(tab
, isl_tab_undo_redundant
, var
);
960 if (row
!= tab
->n_row
- 1)
961 swap_rows(tab
, row
, tab
->n_row
- 1);
962 isl_tab_var_from_row(tab
, tab
->n_row
- 1)->index
= -1;
968 int isl_tab_mark_empty(struct isl_tab
*tab
)
972 if (!tab
->empty
&& tab
->need_undo
)
973 if (isl_tab_push(tab
, isl_tab_undo_empty
) < 0)
979 int isl_tab_freeze_constraint(struct isl_tab
*tab
, int con
)
981 struct isl_tab_var
*var
;
986 var
= &tab
->con
[con
];
994 return isl_tab_push_var(tab
, isl_tab_undo_freeze
, var
);
999 /* Update the rows signs after a pivot of "row" and "col", with "row_sgn"
1000 * the original sign of the pivot element.
1001 * We only keep track of row signs during PILP solving and in this case
1002 * we only pivot a row with negative sign (meaning the value is always
1003 * non-positive) using a positive pivot element.
1005 * For each row j, the new value of the parametric constant is equal to
1007 * a_j0 - a_jc a_r0/a_rc
1009 * where a_j0 is the original parametric constant, a_rc is the pivot element,
1010 * a_r0 is the parametric constant of the pivot row and a_jc is the
1011 * pivot column entry of the row j.
1012 * Since a_r0 is non-positive and a_rc is positive, the sign of row j
1013 * remains the same if a_jc has the same sign as the row j or if
1014 * a_jc is zero. In all other cases, we reset the sign to "unknown".
1016 static void update_row_sign(struct isl_tab
*tab
, int row
, int col
, int row_sgn
)
1019 struct isl_mat
*mat
= tab
->mat
;
1020 unsigned off
= 2 + tab
->M
;
1025 if (tab
->row_sign
[row
] == 0)
1027 isl_assert(mat
->ctx
, row_sgn
> 0, return);
1028 isl_assert(mat
->ctx
, tab
->row_sign
[row
] == isl_tab_row_neg
, return);
1029 tab
->row_sign
[row
] = isl_tab_row_pos
;
1030 for (i
= 0; i
< tab
->n_row
; ++i
) {
1034 s
= isl_int_sgn(mat
->row
[i
][off
+ col
]);
1037 if (!tab
->row_sign
[i
])
1039 if (s
< 0 && tab
->row_sign
[i
] == isl_tab_row_neg
)
1041 if (s
> 0 && tab
->row_sign
[i
] == isl_tab_row_pos
)
1043 tab
->row_sign
[i
] = isl_tab_row_unknown
;
1047 /* Given a row number "row" and a column number "col", pivot the tableau
1048 * such that the associated variables are interchanged.
1049 * The given row in the tableau expresses
1051 * x_r = a_r0 + \sum_i a_ri x_i
1055 * x_c = 1/a_rc x_r - a_r0/a_rc + sum_{i \ne r} -a_ri/a_rc
1057 * Substituting this equality into the other rows
1059 * x_j = a_j0 + \sum_i a_ji x_i
1061 * with a_jc \ne 0, we obtain
1063 * 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
1070 * where i is any other column and j is any other row,
1071 * is therefore transformed into
1073 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1074 * 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)
1076 * The transformation is performed along the following steps
1078 * d_r/n_rc n_ri/n_rc
1081 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1084 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1085 * n_jc/(|n_rc| d_j) n_ji/(|n_rc| d_j)
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|)/(|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| - s(n_rc)n_jc n_ri)/(|n_rc| d_j)
1093 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1094 * 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)
1097 int isl_tab_pivot(struct isl_tab
*tab
, int row
, int col
)
1102 struct isl_mat
*mat
= tab
->mat
;
1103 struct isl_tab_var
*var
;
1104 unsigned off
= 2 + tab
->M
;
1106 if (tab
->mat
->ctx
->abort
) {
1107 isl_ctx_set_error(tab
->mat
->ctx
, isl_error_abort
);
1111 isl_int_swap(mat
->row
[row
][0], mat
->row
[row
][off
+ col
]);
1112 sgn
= isl_int_sgn(mat
->row
[row
][0]);
1114 isl_int_neg(mat
->row
[row
][0], mat
->row
[row
][0]);
1115 isl_int_neg(mat
->row
[row
][off
+ col
], mat
->row
[row
][off
+ col
]);
1117 for (j
= 0; j
< off
- 1 + tab
->n_col
; ++j
) {
1118 if (j
== off
- 1 + col
)
1120 isl_int_neg(mat
->row
[row
][1 + j
], mat
->row
[row
][1 + j
]);
1122 if (!isl_int_is_one(mat
->row
[row
][0]))
1123 isl_seq_normalize(mat
->ctx
, mat
->row
[row
], off
+ tab
->n_col
);
1124 for (i
= 0; i
< tab
->n_row
; ++i
) {
1127 if (isl_int_is_zero(mat
->row
[i
][off
+ col
]))
1129 isl_int_mul(mat
->row
[i
][0], mat
->row
[i
][0], mat
->row
[row
][0]);
1130 for (j
= 0; j
< off
- 1 + tab
->n_col
; ++j
) {
1131 if (j
== off
- 1 + col
)
1133 isl_int_mul(mat
->row
[i
][1 + j
],
1134 mat
->row
[i
][1 + j
], mat
->row
[row
][0]);
1135 isl_int_addmul(mat
->row
[i
][1 + j
],
1136 mat
->row
[i
][off
+ col
], mat
->row
[row
][1 + j
]);
1138 isl_int_mul(mat
->row
[i
][off
+ col
],
1139 mat
->row
[i
][off
+ col
], mat
->row
[row
][off
+ col
]);
1140 if (!isl_int_is_one(mat
->row
[i
][0]))
1141 isl_seq_normalize(mat
->ctx
, mat
->row
[i
], off
+ tab
->n_col
);
1143 t
= tab
->row_var
[row
];
1144 tab
->row_var
[row
] = tab
->col_var
[col
];
1145 tab
->col_var
[col
] = t
;
1146 var
= isl_tab_var_from_row(tab
, row
);
1149 var
= var_from_col(tab
, col
);
1152 update_row_sign(tab
, row
, col
, sgn
);
1155 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
1156 if (isl_int_is_zero(mat
->row
[i
][off
+ col
]))
1158 if (!isl_tab_var_from_row(tab
, i
)->frozen
&&
1159 isl_tab_row_is_redundant(tab
, i
)) {
1160 int redo
= isl_tab_mark_redundant(tab
, i
);
1170 /* If "var" represents a column variable, then pivot is up (sgn > 0)
1171 * or down (sgn < 0) to a row. The variable is assumed not to be
1172 * unbounded in the specified direction.
1173 * If sgn = 0, then the variable is unbounded in both directions,
1174 * and we pivot with any row we can find.
1176 static int to_row(struct isl_tab
*tab
, struct isl_tab_var
*var
, int sign
) WARN_UNUSED
;
1177 static int to_row(struct isl_tab
*tab
, struct isl_tab_var
*var
, int sign
)
1180 unsigned off
= 2 + tab
->M
;
1186 for (r
= tab
->n_redundant
; r
< tab
->n_row
; ++r
)
1187 if (!isl_int_is_zero(tab
->mat
->row
[r
][off
+var
->index
]))
1189 isl_assert(tab
->mat
->ctx
, r
< tab
->n_row
, return -1);
1191 r
= pivot_row(tab
, NULL
, sign
, var
->index
);
1192 isl_assert(tab
->mat
->ctx
, r
>= 0, return -1);
1195 return isl_tab_pivot(tab
, r
, var
->index
);
1198 /* Check whether all variables that are marked as non-negative
1199 * also have a non-negative sample value. This function is not
1200 * called from the current code but is useful during debugging.
1202 static void check_table(struct isl_tab
*tab
) __attribute__ ((unused
));
1203 static void check_table(struct isl_tab
*tab
)
1209 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
1210 struct isl_tab_var
*var
;
1211 var
= isl_tab_var_from_row(tab
, i
);
1212 if (!var
->is_nonneg
)
1215 isl_assert(tab
->mat
->ctx
,
1216 !isl_int_is_neg(tab
->mat
->row
[i
][2]), abort());
1217 if (isl_int_is_pos(tab
->mat
->row
[i
][2]))
1220 isl_assert(tab
->mat
->ctx
, !isl_int_is_neg(tab
->mat
->row
[i
][1]),
1225 /* Return the sign of the maximal value of "var".
1226 * If the sign is not negative, then on return from this function,
1227 * the sample value will also be non-negative.
1229 * If "var" is manifestly unbounded wrt positive values, we are done.
1230 * Otherwise, we pivot the variable up to a row if needed
1231 * Then we continue pivoting down until either
1232 * - no more down pivots can be performed
1233 * - the sample value is positive
1234 * - the variable is pivoted into a manifestly unbounded column
1236 static int sign_of_max(struct isl_tab
*tab
, struct isl_tab_var
*var
)
1240 if (max_is_manifestly_unbounded(tab
, var
))
1242 if (to_row(tab
, var
, 1) < 0)
1244 while (!isl_int_is_pos(tab
->mat
->row
[var
->index
][1])) {
1245 find_pivot(tab
, var
, var
, 1, &row
, &col
);
1247 return isl_int_sgn(tab
->mat
->row
[var
->index
][1]);
1248 if (isl_tab_pivot(tab
, row
, col
) < 0)
1250 if (!var
->is_row
) /* manifestly unbounded */
1256 int isl_tab_sign_of_max(struct isl_tab
*tab
, int con
)
1258 struct isl_tab_var
*var
;
1263 var
= &tab
->con
[con
];
1264 isl_assert(tab
->mat
->ctx
, !var
->is_redundant
, return -2);
1265 isl_assert(tab
->mat
->ctx
, !var
->is_zero
, return -2);
1267 return sign_of_max(tab
, var
);
1270 static int row_is_neg(struct isl_tab
*tab
, int row
)
1273 return isl_int_is_neg(tab
->mat
->row
[row
][1]);
1274 if (isl_int_is_pos(tab
->mat
->row
[row
][2]))
1276 if (isl_int_is_neg(tab
->mat
->row
[row
][2]))
1278 return isl_int_is_neg(tab
->mat
->row
[row
][1]);
1281 static int row_sgn(struct isl_tab
*tab
, int row
)
1284 return isl_int_sgn(tab
->mat
->row
[row
][1]);
1285 if (!isl_int_is_zero(tab
->mat
->row
[row
][2]))
1286 return isl_int_sgn(tab
->mat
->row
[row
][2]);
1288 return isl_int_sgn(tab
->mat
->row
[row
][1]);
1291 /* Perform pivots until the row variable "var" has a non-negative
1292 * sample value or until no more upward pivots can be performed.
1293 * Return the sign of the sample value after the pivots have been
1296 static int restore_row(struct isl_tab
*tab
, struct isl_tab_var
*var
)
1300 while (row_is_neg(tab
, var
->index
)) {
1301 find_pivot(tab
, var
, var
, 1, &row
, &col
);
1304 if (isl_tab_pivot(tab
, row
, col
) < 0)
1306 if (!var
->is_row
) /* manifestly unbounded */
1309 return row_sgn(tab
, var
->index
);
1312 /* Perform pivots until we are sure that the row variable "var"
1313 * can attain non-negative values. After return from this
1314 * function, "var" is still a row variable, but its sample
1315 * value may not be non-negative, even if the function returns 1.
1317 static int at_least_zero(struct isl_tab
*tab
, struct isl_tab_var
*var
)
1321 while (isl_int_is_neg(tab
->mat
->row
[var
->index
][1])) {
1322 find_pivot(tab
, var
, var
, 1, &row
, &col
);
1325 if (row
== var
->index
) /* manifestly unbounded */
1327 if (isl_tab_pivot(tab
, row
, col
) < 0)
1330 return !isl_int_is_neg(tab
->mat
->row
[var
->index
][1]);
1333 /* Return a negative value if "var" can attain negative values.
1334 * Return a non-negative value otherwise.
1336 * If "var" is manifestly unbounded wrt negative values, we are done.
1337 * Otherwise, if var is in a column, we can pivot it down to a row.
1338 * Then we continue pivoting down until either
1339 * - the pivot would result in a manifestly unbounded column
1340 * => we don't perform the pivot, but simply return -1
1341 * - no more down pivots can be performed
1342 * - the sample value is negative
1343 * If the sample value becomes negative and the variable is supposed
1344 * to be nonnegative, then we undo the last pivot.
1345 * However, if the last pivot has made the pivoting variable
1346 * obviously redundant, then it may have moved to another row.
1347 * In that case we look for upward pivots until we reach a non-negative
1350 static int sign_of_min(struct isl_tab
*tab
, struct isl_tab_var
*var
)
1353 struct isl_tab_var
*pivot_var
= NULL
;
1355 if (min_is_manifestly_unbounded(tab
, var
))
1359 row
= pivot_row(tab
, NULL
, -1, col
);
1360 pivot_var
= var_from_col(tab
, col
);
1361 if (isl_tab_pivot(tab
, row
, col
) < 0)
1363 if (var
->is_redundant
)
1365 if (isl_int_is_neg(tab
->mat
->row
[var
->index
][1])) {
1366 if (var
->is_nonneg
) {
1367 if (!pivot_var
->is_redundant
&&
1368 pivot_var
->index
== row
) {
1369 if (isl_tab_pivot(tab
, row
, col
) < 0)
1372 if (restore_row(tab
, var
) < -1)
1378 if (var
->is_redundant
)
1380 while (!isl_int_is_neg(tab
->mat
->row
[var
->index
][1])) {
1381 find_pivot(tab
, var
, var
, -1, &row
, &col
);
1382 if (row
== var
->index
)
1385 return isl_int_sgn(tab
->mat
->row
[var
->index
][1]);
1386 pivot_var
= var_from_col(tab
, col
);
1387 if (isl_tab_pivot(tab
, row
, col
) < 0)
1389 if (var
->is_redundant
)
1392 if (pivot_var
&& var
->is_nonneg
) {
1393 /* pivot back to non-negative value */
1394 if (!pivot_var
->is_redundant
&& pivot_var
->index
== row
) {
1395 if (isl_tab_pivot(tab
, row
, col
) < 0)
1398 if (restore_row(tab
, var
) < -1)
1404 static int row_at_most_neg_one(struct isl_tab
*tab
, int row
)
1407 if (isl_int_is_pos(tab
->mat
->row
[row
][2]))
1409 if (isl_int_is_neg(tab
->mat
->row
[row
][2]))
1412 return isl_int_is_neg(tab
->mat
->row
[row
][1]) &&
1413 isl_int_abs_ge(tab
->mat
->row
[row
][1],
1414 tab
->mat
->row
[row
][0]);
1417 /* Return 1 if "var" can attain values <= -1.
1418 * Return 0 otherwise.
1420 * The sample value of "var" is assumed to be non-negative when the
1421 * the function is called. If 1 is returned then the constraint
1422 * is not redundant and the sample value is made non-negative again before
1423 * the function returns.
1425 int isl_tab_min_at_most_neg_one(struct isl_tab
*tab
, struct isl_tab_var
*var
)
1428 struct isl_tab_var
*pivot_var
;
1430 if (min_is_manifestly_unbounded(tab
, var
))
1434 row
= pivot_row(tab
, NULL
, -1, col
);
1435 pivot_var
= var_from_col(tab
, col
);
1436 if (isl_tab_pivot(tab
, row
, col
) < 0)
1438 if (var
->is_redundant
)
1440 if (row_at_most_neg_one(tab
, var
->index
)) {
1441 if (var
->is_nonneg
) {
1442 if (!pivot_var
->is_redundant
&&
1443 pivot_var
->index
== row
) {
1444 if (isl_tab_pivot(tab
, row
, col
) < 0)
1447 if (restore_row(tab
, var
) < -1)
1453 if (var
->is_redundant
)
1456 find_pivot(tab
, var
, var
, -1, &row
, &col
);
1457 if (row
== var
->index
) {
1458 if (restore_row(tab
, var
) < -1)
1464 pivot_var
= var_from_col(tab
, col
);
1465 if (isl_tab_pivot(tab
, row
, col
) < 0)
1467 if (var
->is_redundant
)
1469 } while (!row_at_most_neg_one(tab
, var
->index
));
1470 if (var
->is_nonneg
) {
1471 /* pivot back to non-negative value */
1472 if (!pivot_var
->is_redundant
&& pivot_var
->index
== row
)
1473 if (isl_tab_pivot(tab
, row
, col
) < 0)
1475 if (restore_row(tab
, var
) < -1)
1481 /* Return 1 if "var" can attain values >= 1.
1482 * Return 0 otherwise.
1484 static int at_least_one(struct isl_tab
*tab
, struct isl_tab_var
*var
)
1489 if (max_is_manifestly_unbounded(tab
, var
))
1491 if (to_row(tab
, var
, 1) < 0)
1493 r
= tab
->mat
->row
[var
->index
];
1494 while (isl_int_lt(r
[1], r
[0])) {
1495 find_pivot(tab
, var
, var
, 1, &row
, &col
);
1497 return isl_int_ge(r
[1], r
[0]);
1498 if (row
== var
->index
) /* manifestly unbounded */
1500 if (isl_tab_pivot(tab
, row
, col
) < 0)
1506 static void swap_cols(struct isl_tab
*tab
, int col1
, int col2
)
1509 unsigned off
= 2 + tab
->M
;
1510 t
= tab
->col_var
[col1
];
1511 tab
->col_var
[col1
] = tab
->col_var
[col2
];
1512 tab
->col_var
[col2
] = t
;
1513 var_from_col(tab
, col1
)->index
= col1
;
1514 var_from_col(tab
, col2
)->index
= col2
;
1515 tab
->mat
= isl_mat_swap_cols(tab
->mat
, off
+ col1
, off
+ col2
);
1518 /* Mark column with index "col" as representing a zero variable.
1519 * If we may need to undo the operation the column is kept,
1520 * but no longer considered.
1521 * Otherwise, the column is simply removed.
1523 * The column may be interchanged with some other column. If it
1524 * is interchanged with a later column, return 1. Otherwise return 0.
1525 * If the columns are checked in order in the calling function,
1526 * then a return value of 1 means that the column with the given
1527 * column number may now contain a different column that
1528 * hasn't been checked yet.
1530 int isl_tab_kill_col(struct isl_tab
*tab
, int col
)
1532 var_from_col(tab
, col
)->is_zero
= 1;
1533 if (tab
->need_undo
) {
1534 if (isl_tab_push_var(tab
, isl_tab_undo_zero
,
1535 var_from_col(tab
, col
)) < 0)
1537 if (col
!= tab
->n_dead
)
1538 swap_cols(tab
, col
, tab
->n_dead
);
1542 if (col
!= tab
->n_col
- 1)
1543 swap_cols(tab
, col
, tab
->n_col
- 1);
1544 var_from_col(tab
, tab
->n_col
- 1)->index
= -1;
1550 static int row_is_manifestly_non_integral(struct isl_tab
*tab
, int row
)
1552 unsigned off
= 2 + tab
->M
;
1554 if (tab
->M
&& !isl_int_eq(tab
->mat
->row
[row
][2],
1555 tab
->mat
->row
[row
][0]))
1557 if (isl_seq_first_non_zero(tab
->mat
->row
[row
] + off
+ tab
->n_dead
,
1558 tab
->n_col
- tab
->n_dead
) != -1)
1561 return !isl_int_is_divisible_by(tab
->mat
->row
[row
][1],
1562 tab
->mat
->row
[row
][0]);
1565 /* For integer tableaus, check if any of the coordinates are stuck
1566 * at a non-integral value.
1568 static int tab_is_manifestly_empty(struct isl_tab
*tab
)
1577 for (i
= 0; i
< tab
->n_var
; ++i
) {
1578 if (!tab
->var
[i
].is_row
)
1580 if (row_is_manifestly_non_integral(tab
, tab
->var
[i
].index
))
1587 /* Row variable "var" is non-negative and cannot attain any values
1588 * larger than zero. This means that the coefficients of the unrestricted
1589 * column variables are zero and that the coefficients of the non-negative
1590 * column variables are zero or negative.
1591 * Each of the non-negative variables with a negative coefficient can
1592 * then also be written as the negative sum of non-negative variables
1593 * and must therefore also be zero.
1595 static int close_row(struct isl_tab
*tab
, struct isl_tab_var
*var
) WARN_UNUSED
;
1596 static int close_row(struct isl_tab
*tab
, struct isl_tab_var
*var
)
1599 struct isl_mat
*mat
= tab
->mat
;
1600 unsigned off
= 2 + tab
->M
;
1602 isl_assert(tab
->mat
->ctx
, var
->is_nonneg
, return -1);
1605 if (isl_tab_push_var(tab
, isl_tab_undo_zero
, var
) < 0)
1607 for (j
= tab
->n_dead
; j
< tab
->n_col
; ++j
) {
1609 if (isl_int_is_zero(mat
->row
[var
->index
][off
+ j
]))
1611 isl_assert(tab
->mat
->ctx
,
1612 isl_int_is_neg(mat
->row
[var
->index
][off
+ j
]), return -1);
1613 recheck
= isl_tab_kill_col(tab
, j
);
1619 if (isl_tab_mark_redundant(tab
, var
->index
) < 0)
1621 if (tab_is_manifestly_empty(tab
) && isl_tab_mark_empty(tab
) < 0)
1626 /* Add a constraint to the tableau and allocate a row for it.
1627 * Return the index into the constraint array "con".
1629 int isl_tab_allocate_con(struct isl_tab
*tab
)
1633 isl_assert(tab
->mat
->ctx
, tab
->n_row
< tab
->mat
->n_row
, return -1);
1634 isl_assert(tab
->mat
->ctx
, tab
->n_con
< tab
->max_con
, return -1);
1637 tab
->con
[r
].index
= tab
->n_row
;
1638 tab
->con
[r
].is_row
= 1;
1639 tab
->con
[r
].is_nonneg
= 0;
1640 tab
->con
[r
].is_zero
= 0;
1641 tab
->con
[r
].is_redundant
= 0;
1642 tab
->con
[r
].frozen
= 0;
1643 tab
->con
[r
].negated
= 0;
1644 tab
->row_var
[tab
->n_row
] = ~r
;
1648 if (isl_tab_push_var(tab
, isl_tab_undo_allocate
, &tab
->con
[r
]) < 0)
1654 /* Add a variable to the tableau and allocate a column for it.
1655 * Return the index into the variable array "var".
1657 int isl_tab_allocate_var(struct isl_tab
*tab
)
1661 unsigned off
= 2 + tab
->M
;
1663 isl_assert(tab
->mat
->ctx
, tab
->n_col
< tab
->mat
->n_col
, return -1);
1664 isl_assert(tab
->mat
->ctx
, tab
->n_var
< tab
->max_var
, return -1);
1667 tab
->var
[r
].index
= tab
->n_col
;
1668 tab
->var
[r
].is_row
= 0;
1669 tab
->var
[r
].is_nonneg
= 0;
1670 tab
->var
[r
].is_zero
= 0;
1671 tab
->var
[r
].is_redundant
= 0;
1672 tab
->var
[r
].frozen
= 0;
1673 tab
->var
[r
].negated
= 0;
1674 tab
->col_var
[tab
->n_col
] = r
;
1676 for (i
= 0; i
< tab
->n_row
; ++i
)
1677 isl_int_set_si(tab
->mat
->row
[i
][off
+ tab
->n_col
], 0);
1681 if (isl_tab_push_var(tab
, isl_tab_undo_allocate
, &tab
->var
[r
]) < 0)
1687 /* Add a row to the tableau. The row is given as an affine combination
1688 * of the original variables and needs to be expressed in terms of the
1691 * We add each term in turn.
1692 * If r = n/d_r is the current sum and we need to add k x, then
1693 * if x is a column variable, we increase the numerator of
1694 * this column by k d_r
1695 * if x = f/d_x is a row variable, then the new representation of r is
1697 * n k f d_x/g n + d_r/g k f m/d_r n + m/d_g k f
1698 * --- + --- = ------------------- = -------------------
1699 * d_r d_r d_r d_x/g m
1701 * with g the gcd of d_r and d_x and m the lcm of d_r and d_x.
1703 * If tab->M is set, then, internally, each variable x is represented
1704 * as x' - M. We then also need no subtract k d_r from the coefficient of M.
1706 int isl_tab_add_row(struct isl_tab
*tab
, isl_int
*line
)
1712 unsigned off
= 2 + tab
->M
;
1714 r
= isl_tab_allocate_con(tab
);
1720 row
= tab
->mat
->row
[tab
->con
[r
].index
];
1721 isl_int_set_si(row
[0], 1);
1722 isl_int_set(row
[1], line
[0]);
1723 isl_seq_clr(row
+ 2, tab
->M
+ tab
->n_col
);
1724 for (i
= 0; i
< tab
->n_var
; ++i
) {
1725 if (tab
->var
[i
].is_zero
)
1727 if (tab
->var
[i
].is_row
) {
1729 row
[0], tab
->mat
->row
[tab
->var
[i
].index
][0]);
1730 isl_int_swap(a
, row
[0]);
1731 isl_int_divexact(a
, row
[0], a
);
1733 row
[0], tab
->mat
->row
[tab
->var
[i
].index
][0]);
1734 isl_int_mul(b
, b
, line
[1 + i
]);
1735 isl_seq_combine(row
+ 1, a
, row
+ 1,
1736 b
, tab
->mat
->row
[tab
->var
[i
].index
] + 1,
1737 1 + tab
->M
+ tab
->n_col
);
1739 isl_int_addmul(row
[off
+ tab
->var
[i
].index
],
1740 line
[1 + i
], row
[0]);
1741 if (tab
->M
&& i
>= tab
->n_param
&& i
< tab
->n_var
- tab
->n_div
)
1742 isl_int_submul(row
[2], line
[1 + i
], row
[0]);
1744 isl_seq_normalize(tab
->mat
->ctx
, row
, off
+ tab
->n_col
);
1749 tab
->row_sign
[tab
->con
[r
].index
] = isl_tab_row_unknown
;
1754 static int drop_row(struct isl_tab
*tab
, int row
)
1756 isl_assert(tab
->mat
->ctx
, ~tab
->row_var
[row
] == tab
->n_con
- 1, return -1);
1757 if (row
!= tab
->n_row
- 1)
1758 swap_rows(tab
, row
, tab
->n_row
- 1);
1764 static int drop_col(struct isl_tab
*tab
, int col
)
1766 isl_assert(tab
->mat
->ctx
, tab
->col_var
[col
] == tab
->n_var
- 1, return -1);
1767 if (col
!= tab
->n_col
- 1)
1768 swap_cols(tab
, col
, tab
->n_col
- 1);
1774 /* Add inequality "ineq" and check if it conflicts with the
1775 * previously added constraints or if it is obviously redundant.
1777 int isl_tab_add_ineq(struct isl_tab
*tab
, isl_int
*ineq
)
1786 struct isl_basic_map
*bmap
= tab
->bmap
;
1788 isl_assert(tab
->mat
->ctx
, tab
->n_eq
== bmap
->n_eq
, return -1);
1789 isl_assert(tab
->mat
->ctx
,
1790 tab
->n_con
== bmap
->n_eq
+ bmap
->n_ineq
, return -1);
1791 tab
->bmap
= isl_basic_map_add_ineq(tab
->bmap
, ineq
);
1792 if (isl_tab_push(tab
, isl_tab_undo_bmap_ineq
) < 0)
1799 isl_int_swap(ineq
[0], cst
);
1801 r
= isl_tab_add_row(tab
, ineq
);
1803 isl_int_swap(ineq
[0], cst
);
1808 tab
->con
[r
].is_nonneg
= 1;
1809 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
1811 if (isl_tab_row_is_redundant(tab
, tab
->con
[r
].index
)) {
1812 if (isl_tab_mark_redundant(tab
, tab
->con
[r
].index
) < 0)
1817 sgn
= restore_row(tab
, &tab
->con
[r
]);
1821 return isl_tab_mark_empty(tab
);
1822 if (tab
->con
[r
].is_row
&& isl_tab_row_is_redundant(tab
, tab
->con
[r
].index
))
1823 if (isl_tab_mark_redundant(tab
, tab
->con
[r
].index
) < 0)
1828 /* Pivot a non-negative variable down until it reaches the value zero
1829 * and then pivot the variable into a column position.
1831 static int to_col(struct isl_tab
*tab
, struct isl_tab_var
*var
) WARN_UNUSED
;
1832 static int to_col(struct isl_tab
*tab
, struct isl_tab_var
*var
)
1836 unsigned off
= 2 + tab
->M
;
1841 while (isl_int_is_pos(tab
->mat
->row
[var
->index
][1])) {
1842 find_pivot(tab
, var
, NULL
, -1, &row
, &col
);
1843 isl_assert(tab
->mat
->ctx
, row
!= -1, return -1);
1844 if (isl_tab_pivot(tab
, row
, col
) < 0)
1850 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
)
1851 if (!isl_int_is_zero(tab
->mat
->row
[var
->index
][off
+ i
]))
1854 isl_assert(tab
->mat
->ctx
, i
< tab
->n_col
, return -1);
1855 if (isl_tab_pivot(tab
, var
->index
, i
) < 0)
1861 /* We assume Gaussian elimination has been performed on the equalities.
1862 * The equalities can therefore never conflict.
1863 * Adding the equalities is currently only really useful for a later call
1864 * to isl_tab_ineq_type.
1866 static struct isl_tab
*add_eq(struct isl_tab
*tab
, isl_int
*eq
)
1873 r
= isl_tab_add_row(tab
, eq
);
1877 r
= tab
->con
[r
].index
;
1878 i
= isl_seq_first_non_zero(tab
->mat
->row
[r
] + 2 + tab
->M
+ tab
->n_dead
,
1879 tab
->n_col
- tab
->n_dead
);
1880 isl_assert(tab
->mat
->ctx
, i
>= 0, goto error
);
1882 if (isl_tab_pivot(tab
, r
, i
) < 0)
1884 if (isl_tab_kill_col(tab
, i
) < 0)
1894 static int row_is_manifestly_zero(struct isl_tab
*tab
, int row
)
1896 unsigned off
= 2 + tab
->M
;
1898 if (!isl_int_is_zero(tab
->mat
->row
[row
][1]))
1900 if (tab
->M
&& !isl_int_is_zero(tab
->mat
->row
[row
][2]))
1902 return isl_seq_first_non_zero(tab
->mat
->row
[row
] + off
+ tab
->n_dead
,
1903 tab
->n_col
- tab
->n_dead
) == -1;
1906 /* Add an equality that is known to be valid for the given tableau.
1908 int isl_tab_add_valid_eq(struct isl_tab
*tab
, isl_int
*eq
)
1910 struct isl_tab_var
*var
;
1915 r
= isl_tab_add_row(tab
, eq
);
1921 if (row_is_manifestly_zero(tab
, r
)) {
1923 if (isl_tab_mark_redundant(tab
, r
) < 0)
1928 if (isl_int_is_neg(tab
->mat
->row
[r
][1])) {
1929 isl_seq_neg(tab
->mat
->row
[r
] + 1, tab
->mat
->row
[r
] + 1,
1934 if (to_col(tab
, var
) < 0)
1937 if (isl_tab_kill_col(tab
, var
->index
) < 0)
1943 static int add_zero_row(struct isl_tab
*tab
)
1948 r
= isl_tab_allocate_con(tab
);
1952 row
= tab
->mat
->row
[tab
->con
[r
].index
];
1953 isl_seq_clr(row
+ 1, 1 + tab
->M
+ tab
->n_col
);
1954 isl_int_set_si(row
[0], 1);
1959 /* Add equality "eq" and check if it conflicts with the
1960 * previously added constraints or if it is obviously redundant.
1962 int isl_tab_add_eq(struct isl_tab
*tab
, isl_int
*eq
)
1964 struct isl_tab_undo
*snap
= NULL
;
1965 struct isl_tab_var
*var
;
1973 isl_assert(tab
->mat
->ctx
, !tab
->M
, return -1);
1976 snap
= isl_tab_snap(tab
);
1980 isl_int_swap(eq
[0], cst
);
1982 r
= isl_tab_add_row(tab
, eq
);
1984 isl_int_swap(eq
[0], cst
);
1992 if (row_is_manifestly_zero(tab
, row
)) {
1994 if (isl_tab_rollback(tab
, snap
) < 0)
2002 tab
->bmap
= isl_basic_map_add_ineq(tab
->bmap
, eq
);
2003 if (isl_tab_push(tab
, isl_tab_undo_bmap_ineq
) < 0)
2005 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
2006 tab
->bmap
= isl_basic_map_add_ineq(tab
->bmap
, eq
);
2007 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
2008 if (isl_tab_push(tab
, isl_tab_undo_bmap_ineq
) < 0)
2012 if (add_zero_row(tab
) < 0)
2016 sgn
= isl_int_sgn(tab
->mat
->row
[row
][1]);
2019 isl_seq_neg(tab
->mat
->row
[row
] + 1, tab
->mat
->row
[row
] + 1,
2026 sgn
= sign_of_max(tab
, var
);
2030 if (isl_tab_mark_empty(tab
) < 0)
2037 if (to_col(tab
, var
) < 0)
2040 if (isl_tab_kill_col(tab
, var
->index
) < 0)
2046 /* Construct and return an inequality that expresses an upper bound
2048 * In particular, if the div is given by
2052 * then the inequality expresses
2056 static struct isl_vec
*ineq_for_div(struct isl_basic_map
*bmap
, unsigned div
)
2060 struct isl_vec
*ineq
;
2065 total
= isl_basic_map_total_dim(bmap
);
2066 div_pos
= 1 + total
- bmap
->n_div
+ div
;
2068 ineq
= isl_vec_alloc(bmap
->ctx
, 1 + total
);
2072 isl_seq_cpy(ineq
->el
, bmap
->div
[div
] + 1, 1 + total
);
2073 isl_int_neg(ineq
->el
[div_pos
], bmap
->div
[div
][0]);
2077 /* For a div d = floor(f/m), add the constraints
2080 * -(f-(m-1)) + m d >= 0
2082 * Note that the second constraint is the negation of
2086 * If add_ineq is not NULL, then this function is used
2087 * instead of isl_tab_add_ineq to effectively add the inequalities.
2089 static int add_div_constraints(struct isl_tab
*tab
, unsigned div
,
2090 int (*add_ineq
)(void *user
, isl_int
*), void *user
)
2094 struct isl_vec
*ineq
;
2096 total
= isl_basic_map_total_dim(tab
->bmap
);
2097 div_pos
= 1 + total
- tab
->bmap
->n_div
+ div
;
2099 ineq
= ineq_for_div(tab
->bmap
, div
);
2104 if (add_ineq(user
, ineq
->el
) < 0)
2107 if (isl_tab_add_ineq(tab
, ineq
->el
) < 0)
2111 isl_seq_neg(ineq
->el
, tab
->bmap
->div
[div
] + 1, 1 + total
);
2112 isl_int_set(ineq
->el
[div_pos
], tab
->bmap
->div
[div
][0]);
2113 isl_int_add(ineq
->el
[0], ineq
->el
[0], ineq
->el
[div_pos
]);
2114 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
2117 if (add_ineq(user
, ineq
->el
) < 0)
2120 if (isl_tab_add_ineq(tab
, ineq
->el
) < 0)
2132 /* Check whether the div described by "div" is obviously non-negative.
2133 * If we are using a big parameter, then we will encode the div
2134 * as div' = M + div, which is always non-negative.
2135 * Otherwise, we check whether div is a non-negative affine combination
2136 * of non-negative variables.
2138 static int div_is_nonneg(struct isl_tab
*tab
, __isl_keep isl_vec
*div
)
2145 if (isl_int_is_neg(div
->el
[1]))
2148 for (i
= 0; i
< tab
->n_var
; ++i
) {
2149 if (isl_int_is_neg(div
->el
[2 + i
]))
2151 if (isl_int_is_zero(div
->el
[2 + i
]))
2153 if (!tab
->var
[i
].is_nonneg
)
2160 /* Add an extra div, prescribed by "div" to the tableau and
2161 * the associated bmap (which is assumed to be non-NULL).
2163 * If add_ineq is not NULL, then this function is used instead
2164 * of isl_tab_add_ineq to add the div constraints.
2165 * This complication is needed because the code in isl_tab_pip
2166 * wants to perform some extra processing when an inequality
2167 * is added to the tableau.
2169 int isl_tab_add_div(struct isl_tab
*tab
, __isl_keep isl_vec
*div
,
2170 int (*add_ineq
)(void *user
, isl_int
*), void *user
)
2179 isl_assert(tab
->mat
->ctx
, tab
->bmap
, return -1);
2181 nonneg
= div_is_nonneg(tab
, div
);
2183 if (isl_tab_extend_cons(tab
, 3) < 0)
2185 if (isl_tab_extend_vars(tab
, 1) < 0)
2187 r
= isl_tab_allocate_var(tab
);
2192 tab
->var
[r
].is_nonneg
= 1;
2194 tab
->bmap
= isl_basic_map_extend_space(tab
->bmap
,
2195 isl_basic_map_get_space(tab
->bmap
), 1, 0, 2);
2196 k
= isl_basic_map_alloc_div(tab
->bmap
);
2199 isl_seq_cpy(tab
->bmap
->div
[k
], div
->el
, div
->size
);
2200 if (isl_tab_push(tab
, isl_tab_undo_bmap_div
) < 0)
2203 if (add_div_constraints(tab
, k
, add_ineq
, user
) < 0)
2209 /* If "track" is set, then we want to keep track of all constraints in tab
2210 * in its bmap field. This field is initialized from a copy of "bmap",
2211 * so we need to make sure that all constraints in "bmap" also appear
2212 * in the constructed tab.
2214 __isl_give
struct isl_tab
*isl_tab_from_basic_map(
2215 __isl_keep isl_basic_map
*bmap
, int track
)
2218 struct isl_tab
*tab
;
2222 tab
= isl_tab_alloc(bmap
->ctx
,
2223 isl_basic_map_total_dim(bmap
) + bmap
->n_ineq
+ 1,
2224 isl_basic_map_total_dim(bmap
), 0);
2227 tab
->preserve
= track
;
2228 tab
->rational
= ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
2229 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
)) {
2230 if (isl_tab_mark_empty(tab
) < 0)
2234 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
2235 tab
= add_eq(tab
, bmap
->eq
[i
]);
2239 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
2240 if (isl_tab_add_ineq(tab
, bmap
->ineq
[i
]) < 0)
2246 if (track
&& isl_tab_track_bmap(tab
, isl_basic_map_copy(bmap
)) < 0)
2254 __isl_give
struct isl_tab
*isl_tab_from_basic_set(
2255 __isl_keep isl_basic_set
*bset
, int track
)
2257 return isl_tab_from_basic_map(bset
, track
);
2260 /* Construct a tableau corresponding to the recession cone of "bset".
2262 struct isl_tab
*isl_tab_from_recession_cone(__isl_keep isl_basic_set
*bset
,
2267 struct isl_tab
*tab
;
2268 unsigned offset
= 0;
2273 offset
= isl_basic_set_dim(bset
, isl_dim_param
);
2274 tab
= isl_tab_alloc(bset
->ctx
, bset
->n_eq
+ bset
->n_ineq
,
2275 isl_basic_set_total_dim(bset
) - offset
, 0);
2278 tab
->rational
= ISL_F_ISSET(bset
, ISL_BASIC_SET_RATIONAL
);
2282 for (i
= 0; i
< bset
->n_eq
; ++i
) {
2283 isl_int_swap(bset
->eq
[i
][offset
], cst
);
2285 if (isl_tab_add_eq(tab
, bset
->eq
[i
] + offset
) < 0)
2288 tab
= add_eq(tab
, bset
->eq
[i
]);
2289 isl_int_swap(bset
->eq
[i
][offset
], cst
);
2293 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
2295 isl_int_swap(bset
->ineq
[i
][offset
], cst
);
2296 r
= isl_tab_add_row(tab
, bset
->ineq
[i
] + offset
);
2297 isl_int_swap(bset
->ineq
[i
][offset
], cst
);
2300 tab
->con
[r
].is_nonneg
= 1;
2301 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
2313 /* Assuming "tab" is the tableau of a cone, check if the cone is
2314 * bounded, i.e., if it is empty or only contains the origin.
2316 int isl_tab_cone_is_bounded(struct isl_tab
*tab
)
2324 if (tab
->n_dead
== tab
->n_col
)
2328 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
2329 struct isl_tab_var
*var
;
2331 var
= isl_tab_var_from_row(tab
, i
);
2332 if (!var
->is_nonneg
)
2334 sgn
= sign_of_max(tab
, var
);
2339 if (close_row(tab
, var
) < 0)
2343 if (tab
->n_dead
== tab
->n_col
)
2345 if (i
== tab
->n_row
)
2350 int isl_tab_sample_is_integer(struct isl_tab
*tab
)
2357 for (i
= 0; i
< tab
->n_var
; ++i
) {
2359 if (!tab
->var
[i
].is_row
)
2361 row
= tab
->var
[i
].index
;
2362 if (!isl_int_is_divisible_by(tab
->mat
->row
[row
][1],
2363 tab
->mat
->row
[row
][0]))
2369 static struct isl_vec
*extract_integer_sample(struct isl_tab
*tab
)
2372 struct isl_vec
*vec
;
2374 vec
= isl_vec_alloc(tab
->mat
->ctx
, 1 + tab
->n_var
);
2378 isl_int_set_si(vec
->block
.data
[0], 1);
2379 for (i
= 0; i
< tab
->n_var
; ++i
) {
2380 if (!tab
->var
[i
].is_row
)
2381 isl_int_set_si(vec
->block
.data
[1 + i
], 0);
2383 int row
= tab
->var
[i
].index
;
2384 isl_int_divexact(vec
->block
.data
[1 + i
],
2385 tab
->mat
->row
[row
][1], tab
->mat
->row
[row
][0]);
2392 struct isl_vec
*isl_tab_get_sample_value(struct isl_tab
*tab
)
2395 struct isl_vec
*vec
;
2401 vec
= isl_vec_alloc(tab
->mat
->ctx
, 1 + tab
->n_var
);
2407 isl_int_set_si(vec
->block
.data
[0], 1);
2408 for (i
= 0; i
< tab
->n_var
; ++i
) {
2410 if (!tab
->var
[i
].is_row
) {
2411 isl_int_set_si(vec
->block
.data
[1 + i
], 0);
2414 row
= tab
->var
[i
].index
;
2415 isl_int_gcd(m
, vec
->block
.data
[0], tab
->mat
->row
[row
][0]);
2416 isl_int_divexact(m
, tab
->mat
->row
[row
][0], m
);
2417 isl_seq_scale(vec
->block
.data
, vec
->block
.data
, m
, 1 + i
);
2418 isl_int_divexact(m
, vec
->block
.data
[0], tab
->mat
->row
[row
][0]);
2419 isl_int_mul(vec
->block
.data
[1 + i
], m
, tab
->mat
->row
[row
][1]);
2421 vec
= isl_vec_normalize(vec
);
2427 /* Update "bmap" based on the results of the tableau "tab".
2428 * In particular, implicit equalities are made explicit, redundant constraints
2429 * are removed and if the sample value happens to be integer, it is stored
2430 * in "bmap" (unless "bmap" already had an integer sample).
2432 * The tableau is assumed to have been created from "bmap" using
2433 * isl_tab_from_basic_map.
2435 struct isl_basic_map
*isl_basic_map_update_from_tab(struct isl_basic_map
*bmap
,
2436 struct isl_tab
*tab
)
2448 bmap
= isl_basic_map_set_to_empty(bmap
);
2450 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
2451 if (isl_tab_is_equality(tab
, n_eq
+ i
))
2452 isl_basic_map_inequality_to_equality(bmap
, i
);
2453 else if (isl_tab_is_redundant(tab
, n_eq
+ i
))
2454 isl_basic_map_drop_inequality(bmap
, i
);
2456 if (bmap
->n_eq
!= n_eq
)
2457 isl_basic_map_gauss(bmap
, NULL
);
2458 if (!tab
->rational
&&
2459 !bmap
->sample
&& isl_tab_sample_is_integer(tab
))
2460 bmap
->sample
= extract_integer_sample(tab
);
2464 struct isl_basic_set
*isl_basic_set_update_from_tab(struct isl_basic_set
*bset
,
2465 struct isl_tab
*tab
)
2467 return (struct isl_basic_set
*)isl_basic_map_update_from_tab(
2468 (struct isl_basic_map
*)bset
, tab
);
2471 /* Given a non-negative variable "var", add a new non-negative variable
2472 * that is the opposite of "var", ensuring that var can only attain the
2474 * If var = n/d is a row variable, then the new variable = -n/d.
2475 * If var is a column variables, then the new variable = -var.
2476 * If the new variable cannot attain non-negative values, then
2477 * the resulting tableau is empty.
2478 * Otherwise, we know the value will be zero and we close the row.
2480 static int cut_to_hyperplane(struct isl_tab
*tab
, struct isl_tab_var
*var
)
2485 unsigned off
= 2 + tab
->M
;
2489 isl_assert(tab
->mat
->ctx
, !var
->is_redundant
, return -1);
2490 isl_assert(tab
->mat
->ctx
, var
->is_nonneg
, return -1);
2492 if (isl_tab_extend_cons(tab
, 1) < 0)
2496 tab
->con
[r
].index
= tab
->n_row
;
2497 tab
->con
[r
].is_row
= 1;
2498 tab
->con
[r
].is_nonneg
= 0;
2499 tab
->con
[r
].is_zero
= 0;
2500 tab
->con
[r
].is_redundant
= 0;
2501 tab
->con
[r
].frozen
= 0;
2502 tab
->con
[r
].negated
= 0;
2503 tab
->row_var
[tab
->n_row
] = ~r
;
2504 row
= tab
->mat
->row
[tab
->n_row
];
2507 isl_int_set(row
[0], tab
->mat
->row
[var
->index
][0]);
2508 isl_seq_neg(row
+ 1,
2509 tab
->mat
->row
[var
->index
] + 1, 1 + tab
->n_col
);
2511 isl_int_set_si(row
[0], 1);
2512 isl_seq_clr(row
+ 1, 1 + tab
->n_col
);
2513 isl_int_set_si(row
[off
+ var
->index
], -1);
2518 if (isl_tab_push_var(tab
, isl_tab_undo_allocate
, &tab
->con
[r
]) < 0)
2521 sgn
= sign_of_max(tab
, &tab
->con
[r
]);
2525 if (isl_tab_mark_empty(tab
) < 0)
2529 tab
->con
[r
].is_nonneg
= 1;
2530 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
2533 if (close_row(tab
, &tab
->con
[r
]) < 0)
2539 /* Given a tableau "tab" and an inequality constraint "con" of the tableau,
2540 * relax the inequality by one. That is, the inequality r >= 0 is replaced
2541 * by r' = r + 1 >= 0.
2542 * If r is a row variable, we simply increase the constant term by one
2543 * (taking into account the denominator).
2544 * If r is a column variable, then we need to modify each row that
2545 * refers to r = r' - 1 by substituting this equality, effectively
2546 * subtracting the coefficient of the column from the constant.
2547 * We should only do this if the minimum is manifestly unbounded,
2548 * however. Otherwise, we may end up with negative sample values
2549 * for non-negative variables.
2550 * So, if r is a column variable with a minimum that is not
2551 * manifestly unbounded, then we need to move it to a row.
2552 * However, the sample value of this row may be negative,
2553 * even after the relaxation, so we need to restore it.
2554 * We therefore prefer to pivot a column up to a row, if possible.
2556 struct isl_tab
*isl_tab_relax(struct isl_tab
*tab
, int con
)
2558 struct isl_tab_var
*var
;
2559 unsigned off
= 2 + tab
->M
;
2564 var
= &tab
->con
[con
];
2566 if (var
->is_row
&& (var
->index
< 0 || var
->index
< tab
->n_redundant
))
2567 isl_die(tab
->mat
->ctx
, isl_error_invalid
,
2568 "cannot relax redundant constraint", goto error
);
2569 if (!var
->is_row
&& (var
->index
< 0 || var
->index
< tab
->n_dead
))
2570 isl_die(tab
->mat
->ctx
, isl_error_invalid
,
2571 "cannot relax dead constraint", goto error
);
2573 if (!var
->is_row
&& !max_is_manifestly_unbounded(tab
, var
))
2574 if (to_row(tab
, var
, 1) < 0)
2576 if (!var
->is_row
&& !min_is_manifestly_unbounded(tab
, var
))
2577 if (to_row(tab
, var
, -1) < 0)
2581 isl_int_add(tab
->mat
->row
[var
->index
][1],
2582 tab
->mat
->row
[var
->index
][1], tab
->mat
->row
[var
->index
][0]);
2583 if (restore_row(tab
, var
) < 0)
2588 for (i
= 0; i
< tab
->n_row
; ++i
) {
2589 if (isl_int_is_zero(tab
->mat
->row
[i
][off
+ var
->index
]))
2591 isl_int_sub(tab
->mat
->row
[i
][1], tab
->mat
->row
[i
][1],
2592 tab
->mat
->row
[i
][off
+ var
->index
]);
2597 if (isl_tab_push_var(tab
, isl_tab_undo_relax
, var
) < 0)
2606 /* Remove the sign constraint from constraint "con".
2608 * If the constraint variable was originally marked non-negative,
2609 * then we make sure we mark it non-negative again during rollback.
2611 int isl_tab_unrestrict(struct isl_tab
*tab
, int con
)
2613 struct isl_tab_var
*var
;
2618 var
= &tab
->con
[con
];
2619 if (!var
->is_nonneg
)
2623 if (isl_tab_push_var(tab
, isl_tab_undo_unrestrict
, var
) < 0)
2629 int isl_tab_select_facet(struct isl_tab
*tab
, int con
)
2634 return cut_to_hyperplane(tab
, &tab
->con
[con
]);
2637 static int may_be_equality(struct isl_tab
*tab
, int row
)
2639 return tab
->rational
? isl_int_is_zero(tab
->mat
->row
[row
][1])
2640 : isl_int_lt(tab
->mat
->row
[row
][1],
2641 tab
->mat
->row
[row
][0]);
2644 /* Check for (near) equalities among the constraints.
2645 * A constraint is an equality if it is non-negative and if
2646 * its maximal value is either
2647 * - zero (in case of rational tableaus), or
2648 * - strictly less than 1 (in case of integer tableaus)
2650 * We first mark all non-redundant and non-dead variables that
2651 * are not frozen and not obviously not an equality.
2652 * Then we iterate over all marked variables if they can attain
2653 * any values larger than zero or at least one.
2654 * If the maximal value is zero, we mark any column variables
2655 * that appear in the row as being zero and mark the row as being redundant.
2656 * Otherwise, if the maximal value is strictly less than one (and the
2657 * tableau is integer), then we restrict the value to being zero
2658 * by adding an opposite non-negative variable.
2660 int isl_tab_detect_implicit_equalities(struct isl_tab
*tab
)
2669 if (tab
->n_dead
== tab
->n_col
)
2673 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
2674 struct isl_tab_var
*var
= isl_tab_var_from_row(tab
, i
);
2675 var
->marked
= !var
->frozen
&& var
->is_nonneg
&&
2676 may_be_equality(tab
, i
);
2680 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
2681 struct isl_tab_var
*var
= var_from_col(tab
, i
);
2682 var
->marked
= !var
->frozen
&& var
->is_nonneg
;
2687 struct isl_tab_var
*var
;
2689 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
2690 var
= isl_tab_var_from_row(tab
, i
);
2694 if (i
== tab
->n_row
) {
2695 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
2696 var
= var_from_col(tab
, i
);
2700 if (i
== tab
->n_col
)
2705 sgn
= sign_of_max(tab
, var
);
2709 if (close_row(tab
, var
) < 0)
2711 } else if (!tab
->rational
&& !at_least_one(tab
, var
)) {
2712 if (cut_to_hyperplane(tab
, var
) < 0)
2714 return isl_tab_detect_implicit_equalities(tab
);
2716 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
2717 var
= isl_tab_var_from_row(tab
, i
);
2720 if (may_be_equality(tab
, i
))
2730 /* Update the element of row_var or col_var that corresponds to
2731 * constraint tab->con[i] to a move from position "old" to position "i".
2733 static int update_con_after_move(struct isl_tab
*tab
, int i
, int old
)
2738 index
= tab
->con
[i
].index
;
2741 p
= tab
->con
[i
].is_row
? tab
->row_var
: tab
->col_var
;
2742 if (p
[index
] != ~old
)
2743 isl_die(tab
->mat
->ctx
, isl_error_internal
,
2744 "broken internal state", return -1);
2750 /* Rotate the "n" constraints starting at "first" to the right,
2751 * putting the last constraint in the position of the first constraint.
2753 static int rotate_constraints(struct isl_tab
*tab
, int first
, int n
)
2756 struct isl_tab_var var
;
2761 last
= first
+ n
- 1;
2762 var
= tab
->con
[last
];
2763 for (i
= last
; i
> first
; --i
) {
2764 tab
->con
[i
] = tab
->con
[i
- 1];
2765 if (update_con_after_move(tab
, i
, i
- 1) < 0)
2768 tab
->con
[first
] = var
;
2769 if (update_con_after_move(tab
, first
, last
) < 0)
2775 /* Make the equalities that are implicit in "bmap" but that have been
2776 * detected in the corresponding "tab" explicit in "bmap" and update
2777 * "tab" to reflect the new order of the constraints.
2779 * In particular, if inequality i is an implicit equality then
2780 * isl_basic_map_inequality_to_equality will move the inequality
2781 * in front of the other equality and it will move the last inequality
2782 * in the position of inequality i.
2783 * In the tableau, the inequalities of "bmap" are stored after the equalities
2784 * and so the original order
2786 * E E E E E A A A I B B B B L
2790 * I E E E E E A A A L B B B B
2792 * where I is the implicit equality, the E are equalities,
2793 * the A inequalities before I, the B inequalities after I and
2794 * L the last inequality.
2795 * We therefore need to rotate to the right two sets of constraints,
2796 * those up to and including I and those after I.
2798 * If "tab" contains any constraints that are not in "bmap" then they
2799 * appear after those in "bmap" and they should be left untouched.
2801 * Note that this function leaves "bmap" in a temporary state
2802 * as it does not call isl_basic_map_gauss. Calling this function
2803 * is the responsibility of the caller.
2805 __isl_give isl_basic_map
*isl_tab_make_equalities_explicit(struct isl_tab
*tab
,
2806 __isl_take isl_basic_map
*bmap
)
2811 return isl_basic_map_free(bmap
);
2815 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
2816 if (!isl_tab_is_equality(tab
, bmap
->n_eq
+ i
))
2818 isl_basic_map_inequality_to_equality(bmap
, i
);
2819 if (rotate_constraints(tab
, 0, tab
->n_eq
+ i
+ 1) < 0)
2820 return isl_basic_map_free(bmap
);
2821 if (rotate_constraints(tab
, tab
->n_eq
+ i
+ 1,
2822 bmap
->n_ineq
- i
) < 0)
2823 return isl_basic_map_free(bmap
);
2830 static int con_is_redundant(struct isl_tab
*tab
, struct isl_tab_var
*var
)
2834 if (tab
->rational
) {
2835 int sgn
= sign_of_min(tab
, var
);
2840 int irred
= isl_tab_min_at_most_neg_one(tab
, var
);
2847 /* Check for (near) redundant constraints.
2848 * A constraint is redundant if it is non-negative and if
2849 * its minimal value (temporarily ignoring the non-negativity) is either
2850 * - zero (in case of rational tableaus), or
2851 * - strictly larger than -1 (in case of integer tableaus)
2853 * We first mark all non-redundant and non-dead variables that
2854 * are not frozen and not obviously negatively unbounded.
2855 * Then we iterate over all marked variables if they can attain
2856 * any values smaller than zero or at most negative one.
2857 * If not, we mark the row as being redundant (assuming it hasn't
2858 * been detected as being obviously redundant in the mean time).
2860 int isl_tab_detect_redundant(struct isl_tab
*tab
)
2869 if (tab
->n_redundant
== tab
->n_row
)
2873 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
2874 struct isl_tab_var
*var
= isl_tab_var_from_row(tab
, i
);
2875 var
->marked
= !var
->frozen
&& var
->is_nonneg
;
2879 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
2880 struct isl_tab_var
*var
= var_from_col(tab
, i
);
2881 var
->marked
= !var
->frozen
&& var
->is_nonneg
&&
2882 !min_is_manifestly_unbounded(tab
, var
);
2887 struct isl_tab_var
*var
;
2889 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
2890 var
= isl_tab_var_from_row(tab
, i
);
2894 if (i
== tab
->n_row
) {
2895 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
2896 var
= var_from_col(tab
, i
);
2900 if (i
== tab
->n_col
)
2905 red
= con_is_redundant(tab
, var
);
2908 if (red
&& !var
->is_redundant
)
2909 if (isl_tab_mark_redundant(tab
, var
->index
) < 0)
2911 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
2912 var
= var_from_col(tab
, i
);
2915 if (!min_is_manifestly_unbounded(tab
, var
))
2925 int isl_tab_is_equality(struct isl_tab
*tab
, int con
)
2932 if (tab
->con
[con
].is_zero
)
2934 if (tab
->con
[con
].is_redundant
)
2936 if (!tab
->con
[con
].is_row
)
2937 return tab
->con
[con
].index
< tab
->n_dead
;
2939 row
= tab
->con
[con
].index
;
2942 return isl_int_is_zero(tab
->mat
->row
[row
][1]) &&
2943 (!tab
->M
|| isl_int_is_zero(tab
->mat
->row
[row
][2])) &&
2944 isl_seq_first_non_zero(tab
->mat
->row
[row
] + off
+ tab
->n_dead
,
2945 tab
->n_col
- tab
->n_dead
) == -1;
2948 /* Return the minimal value of the affine expression "f" with denominator
2949 * "denom" in *opt, *opt_denom, assuming the tableau is not empty and
2950 * the expression cannot attain arbitrarily small values.
2951 * If opt_denom is NULL, then *opt is rounded up to the nearest integer.
2952 * The return value reflects the nature of the result (empty, unbounded,
2953 * minimal value returned in *opt).
2955 enum isl_lp_result
isl_tab_min(struct isl_tab
*tab
,
2956 isl_int
*f
, isl_int denom
, isl_int
*opt
, isl_int
*opt_denom
,
2960 enum isl_lp_result res
= isl_lp_ok
;
2961 struct isl_tab_var
*var
;
2962 struct isl_tab_undo
*snap
;
2965 return isl_lp_error
;
2968 return isl_lp_empty
;
2970 snap
= isl_tab_snap(tab
);
2971 r
= isl_tab_add_row(tab
, f
);
2973 return isl_lp_error
;
2977 find_pivot(tab
, var
, var
, -1, &row
, &col
);
2978 if (row
== var
->index
) {
2979 res
= isl_lp_unbounded
;
2984 if (isl_tab_pivot(tab
, row
, col
) < 0)
2985 return isl_lp_error
;
2987 isl_int_mul(tab
->mat
->row
[var
->index
][0],
2988 tab
->mat
->row
[var
->index
][0], denom
);
2989 if (ISL_FL_ISSET(flags
, ISL_TAB_SAVE_DUAL
)) {
2992 isl_vec_free(tab
->dual
);
2993 tab
->dual
= isl_vec_alloc(tab
->mat
->ctx
, 1 + tab
->n_con
);
2995 return isl_lp_error
;
2996 isl_int_set(tab
->dual
->el
[0], tab
->mat
->row
[var
->index
][0]);
2997 for (i
= 0; i
< tab
->n_con
; ++i
) {
2999 if (tab
->con
[i
].is_row
) {
3000 isl_int_set_si(tab
->dual
->el
[1 + i
], 0);
3003 pos
= 2 + tab
->M
+ tab
->con
[i
].index
;
3004 if (tab
->con
[i
].negated
)
3005 isl_int_neg(tab
->dual
->el
[1 + i
],
3006 tab
->mat
->row
[var
->index
][pos
]);
3008 isl_int_set(tab
->dual
->el
[1 + i
],
3009 tab
->mat
->row
[var
->index
][pos
]);
3012 if (opt
&& res
== isl_lp_ok
) {
3014 isl_int_set(*opt
, tab
->mat
->row
[var
->index
][1]);
3015 isl_int_set(*opt_denom
, tab
->mat
->row
[var
->index
][0]);
3017 isl_int_cdiv_q(*opt
, tab
->mat
->row
[var
->index
][1],
3018 tab
->mat
->row
[var
->index
][0]);
3020 if (isl_tab_rollback(tab
, snap
) < 0)
3021 return isl_lp_error
;
3025 int isl_tab_is_redundant(struct isl_tab
*tab
, int con
)
3029 if (tab
->con
[con
].is_zero
)
3031 if (tab
->con
[con
].is_redundant
)
3033 return tab
->con
[con
].is_row
&& tab
->con
[con
].index
< tab
->n_redundant
;
3036 /* Take a snapshot of the tableau that can be restored by s call to
3039 struct isl_tab_undo
*isl_tab_snap(struct isl_tab
*tab
)
3047 /* Undo the operation performed by isl_tab_relax.
3049 static int unrelax(struct isl_tab
*tab
, struct isl_tab_var
*var
) WARN_UNUSED
;
3050 static int unrelax(struct isl_tab
*tab
, struct isl_tab_var
*var
)
3052 unsigned off
= 2 + tab
->M
;
3054 if (!var
->is_row
&& !max_is_manifestly_unbounded(tab
, var
))
3055 if (to_row(tab
, var
, 1) < 0)
3059 isl_int_sub(tab
->mat
->row
[var
->index
][1],
3060 tab
->mat
->row
[var
->index
][1], tab
->mat
->row
[var
->index
][0]);
3061 if (var
->is_nonneg
) {
3062 int sgn
= restore_row(tab
, var
);
3063 isl_assert(tab
->mat
->ctx
, sgn
>= 0, return -1);
3068 for (i
= 0; i
< tab
->n_row
; ++i
) {
3069 if (isl_int_is_zero(tab
->mat
->row
[i
][off
+ var
->index
]))
3071 isl_int_add(tab
->mat
->row
[i
][1], tab
->mat
->row
[i
][1],
3072 tab
->mat
->row
[i
][off
+ var
->index
]);
3080 /* Undo the operation performed by isl_tab_unrestrict.
3082 * In particular, mark the variable as being non-negative and make
3083 * sure the sample value respects this constraint.
3085 static int ununrestrict(struct isl_tab
*tab
, struct isl_tab_var
*var
)
3089 if (var
->is_row
&& restore_row(tab
, var
) < -1)
3095 static int perform_undo_var(struct isl_tab
*tab
, struct isl_tab_undo
*undo
) WARN_UNUSED
;
3096 static int perform_undo_var(struct isl_tab
*tab
, struct isl_tab_undo
*undo
)
3098 struct isl_tab_var
*var
= var_from_index(tab
, undo
->u
.var_index
);
3099 switch (undo
->type
) {
3100 case isl_tab_undo_nonneg
:
3103 case isl_tab_undo_redundant
:
3104 var
->is_redundant
= 0;
3106 restore_row(tab
, isl_tab_var_from_row(tab
, tab
->n_redundant
));
3108 case isl_tab_undo_freeze
:
3111 case isl_tab_undo_zero
:
3116 case isl_tab_undo_allocate
:
3117 if (undo
->u
.var_index
>= 0) {
3118 isl_assert(tab
->mat
->ctx
, !var
->is_row
, return -1);
3119 drop_col(tab
, var
->index
);
3123 if (!max_is_manifestly_unbounded(tab
, var
)) {
3124 if (to_row(tab
, var
, 1) < 0)
3126 } else if (!min_is_manifestly_unbounded(tab
, var
)) {
3127 if (to_row(tab
, var
, -1) < 0)
3130 if (to_row(tab
, var
, 0) < 0)
3133 drop_row(tab
, var
->index
);
3135 case isl_tab_undo_relax
:
3136 return unrelax(tab
, var
);
3137 case isl_tab_undo_unrestrict
:
3138 return ununrestrict(tab
, var
);
3140 isl_die(tab
->mat
->ctx
, isl_error_internal
,
3141 "perform_undo_var called on invalid undo record",
3148 /* Restore the tableau to the state where the basic variables
3149 * are those in "col_var".
3150 * We first construct a list of variables that are currently in
3151 * the basis, but shouldn't. Then we iterate over all variables
3152 * that should be in the basis and for each one that is currently
3153 * not in the basis, we exchange it with one of the elements of the
3154 * list constructed before.
3155 * We can always find an appropriate variable to pivot with because
3156 * the current basis is mapped to the old basis by a non-singular
3157 * matrix and so we can never end up with a zero row.
3159 static int restore_basis(struct isl_tab
*tab
, int *col_var
)
3163 int *extra
= NULL
; /* current columns that contain bad stuff */
3164 unsigned off
= 2 + tab
->M
;
3166 extra
= isl_alloc_array(tab
->mat
->ctx
, int, tab
->n_col
);
3167 if (tab
->n_col
&& !extra
)
3169 for (i
= 0; i
< tab
->n_col
; ++i
) {
3170 for (j
= 0; j
< tab
->n_col
; ++j
)
3171 if (tab
->col_var
[i
] == col_var
[j
])
3175 extra
[n_extra
++] = i
;
3177 for (i
= 0; i
< tab
->n_col
&& n_extra
> 0; ++i
) {
3178 struct isl_tab_var
*var
;
3181 for (j
= 0; j
< tab
->n_col
; ++j
)
3182 if (col_var
[i
] == tab
->col_var
[j
])
3186 var
= var_from_index(tab
, col_var
[i
]);
3188 for (j
= 0; j
< n_extra
; ++j
)
3189 if (!isl_int_is_zero(tab
->mat
->row
[row
][off
+extra
[j
]]))
3191 isl_assert(tab
->mat
->ctx
, j
< n_extra
, goto error
);
3192 if (isl_tab_pivot(tab
, row
, extra
[j
]) < 0)
3194 extra
[j
] = extra
[--n_extra
];
3204 /* Remove all samples with index n or greater, i.e., those samples
3205 * that were added since we saved this number of samples in
3206 * isl_tab_save_samples.
3208 static void drop_samples_since(struct isl_tab
*tab
, int n
)
3212 for (i
= tab
->n_sample
- 1; i
>= 0 && tab
->n_sample
> n
; --i
) {
3213 if (tab
->sample_index
[i
] < n
)
3216 if (i
!= tab
->n_sample
- 1) {
3217 int t
= tab
->sample_index
[tab
->n_sample
-1];
3218 tab
->sample_index
[tab
->n_sample
-1] = tab
->sample_index
[i
];
3219 tab
->sample_index
[i
] = t
;
3220 isl_mat_swap_rows(tab
->samples
, tab
->n_sample
-1, i
);
3226 static int perform_undo(struct isl_tab
*tab
, struct isl_tab_undo
*undo
) WARN_UNUSED
;
3227 static int perform_undo(struct isl_tab
*tab
, struct isl_tab_undo
*undo
)
3229 switch (undo
->type
) {
3230 case isl_tab_undo_empty
:
3233 case isl_tab_undo_nonneg
:
3234 case isl_tab_undo_redundant
:
3235 case isl_tab_undo_freeze
:
3236 case isl_tab_undo_zero
:
3237 case isl_tab_undo_allocate
:
3238 case isl_tab_undo_relax
:
3239 case isl_tab_undo_unrestrict
:
3240 return perform_undo_var(tab
, undo
);
3241 case isl_tab_undo_bmap_eq
:
3242 return isl_basic_map_free_equality(tab
->bmap
, 1);
3243 case isl_tab_undo_bmap_ineq
:
3244 return isl_basic_map_free_inequality(tab
->bmap
, 1);
3245 case isl_tab_undo_bmap_div
:
3246 if (isl_basic_map_free_div(tab
->bmap
, 1) < 0)
3249 tab
->samples
->n_col
--;
3251 case isl_tab_undo_saved_basis
:
3252 if (restore_basis(tab
, undo
->u
.col_var
) < 0)
3255 case isl_tab_undo_drop_sample
:
3258 case isl_tab_undo_saved_samples
:
3259 drop_samples_since(tab
, undo
->u
.n
);
3261 case isl_tab_undo_callback
:
3262 return undo
->u
.callback
->run(undo
->u
.callback
);
3264 isl_assert(tab
->mat
->ctx
, 0, return -1);
3269 /* Return the tableau to the state it was in when the snapshot "snap"
3272 int isl_tab_rollback(struct isl_tab
*tab
, struct isl_tab_undo
*snap
)
3274 struct isl_tab_undo
*undo
, *next
;
3280 for (undo
= tab
->top
; undo
&& undo
!= &tab
->bottom
; undo
= next
) {
3284 if (perform_undo(tab
, undo
) < 0) {
3290 free_undo_record(undo
);
3299 /* The given row "row" represents an inequality violated by all
3300 * points in the tableau. Check for some special cases of such
3301 * separating constraints.
3302 * In particular, if the row has been reduced to the constant -1,
3303 * then we know the inequality is adjacent (but opposite) to
3304 * an equality in the tableau.
3305 * If the row has been reduced to r = c*(-1 -r'), with r' an inequality
3306 * of the tableau and c a positive constant, then the inequality
3307 * is adjacent (but opposite) to the inequality r'.
3309 static enum isl_ineq_type
separation_type(struct isl_tab
*tab
, unsigned row
)
3312 unsigned off
= 2 + tab
->M
;
3315 return isl_ineq_separate
;
3317 if (!isl_int_is_one(tab
->mat
->row
[row
][0]))
3318 return isl_ineq_separate
;
3320 pos
= isl_seq_first_non_zero(tab
->mat
->row
[row
] + off
+ tab
->n_dead
,
3321 tab
->n_col
- tab
->n_dead
);
3323 if (isl_int_is_negone(tab
->mat
->row
[row
][1]))
3324 return isl_ineq_adj_eq
;
3326 return isl_ineq_separate
;
3329 if (!isl_int_eq(tab
->mat
->row
[row
][1],
3330 tab
->mat
->row
[row
][off
+ tab
->n_dead
+ pos
]))
3331 return isl_ineq_separate
;
3333 pos
= isl_seq_first_non_zero(
3334 tab
->mat
->row
[row
] + off
+ tab
->n_dead
+ pos
+ 1,
3335 tab
->n_col
- tab
->n_dead
- pos
- 1);
3337 return pos
== -1 ? isl_ineq_adj_ineq
: isl_ineq_separate
;
3340 /* Check the effect of inequality "ineq" on the tableau "tab".
3342 * isl_ineq_redundant: satisfied by all points in the tableau
3343 * isl_ineq_separate: satisfied by no point in the tableau
3344 * isl_ineq_cut: satisfied by some by not all points
3345 * isl_ineq_adj_eq: adjacent to an equality
3346 * isl_ineq_adj_ineq: adjacent to an inequality.
3348 enum isl_ineq_type
isl_tab_ineq_type(struct isl_tab
*tab
, isl_int
*ineq
)
3350 enum isl_ineq_type type
= isl_ineq_error
;
3351 struct isl_tab_undo
*snap
= NULL
;
3356 return isl_ineq_error
;
3358 if (isl_tab_extend_cons(tab
, 1) < 0)
3359 return isl_ineq_error
;
3361 snap
= isl_tab_snap(tab
);
3363 con
= isl_tab_add_row(tab
, ineq
);
3367 row
= tab
->con
[con
].index
;
3368 if (isl_tab_row_is_redundant(tab
, row
))
3369 type
= isl_ineq_redundant
;
3370 else if (isl_int_is_neg(tab
->mat
->row
[row
][1]) &&
3372 isl_int_abs_ge(tab
->mat
->row
[row
][1],
3373 tab
->mat
->row
[row
][0]))) {
3374 int nonneg
= at_least_zero(tab
, &tab
->con
[con
]);
3378 type
= isl_ineq_cut
;
3380 type
= separation_type(tab
, row
);
3382 int red
= con_is_redundant(tab
, &tab
->con
[con
]);
3386 type
= isl_ineq_cut
;
3388 type
= isl_ineq_redundant
;
3391 if (isl_tab_rollback(tab
, snap
))
3392 return isl_ineq_error
;
3395 return isl_ineq_error
;
3398 int isl_tab_track_bmap(struct isl_tab
*tab
, __isl_take isl_basic_map
*bmap
)
3400 bmap
= isl_basic_map_cow(bmap
);
3405 bmap
= isl_basic_map_set_to_empty(bmap
);
3412 isl_assert(tab
->mat
->ctx
, tab
->n_eq
== bmap
->n_eq
, goto error
);
3413 isl_assert(tab
->mat
->ctx
,
3414 tab
->n_con
== bmap
->n_eq
+ bmap
->n_ineq
, goto error
);
3420 isl_basic_map_free(bmap
);
3424 int isl_tab_track_bset(struct isl_tab
*tab
, __isl_take isl_basic_set
*bset
)
3426 return isl_tab_track_bmap(tab
, (isl_basic_map
*)bset
);
3429 __isl_keep isl_basic_set
*isl_tab_peek_bset(struct isl_tab
*tab
)
3434 return (isl_basic_set
*)tab
->bmap
;
3437 static void isl_tab_print_internal(__isl_keep
struct isl_tab
*tab
,
3438 FILE *out
, int indent
)
3444 fprintf(out
, "%*snull tab\n", indent
, "");
3447 fprintf(out
, "%*sn_redundant: %d, n_dead: %d", indent
, "",
3448 tab
->n_redundant
, tab
->n_dead
);
3450 fprintf(out
, ", rational");
3452 fprintf(out
, ", empty");
3454 fprintf(out
, "%*s[", indent
, "");
3455 for (i
= 0; i
< tab
->n_var
; ++i
) {
3457 fprintf(out
, (i
== tab
->n_param
||
3458 i
== tab
->n_var
- tab
->n_div
) ? "; "
3460 fprintf(out
, "%c%d%s", tab
->var
[i
].is_row
? 'r' : 'c',
3462 tab
->var
[i
].is_zero
? " [=0]" :
3463 tab
->var
[i
].is_redundant
? " [R]" : "");
3465 fprintf(out
, "]\n");
3466 fprintf(out
, "%*s[", indent
, "");
3467 for (i
= 0; i
< tab
->n_con
; ++i
) {
3470 fprintf(out
, "%c%d%s", tab
->con
[i
].is_row
? 'r' : 'c',
3472 tab
->con
[i
].is_zero
? " [=0]" :
3473 tab
->con
[i
].is_redundant
? " [R]" : "");
3475 fprintf(out
, "]\n");
3476 fprintf(out
, "%*s[", indent
, "");
3477 for (i
= 0; i
< tab
->n_row
; ++i
) {
3478 const char *sign
= "";
3481 if (tab
->row_sign
) {
3482 if (tab
->row_sign
[i
] == isl_tab_row_unknown
)
3484 else if (tab
->row_sign
[i
] == isl_tab_row_neg
)
3486 else if (tab
->row_sign
[i
] == isl_tab_row_pos
)
3491 fprintf(out
, "r%d: %d%s%s", i
, tab
->row_var
[i
],
3492 isl_tab_var_from_row(tab
, i
)->is_nonneg
? " [>=0]" : "", sign
);
3494 fprintf(out
, "]\n");
3495 fprintf(out
, "%*s[", indent
, "");
3496 for (i
= 0; i
< tab
->n_col
; ++i
) {
3499 fprintf(out
, "c%d: %d%s", i
, tab
->col_var
[i
],
3500 var_from_col(tab
, i
)->is_nonneg
? " [>=0]" : "");
3502 fprintf(out
, "]\n");
3503 r
= tab
->mat
->n_row
;
3504 tab
->mat
->n_row
= tab
->n_row
;
3505 c
= tab
->mat
->n_col
;
3506 tab
->mat
->n_col
= 2 + tab
->M
+ tab
->n_col
;
3507 isl_mat_print_internal(tab
->mat
, out
, indent
);
3508 tab
->mat
->n_row
= r
;
3509 tab
->mat
->n_col
= c
;
3511 isl_basic_map_print_internal(tab
->bmap
, out
, indent
);
3514 void isl_tab_dump(__isl_keep
struct isl_tab
*tab
)
3516 isl_tab_print_internal(tab
, stderr
, 0);