Coverage Report

Created: 2017-06-23 12:40

/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/llvm/tools/polly/lib/External/isl/isl_tab.c
Line
Count
Source (jump to first uncovered line)
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
33
struct isl_tab *isl_tab_alloc(struct isl_ctx *ctx,
34
  unsigned n_row, unsigned n_var, unsigned M)
35
547k
{
36
547k
  int i;
37
547k
  struct isl_tab *tab;
38
547k
  unsigned off = 2 + M;
39
547k
40
547k
  tab = isl_calloc_type(ctx, struct isl_tab);
41
547k
  if (!tab)
42
0
    return NULL;
43
547k
  tab->mat = isl_mat_alloc(ctx, n_row, off + n_var);
44
547k
  if (!tab->mat)
45
0
    goto error;
46
547k
  
tab->var = 547k
isl_alloc_array547k
(ctx, struct isl_tab_var, n_var);
47
547k
  if (
n_var && 547k
!tab->var546k
)
48
0
    goto error;
49
547k
  
tab->con = 547k
isl_alloc_array547k
(ctx, struct isl_tab_var, n_row);
50
547k
  if (
n_row && 547k
!tab->con547k
)
51
0
    goto error;
52
547k
  
tab->col_var = 547k
isl_alloc_array547k
(ctx, int, n_var);
53
547k
  if (
n_var && 547k
!tab->col_var546k
)
54
0
    goto error;
55
547k
  
tab->row_var = 547k
isl_alloc_array547k
(ctx, int, n_row);
56
547k
  if (
n_row && 547k
!tab->row_var547k
)
57
0
    goto error;
58
2.78M
  
for (i = 0; 547k
i < n_var2.78M
;
++i2.23M
)
{2.23M
59
2.23M
    tab->var[i].index = i;
60
2.23M
    tab->var[i].is_row = 0;
61
2.23M
    tab->var[i].is_nonneg = 0;
62
2.23M
    tab->var[i].is_zero = 0;
63
2.23M
    tab->var[i].is_redundant = 0;
64
2.23M
    tab->var[i].frozen = 0;
65
2.23M
    tab->var[i].negated = 0;
66
2.23M
    tab->col_var[i] = i;
67
2.23M
  }
68
547k
  tab->n_row = 0;
69
547k
  tab->n_con = 0;
70
547k
  tab->n_eq = 0;
71
547k
  tab->max_con = n_row;
72
547k
  tab->n_col = n_var;
73
547k
  tab->n_var = n_var;
74
547k
  tab->max_var = n_var;
75
547k
  tab->n_param = 0;
76
547k
  tab->n_div = 0;
77
547k
  tab->n_dead = 0;
78
547k
  tab->n_redundant = 0;
79
547k
  tab->strict_redundant = 0;
80
547k
  tab->need_undo = 0;
81
547k
  tab->rational = 0;
82
547k
  tab->empty = 0;
83
547k
  tab->in_undo = 0;
84
547k
  tab->M = M;
85
547k
  tab->cone = 0;
86
547k
  tab->bottom.type = isl_tab_undo_bottom;
87
547k
  tab->bottom.next = NULL;
88
547k
  tab->top = &tab->bottom;
89
547k
90
547k
  tab->n_zero = 0;
91
547k
  tab->n_unbounded = 0;
92
547k
  tab->basis = NULL;
93
547k
94
547k
  return tab;
95
0
error:
96
0
  isl_tab_free(tab);
97
0
  return NULL;
98
547k
}
99
100
isl_ctx *isl_tab_get_ctx(struct isl_tab *tab)
101
2.36M
{
102
2.36M
  return tab ? isl_mat_get_ctx(tab->mat) : NULL;
103
2.36M
}
104
105
int isl_tab_extend_cons(struct isl_tab *tab, unsigned n_new)
106
636k
{
107
636k
  unsigned off;
108
636k
109
636k
  if (!tab)
110
0
    return -1;
111
636k
112
636k
  off = 2 + tab->M;
113
636k
114
636k
  if (
tab->max_con < tab->n_con + n_new636k
)
{68.1k
115
68.1k
    struct isl_tab_var *con;
116
68.1k
117
68.1k
    con = isl_realloc_array(tab->mat->ctx, tab->con,
118
68.1k
            struct isl_tab_var, tab->max_con + n_new);
119
68.1k
    if (!con)
120
0
      return -1;
121
68.1k
    tab->con = con;
122
68.1k
    tab->max_con += n_new;
123
68.1k
  }
124
636k
  
if (636k
tab->mat->n_row < tab->n_row + n_new636k
)
{71.3k
125
71.3k
    int *row_var;
126
71.3k
127
71.3k
    tab->mat = isl_mat_extend(tab->mat,
128
71.3k
          tab->n_row + n_new, off + tab->n_col);
129
71.3k
    if (!tab->mat)
130
0
      return -1;
131
71.3k
    
row_var = 71.3k
isl_realloc_array71.3k
(tab->mat->ctx, tab->row_var,
132
71.3k
              int, tab->mat->n_row);
133
71.3k
    if (!row_var)
134
0
      return -1;
135
71.3k
    tab->row_var = row_var;
136
71.3k
    if (
tab->row_sign71.3k
)
{392
137
392
      enum isl_tab_row_sign *s;
138
392
      s = isl_realloc_array(tab->mat->ctx, tab->row_sign,
139
392
          enum isl_tab_row_sign, tab->mat->n_row);
140
392
      if (!s)
141
0
        return -1;
142
392
      tab->row_sign = s;
143
392
    }
144
71.3k
  }
145
636k
  return 0;
146
636k
}
147
148
/* Make room for at least n_new extra variables.
149
 * Return -1 if anything went wrong.
150
 */
151
int isl_tab_extend_vars(struct isl_tab *tab, unsigned n_new)
152
8.53k
{
153
8.53k
  struct isl_tab_var *var;
154
8.53k
  unsigned off = 2 + tab->M;
155
8.53k
156
8.53k
  if (
tab->max_var < tab->n_var + n_new8.53k
)
{4.84k
157
4.84k
    var = isl_realloc_array(tab->mat->ctx, tab->var,
158
4.84k
            struct isl_tab_var, tab->n_var + n_new);
159
4.84k
    if (!var)
160
0
      return -1;
161
4.84k
    tab->var = var;
162
4.84k
    tab->max_var = tab->n_var + n_new;
163
4.84k
  }
164
8.53k
165
8.53k
  
if (8.53k
tab->mat->n_col < off + tab->n_col + n_new8.53k
)
{3.80k
166
3.80k
    int *p;
167
3.80k
168
3.80k
    tab->mat = isl_mat_extend(tab->mat,
169
3.80k
            tab->mat->n_row, off + tab->n_col + n_new);
170
3.80k
    if (!tab->mat)
171
0
      return -1;
172
3.80k
    
p = 3.80k
isl_realloc_array3.80k
(tab->mat->ctx, tab->col_var,
173
3.80k
              int, tab->n_col + n_new);
174
3.80k
    if (!p)
175
0
      return -1;
176
3.80k
    tab->col_var = p;
177
3.80k
  }
178
8.53k
179
8.53k
  return 0;
180
8.53k
}
181
182
static void free_undo_record(struct isl_tab_undo *undo)
183
2.67M
{
184
2.67M
  switch (undo->type) {
185
744
  case isl_tab_undo_saved_basis:
186
744
    free(undo->u.col_var);
187
744
    break;
188
2.67M
  default:;
189
2.67M
  }
190
2.67M
  free(undo);
191
2.67M
}
192
193
static void free_undo(struct isl_tab *tab)
194
553k
{
195
553k
  struct isl_tab_undo *undo, *next;
196
553k
197
974k
  for (undo = tab->top; 
undo && 974k
undo != &tab->bottom974k
;
undo = next421k
)
{421k
198
421k
    next = undo->next;
199
421k
    free_undo_record(undo);
200
421k
  }
201
553k
  tab->top = undo;
202
553k
}
203
204
void isl_tab_free(struct isl_tab *tab)
205
579k
{
206
579k
  if (!tab)
207
26.1k
    return;
208
553k
  free_undo(tab);
209
553k
  isl_mat_free(tab->mat);
210
553k
  isl_vec_free(tab->dual);
211
553k
  isl_basic_map_free(tab->bmap);
212
553k
  free(tab->var);
213
553k
  free(tab->con);
214
553k
  free(tab->row_var);
215
553k
  free(tab->col_var);
216
553k
  free(tab->row_sign);
217
553k
  isl_mat_free(tab->samples);
218
553k
  free(tab->sample_index);
219
553k
  isl_mat_free(tab->basis);
220
553k
  free(tab);
221
553k
}
222
223
struct isl_tab *isl_tab_dup(struct isl_tab *tab)
224
2.35k
{
225
2.35k
  int i;
226
2.35k
  struct isl_tab *dup;
227
2.35k
  unsigned off;
228
2.35k
229
2.35k
  if (!tab)
230
0
    return NULL;
231
2.35k
232
2.35k
  off = 2 + tab->M;
233
2.35k
  dup = isl_calloc_type(tab->mat->ctx, struct isl_tab);
234
2.35k
  if (!dup)
235
0
    return NULL;
236
2.35k
  dup->mat = isl_mat_dup(tab->mat);
237
2.35k
  if (!dup->mat)
238
0
    goto error;
239
2.35k
  
dup->var = 2.35k
isl_alloc_array2.35k
(tab->mat->ctx, struct isl_tab_var, tab->max_var);
240
2.35k
  if (
tab->max_var && 2.35k
!dup->var2.35k
)
241
0
    goto error;
242
21.6k
  
for (i = 0; 2.35k
i < tab->n_var21.6k
;
++i19.3k
)
243
19.3k
    dup->var[i] = tab->var[i];
244
2.35k
  dup->con = isl_alloc_array(tab->mat->ctx, struct isl_tab_var, tab->max_con);
245
2.35k
  if (
tab->max_con && 2.35k
!dup->con2.35k
)
246
0
    goto error;
247
24.5k
  
for (i = 0; 2.35k
i < tab->n_con24.5k
;
++i22.2k
)
248
22.2k
    dup->con[i] = tab->con[i];
249
2.35k
  dup->col_var = isl_alloc_array(tab->mat->ctx, int, tab->mat->n_col - off);
250
2.35k
  if (
(tab->mat->n_col - off) && 2.35k
!dup->col_var2.35k
)
251
0
    goto error;
252
11.2k
  
for (i = 0; 2.35k
i < tab->n_col11.2k
;
++i8.89k
)
253
8.89k
    dup->col_var[i] = tab->col_var[i];
254
2.35k
  dup->row_var = isl_alloc_array(tab->mat->ctx, int, tab->mat->n_row);
255
2.35k
  if (
tab->mat->n_row && 2.35k
!dup->row_var2.35k
)
256
0
    goto error;
257
24.4k
  
for (i = 0; 2.35k
i < tab->n_row24.4k
;
++i22.0k
)
258
22.0k
    dup->row_var[i] = tab->row_var[i];
259
2.35k
  if (
tab->row_sign2.35k
)
{2.34k
260
2.34k
    dup->row_sign = isl_alloc_array(tab->mat->ctx, enum isl_tab_row_sign,
261
2.34k
            tab->mat->n_row);
262
2.34k
    if (
tab->mat->n_row && 2.34k
!dup->row_sign2.34k
)
263
0
      goto error;
264
24.3k
    
for (i = 0; 2.34k
i < tab->n_row24.3k
;
++i22.0k
)
265
22.0k
      dup->row_sign[i] = tab->row_sign[i];
266
2.34k
  }
267
2.35k
  
if (2.35k
tab->samples2.35k
)
{0
268
0
    dup->samples = isl_mat_dup(tab->samples);
269
0
    if (!dup->samples)
270
0
      goto error;
271
0
    
dup->sample_index = 0
isl_alloc_array0
(tab->mat->ctx, int,
272
0
              tab->samples->n_row);
273
0
    if (
tab->samples->n_row && 0
!dup->sample_index0
)
274
0
      goto error;
275
0
    dup->n_sample = tab->n_sample;
276
0
    dup->n_outside = tab->n_outside;
277
0
  }
278
2.35k
  dup->n_row = tab->n_row;
279
2.35k
  dup->n_con = tab->n_con;
280
2.35k
  dup->n_eq = tab->n_eq;
281
2.35k
  dup->max_con = tab->max_con;
282
2.35k
  dup->n_col = tab->n_col;
283
2.35k
  dup->n_var = tab->n_var;
284
2.35k
  dup->max_var = tab->max_var;
285
2.35k
  dup->n_param = tab->n_param;
286
2.35k
  dup->n_div = tab->n_div;
287
2.35k
  dup->n_dead = tab->n_dead;
288
2.35k
  dup->n_redundant = tab->n_redundant;
289
2.35k
  dup->rational = tab->rational;
290
2.35k
  dup->empty = tab->empty;
291
2.35k
  dup->strict_redundant = 0;
292
2.35k
  dup->need_undo = 0;
293
2.35k
  dup->in_undo = 0;
294
2.35k
  dup->M = tab->M;
295
2.35k
  tab->cone = tab->cone;
296
2.35k
  dup->bottom.type = isl_tab_undo_bottom;
297
2.35k
  dup->bottom.next = NULL;
298
2.35k
  dup->top = &dup->bottom;
299
2.35k
300
2.35k
  dup->n_zero = tab->n_zero;
301
2.35k
  dup->n_unbounded = tab->n_unbounded;
302
2.35k
  dup->basis = isl_mat_dup(tab->basis);
303
2.35k
304
2.35k
  return dup;
305
0
error:
306
0
  isl_tab_free(dup);
307
0
  return NULL;
308
2.35k
}
309
310
/* Construct the coefficient matrix of the product tableau
311
 * of two tableaus.
312
 * mat{1,2} is the coefficient matrix of tableau {1,2}
313
 * row{1,2} is the number of rows in tableau {1,2}
314
 * col{1,2} is the number of columns in tableau {1,2}
315
 * off is the offset to the coefficient column (skipping the
316
 *  denominator, the constant term and the big parameter if any)
317
 * r{1,2} is the number of redundant rows in tableau {1,2}
318
 * d{1,2} is the number of dead columns in tableau {1,2}
319
 *
320
 * The order of the rows and columns in the result is as explained
321
 * in isl_tab_product.
322
 */
323
static struct isl_mat *tab_mat_product(struct isl_mat *mat1,
324
  struct isl_mat *mat2, unsigned row1, unsigned row2,
325
  unsigned col1, unsigned col2,
326
  unsigned off, unsigned r1, unsigned r2, unsigned d1, unsigned d2)
327
3.30k
{
328
3.30k
  int i;
329
3.30k
  struct isl_mat *prod;
330
3.30k
  unsigned n;
331
3.30k
332
3.30k
  prod = isl_mat_alloc(mat1->ctx, mat1->n_row + mat2->n_row,
333
3.30k
          off + col1 + col2);
334
3.30k
  if (!prod)
335
0
    return NULL;
336
3.30k
337
3.30k
  n = 0;
338
16.3k
  for (i = 0; 
i < r116.3k
;
++i13.0k
)
{13.0k
339
13.0k
    isl_seq_cpy(prod->row[n + i], mat1->row[i], off + d1);
340
13.0k
    isl_seq_clr(prod->row[n + i] + off + d1, d2);
341
13.0k
    isl_seq_cpy(prod->row[n + i] + off + d1 + d2,
342
13.0k
        mat1->row[i] + off + d1, col1 - d1);
343
13.0k
    isl_seq_clr(prod->row[n + i] + off + col1 + d1, col2 - d2);
344
13.0k
  }
345
3.30k
346
3.30k
  n += r1;
347
16.3k
  for (i = 0; 
i < r216.3k
;
++i13.0k
)
{13.0k
348
13.0k
    isl_seq_cpy(prod->row[n + i], mat2->row[i], off);
349
13.0k
    isl_seq_clr(prod->row[n + i] + off, d1);
350
13.0k
    isl_seq_cpy(prod->row[n + i] + off + d1,
351
13.0k
          mat2->row[i] + off, d2);
352
13.0k
    isl_seq_clr(prod->row[n + i] + off + d1 + d2, col1 - d1);
353
13.0k
    isl_seq_cpy(prod->row[n + i] + off + col1 + d1,
354
13.0k
          mat2->row[i] + off + d2, col2 - d2);
355
13.0k
  }
356
3.30k
357
3.30k
  n += r2;
358
37.4k
  for (i = 0; 
i < row1 - r137.4k
;
++i34.0k
)
{34.0k
359
34.0k
    isl_seq_cpy(prod->row[n + i], mat1->row[r1 + i], off + d1);
360
34.0k
    isl_seq_clr(prod->row[n + i] + off + d1, d2);
361
34.0k
    isl_seq_cpy(prod->row[n + i] + off + d1 + d2,
362
34.0k
        mat1->row[r1 + i] + off + d1, col1 - d1);
363
34.0k
    isl_seq_clr(prod->row[n + i] + off + col1 + d1, col2 - d2);
364
34.0k
  }
365
3.30k
366
3.30k
  n += row1 - r1;
367
37.4k
  for (i = 0; 
i < row2 - r237.4k
;
++i34.0k
)
{34.0k
368
34.0k
    isl_seq_cpy(prod->row[n + i], mat2->row[r2 + i], off);
369
34.0k
    isl_seq_clr(prod->row[n + i] + off, d1);
370
34.0k
    isl_seq_cpy(prod->row[n + i] + off + d1,
371
34.0k
          mat2->row[r2 + i] + off, d2);
372
34.0k
    isl_seq_clr(prod->row[n + i] + off + d1 + d2, col1 - d1);
373
34.0k
    isl_seq_cpy(prod->row[n + i] + off + col1 + d1,
374
34.0k
          mat2->row[r2 + i] + off + d2, col2 - d2);
375
34.0k
  }
376
3.30k
377
3.30k
  return prod;
378
3.30k
}
379
380
/* Update the row or column index of a variable that corresponds
381
 * to a variable in the first input tableau.
382
 */
383
static void update_index1(struct isl_tab_var *var,
384
  unsigned r1, unsigned r2, unsigned d1, unsigned d2)
385
65.7k
{
386
65.7k
  if (var->index == -1)
387
309
    return;
388
65.4k
  
if (65.4k
var->is_row && 65.4k
var->index >= r147.1k
)
389
34.0k
    var->index += r2;
390
65.4k
  if (
!var->is_row && 65.4k
var->index >= d118.3k
)
391
15.8k
    var->index += d2;
392
65.4k
}
393
394
/* Update the row or column index of a variable that corresponds
395
 * to a variable in the second input tableau.
396
 */
397
static void update_index2(struct isl_tab_var *var,
398
  unsigned row1, unsigned col1,
399
  unsigned r1, unsigned r2, unsigned d1, unsigned d2)
400
65.7k
{
401
65.7k
  if (var->index == -1)
402
309
    return;
403
65.4k
  
if (65.4k
var->is_row65.4k
)
{47.1k
404
47.1k
    if (var->index < r2)
405
13.0k
      var->index += r1;
406
47.1k
    else
407
34.0k
      var->index += row1;
408
18.3k
  } else {
409
18.3k
    if (var->index < d2)
410
2.54k
      var->index += d1;
411
18.3k
    else
412
15.8k
      var->index += col1;
413
18.3k
  }
414
65.4k
}
415
416
/* Create a tableau that represents the Cartesian product of the sets
417
 * represented by tableaus tab1 and tab2.
418
 * The order of the rows in the product is
419
 *  - redundant rows of tab1
420
 *  - redundant rows of tab2
421
 *  - non-redundant rows of tab1
422
 *  - non-redundant rows of tab2
423
 * The order of the columns is
424
 *  - denominator
425
 *  - constant term
426
 *  - coefficient of big parameter, if any
427
 *  - dead columns of tab1
428
 *  - dead columns of tab2
429
 *  - live columns of tab1
430
 *  - live columns of tab2
431
 * The order of the variables and the constraints is a concatenation
432
 * of order in the two input tableaus.
433
 */
434
struct isl_tab *isl_tab_product(struct isl_tab *tab1, struct isl_tab *tab2)
435
3.30k
{
436
3.30k
  int i;
437
3.30k
  struct isl_tab *prod;
438
3.30k
  unsigned off;
439
3.30k
  unsigned r1, r2, d1, d2;
440
3.30k
441
3.30k
  if (
!tab1 || 3.30k
!tab23.30k
)
442
0
    return NULL;
443
3.30k
444
3.30k
  
isl_assert3.30k
(tab1->mat->ctx, tab1->M == tab2->M, return NULL);3.30k
445
3.30k
  
isl_assert3.30k
(tab1->mat->ctx, tab1->rational == tab2->rational, return NULL);3.30k
446
3.30k
  
isl_assert3.30k
(tab1->mat->ctx, tab1->cone == tab2->cone, return NULL);3.30k
447
3.30k
  
isl_assert3.30k
(tab1->mat->ctx, !tab1->row_sign, return NULL);3.30k
448
3.30k
  
isl_assert3.30k
(tab1->mat->ctx, !tab2->row_sign, return NULL);3.30k
449
3.30k
  
isl_assert3.30k
(tab1->mat->ctx, tab1->n_param == 0, return NULL);3.30k
450
3.30k
  
isl_assert3.30k
(tab1->mat->ctx, tab2->n_param == 0, return NULL);3.30k
451
3.30k
  
isl_assert3.30k
(tab1->mat->ctx, tab1->n_div == 0, return NULL);3.30k
452
3.30k
  
isl_assert3.30k
(tab1->mat->ctx, tab2->n_div == 0, return NULL);3.30k
453
3.30k
454
3.30k
  off = 2 + tab1->M;
455
3.30k
  r1 = tab1->n_redundant;
456
3.30k
  r2 = tab2->n_redundant;
457
3.30k
  d1 = tab1->n_dead;
458
3.30k
  d2 = tab2->n_dead;
459
3.30k
  prod = isl_calloc_type(tab1->mat->ctx, struct isl_tab);
460
3.30k
  if (!prod)
461
0
    return NULL;
462
3.30k
  prod->mat = tab_mat_product(tab1->mat, tab2->mat,
463
3.30k
        tab1->n_row, tab2->n_row,
464
3.30k
        tab1->n_col, tab2->n_col, off, r1, r2, d1, d2);
465
3.30k
  if (!prod->mat)
466
0
    goto error;
467
3.30k
  
prod->var = 3.30k
isl_alloc_array3.30k
(tab1->mat->ctx, struct isl_tab_var,
468
3.30k
          tab1->max_var + tab2->max_var);
469
3.30k
  if (
(tab1->max_var + tab2->max_var) && 3.30k
!prod->var3.30k
)
470
0
    goto error;
471
21.9k
  
for (i = 0; 3.30k
i < tab1->n_var21.9k
;
++i18.6k
)
{18.6k
472
18.6k
    prod->var[i] = tab1->var[i];
473
18.6k
    update_index1(&prod->var[i], r1, r2, d1, d2);
474
18.6k
  }
475
21.9k
  for (i = 0; 
i < tab2->n_var21.9k
;
++i18.6k
)
{18.6k
476
18.6k
    prod->var[tab1->n_var + i] = tab2->var[i];
477
18.6k
    update_index2(&prod->var[tab1->n_var + i],
478
18.6k
        tab1->n_row, tab1->n_col,
479
18.6k
        r1, r2, d1, d2);
480
18.6k
  }
481
3.30k
  prod->con = isl_alloc_array(tab1->mat->ctx, struct isl_tab_var,
482
3.30k
          tab1->max_con +  tab2->max_con);
483
3.30k
  if (
(tab1->max_con + tab2->max_con) && 3.30k
!prod->con3.30k
)
484
0
    goto error;
485
50.4k
  
for (i = 0; 3.30k
i < tab1->n_con50.4k
;
++i47.1k
)
{47.1k
486
47.1k
    prod->con[i] = tab1->con[i];
487
47.1k
    update_index1(&prod->con[i], r1, r2, d1, d2);
488
47.1k
  }
489
50.4k
  for (i = 0; 
i < tab2->n_con50.4k
;
++i47.1k
)
{47.1k
490
47.1k
    prod->con[tab1->n_con + i] = tab2->con[i];
491
47.1k
    update_index2(&prod->con[tab1->n_con + i],
492
47.1k
        tab1->n_row, tab1->n_col,
493
47.1k
        r1, r2, d1, d2);
494
47.1k
  }
495
3.30k
  prod->col_var = isl_alloc_array(tab1->mat->ctx, int,
496
3.30k
          tab1->n_col + tab2->n_col);
497
3.30k
  if (
(tab1->n_col + tab2->n_col) && 3.30k
!prod->col_var3.30k
)
498
0
    goto error;
499
21.6k
  
for (i = 0; 3.30k
i < tab1->n_col21.6k
;
++i18.3k
)
{18.3k
500
15.8k
    int pos = i < d1 ? 
i2.54k
:
i + d215.8k
;
501
18.3k
    prod->col_var[pos] = tab1->col_var[i];
502
18.3k
  }
503
21.6k
  for (i = 0; 
i < tab2->n_col21.6k
;
++i18.3k
)
{18.3k
504
15.8k
    int pos = i < d2 ? 
d1 + i2.54k
:
tab1->n_col + i15.8k
;
505
18.3k
    int t = tab2->col_var[i];
506
18.3k
    if (t >= 0)
507
234
      t += tab1->n_var;
508
18.3k
    else
509
18.1k
      t -= tab1->n_con;
510
18.3k
    prod->col_var[pos] = t;
511
18.3k
  }
512
3.30k
  prod->row_var = isl_alloc_array(tab1->mat->ctx, int,
513
3.30k
          tab1->mat->n_row + tab2->mat->n_row);
514
3.30k
  if (
(tab1->mat->n_row + tab2->mat->n_row) && 3.30k
!prod->row_var3.30k
)
515
0
    goto error;
516
50.4k
  
for (i = 0; 3.30k
i < tab1->n_row50.4k
;
++i47.1k
)
{47.1k
517
34.0k
    int pos = i < r1 ? 
i13.0k
:
i + r234.0k
;
518
47.1k
    prod->row_var[pos] = tab1->row_var[i];
519
47.1k
  }
520
50.4k
  for (i = 0; 
i < tab2->n_row50.4k
;
++i47.1k
)
{47.1k
521
34.0k
    int pos = i < r2 ? 
r1 + i13.0k
:
tab1->n_row + i34.0k
;
522
47.1k
    int t = tab2->row_var[i];
523
47.1k
    if (t >= 0)
524
18.4k
      t += tab1->n_var;
525
47.1k
    else
526
28.6k
      t -= tab1->n_con;
527
47.1k
    prod->row_var[pos] = t;
528
47.1k
  }
529
3.30k
  prod->samples = NULL;
530
3.30k
  prod->sample_index = NULL;
531
3.30k
  prod->n_row = tab1->n_row + tab2->n_row;
532
3.30k
  prod->n_con = tab1->n_con + tab2->n_con;
533
3.30k
  prod->n_eq = 0;
534
3.30k
  prod->max_con = tab1->max_con + tab2->max_con;
535
3.30k
  prod->n_col = tab1->n_col + tab2->n_col;
536
3.30k
  prod->n_var = tab1->n_var + tab2->n_var;
537
3.30k
  prod->max_var = tab1->max_var + tab2->max_var;
538
3.30k
  prod->n_param = 0;
539
3.30k
  prod->n_div = 0;
540
3.30k
  prod->n_dead = tab1->n_dead + tab2->n_dead;
541
3.30k
  prod->n_redundant = tab1->n_redundant + tab2->n_redundant;
542
3.30k
  prod->rational = tab1->rational;
543
3.30k
  prod->empty = tab1->empty || tab2->empty;
544
3.30k
  prod->strict_redundant = tab1->strict_redundant || tab2->strict_redundant;
545
3.30k
  prod->need_undo = 0;
546
3.30k
  prod->in_undo = 0;
547
3.30k
  prod->M = tab1->M;
548
3.30k
  prod->cone = tab1->cone;
549
3.30k
  prod->bottom.type = isl_tab_undo_bottom;
550
3.30k
  prod->bottom.next = NULL;
551
3.30k
  prod->top = &prod->bottom;
552
3.30k
553
3.30k
  prod->n_zero = 0;
554
3.30k
  prod->n_unbounded = 0;
555
3.30k
  prod->basis = NULL;
556
3.30k
557
3.30k
  return prod;
558
0
error:
559
0
  isl_tab_free(prod);
560
0
  return NULL;
561
3.30k
}
562
563
static struct isl_tab_var *var_from_index(struct isl_tab *tab, int i)
564
76.6M
{
565
76.6M
  if (i >= 0)
566
21.1M
    return &tab->var[i];
567
76.6M
  else
568
55.4M
    return &tab->con[~i];
569
76.6M
}
570
571
struct isl_tab_var *isl_tab_var_from_row(struct isl_tab *tab, int i)
572
56.6M
{
573
56.6M
  return var_from_index(tab, tab->row_var[i]);
574
56.6M
}
575
576
static struct isl_tab_var *var_from_col(struct isl_tab *tab, int i)
577
18.0M
{
578
18.0M
  return var_from_index(tab, tab->col_var[i]);
579
18.0M
}
580
581
/* Check if there are any upper bounds on column variable "var",
582
 * i.e., non-negative rows where var appears with a negative coefficient.
583
 * Return 1 if there are no such bounds.
584
 */
