Coverage Report

Created: 2017-04-29 12:21

/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
475k
{
33
475k
  isl_int *t = bmap->eq[a];
34
475k
  bmap->eq[a] = bmap->eq[b];
35
475k
  bmap->eq[b] = t;
36
475k
}
37
38
static void swap_inequality(struct isl_basic_map *bmap, int a, int b)
39
43.8k
{
40
43.8k
  if (
a != b43.8k
)
{43.8k
41
43.8k
    isl_int *t = bmap->ineq[a];
42
43.8k
    bmap->ineq[a] = bmap->ineq[b];
43
43.8k
    bmap->ineq[b] = t;
44
43.8k
  }
45
43.8k
}
46
47
__isl_give isl_basic_map *isl_basic_map_normalize_constraints(
48
  __isl_take isl_basic_map *bmap)
49
1.93M
{
50
1.93M
  int i;
51
1.93M
  isl_int gcd;
52
1.93M
  unsigned total = isl_basic_map_total_dim(bmap);
53
1.93M
54
1.93M
  if (!bmap)
55
0
    return NULL;
56
1.93M
57
1.93M
  
isl_int_init1.93M
(gcd);1.93M
58
3.87M
  for (i = bmap->n_eq - 1; 
i >= 03.87M
;
--i1.93M
)
{1.93M
59
1.93M
    isl_seq_gcd(bmap->eq[i]+1, total, &gcd);
60
1.93M
    if (
isl_int_is_zero1.93M
(gcd))
{163k
61
163k
      if (
!163k
isl_int_is_zero163k
(bmap->eq[i][0]))
{1.94k
62
1.94k
        bmap = isl_basic_map_set_to_empty(bmap);
63
1.94k
        break;
64
1.94k
      }
65
161k
      isl_basic_map_drop_equality(bmap, i);
66
161k
      continue;
67
163k
    }
68
1.77M
    
if (1.77M
ISL_F_ISSET1.77M
(bmap, ISL_BASIC_MAP_RATIONAL))
69
161k
      isl_int_gcd(gcd, gcd, bmap->eq[i][0]);
70
1.77M
    if (isl_int_is_one(gcd))
71
1.74M
      continue;
72
31.4k
    
if (31.4k
!31.4k
isl_int_is_divisible_by31.4k
(bmap->eq[i][0], gcd))
{801
73
801
      bmap = isl_basic_map_set_to_empty(bmap);
74
801
      break;
75
801
    }
76
30.6k
    isl_seq_scale_down(bmap->eq[i], bmap->eq[i], gcd, 1+total);
77
30.6k
  }
78
1.93M
79
9.13M
  for (i = bmap->n_ineq - 1; 
i >= 09.13M
;
--i7.19M
)
{7.21M
80
7.21M
    isl_seq_gcd(bmap->ineq[i]+1, total, &gcd);
81
7.21M
    if (
isl_int_is_zero7.21M
(gcd))
{311k
82
311k
      if (
isl_int_is_neg311k
(bmap->ineq[i][0]))
{20.8k
83
20.8k
        bmap = isl_basic_map_set_to_empty(bmap);
84
20.8k
        break;
85
20.8k
      }
86
290k
      isl_basic_map_drop_inequality(bmap, i);
87
290k
      continue;
88
311k
    }
89
6.90M
    
if (6.90M
ISL_F_ISSET6.90M
(bmap, ISL_BASIC_MAP_RATIONAL))
90
305k
      isl_int_gcd(gcd, gcd, bmap->ineq[i][0]);
91
6.90M
    if (isl_int_is_one(gcd))
92
6.80M
      continue;
93
99.0k
    
isl_int_fdiv_q99.0k
(bmap->ineq[i][0], bmap->ineq[i][0], gcd);99.0k
94
99.0k
    isl_seq_scale_down(bmap->ineq[i]+1, bmap->ineq[i]+1, gcd, total);
95
99.0k
  }
96
1.93M
  isl_int_clear(gcd);
97
1.93M
98
1.93M
  return bmap;
99
1.93M
}
100
101
struct isl_basic_set *isl_basic_set_normalize_constraints(
102
  struct isl_basic_set *bset)
103
130k
{
104
130k
  isl_basic_map *bmap = bset_to_bmap(bset);
105
130k
  return bset_from_bmap(isl_basic_map_normalize_constraints(bmap));
106
130k
}
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
7.46k
{
131
7.46k
  isl_int shift;
132
7.46k
  int add_one;
133
7.46k
134
7.46k
  isl_int_init(shift);
135
7.46k
  isl_int_fdiv_r(shift, bmap->div[div][1 + pos], bmap->div[div][0]);
136
7.46k
  isl_int_mul_ui(shift, shift, 2);
137
7.46k
  add_one = isl_int_gt(shift, bmap->div[div][0]);
138
7.46k
  isl_int_fdiv_q(shift, bmap->div[div][1 + pos], bmap->div[div][0]);
139
7.46k
  if (add_one)
140
2.93k
    isl_int_add_ui(shift, shift, 1);
141
7.46k
  isl_int_neg(shift, shift);
142
7.46k
  bmap = isl_basic_map_shift_div(bmap, div, pos, shift);
143
7.46k
  isl_int_clear(shift);
144
7.46k
145
7.46k
  return bmap;
146
7.46k
}
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
523k
{
157
523k
  isl_bool r;
158
523k
159
523k
  if (isl_int_is_zero(bmap->div[div][1 + pos]))
160
400k
    return isl_bool_false;
161
523k
162
123k
  
isl_int_mul_ui123k
(bmap->div[div][1 + pos], bmap->div[div][1 + pos], 2);123k
163
123k
  r = isl_int_abs_ge(bmap->div[div][1 + pos], bmap->div[div][0]) &&
164
38.1k
      
!38.1k
isl_int_eq38.1k
(bmap->div[div][1 + pos], bmap->div[div][0]);
165
123k
  isl_int_divexact_ui(bmap->div[div][1 + pos],
166
123k
          bmap->div[div][1 + pos], 2);
167
123k
168
123k
  return r;
169
523k
}
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
71.8k
{
179
71.8k
  int i;
180
71.8k
  unsigned total = 1 + isl_basic_map_total_dim(bmap);
181
71.8k
182
595k
  for (i = 0; 
i < total595k
;
++i523k
)
{523k
183
523k
    isl_bool reduce;
184
523k
185
523k
    reduce = needs_reduction(bmap, div, i);
186
523k
    if (reduce < 0)
187
0
      return isl_basic_map_free(bmap);
188
523k
    
if (523k
!reduce523k
)
189
516k
      continue;
190
7.46k
    bmap = reduce_coefficient_in_div(bmap, div, i);
191
7.46k
    if (!bmap)
192
0
      break;
193
7.46k
  }
194
71.8k
195
71.8k
  return bmap;
196
71.8k
}
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.78M
{
206
1.78M
  int i;
207
1.78M
208
1.78M
  if (!bmap)
209
0
    return NULL;
210
1.78M
  
if (1.78M
bmap->n_div == 01.78M
)
211
1.62M
    return bmap;
212
1.78M
213
433k
  
for (i = 0; 159k
i < bmap->n_div433k
;
++i274k
)
{274k
214
274k
    if (isl_int_is_zero(bmap->div[i][0]))
215
202k
      continue;
216
71.8k
    bmap = reduce_div_coefficients_of_div(bmap, i);
217
71.8k
    if (!bmap)
218
0
      break;
219
71.8k
  }
220
159k
221
159k
  return bmap;
222
1.78M
}
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
281k
{
239
281k
  unsigned total = isl_basic_map_total_dim(bmap);
240
281k
  isl_ctx *ctx = bmap->ctx;
241
281k
242
281k
  if (isl_int_is_zero(bmap->div[div][0]))
243
202k
    return;
244
79.5k
  isl_seq_gcd(bmap->div[div] + 2, total, &ctx->normalize_gcd);
245
79.5k
  isl_int_gcd(ctx->normalize_gcd, ctx->normalize_gcd, bmap->div[div][0]);
246
79.5k
  if (isl_int_is_one(ctx->normalize_gcd))
247
78.5k
    return;
248
1.02k
  
isl_int_fdiv_q1.02k
(bmap->div[div][1], bmap->div[div][1],1.02k
249
1.02k
      ctx->normalize_gcd);
250
1.02k
  isl_int_divexact(bmap->div[div][0], bmap->div[div][0],
251
1.02k
      ctx->normalize_gcd);
252
1.02k
  isl_seq_scale_down(bmap->div[div] + 2, bmap->div[div] + 2,
253
1.02k
      ctx->normalize_gcd, total);
254
1.02k
}
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.78M
{
272
1.78M
  int i;
273
1.78M
274
1.78M
  if (!bmap)
275
0
    return NULL;
276
1.78M
  
if (1.78M
bmap->n_div == 01.78M
)
277
1.62M
    return bmap;
278
1.78M
279
433k
  
for (i = 0; 159k
i < bmap->n_div433k
;
++i274k
)
280
274k
    normalize_div_expression(bmap, i);
281
159k
282
159k
  return bmap;
283
1.78M
}
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.66M
{
290
2.66M
  unsigned total;
291
2.66M
  unsigned space_total;
292
2.66M
  int k;
293
2.66M
  int last_div;
294
2.66M
295
2.66M
  total = isl_basic_map_total_dim(bmap);
296
2.66M
  space_total = isl_space_dim(bmap->dim, isl_dim_all);
297
2.66M
  last_div = isl_seq_last_non_zero(eq + 1 + space_total, bmap->n_div);
298
17.0M
  for (k = 0; 
k < bmap->n_eq17.0M
;
++k14.3M
)
{14.3M
299
14.3M
    if (bmap->eq[k] == eq)
300
2.65M
      continue;
301
11.7M
    
if (11.7M
isl_int_is_zero11.7M
(bmap->eq[k][1+pos]))
302
10.9M
      continue;
303
733k
    
if (733k
progress733k
)
304
153k
      *progress = 1;
305
733k
    isl_seq_elim(bmap->eq[k], eq, 1+pos, 1+total, NULL);
306
733k
    isl_seq_normalize(bmap->ctx, bmap->eq[k], 1 + total);
307
733k
  }
308
2.66M
309
12.4M
  for (k = 0; 
k < bmap->n_ineq12.4M
;
++k9.75M
)
{9.75M
310
9.75M
    if (isl_int_is_zero(bmap->ineq[k][1+pos]))
311
9.14M
      continue;
312
607k
    
if (607k
progress607k
)
313
229k
      *progress = 1;
314
607k
    isl_seq_elim(bmap->ineq[k], eq, 1+pos, 1+total, NULL);
315
607k
    isl_seq_normalize(bmap->ctx, bmap->ineq[k], 1 + total);
316
607k
    ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
317
607k
  }
318
2.66M
319
3.19M
  for (k = 0; 
k < bmap->n_div3.19M
;
++k534k
)
{534k
320
534k
    if (isl_int_is_zero(bmap->div[k][0]))
321
352k
      continue;
322
182k
    
if (182k
isl_int_is_zero182k
(bmap->div[k][1+1+pos]))
323
174k
      continue;
324
7.68k
    
if (7.68k
progress7.68k
)
325
3.66k
      *progress = 1;
326
7.68k
    /* We need to be careful about circular definitions,
327
7.68k
     * so for now we just remove the definition of div k
328
7.68k
     * if the equality contains any divs.
329
7.68k
     * If keep_divs is set, then the divs have been ordered
330
7.68k
     * and we can keep the definition as long as the result
331
7.68k
     * is still ordered.
332
7.68k
     */
333
7.68k
    if (
last_div == -1 || 7.68k
(keep_divs && 2.75k
last_div < k2.75k
))
{7.68k
334
7.68k
      isl_seq_elim(bmap->div[k]+1, eq,
335
7.68k
          1+pos, 1+total, &bmap->div[k][0]);
336
7.68k
      normalize_div_expression(bmap, k);
337
7.68k
    } else
338
0
      isl_seq_clr(bmap->div[k], 1 + total);
339
7.68k
    ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
340
7.68k
  }
341
2.66M
}
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
152k
{
348
152k
  unsigned pos = isl_space_dim(bmap->dim, isl_dim_all) + div;
349
152k
350
152k
  eliminate_var_using_equality(bmap, pos, eq, keep_divs, NULL);
351
152k
352
152k
  bmap = isl_basic_map_drop_div(bmap, div);
353
152k
354
152k
  return bmap;
355
152k
}
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
152k
{
363
152k
  int k;
364
152k
  int last_div;
365
152k
  unsigned space_total = isl_space_dim(bmap->dim, isl_dim_all);
366
152k
  unsigned pos = space_total + div;
367
152k
368
152k
  last_div = isl_seq_last_non_zero(eq + 1 + space_total, bmap->n_div);
369
152k
  if (
last_div < 0 || 152k
last_div <= div152k
)
370
148k
    return isl_bool_true;
371
152k
372
14.2k
  
for (k = 0; 4.23k
k <= last_div14.2k
;
++k9.99k
)
{13.8k
373
13.8k
    if (isl_int_is_zero(bmap->div[k][0]))
374
5.52k
      continue;
375
8.36k
    
if (8.36k
!8.36k
isl_int_is_zero8.36k
(bmap->div[k][1 + 1 + pos]))
376
3.89k
      return isl_bool_false;
377
8.36k
  }
378
4.23k
379
340
  return isl_bool_true;
380
4.23k
}
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
1.86M
{
387
1.86M
  int d;
388
1.86M
  int i;
389
1.86M
  int modified = 0;
390
1.86M
  unsigned off;
391
1.86M
392
1.86M
  bmap = isl_basic_map_order_divs(bmap);
393
1.86M
394
1.86M
  if (!bmap)
395
0
    return NULL;
396
1.86M
397
1.86M
  off = 1 + isl_space_dim(bmap->dim, isl_dim_all);
398
1.86M
399
2.15M
  for (d = bmap->n_div - 1; 
d >= 02.15M
;
--d287k
)
{287k
400
617k
    for (i = 0; 
i < bmap->n_eq617k
;
++i330k
)
{479k
401
479k
      isl_bool ok;
402
479k
403
479k
      if (
!479k
isl_int_is_one479k
(bmap->eq[i][off + d]) &&
404
356k
          
!356k
isl_int_is_negone356k
(bmap->eq[i][off + d]))
405
326k
        continue;
406
152k
      ok = ok_to_eliminate_div(bmap, bmap->eq[i], d);
407
152k
      if (ok < 0)
408
0
        return isl_basic_map_free(bmap);
409
152k
      
if (152k
!ok152k
)
410
3.89k
        continue;
411
148k
      modified = 1;
412
148k
      *progress = 1;
413
148k
      bmap = eliminate_div(bmap, bmap->eq[i], d, 1);
414
148k
      if (isl_basic_map_drop_equality(bmap, i) < 0)
415
0
        return isl_basic_map_free(bmap);
416
148k
      break;
417
148k
    }
418
287k
  }
419
1.86M
  
if (1.86M
modified1.86M
)
420
82.0k
    return eliminate_divs_eq(bmap, progress);
421
1.78M
  return bmap;
422
1.86M
}
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.78M
{
429
1.78M
  int d;
430
1.78M
  int i;
431
1.78M
  unsigned off;
432
1.78M
  struct isl_ctx *ctx;
433
1.78M
434
1.78M
  if (!bmap)
435
0
    return NULL;
436
1.78M
437
1.78M
  ctx = bmap->ctx;
438
1.78M
  off = 1 + isl_space_dim(bmap->dim, isl_dim_all);
439
1.78M
440
1.90M
  for (d = bmap->n_div - 1; 
d >= 01.90M
;
--d121k
)
{121k
441
196k
    for (i = 0; 
i < bmap->n_eq196k
;
++i74.7k
)
442
113k
      
if (113k
!113k
isl_int_is_zero113k
(bmap->eq[i][off + d]))
443
38.6k
        break;
444
121k
    if (i < bmap->n_eq)
445
38.6k
      continue;
446
316k
    
for (i = 0; 82.8k
i < bmap->n_ineq316k
;
++i234k
)
447
269k
      
if (269k
isl_int_abs_gt269k
(bmap->ineq[i][off + d], ctx->one))
448
35.4k
        break;
449
82.8k
    if (i < bmap->n_ineq)
450
35.4k
      continue;
451
47.4k
    *progress = 1;
452
47.4k
    bmap = isl_basic_map_eliminate_vars(bmap, (off-1)+d, 1);
453
47.4k
    if (
!bmap || 47.4k
ISL_F_ISSET47.4k
(bmap, ISL_BASIC_MAP_EMPTY))
454
69
      break;
455
47.3k
    bmap = isl_basic_map_drop_div(bmap, d);
456
47.3k
    if (!bmap)
457
0
      break;
458
47.3k
  }
459
1.78M
  return bmap;
460
1.78M
}
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
1.80k
{
469
1.80k
  unsigned o_div;
470
1.80k
  int i;
471
1.80k
472
1.80k
  if (!bmap)
473
0
    return isl_bool_error;
474
1.80k
475
1.80k
  o_div = isl_basic_map_offset(bmap, isl_dim_div);
476
2.31k
  for (i = 0; 
i < n2.31k
;
++i510
)
{875
477
875
    isl_bool unknown;
478
875
479
875
    if (isl_int_is_zero(bmap->eq[eq][o_div + first + i]))
480
497
      continue;
481
378
    unknown = isl_basic_map_div_is_marked_unknown(bmap, first + i);
482
378
    if (unknown < 0)
483
0
      return isl_bool_error;
484
378
    
if (378
unknown378
)
485
365
      return isl_bool_true;
486
378
  }
487
1.80k
488
1.43k
  return isl_bool_false;
489
1.80k
}
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
45.8k
{
517
45.8k
  unsigned total, o_div;
518
45.8k
  isl_bool involves;
519
45.8k
520
45.8k
  if (!bmap)
521
0
    return NULL;
522
45.8k
523
45.8k
  
if (45.8k
!45.8k
isl_int_is_zero45.8k
(bmap->div[div][0]))
524
44.0k
    return bmap;
525
45.8k
526
1.80k
  involves = bmap_eq_involves_unknown_divs(bmap, eq, 0, div);
527
1.80k
  if (involves < 0)
528
0
    return isl_basic_map_free(bmap);
529
1.80k
  
if (1.80k
involves1.80k
)
530
365
    return bmap;
531
1.80k
532
1.43k
  total = isl_basic_map_dim(bmap, isl_dim_all);
533
1.43k
  o_div = isl_basic_map_offset(bmap, isl_dim_div);
534
1.43k
  isl_seq_neg(bmap->div[div] + 1, bmap->eq[eq], 1 + total);
535
1.43k
  isl_int_set_si(bmap->div[div][1 + o_div + div], 0);
536
1.43k
  isl_int_set(bmap->div[div][0], bmap->eq[eq][o_div + div]);
537
1.43k
  if (progress)
538
1.20k
    *progress = 1;
539
1.43k
  ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
540
1.43k
541
1.43k
  return bmap;
542
1.80k
}
543
544
__isl_give isl_basic_map *isl_basic_map_gauss(__isl_take isl_basic_map *bmap,
545
  int *progress)
