Coverage Report

Created: 2017-10-03 07:32

/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
724k
{
36
724k
  int i;
37
724k
  struct isl_tab *tab;
38
724k
  unsigned off = 2 + M;
39
724k
40
724k
  tab = isl_calloc_type(ctx, struct isl_tab);
41
724k
  if (!tab)
42
0
    return NULL;
43
724k
  tab->mat = isl_mat_alloc(ctx, n_row, off + n_var);
44
724k
  if (!tab->mat)
45
0
    goto error;
46
724k
  
tab->var = 724k
isl_alloc_array724k
(ctx, struct isl_tab_var, n_var);
47
724k
  if (
n_var && 724k
!tab->var723k
)
48
0
    goto error;
49
724k
  
tab->con = 724k
isl_alloc_array724k
(ctx, struct isl_tab_var, n_row);
50
724k
  if (
n_row && 724k
!tab->con724k
)
51
0
    goto error;
52
724k
  
tab->col_var = 724k
isl_alloc_array724k
(ctx, int, n_var);
53
724k
  if (
n_var && 724k
!tab->col_var723k
)
54
0
    goto error;
55
724k
  
tab->row_var = 724k
isl_alloc_array724k
(ctx, int, n_row);
56
724k
  if (
n_row && 724k
!tab->row_var724k
)
57
0
    goto error;
58
3.91M
  
for (i = 0; 724k
i < n_var3.91M
;
++i3.19M
) {
59
3.19M
    tab->var[i].index = i;
60
3.19M
    tab->var[i].is_row = 0;
61
3.19M
    tab->var[i].is_nonneg = 0;
62
3.19M
    tab->var[i].is_zero = 0;
63
3.19M
    tab->var[i].is_redundant = 0;
64
3.19M
    tab->var[i].frozen = 0;
65
3.19M
    tab->var[i].negated = 0;
66
3.19M
    tab->col_var[i] = i;
67
3.19M
  }
68
724k
  tab->n_row = 0;
69
724k
  tab->n_con = 0;
70
724k
  tab->n_eq = 0;
71
724k
  tab->max_con = n_row;
72
724k
  tab->n_col = n_var;
73
724k
  tab->n_var = n_var;
74
724k
  tab->max_var = n_var;
75
724k
  tab->n_param = 0;
76
724k
  tab->n_div = 0;
77
724k
  tab->n_dead = 0;
78
724k
  tab->n_redundant = 0;
79
724k
  tab->strict_redundant = 0;
80
724k
  tab->need_undo = 0;
81
724k
  tab->rational = 0;
82
724k
  tab->empty = 0;
83
724k
  tab->in_undo = 0;
84
724k
  tab->M = M;
85
724k
  tab->cone = 0;
86
724k
  tab->bottom.type = isl_tab_undo_bottom;
87
724k
  tab->bottom.next = NULL;
88
724k
  tab->top = &tab->bottom;
89
724k
90
724k
  tab->n_zero = 0;
91
724k
  tab->n_unbounded = 0;
92
724k
  tab->basis = NULL;
93
724k
94
724k
  return tab;
95
0
error:
96
0
  isl_tab_free(tab);
97
0
  return NULL;
98
724k
}
99
100
isl_ctx *isl_tab_get_ctx(struct isl_tab *tab)
101
3.21M
{
102
3.21M
  return tab ? isl_mat_get_ctx(tab->mat) : NULL;
103
3.21M
}
104
105
int isl_tab_extend_cons(struct isl_tab *tab, unsigned n_new)
106
924k
{
107
924k
  unsigned off;
108
924k
109
924k
  if (!tab)
110
0
    return -1;
111
924k
112
924k
  off = 2 + tab->M;
113
924k
114
924k
  if (
tab->max_con < tab->n_con + n_new924k
) {
115
91.1k
    struct isl_tab_var *con;
116
91.1k
117
91.1k
    con = isl_realloc_array(tab->mat->ctx, tab->con,
118
91.1k
            struct isl_tab_var, tab->max_con + n_new);
119
91.1k
    if (!con)
120
0
      return -1;
121
91.1k
    tab->con = con;
122
91.1k
    tab->max_con += n_new;
123
91.1k
  }
124
924k
  
if (924k
tab->mat->n_row < tab->n_row + n_new924k
) {
125
94.2k
    int *row_var;
126
94.2k
127
94.2k
    tab->mat = isl_mat_extend(tab->mat,
128
94.2k
          tab->n_row + n_new, off + tab->n_col);
129
94.2k
    if (!tab->mat)
130
0
      return -1;
131
94.2k
    
row_var = 94.2k
isl_realloc_array94.2k
(tab->mat->ctx, tab->row_var,
132
94.2k
              int, tab->mat->n_row);
133
94.2k
    if (!row_var)
134
0
      return -1;
135
94.2k
    tab->row_var = row_var;
136
94.2k
    if (
tab->row_sign94.2k
) {
137
190
      enum isl_tab_row_sign *s;
138
190
      s = isl_realloc_array(tab->mat->ctx, tab->row_sign,
139
190
          enum isl_tab_row_sign, tab->mat->n_row);
140
190
      if (!s)
141
0
        return -1;
142
190
      tab->row_sign = s;
143
190
    }
144
94.2k
  }
145
924k
  return 0;
146
924k
}
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
5.56k
{
153
5.56k
  struct isl_tab_var *var;
154
5.56k
  unsigned off = 2 + tab->M;
155
5.56k
156
5.56k
  if (
tab->max_var < tab->n_var + n_new5.56k
) {
157
4.33k
    var = isl_realloc_array(tab->mat->ctx, tab->var,
158
4.33k
            struct isl_tab_var, tab->n_var + n_new);
159
4.33k
    if (!var)
160
0
      return -1;
161
4.33k
    tab->var = var;
162
4.33k
    tab->max_var = tab->n_var + n_new;
163
4.33k
  }
164
5.56k
165
5.56k
  
if (5.56k
tab->mat->n_col < off + tab->n_col + n_new5.56k
) {
166
3.05k
    int *p;
167
3.05k
168
3.05k
    tab->mat = isl_mat_extend(tab->mat,
169
3.05k
            tab->mat->n_row, off + tab->n_col + n_new);
170
3.05k
    if (!tab->mat)
171
0
      return -1;
172
3.05k
    
p = 3.05k
isl_realloc_array3.05k
(tab->mat->ctx, tab->col_var,
173
3.05k
              int, tab->n_col + n_new);
174
3.05k
    if (!p)
175
0
      return -1;
176
3.05k
    tab->col_var = p;
177
3.05k
  }
178
5.56k
179
5.56k
  return 0;
180
5.56k
}
181
182
static void free_undo_record(struct isl_tab_undo *undo)
183
3.49M
{
184
3.49M
  switch (undo->type) {
185
783
  case isl_tab_undo_saved_basis:
186
783
    free(undo->u.col_var);
187
783
    break;
188
3.49M
  default:;
189
3.49M
  }
190
3.49M
  free(undo);
191
3.49M
}
192
193
static void free_undo(struct isl_tab *tab)
194
730k
{
195
730k
  struct isl_tab_undo *undo, *next;
196
730k
197
1.25M
  for (undo = tab->top; 
undo && 1.25M
undo != &tab->bottom1.25M
;
undo = next528k
) {
198
528k
    next = undo->next;
199
528k
    free_undo_record(undo);
200
528k
  }
201
730k
  tab->top = undo;
202
730k
}
203
204
void isl_tab_free(struct isl_tab *tab)
205
762k
{
206
762k
  if (!tab)
207
32.4k
    return;
208
730k
  free_undo(tab);
209
730k
  isl_mat_free(tab->mat);
210
730k
  isl_vec_free(tab->dual);
211
730k
  isl_basic_map_free(tab->bmap);
212
730k
  free(tab->var);
213
730k
  free(tab->con);
214
730k
  free(tab->row_var);
215
730k
  free(tab->col_var);
216
730k
  free(tab->row_sign);
217
730k
  isl_mat_free(tab->samples);
218
730k
  free(tab->sample_index);
219
730k
  isl_mat_free(tab->basis);
220
730k
  free(tab);
221
730k
}
222
223
struct isl_tab *isl_tab_dup(struct isl_tab *tab)
224
2.85k
{
225
2.85k
  int i;
226
2.85k
  struct isl_tab *dup;
227
2.85k
  unsigned off;
228
2.85k
229
2.85k
  if (!tab)
230
0
    return NULL;
231
2.85k
232
2.85k
  off = 2 + tab->M;
233
2.85k
  dup = isl_calloc_type(tab->mat->ctx, struct isl_tab);
234
2.85k
  if (!dup)
235
0
    return NULL;
236
2.85k
  dup->mat = isl_mat_dup(tab->mat);
237
2.85k
  if (!dup->mat)
238
0
    goto error;
239
2.85k
  
dup->var = 2.85k
isl_alloc_array2.85k
(tab->mat->ctx, struct isl_tab_var, tab->max_var);
240
2.85k
  if (
tab->max_var && 2.85k
!dup->var2.85k
)
241
0
    goto error;
242
26.0k
  
for (i = 0; 2.85k
i < tab->n_var26.0k
;
++i23.1k
)
243
23.1k
    dup->var[i] = tab->var[i];
244
2.85k
  dup->con = isl_alloc_array(tab->mat->ctx, struct isl_tab_var, tab->max_con);
245
2.85k
  if (
tab->max_con && 2.85k
!dup->con2.85k
)
246
0
    goto error;
247
29.0k
  
for (i = 0; 2.85k
i < tab->n_con29.0k
;
++i26.1k
)
248
26.1k
    dup->con[i] = tab->con[i];
249
2.85k
  dup->col_var = isl_alloc_array(tab->mat->ctx, int, tab->mat->n_col - off);
250
2.85k
  if (
(tab->mat->n_col - off) && 2.85k
!dup->col_var2.85k
)
251
0
    goto error;
252
13.1k
  
for (i = 0; 2.85k
i < tab->n_col13.1k
;
++i10.2k
)
253
10.2k
    dup->col_var[i] = tab->col_var[i];
254
2.85k
  dup->row_var = isl_alloc_array(tab->mat->ctx, int, tab->mat->n_row);
255
2.85k
  if (
tab->mat->n_row && 2.85k
!dup->row_var2.85k
)
256
0
    goto error;
257
28.8k
  
for (i = 0; 2.85k
i < tab->n_row28.8k
;
++i26.0k
)
258
26.0k
    dup->row_var[i] = tab->row_var[i];
259
2.85k
  if (
tab->row_sign2.85k
) {
260
2.84k
    dup->row_sign = isl_alloc_array(tab->mat->ctx, enum isl_tab_row_sign,
261
2.84k
            tab->mat->n_row);
262
2.84k
    if (
tab->mat->n_row && 2.84k
!dup->row_sign2.84k
)
263
0
      goto error;
264
28.8k
    
for (i = 0; 2.84k
i < tab->n_row28.8k
;
++i25.9k
)
265
25.9k
      dup->row_sign[i] = tab->row_sign[i];
266
2.84k
  }
267
2.85k
  
if (2.85k
tab->samples2.85k
) {
268
0
    dup->samples = isl_mat_dup(tab->samples);
269
0
    if (!dup->samples)
270
0
      goto error;
271
0
    
dup->sample_index = 0
isl_alloc_array0
(tab->mat->ctx, int,
272
0
              tab->samples->n_row);
273
0
    if (
tab->samples->n_row && 0
!dup->sample_index0
)
274
0
      goto error;
275
0
    dup->n_sample = tab->n_sample;
276
0
    dup->n_outside = tab->n_outside;
277
0
  }
278
2.85k
  dup->n_row = tab->n_row;
279
2.85k
  dup->n_con = tab->n_con;
280
2.85k
  dup->n_eq = tab->n_eq;
281
2.85k
  dup->max_con = tab->max_con;
282
2.85k
  dup->n_col = tab->n_col;
283
2.85k
  dup->n_var = tab->n_var;
284
2.85k
  dup->max_var = tab->max_var;
285
2.85k
  dup->n_param = tab->n_param;
286
2.85k
  dup->n_div = tab->n_div;
287
2.85k
  dup->n_dead = tab->n_dead;
288
2.85k
  dup->n_redundant = tab->n_redundant;
289
2.85k
  dup->rational = tab->rational;
290
2.85k
  dup->empty = tab->empty;
291
2.85k
  dup->strict_redundant = 0;
292
2.85k
  dup->need_undo = 0;
293
2.85k
  dup->in_undo = 0;
294
2.85k
  dup->M = tab->M;
295
2.85k
  tab->cone = tab->cone;
296
2.85k
  dup->bottom.type = isl_tab_undo_bottom;
297
2.85k
  dup->bottom.next = NULL;
298
2.85k
  dup->top = &dup->bottom;
299
2.85k
300
2.85k
  dup->n_zero = tab->n_zero;
301
2.85k
  dup->n_unbounded = tab->n_unbounded;
302
2.85k
  dup->basis = isl_mat_dup(tab->basis);
303
2.85k
304
2.85k
  return dup;
305
0
error:
306
0
  isl_tab_free(dup);
307
0
  return NULL;
308
2.85k
}
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
2.84k
{
328
2.84k
  int i;
329
2.84k
  struct isl_mat *prod;
330
2.84k
  unsigned n;
331
2.84k
332
2.84k
  prod = isl_mat_alloc(mat1->ctx, mat1->n_row + mat2->n_row,
333
2.84k
          off + col1 + col2);
334
2.84k
  if (!prod)
335
0
    return NULL;
336
2.84k
337
2.84k
  n = 0;
338
9.93k
  for (i = 0; 
i < r19.93k
;
++i7.08k
) {
339
7.08k
    isl_seq_cpy(prod->row[n + i], mat1->row[i], off + d1);
340
7.08k
    isl_seq_clr(prod->row[n + i] + off + d1, d2);
341
7.08k
    isl_seq_cpy(prod->row[n + i] + off + d1 + d2,
342
7.08k
        mat1->row[i] + off + d1, col1 - d1);
343
7.08k
    isl_seq_clr(prod->row[n + i] + off + col1 + d1, col2 - d2);
344
7.08k
  }
345
2.84k
346
2.84k
  n += r1;
347
9.93k
  for (i = 0; 
i < r29.93k
;
++i7.08k
) {
348
7.08k
    isl_seq_cpy(prod->row[n + i], mat2->row[i], off);
349
7.08k
    isl_seq_clr(prod->row[n + i] + off, d1);
350
7.08k
    isl_seq_cpy(prod->row[n + i] + off + d1,
351
7.08k
          mat2->row[i] + off, d2);
352
7.08k
    isl_seq_clr(prod->row[n + i] + off + d1 + d2, col1 - d1);
353
7.08k
    isl_seq_cpy(prod->row[n + i] + off + col1 + d1,
354
7.08k
          mat2->row[i] + off + d2, col2 - d2);
355
7.08k
  }
356
2.84k
357
2.84k
  n += r2;
358
32.5k
  for (i = 0; 
i < row1 - r132.5k
;
++i29.7k
) {
359
29.7k
    isl_seq_cpy(prod->row[n + i], mat1->row[r1 + i], off + d1);
360
29.7k
    isl_seq_clr(prod->row[n + i] + off + d1, d2);
361
29.7k
    isl_seq_cpy(prod->row[n + i] + off + d1 + d2,
362
29.7k
        mat1->row[r1 + i] + off + d1, col1 - d1);
363
29.7k
    isl_seq_clr(prod->row[n + i] + off + col1 + d1, col2 - d2);
364
29.7k
  }
365
2.84k
366
2.84k
  n += row1 - r1;
367
32.5k
  for (i = 0; 
i < row2 - r232.5k
;
++i29.7k
) {
368
29.7k
    isl_seq_cpy(prod->row[n + i], mat2->row[r2 + i], off);
369
29.7k
    isl_seq_clr(prod->row[n + i] + off, d1);
370
29.7k
    isl_seq_cpy(prod->row[n + i] + off + d1,
371
29.7k
          mat2->row[r2 + i] + off, d2);
372
29.7k
    isl_seq_clr(prod->row[n + i] + off + d1 + d2, col1 - d1);
373
29.7k
    isl_seq_cpy(prod->row[n + i] + off + col1 + d1,
374
29.7k
          mat2->row[r2 + i] + off + d2, col2 - d2);
375
29.7k
  }
376
2.84k
377
2.84k
  return prod;
378
2.84k
}
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
52.2k
{
386
52.2k
  if (var->index == -1)
387
225
    return;
388
52.0k
  
if (52.0k
var->is_row && 52.0k
var->index >= r136.7k
)
389
29.7k
    var->index += r2;
390
52.0k
  if (
!var->is_row && 52.0k
var->index >= d115.2k
)
391
14.3k
    var->index += d2;
392
52.2k
}
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
52.2k
{
401
52.2k
  if (var->index == -1)
402
225
    return;
403
52.0k
  
if (52.0k
var->is_row52.0k
) {
404
36.7k
    if (var->index < r2)
405
7.08k
      var->index += r1;
406
36.7k
    else
407
29.7k
      var->index += row1;
408
52.0k
  } else {
409
15.2k
    if (var->index < d2)
410
945
      var->index += d1;
411
15.2k
    else
412
14.3k
      var->index += col1;
413
15.2k
  }
414
52.2k
}
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
2.84k
{
436
2.84k
  int i;
437
2.84k
  struct isl_tab *prod;
438
2.84k
  unsigned off;
439
2.84k
  unsigned r1, r2, d1, d2;
440
2.84k
441
2.84k
  if (
!tab1 || 2.84k
!tab22.84k
)
442
0
    return NULL;
443
2.84k
444
2.84k
  
isl_assert2.84k
(tab1->mat->ctx, tab1->M == tab2->M, return NULL);
445
2.84k
  
isl_assert2.84k
(tab1->mat->ctx, tab1->rational == tab2->rational, return NULL);
446
2.84k
  
isl_assert2.84k
(tab1->mat->ctx, tab1->cone == tab2->cone, return NULL);
447
2.84k
  
isl_assert2.84k
(tab1->mat->ctx, !tab1->row_sign, return NULL);
448
2.84k
  
isl_assert2.84k
(tab1->mat->ctx, !tab2->row_sign, return NULL);
449
2.84k
  
isl_assert2.84k
(tab1->mat->ctx, tab1->n_param == 0, return NULL);
450
2.84k
  
isl_assert2.84k
(tab1->mat->ctx, tab2->n_param == 0, return NULL);
451
2.84k
  
isl_assert2.84k
(tab1->mat->ctx, tab1->n_div == 0, return NULL);
452
2.84k
  
isl_assert2.84k
(tab1->mat->ctx, tab2->n_div == 0, return NULL);
453
2.84k
454
2.84k
  off = 2 + tab1->M;
455
2.84k
  r1 = tab1->n_redundant;
456
2.84k
  r2 = tab2->n_redundant;
457
2.84k
  d1 = tab1->n_dead;
458
2.84k
  d2 = tab2->n_dead;
459
2.84k
  prod = isl_calloc_type(tab1->mat->ctx, struct isl_tab);
460
2.84k
  if (!prod)
461
0
    return NULL;
462
2.84k
  prod->mat = tab_mat_product(tab1->mat, tab2->mat,
463
2.84k
        tab1->n_row, tab2->n_row,
464
2.84k
        tab1->n_col, tab2->n_col, off, r1, r2, d1, d2);
465
2.84k
  if (!prod->mat)
466
0
    goto error;
467
2.84k
  
prod->var = 2.84k
isl_alloc_array2.84k
(tab1->mat->ctx, struct isl_tab_var,
468
2.84k
          tab1->max_var + tab2->max_var);
469
2.84k
  if (
(tab1->max_var + tab2->max_var) && 2.84k
!prod->var2.84k
)
470
0
    goto error;
471
18.3k
  
for (i = 0; 2.84k
i < tab1->n_var18.3k
;
++i15.4k
) {
472
15.4k
    prod->var[i] = tab1->var[i];
473
15.4k
    update_index1(&prod->var[i], r1, r2, d1, d2);
474
15.4k
  }
475
18.3k
  for (i = 0; 
i < tab2->n_var18.3k
;
++i15.4k
) {
476
15.4k
    prod->var[tab1->n_var + i] = tab2->var[i];
477
15.4k
    update_index2(&prod->var[tab1->n_var + i],
478
15.4k
        tab1->n_row, tab1->n_col,
479
15.4k
        r1, r2, d1, d2);
480
15.4k
  }
481
2.84k
  prod->con = isl_alloc_array(tab1->mat->ctx, struct isl_tab_var,
482
2.84k
          tab1->max_con +  tab2->max_con);
483
2.84k
  if (
(tab1->max_con + tab2->max_con) && 2.84k
!prod->con2.84k
)
484
0
    goto error;
485
39.6k
  
for (i = 0; 2.84k
i < tab1->n_con39.6k
;
++i36.7k
) {
486
36.7k
    prod->con[i] = tab1->con[i];
487
36.7k
    update_index1(&prod->con[i], r1, r2, d1, d2);
488
36.7k
  }
489
39.6k
  for (i = 0; 
i < tab2->n_con39.6k
;
++i36.7k
) {
490
36.7k
    prod->con[tab1->n_con + i] = tab2->con[i];
491
36.7k
    update_index2(&prod->con[tab1->n_con + i],
492
36.7k
        tab1->n_row, tab1->n_col,
493
36.7k
        r1, r2, d1, d2);
494
36.7k
  }
495
2.84k
  prod->col_var = isl_alloc_array(tab1->mat->ctx, int,
496
2.84k
          tab1->n_col + tab2->n_col);
497
2.84k
  if (
(tab1->n_col + tab2->n_col) && 2.84k
!prod->col_var2.84k
)
498
0
    goto error;
499
18.1k
  
for (i = 0; 2.84k
i < tab1->n_col18.1k
;
++i15.2k
) {
500
15.2k
    int pos = i < d1 ? 
i945
:
i + d214.3k
;
501
15.2k
    prod->col_var[pos] = tab1->col_var[i];
502
15.2k
  }
503
18.1k
  for (i = 0; 
i < tab2->n_col18.1k
;
++i15.2k
) {
504
15.2k
    int pos = i < d2 ? 
d1 + i945
:
tab1->n_col + i14.3k
;
505
15.2k
    int t = tab2->col_var[i];
506
15.2k
    if (t >= 0)
507
175
      t += tab1->n_var;
508
15.2k
    else
509
15.0k
      t -= tab1->n_con;
510
15.2k
    prod->col_var[pos] = t;
511
15.2k
  }
512
2.84k
  prod->row_var = isl_alloc_array(tab1->mat->ctx, int,
513
2.84k
          tab1->mat->n_row + tab2->mat->n_row);
514
2.84k
  if (
(tab1->mat->n_row + tab2->mat->n_row) && 2.84k
!prod->row_var2.84k
)
515
0
    goto error;
516
39.6k
  
for (i = 0; 2.84k
i < tab1->n_row39.6k
;
++i36.7k
) {
517
36.7k
    int pos = i < r1 ? 
i7.08k
:
i + r229.7k
;
518
36.7k
    prod->row_var[pos] = tab1->row_var[i];
519
36.7k
  }
520
39.6k
  for (i = 0; 
i < tab2->n_row39.6k
;
++i36.7k
) {
521
36.7k
    int pos = i < r2 ? 
r1 + i7.08k
:
tab1->n_row + i29.7k
;
522
36.7k
    int t = tab2->row_var[i];
523
36.7k
    if (t >= 0)
524
15.3k
      t += tab1->n_var;
525
36.7k
    else
526
21.4k
      t -= tab1->n_con;
527
36.7k
    prod->row_var[pos] = t;
528
36.7k
  }
529
2.84k
  prod->samples = NULL;
530
2.84k
  prod->sample_index = NULL;
531
2.84k
  prod->n_row = tab1->n_row + tab2->n_row;
532
2.84k
  prod->n_con = tab1->n_con + tab2->n_con;
533
2.84k
  prod->n_eq = 0;
534
2.84k
  prod->max_con = tab1->max_con + tab2->max_con;
535
2.84k
  prod->n_col = tab1->n_col + tab2->n_col;
536
2.84k
  prod->n_var = tab1->n_var + tab2->n_var;
537
2.84k
  prod->max_var = tab1->max_var + tab2->max_var;
538
2.84k
  prod->n_param = 0;
539
2.84k
  prod->n_div = 0;
540
2.84k
  prod->n_dead = tab1->n_dead + tab2->n_dead;
541
2.84k
  prod->n_redundant = tab1->n_redundant + tab2->n_redundant;
542
2.84k
  prod->rational = tab1->rational;
543
2.84k
  prod->empty = tab1->empty || tab2->empty;
544
2.84k
  prod->strict_redundant = tab1->strict_redundant || tab2->strict_redundant;
545
2.84k
  prod->need_undo = 0;
546
2.84k
  prod->in_undo = 0;
547
2.84k
  prod->M = tab1->M;
548
2.84k
  prod->cone = tab1->cone;
549
2.84k
  prod->bottom.type = isl_tab_undo_bottom;
550
2.84k
  prod->bottom.next = NULL;
551
2.84k
  prod->top = &prod->bottom;
552
2.84k
553
2.84k
  prod->n_zero = 0;
554
2.84k
  prod->n_unbounded = 0;
555
2.84k
  prod->basis = NULL;
556
2.84k
557
2.84k
  return prod;
558
0
error:
559
0
  isl_tab_free(prod);
560
0
  return NULL;
561
2.84k
}
562
563
static struct isl_tab_var *var_from_index(struct isl_tab *tab, int i)
564
112M
{
565
112M
  if (i >= 0)
566
29.9M
    return &tab->var[i];
567
112M
  else
568
82.3M
    return &tab->con[~i];
569
0
}
570
571
struct isl_tab_var *isl_tab_var_from_row(struct isl_tab *tab, int i)
572
84.9M
{
573
84.9M
  return var_from_index(tab, tab->row_var[i]);
574
84.9M
}
575
576
static struct isl_tab_var *var_from_col(struct isl_tab *tab, int i)
577
24.8M
{
578
24.8M
  return var_from_index(tab, tab->col_var[i]);
579
24.8M
}
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
2.06M
{
588
2.06M
  int i;
589
2.06M
  unsigned off = 2 + tab->M;
590
2.06M
591
2.06M
  if (var->is_row)
592
1.41M
    return 0;
593
3.71M
  
for (i = tab->n_redundant; 652k
i < tab->n_row3.71M
;
++i3.06M
) {
594
3.26M
    if (
!3.26M
isl_int_is_neg3.26M
(tab->mat->row[i][off + var->index]))
595
2.81M
      continue;
596
448k
    
if (448k
isl_tab_var_from_row(tab, i)->is_nonneg448k
)
597
205k
      return 0;
598
3.26M
  }
599
447k
  return 1;
600
2.06M
}
601
602
/* Check if there are any lower bounds on column variable "var",
603
 * i.e., non-negative rows where var appears with a positive coefficient.
604
 * Return 1 if there are no such bounds.
605
 */
