Coverage Report

Created: 2017-11-21 16:49

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/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
280k
{
36
280k
  int i;
37
280k
  struct isl_tab *tab;
38
280k
  unsigned off = 2 + M;
39
280k
40
280k
  tab = isl_calloc_type(ctx, struct isl_tab);
41
280k
  if (!tab)
42
0
    return NULL;
43
280k
  tab->mat = isl_mat_alloc(ctx, n_row, off + n_var);
44
280k
  if (!tab->mat)
45
0
    goto error;
46
280k
  tab->var = isl_alloc_array(ctx, struct isl_tab_var, n_var);
47
280k
  if (n_var && 
!tab->var280k
)
48
0
    goto error;
49
280k
  tab->con = isl_alloc_array(ctx, struct isl_tab_var, n_row);
50
280k
  if (n_row && !tab->con)
51
0
    goto error;
52
280k
  tab->col_var = isl_alloc_array(ctx, int, n_var);
53
280k
  if (n_var && 
!tab->col_var280k
)
54
0
    goto error;
55
280k
  tab->row_var = isl_alloc_array(ctx, int, n_row);
56
280k
  if (n_row && !tab->row_var)
57
0
    goto error;
58
1.77M
  
for (i = 0; 280k
i < n_var;
++i1.49M
) {
59
1.49M
    tab->var[i].index = i;
60
1.49M
    tab->var[i].is_row = 0;
61
1.49M
    tab->var[i].is_nonneg = 0;
62
1.49M
    tab->var[i].is_zero = 0;
63
1.49M
    tab->var[i].is_redundant = 0;
64
1.49M
    tab->var[i].frozen = 0;
65
1.49M
    tab->var[i].negated = 0;
66
1.49M
    tab->col_var[i] = i;
67
1.49M
  }
68
280k
  tab->n_row = 0;
69
280k
  tab->n_con = 0;
70
280k
  tab->n_eq = 0;
71
280k
  tab->max_con = n_row;
72
280k
  tab->n_col = n_var;
73
280k
  tab->n_var = n_var;
74
280k
  tab->max_var = n_var;
75
280k
  tab->n_param = 0;
76
280k
  tab->n_div = 0;
77
280k
  tab->n_dead = 0;
78
280k
  tab->n_redundant = 0;
79
280k
  tab->strict_redundant = 0;
80
280k
  tab->need_undo = 0;
81
280k
  tab->rational = 0;
82
280k
  tab->empty = 0;
83
280k
  tab->in_undo = 0;
84
280k
  tab->M = M;
85
280k
  tab->cone = 0;
86
280k
  tab->bottom.type = isl_tab_undo_bottom;
87
280k
  tab->bottom.next = NULL;
88
280k
  tab->top = &tab->bottom;
89
280k
90
280k
  tab->n_zero = 0;
91
280k
  tab->n_unbounded = 0;
92
280k
  tab->basis = NULL;
93
280k
94
280k
  return tab;
95
0
error:
96
0
  isl_tab_free(tab);
97
0
  return NULL;
98
280k
}
99
100
isl_ctx *isl_tab_get_ctx(struct isl_tab *tab)
101
1.49M
{
102
1.49M
  return tab ? isl_mat_get_ctx(tab->mat) : NULL;
103
1.49M
}
104
105
int isl_tab_extend_cons(struct isl_tab *tab, unsigned n_new)
106
268k
{
107
268k
  unsigned off;
108
268k
109
268k
  if (!tab)
110
0
    return -1;
111
268k
112
268k
  off = 2 + tab->M;
113
268k
114
268k
  if (tab->max_con < tab->n_con + n_new) {
115
37.2k
    struct isl_tab_var *con;
116
37.2k
117
37.2k
    con = isl_realloc_array(tab->mat->ctx, tab->con,
118
37.2k
            struct isl_tab_var, tab->max_con + n_new);
119
37.2k
    if (!con)
120
0
      return -1;
121
37.2k
    tab->con = con;
122
37.2k
    tab->max_con += n_new;
123
37.2k
  }
124
268k
  if (tab->mat->n_row < tab->n_row + n_new) {
125
38.3k
    int *row_var;
126
38.3k
127
38.3k
    tab->mat = isl_mat_extend(tab->mat,
128
38.3k
          tab->n_row + n_new, off + tab->n_col);
129
38.3k
    if (!tab->mat)
130
0
      return -1;
131
38.3k
    row_var = isl_realloc_array(tab->mat->ctx, tab->row_var,
132
38.3k
              int, tab->mat->n_row);
133
38.3k
    if (!row_var)
134
0
      return -1;
135
38.3k
    tab->row_var = row_var;
136
38.3k
    if (tab->row_sign) {
137
105
      enum isl_tab_row_sign *s;
138
105
      s = isl_realloc_array(tab->mat->ctx, tab->row_sign,
139
105
          enum isl_tab_row_sign, tab->mat->n_row);
140
105
      if (!s)
141
0
        return -1;
142
105
      tab->row_sign = s;
143
105
    }
144
38.3k
  }
145
268k
  return 0;
146
268k
}
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
2.33k
{
153
2.33k
  struct isl_tab_var *var;
154
2.33k
  unsigned off = 2 + tab->M;
155
2.33k
156
2.33k
  if (tab->max_var < tab->n_var + n_new) {
157
1.89k
    var = isl_realloc_array(tab->mat->ctx, tab->var,
158
1.89k
            struct isl_tab_var, tab->n_var + n_new);
159
1.89k
    if (!var)
160
0
      return -1;
161
1.89k
    tab->var = var;
162
1.89k
    tab->max_var = tab->n_var + n_new;
163
1.89k
  }
164
2.33k
165
2.33k
  if (tab->mat->n_col < off + tab->n_col + n_new) {
166
1.21k
    int *p;
167
1.21k
168
1.21k
    tab->mat = isl_mat_extend(tab->mat,
169
1.21k
            tab->mat->n_row, off + tab->n_col + n_new);
170
1.21k
    if (!tab->mat)
171
0
      return -1;
172
1.21k
    p = isl_realloc_array(tab->mat->ctx, tab->col_var,
173
1.21k
              int, tab->n_col + n_new);
174
1.21k
    if (!p)
175
0
      return -1;
176
1.21k
    tab->col_var = p;
177
1.21k
  }
178
2.33k
179
2.33k
  return 0;
180
2.33k
}
181
182
static void free_undo_record(struct isl_tab_undo *undo)
183
1.48M
{
184
1.48M
  switch (undo->type) {
185
1.48M
  case isl_tab_undo_saved_basis:
186
65
    free(undo->u.col_var);
187
65
    break;
188
1.48M
  
default:;1.48M
189
1.48M
  }
190
1.48M
  free(undo);
191
1.48M
}
192
193
static void free_undo(struct isl_tab *tab)
194
283k
{
195
283k
  struct isl_tab_undo *undo, *next;
196
283k
197
534k
  for (undo = tab->top; undo && undo != &tab->bottom; 
undo = next250k
) {
198
250k
    next = undo->next;
199
250k
    free_undo_record(undo);
200
250k
  }
201
283k
  tab->top = undo;
202
283k
}
203
204
void isl_tab_free(struct isl_tab *tab)
205
294k
{
206
294k
  if (!tab)
207
10.7k
    return;
208
283k
  free_undo(tab);
209
283k
  isl_mat_free(tab->mat);
210
283k
  isl_vec_free(tab->dual);
211
283k
  isl_basic_map_free(tab->bmap);
212
283k
  free(tab->var);
213
283k
  free(tab->con);
214
283k
  free(tab->row_var);
215
283k
  free(tab->col_var);
216
283k
  free(tab->row_sign);
217
283k
  isl_mat_free(tab->samples);
218
283k
  free(tab->sample_index);
219
283k
  isl_mat_free(tab->basis);
220
283k
  free(tab);
221
283k
}
222
223
struct isl_tab *isl_tab_dup(struct isl_tab *tab)
224
945
{
225
945
  int i;
226
945
  struct isl_tab *dup;
227
945
  unsigned off;
228
945
229
945
  if (!tab)
230
0
    return NULL;
231
945
232
945
  off = 2 + tab->M;
233
945
  dup = isl_calloc_type(tab->mat->ctx, struct isl_tab);
234
945
  if (!dup)
235
0
    return NULL;
236
945
  dup->mat = isl_mat_dup(tab->mat);
237
945
  if (!dup->mat)
238
0
    goto error;
239
945
  dup->var = isl_alloc_array(tab->mat->ctx, struct isl_tab_var, tab->max_var);
240
945
  if (tab->max_var && !dup->var)
241
0
    goto error;
242
8.63k
  
for (i = 0; 945
i < tab->n_var;
++i7.69k
)
243
7.69k
    dup->var[i] = tab->var[i];
244
945
  dup->con = isl_alloc_array(tab->mat->ctx, struct isl_tab_var, tab->max_con);
245
945
  if (tab->max_con && !dup->con)
246
0
    goto error;
247
9.46k
  
for (i = 0; 945
i < tab->n_con;
++i8.51k
)
248
8.51k
    dup->con[i] = tab->con[i];
249
945
  dup->col_var = isl_alloc_array(tab->mat->ctx, int, tab->mat->n_col - off);
250
945
  if ((tab->mat->n_col - off) && !dup->col_var)
251
0
    goto error;
252
4.75k
  
for (i = 0; 945
i < tab->n_col;
++i3.80k
)
253
3.80k
    dup->col_var[i] = tab->col_var[i];
254
945
  dup->row_var = isl_alloc_array(tab->mat->ctx, int, tab->mat->n_row);
255
945
  if (tab->mat->n_row && !dup->row_var)
256
0
    goto error;
257
9.45k
  
for (i = 0; 945
i < tab->n_row;
++i8.51k
)
258
8.51k
    dup->row_var[i] = tab->row_var[i];
259
945
  if (tab->row_sign) {
260
945
    dup->row_sign = isl_alloc_array(tab->mat->ctx, enum isl_tab_row_sign,
261
945
            tab->mat->n_row);
262
945
    if (tab->mat->n_row && !dup->row_sign)
263
0
      goto error;
264
9.45k
    
for (i = 0; 945
i < tab->n_row;
++i8.51k
)
265
8.51k
      dup->row_sign[i] = tab->row_sign[i];
266
945
  }
267
945
  if (tab->samples) {
268
0
    dup->samples = isl_mat_dup(tab->samples);
269
0
    if (!dup->samples)
270
0
      goto error;
271
0
    dup->sample_index = isl_alloc_array(tab->mat->ctx, int,
272
0
              tab->samples->n_row);
273
0
    if (tab->samples->n_row && !dup->sample_index)
274
0
      goto error;
275
0
    dup->n_sample = tab->n_sample;
276
0
    dup->n_outside = tab->n_outside;
277
0
  }
278
945
  dup->n_row = tab->n_row;
279
945
  dup->n_con = tab->n_con;
280
945
  dup->n_eq = tab->n_eq;
281
945
  dup->max_con = tab->max_con;
282
945
  dup->n_col = tab->n_col;
283
945
  dup->n_var = tab->n_var;
284
945
  dup->max_var = tab->max_var;
285
945
  dup->n_param = tab->n_param;
286
945
  dup->n_div = tab->n_div;
287
945
  dup->n_dead = tab->n_dead;
288
945
  dup->n_redundant = tab->n_redundant;
289
945
  dup->rational = tab->rational;
290
945
  dup->empty = tab->empty;
291
945
  dup->strict_redundant = 0;
292
945
  dup->need_undo = 0;
293
945
  dup->in_undo = 0;
294
945
  dup->M = tab->M;
295
945
  tab->cone = tab->cone;
296
945
  dup->bottom.type = isl_tab_undo_bottom;
297
945
  dup->bottom.next = NULL;
298
945
  dup->top = &dup->bottom;
299
945
300
945
  dup->n_zero = tab->n_zero;
301
945
  dup->n_unbounded = tab->n_unbounded;
302
945
  dup->basis = isl_mat_dup(tab->basis);
303
945
304
945
  return dup;
305
0
error:
306
0
  isl_tab_free(dup);
307
0
  return NULL;
308
945
}
309
310
/* Construct the coefficient matrix of the product tableau
311
 * of two tableaus.
312
 * mat{1,2} is the coefficient matrix of tableau {1,2}
313
 * row{1,2} is the number of rows in tableau {1,2}
314
 * col{1,2} is the number of columns in tableau {1,2}
315
 * off is the offset to the coefficient column (skipping the
316
 *  denominator, the constant term and the big parameter if any)
317
 * r{1,2} is the number of redundant rows in tableau {1,2}
318
 * d{1,2} is the number of dead columns in tableau {1,2}
319
 *
320
 * The order of the rows and columns in the result is as explained
321
 * in isl_tab_product.
322
 */
323
static struct isl_mat *tab_mat_product(struct isl_mat *mat1,
324
  struct isl_mat *mat2, unsigned row1, unsigned row2,
325
  unsigned col1, unsigned col2,
326
  unsigned off, unsigned r1, unsigned r2, unsigned d1, unsigned d2)
327
1.85k
{
328
1.85k
  int i;
329
1.85k
  struct isl_mat *prod;
330
1.85k
  unsigned n;
331
1.85k
332
1.85k
  prod = isl_mat_alloc(mat1->ctx, mat1->n_row + mat2->n_row,
333
1.85k
          off + col1 + col2);
334
1.85k
  if (!prod)
335
0
    return NULL;
336
1.85k
337
1.85k
  n = 0;
338
6.25k
  for (i = 0; i < r1; 
++i4.40k
) {
339
4.40k
    isl_seq_cpy(prod->row[n + i], mat1->row[i], off + d1);
340
4.40k
    isl_seq_clr(prod->row[n + i] + off + d1, d2);
341
4.40k
    isl_seq_cpy(prod->row[n + i] + off + d1 + d2,
342
4.40k
        mat1->row[i] + off + d1, col1 - d1);
343
4.40k
    isl_seq_clr(prod->row[n + i] + off + col1 + d1, col2 - d2);
344
4.40k
  }
345
1.85k
346
1.85k
  n += r1;
347
6.25k
  for (i = 0; i < r2; 
++i4.40k
) {
348
4.40k
    isl_seq_cpy(prod->row[n + i], mat2->row[i], off);
349
4.40k
    isl_seq_clr(prod->row[n + i] + off, d1);
350
4.40k
    isl_seq_cpy(prod->row[n + i] + off + d1,
351
4.40k
          mat2->row[i] + off, d2);
352
4.40k
    isl_seq_clr(prod->row[n + i] + off + d1 + d2, col1 - d1);
353
4.40k
    isl_seq_cpy(prod->row[n + i] + off + col1 + d1,
354
4.40k
          mat2->row[i] + off + d2, col2 - d2);
355
4.40k
  }
356
1.85k
357
1.85k
  n += r2;
358
22.9k
  for (i = 0; i < row1 - r1; 
++i21.1k
) {
359
21.1k
    isl_seq_cpy(prod->row[n + i], mat1->row[r1 + i], off + d1);
360
21.1k
    isl_seq_clr(prod->row[n + i] + off + d1, d2);
361
21.1k
    isl_seq_cpy(prod->row[n + i] + off + d1 + d2,
362
21.1k
        mat1->row[r1 + i] + off + d1, col1 - d1);
363
21.1k
    isl_seq_clr(prod->row[n + i] + off + col1 + d1, col2 - d2);
364
21.1k
  }
365
1.85k
366
1.85k
  n += row1 - r1;
367
22.9k
  for (i = 0; i < row2 - r2; 
++i21.1k
) {
368
21.1k
    isl_seq_cpy(prod->row[n + i], mat2->row[r2 + i], off);
369
21.1k
    isl_seq_clr(prod->row[n + i] + off, d1);
370
21.1k
    isl_seq_cpy(prod->row[n + i] + off + d1,
371
21.1k
          mat2->row[r2 + i] + off, d2);
372
21.1k
    isl_seq_clr(prod->row[n + i] + off + d1 + d2, col1 - d1);
373
21.1k
    isl_seq_cpy(prod->row[n + i] + off + col1 + d1,
374
21.1k
          mat2->row[r2 + i] + off + d2, col2 - d2);
375
21.1k
  }
376
1.85k
377
1.85k
  return prod;
378
1.85k
}
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
36.5k
{
386
36.5k
  if (var->index == -1)
387
86
    return;
388
36.4k
  if (var->is_row && 
var->index >= r125.5k
)
389
21.1k
    var->index += r2;
390
36.4k
  if (!var->is_row && 
var->index >= d110.9k
)
391
10.4k
    var->index += d2;
392
36.5k
}
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
36.5k
{
401
36.5k
  if (var->index == -1)
402
86
    return;
403
36.4k
  if (var->is_row) {
404
25.5k
    if (var->index < r2)
405
4.40k
      var->index += r1;
406
21.1k
    else
407
21.1k
      var->index += row1;
408
25.5k
  } else {
409
10.9k
    if (var->index < d2)
410
545
      var->index += d1;
411
10.4k
    else
412
10.4k
      var->index += col1;
413
10.9k
  }
414
36.5k
}
415
416
/* Create a tableau that represents the Cartesian product of the sets
417
 * represented by tableaus tab1 and tab2.
418
 * The order of the rows in the product is
419
 *  - redundant rows of tab1
420
 *  - redundant rows of tab2
421
 *  - non-redundant rows of tab1
422
 *  - non-redundant rows of tab2
423
 * The order of the columns is
424
 *  - denominator
425
 *  - constant term
426
 *  - coefficient of big parameter, if any
427
 *  - dead columns of tab1
428
 *  - dead columns of tab2
429
 *  - live columns of tab1
430
 *  - live columns of tab2
431
 * The order of the variables and the constraints is a concatenation
432
 * of order in the two input tableaus.
433
 */
