Coverage Report

Created: 2017-06-23 12:40

/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/llvm/tools/polly/lib/External/isl/isl_map_simplify.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2008-2009 Katholieke Universiteit Leuven
3
 * Copyright 2012-2013 Ecole Normale Superieure
4
 * Copyright 2014-2015 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_map_private.h>
18
#include "isl_equalities.h"
19
#include <isl/map.h>
20
#include <isl_seq.h>
21
#include "isl_tab.h"
22
#include <isl_space_private.h>
23
#include <isl_mat_private.h>
24
#include <isl_vec_private.h>
25
26
#include <bset_to_bmap.c>
27
#include <bset_from_bmap.c>
28
#include <set_to_map.c>
29
#include <set_from_map.c>
30
31
static void swap_equality(struct isl_basic_map *bmap, int a, int b)
32
526k
{
33
526k
  isl_int *t = bmap->eq[a];
34
526k
  bmap->eq[a] = bmap->eq[b];
35
526k
  bmap->eq[b] = t;
36
526k
}
37
38
static void swap_inequality(struct isl_basic_map *bmap, int a, int b)
39
50.4k
{
40
50.4k
  if (
a != b50.4k
)
{50.4k
41
50.4k
    isl_int *t = bmap->ineq[a];
42
50.4k
    bmap->ineq[a] = bmap->ineq[b];
43
50.4k
    bmap->ineq[b] = t;
44
50.4k
  }
45
50.4k
}
46
47
__isl_give isl_basic_map *isl_basic_map_normalize_constraints(
48
  __isl_take isl_basic_map *bmap)
49
2.12M
{
50
2.12M
  int i;
51
2.12M
  isl_int gcd;
52
2.12M
  unsigned total = isl_basic_map_total_dim(bmap);
53
2.12M
54
2.12M
  if (!bmap)
55
0
    return NULL;
56
2.12M
57
2.12M
  
isl_int_init2.12M
(gcd);2.12M
58
4.26M
  for (i = bmap->n_eq - 1; 
i >= 04.26M
;
--i2.13M
)
{2.14M
59
2.14M
    isl_seq_gcd(bmap->eq[i]+1, total, &gcd);
60
2.14M
    if (
isl_int_is_zero2.14M
(gcd))
{183k
61
183k
      if (
!183k
isl_int_is_zero183k
(bmap->eq[i][0]))
{2.11k
62
2.11k
        bmap = isl_basic_map_set_to_empty(bmap);
63
2.11k
        break;
64
2.11k
      }
65
181k
      isl_basic_map_drop_equality(bmap, i);
66
181k
      continue;
67
183k
    }
68
1.95M
    
if (1.95M
ISL_F_ISSET1.95M
(bmap, ISL_BASIC_MAP_RATIONAL))
69
164k
      isl_int_gcd(gcd, gcd, bmap->eq[i][0]);
70
1.95M
    if (isl_int_is_one(gcd))
71
1.92M
      continue;
72
32.2k
    
if (32.2k
!32.2k
isl_int_is_divisible_by32.2k
(bmap->eq[i][0], gcd))
{1.51k
73
1.51k
      bmap = isl_basic_map_set_to_empty(bmap);
74
1.51k
      break;
75
1.51k
    }
76
30.7k
    isl_seq_scale_down(bmap->eq[i], bmap->eq[i], gcd, 1+total);
77
30.7k
  }
78
2.12M
79
9.81M
  for (i = bmap->n_ineq - 1; 
i >= 09.81M
;
--i7.69M
)
{7.71M
80
7.71M
    isl_seq_gcd(bmap->ineq[i]+1, total, &gcd);
81
7.71M
    if (
isl_int_is_zero7.71M
(gcd))
{330k
82
330k
      if (
isl_int_is_neg330k
(bmap->ineq[i][0]))
{23.1k
83
23.1k
        bmap = isl_basic_map_set_to_empty(bmap);
84
23.1k
        break;
85
23.1k
      }
86
306k
      isl_basic_map_drop_inequality(bmap, i);
87
306k
      continue;
88
330k
    }
89
7.38M
    
if (7.38M
ISL_F_ISSET7.38M
(bmap, ISL_BASIC_MAP_RATIONAL))
90
307k
      isl_int_gcd(gcd, gcd, bmap->ineq[i][0]);
91
7.38M
    if (isl_int_is_one(gcd))
92
7.27M
      continue;
93
107k
    
isl_int_fdiv_q107k
(bmap->ineq[i][0], bmap->ineq[i][0], gcd);107k
94
107k
    isl_seq_scale_down(bmap->ineq[i]+1, bmap->ineq[i]+1, gcd, total);
95
107k
  }
96
2.12M
  isl_int_clear(gcd);
97
2.12M
98
2.12M
  return bmap;
99
2.12M
}
100
101
struct isl_basic_set *isl_basic_set_normalize_constraints(
102
  struct isl_basic_set *bset)
103
140k
{
104
140k
  isl_basic_map *bmap = bset_to_bmap(bset);
105
140k
  return bset_from_bmap(isl_basic_map_normalize_constraints(bmap));
106
140k
}
107
108
/* Reduce the coefficient of the variable at position "pos"
109
 * in integer division "div", such that it lies in the half-open
110
 * interval (1/2,1/2], extracting any excess value from this integer division.
111
 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
112
 * corresponds to the constant term.
113
 *
114
 * That is, the integer division is of the form
115
 *
116
 *  floor((... + (c * d + r) * x_pos + ...)/d)
117
 *
118
 * with -d < 2 * r <= d.
119
 * Replace it by
120
 *
121
 *  floor((... + r * x_pos + ...)/d) + c * x_pos
122
 *
123
 * If 2 * ((c * d + r) % d) <= d, then c = floor((c * d + r)/d).
124
 * Otherwise, c = floor((c * d + r)/d) + 1.
125
 *
126
 * This is the same normalization that is performed by isl_aff_floor.
127
 */
128
static __isl_give isl_basic_map *reduce_coefficient_in_div(
129
  __isl_take isl_basic_map *bmap, int div, int pos)
130
10.1k
{
131
10.1k
  isl_int shift;
132
10.1k
  int add_one;
133
10.1k
134
10.1k
  isl_int_init(shift);
135
10.1k
  isl_int_fdiv_r(shift, bmap->div[div][1 + pos], bmap->div[div][0]);
136
10.1k
  isl_int_mul_ui(shift, shift, 2);
137
10.1k
  add_one = isl_int_gt(shift, bmap->div[div][0]);
138
10.1k
  isl_int_fdiv_q(shift, bmap->div[div][1 + pos], bmap->div[div][0]);
139
10.1k
  if (add_one)
140
3.88k
    isl_int_add_ui(shift, shift, 1);
141
10.1k
  isl_int_neg(shift, shift);
142
10.1k
  bmap = isl_basic_map_shift_div(bmap, div, pos, shift);
143
10.1k
  isl_int_clear(shift);
144
10.1k
145
10.1k
  return bmap;
146
10.1k
}
147
148
/* Does the coefficient of the variable at position "pos"
149
 * in integer division "div" need to be reduced?
150
 * That is, does it lie outside the half-open interval (1/2,1/2]?
151
 * The coefficient c/d lies outside this interval if abs(2 * c) >= d and
152
 * 2 * c != d.
153
 */
154
static isl_bool needs_reduction(__isl_keep isl_basic_map *bmap, int div,
155
  int pos)
156
733k
{
157
733k
  isl_bool r;
158
733k
159
733k
  if (isl_int_is_zero(bmap->div[div][1 + pos]))
160
539k
    return isl_bool_false;
161
733k
162
193k
  
isl_int_mul_ui193k
(bmap->div[div][1 + pos], bmap->div[div][1 + pos], 2);193k
163
193k
  r = isl_int_abs_ge(bmap->div[div][1 + pos], bmap->div[div][0]) &&
164
48.0k
      
!48.0k
isl_int_eq48.0k
(bmap->div[div][1 + pos], bmap->div[div][0]);
165
193k
  isl_int_divexact_ui(bmap->div[div][1 + pos],
166
193k
          bmap->div[div][1 + pos], 2);
167
193k
168
193k
  return r;
169
733k
}
170
171
/* Reduce the coefficients (including the constant term) of
172
 * integer division "div", if needed.
173
 * In particular, make sure all coefficients lie in
174
 * the half-open interval (1/2,1/2].
175
 */
176
static __isl_give isl_basic_map *reduce_div_coefficients_of_div(
177
  __isl_take isl_basic_map *bmap, int div)
178
100k
{
179
100k
  int i;
180
100k
  unsigned total = 1 + isl_basic_map_total_dim(bmap);
181
100k
182
833k
  for (i = 0; 
i < total833k
;
++i733k
)
{733k
183
733k
    isl_bool reduce;
184
733k
185
733k
    reduce = needs_reduction(bmap, div, i);
186
733k
    if (reduce < 0)
187
0
      return isl_basic_map_free(bmap);
188
733k
    
if (733k
!reduce733k
)
189
722k
      continue;
190
10.1k
    bmap = reduce_coefficient_in_div(bmap, div, i);
191
10.1k
    if (!bmap)
192
0
      break;
193
10.1k
  }
194
100k
195
100k
  return bmap;
196
100k
}
197
198
/* Reduce the coefficients (including the constant term) of
199
 * the known integer divisions, if needed
200
 * In particular, make sure all coefficients lie in
201
 * the half-open interval (1/2,1/2].
202
 */
203
static __isl_give isl_basic_map *reduce_div_coefficients(
204
  __isl_take isl_basic_map *bmap)
205
1.96M
{
206
1.96M
  int i;
207
1.96M
208
1.96M
  if (!bmap)
209
0
    return NULL;
210
1.96M
  
if (1.96M
bmap->n_div == 01.96M
)
211
1.76M
    return bmap;
212
1.96M
213
530k
  
for (i = 0; 194k
i < bmap->n_div530k
;
++i336k
)
{336k
214
336k
    if (isl_int_is_zero(bmap->div[i][0]))
215
236k
      continue;
216
100k
    bmap = reduce_div_coefficients_of_div(bmap, i);
217
100k
    if (!bmap)
218
0
      break;
219
100k
  }
220
194k
221
194k
  return bmap;
222
1.96M
}
223
224
/* Remove any common factor in numerator and denominator of the div expression,
225
 * not taking into account the constant term.
226
 * That is, if the div is of the form
227
 *
228
 *  floor((a + m f(x))/(m d))
229
 *
230
 * then replace it by
231
 *
232
 *  floor((floor(a/m) + f(x))/d)
233
 *
234
 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
235
 * and can therefore not influence the result of the floor.
236
 */
237
static void normalize_div_expression(__isl_keep isl_basic_map *bmap, int div)
238
347k
{
239
347k
  unsigned total = isl_basic_map_total_dim(bmap);
240
347k
  isl_ctx *ctx = bmap->ctx;
241
347k
242
347k
  if (isl_int_is_zero(bmap->div[div][0]))
243
236k
    return;
244
110k
  isl_seq_gcd(bmap->div[div] + 2, total, &ctx->normalize_gcd);
245
110k
  isl_int_gcd(ctx->normalize_gcd, ctx->normalize_gcd, bmap->div[div][0]);
246
110k
  if (isl_int_is_one(ctx->normalize_gcd))
247
108k
    return;
248
2.47k
  
isl_int_fdiv_q2.47k
(bmap->div[div][1], bmap->div[div][1],2.47k
249
2.47k
      ctx->normalize_gcd);
250
2.47k
  isl_int_divexact(bmap->div[div][0], bmap->div[div][0],
251
2.47k
      ctx->normalize_gcd);
252
2.47k
  isl_seq_scale_down(bmap->div[div] + 2, bmap->div[div] + 2,
253
2.47k
      ctx->normalize_gcd, total);
254
2.47k
}
255
256
/* Remove any common factor in numerator and denominator of a div expression,
257
 * not taking into account the constant term.
258
 * That is, look for any div of the form
259
 *
260
 *  floor((a + m f(x))/(m d))
261
 *
262
 * and replace it by
263
 *
264
 *  floor((floor(a/m) + f(x))/d)
265
 *
266
 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
267
 * and can therefore not influence the result of the floor.
268
 */
269
static __isl_give isl_basic_map *normalize_div_expressions(
270
  __isl_take isl_basic_map *bmap)
271
1.96M
{
272
1.96M
  int i;
273
1.96M
274
1.96M
  if (!bmap)
275
0
    return NULL;
276
1.96M
  
if (1.96M
bmap->n_div == 01.96M
)
277
1.76M
    return bmap;
278
1.96M
279
530k
  
for (i = 0; 194k
i < bmap->n_div530k
;
++i336k
)
280
336k
    normalize_div_expression(bmap, i);
281
194k
282
194k
  return bmap;
283
1.96M
}
284
285
/* Assumes divs have been ordered if keep_divs is set.
286
 */
287
static void eliminate_var_using_equality(struct isl_basic_map *bmap,
288
  unsigned pos, isl_int *eq, int keep_divs, int *progress)
289
2.94M
{
290
2.94M
  unsigned total;
291
2.94M
  unsigned space_total;
292
2.94M
  int k;
293
2.94M
  int last_div;
294
2.94M
295
2.94M
  total = isl_basic_map_total_dim(bmap);
296
2.94M
  space_total = isl_space_dim(bmap->dim, isl_dim_all);
297
2.94M
  last_div = isl_seq_last_non_zero(eq + 1 + space_total, bmap->n_div);
298
18.3M
  for (k = 0; 
k < bmap->n_eq18.3M
;
++k15.4M
)
{15.4M
299
15.4M
    if (bmap->eq[k] == eq)
300
2.93M
      continue;
301
12.4M
    
if (12.4M
isl_int_is_zero12.4M
(bmap->eq[k][1+pos]))
302
11.6M
      continue;
303
824k
    
if (824k
progress824k
)
304
166k
      *progress = 1;
305
824k
    isl_seq_elim(bmap->eq[k], eq, 1+pos, 1+total, NULL);
306
824k
    isl_seq_normalize(bmap->ctx, bmap->eq[k], 1 + total);
307
824k
  }
308
2.94M
309
13.4M
  for (k = 0; 
k < bmap->n_ineq13.4M
;
++k10.4M
)
{10.4M
310
10.4M
    if (isl_int_is_zero(bmap->ineq[k][1+pos]))
311
9.78M
      continue;
312
678k
    
if (678k
progress678k
)
313
257k
      *progress = 1;
314
678k
    isl_seq_elim(bmap->ineq[k], eq, 1+pos, 1+total, NULL);
315
678k
    isl_seq_normalize(bmap->ctx, bmap->ineq[k], 1 + total);
316
678k
    ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
317
678k
  }
318
2.94M
319
3.55M
  for (k = 0; 
k < bmap->n_div3.55M
;
++k612k
)
{612k
320
612k
    if (isl_int_is_zero(bmap->div[k][0]))
321
398k
      continue;
322
214k
    
if (214k
isl_int_is_zero214k
(bmap->div[k][1+1+pos]))
323
204k
      continue;
324
10.5k
    
if (10.5k
progress10.5k
)
325
5.06k
      *progress = 1;
326
10.5k
    /* We need to be careful about circular definitions,
327
10.5k
     * so for now we just remove the definition of div k
328
10.5k
     * if the equality contains any divs.
329
10.5k
     * If keep_divs is set, then the divs have been ordered
330
10.5k
     * and we can keep the definition as long as the result
331
10.5k
     * is still ordered.
332
10.5k
     */
333
10.5k
    if (
last_div == -1 || 10.5k
(keep_divs && 3.75k
last_div < k3.75k
))
{10.5k
334
10.5k
      isl_seq_elim(bmap->div[k]+1, eq,
335
10.5k
          1+pos, 1+total, &bmap->div[k][0]);
336
10.5k
      normalize_div_expression(bmap, k);
337
10.5k
    } else
338
0
      isl_seq_clr(bmap->div[k], 1 + total);
339
10.5k
    ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
340
10.5k
  }
341
2.94M
}
342
343
/* Assumes divs have been ordered if keep_divs is set.
344
 */
345
static __isl_give isl_basic_map *eliminate_div(__isl_take isl_basic_map *bmap,
346
  isl_int *eq, unsigned div, int keep_divs)
347
177k
{
348
177k
  unsigned pos = isl_space_dim(bmap->dim, isl_dim_all) + div;
349
177k
350
177k
  eliminate_var_using_equality(bmap, pos, eq, keep_divs, NULL);
351
177k
352
177k
  bmap = isl_basic_map_drop_div(bmap, div);
353
177k
354
177k
  return bmap;
355
177k
}
356
357
/* Check if elimination of div "div" using equality "eq" would not
358
 * result in a div depending on a later div.
359
 */
360
static isl_bool ok_to_eliminate_div(__isl_keep isl_basic_map *bmap, isl_int *eq,
361
  unsigned div)
362
176k
{
363
176k
  int k;
364
176k
  int last_div;
365
176k
  unsigned space_total = isl_space_dim(bmap->dim, isl_dim_all);
366
176k
  unsigned pos = space_total + div;
367
176k
368
176k
  last_div = isl_seq_last_non_zero(eq + 1 + space_total, bmap->n_div);
369
176k
  if (
last_div < 0 || 176k
last_div <= div176k
)
370
170k
    return isl_bool_true;
371
176k
372
18.0k
  
for (k = 0; 5.80k
k <= last_div18.0k
;
++k12.2k
)
{17.6k
373
17.6k
    if (isl_int_is_zero(bmap->div[k][0]))
374
6.57k
      continue;
375
11.0k
    
if (11.0k
!11.0k
isl_int_is_zero11.0k
(bmap->div[k][1 + 1 + pos]))
376
5.40k
      return isl_bool_false;
377
11.0k
  }
378
5.80k
379
399
  return isl_bool_true;
380
5.80k
}
381
382
/* Eliminate divs based on equalities
383
 */
384
static __isl_give isl_basic_map *eliminate_divs_eq(
385
  __isl_take isl_basic_map *bmap, int *progress)
386
2.06M
{
387
2.06M
  int d;
388
2.06M
  int i;
389
2.06M
  int modified = 0;
390
2.06M
  unsigned off;
391
2.06M
392
2.06M
  bmap = isl_basic_map_order_divs(bmap);
393
2.06M
394
2.06M
  if (!bmap)
395
0
    return NULL;
396
2.06M
397
2.06M
  off = 1 + isl_space_dim(bmap->dim, isl_dim_all);
398
2.06M
399
2.41M
  for (d = bmap->n_div - 1; 
d >= 02.41M
;
--d351k
)
{351k
400
724k
    for (i = 0; 
i < bmap->n_eq724k
;
++i372k
)
{544k
401
544k
      isl_bool ok;
402
544k
403
544k
      if (
!544k
isl_int_is_one544k
(bmap->eq[i][off + d]) &&
404
401k
          
!401k
isl_int_is_negone401k
(bmap->eq[i][off + d]))
405
367k
        continue;
406
176k
      ok = ok_to_eliminate_div(bmap, bmap->eq[i], d);
407
176k
      if (ok < 0)
408
0
        return isl_basic_map_free(bmap);
409
176k
      
if (176k
!ok176k
)
410
5.40k
        continue;
411
171k
      modified = 1;
412
171k
      *progress = 1;
413
171k
      bmap = eliminate_div(bmap, bmap->eq[i], d, 1);
414
171k
      if (isl_basic_map_drop_equality(bmap, i) < 0)
415
0
        return isl_basic_map_free(bmap);
416
171k
      break;
417
171k
    }
418
351k
  }
419
2.06M
  
if (2.06M
modified2.06M
)
420
97.3k
    return eliminate_divs_eq(bmap, progress);
421
1.96M
  return bmap;
422
2.06M
}
423
424
/* Eliminate divs based on inequalities
425
 */
426
static __isl_give isl_basic_map *eliminate_divs_ineq(
427
  __isl_take isl_basic_map *bmap, int *progress)
428
1.96M
{
429
1.96M
  int d;
430
1.96M
  int i;
431
1.96M
  unsigned off;
432
1.96M
  struct isl_ctx *ctx;
433
1.96M
434
1.96M
  if (!bmap)
435
0
    return NULL;
436
1.96M
437
1.96M
  ctx = bmap->ctx;
438
1.96M
  off = 1 + isl_space_dim(bmap->dim, isl_dim_all);
439
1.96M
440
2.12M
  for (d = bmap->n_div - 1; 
d >= 02.12M
;
--d159k
)
{159k
441
249k
    for (i = 0; 
i < bmap->n_eq249k
;
++i90.2k
)
442
136k
      
if (136k
!136k
isl_int_is_zero136k
(bmap->eq[i][off + d]))
443
46.0k
        break;
444
159k
    if (i < bmap->n_eq)
445
46.0k
      continue;
446
465k
    
for (i = 0; 113k
i < bmap->n_ineq465k
;
++i352k
)
447
410k
      
if (410k
isl_int_abs_gt410k
(bmap->ineq[i][off + d], ctx->one))
448
58.3k
        break;
449
113k
    if (i < bmap->n_ineq)
450
58.3k
      continue;
451
54.8k
    *progress = 1;
452
54.8k
    bmap = isl_basic_map_eliminate_vars(bmap, (off-1)+d, 1);
453
54.8k
    if (
!bmap || 54.8k
ISL_F_ISSET54.8k
(bmap, ISL_BASIC_MAP_EMPTY))
454
107
      break;
455
54.7k
    bmap = isl_basic_map_drop_div(bmap, d);
456
54.7k
    if (!bmap)
457
0
      break;
458
54.7k
  }
459
1.96M
  return bmap;
460
1.96M
}
461
462
/* Does the equality constraint at position "eq" in "bmap" involve
463
 * any local variables in the range [first, first + n)
464
 * that are not marked as having an explicit representation?
465
 */
466
static isl_bool bmap_eq_involves_unknown_divs(__isl_keep isl_basic_map *bmap,
467
  int eq, unsigned first, unsigned n)
468
2.73k
{
469
2.73k
  unsigned o_div;
470
2.73k
  int i;
471
2.73k
472
2.73k
  if (!bmap)
473
0
    return isl_bool_error;
474
2.73k
475
2.73k
  o_div = isl_basic_map_offset(bmap, isl_dim_div);
476
3.92k
  for (i = 0; 
i < n3.92k
;
++i1.19k
)
{1.76k
477
1.76k
    isl_bool unknown;
478
1.76k
479
1.76k
    if (isl_int_is_zero(bmap->eq[eq][o_div + first + i]))
480
1.08k
      continue;
481
681
    unknown = isl_basic_map_div_is_marked_unknown(bmap, first + i);
482
681
    if (unknown < 0)
483
0
      return isl_bool_error;
484
681
    
if (681
unknown681
)
485
571
      return isl_bool_true;
486
681
  }
487
2.73k
488
2.16k
  return isl_bool_false;
489
2.73k
}
490
491
/* The last local variable involved in the equality constraint
492
 * at position "eq" in "bmap" is the local variable at position "div".
493
 * It can therefore be used to extract an explicit representation
494
 * for that variable.
495
 * Do so unless the local variable already has an explicit representation or
496
 * the explicit representation would involve any other local variables
497
 * that in turn do not have an explicit representation.
498
 * An equality constraint involving local variables without an explicit
499
 * representation can be used in isl_basic_map_drop_redundant_divs
500
 * to separate out an independent local variable.  Introducing
501
 * an explicit representation here would block this transformation,
502
 * while the partial explicit representation in itself is not very useful.
503
 * Set *progress if anything is changed.
504
 *
505
 * The equality constraint is of the form
506
 *
507
 *  f(x) + n e >= 0
508
 *
509
 * with n a positive number.  The explicit representation derived from
510
 * this constraint is
511
 *
512
 *  floor((-f(x))/n)
513
 */
514
static __isl_give isl_basic_map *set_div_from_eq(__isl_take isl_basic_map *bmap,
515
  int div, int eq, int *progress)
516
54.3k
{
517
54.3k
  unsigned total, o_div;
518
54.3k
  isl_bool involves;
519
54.3k
520
54.3k
  if (!bmap)
521
0
    return NULL;
522
54.3k
523
54.3k
  
if (54.3k
!54.3k
isl_int_is_zero54.3k
(bmap->div[div][0]))
524
51.6k
    return bmap;
525
54.3k
526
2.73k
  involves = bmap_eq_involves_unknown_divs(bmap, eq, 0, div);
527
2.73k
  if (involves < 0)
528
0
    return isl_basic_map_free(bmap);
529
2.73k
  
if (2.73k
involves2.73k
)
530
571
    return bmap;
531
2.73k
532
2.16k
  total = isl_basic_map_dim(bmap, isl_dim_all);
533
2.16k
  o_div = isl_basic_map_offset(bmap, isl_dim_div);
534
2.16k
  isl_seq_neg(bmap->div[div] + 1, bmap->eq[eq], 1 + total);
535
2.16k
  isl_int_set_si(bmap->div[div][1 + o_div + div], 0);
536
2.16k
  isl_int_set(bmap->div[div][0], bmap->eq[eq][o_div + div]);
537
2.16k
  if (progress)
538
1.76k
    *progress = 1;
539
2.16k
  ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
540
2.16k
541
2.16k
  return bmap;
542
2.73k
}
543
544
__isl_give isl_basic_map *isl_basic_map_gauss(__isl_take isl_basic_map *bmap,
545
  int *progress)
546
3.33M
{
547
3.33M
  int k;
548
3.33M
  int done;
549
3.33M
  int last_var;
550
3.33M
  unsigned total_var;
551
3.33M
  unsigned total;
552
3.33M
553
3.33M
  bmap = isl_basic_map_order_divs(bmap);
554
3.33M
555
3.33M
  if (!bmap)
556
0
    return NULL;
557
3.33M
558
3.33M
  total = isl_basic_map_total_dim(bmap);
559
3.33M
  total_var = total - bmap->n_div;
560
3.33M
561
3.33M
  last_var = total - 1;
562
6.09M
  for (done = 0; 
done < bmap->n_eq6.09M
;
++done2.76M
)
{2.96M
563
6.01M
    for (; 
last_var >= 06.01M
;
--last_var3.04M
)
{5.80M
564
17.9M
      for (k = done; 
k < bmap->n_eq17.9M
;
++k12.1M
)
565
14.8M
        
if (14.8M
!14.8M
isl_int_is_zero14.8M
(bmap->eq[k][1+last_var]))
566
2.76M
          break;
567
5.80M
      if (k < bmap->n_eq)
568
2.76M
        break;
569
5.80M
    }
570
2.96M
    if (last_var < 0)
571
205k
      break;
572
2.76M
    
if (2.76M
k != done2.76M
)
573
526k
      swap_equality(bmap, k, done);
574
2.76M
    if (isl_int_is_neg(bmap->eq[done][1+last_var]))
575
291k
      isl_seq_neg(bmap->eq[done], bmap->eq[done], 1+total);
576
2.76M
577
2.76M
    eliminate_var_using_equality(bmap, last_var, bmap->eq[done], 1,
578
2.76M
            progress);
579
2.76M
580
2.76M
    if (last_var >= total_var)
581
54.3k
      bmap = set_div_from_eq(bmap, last_var - total_var,
582
54.3k
            done, progress);
583
2.76M
    if (!bmap)
584
0
      return NULL;
585
2.76M
  }
586
3.33M
  
if (3.33M
done == bmap->n_eq3.33M
)
587
3.12M
    return bmap;
588
584k
  
for (k = done; 205k
k < bmap->n_eq584k
;
++k379k
)
{425k
589
425k
    if (isl_int_is_zero(bmap->eq[k][0]))
590
379k
      continue;
591
45.2k
    return isl_basic_map_set_to_empty(bmap);
592
425k
  }
593
159k
  isl_basic_map_free_equality(bmap, bmap->n_eq-done);
594
159k
  return bmap;
595
205k
}
596
597
struct isl_basic_set *isl_basic_set_gauss(
598
  struct isl_basic_set *bset, int *progress)
599
105k
{
600
105k
  return bset_from_bmap(isl_basic_map_gauss(bset_to_bmap(bset),
601
105k
              progress));
602
105k
}
603
604
605
static unsigned int round_up(unsigned int v)
606
1.35M
{
607
1.35M
  int old_v = v;
608
1.35M
609
4.06M
  while (
v4.06M
)
{2.70M
610
2.70M
    old_v = v;
611
2.70M
    v ^= v & -v;
612
2.70M
  }
613
1.35M
  return old_v << 1;
614
1.35M
}
615
616
/* Hash table of inequalities in a basic map.
617
 * "index" is an array of addresses of inequalities in the basic map, some
618
 * of which are NULL.  The inequalities are hashed on the coefficients
619
 * except the constant term.
620
 * "size" is the number of elements in the array and is always a power of two
621
 * "bits" is the number of bits need to represent an index into the array.
622
 * "total" is the total dimension of the basic map.
623
 */
624
struct isl_constraint_index {
625
  unsigned int size;
626
  int bits;
627
  isl_int ***index;
628
  unsigned total;
629
};
630
631
/* Fill in the "ci" data structure for holding the inequalities of "bmap".
632
 */