606
static int min_is_manifestly_unbounded(struct isl_tab *tab,
607
  struct isl_tab_var *var)
608
1.68M
{
609
1.68M
  int i;
610
1.68M
  unsigned off = 2 + tab->M;
611
1.68M
612
1.68M
  if (var->is_row)
613
839k
    return 0;
614
6.35M
  
for (i = tab->n_redundant; 850k
i < tab->n_row6.35M
;
++i5.50M
) {
615
5.99M
    if (
!5.99M
isl_int_is_pos5.99M
(tab->mat->row[i][off + var->index]))
616
5.21M
      continue;
617
783k
    
if (783k
isl_tab_var_from_row(tab, i)->is_nonneg783k
)
618
490k
      return 0;
619
5.99M
  }
620
359k
  return 1;
621
1.68M
}
622
623
static int row_cmp(struct isl_tab *tab, int r1, int r2, int c, isl_int *t)
624
1.52M
{
625
1.52M
  unsigned off = 2 + tab->M;
626
1.52M
627
1.52M
  if (
tab->M1.52M
) {
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
1.52M
  }
635
1.52M
  
isl_int_mul1.52M
(*t, tab->mat->row[r1][1], tab->mat->row[r2][off + c]);
636
1.52M
  isl_int_submul(*t, tab->mat->row[r2][1], tab->mat->row[r1][off + c]);
637
1.52M
  return isl_int_sgn(*t);
638
1.52M
}
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
3.24M
{
663
3.24M
  int j, r, tsgn;
664
3.24M
  isl_int t;
665
3.24M
  unsigned off = 2 + tab->M;
666
3.24M
667
3.24M
  isl_int_init(t);
668
3.24M
  r = -1;
669
39.9M
  for (j = tab->n_redundant; 
j < tab->n_row39.9M
;
++j36.7M
) {
670
36.7M
    if (
var && 36.7M
j == var->index31.2M
)
671
2.82M
      continue;
672
33.8M
    
if (33.8M
!isl_tab_var_from_row(tab, j)->is_nonneg33.8M
)
673
7.55M
      continue;
674
26.3M
    
if (26.3M
sgn * 26.3M
isl_int_sgn26.3M
(tab->mat->row[j][off + c]) >= 0)
675
22.6M
      continue;
676
3.70M
    
if (3.70M
r < 03.70M
) {
677
2.18M
      r = j;
678
2.18M
      continue;
679
2.18M
    }
680
1.52M
    tsgn = sgn * row_cmp(tab, r, j, c, &t);
681
1.52M
    if (
tsgn < 0 || 1.52M
(tsgn == 0 &&
682
522k
              tab->row_var[j] < tab->row_var[r]))
683
766k
      r = j;
684
36.7M
  }
685
3.24M
  isl_int_clear(t);
686
3.24M
  return r;
687
3.24M
}
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
4.23M
{
709
4.23M
  int j, r, c;
710
4.23M
  isl_int *tr;
711
4.23M
712
4.23M
  *row = *col = -1;
713
4.23M
714
4.23M
  isl_assert(tab->mat->ctx, var->is_row, return);
715
4.23M
  tr = tab->mat->row[var->index] + 2 + tab->M;
716
4.23M
717
4.23M
  c = -1;
718
32.8M
  for (j = tab->n_dead; 
j < tab->n_col32.8M
;
++j28.6M
) {
719
28.6M
    if (isl_int_is_zero(tr[j]))
720
22.3M
      continue;
721
6.20M
    
if (6.20M
isl_int_sgn6.20M
(tr[j]) != sgn &&
722
3.48M
        var_from_col(tab, j)->is_nonneg)
723
2.33M
      continue;
724
3.87M
    
if (3.87M
c < 0 || 3.87M
tab->col_var[j] < tab->col_var[c]946k
)
725
3.15M
      c = j;
726
28.6M
  }
727
4.23M
  if (c < 0)
728
1.30M
    return;
729
2.92M
730
2.92M
  
sgn *= 2.92M
isl_int_sgn2.92M
(tr[c]);
731
2.92M
  r = pivot_row(tab, skip_var, sgn, c);
732
2.92M
  *row = r < 0 ? 
var->index1.06M
:
r1.86M
;
733
4.23M
  *col = c;
734
4.23M
}
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
18.4M
{
744
18.4M
  int i;
745
18.4M
  unsigned off = 2 + tab->M;
746
18.4M
747
18.4M
  if (
tab->row_var[row] < 0 && 18.4M
!isl_tab_var_from_row(tab, row)->is_nonneg14.4M
)
748
1.01M
    return 0;
749
17.4M
750
17.4M
  
if (17.4M
isl_int_is_neg17.4M
(tab->mat->row[row][1]))
751
1.50M
    return 0;
752
15.9M
  
if (15.9M
tab->strict_redundant && 15.9M
isl_int_is_zero43
(tab->mat->row[row][1]))
753
42
    return 0;
754
15.9M
  
if (15.9M
tab->M && 15.9M
isl_int_is_neg38.4k
(tab->mat->row[row][2]))
755
2.06k
    return 0;
756
15.9M
757
62.9M
  
for (i = tab->n_dead; 15.9M
i < tab->n_col62.9M
;
++i47.0M
) {
758
61.2M
    if (isl_int_is_zero(tab->mat->row[row][off + i]))
759
42.0M
      continue;
760
19.2M
    
if (19.2M
tab->col_var[i] >= 019.2M
)
761
7.53M
      return 0;
762
11.6M
    
if (11.6M
isl_int_is_neg11.6M
(tab->mat->row[row][off + i]))
763
6.52M
      return 0;
764
5.15M
    
if (5.15M
!var_from_col(tab, i)->is_nonneg5.15M
)
765
172k
      return 0;
766
61.2M
  }
767
1.69M
  return 1;
768
18.4M
}
769
770
static void swap_rows(struct isl_tab *tab, int row1, int row2)
771
1.70M
{
772
1.70M
  int t;
773
1.70M
  enum isl_tab_row_sign s;
774
1.70M
775
1.70M
  t = tab->row_var[row1];
776
1.70M
  tab->row_var[row1] = tab->row_var[row2];
777
1.70M
  tab->row_var[row2] = t;
778
1.70M
  isl_tab_var_from_row(tab, row1)->index = row1;
779
1.70M
  isl_tab_var_from_row(tab, row2)->index = row2;
780
1.70M
  tab->mat = isl_mat_swap_rows(tab->mat, row1, row2);
781
1.70M
782
1.70M
  if (!tab->row_sign)
783
1.69M
    return;
784
9.27k
  s = tab->row_sign[row1];
785
9.27k
  tab->row_sign[row1] = tab->row_sign[row2];
786
9.27k
  tab->row_sign[row2] = s;
787
9.27k
}
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
12.9M
{
794
12.9M
  struct isl_tab_undo *undo;
795
12.9M
796
12.9M
  if (!tab)
797
0
    return -1;
798
12.9M
  
if (12.9M
!tab->need_undo12.9M
)
799
9.44M
    return 0;
800
3.49M
801
3.49M
  
undo = 3.49M
isl_alloc_type3.49M
(tab->mat->ctx, struct isl_tab_undo);
802
3.49M
  if (!undo)
803
0
    return -1;
804
3.49M
  undo->type = type;
805
3.49M
  undo->u = u;
806
3.49M
  undo->next = tab->top;
807
3.49M
  tab->top = undo;
808
3.49M
809
3.49M
  return 0;
810
3.49M
}
811
812
int isl_tab_push_var(struct isl_tab *tab,
813
  enum isl_tab_undo_type type, struct isl_tab_var *var)