434
struct isl_tab *isl_tab_product(struct isl_tab *tab1, struct isl_tab *tab2)
435
1.85k
{
436
1.85k
  int i;
437
1.85k
  struct isl_tab *prod;
438
1.85k
  unsigned off;
439
1.85k
  unsigned r1, r2, d1, d2;
440
1.85k
441
1.85k
  if (!tab1 || !tab2)
442
0
    return NULL;
443
1.85k
444
1.85k
  isl_assert(tab1->mat->ctx, tab1->M == tab2->M, return NULL);
445
1.85k
  isl_assert(tab1->mat->ctx, tab1->rational == tab2->rational, return NULL);
446
1.85k
  isl_assert(tab1->mat->ctx, tab1->cone == tab2->cone, return NULL);
447
1.85k
  isl_assert(tab1->mat->ctx, !tab1->row_sign, return NULL);
448
1.85k
  isl_assert(tab1->mat->ctx, !tab2->row_sign, return NULL);
449
1.85k
  isl_assert(tab1->mat->ctx, tab1->n_param == 0, return NULL);
450
1.85k
  isl_assert(tab1->mat->ctx, tab2->n_param == 0, return NULL);
451
1.85k
  isl_assert(tab1->mat->ctx, tab1->n_div == 0, return NULL);
452
1.85k
  isl_assert(tab1->mat->ctx, tab2->n_div == 0, return NULL);
453
1.85k
454
1.85k
  off = 2 + tab1->M;
455
1.85k
  r1 = tab1->n_redundant;
456
1.85k
  r2 = tab2->n_redundant;
457
1.85k
  d1 = tab1->n_dead;
458
1.85k
  d2 = tab2->n_dead;
459
1.85k
  prod = isl_calloc_type(tab1->mat->ctx, struct isl_tab);
460
1.85k
  if (!prod)
461
0
    return NULL;
462
1.85k
  prod->mat = tab_mat_product(tab1->mat, tab2->mat,
463
1.85k
        tab1->n_row, tab2->n_row,
464
1.85k
        tab1->n_col, tab2->n_col, off, r1, r2, d1, d2);
465
1.85k
  if (!prod->mat)
466
0
    goto error;
467
1.85k
  prod->var = isl_alloc_array(tab1->mat->ctx, struct isl_tab_var,
468
1.85k
          tab1->max_var + tab2->max_var);
469
1.85k
  if ((tab1->max_var + tab2->max_var) && !prod->var)
470
0
    goto error;
471
12.8k
  
for (i = 0; 1.85k
i < tab1->n_var;
++i11.0k
) {
472
11.0k
    prod->var[i] = tab1->var[i];
473
11.0k
    update_index1(&prod->var[i], r1, r2, d1, d2);
474
11.0k
  }
475
12.8k
  for (i = 0; i < tab2->n_var; 
++i11.0k
) {
476
11.0k
    prod->var[tab1->n_var + i] = tab2->var[i];
477
11.0k
    update_index2(&prod->var[tab1->n_var + i],
478
11.0k
        tab1->n_row, tab1->n_col,
479
11.0k
        r1, r2, d1, d2);
480
11.0k
  }
481
1.85k
  prod->con = isl_alloc_array(tab1->mat->ctx, struct isl_tab_var,
482
1.85k
          tab1->max_con +  tab2->max_con);
483
1.85k
  if ((tab1->max_con + tab2->max_con) && !prod->con)
484
0
    goto error;
485
27.3k
  
for (i = 0; 1.85k
i < tab1->n_con;
++i25.5k
) {
486
25.5k
    prod->con[i] = tab1->con[i];
487
25.5k
    update_index1(&prod->con[i], r1, r2, d1, d2);
488
25.5k
  }
489
27.3k
  for (i = 0; i < tab2->n_con; 
++i25.5k
) {
490
25.5k
    prod->con[tab1->n_con + i] = tab2->con[i];
491
25.5k
    update_index2(&prod->con[tab1->n_con + i],
492
25.5k
        tab1->n_row, tab1->n_col,
493
25.5k
        r1, r2, d1, d2);
494
25.5k
  }
495
1.85k
  prod->col_var = isl_alloc_array(tab1->mat->ctx, int,
496
1.85k
          tab1->n_col + tab2->n_col);
497
1.85k
  if ((tab1->n_col + tab2->n_col) && !prod->col_var)
498
0
    goto error;
499
12.7k
  
for (i = 0; 1.85k
i < tab1->n_col;
++i10.9k
) {
500
10.9k
    int pos = i < d1 ? 
i545
:
i + d210.4k
;
501
10.9k
    prod->col_var[pos] = tab1->col_var[i];
502
10.9k
  }
503
12.7k
  for (i = 0; i < tab2->n_col; 
++i10.9k
) {
504
10.9k
    int pos = i < d2 ? 
d1 + i545
:
tab1->n_col + i10.4k
;
505
10.9k
    int t = tab2->col_var[i];
506
10.9k
    if (t >= 0)
507
131
      t += tab1->n_var;
508
10.8k
    else
509
10.8k
      t -= tab1->n_con;
510
10.9k
    prod->col_var[pos] = t;
511
10.9k
  }
512
1.85k
  prod->row_var = isl_alloc_array(tab1->mat->ctx, int,
513
1.85k
          tab1->mat->n_row + tab2->mat->n_row);
514
1.85k
  if ((tab1->mat->n_row + tab2->mat->n_row) && !prod->row_var)
515
0
    goto error;
516
27.3k
  
for (i = 0; 1.85k
i < tab1->n_row;
++i25.5k
) {
517
25.5k
    int pos = i < r1 ? 
i4.40k
:
i + r221.1k
;
518
25.5k
    prod->row_var[pos] = tab1->row_var[i];
519
25.5k
  }
520
27.3k
  for (i = 0; i < tab2->n_row; 
++i25.5k
) {
521
25.5k
    int pos = i < r2 ? 
r1 + i4.40k
:
tab1->n_row + i21.1k
;
522
25.5k
    int t = tab2->row_var[i];
523
25.5k
    if (t >= 0)
524
10.9k
      t += tab1->n_var;
525
14.6k
    else
526
14.6k
      t -= tab1->n_con;
527
25.5k
    prod->row_var[pos] = t;
528
25.5k
  }
529
1.85k
  prod->samples = NULL;
530
1.85k
  prod->sample_index = NULL;
531
1.85k
  prod->n_row = tab1->n_row + tab2->n_row;
532
1.85k
  prod->n_con = tab1->n_con + tab2->n_con;
533
1.85k
  prod->n_eq = 0;
534
1.85k
  prod->max_con = tab1->max_con + tab2->max_con;
535
1.85k
  prod->n_col = tab1->n_col + tab2->n_col;
536
1.85k
  prod->n_var = tab1->n_var + tab2->n_var;
537
1.85k
  prod->max_var = tab1->max_var + tab2->max_var;
538
1.85k
  prod->n_param = 0;
539
1.85k
  prod->n_div = 0;
540
1.85k
  prod->n_dead = tab1->n_dead + tab2->n_dead;
541
1.85k
  prod->n_redundant = tab1->n_redundant + tab2->n_redundant;
542
1.85k
  prod->rational = tab1->rational;
543
1.85k
  prod->empty = tab1->empty || tab2->empty;
544
1.85k
  prod->strict_redundant = tab1->strict_redundant || tab2->strict_redundant;
545
1.85k
  prod->need_undo = 0;
546
1.85k
  prod->in_undo = 0;
547
1.85k
  prod->M = tab1->M;
548
1.85k
  prod->cone = tab1->cone;
549
1.85k
  prod->bottom.type = isl_tab_undo_bottom;
550
1.85k
  prod->bottom.next = NULL;
551
1.85k
  prod->top = &prod->bottom;
552
1.85k
553
1.85k
  prod->n_zero = 0;
554
1.85k
  prod->n_unbounded = 0;
555
1.85k
  prod->basis = NULL;
556
1.85k
557
1.85k
  return prod;
558
0
error:
559
0
  isl_tab_free(prod);
560
0
  return NULL;
561
1.85k
}
562
563
static struct isl_tab_var *var_from_index(struct isl_tab *tab, int i)
564
57.6M
{
565
57.6M
  if (i >= 0)
566
15.7M
    return &tab->var[i];
567
41.8M
  else
568
41.8M
    return &tab->con[~i];
569
57.6M
}
570
571
struct isl_tab_var *isl_tab_var_from_row(struct isl_tab *tab, int i)
572
44.0M
{
573
44.0M
  return var_from_index(tab, tab->row_var[i]);
574
44.0M
}
575
576
static struct isl_tab_var *var_from_col(struct isl_tab *tab, int i)
577
12.4M
{
578
12.4M
  return var_from_index(tab, tab->col_var[i]);
579
12.4M
}
580
581
/* Check if there are any upper bounds on column variable "var",
582
 * i.e., non-negative rows where var appears with a negative coefficient.
583
 * Return 1 if there are no such bounds.
584
 */
585
static int max_is_manifestly_unbounded(struct isl_tab *tab,
586
  struct isl_tab_var *var)
587
982k
{
588
982k
  int i;
589
982k
  unsigned off = 2 + tab->M;
590
982k
591
982k
  if (var->is_row)
592
692k
    return 0;
593
2.13M
  
for (i = tab->n_redundant; 290k
i < tab->n_row;
++i1.84M
) {
594
1.96M
    if (!isl_int_is_neg(tab->mat->row[i][off + var->index]))
595
1.96M
      
continue1.71M
;
596
248k
    if (isl_tab_var_from_row(tab, i)->is_nonneg)
597
116k
      return 0;
598
1.96M
  }
599
290k
  
return 1173k
;
600
982k
}
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
719k
{
609
719k
  int i;
610
719k
  unsigned off = 2 + tab->M;
611
719k
612
719k
  if (var->is_row)
613
298k
    return 0;
614
3.48M
  
for (i = tab->n_redundant; 420k
i < tab->n_row;
++i3.06M
) {
615
3.31M
    if (!isl_int_is_pos(tab->mat->row[i][off + var->index]))
616
3.31M
      
continue2.91M
;
617
396k
    if (isl_tab_var_from_row(tab, i)->is_nonneg)
618
255k
      return 0;
619
3.31M
  }
620
420k
  
return 1164k
;
621
719k
}
622
623
static int row_cmp(struct isl_tab *tab, int r1, int r2, int c, isl_int *t)
624
625k
{
625
625k
  unsigned off = 2 + tab->M;
626
625k
627
625k
  if (tab->M) {
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
625k
  }
635
625k
  isl_int_mul(*t, tab->mat->row[r1][1], tab->mat->row[r2][off + c]);
636
625k
  isl_int_submul(*t, tab->mat->row[r2][1], tab->mat->row[r1][off + c]);
637
625k
  return isl_int_sgn(*t);
638
625k
}
639
640
/* Given the index of a column "c", return the index of a row
641
 * that can be used to pivot the column in, with either an increase
642
 * (sgn > 0) or a decrease (sgn < 0) of the corresponding variable.
643
 * If "var" is not NULL, then the row returned will be different from
644
 * the one associated with "var".
645
 *
646
 * Each row in the tableau is of the form
647
 *
648
 *  x_r = a_r0 + \sum_i a_ri x_i
649
 *
650
 * Only rows with x_r >= 0 and with the sign of a_ri opposite to "sgn"
651
 * impose any limit on the increase or decrease in the value of x_c
652
 * and this bound is equal to a_r0 / |a_rc|.  We are therefore looking
653
 * for the row with the smallest (most stringent) such bound.
654
 * Note that the common denominator of each row drops out of the fraction.
655
 * To check if row j has a smaller bound than row r, i.e.,
656
 * a_j0 / |a_jc| < a_r0 / |a_rc| or a_j0 |a_rc| < a_r0 |a_jc|,
657
 * we check if -sign(a_jc) (a_j0 a_rc - a_r0 a_jc) < 0,
658
 * where -sign(a_jc) is equal to "sgn".
659
 */
660
static int pivot_row(struct isl_tab *tab,
661
  struct isl_tab_var *var, int sgn, int c)
662
1.52M
{
663
1.52M
  int j, r, tsgn;
664
1.52M
  isl_int t;
665
1.52M
  unsigned off = 2 + tab->M;
666
1.52M
667
1.52M
  isl_int_init(t);
668
1.52M
  r = -1;
669
22.7M
  for (j = tab->n_redundant; j < tab->n_row; 
++j21.2M
) {
670
21.2M
    if (var && 
j == var->index17.2M
)
671
1.28M
      continue;
672
19.9M
    if (!isl_tab_var_from_row(tab, j)->is_nonneg)
673
4.18M
      continue;
674
15.7M
    if (sgn * isl_int_sgn(tab->mat->row[j][off + c]) >= 0)
675
13.9M
      continue;
676
1.75M
    if (r < 0) {
677
1.13M
      r = j;
678
1.13M
      continue;
679
1.13M
    }
680
625k
    tsgn = sgn * row_cmp(tab, r, j, c, &t);
681
625k
    if (tsgn < 0 || 
(403k
tsgn == 0403k
&&
682
403k
              
tab->row_var[j] < tab->row_var[r]216k
))
683
421k
      r = j;
684
21.2M
  }
685
1.52M
  isl_int_clear(t);
686
1.52M
  return r;
687
1.52M
}
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
1.92M
{
709
1.92M
  int j, r, c;
710
1.92M
  isl_int *tr;
711
1.92M
712
1.92M
  *row = *col = -1;
713
1.92M
714
1.92M
  isl_assert(tab->mat->ctx, var->is_row, return);
715
1.92M
  tr = tab->mat->row[var->index] + 2 + tab->M;
716
1.92M
717
1.92M
  c = -1;
718
18.4M
  for (j = tab->n_dead; j < tab->n_col; 
++j16.5M
) {
719
16.5M
    if (isl_int_is_zero(tr[j]))
720
16.5M
      
continue13.5M
;
721
3.00M
    if (isl_int_sgn(tr[j]) != sgn &&
722
3.00M
        
var_from_col(tab, j)->is_nonneg1.69M
)
723
1.14M
      continue;
724
1.85M
    if (c < 0 || 
tab->col_var[j] < tab->col_var[c]493k
)
725
1.47M
      c = j;
726
16.5M
  }
727
1.92M
  if (c < 0)
728
557k
    return;
729
1.36M
730
1.36M
  sgn *= isl_int_sgn(tr[c]);
731
1.36M
  r = pivot_row(tab, skip_var, sgn, c);
732
1.36M
  *row = r < 0 ? 
var->index397k
:
r966k
;
733
1.92M
  *col = c;
734
1.92M
}
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
8.21M
{
744
8.21M
  int i;
745
8.21M
  unsigned off = 2 + tab->M;
746
8.21M
747
8.21M
  if (tab->row_var[row] < 0 && 
!isl_tab_var_from_row(tab, row)->is_nonneg6.36M
)
748
329k
    return 0;
749
7.88M
750
7.88M
  if (isl_int_is_neg(tab->mat->row[row][1]))
751
7.88M
    
return 0667k
;
752
7.21M
  if (tab->strict_redundant && 
isl_int_is_zero0
(tab->mat->row[row][1]))
753
7.21M
    
return 00
;
754
7.21M
  if (tab->M && 
isl_int_is_neg12.3k
(tab->mat->row[row][2]))
755
7.21M
    
return 0862
;
756
7.21M
757
34.2M
  
for (i = tab->n_dead; 7.21M
i < tab->n_col;
++i27.0M
) {
758
33.4M
    if (isl_int_is_zero(tab->mat->row[row][off + i]))
759
33.4M
      
continue24.9M
;
760
8.46M
    if (tab->col_var[i] >= 0)
761
3.53M
      return 0;
762
4.93M
    if (isl_int_is_neg(tab->mat->row[row][off + i]))
763
4.93M
      
return 02.84M
;
764
2.08M
    if (!var_from_col(tab, i)->is_nonneg)
765
39.7k
      return 0;
766
33.4M
  }
767
7.21M
  
return 1789k
;
768
8.21M
}
769
770
static void swap_rows(struct isl_tab *tab, int row1, int row2)
771
833k
{
772
833k
  int t;
773
833k
  enum isl_tab_row_sign s;
774
833k
775
833k
  t = tab->row_var[row1];
776
833k
  tab->row_var[row1] = tab->row_var[row2];
777
833k
  tab->row_var[row2] = t;
778
833k
  isl_tab_var_from_row(tab, row1)->index = row1;
779
833k
  isl_tab_var_from_row(tab, row2)->index = row2;
780
833k
  tab->mat = isl_mat_swap_rows(tab->mat, row1, row2);
781
833k
782
833k
  if (!tab->row_sign)
783
831k
    return;
784
2.57k
  s = tab->row_sign[row1];
785
2.57k
  tab->row_sign[row1] = tab->row_sign[row2];
786
2.57k
  tab->row_sign[row2] = s;
787
2.57k
}
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
5.72M
{
794
5.72M
  struct isl_tab_undo *undo;
795
5.72M
796
5.72M
  if (!tab)
797
0
    return -1;
798
5.72M
  if (!tab->need_undo)
799
4.24M
    return 0;
800
1.48M
801
1.48M
  undo = isl_alloc_type(tab->mat->ctx, struct isl_tab_undo);
802
1.48M
  if (!undo)
803
0
    return -1;
804
1.48M
  undo->type = type;
805
1.48M
  undo->u = u;
806
1.48M
  undo->next = tab->top;
807
1.48M
  tab->top = undo;
808
1.48M
809
1.48M
  return 0;
810
1.48M
}
811
812
int isl_tab_push_var(struct isl_tab *tab,
813
  enum isl_tab_undo_type type, struct isl_tab_var *var)
814
5.55M
{
815
5.55M
  union isl_tab_undo_val u;
816
5.55M
  if (var->is_row)
817
5.42M
    u.var_index = tab->row_var[var->index];
818
129k
  else
819
129k
    u.var_index = tab->col_var[var->index];
820
5.55M
  return push_union(tab, type, u);
821
5.55M
}
822
823
int isl_tab_push(struct isl_tab *tab, enum isl_tab_undo_type type)
824
163k
{
825
163k
  union isl_tab_undo_val u = { 0 };
826
163k
  return push_union(tab, type, u);
827
163k
}
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
65
{
834
65
  int i;
835
65
  union isl_tab_undo_val u;
836
65
837
65
  u.col_var = isl_alloc_array(tab->mat->ctx, int, tab->n_col);
838
65
  if (tab->n_col && !u.col_var)
839
0
    return -1;
840
600
  
for (i = 0; 65
i < tab->n_col;
++i535
)
841
535
    u.col_var[i] = tab->col_var[i];
842
65
  return push_union(tab, isl_tab_undo_saved_basis, u);
843
65
}
844
845
int isl_tab_push_callback(struct isl_tab *tab, struct isl_tab_callback *callback)
846
6.25k
{
847
6.25k
  union isl_tab_undo_val u;
848
6.25k
  u.callback = callback;
849
6.25k
  return push_union(tab, isl_tab_undo_callback, u);
850
6.25k
}
851
852
struct isl_tab *isl_tab_init_samples(struct isl_tab *tab)
853
2.71k
{
854
2.71k
  if (!tab)
855
0
    return NULL;
856
2.71k
857
2.71k
  tab->n_sample = 0;
858
2.71k
  tab->n_outside = 0;
859
2.71k
  tab->samples = isl_mat_alloc(tab->mat->ctx, 1, 1 + tab->n_var);
860
2.71k
  if (!tab->samples)
861
0
    goto error;
862
2.71k
  tab->sample_index = isl_alloc_array(tab->mat->ctx, int, 1);
863
2.71k
  if (!tab->sample_index)
864
0
    goto error;
865
2.71k
  return tab;
866
0
error:
867
0
  isl_tab_free(tab);
868
0
  return NULL;
869
2.71k
}
870
871
int isl_tab_add_sample(struct isl_tab *tab, __isl_take isl_vec *sample)
872
4.21k
{
873
4.21k
  if (!tab || !sample)
874
0
    goto error;
875
4.21k
876
4.21k
  if (tab->n_sample + 1 > tab->samples->n_row) {
877
1.45k
    int *t = isl_realloc_array(tab->mat->ctx,
878
1.45k
          tab->sample_index, int, tab->n_sample + 1);
879
1.45k
    if (!t)
880
0
      goto error;
881
1.45k
    tab->sample_index = t;
882
1.45k
  }
883
4.21k
884
4.21k
  tab->samples = isl_mat_extend(tab->samples,
885
4.21k
        tab->n_sample + 1, tab->samples->n_col);
886
4.21k
  if (!tab->samples)
887
0
    goto error;
888
4.21k
889
4.21k
  isl_seq_cpy(tab->samples->row[tab->n_sample], sample->el, sample->size);
890
4.21k
  isl_vec_free(sample);
891
4.21k
  tab->sample_index[tab->n_sample] = tab->n_sample;
892
4.21k
  tab->n_sample++;
893
4.21k
894
4.21k
  return 0;
895
0
error:
896
0
  isl_vec_free(sample);
897
0
  return -1;
898
4.21k
}
899
900
struct isl_tab *isl_tab_drop_sample(struct isl_tab *tab, int s)
901
2.25k
{
902
2.25k
  if (s != tab->n_outside) {
903
1.53k
    int t = tab->sample_index[tab->n_outside];
904
1.53k
    tab->sample_index[tab->n_outside] = tab->sample_index[s];
905
1.53k
    tab->sample_index[s] = t;
906
1.53k
    isl_mat_swap_rows(tab->samples, tab->n_outside, s);
907
1.53k
  }
908
2.25k
  tab->n_outside++;
909
2.25k
  if (isl_tab_push(tab, isl_tab_undo_drop_sample) < 0) {
910
0
    isl_tab_free(tab);
911
0
    return NULL;
912
0
  }
913
2.25k
914
2.25k
  return tab;
915
2.25k
}
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
8.74k
{
922
8.74k
  union isl_tab_undo_val u;
923
8.74k
924
8.74k
  if (!tab)
925
0
    return -1;
926
8.74k
927
8.74k
  u.n = tab->n_sample;
928
8.74k
  return push_union(tab, isl_tab_undo_saved_samples, u);
929
8.74k
}
930
931
/* Mark row with index "row" as being redundant.
932
 * If we may need to undo the operation or if the row represents
933
 * a variable of the original problem, the row is kept,
934
 * but no longer considered when looking for a pivot row.
935
 * Otherwise, the row is simply removed.
936
 *
937
 * The row may be interchanged with some other row.  If it
938
 * is interchanged with a later row, return 1.  Otherwise return 0.
939
 * If the rows are checked in order in the calling function,
940
 * then a return value of 1 means that the row with the given
941
 * row number may now contain a different row that hasn't been checked yet.
942
 */