633
static isl_stat create_constraint_index(struct isl_constraint_index *ci,
634
  __isl_keep isl_basic_map *bmap)
635
1.33M
{
636
1.33M
  isl_ctx *ctx;
637
1.33M
638
1.33M
  ci->index = NULL;
639
1.33M
  if (!bmap)
640
0
    return isl_stat_error;
641
1.33M
  ci->total = isl_basic_set_total_dim(bmap);
642
1.33M
  if (bmap->n_ineq == 0)
643
0
    return isl_stat_ok;
644
1.33M
  ci->size = round_up(4 * (bmap->n_ineq + 1) / 3 - 1);
645
1.33M
  ci->bits = ffs(ci->size) - 1;
646
1.33M
  ctx = isl_basic_map_get_ctx(bmap);
647
1.33M
  ci->index = isl_calloc_array(ctx, isl_int **, ci->size);
648
1.33M
  if (!ci->index)
649
0
    return isl_stat_error;
650
1.33M
651
1.33M
  return isl_stat_ok;
652
1.33M
}
653
654
/* Free the memory allocated by create_constraint_index.
655
 */
656
static void constraint_index_free(struct isl_constraint_index *ci)
657
1.33M
{
658
1.33M
  free(ci->index);
659
1.33M
}
660
661
/* Return the position in ci->index that contains the address of
662
 * an inequality that is equal to *ineq up to the constant term,
663
 * provided this address is not identical to "ineq".
664
 * If there is no such inequality, then return the position where
665
 * such an inequality should be inserted.
666
 */
667
static int hash_index_ineq(struct isl_constraint_index *ci, isl_int **ineq)
668
10.7M
{
669
10.7M
  int h;
670
10.7M
  uint32_t hash = isl_seq_get_hash_bits((*ineq) + 1, ci->total, ci->bits);
671
14.3M
  for (h = hash; 
ci->index[h]14.3M
;
h = (h+1) % ci->size3.67M
)
672
7.67M
    
if (7.67M
ineq != ci->index[h] &&7.67M
673
7.43M
        isl_seq_eq((*ineq) + 1, ci->index[h][0]+1, ci->total))
674
3.99M
      break;
675
10.7M
  return h;
676
10.7M
}
677
678
/* Return the position in ci->index that contains the address of
679
 * an inequality that is equal to the k'th inequality of "bmap"
680
 * up to the constant term, provided it does not point to the very
681
 * same inequality.
682
 * If there is no such inequality, then return the position where
683
 * such an inequality should be inserted.
684
 */
685
static int hash_index(struct isl_constraint_index *ci,
686
  __isl_keep isl_basic_map *bmap, int k)
687
10.6M
{
688
10.6M
  return hash_index_ineq(ci, &bmap->ineq[k]);
689
10.6M
}
690
691
static int set_hash_index(struct isl_constraint_index *ci,
692
  __isl_keep isl_basic_set *bset, int k)
693
35.7k
{
694
35.7k
  return hash_index(ci, bset, k);
695
35.7k
}
696
697
/* Fill in the "ci" data structure with the inequalities of "bset".
698
 */
699
static isl_stat setup_constraint_index(struct isl_constraint_index *ci,
700
  __isl_keep isl_basic_set *bset)
701
10.3k
{
702
10.3k
  int k, h;
703
10.3k
704
10.3k
  if (create_constraint_index(ci, bset) < 0)
705
0
    return isl_stat_error;
706
10.3k
707
46.0k
  
for (k = 0; 10.3k
k < bset->n_ineq46.0k
;
++k35.7k
)
{35.7k
708
35.7k
    h = set_hash_index(ci, bset, k);
709
35.7k
    ci->index[h] = &bset->ineq[k];
710
35.7k
  }
711
10.3k
712
10.3k
  return isl_stat_ok;
713
10.3k
}
714
715
/* Is the inequality ineq (obviously) redundant with respect
716
 * to the constraints in "ci"?
717
 *
718
 * Look for an inequality in "ci" with the same coefficients and then
719
 * check if the contant term of "ineq" is greater than or equal
720
 * to the constant term of that inequality.  If so, "ineq" is clearly
721
 * redundant.
722
 *
723
 * Note that hash_index_ineq ignores a stored constraint if it has
724
 * the same address as the passed inequality.  It is ok to pass
725
 * the address of a local variable here since it will never be
726
 * the same as the address of a constraint in "ci".
727
 */
728
static isl_bool constraint_index_is_redundant(struct isl_constraint_index *ci,
729
  isl_int *ineq)
730
27.1k
{
731
27.1k
  int h;
732
27.1k
733
27.1k
  h = hash_index_ineq(ci, &ineq);
734
27.1k
  if (!ci->index[h])
735
11.6k
    return isl_bool_false;
736
15.4k
  
return 15.4k
isl_int_ge15.4k
(ineq[0], (*ci->index[h])[0]);
737
27.1k
}
738
739
/* If we can eliminate more than one div, then we need to make
740
 * sure we do it from last div to first div, in order not to
741
 * change the position of the other divs that still need to
742
 * be removed.
743
 */
744
static __isl_give isl_basic_map *remove_duplicate_divs(
745
  __isl_take isl_basic_map *bmap, int *progress)
746
1.96M
{
747
1.96M
  unsigned int size;
748
1.96M
  int *index;
749
1.96M
  int *elim_for;
750
1.96M
  int k, l, h;
751
1.96M
  int bits;
752
1.96M
  struct isl_blk eq;
753
1.96M
  unsigned total_var;
754
1.96M
  unsigned total;
755
1.96M
  struct isl_ctx *ctx;
756
1.96M
757
1.96M
  bmap = isl_basic_map_order_divs(bmap);
758
1.96M
  if (
!bmap || 1.96M
bmap->n_div <= 11.96M
)
759
1.89M
    return bmap;
760
1.96M
761
73.8k
  total_var = isl_space_dim(bmap->dim, isl_dim_all);
762
73.8k
  total = total_var + bmap->n_div;
763
73.8k
764
73.8k
  ctx = bmap->ctx;
765
221k
  for (k = bmap->n_div - 1; 
k >= 0221k
;
--k147k
)
766
172k
    
if (172k
!172k
isl_int_is_zero172k
(bmap->div[k][0]))
767
24.8k
      break;
768
73.8k
  if (k <= 0)
769
49.8k
    return bmap;
770
73.8k
771
23.9k
  size = round_up(4 * bmap->n_div / 3 - 1);
772
23.9k
  if (size == 0)
773
0
    return bmap;
774
23.9k
  
elim_for = 23.9k
isl_calloc_array23.9k
(ctx, int, bmap->n_div);
775
23.9k
  bits = ffs(size) - 1;
776
23.9k
  index = isl_calloc_array(ctx, int, size);
777
23.9k
  if (
!elim_for || 23.9k
!index23.9k
)
778
0
    goto out;
779
23.9k
  eq = isl_blk_alloc(ctx, 1+total);
780
23.9k
  if (isl_blk_is_error(eq))
781
0
    goto out;
782
23.9k
783
23.9k
  isl_seq_clr(eq.data, 1+total);
784
23.9k
  index[isl_seq_get_hash_bits(bmap->div[k], 2+total, bits)] = k + 1;
785
67.8k
  for (--k; 
k >= 067.8k
;
--k43.8k
)
{43.8k
786
43.8k
    uint32_t hash;
787
43.8k
788
43.8k
    if (isl_int_is_zero(bmap->div[k][0]))
789
9.09k
      continue;
790
43.8k
791
34.8k
    hash = isl_seq_get_hash_bits(bmap->div[k], 2+total, bits);
792
43.6k
    for (h = hash; 
index[h]43.6k
;
h = (h+1) % size8.83k
)
793
15.0k
      
if (15.0k
isl_seq_eq(bmap->div[k],15.0k
794
15.0k
               bmap->div[index[h]-1], 2+total))
795
6.24k
        break;
796
34.8k
    if (
index[h]34.8k
)
{6.24k
797
6.24k
      *progress = 1;
798
6.24k
      l = index[h] - 1;
799
6.24k
      elim_for[l] = k + 1;
800
6.24k
    }
801
34.8k
    index[h] = k+1;
802
34.8k
  }
803
94.3k
  for (l = bmap->n_div - 1; 
l >= 094.3k
;
--l70.3k
)
{70.3k
804
70.3k
    if (!elim_for[l])
805
64.1k
      continue;
806
6.24k
    k = elim_for[l] - 1;
807
6.24k
    isl_int_set_si(eq.data[1+total_var+k], -1);
808
6.24k
    isl_int_set_si(eq.data[1+total_var+l], 1);
809
6.24k
    bmap = eliminate_div(bmap, eq.data, l, 1);
810
6.24k
    if (!bmap)
811
0
      break;
812
6.24k
    
isl_int_set_si6.24k
(eq.data[1+total_var+k], 0);6.24k
813
6.24k
    isl_int_set_si(eq.data[1+total_var+l], 0);
814
6.24k
  }
815
23.9k
816
23.9k
  isl_blk_free(ctx, eq);
817
23.9k
out:
818
23.9k
  free(index);
819
23.9k
  free(elim_for);
820
23.9k
  return bmap;
821
23.9k
}
822
823
static int n_pure_div_eq(struct isl_basic_map *bmap)
824
23.1k
{
825
23.1k
  int i, j;
826
23.1k
  unsigned total;
827
23.1k
828
23.1k
  total = isl_space_dim(bmap->dim, isl_dim_all);
829
38.9k
  for (i = 0, j = bmap->n_div-1; 
i < bmap->n_eq38.9k
;
++i15.7k
)
{33.5k
830
57.6k
    while (
j >= 0 && 57.6k
isl_int_is_zero43.0k
(bmap->eq[i][1 + total + j]))
831
24.0k
      --j;
832
33.5k
    if (j < 0)
833
14.6k
      break;
834
18.9k
    
if (18.9k
isl_seq_first_non_zero(bmap->eq[i] + 1 + total, j) != -118.9k
)
835
3.17k
      return 0;
836
18.9k
  }
837
19.9k
  return i;
838
23.1k
}
839
840
/* Normalize divs that appear in equalities.
841
 *
842
 * In particular, we assume that bmap contains some equalities
843
 * of the form
844
 *
845
 *  a x = m * e_i
846
 *
847
 * and we want to replace the set of e_i by a minimal set and
848
 * such that the new e_i have a canonical representation in terms
849
 * of the vector x.
850
 * If any of the equalities involves more than one divs, then
851
 * we currently simply bail out.
852
 *
853
 * Let us first additionally assume that all equalities involve
854
 * a div.  The equalities then express modulo constraints on the
855
 * remaining variables and we can use "parameter compression"
856
 * to find a minimal set of constraints.  The result is a transformation
857
 *
858
 *  x = T(x') = x_0 + G x'
859
 *
860
 * with G a lower-triangular matrix with all elements below the diagonal
861
 * non-negative and smaller than the diagonal element on the same row.
862
 * We first normalize x_0 by making the same property hold in the affine
863
 * T matrix.
864
 * The rows i of G with a 1 on the diagonal do not impose any modulo
865
 * constraint and simply express x_i = x'_i.
866
 * For each of the remaining rows i, we introduce a div and a corresponding
867
 * equality.  In particular
868
 *
869
 *  g_ii e_j = x_i - g_i(x')
870
 *
871
 * where each x'_k is replaced either by x_k (if g_kk = 1) or the
872
 * corresponding div (if g_kk != 1).
873
 *
874
 * If there are any equalities not involving any div, then we
875
 * first apply a variable compression on the variables x:
876
 *
877
 *  x = C x'' x'' = C_2 x
878
 *
879
 * and perform the above parameter compression on A C instead of on A.
880
 * The resulting compression is then of the form
881
 *
882
 *  x'' = T(x') = x_0 + G x'
883
 *
884
 * and in constructing the new divs and the corresponding equalities,
885
 * we have to replace each x'', i.e., the x'_k with (g_kk = 1),
886
 * by the corresponding row from C_2.
887
 */
888
static __isl_give isl_basic_map *normalize_divs(__isl_take isl_basic_map *bmap,
889
  int *progress)
890
1.96M
{
891
1.96M
  int i, j, k;
892
1.96M
  int total;
893
1.96M
  int div_eq;
894
1.96M
  struct isl_mat *B;
895
1.96M
  struct isl_vec *d;
896
1.96M
  struct isl_mat *T = NULL;
897
1.96M
  struct isl_mat *C = NULL;
898
1.96M
  struct isl_mat *C2 = NULL;
899
1.96M
  isl_int v;
900
1.96M
  int *pos = NULL;
901
1.96M
  int dropped, needed;
902
1.96M
903
1.96M
  if (!bmap)
904
0
    return NULL;
905
1.96M
906
1.96M
  
if (1.96M
bmap->n_div == 01.96M
)
907
1.89M
    return bmap;
908
1.96M
909
68.9k
  
if (68.9k
bmap->n_eq == 068.9k
)
910
27.0k
    return bmap;
911
68.9k
912
41.8k
  
if (41.8k
ISL_F_ISSET41.8k
(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS))
913
18.7k
    return bmap;
914
41.8k
915
23.1k
  total = isl_space_dim(bmap->dim, isl_dim_all);
916
23.1k
  div_eq = n_pure_div_eq(bmap);
917
23.1k
  if (div_eq == 0)
918
10.6k
    return bmap;
919
23.1k
920
12.4k
  
if (12.4k
div_eq < bmap->n_eq12.4k
)
{7.15k
921
7.15k
    B = isl_mat_sub_alloc6(bmap->ctx, bmap->eq, div_eq,
922
7.15k
          bmap->n_eq - div_eq, 0, 1 + total);
923
7.15k
    C = isl_mat_variable_compression(B, &C2);
924
7.15k
    if (
!C || 7.15k
!C27.15k
)
925
0
      goto error;
926
7.15k
    
if (7.15k
C->n_col == 07.15k
)
{12
927
12
      bmap = isl_basic_map_set_to_empty(bmap);
928
12
      isl_mat_free(C);
929
12
      isl_mat_free(C2);
930
12
      goto done;
931
12
    }
932
7.15k
  }
933
12.4k
934
12.4k
  d = isl_vec_alloc(bmap->ctx, div_eq);
935
12.4k
  if (!d)
936
0
    goto error;
937
28.1k
  
for (i = 0, j = bmap->n_div-1; 12.4k
i < div_eq28.1k
;
++i15.6k
)
{15.6k
938
20.1k
    while (
j >= 0 && 20.1k
isl_int_is_zero20.1k
(bmap->eq[i][1 + total + j]))
939
4.50k
      --j;
940
15.6k
    isl_int_set(d->block.data[i], bmap->eq[i][1 + total + j]);
941
15.6k
  }
942
12.4k
  B = isl_mat_sub_alloc6(bmap->ctx, bmap->eq, 0, div_eq, 0, 1 + total);
943
12.4k
944
12.4k
  if (
C12.4k
)
{7.14k
945
7.14k
    B = isl_mat_product(B, C);
946
7.14k
    C = NULL;
947
7.14k
  }
948
12.4k
949
12.4k
  T = isl_mat_parameter_compression(B, d);
950
12.4k
  if (!T)
951
0
    goto error;
952
12.4k
  
if (12.4k
T->n_col == 012.4k
)
{1.34k
953
1.34k
    bmap = isl_basic_map_set_to_empty(bmap);
954
1.34k
    isl_mat_free(C2);
955
1.34k
    isl_mat_free(T);
956
1.34k
    goto done;
957
1.34k
  }
958
11.1k
  
isl_int_init11.1k
(v);11.1k
959
40.7k
  for (i = 0; 
i < T->n_row - 140.7k
;
++i29.5k
)
{29.5k
960
29.5k
    isl_int_fdiv_q(v, T->row[1 + i][0], T->row[1 + i][1 + i]);
961
29.5k
    if (isl_int_is_zero(v))
962
25.8k
      continue;
963
3.73k
    isl_mat_col_submul(T, 0, v, 1 + i);
964
3.73k
  }
965
11.1k
  isl_int_clear(v);
966
11.1k
  pos = isl_alloc_array(bmap->ctx, int, T->n_row);
967
11.1k
  if (!pos)
968
0
    goto error;
969
11.1k
  /* We have to be careful because dropping equalities may reorder them */
970
11.1k
  dropped = 0;
971
27.6k
  for (j = bmap->n_div - 1; 
j >= 027.6k
;
--j16.5k
)
{16.5k
972
22.5k
    for (i = 0; 
i < bmap->n_eq22.5k
;
++i5.99k
)
973
19.4k
      
if (19.4k
!19.4k
isl_int_is_zero19.4k
(bmap->eq[i][1 + total + j]))
974
13.4k
        break;
975
16.5k
    if (
i < bmap->n_eq16.5k
)
{13.4k
976
13.4k
      bmap = isl_basic_map_drop_div(bmap, j);
977
13.4k
      isl_basic_map_drop_equality(bmap, i);
978
13.4k
      ++dropped;
979
13.4k
    }
980
16.5k
  }
981
11.1k
  pos[0] = 0;
982
11.1k
  needed = 0;
983
40.7k
  for (i = 1; 
i < T->n_row40.7k
;
++i29.5k
)
{29.5k
984
29.5k
    if (isl_int_is_one(T->row[i][i]))
985
17.9k
      pos[i] = i;
986
29.5k
    else
987
11.6k
      needed++;
988
29.5k
  }
989
11.1k
  if (
needed > dropped11.1k
)
{13
990
13
    bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
991
13
        needed, needed, 0);
992
13
    if (!bmap)
993
0
      goto error;
994
13
  }
995
40.7k
  
for (i = 1; 11.1k
i < T->n_row40.7k
;
++i29.5k
)
{29.5k
996
29.5k
    if (isl_int_is_one(T->row[i][i]))
997
17.9k
      continue;
998
11.6k
    k = isl_basic_map_alloc_div(bmap);
999
11.6k
    pos[i] = 1 + total + k;
1000
11.6k
    isl_seq_clr(bmap->div[k] + 1, 1 + total + bmap->n_div);
1001
11.6k
    isl_int_set(bmap->div[k][0], T->row[i][i]);
1002
11.6k
    if (C2)
1003
6.11k
      isl_seq_cpy(bmap->div[k] + 1, C2->row[i], 1 + total);
1004
11.6k
    else
1005
5.54k
      isl_int_set_si(bmap->div[k][1 + i], 1);
1006
40.0k
    for (j = 0; 
j < i40.0k
;
++j28.4k
)
{28.4k
1007
28.4k
      if (isl_int_is_zero(T->row[i][j]))
1008
20.1k
        continue;
1009
8.21k
      
if (8.21k
pos[j] < T->n_row && 8.21k
C28.13k
)
1010
4.08k
        isl_seq_submul(bmap->div[k] + 1, T->row[i][j],
1011
4.08k
            C2->row[pos[j]], 1 + total);
1012
8.21k
      else
1013
4.13k
        isl_int_neg(bmap->div[k][1 + pos[j]],
1014
8.21k
                T->row[i][j]);
1015
8.21k
    }
1016
11.6k
    j = isl_basic_map_alloc_equality(bmap);
1017
11.6k
    isl_seq_neg(bmap->eq[j], bmap->div[k]+1, 1+total+bmap->n_div);
1018
11.6k
    isl_int_set(bmap->eq[j][pos[i]], bmap->div[k][0]);
1019
11.6k
  }
1020
11.1k
  free(pos);
1021
11.1k
  isl_mat_free(C2);
1022
11.1k
  isl_mat_free(T);
1023
11.1k
1024
11.1k
  if (progress)
1025
11.1k
    *progress = 1;
1026
12.4k
done:
1027
12.4k
  ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1028
12.4k
1029
12.4k
  return bmap;
1030
0
error:
1031
0
  free(pos);
1032
0
  isl_mat_free(C);
1033
0
  isl_mat_free(C2);
1034
0
  isl_mat_free(T);
1035
0
  return bmap;
1036
11.1k
}
1037
1038
static __isl_give isl_basic_map *set_div_from_lower_bound(
1039
  __isl_take isl_basic_map *bmap, int div, int ineq)
1040
1.17k
{
1041
1.17k
  unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1042
1.17k
1043
1.17k
  isl_seq_neg(bmap->div[div] + 1, bmap->ineq[ineq], total + bmap->n_div);
1044
1.17k
  isl_int_set(bmap->div[div][0], bmap->ineq[ineq][total + div]);
1045
1.17k
  isl_int_add(bmap->div[div][1], bmap->div[div][1], bmap->div[div][0]);
1046
1.17k
  isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
1047
1.17k
  isl_int_set_si(bmap->div[div][1 + total + div], 0);
1048
1.17k
1049
1.17k
  return bmap;
1050
1.17k
}
1051
1052
/* Check whether it is ok to define a div based on an inequality.
1053
 * To avoid the introduction of circular definitions of divs, we
1054
 * do not allow such a definition if the resulting expression would refer to
1055
 * any other undefined divs or if any known div is defined in
1056
 * terms of the unknown div.
1057
 */
1058
static isl_bool ok_to_set_div_from_bound(__isl_keep isl_basic_map *bmap,
1059
  int div, int ineq)
1060
5.27k
{
1061
5.27k
  int j;
1062
5.27k
  unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1063
5.27k
1064
5.27k
  /* Not defined in terms of unknown divs */
1065
16.7k
  for (j = 0; 
j < bmap->n_div16.7k
;
++j11.4k
)
{15.3k
1066
15.3k
    if (div == j)
1067
4.74k
      continue;
1068
10.5k
    
if (10.5k
isl_int_is_zero10.5k
(bmap->ineq[ineq][total + j]))
1069
6.61k
      continue;
1070
3.94k
    
if (3.94k
isl_int_is_zero3.94k
(bmap->div[j][0]))
1071
3.86k
      return isl_bool_false;
1072
3.94k
  }
1073
5.27k
1074
5.27k
  /* No other div defined in terms of this one => avoid loops */
1075
5.79k
  
for (j = 0; 1.40k
j < bmap->n_div5.79k
;
++j4.38k
)
{4.61k
1076
4.61k
    if (div == j)
1077
1.40k
      continue;
1078
3.20k
    
if (3.20k
isl_int_is_zero3.20k
(bmap->div[j][0]))
1079
956
      continue;
1080
2.25k
    
if (2.25k
!2.25k
isl_int_is_zero2.25k
(bmap->div[j][1 + total + div]))
1081
235
      return isl_bool_false;
1082
2.25k
  }
1083
1.40k
1084
1.17k
  return isl_bool_true;
1085
1.40k
}
1086
1087
/* Would an expression for div "div" based on inequality "ineq" of "bmap"
1088
 * be a better expression than the current one?
1089
 *
1090
 * If we do not have any expression yet, then any expression would be better.
1091
 * Otherwise we check if the last variable involved in the inequality
1092
 * (disregarding the div that it would define) is in an earlier position
1093
 * than the last variable involved in the current div expression.
1094
 */
1095
static isl_bool better_div_constraint(__isl_keep isl_basic_map *bmap,
1096
  int div, int ineq)
1097
87.9k
{
1098
87.9k
  unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1099
87.9k
  int last_div;
1100
87.9k
  int last_ineq;
1101
87.9k
1102
87.9k
  if (isl_int_is_zero(bmap->div[div][0]))
1103
4.98k
    return isl_bool_true;
1104
87.9k
1105
82.9k
  
if (82.9k
isl_seq_last_non_zero(bmap->ineq[ineq] + total + div + 1,82.9k
1106
82.9k
          bmap->n_div - (div + 1)) >= 0)
1107
2.08k
    return isl_bool_false;
1108
82.9k
1109
80.8k
  last_ineq = isl_seq_last_non_zero(bmap->ineq[ineq], total + div);
1110
80.8k
  last_div = isl_seq_last_non_zero(bmap->div[div] + 1,
1111
80.8k
           total + bmap->n_div);
1112
80.8k
1113
80.8k
  return last_ineq < last_div;
1114
82.9k
}
1115
1116
/* Given two constraints "k" and "l" that are opposite to each other,
1117
 * except for the constant term, check if we can use them
1118
 * to obtain an expression for one of the hitherto unknown divs or
1119
 * a "better" expression for a div for which we already have an expression.
1120
 * "sum" is the sum of the constant terms of the constraints.
1121
 * If this sum is strictly smaller than the coefficient of one
1122
 * of the divs, then this pair can be used define the div.
1123
 * To avoid the introduction of circular definitions of divs, we
1124
 * do not use the pair if the resulting expression would refer to
1125
 * any other undefined divs or if any known div is defined in
1126
 * terms of the unknown div.
1127
 */
1128
static __isl_give isl_basic_map *check_for_div_constraints(
1129
  __isl_take isl_basic_map *bmap, int k, int l, isl_int sum,
1130
  int *progress)
1131
3.33M
{
1132
3.33M
  int i;
1133
3.33M
  unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1134
3.33M
1135
3.54M
  for (i = 0; 
i < bmap->n_div3.54M
;
++i214k
)
{302k
1136
302k
    isl_bool set_div;
1137
302k
1138
302k
    if (isl_int_is_zero(bmap->ineq[k][total + i]))
1139
199k
      continue;
1140
103k
    
if (103k
isl_int_abs_ge103k
(sum, bmap->ineq[k][total + i]))
1141
15.7k
      continue;
1142
87.9k
    set_div = better_div_constraint(bmap, i, k);
1143
87.9k
    if (
set_div >= 0 && 87.9k
set_div87.9k
)
1144
5.26k
      set_div = ok_to_set_div_from_bound(bmap, i, k);
1145
87.9k
    if (set_div < 0)
1146
0
      return isl_basic_map_free(bmap);
1147
87.9k
    
if (87.9k
!set_div87.9k
)
1148
86.7k
      break;
1149
1.17k
    
if (1.17k
isl_int_is_pos1.17k
(bmap->ineq[k][total + i]))
1150
440
      bmap = set_div_from_lower_bound(bmap, i, k);
1151
1.17k
    else
1152
731
      bmap = set_div_from_lower_bound(bmap, i, l);
1153
1.17k
    if (progress)
1154
1.17k
      *progress = 1;
1155
1.17k
    break;
1156
87.9k
  }
1157
3.33M
  return bmap;
1158
3.33M
}
1159
1160
__isl_give isl_basic_map *isl_basic_map_remove_duplicate_constraints(
1161
  __isl_take isl_basic_map *bmap, int *progress, int detect_divs)
1162
1.98M
{
1163
1.98M
  struct isl_constraint_index ci;
1164
1.98M
  int k, l, h;
1165
1.98M
  unsigned total = isl_basic_map_total_dim(bmap);
1166
1.98M
  isl_int sum;
1167
1.98M
1168
1.98M
  if (
!bmap || 1.98M
bmap->n_ineq <= 11.98M
)
1169
663k
    return bmap;
1170
1.98M
1171
1.32M
  
if (1.32M
create_constraint_index(&ci, bmap) < 01.32M
)
1172
0
    return bmap;
1173
1.32M
1174
1.32M
  h = isl_seq_get_hash_bits(bmap->ineq[0] + 1, total, ci.bits);
1175
1.32M
  ci.index[h] = &bmap->ineq[0];
1176
7.12M
  for (k = 1; 
k < bmap->n_ineq7.12M
;
++k5.80M
)
{5.80M
1177
5.80M
    h = hash_index(&ci, bmap, k);
1178
5.80M
    if (
!ci.index[h]5.80M
)
{5.29M
1179
5.29M
      ci.index[h] = &bmap->ineq[k];
1180
5.29M
      continue;
1181
5.29M
    }
1182
509k
    
if (509k
progress509k
)
1183
503k
      *progress = 1;
1184
509k
    l = ci.index[h] - &bmap->ineq[0];
1185
509k
    if (isl_int_lt(bmap->ineq[k][0], bmap->ineq[l][0]))
1186
50.4k
      swap_inequality(bmap, k, l);
1187
509k
    isl_basic_map_drop_inequality(bmap, k);
1188
509k
    --k;
1189
509k
  }
1190
1.32M
  isl_int_init(sum);
1191
6.05M
  for (k = 0; 
k < bmap->n_ineq-16.05M
;
++k4.73M
)
{4.84M
1192
4.84M
    isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
1193
4.84M
    h = hash_index(&ci, bmap, k);
1194
4.84M
    isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
1195
4.84M
    if (!ci.index[h])
1196
1.37M
      continue;
1197
3.46M
    l = ci.index[h] - &bmap->ineq[0];
1198
3.46M
    isl_int_add(sum, bmap->ineq[k][0], bmap->ineq[l][0]);
1199
3.46M
    if (
isl_int_is_pos3.46M
(sum))
{3.35M
1200
3.35M
      if (detect_divs)
1201
3.33M
        bmap = check_for_div_constraints(bmap, k, l,
1202
3.33M
                 sum, progress);
1203
3.35M
      continue;
1204
3.35M
    }
1205
111k
    
if (111k
isl_int_is_zero111k
(sum))
{12.8k
1206
12.8k
      /* We need to break out of the loop after these
1207
12.8k
       * changes since the contents of the hash
1208
12.8k
       * will no longer be valid.
1209
12.8k
       * Plus, we probably we want to regauss first.
1210
12.8k
       */
1211
12.8k
      if (progress)
1212
12.7k
        *progress = 1;
1213
12.8k
      isl_basic_map_drop_inequality(bmap, l);
1214
12.8k
      isl_basic_map_inequality_to_equality(bmap, k);
1215
12.8k
    } else
1216
98.9k
      bmap = isl_basic_map_set_to_empty(bmap);
1217
111k
    break;
1218
3.46M
  }
1219
1.32M
  isl_int_clear(sum);
1220
1.32M
1221
1.32M
  constraint_index_free(&ci);
1222
1.32M
  return bmap;
1223
1.32M
}
1224
1225
/* Detect all pairs of inequalities that form an equality.
1226
 *
1227
 * isl_basic_map_remove_duplicate_constraints detects at most one such pair.
1228
 * Call it repeatedly while it is making progress.
1229
 */