585
static int max_is_manifestly_unbounded(struct isl_tab *tab,
586
  struct isl_tab_var *var)
587
1.40M
{
588
1.40M
  int i;
589
1.40M
  unsigned off = 2 + tab->M;
590
1.40M
591
1.40M
  if (var->is_row)
592
850k
    return 0;
593
2.84M
  
for (i = tab->n_redundant; 552k
i < tab->n_row2.84M
;
++i2.29M
)
{2.44M
594
2.44M
    if (
!2.44M
isl_int_is_neg2.44M
(tab->mat->row[i][off + var->index]))
595
2.04M
      continue;
596
400k
    
if (400k
isl_tab_var_from_row(tab, i)->is_nonneg400k
)
597
151k
      return 0;
598
400k
  }
599
400k
  return 1;
600
552k
}
601
602
/* Check if there are any lower bounds on column variable "var",
603
 * i.e., non-negative rows where var appears with a positive coefficient.
604
 * Return 1 if there are no such bounds.
605
 */
606
static int min_is_manifestly_unbounded(struct isl_tab *tab,
607
  struct isl_tab_var *var)
608
1.09M
{
609
1.09M
  int i;
610
1.09M
  unsigned off = 2 + tab->M;
611
1.09M
612
1.09M
  if (var->is_row)
613
529k
    return 0;
614
3.82M
  
for (i = tab->n_redundant; 565k
i < tab->n_row3.82M
;
++i3.25M
)
{3.54M
615
3.54M
    if (
!3.54M
isl_int_is_pos3.54M
(tab->mat->row[i][off + var->index]))
616
2.97M
      continue;
617
575k
    
if (575k
isl_tab_var_from_row(tab, i)->is_nonneg575k
)
618
294k
      return 0;
619
575k
  }
620
270k
  return 1;
621
565k
}
622
623
static int row_cmp(struct isl_tab *tab, int r1, int r2, int c, isl_int *t)
624
1.47M
{
625
1.47M
  unsigned off = 2 + tab->M;
626
1.47M
627
1.47M
  if (
tab->M1.47M
)
{0
628
0
    int s;
629
0
    isl_int_mul(*t, tab->mat->row[r1][2], tab->mat->row[r2][off+c]);
630
0
    isl_int_submul(*t, tab->mat->row[r2][2], tab->mat->row[r1][off+c]);
631
0
    s = isl_int_sgn(*t);
632
0
    if (s)
633
0
      return s;
634
0
  }
635
1.47M
  
isl_int_mul1.47M
(*t, tab->mat->row[r1][1], tab->mat->row[r2][off + c]);1.47M
636
1.47M
  isl_int_submul(*t, tab->mat->row[r2][1], tab->mat->row[r1][off + c]);
637
1.47M
  return isl_int_sgn(*t);
638
1.47M
}
639
640
/* Given the index of a column "c", return the index of a row
641
 * that can be used to pivot the column in, with either an increase
642
 * (sgn > 0) or a decrease (sgn < 0) of the corresponding variable.
643
 * If "var" is not NULL, then the row returned will be different from
644
 * the one associated with "var".
645
 *
646
 * Each row in the tableau is of the form
647
 *
648
 *  x_r = a_r0 + \sum_i a_ri x_i
649
 *
650
 * Only rows with x_r >= 0 and with the sign of a_ri opposite to "sgn"
651
 * impose any limit on the increase or decrease in the value of x_c
652
 * and this bound is equal to a_r0 / |a_rc|.  We are therefore looking
653
 * for the row with the smallest (most stringent) such bound.
654
 * Note that the common denominator of each row drops out of the fraction.
655
 * To check if row j has a smaller bound than row r, i.e.,
656
 * a_j0 / |a_jc| < a_r0 / |a_rc| or a_j0 |a_rc| < a_r0 |a_jc|,
657
 * we check if -sign(a_jc) (a_j0 a_rc - a_r0 a_jc) < 0,
658
 * where -sign(a_jc) is equal to "sgn".
659
 */
660
static int pivot_row(struct isl_tab *tab,
661
  struct isl_tab_var *var, int sgn, int c)
662
2.37M
{
663
2.37M
  int j, r, tsgn;
664
2.37M
  isl_int t;
665
2.37M
  unsigned off = 2 + tab->M;
666
2.37M
667
2.37M
  isl_int_init(t);
668
2.37M
  r = -1;
669
25.2M
  for (j = tab->n_redundant; 
j < tab->n_row25.2M
;
++j22.9M
)
{22.9M
670
22.9M
    if (
var && 22.9M
j == var->index19.1M
)
671
2.07M
      continue;
672
20.8M
    
if (20.8M
!isl_tab_var_from_row(tab, j)->is_nonneg20.8M
)
673
6.04M
      continue;
674
14.7M
    
if (14.7M
sgn * 14.7M
isl_int_sgn14.7M
(tab->mat->row[j][off + c]) >= 0)
675
11.7M
      continue;
676
3.01M
    
if (3.01M
r < 03.01M
)
{1.53M
677
1.53M
      r = j;
678
1.53M
      continue;
679
1.53M
    }
680
1.47M
    tsgn = sgn * row_cmp(tab, r, j, c, &t);
681
1.47M
    if (
tsgn < 0 || 1.47M
(tsgn == 0 &&1.14M
682
412k
              tab->row_var[j] < tab->row_var[r]))
683
624k
      r = j;
684
1.47M
  }
685
2.37M
  isl_int_clear(t);
686
2.37M
  return r;
687
2.37M
}
688
689
/* Find a pivot (row and col) that will increase (sgn > 0) or decrease
690
 * (sgn < 0) the value of row variable var.
691
 * If not NULL, then skip_var is a row variable that should be ignored
692
 * while looking for a pivot row.  It is usually equal to var.
693
 *
694
 * As the given row in the tableau is of the form
695
 *
696
 *  x_r = a_r0 + \sum_i a_ri x_i
697
 *
698
 * we need to find a column such that the sign of a_ri is equal to "sgn"
699
 * (such that an increase in x_i will have the desired effect) or a
700
 * column with a variable that may attain negative values.
701
 * If a_ri is positive, then we need to move x_i in the same direction
702
 * to obtain the desired effect.  Otherwise, x_i has to move in the
703
 * opposite direction.
704
 */
705
static void find_pivot(struct isl_tab *tab,
706
  struct isl_tab_var *var, struct isl_tab_var *skip_var,
707
  int sgn, int *row, int *col)
708
2.95M
{
709
2.95M
  int j, r, c;
710
2.95M
  isl_int *tr;
711
2.95M
712
2.95M
  *row = *col = -1;
713
2.95M
714
2.95M
  isl_assert(tab->mat->ctx, var->is_row, return);
715
2.95M
  tr = tab->mat->row[var->index] + 2 + tab->M;
716
2.95M
717
2.95M
  c = -1;
718
20.4M
  for (j = tab->n_dead; 
j < tab->n_col20.4M
;
++j17.5M
)
{17.5M
719
17.5M
    if (isl_int_is_zero(tr[j]))
720
11.8M
      continue;
721
5.64M
    
if (5.64M
isl_int_sgn5.64M
(tr[j]) != sgn &&5.64M
722
3.26M
        var_from_col(tab, j)->is_nonneg)
723
2.37M
      continue;
724
3.26M
    
if (3.26M
c < 0 || 3.26M
tab->col_var[j] < tab->col_var[c]1.11M
)
725
2.42M
      c = j;
726
3.26M
  }
727
2.95M
  if (c < 0)
728
806k
    return;
729
2.95M
730
2.15M
  
sgn *= 2.15M
isl_int_sgn2.15M
(tr[c]);
731
2.15M
  r = pivot_row(tab, skip_var, sgn, c);
732
1.30M
  *row = r < 0 ? 
var->index846k
:
r1.30M
;
733
2.15M
  *col = c;
734
2.15M
}
735
736
/* Return 1 if row "row" represents an obviously redundant inequality.
737
 * This means
738
 *  - it represents an inequality or a variable
739
 *  - that is the sum of a non-negative sample value and a positive
740
 *    combination of zero or more non-negative constraints.
741
 */
742
int isl_tab_row_is_redundant(struct isl_tab *tab, int row)
743
14.1M
{
744
14.1M
  int i;
745
14.1M
  unsigned off = 2 + tab->M;
746
14.1M
747
14.1M
  if (
tab->row_var[row] < 0 && 14.1M
!isl_tab_var_from_row(tab, row)->is_nonneg10.4M
)
748
741k
    return 0;
749
14.1M
750
13.3M
  
if (13.3M
isl_int_is_neg13.3M
(tab->mat->row[row][1]))
751
1.65M
    return 0;
752
11.7M
  
if (11.7M
tab->strict_redundant && 11.7M
isl_int_is_zero43
(tab->mat->row[row][1]))
753
42
    return 0;
754
11.7M
  
if (11.7M
tab->M && 11.7M
isl_int_is_neg31.2k
(tab->mat->row[row][2]))
755
1.36k
    return 0;
756
11.7M
757
41.6M
  
for (i = tab->n_dead; 11.7M
i < tab->n_col41.6M
;
++i29.9M
)
{40.5M
758
40.5M
    if (isl_int_is_zero(tab->mat->row[row][off + i]))
759
24.8M
      continue;
760
15.6M
    
if (15.6M
tab->col_var[i] >= 015.6M
)
761
4.75M
      return 0;
762
10.8M
    
if (10.8M
isl_int_is_neg10.8M
(tab->mat->row[row][off + i]))
763
5.70M
      return 0;
764
5.17M
    
if (5.17M
!var_from_col(tab, i)->is_nonneg5.17M
)
765
124k
      return 0;
766
5.17M
  }
767
1.13M
  return 1;
768
11.7M
}
769
770
static void swap_rows(struct isl_tab *tab, int row1, int row2)
771
1.03M
{
772
1.03M
  int t;
773
1.03M
  enum isl_tab_row_sign s;
774
1.03M
775
1.03M
  t = tab->row_var[row1];
776
1.03M
  tab->row_var[row1] = tab->row_var[row2];
777
1.03M
  tab->row_var[row2] = t;
778
1.03M
  isl_tab_var_from_row(tab, row1)->index = row1;
779
1.03M
  isl_tab_var_from_row(tab, row2)->index = row2;
780
1.03M
  tab->mat = isl_mat_swap_rows(tab->mat, row1, row2);
781
1.03M
782
1.03M
  if (!tab->row_sign)
783
1.03M
    return;
784
7.58k
  s = tab->row_sign[row1];
785
7.58k
  tab->row_sign[row1] = tab->row_sign[row2];
786
7.58k
  tab->row_sign[row2] = s;
787
7.58k
}
788
789
static int push_union(struct isl_tab *tab,
790
  enum isl_tab_undo_type type, union isl_tab_undo_val u) WARN_UNUSED;
791
static int push_union(struct isl_tab *tab,
792
  enum isl_tab_undo_type type, union isl_tab_undo_val u)
793
8.75M
{
794
8.75M
  struct isl_tab_undo *undo;
795
8.75M
796
8.75M
  if (!tab)
797
0
    return -1;
798
8.75M
  
if (8.75M
!tab->need_undo8.75M
)
799
6.08M
    return 0;
800
8.75M
801
2.67M
  
undo = 2.67M
isl_alloc_type2.67M
(tab->mat->ctx, struct isl_tab_undo);
802
2.67M
  if (!undo)
803
0
    return -1;
804
2.67M
  undo->type = type;
805
2.67M
  undo->u = u;
806
2.67M
  undo->next = tab->top;
807
2.67M
  tab->top = undo;
808
2.67M
809
2.67M
  return 0;
810
2.67M
}
811
812
int isl_tab_push_var(struct isl_tab *tab,
813
  enum isl_tab_undo_type type, struct isl_tab_var *var)
814
8.34M
{
815
8.34M
  union isl_tab_undo_val u;
816
8.34M
  if (var->is_row)
817
8.09M
    u.var_index = tab->row_var[var->index];
818
8.34M
  else
819
242k
    u.var_index = tab->col_var[var->index];
820
8.34M
  return push_union(tab, type, u);
821
8.34M
}
822
823
int isl_tab_push(struct isl_tab *tab, enum isl_tab_undo_type type)
824
377k
{
825
377k
  union isl_tab_undo_val u = { 0 };
826
377k
  return push_union(tab, type, u);
827
377k
}
828
829
/* Push a record on the undo stack describing the current basic
830
 * variables, so that the this state can be restored during rollback.
831
 */
832
int isl_tab_push_basis(struct isl_tab *tab)
833
744
{
834
744
  int i;
835
744
  union isl_tab_undo_val u;
836
744
837
744
  u.col_var = isl_alloc_array(tab->mat->ctx, int, tab->n_col);
838
744
  if (
tab->n_col && 744
!u.col_var744
)
839
0
    return -1;
840
8.52k
  
for (i = 0; 744
i < tab->n_col8.52k
;
++i7.78k
)
841
7.78k
    u.col_var[i] = tab->col_var[i];
842
744
  return push_union(tab, isl_tab_undo_saved_basis, u);
843
744
}
844
845
int isl_tab_push_callback(struct isl_tab *tab, struct isl_tab_callback *callback)
846
17.2k
{
847
17.2k
  union isl_tab_undo_val u;
848
17.2k
  u.callback = callback;
849
17.2k
  return push_union(tab, isl_tab_undo_callback, u);
850
17.2k
}
851
852
struct isl_tab *isl_tab_init_samples(struct isl_tab *tab)
853
6.04k
{
854
6.04k
  if (!tab)
855
0
    return NULL;
856
6.04k
857
6.04k
  tab->n_sample = 0;
858
6.04k
  tab->n_outside = 0;
859
6.04k
  tab->samples = isl_mat_alloc(tab->mat->ctx, 1, 1 + tab->n_var);
860
6.04k
  if (!tab->samples)
861
0
    goto error;
862
6.04k
  
tab->sample_index = 6.04k
isl_alloc_array6.04k
(tab->mat->ctx, int, 1);
863
6.04k
  if (!tab->sample_index)
864
0
    goto error;
865
6.04k
  return tab;
866
0
error:
867
0
  isl_tab_free(tab);
868
0
  return NULL;
869
6.04k
}
870
871
int isl_tab_add_sample(struct isl_tab *tab, __isl_take isl_vec *sample)
872
9.46k
{
873
9.46k
  if (
!tab || 9.46k
!sample9.46k
)
874
0
    goto error;
875
9.46k
876
9.46k
  
if (9.46k
tab->n_sample + 1 > tab->samples->n_row9.46k
)
{3.07k
877
3.07k
    int *t = isl_realloc_array(tab->mat->ctx,
878
3.07k
          tab->sample_index, int, tab->n_sample + 1);
879
3.07k
    if (!t)
880
0
      goto error;
881
3.07k
    tab->sample_index = t;
882
3.07k
  }
883
9.46k
884
9.46k
  tab->samples = isl_mat_extend(tab->samples,
885
9.46k
        tab->n_sample + 1, tab->samples->n_col);
886
9.46k
  if (!tab->samples)
887
0
    goto error;
888
9.46k
889
9.46k
  isl_seq_cpy(tab->samples->row[tab->n_sample], sample->el, sample->size);
890
9.46k
  isl_vec_free(sample);
891
9.46k
  tab->sample_index[tab->n_sample] = tab->n_sample;
892
9.46k
  tab->n_sample++;
893
9.46k
894
9.46k
  return 0;
895
0
error:
896
0
  isl_vec_free(sample);
897
0
  return -1;
898
9.46k
}
899
900
struct isl_tab *isl_tab_drop_sample(struct isl_tab *tab, int s)
901
5.55k
{
902
5.55k
  if (
s != tab->n_outside5.55k
)
{3.50k
903
3.50k
    int t = tab->sample_index[tab->n_outside];
904
3.50k
    tab->sample_index[tab->n_outside] = tab->sample_index[s];
905
3.50k
    tab->sample_index[s] = t;
906
3.50k
    isl_mat_swap_rows(tab->samples, tab->n_outside, s);
907
3.50k
  }
908
5.55k
  tab->n_outside++;
909
5.55k
  if (
isl_tab_push(tab, isl_tab_undo_drop_sample) < 05.55k
)
{0
910
0
    isl_tab_free(tab);
911
0
    return NULL;
912
0
  }
913
5.55k
914
5.55k
  return tab;
915
5.55k
}
916
917
/* Record the current number of samples so that we can remove newer
918
 * samples during a rollback.
919
 */
920
int isl_tab_save_samples(struct isl_tab *tab)
921
22.2k
{
922
22.2k
  union isl_tab_undo_val u;
923
22.2k
924
22.2k
  if (!tab)
925
0
    return -1;
926
22.2k
927
22.2k
  u.n = tab->n_sample;
928
22.2k
  return push_union(tab, isl_tab_undo_saved_samples, u);
929
22.2k
}
930
931
/* Mark row with index "row" as being redundant.
932
 * If we may need to undo the operation or if the row represents
933
 * a variable of the original problem, the row is kept,
934
 * but no longer considered when looking for a pivot row.
935
 * Otherwise, the row is simply removed.
936
 *
937
 * The row may be interchanged with some other row.  If it
938
 * is interchanged with a later row, return 1.  Otherwise return 0.
939
 * If the rows are checked in order in the calling function,
940
 * then a return value of 1 means that the row with the given
941
 * row number may now contain a different row that hasn't been checked yet.
942
 */
943
int isl_tab_mark_redundant(struct isl_tab *tab, int row)
944
1.46M
{
945
1.46M
  struct isl_tab_var *var = isl_tab_var_from_row(tab, row);
946
1.46M
  var->is_redundant = 1;
947
1.46M
  isl_assert(tab->mat->ctx, row >= tab->n_redundant, return -1);
948
1.46M
  
if (1.46M
tab->preserve || 1.46M
tab->need_undo999k
||
tab->row_var[row] >= 0911k
)
{1.04M
949
1.04M
    if (
tab->row_var[row] >= 0 && 1.04M
!var->is_nonneg758k
)
{751k
950
751k
      var->is_nonneg = 1;
951
751k
      if (isl_tab_push_var(tab, isl_tab_undo_nonneg, var) < 0)
952
0
        return -1;
953
751k
    }
954
1.04M
    
if (1.04M
row != tab->n_redundant1.04M
)
955
693k
      swap_rows(tab, row, tab->n_redundant);
956
1.04M
    tab->n_redundant++;
957
1.04M
    return isl_tab_push_var(tab, isl_tab_undo_redundant, var);
958
417k
  } else {
959
417k
    if (row != tab->n_row - 1)
960
235k
      swap_rows(tab, row, tab->n_row - 1);
961
417k
    isl_tab_var_from_row(tab, tab->n_row - 1)->index = -1;
962
417k
    tab->n_row--;
963
417k
    return 1;
964
417k
  }
965
1.46M
}
966
967
/* Mark "tab" as a rational tableau.
968
 * If it wasn't marked as a rational tableau already and if we may
969
 * need to undo changes, then arrange for the marking to be undone
970
 * during the undo.
971
 */
972
int isl_tab_mark_rational(struct isl_tab *tab)
973
9.75k
{
974
9.75k
  if (!tab)
975
0
    return -1;
976
9.75k
  
if (9.75k
!tab->rational && 9.75k
tab->need_undo9.72k
)
977
9.72k
    
if (9.72k
isl_tab_push(tab, isl_tab_undo_rational) < 09.72k
)
978
0
      return -1;
979
9.75k
  tab->rational = 1;
980
9.75k
  return 0;
981
9.75k
}
982
983
isl_stat isl_tab_mark_empty(struct isl_tab *tab)
984
67.1k
{
985
67.1k
  if (!tab)
986
0
    return isl_stat_error;
987
67.1k
  
if (67.1k
!tab->empty && 67.1k
tab->need_undo66.4k
)
988
56.4k
    
if (56.4k
isl_tab_push(tab, isl_tab_undo_empty) < 056.4k
)
989
0
      return isl_stat_error;
990
67.1k
  tab->empty = 1;
991
67.1k
  return isl_stat_ok;
992
67.1k
}
993
994
int isl_tab_freeze_constraint(struct isl_tab *tab, int con)
995
390k
{
996
390k
  struct isl_tab_var *var;
997
390k
998
390k
  if (!tab)
999
0
    return -1;
1000
390k
1001
390k
  var = &tab->con[con];
1002
390k
  if (var->frozen)
1003
0
    return 0;
1004
390k
  
if (390k
var->index < 0390k
)
1005
23.4k
    return 0;
1006
366k
  var->frozen = 1;
1007
366k
1008
366k
  if (tab->need_undo)
1009
339k
    return isl_tab_push_var(tab, isl_tab_undo_freeze, var);
1010
366k
1011
26.9k
  return 0;
1012
366k
}
1013
1014
/* Update the rows signs after a pivot of "row" and "col", with "row_sgn"
1015
 * the original sign of the pivot element.
1016
 * We only keep track of row signs during PILP solving and in this case
1017
 * we only pivot a row with negative sign (meaning the value is always
1018
 * non-positive) using a positive pivot element.
1019
 *
1020
 * For each row j, the new value of the parametric constant is equal to
1021
 *
1022
 *  a_j0 - a_jc a_r0/a_rc
1023
 *
1024
 * where a_j0 is the original parametric constant, a_rc is the pivot element,
1025
 * a_r0 is the parametric constant of the pivot row and a_jc is the
1026
 * pivot column entry of the row j.
1027
 * Since a_r0 is non-positive and a_rc is positive, the sign of row j
1028
 * remains the same if a_jc has the same sign as the row j or if
1029
 * a_jc is zero.  In all other cases, we reset the sign to "unknown".
1030
 */