814
12.4M
{
815
12.4M
  union isl_tab_undo_val u;
816
12.4M
  if (var->is_row)
817
12.1M
    u.var_index = tab->row_var[var->index];
818
12.4M
  else
819
248k
    u.var_index = tab->col_var[var->index];
820
12.4M
  return push_union(tab, type, u);
821
12.4M
}
822
823
int isl_tab_push(struct isl_tab *tab, enum isl_tab_undo_type type)
824
439k
{
825
439k
  union isl_tab_undo_val u = { 0 };
826
439k
  return push_union(tab, type, u);
827
439k
}
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
783
{
834
783
  int i;
835
783
  union isl_tab_undo_val u;
836
783
837
783
  u.col_var = isl_alloc_array(tab->mat->ctx, int, tab->n_col);
838
783
  if (
tab->n_col && 783
!u.col_var783
)
839
0
    return -1;
840
8.98k
  
for (i = 0; 783
i < tab->n_col8.98k
;
++i8.20k
)
841
8.20k
    u.col_var[i] = tab->col_var[i];
842
783
  return push_union(tab, isl_tab_undo_saved_basis, u);
843
783
}
844
845
int isl_tab_push_callback(struct isl_tab *tab, struct isl_tab_callback *callback)
846
21.1k
{
847
21.1k
  union isl_tab_undo_val u;
848
21.1k
  u.callback = callback;
849
21.1k
  return push_union(tab, isl_tab_undo_callback, u);
850
21.1k
}
851
852
struct isl_tab *isl_tab_init_samples(struct isl_tab *tab)
853
7.57k
{
854
7.57k
  if (!tab)
855
0
    return NULL;
856
7.57k
857
7.57k
  tab->n_sample = 0;
858
7.57k
  tab->n_outside = 0;
859
7.57k
  tab->samples = isl_mat_alloc(tab->mat->ctx, 1, 1 + tab->n_var);
860
7.57k
  if (!tab->samples)
861
0
    goto error;
862
7.57k
  
tab->sample_index = 7.57k
isl_alloc_array7.57k
(tab->mat->ctx, int, 1);
863
7.57k
  if (!tab->sample_index)
864
0
    goto error;
865
7.57k
  return tab;
866
0
error:
867
0
  isl_tab_free(tab);
868
0
  return NULL;
869
7.57k
}
870
871
int isl_tab_add_sample(struct isl_tab *tab, __isl_take isl_vec *sample)
872
11.6k
{
873
11.6k
  if (
!tab || 11.6k
!sample11.6k
)
874
0
    goto error;
875
11.6k
876
11.6k
  
if (11.6k
tab->n_sample + 1 > tab->samples->n_row11.6k
) {
877
3.87k
    int *t = isl_realloc_array(tab->mat->ctx,
878
3.87k
          tab->sample_index, int, tab->n_sample + 1);
879
3.87k
    if (!t)
880
0
      goto error;
881
3.87k
    tab->sample_index = t;
882
3.87k
  }
883
11.6k
884
11.6k
  tab->samples = isl_mat_extend(tab->samples,
885
11.6k
        tab->n_sample + 1, tab->samples->n_col);
886
11.6k
  if (!tab->samples)
887
0
    goto error;
888
11.6k
889
11.6k
  isl_seq_cpy(tab->samples->row[tab->n_sample], sample->el, sample->size);
890
11.6k
  isl_vec_free(sample);
891
11.6k
  tab->sample_index[tab->n_sample] = tab->n_sample;
892
11.6k
  tab->n_sample++;
893
11.6k
894
11.6k
  return 0;
895
0
error:
896
0
  isl_vec_free(sample);
897
0
  return -1;
898
11.6k
}
899
900
struct isl_tab *isl_tab_drop_sample(struct isl_tab *tab, int s)
901
6.50k
{
902
6.50k
  if (
s != tab->n_outside6.50k
) {
903
4.06k
    int t = tab->sample_index[tab->n_outside];
904
4.06k
    tab->sample_index[tab->n_outside] = tab->sample_index[s];
905
4.06k
    tab->sample_index[s] = t;
906
4.06k
    isl_mat_swap_rows(tab->samples, tab->n_outside, s);
907
4.06k
  }
908
6.50k
  tab->n_outside++;
909
6.50k
  if (
isl_tab_push(tab, isl_tab_undo_drop_sample) < 06.50k
) {
910
0
    isl_tab_free(tab);
911
0
    return NULL;
912
0
  }
913
6.50k
914
6.50k
  return tab;
915
6.50k
}
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
27.7k
{
922
27.7k
  union isl_tab_undo_val u;
923
27.7k
924
27.7k
  if (!tab)
925
0
    return -1;
926
27.7k
927
27.7k
  u.n = tab->n_sample;
928
27.7k
  return push_union(tab, isl_tab_undo_saved_samples, u);
929
27.7k
}
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
2.27M
{
945
2.27M
  struct isl_tab_var *var = isl_tab_var_from_row(tab, row);
946
2.27M
  var->is_redundant = 1;
947
2.27M
  isl_assert(tab->mat->ctx, row >= tab->n_redundant, return -1);
948
2.27M
  
if (2.27M
tab->preserve || 2.27M
tab->need_undo1.60M
||
tab->row_var[row] >= 01.48M
) {
949
1.52M
    if (
tab->row_var[row] >= 0 && 1.52M
!var->is_nonneg1.14M
) {
950
1.13M
      var->is_nonneg = 1;
951
1.13M
      if (isl_tab_push_var(tab, isl_tab_undo_nonneg, var) < 0)
952
0
        return -1;
953
1.52M
    }
954
1.52M
    
if (1.52M
row != tab->n_redundant1.52M
)
955
1.06M
      swap_rows(tab, row, tab->n_redundant);
956
1.52M
    tab->n_redundant++;
957
1.52M
    return isl_tab_push_var(tab, isl_tab_undo_redundant, var);
958
0
  } else {
959
747k
    if (row != tab->n_row - 1)
960
501k
      swap_rows(tab, row, tab->n_row - 1);
961
747k
    isl_tab_var_from_row(tab, tab->n_row - 1)->index = -1;
962
747k
    tab->n_row--;
963
747k
    return 1;
964
747k
  }
965
0
}
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
10.3k
{
974
10.3k
  if (!tab)
975
0
    return -1;
976
10.3k
  
if (10.3k
!tab->rational && 10.3k
tab->need_undo10.3k
)
977
10.3k
    
if (10.3k
isl_tab_push(tab, isl_tab_undo_rational) < 010.3k
)
978
0
      return -1;
979
10.3k
  tab->rational = 1;
980
10.3k
  return 0;
981
10.3k
}
982
983
isl_stat isl_tab_mark_empty(struct isl_tab *tab)
984
76.8k
{
985
76.8k
  if (!tab)
986
0
    return isl_stat_error;
987
76.8k
  
if (76.8k
!tab->empty && 76.8k
tab->need_undo75.2k
)
988
64.7k
    
if (64.7k
isl_tab_push(tab, isl_tab_undo_empty) < 064.7k
)
989
0
      return isl_stat_error;
990
76.8k
  tab->empty = 1;
991
76.8k
  return isl_stat_ok;
992
76.8k
}
993
994
int isl_tab_freeze_constraint(struct isl_tab *tab, int con)
995
406k
{
996
406k
  struct isl_tab_var *var;
997
406k
998
406k
  if (!tab)
999
0
    return -1;
1000
406k
1001
406k
  var = &tab->con[con];
1002
406k
  if (var->frozen)
1003
0
    return 0;
1004
406k
  
if (406k
var->index < 0406k
)
1005
43.3k
    return 0;
1006
363k
  var->frozen = 1;
1007
363k
1008
363k
  if (tab->need_undo)
1009
329k
    return isl_tab_push_var(tab, isl_tab_undo_freeze, var);
1010
33.1k
1011
33.1k
  return 0;
1012
33.1k
}
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
3.21M
{
1033
3.21M
  int i;
1034
3.21M
  struct isl_mat *mat = tab->mat;
1035
3.21M
  unsigned off = 2 + tab->M;
1036
3.21M
1037
3.21M
  if (!tab->row_sign)
1038
3.18M
    return;
1039
26.1k
1040
26.1k
  
if (26.1k
tab->row_sign[row] == 026.1k
)
1041
20.4k
    return;
1042
5.71k
  
isl_assert5.71k
(mat->ctx, row_sgn > 0, return);
1043
5.71k
  
isl_assert5.71k
(mat->ctx, tab->row_sign[row] == isl_tab_row_neg, return);
1044
5.71k
  tab->row_sign[row] = isl_tab_row_pos;
1045
51.5k
  for (i = 0; 
i < tab->n_row51.5k
;
++i45.7k
) {
1046
45.7k
    int s;
1047
45.7k
    if (i == row)
1048
5.71k
      continue;
1049
40.0k
    
s = 40.0k
isl_int_sgn40.0k
(mat->row[i][off + col]);
1050
40.0k
    if (!s)
1051
23.1k
      continue;
1052
16.9k
    
if (16.9k
!tab->row_sign[i]16.9k
)
1053
6.67k
      continue;
1054
10.2k
    
if (10.2k
s < 0 && 10.2k
tab->row_sign[i] == isl_tab_row_neg5.77k
)
1055
0
      continue;
1056
10.2k
    
if (10.2k
s > 0 && 10.2k
tab->row_sign[i] == isl_tab_row_pos4.49k
)
1057
4.49k
      continue;
1058
5.77k
    tab->row_sign[i] = isl_tab_row_unknown;
1059
5.77k
  }
1060
3.21M
}
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
3.21M
{
1114
3.21M
  int i, j;
1115
3.21M
  int sgn;
1116
3.21M
  int t;
1117
3.21M
  isl_ctx *ctx;
1118
3.21M
  struct isl_mat *mat = tab->mat;
1119
3.21M
  struct isl_tab_var *var;
1120
3.21M
  unsigned off = 2 + tab->M;
1121
3.21M
1122
3.21M
  ctx = isl_tab_get_ctx(tab);
1123
3.21M
  if (isl_ctx_next_operation(ctx) < 0)
1124
0
    return -1;
1125
3.21M
1126
3.21M
  
isl_int_swap3.21M
(mat->row[row][0], mat->row[row][off + col]);
1127
3.21M
  sgn = isl_int_sgn(mat->row[row][0]);
1128
3.21M
  if (
sgn < 03.21M
) {
1129
1.83M
    isl_int_neg(mat->row[row][0], mat->row[row][0]);
1130
1.83M
    isl_int_neg(mat->row[row][off + col], mat->row[row][off + col]);
1131
1.83M
  } else
1132
11.2M
    
for (j = 0; 1.37M
j < off - 1 + tab->n_col11.2M
;
++j9.88M
) {
1133
9.88M
      if (j == off - 1 + col)
1134
1.37M
        continue;
1135
8.50M
      
isl_int_neg8.50M
(mat->row[row][1 + j], mat->row[row][1 + j]);
1136
8.50M
    }
1137
3.21M
  if (
!3.21M
isl_int_is_one3.21M
(mat->row[row][0]))
1138
561k
    isl_seq_normalize(mat->ctx, mat->row[row], off + tab->n_col);
1139
39.5M
  for (i = 0; 
i < tab->n_row39.5M
;
++i36.3M
) {
1140
36.3M
    if (i == row)
1141
3.21M
      continue;
1142
33.1M
    
if (33.1M
isl_int_is_zero33.1M
(mat->row[i][off + col]))
1143
24.3M
      continue;
1144
8.79M
    
isl_int_mul8.79M
(mat->row[i][0], mat->row[i][0], mat->row[row][0]);
1145
92.8M
    for (j = 0; 
j < off - 1 + tab->n_col92.8M
;
++j84.0M
) {
1146
84.0M
      if (j == off - 1 + col)
1147
8.79M
        continue;
1148
75.2M
      
isl_int_mul75.2M
(mat->row[i][1 + j],
1149
75.2M
            mat->row[i][1 + j], mat->row[row][0]);
1150
75.2M
      isl_int_addmul(mat->row[i][1 + j],
1151
84.0M
            mat->row[i][off + col], mat->row[row][1 + j]);
1152
84.0M
    }
1153
8.79M
    isl_int_mul(mat->row[i][off + col],
1154
8.79M
          mat->row[i][off + col], mat->row[row][off + col]);
1155
8.79M
    if (
!8.79M
isl_int_is_one8.79M
(mat->row[i][0]))
1156
3.96M
      isl_seq_normalize(mat->ctx, mat->row[i], off + tab->n_col);
1157
36.3M
  }
1158
3.21M
  t = tab->row_var[row];
1159
3.21M
  tab->row_var[row] = tab->col_var[col];
1160
3.21M
  tab->col_var[col] = t;
1161
3.21M
  var = isl_tab_var_from_row(tab, row);
1162
3.21M
  var->is_row = 1;
1163
3.21M
  var->index = row;
1164
3.21M
  var = var_from_col(tab, col);
1165
3.21M
  var->is_row = 0;
1166
3.21M
  var->index = col;
1167
3.21M
  update_row_sign(tab, row, col, sgn);
1168
3.21M
  if (tab->in_undo)
1169
116k
    return 0;
1170
31.3M
  
for (i = tab->n_redundant; 3.09M
i < tab->n_row31.3M
;
++i28.2M
) {
1171
28.2M
    if (isl_int_is_zero(mat->row[i][off + col]))
1172
17.5M
      continue;
1173
10.6M
    
if (10.6M
!isl_tab_var_from_row(tab, i)->frozen &&
1174
10.6M
        
isl_tab_row_is_redundant(tab, i)10.3M
) {
1175
1.57M
      int redo = isl_tab_mark_redundant(tab, i);
1176
1.57M
      if (redo < 0)
1177
0
        return -1;
1178
1.57M
      
if (1.57M
redo1.57M
)
1179
148k
        --i;
1180
1.57M
    }
1181
28.2M
  }
1182
3.09M
  return 0;
1183
3.21M
}
1184
1185
/* If "var" represents a column variable, then pivot is up (sgn > 0)
1186
 * or down (sgn < 0) to a row.  The variable is assumed not to be
1187
 * unbounded in the specified direction.
1188
 * If sgn = 0, then the variable is unbounded in both directions,
1189
 * and we pivot with any row we can find.
1190
 */