943
int isl_tab_mark_redundant(struct isl_tab *tab, int row)
944
1.08M
{
945
1.08M
  struct isl_tab_var *var = isl_tab_var_from_row(tab, row);
946
1.08M
  var->is_redundant = 1;
947
1.08M
  isl_assert(tab->mat->ctx, row >= tab->n_redundant, return -1);
948
1.08M
  if (tab->preserve || 
tab->need_undo735k
||
tab->row_var[row] >= 0668k
) {
949
719k
    if (tab->row_var[row] >= 0 && 
!var->is_nonneg513k
) {
950
511k
      var->is_nonneg = 1;
951
511k
      if (isl_tab_push_var(tab, isl_tab_undo_nonneg, var) < 0)
952
0
        return -1;
953
719k
    }
954
719k
    if (row != tab->n_redundant)
955
521k
      swap_rows(tab, row, tab->n_redundant);
956
719k
    tab->n_redundant++;
957
719k
    return isl_tab_push_var(tab, isl_tab_undo_redundant, var);
958
719k
  } else {
959
362k
    if (row != tab->n_row - 1)
960
260k
      swap_rows(tab, row, tab->n_row - 1);
961
362k
    isl_tab_var_from_row(tab, tab->n_row - 1)->index = -1;
962
362k
    tab->n_row--;
963
362k
    return 1;
964
362k
  }
965
1.08M
}
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
3.29k
{
974
3.29k
  if (!tab)
975
0
    return -1;
976
3.29k
  if (!tab->rational && tab->need_undo)
977
3.29k
    if (isl_tab_push(tab, isl_tab_undo_rational) < 0)
978
0
      return -1;
979
3.29k
  tab->rational = 1;
980
3.29k
  return 0;
981
3.29k
}
982
983
isl_stat isl_tab_mark_empty(struct isl_tab *tab)
984
26.9k
{
985
26.9k
  if (!tab)
986
0
    return isl_stat_error;
987
26.9k
  if (!tab->empty && 
tab->need_undo26.6k
)
988
22.2k
    if (isl_tab_push(tab, isl_tab_undo_empty) < 0)
989
0
      return isl_stat_error;
990
26.9k
  tab->empty = 1;
991
26.9k
  return isl_stat_ok;
992
26.9k
}
993
994
int isl_tab_freeze_constraint(struct isl_tab *tab, int con)
995
156k
{
996
156k
  struct isl_tab_var *var;
997
156k
998
156k
  if (!tab)
999
0
    return -1;
1000
156k
1001
156k
  var = &tab->con[con];
1002
156k
  if (var->frozen)
1003
0
    return 0;
1004
156k
  if (var->index < 0)
1005
10.5k
    return 0;
1006
145k
  var->frozen = 1;
1007
145k
1008
145k
  if (tab->need_undo)
1009
131k
    return isl_tab_push_var(tab, isl_tab_undo_freeze, var);
1010
14.0k
1011
14.0k
  return 0;
1012
14.0k
}
1013
1014
/* Update the rows signs after a pivot of "row" and "col", with "row_sgn"
1015
 * the original sign of the pivot element.
1016
 * We only keep track of row signs during PILP solving and in this case
1017
 * we only pivot a row with negative sign (meaning the value is always
1018
 * non-positive) using a positive pivot element.
1019
 *
1020
 * For each row j, the new value of the parametric constant is equal to
1021
 *
1022
 *  a_j0 - a_jc a_r0/a_rc
1023
 *
1024
 * where a_j0 is the original parametric constant, a_rc is the pivot element,
1025
 * a_r0 is the parametric constant of the pivot row and a_jc is the
1026
 * pivot column entry of the row j.
1027
 * Since a_r0 is non-positive and a_rc is positive, the sign of row j
1028
 * remains the same if a_jc has the same sign as the row j or if
1029
 * a_jc is zero.  In all other cases, we reset the sign to "unknown".
1030
 */
1031
static void update_row_sign(struct isl_tab *tab, int row, int col, int row_sgn)
1032
1.49M
{
1033
1.49M
  int i;
1034
1.49M
  struct isl_mat *mat = tab->mat;
1035
1.49M
  unsigned off = 2 + tab->M;
1036
1.49M
1037
1.49M
  if (!tab->row_sign)
1038
1.48M
    return;
1039
8.86k
1040
8.86k
  if (tab->row_sign[row] == 0)
1041
6.78k
    return;
1042
2.07k
  isl_assert(mat->ctx, row_sgn > 0, return);
1043
2.07k
  isl_assert(mat->ctx, tab->row_sign[row] == isl_tab_row_neg, return);
1044
2.07k
  tab->row_sign[row] = isl_tab_row_pos;
1045
15.7k
  for (i = 0; i < tab->n_row; 
++i13.6k
) {
1046
13.6k
    int s;
1047
13.6k
    if (i == row)
1048
2.07k
      continue;
1049
11.5k
    s = isl_int_sgn(mat->row[i][off + col]);
1050
11.5k
    if (!s)
1051
7.78k
      continue;
1052
3.79k
    if (!tab->row_sign[i])
1053
1.68k
      continue;
1054
2.11k
    if (s < 0 && 
tab->row_sign[i] == isl_tab_row_neg1.07k
)
1055
0
      continue;
1056
2.11k
    if (s > 0 && 
tab->row_sign[i] == isl_tab_row_pos1.04k
)
1057
1.04k
      continue;
1058
1.07k
    tab->row_sign[i] = isl_tab_row_unknown;
1059
1.07k
  }
1060
1.49M
}
1061
1062
/* Given a row number "row" and a column number "col", pivot the tableau
1063
 * such that the associated variables are interchanged.
1064
 * The given row in the tableau expresses
1065
 *
1066
 *  x_r = a_r0 + \sum_i a_ri x_i
1067
 *
1068
 * or
1069
 *
1070
 *  x_c = 1/a_rc x_r - a_r0/a_rc + sum_{i \ne r} -a_ri/a_rc
1071
 *
1072
 * Substituting this equality into the other rows
1073
 *
1074
 *  x_j = a_j0 + \sum_i a_ji x_i
1075
 *
1076
 * with a_jc \ne 0, we obtain
1077
 *
1078
 *  x_j = a_jc/a_rc x_r + a_j0 - a_jc a_r0/a_rc + sum a_ji - a_jc a_ri/a_rc 
1079
 *
1080
 * The tableau
1081
 *
1082
 *  n_rc/d_r    n_ri/d_r
1083
 *  n_jc/d_j    n_ji/d_j
1084
 *
1085
 * where i is any other column and j is any other row,
1086
 * is therefore transformed into
1087
 *
1088
 * s(n_rc)d_r/|n_rc|    -s(n_rc)n_ri/|n_rc|
1089
 * s(n_rc)d_r n_jc/(|n_rc| d_j) (n_ji |n_rc| - s(n_rc)n_jc n_ri)/(|n_rc| d_j)
1090
 *
1091
 * The transformation is performed along the following steps
1092
 *
1093
 *  d_r/n_rc    n_ri/n_rc
1094
 *  n_jc/d_j    n_ji/d_j
1095
 *
1096
 *  s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1097
 *  n_jc/d_j    n_ji/d_j
1098
 *
1099
 *  s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1100
 *  n_jc/(|n_rc| d_j) n_ji/(|n_rc| d_j)
1101
 *
1102
 *  s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1103
 *  n_jc/(|n_rc| d_j) (n_ji |n_rc|)/(|n_rc| d_j)
1104
 *
1105
 *  s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1106
 *  n_jc/(|n_rc| d_j) (n_ji |n_rc| - s(n_rc)n_jc n_ri)/(|n_rc| d_j)
1107
 *
1108
 * s(n_rc)d_r/|n_rc|    -s(n_rc)n_ri/|n_rc|
1109
 * s(n_rc)d_r n_jc/(|n_rc| d_j) (n_ji |n_rc| - s(n_rc)n_jc n_ri)/(|n_rc| d_j)
1110
 *
1111
 */
1112
int isl_tab_pivot(struct isl_tab *tab, int row, int col)
1113
1.49M
{
1114
1.49M
  int i, j;
1115
1.49M
  int sgn;
1116
1.49M
  int t;
1117
1.49M
  isl_ctx *ctx;
1118
1.49M
  struct isl_mat *mat = tab->mat;
1119
1.49M
  struct isl_tab_var *var;
1120
1.49M
  unsigned off = 2 + tab->M;
1121
1.49M
1122
1.49M
  ctx = isl_tab_get_ctx(tab);
1123
1.49M
  if (isl_ctx_next_operation(ctx) < 0)
1124
0
    return -1;
1125
1.49M
1126
1.49M
  isl_int_swap(mat->row[row][0], mat->row[row][off + col]);
1127
1.49M
  sgn = isl_int_sgn(mat->row[row][0]);
1128
1.49M
  if (sgn < 0) {
1129
939k
    isl_int_neg(mat->row[row][0], mat->row[row][0]);
1130
939k
    isl_int_neg(mat->row[row][off + col], mat->row[row][off + col]);
1131
939k
  } else
1132
5.45M
    
for (j = 0; 557k
j < off - 1 + tab->n_col;
++j4.90M
) {
1133
4.90M
      if (j == off - 1 + col)
1134
557k
        continue;
1135
4.34M
      isl_int_neg(mat->row[row][1 + j], mat->row[row][1 + j]);
1136
4.34M
    }
1137
1.49M
  if (!isl_int_is_one(mat->row[row][0]))
1138
1.49M
    
isl_seq_normalize(mat->ctx, mat->row[row], off + tab->n_col)223k
;
1139
22.0M
  for (i = 0; i < tab->n_row; 
++i20.5M
) {
1140
20.5M
    if (i == row)
1141
1.49M
      continue;
1142
19.0M
    if (isl_int_is_zero(mat->row[i][off + col]))
1143
19.0M
      
continue15.1M
;
1144
3.95M
    isl_int_mul(mat->row[i][0], mat->row[i][0], mat->row[row][0]);
1145
39.6M
    for (j = 0; j < off - 1 + tab->n_col; 
++j35.6M
) {
1146
35.6M
      if (j == off - 1 + col)
1147
3.95M
        continue;
1148
31.7M
      isl_int_mul(mat->row[i][1 + j],
1149
31.7M
            mat->row[i][1 + j], mat->row[row][0]);
1150
31.7M
      isl_int_addmul(mat->row[i][1 + j],
1151
35.6M
            mat->row[i][off + col], mat->row[row][1 + j]);
1152
35.6M
    }
1153
3.95M
    isl_int_mul(mat->row[i][off + col],
1154
3.95M
          mat->row[i][off + col], mat->row[row][off + col]);
1155
3.95M
    if (!isl_int_is_one(mat->row[i][0]))
1156
3.95M
      
isl_seq_normalize(mat->ctx, mat->row[i], off + tab->n_col)1.48M
;
1157
20.5M
  }
1158
1.49M
  t = tab->row_var[row];
1159
1.49M
  tab->row_var[row] = tab->col_var[col];
1160
1.49M
  tab->col_var[col] = t;
1161
1.49M
  var = isl_tab_var_from_row(tab, row);
1162
1.49M
  var->is_row = 1;
1163
1.49M
  var->index = row;
1164
1.49M
  var = var_from_col(tab, col);
1165
1.49M
  var->is_row = 0;
1166
1.49M
  var->index = col;
1167
1.49M
  update_row_sign(tab, row, col, sgn);
1168
1.49M
  if (tab->in_undo)
1169
71.8k
    return 0;
1170
16.7M
  
for (i = tab->n_redundant; 1.42M
i < tab->n_row;
++i15.3M
) {
1171
15.3M
    if (isl_int_is_zero(mat->row[i][off + col]))
1172
15.3M
      
continue10.5M
;
1173
4.73M
    if (!isl_tab_var_from_row(tab, i)->frozen &&
1174
4.73M
        
isl_tab_row_is_redundant(tab, i)4.63M
) {
1175
754k
      int redo = isl_tab_mark_redundant(tab, i);
1176
754k
      if (redo < 0)
1177
0
        return -1;
1178
754k
      if (redo)
1179
71.2k
        --i;
1180
754k
    }
1181
15.3M
  }
1182
1.42M
  return 0;
1183
1.49M
}
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
821k
{
1194
821k
  int r;
1195
821k
  unsigned off = 2 + tab->M;
1196
821k
1197
821k
  if (var->is_row)
1198
692k
    return 0;
1199
128k
1200
128k
  if (sign == 0) {
1201
20.3k
    for (r = tab->n_redundant; r < tab->n_row; 
++r13.8k
)
1202
20.3k
      if (!isl_int_is_zero(tab->mat->row[r][off+var->index]))
1203
20.3k
        
break6.53k
;
1204
6.53k
    isl_assert(tab->mat->ctx, r < tab->n_row, return -1);
1205
121k
  } else {
1206
121k
    r = pivot_row(tab, NULL, sign, var->index);
1207
121k
    isl_assert(tab->mat->ctx, r >= 0, return -1);
1208
121k
  }
1209
128k
1210
128k
  return isl_tab_pivot(tab, r, var->index);
1211
821k
}
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
594k
{
1253
594k
  int row, col;
1254
594k
1255
594k
  if (max_is_manifestly_unbounded(tab, var))
1256
41.8k
    return 1;
1257
552k
  if (to_row(tab, var, 1) < 0)
1258
0
    return -2;
1259
969k
  
while (552k
!isl_int_is_pos(tab->mat->row[var->index][1])) {
1260
773k
    find_pivot(tab, var, var, 1, &row, &col);
1261
773k
    if (row == -1)
1262
278k
      return isl_int_sgn(tab->mat->row[var->index][1]);
1263
494k
    if (isl_tab_pivot(tab, row, col) < 0)
1264
0
      return -2;
1265
494k
    if (!var->is_row) /* manifestly unbounded */
1266
77.8k
      return 1;
1267
773k
  }
1268
552k
  
return 1196k
;
1269
594k
}
1270
1271
int isl_tab_sign_of_max(struct isl_tab *tab, int con)
1272
0
{
1273
0
  struct isl_tab_var *var;
1274
0
1275
0
  if (!tab)
1276
0
    return -2;
1277
0
1278
0
  var = &tab->con[con];
1279
0
  isl_assert(tab->mat->ctx, !var->is_redundant, return -2);
1280
0
  isl_assert(tab->mat->ctx, !var->is_zero, return -2);
1281
0
1282
0
  return sign_of_max(tab, var);
1283
0
}
1284
1285
static int row_is_neg(struct isl_tab *tab, int row)
1286
2.41M
{
1287
2.41M
  if (!tab->M)
1288
2.41M
    return isl_int_is_neg(tab->mat->row[row][1]);
1289
0
  if (isl_int_is_pos(tab->mat->row[row][2]))
1290
0
    return 0;
1291
0
  if (isl_int_is_neg(tab->mat->row[row][2]))
1292
0
    return 1;
1293
0
  return isl_int_is_neg(tab->mat->row[row][1]);
1294
2.41M
}
1295
1296
static int row_sgn(struct isl_tab *tab, int row)
1297
2.16M
{
1298
2.16M
  if (!tab->M)
1299
2.16M
    return isl_int_sgn(tab->mat->row[row][1]);
1300
0
  if (!isl_int_is_zero(tab->mat->row[row][2]))
1301
0
    return isl_int_sgn(tab->mat->row[row][2]);
1302
0
  else
1303
0
    return isl_int_sgn(tab->mat->row[row][1]);
1304
2.16M
}
1305
1306
/* Perform pivots until the row variable "var" has a non-negative
1307
 * sample value or until no more upward pivots can be performed.
1308
 * Return the sign of the sample value after the pivots have been
1309
 * performed.
1310
 */