546
3.02M
{
547
3.02M
  int k;
548
3.02M
  int done;
549
3.02M
  int last_var;
550
3.02M
  unsigned total_var;
551
3.02M
  unsigned total;
552
3.02M
553
3.02M
  bmap = isl_basic_map_order_divs(bmap);
554
3.02M
555
3.02M
  if (!bmap)
556
0
    return NULL;
557
3.02M
558
3.02M
  total = isl_basic_map_total_dim(bmap);
559
3.02M
  total_var = total - bmap->n_div;
560
3.02M
561
3.02M
  last_var = total - 1;
562
5.53M
  for (done = 0; 
done < bmap->n_eq5.53M
;
++done2.50M
)
{2.68M
563
5.48M
    for (; 
last_var >= 05.48M
;
--last_var2.79M
)
{5.30M
564
16.7M
      for (k = done; 
k < bmap->n_eq16.7M
;
++k11.4M
)
565
13.9M
        
if (13.9M
!13.9M
isl_int_is_zero13.9M
(bmap->eq[k][1+last_var]))
566
2.50M
          break;
567
5.30M
      if (k < bmap->n_eq)
568
2.50M
        break;
569
5.30M
    }
570
2.68M
    if (last_var < 0)
571
181k
      break;
572
2.50M
    
if (2.50M
k != done2.50M
)
573
475k
      swap_equality(bmap, k, done);
574
2.50M
    if (isl_int_is_neg(bmap->eq[done][1+last_var]))
575
264k
      isl_seq_neg(bmap->eq[done], bmap->eq[done], 1+total);
576
2.50M
577
2.50M
    eliminate_var_using_equality(bmap, last_var, bmap->eq[done], 1,
578
2.50M
            progress);
579
2.50M
580
2.50M
    if (last_var >= total_var)
581
45.8k
      bmap = set_div_from_eq(bmap, last_var - total_var,
582
45.8k
            done, progress);
583
2.50M
    if (!bmap)
584
0
      return NULL;
585
2.50M
  }
586
3.02M
  
if (3.02M
done == bmap->n_eq3.02M
)
587
2.84M
    return bmap;
588
526k
  
for (k = done; 181k
k < bmap->n_eq526k
;
++k345k
)
{383k
589
383k
    if (isl_int_is_zero(bmap->eq[k][0]))
590
345k
      continue;
591
38.2k
    return isl_basic_map_set_to_empty(bmap);
592
383k
  }
593
142k
  isl_basic_map_free_equality(bmap, bmap->n_eq-done);
594
142k
  return bmap;
595
181k
}
596
597
struct isl_basic_set *isl_basic_set_gauss(
598
  struct isl_basic_set *bset, int *progress)
599
95.0k
{
600
95.0k
  return bset_from_bmap(isl_basic_map_gauss(bset_to_bmap(bset),
601
95.0k
              progress));
602
95.0k
}
603
604
605
static unsigned int round_up(unsigned int v)
606
1.23M
{
607
1.23M
  int old_v = v;
608
1.23M
609
3.71M
  while (
v3.71M
)
{2.47M
610
2.47M
    old_v = v;
611
2.47M
    v ^= v & -v;
612
2.47M
  }
613
1.23M
  return old_v << 1;
614
1.23M
}
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.22M
{
636
1.22M
  isl_ctx *ctx;
637
1.22M
638
1.22M
  ci->index = NULL;
639
1.22M
  if (!bmap)
640
0
    return isl_stat_error;
641
1.22M
  ci->total = isl_basic_set_total_dim(bmap);
642
1.22M
  if (bmap->n_ineq == 0)
643
0
    return isl_stat_ok;
644
1.22M
  ci->size = round_up(4 * (bmap->n_ineq + 1) / 3 - 1);
645
1.22M
  ci->bits = ffs(ci->size) - 1;
646
1.22M
  ctx = isl_basic_map_get_ctx(bmap);
647
1.22M
  ci->index = isl_calloc_array(ctx, isl_int **, ci->size);
648
1.22M
  if (!ci->index)
649
0
    return isl_stat_error;
650
1.22M
651
1.22M
  return isl_stat_ok;
652
1.22M
}
653
654
/* Free the memory allocated by create_constraint_index.
655
 */
656
static void constraint_index_free(struct isl_constraint_index *ci)
657
1.22M
{
658
1.22M
  free(ci->index);
659
1.22M
}
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.0M
{
669
10.0M
  int h;
670
10.0M
  uint32_t hash = isl_seq_get_hash_bits((*ineq) + 1, ci->total, ci->bits);
671
13.5M
  for (h = hash; 
ci->index[h]13.5M
;
h = (h+1) % ci->size3.46M
)
672
7.22M
    
if (7.22M
ineq != ci->index[h] &&7.22M
673
6.99M
        isl_seq_eq((*ineq) + 1, ci->index[h][0]+1, ci->total))
674
3.75M
      break;
675
10.0M
  return h;
676
10.0M
}
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.0M
{
688
10.0M
  return hash_index_ineq(ci, &bmap->ineq[k]);
689
10.0M
}
690
691
static int set_hash_index(struct isl_constraint_index *ci,
692
  __isl_keep isl_basic_set *bset, int k)