1031
static void update_row_sign(struct isl_tab *tab, int row, int col, int row_sgn)
1032
2.36M
{
1033
2.36M
  int i;
1034
2.36M
  struct isl_mat *mat = tab->mat;
1035
2.36M
  unsigned off = 2 + tab->M;
1036
2.36M
1037
2.36M
  if (!tab->row_sign)
1038
2.34M
    return;
1039
2.36M
1040
20.8k
  
if (20.8k
tab->row_sign[row] == 020.8k
)
1041
16.1k
    return;
1042
4.71k
  
isl_assert4.71k
(mat->ctx, row_sgn > 0, return);4.71k
1043
4.71k
  
isl_assert4.71k
(mat->ctx, tab->row_sign[row] == isl_tab_row_neg, return);4.71k
1044
4.71k
  tab->row_sign[row] = isl_tab_row_pos;
1045
48.0k
  for (i = 0; 
i < tab->n_row48.0k
;
++i43.3k
)
{43.3k
1046
43.3k
    int s;
1047
43.3k
    if (i == row)
1048
4.71k
      continue;
1049
38.6k
    
s = 38.6k
isl_int_sgn38.6k
(mat->row[i][off + col]);
1050
38.6k
    if (!s)
1051
21.5k
      continue;
1052
17.0k
    
if (17.0k
!tab->row_sign[i]17.0k
)
1053
5.44k
      continue;
1054
11.6k
    
if (11.6k
s < 0 && 11.6k
tab->row_sign[i] == isl_tab_row_neg6.17k
)
1055
0
      continue;
1056
11.6k
    
if (11.6k
s > 0 && 11.6k
tab->row_sign[i] == isl_tab_row_pos5.46k
)
1057
5.43k
      continue;
1058
6.20k
    tab->row_sign[i] = isl_tab_row_unknown;
1059
6.20k
  }
1060
4.71k
}
1061
1062
/* Given a row number "row" and a column number "col", pivot the tableau
1063
 * such that the associated variables are interchanged.
1064
 * The given row in the tableau expresses
1065
 *
1066
 *  x_r = a_r0 + \sum_i a_ri x_i
1067
 *
1068
 * or
1069
 *
1070
 *  x_c = 1/a_rc x_r - a_r0/a_rc + sum_{i \ne r} -a_ri/a_rc
1071
 *
1072
 * Substituting this equality into the other rows
1073
 *
1074
 *  x_j = a_j0 + \sum_i a_ji x_i
1075
 *
1076
 * with a_jc \ne 0, we obtain
1077
 *
1078
 *  x_j = a_jc/a_rc x_r + a_j0 - a_jc a_r0/a_rc + sum a_ji - a_jc a_ri/a_rc 
1079
 *
1080
 * The tableau
1081
 *
1082
 *  n_rc/d_r    n_ri/d_r
1083
 *  n_jc/d_j    n_ji/d_j
1084
 *
1085
 * where i is any other column and j is any other row,
1086
 * is therefore transformed into
1087
 *
1088
 * s(n_rc)d_r/|n_rc|    -s(n_rc)n_ri/|n_rc|
1089
 * s(n_rc)d_r n_jc/(|n_rc| d_j) (n_ji |n_rc| - s(n_rc)n_jc n_ri)/(|n_rc| d_j)
1090
 *
1091
 * The transformation is performed along the following steps
1092
 *
1093
 *  d_r/n_rc    n_ri/n_rc
1094
 *  n_jc/d_j    n_ji/d_j
1095
 *
1096
 *  s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1097
 *  n_jc/d_j    n_ji/d_j
1098
 *
1099
 *  s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1100
 *  n_jc/(|n_rc| d_j) n_ji/(|n_rc| d_j)
1101
 *
1102
 *  s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1103
 *  n_jc/(|n_rc| d_j) (n_ji |n_rc|)/(|n_rc| d_j)
1104
 *
1105
 *  s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1106
 *  n_jc/(|n_rc| d_j) (n_ji |n_rc| - s(n_rc)n_jc n_ri)/(|n_rc| d_j)
1107
 *
1108
 * s(n_rc)d_r/|n_rc|    -s(n_rc)n_ri/|n_rc|
1109
 * s(n_rc)d_r n_jc/(|n_rc| d_j) (n_ji |n_rc| - s(n_rc)n_jc n_ri)/(|n_rc| d_j)
1110
 *
1111
 */
1112
int isl_tab_pivot(struct isl_tab *tab, int row, int col)
1113
2.36M
{
1114
2.36M
  int i, j;
1115
2.36M
  int sgn;
1116
2.36M
  int t;
1117
2.36M
  isl_ctx *ctx;
1118
2.36M
  struct isl_mat *mat = tab->mat;
1119
2.36M
  struct isl_tab_var *var;
1120
2.36M
  unsigned off = 2 + tab->M;
1121
2.36M
1122
2.36M
  ctx = isl_tab_get_ctx(tab);
1123
2.36M
  if (isl_ctx_next_operation(ctx) < 0)
1124
0
    return -1;
1125
2.36M
1126
2.36M
  
isl_int_swap2.36M
(mat->row[row][0], mat->row[row][off + col]);2.36M
1127
2.36M
  sgn = isl_int_sgn(mat->row[row][0]);
1128
2.36M
  if (
sgn < 02.36M
)
{1.33M
1129
1.33M
    isl_int_neg(mat->row[row][0], mat->row[row][0]);
1130
1.33M
    isl_int_neg(mat->row[row][off + col], mat->row[row][off + col]);
1131
1.33M
  } else
1132
8.01M
    
for (j = 0; 1.02M
j < off - 1 + tab->n_col8.01M
;
++j6.98M
)
{6.98M
1133
6.98M
      if (j == off - 1 + col)
1134
1.02M
        continue;
1135
5.95M
      
isl_int_neg5.95M
(mat->row[row][1 + j], mat->row[row][1 + j]);5.95M
1136
5.95M
    }
1137
2.36M
  if (
!2.36M
isl_int_is_one2.36M
(mat->row[row][0]))
1138
566k
    isl_seq_normalize(mat->ctx, mat->row[row], off + tab->n_col);
1139
27.1M
  for (i = 0; 
i < tab->n_row27.1M
;
++i24.7M
)
{24.7M
1140
24.7M
    if (i == row)
1141
2.36M
      continue;
1142
22.4M
    
if (22.4M
isl_int_is_zero22.4M
(mat->row[i][off + col]))
1143
13.9M
      continue;
1144
8.44M
    
isl_int_mul8.44M
(mat->row[i][0], mat->row[i][0], mat->row[row][0]);8.44M
1145
100M
    for (j = 0; 
j < off - 1 + tab->n_col100M
;
++j91.5M
)
{91.5M
1146
91.5M
      if (j == off - 1 + col)
1147
8.44M
        continue;
1148
83.1M
      
isl_int_mul83.1M
(mat->row[i][1 + j],83.1M
1149
83.1M
            mat->row[i][1 + j], mat->row[row][0]);
1150
83.1M
      isl_int_addmul(mat->row[i][1 + j],
1151
83.1M
            mat->row[i][off + col], mat->row[row][1 + j]);
1152
83.1M
    }
1153
8.44M
    isl_int_mul(mat->row[i][off + col],
1154
8.44M
          mat->row[i][off + col], mat->row[row][off + col]);
1155
8.44M
    if (
!8.44M
isl_int_is_one8.44M
(mat->row[i][0]))
1156
5.42M
      isl_seq_normalize(mat->ctx, mat->row[i], off + tab->n_col);
1157
8.44M
  }
1158
2.36M
  t = tab->row_var[row];
1159
2.36M
  tab->row_var[row] = tab->col_var[col];
1160
2.36M
  tab->col_var[col] = t;
1161
2.36M
  var = isl_tab_var_from_row(tab, row);
1162
2.36M
  var->is_row = 1;
1163
2.36M
  var->index = row;
1164
2.36M
  var = var_from_col(tab, col);
1165
2.36M
  var->is_row = 0;
1166
2.36M
  var->index = col;
1167
2.36M
  update_row_sign(tab, row, col, sgn);
1168
2.36M
  if (tab->in_undo)
1169
91.3k
    return 0;
1170
21.6M
  
for (i = tab->n_redundant; 2.27M
i < tab->n_row21.6M
;
++i19.3M
)
{19.3M
1171
19.3M
    if (isl_int_is_zero(mat->row[i][off + col]))
1172
10.0M
      continue;
1173
9.29M
    
if (9.29M
!isl_tab_var_from_row(tab, i)->frozen &&9.29M
1174
8.99M
        
isl_tab_row_is_redundant(tab, i)8.99M
)
{1.03M
1175
1.03M
      int redo = isl_tab_mark_redundant(tab, i);
1176
1.03M
      if (redo < 0)
1177
0
        return -1;
1178
1.03M
      
if (1.03M
redo1.03M
)
1179
78.5k
        --i;
1180
1.03M
    }
1181
9.29M
  }
1182
2.27M
  return 0;
1183
2.27M
}
1184
1185
/* If "var" represents a column variable, then pivot is up (sgn > 0)
1186
 * or down (sgn < 0) to a row.  The variable is assumed not to be
1187
 * unbounded in the specified direction.
1188
 * If sgn = 0, then the variable is unbounded in both directions,
1189
 * and we pivot with any row we can find.
1190
 */
1191
static int to_row(struct isl_tab *tab, struct isl_tab_var *var, int sign) WARN_UNUSED;
1192
static int to_row(struct isl_tab *tab, struct isl_tab_var *var, int sign)
1193
1.03M
{
1194
1.03M
  int r;
1195
1.03M
  unsigned off = 2 + tab->M;
1196
1.03M
1197
1.03M
  if (var->is_row)
1198
851k
    return 0;
1199
1.03M
1200
184k
  
if (184k
sign == 0184k
)
{18.9k
1201
67.9k
    for (r = tab->n_redundant; 
r < tab->n_row67.9k
;
++r49.0k
)
1202
67.9k
      
if (67.9k
!67.9k
isl_int_is_zero67.9k
(tab->mat->row[r][off+var->index]))
1203
18.9k
        break;
1204
18.9k
    isl_assert(tab->mat->ctx, r < tab->n_row, return -1);
1205
165k
  } else {
1206
165k
    r = pivot_row(tab, NULL, sign, var->index);
1207
165k
    isl_assert(tab->mat->ctx, r >= 0, return -1);
1208
165k
  }
1209
184k
1210
184k
  return isl_tab_pivot(tab, r, var->index);
1211
184k
}
1212
1213
/* Check whether all variables that are marked as non-negative
1214
 * also have a non-negative sample value.  This function is not
1215
 * called from the current code but is useful during debugging.
1216
 */
1217
static void check_table(struct isl_tab *tab) __attribute__ ((unused));
1218
static void check_table(struct isl_tab *tab)
1219
0
{
1220
0
  int i;
1221
0
1222
0
  if (tab->empty)
1223
0
    return;
1224
0
  for (i = tab->n_redundant; i < tab->n_row; ++i) {
1225
0
    struct isl_tab_var *var;
1226
0
    var = isl_tab_var_from_row(tab, i);
1227
0
    if (!var->is_nonneg)
1228
0
      continue;
1229
0
    if (tab->M) {
1230
0
      isl_assert(tab->mat->ctx,
1231
0
        !isl_int_is_neg(tab->mat->row[i][2]), abort());
1232
0
      if (isl_int_is_pos(tab->mat->row[i][2]))
1233
0
        continue;
1234
0
    }
1235
0
    isl_assert(tab->mat->ctx, !isl_int_is_neg(tab->mat->row[i][1]),
1236
0
        abort());
1237
0
  }
1238
0
}
1239
1240
/* Return the sign of the maximal value of "var".
1241
 * If the sign is not negative, then on return from this function,
1242
 * the sample value will also be non-negative.
1243
 *
1244
 * If "var" is manifestly unbounded wrt positive values, we are done.
1245
 * Otherwise, we pivot the variable up to a row if needed
1246
 * Then we continue pivoting down until either
1247
 *  - no more down pivots can be performed
1248
 *  - the sample value is positive
1249
 *  - the variable is pivoted into a manifestly unbounded column
1250
 */
1251
static int sign_of_max(struct isl_tab *tab, struct isl_tab_var *var)
1252
825k
{
1253
825k
  int row, col;
1254
825k
1255
825k
  if (max_is_manifestly_unbounded(tab, var))
1256
87.2k
    return 1;
1257
738k
  
if (738k
to_row(tab, var, 1) < 0738k
)
1258
0
    return -2;
1259
1.20M
  
while (738k
!1.20M
isl_int_is_pos1.20M
(tab->mat->row[var->index][1]))
{979k
1260
979k
    find_pivot(tab, var, var, 1, &row, &col);
1261
979k
    if (row == -1)
1262
316k
      
return 316k
isl_int_sgn316k
(tab->mat->row[var->index][1]);
1263
663k
    
if (663k
isl_tab_pivot(tab, row, col) < 0663k
)
1264
0
      return -2;
1265
663k
    
if (663k
!var->is_row663k
) /* manifestly unbounded */
1266
194k
      return 1;
1267
663k
  }
1268
228k
  return 1;
1269
738k
}
1270
1271
int isl_tab_sign_of_max(struct isl_tab *tab, int con)
1272
130
{
1273
130
  struct isl_tab_var *var;
1274
130
1275
130
  if (!tab)
1276
0
    return -2;
1277
130
1278
130
  var = &tab->con[con];
1279
130
  isl_assert(tab->mat->ctx, !var->is_redundant, return -2);
1280
130
  
isl_assert130
(tab->mat->ctx, !var->is_zero, return -2);130
1281
130
1282
130
  return sign_of_max(tab, var);
1283
130
}
1284
1285
static int row_is_neg(struct isl_tab *tab, int row)
1286
3.42M
{
1287
3.42M
  if (!tab->M)
1288
3.42M
    
return 3.42M
isl_int_is_neg3.42M
(tab->mat->row[row][1]);
1289
0
  
if (0
isl_int_is_pos0
(tab->mat->row[row][2]))
1290
0
    return 0;
1291
0
  
if (0
isl_int_is_neg0
(tab->mat->row[row][2]))
1292
0
    return 1;
1293
0
  
return 0
isl_int_is_neg0
(tab->mat->row[row][1]);
1294
0
}
1295
1296
static int row_sgn(struct isl_tab *tab, int row)
1297
2.89M
{
1298
2.89M
  if (!tab->M)
1299
2.89M
    
return 2.89M
isl_int_sgn2.89M
(tab->mat->row[row][1]);
1300
0
  
if (0
!0
isl_int_is_zero0
(tab->mat->row[row][2]))
1301
0
    
return 0
isl_int_sgn0
(tab->mat->row[row][2]);
1302
0
  else
1303
0
    
return 0
isl_int_sgn0
(tab->mat->row[row][1]);
1304
0
}
1305
1306
/* Perform pivots until the row variable "var" has a non-negative
1307
 * sample value or until no more upward pivots can be performed.
1308
 * Return the sign of the sample value after the pivots have been
1309
 * performed.
1310
 */
1311
static int restore_row(struct isl_tab *tab, struct isl_tab_var *var)
1312
3.21M
{
1313
3.21M
  int row, col;
1314
3.21M
1315
3.42M
  while (
row_is_neg(tab, var->index)3.42M
)
{596k
1316
596k
    find_pivot(tab, var, var, 1, &row, &col);
1317
596k
    if (row == -1)
1318
64.2k
      break;
1319
532k
    
if (532k
isl_tab_pivot(tab, row, col) < 0532k
)
1320
0
      return -2;
1321
532k
    
if (532k
!var->is_row532k
) /* manifestly unbounded */
1322
324k
      return 1;
1323
532k
  }
1324
2.89M
  return row_sgn(tab, var->index);
1325
3.21M
}
1326
1327
/* Perform pivots until we are sure that the row variable "var"
1328
 * can attain non-negative values.  After return from this
1329
 * function, "var" is still a row variable, but its sample
1330
 * value may not be non-negative, even if the function returns 1.
1331
 */
1332
static int at_least_zero(struct isl_tab *tab, struct isl_tab_var *var)
1333
138k
{
1334
138k
  int row, col;
1335
138k
1336
155k
  while (
isl_int_is_neg155k
(tab->mat->row[var->index][1]))
{148k
1337
148k
    find_pivot(tab, var, var, 1, &row, &col);
1338
148k
    if (row == -1)
1339
65.0k
      break;
1340
82.9k
    
if (82.9k
row == var->index82.9k
) /* manifestly unbounded */
1341
65.8k
      return 1;
1342
17.1k
    
if (17.1k
isl_tab_pivot(tab, row, col) < 017.1k
)
1343
0
      return -1;
1344
17.1k
  }
1345
72.8k
  
return !72.8k
isl_int_is_neg72.8k
(tab->mat->row[var->index][1]);
1346
138k
}
1347
1348
/* Return a negative value if "var" can attain negative values.
1349
 * Return a non-negative value otherwise.
1350
 *
1351
 * If "var" is manifestly unbounded wrt negative values, we are done.
1352
 * Otherwise, if var is in a column, we can pivot it down to a row.
1353
 * Then we continue pivoting down until either
1354
 *  - the pivot would result in a manifestly unbounded column
1355
 *    => we don't perform the pivot, but simply return -1
1356
 *  - no more down pivots can be performed
1357
 *  - the sample value is negative
1358
 * If the sample value becomes negative and the variable is supposed
1359
 * to be nonnegative, then we undo the last pivot.
1360
 * However, if the last pivot has made the pivoting variable
1361
 * obviously redundant, then it may have moved to another row.
1362
 * In that case we look for upward pivots until we reach a non-negative
1363
 * value again.
1364
 */
1365
static int sign_of_min(struct isl_tab *tab, struct isl_tab_var *var)
1366
70.3k
{
1367
70.3k
  int row, col;
1368
70.3k
  struct isl_tab_var *pivot_var = NULL;
1369
70.3k
1370
70.3k
  if (min_is_manifestly_unbounded(tab, var))
1371
0
    return -1;
1372
70.3k
  
if (70.3k
!var->is_row70.3k
)
{1.84k
1373
1.84k
    col = var->index;
1374
1.84k
    row = pivot_row(tab, NULL, -1, col);
1375
1.84k
    pivot_var = var_from_col(tab, col);
1376
1.84k
    if (isl_tab_pivot(tab, row, col) < 0)
1377
0
      return -2;
1378
1.84k
    
if (1.84k
var->is_redundant1.84k
)
1379
317
      return 0;
1380
1.52k
    
if (1.52k
isl_int_is_neg1.52k
(tab->mat->row[var->index][1]))
{693
1381
693
      if (
var->is_nonneg693
)
{693
1382
693
        if (!pivot_var->is_redundant &&
1383
693
            
pivot_var->index == row693
)
{680
1384
680
          if (isl_tab_pivot(tab, row, col) < 0)
1385
0
            return -2;
1386
680
        } else
1387
13
          
if (13
restore_row(tab, var) < -113
)
1388
0
            return -2;
1389
693
      }
1390
693
      return -1;
1391
693
    }
1392
1.52k
  }
1393
69.3k
  
if (69.3k
var->is_redundant69.3k
)
1394
0
    return 0;
1395
106k
  
while (69.3k
!106k
isl_int_is_neg106k
(tab->mat->row[var->index][1]))
{93.3k
1396
93.3k
    find_pivot(tab, var, var, -1, &row, &col);
1397
93.3k
    if (row == var->index)
1398
16.0k
      return -1;
1399
77.3k
    
if (77.3k
row == -177.3k
)
1400
38.7k
      
return 38.7k
isl_int_sgn38.7k
(tab->mat->row[var->index][1]);
1401
38.5k
    pivot_var = var_from_col(tab, col);
1402
38.5k
    if (isl_tab_pivot(tab, row, col) < 0)
1403
0
      return -2;
1404
38.5k
    
if (38.5k
var->is_redundant38.5k
)
1405
1.14k
      return 0;
1406
38.5k
  }
1407
13.3k
  
if (13.3k
pivot_var && 13.3k
var->is_nonneg13.3k
)
{968
1408
968
    /* pivot back to non-negative value */
1409
968
    if (
!pivot_var->is_redundant && 968
pivot_var->index == row968
)
{955
1410
955
      if (isl_tab_pivot(tab, row, col) < 0)
1411
0
        return -2;
1412
955
    } else
1413
13
      
if (13
restore_row(tab, var) < -113
)
1414
0
        return -2;
1415
968
  }
1416
13.3k
  return -1;
1417
13.3k
}
1418
1419
static int row_at_most_neg_one(struct isl_tab *tab, int row)
1420
187k
{
1421
187k
  if (
tab->M187k
)
{0
1422
0
    if (isl_int_is_pos(tab->mat->row[row][2]))
1423
0
      return 0;
1424
0
    
if (0
isl_int_is_neg0
(tab->mat->row[row][2]))
1425
0
      return 1;
1426
0
  }
1427
187k
  
return 187k
isl_int_is_neg187k
(tab->mat->row[row][1]) &&
1428
95.8k
         isl_int_abs_ge(tab->mat->row[row][1],
1429
187k
            tab->mat->row[row][0]);
1430
187k
}
1431
1432
/* Return 1 if "var" can attain values <= -1.
1433
 * Return 0 otherwise.
1434
 *
1435
 * If the variable "var" is supposed to be non-negative (is_nonneg is set),
1436
 * then the sample value of "var" is assumed to be non-negative when the
1437
 * the function is called.  If 1 is returned then the constraint
1438
 * is not redundant and the sample value is made non-negative again before
1439
 * the function returns.
1440
 */
1441
int isl_tab_min_at_most_neg_one(struct isl_tab *tab, struct isl_tab_var *var)
1442
520k
{
1443
520k
  int row, col;
1444
520k
  struct isl_tab_var *pivot_var;
1445
520k
1446
520k
  if (min_is_manifestly_unbounded(tab, var))
1447
367
    return 1;
1448
520k
  
if (520k
!var->is_row520k
)
{61.0k
1449
61.0k
    col = var->index;
1450
61.0k
    row = pivot_row(tab, NULL, -1, col);
1451
61.0k
    pivot_var = var_from_col(tab, col);
1452
61.0k
    if (isl_tab_pivot(tab, row, col) < 0)
1453
0
      return -1;
1454
61.0k
    
if (61.0k
var->is_redundant61.0k
)
1455
9.35k
      return 0;
1456
51.6k
    
if (51.6k
row_at_most_neg_one(tab, var->index)51.6k
)
{36.6k
1457
36.6k
      if (
var->is_nonneg36.6k
)
{36.6k
1458
36.6k
        if (!pivot_var->is_redundant &&
1459
36.6k
            
pivot_var->index == row36.6k
)
{34.2k
1460
34.2k
          if (isl_tab_pivot(tab, row, col) < 0)
1461
0
            return -1;
1462
34.2k
        } else
1463
2.45k
          
if (2.45k
restore_row(tab, var) < -12.45k
)
1464
0
            return -1;
1465
36.6k
      }
1466
36.6k
      return 1;
1467
36.6k
    }
1468
51.6k
  }
1469
474k
  
if (474k
var->is_redundant474k
)
1470
8.53k
    return 0;
1471
556k
  
do 465k
{556k
1472
556k
    find_pivot(tab, var, var, -1, &row, &col);
1473
556k
    if (
row == var->index556k
)
{240k
1474
240k
      if (
var->is_nonneg && 240k
restore_row(tab, var) < -1207k
)
1475
0
        return -1;
1476
240k
      return 1;
1477
240k
    }
1478
316k
    
if (316k
row == -1316k
)
1479
122k
      return 0;
1480
193k
    pivot_var = var_from_col(tab, col);
1481
193k
    if (isl_tab_pivot(tab, row, col) < 0)
1482
0
      return -1;
1483
193k
    
if (193k
var->is_redundant193k
)
1484
57.4k
      return 0;
1485
136k
  } while (!row_at_most_neg_one(tab, var->index));
1486
45.3k
  
if (45.3k
var->is_nonneg45.3k
)
{36.2k
1487
36.2k
    /* pivot back to non-negative value */
1488
36.2k
    if (
!pivot_var->is_redundant && 36.2k
pivot_var->index == row36.2k
)
1489
34.8k
      
if (34.8k
isl_tab_pivot(tab, row, col) < 034.8k
)
1490
0
        return -1;
1491
36.2k
    
if (36.2k
restore_row(tab, var) < -136.2k
)
1492
0
      return -1;
1493
36.2k
  }
1494
45.3k
  return 1;
1495
45.3k
}
1496
1497
/* Return 1 if "var" can attain values >= 1.
1498
 * Return 0 otherwise.
1499
 */