1191
static int to_row(struct isl_tab *tab, struct isl_tab_var *var, int sign) WARN_UNUSED;
1192
static int to_row(struct isl_tab *tab, struct isl_tab_var *var, int sign)
1193
1.65M
{
1194
1.65M
  int r;
1195
1.65M
  unsigned off = 2 + tab->M;
1196
1.65M
1197
1.65M
  if (var->is_row)
1198
1.41M
    return 0;
1199
238k
1200
238k
  
if (238k
sign == 0238k
) {
1201
47.8k
    for (r = tab->n_redundant; 
r < tab->n_row47.8k
;
++r29.5k
)
1202
47.8k
      
if (47.8k
!47.8k
isl_int_is_zero47.8k
(tab->mat->row[r][off+var->index]))
1203
18.3k
        break;
1204
18.3k
    isl_assert(tab->mat->ctx, r < tab->n_row, return -1);
1205
238k
  } else {
1206
220k
    r = pivot_row(tab, NULL, sign, var->index);
1207
220k
    isl_assert(tab->mat->ctx, r >= 0, return -1);
1208
220k
  }
1209
238k
1210
238k
  return isl_tab_pivot(tab, r, var->index);
1211
1.65M
}
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
1.26M
{
1253
1.26M
  int row, col;
1254
1.26M
1255
1.26M
  if (max_is_manifestly_unbounded(tab, var))
1256
95.6k
    return 1;
1257
1.17M
  
if (1.17M
to_row(tab, var, 1) < 01.17M
)
1258
0
    return -2;
1259
1.98M
  
while (1.17M
!1.98M
isl_int_is_pos1.98M
(tab->mat->row[var->index][1])) {
1260
1.60M
    find_pivot(tab, var, var, 1, &row, &col);
1261
1.60M
    if (row == -1)
1262
563k
      
return 563k
isl_int_sgn563k
(tab->mat->row[var->index][1]);
1263
1.03M
    
if (1.03M
isl_tab_pivot(tab, row, col) < 01.03M
)
1264
0
      return -2;
1265
1.03M
    
if (1.03M
!var->is_row1.03M
) /* manifestly unbounded */
1266
222k
      return 1;
1267
1.60M
  }
1268
386k
  return 1;
1269
1.26M
}
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);
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
5.14M
{
1287
5.14M
  if (!tab->M)
1288
5.14M
    
return 5.14M
isl_int_is_neg5.14M
(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
5.14M
}
1295
1296
static int row_sgn(struct isl_tab *tab, int row)
1297
4.56M
{
1298
4.56M
  if (!tab->M)
1299
4.56M
    
return 4.56M
isl_int_sgn4.56M
(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
4.92M
{
1313
4.92M
  int row, col;
1314
4.92M
1315
5.14M
  while (
row_is_neg(tab, var->index)5.14M
) {
1316
658k
    find_pivot(tab, var, var, 1, &row, &col);
1317
658k
    if (row == -1)
1318
73.4k
      break;
1319
585k
    
if (585k
isl_tab_pivot(tab, row, col) < 0585k
)
1320
0
      return -2;
1321
585k
    
if (585k
!var->is_row585k
) /* manifestly unbounded */
1322
365k
      return 1;
1323
658k
  }
1324
4.56M
  return row_sgn(tab, var->index);
1325
4.92M
}
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
192k
{
1334
192k
  int row, col;
1335
192k
1336
214k
  while (
isl_int_is_neg214k
(tab->mat->row[var->index][1])) {
1337
204k
    find_pivot(tab, var, var, 1, &row, &col);
1338
204k
    if (row == -1)
1339
102k
      break;
1340
101k
    
if (101k
row == var->index101k
) /* manifestly unbounded */
1341
80.3k
      return 1;
1342
21.3k
    
if (21.3k
isl_tab_pivot(tab, row, col) < 021.3k
)
1343
0
      return -1;
1344
204k
  }
1345
112k
  
return !112k
isl_int_is_neg112k
(tab->mat->row[var->index][1]);
1346
192k
}
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.5k
{
1367
70.5k
  int row, col;
1368
70.5k
  struct isl_tab_var *pivot_var = NULL;
1369
70.5k
1370
70.5k
  if (min_is_manifestly_unbounded(tab, var))
1371
0
    return -1;
1372
70.5k
  
if (70.5k
!var->is_row70.5k
) {
1373
1.86k
    col = var->index;
1374
1.86k
    row = pivot_row(tab, NULL, -1, col);
1375
1.86k
    pivot_var = var_from_col(tab, col);
1376
1.86k
    if (isl_tab_pivot(tab, row, col) < 0)
1377
0
      return -2;
1378
1.86k
    
if (1.86k
var->is_redundant1.86k
)
1379
317
      return 0;
1380
1.54k
    
if (1.54k
isl_int_is_neg1.54k
(tab->mat->row[var->index][1])) {
1381
693
      if (
var->is_nonneg693
) {
1382
693
        if (!pivot_var->is_redundant &&
1383
693
            
pivot_var->index == row693
) {
1384
680
          if (isl_tab_pivot(tab, row, col) < 0)
1385
0
            return -2;
1386
693
        } else
1387
13
          
if (13
restore_row(tab, var) < -113
)
1388
0
            return -2;
1389
693
      }
1390
693
      return -1;
1391
693
    }
1392
1.86k
  }
1393
69.5k
  
if (69.5k
var->is_redundant69.5k
)
1394
0
    return 0;
1395
107k
  
while (69.5k
!107k
isl_int_is_neg107k
(tab->mat->row[var->index][1])) {
1396
93.6k
    find_pivot(tab, var, var, -1, &row, &col);
1397
93.6k
    if (row == var->index)
1398
16.2k
      return -1;
1399
77.4k
    
if (77.4k
row == -177.4k
)
1400
38.7k
      
return 38.7k
isl_int_sgn38.7k
(tab->mat->row[var->index][1]);
1401
38.6k
    pivot_var = var_from_col(tab, col);
1402
38.6k
    if (isl_tab_pivot(tab, row, col) < 0)
1403
0
      return -2;
1404
38.6k
    
if (38.6k
var->is_redundant38.6k
)
1405
1.14k
      return 0;
1406
93.6k
  }
1407
13.3k
  
if (13.3k
pivot_var && 13.3k
var->is_nonneg13.3k
) {
1408
968
    /* pivot back to non-negative value */
1409
968
    if (
!pivot_var->is_redundant && 968
pivot_var->index == row968
) {
1410
955
      if (isl_tab_pivot(tab, row, col) < 0)
1411
0
        return -2;
1412
968
    } else
1413
13
      
if (13
restore_row(tab, var) < -113
)
1414
0
        return -2;
1415
13.3k
  }
1416
13.3k
  return -1;
1417
13.3k
}
1418
1419
static int row_at_most_neg_one(struct isl_tab *tab, int row)
1420
283k
{
1421
283k
  if (
tab->M283k
) {
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
283k
  }
1427
283k
  
return 283k
isl_int_is_neg283k
(tab->mat->row[row][1]) &&
1428
161k
         isl_int_abs_ge(tab->mat->row[row][1],
1429
283k
            tab->mat->row[row][0]);
1430
283k
}
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
858k
{
1443
858k
  int row, col;
1444
858k
  struct isl_tab_var *pivot_var;
1445
858k
1446
858k
  if (min_is_manifestly_unbounded(tab, var))
1447
379
    return 1;
1448
858k
  
if (858k
!var->is_row858k
) {
1449
89.8k
    col = var->index;
1450
89.8k
    row = pivot_row(tab, NULL, -1, col);
1451
89.8k
    pivot_var = var_from_col(tab, col);
1452
89.8k
    if (isl_tab_pivot(tab, row, col) < 0)
1453
0
      return -1;
1454
89.8k
    
if (89.8k
var->is_redundant89.8k
)
1455
12.3k
      return 0;
1456
77.4k
    
if (77.4k
row_at_most_neg_one(tab, var->index)77.4k
) {
1457
59.1k
      if (
var->is_nonneg59.1k
) {
1458
59.1k
        if (!pivot_var->is_redundant &&
1459
59.1k
            
pivot_var->index == row59.1k
) {
1460
55.6k
          if (isl_tab_pivot(tab, row, col) < 0)
1461
0
            return -1;
1462
59.1k
        } else
1463
3.52k
          
if (3.52k
restore_row(tab, var) < -13.52k
)
1464
0
            return -1;
1465
59.1k
      }
1466
59.1k
      return 1;
1467
59.1k
    }
1468
89.8k
  }
1469
787k
  
if (787k
var->is_redundant787k
)
1470
10.6k
    return 0;
1471
776k
  
do 776k
{
1472
904k
    find_pivot(tab, var, var, -1, &row, &col);
1473
904k
    if (
row == var->index904k
) {
1474
364k
      if (
var->is_nonneg && 364k
restore_row(tab, var) < -1301k
)
1475
0
        return -1;
1476
364k
      return 1;
1477
364k
    }
1478
540k
    
if (540k
row == -1540k
)
1479
238k
      return 0;
1480
301k
    pivot_var = var_from_col(tab, col);
1481
301k
    if (isl_tab_pivot(tab, row, col) < 0)
1482
0
      return -1;
1483
301k
    
if (301k
var->is_redundant301k
)
1484
95.5k
      return 0;
1485
206k
  } while (!row_at_most_neg_one(tab, var->index));
1486
77.3k
  
if (77.3k
var->is_nonneg77.3k
) {
1487
64.1k
    /* pivot back to non-negative value */
1488
64.1k
    if (
!pivot_var->is_redundant && 64.1k
pivot_var->index == row64.1k
)
1489
59.4k
      
if (59.4k
isl_tab_pivot(tab, row, col) < 059.4k
)
1490
0
        return -1;
1491
64.1k
    
if (64.1k
restore_row(tab, var) < -164.1k
)
1492
0
      return -1;
1493
77.3k
  }
1494
77.3k
  return 1;
1495
77.3k
}
1496
1497
/* Return 1 if "var" can attain values >= 1.
1498
 * Return 0 otherwise.
1499
 */
1500
static int at_least_one(struct isl_tab *tab, struct isl_tab_var *var)
1501
679k
{
1502
679k
  int row, col;
1503
679k
  isl_int *r;
1504
679k
1505
679k
  if (max_is_manifestly_unbounded(tab, var))
1506
317k
    return 1;
1507
362k
  
if (362k
to_row(tab, var, 1) < 0362k
)
1508
0
    return -1;
1509
362k
  r = tab->mat->row[var->index];
1510
376k
  while (
isl_int_lt376k
(r[1], r[0])) {
1511
14.5k
    find_pivot(tab, var, var, 1, &row, &col);
1512
14.5k
    if (row == -1)
1513
495
      
return 495
isl_int_ge495
(r[1], r[0]);
1514
14.0k
    
if (14.0k
row == var->index14.0k
) /* manifestly unbounded */
1515
87
      return 1;
1516
13.9k
    
if (13.9k
isl_tab_pivot(tab, row, col) < 013.9k
)
1517
0
      return -1;
1518
14.5k
  }
1519
361k
  return 1;
1520
679k
}
1521
1522
static void swap_cols(struct isl_tab *tab, int col1, int col2)
1523
616k
{
1524
616k
  int t;
1525
616k
  unsigned off = 2 + tab->M;
1526
616k
  t = tab->col_var[col1];
1527
616k
  tab->col_var[col1] = tab->col_var[col2];
1528
616k
  tab->col_var[col2] = t;
1529
616k
  var_from_col(tab, col1)->index = col1;
1530
616k
  var_from_col(tab, col2)->index = col2;
1531
616k
  tab->mat = isl_mat_swap_cols(tab->mat, off + col1, off + col2);
1532
616k
}
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
875k
{
1548
875k
  var_from_col(tab, col)->is_zero = 1;
1549
875k
  if (
tab->need_undo875k
) {
1550
99.6k
    if (isl_tab_push_var(tab, isl_tab_undo_zero,
1551
99.6k
              var_from_col(tab, col)) < 0)
1552
0
      return -1;
1553
99.6k
    
if (99.6k
col != tab->n_dead99.6k
)
1554
41.1k
      swap_cols(tab, col, tab->n_dead);
1555
99.6k
    tab->n_dead++;
1556
99.6k
    return 0;
1557
0
  } else {
1558
775k
    if (col != tab->n_col - 1)
1559
574k
      swap_cols(tab, col, tab->n_col - 1);
1560
775k
    var_from_col(tab, tab->n_col - 1)->index = -1;
1561
775k
    tab->n_col--;
1562
775k
    return 1;
1563
775k
  }
1564
0
}
1565
1566
static int row_is_manifestly_non_integral(struct isl_tab *tab, int row)
1567
3.07M
{
1568
3.07M
  unsigned off = 2 + tab->M;
1569
3.07M
1570
3.07M
  if (
tab->M && 3.07M
!0
isl_int_eq0
(tab->mat->row[row][2],
1571
3.07M
          tab->mat->row[row][0]))
1572
0
    return 0;
1573
3.07M
  
if (3.07M
isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
1574
3.07M
            tab->n_col - tab->n_dead) != -1)
1575
255k
    return 0;
1576
2.82M
1577
2.82M
  
return !2.82M
isl_int_is_divisible_by2.82M
(tab->mat->row[row][1],
1578
3.07M
          tab->mat->row[row][0]);
1579
3.07M
}
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
563k
{
1586
563k
  int i;
1587
563k
1588
563k
  if (tab->empty)
1589
0
    return 1;
1590
563k
  
if (563k
tab->rational563k
)
1591
12.3k
    return 0;
1592
551k
1593
6.65M
  
for (i = 0; 551k
i < tab->n_var6.65M
;
++i6.10M
) {
1594
6.10M
    if (!tab->var[i].is_row)
1595
3.02M
      continue;
1596
3.07M
    
if (3.07M
row_is_manifestly_non_integral(tab, tab->var[i].index)3.07M
)
1597
32
      return 1;
1598
6.10M
  }
1599
551k
1600
550k
  return 0;
1601
563k
}
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
563k
{
1625
563k
  int j;
1626
563k
  struct isl_mat *mat = tab->mat;
1627
563k
  unsigned off = 2 + tab->M;
1628
563k
1629
563k
  if (!var->is_nonneg)
1630
0
    isl_die(isl_tab_get_ctx(tab), isl_error_internal,
1631
563k
      "expecting non-negative variable",
1632
563k
      return isl_stat_error);
1633
563k
  var->is_zero = 1;
1634
563k
  if (
!temp_var && 563k
tab->need_undo550k
)
1635
376
    
if (376
isl_tab_push_var(tab, isl_tab_undo_zero, var) < 0376
)
1636
0
      return isl_stat_error;
1637
4.28M
  
for (j = tab->n_dead; 563k
j < tab->n_col4.28M
;
++j3.71M
) {
1638
3.71M
    int recheck;
1639
3.71M
    if (isl_int_is_zero(mat->row[var->index][off + j]))
1640
3.22M
      continue;
1641
493k
    
if (493k
isl_int_is_pos493k
(mat->row[var->index][off + j]))
1642
0
      isl_die(isl_tab_get_ctx(tab), isl_error_internal,
1643
493k
        "row cannot have positive coefficients",
1644
493k
        return isl_stat_error);
1645
493k
    recheck = isl_tab_kill_col(tab, j);
1646
493k
    if (recheck < 0)
1647
0
      return isl_stat_error;
1648
493k
    
if (493k
recheck493k
)
1649
480k
      --j;
1650
3.71M
  }
1651
563k
  
if (563k
!temp_var && 563k
isl_tab_mark_redundant(tab, var->index) < 0550k
)
1652
0
    return isl_stat_error;
1653
563k
  
if (563k
tab_is_manifestly_empty(tab) && 563k
isl_tab_mark_empty(tab) < 032
)
1654
0
    return isl_stat_error;
1655
563k
  return isl_stat_ok;
1656
563k
}
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
5.28M
{
1666
5.28M
  int r;
1667
5.28M
1668
5.28M
  isl_assert(tab->mat->ctx, tab->n_row < tab->mat->n_row, return -1);
1669
5.28M
  
isl_assert5.28M
(tab->mat->ctx, tab->n_con < tab->max_con, return -1);
1670
5.28M
1671
5.28M
  r = tab->n_con;
1672
5.28M
  tab->con[r].index = tab->n_row;
1673
5.28M
  tab->con[r].is_row = 1;
1674
5.28M
  tab->con[r].is_nonneg = 0;
1675
5.28M
  tab->con[r].is_zero = 0;
1676
5.28M
  tab->con[r].is_redundant = 0;
1677
5.28M
  tab->con[r].frozen = 0;
1678
5.28M
  tab->con[r].negated = 0;
1679
5.28M
  tab->row_var[tab->n_row] = ~r;
1680
5.28M
1681
5.28M
  tab->n_row++;
1682
5.28M
  tab->n_con++;
1683
5.28M
  if (isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->con[r]) < 0)
1684
0
    return -1;
1685
5.28M
1686
5.28M
  return r;
1687
5.28M
}
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
5.62k
{
1696
5.62k
  int i;
1697
5.62k
1698
5.62k
  if (tab->n_var >= tab->max_var)
1699
0
    isl_die(isl_tab_get_ctx(tab), isl_error_internal,
1700
5.62k
      "not enough room for new variable", return -1);
1701
5.62k
  
if (5.62k
first > tab->n_var5.62k
)
1702
0
    isl_die(isl_tab_get_ctx(tab), isl_error_internal,
1703
5.62k
      "invalid initial position", return -1);
1704
5.62k
1705
6.36k
  
for (i = tab->n_var - 1; 5.62k
i >= first6.36k
;
--i747
) {
1706
747
    tab->var[i + 1] = tab->var[i];
1707
747
    if (tab->var[i + 1].is_row)
1708
501
      tab->row_var[tab->var[i + 1].index]++;
1709
747
    else
1710
246
      tab->col_var[tab->var[i + 1].index]++;
1711
747
  }
1712
5.62k
1713
5.62k
  tab->n_var++;
1714
5.62k
1715
5.62k
  return 0;
1716
5.62k
}
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
3.27k
{
1725
3.27k
  int i;
1726
3.27k
1727
3.27k
  if (first >= tab->n_var)
1728
0
    isl_die(isl_tab_get_ctx(tab), isl_error_internal,
1729
3.27k
      "invalid initial position", return -1);
1730
3.27k
1731
3.27k
  tab->n_var--;
1732
3.27k
1733
3.76k
  for (i = first; 
i < tab->n_var3.76k
;
++i487
) {
1734
487
    tab->var[i] = tab->var[i + 1];
1735
487
    if (tab->var[i + 1].is_row)
1736
481
      tab->row_var[tab->var[i].index]--;
1737
487
    else
1738
6
      tab->col_var[tab->var[i].index]--;
1739
487
  }
1740
3.27k
1741
3.27k
  return 0;
1742
3.27k
}
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
5.62k
{
1750
5.62k
  int i;
1751
5.62k
  unsigned off = 2 + tab->M;
1752
5.62k
1753
5.62k
  isl_assert(tab->mat->ctx, tab->n_col < tab->mat->n_col, return -1);
1754
5.62k
1755
5.62k
  
if (5.62k
var_insert_entry(tab, r) < 05.62k
)
1756
0
    return -1;
1757
5.62k
1758
5.62k
  tab->var[r].index = tab->n_col;
1759
5.62k
  tab->var[r].is_row = 0;
1760
5.62k
  tab->var[r].is_nonneg = 0;
1761
5.62k
  tab->var[r].is_zero = 0;
1762
5.62k
  tab->var[r].is_redundant = 0;
1763
5.62k
  tab->var[r].frozen = 0;
1764
5.62k
  tab->var[r].negated = 0;
1765
5.62k
  tab->col_var[tab->n_col] = r;
1766
5.62k
1767
26.1k
  for (i = 0; 
i < tab->n_row26.1k
;
++i20.5k
)
1768
20.5k
    isl_int_set_si(tab->mat->row[i][off + tab->n_col], 0);
1769
5.62k
1770
5.62k
  tab->n_col++;
1771
5.62k
  if (isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->var[r]) < 0)
1772
0
    return -1;
1773
5.62k
1774
5.62k
  return r;
1775
5.62k
}
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
5.28M
{
1812
5.28M
  int i;
1813
5.28M
  int r;
1814
5.28M
  isl_int *row;
1815
5.28M
  isl_int a, b;
1816
5.28M
  unsigned off = 2 + tab->M;
1817
5.28M
1818
5.28M
  r = isl_tab_allocate_con(tab);
1819
5.28M
  if (r < 0)
1820
0
    return -1;
1821
5.28M
1822
5.28M
  
isl_int_init5.28M
(a);
1823
5.28M
  isl_int_init(b);
1824
5.28M
  row = tab->mat->row[tab->con[r].index];
1825
5.28M
  isl_int_set_si(row[0], 1);
1826
5.28M
  isl_int_set(row[1], line[0]);
1827
5.28M
  isl_seq_clr(row + 2, tab->M + tab->n_col);
1828
47.7M
  for (i = 0; 
i < tab->n_var47.7M
;
++i42.4M
) {
1829
42.4M
    if (tab->var[i].is_zero)
1830
0
      continue;
1831
42.4M
    
if (42.4M
tab->var[i].is_row42.4M
) {
1832
8.48M
      isl_int_lcm(a,
1833
8.48M
        row[0], tab->mat->row[tab->var[i].index][0]);
1834
8.48M
      isl_int_swap(a, row[0]);
1835
8.48M
      isl_int_divexact(a, row[0], a);
1836
8.48M
      isl_int_divexact(b,
1837
8.48M
        row[0], tab->mat->row[tab->var[i].index][0]);
1838
8.48M
      isl_int_mul(b, b, line[1 + i]);
1839
8.48M
      isl_seq_combine(row + 1, a, row + 1,
1840
8.48M
          b, tab->mat->row[tab->var[i].index] + 1,
1841
8.48M
          1 + tab->M + tab->n_col);
1842
8.48M
    } else
1843
34.0M
      isl_int_addmul(row[off + tab->var[i].index],
1844
42.4M
              line[1 + i], row[0]);
1845
42.4M
    if (
tab->M && 42.4M
i >= tab->n_param307k
&&
i < tab->n_var - tab->n_div132k
)
1846
130k
      isl_int_submul(row[2], line[1 + i], row[0]);
1847
42.4M
  }
1848
5.28M
  isl_seq_normalize(tab->mat->ctx, row, off + tab->n_col);
1849
5.28M
  isl_int_clear(a);
1850
5.28M
  isl_int_clear(b);
1851
5.28M
1852
5.28M
  if (tab->row_sign)
1853
37.6k
    tab->row_sign[tab->con[r].index] = isl_tab_row_unknown;
1854
5.28M
1855
5.28M
  return r;
1856
5.28M
}
1857
1858
static isl_stat drop_row(struct isl_tab *tab, int row)
1859
1.20M
{
1860
1.20M
  isl_assert(tab->mat->ctx, ~tab->row_var[row] == tab->n_con - 1,
1861
1.20M
    return isl_stat_error);
1862
1.20M
  
if (1.20M
row != tab->n_row - 11.20M
)
1863
132k
    swap_rows(tab, row, tab->n_row - 1);
1864
1.20M
  tab->n_row--;
1865
1.20M
  tab->n_con--;
1866
1.20M
  return isl_stat_ok;
1867
1.20M
}
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
3.27k
{
1877
3.27k
  int var;
1878
3.27k
1879
3.27k
  var = tab->col_var[col];
1880
3.27k
  if (col != tab->n_col - 1)
1881
571
    swap_cols(tab, col, tab->n_col - 1);
1882
3.27k
  tab->n_col--;
1883
3.27k
  if (var_drop_entry(tab, var) < 0)
1884
0
    return isl_stat_error;
1885
3.27k
  return isl_stat_ok;
1886
3.27k
}
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
4.01M
{
1896
4.01M
  int r;
1897
4.01M
  int sgn;
1898
4.01M
  isl_int cst;
1899
4.01M
1900
4.01M
  if (!tab)
1901
0
    return isl_stat_error;
1902
4.01M
  
if (4.01M
tab->bmap4.01M
) {
1903
352k
    struct isl_basic_map *bmap = tab->bmap;
1904
352k
1905
352k
    isl_assert(tab->mat->ctx, tab->n_eq == bmap->n_eq,
1906
352k
      return isl_stat_error);
1907
352k
    
isl_assert352k
(tab->mat->ctx,
1908
352k
          tab->n_con == bmap->n_eq + bmap->n_ineq,
1909
352k
          return isl_stat_error);
1910
352k
    tab->bmap = isl_basic_map_add_ineq(tab->bmap, ineq);
1911
352k
    if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1912
0
      return isl_stat_error;
1913
352k
    
if (352k
!tab->bmap352k
)
1914
0
      return isl_stat_error;
1915
4.01M
  }
1916
4.01M
  
if (4.01M
tab->cone4.01M
) {
1917
3.09k
    isl_int_init(cst);
1918
3.09k
    isl_int_set_si(cst, 0);
1919
3.09k
    isl_int_swap(ineq[0], cst);
1920
3.09k
  }
1921
4.01M
  r = isl_tab_add_row(tab, ineq);
1922
4.01M
  if (
tab->cone4.01M
) {
1923
3.09k
    isl_int_swap(ineq[0], cst);
1924
3.09k
    isl_int_clear(cst);
1925
3.09k
  }
1926
4.01M
  if (r < 0)
1927
0
    return isl_stat_error;
1928
4.01M
  tab->con[r].is_nonneg = 1;
1929
4.01M
  if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1930
0
    return isl_stat_error;
1931
4.01M
  
if (4.01M
isl_tab_row_is_redundant(tab, tab->con[r].index)4.01M
) {
1932
118k
    if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1933
0
      return isl_stat_error;
1934
118k
    return isl_stat_ok;
1935
118k
  }
1936
3.90M
1937
3.90M
  sgn = restore_row(tab, &tab->con[r]);
1938
3.90M
  if (sgn < -1)
1939
0
    return isl_stat_error;
1940
3.90M
  
if (3.90M
sgn < 03.90M
)
1941
73.4k
    return isl_tab_mark_empty(tab);
1942
3.82M
  
if (3.82M
tab->con[r].is_row && 3.82M
isl_tab_row_is_redundant(tab, tab->con[r].index)3.46M
)
1943
0
    
if (0
isl_tab_mark_redundant(tab, tab->con[r].index) < 00
)
1944
0
      return isl_stat_error;
1945
3.82M
  return isl_stat_ok;
1946
3.82M
}
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
87.3k
{
1954
87.3k
  int i;
1955
87.3k
  int row, col;
1956
87.3k
  unsigned off = 2 + tab->M;
1957
87.3k
1958
87.3k
  if (!var->is_row)
1959
112
    return 0;
1960
87.2k
1961
106k
  
while (87.2k
isl_int_is_pos106k
(tab->mat->row[var->index][1])) {
1962
103k
    find_pivot(tab, var, NULL, -1, &row, &col);
1963
103k
    isl_assert(tab->mat->ctx, row != -1, return -1);
1964
103k
    
if (103k
isl_tab_pivot(tab, row, col) < 0103k
)
1965
0
      return -1;
1966
103k
    
if (103k
!var->is_row103k
)
1967
83.9k
      return 0;
1968
103k
  }
1969
87.2k
1970
5.46k
  
for (i = tab->n_dead; 3.33k
i < tab->n_col5.46k
;
++i2.13k
)
1971
5.46k
    
if (5.46k
!5.46k
isl_int_is_zero5.46k
(tab->mat->row[var->index][off + i]))
1972
3.33k
      break;
1973
3.33k
1974
3.33k
  isl_assert(tab->mat->ctx, i < tab->n_col, return -1);
1975
3.33k
  
if (3.33k
isl_tab_pivot(tab, var->index, i) < 03.33k
)
1976
0
    return -1;
1977
3.33k
1978
3.33k
  return 0;
1979
3.33k
}
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
272k
{
1991
272k
  int i;
1992
272k
  int r;
1993
272k
1994
272k
  if (!tab)
1995
0
    return NULL;
1996
272k
  r = isl_tab_add_row(tab, eq);
1997
272k
  if (r < 0)
1998
0
    goto error;
1999
272k
2000
272k
  r = tab->con[r].index;
2001
272k
  i = isl_seq_first_non_zero(tab->mat->row[r] + 2 + tab->M + tab->n_dead,
2002
272k
          tab->n_col - tab->n_dead);
2003
272k
  isl_assert(tab->mat->ctx, i >= 0, goto error);
2004
272k
  i += tab->n_dead;
2005
272k
  if (isl_tab_pivot(tab, r, i) < 0)
2006
0
    goto error;
2007
272k
  
if (272k
isl_tab_kill_col(tab, i) < 0272k
)
2008
0
    goto error;
2009
272k
  tab->n_eq++;
2010
272k
2011
272k
  return tab;
2012
0
error:
2013
0
  isl_tab_free(tab);
2014
0
  return NULL;
2015
272k
}
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
77.8k
{
2022
0
  return tab->M && 
!0
isl_int_is_zero0
(tab->mat->row[row][2]);
2023
77.8k
}
2024
2025
static int row_is_manifestly_zero(struct isl_tab *tab, int row)
2026
98.3k
{
2027
98.3k
  unsigned off = 2 + tab->M;
2028
98.3k
2029
98.3k
  if (
!98.3k
isl_int_is_zero98.3k
(tab->mat->row[row][1]))
2030
84.2k
    return 0;
2031
14.0k
  
if (14.0k
row_is_big(tab, row)14.0k
)
2032
0
    return 0;
2033
14.0k
  return isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
2034
14.0k
          tab->n_col - tab->n_dead) == -1;