693
32.7k
{
694
32.7k
  return hash_index(ci, bset, k);
695
32.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
9.31k
{
702
9.31k
  int k, h;
703
9.31k
704
9.31k
  if (create_constraint_index(ci, bset) < 0)
705
0
    return isl_stat_error;
706
9.31k
707
42.1k
  
for (k = 0; 9.31k
k < bset->n_ineq42.1k
;
++k32.7k
)
{32.7k
708
32.7k
    h = set_hash_index(ci, bset, k);
709
32.7k
    ci->index[h] = &bset->ineq[k];
710
32.7k
  }
711
9.31k
712
9.31k
  return isl_stat_ok;
713
9.31k
}
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
25.0k
{
731
25.0k
  int h;
732
25.0k
733
25.0k
  h = hash_index_ineq(ci, &ineq);
734
25.0k
  if (!ci->index[h])
735
10.8k
    return isl_bool_false;
736
14.1k
  
return 14.1k
isl_int_ge14.1k
(ineq[0], (*ci->index[h])[0]);
737
25.0k
}
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.78M
{
747
1.78M
  unsigned int size;
748
1.78M
  int *index;
749
1.78M
  int *elim_for;
750
1.78M
  int k, l, h;
751
1.78M
  int bits;
752
1.78M
  struct isl_blk eq;
753
1.78M
  unsigned total_var;
754
1.78M
  unsigned total;
755
1.78M
  struct isl_ctx *ctx;
756
1.78M
757
1.78M
  bmap = isl_basic_map_order_divs(bmap);
758
1.78M
  if (
!bmap || 1.78M
bmap->n_div <= 11.78M
)
759
1.72M
    return bmap;
760
1.78M
761
58.0k
  total_var = isl_space_dim(bmap->dim, isl_dim_all);
762
58.0k
  total = total_var + bmap->n_div;
763
58.0k
764
58.0k
  ctx = bmap->ctx;
765
186k
  for (k = bmap->n_div - 1; 
k >= 0186k
;
--k128k
)
766
144k
    
if (144k
!144k
isl_int_is_zero144k
(bmap->div[k][0]))
767
16.0k
      break;
768
58.0k
  if (k <= 0)
769
42.5k
    return bmap;
770
58.0k
771
15.4k
  size = round_up(4 * bmap->n_div / 3 - 1);
772
15.4k
  if (size == 0)
773
0
    return bmap;
774
15.4k
  
elim_for = 15.4k
isl_calloc_array15.4k
(ctx, int, bmap->n_div);
775
15.4k
  bits = ffs(size) - 1;
776
15.4k
  index = isl_calloc_array(ctx, int, size);
777
15.4k
  if (
!elim_for || 15.4k
!index15.4k
)
778
0
    goto out;
779
15.4k
  eq = isl_blk_alloc(ctx, 1+total);
780
15.4k
  if (isl_blk_is_error(eq))
781
0
    goto out;
782
15.4k
783
15.4k
  isl_seq_clr(eq.data, 1+total);
784
15.4k
  index[isl_seq_get_hash_bits(bmap->div[k], 2+total, bits)] = k + 1;
785
43.2k
  for (--k; 
k >= 043.2k
;
--k27.8k
)
{27.8k
786
27.8k
    uint32_t hash;
787
27.8k
788
27.8k
    if (isl_int_is_zero(bmap->div[k][0]))
789
6.79k
      continue;
790
27.8k
791
21.0k
    hash = isl_seq_get_hash_bits(bmap->div[k], 2+total, bits);
792
27.3k
    for (h = hash; 
index[h]27.3k
;
h = (h+1) % size6.26k
)
793
10.0k
      
if (10.0k
isl_seq_eq(bmap->div[k],10.0k
794
10.0k
               bmap->div[index[h]-1], 2+total))
795
3.78k
        break;
796
21.0k
    if (
index[h]21.0k
)
{3.78k
797
3.78k
      *progress = 1;
798
3.78k
      l = index[h] - 1;
799
3.78k
      elim_for[l] = k + 1;
800
3.78k
    }
801
21.0k
    index[h] = k+1;
802
21.0k
  }
803
60.0k
  for (l = bmap->n_div - 1; 
l >= 060.0k
;
--l44.5k
)
{44.5k
804
44.5k
    if (!elim_for[l])
805
40.8k
      continue;
806
3.78k
    k = elim_for[l] - 1;
807
3.78k
    isl_int_set_si(eq.data[1+total_var+k], -1);
808
3.78k
    isl_int_set_si(eq.data[1+total_var+l], 1);
809
3.78k
    bmap = eliminate_div(bmap, eq.data, l, 1);
810
3.78k
    if (!bmap)
811
0
      break;
812
3.78k
    
isl_int_set_si3.78k
(eq.data[1+total_var+k], 0);3.78k
813
3.78k
    isl_int_set_si(eq.data[1+total_var+l], 0);
814
3.78k
  }
815
15.4k
816
15.4k
  isl_blk_free(ctx, eq);
817
15.4k
out:
818
15.4k
  free(index);
819
15.4k
  free(elim_for);
820
15.4k
  return bmap;
821
15.4k
}
822
823
static int n_pure_div_eq(struct isl_basic_map *bmap)
824
18.3k
{
825
18.3k
  int i, j;
826
18.3k
  unsigned total;
827
18.3k
828
18.3k
  total = isl_space_dim(bmap->dim, isl_dim_all);
829
31.3k
  for (i = 0, j = bmap->n_div-1; 
i < bmap->n_eq31.3k
;
++i13.0k
)
{27.2k
830
44.7k
    while (
j >= 0 && 44.7k
isl_int_is_zero32.7k
(bmap->eq[i][1 + total + j]))
831
17.4k
      --j;
832
27.2k
    if (j < 0)
833
12.0k
      break;
834
15.2k
    
if (15.2k
isl_seq_first_non_zero(bmap->eq[i] + 1 + total, j) != -115.2k
)
835
2.16k
      return 0;
836
15.2k
  }
837
16.1k
  return i;
838
18.3k
}
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.78M
{
891
1.78M
  int i, j, k;
892
1.78M
  int total;
893
1.78M
  int div_eq;
894
1.78M
  struct isl_mat *B;
895
1.78M
  struct isl_vec *d;
896
1.78M
  struct isl_mat *T = NULL;
897
1.78M
  struct isl_mat *C = NULL;
898
1.78M
  struct isl_mat *C2 = NULL;
899
1.78M
  isl_int v;
900
1.78M
  int *pos = NULL;
901
1.78M
  int dropped, needed;
902
1.78M
903
1.78M
  if (!bmap)
904
0
    return NULL;
905
1.78M
906
1.78M
  
if (1.78M
bmap->n_div == 01.78M
)
907
1.73M
    return bmap;
908
1.78M
909
53.3k
  
if (53.3k
bmap->n_eq == 053.3k
)
910
18.4k
    return bmap;
911
53.3k
912
34.8k
  
if (34.8k
ISL_F_ISSET34.8k
(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS))
913
16.4k
    return bmap;
914
34.8k
915
18.3k
  total = isl_space_dim(bmap->dim, isl_dim_all);
916
18.3k
  div_eq = n_pure_div_eq(bmap);
917
18.3k
  if (div_eq == 0)
918
8.26k
    return bmap;
919
18.3k
920
10.0k
  
if (10.0k
div_eq < bmap->n_eq10.0k
)
{5.90k
921
5.90k
    B = isl_mat_sub_alloc6(bmap->ctx, bmap->eq, div_eq,
922
5.90k
          bmap->n_eq - div_eq, 0, 1 + total);
923
5.90k
    C = isl_mat_variable_compression(B, &C2);
924
5.90k
    if (
!C || 5.90k
!C25.90k
)
925
0
      goto error;
926
5.90k
    
if (5.90k
C->n_col == 05.90k
)
{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
5.90k
  }
933
10.0k
934
10.0k
  d = isl_vec_alloc(bmap->ctx, div_eq);
935
10.0k
  if (!d)
936
0
    goto error;
937
22.9k
  
for (i = 0, j = bmap->n_div-1; 10.0k
i < div_eq22.9k
;
++i12.9k
)
{12.9k
938
16.5k
    while (
j >= 0 && 16.5k
isl_int_is_zero16.5k
(bmap->eq[i][1 + total + j]))
939
3.59k
      --j;
940
12.9k
    isl_int_set(d->block.data[i], bmap->eq[i][1 + total + j]);
941
12.9k
  }
942
10.0k
  B = isl_mat_sub_alloc6(bmap->ctx, bmap->eq, 0, div_eq, 0, 1 + total);
943
10.0k
944
10.0k
  if (
C10.0k
)
{5.89k
945
5.89k
    B = isl_mat_product(B, C);
946
5.89k
    C = NULL;
947
5.89k
  }
948
10.0k
949
10.0k
  T = isl_mat_parameter_compression(B, d);
950
10.0k
  if (!T)
951
0
    goto error;
952
10.0k
  
if (10.0k
T->n_col == 010.0k
)
{776
953
776
    bmap = isl_basic_map_set_to_empty(bmap);
954
776
    isl_mat_free(C2);
955
776
    isl_mat_free(T);
956
776
    goto done;
957
776
  }
958
9.29k
  
isl_int_init9.29k
(v);9.29k
959
35.3k
  for (i = 0; 
i < T->n_row - 135.3k
;
++i26.0k
)
{26.0k
960
26.0k
    isl_int_fdiv_q(v, T->row[1 + i][0], T->row[1 + i][1 + i]);
961
26.0k
    if (isl_int_is_zero(v))
962
23.5k
      continue;
963
2.42k
    isl_mat_col_submul(T, 0, v, 1 + i);
964
2.42k
  }
965
9.29k
  isl_int_clear(v);
966
9.29k
  pos = isl_alloc_array(bmap->ctx, int, T->n_row);
967
9.29k
  if (!pos)
968
0
    goto error;
969
9.29k
  /* We have to be careful because dropping equalities may reorder them */
970
9.29k
  dropped = 0;
971
22.4k
  for (j = bmap->n_div - 1; 
j >= 022.4k
;
--j13.1k
)
{13.1k
972
17.8k
    for (i = 0; 
i < bmap->n_eq17.8k
;
++i4.65k
)
973
16.1k
      
if (16.1k
!16.1k
isl_int_is_zero16.1k
(bmap->eq[i][1 + total + j]))
974
11.5k
        break;
975
13.1k
    if (
i < bmap->n_eq13.1k
)
{11.5k
976
11.5k
      bmap = isl_basic_map_drop_div(bmap, j);
977
11.5k
      isl_basic_map_drop_equality(bmap, i);
978
11.5k
      ++dropped;
979
11.5k
    }
980
13.1k
  }
981
9.29k
  pos[0] = 0;
982
9.29k
  needed = 0;
983
35.3k
  for (i = 1; 
i < T->n_row35.3k
;
++i26.0k
)
{26.0k
984
26.0k
    if (isl_int_is_one(T->row[i][i]))
985
16.0k
      pos[i] = i;
986
26.0k
    else
987
9.99k
      needed++;
988
26.0k
  }
989
9.29k
  if (
needed > dropped9.29k
)
{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
35.3k
  
for (i = 1; 9.29k
i < T->n_row35.3k
;
++i26.0k
)
{26.0k
996
26.0k
    if (isl_int_is_one(T->row[i][i]))
997
16.0k
      continue;
998
9.99k
    k = isl_basic_map_alloc_div(bmap);
999
9.99k
    pos[i] = 1 + total + k;
1000
9.99k
    isl_seq_clr(bmap->div[k] + 1, 1 + total + bmap->n_div);
1001
9.99k
    isl_int_set(bmap->div[k][0], T->row[i][i]);
1002
9.99k
    if (C2)
1003
5.47k
      isl_seq_cpy(bmap->div[k] + 1, C2->row[i], 1 + total);
1004
9.99k
    else
1005
4.52k
      isl_int_set_si(bmap->div[k][1 + i], 1);
1006
35.1k
    for (j = 0; 
j < i35.1k
;
++j25.1k
)
{25.1k
1007
25.1k
      if (isl_int_is_zero(T->row[i][j]))
1008
18.5k
        continue;
1009
6.64k
      
if (6.64k
pos[j] < T->n_row && 6.64k
C26.56k
)
1010
3.60k
        isl_seq_submul(bmap->div[k] + 1, T->row[i][j],
1011
3.60k
            C2->row[pos[j]], 1 + total);
1012
6.64k
      else
1013
3.03k
        isl_int_neg(bmap->div[k][1 + pos[j]],
1014
6.64k
                T->row[i][j]);
1015
6.64k
    }
1016
9.99k
    j = isl_basic_map_alloc_equality(bmap);
1017
9.99k
    isl_seq_neg(bmap->eq[j], bmap->div[k]+1, 1+total+bmap->n_div);
1018
9.99k
    isl_int_set(bmap->eq[j][pos[i]], bmap->div[k][0]);
1019
9.99k
  }
1020
9.29k
  free(pos);
1021
9.29k
  isl_mat_free(C2);
1022
9.29k
  isl_mat_free(T);
1023
9.29k
1024
9.29k
  if (progress)
1025
9.29k
    *progress = 1;
1026
10.0k
done:
1027
10.0k
  ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1028
10.0k
1029
10.0k
  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
9.29k
}
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
921
{
1041
921
  unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1042
921
1043
921
  isl_seq_neg(bmap->div[div] + 1, bmap->ineq[ineq], total + bmap->n_div);
1044
921
  isl_int_set(bmap->div[div][0], bmap->ineq[ineq][total + div]);
1045
921
  isl_int_add(bmap->div[div][1], bmap->div[div][1], bmap->div[div][0]);
1046
921
  isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
1047
921
  isl_int_set_si(bmap->div[div][1 + total + div], 0);
1048
921
1049
921
  return bmap;
1050
921
}
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
1.66k
{
1061
1.66k
  int j;
1062
1.66k
  unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1063
1.66k
1064
1.66k
  /* Not defined in terms of unknown divs */
1065
6.41k
  for (j = 0; 
j < bmap->n_div6.41k
;
++j4.75k
)
{5.37k
1066
5.37k
    if (div == j)
1067
1.34k
      continue;
1068
4.03k
    
if (4.03k
isl_int_is_zero4.03k
(bmap->ineq[ineq][total + j]))
1069
3.33k
      continue;
1070
695
    
if (695
isl_int_is_zero695
(bmap->div[j][0]))
1071
626
      return isl_bool_false;
1072
695
  }
1073
1.66k
1074
1.66k
  /* No other div defined in terms of this one => avoid loops */
1075
4.51k
  
for (j = 0; 1.03k
j < bmap->n_div4.51k
;
++j3.48k
)
{3.59k
1076
3.59k
    if (div == j)
1077
1.03k
      continue;
1078
2.56k
    
if (2.56k
isl_int_is_zero2.56k
(bmap->div[j][0]))
1079
715
      continue;
1080
1.84k
    
if (1.84k
!1.84k
isl_int_is_zero1.84k
(bmap->div[j][1 + total + div]))
1081
113
      return isl_bool_false;
1082
1.84k
  }
1083
1.03k
1084
921
  return isl_bool_true;
1085
1.03k
}
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
55.2k
{
1098
55.2k
  unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1099
55.2k
  int last_div;
1100
55.2k
  int last_ineq;
1101
55.2k
1102
55.2k
  if (isl_int_is_zero(bmap->div[div][0]))
1103
1.52k
    return isl_bool_true;
1104
55.2k
1105
53.7k
  
if (53.7k
isl_seq_last_non_zero(bmap->ineq[ineq] + total + div + 1,53.7k
1106
53.7k
          bmap->n_div - (div + 1)) >= 0)
1107
2.08k
    return isl_bool_false;
1108
53.7k
1109
51.6k
  last_ineq = isl_seq_last_non_zero(bmap->ineq[ineq], total + div);
1110
51.6k
  last_div = isl_seq_last_non_zero(bmap->div[div] + 1,
1111
51.6k
           total + bmap->n_div);
1112
51.6k
1113
51.6k
  return last_ineq < last_div;
1114
53.7k
}
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.13M
{
1132
3.13M
  int i;
1133
3.13M
  unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1134
3.13M
1135
3.27M
  for (i = 0; 
i < bmap->n_div3.27M
;
++i138k
)
{193k
1136
193k
    isl_bool set_div;
1137
193k
1138
193k
    if (isl_int_is_zero(bmap->ineq[k][total + i]))
1139
129k
      continue;
1140
63.5k
    
if (63.5k
isl_int_abs_ge63.5k
(sum, bmap->ineq[k][total + i]))
1141
8.27k
      continue;
1142
55.2k
    set_div = better_div_constraint(bmap, i, k);
1143
55.2k
    if (
set_div >= 0 && 55.2k
set_div55.2k
)
1144
1.65k
      set_div = ok_to_set_div_from_bound(bmap, i, k);
1145
55.2k
    if (set_div < 0)
1146
0
      return isl_basic_map_free(bmap);
1147
55.2k
    
if (55.2k
!set_div55.2k
)
1148
54.3k
      break;
1149
918
    
if (918
isl_int_is_pos918
(bmap->ineq[k][total + i]))
1150
323
      bmap = set_div_from_lower_bound(bmap, i, k);
1151
918
    else
1152
595
      bmap = set_div_from_lower_bound(bmap, i, l);
1153
918
    if (progress)
1154
918
      *progress = 1;
1155
918
    break;
1156
55.2k
  }
1157
3.13M
  return bmap;
1158
3.13M
}
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.80M
{
1163
1.80M
  struct isl_constraint_index ci;
1164
1.80M
  int k, l, h;
1165
1.80M
  unsigned total = isl_basic_map_total_dim(bmap);
1166
1.80M
  isl_int sum;
1167
1.80M
1168
1.80M
  if (
!bmap || 1.80M
bmap->n_ineq <= 11.80M
)
1169
592k
    return bmap;
1170
1.80M
1171
1.21M
  
if (1.21M
create_constraint_index(&ci, bmap) < 01.21M
)
1172
0
    return bmap;
1173
1.21M
1174
1.21M
  h = isl_seq_get_hash_bits(bmap->ineq[0] + 1, total, ci.bits);
1175
1.21M
  ci.index[h] = &bmap->ineq[0];
1176
6.68M
  for (k = 1; 
k < bmap->n_ineq6.68M
;
++k5.46M
)
{5.46M
1177
5.46M
    h = hash_index(&ci, bmap, k);
1178
5.46M
    if (
!ci.index[h]5.46M
)
{4.98M
1179
4.98M
      ci.index[h] = &bmap->ineq[k];
1180
4.98M
      continue;
1181
4.98M
    }
1182
478k
    
if (478k
progress478k
)
1183
472k
      *progress = 1;
1184
478k
    l = ci.index[h] - &bmap->ineq[0];
1185
478k
    if (isl_int_lt(bmap->ineq[k][0], bmap->ineq[l][0]))
1186
43.8k
      swap_inequality(bmap, k, l);
1187
478k
    isl_basic_map_drop_inequality(bmap, k);
1188
478k
    --k;
1189
478k
  }
1190
1.21M
  isl_int_init(sum);
1191
5.65M
  for (k = 0; 
k < bmap->n_ineq-15.65M
;
++k4.44M
)
{4.54M
1192
4.54M
    isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
1193
4.54M
    h = hash_index(&ci, bmap, k);
1194
4.54M
    isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
1195
4.54M
    if (!ci.index[h])
1196
1.28M
      continue;
1197
3.26M
    l = ci.index[h] - &bmap->ineq[0];
1198
3.26M
    isl_int_add(sum, bmap->ineq[k][0], bmap->ineq[l][0]);
1199
3.26M
    if (
isl_int_is_pos3.26M
(sum))
{3.15M
1200
3.15M
      if (detect_divs)
1201
3.13M
        bmap = check_for_div_constraints(bmap, k, l,
1202
3.13M
                 sum, progress);
1203
3.15M
      continue;
1204
3.15M
    }
1205
106k
    
if (106k
isl_int_is_zero106k
(sum))
{10.9k
1206
10.9k
      /* We need to break out of the loop after these
1207
10.9k
       * changes since the contents of the hash
1208
10.9k
       * will no longer be valid.
1209
10.9k
       * Plus, we probably we want to regauss first.
1210
10.9k
       */
1211
10.9k
      if (progress)
1212
10.8k
        *progress = 1;
1213
10.9k
      isl_basic_map_drop_inequality(bmap, l);
1214
10.9k
      isl_basic_map_inequality_to_equality(bmap, k);
1215
10.9k
    } else
1216
95.9k
      bmap = isl_basic_map_set_to_empty(bmap);
1217
106k
    break;
1218
3.26M
  }
1219
1.21M
  isl_int_clear(sum);
1220
1.21M
1221
1.21M
  constraint_index_free(&ci);
1222
1.21M
  return bmap;
1223
1.21M
}
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
195
{
1233
195
  int duplicate;
1234
195
1235
311
  do {
1236
311
    duplicate = 0;
1237
311
    bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1238
311
                &duplicate, 0);
1239
311
    if (
progress && 311
duplicate193
)
1240
56
      *progress = 1;
1241
311
  } while (duplicate);
1242
195
1243
195
  return bmap;
1244
195
}
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.78M
{
1287
1.78M
  int i, j;
1288
1.78M
  isl_ctx *ctx;
1289
1.78M
  unsigned total;
1290
1.78M
1291
1.78M
  if (!bmap)
1292
0
    return NULL;
1293
1.78M
1294
1.78M
  ctx = isl_basic_map_get_ctx(bmap);
1295
1.78M
  total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1296
1.78M
1297
2.05M
  for (i = 0; 
i < bmap->n_div2.05M
;
++i270k
)
{270k
1298
270k
    if (isl_int_is_zero(bmap->div[i][0]))
1299
202k
      continue;
1300
68.0k
    
if (68.0k
isl_int_is_one68.0k
(bmap->div[i][0]))
1301
210
      continue;
1302
519k
    
for (j = 0; 67.8k
j < bmap->n_ineq519k
;
++j451k
)
{451k
1303
451k
      int s;
1304
451k
1305
451k
      if (
!451k
isl_int_is_one451k
(bmap->ineq[j][total + i]) &&
1306
448k
          
!448k
isl_int_is_negone448k
(bmap->ineq[j][total + i]))
1307
445k
        continue;
1308
451k
1309
6.49k
      *progress = 1;
1310
6.49k
1311
6.49k
      s = isl_int_sgn(bmap->ineq[j][total + i]);
1312
6.49k
      isl_int_set_si(bmap->ineq[j][total + i], 0);
1313
6.49k
      if (s < 0)
1314
3.22k
        isl_seq_combine(bmap->ineq[j],
1315
3.22k
          ctx->negone, bmap->div[i] + 1,
1316
3.22k
          bmap->div[i][0], bmap->ineq[j],
1317
3.22k
          total + bmap->n_div);
1318
6.49k
      else
1319
3.27k
        isl_seq_combine(bmap->ineq[j],
1320
3.27k
          ctx->one, bmap->div[i] + 1,
1321
3.27k
          bmap->div[i][0], bmap->ineq[j],
1322
3.27k
          total + bmap->n_div);
1323
6.49k
      if (
s < 06.49k
)
{3.22k
1324
3.22k
        isl_int_add(bmap->ineq[j][0],
1325
3.22k
          bmap->ineq[j][0], bmap->div[i][0]);
1326
3.22k
        isl_int_sub_ui(bmap->ineq[j][0],
1327
3.22k
          bmap->ineq[j][0], 1);
1328
3.22k
      }
1329
6.49k
1330
6.49k
      bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1331
6.49k
      if (isl_basic_map_add_div_constraint(bmap, i, s) < 0)
1332
0
        return isl_basic_map_free(bmap);
1333
6.49k
    }
1334
67.8k
  }
1335
1.78M
1336
1.78M
  return bmap;
1337
1.78M
}
1338
1339
__isl_give isl_basic_map *isl_basic_map_simplify(__isl_take isl_basic_map *bmap)
1340
1.49M
{
1341
1.49M
  int progress = 1;
1342
1.49M
  if (!bmap)
1343
0
    return NULL;
1344
3.28M
  
while (1.49M
progress3.28M
)
{1.84M
1345
1.84M
    isl_bool empty;
1346
1.84M
1347
1.84M
    progress = 0;
1348
1.84M
    empty = isl_basic_map_plain_is_empty(bmap);
1349
1.84M
    if (empty < 0)
1350
0
      return isl_basic_map_free(bmap);
1351
1.84M
    
if (1.84M
empty1.84M
)
1352
59.9k
      break;
1353
1.78M
    bmap = isl_basic_map_normalize_constraints(bmap);
1354
1.78M
    bmap = reduce_div_coefficients(bmap);
1355
1.78M
    bmap = normalize_div_expressions(bmap);
1356
1.78M
    bmap = remove_duplicate_divs(bmap, &progress);
1357
1.78M
    bmap = eliminate_unit_divs(bmap, &progress);
1358
1.78M
    bmap = eliminate_divs_eq(bmap, &progress);
1359
1.78M
    bmap = eliminate_divs_ineq(bmap, &progress);
1360
1.78M
    bmap = isl_basic_map_gauss(bmap, &progress);
1361
1.78M
    /* requires equalities in normal form */
1362
1.78M
    bmap = normalize_divs(bmap, &progress);
1363
1.78M
    bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1364
1.78M
                &progress, 1);
1365
1.78M
    if (
bmap && 1.78M
progress1.78M
)
1366
1.78M
      ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1367
1.78M
  }
1368
1.49M
  return bmap;
1369
1.49M
}
1370
1371
struct isl_basic_set *isl_basic_set_simplify(struct isl_basic_set *bset)
1372
539k
{
1373
539k
  return bset_from_bmap(isl_basic_map_simplify(bset_to_bmap(bset)));
1374
539k
}
1375
1376
1377
isl_bool isl_basic_map_is_div_constraint(__isl_keep isl_basic_map *bmap,
1378
  isl_int *constraint, unsigned div)
1379
36.5k
{
1380
36.5k
  unsigned pos;
1381
36.5k
1382
36.5k
  if (!bmap)
1383
0
    return isl_bool_error;
1384
36.5k
1385
36.5k
  pos = 1 + isl_space_dim(bmap->dim, isl_dim_all) + div;
1386
36.5k
1387
36.5k
  if (
isl_int_eq36.5k
(constraint[pos], bmap->div[div][0]))
{16.0k
1388
16.0k
    int neg;
1389
16.0k
    isl_int_sub(bmap->div[div][1],
1390
16.0k
        bmap->div[div][1], bmap->div[div][0]);
1391
16.0k
    isl_int_add_ui(bmap->div[div][1], bmap->div[div][1], 1);
1392
16.0k
    neg = isl_seq_is_neg(constraint, bmap->div[div]+1, pos);
1393
16.0k
    isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
1394
16.0k
    isl_int_add(bmap->div[div][1],
1395
16.0k
        bmap->div[div][1], bmap->div[div][0]);
1396
16.0k
    if (!neg)
1397
3.64k
      return isl_bool_false;
1398
12.4k
    
if (12.4k
isl_seq_first_non_zero(constraint+pos+1,12.4k
1399
12.4k
              bmap->n_div-div-1) != -1)
1400
0
      return isl_bool_false;
1401
20.4k
  } else 
if (20.4k
isl_int_abs_eq20.4k
(constraint[pos], bmap->div[div][0]))
{17.1k
1402
17.1k
    if (!isl_seq_eq(constraint, bmap->div[div]+1, pos))
1403
6.61k
      return isl_bool_false;
1404
10.5k
    
if (10.5k
isl_seq_first_non_zero(constraint+pos+1,10.5k
1405
10.5k
              bmap->n_div-div-1) != -1)
1406
1
      return isl_bool_false;
1407
10.5k
  } else
1408
3.34k
    return isl_bool_false;
1409
36.5k
1410
22.9k
  return isl_bool_true;
1411
36.5k
}
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
72.3k
{
1430
72.3k
  int i;
1431
72.3k
  unsigned pos = 1 + isl_space_dim(bmap->dim, isl_dim_all) + div;
1432
72.3k
1433
100k
  for (i = 0; 
i < bmap->n_eq100k
;
++i28.0k
)
1434
63.3k
    
if (63.3k
!63.3k
isl_int_is_zero63.3k
(bmap->eq[i][pos]))
1435
35.2k
      return isl_bool_false;
1436
72.3k
1437
126k
  
for (i = 0; 37.0k
i < bmap->n_ineq126k
;
++i89.8k
)
{103k
1438
103k
    isl_bool red;
1439
103k
1440
103k
    if (isl_int_is_zero(bmap->ineq[i][pos]))
1441
66.9k
      continue;
1442
36.4k
    red = isl_basic_map_is_div_constraint(bmap, bmap->ineq[i], div);
1443
36.4k
    if (
red < 0 || 36.4k
!red36.4k
)
1444
13.5k
      return red;
1445
36.4k
  }
1446
37.0k
1447
50.4k
  
for (i = 0; 23.4k
i < bmap->n_div50.4k
;
++i26.9k
)
{26.9k
1448
26.9k
    if (isl_int_is_zero(bmap->div[i][0]))
1449
123
      continue;
1450
26.8k
    
if (26.8k
!26.8k
isl_int_is_zero26.8k
(bmap->div[i][1+pos]))
1451
1
      return isl_bool_false;
1452
26.8k
  }
1453
23.4k
1454
23.4k
  return isl_bool_true;
1455
23.4k
}
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.40M
{
1466
3.40M
  int i;
1467
3.40M
1468
3.40M
  if (!bmap)
1469
0
    return NULL;
1470
3.40M
1471
3.48M
  
for (i = bmap->n_div-1; 3.40M
i >= 03.48M
;
--i72.3k
)
{72.3k
1472
72.3k
    isl_bool redundant;
1473
72.3k
1474
72.3k
    redundant = div_is_redundant(bmap, i);
1475
72.3k
    if (redundant < 0)
1476
0
      return isl_basic_map_free(bmap);
1477
72.3k
    
if (72.3k
!redundant72.3k
)
1478
48.8k
      continue;
1479
23.4k
    bmap = isl_basic_map_drop_div(bmap, i);
1480
23.4k
  }
1481
3.40M
  return bmap;
1482
3.40M
}
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.41M
{
1491
3.41M
  if (!bmap)
1492
0
    return NULL;
1493
3.41M
  
ISL_F_SET3.41M
(bmap, ISL_BASIC_SET_FINAL);3.41M
1494
3.41M
  return bmap;
1495
3.41M
}
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.40M
{
1501
3.40M
  bmap = remove_redundant_divs(bmap);
1502
3.40M
  bmap = isl_basic_map_mark_final(bmap);
1503
3.40M
  return bmap;
1504
3.40M
}
1505
1506
struct isl_basic_set *isl_basic_set_finalize(struct isl_basic_set *bset)
1507
631k
{
1508
631k
  return bset_from_bmap(isl_basic_map_finalize(bset_to_bmap(bset)));
1509
631k
}
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
65.1k
{
1518
65.1k
  int i;
1519
65.1k
1520
65.1k
  if (!bmap)
1521
0
    return NULL;
1522
65.1k
1523
135k
  
for (i = 0; 65.1k
i < bmap->n_div135k
;
++i70.7k
)
{70.7k
1524
70.7k
    if (isl_int_is_zero(bmap->div[i][0]))
1525
69.3k
      continue;
1526
1.47k
    
if (1.47k
isl_int_is_zero1.47k
(bmap->div[i][1+1+pos]))
1527
1.40k
      continue;
1528
65
    bmap = isl_basic_map_mark_div_unknown(bmap, i);
1529
65
    if (!bmap)
1530
0
      return NULL;
1531
65
  }
1532
65.1k
  return bmap;
1533
65.1k
}
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
65.3k
{
1541
65.3k
  int d;
1542
65.3k
  int i, j, k;
1543
65.3k
  unsigned total;
1544
65.3k
  int need_gauss = 0;
1545
65.3k
1546
65.3k
  if (n == 0)
1547
1.61k
    return bmap;
1548
63.7k
  
if (63.7k
!bmap63.7k
)
1549
0
    return NULL;
1550
63.7k
  total = isl_basic_map_total_dim(bmap);
1551
63.7k
1552
63.7k
  bmap = isl_basic_map_cow(bmap);
1553
128k
  for (d = pos + n - 1; 
d >= 0 && 128k
d >= pos115k
;
--d65.1k
)
1554
65.1k
    bmap = remove_dependent_vars(bmap, d);
1555
63.7k
  if (!bmap)
1556
0
    return NULL;
1557
63.7k
1558
63.7k
  for (d = pos + n - 1;
1559
112k
       
d >= 0 && 112k
d >= total - bmap->n_div103k
&&
d >= pos61.1k
;
--d48.7k
)
1560
48.7k
    isl_seq_clr(bmap->div[d-(total-bmap->n_div)], 2+total);
1561
128k
  for (d = pos + n - 1; 
d >= 0 && 128k
d >= pos115k
;
--d65.0k
)
{65.1k
1562
65.1k
    int n_lower, n_upper;
1563
65.1k
    if (!bmap)
1564
0
      return NULL;
1565
83.0k
    
for (i = 0; 65.1k
i < bmap->n_eq83.0k
;
++i17.9k
)
{20.2k
1566
20.2k
      if (isl_int_is_zero(bmap->eq[i][1+d]))
1567
17.9k
        continue;
1568
2.29k
      eliminate_var_using_equality(bmap, d, bmap->eq[i], 0, NULL);
1569
2.29k
      isl_basic_map_drop_equality(bmap, i);
1570
2.29k
      need_gauss = 1;
1571
2.29k
      break;
1572
20.2k
    }
1573
65.1k
    if (i < bmap->n_eq)
1574
853
      continue;
1575
64.2k
    n_lower = 0;
1576
64.2k
    n_upper = 0;
1577
208k
    for (i = 0; 
i < bmap->n_ineq208k
;
++i144k
)
{144k
1578
144k
      if (isl_int_is_pos(bmap->ineq[i][1+d]))
1579
24.5k
        n_lower++;
1580
120k
      else 
if (120k
isl_int_is_neg120k
(bmap->ineq[i][1+d]))
1581
23.2k
        n_upper++;
1582
144k
    }
1583
64.2k
    bmap = isl_basic_map_extend_constraints(bmap,
1584
64.2k
        0, n_lower * n_upper);
1585
64.2k
    if (!bmap)
1586
0
      goto error;
1587
166k
    
for (i = bmap->n_ineq - 1; 64.2k
i >= 0166k
;
--i102k
)
{102k
1588
102k
      int last;
1589
102k
      if (isl_int_is_zero(bmap->ineq[i][1+d]))
1590
54.1k
        continue;
1591
47.8k
      last = -1;
1592
188k
      for (j = 0; 
j < i188k
;
++j140k
)
{140k
1593
140k
        if (isl_int_is_zero(bmap->ineq[j][1+d]))
1594
100k
          continue;
1595
40.6k
        last = j;
1596
40.6k
        if (
isl_int_sgn40.6k
(bmap->ineq[i][1+d]) ==40.6k
1597
40.6k
            isl_int_sgn(bmap->ineq[j][1+d]))
1598
9.50k
          continue;
1599
31.1k
        k = isl_basic_map_alloc_inequality(bmap);
1600
31.1k
        if (k < 0)
1601
0
          goto error;
1602
31.1k
        isl_seq_cpy(bmap->ineq[k], bmap->ineq[i],
1603
31.1k
            1+total);
1604
31.1k
        isl_seq_elim(bmap->ineq[k], bmap->ineq[j],
1605
31.1k
            1+d, 1+total, NULL);
1606
31.1k
      }
1607
47.8k
      isl_basic_map_drop_inequality(bmap, i);
1608
47.8k
      i = last + 1;
1609
47.8k
    }
1610
64.2k
    
if (64.2k
n_lower > 0 && 64.2k
n_upper > 020.6k
)
{19.7k
1611
19.7k
      bmap = isl_basic_map_normalize_constraints(bmap);
1612
19.7k
      bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1613
19.7k
                    NULL, 0);
1614
19.7k
      bmap = isl_basic_map_gauss(bmap, NULL);
1615
19.7k
      bmap = isl_basic_map_remove_redundancies(bmap);
1616
19.7k
      need_gauss = 0;
1617
19.7k
      if (!bmap)
1618
0
        goto error;
1619
19.7k
      
if (19.7k
ISL_F_ISSET19.7k
(bmap, ISL_BASIC_MAP_EMPTY))
1620
69
        break;
1621
19.7k
    }
1622
64.2k
  }
1623
63.7k
  
ISL_F_CLR63.7k
(bmap, ISL_BASIC_MAP_NORMALIZED);63.7k
1624
63.7k
  if (need_gauss)
1625
1.09k
    bmap = isl_basic_map_gauss(bmap, NULL);
1626
63.7k
  return bmap;
1627
0
error:
1628
0
  isl_basic_map_free(bmap);
1629
0
  return NULL;
1630
63.7k
}
1631
1632
struct isl_basic_set *isl_basic_set_eliminate_vars(
1633
  struct isl_basic_set *bset, unsigned pos, unsigned n)