1230
__isl_give isl_basic_map *isl_basic_map_detect_inequality_pairs(
1231
  __isl_take isl_basic_map *bmap, int *progress)
1232
281
{
1233
281
  int duplicate;
1234
281
1235
479
  do {
1236
479
    duplicate = 0;
1237
479
    bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1238
479
                &duplicate, 0);
1239
479
    if (
progress && 479
duplicate197
)
1240
56
      *progress = 1;
1241
479
  } while (duplicate);
1242
281
1243
281
  return bmap;
1244
281
}
1245
1246
/* Eliminate knowns divs from constraints where they appear with
1247
 * a (positive or negative) unit coefficient.
1248
 *
1249
 * That is, replace
1250
 *
1251
 *  floor(e/m) + f >= 0
1252
 *
1253
 * by
1254
 *
1255
 *  e + m f >= 0
1256
 *
1257
 * and
1258
 *
1259
 *  -floor(e/m) + f >= 0
1260
 *
1261
 * by
1262
 *
1263
 *  -e + m f + m - 1 >= 0
1264
 *
1265
 * The first conversion is valid because floor(e/m) >= -f is equivalent
1266
 * to e/m >= -f because -f is an integral expression.
1267
 * The second conversion follows from the fact that
1268
 *
1269
 *  -floor(e/m) = ceil(-e/m) = floor((-e + m - 1)/m)
1270
 *
1271
 *
1272
 * Note that one of the div constraints may have been eliminated
1273
 * due to being redundant with respect to the constraint that is
1274
 * being modified by this function.  The modified constraint may
1275
 * no longer imply this div constraint, so we add it back to make
1276
 * sure we do not lose any information.
1277
 *
1278
 * We skip integral divs, i.e., those with denominator 1, as we would
1279
 * risk eliminating the div from the div constraints.  We do not need
1280
 * to handle those divs here anyway since the div constraints will turn
1281
 * out to form an equality and this equality can then be used to eliminate
1282
 * the div from all constraints.
1283
 */
1284
static __isl_give isl_basic_map *eliminate_unit_divs(
1285
  __isl_take isl_basic_map *bmap, int *progress)
1286
1.96M
{
1287
1.96M
  int i, j;
1288
1.96M
  isl_ctx *ctx;
1289
1.96M
  unsigned total;
1290
1.96M
1291
1.96M
  if (!bmap)
1292
0
    return NULL;
1293
1.96M
1294
1.96M
  ctx = isl_basic_map_get_ctx(bmap);
1295
1.96M
  total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1296
1.96M
1297
2.29M
  for (i = 0; 
i < bmap->n_div2.29M
;
++i330k
)
{330k
1298
330k
    if (isl_int_is_zero(bmap->div[i][0]))
1299
236k
      continue;
1300
94.0k
    
if (94.0k
isl_int_is_one94.0k
(bmap->div[i][0]))
1301
388
      continue;
1302
806k
    
for (j = 0; 93.6k
j < bmap->n_ineq806k
;
++j713k
)
{713k
1303
713k
      int s;
1304
713k
1305
713k
      if (
!713k
isl_int_is_one713k
(bmap->ineq[j][total + i]) &&
1306
709k
          
!709k
isl_int_is_negone709k
(bmap->ineq[j][total + i]))
1307
705k
        continue;
1308
713k
1309
7.31k
      *progress = 1;
1310
7.31k
1311
7.31k
      s = isl_int_sgn(bmap->ineq[j][total + i]);
1312
7.31k
      isl_int_set_si(bmap->ineq[j][total + i], 0);
1313
7.31k
      if (s < 0)
1314
3.59k
        isl_seq_combine(bmap->ineq[j],
1315
3.59k
          ctx->negone, bmap->div[i] + 1,
1316
3.59k
          bmap->div[i][0], bmap->ineq[j],
1317
3.59k
          total + bmap->n_div);
1318
7.31k
      else
1319
3.72k
        isl_seq_combine(bmap->ineq[j],
1320
3.72k
          ctx->one, bmap->div[i] + 1,
1321
3.72k
          bmap->div[i][0], bmap->ineq[j],
1322
3.72k
          total + bmap->n_div);
1323
7.31k
      if (
s < 07.31k
)
{3.59k
1324
3.59k
        isl_int_add(bmap->ineq[j][0],
1325
3.59k
          bmap->ineq[j][0], bmap->div[i][0]);
1326
3.59k
        isl_int_sub_ui(bmap->ineq[j][0],
1327
3.59k
          bmap->ineq[j][0], 1);
1328
3.59k
      }
1329
7.31k
1330
7.31k
      bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1331
7.31k
      if (isl_basic_map_add_div_constraint(bmap, i, s) < 0)
1332
0
        return isl_basic_map_free(bmap);
1333
7.31k
    }
1334
93.6k
  }
1335
1.96M
1336
1.96M
  return bmap;
1337
1.96M
}
1338
1339
__isl_give isl_basic_map *isl_basic_map_simplify(__isl_take isl_basic_map *bmap)
1340
1.64M
{
1341
1.64M
  int progress = 1;
1342
1.64M
  if (!bmap)
1343
0
    return NULL;
1344
3.60M
  
while (1.64M
progress3.60M
)
{2.03M
1345
2.03M
    isl_bool empty;
1346
2.03M
1347
2.03M
    progress = 0;
1348
2.03M
    empty = isl_basic_map_plain_is_empty(bmap);
1349
2.03M
    if (empty < 0)
1350
0
      return isl_basic_map_free(bmap);
1351
2.03M
    
if (2.03M
empty2.03M
)
1352
66.1k
      break;
1353
1.96M
    bmap = isl_basic_map_normalize_constraints(bmap);
1354
1.96M
    bmap = reduce_div_coefficients(bmap);
1355
1.96M
    bmap = normalize_div_expressions(bmap);
1356
1.96M
    bmap = remove_duplicate_divs(bmap, &progress);
1357
1.96M
    bmap = eliminate_unit_divs(bmap, &progress);
1358
1.96M
    bmap = eliminate_divs_eq(bmap, &progress);
1359
1.96M
    bmap = eliminate_divs_ineq(bmap, &progress);
1360
1.96M
    bmap = isl_basic_map_gauss(bmap, &progress);
1361
1.96M
    /* requires equalities in normal form */
1362
1.96M
    bmap = normalize_divs(bmap, &progress);
1363
1.96M
    bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1364
1.96M
                &progress, 1);
1365
1.96M
    if (
bmap && 1.96M
progress1.96M
)
1366
1.96M
      ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1367
1.96M
  }
1368
1.64M
  return bmap;
1369
1.64M
}
1370
1371
struct isl_basic_set *isl_basic_set_simplify(struct isl_basic_set *bset)
1372
590k
{
1373
590k
  return bset_from_bmap(isl_basic_map_simplify(bset_to_bmap(bset)));
1374
590k
}
1375
1376
1377
isl_bool isl_basic_map_is_div_constraint(__isl_keep isl_basic_map *bmap,
1378
  isl_int *constraint, unsigned div)
1379
62.0k
{
1380
62.0k
  unsigned pos;
1381
62.0k
1382
62.0k
  if (!bmap)
1383
0
    return isl_bool_error;
1384
62.0k
1385
62.0k
  pos = 1 + isl_space_dim(bmap->dim, isl_dim_all) + div;
1386
62.0k
1387
62.0k
  if (
isl_int_eq62.0k
(constraint[pos], bmap->div[div][0]))
{26.8k
1388
26.8k
    int neg;
1389
26.8k
    isl_int_sub(bmap->div[div][1],
1390
26.8k
        bmap->div[div][1], bmap->div[div][0]);
1391
26.8k
    isl_int_add_ui(bmap->div[div][1], bmap->div[div][1], 1);
1392
26.8k
    neg = isl_seq_is_neg(constraint, bmap->div[div]+1, pos);
1393
26.8k
    isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
1394
26.8k
    isl_int_add(bmap->div[div][1],
1395
26.8k
        bmap->div[div][1], bmap->div[div][0]);
1396
26.8k
    if (!neg)
1397
7.96k
      return isl_bool_false;
1398
18.8k
    
if (18.8k
isl_seq_first_non_zero(constraint+pos+1,18.8k
1399
18.8k
              bmap->n_div-div-1) != -1)
1400
0
      return isl_bool_false;
1401
35.2k
  } else 
if (35.2k
isl_int_abs_eq35.2k
(constraint[pos], bmap->div[div][0]))
{25.4k
1402
25.4k
    if (!isl_seq_eq(constraint, bmap->div[div]+1, pos))
1403
10.5k
      return isl_bool_false;
1404
14.8k
    
if (14.8k
isl_seq_first_non_zero(constraint+pos+1,14.8k
1405
14.8k
              bmap->n_div-div-1) != -1)
1406
1
      return isl_bool_false;
1407
14.8k
  } else
1408
9.78k
    return isl_bool_false;
1409
62.0k
1410
33.7k
  return isl_bool_true;
1411
62.0k
}
1412
1413
isl_bool isl_basic_set_is_div_constraint(__isl_keep isl_basic_set *bset,
1414
  isl_int *constraint, unsigned div)
1415
0
{
1416
0
  return isl_basic_map_is_div_constraint(bset, constraint, div);
1417
0
}
1418
1419
1420
/* If the only constraints a div d=floor(f/m)
1421
 * appears in are its two defining constraints
1422
 *
1423
 *  f - m d >=0
1424
 *  -(f - (m - 1)) + m d >= 0
1425
 *
1426
 * then it can safely be removed.
1427
 */
1428
static isl_bool div_is_redundant(__isl_keep isl_basic_map *bmap, int div)
1429
96.4k
{
1430
96.4k
  int i;
1431
96.4k
  unsigned pos = 1 + isl_space_dim(bmap->dim, isl_dim_all) + div;
1432
96.4k
1433
129k
  for (i = 0; 
i < bmap->n_eq129k
;
++i33.2k
)
1434
74.7k
    
if (74.7k
!74.7k
isl_int_is_zero74.7k
(bmap->eq[i][pos]))
1435
41.4k
      return isl_bool_false;
1436
96.4k
1437
229k
  
for (i = 0; 54.9k
i < bmap->n_ineq229k
;
++i174k
)
{202k
1438
202k
    isl_bool red;
1439
202k
1440
202k
    if (isl_int_is_zero(bmap->ineq[i][pos]))
1441
140k
      continue;
1442
61.7k
    red = isl_basic_map_is_div_constraint(bmap, bmap->ineq[i], div);
1443
61.7k
    if (
red < 0 || 61.7k
!red61.7k
)
1444
28.0k
      return red;
1445
61.7k
  }
1446
54.9k
1447
60.2k
  
for (i = 0; 26.8k
i < bmap->n_div60.2k
;
++i33.3k
)
{33.3k
1448
33.3k
    if (isl_int_is_zero(bmap->div[i][0]))
1449
470
      continue;
1450
32.8k
    
if (32.8k
!32.8k
isl_int_is_zero32.8k
(bmap->div[i][1+pos]))
1451
1
      return isl_bool_false;
1452
32.8k
  }
1453
26.8k
1454
26.8k
  return isl_bool_true;
1455
26.8k
}
1456
1457
/*
1458
 * Remove divs that don't occur in any of the constraints or other divs.
1459
 * These can arise when dropping constraints from a basic map or
1460
 * when the divs of a basic map have been temporarily aligned
1461
 * with the divs of another basic map.
1462
 */
1463
static __isl_give isl_basic_map *remove_redundant_divs(
1464
  __isl_take isl_basic_map *bmap)
1465
3.74M
{
1466
3.74M
  int i;
1467
3.74M
1468
3.74M
  if (!bmap)
1469
0
    return NULL;
1470
3.74M
1471
3.84M
  
for (i = bmap->n_div-1; 3.74M
i >= 03.84M
;
--i96.4k
)
{96.4k
1472
96.4k
    isl_bool redundant;
1473
96.4k
1474
96.4k
    redundant = div_is_redundant(bmap, i);
1475
96.4k
    if (redundant < 0)
1476
0
      return isl_basic_map_free(bmap);
1477
96.4k
    
if (96.4k
!redundant96.4k
)
1478
69.5k
      continue;
1479
26.8k
    bmap = isl_basic_map_drop_div(bmap, i);
1480
26.8k
  }
1481
3.74M
  return bmap;
1482
3.74M
}
1483
1484
/* Mark "bmap" as final, without checking for obviously redundant
1485
 * integer divisions.  This function should be used when "bmap"
1486
 * is known not to involve any such integer divisions.
1487
 */
1488
__isl_give isl_basic_map *isl_basic_map_mark_final(
1489
  __isl_take isl_basic_map *bmap)
1490
3.75M
{
1491
3.75M
  if (!bmap)
1492
0
    return NULL;
1493
3.75M
  
ISL_F_SET3.75M
(bmap, ISL_BASIC_SET_FINAL);3.75M
1494
3.75M
  return bmap;
1495
3.75M
}
1496
1497
/* Mark "bmap" as final, after removing obviously redundant integer divisions.
1498
 */
1499
__isl_give isl_basic_map *isl_basic_map_finalize(__isl_take isl_basic_map *bmap)
1500
3.74M
{
1501
3.74M
  bmap = remove_redundant_divs(bmap);
1502
3.74M
  bmap = isl_basic_map_mark_final(bmap);
1503
3.74M
  return bmap;
1504
3.74M
}
1505
1506
struct isl_basic_set *isl_basic_set_finalize(struct isl_basic_set *bset)
1507
691k
{
1508
691k
  return bset_from_bmap(isl_basic_map_finalize(bset_to_bmap(bset)));
1509
691k
}
1510
1511
/* Remove definition of any div that is defined in terms of the given variable.
1512
 * The div itself is not removed.  Functions such as
1513
 * eliminate_divs_ineq depend on the other divs remaining in place.
1514
 */
1515
static __isl_give isl_basic_map *remove_dependent_vars(
1516
  __isl_take isl_basic_map *bmap, int pos)
1517
75.4k
{
1518
75.4k
  int i;
1519
75.4k
1520
75.4k
  if (!bmap)
1521
0
    return NULL;
1522
75.4k
1523
157k
  
for (i = 0; 75.4k
i < bmap->n_div157k
;
++i81.8k
)
{81.8k
1524
81.8k
    if (isl_int_is_zero(bmap->div[i][0]))
1525
79.7k
      continue;
1526
2.12k
    
if (2.12k
isl_int_is_zero2.12k
(bmap->div[i][1+1+pos]))
1527
2.06k
      continue;
1528
63
    bmap = isl_basic_map_mark_div_unknown(bmap, i);
1529
63
    if (!bmap)
1530
0
      return NULL;
1531
63
  }
1532
75.4k
  return bmap;
1533
75.4k
}
1534
1535
/* Eliminate the specified variables from the constraints using
1536
 * Fourier-Motzkin.  The variables themselves are not removed.
1537
 */
1538
__isl_give isl_basic_map *isl_basic_map_eliminate_vars(
1539
  __isl_take isl_basic_map *bmap, unsigned pos, unsigned n)
1540
74.4k
{
1541
74.4k
  int d;
1542
74.4k
  int i, j, k;
1543
74.4k
  unsigned total;
1544
74.4k
  int need_gauss = 0;
1545
74.4k
1546
74.4k
  if (n == 0)
1547
1.69k
    return bmap;
1548
72.7k
  
if (72.7k
!bmap72.7k
)
1549
0
    return NULL;
1550
72.7k
  total = isl_basic_map_total_dim(bmap);
1551
72.7k
1552
72.7k
  bmap = isl_basic_map_cow(bmap);
1553
148k
  for (d = pos + n - 1; 
d >= 0 && 148k
d >= pos132k
;
--d75.4k
)
1554
75.4k
    bmap = remove_dependent_vars(bmap, d);
1555
72.7k
  if (!bmap)
1556
0
    return NULL;
1557
72.7k
1558
72.7k
  for (d = pos + n - 1;
1559
129k
       
d >= 0 && 129k
d >= total - bmap->n_div119k
&&
d >= pos70.8k
;
--d56.5k
)
1560
56.5k
    isl_seq_clr(bmap->div[d-(total-bmap->n_div)], 2+total);
1561
148k
  for (d = pos + n - 1; 
d >= 0 && 148k
d >= pos132k
;
--d75.3k
)
{75.4k
1562
75.4k
    int n_lower, n_upper;
1563
75.4k
    if (!bmap)
1564
0
      return NULL;
1565
97.6k
    
for (i = 0; 75.4k
i < bmap->n_eq97.6k
;
++i22.1k
)
{26.5k
1566
26.5k
      if (isl_int_is_zero(bmap->eq[i][1+d]))
1567
22.1k
        continue;
1568
4.42k
      eliminate_var_using_equality(bmap, d, bmap->eq[i], 0, NULL);
1569
4.42k
      isl_basic_map_drop_equality(bmap, i);
1570
4.42k
      need_gauss = 1;
1571
4.42k
      break;
1572
26.5k
    }
1573
75.4k
    if (i < bmap->n_eq)
1574
1.66k
      continue;
1575
73.8k
    n_lower = 0;
1576
73.8k
    n_upper = 0;
1577
238k
    for (i = 0; 
i < bmap->n_ineq238k
;
++i164k
)
{164k
1578
164k
      if (isl_int_is_pos(bmap->ineq[i][1+d]))
1579
28.1k
        n_lower++;
1580
136k
      else 
if (136k
isl_int_is_neg136k
(bmap->ineq[i][1+d]))
1581
26.8k
        n_upper++;
1582
164k
    }
1583
73.8k
    bmap = isl_basic_map_extend_constraints(bmap,
1584
73.8k
        0, n_lower * n_upper);
1585
73.8k
    if (!bmap)
1586
0
      goto error;
1587
193k
    
for (i = bmap->n_ineq - 1; 73.8k
i >= 0193k
;
--i119k
)
{119k
1588
119k
      int last;
1589
119k
      if (isl_int_is_zero(bmap->ineq[i][1+d]))
1590
64.4k
        continue;
1591
55.0k
      last = -1;
1592
208k
      for (j = 0; 
j < i208k
;
++j153k
)
{153k
1593
153k
        if (isl_int_is_zero(bmap->ineq[j][1+d]))
1594
106k
          continue;
1595
46.6k
        last = j;
1596
46.6k
        if (
isl_int_sgn46.6k
(bmap->ineq[i][1+d]) ==46.6k
1597
46.6k
            isl_int_sgn(bmap->ineq[j][1+d]))
1598
10.8k
          continue;
1599
35.7k
        k = isl_basic_map_alloc_inequality(bmap);
1600
35.7k
        if (k < 0)
1601
0
          goto error;
1602
35.7k
        isl_seq_cpy(bmap->ineq[k], bmap->ineq[i],
1603
35.7k
            1+total);
1604
35.7k
        isl_seq_elim(bmap->ineq[k], bmap->ineq[j],
1605
35.7k
            1+d, 1+total, NULL);
1606
35.7k
      }
1607
55.0k
      isl_basic_map_drop_inequality(bmap, i);
1608
55.0k
      i = last + 1;
1609
55.0k
    }
1610
73.8k
    
if (73.8k
n_lower > 0 && 73.8k
n_upper > 023.7k
)
{22.6k
1611
22.6k
      bmap = isl_basic_map_normalize_constraints(bmap);
1612
22.6k
      bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1613
22.6k
                    NULL, 0);
1614
22.6k
      bmap = isl_basic_map_gauss(bmap, NULL);
1615
22.6k
      bmap = isl_basic_map_remove_redundancies(bmap);
1616
22.6k
      need_gauss = 0;
1617
22.6k
      if (!bmap)
1618
0
        goto error;
1619
22.6k
      
if (22.6k
ISL_F_ISSET22.6k
(bmap, ISL_BASIC_MAP_EMPTY))
1620
130
        break;
1621
22.6k
    }
1622
73.8k
  }
1623
72.7k
  
ISL_F_CLR72.7k
(bmap, ISL_BASIC_MAP_NORMALIZED);72.7k
1624
72.7k
  if (need_gauss)
1625
1.84k
    bmap = isl_basic_map_gauss(bmap, NULL);
1626
72.7k
  return bmap;
1627
0
error:
1628
0
  isl_basic_map_free(bmap);
1629
0
  return NULL;
1630
72.7k
}
1631
1632
struct isl_basic_set *isl_basic_set_eliminate_vars(
1633
  struct isl_basic_set *bset, unsigned pos, unsigned n)
1634
14.9k
{
1635
14.9k
  return bset_from_bmap(isl_basic_map_eliminate_vars(bset_to_bmap(bset),
1636
14.9k
                pos, n));
1637
14.9k
}
1638
1639
/* Eliminate the specified n dimensions starting at first from the
1640
 * constraints, without removing the dimensions from the space.
1641
 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1642
 * Otherwise, they are projected out and the original space is restored.
1643
 */
1644
__isl_give isl_basic_map *isl_basic_map_eliminate(
1645
  __isl_take isl_basic_map *bmap,
1646
  enum isl_dim_type type, unsigned first, unsigned n)
1647
9.70k
{
1648
9.70k
  isl_space *space;
1649
9.70k
1650
9.70k
  if (!bmap)
1651
0
    return NULL;
1652
9.70k
  
if (9.70k
n == 09.70k
)
1653
0
    return bmap;
1654
9.70k
1655
9.70k
  
if (9.70k
first + n > isl_basic_map_dim(bmap, type) || 9.70k
first + n < first9.70k
)
1656
0
    isl_die(bmap->ctx, isl_error_invalid,
1657
9.70k
      "index out of bounds", goto error);
1658
9.70k
1659
9.70k
  
if (9.70k
ISL_F_ISSET9.70k
(bmap, ISL_BASIC_MAP_RATIONAL))
{0
1660
0
    first += isl_basic_map_offset(bmap, type) - 1;
1661
0
    bmap = isl_basic_map_eliminate_vars(bmap, first, n);
1662
0
    return isl_basic_map_finalize(bmap);
1663
0
  }
1664
9.70k
1665
9.70k
  space = isl_basic_map_get_space(bmap);
1666
9.70k
  bmap = isl_basic_map_project_out(bmap, type, first, n);
1667
9.70k
  bmap = isl_basic_map_insert_dims(bmap, type, first, n);
1668
9.70k
  bmap = isl_basic_map_reset_space(bmap, space);
1669
9.70k
  return bmap;
1670
0
error:
1671
0
  isl_basic_map_free(bmap);
1672
0
  return NULL;
1673
9.70k
}
1674
1675
__isl_give isl_basic_set *isl_basic_set_eliminate(
1676
  __isl_take isl_basic_set *bset,
1677
  enum isl_dim_type type, unsigned first, unsigned n)
1678
3.20k
{
1679
3.20k
  return isl_basic_map_eliminate(bset, type, first, n);
1680
3.20k
}
1681
1682
/* Remove all constraints from "bmap" that reference any unknown local
1683
 * variables (directly or indirectly).
1684
 *
1685
 * Dropping all constraints on a local variable will make it redundant,
1686
 * so it will get removed implicitly by
1687
 * isl_basic_map_drop_constraints_involving_dims.  Some other local
1688
 * variables may also end up becoming redundant if they only appear
1689
 * in constraints together with the unknown local variable.
1690
 * Therefore, start over after calling
1691
 * isl_basic_map_drop_constraints_involving_dims.
1692
 */
1693
__isl_give isl_basic_map *isl_basic_map_drop_constraint_involving_unknown_divs(
1694
  __isl_take isl_basic_map *bmap)
1695
6.02k
{
1696
6.02k
  isl_bool known;
1697
6.02k
  int i, n_div, o_div;
1698
6.02k
1699
6.02k
  known = isl_basic_map_divs_known(bmap);
1700
6.02k
  if (known < 0)
1701
0
    return isl_basic_map_free(bmap);
1702
6.02k
  
if (6.02k
known6.02k
)
1703
6.02k
    return bmap;
1704
6.02k
1705
0
  n_div = isl_basic_map_dim(bmap, isl_dim_div);
1706
0
  o_div = isl_basic_map_offset(bmap, isl_dim_div) - 1;
1707
0
1708
0
  for (i = 0; 
i < n_div0
;
++i0
)
{0
1709
0
    known = isl_basic_map_div_is_known(bmap, i);
1710
0
    if (known < 0)
1711
0
      return isl_basic_map_free(bmap);
1712
0
    
if (0
known0
)
1713
0
      continue;
1714
0
    bmap = remove_dependent_vars(bmap, o_div + i);
1715
0
    bmap = isl_basic_map_drop_constraints_involving_dims(bmap,
1716
0
                  isl_dim_div, i, 1);
1717
0
    if (!bmap)
1718
0
      return NULL;
1719
0
    n_div = isl_basic_map_dim(bmap, isl_dim_div);
1720
0
    i = -1;
1721
0
  }
1722
0
1723
0
  return bmap;
1724
0
}
1725
1726
/* Remove all constraints from "map" that reference any unknown local
1727
 * variables (directly or indirectly).
1728
 *
1729
 * Since constraints may get dropped from the basic maps,
1730
 * they may no longer be disjoint from each other.
1731
 */
1732
__isl_give isl_map *isl_map_drop_constraint_involving_unknown_divs(
1733
  __isl_take isl_map *map)
1734
974
{
1735
974
  int i;
1736
974
  isl_bool known;
1737
974
1738
974
  known = isl_map_divs_known(map);
1739
974
  if (known < 0)
1740
0
    return isl_map_free(map);
1741
974
  
if (974
known974
)
1742
974
    return map;
1743
974
1744
0
  map = isl_map_cow(map);
1745
0
  if (!map)
1746
0
    return NULL;
1747
0
1748
0
  
for (i = 0; 0
i < map->n0
;
++i0
)
{0
1749
0
    map->p[i] =
1750
0
        isl_basic_map_drop_constraint_involving_unknown_divs(
1751
0
                    map->p[i]);
1752
0
    if (!map->p[i])
1753
0
      return isl_map_free(map);
1754
0
  }
1755
0
1756
0
  
if (0
map->n > 10
)
1757
0
    ISL_F_CLR(map, ISL_MAP_DISJOINT);
1758
0
1759
0
  return map;
1760
0
}
1761
1762
/* Don't assume equalities are in order, because align_divs
1763
 * may have changed the order of the divs.
1764
 */
1765
static void compute_elimination_index(__isl_keep isl_basic_map *bmap, int *elim)
1766
17.0k
{
1767
17.0k
  int d, i;
1768
17.0k
  unsigned total;
1769
17.0k
1770
17.0k
  total = isl_space_dim(bmap->dim, isl_dim_all);
1771
133k
  for (d = 0; 
d < total133k
;
++d116k
)
1772
116k
    elim[d] = -1;
1773
38.0k
  for (i = 0; 
i < bmap->n_eq38.0k
;
++i21.0k
)
{21.0k
1774
65.1k
    for (d = total - 1; 
d >= 065.1k
;
--d44.0k
)
{65.1k
1775
65.1k
      if (isl_int_is_zero(bmap->eq[i][1+d]))
1776
44.0k
        continue;
1777
21.0k
      elim[d] = i;
1778
21.0k
      break;
1779
65.1k
    }
1780
21.0k
  }
1781
17.0k
}
1782
1783
static void set_compute_elimination_index(__isl_keep isl_basic_set *bset,
1784
  int *elim)
1785
249
{
1786
249
  compute_elimination_index(bset_to_bmap(bset), elim);
1787
249
}
1788
1789
static int reduced_using_equalities(isl_int *dst, isl_int *src,
1790
  __isl_keep isl_basic_map *bmap, int *elim)