1500
static int at_least_one(struct isl_tab *tab, struct isl_tab_var *var)
1501
484k
{
1502
484k
  int row, col;
1503
484k
  isl_int *r;
1504
484k
1505
484k
  if (max_is_manifestly_unbounded(tab, var))
1506
280k
    return 1;
1507
204k
  
if (204k
to_row(tab, var, 1) < 0204k
)
1508
0
    return -1;
1509
204k
  r = tab->mat->row[var->index];
1510
206k
  while (
isl_int_lt206k
(r[1], r[0]))
{3.12k
1511
3.12k
    find_pivot(tab, var, var, 1, &row, &col);
1512
3.12k
    if (row == -1)
1513
496
      
return 496
isl_int_ge496
(r[1], r[0]);
1514
2.63k
    
if (2.63k
row == var->index2.63k
) /* manifestly unbounded */
1515
127
      return 1;
1516
2.50k
    
if (2.50k
isl_tab_pivot(tab, row, col) < 02.50k
)
1517
0
      return -1;
1518
2.50k
  }
1519
203k
  return 1;
1520
204k
}
1521
1522
static void swap_cols(struct isl_tab *tab, int col1, int col2)
1523
380k
{
1524
380k
  int t;
1525
380k
  unsigned off = 2 + tab->M;
1526
380k
  t = tab->col_var[col1];
1527
380k
  tab->col_var[col1] = tab->col_var[col2];
1528
380k
  tab->col_var[col2] = t;
1529
380k
  var_from_col(tab, col1)->index = col1;
1530
380k
  var_from_col(tab, col2)->index = col2;
1531
380k
  tab->mat = isl_mat_swap_cols(tab->mat, off + col1, off + col2);
1532
380k
}
1533
1534
/* Mark column with index "col" as representing a zero variable.
1535
 * If we may need to undo the operation the column is kept,
1536
 * but no longer considered.
1537
 * Otherwise, the column is simply removed.
1538
 *
1539
 * The column may be interchanged with some other column.  If it
1540
 * is interchanged with a later column, return 1.  Otherwise return 0.
1541
 * If the columns are checked in order in the calling function,
1542
 * then a return value of 1 means that the column with the given
1543
 * column number may now contain a different column that
1544
 * hasn't been checked yet.
1545
 */
1546
int isl_tab_kill_col(struct isl_tab *tab, int col)
1547
562k
{
1548
562k
  var_from_col(tab, col)->is_zero = 1;
1549
562k
  if (
tab->need_undo562k
)
{66.5k
1550
66.5k
    if (isl_tab_push_var(tab, isl_tab_undo_zero,
1551
66.5k
              var_from_col(tab, col)) < 0)
1552
0
      return -1;
1553
66.5k
    
if (66.5k
col != tab->n_dead66.5k
)
1554
31.1k
      swap_cols(tab, col, tab->n_dead);
1555
66.5k
    tab->n_dead++;
1556
66.5k
    return 0;
1557
495k
  } else {
1558
495k
    if (col != tab->n_col - 1)
1559
347k
      swap_cols(tab, col, tab->n_col - 1);
1560
495k
    var_from_col(tab, tab->n_col - 1)->index = -1;
1561
495k
    tab->n_col--;
1562
495k
    return 1;
1563
495k
  }
1564
562k
}
1565
1566
static int row_is_manifestly_non_integral(struct isl_tab *tab, int row)
1567
1.45M
{
1568
1.45M
  unsigned off = 2 + tab->M;
1569
1.45M
1570
1.45M
  if (
tab->M && 1.45M
!0
isl_int_eq0
(tab->mat->row[row][2],
1571
1.45M
          tab->mat->row[row][0]))
1572
0
    return 0;
1573
1.45M
  
if (1.45M
isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,1.45M
1574
1.45M
            tab->n_col - tab->n_dead) != -1)
1575
166k
    return 0;
1576
1.45M
1577
1.29M
  
return !1.29M
isl_int_is_divisible_by1.29M
(tab->mat->row[row][1],
1578
1.45M
          tab->mat->row[row][0]);
1579
1.45M
}
1580
1581
/* For integer tableaus, check if any of the coordinates are stuck
1582
 * at a non-integral value.
1583
 */
1584
static int tab_is_manifestly_empty(struct isl_tab *tab)
1585
315k
{
1586
315k
  int i;
1587
315k
1588
315k
  if (tab->empty)
1589
0
    return 1;
1590
315k
  
if (315k
tab->rational315k
)
1591
11.7k
    return 0;
1592
315k
1593
3.11M
  
for (i = 0; 304k
i < tab->n_var3.11M
;
++i2.81M
)
{2.81M
1594
2.81M
    if (!tab->var[i].is_row)
1595
1.35M
      continue;
1596
1.45M
    
if (1.45M
row_is_manifestly_non_integral(tab, tab->var[i].index)1.45M
)
1597
40
      return 1;
1598
1.45M
  }
1599
304k
1600
304k
  return 0;
1601
304k
}
1602
1603
/* Row variable "var" is non-negative and cannot attain any values
1604
 * larger than zero.  This means that the coefficients of the unrestricted
1605
 * column variables are zero and that the coefficients of the non-negative
1606
 * column variables are zero or negative.
1607
 * Each of the non-negative variables with a negative coefficient can
1608
 * then also be written as the negative sum of non-negative variables
1609
 * and must therefore also be zero.
1610
 *
1611
 * If "temp_var" is set, then "var" is a temporary variable that
1612
 * will be removed after this function returns and for which
1613
 * no information is recorded on the undo stack.
1614
 * Do not add any undo records involving this variable in this case
1615
 * since the variable will have been removed before any future undo
1616
 * operations.  Also avoid marking the variable as redundant,
1617
 * since that either adds an undo record or needlessly removes the row
1618
 * (the caller will take care of removing the row).
1619
 */
1620
static isl_stat close_row(struct isl_tab *tab, struct isl_tab_var *var,
1621
  int temp_var) WARN_UNUSED;
1622
static isl_stat close_row(struct isl_tab *tab, struct isl_tab_var *var,
1623
  int temp_var)
1624
315k
{
1625
315k
  int j;
1626
315k
  struct isl_mat *mat = tab->mat;
1627
315k
  unsigned off = 2 + tab->M;
1628
315k
1629
315k
  if (!var->is_nonneg)
1630
0
    isl_die(isl_tab_get_ctx(tab), isl_error_internal,
1631
315k
      "expecting non-negative variable",
1632
315k
      return isl_stat_error);
1633
315k
  var->is_zero = 1;
1634
315k
  if (
!temp_var && 315k
tab->need_undo304k
)
1635
480
    
if (480
isl_tab_push_var(tab, isl_tab_undo_zero, var) < 0480
)
1636
0
      return isl_stat_error;
1637
2.07M
  
for (j = tab->n_dead; 315k
j < tab->n_col2.07M
;
++j1.75M
)
{1.75M
1638
1.75M
    int recheck;
1639
1.75M
    if (isl_int_is_zero(mat->row[var->index][off + j]))
1640
1.46M
      continue;
1641
292k
    
if (292k
isl_int_is_pos292k
(mat->row[var->index][off + j]))
1642
0
      isl_die(isl_tab_get_ctx(tab), isl_error_internal,
1643
292k
        "row cannot have positive coefficients",
1644
292k
        return isl_stat_error);
1645
292k
    recheck = isl_tab_kill_col(tab, j);
1646
292k
    if (recheck < 0)
1647
0
      return isl_stat_error;
1648
292k
    
if (292k
recheck292k
)
1649
280k
      --j;
1650
292k
  }
1651
315k
  
if (315k
!temp_var && 315k
isl_tab_mark_redundant(tab, var->index) < 0304k
)
1652
0
    return isl_stat_error;
1653
315k
  
if (315k
tab_is_manifestly_empty(tab) && 315k
isl_tab_mark_empty(tab) < 040
)
1654
0
    return isl_stat_error;
1655
315k
  return isl_stat_ok;
1656
315k
}
1657
1658
/* Add a constraint to the tableau and allocate a row for it.
1659
 * Return the index into the constraint array "con".
1660
 *
1661
 * This function assumes that at least one more row and at least
1662
 * one more element in the constraint array are available in the tableau.
1663
 */
1664
int isl_tab_allocate_con(struct isl_tab *tab)
1665
3.47M
{
1666
3.47M
  int r;
1667
3.47M
1668
3.47M
  isl_assert(tab->mat->ctx, tab->n_row < tab->mat->n_row, return -1);
1669
3.47M
  
isl_assert3.47M
(tab->mat->ctx, tab->n_con < tab->max_con, return -1);3.47M
1670
3.47M
1671
3.47M
  r = tab->n_con;
1672
3.47M
  tab->con[r].index = tab->n_row;
1673
3.47M
  tab->con[r].is_row = 1;
1674
3.47M
  tab->con[r].is_nonneg = 0;
1675
3.47M
  tab->con[r].is_zero = 0;
1676
3.47M
  tab->con[r].is_redundant = 0;
1677
3.47M
  tab->con[r].frozen = 0;
1678
3.47M
  tab->con[r].negated = 0;
1679
3.47M
  tab->row_var[tab->n_row] = ~r;
1680
3.47M
1681
3.47M
  tab->n_row++;
1682
3.47M
  tab->n_con++;
1683
3.47M
  if (isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->con[r]) < 0)
1684
0
    return -1;
1685
3.47M
1686
3.47M
  return r;
1687
3.47M
}
1688
1689
/* Move the entries in tab->var up one position, starting at "first",
1690
 * creating room for an extra entry at position "first".
1691
 * Since some of the entries of tab->row_var and tab->col_var contain
1692
 * indices into this array, they have to be updated accordingly.
1693
 */
1694
static int var_insert_entry(struct isl_tab *tab, int first)
1695
8.58k
{
1696
8.58k
  int i;
1697
8.58k
1698
8.58k
  if (tab->n_var >= tab->max_var)
1699
0
    isl_die(isl_tab_get_ctx(tab), isl_error_internal,
1700
8.58k
      "not enough room for new variable", return -1);
1701
8.58k
  
if (8.58k
first > tab->n_var8.58k
)
1702
0
    isl_die(isl_tab_get_ctx(tab), isl_error_internal,
1703
8.58k
      "invalid initial position", return -1);
1704
8.58k
1705
9.22k
  
for (i = tab->n_var - 1; 8.58k
i >= first9.22k
;
--i635
)
{635
1706
635
    tab->var[i + 1] = tab->var[i];
1707
635
    if (tab->var[i + 1].is_row)
1708
429
      tab->row_var[tab->var[i + 1].index]++;
1709
635
    else
1710
206
      tab->col_var[tab->var[i + 1].index]++;
1711
635
  }
1712
8.58k
1713
8.58k
  tab->n_var++;
1714
8.58k
1715
8.58k
  return 0;
1716
8.58k
}
1717
1718
/* Drop the entry at position "first" in tab->var, moving all
1719
 * subsequent entries down.
1720
 * Since some of the entries of tab->row_var and tab->col_var contain
1721
 * indices into this array, they have to be updated accordingly.
1722
 */
1723
static int var_drop_entry(struct isl_tab *tab, int first)
1724
5.79k
{
1725
5.79k
  int i;
1726
5.79k
1727
5.79k
  if (first >= tab->n_var)
1728
0
    isl_die(isl_tab_get_ctx(tab), isl_error_internal,
1729
5.79k
      "invalid initial position", return -1);
1730
5.79k
1731
5.79k
  tab->n_var--;
1732
5.79k
1733
6.21k
  for (i = first; 
i < tab->n_var6.21k
;
++i415
)
{415
1734
415
    tab->var[i] = tab->var[i + 1];
1735
415
    if (tab->var[i + 1].is_row)
1736
409
      tab->row_var[tab->var[i].index]--;
1737
415
    else
1738
6
      tab->col_var[tab->var[i].index]--;
1739
415
  }
1740
5.79k
1741
5.79k
  return 0;
1742
5.79k
}
1743
1744
/* Add a variable to the tableau at position "r" and allocate a column for it.
1745
 * Return the index into the variable array "var", i.e., "r",
1746
 * or -1 on error.
1747
 */
1748
int isl_tab_insert_var(struct isl_tab *tab, int r)
1749
8.58k
{
1750
8.58k
  int i;
1751
8.58k
  unsigned off = 2 + tab->M;
1752
8.58k
1753
8.58k
  isl_assert(tab->mat->ctx, tab->n_col < tab->mat->n_col, return -1);
1754
8.58k
1755
8.58k
  
if (8.58k
var_insert_entry(tab, r) < 08.58k
)
1756
0
    return -1;
1757
8.58k
1758
8.58k
  tab->var[r].index = tab->n_col;
1759
8.58k
  tab->var[r].is_row = 0;
1760
8.58k
  tab->var[r].is_nonneg = 0;
1761
8.58k
  tab->var[r].is_zero = 0;
1762
8.58k
  tab->var[r].is_redundant = 0;
1763
8.58k
  tab->var[r].frozen = 0;
1764
8.58k
  tab->var[r].negated = 0;
1765
8.58k
  tab->col_var[tab->n_col] = r;
1766
8.58k
1767
76.3k
  for (i = 0; 
i < tab->n_row76.3k
;
++i67.8k
)
1768
67.8k
    isl_int_set_si(tab->mat->row[i][off + tab->n_col], 0);
1769
8.58k
1770
8.58k
  tab->n_col++;
1771
8.58k
  if (isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->var[r]) < 0)
1772
0
    return -1;
1773
8.58k
1774
8.58k
  return r;
1775
8.58k
}
1776
1777
/* Add a variable to the tableau and allocate a column for it.
1778
 * Return the index into the variable array "var".
1779
 */
1780
int isl_tab_allocate_var(struct isl_tab *tab)
1781
0
{
1782
0
  if (!tab)
1783
0
    return -1;
1784
0
1785
0
  return isl_tab_insert_var(tab, tab->n_var);
1786
0
}
1787
1788
/* Add a row to the tableau.  The row is given as an affine combination
1789
 * of the original variables and needs to be expressed in terms of the
1790
 * column variables.
1791
 *
1792
 * This function assumes that at least one more row and at least
1793
 * one more element in the constraint array are available in the tableau.
1794
 *
1795
 * We add each term in turn.
1796
 * If r = n/d_r is the current sum and we need to add k x, then
1797
 *  if x is a column variable, we increase the numerator of
1798
 *    this column by k d_r
1799
 *  if x = f/d_x is a row variable, then the new representation of r is
1800
 *
1801
 *     n    k f   d_x/g n + d_r/g k f   m/d_r n + m/d_g k f
1802
 *    --- + --- = ------------------- = -------------------
1803
 *    d_r   d_r        d_r d_x/g                m
1804
 *
1805
 *  with g the gcd of d_r and d_x and m the lcm of d_r and d_x.
1806
 *
1807
 * If tab->M is set, then, internally, each variable x is represented
1808
 * as x' - M.  We then also need no subtract k d_r from the coefficient of M.
1809
 */
1810
int isl_tab_add_row(struct isl_tab *tab, isl_int *line)
1811
3.47M
{
1812
3.47M
  int i;
1813
3.47M
  int r;
1814
3.47M
  isl_int *row;
1815
3.47M
  isl_int a, b;
1816
3.47M
  unsigned off = 2 + tab->M;
1817
3.47M
1818
3.47M
  r = isl_tab_allocate_con(tab);
1819
3.47M
  if (r < 0)
1820
0
    return -1;
1821
3.47M
1822
3.47M
  
isl_int_init3.47M
(a);3.47M
1823
3.47M
  isl_int_init(b);
1824
3.47M
  row = tab->mat->row[tab->con[r].index];
1825
3.47M
  isl_int_set_si(row[0], 1);
1826
3.47M
  isl_int_set(row[1], line[0]);
1827
3.47M
  isl_seq_clr(row + 2, tab->M + tab->n_col);
1828
27.1M
  for (i = 0; 
i < tab->n_var27.1M
;
++i23.6M
)
{23.6M
1829
23.6M
    if (tab->var[i].is_zero)
1830
0
      continue;
1831
23.6M
    
if (23.6M
tab->var[i].is_row23.6M
)
{5.94M
1832
5.94M
      isl_int_lcm(a,
1833
5.94M
        row[0], tab->mat->row[tab->var[i].index][0]);
1834
5.94M
      isl_int_swap(a, row[0]);
1835
5.94M
      isl_int_divexact(a, row[0], a);
1836
5.94M
      isl_int_divexact(b,
1837
5.94M
        row[0], tab->mat->row[tab->var[i].index][0]);
1838
5.94M
      isl_int_mul(b, b, line[1 + i]);
1839
5.94M
      isl_seq_combine(row + 1, a, row + 1,
1840
5.94M
          b, tab->mat->row[tab->var[i].index] + 1,
1841
5.94M
          1 + tab->M + tab->n_col);
1842
5.94M
    } else
1843
17.6M
      isl_int_addmul(row[off + tab->var[i].index],
1844
23.6M
              line[1 + i], row[0]);
1845
23.6M
    if (
tab->M && 23.6M
i >= tab->n_param243k
&&
i < tab->n_var - tab->n_div105k
)
1846
101k
      isl_int_submul(row[2], line[1 + i], row[0]);
1847
23.6M
  }
1848
3.47M
  isl_seq_normalize(tab->mat->ctx, row, off + tab->n_col);
1849
3.47M
  isl_int_clear(a);
1850
3.47M
  isl_int_clear(b);
1851
3.47M
1852
3.47M
  if (tab->row_sign)
1853
29.1k
    tab->row_sign[tab->con[r].index] = isl_tab_row_unknown;
1854
3.47M
1855
3.47M
  return r;
1856
3.47M
}
1857
1858
static isl_stat drop_row(struct isl_tab *tab, int row)
1859
847k
{
1860
847k
  isl_assert(tab->mat->ctx, ~tab->row_var[row] == tab->n_con - 1,
1861
847k
    return isl_stat_error);
1862
847k
  
if (847k
row != tab->n_row - 1847k
)
1863
108k
    swap_rows(tab, row, tab->n_row - 1);
1864
847k
  tab->n_row--;
1865
847k
  tab->n_con--;
1866
847k
  return isl_stat_ok;
1867
847k
}
1868
1869
/* Drop the variable in column "col" along with the column.
1870
 * The column is removed first because it may need to be moved
1871
 * into the last position and this process requires
1872
 * the contents of the col_var array in a state
1873
 * before the removal of the variable.
1874
 */
1875
static isl_stat drop_col(struct isl_tab *tab, int col)
1876
5.79k
{
1877
5.79k
  int var;
1878
5.79k
1879
5.79k
  var = tab->col_var[col];
1880
5.79k
  if (col != tab->n_col - 1)
1881
2.12k
    swap_cols(tab, col, tab->n_col - 1);
1882
5.79k
  tab->n_col--;
1883
5.79k
  if (var_drop_entry(tab, var) < 0)
1884
0
    return isl_stat_error;
1885
5.79k
  return isl_stat_ok;
1886
5.79k
}
1887
1888
/* Add inequality "ineq" and check if it conflicts with the
1889
 * previously added constraints or if it is obviously redundant.
1890
 *
1891
 * This function assumes that at least one more row and at least
1892
 * one more element in the constraint array are available in the tableau.
1893
 */
1894
isl_stat isl_tab_add_ineq(struct isl_tab *tab, isl_int *ineq)
1895
2.61M
{
1896
2.61M
  int r;
1897
2.61M
  int sgn;
1898
2.61M
  isl_int cst;
1899
2.61M
1900
2.61M
  if (!tab)
1901
0
    return isl_stat_error;
1902
2.61M
  
if (2.61M
tab->bmap2.61M
)
{300k
1903
300k
    struct isl_basic_map *bmap = tab->bmap;
1904
300k
1905
300k
    isl_assert(tab->mat->ctx, tab->n_eq == bmap->n_eq,
1906
300k
      return isl_stat_error);
1907
300k
    
isl_assert300k
(tab->mat->ctx,300k
1908
300k
          tab->n_con == bmap->n_eq + bmap->n_ineq,
1909
300k
          return isl_stat_error);
1910
300k
    tab->bmap = isl_basic_map_add_ineq(tab->bmap, ineq);
1911
300k
    if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1912
0
      return isl_stat_error;
1913
300k
    
if (300k
!tab->bmap300k
)
1914
0
      return isl_stat_error;
1915
300k
  }
1916
2.61M
  
if (2.61M
tab->cone2.61M
)
{3.71k
1917
3.71k
    isl_int_init(cst);
1918
3.71k
    isl_int_set_si(cst, 0);
1919
3.71k
    isl_int_swap(ineq[0], cst);
1920
3.71k
  }
1921
2.61M
  r = isl_tab_add_row(tab, ineq);
1922
2.61M
  if (
tab->cone2.61M
)
{3.71k
1923
3.71k
    isl_int_swap(ineq[0], cst);
1924
3.71k
    isl_int_clear(cst);
1925
3.71k
  }
1926
2.61M
  if (r < 0)
1927
0
    return isl_stat_error;
1928
2.61M
  tab->con[r].is_nonneg = 1;
1929
2.61M
  if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1930
0
    return isl_stat_error;
1931
2.61M
  
if (2.61M
isl_tab_row_is_redundant(tab, tab->con[r].index)2.61M
)
{101k
1932
101k
    if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1933
0
      return isl_stat_error;
1934
101k
    return isl_stat_ok;
1935
101k
  }
1936
2.61M
1937
2.51M
  sgn = restore_row(tab, &tab->con[r]);
1938
2.51M
  if (sgn < -1)
1939
0
    return isl_stat_error;
1940
2.51M
  
if (2.51M
sgn < 02.51M
)
1941
64.2k
    return isl_tab_mark_empty(tab);
1942
2.45M
  
if (2.45M
tab->con[r].is_row && 2.45M
isl_tab_row_is_redundant(tab, tab->con[r].index)2.12M
)
1943
0
    
if (0
isl_tab_mark_redundant(tab, tab->con[r].index) < 00
)
1944
0
      return isl_stat_error;
1945
2.45M
  return isl_stat_ok;
1946
2.45M
}
1947
1948
/* Pivot a non-negative variable down until it reaches the value zero
1949
 * and then pivot the variable into a column position.
1950
 */
1951
static int to_col(struct isl_tab *tab, struct isl_tab_var *var) WARN_UNUSED;
1952
static int to_col(struct isl_tab *tab, struct isl_tab_var *var)
1953
55.4k
{
1954
55.4k
  int i;
1955
55.4k
  int row, col;
1956
55.4k
  unsigned off = 2 + tab->M;
1957
55.4k
1958
55.4k
  if (!var->is_row)
1959
115
    return 0;
1960
55.4k
1961
80.9k
  
while (55.3k
isl_int_is_pos80.9k
(tab->mat->row[var->index][1]))
{76.2k
1962
76.2k
    find_pivot(tab, var, NULL, -1, &row, &col);
1963
76.2k
    isl_assert(tab->mat->ctx, row != -1, return -1);
1964
76.2k
    
if (76.2k
isl_tab_pivot(tab, row, col) < 076.2k
)
1965
0
      return -1;
1966
76.2k
    
if (76.2k
!var->is_row76.2k
)
1967
50.5k
      return 0;
1968
76.2k
  }
1969
55.3k
1970
7.89k
  
for (i = tab->n_dead; 4.75k
i < tab->n_col7.89k
;
++i3.13k
)
1971
7.89k
    
if (7.89k
!7.89k
isl_int_is_zero7.89k
(tab->mat->row[var->index][off + i]))
1972
4.75k
      break;
1973
4.75k
1974
4.75k
  isl_assert(tab->mat->ctx, i < tab->n_col, return -1);
1975
4.75k
  
if (4.75k
isl_tab_pivot(tab, var->index, i) < 04.75k
)
1976
0
    return -1;
1977
4.75k
1978
4.75k
  return 0;
1979
4.75k
}
1980
1981
/* We assume Gaussian elimination has been performed on the equalities.
1982
 * The equalities can therefore never conflict.
1983
 * Adding the equalities is currently only really useful for a later call
1984
 * to isl_tab_ineq_type.
1985
 *
1986
 * This function assumes that at least one more row and at least
1987
 * one more element in the constraint array are available in the tableau.
1988
 */
1989
static struct isl_tab *add_eq(struct isl_tab *tab, isl_int *eq)
1990
197k
{
1991
197k
  int i;
1992
197k
  int r;
1993
197k
1994
197k
  if (!tab)
1995
0
    return NULL;
1996
197k
  r = isl_tab_add_row(tab, eq);
1997
197k
  if (r < 0)
1998
0
    goto error;
1999
197k
2000
197k
  r = tab->con[r].index;
2001
197k
  i = isl_seq_first_non_zero(tab->mat->row[r] + 2 + tab->M + tab->n_dead,
2002
197k
          tab->n_col - tab->n_dead);
2003
197k
  isl_assert(tab->mat->ctx, i >= 0, goto error);
2004
197k
  i += tab->n_dead;
2005
197k
  if (isl_tab_pivot(tab, r, i) < 0)
2006
0
    goto error;
2007
197k
  
if (197k
isl_tab_kill_col(tab, i) < 0197k
)
2008
0
    goto error;
2009
197k
  tab->n_eq++;
2010
197k
2011
197k
  return tab;
2012
0
error:
2013
0
  isl_tab_free(tab);
2014
0
  return NULL;
2015
197k
}
2016
2017
/* Does the sample value of row "row" of "tab" involve the big parameter,
2018
 * if any?
2019
 */