1311
static int restore_row(struct isl_tab *tab, struct isl_tab_var *var)
1312
2.30M
{
1313
2.30M
  int row, col;
1314
2.30M
1315
2.41M
  while (row_is_neg(tab, var->index)) {
1316
283k
    find_pivot(tab, var, var, 1, &row, &col);
1317
283k
    if (row == -1)
1318
25.9k
      break;
1319
257k
    if (isl_tab_pivot(tab, row, col) < 0)
1320
0
      return -2;
1321
257k
    if (!var->is_row) /* manifestly unbounded */
1322
144k
      return 1;
1323
283k
  }
1324
2.30M
  
return row_sgn(tab, var->index)2.16M
;
1325
2.30M
}
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
31.4k
{
1334
31.4k
  int row, col;
1335
31.4k
1336
33.4k
  while (isl_int_is_neg(tab->mat->row[var->index][1])) {
1337
32.9k
    find_pivot(tab, var, var, 1, &row, &col);
1338
32.9k
    if (row == -1)
1339
14.9k
      break;
1340
17.9k
    if (row == var->index) /* manifestly unbounded */
1341
16.0k
      return 1;
1342
1.91k
    if (isl_tab_pivot(tab, row, col) < 0)
1343
0
      return -1;
1344
32.9k
  }
1345
31.4k
  
return !15.4k
isl_int_is_neg15.4k
(tab->mat->row[var->index][1]);
1346
31.4k
}
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
378
{
1367
378
  int row, col;
1368
378
  struct isl_tab_var *pivot_var = NULL;
1369
378
1370
378
  if (min_is_manifestly_unbounded(tab, var))
1371
0
    return -1;
1372
378
  if (!var->is_row) {
1373
0
    col = var->index;
1374
0
    row = pivot_row(tab, NULL, -1, col);
1375
0
    pivot_var = var_from_col(tab, col);
1376
0
    if (isl_tab_pivot(tab, row, col) < 0)
1377
0
      return -2;
1378
0
    if (var->is_redundant)
1379
0
      return 0;
1380
0
    if (isl_int_is_neg(tab->mat->row[var->index][1])) {
1381
0
      if (var->is_nonneg) {
1382
0
        if (!pivot_var->is_redundant &&
1383
0
            pivot_var->index == row) {
1384
0
          if (isl_tab_pivot(tab, row, col) < 0)
1385
0
            return -2;
1386
0
        } else
1387
0
          if (restore_row(tab, var) < -1)
1388
0
            return -2;
1389
0
      }
1390
0
      return -1;
1391
0
    }
1392
0
  }
1393
378
  if (var->is_redundant)
1394
0
    return 0;
1395
380
  
while (378
!isl_int_is_neg(tab->mat->row[var->index][1])) {
1396
378
    find_pivot(tab, var, var, -1, &row, &col);
1397
378
    if (row == var->index)
1398
374
      return -1;
1399
4
    if (row == -1)
1400
2
      return isl_int_sgn(tab->mat->row[var->index][1]);
1401
2
    pivot_var = var_from_col(tab, col);
1402
2
    if (isl_tab_pivot(tab, row, col) < 0)
1403
0
      return -2;
1404
2
    if (var->is_redundant)
1405
0
      return 0;
1406
378
  }
1407
378
  
if (2
pivot_var2
&&
var->is_nonneg2
) {
1408
0
    /* pivot back to non-negative value */
1409
0
    if (!pivot_var->is_redundant && pivot_var->index == row) {
1410
0
      if (isl_tab_pivot(tab, row, col) < 0)
1411
0
        return -2;
1412
0
    } else
1413
0
      if (restore_row(tab, var) < -1)
1414
0
        return -2;
1415
2
  }
1416
2
  return -1;
1417
2
}
1418
1419
static int row_at_most_neg_one(struct isl_tab *tab, int row)
1420
131k
{
1421
131k
  if (tab->M) {
1422
0
    if (isl_int_is_pos(tab->mat->row[row][2]))
1423
0
      return 0;
1424
0
    if (isl_int_is_neg(tab->mat->row[row][2]))
1425
0
      return 1;
1426
131k
  }
1427
131k
  return isl_int_is_neg(tab->mat->row[row][1]) &&
1428
131k
         
isl_int_abs_ge83.9k
(tab->mat->row[row][1],
1429
131k
            tab->mat->row[row][0]);
1430
131k
}
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
342k
{
1443
342k
  int row, col;
1444
342k
  struct isl_tab_var *pivot_var;
1445
342k
1446
342k
  if (min_is_manifestly_unbounded(tab, var))
1447
185
    return 1;
1448
341k
  if (!var->is_row) {
1449
43.8k
    col = var->index;
1450
43.8k
    row = pivot_row(tab, NULL, -1, col);
1451
43.8k
    pivot_var = var_from_col(tab, col);
1452
43.8k
    if (isl_tab_pivot(tab, row, col) < 0)
1453
0
      return -1;
1454
43.8k
    if (var->is_redundant)
1455
4.43k
      return 0;
1456
39.4k
    if (row_at_most_neg_one(tab, var->index)) {
1457
32.2k
      if (var->is_nonneg) {
1458
32.2k
        if (!pivot_var->is_redundant &&
1459
32.2k
            pivot_var->index == row) {
1460
30.7k
          if (isl_tab_pivot(tab, row, col) < 0)
1461
0
            return -1;
1462
1.44k
        } else
1463
1.44k
          if (restore_row(tab, var) < -1)
1464
0
            return -1;
1465
32.2k
      }
1466
32.2k
      return 1;
1467
32.2k
    }
1468
43.8k
  }
1469
305k
  if (var->is_redundant)
1470
3.28k
    return 0;
1471
357k
  
do 301k
{
1472
357k
    find_pivot(tab, var, var, -1, &row, &col);
1473
357k
    if (row == var->index) {
1474
153k
      if (var->is_nonneg && 
restore_row(tab, var) < -1137k
)
1475
0
        return -1;
1476
153k
      return 1;
1477
153k
    }
1478
204k
    if (row == -1)
1479
67.0k
      return 0;
1480
137k
    pivot_var = var_from_col(tab, col);
1481
137k
    if (isl_tab_pivot(tab, row, col) < 0)
1482
0
      return -1;
1483
137k
    if (var->is_redundant)
1484
44.9k
      return 0;
1485
92.2k
  } while (!row_at_most_neg_one(tab, var->index));
1486
301k
  
if (36.5k
var->is_nonneg36.5k
) {
1487
34.1k
    /* pivot back to non-negative value */
1488
34.1k
    if (!pivot_var->is_redundant && pivot_var->index == row)
1489
30.8k
      if (isl_tab_pivot(tab, row, col) < 0)
1490
0
        return -1;
1491
34.1k
    if (restore_row(tab, var) < -1)
1492
0
      return -1;
1493
36.5k
  }
1494
36.5k
  return 1;
1495
36.5k
}
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
315k
{
1502
315k
  int row, col;
1503
315k
  isl_int *r;
1504
315k
1505
315k
  if (max_is_manifestly_unbounded(tab, var))
1506
119k
    return 1;
1507
195k
  if (to_row(tab, var, 1) < 0)
1508
0
    return -1;
1509
195k
  r = tab->mat->row[var->index];
1510
207k
  while (isl_int_lt(r[1], r[0])) {
1511
12.3k
    find_pivot(tab, var, var, 1, &row, &col);
1512
12.3k
    if (row == -1)
1513
234
      return isl_int_ge(r[1], r[0]);
1514
12.1k
    if (row == var->index) /* manifestly unbounded */
1515
5
      return 1;
1516
12.1k
    if (isl_tab_pivot(tab, row, col) < 0)
1517
0
      return -1;
1518
12.3k
  }
1519
195k
  
return 1195k
;
1520
315k
}
1521
1522
static void swap_cols(struct isl_tab *tab, int col1, int col2)
1523
277k
{
1524
277k
  int t;
1525
277k
  unsigned off = 2 + tab->M;
1526
277k
  t = tab->col_var[col1];
1527
277k
  tab->col_var[col1] = tab->col_var[col2];
1528
277k
  tab->col_var[col2] = t;
1529
277k
  var_from_col(tab, col1)->index = col1;
1530
277k
  var_from_col(tab, col2)->index = col2;
1531
277k
  tab->mat = isl_mat_swap_cols(tab->mat, off + col1, off + col2);
1532
277k
}
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
383k
{
1548
383k
  var_from_col(tab, col)->is_zero = 1;
1549
383k
  if (tab->need_undo) {
1550
72.0k
    if (isl_tab_push_var(tab, isl_tab_undo_zero,
1551
72.0k
              var_from_col(tab, col)) < 0)
1552
0
      return -1;
1553
72.0k
    if (col != tab->n_dead)
1554
30.8k
      swap_cols(tab, col, tab->n_dead);
1555
72.0k
    tab->n_dead++;
1556
72.0k
    return 0;
1557
311k
  } else {
1558
311k
    if (col != tab->n_col - 1)
1559
246k
      swap_cols(tab, col, tab->n_col - 1);
1560
311k
    var_from_col(tab, tab->n_col - 1)->index = -1;
1561
311k
    tab->n_col--;
1562
311k
    return 1;
1563
311k
  }
1564
383k
}
1565
1566
static int row_is_manifestly_non_integral(struct isl_tab *tab, int row)
1567
1.77M
{
1568
1.77M
  unsigned off = 2 + tab->M;
1569
1.77M
1570
1.77M
  if (tab->M && 
!0
isl_int_eq0
(tab->mat->row[row][2],
1571
1.77M
          tab->mat->row[row][0]))
1572
1.77M
    
return 00
;
1573
1.77M
  if (isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
1574
1.77M
            tab->n_col - tab->n_dead) != -1)
1575
132k
    return 0;
1576
1.63M
1577
1.63M
  return !isl_int_is_divisible_by(tab->mat->row[row][1],
1578
1.77M
          tab->mat->row[row][0]);
1579
1.77M
}
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
278k
{
1586
278k
  int i;
1587
278k
1588
278k
  if (tab->empty)
1589
0
    return 1;
1590
278k
  if (tab->rational)
1591
3.29k
    return 0;
1592
275k
1593
3.89M
  
for (i = 0; 275k
i < tab->n_var;
++i3.61M
) {
1594
3.61M
    if (!tab->var[i].is_row)
1595
1.84M
      continue;
1596
1.77M
    if (row_is_manifestly_non_integral(tab, tab->var[i].index))
1597
20
      return 1;
1598
3.61M
  }
1599
275k
1600
275k
  
return 0275k
;
1601
278k
}
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
278k
{
1625
278k
  int j;
1626
278k
  struct isl_mat *mat = tab->mat;
1627
278k
  unsigned off = 2 + tab->M;
1628
278k
1629
278k
  if (!var->is_nonneg)
1630
278k
    
isl_die0
(isl_tab_get_ctx(tab), isl_error_internal,
1631
278k
      "expecting non-negative variable",
1632
278k
      return isl_stat_error);
1633
278k
  var->is_zero = 1;
1634
278k
  if (!temp_var && 
tab->need_undo274k
)
1635
194
    if (isl_tab_push_var(tab, isl_tab_undo_zero, var) < 0)
1636
0
      return isl_stat_error;
1637
2.45M
  
for (j = tab->n_dead; 278k
j < tab->n_col;
++j2.18M
) {
1638
2.18M
    int recheck;
1639
2.18M
    if (isl_int_is_zero(mat->row[var->index][off + j]))
1640
2.18M
      
continue1.93M
;
1641
246k
    if (isl_int_is_pos(mat->row[var->index][off + j]))
1642
246k
      
isl_die0
(isl_tab_get_ctx(tab), isl_error_internal,
1643
246k
        "row cannot have positive coefficients",
1644
246k
        return isl_stat_error);
1645
246k
    recheck = isl_tab_kill_col(tab, j);
1646
246k
    if (recheck < 0)
1647
0
      return isl_stat_error;
1648
246k
    if (recheck)
1649
242k
      --j;
1650
2.18M
  }
1651
278k
  if (!temp_var && 
isl_tab_mark_redundant(tab, var->index) < 0274k
)
1652
0
    return isl_stat_error;
1653
278k
  if (tab_is_manifestly_empty(tab) && 
isl_tab_mark_empty(tab) < 020
)
1654
0
    return isl_stat_error;
1655
278k
  return isl_stat_ok;
1656
278k
}
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
2.26M
{
1666
2.26M
  int r;
1667
2.26M
1668
2.26M
  isl_assert(tab->mat->ctx, tab->n_row < tab->mat->n_row, return -1);
1669
2.26M
  isl_assert(tab->mat->ctx, tab->n_con < tab->max_con, return -1);
1670
2.26M
1671
2.26M
  r = tab->n_con;
1672
2.26M
  tab->con[r].index = tab->n_row;
1673
2.26M
  tab->con[r].is_row = 1;
1674
2.26M
  tab->con[r].is_nonneg = 0;
1675
2.26M
  tab->con[r].is_zero = 0;
1676
2.26M
  tab->con[r].is_redundant = 0;
1677
2.26M
  tab->con[r].frozen = 0;
1678
2.26M
  tab->con[r].negated = 0;
1679
2.26M
  tab->row_var[tab->n_row] = ~r;
1680
2.26M
1681
2.26M
  tab->n_row++;
1682
2.26M
  tab->n_con++;
1683
2.26M
  if (isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->con[r]) < 0)
1684
0
    return -1;
1685
2.26M
1686
2.26M
  return r;
