isl_tab_rollback: return isl_stat
[isl.git] / isl_tab.c
blobccdd91d1beea7ecbab83263684816fc27839a7cf
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2013 Ecole Normale Superieure
4 * Copyright 2014 INRIA Rocquencourt
5 * Copyright 2016 Sven Verdoolaege
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, K.U.Leuven, Departement
10 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
11 * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
12 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
13 * B.P. 105 - 78153 Le Chesnay, France
16 #include <isl_ctx_private.h>
17 #include <isl_mat_private.h>
18 #include <isl_vec_private.h>
19 #include "isl_map_private.h"
20 #include "isl_tab.h"
21 #include <isl_seq.h>
22 #include <isl_config.h>
24 #include <bset_to_bmap.c>
25 #include <bset_from_bmap.c>
28 * The implementation of tableaus in this file was inspired by Section 8
29 * of David Detlefs, Greg Nelson and James B. Saxe, "Simplify: a theorem
30 * prover for program checking".
33 struct isl_tab *isl_tab_alloc(struct isl_ctx *ctx,
34 unsigned n_row, unsigned n_var, unsigned M)
36 int i;
37 struct isl_tab *tab;
38 unsigned off = 2 + M;
40 tab = isl_calloc_type(ctx, struct isl_tab);
41 if (!tab)
42 return NULL;
43 tab->mat = isl_mat_alloc(ctx, n_row, off + n_var);
44 if (!tab->mat)
45 goto error;
46 tab->var = isl_alloc_array(ctx, struct isl_tab_var, n_var);
47 if (n_var && !tab->var)
48 goto error;
49 tab->con = isl_alloc_array(ctx, struct isl_tab_var, n_row);
50 if (n_row && !tab->con)
51 goto error;
52 tab->col_var = isl_alloc_array(ctx, int, n_var);
53 if (n_var && !tab->col_var)
54 goto error;
55 tab->row_var = isl_alloc_array(ctx, int, n_row);
56 if (n_row && !tab->row_var)
57 goto error;
58 for (i = 0; i < n_var; ++i) {
59 tab->var[i].index = i;
60 tab->var[i].is_row = 0;
61 tab->var[i].is_nonneg = 0;
62 tab->var[i].is_zero = 0;
63 tab->var[i].is_redundant = 0;
64 tab->var[i].frozen = 0;
65 tab->var[i].negated = 0;
66 tab->col_var[i] = i;
68 tab->n_row = 0;
69 tab->n_con = 0;
70 tab->n_eq = 0;
71 tab->max_con = n_row;
72 tab->n_col = n_var;
73 tab->n_var = n_var;
74 tab->max_var = n_var;
75 tab->n_param = 0;
76 tab->n_div = 0;
77 tab->n_dead = 0;
78 tab->n_redundant = 0;
79 tab->strict_redundant = 0;
80 tab->need_undo = 0;
81 tab->rational = 0;
82 tab->empty = 0;
83 tab->in_undo = 0;
84 tab->M = M;
85 tab->cone = 0;
86 tab->bottom.type = isl_tab_undo_bottom;
87 tab->bottom.next = NULL;
88 tab->top = &tab->bottom;
90 tab->n_zero = 0;
91 tab->n_unbounded = 0;
92 tab->basis = NULL;
94 return tab;
95 error:
96 isl_tab_free(tab);
97 return NULL;
100 isl_ctx *isl_tab_get_ctx(struct isl_tab *tab)
102 return tab ? isl_mat_get_ctx(tab->mat) : NULL;
105 int isl_tab_extend_cons(struct isl_tab *tab, unsigned n_new)
107 unsigned off;
109 if (!tab)
110 return -1;
112 off = 2 + tab->M;
114 if (tab->max_con < tab->n_con + n_new) {
115 struct isl_tab_var *con;
117 con = isl_realloc_array(tab->mat->ctx, tab->con,
118 struct isl_tab_var, tab->max_con + n_new);
119 if (!con)
120 return -1;
121 tab->con = con;
122 tab->max_con += n_new;
124 if (tab->mat->n_row < tab->n_row + n_new) {
125 int *row_var;
127 tab->mat = isl_mat_extend(tab->mat,
128 tab->n_row + n_new, off + tab->n_col);
129 if (!tab->mat)
130 return -1;
131 row_var = isl_realloc_array(tab->mat->ctx, tab->row_var,
132 int, tab->mat->n_row);
133 if (!row_var)
134 return -1;
135 tab->row_var = row_var;
136 if (tab->row_sign) {
137 enum isl_tab_row_sign *s;
138 s = isl_realloc_array(tab->mat->ctx, tab->row_sign,
139 enum isl_tab_row_sign, tab->mat->n_row);
140 if (!s)
141 return -1;
142 tab->row_sign = s;
145 return 0;
148 /* Make room for at least n_new extra variables.
149 * Return -1 if anything went wrong.
151 int isl_tab_extend_vars(struct isl_tab *tab, unsigned n_new)
153 struct isl_tab_var *var;
154 unsigned off = 2 + tab->M;
156 if (tab->max_var < tab->n_var + n_new) {
157 var = isl_realloc_array(tab->mat->ctx, tab->var,
158 struct isl_tab_var, tab->n_var + n_new);
159 if (!var)
160 return -1;
161 tab->var = var;
162 tab->max_var = tab->n_var + n_new;
165 if (tab->mat->n_col < off + tab->n_col + n_new) {
166 int *p;
168 tab->mat = isl_mat_extend(tab->mat,
169 tab->mat->n_row, off + tab->n_col + n_new);
170 if (!tab->mat)
171 return -1;
172 p = isl_realloc_array(tab->mat->ctx, tab->col_var,
173 int, tab->n_col + n_new);
174 if (!p)
175 return -1;
176 tab->col_var = p;
179 return 0;
182 static void free_undo_record(struct isl_tab_undo *undo)
184 switch (undo->type) {
185 case isl_tab_undo_saved_basis:
186 free(undo->u.col_var);
187 break;
188 default:;
190 free(undo);
193 static void free_undo(struct isl_tab *tab)
195 struct isl_tab_undo *undo, *next;
197 for (undo = tab->top; undo && undo != &tab->bottom; undo = next) {
198 next = undo->next;
199 free_undo_record(undo);
201 tab->top = undo;
204 void isl_tab_free(struct isl_tab *tab)
206 if (!tab)
207 return;
208 free_undo(tab);
209 isl_mat_free(tab->mat);
210 isl_vec_free(tab->dual);
211 isl_basic_map_free(tab->bmap);
212 free(tab->var);
213 free(tab->con);
214 free(tab->row_var);
215 free(tab->col_var);
216 free(tab->row_sign);
217 isl_mat_free(tab->samples);
218 free(tab->sample_index);
219 isl_mat_free(tab->basis);
220 free(tab);
223 struct isl_tab *isl_tab_dup(struct isl_tab *tab)
225 int i;
226 struct isl_tab *dup;
227 unsigned off;
229 if (!tab)
230 return NULL;
232 off = 2 + tab->M;
233 dup = isl_calloc_type(tab->mat->ctx, struct isl_tab);
234 if (!dup)
235 return NULL;
236 dup->mat = isl_mat_dup(tab->mat);
237 if (!dup->mat)
238 goto error;
239 dup->var = isl_alloc_array(tab->mat->ctx, struct isl_tab_var, tab->max_var);
240 if (tab->max_var && !dup->var)
241 goto error;
242 for (i = 0; i < tab->n_var; ++i)
243 dup->var[i] = tab->var[i];
244 dup->con = isl_alloc_array(tab->mat->ctx, struct isl_tab_var, tab->max_con);
245 if (tab->max_con && !dup->con)
246 goto error;
247 for (i = 0; i < tab->n_con; ++i)
248 dup->con[i] = tab->con[i];
249 dup->col_var = isl_alloc_array(tab->mat->ctx, int, tab->mat->n_col - off);
250 if ((tab->mat->n_col - off) && !dup->col_var)
251 goto error;
252 for (i = 0; i < tab->n_col; ++i)
253 dup->col_var[i] = tab->col_var[i];
254 dup->row_var = isl_alloc_array(tab->mat->ctx, int, tab->mat->n_row);
255 if (tab->mat->n_row && !dup->row_var)
256 goto error;
257 for (i = 0; i < tab->n_row; ++i)
258 dup->row_var[i] = tab->row_var[i];
259 if (tab->row_sign) {
260 dup->row_sign = isl_alloc_array(tab->mat->ctx, enum isl_tab_row_sign,
261 tab->mat->n_row);
262 if (tab->mat->n_row && !dup->row_sign)
263 goto error;
264 for (i = 0; i < tab->n_row; ++i)
265 dup->row_sign[i] = tab->row_sign[i];
267 if (tab->samples) {
268 dup->samples = isl_mat_dup(tab->samples);
269 if (!dup->samples)
270 goto error;
271 dup->sample_index = isl_alloc_array(tab->mat->ctx, int,
272 tab->samples->n_row);
273 if (tab->samples->n_row && !dup->sample_index)
274 goto error;
275 dup->n_sample = tab->n_sample;
276 dup->n_outside = tab->n_outside;
278 dup->n_row = tab->n_row;
279 dup->n_con = tab->n_con;
280 dup->n_eq = tab->n_eq;
281 dup->max_con = tab->max_con;
282 dup->n_col = tab->n_col;
283 dup->n_var = tab->n_var;
284 dup->max_var = tab->max_var;
285 dup->n_param = tab->n_param;
286 dup->n_div = tab->n_div;
287 dup->n_dead = tab->n_dead;
288 dup->n_redundant = tab->n_redundant;
289 dup->rational = tab->rational;
290 dup->empty = tab->empty;
291 dup->strict_redundant = 0;
292 dup->need_undo = 0;
293 dup->in_undo = 0;
294 dup->M = tab->M;
295 tab->cone = tab->cone;
296 dup->bottom.type = isl_tab_undo_bottom;
297 dup->bottom.next = NULL;
298 dup->top = &dup->bottom;
300 dup->n_zero = tab->n_zero;
301 dup->n_unbounded = tab->n_unbounded;
302 dup->basis = isl_mat_dup(tab->basis);
304 return dup;
305 error:
306 isl_tab_free(dup);
307 return NULL;
310 /* Construct the coefficient matrix of the product tableau
311 * of two tableaus.
312 * mat{1,2} is the coefficient matrix of tableau {1,2}
313 * row{1,2} is the number of rows in tableau {1,2}
314 * col{1,2} is the number of columns in tableau {1,2}
315 * off is the offset to the coefficient column (skipping the
316 * denominator, the constant term and the big parameter if any)
317 * r{1,2} is the number of redundant rows in tableau {1,2}
318 * d{1,2} is the number of dead columns in tableau {1,2}
320 * The order of the rows and columns in the result is as explained
321 * in isl_tab_product.
323 static struct isl_mat *tab_mat_product(struct isl_mat *mat1,
324 struct isl_mat *mat2, unsigned row1, unsigned row2,
325 unsigned col1, unsigned col2,
326 unsigned off, unsigned r1, unsigned r2, unsigned d1, unsigned d2)
328 int i;
329 struct isl_mat *prod;
330 unsigned n;
332 prod = isl_mat_alloc(mat1->ctx, mat1->n_row + mat2->n_row,
333 off + col1 + col2);
334 if (!prod)
335 return NULL;
337 n = 0;
338 for (i = 0; i < r1; ++i) {
339 isl_seq_cpy(prod->row[n + i], mat1->row[i], off + d1);
340 isl_seq_clr(prod->row[n + i] + off + d1, d2);
341 isl_seq_cpy(prod->row[n + i] + off + d1 + d2,
342 mat1->row[i] + off + d1, col1 - d1);
343 isl_seq_clr(prod->row[n + i] + off + col1 + d1, col2 - d2);
346 n += r1;
347 for (i = 0; i < r2; ++i) {
348 isl_seq_cpy(prod->row[n + i], mat2->row[i], off);
349 isl_seq_clr(prod->row[n + i] + off, d1);
350 isl_seq_cpy(prod->row[n + i] + off + d1,
351 mat2->row[i] + off, d2);
352 isl_seq_clr(prod->row[n + i] + off + d1 + d2, col1 - d1);
353 isl_seq_cpy(prod->row[n + i] + off + col1 + d1,
354 mat2->row[i] + off + d2, col2 - d2);
357 n += r2;
358 for (i = 0; i < row1 - r1; ++i) {
359 isl_seq_cpy(prod->row[n + i], mat1->row[r1 + i], off + d1);
360 isl_seq_clr(prod->row[n + i] + off + d1, d2);
361 isl_seq_cpy(prod->row[n + i] + off + d1 + d2,
362 mat1->row[r1 + i] + off + d1, col1 - d1);
363 isl_seq_clr(prod->row[n + i] + off + col1 + d1, col2 - d2);
366 n += row1 - r1;
367 for (i = 0; i < row2 - r2; ++i) {
368 isl_seq_cpy(prod->row[n + i], mat2->row[r2 + i], off);
369 isl_seq_clr(prod->row[n + i] + off, d1);
370 isl_seq_cpy(prod->row[n + i] + off + d1,
371 mat2->row[r2 + i] + off, d2);
372 isl_seq_clr(prod->row[n + i] + off + d1 + d2, col1 - d1);
373 isl_seq_cpy(prod->row[n + i] + off + col1 + d1,
374 mat2->row[r2 + i] + off + d2, col2 - d2);
377 return prod;
380 /* Update the row or column index of a variable that corresponds
381 * to a variable in the first input tableau.
383 static void update_index1(struct isl_tab_var *var,
384 unsigned r1, unsigned r2, unsigned d1, unsigned d2)
386 if (var->index == -1)
387 return;
388 if (var->is_row && var->index >= r1)
389 var->index += r2;
390 if (!var->is_row && var->index >= d1)
391 var->index += d2;
394 /* Update the row or column index of a variable that corresponds
395 * to a variable in the second input tableau.
397 static void update_index2(struct isl_tab_var *var,
398 unsigned row1, unsigned col1,
399 unsigned r1, unsigned r2, unsigned d1, unsigned d2)
401 if (var->index == -1)
402 return;
403 if (var->is_row) {
404 if (var->index < r2)
405 var->index += r1;
406 else
407 var->index += row1;
408 } else {
409 if (var->index < d2)
410 var->index += d1;
411 else
412 var->index += col1;
416 /* Create a tableau that represents the Cartesian product of the sets
417 * represented by tableaus tab1 and tab2.
418 * The order of the rows in the product is
419 * - redundant rows of tab1
420 * - redundant rows of tab2
421 * - non-redundant rows of tab1
422 * - non-redundant rows of tab2
423 * The order of the columns is
424 * - denominator
425 * - constant term
426 * - coefficient of big parameter, if any
427 * - dead columns of tab1
428 * - dead columns of tab2
429 * - live columns of tab1
430 * - live columns of tab2
431 * The order of the variables and the constraints is a concatenation
432 * of order in the two input tableaus.
434 struct isl_tab *isl_tab_product(struct isl_tab *tab1, struct isl_tab *tab2)
436 int i;
437 struct isl_tab *prod;
438 unsigned off;
439 unsigned r1, r2, d1, d2;
441 if (!tab1 || !tab2)
442 return NULL;
444 isl_assert(tab1->mat->ctx, tab1->M == tab2->M, return NULL);
445 isl_assert(tab1->mat->ctx, tab1->rational == tab2->rational, return NULL);
446 isl_assert(tab1->mat->ctx, tab1->cone == tab2->cone, return NULL);
447 isl_assert(tab1->mat->ctx, !tab1->row_sign, return NULL);
448 isl_assert(tab1->mat->ctx, !tab2->row_sign, return NULL);
449 isl_assert(tab1->mat->ctx, tab1->n_param == 0, return NULL);
450 isl_assert(tab1->mat->ctx, tab2->n_param == 0, return NULL);
451 isl_assert(tab1->mat->ctx, tab1->n_div == 0, return NULL);
452 isl_assert(tab1->mat->ctx, tab2->n_div == 0, return NULL);
454 off = 2 + tab1->M;
455 r1 = tab1->n_redundant;
456 r2 = tab2->n_redundant;
457 d1 = tab1->n_dead;
458 d2 = tab2->n_dead;
459 prod = isl_calloc_type(tab1->mat->ctx, struct isl_tab);
460 if (!prod)
461 return NULL;
462 prod->mat = tab_mat_product(tab1->mat, tab2->mat,
463 tab1->n_row, tab2->n_row,
464 tab1->n_col, tab2->n_col, off, r1, r2, d1, d2);
465 if (!prod->mat)
466 goto error;
467 prod->var = isl_alloc_array(tab1->mat->ctx, struct isl_tab_var,
468 tab1->max_var + tab2->max_var);
469 if ((tab1->max_var + tab2->max_var) && !prod->var)
470 goto error;
471 for (i = 0; i < tab1->n_var; ++i) {
472 prod->var[i] = tab1->var[i];
473 update_index1(&prod->var[i], r1, r2, d1, d2);
475 for (i = 0; i < tab2->n_var; ++i) {
476 prod->var[tab1->n_var + i] = tab2->var[i];
477 update_index2(&prod->var[tab1->n_var + i],
478 tab1->n_row, tab1->n_col,
479 r1, r2, d1, d2);
481 prod->con = isl_alloc_array(tab1->mat->ctx, struct isl_tab_var,
482 tab1->max_con + tab2->max_con);
483 if ((tab1->max_con + tab2->max_con) && !prod->con)
484 goto error;
485 for (i = 0; i < tab1->n_con; ++i) {
486 prod->con[i] = tab1->con[i];
487 update_index1(&prod->con[i], r1, r2, d1, d2);
489 for (i = 0; i < tab2->n_con; ++i) {
490 prod->con[tab1->n_con + i] = tab2->con[i];
491 update_index2(&prod->con[tab1->n_con + i],
492 tab1->n_row, tab1->n_col,
493 r1, r2, d1, d2);
495 prod->col_var = isl_alloc_array(tab1->mat->ctx, int,
496 tab1->n_col + tab2->n_col);
497 if ((tab1->n_col + tab2->n_col) && !prod->col_var)
498 goto error;
499 for (i = 0; i < tab1->n_col; ++i) {
500 int pos = i < d1 ? i : i + d2;
501 prod->col_var[pos] = tab1->col_var[i];
503 for (i = 0; i < tab2->n_col; ++i) {
504 int pos = i < d2 ? d1 + i : tab1->n_col + i;
505 int t = tab2->col_var[i];
506 if (t >= 0)
507 t += tab1->n_var;
508 else
509 t -= tab1->n_con;
510 prod->col_var[pos] = t;
512 prod->row_var = isl_alloc_array(tab1->mat->ctx, int,
513 tab1->mat->n_row + tab2->mat->n_row);
514 if ((tab1->mat->n_row + tab2->mat->n_row) && !prod->row_var)
515 goto error;
516 for (i = 0; i < tab1->n_row; ++i) {
517 int pos = i < r1 ? i : i + r2;
518 prod->row_var[pos] = tab1->row_var[i];
520 for (i = 0; i < tab2->n_row; ++i) {
521 int pos = i < r2 ? r1 + i : tab1->n_row + i;
522 int t = tab2->row_var[i];
523 if (t >= 0)
524 t += tab1->n_var;
525 else
526 t -= tab1->n_con;
527 prod->row_var[pos] = t;
529 prod->samples = NULL;
530 prod->sample_index = NULL;
531 prod->n_row = tab1->n_row + tab2->n_row;
532 prod->n_con = tab1->n_con + tab2->n_con;
533 prod->n_eq = 0;
534 prod->max_con = tab1->max_con + tab2->max_con;
535 prod->n_col = tab1->n_col + tab2->n_col;
536 prod->n_var = tab1->n_var + tab2->n_var;
537 prod->max_var = tab1->max_var + tab2->max_var;
538 prod->n_param = 0;
539 prod->n_div = 0;
540 prod->n_dead = tab1->n_dead + tab2->n_dead;
541 prod->n_redundant = tab1->n_redundant + tab2->n_redundant;
542 prod->rational = tab1->rational;
543 prod->empty = tab1->empty || tab2->empty;
544 prod->strict_redundant = tab1->strict_redundant || tab2->strict_redundant;
545 prod->need_undo = 0;
546 prod->in_undo = 0;
547 prod->M = tab1->M;
548 prod->cone = tab1->cone;
549 prod->bottom.type = isl_tab_undo_bottom;
550 prod->bottom.next = NULL;
551 prod->top = &prod->bottom;
553 prod->n_zero = 0;
554 prod->n_unbounded = 0;
555 prod->basis = NULL;
557 return prod;
558 error:
559 isl_tab_free(prod);
560 return NULL;
563 static struct isl_tab_var *var_from_index(struct isl_tab *tab, int i)
565 if (i >= 0)
566 return &tab->var[i];
567 else
568 return &tab->con[~i];
571 struct isl_tab_var *isl_tab_var_from_row(struct isl_tab *tab, int i)
573 return var_from_index(tab, tab->row_var[i]);
576 static struct isl_tab_var *var_from_col(struct isl_tab *tab, int i)
578 return var_from_index(tab, tab->col_var[i]);
581 /* Check if there are any upper bounds on column variable "var",
582 * i.e., non-negative rows where var appears with a negative coefficient.
583 * Return 1 if there are no such bounds.
585 static int max_is_manifestly_unbounded(struct isl_tab *tab,
586 struct isl_tab_var *var)
588 int i;
589 unsigned off = 2 + tab->M;
591 if (var->is_row)
592 return 0;
593 for (i = tab->n_redundant; i < tab->n_row; ++i) {
594 if (!isl_int_is_neg(tab->mat->row[i][off + var->index]))
595 continue;
596 if (isl_tab_var_from_row(tab, i)->is_nonneg)
597 return 0;
599 return 1;
602 /* Check if there are any lower bounds on column variable "var",
603 * i.e., non-negative rows where var appears with a positive coefficient.
604 * Return 1 if there are no such bounds.
606 static int min_is_manifestly_unbounded(struct isl_tab *tab,
607 struct isl_tab_var *var)
609 int i;
610 unsigned off = 2 + tab->M;
612 if (var->is_row)
613 return 0;
614 for (i = tab->n_redundant; i < tab->n_row; ++i) {
615 if (!isl_int_is_pos(tab->mat->row[i][off + var->index]))
616 continue;
617 if (isl_tab_var_from_row(tab, i)->is_nonneg)
618 return 0;
620 return 1;
623 static int row_cmp(struct isl_tab *tab, int r1, int r2, int c, isl_int *t)
625 unsigned off = 2 + tab->M;
627 if (tab->M) {
628 int s;
629 isl_int_mul(*t, tab->mat->row[r1][2], tab->mat->row[r2][off+c]);
630 isl_int_submul(*t, tab->mat->row[r2][2], tab->mat->row[r1][off+c]);
631 s = isl_int_sgn(*t);
632 if (s)
633 return s;
635 isl_int_mul(*t, tab->mat->row[r1][1], tab->mat->row[r2][off + c]);
636 isl_int_submul(*t, tab->mat->row[r2][1], tab->mat->row[r1][off + c]);
637 return isl_int_sgn(*t);
640 /* Given the index of a column "c", return the index of a row
641 * that can be used to pivot the column in, with either an increase
642 * (sgn > 0) or a decrease (sgn < 0) of the corresponding variable.
643 * If "var" is not NULL, then the row returned will be different from
644 * the one associated with "var".
646 * Each row in the tableau is of the form
648 * x_r = a_r0 + \sum_i a_ri x_i
650 * Only rows with x_r >= 0 and with the sign of a_ri opposite to "sgn"
651 * impose any limit on the increase or decrease in the value of x_c
652 * and this bound is equal to a_r0 / |a_rc|. We are therefore looking
653 * for the row with the smallest (most stringent) such bound.
654 * Note that the common denominator of each row drops out of the fraction.
655 * To check if row j has a smaller bound than row r, i.e.,
656 * a_j0 / |a_jc| < a_r0 / |a_rc| or a_j0 |a_rc| < a_r0 |a_jc|,
657 * we check if -sign(a_jc) (a_j0 a_rc - a_r0 a_jc) < 0,
658 * where -sign(a_jc) is equal to "sgn".
660 static int pivot_row(struct isl_tab *tab,
661 struct isl_tab_var *var, int sgn, int c)
663 int j, r, tsgn;
664 isl_int t;
665 unsigned off = 2 + tab->M;
667 isl_int_init(t);
668 r = -1;
669 for (j = tab->n_redundant; j < tab->n_row; ++j) {
670 if (var && j == var->index)
671 continue;
672 if (!isl_tab_var_from_row(tab, j)->is_nonneg)
673 continue;
674 if (sgn * isl_int_sgn(tab->mat->row[j][off + c]) >= 0)
675 continue;
676 if (r < 0) {
677 r = j;
678 continue;
680 tsgn = sgn * row_cmp(tab, r, j, c, &t);
681 if (tsgn < 0 || (tsgn == 0 &&
682 tab->row_var[j] < tab->row_var[r]))
683 r = j;
685 isl_int_clear(t);
686 return r;
689 /* Find a pivot (row and col) that will increase (sgn > 0) or decrease
690 * (sgn < 0) the value of row variable var.
691 * If not NULL, then skip_var is a row variable that should be ignored
692 * while looking for a pivot row. It is usually equal to var.
694 * As the given row in the tableau is of the form
696 * x_r = a_r0 + \sum_i a_ri x_i
698 * we need to find a column such that the sign of a_ri is equal to "sgn"
699 * (such that an increase in x_i will have the desired effect) or a
700 * column with a variable that may attain negative values.
701 * If a_ri is positive, then we need to move x_i in the same direction
702 * to obtain the desired effect. Otherwise, x_i has to move in the
703 * opposite direction.
705 static void find_pivot(struct isl_tab *tab,
706 struct isl_tab_var *var, struct isl_tab_var *skip_var,
707 int sgn, int *row, int *col)
709 int j, r, c;
710 isl_int *tr;
712 *row = *col = -1;
714 isl_assert(tab->mat->ctx, var->is_row, return);
715 tr = tab->mat->row[var->index] + 2 + tab->M;
717 c = -1;
718 for (j = tab->n_dead; j < tab->n_col; ++j) {
719 if (isl_int_is_zero(tr[j]))
720 continue;
721 if (isl_int_sgn(tr[j]) != sgn &&
722 var_from_col(tab, j)->is_nonneg)
723 continue;
724 if (c < 0 || tab->col_var[j] < tab->col_var[c])
725 c = j;
727 if (c < 0)
728 return;
730 sgn *= isl_int_sgn(tr[c]);
731 r = pivot_row(tab, skip_var, sgn, c);
732 *row = r < 0 ? var->index : r;
733 *col = c;
736 /* Return 1 if row "row" represents an obviously redundant inequality.
737 * This means
738 * - it represents an inequality or a variable
739 * - that is the sum of a non-negative sample value and a positive
740 * combination of zero or more non-negative constraints.
742 int isl_tab_row_is_redundant(struct isl_tab *tab, int row)
744 int i;
745 unsigned off = 2 + tab->M;
747 if (tab->row_var[row] < 0 && !isl_tab_var_from_row(tab, row)->is_nonneg)
748 return 0;
750 if (isl_int_is_neg(tab->mat->row[row][1]))
751 return 0;
752 if (tab->strict_redundant && isl_int_is_zero(tab->mat->row[row][1]))
753 return 0;
754 if (tab->M && isl_int_is_neg(tab->mat->row[row][2]))
755 return 0;
757 for (i = tab->n_dead; i < tab->n_col; ++i) {
758 if (isl_int_is_zero(tab->mat->row[row][off + i]))
759 continue;
760 if (tab->col_var[i] >= 0)
761 return 0;
762 if (isl_int_is_neg(tab->mat->row[row][off + i]))
763 return 0;
764 if (!var_from_col(tab, i)->is_nonneg)
765 return 0;
767 return 1;
770 static void swap_rows(struct isl_tab *tab, int row1, int row2)
772 int t;
773 enum isl_tab_row_sign s;
775 t = tab->row_var[row1];
776 tab->row_var[row1] = tab->row_var[row2];
777 tab->row_var[row2] = t;
778 isl_tab_var_from_row(tab, row1)->index = row1;
779 isl_tab_var_from_row(tab, row2)->index = row2;
780 tab->mat = isl_mat_swap_rows(tab->mat, row1, row2);
782 if (!tab->row_sign)
783 return;
784 s = tab->row_sign[row1];
785 tab->row_sign[row1] = tab->row_sign[row2];
786 tab->row_sign[row2] = s;
789 static isl_stat push_union(struct isl_tab *tab,
790 enum isl_tab_undo_type type, union isl_tab_undo_val u) WARN_UNUSED;
792 /* Push record "u" onto the undo stack of "tab", provided "tab"
793 * keeps track of undo information.
795 * If the record cannot be pushed, then mark the undo stack as invalid
796 * such that a later rollback attempt will not try to undo earlier
797 * records without having been able to undo the current record.
799 static isl_stat push_union(struct isl_tab *tab,
800 enum isl_tab_undo_type type, union isl_tab_undo_val u)
802 struct isl_tab_undo *undo;
804 if (!tab)
805 return isl_stat_error;
806 if (!tab->need_undo)
807 return isl_stat_ok;
809 undo = isl_alloc_type(tab->mat->ctx, struct isl_tab_undo);
810 if (!undo)
811 goto error;
812 undo->type = type;
813 undo->u = u;
814 undo->next = tab->top;
815 tab->top = undo;
817 return isl_stat_ok;
818 error:
819 free_undo(tab);
820 tab->top = NULL;
821 return isl_stat_error;
824 isl_stat isl_tab_push_var(struct isl_tab *tab,
825 enum isl_tab_undo_type type, struct isl_tab_var *var)
827 union isl_tab_undo_val u;
828 if (var->is_row)
829 u.var_index = tab->row_var[var->index];
830 else
831 u.var_index = tab->col_var[var->index];
832 return push_union(tab, type, u);
835 isl_stat isl_tab_push(struct isl_tab *tab, enum isl_tab_undo_type type)
837 union isl_tab_undo_val u = { 0 };
838 return push_union(tab, type, u);
841 /* Push a record on the undo stack describing the current basic
842 * variables, so that the this state can be restored during rollback.
844 isl_stat isl_tab_push_basis(struct isl_tab *tab)
846 int i;
847 union isl_tab_undo_val u;
849 u.col_var = isl_alloc_array(tab->mat->ctx, int, tab->n_col);
850 if (tab->n_col && !u.col_var)
851 return isl_stat_error;
852 for (i = 0; i < tab->n_col; ++i)
853 u.col_var[i] = tab->col_var[i];
854 return push_union(tab, isl_tab_undo_saved_basis, u);
857 isl_stat isl_tab_push_callback(struct isl_tab *tab,
858 struct isl_tab_callback *callback)
860 union isl_tab_undo_val u;
861 u.callback = callback;
862 return push_union(tab, isl_tab_undo_callback, u);
865 struct isl_tab *isl_tab_init_samples(struct isl_tab *tab)
867 if (!tab)
868 return NULL;
870 tab->n_sample = 0;
871 tab->n_outside = 0;
872 tab->samples = isl_mat_alloc(tab->mat->ctx, 1, 1 + tab->n_var);
873 if (!tab->samples)
874 goto error;
875 tab->sample_index = isl_alloc_array(tab->mat->ctx, int, 1);
876 if (!tab->sample_index)
877 goto error;
878 return tab;
879 error:
880 isl_tab_free(tab);
881 return NULL;
884 int isl_tab_add_sample(struct isl_tab *tab, __isl_take isl_vec *sample)
886 if (!tab || !sample)
887 goto error;
889 if (tab->n_sample + 1 > tab->samples->n_row) {
890 int *t = isl_realloc_array(tab->mat->ctx,
891 tab->sample_index, int, tab->n_sample + 1);
892 if (!t)
893 goto error;
894 tab->sample_index = t;
897 tab->samples = isl_mat_extend(tab->samples,
898 tab->n_sample + 1, tab->samples->n_col);
899 if (!tab->samples)
900 goto error;
902 isl_seq_cpy(tab->samples->row[tab->n_sample], sample->el, sample->size);
903 isl_vec_free(sample);
904 tab->sample_index[tab->n_sample] = tab->n_sample;
905 tab->n_sample++;
907 return 0;
908 error:
909 isl_vec_free(sample);
910 return -1;
913 struct isl_tab *isl_tab_drop_sample(struct isl_tab *tab, int s)
915 if (s != tab->n_outside) {
916 int t = tab->sample_index[tab->n_outside];
917 tab->sample_index[tab->n_outside] = tab->sample_index[s];
918 tab->sample_index[s] = t;
919 isl_mat_swap_rows(tab->samples, tab->n_outside, s);
921 tab->n_outside++;
922 if (isl_tab_push(tab, isl_tab_undo_drop_sample) < 0) {
923 isl_tab_free(tab);
924 return NULL;
927 return tab;
930 /* Record the current number of samples so that we can remove newer
931 * samples during a rollback.
933 isl_stat isl_tab_save_samples(struct isl_tab *tab)
935 union isl_tab_undo_val u;
937 if (!tab)
938 return isl_stat_error;
940 u.n = tab->n_sample;
941 return push_union(tab, isl_tab_undo_saved_samples, u);
944 /* Mark row with index "row" as being redundant.
945 * If we may need to undo the operation or if the row represents
946 * a variable of the original problem, the row is kept,
947 * but no longer considered when looking for a pivot row.
948 * Otherwise, the row is simply removed.
950 * The row may be interchanged with some other row. If it
951 * is interchanged with a later row, return 1. Otherwise return 0.
952 * If the rows are checked in order in the calling function,
953 * then a return value of 1 means that the row with the given
954 * row number may now contain a different row that hasn't been checked yet.
956 int isl_tab_mark_redundant(struct isl_tab *tab, int row)
958 struct isl_tab_var *var = isl_tab_var_from_row(tab, row);
959 var->is_redundant = 1;
960 isl_assert(tab->mat->ctx, row >= tab->n_redundant, return -1);
961 if (tab->preserve || tab->need_undo || tab->row_var[row] >= 0) {
962 if (tab->row_var[row] >= 0 && !var->is_nonneg) {
963 var->is_nonneg = 1;
964 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, var) < 0)
965 return -1;
967 if (row != tab->n_redundant)
968 swap_rows(tab, row, tab->n_redundant);
969 tab->n_redundant++;
970 return isl_tab_push_var(tab, isl_tab_undo_redundant, var);
971 } else {
972 if (row != tab->n_row - 1)
973 swap_rows(tab, row, tab->n_row - 1);
974 isl_tab_var_from_row(tab, tab->n_row - 1)->index = -1;
975 tab->n_row--;
976 return 1;
980 /* Mark "tab" as a rational tableau.
981 * If it wasn't marked as a rational tableau already and if we may
982 * need to undo changes, then arrange for the marking to be undone
983 * during the undo.
985 int isl_tab_mark_rational(struct isl_tab *tab)
987 if (!tab)
988 return -1;
989 if (!tab->rational && tab->need_undo)
990 if (isl_tab_push(tab, isl_tab_undo_rational) < 0)
991 return -1;
992 tab->rational = 1;
993 return 0;
996 isl_stat isl_tab_mark_empty(struct isl_tab *tab)
998 if (!tab)
999 return isl_stat_error;
1000 if (!tab->empty && tab->need_undo)
1001 if (isl_tab_push(tab, isl_tab_undo_empty) < 0)
1002 return isl_stat_error;
1003 tab->empty = 1;
1004 return isl_stat_ok;
1007 int isl_tab_freeze_constraint(struct isl_tab *tab, int con)
1009 struct isl_tab_var *var;
1011 if (!tab)
1012 return -1;
1014 var = &tab->con[con];
1015 if (var->frozen)
1016 return 0;
1017 if (var->index < 0)
1018 return 0;
1019 var->frozen = 1;
1021 if (tab->need_undo)
1022 return isl_tab_push_var(tab, isl_tab_undo_freeze, var);
1024 return 0;
1027 /* Update the rows signs after a pivot of "row" and "col", with "row_sgn"
1028 * the original sign of the pivot element.
1029 * We only keep track of row signs during PILP solving and in this case
1030 * we only pivot a row with negative sign (meaning the value is always
1031 * non-positive) using a positive pivot element.
1033 * For each row j, the new value of the parametric constant is equal to
1035 * a_j0 - a_jc a_r0/a_rc
1037 * where a_j0 is the original parametric constant, a_rc is the pivot element,
1038 * a_r0 is the parametric constant of the pivot row and a_jc is the
1039 * pivot column entry of the row j.
1040 * Since a_r0 is non-positive and a_rc is positive, the sign of row j
1041 * remains the same if a_jc has the same sign as the row j or if
1042 * a_jc is zero. In all other cases, we reset the sign to "unknown".
1044 static void update_row_sign(struct isl_tab *tab, int row, int col, int row_sgn)
1046 int i;
1047 struct isl_mat *mat = tab->mat;
1048 unsigned off = 2 + tab->M;
1050 if (!tab->row_sign)
1051 return;
1053 if (tab->row_sign[row] == 0)
1054 return;
1055 isl_assert(mat->ctx, row_sgn > 0, return);
1056 isl_assert(mat->ctx, tab->row_sign[row] == isl_tab_row_neg, return);
1057 tab->row_sign[row] = isl_tab_row_pos;
1058 for (i = 0; i < tab->n_row; ++i) {
1059 int s;
1060 if (i == row)
1061 continue;
1062 s = isl_int_sgn(mat->row[i][off + col]);
1063 if (!s)
1064 continue;
1065 if (!tab->row_sign[i])
1066 continue;
1067 if (s < 0 && tab->row_sign[i] == isl_tab_row_neg)
1068 continue;
1069 if (s > 0 && tab->row_sign[i] == isl_tab_row_pos)
1070 continue;
1071 tab->row_sign[i] = isl_tab_row_unknown;
1075 /* Given a row number "row" and a column number "col", pivot the tableau
1076 * such that the associated variables are interchanged.
1077 * The given row in the tableau expresses
1079 * x_r = a_r0 + \sum_i a_ri x_i
1081 * or
1083 * x_c = 1/a_rc x_r - a_r0/a_rc + sum_{i \ne r} -a_ri/a_rc
1085 * Substituting this equality into the other rows
1087 * x_j = a_j0 + \sum_i a_ji x_i
1089 * with a_jc \ne 0, we obtain
1091 * 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
1093 * The tableau
1095 * n_rc/d_r n_ri/d_r
1096 * n_jc/d_j n_ji/d_j
1098 * where i is any other column and j is any other row,
1099 * is therefore transformed into
1101 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1102 * 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)
1104 * The transformation is performed along the following steps
1106 * d_r/n_rc n_ri/n_rc
1107 * n_jc/d_j n_ji/d_j
1109 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1110 * n_jc/d_j n_ji/d_j
1112 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1113 * n_jc/(|n_rc| d_j) n_ji/(|n_rc| d_j)
1115 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1116 * n_jc/(|n_rc| d_j) (n_ji |n_rc|)/(|n_rc| d_j)
1118 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1119 * n_jc/(|n_rc| d_j) (n_ji |n_rc| - s(n_rc)n_jc n_ri)/(|n_rc| d_j)
1121 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1122 * 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)
1125 int isl_tab_pivot(struct isl_tab *tab, int row, int col)
1127 int i, j;
1128 int sgn;
1129 int t;
1130 isl_ctx *ctx;
1131 struct isl_mat *mat = tab->mat;
1132 struct isl_tab_var *var;
1133 unsigned off = 2 + tab->M;
1135 ctx = isl_tab_get_ctx(tab);
1136 if (isl_ctx_next_operation(ctx) < 0)
1137 return -1;
1139 isl_int_swap(mat->row[row][0], mat->row[row][off + col]);
1140 sgn = isl_int_sgn(mat->row[row][0]);
1141 if (sgn < 0) {
1142 isl_int_neg(mat->row[row][0], mat->row[row][0]);
1143 isl_int_neg(mat->row[row][off + col], mat->row[row][off + col]);
1144 } else
1145 for (j = 0; j < off - 1 + tab->n_col; ++j) {
1146 if (j == off - 1 + col)
1147 continue;
1148 isl_int_neg(mat->row[row][1 + j], mat->row[row][1 + j]);
1150 if (!isl_int_is_one(mat->row[row][0]))
1151 isl_seq_normalize(mat->ctx, mat->row[row], off + tab->n_col);
1152 for (i = 0; i < tab->n_row; ++i) {
1153 if (i == row)
1154 continue;
1155 if (isl_int_is_zero(mat->row[i][off + col]))
1156 continue;
1157 isl_int_mul(mat->row[i][0], mat->row[i][0], mat->row[row][0]);
1158 for (j = 0; j < off - 1 + tab->n_col; ++j) {
1159 if (j == off - 1 + col)
1160 continue;
1161 isl_int_mul(mat->row[i][1 + j],
1162 mat->row[i][1 + j], mat->row[row][0]);
1163 isl_int_addmul(mat->row[i][1 + j],
1164 mat->row[i][off + col], mat->row[row][1 + j]);
1166 isl_int_mul(mat->row[i][off + col],
1167 mat->row[i][off + col], mat->row[row][off + col]);
1168 if (!isl_int_is_one(mat->row[i][0]))
1169 isl_seq_normalize(mat->ctx, mat->row[i], off + tab->n_col);
1171 t = tab->row_var[row];
1172 tab->row_var[row] = tab->col_var[col];
1173 tab->col_var[col] = t;
1174 var = isl_tab_var_from_row(tab, row);
1175 var->is_row = 1;
1176 var->index = row;
1177 var = var_from_col(tab, col);
1178 var->is_row = 0;
1179 var->index = col;
1180 update_row_sign(tab, row, col, sgn);
1181 if (tab->in_undo)
1182 return 0;
1183 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1184 if (isl_int_is_zero(mat->row[i][off + col]))
1185 continue;
1186 if (!isl_tab_var_from_row(tab, i)->frozen &&
1187 isl_tab_row_is_redundant(tab, i)) {
1188 int redo = isl_tab_mark_redundant(tab, i);
1189 if (redo < 0)
1190 return -1;
1191 if (redo)
1192 --i;
1195 return 0;
1198 /* If "var" represents a column variable, then pivot is up (sgn > 0)
1199 * or down (sgn < 0) to a row. The variable is assumed not to be
1200 * unbounded in the specified direction.
1201 * If sgn = 0, then the variable is unbounded in both directions,
1202 * and we pivot with any row we can find.
1204 static int to_row(struct isl_tab *tab, struct isl_tab_var *var, int sign) WARN_UNUSED;
1205 static int to_row(struct isl_tab *tab, struct isl_tab_var *var, int sign)
1207 int r;
1208 unsigned off = 2 + tab->M;
1210 if (var->is_row)
1211 return 0;
1213 if (sign == 0) {
1214 for (r = tab->n_redundant; r < tab->n_row; ++r)
1215 if (!isl_int_is_zero(tab->mat->row[r][off+var->index]))
1216 break;
1217 isl_assert(tab->mat->ctx, r < tab->n_row, return -1);
1218 } else {
1219 r = pivot_row(tab, NULL, sign, var->index);
1220 isl_assert(tab->mat->ctx, r >= 0, return -1);
1223 return isl_tab_pivot(tab, r, var->index);
1226 /* Check whether all variables that are marked as non-negative
1227 * also have a non-negative sample value. This function is not
1228 * called from the current code but is useful during debugging.
1230 static void check_table(struct isl_tab *tab) __attribute__ ((unused));
1231 static void check_table(struct isl_tab *tab)
1233 int i;
1235 if (tab->empty)
1236 return;
1237 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1238 struct isl_tab_var *var;
1239 var = isl_tab_var_from_row(tab, i);
1240 if (!var->is_nonneg)
1241 continue;
1242 if (tab->M) {
1243 isl_assert(tab->mat->ctx,
1244 !isl_int_is_neg(tab->mat->row[i][2]), abort());
1245 if (isl_int_is_pos(tab->mat->row[i][2]))
1246 continue;
1248 isl_assert(tab->mat->ctx, !isl_int_is_neg(tab->mat->row[i][1]),
1249 abort());
1253 /* Return the sign of the maximal value of "var".
1254 * If the sign is not negative, then on return from this function,
1255 * the sample value will also be non-negative.
1257 * If "var" is manifestly unbounded wrt positive values, we are done.
1258 * Otherwise, we pivot the variable up to a row if needed
1259 * Then we continue pivoting down until either
1260 * - no more down pivots can be performed
1261 * - the sample value is positive
1262 * - the variable is pivoted into a manifestly unbounded column
1264 static int sign_of_max(struct isl_tab *tab, struct isl_tab_var *var)
1266 int row, col;
1268 if (max_is_manifestly_unbounded(tab, var))
1269 return 1;
1270 if (to_row(tab, var, 1) < 0)
1271 return -2;
1272 while (!isl_int_is_pos(tab->mat->row[var->index][1])) {
1273 find_pivot(tab, var, var, 1, &row, &col);
1274 if (row == -1)
1275 return isl_int_sgn(tab->mat->row[var->index][1]);
1276 if (isl_tab_pivot(tab, row, col) < 0)
1277 return -2;
1278 if (!var->is_row) /* manifestly unbounded */
1279 return 1;
1281 return 1;
1284 int isl_tab_sign_of_max(struct isl_tab *tab, int con)
1286 struct isl_tab_var *var;
1288 if (!tab)
1289 return -2;
1291 var = &tab->con[con];
1292 isl_assert(tab->mat->ctx, !var->is_redundant, return -2);
1293 isl_assert(tab->mat->ctx, !var->is_zero, return -2);
1295 return sign_of_max(tab, var);
1298 static int row_is_neg(struct isl_tab *tab, int row)
1300 if (!tab->M)
1301 return isl_int_is_neg(tab->mat->row[row][1]);
1302 if (isl_int_is_pos(tab->mat->row[row][2]))
1303 return 0;
1304 if (isl_int_is_neg(tab->mat->row[row][2]))
1305 return 1;
1306 return isl_int_is_neg(tab->mat->row[row][1]);
1309 static int row_sgn(struct isl_tab *tab, int row)
1311 if (!tab->M)
1312 return isl_int_sgn(tab->mat->row[row][1]);
1313 if (!isl_int_is_zero(tab->mat->row[row][2]))
1314 return isl_int_sgn(tab->mat->row[row][2]);
1315 else
1316 return isl_int_sgn(tab->mat->row[row][1]);
1319 /* Perform pivots until the row variable "var" has a non-negative
1320 * sample value or until no more upward pivots can be performed.
1321 * Return the sign of the sample value after the pivots have been
1322 * performed.
1324 static int restore_row(struct isl_tab *tab, struct isl_tab_var *var)
1326 int row, col;
1328 while (row_is_neg(tab, var->index)) {
1329 find_pivot(tab, var, var, 1, &row, &col);
1330 if (row == -1)
1331 break;
1332 if (isl_tab_pivot(tab, row, col) < 0)
1333 return -2;
1334 if (!var->is_row) /* manifestly unbounded */
1335 return 1;
1337 return row_sgn(tab, var->index);
1340 /* Perform pivots until we are sure that the row variable "var"
1341 * can attain non-negative values. After return from this
1342 * function, "var" is still a row variable, but its sample
1343 * value may not be non-negative, even if the function returns 1.
1345 static int at_least_zero(struct isl_tab *tab, struct isl_tab_var *var)
1347 int row, col;
1349 while (isl_int_is_neg(tab->mat->row[var->index][1])) {
1350 find_pivot(tab, var, var, 1, &row, &col);
1351 if (row == -1)
1352 break;
1353 if (row == var->index) /* manifestly unbounded */
1354 return 1;
1355 if (isl_tab_pivot(tab, row, col) < 0)
1356 return -1;
1358 return !isl_int_is_neg(tab->mat->row[var->index][1]);
1361 /* Return a negative value if "var" can attain negative values.
1362 * Return a non-negative value otherwise.
1364 * If "var" is manifestly unbounded wrt negative values, we are done.
1365 * Otherwise, if var is in a column, we can pivot it down to a row.
1366 * Then we continue pivoting down until either
1367 * - the pivot would result in a manifestly unbounded column
1368 * => we don't perform the pivot, but simply return -1
1369 * - no more down pivots can be performed
1370 * - the sample value is negative
1371 * If the sample value becomes negative and the variable is supposed
1372 * to be nonnegative, then we undo the last pivot.
1373 * However, if the last pivot has made the pivoting variable
1374 * obviously redundant, then it may have moved to another row.
1375 * In that case we look for upward pivots until we reach a non-negative
1376 * value again.
1378 static int sign_of_min(struct isl_tab *tab, struct isl_tab_var *var)
1380 int row, col;
1381 struct isl_tab_var *pivot_var = NULL;
1383 if (min_is_manifestly_unbounded(tab, var))
1384 return -1;
1385 if (!var->is_row) {
1386 col = var->index;
1387 row = pivot_row(tab, NULL, -1, col);
1388 pivot_var = var_from_col(tab, col);
1389 if (isl_tab_pivot(tab, row, col) < 0)
1390 return -2;
1391 if (var->is_redundant)
1392 return 0;
1393 if (isl_int_is_neg(tab->mat->row[var->index][1])) {
1394 if (var->is_nonneg) {
1395 if (!pivot_var->is_redundant &&
1396 pivot_var->index == row) {
1397 if (isl_tab_pivot(tab, row, col) < 0)
1398 return -2;
1399 } else
1400 if (restore_row(tab, var) < -1)
1401 return -2;
1403 return -1;
1406 if (var->is_redundant)
1407 return 0;
1408 while (!isl_int_is_neg(tab->mat->row[var->index][1])) {
1409 find_pivot(tab, var, var, -1, &row, &col);
1410 if (row == var->index)
1411 return -1;
1412 if (row == -1)
1413 return isl_int_sgn(tab->mat->row[var->index][1]);
1414 pivot_var = var_from_col(tab, col);
1415 if (isl_tab_pivot(tab, row, col) < 0)
1416 return -2;
1417 if (var->is_redundant)
1418 return 0;
1420 if (pivot_var && var->is_nonneg) {
1421 /* pivot back to non-negative value */
1422 if (!pivot_var->is_redundant && pivot_var->index == row) {
1423 if (isl_tab_pivot(tab, row, col) < 0)
1424 return -2;
1425 } else
1426 if (restore_row(tab, var) < -1)
1427 return -2;
1429 return -1;
1432 static int row_at_most_neg_one(struct isl_tab *tab, int row)
1434 if (tab->M) {
1435 if (isl_int_is_pos(tab->mat->row[row][2]))
1436 return 0;
1437 if (isl_int_is_neg(tab->mat->row[row][2]))
1438 return 1;
1440 return isl_int_is_neg(tab->mat->row[row][1]) &&
1441 isl_int_abs_ge(tab->mat->row[row][1],
1442 tab->mat->row[row][0]);
1445 /* Return 1 if "var" can attain values <= -1.
1446 * Return 0 otherwise.
1448 * If the variable "var" is supposed to be non-negative (is_nonneg is set),
1449 * then the sample value of "var" is assumed to be non-negative when the
1450 * the function is called. If 1 is returned then the constraint
1451 * is not redundant and the sample value is made non-negative again before
1452 * the function returns.
1454 int isl_tab_min_at_most_neg_one(struct isl_tab *tab, struct isl_tab_var *var)
1456 int row, col;
1457 struct isl_tab_var *pivot_var;
1459 if (min_is_manifestly_unbounded(tab, var))
1460 return 1;
1461 if (!var->is_row) {
1462 col = var->index;
1463 row = pivot_row(tab, NULL, -1, col);
1464 pivot_var = var_from_col(tab, col);
1465 if (isl_tab_pivot(tab, row, col) < 0)
1466 return -1;
1467 if (var->is_redundant)
1468 return 0;
1469 if (row_at_most_neg_one(tab, var->index)) {
1470 if (var->is_nonneg) {
1471 if (!pivot_var->is_redundant &&
1472 pivot_var->index == row) {
1473 if (isl_tab_pivot(tab, row, col) < 0)
1474 return -1;
1475 } else
1476 if (restore_row(tab, var) < -1)
1477 return -1;
1479 return 1;
1482 if (var->is_redundant)
1483 return 0;
1484 do {
1485 find_pivot(tab, var, var, -1, &row, &col);
1486 if (row == var->index) {
1487 if (var->is_nonneg && restore_row(tab, var) < -1)
1488 return -1;
1489 return 1;
1491 if (row == -1)
1492 return 0;
1493 pivot_var = var_from_col(tab, col);
1494 if (isl_tab_pivot(tab, row, col) < 0)
1495 return -1;
1496 if (var->is_redundant)
1497 return 0;
1498 } while (!row_at_most_neg_one(tab, var->index));
1499 if (var->is_nonneg) {
1500 /* pivot back to non-negative value */
1501 if (!pivot_var->is_redundant && pivot_var->index == row)
1502 if (isl_tab_pivot(tab, row, col) < 0)
1503 return -1;
1504 if (restore_row(tab, var) < -1)
1505 return -1;
1507 return 1;
1510 /* Return 1 if "var" can attain values >= 1.
1511 * Return 0 otherwise.
1513 static int at_least_one(struct isl_tab *tab, struct isl_tab_var *var)
1515 int row, col;
1516 isl_int *r;
1518 if (max_is_manifestly_unbounded(tab, var))
1519 return 1;
1520 if (to_row(tab, var, 1) < 0)
1521 return -1;
1522 r = tab->mat->row[var->index];
1523 while (isl_int_lt(r[1], r[0])) {
1524 find_pivot(tab, var, var, 1, &row, &col);
1525 if (row == -1)
1526 return isl_int_ge(r[1], r[0]);
1527 if (row == var->index) /* manifestly unbounded */
1528 return 1;
1529 if (isl_tab_pivot(tab, row, col) < 0)
1530 return -1;
1532 return 1;
1535 static void swap_cols(struct isl_tab *tab, int col1, int col2)
1537 int t;
1538 unsigned off = 2 + tab->M;
1539 t = tab->col_var[col1];
1540 tab->col_var[col1] = tab->col_var[col2];
1541 tab->col_var[col2] = t;
1542 var_from_col(tab, col1)->index = col1;
1543 var_from_col(tab, col2)->index = col2;
1544 tab->mat = isl_mat_swap_cols(tab->mat, off + col1, off + col2);
1547 /* Mark column with index "col" as representing a zero variable.
1548 * If we may need to undo the operation the column is kept,
1549 * but no longer considered.
1550 * Otherwise, the column is simply removed.
1552 * The column may be interchanged with some other column. If it
1553 * is interchanged with a later column, return 1. Otherwise return 0.
1554 * If the columns are checked in order in the calling function,
1555 * then a return value of 1 means that the column with the given
1556 * column number may now contain a different column that
1557 * hasn't been checked yet.
1559 int isl_tab_kill_col(struct isl_tab *tab, int col)
1561 var_from_col(tab, col)->is_zero = 1;
1562 if (tab->need_undo) {
1563 if (isl_tab_push_var(tab, isl_tab_undo_zero,
1564 var_from_col(tab, col)) < 0)
1565 return -1;
1566 if (col != tab->n_dead)
1567 swap_cols(tab, col, tab->n_dead);
1568 tab->n_dead++;
1569 return 0;
1570 } else {
1571 if (col != tab->n_col - 1)
1572 swap_cols(tab, col, tab->n_col - 1);
1573 var_from_col(tab, tab->n_col - 1)->index = -1;
1574 tab->n_col--;
1575 return 1;
1579 static int row_is_manifestly_non_integral(struct isl_tab *tab, int row)
1581 unsigned off = 2 + tab->M;
1583 if (tab->M && !isl_int_eq(tab->mat->row[row][2],
1584 tab->mat->row[row][0]))
1585 return 0;
1586 if (isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
1587 tab->n_col - tab->n_dead) != -1)
1588 return 0;
1590 return !isl_int_is_divisible_by(tab->mat->row[row][1],
1591 tab->mat->row[row][0]);
1594 /* For integer tableaus, check if any of the coordinates are stuck
1595 * at a non-integral value.
1597 static int tab_is_manifestly_empty(struct isl_tab *tab)
1599 int i;
1601 if (tab->empty)
1602 return 1;
1603 if (tab->rational)
1604 return 0;
1606 for (i = 0; i < tab->n_var; ++i) {
1607 if (!tab->var[i].is_row)
1608 continue;
1609 if (row_is_manifestly_non_integral(tab, tab->var[i].index))
1610 return 1;
1613 return 0;
1616 /* Row variable "var" is non-negative and cannot attain any values
1617 * larger than zero. This means that the coefficients of the unrestricted
1618 * column variables are zero and that the coefficients of the non-negative
1619 * column variables are zero or negative.
1620 * Each of the non-negative variables with a negative coefficient can
1621 * then also be written as the negative sum of non-negative variables
1622 * and must therefore also be zero.
1624 * If "temp_var" is set, then "var" is a temporary variable that
1625 * will be removed after this function returns and for which
1626 * no information is recorded on the undo stack.
1627 * Do not add any undo records involving this variable in this case
1628 * since the variable will have been removed before any future undo
1629 * operations. Also avoid marking the variable as redundant,
1630 * since that either adds an undo record or needlessly removes the row
1631 * (the caller will take care of removing the row).
1633 static isl_stat close_row(struct isl_tab *tab, struct isl_tab_var *var,
1634 int temp_var) WARN_UNUSED;
1635 static isl_stat close_row(struct isl_tab *tab, struct isl_tab_var *var,
1636 int temp_var)
1638 int j;
1639 struct isl_mat *mat = tab->mat;
1640 unsigned off = 2 + tab->M;
1642 if (!var->is_nonneg)
1643 isl_die(isl_tab_get_ctx(tab), isl_error_internal,
1644 "expecting non-negative variable",
1645 return isl_stat_error);
1646 var->is_zero = 1;
1647 if (!temp_var && tab->need_undo)
1648 if (isl_tab_push_var(tab, isl_tab_undo_zero, var) < 0)
1649 return isl_stat_error;
1650 for (j = tab->n_dead; j < tab->n_col; ++j) {
1651 int recheck;
1652 if (isl_int_is_zero(mat->row[var->index][off + j]))
1653 continue;
1654 if (isl_int_is_pos(mat->row[var->index][off + j]))
1655 isl_die(isl_tab_get_ctx(tab), isl_error_internal,
1656 "row cannot have positive coefficients",
1657 return isl_stat_error);
1658 recheck = isl_tab_kill_col(tab, j);
1659 if (recheck < 0)
1660 return isl_stat_error;
1661 if (recheck)
1662 --j;
1664 if (!temp_var && isl_tab_mark_redundant(tab, var->index) < 0)
1665 return isl_stat_error;
1666 if (tab_is_manifestly_empty(tab) && isl_tab_mark_empty(tab) < 0)
1667 return isl_stat_error;
1668 return isl_stat_ok;
1671 /* Add a constraint to the tableau and allocate a row for it.
1672 * Return the index into the constraint array "con".
1674 * This function assumes that at least one more row and at least
1675 * one more element in the constraint array are available in the tableau.
1677 int isl_tab_allocate_con(struct isl_tab *tab)
1679 int r;
1681 isl_assert(tab->mat->ctx, tab->n_row < tab->mat->n_row, return -1);
1682 isl_assert(tab->mat->ctx, tab->n_con < tab->max_con, return -1);
1684 r = tab->n_con;
1685 tab->con[r].index = tab->n_row;
1686 tab->con[r].is_row = 1;
1687 tab->con[r].is_nonneg = 0;
1688 tab->con[r].is_zero = 0;
1689 tab->con[r].is_redundant = 0;
1690 tab->con[r].frozen = 0;
1691 tab->con[r].negated = 0;
1692 tab->row_var[tab->n_row] = ~r;
1694 tab->n_row++;
1695 tab->n_con++;
1696 if (isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->con[r]) < 0)
1697 return -1;
1699 return r;
1702 /* Move the entries in tab->var up one position, starting at "first",
1703 * creating room for an extra entry at position "first".
1704 * Since some of the entries of tab->row_var and tab->col_var contain
1705 * indices into this array, they have to be updated accordingly.
1707 static int var_insert_entry(struct isl_tab *tab, int first)
1709 int i;
1711 if (tab->n_var >= tab->max_var)
1712 isl_die(isl_tab_get_ctx(tab), isl_error_internal,
1713 "not enough room for new variable", return -1);
1714 if (first > tab->n_var)
1715 isl_die(isl_tab_get_ctx(tab), isl_error_internal,
1716 "invalid initial position", return -1);
1718 for (i = tab->n_var - 1; i >= first; --i) {
1719 tab->var[i + 1] = tab->var[i];
1720 if (tab->var[i + 1].is_row)
1721 tab->row_var[tab->var[i + 1].index]++;
1722 else
1723 tab->col_var[tab->var[i + 1].index]++;
1726 tab->n_var++;
1728 return 0;
1731 /* Drop the entry at position "first" in tab->var, moving all
1732 * subsequent entries down.
1733 * Since some of the entries of tab->row_var and tab->col_var contain
1734 * indices into this array, they have to be updated accordingly.
1736 static int var_drop_entry(struct isl_tab *tab, int first)
1738 int i;
1740 if (first >= tab->n_var)
1741 isl_die(isl_tab_get_ctx(tab), isl_error_internal,
1742 "invalid initial position", return -1);
1744 tab->n_var--;
1746 for (i = first; i < tab->n_var; ++i) {
1747 tab->var[i] = tab->var[i + 1];
1748 if (tab->var[i + 1].is_row)
1749 tab->row_var[tab->var[i].index]--;
1750 else
1751 tab->col_var[tab->var[i].index]--;
1754 return 0;
1757 /* Add a variable to the tableau at position "r" and allocate a column for it.
1758 * Return the index into the variable array "var", i.e., "r",
1759 * or -1 on error.
1761 int isl_tab_insert_var(struct isl_tab *tab, int r)
1763 int i;
1764 unsigned off = 2 + tab->M;
1766 isl_assert(tab->mat->ctx, tab->n_col < tab->mat->n_col, return -1);
1768 if (var_insert_entry(tab, r) < 0)
1769 return -1;
1771 tab->var[r].index = tab->n_col;
1772 tab->var[r].is_row = 0;
1773 tab->var[r].is_nonneg = 0;
1774 tab->var[r].is_zero = 0;
1775 tab->var[r].is_redundant = 0;
1776 tab->var[r].frozen = 0;
1777 tab->var[r].negated = 0;
1778 tab->col_var[tab->n_col] = r;
1780 for (i = 0; i < tab->n_row; ++i)
1781 isl_int_set_si(tab->mat->row[i][off + tab->n_col], 0);
1783 tab->n_col++;
1784 if (isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->var[r]) < 0)
1785 return -1;
1787 return r;
1790 /* Add a variable to the tableau and allocate a column for it.
1791 * Return the index into the variable array "var".
1793 int isl_tab_allocate_var(struct isl_tab *tab)
1795 if (!tab)
1796 return -1;
1798 return isl_tab_insert_var(tab, tab->n_var);
1801 /* Add a row to the tableau. The row is given as an affine combination
1802 * of the original variables and needs to be expressed in terms of the
1803 * column variables.
1805 * This function assumes that at least one more row and at least
1806 * one more element in the constraint array are available in the tableau.
1808 * We add each term in turn.
1809 * If r = n/d_r is the current sum and we need to add k x, then
1810 * if x is a column variable, we increase the numerator of
1811 * this column by k d_r
1812 * if x = f/d_x is a row variable, then the new representation of r is
1814 * n k f d_x/g n + d_r/g k f m/d_r n + m/d_g k f
1815 * --- + --- = ------------------- = -------------------
1816 * d_r d_r d_r d_x/g m
1818 * with g the gcd of d_r and d_x and m the lcm of d_r and d_x.
1820 * If tab->M is set, then, internally, each variable x is represented
1821 * as x' - M. We then also need no subtract k d_r from the coefficient of M.
1823 int isl_tab_add_row(struct isl_tab *tab, isl_int *line)
1825 int i;
1826 int r;
1827 isl_int *row;
1828 isl_int a, b;
1829 unsigned off = 2 + tab->M;
1831 r = isl_tab_allocate_con(tab);
1832 if (r < 0)
1833 return -1;
1835 isl_int_init(a);
1836 isl_int_init(b);
1837 row = tab->mat->row[tab->con[r].index];
1838 isl_int_set_si(row[0], 1);
1839 isl_int_set(row[1], line[0]);
1840 isl_seq_clr(row + 2, tab->M + tab->n_col);
1841 for (i = 0; i < tab->n_var; ++i) {
1842 if (tab->var[i].is_zero)
1843 continue;
1844 if (tab->var[i].is_row) {
1845 isl_int_lcm(a,
1846 row[0], tab->mat->row[tab->var[i].index][0]);
1847 isl_int_swap(a, row[0]);
1848 isl_int_divexact(a, row[0], a);
1849 isl_int_divexact(b,
1850 row[0], tab->mat->row[tab->var[i].index][0]);
1851 isl_int_mul(b, b, line[1 + i]);
1852 isl_seq_combine(row + 1, a, row + 1,
1853 b, tab->mat->row[tab->var[i].index] + 1,
1854 1 + tab->M + tab->n_col);
1855 } else
1856 isl_int_addmul(row[off + tab->var[i].index],
1857 line[1 + i], row[0]);
1858 if (tab->M && i >= tab->n_param && i < tab->n_var - tab->n_div)
1859 isl_int_submul(row[2], line[1 + i], row[0]);
1861 isl_seq_normalize(tab->mat->ctx, row, off + tab->n_col);
1862 isl_int_clear(a);
1863 isl_int_clear(b);
1865 if (tab->row_sign)
1866 tab->row_sign[tab->con[r].index] = isl_tab_row_unknown;
1868 return r;
1871 static isl_stat drop_row(struct isl_tab *tab, int row)
1873 isl_assert(tab->mat->ctx, ~tab->row_var[row] == tab->n_con - 1,
1874 return isl_stat_error);
1875 if (row != tab->n_row - 1)
1876 swap_rows(tab, row, tab->n_row - 1);
1877 tab->n_row--;
1878 tab->n_con--;
1879 return isl_stat_ok;
1882 /* Drop the variable in column "col" along with the column.
1883 * The column is removed first because it may need to be moved
1884 * into the last position and this process requires
1885 * the contents of the col_var array in a state
1886 * before the removal of the variable.
1888 static isl_stat drop_col(struct isl_tab *tab, int col)
1890 int var;
1892 var = tab->col_var[col];
1893 if (col != tab->n_col - 1)
1894 swap_cols(tab, col, tab->n_col - 1);
1895 tab->n_col--;
1896 if (var_drop_entry(tab, var) < 0)
1897 return isl_stat_error;
1898 return isl_stat_ok;
1901 /* Add inequality "ineq" and check if it conflicts with the
1902 * previously added constraints or if it is obviously redundant.
1904 * This function assumes that at least one more row and at least
1905 * one more element in the constraint array are available in the tableau.
1907 isl_stat isl_tab_add_ineq(struct isl_tab *tab, isl_int *ineq)
1909 int r;
1910 int sgn;
1911 isl_int cst;
1913 if (!tab)
1914 return isl_stat_error;
1915 if (tab->bmap) {
1916 struct isl_basic_map *bmap = tab->bmap;
1918 isl_assert(tab->mat->ctx, tab->n_eq == bmap->n_eq,
1919 return isl_stat_error);
1920 isl_assert(tab->mat->ctx,
1921 tab->n_con == bmap->n_eq + bmap->n_ineq,
1922 return isl_stat_error);
1923 tab->bmap = isl_basic_map_add_ineq(tab->bmap, ineq);
1924 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1925 return isl_stat_error;
1926 if (!tab->bmap)
1927 return isl_stat_error;
1929 if (tab->cone) {
1930 isl_int_init(cst);
1931 isl_int_set_si(cst, 0);
1932 isl_int_swap(ineq[0], cst);
1934 r = isl_tab_add_row(tab, ineq);
1935 if (tab->cone) {
1936 isl_int_swap(ineq[0], cst);
1937 isl_int_clear(cst);
1939 if (r < 0)
1940 return isl_stat_error;
1941 tab->con[r].is_nonneg = 1;
1942 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1943 return isl_stat_error;
1944 if (isl_tab_row_is_redundant(tab, tab->con[r].index)) {
1945 if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1946 return isl_stat_error;
1947 return isl_stat_ok;
1950 sgn = restore_row(tab, &tab->con[r]);
1951 if (sgn < -1)
1952 return isl_stat_error;
1953 if (sgn < 0)
1954 return isl_tab_mark_empty(tab);
1955 if (tab->con[r].is_row && isl_tab_row_is_redundant(tab, tab->con[r].index))
1956 if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1957 return isl_stat_error;
1958 return isl_stat_ok;
1961 /* Pivot a non-negative variable down until it reaches the value zero
1962 * and then pivot the variable into a column position.
1964 static int to_col(struct isl_tab *tab, struct isl_tab_var *var) WARN_UNUSED;
1965 static int to_col(struct isl_tab *tab, struct isl_tab_var *var)
1967 int i;
1968 int row, col;
1969 unsigned off = 2 + tab->M;
1971 if (!var->is_row)
1972 return 0;
1974 while (isl_int_is_pos(tab->mat->row[var->index][1])) {
1975 find_pivot(tab, var, NULL, -1, &row, &col);
1976 isl_assert(tab->mat->ctx, row != -1, return -1);
1977 if (isl_tab_pivot(tab, row, col) < 0)
1978 return -1;
1979 if (!var->is_row)
1980 return 0;
1983 for (i = tab->n_dead; i < tab->n_col; ++i)
1984 if (!isl_int_is_zero(tab->mat->row[var->index][off + i]))
1985 break;
1987 isl_assert(tab->mat->ctx, i < tab->n_col, return -1);
1988 if (isl_tab_pivot(tab, var->index, i) < 0)
1989 return -1;
1991 return 0;
1994 /* We assume Gaussian elimination has been performed on the equalities.
1995 * The equalities can therefore never conflict.
1996 * Adding the equalities is currently only really useful for a later call
1997 * to isl_tab_ineq_type.
1999 * This function assumes that at least one more row and at least
2000 * one more element in the constraint array are available in the tableau.
2002 static struct isl_tab *add_eq(struct isl_tab *tab, isl_int *eq)
2004 int i;
2005 int r;
2007 if (!tab)
2008 return NULL;
2009 r = isl_tab_add_row(tab, eq);
2010 if (r < 0)
2011 goto error;
2013 r = tab->con[r].index;
2014 i = isl_seq_first_non_zero(tab->mat->row[r] + 2 + tab->M + tab->n_dead,
2015 tab->n_col - tab->n_dead);
2016 isl_assert(tab->mat->ctx, i >= 0, goto error);
2017 i += tab->n_dead;
2018 if (isl_tab_pivot(tab, r, i) < 0)
2019 goto error;
2020 if (isl_tab_kill_col(tab, i) < 0)
2021 goto error;
2022 tab->n_eq++;
2024 return tab;
2025 error:
2026 isl_tab_free(tab);
2027 return NULL;
2030 /* Does the sample value of row "row" of "tab" involve the big parameter,
2031 * if any?
2033 static int row_is_big(struct isl_tab *tab, int row)
2035 return tab->M && !isl_int_is_zero(tab->mat->row[row][2]);
2038 static int row_is_manifestly_zero(struct isl_tab *tab, int row)
2040 unsigned off = 2 + tab->M;
2042 if (!isl_int_is_zero(tab->mat->row[row][1]))
2043 return 0;
2044 if (row_is_big(tab, row))
2045 return 0;
2046 return isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
2047 tab->n_col - tab->n_dead) == -1;
2050 /* Add an equality that is known to be valid for the given tableau.
2052 * This function assumes that at least one more row and at least
2053 * one more element in the constraint array are available in the tableau.
2055 int isl_tab_add_valid_eq(struct isl_tab *tab, isl_int *eq)
2057 struct isl_tab_var *var;
2058 int r;
2060 if (!tab)
2061 return -1;
2062 r = isl_tab_add_row(tab, eq);
2063 if (r < 0)
2064 return -1;
2066 var = &tab->con[r];
2067 r = var->index;
2068 if (row_is_manifestly_zero(tab, r)) {
2069 var->is_zero = 1;
2070 if (isl_tab_mark_redundant(tab, r) < 0)
2071 return -1;
2072 return 0;
2075 if (isl_int_is_neg(tab->mat->row[r][1])) {
2076 isl_seq_neg(tab->mat->row[r] + 1, tab->mat->row[r] + 1,
2077 1 + tab->n_col);
2078 var->negated = 1;
2080 var->is_nonneg = 1;
2081 if (to_col(tab, var) < 0)
2082 return -1;
2083 var->is_nonneg = 0;
2084 if (isl_tab_kill_col(tab, var->index) < 0)
2085 return -1;
2087 return 0;
2090 /* Add a zero row to "tab" and return the corresponding index
2091 * in the constraint array.
2093 * This function assumes that at least one more row and at least
2094 * one more element in the constraint array are available in the tableau.
2096 static int add_zero_row(struct isl_tab *tab)
2098 int r;
2099 isl_int *row;
2101 r = isl_tab_allocate_con(tab);
2102 if (r < 0)
2103 return -1;
2105 row = tab->mat->row[tab->con[r].index];
2106 isl_seq_clr(row + 1, 1 + tab->M + tab->n_col);
2107 isl_int_set_si(row[0], 1);
2109 return r;
2112 /* Add equality "eq" and check if it conflicts with the
2113 * previously added constraints or if it is obviously redundant.
2115 * This function assumes that at least one more row and at least
2116 * one more element in the constraint array are available in the tableau.
2117 * If tab->bmap is set, then two rows are needed instead of one.
2119 int isl_tab_add_eq(struct isl_tab *tab, isl_int *eq)
2121 struct isl_tab_undo *snap = NULL;
2122 struct isl_tab_var *var;
2123 int r;
2124 int row;
2125 int sgn;
2126 isl_int cst;
2128 if (!tab)
2129 return -1;
2130 isl_assert(tab->mat->ctx, !tab->M, return -1);
2132 if (tab->need_undo)
2133 snap = isl_tab_snap(tab);
2135 if (tab->cone) {
2136 isl_int_init(cst);
2137 isl_int_set_si(cst, 0);
2138 isl_int_swap(eq[0], cst);
2140 r = isl_tab_add_row(tab, eq);
2141 if (tab->cone) {
2142 isl_int_swap(eq[0], cst);
2143 isl_int_clear(cst);
2145 if (r < 0)
2146 return -1;
2148 var = &tab->con[r];
2149 row = var->index;
2150 if (row_is_manifestly_zero(tab, row)) {
2151 if (snap)
2152 return isl_tab_rollback(tab, snap);
2153 return drop_row(tab, row);
2156 if (tab->bmap) {
2157 tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
2158 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
2159 return -1;
2160 isl_seq_neg(eq, eq, 1 + tab->n_var);
2161 tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
2162 isl_seq_neg(eq, eq, 1 + tab->n_var);
2163 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
2164 return -1;
2165 if (!tab->bmap)
2166 return -1;
2167 if (add_zero_row(tab) < 0)
2168 return -1;
2171 sgn = isl_int_sgn(tab->mat->row[row][1]);
2173 if (sgn > 0) {
2174 isl_seq_neg(tab->mat->row[row] + 1, tab->mat->row[row] + 1,
2175 1 + tab->n_col);
2176 var->negated = 1;
2177 sgn = -1;
2180 if (sgn < 0) {
2181 sgn = sign_of_max(tab, var);
2182 if (sgn < -1)
2183 return -1;
2184 if (sgn < 0) {
2185 if (isl_tab_mark_empty(tab) < 0)
2186 return -1;
2187 return 0;
2191 var->is_nonneg = 1;
2192 if (to_col(tab, var) < 0)
2193 return -1;
2194 var->is_nonneg = 0;
2195 if (isl_tab_kill_col(tab, var->index) < 0)
2196 return -1;
2198 return 0;
2201 /* Construct and return an inequality that expresses an upper bound
2202 * on the given div.
2203 * In particular, if the div is given by
2205 * d = floor(e/m)
2207 * then the inequality expresses
2209 * m d <= e
2211 static __isl_give isl_vec *ineq_for_div(__isl_keep isl_basic_map *bmap,
2212 unsigned div)
2214 isl_size total;
2215 unsigned div_pos;
2216 struct isl_vec *ineq;
2218 total = isl_basic_map_dim(bmap, isl_dim_all);
2219 if (total < 0)
2220 return NULL;
2222 div_pos = 1 + total - bmap->n_div + div;
2224 ineq = isl_vec_alloc(bmap->ctx, 1 + total);
2225 if (!ineq)
2226 return NULL;
2228 isl_seq_cpy(ineq->el, bmap->div[div] + 1, 1 + total);
2229 isl_int_neg(ineq->el[div_pos], bmap->div[div][0]);
2230 return ineq;
2233 /* For a div d = floor(f/m), add the constraints
2235 * f - m d >= 0
2236 * -(f-(m-1)) + m d >= 0
2238 * Note that the second constraint is the negation of
2240 * f - m d >= m
2242 * If add_ineq is not NULL, then this function is used
2243 * instead of isl_tab_add_ineq to effectively add the inequalities.
2245 * This function assumes that at least two more rows and at least
2246 * two more elements in the constraint array are available in the tableau.
2248 static isl_stat add_div_constraints(struct isl_tab *tab, unsigned div,
2249 isl_stat (*add_ineq)(void *user, isl_int *), void *user)
2251 isl_size total;
2252 unsigned div_pos;
2253 struct isl_vec *ineq;
2255 total = isl_basic_map_dim(tab->bmap, isl_dim_all);
2256 if (total < 0)
2257 return isl_stat_error;
2258 div_pos = 1 + total - tab->bmap->n_div + div;
2260 ineq = ineq_for_div(tab->bmap, div);
2261 if (!ineq)
2262 goto error;
2264 if (add_ineq) {
2265 if (add_ineq(user, ineq->el) < 0)
2266 goto error;
2267 } else {
2268 if (isl_tab_add_ineq(tab, ineq->el) < 0)
2269 goto error;
2272 isl_seq_neg(ineq->el, tab->bmap->div[div] + 1, 1 + total);
2273 isl_int_set(ineq->el[div_pos], tab->bmap->div[div][0]);
2274 isl_int_add(ineq->el[0], ineq->el[0], ineq->el[div_pos]);
2275 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
2277 if (add_ineq) {
2278 if (add_ineq(user, ineq->el) < 0)
2279 goto error;
2280 } else {
2281 if (isl_tab_add_ineq(tab, ineq->el) < 0)
2282 goto error;
2285 isl_vec_free(ineq);
2287 return isl_stat_ok;
2288 error:
2289 isl_vec_free(ineq);
2290 return isl_stat_error;
2293 /* Check whether the div described by "div" is obviously non-negative.
2294 * If we are using a big parameter, then we will encode the div
2295 * as div' = M + div, which is always non-negative.
2296 * Otherwise, we check whether div is a non-negative affine combination
2297 * of non-negative variables.
2299 static int div_is_nonneg(struct isl_tab *tab, __isl_keep isl_vec *div)
2301 int i;
2303 if (tab->M)
2304 return 1;
2306 if (isl_int_is_neg(div->el[1]))
2307 return 0;
2309 for (i = 0; i < tab->n_var; ++i) {
2310 if (isl_int_is_neg(div->el[2 + i]))
2311 return 0;
2312 if (isl_int_is_zero(div->el[2 + i]))
2313 continue;
2314 if (!tab->var[i].is_nonneg)
2315 return 0;
2318 return 1;
2321 /* Insert an extra div, prescribed by "div", to the tableau and
2322 * the associated bmap (which is assumed to be non-NULL).
2323 * The extra integer division is inserted at (tableau) position "pos".
2324 * Return "pos" or -1 if an error occurred.
2326 * If add_ineq is not NULL, then this function is used instead
2327 * of isl_tab_add_ineq to add the div constraints.
2328 * This complication is needed because the code in isl_tab_pip
2329 * wants to perform some extra processing when an inequality
2330 * is added to the tableau.
2332 int isl_tab_insert_div(struct isl_tab *tab, int pos, __isl_keep isl_vec *div,
2333 isl_stat (*add_ineq)(void *user, isl_int *), void *user)
2335 int r;
2336 int nonneg;
2337 isl_size n_div;
2338 int o_div;
2340 if (!tab || !div)
2341 return -1;
2343 if (div->size != 1 + 1 + tab->n_var)
2344 isl_die(isl_tab_get_ctx(tab), isl_error_invalid,
2345 "unexpected size", return -1);
2347 n_div = isl_basic_map_dim(tab->bmap, isl_dim_div);
2348 if (n_div < 0)
2349 return -1;
2350 o_div = tab->n_var - n_div;
2351 if (pos < o_div || pos > tab->n_var)
2352 isl_die(isl_tab_get_ctx(tab), isl_error_invalid,
2353 "invalid position", return -1);
2355 nonneg = div_is_nonneg(tab, div);
2357 if (isl_tab_extend_cons(tab, 3) < 0)
2358 return -1;
2359 if (isl_tab_extend_vars(tab, 1) < 0)
2360 return -1;
2361 r = isl_tab_insert_var(tab, pos);
2362 if (r < 0)
2363 return -1;
2365 if (nonneg)
2366 tab->var[r].is_nonneg = 1;
2368 tab->bmap = isl_basic_map_insert_div(tab->bmap, pos - o_div, div);
2369 if (!tab->bmap)
2370 return -1;
2371 if (isl_tab_push_var(tab, isl_tab_undo_bmap_div, &tab->var[r]) < 0)
2372 return -1;
2374 if (add_div_constraints(tab, pos - o_div, add_ineq, user) < 0)
2375 return -1;
2377 return r;
2380 /* Add an extra div, prescribed by "div", to the tableau and
2381 * the associated bmap (which is assumed to be non-NULL).
2383 int isl_tab_add_div(struct isl_tab *tab, __isl_keep isl_vec *div)
2385 if (!tab)
2386 return -1;
2387 return isl_tab_insert_div(tab, tab->n_var, div, NULL, NULL);
2390 /* If "track" is set, then we want to keep track of all constraints in tab
2391 * in its bmap field. This field is initialized from a copy of "bmap",
2392 * so we need to make sure that all constraints in "bmap" also appear
2393 * in the constructed tab.
2395 __isl_give struct isl_tab *isl_tab_from_basic_map(
2396 __isl_keep isl_basic_map *bmap, int track)
2398 int i;
2399 struct isl_tab *tab;
2400 isl_size total;
2402 total = isl_basic_map_dim(bmap, isl_dim_all);
2403 if (total < 0)
2404 return NULL;
2405 tab = isl_tab_alloc(bmap->ctx, total + bmap->n_ineq + 1, total, 0);
2406 if (!tab)
2407 return NULL;
2408 tab->preserve = track;
2409 tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
2410 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)) {
2411 if (isl_tab_mark_empty(tab) < 0)
2412 goto error;
2413 goto done;
2415 for (i = 0; i < bmap->n_eq; ++i) {
2416 tab = add_eq(tab, bmap->eq[i]);
2417 if (!tab)
2418 return tab;
2420 for (i = 0; i < bmap->n_ineq; ++i) {
2421 if (isl_tab_add_ineq(tab, bmap->ineq[i]) < 0)
2422 goto error;
2423 if (tab->empty)
2424 goto done;
2426 done:
2427 if (track && isl_tab_track_bmap(tab, isl_basic_map_copy(bmap)) < 0)
2428 goto error;
2429 return tab;
2430 error:
2431 isl_tab_free(tab);
2432 return NULL;
2435 __isl_give struct isl_tab *isl_tab_from_basic_set(
2436 __isl_keep isl_basic_set *bset, int track)
2438 return isl_tab_from_basic_map(bset, track);
2441 /* Construct a tableau corresponding to the recession cone of "bset".
2443 struct isl_tab *isl_tab_from_recession_cone(__isl_keep isl_basic_set *bset,
2444 int parametric)
2446 isl_int cst;
2447 int i;
2448 struct isl_tab *tab;
2449 isl_size offset = 0;
2450 isl_size total;
2452 total = isl_basic_set_dim(bset, isl_dim_all);
2453 if (parametric)
2454 offset = isl_basic_set_dim(bset, isl_dim_param);
2455 if (total < 0 || offset < 0)
2456 return NULL;
2457 tab = isl_tab_alloc(bset->ctx, bset->n_eq + bset->n_ineq,
2458 total - offset, 0);
2459 if (!tab)
2460 return NULL;
2461 tab->rational = ISL_F_ISSET(bset, ISL_BASIC_SET_RATIONAL);
2462 tab->cone = 1;
2464 isl_int_init(cst);
2465 isl_int_set_si(cst, 0);
2466 for (i = 0; i < bset->n_eq; ++i) {
2467 isl_int_swap(bset->eq[i][offset], cst);
2468 if (offset > 0) {
2469 if (isl_tab_add_eq(tab, bset->eq[i] + offset) < 0)
2470 goto error;
2471 } else
2472 tab = add_eq(tab, bset->eq[i]);
2473 isl_int_swap(bset->eq[i][offset], cst);
2474 if (!tab)
2475 goto done;
2477 for (i = 0; i < bset->n_ineq; ++i) {
2478 int r;
2479 isl_int_swap(bset->ineq[i][offset], cst);
2480 r = isl_tab_add_row(tab, bset->ineq[i] + offset);
2481 isl_int_swap(bset->ineq[i][offset], cst);
2482 if (r < 0)
2483 goto error;
2484 tab->con[r].is_nonneg = 1;
2485 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
2486 goto error;
2488 done:
2489 isl_int_clear(cst);
2490 return tab;
2491 error:
2492 isl_int_clear(cst);
2493 isl_tab_free(tab);
2494 return NULL;
2497 /* Assuming "tab" is the tableau of a cone, check if the cone is
2498 * bounded, i.e., if it is empty or only contains the origin.
2500 isl_bool isl_tab_cone_is_bounded(struct isl_tab *tab)
2502 int i;
2504 if (!tab)
2505 return isl_bool_error;
2506 if (tab->empty)
2507 return isl_bool_true;
2508 if (tab->n_dead == tab->n_col)
2509 return isl_bool_true;
2511 for (;;) {
2512 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2513 struct isl_tab_var *var;
2514 int sgn;
2515 var = isl_tab_var_from_row(tab, i);
2516 if (!var->is_nonneg)
2517 continue;
2518 sgn = sign_of_max(tab, var);
2519 if (sgn < -1)
2520 return isl_bool_error;
2521 if (sgn != 0)
2522 return isl_bool_false;
2523 if (close_row(tab, var, 0) < 0)
2524 return isl_bool_error;
2525 break;
2527 if (tab->n_dead == tab->n_col)
2528 return isl_bool_true;
2529 if (i == tab->n_row)
2530 return isl_bool_false;
2534 int isl_tab_sample_is_integer(struct isl_tab *tab)
2536 int i;
2538 if (!tab)
2539 return -1;
2541 for (i = 0; i < tab->n_var; ++i) {
2542 int row;
2543 if (!tab->var[i].is_row)
2544 continue;
2545 row = tab->var[i].index;
2546 if (!isl_int_is_divisible_by(tab->mat->row[row][1],
2547 tab->mat->row[row][0]))
2548 return 0;
2550 return 1;
2553 static struct isl_vec *extract_integer_sample(struct isl_tab *tab)
2555 int i;
2556 struct isl_vec *vec;
2558 vec = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
2559 if (!vec)
2560 return NULL;
2562 isl_int_set_si(vec->block.data[0], 1);
2563 for (i = 0; i < tab->n_var; ++i) {
2564 if (!tab->var[i].is_row)
2565 isl_int_set_si(vec->block.data[1 + i], 0);
2566 else {
2567 int row = tab->var[i].index;
2568 isl_int_divexact(vec->block.data[1 + i],
2569 tab->mat->row[row][1], tab->mat->row[row][0]);
2573 return vec;
2576 struct isl_vec *isl_tab_get_sample_value(struct isl_tab *tab)
2578 int i;
2579 struct isl_vec *vec;
2580 isl_int m;
2582 if (!tab)
2583 return NULL;
2585 vec = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
2586 if (!vec)
2587 return NULL;
2589 isl_int_init(m);
2591 isl_int_set_si(vec->block.data[0], 1);
2592 for (i = 0; i < tab->n_var; ++i) {
2593 int row;
2594 if (!tab->var[i].is_row) {
2595 isl_int_set_si(vec->block.data[1 + i], 0);
2596 continue;
2598 row = tab->var[i].index;
2599 isl_int_gcd(m, vec->block.data[0], tab->mat->row[row][0]);
2600 isl_int_divexact(m, tab->mat->row[row][0], m);
2601 isl_seq_scale(vec->block.data, vec->block.data, m, 1 + i);
2602 isl_int_divexact(m, vec->block.data[0], tab->mat->row[row][0]);
2603 isl_int_mul(vec->block.data[1 + i], m, tab->mat->row[row][1]);
2605 vec = isl_vec_normalize(vec);
2607 isl_int_clear(m);
2608 return vec;
2611 /* Store the sample value of "var" of "tab" rounded up (if sgn > 0)
2612 * or down (if sgn < 0) to the nearest integer in *v.
2614 static void get_rounded_sample_value(struct isl_tab *tab,
2615 struct isl_tab_var *var, int sgn, isl_int *v)
2617 if (!var->is_row)
2618 isl_int_set_si(*v, 0);
2619 else if (sgn > 0)
2620 isl_int_cdiv_q(*v, tab->mat->row[var->index][1],
2621 tab->mat->row[var->index][0]);
2622 else
2623 isl_int_fdiv_q(*v, tab->mat->row[var->index][1],
2624 tab->mat->row[var->index][0]);
2627 /* Update "bmap" based on the results of the tableau "tab".
2628 * In particular, implicit equalities are made explicit, redundant constraints
2629 * are removed and if the sample value happens to be integer, it is stored
2630 * in "bmap" (unless "bmap" already had an integer sample).
2632 * The tableau is assumed to have been created from "bmap" using
2633 * isl_tab_from_basic_map.
2635 struct isl_basic_map *isl_basic_map_update_from_tab(struct isl_basic_map *bmap,
2636 struct isl_tab *tab)
2638 int i;
2639 unsigned n_eq;
2641 if (!bmap)
2642 return NULL;
2643 if (!tab)
2644 return bmap;
2646 n_eq = tab->n_eq;
2647 if (tab->empty)
2648 bmap = isl_basic_map_set_to_empty(bmap);
2649 else
2650 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2651 if (isl_tab_is_equality(tab, n_eq + i))
2652 isl_basic_map_inequality_to_equality(bmap, i);
2653 else if (isl_tab_is_redundant(tab, n_eq + i))
2654 isl_basic_map_drop_inequality(bmap, i);
2656 if (bmap->n_eq != n_eq)
2657 bmap = isl_basic_map_gauss(bmap, NULL);
2658 if (!tab->rational &&
2659 bmap && !bmap->sample && isl_tab_sample_is_integer(tab))
2660 bmap->sample = extract_integer_sample(tab);
2661 return bmap;
2664 struct isl_basic_set *isl_basic_set_update_from_tab(struct isl_basic_set *bset,
2665 struct isl_tab *tab)
2667 return bset_from_bmap(isl_basic_map_update_from_tab(bset_to_bmap(bset),
2668 tab));
2671 /* Drop the last constraint added to "tab" in position "r".
2672 * The constraint is expected to have remained in a row.
2674 static isl_stat drop_last_con_in_row(struct isl_tab *tab, int r)
2676 if (!tab->con[r].is_row)
2677 isl_die(isl_tab_get_ctx(tab), isl_error_internal,
2678 "row unexpectedly moved to column",
2679 return isl_stat_error);
2680 if (r + 1 != tab->n_con)
2681 isl_die(isl_tab_get_ctx(tab), isl_error_internal,
2682 "additional constraints added", return isl_stat_error);
2683 if (drop_row(tab, tab->con[r].index) < 0)
2684 return isl_stat_error;
2686 return isl_stat_ok;
2689 /* Given a non-negative variable "var", temporarily add a new non-negative
2690 * variable that is the opposite of "var", ensuring that "var" can only attain
2691 * the value zero. The new variable is removed again before this function
2692 * returns. However, the effect of forcing "var" to be zero remains.
2693 * If var = n/d is a row variable, then the new variable = -n/d.
2694 * If var is a column variables, then the new variable = -var.
2695 * If the new variable cannot attain non-negative values, then
2696 * the resulting tableau is empty.
2697 * Otherwise, we know the value will be zero and we close the row.
2699 static isl_stat cut_to_hyperplane(struct isl_tab *tab, struct isl_tab_var *var)
2701 unsigned r;
2702 isl_int *row;
2703 int sgn;
2704 unsigned off = 2 + tab->M;
2706 if (var->is_zero)
2707 return isl_stat_ok;
2708 if (var->is_redundant || !var->is_nonneg)
2709 isl_die(isl_tab_get_ctx(tab), isl_error_invalid,
2710 "expecting non-redundant non-negative variable",
2711 return isl_stat_error);
2713 if (isl_tab_extend_cons(tab, 1) < 0)
2714 return isl_stat_error;
2716 r = tab->n_con;
2717 tab->con[r].index = tab->n_row;
2718 tab->con[r].is_row = 1;
2719 tab->con[r].is_nonneg = 0;
2720 tab->con[r].is_zero = 0;
2721 tab->con[r].is_redundant = 0;
2722 tab->con[r].frozen = 0;
2723 tab->con[r].negated = 0;
2724 tab->row_var[tab->n_row] = ~r;
2725 row = tab->mat->row[tab->n_row];
2727 if (var->is_row) {
2728 isl_int_set(row[0], tab->mat->row[var->index][0]);
2729 isl_seq_neg(row + 1,
2730 tab->mat->row[var->index] + 1, 1 + tab->n_col);
2731 } else {
2732 isl_int_set_si(row[0], 1);
2733 isl_seq_clr(row + 1, 1 + tab->n_col);
2734 isl_int_set_si(row[off + var->index], -1);
2737 tab->n_row++;
2738 tab->n_con++;
2740 sgn = sign_of_max(tab, &tab->con[r]);
2741 if (sgn < -1)
2742 return isl_stat_error;
2743 if (sgn < 0) {
2744 if (drop_last_con_in_row(tab, r) < 0)
2745 return isl_stat_error;
2746 if (isl_tab_mark_empty(tab) < 0)
2747 return isl_stat_error;
2748 return isl_stat_ok;
2750 tab->con[r].is_nonneg = 1;
2751 /* sgn == 0 */
2752 if (close_row(tab, &tab->con[r], 1) < 0)
2753 return isl_stat_error;
2754 if (drop_last_con_in_row(tab, r) < 0)
2755 return isl_stat_error;
2757 return isl_stat_ok;
2760 /* Given a tableau "tab" and an inequality constraint "con" of the tableau,
2761 * relax the inequality by one. That is, the inequality r >= 0 is replaced
2762 * by r' = r + 1 >= 0.
2763 * If r is a row variable, we simply increase the constant term by one
2764 * (taking into account the denominator).
2765 * If r is a column variable, then we need to modify each row that
2766 * refers to r = r' - 1 by substituting this equality, effectively
2767 * subtracting the coefficient of the column from the constant.
2768 * We should only do this if the minimum is manifestly unbounded,
2769 * however. Otherwise, we may end up with negative sample values
2770 * for non-negative variables.
2771 * So, if r is a column variable with a minimum that is not
2772 * manifestly unbounded, then we need to move it to a row.
2773 * However, the sample value of this row may be negative,
2774 * even after the relaxation, so we need to restore it.
2775 * We therefore prefer to pivot a column up to a row, if possible.
2777 int isl_tab_relax(struct isl_tab *tab, int con)
2779 struct isl_tab_var *var;
2781 if (!tab)
2782 return -1;
2784 var = &tab->con[con];
2786 if (var->is_row && (var->index < 0 || var->index < tab->n_redundant))
2787 isl_die(tab->mat->ctx, isl_error_invalid,
2788 "cannot relax redundant constraint", return -1);
2789 if (!var->is_row && (var->index < 0 || var->index < tab->n_dead))
2790 isl_die(tab->mat->ctx, isl_error_invalid,
2791 "cannot relax dead constraint", return -1);
2793 if (!var->is_row && !max_is_manifestly_unbounded(tab, var))
2794 if (to_row(tab, var, 1) < 0)
2795 return -1;
2796 if (!var->is_row && !min_is_manifestly_unbounded(tab, var))
2797 if (to_row(tab, var, -1) < 0)
2798 return -1;
2800 if (var->is_row) {
2801 isl_int_add(tab->mat->row[var->index][1],
2802 tab->mat->row[var->index][1], tab->mat->row[var->index][0]);
2803 if (restore_row(tab, var) < 0)
2804 return -1;
2805 } else {
2806 int i;
2807 unsigned off = 2 + tab->M;
2809 for (i = 0; i < tab->n_row; ++i) {
2810 if (isl_int_is_zero(tab->mat->row[i][off + var->index]))
2811 continue;
2812 isl_int_sub(tab->mat->row[i][1], tab->mat->row[i][1],
2813 tab->mat->row[i][off + var->index]);
2818 if (isl_tab_push_var(tab, isl_tab_undo_relax, var) < 0)
2819 return -1;
2821 return 0;
2824 /* Replace the variable v at position "pos" in the tableau "tab"
2825 * by v' = v + shift.
2827 * If the variable is in a column, then we first check if we can
2828 * simply plug in v = v' - shift. The effect on a row with
2829 * coefficient f/d for variable v is that the constant term c/d
2830 * is replaced by (c - f * shift)/d. If shift is positive and
2831 * f is negative for each row that needs to remain non-negative,
2832 * then this is clearly safe. In other words, if the minimum of v
2833 * is manifestly unbounded, then we can keep v in a column position.
2834 * Otherwise, we can pivot it down to a row.
2835 * Similarly, if shift is negative, we need to check if the maximum
2836 * of is manifestly unbounded.
2838 * If the variable is in a row (from the start or after pivoting),
2839 * then the constant term c/d is replaced by (c + d * shift)/d.
2841 int isl_tab_shift_var(struct isl_tab *tab, int pos, isl_int shift)
2843 struct isl_tab_var *var;
2845 if (!tab)
2846 return -1;
2847 if (isl_int_is_zero(shift))
2848 return 0;
2850 var = &tab->var[pos];
2851 if (!var->is_row) {
2852 if (isl_int_is_neg(shift)) {
2853 if (!max_is_manifestly_unbounded(tab, var))
2854 if (to_row(tab, var, 1) < 0)
2855 return -1;
2856 } else {
2857 if (!min_is_manifestly_unbounded(tab, var))
2858 if (to_row(tab, var, -1) < 0)
2859 return -1;
2863 if (var->is_row) {
2864 isl_int_addmul(tab->mat->row[var->index][1],
2865 shift, tab->mat->row[var->index][0]);
2866 } else {
2867 int i;
2868 unsigned off = 2 + tab->M;
2870 for (i = 0; i < tab->n_row; ++i) {
2871 if (isl_int_is_zero(tab->mat->row[i][off + var->index]))
2872 continue;
2873 isl_int_submul(tab->mat->row[i][1],
2874 shift, tab->mat->row[i][off + var->index]);
2879 return 0;
2882 /* Remove the sign constraint from constraint "con".
2884 * If the constraint variable was originally marked non-negative,
2885 * then we make sure we mark it non-negative again during rollback.
2887 int isl_tab_unrestrict(struct isl_tab *tab, int con)
2889 struct isl_tab_var *var;
2891 if (!tab)
2892 return -1;
2894 var = &tab->con[con];
2895 if (!var->is_nonneg)
2896 return 0;
2898 var->is_nonneg = 0;
2899 if (isl_tab_push_var(tab, isl_tab_undo_unrestrict, var) < 0)
2900 return -1;
2902 return 0;
2905 int isl_tab_select_facet(struct isl_tab *tab, int con)
2907 if (!tab)
2908 return -1;
2910 return cut_to_hyperplane(tab, &tab->con[con]);
2913 static int may_be_equality(struct isl_tab *tab, int row)
2915 return tab->rational ? isl_int_is_zero(tab->mat->row[row][1])
2916 : isl_int_lt(tab->mat->row[row][1],
2917 tab->mat->row[row][0]);
2920 /* Return an isl_tab_var that has been marked or NULL if no such
2921 * variable can be found.
2922 * The marked field has only been set for variables that
2923 * appear in non-redundant rows or non-dead columns.
2925 * Pick the last constraint variable that is marked and
2926 * that appears in either a non-redundant row or a non-dead columns.
2927 * Since the returned variable is tested for being a redundant constraint or
2928 * an implicit equality, there is no need to return any tab variable that
2929 * corresponds to a variable.
2931 static struct isl_tab_var *select_marked(struct isl_tab *tab)
2933 int i;
2934 struct isl_tab_var *var;
2936 for (i = tab->n_con - 1; i >= 0; --i) {
2937 var = &tab->con[i];
2938 if (var->index < 0)
2939 continue;
2940 if (var->is_row && var->index < tab->n_redundant)
2941 continue;
2942 if (!var->is_row && var->index < tab->n_dead)
2943 continue;
2944 if (var->marked)
2945 return var;
2948 return NULL;
2951 /* Check for (near) equalities among the constraints.
2952 * A constraint is an equality if it is non-negative and if
2953 * its maximal value is either
2954 * - zero (in case of rational tableaus), or
2955 * - strictly less than 1 (in case of integer tableaus)
2957 * We first mark all non-redundant and non-dead variables that
2958 * are not frozen and not obviously not an equality.
2959 * Then we iterate over all marked variables if they can attain
2960 * any values larger than zero or at least one.
2961 * If the maximal value is zero, we mark any column variables
2962 * that appear in the row as being zero and mark the row as being redundant.
2963 * Otherwise, if the maximal value is strictly less than one (and the
2964 * tableau is integer), then we restrict the value to being zero
2965 * by adding an opposite non-negative variable.
2966 * The order in which the variables are considered is not important.
2968 int isl_tab_detect_implicit_equalities(struct isl_tab *tab)
2970 int i;
2971 unsigned n_marked;
2973 if (!tab)
2974 return -1;
2975 if (tab->empty)
2976 return 0;
2977 if (tab->n_dead == tab->n_col)
2978 return 0;
2980 n_marked = 0;
2981 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2982 struct isl_tab_var *var = isl_tab_var_from_row(tab, i);
2983 var->marked = !var->frozen && var->is_nonneg &&
2984 may_be_equality(tab, i);
2985 if (var->marked)
2986 n_marked++;
2988 for (i = tab->n_dead; i < tab->n_col; ++i) {
2989 struct isl_tab_var *var = var_from_col(tab, i);
2990 var->marked = !var->frozen && var->is_nonneg;
2991 if (var->marked)
2992 n_marked++;
2994 while (n_marked) {
2995 struct isl_tab_var *var;
2996 int sgn;
2997 var = select_marked(tab);
2998 if (!var)
2999 break;
3000 var->marked = 0;
3001 n_marked--;
3002 sgn = sign_of_max(tab, var);
3003 if (sgn < 0)
3004 return -1;
3005 if (sgn == 0) {
3006 if (close_row(tab, var, 0) < 0)
3007 return -1;
3008 } else if (!tab->rational && !at_least_one(tab, var)) {
3009 if (cut_to_hyperplane(tab, var) < 0)
3010 return -1;
3011 return isl_tab_detect_implicit_equalities(tab);
3013 for (i = tab->n_redundant; i < tab->n_row; ++i) {
3014 var = isl_tab_var_from_row(tab, i);
3015 if (!var->marked)
3016 continue;
3017 if (may_be_equality(tab, i))
3018 continue;
3019 var->marked = 0;
3020 n_marked--;
3024 return 0;
3027 /* Update the element of row_var or col_var that corresponds to
3028 * constraint tab->con[i] to a move from position "old" to position "i".
3030 static int update_con_after_move(struct isl_tab *tab, int i, int old)
3032 int *p;
3033 int index;
3035 index = tab->con[i].index;
3036 if (index == -1)
3037 return 0;
3038 p = tab->con[i].is_row ? tab->row_var : tab->col_var;
3039 if (p[index] != ~old)
3040 isl_die(tab->mat->ctx, isl_error_internal,
3041 "broken internal state", return -1);
3042 p[index] = ~i;
3044 return 0;
3047 /* Rotate the "n" constraints starting at "first" to the right,
3048 * putting the last constraint in the position of the first constraint.
3050 static int rotate_constraints(struct isl_tab *tab, int first, int n)
3052 int i, last;
3053 struct isl_tab_var var;
3055 if (n <= 1)
3056 return 0;
3058 last = first + n - 1;
3059 var = tab->con[last];
3060 for (i = last; i > first; --i) {
3061 tab->con[i] = tab->con[i - 1];
3062 if (update_con_after_move(tab, i, i - 1) < 0)
3063 return -1;
3065 tab->con[first] = var;
3066 if (update_con_after_move(tab, first, last) < 0)
3067 return -1;
3069 return 0;
3072 /* Make the equalities that are implicit in "bmap" but that have been
3073 * detected in the corresponding "tab" explicit in "bmap" and update
3074 * "tab" to reflect the new order of the constraints.
3076 * In particular, if inequality i is an implicit equality then
3077 * isl_basic_map_inequality_to_equality will move the inequality
3078 * in front of the other equality and it will move the last inequality
3079 * in the position of inequality i.
3080 * In the tableau, the inequalities of "bmap" are stored after the equalities
3081 * and so the original order
3083 * E E E E E A A A I B B B B L
3085 * is changed into
3087 * I E E E E E A A A L B B B B
3089 * where I is the implicit equality, the E are equalities,
3090 * the A inequalities before I, the B inequalities after I and
3091 * L the last inequality.
3092 * We therefore need to rotate to the right two sets of constraints,
3093 * those up to and including I and those after I.
3095 * If "tab" contains any constraints that are not in "bmap" then they
3096 * appear after those in "bmap" and they should be left untouched.
3098 * Note that this function leaves "bmap" in a temporary state
3099 * as it does not call isl_basic_map_gauss. Calling this function
3100 * is the responsibility of the caller.
3102 __isl_give isl_basic_map *isl_tab_make_equalities_explicit(struct isl_tab *tab,
3103 __isl_take isl_basic_map *bmap)
3105 int i;
3107 if (!tab || !bmap)
3108 return isl_basic_map_free(bmap);
3109 if (tab->empty)
3110 return bmap;
3112 for (i = bmap->n_ineq - 1; i >= 0; --i) {
3113 if (!isl_tab_is_equality(tab, bmap->n_eq + i))
3114 continue;
3115 isl_basic_map_inequality_to_equality(bmap, i);
3116 if (rotate_constraints(tab, 0, tab->n_eq + i + 1) < 0)
3117 return isl_basic_map_free(bmap);
3118 if (rotate_constraints(tab, tab->n_eq + i + 1,
3119 bmap->n_ineq - i) < 0)
3120 return isl_basic_map_free(bmap);
3121 tab->n_eq++;
3124 return bmap;
3127 static int con_is_redundant(struct isl_tab *tab, struct isl_tab_var *var)
3129 if (!tab)
3130 return -1;
3131 if (tab->rational) {
3132 int sgn = sign_of_min(tab, var);
3133 if (sgn < -1)
3134 return -1;
3135 return sgn >= 0;
3136 } else {
3137 int irred = isl_tab_min_at_most_neg_one(tab, var);
3138 if (irred < 0)
3139 return -1;
3140 return !irred;
3144 /* Check for (near) redundant constraints.
3145 * A constraint is redundant if it is non-negative and if
3146 * its minimal value (temporarily ignoring the non-negativity) is either
3147 * - zero (in case of rational tableaus), or
3148 * - strictly larger than -1 (in case of integer tableaus)
3150 * We first mark all non-redundant and non-dead variables that
3151 * are not frozen and not obviously negatively unbounded.
3152 * Then we iterate over all marked variables if they can attain
3153 * any values smaller than zero or at most negative one.
3154 * If not, we mark the row as being redundant (assuming it hasn't
3155 * been detected as being obviously redundant in the mean time).
3157 int isl_tab_detect_redundant(struct isl_tab *tab)
3159 int i;
3160 unsigned n_marked;
3162 if (!tab)
3163 return -1;
3164 if (tab->empty)
3165 return 0;
3166 if (tab->n_redundant == tab->n_row)
3167 return 0;
3169 n_marked = 0;
3170 for (i = tab->n_redundant; i < tab->n_row; ++i) {
3171 struct isl_tab_var *var = isl_tab_var_from_row(tab, i);
3172 var->marked = !var->frozen && var->is_nonneg;
3173 if (var->marked)
3174 n_marked++;
3176 for (i = tab->n_dead; i < tab->n_col; ++i) {
3177 struct isl_tab_var *var = var_from_col(tab, i);
3178 var->marked = !var->frozen && var->is_nonneg &&
3179 !min_is_manifestly_unbounded(tab, var);
3180 if (var->marked)
3181 n_marked++;
3183 while (n_marked) {
3184 struct isl_tab_var *var;
3185 int red;
3186 var = select_marked(tab);
3187 if (!var)
3188 break;
3189 var->marked = 0;
3190 n_marked--;
3191 red = con_is_redundant(tab, var);
3192 if (red < 0)
3193 return -1;
3194 if (red && !var->is_redundant)
3195 if (isl_tab_mark_redundant(tab, var->index) < 0)
3196 return -1;
3197 for (i = tab->n_dead; i < tab->n_col; ++i) {
3198 var = var_from_col(tab, i);
3199 if (!var->marked)
3200 continue;
3201 if (!min_is_manifestly_unbounded(tab, var))
3202 continue;
3203 var->marked = 0;
3204 n_marked--;
3208 return 0;
3211 int isl_tab_is_equality(struct isl_tab *tab, int con)
3213 int row;
3214 unsigned off;
3216 if (!tab)
3217 return -1;
3218 if (tab->con[con].is_zero)
3219 return 1;
3220 if (tab->con[con].is_redundant)
3221 return 0;
3222 if (!tab->con[con].is_row)
3223 return tab->con[con].index < tab->n_dead;
3225 row = tab->con[con].index;
3227 off = 2 + tab->M;
3228 return isl_int_is_zero(tab->mat->row[row][1]) &&
3229 !row_is_big(tab, row) &&
3230 isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
3231 tab->n_col - tab->n_dead) == -1;
3234 /* Return the minimal value of the affine expression "f" with denominator
3235 * "denom" in *opt, *opt_denom, assuming the tableau is not empty and
3236 * the expression cannot attain arbitrarily small values.
3237 * If opt_denom is NULL, then *opt is rounded up to the nearest integer.
3238 * The return value reflects the nature of the result (empty, unbounded,
3239 * minimal value returned in *opt).
3241 * This function assumes that at least one more row and at least
3242 * one more element in the constraint array are available in the tableau.
3244 enum isl_lp_result isl_tab_min(struct isl_tab *tab,
3245 isl_int *f, isl_int denom, isl_int *opt, isl_int *opt_denom,
3246 unsigned flags)
3248 int r;
3249 enum isl_lp_result res = isl_lp_ok;
3250 struct isl_tab_var *var;
3251 struct isl_tab_undo *snap;
3253 if (!tab)
3254 return isl_lp_error;
3256 if (tab->empty)
3257 return isl_lp_empty;
3259 snap = isl_tab_snap(tab);
3260 r = isl_tab_add_row(tab, f);
3261 if (r < 0)
3262 return isl_lp_error;
3263 var = &tab->con[r];
3264 for (;;) {
3265 int row, col;
3266 find_pivot(tab, var, var, -1, &row, &col);
3267 if (row == var->index) {
3268 res = isl_lp_unbounded;
3269 break;
3271 if (row == -1)
3272 break;
3273 if (isl_tab_pivot(tab, row, col) < 0)
3274 return isl_lp_error;
3276 isl_int_mul(tab->mat->row[var->index][0],
3277 tab->mat->row[var->index][0], denom);
3278 if (ISL_FL_ISSET(flags, ISL_TAB_SAVE_DUAL)) {
3279 int i;
3281 isl_vec_free(tab->dual);
3282 tab->dual = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_con);
3283 if (!tab->dual)
3284 return isl_lp_error;
3285 isl_int_set(tab->dual->el[0], tab->mat->row[var->index][0]);
3286 for (i = 0; i < tab->n_con; ++i) {
3287 int pos;
3288 if (tab->con[i].is_row) {
3289 isl_int_set_si(tab->dual->el[1 + i], 0);
3290 continue;
3292 pos = 2 + tab->M + tab->con[i].index;
3293 if (tab->con[i].negated)
3294 isl_int_neg(tab->dual->el[1 + i],
3295 tab->mat->row[var->index][pos]);
3296 else
3297 isl_int_set(tab->dual->el[1 + i],
3298 tab->mat->row[var->index][pos]);
3301 if (opt && res == isl_lp_ok) {
3302 if (opt_denom) {
3303 isl_int_set(*opt, tab->mat->row[var->index][1]);
3304 isl_int_set(*opt_denom, tab->mat->row[var->index][0]);
3305 } else
3306 get_rounded_sample_value(tab, var, 1, opt);
3308 if (isl_tab_rollback(tab, snap) < 0)
3309 return isl_lp_error;
3310 return res;
3313 /* Is the constraint at position "con" marked as being redundant?
3314 * If it is marked as representing an equality, then it is not
3315 * considered to be redundant.
3316 * Note that isl_tab_mark_redundant marks both the isl_tab_var as
3317 * redundant and moves the corresponding row into the first
3318 * tab->n_redundant positions (or removes the row, assigning it index -1),
3319 * so the final test is actually redundant itself.
3321 int isl_tab_is_redundant(struct isl_tab *tab, int con)
3323 if (!tab)
3324 return -1;
3325 if (con < 0 || con >= tab->n_con)
3326 isl_die(isl_tab_get_ctx(tab), isl_error_invalid,
3327 "position out of bounds", return -1);
3328 if (tab->con[con].is_zero)
3329 return 0;
3330 if (tab->con[con].is_redundant)
3331 return 1;
3332 return tab->con[con].is_row && tab->con[con].index < tab->n_redundant;
3335 /* Is variable "var" of "tab" fixed to a constant value by its row
3336 * in the tableau?
3337 * If so and if "value" is not NULL, then store this constant value
3338 * in "value".
3340 * That is, is it a row variable that only has non-zero coefficients
3341 * for dead columns?
3343 static isl_bool is_constant(struct isl_tab *tab, struct isl_tab_var *var,
3344 isl_int *value)
3346 unsigned off = 2 + tab->M;
3347 isl_mat *mat = tab->mat;
3348 int n;
3349 int row;
3350 int pos;
3352 if (!var->is_row)
3353 return isl_bool_false;
3354 row = var->index;
3355 if (row_is_big(tab, row))
3356 return isl_bool_false;
3357 n = tab->n_col - tab->n_dead;
3358 pos = isl_seq_first_non_zero(mat->row[row] + off + tab->n_dead, n);
3359 if (pos != -1)
3360 return isl_bool_false;
3361 if (value)
3362 isl_int_divexact(*value, mat->row[row][1], mat->row[row][0]);
3363 return isl_bool_true;
3366 /* Has the variable "var' of "tab" reached a value that is greater than
3367 * or equal (if sgn > 0) or smaller than or equal (if sgn < 0) to "target"?
3368 * "tmp" has been initialized by the caller and can be used
3369 * to perform local computations.
3371 * If the sample value involves the big parameter, then any value
3372 * is reached.
3373 * Otherwise check if n/d >= t, i.e., n >= d * t (if sgn > 0)
3374 * or n/d <= t, i.e., n <= d * t (if sgn < 0).
3376 static int reached(struct isl_tab *tab, struct isl_tab_var *var, int sgn,
3377 isl_int target, isl_int *tmp)
3379 if (row_is_big(tab, var->index))
3380 return 1;
3381 isl_int_mul(*tmp, tab->mat->row[var->index][0], target);
3382 if (sgn > 0)
3383 return isl_int_ge(tab->mat->row[var->index][1], *tmp);
3384 else
3385 return isl_int_le(tab->mat->row[var->index][1], *tmp);
3388 /* Can variable "var" of "tab" attain the value "target" by
3389 * pivoting up (if sgn > 0) or down (if sgn < 0)?
3390 * If not, then pivot up [down] to the greatest [smallest]
3391 * rational value.
3392 * "tmp" has been initialized by the caller and can be used
3393 * to perform local computations.
3395 * If the variable is manifestly unbounded in the desired direction,
3396 * then it can attain any value.
3397 * Otherwise, it can be moved to a row.
3398 * Continue pivoting until the target is reached.
3399 * If no more pivoting can be performed, the maximal [minimal]
3400 * rational value has been reached and the target cannot be reached.
3401 * If the variable would be pivoted into a manifestly unbounded column,
3402 * then the target can be reached.
3404 static isl_bool var_reaches(struct isl_tab *tab, struct isl_tab_var *var,
3405 int sgn, isl_int target, isl_int *tmp)
3407 int row, col;
3409 if (sgn < 0 && min_is_manifestly_unbounded(tab, var))
3410 return isl_bool_true;
3411 if (sgn > 0 && max_is_manifestly_unbounded(tab, var))
3412 return isl_bool_true;
3413 if (to_row(tab, var, sgn) < 0)
3414 return isl_bool_error;
3415 while (!reached(tab, var, sgn, target, tmp)) {
3416 find_pivot(tab, var, var, sgn, &row, &col);
3417 if (row == -1)
3418 return isl_bool_false;
3419 if (row == var->index)
3420 return isl_bool_true;
3421 if (isl_tab_pivot(tab, row, col) < 0)
3422 return isl_bool_error;
3425 return isl_bool_true;
3428 /* Check if variable "var" of "tab" can only attain a single (integer)
3429 * value, and, if so, add an equality constraint to fix the variable
3430 * to this single value and store the result in "target".
3431 * "target" and "tmp" have been initialized by the caller.
3433 * Given the current sample value, round it down and check
3434 * whether it is possible to attain a strictly smaller integer value.
3435 * If so, the variable is not restricted to a single integer value.
3436 * Otherwise, the search stops at the smallest rational value.
3437 * Round up this value and check whether it is possible to attain
3438 * a strictly greater integer value.
3439 * If so, the variable is not restricted to a single integer value.
3440 * Otherwise, the search stops at the greatest rational value.
3441 * If rounding down this value yields a value that is different
3442 * from rounding up the smallest rational value, then the variable
3443 * cannot attain any integer value. Mark the tableau empty.
3444 * Otherwise, add an equality constraint that fixes the variable
3445 * to the single integer value found.
3447 static isl_bool detect_constant_with_tmp(struct isl_tab *tab,
3448 struct isl_tab_var *var, isl_int *target, isl_int *tmp)
3450 isl_bool reached;
3451 isl_vec *eq;
3452 int pos;
3453 isl_stat r;
3455 get_rounded_sample_value(tab, var, -1, target);
3456 isl_int_sub_ui(*target, *target, 1);
3457 reached = var_reaches(tab, var, -1, *target, tmp);
3458 if (reached < 0 || reached)
3459 return isl_bool_not(reached);
3460 get_rounded_sample_value(tab, var, 1, target);
3461 isl_int_add_ui(*target, *target, 1);
3462 reached = var_reaches(tab, var, 1, *target, tmp);
3463 if (reached < 0 || reached)
3464 return isl_bool_not(reached);
3465 get_rounded_sample_value(tab, var, -1, tmp);
3466 isl_int_sub_ui(*target, *target, 1);
3467 if (isl_int_ne(*target, *tmp)) {
3468 if (isl_tab_mark_empty(tab) < 0)
3469 return isl_bool_error;
3470 return isl_bool_false;
3473 if (isl_tab_extend_cons(tab, 1) < 0)
3474 return isl_bool_error;
3475 eq = isl_vec_alloc(isl_tab_get_ctx(tab), 1 + tab->n_var);
3476 if (!eq)
3477 return isl_bool_error;
3478 pos = var - tab->var;
3479 isl_seq_clr(eq->el + 1, tab->n_var);
3480 isl_int_set_si(eq->el[1 + pos], -1);
3481 isl_int_set(eq->el[0], *target);
3482 r = isl_tab_add_eq(tab, eq->el);
3483 isl_vec_free(eq);
3485 return r < 0 ? isl_bool_error : isl_bool_true;
3488 /* Check if variable "var" of "tab" can only attain a single (integer)
3489 * value, and, if so, add an equality constraint to fix the variable
3490 * to this single value and store the result in "value" (if "value"
3491 * is not NULL).
3493 * If the current sample value involves the big parameter,
3494 * then the variable cannot have a fixed integer value.
3495 * If the variable is already fixed to a single value by its row, then
3496 * there is no need to add another equality constraint.
3498 * Otherwise, allocate some temporary variables and continue
3499 * with detect_constant_with_tmp.
3501 static isl_bool get_constant(struct isl_tab *tab, struct isl_tab_var *var,
3502 isl_int *value)
3504 isl_int target, tmp;
3505 isl_bool is_cst;
3507 if (var->is_row && row_is_big(tab, var->index))
3508 return isl_bool_false;
3509 is_cst = is_constant(tab, var, value);
3510 if (is_cst < 0 || is_cst)
3511 return is_cst;
3513 if (!value)
3514 isl_int_init(target);
3515 isl_int_init(tmp);
3517 is_cst = detect_constant_with_tmp(tab, var,
3518 value ? value : &target, &tmp);
3520 isl_int_clear(tmp);
3521 if (!value)
3522 isl_int_clear(target);
3524 return is_cst;
3527 /* Check if variable "var" of "tab" can only attain a single (integer)
3528 * value, and, if so, add an equality constraint to fix the variable
3529 * to this single value and store the result in "value" (if "value"
3530 * is not NULL).
3532 * For rational tableaus, nothing needs to be done.
3534 isl_bool isl_tab_is_constant(struct isl_tab *tab, int var, isl_int *value)
3536 if (!tab)
3537 return isl_bool_error;
3538 if (var < 0 || var >= tab->n_var)
3539 isl_die(isl_tab_get_ctx(tab), isl_error_invalid,
3540 "position out of bounds", return isl_bool_error);
3541 if (tab->rational)
3542 return isl_bool_false;
3544 return get_constant(tab, &tab->var[var], value);
3547 /* Check if any of the variables of "tab" can only attain a single (integer)
3548 * value, and, if so, add equality constraints to fix those variables
3549 * to these single values.
3551 * For rational tableaus, nothing needs to be done.
3553 isl_stat isl_tab_detect_constants(struct isl_tab *tab)
3555 int i;
3557 if (!tab)
3558 return isl_stat_error;
3559 if (tab->rational)
3560 return isl_stat_ok;
3562 for (i = 0; i < tab->n_var; ++i) {
3563 if (get_constant(tab, &tab->var[i], NULL) < 0)
3564 return isl_stat_error;
3567 return isl_stat_ok;
3570 /* Take a snapshot of the tableau that can be restored by a call to
3571 * isl_tab_rollback.
3573 struct isl_tab_undo *isl_tab_snap(struct isl_tab *tab)
3575 if (!tab)
3576 return NULL;
3577 tab->need_undo = 1;
3578 return tab->top;
3581 /* Does "tab" need to keep track of undo information?
3582 * That is, was a snapshot taken that may need to be restored?
3584 isl_bool isl_tab_need_undo(struct isl_tab *tab)
3586 if (!tab)
3587 return isl_bool_error;
3589 return isl_bool_ok(tab->need_undo);
3592 /* Remove all tracking of undo information from "tab", invalidating
3593 * any snapshots that may have been taken of the tableau.
3594 * Since all snapshots have been invalidated, there is also
3595 * no need to start keeping track of undo information again.
3597 void isl_tab_clear_undo(struct isl_tab *tab)
3599 if (!tab)
3600 return;
3602 free_undo(tab);
3603 tab->need_undo = 0;
3606 /* Undo the operation performed by isl_tab_relax.
3608 static isl_stat unrelax(struct isl_tab *tab, struct isl_tab_var *var)
3609 WARN_UNUSED;
3610 static isl_stat unrelax(struct isl_tab *tab, struct isl_tab_var *var)
3612 unsigned off = 2 + tab->M;
3614 if (!var->is_row && !max_is_manifestly_unbounded(tab, var))
3615 if (to_row(tab, var, 1) < 0)
3616 return isl_stat_error;
3618 if (var->is_row) {
3619 isl_int_sub(tab->mat->row[var->index][1],
3620 tab->mat->row[var->index][1], tab->mat->row[var->index][0]);
3621 if (var->is_nonneg) {
3622 int sgn = restore_row(tab, var);
3623 isl_assert(tab->mat->ctx, sgn >= 0,
3624 return isl_stat_error);
3626 } else {
3627 int i;
3629 for (i = 0; i < tab->n_row; ++i) {
3630 if (isl_int_is_zero(tab->mat->row[i][off + var->index]))
3631 continue;
3632 isl_int_add(tab->mat->row[i][1], tab->mat->row[i][1],
3633 tab->mat->row[i][off + var->index]);
3638 return isl_stat_ok;
3641 /* Undo the operation performed by isl_tab_unrestrict.
3643 * In particular, mark the variable as being non-negative and make
3644 * sure the sample value respects this constraint.
3646 static isl_stat ununrestrict(struct isl_tab *tab, struct isl_tab_var *var)
3648 var->is_nonneg = 1;
3650 if (var->is_row && restore_row(tab, var) < -1)
3651 return isl_stat_error;
3653 return isl_stat_ok;
3656 /* Unmark the last redundant row in "tab" as being redundant.
3657 * This undoes part of the modifications performed by isl_tab_mark_redundant.
3658 * In particular, remove the redundant mark and make
3659 * sure the sample value respects the constraint again.
3660 * A variable that is marked non-negative by isl_tab_mark_redundant
3661 * is covered by a separate undo record.
3663 static isl_stat restore_last_redundant(struct isl_tab *tab)
3665 struct isl_tab_var *var;
3667 if (tab->n_redundant < 1)
3668 isl_die(isl_tab_get_ctx(tab), isl_error_internal,
3669 "no redundant rows", return isl_stat_error);
3671 var = isl_tab_var_from_row(tab, tab->n_redundant - 1);
3672 var->is_redundant = 0;
3673 tab->n_redundant--;
3674 restore_row(tab, var);
3676 return isl_stat_ok;
3679 static isl_stat perform_undo_var(struct isl_tab *tab, struct isl_tab_undo *undo)
3680 WARN_UNUSED;
3681 static isl_stat perform_undo_var(struct isl_tab *tab, struct isl_tab_undo *undo)
3683 struct isl_tab_var *var = var_from_index(tab, undo->u.var_index);
3684 switch (undo->type) {
3685 case isl_tab_undo_nonneg:
3686 var->is_nonneg = 0;
3687 break;
3688 case isl_tab_undo_redundant:
3689 if (!var->is_row || var->index != tab->n_redundant - 1)
3690 isl_die(isl_tab_get_ctx(tab), isl_error_internal,
3691 "not undoing last redundant row",
3692 return isl_stat_error);
3693 return restore_last_redundant(tab);
3694 case isl_tab_undo_freeze:
3695 var->frozen = 0;
3696 break;
3697 case isl_tab_undo_zero:
3698 var->is_zero = 0;
3699 if (!var->is_row)
3700 tab->n_dead--;
3701 break;
3702 case isl_tab_undo_allocate:
3703 if (undo->u.var_index >= 0) {
3704 isl_assert(tab->mat->ctx, !var->is_row,
3705 return isl_stat_error);
3706 return drop_col(tab, var->index);
3708 if (!var->is_row) {
3709 if (!max_is_manifestly_unbounded(tab, var)) {
3710 if (to_row(tab, var, 1) < 0)
3711 return isl_stat_error;
3712 } else if (!min_is_manifestly_unbounded(tab, var)) {
3713 if (to_row(tab, var, -1) < 0)
3714 return isl_stat_error;
3715 } else
3716 if (to_row(tab, var, 0) < 0)
3717 return isl_stat_error;
3719 return drop_row(tab, var->index);
3720 case isl_tab_undo_relax:
3721 return unrelax(tab, var);
3722 case isl_tab_undo_unrestrict:
3723 return ununrestrict(tab, var);
3724 default:
3725 isl_die(tab->mat->ctx, isl_error_internal,
3726 "perform_undo_var called on invalid undo record",
3727 return isl_stat_error);
3730 return isl_stat_ok;
3733 /* Restore all rows that have been marked redundant by isl_tab_mark_redundant
3734 * and that have been preserved in the tableau.
3735 * Note that isl_tab_mark_redundant may also have marked some variables
3736 * as being non-negative before marking them redundant. These need
3737 * to be removed as well as otherwise some constraints could end up
3738 * getting marked redundant with respect to the variable.
3740 isl_stat isl_tab_restore_redundant(struct isl_tab *tab)
3742 if (!tab)
3743 return isl_stat_error;
3745 if (tab->need_undo)
3746 isl_die(isl_tab_get_ctx(tab), isl_error_invalid,
3747 "manually restoring redundant constraints "
3748 "interferes with undo history",
3749 return isl_stat_error);
3751 while (tab->n_redundant > 0) {
3752 if (tab->row_var[tab->n_redundant - 1] >= 0) {
3753 struct isl_tab_var *var;
3755 var = isl_tab_var_from_row(tab, tab->n_redundant - 1);
3756 var->is_nonneg = 0;
3758 restore_last_redundant(tab);
3760 return isl_stat_ok;
3763 /* Undo the addition of an integer division to the basic map representation
3764 * of "tab" in position "pos".
3766 static isl_stat drop_bmap_div(struct isl_tab *tab, int pos)
3768 int off;
3769 isl_size n_div;
3771 n_div = isl_basic_map_dim(tab->bmap, isl_dim_div);
3772 if (n_div < 0)
3773 return isl_stat_error;
3774 off = tab->n_var - n_div;
3775 if (isl_basic_map_drop_div(tab->bmap, pos - off) < 0)
3776 return isl_stat_error;
3777 if (tab->samples) {
3778 tab->samples = isl_mat_drop_cols(tab->samples, 1 + pos, 1);
3779 if (!tab->samples)
3780 return isl_stat_error;
3783 return isl_stat_ok;
3786 /* Restore the tableau to the state where the basic variables
3787 * are those in "col_var".
3788 * We first construct a list of variables that are currently in
3789 * the basis, but shouldn't. Then we iterate over all variables
3790 * that should be in the basis and for each one that is currently
3791 * not in the basis, we exchange it with one of the elements of the
3792 * list constructed before.
3793 * We can always find an appropriate variable to pivot with because
3794 * the current basis is mapped to the old basis by a non-singular
3795 * matrix and so we can never end up with a zero row.
3797 static int restore_basis(struct isl_tab *tab, int *col_var)
3799 int i, j;
3800 int n_extra = 0;
3801 int *extra = NULL; /* current columns that contain bad stuff */
3802 unsigned off = 2 + tab->M;
3804 extra = isl_alloc_array(tab->mat->ctx, int, tab->n_col);
3805 if (tab->n_col && !extra)
3806 goto error;
3807 for (i = 0; i < tab->n_col; ++i) {
3808 for (j = 0; j < tab->n_col; ++j)
3809 if (tab->col_var[i] == col_var[j])
3810 break;
3811 if (j < tab->n_col)
3812 continue;
3813 extra[n_extra++] = i;
3815 for (i = 0; i < tab->n_col && n_extra > 0; ++i) {
3816 struct isl_tab_var *var;
3817 int row;
3819 for (j = 0; j < tab->n_col; ++j)
3820 if (col_var[i] == tab->col_var[j])
3821 break;
3822 if (j < tab->n_col)
3823 continue;
3824 var = var_from_index(tab, col_var[i]);
3825 row = var->index;
3826 for (j = 0; j < n_extra; ++j)
3827 if (!isl_int_is_zero(tab->mat->row[row][off+extra[j]]))
3828 break;
3829 isl_assert(tab->mat->ctx, j < n_extra, goto error);
3830 if (isl_tab_pivot(tab, row, extra[j]) < 0)
3831 goto error;
3832 extra[j] = extra[--n_extra];
3835 free(extra);
3836 return 0;
3837 error:
3838 free(extra);
3839 return -1;
3842 /* Remove all samples with index n or greater, i.e., those samples
3843 * that were added since we saved this number of samples in
3844 * isl_tab_save_samples.
3846 static void drop_samples_since(struct isl_tab *tab, int n)
3848 int i;
3850 for (i = tab->n_sample - 1; i >= 0 && tab->n_sample > n; --i) {
3851 if (tab->sample_index[i] < n)
3852 continue;
3854 if (i != tab->n_sample - 1) {
3855 int t = tab->sample_index[tab->n_sample-1];
3856 tab->sample_index[tab->n_sample-1] = tab->sample_index[i];
3857 tab->sample_index[i] = t;
3858 isl_mat_swap_rows(tab->samples, tab->n_sample-1, i);
3860 tab->n_sample--;
3864 static isl_stat perform_undo(struct isl_tab *tab, struct isl_tab_undo *undo)
3865 WARN_UNUSED;
3866 static isl_stat perform_undo(struct isl_tab *tab, struct isl_tab_undo *undo)
3868 switch (undo->type) {
3869 case isl_tab_undo_rational:
3870 tab->rational = 0;
3871 break;
3872 case isl_tab_undo_empty:
3873 tab->empty = 0;
3874 break;
3875 case isl_tab_undo_nonneg:
3876 case isl_tab_undo_redundant:
3877 case isl_tab_undo_freeze:
3878 case isl_tab_undo_zero:
3879 case isl_tab_undo_allocate:
3880 case isl_tab_undo_relax:
3881 case isl_tab_undo_unrestrict:
3882 return perform_undo_var(tab, undo);
3883 case isl_tab_undo_bmap_eq:
3884 return isl_basic_map_free_equality(tab->bmap, 1);
3885 case isl_tab_undo_bmap_ineq:
3886 return isl_basic_map_free_inequality(tab->bmap, 1);
3887 case isl_tab_undo_bmap_div:
3888 return drop_bmap_div(tab, undo->u.var_index);
3889 case isl_tab_undo_saved_basis:
3890 if (restore_basis(tab, undo->u.col_var) < 0)
3891 return isl_stat_error;
3892 break;
3893 case isl_tab_undo_drop_sample:
3894 tab->n_outside--;
3895 break;
3896 case isl_tab_undo_saved_samples:
3897 drop_samples_since(tab, undo->u.n);
3898 break;
3899 case isl_tab_undo_callback:
3900 return undo->u.callback->run(undo->u.callback);
3901 default:
3902 isl_assert(tab->mat->ctx, 0, return isl_stat_error);
3904 return isl_stat_ok;
3907 /* Return the tableau to the state it was in when the snapshot "snap"
3908 * was taken.
3910 isl_stat isl_tab_rollback(struct isl_tab *tab, struct isl_tab_undo *snap)
3912 struct isl_tab_undo *undo, *next;
3914 if (!tab)
3915 return isl_stat_error;
3917 tab->in_undo = 1;
3918 for (undo = tab->top; undo && undo != &tab->bottom; undo = next) {
3919 next = undo->next;
3920 if (undo == snap)
3921 break;
3922 if (perform_undo(tab, undo) < 0) {
3923 tab->top = undo;
3924 free_undo(tab);
3925 tab->in_undo = 0;
3926 return isl_stat_error;
3928 free_undo_record(undo);
3930 tab->in_undo = 0;
3931 tab->top = undo;
3932 if (!undo)
3933 return isl_stat_error;
3934 return isl_stat_ok;
3937 /* The given row "row" represents an inequality violated by all
3938 * points in the tableau. Check for some special cases of such
3939 * separating constraints.
3940 * In particular, if the row has been reduced to the constant -1,
3941 * then we know the inequality is adjacent (but opposite) to
3942 * an equality in the tableau.
3943 * If the row has been reduced to r = c*(-1 -r'), with r' an inequality
3944 * of the tableau and c a positive constant, then the inequality
3945 * is adjacent (but opposite) to the inequality r'.
3947 static enum isl_ineq_type separation_type(struct isl_tab *tab, unsigned row)
3949 int pos;
3950 unsigned off = 2 + tab->M;
3952 if (tab->rational)
3953 return isl_ineq_separate;
3955 if (!isl_int_is_one(tab->mat->row[row][0]))
3956 return isl_ineq_separate;
3958 pos = isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
3959 tab->n_col - tab->n_dead);
3960 if (pos == -1) {
3961 if (isl_int_is_negone(tab->mat->row[row][1]))
3962 return isl_ineq_adj_eq;
3963 else
3964 return isl_ineq_separate;
3967 if (!isl_int_eq(tab->mat->row[row][1],
3968 tab->mat->row[row][off + tab->n_dead + pos]))
3969 return isl_ineq_separate;
3971 pos = isl_seq_first_non_zero(
3972 tab->mat->row[row] + off + tab->n_dead + pos + 1,
3973 tab->n_col - tab->n_dead - pos - 1);
3975 return pos == -1 ? isl_ineq_adj_ineq : isl_ineq_separate;
3978 /* Check the effect of inequality "ineq" on the tableau "tab".
3979 * The result may be
3980 * isl_ineq_redundant: satisfied by all points in the tableau
3981 * isl_ineq_separate: satisfied by no point in the tableau
3982 * isl_ineq_cut: satisfied by some by not all points
3983 * isl_ineq_adj_eq: adjacent to an equality
3984 * isl_ineq_adj_ineq: adjacent to an inequality.
3986 enum isl_ineq_type isl_tab_ineq_type(struct isl_tab *tab, isl_int *ineq)
3988 enum isl_ineq_type type = isl_ineq_error;
3989 struct isl_tab_undo *snap = NULL;
3990 int con;
3991 int row;
3993 if (!tab)
3994 return isl_ineq_error;
3996 if (isl_tab_extend_cons(tab, 1) < 0)
3997 return isl_ineq_error;
3999 snap = isl_tab_snap(tab);
4001 con = isl_tab_add_row(tab, ineq);
4002 if (con < 0)
4003 goto error;
4005 row = tab->con[con].index;
4006 if (isl_tab_row_is_redundant(tab, row))
4007 type = isl_ineq_redundant;
4008 else if (isl_int_is_neg(tab->mat->row[row][1]) &&
4009 (tab->rational ||
4010 isl_int_abs_ge(tab->mat->row[row][1],
4011 tab->mat->row[row][0]))) {
4012 int nonneg = at_least_zero(tab, &tab->con[con]);
4013 if (nonneg < 0)
4014 goto error;
4015 if (nonneg)
4016 type = isl_ineq_cut;
4017 else
4018 type = separation_type(tab, row);
4019 } else {
4020 int red = con_is_redundant(tab, &tab->con[con]);
4021 if (red < 0)
4022 goto error;
4023 if (!red)
4024 type = isl_ineq_cut;
4025 else
4026 type = isl_ineq_redundant;
4029 if (isl_tab_rollback(tab, snap))
4030 return isl_ineq_error;
4031 return type;
4032 error:
4033 return isl_ineq_error;
4036 isl_stat isl_tab_track_bmap(struct isl_tab *tab, __isl_take isl_basic_map *bmap)
4038 bmap = isl_basic_map_cow(bmap);
4039 if (!tab || !bmap)
4040 goto error;
4042 if (tab->empty) {
4043 bmap = isl_basic_map_set_to_empty(bmap);
4044 if (!bmap)
4045 goto error;
4046 tab->bmap = bmap;
4047 return isl_stat_ok;
4050 isl_assert(tab->mat->ctx, tab->n_eq == bmap->n_eq, goto error);
4051 isl_assert(tab->mat->ctx,
4052 tab->n_con == bmap->n_eq + bmap->n_ineq, goto error);
4054 tab->bmap = bmap;
4056 return isl_stat_ok;
4057 error:
4058 isl_basic_map_free(bmap);
4059 return isl_stat_error;
4062 isl_stat isl_tab_track_bset(struct isl_tab *tab, __isl_take isl_basic_set *bset)
4064 return isl_tab_track_bmap(tab, bset_to_bmap(bset));
4067 __isl_keep isl_basic_set *isl_tab_peek_bset(struct isl_tab *tab)
4069 if (!tab)
4070 return NULL;
4072 return bset_from_bmap(tab->bmap);
4075 static void isl_tab_print_internal(__isl_keep struct isl_tab *tab,
4076 FILE *out, int indent)
4078 unsigned r, c;
4079 int i;
4081 if (!tab) {
4082 fprintf(out, "%*snull tab\n", indent, "");
4083 return;
4085 fprintf(out, "%*sn_redundant: %d, n_dead: %d", indent, "",
4086 tab->n_redundant, tab->n_dead);
4087 if (tab->rational)
4088 fprintf(out, ", rational");
4089 if (tab->empty)
4090 fprintf(out, ", empty");
4091 fprintf(out, "\n");
4092 fprintf(out, "%*s[", indent, "");
4093 for (i = 0; i < tab->n_var; ++i) {
4094 if (i)
4095 fprintf(out, (i == tab->n_param ||
4096 i == tab->n_var - tab->n_div) ? "; "
4097 : ", ");
4098 fprintf(out, "%c%d%s", tab->var[i].is_row ? 'r' : 'c',
4099 tab->var[i].index,
4100 tab->var[i].is_zero ? " [=0]" :
4101 tab->var[i].is_redundant ? " [R]" : "");
4103 fprintf(out, "]\n");
4104 fprintf(out, "%*s[", indent, "");
4105 for (i = 0; i < tab->n_con; ++i) {
4106 if (i)
4107 fprintf(out, ", ");
4108 fprintf(out, "%c%d%s", tab->con[i].is_row ? 'r' : 'c',
4109 tab->con[i].index,
4110 tab->con[i].is_zero ? " [=0]" :
4111 tab->con[i].is_redundant ? " [R]" : "");
4113 fprintf(out, "]\n");
4114 fprintf(out, "%*s[", indent, "");
4115 for (i = 0; i < tab->n_row; ++i) {
4116 const char *sign = "";
4117 if (i)
4118 fprintf(out, ", ");
4119 if (tab->row_sign) {
4120 if (tab->row_sign[i] == isl_tab_row_unknown)
4121 sign = "?";
4122 else if (tab->row_sign[i] == isl_tab_row_neg)
4123 sign = "-";
4124 else if (tab->row_sign[i] == isl_tab_row_pos)
4125 sign = "+";
4126 else
4127 sign = "+-";
4129 fprintf(out, "r%d: %d%s%s", i, tab->row_var[i],
4130 isl_tab_var_from_row(tab, i)->is_nonneg ? " [>=0]" : "", sign);
4132 fprintf(out, "]\n");
4133 fprintf(out, "%*s[", indent, "");
4134 for (i = 0; i < tab->n_col; ++i) {
4135 if (i)
4136 fprintf(out, ", ");
4137 fprintf(out, "c%d: %d%s", i, tab->col_var[i],
4138 var_from_col(tab, i)->is_nonneg ? " [>=0]" : "");
4140 fprintf(out, "]\n");
4141 r = tab->mat->n_row;
4142 tab->mat->n_row = tab->n_row;
4143 c = tab->mat->n_col;
4144 tab->mat->n_col = 2 + tab->M + tab->n_col;
4145 isl_mat_print_internal(tab->mat, out, indent);
4146 tab->mat->n_row = r;
4147 tab->mat->n_col = c;
4148 if (tab->bmap)
4149 isl_basic_map_print_internal(tab->bmap, out, indent);
4152 void isl_tab_dump(__isl_keep struct isl_tab *tab)
4154 isl_tab_print_internal(tab, stderr, 0);