2020
static int row_is_big(struct isl_tab *tab, int row)
2021
79.3k
{
2022
0
  return tab->M && 
!0
isl_int_is_zero0
(tab->mat->row[row][2]);
2023
79.3k
}
2024
2025
static int row_is_manifestly_zero(struct isl_tab *tab, int row)
2026
66.4k
{
2027
66.4k
  unsigned off = 2 + tab->M;
2028
66.4k
2029
66.4k
  if (
!66.4k
isl_int_is_zero66.4k
(tab->mat->row[row][1]))
2030
50.9k
    return 0;
2031
15.4k
  
if (15.4k
row_is_big(tab, row)15.4k
)
2032
0
    return 0;
2033
15.4k
  return isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
2034
15.4k
          tab->n_col - tab->n_dead) == -1;
2035
15.4k
}
2036
2037
/* Add an equality that is known to be valid for the given tableau.
2038
 *
2039
 * This function assumes that at least one more row and at least
2040
 * one more element in the constraint array are available in the tableau.
2041
 */
2042
int isl_tab_add_valid_eq(struct isl_tab *tab, isl_int *eq)
2043
53.9k
{
2044
53.9k
  struct isl_tab_var *var;
2045
53.9k
  int r;
2046
53.9k
2047
53.9k
  if (!tab)
2048
0
    return -1;
2049
53.9k
  r = isl_tab_add_row(tab, eq);
2050
53.9k
  if (r < 0)
2051
0
    return -1;
2052
53.9k
2053
53.9k
  var = &tab->con[r];
2054
53.9k
  r = var->index;
2055
53.9k
  if (
row_is_manifestly_zero(tab, r)53.9k
)
{1.70k
2056
1.70k
    var->is_zero = 1;
2057
1.70k
    if (isl_tab_mark_redundant(tab, r) < 0)
2058
0
      return -1;
2059
1.70k
    return 0;
2060
1.70k
  }
2061
53.9k
2062
52.2k
  
if (52.2k
isl_int_is_neg52.2k
(tab->mat->row[r][1]))
{19.7k
2063
19.7k
    isl_seq_neg(tab->mat->row[r] + 1, tab->mat->row[r] + 1,
2064
19.7k
          1 + tab->n_col);
2065
19.7k
    var->negated = 1;
2066
19.7k
  }
2067
52.2k
  var->is_nonneg = 1;
2068
52.2k
  if (to_col(tab, var) < 0)
2069
0
    return -1;
2070
52.2k
  var->is_nonneg = 0;
2071
52.2k
  if (isl_tab_kill_col(tab, var->index) < 0)
2072
0
    return -1;
2073
52.2k
2074
52.2k
  return 0;
2075
52.2k
}
2076
2077
/* Add a zero row to "tab" and return the corresponding index
2078
 * in the constraint array.
2079
 *
2080
 * This function assumes that at least one more row and at least
2081
 * one more element in the constraint array are available in the tableau.
2082
 */
2083
static int add_zero_row(struct isl_tab *tab)
2084
2.47k
{
2085
2.47k
  int r;
2086
2.47k
  isl_int *row;
2087
2.47k
2088
2.47k
  r = isl_tab_allocate_con(tab);
2089
2.47k
  if (r < 0)
2090
0
    return -1;
2091
2.47k
2092
2.47k
  row = tab->mat->row[tab->con[r].index];
2093
2.47k
  isl_seq_clr(row + 1, 1 + tab->M + tab->n_col);
2094
2.47k
  isl_int_set_si(row[0], 1);
2095
2.47k
2096
2.47k
  return r;
2097
2.47k
}
2098
2099
/* Add equality "eq" and check if it conflicts with the
2100
 * previously added constraints or if it is obviously redundant.
2101
 *
2102
 * This function assumes that at least one more row and at least
2103
 * one more element in the constraint array are available in the tableau.
2104
 * If tab->bmap is set, then two rows are needed instead of one.
2105
 */
2106
int isl_tab_add_eq(struct isl_tab *tab, isl_int *eq)
2107
12.5k
{
2108
12.5k
  struct isl_tab_undo *snap = NULL;
2109
12.5k
  struct isl_tab_var *var;
2110
12.5k
  int r;
2111
12.5k
  int row;
2112
12.5k
  int sgn;
2113
12.5k
  isl_int cst;
2114
12.5k
2115
12.5k
  if (!tab)
2116
0
    return -1;
2117
12.5k
  
isl_assert12.5k
(tab->mat->ctx, !tab->M, return -1);12.5k
2118
12.5k
2119
12.5k
  
if (12.5k
tab->need_undo12.5k
)
2120
11.9k
    snap = isl_tab_snap(tab);
2121
12.5k
2122
12.5k
  if (
tab->cone12.5k
)
{1.01k
2123
1.01k
    isl_int_init(cst);
2124
1.01k
    isl_int_set_si(cst, 0);
2125
1.01k
    isl_int_swap(eq[0], cst);
2126
1.01k
  }
2127
12.5k
  r = isl_tab_add_row(tab, eq);
2128
12.5k
  if (
tab->cone12.5k
)
{1.01k
2129
1.01k
    isl_int_swap(eq[0], cst);
2130
1.01k
    isl_int_clear(cst);
2131
1.01k
  }
2132
12.5k
  if (r < 0)
2133
0
    return -1;
2134
12.5k
2135
12.5k
  var = &tab->con[r];
2136
12.5k
  row = var->index;
2137
12.5k
  if (
row_is_manifestly_zero(tab, row)12.5k
)
{9.26k
2138
9.26k
    if (snap)
2139
9.21k
      return isl_tab_rollback(tab, snap);
2140
58
    return drop_row(tab, row);
2141
9.26k
  }
2142
12.5k
2143
3.23k
  
if (3.23k
tab->bmap3.23k
)
{2.47k
2144
2.47k
    tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
2145
2.47k
    if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
2146
0
      return -1;
2147
2.47k
    isl_seq_neg(eq, eq, 1 + tab->n_var);
2148
2.47k
    tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
2149
2.47k
    isl_seq_neg(eq, eq, 1 + tab->n_var);
2150
2.47k
    if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
2151
0
      return -1;
2152
2.47k
    
if (2.47k
!tab->bmap2.47k
)
2153
0
      return -1;
2154
2.47k
    
if (2.47k
add_zero_row(tab) < 02.47k
)
2155
0
      return -1;
2156
2.47k
  }
2157
3.23k
2158
3.23k
  
sgn = 3.23k
isl_int_sgn3.23k
(tab->mat->row[row][1]);
2159
3.23k
2160
3.23k
  if (
sgn > 03.23k
)
{323
2161
323
    isl_seq_neg(tab->mat->row[row] + 1, tab->mat->row[row] + 1,
2162
323
          1 + tab->n_col);
2163
323
    var->negated = 1;
2164
323
    sgn = -1;
2165
323
  }
2166
3.23k
2167
3.23k
  if (
sgn < 03.23k
)
{2.08k
2168
2.08k
    sgn = sign_of_max(tab, var);
2169
2.08k
    if (sgn < -1)
2170
0
      return -1;
2171
2.08k
    
if (2.08k
sgn < 02.08k
)
{0
2172
0
      if (isl_tab_mark_empty(tab) < 0)
2173
0
        return -1;
2174
0
      return 0;
2175
0
    }
2176
2.08k
  }
2177
3.23k
2178
3.23k
  var->is_nonneg = 1;
2179
3.23k
  if (to_col(tab, var) < 0)
2180
0
    return -1;
2181
3.23k
  var->is_nonneg = 0;
2182
3.23k
  if (isl_tab_kill_col(tab, var->index) < 0)
2183
0
    return -1;
2184
3.23k
2185
3.23k
  return 0;
2186
3.23k
}
2187
2188
/* Construct and return an inequality that expresses an upper bound
2189
 * on the given div.
2190
 * In particular, if the div is given by
2191
 *
2192
 *  d = floor(e/m)
2193
 *
2194
 * then the inequality expresses
2195
 *
2196
 *  m d <= e
2197
 */
2198
static struct isl_vec *ineq_for_div(struct isl_basic_map *bmap, unsigned div)
2199
4.90k
{
2200
4.90k
  unsigned total;
2201
4.90k
  unsigned div_pos;
2202
4.90k
  struct isl_vec *ineq;
2203
4.90k
2204
4.90k
  if (!bmap)
2205
0
    return NULL;
2206
4.90k
2207
4.90k
  total = isl_basic_map_total_dim(bmap);
2208
4.90k
  div_pos = 1 + total - bmap->n_div + div;
2209
4.90k
2210
4.90k
  ineq = isl_vec_alloc(bmap->ctx, 1 + total);
2211
4.90k
  if (!ineq)
2212
0
    return NULL;
2213
4.90k
2214
4.90k
  isl_seq_cpy(ineq->el, bmap->div[div] + 1, 1 + total);
2215
4.90k
  isl_int_neg(ineq->el[div_pos], bmap->div[div][0]);
2216
4.90k
  return ineq;
2217
4.90k
}
2218
2219
/* For a div d = floor(f/m), add the constraints
2220
 *
2221
 *    f - m d >= 0
2222
 *    -(f-(m-1)) + m d >= 0
2223
 *
2224
 * Note that the second constraint is the negation of
2225
 *
2226
 *    f - m d >= m
2227
 *
2228
 * If add_ineq is not NULL, then this function is used
2229
 * instead of isl_tab_add_ineq to effectively add the inequalities.
2230
 *
2231
 * This function assumes that at least two more rows and at least
2232
 * two more elements in the constraint array are available in the tableau.
2233
 */
2234
static isl_stat add_div_constraints(struct isl_tab *tab, unsigned div,
2235
  isl_stat (*add_ineq)(void *user, isl_int *), void *user)
2236
4.90k
{
2237
4.90k
  unsigned total;
2238
4.90k
  unsigned div_pos;
2239
4.90k
  struct isl_vec *ineq;
2240
4.90k
2241
4.90k
  total = isl_basic_map_total_dim(tab->bmap);
2242
4.90k
  div_pos = 1 + total - tab->bmap->n_div + div;
2243
4.90k
2244
4.90k
  ineq = ineq_for_div(tab->bmap, div);
2245
4.90k
  if (!ineq)
2246
0
    goto error;
2247
4.90k
2248
4.90k
  
if (4.90k
add_ineq4.90k
)
{604
2249
604
    if (add_ineq(user, ineq->el) < 0)
2250
0
      goto error;
2251
4.29k
  } else {
2252
4.29k
    if (isl_tab_add_ineq(tab, ineq->el) < 0)
2253
0
      goto error;
2254
4.29k
  }
2255
4.90k
2256
4.90k
  isl_seq_neg(ineq->el, tab->bmap->div[div] + 1, 1 + total);
2257
4.90k
  isl_int_set(ineq->el[div_pos], tab->bmap->div[div][0]);
2258
4.90k
  isl_int_add(ineq->el[0], ineq->el[0], ineq->el[div_pos]);
2259
4.90k
  isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
2260
4.90k
2261
4.90k
  if (
add_ineq4.90k
)
{604
2262
604
    if (add_ineq(user, ineq->el) < 0)
2263
0
      goto error;
2264
4.29k
  } else {
2265
4.29k
    if (isl_tab_add_ineq(tab, ineq->el) < 0)
2266
0
      goto error;
2267
4.29k
  }
2268
4.90k
2269
4.90k
  isl_vec_free(ineq);
2270
4.90k
2271
4.90k
  return 0;
2272
0
error:
2273
0
  isl_vec_free(ineq);
2274
0
  return -1;
2275
4.90k
}
2276
2277
/* Check whether the div described by "div" is obviously non-negative.
2278
 * If we are using a big parameter, then we will encode the div
2279
 * as div' = M + div, which is always non-negative.
2280
 * Otherwise, we check whether div is a non-negative affine combination
2281
 * of non-negative variables.
2282
 */
2283
static int div_is_nonneg(struct isl_tab *tab, __isl_keep isl_vec *div)
2284
4.90k
{
2285
4.90k
  int i;
2286
4.90k
2287
4.90k
  if (tab->M)
2288
0
    return 1;
2289
4.90k
2290
4.90k
  
if (4.90k
isl_int_is_neg4.90k
(div->el[1]))
2291
2.61k
    return 0;
2292
4.90k
2293
5.63k
  
for (i = 0; 2.28k
i < tab->n_var5.63k
;
++i3.35k
)
{5.38k
2294
5.38k
    if (isl_int_is_neg(div->el[2 + i]))
2295
388
      return 0;
2296
4.99k
    
if (4.99k
isl_int_is_zero4.99k
(div->el[2 + i]))
2297
3.05k
      continue;
2298
1.94k
    
if (1.94k
!tab->var[i].is_nonneg1.94k
)
2299
1.64k
      return 0;
2300
1.94k
  }
2301
2.28k
2302
254
  return 1;
2303
2.28k
}
2304
2305
/* Insert an extra div, prescribed by "div", to the tableau and
2306
 * the associated bmap (which is assumed to be non-NULL).
2307
 * The extra integer division is inserted at (tableau) position "pos".
2308
 * Return "pos" or -1 if an error occurred.
2309
 *
2310
 * If add_ineq is not NULL, then this function is used instead
2311
 * of isl_tab_add_ineq to add the div constraints.
2312
 * This complication is needed because the code in isl_tab_pip
2313
 * wants to perform some extra processing when an inequality
2314
 * is added to the tableau.
2315
 */
2316
int isl_tab_insert_div(struct isl_tab *tab, int pos, __isl_keep isl_vec *div,
2317
  isl_stat (*add_ineq)(void *user, isl_int *), void *user)
2318
4.90k
{
2319
4.90k
  int r;
2320
4.90k
  int nonneg;
2321
4.90k
  int n_div, o_div;
2322
4.90k
2323
4.90k
  if (
!tab || 4.90k
!div4.90k
)
2324
0
    return -1;
2325
4.90k
2326
4.90k
  
if (4.90k
div->size != 1 + 1 + tab->n_var4.90k
)
2327
0
    isl_die(isl_tab_get_ctx(tab), isl_error_invalid,
2328
4.90k
      "unexpected size", return -1);
2329
4.90k
2330
4.90k
  
isl_assert4.90k
(tab->mat->ctx, tab->bmap, return -1);4.90k
2331
4.90k
  n_div = isl_basic_map_dim(tab->bmap, isl_dim_div);
2332
4.90k
  o_div = tab->n_var - n_div;
2333
4.90k
  if (
pos < o_div || 4.90k
pos > tab->n_var4.90k
)
2334
0
    isl_die(isl_tab_get_ctx(tab), isl_error_invalid,
2335
4.90k
      "invalid position", return -1);
2336
4.90k
2337
4.90k
  nonneg = div_is_nonneg(tab, div);
2338
4.90k
2339
4.90k
  if (isl_tab_extend_cons(tab, 3) < 0)
2340
0
    return -1;
2341
4.90k
  
if (4.90k
isl_tab_extend_vars(tab, 1) < 04.90k
)
2342
0
    return -1;
2343
4.90k
  r = isl_tab_insert_var(tab, pos);
2344
4.90k
  if (r < 0)
2345
0
    return -1;
2346
4.90k
2347
4.90k
  
if (4.90k
nonneg4.90k
)
2348
254
    tab->var[r].is_nonneg = 1;
2349
4.90k
2350
4.90k
  tab->bmap = isl_basic_map_insert_div(tab->bmap, pos - o_div, div);
2351
4.90k
  if (!tab->bmap)
2352
0
    return -1;
2353
4.90k
  
if (4.90k
isl_tab_push_var(tab, isl_tab_undo_bmap_div, &tab->var[r]) < 04.90k
)
2354
0
    return -1;
2355
4.90k
2356
4.90k
  
if (4.90k
add_div_constraints(tab, pos - o_div, add_ineq, user) < 04.90k
)
2357
0
    return -1;
2358
4.90k
2359
4.90k
  return r;
2360
4.90k
}
2361
2362
/* Add an extra div, prescribed by "div", to the tableau and
2363
 * the associated bmap (which is assumed to be non-NULL).
2364
 */
2365
int isl_tab_add_div(struct isl_tab *tab, __isl_keep isl_vec *div)
2366
4.29k
{
2367
4.29k
  if (!tab)
2368
0
    return -1;
2369
4.29k
  return isl_tab_insert_div(tab, tab->n_var, div, NULL, NULL);
2370
4.29k
}
2371
2372
/* If "track" is set, then we want to keep track of all constraints in tab
2373
 * in its bmap field.  This field is initialized from a copy of "bmap",
2374
 * so we need to make sure that all constraints in "bmap" also appear
2375
 * in the constructed tab.
2376
 */
2377
__isl_give struct isl_tab *isl_tab_from_basic_map(
2378
  __isl_keep isl_basic_map *bmap, int track)
2379
538k
{
2380
538k
  int i;
2381
538k
  struct isl_tab *tab;
2382
538k
2383
538k
  if (!bmap)
2384
0
    return NULL;
2385
538k
  tab = isl_tab_alloc(bmap->ctx,
2386
538k
          isl_basic_map_total_dim(bmap) + bmap->n_ineq + 1,
2387
538k
          isl_basic_map_total_dim(bmap), 0);
2388
538k
  if (!tab)
2389
0
    return NULL;
2390
538k
  tab->preserve = track;
2391
538k
  tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
2392
538k
  if (
ISL_F_ISSET538k
(bmap, ISL_BASIC_MAP_EMPTY))
{23
2393
23
    if (isl_tab_mark_empty(tab) < 0)
2394
0
      goto error;
2395
23
    goto done;
2396
23
  }
2397
734k
  
for (i = 0; 538k
i < bmap->n_eq734k
;
++i195k
)
{195k
2398
195k
    tab = add_eq(tab, bmap->eq[i]);
2399
195k
    if (!tab)
2400
0
      return tab;
2401
195k
  }
2402
2.82M
  
for (i = 0; 538k
i < bmap->n_ineq2.82M
;
++i2.28M
)
{2.29M
2403
2.29M
    if (isl_tab_add_ineq(tab, bmap->ineq[i]) < 0)
2404
0
      goto error;
2405
2.29M
    
if (2.29M
tab->empty2.29M
)
2406
5.46k
      goto done;
2407
2.29M
  }
2408
538k
done:
2409
538k
  if (
track && 538k
isl_tab_track_bmap(tab, isl_basic_map_copy(bmap)) < 0128k
)
2410
0
    goto error;
2411
538k
  return tab;
2412
0
error:
2413
0
  isl_tab_free(tab);
2414
0
  return NULL;
2415
538k
}
2416
2417
__isl_give struct isl_tab *isl_tab_from_basic_set(
2418
  __isl_keep isl_basic_set *bset, int track)
2419
184k
{
2420
184k
  return isl_tab_from_basic_map(bset, track);
2421
184k
}
2422
2423
/* Construct a tableau corresponding to the recession cone of "bset".
2424
 */
2425
struct isl_tab *isl_tab_from_recession_cone(__isl_keep isl_basic_set *bset,
2426
  int parametric)
2427
3.40k
{
2428
3.40k
  isl_int cst;
2429
3.40k
  int i;
2430
3.40k
  struct isl_tab *tab;
2431
3.40k
  unsigned offset = 0;
2432
3.40k
2433
3.40k
  if (!bset)
2434
0
    return NULL;
2435
3.40k
  
if (3.40k
parametric3.40k
)
2436
2.54k
    offset = isl_basic_set_dim(bset, isl_dim_param);
2437
3.40k
  tab = isl_tab_alloc(bset->ctx, bset->n_eq + bset->n_ineq,
2438
3.40k
        isl_basic_set_total_dim(bset) - offset, 0);
2439
3.40k
  if (!tab)
2440
0
    return NULL;
2441
3.40k
  
tab->rational = 3.40k
ISL_F_ISSET3.40k
(bset, ISL_BASIC_SET_RATIONAL);
2442
3.40k
  tab->cone = 1;
2443
3.40k
2444
3.40k
  isl_int_init(cst);
2445
3.40k
  isl_int_set_si(cst, 0);
2446
5.67k
  for (i = 0; 
i < bset->n_eq5.67k
;
++i2.27k
)
{2.27k
2447
2.27k
    isl_int_swap(bset->eq[i][offset], cst);
2448
2.27k
    if (
offset > 02.27k
)
{574
2449
574
      if (isl_tab_add_eq(tab, bset->eq[i] + offset) < 0)
2450
0
        goto error;
2451
574
    } else
2452
1.69k
      tab = add_eq(tab, bset->eq[i]);
2453
2.27k
    
isl_int_swap2.27k
(bset->eq[i][offset], cst);2.27k
2454
2.27k
    if (!tab)
2455
0
      goto done;
2456
2.27k
  }
2457
14.4k
  
for (i = 0; 3.40k
i < bset->n_ineq14.4k
;
++i11.0k
)
{11.0k
2458
11.0k
    int r;
2459
11.0k
    isl_int_swap(bset->ineq[i][offset], cst);
2460
11.0k
    r = isl_tab_add_row(tab, bset->ineq[i] + offset);
2461
11.0k
    isl_int_swap(bset->ineq[i][offset], cst);
2462
11.0k
    if (r < 0)
2463
0
      goto error;
2464
11.0k
    tab->con[r].is_nonneg = 1;
2465
11.0k
    if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
2466
0
      goto error;
2467
11.0k
  }
2468
3.40k
done:
2469
3.40k
  isl_int_clear(cst);
2470
3.40k
  return tab;
2471
0
error:
2472
0
  isl_int_clear(cst);
2473
0
  isl_tab_free(tab);
2474
0
  return NULL;
2475
3.40k
}
2476
2477
/* Assuming "tab" is the tableau of a cone, check if the cone is
2478
 * bounded, i.e., if it is empty or only contains the origin.
2479
 */
2480
isl_bool isl_tab_cone_is_bounded(struct isl_tab *tab)
2481
2.54k
{
2482
2.54k
  int i;
2483
2.54k
2484
2.54k
  if (!tab)
2485
0
    return isl_bool_error;
2486
2.54k
  
if (2.54k
tab->empty2.54k
)
2487
0
    return isl_bool_true;
2488
2.54k
  
if (2.54k
tab->n_dead == tab->n_col2.54k
)
2489
659
    return isl_bool_true;
2490
2.54k
2491
2.75k
  
for (;;) 1.88k
{2.75k
2492
3.00k
    for (i = tab->n_redundant; 
i < tab->n_row3.00k
;
++i249
)
{3.00k
2493
3.00k
      struct isl_tab_var *var;
2494
3.00k
      int sgn;
2495
3.00k
      var = isl_tab_var_from_row(tab, i);
2496
3.00k
      if (!var->is_nonneg)
2497
249
        continue;
2498
2.75k
      sgn = sign_of_max(tab, var);
2499
2.75k
      if (sgn < -1)
2500
0
        return isl_bool_error;
2501
2.75k
      
if (2.75k
sgn != 02.75k
)
2502
286
        return isl_bool_false;
2503
2.46k
      
if (2.46k
close_row(tab, var, 0) < 02.46k
)
2504
0
        return isl_bool_error;
2505
2.46k
      break;
2506
2.46k
    }
2507
2.46k
    
if (2.46k
tab->n_dead == tab->n_col2.46k
)
2508
1.59k
      return isl_bool_true;
2509
875
    
if (875
i == tab->n_row875
)
2510
3
      return isl_bool_false;
2511
875
  }
2512
1.88k
}
2513
2514
int isl_tab_sample_is_integer(struct isl_tab *tab)
2515
297k
{
2516
297k
  int i;
2517
297k
2518
297k
  if (!tab)
2519
0
    return -1;
2520
297k
2521
1.37M
  
for (i = 0; 297k
i < tab->n_var1.37M
;
++i1.07M
)
{1.14M
2522
1.14M
    int row;
2523
1.14M
    if (!tab->var[i].is_row)
2524
342k
      continue;
2525
802k
    row = tab->var[i].index;
2526
802k
    if (
!802k
isl_int_is_divisible_by802k
(tab->mat->row[row][1],
2527
802k
            tab->mat->row[row][0]))
2528
68.1k
      return 0;
2529
802k
  }
2530
229k
  return 1;
2531
297k
}
2532
2533
static struct isl_vec *extract_integer_sample(struct isl_tab *tab)
2534
155k
{
2535
155k
  int i;
2536
155k
  struct isl_vec *vec;
2537
155k
2538
155k
  vec = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
2539
155k
  if (!vec)
2540
0
    return NULL;
2541
155k
2542
155k
  
isl_int_set_si155k
(vec->block.data[0], 1);155k
2543
920k
  for (i = 0; 
i < tab->n_var920k
;
++i765k
)
{765k
2544
765k
    if (!tab->var[i].is_row)
2545
296k
      isl_int_set_si(vec->block.data[1 + i], 0);
2546
468k
    else {
2547
468k
      int row = tab->var[i].index;
2548
468k
      isl_int_divexact(vec->block.data[1 + i],
2549
468k
        tab->mat->row[row][1], tab->mat->row[row][0]);
2550
468k
    }
2551
765k
  }
2552
155k
2553
155k
  return vec;
2554
155k
}
2555
2556
struct isl_vec *isl_tab_get_sample_value(struct isl_tab *tab)
2557
171k
{
2558
171k
  int i;
2559
171k
  struct isl_vec *vec;
2560
171k
  isl_int m;
2561
171k
2562
171k
  if (!tab)
2563
0
    return NULL;
2564
171k
2565
171k
  vec = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
2566
171k
  if (!vec)
2567
0
    return NULL;
2568
171k
2569
171k
  
isl_int_init171k
(m);171k
2570
171k
2571
171k
  isl_int_set_si(vec->block.data[0], 1);
2572
831k
  for (i = 0; 
i < tab->n_var831k
;
++i659k
)
{659k
2573
659k
    int row;
2574
659k
    if (
!tab->var[i].is_row659k
)
{264k
2575
264k
      isl_int_set_si(vec->block.data[1 + i], 0);
2576
264k
      continue;
2577
264k
    }
2578
394k
    row = tab->var[i].index;
2579
394k
    isl_int_gcd(m, vec->block.data[0], tab->mat->row[row][0]);
2580
394k
    isl_int_divexact(m, tab->mat->row[row][0], m);
2581
394k
    isl_seq_scale(vec->block.data, vec->block.data, m, 1 + i);
2582
394k
    isl_int_divexact(m, vec->block.data[0], tab->mat->row[row][0]);
2583
394k
    isl_int_mul(vec->block.data[1 + i], m, tab->mat->row[row][1]);
2584
394k
  }
2585
171k
  vec = isl_vec_normalize(vec);
2586
171k
2587
171k
  isl_int_clear(m);
2588
171k
  return vec;
2589
171k
}
2590
2591
/* Store the sample value of "var" of "tab" rounded up (if sgn > 0)
2592
 * or down (if sgn < 0) to the nearest integer in *v.
2593
 */
