Polly 23.0.0git
isl_tab.c
Go to the documentation of this file.
1/*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2013 Ecole Normale Superieure
4 * Copyright 2014 INRIA Rocquencourt
5 * Copyright 2016 Sven Verdoolaege
6 *
7 * Use of this software is governed by the MIT license
8 *
9 * Written by Sven Verdoolaege, K.U.Leuven, Departement
10 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
11 * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
12 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
13 * B.P. 105 - 78153 Le Chesnay, France
14 */
15
16#include <isl_ctx_private.h>
17#include <isl_mat_private.h>
18#include <isl_vec_private.h>
19#include "isl_map_private.h"
20#include "isl_tab.h"
21#include <isl_seq.h>
22#include <isl_config.h>
23
24#include <bset_to_bmap.c>
25#include <bset_from_bmap.c>
26
27/*
28 * The implementation of tableaus in this file was inspired by Section 8
29 * of David Detlefs, Greg Nelson and James B. Saxe, "Simplify: a theorem
30 * prover for program checking".
31 */
32
33struct isl_tab *isl_tab_alloc(struct isl_ctx *ctx,
34 unsigned n_row, unsigned n_var, unsigned M)
35{
36 int i;
37 struct isl_tab *tab;
38 unsigned off = 2 + M;
39
40 tab = isl_calloc_type(ctx, struct isl_tab);
41 if (!tab)
42 return NULL;
43 tab->mat = isl_mat_alloc(ctx, n_row, off + n_var);
44 if (!tab->mat)
45 goto error;
46 tab->var = isl_alloc_array(ctx, struct isl_tab_var, n_var);
47 if (n_var && !tab->var)
48 goto error;
49 tab->con = isl_alloc_array(ctx, struct isl_tab_var, n_row);
50 if (n_row && !tab->con)
51 goto error;
52 tab->col_var = isl_alloc_array(ctx, int, n_var);
53 if (n_var && !tab->col_var)
54 goto error;
55 tab->row_var = isl_alloc_array(ctx, int, n_row);
56 if (n_row && !tab->row_var)
57 goto error;
58 for (i = 0; i < n_var; ++i) {
59 tab->var[i].index = i;
60 tab->var[i].is_row = 0;
61 tab->var[i].is_nonneg = 0;
62 tab->var[i].is_zero = 0;
63 tab->var[i].is_redundant = 0;
64 tab->var[i].frozen = 0;
65 tab->var[i].negated = 0;
66 tab->col_var[i] = i;
67 }
68 tab->n_row = 0;
69 tab->n_con = 0;
70 tab->n_eq = 0;
71 tab->max_con = n_row;
72 tab->n_col = n_var;
73 tab->n_var = n_var;
74 tab->max_var = n_var;
75 tab->n_param = 0;
76 tab->n_div = 0;
77 tab->n_dead = 0;
78 tab->n_redundant = 0;
79 tab->strict_redundant = 0;
80 tab->need_undo = 0;
81 tab->rational = 0;
82 tab->empty = 0;
83 tab->in_undo = 0;
84 tab->M = M;
85 tab->cone = 0;
87 tab->bottom.next = NULL;
88 tab->top = &tab->bottom;
89
90 tab->n_zero = 0;
91 tab->n_unbounded = 0;
92 tab->basis = NULL;
93
94 return tab;
95error:
96 isl_tab_free(tab);
97 return NULL;
98}
99
101{
102 return tab ? isl_mat_get_ctx(tab->mat) : NULL;
103}
104
105int isl_tab_extend_cons(struct isl_tab *tab, unsigned n_new)
106{
107 unsigned off;
108 isl_ctx *ctx;
109
110 if (!tab)
111 return -1;
112
113 off = 2 + tab->M;
114
115 ctx = isl_tab_get_ctx(tab);
116 if (tab->max_con < tab->n_con + n_new) {
117 struct isl_tab_var *con;
118
119 con = isl_realloc_array(ctx, tab->con,
120 struct isl_tab_var, tab->max_con + n_new);
121 if (!con)
122 return -1;
123 tab->con = con;
124 tab->max_con += n_new;
125 }
126 if (tab->mat->n_row < tab->n_row + n_new) {
127 int *row_var;
128
129 tab->mat = isl_mat_extend(tab->mat,
130 tab->n_row + n_new, off + tab->n_col);
131 if (!tab->mat)
132 return -1;
133 row_var = isl_realloc_array(ctx, tab->row_var,
134 int, tab->mat->n_row);
135 if (!row_var)
136 return -1;
137 tab->row_var = row_var;
138 if (tab->row_sign) {
139 enum isl_tab_row_sign *s;
140 s = isl_realloc_array(ctx, tab->row_sign,
141 enum isl_tab_row_sign, tab->mat->n_row);
142 if (!s)
143 return -1;
144 tab->row_sign = s;
145 }
146 }
147 return 0;
148}
149
150/* Make room for at least n_new extra variables.
151 * Return -1 if anything went wrong.
152 */
153int isl_tab_extend_vars(struct isl_tab *tab, unsigned n_new)
154{
155 struct isl_tab_var *var;
156 unsigned off = 2 + tab->M;
157
158 if (tab->max_var < tab->n_var + n_new) {
159 var = isl_realloc_array(tab->mat->ctx, tab->var,
160 struct isl_tab_var, tab->n_var + n_new);
161 if (!var)
162 return -1;
163 tab->var = var;
164 tab->max_var = tab->n_var + n_new;
165 }
166
167 if (tab->mat->n_col < off + tab->n_col + n_new) {
168 int *p;
169
170 tab->mat = isl_mat_extend(tab->mat,
171 tab->mat->n_row, off + tab->n_col + n_new);
172 if (!tab->mat)
173 return -1;
174 p = isl_realloc_array(tab->mat->ctx, tab->col_var,
175 int, tab->n_col + n_new);
176 if (!p)
177 return -1;
178 tab->col_var = p;
179 }
180
181 return 0;
182}
183
184static void free_undo_record(struct isl_tab_undo *undo)
185{
186 switch (undo->type) {
188 free(undo->u.col_var);
189 break;
190 default:;
191 }
192 free(undo);
193}
194
195static void free_undo(struct isl_tab *tab)
196{
197 struct isl_tab_undo *undo, *next;
198
199 for (undo = tab->top; undo && undo != &tab->bottom; undo = next) {
200 next = undo->next;
201 free_undo_record(undo);
202 }
203 tab->top = undo;
204}
205
206void isl_tab_free(struct isl_tab *tab)
207{
208 if (!tab)
209 return;
210 free_undo(tab);
211 isl_mat_free(tab->mat);
212 isl_vec_free(tab->dual);
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);
223}
224
225struct isl_tab *isl_tab_dup(struct isl_tab *tab)
226{
227 int i;
228 struct isl_tab *dup;
229 unsigned off;
230
231 if (!tab)
232 return NULL;
233
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];
268 }
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;
279 }
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 dup->cone = tab->cone;
298 dup->bottom.type = isl_tab_undo_bottom;
299 dup->bottom.next = NULL;
300 dup->top = &dup->bottom;
301
302 dup->n_zero = tab->n_zero;
303 dup->n_unbounded = tab->n_unbounded;
304 dup->basis = isl_mat_dup(tab->basis);
305
306 return dup;
307error:
309 return NULL;
310}
311
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}
321 *
322 * The order of the rows and columns in the result is as explained
323 * in isl_tab_product.
324 */
326 __isl_keep isl_mat *mat2, unsigned row1, unsigned row2,
327 unsigned col1, unsigned col2,
328 unsigned off, unsigned r1, unsigned r2, unsigned d1, unsigned d2)
329{
330 int i;
331 struct isl_mat *prod;
332 unsigned n;
333
334 prod = isl_mat_alloc(mat1->ctx, mat1->n_row + mat2->n_row,
335 off + col1 + col2);
336 if (!prod)
337 return NULL;
338
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);
346 }
347
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);
357 }
358
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);
366 }
367
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);
377 }
378
379 return prod;
380}
381
382/* Update the row or column index of a variable that corresponds
383 * to a variable in the first input tableau.
384 */
385static void update_index1(struct isl_tab_var *var,
386 unsigned r1, unsigned r2, unsigned d1, unsigned d2)
387{
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;
394}
395
396/* Update the row or column index of a variable that corresponds
397 * to a variable in the second input tableau.
398 */
399static void update_index2(struct isl_tab_var *var,
400 unsigned row1, unsigned col1,
401 unsigned r1, unsigned r2, unsigned d1, unsigned d2)
402{
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;
415 }
416}
417
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.
435 */
436struct isl_tab *isl_tab_product(struct isl_tab *tab1, struct isl_tab *tab2)
437{
438 int i;
439 struct isl_tab *prod;
440 unsigned off;
441 unsigned r1, r2, d1, d2;
442
443 if (!tab1 || !tab2)
444 return NULL;
445
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);
455
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);
476 }
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);
482 }
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);
490 }
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);
496 }
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];
504 }
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;
513 }
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];
521 }
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;
530 }
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;
547 prod->need_undo = 0;
548 prod->in_undo = 0;
549 prod->M = tab1->M;
550 prod->cone = tab1->cone;
552 prod->bottom.next = NULL;
553 prod->top = &prod->bottom;
554
555 prod->n_zero = 0;
556 prod->n_unbounded = 0;
557 prod->basis = NULL;
558
559 return prod;
560error:
561 isl_tab_free(prod);
562 return NULL;
563}
564
565static struct isl_tab_var *var_from_index(struct isl_tab *tab, int i)
566{
567 if (i >= 0)
568 return &tab->var[i];
569 else
570 return &tab->con[~i];
571}
572
573struct isl_tab_var *isl_tab_var_from_row(struct isl_tab *tab, int i)
574{
575 return var_from_index(tab, tab->row_var[i]);
576}
577
578static struct isl_tab_var *var_from_col(struct isl_tab *tab, int i)
579{
580 return var_from_index(tab, tab->col_var[i]);
581}
582
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.
586 */
588 struct isl_tab_var *var)
589{
590 int i;
591 unsigned off = 2 + tab->M;
592
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;
600 }
601 return 1;
602}
603
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.
607 */
609 struct isl_tab_var *var)
610{
611 int i;
612 unsigned off = 2 + tab->M;
613
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;
621 }
622 return 1;
623}
624
625static int row_cmp(struct isl_tab *tab, int r1, int r2, int c, isl_int *t)
626{
627 unsigned off = 2 + tab->M;
628
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;
636 }
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);
640}
641
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".
647 *
648 * Each row in the tableau is of the form
649 *
650 * x_r = a_r0 + \sum_i a_ri x_i
651 *
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".
661 */
662static int pivot_row(struct isl_tab *tab,
663 struct isl_tab_var *var, int sgn, int c)
664{
665 int j, r, tsgn;
666 isl_int t;
667 unsigned off = 2 + tab->M;
668
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;
681 }
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;
686 }
688 return r;
689}
690
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.
695 *
696 * As the given row in the tableau is of the form
697 *
698 * x_r = a_r0 + \sum_i a_ri x_i
699 *
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.
706 */
707static 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)
710{
711 int j, r, c;
712 isl_int *tr;
713
714 *row = *col = -1;
715
716 isl_assert(tab->mat->ctx, var->is_row, return);
717 tr = tab->mat->row[var->index] + 2 + tab->M;
718
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;
728 }
729 if (c < 0)
730 return;
731
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;
736}
737
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.
743 */
744int isl_tab_row_is_redundant(struct isl_tab *tab, int row)
745{
746 int i;
747 unsigned off = 2 + tab->M;
748
749 if (tab->row_var[row] < 0 && !isl_tab_var_from_row(tab, row)->is_nonneg)
750 return 0;
751
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;
758
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;
768 }
769 return 1;
770}
771
772static void swap_rows(struct isl_tab *tab, int row1, int row2)
773{
774 int t;
775 enum isl_tab_row_sign s;
776
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);
783
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;
789}
790
791static isl_stat push_union(struct isl_tab *tab,
793
794/* Push record "u" onto the undo stack of "tab", provided "tab"
795 * keeps track of undo information.
796 *
797 * If the record cannot be pushed, then mark the undo stack as invalid
798 * such that a later rollback attempt will not try to undo earlier
799 * records without having been able to undo the current record.
800 */
801static isl_stat push_union(struct isl_tab *tab,
803{
804 struct isl_tab_undo *undo;
805
806 if (!tab)
807 return isl_stat_error;
808 if (!tab->need_undo)
809 return isl_stat_ok;
810
811 undo = isl_alloc_type(tab->mat->ctx, struct isl_tab_undo);
812 if (!undo)
813 goto error;
814 undo->type = type;
815 undo->u = u;
816 undo->next = tab->top;
817 tab->top = undo;
818
819 return isl_stat_ok;
820error:
821 free_undo(tab);
822 tab->top = NULL;
823 return isl_stat_error;
824}
825
828{
829 union isl_tab_undo_val u;
830 if (var->is_row)
831 u.var_index = tab->row_var[var->index];
832 else
833 u.var_index = tab->col_var[var->index];
834 return push_union(tab, type, u);
835}
836
838{
839 union isl_tab_undo_val u = { 0 };
840 return push_union(tab, type, u);
841}
842
843/* Push a record on the undo stack describing the current basic
844 * variables, so that the this state can be restored during rollback.
845 */
847{
848 int i;
849 union isl_tab_undo_val u;
850
851 u.col_var = isl_alloc_array(tab->mat->ctx, int, tab->n_col);
852 if (tab->n_col && !u.col_var)
853 return isl_stat_error;
854 for (i = 0; i < tab->n_col; ++i)
855 u.col_var[i] = tab->col_var[i];
856 return push_union(tab, isl_tab_undo_saved_basis, u);
857}
858
861{
862 union isl_tab_undo_val u;
863 u.callback = callback;
864 return push_union(tab, isl_tab_undo_callback, u);
865}
866
867/* Push a record onto the undo stack indicating that inequality "ineq"
868 * has been turned into an equality constraint (in the first position).
869 */
870static isl_stat isl_tab_push_ineq_to_eq(struct isl_tab *tab, int ineq)
871{
872 union isl_tab_undo_val u = { .n = ineq };
873
874 return push_union(tab, isl_tab_undo_ineq_to_eq, u);
875}
876
878{
879 if (!tab)
880 return NULL;
881
882 tab->n_sample = 0;
883 tab->n_outside = 0;
884 tab->samples = isl_mat_alloc(tab->mat->ctx, 1, 1 + tab->n_var);
885 if (!tab->samples)
886 goto error;
887 tab->sample_index = isl_alloc_array(tab->mat->ctx, int, 1);
888 if (!tab->sample_index)
889 goto error;
890 return tab;
891error:
892 isl_tab_free(tab);
893 return NULL;
894}
895
897{
898 if (!tab || !sample)
899 goto error;
900
901 if (tab->n_sample + 1 > tab->samples->n_row) {
902 int *t = isl_realloc_array(tab->mat->ctx,
903 tab->sample_index, int, tab->n_sample + 1);
904 if (!t)
905 goto error;
906 tab->sample_index = t;
907 }
908
909 tab->samples = isl_mat_extend(tab->samples,
910 tab->n_sample + 1, tab->samples->n_col);
911 if (!tab->samples)
912 goto error;
913
914 isl_seq_cpy(tab->samples->row[tab->n_sample], sample->el, sample->size);
915 isl_vec_free(sample);
916 tab->sample_index[tab->n_sample] = tab->n_sample;
917 tab->n_sample++;
918
919 return 0;
920error:
921 isl_vec_free(sample);
922 return -1;
923}
924
925struct isl_tab *isl_tab_drop_sample(struct isl_tab *tab, int s)
926{
927 if (s != tab->n_outside) {
928 int t = tab->sample_index[tab->n_outside];
929 tab->sample_index[tab->n_outside] = tab->sample_index[s];
930 tab->sample_index[s] = t;
931 isl_mat_swap_rows(tab->samples, tab->n_outside, s);
932 }
933 tab->n_outside++;
935 isl_tab_free(tab);
936 return NULL;
937 }
938
939 return tab;
940}
941
942/* Record the current number of samples so that we can remove newer
943 * samples during a rollback.
944 */
946{
947 union isl_tab_undo_val u;
948
949 if (!tab)
950 return isl_stat_error;
951
952 u.n = tab->n_sample;
954}
955
956/* Mark row with index "row" as being redundant.
957 * If we may need to undo the operation or if the row represents
958 * a variable of the original problem, the row is kept,
959 * but no longer considered when looking for a pivot row.
960 * Otherwise, the row is simply removed.
961 *
962 * The row may be interchanged with some other row. If it
963 * is interchanged with a later row, return 1. Otherwise return 0.
964 * If the rows are checked in order in the calling function,
965 * then a return value of 1 means that the row with the given
966 * row number may now contain a different row that hasn't been checked yet.
967 */
968int isl_tab_mark_redundant(struct isl_tab *tab, int row)
969{
970 struct isl_tab_var *var = isl_tab_var_from_row(tab, row);
971 var->is_redundant = 1;
972 isl_assert(tab->mat->ctx, row >= tab->n_redundant, return -1);
973 if (tab->preserve || tab->need_undo || tab->row_var[row] >= 0) {
974 if (tab->row_var[row] >= 0 && !var->is_nonneg) {
975 var->is_nonneg = 1;
977 return -1;
978 }
979 if (row != tab->n_redundant)
980 swap_rows(tab, row, tab->n_redundant);
981 tab->n_redundant++;
983 } else {
984 if (row != tab->n_row - 1)
985 swap_rows(tab, row, tab->n_row - 1);
986 isl_tab_var_from_row(tab, tab->n_row - 1)->index = -1;
987 tab->n_row--;
988 return 1;
989 }
990}
991
992/* Mark "tab" as a rational tableau.
993 * If it wasn't marked as a rational tableau already and if we may
994 * need to undo changes, then arrange for the marking to be undone
995 * during the undo.
996 */
998{
999 if (!tab)
1000 return -1;
1001 if (!tab->rational && tab->need_undo)
1002 if (isl_tab_push(tab, isl_tab_undo_rational) < 0)
1003 return -1;
1004 tab->rational = 1;
1005 return 0;
1006}
1007
1009{
1010 if (!tab)
1011 return isl_stat_error;
1012 if (!tab->empty && tab->need_undo)
1013 if (isl_tab_push(tab, isl_tab_undo_empty) < 0)
1014 return isl_stat_error;
1015 tab->empty = 1;
1016 return isl_stat_ok;
1017}
1018
1019int isl_tab_freeze_constraint(struct isl_tab *tab, int con)
1020{
1021 struct isl_tab_var *var;
1022
1023 if (!tab)
1024 return -1;
1025
1026 var = &tab->con[con];
1027 if (var->frozen)
1028 return 0;
1029 if (var->index < 0)
1030 return 0;
1031 var->frozen = 1;
1032
1033 if (tab->need_undo)
1035
1036 return 0;
1037}
1038
1039/* Update the rows signs after a pivot of "row" and "col", with "row_sgn"
1040 * the original sign of the pivot element.
1041 * We only keep track of row signs during PILP solving and in this case
1042 * we only pivot a row with negative sign (meaning the value is always
1043 * non-positive) using a positive pivot element.
1044 *
1045 * For each row j, the new value of the parametric constant is equal to
1046 *
1047 * a_j0 - a_jc a_r0/a_rc
1048 *
1049 * where a_j0 is the original parametric constant, a_rc is the pivot element,
1050 * a_r0 is the parametric constant of the pivot row and a_jc is the
1051 * pivot column entry of the row j.
1052 * Since a_r0 is non-positive and a_rc is positive, the sign of row j
1053 * remains the same if a_jc has the same sign as the row j or if
1054 * a_jc is zero. In all other cases, we reset the sign to "unknown".
1055 */
1056static void update_row_sign(struct isl_tab *tab, int row, int col, int row_sgn)
1057{
1058 int i;
1059 struct isl_mat *mat = tab->mat;
1060 unsigned off = 2 + tab->M;
1061
1062 if (!tab->row_sign)
1063 return;
1064
1065 if (tab->row_sign[row] == 0)
1066 return;
1067 isl_assert(mat->ctx, row_sgn > 0, return);
1068 isl_assert(mat->ctx, tab->row_sign[row] == isl_tab_row_neg, return);
1070 for (i = 0; i < tab->n_row; ++i) {
1071 int s;
1072 if (i == row)
1073 continue;
1074 s = isl_int_sgn(mat->row[i][off + col]);
1075 if (!s)
1076 continue;
1077 if (!tab->row_sign[i])
1078 continue;
1079 if (s < 0 && tab->row_sign[i] == isl_tab_row_neg)
1080 continue;
1081 if (s > 0 && tab->row_sign[i] == isl_tab_row_pos)
1082 continue;
1083 tab->row_sign[i] = isl_tab_row_unknown;
1084 }
1085}
1086
1087/* Given a row number "row" and a column number "col", pivot the tableau
1088 * such that the associated variables are interchanged.
1089 * The given row in the tableau expresses
1090 *
1091 * x_r = a_r0 + \sum_i a_ri x_i
1092 *
1093 * or
1094 *
1095 * x_c = 1/a_rc x_r - a_r0/a_rc + sum_{i \ne r} -a_ri/a_rc
1096 *
1097 * Substituting this equality into the other rows
1098 *
1099 * x_j = a_j0 + \sum_i a_ji x_i
1100 *
1101 * with a_jc \ne 0, we obtain
1102 *
1103 * 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
1104 *
1105 * The tableau
1106 *
1107 * n_rc/d_r n_ri/d_r
1108 * n_jc/d_j n_ji/d_j
1109 *
1110 * where i is any other column and j is any other row,
1111 * is therefore transformed into
1112 *
1113 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1114 * 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)
1115 *
1116 * The transformation is performed along the following steps
1117 *
1118 * d_r/n_rc n_ri/n_rc
1119 * n_jc/d_j n_ji/d_j
1120 *
1121 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1122 * n_jc/d_j n_ji/d_j
1123 *
1124 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1125 * n_jc/(|n_rc| d_j) n_ji/(|n_rc| d_j)
1126 *
1127 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1128 * n_jc/(|n_rc| d_j) (n_ji |n_rc|)/(|n_rc| d_j)
1129 *
1130 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1131 * n_jc/(|n_rc| d_j) (n_ji |n_rc| - s(n_rc)n_jc n_ri)/(|n_rc| d_j)
1132 *
1133 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1134 * 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)
1135 *
1136 */
1137int isl_tab_pivot(struct isl_tab *tab, int row, int col)
1138{
1139 int i, j;
1140 int sgn;
1141 int t;
1142 isl_ctx *ctx;
1143 struct isl_mat *mat = tab->mat;
1144 struct isl_tab_var *var;
1145 unsigned off = 2 + tab->M;
1146
1147 ctx = isl_tab_get_ctx(tab);
1148 if (isl_ctx_next_operation(ctx) < 0)
1149 return -1;
1150
1151 isl_int_swap(mat->row[row][0], mat->row[row][off + col]);
1152 sgn = isl_int_sgn(mat->row[row][0]);
1153 if (sgn < 0) {
1154 isl_int_neg(mat->row[row][0], mat->row[row][0]);
1155 isl_int_neg(mat->row[row][off + col], mat->row[row][off + col]);
1156 } else
1157 for (j = 0; j < off - 1 + tab->n_col; ++j) {
1158 if (j == off - 1 + col)
1159 continue;
1160 isl_int_neg(mat->row[row][1 + j], mat->row[row][1 + j]);
1161 }
1162 if (!isl_int_is_one(mat->row[row][0]))
1163 isl_seq_normalize(mat->ctx, mat->row[row], off + tab->n_col);
1164 for (i = 0; i < tab->n_row; ++i) {
1165 if (i == row)
1166 continue;
1167 if (isl_int_is_zero(mat->row[i][off + col]))
1168 continue;
1169 isl_int_mul(mat->row[i][0], mat->row[i][0], mat->row[row][0]);
1170 for (j = 0; j < off - 1 + tab->n_col; ++j) {
1171 if (j == off - 1 + col)
1172 continue;
1173 isl_int_mul(mat->row[i][1 + j],
1174 mat->row[i][1 + j], mat->row[row][0]);
1175 isl_int_addmul(mat->row[i][1 + j],
1176 mat->row[i][off + col], mat->row[row][1 + j]);
1177 }
1178 isl_int_mul(mat->row[i][off + col],
1179 mat->row[i][off + col], mat->row[row][off + col]);
1180 if (!isl_int_is_one(mat->row[i][0]))
1181 isl_seq_normalize(mat->ctx, mat->row[i], off + tab->n_col);
1182 }
1183 t = tab->row_var[row];
1184 tab->row_var[row] = tab->col_var[col];
1185 tab->col_var[col] = t;
1186 var = isl_tab_var_from_row(tab, row);
1187 var->is_row = 1;
1188 var->index = row;
1189 var = var_from_col(tab, col);
1190 var->is_row = 0;
1191 var->index = col;
1192 update_row_sign(tab, row, col, sgn);
1193 if (tab->in_undo)
1194 return 0;
1195 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1196 if (isl_int_is_zero(mat->row[i][off + col]))
1197 continue;
1198 if (!isl_tab_var_from_row(tab, i)->frozen &&
1199 isl_tab_row_is_redundant(tab, i)) {
1200 int redo = isl_tab_mark_redundant(tab, i);
1201 if (redo < 0)
1202 return -1;
1203 if (redo)
1204 --i;
1205 }
1206 }
1207 return 0;
1208}
1209
1210/* If "var" represents a column variable, then pivot is up (sgn > 0)
1211 * or down (sgn < 0) to a row. The variable is assumed not to be
1212 * unbounded in the specified direction.
1213 * If sgn = 0, then the variable is unbounded in both directions,
1214 * and we pivot with any row we can find.
1215 */
1216static int to_row(struct isl_tab *tab, struct isl_tab_var *var, int sign) WARN_UNUSED;
1217static int to_row(struct isl_tab *tab, struct isl_tab_var *var, int sign)
1218{
1219 int r;
1220 unsigned off = 2 + tab->M;
1221
1222 if (var->is_row)
1223 return 0;
1224
1225 if (sign == 0) {
1226 for (r = tab->n_redundant; r < tab->n_row; ++r)
1227 if (!isl_int_is_zero(tab->mat->row[r][off+var->index]))
1228 break;
1229 isl_assert(tab->mat->ctx, r < tab->n_row, return -1);
1230 } else {
1231 r = pivot_row(tab, NULL, sign, var->index);
1232 isl_assert(tab->mat->ctx, r >= 0, return -1);
1233 }
1234
1235 return isl_tab_pivot(tab, r, var->index);
1236}
1237
1238/* Check whether all variables that are marked as non-negative
1239 * also have a non-negative sample value. This function is not
1240 * called from the current code but is useful during debugging.
1241 */
1242static void check_table(struct isl_tab *tab) __attribute__ ((unused));
1243static void check_table(struct isl_tab *tab)
1244{
1245 int i;
1246
1247 if (tab->empty)
1248 return;
1249 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1250 struct isl_tab_var *var;
1251 var = isl_tab_var_from_row(tab, i);
1252 if (!var->is_nonneg)
1253 continue;
1254 if (tab->M) {
1255 isl_assert(tab->mat->ctx,
1256 !isl_int_is_neg(tab->mat->row[i][2]), abort());
1257 if (isl_int_is_pos(tab->mat->row[i][2]))
1258 continue;
1259 }
1260 isl_assert(tab->mat->ctx, !isl_int_is_neg(tab->mat->row[i][1]),
1261 abort());
1262 }
1263}
1264
1265/* Return the sign of the maximal value of "var".
1266 * If the sign is not negative, then on return from this function,
1267 * the sample value will also be non-negative.
1268 *
1269 * If "var" is manifestly unbounded wrt positive values, we are done.
1270 * Otherwise, we pivot the variable up to a row if needed.
1271 * Then we continue pivoting up until either
1272 * - no more up pivots can be performed
1273 * - the sample value is positive
1274 * - the variable is pivoted into a manifestly unbounded column
1275 */
1276static int sign_of_max(struct isl_tab *tab, struct isl_tab_var *var)
1277{
1278 int row, col;
1279
1281 return 1;
1282 if (to_row(tab, var, 1) < 0)
1283 return -2;
1284 while (!isl_int_is_pos(tab->mat->row[var->index][1])) {
1285 find_pivot(tab, var, var, 1, &row, &col);
1286 if (row == -1)
1287 return isl_int_sgn(tab->mat->row[var->index][1]);
1288 if (isl_tab_pivot(tab, row, col) < 0)
1289 return -2;
1290 if (!var->is_row) /* manifestly unbounded */
1291 return 1;
1292 }
1293 return 1;
1294}
1295
1296int isl_tab_sign_of_max(struct isl_tab *tab, int con)
1297{
1298 struct isl_tab_var *var;
1299
1300 if (!tab)
1301 return -2;
1302
1303 var = &tab->con[con];
1304 isl_assert(tab->mat->ctx, !var->is_redundant, return -2);
1305 isl_assert(tab->mat->ctx, !var->is_zero, return -2);
1306
1307 return sign_of_max(tab, var);
1308}
1309
1310static int row_is_neg(struct isl_tab *tab, int row)
1311{
1312 if (!tab->M)
1313 return isl_int_is_neg(tab->mat->row[row][1]);
1314 if (isl_int_is_pos(tab->mat->row[row][2]))
1315 return 0;
1316 if (isl_int_is_neg(tab->mat->row[row][2]))
1317 return 1;
1318 return isl_int_is_neg(tab->mat->row[row][1]);
1319}
1320
1321static int row_sgn(struct isl_tab *tab, int row)
1322{
1323 if (!tab->M)
1324 return isl_int_sgn(tab->mat->row[row][1]);
1325 if (!isl_int_is_zero(tab->mat->row[row][2]))
1326 return isl_int_sgn(tab->mat->row[row][2]);
1327 else
1328 return isl_int_sgn(tab->mat->row[row][1]);
1329}
1330
1331/* Perform pivots until the row variable "var" has a non-negative
1332 * sample value or until no more upward pivots can be performed.
1333 * Return the sign of the sample value after the pivots have been
1334 * performed.
1335 */
1336static int restore_row(struct isl_tab *tab, struct isl_tab_var *var)
1337{
1338 int row, col;
1339
1340 while (row_is_neg(tab, var->index)) {
1341 find_pivot(tab, var, var, 1, &row, &col);
1342 if (row == -1)
1343 break;
1344 if (isl_tab_pivot(tab, row, col) < 0)
1345 return -2;
1346 if (!var->is_row) /* manifestly unbounded */
1347 return 1;
1348 }
1349 return row_sgn(tab, var->index);
1350}
1351
1352/* Perform pivots until we are sure that the row variable "var"
1353 * can attain non-negative values. After return from this
1354 * function, "var" is still a row variable, but its sample
1355 * value may not be non-negative, even if the function returns 1.
1356 */
1357static int at_least_zero(struct isl_tab *tab, struct isl_tab_var *var)
1358{
1359 int row, col;
1360
1361 while (isl_int_is_neg(tab->mat->row[var->index][1])) {
1362 find_pivot(tab, var, var, 1, &row, &col);
1363 if (row == -1)
1364 break;
1365 if (row == var->index) /* manifestly unbounded */
1366 return 1;
1367 if (isl_tab_pivot(tab, row, col) < 0)
1368 return -1;
1369 }
1370 return !isl_int_is_neg(tab->mat->row[var->index][1]);
1371}
1372
1373/* Return a negative value if "var" can attain negative values.
1374 * Return a non-negative value otherwise.
1375 *
1376 * If "var" is manifestly unbounded wrt negative values, we are done.
1377 * Otherwise, if var is in a column, we can pivot it down to a row.
1378 * Then we continue pivoting down until either
1379 * - the pivot would result in a manifestly unbounded column
1380 * => we don't perform the pivot, but simply return -1
1381 * - no more down pivots can be performed
1382 * - the sample value is negative
1383 * If the sample value becomes negative and the variable is supposed
1384 * to be nonnegative, then we undo the last pivot.
1385 * However, if the last pivot has made the pivoting variable
1386 * obviously redundant, then it may have moved to another row.
1387 * In that case we look for upward pivots until we reach a non-negative
1388 * value again.
1389 */
1390static int sign_of_min(struct isl_tab *tab, struct isl_tab_var *var)
1391{
1392 int row, col;
1393 struct isl_tab_var *pivot_var = NULL;
1394
1396 return -1;
1397 if (!var->is_row) {
1398 col = var->index;
1399 row = pivot_row(tab, NULL, -1, col);
1400 pivot_var = var_from_col(tab, col);
1401 if (isl_tab_pivot(tab, row, col) < 0)
1402 return -2;
1403 if (var->is_redundant)
1404 return 0;
1405 if (isl_int_is_neg(tab->mat->row[var->index][1])) {
1406 if (var->is_nonneg) {
1407 if (!pivot_var->is_redundant &&
1408 pivot_var->index == row) {
1409 if (isl_tab_pivot(tab, row, col) < 0)
1410 return -2;
1411 } else
1412 if (restore_row(tab, var) < -1)
1413 return -2;
1414 }
1415 return -1;
1416 }
1417 }
1418 if (var->is_redundant)
1419 return 0;
1420 while (!isl_int_is_neg(tab->mat->row[var->index][1])) {
1421 find_pivot(tab, var, var, -1, &row, &col);
1422 if (row == var->index)
1423 return -1;
1424 if (row == -1)
1425 return isl_int_sgn(tab->mat->row[var->index][1]);
1426 pivot_var = var_from_col(tab, col);
1427 if (isl_tab_pivot(tab, row, col) < 0)
1428 return -2;
1429 if (var->is_redundant)
1430 return 0;
1431 }
1432 if (pivot_var && var->is_nonneg) {
1433 /* pivot back to non-negative value */
1434 if (!pivot_var->is_redundant && pivot_var->index == row) {
1435 if (isl_tab_pivot(tab, row, col) < 0)
1436 return -2;
1437 } else
1438 if (restore_row(tab, var) < -1)
1439 return -2;
1440 }
1441 return -1;
1442}
1443
1444static int row_at_most_neg_one(struct isl_tab *tab, int row)
1445{
1446 if (tab->M) {
1447 if (isl_int_is_pos(tab->mat->row[row][2]))
1448 return 0;
1449 if (isl_int_is_neg(tab->mat->row[row][2]))
1450 return 1;
1451 }
1452 return isl_int_is_neg(tab->mat->row[row][1]) &&
1453 isl_int_abs_ge(tab->mat->row[row][1],
1454 tab->mat->row[row][0]);
1455}
1456
1457/* Return 1 if "var" can attain values <= -1.
1458 * Return 0 otherwise.
1459 *
1460 * If the variable "var" is supposed to be non-negative (is_nonneg is set),
1461 * then the sample value of "var" is assumed to be non-negative when the
1462 * the function is called. If 1 is returned then the constraint
1463 * is not redundant and the sample value is made non-negative again before
1464 * the function returns.
1465 */
1467{
1468 int row, col;
1469 struct isl_tab_var *pivot_var;
1470
1472 return 1;
1473 if (!var->is_row) {
1474 col = var->index;
1475 row = pivot_row(tab, NULL, -1, col);
1476 pivot_var = var_from_col(tab, col);
1477 if (isl_tab_pivot(tab, row, col) < 0)
1478 return -1;
1479 if (var->is_redundant)
1480 return 0;
1481 if (row_at_most_neg_one(tab, var->index)) {
1482 if (var->is_nonneg) {
1483 if (!pivot_var->is_redundant &&
1484 pivot_var->index == row) {
1485 if (isl_tab_pivot(tab, row, col) < 0)
1486 return -1;
1487 } else
1488 if (restore_row(tab, var) < -1)
1489 return -1;
1490 }
1491 return 1;
1492 }
1493 }
1494 if (var->is_redundant)
1495 return 0;
1496 do {
1497 find_pivot(tab, var, var, -1, &row, &col);
1498 if (row == var->index) {
1499 if (var->is_nonneg && restore_row(tab, var) < -1)
1500 return -1;
1501 return 1;
1502 }
1503 if (row == -1)
1504 return 0;
1505 pivot_var = var_from_col(tab, col);
1506 if (isl_tab_pivot(tab, row, col) < 0)
1507 return -1;
1508 if (var->is_redundant)
1509 return 0;
1510 } while (!row_at_most_neg_one(tab, var->index));
1511 if (var->is_nonneg) {
1512 /* pivot back to non-negative value */
1513 if (!pivot_var->is_redundant && pivot_var->index == row)
1514 if (isl_tab_pivot(tab, row, col) < 0)
1515 return -1;
1516 if (restore_row(tab, var) < -1)
1517 return -1;
1518 }
1519 return 1;
1520}
1521
1522/* Return 1 if "var" can attain values >= 1.
1523 * Return 0 otherwise.
1524 */
1525static int at_least_one(struct isl_tab *tab, struct isl_tab_var *var)
1526{
1527 int row, col;
1528 isl_int *r;
1529
1531 return 1;
1532 if (to_row(tab, var, 1) < 0)
1533 return -1;
1534 r = tab->mat->row[var->index];
1535 while (isl_int_lt(r[1], r[0])) {
1536 find_pivot(tab, var, var, 1, &row, &col);
1537 if (row == -1)
1538 return isl_int_ge(r[1], r[0]);
1539 if (row == var->index) /* manifestly unbounded */
1540 return 1;
1541 if (isl_tab_pivot(tab, row, col) < 0)
1542 return -1;
1543 }
1544 return 1;
1545}
1546
1547static void swap_cols(struct isl_tab *tab, int col1, int col2)
1548{
1549 int t;
1550 unsigned off = 2 + tab->M;
1551 t = tab->col_var[col1];
1552 tab->col_var[col1] = tab->col_var[col2];
1553 tab->col_var[col2] = t;
1554 var_from_col(tab, col1)->index = col1;
1555 var_from_col(tab, col2)->index = col2;
1556 tab->mat = isl_mat_swap_cols(tab->mat, off + col1, off + col2);
1557}
1558
1559/* Mark column with index "col" as representing a zero variable.
1560 * If we may need to undo the operation the column is kept,
1561 * but no longer considered.
1562 * Otherwise, the column is simply removed.
1563 *
1564 * The column may be interchanged with some other column. If it
1565 * is interchanged with a later column, return 1. Otherwise return 0.
1566 * If the columns are checked in order in the calling function,
1567 * then a return value of 1 means that the column with the given
1568 * column number may now contain a different column that
1569 * hasn't been checked yet.
1570 */
1571int isl_tab_kill_col(struct isl_tab *tab, int col)
1572{
1573 var_from_col(tab, col)->is_zero = 1;
1574 if (tab->need_undo) {
1576 var_from_col(tab, col)) < 0)
1577 return -1;
1578 if (col != tab->n_dead)
1579 swap_cols(tab, col, tab->n_dead);
1580 tab->n_dead++;
1581 return 0;
1582 } else {
1583 if (col != tab->n_col - 1)
1584 swap_cols(tab, col, tab->n_col - 1);
1585 var_from_col(tab, tab->n_col - 1)->index = -1;
1586 tab->n_col--;
1587 return 1;
1588 }
1589}
1590
1591static int row_is_manifestly_non_integral(struct isl_tab *tab, int row)
1592{
1593 unsigned off = 2 + tab->M;
1594
1595 if (tab->M && !isl_int_eq(tab->mat->row[row][2],
1596 tab->mat->row[row][0]))
1597 return 0;
1598 if (isl_seq_any_non_zero(tab->mat->row[row] + off + tab->n_dead,
1599 tab->n_col - tab->n_dead))
1600 return 0;
1601
1602 return !isl_int_is_divisible_by(tab->mat->row[row][1],
1603 tab->mat->row[row][0]);
1604}
1605
1606/* For integer tableaus, check if any of the coordinates are stuck
1607 * at a non-integral value.
1608 */
1609static int tab_is_manifestly_empty(struct isl_tab *tab)
1610{
1611 int i;
1612
1613 if (tab->empty)
1614 return 1;
1615 if (tab->rational)
1616 return 0;
1617
1618 for (i = 0; i < tab->n_var; ++i) {
1619 if (!tab->var[i].is_row)
1620 continue;
1621 if (row_is_manifestly_non_integral(tab, tab->var[i].index))
1622 return 1;
1623 }
1624
1625 return 0;
1626}
1627
1628/* Row variable "var" is non-negative and cannot attain any values
1629 * larger than zero. This means that the coefficients of the unrestricted
1630 * column variables are zero and that the coefficients of the non-negative
1631 * column variables are zero or negative.
1632 * Each of the non-negative variables with a negative coefficient can
1633 * then also be written as the negative sum of non-negative variables
1634 * and must therefore also be zero.
1635 *
1636 * If "temp_var" is set, then "var" is a temporary variable that
1637 * will be removed after this function returns and for which
1638 * no information is recorded on the undo stack.
1639 * Do not add any undo records involving this variable in this case
1640 * since the variable will have been removed before any future undo
1641 * operations. Also avoid marking the variable as redundant,
1642 * since that either adds an undo record or needlessly removes the row
1643 * (the caller will take care of removing the row).
1644 */
1645static isl_stat close_row(struct isl_tab *tab, struct isl_tab_var *var,
1646 int temp_var) WARN_UNUSED;
1647static isl_stat close_row(struct isl_tab *tab, struct isl_tab_var *var,
1648 int temp_var)
1649{
1650 int j;
1651 struct isl_mat *mat = tab->mat;
1652 unsigned off = 2 + tab->M;
1653
1654 if (!var->is_nonneg)
1656 "expecting non-negative variable",
1657 return isl_stat_error);
1658 var->is_zero = 1;
1659 if (!temp_var && tab->need_undo)
1660 if (isl_tab_push_var(tab, isl_tab_undo_zero, var) < 0)
1661 return isl_stat_error;
1662 for (j = tab->n_dead; j < tab->n_col; ++j) {
1663 int recheck;
1664 if (isl_int_is_zero(mat->row[var->index][off + j]))
1665 continue;
1666 if (isl_int_is_pos(mat->row[var->index][off + j]))
1668 "row cannot have positive coefficients",
1669 return isl_stat_error);
1670 recheck = isl_tab_kill_col(tab, j);
1671 if (recheck < 0)
1672 return isl_stat_error;
1673 if (recheck)
1674 --j;
1675 }
1676 if (!temp_var && isl_tab_mark_redundant(tab, var->index) < 0)
1677 return isl_stat_error;
1678 if (tab_is_manifestly_empty(tab) && isl_tab_mark_empty(tab) < 0)
1679 return isl_stat_error;
1680 return isl_stat_ok;
1681}
1682
1683/* Add a constraint to the tableau and allocate a row for it.
1684 * Return the index into the constraint array "con".
1685 *
1686 * This function assumes that at least one more row and at least
1687 * one more element in the constraint array are available in the tableau.
1688 */
1690{
1691 int r;
1692
1693 isl_assert(tab->mat->ctx, tab->n_row < tab->mat->n_row, return -1);
1694 isl_assert(tab->mat->ctx, tab->n_con < tab->max_con, return -1);
1695
1696 r = tab->n_con;
1697 tab->con[r].index = tab->n_row;
1698 tab->con[r].is_row = 1;
1699 tab->con[r].is_nonneg = 0;
1700 tab->con[r].is_zero = 0;
1701 tab->con[r].is_redundant = 0;
1702 tab->con[r].frozen = 0;
1703 tab->con[r].negated = 0;
1704 tab->row_var[tab->n_row] = ~r;
1705
1706 tab->n_row++;
1707 tab->n_con++;
1708 if (isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->con[r]) < 0)
1709 return -1;
1710
1711 return r;
1712}
1713
1714/* Move the entries in tab->var up one position, starting at "first",
1715 * creating room for an extra entry at position "first".
1716 * Since some of the entries of tab->row_var and tab->col_var contain
1717 * indices into this array, they have to be updated accordingly.
1718 */
1719static int var_insert_entry(struct isl_tab *tab, int first)
1720{
1721 int i;
1722
1723 if (tab->n_var >= tab->max_var)
1725 "not enough room for new variable", return -1);
1726 if (first > tab->n_var)
1728 "invalid initial position", return -1);
1729
1730 for (i = tab->n_var - 1; i >= first; --i) {
1731 tab->var[i + 1] = tab->var[i];
1732 if (tab->var[i + 1].is_row)
1733 tab->row_var[tab->var[i + 1].index]++;
1734 else
1735 tab->col_var[tab->var[i + 1].index]++;
1736 }
1737
1738 tab->n_var++;
1739
1740 return 0;
1741}
1742
1743/* Drop the entry at position "first" in tab->var, moving all
1744 * subsequent entries down.
1745 * Since some of the entries of tab->row_var and tab->col_var contain
1746 * indices into this array, they have to be updated accordingly.
1747 */
1748static int var_drop_entry(struct isl_tab *tab, int first)
1749{
1750 int i;
1751
1752 if (first >= tab->n_var)
1754 "invalid initial position", return -1);
1755
1756 tab->n_var--;
1757
1758 for (i = first; i < tab->n_var; ++i) {
1759 tab->var[i] = tab->var[i + 1];
1760 if (tab->var[i + 1].is_row)
1761 tab->row_var[tab->var[i].index]--;
1762 else
1763 tab->col_var[tab->var[i].index]--;
1764 }
1765
1766 return 0;
1767}
1768
1769/* Add a variable to the tableau at position "r" and allocate a column for it.
1770 * Return the index into the variable array "var", i.e., "r",
1771 * or -1 on error.
1772 */
1773int isl_tab_insert_var(struct isl_tab *tab, int r)
1774{
1775 int i;
1776 unsigned off = 2 + tab->M;
1777
1778 isl_assert(tab->mat->ctx, tab->n_col < tab->mat->n_col, return -1);
1779
1780 if (var_insert_entry(tab, r) < 0)
1781 return -1;
1782
1783 tab->var[r].index = tab->n_col;
1784 tab->var[r].is_row = 0;
1785 tab->var[r].is_nonneg = 0;
1786 tab->var[r].is_zero = 0;
1787 tab->var[r].is_redundant = 0;
1788 tab->var[r].frozen = 0;
1789 tab->var[r].negated = 0;
1790 tab->col_var[tab->n_col] = r;
1791
1792 for (i = 0; i < tab->n_row; ++i)
1793 isl_int_set_si(tab->mat->row[i][off + tab->n_col], 0);
1794
1795 tab->n_col++;
1796 if (isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->var[r]) < 0)
1797 return -1;
1798
1799 return r;
1800}
1801
1802/* Add a row to the tableau. The row is given as an affine combination
1803 * of the original variables and needs to be expressed in terms of the
1804 * column variables.
1805 *
1806 * This function assumes that at least one more row and at least
1807 * one more element in the constraint array are available in the tableau.
1808 *
1809 * We add each term in turn.
1810 * If r = n/d_r is the current sum and we need to add k x, then
1811 * if x is a column variable, we increase the numerator of
1812 * this column by k d_r
1813 * if x = f/d_x is a row variable, then the new representation of r is
1814 *
1815 * n k f d_x/g n + d_r/g k f m/d_r n + m/d_g k f
1816 * --- + --- = ------------------- = -------------------
1817 * d_r d_r d_r d_x/g m
1818 *
1819 * with g the gcd of d_r and d_x and m the lcm of d_r and d_x.
1820 *
1821 * If tab->M is set, then, internally, each variable x is represented
1822 * as x' - M. We then also need no subtract k d_r from the coefficient of M.
1823 */
1824int isl_tab_add_row(struct isl_tab *tab, isl_int *line)
1825{
1826 int i;
1827 int r;
1828 isl_int *row;
1829 isl_int a, b;
1830 unsigned off = 2 + tab->M;
1831
1832 r = isl_tab_allocate_con(tab);
1833 if (r < 0)
1834 return -1;
1835
1836 isl_int_init(a);
1837 isl_int_init(b);
1838 row = tab->mat->row[tab->con[r].index];
1839 isl_int_set_si(row[0], 1);
1840 isl_int_set(row[1], line[0]);
1841 isl_seq_clr(row + 2, tab->M + tab->n_col);
1842 for (i = 0; i < tab->n_var; ++i) {
1843 if (tab->var[i].is_zero)
1844 continue;
1845 if (tab->var[i].is_row) {
1846 isl_int_lcm(a,
1847 row[0], tab->mat->row[tab->var[i].index][0]);
1848 isl_int_swap(a, row[0]);
1849 isl_int_divexact(a, row[0], a);
1851 row[0], tab->mat->row[tab->var[i].index][0]);
1852 isl_int_mul(b, b, line[1 + i]);
1853 isl_seq_combine(row + 1, a, row + 1,
1854 b, tab->mat->row[tab->var[i].index] + 1,
1855 1 + tab->M + tab->n_col);
1856 } else
1857 isl_int_addmul(row[off + tab->var[i].index],
1858 line[1 + i], row[0]);
1859 if (tab->M && i >= tab->n_param && i < tab->n_var - tab->n_div)
1860 isl_int_submul(row[2], line[1 + i], row[0]);
1861 }
1862 isl_seq_normalize(tab->mat->ctx, row, off + tab->n_col);
1865
1866 if (tab->row_sign)
1867 tab->row_sign[tab->con[r].index] = isl_tab_row_unknown;
1868
1869 return r;
1870}
1871
1872static isl_stat drop_row(struct isl_tab *tab, int row)
1873{
1874 isl_assert(tab->mat->ctx, ~tab->row_var[row] == tab->n_con - 1,
1875 return isl_stat_error);
1876 if (row != tab->n_row - 1)
1877 swap_rows(tab, row, tab->n_row - 1);
1878 tab->n_row--;
1879 tab->n_con--;
1880 return isl_stat_ok;
1881}
1882
1883/* Drop the variable in column "col" along with the column.
1884 * The column is removed first because it may need to be moved
1885 * into the last position and this process requires
1886 * the contents of the col_var array in a state
1887 * before the removal of the variable.
1888 */
1889static isl_stat drop_col(struct isl_tab *tab, int col)
1890{
1891 int var;
1892
1893 var = tab->col_var[col];
1894 if (col != tab->n_col - 1)
1895 swap_cols(tab, col, tab->n_col - 1);
1896 tab->n_col--;
1897 if (var_drop_entry(tab, var) < 0)
1898 return isl_stat_error;
1899 return isl_stat_ok;
1900}
1901
1902/* Add inequality "ineq" and check if it conflicts with the
1903 * previously added constraints or if it is obviously redundant.
1904 *
1905 * This function assumes that at least one more row and at least
1906 * one more element in the constraint array are available in the tableau.
1907 */
1909{
1910 int r;
1911 int sgn;
1912 isl_int cst;
1913
1914 if (!tab)
1915 return isl_stat_error;
1916 if (tab->bmap) {
1917 struct isl_basic_map *bmap = tab->bmap;
1918
1919 isl_assert(tab->mat->ctx, tab->n_eq == bmap->n_eq,
1920 return isl_stat_error);
1921 isl_assert(tab->mat->ctx,
1922 tab->n_con == bmap->n_eq + bmap->n_ineq,
1923 return isl_stat_error);
1924 tab->bmap = isl_basic_map_add_ineq(tab->bmap, ineq);
1926 return isl_stat_error;
1927 if (!tab->bmap)
1928 return isl_stat_error;
1929 }
1930 if (tab->cone) {
1931 isl_int_init(cst);
1932 isl_int_set_si(cst, 0);
1933 isl_int_swap(ineq[0], cst);
1934 }
1935 r = isl_tab_add_row(tab, ineq);
1936 if (tab->cone) {
1937 isl_int_swap(ineq[0], cst);
1938 isl_int_clear(cst);
1939 }
1940 if (r < 0)
1941 return isl_stat_error;
1942 tab->con[r].is_nonneg = 1;
1943 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1944 return isl_stat_error;
1945 if (isl_tab_row_is_redundant(tab, tab->con[r].index)) {
1946 if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1947 return isl_stat_error;
1948 return isl_stat_ok;
1949 }
1950
1951 sgn = restore_row(tab, &tab->con[r]);
1952 if (sgn < -1)
1953 return isl_stat_error;
1954 if (sgn < 0)
1955 return isl_tab_mark_empty(tab);
1956 if (tab->con[r].is_row && isl_tab_row_is_redundant(tab, tab->con[r].index))
1957 if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1958 return isl_stat_error;
1959 return isl_stat_ok;
1960}
1961
1962/* Pivot a non-negative variable down until it reaches the value zero
1963 * and then pivot the variable into a column position.
1964 */
1965static int to_col(struct isl_tab *tab, struct isl_tab_var *var) WARN_UNUSED;
1966static int to_col(struct isl_tab *tab, struct isl_tab_var *var)
1967{
1968 int i;
1969 int row, col;
1970 unsigned off = 2 + tab->M;
1971
1972 if (!var->is_row)
1973 return 0;
1974
1975 while (isl_int_is_pos(tab->mat->row[var->index][1])) {
1976 find_pivot(tab, var, NULL, -1, &row, &col);
1977 isl_assert(tab->mat->ctx, row != -1, return -1);
1978 if (isl_tab_pivot(tab, row, col) < 0)
1979 return -1;
1980 if (!var->is_row)
1981 return 0;
1982 }
1983
1984 for (i = tab->n_dead; i < tab->n_col; ++i)
1985 if (!isl_int_is_zero(tab->mat->row[var->index][off + i]))
1986 break;
1987
1988 isl_assert(tab->mat->ctx, i < tab->n_col, return -1);
1989 if (isl_tab_pivot(tab, var->index, i) < 0)
1990 return -1;
1991
1992 return 0;
1993}
1994
1995/* We assume Gaussian elimination has been performed on the equalities.
1996 * The equalities can therefore never conflict.
1997 * Adding the equalities is currently only really useful for a later call
1998 * to isl_tab_ineq_type.
1999 *
2000 * This function assumes that at least one more row and at least
2001 * one more element in the constraint array are available in the tableau.
2002 */
2003static struct isl_tab *add_eq(struct isl_tab *tab, isl_int *eq)
2004{
2005 int i;
2006 int r;
2007
2008 if (!tab)
2009 return NULL;
2010 r = isl_tab_add_row(tab, eq);
2011 if (r < 0)
2012 goto error;
2013
2014 r = tab->con[r].index;
2015 i = isl_seq_first_non_zero(tab->mat->row[r] + 2 + tab->M + tab->n_dead,
2016 tab->n_col - tab->n_dead);
2017 isl_assert(tab->mat->ctx, i >= 0, goto error);
2018 i += tab->n_dead;
2019 if (isl_tab_pivot(tab, r, i) < 0)
2020 goto error;
2021 if (isl_tab_kill_col(tab, i) < 0)
2022 goto error;
2023 tab->n_eq++;
2024
2025 return tab;
2026error:
2027 isl_tab_free(tab);
2028 return NULL;
2029}
2030
2031/* Does the sample value of row "row" of "tab" involve the big parameter,
2032 * if any?
2033 */
2034static int row_is_big(struct isl_tab *tab, int row)
2035{
2036 return tab->M && !isl_int_is_zero(tab->mat->row[row][2]);
2037}
2038
2039static int row_is_manifestly_zero(struct isl_tab *tab, int row)
2040{
2041 unsigned off = 2 + tab->M;
2042
2043 if (!isl_int_is_zero(tab->mat->row[row][1]))
2044 return 0;
2045 if (row_is_big(tab, row))
2046 return 0;
2047 return !isl_seq_any_non_zero(tab->mat->row[row] + off + tab->n_dead,
2048 tab->n_col - tab->n_dead);
2049}
2050
2051/* Add an equality that is known to be valid for the given tableau.
2052 *
2053 * This function assumes that at least one more row and at least
2054 * one more element in the constraint array are available in the tableau.
2055 */
2057{
2058 struct isl_tab_var *var;
2059 int r;
2060
2061 if (!tab)
2062 return -1;
2063 r = isl_tab_add_row(tab, eq);
2064 if (r < 0)
2065 return -1;
2066
2067 var = &tab->con[r];
2068 r = var->index;
2069 if (row_is_manifestly_zero(tab, r)) {
2070 var->is_zero = 1;
2071 if (isl_tab_mark_redundant(tab, r) < 0)
2072 return -1;
2073 return 0;
2074 }
2075
2076 if (isl_int_is_neg(tab->mat->row[r][1])) {
2077 isl_seq_neg(tab->mat->row[r] + 1, tab->mat->row[r] + 1,
2078 1 + tab->n_col);
2079 var->negated = 1;
2080 }
2081 var->is_nonneg = 1;
2082 if (to_col(tab, var) < 0)
2083 return -1;
2084 var->is_nonneg = 0;
2085 if (isl_tab_kill_col(tab, var->index) < 0)
2086 return -1;
2087
2088 return 0;
2089}
2090
2091/* Add a zero row to "tab" and return the corresponding index
2092 * in the constraint array.
2093 *
2094 * This function assumes that at least one more row and at least
2095 * one more element in the constraint array are available in the tableau.
2096 */
2097static int add_zero_row(struct isl_tab *tab)
2098{
2099 int r;
2100 isl_int *row;
2101
2102 r = isl_tab_allocate_con(tab);
2103 if (r < 0)
2104 return -1;
2105
2106 row = tab->mat->row[tab->con[r].index];
2107 isl_seq_clr(row + 1, 1 + tab->M + tab->n_col);
2108 isl_int_set_si(row[0], 1);
2109
2110 return r;
2111}
2112
2113/* Add equality "eq" and check if it conflicts with the
2114 * previously added constraints or if it is obviously redundant.
2115 *
2116 * This function assumes that at least one more row and at least
2117 * one more element in the constraint array are available in the tableau.
2118 * If tab->bmap is set, then two rows are needed instead of one.
2119 */
2121{
2122 struct isl_tab_undo *snap = NULL;
2123 struct isl_tab_var *var;
2124 int r;
2125 int row;
2126 int sgn;
2127 isl_int cst;
2128
2129 if (!tab)
2130 return isl_stat_error;
2131 isl_assert(tab->mat->ctx, !tab->M, return isl_stat_error);
2132
2133 if (tab->need_undo)
2134 snap = isl_tab_snap(tab);
2135
2136 if (tab->cone) {
2137 isl_int_init(cst);
2138 isl_int_set_si(cst, 0);
2139 isl_int_swap(eq[0], cst);
2140 }
2141 r = isl_tab_add_row(tab, eq);
2142 if (tab->cone) {
2143 isl_int_swap(eq[0], cst);
2144 isl_int_clear(cst);
2145 }
2146 if (r < 0)
2147 return isl_stat_error;
2148
2149 var = &tab->con[r];
2150 row = var->index;
2151 if (row_is_manifestly_zero(tab, row)) {
2152 if (snap)
2153 return isl_tab_rollback(tab, snap);
2154 return drop_row(tab, row);
2155 }
2156
2157 if (tab->bmap) {
2158 tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
2160 return isl_stat_error;
2161 isl_seq_neg(eq, eq, 1 + tab->n_var);
2162 tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
2163 isl_seq_neg(eq, eq, 1 + tab->n_var);
2165 return isl_stat_error;
2166 if (!tab->bmap)
2167 return isl_stat_error;
2168 if (add_zero_row(tab) < 0)
2169 return isl_stat_error;
2170 }
2171
2172 sgn = isl_int_sgn(tab->mat->row[row][1]);
2173
2174 if (sgn > 0) {
2175 isl_seq_neg(tab->mat->row[row] + 1, tab->mat->row[row] + 1,
2176 1 + tab->n_col);
2177 var->negated = 1;
2178 sgn = -1;
2179 }
2180
2181 if (sgn < 0) {
2182 sgn = sign_of_max(tab, var);
2183 if (sgn < -1)
2184 return isl_stat_error;
2185 if (sgn < 0) {
2186 if (isl_tab_mark_empty(tab) < 0)
2187 return isl_stat_error;
2188 return isl_stat_ok;
2189 }
2190 }
2191
2192 var->is_nonneg = 1;
2193 if (to_col(tab, var) < 0)
2194 return isl_stat_error;
2195 var->is_nonneg = 0;
2196 if (isl_tab_kill_col(tab, var->index) < 0)
2197 return isl_stat_error;
2198
2199 return isl_stat_ok;
2200}
2201
2202/* Construct and return an inequality that expresses an upper bound
2203 * on the given div.
2204 * In particular, if the div is given by
2205 *
2206 * d = floor(e/m)
2207 *
2208 * then the inequality expresses
2209 *
2210 * m d <= e
2211 */
2213 unsigned div)
2214{
2216 unsigned div_pos;
2217 struct isl_vec *ineq;
2218
2220 if (total < 0)
2221 return NULL;
2222
2223 div_pos = 1 + total - bmap->n_div + div;
2224
2225 ineq = isl_vec_alloc(bmap->ctx, 1 + total);
2226 if (!ineq)
2227 return NULL;
2228
2229 isl_seq_cpy(ineq->el, bmap->div[div] + 1, 1 + total);
2230 isl_int_neg(ineq->el[div_pos], bmap->div[div][0]);
2231 return ineq;
2232}
2233
2234/* For a div d = floor(f/m), add the constraints
2235 *
2236 * f - m d >= 0
2237 * -(f-(m-1)) + m d >= 0
2238 *
2239 * Note that the second constraint is the negation of
2240 *
2241 * f - m d >= m
2242 *
2243 * If add_ineq is not NULL, then this function is used
2244 * instead of isl_tab_add_ineq to effectively add the inequalities.
2245 *
2246 * This function assumes that at least two more rows and at least
2247 * two more elements in the constraint array are available in the tableau.
2248 */
2249static isl_stat add_div_constraints(struct isl_tab *tab, unsigned div,
2250 isl_stat (*add_ineq)(void *user, isl_int *), void *user)
2251{
2253 unsigned div_pos;
2254 struct isl_vec *ineq;
2255
2257 if (total < 0)
2258 return isl_stat_error;
2259 div_pos = 1 + total - tab->bmap->n_div + div;
2260
2261 ineq = ineq_for_div(tab->bmap, div);
2262 if (!ineq)
2263 goto error;
2264
2265 if (add_ineq) {
2266 if (add_ineq(user, ineq->el) < 0)
2267 goto error;
2268 } else {
2269 if (isl_tab_add_ineq(tab, ineq->el) < 0)
2270 goto error;
2271 }
2272
2273 isl_seq_neg(ineq->el, tab->bmap->div[div] + 1, 1 + total);
2274 isl_int_set(ineq->el[div_pos], tab->bmap->div[div][0]);
2275 isl_int_add(ineq->el[0], ineq->el[0], ineq->el[div_pos]);
2276 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
2277
2278 if (add_ineq) {
2279 if (add_ineq(user, ineq->el) < 0)
2280 goto error;
2281 } else {
2282 if (isl_tab_add_ineq(tab, ineq->el) < 0)
2283 goto error;
2284 }
2285
2286 isl_vec_free(ineq);
2287
2288 return isl_stat_ok;
2289error:
2290 isl_vec_free(ineq);
2291 return isl_stat_error;
2292}
2293
2294/* Check whether the div described by "div" is obviously non-negative.
2295 * If we are using a big parameter, then we will encode the div
2296 * as div' = M + div, which is always non-negative.
2297 * Otherwise, we check whether div is a non-negative affine combination
2298 * of non-negative variables.
2299 */
2300static int div_is_nonneg(struct isl_tab *tab, __isl_keep isl_vec *div)
2301{
2302 int i;
2303
2304 if (tab->M)
2305 return 1;
2306
2307 if (isl_int_is_neg(div->el[1]))
2308 return 0;
2309
2310 for (i = 0; i < tab->n_var; ++i) {
2311 if (isl_int_is_neg(div->el[2 + i]))
2312 return 0;
2313 if (isl_int_is_zero(div->el[2 + i]))
2314 continue;
2315 if (!tab->var[i].is_nonneg)
2316 return 0;
2317 }
2318
2319 return 1;
2320}
2321
2322/* Insert an extra div, prescribed by "div", to the tableau and
2323 * the associated bmap (which is assumed to be non-NULL).
2324 * The extra integer division is inserted at (tableau) position "pos".
2325 * Return "pos" or -1 if an error occurred.
2326 *
2327 * If add_ineq is not NULL, then this function is used instead
2328 * of isl_tab_add_ineq to add the div constraints.
2329 * This complication is needed because the code in isl_tab_pip
2330 * wants to perform some extra processing when an inequality
2331 * is added to the tableau.
2332 */
2334 isl_stat (*add_ineq)(void *user, isl_int *), void *user)
2335{
2336 int r;
2337 int nonneg;
2338 isl_size n_div;
2339 int o_div;
2340
2341 if (!tab || !div)
2342 return -1;
2343
2344 if (div->size != 1 + 1 + tab->n_var)
2346 "unexpected size", return -1);
2347
2348 n_div = isl_basic_map_dim(tab->bmap, isl_dim_div);
2349 if (n_div < 0)
2350 return -1;
2351 o_div = tab->n_var - n_div;
2352 if (pos < o_div || pos > tab->n_var)
2354 "invalid position", return -1);
2355
2356 nonneg = div_is_nonneg(tab, div);
2357
2358 if (isl_tab_extend_cons(tab, 3) < 0)
2359 return -1;
2360 if (isl_tab_extend_vars(tab, 1) < 0)
2361 return -1;
2362 r = isl_tab_insert_var(tab, pos);
2363 if (r < 0)
2364 return -1;
2365
2366 if (nonneg)
2367 tab->var[r].is_nonneg = 1;
2368
2369 tab->bmap = isl_basic_map_insert_div(tab->bmap, pos - o_div, div);
2370 if (!tab->bmap)
2371 return -1;
2372 if (isl_tab_push_var(tab, isl_tab_undo_bmap_div, &tab->var[r]) < 0)
2373 return -1;
2374
2375 if (add_div_constraints(tab, pos - o_div, add_ineq, user) < 0)
2376 return -1;
2377
2378 return r;
2379}
2380
2381/* Add an extra div, prescribed by "div", to the tableau and
2382 * the associated bmap (which is assumed to be non-NULL).
2383 */
2385{
2386 if (!tab)
2387 return -1;
2388 return isl_tab_insert_div(tab, tab->n_var, div, NULL, NULL);
2389}
2390
2391/* If "track" is set, then we want to keep track of all constraints in tab
2392 * in its bmap field. This field is initialized from a copy of "bmap",
2393 * so we need to make sure that all constraints in "bmap" also appear
2394 * in the constructed tab.
2395 */
2397 __isl_keep isl_basic_map *bmap, int track)
2398{
2399 int i;
2400 struct isl_tab *tab;
2402
2404 if (total < 0)
2405 return NULL;
2406 tab = isl_tab_alloc(bmap->ctx, total + bmap->n_ineq + 1, total, 0);
2407 if (!tab)
2408 return NULL;
2409 tab->preserve = track;
2412 if (isl_tab_mark_empty(tab) < 0)
2413 goto error;
2414 goto done;
2415 }
2416 for (i = 0; i < bmap->n_eq; ++i) {
2417 tab = add_eq(tab, bmap->eq[i]);
2418 if (!tab)
2419 return tab;
2420 }
2421 for (i = 0; i < bmap->n_ineq; ++i) {
2422 if (isl_tab_add_ineq(tab, bmap->ineq[i]) < 0)
2423 goto error;
2424 if (tab->empty)
2425 goto done;
2426 }
2427done:
2428 if (track && isl_tab_track_bmap(tab, isl_basic_map_copy(bmap)) < 0)
2429 goto error;
2430 return tab;
2431error:
2432 isl_tab_free(tab);
2433 return NULL;
2434}
2435
2437 __isl_keep isl_basic_set *bset, int track)
2438{
2439 return isl_tab_from_basic_map(bset, track);
2440}
2441
2442/* Construct a tableau corresponding to the recession cone of "bset".
2443 */
2445 int parametric)
2446{
2447 isl_int cst;
2448 int i;
2449 struct isl_tab *tab;
2450 isl_size offset = 0;
2452
2454 if (parametric)
2456 if (total < 0 || offset < 0)
2457 return NULL;
2458 tab = isl_tab_alloc(bset->ctx, bset->n_eq + bset->n_ineq,
2459 total - offset, 0);
2460 if (!tab)
2461 return NULL;
2463 tab->cone = 1;
2464
2465 isl_int_init(cst);
2466 isl_int_set_si(cst, 0);
2467 for (i = 0; i < bset->n_eq; ++i) {
2468 isl_int_swap(bset->eq[i][offset], cst);
2469 if (offset > 0) {
2470 if (isl_tab_add_eq(tab, bset->eq[i] + offset) < 0)
2471 goto error;
2472 } else
2473 tab = add_eq(tab, bset->eq[i]);
2474 isl_int_swap(bset->eq[i][offset], cst);
2475 if (!tab)
2476 goto done;
2477 }
2478 for (i = 0; i < bset->n_ineq; ++i) {
2479 int r;
2480 isl_int_swap(bset->ineq[i][offset], cst);
2481 r = isl_tab_add_row(tab, bset->ineq[i] + offset);
2482 isl_int_swap(bset->ineq[i][offset], cst);
2483 if (r < 0)
2484 goto error;
2485 tab->con[r].is_nonneg = 1;
2486 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
2487 goto error;
2488 }
2489done:
2490 isl_int_clear(cst);
2491 return tab;
2492error:
2493 isl_int_clear(cst);
2494 isl_tab_free(tab);
2495 return NULL;
2496}
2497
2498/* Assuming "tab" is the tableau of a cone, check if the cone is
2499 * bounded, i.e., if it is empty or only contains the origin.
2500 */
2502{
2503 int i;
2504
2505 if (!tab)
2506 return isl_bool_error;
2507 if (tab->empty)
2508 return isl_bool_true;
2509 if (tab->n_dead == tab->n_col)
2510 return isl_bool_true;
2511
2512 for (;;) {
2513 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2514 struct isl_tab_var *var;
2515 int sgn;
2516 var = isl_tab_var_from_row(tab, i);
2517 if (!var->is_nonneg)
2518 continue;
2519 sgn = sign_of_max(tab, var);
2520 if (sgn < -1)
2521 return isl_bool_error;
2522 if (sgn != 0)
2523 return isl_bool_false;
2524 if (close_row(tab, var, 0) < 0)
2525 return isl_bool_error;
2526 break;
2527 }
2528 if (tab->n_dead == tab->n_col)
2529 return isl_bool_true;
2530 if (i == tab->n_row)
2531 return isl_bool_false;
2532 }
2533}
2534
2536{
2537 int i;
2538
2539 if (!tab)
2540 return -1;
2541
2542 for (i = 0; i < tab->n_var; ++i) {
2543 int row;
2544 if (!tab->var[i].is_row)
2545 continue;
2546 row = tab->var[i].index;
2547 if (!isl_int_is_divisible_by(tab->mat->row[row][1],
2548 tab->mat->row[row][0]))
2549 return 0;
2550 }
2551 return 1;
2552}
2553
2554static struct isl_vec *extract_integer_sample(struct isl_tab *tab)
2555{
2556 int i;
2557 struct isl_vec *vec;
2558
2559 vec = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
2560 if (!vec)
2561 return NULL;
2562
2563 isl_int_set_si(vec->block.data[0], 1);
2564 for (i = 0; i < tab->n_var; ++i) {
2565 if (!tab->var[i].is_row)
2566 isl_int_set_si(vec->block.data[1 + i], 0);
2567 else {
2568 int row = tab->var[i].index;
2569 isl_int_divexact(vec->block.data[1 + i],
2570 tab->mat->row[row][1], tab->mat->row[row][0]);
2571 }
2572 }
2573
2574 return vec;
2575}
2576
2578{
2579 int i;
2580 struct isl_vec *vec;
2581 isl_int m;
2582
2583 if (!tab)
2584 return NULL;
2585
2586 vec = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
2587 if (!vec)
2588 return NULL;
2589
2590 isl_int_init(m);
2591
2592 isl_int_set_si(vec->block.data[0], 1);
2593 for (i = 0; i < tab->n_var; ++i) {
2594 int row;
2595 if (!tab->var[i].is_row) {
2596 isl_int_set_si(vec->block.data[1 + i], 0);
2597 continue;
2598 }
2599 row = tab->var[i].index;
2600 isl_int_gcd(m, vec->block.data[0], tab->mat->row[row][0]);
2601 isl_int_divexact(m, tab->mat->row[row][0], m);
2602 isl_seq_scale(vec->block.data, vec->block.data, m, 1 + i);
2603 isl_int_divexact(m, vec->block.data[0], tab->mat->row[row][0]);
2604 isl_int_mul(vec->block.data[1 + i], m, tab->mat->row[row][1]);
2605 }
2606 vec = isl_vec_normalize(vec);
2607
2609 return vec;
2610}
2611
2612/* Store the sample value of "var" of "tab" rounded up (if sgn > 0)
2613 * or down (if sgn < 0) to the nearest integer in *v.
2614 */
2615static void get_rounded_sample_value(struct isl_tab *tab,
2616 struct isl_tab_var *var, int sgn, isl_int *v)
2617{
2618 if (!var->is_row)
2619 isl_int_set_si(*v, 0);
2620 else if (sgn > 0)
2621 isl_int_cdiv_q(*v, tab->mat->row[var->index][1],
2622 tab->mat->row[var->index][0]);
2623 else
2624 isl_int_fdiv_q(*v, tab->mat->row[var->index][1],
2625 tab->mat->row[var->index][0]);
2626}
2627
2628/* Update "bmap" based on the results of the tableau "tab".
2629 * In particular, implicit equalities are made explicit, redundant constraints
2630 * are removed and if the sample value happens to be integer, it is stored
2631 * in "bmap" (unless "bmap" already had an integer sample).
2632 *
2633 * The tableau is assumed to have been created from "bmap" using
2634 * isl_tab_from_basic_map.
2635 */
2637 __isl_take isl_basic_map *bmap, struct isl_tab *tab)
2638{
2639 int i;
2640 unsigned n_eq;
2641
2642 if (!bmap)
2643 return NULL;
2644 if (!tab)
2645 return bmap;
2646
2647 n_eq = tab->n_eq;
2648 if (tab->empty)
2649 bmap = isl_basic_map_set_to_empty(bmap);
2650 else
2651 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2652 if (isl_tab_is_equality(tab, n_eq + i))
2654 else if (isl_tab_is_redundant(tab, n_eq + i))
2656 }
2657 if (bmap->n_eq != n_eq)
2658 bmap = isl_basic_map_gauss(bmap, NULL);
2659 if (!tab->rational &&
2660 bmap && !bmap->sample && isl_tab_sample_is_integer(tab))
2661 bmap->sample = extract_integer_sample(tab);
2662 return bmap;
2663}
2664
2671
2672/* Drop the last constraint added to "tab" in position "r".
2673 * The constraint is expected to have remained in a row.
2674 */
2675static isl_stat drop_last_con_in_row(struct isl_tab *tab, int r)
2676{
2677 if (!tab->con[r].is_row)
2679 "row unexpectedly moved to column",
2680 return isl_stat_error);
2681 if (r + 1 != tab->n_con)
2683 "additional constraints added", return isl_stat_error);
2684 if (drop_row(tab, tab->con[r].index) < 0)
2685 return isl_stat_error;
2686
2687 return isl_stat_ok;
2688}
2689
2690/* Given a non-negative variable "var", temporarily add a new non-negative
2691 * variable that is the opposite of "var", ensuring that "var" can only attain
2692 * the value zero. The new variable is removed again before this function
2693 * returns. However, the effect of forcing "var" to be zero remains.
2694 * If var = n/d is a row variable, then the new variable = -n/d.
2695 * If var is a column variables, then the new variable = -var.
2696 * If the new variable cannot attain non-negative values, then
2697 * the resulting tableau is empty.
2698 * Otherwise, we know the value will be zero and we close the row.
2699 */
2701{
2702 unsigned r;
2703 isl_int *row;
2704 int sgn;
2705 unsigned off = 2 + tab->M;
2706
2707 if (var->is_zero)
2708 return isl_stat_ok;
2709 if (var->is_redundant || !var->is_nonneg)
2711 "expecting non-redundant non-negative variable",
2712 return isl_stat_error);
2713
2714 if (isl_tab_extend_cons(tab, 1) < 0)
2715 return isl_stat_error;
2716
2717 r = tab->n_con;
2718 tab->con[r].index = tab->n_row;
2719 tab->con[r].is_row = 1;
2720 tab->con[r].is_nonneg = 0;
2721 tab->con[r].is_zero = 0;
2722 tab->con[r].is_redundant = 0;
2723 tab->con[r].frozen = 0;
2724 tab->con[r].negated = 0;
2725 tab->row_var[tab->n_row] = ~r;
2726 row = tab->mat->row[tab->n_row];
2727
2728 if (var->is_row) {
2729 isl_int_set(row[0], tab->mat->row[var->index][0]);
2730 isl_seq_neg(row + 1,
2731 tab->mat->row[var->index] + 1, 1 + tab->n_col);
2732 } else {
2733 isl_int_set_si(row[0], 1);
2734 isl_seq_clr(row + 1, 1 + tab->n_col);
2735 isl_int_set_si(row[off + var->index], -1);
2736 }
2737
2738 tab->n_row++;
2739 tab->n_con++;
2740
2741 sgn = sign_of_max(tab, &tab->con[r]);
2742 if (sgn < -1)
2743 return isl_stat_error;
2744 if (sgn < 0) {
2745 if (drop_last_con_in_row(tab, r) < 0)
2746 return isl_stat_error;
2747 if (isl_tab_mark_empty(tab) < 0)
2748 return isl_stat_error;
2749 return isl_stat_ok;
2750 }
2751 tab->con[r].is_nonneg = 1;
2752 /* sgn == 0 */
2753 if (close_row(tab, &tab->con[r], 1) < 0)
2754 return isl_stat_error;
2755 if (drop_last_con_in_row(tab, r) < 0)
2756 return isl_stat_error;
2757
2758 return isl_stat_ok;
2759}
2760
2761/* Check that "con" is a valid constraint position for "tab".
2762 */
2763static isl_stat isl_tab_check_con(struct isl_tab *tab, int con)
2764{
2765 if (!tab)
2766 return isl_stat_error;
2767 if (con < 0 || con >= tab->n_con)
2769 "position out of bounds", return isl_stat_error);
2770 return isl_stat_ok;
2771}
2772
2773/* Given a tableau "tab" and an inequality constraint "con" of the tableau,
2774 * relax the inequality by one. That is, the inequality r >= 0 is replaced
2775 * by r' = r + 1 >= 0.
2776 * If r is a row variable, we simply increase the constant term by one
2777 * (taking into account the denominator).
2778 * If r is a column variable, then we need to modify each row that
2779 * refers to r = r' - 1 by substituting this equality, effectively
2780 * subtracting the coefficient of the column from the constant.
2781 * We should only do this if the minimum is manifestly unbounded,
2782 * however. Otherwise, we may end up with negative sample values
2783 * for non-negative variables.
2784 * So, if r is a column variable with a minimum that is not
2785 * manifestly unbounded, then we need to move it to a row.
2786 * However, the sample value of this row may be negative,
2787 * even after the relaxation, so we need to restore it.
2788 * We therefore prefer to pivot a column up to a row, if possible.
2789 */
2790int isl_tab_relax(struct isl_tab *tab, int con)
2791{
2792 struct isl_tab_var *var;
2793
2794 if (!tab)
2795 return -1;
2796
2797 var = &tab->con[con];
2798
2799 if (var->is_row && (var->index < 0 || var->index < tab->n_redundant))
2801 "cannot relax redundant constraint", return -1);
2802 if (!var->is_row && (var->index < 0 || var->index < tab->n_dead))
2804 "cannot relax dead constraint", return -1);
2805
2806 if (!var->is_row && !max_is_manifestly_unbounded(tab, var))
2807 if (to_row(tab, var, 1) < 0)
2808 return -1;
2809 if (!var->is_row && !min_is_manifestly_unbounded(tab, var))
2810 if (to_row(tab, var, -1) < 0)
2811 return -1;
2812
2813 if (var->is_row) {
2814 isl_int_add(tab->mat->row[var->index][1],
2815 tab->mat->row[var->index][1], tab->mat->row[var->index][0]);
2816 if (restore_row(tab, var) < 0)
2817 return -1;
2818 } else {
2819 int i;
2820 unsigned off = 2 + tab->M;
2821
2822 for (i = 0; i < tab->n_row; ++i) {
2823 if (isl_int_is_zero(tab->mat->row[i][off + var->index]))
2824 continue;
2825 isl_int_sub(tab->mat->row[i][1], tab->mat->row[i][1],
2826 tab->mat->row[i][off + var->index]);
2827 }
2828
2829 }
2830
2832 return -1;
2833
2834 return 0;
2835}
2836
2837/* Replace the variable v at position "pos" in the tableau "tab"
2838 * by v' = v + shift.
2839 *
2840 * If the variable is in a column, then we first check if we can
2841 * simply plug in v = v' - shift. The effect on a row with
2842 * coefficient f/d for variable v is that the constant term c/d
2843 * is replaced by (c - f * shift)/d. If shift is positive and
2844 * f is negative for each row that needs to remain non-negative,
2845 * then this is clearly safe. In other words, if the minimum of v
2846 * is manifestly unbounded, then we can keep v in a column position.
2847 * Otherwise, we can pivot it down to a row.
2848 * Similarly, if shift is negative, we need to check if the maximum
2849 * of is manifestly unbounded.
2850 *
2851 * If the variable is in a row (from the start or after pivoting),
2852 * then the constant term c/d is replaced by (c + d * shift)/d.
2853 */
2854int isl_tab_shift_var(struct isl_tab *tab, int pos, isl_int shift)
2855{
2856 struct isl_tab_var *var;
2857
2858 if (!tab)
2859 return -1;
2860 if (isl_int_is_zero(shift))
2861 return 0;
2862
2863 var = &tab->var[pos];
2864 if (!var->is_row) {
2865 if (isl_int_is_neg(shift)) {
2867 if (to_row(tab, var, 1) < 0)
2868 return -1;
2869 } else {
2871 if (to_row(tab, var, -1) < 0)
2872 return -1;
2873 }
2874 }
2875
2876 if (var->is_row) {
2877 isl_int_addmul(tab->mat->row[var->index][1],
2878 shift, tab->mat->row[var->index][0]);
2879 } else {
2880 int i;
2881 unsigned off = 2 + tab->M;
2882
2883 for (i = 0; i < tab->n_row; ++i) {
2884 if (isl_int_is_zero(tab->mat->row[i][off + var->index]))
2885 continue;
2886 isl_int_submul(tab->mat->row[i][1],
2887 shift, tab->mat->row[i][off + var->index]);
2888 }
2889
2890 }
2891
2892 return 0;
2893}
2894
2895/* Remove the sign constraint from constraint "con".
2896 *
2897 * If the constraint variable was originally marked non-negative,
2898 * then we make sure we mark it non-negative again during rollback.
2899 */
2900int isl_tab_unrestrict(struct isl_tab *tab, int con)
2901{
2902 struct isl_tab_var *var;
2903
2904 if (!tab)
2905 return -1;
2906
2907 var = &tab->con[con];
2908 if (!var->is_nonneg)
2909 return 0;
2910
2911 var->is_nonneg = 0;
2913 return -1;
2914
2915 return 0;
2916}
2917
2918int isl_tab_select_facet(struct isl_tab *tab, int con)
2919{
2920 if (!tab)
2921 return -1;
2922
2923 return cut_to_hyperplane(tab, &tab->con[con]);
2924}
2925
2926static int may_be_equality(struct isl_tab *tab, int row)
2927{
2928 return tab->rational ? isl_int_is_zero(tab->mat->row[row][1])
2929 : isl_int_lt(tab->mat->row[row][1],
2930 tab->mat->row[row][0]);
2931}
2932
2933/* Return an isl_tab_var that has been marked or NULL if no such
2934 * variable can be found.
2935 * The marked field has only been set for variables that
2936 * appear in non-redundant rows or non-dead columns.
2937 *
2938 * Pick the last constraint variable that is marked and
2939 * that appears in either a non-redundant row or a non-dead columns.
2940 * Since the returned variable is tested for being a redundant constraint or
2941 * an implicit equality, there is no need to return any tab variable that
2942 * corresponds to a variable.
2943 */
2944static struct isl_tab_var *select_marked(struct isl_tab *tab)
2945{
2946 int i;
2947 struct isl_tab_var *var;
2948
2949 for (i = tab->n_con - 1; i >= 0; --i) {
2950 var = &tab->con[i];
2951 if (var->index < 0)
2952 continue;
2953 if (var->is_row && var->index < tab->n_redundant)
2954 continue;
2955 if (!var->is_row && var->index < tab->n_dead)
2956 continue;
2957 if (var->marked)
2958 return var;
2959 }
2960
2961 return NULL;
2962}
2963
2964/* Check for (near) equalities among the constraints.
2965 * A constraint is an equality if it is non-negative and if
2966 * its maximal value is either
2967 * - zero (in case of rational tableaus), or
2968 * - strictly less than 1 (in case of integer tableaus)
2969 *
2970 * We first mark all non-redundant and non-dead variables that
2971 * are not frozen and not obviously not an equality.
2972 * Then we iterate over all marked variables if they can attain
2973 * any values larger than zero or at least one.
2974 * If the maximal value is zero, we mark any column variables
2975 * that appear in the row as being zero and mark the row as being redundant.
2976 * Otherwise, if the maximal value is strictly less than one (and the
2977 * tableau is integer), then we restrict the value to being zero
2978 * by adding an opposite non-negative variable.
2979 * The order in which the variables are considered is not important.
2980 */
2982{
2983 int i;
2984 unsigned n_marked;
2985
2986 if (!tab)
2987 return -1;
2988 if (tab->empty)
2989 return 0;
2990 if (tab->n_dead == tab->n_col)
2991 return 0;
2992
2993 n_marked = 0;
2994 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2995 struct isl_tab_var *var = isl_tab_var_from_row(tab, i);
2996 var->marked = !var->frozen && var->is_nonneg &&
2997 may_be_equality(tab, i);
2998 if (var->marked)
2999 n_marked++;
3000 }
3001 for (i = tab->n_dead; i < tab->n_col; ++i) {
3002 struct isl_tab_var *var = var_from_col(tab, i);
3003 var->marked = !var->frozen && var->is_nonneg;
3004 if (var->marked)
3005 n_marked++;
3006 }
3007 while (n_marked) {
3008 struct isl_tab_var *var;
3009 int sgn;
3010 var = select_marked(tab);
3011 if (!var)
3012 break;
3013 var->marked = 0;
3014 n_marked--;
3015 sgn = sign_of_max(tab, var);
3016 if (sgn < 0)
3017 return -1;
3018 if (sgn == 0) {
3019 if (close_row(tab, var, 0) < 0)
3020 return -1;
3021 } else if (!tab->rational && !at_least_one(tab, var)) {
3022 if (cut_to_hyperplane(tab, var) < 0)
3023 return -1;
3025 }
3026 for (i = tab->n_redundant; i < tab->n_row; ++i) {
3027 var = isl_tab_var_from_row(tab, i);
3028 if (!var->marked)
3029 continue;
3030 if (may_be_equality(tab, i))
3031 continue;
3032 var->marked = 0;
3033 n_marked--;
3034 }
3035 }
3036
3037 return 0;
3038}
3039
3040/* Update the element of row_var or col_var that corresponds to
3041 * constraint tab->con[i] to a move from position "old" to position "i".
3042 */
3043static int update_con_after_move(struct isl_tab *tab, int i, int old)
3044{
3045 int *p;
3046 int index;
3047
3048 index = tab->con[i].index;
3049 if (index == -1)
3050 return 0;
3051 p = tab->con[i].is_row ? tab->row_var : tab->col_var;
3052 if (p[index] != ~old)
3054 "broken internal state", return -1);
3055 p[index] = ~i;
3056
3057 return 0;
3058}
3059
3060/* Interchange constraints "con1" and "con2" in "tab".
3061 * In particular, interchange the contents of these entries in tab->con.
3062 * Since tab->col_var and tab->row_var point back into this array,
3063 * they need to be updated accordingly.
3064 */
3065isl_stat isl_tab_swap_constraints(struct isl_tab *tab, int con1, int con2)
3066{
3067 struct isl_tab_var var;
3068
3069 if (isl_tab_check_con(tab, con1) < 0 ||
3070 isl_tab_check_con(tab, con2) < 0)
3071 return isl_stat_error;
3072
3073 var = tab->con[con1];
3074 tab->con[con1] = tab->con[con2];
3075 if (update_con_after_move(tab, con1, con2) < 0)
3076 return isl_stat_error;
3077 tab->con[con2] = var;
3078 if (update_con_after_move(tab, con2, con1) < 0)
3079 return isl_stat_error;
3080
3081 return isl_stat_ok;
3082}
3083
3084/* Rotate the "n" constraints starting at "first" to the right,
3085 * putting the last constraint in the position of the first constraint.
3086 */
3087static isl_stat rotate_constraints_right(struct isl_tab *tab, int first, int n)
3088{
3089 int i, last;
3090 struct isl_tab_var var;
3091
3092 if (n <= 1)
3093 return isl_stat_ok;
3094
3095 last = first + n - 1;
3096 var = tab->con[last];
3097 for (i = last; i > first; --i) {
3098 tab->con[i] = tab->con[i - 1];
3099 if (update_con_after_move(tab, i, i - 1) < 0)
3100 return isl_stat_error;
3101 }
3102 tab->con[first] = var;
3103 if (update_con_after_move(tab, first, last) < 0)
3104 return isl_stat_error;
3105
3106 return isl_stat_ok;
3107}
3108
3109/* Rotate the "n" constraints starting at "first" to the left,
3110 * putting the first constraint in the position of the last constraint.
3111 */
3112static isl_stat rotate_constraints_left(struct isl_tab *tab, int first, int n)
3113{
3114 int i, last;
3115 struct isl_tab_var var;
3116
3117 if (n <= 1)
3118 return isl_stat_ok;
3119
3120 last = first + n - 1;
3121 var = tab->con[first];
3122 for (i = first; i < last; ++i) {
3123 tab->con[i] = tab->con[i + 1];
3124 if (update_con_after_move(tab, i, i + 1) < 0)
3125 return isl_stat_error;
3126 }
3127 tab->con[last] = var;
3128 if (update_con_after_move(tab, last, first) < 0)
3129 return isl_stat_error;
3130
3131 return isl_stat_ok;
3132}
3133
3134/* Drop the "n" entries starting at position "first" in tab->con, moving all
3135 * subsequent entries down.
3136 * Since some of the entries of tab->row_var and tab->col_var contain
3137 * indices into this array, they have to be updated accordingly.
3138 */
3140 unsigned first, unsigned n)
3141{
3142 int i;
3143
3144 if (first + n > tab->n_con || first + n < first)
3146 "invalid range", return isl_stat_error);
3147
3148 tab->n_con -= n;
3149
3150 for (i = first; i < tab->n_con; ++i) {
3151 tab->con[i] = tab->con[i + n];
3152 if (update_con_after_move(tab, i, i + n) < 0)
3153 return isl_stat_error;
3154 }
3155
3156 return isl_stat_ok;
3157}
3158
3159/* isl_basic_map_gauss5 callback that gets called when
3160 * two (equality) constraints "a" and "b" get interchanged
3161 * in the basic map. Perform the same interchange in "tab".
3162 */
3163static isl_stat swap_eq(unsigned a, unsigned b, void *user)
3164{
3165 struct isl_tab *tab = user;
3166
3167 return isl_tab_swap_constraints(tab, a, b);
3168}
3169
3170/* isl_basic_map_gauss5 callback that gets called when
3171 * the final "n" equality constraints get removed.
3172 * As a special case, if "n" is equal to the total number
3173 * of equality constraints, then this means the basic map
3174 * turned out to be empty.
3175 * Drop the same number of equality constraints from "tab" or
3176 * mark it empty in the special case.
3177 */
3178static isl_stat drop_eq(unsigned n, void *user)
3179{
3180 struct isl_tab *tab = user;
3181
3182 if (tab->n_eq == n)
3183 return isl_tab_mark_empty(tab);
3184
3185 tab->n_eq -= n;
3186 return con_drop_entries(tab, tab->n_eq, n);
3187}
3188
3189/* If "bmap" has more than a single reference, then call
3190 * isl_basic_map_gauss on it, updating "tab" accordingly.
3191 */
3193 struct isl_tab *tab)
3194{
3195 isl_bool single;
3196
3198 if (single < 0)
3199 return isl_basic_map_free(bmap);
3200 if (single)
3201 return bmap;
3202 return isl_basic_map_gauss5(bmap, NULL, &swap_eq, &drop_eq, tab);
3203}
3204
3205/* Make the equalities that are implicit in "bmap" but that have been
3206 * detected in the corresponding "tab" explicit in "bmap" and update
3207 * "tab" to reflect the new order of the constraints.
3208 *
3209 * In particular, if inequality i is an implicit equality then
3210 * isl_basic_map_inequality_to_equality will move the inequality
3211 * in front of the other equality and it will move the last inequality
3212 * in the position of inequality i.
3213 * In the tableau, the inequalities of "bmap" are stored after the equalities
3214 * and so the original order
3215 *
3216 * E E E E E A A A I B B B B L
3217 *
3218 * is changed into
3219 *
3220 * I E E E E E A A A L B B B B
3221 *
3222 * where I is the implicit equality, the E are equalities,
3223 * the A inequalities before I, the B inequalities after I and
3224 * L the last inequality.
3225 * We therefore need to rotate to the right two sets of constraints,
3226 * those up to and including I and those after I.
3227 *
3228 * If "tab" contains any constraints that are not in "bmap" then they
3229 * appear after those in "bmap" and they should be left untouched.
3230 *
3231 * If the operation may need to be undone, then keep track
3232 * of the inequality constraints that have been turned
3233 * into equality constraints.
3234 *
3235 * Note that this function only calls isl_basic_map_gauss
3236 * (in case some equality constraints got detected)
3237 * if "bmap" has more than one reference and if the operation
3238 * does not need to be undone.
3239 * If it only has a single reference, then it is left in a temporary state,
3240 * because the caller may require this state.
3241 * Calling isl_basic_map_gauss is then the responsibility of the caller.
3242 * This is also the case if the operation may need to be undone.
3243 */
3246{
3247 int i;
3248 unsigned n_eq;
3249
3250 if (!tab || !bmap)
3251 return isl_basic_map_free(bmap);
3252 if (tab->empty)
3253 return bmap;
3254
3255 n_eq = tab->n_eq;
3256 for (i = bmap->n_ineq - 1; i >= 0; --i) {
3257 if (!isl_tab_is_equality(tab, bmap->n_eq + i))
3258 continue;
3260 if (rotate_constraints_right(tab, 0, tab->n_eq + i + 1) < 0)
3261 return isl_basic_map_free(bmap);
3262 if (rotate_constraints_right(tab, tab->n_eq + i + 1,
3263 bmap->n_ineq - i) < 0)
3264 return isl_basic_map_free(bmap);
3265 tab->n_eq++;
3266 if (tab->need_undo)
3268 }
3269
3270 if (!tab->need_undo && n_eq != tab->n_eq)
3271 bmap = gauss_if_shared(bmap, tab);
3272
3273 return bmap;
3274}
3275
3276/* Undo the effect of turning an inequality constraint
3277 * into an equality constraint in isl_tab_make_equalities_explicit.
3278 * "ineq" is the original position of the inequality constraint that
3279 * now appears as the first equality constraint.
3280 *
3281 * That is, the order
3282 *
3283 * I E E E E E A A A L B B B B
3284 *
3285 * needs to be changed back into
3286 *
3287 * E E E E E A A A I B B B B L
3288 *
3289 * where I is the inequality turned equality, the E are the original equalities,
3290 * the A inequalities originally before I,
3291 * the B inequalities originally after I and
3292 * L the originally last inequality.
3293 *
3294 * Two groups of constraints therefore need to be rotated left,
3295 * those up to and including the original position of I and
3296 * those after this position.
3297 */
3298static isl_stat first_eq_to_ineq(struct isl_tab *tab, int ineq)
3299{
3300 unsigned n_ineq, n_eq;
3301
3302 if (!tab)
3303 return isl_stat_error;
3304
3305 n_ineq = tab->n_con - tab->n_eq;
3306 tab->n_eq--;
3307 n_eq = tab->n_eq;
3308 if (rotate_constraints_left(tab, 0, n_eq + ineq + 1) < 0)
3309 return isl_stat_error;
3310 if (rotate_constraints_left(tab, n_eq + ineq + 1, n_ineq - ineq) < 0)
3311 return isl_stat_error;
3312 return isl_stat_ok;
3313}
3314
3315static int con_is_redundant(struct isl_tab *tab, struct isl_tab_var *var)
3316{
3317 if (!tab)
3318 return -1;
3319 if (tab->rational) {
3320 int sgn = sign_of_min(tab, var);
3321 if (sgn < -1)
3322 return -1;
3323 return sgn >= 0;
3324 } else {
3325 int irred = isl_tab_min_at_most_neg_one(tab, var);
3326 if (irred < 0)
3327 return -1;
3328 return !irred;
3329 }
3330}
3331
3332/* Check for (near) redundant constraints.
3333 * A constraint is redundant if it is non-negative and if
3334 * its minimal value (temporarily ignoring the non-negativity) is either
3335 * - zero (in case of rational tableaus), or
3336 * - strictly larger than -1 (in case of integer tableaus)
3337 *
3338 * We first mark all non-redundant and non-dead variables that
3339 * are not frozen and not obviously negatively unbounded.
3340 * Then we iterate over all marked variables if they can attain
3341 * any values smaller than zero or at most negative one.
3342 * If not, we mark the row as being redundant (assuming it hasn't
3343 * been detected as being obviously redundant in the mean time).
3344 */
3346{
3347 int i;
3348 unsigned n_marked;
3349
3350 if (!tab)
3351 return -1;
3352 if (tab->empty)
3353 return 0;
3354 if (tab->n_redundant == tab->n_row)
3355 return 0;
3356
3357 n_marked = 0;
3358 for (i = tab->n_redundant; i < tab->n_row; ++i) {
3359 struct isl_tab_var *var = isl_tab_var_from_row(tab, i);
3360 var->marked = !var->frozen && var->is_nonneg;
3361 if (var->marked)
3362 n_marked++;
3363 }
3364 for (i = tab->n_dead; i < tab->n_col; ++i) {
3365 struct isl_tab_var *var = var_from_col(tab, i);
3366 var->marked = !var->frozen && var->is_nonneg &&
3368 if (var->marked)
3369 n_marked++;
3370 }
3371 while (n_marked) {
3372 struct isl_tab_var *var;
3373 int red;
3374 var = select_marked(tab);
3375 if (!var)
3376 break;
3377 var->marked = 0;
3378 n_marked--;
3379 red = con_is_redundant(tab, var);
3380 if (red < 0)
3381 return -1;
3382 if (red && !var->is_redundant)
3383 if (isl_tab_mark_redundant(tab, var->index) < 0)
3384 return -1;
3385 for (i = tab->n_dead; i < tab->n_col; ++i) {
3386 var = var_from_col(tab, i);
3387 if (!var->marked)
3388 continue;
3390 continue;
3391 var->marked = 0;
3392 n_marked--;
3393 }
3394 }
3395
3396 return 0;
3397}
3398
3399int isl_tab_is_equality(struct isl_tab *tab, int con)
3400{
3401 int row;
3402 unsigned off;
3403
3404 if (!tab)
3405 return -1;
3406 if (tab->con[con].is_zero)
3407 return 1;
3408 if (tab->con[con].is_redundant)
3409 return 0;
3410 if (!tab->con[con].is_row)
3411 return tab->con[con].index < tab->n_dead;
3412
3413 row = tab->con[con].index;
3414
3415 off = 2 + tab->M;
3416 return isl_int_is_zero(tab->mat->row[row][1]) &&
3417 !row_is_big(tab, row) &&
3418 !isl_seq_any_non_zero(tab->mat->row[row] + off + tab->n_dead,
3419 tab->n_col - tab->n_dead);
3420}
3421
3422/* Return the minimal value of the affine expression "f" with denominator
3423 * "denom" in *opt, *opt_denom, assuming the tableau is not empty and
3424 * the expression cannot attain arbitrarily small values.
3425 * If opt_denom is NULL, then *opt is rounded up to the nearest integer.
3426 * The return value reflects the nature of the result (empty, unbounded,
3427 * minimal value returned in *opt).
3428 *
3429 * This function assumes that at least one more row and at least
3430 * one more element in the constraint array are available in the tableau.
3431 */
3433 isl_int *f, isl_int denom, isl_int *opt, isl_int *opt_denom,
3434 unsigned flags)
3435{
3436 int r;
3438 struct isl_tab_var *var;
3439 struct isl_tab_undo *snap;
3440
3441 if (!tab)
3442 return isl_lp_error;
3443
3444 if (tab->empty)
3445 return isl_lp_empty;
3446
3447 snap = isl_tab_snap(tab);
3448 r = isl_tab_add_row(tab, f);
3449 if (r < 0)
3450 return isl_lp_error;
3451 var = &tab->con[r];
3452 for (;;) {
3453 int row, col;
3454 find_pivot(tab, var, var, -1, &row, &col);
3455 if (row == var->index) {
3457 break;
3458 }
3459 if (row == -1)
3460 break;
3461 if (isl_tab_pivot(tab, row, col) < 0)
3462 return isl_lp_error;
3463 }
3464 isl_int_mul(tab->mat->row[var->index][0],
3465 tab->mat->row[var->index][0], denom);
3466 if (ISL_FL_ISSET(flags, ISL_TAB_SAVE_DUAL)) {
3467 int i;
3468
3469 isl_vec_free(tab->dual);
3470 tab->dual = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_con);
3471 if (!tab->dual)
3472 return isl_lp_error;
3473 isl_int_set(tab->dual->el[0], tab->mat->row[var->index][0]);
3474 for (i = 0; i < tab->n_con; ++i) {
3475 int pos;
3476 if (tab->con[i].is_row) {
3477 isl_int_set_si(tab->dual->el[1 + i], 0);
3478 continue;
3479 }
3480 pos = 2 + tab->M + tab->con[i].index;
3481 if (tab->con[i].negated)
3482 isl_int_neg(tab->dual->el[1 + i],
3483 tab->mat->row[var->index][pos]);
3484 else
3485 isl_int_set(tab->dual->el[1 + i],
3486 tab->mat->row[var->index][pos]);
3487 }
3488 }
3489 if (opt && res == isl_lp_ok) {
3490 if (opt_denom) {
3491 isl_int_set(*opt, tab->mat->row[var->index][1]);
3492 isl_int_set(*opt_denom, tab->mat->row[var->index][0]);
3493 } else
3494 get_rounded_sample_value(tab, var, 1, opt);
3495 }
3496 if (isl_tab_rollback(tab, snap) < 0)
3497 return isl_lp_error;
3498 return res;
3499}
3500
3501/* Is the constraint at position "con" marked as being redundant?
3502 * If it is marked as representing an equality, then it is not
3503 * considered to be redundant.
3504 * Note that isl_tab_mark_redundant marks both the isl_tab_var as
3505 * redundant and moves the corresponding row into the first
3506 * tab->n_redundant positions (or removes the row, assigning it index -1),
3507 * so the final test is actually redundant itself.
3508 */
3509int isl_tab_is_redundant(struct isl_tab *tab, int con)
3510{
3511 if (isl_tab_check_con(tab, con) < 0)
3512 return -1;
3513 if (tab->con[con].is_zero)
3514 return 0;
3515 if (tab->con[con].is_redundant)
3516 return 1;
3517 return tab->con[con].is_row && tab->con[con].index < tab->n_redundant;
3518}
3519
3520/* Is variable "var" of "tab" fixed to a constant value by its row
3521 * in the tableau?
3522 * If so and if "value" is not NULL, then store this constant value
3523 * in "value".
3524 *
3525 * That is, is it a row variable that only has non-zero coefficients
3526 * for dead columns?
3527 */
3528static isl_bool is_constant(struct isl_tab *tab, struct isl_tab_var *var,
3529 isl_int *value)
3530{
3531 unsigned off = 2 + tab->M;
3532 isl_mat *mat = tab->mat;
3533 int n;
3534 int row;
3535
3536 if (!var->is_row)
3537 return isl_bool_false;
3538 row = var->index;
3539 if (row_is_big(tab, row))
3540 return isl_bool_false;
3541 n = tab->n_col - tab->n_dead;
3542 if (isl_seq_any_non_zero(mat->row[row] + off + tab->n_dead, n))
3543 return isl_bool_false;
3544 if (value)
3545 isl_int_divexact(*value, mat->row[row][1], mat->row[row][0]);
3546 return isl_bool_true;
3547}
3548
3549/* Has the variable "var' of "tab" reached a value that is greater than
3550 * or equal (if sgn > 0) or smaller than or equal (if sgn < 0) to "target"?
3551 * "tmp" has been initialized by the caller and can be used
3552 * to perform local computations.
3553 *
3554 * If the sample value involves the big parameter, then any value
3555 * is reached.
3556 * Otherwise check if n/d >= t, i.e., n >= d * t (if sgn > 0)
3557 * or n/d <= t, i.e., n <= d * t (if sgn < 0).
3558 */
3559static int reached(struct isl_tab *tab, struct isl_tab_var *var, int sgn,
3560 isl_int target, isl_int *tmp)
3561{
3562 if (row_is_big(tab, var->index))
3563 return 1;
3564 isl_int_mul(*tmp, tab->mat->row[var->index][0], target);
3565 if (sgn > 0)
3566 return isl_int_ge(tab->mat->row[var->index][1], *tmp);
3567 else
3568 return isl_int_le(tab->mat->row[var->index][1], *tmp);
3569}
3570
3571/* Can variable "var" of "tab" attain the value "target" by
3572 * pivoting up (if sgn > 0) or down (if sgn < 0)?
3573 * If not, then pivot up [down] to the greatest [smallest]
3574 * rational value.
3575 * "tmp" has been initialized by the caller and can be used
3576 * to perform local computations.
3577 *
3578 * If the variable is manifestly unbounded in the desired direction,
3579 * then it can attain any value.
3580 * Otherwise, it can be moved to a row.
3581 * Continue pivoting until the target is reached.
3582 * If no more pivoting can be performed, the maximal [minimal]
3583 * rational value has been reached and the target cannot be reached.
3584 * If the variable would be pivoted into a manifestly unbounded column,
3585 * then the target can be reached.
3586 */
3587static isl_bool var_reaches(struct isl_tab *tab, struct isl_tab_var *var,
3588 int sgn, isl_int target, isl_int *tmp)
3589{
3590 int row, col;
3591
3592 if (sgn < 0 && min_is_manifestly_unbounded(tab, var))
3593 return isl_bool_true;
3594 if (sgn > 0 && max_is_manifestly_unbounded(tab, var))
3595 return isl_bool_true;
3596 if (to_row(tab, var, sgn) < 0)
3597 return isl_bool_error;
3598 while (!reached(tab, var, sgn, target, tmp)) {
3599 find_pivot(tab, var, var, sgn, &row, &col);
3600 if (row == -1)
3601 return isl_bool_false;
3602 if (row == var->index)
3603 return isl_bool_true;
3604 if (isl_tab_pivot(tab, row, col) < 0)
3605 return isl_bool_error;
3606 }
3607
3608 return isl_bool_true;
3609}
3610
3611/* Check if variable "var" of "tab" can only attain a single (integer)
3612 * value, and, if so, add an equality constraint to fix the variable
3613 * to this single value and store the result in "target".
3614 * "target" and "tmp" have been initialized by the caller.
3615 *
3616 * Given the current sample value, round it down and check
3617 * whether it is possible to attain a strictly smaller integer value.
3618 * If so, the variable is not restricted to a single integer value.
3619 * Otherwise, the search stops at the smallest rational value.
3620 * Round up this value and check whether it is possible to attain
3621 * a strictly greater integer value.
3622 * If so, the variable is not restricted to a single integer value.
3623 * Otherwise, the search stops at the greatest rational value.
3624 * If rounding down this value yields a value that is different
3625 * from rounding up the smallest rational value, then the variable
3626 * cannot attain any integer value. Mark the tableau empty.
3627 * Otherwise, add an equality constraint that fixes the variable
3628 * to the single integer value found.
3629 */
3631 struct isl_tab_var *var, isl_int *target, isl_int *tmp)
3632{
3634 isl_vec *eq;
3635 int pos;
3636 isl_stat r;
3637
3638 get_rounded_sample_value(tab, var, -1, target);
3639 isl_int_sub_ui(*target, *target, 1);
3640 reached = var_reaches(tab, var, -1, *target, tmp);
3641 if (reached < 0 || reached)
3642 return isl_bool_not(reached);
3643 get_rounded_sample_value(tab, var, 1, target);
3644 isl_int_add_ui(*target, *target, 1);
3645 reached = var_reaches(tab, var, 1, *target, tmp);
3646 if (reached < 0 || reached)
3647 return isl_bool_not(reached);
3648 get_rounded_sample_value(tab, var, -1, tmp);
3649 isl_int_sub_ui(*target, *target, 1);
3650 if (isl_int_ne(*target, *tmp)) {
3651 if (isl_tab_mark_empty(tab) < 0)
3652 return isl_bool_error;
3653 return isl_bool_false;
3654 }
3655
3656 if (isl_tab_extend_cons(tab, 1) < 0)
3657 return isl_bool_error;
3658 eq = isl_vec_alloc(isl_tab_get_ctx(tab), 1 + tab->n_var);
3659 if (!eq)
3660 return isl_bool_error;
3661 pos = var - tab->var;
3662 isl_seq_clr(eq->el + 1, tab->n_var);
3663 isl_int_set_si(eq->el[1 + pos], -1);
3664 isl_int_set(eq->el[0], *target);
3665 r = isl_tab_add_eq(tab, eq->el);
3666 isl_vec_free(eq);
3667
3668 return r < 0 ? isl_bool_error : isl_bool_true;
3669}
3670
3671/* Check if variable "var" of "tab" can only attain a single (integer)
3672 * value, and, if so, add an equality constraint to fix the variable
3673 * to this single value and store the result in "value" (if "value"
3674 * is not NULL).
3675 *
3676 * If the current sample value involves the big parameter,
3677 * then the variable cannot have a fixed integer value.
3678 * If the variable is already fixed to a single value by its row, then
3679 * there is no need to add another equality constraint.
3680 *
3681 * Otherwise, allocate some temporary variables and continue
3682 * with detect_constant_with_tmp.
3683 */
3684static isl_bool get_constant(struct isl_tab *tab, struct isl_tab_var *var,
3685 isl_int *value)
3686{
3687 isl_int target, tmp;
3688 isl_bool is_cst;
3689
3690 if (var->is_row && row_is_big(tab, var->index))
3691 return isl_bool_false;
3692 is_cst = is_constant(tab, var, value);
3693 if (is_cst < 0 || is_cst)
3694 return is_cst;
3695
3696 if (!value)
3697 isl_int_init(target);
3698 isl_int_init(tmp);
3699
3700 is_cst = detect_constant_with_tmp(tab, var,
3701 value ? value : &target, &tmp);
3702
3703 isl_int_clear(tmp);
3704 if (!value)
3705 isl_int_clear(target);
3706
3707 return is_cst;
3708}
3709
3710/* Check if variable "var" of "tab" can only attain a single (integer)
3711 * value, and, if so, add an equality constraint to fix the variable
3712 * to this single value and store the result in "value" (if "value"
3713 * is not NULL).
3714 *
3715 * For rational tableaus, nothing needs to be done.
3716 */
3718{
3719 if (!tab)
3720 return isl_bool_error;
3721 if (var < 0 || var >= tab->n_var)
3723 "position out of bounds", return isl_bool_error);
3724 if (tab->rational)
3725 return isl_bool_false;
3726
3727 return get_constant(tab, &tab->var[var], value);
3728}
3729
3730/* Check if any of the variables of "tab" can only attain a single (integer)
3731 * value, and, if so, add equality constraints to fix those variables
3732 * to these single values.
3733 *
3734 * For rational tableaus, nothing needs to be done.
3735 */
3737{
3738 int i;
3739
3740 if (!tab)
3741 return isl_stat_error;
3742 if (tab->rational)
3743 return isl_stat_ok;
3744
3745 for (i = 0; i < tab->n_var; ++i) {
3746 if (get_constant(tab, &tab->var[i], NULL) < 0)
3747 return isl_stat_error;
3748 }
3749
3750 return isl_stat_ok;
3751}
3752
3753/* Take a snapshot of the tableau that can be restored by a call to
3754 * isl_tab_rollback.
3755 */
3757{
3758 if (!tab)
3759 return NULL;
3760 tab->need_undo = 1;
3761 return tab->top;
3762}
3763
3764/* Does "tab" need to keep track of undo information?
3765 * That is, was a snapshot taken that may need to be restored?
3766 */
3768{
3769 if (!tab)
3770 return isl_bool_error;
3771
3772 return isl_bool_ok(tab->need_undo);
3773}
3774
3775/* Remove all tracking of undo information from "tab", invalidating
3776 * any snapshots that may have been taken of the tableau.
3777 * Since all snapshots have been invalidated, there is also
3778 * no need to start keeping track of undo information again.
3779 */
3781{
3782 if (!tab)
3783 return;
3784
3785 free_undo(tab);
3786 tab->need_undo = 0;
3787}
3788
3789/* Undo the operation performed by isl_tab_relax.
3790 */
3791static isl_stat unrelax(struct isl_tab *tab, struct isl_tab_var *var)
3793static isl_stat unrelax(struct isl_tab *tab, struct isl_tab_var *var)
3794{
3795 unsigned off = 2 + tab->M;
3796
3797 if (!var->is_row && !max_is_manifestly_unbounded(tab, var))
3798 if (to_row(tab, var, 1) < 0)
3799 return isl_stat_error;
3800
3801 if (var->is_row) {
3802 isl_int_sub(tab->mat->row[var->index][1],
3803 tab->mat->row[var->index][1], tab->mat->row[var->index][0]);
3804 if (var->is_nonneg) {
3805 int sgn = restore_row(tab, var);
3806 isl_assert(tab->mat->ctx, sgn >= 0,
3807 return isl_stat_error);
3808 }
3809 } else {
3810 int i;
3811
3812 for (i = 0; i < tab->n_row; ++i) {
3813 if (isl_int_is_zero(tab->mat->row[i][off + var->index]))
3814 continue;
3815 isl_int_add(tab->mat->row[i][1], tab->mat->row[i][1],
3816 tab->mat->row[i][off + var->index]);
3817 }
3818
3819 }
3820
3821 return isl_stat_ok;
3822}
3823
3824/* Undo the operation performed by isl_tab_unrestrict.
3825 *
3826 * In particular, mark the variable as being non-negative and make
3827 * sure the sample value respects this constraint.
3828 */
3829static isl_stat ununrestrict(struct isl_tab *tab, struct isl_tab_var *var)
3830{
3831 var->is_nonneg = 1;
3832
3833 if (var->is_row && restore_row(tab, var) < -1)
3834 return isl_stat_error;
3835
3836 return isl_stat_ok;
3837}
3838
3839/* Unmark the last redundant row in "tab" as being redundant.
3840 * This undoes part of the modifications performed by isl_tab_mark_redundant.
3841 * In particular, remove the redundant mark and make
3842 * sure the sample value respects the constraint again.
3843 * A variable that is marked non-negative by isl_tab_mark_redundant
3844 * is covered by a separate undo record.
3845 */
3847{
3848 struct isl_tab_var *var;
3849
3850 if (tab->n_redundant < 1)
3852 "no redundant rows", return isl_stat_error);
3853
3854 var = isl_tab_var_from_row(tab, tab->n_redundant - 1);
3855 var->is_redundant = 0;
3856 tab->n_redundant--;
3857 restore_row(tab, var);
3858
3859 return isl_stat_ok;
3860}
3861
3862static isl_stat perform_undo_var(struct isl_tab *tab, struct isl_tab_undo *undo)
3864static isl_stat perform_undo_var(struct isl_tab *tab, struct isl_tab_undo *undo)
3865{
3866 struct isl_tab_var *var = var_from_index(tab, undo->u.var_index);
3867 switch (undo->type) {
3869 var->is_nonneg = 0;
3870 break;
3872 if (!var->is_row || var->index != tab->n_redundant - 1)
3874 "not undoing last redundant row",
3875 return isl_stat_error);
3876 return restore_last_redundant(tab);
3878 var->frozen = 0;
3879 break;
3880 case isl_tab_undo_zero:
3881 var->is_zero = 0;
3882 if (!var->is_row)
3883 tab->n_dead--;
3884 break;
3886 if (undo->u.var_index >= 0) {
3887 isl_assert(tab->mat->ctx, !var->is_row,
3888 return isl_stat_error);
3889 return drop_col(tab, var->index);
3890 }
3891 if (!var->is_row) {
3892 if (!max_is_manifestly_unbounded(tab, var)) {
3893 if (to_row(tab, var, 1) < 0)
3894 return isl_stat_error;
3895 } else if (!min_is_manifestly_unbounded(tab, var)) {
3896 if (to_row(tab, var, -1) < 0)
3897 return isl_stat_error;
3898 } else
3899 if (to_row(tab, var, 0) < 0)
3900 return isl_stat_error;
3901 }
3902 return drop_row(tab, var->index);
3903 case isl_tab_undo_relax:
3904 return unrelax(tab, var);
3906 return ununrestrict(tab, var);
3907 default:
3909 "perform_undo_var called on invalid undo record",
3910 return isl_stat_error);
3911 }
3912
3913 return isl_stat_ok;
3914}
3915
3916/* Restore all rows that have been marked redundant by isl_tab_mark_redundant
3917 * and that have been preserved in the tableau.
3918 * Note that isl_tab_mark_redundant may also have marked some variables
3919 * as being non-negative before marking them redundant. These need
3920 * to be removed as well as otherwise some constraints could end up
3921 * getting marked redundant with respect to the variable.
3922 */
3924{
3925 if (!tab)
3926 return isl_stat_error;
3927
3928 if (tab->need_undo)
3930 "manually restoring redundant constraints "
3931 "interferes with undo history",
3932 return isl_stat_error);
3933
3934 while (tab->n_redundant > 0) {
3935 if (tab->row_var[tab->n_redundant - 1] >= 0) {
3936 struct isl_tab_var *var;
3937
3938 var = isl_tab_var_from_row(tab, tab->n_redundant - 1);
3939 var->is_nonneg = 0;
3940 }
3942 }
3943 return isl_stat_ok;
3944}
3945
3946/* Undo the addition of an integer division to the basic map representation
3947 * of "tab" in position "pos".
3948 */
3949static isl_stat drop_bmap_div(struct isl_tab *tab, int pos)
3950{
3951 int off;
3952 isl_size n_div;
3953
3954 n_div = isl_basic_map_dim(tab->bmap, isl_dim_div);
3955 if (n_div < 0)
3956 return isl_stat_error;
3957 off = tab->n_var - n_div;
3958 tab->bmap = isl_basic_map_drop_div(tab->bmap, pos - off);
3959 if (!tab->bmap)
3960 return isl_stat_error;
3961 if (tab->samples) {
3962 tab->samples = isl_mat_drop_cols(tab->samples, 1 + pos, 1);
3963 if (!tab->samples)
3964 return isl_stat_error;
3965 }
3966
3967 return isl_stat_ok;
3968}
3969
3970/* Restore the tableau to the state where the basic variables
3971 * are those in "col_var".
3972 * We first construct a list of variables that are currently in
3973 * the basis, but shouldn't. Then we iterate over all variables
3974 * that should be in the basis and for each one that is currently
3975 * not in the basis, we exchange it with one of the elements of the
3976 * list constructed before.
3977 * We can always find an appropriate variable to pivot with because
3978 * the current basis is mapped to the old basis by a non-singular
3979 * matrix and so we can never end up with a zero row.
3980 */
3981static int restore_basis(struct isl_tab *tab, int *col_var)
3982{
3983 int i, j;
3984 int n_extra = 0;
3985 int *extra = NULL; /* current columns that contain bad stuff */
3986 unsigned off = 2 + tab->M;
3987
3988 extra = isl_alloc_array(tab->mat->ctx, int, tab->n_col);
3989 if (tab->n_col && !extra)
3990 goto error;
3991 for (i = 0; i < tab->n_col; ++i) {
3992 for (j = 0; j < tab->n_col; ++j)
3993 if (tab->col_var[i] == col_var[j])
3994 break;
3995 if (j < tab->n_col)
3996 continue;
3997 extra[n_extra++] = i;
3998 }
3999 for (i = 0; i < tab->n_col && n_extra > 0; ++i) {
4000 struct isl_tab_var *var;
4001 int row;
4002
4003 for (j = 0; j < tab->n_col; ++j)
4004 if (col_var[i] == tab->col_var[j])
4005 break;
4006 if (j < tab->n_col)
4007 continue;
4008 var = var_from_index(tab, col_var[i]);
4009 row = var->index;
4010 for (j = 0; j < n_extra; ++j)
4011 if (!isl_int_is_zero(tab->mat->row[row][off+extra[j]]))
4012 break;
4013 isl_assert(tab->mat->ctx, j < n_extra, goto error);
4014 if (isl_tab_pivot(tab, row, extra[j]) < 0)
4015 goto error;
4016 extra[j] = extra[--n_extra];
4017 }
4018
4019 free(extra);
4020 return 0;
4021error:
4022 free(extra);
4023 return -1;
4024}
4025
4026/* Remove all samples with index n or greater, i.e., those samples
4027 * that were added since we saved this number of samples in
4028 * isl_tab_save_samples.
4029 */
4030static void drop_samples_since(struct isl_tab *tab, int n)
4031{
4032 int i;
4033
4034 for (i = tab->n_sample - 1; i >= 0 && tab->n_sample > n; --i) {
4035 if (tab->sample_index[i] < n)
4036 continue;
4037
4038 if (i != tab->n_sample - 1) {
4039 int t = tab->sample_index[tab->n_sample-1];
4040 tab->sample_index[tab->n_sample-1] = tab->sample_index[i];
4041 tab->sample_index[i] = t;
4042 isl_mat_swap_rows(tab->samples, tab->n_sample-1, i);
4043 }
4044 tab->n_sample--;
4045 }
4046}
4047
4048static isl_stat perform_undo(struct isl_tab *tab, struct isl_tab_undo *undo)
4050static isl_stat perform_undo(struct isl_tab *tab, struct isl_tab_undo *undo)
4051{
4052 switch (undo->type) {
4054 tab->rational = 0;
4055 break;
4056 case isl_tab_undo_empty:
4057 tab->empty = 0;
4058 break;
4062 case isl_tab_undo_zero:
4064 case isl_tab_undo_relax:
4066 return perform_undo_var(tab, undo);
4068 tab->bmap = isl_basic_map_free_equality(tab->bmap, 1);
4069 return tab->bmap ? isl_stat_ok : isl_stat_error;
4071 tab->bmap = isl_basic_map_free_inequality(tab->bmap, 1);
4072 return tab->bmap ? isl_stat_ok : isl_stat_error;
4074 return drop_bmap_div(tab, undo->u.var_index);
4076 if (restore_basis(tab, undo->u.col_var) < 0)
4077 return isl_stat_error;
4078 break;
4080 tab->n_outside--;
4081 break;
4083 drop_samples_since(tab, undo->u.n);
4084 break;
4086 return undo->u.callback->run(undo->u.callback);
4088 return first_eq_to_ineq(tab, undo->u.n);
4089 default:
4090 isl_assert(tab->mat->ctx, 0, return isl_stat_error);
4091 }
4092 return isl_stat_ok;
4093}
4094
4095/* Return the tableau to the state it was in when the snapshot "snap"
4096 * was taken.
4097 */
4099{
4100 struct isl_tab_undo *undo, *next;
4101
4102 if (!tab)
4103 return isl_stat_error;
4104
4105 tab->in_undo = 1;
4106 for (undo = tab->top; undo && undo != &tab->bottom; undo = next) {
4107 next = undo->next;
4108 if (undo == snap)
4109 break;
4110 if (perform_undo(tab, undo) < 0) {
4111 tab->top = undo;
4112 free_undo(tab);
4113 tab->in_undo = 0;
4114 return isl_stat_error;
4115 }
4116 free_undo_record(undo);
4117 }
4118 tab->in_undo = 0;
4119 tab->top = undo;
4120 if (!undo)
4121 return isl_stat_error;
4122 return isl_stat_ok;
4123}
4124
4125/* The given row "row" represents an inequality violated by all
4126 * points in the tableau. Check for some special cases of such
4127 * separating constraints.
4128 * In particular, if the row has been reduced to the constant -1,
4129 * then we know the inequality is adjacent (but opposite) to
4130 * an equality in the tableau.
4131 * If the row has been reduced to r = c*(-1 -r'), with r' an inequality
4132 * of the tableau and c a positive constant, then the inequality
4133 * is adjacent (but opposite) to the inequality r'.
4134 */
4135static enum isl_ineq_type separation_type(struct isl_tab *tab, unsigned row)
4136{
4137 int pos;
4138 int separate;
4139 unsigned off = 2 + tab->M;
4140
4141 if (tab->rational)
4142 return isl_ineq_separate;
4143
4144 if (!isl_int_is_one(tab->mat->row[row][0]))
4145 return isl_ineq_separate;
4146
4147 pos = isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
4148 tab->n_col - tab->n_dead);
4149 if (pos == -1) {
4150 if (isl_int_is_negone(tab->mat->row[row][1]))
4151 return isl_ineq_adj_eq;
4152 else
4153 return isl_ineq_separate;
4154 }
4155
4156 if (!isl_int_eq(tab->mat->row[row][1],
4157 tab->mat->row[row][off + tab->n_dead + pos]))
4158 return isl_ineq_separate;
4159
4160 separate = isl_seq_any_non_zero(
4161 tab->mat->row[row] + off + tab->n_dead + pos + 1,
4162 tab->n_col - tab->n_dead - pos - 1);
4163
4164 return !separate ? isl_ineq_adj_ineq : isl_ineq_separate;
4165}
4166
4167/* Check the effect of inequality "ineq" on the tableau "tab".
4168 * The result may be
4169 * isl_ineq_redundant: satisfied by all points in the tableau
4170 * isl_ineq_separate: satisfied by no point in the tableau
4171 * isl_ineq_cut: satisfied by some by not all points
4172 * isl_ineq_adj_eq: adjacent to an equality
4173 * isl_ineq_adj_ineq: adjacent to an inequality.
4174 */
4176{
4178 struct isl_tab_undo *snap = NULL;
4179 int con;
4180 int row;
4181
4182 if (!tab)
4183 return isl_ineq_error;
4184
4185 if (isl_tab_extend_cons(tab, 1) < 0)
4186 return isl_ineq_error;
4187
4188 snap = isl_tab_snap(tab);
4189
4190 con = isl_tab_add_row(tab, ineq);
4191 if (con < 0)
4192 goto error;
4193
4194 row = tab->con[con].index;
4195 if (isl_tab_row_is_redundant(tab, row))
4197 else if (isl_int_is_neg(tab->mat->row[row][1]) &&
4198 (tab->rational ||
4199 isl_int_abs_ge(tab->mat->row[row][1],
4200 tab->mat->row[row][0]))) {
4201 int nonneg = at_least_zero(tab, &tab->con[con]);
4202 if (nonneg < 0)
4203 goto error;
4204 if (nonneg)
4206 else
4207 type = separation_type(tab, row);
4208 } else {
4209 int red = con_is_redundant(tab, &tab->con[con]);
4210 if (red < 0)
4211 goto error;
4212 if (!red)
4214 else
4216 }
4217
4218 if (isl_tab_rollback(tab, snap))
4219 return isl_ineq_error;
4220 return type;
4221error:
4222 return isl_ineq_error;
4223}
4224
4226{
4227 bmap = isl_basic_map_cow(bmap);
4228 if (!tab || !bmap)
4229 goto error;
4230
4231 if (tab->empty) {
4232 bmap = isl_basic_map_set_to_empty(bmap);
4233 if (!bmap)
4234 goto error;
4235 tab->bmap = bmap;
4236 return isl_stat_ok;
4237 }
4238
4239 isl_assert(tab->mat->ctx, tab->n_eq == bmap->n_eq, goto error);
4240 isl_assert(tab->mat->ctx,
4241 tab->n_con == bmap->n_eq + bmap->n_ineq, goto error);
4242
4243 tab->bmap = bmap;
4244
4245 return isl_stat_ok;
4246error:
4247 isl_basic_map_free(bmap);
4248 return isl_stat_error;
4249}
4250
4252{
4253 return isl_tab_track_bmap(tab, bset_to_bmap(bset));
4254}
4255
4257{
4258 if (!tab)
4259 return NULL;
4260
4261 return bset_from_bmap(tab->bmap);
4262}
4263
4264/* Print information about a tab variable representing a variable or
4265 * a constraint.
4266 * In particular, print its position (row or column) in the tableau and
4267 * an indication of whether it is zero, redundant and/or frozen.
4268 * Note that only constraints can be frozen.
4269 */
4270static void print_tab_var(FILE *out, struct isl_tab_var *var)
4271{
4272 fprintf(out, "%c%d%s%s", var->is_row ? 'r' : 'c',
4273 var->index,
4274 var->is_zero ? " [=0]" :
4275 var->is_redundant ? " [R]" : "",
4276 var->frozen ? " [F]" : "");
4277}
4278
4280 FILE *out, int indent)
4281{
4282 unsigned r, c;
4283 int i;
4284
4285 if (!tab) {
4286 fprintf(out, "%*snull tab\n", indent, "");
4287 return;
4288 }
4289 fprintf(out, "%*sn_redundant: %d, n_dead: %d", indent, "",
4290 tab->n_redundant, tab->n_dead);
4291 if (tab->rational)
4292 fprintf(out, ", rational");
4293 if (tab->empty)
4294 fprintf(out, ", empty");
4295 fprintf(out, "\n");
4296 fprintf(out, "%*s[", indent, "");
4297 for (i = 0; i < tab->n_var; ++i) {
4298 if (i)
4299 fprintf(out, (i == tab->n_param ||
4300 i == tab->n_var - tab->n_div) ? "; "
4301 : ", ");
4302 print_tab_var(out, &tab->var[i]);
4303 }
4304 fprintf(out, "]\n");
4305 fprintf(out, "%*s[", indent, "");
4306 for (i = 0; i < tab->n_con; ++i) {
4307 if (i)
4308 fprintf(out, ", ");
4309 print_tab_var(out, &tab->con[i]);
4310 }
4311 fprintf(out, "]\n");
4312 fprintf(out, "%*s[", indent, "");
4313 for (i = 0; i < tab->n_row; ++i) {
4314 const char *sign = "";
4315 if (i)
4316 fprintf(out, ", ");
4317 if (tab->row_sign) {
4318 if (tab->row_sign[i] == isl_tab_row_unknown)
4319 sign = "?";
4320 else if (tab->row_sign[i] == isl_tab_row_neg)
4321 sign = "-";
4322 else if (tab->row_sign[i] == isl_tab_row_pos)
4323 sign = "+";
4324 else
4325 sign = "+-";
4326 }
4327 fprintf(out, "r%d: %d%s%s", i, tab->row_var[i],
4328 isl_tab_var_from_row(tab, i)->is_nonneg ? " [>=0]" : "", sign);
4329 }
4330 fprintf(out, "]\n");
4331 fprintf(out, "%*s[", indent, "");
4332 for (i = 0; i < tab->n_col; ++i) {
4333 if (i)
4334 fprintf(out, ", ");
4335 fprintf(out, "c%d: %d%s", i, tab->col_var[i],
4336 var_from_col(tab, i)->is_nonneg ? " [>=0]" : "");
4337 }
4338 fprintf(out, "]\n");
4339 r = tab->mat->n_row;
4340 tab->mat->n_row = tab->n_row;
4341 c = tab->mat->n_col;
4342 tab->mat->n_col = 2 + tab->M + tab->n_col;
4343 isl_mat_print_internal(tab->mat, out, indent);
4344 tab->mat->n_row = r;
4345 tab->mat->n_col = c;
4346 if (tab->bmap)
4347 isl_basic_map_print_internal(tab->bmap, out, indent);
4348}
4349
4351{
4352 isl_tab_print_internal(tab, stderr, 0);
4353}
static __isl_give isl_basic_set * bset_from_bmap(__isl_take isl_basic_map *bmap)
static __isl_give isl_basic_map * bset_to_bmap(__isl_take isl_basic_set *bset)
Definition bset_to_bmap.c:7
#define __isl_take
Definition ctx.h:23
#define isl_calloc_type(ctx, type)
Definition ctx.h:131
isl_stat
Definition ctx.h:85
@ isl_stat_error
Definition ctx.h:86
@ isl_stat_ok
Definition ctx.h:87
#define __isl_give
Definition ctx.h:20
#define ISL_FL_ISSET(l, f)
Definition ctx.h:114
#define isl_die(ctx, errno, msg, code)
Definition ctx.h:139
#define isl_assert(ctx, test, code)
Definition ctx.h:154
isl_bool isl_bool_ok(int b)
Definition isl_ctx.c:58
@ isl_error_invalid
Definition ctx.h:81
@ isl_error_internal
Definition ctx.h:80
#define isl_alloc_array(ctx, type, n)
Definition ctx.h:133
#define ISL_F_ISSET(p, f)
Definition ctx.h:119
#define __isl_keep
Definition ctx.h:26
int isl_size
Definition ctx.h:98
#define isl_alloc_type(ctx, type)
Definition ctx.h:130
#define isl_realloc_array(ctx, ptr, type, n)
Definition ctx.h:136
isl_bool isl_bool_not(isl_bool b)
Definition isl_ctx.c:44
isl_bool
Definition ctx.h:90
@ isl_bool_false
Definition ctx.h:92
@ isl_bool_true
Definition ctx.h:93
@ isl_bool_error
Definition ctx.h:91
m
Definition guard1-0.c:2
isl_stat isl_stat void * user
Definition hmap.h:39
int GMPQAPI sgn(mp_rat op)
static __isl_give isl_ast_expr * var(struct isl_ast_add_term_data *data, enum isl_dim_type type, int pos)
#define WARN_UNUSED
#define __attribute__(x)
static unsigned offset(__isl_keep isl_constraint *c, enum isl_dim_type type)
int isl_ctx_next_operation(isl_ctx *ctx)
Definition isl_ctx.c:83
#define isl_int_is_zero(i)
Definition isl_int.h:31
#define isl_int_is_one(i)
Definition isl_int.h:32
#define isl_int_is_pos(i)
Definition isl_int.h:34
#define isl_int_is_negone(i)
Definition isl_int.h:33
#define isl_int_is_neg(i)
Definition isl_int.h:35
#define isl_int_gcd(r, i, j)
Definition isl_int_gmp.h:42
#define isl_int_neg(r, i)
Definition isl_int_gmp.h:24
#define isl_int_add_ui(r, i, j)
Definition isl_int_gmp.h:27
#define isl_int_le(i, j)
Definition isl_int_gmp.h:60
#define isl_int_add(r, i, j)
Definition isl_int_gmp.h:30
#define isl_int_abs_ge(i, j)
Definition isl_int_gmp.h:68
#define isl_int_ne(i, j)
Definition isl_int_gmp.h:58
#define isl_int_addmul(r, i, j)
Definition isl_int_gmp.h:37
#define isl_int_is_divisible_by(i, j)
Definition isl_int_gmp.h:69
#define isl_int_eq(i, j)
Definition isl_int_gmp.h:57
#define isl_int_set(r, i)
Definition isl_int_gmp.h:14
#define isl_int_cdiv_q(r, i, j)
Definition isl_int_gmp.h:47
#define isl_int_lcm(r, i, j)
Definition isl_int_gmp.h:43
#define isl_int_divexact(r, i, j)
Definition isl_int_gmp.h:44
#define isl_int_sgn(i)
Definition isl_int_gmp.h:54
#define isl_int_mul(r, i, j)
Definition isl_int_gmp.h:32
#define isl_int_lt(i, j)
Definition isl_int_gmp.h:59
#define isl_int_set_si(r, i)
Definition isl_int_gmp.h:15
#define isl_int_ge(i, j)
Definition isl_int_gmp.h:62
mpz_t isl_int
Definition isl_int_gmp.h:9
#define isl_int_sub_ui(r, i, j)
Definition isl_int_gmp.h:28
#define isl_int_fdiv_q(r, i, j)
Definition isl_int_gmp.h:49
#define isl_int_swap(i, j)
Definition isl_int_gmp.h:25
#define isl_int_sub(r, i, j)
Definition isl_int_gmp.h:31
#define isl_int_init(i)
Definition isl_int_gmp.h:11
#define isl_int_clear(i)
Definition isl_int_gmp.h:12
#define isl_int_submul(r, i, j)
Definition isl_int_gmp.h:39
__isl_give dup(__isl_keep LIST(EL) *list)
__isl_give isl_basic_map * isl_basic_map_free_inequality(__isl_take isl_basic_map *bmap, unsigned n)
Definition isl_map.c:1767
__isl_give isl_basic_map * isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap, isl_int *ineq)
Definition isl_map.c:1843
void isl_basic_map_inequality_to_equality(__isl_keep isl_basic_map *bmap, unsigned pos)
Definition isl_map.c:1719
static unsigned pos(__isl_keep isl_space *space, enum isl_dim_type type)
Definition isl_map.c:73
__isl_give isl_basic_map * isl_basic_map_set_to_empty(__isl_take isl_basic_map *bmap)
Definition isl_map.c:2177
__isl_give isl_basic_map * isl_basic_map_free_equality(__isl_take isl_basic_map *bmap, unsigned n)
Definition isl_map.c:1671
int isl_basic_map_drop_inequality(__isl_keep isl_basic_map *bmap, unsigned pos)
Definition isl_map.c:1787
__isl_give isl_basic_map * isl_basic_map_cow(__isl_take isl_basic_map *bmap)
Definition isl_map.c:2064
__isl_give isl_basic_map * isl_basic_map_insert_div(__isl_take isl_basic_map *bmap, int pos, __isl_keep isl_vec *div)
Definition isl_map.c:1909
__isl_give isl_basic_map * isl_basic_map_drop_div(__isl_take isl_basic_map *bmap, unsigned div)
Definition isl_map.c:2707
#define ISL_BASIC_SET_RATIONAL
#define ISL_BASIC_MAP_EMPTY
__isl_give isl_basic_map * isl_basic_map_gauss(__isl_take isl_basic_map *bmap, int *progress)
#define ISL_BASIC_MAP_RATIONAL
isl_bool isl_basic_map_has_single_reference(__isl_keep isl_basic_map *bmap)
__isl_give isl_basic_map * isl_basic_map_gauss5(__isl_take isl_basic_map *bmap, int *progress, isl_stat(*swap)(unsigned a, unsigned b, void *user), isl_stat(*drop)(unsigned n, void *user), void *user)
#define isl_basic_set
__isl_give isl_mat * isl_mat_dup(__isl_keep isl_mat *mat)
Definition isl_mat.c:211
void isl_seq_combine(isl_int *dst, isl_int m1, isl_int *src1, isl_int m2, isl_int *src2, unsigned len)
Definition isl_seq.c:116
int isl_seq_first_non_zero(isl_int *p, unsigned len)
Definition isl_seq.c:207
void isl_seq_clr(isl_int *p, unsigned len)
Definition isl_seq.c:14
void isl_seq_scale(isl_int *dst, isl_int *src, isl_int m, unsigned len)
Definition isl_seq.c:81
void isl_seq_cpy(isl_int *dst, isl_int *src, unsigned len)
Definition isl_seq.c:42
int isl_seq_any_non_zero(isl_int *p, unsigned len)
Definition isl_seq.c:230
void isl_seq_normalize(struct isl_ctx *ctx, isl_int *p, unsigned len)
Definition isl_seq.c:278
void isl_seq_neg(isl_int *dst, isl_int *src, unsigned len)
Definition isl_seq.c:35
int isl_tab_detect_redundant(struct isl_tab *tab)
Definition isl_tab.c:3345
static isl_stat con_drop_entries(struct isl_tab *tab, unsigned first, unsigned n)
Definition isl_tab.c:3139
struct isl_tab * isl_tab_drop_sample(struct isl_tab *tab, int s)
Definition isl_tab.c:925
int isl_tab_min_at_most_neg_one(struct isl_tab *tab, struct isl_tab_var *var)
Definition isl_tab.c:1466
void isl_tab_free(struct isl_tab *tab)
Definition isl_tab.c:206
static isl_stat perform_undo(struct isl_tab *tab, struct isl_tab_undo *undo) WARN_UNUSED
Definition isl_tab.c:4050
static struct isl_tab * add_eq(struct isl_tab *tab, isl_int *eq)
Definition isl_tab.c:2003
static int sign_of_min(struct isl_tab *tab, struct isl_tab_var *var)
Definition isl_tab.c:1390
static isl_stat push_union(struct isl_tab *tab, enum isl_tab_undo_type type, union isl_tab_undo_val u) WARN_UNUSED
Definition isl_tab.c:801
static int sign_of_max(struct isl_tab *tab, struct isl_tab_var *var)
Definition isl_tab.c:1276
static int at_least_one(struct isl_tab *tab, struct isl_tab_var *var)
Definition isl_tab.c:1525
static isl_stat close_row(struct isl_tab *tab, struct isl_tab_var *var, int temp_var) WARN_UNUSED
Definition isl_tab.c:1647
static int con_is_redundant(struct isl_tab *tab, struct isl_tab_var *var)
Definition isl_tab.c:3315
static struct isl_tab_var * select_marked(struct isl_tab *tab)
Definition isl_tab.c:2944
static isl_stat restore_last_redundant(struct isl_tab *tab)
Definition isl_tab.c:3846
static int reached(struct isl_tab *tab, struct isl_tab_var *var, int sgn, isl_int target, isl_int *tmp)
Definition isl_tab.c:3559
static struct isl_tab_var * var_from_col(struct isl_tab *tab, int i)
Definition isl_tab.c:578
static void update_row_sign(struct isl_tab *tab, int row, int col, int row_sgn)
Definition isl_tab.c:1056
static int to_col(struct isl_tab *tab, struct isl_tab_var *var) WARN_UNUSED
Definition isl_tab.c:1966
isl_stat isl_tab_push_basis(struct isl_tab *tab)
Definition isl_tab.c:846
static void print_tab_var(FILE *out, struct isl_tab_var *var)
Definition isl_tab.c:4270
void isl_tab_clear_undo(struct isl_tab *tab)
Definition isl_tab.c:3780
int isl_tab_insert_var(struct isl_tab *tab, int r)
Definition isl_tab.c:1773
isl_stat isl_tab_track_bset(struct isl_tab *tab, __isl_take isl_basic_set *bset)
Definition isl_tab.c:4251
struct isl_tab * isl_tab_init_samples(struct isl_tab *tab)
Definition isl_tab.c:877
int isl_tab_extend_vars(struct isl_tab *tab, unsigned n_new)
Definition isl_tab.c:153
struct isl_tab * isl_tab_product(struct isl_tab *tab1, struct isl_tab *tab2)
Definition isl_tab.c:436
int isl_tab_add_sample(struct isl_tab *tab, __isl_take isl_vec *sample)
Definition isl_tab.c:896
static isl_stat drop_last_con_in_row(struct isl_tab *tab, int r)
Definition isl_tab.c:2675
static void swap_cols(struct isl_tab *tab, int col1, int col2)
Definition isl_tab.c:1547
static enum isl_ineq_type separation_type(struct isl_tab *tab, unsigned row)
Definition isl_tab.c:4135
static isl_stat swap_eq(unsigned a, unsigned b, void *user)
Definition isl_tab.c:3163
int isl_tab_is_redundant(struct isl_tab *tab, int con)
Definition isl_tab.c:3509
struct isl_tab_var * isl_tab_var_from_row(struct isl_tab *tab, int i)
Definition isl_tab.c:573
static int tab_is_manifestly_empty(struct isl_tab *tab)
Definition isl_tab.c:1609
static isl_bool get_constant(struct isl_tab *tab, struct isl_tab_var *var, isl_int *value)
Definition isl_tab.c:3684
static void drop_samples_since(struct isl_tab *tab, int n)
Definition isl_tab.c:4030
int isl_tab_mark_rational(struct isl_tab *tab)
Definition isl_tab.c:997
static int var_insert_entry(struct isl_tab *tab, int first)
Definition isl_tab.c:1719
static int at_least_zero(struct isl_tab *tab, struct isl_tab_var *var)
Definition isl_tab.c:1357
isl_stat isl_tab_track_bmap(struct isl_tab *tab, __isl_take isl_basic_map *bmap)
Definition isl_tab.c:4225
isl_stat isl_tab_add_eq(struct isl_tab *tab, isl_int *eq)
Definition isl_tab.c:2120
static isl_bool is_constant(struct isl_tab *tab, struct isl_tab_var *var, isl_int *value)
Definition isl_tab.c:3528
static isl_stat first_eq_to_ineq(struct isl_tab *tab, int ineq)
Definition isl_tab.c:3298
static isl_stat ununrestrict(struct isl_tab *tab, struct isl_tab_var *var)
Definition isl_tab.c:3829
int isl_tab_mark_redundant(struct isl_tab *tab, int row)
Definition isl_tab.c:968
isl_stat isl_tab_swap_constraints(struct isl_tab *tab, int con1, int con2)
Definition isl_tab.c:3065
static struct isl_vec * extract_integer_sample(struct isl_tab *tab)
Definition isl_tab.c:2554
__isl_keep isl_basic_set * isl_tab_peek_bset(struct isl_tab *tab)
Definition isl_tab.c:4256
int isl_tab_pivot(struct isl_tab *tab, int row, int col)
Definition isl_tab.c:1137
static isl_stat perform_undo_var(struct isl_tab *tab, struct isl_tab_undo *undo) WARN_UNUSED
Definition isl_tab.c:3864
static int var_drop_entry(struct isl_tab *tab, int first)
Definition isl_tab.c:1748
isl_stat isl_tab_push_callback(struct isl_tab *tab, struct isl_tab_callback *callback)
Definition isl_tab.c:859
static isl_stat rotate_constraints_left(struct isl_tab *tab, int first, int n)
Definition isl_tab.c:3112
int isl_tab_relax(struct isl_tab *tab, int con)
Definition isl_tab.c:2790
static __isl_give isl_vec * ineq_for_div(__isl_keep isl_basic_map *bmap, unsigned div)
Definition isl_tab.c:2212
static int may_be_equality(struct isl_tab *tab, int row)
Definition isl_tab.c:2926
static isl_stat unrelax(struct isl_tab *tab, struct isl_tab_var *var) WARN_UNUSED
Definition isl_tab.c:3793
int isl_tab_add_row(struct isl_tab *tab, isl_int *line)
Definition isl_tab.c:1824
static struct isl_tab_var * var_from_index(struct isl_tab *tab, int i)
Definition isl_tab.c:565
static int row_is_manifestly_non_integral(struct isl_tab *tab, int row)
Definition isl_tab.c:1591
static isl_stat drop_col(struct isl_tab *tab, int col)
Definition isl_tab.c:1889
struct isl_tab_undo * isl_tab_snap(struct isl_tab *tab)
Definition isl_tab.c:3756
isl_stat isl_tab_restore_redundant(struct isl_tab *tab)
Definition isl_tab.c:3923
static int min_is_manifestly_unbounded(struct isl_tab *tab, struct isl_tab_var *var)
Definition isl_tab.c:608
int isl_tab_unrestrict(struct isl_tab *tab, int con)
Definition isl_tab.c:2900
int isl_tab_sign_of_max(struct isl_tab *tab, int con)
Definition isl_tab.c:1296
static isl_stat isl_tab_push_ineq_to_eq(struct isl_tab *tab, int ineq)
Definition isl_tab.c:870
int isl_tab_is_equality(struct isl_tab *tab, int con)
Definition isl_tab.c:3399
int isl_tab_kill_col(struct isl_tab *tab, int col)
Definition isl_tab.c:1571
__isl_give isl_basic_set * isl_basic_set_update_from_tab(__isl_take isl_basic_set *bset, struct isl_tab *tab)
Definition isl_tab.c:2665
struct isl_tab * isl_tab_from_recession_cone(__isl_keep isl_basic_set *bset, int parametric)
Definition isl_tab.c:2444
static isl_stat add_div_constraints(struct isl_tab *tab, unsigned div, isl_stat(*add_ineq)(void *user, isl_int *), void *user)
Definition isl_tab.c:2249
isl_stat isl_tab_mark_empty(struct isl_tab *tab)
Definition isl_tab.c:1008
static isl_bool detect_constant_with_tmp(struct isl_tab *tab, struct isl_tab_var *var, isl_int *target, isl_int *tmp)
Definition isl_tab.c:3630
void isl_tab_dump(__isl_keep struct isl_tab *tab)
Definition isl_tab.c:4350
static isl_stat isl_tab_check_con(struct isl_tab *tab, int con)
Definition isl_tab.c:2763
static __isl_give isl_basic_map * gauss_if_shared(__isl_take isl_basic_map *bmap, struct isl_tab *tab)
Definition isl_tab.c:3192
static int row_sgn(struct isl_tab *tab, int row)
Definition isl_tab.c:1321
static void swap_rows(struct isl_tab *tab, int row1, int row2)
Definition isl_tab.c:772
isl_stat isl_tab_add_ineq(struct isl_tab *tab, isl_int *ineq)
Definition isl_tab.c:1908
static __isl_give isl_mat * tab_mat_product(__isl_keep isl_mat *mat1, __isl_keep isl_mat *mat2, unsigned row1, unsigned row2, unsigned col1, unsigned col2, unsigned off, unsigned r1, unsigned r2, unsigned d1, unsigned d2)
Definition isl_tab.c:325
static int row_is_big(struct isl_tab *tab, int row)
Definition isl_tab.c:2034
__isl_give isl_vec * isl_tab_get_sample_value(struct isl_tab *tab)
Definition isl_tab.c:2577
isl_ctx * isl_tab_get_ctx(struct isl_tab *tab)
Definition isl_tab.c:100
static int row_at_most_neg_one(struct isl_tab *tab, int row)
Definition isl_tab.c:1444
int isl_tab_add_valid_eq(struct isl_tab *tab, isl_int *eq)
Definition isl_tab.c:2056
static void free_undo_record(struct isl_tab_undo *undo)
Definition isl_tab.c:184
static isl_stat cut_to_hyperplane(struct isl_tab *tab, struct isl_tab_var *var)
Definition isl_tab.c:2700
static void free_undo(struct isl_tab *tab)
Definition isl_tab.c:195
int isl_tab_add_div(struct isl_tab *tab, __isl_keep isl_vec *div)
Definition isl_tab.c:2384
isl_stat isl_tab_detect_constants(struct isl_tab *tab)
Definition isl_tab.c:3736
int isl_tab_sample_is_integer(struct isl_tab *tab)
Definition isl_tab.c:2535
isl_bool isl_tab_cone_is_bounded(struct isl_tab *tab)
Definition isl_tab.c:2501
__isl_give isl_basic_map * isl_tab_make_equalities_explicit(struct isl_tab *tab, __isl_take isl_basic_map *bmap)
Definition isl_tab.c:3244
isl_bool isl_tab_is_constant(struct isl_tab *tab, int var, isl_int *value)
Definition isl_tab.c:3717
static int row_is_neg(struct isl_tab *tab, int row)
Definition isl_tab.c:1310
static isl_stat drop_eq(unsigned n, void *user)
Definition isl_tab.c:3178
isl_bool isl_tab_need_undo(struct isl_tab *tab)
Definition isl_tab.c:3767
struct isl_tab * isl_tab_dup(struct isl_tab *tab)
Definition isl_tab.c:225
struct isl_tab * isl_tab_alloc(struct isl_ctx *ctx, unsigned n_row, unsigned n_var, unsigned M)
Definition isl_tab.c:33
enum isl_ineq_type isl_tab_ineq_type(struct isl_tab *tab, isl_int *ineq)
Definition isl_tab.c:4175
static isl_bool var_reaches(struct isl_tab *tab, struct isl_tab_var *var, int sgn, isl_int target, isl_int *tmp)
Definition isl_tab.c:3587
static int max_is_manifestly_unbounded(struct isl_tab *tab, struct isl_tab_var *var)
Definition isl_tab.c:587
static int pivot_row(struct isl_tab *tab, struct isl_tab_var *var, int sgn, int c)
Definition isl_tab.c:662
isl_stat isl_tab_push(struct isl_tab *tab, enum isl_tab_undo_type type)
Definition isl_tab.c:837
int isl_tab_shift_var(struct isl_tab *tab, int pos, isl_int shift)
Definition isl_tab.c:2854
static int to_row(struct isl_tab *tab, struct isl_tab_var *var, int sign) WARN_UNUSED
Definition isl_tab.c:1217
int isl_tab_row_is_redundant(struct isl_tab *tab, int row)
Definition isl_tab.c:744
static int add_zero_row(struct isl_tab *tab)
Definition isl_tab.c:2097
enum isl_lp_result isl_tab_min(struct isl_tab *tab, isl_int *f, isl_int denom, isl_int *opt, isl_int *opt_denom, unsigned flags)
Definition isl_tab.c:3432
static isl_stat rotate_constraints_right(struct isl_tab *tab, int first, int n)
Definition isl_tab.c:3087
static void find_pivot(struct isl_tab *tab, struct isl_tab_var *var, struct isl_tab_var *skip_var, int sgn, int *row, int *col)
Definition isl_tab.c:707
static void get_rounded_sample_value(struct isl_tab *tab, struct isl_tab_var *var, int sgn, isl_int *v)
Definition isl_tab.c:2615
static int restore_row(struct isl_tab *tab, struct isl_tab_var *var)
Definition isl_tab.c:1336
static void update_index2(struct isl_tab_var *var, unsigned row1, unsigned col1, unsigned r1, unsigned r2, unsigned d1, unsigned d2)
Definition isl_tab.c:399
int isl_tab_detect_implicit_equalities(struct isl_tab *tab)
Definition isl_tab.c:2981
static void update_index1(struct isl_tab_var *var, unsigned r1, unsigned r2, unsigned d1, unsigned d2)
Definition isl_tab.c:385
static int restore_basis(struct isl_tab *tab, int *col_var)
Definition isl_tab.c:3981
int isl_tab_insert_div(struct isl_tab *tab, int pos, __isl_keep isl_vec *div, isl_stat(*add_ineq)(void *user, isl_int *), void *user)
Definition isl_tab.c:2333
static void check_table(struct isl_tab *tab) __attribute__((unused))
Definition isl_tab.c:1243
__isl_give struct isl_tab * isl_tab_from_basic_map(__isl_keep isl_basic_map *bmap, int track)
Definition isl_tab.c:2396
__isl_give struct isl_tab * isl_tab_from_basic_set(__isl_keep isl_basic_set *bset, int track)
Definition isl_tab.c:2436
isl_stat isl_tab_push_var(struct isl_tab *tab, enum isl_tab_undo_type type, struct isl_tab_var *var)
Definition isl_tab.c:826
static isl_stat drop_bmap_div(struct isl_tab *tab, int pos)
Definition isl_tab.c:3949
isl_stat isl_tab_save_samples(struct isl_tab *tab)
Definition isl_tab.c:945
static isl_stat drop_row(struct isl_tab *tab, int row)
Definition isl_tab.c:1872
isl_stat isl_tab_rollback(struct isl_tab *tab, struct isl_tab_undo *snap)
Definition isl_tab.c:4098
int isl_tab_freeze_constraint(struct isl_tab *tab, int con)
Definition isl_tab.c:1019
static int row_is_manifestly_zero(struct isl_tab *tab, int row)
Definition isl_tab.c:2039
int isl_tab_allocate_con(struct isl_tab *tab)
Definition isl_tab.c:1689
static int update_con_after_move(struct isl_tab *tab, int i, int old)
Definition isl_tab.c:3043
static int div_is_nonneg(struct isl_tab *tab, __isl_keep isl_vec *div)
Definition isl_tab.c:2300
static void isl_tab_print_internal(__isl_keep struct isl_tab *tab, FILE *out, int indent)
Definition isl_tab.c:4279
int isl_tab_select_facet(struct isl_tab *tab, int con)
Definition isl_tab.c:2918
int isl_tab_extend_cons(struct isl_tab *tab, unsigned n_new)
Definition isl_tab.c:105
__isl_give isl_basic_map * isl_basic_map_update_from_tab(__isl_take isl_basic_map *bmap, struct isl_tab *tab)
Definition isl_tab.c:2636
static int row_cmp(struct isl_tab *tab, int r1, int r2, int c, isl_int *t)
Definition isl_tab.c:625
isl_ineq_type
Definition isl_tab.h:230
@ isl_ineq_cut
Definition isl_tab.h:234
@ isl_ineq_adj_eq
Definition isl_tab.h:235
@ isl_ineq_adj_ineq
Definition isl_tab.h:236
@ isl_ineq_redundant
Definition isl_tab.h:232
@ isl_ineq_error
Definition isl_tab.h:231
@ isl_ineq_separate
Definition isl_tab.h:233
#define ISL_TAB_SAVE_DUAL
Definition isl_tab.h:207
isl_tab_row_sign
Definition isl_tab.h:130
@ isl_tab_row_unknown
Definition isl_tab.h:131
@ isl_tab_row_pos
Definition isl_tab.h:132
@ isl_tab_row_neg
Definition isl_tab.h:133
isl_tab_undo_type
Definition isl_tab.h:31
@ isl_tab_undo_bmap_ineq
Definition isl_tab.h:42
@ isl_tab_undo_bottom
Definition isl_tab.h:32
@ isl_tab_undo_bmap_eq
Definition isl_tab.h:43
@ isl_tab_undo_callback
Definition isl_tab.h:48
@ isl_tab_undo_redundant
Definition isl_tab.h:36
@ isl_tab_undo_saved_samples
Definition isl_tab.h:47
@ isl_tab_undo_drop_sample
Definition isl_tab.h:46
@ isl_tab_undo_relax
Definition isl_tab.h:40
@ isl_tab_undo_ineq_to_eq
Definition isl_tab.h:49
@ isl_tab_undo_rational
Definition isl_tab.h:33
@ isl_tab_undo_allocate
Definition isl_tab.h:39
@ isl_tab_undo_freeze
Definition isl_tab.h:37
@ isl_tab_undo_nonneg
Definition isl_tab.h:35
@ isl_tab_undo_saved_basis
Definition isl_tab.h:45
@ isl_tab_undo_unrestrict
Definition isl_tab.h:41
@ isl_tab_undo_zero
Definition isl_tab.h:38
@ isl_tab_undo_bmap_div
Definition isl_tab.h:44
@ isl_tab_undo_empty
Definition isl_tab.h:34
static enum isl_tab_row_sign row_sign(struct isl_tab *tab, struct isl_sol *sol, int row)
enum isl_fold type
Definition isl_test.c:3810
const char * p
Definition isl_test.c:8397
const char * res
Definition isl_test.c:783
const char * f
Definition isl_test.c:8396
static __isl_give isl_union_map * total(__isl_take isl_union_map *umap, __isl_give isl_map *(*fn)(__isl_take isl_map *))
t0 *a *b *t *a *b * t
isl_lp_result
Definition lp.h:17
@ isl_lp_error
Definition lp.h:18
@ isl_lp_ok
Definition lp.h:19
@ isl_lp_empty
Definition lp.h:21
@ isl_lp_unbounded
Definition lp.h:20
__isl_null isl_basic_map * isl_basic_map_free(__isl_take isl_basic_map *bmap)
Definition isl_map.c:1503
isl_size isl_basic_map_dim(__isl_keep isl_basic_map *bmap, enum isl_dim_type type)
Definition isl_map.c:83
__isl_give isl_basic_map * isl_basic_map_copy(__isl_keep isl_basic_map *bmap)
Definition isl_map.c:1479
void isl_basic_map_print_internal(__isl_keep isl_basic_map *bmap, FILE *out, int indent)
Definition isl_map.c:3933
void isl_mat_print_internal(__isl_keep isl_mat *mat, FILE *out, int indent)
Definition isl_mat.c:1472
__isl_give isl_mat * isl_mat_swap_cols(__isl_take isl_mat *mat, unsigned i, unsigned j)
Definition isl_mat.c:1233
__isl_give isl_mat * isl_mat_swap_rows(__isl_take isl_mat *mat, unsigned i, unsigned j)
Definition isl_mat.c:1248
__isl_null isl_mat * isl_mat_free(__isl_take isl_mat *mat)
Definition isl_mat.c:240
__isl_give isl_mat * isl_mat_alloc(isl_ctx *ctx, unsigned n_row, unsigned n_col)
Definition isl_mat.c:53
isl_ctx * isl_mat_get_ctx(__isl_keep isl_mat *mat)
Definition isl_mat.c:25
__isl_give isl_mat * isl_mat_extend(__isl_take isl_mat *mat, unsigned n_row, unsigned n_col)
Definition isl_mat.c:91
__isl_give isl_mat * isl_mat_drop_cols(__isl_take isl_mat *mat, unsigned col, unsigned n)
Definition isl_mat.c:1506
a(0)
b(9)
isl_size isl_basic_set_dim(__isl_keep isl_basic_set *bset, enum isl_dim_type type)
Definition isl_map.c:202
@ isl_dim_param
Definition space_type.h:15
@ isl_dim_all
Definition space_type.h:20
@ isl_dim_div
Definition space_type.h:19
struct isl_ctx * ctx
isl_int ** ineq
isl_int * data
Definition isl_blk.h:21
unsigned n_row
unsigned n_col
struct isl_ctx * ctx
isl_int ** row
isl_stat(* run)(struct isl_tab_callback *cb)
Definition isl_tab.h:53
enum isl_tab_undo_type type
Definition isl_tab.h:64
struct isl_tab_undo * next
Definition isl_tab.h:66
union isl_tab_undo_val u
Definition isl_tab.h:65
unsigned negated
Definition isl_tab.h:28
int index
Definition isl_tab.h:21
unsigned frozen
Definition isl_tab.h:27
unsigned is_nonneg
Definition isl_tab.h:23
unsigned is_zero
Definition isl_tab.h:24
unsigned is_redundant
Definition isl_tab.h:25
unsigned is_row
Definition isl_tab.h:22
struct isl_mat * mat
Definition isl_tab.h:137
unsigned n_con
Definition isl_tab.h:148
unsigned n_col
Definition isl_tab.h:140
struct isl_tab_undo bottom
Definition isl_tab.h:157
unsigned rational
Definition isl_tab.h:178
unsigned need_undo
Definition isl_tab.h:176
unsigned in_undo
Definition isl_tab.h:180
unsigned n_sample
Definition isl_tab.h:163
unsigned n_var
Definition isl_tab.h:144
int n_unbounded
Definition isl_tab.h:169
unsigned cone
Definition isl_tab.h:182
int * col_var
Definition isl_tab.h:154
unsigned empty
Definition isl_tab.h:179
unsigned n_eq
Definition isl_tab.h:149
struct isl_vec * dual
Definition isl_tab.h:160
struct isl_tab_var * con
Definition isl_tab.h:152
unsigned max_con
Definition isl_tab.h:150
unsigned n_dead
Definition isl_tab.h:141
struct isl_basic_map * bmap
Definition isl_tab.h:161
struct isl_mat * samples
Definition isl_tab.h:166
struct isl_tab_undo * top
Definition isl_tab.h:158
int * sample_index
Definition isl_tab.h:165
unsigned strict_redundant
Definition isl_tab.h:175
unsigned M
Definition isl_tab.h:181
enum isl_tab_row_sign * row_sign
Definition isl_tab.h:155
int * row_var
Definition isl_tab.h:153
unsigned n_param
Definition isl_tab.h:145
unsigned n_redundant
Definition isl_tab.h:142
struct isl_mat * basis
Definition isl_tab.h:170
unsigned n_outside
Definition isl_tab.h:164
unsigned n_row
Definition isl_tab.h:139
unsigned n_div
Definition isl_tab.h:146
int n_zero
Definition isl_tab.h:168
struct isl_tab_var * var
Definition isl_tab.h:151
unsigned max_var
Definition isl_tab.h:147
unsigned preserve
Definition isl_tab.h:177
isl_int * el
struct isl_blk block
struct isl_tab_callback * callback
Definition isl_tab.h:60
__isl_null isl_vec * isl_vec_free(__isl_take isl_vec *vec)
Definition isl_vec.c:234
__isl_give isl_vec * isl_vec_normalize(__isl_take isl_vec *vec)
Definition isl_vec.c:456
__isl_give isl_vec * isl_vec_alloc(isl_ctx *ctx, unsigned size)
Definition isl_vec.c:33
n
Definition youcefn.c:8