1791
127k
{
1792
127k
  int d;
1793
127k
  int copied = 0;
1794
127k
  unsigned total;
1795
127k
1796
127k
  total = isl_space_dim(bmap->dim, isl_dim_all);
1797
1.18M
  for (d = total - 1; 
d >= 01.18M
;
--d1.05M
)
{1.05M
1798
1.05M
    if (isl_int_is_zero(src[1+d]))
1799
914k
      continue;
1800
144k
    
if (144k
elim[d] == -1144k
)
1801
130k
      continue;
1802
13.9k
    
if (13.9k
!copied13.9k
)
{13.3k
1803
13.3k
      isl_seq_cpy(dst, src, 1 + total);
1804
13.3k
      copied = 1;
1805
13.3k
    }
1806
13.9k
    isl_seq_elim(dst, bmap->eq[elim[d]], 1 + d, 1 + total, NULL);
1807
13.9k
  }
1808
127k
  return copied;
1809
127k
}
1810
1811
static int set_reduced_using_equalities(isl_int *dst, isl_int *src,
1812
  __isl_keep isl_basic_set *bset, int *elim)
1813
545
{
1814
545
  return reduced_using_equalities(dst, src,
1815
545
          bset_to_bmap(bset), elim);
1816
545
}
1817
1818
static __isl_give isl_basic_set *isl_basic_set_reduce_using_equalities(
1819
  __isl_take isl_basic_set *bset, __isl_take isl_basic_set *context)
1820
7.08k
{
1821
7.08k
  int i;
1822
7.08k
  int *elim;
1823
7.08k
1824
7.08k
  if (
!bset || 7.08k
!context7.08k
)
1825
0
    goto error;
1826
7.08k
1827
7.08k
  
if (7.08k
context->n_eq == 07.08k
)
{6.83k
1828
6.83k
    isl_basic_set_free(context);
1829
6.83k
    return bset;
1830
6.83k
  }
1831
7.08k
1832
249
  bset = isl_basic_set_cow(bset);
1833
249
  if (!bset)
1834
0
    goto error;
1835
249
1836
249
  
elim = 249
isl_alloc_array249
(bset->ctx, int, isl_basic_set_n_dim(bset));
1837
249
  if (!elim)
1838
0
    goto error;
1839
249
  set_compute_elimination_index(context, elim);
1840
725
  for (i = 0; 
i < bset->n_eq725
;
++i476
)
1841
476
    set_reduced_using_equalities(bset->eq[i], bset->eq[i],
1842
476
              context, elim);
1843
318
  for (i = 0; 
i < bset->n_ineq318
;
++i69
)
1844
69
    set_reduced_using_equalities(bset->ineq[i], bset->ineq[i],
1845
69
              context, elim);
1846
249
  isl_basic_set_free(context);
1847
249
  free(elim);
1848
249
  bset = isl_basic_set_simplify(bset);
1849
249
  bset = isl_basic_set_finalize(bset);
1850
249
  return bset;
1851
0
error:
1852
0
  isl_basic_set_free(bset);
1853
0
  isl_basic_set_free(context);
1854
0
  return NULL;
1855
249
}
1856
1857
/* For each inequality in "ineq" that is a shifted (more relaxed)
1858
 * copy of an inequality in "context", mark the corresponding entry
1859
 * in "row" with -1.
1860
 * If an inequality only has a non-negative constant term, then
1861
 * mark it as well.
1862
 */
1863
static isl_stat mark_shifted_constraints(__isl_keep isl_mat *ineq,
1864
  __isl_keep isl_basic_set *context, int *row)
1865
10.3k
{
1866
10.3k
  struct isl_constraint_index ci;
1867
10.3k
  int n_ineq;
1868
10.3k
  unsigned total;
1869
10.3k
  int k;
1870
10.3k
1871
10.3k
  if (
!ineq || 10.3k
!context10.3k
)
1872
0
    return isl_stat_error;
1873
10.3k
  
if (10.3k
context->n_ineq == 010.3k
)
1874
0
    return isl_stat_ok;
1875
10.3k
  
if (10.3k
setup_constraint_index(&ci, context) < 010.3k
)
1876
0
    return isl_stat_error;
1877
10.3k
1878
10.3k
  n_ineq = isl_mat_rows(ineq);
1879
10.3k
  total = isl_mat_cols(ineq) - 1;
1880
37.1k
  for (k = 0; 
k < n_ineq37.1k
;
++k26.8k
)
{26.8k
1881
26.8k
    int l;
1882
26.8k
    isl_bool redundant;
1883
26.8k
1884
26.8k
    l = isl_seq_first_non_zero(ineq->row[k] + 1, total);
1885
26.8k
    if (
l < 0 && 26.8k
isl_int_is_nonneg4
(ineq->row[k][0]))
{0
1886
0
      row[k] = -1;
1887
0
      continue;
1888
0
    }
1889
26.8k
    redundant = constraint_index_is_redundant(&ci, ineq->row[k]);
1890
26.8k
    if (redundant < 0)
1891
0
      goto error;
1892
26.8k
    
if (26.8k
!redundant26.8k
)
1893
18.1k
      continue;
1894
8.69k
    row[k] = -1;
1895
8.69k
  }
1896
10.3k
  constraint_index_free(&ci);
1897
10.3k
  return isl_stat_ok;
1898
0
error:
1899
0
  constraint_index_free(&ci);
1900
0
  return isl_stat_error;
1901
10.3k
}
1902
1903
static __isl_give isl_basic_set *remove_shifted_constraints(
1904
  __isl_take isl_basic_set *bset, __isl_keep isl_basic_set *context)
1905
33
{
1906
33
  struct isl_constraint_index ci;
1907
33
  int k;
1908
33
1909
33
  if (
!bset || 33
!context33
)
1910
0
    return bset;
1911
33
1912
33
  
if (33
context->n_ineq == 033
)
1913
0
    return bset;
1914
33
  
if (33
setup_constraint_index(&ci, context) < 033
)
1915
0
    return bset;
1916
33
1917
271
  
for (k = 0; 33
k < bset->n_ineq271
;
++k238
)
{238
1918
238
    isl_bool redundant;
1919
238
1920
238
    redundant = constraint_index_is_redundant(&ci, bset->ineq[k]);
1921
238
    if (redundant < 0)
1922
0
      goto error;
1923
238
    
if (238
!redundant238
)
1924
81
      continue;
1925
157
    bset = isl_basic_set_cow(bset);
1926
157
    if (!bset)
1927
0
      goto error;
1928
157
    isl_basic_set_drop_inequality(bset, k);
1929
157
    --k;
1930
157
  }
1931
33
  constraint_index_free(&ci);
1932
33
  return bset;
1933
0
error:
1934
0
  constraint_index_free(&ci);
1935
0
  return bset;
1936
33
}
1937
1938
/* Remove constraints from "bmap" that are identical to constraints
1939
 * in "context" or that are more relaxed (greater constant term).
1940
 *
1941
 * We perform the test for shifted copies on the pure constraints
1942
 * in remove_shifted_constraints.
1943
 */
1944
static __isl_give isl_basic_map *isl_basic_map_remove_shifted_constraints(
1945
  __isl_take isl_basic_map *bmap, __isl_take isl_basic_map *context)
1946
434
{
1947
434
  isl_basic_set *bset, *bset_context;
1948
434
1949
434
  if (
!bmap || 434
!context434
)
1950
0
    goto error;
1951
434
1952
434
  
if (434
bmap->n_ineq == 0 || 434
context->n_ineq == 033
)
{401
1953
401
    isl_basic_map_free(context);
1954
401
    return bmap;
1955
401
  }
1956
434
1957
33
  context = isl_basic_map_align_divs(context, bmap);
1958
33
  bmap = isl_basic_map_align_divs(bmap, context);
1959
33
1960
33
  bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
1961
33
  bset_context = isl_basic_map_underlying_set(context);
1962
33
  bset = remove_shifted_constraints(bset, bset_context);
1963
33
  isl_basic_set_free(bset_context);
1964
33
1965
33
  bmap = isl_basic_map_overlying_set(bset, bmap);
1966
33
1967
33
  return bmap;
1968
0
error:
1969
0
  isl_basic_map_free(bmap);
1970
0
  isl_basic_map_free(context);
1971
0
  return NULL;
1972
434
}
1973
1974
/* Does the (linear part of a) constraint "c" involve any of the "len"
1975
 * "relevant" dimensions?
1976
 */
1977
static int is_related(isl_int *c, int len, int *relevant)
1978
157k
{
1979
157k
  int i;
1980
157k
1981
1.93M
  for (i = 0; 
i < len1.93M
;
++i1.77M
)
{1.84M
1982
1.84M
    if (!relevant[i])
1983
1.40M
      continue;
1984
433k
    
if (433k
!433k
isl_int_is_zero433k
(c[i]))
1985
64.5k
      return 1;
1986
433k
  }
1987
157k
1988
92.7k
  return 0;
1989
157k
}
1990
1991
/* Drop constraints from "bmap" that do not involve any of
1992
 * the dimensions marked "relevant".
1993
 */
1994
static __isl_give isl_basic_map *drop_unrelated_constraints(
1995
  __isl_take isl_basic_map *bmap, int *relevant)
1996
49.9k
{
1997
49.9k
  int i, dim;
1998
49.9k
1999
49.9k
  dim = isl_basic_map_dim(bmap, isl_dim_all);
2000
209k
  for (i = 0; 
i < dim209k
;
++i159k
)
2001
188k
    
if (188k
!relevant[i]188k
)
2002
29.3k
      break;
2003
49.9k
  if (i >= dim)
2004
20.6k
    return bmap;
2005
49.9k
2006
47.1k
  
for (i = bmap->n_eq - 1; 29.3k
i >= 047.1k
;
--i17.8k
)
2007
17.8k
    
if (17.8k
!is_related(bmap->eq[i] + 1, dim, relevant)17.8k
)
{8.91k
2008
8.91k
      bmap = isl_basic_map_cow(bmap);
2009
8.91k
      if (isl_basic_map_drop_equality(bmap, i) < 0)
2010
0
        return isl_basic_map_free(bmap);
2011
8.91k
    }
2012
29.3k
2013
168k
  
for (i = bmap->n_ineq - 1; 29.3k
i >= 0168k
;
--i139k
)
2014
139k
    
if (139k
!is_related(bmap->ineq[i] + 1, dim, relevant)139k
)
{83.8k
2015
83.8k
      bmap = isl_basic_map_cow(bmap);
2016
83.8k
      if (isl_basic_map_drop_inequality(bmap, i) < 0)
2017
0
        return isl_basic_map_free(bmap);
2018
83.8k
    }
2019
29.3k
2020
29.3k
  return bmap;
2021
29.3k
}
2022
2023
/* Update the groups in "group" based on the (linear part of a) constraint "c".
2024
 *
2025
 * In particular, for any variable involved in the constraint,
2026
 * find the actual group id from before and replace the group
2027
 * of the corresponding variable by the minimal group of all
2028
 * the variables involved in the constraint considered so far
2029
 * (if this minimum is smaller) or replace the minimum by this group
2030
 * (if the minimum is larger).
2031
 *
2032
 * At the end, all the variables in "c" will (indirectly) point
2033
 * to the minimal of the groups that they referred to originally.
2034
 */
2035
static void update_groups(int dim, int *group, isl_int *c)
2036
318k
{
2037
318k
  int j;
2038
318k
  int min = dim;
2039
318k
2040
4.02M
  for (j = 0; 
j < dim4.02M
;
++j3.71M
)
{3.71M
2041
3.71M
    if (isl_int_is_zero(c[j]))
2042
3.29M
      continue;
2043
419k
    
while (419k
group[j] >= 0 && 419k
group[group[j]] != group[j]151k
)
2044
77
      group[j] = group[group[j]];
2045
419k
    if (group[j] == min)
2046
55.1k
      continue;
2047
364k
    
if (364k
group[j] < min364k
)
{322k
2048
322k
      if (
min >= 0 && 322k
min < dim322k
)
2049
4.00k
        group[min] = group[j];
2050
322k
      min = group[j];
2051
322k
    } else
2052
41.4k
      group[group[j]] = min;
2053
364k
  }
2054
318k
}
2055
2056
/* Allocate an array of groups of variables, one for each variable
2057
 * in "context", initialized to zero.
2058
 */
2059
static int *alloc_groups(__isl_keep isl_basic_set *context)
2060
24.3k
{
2061
24.3k
  isl_ctx *ctx;
2062
24.3k
  int dim;
2063
24.3k
2064
24.3k
  dim = isl_basic_set_dim(context, isl_dim_set);
2065
24.3k
  ctx = isl_basic_set_get_ctx(context);
2066
24.3k
  return isl_calloc_array(ctx, int, dim);
2067
24.3k
}
2068
2069
/* Drop constraints from "bmap" that only involve variables that are
2070
 * not related to any of the variables marked with a "-1" in "group".
2071
 *
2072
 * We construct groups of variables that collect variables that
2073
 * (indirectly) appear in some common constraint of "bmap".
2074
 * Each group is identified by the first variable in the group,
2075
 * except for the special group of variables that was already identified
2076
 * in the input as -1 (or are related to those variables).
2077
 * If group[i] is equal to i (or -1), then the group of i is i (or -1),
2078
 * otherwise the group of i is the group of group[i].
2079
 *
2080
 * We first initialize groups for the remaining variables.
2081
 * Then we iterate over the constraints of "bmap" and update the
2082
 * group of the variables in the constraint by the smallest group.
2083
 * Finally, we resolve indirect references to groups by running over
2084
 * the variables.
2085
 *
2086
 * After computing the groups, we drop constraints that do not involve
2087
 * any variables in the -1 group.
2088
 */
2089
__isl_give isl_basic_map *isl_basic_map_drop_unrelated_constraints(
2090
  __isl_take isl_basic_map *bmap, __isl_take int *group)
2091
61.6k
{
2092
61.6k
  int dim;
2093
61.6k
  int i;
2094
61.6k
  int last;
2095
61.6k
2096
61.6k
  if (!bmap)
2097
0
    return NULL;
2098
61.6k
2099
61.6k
  dim = isl_basic_map_dim(bmap, isl_dim_all);
2100
61.6k
2101
61.6k
  last = -1;
2102
355k
  for (i = 0; 
i < dim355k
;
++i293k
)
2103
293k
    
if (293k
group[i] >= 0293k
)
2104
126k
      last = group[i] = i;
2105
61.6k
  if (
last < 061.6k
)
{11.6k
2106
11.6k
    free(group);
2107
11.6k
    return bmap;
2108
11.6k
  }
2109
61.6k
2110
103k
  
for (i = 0; 49.9k
i < bmap->n_eq103k
;
++i53.4k
)
2111
53.4k
    update_groups(dim, group, bmap->eq[i] + 1);
2112
315k
  for (i = 0; 
i < bmap->n_ineq315k
;
++i265k
)
2113
265k
    update_groups(dim, group, bmap->ineq[i] + 1);
2114
49.9k
2115
316k
  for (i = 0; 
i < dim316k
;
++i266k
)
2116
266k
    
if (266k
group[i] >= 0266k
)
2117
83.6k
      group[i] = group[group[i]];
2118
49.9k
2119
316k
  for (i = 0; 
i < dim316k
;
++i266k
)
2120
266k
    group[i] = group[i] == -1;
2121
49.9k
2122
49.9k
  bmap = drop_unrelated_constraints(bmap, group);
2123
49.9k
2124
49.9k
  free(group);
2125
49.9k
  return bmap;
2126
61.6k
}
2127
2128
/* Drop constraints from "context" that are irrelevant for computing
2129
 * the gist of "bset".
2130
 *
2131
 * In particular, drop constraints in variables that are not related
2132
 * to any of the variables involved in the constraints of "bset"
2133
 * in the sense that there is no sequence of constraints that connects them.
2134
 *
2135
 * We first mark all variables that appear in "bset" as belonging
2136
 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2137
 */
2138
static __isl_give isl_basic_set *drop_irrelevant_constraints(
2139
  __isl_take isl_basic_set *context, __isl_keep isl_basic_set *bset)
2140
15.3k
{
2141
15.3k
  int *group;
2142
15.3k
  int dim;
2143
15.3k
  int i, j;
2144
15.3k
2145
15.3k
  if (
!context || 15.3k
!bset15.3k
)
2146
0
    return isl_basic_set_free(context);
2147
15.3k
2148
15.3k
  group = alloc_groups(context);
2149
15.3k
2150
15.3k
  if (!group)
2151
0
    return isl_basic_set_free(context);
2152
15.3k
2153
15.3k
  dim = isl_basic_set_dim(bset, isl_dim_set);
2154
91.1k
  for (i = 0; 
i < dim91.1k
;
++i75.8k
)
{75.8k
2155
123k
    for (j = 0; 
j < bset->n_eq123k
;
++j47.4k
)
2156
66.3k
      
if (66.3k
!66.3k
isl_int_is_zero66.3k
(bset->eq[j][1 + i]))
2157
18.9k
        break;
2158
75.8k
    if (
j < bset->n_eq75.8k
)
{18.9k
2159
18.9k
      group[i] = -1;
2160
18.9k
      continue;
2161
18.9k
    }
2162
185k
    
for (j = 0; 56.9k
j < bset->n_ineq185k
;
++j128k
)
2163
149k
      
if (149k
!149k
isl_int_is_zero149k
(bset->ineq[j][1 + i]))
2164
20.4k
        break;
2165
56.9k
    if (j < bset->n_ineq)
2166
20.4k
      group[i] = -1;
2167
56.9k
  }
2168
15.3k
2169
15.3k
  return isl_basic_map_drop_unrelated_constraints(context, group);
2170
15.3k
}
2171
2172
/* Drop constraints from "context" that are irrelevant for computing
2173
 * the gist of the inequalities "ineq".
2174
 * Inequalities in "ineq" for which the corresponding element of row
2175
 * is set to -1 have already been marked for removal and should be ignored.
2176
 *
2177
 * In particular, drop constraints in variables that are not related
2178
 * to any of the variables involved in "ineq"
2179
 * in the sense that there is no sequence of constraints that connects them.
2180
 *
2181
 * We first mark all variables that appear in "bset" as belonging
2182
 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2183
 */
2184
static __isl_give isl_basic_set *drop_irrelevant_constraints_marked(
2185
  __isl_take isl_basic_set *context, __isl_keep isl_mat *ineq, int *row)
2186
9.05k
{
2187
9.05k
  int *group;
2188
9.05k
  int dim;
2189
9.05k
  int i, j, n;
2190
9.05k
2191
9.05k
  if (
!context || 9.05k
!ineq9.05k
)
2192
0
    return isl_basic_set_free(context);
2193
9.05k
2194
9.05k
  group = alloc_groups(context);
2195
9.05k
2196
9.05k
  if (!group)
2197
0
    return isl_basic_set_free(context);
2198
9.05k
2199
9.05k
  dim = isl_basic_set_dim(context, isl_dim_set);
2200
9.05k
  n = isl_mat_rows(ineq);
2201
54.1k
  for (i = 0; 
i < dim54.1k
;
++i45.1k
)
{45.1k
2202
177k
    for (j = 0; 
j < n177k
;
++j132k
)
{150k
2203
150k
      if (row[j] < 0)
2204
29.1k
        continue;
2205
121k
      
if (121k
!121k
isl_int_is_zero121k
(ineq->row[j][1 + i]))
2206
18.0k
        break;
2207
121k
    }
2208
45.1k
    if (j < n)
2209
18.0k
      group[i] = -1;
2210
45.1k
  }
2211
9.05k
2212
9.05k
  return isl_basic_map_drop_unrelated_constraints(context, group);
2213
9.05k
}
2214
2215
/* Do all "n" entries of "row" contain a negative value?
2216
 */
2217
static int all_neg(int *row, int n)
2218
10.3k
{
2219
10.3k
  int i;
2220
10.3k
2221
17.0k
  for (i = 0; 
i < n17.0k
;
++i6.72k
)
2222
15.7k
    
if (15.7k
row[i] >= 015.7k
)
2223
9.05k
      return 0;
2224
10.3k
2225
1.25k
  return 1;
2226
10.3k
}
2227
2228
/* Update the inequalities in "bset" based on the information in "row"
2229
 * and "tab".
2230
 *
2231
 * In particular, the array "row" contains either -1, meaning that
2232
 * the corresponding inequality of "bset" is redundant, or the index
2233
 * of an inequality in "tab".
2234
 *
2235
 * If the row entry is -1, then drop the inequality.
2236
 * Otherwise, if the constraint is marked redundant in the tableau,
2237
 * then drop the inequality.  Similarly, if it is marked as an equality
2238
 * in the tableau, then turn the inequality into an equality and
2239
 * perform Gaussian elimination.
2240
 */
2241
static __isl_give isl_basic_set *update_ineq(__isl_take isl_basic_set *bset,
2242
  __isl_keep int *row, struct isl_tab *tab)
2243
10.3k
{
2244
10.3k
  int i;
2245
10.3k
  unsigned n_ineq;
2246
10.3k
  unsigned n_eq;
2247
10.3k
  int found_equality = 0;
2248
10.3k
2249
10.3k
  if (!bset)
2250
0
    return NULL;
2251
10.3k
  
if (10.3k
tab && 10.3k
tab->empty8.81k
)
2252
2.31k
    return isl_basic_set_set_to_empty(bset);
2253
10.3k
2254
7.99k
  n_ineq = bset->n_ineq;
2255
29.1k
  for (i = n_ineq - 1; 
i >= 029.1k
;
--i21.1k
)
{21.1k
2256
21.1k
    if (
row[i] < 021.1k
)
{8.11k
2257
8.11k
      if (isl_basic_set_drop_inequality(bset, i) < 0)
2258
0
        return isl_basic_set_free(bset);
2259
8.11k
      continue;
2260
8.11k
    }
2261
13.0k
    
if (13.0k
!tab13.0k
)
2262
423
      continue;
2263
12.6k
    n_eq = tab->n_eq;
2264
12.6k
    if (
isl_tab_is_equality(tab, n_eq + row[i])12.6k
)
{173
2265
173
      isl_basic_map_inequality_to_equality(bset, i);
2266
173
      found_equality = 1;
2267
12.4k
    } else 
if (12.4k
isl_tab_is_redundant(tab, n_eq + row[i])12.4k
)
{260
2268
260
      if (isl_basic_set_drop_inequality(bset, i) < 0)
2269
0
        return isl_basic_set_free(bset);
2270
260
    }
2271
12.6k
  }
2272
7.99k
2273
7.99k
  
if (7.99k
found_equality7.99k
)
2274
164
    bset = isl_basic_set_gauss(bset, NULL);
2275
7.99k
  bset = isl_basic_set_finalize(bset);
2276
7.99k
  return bset;
2277
7.99k
}
2278
2279
/* Update the inequalities in "bset" based on the information in "row"
2280
 * and "tab" and free all arguments (other than "bset").
2281
 */
2282
static __isl_give isl_basic_set *update_ineq_free(
2283
  __isl_take isl_basic_set *bset, __isl_take isl_mat *ineq,
2284
  __isl_take isl_basic_set *context, __isl_take int *row,
2285
  struct isl_tab *tab)
2286
10.3k
{
2287
10.3k
  isl_mat_free(ineq);
2288
10.3k
  isl_basic_set_free(context);
2289
10.3k
2290
10.3k
  bset = update_ineq(bset, row, tab);
2291
10.3k
2292
10.3k
  free(row);
2293
10.3k
  isl_tab_free(tab);
2294
10.3k
  return bset;
2295
10.3k
}
2296
2297
/* Remove all information from bset that is redundant in the context
2298
 * of context.
2299
 * "ineq" contains the (possibly transformed) inequalities of "bset",
2300
 * in the same order.
2301
 * The (explicit) equalities of "bset" are assumed to have been taken
2302
 * into account by the transformation such that only the inequalities
2303
 * are relevant.
2304
 * "context" is assumed not to be empty.
2305
 *
2306
 * "row" keeps track of the constraint index of a "bset" inequality in "tab".
2307
 * A value of -1 means that the inequality is obviously redundant and may
2308
 * not even appear in  "tab".
2309
 *
2310
 * We first mark the inequalities of "bset"
2311
 * that are obviously redundant with respect to some inequality in "context".
2312
 * Then we remove those constraints from "context" that have become
2313
 * irrelevant for computing the gist of "bset".
2314
 * Note that this removal of constraints cannot be replaced by
2315
 * a factorization because factors in "bset" may still be connected
2316
 * to each other through constraints in "context".
2317
 *
2318
 * If there are any inequalities left, we construct a tableau for
2319
 * the context and then add the inequalities of "bset".
2320
 * Before adding these inequalities, we freeze all constraints such that
2321
 * they won't be considered redundant in terms of the constraints of "bset".
2322
 * Then we detect all redundant constraints (among the
2323
 * constraints that weren't frozen), first by checking for redundancy in the
2324
 * the tableau and then by checking if replacing a constraint by its negation
2325
 * would lead to an empty set.  This last step is fairly expensive
2326
 * and could be optimized by more reuse of the tableau.
2327
 * Finally, we update bset according to the results.
2328
 */
2329
static __isl_give isl_basic_set *uset_gist_full(__isl_take isl_basic_set *bset,
2330
  __isl_take isl_mat *ineq, __isl_take isl_basic_set *context)
2331
15.2k
{
2332
15.2k
  int i, r;
2333
15.2k
  int *row = NULL;
2334
15.2k
  isl_ctx *ctx;
2335
15.2k
  isl_basic_set *combined = NULL;
2336
15.2k
  struct isl_tab *tab = NULL;
2337
15.2k
  unsigned n_eq, context_ineq;
2338
15.2k
2339
15.2k
  if (
!bset || 15.2k
!ineq15.2k
||
!context15.2k
)
2340
0
    goto error;
2341
15.2k
2342
15.2k
  
if (15.2k
bset->n_ineq == 0 || 15.2k
isl_basic_set_plain_is_universe(context)10.8k
)
{4.98k
2343
4.98k
    isl_basic_set_free(context);
2344
4.98k
    isl_mat_free(ineq);
2345
4.98k
    return bset;
2346
4.98k
  }
2347
15.2k
2348
10.3k
  ctx = isl_basic_set_get_ctx(context);
2349
10.3k
  row = isl_calloc_array(ctx, int, bset->n_ineq);
2350
10.3k
  if (!row)
2351
0
    goto error;
2352
10.3k
2353
10.3k
  
if (10.3k
mark_shifted_constraints(ineq, context, row) < 010.3k
)
2354
0
    goto error;
2355
10.3k
  
if (10.3k
all_neg(row, bset->n_ineq)10.3k
)
2356
1.25k
    return update_ineq_free(bset, ineq, context, row, NULL);
2357
10.3k
2358
9.05k
  context = drop_irrelevant_constraints_marked(context, ineq, row);
2359
9.05k
  if (!context)
2360
0
    goto error;
2361
9.05k
  
if (9.05k
isl_basic_set_plain_is_universe(context)9.05k
)
2362
238
    return update_ineq_free(bset, ineq, context, row, NULL);
2363
9.05k
2364
8.81k
  n_eq = context->n_eq;
2365
8.81k
  context_ineq = context->n_ineq;
2366
8.81k
  combined = isl_basic_set_cow(isl_basic_set_copy(context));
2367
8.81k
  combined = isl_basic_set_extend_constraints(combined, 0, bset->n_ineq);
2368
8.81k
  tab = isl_tab_from_basic_set(combined, 0);
2369
35.7k
  for (i = 0; 
i < context_ineq35.7k
;
++i26.9k
)
2370
26.9k
    
if (26.9k
isl_tab_freeze_constraint(tab, n_eq + i) < 026.9k
)
2371
0
      goto error;
2372
8.81k
  
if (8.81k
isl_tab_extend_cons(tab, bset->n_ineq) < 08.81k
)
2373
0
    goto error;
2374
8.81k
  r = context_ineq;
2375
31.1k
  for (i = 0; 
i < bset->n_ineq31.1k
;
++i22.3k
)
{22.3k
2376
22.3k
    if (row[i] < 0)
2377
4.56k
      continue;
2378
17.7k
    combined = isl_basic_set_add_ineq(combined, ineq->row[i]);
2379
17.7k
    if (isl_tab_add_ineq(tab, ineq->row[i]) < 0)
2380
0
      goto error;
2381
17.7k
    row[i] = r++;
2382
17.7k
  }
2383
8.81k
  
if (8.81k
isl_tab_detect_implicit_equalities(tab) < 08.81k
)
2384
0
    goto error;
2385
8.81k
  
if (8.81k
isl_tab_detect_redundant(tab) < 08.81k
)
2386
0
    goto error;
2387
31.1k
  
for (i = bset->n_ineq - 1; 8.81k
i >= 031.1k
;
--i22.3k
)
{22.3k
2388
22.3k
    isl_basic_set *test;
2389
22.3k
    int is_empty;
2390
22.3k
2391
22.3k
    if (row[i] < 0)
2392
4.56k
      continue;
2393
17.7k
    r = row[i];
2394
17.7k
    if (tab->con[n_eq + r].is_redundant)
2395
421
      continue;
2396
17.3k
    test = isl_basic_set_dup(combined);
2397
17.3k
    if (isl_inequality_negate(test, r) < 0)
2398
0
      test = isl_basic_set_free(test);
2399
17.3k
    test = isl_basic_set_update_from_tab(test, tab);
2400
17.3k
    is_empty = isl_basic_set_is_empty(test);
2401
17.3k
    isl_basic_set_free(test);
2402
17.3k
    if (is_empty < 0)
2403
0
      goto error;
2404
17.3k
    
if (17.3k
is_empty17.3k
)
2405
5.16k
      tab->con[n_eq + r].is_redundant = 1;
2406
17.3k
  }
2407
8.81k
  bset = update_ineq_free(bset, ineq, context, row, tab);
2408
8.81k
  if (
bset8.81k
)
{8.81k
2409
8.81k
    ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT);
2410
8.81k
    ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT);