1687
2.26M
}
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
2.34k
{
1696
2.34k
  int i;
1697
2.34k
1698
2.34k
  if (tab->n_var >= tab->max_var)
1699
2.34k
    
isl_die0
(isl_tab_get_ctx(tab), isl_error_internal,
1700
2.34k
      "not enough room for new variable", return -1);
1701
2.34k
  if (first > tab->n_var)
1702
2.34k
    
isl_die0
(isl_tab_get_ctx(tab), isl_error_internal,
1703
2.34k
      "invalid initial position", return -1);
1704
2.34k
1705
2.70k
  
for (i = tab->n_var - 1; 2.34k
i >= first;
--i356
) {
1706
356
    tab->var[i + 1] = tab->var[i];
1707
356
    if (tab->var[i + 1].is_row)
1708
232
      tab->row_var[tab->var[i + 1].index]++;
1709
124
    else
1710
124
      tab->col_var[tab->var[i + 1].index]++;
1711
356
  }
1712
2.34k
1713
2.34k
  tab->n_var++;
1714
2.34k
1715
2.34k
  return 0;
1716
2.34k
}
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
1.27k
{
1725
1.27k
  int i;
1726
1.27k
1727
1.27k
  if (first >= tab->n_var)
1728
1.27k
    
isl_die0
(isl_tab_get_ctx(tab), isl_error_internal,
1729
1.27k
      "invalid initial position", return -1);
1730
1.27k
1731
1.27k
  tab->n_var--;
1732
1.27k
1733
1.50k
  for (i = first; i < tab->n_var; 
++i236
) {
1734
236
    tab->var[i] = tab->var[i + 1];
1735
236
    if (tab->var[i + 1].is_row)
1736
232
      tab->row_var[tab->var[i].index]--;
1737
4
    else
1738
4
      tab->col_var[tab->var[i].index]--;
1739
236
  }
1740
1.27k
1741
1.27k
  return 0;
1742
1.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
2.34k
{
1750
2.34k
  int i;
1751
2.34k
  unsigned off = 2 + tab->M;
1752
2.34k
1753
2.34k
  isl_assert(tab->mat->ctx, tab->n_col < tab->mat->n_col, return -1);
1754
2.34k
1755
2.34k
  if (var_insert_entry(tab, r) < 0)
1756
0
    return -1;
1757
2.34k
1758
2.34k
  tab->var[r].index = tab->n_col;
1759
2.34k
  tab->var[r].is_row = 0;
1760
2.34k
  tab->var[r].is_nonneg = 0;
1761
2.34k
  tab->var[r].is_zero = 0;
1762
2.34k
  tab->var[r].is_redundant = 0;
1763
2.34k
  tab->var[r].frozen = 0;
1764
2.34k
  tab->var[r].negated = 0;
1765
2.34k
  tab->col_var[tab->n_col] = r;
1766
2.34k
1767
12.0k
  for (i = 0; i < tab->n_row; 
++i9.71k
)
1768
9.71k
    isl_int_set_si(tab->mat->row[i][off + tab->n_col], 0);
1769
2.34k
1770
2.34k
  tab->n_col++;
1771
2.34k
  if (isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->var[r]) < 0)
1772
0
    return -1;
1773
2.34k
1774
2.34k
  return r;
1775
2.34k
}
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
2.26M
{
1812
2.26M
  int i;
1813
2.26M
  int r;
1814
2.26M
  isl_int *row;
1815
2.26M
  isl_int a, b;
1816
2.26M
  unsigned off = 2 + tab->M;
1817
2.26M
1818
2.26M
  r = isl_tab_allocate_con(tab);
1819
2.26M
  if (r < 0)
1820
0
    return -1;
1821
2.26M
1822
2.26M
  isl_int_init(a);
1823
2.26M
  isl_int_init(b);
1824
2.26M
  row = tab->mat->row[tab->con[r].index];
1825
2.26M
  isl_int_set_si(row[0], 1);
1826
2.26M
  isl_int_set(row[1], line[0]);
1827
2.26M
  isl_seq_clr(row + 2, tab->M + tab->n_col);
1828
25.0M
  for (i = 0; i < tab->n_var; 
++i22.8M
) {
1829
22.8M
    if (tab->var[i].is_zero)
1830
0
      continue;
1831
22.8M
    if (tab->var[i].is_row) {
1832
3.61M
      isl_int_lcm(a,
1833
3.61M
        row[0], tab->mat->row[tab->var[i].index][0]);
1834
3.61M
      isl_int_swap(a, row[0]);
1835
3.61M
      isl_int_divexact(a, row[0], a);
1836
3.61M
      isl_int_divexact(b,
1837
3.61M
        row[0], tab->mat->row[tab->var[i].index][0]);
1838
3.61M
      isl_int_mul(b, b, line[1 + i]);
1839
3.61M
      isl_seq_combine(row + 1, a, row + 1,
1840
3.61M
          b, tab->mat->row[tab->var[i].index] + 1,
1841
3.61M
          1 + tab->M + tab->n_col);
1842
3.61M
    } else
1843
22.8M
      
isl_int_addmul19.1M
(row[off + tab->var[i].index],
1844
22.8M
              line[1 + i], row[0]);
1845
22.8M
    if (tab->M && 
i >= tab->n_param102k
&&
i < tab->n_var - tab->n_div45.6k
)
1846
22.8M
      
isl_int_submul44.6k
(row[2], line[1 + i], row[0]);
1847
22.8M
  }
1848
2.26M
  isl_seq_normalize(tab->mat->ctx, row, off + tab->n_col);
1849
2.26M
  isl_int_clear(a);
1850
2.26M
  isl_int_clear(b);
1851
2.26M
1852
2.26M
  if (tab->row_sign)
1853
13.0k
    tab->row_sign[tab->con[r].index] = isl_tab_row_unknown;
1854
2.26M
1855
2.26M
  return r;
1856
2.26M
}
1857
1858
static isl_stat drop_row(struct isl_tab *tab, int row)
1859
438k
{
1860
438k
  isl_assert(tab->mat->ctx, ~tab->row_var[row] == tab->n_con - 1,
1861
438k
    return isl_stat_error);
1862
438k
  if (row != tab->n_row - 1)
1863
51.5k
    swap_rows(tab, row, tab->n_row - 1);
1864
438k
  tab->n_row--;
1865
438k
  tab->n_con--;
1866
438k
  return isl_stat_ok;
1867
438k
}
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
1.27k
{
1877
1.27k
  int var;
1878
1.27k
1879
1.27k
  var = tab->col_var[col];
1880
1.27k
  if (col != tab->n_col - 1)
1881
239
    swap_cols(tab, col, tab->n_col - 1);
1882
1.27k
  tab->n_col--;
1883
1.27k
  if (var_drop_entry(tab, var) < 0)
1884
0
    return isl_stat_error;
1885
1.27k
  return isl_stat_ok;
1886
1.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
1.83M
{
1896
1.83M
  int r;
1897
1.83M
  int sgn;
1898
1.83M
  isl_int cst;
1899
1.83M
1900
1.83M
  if (!tab)
1901
0
    return isl_stat_error;
1902
1.83M
  if (tab->bmap) {
1903
133k
    struct isl_basic_map *bmap = tab->bmap;
1904
133k
1905
133k
    isl_assert(tab->mat->ctx, tab->n_eq == bmap->n_eq,
1906
133k
      return isl_stat_error);
1907
133k
    isl_assert(tab->mat->ctx,
1908
133k
          tab->n_con == bmap->n_eq + bmap->n_ineq,
1909
133k
          return isl_stat_error);
1910
133k
    tab->bmap = isl_basic_map_add_ineq(tab->bmap, ineq);
1911
133k
    if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1912
0
      return isl_stat_error;
1913
133k
    if (!tab->bmap)
1914
0
      return isl_stat_error;
1915
1.83M
  }
1916
1.83M
  if (tab->cone) {
1917
1.44k
    isl_int_init(cst);
1918
1.44k
    isl_int_set_si(cst, 0);
1919
1.44k
    isl_int_swap(ineq[0], cst);
1920
1.44k
  }
1921
1.83M
  r = isl_tab_add_row(tab, ineq);
1922
1.83M
  if (tab->cone) {
1923
1.44k
    isl_int_swap(ineq[0], cst);
1924
1.44k
    isl_int_clear(cst);
1925
1.44k
  }
1926
1.83M
  if (r < 0)
1927
0
    return isl_stat_error;
1928
1.83M
  tab->con[r].is_nonneg = 1;
1929
1.83M
  if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1930
0
    return isl_stat_error;
1931
1.83M
  if (isl_tab_row_is_redundant(tab, tab->con[r].index)) {
1932
35.0k
    if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1933
0
      return isl_stat_error;
1934
35.0k
    return isl_stat_ok;
1935
35.0k
  }
1936
1.79M
1937
1.79M
  sgn = restore_row(tab, &tab->con[r]);
1938
1.79M
  if (sgn < -1)
1939
0
    return isl_stat_error;
1940
1.79M
  if (sgn < 0)
1941
25.9k
    return isl_tab_mark_empty(tab);
1942
1.77M
  if (tab->con[r].is_row && 
isl_tab_row_is_redundant(tab, tab->con[r].index)1.62M
)
1943
0
    if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1944
0
      return isl_stat_error;
1945
1.77M
  return isl_stat_ok;
1946
1.77M
}
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
68.1k
{
1954
68.1k
  int i;
1955
68.1k
  int row, col;
1956
68.1k
  unsigned off = 2 + tab->M;
1957
68.1k
1958
68.1k
  if (!var->is_row)
1959
14
    return 0;
1960
68.0k
1961
81.7k
  
while (68.0k
isl_int_is_pos(tab->mat->row[var->index][1])) {
1962
80.1k
    find_pivot(tab, var, NULL, -1, &row, &col);
1963
80.1k
    isl_assert(tab->mat->ctx, row != -1, return -1);
1964
80.1k
    if (isl_tab_pivot(tab, row, col) < 0)
1965
0
      return -1;
1966
80.1k
    if (!var->is_row)
1967
66.5k
      return 0;
1968
80.1k
  }
1969
68.0k
1970
68.0k
  
for (i = tab->n_dead; 1.53k
i < tab->n_col2.58k
;
++i1.04k
)
1971
2.58k
    if (!isl_int_is_zero(tab->mat->row[var->index][off + i]))
1972
2.58k
      
break1.53k
;
1973
1.53k
1974
1.53k
  isl_assert(tab->mat->ctx, i < tab->n_col, return -1);
1975
1.53k
  if (isl_tab_pivot(tab, var->index, i) < 0)
1976
0
    return -1;
1977
1.53k
1978
1.53k
  return 0;
1979
1.53k
}
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
62.3k
{
1991
62.3k
  int i;
1992
62.3k
  int r;
1993
62.3k
1994
62.3k
  if (!tab)
1995
0
    return NULL;
1996
62.3k
  r = isl_tab_add_row(tab, eq);
1997
62.3k
  if (r < 0)
1998
0
    goto error;
1999
62.3k
2000
62.3k
  r = tab->con[r].index;
2001
62.3k
  i = isl_seq_first_non_zero(tab->mat->row[r] + 2 + tab->M + tab->n_dead,
2002
62.3k
          tab->n_col - tab->n_dead);
2003
62.3k
  isl_assert(tab->mat->ctx, i >= 0, goto error);
2004
62.3k
  i += tab->n_dead;
2005
62.3k
  if (isl_tab_pivot(tab, r, i) < 0)
2006
0
    goto error;
2007
62.3k
  if (isl_tab_kill_col(tab, i) < 0)
2008
0
    goto error;
2009
62.3k
  tab->n_eq++;
2010
62.3k
2011
62.3k
  return tab;
2012
0
error:
2013
0
  isl_tab_free(tab);
2014
0
  return NULL;
2015
62.3k
}
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
26.8k
{
2022
26.8k
  return tab->M && 
!0
isl_int_is_zero0
(tab->mat->row[row][2]);
2023
26.8k
}
2024
2025
static int row_is_manifestly_zero(struct isl_tab *tab, int row)
2026
71.8k
{
2027
71.8k
  unsigned off = 2 + tab->M;
2028
71.8k
2029
71.8k
  if (!isl_int_is_zero(tab->mat->row[row][1]))
2030
71.8k
    
return 066.6k
;
2031
5.14k
  if (row_is_big(tab, row))
2032
0
    return 0;
2033
5.14k
  return isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
2034
5.14k
          tab->n_col - tab->n_dead) == -1;
2035
5.14k
}
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
66.9k
{
2044
66.9k
  struct isl_tab_var *var;
2045
66.9k
  int r;
2046
66.9k
2047
66.9k
  if (!tab)
2048
0
    return -1;
2049
66.9k
  r = isl_tab_add_row(tab, eq);
2050
66.9k
  if (r < 0)
2051
0
    return -1;
2052
66.9k
2053
66.9k
  var = &tab->con[r];
2054
66.9k
  r = var->index;
2055
66.9k
  if (row_is_manifestly_zero(tab, r)) {
2056
453
    var->is_zero = 1;
2057
453
    if (isl_tab_mark_redundant(tab, r) < 0)
2058
0
      return -1;
2059
453
    return 0;
2060
453
  }
2061
66.4k
2062
66.4k
  if (isl_int_is_neg(tab->mat->row[r][1])) {
2063
27.6k
    isl_seq_neg(tab->mat->row[r] + 1, tab->mat->row[r] + 1,
2064
27.6k
          1 + tab->n_col);
2065
27.6k
    var->negated = 1;
2066
27.6k
  }
2067
66.4k
  var->is_nonneg = 1;
2068
66.4k
  if (to_col(tab, var) < 0)
2069
0
    return -1;
2070
66.4k
  var->is_nonneg = 0;
2071
66.4k
  if (isl_tab_kill_col(tab, var->index) < 0)
2072
0
    return -1;
2073
66.4k
2074
66.4k
  return 0;
2075
66.4k
}
2076
2077
/* Add a zero row to "tab" and return the corresponding index
2078
 * in the constraint array.
2079
 *
2080
 * This function assumes that at least one more row and at least
2081
 * one more element in the constraint array are available in the tableau.
2082
 */
2083
static int add_zero_row(struct isl_tab *tab)
2084
1.31k
{
2085
1.31k
  int r;
2086
1.31k
  isl_int *row;
2087
1.31k
2088
1.31k
  r = isl_tab_allocate_con(tab);
2089
1.31k
  if (r < 0)
2090
0
    return -1;
2091
1.31k
2092
1.31k
  row = tab->mat->row[tab->con[r].index];
2093
1.31k
  isl_seq_clr(row + 1, 1 + tab->M + tab->n_col);
2094
1.31k
  isl_int_set_si(row[0], 1);
2095
1.31k
2096
1.31k
  return r;
2097
1.31k
}
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
4.87k
{
2108
4.87k
  struct isl_tab_undo *snap = NULL;
2109
4.87k
  struct isl_tab_var *var;
2110
4.87k
  int r;
2111
4.87k
  int row;
2112
4.87k
  int sgn;
2113
4.87k
  isl_int cst;
2114
4.87k
2115
4.87k
  if (!tab)
2116
0
    return -1;
2117
4.87k
  isl_assert(tab->mat->ctx, !tab->M, return -1);
2118
4.87k
2119
4.87k
  if (tab->need_undo)
2120
4.58k
    snap = isl_tab_snap(tab);
2121
4.87k
2122
4.87k
  if (tab->cone) {
2123
450
    isl_int_init(cst);
2124
450
    isl_int_set_si(cst, 0);
2125
450
    isl_int_swap(eq[0], cst);
2126
450
  }
2127
4.87k
  r = isl_tab_add_row(tab, eq);
2128
4.87k
  if (tab->cone) {
2129
450
    isl_int_swap(eq[0], cst);
2130
450
    isl_int_clear(cst);
2131
450
  }
2132
4.87k
  if (r < 0)
2133
0
    return -1;
2134
4.87k
2135
4.87k
  var = &tab->con[r];
2136
4.87k
  row = var->index;
2137
4.87k
  if (row_is_manifestly_zero(tab, row)) {
2138
3.24k
    if (snap)
2139
3.22k
      return isl_tab_rollback(tab, snap);
2140
25
    return drop_row(tab, row);
2141
25
  }
2142
1.63k
2143
1.63k
  if (tab->bmap) {
2144
1.31k
    tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
2145
1.31k
    if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
2146
0
      return -1;
2147
1.31k
    isl_seq_neg(eq, eq, 1 + tab->n_var);
2148
1.31k
    tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
2149
1.31k
    isl_seq_neg(eq, eq, 1 + tab->n_var);
2150
1.31k
    if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
2151
0
      return -1;
2152
1.31k
    if (!tab->bmap)
2153
0
      return -1;
2154
1.31k
    if (add_zero_row(tab) < 0)
2155
0
      return -1;
2156
1.63k
  }
2157
1.63k
2158
1.63k
  sgn = isl_int_sgn(tab->mat->row[row][1]);
2159
1.63k
2160
1.63k
  if (sgn > 0) {
2161
49
    isl_seq_neg(tab->mat->row[row] + 1, tab->mat->row[row] + 1,
2162
49
          1 + tab->n_col);
2163
49
    var->negated = 1;
2164
49
    sgn = -1;
2165
49
  }
2166
1.63k
2167
1.63k
  if (sgn < 0) {
2168
1.19k
    sgn = sign_of_max(tab, var);
2169
1.19k
    if (sgn < -1)
2170
0
      return -1;
2171
1.19k
    if (sgn < 0) {
2172
0
      if (isl_tab_mark_empty(tab) < 0)
2173
0
        return -1;
2174
0
      return 0;
2175
0
    }
2176
1.19k
  }
2177
1.63k
2178
1.63k
  var->is_nonneg = 1;
2179
1.63k
  if (to_col(tab, var) < 0)
2180
0
    return -1;
2181
1.63k
  var->is_nonneg = 0;
2182
1.63k
  if (isl_tab_kill_col(tab, var->index) < 0)
2183
0
    return -1;
2184
1.63k
2185
1.63k
  return 0;
2186
1.63k
}
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
903
{
2200
903
  unsigned total;
2201
903
  unsigned div_pos;
2202
903
  struct isl_vec *ineq;
2203
903
2204
903
  if (!bmap)
2205
0
    return NULL;
2206
903
2207
903
  total = isl_basic_map_total_dim(bmap);
2208
903
  div_pos = 1 + total - bmap->n_div + div;
2209
903
2210
903
  ineq = isl_vec_alloc(bmap->ctx, 1 + total);
2211
903
  if (!ineq)
2212
0
    return NULL;
2213
903
2214
903
  isl_seq_cpy(ineq->el, bmap->div[div] + 1, 1 + total);
2215
903
  isl_int_neg(ineq->el[div_pos], bmap->div[div][0]);
2216
903
  return ineq;
2217
903
}
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
903
{
2237
903
  unsigned total;
2238
903
  unsigned div_pos;
2239
903
  struct isl_vec *ineq;
2240
903
2241
903
  total = isl_basic_map_total_dim(tab->bmap);
2242
903
  div_pos = 1 + total - tab->bmap->n_div + div;
2243
903
2244
903
  ineq = ineq_for_div(tab->bmap, div);
2245
903
  if (!ineq)
2246
0
    goto error;
2247
903
2248
903
  if (add_ineq) {
2249
306
    if (add_ineq(user, ineq->el) < 0)
2250
0
      goto error;
2251
597
  } else {
2252
597
    if (isl_tab_add_ineq(tab, ineq->el) < 0)
2253
0
      goto error;
2254
903
  }
2255
903
2256
903
  isl_seq_neg(ineq->el, tab->bmap->div[div] + 1, 1 + total);
2257
903
  isl_int_set(ineq->el[div_pos], tab->bmap->div[div][0]);
2258
903
  isl_int_add(ineq->el[0], ineq->el[0], ineq->el[div_pos]);
2259
903
  isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
2260
903
2261
903
  if (add_ineq) {
2262
306
    if (add_ineq(user, ineq->el) < 0)
2263
0
      goto error;
2264
597
  } else {
2265
597
    if (isl_tab_add_ineq(tab, ineq->el) < 0)
2266
0
      goto error;
2267
903
  }
2268
903
2269
903
  isl_vec_free(ineq);
2270
903
2271
903
  return 0;
2272
0
error:
2273
0
  isl_vec_free(ineq);
2274
0
  return -1;
2275
903
}
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
903
{
2285
903
  int i;
2286
903
2287
903
  if (tab->M)
2288
0
    return 1;
2289
903
2290
903
  if (isl_int_is_neg(div->el[1]))
2291
903
    
return 094
;
2292
809
2293
1.72k
  
for (i = 0; 809
i < tab->n_var;
++i913
) {
2294
1.62k
    if (isl_int_is_neg(div->el[2 + i]))
2295
1.62k
      
return 0210
;
2296
1.41k
    if (isl_int_is_zero(div->el[2 + i]))
2297
1.41k
      
continue805
;
2298
607
    if (!tab->var[i].is_nonneg)
2299
499
      return 0;
2300
1.62k
  }
2301
809
2302
809
  
return 1100
;
2303
903
}
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
903
{
2319
903
  int r;
2320
903
  int nonneg;
2321
903
  int n_div, o_div;
2322
903
2323
903
  if (!tab || !div)
2324
0
    return -1;
2325
903
2326
903
  if (div->size != 1 + 1 + tab->n_var)
2327
903
    
isl_die0
(isl_tab_get_ctx(tab), isl_error_invalid,
2328
903
      "unexpected size", return -1);
2329
903
2330
903
  isl_assert(tab->mat->ctx, tab->bmap, return -1);
2331
903
  n_div = isl_basic_map_dim(tab->bmap, isl_dim_div);
2332
903
  o_div = tab->n_var - n_div;
2333
903
  if (pos < o_div || pos > tab->n_var)
2334
903
    
isl_die0
(isl_tab_get_ctx(tab), isl_error_invalid,
2335
903
      "invalid position", return -1);
2336
903
2337
903
  nonneg = div_is_nonneg(tab, div);
2338
903
2339
903
  if (isl_tab_extend_cons(tab, 3) < 0)
2340
0
    return -1;
2341
903
  if (isl_tab_extend_vars(tab, 1) < 0)
2342
0
    return -1;
2343
903
  r = isl_tab_insert_var(tab, pos);
2344
903
  if (r < 0)
2345
0
    return -1;
2346
903
2347
903
  if (nonneg)
2348
100
    tab->var[r].is_nonneg = 1;
2349
903
2350
903
  tab->bmap = isl_basic_map_insert_div(tab->bmap, pos - o_div, div);
2351
903
  if (!tab->bmap)
2352
0
    return -1;
2353
903
  if (isl_tab_push_var(tab, isl_tab_undo_bmap_div, &tab->var[r]) < 0)
2354
0
    return -1;
2355
903
2356
903
  if (add_div_constraints(tab, pos - o_div, add_ineq, user) < 0)
2357
0
    return -1;
2358
903
2359
903
  return r;
2360
903
}
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
597
{
2367
597
  if (!tab)
2368
0
    return -1;
2369
597
  return isl_tab_insert_div(tab, tab->n_var, div, NULL, NULL);
2370
597
}
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
276k
{
2380
276k
  int i;
2381
276k
  struct isl_tab *tab;
2382
276k
2383
276k
  if (!bmap)
2384
0
    return NULL;
2385
276k
  tab = isl_tab_alloc(bmap->ctx,
2386
276k
          isl_basic_map_total_dim(bmap) + bmap->n_ineq + 1,
2387
276k
          isl_basic_map_total_dim(bmap), 0);
2388
276k
  if (!tab)
2389
0
    return NULL;
2390
276k
  tab->preserve = track;
2391
276k
  tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
2392
276k
  if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)) {
2393
8
    if (isl_tab_mark_empty(tab) < 0)
2394
0
      goto error;
2395
8
    goto done;
2396
8
  }
2397
338k
  
for (i = 0; 276k
i < bmap->n_eq;
++i61.5k
) {
2398
61.5k
    tab = add_eq(tab, bmap->eq[i]);
2399
61.5k
    if (!tab)
2400
0
      return tab;
2401
61.5k
  }
2402
1.96M
  
for (i = 0; 276k
i < bmap->n_ineq;
++i1.68M
) {
2403
1.69M
    if (isl_tab_add_ineq(tab, bmap->ineq[i]) < 0)
2404
0
      goto error;
2405
1.69M
    if (tab->empty)
2406
2.78k
      goto done;
2407
1.69M
  }
2408
276k
done:
2409
276k
  if (track && 
isl_tab_track_bmap(tab, isl_basic_map_copy(bmap)) < 088.8k
)
2410
0
    goto error;
2411
276k
  return tab;
2412
0
error:
2413
0
  isl_tab_free(tab);
2414
0
  return NULL;
2415
276k
}
2416
2417
__isl_give struct isl_tab *isl_tab_from_basic_set(
2418
  __isl_keep isl_basic_set *bset, int track)
