Coverage Report

Created: 2017-04-29 12:21

/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
492k
{
36
492k
  int i;
37
492k
  struct isl_tab *tab;
38
492k
  unsigned off = 2 + M;
39
492k
40
492k
  tab = isl_calloc_type(ctx, struct isl_tab);
41
492k
  if (!tab)
42
0
    return NULL;
43
492k
  tab->mat = isl_mat_alloc(ctx, n_row, off + n_var);
44
492k
  if (!tab->mat)
45
0
    goto error;
46
492k
  
tab->var = 492k
isl_alloc_array492k
(ctx, struct isl_tab_var, n_var);
47
492k
  if (
n_var && 492k
!tab->var492k
)
48
0
    goto error;
49
492k
  
tab->con = 492k
isl_alloc_array492k
(ctx, struct isl_tab_var, n_row);
50
492k
  if (
n_row && 492k
!tab->con492k
)
51
0
    goto error;
52
492k
  
tab->col_var = 492k
isl_alloc_array492k
(ctx, int, n_var);
53
492k
  if (
n_var && 492k
!tab->col_var492k
)
54
0
    goto error;
55
492k
  
tab->row_var = 492k
isl_alloc_array492k
(ctx, int, n_row);
56
492k
  if (
n_row && 492k
!tab->row_var492k
)
57
0
    goto error;
58
2.59M
  
for (i = 0; 492k
i < n_var2.59M
;
++i2.09M
)
{2.09M
59
2.09M
    tab->var[i].index = i;
60
2.09M
    tab->var[i].is_row = 0;
61
2.09M
    tab->var[i].is_nonneg = 0;
62
2.09M
    tab->var[i].is_zero = 0;
63
2.09M
    tab->var[i].is_redundant = 0;
64
2.09M
    tab->var[i].frozen = 0;
65
2.09M
    tab->var[i].negated = 0;
66
2.09M
    tab->col_var[i] = i;
67
2.09M
  }
68
492k
  tab->n_row = 0;
69
492k
  tab->n_con = 0;
70
492k
  tab->n_eq = 0;
71
492k
  tab->max_con = n_row;
72
492k
  tab->n_col = n_var;
73
492k
  tab->n_var = n_var;
74
492k
  tab->max_var = n_var;
75
492k
  tab->n_param = 0;
76
492k
  tab->n_div = 0;
77
492k
  tab->n_dead = 0;
78
492k
  tab->n_redundant = 0;
79
492k
  tab->strict_redundant = 0;
80
492k
  tab->need_undo = 0;
81
492k
  tab->rational = 0;
82
492k
  tab->empty = 0;
83
492k
  tab->in_undo = 0;
84
492k
  tab->M = M;
85
492k
  tab->cone = 0;
86
492k
  tab->bottom.type = isl_tab_undo_bottom;
87
492k
  tab->bottom.next = NULL;
88
492k
  tab->top = &tab->bottom;
89
492k
90
492k
  tab->n_zero = 0;
91
492k
  tab->n_unbounded = 0;
92
492k
  tab->basis = NULL;
93
492k
94
492k
  return tab;
95
0
error:
96
0
  isl_tab_free(tab);
97
0
  return NULL;
98
492k
}
99
100
isl_ctx *isl_tab_get_ctx(struct isl_tab *tab)
101
1.94M
{
102
1.94M
  return tab ? isl_mat_get_ctx(tab->mat) : NULL;
103
1.94M
}
104
105
int isl_tab_extend_cons(struct isl_tab *tab, unsigned n_new)
106
562k
{
107
562k
  unsigned off;
108
562k
109
562k
  if (!tab)
110
0
    return -1;
111
562k
112
562k
  off = 2 + tab->M;
113
562k
114
562k
  if (
tab->max_con < tab->n_con + n_new562k
)
{57.6k
115
57.6k
    struct isl_tab_var *con;
116
57.6k
117
57.6k
    con = isl_realloc_array(tab->mat->ctx, tab->con,
118
57.6k
            struct isl_tab_var, tab->max_con + n_new);
119
57.6k
    if (!con)
120
0
      return -1;
121
57.6k
    tab->con = con;
122
57.6k
    tab->max_con += n_new;
123
57.6k
  }
124
562k
  
if (562k
tab->mat->n_row < tab->n_row + n_new562k
)
{59.8k
125
59.8k
    int *row_var;
126
59.8k
127
59.8k
    tab->mat = isl_mat_extend(tab->mat,
128
59.8k
          tab->n_row + n_new, off + tab->n_col);
129
59.8k
    if (!tab->mat)
130
0
      return -1;
131
59.8k
    
row_var = 59.8k
isl_realloc_array59.8k
(tab->mat->ctx, tab->row_var,
132
59.8k
              int, tab->mat->n_row);
133
59.8k
    if (!row_var)
134
0
      return -1;
135
59.8k
    tab->row_var = row_var;
136
59.8k
    if (
tab->row_sign59.8k
)
{305
137
305
      enum isl_tab_row_sign *s;
138
305
      s = isl_realloc_array(tab->mat->ctx, tab->row_sign,
139
305
          enum isl_tab_row_sign, tab->mat->n_row);
140
305
      if (!s)
141
0
        return -1;
142
305
      tab->row_sign = s;
143
305
    }
144
59.8k
  }
145
562k
  return 0;
146
562k
}
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
4.36k
{
153
4.36k
  struct isl_tab_var *var;
154
4.36k
  unsigned off = 2 + tab->M;
155
4.36k
156
4.36k
  if (
tab->max_var < tab->n_var + n_new4.36k
)
{3.06k
157
3.06k
    var = isl_realloc_array(tab->mat->ctx, tab->var,
158
3.06k
            struct isl_tab_var, tab->n_var + n_new);
159
3.06k
    if (!var)
160
0
      return -1;
161
3.06k
    tab->var = var;
162
3.06k
    tab->max_var = tab->n_var + n_new;
163
3.06k
  }
164
4.36k
165
4.36k
  
if (4.36k
tab->mat->n_col < off + tab->n_col + n_new4.36k
)
{2.49k
166
2.49k
    int *p;
167
2.49k
168
2.49k
    tab->mat = isl_mat_extend(tab->mat,
169
2.49k
            tab->mat->n_row, off + tab->n_col + n_new);
170
2.49k
    if (!tab->mat)
171
0
      return -1;
172
2.49k
    
p = 2.49k
isl_realloc_array2.49k
(tab->mat->ctx, tab->col_var,
173
2.49k
              int, tab->n_col + n_new);
174
2.49k
    if (!p)
175
0
      return -1;
176
2.49k
    tab->col_var = p;
177
2.49k
  }
178
4.36k
179
4.36k
  return 0;
180
4.36k
}
181
182
static void free_undo_record(struct isl_tab_undo *undo)
183
2.13M
{
184
2.13M
  switch (undo->type) {
185
742
  case isl_tab_undo_saved_basis:
186
742
    free(undo->u.col_var);
187
742
    break;
188
2.13M
  default:;
189
2.13M
  }
190
2.13M
  free(undo);
191
2.13M
}
192
193
static void free_undo(struct isl_tab *tab)
194
496k
{
195
496k
  struct isl_tab_undo *undo, *next;
196
496k
197
855k
  for (undo = tab->top; 
undo && 855k
undo != &tab->bottom855k
;
undo = next359k
)
{359k
198
359k
    next = undo->next;
199
359k
    free_undo_record(undo);
200
359k
  }
201
496k
  tab->top = undo;
202
496k
}
203
204
void isl_tab_free(struct isl_tab *tab)
205
518k
{
206
518k
  if (!tab)
207
22.8k
    return;
208
496k
  free_undo(tab);
209
496k
  isl_mat_free(tab->mat);
210
496k
  isl_vec_free(tab->dual);
211
496k
  isl_basic_map_free(tab->bmap);
212
496k
  free(tab->var);
213
496k
  free(tab->con);
214
496k
  free(tab->row_var);
215
496k
  free(tab->col_var);
216
496k
  free(tab->row_sign);
217
496k
  isl_mat_free(tab->samples);
218
496k
  free(tab->sample_index);
219
496k
  isl_mat_free(tab->basis);
220
496k
  free(tab);
221
496k
}
222
223
struct isl_tab *isl_tab_dup(struct isl_tab *tab)
224
1.96k
{
225
1.96k
  int i;
226
1.96k
  struct isl_tab *dup;
227
1.96k
  unsigned off;
228
1.96k
229
1.96k
  if (!tab)
230
0
    return NULL;
231
1.96k
232
1.96k
  off = 2 + tab->M;
233
1.96k
  dup = isl_calloc_type(tab->mat->ctx, struct isl_tab);
234
1.96k
  if (!dup)
235
0
    return NULL;
236
1.96k
  dup->mat = isl_mat_dup(tab->mat);
237
1.96k
  if (!dup->mat)
238
0
    goto error;
239
1.96k
  
dup->var = 1.96k
isl_alloc_array1.96k
(tab->mat->ctx, struct isl_tab_var, tab->max_var);
240
1.96k
  if (
tab->max_var && 1.96k
!dup->var1.96k
)
241
0
    goto error;
242
18.8k
  
for (i = 0; 1.96k
i < tab->n_var18.8k
;
++i16.8k
)
243
16.8k
    dup->var[i] = tab->var[i];
244
1.96k
  dup->con = isl_alloc_array(tab->mat->ctx, struct isl_tab_var, tab->max_con);
245
1.96k
  if (
tab->max_con && 1.96k
!dup->con1.96k
)
246
0
    goto error;
247
21.5k
  
for (i = 0; 1.96k
i < tab->n_con21.5k
;
++i19.6k
)
248
19.6k
    dup->con[i] = tab->con[i];
249
1.96k
  dup->col_var = isl_alloc_array(tab->mat->ctx, int, tab->mat->n_col - off);
250
1.96k
  if (
(tab->mat->n_col - off) && 1.96k
!dup->col_var1.96k
)
251
0
    goto error;
252
9.32k
  
for (i = 0; 1.96k
i < tab->n_col9.32k
;
++i7.35k
)
253
7.35k
    dup->col_var[i] = tab->col_var[i];
254
1.96k
  dup->row_var = isl_alloc_array(tab->mat->ctx, int, tab->mat->n_row);
255
1.96k
  if (
tab->mat->n_row && 1.96k
!dup->row_var1.96k
)
256
0
    goto error;
257
21.4k
  
for (i = 0; 1.96k
i < tab->n_row21.4k
;
++i19.4k
)
258
19.4k
    dup->row_var[i] = tab->row_var[i];
259
1.96k
  if (
tab->row_sign1.96k
)
{1.95k
260
1.95k
    dup->row_sign = isl_alloc_array(tab->mat->ctx, enum isl_tab_row_sign,
261
1.95k
            tab->mat->n_row);
262
1.95k
    if (
tab->mat->n_row && 1.95k
!dup->row_sign1.95k
)
263
0
      goto error;
264
21.3k
    
for (i = 0; 1.95k
i < tab->n_row21.3k
;
++i19.4k
)
265
19.4k
      dup->row_sign[i] = tab->row_sign[i];
266
1.95k
  }
267
1.96k
  
if (1.96k
tab->samples1.96k
)
{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
1.96k
  dup->n_row = tab->n_row;
279
1.96k
  dup->n_con = tab->n_con;
280
1.96k
  dup->n_eq = tab->n_eq;
281
1.96k
  dup->max_con = tab->max_con;
282
1.96k
  dup->n_col = tab->n_col;
283
1.96k
  dup->n_var = tab->n_var;
284
1.96k
  dup->max_var = tab->max_var;
285
1.96k
  dup->n_param = tab->n_param;
286
1.96k
  dup->n_div = tab->n_div;
287
1.96k
  dup->n_dead = tab->n_dead;
288
1.96k
  dup->n_redundant = tab->n_redundant;
289
1.96k
  dup->rational = tab->rational;
290
1.96k
  dup->empty = tab->empty;
291
1.96k
  dup->strict_redundant = 0;
292
1.96k
  dup->need_undo = 0;
293
1.96k
  dup->in_undo = 0;
294
1.96k
  dup->M = tab->M;
295
1.96k
  tab->cone = tab->cone;
296
1.96k
  dup->bottom.type = isl_tab_undo_bottom;
297
1.96k
  dup->bottom.next = NULL;
298
1.96k
  dup->top = &dup->bottom;
299
1.96k
300
1.96k
  dup->n_zero = tab->n_zero;
301
1.96k
  dup->n_unbounded = tab->n_unbounded;
302
1.96k
  dup->basis = isl_mat_dup(tab->basis);
303
1.96k
304
1.96k
  return dup;
305
0
error:
306
0
  isl_tab_free(dup);
307
0
  return NULL;
308
1.96k
}
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
1.37k
{
328
1.37k
  int i;
329
1.37k
  struct isl_mat *prod;
330
1.37k
  unsigned n;
331
1.37k
332
1.37k
  prod = isl_mat_alloc(mat1->ctx, mat1->n_row + mat2->n_row,
333
1.37k
          off + col1 + col2);
334
1.37k
  if (!prod)
335
0
    return NULL;
336
1.37k
337
1.37k
  n = 0;
338
7.23k
  for (i = 0; 
i < r17.23k
;
++i5.86k
)
{5.86k
339
5.86k
    isl_seq_cpy(prod->row[n + i], mat1->row[i], off + d1);
340
5.86k
    isl_seq_clr(prod->row[n + i] + off + d1, d2);
341
5.86k
    isl_seq_cpy(prod->row[n + i] + off + d1 + d2,
342
5.86k
        mat1->row[i] + off + d1, col1 - d1);
343
5.86k
    isl_seq_clr(prod->row[n + i] + off + col1 + d1, col2 - d2);
344
5.86k
  }
345
1.37k
346
1.37k
  n += r1;
347
7.23k
  for (i = 0; 
i < r27.23k
;
++i5.86k
)
{5.86k
348
5.86k
    isl_seq_cpy(prod->row[n + i], mat2->row[i], off);
349
5.86k
    isl_seq_clr(prod->row[n + i] + off, d1);
350
5.86k
    isl_seq_cpy(prod->row[n + i] + off + d1,
351
5.86k
          mat2->row[i] + off, d2);
352
5.86k
    isl_seq_clr(prod->row[n + i] + off + d1 + d2, col1 - d1);
353
5.86k
    isl_seq_cpy(prod->row[n + i] + off + col1 + d1,
354
5.86k
          mat2->row[i] + off + d2, col2 - d2);
355
5.86k
  }
356
1.37k
357
1.37k
  n += r2;
358
18.3k
  for (i = 0; 
i < row1 - r118.3k
;
++i16.9k
)
{16.9k
359
16.9k
    isl_seq_cpy(prod->row[n + i], mat1->row[r1 + i], off + d1);
360
16.9k
    isl_seq_clr(prod->row[n + i] + off + d1, d2);
361
16.9k
    isl_seq_cpy(prod->row[n + i] + off + d1 + d2,
362
16.9k
        mat1->row[r1 + i] + off + d1, col1 - d1);
363
16.9k
    isl_seq_clr(prod->row[n + i] + off + col1 + d1, col2 - d2);
364
16.9k
  }
365
1.37k
366
1.37k
  n += row1 - r1;
367
18.3k
  for (i = 0; 
i < row2 - r218.3k
;
++i16.9k
)
{16.9k
368
16.9k
    isl_seq_cpy(prod->row[n + i], mat2->row[r2 + i], off);
369
16.9k
    isl_seq_clr(prod->row[n + i] + off, d1);
370
16.9k
    isl_seq_cpy(prod->row[n + i] + off + d1,
371
16.9k
          mat2->row[r2 + i] + off, d2);
372
16.9k
    isl_seq_clr(prod->row[n + i] + off + d1 + d2, col1 - d1);
373
16.9k
    isl_seq_cpy(prod->row[n + i] + off + col1 + d1,
374
16.9k
          mat2->row[r2 + i] + off + d2, col2 - d2);
375
16.9k
  }
376
1.37k
377
1.37k
  return prod;
378
1.37k
}
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
31.1k
{
386
31.1k
  if (var->index == -1)
387
195
    return;
388
30.9k
  
if (30.9k
var->is_row && 30.9k
var->index >= r122.8k
)
389
16.9k
    var->index += r2;
390
30.9k
  if (
!var->is_row && 30.9k
var->index >= d18.12k
)
391
6.69k
    var->index += d2;
392
30.9k
}
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
31.1k
{
401
31.1k
  if (var->index == -1)
402
195
    return;
403
30.9k
  
if (30.9k
var->is_row30.9k
)
{22.8k
404
22.8k
    if (var->index < r2)
405
5.86k
      var->index += r1;
406
22.8k
    else
407
16.9k
      var->index += row1;
408
8.12k
  } else {
409
8.12k
    if (var->index < d2)
410
1.43k
      var->index += d1;
411
8.12k
    else
412
6.69k
      var->index += col1;
413
8.12k
  }
414
30.9k
}
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
1.37k
{
436
1.37k
  int i;
437
1.37k
  struct isl_tab *prod;
438
1.37k
  unsigned off;
439
1.37k
  unsigned r1, r2, d1, d2;
440
1.37k
441
1.37k
  if (
!tab1 || 1.37k
!tab21.37k
)
442
0
    return NULL;
443
1.37k
444
1.37k
  
isl_assert1.37k
(tab1->mat->ctx, tab1->M == tab2->M, return NULL);1.37k
445
1.37k
  
isl_assert1.37k
(tab1->mat->ctx, tab1->rational == tab2->rational, return NULL);1.37k
446
1.37k
  
isl_assert1.37k
(tab1->mat->ctx, tab1->cone == tab2->cone, return NULL);1.37k
447
1.37k
  
isl_assert1.37k
(tab1->mat->ctx, !tab1->row_sign, return NULL);1.37k
448
1.37k
  
isl_assert1.37k
(tab1->mat->ctx, !tab2->row_sign, return NULL);1.37k
449
1.37k
  
isl_assert1.37k
(tab1->mat->ctx, tab1->n_param == 0, return NULL);1.37k
450
1.37k
  
isl_assert1.37k
(tab1->mat->ctx, tab2->n_param == 0, return NULL);1.37k
451
1.37k
  
isl_assert1.37k
(tab1->mat->ctx, tab1->n_div == 0, return NULL);1.37k
452
1.37k
  
isl_assert1.37k
(tab1->mat->ctx, tab2->n_div == 0, return NULL);1.37k
453
1.37k
454
1.37k
  off = 2 + tab1->M;
455
1.37k
  r1 = tab1->n_redundant;
456
1.37k
  r2 = tab2->n_redundant;
457
1.37k
  d1 = tab1->n_dead;
458
1.37k
  d2 = tab2->n_dead;
459
1.37k
  prod = isl_calloc_type(tab1->mat->ctx, struct isl_tab);
460
1.37k
  if (!prod)
461
0
    return NULL;
462
1.37k
  prod->mat = tab_mat_product(tab1->mat, tab2->mat,
463
1.37k
        tab1->n_row, tab2->n_row,
464
1.37k
        tab1->n_col, tab2->n_col, off, r1, r2, d1, d2);
465
1.37k
  if (!prod->mat)
466
0
    goto error;
467
1.37k
  
prod->var = 1.37k
isl_alloc_array1.37k
(tab1->mat->ctx, struct isl_tab_var,
468
1.37k
          tab1->max_var + tab2->max_var);
469
1.37k
  if (
(tab1->max_var + tab2->max_var) && 1.37k
!prod->var1.37k
)
470
0
    goto error;
471
9.69k
  
for (i = 0; 1.37k
i < tab1->n_var9.69k
;
++i8.31k
)
{8.31k
472
8.31k
    prod->var[i] = tab1->var[i];
473
8.31k
    update_index1(&prod->var[i], r1, r2, d1, d2);
474
8.31k
  }
475
9.69k
  for (i = 0; 
i < tab2->n_var9.69k
;
++i8.31k
)
{8.31k
476
8.31k
    prod->var[tab1->n_var + i] = tab2->var[i];
477
8.31k
    update_index2(&prod->var[tab1->n_var + i],
478
8.31k
        tab1->n_row, tab1->n_col,
479
8.31k
        r1, r2, d1, d2);
480
8.31k
  }
481
1.37k
  prod->con = isl_alloc_array(tab1->mat->ctx, struct isl_tab_var,
482
1.37k
          tab1->max_con +  tab2->max_con);
483
1.37k
  if (
(tab1->max_con + tab2->max_con) && 1.37k
!prod->con1.37k
)
484
0
    goto error;
485
24.1k
  
for (i = 0; 1.37k
i < tab1->n_con24.1k
;
++i22.8k
)
{22.8k
486
22.8k
    prod->con[i] = tab1->con[i];
487
22.8k
    update_index1(&prod->con[i], r1, r2, d1, d2);
488
22.8k
  }
489
24.1k
  for (i = 0; 
i < tab2->n_con24.1k
;
++i22.8k
)
{22.8k
490
22.8k
    prod->con[tab1->n_con + i] = tab2->con[i];
491
22.8k
    update_index2(&prod->con[tab1->n_con + i],
492
22.8k
        tab1->n_row, tab1->n_col,
493
22.8k
        r1, r2, d1, d2);
494
22.8k
  }
495
1.37k
  prod->col_var = isl_alloc_array(tab1->mat->ctx, int,
496
1.37k
          tab1->n_col + tab2->n_col);
497
1.37k
  if (
(tab1->n_col + tab2->n_col) && 1.37k
!prod->col_var1.37k
)
498
0
    goto error;
499
9.49k
  
for (i = 0; 1.37k
i < tab1->n_col9.49k
;
++i8.12k
)
{8.12k
500
6.69k
    int pos = i < d1 ? 
i1.43k
:
i + d26.69k
;
501
8.12k
    prod->col_var[pos] = tab1->col_var[i];
502
8.12k
  }
503
9.49k
  for (i = 0; 
i < tab2->n_col9.49k
;
++i8.12k
)
{8.12k
504
6.69k
    int pos = i < d2 ? 
d1 + i1.43k
:
tab1->n_col + i6.69k
;
505
8.12k
    int t = tab2->col_var[i];
506
8.12k
    if (t >= 0)
507
188
      t += tab1->n_var;
508
8.12k
    else
509
7.93k
      t -= tab1->n_con;
510
8.12k
    prod->col_var[pos] = t;
511
8.12k
  }
512
1.37k
  prod->row_var = isl_alloc_array(tab1->mat->ctx, int,
513
1.37k
          tab1->mat->n_row + tab2->mat->n_row);
514
1.37k
  if (
(tab1->mat->n_row + tab2->mat->n_row) && 1.37k
!prod->row_var1.37k
)
515
0
    goto error;
516
24.1k
  
for (i = 0; 1.37k
i < tab1->n_row24.1k
;
++i22.8k
)
{22.8k
517
16.9k
    int pos = i < r1 ? 
i5.86k
:
i + r216.9k
;
518
22.8k
    prod->row_var[pos] = tab1->row_var[i];
519
22.8k
  }
520
24.1k
  for (i = 0; 
i < tab2->n_row24.1k
;
++i22.8k
)
{22.8k
521
16.9k
    int pos = i < r2 ? 
r1 + i5.86k
:
tab1->n_row + i16.9k
;
522
22.8k
    int t = tab2->row_var[i];
523
22.8k
    if (t >= 0)
524
8.12k
      t += tab1->n_var;
525
22.8k
    else
526
14.6k
      t -= tab1->n_con;
527
22.8k
    prod->row_var[pos] = t;
528
22.8k
  }
529
1.37k
  prod->samples = NULL;
530
1.37k
  prod->sample_index = NULL;
531
1.37k
  prod->n_row = tab1->n_row + tab2->n_row;
532
1.37k
  prod->n_con = tab1->n_con + tab2->n_con;
533
1.37k
  prod->n_eq = 0;
534
1.37k
  prod->max_con = tab1->max_con + tab2->max_con;
535
1.37k
  prod->n_col = tab1->n_col + tab2->n_col;
536
1.37k
  prod->n_var = tab1->n_var + tab2->n_var;
537
1.37k
  prod->max_var = tab1->max_var + tab2->max_var;
538
1.37k
  prod->n_param = 0;
539
1.37k
  prod->n_div = 0;
540
1.37k
  prod->n_dead = tab1->n_dead + tab2->n_dead;
541
1.37k
  prod->n_redundant = tab1->n_redundant + tab2->n_redundant;
542
1.37k
  prod->rational = tab1->rational;
543
1.37k
  prod->empty = tab1->empty || tab2->empty;
544
1.37k
  prod->strict_redundant = tab1->strict_redundant || tab2->strict_redundant;
545
1.37k
  prod->need_undo = 0;
546
1.37k
  prod->in_undo = 0;
547
1.37k
  prod->M = tab1->M;
548
1.37k
  prod->cone = tab1->cone;
549
1.37k
  prod->bottom.type = isl_tab_undo_bottom;
550
1.37k
  prod->bottom.next = NULL;
551
1.37k
  prod->top = &prod->bottom;
552
1.37k
553
1.37k
  prod->n_zero = 0;
554
1.37k
  prod->n_unbounded = 0;
555
1.37k
  prod->basis = NULL;
556
1.37k
557
1.37k
  return prod;
558
0
error:
559
0
  isl_tab_free(prod);
560
0
  return NULL;
561
1.37k
}
562
563
static struct isl_tab_var *var_from_index(struct isl_tab *tab, int i)
564
61.5M
{
565
61.5M
  if (i >= 0)
566
17.2M
    return &tab->var[i];
567
61.5M
  else
568
44.2M
    return &tab->con[~i];
569
61.5M
}
570
571
struct isl_tab_var *isl_tab_var_from_row(struct isl_tab *tab, int i)
572
45.5M
{
573
45.5M
  return var_from_index(tab, tab->row_var[i]);
574
45.5M
}
575
576
static struct isl_tab_var *var_from_col(struct isl_tab *tab, int i)
577
14.4M
{
578
14.4M
  return var_from_index(tab, tab->col_var[i]);
579
14.4M
}
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.26M
{
588
1.26M
  int i;
589
1.26M
  unsigned off = 2 + tab->M;
590
1.26M
591
1.26M
  if (var->is_row)
592
781k
    return 0;
593
2.42M
  
for (i = tab->n_redundant; 485k
i < tab->n_row2.42M
;
++i1.94M
)
{2.05M
594
2.05M
    if (
!2.05M
isl_int_is_neg2.05M
(tab->mat->row[i][off + var->index]))
595
1.73M
      continue;
596
320k
    
if (320k
isl_tab_var_from_row(tab, i)->is_nonneg320k
)
597
110k
      return 0;
598
320k
  }
599
375k
  return 1;
600
485k
}
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
997k
{
609
997k
  int i;
610
997k
  unsigned off = 2 + tab->M;
611
997k
612
997k
  if (var->is_row)
613
481k
    return 0;
614
3.49M
  
for (i = tab->n_redundant; 516k
i < tab->n_row3.49M
;
++i2.97M
)
{3.23M
615
3.23M
    if (
!3.23M
isl_int_is_pos3.23M
(tab->mat->row[i][off + var->index]))
616
2.74M
      continue;
617
494k
    
if (494k
isl_tab_var_from_row(tab, i)->is_nonneg494k
)
618
260k
      return 0;
619
494k
  }
620
255k
  return 1;
621
516k
}
622
623
static int row_cmp(struct isl_tab *tab, int r1, int r2, int c, isl_int *t)
624
961k
{
625
961k
  unsigned off = 2 + tab->M;
626
961k
627
961k
  if (
tab->M961k
)
{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
961k
  
isl_int_mul961k
(*t, tab->mat->row[r1][1], tab->mat->row[r2][off + c]);961k
636
961k
  isl_int_submul(*t, tab->mat->row[r2][1], tab->mat->row[r1][off + c]);
637
961k
  return isl_int_sgn(*t);
638
961k
}
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
1.97M
{
663
1.97M
  int j, r, tsgn;
664
1.97M
  isl_int t;
665
1.97M
  unsigned off = 2 + tab->M;
666
1.97M
667
1.97M
  isl_int_init(t);
668
1.97M
  r = -1;
669
19.7M
  for (j = tab->n_redundant; 
j < tab->n_row19.7M
;
++j17.8M
)
{17.8M
670
17.8M
    if (
var && 17.8M
j == var->index15.4M
)
671
1.75M
      continue;
672
16.0M
    
if (16.0M
!isl_tab_var_from_row(tab, j)->is_nonneg16.0M
)
673
4.22M
      continue;
674
11.8M
    
if (11.8M
sgn * 11.8M
isl_int_sgn11.8M
(tab->mat->row[j][off + c]) >= 0)
675
9.69M
      continue;
676
2.14M
    
if (2.14M
r < 02.14M
)
{1.18M
677
1.18M
      r = j;
678
1.18M
      continue;
679
1.18M
    }
680
961k
    tsgn = sgn * row_cmp(tab, r, j, c, &t);
681
961k
    if (
tsgn < 0 || 961k
(tsgn == 0 &&788k
682
313k
              tab->row_var[j] < tab->row_var[r]))
683
385k
      r = j;
684
961k
  }
685
1.97M
  isl_int_clear(t);
686
1.97M
  return r;
687
1.97M
}
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.48M
{
709
2.48M
  int j, r, c;
710
2.48M
  isl_int *tr;
711
2.48M
712
2.48M
  *row = *col = -1;
713
2.48M
714
2.48M
  isl_assert(tab->mat->ctx, var->is_row, return);
715
2.48M
  tr = tab->mat->row[var->index] + 2 + tab->M;
716
2.48M
717
2.48M
  c = -1;
718
17.2M
  for (j = tab->n_dead; 
j < tab->n_col17.2M
;
++j14.7M
)
{14.7M
719
14.7M
    if (isl_int_is_zero(tr[j]))
720
10.9M
      continue;
721
3.86M
    
if (3.86M
isl_int_sgn3.86M
(tr[j]) != sgn &&3.86M
722
2.19M
        var_from_col(tab, j)->is_nonneg)
723
1.41M
      continue;
724
2.45M
    
if (2.45M
c < 0 || 2.45M
tab->col_var[j] < tab->col_var[c]661k
)
725
1.93M
      c = j;
726
2.45M
  }
727
2.48M
  if (c < 0)
728
686k
    return;
729
2.48M
730
1.79M
  
sgn *= 1.79M
isl_int_sgn1.79M
(tr[c]);
731
1.79M
  r = pivot_row(tab, skip_var, sgn, c);
732
1.00M
  *row = r < 0 ? 
var->index785k
:
r1.00M
;
733
1.79M
  *col = c;
734
1.79M
}
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
11.0M
{
744
11.0M
  int i;
745
11.0M
  unsigned off = 2 + tab->M;
746
11.0M
747
11.0M
  if (
tab->row_var[row] < 0 && 11.0M
!isl_tab_var_from_row(tab, row)->is_nonneg8.52M
)
748
573k
    return 0;
749
11.0M
750
10.5M
  
if (10.5M
isl_int_is_neg10.5M
(tab->mat->row[row][1]))
751
1.10M
    return 0;
752
9.41M
  
if (9.41M
tab->strict_redundant && 9.41M
isl_int_is_zero43
(tab->mat->row[row][1]))
753
42
    return 0;
754
9.41M
  
if (9.41M
tab->M && 9.41M
isl_int_is_neg26.6k
(tab->mat->row[row][2]))
755
1.00k
    return 0;
756
9.41M
757
34.2M
  
for (i = tab->n_dead; 9.41M
i < tab->n_col34.2M
;
++i24.8M
)
{33.3M
758
33.3M
    if (isl_int_is_zero(tab->mat->row[row][off + i]))
759
21.3M
      continue;
760
11.9M
    
if (11.9M
tab->col_var[i] >= 011.9M
)
761
4.36M
      return 0;
762
7.60M
    
if (7.60M
isl_int_is_neg7.60M
(tab->mat->row[row][off + i]))
763
3.96M
      return 0;
764
3.63M
    
if (3.63M
!var_from_col(tab, i)->is_nonneg3.63M
)
765
113k
      return 0;
766
3.63M
  }
767
977k
  return 1;
768
9.41M
}
769
770
static void swap_rows(struct isl_tab *tab, int row1, int row2)
771
887k
{
772
887k
  int t;
773
887k
  enum isl_tab_row_sign s;
774
887k
775
887k
  t = tab->row_var[row1];
776
887k
  tab->row_var[row1] = tab->row_var[row2];
777
887k
  tab->row_var[row2] = t;
778
887k
  isl_tab_var_from_row(tab, row1)->index = row1;
779
887k
  isl_tab_var_from_row(tab, row2)->index = row2;
780
887k
  tab->mat = isl_mat_swap_rows(tab->mat, row1, row2);
781
887k
782
887k
  if (!tab->row_sign)
783
880k
    return;
784
6.83k
  s = tab->row_sign[row1];
785
6.83k
  tab->row_sign[row1] = tab->row_sign[row2];
786
6.83k
  tab->row_sign[row2] = s;
787
6.83k
}
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
7.70M
{
794
7.70M
  struct isl_tab_undo *undo;
795
7.70M
796
7.70M
  if (!tab)
797
0
    return -1;
798
7.70M
  
if (7.70M
!tab->need_undo7.70M
)
799
5.56M
    return 0;
800
7.70M
801
2.13M
  
undo = 2.13M
isl_alloc_type2.13M
(tab->mat->ctx, struct isl_tab_undo);
802
2.13M
  if (!undo)
803
0
    return -1;
804
2.13M
  undo->type = type;
805
2.13M
  undo->u = u;
806
2.13M
  undo->next = tab->top;
807
2.13M
  tab->top = undo;
808
2.13M
809
2.13M
  return 0;
810
2.13M
}
811
812
int isl_tab_push_var(struct isl_tab *tab,
813
  enum isl_tab_undo_type type, struct isl_tab_var *var)
814
7.35M
{
815
7.35M
  union isl_tab_undo_val u;
816
7.35M
  if (var->is_row)
817
7.18M
    u.var_index = tab->row_var[var->index];
818
7.35M
  else
819
170k
    u.var_index = tab->col_var[var->index];
820
7.35M
  return push_union(tab, type, u);
821
7.35M
}
822
823
int isl_tab_push(struct isl_tab *tab, enum isl_tab_undo_type type)
824
312k
{
825
312k
  union isl_tab_undo_val u = { 0 };
826
312k
  return push_union(tab, type, u);
827
312k
}
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
742
{
834
742
  int i;
835
742
  union isl_tab_undo_val u;
836
742
837
742
  u.col_var = isl_alloc_array(tab->mat->ctx, int, tab->n_col);
838
742
  if (
tab->n_col && 742
!u.col_var742
)
839
0
    return -1;
840
8.52k
  
for (i = 0; 742
i < tab->n_col8.52k
;
++i7.77k
)
841
7.77k
    u.col_var[i] = tab->col_var[i];
842
742
  return push_union(tab, isl_tab_undo_saved_basis, u);
843
742
}
844
845
int isl_tab_push_callback(struct isl_tab *tab, struct isl_tab_callback *callback)
846
15.2k
{
847
15.2k
  union isl_tab_undo_val u;
848
15.2k
  u.callback = callback;
849
15.2k
  return push_union(tab, isl_tab_undo_callback, u);
850
15.2k
}
851
852
struct isl_tab *isl_tab_init_samples(struct isl_tab *tab)
853
4.76k
{
854
4.76k
  if (!tab)
855
0
    return NULL;
856
4.76k
857
4.76k
  tab->n_sample = 0;
858
4.76k
  tab->n_outside = 0;
859
4.76k
  tab->samples = isl_mat_alloc(tab->mat->ctx, 1, 1 + tab->n_var);
860
4.76k
  if (!tab->samples)
861
0
    goto error;
862
4.76k
  
tab->sample_index = 4.76k
isl_alloc_array4.76k
(tab->mat->ctx, int, 1);
863
4.76k
  if (!tab->sample_index)
864
0
    goto error;
865
4.76k
  return tab;
866
0
error:
867
0
  isl_tab_free(tab);
868
0
  return NULL;
869
4.76k
}
870
871
int isl_tab_add_sample(struct isl_tab *tab, __isl_take isl_vec *sample)
872
7.36k
{
873
7.36k
  if (
!tab || 7.36k
!sample7.36k
)
874
0
    goto error;
875
7.36k
876
7.36k
  
if (7.36k
tab->n_sample + 1 > tab->samples->n_row7.36k
)
{2.28k
877
2.28k
    int *t = isl_realloc_array(tab->mat->ctx,
878
2.28k
          tab->sample_index, int, tab->n_sample + 1);
879
2.28k
    if (!t)
880
0
      goto error;
881
2.28k
    tab->sample_index = t;
882
2.28k
  }
883
7.36k
884
7.36k
  tab->samples = isl_mat_extend(tab->samples,
885
7.36k
        tab->n_sample + 1, tab->samples->n_col);
886
7.36k
  if (!tab->samples)
887
0
    goto error;
888
7.36k
889
7.36k
  isl_seq_cpy(tab->samples->row[tab->n_sample], sample->el, sample->size);
890
7.36k
  isl_vec_free(sample);
891
7.36k
  tab->sample_index[tab->n_sample] = tab->n_sample;
892
7.36k
  tab->n_sample++;
893
7.36k
894
7.36k
  return 0;
895
0
error:
896
0
  isl_vec_free(sample);
897
0
  return -1;
898
7.36k
}
899
900
struct isl_tab *isl_tab_drop_sample(struct isl_tab *tab, int s)
901
4.46k
{
902
4.46k
  if (
s != tab->n_outside4.46k
)
{2.79k
903
2.79k
    int t = tab->sample_index[tab->n_outside];
904
2.79k
    tab->sample_index[tab->n_outside] = tab->sample_index[s];
905
2.79k
    tab->sample_index[s] = t;
906
2.79k
    isl_mat_swap_rows(tab->samples, tab->n_outside, s);
907
2.79k
  }
908
4.46k
  tab->n_outside++;
909
4.46k
  if (
isl_tab_push(tab, isl_tab_undo_drop_sample) < 04.46k
)
{0
910
0
    isl_tab_free(tab);
911
0
    return NULL;
912
0
  }
913
4.46k
914
4.46k
  return tab;
915
4.46k
}
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
19.0k
{
922
19.0k
  union isl_tab_undo_val u;
923
19.0k
924
19.0k
  if (!tab)
925
0
    return -1;
926
19.0k
927
19.0k
  u.n = tab->n_sample;
928
19.0k
  return push_union(tab, isl_tab_undo_saved_samples, u);
929
19.0k
}
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.27M
{
945
1.27M
  struct isl_tab_var *var = isl_tab_var_from_row(tab, row);
946
1.27M
  var->is_redundant = 1;
947
1.27M
  isl_assert(tab->mat->ctx, row >= tab->n_redundant, return -1);
948
1.27M
  
if (1.27M
tab->preserve || 1.27M
tab->need_undo896k
||
tab->row_var[row] >= 0831k
)
{889k
949
889k
    if (
tab->row_var[row] >= 0 && 889k
!var->is_nonneg671k
)
{665k
950
665k
      var->is_nonneg = 1;
951
665k
      if (isl_tab_push_var(tab, isl_tab_undo_nonneg, var) < 0)
952
0
        return -1;
953
665k
    }
954
889k
    
if (889k
row != tab->n_redundant889k
)
955
590k
      swap_rows(tab, row, tab->n_redundant);
956
889k
    tab->n_redundant++;
957
889k
    return isl_tab_push_var(tab, isl_tab_undo_redundant, var);
958
382k
  } else {
959
382k
    if (row != tab->n_row - 1)
960
217k
      swap_rows(tab, row, tab->n_row - 1);
961
382k
    isl_tab_var_from_row(tab, tab->n_row - 1)->index = -1;
962
382k
    tab->n_row--;
963
382k
    return 1;
964
382k
  }
965
1.27M
}
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.23k
{
974
9.23k
  if (!tab)
975
0
    return -1;
976
9.23k
  
if (9.23k
!tab->rational && 9.23k
tab->need_undo9.20k
)
977
9.20k
    
if (9.20k
isl_tab_push(tab, isl_tab_undo_rational) < 09.20k
)
978
0
      return -1;
979
9.23k
  tab->rational = 1;
980
9.23k
  return 0;
981
9.23k
}
982
983
isl_stat isl_tab_mark_empty(struct isl_tab *tab)
984
56.6k
{
985
56.6k
  if (!tab)
986
0
    return isl_stat_error;
987
56.6k
  
if (56.6k
!tab->empty && 56.6k
tab->need_undo56.3k
)
988
46.9k
    
if (46.9k
isl_tab_push(tab, isl_tab_undo_empty) < 046.9k
)
989
0
      return isl_stat_error;
990
56.6k
  tab->empty = 1;
991
56.6k
  return isl_stat_ok;
992
56.6k
}
993
994
int isl_tab_freeze_constraint(struct isl_tab *tab, int con)
995
296k
{
996
296k
  struct isl_tab_var *var;
997
296k
998
296k
  if (!tab)
999
0
    return -1;
1000
296k
1001
296k
  var = &tab->con[con];
1002
296k
  if (var->frozen)
1003
0
    return 0;
1004
296k
  
if (296k
var->index < 0296k
)
1005
19.7k
    return 0;
1006
277k
  var->frozen = 1;
1007
277k
1008
277k
  if (tab->need_undo)
1009
252k
    return isl_tab_push_var(tab, isl_tab_undo_freeze, var);
1010
277k
1011
24.4k
  return 0;
1012
277k
}
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
1.94M
{
1033
1.94M
  int i;
1034
1.94M
  struct isl_mat *mat = tab->mat;
1035
1.94M
  unsigned off = 2 + tab->M;
1036
1.94M
1037
1.94M
  if (!tab->row_sign)
1038
1.93M
    return;
1039
1.94M
1040
17.5k
  
if (17.5k
tab->row_sign[row] == 017.5k
)
1041
13.8k
    return;
1042
3.70k
  
isl_assert3.70k
(mat->ctx, row_sgn > 0, return);3.70k
1043
3.70k
  
isl_assert3.70k
(mat->ctx, tab->row_sign[row] == isl_tab_row_neg, return);3.70k
1044
3.70k
  tab->row_sign[row] = isl_tab_row_pos;
1045
40.7k
  for (i = 0; 
i < tab->n_row40.7k
;
++i37.0k
)
{37.0k
1046
37.0k
    int s;
1047
37.0k
    if (i == row)
1048
3.70k
      continue;
1049
33.3k
    
s = 33.3k
isl_int_sgn33.3k
(mat->row[i][off + col]);
1050
33.3k
    if (!s)
1051
18.5k
      continue;
1052
14.7k
    
if (14.7k
!tab->row_sign[i]14.7k
)
1053
4.72k
      continue;
1054
10.0k
    
if (10.0k
s < 0 && 10.0k
tab->row_sign[i] == isl_tab_row_neg5.37k
)
1055
0
      continue;
1056
10.0k
    
if (10.0k
s > 0 && 10.0k
tab->row_sign[i] == isl_tab_row_pos4.65k
)
1057
4.62k
      continue;
1058
5.39k
    tab->row_sign[i] = isl_tab_row_unknown;
1059
5.39k
  }
1060
3.70k
}
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
1.94M
{
1114
1.94M
  int i, j;
1115
1.94M
  int sgn;
1116
1.94M
  int t;
1117
1.94M
  isl_ctx *ctx;
1118
1.94M
  struct isl_mat *mat = tab->mat;
1119
1.94M
  struct isl_tab_var *var;
1120
1.94M
  unsigned off = 2 + tab->M;
1121
1.94M
1122
1.94M
  ctx = isl_tab_get_ctx(tab);
1123
1.94M
  if (isl_ctx_next_operation(ctx) < 0)
1124
0
    return -1;
1125
1.94M
1126
1.94M
  
isl_int_swap1.94M
(mat->row[row][0], mat->row[row][off + col]);1.94M
1127
1.94M
  sgn = isl_int_sgn(mat->row[row][0]);
1128
1.94M
  if (
sgn < 01.94M
)
{1.01M
1129
1.01M
    isl_int_neg(mat->row[row][0], mat->row[row][0]);
1130
1.01M
    isl_int_neg(mat->row[row][off + col], mat->row[row][off + col]);
1131
1.01M
  } else
1132
7.49M
    
for (j = 0; 935k
j < off - 1 + tab->n_col7.49M
;
++j6.55M
)
{6.55M
1133
6.55M
      if (j == off - 1 + col)
1134
935k
        continue;
1135
5.62M
      
isl_int_neg5.62M
(mat->row[row][1 + j], mat->row[row][1 + j]);5.62M
1136
5.62M
    }
1137
1.94M
  if (
!1.94M
isl_int_is_one1.94M
(mat->row[row][0]))
1138
365k
    isl_seq_normalize(mat->ctx, mat->row[row], off + tab->n_col);
1139
20.1M
  for (i = 0; 
i < tab->n_row20.1M
;
++i18.1M
)
{18.1M
1140
18.1M
    if (i == row)
1141
1.94M
      continue;
1142
16.2M
    
if (16.2M
isl_int_is_zero16.2M
(mat->row[i][off + col]))
1143
10.7M
      continue;
1144
5.47M
    
isl_int_mul5.47M
(mat->row[i][0], mat->row[i][0], mat->row[row][0]);5.47M
1145
62.7M
    for (j = 0; 
j < off - 1 + tab->n_col62.7M
;
++j57.2M
)
{57.2M
1146
57.2M
      if (j == off - 1 + col)
1147
5.47M
        continue;
1148
51.8M
      
isl_int_mul51.8M
(mat->row[i][1 + j],51.8M
1149
51.8M
            mat->row[i][1 + j], mat->row[row][0]);
1150
51.8M
      isl_int_addmul(mat->row[i][1 + j],
1151
51.8M
            mat->row[i][off + col], mat->row[row][1 + j]);
1152
51.8M
    }
1153
5.47M
    isl_int_mul(mat->row[i][off + col],
1154
5.47M
          mat->row[i][off + col], mat->row[row][off + col]);
1155
5.47M
    if (
!5.47M
isl_int_is_one5.47M
(mat->row[i][0]))
1156
2.76M
      isl_seq_normalize(mat->ctx, mat->row[i], off + tab->n_col);
1157
5.47M
  }
1158
1.94M
  t = tab->row_var[row];
1159
1.94M
  tab->row_var[row] = tab->col_var[col];
1160
1.94M
  tab->col_var[col] = t;
1161
1.94M
  var = isl_tab_var_from_row(tab, row);
1162
1.94M
  var->is_row = 1;
1163
1.94M
  var->index = row;
1164
1.94M
  var = var_from_col(tab, col);
1165
1.94M
  var->is_row = 0;
1166
1.94M
  var->index = col;
1167
1.94M
  update_row_sign(tab, row, col, sgn);
1168
1.94M
  if (tab->in_undo)
1169
62.1k
    return 0;
1170
16.4M
  
for (i = tab->n_redundant; 1.88M
i < tab->n_row16.4M
;
++i14.5M
)
{14.5M
1171
14.5M
    if (isl_int_is_zero(mat->row[i][off + col]))
1172
7.92M
      continue;
1173
6.66M
    
if (6.66M
!isl_tab_var_from_row(tab, i)->frozen &&6.66M
1174
6.43M
        
isl_tab_row_is_redundant(tab, i)6.43M
)
{889k
1175
889k
      int redo = isl_tab_mark_redundant(tab, i);
1176
889k
      if (redo < 0)
1177
0
        return -1;
1178
889k
      
if (889k
redo889k
)
1179
72.9k
        --i;
1180
889k
    }
1181
6.66M
  }
1182
1.88M
  return 0;
1183
1.88M
}
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
920k
{
1194
920k
  int r;
1195
920k
  unsigned off = 2 + tab->M;
1196
920k
1197
920k
  if (var->is_row)
1198
782k
    return 0;
1199
920k
1200
137k
  
if (137k
sign == 0137k
)
{15.5k
1201
41.0k
    for (r = tab->n_redundant; 
r < tab->n_row41.0k
;
++r25.5k
)
1202
41.0k
      
if (41.0k
!41.0k
isl_int_is_zero41.0k
(tab->mat->row[r][off+var->index]))
1203
15.5k
        break;
1204
15.5k
    isl_assert(tab->mat->ctx, r < tab->n_row, return -1);
1205
122k
  } else {
1206
122k
    r = pivot_row(tab, NULL, sign, var->index);
1207
122k
    isl_assert(tab->mat->ctx, r >= 0, return -1);
1208
122k
  }
1209
137k
1210
137k
  return isl_tab_pivot(tab, r, var->index);
1211
137k
}
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
758k
{
1253
758k
  int row, col;
1254
758k
1255
758k
  if (max_is_manifestly_unbounded(tab, var))
1256
84.4k
    return 1;
1257
673k
  
if (673k
to_row(tab, var, 1) < 0673k
)
1258
0
    return -2;
1259
1.10M
  
while (673k
!1.10M
isl_int_is_pos1.10M
(tab->mat->row[var->index][1]))
{896k
1260
896k
    find_pivot(tab, var, var, 1, &row, &col);
1261
896k
    if (row == -1)
1262
286k
      
return 286k
isl_int_sgn286k
(tab->mat->row[var->index][1]);
1263
609k
    
if (609k
isl_tab_pivot(tab, row, col) < 0609k
)
1264
0
      return -2;
1265
609k
    
if (609k
!var->is_row609k
) /* manifestly unbounded */
1266
179k
      return 1;
1267
609k
  }
1268
207k
  return 1;
1269
673k
}
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.03M
{
1287
3.03M
  if (!tab->M)
1288
3.03M
    
return 3.03M
isl_int_is_neg3.03M
(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.58M
{
1298
2.58M
  if (!tab->M)
1299
2.58M
    
return 2.58M
isl_int_sgn2.58M
(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
2.87M
{
1313
2.87M
  int row, col;
1314
2.87M
1315
3.03M
  while (
row_is_neg(tab, var->index)3.03M
)
{505k
1316
505k
    find_pivot(tab, var, var, 1, &row, &col);
1317
505k
    if (row == -1)
1318
54.2k
      break;
1319
451k
    
if (451k
isl_tab_pivot(tab, row, col) < 0451k
)
1320
0
      return -2;
1321
451k
    
if (451k
!var->is_row451k
) /* manifestly unbounded */
1322
295k
      return 1;
1323
451k
  }
1324
2.58M
  return row_sgn(tab, var->index);
1325
2.87M
}
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
129k
{
1334
129k
  int row, col;
1335
129k
1336
146k
  while (
isl_int_is_neg146k
(tab->mat->row[var->index][1]))
{138k
1337
138k
    find_pivot(tab, var, var, 1, &row, &col);
1338
138k
    if (row == -1)
1339
59.3k
      break;
1340
79.5k
    
if (79.5k
row == var->index79.5k
) /* manifestly unbounded */
1341
63.1k
      return 1;
1342
16.4k
    
if (16.4k
isl_tab_pivot(tab, row, col) < 016.4k
)
1343
0
      return -1;
1344
16.4k
  }
1345
66.8k
  
return !66.8k
isl_int_is_neg66.8k
(tab->mat->row[var->index][1]);
1346
129k
}
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.2k
{
1367
70.2k
  int row, col;
1368
70.2k
  struct isl_tab_var *pivot_var = NULL;
1369
70.2k
1370
70.2k
  if (min_is_manifestly_unbounded(tab, var))
1371
0
    return -1;
1372
70.2k
  
if (70.2k
!var->is_row70.2k
)
{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.2k
  
if (69.2k
var->is_redundant69.2k
)
1394
0
    return 0;
1395
106k
  
while (69.2k
!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
15.9k
      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
160k
{
1421
160k
  if (
tab->M160k
)
{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
160k
  
return 160k
isl_int_is_neg160k
(tab->mat->row[row][1]) &&
1428
81.7k
         isl_int_abs_ge(tab->mat->row[row][1],
1429
160k
            tab->mat->row[row][0]);
1430
160k
}
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
466k
{
1443
466k
  int row, col;
1444
466k
  struct isl_tab_var *pivot_var;
1445
466k
1446
466k
  if (min_is_manifestly_unbounded(tab, var))
1447
361
    return 1;
1448
466k
  
if (466k
!var->is_row466k
)
{54.5k
1449
54.5k
    col = var->index;
1450
54.5k
    row = pivot_row(tab, NULL, -1, col);
1451
54.5k
    pivot_var = var_from_col(tab, col);
1452
54.5k
    if (isl_tab_pivot(tab, row, col) < 0)
1453
0
      return -1;
1454
54.5k
    
if (54.5k
var->is_redundant54.5k
)
1455
8.63k
      return 0;
1456
45.9k
    
if (45.9k
row_at_most_neg_one(tab, var->index)45.9k
)
{32.2k
1457
32.2k
      if (
var->is_nonneg32.2k
)
{32.2k
1458
32.2k
        if (!pivot_var->is_redundant &&
1459
32.2k
            
pivot_var->index == row32.2k
)
{30.0k
1460
30.0k
          if (isl_tab_pivot(tab, row, col) < 0)
1461
0
            return -1;
1462
30.0k
        } else
1463
2.22k
          
if (2.22k
restore_row(tab, var) < -12.22k
)
1464
0
            return -1;
1465
32.2k
      }
1466
32.2k
      return 1;
1467
32.2k
    }
1468
45.9k
  }
1469
425k
  
if (425k
var->is_redundant425k
)
1470
7.46k
    return 0;
1471
495k
  
do 418k
{495k
1472
495k
    find_pivot(tab, var, var, -1, &row, &col);
1473
495k
    if (
row == var->index495k
)
{225k
1474
225k
      if (
var->is_nonneg && 225k
restore_row(tab, var) < -1194k
)
1475
0
        return -1;
1476
225k
      return 1;
1477
225k
    }
1478
269k
    
if (269k
row == -1269k
)
1479
104k
      return 0;
1480
164k
    pivot_var = var_from_col(tab, col);
1481
164k
    if (isl_tab_pivot(tab, row, col) < 0)
1482
0
      return -1;
1483
164k
    
if (164k
var->is_redundant164k
)
1484
50.0k
      return 0;
1485
114k
  } while (!row_at_most_neg_one(tab, var->index));
1486
37.7k
  
if (37.7k
var->is_nonneg37.7k
)
{29.6k
1487
29.6k
    /* pivot back to non-negative value */
1488
29.6k
    if (
!pivot_var->is_redundant && 29.6k
pivot_var->index == row29.6k
)
1489
28.3k
      
if (28.3k
isl_tab_pivot(tab, row, col) < 028.3k
)
1490
0
        return -1;
1491
29.6k
    
if (29.6k
restore_row(tab, var) < -129.6k
)
1492
0
      return -1;
1493
29.6k
  }
1494
37.7k
  return 1;
1495
37.7k
}
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
446k
{
1502
446k
  int row, col;
1503
446k
  isl_int *r;
1504
446k
1505
446k
  if (max_is_manifestly_unbounded(tab, var))
1506
263k
    return 1;
1507
183k
  
if (183k
to_row(tab, var, 1) < 0183k
)
1508
0
    return -1;
1509
183k
  r = tab->mat->row[var->index];
1510
184k
  while (
isl_int_lt184k
(r[1], r[0]))
{1.57k
1511
1.57k
    find_pivot(tab, var, var, 1, &row, &col);
1512
1.57k
    if (row == -1)
1513
228
      
return 228
isl_int_ge228
(r[1], r[0]);
1514
1.34k
    
if (1.34k
row == var->index1.34k
) /* manifestly unbounded */
1515
90
      return 1;
1516
1.25k
    
if (1.25k
isl_tab_pivot(tab, row, col) < 01.25k
)
1517
0
      return -1;
1518
1.25k
  }
1519
183k
  return 1;
1520
183k
}
1521
1522
static void swap_cols(struct isl_tab *tab, int col1, int col2)
1523
337k
{
1524
337k
  int t;
1525
337k
  unsigned off = 2 + tab->M;
1526
337k
  t = tab->col_var[col1];
1527
337k
  tab->col_var[col1] = tab->col_var[col2];
1528
337k
  tab->col_var[col2] = t;
1529
337k
  var_from_col(tab, col1)->index = col1;
1530
337k
  var_from_col(tab, col2)->index = col2;
1531
337k
  tab->mat = isl_mat_swap_cols(tab->mat, off + col1, off + col2);
1532
337k
}
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
493k
{
1548
493k
  var_from_col(tab, col)->is_zero = 1;
1549
493k
  if (
tab->need_undo493k
)
{41.7k
1550
41.7k
    if (isl_tab_push_var(tab, isl_tab_undo_zero,
1551
41.7k
              var_from_col(tab, col)) < 0)
1552
0
      return -1;
1553
41.7k
    
if (41.7k
col != tab->n_dead41.7k
)
1554
16.0k
      swap_cols(tab, col, tab->n_dead);
1555
41.7k
    tab->n_dead++;
1556
41.7k
    return 0;
1557
452k
  } else {
1558
452k
    if (col != tab->n_col - 1)
1559
321k
      swap_cols(tab, col, tab->n_col - 1);
1560
452k
    var_from_col(tab, tab->n_col - 1)->index = -1;
1561
452k
    tab->n_col--;
1562
452k
    return 1;
1563
452k
  }
1564
493k
}
1565
1566
static int row_is_manifestly_non_integral(struct isl_tab *tab, int row)
1567
1.36M
{
1568
1.36M
  unsigned off = 2 + tab->M;
1569
1.36M
1570
1.36M
  if (
tab->M && 1.36M
!0
isl_int_eq0
(tab->mat->row[row][2],
1571
1.36M
          tab->mat->row[row][0]))
1572
0
    return 0;
1573
1.36M
  
if (1.36M
isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,1.36M
1574
1.36M
            tab->n_col - tab->n_dead) != -1)
1575
120k
    return 0;
1576
1.36M
1577
1.24M
  
return !1.24M
isl_int_is_divisible_by1.24M
(tab->mat->row[row][1],
1578
1.36M
          tab->mat->row[row][0]);
1579
1.36M
}
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
286k
{
1586
286k
  int i;
1587
286k
1588
286k
  if (tab->empty)
1589
0
    return 1;
1590
286k
  
if (286k
tab->rational286k
)
1591
11.2k
    return 0;
1592
286k
1593
2.95M
  
for (i = 0; 275k
i < tab->n_var2.95M
;
++i2.67M
)
{2.67M
1594
2.67M
    if (!tab->var[i].is_row)
1595
1.31M
      continue;
1596
1.36M
    
if (1.36M
row_is_manifestly_non_integral(tab, tab->var[i].index)1.36M
)
1597
16
      return 1;
1598
1.36M
  }
1599
275k
1600
275k
  return 0;
1601
275k
}
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
286k
{
1625
286k
  int j;
1626
286k
  struct isl_mat *mat = tab->mat;
1627
286k
  unsigned off = 2 + tab->M;
1628
286k
1629
286k
  if (!var->is_nonneg)
1630
0
    isl_die(isl_tab_get_ctx(tab), isl_error_internal,
1631
286k
      "expecting non-negative variable",
1632
286k
      return isl_stat_error);
1633
286k
  var->is_zero = 1;
1634
286k
  if (
!temp_var && 286k
tab->need_undo276k
)
1635
325
    
if (325
isl_tab_push_var(tab, isl_tab_undo_zero, var) < 0325
)
1636
0
      return isl_stat_error;
1637
1.95M
  
for (j = tab->n_dead; 286k
j < tab->n_col1.95M
;
++j1.66M
)
{1.66M
1638
1.66M
    int recheck;
1639
1.66M
    if (isl_int_is_zero(mat->row[var->index][off + j]))
1640
1.40M
      continue;
1641
264k
    
if (264k
isl_int_is_pos264k
(mat->row[var->index][off + j]))
1642
0
      isl_die(isl_tab_get_ctx(tab), isl_error_internal,
1643
264k
        "row cannot have positive coefficients",
1644
264k
        return isl_stat_error);
1645
264k
    recheck = isl_tab_kill_col(tab, j);
1646
264k
    if (recheck < 0)
1647
0
      return isl_stat_error;
1648
264k
    
if (264k
recheck264k
)
1649
254k
      --j;
1650
264k
  }
1651
286k
  
if (286k
!temp_var && 286k
isl_tab_mark_redundant(tab, var->index) < 0276k
)
1652
0
    return isl_stat_error;
1653
286k
  
if (286k
tab_is_manifestly_empty(tab) && 286k
isl_tab_mark_empty(tab) < 016
)
1654
0
    return isl_stat_error;
1655
286k
  return isl_stat_ok;
1656
286k
}
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.10M
{
1666
3.10M
  int r;
1667
3.10M
1668
3.10M
  isl_assert(tab->mat->ctx, tab->n_row < tab->mat->n_row, return -1);
1669
3.10M
  
isl_assert3.10M
(tab->mat->ctx, tab->n_con < tab->max_con, return -1);3.10M
1670
3.10M
1671
3.10M
  r = tab->n_con;
1672
3.10M
  tab->con[r].index = tab->n_row;
1673
3.10M
  tab->con[r].is_row = 1;
1674
3.10M
  tab->con[r].is_nonneg = 0;
1675
3.10M
  tab->con[r].is_zero = 0;
1676
3.10M
  tab->con[r].is_redundant = 0;
1677
3.10M
  tab->con[r].frozen = 0;
1678
3.10M
  tab->con[r].negated = 0;
1679
3.10M
  tab->row_var[tab->n_row] = ~r;
1680
3.10M
1681
3.10M
  tab->n_row++;
1682
3.10M
  tab->n_con++;
1683
3.10M
  if (isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->con[r]) < 0)
1684
0
    return -1;
1685
3.10M
1686
3.10M
  return r;
1687
3.10M
}
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
4.42k
{
1696
4.42k
  int i;
1697
4.42k
1698
4.42k
  if (tab->n_var >= tab->max_var)
1699
0
    isl_die(isl_tab_get_ctx(tab), isl_error_internal,
1700
4.42k
      "not enough room for new variable", return -1);
1701
4.42k
  
if (4.42k
first > tab->n_var4.42k
)
1702
0
    isl_die(isl_tab_get_ctx(tab), isl_error_internal,
1703
4.42k
      "invalid initial position", return -1);
1704
4.42k
1705
4.51k
  
for (i = tab->n_var - 1; 4.42k
i >= first4.51k
;
--i97
)
{97
1706
97
    tab->var[i + 1] = tab->var[i];
1707
97
    if (tab->var[i + 1].is_row)
1708
75
      tab->row_var[tab->var[i + 1].index]++;
1709
97
    else
1710
22
      tab->col_var[tab->var[i + 1].index]++;
1711
97
  }
1712
4.42k
1713
4.42k
  tab->n_var++;
1714
4.42k
1715
4.42k
  return 0;
1716
4.42k
}
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
2.73k
{
1725
2.73k
  int i;
1726
2.73k
1727
2.73k
  if (first >= tab->n_var)
1728
0
    isl_die(isl_tab_get_ctx(tab), isl_error_internal,
1729
2.73k
      "invalid initial position", return -1);
1730
2.73k
1731
2.73k
  tab->n_var--;
1732
2.73k
1733
2.79k
  for (i = first; 
i < tab->n_var2.79k
;
++i61
)
{61
1734
61
    tab->var[i] = tab->var[i + 1];
1735
61
    if (tab->var[i + 1].is_row)
1736
55
      tab->row_var[tab->var[i].index]--;
1737
61
    else
1738
6
      tab->col_var[tab->var[i].index]--;
1739
61
  }
1740
2.73k
1741
2.73k
  return 0;
1742
2.73k
}
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
4.42k
{
1750
4.42k
  int i;
1751
4.42k
  unsigned off = 2 + tab->M;
1752
4.42k
1753
4.42k
  isl_assert(tab->mat->ctx, tab->n_col < tab->mat->n_col, return -1);
1754
4.42k
1755
4.42k
  
if (4.42k
var_insert_entry(tab, r) < 04.42k
)
1756
0
    return -1;
1757
4.42k
1758
4.42k
  tab->var[r].index = tab->n_col;
1759
4.42k
  tab->var[r].is_row = 0;
1760
4.42k
  tab->var[r].is_nonneg = 0;
1761
4.42k
  tab->var[r].is_zero = 0;
1762
4.42k
  tab->var[r].is_redundant = 0;
1763
4.42k
  tab->var[r].frozen = 0;
1764
4.42k
  tab->var[r].negated = 0;
1765
4.42k
  tab->col_var[tab->n_col] = r;
1766
4.42k
1767
27.3k
  for (i = 0; 
i < tab->n_row27.3k
;
++i22.9k
)
1768
22.9k
    isl_int_set_si(tab->mat->row[i][off + tab->n_col], 0);
1769
4.42k
1770
4.42k
  tab->n_col++;
1771
4.42k
  if (isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->var[r]) < 0)
1772
0
    return -1;
1773
4.42k
1774
4.42k
  return r;
1775
4.42k
}
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.09M
{
1812
3.09M
  int i;
1813
3.09M
  int r;
1814
3.09M
  isl_int *row;
1815
3.09M
  isl_int a, b;
1816
3.09M
  unsigned off = 2 + tab->M;
1817
3.09M
1818
3.09M
  r = isl_tab_allocate_con(tab);
1819
3.09M
  if (r < 0)
1820
0
    return -1;
1821
3.09M
1822
3.09M
  
isl_int_init3.09M
(a);3.09M
1823
3.09M
  isl_int_init(b);
1824
3.09M
  row = tab->mat->row[tab->con[r].index];
1825
3.09M
  isl_int_set_si(row[0], 1);
1826
3.09M
  isl_int_set(row[1], line[0]);
1827
3.09M
  isl_seq_clr(row + 2, tab->M + tab->n_col);
1828
24.8M
  for (i = 0; 
i < tab->n_var24.8M
;
++i21.7M
)
{21.7M
1829
21.7M
    if (tab->var[i].is_zero)
1830
0
      continue;
1831
21.7M
    
if (21.7M
tab->var[i].is_row21.7M
)
{4.75M
1832
4.75M
      isl_int_lcm(a,
1833
4.75M
        row[0], tab->mat->row[tab->var[i].index][0]);
1834
4.75M
      isl_int_swap(a, row[0]);
1835
4.75M
      isl_int_divexact(a, row[0], a);
1836
4.75M
      isl_int_divexact(b,
1837
4.75M
        row[0], tab->mat->row[tab->var[i].index][0]);
1838
4.75M
      isl_int_mul(b, b, line[1 + i]);
1839
4.75M
      isl_seq_combine(row + 1, a, row + 1,
1840
4.75M
          b, tab->mat->row[tab->var[i].index] + 1,
1841
4.75M
          1 + tab->M + tab->n_col);
1842
4.75M
    } else
1843
16.9M
      isl_int_addmul(row[off + tab->var[i].index],
1844
21.7M
              line[1 + i], row[0]);
1845
21.7M
    if (
tab->M && 21.7M
i >= tab->n_param217k
&&
i < tab->n_var - tab->n_div93.2k
)
1846
90.2k
      isl_int_submul(row[2], line[1 + i], row[0]);
1847
21.7M
  }
1848
3.09M
  isl_seq_normalize(tab->mat->ctx, row, off + tab->n_col);
1849
3.09M
  isl_int_clear(a);
1850
3.09M
  isl_int_clear(b);
1851
3.09M
1852
3.09M
  if (tab->row_sign)
1853
24.3k
    tab->row_sign[tab->con[r].index] = isl_tab_row_unknown;
1854
3.09M
1855
3.09M
  return r;
1856
3.09M
}
1857
1858
static isl_stat drop_row(struct isl_tab *tab, int row)
1859
694k
{
1860
694k
  isl_assert(tab->mat->ctx, ~tab->row_var[row] == tab->n_con - 1,
1861
694k
    return isl_stat_error);
1862
694k
  
if (694k
row != tab->n_row - 1694k
)
1863
80.3k
    swap_rows(tab, row, tab->n_row - 1);
1864
694k
  tab->n_row--;
1865
694k
  tab->n_con--;
1866
694k
  return isl_stat_ok;
1867
694k
}
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
2.73k
{
1877
2.73k
  int var;
1878
2.73k
1879
2.73k
  var = tab->col_var[col];
1880
2.73k
  if (col != tab->n_col - 1)
1881
632
    swap_cols(tab, col, tab->n_col - 1);
1882
2.73k
  tab->n_col--;
1883
2.73k
  if (var_drop_entry(tab, var) < 0)
1884
0
    return isl_stat_error;
1885
2.73k
  return isl_stat_ok;
1886
2.73k
}
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.37M
{
1896
2.37M
  int r;
1897
2.37M
  int sgn;
1898
2.37M
  isl_int cst;
1899
2.37M
1900
2.37M
  if (!tab)
1901
0
    return isl_stat_error;
1902
2.37M
  
if (2.37M
tab->bmap2.37M
)
{248k
1903
248k
    struct isl_basic_map *bmap = tab->bmap;
1904
248k
1905
248k
    isl_assert(tab->mat->ctx, tab->n_eq == bmap->n_eq,
1906
248k
      return isl_stat_error);
1907
248k
    
isl_assert248k
(tab->mat->ctx,248k
1908
248k
          tab->n_con == bmap->n_eq + bmap->n_ineq,
1909
248k
          return isl_stat_error);
1910
248k
    tab->bmap = isl_basic_map_add_ineq(tab->bmap, ineq);
1911
248k
    if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1912
0
      return isl_stat_error;
1913
248k
    
if (248k
!tab->bmap248k
)
1914
0
      return isl_stat_error;
1915
248k
  }
1916
2.37M
  
if (2.37M
tab->cone2.37M
)
{2.17k
1917
2.17k
    isl_int_init(cst);
1918
2.17k
    isl_int_set_si(cst, 0);
1919
2.17k
    isl_int_swap(ineq[0], cst);
1920
2.17k
  }
1921
2.37M
  r = isl_tab_add_row(tab, ineq);
1922
2.37M
  if (
tab->cone2.37M
)
{2.17k
1923
2.17k
    isl_int_swap(ineq[0], cst);
1924
2.17k
    isl_int_clear(cst);
1925
2.17k
  }
1926
2.37M
  if (r < 0)
1927
0
    return isl_stat_error;
1928
2.37M
  tab->con[r].is_nonneg = 1;
1929
2.37M
  if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1930
0
    return isl_stat_error;
1931
2.37M
  
if (2.37M
isl_tab_row_is_redundant(tab, tab->con[r].index)2.37M
)
{87.8k
1932
87.8k
    if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1933
0
      return isl_stat_error;
1934
87.8k
    return isl_stat_ok;
1935
87.8k
  }
1936
2.37M
1937
2.28M
  sgn = restore_row(tab, &tab->con[r]);
1938
2.28M
  if (sgn < -1)
1939
0
    return isl_stat_error;
1940
2.28M
  
if (2.28M
sgn < 02.28M
)
1941
54.2k
    return isl_tab_mark_empty(tab);
1942
2.23M
  
if (2.23M
tab->con[r].is_row && 2.23M
isl_tab_row_is_redundant(tab, tab->con[r].index)1.93M
)
1943
0
    
if (0
isl_tab_mark_redundant(tab, tab->con[r].index) < 00
)
1944
0
      return isl_stat_error;
1945
2.23M
  return isl_stat_ok;
1946
2.23M
}
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
31.6k
{
1954
31.6k
  int i;
1955
31.6k
  int row, col;
1956
31.6k
  unsigned off = 2 + tab->M;
1957
31.6k
1958
31.6k
  if (!var->is_row)
1959
93
    return 0;
1960
31.6k
1961
38.5k
  
while (31.5k
isl_int_is_pos38.5k
(tab->mat->row[var->index][1]))
{35.8k
1962
35.8k
    find_pivot(tab, var, NULL, -1, &row, &col);
1963
35.8k
    isl_assert(tab->mat->ctx, row != -1, return -1);
1964
35.8k
    
if (35.8k
isl_tab_pivot(tab, row, col) < 035.8k
)
1965
0
      return -1;
1966
35.8k
    
if (35.8k
!var->is_row35.8k
)
1967
28.8k
      return 0;
1968
35.8k
  }
1969
31.5k
1970
4.58k
  
for (i = tab->n_dead; 2.69k
i < tab->n_col4.58k
;
++i1.89k
)
1971
4.58k
    
if (4.58k
!4.58k
isl_int_is_zero4.58k
(tab->mat->row[var->index][off + i]))
1972
2.69k
      break;
1973
2.69k
1974
2.69k
  isl_assert(tab->mat->ctx, i < tab->n_col, return -1);
1975
2.69k
  
if (2.69k
isl_tab_pivot(tab, var->index, i) < 02.69k
)
1976
0
    return -1;
1977
2.69k
1978
2.69k
  return 0;
1979
2.69k
}
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
182k
{
1991
182k
  int i;
1992
182k
  int r;
1993
182k
1994
182k
  if (!tab)
1995
0
    return NULL;
1996
182k
  r = isl_tab_add_row(tab, eq);
1997
182k
  if (r < 0)
1998
0
    goto error;
1999
182k
2000
182k
  r = tab->con[r].index;
2001
182k
  i = isl_seq_first_non_zero(tab->mat->row[r] + 2 + tab->M + tab->n_dead,
2002
182k
          tab->n_col - tab->n_dead);
2003
182k
  isl_assert(tab->mat->ctx, i >= 0, goto error);
2004
182k
  i += tab->n_dead;
2005
182k
  if (isl_tab_pivot(tab, r, i) < 0)
2006
0
    goto error;
2007
182k
  
if (182k
isl_tab_kill_col(tab, i) < 0182k
)
2008
0
    goto error;
2009
182k
  tab->n_eq++;
2010
182k
2011
182k
  return tab;
2012
0
error:
2013
0
  isl_tab_free(tab);
2014
0
  return NULL;
2015
182k
}
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
71.8k
{
2022
0
  return tab->M && 
!0
isl_int_is_zero0
(tab->mat->row[row][2]);
2023
71.8k
}
2024
2025
static int row_is_manifestly_zero(struct isl_tab *tab, int row)
2026
41.2k
{
2027
41.2k
  unsigned off = 2 + tab->M;
2028
41.2k
2029
41.2k
  if (
!41.2k
isl_int_is_zero41.2k
(tab->mat->row[row][1]))
2030
29.1k
    return 0;
2031
12.1k
  
if (12.1k
row_is_big(tab, row)12.1k
)
2032
0
    return 0;
2033
12.1k
  return isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
2034
12.1k
          tab->n_col - tab->n_dead) == -1;
2035
12.1k
}
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
30.8k
{
2044
30.8k
  struct isl_tab_var *var;
2045
30.8k
  int r;
2046
30.8k
2047
30.8k
  if (!tab)
2048
0
    return -1;
2049
30.8k
  r = isl_tab_add_row(tab, eq);
2050
30.8k
  if (r < 0)
2051
0
    return -1;
2052
30.8k
2053
30.8k
  var = &tab->con[r];
2054
30.8k
  r = var->index;
2055
30.8k
  if (
row_is_manifestly_zero(tab, r)30.8k
)
{1.44k
2056
1.44k
    var->is_zero = 1;
2057
1.44k
    if (isl_tab_mark_redundant(tab, r) < 0)
2058
0
      return -1;
2059
1.44k
    return 0;
2060
1.44k
  }
2061
30.8k
2062
29.4k
  
if (29.4k
isl_int_is_neg29.4k
(tab->mat->row[r][1]))
{9.33k
2063
9.33k
    isl_seq_neg(tab->mat->row[r] + 1, tab->mat->row[r] + 1,
2064
9.33k
          1 + tab->n_col);
2065
9.33k
    var->negated = 1;
2066
9.33k
  }
2067
29.4k
  var->is_nonneg = 1;
2068
29.4k
  if (to_col(tab, var) < 0)
2069
0
    return -1;
2070
29.4k
  var->is_nonneg = 0;
2071
29.4k
  if (isl_tab_kill_col(tab, var->index) < 0)
2072
0
    return -1;
2073
29.4k
2074
29.4k
  return 0;
2075
29.4k
}
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
1.60k
{
2085
1.60k
  int r;
2086
1.60k
  isl_int *row;
2087
1.60k
2088
1.60k
  r = isl_tab_allocate_con(tab);
2089
1.60k
  if (r < 0)
2090
0
    return -1;
2091
1.60k
2092
1.60k
  row = tab->mat->row[tab->con[r].index];
2093
1.60k
  isl_seq_clr(row + 1, 1 + tab->M + tab->n_col);
2094
1.60k
  isl_int_set_si(row[0], 1);
2095
1.60k
2096
1.60k
  return r;
2097
1.60k
}
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
10.3k
{
2108
10.3k
  struct isl_tab_undo *snap = NULL;
2109
10.3k
  struct isl_tab_var *var;
2110
10.3k
  int r;
2111
10.3k
  int row;
2112
10.3k
  int sgn;
2113
10.3k
  isl_int cst;
2114
10.3k
2115
10.3k
  if (!tab)
2116
0
    return -1;
2117
10.3k
  
isl_assert10.3k
(tab->mat->ctx, !tab->M, return -1);10.3k
2118
10.3k
2119
10.3k
  
if (10.3k
tab->need_undo10.3k
)
2120
9.89k
    snap = isl_tab_snap(tab);
2121
10.3k
2122
10.3k
  if (
tab->cone10.3k
)
{681
2123
681
    isl_int_init(cst);
2124
681
    isl_int_set_si(cst, 0);
2125
681
    isl_int_swap(eq[0], cst);
2126
681
  }
2127
10.3k
  r = isl_tab_add_row(tab, eq);
2128
10.3k
  if (
tab->cone10.3k
)
{681
2129
681
    isl_int_swap(eq[0], cst);
2130
681
    isl_int_clear(cst);
2131
681
  }
2132
10.3k
  if (r < 0)
2133
0
    return -1;
2134
10.3k
2135
10.3k
  var = &tab->con[r];
2136
10.3k
  row = var->index;
2137
10.3k
  if (
row_is_manifestly_zero(tab, row)10.3k
)
{8.17k
2138
8.17k
    if (snap)
2139
8.11k
      return isl_tab_rollback(tab, snap);
2140
54
    return drop_row(tab, row);
2141
8.17k
  }
2142
10.3k
2143
2.20k
  
if (2.20k
tab->bmap2.20k
)
{1.60k
2144
1.60k
    tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
2145
1.60k
    if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
2146
0
      return -1;
2147
1.60k
    isl_seq_neg(eq, eq, 1 + tab->n_var);
2148
1.60k
    tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
2149
1.60k
    isl_seq_neg(eq, eq, 1 + tab->n_var);
2150
1.60k
    if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
2151
0
      return -1;
2152
1.60k
    
if (1.60k
!tab->bmap1.60k
)
2153
0
      return -1;
2154
1.60k
    
if (1.60k
add_zero_row(tab) < 01.60k
)
2155
0
      return -1;
2156
1.60k
  }
2157
2.20k
2158
2.20k
  
sgn = 2.20k
isl_int_sgn2.20k
(tab->mat->row[row][1]);
2159
2.20k
2160
2.20k
  if (
sgn > 02.20k
)
{260
2161
260
    isl_seq_neg(tab->mat->row[row] + 1, tab->mat->row[row] + 1,
2162
260
          1 + tab->n_col);
2163
260
    var->negated = 1;
2164
260
    sgn = -1;
2165
260
  }
2166
2.20k
2167
2.20k
  if (
sgn < 02.20k
)
{1.35k
2168
1.35k
    sgn = sign_of_max(tab, var);
2169
1.35k
    if (sgn < -1)
2170
0
      return -1;
2171
1.35k
    
if (1.35k
sgn < 01.35k
)
{0
2172
0
      if (isl_tab_mark_empty(tab) < 0)
2173
0
        return -1;
2174
0
      return 0;
2175
0
    }
2176
1.35k
  }
2177
2.20k
2178
2.20k
  var->is_nonneg = 1;
2179
2.20k
  if (to_col(tab, var) < 0)
2180
0
    return -1;
2181
2.20k
  var->is_nonneg = 0;
2182
2.20k
  if (isl_tab_kill_col(tab, var->index) < 0)
2183
0
    return -1;
2184
2.20k
2185
2.20k
  return 0;
2186
2.20k
}
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
1.26k
{
2200
1.26k
  unsigned total;
2201
1.26k
  unsigned div_pos;
2202
1.26k
  struct isl_vec *ineq;
2203
1.26k
2204
1.26k
  if (!bmap)
2205
0
    return NULL;
2206
1.26k
2207
1.26k
  total = isl_basic_map_total_dim(bmap);
2208
1.26k
  div_pos = 1 + total - bmap->n_div + div;
2209
1.26k
2210
1.26k
  ineq = isl_vec_alloc(bmap->ctx, 1 + total);
2211
1.26k
  if (!ineq)
2212
0
    return NULL;
2213
1.26k
2214
1.26k
  isl_seq_cpy(ineq->el, bmap->div[div] + 1, 1 + total);
2215
1.26k
  isl_int_neg(ineq->el[div_pos], bmap->div[div][0]);
2216
1.26k
  return ineq;
2217
1.26k
}
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
1.26k
{
2237
1.26k
  unsigned total;
2238
1.26k
  unsigned div_pos;
2239
1.26k
  struct isl_vec *ineq;
2240
1.26k
2241
1.26k
  total = isl_basic_map_total_dim(tab->bmap);
2242
1.26k
  div_pos = 1 + total - tab->bmap->n_div + div;
2243
1.26k
2244
1.26k
  ineq = ineq_for_div(tab->bmap, div);
2245
1.26k
  if (!ineq)
2246
0
    goto error;
2247
1.26k
2248
1.26k
  
if (1.26k
add_ineq1.26k
)
{421
2249
421
    if (add_ineq(user, ineq->el) < 0)
2250
0
      goto error;
2251
843
  } else {
2252
843
    if (isl_tab_add_ineq(tab, ineq->el) < 0)
2253
0
      goto error;
2254
843
  }
2255
1.26k
2256
1.26k
  isl_seq_neg(ineq->el, tab->bmap->div[div] + 1, 1 + total);
2257
1.26k
  isl_int_set(ineq->el[div_pos], tab->bmap->div[div][0]);
2258
1.26k
  isl_int_add(ineq->el[0], ineq->el[0], ineq->el[div_pos]);
2259
1.26k
  isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
2260
1.26k
2261
1.26k
  if (
add_ineq1.26k
)
{421
2262
421
    if (add_ineq(user, ineq->el) < 0)
2263
0
      goto error;
2264
843
  } else {
2265
843
    if (isl_tab_add_ineq(tab, ineq->el) < 0)
2266
0
      goto error;
2267
843
  }
2268
1.26k
2269
1.26k
  isl_vec_free(ineq);
2270
1.26k
2271
1.26k
  return 0;
2272
0
error:
2273
0
  isl_vec_free(ineq);
2274
0
  return -1;
2275
1.26k
}
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
1.26k
{
2285
1.26k
  int i;
2286
1.26k
2287
1.26k
  if (tab->M)
2288
0
    return 1;
2289
1.26k
2290
1.26k
  
if (1.26k
isl_int_is_neg1.26k
(div->el[1]))
2291
139
    return 0;
2292
1.26k
2293
2.14k
  
for (i = 0; 1.12k
i < tab->n_var2.14k
;
++i1.02k
)
{2.03k
2294
2.03k
    if (isl_int_is_neg(div->el[2 + i]))
2295
265
      return 0;
2296
1.76k
    
if (1.76k
isl_int_is_zero1.76k
(div->el[2 + i]))
2297
886
      continue;
2298
883
    
if (883
!tab->var[i].is_nonneg883
)
2299
746
      return 0;
2300
883
  }
2301
1.12k
2302
114
  return 1;
2303
1.12k
}
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
1.26k
{
2319
1.26k
  int r;
2320
1.26k
  int nonneg;
2321
1.26k
  int n_div, o_div;
2322
1.26k
2323
1.26k
  if (
!tab || 1.26k
!div1.26k
)
2324
0
    return -1;
2325
1.26k
2326
1.26k
  
if (1.26k
div->size != 1 + 1 + tab->n_var1.26k
)
2327
0
    isl_die(isl_tab_get_ctx(tab), isl_error_invalid,
2328
1.26k
      "unexpected size", return -1);
2329
1.26k
2330
1.26k
  
isl_assert1.26k
(tab->mat->ctx, tab->bmap, return -1);1.26k
2331
1.26k
  n_div = isl_basic_map_dim(tab->bmap, isl_dim_div);
2332
1.26k
  o_div = tab->n_var - n_div;
2333
1.26k
  if (
pos < o_div || 1.26k
pos > tab->n_var1.26k
)
2334
0
    isl_die(isl_tab_get_ctx(tab), isl_error_invalid,
2335
1.26k
      "invalid position", return -1);
2336
1.26k
2337
1.26k
  nonneg = div_is_nonneg(tab, div);
2338
1.26k
2339
1.26k
  if (isl_tab_extend_cons(tab, 3) < 0)
2340
0
    return -1;
2341
1.26k
  
if (1.26k
isl_tab_extend_vars(tab, 1) < 01.26k
)
2342
0
    return -1;
2343
1.26k
  r = isl_tab_insert_var(tab, pos);
2344
1.26k
  if (r < 0)
2345
0
    return -1;
2346
1.26k
2347
1.26k
  
if (1.26k
nonneg1.26k
)
2348
114
    tab->var[r].is_nonneg = 1;
2349
1.26k
2350
1.26k
  tab->bmap = isl_basic_map_insert_div(tab->bmap, pos - o_div, div);
2351
1.26k
  if (!tab->bmap)
2352
0
    return -1;
2353
1.26k
  
if (1.26k
isl_tab_push_var(tab, isl_tab_undo_bmap_div, &tab->var[r]) < 01.26k
)
2354
0
    return -1;
2355
1.26k
2356
1.26k
  
if (1.26k
add_div_constraints(tab, pos - o_div, add_ineq, user) < 01.26k
)
2357
0
    return -1;
2358
1.26k
2359
1.26k
  return r;
2360
1.26k
}
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
843
{
2367
843
  if (!tab)
2368
0
    return -1;
2369
843
  return isl_tab_insert_div(tab, tab->n_var, div, NULL, NULL);
2370
843
}
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
485k
{
2380
485k
  int i;
2381
485k
  struct isl_tab *tab;
2382
485k
2383
485k
  if (!bmap)
2384
0
    return NULL;
2385
485k
  tab = isl_tab_alloc(bmap->ctx,
2386
485k
          isl_basic_map_total_dim(bmap) + bmap->n_ineq + 1,
2387
485k
          isl_basic_map_total_dim(bmap), 0);
2388
485k
  if (!tab)
2389
0
    return NULL;
2390
485k
  tab->preserve = track;
2391
485k
  tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
2392
485k
  if (
ISL_F_ISSET485k
(bmap, ISL_BASIC_MAP_EMPTY))
{15
2393
15
    if (isl_tab_mark_empty(tab) < 0)
2394
0
      goto error;
2395
15
    goto done;
2396
15
  }
2397
667k
  
for (i = 0; 485k
i < bmap->n_eq667k
;
++i181k
)
{181k
2398
181k
    tab = add_eq(tab, bmap->eq[i]);
2399
181k
    if (!tab)
2400
0
      return tab;
2401
181k
  }
2402
2.58M
  
for (i = 0; 485k
i < bmap->n_ineq2.58M
;
++i2.09M
)
{2.10M
2403
2.10M
    if (isl_tab_add_ineq(tab, bmap->ineq[i]) < 0)
2404
0
      goto error;
2405
2.10M
    
if (2.10M
tab->empty2.10M
)
2406
5.31k
      goto done;
2407
2.10M
  }
2408
485k
done:
2409
485k
  if (
track && 485k
isl_tab_track_bmap(tab, isl_basic_map_copy(bmap)) < 0113k
)
2410
0
    goto error;
2411
485k
  return tab;
2412
0
error:
2413
0
  isl_tab_free(tab);
2414
0
  return NULL;
2415
485k
}
2416
2417
__isl_give struct isl_tab *isl_tab_from_basic_set(
2418
  __isl_keep isl_basic_set *bset, int track)
2419
164k
{
2420
164k
  return isl_tab_from_basic_map(bset, track);
2421
164k
}
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
2.71k
{
2428
2.71k
  isl_int cst;
2429
2.71k
  int i;
2430
2.71k
  struct isl_tab *tab;
2431
2.71k
  unsigned offset = 0;
2432
2.71k
2433
2.71k
  if (!bset)
2434
0
    return NULL;
2435
2.71k
  
if (2.71k
parametric2.71k
)
2436
2.05k
    offset = isl_basic_set_dim(bset, isl_dim_param);
2437
2.71k
  tab = isl_tab_alloc(bset->ctx, bset->n_eq + bset->n_ineq,
2438
2.71k
        isl_basic_set_total_dim(bset) - offset, 0);
2439
2.71k
  if (!tab)
2440
0
    return NULL;
2441
2.71k
  
tab->rational = 2.71k
ISL_F_ISSET2.71k
(bset, ISL_BASIC_SET_RATIONAL);
2442
2.71k
  tab->cone = 1;
2443
2.71k
2444
2.71k
  isl_int_init(cst);
2445
2.71k
  isl_int_set_si(cst, 0);
2446
4.45k
  for (i = 0; 
i < bset->n_eq4.45k
;
++i1.73k
)
{1.73k
2447
1.73k
    isl_int_swap(bset->eq[i][offset], cst);
2448
1.73k
    if (
offset > 01.73k
)
{475
2449
475
      if (isl_tab_add_eq(tab, bset->eq[i] + offset) < 0)
2450
0
        goto error;
2451
475
    } else
2452
1.25k
      tab = add_eq(tab, bset->eq[i]);
2453
1.73k
    
isl_int_swap1.73k
(bset->eq[i][offset], cst);1.73k
2454
1.73k
    if (!tab)
2455
0
      goto done;
2456
1.73k
  }
2457
11.9k
  
for (i = 0; 2.71k
i < bset->n_ineq11.9k
;
++i9.27k
)
{9.27k
2458
9.27k
    int r;
2459
9.27k
    isl_int_swap(bset->ineq[i][offset], cst);
2460
9.27k
    r = isl_tab_add_row(tab, bset->ineq[i] + offset);
2461
9.27k
    isl_int_swap(bset->ineq[i][offset], cst);
2462
9.27k
    if (r < 0)
2463
0
      goto error;
2464
9.27k
    tab->con[r].is_nonneg = 1;
2465
9.27k
    if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
2466
0
      goto error;
2467
9.27k
  }
2468
2.71k
done:
2469
2.71k
  isl_int_clear(cst);
2470
2.71k
  return tab;
2471
0
error:
2472
0
  isl_int_clear(cst);
2473
0
  isl_tab_free(tab);
2474
0
  return NULL;
2475
2.71k
}
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.05k
{
2482
2.05k
  int i;
2483
2.05k
2484
2.05k
  if (!tab)
2485
0
    return isl_bool_error;
2486
2.05k
  
if (2.05k
tab->empty2.05k
)
2487
0
    return isl_bool_true;
2488
2.05k
  
if (2.05k
tab->n_dead == tab->n_col2.05k
)
2489
389
    return isl_bool_true;
2490
2.05k
2491
2.50k
  
for (;;) 1.67k
{2.50k
2492
2.68k
    for (i = tab->n_redundant; 
i < tab->n_row2.68k
;
++i175
)
{2.68k
2493
2.68k
      struct isl_tab_var *var;
2494
2.68k
      int sgn;
2495
2.68k
      var = isl_tab_var_from_row(tab, i);
2496
2.68k
      if (!var->is_nonneg)
2497
175
        continue;
2498
2.50k
      sgn = sign_of_max(tab, var);
2499
2.50k
      if (sgn < -1)
2500
0
        return isl_bool_error;
2501
2.50k
      
if (2.50k
sgn != 02.50k
)
2502
275
        return isl_bool_false;
2503
2.23k
      
if (2.23k
close_row(tab, var, 0) < 02.23k
)
2504
0
        return isl_bool_error;
2505
2.23k
      break;
2506
2.23k
    }
2507
2.23k
    
if (2.23k
tab->n_dead == tab->n_col2.23k
)
2508
1.39k
      return isl_bool_true;
2509
841
    
if (841
i == tab->n_row841
)
2510
3
      return isl_bool_false;
2511
841
  }
2512
1.67k
}
2513
2514
int isl_tab_sample_is_integer(struct isl_tab *tab)
2515
249k
{
2516
249k
  int i;
2517
249k
2518
249k
  if (!tab)
2519
0
    return -1;
2520
249k
2521
1.22M
  
for (i = 0; 249k
i < tab->n_var1.22M
;
++i979k
)
{1.02M
2522
1.02M
    int row;
2523
1.02M
    if (!tab->var[i].is_row)
2524
335k
      continue;
2525
687k
    row = tab->var[i].index;
2526
687k
    if (
!687k
isl_int_is_divisible_by687k
(tab->mat->row[row][1],
2527
687k
            tab->mat->row[row][0]))
2528
43.8k
      return 0;
2529
687k
  }
2530
206k
  return 1;
2531
249k
}
2532
2533
static struct isl_vec *extract_integer_sample(struct isl_tab *tab)
2534
139k
{
2535
139k
  int i;
2536
139k
  struct isl_vec *vec;
2537
139k
2538
139k
  vec = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
2539
139k
  if (!vec)
2540
0
    return NULL;
2541
139k
2542
139k
  
isl_int_set_si139k
(vec->block.data[0], 1);139k
2543
859k
  for (i = 0; 
i < tab->n_var859k
;
++i720k
)
{720k
2544
720k
    if (!tab->var[i].is_row)
2545
293k
      isl_int_set_si(vec->block.data[1 + i], 0);
2546
427k
    else {
2547
427k
      int row = tab->var[i].index;
2548
427k
      isl_int_divexact(vec->block.data[1 + i],
2549
427k
        tab->mat->row[row][1], tab->mat->row[row][0]);
2550
427k
    }
2551
720k
  }
2552
139k
2553
139k
  return vec;
2554
139k
}
2555
2556
struct isl_vec *isl_tab_get_sample_value(struct isl_tab *tab)
2557
147k
{
2558
147k
  int i;
2559
147k
  struct isl_vec *vec;
2560
147k
  isl_int m;
2561
147k
2562
147k
  if (!tab)
2563
0
    return NULL;
2564
147k
2565
147k
  vec = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
2566
147k
  if (!vec)
2567
0
    return NULL;
2568
147k
2569
147k
  
isl_int_init147k
(m);147k
2570
147k
2571
147k
  isl_int_set_si(vec->block.data[0], 1);
2572
678k
  for (i = 0; 
i < tab->n_var678k
;
++i531k
)
{531k
2573
531k
    int row;
2574
531k
    if (
!tab->var[i].is_row531k
)
{256k
2575
256k
      isl_int_set_si(vec->block.data[1 + i], 0);
2576
256k
      continue;
2577
256k
    }
2578
274k
    row = tab->var[i].index;
2579
274k
    isl_int_gcd(m, vec->block.data[0], tab->mat->row[row][0]);
2580
274k
    isl_int_divexact(m, tab->mat->row[row][0], m);
2581
274k
    isl_seq_scale(vec->block.data, vec->block.data, m, 1 + i);
2582
274k
    isl_int_divexact(m, vec->block.data[0], tab->mat->row[row][0]);
2583
274k
    isl_int_mul(vec->block.data[1 + i], m, tab->mat->row[row][1]);
2584
274k
  }
2585
147k
  vec = isl_vec_normalize(vec);
2586
147k
2587
147k
  isl_int_clear(m);
2588
147k
  return vec;
2589
147k
}
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
112k
{
2597
112k
  if (!var->is_row)
2598
2.45k
    isl_int_set_si(*v, 0);
2599
110k
  else 
if (110k
sgn > 0110k
)
2600
108k
    isl_int_cdiv_q(*v, tab->mat->row[var->index][1],
2601
110k
           tab->mat->row[var->index][0]);
2602
110k
  else
2603
1.27k
    isl_int_fdiv_q(*v, tab->mat->row[var->index][1],
2604
112k
           tab->mat->row[var->index][0]);
2605
112k
}
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
302k
{
2618
302k
  int i;
2619
302k
  unsigned n_eq;
2620
302k
2621
302k
  if (!bmap)
2622
0
    return NULL;
2623
302k
  
if (302k
!tab302k
)
2624
0
    return bmap;
2625
302k
2626
302k
  n_eq = tab->n_eq;
2627
302k
  if (tab->empty)
2628
5.96k
    bmap = isl_basic_map_set_to_empty(bmap);
2629
302k
  else
2630
1.80M
    
for (i = bmap->n_ineq - 1; 296k
i >= 01.80M
;
--i1.50M
)
{1.50M
2631
1.50M
      if (isl_tab_is_equality(tab, n_eq + i))
2632
526k
        isl_basic_map_inequality_to_equality(bmap, i);
2633
979k
      else 
if (979k
isl_tab_is_redundant(tab, n_eq + i)979k
)
2634
98.6k
        isl_basic_map_drop_inequality(bmap, i);
2635
1.50M
    }
2636
302k
  if (bmap->n_eq != n_eq)
2637
100k
    bmap = isl_basic_map_gauss(bmap, NULL);
2638
302k
  if (!tab->rational &&
2639
268k
      
bmap268k
&&
!bmap->sample268k
&&
isl_tab_sample_is_integer(tab)148k
)
2640
139k
    bmap->sample = extract_integer_sample(tab);
2641
302k
  return bmap;
2642
302k
}
2643
2644
struct isl_basic_set *isl_basic_set_update_from_tab(struct isl_basic_set *bset,
2645
  struct isl_tab *tab)
2646
24.9k
{
2647
24.9k
  return bset_from_bmap(isl_basic_map_update_from_tab(bset_to_bmap(bset),
2648
24.9k
                tab));
2649
24.9k
}
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
10.2k
{
2656
10.2k
  if (!tab->con[r].is_row)
2657
0
    isl_die(isl_tab_get_ctx(tab), isl_error_internal,
2658
10.2k
      "row unexpectedly moved to column",
2659
10.2k
      return isl_stat_error);
2660
10.2k
  
if (10.2k
r + 1 != tab->n_con10.2k
)
2661
0
    isl_die(isl_tab_get_ctx(tab), isl_error_internal,
2662
10.2k
      "additional constraints added", return isl_stat_error);
2663
10.2k
  
if (10.2k
drop_row(tab, tab->con[r].index) < 010.2k
)
2664
0
    return isl_stat_error;
2665
10.2k
2666
10.2k
  return isl_stat_ok;
2667
10.2k
}
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
10.2k
{
2681
10.2k
  unsigned r;
2682
10.2k
  isl_int *row;
2683
10.2k
  int sgn;
2684
10.2k
  unsigned off = 2 + tab->M;
2685
10.2k
2686
10.2k
  if (var->is_zero)
2687
0
    return isl_stat_ok;
2688
10.2k
  
if (10.2k
var->is_redundant || 10.2k
!var->is_nonneg10.2k
)
2689
0
    isl_die(isl_tab_get_ctx(tab), isl_error_invalid,
2690
10.2k
      "expecting non-redundant non-negative variable",
2691
10.2k
      return isl_stat_error);
2692
10.2k
2693
10.2k
  
if (10.2k
isl_tab_extend_cons(tab, 1) < 010.2k
)
2694
0
    return isl_stat_error;
2695
10.2k
2696
10.2k
  r = tab->n_con;
2697
10.2k
  tab->con[r].index = tab->n_row;
2698
10.2k
  tab->con[r].is_row = 1;
2699
10.2k
  tab->con[r].is_nonneg = 0;
2700
10.2k
  tab->con[r].is_zero = 0;
2701
10.2k
  tab->con[r].is_redundant = 0;
2702
10.2k
  tab->con[r].frozen = 0;
2703
10.2k
  tab->con[r].negated = 0;
2704
10.2k
  tab->row_var[tab->n_row] = ~r;
2705
10.2k
  row = tab->mat->row[tab->n_row];
2706
10.2k
2707
10.2k
  if (
var->is_row10.2k
)
{707
2708
707
    isl_int_set(row[0], tab->mat->row[var->index][0]);
2709
707
    isl_seq_neg(row + 1,
2710
707
          tab->mat->row[var->index] + 1, 1 + tab->n_col);
2711
9.59k
  } else {
2712
9.59k
    isl_int_set_si(row[0], 1);
2713
9.59k
    isl_seq_clr(row + 1, 1 + tab->n_col);
2714
9.59k
    isl_int_set_si(row[off + var->index], -1);
2715
9.59k
  }
2716
10.2k
2717
10.2k
  tab->n_row++;
2718
10.2k
  tab->n_con++;
2719
10.2k
2720
10.2k
  sgn = sign_of_max(tab, &tab->con[r]);
2721
10.2k
  if (sgn < -1)
2722
0
    return isl_stat_error;
2723
10.2k
  
if (10.2k
sgn < 010.2k
)
{5
2724
5
    if (drop_last_con_in_row(tab, r) < 0)
2725
0
      return isl_stat_error;
2726
5
    
if (5
isl_tab_mark_empty(tab) < 05
)
2727
0
      return isl_stat_error;
2728
5
    return isl_stat_ok;
2729
5
  }
2730
10.2k
  tab->con[r].is_nonneg = 1;
2731
10.2k
  /* sgn == 0 */
2732
10.2k
  if (close_row(tab, &tab->con[r], 1) < 0)
2733
0
    return isl_stat_error;
2734
10.2k
  
if (10.2k
drop_last_con_in_row(tab, r) < 010.2k
)
2735
0
    return isl_stat_error;
2736
10.2k
2737
10.2k
  return isl_stat_ok;
2738
10.2k
}
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
589
{
2759
589
  struct isl_tab_var *var;
2760
589
2761
589
  if (!tab)
2762
0
    return -1;
2763
589
2764
589
  var = &tab->con[con];
2765
589
2766
589
  if (
var->is_row && 589
(var->index < 0 || 9
var->index < tab->n_redundant9
))
2767
0
    isl_die(tab->mat->ctx, isl_error_invalid,
2768
589
      "cannot relax redundant constraint", return -1);
2769
589
  
if (589
!var->is_row && 589
(var->index < 0 || 580
var->index < tab->n_dead580
))
2770
0
    isl_die(tab->mat->ctx, isl_error_invalid,
2771
589
      "cannot relax dead constraint", return -1);
2772
589
2773
589
  
if (589
!var->is_row && 589
!max_is_manifestly_unbounded(tab, var)580
)
2774
328
    
if (328
to_row(tab, var, 1) < 0328
)
2775
0
      return -1;
2776
589
  
if (589
!var->is_row && 589
!min_is_manifestly_unbounded(tab, var)252
)
2777
14
    
if (14
to_row(tab, var, -1) < 014
)
2778
0
      return -1;
2779
589
2780
589
  
if (589
var->is_row589
)
{351
2781
351
    isl_int_add(tab->mat->row[var->index][1],
2782
351
        tab->mat->row[var->index][1], tab->mat->row[var->index][0]);
2783
351
    if (restore_row(tab, var) < 0)
2784
0
      return -1;
2785
238
  } else {
2786
238
    int i;
2787
238
    unsigned off = 2 + tab->M;
2788
238
2789
1.09k
    for (i = 0; 
i < tab->n_row1.09k
;
++i857
)
{857
2790
857
      if (isl_int_is_zero(tab->mat->row[i][off + var->index]))
2791
561
        continue;
2792
296
      
isl_int_sub296
(tab->mat->row[i][1], tab->mat->row[i][1],296
2793
296
          tab->mat->row[i][off + var->index]);
2794
296
    }
2795
238
2796
238
  }
2797
589
2798
589
  
if (589
isl_tab_push_var(tab, isl_tab_undo_relax, var) < 0589
)
2799
0
    return -1;
2800
589
2801
589
  return 0;
2802
589
}
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
117
{
2823
117
  struct isl_tab_var *var;
2824
117
2825
117
  if (!tab)
2826
0
    return -1;
2827
117
  
if (117
isl_int_is_zero117
(shift))
2828
58
    return 0;
2829
117
2830
59
  var = &tab->var[pos];
2831
59
  if (
!var->is_row59
)
{8
2832
8
    if (
isl_int_is_neg8
(shift))
{5
2833
5
      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
8
  }
2842
59
2843
59
  
if (59
var->is_row59
)
{54
2844
54
    isl_int_addmul(tab->mat->row[var->index][1],
2845
54
        shift, tab->mat->row[var->index][0]);
2846
5
  } else {
2847
5
    int i;
2848
5
    unsigned off = 2 + tab->M;
2849
5
2850
27
    for (i = 0; 
i < tab->n_row27
;
++i22
)
{22
2851
22
      if (isl_int_is_zero(tab->mat->row[i][off + var->index]))
2852
15
        continue;
2853
7
      
isl_int_submul7
(tab->mat->row[i][1],7
2854
7
            shift, tab->mat->row[i][off + var->index]);
2855
7
    }
2856
5
2857
5
  }
2858
59
2859
59
  return 0;
2860
59
}
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
245
{
2869
245
  struct isl_tab_var *var;
2870
245
2871
245
  if (!tab)
2872
0
    return -1;
2873
245
2874
245
  var = &tab->con[con];
2875
245
  if (!var->is_nonneg)
2876
0
    return 0;
2877
245
2878
245
  var->is_nonneg = 0;
2879
245
  if (isl_tab_push_var(tab, isl_tab_undo_unrestrict, var) < 0)
2880
0
    return -1;
2881
245
2882
245
  return 0;
2883
245
}
2884
2885
int isl_tab_select_facet(struct isl_tab *tab, int con)
2886
10.0k
{
2887
10.0k
  if (!tab)
2888
0
    return -1;
2889
10.0k
2890
10.0k
  return cut_to_hyperplane(tab, &tab->con[con]);
2891
10.0k
}
2892
2893
static int may_be_equality(struct isl_tab *tab, int row)
2894
3.68M
{
2895
36.1k
  return tab->rational ? isl_int_is_zero(tab->mat->row[row][1])
2896
3.65M
           : isl_int_lt(tab->mat->row[row][1],
2897
3.68M
              tab->mat->row[row][0]);
2898
3.68M
}
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.18M
{
2913
1.18M
  int i;
2914
1.18M
  struct isl_tab_var *var;
2915
1.18M
2916
7.38M
  for (i = tab->n_con - 1; 
i >= 07.38M
;
--i6.20M
)
{7.28M
2917
7.28M
    var = &tab->con[i];
2918
7.28M
    if (var->index < 0)
2919
2.78M
      continue;
2920
4.49M
    
if (4.49M
var->is_row && 4.49M
var->index < tab->n_redundant3.11M
)
2921
128k
      continue;
2922
4.36M
    
if (4.36M
!var->is_row && 4.36M
var->index < tab->n_dead1.38M
)
2923
3.95k
      continue;
2924
4.36M
    
if (4.36M
var->marked4.36M
)
2925
1.07M
      return var;
2926
4.36M
  }
2927
1.18M
2928
103k
  return NULL;
2929
1.18M
}
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
279k
{
2950
279k
  int i;
2951
279k
  unsigned n_marked;
2952
279k
2953
279k
  if (!tab)
2954
0
    return -1;
2955
279k
  
if (279k
tab->empty279k
)
2956
3.30k
    return 0;
2957
276k
  
if (276k
tab->n_dead == tab->n_col276k
)
2958
17.8k
    return 0;
2959
276k
2960
258k
  n_marked = 0;
2961
1.67M
  for (i = tab->n_redundant; 
i < tab->n_row1.67M
;
++i1.41M
)
{1.41M
2962
1.41M
    struct isl_tab_var *var = isl_tab_var_from_row(tab, i);
2963
1.40M
    var->marked = !var->frozen && var->is_nonneg &&
2964
1.25M
      may_be_equality(tab, i);
2965
1.41M
    if (var->marked)
2966
924k
      n_marked++;
2967
1.41M
  }
2968
1.41M
  for (i = tab->n_dead; 
i < tab->n_col1.41M
;
++i1.15M
)
{1.15M
2969
1.15M
    struct isl_tab_var *var = var_from_col(tab, i);
2970
1.14M
    var->marked = !var->frozen && var->is_nonneg;
2971
1.15M
    if (var->marked)
2972
134k
      n_marked++;
2973
1.15M
  }
2974
1.00M
  while (
n_marked1.00M
)
{842k
2975
842k
    struct isl_tab_var *var;
2976
842k
    int sgn;
2977
842k
    var = select_marked(tab);
2978
842k
    if (!var)
2979
98.5k
      break;
2980
743k
    var->marked = 0;
2981
743k
    n_marked--;
2982
743k
    sgn = sign_of_max(tab, var);
2983
743k
    if (sgn < 0)
2984
0
      return -1;
2985
743k
    
if (743k
sgn == 0743k
)
{274k
2986
274k
      if (close_row(tab, var, 0) < 0)
2987
0
        return -1;
2988
469k
    } else 
if (469k
!tab->rational && 469k
!at_least_one(tab, var)446k
)
{228
2989
228
      if (cut_to_hyperplane(tab, var) < 0)
2990
0
        return -1;
2991
228
      return isl_tab_detect_implicit_equalities(tab);
2992
228
    }
2993
5.71M
    
for (i = tab->n_redundant; 743k
i < tab->n_row5.71M
;
++i4.97M
)
{4.97M
2994
4.97M
      var = isl_tab_var_from_row(tab, i);
2995
4.97M
      if (!var->marked)
2996
2.54M
        continue;
2997
2.43M
      
if (2.43M
may_be_equality(tab, i)2.43M
)
2998
2.39M
        continue;
2999
32.2k
      var->marked = 0;
3000
32.2k
      n_marked--;
3001
32.2k
    }
3002
743k
  }
3003
258k
3004
258k
  return 0;
3005
258k
}
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
52.8k
{
3085
52.8k
  int i;
3086
52.8k
3087
52.8k
  if (
!tab || 52.8k
!bmap52.8k
)
3088
0
    return isl_basic_map_free(bmap);
3089
52.8k
  
if (52.8k
tab->empty52.8k
)
3090
75
    return bmap;
3091
52.8k
3092
166k
  
for (i = bmap->n_ineq - 1; 52.7k
i >= 0166k
;
--i114k
)
{114k
3093
114k
    if (!isl_tab_is_equality(tab, bmap->n_eq + i))
3094
113k
      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
52.7k
3104
52.7k
  return bmap;
3105
52.7k
}
3106
3107
static int con_is_redundant(struct isl_tab *tab, struct isl_tab_var *var)
3108
522k
{
3109
522k
  if (!tab)
3110
0
    return -1;
3111
522k
  
if (522k
tab->rational522k
)
{70.2k
3112
70.2k
    int sgn = sign_of_min(tab, var);
3113
70.2k
    if (sgn < -1)
3114
0
      return -1;
3115
70.2k
    return sgn >= 0;
3116
452k
  } else {
3117
452k
    int irred = isl_tab_min_at_most_neg_one(tab, var);
3118
452k
    if (irred < 0)
3119
0
      return -1;
3120
452k
    return !irred;
3121
452k
  }
3122
522k
}
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
148k
{
3139
148k
  int i;
3140
148k
  unsigned n_marked;
3141
148k
3142
148k
  if (!tab)
3143
0
    return -1;
3144
148k
  
if (148k
tab->empty148k
)
3145
2.34k
    return 0;
3146
146k
  
if (146k
tab->n_redundant == tab->n_row146k
)
3147
2.92k
    return 0;
3148
146k
3149
143k
  n_marked = 0;
3150
922k
  for (i = tab->n_redundant; 
i < tab->n_row922k
;
++i779k
)
{779k
3151
779k
    struct isl_tab_var *var = isl_tab_var_from_row(tab, i);
3152
685k
    var->marked = !var->frozen && var->is_nonneg;
3153
779k
    if (var->marked)
3154
331k
      n_marked++;
3155
779k
  }
3156
749k
  for (i = tab->n_dead; 
i < tab->n_col749k
;
++i606k
)
{606k
3157
606k
    struct isl_tab_var *var = var_from_col(tab, i);
3158
556k
    var->marked = !var->frozen && var->is_nonneg &&
3159
249k
      !min_is_manifestly_unbounded(tab, var);
3160
606k
    if (var->marked)
3161
63.8k
      n_marked++;
3162
606k
  }
3163
479k
  while (
n_marked479k
)
{340k
3164
340k
    struct isl_tab_var *var;
3165
340k
    int red;
3166
340k
    var = select_marked(tab);
3167
340k
    if (!var)
3168
4.58k
      break;
3169
336k
    var->marked = 0;
3170
336k
    n_marked--;
3171
336k
    red = con_is_redundant(tab, var);
3172
336k
    if (red < 0)
3173
0
      return -1;
3174
336k
    
if (336k
red && 336k
!var->is_redundant67.7k
)
3175
10.1k
      
if (10.1k
isl_tab_mark_redundant(tab, var->index) < 010.1k
)
3176
0
        return -1;
3177
3.34M
    
for (i = tab->n_dead; 336k
i < tab->n_col3.34M
;
++i3.01M
)
{3.01M
3178
3.01M
      var = var_from_col(tab, i);
3179
3.01M
      if (!var->marked)
3180
2.83M
        continue;
3181
180k
      
if (180k
!min_is_manifestly_unbounded(tab, var)180k
)
3182
128k
        continue;
3183
52.0k
      var->marked = 0;
3184
52.0k
      n_marked--;
3185
52.0k
    }
3186
336k
  }
3187
143k
3188
143k
  return 0;
3189
143k
}
3190
3191
int isl_tab_is_equality(struct isl_tab *tab, int con)
3192
1.63M
{
3193
1.63M
  int row;
3194
1.63M
  unsigned off;
3195
1.63M
3196
1.63M
  if (!tab)
3197
0
    return -1;
3198
1.63M
  
if (1.63M
tab->con[con].is_zero1.63M
)
3199
528k
    return 1;
3200
1.10M
  
if (1.10M
tab->con[con].is_redundant1.10M
)
3201
101k
    return 0;
3202
1.00M
  
if (1.00M
!tab->con[con].is_row1.00M
)
3203
578k
    return tab->con[con].index < tab->n_dead;
3204
1.00M
3205
427k
  row = tab->con[con].index;
3206
427k
3207
427k
  off = 2 + tab->M;
3208
427k
  return isl_int_is_zero(tab->mat->row[row][1]) &&
3209
52.8k
    !row_is_big(tab, row) &&
3210
52.8k
    isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
3211
52.8k
          tab->n_col - tab->n_dead) == -1;
3212
1.00M
}
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
145k
{
3228
145k
  int r;
3229
145k
  enum isl_lp_result res = isl_lp_ok;
3230
145k
  struct isl_tab_var *var;
3231
145k
  struct isl_tab_undo *snap;
3232
145k
3233
145k
  if (!tab)
3234
0
    return isl_lp_error;
3235
145k
3236
145k
  
if (145k
tab->empty145k
)
3237
14
    return isl_lp_empty;
3238
145k
3239
145k
  snap = isl_tab_snap(tab);
3240
145k
  r = isl_tab_add_row(tab, f);
3241
145k
  if (r < 0)
3242
0
    return isl_lp_error;
3243
145k
  var = &tab->con[r];
3244
309k
  for (;;) {
3245
309k
    int row, col;
3246
309k
    find_pivot(tab, var, var, -1, &row, &col);
3247
309k
    if (
row == var->index309k
)
{3.64k
3248
3.64k
      res = isl_lp_unbounded;
3249
3.64k
      break;
3250
3.64k
    }
3251
305k
    
if (305k
row == -1305k
)
3252
141k
      break;
3253
164k
    
if (164k
isl_tab_pivot(tab, row, col) < 0164k
)
3254
0
      return isl_lp_error;
3255
164k
  }
3256
145k
  
isl_int_mul145k
(tab->mat->row[var->index][0],145k
3257
145k
        tab->mat->row[var->index][0], denom);
3258
145k
  if (
ISL_FL_ISSET145k
(flags, ISL_TAB_SAVE_DUAL))
{7.91k
3259
7.91k
    int i;
3260
7.91k
3261
7.91k
    isl_vec_free(tab->dual);
3262
7.91k
    tab->dual = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_con);
3263
7.91k
    if (!tab->dual)
3264
0
      return isl_lp_error;
3265
7.91k
    
isl_int_set7.91k
(tab->dual->el[0], tab->mat->row[var->index][0]);7.91k
3266
344k
    for (i = 0; 
i < tab->n_con344k
;
++i336k
)
{336k
3267
336k
      int pos;
3268
336k
      if (
tab->con[i].is_row336k
)
{220k
3269
220k
        isl_int_set_si(tab->dual->el[1 + i], 0);
3270
220k
        continue;
3271
220k
      }
3272
116k
      pos = 2 + tab->M + tab->con[i].index;
3273
116k
      if (tab->con[i].negated)
3274
24.0k
        isl_int_neg(tab->dual->el[1 + i],
3275
116k
              tab->mat->row[var->index][pos]);
3276
116k
      else
3277
92.3k
        isl_int_set(tab->dual->el[1 + i],
3278
116k
              tab->mat->row[var->index][pos]);
3279
116k
    }
3280
7.91k
  }
3281
145k
  
if (145k
opt && 145k
res == isl_lp_ok145k
)
{141k
3282
141k
    if (
opt_denom141k
)
{33.5k
3283
33.5k
      isl_int_set(*opt, tab->mat->row[var->index][1]);
3284
33.5k
      isl_int_set(*opt_denom, tab->mat->row[var->index][0]);
3285
33.5k
    } else
3286
108k
      get_rounded_sample_value(tab, var, 1, opt);
3287
141k
  }
3288
145k
  if (isl_tab_rollback(tab, snap) < 0)
3289
0
    return isl_lp_error;
3290
145k
  return res;
3291
145k
}
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.31M
{
3303
1.31M
  if (!tab)
3304
0
    return -1;
3305
1.31M
  
if (1.31M
con < 0 || 1.31M
con >= tab->n_con1.31M
)
3306
0
    isl_die(isl_tab_get_ctx(tab), isl_error_invalid,
3307
1.31M
      "position out of bounds", return -1);
3308
1.31M
  
if (1.31M
tab->con[con].is_zero1.31M
)
3309
63
    return 0;
3310
1.31M
  
if (1.31M
tab->con[con].is_redundant1.31M
)
3311
177k
    return 1;
3312
1.13M
  
return tab->con[con].is_row && 1.13M
tab->con[con].index < tab->n_redundant439k
;
3313
1.31M
}
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
3.69k
{
3326
3.69k
  unsigned off = 2 + tab->M;
3327
3.69k
  isl_mat *mat = tab->mat;
3328
3.69k
  int n;
3329
3.69k
  int row;
3330
3.69k
  int pos;
3331
3.69k
3332
3.69k
  if (!var->is_row)
3333
2.45k
    return isl_bool_false;
3334
1.23k
  row = var->index;
3335
1.23k
  if (row_is_big(tab, row))
3336
0
    return isl_bool_false;
3337
1.23k
  n = tab->n_col - tab->n_dead;
3338
1.23k
  pos = isl_seq_first_non_zero(mat->row[row] + off + tab->n_dead, n);
3339
1.23k
  if (pos != -1)
3340
1.19k
    return isl_bool_false;
3341
42
  
if (42
value42
)
3342
0
    isl_int_divexact(*value, mat->row[row][1], mat->row[row][0]);
3343
42
  return isl_bool_true;
3344
1.23k
}
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.34k
{
3359
4.34k
  if (row_is_big(tab, var->index))
3360
0
    return 1;
3361
4.34k
  
isl_int_mul4.34k
(*tmp, tab->mat->row[var->index][0], target);4.34k
3362
4.34k
  if (sgn > 0)
3363
1.76k
    
return 1.76k
isl_int_ge1.76k
(tab->mat->row[var->index][1], *tmp);
3364
4.34k
  else
3365
2.58k
    
return 2.58k
isl_int_le2.58k
(tab->mat->row[var->index][1], *tmp);
3366
4.34k
}
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.55k
{
3387
4.55k
  int row, col;
3388
4.55k
3389
4.55k
  if (
sgn < 0 && 4.55k
min_is_manifestly_unbounded(tab, var)3.65k
)
3390
1.80k
    return isl_bool_true;
3391
2.74k
  
if (2.74k
sgn > 0 && 2.74k
max_is_manifestly_unbounded(tab, var)896
)
3392
0
    return isl_bool_true;
3393
2.74k
  
if (2.74k
to_row(tab, var, sgn) < 02.74k
)
3394
0
    return isl_bool_error;
3395
4.34k
  
while (2.74k
!reached(tab, var, sgn, target, tmp)4.34k
)
{3.95k
3396
3.95k
    find_pivot(tab, var, var, sgn, &row, &col);
3397
3.95k
    if (row == -1)
3398
976
      return isl_bool_false;
3399
2.97k
    
if (2.97k
row == var->index2.97k
)
3400
1.37k
      return isl_bool_true;
3401
1.60k
    
if (1.60k
isl_tab_pivot(tab, row, col) < 01.60k
)
3402
0
      return isl_bool_error;
3403
1.60k
  }
3404
2.74k
3405
391
  return isl_bool_true;
3406
2.74k
}
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.65k
{
3430
3.65k
  isl_bool reached;
3431
3.65k
  isl_vec *eq;
3432
3.65k
  int pos;
3433
3.65k
  isl_stat r;
3434
3.65k
3435
3.65k
  get_rounded_sample_value(tab, var, -1, target);
3436
3.65k
  isl_int_sub_ui(*target, *target, 1);
3437
3.65k
  reached = var_reaches(tab, var, -1, *target, tmp);
3438
3.65k
  if (
reached < 0 || 3.65k
reached3.65k
)
3439
2.75k
    return isl_bool_not(reached);
3440
896
  get_rounded_sample_value(tab, var, 1, target);
3441
896
  isl_int_add_ui(*target, *target, 1);
3442
896
  reached = var_reaches(tab, var, 1, *target, tmp);
3443
896
  if (
reached < 0 || 896
reached896
)
3444
816
    return isl_bool_not(reached);
3445
80
  get_rounded_sample_value(tab, var, -1, tmp);
3446
80
  isl_int_sub_ui(*target, *target, 1);
3447
80
  if (
isl_int_ne80
(*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
80
3453
80
  
if (80
isl_tab_extend_cons(tab, 1) < 080
)
3454
0
    return isl_bool_error;
3455
80
  eq = isl_vec_alloc(isl_tab_get_ctx(tab), 1 + tab->n_var);
3456
80
  if (!eq)
3457
0
    return isl_bool_error;
3458
80
  pos = var - tab->var;
3459
80
  isl_seq_clr(eq->el + 1, tab->n_var);
3460
80
  isl_int_set_si(eq->el[1 + pos], -1);
3461
80
  isl_int_set(eq->el[0], *target);
3462
80
  r = isl_tab_add_eq(tab, eq->el);
3463
80
  isl_vec_free(eq);
3464
80
3465
80
  return r < 0 ? 
isl_bool_error0
:
isl_bool_true80
;
3466
80
}
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
3.69k
{
3484
3.69k
  isl_int target, tmp;
3485
3.69k
  isl_bool is_cst;
3486
3.69k
3487
3.69k
  if (
var->is_row && 3.69k
row_is_big(tab, var->index)1.23k
)
3488
0
    return isl_bool_false;
3489
3.69k
  is_cst = is_constant(tab, var, value);
3490
3.69k
  if (
is_cst < 0 || 3.69k
is_cst3.69k
)
3491
42
    return is_cst;
3492
3.69k
3493
3.65k
  
if (3.65k
!value3.65k
)
3494
1.22k
    isl_int_init(target);
3495
3.65k
  isl_int_init(tmp);
3496
3.65k
3497
3.65k
  is_cst = detect_constant_with_tmp(tab, var,
3498
2.43k
              value ? 
value2.43k
:
&target1.22k
, &tmp);
3499
3.65k
3500
3.65k
  isl_int_clear(tmp);
3501
3.65k
  if (!value)
3502
1.22k
    isl_int_clear(target);
3503
3.65k
3504
3.65k
  return is_cst;
3505
3.69k
}
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.43k
{
3516
2.43k
  if (!tab)
3517
0
    return isl_bool_error;
3518
2.43k
  
if (2.43k
var < 0 || 2.43k
var >= tab->n_var2.43k
)
3519
0
    isl_die(isl_tab_get_ctx(tab), isl_error_invalid,
3520
2.43k
      "position out of bounds", return isl_bool_error);
3521
2.43k
  
if (2.43k
tab->rational2.43k
)
3522
0
    return isl_bool_false;
3523
2.43k
3524
2.43k
  return get_constant(tab, &tab->var[var], value);
3525
2.43k
}
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
245
{
3535
245
  int i;
3536
245
3537
245
  if (!tab)
3538
0
    return isl_stat_error;
3539
245
  
if (245
tab->rational245
)
3540
0
    return isl_stat_ok;
3541
245
3542
1.50k
  
for (i = 0; 245
i < tab->n_var1.50k
;
++i1.26k
)
{1.26k
3543
1.26k
    if (get_constant(tab, &tab->var[i], NULL) < 0)
3544
0
      return isl_stat_error;
3545
1.26k
  }
3546
245
3547
245
  return isl_stat_ok;
3548
245
}
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
710k
{
3555
710k
  if (!tab)
3556
0
    return NULL;
3557
710k
  tab->need_undo = 1;
3558
710k
  return tab->top;
3559
710k
}
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
188
{
3566
188
  if (!tab)
3567
0
    return isl_bool_error;
3568
188
3569
188
  return tab->need_undo;
3570
188
}
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
188
{
3579
188
  if (!tab)
3580
0
    return;
3581
188
3582
188
  free_undo(tab);
3583
188
  tab->need_undo = 0;
3584
188
}
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
265
{
3592
265
  unsigned off = 2 + tab->M;
3593
265
3594
265
  if (
!var->is_row && 265
!max_is_manifestly_unbounded(tab, var)261
)
3595
76
    
if (76
to_row(tab, var, 1) < 076
)
3596
0
      return isl_stat_error;
3597
265
3598
265
  
if (265
var->is_row265
)
{80
3599
80
    isl_int_sub(tab->mat->row[var->index][1],
3600
80
        tab->mat->row[var->index][1], tab->mat->row[var->index][0]);
3601
80
    if (
var->is_nonneg80
)
{80
3602
80
      int sgn = restore_row(tab, var);
3603
80
      isl_assert(tab->mat->ctx, sgn >= 0,
3604
80
        return isl_stat_error);
3605
80
    }
3606
185
  } else {
3607
185
    int i;
3608
185
3609
867
    for (i = 0; 
i < tab->n_row867
;
++i682
)
{682
3610
682
      if (isl_int_is_zero(tab->mat->row[i][off + var->index]))
3611
468
        continue;
3612
214
      
isl_int_add214
(tab->mat->row[i][1], tab->mat->row[i][1],214
3613
214
          tab->mat->row[i][off + var->index]);
3614
214
    }
3615
185
3616
185
  }
3617
265
3618
265
  return isl_stat_ok;
3619
265
}
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
233
{
3628
233
  var->is_nonneg = 1;
3629
233
3630
233
  if (
var->is_row && 233
restore_row(tab, var) < -1206
)
3631
0
    return isl_stat_error;
3632
233
3633
233
  return isl_stat_ok;
3634
233
}
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
363k
{
3645
363k
  struct isl_tab_var *var;
3646
363k
3647
363k
  if (tab->n_redundant < 1)
3648
0
    isl_die(isl_tab_get_ctx(tab), isl_error_internal,
3649
363k
      "no redundant rows", return isl_stat_error);
3650
363k
3651
363k
  var = isl_tab_var_from_row(tab, tab->n_redundant - 1);
3652
363k
  var->is_redundant = 0;
3653
363k
  tab->n_redundant--;
3654
363k
  restore_row(tab, var);
3655
363k
3656
363k
  return isl_stat_ok;
3657
363k
}
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.51M
{
3663
1.51M
  struct isl_tab_var *var = var_from_index(tab, undo->u.var_index);
3664
1.51M
  switch (undo->type) {
3665
310k
  case isl_tab_undo_nonneg:
3666
310k
    var->is_nonneg = 0;
3667
310k
    break;
3668
266k
  case isl_tab_undo_redundant:
3669
266k
    if (
!var->is_row || 266k
var->index != tab->n_redundant - 1266k
)
3670
0
      isl_die(isl_tab_get_ctx(tab), isl_error_internal,
3671
266k
        "not undoing last redundant row", return -1);
3672
266k
    return restore_last_redundant(tab);
3673
214k
  case isl_tab_undo_freeze:
3674
214k
    var->frozen = 0;
3675
214k
    break;
3676
34.7k
  case isl_tab_undo_zero:
3677
34.7k
    var->is_zero = 0;
3678
34.7k
    if (!var->is_row)
3679
34.4k
      tab->n_dead--;
3680
34.7k
    break;
3681
687k
  case isl_tab_undo_allocate:
3682
687k
    if (
undo->u.var_index >= 0687k
)
{2.73k
3683
2.73k
      isl_assert(tab->mat->ctx, !var->is_row,
3684
2.73k
        return isl_stat_error);
3685
2.73k
      return drop_col(tab, var->index);
3686
2.73k
    }
3687
684k
    
if (684k
!var->is_row684k
)
{60.3k
3688
60.3k
      if (
!max_is_manifestly_unbounded(tab, var)60.3k
)
{33.3k
3689
33.3k
        if (to_row(tab, var, 1) < 0)
3690
0
          return isl_stat_error;
3691
27.0k
      } else 
if (27.0k
!min_is_manifestly_unbounded(tab, var)27.0k
)
{11.5k
3692
11.5k
        if (to_row(tab, var, -1) < 0)
3693
0
          return isl_stat_error;
3694
11.5k
      } else
3695
15.5k
        
if (15.5k
to_row(tab, var, 0) < 015.5k
)
3696
0
          return isl_stat_error;
3697
60.3k
    }
3698
684k
    return drop_row(tab, var->index);
3699
265
  case isl_tab_undo_relax:
3700
265
    return unrelax(tab, var);
3701
233
  case isl_tab_undo_unrestrict:
3702
233
    return ununrestrict(tab, var);
3703
0
  default:
3704
0
    isl_die(tab->mat->ctx, isl_error_internal,
3705
1.51M
      "perform_undo_var called on invalid undo record",
3706
1.51M
      return isl_stat_error);
3707
1.51M
  }
3708
1.51M
3709
560k
  return isl_stat_ok;
3710
1.51M
}
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
74.6k
{
3721
74.6k
  if (!tab)
3722
0
    return isl_stat_error;
3723
74.6k
3724
74.6k
  
if (74.6k
tab->need_undo74.6k
)
3725
0
    isl_die(isl_tab_get_ctx(tab), isl_error_invalid,
3726
74.6k
      "manually restoring redundant constraints "
3727
74.6k
      "interferes with undo history",
3728
74.6k
      return isl_stat_error);
3729
74.6k
3730
171k
  
while (74.6k
tab->n_redundant > 0171k
)
{96.7k
3731
96.7k
    if (
tab->row_var[tab->n_redundant - 1] >= 096.7k
)
{85.0k
3732
85.0k
      struct isl_tab_var *var;
3733
85.0k
3734
85.0k
      var = isl_tab_var_from_row(tab, tab->n_redundant - 1);
3735
85.0k
      var->is_nonneg = 0;
3736
85.0k
    }
3737
96.7k
    restore_last_redundant(tab);
3738
96.7k
  }
3739
74.6k
  return isl_stat_ok;
3740
74.6k
}
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
934
{
3747
934
  int off;
3748
934
3749
934
  off = tab->n_var - isl_basic_map_dim(tab->bmap, isl_dim_div);
3750
934
  if (isl_basic_map_drop_div(tab->bmap, pos - off) < 0)
3751
0
    return isl_stat_error;
3752
934
  
if (934
tab->samples934
)
{380
3753
380
    tab->samples = isl_mat_drop_cols(tab->samples, 1 + pos, 1);
3754
380
    if (!tab->samples)
3755
0
      return isl_stat_error;
3756
380
  }
3757
934
3758
934
  return isl_stat_ok;
3759
934
}
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
570
{
3774
570
  int i, j;
3775
570
  int n_extra = 0;
3776
570
  int *extra = NULL;  /* current columns that contain bad stuff */
3777
570
  unsigned off = 2 + tab->M;
3778
570
3779
570
  extra = isl_alloc_array(tab->mat->ctx, int, tab->n_col);
3780
570
  if (
tab->n_col && 570
!extra570
)
3781
0
    goto error;
3782
6.88k
  
for (i = 0; 570
i < tab->n_col6.88k
;
++i6.31k
)
{6.31k
3783
52.8k
    for (j = 0; 
j < tab->n_col52.8k
;
++j46.5k
)
3784
51.3k
      
if (51.3k
tab->col_var[i] == col_var[j]51.3k
)
3785
4.85k
        break;
3786
6.31k
    if (j < tab->n_col)
3787
4.85k
      continue;
3788
1.45k
    extra[n_extra++] = i;
3789
1.45k
  }
3790
5.06k
  for (i = 0; 
i < tab->n_col && 5.06k
n_extra > 04.95k
;
++i4.49k
)
{4.49k
3791
4.49k
    struct isl_tab_var *var;
3792
4.49k
    int row;
3793
4.49k
3794
36.3k
    for (j = 0; 
j < tab->n_col36.3k
;
++j31.8k
)
3795
34.8k
      
if (34.8k
col_var[i] == tab->col_var[j]34.8k
)
3796
3.04k
        break;
3797
4.49k
    if (j < tab->n_col)
3798
3.04k
      continue;
3799
1.45k
    var = var_from_index(tab, col_var[i]);
3800
1.45k
    row = var->index;
3801
1.85k
    for (j = 0; 
j < n_extra1.85k
;
++j397
)
3802
1.85k
      
if (1.85k
!1.85k
isl_int_is_zero1.85k
(tab->mat->row[row][off+extra[j]]))
3803
1.45k
        break;
3804
1.45k
    isl_assert(tab->mat->ctx, j < n_extra, goto error);
3805
1.45k
    
if (1.45k
isl_tab_pivot(tab, row, extra[j]) < 01.45k
)
3806
0
      goto error;
3807
1.45k
    extra[j] = extra[--n_extra];
3808
1.45k
  }
3809
570
3810
570
  free(extra);
3811
570
  return 0;
3812
0
error:
3813
0
  free(extra);
3814
0
  return -1;
3815
570
}
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
16.5k
{
3823
16.5k
  int i;
3824
16.5k
3825
19.8k
  for (i = tab->n_sample - 1; 
i >= 0 && 19.8k
tab->n_sample > n18.7k
;
--i3.37k
)
{3.37k
3826
3.37k
    if (tab->sample_index[i] < n)
3827
1.36k
      continue;
3828
3.37k
3829
2.01k
    
if (2.01k
i != tab->n_sample - 12.01k
)
{1.39k
3830
1.39k
      int t = tab->sample_index[tab->n_sample-1];
3831
1.39k
      tab->sample_index[tab->n_sample-1] = tab->sample_index[i];
3832
1.39k
      tab->sample_index[i] = t;
3833
1.39k
      isl_mat_swap_rows(tab->samples, tab->n_sample-1, i);
3834
1.39k
    }
3835
2.01k
    tab->n_sample--;
3836
2.01k
  }
3837
16.5k
}
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
1.77M
{
3843
1.77M
  switch (undo->type) {
3844
9.17k
  case isl_tab_undo_rational:
3845
9.17k
    tab->rational = 0;
3846
9.17k
    break;
3847
46.9k
  case isl_tab_undo_empty:
3848
46.9k
    tab->empty = 0;
3849
46.9k
    break;
3850
1.51M
  case isl_tab_undo_nonneg:
3851
1.51M
  case isl_tab_undo_redundant:
3852
1.51M
  case isl_tab_undo_freeze:
3853
1.51M
  case isl_tab_undo_zero:
3854
1.51M
  case isl_tab_undo_allocate:
3855
1.51M
  case isl_tab_undo_relax:
3856
1.51M
  case isl_tab_undo_unrestrict:
3857
1.51M
    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
184k
  case isl_tab_undo_bmap_ineq:
3861
184k
    return isl_basic_map_free_inequality(tab->bmap, 1);
3862
934
  case isl_tab_undo_bmap_div:
3863
934
    return drop_bmap_div(tab, undo->u.var_index);
3864
570
  case isl_tab_undo_saved_basis:
3865
570
    if (restore_basis(tab, undo->u.col_var) < 0)
3866
0
      return isl_stat_error;
3867
570
    break;
3868
3.96k
  case isl_tab_undo_drop_sample:
3869
3.96k
    tab->n_outside--;
3870
3.96k
    break;
3871
16.5k
  case isl_tab_undo_saved_samples:
3872
16.5k
    drop_samples_since(tab, undo->u.n);
3873
16.5k
    break;
3874
1.72k
  case isl_tab_undo_callback:
3875
1.72k
    return undo->u.callback->run(undo->u.callback);
3876
0
  default:
3877
0
    isl_assert(tab->mat->ctx, 0, return isl_stat_error);
3878
1.77M
  }
3879
77.1k
  return isl_stat_ok;
3880
1.77M
}
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
638k
{
3887
638k
  struct isl_tab_undo *undo, *next;
3888
638k
3889
638k
  if (!tab)
3890
0
    return -1;
3891
638k
3892
638k
  tab->in_undo = 1;
3893
2.41M
  for (undo = tab->top; 
undo && 2.41M
undo != &tab->bottom2.41M
;
undo = next1.77M
)
{1.93M
3894
1.93M
    next = undo->next;
3895
1.93M
    if (undo == snap)
3896
154k
      break;
3897
1.77M
    
if (1.77M
perform_undo(tab, undo) < 01.77M
)
{0
3898
0
      tab->top = undo;
3899
0
      free_undo(tab);
3900
0
      tab->in_undo = 0;
3901
0
      return -1;
3902
0
    }
3903
1.77M
    free_undo_record(undo);
3904
1.77M
  }
3905
638k
  tab->in_undo = 0;
3906
638k
  tab->top = undo;
3907
638k
  if (!undo)
3908
0
    return -1;
3909
638k
  return 0;
3910
638k
}
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
59.3k
{
3924
59.3k
  int pos;
3925
59.3k
  unsigned off = 2 + tab->M;
3926
59.3k
3927
59.3k
  if (tab->rational)
3928
9.20k
    return isl_ineq_separate;
3929
59.3k
3930
50.1k
  
if (50.1k
!50.1k
isl_int_is_one50.1k
(tab->mat->row[row][0]))
3931
125
    return isl_ineq_separate;
3932
50.1k
3933
50.0k
  pos = isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
3934
50.0k
          tab->n_col - tab->n_dead);
3935
50.0k
  if (
pos == -150.0k
)
{3.87k
3936
3.87k
    if (isl_int_is_negone(tab->mat->row[row][1]))
3937
3.06k
      return isl_ineq_adj_eq;
3938
3.87k
    else
3939
806
      return isl_ineq_separate;
3940
3.87k
  }
3941
50.0k
3942
46.1k
  
if (46.1k
!46.1k
isl_int_eq46.1k
(tab->mat->row[row][1],
3943
46.1k
      tab->mat->row[row][off + tab->n_dead + pos]))
3944
18.5k
    return isl_ineq_separate;
3945
46.1k
3946
27.6k
  pos = isl_seq_first_non_zero(
3947
27.6k
      tab->mat->row[row] + off + tab->n_dead + pos + 1,
3948
27.6k
      tab->n_col - tab->n_dead - pos - 1);
3949
27.6k
3950
25.6k
  return pos == -1 ? 
isl_ineq_adj_ineq25.6k
:
isl_ineq_separate2.05k
;
3951
46.1k
}
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
316k
{
3963
316k
  enum isl_ineq_type type = isl_ineq_error;
3964
316k
  struct isl_tab_undo *snap = NULL;
3965
316k
  int con;
3966
316k
  int row;
3967
316k
3968
316k
  if (!tab)
3969
0
    return isl_ineq_error;
3970
316k
3971
316k
  
if (316k
isl_tab_extend_cons(tab, 1) < 0316k
)
3972
0
    return isl_ineq_error;
3973
316k
3974
316k
  snap = isl_tab_snap(tab);
3975
316k
3976
316k
  con = isl_tab_add_row(tab, ineq);
3977
316k
  if (con < 0)
3978
0
    goto error;
3979
316k
3980
316k
  row = tab->con[con].index;
3981
316k
  if (isl_tab_row_is_redundant(tab, row))
3982
0
    type = isl_ineq_redundant;
3983
316k
  else 
if (316k
isl_int_is_neg316k
(tab->mat->row[row][1]) &&316k
3984
130k
     (tab->rational ||
3985
108k
        isl_int_abs_ge(tab->mat->row[row][1],
3986
129k
           tab->mat->row[row][0]))) {
3987
129k
    int nonneg = at_least_zero(tab, &tab->con[con]);
3988
129k
    if (nonneg < 0)
3989
0
      goto error;
3990
129k
    
if (129k
nonneg129k
)
3991
70.6k
      type = isl_ineq_cut;
3992
129k
    else
3993
59.3k
      type = separation_type(tab, row);
3994
186k
  } else {
3995
186k
    int red = con_is_redundant(tab, &tab->con[con]);
3996
186k
    if (red < 0)
3997
0
      goto error;
3998
186k
    
if (186k
!red186k
)
3999
53.6k
      type = isl_ineq_cut;
4000
186k
    else
4001
132k
      type = isl_ineq_redundant;
4002
186k
  }
4003
316k
4004
316k
  
if (316k
isl_tab_rollback(tab, snap)316k
)
4005
0
    return isl_ineq_error;
4006
316k
  return type;
4007
0
error:
4008
0
  return isl_ineq_error;
4009
316k
}
4010
4011
isl_stat isl_tab_track_bmap(struct isl_tab *tab, __isl_take isl_basic_map *bmap)
4012
114k
{
4013
114k
  bmap = isl_basic_map_cow(bmap);
4014
114k
  if (
!tab || 114k
!bmap114k
)
4015
0
    goto error;
4016
114k
4017
114k
  
if (114k
tab->empty114k
)
{4.17k
4018
4.17k
    bmap = isl_basic_map_set_to_empty(bmap);
4019
4.17k
    if (!bmap)
4020
0
      goto error;
4021
4.17k
    tab->bmap = bmap;
4022
4.17k
    return isl_stat_ok;
4023
4.17k
  }
4024
114k
4025
109k
  
isl_assert109k
(tab->mat->ctx, tab->n_eq == bmap->n_eq, goto error);109k
4026
109k
  
isl_assert109k
(tab->mat->ctx,109k
4027
109k
        tab->n_con == bmap->n_eq + bmap->n_ineq, goto error);
4028
109k
4029
109k
  tab->bmap = bmap;
4030
109k
4031
109k
  return isl_stat_ok;
4032
0
error:
4033
0
  isl_basic_map_free(bmap);
4034
0
  return isl_stat_error;
4035
109k
}
4036
4037
isl_stat isl_tab_track_bset(struct isl_tab *tab, __isl_take isl_basic_set *bset)
4038
658
{
4039
658
  return isl_tab_track_bmap(tab, bset_to_bmap(bset));
4040
658
}
4041
4042
__isl_keep isl_basic_set *isl_tab_peek_bset(struct isl_tab *tab)
4043
18.5k
{
4044
18.5k
  if (!tab)
4045
0
    return NULL;
4046
18.5k
4047
18.5k
  return bset_from_bmap(tab->bmap);
4048
18.5k
}
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
}