2411
8.81k
  }
2412
8.81k
2413
8.81k
  isl_basic_set_free(combined);
2414
8.81k
  return bset;
2415
0
error:
2416
0
  free(row);
2417
0
  isl_mat_free(ineq);
2418
0
  isl_tab_free(tab);
2419
0
  isl_basic_set_free(combined);
2420
0
  isl_basic_set_free(context);
2421
0
  isl_basic_set_free(bset);
2422
0
  return NULL;
2423
8.81k
}
2424
2425
/* Extract the inequalities of "bset" as an isl_mat.
2426
 */
2427
static __isl_give isl_mat *extract_ineq(__isl_keep isl_basic_set *bset)
2428
15.3k
{
2429
15.3k
  unsigned total;
2430
15.3k
  isl_ctx *ctx;
2431
15.3k
  isl_mat *ineq;
2432
15.3k
2433
15.3k
  if (!bset)
2434
0
    return NULL;
2435
15.3k
2436
15.3k
  ctx = isl_basic_set_get_ctx(bset);
2437
15.3k
  total = isl_basic_set_total_dim(bset);
2438
15.3k
  ineq = isl_mat_sub_alloc6(ctx, bset->ineq, 0, bset->n_ineq,
2439
15.3k
            0, 1 + total);
2440
15.3k
2441
15.3k
  return ineq;
2442
15.3k
}
2443
2444
/* Remove all information from "bset" that is redundant in the context
2445
 * of "context", for the case where both "bset" and "context" are
2446
 * full-dimensional.
2447
 */
2448
static __isl_give isl_basic_set *uset_gist_uncompressed(
2449
  __isl_take isl_basic_set *bset, __isl_take isl_basic_set *context)
2450
8.21k
{
2451
8.21k
  isl_mat *ineq;
2452
8.21k
2453
8.21k
  ineq = extract_ineq(bset);
2454
8.21k
  return uset_gist_full(bset, ineq, context);
2455
8.21k
}
2456
2457
/* Remove all information from "bset" that is redundant in the context
2458
 * of "context", for the case where the combined equalities of
2459
 * "bset" and "context" allow for a compression that can be obtained
2460
 * by preapplication of "T".
2461
 *
2462
 * "bset" itself is not transformed by "T".  Instead, the inequalities
2463
 * are extracted from "bset" and those are transformed by "T".
2464
 * uset_gist_full then determines which of the transformed inequalities
2465
 * are redundant with respect to the transformed "context" and removes
2466
 * the corresponding inequalities from "bset".
2467
 *
2468
 * After preapplying "T" to the inequalities, any common factor is
2469
 * removed from the coefficients.  If this results in a tightening
2470
 * of the constant term, then the same tightening is applied to
2471
 * the corresponding untransformed inequality in "bset".
2472
 * That is, if after plugging in T, a constraint f(x) >= 0 is of the form
2473
 *
2474
 *  g f'(x) + r >= 0
2475
 *
2476
 * with 0 <= r < g, then it is equivalent to
2477
 *
2478
 *  f'(x) >= 0
2479
 *
2480
 * This means that f(x) >= 0 is equivalent to f(x) - r >= 0 in the affine
2481
 * subspace compressed by T since the latter would be transformed to
2482
 *
2483
 *  g f'(x) >= 0
2484
 */
2485
static __isl_give isl_basic_set *uset_gist_compressed(
2486
  __isl_take isl_basic_set *bset, __isl_take isl_basic_set *context,
2487
  __isl_take isl_mat *T)
2488
7.08k
{
2489
7.08k
  isl_ctx *ctx;
2490
7.08k
  isl_mat *ineq;
2491
7.08k
  int i, n_row, n_col;
2492
7.08k
  isl_int rem;
2493
7.08k
2494
7.08k
  ineq = extract_ineq(bset);
2495
7.08k
  ineq = isl_mat_product(ineq, isl_mat_copy(T));
2496
7.08k
  context = isl_basic_set_preimage(context, T);
2497
7.08k
2498
7.08k
  if (
!ineq || 7.08k
!context7.08k
)
2499
0
    goto error;
2500
7.08k
  
if (7.08k
isl_basic_set_plain_is_empty(context)7.08k
)
{7
2501
7
    isl_mat_free(ineq);
2502
7
    isl_basic_set_free(context);
2503
7
    return isl_basic_set_set_to_empty(bset);
2504
7
  }
2505
7.08k
2506
7.07k
  ctx = isl_mat_get_ctx(ineq);
2507
7.07k
  n_row = isl_mat_rows(ineq);
2508
7.07k
  n_col = isl_mat_cols(ineq);
2509
7.07k
  isl_int_init(rem);
2510
14.0k
  for (i = 0; 
i < n_row14.0k
;
++i6.93k
)
{6.93k
2511
6.93k
    isl_seq_gcd(ineq->row[i] + 1, n_col - 1, &ctx->normalize_gcd);
2512
6.93k
    if (isl_int_is_zero(ctx->normalize_gcd))
2513
17
      continue;
2514
6.91k
    
if (6.91k
isl_int_is_one6.91k
(ctx->normalize_gcd))
2515
6.53k
      continue;
2516
389
    isl_seq_scale_down(ineq->row[i] + 1, ineq->row[i] + 1,
2517
389
            ctx->normalize_gcd, n_col - 1);
2518
389
    isl_int_fdiv_r(rem, ineq->row[i][0], ctx->normalize_gcd);
2519
389
    isl_int_fdiv_q(ineq->row[i][0],
2520
389
        ineq->row[i][0], ctx->normalize_gcd);
2521
389
    if (isl_int_is_zero(rem))
2522
265
      continue;
2523
124
    bset = isl_basic_set_cow(bset);
2524
124
    if (!bset)
2525
0
      break;
2526
124
    
isl_int_sub124
(bset->ineq[i][0], bset->ineq[i][0], rem);124
2527
124
  }
2528
7.07k
  isl_int_clear(rem);
2529
7.07k
2530
7.07k
  return uset_gist_full(bset, ineq, context);
2531
0
error:
2532
0
  isl_mat_free(ineq);
2533
0
  isl_basic_set_free(context);
2534
0
  isl_basic_set_free(bset);
2535
0
  return NULL;
2536
7.08k
}
2537
2538
/* Project "bset" onto the variables that are involved in "template".
2539
 */
2540
static __isl_give isl_basic_set *project_onto_involved(
2541
  __isl_take isl_basic_set *bset, __isl_keep isl_basic_set *template)
2542
7.08k
{
2543
7.08k
  int i, n;
2544
7.08k
2545
7.08k
  if (
!bset || 7.08k
!template7.08k
)
2546
0
    return isl_basic_set_free(bset);
2547
7.08k
2548
7.08k
  n = isl_basic_set_dim(template, isl_dim_set);
2549
7.08k
2550
43.2k
  for (i = 0; 
i < n43.2k
;
++i36.1k
)
{36.1k
2551
36.1k
    isl_bool involved;
2552
36.1k
2553
36.1k
    involved = isl_basic_set_involves_dims(template,
2554
36.1k
              isl_dim_set, i, 1);
2555
36.1k
    if (involved < 0)
2556
0
      return isl_basic_set_free(bset);
2557
36.1k
    
if (36.1k
involved36.1k
)
2558
21.2k
      continue;
2559
14.9k
    bset = isl_basic_set_eliminate_vars(bset, i, 1);
2560
14.9k
  }
2561
7.08k
2562
7.08k
  return bset;
2563
7.08k
}
2564
2565
/* Remove all information from bset that is redundant in the context
2566
 * of context.  In particular, equalities that are linear combinations
2567
 * of those in context are removed.  Then the inequalities that are
2568
 * redundant in the context of the equalities and inequalities of
2569
 * context are removed.
2570
 *
2571
 * First of all, we drop those constraints from "context"
2572
 * that are irrelevant for computing the gist of "bset".
2573
 * Alternatively, we could factorize the intersection of "context" and "bset".
2574
 *
2575
 * We first compute the intersection of the integer affine hulls
2576
 * of "bset" and "context",
2577
 * compute the gist inside this intersection and then reduce
2578
 * the constraints with respect to the equalities of the context
2579
 * that only involve variables already involved in the input.
2580
 *
2581
 * If two constraints are mutually redundant, then uset_gist_full
2582
 * will remove the second of those constraints.  We therefore first
2583
 * sort the constraints so that constraints not involving existentially
2584
 * quantified variables are given precedence over those that do.
2585
 * We have to perform this sorting before the variable compression,
2586
 * because that may effect the order of the variables.
2587
 */
2588
static __isl_give isl_basic_set *uset_gist(__isl_take isl_basic_set *bset,
2589
  __isl_take isl_basic_set *context)
2590
15.3k
{
2591
15.3k
  isl_mat *eq;
2592
15.3k
  isl_mat *T;
2593
15.3k
  isl_basic_set *aff;
2594
15.3k
  isl_basic_set *aff_context;
2595
15.3k
  unsigned total;
2596
15.3k
2597
15.3k
  if (
!bset || 15.3k
!context15.3k
)
2598
0
    goto error;
2599
15.3k
2600
15.3k
  context = drop_irrelevant_constraints(context, bset);
2601
15.3k
2602
15.3k
  bset = isl_basic_set_detect_equalities(bset);
2603
15.3k
  aff = isl_basic_set_copy(bset);
2604
15.3k
  aff = isl_basic_set_plain_affine_hull(aff);
2605
15.3k
  context = isl_basic_set_detect_equalities(context);
2606
15.3k
  aff_context = isl_basic_set_copy(context);
2607
15.3k
  aff_context = isl_basic_set_plain_affine_hull(aff_context);
2608
15.3k
  aff = isl_basic_set_intersect(aff, aff_context);
2609
15.3k
  if (!aff)
2610
0
    goto error;
2611
15.3k
  
if (15.3k
isl_basic_set_plain_is_empty(aff)15.3k
)
{1
2612
1
    isl_basic_set_free(bset);
2613
1
    isl_basic_set_free(context);
2614
1
    return aff;
2615
1
  }
2616
15.3k
  bset = isl_basic_set_sort_constraints(bset);
2617
15.3k
  if (
aff->n_eq == 015.3k
)
{8.21k
2618
8.21k
    isl_basic_set_free(aff);
2619
8.21k
    return uset_gist_uncompressed(bset, context);
2620
8.21k
  }
2621
7.08k
  total = isl_basic_set_total_dim(bset);
2622
7.08k
  eq = isl_mat_sub_alloc6(bset->ctx, aff->eq, 0, aff->n_eq, 0, 1 + total);
2623
7.08k
  eq = isl_mat_cow(eq);
2624
7.08k
  T = isl_mat_variable_compression(eq, NULL);
2625
7.08k
  isl_basic_set_free(aff);
2626
7.08k
  if (
T && 7.08k
T->n_col == 07.08k
)
{0
2627
0
    isl_mat_free(T);
2628
0
    isl_basic_set_free(context);
2629
0
    return isl_basic_set_set_to_empty(bset);
2630
0
  }
2631
7.08k
2632
7.08k
  aff_context = isl_basic_set_affine_hull(isl_basic_set_copy(context));
2633
7.08k
  aff_context = project_onto_involved(aff_context, bset);
2634
7.08k
2635
7.08k
  bset = uset_gist_compressed(bset, context, T);
2636
7.08k
  bset = isl_basic_set_reduce_using_equalities(bset, aff_context);
2637
7.08k
2638
7.08k
  if (
bset7.08k
)
{7.08k
2639
7.08k
    ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT);
2640
7.08k
    ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT);
2641
7.08k
  }
2642
7.08k
2643
7.08k
  return bset;
2644
0
error:
2645
0
  isl_basic_set_free(bset);
2646
0
  isl_basic_set_free(context);
2647
0
  return NULL;
2648
7.08k
}
2649
2650
/* Return the number of equality constraints in "bmap" that involve
2651
 * local variables.  This function assumes that Gaussian elimination
2652
 * has been applied to the equality constraints.
2653
 */
2654
static int n_div_eq(__isl_keep isl_basic_map *bmap)
2655
868
{
2656
868
  int i;
2657
868
  int total, n_div;
2658
868
2659
868
  if (!bmap)
2660
0
    return -1;
2661
868
2662
868
  
if (868
bmap->n_eq == 0868
)
2663
318
    return 0;
2664
868
2665
550
  total = isl_basic_map_dim(bmap, isl_dim_all);
2666
550
  n_div = isl_basic_map_dim(bmap, isl_dim_div);
2667
550
  total -= n_div;
2668
550
2669
938
  for (i = 0; 
i < bmap->n_eq938
;
++i388
)
2670
625
    
if (625
isl_seq_first_non_zero(bmap->eq[i] + 1 + total,625
2671
625
              n_div) == -1)
2672
237
      return i;
2673
550
2674
313
  return bmap->n_eq;
2675
550
}
2676
2677
/* Construct a basic map in "space" defined by the equality constraints in "eq".
2678
 * The constraints are assumed not to involve any local variables.
2679
 */
2680
static __isl_give isl_basic_map *basic_map_from_equalities(
2681
  __isl_take isl_space *space, __isl_take isl_mat *eq)
2682
2
{
2683
2
  int i, k;
2684
2
  isl_basic_map *bmap = NULL;
2685
2
2686
2
  if (
!space || 2
!eq2
)
2687
0
    goto error;
2688
2
2689
2
  
if (2
1 + isl_space_dim(space, isl_dim_all) != eq->n_col2
)
2690
0
    isl_die(isl_space_get_ctx(space), isl_error_internal,
2691
2
      "unexpected number of columns", goto error);
2692
2
2693
2
  bmap = isl_basic_map_alloc_space(isl_space_copy(space),
2694
2
              0, eq->n_row, 0);
2695
6
  for (i = 0; 
i < eq->n_row6
;
++i4
)
{4
2696
4
    k = isl_basic_map_alloc_equality(bmap);
2697
4
    if (k < 0)
2698
0
      goto error;
2699
4
    isl_seq_cpy(bmap->eq[k], eq->row[i], eq->n_col);
2700
4
  }
2701
2
2702
2
  isl_space_free(space);
2703
2
  isl_mat_free(eq);
2704
2
  return bmap;
2705
0
error:
2706
0
  isl_space_free(space);
2707
0
  isl_mat_free(eq);
2708
0
  isl_basic_map_free(bmap);
2709
0
  return NULL;
2710
2
}
2711
2712
/* Construct and return a variable compression based on the equality
2713
 * constraints in "bmap1" and "bmap2" that do not involve the local variables.
2714
 * "n1" is the number of (initial) equality constraints in "bmap1"
2715
 * that do involve local variables.
2716
 * "n2" is the number of (initial) equality constraints in "bmap2"
2717
 * that do involve local variables.
2718
 * "total" is the total number of other variables.
2719
 * This function assumes that Gaussian elimination
2720
 * has been applied to the equality constraints in both "bmap1" and "bmap2"
2721
 * such that the equality constraints not involving local variables
2722
 * are those that start at "n1" or "n2".
2723
 *
2724
 * If either of "bmap1" and "bmap2" does not have such equality constraints,
2725
 * then simply compute the compression based on the equality constraints
2726
 * in the other basic map.
2727
 * Otherwise, combine the equality constraints from both into a new
2728
 * basic map such that Gaussian elimination can be applied to this combination
2729
 * and then construct a variable compression from the resulting
2730
 * equality constraints.
2731
 */
2732
static __isl_give isl_mat *combined_variable_compression(
2733
  __isl_keep isl_basic_map *bmap1, int n1,
2734
  __isl_keep isl_basic_map *bmap2, int n2, int total)
2735
7
{
2736
7
  isl_ctx *ctx;
2737
7
  isl_mat *E1, *E2, *V;
2738
7
  isl_basic_map *bmap;
2739
7
2740
7
  ctx = isl_basic_map_get_ctx(bmap1);
2741
7
  if (
bmap1->n_eq == n17
)
{3
2742
3
    E2 = isl_mat_sub_alloc6(ctx, bmap2->eq,
2743
3
          n2, bmap2->n_eq - n2, 0, 1 + total);
2744
3
    return isl_mat_variable_compression(E2, NULL);
2745
3
  }
2746
4
  
if (4
bmap2->n_eq == n24
)
{2
2747
2
    E1 = isl_mat_sub_alloc6(ctx, bmap1->eq,
2748
2
          n1, bmap1->n_eq - n1, 0, 1 + total);
2749
2
    return isl_mat_variable_compression(E1, NULL);
2750
2
  }
2751
2
  E1 = isl_mat_sub_alloc6(ctx, bmap1->eq,
2752
2
        n1, bmap1->n_eq - n1, 0, 1 + total);
2753
2
  E2 = isl_mat_sub_alloc6(ctx, bmap2->eq,
2754
2
        n2, bmap2->n_eq - n2, 0, 1 + total);
2755
2
  E1 = isl_mat_concat(E1, E2);
2756
2
  bmap = basic_map_from_equalities(isl_basic_map_get_space(bmap1), E1);
2757
2
  bmap = isl_basic_map_gauss(bmap, NULL);
2758
2
  if (!bmap)
2759
0
    return NULL;
2760
2
  E1 = isl_mat_sub_alloc6(ctx, bmap->eq, 0, bmap->n_eq, 0, 1 + total);
2761
2
  V = isl_mat_variable_compression(E1, NULL);
2762
2
  isl_basic_map_free(bmap);
2763
2
2764
2
  return V;
2765
2
}
2766
2767
/* Extract the stride constraints from "bmap", compressed
2768
 * with respect to both the stride constraints in "context" and
2769
 * the remaining equality constraints in both "bmap" and "context".
2770
 * "bmap_n_eq" is the number of (initial) stride constraints in "bmap".
2771
 * "context_n_eq" is the number of (initial) stride constraints in "context".
2772
 *
2773
 * Let x be all variables in "bmap" (and "context") other than the local
2774
 * variables.  First compute a variable compression
2775
 *
2776
 *  x = V x'
2777
 *
2778
 * based on the non-stride equality constraints in "bmap" and "context".
2779
 * Consider the stride constraints of "context",
2780
 *
2781
 *  A(x) + B(y) = 0
2782
 *
2783
 * with y the local variables and plug in the variable compression,
2784
 * resulting in
2785
 *
2786
 *  A(V x') + B(y) = 0
2787
 *
2788
 * Use these constraints to compute a parameter compression on x'
2789
 *
2790
 *  x' = T x''
2791
 *
2792
 * Now consider the stride constraints of "bmap"
2793
 *
2794
 *  C(x) + D(y) = 0
2795
 *
2796
 * and plug in x = V*T x''.
2797
 * That is, return A = [C*V*T D].
2798
 */
2799
static __isl_give isl_mat *extract_compressed_stride_constraints(
2800
  __isl_keep isl_basic_map *bmap, int bmap_n_eq,
2801
  __isl_keep isl_basic_map *context, int context_n_eq)
2802
7
{
2803
7
  int total, n_div;
2804
7
  isl_ctx *ctx;
2805
7
  isl_mat *A, *B, *T, *V;
2806
7
2807
7
  total = isl_basic_map_dim(context, isl_dim_all);
2808
7
  n_div = isl_basic_map_dim(context, isl_dim_div);
2809
7
  total -= n_div;
2810
7
2811
7
  ctx = isl_basic_map_get_ctx(bmap);
2812
7
2813
7
  V = combined_variable_compression(bmap, bmap_n_eq,
2814
7
            context, context_n_eq, total);
2815
7
2816
7
  A = isl_mat_sub_alloc6(ctx, context->eq, 0, context_n_eq, 0, 1 + total);
2817
7
  B = isl_mat_sub_alloc6(ctx, context->eq,
2818
7
        0, context_n_eq, 1 + total, n_div);
2819
7
  A = isl_mat_product(A, isl_mat_copy(V));
2820
7
  T = isl_mat_parameter_compression_ext(A, B);
2821
7
  T = isl_mat_product(V, T);
2822
7
2823
7
  n_div = isl_basic_map_dim(bmap, isl_dim_div);
2824
7
  T = isl_mat_diagonal(T, isl_mat_identity(ctx, n_div));
2825
7
2826
7
  A = isl_mat_sub_alloc6(ctx, bmap->eq,
2827
7
        0, bmap_n_eq, 0, 1 + total + n_div);
2828
7
  A = isl_mat_product(A, T);
2829
7
2830
7
  return A;
2831
7
}
2832
2833
/* Remove the prime factors from *g that have an exponent that
2834
 * is strictly smaller than the exponent in "c".
2835
 * All exponents in *g are known to be smaller than or equal
2836
 * to those in "c".
2837
 *
2838
 * That is, if *g is equal to
2839
 *
2840
 *  p_1^{e_1} p_2^{e_2} ... p_n^{e_n}
2841
 *
2842
 * and "c" is equal to
2843
 *
2844
 *  p_1^{f_1} p_2^{f_2} ... p_n^{f_n}
2845
 *
2846
 * then update *g to
2847
 *
2848
 *  p_1^{e_1 * (e_1 = f_1)} p_2^{e_2 * (e_2 = f_2)} ...
2849
 *    p_n^{e_n * (e_n = f_n)}
2850
 *
2851
 * If e_i = f_i, then c / *g does not have any p_i factors and therefore
2852
 * neither does the gcd of *g and c / *g.
2853
 * If e_i < f_i, then the gcd of *g and c / *g has a positive
2854
 * power min(e_i, s_i) of p_i with s_i = f_i - e_i among its factors.
2855
 * Dividing *g by this gcd therefore strictly reduces the exponent
2856
 * of the prime factors that need to be removed, while leaving the
2857
 * other prime factors untouched.
2858
 * Repeating this process until gcd(*g, c / *g) = 1 therefore
2859
 * removes all undesired factors, without removing any others.
2860
 */
2861
static void remove_incomplete_powers(isl_int *g, isl_int c)
2862
6
{
2863
6
  isl_int t;
2864
6
2865
6
  isl_int_init(t);
2866
9
  for (;;) {
2867
9
    isl_int_divexact(t, c, *g);
2868
9
    isl_int_gcd(t, t, *g);
2869
9
    if (isl_int_is_one(t))
2870
6
      break;
2871
3
    
isl_int_divexact3
(*g, *g, t);3
2872
3
  }
2873
6
  isl_int_clear(t);
2874
6
}
2875
2876
/* Reduce the "n" stride constraints in "bmap" based on a copy "A"
2877
 * of the same stride constraints in a compressed space that exploits
2878
 * all equalities in the context and the other equalities in "bmap".
2879
 *
2880
 * If the stride constraints of "bmap" are of the form
2881
 *
2882
 *  C(x) + D(y) = 0
2883
 *
2884
 * then A is of the form
2885
 *
2886
 *  B(x') + D(y) = 0
2887
 *
2888
 * If any of these constraints involves only a single local variable y,
2889
 * then the constraint appears as
2890
 *
2891
 *  f(x) + m y_i = 0
2892
 *
2893
 * in "bmap" and as
2894
 *
2895
 *  h(x') + m y_i = 0
2896
 *
2897
 * in "A".
2898
 *
2899
 * Let g be the gcd of m and the coefficients of h.
2900
 * Then, in particular, g is a divisor of the coefficients of h and
2901
 *
2902
 *  f(x) = h(x')
2903
 *
2904
 * is known to be a multiple of g.
2905
 * If some prime factor in m appears with the same exponent in g,
2906
 * then it can be removed from m because f(x) is already known
2907
 * to be a multiple of g and therefore in particular of this power
2908
 * of the prime factors.
2909
 * Prime factors that appear with a smaller exponent in g cannot
2910
 * be removed from m.
2911
 * Let g' be the divisor of g containing all prime factors that
2912
 * appear with the same exponent in m and g, then
2913
 *
2914
 *  f(x) + m y_i = 0
2915
 *
2916
 * can be replaced by
2917
 *
2918
 *  f(x) + m/g' y_i' = 0
2919
 *
2920
 * Note that (if g' != 1) this changes the explicit representation
2921
 * of y_i to that of y_i', so the integer division at position i
2922
 * is marked unknown and later recomputed by a call to
2923
 * isl_basic_map_gauss.
2924
 */
2925
static __isl_give isl_basic_map *reduce_stride_constraints(
2926
  __isl_take isl_basic_map *bmap, int n, __isl_keep isl_mat *A)
2927
7
{
2928
7
  int i;
2929
7
  int total, n_div;
2930
7
  int any = 0;
2931
7
  isl_int gcd;
2932
7
2933
7
  if (
!bmap || 7
!A7
)
2934
0
    return isl_basic_map_free(bmap);
2935
7
2936
7
  total = isl_basic_map_dim(bmap, isl_dim_all);
2937
7
  n_div = isl_basic_map_dim(bmap, isl_dim_div);
2938
7
  total -= n_div;
2939
7
2940
7
  isl_int_init(gcd);
2941
14
  for (i = 0; 
i < n14
;
++i7
)
{7
2942
7
    int div;
2943
7
2944
7
    div = isl_seq_first_non_zero(bmap->eq[i] + 1 + total, n_div);
2945
7
    if (div < 0)
2946
0
      isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
2947
7
        "equality constraints modified unexpectedly",
2948
7
        goto error);
2949
7
    
if (7
isl_seq_first_non_zero(bmap->eq[i] + 1 + total + div + 1,7
2950
7
            n_div - div - 1) != -1)
2951
0
      continue;
2952
7
    
if (7
isl_mat_row_gcd(A, i, &gcd) < 07
)
2953
0
      goto error;
2954
7
    
if (7
isl_int_is_one7
(gcd))
2955
1
      continue;
2956
6
    remove_incomplete_powers(&gcd, bmap->eq[i][1 + total + div]);
2957
6
    if (isl_int_is_one(gcd))
2958
1
      continue;
2959
5
    
isl_int_divexact5
(bmap->eq[i][1 + total + div],5
2960
5
        bmap->eq[i][1 + total + div], gcd);
2961
5
    bmap = isl_basic_map_mark_div_unknown(bmap, div);
2962
5
    if (!bmap)
2963
0
      goto error;
2964
5
    any = 1;
2965
5
  }
2966
7
  
isl_int_clear7
(gcd);7
2967
7
2968
7
  if (any)
2969
5
    bmap = isl_basic_map_gauss(bmap, NULL);
2970
7
2971
7
  return bmap;
2972
0
error:
2973
0
  isl_int_clear(gcd);
2974
0
  isl_basic_map_free(bmap);
2975
0
  return NULL;
2976
7
}
2977
2978
/* Simplify the stride constraints in "bmap" based on
2979
 * the remaining equality constraints in "bmap" and all equality
2980
 * constraints in "context".
2981
 * Only do this if both "bmap" and "context" have stride constraints.
2982
 *
2983
 * First extract a copy of the stride constraints in "bmap" in a compressed
2984
 * space exploiting all the other equality constraints and then
2985
 * use this compressed copy to simplify the original stride constraints.
2986
 */
2987
static __isl_give isl_basic_map *gist_strides(__isl_take isl_basic_map *bmap,
2988
  __isl_keep isl_basic_map *context)
2989
434
{
2990
434
  int bmap_n_eq, context_n_eq;
2991
434
  isl_mat *A;
2992
434
2993
434
  if (
!bmap || 434
!context434
)
2994
0
    return isl_basic_map_free(bmap);
2995
434
2996
434
  bmap_n_eq = n_div_eq(bmap);
2997
434
  context_n_eq = n_div_eq(context);
2998
434
2999
434
  if (
bmap_n_eq < 0 || 434
context_n_eq < 0434
)
3000
0
    return isl_basic_map_free(bmap);
3001
434
  
if (434
bmap_n_eq == 0 || 434
context_n_eq == 0245
)
3002
427
    return bmap;
3003
434
3004
7
  A = extract_compressed_stride_constraints(bmap, bmap_n_eq,
3005
7
                context, context_n_eq);
3006
7
  bmap = reduce_stride_constraints(bmap, bmap_n_eq, A);
3007
7
3008
7
  isl_mat_free(A);
3009
7
3010
7
  return bmap;
3011
434
}
3012
3013
/* Return a basic map that has the same intersection with "context" as "bmap"
3014
 * and that is as "simple" as possible.
3015
 *
3016
 * The core computation is performed on the pure constraints.
3017
 * When we add back the meaning of the integer divisions, we need
3018
 * to (re)introduce the div constraints.  If we happen to have
3019
 * discovered that some of these integer divisions are equal to
3020
 * some affine combination of other variables, then these div
3021
 * constraints may end up getting simplified in terms of the equalities,
3022
 * resulting in extra inequalities on the other variables that
3023
 * may have been removed already or that may not even have been
3024
 * part of the input.  We try and remove those constraints of
3025
 * this form that are most obviously redundant with respect to
3026
 * the context.  We also remove those div constraints that are
3027
 * redundant with respect to the other constraints in the result.
3028
 *
3029
 * The stride constraints among the equality constraints in "bmap" are
3030
 * also simplified with respecting to the other equality constraints
3031
 * in "bmap" and with respect to all equality constraints in "context".
3032
 */