2035
14.0k
}
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
84.6k
{
2044
84.6k
  struct isl_tab_var *var;
2045
84.6k
  int r;
2046
84.6k
2047
84.6k
  if (!tab)
2048
0
    return -1;
2049
84.6k
  r = isl_tab_add_row(tab, eq);
2050
84.6k
  if (r < 0)
2051
0
    return -1;
2052
84.6k
2053
84.6k
  var = &tab->con[r];
2054
84.6k
  r = var->index;
2055
84.6k
  if (
row_is_manifestly_zero(tab, r)84.6k
) {
2056
754
    var->is_zero = 1;
2057
754
    if (isl_tab_mark_redundant(tab, r) < 0)
2058
0
      return -1;
2059
754
    return 0;
2060
754
  }
2061
83.8k
2062
83.8k
  
if (83.8k
isl_int_is_neg83.8k
(tab->mat->row[r][1])) {
2063
32.6k
    isl_seq_neg(tab->mat->row[r] + 1, tab->mat->row[r] + 1,
2064
32.6k
          1 + tab->n_col);
2065
32.6k
    var->negated = 1;
2066
32.6k
  }
2067
83.8k
  var->is_nonneg = 1;
2068
83.8k
  if (to_col(tab, var) < 0)
2069
0
    return -1;
2070
83.8k
  var->is_nonneg = 0;
2071
83.8k
  if (isl_tab_kill_col(tab, var->index) < 0)
2072
0
    return -1;
2073
83.8k
2074
83.8k
  return 0;
2075
83.8k
}
2076
2077
/* Add a zero row to "tab" and return the corresponding index
2078
 * in the constraint array.
2079
 *
2080
 * This function assumes that at least one more row and at least
2081
 * one more element in the constraint array are available in the tableau.
2082
 */