1634
14.4k
{
1635
14.4k
  return bset_from_bmap(isl_basic_map_eliminate_vars(bset_to_bmap(bset),
1636
14.4k
                pos, n));
1637
14.4k
}
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.41k
{
1648
9.41k
  isl_space *space;
1649
9.41k
1650
9.41k
  if (!bmap)
1651
0
    return NULL;
1652
9.41k
  
if (9.41k
n == 09.41k
)
1653
0
    return bmap;
1654
9.41k
1655
9.41k
  
if (9.41k
first + n > isl_basic_map_dim(bmap, type) || 9.41k
first + n < first9.41k
)
1656
0
    isl_die(bmap->ctx, isl_error_invalid,
1657
9.41k
      "index out of bounds", goto error);
1658
9.41k
1659
9.41k
  
if (9.41k
ISL_F_ISSET9.41k
(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.41k
1665
9.41k
  space = isl_basic_map_get_space(bmap);
1666
9.41k
  bmap = isl_basic_map_project_out(bmap, type, first, n);
1667
9.41k
  bmap = isl_basic_map_insert_dims(bmap, type, first, n);
1668
9.41k
  bmap = isl_basic_map_reset_space(bmap, space);
1669
9.41k
  return bmap;
1670
0
error:
1671
0
  isl_basic_map_free(bmap);
1672
0
  return NULL;
1673
9.41k
}
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.10k
{
1679
3.10k
  return isl_basic_map_eliminate(bset, type, first, n);
1680
3.10k
}
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
5.45k
{
1696
5.45k
  isl_bool known;
1697
5.45k
  int i, n_div, o_div;
1698
5.45k
1699
5.45k
  known = isl_basic_map_divs_known(bmap);
1700
5.45k
  if (known < 0)
1701
0
    return isl_basic_map_free(bmap);
1702
5.45k
  
if (5.45k
known5.45k
)
1703
5.45k
    return bmap;
1704
5.45k
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
933
{
1735
933
  int i;
1736
933
  isl_bool known;
1737
933
1738
933
  known = isl_map_divs_known(map);
1739
933
  if (known < 0)
1740
0
    return isl_map_free(map);
1741
933
  
if (933
known933
)
1742
933
    return map;
1743
933
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
15.3k
{
1767
15.3k
  int d, i;
1768
15.3k
  unsigned total;
1769
15.3k
1770
15.3k
  total = isl_space_dim(bmap->dim, isl_dim_all);
1771
126k
  for (d = 0; 
d < total126k
;
++d110k
)
1772
110k
    elim[d] = -1;
1773
34.2k
  for (i = 0; 
i < bmap->n_eq34.2k
;
++i18.8k
)
{18.8k
1774
60.1k
    for (d = total - 1; 
d >= 060.1k
;
--d41.2k
)
{60.1k
1775
60.1k
      if (isl_int_is_zero(bmap->eq[i][1+d]))
1776
41.2k
        continue;
1777
18.8k
      elim[d] = i;
1778
18.8k
      break;
1779
60.1k
    }
1780
18.8k
  }
1781
15.3k
}
1782
1783
static void set_compute_elimination_index(__isl_keep isl_basic_set *bset,
1784
  int *elim)
1785
242
{
1786
242
  compute_elimination_index(bset_to_bmap(bset), elim);
1787
242
}
1788
1789
static int reduced_using_equalities(isl_int *dst, isl_int *src,
1790
  __isl_keep isl_basic_map *bmap, int *elim)
1791
124k
{
1792
124k
  int d;
1793
124k
  int copied = 0;
1794
124k
  unsigned total;
1795
124k
1796
124k
  total = isl_space_dim(bmap->dim, isl_dim_all);
1797
1.16M
  for (d = total - 1; 
d >= 01.16M
;
--d1.04M
)
{1.04M
1798
1.04M
    if (isl_int_is_zero(src[1+d]))
1799
905k
      continue;
1800
140k
    
if (140k
elim[d] == -1140k
)
1801
127k
      continue;
1802
12.4k
    
if (12.4k
!copied12.4k
)
{11.8k
1803
11.8k
      isl_seq_cpy(dst, src, 1 + total);
1804
11.8k
      copied = 1;
1805
11.8k
    }
1806
12.4k
    isl_seq_elim(dst, bmap->eq[elim[d]], 1 + d, 1 + total, NULL);
1807
12.4k
  }
1808
124k
  return copied;
1809
124k
}
1810
1811
static int set_reduced_using_equalities(isl_int *dst, isl_int *src,
1812
  __isl_keep isl_basic_set *bset, int *elim)
1813
525
{
1814
525
  return reduced_using_equalities(dst, src,
1815
525
          bset_to_bmap(bset), elim);
1816
525
}
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
6.49k
{
1821
6.49k
  int i;
1822
6.49k
  int *elim;
1823
6.49k
1824
6.49k
  if (
!bset || 6.49k
!context6.49k
)
1825
0
    goto error;
1826
6.49k
1827
6.49k
  
if (6.49k
context->n_eq == 06.49k
)
{6.24k
1828
6.24k
    isl_basic_set_free(context);
1829
6.24k
    return bset;
1830
6.24k
  }
1831
6.49k
1832
242
  bset = isl_basic_set_cow(bset);
1833
242
  if (!bset)
1834
0
    goto error;
1835
242
1836
242
  
elim = 242
isl_alloc_array242
(bset->ctx, int, isl_basic_set_n_dim(bset));
1837
242
  if (!elim)
1838
0
    goto error;
1839
242
  set_compute_elimination_index(context, elim);
1840
698
  for (i = 0; 
i < bset->n_eq698
;
++i456
)
1841
456
    set_reduced_using_equalities(bset->eq[i], bset->eq[i],
1842
456
              context, elim);
1843
311
  for (i = 0; 
i < bset->n_ineq311
;
++i69
)
1844
69
    set_reduced_using_equalities(bset->ineq[i], bset->ineq[i],
1845
69
              context, elim);
1846
242
  isl_basic_set_free(context);
1847
242
  free(elim);
1848
242
  bset = isl_basic_set_simplify(bset);
1849
242
  bset = isl_basic_set_finalize(bset);
1850
242
  return bset;
1851
0
error:
1852
0
  isl_basic_set_free(bset);
1853
0
  isl_basic_set_free(context);
1854
0
  return NULL;
1855
242
}
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
9.28k
{
1866
9.28k
  struct isl_constraint_index ci;
1867
9.28k
  int n_ineq;
1868
9.28k
  unsigned total;
1869
9.28k
  int k;
1870
9.28k
1871
9.28k
  if (
!ineq || 9.28k
!context9.28k
)
1872
0
    return isl_stat_error;
1873
9.28k
  
if (9.28k
context->n_ineq == 09.28k
)
1874
0
    return isl_stat_ok;
1875
9.28k
  
if (9.28k
setup_constraint_index(&ci, context) < 09.28k
)
1876
0
    return isl_stat_error;
1877
9.28k
1878
9.28k
  n_ineq = isl_mat_rows(ineq);
1879
9.28k
  total = isl_mat_cols(ineq) - 1;
1880
34.0k
  for (k = 0; 
k < n_ineq34.0k
;
++k24.7k
)
{24.7k
1881
24.7k
    int l;
1882
24.7k
    isl_bool redundant;
1883
24.7k
1884
24.7k
    l = isl_seq_first_non_zero(ineq->row[k] + 1, total);
1885
24.7k
    if (
l < 0 && 24.7k
isl_int_is_nonneg4
(ineq->row[k][0]))
{0
1886
0
      row[k] = -1;
1887
0
      continue;
1888
0
    }
1889
24.7k
    redundant = constraint_index_is_redundant(&ci, ineq->row[k]);
1890
24.7k
    if (redundant < 0)
1891
0
      goto error;
1892
24.7k
    
if (24.7k
!redundant24.7k
)
1893
16.7k
      continue;
1894
8.07k
    row[k] = -1;
1895
8.07k
  }
1896
9.28k
  constraint_index_free(&ci);
1897
9.28k
  return isl_stat_ok;
1898
0
error:
1899
0
  constraint_index_free(&ci);
1900
0
  return isl_stat_error;
1901
9.28k
}
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
403
{
1947
403
  isl_basic_set *bset, *bset_context;
1948
403
1949
403
  if (
!bmap || 403
!context403
)
1950
0
    goto error;
1951
403
1952
403
  
if (403
bmap->n_ineq == 0 || 403
context->n_ineq == 033
)
{370
1953
370
    isl_basic_map_free(context);
1954
370
    return bmap;
1955
370
  }
1956
403
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
403
}
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
143k
{
1979
143k
  int i;
1980
143k
1981
1.88M
  for (i = 0; 
i < len1.88M
;
++i1.74M
)
{1.80M
1982
1.80M
    if (!relevant[i])
1983
1.39M
      continue;
1984
413k
    
if (413k
!413k
isl_int_is_zero413k
(c[i]))
1985
59.1k
      return 1;
1986
413k
  }
1987
143k
1988
84.4k
  return 0;
1989
143k
}
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
43.7k
{
1997
43.7k
  int i, dim;
1998
43.7k
1999
43.7k
  dim = isl_basic_map_dim(bmap, isl_dim_all);
2000
191k
  for (i = 0; 
i < dim191k
;
++i147k
)
2001
173k
    
if (173k
!relevant[i]173k
)
2002
25.4k
      break;
2003
43.7k
  if (i >= dim)
2004
18.2k
    return bmap;
2005
43.7k
2006
40.0k
  
for (i = bmap->n_eq - 1; 25.4k
i >= 040.0k
;
--i14.5k
)
2007
14.5k
    
if (14.5k
!is_related(bmap->eq[i] + 1, dim, relevant)14.5k
)
{7.14k
2008
7.14k
      bmap = isl_basic_map_cow(bmap);
2009
7.14k
      if (isl_basic_map_drop_equality(bmap, i) < 0)
2010
0
        return isl_basic_map_free(bmap);
2011
7.14k
    }
2012
25.4k
2013
154k
  
for (i = bmap->n_ineq - 1; 25.4k
i >= 0154k
;
--i129k
)
2014
129k
    
if (129k
!is_related(bmap->ineq[i] + 1, dim, relevant)129k
)
{77.3k
2015
77.3k
      bmap = isl_basic_map_cow(bmap);
2016
77.3k
      if (isl_basic_map_drop_inequality(bmap, i) < 0)
2017
0
        return isl_basic_map_free(bmap);
2018
77.3k
    }
2019
25.4k
2020
25.4k
  return bmap;
2021
25.4k
}
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
296k
{
2037
296k
  int j;
2038
296k
  int min = dim;
2039
296k
2040
3.92M
  for (j = 0; 
j < dim3.92M
;
++j3.62M
)
{3.62M
2041
3.62M
    if (isl_int_is_zero(c[j]))
2042
3.23M
      continue;
2043
391k
    
while (391k
group[j] >= 0 && 391k
group[group[j]] != group[j]138k
)
2044
59
      group[j] = group[group[j]];
2045
391k
    if (group[j] == min)
2046
52.6k
      continue;
2047
338k
    
if (338k
group[j] < min338k
)
{300k
2048
300k
      if (
min >= 0 && 300k
min < dim300k
)
2049
3.84k
        group[min] = group[j];
2050
300k
      min = group[j];
2051
300k
    } else
2052
38.2k
      group[group[j]] = min;
2053
338k
  }
2054
296k
}
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
22.1k
{
2061
22.1k
  isl_ctx *ctx;
2062
22.1k
  int dim;
2063
22.1k
2064
22.1k
  dim = isl_basic_set_dim(context, isl_dim_set);
2065
22.1k
  ctx = isl_basic_set_get_ctx(context);
2066
22.1k
  return isl_calloc_array(ctx, int, dim);
2067
22.1k
}
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
54.1k
{
2092
54.1k
  int dim;
2093
54.1k
  int i;
2094
54.1k
  int last;
2095
54.1k
2096
54.1k
  if (!bmap)
2097
0
    return NULL;
2098
54.1k
2099
54.1k
  dim = isl_basic_map_dim(bmap, isl_dim_all);
2100
54.1k
2101
54.1k
  last = -1;
2102
326k
  for (i = 0; 
i < dim326k
;
++i272k
)
2103
272k
    
if (272k
group[i] >= 0272k
)
2104
117k
      last = group[i] = i;
2105
54.1k
  if (
last < 054.1k
)
{10.3k
2106
10.3k
    free(group);
2107
10.3k
    return bmap;
2108
10.3k
  }
2109
54.1k
2110
91.8k
  
for (i = 0; 43.7k
i < bmap->n_eq91.8k
;
++i48.1k
)
2111
48.1k
    update_groups(dim, group, bmap->eq[i] + 1);
2112
292k
  for (i = 0; 
i < bmap->n_ineq292k
;
++i248k
)
2113
248k
    update_groups(dim, group, bmap->ineq[i] + 1);
2114
43.7k
2115
291k
  for (i = 0; 
i < dim291k
;
++i247k
)
2116
247k
    
if (247k
group[i] >= 0247k
)
2117
77.5k
      group[i] = group[group[i]];
2118
43.7k
2119
291k
  for (i = 0; 
i < dim291k
;
++i247k
)
2120
247k
    group[i] = group[i] == -1;
2121
43.7k
2122
43.7k
  bmap = drop_unrelated_constraints(bmap, group);
2123
43.7k
2124
43.7k
  free(group);
2125
43.7k
  return bmap;
2126
54.1k
}
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
13.8k
{
2141
13.8k
  int *group;
2142
13.8k
  int dim;
2143
13.8k
  int i, j;
2144
13.8k
2145
13.8k
  if (
!context || 13.8k
!bset13.8k
)
2146
0
    return isl_basic_set_free(context);
2147
13.8k
2148
13.8k
  group = alloc_groups(context);
2149
13.8k
2150
13.8k
  if (!group)
2151
0
    return isl_basic_set_free(context);
2152
13.8k
2153
13.8k
  dim = isl_basic_set_dim(bset, isl_dim_set);
2154
85.5k
  for (i = 0; 
i < dim85.5k
;
++i71.6k
)
{71.6k
2155
117k
    for (j = 0; 
j < bset->n_eq117k
;
++j46.2k
)
2156
63.8k
      
if (63.8k
!63.8k
isl_int_is_zero63.8k
(bset->eq[j][1 + i]))
2157
17.6k
        break;
2158
71.6k
    if (
j < bset->n_eq71.6k
)
{17.6k
2159
17.6k
      group[i] = -1;
2160
17.6k
      continue;
2161
17.6k
    }
2162
180k
    
for (j = 0; 54.0k
j < bset->n_ineq180k
;
++j126k
)
2163
145k
      
if (145k
!145k
isl_int_is_zero145k
(bset->ineq[j][1 + i]))
2164
18.8k
        break;
2165
54.0k
    if (j < bset->n_ineq)
2166
18.8k
      group[i] = -1;
2167
54.0k
  }
2168
13.8k
2169
13.8k
  return isl_basic_map_drop_unrelated_constraints(context, group);
2170
13.8k
}
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
8.22k
{
2187
8.22k
  int *group;
2188
8.22k
  int dim;
2189
8.22k
  int i, j, n;
2190
8.22k
2191
8.22k
  if (
!context || 8.22k
!ineq8.22k
)
2192
0
    return isl_basic_set_free(context);
2193
8.22k
2194
8.22k
  group = alloc_groups(context);
2195
8.22k
2196
8.22k
  if (!group)
2197
0
    return isl_basic_set_free(context);
2198
8.22k
2199
8.22k
  dim = isl_basic_set_dim(context, isl_dim_set);
2200
8.22k
  n = isl_mat_rows(ineq);
2201
51.0k
  for (i = 0; 
i < dim51.0k
;
++i42.8k
)
{42.8k
2202
173k
    for (j = 0; 
j < n173k
;
++j130k
)
{147k
2203
147k
      if (row[j] < 0)
2204
28.8k
        continue;
2205
118k
      
if (118k
!118k
isl_int_is_zero118k
(ineq->row[j][1 + i]))
2206
16.5k
        break;
2207
118k
    }
2208
42.8k
    if (j < n)
2209
16.5k
      group[i] = -1;
2210
42.8k
  }
2211
8.22k
2212
8.22k
  return isl_basic_map_drop_unrelated_constraints(context, group);
2213
8.22k
}
2214
2215
/* Do all "n" entries of "row" contain a negative value?
2216
 */
2217
static int all_neg(int *row, int n)
2218
9.28k
{
2219
9.28k
  int i;
2220
9.28k
2221
15.4k
  for (i = 0; 
i < n15.4k
;
++i6.15k
)
2222
14.3k
    
if (14.3k
row[i] >= 014.3k
)
2223
8.22k
      return 0;
2224
9.28k
2225
1.05k
  return 1;
2226
9.28k
}
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
9.28k
{
2244
9.28k
  int i;
2245
9.28k
  unsigned n_ineq;
2246
9.28k
  unsigned n_eq;
2247
9.28k
  int found_equality = 0;
2248
9.28k
2249
9.28k
  if (!bset)
2250
0
    return NULL;
2251
9.28k
  
if (9.28k
tab && 9.28k
tab->empty7.99k
)
2252
2.18k
    return isl_basic_set_set_to_empty(bset);
2253
9.28k
2254
7.10k
  n_ineq = bset->n_ineq;
2255
26.3k
  for (i = n_ineq - 1; 
i >= 026.3k
;
--i19.2k
)
{19.2k
2256
19.2k
    if (
row[i] < 019.2k
)
{7.49k
2257
7.49k
      if (isl_basic_set_drop_inequality(bset, i) < 0)
2258
0
        return isl_basic_set_free(bset);
2259
7.49k
      continue;
2260
7.49k
    }
2261
11.7k
    
if (11.7k
!tab11.7k
)
2262
416
      continue;
2263
11.3k
    n_eq = tab->n_eq;
2264
11.3k
    if (
isl_tab_is_equality(tab, n_eq + row[i])11.3k
)
{151
2265
151
      isl_basic_map_inequality_to_equality(bset, i);
2266
151
      found_equality = 1;
2267
11.1k
    } else 
if (11.1k
isl_tab_is_redundant(tab, n_eq + row[i])11.1k
)
{244
2268
244
      if (isl_basic_set_drop_inequality(bset, i) < 0)
2269
0
        return isl_basic_set_free(bset);
2270
244
    }
2271
11.3k
  }
2272
7.10k
2273
7.10k
  
if (7.10k
found_equality7.10k
)
2274
144
    bset = isl_basic_set_gauss(bset, NULL);
2275
7.10k
  bset = isl_basic_set_finalize(bset);
2276
7.10k
  return bset;
2277
7.10k
}
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
9.28k
{
2287
9.28k
  isl_mat_free(ineq);
2288
9.28k
  isl_basic_set_free(context);
2289
9.28k
2290
9.28k
  bset = update_ineq(bset, row, tab);
2291
9.28k
2292
9.28k
  free(row);
2293
9.28k
  isl_tab_free(tab);
2294
9.28k
  return bset;
2295
9.28k
}
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
13.8k
{
2332
13.8k
  int i, r;
2333
13.8k
  int *row = NULL;
2334
13.8k
  isl_ctx *ctx;
2335
13.8k
  isl_basic_set *combined = NULL;
2336
13.8k
  struct isl_tab *tab = NULL;
2337
13.8k
  unsigned n_eq, context_ineq;
2338
13.8k
2339
13.8k
  if (
!bset || 13.8k
!ineq13.8k
||
!context13.8k
)
2340
0
    goto error;
2341
13.8k
2342
13.8k
  
if (13.8k
bset->n_ineq == 0 || 13.8k
isl_basic_set_plain_is_universe(context)9.80k
)
{4.59k
2343
4.59k
    isl_basic_set_free(context);
2344
4.59k
    isl_mat_free(ineq);
2345
4.59k
    return bset;
2346
4.59k
  }
2347
13.8k
2348
9.28k
  ctx = isl_basic_set_get_ctx(context);
2349
9.28k
  row = isl_calloc_array(ctx, int, bset->n_ineq);
2350
9.28k
  if (!row)
2351
0
    goto error;
2352
9.28k
2353
9.28k
  
if (9.28k
mark_shifted_constraints(ineq, context, row) < 09.28k
)
2354
0
    goto error;
2355
9.28k
  
if (9.28k
all_neg(row, bset->n_ineq)9.28k
)
2356
1.05k
    return update_ineq_free(bset, ineq, context, row, NULL);
2357
9.28k
2358
8.22k
  context = drop_irrelevant_constraints_marked(context, ineq, row);
2359
8.22k
  if (!context)
2360
0
    goto error;
2361
8.22k
  
if (8.22k
isl_basic_set_plain_is_universe(context)8.22k
)
2362
232
    return update_ineq_free(bset, ineq, context, row, NULL);
2363
8.22k
2364
7.99k
  n_eq = context->n_eq;
2365
7.99k
  context_ineq = context->n_ineq;
2366
7.99k
  combined = isl_basic_set_cow(isl_basic_set_copy(context));
2367
7.99k
  combined = isl_basic_set_extend_constraints(combined, 0, bset->n_ineq);
2368
7.99k
  tab = isl_tab_from_basic_set(combined, 0);
2369
32.4k
  for (i = 0; 
i < context_ineq32.4k
;
++i24.4k
)
2370
24.4k
    
if (24.4k
isl_tab_freeze_constraint(tab, n_eq + i) < 024.4k
)
2371
0
      goto error;
2372
7.99k
  
if (7.99k
isl_tab_extend_cons(tab, bset->n_ineq) < 07.99k
)
2373
0
    goto error;
2374
7.99k
  r = context_ineq;
2375
28.7k
  for (i = 0; 
i < bset->n_ineq28.7k
;
++i20.7k
)
{20.7k
2376
20.7k
    if (row[i] < 0)
2377
4.44k
      continue;
2378
16.2k
    combined = isl_basic_set_add_ineq(combined, ineq->row[i]);
2379
16.2k
    if (isl_tab_add_ineq(tab, ineq->row[i]) < 0)
2380
0
      goto error;
2381
16.2k
    row[i] = r++;
2382
16.2k
  }
2383
7.99k
  
if (7.99k
isl_tab_detect_implicit_equalities(tab) < 07.99k
)
2384
0
    goto error;
2385
7.99k
  
if (7.99k
isl_tab_detect_redundant(tab) < 07.99k
)
2386
0
    goto error;
2387
28.7k
  
for (i = bset->n_ineq - 1; 7.99k
i >= 028.7k
;
--i20.7k
)
{20.7k
2388
20.7k
    isl_basic_set *test;
2389
20.7k
    int is_empty;
2390
20.7k
2391
20.7k
    if (row[i] < 0)
2392
4.44k
      continue;
2393
16.2k
    r = row[i];
2394
16.2k
    if (tab->con[n_eq + r].is_redundant)
2395
385
      continue;
2396
15.9k
    test = isl_basic_set_dup(combined);
2397
15.9k
    if (isl_inequality_negate(test, r) < 0)
2398
0
      test = isl_basic_set_free(test);
2399
15.9k
    test = isl_basic_set_update_from_tab(test, tab);
2400
15.9k
    is_empty = isl_basic_set_is_empty(test);
2401
15.9k
    isl_basic_set_free(test);
2402
15.9k
    if (is_empty < 0)
2403
0
      goto error;
2404
15.9k
    
if (15.9k
is_empty15.9k
)
2405
4.95k
      tab->con[n_eq + r].is_redundant = 1;
2406
15.9k
  }
2407
7.99k
  bset = update_ineq_free(bset, ineq, context, row, tab);
2408
7.99k
  if (
bset7.99k
)
{7.99k
2409
7.99k
    ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT);
2410
7.99k
    ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT);
2411
7.99k
  }
2412
7.99k
2413
7.99k
  isl_basic_set_free(combined);
2414
7.99k
  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
7.99k
}
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
13.8k
{
2429
13.8k
  unsigned total;
2430
13.8k
  isl_ctx *ctx;
2431
13.8k
  isl_mat *ineq;
2432
13.8k
2433
13.8k
  if (!bset)
2434
0
    return NULL;
2435
13.8k
2436
13.8k
  ctx = isl_basic_set_get_ctx(bset);
2437
13.8k
  total = isl_basic_set_total_dim(bset);
2438
13.8k
  ineq = isl_mat_sub_alloc6(ctx, bset->ineq, 0, bset->n_ineq,
2439
13.8k
            0, 1 + total);
2440
13.8k
2441
13.8k
  return ineq;
2442
13.8k
}
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
7.39k
{
2451
7.39k
  isl_mat *ineq;
2452
7.39k
2453
7.39k
  ineq = extract_ineq(bset);
2454
7.39k
  return uset_gist_full(bset, ineq, context);
2455
7.39k
}
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
6.49k
{
2489
6.49k
  isl_ctx *ctx;
2490
6.49k
  isl_mat *ineq;
2491
6.49k
  int i, n_row, n_col;
2492
6.49k
  isl_int rem;
2493
6.49k
2494
6.49k
  ineq = extract_ineq(bset);
2495
6.49k
  ineq = isl_mat_product(ineq, isl_mat_copy(T));
2496
6.49k
  context = isl_basic_set_preimage(context, T);
2497
6.49k
2498
6.49k
  if (
!ineq || 6.49k
!context6.49k
)
2499
0
    goto error;
2500
6.49k
  
if (6.49k
isl_basic_set_plain_is_empty(context)6.49k
)
{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
6.49k
2506
6.48k
  ctx = isl_mat_get_ctx(ineq);
2507
6.48k
  n_row = isl_mat_rows(ineq);
2508
6.48k
  n_col = isl_mat_cols(ineq);
2509
6.48k
  isl_int_init(rem);
2510
12.8k
  for (i = 0; 
i < n_row12.8k
;
++i6.33k
)
{6.33k
2511
6.33k
    isl_seq_gcd(ineq->row[i] + 1, n_col - 1, &ctx->normalize_gcd);
2512
6.33k
    if (isl_int_is_zero(ctx->normalize_gcd))
2513
17
      continue;
2514
6.31k
    
if (6.31k
isl_int_is_one6.31k
(ctx->normalize_gcd))
2515
5.92k
      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
6.48k
  isl_int_clear(rem);
2529
6.48k
2530
6.48k
  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
6.49k
}
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
6.49k
{
2543
6.49k
  int i, n;
2544
6.49k
2545
6.49k
  if (
!bset || 6.49k
!template6.49k
)
2546
0
    return isl_basic_set_free(bset);
2547
6.49k
2548
6.49k
  n = isl_basic_set_dim(template, isl_dim_set);
2549
6.49k
2550
40.7k
  for (i = 0; 
i < n40.7k
;
++i34.2k
)
{34.2k
2551
34.2k
    isl_bool involved;
2552
34.2k
2553
34.2k
    involved = isl_basic_set_involves_dims(template,
2554
34.2k
              isl_dim_set, i, 1);
2555
34.2k
    if (involved < 0)
2556
0
      return isl_basic_set_free(bset);
2557
34.2k
    
if (34.2k
involved34.2k
)
2558
19.7k
      continue;
2559
14.4k
    bset = isl_basic_set_eliminate_vars(bset, i, 1);
2560
14.4k
  }
2561
6.49k
2562
6.49k
  return bset;
2563
6.49k
}
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
13.8k
{
2591
13.8k
  isl_mat *eq;
2592
13.8k
  isl_mat *T;
2593
13.8k
  isl_basic_set *aff;
2594
13.8k
  isl_basic_set *aff_context;
2595
13.8k
  unsigned total;
2596
13.8k
2597
13.8k
  if (
!bset || 13.8k
!context13.8k
)
2598
0
    goto error;
2599
13.8k
2600
13.8k
  context = drop_irrelevant_constraints(context, bset);
2601
13.8k
2602
13.8k
  bset = isl_basic_set_detect_equalities(bset);
2603
13.8k
  aff = isl_basic_set_copy(bset);
2604
13.8k
  aff = isl_basic_set_plain_affine_hull(aff);
2605
13.8k
  context = isl_basic_set_detect_equalities(context);
2606
13.8k
  aff_context = isl_basic_set_copy(context);
2607
13.8k
  aff_context = isl_basic_set_plain_affine_hull(aff_context);
2608
13.8k
  aff = isl_basic_set_intersect(aff, aff_context);
2609
13.8k
  if (!aff)
2610
0
    goto error;
2611
13.8k
  
if (13.8k
isl_basic_set_plain_is_empty(aff)13.8k
)
{1
2612
1
    isl_basic_set_free(bset);
2613
1
    isl_basic_set_free(context);
2614
1
    return aff;
2615
1
  }
2616
13.8k
  bset = isl_basic_set_sort_constraints(bset);
2617
13.8k
  if (
aff->n_eq == 013.8k
)
{7.39k
2618
7.39k
    isl_basic_set_free(aff);
2619
7.39k
    return uset_gist_uncompressed(bset, context);
2620
7.39k
  }
2621
6.49k
  total = isl_basic_set_total_dim(bset);
2622
6.49k
  eq = isl_mat_sub_alloc6(bset->ctx, aff->eq, 0, aff->n_eq, 0, 1 + total);
2623
6.49k
  eq = isl_mat_cow(eq);
2624
6.49k
  T = isl_mat_variable_compression(eq, NULL);
2625
6.49k
  isl_basic_set_free(aff);
2626
6.49k
  if (
T && 6.49k
T->n_col == 06.49k
)
{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
6.49k
2632
6.49k
  aff_context = isl_basic_set_affine_hull(isl_basic_set_copy(context));
2633
6.49k
  aff_context = project_onto_involved(aff_context, bset);
2634
6.49k
2635
6.49k
  bset = uset_gist_compressed(bset, context, T);
2636
6.49k
  bset = isl_basic_set_reduce_using_equalities(bset, aff_context);
2637
6.49k
2638
6.49k
  if (
bset6.49k
)
{6.49k
2639
6.49k
    ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT);
2640
6.49k
    ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT);
2641
6.49k
  }
2642
6.49k
2643
6.49k
  return bset;
2644
0
error:
2645
0
  isl_basic_set_free(bset);
2646
0
  isl_basic_set_free(context);
2647
0
  return NULL;
2648
6.49k
}
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
806
{
2656
806
  int i;
2657
806
  int total, n_div;
2658
806
2659
806
  if (!bmap)
2660
0
    return -1;
2661
806
2662
806
  
if (806
bmap->n_eq == 0806
)
2663
287
    return 0;
2664
806
2665
519
  total = isl_basic_map_dim(bmap, isl_dim_all);
2666
519
  n_div = isl_basic_map_dim(bmap, isl_dim_div);
2667
519
  total -= n_div;
2668
519
2669
907
  for (i = 0; 
i < bmap->n_eq907
;
++i388
)
2670
594
    
if (594
isl_seq_first_non_zero(bmap->eq[i] + 1 + total,594
2671
594
              n_div) == -1)
2672
206
      return i;
2673
519
2674
313
  return bmap->n_eq;
2675
519
}
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
403
{
2990
403
  int bmap_n_eq, context_n_eq;
2991
403
  isl_mat *A;
2992
403
2993
403
  if (
!bmap || 403
!context403
)
2994
0
    return isl_basic_map_free(bmap);
2995
403
2996
403
  bmap_n_eq = n_div_eq(bmap);
2997
403
  context_n_eq = n_div_eq(context);
2998
403
2999
403
  if (
bmap_n_eq < 0 || 403
context_n_eq < 0403
)
3000
0
    return isl_basic_map_free(bmap);
3001
403
  
if (403
bmap_n_eq == 0 || 403
context_n_eq == 0245
)
3002
396
    return bmap;
3003
403
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
403
}
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
15.1k
{
3036
15.1k
  isl_basic_set *bset, *eq;
3037
15.1k
  isl_basic_map *eq_bmap;
3038
15.1k
  unsigned total, n_div, extra, n_eq, n_ineq;
3039
15.1k
3040
15.1k
  if (
!bmap || 15.1k
!context15.1k
)
3041
0
    goto error;
3042
15.1k
3043
15.1k
  
if (15.1k
isl_basic_map_plain_is_universe(bmap)15.1k
)
{1.25k
3044
1.25k
    isl_basic_map_free(context);
3045
1.25k
    return bmap;
3046
1.25k
  }
3047
13.8k
  
if (13.8k
isl_basic_map_plain_is_empty(context)13.8k
)
{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
13.8k
  
if (13.8k
isl_basic_map_plain_is_empty(bmap)13.8k
)
{0
3054
0
    isl_basic_map_free(context);
3055
0
    return bmap;
3056
0
  }
3057
13.8k
3058
13.8k
  bmap = isl_basic_map_remove_redundancies(bmap);
3059
13.8k
  context = isl_basic_map_remove_redundancies(context);
3060
13.8k
  if (!context)
3061
0
    goto error;
3062
13.8k
3063
13.8k
  context = isl_basic_map_align_divs(context, bmap);
3064
13.8k
  n_div = isl_basic_map_dim(context, isl_dim_div);
3065
13.8k
  total = isl_basic_map_dim(bmap, isl_dim_all);
3066
13.8k
  extra = n_div - isl_basic_map_dim(bmap, isl_dim_div);
3067
13.8k
3068
13.8k
  bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
3069
13.8k
  bset = isl_basic_set_add_dims(bset, isl_dim_set, extra);
3070
13.8k
  bset = uset_gist(bset,
3071
13.8k
        isl_basic_map_underlying_set(isl_basic_map_copy(context)));
3072
13.8k
  bset = isl_basic_set_project_out(bset, isl_dim_set, total, extra);
3073
13.8k
3074
13.8k
  if (
!bset || 13.8k
bset->n_eq == 013.8k
||
n_div == 08.21k
||
3075
13.4k
      
isl_basic_set_plain_is_empty(bset)504
)
{13.4k
3076
13.4k
    isl_basic_map_free(context);
3077
13.4k
    return isl_basic_map_overlying_set(bset, bmap);
3078
13.4k
  }
3079
13.8k
3080
403
  n_eq = bset->n_eq;
3081
403
  n_ineq = bset->n_ineq;
3082
403
  eq = isl_basic_set_copy(bset);
3083
403
  eq = isl_basic_set_cow(eq);
3084
403
  if (isl_basic_set_free_inequality(eq, n_ineq) < 0)
3085
0
    eq = isl_basic_set_free(eq);
3086
403
  if (isl_basic_set_free_equality(bset, n_eq) < 0)
3087
0
    bset = isl_basic_set_free(bset);
3088
403
3089
403
  eq_bmap = isl_basic_map_overlying_set(eq, isl_basic_map_copy(bmap));
3090
403
  eq_bmap = gist_strides(eq_bmap, context);
3091
403
  eq_bmap = isl_basic_map_remove_shifted_constraints(eq_bmap, context);
3092
403
  bmap = isl_basic_map_overlying_set(bset, bmap);
3093
403
  bmap = isl_basic_map_intersect(bmap, eq_bmap);
3094
403
  bmap = isl_basic_map_remove_redundancies(bmap);
3095
403
3096
403
  return bmap;
3097
0
error:
3098
0
  isl_basic_map_free(bmap);
3099
0
  isl_basic_map_free(context);
3100
0
  return NULL;
3101
13.8k
}
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
14.0k
{
3109
14.0k
  int i;
3110
14.0k
3111
14.0k
  if (
!map || 14.0k
!context14.0k
)
3112
0
    goto error;
3113
14.0k
3114
14.0k
  
if (14.0k
isl_basic_map_plain_is_empty(context)14.0k
)
{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
14.0k
3121
14.0k
  context = isl_basic_map_remove_redundancies(context);
3122
14.0k
  map = isl_map_cow(map);
3123
14.0k
  if (
!map || 14.0k
!context14.0k
)
3124
0
    goto error;
3125
14.0k
  
isl_assert14.0k
(map->ctx, isl_space_is_equal(map->dim, context->dim), goto error);14.0k
3126
14.0k
  map = isl_map_compute_divs(map);
3127
14.0k
  if (!map)
3128
0
    goto error;
3129
28.4k
  
for (i = map->n - 1; 14.0k
i >= 028.4k
;
--i14.4k
)
{14.4k
3130
14.4k
    map->p[i] = isl_basic_map_gist(map->p[i],
3131
14.4k
            isl_basic_map_copy(context));
3132
14.4k
    if (!map->p[i])
3133
0
      goto error;
3134
14.4k
    
if (14.4k
isl_basic_map_plain_is_empty(map->p[i])14.4k
)
{2.20k
3135
2.20k
      isl_basic_map_free(map->p[i]);
3136
2.20k
      if (i != map->n - 1)
3137
934
        map->p[i] = map->p[map->n - 1];
3138
2.20k
      map->n--;
3139
2.20k
    }
3140
14.4k
  }
3141
14.0k
  isl_basic_map_free(context);
3142
14.0k
  ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3143
14.0k
  return map;
3144
0
error:
3145
0
  isl_map_free(map);
3146
0
  isl_basic_map_free(context);
3147
0
  return NULL;
3148
14.0k
}
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
329
{
3166
329
  int i1, i2;
3167
329
  unsigned total, extra;
3168
329
3169
329
  if (
!bmap || 329
!context329
)
3170
0
    return isl_basic_map_free(bmap);
3171
329
3172
329
  total = isl_basic_map_total_dim(context);
3173
329
  extra = isl_basic_map_total_dim(bmap) - total;
3174
329
3175
329
  i1 = bmap->n_ineq - 1;
3176
329
  i2 = context->n_ineq - 1;
3177
1.38k
  while (
bmap && 1.38k
i1 >= 01.38k
&&
i2 >= 01.17k
)
{1.05k
3178
1.05k
    int cmp;
3179
1.05k
3180
1.05k
    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.03k
    cmp = isl_basic_map_constraint_cmp(context, bmap->ineq[i1],
3186
1.03k
              context->ineq[i2]);
3187
1.03k
    if (
cmp < 01.03k
)
{0
3188
0
      --i2;
3189
0
      continue;
3190
0
    }
3191
1.03k
    
if (1.03k
cmp > 01.03k
)
{289
3192
289
      --i1;
3193
289
      continue;
3194
289
    }
3195
745
    
if (745
isl_int_eq745
(bmap->ineq[i1][0], context->ineq[i2][0]))
{673
3196
673
      bmap = isl_basic_map_cow(bmap);
3197
673
      if (isl_basic_map_drop_inequality(bmap, i1) < 0)
3198
0
        bmap = isl_basic_map_free(bmap);
3199
673
    }
3200
745
    --i1;
3201
745
    --i2;
3202
745
  }
3203
329
3204
329
  return bmap;
3205
329
}
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
329
{
3221
329
  int i1, i2;
3222
329
  unsigned total, extra;
3223
329
3224
329
  if (
!bmap || 329
!context329
)
3225
0
    return isl_basic_map_free(bmap);
3226
329
3227
329
  total = isl_basic_map_total_dim(context);
3228
329
  extra = isl_basic_map_total_dim(bmap) - total;
3229
329
3230
329
  i1 = bmap->n_eq - 1;
3231
329
  i2 = context->n_eq - 1;
3232
329
3233
355
  while (
bmap && 355
i1 >= 0355
&&
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
329
3258
329
  return bmap;
3259
329
}
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
329
{
3272
329
  isl_bool done, known;
3273
329
3274
329
  done = isl_basic_map_plain_is_universe(context);
3275
329
  if (done == isl_bool_false)
3276
329
    done = isl_basic_map_plain_is_universe(bmap);
3277
329
  if (done == isl_bool_false)
3278
329
    done = isl_basic_map_plain_is_empty(context);
3279
329
  if (done == isl_bool_false)
3280
329
    done = isl_basic_map_plain_is_empty(bmap);
3281
329
  if (done < 0)
3282
0
    goto error;
3283
329
  
if (329
done329
)
{0
3284
0
    isl_basic_map_free(context);
3285
0
    return bmap;
3286
0
  }
3287
329
  known = isl_basic_map_divs_known(context);
3288
329
  if (known < 0)
3289
0
    goto error;
3290
329
  
if (329
!known329
)
3291
0
    isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3292
329
      "context has unknown divs", goto error);
3293
329
3294
329
  bmap = isl_basic_map_align_divs(bmap, context);
3295
329
  bmap = isl_basic_map_gauss(bmap, NULL);
3296
329
  bmap = isl_basic_map_sort_constraints(bmap);
3297
329
  context = isl_basic_map_sort_constraints(context);
3298
329
3299
329
  bmap = drop_inequalities(bmap, context);
3300
329
  bmap = drop_equalities(bmap, context);
3301
329
3302
329
  isl_basic_map_free(context);
3303
329
  bmap = isl_basic_map_finalize(bmap);
3304
329
  return bmap;
3305
0
error:
3306
0
  isl_basic_map_free(bmap);
3307
0
  isl_basic_map_free(context);
3308
0
  return NULL;
3309
329
}
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
152
{
3333
152
  int i;
3334
152
  isl_bool univ, known;
3335
152
3336
152
  univ = isl_basic_map_plain_is_universe(context);
3337
152
  if (univ < 0)
3338
0
    goto error;
3339
152
  
if (152
univ152
)
{0
3340
0
    isl_basic_map_free(context);
3341
0
    return map;
3342
0
  }
3343
152
  known = isl_basic_map_divs_known(context);
3344
152
  if (known < 0)
3345
0
    goto error;
3346
152
  
if (152
!known152
)
3347
0
    isl_die(isl_map_get_ctx(map), isl_error_invalid,
3348
152
      "context has unknown divs", goto error);
3349
152
3350
152
  map = isl_map_cow(map);
3351
152
  if (!map)
3352
0
    goto error;
3353
474
  
for (i = 0; 152
i < map->n474
;
++i322
)
{329
3354
329
    map->p[i] = isl_basic_map_plain_gist(map->p[i],
3355
329
            isl_basic_map_copy(context));
3356
329
    univ = isl_basic_map_plain_is_universe(map->p[i]);
3357
329
    if (univ < 0)
3358
0
      goto error;
3359
329
    
if (329
univ && 329
map->n > 17
)
3360
7
      return replace_by_disjunct(map, i, context);
3361
329
  }
3362
152
3363
145
  isl_basic_map_free(context);
3364
145
  ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3365
145
  if (map->n > 1)
3366
145
    ISL_F_CLR(map, ISL_MAP_DISJOINT);
3367
145
  return map;
3368
0
error:
3369
0
  isl_map_free(map);
3370
0
  isl_basic_map_free(context);
3371
0
  return NULL;
3372
152
}
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.16k
{
3379
5.16k
  isl_map *res;
3380
5.16k
3381
5.16k
  res = isl_map_universe(isl_map_get_space(map));
3382
5.16k
  isl_map_free(map);
3383
5.16k
  isl_map_free(drop);
3384
5.16k
  return res;
3385
5.16k
}
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
39.5k
{
3415
39.5k
  int equal;
3416
39.5k
  int is_universe;
3417
39.5k
  int single_disjunct_map, single_disjunct_context;
3418
39.5k
  isl_bool subset;
3419
39.5k
  isl_basic_map *hull;
3420
39.5k
3421
39.5k
  is_universe = isl_map_plain_is_universe(map);
3422
39.5k
  if (
is_universe >= 0 && 39.5k
!is_universe39.5k
)
3423
24.2k
    is_universe = isl_map_plain_is_universe(context);
3424
39.5k
  if (is_universe < 0)
3425
0
    goto error;
3426
39.5k
  
if (39.5k
is_universe39.5k
)
{22.2k
3427
22.2k
    isl_map_free(context);
3428
22.2k
    return map;
3429
22.2k
  }
3430
39.5k
3431
17.3k
  equal = isl_map_plain_is_equal(map, context);
3432
17.3k
  if (equal < 0)
3433
0
    goto error;
3434
17.3k
  
if (17.3k
equal17.3k
)
3435
5.14k
    return replace_by_universe(map, context);
3436
17.3k
3437
12.2k
  single_disjunct_map = isl_map_n_basic_map(map) == 1;
3438
12.2k
  single_disjunct_context = isl_map_n_basic_map(context) == 1;
3439
12.2k
  if (
!single_disjunct_map || 12.2k
!single_disjunct_context8.22k
)
{4.19k
3440
4.19k
    subset = isl_map_is_subset(context, map);
3441
4.19k
    if (subset < 0)
3442
0
      goto error;
3443
4.19k
    
if (4.19k
subset4.19k
)
3444
24
      return replace_by_universe(map, context);
3445
4.19k
  }
3446
12.2k
3447
12.1k
  context = isl_map_compute_divs(context);
3448
12.1k
  if (!context)
3449
0
    goto error;
3450
12.1k
  
if (12.1k
single_disjunct_context12.1k
)
{11.7k
3451
11.7k
    hull = isl_map_simple_hull(context);
3452
396
  } else {
3453
396
    isl_ctx *ctx;
3454
396
    isl_map_list *list;
3455
396
3456
396
    ctx = isl_map_get_ctx(map);
3457
396
    list = isl_map_list_alloc(ctx, 2);
3458
396
    list = isl_map_list_add(list, isl_map_copy(context));
3459
396
    list = isl_map_list_add(list, isl_map_copy(map));
3460
396
    hull = isl_map_unshifted_simple_hull_from_map_list(context,
3461
396
                    list);
3462
396
  }
3463
12.1k
  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
12.1k
}
3469
3470
__isl_give isl_map *isl_map_gist(__isl_take isl_map *map,
3471
  __isl_take isl_map *context)
3472
39.5k
{
3473
39.5k
  return isl_map_align_params_map_map_and(map, context, &map_gist);
3474
39.5k
}
3475
3476
struct isl_basic_set *isl_basic_set_gist(struct isl_basic_set *bset,
3477
            struct isl_basic_set *context)
3478
728
{
3479
728
  return bset_from_bmap(isl_basic_map_gist(bset_to_bmap(bset),
3480
728
            bset_to_bmap(context)));
3481
728
}
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.85k
{
3486
1.85k
  return set_from_map(isl_map_gist_basic_map(set_to_map(set),
3487
1.85k
          bset_to_bmap(context)));
3488
1.85k
}
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
11.3k
{
3502
11.3k
  return set_from_map(isl_map_gist(set_to_map(set), set_to_map(context)));
3503
11.3k
}
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
4.60k
{
3521
4.60k
  isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3522
4.60k
  map_context = isl_map_intersect_domain(map_context, context);
3523
4.60k
  return isl_map_gist(map, map_context);
3524
4.60k
}
3525
3526
__isl_give isl_map *isl_map_gist_range(__isl_take isl_map *map,
3527
  __isl_take isl_set *context)
3528
76
{
3529
76
  isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3530
76
  map_context = isl_map_intersect_range(map_context, context);
3531
76
  return isl_map_gist(map, map_context);
3532
76
}
3533
3534
__isl_give isl_map *isl_map_gist_params(__isl_take isl_map *map,
3535
  __isl_take isl_set *context)
3536
20.3k
{
3537
20.3k
  isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3538
20.3k
  map_context = isl_map_intersect_params(map_context, context);
3539
20.3k
  return isl_map_gist(map, map_context);
3540
20.3k
}
3541
3542
__isl_give isl_set *isl_set_gist_params(__isl_take isl_set *set,
3543
  __isl_take isl_set *context)
3544
15.3k
{
3545
15.3k
  return isl_map_gist_params(set, context);
3546
15.3k
}
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
16.5k
{
3556
16.5k
  struct isl_vec *v = NULL;
3557
16.5k
  int *elim = NULL;
3558
16.5k
  unsigned total;
3559
16.5k
  int i;
3560
16.5k
3561
16.5k
  if (
!bmap1 || 16.5k
!bmap216.5k
)
3562
0
    return isl_bool_error;
3563
16.5k
  
isl_assert16.5k
(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),16.5k
3564
16.5k
      return isl_bool_error);
3565
16.5k
  
if (16.5k
bmap1->n_div || 16.5k
bmap2->n_div15.4k
)
3566
1.32k
    return isl_bool_false;
3567
15.2k
  
if (15.2k
!bmap1->n_eq && 15.2k
!bmap2->n_eq6.89k
)
3568
6.12k
    return isl_bool_false;
3569
15.2k
3570
9.13k
  total = isl_space_dim(bmap1->dim, isl_dim_all);
3571
9.13k
  if (total == 0)
3572
0
    return isl_bool_false;
3573
9.13k
  v = isl_vec_alloc(bmap1->ctx, 1 + total);
3574
9.13k
  if (!v)
3575
0
    goto error;
3576
9.13k
  
elim = 9.13k
isl_alloc_array9.13k
(bmap1->ctx, int, total);
3577
9.13k
  if (!elim)
3578
0
    goto error;
3579
9.13k
  compute_elimination_index(bmap1, elim);
3580
19.3k
  for (i = 0; 
i < bmap2->n_eq19.3k
;
++i10.2k
)
{10.4k
3581
10.4k
    int reduced;
3582
10.4k
    reduced = reduced_using_equalities(v->block.data, bmap2->eq[i],
3583
10.4k
              bmap1, elim);
3584
10.4k
    if (
reduced && 10.4k
!4.92k
isl_int_is_zero4.92k
(v->block.data[0]) &&
3585
949
        isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3586
216
      goto disjoint;
3587
10.4k
  }
3588
78.1k
  
for (i = 0; 8.92k
i < bmap2->n_ineq78.1k
;
++i69.2k
)
{72.1k
3589
72.1k
    int reduced;
3590
72.1k
    reduced = reduced_using_equalities(v->block.data,
3591
72.1k
            bmap2->ineq[i], bmap1, elim);
3592
72.1k
    if (
reduced && 72.1k
isl_int_is_neg4.42k
(v->block.data[0]) &&
3593
3.40k
        isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3594
2.91k
      goto disjoint;
3595
72.1k
  }
3596
6.00k
  compute_elimination_index(bmap2, elim);
3597
45.3k
  for (i = 0; 
i < bmap1->n_ineq45.3k
;
++i39.3k
)
{40.8k
3598
40.8k
    int reduced;
3599
40.8k
    reduced = reduced_using_equalities(v->block.data,
3600
40.8k
            bmap1->ineq[i], bmap2, elim);
3601
40.8k
    if (
reduced && 40.8k
isl_int_is_neg2.27k
(v->block.data[0]) &&
3602
1.69k
        isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3603
1.48k
      goto disjoint;
3604
40.8k
  }
3605
4.52k
  isl_vec_free(v);
3606
4.52k
  free(elim);
3607
4.52k
  return isl_bool_false;
3608
4.61k
disjoint:
3609
4.61k
  isl_vec_free(v);
3610
4.61k
  free(elim);
3611
4.61k
  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.00k
}
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
12.6k
{
3631
12.6k
  int i, j;
3632
12.6k
3633
12.6k
  if (
!map1 || 12.6k
!map212.6k
)
3634
0
    return isl_bool_error;
3635
12.6k
3636
19.1k
  
for (i = 0; 12.6k
i < map1->n19.1k
;
++i6.42k
)
{14.9k
3637
22.9k
    for (j = 0; 
j < map2->n22.9k
;
++j7.99k
)
{16.5k
3638
16.5k
      isl_bool d = test(map1->p[i], map2->p[j]);
3639
16.5k
      if (d != isl_bool_true)
3640
8.56k
        return d;
3641
16.5k
    }
3642
14.9k
  }
3643
12.6k
3644
4.12k
  return isl_bool_true;
3645
12.6k
}
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
25.6k
{
3656
25.6k
  isl_bool disjoint;
3657
25.6k
  isl_bool match;
3658
25.6k
3659
25.6k
  if (
!map1 || 25.6k
!map225.6k
)
3660
0
    return isl_bool_error;
3661
25.6k
3662
25.6k
  disjoint = isl_map_plain_is_empty(map1);
3663
25.6k
  if (
disjoint < 0 || 25.6k
disjoint25.6k
)
3664
3.47k
    return disjoint;
3665
25.6k
3666
22.1k
  disjoint = isl_map_plain_is_empty(map2);
3667
22.1k
  if (
disjoint < 0 || 22.1k
disjoint22.1k
)
3668
5.06k
    return disjoint;
3669
22.1k
3670
17.0k
  match = isl_space_tuple_is_equal(map1->dim, isl_dim_in,
3671
17.0k
        map2->dim, isl_dim_in);
3672
17.0k
  if (
match < 0 || 17.0k
!match17.0k
)
3673
0
    
return match < 0 ? 0
isl_bool_error0
:
isl_bool_true0
;
3674
17.0k
3675
17.0k
  match = isl_space_tuple_is_equal(map1->dim, isl_dim_out,
3676
17.0k
        map2->dim, isl_dim_out);
3677
17.0k
  if (
match < 0 || 17.0k
!match17.0k
)
3678
0
    
return match < 0 ? 0
isl_bool_error0
:
isl_bool_true0
;
3679
17.0k
3680
17.0k
  return isl_bool_false;
3681
17.0k
}
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
25.6k
{
3730
25.6k
  isl_bool disjoint;
3731
25.6k
  isl_bool intersect;
3732
25.6k
3733
25.6k
  disjoint = isl_map_plain_is_disjoint_global(map1, map2);
3734
25.6k
  if (
disjoint < 0 || 25.6k
disjoint25.6k
)
3735
8.53k
    return disjoint;
3736
25.6k
3737
17.0k
  disjoint = isl_map_is_empty(map1);
3738
17.0k
  if (
disjoint < 0 || 17.0k
disjoint17.0k
)
3739
0
    return disjoint;
3740
17.0k
3741
17.0k
  disjoint = isl_map_is_empty(map2);
3742
17.0k
  if (
disjoint < 0 || 17.0k
disjoint17.0k
)
3743
0
    return disjoint;
3744
17.0k
3745
17.0k
  intersect = isl_map_plain_is_universe(map1);
3746
17.0k
  if (
intersect < 0 || 17.0k
intersect17.0k
)
3747
2.68k
    
return intersect < 0 ? 2.68k
isl_bool_error0
:
isl_bool_false2.68k
;
3748
17.0k
3749
14.4k
  intersect = isl_map_plain_is_universe(map2);
3750
14.4k
  if (
intersect < 0 || 14.4k
intersect14.4k
)
3751
1.27k
    
return intersect < 0 ? 1.27k
isl_bool_error0
:
isl_bool_false1.27k
;
3752
14.4k
3753
13.1k
  intersect = isl_map_plain_is_equal(map1, map2);
3754
13.1k
  if (
intersect < 0 || 13.1k
intersect13.1k
)
3755
441
    return isl_bool_not(intersect);
3756
13.1k
3757
12.6k
  return all_pairs(map1, map2, &isl_basic_map_is_disjoint);
3758
13.1k
}
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
16.5k
{
3770
16.5k
  isl_bool disjoint;
3771
16.5k
  isl_bool intersect;
3772
16.5k
  isl_basic_map *test;
3773
16.5k
3774
16.5k
  disjoint = isl_basic_map_plain_is_disjoint(bmap1, bmap2);
3775
16.5k
  if (
disjoint < 0 || 16.5k
disjoint16.5k
)
3776
4.61k
    return disjoint;
3777
16.5k
3778
11.9k
  disjoint = isl_basic_map_is_empty(bmap1);
3779
11.9k
  if (
disjoint < 0 || 11.9k
disjoint11.9k
)
3780
0
    return disjoint;
3781
11.9k
3782
11.9k
  disjoint = isl_basic_map_is_empty(bmap2);
3783
11.9k
  if (
disjoint < 0 || 11.9k
disjoint11.9k
)
3784
0
    return disjoint;
3785
11.9k
3786
11.9k
  intersect = isl_basic_map_plain_is_universe(bmap1);
3787
11.9k
  if (
intersect < 0 || 11.9k
intersect11.9k
)
3788
0
    
return intersect < 0 ? 0
isl_bool_error0
:
isl_bool_false0
;
3789
11.9k
3790
11.9k
  intersect = isl_basic_map_plain_is_universe(bmap2);
3791
11.9k
  if (
intersect < 0 || 11.9k
intersect11.9k
)
3792
0
    
return intersect < 0 ? 0
isl_bool_error0
:
isl_bool_false0
;
3793
11.9k
3794
11.9k
  test = isl_basic_map_intersect(isl_basic_map_copy(bmap1),
3795
11.9k
    isl_basic_map_copy(bmap2));
3796
11.9k
  disjoint = isl_basic_map_is_empty(test);
3797
11.9k
  isl_basic_map_free(test);
3798
11.9k
3799
11.9k
  return disjoint;
3800
11.9k
}
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
8.98k
{
3820
8.98k
  return isl_map_is_disjoint(set1, set2);
3821
8.98k
}
3822
3823
/* Is "v" equal to 0, 1 or -1?
3824
 */
3825
static int is_zero_or_one(isl_int v)
3826
118
{
3827
118
  return 
isl_int_is_zero118
(v) ||
isl_int_is_one51
(v) ||
isl_int_is_negone29
(v);
3828
118
}
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 (execpt 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
478
{
3863
478
  int i, j;
3864
478
  unsigned dim;
3865
478
  int coalesce = -1;
3866
478
3867
478
  if (bmap->n_div <= 1)
3868
262
    return -1;
3869
216
  dim = isl_space_dim(bmap->dim, isl_dim_all);
3870
216
  if (isl_seq_first_non_zero(bmap->ineq[l] + 1 + dim, div) != -1)
3871
6
    return -1;
3872
210
  
if (210
isl_seq_first_non_zero(bmap->ineq[l] + 1 + dim + div + 1,210
3873
210
           bmap->n_div - div - 1) != -1)
3874
19
    return -1;
3875
191
  
if (191
!isl_seq_is_neg(bmap->ineq[l] + 1, bmap->ineq[u] + 1,191
3876
191
          dim + bmap->n_div))
3877
123
    return -1;
3878
191
3879
224
  
for (i = 0; 68
i < bmap->n_div224
;
++i156
)
{156
3880
156
    if (isl_int_is_zero(bmap->div[i][0]))
3881
102
      continue;
3882
54
    
if (54
!54
isl_int_is_zero54
(bmap->div[i][1 + 1 + dim + div]))
3883
0
      return -1;
3884
54
  }
3885
68
3886
68
  
isl_int_add68
(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0]);68
3887
68
  if (
isl_int_is_neg68
(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
68
  
isl_int_add_ui68
(bmap->ineq[l][0], bmap->ineq[l][0], 1);68
3896
222
  for (i = 0; 
i < bmap->n_div222
;
++i154
)
{155
3897
155
    if (i == div)
3898
67
      continue;
3899
88
    
if (88
!pairs[i]88
)
3900
48
      continue;
3901
150
    
for (j = 0; 40
j < bmap->n_div150
;
++j110
)
{110
3902
110
      if (isl_int_is_zero(bmap->div[j][0]))
3903
79
        continue;
3904
31
      
if (31
!31
isl_int_is_zero31
(bmap->div[j][1 + 1 + dim + i]))
3905
0
        break;
3906
31
    }
3907
40
    if (j < bmap->n_div)
3908
0
      continue;
3909
164
    
for (j = 0; 40
j < bmap->n_ineq164
;
++j124
)
{163
3910
163
      int valid;
3911
163
      if (
j == l || 163
j == u152
)
3912
22
        continue;
3913
141
      
if (141
isl_int_is_zero141
(bmap->ineq[j][1 + dim + div]))
{118
3914
118
        if (is_zero_or_one(bmap->ineq[j][1 + dim + i]))
3915
98
          continue;
3916
20
        break;
3917
118
      }
3918
23
      
if (23
isl_int_is_zero23
(bmap->ineq[j][1 + dim + i]))
3919
11
        break;
3920
12
      
isl_int_mul12
(bmap->ineq[j][1 + dim + div],12
3921
12
            bmap->ineq[j][1 + dim + div],
3922
12
            bmap->ineq[l][0]);
3923
12
      valid = isl_int_eq(bmap->ineq[j][1 + dim + div],
3924
12
             bmap->ineq[j][1 + dim + i]);
3925
12
      isl_int_divexact(bmap->ineq[j][1 + dim + div],
3926
12
           bmap->ineq[j][1 + dim + div],
3927
12
           bmap->ineq[l][0]);
3928
12
      if (!valid)
3929
8
        break;
3930
12
    }
3931
40
    if (j < bmap->n_ineq)
3932
39
      continue;
3933
1
    coalesce = i;
3934
1
    break;
3935
40
  }
3936
68
  isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
3937
68
  isl_int_sub(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0]);
3938
68
  return coalesce;
3939
68
}
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
737
{
3963
737
  isl_tab_free(data->tab);
3964
737
  isl_vec_free(data->v);
3965
737
  isl_int_clear(data->g);
3966
737
  isl_int_clear(data->fl);
3967
737
  isl_int_clear(data->fu);
3968
737
}
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
540
{
3977
540
  isl_ctx *ctx;
3978
540
  enum isl_lp_result res;
3979
540
3980
540
  ctx = isl_basic_map_get_ctx(bmap);
3981
540
  if (!data->tab)
3982
436
    data->tab = isl_tab_from_basic_map(bmap, 0);
3983
540
  res = isl_tab_min(data->tab, data->v->el, ctx->one, &data->g, NULL, 0);
3984
540
  if (res == isl_lp_error)
3985
0
    return isl_bool_error;
3986
540
  
return res == isl_lp_ok && 540
isl_int_is_nonneg540
(data->g);
3987
540
}
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
1.47k
{
4044
1.47k
  unsigned offset, n_div;
4045
1.47k
  offset = isl_basic_map_offset(bmap, isl_dim_div);
4046
1.47k
  n_div = isl_basic_map_dim(bmap, isl_dim_div);
4047
1.47k
4048
1.47k
  isl_int_gcd(data->g,
4049
1.47k
        bmap->ineq[l][offset + i], bmap->ineq[u][offset + i]);
4050
1.47k
  isl_int_divexact(data->fl, bmap->ineq[l][offset + i], data->g);
4051
1.47k
  isl_int_divexact(data->fu, bmap->ineq[u][offset + i], data->g);
4052
1.47k
  isl_int_neg(data->fu, data->fu);
4053
1.47k
  isl_seq_combine(data->v->el, data->fl, bmap->ineq[u],
4054
1.47k
      data->fu, bmap->ineq[l], offset + n_div);
4055
1.47k
  isl_int_mul(data->g, data->g, data->fl);
4056
1.47k
  isl_int_mul(data->g, data->g, data->fu);
4057
1.47k
  isl_int_sub(data->g, data->g, data->fl);
4058
1.47k
  isl_int_sub(data->g, data->g, data->fu);
4059
1.47k
  isl_int_add_ui(data->g, data->g, 1);
4060
1.47k
  isl_int_sub(data->fl, data->v->el[0], data->g);
4061
1.47k
4062
1.47k
  isl_seq_gcd(data->v->el + 1, offset - 1 + n_div, &data->g);
4063
1.47k
  if (isl_int_is_zero(data->g))
4064
431
    
return 431
isl_int_is_nonneg431
(data->fl);
4065
1.04k
  
if (1.04k
isl_int_is_one1.04k
(data->g))
{374
4066
374
    isl_int_set(data->v->el[0], data->fl);
4067
374
    return test_ineq_is_satisfied(bmap, data);
4068
374
  }
4069
674
  
isl_int_fdiv_q674
(data->fl, data->fl, data->g);674
4070
674
  isl_int_fdiv_q(data->v->el[0], data->v->el[0], data->g);
4071
674
  if (isl_int_eq(data->fl, data->v->el[0]))
4072
508
    return isl_bool_true;
4073
166
  
isl_int_set166
(data->v->el[0], data->fl);166
4074
166
  isl_seq_scale_down(data->v->el + 1, data->v->el + 1, data->g,
4075
166
          offset - 1 + n_div);
4076
166
4077
166
  return test_ineq_is_satisfied(bmap, data);
4078
674
}
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
737
{
4094
737
  isl_ctx *ctx;
4095
737
  struct test_ineq_data data = { NULL, NULL };
4096
737
  unsigned off, n_div;
4097
737
  int remove = -1;
4098
737
4099
737
  isl_int_init(data.g);
4100
737
  isl_int_init(data.fl);
4101
737
  isl_int_init(data.fu);
4102
737
4103
737
  if (!bmap)
4104
0
    goto error;
4105
737
4106
737
  ctx = isl_basic_map_get_ctx(bmap);
4107
737
  off = isl_basic_map_offset(bmap, isl_dim_div);
4108
737
  n_div = isl_basic_map_dim(bmap, isl_dim_div);
4109
737
  data.v = isl_vec_alloc(ctx, off + n_div);
4110
737
  if (!data.v)
4111
0
    goto error;
4112
737
4113
1.24k
  
while (737
n > 01.24k
)
{869
4114
869
    int i, l, u;
4115
869
    int best = -1;
4116
869
    isl_bool has_int;
4117
869
4118
2.53k
    for (i = 0; 
i < n_div2.53k
;
++i1.67k
)
{1.67k
4119
1.67k
      if (!pairs[i])
4120
514
        continue;
4121
1.15k
      
if (1.15k
best >= 0 && 1.15k
pairs[best] <= pairs[i]287
)
4122
215
        continue;
4123
941
      best = i;
4124
941
    }
4125
869
4126
869
    i = best;
4127
6.16k
    for (l = 0; 
l < bmap->n_ineq6.16k
;
++l5.29k
)
{5.80k
4128
5.80k
      if (
!5.80k
isl_int_is_pos5.80k
(bmap->ineq[l][off + i]))
4129
4.37k
        continue;
4130
1.42k
      
if (1.42k
isl_int_is_one1.42k
(bmap->ineq[l][off + i]))
4131
405
        continue;
4132
9.55k
      
for (u = 0; 1.02k
u < bmap->n_ineq9.55k
;
++u8.52k
)
{9.03k
4133
9.03k
        if (
!9.03k
isl_int_is_neg9.03k
(bmap->ineq[u][off + i]))
4134
7.16k
          continue;
4135
1.86k
        
if (1.86k
isl_int_is_negone1.86k
(bmap->ineq[u][off + i]))
4136
384
          continue;
4137
1.47k
        has_int = int_between_bounds(bmap, i, l, u,
4138
1.47k
                &data);
4139
1.47k
        if (has_int < 0)
4140
0
          goto error;
4141
1.47k
        
if (1.47k
data.tab && 1.47k
data.tab->empty1.10k
)
4142
0
          break;
4143
1.47k
        
if (1.47k
!has_int1.47k
)
4144
503
          break;
4145
1.47k
      }
4146
1.02k
      
if (1.02k
u < bmap->n_ineq1.02k
)
4147
503
        break;
4148
1.02k
    }
4149
869
    
if (869
data.tab && 869
data.tab->empty555
)
{0
4150
0
      bmap = isl_basic_map_set_to_empty(bmap);
4151
0
      break;
4152
0
    }
4153
869
    
if (869
l == bmap->n_ineq869
)
{366
4154
366
      remove = i;
4155
366
      break;
4156
366
    }
4157
503
    pairs[i] = 0;
4158
503
    --n;
4159
503
  }
4160
737
4161
737
  test_ineq_data_clear(&data);
4162
737
4163
737
  free(pairs);
4164
737
4165
737
  if (remove < 0)
4166
371
    return bmap;
4167
737
4168
366
  bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, remove, 1);
4169
366
  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
737
}
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 the 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
738
{
4285
738
  int i, l, u;
4286
738
  unsigned dim;
4287
738
4288
738
  dim = isl_space_dim(bmap->dim, isl_dim_all);
4289
738
4290
1.98k
  for (i = 0; 
i < bmap->n_div1.98k
;
++i1.24k
)
{1.25k
4291
1.25k
    if (!pairs[i])
4292
301
      continue;
4293
10.8k
    
for (l = 0; 949
l < bmap->n_ineq10.8k
;
++l9.88k
)
{9.88k
4294
9.88k
      if (
!9.88k
isl_int_is_one9.88k
(bmap->ineq[l][1 + dim + i]))
4295
9.41k
        continue;
4296
4.26k
      
for (u = 0; 474
u < bmap->n_ineq4.26k
;
++u3.78k
)
{3.79k
4297
3.79k
        int c;
4298
3.79k
4299
3.79k
        if (
!3.79k
isl_int_is_negone3.79k
(bmap->ineq[u][1+dim+i]))
4300
3.31k
          continue;
4301
478
        c = div_find_coalesce(bmap, pairs, i, l, u);
4302
478
        if (c < 0)
4303
477
          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
478
      }
4308
474
    }
4309
949
  }
4310
738
4311
737
  
if (737
ISL_F_ISSET737
(bmap, ISL_BASIC_MAP_EMPTY))
{0
4312
0
    free(pairs);
4313
0
    return bmap;
4314
0
  }
4315
737
4316
737
  return drop_more_redundant_divs(bmap, pairs, n);
4317
737
}
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
286
{
4325
286
  return isl_seq_eq(bmap->ineq[i] + first, bmap->ineq[j] + first, n);
4326
286
}
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
2.75k
{
4334
2.75k
  return isl_seq_is_neg(bmap->ineq[i] + first, bmap->ineq[j] + first, n);
4335
2.75k
}
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
2.42k
{
4342
2.42k
  unsigned total;
4343
2.42k
4344
2.42k
  total = isl_basic_map_dim(bmap, isl_dim_all);
4345
2.42k
  return is_opposite_part(bmap, i, j, 1, total);
4346
2.42k
}
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
269
{
4354
269
  unsigned total;
4355
269
4356
269
  total = isl_basic_map_dim(bmap, isl_dim_all);
4357
269
  return is_parallel_part(bmap, i, j, 1, pos - 1) &&
4358
17
    is_parallel_part(bmap, i, j, pos + 1, total - pos);
4359
269
}
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
271
{
4367
271
  unsigned total;
4368
271
4369
271
  total = isl_basic_map_dim(bmap, isl_dim_all);
4370
271
  return is_opposite_part(bmap, i, j, 1, pos - 1) &&
4371
61
    is_opposite_part(bmap, i, j, pos + 1, total - pos);
4372
271
}
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.56k
{
4382
2.56k
  if (simplify)
4383
904
    bmap = isl_basic_map_simplify(bmap);
4384
2.56k
  free(pairs);
4385
2.56k
  return isl_basic_map_drop_redundant_divs(bmap);
4386
2.56k
}
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.10k
{
4395
1.10k
  int i;
4396
1.10k
  unsigned n_div, o_div;
4397
1.10k
  isl_bool known;
4398
1.10k
4399
1.10k
  known = isl_basic_map_div_is_known(bmap, div);
4400
1.10k
  if (
known < 0 || 1.10k
known1.10k
)
4401
68
    return isl_bool_not(known);
4402
1.03k
  n_div = isl_basic_map_dim(bmap, isl_dim_div);
4403
1.03k
  if (n_div == 1)
4404
731
    return isl_bool_true;
4405
302
  o_div = isl_basic_map_offset(bmap, isl_dim_div);
4406
982
  for (i = 0; 
i < n_div982
;
++i680
)
{715
4407
715
    isl_bool known;
4408
715
4409
715
    if (i == div)
4410
284
      continue;
4411
431
    
if (431
isl_int_is_zero431
(bmap->ineq[ineq][o_div + i]))
4412
384
      continue;
4413
47
    known = isl_basic_map_div_is_known(bmap, i);
4414
47
    if (
known < 0 || 47
!known47
)
4415
35
      return known;
4416
47
  }
4417
302
4418
267
  return isl_bool_true;
4419
302
}
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
998
{
4426
998
  unsigned o_div;
4427
998
4428
998
  o_div = isl_basic_map_offset(bmap, isl_dim_div);
4429
998
  if (isl_int_is_one(bmap->ineq[ineq][o_div + div]))
4430
901
    return isl_bool_true;
4431
998
4432
97
  return isl_bool_false;
4433
998
}
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
901
{
4443
901
  bmap = isl_basic_map_cow(bmap);
4444
901
  isl_basic_map_inequality_to_equality(bmap, ineq);
4445
901
  return drop_redundant_divs_again(bmap, pairs, 1);
4446
901
}
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
567
{
4458
567
  if (
ineq1 > ineq2567
)
{390
4459
390
    isl_basic_map_drop_inequality(bmap, ineq1);
4460
390
    isl_basic_map_drop_inequality(bmap, ineq2);
4461
177
  } else {
4462
177
    isl_basic_map_drop_inequality(bmap, ineq2);
4463
177
    isl_basic_map_drop_inequality(bmap, ineq1);
4464
177
  }
4465
567
  bmap = isl_basic_map_drop_div(bmap, div);
4466
567
  return drop_redundant_divs_again(bmap, pairs, 0);
4467
567
}
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
9
{
4488
9
  isl_int_neg(*l, bmap->ineq[ineq][0]);
4489
9
  isl_int_add(*l, *l, bmap->ineq[lower][0]);
4490
9
  isl_int_cdiv_q(*l, *l, bmap->ineq[ineq][pos]);
4491
9
}
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
7
{
4512
7
  isl_int_neg(*u, bmap->ineq[ineq][0]);
4513
7
  isl_int_sub(*u, *u, bmap->ineq[upper][0]);
4514
7
  isl_int_cdiv_q(*u, *u, bmap->ineq[ineq][pos]);
4515
7
}
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
97
{
4545
97
  int i;
4546
97
  int lower = -1, upper = -1;
4547
97
  unsigned o_div;
4548
97
  isl_int l, u;
4549
97
  int equal;
4550
97
4551
97
  o_div = isl_basic_map_offset(bmap, isl_dim_div);
4552
604
  for (i = 0; 
i < bmap->n_ineq && 604
(lower < 0 || 512
upper < 049
);
++i507
)
{507
4553
507
    if (i == ineq)
4554
92
      continue;
4555
415
    
if (415
!415
isl_int_is_zero415
(bmap->ineq[i][o_div + div]))
4556
118
      continue;
4557
297
    
if (297
lower < 0 &&297
4558
269
        
is_parallel_except(bmap, ineq, i, o_div + div)269
)
{13
4559
13
      lower = i;
4560
13
      continue;
4561
13
    }
4562
284
    
if (284
upper < 0 &&284
4563
271
        
is_opposite_except(bmap, ineq, i, o_div + div)271
)
{58
4564
58
      upper = i;
4565
58
    }
4566
284
  }
4567
97
4568
97
  if (
lower < 0 || 97
upper < 013
)
4569
90
    return -1;
4570
97
4571
7
  
isl_int_init7
(l);7
4572
7
  isl_int_init(u);
4573
7
4574
7
  lower_bound_from_parallel(bmap, ineq, lower, o_div + div, &l);
4575
7
  lower_bound_from_opposite(bmap, ineq, upper, o_div + div, &u);
4576
7
4577
7
  equal = isl_int_eq(l, u);
4578
7
4579
7
  isl_int_clear(l);
4580
7
  isl_int_clear(u);
4581
7
4582
5
  return equal ? 
lower2
:
-15
;
4583
97
}
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
2
{
4607
2
  isl_int c;
4608
2
  unsigned o_div;
4609
2
4610
2
  isl_int_init(c);
4611
2
4612
2
  o_div = isl_basic_map_offset(bmap, isl_dim_div);
4613
2
  lower_bound_from_parallel(bmap, ineq, lower, o_div + div, &c);
4614
2
  bmap = isl_basic_map_fix(bmap, isl_dim_div, div, c);
4615
2
  free(pairs);
4616
2
4617
2
  isl_int_clear(c);
4618
2
4619
2
  return isl_basic_map_drop_redundant_divs(bmap);
4620
2
}
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
131k
{
4667
131k
  int i, j;
4668
131k
  unsigned off;
4669
131k
  int *pairs = NULL;
4670
131k
  int n = 0;
4671
131k
4672
131k
  if (!bmap)
4673
0
    goto error;
4674
131k
  
if (131k
bmap->n_div == 0131k
)
4675
124k
    return bmap;
4676
131k
4677
7.34k
  off = isl_space_dim(bmap->dim, isl_dim_all);
4678
7.34k
  pairs = isl_calloc_array(bmap->ctx, int, bmap->n_div);
4679
7.34k
  if (!pairs)
4680
0
    goto error;
4681
7.34k
4682
13.7k
  
for (i = 0; 7.34k
i < bmap->n_div13.7k
;
++i6.37k
)
{8.94k
4683
8.94k
    int pos, neg;
4684
8.94k
    int last_pos, last_neg;
4685
8.94k
    int redundant;
4686
8.94k
    int defined;
4687
8.94k
    isl_bool opp, set_div;
4688
8.94k
4689
8.94k
    defined = !isl_int_is_zero(bmap->div[i][0]);
4690
21.1k
    for (j = i; 
j < bmap->n_div21.1k
;
++j12.2k
)
4691
12.2k
      
if (12.2k
!12.2k
isl_int_is_zero12.2k
(bmap->div[j][1 + 1 + off + i]))
4692
16
        break;
4693
8.94k
    if (j < bmap->n_div)
4694
16
      continue;
4695
14.8k
    
for (j = 0; 8.93k
j < bmap->n_eq14.8k
;
++j5.90k
)
4696
10.1k
      
if (10.1k
!10.1k
isl_int_is_zero10.1k
(bmap->eq[j][1 + off + i]))
4697
4.26k
        break;
4698
8.93k
    if (j < bmap->n_eq)
4699
4.26k
      continue;
4700
4.67k
    ++n;
4701
4.67k
    pos = neg = 0;
4702
31.7k
    for (j = 0; 
j < bmap->n_ineq31.7k
;
++j27.1k
)
{27.1k
4703
27.1k
      if (
isl_int_is_pos27.1k
(bmap->ineq[j][1 + off + i]))
{4.78k
4704
4.78k
        last_pos = j;
4705
4.78k
        ++pos;
4706
4.78k
      }
4707
27.1k
      if (
isl_int_is_neg27.1k
(bmap->ineq[j][1 + off + i]))
{4.81k
4708
4.81k
        last_neg = j;
4709
4.81k
        ++neg;
4710
4.81k
      }
4711
27.1k
    }
4712
4.67k
    pairs[i] = pos * neg;
4713
4.67k
    if (
pairs[i] == 04.67k
)
{1.09k
4714
1.16k
      for (j = bmap->n_ineq - 1; 
j >= 01.16k
;
--j72
)
4715
72
        
if (72
!72
isl_int_is_zero72
(bmap->ineq[j][1+off+i]))
4716
34
          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
3.57k
    
if (3.57k
pairs[i] != 13.57k
)
4721
1.15k
      opp = isl_bool_false;
4722
3.57k
    else
4723
2.42k
      opp = is_opposite(bmap, last_pos, last_neg);
4724
3.57k
    if (opp < 0)
4725
0
      goto error;
4726
3.57k
    
if (3.57k
!opp3.57k
)
{1.97k
4727
1.97k
      int lower;
4728
1.97k
      isl_bool single, one;
4729
1.97k
4730
1.97k
      if (pos != 1)
4731
871
        continue;
4732
1.10k
      single = single_unknown(bmap, last_pos, i);
4733
1.10k
      if (single < 0)
4734
0
        goto error;
4735
1.10k
      
if (1.10k
!single1.10k
)
4736
103
        continue;
4737
998
      one = has_coef_one(bmap, i, last_pos);
4738
998
      if (one < 0)
4739
0
        goto error;
4740
998
      
if (998
one998
)
4741
901
        return set_eq_and_try_again(bmap, last_pos,
4742
901
                  pairs);
4743
97
      lower = lower_bound_is_cst(bmap, i, last_pos);
4744
97
      if (lower >= 0)
4745
2
        return fix_cst_lower(bmap, i, last_pos, lower,
4746
2
            pairs);
4747
95
      continue;
4748
97
    }
4749
3.57k
4750
1.60k
    
isl_int_add1.60k
(bmap->ineq[last_pos][0],1.60k
4751
1.60k
          bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
4752
1.60k
    isl_int_add_ui(bmap->ineq[last_pos][0],
4753
1.60k
             bmap->ineq[last_pos][0], 1);
4754
1.60k
    redundant = isl_int_ge(bmap->ineq[last_pos][0],
4755
1.60k
        bmap->ineq[last_pos][1+off+i]);
4756
1.60k
    isl_int_sub_ui(bmap->ineq[last_pos][0],
4757
1.60k
             bmap->ineq[last_pos][0], 1);
4758
1.60k
    isl_int_sub(bmap->ineq[last_pos][0],
4759
1.60k
          bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
4760
1.60k
    if (redundant)
4761
567
      return drop_div_and_try_again(bmap, i,
4762
567
                last_pos, last_neg, pairs);
4763
1.03k
    
if (1.03k
defined1.03k
)
4764
1.03k
      set_div = isl_bool_false;
4765
1.03k
    else
4766
6
      set_div = ok_to_set_div_from_bound(bmap, i, last_pos);
4767
1.03k
    if (set_div < 0)
4768
0
      return isl_basic_map_free(bmap);
4769
1.03k
    
if (1.03k
set_div1.03k
)
{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.03k
    pairs[i] = 0;
4774
1.03k
    --n;
4775
1.03k
  }
4776
7.34k
4777
4.77k
  
if (4.77k
n > 04.77k
)
4778
738
    return coalesce_or_drop_more_redundant_divs(bmap, pairs, n);
4779
4.77k
4780
4.03k
  free(pairs);
4781
4.03k
  return bmap;
4782
0
error:
4783
0
  free(pairs);
4784
0
  isl_basic_map_free(bmap);
4785
0
  return NULL;
4786
4.77k
}
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
9.40k
{
4793
9.40k
  int n;
4794
9.40k
  isl_ctx *ctx;
4795
9.40k
  isl_vec *v;
4796
9.40k
4797
9.40k
  if (!T)
4798
0
    return isl_stat_error;
4799
9.40k
  n = isl_mat_rows(T);
4800
9.40k
  if (isl_seq_first_non_zero(c, n) == -1)
4801
4.66k
    return isl_stat_ok;
4802
4.74k
  ctx = isl_mat_get_ctx(T);
4803
4.74k
  v = isl_vec_alloc(ctx, n);
4804
4.74k
  if (!v)
4805
0
    return isl_stat_error;
4806
4.74k
  isl_seq_swp_or_cpy(v->el, c, n);
4807