3033
__isl_give isl_basic_map *isl_basic_map_gist(__isl_take isl_basic_map *bmap,
3034
  __isl_take isl_basic_map *context)
3035
16.6k
{
3036
16.6k
  isl_basic_set *bset, *eq;
3037
16.6k
  isl_basic_map *eq_bmap;
3038
16.6k
  unsigned total, n_div, extra, n_eq, n_ineq;
3039
16.6k
3040
16.6k
  if (
!bmap || 16.6k
!context16.6k
)
3041
0
    goto error;
3042
16.6k
3043
16.6k
  
if (16.6k
isl_basic_map_plain_is_universe(bmap)16.6k
)
{1.34k
3044
1.34k
    isl_basic_map_free(context);
3045
1.34k
    return bmap;
3046
1.34k
  }
3047
15.3k
  
if (15.3k
isl_basic_map_plain_is_empty(context)15.3k
)
{0
3048
0
    isl_space *space = isl_basic_map_get_space(bmap);
3049
0
    isl_basic_map_free(bmap);
3050
0
    isl_basic_map_free(context);
3051
0
    return isl_basic_map_universe(space);
3052
0
  }
3053
15.3k
  
if (15.3k
isl_basic_map_plain_is_empty(bmap)15.3k
)
{0
3054
0
    isl_basic_map_free(context);
3055
0
    return bmap;
3056
0
  }
3057
15.3k
3058
15.3k
  bmap = isl_basic_map_remove_redundancies(bmap);
3059
15.3k
  context = isl_basic_map_remove_redundancies(context);
3060
15.3k
  if (!context)
3061
0
    goto error;
3062
15.3k
3063
15.3k
  context = isl_basic_map_align_divs(context, bmap);
3064
15.3k
  n_div = isl_basic_map_dim(context, isl_dim_div);
3065
15.3k
  total = isl_basic_map_dim(bmap, isl_dim_all);
3066
15.3k
  extra = n_div - isl_basic_map_dim(bmap, isl_dim_div);
3067
15.3k
3068
15.3k
  bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
3069
15.3k
  bset = isl_basic_set_add_dims(bset, isl_dim_set, extra);
3070
15.3k
  bset = uset_gist(bset,
3071
15.3k
        isl_basic_map_underlying_set(isl_basic_map_copy(context)));
3072
15.3k
  bset = isl_basic_set_project_out(bset, isl_dim_set, total, extra);
3073
15.3k
3074
15.3k
  if (
!bset || 15.3k
bset->n_eq == 015.3k
||
n_div == 08.95k
||
3075
14.8k
      
isl_basic_set_plain_is_empty(bset)535
)
{14.8k
3076
14.8k
    isl_basic_map_free(context);
3077
14.8k
    return isl_basic_map_overlying_set(bset, bmap);
3078
14.8k
  }
3079
15.3k
3080
434
  n_eq = bset->n_eq;
3081
434
  n_ineq = bset->n_ineq;
3082
434
  eq = isl_basic_set_copy(bset);
3083
434
  eq = isl_basic_set_cow(eq);
3084
434
  if (isl_basic_set_free_inequality(eq, n_ineq) < 0)
3085
0
    eq = isl_basic_set_free(eq);
3086
434
  if (isl_basic_set_free_equality(bset, n_eq) < 0)
3087
0
    bset = isl_basic_set_free(bset);
3088
434
3089
434
  eq_bmap = isl_basic_map_overlying_set(eq, isl_basic_map_copy(bmap));
3090
434
  eq_bmap = gist_strides(eq_bmap, context);
3091
434
  eq_bmap = isl_basic_map_remove_shifted_constraints(eq_bmap, context);
3092
434
  bmap = isl_basic_map_overlying_set(bset, bmap);
3093
434
  bmap = isl_basic_map_intersect(bmap, eq_bmap);
3094
434
  bmap = isl_basic_map_remove_redundancies(bmap);
3095
434
3096
434
  return bmap;
3097
0
error:
3098
0
  isl_basic_map_free(bmap);
3099
0
  isl_basic_map_free(context);
3100
0
  return NULL;
3101
15.3k
}
3102
3103
/*
3104
 * Assumes context has no implicit divs.
3105
 */
3106
__isl_give isl_map *isl_map_gist_basic_map(__isl_take isl_map *map,
3107
  __isl_take isl_basic_map *context)
3108
15.4k
{
3109
15.4k
  int i;
3110
15.4k
3111
15.4k
  if (
!map || 15.4k
!context15.4k
)
3112
0
    goto error;
3113
15.4k
3114
15.4k
  
if (15.4k
isl_basic_map_plain_is_empty(context)15.4k
)
{0
3115
0
    isl_space *space = isl_map_get_space(map);
3116
0
    isl_map_free(map);
3117
0
    isl_basic_map_free(context);
3118
0
    return isl_map_universe(space);
3119
0
  }
3120
15.4k
3121
15.4k
  context = isl_basic_map_remove_redundancies(context);
3122
15.4k
  map = isl_map_cow(map);
3123
15.4k
  if (
!map || 15.4k
!context15.4k
)
3124
0
    goto error;
3125
15.4k
  
isl_assert15.4k
(map->ctx, isl_space_is_equal(map->dim, context->dim), goto error);15.4k
3126
15.4k
  map = isl_map_compute_divs(map);
3127
15.4k
  if (!map)
3128
0
    goto error;
3129
31.3k
  
for (i = map->n - 1; 15.4k
i >= 031.3k
;
--i15.8k
)
{15.8k
3130
15.8k
    map->p[i] = isl_basic_map_gist(map->p[i],
3131
15.8k
            isl_basic_map_copy(context));
3132
15.8k
    if (!map->p[i])
3133
0
      goto error;
3134
15.8k
    
if (15.8k
isl_basic_map_plain_is_empty(map->p[i])15.8k
)
{2.34k
3135
2.34k
      isl_basic_map_free(map->p[i]);
3136
2.34k
      if (i != map->n - 1)
3137
997
        map->p[i] = map->p[map->n - 1];
3138
2.34k
      map->n--;
3139
2.34k
    }
3140
15.8k
  }
3141
15.4k
  isl_basic_map_free(context);
3142
15.4k
  ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3143
15.4k
  return map;
3144
0
error:
3145
0
  isl_map_free(map);
3146
0
  isl_basic_map_free(context);
3147
0
  return NULL;
3148
15.4k
}
3149
3150
/* Drop all inequalities from "bmap" that also appear in "context".
3151
 * "context" is assumed to have only known local variables and
3152
 * the initial local variables of "bmap" are assumed to be the same
3153
 * as those of "context".
3154
 * The constraints of both "bmap" and "context" are assumed
3155
 * to have been sorted using isl_basic_map_sort_constraints.
3156
 *
3157
 * Run through the inequality constraints of "bmap" and "context"
3158
 * in sorted order.
3159
 * If a constraint of "bmap" involves variables not in "context",
3160
 * then it cannot appear in "context".
3161
 * If a matching constraint is found, it is removed from "bmap".
3162
 */
3163
static __isl_give isl_basic_map *drop_inequalities(
3164
  __isl_take isl_basic_map *bmap, __isl_keep isl_basic_map *context)
3165
347
{
3166
347
  int i1, i2;
3167
347
  unsigned total, extra;
3168
347
3169
347
  if (
!bmap || 347
!context347
)
3170
0
    return isl_basic_map_free(bmap);
3171
347
3172
347
  total = isl_basic_map_total_dim(context);
3173
347
  extra = isl_basic_map_total_dim(bmap) - total;
3174
347
3175
347
  i1 = bmap->n_ineq - 1;
3176
347
  i2 = context->n_ineq - 1;
3177
1.42k
  while (
bmap && 1.42k
i1 >= 01.42k
&&
i2 >= 01.21k
)
{1.08k
3178
1.08k
    int cmp;
3179
1.08k
3180
1.08k
    if (isl_seq_first_non_zero(bmap->ineq[i1] + 1 + total,
3181
18
              extra) != -1) {
3182
18
      --i1;
3183
18
      continue;
3184
18
    }
3185
1.06k
    cmp = isl_basic_map_constraint_cmp(context, bmap->ineq[i1],
3186
1.06k
              context->ineq[i2]);
3187
1.06k
    if (
cmp < 01.06k
)
{0
3188
0
      --i2;
3189
0
      continue;
3190
0
    }
3191
1.06k
    
if (1.06k
cmp > 01.06k
)
{295
3192
295
      --i1;
3193
295
      continue;
3194
295
    }
3195
767
    
if (767
isl_int_eq767
(bmap->ineq[i1][0], context->ineq[i2][0]))
{695
3196
695
      bmap = isl_basic_map_cow(bmap);
3197
695
      if (isl_basic_map_drop_inequality(bmap, i1) < 0)
3198
0
        bmap = isl_basic_map_free(bmap);
3199
695
    }
3200
767
    --i1;
3201
767
    --i2;
3202
767
  }
3203
347
3204
347
  return bmap;
3205
347
}
3206
3207
/* Drop all equalities from "bmap" that also appear in "context".
3208
 * "context" is assumed to have only known local variables and
3209
 * the initial local variables of "bmap" are assumed to be the same
3210
 * as those of "context".
3211
 *
3212
 * Run through the equality constraints of "bmap" and "context"
3213
 * in sorted order.
3214
 * If a constraint of "bmap" involves variables not in "context",
3215
 * then it cannot appear in "context".
3216
 * If a matching constraint is found, it is removed from "bmap".
3217
 */
3218
static __isl_give isl_basic_map *drop_equalities(
3219
  __isl_take isl_basic_map *bmap, __isl_keep isl_basic_map *context)
3220
347
{
3221
347
  int i1, i2;
3222
347
  unsigned total, extra;
3223
347
3224
347
  if (
!bmap || 347
!context347
)
3225
0
    return isl_basic_map_free(bmap);
3226
347
3227
347
  total = isl_basic_map_total_dim(context);
3228
347
  extra = isl_basic_map_total_dim(bmap) - total;
3229
347
3230
347
  i1 = bmap->n_eq - 1;
3231
347
  i2 = context->n_eq - 1;
3232
347
3233
373
  while (
bmap && 373
i1 >= 0373
&&
i2 >= 040
)
{26
3234
26
    int last1, last2;
3235
26
3236
26
    if (isl_seq_first_non_zero(bmap->eq[i1] + 1 + total,
3237
26
              extra) != -1)
3238
0
      break;
3239
26
    last1 = isl_seq_last_non_zero(bmap->eq[i1] + 1, total);
3240
26
    last2 = isl_seq_last_non_zero(context->eq[i2] + 1, total);
3241
26
    if (
last1 > last226
)
{0
3242
0
      --i2;
3243
0
      continue;
3244
0
    }
3245
26
    
if (26
last1 < last226
)
{3
3246
3
      --i1;
3247
3
      continue;
3248
3
    }
3249
23
    
if (23
isl_seq_eq(bmap->eq[i1], context->eq[i2], 1 + total)23
)
{23
3250
23
      bmap = isl_basic_map_cow(bmap);
3251
23
      if (isl_basic_map_drop_equality(bmap, i1) < 0)
3252
0
        bmap = isl_basic_map_free(bmap);
3253
23
    }
3254
23
    --i1;
3255
23
    --i2;
3256
23
  }
3257
347
3258
347
  return bmap;
3259
347
}
3260
3261
/* Remove the constraints in "context" from "bmap".
3262
 * "context" is assumed to have explicit representations
3263
 * for all local variables.
3264
 *
3265
 * First align the divs of "bmap" to those of "context" and
3266
 * sort the constraints.  Then drop all constraints from "bmap"
3267
 * that appear in "context".
3268
 */
3269
__isl_give isl_basic_map *isl_basic_map_plain_gist(
3270
  __isl_take isl_basic_map *bmap, __isl_take isl_basic_map *context)
3271
347
{
3272
347
  isl_bool done, known;
3273
347
3274
347
  done = isl_basic_map_plain_is_universe(context);
3275
347
  if (done == isl_bool_false)
3276
347
    done = isl_basic_map_plain_is_universe(bmap);
3277
347
  if (done == isl_bool_false)
3278
347
    done = isl_basic_map_plain_is_empty(context);
3279
347
  if (done == isl_bool_false)
3280
347
    done = isl_basic_map_plain_is_empty(bmap);
3281
347
  if (done < 0)
3282
0
    goto error;
3283
347
  
if (347
done347
)
{0
3284
0
    isl_basic_map_free(context);
3285
0
    return bmap;
3286
0
  }
3287
347
  known = isl_basic_map_divs_known(context);
3288
347
  if (known < 0)
3289
0
    goto error;
3290
347
  
if (347
!known347
)
3291
0
    isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3292
347
      "context has unknown divs", goto error);
3293
347
3294
347
  bmap = isl_basic_map_align_divs(bmap, context);
3295
347
  bmap = isl_basic_map_gauss(bmap, NULL);
3296
347
  bmap = isl_basic_map_sort_constraints(bmap);
3297
347
  context = isl_basic_map_sort_constraints(context);
3298
347
3299
347
  bmap = drop_inequalities(bmap, context);
3300
347
  bmap = drop_equalities(bmap, context);
3301
347
3302
347
  isl_basic_map_free(context);
3303
347
  bmap = isl_basic_map_finalize(bmap);
3304
347
  return bmap;
3305
0
error:
3306
0
  isl_basic_map_free(bmap);
3307
0
  isl_basic_map_free(context);
3308
0
  return NULL;
3309
347
}
3310
3311
/* Replace "map" by the disjunct at position "pos" and free "context".
3312
 */
3313
static __isl_give isl_map *replace_by_disjunct(__isl_take isl_map *map,
3314
  int pos, __isl_take isl_basic_map *context)
3315
7
{
3316
7
  isl_basic_map *bmap;
3317
7
3318
7
  bmap = isl_basic_map_copy(map->p[pos]);
3319
7
  isl_map_free(map);
3320
7
  isl_basic_map_free(context);
3321
7
  return isl_map_from_basic_map(bmap);
3322
7
}
3323
3324
/* Remove the constraints in "context" from "map".
3325
 * If any of the disjuncts in the result turns out to be the universe,
3326
 * then return this universe.
3327
 * "context" is assumed to have explicit representations
3328
 * for all local variables.
3329
 */
3330
__isl_give isl_map *isl_map_plain_gist_basic_map(__isl_take isl_map *map,
3331
  __isl_take isl_basic_map *context)
3332
161
{
3333
161
  int i;
3334
161
  isl_bool univ, known;
3335
161
3336
161
  univ = isl_basic_map_plain_is_universe(context);
3337
161
  if (univ < 0)
3338
0
    goto error;
3339
161
  
if (161
univ161
)
{0
3340
0
    isl_basic_map_free(context);
3341
0
    return map;
3342
0
  }
3343
161
  known = isl_basic_map_divs_known(context);
3344
161
  if (known < 0)
3345
0
    goto error;
3346
161
  
if (161
!known161
)
3347
0
    isl_die(isl_map_get_ctx(map), isl_error_invalid,
3348
161
      "context has unknown divs", goto error);
3349
161
3350
161
  map = isl_map_cow(map);
3351
161
  if (!map)
3352
0
    goto error;
3353
501
  
for (i = 0; 161
i < map->n501
;
++i340
)
{347
3354
347
    map->p[i] = isl_basic_map_plain_gist(map->p[i],
3355
347
            isl_basic_map_copy(context));
3356
347
    univ = isl_basic_map_plain_is_universe(map->p[i]);
3357
347
    if (univ < 0)
3358
0
      goto error;
3359
347
    
if (347
univ && 347
map->n > 17
)
3360
7
      return replace_by_disjunct(map, i, context);
3361
347
  }
3362
161
3363
154
  isl_basic_map_free(context);
3364
154
  ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3365
154
  if (map->n > 1)
3366
154
    ISL_F_CLR(map, ISL_MAP_DISJOINT);
3367
154
  return map;
3368
0
error:
3369
0
  isl_map_free(map);
3370
0
  isl_basic_map_free(context);
3371
0
  return NULL;
3372
161
}
3373
3374
/* Replace "map" by a universe map in the same space and free "drop".
3375
 */
3376
static __isl_give isl_map *replace_by_universe(__isl_take isl_map *map,
3377
  __isl_take isl_map *drop)
3378
5.95k
{
3379
5.95k
  isl_map *res;
3380
5.95k
3381
5.95k
  res = isl_map_universe(isl_map_get_space(map));
3382
5.95k
  isl_map_free(map);
3383
5.95k
  isl_map_free(drop);
3384
5.95k
  return res;
3385
5.95k
}
3386
3387
/* Return a map that has the same intersection with "context" as "map"
3388
 * and that is as "simple" as possible.
3389
 *
3390
 * If "map" is already the universe, then we cannot make it any simpler.
3391
 * Similarly, if "context" is the universe, then we cannot exploit it
3392
 * to simplify "map"
3393
 * If "map" and "context" are identical to each other, then we can
3394
 * return the corresponding universe.
3395
 *
3396
 * If either "map" or "context" consists of multiple disjuncts,
3397
 * then check if "context" happens to be a subset of "map",
3398
 * in which case all constraints can be removed.
3399
 * In case of multiple disjuncts, the standard procedure
3400
 * may not be able to detect that all constraints can be removed.
3401
 *
3402
 * If none of these cases apply, we have to work a bit harder.
3403
 * During this computation, we make use of a single disjunct context,
3404
 * so if the original context consists of more than one disjunct
3405
 * then we need to approximate the context by a single disjunct set.
3406
 * Simply taking the simple hull may drop constraints that are
3407
 * only implicitly available in each disjunct.  We therefore also
3408
 * look for constraints among those defining "map" that are valid
3409
 * for the context.  These can then be used to simplify away
3410
 * the corresponding constraints in "map".
3411
 */
3412
static __isl_give isl_map *map_gist(__isl_take isl_map *map,
3413
  __isl_take isl_map *context)
3414
43.8k
{
3415
43.8k
  int equal;
3416
43.8k
  int is_universe;
3417
43.8k
  int single_disjunct_map, single_disjunct_context;
3418
43.8k
  isl_bool subset;
3419
43.8k
  isl_basic_map *hull;
3420
43.8k
3421
43.8k
  is_universe = isl_map_plain_is_universe(map);
3422
43.8k
  if (
is_universe >= 0 && 43.8k
!is_universe43.8k
)
3423
26.9k
    is_universe = isl_map_plain_is_universe(context);
3424
43.8k
  if (is_universe < 0)
3425
0
    goto error;
3426
43.8k
  
if (43.8k
is_universe43.8k
)
{24.3k
3427
24.3k
    isl_map_free(context);
3428
24.3k
    return map;
3429
24.3k
  }
3430
43.8k
3431
19.4k
  equal = isl_map_plain_is_equal(map, context);
3432
19.4k
  if (equal < 0)
3433
0
    goto error;
3434
19.4k
  
if (19.4k
equal19.4k
)
3435
5.93k
    return replace_by_universe(map, context);
3436
19.4k
3437
13.5k
  single_disjunct_map = isl_map_n_basic_map(map) == 1;
3438
13.5k
  single_disjunct_context = isl_map_n_basic_map(context) == 1;
3439
13.5k
  if (
!single_disjunct_map || 13.5k
!single_disjunct_context9.13k
)
{4.64k
3440
4.64k
    subset = isl_map_is_subset(context, map);
3441
4.64k
    if (subset < 0)
3442
0
      goto error;
3443
4.64k
    
if (4.64k
subset4.64k
)
3444
24
      return replace_by_universe(map, context);
3445
4.64k
  }
3446
13.5k
3447
13.5k
  context = isl_map_compute_divs(context);
3448
13.5k
  if (!context)
3449
0
    goto error;
3450
13.5k
  
if (13.5k
single_disjunct_context13.5k
)
{13.1k
3451
13.1k
    hull = isl_map_simple_hull(context);
3452
409
  } else {
3453
409
    isl_ctx *ctx;
3454
409
    isl_map_list *list;
3455
409
3456
409
    ctx = isl_map_get_ctx(map);
3457
409
    list = isl_map_list_alloc(ctx, 2);
3458
409
    list = isl_map_list_add(list, isl_map_copy(context));
3459
409
    list = isl_map_list_add(list, isl_map_copy(map));
3460
409
    hull = isl_map_unshifted_simple_hull_from_map_list(context,
3461
409
                    list);
3462
409
  }
3463
13.5k
  return isl_map_gist_basic_map(map, hull);
3464
0
error:
3465
0
  isl_map_free(map);
3466
0
  isl_map_free(context);
3467
0
  return NULL;
3468
13.5k
}
3469
3470
__isl_give isl_map *isl_map_gist(__isl_take isl_map *map,
3471
  __isl_take isl_map *context)
3472
43.8k
{
3473
43.8k
  return isl_map_align_params_map_map_and(map, context, &map_gist);
3474
43.8k
}
3475
3476
struct isl_basic_set *isl_basic_set_gist(struct isl_basic_set *bset,
3477
            struct isl_basic_set *context)
3478
792
{
3479
792
  return bset_from_bmap(isl_basic_map_gist(bset_to_bmap(bset),
3480
792
            bset_to_bmap(context)));
3481
792
}
3482
3483
__isl_give isl_set *isl_set_gist_basic_set(__isl_take isl_set *set,
3484
  __isl_take isl_basic_set *context)
3485
1.96k
{
3486
1.96k
  return set_from_map(isl_map_gist_basic_map(set_to_map(set),
3487
1.96k
          bset_to_bmap(context)));
3488
1.96k
}
3489
3490
__isl_give isl_set *isl_set_gist_params_basic_set(__isl_take isl_set *set,
3491
  __isl_take isl_basic_set *context)
3492
0
{
3493
0
  isl_space *space = isl_set_get_space(set);
3494
0
  isl_basic_set *dom_context = isl_basic_set_universe(space);
3495
0
  dom_context = isl_basic_set_intersect_params(dom_context, context);
3496
0
  return isl_set_gist_basic_set(set, dom_context);
3497
0
}
3498
3499
__isl_give isl_set *isl_set_gist(__isl_take isl_set *set,
3500
  __isl_take isl_set *context)
3501
8.24k
{
3502
8.24k
  return set_from_map(isl_map_gist(set_to_map(set), set_to_map(context)));
3503
8.24k
}
3504
3505
/* Compute the gist of "bmap" with respect to the constraints "context"
3506
 * on the domain.
3507
 */
3508
__isl_give isl_basic_map *isl_basic_map_gist_domain(
3509
  __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *context)
3510
0
{
3511
0
  isl_space *space = isl_basic_map_get_space(bmap);
3512
0
  isl_basic_map *bmap_context = isl_basic_map_universe(space);
3513
0
3514
0
  bmap_context = isl_basic_map_intersect_domain(bmap_context, context);
3515
0
  return isl_basic_map_gist(bmap, bmap_context);
3516
0
}
3517
3518
__isl_give isl_map *isl_map_gist_domain(__isl_take isl_map *map,
3519
  __isl_take isl_set *context)
3520
5.21k
{
3521
5.21k
  isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3522
5.21k
  map_context = isl_map_intersect_domain(map_context, context);
3523
5.21k
  return isl_map_gist(map, map_context);
3524
5.21k
}
3525
3526
__isl_give isl_map *isl_map_gist_range(__isl_take isl_map *map,
3527
  __isl_take isl_set *context)
3528
82
{
3529
82
  isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3530
82
  map_context = isl_map_intersect_range(map_context, context);
3531
82
  return isl_map_gist(map, map_context);
3532
82
}
3533
3534
__isl_give isl_map *isl_map_gist_params(__isl_take isl_map *map,
3535
  __isl_take isl_set *context)
3536
26.4k
{
3537
26.4k
  isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3538
26.4k
  map_context = isl_map_intersect_params(map_context, context);
3539
26.4k
  return isl_map_gist(map, map_context);
3540
26.4k
}
3541
3542
__isl_give isl_set *isl_set_gist_params(__isl_take isl_set *set,
3543
  __isl_take isl_set *context)
3544
20.7k
{
3545
20.7k
  return isl_map_gist_params(set, context);
3546
20.7k
}
3547
3548
/* Quick check to see if two basic maps are disjoint.
3549
 * In particular, we reduce the equalities and inequalities of
3550
 * one basic map in the context of the equalities of the other
3551
 * basic map and check if we get a contradiction.
3552
 */
3553
isl_bool isl_basic_map_plain_is_disjoint(__isl_keep isl_basic_map *bmap1,
3554
  __isl_keep isl_basic_map *bmap2)
3555
18.8k
{
3556
18.8k
  struct isl_vec *v = NULL;
3557
18.8k
  int *elim = NULL;
3558
18.8k
  unsigned total;
3559
18.8k
  int i;
3560
18.8k
3561
18.8k
  if (
!bmap1 || 18.8k
!bmap218.8k
)
3562
0
    return isl_bool_error;
3563
18.8k
  
isl_assert18.8k
(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),18.8k
3564
18.8k
      return isl_bool_error);
3565
18.8k
  
if (18.8k
bmap1->n_div || 18.8k
bmap2->n_div17.0k
)
3566
1.98k
    return isl_bool_false;
3567
16.8k
  
if (16.8k
!bmap1->n_eq && 16.8k
!bmap2->n_eq7.69k
)
3568
6.72k
    return isl_bool_false;
3569
16.8k
3570
10.1k
  total = isl_space_dim(bmap1->dim, isl_dim_all);
3571
10.1k
  if (total == 0)
3572
0
    return isl_bool_false;
3573
10.1k
  v = isl_vec_alloc(bmap1->ctx, 1 + total);
3574
10.1k
  if (!v)
3575
0
    goto error;
3576
10.1k
  
elim = 10.1k
isl_alloc_array10.1k
(bmap1->ctx, int, total);
3577
10.1k
  if (!elim)
3578
0
    goto error;
3579
10.1k
  compute_elimination_index(bmap1, elim);
