isl_tab_track_bset: return isl_stat
[isl.git] / isl_tab.c
blob5977f940295527c8d93810d515f20fae34a756fb
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 int push_union(struct isl_tab *tab,
790 enum isl_tab_undo_type type, union isl_tab_undo_val u) WARN_UNUSED;
791 static int push_union(struct isl_tab *tab,
792 enum isl_tab_undo_type type, union isl_tab_undo_val u)
794 struct isl_tab_undo *undo;
796 if (!tab)
797 return -1;
798 if (!tab->need_undo)
799 return 0;
801 undo = isl_alloc_type(tab->mat->ctx, struct isl_tab_undo);
802 if (!undo)
803 return -1;
804 undo->type = type;
805 undo->u = u;
806 undo->next = tab->top;
807 tab->top = undo;
809 return 0;
812 int isl_tab_push_var(struct isl_tab *tab,
813 enum isl_tab_undo_type type, struct isl_tab_var *var)
815 union isl_tab_undo_val u;
816 if (var->is_row)
817 u.var_index = tab->row_var[var->index];
818 else
819 u.var_index = tab->col_var[var->index];
820 return push_union(tab, type, u);
823 int isl_tab_push(struct isl_tab *tab, enum isl_tab_undo_type type)
825 union isl_tab_undo_val u = { 0 };
826 return push_union(tab, type, u);
829 /* Push a record on the undo stack describing the current basic
830 * variables, so that the this state can be restored during rollback.
832 int isl_tab_push_basis(struct isl_tab *tab)
834 int i;
835 union isl_tab_undo_val u;
837 u.col_var = isl_alloc_array(tab->mat->ctx, int, tab->n_col);
838 if (tab->n_col && !u.col_var)
839 return -1;
840 for (i = 0; i < tab->n_col; ++i)
841 u.col_var[i] = tab->col_var[i];
842 return push_union(tab, isl_tab_undo_saved_basis, u);
845 int isl_tab_push_callback(struct isl_tab *tab, struct isl_tab_callback *callback)
847 union isl_tab_undo_val u;
848 u.callback = callback;
849 return push_union(tab, isl_tab_undo_callback, u);
852 struct isl_tab *isl_tab_init_samples(struct isl_tab *tab)
854 if (!tab)
855 return NULL;
857 tab->n_sample = 0;
858 tab->n_outside = 0;
859 tab->samples = isl_mat_alloc(tab->mat->ctx, 1, 1 + tab->n_var);
860 if (!tab->samples)
861 goto error;
862 tab->sample_index = isl_alloc_array(tab->mat->ctx, int, 1);
863 if (!tab->sample_index)
864 goto error;
865 return tab;
866 error:
867 isl_tab_free(tab);
868 return NULL;
871 int isl_tab_add_sample(struct isl_tab *tab, __isl_take isl_vec *sample)
873 if (!tab || !sample)
874 goto error;
876 if (tab->n_sample + 1 > tab->samples->n_row) {
877 int *t = isl_realloc_array(tab->mat->ctx,
878 tab->sample_index, int, tab->n_sample + 1);
879 if (!t)
880 goto error;
881 tab->sample_index = t;
884 tab->samples = isl_mat_extend(tab->samples,
885 tab->n_sample + 1, tab->samples->n_col);
886 if (!tab->samples)
887 goto error;
889 isl_seq_cpy(tab->samples->row[tab->n_sample], sample->el, sample->size);
890 isl_vec_free(sample);
891 tab->sample_index[tab->n_sample] = tab->n_sample;
892 tab->n_sample++;
894 return 0;
895 error:
896 isl_vec_free(sample);
897 return -1;
900 struct isl_tab *isl_tab_drop_sample(struct isl_tab *tab, int s)
902 if (s != tab->n_outside) {
903 int t = tab->sample_index[tab->n_outside];
904 tab->sample_index[tab->n_outside] = tab->sample_index[s];
905 tab->sample_index[s] = t;
906 isl_mat_swap_rows(tab->samples, tab->n_outside, s);
908 tab->n_outside++;
909 if (isl_tab_push(tab, isl_tab_undo_drop_sample) < 0) {
910 isl_tab_free(tab);
911 return NULL;
914 return tab;
917 /* Record the current number of samples so that we can remove newer
918 * samples during a rollback.
920 int isl_tab_save_samples(struct isl_tab *tab)
922 union isl_tab_undo_val u;
924 if (!tab)
925 return -1;
927 u.n = tab->n_sample;
928 return push_union(tab, isl_tab_undo_saved_samples, u);
931 /* Mark row with index "row" as being redundant.
932 * If we may need to undo the operation or if the row represents
933 * a variable of the original problem, the row is kept,
934 * but no longer considered when looking for a pivot row.
935 * Otherwise, the row is simply removed.
937 * The row may be interchanged with some other row. If it
938 * is interchanged with a later row, return 1. Otherwise return 0.
939 * If the rows are checked in order in the calling function,
940 * then a return value of 1 means that the row with the given
941 * row number may now contain a different row that hasn't been checked yet.
943 int isl_tab_mark_redundant(struct isl_tab *tab, int row)
945 struct isl_tab_var *var = isl_tab_var_from_row(tab, row);
946 var->is_redundant = 1;
947 isl_assert(tab->mat->ctx, row >= tab->n_redundant, return -1);
948 if (tab->preserve || tab->need_undo || tab->row_var[row] >= 0) {
949 if (tab->row_var[row] >= 0 && !var->is_nonneg) {
950 var->is_nonneg = 1;
951 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, var) < 0)
952 return -1;
954 if (row != tab->n_redundant)
955 swap_rows(tab, row, tab->n_redundant);
956 tab->n_redundant++;
957 return isl_tab_push_var(tab, isl_tab_undo_redundant, var);
958 } else {
959 if (row != tab->n_row - 1)
960 swap_rows(tab, row, tab->n_row - 1);
961 isl_tab_var_from_row(tab, tab->n_row - 1)->index = -1;
962 tab->n_row--;
963 return 1;
967 /* Mark "tab" as a rational tableau.
968 * If it wasn't marked as a rational tableau already and if we may
969 * need to undo changes, then arrange for the marking to be undone
970 * during the undo.
972 int isl_tab_mark_rational(struct isl_tab *tab)
974 if (!tab)
975 return -1;
976 if (!tab->rational && tab->need_undo)
977 if (isl_tab_push(tab, isl_tab_undo_rational) < 0)
978 return -1;
979 tab->rational = 1;
980 return 0;
983 isl_stat isl_tab_mark_empty(struct isl_tab *tab)
985 if (!tab)
986 return isl_stat_error;
987 if (!tab->empty && tab->need_undo)
988 if (isl_tab_push(tab, isl_tab_undo_empty) < 0)
989 return isl_stat_error;
990 tab->empty = 1;
991 return isl_stat_ok;
994 int isl_tab_freeze_constraint(struct isl_tab *tab, int con)
996 struct isl_tab_var *var;
998 if (!tab)
999 return -1;
1001 var = &tab->con[con];
1002 if (var->frozen)
1003 return 0;
1004 if (var->index < 0)
1005 return 0;
1006 var->frozen = 1;
1008 if (tab->need_undo)
1009 return isl_tab_push_var(tab, isl_tab_undo_freeze, var);
1011 return 0;
1014 /* Update the rows signs after a pivot of "row" and "col", with "row_sgn"
1015 * the original sign of the pivot element.
1016 * We only keep track of row signs during PILP solving and in this case
1017 * we only pivot a row with negative sign (meaning the value is always
1018 * non-positive) using a positive pivot element.
1020 * For each row j, the new value of the parametric constant is equal to
1022 * a_j0 - a_jc a_r0/a_rc
1024 * where a_j0 is the original parametric constant, a_rc is the pivot element,
1025 * a_r0 is the parametric constant of the pivot row and a_jc is the
1026 * pivot column entry of the row j.
1027 * Since a_r0 is non-positive and a_rc is positive, the sign of row j
1028 * remains the same if a_jc has the same sign as the row j or if
1029 * a_jc is zero. In all other cases, we reset the sign to "unknown".
1031 static void update_row_sign(struct isl_tab *tab, int row, int col, int row_sgn)
1033 int i;
1034 struct isl_mat *mat = tab->mat;
1035 unsigned off = 2 + tab->M;
1037 if (!tab->row_sign)
1038 return;
1040 if (tab->row_sign[row] == 0)
1041 return;
1042 isl_assert(mat->ctx, row_sgn > 0, return);
1043 isl_assert(mat->ctx, tab->row_sign[row] == isl_tab_row_neg, return);
1044 tab->row_sign[row] = isl_tab_row_pos;
1045 for (i = 0; i < tab->n_row; ++i) {
1046 int s;
1047 if (i == row)
1048 continue;
1049 s = isl_int_sgn(mat->row[i][off + col]);
1050 if (!s)
1051 continue;
1052 if (!tab->row_sign[i])
1053 continue;
1054 if (s < 0 && tab->row_sign[i] == isl_tab_row_neg)
1055 continue;
1056 if (s > 0 && tab->row_sign[i] == isl_tab_row_pos)
1057 continue;
1058 tab->row_sign[i] = isl_tab_row_unknown;
1062 /* Given a row number "row" and a column number "col", pivot the tableau
1063 * such that the associated variables are interchanged.
1064 * The given row in the tableau expresses
1066 * x_r = a_r0 + \sum_i a_ri x_i
1068 * or
1070 * x_c = 1/a_rc x_r - a_r0/a_rc + sum_{i \ne r} -a_ri/a_rc
1072 * Substituting this equality into the other rows
1074 * x_j = a_j0 + \sum_i a_ji x_i
1076 * with a_jc \ne 0, we obtain
1078 * 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
1080 * The tableau
1082 * n_rc/d_r n_ri/d_r
1083 * n_jc/d_j n_ji/d_j
1085 * where i is any other column and j is any other row,
1086 * is therefore transformed into
1088 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1089 * 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)
1091 * The transformation is performed along the following steps
1093 * d_r/n_rc n_ri/n_rc
1094 * n_jc/d_j n_ji/d_j
1096 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1097 * n_jc/d_j n_ji/d_j
1099 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1100 * n_jc/(|n_rc| d_j) n_ji/(|n_rc| d_j)
1102 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1103 * n_jc/(|n_rc| d_j) (n_ji |n_rc|)/(|n_rc| d_j)
1105 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1106 * n_jc/(|n_rc| d_j) (n_ji |n_rc| - s(n_rc)n_jc n_ri)/(|n_rc| d_j)
1108 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1109 * 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)
1112 int isl_tab_pivot(struct isl_tab *tab, int row, int col)
1114 int i, j;
1115 int sgn;
1116 int t;
1117 isl_ctx *ctx;
1118 struct isl_mat *mat = tab->mat;
1119 struct isl_tab_var *var;
1120 unsigned off = 2 + tab->M;
1122 ctx = isl_tab_get_ctx(tab);
1123 if (isl_ctx_next_operation(ctx) < 0)
1124 return -1;
1126 isl_int_swap(mat->row[row][0], mat->row[row][off + col]);
1127 sgn = isl_int_sgn(mat->row[row][0]);
1128 if (sgn < 0) {
1129 isl_int_neg(mat->row[row][0], mat->row[row][0]);
1130 isl_int_neg(mat->row[row][off + col], mat->row[row][off + col]);
1131 } else
1132 for (j = 0; j < off - 1 + tab->n_col; ++j) {
1133 if (j == off - 1 + col)
1134 continue;
1135 isl_int_neg(mat->row[row][1 + j], mat->row[row][1 + j]);
1137 if (!isl_int_is_one(mat->row[row][0]))
1138 isl_seq_normalize(mat->ctx, mat->row[row], off + tab->n_col);
1139 for (i = 0; i < tab->n_row; ++i) {
1140 if (i == row)
1141 continue;
1142 if (isl_int_is_zero(mat->row[i][off + col]))
1143 continue;
1144 isl_int_mul(mat->row[i][0], mat->row[i][0], mat->row[row][0]);
1145 for (j = 0; j < off - 1 + tab->n_col; ++j) {
1146 if (j == off - 1 + col)
1147 continue;
1148 isl_int_mul(mat->row[i][1 + j],
1149 mat->row[i][1 + j], mat->row[row][0]);
1150 isl_int_addmul(mat->row[i][1 + j],
1151 mat->row[i][off + col], mat->row[row][1 + j]);
1153 isl_int_mul(mat->row[i][off + col],
1154 mat->row[i][off + col], mat->row[row][off + col]);
1155 if (!isl_int_is_one(mat->row[i][0]))
1156 isl_seq_normalize(mat->ctx, mat->row[i], off + tab->n_col);
1158 t = tab->row_var[row];
1159 tab->row_var[row] = tab->col_var[col];
1160 tab->col_var[col] = t;
1161 var = isl_tab_var_from_row(tab, row);
1162 var->is_row = 1;
1163 var->index = row;
1164 var = var_from_col(tab, col);
1165 var->is_row = 0;
1166 var->index = col;
1167 update_row_sign(tab, row, col, sgn);
1168 if (tab->in_undo)
1169 return 0;
1170 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1171 if (isl_int_is_zero(mat->row[i][off + col]))
1172 continue;
1173 if (!isl_tab_var_from_row(tab, i)->frozen &&
1174 isl_tab_row_is_redundant(tab, i)) {
1175 int redo = isl_tab_mark_redundant(tab, i);
1176 if (redo < 0)
1177 return -1;
1178 if (redo)
1179 --i;
1182 return 0;
1185 /* If "var" represents a column variable, then pivot is up (sgn > 0)
1186 * or down (sgn < 0) to a row. The variable is assumed not to be
1187 * unbounded in the specified direction.
1188 * If sgn = 0, then the variable is unbounded in both directions,
1189 * and we pivot with any row we can find.
1191 static int to_row(struct isl_tab *tab, struct isl_tab_var *var, int sign) WARN_UNUSED;
1192 static int to_row(struct isl_tab *tab, struct isl_tab_var *var, int sign)
1194 int r;
1195 unsigned off = 2 + tab->M;
1197 if (var->is_row)
1198 return 0;
1200 if (sign == 0) {
1201 for (r = tab->n_redundant; r < tab->n_row; ++r)
1202 if (!isl_int_is_zero(tab->mat->row[r][off+var->index]))
1203 break;
1204 isl_assert(tab->mat->ctx, r < tab->n_row, return -1);
1205 } else {
1206 r = pivot_row(tab, NULL, sign, var->index);
1207 isl_assert(tab->mat->ctx, r >= 0, return -1);
1210 return isl_tab_pivot(tab, r, var->index);
1213 /* Check whether all variables that are marked as non-negative
1214 * also have a non-negative sample value. This function is not
1215 * called from the current code but is useful during debugging.
1217 static void check_table(struct isl_tab *tab) __attribute__ ((unused));
1218 static void check_table(struct isl_tab *tab)
1220 int i;
1222 if (tab->empty)
1223 return;
1224 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1225 struct isl_tab_var *var;
1226 var = isl_tab_var_from_row(tab, i);
1227 if (!var->is_nonneg)
1228 continue;
1229 if (tab->M) {
1230 isl_assert(tab->mat->ctx,
1231 !isl_int_is_neg(tab->mat->row[i][2]), abort());
1232 if (isl_int_is_pos(tab->mat->row[i][2]))
1233 continue;
1235 isl_assert(tab->mat->ctx, !isl_int_is_neg(tab->mat->row[i][1]),
1236 abort());
1240 /* Return the sign of the maximal value of "var".
1241 * If the sign is not negative, then on return from this function,
1242 * the sample value will also be non-negative.
1244 * If "var" is manifestly unbounded wrt positive values, we are done.
1245 * Otherwise, we pivot the variable up to a row if needed
1246 * Then we continue pivoting down until either
1247 * - no more down pivots can be performed
1248 * - the sample value is positive
1249 * - the variable is pivoted into a manifestly unbounded column
1251 static int sign_of_max(struct isl_tab *tab, struct isl_tab_var *var)
1253 int row, col;
1255 if (max_is_manifestly_unbounded(tab, var))
1256 return 1;
1257 if (to_row(tab, var, 1) < 0)
1258 return -2;
1259 while (!isl_int_is_pos(tab->mat->row[var->index][1])) {
1260 find_pivot(tab, var, var, 1, &row, &col);
1261 if (row == -1)
1262 return isl_int_sgn(tab->mat->row[var->index][1]);
1263 if (isl_tab_pivot(tab, row, col) < 0)
1264 return -2;
1265 if (!var->is_row) /* manifestly unbounded */
1266 return 1;
1268 return 1;
1271 int isl_tab_sign_of_max(struct isl_tab *tab, int con)
1273 struct isl_tab_var *var;
1275 if (!tab)
1276 return -2;
1278 var = &tab->con[con];
1279 isl_assert(tab->mat->ctx, !var->is_redundant, return -2);
1280 isl_assert(tab->mat->ctx, !var->is_zero, return -2);
1282 return sign_of_max(tab, var);
1285 static int row_is_neg(struct isl_tab *tab, int row)
1287 if (!tab->M)
1288 return isl_int_is_neg(tab->mat->row[row][1]);
1289 if (isl_int_is_pos(tab->mat->row[row][2]))
1290 return 0;
1291 if (isl_int_is_neg(tab->mat->row[row][2]))
1292 return 1;
1293 return isl_int_is_neg(tab->mat->row[row][1]);
1296 static int row_sgn(struct isl_tab *tab, int row)
1298 if (!tab->M)
1299 return isl_int_sgn(tab->mat->row[row][1]);
1300 if (!isl_int_is_zero(tab->mat->row[row][2]))
1301 return isl_int_sgn(tab->mat->row[row][2]);
1302 else
1303 return isl_int_sgn(tab->mat->row[row][1]);
1306 /* Perform pivots until the row variable "var" has a non-negative
1307 * sample value or until no more upward pivots can be performed.
1308 * Return the sign of the sample value after the pivots have been
1309 * performed.
1311 static int restore_row(struct isl_tab *tab, struct isl_tab_var *var)
1313 int row, col;
1315 while (row_is_neg(tab, var->index)) {
1316 find_pivot(tab, var, var, 1, &row, &col);
1317 if (row == -1)
1318 break;
1319 if (isl_tab_pivot(tab, row, col) < 0)
1320 return -2;
1321 if (!var->is_row) /* manifestly unbounded */
1322 return 1;
1324 return row_sgn(tab, var->index);
1327 /* Perform pivots until we are sure that the row variable "var"
1328 * can attain non-negative values. After return from this
1329 * function, "var" is still a row variable, but its sample
1330 * value may not be non-negative, even if the function returns 1.
1332 static int at_least_zero(struct isl_tab *tab, struct isl_tab_var *var)
1334 int row, col;
1336 while (isl_int_is_neg(tab->mat->row[var->index][1])) {
1337 find_pivot(tab, var, var, 1, &row, &col);
1338 if (row == -1)
1339 break;
1340 if (row == var->index) /* manifestly unbounded */
1341 return 1;
1342 if (isl_tab_pivot(tab, row, col) < 0)
1343 return -1;
1345 return !isl_int_is_neg(tab->mat->row[var->index][1]);
1348 /* Return a negative value if "var" can attain negative values.
1349 * Return a non-negative value otherwise.
1351 * If "var" is manifestly unbounded wrt negative values, we are done.
1352 * Otherwise, if var is in a column, we can pivot it down to a row.
1353 * Then we continue pivoting down until either
1354 * - the pivot would result in a manifestly unbounded column
1355 * => we don't perform the pivot, but simply return -1
1356 * - no more down pivots can be performed
1357 * - the sample value is negative
1358 * If the sample value becomes negative and the variable is supposed
1359 * to be nonnegative, then we undo the last pivot.
1360 * However, if the last pivot has made the pivoting variable
1361 * obviously redundant, then it may have moved to another row.
1362 * In that case we look for upward pivots until we reach a non-negative
1363 * value again.
1365 static int sign_of_min(struct isl_tab *tab, struct isl_tab_var *var)
1367 int row, col;
1368 struct isl_tab_var *pivot_var = NULL;
1370 if (min_is_manifestly_unbounded(tab, var))
1371 return -1;
1372 if (!var->is_row) {
1373 col = var->index;
1374 row = pivot_row(tab, NULL, -1, col);
1375 pivot_var = var_from_col(tab, col);
1376 if (isl_tab_pivot(tab, row, col) < 0)
1377 return -2;
1378 if (var->is_redundant)
1379 return 0;
1380 if (isl_int_is_neg(tab->mat->row[var->index][1])) {
1381 if (var->is_nonneg) {
1382 if (!pivot_var->is_redundant &&
1383 pivot_var->index == row) {
1384 if (isl_tab_pivot(tab, row, col) < 0)
1385 return -2;
1386 } else
1387 if (restore_row(tab, var) < -1)
1388 return -2;
1390 return -1;
1393 if (var->is_redundant)
1394 return 0;
1395 while (!isl_int_is_neg(tab->mat->row[var->index][1])) {
1396 find_pivot(tab, var, var, -1, &row, &col);
1397 if (row == var->index)
1398 return -1;
1399 if (row == -1)
1400 return isl_int_sgn(tab->mat->row[var->index][1]);
1401 pivot_var = var_from_col(tab, col);
1402 if (isl_tab_pivot(tab, row, col) < 0)
1403 return -2;
1404 if (var->is_redundant)
1405 return 0;
1407 if (pivot_var && var->is_nonneg) {
1408 /* pivot back to non-negative value */
1409 if (!pivot_var->is_redundant && pivot_var->index == row) {
1410 if (isl_tab_pivot(tab, row, col) < 0)
1411 return -2;
1412 } else
1413 if (restore_row(tab, var) < -1)
1414 return -2;
1416 return -1;
1419 static int row_at_most_neg_one(struct isl_tab *tab, int row)
1421 if (tab->M) {
1422 if (isl_int_is_pos(tab->mat->row[row][2]))
1423 return 0;
1424 if (isl_int_is_neg(tab->mat->row[row][2]))
1425 return 1;
1427 return isl_int_is_neg(tab->mat->row[row][1]) &&
1428 isl_int_abs_ge(tab->mat->row[row][1],
1429 tab->mat->row[row][0]);
1432 /* Return 1 if "var" can attain values <= -1.
1433 * Return 0 otherwise.
1435 * If the variable "var" is supposed to be non-negative (is_nonneg is set),
1436 * then the sample value of "var" is assumed to be non-negative when the
1437 * the function is called. If 1 is returned then the constraint
1438 * is not redundant and the sample value is made non-negative again before
1439 * the function returns.
1441 int isl_tab_min_at_most_neg_one(struct isl_tab *tab, struct isl_tab_var *var)
1443 int row, col;
1444 struct isl_tab_var *pivot_var;
1446 if (min_is_manifestly_unbounded(tab, var))
1447 return 1;
1448 if (!var->is_row) {
1449 col = var->index;
1450 row = pivot_row(tab, NULL, -1, col);
1451 pivot_var = var_from_col(tab, col);
1452 if (isl_tab_pivot(tab, row, col) < 0)
1453 return -1;
1454 if (var->is_redundant)
1455 return 0;
1456 if (row_at_most_neg_one(tab, var->index)) {
1457 if (var->is_nonneg) {
1458 if (!pivot_var->is_redundant &&
1459 pivot_var->index == row) {
1460 if (isl_tab_pivot(tab, row, col) < 0)
1461 return -1;
1462 } else
1463 if (restore_row(tab, var) < -1)
1464 return -1;
1466 return 1;
1469 if (var->is_redundant)
1470 return 0;
1471 do {
1472 find_pivot(tab, var, var, -1, &row, &col);
1473 if (row == var->index) {
1474 if (var->is_nonneg && restore_row(tab, var) < -1)
1475 return -1;
1476 return 1;
1478 if (row == -1)
1479 return 0;
1480 pivot_var = var_from_col(tab, col);
1481 if (isl_tab_pivot(tab, row, col) < 0)
1482 return -1;
1483 if (var->is_redundant)
1484 return 0;
1485 } while (!row_at_most_neg_one(tab, var->index));
1486 if (var->is_nonneg) {
1487 /* pivot back to non-negative value */
1488 if (!pivot_var->is_redundant && pivot_var->index == row)
1489 if (isl_tab_pivot(tab, row, col) < 0)
1490 return -1;
1491 if (restore_row(tab, var) < -1)
1492 return -1;
1494 return 1;
1497 /* Return 1 if "var" can attain values >= 1.
1498 * Return 0 otherwise.
1500 static int at_least_one(struct isl_tab *tab, struct isl_tab_var *var)
1502 int row, col;
1503 isl_int *r;
1505 if (max_is_manifestly_unbounded(tab, var))
1506 return 1;
1507 if (to_row(tab, var, 1) < 0)
1508 return -1;
1509 r = tab->mat->row[var->index];
1510 while (isl_int_lt(r[1], r[0])) {
1511 find_pivot(tab, var, var, 1, &row, &col);
1512 if (row == -1)
1513 return isl_int_ge(r[1], r[0]);
1514 if (row == var->index) /* manifestly unbounded */
1515 return 1;
1516 if (isl_tab_pivot(tab, row, col) < 0)
1517 return -1;
1519 return 1;
1522 static void swap_cols(struct isl_tab *tab, int col1, int col2)
1524 int t;
1525 unsigned off = 2 + tab->M;
1526 t = tab->col_var[col1];
1527 tab->col_var[col1] = tab->col_var[col2];
1528 tab->col_var[col2] = t;
1529 var_from_col(tab, col1)->index = col1;
1530 var_from_col(tab, col2)->index = col2;
1531 tab->mat = isl_mat_swap_cols(tab->mat, off + col1, off + col2);
1534 /* Mark column with index "col" as representing a zero variable.
1535 * If we may need to undo the operation the column is kept,
1536 * but no longer considered.
1537 * Otherwise, the column is simply removed.
1539 * The column may be interchanged with some other column. If it
1540 * is interchanged with a later column, return 1. Otherwise return 0.
1541 * If the columns are checked in order in the calling function,
1542 * then a return value of 1 means that the column with the given
1543 * column number may now contain a different column that
1544 * hasn't been checked yet.
1546 int isl_tab_kill_col(struct isl_tab *tab, int col)
1548 var_from_col(tab, col)->is_zero = 1;
1549 if (tab->need_undo) {
1550 if (isl_tab_push_var(tab, isl_tab_undo_zero,
1551 var_from_col(tab, col)) < 0)
1552 return -1;
1553 if (col != tab->n_dead)
1554 swap_cols(tab, col, tab->n_dead);
1555 tab->n_dead++;
1556 return 0;
1557 } else {
1558 if (col != tab->n_col - 1)
1559 swap_cols(tab, col, tab->n_col - 1);
1560 var_from_col(tab, tab->n_col - 1)->index = -1;
1561 tab->n_col--;
1562 return 1;
1566 static int row_is_manifestly_non_integral(struct isl_tab *tab, int row)
1568 unsigned off = 2 + tab->M;
1570 if (tab->M && !isl_int_eq(tab->mat->row[row][2],
1571 tab->mat->row[row][0]))
1572 return 0;
1573 if (isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
1574 tab->n_col - tab->n_dead) != -1)
1575 return 0;
1577 return !isl_int_is_divisible_by(tab->mat->row[row][1],
1578 tab->mat->row[row][0]);
1581 /* For integer tableaus, check if any of the coordinates are stuck
1582 * at a non-integral value.
1584 static int tab_is_manifestly_empty(struct isl_tab *tab)
1586 int i;
1588 if (tab->empty)
1589 return 1;
1590 if (tab->rational)
1591 return 0;
1593 for (i = 0; i < tab->n_var; ++i) {
1594 if (!tab->var[i].is_row)
1595 continue;
1596 if (row_is_manifestly_non_integral(tab, tab->var[i].index))
1597 return 1;
1600 return 0;
1603 /* Row variable "var" is non-negative and cannot attain any values
1604 * larger than zero. This means that the coefficients of the unrestricted
1605 * column variables are zero and that the coefficients of the non-negative
1606 * column variables are zero or negative.
1607 * Each of the non-negative variables with a negative coefficient can
1608 * then also be written as the negative sum of non-negative variables
1609 * and must therefore also be zero.
1611 * If "temp_var" is set, then "var" is a temporary variable that
1612 * will be removed after this function returns and for which
1613 * no information is recorded on the undo stack.
1614 * Do not add any undo records involving this variable in this case
1615 * since the variable will have been removed before any future undo
1616 * operations. Also avoid marking the variable as redundant,
1617 * since that either adds an undo record or needlessly removes the row
1618 * (the caller will take care of removing the row).
1620 static isl_stat close_row(struct isl_tab *tab, struct isl_tab_var *var,
1621 int temp_var) WARN_UNUSED;
1622 static isl_stat close_row(struct isl_tab *tab, struct isl_tab_var *var,
1623 int temp_var)
1625 int j;
1626 struct isl_mat *mat = tab->mat;
1627 unsigned off = 2 + tab->M;
1629 if (!var->is_nonneg)
1630 isl_die(isl_tab_get_ctx(tab), isl_error_internal,
1631 "expecting non-negative variable",
1632 return isl_stat_error);
1633 var->is_zero = 1;
1634 if (!temp_var && tab->need_undo)
1635 if (isl_tab_push_var(tab, isl_tab_undo_zero, var) < 0)
1636 return isl_stat_error;
1637 for (j = tab->n_dead; j < tab->n_col; ++j) {
1638 int recheck;
1639 if (isl_int_is_zero(mat->row[var->index][off + j]))
1640 continue;
1641 if (isl_int_is_pos(mat->row[var->index][off + j]))
1642 isl_die(isl_tab_get_ctx(tab), isl_error_internal,
1643 "row cannot have positive coefficients",
1644 return isl_stat_error);
1645 recheck = isl_tab_kill_col(tab, j);
1646 if (recheck < 0)
1647 return isl_stat_error;
1648 if (recheck)
1649 --j;
1651 if (!temp_var && isl_tab_mark_redundant(tab, var->index) < 0)
1652 return isl_stat_error;
1653 if (tab_is_manifestly_empty(tab) && isl_tab_mark_empty(tab) < 0)
1654 return isl_stat_error;
1655 return isl_stat_ok;
1658 /* Add a constraint to the tableau and allocate a row for it.
1659 * Return the index into the constraint array "con".
1661 * This function assumes that at least one more row and at least
1662 * one more element in the constraint array are available in the tableau.
1664 int isl_tab_allocate_con(struct isl_tab *tab)
1666 int r;
1668 isl_assert(tab->mat->ctx, tab->n_row < tab->mat->n_row, return -1);
1669 isl_assert(tab->mat->ctx, tab->n_con < tab->max_con, return -1);
1671 r = tab->n_con;
1672 tab->con[r].index = tab->n_row;
1673 tab->con[r].is_row = 1;
1674 tab->con[r].is_nonneg = 0;
1675 tab->con[r].is_zero = 0;
1676 tab->con[r].is_redundant = 0;
1677 tab->con[r].frozen = 0;
1678 tab->con[r].negated = 0;
1679 tab->row_var[tab->n_row] = ~r;
1681 tab->n_row++;
1682 tab->n_con++;
1683 if (isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->con[r]) < 0)
1684 return -1;
1686 return r;
1689 /* Move the entries in tab->var up one position, starting at "first",
1690 * creating room for an extra entry at position "first".
1691 * Since some of the entries of tab->row_var and tab->col_var contain
1692 * indices into this array, they have to be updated accordingly.
1694 static int var_insert_entry(struct isl_tab *tab, int first)
1696 int i;
1698 if (tab->n_var >= tab->max_var)
1699 isl_die(isl_tab_get_ctx(tab), isl_error_internal,
1700 "not enough room for new variable", return -1);
1701 if (first > tab->n_var)
1702 isl_die(isl_tab_get_ctx(tab), isl_error_internal,
1703 "invalid initial position", return -1);
1705 for (i = tab->n_var - 1; i >= first; --i) {
1706 tab->var[i + 1] = tab->var[i];
1707 if (tab->var[i + 1].is_row)
1708 tab->row_var[tab->var[i + 1].index]++;
1709 else
1710 tab->col_var[tab->var[i + 1].index]++;
1713 tab->n_var++;
1715 return 0;
1718 /* Drop the entry at position "first" in tab->var, moving all
1719 * subsequent entries down.
1720 * Since some of the entries of tab->row_var and tab->col_var contain
1721 * indices into this array, they have to be updated accordingly.
1723 static int var_drop_entry(struct isl_tab *tab, int first)
1725 int i;
1727 if (first >= tab->n_var)
1728 isl_die(isl_tab_get_ctx(tab), isl_error_internal,
1729 "invalid initial position", return -1);
1731 tab->n_var--;
1733 for (i = first; i < tab->n_var; ++i) {
1734 tab->var[i] = tab->var[i + 1];
1735 if (tab->var[i + 1].is_row)
1736 tab->row_var[tab->var[i].index]--;
1737 else
1738 tab->col_var[tab->var[i].index]--;
1741 return 0;
1744 /* Add a variable to the tableau at position "r" and allocate a column for it.
1745 * Return the index into the variable array "var", i.e., "r",
1746 * or -1 on error.
1748 int isl_tab_insert_var(struct isl_tab *tab, int r)
1750 int i;
1751 unsigned off = 2 + tab->M;
1753 isl_assert(tab->mat->ctx, tab->n_col < tab->mat->n_col, return -1);
1755 if (var_insert_entry(tab, r) < 0)
1756 return -1;
1758 tab->var[r].index = tab->n_col;
1759 tab->var[r].is_row = 0;
1760 tab->var[r].is_nonneg = 0;
1761 tab->var[r].is_zero = 0;
1762 tab->var[r].is_redundant = 0;
1763 tab->var[r].frozen = 0;
1764 tab->var[r].negated = 0;
1765 tab->col_var[tab->n_col] = r;
1767 for (i = 0; i < tab->n_row; ++i)
1768 isl_int_set_si(tab->mat->row[i][off + tab->n_col], 0);
1770 tab->n_col++;
1771 if (isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->var[r]) < 0)
1772 return -1;
1774 return r;
1777 /* Add a variable to the tableau and allocate a column for it.
1778 * Return the index into the variable array "var".
1780 int isl_tab_allocate_var(struct isl_tab *tab)
1782 if (!tab)
1783 return -1;
1785 return isl_tab_insert_var(tab, tab->n_var);
1788 /* Add a row to the tableau. The row is given as an affine combination
1789 * of the original variables and needs to be expressed in terms of the
1790 * column variables.
1792 * This function assumes that at least one more row and at least
1793 * one more element in the constraint array are available in the tableau.
1795 * We add each term in turn.
1796 * If r = n/d_r is the current sum and we need to add k x, then
1797 * if x is a column variable, we increase the numerator of
1798 * this column by k d_r
1799 * if x = f/d_x is a row variable, then the new representation of r is
1801 * n k f d_x/g n + d_r/g k f m/d_r n + m/d_g k f
1802 * --- + --- = ------------------- = -------------------
1803 * d_r d_r d_r d_x/g m
1805 * with g the gcd of d_r and d_x and m the lcm of d_r and d_x.
1807 * If tab->M is set, then, internally, each variable x is represented
1808 * as x' - M. We then also need no subtract k d_r from the coefficient of M.
1810 int isl_tab_add_row(struct isl_tab *tab, isl_int *line)
1812 int i;
1813 int r;
1814 isl_int *row;
1815 isl_int a, b;
1816 unsigned off = 2 + tab->M;
1818 r = isl_tab_allocate_con(tab);
1819 if (r < 0)
1820 return -1;
1822 isl_int_init(a);
1823 isl_int_init(b);
1824 row = tab->mat->row[tab->con[r].index];
1825 isl_int_set_si(row[0], 1);
1826 isl_int_set(row[1], line[0]);
1827 isl_seq_clr(row + 2, tab->M + tab->n_col);
1828 for (i = 0; i < tab->n_var; ++i) {
1829 if (tab->var[i].is_zero)
1830 continue;
1831 if (tab->var[i].is_row) {
1832 isl_int_lcm(a,
1833 row[0], tab->mat->row[tab->var[i].index][0]);
1834 isl_int_swap(a, row[0]);
1835 isl_int_divexact(a, row[0], a);
1836 isl_int_divexact(b,
1837 row[0], tab->mat->row[tab->var[i].index][0]);
1838 isl_int_mul(b, b, line[1 + i]);
1839 isl_seq_combine(row + 1, a, row + 1,
1840 b, tab->mat->row[tab->var[i].index] + 1,
1841 1 + tab->M + tab->n_col);
1842 } else
1843 isl_int_addmul(row[off + tab->var[i].index],
1844 line[1 + i], row[0]);
1845 if (tab->M && i >= tab->n_param && i < tab->n_var - tab->n_div)
1846 isl_int_submul(row[2], line[1 + i], row[0]);
1848 isl_seq_normalize(tab->mat->ctx, row, off + tab->n_col);
1849 isl_int_clear(a);
1850 isl_int_clear(b);
1852 if (tab->row_sign)
1853 tab->row_sign[tab->con[r].index] = isl_tab_row_unknown;
1855 return r;
1858 static int drop_row(struct isl_tab *tab, int row)
1860 isl_assert(tab->mat->ctx, ~tab->row_var[row] == tab->n_con - 1, return -1);
1861 if (row != tab->n_row - 1)
1862 swap_rows(tab, row, tab->n_row - 1);
1863 tab->n_row--;
1864 tab->n_con--;
1865 return 0;
1868 /* Drop the variable in column "col" along with the column.
1869 * The column is removed first because it may need to be moved
1870 * into the last position and this process requires
1871 * the contents of the col_var array in a state
1872 * before the removal of the variable.
1874 static int drop_col(struct isl_tab *tab, int col)
1876 int var;
1878 var = tab->col_var[col];
1879 if (col != tab->n_col - 1)
1880 swap_cols(tab, col, tab->n_col - 1);
1881 tab->n_col--;
1882 if (var_drop_entry(tab, var) < 0)
1883 return -1;
1884 return 0;
1887 /* Add inequality "ineq" and check if it conflicts with the
1888 * previously added constraints or if it is obviously redundant.
1890 * This function assumes that at least one more row and at least
1891 * one more element in the constraint array are available in the tableau.
1893 isl_stat isl_tab_add_ineq(struct isl_tab *tab, isl_int *ineq)
1895 int r;
1896 int sgn;
1897 isl_int cst;
1899 if (!tab)
1900 return isl_stat_error;
1901 if (tab->bmap) {
1902 struct isl_basic_map *bmap = tab->bmap;
1904 isl_assert(tab->mat->ctx, tab->n_eq == bmap->n_eq,
1905 return isl_stat_error);
1906 isl_assert(tab->mat->ctx,
1907 tab->n_con == bmap->n_eq + bmap->n_ineq,
1908 return isl_stat_error);
1909 tab->bmap = isl_basic_map_add_ineq(tab->bmap, ineq);
1910 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1911 return isl_stat_error;
1912 if (!tab->bmap)
1913 return isl_stat_error;
1915 if (tab->cone) {
1916 isl_int_init(cst);
1917 isl_int_set_si(cst, 0);
1918 isl_int_swap(ineq[0], cst);
1920 r = isl_tab_add_row(tab, ineq);
1921 if (tab->cone) {
1922 isl_int_swap(ineq[0], cst);
1923 isl_int_clear(cst);
1925 if (r < 0)
1926 return isl_stat_error;
1927 tab->con[r].is_nonneg = 1;
1928 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1929 return isl_stat_error;
1930 if (isl_tab_row_is_redundant(tab, tab->con[r].index)) {
1931 if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1932 return isl_stat_error;
1933 return isl_stat_ok;
1936 sgn = restore_row(tab, &tab->con[r]);
1937 if (sgn < -1)
1938 return isl_stat_error;
1939 if (sgn < 0)
1940 return isl_tab_mark_empty(tab);
1941 if (tab->con[r].is_row && isl_tab_row_is_redundant(tab, tab->con[r].index))
1942 if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1943 return isl_stat_error;
1944 return isl_stat_ok;
1947 /* Pivot a non-negative variable down until it reaches the value zero
1948 * and then pivot the variable into a column position.
1950 static int to_col(struct isl_tab *tab, struct isl_tab_var *var) WARN_UNUSED;
1951 static int to_col(struct isl_tab *tab, struct isl_tab_var *var)
1953 int i;
1954 int row, col;
1955 unsigned off = 2 + tab->M;
1957 if (!var->is_row)
1958 return 0;
1960 while (isl_int_is_pos(tab->mat->row[var->index][1])) {
1961 find_pivot(tab, var, NULL, -1, &row, &col);
1962 isl_assert(tab->mat->ctx, row != -1, return -1);
1963 if (isl_tab_pivot(tab, row, col) < 0)
1964 return -1;
1965 if (!var->is_row)
1966 return 0;
1969 for (i = tab->n_dead; i < tab->n_col; ++i)
1970 if (!isl_int_is_zero(tab->mat->row[var->index][off + i]))
1971 break;
1973 isl_assert(tab->mat->ctx, i < tab->n_col, return -1);
1974 if (isl_tab_pivot(tab, var->index, i) < 0)
1975 return -1;
1977 return 0;
1980 /* We assume Gaussian elimination has been performed on the equalities.
1981 * The equalities can therefore never conflict.
1982 * Adding the equalities is currently only really useful for a later call
1983 * to isl_tab_ineq_type.
1985 * This function assumes that at least one more row and at least
1986 * one more element in the constraint array are available in the tableau.
1988 static struct isl_tab *add_eq(struct isl_tab *tab, isl_int *eq)
1990 int i;
1991 int r;
1993 if (!tab)
1994 return NULL;
1995 r = isl_tab_add_row(tab, eq);
1996 if (r < 0)
1997 goto error;
1999 r = tab->con[r].index;
2000 i = isl_seq_first_non_zero(tab->mat->row[r] + 2 + tab->M + tab->n_dead,
2001 tab->n_col - tab->n_dead);
2002 isl_assert(tab->mat->ctx, i >= 0, goto error);
2003 i += tab->n_dead;
2004 if (isl_tab_pivot(tab, r, i) < 0)
2005 goto error;
2006 if (isl_tab_kill_col(tab, i) < 0)
2007 goto error;
2008 tab->n_eq++;
2010 return tab;
2011 error:
2012 isl_tab_free(tab);
2013 return NULL;
2016 /* Does the sample value of row "row" of "tab" involve the big parameter,
2017 * if any?
2019 static int row_is_big(struct isl_tab *tab, int row)
2021 return tab->M && !isl_int_is_zero(tab->mat->row[row][2]);
2024 static int row_is_manifestly_zero(struct isl_tab *tab, int row)
2026 unsigned off = 2 + tab->M;
2028 if (!isl_int_is_zero(tab->mat->row[row][1]))
2029 return 0;
2030 if (row_is_big(tab, row))
2031 return 0;
2032 return isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
2033 tab->n_col - tab->n_dead) == -1;
2036 /* Add an equality that is known to be valid for the given tableau.
2038 * This function assumes that at least one more row and at least
2039 * one more element in the constraint array are available in the tableau.
2041 int isl_tab_add_valid_eq(struct isl_tab *tab, isl_int *eq)
2043 struct isl_tab_var *var;
2044 int r;
2046 if (!tab)
2047 return -1;
2048 r = isl_tab_add_row(tab, eq);
2049 if (r < 0)
2050 return -1;
2052 var = &tab->con[r];
2053 r = var->index;
2054 if (row_is_manifestly_zero(tab, r)) {
2055 var->is_zero = 1;
2056 if (isl_tab_mark_redundant(tab, r) < 0)
2057 return -1;
2058 return 0;
2061 if (isl_int_is_neg(tab->mat->row[r][1])) {
2062 isl_seq_neg(tab->mat->row[r] + 1, tab->mat->row[r] + 1,
2063 1 + tab->n_col);
2064 var->negated = 1;
2066 var->is_nonneg = 1;
2067 if (to_col(tab, var) < 0)
2068 return -1;
2069 var->is_nonneg = 0;
2070 if (isl_tab_kill_col(tab, var->index) < 0)
2071 return -1;
2073 return 0;
2076 /* Add a zero row to "tab" and return the corresponding index
2077 * in the constraint array.
2079 * This function assumes that at least one more row and at least
2080 * one more element in the constraint array are available in the tableau.
2082 static int add_zero_row(struct isl_tab *tab)
2084 int r;
2085 isl_int *row;
2087 r = isl_tab_allocate_con(tab);
2088 if (r < 0)
2089 return -1;
2091 row = tab->mat->row[tab->con[r].index];
2092 isl_seq_clr(row + 1, 1 + tab->M + tab->n_col);
2093 isl_int_set_si(row[0], 1);
2095 return r;
2098 /* Add equality "eq" and check if it conflicts with the
2099 * previously added constraints or if it is obviously redundant.
2101 * This function assumes that at least one more row and at least
2102 * one more element in the constraint array are available in the tableau.
2103 * If tab->bmap is set, then two rows are needed instead of one.
2105 int isl_tab_add_eq(struct isl_tab *tab, isl_int *eq)
2107 struct isl_tab_undo *snap = NULL;
2108 struct isl_tab_var *var;
2109 int r;
2110 int row;
2111 int sgn;
2112 isl_int cst;
2114 if (!tab)
2115 return -1;
2116 isl_assert(tab->mat->ctx, !tab->M, return -1);
2118 if (tab->need_undo)
2119 snap = isl_tab_snap(tab);
2121 if (tab->cone) {
2122 isl_int_init(cst);
2123 isl_int_set_si(cst, 0);
2124 isl_int_swap(eq[0], cst);
2126 r = isl_tab_add_row(tab, eq);
2127 if (tab->cone) {
2128 isl_int_swap(eq[0], cst);
2129 isl_int_clear(cst);
2131 if (r < 0)
2132 return -1;
2134 var = &tab->con[r];
2135 row = var->index;
2136 if (row_is_manifestly_zero(tab, row)) {
2137 if (snap)
2138 return isl_tab_rollback(tab, snap);
2139 return drop_row(tab, row);
2142 if (tab->bmap) {
2143 tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
2144 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
2145 return -1;
2146 isl_seq_neg(eq, eq, 1 + tab->n_var);
2147 tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
2148 isl_seq_neg(eq, eq, 1 + tab->n_var);
2149 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
2150 return -1;
2151 if (!tab->bmap)
2152 return -1;
2153 if (add_zero_row(tab) < 0)
2154 return -1;
2157 sgn = isl_int_sgn(tab->mat->row[row][1]);
2159 if (sgn > 0) {
2160 isl_seq_neg(tab->mat->row[row] + 1, tab->mat->row[row] + 1,
2161 1 + tab->n_col);
2162 var->negated = 1;
2163 sgn = -1;
2166 if (sgn < 0) {
2167 sgn = sign_of_max(tab, var);
2168 if (sgn < -1)
2169 return -1;
2170 if (sgn < 0) {
2171 if (isl_tab_mark_empty(tab) < 0)
2172 return -1;
2173 return 0;
2177 var->is_nonneg = 1;
2178 if (to_col(tab, var) < 0)
2179 return -1;
2180 var->is_nonneg = 0;
2181 if (isl_tab_kill_col(tab, var->index) < 0)
2182 return -1;
2184 return 0;
2187 /* Construct and return an inequality that expresses an upper bound
2188 * on the given div.
2189 * In particular, if the div is given by
2191 * d = floor(e/m)
2193 * then the inequality expresses
2195 * m d <= e
2197 static struct isl_vec *ineq_for_div(struct isl_basic_map *bmap, unsigned div)
2199 unsigned total;
2200 unsigned div_pos;
2201 struct isl_vec *ineq;
2203 if (!bmap)
2204 return NULL;
2206 total = isl_basic_map_total_dim(bmap);
2207 div_pos = 1 + total - bmap->n_div + div;
2209 ineq = isl_vec_alloc(bmap->ctx, 1 + total);
2210 if (!ineq)
2211 return NULL;
2213 isl_seq_cpy(ineq->el, bmap->div[div] + 1, 1 + total);
2214 isl_int_neg(ineq->el[div_pos], bmap->div[div][0]);
2215 return ineq;
2218 /* For a div d = floor(f/m), add the constraints
2220 * f - m d >= 0
2221 * -(f-(m-1)) + m d >= 0
2223 * Note that the second constraint is the negation of
2225 * f - m d >= m
2227 * If add_ineq is not NULL, then this function is used
2228 * instead of isl_tab_add_ineq to effectively add the inequalities.
2230 * This function assumes that at least two more rows and at least
2231 * two more elements in the constraint array are available in the tableau.
2233 static int add_div_constraints(struct isl_tab *tab, unsigned div,
2234 int (*add_ineq)(void *user, isl_int *), void *user)
2236 unsigned total;
2237 unsigned div_pos;
2238 struct isl_vec *ineq;
2240 total = isl_basic_map_total_dim(tab->bmap);
2241 div_pos = 1 + total - tab->bmap->n_div + div;
2243 ineq = ineq_for_div(tab->bmap, div);
2244 if (!ineq)
2245 goto error;
2247 if (add_ineq) {
2248 if (add_ineq(user, ineq->el) < 0)
2249 goto error;
2250 } else {
2251 if (isl_tab_add_ineq(tab, ineq->el) < 0)
2252 goto error;
2255 isl_seq_neg(ineq->el, tab->bmap->div[div] + 1, 1 + total);
2256 isl_int_set(ineq->el[div_pos], tab->bmap->div[div][0]);
2257 isl_int_add(ineq->el[0], ineq->el[0], ineq->el[div_pos]);
2258 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
2260 if (add_ineq) {
2261 if (add_ineq(user, ineq->el) < 0)
2262 goto error;
2263 } else {
2264 if (isl_tab_add_ineq(tab, ineq->el) < 0)
2265 goto error;
2268 isl_vec_free(ineq);
2270 return 0;
2271 error:
2272 isl_vec_free(ineq);
2273 return -1;
2276 /* Check whether the div described by "div" is obviously non-negative.
2277 * If we are using a big parameter, then we will encode the div
2278 * as div' = M + div, which is always non-negative.
2279 * Otherwise, we check whether div is a non-negative affine combination
2280 * of non-negative variables.
2282 static int div_is_nonneg(struct isl_tab *tab, __isl_keep isl_vec *div)
2284 int i;
2286 if (tab->M)
2287 return 1;
2289 if (isl_int_is_neg(div->el[1]))
2290 return 0;
2292 for (i = 0; i < tab->n_var; ++i) {
2293 if (isl_int_is_neg(div->el[2 + i]))
2294 return 0;
2295 if (isl_int_is_zero(div->el[2 + i]))
2296 continue;
2297 if (!tab->var[i].is_nonneg)
2298 return 0;
2301 return 1;
2304 /* Insert an extra div, prescribed by "div", to the tableau and
2305 * the associated bmap (which is assumed to be non-NULL).
2306 * The extra integer division is inserted at (tableau) position "pos".
2307 * Return "pos" or -1 if an error occurred.
2309 * If add_ineq is not NULL, then this function is used instead
2310 * of isl_tab_add_ineq to add the div constraints.
2311 * This complication is needed because the code in isl_tab_pip
2312 * wants to perform some extra processing when an inequality
2313 * is added to the tableau.
2315 int isl_tab_insert_div(struct isl_tab *tab, int pos, __isl_keep isl_vec *div,
2316 int (*add_ineq)(void *user, isl_int *), void *user)
2318 int r;
2319 int nonneg;
2320 int n_div, o_div;
2322 if (!tab || !div)
2323 return -1;
2325 if (div->size != 1 + 1 + tab->n_var)
2326 isl_die(isl_tab_get_ctx(tab), isl_error_invalid,
2327 "unexpected size", return -1);
2329 isl_assert(tab->mat->ctx, tab->bmap, return -1);
2330 n_div = isl_basic_map_dim(tab->bmap, isl_dim_div);
2331 o_div = tab->n_var - n_div;
2332 if (pos < o_div || pos > tab->n_var)
2333 isl_die(isl_tab_get_ctx(tab), isl_error_invalid,
2334 "invalid position", return -1);
2336 nonneg = div_is_nonneg(tab, div);
2338 if (isl_tab_extend_cons(tab, 3) < 0)
2339 return -1;
2340 if (isl_tab_extend_vars(tab, 1) < 0)
2341 return -1;
2342 r = isl_tab_insert_var(tab, pos);
2343 if (r < 0)
2344 return -1;
2346 if (nonneg)
2347 tab->var[r].is_nonneg = 1;
2349 tab->bmap = isl_basic_map_insert_div(tab->bmap, pos - o_div, div);
2350 if (!tab->bmap)
2351 return -1;
2352 if (isl_tab_push_var(tab, isl_tab_undo_bmap_div, &tab->var[r]) < 0)
2353 return -1;
2355 if (add_div_constraints(tab, pos - o_div, add_ineq, user) < 0)
2356 return -1;
2358 return r;
2361 /* Add an extra div, prescribed by "div", to the tableau and
2362 * the associated bmap (which is assumed to be non-NULL).
2364 int isl_tab_add_div(struct isl_tab *tab, __isl_keep isl_vec *div)
2366 if (!tab)
2367 return -1;
2368 return isl_tab_insert_div(tab, tab->n_var, div, NULL, NULL);
2371 /* If "track" is set, then we want to keep track of all constraints in tab
2372 * in its bmap field. This field is initialized from a copy of "bmap",
2373 * so we need to make sure that all constraints in "bmap" also appear
2374 * in the constructed tab.
2376 __isl_give struct isl_tab *isl_tab_from_basic_map(
2377 __isl_keep isl_basic_map *bmap, int track)
2379 int i;
2380 struct isl_tab *tab;
2382 if (!bmap)
2383 return NULL;
2384 tab = isl_tab_alloc(bmap->ctx,
2385 isl_basic_map_total_dim(bmap) + bmap->n_ineq + 1,
2386 isl_basic_map_total_dim(bmap), 0);
2387 if (!tab)
2388 return NULL;
2389 tab->preserve = track;
2390 tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
2391 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)) {
2392 if (isl_tab_mark_empty(tab) < 0)
2393 goto error;
2394 goto done;
2396 for (i = 0; i < bmap->n_eq; ++i) {
2397 tab = add_eq(tab, bmap->eq[i]);
2398 if (!tab)
2399 return tab;
2401 for (i = 0; i < bmap->n_ineq; ++i) {
2402 if (isl_tab_add_ineq(tab, bmap->ineq[i]) < 0)
2403 goto error;
2404 if (tab->empty)
2405 goto done;
2407 done:
2408 if (track && isl_tab_track_bmap(tab, isl_basic_map_copy(bmap)) < 0)
2409 goto error;
2410 return tab;
2411 error:
2412 isl_tab_free(tab);
2413 return NULL;
2416 __isl_give struct isl_tab *isl_tab_from_basic_set(
2417 __isl_keep isl_basic_set *bset, int track)
2419 return isl_tab_from_basic_map(bset, track);
2422 /* Construct a tableau corresponding to the recession cone of "bset".
2424 struct isl_tab *isl_tab_from_recession_cone(__isl_keep isl_basic_set *bset,
2425 int parametric)
2427 isl_int cst;
2428 int i;
2429 struct isl_tab *tab;
2430 unsigned offset = 0;
2432 if (!bset)
2433 return NULL;
2434 if (parametric)
2435 offset = isl_basic_set_dim(bset, isl_dim_param);
2436 tab = isl_tab_alloc(bset->ctx, bset->n_eq + bset->n_ineq,
2437 isl_basic_set_total_dim(bset) - offset, 0);
2438 if (!tab)
2439 return NULL;
2440 tab->rational = ISL_F_ISSET(bset, ISL_BASIC_SET_RATIONAL);
2441 tab->cone = 1;
2443 isl_int_init(cst);
2444 isl_int_set_si(cst, 0);
2445 for (i = 0; i < bset->n_eq; ++i) {
2446 isl_int_swap(bset->eq[i][offset], cst);
2447 if (offset > 0) {
2448 if (isl_tab_add_eq(tab, bset->eq[i] + offset) < 0)
2449 goto error;
2450 } else
2451 tab = add_eq(tab, bset->eq[i]);
2452 isl_int_swap(bset->eq[i][offset], cst);
2453 if (!tab)
2454 goto done;
2456 for (i = 0; i < bset->n_ineq; ++i) {
2457 int r;
2458 isl_int_swap(bset->ineq[i][offset], cst);
2459 r = isl_tab_add_row(tab, bset->ineq[i] + offset);
2460 isl_int_swap(bset->ineq[i][offset], cst);
2461 if (r < 0)
2462 goto error;
2463 tab->con[r].is_nonneg = 1;
2464 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
2465 goto error;
2467 done:
2468 isl_int_clear(cst);
2469 return tab;
2470 error:
2471 isl_int_clear(cst);
2472 isl_tab_free(tab);
2473 return NULL;
2476 /* Assuming "tab" is the tableau of a cone, check if the cone is
2477 * bounded, i.e., if it is empty or only contains the origin.
2479 isl_bool isl_tab_cone_is_bounded(struct isl_tab *tab)
2481 int i;
2483 if (!tab)
2484 return isl_bool_error;
2485 if (tab->empty)
2486 return isl_bool_true;
2487 if (tab->n_dead == tab->n_col)
2488 return isl_bool_true;
2490 for (;;) {
2491 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2492 struct isl_tab_var *var;
2493 int sgn;
2494 var = isl_tab_var_from_row(tab, i);
2495 if (!var->is_nonneg)
2496 continue;
2497 sgn = sign_of_max(tab, var);
2498 if (sgn < -1)
2499 return isl_bool_error;
2500 if (sgn != 0)
2501 return isl_bool_false;
2502 if (close_row(tab, var, 0) < 0)
2503 return isl_bool_error;
2504 break;
2506 if (tab->n_dead == tab->n_col)
2507 return isl_bool_true;
2508 if (i == tab->n_row)
2509 return isl_bool_false;
2513 int isl_tab_sample_is_integer(struct isl_tab *tab)
2515 int i;
2517 if (!tab)
2518 return -1;
2520 for (i = 0; i < tab->n_var; ++i) {
2521 int row;
2522 if (!tab->var[i].is_row)
2523 continue;
2524 row = tab->var[i].index;
2525 if (!isl_int_is_divisible_by(tab->mat->row[row][1],
2526 tab->mat->row[row][0]))
2527 return 0;
2529 return 1;
2532 static struct isl_vec *extract_integer_sample(struct isl_tab *tab)
2534 int i;
2535 struct isl_vec *vec;
2537 vec = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
2538 if (!vec)
2539 return NULL;
2541 isl_int_set_si(vec->block.data[0], 1);
2542 for (i = 0; i < tab->n_var; ++i) {
2543 if (!tab->var[i].is_row)
2544 isl_int_set_si(vec->block.data[1 + i], 0);
2545 else {
2546 int row = tab->var[i].index;
2547 isl_int_divexact(vec->block.data[1 + i],
2548 tab->mat->row[row][1], tab->mat->row[row][0]);
2552 return vec;
2555 struct isl_vec *isl_tab_get_sample_value(struct isl_tab *tab)
2557 int i;
2558 struct isl_vec *vec;
2559 isl_int m;
2561 if (!tab)
2562 return NULL;
2564 vec = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
2565 if (!vec)
2566 return NULL;
2568 isl_int_init(m);
2570 isl_int_set_si(vec->block.data[0], 1);
2571 for (i = 0; i < tab->n_var; ++i) {
2572 int row;
2573 if (!tab->var[i].is_row) {
2574 isl_int_set_si(vec->block.data[1 + i], 0);
2575 continue;
2577 row = tab->var[i].index;
2578 isl_int_gcd(m, vec->block.data[0], tab->mat->row[row][0]);
2579 isl_int_divexact(m, tab->mat->row[row][0], m);
2580 isl_seq_scale(vec->block.data, vec->block.data, m, 1 + i);
2581 isl_int_divexact(m, vec->block.data[0], tab->mat->row[row][0]);
2582 isl_int_mul(vec->block.data[1 + i], m, tab->mat->row[row][1]);
2584 vec = isl_vec_normalize(vec);
2586 isl_int_clear(m);
2587 return vec;
2590 /* Store the sample value of "var" of "tab" rounded up (if sgn > 0)
2591 * or down (if sgn < 0) to the nearest integer in *v.
2593 static void get_rounded_sample_value(struct isl_tab *tab,
2594 struct isl_tab_var *var, int sgn, isl_int *v)
2596 if (!var->is_row)
2597 isl_int_set_si(*v, 0);
2598 else if (sgn > 0)
2599 isl_int_cdiv_q(*v, tab->mat->row[var->index][1],
2600 tab->mat->row[var->index][0]);
2601 else
2602 isl_int_fdiv_q(*v, tab->mat->row[var->index][1],
2603 tab->mat->row[var->index][0]);
2606 /* Update "bmap" based on the results of the tableau "tab".
2607 * In particular, implicit equalities are made explicit, redundant constraints
2608 * are removed and if the sample value happens to be integer, it is stored
2609 * in "bmap" (unless "bmap" already had an integer sample).
2611 * The tableau is assumed to have been created from "bmap" using
2612 * isl_tab_from_basic_map.
2614 struct isl_basic_map *isl_basic_map_update_from_tab(struct isl_basic_map *bmap,
2615 struct isl_tab *tab)
2617 int i;
2618 unsigned n_eq;
2620 if (!bmap)
2621 return NULL;
2622 if (!tab)
2623 return bmap;
2625 n_eq = tab->n_eq;
2626 if (tab->empty)
2627 bmap = isl_basic_map_set_to_empty(bmap);
2628 else
2629 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2630 if (isl_tab_is_equality(tab, n_eq + i))
2631 isl_basic_map_inequality_to_equality(bmap, i);
2632 else if (isl_tab_is_redundant(tab, n_eq + i))
2633 isl_basic_map_drop_inequality(bmap, i);
2635 if (bmap->n_eq != n_eq)
2636 bmap = isl_basic_map_gauss(bmap, NULL);
2637 if (!tab->rational &&
2638 bmap && !bmap->sample && isl_tab_sample_is_integer(tab))
2639 bmap->sample = extract_integer_sample(tab);
2640 return bmap;
2643 struct isl_basic_set *isl_basic_set_update_from_tab(struct isl_basic_set *bset,
2644 struct isl_tab *tab)
2646 return bset_from_bmap(isl_basic_map_update_from_tab(bset_to_bmap(bset),
2647 tab));
2650 /* Drop the last constraint added to "tab" in position "r".
2651 * The constraint is expected to have remained in a row.
2653 static isl_stat drop_last_con_in_row(struct isl_tab *tab, int r)
2655 if (!tab->con[r].is_row)
2656 isl_die(isl_tab_get_ctx(tab), isl_error_internal,
2657 "row unexpectedly moved to column",
2658 return isl_stat_error);
2659 if (r + 1 != tab->n_con)
2660 isl_die(isl_tab_get_ctx(tab), isl_error_internal,
2661 "additional constraints added", return isl_stat_error);
2662 if (drop_row(tab, tab->con[r].index) < 0)
2663 return isl_stat_error;
2665 return isl_stat_ok;
2668 /* Given a non-negative variable "var", temporarily add a new non-negative
2669 * variable that is the opposite of "var", ensuring that "var" can only attain
2670 * the value zero. The new variable is removed again before this function
2671 * returns. However, the effect of forcing "var" to be zero remains.
2672 * If var = n/d is a row variable, then the new variable = -n/d.
2673 * If var is a column variables, then the new variable = -var.
2674 * If the new variable cannot attain non-negative values, then
2675 * the resulting tableau is empty.
2676 * Otherwise, we know the value will be zero and we close the row.
2678 static isl_stat cut_to_hyperplane(struct isl_tab *tab, struct isl_tab_var *var)
2680 unsigned r;
2681 isl_int *row;
2682 int sgn;
2683 unsigned off = 2 + tab->M;
2685 if (var->is_zero)
2686 return isl_stat_ok;
2687 if (var->is_redundant || !var->is_nonneg)
2688 isl_die(isl_tab_get_ctx(tab), isl_error_invalid,
2689 "expecting non-redundant non-negative variable",
2690 return isl_stat_error);
2692 if (isl_tab_extend_cons(tab, 1) < 0)
2693 return isl_stat_error;
2695 r = tab->n_con;
2696 tab->con[r].index = tab->n_row;
2697 tab->con[r].is_row = 1;
2698 tab->con[r].is_nonneg = 0;
2699 tab->con[r].is_zero = 0;
2700 tab->con[r].is_redundant = 0;
2701 tab->con[r].frozen = 0;
2702 tab->con[r].negated = 0;
2703 tab->row_var[tab->n_row] = ~r;
2704 row = tab->mat->row[tab->n_row];
2706 if (var->is_row) {
2707 isl_int_set(row[0], tab->mat->row[var->index][0]);
2708 isl_seq_neg(row + 1,
2709 tab->mat->row[var->index] + 1, 1 + tab->n_col);
2710 } else {
2711 isl_int_set_si(row[0], 1);
2712 isl_seq_clr(row + 1, 1 + tab->n_col);
2713 isl_int_set_si(row[off + var->index], -1);
2716 tab->n_row++;
2717 tab->n_con++;
2719 sgn = sign_of_max(tab, &tab->con[r]);
2720 if (sgn < -1)
2721 return isl_stat_error;
2722 if (sgn < 0) {
2723 if (drop_last_con_in_row(tab, r) < 0)
2724 return isl_stat_error;
2725 if (isl_tab_mark_empty(tab) < 0)
2726 return isl_stat_error;
2727 return isl_stat_ok;
2729 tab->con[r].is_nonneg = 1;
2730 /* sgn == 0 */
2731 if (close_row(tab, &tab->con[r], 1) < 0)
2732 return isl_stat_error;
2733 if (drop_last_con_in_row(tab, r) < 0)
2734 return isl_stat_error;
2736 return isl_stat_ok;
2739 /* Given a tableau "tab" and an inequality constraint "con" of the tableau,
2740 * relax the inequality by one. That is, the inequality r >= 0 is replaced
2741 * by r' = r + 1 >= 0.
2742 * If r is a row variable, we simply increase the constant term by one
2743 * (taking into account the denominator).
2744 * If r is a column variable, then we need to modify each row that
2745 * refers to r = r' - 1 by substituting this equality, effectively
2746 * subtracting the coefficient of the column from the constant.
2747 * We should only do this if the minimum is manifestly unbounded,
2748 * however. Otherwise, we may end up with negative sample values
2749 * for non-negative variables.
2750 * So, if r is a column variable with a minimum that is not
2751 * manifestly unbounded, then we need to move it to a row.
2752 * However, the sample value of this row may be negative,
2753 * even after the relaxation, so we need to restore it.
2754 * We therefore prefer to pivot a column up to a row, if possible.
2756 int isl_tab_relax(struct isl_tab *tab, int con)
2758 struct isl_tab_var *var;
2760 if (!tab)
2761 return -1;
2763 var = &tab->con[con];
2765 if (var->is_row && (var->index < 0 || var->index < tab->n_redundant))
2766 isl_die(tab->mat->ctx, isl_error_invalid,
2767 "cannot relax redundant constraint", return -1);
2768 if (!var->is_row && (var->index < 0 || var->index < tab->n_dead))
2769 isl_die(tab->mat->ctx, isl_error_invalid,
2770 "cannot relax dead constraint", return -1);
2772 if (!var->is_row && !max_is_manifestly_unbounded(tab, var))
2773 if (to_row(tab, var, 1) < 0)
2774 return -1;
2775 if (!var->is_row && !min_is_manifestly_unbounded(tab, var))
2776 if (to_row(tab, var, -1) < 0)
2777 return -1;
2779 if (var->is_row) {
2780 isl_int_add(tab->mat->row[var->index][1],
2781 tab->mat->row[var->index][1], tab->mat->row[var->index][0]);
2782 if (restore_row(tab, var) < 0)
2783 return -1;
2784 } else {
2785 int i;
2786 unsigned off = 2 + tab->M;
2788 for (i = 0; i < tab->n_row; ++i) {
2789 if (isl_int_is_zero(tab->mat->row[i][off + var->index]))
2790 continue;
2791 isl_int_sub(tab->mat->row[i][1], tab->mat->row[i][1],
2792 tab->mat->row[i][off + var->index]);
2797 if (isl_tab_push_var(tab, isl_tab_undo_relax, var) < 0)
2798 return -1;
2800 return 0;
2803 /* Replace the variable v at position "pos" in the tableau "tab"
2804 * by v' = v + shift.
2806 * If the variable is in a column, then we first check if we can
2807 * simply plug in v = v' - shift. The effect on a row with
2808 * coefficient f/d for variable v is that the constant term c/d
2809 * is replaced by (c - f * shift)/d. If shift is positive and
2810 * f is negative for each row that needs to remain non-negative,
2811 * then this is clearly safe. In other words, if the minimum of v
2812 * is manifestly unbounded, then we can keep v in a column position.
2813 * Otherwise, we can pivot it down to a row.
2814 * Similarly, if shift is negative, we need to check if the maximum
2815 * of is manifestly unbounded.
2817 * If the variable is in a row (from the start or after pivoting),
2818 * then the constant term c/d is replaced by (c + d * shift)/d.
2820 int isl_tab_shift_var(struct isl_tab *tab, int pos, isl_int shift)
2822 struct isl_tab_var *var;
2824 if (!tab)
2825 return -1;
2826 if (isl_int_is_zero(shift))
2827 return 0;
2829 var = &tab->var[pos];
2830 if (!var->is_row) {
2831 if (isl_int_is_neg(shift)) {
2832 if (!max_is_manifestly_unbounded(tab, var))
2833 if (to_row(tab, var, 1) < 0)
2834 return -1;
2835 } else {
2836 if (!min_is_manifestly_unbounded(tab, var))
2837 if (to_row(tab, var, -1) < 0)
2838 return -1;
2842 if (var->is_row) {
2843 isl_int_addmul(tab->mat->row[var->index][1],
2844 shift, tab->mat->row[var->index][0]);
2845 } else {
2846 int i;
2847 unsigned off = 2 + tab->M;
2849 for (i = 0; i < tab->n_row; ++i) {
2850 if (isl_int_is_zero(tab->mat->row[i][off + var->index]))
2851 continue;
2852 isl_int_submul(tab->mat->row[i][1],
2853 shift, tab->mat->row[i][off + var->index]);
2858 return 0;
2861 /* Remove the sign constraint from constraint "con".
2863 * If the constraint variable was originally marked non-negative,
2864 * then we make sure we mark it non-negative again during rollback.
2866 int isl_tab_unrestrict(struct isl_tab *tab, int con)
2868 struct isl_tab_var *var;
2870 if (!tab)
2871 return -1;
2873 var = &tab->con[con];
2874 if (!var->is_nonneg)
2875 return 0;
2877 var->is_nonneg = 0;
2878 if (isl_tab_push_var(tab, isl_tab_undo_unrestrict, var) < 0)
2879 return -1;
2881 return 0;
2884 int isl_tab_select_facet(struct isl_tab *tab, int con)
2886 if (!tab)
2887 return -1;
2889 return cut_to_hyperplane(tab, &tab->con[con]);
2892 static int may_be_equality(struct isl_tab *tab, int row)
2894 return tab->rational ? isl_int_is_zero(tab->mat->row[row][1])
2895 : isl_int_lt(tab->mat->row[row][1],
2896 tab->mat->row[row][0]);
2899 /* Return an isl_tab_var that has been marked or NULL if no such
2900 * variable can be found.
2901 * The marked field has only been set for variables that
2902 * appear in non-redundant rows or non-dead columns.
2904 * Pick the last constraint variable that is marked and
2905 * that appears in either a non-redundant row or a non-dead columns.
2906 * Since the returned variable is tested for being a redundant constraint or
2907 * an implicit equality, there is no need to return any tab variable that
2908 * corresponds to a variable.
2910 static struct isl_tab_var *select_marked(struct isl_tab *tab)
2912 int i;
2913 struct isl_tab_var *var;
2915 for (i = tab->n_con - 1; i >= 0; --i) {
2916 var = &tab->con[i];
2917 if (var->index < 0)
2918 continue;
2919 if (var->is_row && var->index < tab->n_redundant)
2920 continue;
2921 if (!var->is_row && var->index < tab->n_dead)
2922 continue;
2923 if (var->marked)
2924 return var;
2927 return NULL;
2930 /* Check for (near) equalities among the constraints.
2931 * A constraint is an equality if it is non-negative and if
2932 * its maximal value is either
2933 * - zero (in case of rational tableaus), or
2934 * - strictly less than 1 (in case of integer tableaus)
2936 * We first mark all non-redundant and non-dead variables that
2937 * are not frozen and not obviously not an equality.
2938 * Then we iterate over all marked variables if they can attain
2939 * any values larger than zero or at least one.
2940 * If the maximal value is zero, we mark any column variables
2941 * that appear in the row as being zero and mark the row as being redundant.
2942 * Otherwise, if the maximal value is strictly less than one (and the
2943 * tableau is integer), then we restrict the value to being zero
2944 * by adding an opposite non-negative variable.
2945 * The order in which the variables are considered is not important.
2947 int isl_tab_detect_implicit_equalities(struct isl_tab *tab)
2949 int i;
2950 unsigned n_marked;
2952 if (!tab)
2953 return -1;
2954 if (tab->empty)
2955 return 0;
2956 if (tab->n_dead == tab->n_col)
2957 return 0;
2959 n_marked = 0;
2960 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2961 struct isl_tab_var *var = isl_tab_var_from_row(tab, i);
2962 var->marked = !var->frozen && var->is_nonneg &&
2963 may_be_equality(tab, i);
2964 if (var->marked)
2965 n_marked++;
2967 for (i = tab->n_dead; i < tab->n_col; ++i) {
2968 struct isl_tab_var *var = var_from_col(tab, i);
2969 var->marked = !var->frozen && var->is_nonneg;
2970 if (var->marked)
2971 n_marked++;
2973 while (n_marked) {
2974 struct isl_tab_var *var;
2975 int sgn;
2976 var = select_marked(tab);
2977 if (!var)
2978 break;
2979 var->marked = 0;
2980 n_marked--;
2981 sgn = sign_of_max(tab, var);
2982 if (sgn < 0)
2983 return -1;
2984 if (sgn == 0) {
2985 if (close_row(tab, var, 0) < 0)
2986 return -1;
2987 } else if (!tab->rational && !at_least_one(tab, var)) {
2988 if (cut_to_hyperplane(tab, var) < 0)
2989 return -1;
2990 return isl_tab_detect_implicit_equalities(tab);
2992 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2993 var = isl_tab_var_from_row(tab, i);
2994 if (!var->marked)
2995 continue;
2996 if (may_be_equality(tab, i))
2997 continue;
2998 var->marked = 0;
2999 n_marked--;
3003 return 0;
3006 /* Update the element of row_var or col_var that corresponds to
3007 * constraint tab->con[i] to a move from position "old" to position "i".
3009 static int update_con_after_move(struct isl_tab *tab, int i, int old)
3011 int *p;
3012 int index;
3014 index = tab->con[i].index;
3015 if (index == -1)
3016 return 0;
3017 p = tab->con[i].is_row ? tab->row_var : tab->col_var;
3018 if (p[index] != ~old)
3019 isl_die(tab->mat->ctx, isl_error_internal,
3020 "broken internal state", return -1);
3021 p[index] = ~i;
3023 return 0;
3026 /* Rotate the "n" constraints starting at "first" to the right,
3027 * putting the last constraint in the position of the first constraint.
3029 static int rotate_constraints(struct isl_tab *tab, int first, int n)
3031 int i, last;
3032 struct isl_tab_var var;
3034 if (n <= 1)
3035 return 0;
3037 last = first + n - 1;
3038 var = tab->con[last];
3039 for (i = last; i > first; --i) {
3040 tab->con[i] = tab->con[i - 1];
3041 if (update_con_after_move(tab, i, i - 1) < 0)
3042 return -1;
3044 tab->con[first] = var;
3045 if (update_con_after_move(tab, first, last) < 0)
3046 return -1;
3048 return 0;
3051 /* Make the equalities that are implicit in "bmap" but that have been
3052 * detected in the corresponding "tab" explicit in "bmap" and update
3053 * "tab" to reflect the new order of the constraints.
3055 * In particular, if inequality i is an implicit equality then
3056 * isl_basic_map_inequality_to_equality will move the inequality
3057 * in front of the other equality and it will move the last inequality
3058 * in the position of inequality i.
3059 * In the tableau, the inequalities of "bmap" are stored after the equalities
3060 * and so the original order
3062 * E E E E E A A A I B B B B L
3064 * is changed into
3066 * I E E E E E A A A L B B B B
3068 * where I is the implicit equality, the E are equalities,
3069 * the A inequalities before I, the B inequalities after I and
3070 * L the last inequality.
3071 * We therefore need to rotate to the right two sets of constraints,
3072 * those up to and including I and those after I.
3074 * If "tab" contains any constraints that are not in "bmap" then they
3075 * appear after those in "bmap" and they should be left untouched.
3077 * Note that this function leaves "bmap" in a temporary state
3078 * as it does not call isl_basic_map_gauss. Calling this function
3079 * is the responsibility of the caller.
3081 __isl_give isl_basic_map *isl_tab_make_equalities_explicit(struct isl_tab *tab,
3082 __isl_take isl_basic_map *bmap)
3084 int i;
3086 if (!tab || !bmap)
3087 return isl_basic_map_free(bmap);
3088 if (tab->empty)
3089 return bmap;
3091 for (i = bmap->n_ineq - 1; i >= 0; --i) {
3092 if (!isl_tab_is_equality(tab, bmap->n_eq + i))
3093 continue;
3094 isl_basic_map_inequality_to_equality(bmap, i);
3095 if (rotate_constraints(tab, 0, tab->n_eq + i + 1) < 0)
3096 return isl_basic_map_free(bmap);
3097 if (rotate_constraints(tab, tab->n_eq + i + 1,
3098 bmap->n_ineq - i) < 0)
3099 return isl_basic_map_free(bmap);
3100 tab->n_eq++;
3103 return bmap;
3106 static int con_is_redundant(struct isl_tab *tab, struct isl_tab_var *var)
3108 if (!tab)
3109 return -1;
3110 if (tab->rational) {
3111 int sgn = sign_of_min(tab, var);
3112 if (sgn < -1)
3113 return -1;
3114 return sgn >= 0;
3115 } else {
3116 int irred = isl_tab_min_at_most_neg_one(tab, var);
3117 if (irred < 0)
3118 return -1;
3119 return !irred;
3123 /* Check for (near) redundant constraints.
3124 * A constraint is redundant if it is non-negative and if
3125 * its minimal value (temporarily ignoring the non-negativity) is either
3126 * - zero (in case of rational tableaus), or
3127 * - strictly larger than -1 (in case of integer tableaus)
3129 * We first mark all non-redundant and non-dead variables that
3130 * are not frozen and not obviously negatively unbounded.
3131 * Then we iterate over all marked variables if they can attain
3132 * any values smaller than zero or at most negative one.
3133 * If not, we mark the row as being redundant (assuming it hasn't
3134 * been detected as being obviously redundant in the mean time).
3136 int isl_tab_detect_redundant(struct isl_tab *tab)
3138 int i;
3139 unsigned n_marked;
3141 if (!tab)
3142 return -1;
3143 if (tab->empty)
3144 return 0;
3145 if (tab->n_redundant == tab->n_row)
3146 return 0;
3148 n_marked = 0;
3149 for (i = tab->n_redundant; i < tab->n_row; ++i) {
3150 struct isl_tab_var *var = isl_tab_var_from_row(tab, i);
3151 var->marked = !var->frozen && var->is_nonneg;
3152 if (var->marked)
3153 n_marked++;
3155 for (i = tab->n_dead; i < tab->n_col; ++i) {
3156 struct isl_tab_var *var = var_from_col(tab, i);
3157 var->marked = !var->frozen && var->is_nonneg &&
3158 !min_is_manifestly_unbounded(tab, var);
3159 if (var->marked)
3160 n_marked++;
3162 while (n_marked) {
3163 struct isl_tab_var *var;
3164 int red;
3165 var = select_marked(tab);
3166 if (!var)
3167 break;
3168 var->marked = 0;
3169 n_marked--;
3170 red = con_is_redundant(tab, var);
3171 if (red < 0)
3172 return -1;
3173 if (red && !var->is_redundant)
3174 if (isl_tab_mark_redundant(tab, var->index) < 0)
3175 return -1;
3176 for (i = tab->n_dead; i < tab->n_col; ++i) {
3177 var = var_from_col(tab, i);
3178 if (!var->marked)
3179 continue;
3180 if (!min_is_manifestly_unbounded(tab, var))
3181 continue;
3182 var->marked = 0;
3183 n_marked--;
3187 return 0;
3190 int isl_tab_is_equality(struct isl_tab *tab, int con)
3192 int row;
3193 unsigned off;
3195 if (!tab)
3196 return -1;
3197 if (tab->con[con].is_zero)
3198 return 1;
3199 if (tab->con[con].is_redundant)
3200 return 0;
3201 if (!tab->con[con].is_row)
3202 return tab->con[con].index < tab->n_dead;
3204 row = tab->con[con].index;
3206 off = 2 + tab->M;
3207 return isl_int_is_zero(tab->mat->row[row][1]) &&
3208 !row_is_big(tab, row) &&
3209 isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
3210 tab->n_col - tab->n_dead) == -1;
3213 /* Return the minimal value of the affine expression "f" with denominator
3214 * "denom" in *opt, *opt_denom, assuming the tableau is not empty and
3215 * the expression cannot attain arbitrarily small values.
3216 * If opt_denom is NULL, then *opt is rounded up to the nearest integer.
3217 * The return value reflects the nature of the result (empty, unbounded,
3218 * minimal value returned in *opt).
3220 * This function assumes that at least one more row and at least
3221 * one more element in the constraint array are available in the tableau.
3223 enum isl_lp_result isl_tab_min(struct isl_tab *tab,
3224 isl_int *f, isl_int denom, isl_int *opt, isl_int *opt_denom,
3225 unsigned flags)
3227 int r;
3228 enum isl_lp_result res = isl_lp_ok;
3229 struct isl_tab_var *var;
3230 struct isl_tab_undo *snap;
3232 if (!tab)
3233 return isl_lp_error;
3235 if (tab->empty)
3236 return isl_lp_empty;
3238 snap = isl_tab_snap(tab);
3239 r = isl_tab_add_row(tab, f);
3240 if (r < 0)
3241 return isl_lp_error;
3242 var = &tab->con[r];
3243 for (;;) {
3244 int row, col;
3245 find_pivot(tab, var, var, -1, &row, &col);
3246 if (row == var->index) {
3247 res = isl_lp_unbounded;
3248 break;
3250 if (row == -1)
3251 break;
3252 if (isl_tab_pivot(tab, row, col) < 0)
3253 return isl_lp_error;
3255 isl_int_mul(tab->mat->row[var->index][0],
3256 tab->mat->row[var->index][0], denom);
3257 if (ISL_FL_ISSET(flags, ISL_TAB_SAVE_DUAL)) {
3258 int i;
3260 isl_vec_free(tab->dual);
3261 tab->dual = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_con);
3262 if (!tab->dual)
3263 return isl_lp_error;
3264 isl_int_set(tab->dual->el[0], tab->mat->row[var->index][0]);
3265 for (i = 0; i < tab->n_con; ++i) {
3266 int pos;
3267 if (tab->con[i].is_row) {
3268 isl_int_set_si(tab->dual->el[1 + i], 0);
3269 continue;
3271 pos = 2 + tab->M + tab->con[i].index;
3272 if (tab->con[i].negated)
3273 isl_int_neg(tab->dual->el[1 + i],
3274 tab->mat->row[var->index][pos]);
3275 else
3276 isl_int_set(tab->dual->el[1 + i],
3277 tab->mat->row[var->index][pos]);
3280 if (opt && res == isl_lp_ok) {
3281 if (opt_denom) {
3282 isl_int_set(*opt, tab->mat->row[var->index][1]);
3283 isl_int_set(*opt_denom, tab->mat->row[var->index][0]);
3284 } else
3285 get_rounded_sample_value(tab, var, 1, opt);
3287 if (isl_tab_rollback(tab, snap) < 0)
3288 return isl_lp_error;
3289 return res;
3292 /* Is the constraint at position "con" marked as being redundant?
3293 * If it is marked as representing an equality, then it is not
3294 * considered to be redundant.
3295 * Note that isl_tab_mark_redundant marks both the isl_tab_var as
3296 * redundant and moves the corresponding row into the first
3297 * tab->n_redundant positions (or removes the row, assigning it index -1),
3298 * so the final test is actually redundant itself.
3300 int isl_tab_is_redundant(struct isl_tab *tab, int con)
3302 if (!tab)
3303 return -1;
3304 if (con < 0 || con >= tab->n_con)
3305 isl_die(isl_tab_get_ctx(tab), isl_error_invalid,
3306 "position out of bounds", return -1);
3307 if (tab->con[con].is_zero)
3308 return 0;
3309 if (tab->con[con].is_redundant)
3310 return 1;
3311 return tab->con[con].is_row && tab->con[con].index < tab->n_redundant;
3314 /* Is variable "var" of "tab" fixed to a constant value by its row
3315 * in the tableau?
3316 * If so and if "value" is not NULL, then store this constant value
3317 * in "value".
3319 * That is, is it a row variable that only has non-zero coefficients
3320 * for dead columns?
3322 static isl_bool is_constant(struct isl_tab *tab, struct isl_tab_var *var,
3323 isl_int *value)
3325 unsigned off = 2 + tab->M;
3326 isl_mat *mat = tab->mat;
3327 int n;
3328 int row;
3329 int pos;
3331 if (!var->is_row)
3332 return isl_bool_false;
3333 row = var->index;
3334 if (row_is_big(tab, row))
3335 return isl_bool_false;
3336 n = tab->n_col - tab->n_dead;
3337 pos = isl_seq_first_non_zero(mat->row[row] + off + tab->n_dead, n);
3338 if (pos != -1)
3339 return isl_bool_false;
3340 if (value)
3341 isl_int_divexact(*value, mat->row[row][1], mat->row[row][0]);
3342 return isl_bool_true;
3345 /* Has the variable "var' of "tab" reached a value that is greater than
3346 * or equal (if sgn > 0) or smaller than or equal (if sgn < 0) to "target"?
3347 * "tmp" has been initialized by the caller and can be used
3348 * to perform local computations.
3350 * If the sample value involves the big parameter, then any value
3351 * is reached.
3352 * Otherwise check if n/d >= t, i.e., n >= d * t (if sgn > 0)
3353 * or n/d <= t, i.e., n <= d * t (if sgn < 0).
3355 static int reached(struct isl_tab *tab, struct isl_tab_var *var, int sgn,
3356 isl_int target, isl_int *tmp)
3358 if (row_is_big(tab, var->index))
3359 return 1;
3360 isl_int_mul(*tmp, tab->mat->row[var->index][0], target);
3361 if (sgn > 0)
3362 return isl_int_ge(tab->mat->row[var->index][1], *tmp);
3363 else
3364 return isl_int_le(tab->mat->row[var->index][1], *tmp);
3367 /* Can variable "var" of "tab" attain the value "target" by
3368 * pivoting up (if sgn > 0) or down (if sgn < 0)?
3369 * If not, then pivot up [down] to the greatest [smallest]
3370 * rational value.
3371 * "tmp" has been initialized by the caller and can be used
3372 * to perform local computations.
3374 * If the variable is manifestly unbounded in the desired direction,
3375 * then it can attain any value.
3376 * Otherwise, it can be moved to a row.
3377 * Continue pivoting until the target is reached.
3378 * If no more pivoting can be performed, the maximal [minimal]
3379 * rational value has been reached and the target cannot be reached.
3380 * If the variable would be pivoted into a manifestly unbounded column,
3381 * then the target can be reached.
3383 static isl_bool var_reaches(struct isl_tab *tab, struct isl_tab_var *var,
3384 int sgn, isl_int target, isl_int *tmp)
3386 int row, col;
3388 if (sgn < 0 && min_is_manifestly_unbounded(tab, var))
3389 return isl_bool_true;
3390 if (sgn > 0 && max_is_manifestly_unbounded(tab, var))
3391 return isl_bool_true;
3392 if (to_row(tab, var, sgn) < 0)
3393 return isl_bool_error;
3394 while (!reached(tab, var, sgn, target, tmp)) {
3395 find_pivot(tab, var, var, sgn, &row, &col);
3396 if (row == -1)
3397 return isl_bool_false;
3398 if (row == var->index)
3399 return isl_bool_true;
3400 if (isl_tab_pivot(tab, row, col) < 0)
3401 return isl_bool_error;
3404 return isl_bool_true;
3407 /* Check if variable "var" of "tab" can only attain a single (integer)
3408 * value, and, if so, add an equality constraint to fix the variable
3409 * to this single value and store the result in "target".
3410 * "target" and "tmp" have been initialized by the caller.
3412 * Given the current sample value, round it down and check
3413 * whether it is possible to attain a strictly smaller integer value.
3414 * If so, the variable is not restricted to a single integer value.
3415 * Otherwise, the search stops at the smallest rational value.
3416 * Round up this value and check whether it is possible to attain
3417 * a strictly greater integer value.
3418 * If so, the variable is not restricted to a single integer value.
3419 * Otherwise, the search stops at the greatest rational value.
3420 * If rounding down this value yields a value that is different
3421 * from rounding up the smallest rational value, then the variable
3422 * cannot attain any integer value. Mark the tableau empty.
3423 * Otherwise, add an equality constraint that fixes the variable
3424 * to the single integer value found.
3426 static isl_bool detect_constant_with_tmp(struct isl_tab *tab,
3427 struct isl_tab_var *var, isl_int *target, isl_int *tmp)
3429 isl_bool reached;
3430 isl_vec *eq;
3431 int pos;
3432 isl_stat r;
3434 get_rounded_sample_value(tab, var, -1, target);
3435 isl_int_sub_ui(*target, *target, 1);
3436 reached = var_reaches(tab, var, -1, *target, tmp);
3437 if (reached < 0 || reached)
3438 return isl_bool_not(reached);
3439 get_rounded_sample_value(tab, var, 1, target);
3440 isl_int_add_ui(*target, *target, 1);
3441 reached = var_reaches(tab, var, 1, *target, tmp);
3442 if (reached < 0 || reached)
3443 return isl_bool_not(reached);
3444 get_rounded_sample_value(tab, var, -1, tmp);
3445 isl_int_sub_ui(*target, *target, 1);
3446 if (isl_int_ne(*target, *tmp)) {
3447 if (isl_tab_mark_empty(tab) < 0)
3448 return isl_bool_error;
3449 return isl_bool_false;
3452 if (isl_tab_extend_cons(tab, 1) < 0)
3453 return isl_bool_error;
3454 eq = isl_vec_alloc(isl_tab_get_ctx(tab), 1 + tab->n_var);
3455 if (!eq)
3456 return isl_bool_error;
3457 pos = var - tab->var;
3458 isl_seq_clr(eq->el + 1, tab->n_var);
3459 isl_int_set_si(eq->el[1 + pos], -1);
3460 isl_int_set(eq->el[0], *target);
3461 r = isl_tab_add_eq(tab, eq->el);
3462 isl_vec_free(eq);
3464 return r < 0 ? isl_bool_error : isl_bool_true;
3467 /* Check if variable "var" of "tab" can only attain a single (integer)
3468 * value, and, if so, add an equality constraint to fix the variable
3469 * to this single value and store the result in "value" (if "value"
3470 * is not NULL).
3472 * If the current sample value involves the big parameter,
3473 * then the variable cannot have a fixed integer value.
3474 * If the variable is already fixed to a single value by its row, then
3475 * there is no need to add another equality constraint.
3477 * Otherwise, allocate some temporary variables and continue
3478 * with detect_constant_with_tmp.
3480 static isl_bool get_constant(struct isl_tab *tab, struct isl_tab_var *var,
3481 isl_int *value)
3483 isl_int target, tmp;
3484 isl_bool is_cst;
3486 if (var->is_row && row_is_big(tab, var->index))
3487 return isl_bool_false;
3488 is_cst = is_constant(tab, var, value);
3489 if (is_cst < 0 || is_cst)
3490 return is_cst;
3492 if (!value)
3493 isl_int_init(target);
3494 isl_int_init(tmp);
3496 is_cst = detect_constant_with_tmp(tab, var,
3497 value ? value : &target, &tmp);
3499 isl_int_clear(tmp);
3500 if (!value)
3501 isl_int_clear(target);
3503 return is_cst;
3506 /* Check if variable "var" of "tab" can only attain a single (integer)
3507 * value, and, if so, add an equality constraint to fix the variable
3508 * to this single value and store the result in "value" (if "value"
3509 * is not NULL).
3511 * For rational tableaus, nothing needs to be done.
3513 isl_bool isl_tab_is_constant(struct isl_tab *tab, int var, isl_int *value)
3515 if (!tab)
3516 return isl_bool_error;
3517 if (var < 0 || var >= tab->n_var)
3518 isl_die(isl_tab_get_ctx(tab), isl_error_invalid,
3519 "position out of bounds", return isl_bool_error);
3520 if (tab->rational)
3521 return isl_bool_false;
3523 return get_constant(tab, &tab->var[var], value);
3526 /* Check if any of the variables of "tab" can only attain a single (integer)
3527 * value, and, if so, add equality constraints to fix those variables
3528 * to these single values.
3530 * For rational tableaus, nothing needs to be done.
3532 isl_stat isl_tab_detect_constants(struct isl_tab *tab)
3534 int i;
3536 if (!tab)
3537 return isl_stat_error;
3538 if (tab->rational)
3539 return isl_stat_ok;
3541 for (i = 0; i < tab->n_var; ++i) {
3542 if (get_constant(tab, &tab->var[i], NULL) < 0)
3543 return isl_stat_error;
3546 return isl_stat_ok;
3549 /* Take a snapshot of the tableau that can be restored by a call to
3550 * isl_tab_rollback.
3552 struct isl_tab_undo *isl_tab_snap(struct isl_tab *tab)
3554 if (!tab)
3555 return NULL;
3556 tab->need_undo = 1;
3557 return tab->top;
3560 /* Does "tab" need to keep track of undo information?
3561 * That is, was a snapshot taken that may need to be restored?
3563 isl_bool isl_tab_need_undo(struct isl_tab *tab)
3565 if (!tab)
3566 return isl_bool_error;
3568 return tab->need_undo;
3571 /* Remove all tracking of undo information from "tab", invalidating
3572 * any snapshots that may have been taken of the tableau.
3573 * Since all snapshots have been invalidated, there is also
3574 * no need to start keeping track of undo information again.
3576 void isl_tab_clear_undo(struct isl_tab *tab)
3578 if (!tab)
3579 return;
3581 free_undo(tab);
3582 tab->need_undo = 0;
3585 /* Undo the operation performed by isl_tab_relax.
3587 static int unrelax(struct isl_tab *tab, struct isl_tab_var *var) WARN_UNUSED;
3588 static int unrelax(struct isl_tab *tab, struct isl_tab_var *var)
3590 unsigned off = 2 + tab->M;
3592 if (!var->is_row && !max_is_manifestly_unbounded(tab, var))
3593 if (to_row(tab, var, 1) < 0)
3594 return -1;
3596 if (var->is_row) {
3597 isl_int_sub(tab->mat->row[var->index][1],
3598 tab->mat->row[var->index][1], tab->mat->row[var->index][0]);
3599 if (var->is_nonneg) {
3600 int sgn = restore_row(tab, var);
3601 isl_assert(tab->mat->ctx, sgn >= 0, return -1);
3603 } else {
3604 int i;
3606 for (i = 0; i < tab->n_row; ++i) {
3607 if (isl_int_is_zero(tab->mat->row[i][off + var->index]))
3608 continue;
3609 isl_int_add(tab->mat->row[i][1], tab->mat->row[i][1],
3610 tab->mat->row[i][off + var->index]);
3615 return 0;
3618 /* Undo the operation performed by isl_tab_unrestrict.
3620 * In particular, mark the variable as being non-negative and make
3621 * sure the sample value respects this constraint.
3623 static int ununrestrict(struct isl_tab *tab, struct isl_tab_var *var)
3625 var->is_nonneg = 1;
3627 if (var->is_row && restore_row(tab, var) < -1)
3628 return -1;
3630 return 0;
3633 /* Unmark the last redundant row in "tab" as being redundant.
3634 * This undoes part of the modifications performed by isl_tab_mark_redundant.
3635 * In particular, remove the redundant mark and make
3636 * sure the sample value respects the constraint again.
3637 * A variable that is marked non-negative by isl_tab_mark_redundant
3638 * is covered by a separate undo record.
3640 static isl_stat restore_last_redundant(struct isl_tab *tab)
3642 struct isl_tab_var *var;
3644 if (tab->n_redundant < 1)
3645 isl_die(isl_tab_get_ctx(tab), isl_error_internal,
3646 "no redundant rows", return isl_stat_error);
3648 var = isl_tab_var_from_row(tab, tab->n_redundant - 1);
3649 var->is_redundant = 0;
3650 tab->n_redundant--;
3651 restore_row(tab, var);
3653 return isl_stat_ok;
3656 static int perform_undo_var(struct isl_tab *tab, struct isl_tab_undo *undo) WARN_UNUSED;
3657 static int perform_undo_var(struct isl_tab *tab, struct isl_tab_undo *undo)
3659 struct isl_tab_var *var = var_from_index(tab, undo->u.var_index);
3660 switch (undo->type) {
3661 case isl_tab_undo_nonneg:
3662 var->is_nonneg = 0;
3663 break;
3664 case isl_tab_undo_redundant:
3665 if (!var->is_row || var->index != tab->n_redundant - 1)
3666 isl_die(isl_tab_get_ctx(tab), isl_error_internal,
3667 "not undoing last redundant row", return -1);
3668 return restore_last_redundant(tab);
3669 case isl_tab_undo_freeze:
3670 var->frozen = 0;
3671 break;
3672 case isl_tab_undo_zero:
3673 var->is_zero = 0;
3674 if (!var->is_row)
3675 tab->n_dead--;
3676 break;
3677 case isl_tab_undo_allocate:
3678 if (undo->u.var_index >= 0) {
3679 isl_assert(tab->mat->ctx, !var->is_row, return -1);
3680 return drop_col(tab, var->index);
3682 if (!var->is_row) {
3683 if (!max_is_manifestly_unbounded(tab, var)) {
3684 if (to_row(tab, var, 1) < 0)
3685 return -1;
3686 } else if (!min_is_manifestly_unbounded(tab, var)) {
3687 if (to_row(tab, var, -1) < 0)
3688 return -1;
3689 } else
3690 if (to_row(tab, var, 0) < 0)
3691 return -1;
3693 return drop_row(tab, var->index);
3694 case isl_tab_undo_relax:
3695 return unrelax(tab, var);
3696 case isl_tab_undo_unrestrict:
3697 return ununrestrict(tab, var);
3698 default:
3699 isl_die(tab->mat->ctx, isl_error_internal,
3700 "perform_undo_var called on invalid undo record",
3701 return -1);
3704 return 0;
3707 /* Restore all rows that have been marked redundant by isl_tab_mark_redundant
3708 * and that have been preserved in the tableau.
3709 * Note that isl_tab_mark_redundant may also have marked some variables
3710 * as being non-negative before marking them redundant. These need
3711 * to be removed as well as otherwise some constraints could end up
3712 * getting marked redundant with respect to the variable.
3714 isl_stat isl_tab_restore_redundant(struct isl_tab *tab)
3716 if (!tab)
3717 return isl_stat_error;
3719 if (tab->need_undo)
3720 isl_die(isl_tab_get_ctx(tab), isl_error_invalid,
3721 "manually restoring redundant constraints "
3722 "interferes with undo history",
3723 return isl_stat_error);
3725 while (tab->n_redundant > 0) {
3726 if (tab->row_var[tab->n_redundant - 1] >= 0) {
3727 struct isl_tab_var *var;
3729 var = isl_tab_var_from_row(tab, tab->n_redundant - 1);
3730 var->is_nonneg = 0;
3732 restore_last_redundant(tab);
3734 return isl_stat_ok;
3737 /* Undo the addition of an integer division to the basic map representation
3738 * of "tab" in position "pos".
3740 static isl_stat drop_bmap_div(struct isl_tab *tab, int pos)
3742 int off;
3744 off = tab->n_var - isl_basic_map_dim(tab->bmap, isl_dim_div);
3745 if (isl_basic_map_drop_div(tab->bmap, pos - off) < 0)
3746 return isl_stat_error;
3747 if (tab->samples) {
3748 tab->samples = isl_mat_drop_cols(tab->samples, 1 + pos, 1);
3749 if (!tab->samples)
3750 return isl_stat_error;
3753 return isl_stat_ok;
3756 /* Restore the tableau to the state where the basic variables
3757 * are those in "col_var".
3758 * We first construct a list of variables that are currently in
3759 * the basis, but shouldn't. Then we iterate over all variables
3760 * that should be in the basis and for each one that is currently
3761 * not in the basis, we exchange it with one of the elements of the
3762 * list constructed before.
3763 * We can always find an appropriate variable to pivot with because
3764 * the current basis is mapped to the old basis by a non-singular
3765 * matrix and so we can never end up with a zero row.
3767 static int restore_basis(struct isl_tab *tab, int *col_var)
3769 int i, j;
3770 int n_extra = 0;
3771 int *extra = NULL; /* current columns that contain bad stuff */
3772 unsigned off = 2 + tab->M;
3774 extra = isl_alloc_array(tab->mat->ctx, int, tab->n_col);
3775 if (tab->n_col && !extra)
3776 goto error;
3777 for (i = 0; i < tab->n_col; ++i) {
3778 for (j = 0; j < tab->n_col; ++j)
3779 if (tab->col_var[i] == col_var[j])
3780 break;
3781 if (j < tab->n_col)
3782 continue;
3783 extra[n_extra++] = i;
3785 for (i = 0; i < tab->n_col && n_extra > 0; ++i) {
3786 struct isl_tab_var *var;
3787 int row;
3789 for (j = 0; j < tab->n_col; ++j)
3790 if (col_var[i] == tab->col_var[j])
3791 break;
3792 if (j < tab->n_col)
3793 continue;
3794 var = var_from_index(tab, col_var[i]);
3795 row = var->index;
3796 for (j = 0; j < n_extra; ++j)
3797 if (!isl_int_is_zero(tab->mat->row[row][off+extra[j]]))
3798 break;
3799 isl_assert(tab->mat->ctx, j < n_extra, goto error);
3800 if (isl_tab_pivot(tab, row, extra[j]) < 0)
3801 goto error;
3802 extra[j] = extra[--n_extra];
3805 free(extra);
3806 return 0;
3807 error:
3808 free(extra);
3809 return -1;
3812 /* Remove all samples with index n or greater, i.e., those samples
3813 * that were added since we saved this number of samples in
3814 * isl_tab_save_samples.
3816 static void drop_samples_since(struct isl_tab *tab, int n)
3818 int i;
3820 for (i = tab->n_sample - 1; i >= 0 && tab->n_sample > n; --i) {
3821 if (tab->sample_index[i] < n)
3822 continue;
3824 if (i != tab->n_sample - 1) {
3825 int t = tab->sample_index[tab->n_sample-1];
3826 tab->sample_index[tab->n_sample-1] = tab->sample_index[i];
3827 tab->sample_index[i] = t;
3828 isl_mat_swap_rows(tab->samples, tab->n_sample-1, i);
3830 tab->n_sample--;
3834 static int perform_undo(struct isl_tab *tab, struct isl_tab_undo *undo) WARN_UNUSED;
3835 static int perform_undo(struct isl_tab *tab, struct isl_tab_undo *undo)
3837 switch (undo->type) {
3838 case isl_tab_undo_rational:
3839 tab->rational = 0;
3840 break;
3841 case isl_tab_undo_empty:
3842 tab->empty = 0;
3843 break;
3844 case isl_tab_undo_nonneg:
3845 case isl_tab_undo_redundant:
3846 case isl_tab_undo_freeze:
3847 case isl_tab_undo_zero:
3848 case isl_tab_undo_allocate:
3849 case isl_tab_undo_relax:
3850 case isl_tab_undo_unrestrict:
3851 return perform_undo_var(tab, undo);
3852 case isl_tab_undo_bmap_eq:
3853 return isl_basic_map_free_equality(tab->bmap, 1);
3854 case isl_tab_undo_bmap_ineq:
3855 return isl_basic_map_free_inequality(tab->bmap, 1);
3856 case isl_tab_undo_bmap_div:
3857 return drop_bmap_div(tab, undo->u.var_index);
3858 case isl_tab_undo_saved_basis:
3859 if (restore_basis(tab, undo->u.col_var) < 0)
3860 return -1;
3861 break;
3862 case isl_tab_undo_drop_sample:
3863 tab->n_outside--;
3864 break;
3865 case isl_tab_undo_saved_samples:
3866 drop_samples_since(tab, undo->u.n);
3867 break;
3868 case isl_tab_undo_callback:
3869 return undo->u.callback->run(undo->u.callback);
3870 default:
3871 isl_assert(tab->mat->ctx, 0, return -1);
3873 return 0;
3876 /* Return the tableau to the state it was in when the snapshot "snap"
3877 * was taken.
3879 int isl_tab_rollback(struct isl_tab *tab, struct isl_tab_undo *snap)
3881 struct isl_tab_undo *undo, *next;
3883 if (!tab)
3884 return -1;
3886 tab->in_undo = 1;
3887 for (undo = tab->top; undo && undo != &tab->bottom; undo = next) {
3888 next = undo->next;
3889 if (undo == snap)
3890 break;
3891 if (perform_undo(tab, undo) < 0) {
3892 tab->top = undo;
3893 free_undo(tab);
3894 tab->in_undo = 0;
3895 return -1;
3897 free_undo_record(undo);
3899 tab->in_undo = 0;
3900 tab->top = undo;
3901 if (!undo)
3902 return -1;
3903 return 0;
3906 /* The given row "row" represents an inequality violated by all
3907 * points in the tableau. Check for some special cases of such
3908 * separating constraints.
3909 * In particular, if the row has been reduced to the constant -1,
3910 * then we know the inequality is adjacent (but opposite) to
3911 * an equality in the tableau.
3912 * If the row has been reduced to r = c*(-1 -r'), with r' an inequality
3913 * of the tableau and c a positive constant, then the inequality
3914 * is adjacent (but opposite) to the inequality r'.
3916 static enum isl_ineq_type separation_type(struct isl_tab *tab, unsigned row)
3918 int pos;
3919 unsigned off = 2 + tab->M;
3921 if (tab->rational)
3922 return isl_ineq_separate;
3924 if (!isl_int_is_one(tab->mat->row[row][0]))
3925 return isl_ineq_separate;
3927 pos = isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
3928 tab->n_col - tab->n_dead);
3929 if (pos == -1) {
3930 if (isl_int_is_negone(tab->mat->row[row][1]))
3931 return isl_ineq_adj_eq;
3932 else
3933 return isl_ineq_separate;
3936 if (!isl_int_eq(tab->mat->row[row][1],
3937 tab->mat->row[row][off + tab->n_dead + pos]))
3938 return isl_ineq_separate;
3940 pos = isl_seq_first_non_zero(
3941 tab->mat->row[row] + off + tab->n_dead + pos + 1,
3942 tab->n_col - tab->n_dead - pos - 1);
3944 return pos == -1 ? isl_ineq_adj_ineq : isl_ineq_separate;
3947 /* Check the effect of inequality "ineq" on the tableau "tab".
3948 * The result may be
3949 * isl_ineq_redundant: satisfied by all points in the tableau
3950 * isl_ineq_separate: satisfied by no point in the tableau
3951 * isl_ineq_cut: satisfied by some by not all points
3952 * isl_ineq_adj_eq: adjacent to an equality
3953 * isl_ineq_adj_ineq: adjacent to an inequality.
3955 enum isl_ineq_type isl_tab_ineq_type(struct isl_tab *tab, isl_int *ineq)
3957 enum isl_ineq_type type = isl_ineq_error;
3958 struct isl_tab_undo *snap = NULL;
3959 int con;
3960 int row;
3962 if (!tab)
3963 return isl_ineq_error;
3965 if (isl_tab_extend_cons(tab, 1) < 0)
3966 return isl_ineq_error;
3968 snap = isl_tab_snap(tab);
3970 con = isl_tab_add_row(tab, ineq);
3971 if (con < 0)
3972 goto error;
3974 row = tab->con[con].index;
3975 if (isl_tab_row_is_redundant(tab, row))
3976 type = isl_ineq_redundant;
3977 else if (isl_int_is_neg(tab->mat->row[row][1]) &&
3978 (tab->rational ||
3979 isl_int_abs_ge(tab->mat->row[row][1],
3980 tab->mat->row[row][0]))) {
3981 int nonneg = at_least_zero(tab, &tab->con[con]);
3982 if (nonneg < 0)
3983 goto error;
3984 if (nonneg)
3985 type = isl_ineq_cut;
3986 else
3987 type = separation_type(tab, row);
3988 } else {
3989 int red = con_is_redundant(tab, &tab->con[con]);
3990 if (red < 0)
3991 goto error;
3992 if (!red)
3993 type = isl_ineq_cut;
3994 else
3995 type = isl_ineq_redundant;
3998 if (isl_tab_rollback(tab, snap))
3999 return isl_ineq_error;
4000 return type;
4001 error:
4002 return isl_ineq_error;
4005 isl_stat isl_tab_track_bmap(struct isl_tab *tab, __isl_take isl_basic_map *bmap)
4007 bmap = isl_basic_map_cow(bmap);
4008 if (!tab || !bmap)
4009 goto error;
4011 if (tab->empty) {
4012 bmap = isl_basic_map_set_to_empty(bmap);
4013 if (!bmap)
4014 goto error;
4015 tab->bmap = bmap;
4016 return isl_stat_ok;
4019 isl_assert(tab->mat->ctx, tab->n_eq == bmap->n_eq, goto error);
4020 isl_assert(tab->mat->ctx,
4021 tab->n_con == bmap->n_eq + bmap->n_ineq, goto error);
4023 tab->bmap = bmap;
4025 return isl_stat_ok;
4026 error:
4027 isl_basic_map_free(bmap);
4028 return isl_stat_error;
4031 isl_stat isl_tab_track_bset(struct isl_tab *tab, __isl_take isl_basic_set *bset)
4033 return isl_tab_track_bmap(tab, bset_to_bmap(bset));
4036 __isl_keep isl_basic_set *isl_tab_peek_bset(struct isl_tab *tab)
4038 if (!tab)
4039 return NULL;
4041 return bset_from_bmap(tab->bmap);
4044 static void isl_tab_print_internal(__isl_keep struct isl_tab *tab,
4045 FILE *out, int indent)
4047 unsigned r, c;
4048 int i;
4050 if (!tab) {
4051 fprintf(out, "%*snull tab\n", indent, "");
4052 return;
4054 fprintf(out, "%*sn_redundant: %d, n_dead: %d", indent, "",
4055 tab->n_redundant, tab->n_dead);
4056 if (tab->rational)
4057 fprintf(out, ", rational");
4058 if (tab->empty)
4059 fprintf(out, ", empty");
4060 fprintf(out, "\n");
4061 fprintf(out, "%*s[", indent, "");
4062 for (i = 0; i < tab->n_var; ++i) {
4063 if (i)
4064 fprintf(out, (i == tab->n_param ||
4065 i == tab->n_var - tab->n_div) ? "; "
4066 : ", ");
4067 fprintf(out, "%c%d%s", tab->var[i].is_row ? 'r' : 'c',
4068 tab->var[i].index,
4069 tab->var[i].is_zero ? " [=0]" :
4070 tab->var[i].is_redundant ? " [R]" : "");
4072 fprintf(out, "]\n");
4073 fprintf(out, "%*s[", indent, "");
4074 for (i = 0; i < tab->n_con; ++i) {
4075 if (i)
4076 fprintf(out, ", ");
4077 fprintf(out, "%c%d%s", tab->con[i].is_row ? 'r' : 'c',
4078 tab->con[i].index,
4079 tab->con[i].is_zero ? " [=0]" :
4080 tab->con[i].is_redundant ? " [R]" : "");
4082 fprintf(out, "]\n");
4083 fprintf(out, "%*s[", indent, "");
4084 for (i = 0; i < tab->n_row; ++i) {
4085 const char *sign = "";
4086 if (i)
4087 fprintf(out, ", ");
4088 if (tab->row_sign) {
4089 if (tab->row_sign[i] == isl_tab_row_unknown)
4090 sign = "?";
4091 else if (tab->row_sign[i] == isl_tab_row_neg)
4092 sign = "-";
4093 else if (tab->row_sign[i] == isl_tab_row_pos)
4094 sign = "+";
4095 else
4096 sign = "+-";
4098 fprintf(out, "r%d: %d%s%s", i, tab->row_var[i],
4099 isl_tab_var_from_row(tab, i)->is_nonneg ? " [>=0]" : "", sign);
4101 fprintf(out, "]\n");
4102 fprintf(out, "%*s[", indent, "");
4103 for (i = 0; i < tab->n_col; ++i) {
4104 if (i)
4105 fprintf(out, ", ");
4106 fprintf(out, "c%d: %d%s", i, tab->col_var[i],
4107 var_from_col(tab, i)->is_nonneg ? " [>=0]" : "");
4109 fprintf(out, "]\n");
4110 r = tab->mat->n_row;
4111 tab->mat->n_row = tab->n_row;
4112 c = tab->mat->n_col;
4113 tab->mat->n_col = 2 + tab->M + tab->n_col;
4114 isl_mat_print_internal(tab->mat, out, indent);
4115 tab->mat->n_row = r;
4116 tab->mat->n_col = c;
4117 if (tab->bmap)
4118 isl_basic_map_print_internal(tab->bmap, out, indent);
4121 void isl_tab_dump(__isl_keep struct isl_tab *tab)
4123 isl_tab_print_internal(tab, stderr, 0);