2594
static void get_rounded_sample_value(struct isl_tab *tab,
2595
  struct isl_tab_var *var, int sgn, isl_int *v)
2596
148k
{
2597
148k
  if (!var->is_row)
2598
2.64k
    isl_int_set_si(*v, 0);
2599
145k
  else 
if (145k
sgn > 0145k
)
2600
144k
    isl_int_cdiv_q(*v, tab->mat->row[var->index][1],
2601
145k
           tab->mat->row[var->index][0]);
2602
145k
  else
2603
1.43k
    isl_int_fdiv_q(*v, tab->mat->row[var->index][1],
2604
148k
           tab->mat->row[var->index][0]);
2605
148k
}
2606
2607
/* Update "bmap" based on the results of the tableau "tab".
2608
 * In particular, implicit equalities are made explicit, redundant constraints
2609
 * are removed and if the sample value happens to be integer, it is stored
2610
 * in "bmap" (unless "bmap" already had an integer sample).
2611
 *
2612
 * The tableau is assumed to have been created from "bmap" using
2613
 * isl_tab_from_basic_map.
2614
 */
2615
struct isl_basic_map *isl_basic_map_update_from_tab(struct isl_basic_map *bmap,
2616
  struct isl_tab *tab)
2617
335k
{
2618
335k
  int i;
2619
335k
  unsigned n_eq;
2620
335k
2621
335k
  if (!bmap)
2622
0
    return NULL;
2623
335k
  
if (335k
!tab335k
)
2624
0
    return bmap;
2625
335k
2626
335k
  n_eq = tab->n_eq;
2627
335k
  if (tab->empty)
2628
6.20k
    bmap = isl_basic_map_set_to_empty(bmap);
2629
335k
  else
2630
1.97M
    
for (i = bmap->n_ineq - 1; 329k
i >= 01.97M
;
--i1.64M
)
{1.64M
2631
1.64M
      if (isl_tab_is_equality(tab, n_eq + i))
2632
580k
        isl_basic_map_inequality_to_equality(bmap, i);
2633
1.06M
      else 
if (1.06M
isl_tab_is_redundant(tab, n_eq + i)1.06M
)
2634
107k
        isl_basic_map_drop_inequality(bmap, i);
2635
1.64M
    }
2636
335k
  if (bmap->n_eq != n_eq)
2637
113k
    bmap = isl_basic_map_gauss(bmap, NULL);
2638
335k
  if (!tab->rational &&
2639
301k
      
bmap301k
&&
!bmap->sample301k
&&
isl_tab_sample_is_integer(tab)166k
)
2640
155k
    bmap->sample = extract_integer_sample(tab);
2641
335k
  return bmap;
2642
335k
}
2643
2644
struct isl_basic_set *isl_basic_set_update_from_tab(struct isl_basic_set *bset,
2645
  struct isl_tab *tab)
2646
29.4k
{
2647
29.4k
  return bset_from_bmap(isl_basic_map_update_from_tab(bset_to_bmap(bset),
2648
29.4k
                tab));
2649
29.4k
}
2650
2651
/* Drop the last constraint added to "tab" in position "r".
2652
 * The constraint is expected to have remained in a row.
2653
 */
2654
static isl_stat drop_last_con_in_row(struct isl_tab *tab, int r)
2655
11.4k
{
2656
11.4k
  if (!tab->con[r].is_row)
2657
0
    isl_die(isl_tab_get_ctx(tab), isl_error_internal,
2658
11.4k
      "row unexpectedly moved to column",
2659
11.4k
      return isl_stat_error);
2660
11.4k
  
if (11.4k
r + 1 != tab->n_con11.4k
)
2661
0
    isl_die(isl_tab_get_ctx(tab), isl_error_internal,
2662
11.4k
      "additional constraints added", return isl_stat_error);
2663
11.4k
  
if (11.4k
drop_row(tab, tab->con[r].index) < 011.4k
)
2664
0
    return isl_stat_error;
2665
11.4k
2666
11.4k
  return isl_stat_ok;
2667
11.4k
}
2668
2669
/* Given a non-negative variable "var", temporarily add a new non-negative
2670
 * variable that is the opposite of "var", ensuring that "var" can only attain
2671
 * the value zero.  The new variable is removed again before this function
2672
 * returns.  However, the effect of forcing "var" to be zero remains.
2673
 * If var = n/d is a row variable, then the new variable = -n/d.
2674
 * If var is a column variables, then the new variable = -var.
2675
 * If the new variable cannot attain non-negative values, then
2676
 * the resulting tableau is empty.
2677
 * Otherwise, we know the value will be zero and we close the row.
2678
 */
2679
static isl_stat cut_to_hyperplane(struct isl_tab *tab, struct isl_tab_var *var)
2680
11.4k
{
2681
11.4k
  unsigned r;
2682
11.4k
  isl_int *row;
2683
11.4k
  int sgn;
2684
11.4k
  unsigned off = 2 + tab->M;
2685
11.4k
2686
11.4k
  if (var->is_zero)
2687
0
    return isl_stat_ok;
2688
11.4k
  
if (11.4k
var->is_redundant || 11.4k
!var->is_nonneg11.4k
)
2689
0
    isl_die(isl_tab_get_ctx(tab), isl_error_invalid,
2690
11.4k
      "expecting non-redundant non-negative variable",
2691
11.4k
      return isl_stat_error);
2692
11.4k
2693
11.4k
  
if (11.4k
isl_tab_extend_cons(tab, 1) < 011.4k
)
2694
0
    return isl_stat_error;
2695
11.4k
2696
11.4k
  r = tab->n_con;
2697
11.4k
  tab->con[r].index = tab->n_row;
2698
11.4k
  tab->con[r].is_row = 1;
2699
11.4k
  tab->con[r].is_nonneg = 0;
2700
11.4k
  tab->con[r].is_zero = 0;
2701
11.4k
  tab->con[r].is_redundant = 0;
2702
11.4k
  tab->con[r].frozen = 0;
2703
11.4k
  tab->con[r].negated = 0;
2704
11.4k
  tab->row_var[tab->n_row] = ~r;
2705
11.4k
  row = tab->mat->row[tab->n_row];
2706
11.4k
2707
11.4k
  if (
var->is_row11.4k
)
{1.07k
2708
1.07k
    isl_int_set(row[0], tab->mat->row[var->index][0]);
2709
1.07k
    isl_seq_neg(row + 1,
2710
1.07k
          tab->mat->row[var->index] + 1, 1 + tab->n_col);
2711
10.3k
  } else {
2712
10.3k
    isl_int_set_si(row[0], 1);
2713
10.3k
    isl_seq_clr(row + 1, 1 + tab->n_col);
2714
10.3k
    isl_int_set_si(row[off + var->index], -1);
2715
10.3k
  }
2716
11.4k
2717
11.4k
  tab->n_row++;
2718
11.4k
  tab->n_con++;
2719
11.4k
2720
11.4k
  sgn = sign_of_max(tab, &tab->con[r]);
2721
11.4k
  if (sgn < -1)
2722
0
    return isl_stat_error;
2723
11.4k
  
if (11.4k
sgn < 011.4k
)
{41
2724
41
    if (drop_last_con_in_row(tab, r) < 0)
2725
0
      return isl_stat_error;
2726
41
    
if (41
isl_tab_mark_empty(tab) < 041
)
2727
0
      return isl_stat_error;
2728
41
    return isl_stat_ok;
2729
41
  }
2730
11.4k
  tab->con[r].is_nonneg = 1;
2731
11.4k
  /* sgn == 0 */
2732
11.4k
  if (close_row(tab, &tab->con[r], 1) < 0)
2733
0
    return isl_stat_error;
2734
11.4k
  
if (11.4k
drop_last_con_in_row(tab, r) < 011.4k
)
2735
0
    return isl_stat_error;
2736
11.4k
2737
11.4k
  return isl_stat_ok;
2738
11.4k
}
2739
2740
/* Given a tableau "tab" and an inequality constraint "con" of the tableau,
2741
 * relax the inequality by one.  That is, the inequality r >= 0 is replaced
2742
 * by r' = r + 1 >= 0.
2743
 * If r is a row variable, we simply increase the constant term by one
2744
 * (taking into account the denominator).
2745
 * If r is a column variable, then we need to modify each row that
2746
 * refers to r = r' - 1 by substituting this equality, effectively
2747
 * subtracting the coefficient of the column from the constant.
2748
 * We should only do this if the minimum is manifestly unbounded,
2749
 * however.  Otherwise, we may end up with negative sample values
2750
 * for non-negative variables.
2751
 * So, if r is a column variable with a minimum that is not
2752
 * manifestly unbounded, then we need to move it to a row.
2753
 * However, the sample value of this row may be negative,
2754
 * even after the relaxation, so we need to restore it.
2755
 * We therefore prefer to pivot a column up to a row, if possible.
2756
 */
2757
int isl_tab_relax(struct isl_tab *tab, int con)
2758
900
{
2759
900
  struct isl_tab_var *var;
2760
900
2761
900
  if (!tab)
2762
0
    return -1;
2763
900
2764
900
  var = &tab->con[con];
2765
900
2766
900
  if (
var->is_row && 900
(var->index < 0 || 47
var->index < tab->n_redundant47
))
2767
0
    isl_die(tab->mat->ctx, isl_error_invalid,
2768
900
      "cannot relax redundant constraint", return -1);
2769
900
  
if (900
!var->is_row && 900
(var->index < 0 || 853
var->index < tab->n_dead853
))
2770
0
    isl_die(tab->mat->ctx, isl_error_invalid,
2771
900
      "cannot relax dead constraint", return -1);
2772
900
2773
900
  
if (900
!var->is_row && 900
!max_is_manifestly_unbounded(tab, var)853
)
2774
351
    
if (351
to_row(tab, var, 1) < 0351
)
2775
0
      return -1;
2776
900
  
if (900
!var->is_row && 900
!min_is_manifestly_unbounded(tab, var)502
)
2777
17
    
if (17
to_row(tab, var, -1) < 017
)
2778
0
      return -1;
2779
900
2780
900
  
if (900
var->is_row900
)
{415
2781
415
    isl_int_add(tab->mat->row[var->index][1],
2782
415
        tab->mat->row[var->index][1], tab->mat->row[var->index][0]);
2783
415
    if (restore_row(tab, var) < 0)
2784
0
      return -1;
2785
485
  } else {
2786
485
    int i;
2787
485
    unsigned off = 2 + tab->M;
2788
485
2789
2.40k
    for (i = 0; 
i < tab->n_row2.40k
;
++i1.91k
)
{1.91k
2790
1.91k
      if (isl_int_is_zero(tab->mat->row[i][off + var->index]))
2791
1.29k
        continue;
2792
621
      
isl_int_sub621
(tab->mat->row[i][1], tab->mat->row[i][1],621
2793
621
          tab->mat->row[i][off + var->index]);
2794
621
    }
2795
485
2796
485
  }
2797
900
2798
900
  
if (900
isl_tab_push_var(tab, isl_tab_undo_relax, var) < 0900
)
2799
0
    return -1;
2800
900
2801
900
  return 0;
2802
900
}
2803
2804
/* Replace the variable v at position "pos" in the tableau "tab"
2805
 * by v' = v + shift.
2806
 *
2807
 * If the variable is in a column, then we first check if we can
2808
 * simply plug in v = v' - shift.  The effect on a row with
2809
 * coefficient f/d for variable v is that the constant term c/d
2810
 * is replaced by (c - f * shift)/d.  If shift is positive and
2811
 * f is negative for each row that needs to remain non-negative,
2812
 * then this is clearly safe.  In other words, if the minimum of v
2813
 * is manifestly unbounded, then we can keep v in a column position.
2814
 * Otherwise, we can pivot it down to a row.
2815
 * Similarly, if shift is negative, we need to check if the maximum
2816
 * of is manifestly unbounded.
2817
 *
2818
 * If the variable is in a row (from the start or after pivoting),
2819
 * then the constant term c/d is replaced by (c + d * shift)/d.
2820
 */
2821
int isl_tab_shift_var(struct isl_tab *tab, int pos, isl_int shift)
2822
123
{
2823
123
  struct isl_tab_var *var;
2824
123
2825
123
  if (!tab)
2826
0
    return -1;
2827
123
  
if (123
isl_int_is_zero123
(shift))
2828
63
    return 0;
2829
123
2830
60
  var = &tab->var[pos];
2831
60
  if (
!var->is_row60
)
{9
2832
9
    if (
isl_int_is_neg9
(shift))
{6
2833
6
      if (!max_is_manifestly_unbounded(tab, var))
2834
3
        
if (3
to_row(tab, var, 1) < 03
)
2835
0
          return -1;
2836
3
    } else {
2837
3
      if (!min_is_manifestly_unbounded(tab, var))
2838
0
        
if (0
to_row(tab, var, -1) < 00
)
2839
0
          return -1;
2840
3
    }
2841
9
  }
2842
60
2843
60
  
if (60
var->is_row60
)
{54
2844
54
    isl_int_addmul(tab->mat->row[var->index][1],
2845
54
        shift, tab->mat->row[var->index][0]);
2846
6
  } else {
2847
6
    int i;
2848
6
    unsigned off = 2 + tab->M;
2849
6
2850
30
    for (i = 0; 
i < tab->n_row30
;
++i24
)
{24
2851
24
      if (isl_int_is_zero(tab->mat->row[i][off + var->index]))
2852
16
        continue;
2853
8
      
isl_int_submul8
(tab->mat->row[i][1],8
2854
8
            shift, tab->mat->row[i][off + var->index]);
2855
8
    }
2856
6
2857
6
  }
2858
60
2859
60
  return 0;
2860
60
}
2861
2862
/* Remove the sign constraint from constraint "con".
2863
 *
2864
 * If the constraint variable was originally marked non-negative,
2865
 * then we make sure we mark it non-negative again during rollback.
2866
 */
2867
int isl_tab_unrestrict(struct isl_tab *tab, int con)
2868
280
{
2869
280
  struct isl_tab_var *var;
2870
280
2871
280
  if (!tab)
2872
0
    return -1;
2873
280
2874
280
  var = &tab->con[con];
2875
280
  if (!var->is_nonneg)
2876
0
    return 0;
2877
280
2878
280
  var->is_nonneg = 0;
2879
280
  if (isl_tab_push_var(tab, isl_tab_undo_unrestrict, var) < 0)
2880
0
    return -1;
2881
280
2882
280
  return 0;
2883
280
}
2884
2885
int isl_tab_select_facet(struct isl_tab *tab, int con)
2886
10.9k
{
2887
10.9k
  if (!tab)
2888
0
    return -1;
2889
10.9k
2890
10.9k
  return cut_to_hyperplane(tab, &tab->con[con]);
2891
10.9k
}
2892
2893
static int may_be_equality(struct isl_tab *tab, int row)
2894
3.88M
{
2895
36.1k
  return tab->rational ? isl_int_is_zero(tab->mat->row[row][1])
2896
3.85M
           : isl_int_lt(tab->mat->row[row][1],
2897
3.88M
              tab->mat->row[row][0]);
2898
3.88M
}
2899
2900
/* Return an isl_tab_var that has been marked or NULL if no such
2901
 * variable can be found.
2902
 * The marked field has only been set for variables that
2903
 * appear in non-redundant rows or non-dead columns.
2904
 *
2905
 * Pick the last constraint variable that is marked and
2906
 * that appears in either a non-redundant row or a non-dead columns.
2907
 * Since the returned variable is tested for being a redundant constraint or
2908
 * an implicit equality, there is no need to return any tab variable that
2909
 * corresponds to a variable.
2910
 */
2911
static struct isl_tab_var *select_marked(struct isl_tab *tab)
2912
1.29M
{
2913
1.29M
  int i;
2914
1.29M
  struct isl_tab_var *var;
2915
1.29M
2916
7.88M
  for (i = tab->n_con - 1; 
i >= 07.88M
;
--i6.59M
)
{7.76M
2917
7.76M
    var = &tab->con[i];
2918
7.76M
    if (var->index < 0)
2919
2.96M
      continue;
2920
4.80M
    
if (4.80M
var->is_row && 4.80M
var->index < tab->n_redundant3.31M
)
2921
158k
      continue;
2922
4.65M
    
if (4.65M
!var->is_row && 4.65M
var->index < tab->n_dead1.48M
)
2923
4.76k
      continue;
2924
4.64M
    
if (4.64M
var->marked4.64M
)
2925
1.17M
      return var;
2926
4.64M
  }
2927
1.29M
2928
117k
  return NULL;
2929
1.29M
}
2930
2931
/* Check for (near) equalities among the constraints.
2932
 * A constraint is an equality if it is non-negative and if
2933
 * its maximal value is either
2934
 *  - zero (in case of rational tableaus), or
2935
 *  - strictly less than 1 (in case of integer tableaus)
2936
 *
2937
 * We first mark all non-redundant and non-dead variables that
2938
 * are not frozen and not obviously not an equality.
2939
 * Then we iterate over all marked variables if they can attain
2940
 * any values larger than zero or at least one.
2941
 * If the maximal value is zero, we mark any column variables
2942
 * that appear in the row as being zero and mark the row as being redundant.
2943
 * Otherwise, if the maximal value is strictly less than one (and the
2944
 * tableau is integer), then we restrict the value to being zero
2945
 * by adding an opposite non-negative variable.
2946
 * The order in which the variables are considered is not important.
2947
 */
2948
int isl_tab_detect_implicit_equalities(struct isl_tab *tab)
2949
308k
{
2950
308k
  int i;
2951
308k
  unsigned n_marked;
2952
308k
2953
308k
  if (!tab)
2954
0
    return -1;
2955
308k
  
if (308k
tab->empty308k
)
2956
3.52k
    return 0;
2957
305k
  
if (305k
tab->n_dead == tab->n_col305k
)
2958
18.0k
    return 0;
2959
305k
2960
287k
  n_marked = 0;
2961
1.83M
  for (i = tab->n_redundant; 
i < tab->n_row1.83M
;
++i1.54M
)
{1.54M
2962
1.54M
    struct isl_tab_var *var = isl_tab_var_from_row(tab, i);
2963
1.53M
    var->marked = !var->frozen && var->is_nonneg &&
2964
1.36M
      may_be_equality(tab, i);
2965
1.54M
    if (var->marked)
2966
1.00M
      n_marked++;
2967
1.54M
  }
2968
1.51M
  for (i = tab->n_dead; 
i < tab->n_col1.51M
;
++i1.23M
)
{1.23M
2969
1.23M
    struct isl_tab_var *var = var_from_col(tab, i);
2970
1.22M
    var->marked = !var->frozen && var->is_nonneg;
2971
1.23M
    if (var->marked)
2972
152k
      n_marked++;
2973
1.23M
  }
2974
1.09M
  while (
n_marked1.09M
)
{921k
2975
921k
    struct isl_tab_var *var;
2976
921k
    int sgn;
2977
921k
    var = select_marked(tab);
2978
921k
    if (!var)
2979
111k
      break;
2980
809k
    var->marked = 0;
2981
809k
    n_marked--;
2982
809k
    sgn = sign_of_max(tab, var);
2983
809k
    if (sgn < 0)
2984
0
      return -1;
2985
809k
    
if (809k
sgn == 0809k
)
{301k
2986
301k
      if (close_row(tab, var, 0) < 0)
2987
0
        return -1;
2988
507k
    } else 
if (507k
!tab->rational && 507k
!at_least_one(tab, var)484k
)
{496
2989
496
      if (cut_to_hyperplane(tab, var) < 0)
2990
0
        return -1;
2991
496
      return isl_tab_detect_implicit_equalities(tab);
2992
496
    }
2993
6.09M
    
for (i = tab->n_redundant; 809k
i < tab->n_row6.09M
;
++i5.28M
)
{5.28M
2994
5.28M
      var = isl_tab_var_from_row(tab, i);
2995
5.28M
      if (!var->marked)
2996
2.76M
        continue;
2997
2.52M
      
if (2.52M
may_be_equality(tab, i)2.52M
)
2998
2.49M
        continue;
2999
34.2k
      var->marked = 0;
3000
34.2k
      n_marked--;
3001
34.2k
    }
3002
809k
  }
3003
287k
3004
286k
  return 0;
3005
287k
}
3006
3007
/* Update the element of row_var or col_var that corresponds to
3008
 * constraint tab->con[i] to a move from position "old" to position "i".
3009
 */
3010
static int update_con_after_move(struct isl_tab *tab, int i, int old)
3011
6.38k
{
3012
6.38k
  int *p;
3013
6.38k
  int index;
3014
6.38k
3015
6.38k
  index = tab->con[i].index;
3016
6.38k
  if (index == -1)
3017
4.21k
    return 0;
3018
2.17k
  
p = tab->con[i].is_row ? 2.17k
tab->row_var1.48k
:
tab->col_var682
;
3019
2.17k
  if (p[index] != ~old)
3020
0
    isl_die(tab->mat->ctx, isl_error_internal,
3021
2.17k
      "broken internal state", return -1);
3022
2.17k
  p[index] = ~i;
3023
2.17k
3024
2.17k
  return 0;
3025
2.17k
}
3026
3027
/* Rotate the "n" constraints starting at "first" to the right,
3028
 * putting the last constraint in the position of the first constraint.
3029
 */
3030
static int rotate_constraints(struct isl_tab *tab, int first, int n)
3031
1.83k
{
3032
1.83k
  int i, last;
3033
1.83k
  struct isl_tab_var var;
3034
1.83k
3035
1.83k
  if (n <= 1)
3036
596
    return 0;
3037
1.83k
3038
1.24k
  last = first + n - 1;
3039
1.24k
  var = tab->con[last];
3040
6.38k
  for (i = last; 
i > first6.38k
;
--i5.13k
)
{5.13k
3041
5.13k
    tab->con[i] = tab->con[i - 1];
3042
5.13k
    if (update_con_after_move(tab, i, i - 1) < 0)
3043
0
      return -1;
3044
5.13k
  }
3045
1.24k
  tab->con[first] = var;
3046
1.24k
  if (update_con_after_move(tab, first, last) < 0)
3047
0
    return -1;
3048
1.24k
3049
1.24k
  return 0;
3050
1.24k
}
3051
3052
/* Make the equalities that are implicit in "bmap" but that have been
3053
 * detected in the corresponding "tab" explicit in "bmap" and update
3054
 * "tab" to reflect the new order of the constraints.
3055
 *
3056
 * In particular, if inequality i is an implicit equality then
3057
 * isl_basic_map_inequality_to_equality will move the inequality
3058
 * in front of the other equality and it will move the last inequality
3059
 * in the position of inequality i.
3060
 * In the tableau, the inequalities of "bmap" are stored after the equalities
3061
 * and so the original order
3062
 *
3063
 *    E E E E E A A A I B B B B L
3064
 *
3065
 * is changed into
3066
 *
3067
 *    I E E E E E A A A L B B B B
3068
 *
3069
 * where I is the implicit equality, the E are equalities,
3070
 * the A inequalities before I, the B inequalities after I and
3071
 * L the last inequality.
3072
 * We therefore need to rotate to the right two sets of constraints,
3073
 * those up to and including I and those after I.
3074
 *
3075
 * If "tab" contains any constraints that are not in "bmap" then they
3076
 * appear after those in "bmap" and they should be left untouched.
3077
 *
3078
 * Note that this function leaves "bmap" in a temporary state
3079
 * as it does not call isl_basic_map_gauss.  Calling this function
3080
 * is the responsibility of the caller.
3081
 */
3082
__isl_give isl_basic_map *isl_tab_make_equalities_explicit(struct isl_tab *tab,
3083
  __isl_take isl_basic_map *bmap)