3580
21.3k
  for (i = 0; 
i < bmap2->n_eq21.3k
;
++i11.2k
)
{11.7k
3581
11.7k
    int reduced;
3582
11.7k
    reduced = reduced_using_equalities(v->block.data, bmap2->eq[i],
3583
11.7k
              bmap1, elim);
3584
11.7k
    if (
reduced && 11.7k
!5.71k
isl_int_is_zero5.71k
(v->block.data[0]) &&
3585
1.22k
        isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3586
487
      goto disjoint;
3587
11.7k
  }
3588
80.0k
  
for (i = 0; 9.61k
i < bmap2->n_ineq80.0k
;
++i70.3k
)
{73.3k
3589
73.3k
    int reduced;
3590
73.3k
    reduced = reduced_using_equalities(v->block.data,
3591
73.3k
            bmap2->ineq[i], bmap1, elim);
3592
73.3k
    if (
reduced && 73.3k
isl_int_is_neg4.66k
(v->block.data[0]) &&
3593
3.47k
        isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3594
2.96k
      goto disjoint;
3595
73.3k
  }
3596
6.65k
  compute_elimination_index(bmap2, elim);
3597
46.8k
  for (i = 0; 
i < bmap1->n_ineq46.8k
;
++i40.2k
)
{41.9k
3598
41.9k
    int reduced;
3599
41.9k
    reduced = reduced_using_equalities(v->block.data,
3600
41.9k
            bmap1->ineq[i], bmap2, elim);
3601
41.9k
    if (
reduced && 41.9k
isl_int_is_neg2.67k
(v->block.data[0]) &&
3602
1.96k
        isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3603
1.73k
      goto disjoint;
3604
41.9k
  }
3605
4.92k
  isl_vec_free(v);
3606
4.92k
  free(elim);
3607
4.92k
  return isl_bool_false;
3608
5.18k
disjoint:
3609
5.18k
  isl_vec_free(v);
3610
5.18k
  free(elim);
3611
5.18k
  return isl_bool_true;
3612
0
error:
3613
0
  isl_vec_free(v);
3614
0
  free(elim);
3615
0
  return isl_bool_error;
3616
6.65k
}
3617
3618
int isl_basic_set_plain_is_disjoint(__isl_keep isl_basic_set *bset1,
3619
  __isl_keep isl_basic_set *bset2)
3620
0
{
3621
0
  return isl_basic_map_plain_is_disjoint(bset_to_bmap(bset1),
3622
0
                bset_to_bmap(bset2));
3623
0
}
3624
3625
/* Does "test" hold for all pairs of basic maps in "map1" and "map2"?
3626
 */
3627
static isl_bool all_pairs(__isl_keep isl_map *map1, __isl_keep isl_map *map2,
3628
  isl_bool (*test)(__isl_keep isl_basic_map *bmap1,
3629
    __isl_keep isl_basic_map *bmap2))
3630
14.2k
{
3631
14.2k
  int i, j;
3632
14.2k
3633
14.2k
  if (
!map1 || 14.2k
!map214.2k
)
3634
0
    return isl_bool_error;
3635
14.2k
3636
21.5k
  
for (i = 0; 14.2k
i < map1->n21.5k
;
++i7.26k
)
{16.8k
3637
26.0k
    for (j = 0; 
j < map2->n26.0k
;
++j9.22k
)
{18.7k
3638
18.7k
      isl_bool d = test(map1->p[i], map2->p[j]);
3639
18.7k
      if (d != isl_bool_true)
3640
9.57k
        return d;
3641
18.7k
    }
3642
16.8k
  }
3643
14.2k
3644
4.68k
  return isl_bool_true;
3645
14.2k
}
3646
3647
/* Are "map1" and "map2" obviously disjoint, based on information
3648
 * that can be derived without looking at the individual basic maps?
3649
 *
3650
 * In particular, if one of them is empty or if they live in different spaces
3651
 * (ignoring parameters), then they are clearly disjoint.
3652
 */
3653
static isl_bool isl_map_plain_is_disjoint_global(__isl_keep isl_map *map1,
3654
  __isl_keep isl_map *map2)
3655
29.3k
{
3656
29.3k
  isl_bool disjoint;
3657
29.3k
  isl_bool match;
3658
29.3k
3659
29.3k
  if (
!map1 || 29.3k
!map229.3k
)
3660
0
    return isl_bool_error;
3661
29.3k
3662
29.3k
  disjoint = isl_map_plain_is_empty(map1);
3663
29.3k
  if (
disjoint < 0 || 29.3k
disjoint29.3k
)
3664
4.02k
    return disjoint;
3665
29.3k
3666
25.3k
  disjoint = isl_map_plain_is_empty(map2);
3667
25.3k
  if (
disjoint < 0 || 25.3k
disjoint25.3k
)
3668
5.96k
    return disjoint;
3669
25.3k
3670
19.3k
  match = isl_space_tuple_is_equal(map1->dim, isl_dim_in,
3671
19.3k
        map2->dim, isl_dim_in);
3672
19.3k
  if (
match < 0 || 19.3k
!match19.3k
)
3673
0
    
return match < 0 ? 0
isl_bool_error0
:
isl_bool_true0
;
3674
19.3k
3675
19.3k
  match = isl_space_tuple_is_equal(map1->dim, isl_dim_out,
3676
19.3k
        map2->dim, isl_dim_out);
3677
19.3k
  if (
match < 0 || 19.3k
!match19.3k
)
3678
0
    
return match < 0 ? 0
isl_bool_error0
:
isl_bool_true0
;
3679
19.3k
3680
19.3k
  return isl_bool_false;
3681
19.3k
}
3682
3683
/* Are "map1" and "map2" obviously disjoint?
3684
 *
3685
 * If one of them is empty or if they live in different spaces (ignoring
3686
 * parameters), then they are clearly disjoint.
3687
 * This is checked by isl_map_plain_is_disjoint_global.
3688
 *
3689
 * If they have different parameters, then we skip any further tests.
3690
 *
3691
 * If they are obviously equal, but not obviously empty, then we will
3692
 * not be able to detect if they are disjoint.
3693
 *
3694
 * Otherwise we check if each basic map in "map1" is obviously disjoint
3695
 * from each basic map in "map2".
3696
 */
3697
isl_bool isl_map_plain_is_disjoint(__isl_keep isl_map *map1,
3698
  __isl_keep isl_map *map2)
3699
0
{
3700
0
  isl_bool disjoint;
3701
0
  isl_bool intersect;
3702
0
  isl_bool match;
3703
0
3704
0
  disjoint = isl_map_plain_is_disjoint_global(map1, map2);
3705
0
  if (
disjoint < 0 || 0
disjoint0
)
3706
0
    return disjoint;
3707
0
3708
0
  match = isl_map_has_equal_params(map1, map2);
3709
0
  if (
match < 0 || 0
!match0
)
3710
0
    
return match < 0 ? 0
isl_bool_error0
:
isl_bool_false0
;
3711
0
3712
0
  intersect = isl_map_plain_is_equal(map1, map2);
3713
0
  if (
intersect < 0 || 0
intersect0
)
3714
0
    
return intersect < 0 ? 0
isl_bool_error0
:
isl_bool_false0
;
3715
0
3716
0
  return all_pairs(map1, map2, &isl_basic_map_plain_is_disjoint);
3717
0
}
3718
3719
/* Are "map1" and "map2" disjoint?
3720
 *
3721
 * They are disjoint if they are "obviously disjoint" or if one of them
3722
 * is empty.  Otherwise, they are not disjoint if one of them is universal.
3723
 * If the two inputs are (obviously) equal and not empty, then they are
3724
 * not disjoint.
3725
 * If none of these cases apply, then check if all pairs of basic maps
3726
 * are disjoint.
3727
 */
3728
isl_bool isl_map_is_disjoint(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
3729
29.3k
{
3730
29.3k
  isl_bool disjoint;
3731
29.3k
  isl_bool intersect;
3732
29.3k
3733
29.3k
  disjoint = isl_map_plain_is_disjoint_global(map1, map2);
3734
29.3k
  if (
disjoint < 0 || 29.3k
disjoint29.3k
)
3735
9.99k
    return disjoint;
3736
29.3k
3737
19.3k
  disjoint = isl_map_is_empty(map1);
3738
19.3k
  if (
disjoint < 0 || 19.3k
disjoint19.3k
)
3739
0
    return disjoint;
3740
19.3k
3741
19.3k
  disjoint = isl_map_is_empty(map2);
3742
19.3k
  if (
disjoint < 0 || 19.3k
disjoint19.3k
)
3743
0
    return disjoint;
3744
19.3k
3745
19.3k
  intersect = isl_map_plain_is_universe(map1);
3746
19.3k
  if (
intersect < 0 || 19.3k
intersect19.3k
)
3747
3.19k
    
return intersect < 0 ? 3.19k
isl_bool_error0
:
isl_bool_false3.19k
;
3748
19.3k
3749
16.1k
  intersect = isl_map_plain_is_universe(map2);
3750
16.1k
  if (
intersect < 0 || 16.1k
intersect16.1k
)
3751
1.39k
    
return intersect < 0 ? 1.39k
isl_bool_error0
:
isl_bool_false1.39k
;
3752
16.1k
3753
14.7k
  intersect = isl_map_plain_is_equal(map1, map2);
3754
14.7k
  if (
intersect < 0 || 14.7k
intersect14.7k
)
3755
533
    return isl_bool_not(intersect);
3756
14.7k
3757
14.2k
  return all_pairs(map1, map2, &isl_basic_map_is_disjoint);
3758
14.7k
}
3759
3760
/* Are "bmap1" and "bmap2" disjoint?
3761
 *
3762
 * They are disjoint if they are "obviously disjoint" or if one of them
3763
 * is empty.  Otherwise, they are not disjoint if one of them is universal.
3764
 * If none of these cases apply, we compute the intersection and see if
3765
 * the result is empty.
3766
 */
3767
isl_bool isl_basic_map_is_disjoint(__isl_keep isl_basic_map *bmap1,
3768
  __isl_keep isl_basic_map *bmap2)
3769
18.8k
{
3770
18.8k
  isl_bool disjoint;
3771
18.8k
  isl_bool intersect;
3772
18.8k
  isl_basic_map *test;
3773
18.8k
3774
18.8k
  disjoint = isl_basic_map_plain_is_disjoint(bmap1, bmap2);
3775
18.8k
  if (
disjoint < 0 || 18.8k
disjoint18.8k
)
3776
5.18k
    return disjoint;
3777
18.8k
3778
13.6k
  disjoint = isl_basic_map_is_empty(bmap1);
3779
13.6k
  if (
disjoint < 0 || 13.6k
disjoint13.6k
)
3780
0
    return disjoint;
3781
13.6k
3782
13.6k
  disjoint = isl_basic_map_is_empty(bmap2);
3783
13.6k
  if (
disjoint < 0 || 13.6k
disjoint13.6k
)
3784
0
    return disjoint;
3785
13.6k
3786
13.6k
  intersect = isl_basic_map_plain_is_universe(bmap1);
3787
13.6k
  if (
intersect < 0 || 13.6k
intersect13.6k
)
3788
0
    
return intersect < 0 ? 0
isl_bool_error0
:
isl_bool_false0
;
3789
13.6k
3790
13.6k
  intersect = isl_basic_map_plain_is_universe(bmap2);
3791
13.6k
  if (
intersect < 0 || 13.6k
intersect13.6k
)
3792
0
    
return intersect < 0 ? 0
isl_bool_error0
:
isl_bool_false0
;
3793
13.6k
3794
13.6k
  test = isl_basic_map_intersect(isl_basic_map_copy(bmap1),
3795
13.6k
    isl_basic_map_copy(bmap2));
3796
13.6k
  disjoint = isl_basic_map_is_empty(test);
3797
13.6k
  isl_basic_map_free(test);
3798
13.6k
3799
13.6k
  return disjoint;
3800
13.6k
}
3801
3802
/* Are "bset1" and "bset2" disjoint?
3803
 */
3804
isl_bool isl_basic_set_is_disjoint(__isl_keep isl_basic_set *bset1,
3805
  __isl_keep isl_basic_set *bset2)
3806
21
{
3807
21
  return isl_basic_map_is_disjoint(bset1, bset2);
3808
21
}
3809
3810
isl_bool isl_set_plain_is_disjoint(__isl_keep isl_set *set1,
3811
  __isl_keep isl_set *set2)
3812
0
{
3813
0
  return isl_map_plain_is_disjoint(set_to_map(set1), set_to_map(set2));
3814
0
}
3815
3816
/* Are "set1" and "set2" disjoint?
3817
 */
3818
isl_bool isl_set_is_disjoint(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
3819
10.2k
{
3820
10.2k
  return isl_map_is_disjoint(set1, set2);
3821
10.2k
}
3822
3823
/* Is "v" equal to 0, 1 or -1?
3824
 */
3825
static int is_zero_or_one(isl_int v)
3826
163
{
3827
163
  return 
isl_int_is_zero163
(v) ||
isl_int_is_one56
(v) ||
isl_int_is_negone32
(v);
3828
163
}
3829
3830
/* Check if we can combine a given div with lower bound l and upper
3831
 * bound u with some other div and if so return that other div.
3832
 * Otherwise return -1.
3833
 *
3834
 * We first check that
3835
 *  - the bounds are opposites of each other (except for the constant
3836
 *    term)
3837
 *  - the bounds do not reference any other div
3838
 *  - no div is defined in terms of this div
3839
 *
3840
 * Let m be the size of the range allowed on the div by the bounds.
3841
 * That is, the bounds are of the form
3842
 *
3843
 *  e <= a <= e + m - 1
3844
 *
3845
 * with e some expression in the other variables.
3846
 * We look for another div b such that no third div is defined in terms
3847
 * of this second div b and such that in any constraint that contains
3848
 * a (except for the given lower and upper bound), also contains b
3849
 * with a coefficient that is m times that of b.
3850
 * That is, all constraints (except for the lower and upper bound)
3851
 * are of the form
3852
 *
3853
 *  e + f (a + m b) >= 0
3854
 *
3855
 * Furthermore, in the constraints that only contain b, the coefficient
3856
 * of b should be equal to 1 or -1.
3857
 * If so, we return b so that "a + m b" can be replaced by
3858
 * a single div "c = a + m b".
3859
 */
3860
static int div_find_coalesce(struct isl_basic_map *bmap, int *pairs,
3861
  unsigned div, unsigned l, unsigned u)
3862
1.04k
{
3863
1.04k
  int i, j;
3864
1.04k
  unsigned dim;
3865
1.04k
  int coalesce = -1;
3866
1.04k
3867
1.04k
  if (bmap->n_div <= 1)
3868
347
    return -1;
3869
698
  dim = isl_space_dim(bmap->dim, isl_dim_all);
3870
698
  if (isl_seq_first_non_zero(bmap->ineq[l] + 1 + dim, div) != -1)
3871
439
    return -1;
3872
259
  
if (259
isl_seq_first_non_zero(bmap->ineq[l] + 1 + dim + div + 1,259
3873
259
           bmap->n_div - div - 1) != -1)
3874
29
    return -1;
3875
230
  
if (230
!isl_seq_is_neg(bmap->ineq[l] + 1, bmap->ineq[u] + 1,230
3876
230
          dim + bmap->n_div))
3877
127
    return -1;
3878
230
3879
349
  
for (i = 0; 103
i < bmap->n_div349
;
++i246
)
{246
3880
246
    if (isl_int_is_zero(bmap->div[i][0]))
3881
182
      continue;
3882
64
    
if (64
!64
isl_int_is_zero64
(bmap->div[i][1 + 1 + dim + div]))
3883
0
      return -1;
3884
64
  }
3885
103
3886
103
  
isl_int_add103
(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0]);103
3887
103
  if (
isl_int_is_neg103
(bmap->ineq[l][0]))
{0
3888
0
    isl_int_sub(bmap->ineq[l][0],
3889
0
          bmap->ineq[l][0], bmap->ineq[u][0]);
3890
0
    bmap = isl_basic_map_copy(bmap);
3891
0
    bmap = isl_basic_map_set_to_empty(bmap);
3892
0
    isl_basic_map_free(bmap);
3893
0
    return -1;
3894
0
  }
3895
103
  
isl_int_add_ui103
(bmap->ineq[l][0], bmap->ineq[l][0], 1);103
3896
347
  for (i = 0; 
i < bmap->n_div347
;
++i244
)
{245
3897
245
    if (i == div)
3898
102
      continue;
3899
143
    
if (143
!pairs[i]143
)
3900
80
      continue;
3901
239
    
for (j = 0; 63
j < bmap->n_div239
;
++j176
)
{176
3902
176
      if (isl_int_is_zero(bmap->div[j][0]))
3903
145
        continue;
3904
31
      
if (31
!31
isl_int_is_zero31
(bmap->div[j][1 + 1 + dim + i]))
3905
0
        break;
3906
31
    }
3907
63
    if (j < bmap->n_div)
3908
0
      continue;
3909
278
    
for (j = 0; 63
j < bmap->n_ineq278
;
++j215
)
{277
3910
277
      int valid;
3911
277
      if (
j == l || 277
j == u243
)
3912
68
        continue;
3913
209
      
if (209
isl_int_is_zero209
(bmap->ineq[j][1 + dim + div]))
{163
3914
163
        if (is_zero_or_one(bmap->ineq[j][1 + dim + i]))
3915
143
          continue;
3916
20
        break;
3917
163
      }
3918
46
      
if (46
isl_int_is_zero46
(bmap->ineq[j][1 + dim + i]))
3919
31
        break;
3920
15
      
isl_int_mul15
(bmap->ineq[j][1 + dim + div],15
3921
15
            bmap->ineq[j][1 + dim + div],
3922
15
            bmap->ineq[l][0]);
3923
15
      valid = isl_int_eq(bmap->ineq[j][1 + dim + div],
3924
15
             bmap->ineq[j][1 + dim + i]);
3925
15
      isl_int_divexact(bmap->ineq[j][1 + dim + div],
3926
15
           bmap->ineq[j][1 + dim + div],
3927
15
           bmap->ineq[l][0]);
3928
15
      if (!valid)
3929
11
        break;
3930
15
    }
3931
63
    if (j < bmap->n_ineq)
3932
62
      continue;
3933
1
    coalesce = i;
3934
1
    break;
3935
63
  }
3936
103
  isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
3937
103
  isl_int_sub(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0]);
3938
103
  return coalesce;
3939
103
}
3940
3941
/* Internal data structure used during the construction and/or evaluation of
3942
 * an inequality that ensures that a pair of bounds always allows
3943
 * for an integer value.
3944
 *
3945
 * "tab" is the tableau in which the inequality is evaluated.  It may
3946
 * be NULL until it is actually needed.
3947
 * "v" contains the inequality coefficients.
3948
 * "g", "fl" and "fu" are temporary scalars used during the construction and
3949
 * evaluation.
3950
 */
3951
struct test_ineq_data {
3952
  struct isl_tab *tab;
3953
  isl_vec *v;
3954
  isl_int g;
3955
  isl_int fl;
3956
  isl_int fu;
3957
};
3958
3959
/* Free all the memory allocated by the fields of "data".
3960
 */
3961
static void test_ineq_data_clear(struct test_ineq_data *data)
3962
1.36k
{
3963
1.36k
  isl_tab_free(data->tab);
3964
1.36k
  isl_vec_free(data->v);
3965
1.36k
  isl_int_clear(data->g);
3966
1.36k
  isl_int_clear(data->fl);
3967
1.36k
  isl_int_clear(data->fu);
3968
1.36k
}
3969
3970
/* Is the inequality stored in data->v satisfied by "bmap"?
3971
 * That is, does it only attain non-negative values?
3972
 * data->tab is a tableau corresponding to "bmap".
3973
 */
3974
static isl_bool test_ineq_is_satisfied(__isl_keep isl_basic_map *bmap,
3975
  struct test_ineq_data *data)
3976
1.21k
{
3977
1.21k
  isl_ctx *ctx;
3978
1.21k
  enum isl_lp_result res;
3979
1.21k
3980
1.21k
  ctx = isl_basic_map_get_ctx(bmap);
3981
1.21k
  if (!data->tab)
3982
957
    data->tab = isl_tab_from_basic_map(bmap, 0);
3983
1.21k
  res = isl_tab_min(data->tab, data->v->el, ctx->one, &data->g, NULL, 0);
3984
1.21k
  if (res == isl_lp_error)
3985
0
    return isl_bool_error;
3986
1.21k
  
return res == isl_lp_ok && 1.21k
isl_int_is_nonneg1.21k
(data->g);
3987
1.21k
}
3988
3989
/* Given a lower and an upper bound on div i, do they always allow
3990
 * for an integer value of the given div?
3991
 * Determine this property by constructing an inequality
3992
 * such that the property is guaranteed when the inequality is nonnegative.
3993
 * The lower bound is inequality l, while the upper bound is inequality u.
3994
 * The constructed inequality is stored in data->v.
3995
 *
3996
 * Let the upper bound be
3997
 *
3998
 *  -n_u a + e_u >= 0
3999
 *
4000
 * and the lower bound
4001
 *
4002
 *  n_l a + e_l >= 0
4003
 *
4004
 * Let n_u = f_u g and n_l = f_l g, with g = gcd(n_u, n_l).
4005
 * We have
4006
 *
4007
 *  - f_u e_l <= f_u f_l g a <= f_l e_u
4008
 *
4009
 * Since all variables are integer valued, this is equivalent to
4010
 *
4011
 *  - f_u e_l - (f_u - 1) <= f_u f_l g a <= f_l e_u + (f_l - 1)
4012
 *
4013
 * If this interval is at least f_u f_l g, then it contains at least
4014
 * one integer value for a.
4015
 * That is, the test constraint is
4016
 *
4017
 *  f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 >= f_u f_l g
4018
 *
4019
 * or
4020
 *
4021
 *  f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 - f_u f_l g >= 0
4022
 *
4023
 * If the coefficients of f_l e_u + f_u e_l have a common divisor g',
4024
 * then the constraint can be scaled down by a factor g',
4025
 * with the constant term replaced by
4026
 * floor((f_l e_{u,0} + f_u e_{l,0} + f_l - 1 + f_u - 1 + 1 - f_u f_l g)/g').
4027
 * Note that the result of applying Fourier-Motzkin to this pair
4028
 * of constraints is
4029
 *
4030
 *  f_l e_u + f_u e_l >= 0
4031
 *
4032
 * If the constant term of the scaled down version of this constraint,
4033
 * i.e., floor((f_l e_{u,0} + f_u e_{l,0})/g') is equal to the constant
4034
 * term of the scaled down test constraint, then the test constraint
4035
 * is known to hold and no explicit evaluation is required.
4036
 * This is essentially the Omega test.
4037
 *
4038
 * If the test constraint consists of only a constant term, then
4039
 * it is sufficient to look at the sign of this constant term.
4040
 */
4041
static isl_bool int_between_bounds(__isl_keep isl_basic_map *bmap, int i,
4042
  int l, int u, struct test_ineq_data *data)
4043
2.69k
{
4044
2.69k
  unsigned offset, n_div;
4045
2.69k
  offset = isl_basic_map_offset(bmap, isl_dim_div);
4046
2.69k
  n_div = isl_basic_map_dim(bmap, isl_dim_div);
4047
2.69k
4048
2.69k
  isl_int_gcd(data->g,
4049
2.69k
        bmap->ineq[l][offset + i], bmap->ineq[u][offset + i]);
4050
2.69k
  isl_int_divexact(data->fl, bmap->ineq[l][offset + i], data->g);
4051
2.69k
  isl_int_divexact(data->fu, bmap->ineq[u][offset + i], data->g);
4052
2.69k
  isl_int_neg(data->fu, data->fu);
4053
2.69k
  isl_seq_combine(data->v->el, data->fl, bmap->ineq[u],
4054
2.69k
      data->fu, bmap->ineq[l], offset + n_div);
4055
2.69k
  isl_int_mul(data->g, data->g, data->fl);
4056
2.69k
  isl_int_mul(data->g, data->g, data->fu);
4057
2.69k
  isl_int_sub(data->g, data->g, data->fl);
4058
2.69k
  isl_int_sub(data->g, data->g, data->fu);
4059
2.69k
  isl_int_add_ui(data->g, data->g, 1);
4060
2.69k
  isl_int_sub(data->fl, data->v->el[0], data->g);
4061
2.69k
4062
2.69k
  isl_seq_gcd(data->v->el + 1, offset - 1 + n_div, &data->g);
4063
2.69k
  if (isl_int_is_zero(data->g))
4064
814
    
return 814
isl_int_is_nonneg814
(data->fl);
4065
1.88k
  
if (1.88k
isl_int_is_one1.88k
(data->g))
{937
4066
937
    isl_int_set(data->v->el[0], data->fl);
4067
937
    return test_ineq_is_satisfied(bmap, data);
4068
937
  }
4069
944
  
isl_int_fdiv_q944
(data->fl, data->fl, data->g);944
4070
944
  isl_int_fdiv_q(data->v->el[0], data->v->el[0], data->g);
4071
944
  if (isl_int_eq(data->fl, data->v->el[0]))
4072
669
    return isl_bool_true;
4073
275
  
isl_int_set275
(data->v->el[0], data->fl);275
4074
275
  isl_seq_scale_down(data->v->el + 1, data->v->el + 1, data->g,
4075
275
          offset - 1 + n_div);
4076
275
4077
275
  return test_ineq_is_satisfied(bmap, data);
4078
944
}
4079
4080
/* Remove more kinds of divs that are not strictly needed.
4081
 * In particular, if all pairs of lower and upper bounds on a div
4082
 * are such that they allow at least one integer value of the div,
4083
 * then we can eliminate the div using Fourier-Motzkin without
4084
 * introducing any spurious solutions.
4085
 *
4086
 * If at least one of the two constraints has a unit coefficient for the div,
4087
 * then the presence of such a value is guaranteed so there is no need to check.
4088
 * In particular, the value attained by the bound with unit coefficient
4089
 * can serve as this intermediate value.
4090
 */
4091
static __isl_give isl_basic_map *drop_more_redundant_divs(
4092
  __isl_take isl_basic_map *bmap, __isl_take int *pairs, int n)
4093
1.36k
{
4094
1.36k
  isl_ctx *ctx;
4095
1.36k
  struct test_ineq_data data = { NULL, NULL };
4096
1.36k
  unsigned off, n_div;
4097
1.36k
  int remove = -1;
4098
1.36k
4099
1.36k
  isl_int_init(data.g);
4100
1.36k
  isl_int_init(data.fl);
4101
1.36k
  isl_int_init(data.fu);
4102
1.36k
4103
1.36k
  if (!bmap)
4104
0
    goto error;
4105
1.36k
4106
1.36k
  ctx = isl_basic_map_get_ctx(bmap);
4107
1.36k
  off = isl_basic_map_offset(bmap, isl_dim_div);
4108
1.36k
  n_div = isl_basic_map_dim(bmap, isl_dim_div);
4109
1.36k
  data.v = isl_vec_alloc(ctx, off + n_div);
4110
1.36k
  if (!data.v)
4111
0
    goto error;
4112
1.36k
4113
2.40k
  
while (1.36k
n > 02.40k
)
{1.67k
4114
1.67k
    int i, l, u;
4115
1.67k
    int best = -1;
4116
1.67k
    isl_bool has_int;
4117
1.67k
4118
5.88k
    for (i = 0; 
i < n_div5.88k
;
++i4.20k
)
{4.20k
4119
4.20k
      if (!pairs[i])
4120
2.02k
        continue;
4121
2.17k
      
if (2.17k
best >= 0 && 2.17k
pairs[best] <= pairs[i]501
)
4122
392
        continue;
4123
1.78k
      best = i;
4124
1.78k
    }
4125
1.67k
4126
1.67k
    i = best;
4127
12.5k
    for (l = 0; 
l < bmap->n_ineq12.5k
;
++l10.9k
)
{11.9k
4128
11.9k
      if (
!11.9k
isl_int_is_pos11.9k
(bmap->ineq[l][off + i]))
4129
9.25k
        continue;
4130
2.69k
      
if (2.69k
isl_int_is_one2.69k
(bmap->ineq[l][off + i]))
4131
769
        continue;
4132
17.7k
      
for (u = 0; 1.92k
u < bmap->n_ineq17.7k
;
++u15.8k
)
{16.8k
4133
16.8k
        if (
!16.8k
isl_int_is_neg16.8k
(bmap->ineq[u][off + i]))
4134
13.3k
          continue;
4135
3.53k
        
if (3.53k
isl_int_is_negone3.53k
(bmap->ineq[u][off + i]))
4136
840
          continue;
4137
2.69k
        has_int = int_between_bounds(bmap, i, l, u,
4138
2.69k
                &data);
4139
2.69k
        if (has_int < 0)
4140
0
          goto error;
4141
2.69k
        
if (2.69k
data.tab && 2.69k
data.tab->empty2.09k
)
4142
0
          break;
4143
2.69k
        
if (2.69k
!has_int2.69k
)
4144
1.03k
          break;
4145
2.69k
      }
4146
1.92k
      
if (1.92k
u < bmap->n_ineq1.92k
)
4147
1.03k
        break;
4148
1.92k
    }
4149
1.67k
    
if (1.67k
data.tab && 1.67k
data.tab->empty1.24k
)
{0
4150
0
      bmap = isl_basic_map_set_to_empty(bmap);
4151
0
      break;
4152
0
    }
4153
1.67k
    
if (1.67k
l == bmap->n_ineq1.67k
)
{644
4154
644
      remove = i;
4155
644
      break;
4156
644
    }
4157
1.03k
    pairs[i] = 0;
4158
1.03k
    --n;
4159
1.03k
  }
4160
1.36k
4161
1.36k
  test_ineq_data_clear(&data);
4162
1.36k
4163
1.36k
  free(pairs);
4164
1.36k
4165
1.36k
  if (remove < 0)
4166
723
    return bmap;
4167
1.36k
4168
644
  bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, remove, 1);
4169
644
  return isl_basic_map_drop_redundant_divs(bmap);
4170
0
error:
4171
0
  free(pairs);
4172
0
  isl_basic_map_free(bmap);
4173
0
  test_ineq_data_clear(&data);
4174
0
  return NULL;
4175
1.36k
}
4176
4177
/* Given a pair of divs div1 and div2 such that, except for the lower bound l
4178
 * and the upper bound u, div1 always occurs together with div2 in the form
4179
 * (div1 + m div2), where m is the constant range on the variable div1
4180
 * allowed by l and u, replace the pair div1 and div2 by a single
4181
 * div that is equal to div1 + m div2.
4182
 *
4183
 * The new div will appear in the location that contains div2.
4184
 * We need to modify all constraints that contain
4185
 * div2 = (div - div1) / m
4186
 * The coefficient of div2 is known to be equal to 1 or -1.
4187
 * (If a constraint does not contain div2, it will also not contain div1.)
4188
 * If the constraint also contains div1, then we know they appear
4189
 * as f (div1 + m div2) and we can simply replace (div1 + m div2) by div,
4190
 * i.e., the coefficient of div is f.
4191
 *
4192
 * Otherwise, we first need to introduce div1 into the constraint.
4193
 * Let l be
4194
 *
4195
 *  div1 + f >=0
4196
 *
4197
 * and u
4198
 *
4199
 *  -div1 + f' >= 0
4200
 *
4201
 * A lower bound on div2
4202
 *
4203
 *  div2 + t >= 0
4204
 *
4205
 * can be replaced by
4206
 *
4207
 *  m div2 + div1 + m t + f >= 0
4208
 *
4209
 * An upper bound
4210
 *
4211
 *  -div2 + t >= 0
4212
 *
4213
 * can be replaced by
4214
 *
4215
 *  -(m div2 + div1) + m t + f' >= 0
4216
 *
4217
 * These constraint are those that we would obtain from eliminating
4218
 * div1 using Fourier-Motzkin.
4219
 *
4220
 * After all constraints have been modified, we drop the lower and upper
4221
 * bound and then drop div1.
4222
 * Since the new div is only placed in the same location that used
4223
 * to store div2, but otherwise has a different meaning, any possible
4224
 * explicit representation of the original div2 is removed.
4225
 */
4226
static __isl_give isl_basic_map *coalesce_divs(__isl_take isl_basic_map *bmap,
4227
  unsigned div1, unsigned div2, unsigned l, unsigned u)
4228
1
{
4229
1
  isl_ctx *ctx;
4230
1
  isl_int m;
4231
1
  unsigned dim, total;
4232
1
  int i;
4233
1
4234
1
  ctx = isl_basic_map_get_ctx(bmap);
4235
1
4236
1
  dim = isl_space_dim(bmap->dim, isl_dim_all);
4237
1
  total = 1 + dim + bmap->n_div;
4238
1
4239
1
  isl_int_init(m);
4240
1
  isl_int_add(m, bmap->ineq[l][0], bmap->ineq[u][0]);
4241
1
  isl_int_add_ui(m, m, 1);
4242
1
4243
7
  for (i = 0; 
i < bmap->n_ineq7
;
++i6
)
{6
4244
6
    if (
i == l || 6
i == u5
)
4245
2
      continue;
4246
4
    
if (4
isl_int_is_zero4
(bmap->ineq[i][1 + dim + div2]))
4247
0
      continue;
4248
4
    
if (4
isl_int_is_zero4
(bmap->ineq[i][1 + dim + div1]))
{2
4249
2
      if (isl_int_is_pos(bmap->ineq[i][1 + dim + div2]))
4250
1
        isl_seq_combine(bmap->ineq[i], m, bmap->ineq[i],
4251
1
            ctx->one, bmap->ineq[l], total);
4252
2
      else
4253
1
        isl_seq_combine(bmap->ineq[i], m, bmap->ineq[i],
4254
1
            ctx->one, bmap->ineq[u], total);
4255
2
    }
4256
4
    isl_int_set(bmap->ineq[i][1 + dim + div2],
4257
4
          bmap->ineq[i][1 + dim + div1]);
4258
4
    isl_int_set_si(bmap->ineq[i][1 + dim + div1], 0);
4259
4
  }
4260
1
4261
1
  isl_int_clear(m);
4262
1
  if (
l > u1
)
{0
4263
0
    isl_basic_map_drop_inequality(bmap, l);
4264
0
    isl_basic_map_drop_inequality(bmap, u);
4265
1
  } else {
4266
1
    isl_basic_map_drop_inequality(bmap, u);
4267
1
    isl_basic_map_drop_inequality(bmap, l);
4268
1
  }
4269
1
  bmap = isl_basic_map_mark_div_unknown(bmap, div2);
4270
1
  bmap = isl_basic_map_drop_div(bmap, div1);
4271
1
  return bmap;
4272
1
}
4273
4274
/* First check if we can coalesce any pair of divs and
4275
 * then continue with dropping more redundant divs.
4276
 *
4277
 * We loop over all pairs of lower and upper bounds on a div
4278
 * with coefficient 1 and -1, respectively, check if there
4279
 * is any other div "c" with which we can coalesce the div
4280
 * and if so, perform the coalescing.
4281
 */
4282
static __isl_give isl_basic_map *coalesce_or_drop_more_redundant_divs(
4283
  __isl_take isl_basic_map *bmap, int *pairs, int n)
4284
1.36k
{
4285
1.36k
  int i, l, u;
4286
1.36k
  unsigned dim;
4287
1.36k
4288
1.36k
  dim = isl_space_dim(bmap->dim, isl_dim_all);
4289
1.36k
4290
4.47k
  for (i = 0; 
i < bmap->n_div4.47k
;
++i3.10k
)
{3.10k
4291
3.10k
    if (!pairs[i])
4292
1.34k
      continue;
4293
21.1k
    
for (l = 0; 1.76k
l < bmap->n_ineq21.1k
;
++l19.3k
)
{19.3k
4294
19.3k
      if (
!19.3k
isl_int_is_one19.3k
(bmap->ineq[l][1 + dim + i]))
4295
18.3k
        continue;
4296
11.2k
      
for (u = 0; 1.03k
u < bmap->n_ineq11.2k
;
++u10.1k
)
{10.1k
4297
10.1k
        int c;
4298
10.1k
4299
10.1k
        if (
!10.1k
isl_int_is_negone10.1k
(bmap->ineq[u][1+dim+i]))
4300
9.14k
          continue;
4301
1.04k
        c = div_find_coalesce(bmap, pairs, i, l, u);
4302
1.04k
        if (c < 0)
4303
1.04k
          continue;
4304
1
        free(pairs);
4305
1
        bmap = coalesce_divs(bmap, i, c, l, u);
4306
1
        return isl_basic_map_drop_redundant_divs(bmap);
4307
1.04k
      }
4308
1.03k
    }
4309
1.76k
  }
4310
1.36k
4311
1.36k
  
if (1.36k
ISL_F_ISSET1.36k
(bmap, ISL_BASIC_MAP_EMPTY))
{0
4312
0
    free(pairs);
4313
0
    return bmap;
4314
0
  }
4315
1.36k
4316
1.36k
  return drop_more_redundant_divs(bmap, pairs, n);
4317
1.36k
}
4318
4319
/* Are the "n" coefficients starting at "first" of inequality constraints
4320
 * "i" and "j" of "bmap" equal to each other?
4321
 */
4322
static int is_parallel_part(__isl_keep isl_basic_map *bmap, int i, int j,
4323
  int first, int n)
4324
438
{
4325
438
  return isl_seq_eq(bmap->ineq[i] + first, bmap->ineq[j] + first, n);
4326
438
}
4327
4328
/* Are the "n" coefficients starting at "first" of inequality constraints
4329
 * "i" and "j" of "bmap" opposite to each other?
4330
 */
4331
static int is_opposite_part(__isl_keep isl_basic_map *bmap, int i, int j,
4332
  int first, int n)
4333
3.67k
{
4334
3.67k
  return isl_seq_is_neg(bmap->ineq[i] + first, bmap->ineq[j] + first, n);
4335
3.67k
}
4336
4337
/* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4338
 * apart from the constant term?
4339
 */
4340
static isl_bool is_opposite(__isl_keep isl_basic_map *bmap, int i, int j)
4341
3.21k
{
4342
3.21k
  unsigned total;
4343
3.21k
4344
3.21k
  total = isl_basic_map_dim(bmap, isl_dim_all);
4345
3.21k
  return is_opposite_part(bmap, i, j, 1, total);
4346
3.21k
}
4347
4348
/* Are inequality constraints "i" and "j" of "bmap" equal to each other,
4349
 * apart from the constant term and the coefficient at position "pos"?
4350
 */
4351
static int is_parallel_except(__isl_keep isl_basic_map *bmap, int i, int j,
4352
  int pos)
4353
415
{
4354
415
  unsigned total;
4355
415
4356
415
  total = isl_basic_map_dim(bmap, isl_dim_all);
4357
415
  return is_parallel_part(bmap, i, j, 1, pos - 1) &&
4358
23
    is_parallel_part(bmap, i, j, pos + 1, total - pos);
4359
415
}
4360
4361
/* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4362
 * apart from the constant term and the coefficient at position "pos"?
4363
 */
4364
static int is_opposite_except(__isl_keep isl_basic_map *bmap, int i, int j,
4365
  int pos)
4366
380
{
4367
380
  unsigned total;
4368
380
4369
380
  total = isl_basic_map_dim(bmap, isl_dim_all);
4370
380
  return is_opposite_part(bmap, i, j, 1, pos - 1) &&
4371
75
    is_opposite_part(bmap, i, j, pos + 1, total - pos);
4372
380
}
4373
4374
/* Restart isl_basic_map_drop_redundant_divs after "bmap" has
4375
 * been modified, simplying it if "simplify" is set.
4376
 * Free the temporary data structure "pairs" that was associated
4377
 * to the old version of "bmap".
4378
 */
4379
static __isl_give isl_basic_map *drop_redundant_divs_again(
4380
  __isl_take isl_basic_map *bmap, __isl_take int *pairs, int simplify)
4381
2.83k
{
4382
2.83k
  if (simplify)
4383
1.07k
    bmap = isl_basic_map_simplify(bmap);
4384
2.83k
  free(pairs);
4385
2.83k
  return isl_basic_map_drop_redundant_divs(bmap);
4386
2.83k
}
4387
4388
/* Is "div" the single unknown existentially quantified variable
4389
 * in inequality constraint "ineq" of "bmap"?
4390
 * "div" is known to have a non-zero coefficient in "ineq".
4391
 */
4392
static isl_bool single_unknown(__isl_keep isl_basic_map *bmap, int ineq,
4393
  int div)
4394
1.39k
{
4395
1.39k
  int i;
4396
1.39k
  unsigned n_div, o_div;
4397
1.39k
  isl_bool known;
4398
1.39k
4399
1.39k
  known = isl_basic_map_div_is_known(bmap, div);
4400
1.39k
  if (
known < 0 || 1.39k
known1.39k
)
4401
157
    return isl_bool_not(known);
4402
1.23k
  n_div = isl_basic_map_dim(bmap, isl_dim_div);
4403
1.23k
  if (n_div == 1)
4404
786
    return isl_bool_true;
4405
452
  o_div = isl_basic_map_offset(bmap, isl_dim_div);
4406
1.57k
  for (i = 0; 
i < n_div1.57k
;
++i1.12k
)
{1.16k
4407
1.16k
    isl_bool known;
4408
1.16k
4409
1.16k
    if (i == div)
4410
432
      continue;
4411
729
    
if (729
isl_int_is_zero729
(bmap->ineq[ineq][o_div + i]))
4412
678
      continue;
4413
51
    known = isl_basic_map_div_is_known(bmap, i);
4414
51
    if (
known < 0 || 51
!known51
)
4415
39
      return known;
4416
51
  }
4417
452
4418
413
  return isl_bool_true;
4419
452
}
4420
4421
/* Does integer division "div" have coefficient 1 in inequality constraint
4422
 * "ineq" of "map"?
4423
 */
4424
static isl_bool has_coef_one(__isl_keep isl_basic_map *bmap, int div, int ineq)
4425
1.19k
{
4426
1.19k
  unsigned o_div;
4427
1.19k
4428
1.19k
  o_div = isl_basic_map_offset(bmap, isl_dim_div);
4429
1.19k
  if (isl_int_is_one(bmap->ineq[ineq][o_div + div]))
4430
1.06k
    return isl_bool_true;
4431
1.19k
4432
131
  return isl_bool_false;
4433
1.19k
}
4434
4435
/* Turn inequality constraint "ineq" of "bmap" into an equality and
4436
 * then try and drop redundant divs again,
4437
 * freeing the temporary data structure "pairs" that was associated
4438
 * to the old version of "bmap".
4439
 */
4440
static __isl_give isl_basic_map *set_eq_and_try_again(
4441
  __isl_take isl_basic_map *bmap, int ineq, __isl_take int *pairs)
4442
1.06k
{
4443
1.06k
  bmap = isl_basic_map_cow(bmap);
4444
1.06k
  isl_basic_map_inequality_to_equality(bmap, ineq);
4445
1.06k
  return drop_redundant_divs_again(bmap, pairs, 1);
4446
1.06k
}
4447
4448
/* Drop the integer division at position "div", along with the two
4449
 * inequality constraints "ineq1" and "ineq2" in which it appears
4450
 * from "bmap" and then try and drop redundant divs again,
4451
 * freeing the temporary data structure "pairs" that was associated
4452
 * to the old version of "bmap".
4453
 */
4454
static __isl_give isl_basic_map *drop_div_and_try_again(
4455
  __isl_take isl_basic_map *bmap, int div, int ineq1, int ineq2,
4456
  __isl_take int *pairs)
4457
664
{
4458
664
  if (
ineq1 > ineq2664
)
{416
4459
416
    isl_basic_map_drop_inequality(bmap, ineq1);
4460
416
    isl_basic_map_drop_inequality(bmap, ineq2);
4461
248
  } else {
4462
248
    isl_basic_map_drop_inequality(bmap, ineq2);
4463
248
    isl_basic_map_drop_inequality(bmap, ineq1);
4464
248
  }
4465
664
  bmap = isl_basic_map_drop_div(bmap, div);
4466
664
  return drop_redundant_divs_again(bmap, pairs, 0);
4467
664
}
4468
4469
/* Given two inequality constraints
4470
 *
4471
 *  f(x) + n d + c >= 0,    (ineq)
4472
 *
4473
 * with d the variable at position "pos", and
4474
 *
4475
 *  f(x) + c0 >= 0,     (lower)
4476
 *
4477
 * compute the maximal value of the lower bound ceil((-f(x) - c)/n)
4478
 * determined by the first constraint.
4479
 * That is, store
4480
 *
4481
 *  ceil((c0 - c)/n)
4482
 *
4483
 * in *l.
4484
 */
4485
static void lower_bound_from_parallel(__isl_keep isl_basic_map *bmap,
4486
  int ineq, int lower, int pos, isl_int *l)
4487
16
{
4488
16
  isl_int_neg(*l, bmap->ineq[ineq][0]);
4489
16
  isl_int_add(*l, *l, bmap->ineq[lower][0]);
4490
16
  isl_int_cdiv_q(*l, *l, bmap->ineq[ineq][pos]);
4491
16
}
4492
4493
/* Given two inequality constraints
4494
 *
4495
 *  f(x) + n d + c >= 0,    (ineq)
4496
 *
4497
 * with d the variable at position "pos", and
4498
 *
4499
 *  -f(x) - c0 >= 0,    (upper)
4500
 *
4501
 * compute the minimal value of the lower bound ceil((-f(x) - c)/n)
4502
 * determined by the first constraint.
4503
 * That is, store
4504
 *
4505
 *  ceil((-c1 - c)/n)
4506
 *
4507
 * in *u.
4508
 */
4509
static void lower_bound_from_opposite(__isl_keep isl_basic_map *bmap,
4510
  int ineq, int upper, int pos, isl_int *u)
4511
13
{
4512
13
  isl_int_neg(*u, bmap->ineq[ineq][0]);
4513
13
  isl_int_sub(*u, *u, bmap->ineq[upper][0]);
4514
13
  isl_int_cdiv_q(*u, *u, bmap->ineq[ineq][pos]);
4515
13
}
4516
4517
/* Given a lower bound constraint "ineq" on "div" in "bmap",
4518
 * does the corresponding lower bound have a fixed value in "bmap"?
4519
 *
4520
 * In particular, "ineq" is of the form
4521
 *
4522
 *  f(x) + n d + c >= 0
4523
 *
4524
 * with n > 0, c the constant term and
4525
 * d the existentially quantified variable "div".
4526
 * That is, the lower bound is
4527
 *
4528
 *  ceil((-f(x) - c)/n)
4529
 *
4530
 * Look for a pair of constraints
4531
 *
4532
 *  f(x) + c0 >= 0
4533
 *  -f(x) + c1 >= 0
4534
 *
4535
 * i.e., -c1 <= -f(x) <= c0, that fix ceil((-f(x) - c)/n) to a constant value.
4536
 * That is, check that
4537
 *
4538
 *  ceil((-c1 - c)/n) = ceil((c0 - c)/n)
4539
 *
4540
 * If so, return the index of inequality f(x) + c0 >= 0.
4541
 * Otherwise, return -1.
4542
 */
4543
static int lower_bound_is_cst(__isl_keep isl_basic_map *bmap, int div, int ineq)
4544
131
{
4545
131
  int i;
4546
131
  int lower = -1, upper = -1;
4547
131
  unsigned o_div;
4548
131
  isl_int l, u;
4549
131
  int equal;
4550
131
4551
131
  o_div = isl_basic_map_offset(bmap, isl_dim_div);
4552
846
  for (i = 0; 
i < bmap->n_ineq && 846
(lower < 0 || 726
upper < 056
);
++i715
)
{715
4553
715
    if (i == ineq)
4554
120
      continue;
4555
595
    
if (595
!595
isl_int_is_zero595
(bmap->ineq[i][o_div + div]))
4556
151
      continue;
4557
444
    
if (444
lower < 0 &&444
4558
415
        
is_parallel_except(bmap, ineq, i, o_div + div)415
)
{19
4559
19
      lower = i;
4560
19
      continue;
4561
19
    }
4562
425
    
if (425
upper < 0 &&425
4563
380
        
is_opposite_except(bmap, ineq, i, o_div + div)380
)
{72
4564
72
      upper = i;
4565
72
    }
4566
425
  }
4567
131
4568
131
  if (
lower < 0 || 131
upper < 019
)
4569
118
    return -1;
4570
131
4571
13
  
isl_int_init13
(l);13
4572
13
  isl_int_init(u);
4573
13
4574
13
  lower_bound_from_parallel(bmap, ineq, lower, o_div + div, &l);
4575
13
  lower_bound_from_opposite(bmap, ineq, upper, o_div + div, &u);
4576
13
4577
13
  equal = isl_int_eq(l, u);
4578
13
4579
13
  isl_int_clear(l);
4580
13
  isl_int_clear(u);
4581
13
4582
10
  return equal ? 
lower3
:
-110
;
4583
131
}
4584
4585
/* Given a lower bound constraint "ineq" on the existentially quantified
4586
 * variable "div", such that the corresponding lower bound has
4587
 * a fixed value in "bmap", assign this fixed value to the variable and
4588
 * then try and drop redundant divs again,
4589
 * freeing the temporary data structure "pairs" that was associated
4590
 * to the old version of "bmap".
4591
 * "lower" determines the constant value for the lower bound.
4592
 *
4593
 * In particular, "ineq" is of the form
4594
 *
4595
 *  f(x) + n d + c >= 0,
4596
 *
4597
 * while "lower" is of the form
4598
 *
4599
 *  f(x) + c0 >= 0
4600
 *
4601
 * The lower bound is ceil((-f(x) - c)/n) and its constant value
4602
 * is ceil((c0 - c)/n).
4603
 */
4604
static __isl_give isl_basic_map *fix_cst_lower(__isl_take isl_basic_map *bmap,
4605
  int div, int ineq, int lower, int *pairs)
4606
3
{
4607
3
  isl_int c;
4608
3
  unsigned o_div;
4609
3
4610
3
  isl_int_init(c);
4611
3
4612
3
  o_div = isl_basic_map_offset(bmap, isl_dim_div);
4613
3
  lower_bound_from_parallel(bmap, ineq, lower, o_div + div, &c);
4614
3
  bmap = isl_basic_map_fix(bmap, isl_dim_div, div, c);
4615
3
  free(pairs);
4616
3
4617
3
  isl_int_clear(c);
4618
3
4619
3
  return isl_basic_map_drop_redundant_divs(bmap);
4620
3
}
4621
4622
/* Remove divs that are not strictly needed based on the inequality
4623
 * constraints.
4624
 * In particular, if a div only occurs positively (or negatively)
4625
 * in constraints, then it can simply be dropped.
4626
 * Also, if a div occurs in only two constraints and if moreover
4627
 * those two constraints are opposite to each other, except for the constant
4628
 * term and if the sum of the constant terms is such that for any value
4629
 * of the other values, there is always at least one integer value of the
4630
 * div, i.e., if one plus this sum is greater than or equal to
4631
 * the (absolute value) of the coefficient of the div in the constraints,
4632
 * then we can also simply drop the div.
4633
 *
4634
 * If an existentially quantified variable does not have an explicit
4635
 * representation, appears in only a single lower bound that does not
4636
 * involve any other such existentially quantified variables and appears
4637
 * in this lower bound with coefficient 1,
4638
 * then fix the variable to the value of the lower bound.  That is,
4639
 * turn the inequality into an equality.
4640
 * If for any value of the other variables, there is any value
4641
 * for the existentially quantified variable satisfying the constraints,
4642
 * then this lower bound also satisfies the constraints.
4643
 * It is therefore safe to pick this lower bound.
4644
 *
4645
 * The same reasoning holds even if the coefficient is not one.
4646
 * However, fixing the variable to the value of the lower bound may
4647
 * in general introduce an extra integer division, in which case
4648
 * it may be better to pick another value.
4649
 * If this integer division has a known constant value, then plugging
4650
 * in this constant value removes the existentially quantified variable
4651
 * completely.  In particular, if the lower bound is of the form
4652
 * ceil((-f(x) - c)/n) and there are two constraints, f(x) + c0 >= 0 and
4653
 * -f(x) + c1 >= 0 such that ceil((-c1 - c)/n) = ceil((c0 - c)/n),
4654
 * then the existentially quantified variable can be assigned this
4655
 * shared value.
4656
 *
4657
 * We skip divs that appear in equalities or in the definition of other divs.
4658
 * Divs that appear in the definition of other divs usually occur in at least
4659
 * 4 constraints, but the constraints may have been simplified.
4660
 *
4661
 * If any divs are left after these simple checks then we move on
4662
 * to more complicated cases in drop_more_redundant_divs.
4663
 */
4664
static __isl_give isl_basic_map *isl_basic_map_drop_redundant_divs_ineq(
4665
  __isl_take isl_basic_map *bmap)
4666
154k
{
4667
154k
  int i, j;
4668
154k
  unsigned off;
4669
154k
  int *pairs = NULL;
4670
154k
  int n = 0;
4671
154k
4672
154k
  if (!bmap)
4673
0
    goto error;
4674
154k
  
if (154k
bmap->n_div == 0154k
)
4675
145k
    return bmap;
4676
154k
4677
9.08k
  off = isl_space_dim(bmap->dim, isl_dim_all);
4678
9.08k
  pairs = isl_calloc_array(bmap->ctx, int, bmap->n_div);
4679
9.08k
  if (!pairs)
4680
0
    goto error;
4681
9.08k
4682
18.7k
  
for (i = 0; 9.08k
i < bmap->n_div18.7k
;
++i9.64k
)
{12.4k
4683
12.4k
    int pos, neg;
4684
12.4k
    int last_pos, last_neg;
4685
12.4k
    int redundant;
4686
12.4k
    int defined;
4687
12.4k
    isl_bool opp, set_div;
4688
12.4k
4689
12.4k
    defined = !isl_int_is_zero(bmap->div[i][0]);
4690
30.2k
    for (j = i; 
j < bmap->n_div30.2k
;
++j17.7k
)
4691
18.5k
      
if (18.5k
!18.5k
isl_int_is_zero18.5k
(bmap->div[j][1 + 1 + off + i]))
4692
783
        break;
4693
12.4k
    if (j < bmap->n_div)
4694
783
      continue;
4695
18.7k
    
for (j = 0; 11.7k
j < bmap->n_eq18.7k
;
++j7.07k
)
4696
12.4k
      
if (12.4k
!12.4k
isl_int_is_zero12.4k
(bmap->eq[j][1 + off + i]))
4697
5.40k
        break;
4698
11.7k
    if (j < bmap->n_eq)
4699
5.40k
      continue;
4700
6.29k
    ++n;
4701
6.29k
    pos = neg = 0;
4702
50.7k
    for (j = 0; 
j < bmap->n_ineq50.7k
;
++j44.4k
)
{44.4k
4703
44.4k
      if (
isl_int_is_pos44.4k
(bmap->ineq[j][1 + off + i]))
{7.48k
4704
7.48k
        last_pos = j;
4705
7.48k
        ++pos;
4706
7.48k
      }
4707
44.4k
      if (
isl_int_is_neg44.4k
(bmap->ineq[j][1 + off + i]))
{7.45k
4708
7.45k
        last_neg = j;
4709
7.45k
        ++neg;
4710
7.45k
      }
4711
44.4k
    }
4712
6.29k
    pairs[i] = pos * neg;
4713
6.29k
    if (
pairs[i] == 06.29k
)
{1.09k
4714
1.17k
      for (j = bmap->n_ineq - 1; 
j >= 01.17k
;
--j78
)
4715
78
        
if (78
!78
isl_int_is_zero78
(bmap->ineq[j][1+off+i]))
4716
37
          isl_basic_map_drop_inequality(bmap, j);
4717
1.09k
      bmap = isl_basic_map_drop_div(bmap, i);
4718
1.09k
      return drop_redundant_divs_again(bmap, pairs, 0);
4719
1.09k
    }
4720
5.19k
    
if (5.19k
pairs[i] != 15.19k
)
4721
1.97k
      opp = isl_bool_false;
4722
5.19k
    else
4723
3.21k
      opp = is_opposite(bmap, last_pos, last_neg);
4724
5.19k
    if (opp < 0)
4725
0
      goto error;
4726
5.19k
    
if (5.19k
!opp5.19k
)
{3.00k
4727
3.00k
      int lower;
4728
3.00k
      isl_bool single, one;
4729
3.00k
4730
3.00k
      if (pos != 1)
4731
1.61k
        continue;
4732
1.39k
      single = single_unknown(bmap, last_pos, i);
4733
1.39k
      if (single < 0)
4734
0
        goto error;
4735
1.39k
      
if (1.39k
!single1.39k
)
4736
196
        continue;
4737
1.19k
      one = has_coef_one(bmap, i, last_pos);
4738
1.19k
      if (one < 0)
4739
0
        goto error;
4740
1.19k
      
if (1.19k
one1.19k
)
4741
1.06k
        return set_eq_and_try_again(bmap, last_pos,
4742
1.06k
                  pairs);
4743
131
      lower = lower_bound_is_cst(bmap, i, last_pos);
4744
131
      if (lower >= 0)
4745
3
        return fix_cst_lower(bmap, i, last_pos, lower,
4746
3
            pairs);
4747
128
      continue;
4748
131
    }
4749
5.19k
4750
2.18k
    
isl_int_add2.18k
(bmap->ineq[last_pos][0],2.18k
4751
2.18k
          bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
4752
2.18k
    isl_int_add_ui(bmap->ineq[last_pos][0],
4753
2.18k
             bmap->ineq[last_pos][0], 1);
4754
2.18k
    redundant = isl_int_ge(bmap->ineq[last_pos][0],
4755
2.18k
        bmap->ineq[last_pos][1+off+i]);
4756
2.18k
    isl_int_sub_ui(bmap->ineq[last_pos][0],
4757
2.18k
             bmap->ineq[last_pos][0], 1);
4758
2.18k
    isl_int_sub(bmap->ineq[last_pos][0],
4759
2.18k
          bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
4760
2.18k
    if (redundant)
4761
664
      return drop_div_and_try_again(bmap, i,
4762
664
                last_pos, last_neg, pairs);
4763
1.52k
    
if (1.52k
defined1.52k
)
4764
1.51k
      set_div = isl_bool_false;
4765
1.52k
    else
4766
9
      set_div = ok_to_set_div_from_bound(bmap, i, last_pos);
4767
1.52k
    if (set_div < 0)
4768
0
      return isl_basic_map_free(bmap);
4769
1.52k
    
if (1.52k
set_div1.52k
)
{3
4770
3
      bmap = set_div_from_lower_bound(bmap, i, last_pos);
4771
3
      return drop_redundant_divs_again(bmap, pairs, 1);
4772
3
    }
4773
1.52k
    pairs[i] = 0;
4774
1.52k
    --n;
4775
1.52k
  }
4776
9.08k
4777
6.24k
  
if (6.24k
n > 06.24k
)
4778
1.36k
    return coalesce_or_drop_more_redundant_divs(bmap, pairs, n);
4779
6.24k
4780
4.87k
  free(pairs);
4781
4.87k
  return bmap;
4782
0
error:
4783
0
  free(pairs);
4784
0
  isl_basic_map_free(bmap);
4785
0
  return NULL;
4786
6.24k
}
4787
4788
/* Consider the coefficients at "c" as a row vector and replace
4789
 * them with their product with "T".  "T" is assumed to be a square matrix.
4790
 */
4791
static isl_stat preimage(isl_int *c, __isl_keep isl_mat *T)
4792
10.6k
{
4793
10.6k
  int n;
4794
10.6k
  isl_ctx *ctx;
4795
10.6k
  isl_vec *v;
4796
10.6k
4797
10.6k
  if (!T)
4798
0
    return isl_stat_error;
4799
10.6k
  n = isl_mat_rows(T);
4800
10.6k
  if (isl_seq_first_non_zero(c, n) == -1)
4801
4.92k
    return isl_stat_ok;
4802
5.69k
  ctx = isl_mat_get_ctx(T);
4803
5.69k
  v = isl_vec_alloc(ctx, n);
4804
5.69k
  if (!v)
4805
0
    return isl_stat_error;
4806
5.69k
  isl_seq_swp_or_cpy(v->el, c, n);
4807
5.69k