2419
120k
{
2420
120k
  return isl_tab_from_basic_map(bset, track);
2421
120k
}
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
1.52k
{
2428
1.52k
  isl_int cst;
2429
1.52k
  int i;
2430
1.52k
  struct isl_tab *tab;
2431
1.52k
  unsigned offset = 0;
2432
1.52k
2433
1.52k
  if (!bset)
2434
0
    return NULL;
2435
1.52k
  if (parametric)
2436
1.05k
    offset = isl_basic_set_dim(bset, isl_dim_param);
2437
1.52k
  tab = isl_tab_alloc(bset->ctx, bset->n_eq + bset->n_ineq,
2438
1.52k
        isl_basic_set_total_dim(bset) - offset, 0);
2439
1.52k
  if (!tab)
2440
0
    return NULL;
2441
1.52k
  tab->rational = ISL_F_ISSET(bset, ISL_BASIC_SET_RATIONAL);
2442
1.52k
  tab->cone = 1;
2443
1.52k
2444
1.52k
  isl_int_init(cst);
2445
1.52k
  isl_int_set_si(cst, 0);
2446
2.65k
  for (i = 0; i < bset->n_eq; 
++i1.13k
) {
2447
1.13k
    isl_int_swap(bset->eq[i][offset], cst);
2448
1.13k
    if (offset > 0) {
2449
292
      if (isl_tab_add_eq(tab, bset->eq[i] + offset) < 0)
2450
0
        goto error;
2451
840
    } else
2452
840
      tab = add_eq(tab, bset->eq[i]);
2453
1.13k
    isl_int_swap(bset->eq[i][offset], cst);
2454
1.13k
    if (!tab)
2455
0
      goto done;
2456
1.13k
  }
2457
6.60k
  
for (i = 0; 1.52k
i < bset->n_ineq;
++i5.07k
) {
2458
5.07k
    int r;
2459
5.07k
    isl_int_swap(bset->ineq[i][offset], cst);
2460
5.07k
    r = isl_tab_add_row(tab, bset->ineq[i] + offset);
2461
5.07k
    isl_int_swap(bset->ineq[i][offset], cst);
2462
5.07k
    if (r < 0)
2463
0
      goto error;
2464
5.07k
    tab->con[r].is_nonneg = 1;
2465
5.07k
    if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
2466
0
      goto error;
2467
5.07k
  }
2468
1.52k
done:
2469
1.52k
  isl_int_clear(cst);
2470
1.52k
  return tab;
2471
0
error:
2472
0
  isl_int_clear(cst);
2473
0
  isl_tab_free(tab);
2474
0
  return NULL;
2475
1.52k
}
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
1.05k
{
2482
1.05k
  int i;
2483
1.05k
2484
1.05k
  if (!tab)
2485
0
    return isl_bool_error;
2486
1.05k
  if (tab->empty)
2487
0
    return isl_bool_true;
2488
1.05k
  if (tab->n_dead == tab->n_col)
2489
344
    return isl_bool_true;
2490
711
2491
1.12k
  
for (;;) 711
{
2492
1.24k
    for (i = tab->n_redundant; i < tab->n_row; 
++i118
) {
2493
1.24k
      struct isl_tab_var *var;
2494
1.24k
      int sgn;
2495
1.24k
      var = isl_tab_var_from_row(tab, i);
2496
1.24k
      if (!var->is_nonneg)
2497
118
        continue;
2498
1.12k
      sgn = sign_of_max(tab, var);
2499
1.12k
      if (sgn < -1)
2500
0
        return isl_bool_error;
2501
1.12k
      if (sgn != 0)
2502
85
        return isl_bool_false;
2503
1.04k
      if (close_row(tab, var, 0) < 0)
2504
0
        return isl_bool_error;
2505
1.04k
      break;
2506
1.04k
    }
2507
1.12k
    
if (1.04k
tab->n_dead == tab->n_col1.04k
)
2508
626
      return isl_bool_true;
2509
415
    if (i == tab->n_row)
2510
0
      return isl_bool_false;
2511
0
  }
2512
1.05k
}
2513
2514
int isl_tab_sample_is_integer(struct isl_tab *tab)
2515
202k
{
2516
202k
  int i;
2517
202k
2518
202k
  if (!tab)
2519
0
    return -1;
2520
202k
2521
1.01M
  
for (i = 0; 202k
i < tab->n_var;
++i815k
) {
2522
879k
    int row;
2523
879k
    if (!tab->var[i].is_row)
2524
239k
      continue;
2525
640k
    row = tab->var[i].index;
2526
640k
    if (!isl_int_is_divisible_by(tab->mat->row[row][1],
2527
640k
            tab->mat->row[row][0]))
2528
640k
      
return 063.6k
;
2529
879k
  }
2530
202k
  
return 1139k
;
2531
202k
}
2532
2533
static struct isl_vec *extract_integer_sample(struct isl_tab *tab)
2534
75.8k
{
2535
75.8k
  int i;
2536
75.8k
  struct isl_vec *vec;
2537
75.8k
2538
75.8k
  vec = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
2539
75.8k
  if (!vec)
2540
0
    return NULL;
2541
75.8k
2542
75.8k
  isl_int_set_si(vec->block.data[0], 1);
2543
546k
  for (i = 0; i < tab->n_var; 
++i470k
) {
2544
470k
    if (!tab->var[i].is_row)
2545
470k
      
isl_int_set_si177k
(vec->block.data[1 + i], 0);
2546
470k
    else {
2547
293k
      int row = tab->var[i].index;
2548
293k
      isl_int_divexact(vec->block.data[1 + i],
2549
293k
        tab->mat->row[row][1], tab->mat->row[row][0]);
2550
293k
    }
2551
470k
  }
2552
75.8k
2553
75.8k
  return vec;
2554
75.8k
}
2555
2556
struct isl_vec *isl_tab_get_sample_value(struct isl_tab *tab)
2557
114k
{
2558
114k
  int i;
2559
114k
  struct isl_vec *vec;
2560
114k
  isl_int m;
2561
114k
2562
114k
  if (!tab)
2563
0
    return NULL;
2564
114k
2565
114k
  vec = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
2566
114k
  if (!vec)
2567
0
    return NULL;
2568
114k
2569
114k
  isl_int_init(m);
2570
114k
2571
114k
  isl_int_set_si(vec->block.data[0], 1);
2572
559k
  for (i = 0; i < tab->n_var; 
++i444k
) {
2573
444k
    int row;
2574
444k
    if (!tab->var[i].is_row) {
2575
212k
      isl_int_set_si(vec->block.data[1 + i], 0);
2576
212k
      continue;
2577
212k
    }
2578
231k
    row = tab->var[i].index;
2579
231k
    isl_int_gcd(m, vec->block.data[0], tab->mat->row[row][0]);
2580
231k
    isl_int_divexact(m, tab->mat->row[row][0], m);
2581
231k
    isl_seq_scale(vec->block.data, vec->block.data, m, 1 + i);
2582
231k
    isl_int_divexact(m, vec->block.data[0], tab->mat->row[row][0]);
2583
231k
    isl_int_mul(vec->block.data[1 + i], m, tab->mat->row[row][1]);
2584
444k
  }
2585
114k
  vec = isl_vec_normalize(vec);
2586
114k
2587
114k
  isl_int_clear(m);
2588
114k
  return vec;
2589
114k
}
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
143k
{
2597
143k
  if (!var->is_row)
2598
143k
    
isl_int_set_si900
(*v, 0);
2599
143k
  else 
if (143k
sgn > 0143k
)
2600
143k
    
isl_int_cdiv_q142k
(*v, tab->mat->row[var->index][1],
2601
143k
           tab->mat->row[var->index][0]);
2602
143k
  else
2603
143k
    
isl_int_fdiv_q579
(*v, tab->mat->row[var->index][1],
2604
143k
           tab->mat->row[var->index][0]);
2605
143k
}
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
156k
{
2618
156k
  int i;
2619
156k
  unsigned n_eq;
2620
156k
2621
156k
  if (!bmap)
2622
0
    return NULL;
2623
156k
  if (!tab)
2624
0
    return bmap;
2625
156k
2626
156k
  n_eq = tab->n_eq;
2627
156k
  if (tab->empty)
2628
1.46k
    bmap = isl_basic_map_set_to_empty(bmap);
2629
154k
  else
2630
1.29M
    
for (i = bmap->n_ineq - 1; 154k
i >= 0;
--i1.14M
) {
2631
1.14M
      if (isl_tab_is_equality(tab, n_eq + i))
2632
514k
        isl_basic_map_inequality_to_equality(bmap, i);
2633
625k
      else if (isl_tab_is_redundant(tab, n_eq + i))
2634
89.8k
        isl_basic_map_drop_inequality(bmap, i);
2635
154k
    }
2636
156k
  if (bmap->n_eq != n_eq)
2637
64.6k
    bmap = isl_basic_map_gauss(bmap, NULL);
2638
156k
  if (!tab->rational &&
2639
156k
      bmap && !bmap->sample && 
isl_tab_sample_is_integer(tab)83.6k
)
2640
75.8k
    bmap->sample = extract_integer_sample(tab);
2641
156k
  return bmap;
2642
156k
}
2643
2644
struct isl_basic_set *isl_basic_set_update_from_tab(struct isl_basic_set *bset,
2645
  struct isl_tab *tab)
2646
14.5k
{
2647
14.5k
  return bset_from_bmap(isl_basic_map_update_from_tab(bset_to_bmap(bset),
2648
14.5k
                tab));
2649
14.5k
}
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
4.17k
{
2656
4.17k
  if (!tab->con[r].is_row)
2657
4.17k
    
isl_die0
(isl_tab_get_ctx(tab), isl_error_internal,
2658
4.17k
      "row unexpectedly moved to column",
2659
4.17k
      return isl_stat_error);
2660
4.17k
  if (r + 1 != tab->n_con)
2661
4.17k
    
isl_die0
(isl_tab_get_ctx(tab), isl_error_internal,
2662
4.17k
      "additional constraints added", return isl_stat_error);
2663
4.17k
  if (drop_row(tab, tab->con[r].index) < 0)
2664
0
    return isl_stat_error;
2665
4.17k
2666
4.17k
  return isl_stat_ok;
2667
4.17k
}
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
4.17k
{
2681
4.17k
  unsigned r;
2682
4.17k
  isl_int *row;
2683
4.17k
  int sgn;
2684
4.17k
  unsigned off = 2 + tab->M;
2685
4.17k
2686
4.17k
  if (var->is_zero)
2687
0
    return isl_stat_ok;
2688
4.17k
  if (var->is_redundant || !var->is_nonneg)
2689
4.17k
    
isl_die0
(isl_tab_get_ctx(tab), isl_error_invalid,
2690
4.17k
      "expecting non-redundant non-negative variable",
2691
4.17k
      return isl_stat_error);
2692
4.17k
2693
4.17k
  if (isl_tab_extend_cons(tab, 1) < 0)
2694
0
    return isl_stat_error;
2695
4.17k
2696
4.17k
  r = tab->n_con;
2697
4.17k
  tab->con[r].index = tab->n_row;
2698
4.17k
  tab->con[r].is_row = 1;
2699
4.17k
  tab->con[r].is_nonneg = 0;
2700
4.17k
  tab->con[r].is_zero = 0;
2701
4.17k
  tab->con[r].is_redundant = 0;
2702
4.17k
  tab->con[r].frozen = 0;
2703
4.17k
  tab->con[r].negated = 0;
2704
4.17k
  tab->row_var[tab->n_row] = ~r;
2705
4.17k
  row = tab->mat->row[tab->n_row];
2706
4.17k
2707
4.17k
  if (var->is_row) {
2708
353
    isl_int_set(row[0], tab->mat->row[var->index][0]);
2709
353
    isl_seq_neg(row + 1,
2710
353
          tab->mat->row[var->index] + 1, 1 + tab->n_col);
2711
3.82k
  } else {
2712
3.82k
    isl_int_set_si(row[0], 1);
2713
3.82k
    isl_seq_clr(row + 1, 1 + tab->n_col);
2714
3.82k
    isl_int_set_si(row[off + var->index], -1);
2715
3.82k
  }
2716
4.17k
2717
4.17k
  tab->n_row++;
2718
4.17k
  tab->n_con++;
2719
4.17k
2720
4.17k
  sgn = sign_of_max(tab, &tab->con[r]);
2721
4.17k
  if (sgn < -1)
2722
0
    return isl_stat_error;
2723
4.17k
  if (sgn < 0) {
2724
19
    if (drop_last_con_in_row(tab, r) < 0)
2725
0
      return isl_stat_error;
2726
19
    if (isl_tab_mark_empty(tab) < 0)
2727
0
      return isl_stat_error;
2728
19
    return isl_stat_ok;
2729
19
  }
2730
4.15k
  tab->con[r].is_nonneg = 1;
2731
4.15k
  /* sgn == 0 */
2732
4.15k
  if (close_row(tab, &tab->con[r], 1) < 0)
2733
0
    return isl_stat_error;
2734
4.15k
  if (drop_last_con_in_row(tab, r) < 0)
2735
0
    return isl_stat_error;
2736
4.15k
2737
4.15k
  return isl_stat_ok;
2738
4.15k
}
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
556
{
2759
556
  struct isl_tab_var *var;
2760
556
2761
556
  if (!tab)
2762
0
    return -1;
2763
556
2764
556
  var = &tab->con[con];
2765
556
2766
556
  if (var->is_row && 
(12
var->index < 012
||
var->index < tab->n_redundant12
))
2767
556
    
isl_die0
(tab->mat->ctx, isl_error_invalid,
2768
556
      "cannot relax redundant constraint", return -1);
2769
556
  if (!var->is_row && 
(544
var->index < 0544
||
var->index < tab->n_dead544
))
2770
556
    
isl_die0
(tab->mat->ctx, isl_error_invalid,
2771
556
      "cannot relax dead constraint", return -1);
2772
556
2773
556
  if (!var->is_row && 
!max_is_manifestly_unbounded(tab, var)544
)
2774
57
    if (to_row(tab, var, 1) < 0)
2775
0
      return -1;
2776
556
  if (!var->is_row && 
!min_is_manifestly_unbounded(tab, var)487
)
2777
5
    if (to_row(tab, var, -1) < 0)
2778
0
      return -1;
2779
556
2780
556
  if (var->is_row) {
2781
74
    isl_int_add(tab->mat->row[var->index][1],
2782
74
        tab->mat->row[var->index][1], tab->mat->row[var->index][0]);
2783
74
    if (restore_row(tab, var) < 0)
2784
0
      return -1;
2785
482
  } else {
2786
482
    int i;
2787
482
    unsigned off = 2 + tab->M;
2788
482
2789
2.75k
    for (i = 0; i < tab->n_row; 
++i2.26k
) {
2790
2.26k
      if (isl_int_is_zero(tab->mat->row[i][off + var->index]))
2791
2.26k
        
continue1.71k
;
2792
557
      isl_int_sub(tab->mat->row[i][1], tab->mat->row[i][1],
2793
557
          tab->mat->row[i][off + var->index]);
2794
557
    }
2795
482
2796
482
  }
2797
556
2798
556
  if (isl_tab_push_var(tab, isl_tab_undo_relax, var) < 0)
2799
0
    return -1;
2800
556
2801
556
  return 0;
2802
556
}
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
61
{
2823
61
  struct isl_tab_var *var;
2824
61
2825
61
  if (!tab)
2826
0
    return -1;
2827
61
  if (isl_int_is_zero(shift))
2828
61
    
return 034
;
2829
27
2830
27
  var = &tab->var[pos];
2831
27
  if (!var->is_row) {
2832
1
    if (isl_int_is_neg(shift)) {
2833
1
      if (!max_is_manifestly_unbounded(tab, var))
2834
0
        if (to_row(tab, var, 1) < 0)
2835
0
          return -1;
2836
0
    } else {
2837
0
      if (!min_is_manifestly_unbounded(tab, var))
2838
0
        if (to_row(tab, var, -1) < 0)
2839
0
          return -1;
2840
27
    }
2841
1
  }
2842
27
2843
27
  if (var->is_row) {
2844
26
    isl_int_addmul(tab->mat->row[var->index][1],
2845
26
        shift, tab->mat->row[var->index][0]);
2846
26
  } else {
2847
1
    int i;
2848
1
    unsigned off = 2 + tab->M;
2849
1
2850
5
    for (i = 0; i < tab->n_row; 
++i4
) {
2851
4
      if (isl_int_is_zero(tab->mat->row[i][off + var->index]))
2852
4
        
continue2
;
2853
2
      isl_int_submul(tab->mat->row[i][1],
2854
2
            shift, tab->mat->row[i][off + var->index]);
2855
2
    }
2856
1
2857
1
  }
2858
61
2859
61
  return 0;
2860
61
}
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
139
{
2869
139
  struct isl_tab_var *var;
2870
139
2871
139
  if (!tab)
2872
0
    return -1;
2873
139
2874
139
  var = &tab->con[con];
2875
139
  if (!var->is_nonneg)
2876
0
    return 0;
2877
139
2878
139
  var->is_nonneg = 0;
2879
139
  if (isl_tab_push_var(tab, isl_tab_undo_unrestrict, var) < 0)
2880
0
    return -1;
2881
139
2882
139
  return 0;
2883
139
}
2884
2885
int isl_tab_select_facet(struct isl_tab *tab, int con)
2886
3.93k
{
2887
3.93k
  if (!tab)
2888
0
    return -1;
2889
3.93k
2890
3.93k
  return cut_to_hyperplane(tab, &tab->con[con]);
2891
3.93k
}
2892
2893
static int may_be_equality(struct isl_tab *tab, int row)
2894
4.23M
{
2895
4.23M
  return tab->rational ? 
isl_int_is_zero0
(tab->mat->row[row][1])
2896
4.23M
           : isl_int_lt(tab->mat->row[row][1],
2897
4.23M
              tab->mat->row[row][0]);
2898
4.23M
}
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
919k
{
2913
919k
  int i;
2914
919k
  struct isl_tab_var *var;
2915
919k
2916
8.83M
  for (i = tab->n_con - 1; i >= 0; 
--i7.91M
) {
2917
8.77M
    var = &tab->con[i];
2918
8.77M
    if (var->index < 0)
2919
3.44M
      continue;
2920
5.33M
    if (var->is_row && 
var->index < tab->n_redundant4.24M
)
2921
311k
      continue;
2922
5.01M
    if (!var->is_row && 
var->index < tab->n_dead1.08M
)
2923
663
      continue;
2924
5.01M
    if (var->marked)
2925
851k
      return var;
2926
8.77M
  }
2927
919k
2928
919k
  
return NULL67.9k
;
2929
919k
}
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
165k
{
2950
165k
  int i;
2951
165k
  unsigned n_marked;
2952
165k
2953
165k
  if (!tab)
2954
0
    return -1;
2955
165k
  if (tab->empty)
2956
768
    return 0;
2957
164k
  if (tab->n_dead == tab->n_col)
2958
175
    return 0;
2959
164k
2960
164k
  n_marked = 0;
2961
1.39M
  for (i = tab->n_redundant; i < tab->n_row; 
++i1.22M
) {
2962
1.22M
    struct isl_tab_var *var = isl_tab_var_from_row(tab, i);
2963
1.22M
    var->marked = !var->frozen && 
var->is_nonneg1.21M
&&
2964
1.22M
      
may_be_equality(tab, i)1.13M
;
2965
1.22M
    if (var->marked)
2966
838k
      n_marked++;
2967
1.22M
  }
2968
1.09M
  for (i = tab->n_dead; i < tab->n_col; 
++i934k
) {
2969
934k
    struct isl_tab_var *var = var_from_col(tab, i);
2970
934k
    var->marked = !var->frozen && 
var->is_nonneg931k
;
2971
934k
    if (var->marked)
2972
76.7k
      n_marked++;
2973
934k
  }
2974
752k
  while (n_marked) {
2975
653k
    struct isl_tab_var *var;
2976
653k
    int sgn;
2977
653k
    var = select_marked(tab);
2978
653k
    if (!var)
2979
65.1k
      break;
2980
588k
    var->marked = 0;
2981
588k
    n_marked--;
2982
588k
    sgn = sign_of_max(tab, var);
2983
588k
    if (sgn < 0)
2984
0
      return -1;
2985
588k
    if (sgn == 0) {
2986
273k
      if (close_row(tab, var, 0) < 0)
2987
0
        return -1;
2988
315k
    } else if (!tab->rational && !at_least_one(tab, var)) {
2989
234
      if (cut_to_hyperplane(tab, var) < 0)
2990
0
        return -1;
2991
234
      return isl_tab_detect_implicit_equalities(tab);
2992
234
    }
2993
6.03M
    
for (i = tab->n_redundant; 588k
i < tab->n_row;
++i5.44M
) {
2994
5.44M
      var = isl_tab_var_from_row(tab, i);
2995
5.44M
      if (!var->marked)
2996
2.34M
        continue;
2997
3.10M
      if (may_be_equality(tab, i))
2998
3.05M
        continue;
2999
44.0k
      var->marked = 0;
3000
44.0k
      n_marked--;
3001
44.0k
    }
3002
653k
  }
3003
164k
3004
164k
  
return 0164k
;
3005
165k
}
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
14
{
3012
14
  int *p;
3013
14
  int index;
3014
14
3015
14
  index = tab->con[i].index;
3016
14
  if (index == -1)
3017
10
    return 0;
3018
4
  p = tab->con[i].is_row ? 
tab->row_var2
:
tab->col_var2
;
3019
4
  if (p[index] != ~old)
3020
4
    
isl_die0
(tab->mat->ctx, isl_error_internal,
3021
4
      "broken internal state", return -1);
3022
4
  p[index] = ~i;
3023
4
3024
4
  return 0;
3025
14
}
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
8
{
3032
8
  int i, last;
3033
8
  struct isl_tab_var var;
3034
8
3035
8
  if (n <= 1)
3036
4
    return 0;
3037
4
3038
4
  last = first + n - 1;
3039
4
  var = tab->con[last];
3040
14
  for (i = last; i > first; 
--i10
) {
3041
10
    tab->con[i] = tab->con[i - 1];
3042
10
    if (update_con_after_move(tab, i, i - 1) < 0)
3043
0
      return -1;
3044
10
  }
3045
4
  tab->con[first] = var;
3046
4
  if (update_con_after_move(tab, first, last) < 0)
3047
0
    return -1;
3048
4
3049
4
  return 0;
3050
4
}
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
10.2k
{
3085
10.2k
  int i;
3086
10.2k
3087
10.2k
  if (!tab || !bmap)
3088
0
    return isl_basic_map_free(bmap);
3089
10.2k
  if (tab->empty)
3090
2
    return bmap;
3091
10.2k
3092
33.0k
  
for (i = bmap->n_ineq - 1; 10.2k
i >= 0;
--i22.7k
) {
3093
22.7k
    if (!isl_tab_is_equality(tab, bmap->n_eq + i))
3094
22.7k
      continue;
3095
4
    isl_basic_map_inequality_to_equality(bmap, i);
3096
4
    if (rotate_constraints(tab, 0, tab->n_eq + i + 1) < 0)
3097
0
      return isl_basic_map_free(bmap);
3098
4
    if (rotate_constraints(tab, tab->n_eq + i + 1,
3099
4
          bmap->n_ineq - i) < 0)
3100
0
      return isl_basic_map_free(bmap);
3101
4
    tab->n_eq++;
3102
4
  }
3103
10.2k
3104
10.2k
  return bmap;
3105
10.2k
}
3106
3107
static int con_is_redundant(struct isl_tab *tab, struct isl_tab_var *var)
3108
334k
{
3109
334k
  if (!tab)
3110
0
    return -1;
3111
334k
  if (tab->rational) {
3112
378
    int sgn = sign_of_min(tab, var);
3113
378
    if (sgn < -1)
3114
0
      return -1;
3115
378
    return sgn >= 0;
3116
334k
  } else {
3117
334k
    int irred = isl_tab_min_at_most_neg_one(tab, var);
3118
334k
    if (irred < 0)
3119
0
      return -1;
3120
334k
    return !irred;
3121
334k
  }
3122
334k
}
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
71.9k
{
3139
71.9k
  int i;
3140
71.9k
  unsigned n_marked;
3141
71.9k
3142
71.9k
  if (!tab)
3143
0
    return -1;
3144
71.9k
  if (tab->empty)
3145
747
    return 0;
3146
71.1k
  if (tab->n_redundant == tab->n_row)
3147
1.08k
    return 0;
3148
70.0k
3149
70.0k
  n_marked = 0;
3150
629k
  for (i = tab->n_redundant; i < tab->n_row; 
++i559k
) {
3151
559k
    struct isl_tab_var *var = isl_tab_var_from_row(tab, i);
3152
559k
    var->marked = !var->frozen && 
var->is_nonneg491k
;
3153
559k
    if (var->marked)
3154
251k
      n_marked++;
3155
559k
  }
3156
524k
  for (i = tab->n_dead; i < tab->n_col; 
++i454k
) {
3157
454k
    struct isl_tab_var *var = var_from_col(tab, i);
3158
454k
    var->marked = !var->frozen && 
var->is_nonneg424k
&&
3159
454k
      
!min_is_manifestly_unbounded(tab, var)173k
;
3160
454k
    if (var->marked)
3161
60.9k
      n_marked++;
3162
454k
  }
3163
333k
  while (n_marked) {
3164
266k
    struct isl_tab_var *var;
3165
266k
    int red;
3166
266k
    var = select_marked(tab);
3167
266k
    if (!var)
3168
2.84k
      break;
3169
263k
    var->marked = 0;
3170
263k
    n_marked--;
3171
263k
    red = con_is_redundant(tab, var);
3172
263k
    if (red < 0)
3173
0
      return -1;
3174
263k
    if (red && 
!var->is_redundant62.7k
)
3175
14.7k
      if (isl_tab_mark_redundant(tab, var->index) < 0)
3176
0
        return -1;
3177
4.54M
    
for (i = tab->n_dead; 263k
i < tab->n_col;
++i4.27M
) {
3178
4.27M
      var = var_from_col(tab, i);
3179
4.27M
      if (!var->marked)
3180
4.08M
        continue;
3181
189k
      if (!min_is_manifestly_unbounded(tab, var))
3182
145k
        continue;
3183
44.4k
      var->marked = 0;
3184
44.4k
      n_marked--;
3185
44.4k
    }
3186
266k
  }
3187
70.0k
3188
70.0k
  return 0;
3189
71.9k
}
3190
3191
int isl_tab_is_equality(struct isl_tab *tab, int con)
3192
1.17M
{
3193
1.17M
  int row;
3194
1.17M
  unsigned off;
3195
1.17M
3196
1.17M
  if (!tab)
3197
0
    return -1;
3198
1.17M
  if (tab->con[con].is_zero)
3199
515k
    return 1;
3200
656k
  if (tab->con[con].is_redundant)
3201
90.3k
    return 0;
3202
565k
  if (!tab->con[con].is_row)
3203
315k
    return tab->con[con].index < tab->n_dead;
3204
250k
3205
250k
  row = tab->con[con].index;
3206
250k
3207
250k
  off = 2 + tab->M;
3208
250k
  return isl_int_is_zero(tab->mat->row[row][1]) &&
3209
250k
    
!row_is_big(tab, row)18.6k
&&
3210
250k
    isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
3211
18.6k
          tab->n_col - tab->n_dead) == -1;