3084
55.5k
{
3085
55.5k
  int i;
3086
55.5k
3087
55.5k
  if (
!tab || 55.5k
!bmap55.5k
)
3088
0
    return isl_basic_map_free(bmap);
3089
55.5k
  
if (55.5k
tab->empty55.5k
)
3090
75
    return bmap;
3091
55.5k
3092
175k
  
for (i = bmap->n_ineq - 1; 55.4k
i >= 0175k
;
--i119k
)
{119k
3093
119k
    if (!isl_tab_is_equality(tab, bmap->n_eq + i))
3094
118k
      continue;
3095
919
    isl_basic_map_inequality_to_equality(bmap, i);
3096
919
    if (rotate_constraints(tab, 0, tab->n_eq + i + 1) < 0)
3097
0
      return isl_basic_map_free(bmap);
3098
919
    
if (919
rotate_constraints(tab, tab->n_eq + i + 1,919
3099
919
          bmap->n_ineq - i) < 0)
3100
0
      return isl_basic_map_free(bmap);
3101
919
    tab->n_eq++;
3102
919
  }
3103
55.4k
3104
55.4k
  return bmap;
3105
55.4k
}
3106
3107
static int con_is_redundant(struct isl_tab *tab, struct isl_tab_var *var)
3108
574k
{
3109
574k
  if (!tab)
3110
0
    return -1;
3111
574k
  
if (574k
tab->rational574k
)
{70.3k
3112
70.3k
    int sgn = sign_of_min(tab, var);
3113
70.3k
    if (sgn < -1)
3114
0
      return -1;
3115
70.3k
    return sgn >= 0;
3116
504k
  } else {
3117
504k
    int irred = isl_tab_min_at_most_neg_one(tab, var);
3118
504k
    if (irred < 0)
3119
0
      return -1;
3120
504k
    return !irred;
3121
504k
  }
3122
574k
}
3123
3124
/* Check for (near) redundant constraints.
3125
 * A constraint is redundant if it is non-negative and if
3126
 * its minimal value (temporarily ignoring the non-negativity) is either
3127
 *  - zero (in case of rational tableaus), or
3128
 *  - strictly larger than -1 (in case of integer tableaus)
3129
 *
3130
 * We first mark all non-redundant and non-dead variables that
3131
 * are not frozen and not obviously negatively unbounded.
3132
 * Then we iterate over all marked variables if they can attain
3133
 * any values smaller than zero or at most negative one.
3134
 * If not, we mark the row as being redundant (assuming it hasn't
3135
 * been detected as being obviously redundant in the mean time).
3136
 */
3137
int isl_tab_detect_redundant(struct isl_tab *tab)
3138
163k
{
3139
163k
  int i;
3140
163k
  unsigned n_marked;
3141
163k
3142
163k
  if (!tab)
3143
0
    return -1;
3144
163k
  
if (163k
tab->empty163k
)
3145
2.52k
    return 0;
3146
160k
  
if (160k
tab->n_redundant == tab->n_row160k
)
3147
3.19k
    return 0;
3148
160k
3149
157k
  n_marked = 0;
3150
1.01M
  for (i = tab->n_redundant; 
i < tab->n_row1.01M
;
++i854k
)
{854k
3151
854k
    struct isl_tab_var *var = isl_tab_var_from_row(tab, i);
3152
743k
    var->marked = !var->frozen && var->is_nonneg;
3153
854k
    if (var->marked)
3154
360k
      n_marked++;
3155
854k
  }
3156
799k
  for (i = tab->n_dead; 
i < tab->n_col799k
;
++i641k
)
{641k
3157
641k
    struct isl_tab_var *var = var_from_col(tab, i);
3158
583k
    var->marked = !var->frozen && var->is_nonneg &&
3159
264k
      !min_is_manifestly_unbounded(tab, var);
3160
641k
    if (var->marked)
3161
69.5k
      n_marked++;
3162
641k
  }
3163
525k
  while (
n_marked525k
)
{373k
3164
373k
    struct isl_tab_var *var;
3165
373k
    int red;
3166
373k
    var = select_marked(tab);
3167
373k
    if (!var)
3168
5.47k
      break;
3169
367k
    var->marked = 0;
3170
367k
    n_marked--;
3171
367k
    red = con_is_redundant(tab, var);
3172
367k
    if (red < 0)
3173
0
      return -1;
3174
367k
    
if (367k
red && 367k
!var->is_redundant76.4k
)
3175
11.1k
      
if (11.1k
isl_tab_mark_redundant(tab, var->index) < 011.1k
)
3176
0
        return -1;
3177
3.52M
    
for (i = tab->n_dead; 367k
i < tab->n_col3.52M
;
++i3.15M
)
{3.15M
3178
3.15M
      var = var_from_col(tab, i);
3179
3.15M
      if (!var->marked)
3180
2.95M
        continue;
3181
202k
      
if (202k
!min_is_manifestly_unbounded(tab, var)202k
)
3182
147k
        continue;
3183
54.1k
      var->marked = 0;
3184
54.1k
      n_marked--;
3185
54.1k
    }
3186
367k
  }
3187
157k
3188
157k
  return 0;
3189
157k
}
3190
3191
int isl_tab_is_equality(struct isl_tab *tab, int con)
3192
1.78M
{
3193
1.78M
  int row;
3194
1.78M
  unsigned off;
3195
1.78M
3196
1.78M
  if (!tab)
3197
0
    return -1;
3198
1.78M
  
if (1.78M
tab->con[con].is_zero1.78M
)
3199
584k
    return 1;
3200
1.20M
  
if (1.20M
tab->con[con].is_redundant1.20M
)
3201
109k
    return 0;
3202
1.09M
  
if (1.09M
!tab->con[con].is_row1.09M
)
3203
628k
    return tab->con[con].index < tab->n_dead;
3204
1.09M
3205
466k
  row = tab->con[con].index;
3206
466k
3207
466k
  off = 2 + tab->M;
3208
466k
  return isl_int_is_zero(tab->mat->row[row][1]) &&
3209
56.2k
    !row_is_big(tab, row) &&
3210
56.2k
    isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
3211
56.2k
          tab->n_col - tab->n_dead) == -1;
3212
1.09M
}
3213
3214
/* Return the minimal value of the affine expression "f" with denominator
3215
 * "denom" in *opt, *opt_denom, assuming the tableau is not empty and
3216
 * the expression cannot attain arbitrarily small values.
3217
 * If opt_denom is NULL, then *opt is rounded up to the nearest integer.
3218
 * The return value reflects the nature of the result (empty, unbounded,
3219
 * minimal value returned in *opt).
3220
 *
3221
 * This function assumes that at least one more row and at least
3222
 * one more element in the constraint array are available in the tableau.
3223
 */
3224
enum isl_lp_result isl_tab_min(struct isl_tab *tab,
3225
  isl_int *f, isl_int denom, isl_int *opt, isl_int *opt_denom,
3226
  unsigned flags)
3227
202k
{
3228
202k
  int r;
3229
202k
  enum isl_lp_result res = isl_lp_ok;
3230
202k
  struct isl_tab_var *var;
3231
202k
  struct isl_tab_undo *snap;
3232
202k
3233
202k
  if (!tab)
3234
0
    return isl_lp_error;
3235
202k
3236
202k
  
if (202k
tab->empty202k
)
3237
14
    return isl_lp_empty;
3238
202k
3239
202k
  snap = isl_tab_snap(tab);
3240
202k
  r = isl_tab_add_row(tab, f);
3241
202k
  if (r < 0)
3242
0
    return isl_lp_error;
3243
202k
  var = &tab->con[r];
3244
498k
  for (;;) {
3245
498k
    int row, col;
3246
498k
    find_pivot(tab, var, var, -1, &row, &col);
3247
498k
    if (
row == var->index498k
)
{4.26k
3248
4.26k
      res = isl_lp_unbounded;
3249
4.26k
      break;
3250
4.26k
    }
3251
494k
    
if (494k
row == -1494k
)
3252
198k
      break;
3253
296k
    
if (296k
isl_tab_pivot(tab, row, col) < 0296k
)
3254
0
      return isl_lp_error;
3255
296k
  }
3256
202k
  
isl_int_mul202k
(tab->mat->row[var->index][0],202k
3257
202k
        tab->mat->row[var->index][0], denom);
3258
202k
  if (
ISL_FL_ISSET202k
(flags, ISL_TAB_SAVE_DUAL))
{21.6k
3259
21.6k
    int i;
3260
21.6k
3261
21.6k
    isl_vec_free(tab->dual);
3262
21.6k
    tab->dual = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_con);
3263
21.6k
    if (!tab->dual)
3264
0
      return isl_lp_error;
3265
21.6k
    
isl_int_set21.6k
(tab->dual->el[0], tab->mat->row[var->index][0]);21.6k
3266
765k
    for (i = 0; 
i < tab->n_con765k
;
++i743k
)
{743k
3267
743k
      int pos;
3268
743k
      if (
tab->con[i].is_row743k
)
{460k
3269
460k
        isl_int_set_si(tab->dual->el[1 + i], 0);
3270
460k
        continue;
3271
460k
      }
3272
282k
      pos = 2 + tab->M + tab->con[i].index;
3273
282k
      if (tab->con[i].negated)
3274
48.4k
        isl_int_neg(tab->dual->el[1 + i],
3275
282k
              tab->mat->row[var->index][pos]);
3276
282k
      else
3277
234k
        isl_int_set(tab->dual->el[1 + i],
3278
282k
              tab->mat->row[var->index][pos]);
3279
282k
    }
3280
21.6k
  }
3281
202k
  
if (202k
opt && 202k
res == isl_lp_ok202k
)
{198k
3282
198k
    if (
opt_denom198k
)
{54.9k
3283
54.9k
      isl_int_set(*opt, tab->mat->row[var->index][1]);
3284
54.9k
      isl_int_set(*opt_denom, tab->mat->row[var->index][0]);
3285
54.9k
    } else
3286
143k
      get_rounded_sample_value(tab, var, 1, opt);
3287
198k
  }
3288
202k
  if (isl_tab_rollback(tab, snap) < 0)
3289
0
    return isl_lp_error;
3290
202k
  return res;
3291
202k
}
3292
3293
/* Is the constraint at position "con" marked as being redundant?
3294
 * If it is marked as representing an equality, then it is not
3295
 * considered to be redundant.
3296
 * Note that isl_tab_mark_redundant marks both the isl_tab_var as
3297
 * redundant and moves the corresponding row into the first
3298
 * tab->n_redundant positions (or removes the row, assigning it index -1),
3299
 * so the final test is actually redundant itself.
3300
 */
3301
int isl_tab_is_redundant(struct isl_tab *tab, int con)
3302
1.43M
{
3303
1.43M
  if (!tab)
3304
0
    return -1;
3305
1.43M
  
if (1.43M
con < 0 || 1.43M
con >= tab->n_con1.43M
)
3306
0
    isl_die(isl_tab_get_ctx(tab), isl_error_invalid,
3307
1.43M
      "position out of bounds", return -1);
3308
1.43M
  
if (1.43M
tab->con[con].is_zero1.43M
)
3309
92
    return 0;
3310
1.43M
  
if (1.43M
tab->con[con].is_redundant1.43M
)
3311
198k
    return 1;
3312
1.23M
  
return tab->con[con].is_row && 1.23M
tab->con[con].index < tab->n_redundant480k
;
3313
1.43M
}
3314
3315
/* Is variable "var" of "tab" fixed to a constant value by its row
3316
 * in the tableau?
3317
 * If so and if "value" is not NULL, then store this constant value
3318
 * in "value".
3319
 *
3320
 * That is, is it a row variable that only has non-zero coefficients
3321
 * for dead columns?
3322
 */
3323
static isl_bool is_constant(struct isl_tab *tab, struct isl_tab_var *var,
3324
  isl_int *value)
3325
4.04k
{
3326
4.04k
  unsigned off = 2 + tab->M;
3327
4.04k
  isl_mat *mat = tab->mat;
3328
4.04k
  int n;
3329
4.04k
  int row;
3330
4.04k
  int pos;
3331
4.04k
3332
4.04k
  if (!var->is_row)
3333
2.64k
    return isl_bool_false;
3334
1.39k
  row = var->index;
3335
1.39k
  if (row_is_big(tab, row))
3336
0
    return isl_bool_false;
3337
1.39k
  n = tab->n_col - tab->n_dead;
3338
1.39k
  pos = isl_seq_first_non_zero(mat->row[row] + off + tab->n_dead, n);
3339
1.39k
  if (pos != -1)
3340
1.31k
    return isl_bool_false;
3341
75
  
if (75
value75
)
3342
0
    isl_int_divexact(*value, mat->row[row][1], mat->row[row][0]);
3343
75
  return isl_bool_true;
3344
1.39k
}
3345
3346
/* Has the variable "var' of "tab" reached a value that is greater than
3347
 * or equal (if sgn > 0) or smaller than or equal (if sgn < 0) to "target"?
3348
 * "tmp" has been initialized by the caller and can be used
3349
 * to perform local computations.
3350
 *
3351
 * If the sample value involves the big parameter, then any value
3352
 * is reached.
3353
 * Otherwise check if n/d >= t, i.e., n >= d * t (if sgn > 0)
3354
 * or n/d <= t, i.e., n <= d * t (if sgn < 0).
3355
 */
3356
static int reached(struct isl_tab *tab, struct isl_tab_var *var, int sgn,
3357
  isl_int target, isl_int *tmp)
3358
4.85k
{
3359
4.85k
  if (row_is_big(tab, var->index))
3360
0
    return 1;
3361
4.85k
  
isl_int_mul4.85k
(*tmp, tab->mat->row[var->index][0], target);4.85k
3362
4.85k
  if (sgn > 0)
3363
1.94k
    
return 1.94k
isl_int_ge1.94k
(tab->mat->row[var->index][1], *tmp);
3364
4.85k
  else
3365
2.90k
    
return 2.90k
isl_int_le2.90k
(tab->mat->row[var->index][1], *tmp);
3366
4.85k
}
3367
3368
/* Can variable "var" of "tab" attain the value "target" by
3369
 * pivoting up (if sgn > 0) or down (if sgn < 0)?
3370
 * If not, then pivot up [down] to the greatest [smallest]
3371
 * rational value.
3372
 * "tmp" has been initialized by the caller and can be used
3373
 * to perform local computations.
3374
 *
3375
 * If the variable is manifestly unbounded in the desired direction,
3376
 * then it can attain any value.
3377
 * Otherwise, it can be moved to a row.
3378
 * Continue pivoting until the target is reached.
3379
 * If no more pivoting can be performed, the maximal [minimal]
3380
 * rational value has been reached and the target cannot be reached.
3381
 * If the variable would be pivoted into a manifestly unbounded column,
3382
 * then the target can be reached.
3383
 */
3384
static isl_bool var_reaches(struct isl_tab *tab, struct isl_tab_var *var,
3385
  int sgn, isl_int target, isl_int *tmp)
3386
4.95k
{
3387
4.95k
  int row, col;
3388
4.95k
3389
4.95k
  if (
sgn < 0 && 4.95k
min_is_manifestly_unbounded(tab, var)3.96k
)
3390
1.88k
    return isl_bool_true;
3391
3.06k
  
if (3.06k
sgn > 0 && 3.06k
max_is_manifestly_unbounded(tab, var)986
)
3392
0
    return isl_bool_true;
3393
3.06k
  
if (3.06k
to_row(tab, var, sgn) < 03.06k
)
3394
0
    return isl_bool_error;
3395
4.85k
  
while (3.06k
!reached(tab, var, sgn, target, tmp)4.85k
)
{4.39k
3396
4.39k
    find_pivot(tab, var, var, sgn, &row, &col);
3397
4.39k
    if (row == -1)
3398
1.10k
      return isl_bool_false;
3399
3.29k
    
if (3.29k
row == var->index3.29k
)
3400
1.50k
      return isl_bool_true;
3401
1.78k
    
if (1.78k
isl_tab_pivot(tab, row, col) < 01.78k
)
3402
0
      return isl_bool_error;
3403
1.78k
  }
3404
3.06k
3405
460
  return isl_bool_true;
3406
3.06k
}
3407
3408
/* Check if variable "var" of "tab" can only attain a single (integer)
3409
 * value, and, if so, add an equality constraint to fix the variable
3410
 * to this single value and store the result in "target".
3411
 * "target" and "tmp" have been initialized by the caller.
3412
 *
3413
 * Given the current sample value, round it down and check
3414
 * whether it is possible to attain a strictly smaller integer value.
3415
 * If so, the variable is not restricted to a single integer value.
3416
 * Otherwise, the search stops at the smallest rational value.
3417
 * Round up this value and check whether it is possible to attain
3418
 * a strictly greater integer value.
3419
 * If so, the variable is not restricted to a single integer value.
3420
 * Otherwise, the search stops at the greatest rational value.
3421
 * If rounding down this value yields a value that is different
3422
 * from rounding up the smallest rational value, then the variable
3423
 * cannot attain any integer value.  Mark the tableau empty.
3424
 * Otherwise, add an equality constraint that fixes the variable
3425
 * to the single integer value found.
3426
 */
3427
static isl_bool detect_constant_with_tmp(struct isl_tab *tab,
3428
  struct isl_tab_var *var, isl_int *target, isl_int *tmp)
3429
3.96k
{
3430
3.96k
  isl_bool reached;
3431
3.96k
  isl_vec *eq;
3432
3.96k
  int pos;
3433
3.96k
  isl_stat r;
3434
3.96k
3435
3.96k
  get_rounded_sample_value(tab, var, -1, target);
3436
3.96k
  isl_int_sub_ui(*target, *target, 1);
3437
3.96k
  reached = var_reaches(tab, var, -1, *target, tmp);
3438
3.96k
  if (
reached < 0 || 3.96k
reached3.96k
)
3439
2.97k
    return isl_bool_not(reached);
3440
986
  get_rounded_sample_value(tab, var, 1, target);
3441
986
  isl_int_add_ui(*target, *target, 1);
3442
986
  reached = var_reaches(tab, var, 1, *target, tmp);
3443
986
  if (
reached < 0 || 986
reached986
)
3444
872
    return isl_bool_not(reached);
3445
114
  get_rounded_sample_value(tab, var, -1, tmp);
3446
114
  isl_int_sub_ui(*target, *target, 1);
3447
114
  if (
isl_int_ne114
(*target, *tmp))
{0
3448
0
    if (isl_tab_mark_empty(tab) < 0)
3449
0
      return isl_bool_error;
3450
0
    return isl_bool_false;
3451
0
  }
3452
114
3453
114
  
if (114
isl_tab_extend_cons(tab, 1) < 0114
)
3454
0
    return isl_bool_error;
3455
114
  eq = isl_vec_alloc(isl_tab_get_ctx(tab), 1 + tab->n_var);
3456
114
  if (!eq)
3457
0
    return isl_bool_error;
3458
114
  pos = var - tab->var;
3459
114
  isl_seq_clr(eq->el + 1, tab->n_var);
3460
114
  isl_int_set_si(eq->el[1 + pos], -1);
3461
114
  isl_int_set(eq->el[0], *target);
3462
114
  r = isl_tab_add_eq(tab, eq->el);
3463
114
  isl_vec_free(eq);
3464
114
3465
114
  return r < 0 ? 
isl_bool_error0
:
isl_bool_true114
;
3466
114
}
3467
3468
/* Check if variable "var" of "tab" can only attain a single (integer)
3469
 * value, and, if so, add an equality constraint to fix the variable
3470
 * to this single value and store the result in "value" (if "value"
3471
 * is not NULL).
3472
 *
3473
 * If the current sample value involves the big parameter,
3474
 * then the variable cannot have a fixed integer value.
3475
 * If the variable is already fixed to a single value by its row, then
3476
 * there is no need to add another equality constraint.
3477
 *
3478
 * Otherwise, allocate some temporary variables and continue
3479
 * with detect_constant_with_tmp.
3480
 */
3481
static isl_bool get_constant(struct isl_tab *tab, struct isl_tab_var *var,
3482
  isl_int *value)
3483
4.04k
{
3484
4.04k
  isl_int target, tmp;
3485
4.04k
  isl_bool is_cst;
3486
4.04k
3487
4.04k
  if (
var->is_row && 4.04k
row_is_big(tab, var->index)1.39k
)
3488
0
    return isl_bool_false;
3489
4.04k
  is_cst = is_constant(tab, var, value);
3490
4.04k
  if (
is_cst < 0 || 4.04k
is_cst4.04k
)
3491
75
    return is_cst;
3492
4.04k
3493
3.96k
  
if (3.96k
!value3.96k
)
3494
1.32k
    isl_int_init(target);
3495
3.96k
  isl_int_init(tmp);
3496
3.96k
3497
3.96k
  is_cst = detect_constant_with_tmp(tab, var,
3498
2.64k
              value ? 
value2.64k
:
&target1.32k
, &tmp);
3499
3.96k
3500
3.96k
  isl_int_clear(tmp);
3501
3.96k
  if (!value)
3502
1.32k
    isl_int_clear(target);
3503
3.96k
3504
3.96k
  return is_cst;
3505
4.04k
}
3506
3507
/* Check if variable "var" of "tab" can only attain a single (integer)
3508
 * value, and, if so, add an equality constraint to fix the variable
3509
 * to this single value and store the result in "value" (if "value"
3510
 * is not NULL).
3511
 *
3512
 * For rational tableaus, nothing needs to be done.
3513
 */
3514
isl_bool isl_tab_is_constant(struct isl_tab *tab, int var, isl_int *value)
3515
2.64k
{
3516
2.64k
  if (!tab)
3517
0
    return isl_bool_error;
3518
2.64k
  
if (2.64k
var < 0 || 2.64k
var >= tab->n_var2.64k
)
3519
0
    isl_die(isl_tab_get_ctx(tab), isl_error_invalid,
3520
2.64k
      "position out of bounds", return isl_bool_error);
3521
2.64k
  
if (2.64k
tab->rational2.64k
)
3522
0
    return isl_bool_false;
3523
2.64k
3524
2.64k
  return get_constant(tab, &tab->var[var], value);
3525
2.64k
}
3526
3527
/* Check if any of the variables of "tab" can only attain a single (integer)
3528
 * value, and, if so, add equality constraints to fix those variables
3529
 * to these single values.
3530
 *
3531
 * For rational tableaus, nothing needs to be done.
3532
 */
3533
isl_stat isl_tab_detect_constants(struct isl_tab *tab)
3534
280
{
3535
280
  int i;
3536
280
3537
280
  if (!tab)
3538
0
    return isl_stat_error;
3539
280
  
if (280
tab->rational280
)
3540
0
    return isl_stat_ok;
3541
280
3542
1.67k
  
for (i = 0; 280
i < tab->n_var1.67k
;
++i1.39k
)
{1.39k
3543
1.39k
    if (get_constant(tab, &tab->var[i], NULL) < 0)
3544
0
      return isl_stat_error;
3545
1.39k
  }
3546
280
3547
280
  return isl_stat_ok;
3548
280
}
3549
3550
/* Take a snapshot of the tableau that can be restored by a call to
3551
 * isl_tab_rollback.
3552
 */
3553
struct isl_tab_undo *isl_tab_snap(struct isl_tab *tab)
3554
854k
{
3555
854k
  if (!tab)
3556
0
    return NULL;
3557
854k
  tab->need_undo = 1;
3558
854k
  return tab->top;
3559
854k
}
3560
3561
/* Does "tab" need to keep track of undo information?
3562
 * That is, was a snapshot taken that may need to be restored?
3563
 */
3564
isl_bool isl_tab_need_undo(struct isl_tab *tab)
3565
244
{
3566
244
  if (!tab)
3567
0
    return isl_bool_error;
3568
244
3569
244
  return tab->need_undo;
3570
244
}
3571
3572
/* Remove all tracking of undo information from "tab", invalidating
3573
 * any snapshots that may have been taken of the tableau.
3574
 * Since all snapshots have been invalidated, there is also
3575
 * no need to start keeping track of undo information again.
3576
 */
3577
void isl_tab_clear_undo(struct isl_tab *tab)
3578
244
{
3579
244
  if (!tab)
3580
0
    return;
3581
244
3582
244
  free_undo(tab);
3583
244
  tab->need_undo = 0;
3584
244
}
3585
3586
/* Undo the operation performed by isl_tab_relax.
3587
 */
3588
static isl_stat unrelax(struct isl_tab *tab, struct isl_tab_var *var)
3589
  WARN_UNUSED;
3590
static isl_stat unrelax(struct isl_tab *tab, struct isl_tab_var *var)
3591
452
{
3592
452
  unsigned off = 2 + tab->M;
3593
452
3594
452
  if (
!var->is_row && 452
!max_is_manifestly_unbounded(tab, var)448
)
3595
78
    
if (78
to_row(tab, var, 1) < 078
)
3596
0
      return isl_stat_error;
3597
452
3598
452
  
if (452
var->is_row452
)
{82
3599
82
    isl_int_sub(tab->mat->row[var->index][1],
3600
82
        tab->mat->row[var->index][1], tab->mat->row[var->index][0]);
3601
82
    if (
var->is_nonneg82
)
{82
3602
82
      int sgn = restore_row(tab, var);
3603
82
      isl_assert(tab->mat->ctx, sgn >= 0,
3604
82
        return isl_stat_error);
3605
82
    }
3606
370
  } else {
3607
370
    int i;
3608
370
3609
1.87k
    for (i = 0; 
i < tab->n_row1.87k
;
++i1.50k
)
{1.50k
3610
1.50k
      if (isl_int_is_zero(tab->mat->row[i][off + var->index]))
3611
1.07k
        continue;
3612
431
      
isl_int_add431
(tab->mat->row[i][1], tab->mat->row[i][1],431
3613
431
          tab->mat->row[i][off + var->index]);
3614
431
    }
3615
370
3616
370
  }
3617
452
3618
452
  return isl_stat_ok;
3619
452
}
3620
3621
/* Undo the operation performed by isl_tab_unrestrict.
3622
 *
3623
 * In particular, mark the variable as being non-negative and make
3624
 * sure the sample value respects this constraint.
3625
 */