2083
static int add_zero_row(struct isl_tab *tab)
2084
2.60k
{
2085
2.60k
  int r;
2086
2.60k
  isl_int *row;
2087
2.60k
2088
2.60k
  r = isl_tab_allocate_con(tab);
2089
2.60k
  if (r < 0)
2090
0
    return -1;
2091
2.60k
2092
2.60k
  row = tab->mat->row[tab->con[r].index];
2093
2.60k
  isl_seq_clr(row + 1, 1 + tab->M + tab->n_col);
2094
2.60k
  isl_int_set_si(row[0], 1);
2095
2.60k
2096
2.60k
  return r;
2097
2.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
13.7k
{
2108
13.7k
  struct isl_tab_undo *snap = NULL;
2109
13.7k
  struct isl_tab_var *var;
2110
13.7k
  int r;
2111
13.7k
  int row;
2112
13.7k
  int sgn;
2113
13.7k
  isl_int cst;
2114
13.7k
2115
13.7k
  if (!tab)
2116
0
    return -1;
2117
13.7k
  
isl_assert13.7k
(tab->mat->ctx, !tab->M, return -1);
2118
13.7k
2119
13.7k
  
if (13.7k
tab->need_undo13.7k
)
2120
13.0k
    snap = isl_tab_snap(tab);
2121
13.7k
2122
13.7k
  if (
tab->cone13.7k
) {
2123
1.11k
    isl_int_init(cst);
2124
1.11k
    isl_int_set_si(cst, 0);
2125
1.11k
    isl_int_swap(eq[0], cst);
2126
1.11k
  }
2127
13.7k
  r = isl_tab_add_row(tab, eq);
2128
13.7k
  if (
tab->cone13.7k
) {
2129
1.11k
    isl_int_swap(eq[0], cst);
2130
1.11k
    isl_int_clear(cst);
2131
1.11k
  }
2132
13.7k
  if (r < 0)
2133
0
    return -1;
2134
13.7k
2135
13.7k
  var = &tab->con[r];
2136
13.7k
  row = var->index;
2137
13.7k
  if (
row_is_manifestly_zero(tab, row)13.7k
) {
2138
10.2k
    if (snap)
2139
10.1k
      return isl_tab_rollback(tab, snap);
2140
81
    return drop_row(tab, row);
2141
81
  }
2142
3.50k
2143
3.50k
  
if (3.50k
tab->bmap3.50k
) {
2144
2.60k
    tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
2145
2.60k
    if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
2146
0
      return -1;
2147
2.60k
    isl_seq_neg(eq, eq, 1 + tab->n_var);
2148
2.60k
    tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
2149
2.60k
    isl_seq_neg(eq, eq, 1 + tab->n_var);
2150
2.60k
    if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
2151
0
      return -1;
2152
2.60k
    
if (2.60k
!tab->bmap2.60k
)
2153
0
      return -1;
2154
2.60k
    
if (2.60k
add_zero_row(tab) < 02.60k
)
2155
0
      return -1;
2156
3.50k
  }
2157
3.50k
2158
3.50k
  
sgn = 3.50k
isl_int_sgn3.50k
(tab->mat->row[row][1]);
2159
3.50k
2160
3.50k
  if (
sgn > 03.50k
) {
2161
145
    isl_seq_neg(tab->mat->row[row] + 1, tab->mat->row[row] + 1,
2162
145
          1 + tab->n_col);
2163
145
    var->negated = 1;
2164
145
    sgn = -1;
2165
145
  }
2166
3.50k
2167
3.50k
  if (
sgn < 03.50k
) {
2168
2.37k
    sgn = sign_of_max(tab, var);
2169
2.37k
    if (sgn < -1)
2170
0
      return -1;
2171
2.37k
    
if (2.37k
sgn < 02.37k
) {
2172
0
      if (isl_tab_mark_empty(tab) < 0)
2173
0
        return -1;
2174
0
      return 0;
2175
0
    }
2176
2.37k
  }
2177
3.50k
2178
3.50k
  var->is_nonneg = 1;
2179
3.50k
  if (to_col(tab, var) < 0)
2180
0
    return -1;
2181
3.50k
  var->is_nonneg = 0;
2182
3.50k
  if (isl_tab_kill_col(tab, var->index) < 0)
2183
0
    return -1;
2184
3.50k
2185
3.50k
  return 0;
2186
3.50k
}
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.92k
{
2200
1.92k
  unsigned total;
2201
1.92k
  unsigned div_pos;
2202
1.92k
  struct isl_vec *ineq;
2203
1.92k
2204
1.92k
  if (!bmap)
2205
0
    return NULL;
2206
1.92k
2207
1.92k
  total = isl_basic_map_total_dim(bmap);
2208
1.92k
  div_pos = 1 + total - bmap->n_div + div;
2209
1.92k
2210
1.92k
  ineq = isl_vec_alloc(bmap->ctx, 1 + total);
2211
1.92k
  if (!ineq)
2212
0
    return NULL;
2213
1.92k
2214
1.92k
  isl_seq_cpy(ineq->el, bmap->div[div] + 1, 1 + total);
2215
1.92k
  isl_int_neg(ineq->el[div_pos], bmap->div[div][0]);
2216
1.92k
  return ineq;
2217
1.92k
}
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.92k
{
2237
1.92k
  unsigned total;
2238
1.92k
  unsigned div_pos;
2239
1.92k
  struct isl_vec *ineq;
2240
1.92k
2241
1.92k
  total = isl_basic_map_total_dim(tab->bmap);
2242
1.92k
  div_pos = 1 + total - tab->bmap->n_div + div;
2243
1.92k
2244
1.92k
  ineq = ineq_for_div(tab->bmap, div);
2245
1.92k
  if (!ineq)
2246
0
    goto error;
2247
1.92k
2248
1.92k
  
if (1.92k
add_ineq1.92k
) {
2249
565
    if (add_ineq(user, ineq->el) < 0)
2250
0
      goto error;
2251
1.35k
  } else {
2252
1.35k
    if (isl_tab_add_ineq(tab, ineq->el) < 0)
2253
0
      goto error;
2254
1.92k
  }
2255
1.92k
2256
1.92k
  isl_seq_neg(ineq->el, tab->bmap->div[div] + 1, 1 + total);
2257
1.92k
  isl_int_set(ineq->el[div_pos], tab->bmap->div[div][0]);
2258
1.92k
  isl_int_add(ineq->el[0], ineq->el[0], ineq->el[div_pos]);
2259
1.92k
  isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
2260
1.92k
2261
1.92k
  if (
add_ineq1.92k
) {
2262
565
    if (add_ineq(user, ineq->el) < 0)
2263
0
      goto error;
2264
1.35k
  } else {
2265
1.35k
    if (isl_tab_add_ineq(tab, ineq->el) < 0)
2266
0
      goto error;
2267
1.92k
  }
2268
1.92k
2269
1.92k
  isl_vec_free(ineq);
2270
1.92k
2271
1.92k
  return 0;
2272
0
error:
2273
0
  isl_vec_free(ineq);
2274
0
  return -1;
2275
1.92k
}
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.92k
{
2285
1.92k
  int i;
2286
1.92k
2287
1.92k
  if (tab->M)
2288
0
    return 1;
2289
1.92k
2290
1.92k
  
if (1.92k
isl_int_is_neg1.92k
(div->el[1]))
2291
217
    return 0;
2292
1.70k
2293
3.77k
  
for (i = 0; 1.70k
i < tab->n_var3.77k
;
++i2.07k
) {
2294
3.51k
    if (isl_int_is_neg(div->el[2 + i]))
2295
336
      return 0;
2296
3.17k
    
if (3.17k
isl_int_is_zero3.17k
(div->el[2 + i]))
2297
1.78k
      continue;
2298
1.39k
    
if (1.39k
!tab->var[i].is_nonneg1.39k
)
2299
1.10k
      return 0;
2300
3.51k
  }
2301
1.70k
2302
258
  return 1;
2303
1.92k
}
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.92k
{
2319
1.92k
  int r;
2320
1.92k
  int nonneg;
2321
1.92k
  int n_div, o_div;
2322
1.92k
2323
1.92k
  if (
!tab || 1.92k
!div1.92k
)
2324
0
    return -1;
2325
1.92k
2326
1.92k
  
if (1.92k
div->size != 1 + 1 + tab->n_var1.92k
)
2327
0
    isl_die(isl_tab_get_ctx(tab), isl_error_invalid,
2328
1.92k
      "unexpected size", return -1);
2329
1.92k
2330
1.92k
  
isl_assert1.92k
(tab->mat->ctx, tab->bmap, return -1);
2331
1.92k
  n_div = isl_basic_map_dim(tab->bmap, isl_dim_div);
2332
1.92k
  o_div = tab->n_var - n_div;
2333
1.92k
  if (
pos < o_div || 1.92k
pos > tab->n_var1.92k
)
2334
0
    isl_die(isl_tab_get_ctx(tab), isl_error_invalid,
2335
1.92k
      "invalid position", return -1);
2336
1.92k
2337
1.92k
  nonneg = div_is_nonneg(tab, div);
2338
1.92k
2339
1.92k
  if (isl_tab_extend_cons(tab, 3) < 0)
2340
0
    return -1;
2341
1.92k
  
if (1.92k
isl_tab_extend_vars(tab, 1) < 01.92k
)
2342
0
    return -1;
2343
1.92k
  r = isl_tab_insert_var(tab, pos);
2344
1.92k
  if (r < 0)
2345
0
    return -1;
2346
1.92k
2347
1.92k
  
if (1.92k
nonneg1.92k
)
2348
258
    tab->var[r].is_nonneg = 1;
2349
1.92k
2350
1.92k
  tab->bmap = isl_basic_map_insert_div(tab->bmap, pos - o_div, div);
2351
1.92k
  if (!tab->bmap)
2352
0
    return -1;
2353
1.92k
  
if (1.92k
isl_tab_push_var(tab, isl_tab_undo_bmap_div, &tab->var[r]) < 01.92k
)
2354
0
    return -1;
2355
1.92k
2356
1.92k
  
if (1.92k
add_div_constraints(tab, pos - o_div, add_ineq, user) < 01.92k
)
2357
0
    return -1;
2358
1.92k
2359
1.92k
  return r;
2360
1.92k
}
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
1.35k
{
2367
1.35k
  if (!tab)
2368
0
    return -1;
2369
1.35k
  return isl_tab_insert_div(tab, tab->n_var, div, NULL, NULL);
2370
1.35k
}
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
713k
{
2380
713k
  int i;
2381
713k
  struct isl_tab *tab;
2382
713k
2383
713k
  if (!bmap)
2384
0
    return NULL;
2385
713k
  tab = isl_tab_alloc(bmap->ctx,
2386
713k
          isl_basic_map_total_dim(bmap) + bmap->n_ineq + 1,
2387
713k
          isl_basic_map_total_dim(bmap), 0);
2388
713k
  if (!tab)
2389
0
    return NULL;
2390
713k
  tab->preserve = track;
2391
713k
  tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
2392
713k
  if (
ISL_F_ISSET713k
(bmap, ISL_BASIC_MAP_EMPTY)) {
2393
19
    if (isl_tab_mark_empty(tab) < 0)
2394
0
      goto error;
2395
19
    goto done;
2396
19
  }
2397
984k
  
for (i = 0; 713k
i < bmap->n_eq984k
;
++i270k
) {
2398
270k
    tab = add_eq(tab, bmap->eq[i]);
2399
270k
    if (!tab)
2400
0
      return tab;
2401
270k
  }
2402
4.34M
  
for (i = 0; 713k
i < bmap->n_ineq4.34M
;
++i3.63M
) {
2403
3.63M
    if (isl_tab_add_ineq(tab, bmap->ineq[i]) < 0)
2404
0
      goto error;
2405
3.63M
    
if (3.63M
tab->empty3.63M
)
2406
5.31k
      goto done;
2407
3.63M
  }
2408
713k
done:
2409
713k
  if (
track && 713k
isl_tab_track_bmap(tab, isl_basic_map_copy(bmap)) < 0190k
)
2410
0
    goto error;
2411
713k
  return tab;
2412
0
error:
2413
0
  isl_tab_free(tab);
2414
0
  return NULL;
2415
713k
}
2416
2417
__isl_give struct isl_tab *isl_tab_from_basic_set(
2418
  __isl_keep isl_basic_set *bset, int track)
2419
270k
{
2420
270k
  return isl_tab_from_basic_map(bset, track);
2421
270k
}
2422
2423
/* Construct a tableau corresponding to the recession cone of "bset".
2424
 */
2425
struct isl_tab *isl_tab_from_recession_cone(__isl_keep isl_basic_set *bset,
2426
  int parametric)
2427
3.80k
{
2428
3.80k
  isl_int cst;
2429
3.80k
  int i;
2430
3.80k
  struct isl_tab *tab;
2431
3.80k
  unsigned offset = 0;
2432
3.80k
2433
3.80k
  if (!bset)
2434
0
    return NULL;
2435
3.80k
  
if (3.80k
parametric3.80k
)
2436
2.83k
    offset = isl_basic_set_dim(bset, isl_dim_param);
2437
3.80k
  tab = isl_tab_alloc(bset->ctx, bset->n_eq + bset->n_ineq,
2438
3.80k
        isl_basic_set_total_dim(bset) - offset, 0);
2439
3.80k
  if (!tab)
2440
0
    return NULL;
2441
3.80k
  
tab->rational = 3.80k
ISL_F_ISSET3.80k
(bset, ISL_BASIC_SET_RATIONAL);
2442
3.80k
  tab->cone = 1;
2443
3.80k
2444
3.80k
  isl_int_init(cst);
2445
3.80k
  isl_int_set_si(cst, 0);
2446
6.21k
  for (i = 0; 
i < bset->n_eq6.21k
;
++i2.41k
) {
2447
2.41k
    isl_int_swap(bset->eq[i][offset], cst);
2448
2.41k
    if (
offset > 02.41k
) {
2449
677
      if (isl_tab_add_eq(tab, bset->eq[i] + offset) < 0)
2450
0
        goto error;
2451
2.41k
    } else
2452
1.74k
      tab = add_eq(tab, bset->eq[i]);
2453
2.41k
    
isl_int_swap2.41k
(bset->eq[i][offset], cst);
2454
2.41k
    if (!tab)
2455
0
      goto done;
2456
2.41k
  }
2457
16.0k
  
for (i = 0; 3.80k
i < bset->n_ineq16.0k
;
++i12.2k
) {
2458
12.2k
    int r;
2459
12.2k
    isl_int_swap(bset->ineq[i][offset], cst);
2460
12.2k
    r = isl_tab_add_row(tab, bset->ineq[i] + offset);
2461
12.2k
    isl_int_swap(bset->ineq[i][offset], cst);
2462
12.2k
    if (r < 0)
2463
0
      goto error;
2464
12.2k
    tab->con[r].is_nonneg = 1;
2465
12.2k
    if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
2466
0
      goto error;
2467
12.2k
  }
2468
3.80k
done:
2469
3.80k
  isl_int_clear(cst);
2470
3.80k
  return tab;
2471
0
error:
2472
0
  isl_int_clear(cst);
2473
0
  isl_tab_free(tab);
2474
0
  return NULL;
2475
3.80k
}
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.83k
{
2482
2.83k
  int i;
2483
2.83k
2484
2.83k
  if (!tab)
2485
0
    return isl_bool_error;
2486
2.83k
  
if (2.83k
tab->empty2.83k
)
2487
0
    return isl_bool_true;
2488
2.83k
  
if (2.83k
tab->n_dead == tab->n_col2.83k
)
2489
754
    return isl_bool_true;
2490
2.08k
2491
2.08k
  
for (;;) 2.08k
{
2492
3.30k
    for (i = tab->n_redundant; 
i < tab->n_row3.30k
;
++i271
) {
2493
3.29k
      struct isl_tab_var *var;
2494
3.29k
      int sgn;
2495
3.29k
      var = isl_tab_var_from_row(tab, i);
2496
3.29k
      if (!var->is_nonneg)
2497
271
        continue;
2498
3.02k
      sgn = sign_of_max(tab, var);
2499
3.02k
      if (sgn < -1)
2500
0
        return isl_bool_error;
2501
3.02k
      
if (3.02k
sgn != 03.02k
)
2502
305
        return isl_bool_false;
2503
2.72k
      
if (2.72k
close_row(tab, var, 0) < 02.72k
)
2504
0
        return isl_bool_error;
2505
2.72k
      break;
2506
2.72k
    }
2507
2.72k
    
if (2.72k
tab->n_dead == tab->n_col2.72k
)
2508
1.77k
      return isl_bool_true;
2509
952
    
if (952
i == tab->n_row952
)
2510
3
      return isl_bool_false;
2511
0
  }
2512
2.83k
}
2513
2514
int isl_tab_sample_is_integer(struct isl_tab *tab)
2515
418k
{
2516
418k
  int i;
2517
418k
2518
418k
  if (!tab)
2519
0
    return -1;
2520
418k
2521
2.02M
  
for (i = 0; 418k
i < tab->n_var2.02M
;
++i1.61M
) {
2522
1.70M
    int row;
2523
1.70M
    if (!tab->var[i].is_row)
2524
484k
      continue;
2525
1.21M
    row = tab->var[i].index;
2526
1.21M
    if (
!1.21M
isl_int_is_divisible_by1.21M
(tab->mat->row[row][1],
2527
1.21M
            tab->mat->row[row][0]))
2528
92.9k
      return 0;
2529
1.70M
  }
2530
325k
  return 1;
2531
418k
}
2532
2533
static struct isl_vec *extract_integer_sample(struct isl_tab *tab)
2534
197k
{
2535
197k
  int i;
2536
197k
  struct isl_vec *vec;
2537
197k
2538
197k
  vec = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
2539
197k
  if (!vec)
2540
0
    return NULL;
2541
197k
2542
197k
  
isl_int_set_si197k
(vec->block.data[0], 1);
2543
1.25M
  for (i = 0; 
i < tab->n_var1.25M
;
++i1.05M
) {
2544
1.05M
    if (!tab->var[i].is_row)
2545
375k
      isl_int_set_si(vec->block.data[1 + i], 0);
2546
677k
    else {
2547
677k
      int row = tab->var[i].index;
2548
677k
      isl_int_divexact(vec->block.data[1 + i],
2549
677k
        tab->mat->row[row][1], tab->mat->row[row][0]);
2550
677k
    }
2551
1.05M
  }
2552
197k
2553
197k
  return vec;
2554
197k
}
2555
2556
struct isl_vec *isl_tab_get_sample_value(struct isl_tab *tab)
2557
249k
{
2558
249k
  int i;
2559
249k
  struct isl_vec *vec;
2560
249k
  isl_int m;
2561
249k
2562
249k
  if (!tab)
2563
0
    return NULL;
2564
249k
2565
249k
  vec = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
2566
249k
  if (!vec)
2567
0
    return NULL;
2568
249k
2569
249k
  
isl_int_init249k
(m);
2570
249k
2571
249k
  isl_int_set_si(vec->block.data[0], 1);
2572
1.09M
  for (i = 0; 
i < tab->n_var1.09M
;
++i842k
) {
2573
842k
    int row;
2574
842k
    if (
!tab->var[i].is_row842k
) {
2575
401k
      isl_int_set_si(vec->block.data[1 + i], 0);
2576
401k
      continue;
2577
401k
    }
2578
440k
    row = tab->var[i].index;
2579
440k
    isl_int_gcd(m, vec->block.data[0], tab->mat->row[row][0]);
2580
440k
    isl_int_divexact(m, tab->mat->row[row][0], m);
2581
440k
    isl_seq_scale(vec->block.data, vec->block.data, m, 1 + i);
2582
440k
    isl_int_divexact(m, vec->block.data[0], tab->mat->row[row][0]);
2583
440k
    isl_int_mul(vec->block.data[1 + i], m, tab->mat->row[row][1]);
2584
842k
  }
2585
249k
  vec = isl_vec_normalize(vec);
2586
249k
2587
249k
  isl_int_clear(m);
2588
249k
  return vec;
2589
249k
}
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
236k
{
2597
236k
  if (!var->is_row)
2598
2.95k
    isl_int_set_si(*v, 0);
2599
233k
  else 
if (233k
sgn > 0233k
)
2600
231k
    isl_int_cdiv_q(*v, tab->mat->row[var->index][1],
2601
233k
           tab->mat->row[var->index][0]);
2602
233k
  else
2603
1.89k
    isl_int_fdiv_q(*v, tab->mat->row[var->index][1],
2604
236k
           tab->mat->row[var->index][0]);
2605
236k
}
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
422k
{
2618
422k
  int i;
2619
422k
  unsigned n_eq;
2620
422k
2621
422k
  if (!bmap)
2622
0
    return NULL;
2623
422k
  
if (422k
!tab422k
)
2624
0
    return bmap;
2625
422k
2626
422k
  n_eq = tab->n_eq;
2627
422k
  if (tab->empty)
2628
6.61k
    bmap = isl_basic_map_set_to_empty(bmap);
2629
422k
  else
2630
2.89M
    
for (i = bmap->n_ineq - 1; 415k
i >= 02.89M
;
--i2.47M
) {
2631
2.47M
      if (isl_tab_is_equality(tab, n_eq + i))
2632
1.02M
        isl_basic_map_inequality_to_equality(bmap, i);
2633
1.45M
      else 
if (1.45M
isl_tab_is_redundant(tab, n_eq + i)1.45M
)
2634
192k
        isl_basic_map_drop_inequality(bmap, i);
2635
415k
    }
2636
422k
  if (bmap->n_eq != n_eq)
2637
153k
    bmap = isl_basic_map_gauss(bmap, NULL);
2638
422k
  if (!tab->rational &&
2639
422k
      
bmap388k
&&
!bmap->sample388k
&&
isl_tab_sample_is_integer(tab)213k
)
2640
197k
    bmap->sample = extract_integer_sample(tab);
2641
422k
  return bmap;
2642
422k
}
2643
2644
struct isl_basic_set *isl_basic_set_update_from_tab(struct isl_basic_set *bset,
2645
  struct isl_tab *tab)
2646
37.9k
{
2647
37.9k
  return bset_from_bmap(isl_basic_map_update_from_tab(bset_to_bmap(bset),
2648
37.9k
                tab));
2649
37.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
12.9k
{
2656
12.9k
  if (!tab->con[r].is_row)
2657
0
    isl_die(isl_tab_get_ctx(tab), isl_error_internal,
2658
12.9k
      "row unexpectedly moved to column",
2659
12.9k
      return isl_stat_error);
2660
12.9k
  
if (12.9k
r + 1 != tab->n_con12.9k
)
2661
0
    isl_die(isl_tab_get_ctx(tab), isl_error_internal,
2662
12.9k
      "additional constraints added", return isl_stat_error);
2663
12.9k
  
if (12.9k
drop_row(tab, tab->con[r].index) < 012.9k
)
2664
0
    return isl_stat_error;
2665
12.9k
2666
12.9k
  return isl_stat_ok;
2667
12.9k
}
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
12.9k
{
2681
12.9k
  unsigned r;
2682
12.9k
  isl_int *row;
2683
12.9k
  int sgn;
2684
12.9k
  unsigned off = 2 + tab->M;
2685
12.9k
2686
12.9k
  if (var->is_zero)
2687
0
    return isl_stat_ok;
2688
12.9k
  
if (12.9k
var->is_redundant || 12.9k
!var->is_nonneg12.9k
)
2689
0
    isl_die(isl_tab_get_ctx(tab), isl_error_invalid,
2690
12.9k
      "expecting non-redundant non-negative variable",
2691
12.9k
      return isl_stat_error);
2692
12.9k
2693
12.9k
  
if (12.9k
isl_tab_extend_cons(tab, 1) < 012.9k
)
2694
0
    return isl_stat_error;
2695
12.9k
2696
12.9k
  r = tab->n_con;
2697
12.9k
  tab->con[r].index = tab->n_row;
2698
12.9k
  tab->con[r].is_row = 1;
2699
12.9k
  tab->con[r].is_nonneg = 0;
2700
12.9k
  tab->con[r].is_zero = 0;
2701
12.9k
  tab->con[r].is_redundant = 0;
2702
12.9k
  tab->con[r].frozen = 0;
2703
12.9k
  tab->con[r].negated = 0;
2704
12.9k
  tab->row_var[tab->n_row] = ~r;
2705
12.9k
  row = tab->mat->row[tab->n_row];
2706
12.9k
2707
12.9k
  if (
var->is_row12.9k
) {
2708
1.17k
    isl_int_set(row[0], tab->mat->row[var->index][0]);
2709
1.17k
    isl_seq_neg(row + 1,
2710
1.17k
          tab->mat->row[var->index] + 1, 1 + tab->n_col);
2711
12.9k
  } else {
2712
11.7k
    isl_int_set_si(row[0], 1);
2713
11.7k
    isl_seq_clr(row + 1, 1 + tab->n_col);
2714
11.7k
    isl_int_set_si(row[off + var->index], -1);
2715
11.7k
  }
2716
12.9k
2717
12.9k
  tab->n_row++;
2718
12.9k
  tab->n_con++;
2719
12.9k
2720
12.9k
  sgn = sign_of_max(tab, &tab->con[r]);
2721
12.9k
  if (sgn < -1)
2722
0
    return isl_stat_error;
2723
12.9k
  
if (12.9k
sgn < 012.9k
) {
2724
48
    if (drop_last_con_in_row(tab, r) < 0)
2725
0
      return isl_stat_error;
2726
48
    
if (48
isl_tab_mark_empty(tab) < 048
)
2727
0
      return isl_stat_error;
2728
48
    return isl_stat_ok;
2729
48
  }
2730
12.8k
  tab->con[r].is_nonneg = 1;
2731
12.8k
  /* sgn == 0 */
2732
12.8k
  if (close_row(tab, &tab->con[r], 1) < 0)
2733
0
    return isl_stat_error;
2734
12.8k
  
if (12.8k
drop_last_con_in_row(tab, r) < 012.8k
)
2735
0
    return isl_stat_error;
2736
12.8k
2737
12.8k
  return isl_stat_ok;
2738
12.8k
}
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
1.45k
{
2759
1.45k
  struct isl_tab_var *var;
2760
1.45k
2761
1.45k
  if (!tab)
2762
0
    return -1;
2763
1.45k
2764
1.45k
  var = &tab->con[con];
2765
1.45k
2766
1.45k
  if (
var->is_row && 1.45k
(var->index < 0 || 54
var->index < tab->n_redundant54
))
2767
0
    isl_die(tab->mat->ctx, isl_error_invalid,
2768
1.45k
      "cannot relax redundant constraint", return -1);
2769
1.45k
  
if (1.45k
!var->is_row && 1.45k
(var->index < 0 || 1.40k
var->index < tab->n_dead1.40k
))
2770
0
    isl_die(tab->mat->ctx, isl_error_invalid,
2771
1.45k
      "cannot relax dead constraint", return -1);
2772
1.45k
2773
1.45k
  
if (1.45k
!var->is_row && 1.45k
!max_is_manifestly_unbounded(tab, var)1.40k
)
2774
412
    
if (412
to_row(tab, var, 1) < 0412
)
2775
0
      return -1;
2776
1.45k
  
if (1.45k
!var->is_row && 1.45k
!min_is_manifestly_unbounded(tab, var)990
)
2777
19
    
if (19
to_row(tab, var, -1) < 019
)
2778
0
      return -1;
2779
1.45k
2780
1.45k
  
if (1.45k
var->is_row1.45k
) {
2781
485
    isl_int_add(tab->mat->row[var->index][1],
2782
485
        tab->mat->row[var->index][1], tab->mat->row[var->index][0]);
2783
485
    if (restore_row(tab, var) < 0)
2784
0
      return -1;
2785
971
  } else {
2786
971
    int i;
2787
971
    unsigned off = 2 + tab->M;
2788
971
2789
5.42k
    for (i = 0; 
i < tab->n_row5.42k
;
++i4.45k
) {
2790
4.45k
      if (isl_int_is_zero(tab->mat->row[i][off + var->index]))
2791
3.03k
        continue;
2792
1.42k
      
isl_int_sub1.42k
(tab->mat->row[i][1], tab->mat->row[i][1],
2793
1.42k
          tab->mat->row[i][off + var->index]);
2794
1.42k
    }
2795
971
2796
971
  }
2797
1.45k
2798
1.45k
  
if (1.45k
isl_tab_push_var(tab, isl_tab_undo_relax, var) < 01.45k
)
2799
0
    return -1;
2800
1.45k
2801
1.45k
  return 0;
2802
1.45k
}
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
136
{
2823
136
  struct isl_tab_var *var;
2824
136
2825
136
  if (!tab)
2826
0
    return -1;
2827
136
  
if (136
isl_int_is_zero136
(shift))
2828
70
    return 0;
2829
66
2830
66
  var = &tab->var[pos];
2831
66
  if (
!var->is_row66
) {
2832
9
    if (
isl_int_is_neg9
(shift)) {
2833
6
      if (!max_is_manifestly_unbounded(tab, var))
2834
3
        
if (3
to_row(tab, var, 1) < 03
)
2835
0
          return -1;
2836
3
    } else {
2837
3
      if (!min_is_manifestly_unbounded(tab, var))
2838
0
        
if (0
to_row(tab, var, -1) < 00
)
2839
0
          return -1;
2840
66
    }
2841
9
  }
2842
66
2843
66
  
if (66
var->is_row66
) {
2844
60
    isl_int_addmul(tab->mat->row[var->index][1],
2845
60
        shift, tab->mat->row[var->index][0]);
2846
66
  } else {
2847
6
    int i;
2848
6
    unsigned off = 2 + tab->M;
2849
6
2850
30
    for (i = 0; 
i < tab->n_row30
;
++i24
) {
2851
24
      if (isl_int_is_zero(tab->mat->row[i][off + var->index]))
2852
16
        continue;
2853
8
      
isl_int_submul8
(tab->mat->row[i][1],
2854
8
            shift, tab->mat->row[i][off + var->index]);
2855
8
    }
2856
6
2857
6
  }
2858
136
2859
136
  return 0;
2860
136
}
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
438
{
2869
438
  struct isl_tab_var *var;
2870
438
2871
438
  if (!tab)
2872
0
    return -1;
2873
438
2874
438
  var = &tab->con[con];
2875
438
  if (!var->is_nonneg)
2876
0
    return 0;
2877
438
2878
438
  var->is_nonneg = 0;
2879
438
  if (isl_tab_push_var(tab, isl_tab_undo_unrestrict, var) < 0)
2880
0
    return -1;
2881
438
2882
438
  return 0;
2883
438
}
2884
2885
int isl_tab_select_facet(struct isl_tab *tab, int con)
2886
12.4k
{
2887
12.4k
  if (!tab)
2888
0
    return -1;
2889
12.4k
2890
12.4k
  return cut_to_hyperplane(tab, &tab->con[con]);
2891
12.4k
}
2892
2893
static int may_be_equality(struct isl_tab *tab, int row)
2894
7.81M
{
2895
36.3k
  return tab->rational ? isl_int_is_zero(tab->mat->row[row][1])
2896
7.78M
           : isl_int_lt(tab->mat->row[row][1],
2897
7.81M
              tab->mat->row[row][0]);
2898
7.81M
}
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.97M
{
2913
1.97M
  int i;
2914
1.97M
  struct isl_tab_var *var;
2915
1.97M
2916
15.6M
  for (i = tab->n_con - 1; 
i >= 015.6M
;
--i13.6M
) {
2917
15.4M
    var = &tab->con[i];
2918
15.4M
    if (var->index < 0)
2919
6.15M
      continue;
2920
9.30M
    
if (9.30M
var->is_row && 9.30M
var->index < tab->n_redundant7.16M
)
2921
603k
      continue;
2922
8.70M
    
if (8.70M
!var->is_row && 8.70M
var->index < tab->n_dead2.13M
)
2923
1.69k
      continue;
2924
8.70M
    
if (8.70M
var->marked8.70M
)
2925
1.81M
      return var;
2926
15.4M
  }
2927
1.97M
2928
159k
  return NULL;
2929
1.97M
}
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
419k
{
2950
419k
  int i;
2951
419k
  unsigned n_marked;
2952
419k
2953
419k
  if (!tab)
2954
0
    return -1;
2955
419k
  
if (419k
tab->empty419k
)
2956
3.68k
    return 0;
2957
415k
  
if (415k
tab->n_dead == tab->n_col415k
)
2958
18.0k
    return 0;
2959
397k
2960
397k
  n_marked = 0;
2961
2.95M
  for (i = tab->n_redundant; 
i < tab->n_row2.95M
;
++i2.55M
) {
2962
2.55M
    struct isl_tab_var *var = isl_tab_var_from_row(tab, i);
2963
2.53M
    var->marked = !var->frozen && var->is_nonneg &&
2964
2.32M
      may_be_equality(tab, i);
2965
2.55M
    if (var->marked)
2966
1.71M
      n_marked++;
2967
2.55M
  }
2968
2.24M
  for (i = tab->n_dead; 
i < tab->n_col2.24M
;
++i1.84M
) {
2969
1.84M
    struct isl_tab_var *var = var_from_col(tab, i);
2970
1.83M
    var->marked = !var->frozen && var->is_nonneg;
2971
1.84M
    if (var->marked)
2972
171k
      n_marked++;
2973
1.84M
  }
2974
1.64M
  while (
n_marked1.64M
) {
2975
1.40M
    struct isl_tab_var *var;
2976
1.40M
    int sgn;
2977
1.40M
    var = select_marked(tab);
2978
1.40M
    if (!var)
2979
152k
      break;
2980
1.25M
    var->marked = 0;
2981
1.25M
    n_marked--;
2982
1.25M
    sgn = sign_of_max(tab, var);
2983
1.25M
    if (sgn < 0)
2984
0
      return -1;
2985
1.25M
    
if (1.25M
sgn == 01.25M
) {
2986
547k
      if (close_row(tab, var, 0) < 0)
2987
0
        return -1;
2988
702k
    } else 
if (702k
!tab->rational && 702k
!at_least_one(tab, var)679k
) {
2989
495
      if (cut_to_hyperplane(tab, var) < 0)
2990
0
        return -1;
2991
495
      return isl_tab_detect_implicit_equalities(tab);
2992
495
    }
2993
11.1M
    
for (i = tab->n_redundant; 1.25M
i < tab->n_row11.1M
;
++i9.87M
) {
2994
9.87M
      var = isl_tab_var_from_row(tab, i);
2995
9.87M
      if (!var->marked)
2996
4.38M
        continue;
2997
5.49M
      
if (5.49M
may_be_equality(tab, i)5.49M
)
2998
5.41M
        continue;
2999
79.7k
      var->marked = 0;
3000
79.7k
      n_marked--;
3001
79.7k
    }
3002
1.40M
  }
3003
397k
3004
396k
  return 0;
3005
419k
}
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.35k
{
3012
6.35k
  int *p;
3013
6.35k
  int index;
3014
6.35k
3015
6.35k
  index = tab->con[i].index;
3016
6.35k
  if (index == -1)
3017
4.20k
    return 0;
3018
2.15k
  
p = tab->con[i].is_row ? 2.15k
tab->row_var1.47k
:
tab->col_var672
;
3019
2.15k
  if (p[index] != ~old)
3020
0
    isl_die(tab->mat->ctx, isl_error_internal,
3021
2.15k
      "broken internal state", return -1);
3022
2.15k
  p[index] = ~i;
3023
2.15k
3024
2.15k
  return 0;
3025
6.35k
}
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.24k
3038
1.24k
  last = first + n - 1;
3039
1.24k
  var = tab->con[last];
3040
6.35k
  for (i = last; 
i > first6.35k
;
--i5.11k
) {
3041
5.11k
    tab->con[i] = tab->con[i - 1];
3042
5.11k
    if (update_con_after_move(tab, i, i - 1) < 0)
3043
0
      return -1;
3044
5.11k
  }
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
57.2k
{
3085
57.2k
  int i;
3086
57.2k
3087
57.2k
  if (
!tab || 57.2k
!bmap57.2k
)
3088
0
    return isl_basic_map_free(bmap);
3089
57.2k
  
if (57.2k
tab->empty57.2k
)
3090
75
    return bmap;
3091
57.1k
3092
167k
  
for (i = bmap->n_ineq - 1; 57.1k
i >= 0167k
;
--i110k
) {
3093
110k
    if (!isl_tab_is_equality(tab, bmap->n_eq + i))
3094
109k
      continue;
3095
918
    isl_basic_map_inequality_to_equality(bmap, i);
3096
918
    if (rotate_constraints(tab, 0, tab->n_eq + i + 1) < 0)
3097
0
      return isl_basic_map_free(bmap);
3098
918
    
if (918
rotate_constraints(tab, tab->n_eq + i + 1,
3099
918
          bmap->n_ineq - i) < 0)
3100
0
      return isl_basic_map_free(bmap);
3101
918
    tab->n_eq++;
3102
918
  }
3103
57.1k
3104
57.1k
  return bmap;
3105
57.2k
}
3106
3107
static int con_is_redundant(struct isl_tab *tab, struct isl_tab_var *var)
3108
908k
{
3109
908k
  if (!tab)
3110
0
    return -1;
3111
908k
  
if (908k
tab->rational908k
) {
3112
70.5k
    int sgn = sign_of_min(tab, var);
3113
70.5k
    if (sgn < -1)
3114
0
      return -1;
3115
70.5k
    return sgn >= 0;
3116
0
  } else {
3117
837k
    int irred = isl_tab_min_at_most_neg_one(tab, var);
3118
837k
    if (irred < 0)
3119
0
      return -1;
3120
837k
    return !irred;
3121
837k
  }
3122
908k
}
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
201k
{
3139
201k
  int i;
3140
201k
  unsigned n_marked;
3141
201k
3142
201k
  if (!tab)
3143
0
    return -1;
3144
201k
  
if (201k
tab->empty201k
)
3145
2.69k
    return 0;
3146
198k
  
if (198k
tab->n_redundant == tab->n_row198k
)
3147
3.43k
    return 0;
3148
195k
3149
195k
  n_marked = 0;
3150
1.42M
  for (i = tab->n_redundant; 
i < tab->n_row1.42M
;
++i1.22M
) {
3151
1.22M
    struct isl_tab_var *var = isl_tab_var_from_row(tab, i);
3152
1.08M
    var->marked = !var->frozen && var->is_nonneg;
3153
1.22M
    if (var->marked)
3154
544k
      n_marked++;
3155
1.22M
  }
3156
1.06M
  for (i = tab->n_dead; 
i < tab->n_col1.06M
;
++i869k
) {
3157
869k
    struct isl_tab_var *var = var_from_col(tab, i);
3158
809k
    var->marked = !var->frozen && var->is_nonneg &&
3159
367k
      !min_is_manifestly_unbounded(tab, var);
3160
869k
    if (var->marked)
3161
116k
      n_marked++;
3162
869k
  }
3163
758k
  while (
n_marked758k
) {
3164
570k
    struct isl_tab_var *var;
3165
570k
    int red;
3166
570k
    var = select_marked(tab);
3167
570k
    if (!var)
3168
6.77k
      break;
3169
563k
    var->marked = 0;
3170
563k
    n_marked--;
3171
563k
    red = con_is_redundant(tab, var);
3172
563k
    if (red < 0)
3173
0
      return -1;
3174
563k
    
if (563k
red && 563k
!var->is_redundant128k
)
3175
23.2k
      
if (23.2k
isl_tab_mark_redundant(tab, var->index) < 023.2k
)
3176
0
        return -1;
3177
7.41M
    
for (i = tab->n_dead; 563k
i < tab->n_col7.41M
;
++i6.85M
) {
3178
6.85M
      var = var_from_col(tab, i);
3179
6.85M
      if (!var->marked)
3180
6.49M
        continue;
3181
355k
      
if (355k
!min_is_manifestly_unbounded(tab, var)355k
)
3182
267k
        continue;
3183
87.9k
      var->marked = 0;
3184
87.9k
      n_marked--;
3185
87.9k
    }
3186
570k
  }
3187
195k
3188
195k
  return 0;
3189
201k
}
3190
3191
int isl_tab_is_equality(struct isl_tab *tab, int con)
3192
2.60M
{
3193
2.60M
  int row;
3194
2.60M
  unsigned off;
3195
2.60M
3196
2.60M
  if (!tab)
3197
0
    return -1;
3198
2.60M
  
if (2.60M
tab->con[con].is_zero2.60M
)
3199
1.02M
    return 1;
3200
1.57M
  
if (1.57M
tab->con[con].is_redundant1.57M
)
3201
194k
    return 0;
3202
1.38M
  
if (1.38M
!tab->con[con].is_row1.38M
)
3203
777k
    return tab->con[con].index < tab->n_dead;
3204
605k
3205
605k
  row = tab->con[con].index;
3206
605k
3207
605k
  off = 2 + tab->M;
3208
605k
  return isl_int_is_zero(tab->mat->row[row][1]) &&
3209
54.1k
    !row_is_big(tab, row) &&
3210
54.1k
    isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
3211
54.1k
          tab->n_col - tab->n_dead) == -1;
3212
2.60M
}
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
298k
{
3228
298k
  int r;
3229
298k
  enum isl_lp_result res = isl_lp_ok;
3230
298k
  struct isl_tab_var *var;
3231
298k
  struct isl_tab_undo *snap;
3232
298k
3233
298k
  if (!tab)
3234
0
    return isl_lp_error;
3235
298k
3236
298k
  
if (298k
tab->empty298k
)
3237
25
    return isl_lp_empty;
3238
298k
3239
298k
  snap = isl_tab_snap(tab);
3240
298k
  r = isl_tab_add_row(tab, f);
3241
298k
  if (r < 0)
3242
0
    return isl_lp_error;
3243
298k
  var = &tab->con[r];
3244
651k
  for (;;) {
3245
651k
    int row, col;
3246
651k
    find_pivot(tab, var, var, -1, &row, &col);
3247
651k
    if (
row == var->index651k
) {
3248
9.73k
      res = isl_lp_unbounded;
3249
9.73k
      break;
3250
9.73k
    }
3251
641k
    
if (641k
row == -1641k
)
3252
288k
      break;
3253
352k
    
if (352k
isl_tab_pivot(tab, row, col) < 0352k
)
3254
0
      return isl_lp_error;
3255
298k
  }
3256
298k
  
isl_int_mul298k
(tab->mat->row[var->index][0],
3257
298k
        tab->mat->row[var->index][0], denom);
3258
298k
  if (
ISL_FL_ISSET298k
(flags, ISL_TAB_SAVE_DUAL)) {
3259
25.5k
    int i;
3260
25.5k
3261
25.5k
    isl_vec_free(tab->dual);
3262
25.5k
    tab->dual = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_con);
3263
25.5k
    if (!tab->dual)
3264
0
      return isl_lp_error;
3265
25.5k
    
isl_int_set25.5k
(tab->dual->el[0], tab->mat->row[var->index][0]);
3266
1.00M
    for (i = 0; 
i < tab->n_con1.00M
;
++i979k
) {
3267
979k
      int pos;
3268
979k
      if (
tab->con[i].is_row979k
) {
3269
603k
        isl_int_set_si(tab->dual->el[1 + i], 0);
3270
603k
        continue;
3271
603k
      }
3272
376k
      pos = 2 + tab->M + tab->con[i].index;
3273
376k
      if (tab->con[i].negated)
3274
68.0k
        isl_int_neg(tab->dual->el[1 + i],
3275
376k
              tab->mat->row[var->index][pos]);
3276
376k
      else
3277
308k
        isl_int_set(tab->dual->el[1 + i],
3278
979k
              tab->mat->row[var->index][pos]);
3279
979k
    }
3280
25.5k
  }
3281
298k
  
if (298k
opt && 298k
res == isl_lp_ok298k
) {
3282
288k
    if (
opt_denom288k
) {
3283
58.0k
      isl_int_set(*opt, tab->mat->row[var->index][1]);
3284
58.0k
      isl_int_set(*opt_denom, tab->mat->row[var->index][0]);
3285
58.0k
    } else
3286
230k
      get_rounded_sample_value(tab, var, 1, opt);
3287
288k
  }
3288
298k
  if (isl_tab_rollback(tab, snap) < 0)
3289
0
    return isl_lp_error;
3290
298k
  return res;
3291
298k
}
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.95M
{
3303
1.95M
  if (!tab)
3304
0
    return -1;
3305
1.95M
  
if (1.95M
con < 0 || 1.95M
con >= tab->n_con1.95M
)
3306
0
    isl_die(isl_tab_get_ctx(tab), isl_error_invalid,
3307
1.95M
      "position out of bounds", return -1);
3308
1.95M
  
if (1.95M
tab->con[con].is_zero1.95M
)
3309
100
    return 0;
3310
1.95M
  
if (1.95M
tab->con[con].is_redundant1.95M
)
3311
328k
    return 1;
3312
1.62M
  
return tab->con[con].is_row && 1.62M
tab->con[con].index < tab->n_redundant647k
;
3313
1.95M
}
3314
3315
/* Is variable "var" of "tab" fixed to a constant value by its row
3316
 * in the tableau?
3317
 * If so and if "value" is not NULL, then store this constant value
3318
 * in "value".
3319
 *
3320
 * That is, is it a row variable that only has non-zero coefficients
3321
 * for dead columns?
3322
 */
