isl_aff_div: handle NaN input
[isl.git] / isl_tab.c
blob0dcfc14c8abaeda1bdd3b980fe34d4b5d5666ff8
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2013 Ecole Normale Superieure
5 * Use of this software is governed by the MIT license
7 * Written by Sven Verdoolaege, K.U.Leuven, Departement
8 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9 * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
12 #include <isl_ctx_private.h>
13 #include <isl_mat_private.h>
14 #include <isl_vec_private.h>
15 #include "isl_map_private.h"
16 #include "isl_tab.h"
17 #include <isl_seq.h>
18 #include <isl_config.h>
21 * The implementation of tableaus in this file was inspired by Section 8
22 * of David Detlefs, Greg Nelson and James B. Saxe, "Simplify: a theorem
23 * prover for program checking".
26 struct isl_tab *isl_tab_alloc(struct isl_ctx *ctx,
27 unsigned n_row, unsigned n_var, unsigned M)
29 int i;
30 struct isl_tab *tab;
31 unsigned off = 2 + M;
33 tab = isl_calloc_type(ctx, struct isl_tab);
34 if (!tab)
35 return NULL;
36 tab->mat = isl_mat_alloc(ctx, n_row, off + n_var);
37 if (!tab->mat)
38 goto error;
39 tab->var = isl_alloc_array(ctx, struct isl_tab_var, n_var);
40 if (n_var && !tab->var)
41 goto error;
42 tab->con = isl_alloc_array(ctx, struct isl_tab_var, n_row);
43 if (n_row && !tab->con)
44 goto error;
45 tab->col_var = isl_alloc_array(ctx, int, n_var);
46 if (n_var && !tab->col_var)
47 goto error;
48 tab->row_var = isl_alloc_array(ctx, int, n_row);
49 if (n_row && !tab->row_var)
50 goto error;
51 for (i = 0; i < n_var; ++i) {
52 tab->var[i].index = i;
53 tab->var[i].is_row = 0;
54 tab->var[i].is_nonneg = 0;
55 tab->var[i].is_zero = 0;
56 tab->var[i].is_redundant = 0;
57 tab->var[i].frozen = 0;
58 tab->var[i].negated = 0;
59 tab->col_var[i] = i;
61 tab->n_row = 0;
62 tab->n_con = 0;
63 tab->n_eq = 0;
64 tab->max_con = n_row;
65 tab->n_col = n_var;
66 tab->n_var = n_var;
67 tab->max_var = n_var;
68 tab->n_param = 0;
69 tab->n_div = 0;
70 tab->n_dead = 0;
71 tab->n_redundant = 0;
72 tab->strict_redundant = 0;
73 tab->need_undo = 0;
74 tab->rational = 0;
75 tab->empty = 0;
76 tab->in_undo = 0;
77 tab->M = M;
78 tab->cone = 0;
79 tab->bottom.type = isl_tab_undo_bottom;
80 tab->bottom.next = NULL;
81 tab->top = &tab->bottom;
83 tab->n_zero = 0;
84 tab->n_unbounded = 0;
85 tab->basis = NULL;
87 return tab;
88 error:
89 isl_tab_free(tab);
90 return NULL;
93 isl_ctx *isl_tab_get_ctx(struct isl_tab *tab)
95 return tab ? isl_mat_get_ctx(tab->mat) : NULL;
98 int isl_tab_extend_cons(struct isl_tab *tab, unsigned n_new)
100 unsigned off;
102 if (!tab)
103 return -1;
105 off = 2 + tab->M;
107 if (tab->max_con < tab->n_con + n_new) {
108 struct isl_tab_var *con;
110 con = isl_realloc_array(tab->mat->ctx, tab->con,
111 struct isl_tab_var, tab->max_con + n_new);
112 if (!con)
113 return -1;
114 tab->con = con;
115 tab->max_con += n_new;
117 if (tab->mat->n_row < tab->n_row + n_new) {
118 int *row_var;
120 tab->mat = isl_mat_extend(tab->mat,
121 tab->n_row + n_new, off + tab->n_col);
122 if (!tab->mat)
123 return -1;
124 row_var = isl_realloc_array(tab->mat->ctx, tab->row_var,
125 int, tab->mat->n_row);
126 if (!row_var)
127 return -1;
128 tab->row_var = row_var;
129 if (tab->row_sign) {
130 enum isl_tab_row_sign *s;
131 s = isl_realloc_array(tab->mat->ctx, tab->row_sign,
132 enum isl_tab_row_sign, tab->mat->n_row);
133 if (!s)
134 return -1;
135 tab->row_sign = s;
138 return 0;
141 /* Make room for at least n_new extra variables.
142 * Return -1 if anything went wrong.
144 int isl_tab_extend_vars(struct isl_tab *tab, unsigned n_new)
146 struct isl_tab_var *var;
147 unsigned off = 2 + tab->M;
149 if (tab->max_var < tab->n_var + n_new) {
150 var = isl_realloc_array(tab->mat->ctx, tab->var,
151 struct isl_tab_var, tab->n_var + n_new);
152 if (!var)
153 return -1;
154 tab->var = var;
155 tab->max_var += n_new;
158 if (tab->mat->n_col < off + tab->n_col + n_new) {
159 int *p;
161 tab->mat = isl_mat_extend(tab->mat,
162 tab->mat->n_row, off + tab->n_col + n_new);
163 if (!tab->mat)
164 return -1;
165 p = isl_realloc_array(tab->mat->ctx, tab->col_var,
166 int, tab->n_col + n_new);
167 if (!p)
168 return -1;
169 tab->col_var = p;
172 return 0;
175 struct isl_tab *isl_tab_extend(struct isl_tab *tab, unsigned n_new)
177 if (isl_tab_extend_cons(tab, n_new) >= 0)
178 return tab;
180 isl_tab_free(tab);
181 return NULL;
184 static void free_undo_record(struct isl_tab_undo *undo)
186 switch (undo->type) {
187 case isl_tab_undo_saved_basis:
188 free(undo->u.col_var);
189 break;
190 default:;
192 free(undo);
195 static void free_undo(struct isl_tab *tab)
197 struct isl_tab_undo *undo, *next;
199 for (undo = tab->top; undo && undo != &tab->bottom; undo = next) {
200 next = undo->next;
201 free_undo_record(undo);
203 tab->top = undo;
206 void isl_tab_free(struct isl_tab *tab)
208 if (!tab)
209 return;
210 free_undo(tab);
211 isl_mat_free(tab->mat);
212 isl_vec_free(tab->dual);
213 isl_basic_map_free(tab->bmap);
214 free(tab->var);
215 free(tab->con);
216 free(tab->row_var);
217 free(tab->col_var);
218 free(tab->row_sign);
219 isl_mat_free(tab->samples);
220 free(tab->sample_index);
221 isl_mat_free(tab->basis);
222 free(tab);
225 struct isl_tab *isl_tab_dup(struct isl_tab *tab)
227 int i;
228 struct isl_tab *dup;
229 unsigned off;
231 if (!tab)
232 return NULL;
234 off = 2 + tab->M;
235 dup = isl_calloc_type(tab->mat->ctx, struct isl_tab);
236 if (!dup)
237 return NULL;
238 dup->mat = isl_mat_dup(tab->mat);
239 if (!dup->mat)
240 goto error;
241 dup->var = isl_alloc_array(tab->mat->ctx, struct isl_tab_var, tab->max_var);
242 if (tab->max_var && !dup->var)
243 goto error;
244 for (i = 0; i < tab->n_var; ++i)
245 dup->var[i] = tab->var[i];
246 dup->con = isl_alloc_array(tab->mat->ctx, struct isl_tab_var, tab->max_con);
247 if (tab->max_con && !dup->con)
248 goto error;
249 for (i = 0; i < tab->n_con; ++i)
250 dup->con[i] = tab->con[i];
251 dup->col_var = isl_alloc_array(tab->mat->ctx, int, tab->mat->n_col - off);
252 if ((tab->mat->n_col - off) && !dup->col_var)
253 goto error;
254 for (i = 0; i < tab->n_col; ++i)
255 dup->col_var[i] = tab->col_var[i];
256 dup->row_var = isl_alloc_array(tab->mat->ctx, int, tab->mat->n_row);
257 if (tab->mat->n_row && !dup->row_var)
258 goto error;
259 for (i = 0; i < tab->n_row; ++i)
260 dup->row_var[i] = tab->row_var[i];
261 if (tab->row_sign) {
262 dup->row_sign = isl_alloc_array(tab->mat->ctx, enum isl_tab_row_sign,
263 tab->mat->n_row);
264 if (tab->mat->n_row && !dup->row_sign)
265 goto error;
266 for (i = 0; i < tab->n_row; ++i)
267 dup->row_sign[i] = tab->row_sign[i];
269 if (tab->samples) {
270 dup->samples = isl_mat_dup(tab->samples);
271 if (!dup->samples)
272 goto error;
273 dup->sample_index = isl_alloc_array(tab->mat->ctx, int,
274 tab->samples->n_row);
275 if (tab->samples->n_row && !dup->sample_index)
276 goto error;
277 dup->n_sample = tab->n_sample;
278 dup->n_outside = tab->n_outside;
280 dup->n_row = tab->n_row;
281 dup->n_con = tab->n_con;
282 dup->n_eq = tab->n_eq;
283 dup->max_con = tab->max_con;
284 dup->n_col = tab->n_col;
285 dup->n_var = tab->n_var;
286 dup->max_var = tab->max_var;
287 dup->n_param = tab->n_param;
288 dup->n_div = tab->n_div;
289 dup->n_dead = tab->n_dead;
290 dup->n_redundant = tab->n_redundant;
291 dup->rational = tab->rational;
292 dup->empty = tab->empty;
293 dup->strict_redundant = 0;
294 dup->need_undo = 0;
295 dup->in_undo = 0;
296 dup->M = tab->M;
297 tab->cone = tab->cone;
298 dup->bottom.type = isl_tab_undo_bottom;
299 dup->bottom.next = NULL;
300 dup->top = &dup->bottom;
302 dup->n_zero = tab->n_zero;
303 dup->n_unbounded = tab->n_unbounded;
304 dup->basis = isl_mat_dup(tab->basis);
306 return dup;
307 error:
308 isl_tab_free(dup);
309 return NULL;
312 /* Construct the coefficient matrix of the product tableau
313 * of two tableaus.
314 * mat{1,2} is the coefficient matrix of tableau {1,2}
315 * row{1,2} is the number of rows in tableau {1,2}
316 * col{1,2} is the number of columns in tableau {1,2}
317 * off is the offset to the coefficient column (skipping the
318 * denominator, the constant term and the big parameter if any)
319 * r{1,2} is the number of redundant rows in tableau {1,2}
320 * d{1,2} is the number of dead columns in tableau {1,2}
322 * The order of the rows and columns in the result is as explained
323 * in isl_tab_product.
325 static struct isl_mat *tab_mat_product(struct isl_mat *mat1,
326 struct isl_mat *mat2, unsigned row1, unsigned row2,
327 unsigned col1, unsigned col2,
328 unsigned off, unsigned r1, unsigned r2, unsigned d1, unsigned d2)
330 int i;
331 struct isl_mat *prod;
332 unsigned n;
334 prod = isl_mat_alloc(mat1->ctx, mat1->n_row + mat2->n_row,
335 off + col1 + col2);
336 if (!prod)
337 return NULL;
339 n = 0;
340 for (i = 0; i < r1; ++i) {
341 isl_seq_cpy(prod->row[n + i], mat1->row[i], off + d1);
342 isl_seq_clr(prod->row[n + i] + off + d1, d2);
343 isl_seq_cpy(prod->row[n + i] + off + d1 + d2,
344 mat1->row[i] + off + d1, col1 - d1);
345 isl_seq_clr(prod->row[n + i] + off + col1 + d1, col2 - d2);
348 n += r1;
349 for (i = 0; i < r2; ++i) {
350 isl_seq_cpy(prod->row[n + i], mat2->row[i], off);
351 isl_seq_clr(prod->row[n + i] + off, d1);
352 isl_seq_cpy(prod->row[n + i] + off + d1,
353 mat2->row[i] + off, d2);
354 isl_seq_clr(prod->row[n + i] + off + d1 + d2, col1 - d1);
355 isl_seq_cpy(prod->row[n + i] + off + col1 + d1,
356 mat2->row[i] + off + d2, col2 - d2);
359 n += r2;
360 for (i = 0; i < row1 - r1; ++i) {
361 isl_seq_cpy(prod->row[n + i], mat1->row[r1 + i], off + d1);
362 isl_seq_clr(prod->row[n + i] + off + d1, d2);
363 isl_seq_cpy(prod->row[n + i] + off + d1 + d2,
364 mat1->row[r1 + i] + off + d1, col1 - d1);
365 isl_seq_clr(prod->row[n + i] + off + col1 + d1, col2 - d2);
368 n += row1 - r1;
369 for (i = 0; i < row2 - r2; ++i) {
370 isl_seq_cpy(prod->row[n + i], mat2->row[r2 + i], off);
371 isl_seq_clr(prod->row[n + i] + off, d1);
372 isl_seq_cpy(prod->row[n + i] + off + d1,
373 mat2->row[r2 + i] + off, d2);
374 isl_seq_clr(prod->row[n + i] + off + d1 + d2, col1 - d1);
375 isl_seq_cpy(prod->row[n + i] + off + col1 + d1,
376 mat2->row[r2 + i] + off + d2, col2 - d2);
379 return prod;
382 /* Update the row or column index of a variable that corresponds
383 * to a variable in the first input tableau.
385 static void update_index1(struct isl_tab_var *var,
386 unsigned r1, unsigned r2, unsigned d1, unsigned d2)
388 if (var->index == -1)
389 return;
390 if (var->is_row && var->index >= r1)
391 var->index += r2;
392 if (!var->is_row && var->index >= d1)
393 var->index += d2;
396 /* Update the row or column index of a variable that corresponds
397 * to a variable in the second input tableau.
399 static void update_index2(struct isl_tab_var *var,
400 unsigned row1, unsigned col1,
401 unsigned r1, unsigned r2, unsigned d1, unsigned d2)
403 if (var->index == -1)
404 return;
405 if (var->is_row) {
406 if (var->index < r2)
407 var->index += r1;
408 else
409 var->index += row1;
410 } else {
411 if (var->index < d2)
412 var->index += d1;
413 else
414 var->index += col1;
418 /* Create a tableau that represents the Cartesian product of the sets
419 * represented by tableaus tab1 and tab2.
420 * The order of the rows in the product is
421 * - redundant rows of tab1
422 * - redundant rows of tab2
423 * - non-redundant rows of tab1
424 * - non-redundant rows of tab2
425 * The order of the columns is
426 * - denominator
427 * - constant term
428 * - coefficient of big parameter, if any
429 * - dead columns of tab1
430 * - dead columns of tab2
431 * - live columns of tab1
432 * - live columns of tab2
433 * The order of the variables and the constraints is a concatenation
434 * of order in the two input tableaus.
436 struct isl_tab *isl_tab_product(struct isl_tab *tab1, struct isl_tab *tab2)
438 int i;
439 struct isl_tab *prod;
440 unsigned off;
441 unsigned r1, r2, d1, d2;
443 if (!tab1 || !tab2)
444 return NULL;
446 isl_assert(tab1->mat->ctx, tab1->M == tab2->M, return NULL);
447 isl_assert(tab1->mat->ctx, tab1->rational == tab2->rational, return NULL);
448 isl_assert(tab1->mat->ctx, tab1->cone == tab2->cone, return NULL);
449 isl_assert(tab1->mat->ctx, !tab1->row_sign, return NULL);
450 isl_assert(tab1->mat->ctx, !tab2->row_sign, return NULL);
451 isl_assert(tab1->mat->ctx, tab1->n_param == 0, return NULL);
452 isl_assert(tab1->mat->ctx, tab2->n_param == 0, return NULL);
453 isl_assert(tab1->mat->ctx, tab1->n_div == 0, return NULL);
454 isl_assert(tab1->mat->ctx, tab2->n_div == 0, return NULL);
456 off = 2 + tab1->M;
457 r1 = tab1->n_redundant;
458 r2 = tab2->n_redundant;
459 d1 = tab1->n_dead;
460 d2 = tab2->n_dead;
461 prod = isl_calloc_type(tab1->mat->ctx, struct isl_tab);
462 if (!prod)
463 return NULL;
464 prod->mat = tab_mat_product(tab1->mat, tab2->mat,
465 tab1->n_row, tab2->n_row,
466 tab1->n_col, tab2->n_col, off, r1, r2, d1, d2);
467 if (!prod->mat)
468 goto error;
469 prod->var = isl_alloc_array(tab1->mat->ctx, struct isl_tab_var,
470 tab1->max_var + tab2->max_var);
471 if ((tab1->max_var + tab2->max_var) && !prod->var)
472 goto error;
473 for (i = 0; i < tab1->n_var; ++i) {
474 prod->var[i] = tab1->var[i];
475 update_index1(&prod->var[i], r1, r2, d1, d2);
477 for (i = 0; i < tab2->n_var; ++i) {
478 prod->var[tab1->n_var + i] = tab2->var[i];
479 update_index2(&prod->var[tab1->n_var + i],
480 tab1->n_row, tab1->n_col,
481 r1, r2, d1, d2);
483 prod->con = isl_alloc_array(tab1->mat->ctx, struct isl_tab_var,
484 tab1->max_con + tab2->max_con);
485 if ((tab1->max_con + tab2->max_con) && !prod->con)
486 goto error;
487 for (i = 0; i < tab1->n_con; ++i) {
488 prod->con[i] = tab1->con[i];
489 update_index1(&prod->con[i], r1, r2, d1, d2);
491 for (i = 0; i < tab2->n_con; ++i) {
492 prod->con[tab1->n_con + i] = tab2->con[i];
493 update_index2(&prod->con[tab1->n_con + i],
494 tab1->n_row, tab1->n_col,
495 r1, r2, d1, d2);
497 prod->col_var = isl_alloc_array(tab1->mat->ctx, int,
498 tab1->n_col + tab2->n_col);
499 if ((tab1->n_col + tab2->n_col) && !prod->col_var)
500 goto error;
501 for (i = 0; i < tab1->n_col; ++i) {
502 int pos = i < d1 ? i : i + d2;
503 prod->col_var[pos] = tab1->col_var[i];
505 for (i = 0; i < tab2->n_col; ++i) {
506 int pos = i < d2 ? d1 + i : tab1->n_col + i;
507 int t = tab2->col_var[i];
508 if (t >= 0)
509 t += tab1->n_var;
510 else
511 t -= tab1->n_con;
512 prod->col_var[pos] = t;
514 prod->row_var = isl_alloc_array(tab1->mat->ctx, int,
515 tab1->mat->n_row + tab2->mat->n_row);
516 if ((tab1->mat->n_row + tab2->mat->n_row) && !prod->row_var)
517 goto error;
518 for (i = 0; i < tab1->n_row; ++i) {
519 int pos = i < r1 ? i : i + r2;
520 prod->row_var[pos] = tab1->row_var[i];
522 for (i = 0; i < tab2->n_row; ++i) {
523 int pos = i < r2 ? r1 + i : tab1->n_row + i;
524 int t = tab2->row_var[i];
525 if (t >= 0)
526 t += tab1->n_var;
527 else
528 t -= tab1->n_con;
529 prod->row_var[pos] = t;
531 prod->samples = NULL;
532 prod->sample_index = NULL;
533 prod->n_row = tab1->n_row + tab2->n_row;
534 prod->n_con = tab1->n_con + tab2->n_con;
535 prod->n_eq = 0;
536 prod->max_con = tab1->max_con + tab2->max_con;
537 prod->n_col = tab1->n_col + tab2->n_col;
538 prod->n_var = tab1->n_var + tab2->n_var;
539 prod->max_var = tab1->max_var + tab2->max_var;
540 prod->n_param = 0;
541 prod->n_div = 0;
542 prod->n_dead = tab1->n_dead + tab2->n_dead;
543 prod->n_redundant = tab1->n_redundant + tab2->n_redundant;
544 prod->rational = tab1->rational;
545 prod->empty = tab1->empty || tab2->empty;
546 prod->strict_redundant = tab1->strict_redundant || tab2->strict_redundant;
547 prod->need_undo = 0;
548 prod->in_undo = 0;
549 prod->M = tab1->M;
550 prod->cone = tab1->cone;
551 prod->bottom.type = isl_tab_undo_bottom;
552 prod->bottom.next = NULL;
553 prod->top = &prod->bottom;
555 prod->n_zero = 0;
556 prod->n_unbounded = 0;
557 prod->basis = NULL;
559 return prod;
560 error:
561 isl_tab_free(prod);
562 return NULL;
565 static struct isl_tab_var *var_from_index(struct isl_tab *tab, int i)
567 if (i >= 0)
568 return &tab->var[i];
569 else
570 return &tab->con[~i];
573 struct isl_tab_var *isl_tab_var_from_row(struct isl_tab *tab, int i)
575 return var_from_index(tab, tab->row_var[i]);
578 static struct isl_tab_var *var_from_col(struct isl_tab *tab, int i)
580 return var_from_index(tab, tab->col_var[i]);
583 /* Check if there are any upper bounds on column variable "var",
584 * i.e., non-negative rows where var appears with a negative coefficient.
585 * Return 1 if there are no such bounds.
587 static int max_is_manifestly_unbounded(struct isl_tab *tab,
588 struct isl_tab_var *var)
590 int i;
591 unsigned off = 2 + tab->M;
593 if (var->is_row)
594 return 0;
595 for (i = tab->n_redundant; i < tab->n_row; ++i) {
596 if (!isl_int_is_neg(tab->mat->row[i][off + var->index]))
597 continue;
598 if (isl_tab_var_from_row(tab, i)->is_nonneg)
599 return 0;
601 return 1;
604 /* Check if there are any lower bounds on column variable "var",
605 * i.e., non-negative rows where var appears with a positive coefficient.
606 * Return 1 if there are no such bounds.
608 static int min_is_manifestly_unbounded(struct isl_tab *tab,
609 struct isl_tab_var *var)
611 int i;
612 unsigned off = 2 + tab->M;
614 if (var->is_row)
615 return 0;
616 for (i = tab->n_redundant; i < tab->n_row; ++i) {
617 if (!isl_int_is_pos(tab->mat->row[i][off + var->index]))
618 continue;
619 if (isl_tab_var_from_row(tab, i)->is_nonneg)
620 return 0;
622 return 1;
625 static int row_cmp(struct isl_tab *tab, int r1, int r2, int c, isl_int t)
627 unsigned off = 2 + tab->M;
629 if (tab->M) {
630 int s;
631 isl_int_mul(t, tab->mat->row[r1][2], tab->mat->row[r2][off+c]);
632 isl_int_submul(t, tab->mat->row[r2][2], tab->mat->row[r1][off+c]);
633 s = isl_int_sgn(t);
634 if (s)
635 return s;
637 isl_int_mul(t, tab->mat->row[r1][1], tab->mat->row[r2][off + c]);
638 isl_int_submul(t, tab->mat->row[r2][1], tab->mat->row[r1][off + c]);
639 return isl_int_sgn(t);
642 /* Given the index of a column "c", return the index of a row
643 * that can be used to pivot the column in, with either an increase
644 * (sgn > 0) or a decrease (sgn < 0) of the corresponding variable.
645 * If "var" is not NULL, then the row returned will be different from
646 * the one associated with "var".
648 * Each row in the tableau is of the form
650 * x_r = a_r0 + \sum_i a_ri x_i
652 * Only rows with x_r >= 0 and with the sign of a_ri opposite to "sgn"
653 * impose any limit on the increase or decrease in the value of x_c
654 * and this bound is equal to a_r0 / |a_rc|. We are therefore looking
655 * for the row with the smallest (most stringent) such bound.
656 * Note that the common denominator of each row drops out of the fraction.
657 * To check if row j has a smaller bound than row r, i.e.,
658 * a_j0 / |a_jc| < a_r0 / |a_rc| or a_j0 |a_rc| < a_r0 |a_jc|,
659 * we check if -sign(a_jc) (a_j0 a_rc - a_r0 a_jc) < 0,
660 * where -sign(a_jc) is equal to "sgn".
662 static int pivot_row(struct isl_tab *tab,
663 struct isl_tab_var *var, int sgn, int c)
665 int j, r, tsgn;
666 isl_int t;
667 unsigned off = 2 + tab->M;
669 isl_int_init(t);
670 r = -1;
671 for (j = tab->n_redundant; j < tab->n_row; ++j) {
672 if (var && j == var->index)
673 continue;
674 if (!isl_tab_var_from_row(tab, j)->is_nonneg)
675 continue;
676 if (sgn * isl_int_sgn(tab->mat->row[j][off + c]) >= 0)
677 continue;
678 if (r < 0) {
679 r = j;
680 continue;
682 tsgn = sgn * row_cmp(tab, r, j, c, t);
683 if (tsgn < 0 || (tsgn == 0 &&
684 tab->row_var[j] < tab->row_var[r]))
685 r = j;
687 isl_int_clear(t);
688 return r;
691 /* Find a pivot (row and col) that will increase (sgn > 0) or decrease
692 * (sgn < 0) the value of row variable var.
693 * If not NULL, then skip_var is a row variable that should be ignored
694 * while looking for a pivot row. It is usually equal to var.
696 * As the given row in the tableau is of the form
698 * x_r = a_r0 + \sum_i a_ri x_i
700 * we need to find a column such that the sign of a_ri is equal to "sgn"
701 * (such that an increase in x_i will have the desired effect) or a
702 * column with a variable that may attain negative values.
703 * If a_ri is positive, then we need to move x_i in the same direction
704 * to obtain the desired effect. Otherwise, x_i has to move in the
705 * opposite direction.
707 static void find_pivot(struct isl_tab *tab,
708 struct isl_tab_var *var, struct isl_tab_var *skip_var,
709 int sgn, int *row, int *col)
711 int j, r, c;
712 isl_int *tr;
714 *row = *col = -1;
716 isl_assert(tab->mat->ctx, var->is_row, return);
717 tr = tab->mat->row[var->index] + 2 + tab->M;
719 c = -1;
720 for (j = tab->n_dead; j < tab->n_col; ++j) {
721 if (isl_int_is_zero(tr[j]))
722 continue;
723 if (isl_int_sgn(tr[j]) != sgn &&
724 var_from_col(tab, j)->is_nonneg)
725 continue;
726 if (c < 0 || tab->col_var[j] < tab->col_var[c])
727 c = j;
729 if (c < 0)
730 return;
732 sgn *= isl_int_sgn(tr[c]);
733 r = pivot_row(tab, skip_var, sgn, c);
734 *row = r < 0 ? var->index : r;
735 *col = c;
738 /* Return 1 if row "row" represents an obviously redundant inequality.
739 * This means
740 * - it represents an inequality or a variable
741 * - that is the sum of a non-negative sample value and a positive
742 * combination of zero or more non-negative constraints.
744 int isl_tab_row_is_redundant(struct isl_tab *tab, int row)
746 int i;
747 unsigned off = 2 + tab->M;
749 if (tab->row_var[row] < 0 && !isl_tab_var_from_row(tab, row)->is_nonneg)
750 return 0;
752 if (isl_int_is_neg(tab->mat->row[row][1]))
753 return 0;
754 if (tab->strict_redundant && isl_int_is_zero(tab->mat->row[row][1]))
755 return 0;
756 if (tab->M && isl_int_is_neg(tab->mat->row[row][2]))
757 return 0;
759 for (i = tab->n_dead; i < tab->n_col; ++i) {
760 if (isl_int_is_zero(tab->mat->row[row][off + i]))
761 continue;
762 if (tab->col_var[i] >= 0)
763 return 0;
764 if (isl_int_is_neg(tab->mat->row[row][off + i]))
765 return 0;
766 if (!var_from_col(tab, i)->is_nonneg)
767 return 0;
769 return 1;
772 static void swap_rows(struct isl_tab *tab, int row1, int row2)
774 int t;
775 enum isl_tab_row_sign s;
777 t = tab->row_var[row1];
778 tab->row_var[row1] = tab->row_var[row2];
779 tab->row_var[row2] = t;
780 isl_tab_var_from_row(tab, row1)->index = row1;
781 isl_tab_var_from_row(tab, row2)->index = row2;
782 tab->mat = isl_mat_swap_rows(tab->mat, row1, row2);
784 if (!tab->row_sign)
785 return;
786 s = tab->row_sign[row1];
787 tab->row_sign[row1] = tab->row_sign[row2];
788 tab->row_sign[row2] = s;
791 static int push_union(struct isl_tab *tab,
792 enum isl_tab_undo_type type, union isl_tab_undo_val u) WARN_UNUSED;
793 static int push_union(struct isl_tab *tab,
794 enum isl_tab_undo_type type, union isl_tab_undo_val u)
796 struct isl_tab_undo *undo;
798 if (!tab)
799 return -1;
800 if (!tab->need_undo)
801 return 0;
803 undo = isl_alloc_type(tab->mat->ctx, struct isl_tab_undo);
804 if (!undo)
805 return -1;
806 undo->type = type;
807 undo->u = u;
808 undo->next = tab->top;
809 tab->top = undo;
811 return 0;
814 int isl_tab_push_var(struct isl_tab *tab,
815 enum isl_tab_undo_type type, struct isl_tab_var *var)
817 union isl_tab_undo_val u;
818 if (var->is_row)
819 u.var_index = tab->row_var[var->index];
820 else
821 u.var_index = tab->col_var[var->index];
822 return push_union(tab, type, u);
825 int isl_tab_push(struct isl_tab *tab, enum isl_tab_undo_type type)
827 union isl_tab_undo_val u = { 0 };
828 return push_union(tab, type, u);
831 /* Push a record on the undo stack describing the current basic
832 * variables, so that the this state can be restored during rollback.
834 int isl_tab_push_basis(struct isl_tab *tab)
836 int i;
837 union isl_tab_undo_val u;
839 u.col_var = isl_alloc_array(tab->mat->ctx, int, tab->n_col);
840 if (tab->n_col && !u.col_var)
841 return -1;
842 for (i = 0; i < tab->n_col; ++i)
843 u.col_var[i] = tab->col_var[i];
844 return push_union(tab, isl_tab_undo_saved_basis, u);
847 int isl_tab_push_callback(struct isl_tab *tab, struct isl_tab_callback *callback)
849 union isl_tab_undo_val u;
850 u.callback = callback;
851 return push_union(tab, isl_tab_undo_callback, u);
854 struct isl_tab *isl_tab_init_samples(struct isl_tab *tab)
856 if (!tab)
857 return NULL;
859 tab->n_sample = 0;
860 tab->n_outside = 0;
861 tab->samples = isl_mat_alloc(tab->mat->ctx, 1, 1 + tab->n_var);
862 if (!tab->samples)
863 goto error;
864 tab->sample_index = isl_alloc_array(tab->mat->ctx, int, 1);
865 if (!tab->sample_index)
866 goto error;
867 return tab;
868 error:
869 isl_tab_free(tab);
870 return NULL;
873 int isl_tab_add_sample(struct isl_tab *tab, __isl_take isl_vec *sample)
875 if (!tab || !sample)
876 goto error;
878 if (tab->n_sample + 1 > tab->samples->n_row) {
879 int *t = isl_realloc_array(tab->mat->ctx,
880 tab->sample_index, int, tab->n_sample + 1);
881 if (!t)
882 goto error;
883 tab->sample_index = t;
886 tab->samples = isl_mat_extend(tab->samples,
887 tab->n_sample + 1, tab->samples->n_col);
888 if (!tab->samples)
889 goto error;
891 isl_seq_cpy(tab->samples->row[tab->n_sample], sample->el, sample->size);
892 isl_vec_free(sample);
893 tab->sample_index[tab->n_sample] = tab->n_sample;
894 tab->n_sample++;
896 return 0;
897 error:
898 isl_vec_free(sample);
899 return -1;
902 struct isl_tab *isl_tab_drop_sample(struct isl_tab *tab, int s)
904 if (s != tab->n_outside) {
905 int t = tab->sample_index[tab->n_outside];
906 tab->sample_index[tab->n_outside] = tab->sample_index[s];
907 tab->sample_index[s] = t;
908 isl_mat_swap_rows(tab->samples, tab->n_outside, s);
910 tab->n_outside++;
911 if (isl_tab_push(tab, isl_tab_undo_drop_sample) < 0) {
912 isl_tab_free(tab);
913 return NULL;
916 return tab;
919 /* Record the current number of samples so that we can remove newer
920 * samples during a rollback.
922 int isl_tab_save_samples(struct isl_tab *tab)
924 union isl_tab_undo_val u;
926 if (!tab)
927 return -1;
929 u.n = tab->n_sample;
930 return push_union(tab, isl_tab_undo_saved_samples, u);
933 /* Mark row with index "row" as being redundant.
934 * If we may need to undo the operation or if the row represents
935 * a variable of the original problem, the row is kept,
936 * but no longer considered when looking for a pivot row.
937 * Otherwise, the row is simply removed.
939 * The row may be interchanged with some other row. If it
940 * is interchanged with a later row, return 1. Otherwise return 0.
941 * If the rows are checked in order in the calling function,
942 * then a return value of 1 means that the row with the given
943 * row number may now contain a different row that hasn't been checked yet.
945 int isl_tab_mark_redundant(struct isl_tab *tab, int row)
947 struct isl_tab_var *var = isl_tab_var_from_row(tab, row);
948 var->is_redundant = 1;
949 isl_assert(tab->mat->ctx, row >= tab->n_redundant, return -1);
950 if (tab->preserve || tab->need_undo || tab->row_var[row] >= 0) {
951 if (tab->row_var[row] >= 0 && !var->is_nonneg) {
952 var->is_nonneg = 1;
953 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, var) < 0)
954 return -1;
956 if (row != tab->n_redundant)
957 swap_rows(tab, row, tab->n_redundant);
958 tab->n_redundant++;
959 return isl_tab_push_var(tab, isl_tab_undo_redundant, var);
960 } else {
961 if (row != tab->n_row - 1)
962 swap_rows(tab, row, tab->n_row - 1);
963 isl_tab_var_from_row(tab, tab->n_row - 1)->index = -1;
964 tab->n_row--;
965 return 1;
969 int isl_tab_mark_empty(struct isl_tab *tab)
971 if (!tab)
972 return -1;
973 if (!tab->empty && tab->need_undo)
974 if (isl_tab_push(tab, isl_tab_undo_empty) < 0)
975 return -1;
976 tab->empty = 1;
977 return 0;
980 int isl_tab_freeze_constraint(struct isl_tab *tab, int con)
982 struct isl_tab_var *var;
984 if (!tab)
985 return -1;
987 var = &tab->con[con];
988 if (var->frozen)
989 return 0;
990 if (var->index < 0)
991 return 0;
992 var->frozen = 1;
994 if (tab->need_undo)
995 return isl_tab_push_var(tab, isl_tab_undo_freeze, var);
997 return 0;
1000 /* Update the rows signs after a pivot of "row" and "col", with "row_sgn"
1001 * the original sign of the pivot element.
1002 * We only keep track of row signs during PILP solving and in this case
1003 * we only pivot a row with negative sign (meaning the value is always
1004 * non-positive) using a positive pivot element.
1006 * For each row j, the new value of the parametric constant is equal to
1008 * a_j0 - a_jc a_r0/a_rc
1010 * where a_j0 is the original parametric constant, a_rc is the pivot element,
1011 * a_r0 is the parametric constant of the pivot row and a_jc is the
1012 * pivot column entry of the row j.
1013 * Since a_r0 is non-positive and a_rc is positive, the sign of row j
1014 * remains the same if a_jc has the same sign as the row j or if
1015 * a_jc is zero. In all other cases, we reset the sign to "unknown".
1017 static void update_row_sign(struct isl_tab *tab, int row, int col, int row_sgn)
1019 int i;
1020 struct isl_mat *mat = tab->mat;
1021 unsigned off = 2 + tab->M;
1023 if (!tab->row_sign)
1024 return;
1026 if (tab->row_sign[row] == 0)
1027 return;
1028 isl_assert(mat->ctx, row_sgn > 0, return);
1029 isl_assert(mat->ctx, tab->row_sign[row] == isl_tab_row_neg, return);
1030 tab->row_sign[row] = isl_tab_row_pos;
1031 for (i = 0; i < tab->n_row; ++i) {
1032 int s;
1033 if (i == row)
1034 continue;
1035 s = isl_int_sgn(mat->row[i][off + col]);
1036 if (!s)
1037 continue;
1038 if (!tab->row_sign[i])
1039 continue;
1040 if (s < 0 && tab->row_sign[i] == isl_tab_row_neg)
1041 continue;
1042 if (s > 0 && tab->row_sign[i] == isl_tab_row_pos)
1043 continue;
1044 tab->row_sign[i] = isl_tab_row_unknown;
1048 /* Given a row number "row" and a column number "col", pivot the tableau
1049 * such that the associated variables are interchanged.
1050 * The given row in the tableau expresses
1052 * x_r = a_r0 + \sum_i a_ri x_i
1054 * or
1056 * x_c = 1/a_rc x_r - a_r0/a_rc + sum_{i \ne r} -a_ri/a_rc
1058 * Substituting this equality into the other rows
1060 * x_j = a_j0 + \sum_i a_ji x_i
1062 * with a_jc \ne 0, we obtain
1064 * 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
1066 * The tableau
1068 * n_rc/d_r n_ri/d_r
1069 * n_jc/d_j n_ji/d_j
1071 * where i is any other column and j is any other row,
1072 * is therefore transformed into
1074 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1075 * 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)
1077 * The transformation is performed along the following steps
1079 * d_r/n_rc n_ri/n_rc
1080 * n_jc/d_j n_ji/d_j
1082 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1083 * n_jc/d_j n_ji/d_j
1085 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1086 * n_jc/(|n_rc| d_j) n_ji/(|n_rc| d_j)
1088 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1089 * n_jc/(|n_rc| d_j) (n_ji |n_rc|)/(|n_rc| d_j)
1091 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1092 * n_jc/(|n_rc| d_j) (n_ji |n_rc| - s(n_rc)n_jc n_ri)/(|n_rc| d_j)
1094 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1095 * 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)
1098 int isl_tab_pivot(struct isl_tab *tab, int row, int col)
1100 int i, j;
1101 int sgn;
1102 int t;
1103 isl_ctx *ctx;
1104 struct isl_mat *mat = tab->mat;
1105 struct isl_tab_var *var;
1106 unsigned off = 2 + tab->M;
1108 ctx = isl_tab_get_ctx(tab);
1109 if (isl_ctx_next_operation(ctx) < 0)
1110 return -1;
1112 isl_int_swap(mat->row[row][0], mat->row[row][off + col]);
1113 sgn = isl_int_sgn(mat->row[row][0]);
1114 if (sgn < 0) {
1115 isl_int_neg(mat->row[row][0], mat->row[row][0]);
1116 isl_int_neg(mat->row[row][off + col], mat->row[row][off + col]);
1117 } else
1118 for (j = 0; j < off - 1 + tab->n_col; ++j) {
1119 if (j == off - 1 + col)
1120 continue;
1121 isl_int_neg(mat->row[row][1 + j], mat->row[row][1 + j]);
1123 if (!isl_int_is_one(mat->row[row][0]))
1124 isl_seq_normalize(mat->ctx, mat->row[row], off + tab->n_col);
1125 for (i = 0; i < tab->n_row; ++i) {
1126 if (i == row)
1127 continue;
1128 if (isl_int_is_zero(mat->row[i][off + col]))
1129 continue;
1130 isl_int_mul(mat->row[i][0], mat->row[i][0], mat->row[row][0]);
1131 for (j = 0; j < off - 1 + tab->n_col; ++j) {
1132 if (j == off - 1 + col)
1133 continue;
1134 isl_int_mul(mat->row[i][1 + j],
1135 mat->row[i][1 + j], mat->row[row][0]);
1136 isl_int_addmul(mat->row[i][1 + j],
1137 mat->row[i][off + col], mat->row[row][1 + j]);
1139 isl_int_mul(mat->row[i][off + col],
1140 mat->row[i][off + col], mat->row[row][off + col]);
1141 if (!isl_int_is_one(mat->row[i][0]))
1142 isl_seq_normalize(mat->ctx, mat->row[i], off + tab->n_col);
1144 t = tab->row_var[row];
1145 tab->row_var[row] = tab->col_var[col];
1146 tab->col_var[col] = t;
1147 var = isl_tab_var_from_row(tab, row);
1148 var->is_row = 1;
1149 var->index = row;
1150 var = var_from_col(tab, col);
1151 var->is_row = 0;
1152 var->index = col;
1153 update_row_sign(tab, row, col, sgn);
1154 if (tab->in_undo)
1155 return 0;
1156 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1157 if (isl_int_is_zero(mat->row[i][off + col]))
1158 continue;
1159 if (!isl_tab_var_from_row(tab, i)->frozen &&
1160 isl_tab_row_is_redundant(tab, i)) {
1161 int redo = isl_tab_mark_redundant(tab, i);
1162 if (redo < 0)
1163 return -1;
1164 if (redo)
1165 --i;
1168 return 0;
1171 /* If "var" represents a column variable, then pivot is up (sgn > 0)
1172 * or down (sgn < 0) to a row. The variable is assumed not to be
1173 * unbounded in the specified direction.
1174 * If sgn = 0, then the variable is unbounded in both directions,
1175 * and we pivot with any row we can find.
1177 static int to_row(struct isl_tab *tab, struct isl_tab_var *var, int sign) WARN_UNUSED;
1178 static int to_row(struct isl_tab *tab, struct isl_tab_var *var, int sign)
1180 int r;
1181 unsigned off = 2 + tab->M;
1183 if (var->is_row)
1184 return 0;
1186 if (sign == 0) {
1187 for (r = tab->n_redundant; r < tab->n_row; ++r)
1188 if (!isl_int_is_zero(tab->mat->row[r][off+var->index]))
1189 break;
1190 isl_assert(tab->mat->ctx, r < tab->n_row, return -1);
1191 } else {
1192 r = pivot_row(tab, NULL, sign, var->index);
1193 isl_assert(tab->mat->ctx, r >= 0, return -1);
1196 return isl_tab_pivot(tab, r, var->index);
1199 /* Check whether all variables that are marked as non-negative
1200 * also have a non-negative sample value. This function is not
1201 * called from the current code but is useful during debugging.
1203 static void check_table(struct isl_tab *tab) __attribute__ ((unused));
1204 static void check_table(struct isl_tab *tab)
1206 int i;
1208 if (tab->empty)
1209 return;
1210 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1211 struct isl_tab_var *var;
1212 var = isl_tab_var_from_row(tab, i);
1213 if (!var->is_nonneg)
1214 continue;
1215 if (tab->M) {
1216 isl_assert(tab->mat->ctx,
1217 !isl_int_is_neg(tab->mat->row[i][2]), abort());
1218 if (isl_int_is_pos(tab->mat->row[i][2]))
1219 continue;
1221 isl_assert(tab->mat->ctx, !isl_int_is_neg(tab->mat->row[i][1]),
1222 abort());
1226 /* Return the sign of the maximal value of "var".
1227 * If the sign is not negative, then on return from this function,
1228 * the sample value will also be non-negative.
1230 * If "var" is manifestly unbounded wrt positive values, we are done.
1231 * Otherwise, we pivot the variable up to a row if needed
1232 * Then we continue pivoting down until either
1233 * - no more down pivots can be performed
1234 * - the sample value is positive
1235 * - the variable is pivoted into a manifestly unbounded column
1237 static int sign_of_max(struct isl_tab *tab, struct isl_tab_var *var)
1239 int row, col;
1241 if (max_is_manifestly_unbounded(tab, var))
1242 return 1;
1243 if (to_row(tab, var, 1) < 0)
1244 return -2;
1245 while (!isl_int_is_pos(tab->mat->row[var->index][1])) {
1246 find_pivot(tab, var, var, 1, &row, &col);
1247 if (row == -1)
1248 return isl_int_sgn(tab->mat->row[var->index][1]);
1249 if (isl_tab_pivot(tab, row, col) < 0)
1250 return -2;
1251 if (!var->is_row) /* manifestly unbounded */
1252 return 1;
1254 return 1;
1257 int isl_tab_sign_of_max(struct isl_tab *tab, int con)
1259 struct isl_tab_var *var;
1261 if (!tab)
1262 return -2;
1264 var = &tab->con[con];
1265 isl_assert(tab->mat->ctx, !var->is_redundant, return -2);
1266 isl_assert(tab->mat->ctx, !var->is_zero, return -2);
1268 return sign_of_max(tab, var);
1271 static int row_is_neg(struct isl_tab *tab, int row)
1273 if (!tab->M)
1274 return isl_int_is_neg(tab->mat->row[row][1]);
1275 if (isl_int_is_pos(tab->mat->row[row][2]))
1276 return 0;
1277 if (isl_int_is_neg(tab->mat->row[row][2]))
1278 return 1;
1279 return isl_int_is_neg(tab->mat->row[row][1]);
1282 static int row_sgn(struct isl_tab *tab, int row)
1284 if (!tab->M)
1285 return isl_int_sgn(tab->mat->row[row][1]);
1286 if (!isl_int_is_zero(tab->mat->row[row][2]))
1287 return isl_int_sgn(tab->mat->row[row][2]);
1288 else
1289 return isl_int_sgn(tab->mat->row[row][1]);
1292 /* Perform pivots until the row variable "var" has a non-negative
1293 * sample value or until no more upward pivots can be performed.
1294 * Return the sign of the sample value after the pivots have been
1295 * performed.
1297 static int restore_row(struct isl_tab *tab, struct isl_tab_var *var)
1299 int row, col;
1301 while (row_is_neg(tab, var->index)) {
1302 find_pivot(tab, var, var, 1, &row, &col);
1303 if (row == -1)
1304 break;
1305 if (isl_tab_pivot(tab, row, col) < 0)
1306 return -2;
1307 if (!var->is_row) /* manifestly unbounded */
1308 return 1;
1310 return row_sgn(tab, var->index);
1313 /* Perform pivots until we are sure that the row variable "var"
1314 * can attain non-negative values. After return from this
1315 * function, "var" is still a row variable, but its sample
1316 * value may not be non-negative, even if the function returns 1.
1318 static int at_least_zero(struct isl_tab *tab, struct isl_tab_var *var)
1320 int row, col;
1322 while (isl_int_is_neg(tab->mat->row[var->index][1])) {
1323 find_pivot(tab, var, var, 1, &row, &col);
1324 if (row == -1)
1325 break;
1326 if (row == var->index) /* manifestly unbounded */
1327 return 1;
1328 if (isl_tab_pivot(tab, row, col) < 0)
1329 return -1;
1331 return !isl_int_is_neg(tab->mat->row[var->index][1]);
1334 /* Return a negative value if "var" can attain negative values.
1335 * Return a non-negative value otherwise.
1337 * If "var" is manifestly unbounded wrt negative values, we are done.
1338 * Otherwise, if var is in a column, we can pivot it down to a row.
1339 * Then we continue pivoting down until either
1340 * - the pivot would result in a manifestly unbounded column
1341 * => we don't perform the pivot, but simply return -1
1342 * - no more down pivots can be performed
1343 * - the sample value is negative
1344 * If the sample value becomes negative and the variable is supposed
1345 * to be nonnegative, then we undo the last pivot.
1346 * However, if the last pivot has made the pivoting variable
1347 * obviously redundant, then it may have moved to another row.
1348 * In that case we look for upward pivots until we reach a non-negative
1349 * value again.
1351 static int sign_of_min(struct isl_tab *tab, struct isl_tab_var *var)
1353 int row, col;
1354 struct isl_tab_var *pivot_var = NULL;
1356 if (min_is_manifestly_unbounded(tab, var))
1357 return -1;
1358 if (!var->is_row) {
1359 col = var->index;
1360 row = pivot_row(tab, NULL, -1, col);
1361 pivot_var = var_from_col(tab, col);
1362 if (isl_tab_pivot(tab, row, col) < 0)
1363 return -2;
1364 if (var->is_redundant)
1365 return 0;
1366 if (isl_int_is_neg(tab->mat->row[var->index][1])) {
1367 if (var->is_nonneg) {
1368 if (!pivot_var->is_redundant &&
1369 pivot_var->index == row) {
1370 if (isl_tab_pivot(tab, row, col) < 0)
1371 return -2;
1372 } else
1373 if (restore_row(tab, var) < -1)
1374 return -2;
1376 return -1;
1379 if (var->is_redundant)
1380 return 0;
1381 while (!isl_int_is_neg(tab->mat->row[var->index][1])) {
1382 find_pivot(tab, var, var, -1, &row, &col);
1383 if (row == var->index)
1384 return -1;
1385 if (row == -1)
1386 return isl_int_sgn(tab->mat->row[var->index][1]);
1387 pivot_var = var_from_col(tab, col);
1388 if (isl_tab_pivot(tab, row, col) < 0)
1389 return -2;
1390 if (var->is_redundant)
1391 return 0;
1393 if (pivot_var && var->is_nonneg) {
1394 /* pivot back to non-negative value */
1395 if (!pivot_var->is_redundant && pivot_var->index == row) {
1396 if (isl_tab_pivot(tab, row, col) < 0)
1397 return -2;
1398 } else
1399 if (restore_row(tab, var) < -1)
1400 return -2;
1402 return -1;
1405 static int row_at_most_neg_one(struct isl_tab *tab, int row)
1407 if (tab->M) {
1408 if (isl_int_is_pos(tab->mat->row[row][2]))
1409 return 0;
1410 if (isl_int_is_neg(tab->mat->row[row][2]))
1411 return 1;
1413 return isl_int_is_neg(tab->mat->row[row][1]) &&
1414 isl_int_abs_ge(tab->mat->row[row][1],
1415 tab->mat->row[row][0]);
1418 /* Return 1 if "var" can attain values <= -1.
1419 * Return 0 otherwise.
1421 * The sample value of "var" is assumed to be non-negative when the
1422 * the function is called. If 1 is returned then the constraint
1423 * is not redundant and the sample value is made non-negative again before
1424 * the function returns.
1426 int isl_tab_min_at_most_neg_one(struct isl_tab *tab, struct isl_tab_var *var)
1428 int row, col;
1429 struct isl_tab_var *pivot_var;
1431 if (min_is_manifestly_unbounded(tab, var))
1432 return 1;
1433 if (!var->is_row) {
1434 col = var->index;
1435 row = pivot_row(tab, NULL, -1, col);
1436 pivot_var = var_from_col(tab, col);
1437 if (isl_tab_pivot(tab, row, col) < 0)
1438 return -1;
1439 if (var->is_redundant)
1440 return 0;
1441 if (row_at_most_neg_one(tab, var->index)) {
1442 if (var->is_nonneg) {
1443 if (!pivot_var->is_redundant &&
1444 pivot_var->index == row) {
1445 if (isl_tab_pivot(tab, row, col) < 0)
1446 return -1;
1447 } else
1448 if (restore_row(tab, var) < -1)
1449 return -1;
1451 return 1;
1454 if (var->is_redundant)
1455 return 0;
1456 do {
1457 find_pivot(tab, var, var, -1, &row, &col);
1458 if (row == var->index) {
1459 if (restore_row(tab, var) < -1)
1460 return -1;
1461 return 1;
1463 if (row == -1)
1464 return 0;
1465 pivot_var = var_from_col(tab, col);
1466 if (isl_tab_pivot(tab, row, col) < 0)
1467 return -1;
1468 if (var->is_redundant)
1469 return 0;
1470 } while (!row_at_most_neg_one(tab, var->index));
1471 if (var->is_nonneg) {
1472 /* pivot back to non-negative value */
1473 if (!pivot_var->is_redundant && pivot_var->index == row)
1474 if (isl_tab_pivot(tab, row, col) < 0)
1475 return -1;
1476 if (restore_row(tab, var) < -1)
1477 return -1;
1479 return 1;
1482 /* Return 1 if "var" can attain values >= 1.
1483 * Return 0 otherwise.
1485 static int at_least_one(struct isl_tab *tab, struct isl_tab_var *var)
1487 int row, col;
1488 isl_int *r;
1490 if (max_is_manifestly_unbounded(tab, var))
1491 return 1;
1492 if (to_row(tab, var, 1) < 0)
1493 return -1;
1494 r = tab->mat->row[var->index];
1495 while (isl_int_lt(r[1], r[0])) {
1496 find_pivot(tab, var, var, 1, &row, &col);
1497 if (row == -1)
1498 return isl_int_ge(r[1], r[0]);
1499 if (row == var->index) /* manifestly unbounded */
1500 return 1;
1501 if (isl_tab_pivot(tab, row, col) < 0)
1502 return -1;
1504 return 1;
1507 static void swap_cols(struct isl_tab *tab, int col1, int col2)
1509 int t;
1510 unsigned off = 2 + tab->M;
1511 t = tab->col_var[col1];
1512 tab->col_var[col1] = tab->col_var[col2];
1513 tab->col_var[col2] = t;
1514 var_from_col(tab, col1)->index = col1;
1515 var_from_col(tab, col2)->index = col2;
1516 tab->mat = isl_mat_swap_cols(tab->mat, off + col1, off + col2);
1519 /* Mark column with index "col" as representing a zero variable.
1520 * If we may need to undo the operation the column is kept,
1521 * but no longer considered.
1522 * Otherwise, the column is simply removed.
1524 * The column may be interchanged with some other column. If it
1525 * is interchanged with a later column, return 1. Otherwise return 0.
1526 * If the columns are checked in order in the calling function,
1527 * then a return value of 1 means that the column with the given
1528 * column number may now contain a different column that
1529 * hasn't been checked yet.
1531 int isl_tab_kill_col(struct isl_tab *tab, int col)
1533 var_from_col(tab, col)->is_zero = 1;
1534 if (tab->need_undo) {
1535 if (isl_tab_push_var(tab, isl_tab_undo_zero,
1536 var_from_col(tab, col)) < 0)
1537 return -1;
1538 if (col != tab->n_dead)
1539 swap_cols(tab, col, tab->n_dead);
1540 tab->n_dead++;
1541 return 0;
1542 } else {
1543 if (col != tab->n_col - 1)
1544 swap_cols(tab, col, tab->n_col - 1);
1545 var_from_col(tab, tab->n_col - 1)->index = -1;
1546 tab->n_col--;
1547 return 1;
1551 static int row_is_manifestly_non_integral(struct isl_tab *tab, int row)
1553 unsigned off = 2 + tab->M;
1555 if (tab->M && !isl_int_eq(tab->mat->row[row][2],
1556 tab->mat->row[row][0]))
1557 return 0;
1558 if (isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
1559 tab->n_col - tab->n_dead) != -1)
1560 return 0;
1562 return !isl_int_is_divisible_by(tab->mat->row[row][1],
1563 tab->mat->row[row][0]);
1566 /* For integer tableaus, check if any of the coordinates are stuck
1567 * at a non-integral value.
1569 static int tab_is_manifestly_empty(struct isl_tab *tab)
1571 int i;
1573 if (tab->empty)
1574 return 1;
1575 if (tab->rational)
1576 return 0;
1578 for (i = 0; i < tab->n_var; ++i) {
1579 if (!tab->var[i].is_row)
1580 continue;
1581 if (row_is_manifestly_non_integral(tab, tab->var[i].index))
1582 return 1;
1585 return 0;
1588 /* Row variable "var" is non-negative and cannot attain any values
1589 * larger than zero. This means that the coefficients of the unrestricted
1590 * column variables are zero and that the coefficients of the non-negative
1591 * column variables are zero or negative.
1592 * Each of the non-negative variables with a negative coefficient can
1593 * then also be written as the negative sum of non-negative variables
1594 * and must therefore also be zero.
1596 static int close_row(struct isl_tab *tab, struct isl_tab_var *var) WARN_UNUSED;
1597 static int close_row(struct isl_tab *tab, struct isl_tab_var *var)
1599 int j;
1600 struct isl_mat *mat = tab->mat;
1601 unsigned off = 2 + tab->M;
1603 isl_assert(tab->mat->ctx, var->is_nonneg, return -1);
1604 var->is_zero = 1;
1605 if (tab->need_undo)
1606 if (isl_tab_push_var(tab, isl_tab_undo_zero, var) < 0)
1607 return -1;
1608 for (j = tab->n_dead; j < tab->n_col; ++j) {
1609 int recheck;
1610 if (isl_int_is_zero(mat->row[var->index][off + j]))
1611 continue;
1612 isl_assert(tab->mat->ctx,
1613 isl_int_is_neg(mat->row[var->index][off + j]), return -1);
1614 recheck = isl_tab_kill_col(tab, j);
1615 if (recheck < 0)
1616 return -1;
1617 if (recheck)
1618 --j;
1620 if (isl_tab_mark_redundant(tab, var->index) < 0)
1621 return -1;
1622 if (tab_is_manifestly_empty(tab) && isl_tab_mark_empty(tab) < 0)
1623 return -1;
1624 return 0;
1627 /* Add a constraint to the tableau and allocate a row for it.
1628 * Return the index into the constraint array "con".
1630 int isl_tab_allocate_con(struct isl_tab *tab)
1632 int r;
1634 isl_assert(tab->mat->ctx, tab->n_row < tab->mat->n_row, return -1);
1635 isl_assert(tab->mat->ctx, tab->n_con < tab->max_con, return -1);
1637 r = tab->n_con;
1638 tab->con[r].index = tab->n_row;
1639 tab->con[r].is_row = 1;
1640 tab->con[r].is_nonneg = 0;
1641 tab->con[r].is_zero = 0;
1642 tab->con[r].is_redundant = 0;
1643 tab->con[r].frozen = 0;
1644 tab->con[r].negated = 0;
1645 tab->row_var[tab->n_row] = ~r;
1647 tab->n_row++;
1648 tab->n_con++;
1649 if (isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->con[r]) < 0)
1650 return -1;
1652 return r;
1655 /* Add a variable to the tableau and allocate a column for it.
1656 * Return the index into the variable array "var".
1658 int isl_tab_allocate_var(struct isl_tab *tab)
1660 int r;
1661 int i;
1662 unsigned off = 2 + tab->M;
1664 isl_assert(tab->mat->ctx, tab->n_col < tab->mat->n_col, return -1);
1665 isl_assert(tab->mat->ctx, tab->n_var < tab->max_var, return -1);
1667 r = tab->n_var;
1668 tab->var[r].index = tab->n_col;
1669 tab->var[r].is_row = 0;
1670 tab->var[r].is_nonneg = 0;
1671 tab->var[r].is_zero = 0;
1672 tab->var[r].is_redundant = 0;
1673 tab->var[r].frozen = 0;
1674 tab->var[r].negated = 0;
1675 tab->col_var[tab->n_col] = r;
1677 for (i = 0; i < tab->n_row; ++i)
1678 isl_int_set_si(tab->mat->row[i][off + tab->n_col], 0);
1680 tab->n_var++;
1681 tab->n_col++;
1682 if (isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->var[r]) < 0)
1683 return -1;
1685 return r;
1688 /* Add a row to the tableau. The row is given as an affine combination
1689 * of the original variables and needs to be expressed in terms of the
1690 * column variables.
1692 * We add each term in turn.
1693 * If r = n/d_r is the current sum and we need to add k x, then
1694 * if x is a column variable, we increase the numerator of
1695 * this column by k d_r
1696 * if x = f/d_x is a row variable, then the new representation of r is
1698 * n k f d_x/g n + d_r/g k f m/d_r n + m/d_g k f
1699 * --- + --- = ------------------- = -------------------
1700 * d_r d_r d_r d_x/g m
1702 * with g the gcd of d_r and d_x and m the lcm of d_r and d_x.
1704 * If tab->M is set, then, internally, each variable x is represented
1705 * as x' - M. We then also need no subtract k d_r from the coefficient of M.
1707 int isl_tab_add_row(struct isl_tab *tab, isl_int *line)
1709 int i;
1710 int r;
1711 isl_int *row;
1712 isl_int a, b;
1713 unsigned off = 2 + tab->M;
1715 r = isl_tab_allocate_con(tab);
1716 if (r < 0)
1717 return -1;
1719 isl_int_init(a);
1720 isl_int_init(b);
1721 row = tab->mat->row[tab->con[r].index];
1722 isl_int_set_si(row[0], 1);
1723 isl_int_set(row[1], line[0]);
1724 isl_seq_clr(row + 2, tab->M + tab->n_col);
1725 for (i = 0; i < tab->n_var; ++i) {
1726 if (tab->var[i].is_zero)
1727 continue;
1728 if (tab->var[i].is_row) {
1729 isl_int_lcm(a,
1730 row[0], tab->mat->row[tab->var[i].index][0]);
1731 isl_int_swap(a, row[0]);
1732 isl_int_divexact(a, row[0], a);
1733 isl_int_divexact(b,
1734 row[0], tab->mat->row[tab->var[i].index][0]);
1735 isl_int_mul(b, b, line[1 + i]);
1736 isl_seq_combine(row + 1, a, row + 1,
1737 b, tab->mat->row[tab->var[i].index] + 1,
1738 1 + tab->M + tab->n_col);
1739 } else
1740 isl_int_addmul(row[off + tab->var[i].index],
1741 line[1 + i], row[0]);
1742 if (tab->M && i >= tab->n_param && i < tab->n_var - tab->n_div)
1743 isl_int_submul(row[2], line[1 + i], row[0]);
1745 isl_seq_normalize(tab->mat->ctx, row, off + tab->n_col);
1746 isl_int_clear(a);
1747 isl_int_clear(b);
1749 if (tab->row_sign)
1750 tab->row_sign[tab->con[r].index] = isl_tab_row_unknown;
1752 return r;
1755 static int drop_row(struct isl_tab *tab, int row)
1757 isl_assert(tab->mat->ctx, ~tab->row_var[row] == tab->n_con - 1, return -1);
1758 if (row != tab->n_row - 1)
1759 swap_rows(tab, row, tab->n_row - 1);
1760 tab->n_row--;
1761 tab->n_con--;
1762 return 0;
1765 static int drop_col(struct isl_tab *tab, int col)
1767 isl_assert(tab->mat->ctx, tab->col_var[col] == tab->n_var - 1, return -1);
1768 if (col != tab->n_col - 1)
1769 swap_cols(tab, col, tab->n_col - 1);
1770 tab->n_col--;
1771 tab->n_var--;
1772 return 0;
1775 /* Add inequality "ineq" and check if it conflicts with the
1776 * previously added constraints or if it is obviously redundant.
1778 int isl_tab_add_ineq(struct isl_tab *tab, isl_int *ineq)
1780 int r;
1781 int sgn;
1782 isl_int cst;
1784 if (!tab)
1785 return -1;
1786 if (tab->bmap) {
1787 struct isl_basic_map *bmap = tab->bmap;
1789 isl_assert(tab->mat->ctx, tab->n_eq == bmap->n_eq, return -1);
1790 isl_assert(tab->mat->ctx,
1791 tab->n_con == bmap->n_eq + bmap->n_ineq, return -1);
1792 tab->bmap = isl_basic_map_add_ineq(tab->bmap, ineq);
1793 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1794 return -1;
1795 if (!tab->bmap)
1796 return -1;
1798 if (tab->cone) {
1799 isl_int_init(cst);
1800 isl_int_swap(ineq[0], cst);
1802 r = isl_tab_add_row(tab, ineq);
1803 if (tab->cone) {
1804 isl_int_swap(ineq[0], cst);
1805 isl_int_clear(cst);
1807 if (r < 0)
1808 return -1;
1809 tab->con[r].is_nonneg = 1;
1810 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1811 return -1;
1812 if (isl_tab_row_is_redundant(tab, tab->con[r].index)) {
1813 if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1814 return -1;
1815 return 0;
1818 sgn = restore_row(tab, &tab->con[r]);
1819 if (sgn < -1)
1820 return -1;
1821 if (sgn < 0)
1822 return isl_tab_mark_empty(tab);
1823 if (tab->con[r].is_row && isl_tab_row_is_redundant(tab, tab->con[r].index))
1824 if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1825 return -1;
1826 return 0;
1829 /* Pivot a non-negative variable down until it reaches the value zero
1830 * and then pivot the variable into a column position.
1832 static int to_col(struct isl_tab *tab, struct isl_tab_var *var) WARN_UNUSED;
1833 static int to_col(struct isl_tab *tab, struct isl_tab_var *var)
1835 int i;
1836 int row, col;
1837 unsigned off = 2 + tab->M;
1839 if (!var->is_row)
1840 return 0;
1842 while (isl_int_is_pos(tab->mat->row[var->index][1])) {
1843 find_pivot(tab, var, NULL, -1, &row, &col);
1844 isl_assert(tab->mat->ctx, row != -1, return -1);
1845 if (isl_tab_pivot(tab, row, col) < 0)
1846 return -1;
1847 if (!var->is_row)
1848 return 0;
1851 for (i = tab->n_dead; i < tab->n_col; ++i)
1852 if (!isl_int_is_zero(tab->mat->row[var->index][off + i]))
1853 break;
1855 isl_assert(tab->mat->ctx, i < tab->n_col, return -1);
1856 if (isl_tab_pivot(tab, var->index, i) < 0)
1857 return -1;
1859 return 0;
1862 /* We assume Gaussian elimination has been performed on the equalities.
1863 * The equalities can therefore never conflict.
1864 * Adding the equalities is currently only really useful for a later call
1865 * to isl_tab_ineq_type.
1867 static struct isl_tab *add_eq(struct isl_tab *tab, isl_int *eq)
1869 int i;
1870 int r;
1872 if (!tab)
1873 return NULL;
1874 r = isl_tab_add_row(tab, eq);
1875 if (r < 0)
1876 goto error;
1878 r = tab->con[r].index;
1879 i = isl_seq_first_non_zero(tab->mat->row[r] + 2 + tab->M + tab->n_dead,
1880 tab->n_col - tab->n_dead);
1881 isl_assert(tab->mat->ctx, i >= 0, goto error);
1882 i += tab->n_dead;
1883 if (isl_tab_pivot(tab, r, i) < 0)
1884 goto error;
1885 if (isl_tab_kill_col(tab, i) < 0)
1886 goto error;
1887 tab->n_eq++;
1889 return tab;
1890 error:
1891 isl_tab_free(tab);
1892 return NULL;
1895 static int row_is_manifestly_zero(struct isl_tab *tab, int row)
1897 unsigned off = 2 + tab->M;
1899 if (!isl_int_is_zero(tab->mat->row[row][1]))
1900 return 0;
1901 if (tab->M && !isl_int_is_zero(tab->mat->row[row][2]))
1902 return 0;
1903 return isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
1904 tab->n_col - tab->n_dead) == -1;
1907 /* Add an equality that is known to be valid for the given tableau.
1909 int isl_tab_add_valid_eq(struct isl_tab *tab, isl_int *eq)
1911 struct isl_tab_var *var;
1912 int r;
1914 if (!tab)
1915 return -1;
1916 r = isl_tab_add_row(tab, eq);
1917 if (r < 0)
1918 return -1;
1920 var = &tab->con[r];
1921 r = var->index;
1922 if (row_is_manifestly_zero(tab, r)) {
1923 var->is_zero = 1;
1924 if (isl_tab_mark_redundant(tab, r) < 0)
1925 return -1;
1926 return 0;
1929 if (isl_int_is_neg(tab->mat->row[r][1])) {
1930 isl_seq_neg(tab->mat->row[r] + 1, tab->mat->row[r] + 1,
1931 1 + tab->n_col);
1932 var->negated = 1;
1934 var->is_nonneg = 1;
1935 if (to_col(tab, var) < 0)
1936 return -1;
1937 var->is_nonneg = 0;
1938 if (isl_tab_kill_col(tab, var->index) < 0)
1939 return -1;
1941 return 0;
1944 static int add_zero_row(struct isl_tab *tab)
1946 int r;
1947 isl_int *row;
1949 r = isl_tab_allocate_con(tab);
1950 if (r < 0)
1951 return -1;
1953 row = tab->mat->row[tab->con[r].index];
1954 isl_seq_clr(row + 1, 1 + tab->M + tab->n_col);
1955 isl_int_set_si(row[0], 1);
1957 return r;
1960 /* Add equality "eq" and check if it conflicts with the
1961 * previously added constraints or if it is obviously redundant.
1963 int isl_tab_add_eq(struct isl_tab *tab, isl_int *eq)
1965 struct isl_tab_undo *snap = NULL;
1966 struct isl_tab_var *var;
1967 int r;
1968 int row;
1969 int sgn;
1970 isl_int cst;
1972 if (!tab)
1973 return -1;
1974 isl_assert(tab->mat->ctx, !tab->M, return -1);
1976 if (tab->need_undo)
1977 snap = isl_tab_snap(tab);
1979 if (tab->cone) {
1980 isl_int_init(cst);
1981 isl_int_swap(eq[0], cst);
1983 r = isl_tab_add_row(tab, eq);
1984 if (tab->cone) {
1985 isl_int_swap(eq[0], cst);
1986 isl_int_clear(cst);
1988 if (r < 0)
1989 return -1;
1991 var = &tab->con[r];
1992 row = var->index;
1993 if (row_is_manifestly_zero(tab, row)) {
1994 if (snap) {
1995 if (isl_tab_rollback(tab, snap) < 0)
1996 return -1;
1997 } else
1998 drop_row(tab, row);
1999 return 0;
2002 if (tab->bmap) {
2003 tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
2004 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
2005 return -1;
2006 isl_seq_neg(eq, eq, 1 + tab->n_var);
2007 tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
2008 isl_seq_neg(eq, eq, 1 + tab->n_var);
2009 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
2010 return -1;
2011 if (!tab->bmap)
2012 return -1;
2013 if (add_zero_row(tab) < 0)
2014 return -1;
2017 sgn = isl_int_sgn(tab->mat->row[row][1]);
2019 if (sgn > 0) {
2020 isl_seq_neg(tab->mat->row[row] + 1, tab->mat->row[row] + 1,
2021 1 + tab->n_col);
2022 var->negated = 1;
2023 sgn = -1;
2026 if (sgn < 0) {
2027 sgn = sign_of_max(tab, var);
2028 if (sgn < -1)
2029 return -1;
2030 if (sgn < 0) {
2031 if (isl_tab_mark_empty(tab) < 0)
2032 return -1;
2033 return 0;
2037 var->is_nonneg = 1;
2038 if (to_col(tab, var) < 0)
2039 return -1;
2040 var->is_nonneg = 0;
2041 if (isl_tab_kill_col(tab, var->index) < 0)
2042 return -1;
2044 return 0;
2047 /* Construct and return an inequality that expresses an upper bound
2048 * on the given div.
2049 * In particular, if the div is given by
2051 * d = floor(e/m)
2053 * then the inequality expresses
2055 * m d <= e
2057 static struct isl_vec *ineq_for_div(struct isl_basic_map *bmap, unsigned div)
2059 unsigned total;
2060 unsigned div_pos;
2061 struct isl_vec *ineq;
2063 if (!bmap)
2064 return NULL;
2066 total = isl_basic_map_total_dim(bmap);
2067 div_pos = 1 + total - bmap->n_div + div;
2069 ineq = isl_vec_alloc(bmap->ctx, 1 + total);
2070 if (!ineq)
2071 return NULL;
2073 isl_seq_cpy(ineq->el, bmap->div[div] + 1, 1 + total);
2074 isl_int_neg(ineq->el[div_pos], bmap->div[div][0]);
2075 return ineq;
2078 /* For a div d = floor(f/m), add the constraints
2080 * f - m d >= 0
2081 * -(f-(m-1)) + m d >= 0
2083 * Note that the second constraint is the negation of
2085 * f - m d >= m
2087 * If add_ineq is not NULL, then this function is used
2088 * instead of isl_tab_add_ineq to effectively add the inequalities.
2090 static int add_div_constraints(struct isl_tab *tab, unsigned div,
2091 int (*add_ineq)(void *user, isl_int *), void *user)
2093 unsigned total;
2094 unsigned div_pos;
2095 struct isl_vec *ineq;
2097 total = isl_basic_map_total_dim(tab->bmap);
2098 div_pos = 1 + total - tab->bmap->n_div + div;
2100 ineq = ineq_for_div(tab->bmap, div);
2101 if (!ineq)
2102 goto error;
2104 if (add_ineq) {
2105 if (add_ineq(user, ineq->el) < 0)
2106 goto error;
2107 } else {
2108 if (isl_tab_add_ineq(tab, ineq->el) < 0)
2109 goto error;
2112 isl_seq_neg(ineq->el, tab->bmap->div[div] + 1, 1 + total);
2113 isl_int_set(ineq->el[div_pos], tab->bmap->div[div][0]);
2114 isl_int_add(ineq->el[0], ineq->el[0], ineq->el[div_pos]);
2115 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
2117 if (add_ineq) {
2118 if (add_ineq(user, ineq->el) < 0)
2119 goto error;
2120 } else {
2121 if (isl_tab_add_ineq(tab, ineq->el) < 0)
2122 goto error;
2125 isl_vec_free(ineq);
2127 return 0;
2128 error:
2129 isl_vec_free(ineq);
2130 return -1;
2133 /* Check whether the div described by "div" is obviously non-negative.
2134 * If we are using a big parameter, then we will encode the div
2135 * as div' = M + div, which is always non-negative.
2136 * Otherwise, we check whether div is a non-negative affine combination
2137 * of non-negative variables.
2139 static int div_is_nonneg(struct isl_tab *tab, __isl_keep isl_vec *div)
2141 int i;
2143 if (tab->M)
2144 return 1;
2146 if (isl_int_is_neg(div->el[1]))
2147 return 0;
2149 for (i = 0; i < tab->n_var; ++i) {
2150 if (isl_int_is_neg(div->el[2 + i]))
2151 return 0;
2152 if (isl_int_is_zero(div->el[2 + i]))
2153 continue;
2154 if (!tab->var[i].is_nonneg)
2155 return 0;
2158 return 1;
2161 /* Add an extra div, prescribed by "div" to the tableau and
2162 * the associated bmap (which is assumed to be non-NULL).
2164 * If add_ineq is not NULL, then this function is used instead
2165 * of isl_tab_add_ineq to add the div constraints.
2166 * This complication is needed because the code in isl_tab_pip
2167 * wants to perform some extra processing when an inequality
2168 * is added to the tableau.
2170 int isl_tab_add_div(struct isl_tab *tab, __isl_keep isl_vec *div,
2171 int (*add_ineq)(void *user, isl_int *), void *user)
2173 int r;
2174 int k;
2175 int nonneg;
2177 if (!tab || !div)
2178 return -1;
2180 isl_assert(tab->mat->ctx, tab->bmap, return -1);
2182 nonneg = div_is_nonneg(tab, div);
2184 if (isl_tab_extend_cons(tab, 3) < 0)
2185 return -1;
2186 if (isl_tab_extend_vars(tab, 1) < 0)
2187 return -1;
2188 r = isl_tab_allocate_var(tab);
2189 if (r < 0)
2190 return -1;
2192 if (nonneg)
2193 tab->var[r].is_nonneg = 1;
2195 tab->bmap = isl_basic_map_extend_space(tab->bmap,
2196 isl_basic_map_get_space(tab->bmap), 1, 0, 2);
2197 k = isl_basic_map_alloc_div(tab->bmap);
2198 if (k < 0)
2199 return -1;
2200 isl_seq_cpy(tab->bmap->div[k], div->el, div->size);
2201 if (isl_tab_push(tab, isl_tab_undo_bmap_div) < 0)
2202 return -1;
2204 if (add_div_constraints(tab, k, add_ineq, user) < 0)
2205 return -1;
2207 return r;
2210 /* If "track" is set, then we want to keep track of all constraints in tab
2211 * in its bmap field. This field is initialized from a copy of "bmap",
2212 * so we need to make sure that all constraints in "bmap" also appear
2213 * in the constructed tab.
2215 __isl_give struct isl_tab *isl_tab_from_basic_map(
2216 __isl_keep isl_basic_map *bmap, int track)
2218 int i;
2219 struct isl_tab *tab;
2221 if (!bmap)
2222 return NULL;
2223 tab = isl_tab_alloc(bmap->ctx,
2224 isl_basic_map_total_dim(bmap) + bmap->n_ineq + 1,
2225 isl_basic_map_total_dim(bmap), 0);
2226 if (!tab)
2227 return NULL;
2228 tab->preserve = track;
2229 tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
2230 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)) {
2231 if (isl_tab_mark_empty(tab) < 0)
2232 goto error;
2233 goto done;
2235 for (i = 0; i < bmap->n_eq; ++i) {
2236 tab = add_eq(tab, bmap->eq[i]);
2237 if (!tab)
2238 return tab;
2240 for (i = 0; i < bmap->n_ineq; ++i) {
2241 if (isl_tab_add_ineq(tab, bmap->ineq[i]) < 0)
2242 goto error;
2243 if (tab->empty)
2244 goto done;
2246 done:
2247 if (track && isl_tab_track_bmap(tab, isl_basic_map_copy(bmap)) < 0)
2248 goto error;
2249 return tab;
2250 error:
2251 isl_tab_free(tab);
2252 return NULL;
2255 __isl_give struct isl_tab *isl_tab_from_basic_set(
2256 __isl_keep isl_basic_set *bset, int track)
2258 return isl_tab_from_basic_map(bset, track);
2261 /* Construct a tableau corresponding to the recession cone of "bset".
2263 struct isl_tab *isl_tab_from_recession_cone(__isl_keep isl_basic_set *bset,
2264 int parametric)
2266 isl_int cst;
2267 int i;
2268 struct isl_tab *tab;
2269 unsigned offset = 0;
2271 if (!bset)
2272 return NULL;
2273 if (parametric)
2274 offset = isl_basic_set_dim(bset, isl_dim_param);
2275 tab = isl_tab_alloc(bset->ctx, bset->n_eq + bset->n_ineq,
2276 isl_basic_set_total_dim(bset) - offset, 0);
2277 if (!tab)
2278 return NULL;
2279 tab->rational = ISL_F_ISSET(bset, ISL_BASIC_SET_RATIONAL);
2280 tab->cone = 1;
2282 isl_int_init(cst);
2283 for (i = 0; i < bset->n_eq; ++i) {
2284 isl_int_swap(bset->eq[i][offset], cst);
2285 if (offset > 0) {
2286 if (isl_tab_add_eq(tab, bset->eq[i] + offset) < 0)
2287 goto error;
2288 } else
2289 tab = add_eq(tab, bset->eq[i]);
2290 isl_int_swap(bset->eq[i][offset], cst);
2291 if (!tab)
2292 goto done;
2294 for (i = 0; i < bset->n_ineq; ++i) {
2295 int r;
2296 isl_int_swap(bset->ineq[i][offset], cst);
2297 r = isl_tab_add_row(tab, bset->ineq[i] + offset);
2298 isl_int_swap(bset->ineq[i][offset], cst);
2299 if (r < 0)
2300 goto error;
2301 tab->con[r].is_nonneg = 1;
2302 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
2303 goto error;
2305 done:
2306 isl_int_clear(cst);
2307 return tab;
2308 error:
2309 isl_int_clear(cst);
2310 isl_tab_free(tab);
2311 return NULL;
2314 /* Assuming "tab" is the tableau of a cone, check if the cone is
2315 * bounded, i.e., if it is empty or only contains the origin.
2317 int isl_tab_cone_is_bounded(struct isl_tab *tab)
2319 int i;
2321 if (!tab)
2322 return -1;
2323 if (tab->empty)
2324 return 1;
2325 if (tab->n_dead == tab->n_col)
2326 return 1;
2328 for (;;) {
2329 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2330 struct isl_tab_var *var;
2331 int sgn;
2332 var = isl_tab_var_from_row(tab, i);
2333 if (!var->is_nonneg)
2334 continue;
2335 sgn = sign_of_max(tab, var);
2336 if (sgn < -1)
2337 return -1;
2338 if (sgn != 0)
2339 return 0;
2340 if (close_row(tab, var) < 0)
2341 return -1;
2342 break;
2344 if (tab->n_dead == tab->n_col)
2345 return 1;
2346 if (i == tab->n_row)
2347 return 0;
2351 int isl_tab_sample_is_integer(struct isl_tab *tab)
2353 int i;
2355 if (!tab)
2356 return -1;
2358 for (i = 0; i < tab->n_var; ++i) {
2359 int row;
2360 if (!tab->var[i].is_row)
2361 continue;
2362 row = tab->var[i].index;
2363 if (!isl_int_is_divisible_by(tab->mat->row[row][1],
2364 tab->mat->row[row][0]))
2365 return 0;
2367 return 1;
2370 static struct isl_vec *extract_integer_sample(struct isl_tab *tab)
2372 int i;
2373 struct isl_vec *vec;
2375 vec = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
2376 if (!vec)
2377 return NULL;
2379 isl_int_set_si(vec->block.data[0], 1);
2380 for (i = 0; i < tab->n_var; ++i) {
2381 if (!tab->var[i].is_row)
2382 isl_int_set_si(vec->block.data[1 + i], 0);
2383 else {
2384 int row = tab->var[i].index;
2385 isl_int_divexact(vec->block.data[1 + i],
2386 tab->mat->row[row][1], tab->mat->row[row][0]);
2390 return vec;
2393 struct isl_vec *isl_tab_get_sample_value(struct isl_tab *tab)
2395 int i;
2396 struct isl_vec *vec;
2397 isl_int m;
2399 if (!tab)
2400 return NULL;
2402 vec = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
2403 if (!vec)
2404 return NULL;
2406 isl_int_init(m);
2408 isl_int_set_si(vec->block.data[0], 1);
2409 for (i = 0; i < tab->n_var; ++i) {
2410 int row;
2411 if (!tab->var[i].is_row) {
2412 isl_int_set_si(vec->block.data[1 + i], 0);
2413 continue;
2415 row = tab->var[i].index;
2416 isl_int_gcd(m, vec->block.data[0], tab->mat->row[row][0]);
2417 isl_int_divexact(m, tab->mat->row[row][0], m);
2418 isl_seq_scale(vec->block.data, vec->block.data, m, 1 + i);
2419 isl_int_divexact(m, vec->block.data[0], tab->mat->row[row][0]);
2420 isl_int_mul(vec->block.data[1 + i], m, tab->mat->row[row][1]);
2422 vec = isl_vec_normalize(vec);
2424 isl_int_clear(m);
2425 return vec;
2428 /* Update "bmap" based on the results of the tableau "tab".
2429 * In particular, implicit equalities are made explicit, redundant constraints
2430 * are removed and if the sample value happens to be integer, it is stored
2431 * in "bmap" (unless "bmap" already had an integer sample).
2433 * The tableau is assumed to have been created from "bmap" using
2434 * isl_tab_from_basic_map.
2436 struct isl_basic_map *isl_basic_map_update_from_tab(struct isl_basic_map *bmap,
2437 struct isl_tab *tab)
2439 int i;
2440 unsigned n_eq;
2442 if (!bmap)
2443 return NULL;
2444 if (!tab)
2445 return bmap;
2447 n_eq = tab->n_eq;
2448 if (tab->empty)
2449 bmap = isl_basic_map_set_to_empty(bmap);
2450 else
2451 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2452 if (isl_tab_is_equality(tab, n_eq + i))
2453 isl_basic_map_inequality_to_equality(bmap, i);
2454 else if (isl_tab_is_redundant(tab, n_eq + i))
2455 isl_basic_map_drop_inequality(bmap, i);
2457 if (bmap->n_eq != n_eq)
2458 isl_basic_map_gauss(bmap, NULL);
2459 if (!tab->rational &&
2460 !bmap->sample && isl_tab_sample_is_integer(tab))
2461 bmap->sample = extract_integer_sample(tab);
2462 return bmap;
2465 struct isl_basic_set *isl_basic_set_update_from_tab(struct isl_basic_set *bset,
2466 struct isl_tab *tab)
2468 return (struct isl_basic_set *)isl_basic_map_update_from_tab(
2469 (struct isl_basic_map *)bset, tab);
2472 /* Given a non-negative variable "var", add a new non-negative variable
2473 * that is the opposite of "var", ensuring that var can only attain the
2474 * value zero.
2475 * If var = n/d is a row variable, then the new variable = -n/d.
2476 * If var is a column variables, then the new variable = -var.
2477 * If the new variable cannot attain non-negative values, then
2478 * the resulting tableau is empty.
2479 * Otherwise, we know the value will be zero and we close the row.
2481 static int cut_to_hyperplane(struct isl_tab *tab, struct isl_tab_var *var)
2483 unsigned r;
2484 isl_int *row;
2485 int sgn;
2486 unsigned off = 2 + tab->M;
2488 if (var->is_zero)
2489 return 0;
2490 isl_assert(tab->mat->ctx, !var->is_redundant, return -1);
2491 isl_assert(tab->mat->ctx, var->is_nonneg, return -1);
2493 if (isl_tab_extend_cons(tab, 1) < 0)
2494 return -1;
2496 r = tab->n_con;
2497 tab->con[r].index = tab->n_row;
2498 tab->con[r].is_row = 1;
2499 tab->con[r].is_nonneg = 0;
2500 tab->con[r].is_zero = 0;
2501 tab->con[r].is_redundant = 0;
2502 tab->con[r].frozen = 0;
2503 tab->con[r].negated = 0;
2504 tab->row_var[tab->n_row] = ~r;
2505 row = tab->mat->row[tab->n_row];
2507 if (var->is_row) {
2508 isl_int_set(row[0], tab->mat->row[var->index][0]);
2509 isl_seq_neg(row + 1,
2510 tab->mat->row[var->index] + 1, 1 + tab->n_col);
2511 } else {
2512 isl_int_set_si(row[0], 1);
2513 isl_seq_clr(row + 1, 1 + tab->n_col);
2514 isl_int_set_si(row[off + var->index], -1);
2517 tab->n_row++;
2518 tab->n_con++;
2519 if (isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->con[r]) < 0)
2520 return -1;
2522 sgn = sign_of_max(tab, &tab->con[r]);
2523 if (sgn < -1)
2524 return -1;
2525 if (sgn < 0) {
2526 if (isl_tab_mark_empty(tab) < 0)
2527 return -1;
2528 return 0;
2530 tab->con[r].is_nonneg = 1;
2531 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
2532 return -1;
2533 /* sgn == 0 */
2534 if (close_row(tab, &tab->con[r]) < 0)
2535 return -1;
2537 return 0;
2540 /* Given a tableau "tab" and an inequality constraint "con" of the tableau,
2541 * relax the inequality by one. That is, the inequality r >= 0 is replaced
2542 * by r' = r + 1 >= 0.
2543 * If r is a row variable, we simply increase the constant term by one
2544 * (taking into account the denominator).
2545 * If r is a column variable, then we need to modify each row that
2546 * refers to r = r' - 1 by substituting this equality, effectively
2547 * subtracting the coefficient of the column from the constant.
2548 * We should only do this if the minimum is manifestly unbounded,
2549 * however. Otherwise, we may end up with negative sample values
2550 * for non-negative variables.
2551 * So, if r is a column variable with a minimum that is not
2552 * manifestly unbounded, then we need to move it to a row.
2553 * However, the sample value of this row may be negative,
2554 * even after the relaxation, so we need to restore it.
2555 * We therefore prefer to pivot a column up to a row, if possible.
2557 struct isl_tab *isl_tab_relax(struct isl_tab *tab, int con)
2559 struct isl_tab_var *var;
2560 unsigned off = 2 + tab->M;
2562 if (!tab)
2563 return NULL;
2565 var = &tab->con[con];
2567 if (var->is_row && (var->index < 0 || var->index < tab->n_redundant))
2568 isl_die(tab->mat->ctx, isl_error_invalid,
2569 "cannot relax redundant constraint", goto error);
2570 if (!var->is_row && (var->index < 0 || var->index < tab->n_dead))
2571 isl_die(tab->mat->ctx, isl_error_invalid,
2572 "cannot relax dead constraint", goto error);
2574 if (!var->is_row && !max_is_manifestly_unbounded(tab, var))
2575 if (to_row(tab, var, 1) < 0)
2576 goto error;
2577 if (!var->is_row && !min_is_manifestly_unbounded(tab, var))
2578 if (to_row(tab, var, -1) < 0)
2579 goto error;
2581 if (var->is_row) {
2582 isl_int_add(tab->mat->row[var->index][1],
2583 tab->mat->row[var->index][1], tab->mat->row[var->index][0]);
2584 if (restore_row(tab, var) < 0)
2585 goto error;
2586 } else {
2587 int i;
2589 for (i = 0; i < tab->n_row; ++i) {
2590 if (isl_int_is_zero(tab->mat->row[i][off + var->index]))
2591 continue;
2592 isl_int_sub(tab->mat->row[i][1], tab->mat->row[i][1],
2593 tab->mat->row[i][off + var->index]);
2598 if (isl_tab_push_var(tab, isl_tab_undo_relax, var) < 0)
2599 goto error;
2601 return tab;
2602 error:
2603 isl_tab_free(tab);
2604 return NULL;
2607 /* Remove the sign constraint from constraint "con".
2609 * If the constraint variable was originally marked non-negative,
2610 * then we make sure we mark it non-negative again during rollback.
2612 int isl_tab_unrestrict(struct isl_tab *tab, int con)
2614 struct isl_tab_var *var;
2616 if (!tab)
2617 return -1;
2619 var = &tab->con[con];
2620 if (!var->is_nonneg)
2621 return 0;
2623 var->is_nonneg = 0;
2624 if (isl_tab_push_var(tab, isl_tab_undo_unrestrict, var) < 0)
2625 return -1;
2627 return 0;
2630 int isl_tab_select_facet(struct isl_tab *tab, int con)
2632 if (!tab)
2633 return -1;
2635 return cut_to_hyperplane(tab, &tab->con[con]);
2638 static int may_be_equality(struct isl_tab *tab, int row)
2640 return tab->rational ? isl_int_is_zero(tab->mat->row[row][1])
2641 : isl_int_lt(tab->mat->row[row][1],
2642 tab->mat->row[row][0]);
2645 /* Check for (near) equalities among the constraints.
2646 * A constraint is an equality if it is non-negative and if
2647 * its maximal value is either
2648 * - zero (in case of rational tableaus), or
2649 * - strictly less than 1 (in case of integer tableaus)
2651 * We first mark all non-redundant and non-dead variables that
2652 * are not frozen and not obviously not an equality.
2653 * Then we iterate over all marked variables if they can attain
2654 * any values larger than zero or at least one.
2655 * If the maximal value is zero, we mark any column variables
2656 * that appear in the row as being zero and mark the row as being redundant.
2657 * Otherwise, if the maximal value is strictly less than one (and the
2658 * tableau is integer), then we restrict the value to being zero
2659 * by adding an opposite non-negative variable.
2661 int isl_tab_detect_implicit_equalities(struct isl_tab *tab)
2663 int i;
2664 unsigned n_marked;
2666 if (!tab)
2667 return -1;
2668 if (tab->empty)
2669 return 0;
2670 if (tab->n_dead == tab->n_col)
2671 return 0;
2673 n_marked = 0;
2674 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2675 struct isl_tab_var *var = isl_tab_var_from_row(tab, i);
2676 var->marked = !var->frozen && var->is_nonneg &&
2677 may_be_equality(tab, i);
2678 if (var->marked)
2679 n_marked++;
2681 for (i = tab->n_dead; i < tab->n_col; ++i) {
2682 struct isl_tab_var *var = var_from_col(tab, i);
2683 var->marked = !var->frozen && var->is_nonneg;
2684 if (var->marked)
2685 n_marked++;
2687 while (n_marked) {
2688 struct isl_tab_var *var;
2689 int sgn;
2690 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2691 var = isl_tab_var_from_row(tab, i);
2692 if (var->marked)
2693 break;
2695 if (i == tab->n_row) {
2696 for (i = tab->n_dead; i < tab->n_col; ++i) {
2697 var = var_from_col(tab, i);
2698 if (var->marked)
2699 break;
2701 if (i == tab->n_col)
2702 break;
2704 var->marked = 0;
2705 n_marked--;
2706 sgn = sign_of_max(tab, var);
2707 if (sgn < 0)
2708 return -1;
2709 if (sgn == 0) {
2710 if (close_row(tab, var) < 0)
2711 return -1;
2712 } else if (!tab->rational && !at_least_one(tab, var)) {
2713 if (cut_to_hyperplane(tab, var) < 0)
2714 return -1;
2715 return isl_tab_detect_implicit_equalities(tab);
2717 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2718 var = isl_tab_var_from_row(tab, i);
2719 if (!var->marked)
2720 continue;
2721 if (may_be_equality(tab, i))
2722 continue;
2723 var->marked = 0;
2724 n_marked--;
2728 return 0;
2731 /* Update the element of row_var or col_var that corresponds to
2732 * constraint tab->con[i] to a move from position "old" to position "i".
2734 static int update_con_after_move(struct isl_tab *tab, int i, int old)
2736 int *p;
2737 int index;
2739 index = tab->con[i].index;
2740 if (index == -1)
2741 return 0;
2742 p = tab->con[i].is_row ? tab->row_var : tab->col_var;
2743 if (p[index] != ~old)
2744 isl_die(tab->mat->ctx, isl_error_internal,
2745 "broken internal state", return -1);
2746 p[index] = ~i;
2748 return 0;
2751 /* Rotate the "n" constraints starting at "first" to the right,
2752 * putting the last constraint in the position of the first constraint.
2754 static int rotate_constraints(struct isl_tab *tab, int first, int n)
2756 int i, last;
2757 struct isl_tab_var var;
2759 if (n <= 1)
2760 return 0;
2762 last = first + n - 1;
2763 var = tab->con[last];
2764 for (i = last; i > first; --i) {
2765 tab->con[i] = tab->con[i - 1];
2766 if (update_con_after_move(tab, i, i - 1) < 0)
2767 return -1;
2769 tab->con[first] = var;
2770 if (update_con_after_move(tab, first, last) < 0)
2771 return -1;
2773 return 0;
2776 /* Make the equalities that are implicit in "bmap" but that have been
2777 * detected in the corresponding "tab" explicit in "bmap" and update
2778 * "tab" to reflect the new order of the constraints.
2780 * In particular, if inequality i is an implicit equality then
2781 * isl_basic_map_inequality_to_equality will move the inequality
2782 * in front of the other equality and it will move the last inequality
2783 * in the position of inequality i.
2784 * In the tableau, the inequalities of "bmap" are stored after the equalities
2785 * and so the original order
2787 * E E E E E A A A I B B B B L
2789 * is changed into
2791 * I E E E E E A A A L B B B B
2793 * where I is the implicit equality, the E are equalities,
2794 * the A inequalities before I, the B inequalities after I and
2795 * L the last inequality.
2796 * We therefore need to rotate to the right two sets of constraints,
2797 * those up to and including I and those after I.
2799 * If "tab" contains any constraints that are not in "bmap" then they
2800 * appear after those in "bmap" and they should be left untouched.
2802 * Note that this function leaves "bmap" in a temporary state
2803 * as it does not call isl_basic_map_gauss. Calling this function
2804 * is the responsibility of the caller.
2806 __isl_give isl_basic_map *isl_tab_make_equalities_explicit(struct isl_tab *tab,
2807 __isl_take isl_basic_map *bmap)
2809 int i;
2811 if (!tab || !bmap)
2812 return isl_basic_map_free(bmap);
2813 if (tab->empty)
2814 return bmap;
2816 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2817 if (!isl_tab_is_equality(tab, bmap->n_eq + i))
2818 continue;
2819 isl_basic_map_inequality_to_equality(bmap, i);
2820 if (rotate_constraints(tab, 0, tab->n_eq + i + 1) < 0)
2821 return isl_basic_map_free(bmap);
2822 if (rotate_constraints(tab, tab->n_eq + i + 1,
2823 bmap->n_ineq - i) < 0)
2824 return isl_basic_map_free(bmap);
2825 tab->n_eq++;
2828 return bmap;
2831 static int con_is_redundant(struct isl_tab *tab, struct isl_tab_var *var)
2833 if (!tab)
2834 return -1;
2835 if (tab->rational) {
2836 int sgn = sign_of_min(tab, var);
2837 if (sgn < -1)
2838 return -1;
2839 return sgn >= 0;
2840 } else {
2841 int irred = isl_tab_min_at_most_neg_one(tab, var);
2842 if (irred < 0)
2843 return -1;
2844 return !irred;
2848 /* Check for (near) redundant constraints.
2849 * A constraint is redundant if it is non-negative and if
2850 * its minimal value (temporarily ignoring the non-negativity) is either
2851 * - zero (in case of rational tableaus), or
2852 * - strictly larger than -1 (in case of integer tableaus)
2854 * We first mark all non-redundant and non-dead variables that
2855 * are not frozen and not obviously negatively unbounded.
2856 * Then we iterate over all marked variables if they can attain
2857 * any values smaller than zero or at most negative one.
2858 * If not, we mark the row as being redundant (assuming it hasn't
2859 * been detected as being obviously redundant in the mean time).
2861 int isl_tab_detect_redundant(struct isl_tab *tab)
2863 int i;
2864 unsigned n_marked;
2866 if (!tab)
2867 return -1;
2868 if (tab->empty)
2869 return 0;
2870 if (tab->n_redundant == tab->n_row)
2871 return 0;
2873 n_marked = 0;
2874 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2875 struct isl_tab_var *var = isl_tab_var_from_row(tab, i);
2876 var->marked = !var->frozen && var->is_nonneg;
2877 if (var->marked)
2878 n_marked++;
2880 for (i = tab->n_dead; i < tab->n_col; ++i) {
2881 struct isl_tab_var *var = var_from_col(tab, i);
2882 var->marked = !var->frozen && var->is_nonneg &&
2883 !min_is_manifestly_unbounded(tab, var);
2884 if (var->marked)
2885 n_marked++;
2887 while (n_marked) {
2888 struct isl_tab_var *var;
2889 int red;
2890 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2891 var = isl_tab_var_from_row(tab, i);
2892 if (var->marked)
2893 break;
2895 if (i == tab->n_row) {
2896 for (i = tab->n_dead; i < tab->n_col; ++i) {
2897 var = var_from_col(tab, i);
2898 if (var->marked)
2899 break;
2901 if (i == tab->n_col)
2902 break;
2904 var->marked = 0;
2905 n_marked--;
2906 red = con_is_redundant(tab, var);
2907 if (red < 0)
2908 return -1;
2909 if (red && !var->is_redundant)
2910 if (isl_tab_mark_redundant(tab, var->index) < 0)
2911 return -1;
2912 for (i = tab->n_dead; i < tab->n_col; ++i) {
2913 var = var_from_col(tab, i);
2914 if (!var->marked)
2915 continue;
2916 if (!min_is_manifestly_unbounded(tab, var))
2917 continue;
2918 var->marked = 0;
2919 n_marked--;
2923 return 0;
2926 int isl_tab_is_equality(struct isl_tab *tab, int con)
2928 int row;
2929 unsigned off;
2931 if (!tab)
2932 return -1;
2933 if (tab->con[con].is_zero)
2934 return 1;
2935 if (tab->con[con].is_redundant)
2936 return 0;
2937 if (!tab->con[con].is_row)
2938 return tab->con[con].index < tab->n_dead;
2940 row = tab->con[con].index;
2942 off = 2 + tab->M;
2943 return isl_int_is_zero(tab->mat->row[row][1]) &&
2944 (!tab->M || isl_int_is_zero(tab->mat->row[row][2])) &&
2945 isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
2946 tab->n_col - tab->n_dead) == -1;
2949 /* Return the minimal value of the affine expression "f" with denominator
2950 * "denom" in *opt, *opt_denom, assuming the tableau is not empty and
2951 * the expression cannot attain arbitrarily small values.
2952 * If opt_denom is NULL, then *opt is rounded up to the nearest integer.
2953 * The return value reflects the nature of the result (empty, unbounded,
2954 * minimal value returned in *opt).
2956 enum isl_lp_result isl_tab_min(struct isl_tab *tab,
2957 isl_int *f, isl_int denom, isl_int *opt, isl_int *opt_denom,
2958 unsigned flags)
2960 int r;
2961 enum isl_lp_result res = isl_lp_ok;
2962 struct isl_tab_var *var;
2963 struct isl_tab_undo *snap;
2965 if (!tab)
2966 return isl_lp_error;
2968 if (tab->empty)
2969 return isl_lp_empty;
2971 snap = isl_tab_snap(tab);
2972 r = isl_tab_add_row(tab, f);
2973 if (r < 0)
2974 return isl_lp_error;
2975 var = &tab->con[r];
2976 for (;;) {
2977 int row, col;
2978 find_pivot(tab, var, var, -1, &row, &col);
2979 if (row == var->index) {
2980 res = isl_lp_unbounded;
2981 break;
2983 if (row == -1)
2984 break;
2985 if (isl_tab_pivot(tab, row, col) < 0)
2986 return isl_lp_error;
2988 isl_int_mul(tab->mat->row[var->index][0],
2989 tab->mat->row[var->index][0], denom);
2990 if (ISL_FL_ISSET(flags, ISL_TAB_SAVE_DUAL)) {
2991 int i;
2993 isl_vec_free(tab->dual);
2994 tab->dual = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_con);
2995 if (!tab->dual)
2996 return isl_lp_error;
2997 isl_int_set(tab->dual->el[0], tab->mat->row[var->index][0]);
2998 for (i = 0; i < tab->n_con; ++i) {
2999 int pos;
3000 if (tab->con[i].is_row) {
3001 isl_int_set_si(tab->dual->el[1 + i], 0);
3002 continue;
3004 pos = 2 + tab->M + tab->con[i].index;
3005 if (tab->con[i].negated)
3006 isl_int_neg(tab->dual->el[1 + i],
3007 tab->mat->row[var->index][pos]);
3008 else
3009 isl_int_set(tab->dual->el[1 + i],
3010 tab->mat->row[var->index][pos]);
3013 if (opt && res == isl_lp_ok) {
3014 if (opt_denom) {
3015 isl_int_set(*opt, tab->mat->row[var->index][1]);
3016 isl_int_set(*opt_denom, tab->mat->row[var->index][0]);
3017 } else
3018 isl_int_cdiv_q(*opt, tab->mat->row[var->index][1],
3019 tab->mat->row[var->index][0]);
3021 if (isl_tab_rollback(tab, snap) < 0)
3022 return isl_lp_error;
3023 return res;
3026 int isl_tab_is_redundant(struct isl_tab *tab, int con)
3028 if (!tab)
3029 return -1;
3030 if (tab->con[con].is_zero)
3031 return 0;
3032 if (tab->con[con].is_redundant)
3033 return 1;
3034 return tab->con[con].is_row && tab->con[con].index < tab->n_redundant;
3037 /* Take a snapshot of the tableau that can be restored by s call to
3038 * isl_tab_rollback.
3040 struct isl_tab_undo *isl_tab_snap(struct isl_tab *tab)
3042 if (!tab)
3043 return NULL;
3044 tab->need_undo = 1;
3045 return tab->top;
3048 /* Undo the operation performed by isl_tab_relax.
3050 static int unrelax(struct isl_tab *tab, struct isl_tab_var *var) WARN_UNUSED;
3051 static int unrelax(struct isl_tab *tab, struct isl_tab_var *var)
3053 unsigned off = 2 + tab->M;
3055 if (!var->is_row && !max_is_manifestly_unbounded(tab, var))
3056 if (to_row(tab, var, 1) < 0)
3057 return -1;
3059 if (var->is_row) {
3060 isl_int_sub(tab->mat->row[var->index][1],
3061 tab->mat->row[var->index][1], tab->mat->row[var->index][0]);
3062 if (var->is_nonneg) {
3063 int sgn = restore_row(tab, var);
3064 isl_assert(tab->mat->ctx, sgn >= 0, return -1);
3066 } else {
3067 int i;
3069 for (i = 0; i < tab->n_row; ++i) {
3070 if (isl_int_is_zero(tab->mat->row[i][off + var->index]))
3071 continue;
3072 isl_int_add(tab->mat->row[i][1], tab->mat->row[i][1],
3073 tab->mat->row[i][off + var->index]);
3078 return 0;
3081 /* Undo the operation performed by isl_tab_unrestrict.
3083 * In particular, mark the variable as being non-negative and make
3084 * sure the sample value respects this constraint.
3086 static int ununrestrict(struct isl_tab *tab, struct isl_tab_var *var)
3088 var->is_nonneg = 1;
3090 if (var->is_row && restore_row(tab, var) < -1)
3091 return -1;
3093 return 0;
3096 static int perform_undo_var(struct isl_tab *tab, struct isl_tab_undo *undo) WARN_UNUSED;
3097 static int perform_undo_var(struct isl_tab *tab, struct isl_tab_undo *undo)
3099 struct isl_tab_var *var = var_from_index(tab, undo->u.var_index);
3100 switch (undo->type) {
3101 case isl_tab_undo_nonneg:
3102 var->is_nonneg = 0;
3103 break;
3104 case isl_tab_undo_redundant:
3105 var->is_redundant = 0;
3106 tab->n_redundant--;
3107 restore_row(tab, isl_tab_var_from_row(tab, tab->n_redundant));
3108 break;
3109 case isl_tab_undo_freeze:
3110 var->frozen = 0;
3111 break;
3112 case isl_tab_undo_zero:
3113 var->is_zero = 0;
3114 if (!var->is_row)
3115 tab->n_dead--;
3116 break;
3117 case isl_tab_undo_allocate:
3118 if (undo->u.var_index >= 0) {
3119 isl_assert(tab->mat->ctx, !var->is_row, return -1);
3120 drop_col(tab, var->index);
3121 break;
3123 if (!var->is_row) {
3124 if (!max_is_manifestly_unbounded(tab, var)) {
3125 if (to_row(tab, var, 1) < 0)
3126 return -1;
3127 } else if (!min_is_manifestly_unbounded(tab, var)) {
3128 if (to_row(tab, var, -1) < 0)
3129 return -1;
3130 } else
3131 if (to_row(tab, var, 0) < 0)
3132 return -1;
3134 drop_row(tab, var->index);
3135 break;
3136 case isl_tab_undo_relax:
3137 return unrelax(tab, var);
3138 case isl_tab_undo_unrestrict:
3139 return ununrestrict(tab, var);
3140 default:
3141 isl_die(tab->mat->ctx, isl_error_internal,
3142 "perform_undo_var called on invalid undo record",
3143 return -1);
3146 return 0;
3149 /* Restore the tableau to the state where the basic variables
3150 * are those in "col_var".
3151 * We first construct a list of variables that are currently in
3152 * the basis, but shouldn't. Then we iterate over all variables
3153 * that should be in the basis and for each one that is currently
3154 * not in the basis, we exchange it with one of the elements of the
3155 * list constructed before.
3156 * We can always find an appropriate variable to pivot with because
3157 * the current basis is mapped to the old basis by a non-singular
3158 * matrix and so we can never end up with a zero row.
3160 static int restore_basis(struct isl_tab *tab, int *col_var)
3162 int i, j;
3163 int n_extra = 0;
3164 int *extra = NULL; /* current columns that contain bad stuff */
3165 unsigned off = 2 + tab->M;
3167 extra = isl_alloc_array(tab->mat->ctx, int, tab->n_col);
3168 if (tab->n_col && !extra)
3169 goto error;
3170 for (i = 0; i < tab->n_col; ++i) {
3171 for (j = 0; j < tab->n_col; ++j)
3172 if (tab->col_var[i] == col_var[j])
3173 break;
3174 if (j < tab->n_col)
3175 continue;
3176 extra[n_extra++] = i;
3178 for (i = 0; i < tab->n_col && n_extra > 0; ++i) {
3179 struct isl_tab_var *var;
3180 int row;
3182 for (j = 0; j < tab->n_col; ++j)
3183 if (col_var[i] == tab->col_var[j])
3184 break;
3185 if (j < tab->n_col)
3186 continue;
3187 var = var_from_index(tab, col_var[i]);
3188 row = var->index;
3189 for (j = 0; j < n_extra; ++j)
3190 if (!isl_int_is_zero(tab->mat->row[row][off+extra[j]]))
3191 break;
3192 isl_assert(tab->mat->ctx, j < n_extra, goto error);
3193 if (isl_tab_pivot(tab, row, extra[j]) < 0)
3194 goto error;
3195 extra[j] = extra[--n_extra];
3198 free(extra);
3199 return 0;
3200 error:
3201 free(extra);
3202 return -1;
3205 /* Remove all samples with index n or greater, i.e., those samples
3206 * that were added since we saved this number of samples in
3207 * isl_tab_save_samples.
3209 static void drop_samples_since(struct isl_tab *tab, int n)
3211 int i;
3213 for (i = tab->n_sample - 1; i >= 0 && tab->n_sample > n; --i) {
3214 if (tab->sample_index[i] < n)
3215 continue;
3217 if (i != tab->n_sample - 1) {
3218 int t = tab->sample_index[tab->n_sample-1];
3219 tab->sample_index[tab->n_sample-1] = tab->sample_index[i];
3220 tab->sample_index[i] = t;
3221 isl_mat_swap_rows(tab->samples, tab->n_sample-1, i);
3223 tab->n_sample--;
3227 static int perform_undo(struct isl_tab *tab, struct isl_tab_undo *undo) WARN_UNUSED;
3228 static int perform_undo(struct isl_tab *tab, struct isl_tab_undo *undo)
3230 switch (undo->type) {
3231 case isl_tab_undo_empty:
3232 tab->empty = 0;
3233 break;
3234 case isl_tab_undo_nonneg:
3235 case isl_tab_undo_redundant:
3236 case isl_tab_undo_freeze:
3237 case isl_tab_undo_zero:
3238 case isl_tab_undo_allocate:
3239 case isl_tab_undo_relax:
3240 case isl_tab_undo_unrestrict:
3241 return perform_undo_var(tab, undo);
3242 case isl_tab_undo_bmap_eq:
3243 return isl_basic_map_free_equality(tab->bmap, 1);
3244 case isl_tab_undo_bmap_ineq:
3245 return isl_basic_map_free_inequality(tab->bmap, 1);
3246 case isl_tab_undo_bmap_div:
3247 if (isl_basic_map_free_div(tab->bmap, 1) < 0)
3248 return -1;
3249 if (tab->samples)
3250 tab->samples->n_col--;
3251 break;
3252 case isl_tab_undo_saved_basis:
3253 if (restore_basis(tab, undo->u.col_var) < 0)
3254 return -1;
3255 break;
3256 case isl_tab_undo_drop_sample:
3257 tab->n_outside--;
3258 break;
3259 case isl_tab_undo_saved_samples:
3260 drop_samples_since(tab, undo->u.n);
3261 break;
3262 case isl_tab_undo_callback:
3263 return undo->u.callback->run(undo->u.callback);
3264 default:
3265 isl_assert(tab->mat->ctx, 0, return -1);
3267 return 0;
3270 /* Return the tableau to the state it was in when the snapshot "snap"
3271 * was taken.
3273 int isl_tab_rollback(struct isl_tab *tab, struct isl_tab_undo *snap)
3275 struct isl_tab_undo *undo, *next;
3277 if (!tab)
3278 return -1;
3280 tab->in_undo = 1;
3281 for (undo = tab->top; undo && undo != &tab->bottom; undo = next) {
3282 next = undo->next;
3283 if (undo == snap)
3284 break;
3285 if (perform_undo(tab, undo) < 0) {
3286 tab->top = undo;
3287 free_undo(tab);
3288 tab->in_undo = 0;
3289 return -1;
3291 free_undo_record(undo);
3293 tab->in_undo = 0;
3294 tab->top = undo;
3295 if (!undo)
3296 return -1;
3297 return 0;
3300 /* The given row "row" represents an inequality violated by all
3301 * points in the tableau. Check for some special cases of such
3302 * separating constraints.
3303 * In particular, if the row has been reduced to the constant -1,
3304 * then we know the inequality is adjacent (but opposite) to
3305 * an equality in the tableau.
3306 * If the row has been reduced to r = c*(-1 -r'), with r' an inequality
3307 * of the tableau and c a positive constant, then the inequality
3308 * is adjacent (but opposite) to the inequality r'.
3310 static enum isl_ineq_type separation_type(struct isl_tab *tab, unsigned row)
3312 int pos;
3313 unsigned off = 2 + tab->M;
3315 if (tab->rational)
3316 return isl_ineq_separate;
3318 if (!isl_int_is_one(tab->mat->row[row][0]))
3319 return isl_ineq_separate;
3321 pos = isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
3322 tab->n_col - tab->n_dead);
3323 if (pos == -1) {
3324 if (isl_int_is_negone(tab->mat->row[row][1]))
3325 return isl_ineq_adj_eq;
3326 else
3327 return isl_ineq_separate;
3330 if (!isl_int_eq(tab->mat->row[row][1],
3331 tab->mat->row[row][off + tab->n_dead + pos]))
3332 return isl_ineq_separate;
3334 pos = isl_seq_first_non_zero(
3335 tab->mat->row[row] + off + tab->n_dead + pos + 1,
3336 tab->n_col - tab->n_dead - pos - 1);
3338 return pos == -1 ? isl_ineq_adj_ineq : isl_ineq_separate;
3341 /* Check the effect of inequality "ineq" on the tableau "tab".
3342 * The result may be
3343 * isl_ineq_redundant: satisfied by all points in the tableau
3344 * isl_ineq_separate: satisfied by no point in the tableau
3345 * isl_ineq_cut: satisfied by some by not all points
3346 * isl_ineq_adj_eq: adjacent to an equality
3347 * isl_ineq_adj_ineq: adjacent to an inequality.
3349 enum isl_ineq_type isl_tab_ineq_type(struct isl_tab *tab, isl_int *ineq)
3351 enum isl_ineq_type type = isl_ineq_error;
3352 struct isl_tab_undo *snap = NULL;
3353 int con;
3354 int row;
3356 if (!tab)
3357 return isl_ineq_error;
3359 if (isl_tab_extend_cons(tab, 1) < 0)
3360 return isl_ineq_error;
3362 snap = isl_tab_snap(tab);
3364 con = isl_tab_add_row(tab, ineq);
3365 if (con < 0)
3366 goto error;
3368 row = tab->con[con].index;
3369 if (isl_tab_row_is_redundant(tab, row))
3370 type = isl_ineq_redundant;
3371 else if (isl_int_is_neg(tab->mat->row[row][1]) &&
3372 (tab->rational ||
3373 isl_int_abs_ge(tab->mat->row[row][1],
3374 tab->mat->row[row][0]))) {
3375 int nonneg = at_least_zero(tab, &tab->con[con]);
3376 if (nonneg < 0)
3377 goto error;
3378 if (nonneg)
3379 type = isl_ineq_cut;
3380 else
3381 type = separation_type(tab, row);
3382 } else {
3383 int red = con_is_redundant(tab, &tab->con[con]);
3384 if (red < 0)
3385 goto error;
3386 if (!red)
3387 type = isl_ineq_cut;
3388 else
3389 type = isl_ineq_redundant;
3392 if (isl_tab_rollback(tab, snap))
3393 return isl_ineq_error;
3394 return type;
3395 error:
3396 return isl_ineq_error;
3399 int isl_tab_track_bmap(struct isl_tab *tab, __isl_take isl_basic_map *bmap)
3401 bmap = isl_basic_map_cow(bmap);
3402 if (!tab || !bmap)
3403 goto error;
3405 if (tab->empty) {
3406 bmap = isl_basic_map_set_to_empty(bmap);
3407 if (!bmap)
3408 goto error;
3409 tab->bmap = bmap;
3410 return 0;
3413 isl_assert(tab->mat->ctx, tab->n_eq == bmap->n_eq, goto error);
3414 isl_assert(tab->mat->ctx,
3415 tab->n_con == bmap->n_eq + bmap->n_ineq, goto error);
3417 tab->bmap = bmap;
3419 return 0;
3420 error:
3421 isl_basic_map_free(bmap);
3422 return -1;
3425 int isl_tab_track_bset(struct isl_tab *tab, __isl_take isl_basic_set *bset)
3427 return isl_tab_track_bmap(tab, (isl_basic_map *)bset);
3430 __isl_keep isl_basic_set *isl_tab_peek_bset(struct isl_tab *tab)
3432 if (!tab)
3433 return NULL;
3435 return (isl_basic_set *)tab->bmap;
3438 static void isl_tab_print_internal(__isl_keep struct isl_tab *tab,
3439 FILE *out, int indent)
3441 unsigned r, c;
3442 int i;
3444 if (!tab) {
3445 fprintf(out, "%*snull tab\n", indent, "");
3446 return;
3448 fprintf(out, "%*sn_redundant: %d, n_dead: %d", indent, "",
3449 tab->n_redundant, tab->n_dead);
3450 if (tab->rational)
3451 fprintf(out, ", rational");
3452 if (tab->empty)
3453 fprintf(out, ", empty");
3454 fprintf(out, "\n");
3455 fprintf(out, "%*s[", indent, "");
3456 for (i = 0; i < tab->n_var; ++i) {
3457 if (i)
3458 fprintf(out, (i == tab->n_param ||
3459 i == tab->n_var - tab->n_div) ? "; "
3460 : ", ");
3461 fprintf(out, "%c%d%s", tab->var[i].is_row ? 'r' : 'c',
3462 tab->var[i].index,
3463 tab->var[i].is_zero ? " [=0]" :
3464 tab->var[i].is_redundant ? " [R]" : "");
3466 fprintf(out, "]\n");
3467 fprintf(out, "%*s[", indent, "");
3468 for (i = 0; i < tab->n_con; ++i) {
3469 if (i)
3470 fprintf(out, ", ");
3471 fprintf(out, "%c%d%s", tab->con[i].is_row ? 'r' : 'c',
3472 tab->con[i].index,
3473 tab->con[i].is_zero ? " [=0]" :
3474 tab->con[i].is_redundant ? " [R]" : "");
3476 fprintf(out, "]\n");
3477 fprintf(out, "%*s[", indent, "");
3478 for (i = 0; i < tab->n_row; ++i) {
3479 const char *sign = "";
3480 if (i)
3481 fprintf(out, ", ");
3482 if (tab->row_sign) {
3483 if (tab->row_sign[i] == isl_tab_row_unknown)
3484 sign = "?";
3485 else if (tab->row_sign[i] == isl_tab_row_neg)
3486 sign = "-";
3487 else if (tab->row_sign[i] == isl_tab_row_pos)
3488 sign = "+";
3489 else
3490 sign = "+-";
3492 fprintf(out, "r%d: %d%s%s", i, tab->row_var[i],
3493 isl_tab_var_from_row(tab, i)->is_nonneg ? " [>=0]" : "", sign);
3495 fprintf(out, "]\n");
3496 fprintf(out, "%*s[", indent, "");
3497 for (i = 0; i < tab->n_col; ++i) {
3498 if (i)
3499 fprintf(out, ", ");
3500 fprintf(out, "c%d: %d%s", i, tab->col_var[i],
3501 var_from_col(tab, i)->is_nonneg ? " [>=0]" : "");
3503 fprintf(out, "]\n");
3504 r = tab->mat->n_row;
3505 tab->mat->n_row = tab->n_row;
3506 c = tab->mat->n_col;
3507 tab->mat->n_col = 2 + tab->M + tab->n_col;
3508 isl_mat_print_internal(tab->mat, out, indent);
3509 tab->mat->n_row = r;
3510 tab->mat->n_col = c;
3511 if (tab->bmap)
3512 isl_basic_map_print_internal(tab->bmap, out, indent);
3515 void isl_tab_dump(__isl_keep struct isl_tab *tab)
3517 isl_tab_print_internal(tab, stderr, 0);