3212
1.17M
}
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
174k
{
3228
174k
  int r;
3229
174k
  enum isl_lp_result res = isl_lp_ok;
3230
174k
  struct isl_tab_var *var;
3231
174k
  struct isl_tab_undo *snap;
3232
174k
3233
174k
  if (!tab)
3234
0
    return isl_lp_error;
3235
174k
3236
174k
  if (tab->empty)
3237
15
    return isl_lp_empty;
3238
174k
3239
174k
  snap = isl_tab_snap(tab);
3240
174k
  r = isl_tab_add_row(tab, f);
3241
174k
  if (r < 0)
3242
0
    return isl_lp_error;
3243
174k
  var = &tab->con[r];
3244
379k
  for (;;) {
3245
379k
    int row, col;
3246
379k
    find_pivot(tab, var, var, -1, &row, &col);
3247
379k
    if (row == var->index) {
3248
4.14k
      res = isl_lp_unbounded;
3249
4.14k
      break;
3250
4.14k
    }
3251
375k
    if (row == -1)
3252
170k
      break;
3253
205k
    if (isl_tab_pivot(tab, row, col) < 0)
3254
0
      return isl_lp_error;
3255
174k
  }
3256
174k
  isl_int_mul(tab->mat->row[var->index][0],
3257
174k
        tab->mat->row[var->index][0], denom);
3258
174k
  if (ISL_FL_ISSET(flags, ISL_TAB_SAVE_DUAL)) {
3259
20.8k
    int i;
3260
20.8k
3261
20.8k
    isl_vec_free(tab->dual);
3262
20.8k
    tab->dual = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_con);
3263
20.8k
    if (!tab->dual)
3264
0
      return isl_lp_error;
3265
20.8k
    isl_int_set(tab->dual->el[0], tab->mat->row[var->index][0]);
3266
852k
    for (i = 0; i < tab->n_con; 
++i831k
) {
3267
831k
      int pos;
3268
831k
      if (tab->con[i].is_row) {
3269
508k
        isl_int_set_si(tab->dual->el[1 + i], 0);
3270
508k
        continue;
3271
508k
      }
3272
322k
      pos = 2 + tab->M + tab->con[i].index;
3273
322k
      if (tab->con[i].negated)
3274
322k
        
isl_int_neg58.8k
(tab->dual->el[1 + i],
3275
322k
              tab->mat->row[var->index][pos]);
3276
322k
      else
3277
322k
        
isl_int_set263k
(tab->dual->el[1 + i],
3278
831k
              tab->mat->row[var->index][pos]);
3279
831k
    }
3280
20.8k
  }
3281
174k
  if (opt && 
res == isl_lp_ok174k
) {
3282
170k
    if (opt_denom) {
3283
28.0k
      isl_int_set(*opt, tab->mat->row[var->index][1]);
3284
28.0k
      isl_int_set(*opt_denom, tab->mat->row[var->index][0]);
3285
28.0k
    } else
3286
142k
      get_rounded_sample_value(tab, var, 1, opt);
3287
170k
  }
3288
174k
  if (isl_tab_rollback(tab, snap) < 0)
3289
0
    return isl_lp_error;
3290
174k
  return res;