3323
static isl_bool is_constant(struct isl_tab *tab, struct isl_tab_var *var,
3324
  isl_int *value)
3325
4.93k
{
3326
4.93k
  unsigned off = 2 + tab->M;
3327
4.93k
  isl_mat *mat = tab->mat;
3328
4.93k
  int n;
3329
4.93k
  int row;
3330
4.93k
  int pos;
3331
4.93k
3332
4.93k
  if (!var->is_row)
3333
2.95k
    return isl_bool_false;
3334
1.97k
  row = var->index;
3335
1.97k
  if (row_is_big(tab, row))
3336
0
    return isl_bool_false;
3337
1.97k
  n = tab->n_col - tab->n_dead;
3338
1.97k
  pos = isl_seq_first_non_zero(mat->row[row] + off + tab->n_dead, n);
3339
1.97k
  if (pos != -1)
3340
1.76k
    return isl_bool_false;
3341
212
  
if (212
value212
)
3342
0
    isl_int_divexact(*value, mat->row[row][1], mat->row[row][0]);
3343
4.93k
  return isl_bool_true;
3344
4.93k
}
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
5.64k
{
3359
5.64k
  if (row_is_big(tab, var->index))
3360
0
    return 1;
3361
5.64k
  
isl_int_mul5.64k
(*tmp, tab->mat->row[var->index][0], target);
3362
5.64k
  if (sgn > 0)
3363
2.13k
    
return 2.13k
isl_int_ge2.13k
(tab->mat->row[var->index][1], *tmp);
3364
5.64k
  else
3365
3.51k
    
return 3.51k
isl_int_le3.51k
(tab->mat->row[var->index][1], *tmp);
3366
0
}
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
5.88k
{
3387
5.88k
  int row, col;
3388
5.88k
3389
5.88k
  if (
sgn < 0 && 5.88k
min_is_manifestly_unbounded(tab, var)4.72k
)
3390
2.12k
    return isl_bool_true;
3391
3.76k
  
if (3.76k
sgn > 0 && 3.76k
max_is_manifestly_unbounded(tab, var)1.16k
)
3392
0
    return isl_bool_true;
3393
3.76k
  
if (3.76k
to_row(tab, var, sgn) < 03.76k
)
3394
0
    return isl_bool_error;
3395
5.64k
  
while (3.76k
!reached(tab, var, sgn, target, tmp)5.64k
) {
3396
5.13k
    find_pivot(tab, var, var, sgn, &row, &col);
3397
5.13k
    if (row == -1)
3398
1.29k
      return isl_bool_false;
3399
3.83k
    
if (3.83k
row == var->index3.83k
)
3400
1.95k
      return isl_bool_true;
3401
1.88k
    
if (1.88k
isl_tab_pivot(tab, row, col) < 01.88k
)
3402
0
      return isl_bool_error;
3403
5.13k
  }
3404
3.76k
3405
517
  return isl_bool_true;
3406
5.88k
}
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
4.72k
{
3430
4.72k
  isl_bool reached;
3431
4.72k
  isl_vec *eq;
3432
4.72k
  int pos;
3433
4.72k
  isl_stat r;
3434
4.72k
3435
4.72k
  get_rounded_sample_value(tab, var, -1, target);
3436
4.72k
  isl_int_sub_ui(*target, *target, 1);
3437
4.72k
  reached = var_reaches(tab, var, -1, *target, tmp);
3438
4.72k
  if (
reached < 0 || 4.72k
reached4.72k
)
3439
3.55k
    return isl_bool_not(reached);
3440
1.16k
  get_rounded_sample_value(tab, var, 1, target);
3441
1.16k
  isl_int_add_ui(*target, *target, 1);
3442
1.16k
  reached = var_reaches(tab, var, 1, *target, tmp);
3443
1.16k
  if (
reached < 0 || 1.16k
reached1.16k
)
3444
1.04k
    return isl_bool_not(reached);
3445
126
  get_rounded_sample_value(tab, var, -1, tmp);
3446
126
  isl_int_sub_ui(*target, *target, 1);
3447
126
  if (
isl_int_ne126
(*target, *tmp)) {
3448
0
    if (isl_tab_mark_empty(tab) < 0)
3449
0
      return isl_bool_error;
3450
0
    return isl_bool_false;
3451
0
  }
3452
126
3453
126
  
if (126
isl_tab_extend_cons(tab, 1) < 0126
)
3454
0
    return isl_bool_error;
3455
126
  eq = isl_vec_alloc(isl_tab_get_ctx(tab), 1 + tab->n_var);
3456
126
  if (!eq)
3457
0
    return isl_bool_error;
3458
126
  pos = var - tab->var;
3459
126
  isl_seq_clr(eq->el + 1, tab->n_var);
3460
126
  isl_int_set_si(eq->el[1 + pos], -1);
3461
126
  isl_int_set(eq->el[0], *target);
3462
126
  r = isl_tab_add_eq(tab, eq->el);
3463
126
  isl_vec_free(eq);
3464
126
3465
126
  return r < 0 ? 
isl_bool_error0
:
isl_bool_true126
;
3466
4.72k
}
3467
3468
/* Check if variable "var" of "tab" can only attain a single (integer)
3469
 * value, and, if so, add an equality constraint to fix the variable
3470
 * to this single value and store the result in "value" (if "value"
3471
 * is not NULL).
3472
 *
3473
 * If the current sample value involves the big parameter,
3474
 * then the variable cannot have a fixed integer value.
3475
 * If the variable is already fixed to a single value by its row, then
3476
 * there is no need to add another equality constraint.
3477
 *
3478
 * Otherwise, allocate some temporary variables and continue
3479
 * with detect_constant_with_tmp.
3480
 */
3481
static isl_bool get_constant(struct isl_tab *tab, struct isl_tab_var *var,
3482
  isl_int *value)