3626
static isl_stat ununrestrict(struct isl_tab *tab, struct isl_tab_var *var)
3627
263
{
3628
263
  var->is_nonneg = 1;
3629
263
3630
263
  if (
var->is_row && 263
restore_row(tab, var) < -1233
)
3631
0
    return isl_stat_error;
3632
263
3633
263
  return isl_stat_ok;
3634
263
}
3635
3636
/* Unmark the last redundant row in "tab" as being redundant.
3637
 * This undoes part of the modifications performed by isl_tab_mark_redundant.
3638
 * In particular, remove the redundant mark and make
3639
 * sure the sample value respects the constraint again.
3640
 * A variable that is marked non-negative by isl_tab_mark_redundant
3641
 * is covered by a separate undo record.
3642
 */
3643
static isl_stat restore_last_redundant(struct isl_tab *tab)
3644
455k
{
3645
455k
  struct isl_tab_var *var;
3646
455k
3647
455k
  if (tab->n_redundant < 1)
3648
0
    isl_die(isl_tab_get_ctx(tab), isl_error_internal,
3649
455k
      "no redundant rows", return isl_stat_error);
3650
455k
3651
455k
  var = isl_tab_var_from_row(tab, tab->n_redundant - 1);
3652
455k
  var->is_redundant = 0;
3653
455k
  tab->n_redundant--;
3654
455k
  restore_row(tab, var);
3655
455k
3656
455k
  return isl_stat_ok;
3657
455k
}
3658
3659
static isl_stat perform_undo_var(struct isl_tab *tab, struct isl_tab_undo *undo)
3660
  WARN_UNUSED;
3661
static isl_stat perform_undo_var(struct isl_tab *tab, struct isl_tab_undo *undo)
3662
1.92M
{
3663
1.92M
  struct isl_tab_var *var = var_from_index(tab, undo->u.var_index);
3664
1.92M
  switch (undo->type) {
3665
382k
  case isl_tab_undo_nonneg:
3666
382k
    var->is_nonneg = 0;
3667
382k
    break;
3668
349k
  case isl_tab_undo_redundant:
3669
349k
    if (
!var->is_row || 349k
var->index != tab->n_redundant - 1349k
)
3670
0
      isl_die(isl_tab_get_ctx(tab), isl_error_internal,
3671
349k
        "not undoing last redundant row", return -1);
3672
349k
    return restore_last_redundant(tab);
3673
296k
  case isl_tab_undo_freeze:
3674
296k
    var->frozen = 0;
3675
296k
    break;
3676
56.5k
  case isl_tab_undo_zero:
3677
56.5k
    var->is_zero = 0;
3678
56.5k
    if (!var->is_row)
3679
56.1k
      tab->n_dead--;
3680
56.5k
    break;
3681
841k
  case isl_tab_undo_allocate:
3682
841k
    if (
undo->u.var_index >= 0841k
)
{5.79k
3683
5.79k
      isl_assert(tab->mat->ctx, !var->is_row,
3684
5.79k
        return isl_stat_error);
3685
5.79k
      return drop_col(tab, var->index);
3686
5.79k
    }
3687
835k
    
if (835k
!var->is_row835k
)
{89.5k
3688
89.5k
      if (
!max_is_manifestly_unbounded(tab, var)89.5k
)
{57.3k
3689
57.3k
        if (to_row(tab, var, 1) < 0)
3690
0
          return isl_stat_error;
3691
32.2k
      } else 
if (32.2k
!min_is_manifestly_unbounded(tab, var)32.2k
)
{13.2k
3692
13.2k
        if (to_row(tab, var, -1) < 0)
3693
0
          return isl_stat_error;
3694
13.2k
      } else
3695
18.9k
        
if (18.9k
to_row(tab, var, 0) < 018.9k
)
3696
0
          return isl_stat_error;
3697
89.5k
    }
3698
835k
    return drop_row(tab, var->index);
3699
452
  case isl_tab_undo_relax:
3700
452
    return unrelax(tab, var);
3701
263
  case isl_tab_undo_unrestrict:
3702
263
    return ununrestrict(tab, var);
3703
0
  default:
3704
0
    isl_die(tab->mat->ctx, isl_error_internal,
3705
1.92M
      "perform_undo_var called on invalid undo record",
3706
1.92M
      return isl_stat_error);
3707
1.92M
  }
3708
1.92M
3709
735k
  return isl_stat_ok;
3710
1.92M
}
3711
3712
/* Restore all rows that have been marked redundant by isl_tab_mark_redundant
3713
 * and that have been preserved in the tableau.
3714
 * Note that isl_tab_mark_redundant may also have marked some variables
3715
 * as being non-negative before marking them redundant.  These need
3716
 * to be removed as well as otherwise some constraints could end up
3717
 * getting marked redundant with respect to the variable.
3718
 */
3719
isl_stat isl_tab_restore_redundant(struct isl_tab *tab)
3720
81.5k
{
3721
81.5k
  if (!tab)
3722
0
    return isl_stat_error;
3723
81.5k
3724
81.5k
  
if (81.5k
tab->need_undo81.5k
)
3725
0
    isl_die(isl_tab_get_ctx(tab), isl_error_invalid,
3726
81.5k
      "manually restoring redundant constraints "
3727
81.5k
      "interferes with undo history",
3728
81.5k
      return isl_stat_error);
3729
81.5k
3730
187k
  
while (81.5k
tab->n_redundant > 0187k
)
{106k
3731
106k
    if (
tab->row_var[tab->n_redundant - 1] >= 0106k
)
{93.5k
3732
93.5k
      struct isl_tab_var *var;
3733
93.5k
3734
93.5k
      var = isl_tab_var_from_row(tab, tab->n_redundant - 1);
3735
93.5k
      var->is_nonneg = 0;
3736
93.5k
    }
3737
106k
    restore_last_redundant(tab);
3738
106k
  }
3739
81.5k
  return isl_stat_ok;
3740
81.5k
}
3741
3742
/* Undo the addition of an integer division to the basic map representation
3743
 * of "tab" in position "pos".
3744
 */
3745
static isl_stat drop_bmap_div(struct isl_tab *tab, int pos)
3746
3.80k
{
3747
3.80k
  int off;
3748
3.80k
3749
3.80k
  off = tab->n_var - isl_basic_map_dim(tab->bmap, isl_dim_div);
3750
3.80k
  if (isl_basic_map_drop_div(tab->bmap, pos - off) < 0)
3751
0
    return isl_stat_error;
3752
3.80k
  
if (3.80k
tab->samples3.80k
)
{536
3753
536
    tab->samples = isl_mat_drop_cols(tab->samples, 1 + pos, 1);
3754
536
    if (!tab->samples)
3755
0
      return isl_stat_error;
3756
536
  }
3757
3.80k
3758
3.80k
  return isl_stat_ok;
3759
3.80k
}
3760
3761
/* Restore the tableau to the state where the basic variables
3762
 * are those in "col_var".
3763
 * We first construct a list of variables that are currently in
3764
 * the basis, but shouldn't.  Then we iterate over all variables
3765
 * that should be in the basis and for each one that is currently
3766
 * not in the basis, we exchange it with one of the elements of the
3767
 * list constructed before.
3768
 * We can always find an appropriate variable to pivot with because
3769
 * the current basis is mapped to the old basis by a non-singular
3770
 * matrix and so we can never end up with a zero row.
3771
 */
3772
static int restore_basis(struct isl_tab *tab, int *col_var)
3773
574
{
3774
574
  int i, j;
3775
574
  int n_extra = 0;
3776
574
  int *extra = NULL;  /* current columns that contain bad stuff */
3777
574
  unsigned off = 2 + tab->M;
3778
574
3779
574
  extra = isl_alloc_array(tab->mat->ctx, int, tab->n_col);
3780
574
  if (
tab->n_col && 574
!extra574
)
3781
0
    goto error;
3782
6.90k
  
for (i = 0; 574
i < tab->n_col6.90k
;
++i6.33k
)
{6.33k
3783
52.5k
    for (j = 0; 
j < tab->n_col52.5k
;
++j46.2k
)
3784
51.1k
      
if (51.1k
tab->col_var[i] == col_var[j]51.1k
)
3785
4.90k
        break;
3786
6.33k
    if (j < tab->n_col)
3787
4.90k
      continue;
3788
1.42k
    extra[n_extra++] = i;
3789
1.42k
  }
3790
5.06k
  for (i = 0; 
i < tab->n_col && 5.06k
n_extra > 04.94k
;
++i4.49k
)
{4.49k
3791
4.49k
    struct isl_tab_var *var;
3792
4.49k
    int row;
3793
4.49k
3794
35.9k
    for (j = 0; 
j < tab->n_col35.9k
;
++j31.4k
)
3795
34.4k
      
if (34.4k
col_var[i] == tab->col_var[j]34.4k
)
3796
3.06k
        break;
3797
4.49k
    if (j < tab->n_col)
3798
3.06k
      continue;
3799
1.42k
    var = var_from_index(tab, col_var[i]);
3800
1.42k
    row = var->index;
3801
1.79k
    for (j = 0; 
j < n_extra1.79k
;
++j364
)
3802
1.79k
      
if (1.79k
!1.79k
isl_int_is_zero1.79k
(tab->mat->row[row][off+extra[j]]))
3803
1.42k
        break;
3804
1.42k
    isl_assert(tab->mat->ctx, j < n_extra, goto error);
3805
1.42k
    
if (1.42k
isl_tab_pivot(tab, row, extra[j]) < 01.42k
)
3806
0
      goto error;
3807
1.42k
    extra[j] = extra[--n_extra];
3808
1.42k
  }
3809
574
3810
574
  free(extra);
3811
574
  return 0;
3812
0
error:
3813
0
  free(extra);
3814
0
  return -1;
3815
574
}
3816
3817
/* Remove all samples with index n or greater, i.e., those samples
3818
 * that were added since we saved this number of samples in
3819
 * isl_tab_save_samples.
3820
 */
3821
static void drop_samples_since(struct isl_tab *tab, int n)
3822
18.6k
{
3823
18.6k
  int i;
3824
18.6k
3825
22.9k
  for (i = tab->n_sample - 1; 
i >= 0 && 22.9k
tab->n_sample > n21.6k
;
--i4.28k
)
{4.28k
3826
4.28k
    if (tab->sample_index[i] < n)
3827
1.59k
      continue;
3828
4.28k
3829
2.68k
    
if (2.68k
i != tab->n_sample - 12.68k
)
{1.74k
3830
1.74k
      int t = tab->sample_index[tab->n_sample-1];
3831
1.74k
      tab->sample_index[tab->n_sample-1] = tab->sample_index[i];
3832
1.74k
      tab->sample_index[i] = t;
3833
1.74k
      isl_mat_swap_rows(tab->samples, tab->n_sample-1, i);
3834
1.74k
    }
3835
2.68k
    tab->n_sample--;
3836
2.68k
  }
3837
18.6k
}
3838
3839
static isl_stat perform_undo(struct isl_tab *tab, struct isl_tab_undo *undo)
3840
  WARN_UNUSED;
3841
static isl_stat perform_undo(struct isl_tab *tab, struct isl_tab_undo *undo)
3842
2.25M
{
3843
2.25M
  switch (undo->type) {
3844
9.69k
  case isl_tab_undo_rational:
3845
9.69k
    tab->rational = 0;
3846
9.69k
    break;
3847
56.4k
  case isl_tab_undo_empty:
3848
56.4k
    tab->empty = 0;
3849
56.4k
    break;
3850
1.92M
  case isl_tab_undo_nonneg:
3851
1.92M
  case isl_tab_undo_redundant:
3852
1.92M
  case isl_tab_undo_freeze:
3853
1.92M
  case isl_tab_undo_zero:
3854
1.92M
  case isl_tab_undo_allocate:
3855
1.92M
  case isl_tab_undo_relax:
3856
1.92M
  case isl_tab_undo_unrestrict:
3857
1.92M
    return perform_undo_var(tab, undo);
3858
0
  case isl_tab_undo_bmap_eq:
3859
0
    return isl_basic_map_free_equality(tab->bmap, 1);
3860
227k
  case isl_tab_undo_bmap_ineq:
3861
227k
    return isl_basic_map_free_inequality(tab->bmap, 1);
3862
3.80k
  case isl_tab_undo_bmap_div:
3863
3.80k
    return drop_bmap_div(tab, undo->u.var_index);
3864
574
  case isl_tab_undo_saved_basis:
3865
574
    if (restore_basis(tab, undo->u.col_var) < 0)
3866
0
      return isl_stat_error;
3867
574
    break;
3868
4.91k
  case isl_tab_undo_drop_sample:
3869
4.91k
    tab->n_outside--;
3870
4.91k
    break;
3871
18.6k
  case isl_tab_undo_saved_samples:
3872
18.6k
    drop_samples_since(tab, undo->u.n);
3873
18.6k
    break;
3874
2.01k
  case isl_tab_undo_callback:
3875
2.01k
    return undo->u.callback->run(undo->u.callback);
3876
0
  default:
3877
0
    isl_assert(tab->mat->ctx, 0, return isl_stat_error);
3878
2.25M
  }
3879
90.3k
  return isl_stat_ok;
3880
2.25M
}
3881
3882
/* Return the tableau to the state it was in when the snapshot "snap"
3883
 * was taken.
3884
 */
3885
int isl_tab_rollback(struct isl_tab *tab, struct isl_tab_undo *snap)
3886
766k
{
3887
766k
  struct isl_tab_undo *undo, *next;
3888
766k
3889
766k
  if (!tab)
3890
0
    return -1;
3891
766k
3892
766k
  tab->in_undo = 1;
3893
3.01M
  for (undo = tab->top; 
undo && 3.01M
undo != &tab->bottom3.01M
;
undo = next2.25M
)
{2.46M
3894
2.46M
    next = undo->next;
3895
2.46M
    if (undo == snap)
3896
218k
      break;
3897
2.25M
    
if (2.25M
perform_undo(tab, undo) < 02.25M
)
{0
3898
0
      tab->top = undo;
3899
0
      free_undo(tab);
3900
0
      tab->in_undo = 0;
3901
0
      return -1;
3902
0
    }
3903
2.25M
    free_undo_record(undo);
3904
2.25M
  }
3905
766k
  tab->in_undo = 0;
3906
766k
  tab->top = undo;
3907
766k
  if (!undo)
3908
0
    return -1;
3909
766k
  return 0;
3910
766k
}
3911
3912
/* The given row "row" represents an inequality violated by all
3913
 * points in the tableau.  Check for some special cases of such
3914
 * separating constraints.
3915
 * In particular, if the row has been reduced to the constant -1,
3916
 * then we know the inequality is adjacent (but opposite) to
3917
 * an equality in the tableau.
3918
 * If the row has been reduced to r = c*(-1 -r'), with r' an inequality
3919
 * of the tableau and c a positive constant, then the inequality
3920
 * is adjacent (but opposite) to the inequality r'.
3921
 */
3922
static enum isl_ineq_type separation_type(struct isl_tab *tab, unsigned row)
3923
65.0k
{
3924
65.0k
  int pos;
3925
65.0k
  unsigned off = 2 + tab->M;
3926
65.0k
3927
65.0k
  if (tab->rational)
3928
9.20k
    return isl_ineq_separate;
3929
65.0k
3930
55.8k
  
if (55.8k
!55.8k
isl_int_is_one55.8k
(tab->mat->row[row][0]))
3931
148
    return isl_ineq_separate;
3932
55.8k
3933
55.7k
  pos = isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
3934
55.7k
          tab->n_col - tab->n_dead);
3935
55.7k
  if (
pos == -155.7k
)
{6.53k
3936
6.53k
    if (isl_int_is_negone(tab->mat->row[row][1]))
3937
5.40k
      return isl_ineq_adj_eq;
3938
6.53k
    else
3939
1.12k
      return isl_ineq_separate;
3940
6.53k
  }
3941
55.7k
3942
49.2k
  
if (49.2k
!49.2k
isl_int_eq49.2k
(tab->mat->row[row][1],
3943
49.2k
      tab->mat->row[row][off + tab->n_dead + pos]))
3944
19.9k
    return isl_ineq_separate;
3945
49.2k
3946
29.2k
  pos = isl_seq_first_non_zero(
3947
29.2k
      tab->mat->row[row] + off + tab->n_dead + pos + 1,
3948
29.2k
      tab->n_col - tab->n_dead - pos - 1);
3949
29.2k
3950
27.1k
  return pos == -1 ? 
isl_ineq_adj_ineq27.1k
:
isl_ineq_separate2.10k
;
3951
49.2k
}
3952
3953
/* Check the effect of inequality "ineq" on the tableau "tab".
3954
 * The result may be
3955
 *  isl_ineq_redundant: satisfied by all points in the tableau
3956
 *  isl_ineq_separate:  satisfied by no point in the tableau
3957
 *  isl_ineq_cut:   satisfied by some by not all points
3958
 *  isl_ineq_adj_eq:  adjacent to an equality
3959
 *  isl_ineq_adj_ineq:  adjacent to an inequality.
3960
 */
3961
enum isl_ineq_type isl_tab_ineq_type(struct isl_tab *tab, isl_int *ineq)
3962
345k
{
3963
345k
  enum isl_ineq_type type = isl_ineq_error;
3964
345k
  struct isl_tab_undo *snap = NULL;
3965
345k
  int con;
3966
345k
  int row;
3967
345k
3968
345k
  if (!tab)
3969
0
    return isl_ineq_error;
3970
345k
3971
345k
  
if (345k
isl_tab_extend_cons(tab, 1) < 0345k
)
3972
0
    return isl_ineq_error;
3973
345k
3974
345k
  snap = isl_tab_snap(tab);
3975
345k
3976
345k
  con = isl_tab_add_row(tab, ineq);
3977
345k
  if (con < 0)
3978
0
    goto error;
3979
345k
3980
345k
  row = tab->con[con].index;
3981
345k
  if (isl_tab_row_is_redundant(tab, row))
3982
0
    type = isl_ineq_redundant;
3983
345k
  else 
if (345k
isl_int_is_neg345k
(tab->mat->row[row][1]) &&345k
3984
139k
     (tab->rational ||
3985
117k
        isl_int_abs_ge(tab->mat->row[row][1],
3986
138k
           tab->mat->row[row][0]))) {
3987
138k
    int nonneg = at_least_zero(tab, &tab->con[con]);
3988
138k
    if (nonneg < 0)
3989
0
      goto error;
3990
138k
    
if (138k
nonneg138k
)
3991
73.5k
      type = isl_ineq_cut;
3992
138k
    else
3993
65.0k
      type = separation_type(tab, row);
3994
206k
  } else {
3995
206k
    int red = con_is_redundant(tab, &tab->con[con]);
3996
206k
    if (red < 0)
3997
0
      goto error;
3998
206k
    
if (206k
!red206k
)
3999
56.6k
      type = isl_ineq_cut;
4000
206k
    else
4001
150k
      type = isl_ineq_redundant;
4002
206k
  }
4003
345k
4004
345k
  
if (345k
isl_tab_rollback(tab, snap)345k
)
4005
0
    return isl_ineq_error;
4006
345k
  return type;
4007
0
error:
4008
0
  return isl_ineq_error;
4009
345k
}
4010
4011
isl_stat isl_tab_track_bmap(struct isl_tab *tab, __isl_take isl_basic_map *bmap)
4012
129k
{
4013
129k
  bmap = isl_basic_map_cow(bmap);
4014
129k
  if (
!tab || 129k
!bmap129k
)
4015
0
    goto error;
4016
129k
4017
129k
  
if (129k
tab->empty129k
)
{4.25k
4018
4.25k
    bmap = isl_basic_map_set_to_empty(bmap);
4019
4.25k
    if (!bmap)
4020
0
      goto error;
4021
4.25k
    tab->bmap = bmap;
4022
4.25k
    return isl_stat_ok;
4023
4.25k
  }
4024
129k
4025
125k
  
isl_assert125k
(tab->mat->ctx, tab->n_eq == bmap->n_eq, goto error);125k
4026
125k
  
isl_assert125k
(tab->mat->ctx,125k
4027
125k
        tab->n_con == bmap->n_eq + bmap->n_ineq, goto error);
4028
125k
4029
125k
  tab->bmap = bmap;
4030
125k
4031
125k
  return isl_stat_ok;
4032
0
error:
4033
0
  isl_basic_map_free(bmap);
4034
0
  return isl_stat_error;
4035
125k
}
4036
4037
isl_stat isl_tab_track_bset(struct isl_tab *tab, __isl_take isl_basic_set *bset)
4038
866
{
4039
866
  return isl_tab_track_bmap(tab, bset_to_bmap(bset));
4040
866
}
4041
4042
__isl_keep isl_basic_set *isl_tab_peek_bset(struct isl_tab *tab)
4043
23.4k
{
4044
23.4k
  if (!tab)
4045
0
    return NULL;
4046
23.4k
4047
23.4k
  return bset_from_bmap(tab->bmap);
4048
23.4k
}
4049
4050
static void isl_tab_print_internal(__isl_keep struct isl_tab *tab,
4051
  FILE *out, int indent)
4052
0
{
4053
0
  unsigned r, c;
4054
0
  int i;
4055
0
4056
0
  if (
!tab0
)
{0
4057
0
    fprintf(out, "%*snull tab\n", indent, "");
4058
0
    return;
4059
0
  }
4060
0
  fprintf(out, "%*sn_redundant: %d, n_dead: %d", indent, "",
4061
0
    tab->n_redundant, tab->n_dead);
4062
0
  if (tab->rational)
4063
0
    fprintf(out, ", rational");
4064
0
  if (tab->empty)
4065
0
    fprintf(out, ", empty");
4066
0
  fprintf(out, "\n");
4067
0
  fprintf(out, "%*s[", indent, "");
4068
0
  for (i = 0; 
i < tab->n_var0
;
++i0
)
{0
4069
0
    if (i)
4070
0
      fprintf(out, (i == tab->n_param ||
4071
0
              
i == tab->n_var - tab->n_div0
) ?
"; "0
4072
0
                    : ", ");
4073
0
    fprintf(out, "%c%d%s", tab->var[i].is_row ? 
'r'0
:
'c'0
,
4074
0
          tab->var[i].index,
4075
0
          tab->var[i].is_zero ? " [=0]" :
4076
0
          
tab->var[i].is_redundant ? 0
" [R]"0
:
""0
);
4077
0
  }
4078
0
  fprintf(out, "]\n");
4079
0
  fprintf(out, "%*s[", indent, "");
4080
0
  for (i = 0; 
i < tab->n_con0
;
++i0
)
{0
4081
0
    if (i)
4082
0
      fprintf(out, ", ");
4083
0
    fprintf(out, "%c%d%s", tab->con[i].is_row ? 
'r'0
:
'c'0
,
4084
0
          tab->con[i].index,
4085
0
          tab->con[i].is_zero ? " [=0]" :
4086
0
          
tab->con[i].is_redundant ? 0
" [R]"0
:
""0
);
4087
0
  }
4088
0
  fprintf(out, "]\n");
4089
0
  fprintf(out, "%*s[", indent, "");
4090
0
  for (i = 0; 
i < tab->n_row0
;
++i0
)
{0
4091
0
    const char *sign = "";
4092
0
    if (i)
4093
0
      fprintf(out, ", ");
4094
0
    if (
tab->row_sign0
)
{0
4095
0
      if (tab->row_sign[i] == isl_tab_row_unknown)
4096
0
        sign = "?";
4097
0
      else 
if (0
tab->row_sign[i] == isl_tab_row_neg0
)
4098
0
        sign = "-";
4099
0
      else 
if (0
tab->row_sign[i] == isl_tab_row_pos0
)
4100
0
        sign = "+";
4101
0
      else
4102
0
        sign = "+-";
4103
0
    }
4104
0
    fprintf(out, "r%d: %d%s%s", i, tab->row_var[i],
4105
0
        isl_tab_var_from_row(tab, i)->is_nonneg ? 
" [>=0]"0
:
""0
, sign);
4106
0
  }
4107
0
  fprintf(out, "]\n");
4108
0
  fprintf(out, "%*s[", indent, "");
4109
0
  for (i = 0; 
i < tab->n_col0
;
++i0
)
{0
4110
0
    if (i)
4111
0
      fprintf(out, ", ");
4112
0
    fprintf(out, "c%d: %d%s", i, tab->col_var[i],
4113
0
        var_from_col(tab, i)->is_nonneg ? 
" [>=0]"0
:
""0
);
4114
0
  }
4115
0
  fprintf(out, "]\n");
4116
0
  r = tab->mat->n_row;
4117
0
  tab->mat->n_row = tab->n_row;
4118
0
  c = tab->mat->n_col;
4119
0
  tab->mat->n_col = 2 + tab->M + tab->n_col;
4120
0
  isl_mat_print_internal(tab->mat, out, indent);
4121
0
  tab->mat->n_row = r;
4122
0
  tab->mat->n_col = c;
4123
0
  if (tab->bmap)
4124
0
    isl_basic_map_print_internal(tab->bmap, out, indent);
4125
0
}
4126
4127
void isl_tab_dump(__isl_keep struct isl_tab *tab)
4128
0
{
4129
0
  isl_tab_print_internal(tab, stderr, 0);
4130
0
}