3291
174k
}
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
739k
{
3303
739k
  if (!tab)
3304
0
    return -1;
3305
739k
  if (con < 0 || con >= tab->n_con)
3306
739k
    
isl_die0
(isl_tab_get_ctx(tab), isl_error_invalid,
3307
739k
      "position out of bounds", return -1);
3308
739k
  if (tab->con[con].is_zero)
3309
19
    return 0;
3310
739k
  if (tab->con[con].is_redundant)
3311
137k
    return 1;
3312
601k
  return tab->con[con].is_row && 
tab->con[con].index < tab->n_redundant260k
;
3313
739k
}
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
1.55k
{
3326
1.55k
  unsigned off = 2 + tab->M;
3327
1.55k
  isl_mat *mat = tab->mat;
3328
1.55k
  int n;
3329
1.55k
  int row;
3330
1.55k
  int pos;
3331
1.55k
3332
1.55k
  if (!var->is_row)
3333
900
    return isl_bool_false;
3334
653
  row = var->index;
3335
653
  if (row_is_big(tab, row))
3336
0
    return isl_bool_false;
3337
653
  n = tab->n_col - tab->n_dead;
3338
653
  pos = isl_seq_first_non_zero(mat->row[row] + off + tab->n_dead, n);
3339
653
  if (pos != -1)
3340
556
    return isl_bool_false;
3341
97
  if (value)
3342
97
    
isl_int_divexact0
(*value, mat->row[row][1], mat->row[row][0]);
3343
1.55k
  return isl_bool_true;
3344
1.55k
}
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
1.83k
{
3359
1.83k
  if (row_is_big(tab, var->index))
3360
0
    return 1;
3361
1.83k
  isl_int_mul(*tmp, tab->mat->row[var->index][0], target);
3362
1.83k
  if (sgn > 0)
3363
747
    return isl_int_ge(tab->mat->row[var->index][1], *tmp);
3364
1.83k
  else
3365
1.83k
    
return 1.08k
isl_int_le1.08k
(tab->mat->row[var->index][1], *tmp);
3366
1.83k
}
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
1.86k
{
3387
1.86k
  int row, col;
3388
1.86k
3389
1.86k
  if (sgn < 0 && 
min_is_manifestly_unbounded(tab, var)1.45k
)
3390
661
    return isl_bool_true;
3391
1.20k
  if (sgn > 0 && 
max_is_manifestly_unbounded(tab, var)409
)
3392
0
    return isl_bool_true;
3393
1.20k
  if (to_row(tab, var, sgn) < 0)
3394
0
    return isl_bool_error;
3395
1.83k
  
while (1.20k
!reached(tab, var, sgn, target, tmp)) {
3396
1.65k
    find_pivot(tab, var, var, sgn, &row, &col);
3397
1.65k
    if (row == -1)
3398
432
      return isl_bool_false;
3399
1.22k
    if (row == var->index)
3400
594
      return isl_bool_true;
3401
632
    if (isl_tab_pivot(tab, row, col) < 0)
3402
0
      return isl_bool_error;
3403
1.65k
  }
3404
1.20k
3405
1.20k
  
return isl_bool_true178
;
3406
1.86k
}
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
1.45k
{
3430
1.45k
  isl_bool reached;
3431
1.45k
  isl_vec *eq;
3432
1.45k
  int pos;
3433
1.45k
  isl_stat r;
3434
1.45k
3435
1.45k
  get_rounded_sample_value(tab, var, -1, target);
3436
1.45k
  isl_int_sub_ui(*target, *target, 1);
3437
1.45k
  reached = var_reaches(tab, var, -1, *target, tmp);
3438
1.45k
  if (reached < 0 || reached)
3439
1.04k
    return isl_bool_not(reached);
3440
409
  get_rounded_sample_value(tab, var, 1, target);
3441
409
  isl_int_add_ui(*target, *target, 1);
3442
409
  reached = var_reaches(tab, var, 1, *target, tmp);
3443
409
  if (reached < 0 || reached)
3444
386
    return isl_bool_not(reached);
3445
23
  get_rounded_sample_value(tab, var, -1, tmp);
3446
23
  isl_int_sub_ui(*target, *target, 1);
3447
23
  if (isl_int_ne(*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
23
3453
23
  if (isl_tab_extend_cons(tab, 1) < 0)
3454
0
    return isl_bool_error;
3455
23
  eq = isl_vec_alloc(isl_tab_get_ctx(tab), 1 + tab->n_var);
3456
23
  if (!eq)
3457
0
    return isl_bool_error;
3458
23
  pos = var - tab->var;
3459
23
  isl_seq_clr(eq->el + 1, tab->n_var);
3460
23
  isl_int_set_si(eq->el[1 + pos], -1);
3461
23
  isl_int_set(eq->el[0], *target);
3462
23
  r = isl_tab_add_eq(tab, eq->el);
3463
23
  isl_vec_free(eq);
3464
23
3465
23
  return r < 0 ? 
isl_bool_error0
: isl_bool_true;
3466
1.45k
}
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
1.55k
{
3484
1.55k
  isl_int target, tmp;
3485
1.55k
  isl_bool is_cst;
3486
1.55k
3487
1.55k
  if (var->is_row && 
row_is_big(tab, var->index)653
)
3488
0
    return isl_bool_false;
3489
1.55k
  is_cst = is_constant(tab, var, value);
3490
1.55k
  if (is_cst < 0 || is_cst)
3491
97
    return is_cst;
3492
1.45k
3493
1.45k
  if (!value)
3494
1.45k
    
isl_int_init507
(target);
3495
1.45k
  isl_int_init(tmp);
3496
1.45k
3497
1.45k
  is_cst = detect_constant_with_tmp(tab, var,
3498
1.45k
              value ? 
value949
:
&target507
, &tmp);
3499
1.45k
3500
1.45k
  isl_int_clear(tmp);
3501
1.45k
  if (!value)
3502
1.45k
    
isl_int_clear507
(target);
3503
1.55k
3504
1.55k
  return is_cst;
3505
1.55k
}
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
949
{
3516
949
  if (!tab)
3517
0
    return isl_bool_error;
3518
949
  if (var < 0 || var >= tab->n_var)
3519
949
    
isl_die0
(isl_tab_get_ctx(tab), isl_error_invalid,
3520
949
      "position out of bounds", return isl_bool_error);
3521
949
  if (tab->rational)
3522
0
    return isl_bool_false;
3523
949
3524
949
  return get_constant(tab, &tab->var[var], value);
3525
949
}
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
139
{
3535
139
  int i;
3536
139
3537
139
  if (!tab)
3538
0
    return isl_stat_error;
3539
139
  if (tab->rational)
3540
0
    return isl_stat_ok;
3541
139
3542
743
  
for (i = 0; 139
i < tab->n_var;
++i604
) {
3543
604
    if (get_constant(tab, &tab->var[i], NULL) < 0)
3544
0
      return isl_stat_error;
3545
604
  }
3546
139
3547
139
  return isl_stat_ok;
3548
139
}
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
436k
{
3555
436k
  if (!tab)
3556
0
    return NULL;
3557
436k
  tab->need_undo = 1;
3558
436k
  return tab->top;
3559
436k
}
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
47
{
3566
47
  if (!tab)
3567
0
    return isl_bool_error;
3568
47
3569
47
  return tab->need_undo;
3570
47
}
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
47
{
3579
47
  if (!tab)
3580
0
    return;
3581
47
3582
47
  free_undo(tab);
3583
47
  tab->need_undo = 0;
3584
47
}
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
406
{
3592
406
  unsigned off = 2 + tab->M;
3593
406
3594
406
  if (!var->is_row && !max_is_manifestly_unbounded(tab, var))
3595
17
    if (to_row(tab, var, 1) < 0)
3596
0
      return isl_stat_error;
3597
406
3598
406
  if (var->is_row) {
3599
17
    isl_int_sub(tab->mat->row[var->index][1],
3600
17
        tab->mat->row[var->index][1], tab->mat->row[var->index][0]);
3601
17
    if (var->is_nonneg) {
3602
17
      int sgn = restore_row(tab, var);
3603
17
      isl_assert(tab->mat->ctx, sgn >= 0,
3604
17
        return isl_stat_error);
3605
17
    }
3606
389
  } else {
3607
389
    int i;
3608
389
3609
2.28k
    for (i = 0; i < tab->n_row; 
++i1.89k
) {
3610
1.89k
      if (isl_int_is_zero(tab->mat->row[i][off + var->index]))
3611
1.89k
        
continue1.47k
;
3612
420
      isl_int_add(tab->mat->row[i][1], tab->mat->row[i][1],
3613
420
          tab->mat->row[i][off + var->index]);
3614
420
    }
3615
389
3616
389
  }
3617
406
3618
406
  return isl_stat_ok;
3619
406
}
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
135
{
3628
135
  var->is_nonneg = 1;
3629
135
3630
135
  if (var->is_row && 
restore_row(tab, var) < -1112
)
3631
0
    return isl_stat_error;
3632
135
3633
135
  return isl_stat_ok;
3634
135
}
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
335k
{
3645
335k
  struct isl_tab_var *var;
3646
335k
3647
335k
  if (tab->n_redundant < 1)
3648
335k
    
isl_die0
(isl_tab_get_ctx(tab), isl_error_internal,
3649
335k
      "no redundant rows", return isl_stat_error);
3650
335k
3651
335k
  var = isl_tab_var_from_row(tab, tab->n_redundant - 1);
3652
335k
  var->is_redundant = 0;
3653
335k
  tab->n_redundant--;
3654
335k
  restore_row(tab, var);
3655
335k
3656
335k
  return isl_stat_ok;
3657
335k
}
3658
3659
static isl_stat perform_undo_var(struct isl_tab *tab, struct isl_tab_undo *undo)
3660
  WARN_UNUSED;
3661
static isl_stat perform_undo_var(struct isl_tab *tab, struct isl_tab_undo *undo)
3662
1.10M
{
3663
1.10M
  struct isl_tab_var *var = var_from_index(tab, undo->u.var_index);
3664
1.10M
  switch (undo->type) {
3665
1.10M
  case isl_tab_undo_nonneg:
3666
228k
    var->is_nonneg = 0;
3667
228k
    break;
3668
1.10M
  case isl_tab_undo_redundant:
3669
281k
    if (!var->is_row || var->index != tab->n_redundant - 1)
3670
281k
      
isl_die0
(isl_tab_get_ctx(tab), isl_error_internal,
3671
281k
        "not undoing last redundant row", return -1);
3672
281k
    return restore_last_redundant(tab);
3673
281k
  case isl_tab_undo_freeze:
3674
98.2k
    var->frozen = 0;
3675
98.2k
    break;
3676
281k
  case isl_tab_undo_zero:
3677
57.7k
    var->is_zero = 0;
3678
57.7k
    if (!var->is_row)
3679
57.6k
      tab->n_dead--;
3680
57.7k
    break;
3681
435k
  case isl_tab_undo_allocate:
3682
435k
    if (undo->u.var_index >= 0) {
3683
1.27k
      isl_assert(tab->mat->ctx, !var->is_row,
3684
1.27k
        return isl_stat_error);
3685
1.27k
      return drop_col(tab, var->index);
3686
433k
    }
3687
433k
    if (!var->is_row) {
3688
71.6k
      if (!max_is_manifestly_unbounded(tab, var)) {
3689
60.1k
        if (to_row(tab, var, 1) < 0)
3690
0
          return isl_stat_error;
3691
11.4k
      } else if (!min_is_manifestly_unbounded(tab, var)) {
3692
4.89k
        if (to_row(tab, var, -1) < 0)
3693
0
          return isl_stat_error;
3694
6.53k
      } else
3695
6.53k
        if (to_row(tab, var, 0) < 0)
3696
0
          return isl_stat_error;
3697
433k
    }
3698
433k
    return drop_row(tab, var->index);
3699
433k
  case isl_tab_undo_relax:
3700
406
    return unrelax(tab, var);
3701
433k
  case isl_tab_undo_unrestrict:
3702
135
    return ununrestrict(tab, var);
3703
433k
  default:
3704
0
    isl_die(tab->mat->ctx, isl_error_internal,
3705
1.10M
      "perform_undo_var called on invalid undo record",
3706
1.10M
      return isl_stat_error);
3707
1.10M
  }
3708
1.10M
3709
1.10M
  
return isl_stat_ok384k
;
3710
1.10M
}
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
46.8k
{
3721
46.8k
  if (!tab)
3722
0
    return isl_stat_error;
3723
46.8k
3724
46.8k
  if (tab->need_undo)
3725
46.8k
    
isl_die0
(isl_tab_get_ctx(tab), isl_error_invalid,
3726
46.8k
      "manually restoring redundant constraints "
3727
46.8k
      "interferes with undo history",
3728
46.8k
      return isl_stat_error);
3729
46.8k
3730
100k
  
while (46.8k
tab->n_redundant > 0) {
3731
53.5k
    if (tab->row_var[tab->n_redundant - 1] >= 0) {
3732
48.6k
      struct isl_tab_var *var;
3733
48.6k
3734
48.6k
      var = isl_tab_var_from_row(tab, tab->n_redundant - 1);
3735
48.6k
      var->is_nonneg = 0;
3736
48.6k
    }
3737
53.5k
    restore_last_redundant(tab);
3738
53.5k
  }
3739
46.8k
  return isl_stat_ok;
3740
46.8k
}
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
556
{
3747
556
  int off;
3748
556
3749
556
  off = tab->n_var - isl_basic_map_dim(tab->bmap, isl_dim_div);
3750
556
  if (isl_basic_map_drop_div(tab->bmap, pos - off) < 0)
3751
0
    return isl_stat_error;
3752
556
  if (tab->samples) {
3753
216
    tab->samples = isl_mat_drop_cols(tab->samples, 1 + pos, 1);
3754
216
    if (!tab->samples)
3755
0
      return isl_stat_error;
3756
556
  }
3757
556
3758
556
  return isl_stat_ok;
3759
556
}
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
32
{
3774
32
  int i, j;
3775
32
  int n_extra = 0;
3776
32
  int *extra = NULL;  /* current columns that contain bad stuff */
3777
32
  unsigned off = 2 + tab->M;
3778
32
3779
32
  extra = isl_alloc_array(tab->mat->ctx, int, tab->n_col);
3780
32
  if (tab->n_col && !extra)
3781
0
    goto error;
3782
312
  
for (i = 0; 32
i < tab->n_col;
++i280
) {
3783
1.79k
    for (j = 0; j < tab->n_col; 
++j1.51k
)
3784
1.72k
      if (tab->col_var[i] == col_var[j])
3785
215
        break;
3786
280
    if (j < tab->n_col)
3787
215
      continue;
3788
65
    extra[n_extra++] = i;
3789
65
  }
3790
240
  for (i = 0; i < tab->n_col && 
n_extra > 0237
;
++i208
) {
3791
208
    struct isl_tab_var *var;
3792
208
    int row;
3793
208
3794
1.15k
    for (j = 0; j < tab->n_col; 
++j950
)
3795
1.09k
      if (col_var[i] == tab->col_var[j])
3796
143
        break;
3797
208
    if (j < tab->n_col)
3798
143
      continue;
3799
65
    var = var_from_index(tab, col_var[i]);
3800
65
    row = var->index;
3801
67
    for (j = 0; j < n_extra; 
++j2
)
3802
67
      if (!isl_int_is_zero(tab->mat->row[row][off+extra[j]]))
3803
67
        
break65
;
3804
65
    isl_assert(tab->mat->ctx, j < n_extra, goto error);
3805
65
    if (isl_tab_pivot(tab, row, extra[j]) < 0)
3806
0
      goto error;
3807
65
    extra[j] = extra[--n_extra];
3808
65
  }
3809
32
3810
32
  free(extra);
3811
32
  return 0;
3812
0
error:
3813
0
  free(extra);
3814
0
  return -1;
3815
32
}
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
6.79k
{
3823
6.79k
  int i;
3824
6.79k
3825
8.44k
  for (i = tab->n_sample - 1; i >= 0 && 
tab->n_sample > n7.95k
;
--i1.65k
) {
3826
1.65k
    if (tab->sample_index[i] < n)
3827
575
      continue;
3828
1.07k
3829
1.07k
    if (i != tab->n_sample - 1) {
3830
651
      int t = tab->sample_index[tab->n_sample-1];
3831
651
      tab->sample_index[tab->n_sample-1] = tab->sample_index[i];
3832
651
      tab->sample_index[i] = t;
3833
651
      isl_mat_swap_rows(tab->samples, tab->n_sample-1, i);
3834
651
    }
3835
1.65k
    tab->n_sample--;
3836
1.65k
  }
3837
6.79k
}
3838
3839
static isl_stat perform_undo(struct isl_tab *tab, struct isl_tab_undo *undo)
3840
  WARN_UNUSED;
3841
static isl_stat perform_undo(struct isl_tab *tab, struct isl_tab_undo *undo)
3842
1.23M
{
3843
1.23M
  switch (undo->type) {
3844
1.23M
  case isl_tab_undo_rational:
3845
3.29k
    tab->rational = 0;
3846
3.29k
    break;
3847
1.23M
  case isl_tab_undo_empty:
3848
22.2k
    tab->empty = 0;
3849
22.2k
    break;
3850
1.23M
  case isl_tab_undo_nonneg:
3851
1.10M
  case isl_tab_undo_redundant:
3852
1.10M
  case isl_tab_undo_freeze:
3853
1.10M
  case isl_tab_undo_zero:
3854
1.10M
  case isl_tab_undo_allocate:
3855
1.10M
  case isl_tab_undo_relax:
3856
1.10M
  case isl_tab_undo_unrestrict:
3857
1.10M
    return perform_undo_var(tab, undo);
3858
1.10M
  case isl_tab_undo_bmap_eq:
3859
0
    return isl_basic_map_free_equality(tab->bmap, 1);
3860
1.10M
  case isl_tab_undo_bmap_ineq:
3861
97.8k
    return isl_basic_map_free_inequality(tab->bmap, 1);
3862
1.10M
  case isl_tab_undo_bmap_div:
3863
556
    return drop_bmap_div(tab, undo->u.var_index);
3864
1.10M
  case isl_tab_undo_saved_basis:
3865
32
    if (restore_basis(tab, undo->u.col_var) < 0)
3866
0
      return isl_stat_error;
3867
32
    break;
3868
1.93k
  case isl_tab_undo_drop_sample:
3869
1.93k
    tab->n_outside--;
3870
1.93k
    break;
3871
6.79k
  case isl_tab_undo_saved_samples:
3872
6.79k
    drop_samples_since(tab, undo->u.n);
3873
6.79k
    break;
3874
734
  case isl_tab_undo_callback:
3875
734
    return undo->u.callback->run(undo->u.callback);
3876
32
  default:
3877
0
    isl_assert(tab->mat->ctx, 0, return isl_stat_error);
3878
1.23M
  }
3879
1.23M
  
return isl_stat_ok34.3k
;
3880
1.23M
}
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
392k
{
3887
392k
  struct isl_tab_undo *undo, *next;
3888
392k
3889
392k
  if (!tab)
3890
0
    return -1;
3891
392k
3892
392k
  tab->in_undo = 1;
3893
1.62M
  for (undo = tab->top; undo && undo != &tab->bottom; 
undo = next1.23M
) {
3894
1.39M
    next = undo->next;
3895
1.39M
    if (undo == snap)
3896
156k
      break;
3897
1.23M
    if (perform_undo(tab, undo) < 0) {
3898
0
      tab->top = undo;
3899
0
      free_undo(tab);
3900
0
      tab->in_undo = 0;
3901
0
      return -1;
3902
0
    }
3903
1.23M
    free_undo_record(undo);
3904
1.23M
  }
3905
392k
  tab->in_undo = 0;
3906
392k
  tab->top = undo;
3907
392k
  if (!undo)
3908
0
    return -1;
3909
392k
  return 0;
3910
392k
}
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
14.9k
{
3924
14.9k
  int pos;
3925
14.9k
  unsigned off = 2 + tab->M;
3926
14.9k
3927
14.9k
  if (tab->rational)
3928
2
    return isl_ineq_separate;
3929
14.9k
3930
14.9k
  if (!isl_int_is_one(tab->mat->row[row][0]))
3931
14.9k
    
return isl_ineq_separate33
;
3932
14.9k
3933
14.9k
  pos = isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
3934
14.9k
          tab->n_col - tab->n_dead);
3935
14.9k
  if (pos == -1) {
3936
5.43k
    if (isl_int_is_negone(tab->mat->row[row][1]))
3937
5.43k
      
return isl_ineq_adj_eq4.78k
;
3938
652
    else
3939
652
      return isl_ineq_separate;
3940
9.48k
  }
3941
9.48k
3942
9.48k
  if (!isl_int_eq(tab->mat->row[row][1],
3943
9.48k
      tab->mat->row[row][off + tab->n_dead + pos]))
3944
9.48k
    
return isl_ineq_separate5.47k
;
3945
4.01k
3946
4.01k
  pos = isl_seq_first_non_zero(
3947
4.01k
      tab->mat->row[row] + off + tab->n_dead + pos + 1,
3948
4.01k
      tab->n_col - tab->n_dead - pos - 1);
3949
4.01k
3950
4.01k
  return pos == -1 ? 
isl_ineq_adj_ineq3.81k
:
isl_ineq_separate194
;
3951
14.9k
}
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
102k
{
3963
102k
  enum isl_ineq_type type = isl_ineq_error;
3964
102k
  struct isl_tab_undo *snap = NULL;
3965
102k
  int con;
3966
102k
  int row;
3967
102k
3968
102k
  if (!tab)
3969
0
    return isl_ineq_error;
3970
102k
3971
102k
  if (isl_tab_extend_cons(tab, 1) < 0)
3972
0
    return isl_ineq_error;
3973
102k
3974
102k
  snap = isl_tab_snap(tab);
3975
102k
3976
102k
  con = isl_tab_add_row(tab, ineq);
3977
102k
  if (con < 0)
3978
0
    goto error;
3979
102k
3980
102k
  row = tab->con[con].index;
3981
102k
  if (isl_tab_row_is_redundant(tab, row))
3982
0
    type = isl_ineq_redundant;
3983
102k
  else if (isl_int_is_neg(tab->mat->row[row][1]) &&
3984
102k
     
(31.6k
tab->rational31.6k
||
3985
31.6k
        
isl_int_abs_ge28.7k
(tab->mat->row[row][1],
3986
102k
           tab->mat->row[row][0]))) {
3987
31.4k
    int nonneg = at_least_zero(tab, &tab->con[con]);
3988
31.4k
    if (nonneg < 0)
3989
0
      goto error;
3990
31.4k
    if (nonneg)
3991
16.5k
      type = isl_ineq_cut;
3992
14.9k
    else
3993
14.9k
      type = separation_type(tab, row);
3994
71.2k
  } else {
3995
71.2k
    int red = con_is_redundant(tab, &tab->con[con]);
3996
71.2k
    if (red < 0)
3997
0
      goto error;
3998
71.2k
    if (!red)
3999
18.9k
      type = isl_ineq_cut;
4000
52.2k
    else
4001
52.2k
      type = isl_ineq_redundant;
4002
102k
  }
4003
102k
4004
102k
  if (isl_tab_rollback(tab, snap))
4005
0
    return isl_ineq_error;
4006
102k
  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
89.3k
{
4013
89.3k
  bmap = isl_basic_map_cow(bmap);
4014
89.3k
  if (!tab || !bmap)
4015
0
    goto error;
4016
89.3k
4017
89.3k
  if (tab->empty) {
4018
2.69k
    bmap = isl_basic_map_set_to_empty(bmap);
4019
2.69k
    if (!bmap)
4020
0
      goto error;
4021
2.69k
    tab->bmap = bmap;
4022
2.69k
    return isl_stat_ok;
4023
2.69k
  }
4024
86.6k
4025
86.6k
  isl_assert(tab->mat->ctx, tab->n_eq == bmap->n_eq, goto error);
4026
86.6k
  isl_assert(tab->mat->ctx,
4027
86.6k
        tab->n_con == bmap->n_eq + bmap->n_ineq, goto error);
4028
86.6k
4029
86.6k
  tab->bmap = bmap;
4030
86.6k
4031
86.6k
  return isl_stat_ok;
4032
0
error:
4033
0
  isl_basic_map_free(bmap);
4034
0
  return isl_stat_error;
4035
89.3k
}
4036
4037
isl_stat isl_tab_track_bset(struct isl_tab *tab, __isl_take isl_basic_set *bset)
4038
472
{
4039
472
  return isl_tab_track_bmap(tab, bset_to_bmap(bset));
4040
472
}
4041
4042
__isl_keep isl_basic_set *isl_tab_peek_bset(struct isl_tab *tab)
4043
10.8k
{
4044
10.8k
  if (!tab)
4045
0
    return NULL;
4046
10.8k
4047
10.8k
  return bset_from_bmap(tab->bmap);
4048
10.8k
}
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 (!tab) {
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_var; ++i) {
4069
0
    if (i)
4070
0
      fprintf(out, (i == tab->n_param ||
4071
0
              i == tab->n_var - tab->n_div) ? "; "
4072
0
                    : ", ");
4073
0
    fprintf(out, "%c%d%s", tab->var[i].is_row ? 'r' : 'c',
4074
0
          tab->var[i].index,
4075
0
          tab->var[i].is_zero ? " [=0]" :
4076
0
          tab->var[i].is_redundant ? " [R]" : "");
4077
0
  }
4078
0
  fprintf(out, "]\n");
4079
0
  fprintf(out, "%*s[", indent, "");
4080
0
  for (i = 0; i < tab->n_con; ++i) {
4081
0
    if (i)
4082
0
      fprintf(out, ", ");
4083
0
    fprintf(out, "%c%d%s", tab->con[i].is_row ? 'r' : 'c',
4084
0
          tab->con[i].index,
4085
0
          tab->con[i].is_zero ? " [=0]" :
4086
0
          tab->con[i].is_redundant ? " [R]" : "");
4087
0
  }
4088
0
  fprintf(out, "]\n");
4089
0
  fprintf(out, "%*s[", indent, "");
4090
0
  for (i = 0; i < tab->n_row; ++i) {
4091
0
    const char *sign = "";
4092
0
    if (i)
4093
0
      fprintf(out, ", ");
4094
0
    if (tab->row_sign) {
4095
0
      if (tab->row_sign[i] == isl_tab_row_unknown)
4096
0
        sign = "?";
4097
0
      else if (tab->row_sign[i] == isl_tab_row_neg)
4098
0
        sign = "-";
4099
0
      else if (tab->row_sign[i] == isl_tab_row_pos)
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]" : "", sign);
4106
0
  }
4107
0
  fprintf(out, "]\n");
4108
0
  fprintf(out, "%*s[", indent, "");
4109
0
  for (i = 0; i < tab->n_col; ++i) {
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]" : "");
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
}