3483
4.93k
{
3484
4.93k
  isl_int target, tmp;
3485
4.93k
  isl_bool is_cst;
3486
4.93k
3487
4.93k
  if (
var->is_row && 4.93k
row_is_big(tab, var->index)1.97k
)
3488
0
    return isl_bool_false;
3489
4.93k
  is_cst = is_constant(tab, var, value);
3490
4.93k
  if (
is_cst < 0 || 4.93k
is_cst4.93k
)
3491
212
    return is_cst;
3492
4.72k
3493
4.72k
  
if (4.72k
!value4.72k
)
3494
1.91k
    isl_int_init(target);
3495
4.72k
  isl_int_init(tmp);
3496
4.72k
3497
4.72k
  is_cst = detect_constant_with_tmp(tab, var,
3498
4.72k
              value ? 
value2.80k
:
&target1.91k
, &tmp);
3499
4.72k
3500
4.72k
  isl_int_clear(tmp);
3501
4.72k
  if (!value)
3502
1.91k
    isl_int_clear(target);
3503
4.93k
3504
4.93k
  return is_cst;
3505
4.93k
}
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.80k
{
3516
2.80k
  if (!tab)
3517
0
    return isl_bool_error;
3518
2.80k
  
if (2.80k
var < 0 || 2.80k
var >= tab->n_var2.80k
)
3519
0
    isl_die(isl_tab_get_ctx(tab), isl_error_invalid,
3520
2.80k
      "position out of bounds", return isl_bool_error);
3521
2.80k
  
if (2.80k
tab->rational2.80k
)
3522
0
    return isl_bool_false;
3523
2.80k
3524
2.80k
  return get_constant(tab, &tab->var[var], value);
3525
2.80k
}
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
438
{
3535
438
  int i;
3536
438
3537
438
  if (!tab)
3538
0
    return isl_stat_error;
3539
438
  
if (438
tab->rational438
)
3540
0
    return isl_stat_ok;
3541
438
3542
2.56k
  
for (i = 0; 438
i < tab->n_var2.56k
;
++i2.12k
) {
3543
2.12k
    if (get_constant(tab, &tab->var[i], NULL) < 0)
3544
0
      return isl_stat_error;
3545
2.12k
  }
3546
438
3547
438
  return isl_stat_ok;
3548
438
}
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
1.20M
{
3555
1.20M
  if (!tab)
3556
0
    return NULL;
3557
1.20M
  tab->need_undo = 1;
3558
1.20M
  return tab->top;
3559
1.20M
}
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
102
{
3566
102
  if (!tab)
3567
0
    return isl_bool_error;
3568
102
3569
102
  return tab->need_undo;
3570
102
}
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
102
{
3579
102
  if (!tab)
3580
0
    return;
3581
102
3582
102
  free_undo(tab);
3583
102
  tab->need_undo = 0;
3584
102
}
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
786
{
3592
786
  unsigned off = 2 + tab->M;
3593
786
3594
786
  if (
!var->is_row && 786
!max_is_manifestly_unbounded(tab, var)777
)
3595
82
    
if (82
to_row(tab, var, 1) < 082
)
3596
0
      return isl_stat_error;
3597
786
3598
786
  
if (786
var->is_row786
) {
3599
91
    isl_int_sub(tab->mat->row[var->index][1],
3600
91
        tab->mat->row[var->index][1], tab->mat->row[var->index][0]);
3601
91
    if (
var->is_nonneg91
) {
3602
91
      int sgn = restore_row(tab, var);
3603
91
      isl_assert(tab->mat->ctx, sgn >= 0,
3604
91
        return isl_stat_error);
3605
91
    }
3606
786
  } else {
3607
695
    int i;
3608
695
3609
3.82k
    for (i = 0; 
i < tab->n_row3.82k
;
++i3.13k
) {
3610
3.13k
      if (isl_int_is_zero(tab->mat->row[i][off + var->index]))
3611
2.22k
        continue;
3612
909
      
isl_int_add909
(tab->mat->row[i][1], tab->mat->row[i][1],
3613
909
          tab->mat->row[i][off + var->index]);
3614
909
    }
3615
695
3616
695
  }
3617
786
3618
786
  return isl_stat_ok;
3619
786
}
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
417
{
3628
417
  var->is_nonneg = 1;
3629
417
3630
417
  if (
var->is_row && 417
restore_row(tab, var) < -1346
)
3631
0
    return isl_stat_error;
3632
417
3633
417
  return isl_stat_ok;
3634
417
}
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
656k
{
3645
656k
  struct isl_tab_var *var;
3646
656k
3647
656k
  if (tab->n_redundant < 1)
3648
0
    isl_die(isl_tab_get_ctx(tab), isl_error_internal,
3649
656k
      "no redundant rows", return isl_stat_error);
3650
656k
3651
656k
  var = isl_tab_var_from_row(tab, tab->n_redundant - 1);
3652
656k
  var->is_redundant = 0;
3653
656k
  tab->n_redundant--;
3654
656k
  restore_row(tab, var);
3655
656k
3656
656k
  return isl_stat_ok;
3657
656k
}
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
2.58M
{
3663
2.58M
  struct isl_tab_var *var = var_from_index(tab, undo->u.var_index);
3664
2.58M
  switch (undo->type) {
3665
524k
  case isl_tab_undo_nonneg:
3666
524k
    var->is_nonneg = 0;
3667
524k
    break;
3668
519k
  case isl_tab_undo_redundant:
3669
519k
    if (
!var->is_row || 519k
var->index != tab->n_redundant - 1519k
)
3670
0
      isl_die(isl_tab_get_ctx(tab), isl_error_internal,
3671
519k
        "not undoing last redundant row", return -1);
3672
519k
    return restore_last_redundant(tab);
3673
269k
  case isl_tab_undo_freeze:
3674
269k
    var->frozen = 0;
3675
269k
    break;
3676
79.8k
  case isl_tab_undo_zero:
3677
79.8k
    var->is_zero = 0;
3678
79.8k
    if (!var->is_row)
3679
79.5k
      tab->n_dead--;
3680
79.8k
    break;
3681
1.19M
  case isl_tab_undo_allocate:
3682
1.19M
    if (
undo->u.var_index >= 01.19M
) {
3683
3.27k
      isl_assert(tab->mat->ctx, !var->is_row,
3684
3.27k
        return isl_stat_error);
3685
3.27k
      return drop_col(tab, var->index);
3686
1.18M
    }
3687
1.18M
    
if (1.18M
!var->is_row1.18M
) {
3688
114k
      if (
!max_is_manifestly_unbounded(tab, var)114k
) {
3689
81.9k
        if (to_row(tab, var, 1) < 0)
3690
0
          return isl_stat_error;
3691
32.3k
      } else 
if (32.3k
!min_is_manifestly_unbounded(tab, var)32.3k
) {
3692
13.9k
        if (to_row(tab, var, -1) < 0)
3693
0
          return isl_stat_error;
3694
32.3k
      } else
3695
18.3k
        
if (18.3k
to_row(tab, var, 0) < 018.3k
)
3696
0
          return isl_stat_error;
3697
1.18M
    }
3698
1.18M
    return drop_row(tab, var->index);
3699
786
  case isl_tab_undo_relax:
3700
786
    return unrelax(tab, var);
3701
417
  case isl_tab_undo_unrestrict:
3702
417
    return ununrestrict(tab, var);
3703
0
  default:
3704
0
    isl_die(tab->mat->ctx, isl_error_internal,
3705
2.58M
      "perform_undo_var called on invalid undo record",
3706
2.58M
      return isl_stat_error);
3707
2.58M
  }
3708
2.58M
3709
873k
  return isl_stat_ok;
3710
2.58M
}
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
110k
{
3721
110k
  if (!tab)
3722
0
    return isl_stat_error;
3723
110k
3724
110k
  
if (110k
tab->need_undo110k
)
3725
0
    isl_die(isl_tab_get_ctx(tab), isl_error_invalid,
3726
110k
      "manually restoring redundant constraints "
3727
110k
      "interferes with undo history",
3728
110k
      return isl_stat_error);
3729
110k
3730
247k
  
while (110k
tab->n_redundant > 0247k
) {
3731
136k
    if (
tab->row_var[tab->n_redundant - 1] >= 0136k
) {
3732
120k
      struct isl_tab_var *var;
3733
120k
3734
120k
      var = isl_tab_var_from_row(tab, tab->n_redundant - 1);
3735
120k
      var->is_nonneg = 0;
3736
120k
    }
3737
136k
    restore_last_redundant(tab);
3738
136k
  }
3739
110k
  return isl_stat_ok;
3740
110k
}
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
1.11k
{
3747
1.11k
  int off;
3748
1.11k
3749
1.11k
  off = tab->n_var - isl_basic_map_dim(tab->bmap, isl_dim_div);
3750
1.11k
  if (isl_basic_map_drop_div(tab->bmap, pos - off) < 0)
3751
0
    return isl_stat_error;
3752
1.11k
  
if (1.11k
tab->samples1.11k
) {
3753
439
    tab->samples = isl_mat_drop_cols(tab->samples, 1 + pos, 1);
3754
439
    if (!tab->samples)
3755
0
      return isl_stat_error;
3756
1.11k
  }
3757
1.11k
3758
1.11k
  return isl_stat_ok;
3759
1.11k
}
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
591
{
3774
591
  int i, j;
3775
591
  int n_extra = 0;
3776
591
  int *extra = NULL;  /* current columns that contain bad stuff */
3777
591
  unsigned off = 2 + tab->M;
3778
591
3779
591
  extra = isl_alloc_array(tab->mat->ctx, int, tab->n_col);
3780
591
  if (
tab->n_col && 591
!extra591
)
3781
0
    goto error;
3782
7.05k
  
for (i = 0; 591
i < tab->n_col7.05k
;
++i6.46k
) {
3783
53.1k
    for (j = 0; 
j < tab->n_col53.1k
;
++j46.6k
)
3784
51.6k
      
if (51.6k
tab->col_var[i] == col_var[j]51.6k
)
3785
4.99k
        break;
3786
6.46k
    if (j < tab->n_col)
3787
4.99k
      continue;
3788
1.47k
    extra[n_extra++] = i;
3789
1.47k
  }
3790
5.37k
  for (i = 0; 
i < tab->n_col && 5.37k
n_extra > 05.26k
;
++i4.78k
) {
3791
4.78k
    struct isl_tab_var *var;
3792
4.78k
    int row;
3793
4.78k
3794
36.8k
    for (j = 0; 
j < tab->n_col36.8k
;
++j32.0k
)
3795
35.3k
      
if (35.3k
col_var[i] == tab->col_var[j]35.3k
)
3796
3.31k
        break;
3797
4.78k
    if (j < tab->n_col)
3798
3.31k
      continue;
3799
1.47k
    var = var_from_index(tab, col_var[i]);
3800
1.47k
    row = var->index;
3801
1.87k
    for (j = 0; 
j < n_extra1.87k
;
++j400
)
3802
1.87k
      
if (1.87k
!1.87k
isl_int_is_zero1.87k
(tab->mat->row[row][off+extra[j]]))
3803
1.47k
        break;
3804
1.47k
    isl_assert(tab->mat->ctx, j < n_extra, goto error);
3805
1.47k
    
if (1.47k
isl_tab_pivot(tab, row, extra[j]) < 01.47k
)
3806
0
      goto error;
3807
1.47k
    extra[j] = extra[--n_extra];
3808
1.47k
  }
3809
591
3810
591
  free(extra);
3811
591
  return 0;
3812
0
error:
3813
0
  free(extra);
3814
0
  return -1;
3815
591
}
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
23.0k
{
3823
23.0k
  int i;
3824
23.0k
3825
28.2k
  for (i = tab->n_sample - 1; 
i >= 0 && 28.2k
tab->n_sample > n26.5k
;
--i5.16k
) {
3826
5.16k
    if (tab->sample_index[i] < n)
3827
2.00k
      continue;
3828
3.16k
3829
3.16k
    
if (3.16k
i != tab->n_sample - 13.16k
) {
3830
2.16k
      int t = tab->sample_index[tab->n_sample-1];
3831
2.16k
      tab->sample_index[tab->n_sample-1] = tab->sample_index[i];
3832
2.16k
      tab->sample_index[i] = t;
3833
2.16k
      isl_mat_swap_rows(tab->samples, tab->n_sample-1, i);
3834
2.16k
    }
3835
5.16k
    tab->n_sample--;
3836
5.16k
  }
3837
23.0k
}
3838
3839
static isl_stat perform_undo(struct isl_tab *tab, struct isl_tab_undo *undo)
3840
  WARN_UNUSED;
3841
static isl_stat perform_undo(struct isl_tab *tab, struct isl_tab_undo *undo)
3842
2.96M
{
3843
2.96M
  switch (undo->type) {
3844
10.3k
  case isl_tab_undo_rational:
3845
10.3k
    tab->rational = 0;
3846
10.3k
    break;
3847
64.7k
  case isl_tab_undo_empty:
3848
64.7k
    tab->empty = 0;
3849
64.7k
    break;
3850
2.58M
  case isl_tab_undo_nonneg:
3851
2.58M
  case isl_tab_undo_redundant:
3852
2.58M
  case isl_tab_undo_freeze:
3853
2.58M
  case isl_tab_undo_zero:
3854
2.58M
  case isl_tab_undo_allocate:
3855
2.58M
  case isl_tab_undo_relax:
3856
2.58M
  case isl_tab_undo_unrestrict:
3857
2.58M
    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
268k
  case isl_tab_undo_bmap_ineq:
3861
268k
    return isl_basic_map_free_inequality(tab->bmap, 1);
3862
1.11k
  case isl_tab_undo_bmap_div:
3863
1.11k
    return drop_bmap_div(tab, undo->u.var_index);
3864
591
  case isl_tab_undo_saved_basis:
3865
591
    if (restore_basis(tab, undo->u.col_var) < 0)
3866
0
      return isl_stat_error;
3867
591
    break;
3868
5.74k
  case isl_tab_undo_drop_sample:
3869
5.74k
    tab->n_outside--;
3870
5.74k
    break;
3871
23.0k
  case isl_tab_undo_saved_samples:
3872
23.0k
    drop_samples_since(tab, undo->u.n);
3873
23.0k
    break;
3874
2.40k
  case isl_tab_undo_callback:
3875
2.40k
    return undo->u.callback->run(undo->u.callback);
3876
0
  default:
3877
0
    isl_assert(tab->mat->ctx, 0, return isl_stat_error);
3878
2.96M
  }
3879
104k
  return isl_stat_ok;
3880
2.96M
}
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
1.10M
{
3887
1.10M
  struct isl_tab_undo *undo, *next;
3888
1.10M
3889
1.10M
  if (!tab)
3890
0
    return -1;
3891
1.10M
3892
1.10M
  tab->in_undo = 1;
3893
4.06M
  for (undo = tab->top; 
undo && 4.06M
undo != &tab->bottom4.06M
;
undo = next2.96M
) {
3894
3.26M
    next = undo->next;
3895
3.26M
    if (undo == snap)
3896
304k
      break;
3897
2.96M
    
if (2.96M
perform_undo(tab, undo) < 02.96M
) {
3898
0
      tab->top = undo;
3899
0
      free_undo(tab);
3900
0
      tab->in_undo = 0;
3901
0
      return -1;
3902
0
    }
3903
2.96M
    free_undo_record(undo);
3904
2.96M
  }
3905
1.10M
  tab->in_undo = 0;
3906
1.10M
  tab->top = undo;
3907
1.10M
  if (!undo)
3908
0
    return -1;
3909
1.10M
  return 0;
3910
1.10M
}
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
102k
{
3924
102k
  int pos;
3925
102k
  unsigned off = 2 + tab->M;
3926
102k
3927
102k
  if (tab->rational)
3928
9.20k
    return isl_ineq_separate;
3929
93.4k
3930
93.4k
  
if (93.4k
!93.4k
isl_int_is_one93.4k
(tab->mat->row[row][0]))
3931
189
    return isl_ineq_separate;
3932
93.2k
3933
93.2k
  pos = isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
3934
93.2k
          tab->n_col - tab->n_dead);
3935
93.2k
  if (
pos == -193.2k
) {
3936
22.1k
    if (isl_int_is_negone(tab->mat->row[row][1]))
3937
14.3k
      return isl_ineq_adj_eq;
3938
22.1k
    else
3939
7.79k
      return isl_ineq_separate;
3940
71.1k
  }
3941
71.1k
3942
71.1k
  
if (71.1k
!71.1k
isl_int_eq71.1k
(tab->mat->row[row][1],
3943
71.1k
      tab->mat->row[row][off + tab->n_dead + pos]))
3944
28.6k
    return isl_ineq_separate;
3945
42.5k
3946
42.5k
  pos = isl_seq_first_non_zero(
3947
42.5k
      tab->mat->row[row] + off + tab->n_dead + pos + 1,
3948
42.5k
      tab->n_col - tab->n_dead - pos - 1);
3949
42.5k
3950
42.5k
  return pos == -1 ? 
isl_ineq_adj_ineq38.8k
:
isl_ineq_separate3.68k
;
3951
102k
}
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
537k
{
3963
537k
  enum isl_ineq_type type = isl_ineq_error;
3964
537k
  struct isl_tab_undo *snap = NULL;
3965
537k
  int con;
3966
537k
  int row;
3967
537k
3968
537k
  if (!tab)
3969
0
    return isl_ineq_error;
3970
537k
3971
537k
  
if (537k
isl_tab_extend_cons(tab, 1) < 0537k
)
3972
0
    return isl_ineq_error;
3973
537k
3974
537k
  snap = isl_tab_snap(tab);
3975
537k
3976
537k
  con = isl_tab_add_row(tab, ineq);
3977
537k
  if (con < 0)
3978
0
    goto error;
3979
537k
3980
537k
  row = tab->con[con].index;
3981
537k
  if (isl_tab_row_is_redundant(tab, row))
3982
0
    type = isl_ineq_redundant;
3983
537k
  else 
if (537k
isl_int_is_neg537k
(tab->mat->row[row][1]) &&
3984
193k
     (tab->rational ||
3985
170k
        isl_int_abs_ge(tab->mat->row[row][1],
3986
192k
           tab->mat->row[row][0]))) {
3987
192k
    int nonneg = at_least_zero(tab, &tab->con[con]);
3988
192k
    if (nonneg < 0)
3989
0
      goto error;
3990
192k
    
if (192k
nonneg192k
)
3991
89.9k
      type = isl_ineq_cut;
3992
192k
    else
3993
102k
      type = separation_type(tab, row);
3994
537k
  } else {
3995
344k
    int red = con_is_redundant(tab, &tab->con[con]);
3996
344k
    if (red < 0)
3997
0
      goto error;
3998
344k
    
if (344k
!red344k
)
3999
90.8k
      type = isl_ineq_cut;
4000
344k
    else
4001
254k
      type = isl_ineq_redundant;
4002
537k
  }
4003
537k
4004
537k
  
if (537k
isl_tab_rollback(tab, snap)537k
)
4005
0
    return isl_ineq_error;
4006
537k
  return type;
4007
0
error:
4008
0
  return isl_ineq_error;
4009
0
}
4010
4011
isl_stat isl_tab_track_bmap(struct isl_tab *tab, __isl_take isl_basic_map *bmap)
4012
191k
{
4013
191k
  bmap = isl_basic_map_cow(bmap);
4014
191k
  if (
!tab || 191k
!bmap191k
)
4015
0
    goto error;
4016
191k
4017
191k
  
if (191k
tab->empty191k
) {
4018
4.06k
    bmap = isl_basic_map_set_to_empty(bmap);
4019
4.06k
    if (!bmap)
4020
0
      goto error;
4021
4.06k
    tab->bmap = bmap;
4022
4.06k
    return isl_stat_ok;
4023
4.06k
  }
4024
187k
4025
187k
  
isl_assert187k
(tab->mat->ctx, tab->n_eq == bmap->n_eq, goto error);
4026
187k
  
isl_assert187k
(tab->mat->ctx,
4027
187k
        tab->n_con == bmap->n_eq + bmap->n_ineq, goto error);
4028
187k
4029
187k
  tab->bmap = bmap;
4030
187k
4031
187k
  return isl_stat_ok;
4032
0
error:
4033
0
  isl_basic_map_free(bmap);
4034
0
  return isl_stat_error;
4035
191k
}
4036
4037
isl_stat isl_tab_track_bset(struct isl_tab *tab, __isl_take isl_basic_set *bset)
4038
965
{
4039
965
  return isl_tab_track_bmap(tab, bset_to_bmap(bset));
4040
965
}
4041
4042
__isl_keep isl_basic_set *isl_tab_peek_bset(struct isl_tab *tab)
4043
28.2k
{
4044
28.2k
  if (!tab)
4045
0
    return NULL;
4046
28.2k
4047
28.2k
  return bset_from_bmap(tab->bmap);
4048
28.2k
}
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
) {
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
) {
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
) {
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
) {
4091
0
    const char *sign = "";
4092
0
    if (i)
4093
0
      fprintf(out, ", ");
4094
0
    if (
tab->row_sign0
) {
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
) {
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
}