Coverage Report

Created: 2017-08-21 19:50

/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
644k
{
33
644k
  isl_int *t = bmap->eq[a];
34
644k
  bmap->eq[a] = bmap->eq[b];
35
644k
  bmap->eq[b] = t;
36
644k
}
37
38
static void swap_inequality(struct isl_basic_map *bmap, int a, int b)
39
52.7k
{
40
52.7k
  if (
a != b52.7k
)
{52.7k
41
52.7k
    isl_int *t = bmap->ineq[a];
42
52.7k
    bmap->ineq[a] = bmap->ineq[b];
43
52.7k
    bmap->ineq[b] = t;
44
52.7k
  }
45
52.7k
}
46
47
__isl_give isl_basic_map *isl_basic_map_normalize_constraints(
48
  __isl_take isl_basic_map *bmap)
49
2.46M
{
50
2.46M
  int i;
51
2.46M
  isl_int gcd;
52
2.46M
  unsigned total = isl_basic_map_total_dim(bmap);
53
2.46M
54
2.46M
  if (!bmap)
55
0
    return NULL;
56
2.46M
57
2.46M
  
isl_int_init2.46M
(gcd);2.46M
58
4.95M
  for (i = bmap->n_eq - 1; 
i >= 04.95M
;
--i2.49M
)
{2.49M
59
2.49M
    isl_seq_gcd(bmap->eq[i]+1, total, &gcd);
60
2.49M
    if (
isl_int_is_zero2.49M
(gcd))
{215k
61
215k
      if (
!215k
isl_int_is_zero215k
(bmap->eq[i][0]))
{1.64k
62
1.64k
        bmap = isl_basic_map_set_to_empty(bmap);
63
1.64k
        break;
64
215k
      }
65
215k
      isl_basic_map_drop_equality(bmap, i);
66
215k
      continue;
67
2.49M
    }
68
2.27M
    
if (2.27M
ISL_F_ISSET2.27M
(bmap, ISL_BASIC_MAP_RATIONAL))
69
165k
      isl_int_gcd(gcd, gcd, bmap->eq[i][0]);
70
2.27M
    if (isl_int_is_one(gcd))
71
2.24M
      continue;
72
31.7k
    
if (31.7k
!31.7k
isl_int_is_divisible_by31.7k
(bmap->eq[i][0], gcd))
{1.50k
73
1.50k
      bmap = isl_basic_map_set_to_empty(bmap);
74
1.50k
      break;
75
31.7k
    }
76
31.7k
    isl_seq_scale_down(bmap->eq[i], bmap->eq[i], gcd, 1+total);
77
2.46M
  }
78
2.46M
79
12.1M
  for (i = bmap->n_ineq - 1; 
i >= 012.1M
;
--i9.71M
)
{9.74M
80
9.74M
    isl_seq_gcd(bmap->ineq[i]+1, total, &gcd);
81
9.74M
    if (
isl_int_is_zero9.74M
(gcd))
{417k
82
417k
      if (
isl_int_is_neg417k
(bmap->ineq[i][0]))
{25.8k
83
25.8k
        bmap = isl_basic_map_set_to_empty(bmap);
84
25.8k
        break;
85
417k
      }
86
417k
      isl_basic_map_drop_inequality(bmap, i);
87
417k
      continue;
88
9.74M
    }
89
9.32M
    
if (9.32M
ISL_F_ISSET9.32M
(bmap, ISL_BASIC_MAP_RATIONAL))
90
309k
      isl_int_gcd(gcd, gcd, bmap->ineq[i][0]);
91
9.32M
    if (isl_int_is_one(gcd))
92
9.21M
      continue;
93
109k
    
isl_int_fdiv_q109k
(bmap->ineq[i][0], bmap->ineq[i][0], gcd);109k
94
109k
    isl_seq_scale_down(bmap->ineq[i]+1, bmap->ineq[i]+1, gcd, total);
95
2.46M
  }
96
2.46M
  isl_int_clear(gcd);
97
2.46M
98
2.46M
  return bmap;
99
2.46M
}
100
101
__isl_give isl_basic_set *isl_basic_set_normalize_constraints(
102
  __isl_take isl_basic_set *bset)
103
163k
{
104
163k
  isl_basic_map *bmap = bset_to_bmap(bset);
105
163k
  return bset_from_bmap(isl_basic_map_normalize_constraints(bmap));
106
163k
}
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
8.59k
{
131
8.59k
  isl_int shift;
132
8.59k
  int add_one;
133
8.59k
134
8.59k
  isl_int_init(shift);
135
8.59k
  isl_int_fdiv_r(shift, bmap->div[div][1 + pos], bmap->div[div][0]);
136
8.59k
  isl_int_mul_ui(shift, shift, 2);
137
8.59k
  add_one = isl_int_gt(shift, bmap->div[div][0]);
138
8.59k
  isl_int_fdiv_q(shift, bmap->div[div][1 + pos], bmap->div[div][0]);
139
8.59k
  if (add_one)
140
2.25k
    isl_int_add_ui(shift, shift, 1);
141
8.59k
  isl_int_neg(shift, shift);
142
8.59k
  bmap = isl_basic_map_shift_div(bmap, div, pos, shift);
143
8.59k
  isl_int_clear(shift);
144
8.59k
145
8.59k
  return bmap;
146
8.59k
}
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
621k
{
157
621k
  isl_bool r;
158
621k
159
621k
  if (isl_int_is_zero(bmap->div[div][1 + pos]))
160
464k
    return isl_bool_false;
161
621k
162
156k
  
isl_int_mul_ui156k
(bmap->div[div][1 + pos], bmap->div[div][1 + pos], 2);156k
163
156k
  r = isl_int_abs_ge(bmap->div[div][1 + pos], bmap->div[div][0]) &&
164
43.7k
      
!43.7k
isl_int_eq43.7k
(bmap->div[div][1 + pos], bmap->div[div][0]);
165
156k
  isl_int_divexact_ui(bmap->div[div][1 + pos],
166
156k
          bmap->div[div][1 + pos], 2);
167
156k
168
621k
  return r;
169
621k
}
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
90.4k
{
179
90.4k
  int i;
180
90.4k
  unsigned total = 1 + isl_basic_map_total_dim(bmap);
181
90.4k
182
711k
  for (i = 0; 
i < total711k
;
++i621k
)
{621k
183
621k
    isl_bool reduce;
184
621k
185
621k
    reduce = needs_reduction(bmap, div, i);
186
621k
    if (reduce < 0)
187
0
      return isl_basic_map_free(bmap);
188
621k
    
if (621k
!reduce621k
)
189
612k
      continue;
190
621k
    bmap = reduce_coefficient_in_div(bmap, div, i);
191
8.59k
    if (!bmap)
192
0
      break;
193
90.4k
  }
194
90.4k
195
90.4k
  return bmap;
196
90.4k
}
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
2.27M
{
206
2.27M
  int i;
207
2.27M
208
2.27M
  if (!bmap)
209
0
    return NULL;
210
2.27M
  
if (2.27M
bmap->n_div == 02.27M
)
211
2.06M
    return bmap;
212
2.27M
213
586k
  
for (i = 0; 212k
i < bmap->n_div586k
;
++i374k
)
{374k
214
374k
    if (isl_int_is_zero(bmap->div[i][0]))
215
284k
      continue;
216
374k
    bmap = reduce_div_coefficients_of_div(bmap, i);
217
90.4k
    if (!bmap)
218
0
      break;
219
212k
  }
220
212k
221
2.27M
  return bmap;
222
2.27M
}
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
384k
{
239
384k
  unsigned total = isl_basic_map_total_dim(bmap);
240
384k
  isl_ctx *ctx = bmap->ctx;
241
384k
242
384k
  if (isl_int_is_zero(bmap->div[div][0]))
243
284k
    return;
244
384k
  isl_seq_gcd(bmap->div[div] + 2, total, &ctx->normalize_gcd);
245
100k
  isl_int_gcd(ctx->normalize_gcd, ctx->normalize_gcd, bmap->div[div][0]);
246
100k
  if (isl_int_is_one(ctx->normalize_gcd))
247
98.2k
    return;
248
2.62k
  
isl_int_fdiv_q2.62k
(bmap->div[div][1], bmap->div[div][1],2.62k
249
2.62k
      ctx->normalize_gcd);
250
2.62k
  isl_int_divexact(bmap->div[div][0], bmap->div[div][0],
251
2.62k
      ctx->normalize_gcd);
252
2.62k
  isl_seq_scale_down(bmap->div[div] + 2, bmap->div[div] + 2,
253
2.62k
      ctx->normalize_gcd, total);
254
384k
}
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
2.27M
{
272
2.27M
  int i;
273
2.27M
274
2.27M
  if (!bmap)
275
0
    return NULL;
276
2.27M
  
if (2.27M
bmap->n_div == 02.27M
)
277
2.06M
    return bmap;
278
2.27M
279
586k
  
for (i = 0; 212k
i < bmap->n_div586k
;
++i374k
)
280
374k
    normalize_div_expression(bmap, i);
281
212k
282
2.27M
  return bmap;
283
2.27M
}
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
3.45M
{
290
3.45M
  unsigned total;
291
3.45M
  unsigned space_total;
292
3.45M
  int k;
293
3.45M
  int last_div;
294
3.45M
295
3.45M
  total = isl_basic_map_total_dim(bmap);
296
3.45M
  space_total = isl_space_dim(bmap->dim, isl_dim_all);
297
3.45M
  last_div = isl_seq_last_non_zero(eq + 1 + space_total, bmap->n_div);
298
25.6M
  for (k = 0; 
k < bmap->n_eq25.6M
;
++k22.2M
)
{22.2M
299
22.2M
    if (bmap->eq[k] == eq)
300
3.45M
      continue;
301
18.7M
    
if (18.7M
isl_int_is_zero18.7M
(bmap->eq[k][1+pos]))
302
17.8M
      continue;
303
954k
    
if (954k
progress954k
)
304
182k
      *progress = 1;
305
954k
    isl_seq_elim(bmap->eq[k], eq, 1+pos, 1+total, NULL);
306
954k
    isl_seq_normalize(bmap->ctx, bmap->eq[k], 1 + total);
307
3.45M
  }
308
3.45M
309
16.9M
  for (k = 0; 
k < bmap->n_ineq16.9M
;
++k13.5M
)
{13.5M
310
13.5M
    if (isl_int_is_zero(bmap->ineq[k][1+pos]))
311
12.7M
      continue;
312
737k
    
if (737k
progress737k
)
313
267k
      *progress = 1;
314
737k
    isl_seq_elim(bmap->ineq[k], eq, 1+pos, 1+total, NULL);
315
737k
    isl_seq_normalize(bmap->ctx, bmap->ineq[k], 1 + total);
316
737k
    ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
317
3.45M
  }
318
3.45M
319
4.18M
  for (k = 0; 
k < bmap->n_div4.18M
;
++k724k
)
{724k
320
724k
    if (isl_int_is_zero(bmap->div[k][0]))
321
519k
      continue;
322
204k
    
if (204k
isl_int_is_zero204k
(bmap->div[k][1+1+pos]))
323
194k
      continue;
324
10.3k
    
if (10.3k
progress10.3k
)
325
5.09k
      *progress = 1;
326
10.3k
    /* We need to be careful about circular definitions,
327
10.3k
     * so for now we just remove the definition of div k
328
10.3k
     * if the equality contains any divs.
329
10.3k
     * If keep_divs is set, then the divs have been ordered
330
10.3k
     * and we can keep the definition as long as the result
331
10.3k
     * is still ordered.
332
10.3k
     */
333
10.3k
    if (
last_div == -1 || 10.3k
(keep_divs && 3.47k
last_div < k3.47k
))
{10.3k
334
10.3k
      isl_seq_elim(bmap->div[k]+1, eq,
335
10.3k
          1+pos, 1+total, &bmap->div[k][0]);
336
10.3k
      normalize_div_expression(bmap, k);
337
10.3k
    } else
338
0
      isl_seq_clr(bmap->div[k], 1 + total);
339
10.3k
    ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
340
3.45M
  }
341
3.45M
}
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
214k
{
348
214k
  unsigned pos = isl_space_dim(bmap->dim, isl_dim_all) + div;
349
214k
350
214k
  eliminate_var_using_equality(bmap, pos, eq, keep_divs, NULL);
351
214k
352
214k
  bmap = isl_basic_map_drop_div(bmap, div);
353
214k
354
214k
  return bmap;
355
214k
}
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
215k
{
363
215k
  int k;
364
215k
  int last_div;
365
215k
  unsigned space_total = isl_space_dim(bmap->dim, isl_dim_all);
366
215k
  unsigned pos = space_total + div;
367
215k
368
215k
  last_div = isl_seq_last_non_zero(eq + 1 + space_total, bmap->n_div);
369
215k
  if (
last_div < 0 || 215k
last_div <= div215k
)
370
209k
    return isl_bool_true;
371
215k
372
19.0k
  
for (k = 0; 6.11k
k <= last_div19.0k
;
++k12.9k
)
{18.6k
373
18.6k
    if (isl_int_is_zero(bmap->div[k][0]))
374
7.98k
      continue;
375
10.6k
    
if (10.6k
!10.6k
isl_int_is_zero10.6k
(bmap->div[k][1 + 1 + pos]))
376
5.68k
      return isl_bool_false;
377
10.6k
  }
378
6.11k
379
426
  return isl_bool_true;
380
215k
}
381
382
/* Eliminate divs based on equalities
383
 */
384
static __isl_give isl_basic_map *eliminate_divs_eq(
385
  __isl_take isl_basic_map *bmap, int *progress)
386
2.38M
{
387
2.38M
  int d;
388
2.38M
  int i;
389
2.38M
  int modified = 0;
390
2.38M
  unsigned off;
391
2.38M
392
2.38M
  bmap = isl_basic_map_order_divs(bmap);
393
2.38M
394
2.38M
  if (!bmap)
395
0
    return NULL;
396
2.38M
397
2.38M
  off = 1 + isl_space_dim(bmap->dim, isl_dim_all);
398
2.38M
399
2.78M
  for (d = bmap->n_div - 1; 
d >= 02.78M
;
--d392k
)
{392k
400
838k
    for (i = 0; 
i < bmap->n_eq838k
;
++i446k
)
{655k
401
655k
      isl_bool ok;
402
655k
403
655k
      if (
!655k
isl_int_is_one655k
(bmap->eq[i][off + d]) &&
404
482k
          
!482k
isl_int_is_negone482k
(bmap->eq[i][off + d]))
405
440k
        continue;
406
655k
      ok = ok_to_eliminate_div(bmap, bmap->eq[i], d);
407
215k
      if (ok < 0)
408
0
        return isl_basic_map_free(bmap);
409
215k
      
if (215k
!ok215k
)
410
5.68k
        continue;
411
215k
      modified = 1;
412
209k
      *progress = 1;
413
209k
      bmap = eliminate_div(bmap, bmap->eq[i], d, 1);
414
209k
      if (isl_basic_map_drop_equality(bmap, i) < 0)
415
0
        return isl_basic_map_free(bmap);
416
209k
      break;
417
392k
    }
418
2.38M
  }
419
2.38M
  
if (2.38M
modified2.38M
)
420
111k
    return eliminate_divs_eq(bmap, progress);
421
2.27M
  return bmap;
422
2.38M
}
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
2.27M
{
429
2.27M
  int d;
430
2.27M
  int i;
431
2.27M
  unsigned off;
432
2.27M
  struct isl_ctx *ctx;
433
2.27M
434
2.27M
  if (!bmap)
435
0
    return NULL;
436
2.27M
437
2.27M
  ctx = bmap->ctx;
438
2.27M
  off = 1 + isl_space_dim(bmap->dim, isl_dim_all);
439
2.27M
440
2.43M
  for (d = bmap->n_div - 1; 
d >= 02.43M
;
--d159k
)
{159k
441
261k
    for (i = 0; 
i < bmap->n_eq261k
;
++i101k
)
442
149k
      
if (149k
!149k
isl_int_is_zero149k
(bmap->eq[i][off + d]))
443
47.8k
        break;
444
159k
    if (i < bmap->n_eq)
445
47.8k
      continue;
446
412k
    
for (i = 0; 111k
i < bmap->n_ineq412k
;
++i300k
)
447
349k
      
if (349k
isl_int_abs_gt349k
(bmap->ineq[i][off + d], ctx->one))
448
49.4k
        break;
449
111k
    if (i < bmap->n_ineq)
450
49.4k
      continue;
451
111k
    *progress = 1;
452
62.4k
    bmap = isl_basic_map_eliminate_vars(bmap, (off-1)+d, 1);
453
62.4k
    if (
!bmap || 62.4k
ISL_F_ISSET62.4k
(bmap, ISL_BASIC_MAP_EMPTY))
454
107
      break;
455
62.4k
    bmap = isl_basic_map_drop_div(bmap, d);
456
62.3k
    if (!bmap)
457
0
      break;
458
2.27M
  }
459
2.27M
  return bmap;
460
2.27M
}
461
462
/* Does the equality constraint at position "eq" in "bmap" involve
463
 * any local variables in the range [first, first + n)
464
 * that are not marked as having an explicit representation?
465
 */
466
static isl_bool bmap_eq_involves_unknown_divs(__isl_keep isl_basic_map *bmap,
467
  int eq, unsigned first, unsigned n)
468
2.88k
{
469
2.88k
  unsigned o_div;
470
2.88k
  int i;
471
2.88k
472
2.88k
  if (!bmap)
473
0
    return isl_bool_error;
474
2.88k
475
2.88k
  o_div = isl_basic_map_offset(bmap, isl_dim_div);
476
4.23k
  for (i = 0; 
i < n4.23k
;
++i1.34k
)
{1.95k
477
1.95k
    isl_bool unknown;
478
1.95k
479
1.95k
    if (isl_int_is_zero(bmap->eq[eq][o_div + first + i]))
480
1.21k
      continue;
481
1.95k
    unknown = isl_basic_map_div_is_marked_unknown(bmap, first + i);
482
744
    if (unknown < 0)
483
0
      return isl_bool_error;
484
744
    
if (744
unknown744
)
485
614
      return isl_bool_true;
486
2.88k
  }
487
2.88k
488
2.27k
  return isl_bool_false;
489
2.88k
}
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
57.0k
{
517
57.0k
  unsigned total, o_div;
518
57.0k
  isl_bool involves;
519
57.0k
520
57.0k
  if (!bmap)
521
0
    return NULL;
522
57.0k
523
57.0k
  
if (57.0k
!57.0k
isl_int_is_zero57.0k
(bmap->div[div][0]))
524
54.2k
    return bmap;
525
57.0k
526
57.0k
  involves = bmap_eq_involves_unknown_divs(bmap, eq, 0, div);
527
2.88k
  if (involves < 0)
528
0
    return isl_basic_map_free(bmap);
529
2.88k
  
if (2.88k
involves2.88k
)
530
614
    return bmap;
531
2.88k
532
2.88k
  total = isl_basic_map_dim(bmap, isl_dim_all);
533
2.27k
  o_div = isl_basic_map_offset(bmap, isl_dim_div);
534
2.27k
  isl_seq_neg(bmap->div[div] + 1, bmap->eq[eq], 1 + total);
535
2.27k
  isl_int_set_si(bmap->div[div][1 + o_div + div], 0);
536
2.27k
  isl_int_set(bmap->div[div][0], bmap->eq[eq][o_div + div]);
537
2.27k
  if (progress)
538
1.81k
    *progress = 1;
539
2.27k
  ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
540
2.27k
541
2.88k
  return bmap;
542
57.0k
}
543
544
__isl_give isl_basic_map *isl_basic_map_gauss(__isl_take isl_basic_map *bmap,
545
  int *progress)
546
3.77M
{
547
3.77M
  int k;
548
3.77M
  int done;
549
3.77M
  int last_var;
550
3.77M
  unsigned total_var;
551
3.77M
  unsigned total;
552
3.77M
553
3.77M
  bmap = isl_basic_map_order_divs(bmap);
554
3.77M
555
3.77M
  if (!bmap)
556
0
    return NULL;
557
3.77M
558
3.77M
  total = isl_basic_map_total_dim(bmap);
559
3.77M
  total_var = total - bmap->n_div;
560
3.77M
561
3.77M
  last_var = total - 1;
562
7.01M
  for (done = 0; 
done < bmap->n_eq7.01M
;
++done3.24M
)
{3.46M
563
7.15M
    for (; 
last_var >= 07.15M
;
--last_var3.68M
)
{6.92M
564
25.1M
      for (k = done; 
k < bmap->n_eq25.1M
;
++k18.2M
)
565
21.4M
        
if (21.4M
!21.4M
isl_int_is_zero21.4M
(bmap->eq[k][1+last_var]))
566
3.24M
          break;
567
6.92M
      if (k < bmap->n_eq)
568
3.24M
        break;
569
6.92M
    }
570
3.46M
    if (last_var < 0)
571
225k
      break;
572
3.24M
    
if (3.24M
k != done3.24M
)
573
644k
      swap_equality(bmap, k, done);
574
3.24M
    if (isl_int_is_neg(bmap->eq[done][1+last_var]))
575
340k
      isl_seq_neg(bmap->eq[done], bmap->eq[done], 1+total);
576
3.24M
577
3.24M
    eliminate_var_using_equality(bmap, last_var, bmap->eq[done], 1,
578
3.24M
            progress);
579
3.24M
580
3.24M
    if (last_var >= total_var)
581
3.24M
      bmap = set_div_from_eq(bmap, last_var - total_var,
582
3.24M
            done, progress);
583
3.24M
    if (!bmap)
584
0
      return NULL;
585
3.77M
  }
586
3.77M
  
if (3.77M
done == bmap->n_eq3.77M
)
587
3.54M
    return bmap;
588
699k
  
for (k = done; 225k
k < bmap->n_eq699k
;
++k474k
)
{521k
589
521k
    if (isl_int_is_zero(bmap->eq[k][0]))
590
474k
      continue;
591
47.4k
    return isl_basic_map_set_to_empty(bmap);
592
521k
  }
593
225k
  isl_basic_map_free_equality(bmap, bmap->n_eq-done);
594
225k
  return bmap;
595
3.77M
}
596
597
__isl_give isl_basic_set *isl_basic_set_gauss(
598
  __isl_take isl_basic_set *bset, int *progress)
599
119k
{
600
119k
  return bset_from_bmap(isl_basic_map_gauss(bset_to_bmap(bset),
601
119k
              progress));
602
119k
}
603
604
605
static unsigned int round_up(unsigned int v)
606
1.60M
{
607
1.60M
  int old_v = v;
608
1.60M
609
4.86M
  while (
v4.86M
)
{3.25M
610
3.25M
    old_v = v;
611
3.25M
    v ^= v & -v;
612
3.25M
  }
613
1.60M
  return old_v << 1;
614
1.60M
}
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.58M
{
636
1.58M
  isl_ctx *ctx;
637
1.58M
638
1.58M
  ci->index = NULL;
639
1.58M
  if (!bmap)
640
0
    return isl_stat_error;
641
1.58M
  ci->total = isl_basic_set_total_dim(bmap);
642
1.58M
  if (bmap->n_ineq == 0)
643
0
    return isl_stat_ok;
644
1.58M
  ci->size = round_up(4 * (bmap->n_ineq + 1) / 3 - 1);
645
1.58M
  ci->bits = ffs(ci->size) - 1;
646
1.58M
  ctx = isl_basic_map_get_ctx(bmap);
647
1.58M
  ci->index = isl_calloc_array(ctx, isl_int **, ci->size);
648
1.58M
  if (!ci->index)
649
0
    return isl_stat_error;
650
1.58M
651
1.58M
  return isl_stat_ok;
652
1.58M
}
653
654
/* Free the memory allocated by create_constraint_index.
655
 */
656
static void constraint_index_free(struct isl_constraint_index *ci)
657
1.58M
{
658
1.58M
  free(ci->index);
659
1.58M
}
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
13.9M
{
669
13.9M
  int h;
670
13.9M
  uint32_t hash = isl_seq_get_hash_bits((*ineq) + 1, ci->total, ci->bits);
671
18.8M
  for (h = hash; 
ci->index[h]18.8M
;
h = (h+1) % ci->size4.86M
)
672
10.4M
    
if (10.4M
ineq != ci->index[h] &&10.4M
673
10.1M
        isl_seq_eq((*ineq) + 1, ci->index[h][0]+1, ci->total))
674
5.54M
      break;
675
13.9M
  return h;
676
13.9M
}
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
13.9M
{
688
13.9M
  return hash_index_ineq(ci, &bmap->ineq[k]);
689
13.9M
}
690
691
static int set_hash_index(struct isl_constraint_index *ci,
692
  __isl_keep isl_basic_set *bset, int k)
693
37.7k
{
694
37.7k
  return hash_index(ci, bset, k);
695
37.7k
}
696
697
/* Fill in the "ci" data structure with the inequalities of "bset".
698
 */
699
static isl_stat setup_constraint_index(struct isl_constraint_index *ci,
700
  __isl_keep isl_basic_set *bset)
701
10.9k
{
702
10.9k
  int k, h;
703
10.9k
704
10.9k
  if (create_constraint_index(ci, bset) < 0)
705
0
    return isl_stat_error;
706
10.9k
707
48.6k
  
for (k = 0; 10.9k
k < bset->n_ineq48.6k
;
++k37.7k
)
{37.7k
708
37.7k
    h = set_hash_index(ci, bset, k);
709
37.7k
    ci->index[h] = &bset->ineq[k];
710
37.7k
  }
711
10.9k
712
10.9k
  return isl_stat_ok;
713
10.9k
}
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
28.6k
{
731
28.6k
  int h;
732
28.6k
733
28.6k
  h = hash_index_ineq(ci, &ineq);
734
28.6k
  if (!ci->index[h])
735
12.2k
    return isl_bool_false;
736
16.4k
  
return 16.4k
isl_int_ge16.4k
(ineq[0], (*ci->index[h])[0]);
737
28.6k
}
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
2.27M
{
747
2.27M
  unsigned int size;
748
2.27M
  int *index;
749
2.27M
  int *elim_for;
750
2.27M
  int k, l, h;
751
2.27M
  int bits;
752
2.27M
  struct isl_blk eq;
753
2.27M
  unsigned total_var;
754
2.27M
  unsigned total;
755
2.27M
  struct isl_ctx *ctx;
756
2.27M
757
2.27M
  bmap = isl_basic_map_order_divs(bmap);
758
2.27M
  if (
!bmap || 2.27M
bmap->n_div <= 12.27M
)
759
2.19M
    return bmap;
760
2.27M
761
2.27M
  total_var = isl_space_dim(bmap->dim, isl_dim_all);
762
82.4k
  total = total_var + bmap->n_div;
763
82.4k
764
82.4k
  ctx = bmap->ctx;
765
268k
  for (k = bmap->n_div - 1; 
k >= 0268k
;
--k185k
)
766
209k
    
if (209k
!209k
isl_int_is_zero209k
(bmap->div[k][0]))
767
23.5k
      break;
768
82.4k
  if (k <= 0)
769
59.9k
    return bmap;
770
82.4k
771
82.4k
  size = round_up(4 * bmap->n_div / 3 - 1);
772
22.5k
  if (size == 0)
773
0
    return bmap;
774
22.5k
  
elim_for = 22.5k
isl_calloc_array22.5k
(ctx, int, bmap->n_div);
775
22.5k
  bits = ffs(size) - 1;
776
22.5k
  index = isl_calloc_array(ctx, int, size);
777
22.5k
  if (
!elim_for || 22.5k
!index22.5k
)
778
0
    goto out;
779
22.5k
  eq = isl_blk_alloc(ctx, 1+total);
780
22.5k
  if (isl_blk_is_error(eq))
781
0
    goto out;
782
22.5k
783
22.5k
  isl_seq_clr(eq.data, 1+total);
784
22.5k
  index[isl_seq_get_hash_bits(bmap->div[k], 2+total, bits)] = k + 1;
785
58.2k
  for (--k; 
k >= 058.2k
;
--k35.6k
)
{35.6k
786
35.6k
    uint32_t hash;
787
35.6k
788
35.6k
    if (isl_int_is_zero(bmap->div[k][0]))
789
10.2k
      continue;
790
35.6k
791
35.6k
    hash = isl_seq_get_hash_bits(bmap->div[k], 2+total, bits);
792
33.6k
    for (h = hash; 
index[h]33.6k
;
h = (h+1) % size8.14k
)
793
13.4k
      
if (13.4k
isl_seq_eq(bmap->div[k],13.4k
794
13.4k
               bmap->div[index[h]-1], 2+total))
795
5.28k
        break;
796
25.4k
    if (
index[h]25.4k
)
{5.28k
797
5.28k
      *progress = 1;
798
5.28k
      l = index[h] - 1;
799
5.28k
      elim_for[l] = k + 1;
800
25.4k
    }
801
25.4k
    index[h] = k+1;
802
25.4k
  }
803
83.6k
  for (l = bmap->n_div - 1; 
l >= 083.6k
;
--l61.1k
)
{61.1k
804
61.1k
    if (!elim_for[l])
805
55.8k
      continue;
806
61.1k
    k = elim_for[l] - 1;
807
5.28k
    isl_int_set_si(eq.data[1+total_var+k], -1);
808
5.28k
    isl_int_set_si(eq.data[1+total_var+l], 1);
809
5.28k
    bmap = eliminate_div(bmap, eq.data, l, 1);
810
5.28k
    if (!bmap)
811
0
      break;
812
5.28k
    
isl_int_set_si5.28k
(eq.data[1+total_var+k], 0);5.28k
813
5.28k
    isl_int_set_si(eq.data[1+total_var+l], 0);
814
22.5k
  }
815
22.5k
816
22.5k
  isl_blk_free(ctx, eq);
817
22.5k
out:
818
22.5k
  free(index);
819
22.5k
  free(elim_for);
820
22.5k
  return bmap;
821
2.27M
}
822
823
static int n_pure_div_eq(struct isl_basic_map *bmap)
824
23.9k
{
825
23.9k
  int i, j;
826
23.9k
  unsigned total;
827
23.9k
828
23.9k
  total = isl_space_dim(bmap->dim, isl_dim_all);
829
41.3k
  for (i = 0, j = bmap->n_div-1; 
i < bmap->n_eq41.3k
;
++i17.3k
)
{35.3k
830
60.3k
    while (
j >= 0 && 60.3k
isl_int_is_zero45.2k
(bmap->eq[i][1 + total + j]))
831
25.0k
      --j;
832
35.3k
    if (j < 0)
833
15.1k
      break;
834
20.1k
    
if (20.1k
isl_seq_first_non_zero(bmap->eq[i] + 1 + total, j) != -120.1k
)
835
2.73k
      return 0;
836
23.9k
  }
837
21.2k
  return i;
838
23.9k
}
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
2.27M
{
891
2.27M
  int i, j, k;
892
2.27M
  int total;
893
2.27M
  int div_eq;
894
2.27M
  struct isl_mat *B;
895
2.27M
  struct isl_vec *d;
896
2.27M
  struct isl_mat *T = NULL;
897
2.27M
  struct isl_mat *C = NULL;
898
2.27M
  struct isl_mat *C2 = NULL;
899
2.27M
  isl_int v;
900
2.27M
  int *pos = NULL;
901
2.27M
  int dropped, needed;
902
2.27M
903
2.27M
  if (!bmap)
904
0
    return NULL;
905
2.27M
906
2.27M
  
if (2.27M
bmap->n_div == 02.27M
)
907
2.20M
    return bmap;
908
2.27M
909
68.6k
  
if (68.6k
bmap->n_eq == 068.6k
)
910
24.5k
    return bmap;
911
68.6k
912
44.0k
  
if (44.0k
ISL_F_ISSET44.0k
(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS))
913
20.1k
    return bmap;
914
44.0k
915
44.0k
  total = isl_space_dim(bmap->dim, isl_dim_all);
916
23.9k
  div_eq = n_pure_div_eq(bmap);
917
23.9k
  if (div_eq == 0)
918
10.3k
    return bmap;
919
23.9k
920
13.6k
  
if (13.6k
div_eq < bmap->n_eq13.6k
)
{7.59k
921
7.59k
    B = isl_mat_sub_alloc6(bmap->ctx, bmap->eq, div_eq,
922
7.59k
          bmap->n_eq - div_eq, 0, 1 + total);
923
7.59k
    C = isl_mat_variable_compression(B, &C2);
924
7.59k
    if (
!C || 7.59k
!C27.59k
)
925
0
      goto error;
926
7.59k
    
if (7.59k
C->n_col == 07.59k
)
{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
7.59k
    }
932
13.6k
  }
933
13.6k
934
13.6k
  d = isl_vec_alloc(bmap->ctx, div_eq);
935
13.6k
  if (!d)
936
0
    goto error;
937
30.8k
  
for (i = 0, j = bmap->n_div-1; 13.6k
i < div_eq30.8k
;
++i17.2k
)
{17.2k
938
22.5k
    while (
j >= 0 && 22.5k
isl_int_is_zero22.5k
(bmap->eq[i][1 + total + j]))
939
5.31k
      --j;
940
17.2k
    isl_int_set(d->block.data[i], bmap->eq[i][1 + total + j]);
941
17.2k
  }
942
13.6k
  B = isl_mat_sub_alloc6(bmap->ctx, bmap->eq, 0, div_eq, 0, 1 + total);
943
13.6k
944
13.6k
  if (
C13.6k
)
{7.58k
945
7.58k
    B = isl_mat_product(B, C);
946
7.58k
    C = NULL;
947
13.6k
  }
948
13.6k
949
13.6k
  T = isl_mat_parameter_compression(B, d);
950
13.6k
  if (!T)
951
0
    goto error;
952
13.6k
  
if (13.6k
T->n_col == 013.6k
)
{1.39k
953
1.39k
    bmap = isl_basic_map_set_to_empty(bmap);
954
1.39k
    isl_mat_free(C2);
955
1.39k
    isl_mat_free(T);
956
1.39k
    goto done;
957
13.6k
  }
958
12.2k
  
isl_int_init12.2k
(v);12.2k
959
47.4k
  for (i = 0; 
i < T->n_row - 147.4k
;
++i35.2k
)
{35.2k
960
35.2k
    isl_int_fdiv_q(v, T->row[1 + i][0], T->row[1 + i][1 + i]);
961
35.2k
    if (isl_int_is_zero(v))
962
31.2k
      continue;
963
35.2k
    isl_mat_col_submul(T, 0, v, 1 + i);
964
12.2k
  }
965
12.2k
  isl_int_clear(v);
966
12.2k
  pos = isl_alloc_array(bmap->ctx, int, T->n_row);
967
12.2k
  if (!pos)
968
0
    goto error;
969
12.2k
  /* We have to be careful because dropping equalities may reorder them */
970
12.2k
  dropped = 0;
971
31.3k
  for (j = bmap->n_div - 1; 
j >= 031.3k
;
--j19.0k
)
{19.0k
972
25.8k
    for (i = 0; 
i < bmap->n_eq25.8k
;
++i6.78k
)
973
21.8k
      
if (21.8k
!21.8k
isl_int_is_zero21.8k
(bmap->eq[i][1 + total + j]))
974
15.0k
        break;
975
19.0k
    if (
i < bmap->n_eq19.0k
)
{15.0k
976
15.0k
      bmap = isl_basic_map_drop_div(bmap, j);
977
15.0k
      isl_basic_map_drop_equality(bmap, i);
978
15.0k
      ++dropped;
979
19.0k
    }
980
19.0k
  }
981
12.2k
  pos[0] = 0;
982
12.2k
  needed = 0;
983
47.4k
  for (i = 1; 
i < T->n_row47.4k
;
++i35.2k
)
{35.2k
984
35.2k
    if (isl_int_is_one(T->row[i][i]))
985
21.8k
      pos[i] = i;
986
35.2k
    else
987
13.4k
      needed++;
988
35.2k
  }
989
12.2k
  if (
needed > dropped12.2k
)
{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
12.2k
  }
995
47.4k
  
for (i = 1; 12.2k
i < T->n_row47.4k
;
++i35.2k
)
{35.2k
996
35.2k
    if (isl_int_is_one(T->row[i][i]))
997
21.8k
      continue;
998
35.2k
    k = isl_basic_map_alloc_div(bmap);
999
13.4k
    pos[i] = 1 + total + k;
1000
13.4k
    isl_seq_clr(bmap->div[k] + 1, 1 + total + bmap->n_div);
1001
13.4k
    isl_int_set(bmap->div[k][0], T->row[i][i]);
1002
13.4k
    if (C2)
1003
6.78k
      isl_seq_cpy(bmap->div[k] + 1, C2->row[i], 1 + total);
1004
13.4k
    else
1005
6.64k
      isl_int_set_si(bmap->div[k][1 + i], 1);
1006
50.7k
    for (j = 0; 
j < i50.7k
;
++j37.2k
)
{37.2k
1007
37.2k
      if (isl_int_is_zero(T->row[i][j]))
1008
27.2k
        continue;
1009
10.0k
      
if (10.0k
pos[j] < T->n_row && 10.0k
C29.95k
)
1010
10.0k
        isl_seq_submul(bmap->div[k] + 1, T->row[i][j],
1011
10.0k
            C2->row[pos[j]], 1 + total);
1012
10.0k
      else
1013
5.28k
        isl_int_neg(bmap->div[k][1 + pos[j]],
1014
10.0k
                T->row[i][j]);
1015
13.4k
    }
1016
13.4k
    j = isl_basic_map_alloc_equality(bmap);
1017
13.4k
    isl_seq_neg(bmap->eq[j], bmap->div[k]+1, 1+total+bmap->n_div);
1018
13.4k
    isl_int_set(bmap->eq[j][pos[i]], bmap->div[k][0]);
1019
13.4k
  }
1020
12.2k
  free(pos);
1021
12.2k
  isl_mat_free(C2);
1022
12.2k
  isl_mat_free(T);
1023
12.2k
1024
12.2k
  if (progress)
1025
12.2k
    *progress = 1;
1026
13.6k
done:
1027
13.6k
  ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1028
13.6k
1029
13.6k
  return bmap;
1030
12.2k
error:
1031
0
  free(pos);
1032
0
  isl_mat_free(C);
1033
0
  isl_mat_free(C2);
1034
0
  isl_mat_free(T);
1035
12.2k
  return bmap;
1036
2.27M
}
1037
1038
static __isl_give isl_basic_map *set_div_from_lower_bound(
1039
  __isl_take isl_basic_map *bmap, int div, int ineq)
1040
1.41k
{
1041
1.41k
  unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1042
1.41k
1043
1.41k
  isl_seq_neg(bmap->div[div] + 1, bmap->ineq[ineq], total + bmap->n_div);
1044
1.41k
  isl_int_set(bmap->div[div][0], bmap->ineq[ineq][total + div]);
1045
1.41k
  isl_int_add(bmap->div[div][1], bmap->div[div][1], bmap->div[div][0]);
1046
1.41k
  isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
1047
1.41k
  isl_int_set_si(bmap->div[div][1 + total + div], 0);
1048
1.41k
1049
1.41k
  return bmap;
1050
1.41k
}
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
6.50k
{
1061
6.50k
  int j;
1062
6.50k
  unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1063
6.50k
1064
6.50k
  /* Not defined in terms of unknown divs */
1065
20.7k
  for (j = 0; 
j < bmap->n_div20.7k
;
++j14.2k
)
{19.0k
1066
19.0k
    if (div == j)
1067
5.86k
      continue;
1068
13.2k
    
if (13.2k
isl_int_is_zero13.2k
(bmap->ineq[ineq][total + j]))
1069
8.25k
      continue;
1070
4.95k
    
if (4.95k
isl_int_is_zero4.95k
(bmap->div[j][0]))
1071
4.85k
      return isl_bool_false;
1072
6.50k
  }
1073
6.50k
1074
6.50k
  /* No other div defined in terms of this one => avoid loops */
1075
7.03k
  
for (j = 0; 1.65k
j < bmap->n_div7.03k
;
++j5.37k
)
{5.61k
1076
5.61k
    if (div == j)
1077
1.65k
      continue;
1078
3.96k
    
if (3.96k
isl_int_is_zero3.96k
(bmap->div[j][0]))
1079
1.25k
      continue;
1080
2.71k
    
if (2.71k
!2.71k
isl_int_is_zero2.71k
(bmap->div[j][1 + total + div]))
1081
241
      return isl_bool_false;
1082
2.71k
  }
1083
1.65k
1084
1.41k
  return isl_bool_true;
1085
6.50k
}
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
73.5k
{
1098
73.5k
  unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1099
73.5k
  int last_div;
1100
73.5k
  int last_ineq;
1101
73.5k
1102
73.5k
  if (isl_int_is_zero(bmap->div[div][0]))
1103
6.21k
    return isl_bool_true;
1104
73.5k
1105
67.3k
  
if (67.3k
isl_seq_last_non_zero(bmap->ineq[ineq] + total + div + 1,67.3k
1106
67.3k
          bmap->n_div - (div + 1)) >= 0)
1107
534
    return isl_bool_false;
1108
67.3k
1109
67.3k
  last_ineq = isl_seq_last_non_zero(bmap->ineq[ineq], total + div);
1110
66.7k
  last_div = isl_seq_last_non_zero(bmap->div[div] + 1,
1111
66.7k
           total + bmap->n_div);
1112
66.7k
1113
67.3k
  return last_ineq < last_div;
1114
73.5k
}
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
4.76M
{
1132
4.76M
  int i;
1133
4.76M
  unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1134
4.76M
1135
4.99M
  for (i = 0; 
i < bmap->n_div4.99M
;
++i233k
)
{306k
1136
306k
    isl_bool set_div;
1137
306k
1138
306k
    if (isl_int_is_zero(bmap->ineq[k][total + i]))
1139
215k
      continue;
1140
90.8k
    
if (90.8k
isl_int_abs_ge90.8k
(sum, bmap->ineq[k][total + i]))
1141
17.2k
      continue;
1142
90.8k
    set_div = better_div_constraint(bmap, i, k);
1143
73.5k
    if (
set_div >= 0 && 73.5k
set_div73.5k
)
1144
6.49k
      set_div = ok_to_set_div_from_bound(bmap, i, k);
1145
73.5k
    if (set_div < 0)
1146
0
      return isl_basic_map_free(bmap);
1147
73.5k
    
if (73.5k
!set_div73.5k
)
1148
72.1k
      break;
1149
1.40k
    
if (1.40k
isl_int_is_pos1.40k
(bmap->ineq[k][total + i]))
1150
532
      bmap = set_div_from_lower_bound(bmap, i, k);
1151
1.40k
    else
1152
877
      bmap = set_div_from_lower_bound(bmap, i, l);
1153
1.40k
    if (progress)
1154
1.40k
      *progress = 1;
1155
73.5k
    break;
1156
4.76M
  }
1157
4.76M
  return bmap;
1158
4.76M
}
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
2.30M
{
1163
2.30M
  struct isl_constraint_index ci;
1164
2.30M
  int k, l, h;
1165
2.30M
  unsigned total = isl_basic_map_total_dim(bmap);
1166
2.30M
  isl_int sum;
1167
2.30M
1168
2.30M
  if (
!bmap || 2.30M
bmap->n_ineq <= 12.30M
)
1169
730k
    return bmap;
1170
2.30M
1171
1.57M
  
if (1.57M
create_constraint_index(&ci, bmap) < 01.57M
)
1172
0
    return bmap;
1173
1.57M
1174
1.57M
  h = isl_seq_get_hash_bits(bmap->ineq[0] + 1, total, ci.bits);
1175
1.57M
  ci.index[h] = &bmap->ineq[0];
1176
9.04M
  for (k = 1; 
k < bmap->n_ineq9.04M
;
++k7.46M
)
{7.46M
1177
7.46M
    h = hash_index(&ci, bmap, k);
1178
7.46M
    if (
!ci.index[h]7.46M
)
{6.84M
1179
6.84M
      ci.index[h] = &bmap->ineq[k];
1180
6.84M
      continue;
1181
7.46M
    }
1182
625k
    
if (625k
progress625k
)
1183
618k
      *progress = 1;
1184
625k
    l = ci.index[h] - &bmap->ineq[0];
1185
625k
    if (isl_int_lt(bmap->ineq[k][0], bmap->ineq[l][0]))
1186
52.7k
      swap_inequality(bmap, k, l);
1187
625k
    isl_basic_map_drop_inequality(bmap, k);
1188
625k
    --k;
1189
1.57M
  }
1190
1.57M
  isl_int_init(sum);
1191
7.86M
  for (k = 0; 
k < bmap->n_ineq-17.86M
;
++k6.28M
)
{6.39M
1192
6.39M
    isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
1193
6.39M
    h = hash_index(&ci, bmap, k);
1194
6.39M
    isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
1195
6.39M
    if (!ci.index[h])
1196
1.49M
      continue;
1197
6.39M
    l = ci.index[h] - &bmap->ineq[0];
1198
4.90M
    isl_int_add(sum, bmap->ineq[k][0], bmap->ineq[l][0]);
1199
4.90M
    if (
isl_int_is_pos4.90M
(sum))
{4.79M
1200
4.79M
      if (detect_divs)
1201
4.79M
        bmap = check_for_div_constraints(bmap, k, l,
1202
4.79M
                 sum, progress);
1203
4.79M
      continue;
1204
4.90M
    }
1205
113k
    
if (113k
isl_int_is_zero113k
(sum))
{13.9k
1206
13.9k
      /* We need to break out of the loop after these
1207
13.9k
       * changes since the contents of the hash
1208
13.9k
       * will no longer be valid.
1209
13.9k
       * Plus, we probably we want to regauss first.
1210
13.9k
       */
1211
13.9k
      if (progress)
1212
13.8k
        *progress = 1;
1213
13.9k
      isl_basic_map_drop_inequality(bmap, l);
1214
13.9k
      isl_basic_map_inequality_to_equality(bmap, k);
1215
113k
    } else
1216
99.3k
      bmap = isl_basic_map_set_to_empty(bmap);
1217
4.90M
    break;
1218
4.90M
  }
1219
1.57M
  isl_int_clear(sum);
1220
1.57M
1221
1.57M
  constraint_index_free(&ci);
1222
1.57M
  return bmap;
1223
2.30M
}
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
290
{
1233
290
  int duplicate;
1234
290
1235
495
  do {
1236
495
    duplicate = 0;
1237
495
    bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1238
495
                &duplicate, 0);
1239
495
    if (
progress && 495
duplicate199
)
1240
56
      *progress = 1;
1241
495
  } while (duplicate);
1242
290
1243
290
  return bmap;
1244
290
}
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
2.27M
{
1287
2.27M
  int i, j;
1288
2.27M
  isl_ctx *ctx;
1289
2.27M
  unsigned total;
1290
2.27M
1291
2.27M
  if (!bmap)
1292
0
    return NULL;
1293
2.27M
1294
2.27M
  ctx = isl_basic_map_get_ctx(bmap);
1295
2.27M
  total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1296
2.27M
1297
2.64M
  for (i = 0; 
i < bmap->n_div2.64M
;
++i369k
)
{369k
1298
369k
    if (isl_int_is_zero(bmap->div[i][0]))
1299
284k
      continue;
1300
85.1k
    
if (85.1k
isl_int_is_one85.1k
(bmap->div[i][0]))
1301
424
      continue;
1302
631k
    
for (j = 0; 84.7k
j < bmap->n_ineq631k
;
++j546k
)
{546k
1303
546k
      int s;
1304
546k
1305
546k
      if (
!546k
isl_int_is_one546k
(bmap->ineq[j][total + i]) &&
1306
542k
          
!542k
isl_int_is_negone542k
(bmap->ineq[j][total + i]))
1307
539k
        continue;
1308
546k
1309
546k
      *progress = 1;
1310
7.48k
1311
7.48k
      s = isl_int_sgn(bmap->ineq[j][total + i]);
1312
7.48k
      isl_int_set_si(bmap->ineq[j][total + i], 0);
1313
7.48k
      if (s < 0)
1314
7.48k
        isl_seq_combine(bmap->ineq[j],
1315
3.67k
          ctx->negone, bmap->div[i] + 1,
1316
3.67k
          bmap->div[i][0], bmap->ineq[j],
1317
7.48k
          total + bmap->n_div);
1318
7.48k
      else
1319
7.48k
        isl_seq_combine(bmap->ineq[j],
1320
3.80k
          ctx->one, bmap->div[i] + 1,
1321
3.80k
          bmap->div[i][0], bmap->ineq[j],
1322
7.48k
          total + bmap->n_div);
1323
7.48k
      if (
s < 07.48k
)
{3.67k
1324
3.67k
        isl_int_add(bmap->ineq[j][0],
1325
3.67k
          bmap->ineq[j][0], bmap->div[i][0]);
1326
3.67k
        isl_int_sub_ui(bmap->ineq[j][0],
1327
3.67k
          bmap->ineq[j][0], 1);
1328
7.48k
      }
1329
7.48k
1330
7.48k
      bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1331
7.48k
      if (isl_basic_map_add_div_constraint(bmap, i, s) < 0)
1332
0
        return isl_basic_map_free(bmap);
1333
84.7k
    }
1334
2.27M
  }
1335
2.27M
1336
2.27M
  return bmap;
1337
2.27M
}
1338
1339
__isl_give isl_basic_map *isl_basic_map_simplify(__isl_take isl_basic_map *bmap)
1340
1.91M
{
1341
1.91M
  int progress = 1;
1342
1.91M
  if (!bmap)
1343
0
    return NULL;
1344
4.19M
  
while (1.91M
progress4.19M
)
{2.34M
1345
2.34M
    isl_bool empty;
1346
2.34M
1347
2.34M
    progress = 0;
1348
2.34M
    empty = isl_basic_map_plain_is_empty(bmap);
1349
2.34M
    if (empty < 0)
1350
0
      return isl_basic_map_free(bmap);
1351
2.34M
    
if (2.34M
empty2.34M
)
1352
67.4k
      break;
1353
2.34M
    bmap = isl_basic_map_normalize_constraints(bmap);
1354
2.27M
    bmap = reduce_div_coefficients(bmap);
1355
2.27M
    bmap = normalize_div_expressions(bmap);
1356
2.27M
    bmap = remove_duplicate_divs(bmap, &progress);
1357
2.27M
    bmap = eliminate_unit_divs(bmap, &progress);
1358
2.27M
    bmap = eliminate_divs_eq(bmap, &progress);
1359
2.27M
    bmap = eliminate_divs_ineq(bmap, &progress);
1360
2.27M
    bmap = isl_basic_map_gauss(bmap, &progress);
1361
2.27M
    /* requires equalities in normal form */
1362
2.27M
    bmap = normalize_divs(bmap, &progress);
1363
2.27M
    bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1364
2.27M
                &progress, 1);
1365
2.27M
    if (
bmap && 2.27M
progress2.27M
)
1366
2.27M
      ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1367
2.27M
  }
1368
1.91M
  return bmap;
1369
1.91M
}
1370
1371
struct isl_basic_set *isl_basic_set_simplify(struct isl_basic_set *bset)
1372
683k
{
1373
683k
  return bset_from_bmap(isl_basic_map_simplify(bset_to_bmap(bset)));
1374
683k
}
1375
1376
1377
isl_bool isl_basic_map_is_div_constraint(__isl_keep isl_basic_map *bmap,
1378
  isl_int *constraint, unsigned div)
1379
48.1k
{
1380
48.1k
  unsigned pos;
1381
48.1k
1382
48.1k
  if (!bmap)
1383
0
    return isl_bool_error;
1384
48.1k
1385
48.1k
  pos = 1 + isl_space_dim(bmap->dim, isl_dim_all) + div;
1386
48.1k
1387
48.1k
  if (
isl_int_eq48.1k
(constraint[pos], bmap->div[div][0]))
{22.7k
1388
22.7k
    int neg;
1389
22.7k
    isl_int_sub(bmap->div[div][1],
1390
22.7k
        bmap->div[div][1], bmap->div[div][0]);
1391
22.7k
    isl_int_add_ui(bmap->div[div][1], bmap->div[div][1], 1);
1392
22.7k
    neg = isl_seq_is_neg(constraint, bmap->div[div]+1, pos);
1393
22.7k
    isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
1394
22.7k
    isl_int_add(bmap->div[div][1],
1395
22.7k
        bmap->div[div][1], bmap->div[div][0]);
1396
22.7k
    if (!neg)
1397
8.21k
      return isl_bool_false;
1398
14.5k
    
if (14.5k
isl_seq_first_non_zero(constraint+pos+1,14.5k
1399
14.5k
              bmap->n_div-div-1) != -1)
1400
0
      return isl_bool_false;
1401
48.1k
  } else 
if (25.4k
isl_int_abs_eq25.4k
(constraint[pos], bmap->div[div][0]))
{20.6k
1402
20.6k
    if (!isl_seq_eq(constraint, bmap->div[div]+1, pos))
1403
9.68k
      return isl_bool_false;
1404
10.9k
    
if (10.9k
isl_seq_first_non_zero(constraint+pos+1,10.9k
1405
10.9k
              bmap->n_div-div-1) != -1)
1406
1
      return isl_bool_false;
1407
25.4k
  } else
1408
4.73k
    return isl_bool_false;
1409
48.1k
1410
25.5k
  return isl_bool_true;
1411
48.1k
}
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
91.3k
{
1430
91.3k
  int i;
1431
91.3k
  unsigned pos = 1 + isl_space_dim(bmap->dim, isl_dim_all) + div;
1432
91.3k
1433
124k
  for (i = 0; 
i < bmap->n_eq124k
;
++i32.8k
)
1434
74.7k
    
if (74.7k
!74.7k
isl_int_is_zero74.7k
(bmap->eq[i][pos]))
1435
41.9k
      return isl_bool_false;
1436
91.3k
1437
154k
  
for (i = 0; 49.3k
i < bmap->n_ineq154k
;
++i105k
)
{127k
1438
127k
    isl_bool red;
1439
127k
1440
127k
    if (isl_int_is_zero(bmap->ineq[i][pos]))
1441
80.0k
      continue;
1442
127k
    red = isl_basic_map_is_div_constraint(bmap, bmap->ineq[i], div);
1443
47.8k
    if (
red < 0 || 47.8k
!red47.8k
)
1444
22.4k
      return red;
1445
49.3k
  }
1446
49.3k
1447
56.7k
  
for (i = 0; 26.9k
i < bmap->n_div56.7k
;
++i29.7k
)
{29.7k
1448
29.7k
    if (isl_int_is_zero(bmap->div[i][0]))
1449
580
      continue;
1450
29.1k
    
if (29.1k
!29.1k
isl_int_is_zero29.1k
(bmap->div[i][1+pos]))
1451
1
      return isl_bool_false;
1452
29.1k
  }
1453
26.9k
1454
26.9k
  return isl_bool_true;
1455
91.3k
}
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
4.21M
{
1466
4.21M
  int i;
1467
4.21M
1468
4.21M
  if (!bmap)
1469
0
    return NULL;
1470
4.21M
1471
4.30M
  
for (i = bmap->n_div-1; 4.21M
i >= 04.30M
;
--i91.3k
)
{91.3k
1472
91.3k
    isl_bool redundant;
1473
91.3k
1474
91.3k
    redundant = div_is_redundant(bmap, i);
1475
91.3k
    if (redundant < 0)
1476
0
      return isl_basic_map_free(bmap);
1477
91.3k
    
if (91.3k
!redundant91.3k
)
1478
64.3k
      continue;
1479
91.3k
    bmap = isl_basic_map_drop_div(bmap, i);
1480
4.21M
  }
1481
4.21M
  return bmap;
1482
4.21M
}
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
4.22M
{
1491
4.22M
  if (!bmap)
1492
0
    return NULL;
1493
4.22M
  
ISL_F_SET4.22M
(bmap, ISL_BASIC_SET_FINAL);4.22M
1494
4.22M
  return bmap;
1495
4.22M
}
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
4.21M
{
1501
4.21M
  bmap = remove_redundant_divs(bmap);
1502
4.21M
  bmap = isl_basic_map_mark_final(bmap);
1503
4.21M
  return bmap;
1504
4.21M
}
1505
1506
struct isl_basic_set *isl_basic_set_finalize(struct isl_basic_set *bset)
1507
783k
{
1508
783k
  return bset_from_bmap(isl_basic_map_finalize(bset_to_bmap(bset)));
1509
783k
}
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
87.4k
{
1518
87.4k
  int i;
1519
87.4k
1520
87.4k
  if (!bmap)
1521
0
    return NULL;
1522
87.4k
1523
180k
  
for (i = 0; 87.4k
i < bmap->n_div180k
;
++i93.4k
)
{93.4k
1524
93.4k
    if (isl_int_is_zero(bmap->div[i][0]))
1525
91.0k
      continue;
1526
2.32k
    
if (2.32k
isl_int_is_zero2.32k
(bmap->div[i][1+1+pos]))
1527
2.25k
      continue;
1528
2.32k
    bmap = isl_basic_map_mark_div_unknown(bmap, i);
1529
64
    if (!bmap)
1530
0
      return NULL;
1531
87.4k
  }
1532
87.4k
  return bmap;
1533
87.4k
}
1534
1535
/* Eliminate the specified variables from the constraints using
1536
 * Fourier-Motzkin.  The variables themselves are not removed.
1537
 */
1538
__isl_give isl_basic_map *isl_basic_map_eliminate_vars(
1539
  __isl_take isl_basic_map *bmap, unsigned pos, unsigned n)
1540
87.8k
{
1541
87.8k
  int d;
1542
87.8k
  int i, j, k;
1543
87.8k
  unsigned total;
1544
87.8k
  int need_gauss = 0;
1545
87.8k
1546
87.8k
  if (n == 0)
1547
2.05k
    return bmap;
1548
85.7k
  
if (85.7k
!bmap85.7k
)
1549
0
    return NULL;
1550
85.7k
  total = isl_basic_map_total_dim(bmap);
1551
85.7k
1552
85.7k
  bmap = isl_basic_map_cow(bmap);
1553
173k
  for (d = pos + n - 1; 
d >= 0 && 173k
d >= pos156k
;
--d87.4k
)
1554
87.4k
    bmap = remove_dependent_vars(bmap, d);
1555
85.7k
  if (!bmap)
1556
0
    return NULL;
1557
85.7k
1558
85.7k
  for (d = pos + n - 1;
1559
150k
       
d >= 0 && 150k
d >= total - bmap->n_div139k
&&
d >= pos80.7k
;
--d64.3k
)
1560
64.3k
    isl_seq_clr(bmap->div[d-(total-bmap->n_div)], 2+total);
1561
173k
  for (d = pos + n - 1; 
d >= 0 && 173k
d >= pos156k
;
--d87.3k
)
{87.4k
1562
87.4k
    int n_lower, n_upper;
1563
87.4k
    if (!bmap)
1564
0
      return NULL;
1565
116k
    
for (i = 0; 87.4k
i < bmap->n_eq116k
;
++i28.7k
)
{31.6k
1566
31.6k
      if (isl_int_is_zero(bmap->eq[i][1+d]))
1567
28.7k
        continue;
1568
31.6k
      eliminate_var_using_equality(bmap, d, bmap->eq[i], 0, NULL);
1569
2.93k
      isl_basic_map_drop_equality(bmap, i);
1570
2.93k
      need_gauss = 1;
1571
31.6k
      break;
1572
87.4k
    }
1573
87.4k
    if (i < bmap->n_eq)
1574
1.06k
      continue;
1575
87.4k
    n_lower = 0;
1576
86.3k
    n_upper = 0;
1577
276k
    for (i = 0; 
i < bmap->n_ineq276k
;
++i190k
)
{190k
1578
190k
      if (isl_int_is_pos(bmap->ineq[i][1+d]))
1579
31.1k
        n_lower++;
1580
159k
      else 
if (159k
isl_int_is_neg159k
(bmap->ineq[i][1+d]))
1581
29.6k
        n_upper++;
1582
190k
    }
1583
86.3k
    bmap = isl_basic_map_extend_constraints(bmap,
1584
86.3k
        0, n_lower * n_upper);
1585
86.3k
    if (!bmap)
1586
0
      goto error;
1587
227k
    
for (i = bmap->n_ineq - 1; 86.3k
i >= 0227k
;
--i141k
)
{141k
1588
141k
      int last;
1589
141k
      if (isl_int_is_zero(bmap->ineq[i][1+d]))
1590
80.4k
        continue;
1591
141k
      last = -1;
1592
230k
      for (j = 0; 
j < i230k
;
++j169k
)
{169k
1593
169k
        if (isl_int_is_zero(bmap->ineq[j][1+d]))
1594
117k
          continue;
1595
169k
        last = j;
1596
51.5k
        if (
isl_int_sgn51.5k
(bmap->ineq[i][1+d]) ==51.5k
1597
51.5k
            isl_int_sgn(bmap->ineq[j][1+d]))
1598
12.0k
          continue;
1599
51.5k
        k = isl_basic_map_alloc_inequality(bmap);
1600
39.4k
        if (k < 0)
1601
0
          goto error;
1602
39.4k
        isl_seq_cpy(bmap->ineq[k], bmap->ineq[i],
1603
39.4k
            1+total);
1604
39.4k
        isl_seq_elim(bmap->ineq[k], bmap->ineq[j],
1605
39.4k
            1+d, 1+total, NULL);
1606
60.8k
      }
1607
60.8k
      isl_basic_map_drop_inequality(bmap, i);
1608
60.8k
      i = last + 1;
1609
86.3k
    }
1610
86.3k
    
if (86.3k
n_lower > 0 && 86.3k
n_upper > 026.3k
)
{24.9k
1611
24.9k
      bmap = isl_basic_map_normalize_constraints(bmap);
1612
24.9k
      bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1613
24.9k
                    NULL, 0);
1614
24.9k
      bmap = isl_basic_map_gauss(bmap, NULL);
1615
24.9k
      bmap = isl_basic_map_remove_redundancies(bmap);
1616
24.9k
      need_gauss = 0;
1617
24.9k
      if (!bmap)
1618
0
        goto error;
1619
24.9k
      
if (24.9k
ISL_F_ISSET24.9k
(bmap, ISL_BASIC_MAP_EMPTY))
1620
138
        break;
1621
86.3k
    }
1622
86.3k
  }
1623
85.7k
  
ISL_F_CLR85.7k
(bmap, ISL_BASIC_MAP_NORMALIZED);85.7k
1624
85.7k
  if (need_gauss)
1625
1.46k
    bmap = isl_basic_map_gauss(bmap, NULL);
1626
85.7k
  return bmap;
1627
85.7k
error:
1628
0
  isl_basic_map_free(bmap);
1629
85.7k
  return NULL;
1630
87.8k
}
1631
1632
struct isl_basic_set *isl_basic_set_eliminate_vars(
1633
  struct isl_basic_set *bset, unsigned pos, unsigned n)
1634
20.7k
{
1635
20.7k
  return bset_from_bmap(isl_basic_map_eliminate_vars(bset_to_bmap(bset),
1636
20.7k
                pos, n));
1637
20.7k
}
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
13.0k
{
1648
13.0k
  isl_space *space;
1649
13.0k
1650
13.0k
  if (!bmap)
1651
0
    return NULL;
1652
13.0k
  
if (13.0k
n == 013.0k
)
1653
0
    return bmap;
1654
13.0k
1655
13.0k
  
if (13.0k
first + n > isl_basic_map_dim(bmap, type) || 13.0k
first + n < first13.0k
)
1656
0
    isl_die(bmap->ctx, isl_error_invalid,
1657
13.0k
      "index out of bounds", goto error);
1658
13.0k
1659
13.0k
  
if (13.0k
ISL_F_ISSET13.0k
(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
13.0k
  }
1664
13.0k
1665
13.0k
  space = isl_basic_map_get_space(bmap);
1666
13.0k
  bmap = isl_basic_map_project_out(bmap, type, first, n);
1667
13.0k
  bmap = isl_basic_map_insert_dims(bmap, type, first, n);
1668
13.0k
  bmap = isl_basic_map_reset_space(bmap, space);
1669
13.0k
  return bmap;
1670
13.0k
error:
1671
0
  isl_basic_map_free(bmap);
1672
13.0k
  return NULL;
1673
13.0k
}
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
4.16k
{
1679
4.16k
  return isl_basic_map_eliminate(bset, type, first, n);
1680
4.16k
}
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
7.84k
{
1696
7.84k
  isl_bool known;
1697
7.84k
  int i, n_div, o_div;
1698
7.84k
1699
7.84k
  known = isl_basic_map_divs_known(bmap);
1700
7.84k
  if (known < 0)
1701
0
    return isl_basic_map_free(bmap);
1702
7.84k
  
if (7.84k
known7.84k
)
1703
7.84k
    return bmap;
1704
7.84k
1705
7.84k
  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
7.84k
}
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
1.28k
{
1735
1.28k
  int i;
1736
1.28k
  isl_bool known;
1737
1.28k
1738
1.28k
  known = isl_map_divs_known(map);
1739
1.28k
  if (known < 0)
1740
0
    return isl_map_free(map);
1741
1.28k
  
if (1.28k
known1.28k
)
1742
1.28k
    return map;
1743
1.28k
1744
1.28k
  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
1.28k
}
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
23.2k
{
1767
23.2k
  int d, i;
1768
23.2k
  unsigned total;
1769
23.2k
1770
23.2k
  total = isl_space_dim(bmap->dim, isl_dim_all);
1771
193k
  for (d = 0; 
d < total193k
;
++d170k
)
1772
170k
    elim[d] = -1;
1773
51.0k
  for (i = 0; 
i < bmap->n_eq51.0k
;
++i27.7k
)
{27.7k
1774
92.3k
    for (d = total - 1; 
d >= 092.3k
;
--d64.6k
)
{92.3k
1775
92.3k
      if (isl_int_is_zero(bmap->eq[i][1+d]))
1776
64.6k
        continue;
1777
92.3k
      elim[d] = i;
1778
92.3k
      break;
1779
92.3k
    }
1780
27.7k
  }
1781
23.2k
}
1782
1783
static void set_compute_elimination_index(__isl_keep isl_basic_set *bset,
1784
  int *elim)
1785
308
{
1786
308
  compute_elimination_index(bset_to_bmap(bset), elim);
1787
308
}
1788
1789
static int reduced_using_equalities(isl_int *dst, isl_int *src,
1790
  __isl_keep isl_basic_map *bmap, int *elim)
1791
194k
{
1792
194k
  int d;
1793
194k
  int copied = 0;
1794
194k
  unsigned total;
1795
194k
1796
194k
  total = isl_space_dim(bmap->dim, isl_dim_all);
1797
1.84M
  for (d = total - 1; 
d >= 01.84M
;
--d1.65M
)
{1.65M
1798
1.65M
    if (isl_int_is_zero(src[1+d]))
1799
1.42M
      continue;
1800
222k
    
if (222k
elim[d] == -1222k
)
1801
203k
      continue;
1802
18.2k
    
if (18.2k
!copied18.2k
)
{17.4k
1803
17.4k
      isl_seq_cpy(dst, src, 1 + total);
1804
17.4k
      copied = 1;
1805
18.2k
    }
1806
18.2k
    isl_seq_elim(dst, bmap->eq[elim[d]], 1 + d, 1 + total, NULL);
1807
194k
  }
1808
194k
  return copied;
1809
194k
}
1810
1811
static int set_reduced_using_equalities(isl_int *dst, isl_int *src,
1812
  __isl_keep isl_basic_set *bset, int *elim)
1813
709
{
1814
709
  return reduced_using_equalities(dst, src,
1815
709
          bset_to_bmap(bset), elim);
1816
709
}
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
8.12k
{
1821
8.12k
  int i;
1822
8.12k
  int *elim;
1823
8.12k
1824
8.12k
  if (
!bset || 8.12k
!context8.12k
)
1825
0
    goto error;
1826
8.12k
1827
8.12k
  
if (8.12k
context->n_eq == 08.12k
)
{7.81k
1828
7.81k
    isl_basic_set_free(context);
1829
7.81k
    return bset;
1830
8.12k
  }
1831
8.12k
1832
8.12k
  bset = isl_basic_set_cow(bset);
1833
308
  if (!bset)
1834
0
    goto error;
1835
308
1836
308
  
elim = 308
isl_alloc_array308
(bset->ctx, int, isl_basic_set_n_dim(bset));
1837
308
  if (!elim)
1838
0
    goto error;
1839
308
  set_compute_elimination_index(context, elim);
1840
885
  for (i = 0; 
i < bset->n_eq885
;
++i577
)
1841
577
    set_reduced_using_equalities(bset->eq[i], bset->eq[i],
1842
577
              context, elim);
1843
440
  for (i = 0; 
i < bset->n_ineq440
;
++i132
)
1844
308
    set_reduced_using_equalities(bset->ineq[i], bset->ineq[i],
1845
308
              context, elim);
1846
308
  isl_basic_set_free(context);
1847
308
  free(elim);
1848
308
  bset = isl_basic_set_simplify(bset);
1849
308
  bset = isl_basic_set_finalize(bset);
1850
308
  return bset;
1851
308
error:
1852
0
  isl_basic_set_free(bset);
1853
0
  isl_basic_set_free(context);
1854
308
  return NULL;
1855
8.12k
}
1856
1857
/* For each inequality in "ineq" that is a shifted (more relaxed)
1858
 * copy of an inequality in "context", mark the corresponding entry
1859
 * in "row" with -1.
1860
 * If an inequality only has a non-negative constant term, then
1861
 * mark it as well.
1862
 */
1863
static isl_stat mark_shifted_constraints(__isl_keep isl_mat *ineq,
1864
  __isl_keep isl_basic_set *context, int *row)
1865
10.9k
{
1866
10.9k
  struct isl_constraint_index ci;
1867
10.9k
  int n_ineq;
1868
10.9k
  unsigned total;
1869
10.9k
  int k;
1870
10.9k
1871
10.9k
  if (
!ineq || 10.9k
!context10.9k
)
1872
0
    return isl_stat_error;
1873
10.9k
  
if (10.9k
context->n_ineq == 010.9k
)
1874
0
    return isl_stat_ok;
1875
10.9k
  
if (10.9k
setup_constraint_index(&ci, context) < 010.9k
)
1876
0
    return isl_stat_error;
1877
10.9k
1878
10.9k
  n_ineq = isl_mat_rows(ineq);
1879
10.9k
  total = isl_mat_cols(ineq) - 1;
1880
39.4k
  for (k = 0; 
k < n_ineq39.4k
;
++k28.5k
)
{28.5k
1881
28.5k
    int l;
1882
28.5k
    isl_bool redundant;
1883
28.5k
1884
28.5k
    l = isl_seq_first_non_zero(ineq->row[k] + 1, total);
1885
28.5k
    if (
l < 0 && 28.5k
isl_int_is_nonneg4
(ineq->row[k][0]))
{0
1886
0
      row[k] = -1;
1887
0
      continue;
1888
28.5k
    }
1889
28.5k
    redundant = constraint_index_is_redundant(&ci, ineq->row[k]);
1890
28.5k
    if (redundant < 0)
1891
0
      goto error;
1892
28.5k
    
if (28.5k
!redundant28.5k
)
1893
18.9k
      continue;
1894
28.5k
    row[k] = -1;
1895
10.9k
  }
1896
10.9k
  constraint_index_free(&ci);
1897
10.9k
  return isl_stat_ok;
1898
10.9k
error:
1899
0
  constraint_index_free(&ci);
1900
10.9k
  return isl_stat_error;
1901
10.9k
}
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
25
{
1906
25
  struct isl_constraint_index ci;
1907
25
  int k;
1908
25
1909
25
  if (
!bset || 25
!context25
)
1910
0
    return bset;
1911
25
1912
25
  
if (25
context->n_ineq == 025
)
1913
0
    return bset;
1914
25
  
if (25
setup_constraint_index(&ci, context) < 025
)
1915
0
    return bset;
1916
25
1917
161
  
for (k = 0; 25
k < bset->n_ineq161
;
++k136
)
{136
1918
136
    isl_bool redundant;
1919
136
1920
136
    redundant = constraint_index_is_redundant(&ci, bset->ineq[k]);
1921
136
    if (redundant < 0)
1922
0
      goto error;
1923
136
    
if (136
!redundant136
)
1924
44
      continue;
1925
136
    bset = isl_basic_set_cow(bset);
1926
92
    if (!bset)
1927
0
      goto error;
1928
92
    isl_basic_set_drop_inequality(bset, k);
1929
92
    --k;
1930
92
  }
1931
25
  constraint_index_free(&ci);
1932
25
  return bset;
1933
25
error:
1934
0
  constraint_index_free(&ci);
1935
25
  return bset;
1936
25
}
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
436
{
1947
436
  isl_basic_set *bset, *bset_context;
1948
436
1949
436
  if (
!bmap || 436
!context436
)
1950
0
    goto error;
1951
436
1952
436
  
if (436
bmap->n_ineq == 0 || 436
context->n_ineq == 025
)
{411
1953
411
    isl_basic_map_free(context);
1954
411
    return bmap;
1955
436
  }
1956
436
1957
436
  context = isl_basic_map_align_divs(context, bmap);
1958
25
  bmap = isl_basic_map_align_divs(bmap, context);
1959
25
1960
25
  bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
1961
25
  bset_context = isl_basic_map_underlying_set(context);
1962
25
  bset = remove_shifted_constraints(bset, bset_context);
1963
25
  isl_basic_set_free(bset_context);
1964
25
1965
25
  bmap = isl_basic_map_overlying_set(bset, bmap);
1966
25
1967
436
  return bmap;
1968
436
error:
1969
0
  isl_basic_map_free(bmap);
1970
0
  isl_basic_map_free(context);
1971
436
  return NULL;
1972
436
}
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
188k
{
1979
188k
  int i;
1980
188k
1981
2.57M
  for (i = 0; 
i < len2.57M
;
++i2.38M
)
{2.46M
1982
2.46M
    if (!relevant[i])
1983
1.95M
      continue;
1984
512k
    
if (512k
!512k
isl_int_is_zero512k
(c[i]))
1985
79.5k
      return 1;
1986
512k
  }
1987
188k
1988
109k
  return 0;
1989
188k
}
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
58.5k
{
1997
58.5k
  int i, dim;
1998
58.5k
1999
58.5k
  dim = isl_basic_map_dim(bmap, isl_dim_all);
2000
281k
  for (i = 0; 
i < dim281k
;
++i223k
)
2001
255k
    
if (255k
!relevant[i]255k
)
2002
32.4k
      break;
2003
58.5k
  if (i >= dim)
2004
26.1k
    return bmap;
2005
58.5k
2006
55.4k
  
for (i = bmap->n_eq - 1; 32.4k
i >= 055.4k
;
--i22.9k
)
2007
22.9k
    
if (22.9k
!is_related(bmap->eq[i] + 1, dim, relevant)22.9k
)
{10.2k
2008
10.2k
      bmap = isl_basic_map_cow(bmap);
2009
10.2k
      if (isl_basic_map_drop_equality(bmap, i) < 0)
2010
0
        return isl_basic_map_free(bmap);
2011
32.4k
    }
2012
32.4k
2013
198k
  
for (i = bmap->n_ineq - 1; 32.4k
i >= 0198k
;
--i165k
)
2014
165k
    
if (165k
!is_related(bmap->ineq[i] + 1, dim, relevant)165k
)
{98.7k
2015
98.7k
      bmap = isl_basic_map_cow(bmap);
2016
98.7k
      if (isl_basic_map_drop_inequality(bmap, i) < 0)
2017
0
        return isl_basic_map_free(bmap);
2018
98.7k
    }
2019
32.4k
2020
32.4k
  return bmap;
2021
58.5k
}
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
419k
{
2037
419k
  int j;
2038
419k
  int min = dim;
2039
419k
2040
5.62M
  for (j = 0; 
j < dim5.62M
;
++j5.20M
)
{5.20M
2041
5.20M
    if (isl_int_is_zero(c[j]))
2042
4.64M
      continue;
2043
562k
    
while (562k
group[j] >= 0 && 562k
group[group[j]] != group[j]182k
)
2044
116
      group[j] = group[group[j]];
2045
562k
    if (group[j] == min)
2046
83.7k
      continue;
2047
478k
    
if (478k
group[j] < min478k
)
{426k
2048
426k
      if (
min >= 0 && 426k
min < dim426k
)
2049
6.62k
        group[min] = group[j];
2050
426k
      min = group[j];
2051
478k
    } else
2052
52.1k
      group[group[j]] = min;
2053
478k
  }
2054
419k
}
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
26.0k
{
2061
26.0k
  isl_ctx *ctx;
2062
26.0k
  int dim;
2063
26.0k
2064
26.0k
  dim = isl_basic_set_dim(context, isl_dim_set);
2065
26.0k
  ctx = isl_basic_set_get_ctx(context);
2066
26.0k
  return isl_calloc_array(ctx, int, dim);
2067
26.0k
}
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
70.7k
{
2092
70.7k
  int dim;
2093
70.7k
  int i;
2094
70.7k
  int last;
2095
70.7k
2096
70.7k
  if (!bmap)
2097
0
    return NULL;
2098
70.7k
2099
70.7k
  dim = isl_basic_map_dim(bmap, isl_dim_all);
2100
70.7k
2101
70.7k
  last = -1;
2102
445k
  for (i = 0; 
i < dim445k
;
++i374k
)
2103
374k
    
if (374k
group[i] >= 0374k
)
2104
150k
      last = group[i] = i;
2105
70.7k
  if (
last < 070.7k
)
{12.2k
2106
12.2k
    free(group);
2107
12.2k
    return bmap;
2108
70.7k
  }
2109
70.7k
2110
139k
  
for (i = 0; 58.5k
i < bmap->n_eq139k
;
++i80.5k
)
2111
80.5k
    update_groups(dim, group, bmap->eq[i] + 1);
2112
397k
  for (i = 0; 
i < bmap->n_ineq397k
;
++i338k
)
2113
338k
    update_groups(dim, group, bmap->ineq[i] + 1);
2114
58.5k
2115
404k
  for (i = 0; 
i < dim404k
;
++i345k
)
2116
345k
    
if (345k
group[i] >= 0345k
)
2117
93.8k
      group[i] = group[group[i]];
2118
58.5k
2119
404k
  for (i = 0; 
i < dim404k
;
++i345k
)
2120
345k
    group[i] = group[i] == -1;
2121
58.5k
2122
58.5k
  bmap = drop_unrelated_constraints(bmap, group);
2123
58.5k
2124
58.5k
  free(group);
2125
70.7k
  return bmap;
2126
70.7k
}
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
16.6k
{
2141
16.6k
  int *group;
2142
16.6k
  int dim;
2143
16.6k
  int i, j;
2144
16.6k
2145
16.6k
  if (
!context || 16.6k
!bset16.6k
)
2146
0
    return isl_basic_set_free(context);
2147
16.6k
2148
16.6k
  group = alloc_groups(context);
2149
16.6k
2150
16.6k
  if (!group)
2151
0
    return isl_basic_set_free(context);
2152
16.6k
2153
16.6k
  dim = isl_basic_set_dim(bset, isl_dim_set);
2154
102k
  for (i = 0; 
i < dim102k
;
++i85.5k
)
{85.5k
2155
140k
    for (j = 0; 
j < bset->n_eq140k
;
++j54.5k
)
2156
75.7k
      
if (75.7k
!75.7k
isl_int_is_zero75.7k
(bset->eq[j][1 + i]))
2157
21.2k
        break;
2158
85.5k
    if (
j < bset->n_eq85.5k
)
{21.2k
2159
21.2k
      group[i] = -1;
2160
21.2k
      continue;
2161
85.5k
    }
2162
196k
    
for (j = 0; 64.3k
j < bset->n_ineq196k
;
++j131k
)
2163
153k
      
if (153k
!153k
isl_int_is_zero153k
(bset->ineq[j][1 + i]))
2164
21.4k
        break;
2165
64.3k
    if (j < bset->n_ineq)
2166
21.4k
      group[i] = -1;
2167
64.3k
  }
2168
16.6k
2169
16.6k
  return isl_basic_map_drop_unrelated_constraints(context, group);
2170
16.6k
}
2171
2172
/* Drop constraints from "context" that are irrelevant for computing
2173
 * the gist of the inequalities "ineq".
2174
 * Inequalities in "ineq" for which the corresponding element of row
2175
 * is set to -1 have already been marked for removal and should be ignored.
2176
 *
2177
 * In particular, drop constraints in variables that are not related
2178
 * to any of the variables involved in "ineq"
2179
 * in the sense that there is no sequence of constraints that connects them.
2180
 *
2181
 * We first mark all variables that appear in "bset" as belonging
2182
 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2183
 */
2184
static __isl_give isl_basic_set *drop_irrelevant_constraints_marked(
2185
  __isl_take isl_basic_set *context, __isl_keep isl_mat *ineq, int *row)
2186
9.41k
{
2187
9.41k
  int *group;
2188
9.41k
  int dim;
2189
9.41k
  int i, j, n;
2190
9.41k
2191
9.41k
  if (
!context || 9.41k
!ineq9.41k
)
2192
0
    return isl_basic_set_free(context);
2193
9.41k
2194
9.41k
  group = alloc_groups(context);
2195
9.41k
2196
9.41k
  if (!group)
2197
0
    return isl_basic_set_free(context);
2198
9.41k
2199
9.41k
  dim = isl_basic_set_dim(context, isl_dim_set);
2200
9.41k
  n = isl_mat_rows(ineq);
2201
56.1k
  for (i = 0; 
i < dim56.1k
;
++i46.7k
)
{46.7k
2202
182k
    for (j = 0; 
j < n182k
;
++j136k
)
{154k
2203
154k
      if (row[j] < 0)
2204
30.5k
        continue;
2205
124k
      
if (124k
!124k
isl_int_is_zero124k
(ineq->row[j][1 + i]))
2206
18.7k
        break;
2207
124k
    }
2208
46.7k
    if (j < n)
2209
18.7k
      group[i] = -1;
2210
46.7k
  }
2211
9.41k
2212
9.41k
  return isl_basic_map_drop_unrelated_constraints(context, group);
2213
9.41k
}
2214
2215
/* Do all "n" entries of "row" contain a negative value?
2216
 */
2217
static int all_neg(int *row, int n)
2218
10.9k
{
2219
10.9k
  int i;
2220
10.9k
2221
18.6k
  for (i = 0; 
i < n18.6k
;
++i7.72k
)
2222
17.1k
    
if (17.1k
row[i] >= 017.1k
)
2223
9.41k
      return 0;
2224
10.9k
2225
1.49k
  return 1;
2226
10.9k
}
2227
2228
/* Update the inequalities in "bset" based on the information in "row"
2229
 * and "tab".
2230
 *
2231
 * In particular, the array "row" contains either -1, meaning that
2232
 * the corresponding inequality of "bset" is redundant, or the index
2233
 * of an inequality in "tab".
2234
 *
2235
 * If the row entry is -1, then drop the inequality.
2236
 * Otherwise, if the constraint is marked redundant in the tableau,
2237
 * then drop the inequality.  Similarly, if it is marked as an equality
2238
 * in the tableau, then turn the inequality into an equality and
2239
 * perform Gaussian elimination.
2240
 */
2241
static __isl_give isl_basic_set *update_ineq(__isl_take isl_basic_set *bset,
2242
  __isl_keep int *row, struct isl_tab *tab)
2243
10.9k
{
2244
10.9k
  int i;
2245
10.9k
  unsigned n_ineq;
2246
10.9k
  unsigned n_eq;
2247
10.9k
  int found_equality = 0;
2248
10.9k
2249
10.9k
  if (!bset)
2250
0
    return NULL;
2251
10.9k
  
if (10.9k
tab && 10.9k
tab->empty9.16k
)
2252
2.44k
    return isl_basic_set_set_to_empty(bset);
2253
10.9k
2254
10.9k
  n_ineq = bset->n_ineq;
2255
31.0k
  for (i = n_ineq - 1; 
i >= 031.0k
;
--i22.5k
)
{22.5k
2256
22.5k
    if (
row[i] < 022.5k
)
{9.06k
2257
9.06k
      if (isl_basic_set_drop_inequality(bset, i) < 0)
2258
0
        return isl_basic_set_free(bset);
2259
9.06k
      continue;
2260
22.5k
    }
2261
13.4k
    
if (13.4k
!tab13.4k
)
2262
461
      continue;
2263
13.4k
    n_eq = tab->n_eq;
2264
13.0k
    if (
isl_tab_is_equality(tab, n_eq + row[i])13.0k
)
{169
2265
169
      isl_basic_map_inequality_to_equality(bset, i);
2266
169
      found_equality = 1;
2267
13.0k
    } else 
if (12.8k
isl_tab_is_redundant(tab, n_eq + row[i])12.8k
)
{271
2268
271
      if (isl_basic_set_drop_inequality(bset, i) < 0)
2269
0
        return isl_basic_set_free(bset);
2270
13.0k
    }
2271
13.0k
  }
2272
8.47k
2273
8.47k
  
if (8.47k
found_equality8.47k
)
2274
164
    bset = isl_basic_set_gauss(bset, NULL);
2275
8.47k
  bset = isl_basic_set_finalize(bset);
2276
8.47k
  return bset;
2277
10.9k
}
2278
2279
/* Update the inequalities in "bset" based on the information in "row"
2280
 * and "tab" and free all arguments (other than "bset").
2281
 */
2282
static __isl_give isl_basic_set *update_ineq_free(
2283
  __isl_take isl_basic_set *bset, __isl_take isl_mat *ineq,
2284
  __isl_take isl_basic_set *context, __isl_take int *row,
2285
  struct isl_tab *tab)
2286
10.9k
{
2287
10.9k
  isl_mat_free(ineq);
2288
10.9k
  isl_basic_set_free(context);
2289
10.9k
2290
10.9k
  bset = update_ineq(bset, row, tab);
2291
10.9k
2292
10.9k
  free(row);
2293
10.9k
  isl_tab_free(tab);
2294
10.9k
  return bset;
2295
10.9k
}
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
16.6k
{
2332
16.6k
  int i, r;
2333
16.6k
  int *row = NULL;
2334
16.6k
  isl_ctx *ctx;
2335
16.6k
  isl_basic_set *combined = NULL;
2336
16.6k
  struct isl_tab *tab = NULL;
2337
16.6k
  unsigned n_eq, context_ineq;
2338
16.6k
2339
16.6k
  if (
!bset || 16.6k
!ineq16.6k
||
!context16.6k
)
2340
0
    goto error;
2341
16.6k
2342
16.6k
  
if (16.6k
bset->n_ineq == 0 || 16.6k
isl_basic_set_plain_is_universe(context)11.5k
)
{5.69k
2343
5.69k
    isl_basic_set_free(context);
2344
5.69k
    isl_mat_free(ineq);
2345
5.69k
    return bset;
2346
16.6k
  }
2347
16.6k
2348
16.6k
  ctx = isl_basic_set_get_ctx(context);
2349
10.9k
  row = isl_calloc_array(ctx, int, bset->n_ineq);
2350
10.9k
  if (!row)
2351
0
    goto error;
2352
10.9k
2353
10.9k
  
if (10.9k
mark_shifted_constraints(ineq, context, row) < 010.9k
)
2354
0
    goto error;
2355
10.9k
  
if (10.9k
all_neg(row, bset->n_ineq)10.9k
)
2356
1.49k
    return update_ineq_free(bset, ineq, context, row, NULL);
2357
10.9k
2358
10.9k
  context = drop_irrelevant_constraints_marked(context, ineq, row);
2359
9.41k
  if (!context)
2360
0
    goto error;
2361
9.41k
  
if (9.41k
isl_basic_set_plain_is_universe(context)9.41k
)
2362
257
    return update_ineq_free(bset, ineq, context, row, NULL);
2363
9.41k
2364
9.41k
  n_eq = context->n_eq;
2365
9.16k
  context_ineq = context->n_ineq;
2366
9.16k
  combined = isl_basic_set_cow(isl_basic_set_copy(context));
2367
9.16k
  combined = isl_basic_set_extend_constraints(combined, 0, bset->n_ineq);
2368
9.16k
  tab = isl_tab_from_basic_set(combined, 0);
2369
37.1k
  for (i = 0; 
i < context_ineq37.1k
;
++i27.9k
)
2370
27.9k
    
if (27.9k
isl_tab_freeze_constraint(tab, n_eq + i) < 027.9k
)
2371
0
      goto error;
2372
9.16k
  
if (9.16k
isl_tab_extend_cons(tab, bset->n_ineq) < 09.16k
)
2373
0
    goto error;
2374
9.16k
  r = context_ineq;
2375
32.2k
  for (i = 0; 
i < bset->n_ineq32.2k
;
++i23.1k
)
{23.1k
2376
23.1k
    if (row[i] < 0)
2377
4.68k
      continue;
2378
23.1k
    combined = isl_basic_set_add_ineq(combined, ineq->row[i]);
2379
18.4k
    if (isl_tab_add_ineq(tab, ineq->row[i]) < 0)
2380
0
      goto error;
2381
18.4k
    row[i] = r++;
2382
18.4k
  }
2383
9.16k
  
if (9.16k
isl_tab_detect_implicit_equalities(tab) < 09.16k
)
2384
0
    goto error;
2385
9.16k
  
if (9.16k
isl_tab_detect_redundant(tab) < 09.16k
)
2386
0
    goto error;
2387
32.2k
  
for (i = bset->n_ineq - 1; 9.16k
i >= 032.2k
;
--i23.1k
)
{23.1k
2388
23.1k
    isl_basic_set *test;
2389
23.1k
    int is_empty;
2390
23.1k
2391
23.1k
    if (row[i] < 0)
2392
4.68k
      continue;
2393
23.1k
    r = row[i];
2394
18.4k
    if (tab->con[n_eq + r].is_redundant)
2395
446
      continue;
2396
18.4k
    test = isl_basic_set_dup(combined);
2397
17.9k
    if (isl_inequality_negate(test, r) < 0)
2398
0
      test = isl_basic_set_free(test);
2399
17.9k
    test = isl_basic_set_update_from_tab(test, tab);
2400
17.9k
    is_empty = isl_basic_set_is_empty(test);
2401
17.9k
    isl_basic_set_free(test);
2402
17.9k
    if (is_empty < 0)
2403
0
      goto error;
2404
17.9k
    
if (17.9k
is_empty17.9k
)
2405
5.42k
      tab->con[n_eq + r].is_redundant = 1;
2406
17.9k
  }
2407
9.16k
  bset = update_ineq_free(bset, ineq, context, row, tab);
2408
9.16k
  if (
bset9.16k
)
{9.16k
2409
9.16k
    ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT);
2410
9.16k
    ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT);
2411
9.16k
  }
2412
9.16k
2413
9.16k
  isl_basic_set_free(combined);
2414
9.16k
  return bset;
2415
9.16k
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
9.16k
  return NULL;
2423
16.6k
}
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
16.6k
{
2429
16.6k
  unsigned total;
2430
16.6k
  isl_ctx *ctx;
2431
16.6k
  isl_mat *ineq;
2432
16.6k
2433
16.6k
  if (!bset)
2434
0
    return NULL;
2435
16.6k
2436
16.6k
  ctx = isl_basic_set_get_ctx(bset);
2437
16.6k
  total = isl_basic_set_total_dim(bset);
2438
16.6k
  ineq = isl_mat_sub_alloc6(ctx, bset->ineq, 0, bset->n_ineq,
2439
16.6k
            0, 1 + total);
2440
16.6k
2441
16.6k
  return ineq;
2442
16.6k
}
2443
2444
/* Remove all information from "bset" that is redundant in the context
2445
 * of "context", for the case where both "bset" and "context" are
2446
 * full-dimensional.
2447
 */
2448
static __isl_give isl_basic_set *uset_gist_uncompressed(
2449
  __isl_take isl_basic_set *bset, __isl_take isl_basic_set *context)
2450
8.49k
{
2451
8.49k
  isl_mat *ineq;
2452
8.49k
2453
8.49k
  ineq = extract_ineq(bset);
2454
8.49k
  return uset_gist_full(bset, ineq, context);
2455
8.49k
}
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
8.12k
{
2489
8.12k
  isl_ctx *ctx;
2490
8.12k
  isl_mat *ineq;
2491
8.12k
  int i, n_row, n_col;
2492
8.12k
  isl_int rem;
2493
8.12k
2494
8.12k
  ineq = extract_ineq(bset);
2495
8.12k
  ineq = isl_mat_product(ineq, isl_mat_copy(T));
2496
8.12k
  context = isl_basic_set_preimage(context, T);
2497
8.12k
2498
8.12k
  if (
!ineq || 8.12k
!context8.12k
)
2499
0
    goto error;
2500
8.12k
  
if (8.12k
isl_basic_set_plain_is_empty(context)8.12k
)
{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
8.12k
  }
2505
8.12k
2506
8.12k
  ctx = isl_mat_get_ctx(ineq);
2507
8.11k
  n_row = isl_mat_rows(ineq);
2508
8.11k
  n_col = isl_mat_cols(ineq);
2509
8.11k
  isl_int_init(rem);
2510
16.0k
  for (i = 0; 
i < n_row16.0k
;
++i7.91k
)
{7.91k
2511
7.91k
    isl_seq_gcd(ineq->row[i] + 1, n_col - 1, &ctx->normalize_gcd);
2512
7.91k
    if (isl_int_is_zero(ctx->normalize_gcd))
2513
21
      continue;
2514
7.89k
    
if (7.89k
isl_int_is_one7.89k
(ctx->normalize_gcd))
2515
7.50k
      continue;
2516
7.89k
    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
389
    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
8.11k
  }
2528
8.11k
  isl_int_clear(rem);
2529
8.11k
2530
8.12k
  return uset_gist_full(bset, ineq, context);
2531
8.12k
error:
2532
0
  isl_mat_free(ineq);
2533
0
  isl_basic_set_free(context);
2534
0
  isl_basic_set_free(bset);
2535
8.12k
  return NULL;
2536
8.12k
}
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
8.12k
{
2543
8.12k
  int i, n;
2544
8.12k
2545
8.12k
  if (
!bset || 8.12k
!template8.12k
)
2546
0
    return isl_basic_set_free(bset);
2547
8.12k
2548
8.12k
  n = isl_basic_set_dim(template, isl_dim_set);
2549
8.12k
2550
52.6k
  for (i = 0; 
i < n52.6k
;
++i44.4k
)
{44.4k
2551
44.4k
    isl_bool involved;
2552
44.4k
2553
44.4k
    involved = isl_basic_set_involves_dims(template,
2554
44.4k
              isl_dim_set, i, 1);
2555
44.4k
    if (involved < 0)
2556
0
      return isl_basic_set_free(bset);
2557
44.4k
    
if (44.4k
involved44.4k
)
2558
23.7k
      continue;
2559
44.4k
    bset = isl_basic_set_eliminate_vars(bset, i, 1);
2560
20.7k
  }
2561
8.12k
2562
8.12k
  return bset;
2563
8.12k
}
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
16.6k
{
2591
16.6k
  isl_mat *eq;
2592
16.6k
  isl_mat *T;
2593
16.6k
  isl_basic_set *aff;
2594
16.6k
  isl_basic_set *aff_context;
2595
16.6k
  unsigned total;
2596
16.6k
2597
16.6k
  if (
!bset || 16.6k
!context16.6k
)
2598
0
    goto error;
2599
16.6k
2600
16.6k
  context = drop_irrelevant_constraints(context, bset);
2601
16.6k
2602
16.6k
  bset = isl_basic_set_detect_equalities(bset);
2603
16.6k
  aff = isl_basic_set_copy(bset);
2604
16.6k
  aff = isl_basic_set_plain_affine_hull(aff);
2605
16.6k
  context = isl_basic_set_detect_equalities(context);
2606
16.6k
  aff_context = isl_basic_set_copy(context);
2607
16.6k
  aff_context = isl_basic_set_plain_affine_hull(aff_context);
2608
16.6k
  aff = isl_basic_set_intersect(aff, aff_context);
2609
16.6k
  if (!aff)
2610
0
    goto error;
2611
16.6k
  
if (16.6k
isl_basic_set_plain_is_empty(aff)16.6k
)
{1
2612
1
    isl_basic_set_free(bset);
2613
1
    isl_basic_set_free(context);
2614
1
    return aff;
2615
16.6k
  }
2616
16.6k
  bset = isl_basic_set_sort_constraints(bset);
2617
16.6k
  if (
aff->n_eq == 016.6k
)
{8.49k
2618
8.49k
    isl_basic_set_free(aff);
2619
8.49k
    return uset_gist_uncompressed(bset, context);
2620
16.6k
  }
2621
16.6k
  total = isl_basic_set_total_dim(bset);
2622
8.12k
  eq = isl_mat_sub_alloc6(bset->ctx, aff->eq, 0, aff->n_eq, 0, 1 + total);
2623
8.12k
  eq = isl_mat_cow(eq);
2624
8.12k
  T = isl_mat_variable_compression(eq, NULL);
2625
8.12k
  isl_basic_set_free(aff);
2626
8.12k
  if (
T && 8.12k
T->n_col == 08.12k
)
{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
8.12k
  }
2631
8.12k
2632
8.12k
  aff_context = isl_basic_set_affine_hull(isl_basic_set_copy(context));
2633
8.12k
  aff_context = project_onto_involved(aff_context, bset);
2634
8.12k
2635
8.12k
  bset = uset_gist_compressed(bset, context, T);
2636
8.12k
  bset = isl_basic_set_reduce_using_equalities(bset, aff_context);
2637
8.12k
2638
8.12k
  if (
bset8.12k
)
{8.12k
2639
8.12k
    ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT);
2640
8.12k
    ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT);
2641
8.12k
  }
2642
8.12k
2643
8.12k
  return bset;
2644
8.12k
error:
2645
0
  isl_basic_set_free(bset);
2646
0
  isl_basic_set_free(context);
2647
8.12k
  return NULL;
2648
16.6k
}
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
872
{
2656
872
  int i;
2657
872
  int total, n_div;
2658
872
2659
872
  if (!bmap)
2660
0
    return -1;
2661
872
2662
872
  
if (872
bmap->n_eq == 0872
)
2663
320
    return 0;
2664
872
2665
872
  total = isl_basic_map_dim(bmap, isl_dim_all);
2666
552
  n_div = isl_basic_map_dim(bmap, isl_dim_div);
2667
552
  total -= n_div;
2668
552
2669
952
  for (i = 0; 
i < bmap->n_eq952
;
++i400
)
2670
639
    
if (639
isl_seq_first_non_zero(bmap->eq[i] + 1 + total,639
2671
639
              n_div) == -1)
2672
239
      return i;
2673
552
2674
313
  return bmap->n_eq;
2675
872
}
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
2
error:
2706
0
  isl_space_free(space);
2707
0
  isl_mat_free(eq);
2708
0
  isl_basic_map_free(bmap);
2709
2
  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
7
  }
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
4
  }
2751
4
  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
7
}
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
6
  }
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
7
    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
7
  }
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
7
error:
2973
0
  isl_int_clear(gcd);
2974
0
  isl_basic_map_free(bmap);
2975
7
  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
436
{
2990
436
  int bmap_n_eq, context_n_eq;
2991
436
  isl_mat *A;
2992
436
2993
436
  if (
!bmap || 436
!context436
)
2994
0
    return isl_basic_map_free(bmap);
2995
436
2996
436
  bmap_n_eq = n_div_eq(bmap);
2997
436
  context_n_eq = n_div_eq(context);
2998
436
2999
436
  if (
bmap_n_eq < 0 || 436
context_n_eq < 0436
)
3000
0
    return isl_basic_map_free(bmap);
3001
436
  
if (436
bmap_n_eq == 0 || 436
context_n_eq == 0251
)
3002
429
    return bmap;
3003
436
3004
436
  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
436
  return bmap;
3011
436
}
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
18.0k
{
3036
18.0k
  isl_basic_set *bset, *eq;
3037
18.0k
  isl_basic_map *eq_bmap;
3038
18.0k
  unsigned total, n_div, extra, n_eq, n_ineq;
3039
18.0k
3040
18.0k
  if (
!bmap || 18.0k
!context18.0k
)
3041
0
    goto error;
3042
18.0k
3043
18.0k
  
if (18.0k
isl_basic_map_plain_is_universe(bmap)18.0k
)
{1.40k
3044
1.40k
    isl_basic_map_free(context);
3045
1.40k
    return bmap;
3046
18.0k
  }
3047
16.6k
  
if (16.6k
isl_basic_map_plain_is_empty(context)16.6k
)
{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
16.6k
  }
3053
16.6k
  
if (16.6k
isl_basic_map_plain_is_empty(bmap)16.6k
)
{0
3054
0
    isl_basic_map_free(context);
3055
0
    return bmap;
3056
16.6k
  }
3057
16.6k
3058
16.6k
  bmap = isl_basic_map_remove_redundancies(bmap);
3059
16.6k
  context = isl_basic_map_remove_redundancies(context);
3060
16.6k
  if (!context)
3061
0
    goto error;
3062
16.6k
3063
16.6k
  context = isl_basic_map_align_divs(context, bmap);
3064
16.6k
  n_div = isl_basic_map_dim(context, isl_dim_div);
3065
16.6k
  total = isl_basic_map_dim(bmap, isl_dim_all);
3066
16.6k
  extra = n_div - isl_basic_map_dim(bmap, isl_dim_div);
3067
16.6k
3068
16.6k
  bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
3069
16.6k
  bset = isl_basic_set_add_dims(bset, isl_dim_set, extra);
3070
16.6k
  bset = uset_gist(bset,
3071
16.6k
        isl_basic_map_underlying_set(isl_basic_map_copy(context)));
3072
16.6k
  bset = isl_basic_set_project_out(bset, isl_dim_set, total, extra);
3073
16.6k
3074
16.6k
  if (
!bset || 16.6k
bset->n_eq == 016.6k
||
n_div == 010.0k
||
3075
16.6k
      
isl_basic_set_plain_is_empty(bset)537
)
{16.1k
3076
16.1k
    isl_basic_map_free(context);
3077
16.1k
    return isl_basic_map_overlying_set(bset, bmap);
3078
16.6k
  }
3079
16.6k
3080
16.6k
  n_eq = bset->n_eq;
3081
436
  n_ineq = bset->n_ineq;
3082
436
  eq = isl_basic_set_copy(bset);
3083
436
  eq = isl_basic_set_cow(eq);
3084
436
  if (isl_basic_set_free_inequality(eq, n_ineq) < 0)
3085
0
    eq = isl_basic_set_free(eq);
3086
436
  if (isl_basic_set_free_equality(bset, n_eq) < 0)
3087
0
    bset = isl_basic_set_free(bset);
3088
436
3089
436
  eq_bmap = isl_basic_map_overlying_set(eq, isl_basic_map_copy(bmap));
3090
436
  eq_bmap = gist_strides(eq_bmap, context);
3091
436
  eq_bmap = isl_basic_map_remove_shifted_constraints(eq_bmap, context);
3092
436
  bmap = isl_basic_map_overlying_set(bset, bmap);
3093
436
  bmap = isl_basic_map_intersect(bmap, eq_bmap);
3094
436
  bmap = isl_basic_map_remove_redundancies(bmap);
3095
436
3096
16.6k
  return bmap;
3097
16.6k
error:
3098
0
  isl_basic_map_free(bmap);
3099
0
  isl_basic_map_free(context);
3100
16.6k
  return NULL;
3101
18.0k
}
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
17.0k
{
3109
17.0k
  int i;
3110
17.0k
3111
17.0k
  if (
!map || 17.0k
!context17.0k
)
3112
0
    goto error;
3113
17.0k
3114
17.0k
  
if (17.0k
isl_basic_map_plain_is_empty(context)17.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
17.0k
  }
3120
17.0k
3121
17.0k
  context = isl_basic_map_remove_redundancies(context);
3122
17.0k
  map = isl_map_cow(map);
3123
17.0k
  if (
!map || 17.0k
!context17.0k
)
3124
0
    goto error;
3125
17.0k
  
isl_assert17.0k
(map->ctx, isl_space_is_equal(map->dim, context->dim), goto error);17.0k
3126
17.0k
  map = isl_map_compute_divs(map);
3127
17.0k
  if (!map)
3128
0
    goto error;
3129
34.3k
  
for (i = map->n - 1; 17.0k
i >= 034.3k
;
--i17.2k
)
{17.2k
3130
17.2k
    map->p[i] = isl_basic_map_gist(map->p[i],
3131
17.2k
            isl_basic_map_copy(context));
3132
17.2k
    if (!map->p[i])
3133
0
      goto error;
3134
17.2k
    
if (17.2k
isl_basic_map_plain_is_empty(map->p[i])17.2k
)
{2.47k
3135
2.47k
      isl_basic_map_free(map->p[i]);
3136
2.47k
      if (i != map->n - 1)
3137
1.02k
        map->p[i] = map->p[map->n - 1];
3138
2.47k
      map->n--;
3139
17.2k
    }
3140
17.2k
  }
3141
17.0k
  isl_basic_map_free(context);
3142
17.0k
  ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3143
17.0k
  return map;
3144
17.0k
error:
3145
0
  isl_map_free(map);
3146
0
  isl_basic_map_free(context);
3147
17.0k
  return NULL;
3148
17.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
388
{
3166
388
  int i1, i2;
3167
388
  unsigned total, extra;
3168
388
3169
388
  if (
!bmap || 388
!context388
)
3170
0
    return isl_basic_map_free(bmap);
3171
388
3172
388
  total = isl_basic_map_total_dim(context);
3173
388
  extra = isl_basic_map_total_dim(bmap) - total;
3174
388
3175
388
  i1 = bmap->n_ineq - 1;
3176
388
  i2 = context->n_ineq - 1;
3177
1.46k
  while (
bmap && 1.46k
i1 >= 01.46k
&&
i2 >= 01.21k
)
{1.08k
3178
1.08k
    int cmp;
3179
1.08k
3180
1.08k
    if (isl_seq_first_non_zero(bmap->ineq[i1] + 1 + total,
3181
1.08k
              extra) != -1) {
3182
18
      --i1;
3183
18
      continue;
3184
1.08k
    }
3185
1.08k
    cmp = isl_basic_map_constraint_cmp(context, bmap->ineq[i1],
3186
1.06k
              context->ineq[i2]);
3187
1.06k
    if (
cmp < 01.06k
)
{0
3188
0
      --i2;
3189
0
      continue;
3190
1.06k
    }
3191
1.06k
    
if (1.06k
cmp > 01.06k
)
{295
3192
295
      --i1;
3193
295
      continue;
3194
1.06k
    }
3195
767
    
if (767
isl_int_eq767
(bmap->ineq[i1][0], context->ineq[i2][0]))
{689
3196
689
      bmap = isl_basic_map_cow(bmap);
3197
689
      if (isl_basic_map_drop_inequality(bmap, i1) < 0)
3198
0
        bmap = isl_basic_map_free(bmap);
3199
767
    }
3200
767
    --i1;
3201
767
    --i2;
3202
767
  }
3203
388
3204
388
  return bmap;
3205
388
}
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
388
{
3221
388
  int i1, i2;
3222
388
  unsigned total, extra;
3223
388
3224
388
  if (
!bmap || 388
!context388
)
3225
0
    return isl_basic_map_free(bmap);
3226
388
3227
388
  total = isl_basic_map_total_dim(context);
3228
388
  extra = isl_basic_map_total_dim(bmap) - total;
3229
388
3230
388
  i1 = bmap->n_eq - 1;
3231
388
  i2 = context->n_eq - 1;
3232
388
3233
420
  while (
bmap && 420
i1 >= 0420
&&
i2 >= 089
)
{32
3234
32
    int last1, last2;
3235
32
3236
32
    if (isl_seq_first_non_zero(bmap->eq[i1] + 1 + total,
3237
32
              extra) != -1)
3238
0
      break;
3239
32
    last1 = isl_seq_last_non_zero(bmap->eq[i1] + 1, total);
3240
32
    last2 = isl_seq_last_non_zero(context->eq[i2] + 1, total);
3241
32
    if (
last1 > last232
)
{0
3242
0
      --i2;
3243
0
      continue;
3244
32
    }
3245
32
    
if (32
last1 < last232
)
{3
3246
3
      --i1;
3247
3
      continue;
3248
32
    }
3249
29
    
if (29
isl_seq_eq(bmap->eq[i1], context->eq[i2], 1 + total)29
)
{29
3250
29
      bmap = isl_basic_map_cow(bmap);
3251
29
      if (isl_basic_map_drop_equality(bmap, i1) < 0)
3252
0
        bmap = isl_basic_map_free(bmap);
3253
29
    }
3254
29
    --i1;
3255
29
    --i2;
3256
388
  }
3257
388
3258
388
  return bmap;
3259
388
}
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
388
{
3272
388
  isl_bool done, known;
3273
388
3274
388
  done = isl_basic_map_plain_is_universe(context);
3275
388
  if (done == isl_bool_false)
3276
388
    done = isl_basic_map_plain_is_universe(bmap);
3277
388
  if (done == isl_bool_false)
3278
388
    done = isl_basic_map_plain_is_empty(context);
3279
388
  if (done == isl_bool_false)
3280
388
    done = isl_basic_map_plain_is_empty(bmap);
3281
388
  if (done < 0)
3282
0
    goto error;
3283
388
  
if (388
done388
)
{0
3284
0
    isl_basic_map_free(context);
3285
0
    return bmap;
3286
388
  }
3287
388
  known = isl_basic_map_divs_known(context);
3288
388
  if (known < 0)
3289
0
    goto error;
3290
388
  
if (388
!known388
)
3291
0
    isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3292
388
      "context has unknown divs", goto error);
3293
388
3294
388
  bmap = isl_basic_map_align_divs(bmap, context);
3295
388
  bmap = isl_basic_map_gauss(bmap, NULL);
3296
388
  bmap = isl_basic_map_sort_constraints(bmap);
3297
388
  context = isl_basic_map_sort_constraints(context);
3298
388
3299
388
  bmap = drop_inequalities(bmap, context);
3300
388
  bmap = drop_equalities(bmap, context);
3301
388
3302
388
  isl_basic_map_free(context);
3303
388
  bmap = isl_basic_map_finalize(bmap);
3304
388
  return bmap;
3305
388
error:
3306
0
  isl_basic_map_free(bmap);
3307
0
  isl_basic_map_free(context);
3308
388
  return NULL;
3309
388
}
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
5
{
3316
5
  isl_basic_map *bmap;
3317
5
3318
5
  bmap = isl_basic_map_copy(map->p[pos]);
3319
5
  isl_map_free(map);
3320
5
  isl_basic_map_free(context);
3321
5
  return isl_map_from_basic_map(bmap);
3322
5
}
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
284
{
3333
284
  int i;
3334
284
  isl_bool univ, known;
3335
284
3336
284
  univ = isl_basic_map_plain_is_universe(context);
3337
284
  if (univ < 0)
3338
0
    goto error;
3339
284
  
if (284
univ284
)
{83
3340
83
    isl_basic_map_free(context);
3341
83
    return map;
3342
284
  }
3343
284
  known = isl_basic_map_divs_known(context);
3344
201
  if (known < 0)
3345
0
    goto error;
3346
201
  
if (201
!known201
)
3347
0
    isl_die(isl_map_get_ctx(map), isl_error_invalid,
3348
201
      "context has unknown divs", goto error);
3349
201
3350
201
  map = isl_map_cow(map);
3351
201
  if (!map)
3352
0
    goto error;
3353
584
  
for (i = 0; 201
i < map->n584
;
++i383
)
{388
3354
388
    map->p[i] = isl_basic_map_plain_gist(map->p[i],
3355
388
            isl_basic_map_copy(context));
3356
388
    univ = isl_basic_map_plain_is_universe(map->p[i]);
3357
388
    if (univ < 0)
3358
0
      goto error;
3359
388
    
if (388
univ && 388
map->n > 15
)
3360
5
      return replace_by_disjunct(map, i, context);
3361
388
  }
3362
201
3363
201
  isl_basic_map_free(context);
3364
196
  ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3365
196
  if (map->n > 1)
3366
196
    ISL_F_CLR(map, ISL_MAP_DISJOINT);
3367
201
  return map;
3368
201
error:
3369
0
  isl_map_free(map);
3370
0
  isl_basic_map_free(context);
3371
201
  return NULL;
3372
284
}
3373
3374
/* Remove the constraints in "context" from "set".
3375
 * If any of the disjuncts in the result turns out to be the universe,
3376
 * then return this universe.
3377
 * "context" is assumed to have explicit representations
3378
 * for all local variables.
3379
 */
3380
__isl_give isl_set *isl_set_plain_gist_basic_set(__isl_take isl_set *set,
3381
  __isl_take isl_basic_set *context)
3382
101
{
3383
101
  return set_from_map(isl_map_plain_gist_basic_map(set_to_map(set),
3384
101
              bset_to_bmap(context)));
3385
101
}
3386
3387
/* Remove the constraints in "context" from "map".
3388
 * If any of the disjuncts in the result turns out to be the universe,
3389
 * then return this universe.
3390
 * "context" is assumed to consist of a single disjunct and
3391
 * to have explicit representations for all local variables.
3392
 */
3393
__isl_give isl_map *isl_map_plain_gist(__isl_take isl_map *map,
3394
  __isl_take isl_map *context)
3395
23
{
3396
23
  isl_basic_map *hull;
3397
23
3398
23
  hull = isl_map_unshifted_simple_hull(context);
3399
23
  return isl_map_plain_gist_basic_map(map, hull);
3400
23
}
3401
3402
/* Replace "map" by a universe map in the same space and free "drop".
3403
 */
3404
static __isl_give isl_map *replace_by_universe(__isl_take isl_map *map,
3405
  __isl_take isl_map *drop)
3406
7.36k
{
3407
7.36k
  isl_map *res;
3408
7.36k
3409
7.36k
  res = isl_map_universe(isl_map_get_space(map));
3410
7.36k
  isl_map_free(map);
3411
7.36k
  isl_map_free(drop);
3412
7.36k
  return res;
3413
7.36k
}
3414
3415
/* Return a map that has the same intersection with "context" as "map"
3416
 * and that is as "simple" as possible.
3417
 *
3418
 * If "map" is already the universe, then we cannot make it any simpler.
3419
 * Similarly, if "context" is the universe, then we cannot exploit it
3420
 * to simplify "map"
3421
 * If "map" and "context" are identical to each other, then we can
3422
 * return the corresponding universe.
3423
 *
3424
 * If either "map" or "context" consists of multiple disjuncts,
3425
 * then check if "context" happens to be a subset of "map",
3426
 * in which case all constraints can be removed.
3427
 * In case of multiple disjuncts, the standard procedure
3428
 * may not be able to detect that all constraints can be removed.
3429
 *
3430
 * If none of these cases apply, we have to work a bit harder.
3431
 * During this computation, we make use of a single disjunct context,
3432
 * so if the original context consists of more than one disjunct
3433
 * then we need to approximate the context by a single disjunct set.
3434
 * Simply taking the simple hull may drop constraints that are
3435
 * only implicitly available in each disjunct.  We therefore also
3436
 * look for constraints among those defining "map" that are valid
3437
 * for the context.  These can then be used to simplify away
3438
 * the corresponding constraints in "map".
3439
 */
3440
static __isl_give isl_map *map_gist(__isl_take isl_map *map,
3441
  __isl_take isl_map *context)
3442
48.8k
{
3443
48.8k
  int equal;
3444
48.8k
  int is_universe;
3445
48.8k
  int single_disjunct_map, single_disjunct_context;
3446
48.8k
  isl_bool subset;
3447
48.8k
  isl_basic_map *hull;
3448
48.8k
3449
48.8k
  is_universe = isl_map_plain_is_universe(map);
3450
48.8k
  if (
is_universe >= 0 && 48.8k
!is_universe48.8k
)
3451
30.3k
    is_universe = isl_map_plain_is_universe(context);
3452
48.8k
  if (is_universe < 0)
3453
0
    goto error;
3454
48.8k
  
if (48.8k
is_universe48.8k
)
{26.3k
3455
26.3k
    isl_map_free(context);
3456
26.3k
    return map;
3457
48.8k
  }
3458
48.8k
3459
48.8k
  equal = isl_map_plain_is_equal(map, context);
3460
22.4k
  if (equal < 0)
3461
0
    goto error;
3462
22.4k
  
if (22.4k
equal22.4k
)
3463
7.34k
    return replace_by_universe(map, context);
3464
22.4k
3465
22.4k
  single_disjunct_map = isl_map_n_basic_map(map) == 1;
3466
15.1k
  single_disjunct_context = isl_map_n_basic_map(context) == 1;
3467
15.1k
  if (
!single_disjunct_map || 15.1k
!single_disjunct_context10.3k
)
{5.01k
3468
5.01k
    subset = isl_map_is_subset(context, map);
3469
5.01k
    if (subset < 0)
3470
0
      goto error;
3471
5.01k
    
if (5.01k
subset5.01k
)
3472
24
      return replace_by_universe(map, context);
3473
15.1k
  }
3474
15.1k
3475
15.1k
  context = isl_map_compute_divs(context);
3476
15.0k
  if (!context)
3477
0
    goto error;
3478
15.0k
  
if (15.0k
single_disjunct_context15.0k
)
{14.6k
3479
14.6k
    hull = isl_map_simple_hull(context);
3480
15.0k
  } else {
3481
433
    isl_ctx *ctx;
3482
433
    isl_map_list *list;
3483
433
3484
433
    ctx = isl_map_get_ctx(map);
3485
433
    list = isl_map_list_alloc(ctx, 2);
3486
433
    list = isl_map_list_add(list, isl_map_copy(context));
3487
433
    list = isl_map_list_add(list, isl_map_copy(map));
3488
433
    hull = isl_map_unshifted_simple_hull_from_map_list(context,
3489
433
                    list);
3490
15.0k
  }
3491
15.0k
  return isl_map_gist_basic_map(map, hull);
3492
15.0k
error:
3493
0
  isl_map_free(map);
3494
0
  isl_map_free(context);
3495
15.0k
  return NULL;
3496
48.8k
}
3497
3498
__isl_give isl_map *isl_map_gist(__isl_take isl_map *map,
3499
  __isl_take isl_map *context)
3500
48.8k
{
3501
48.8k
  return isl_map_align_params_map_map_and(map, context, &map_gist);
3502
48.8k
}
3503
3504
struct isl_basic_set *isl_basic_set_gist(struct isl_basic_set *bset,
3505
            struct isl_basic_set *context)
3506
796
{
3507
796
  return bset_from_bmap(isl_basic_map_gist(bset_to_bmap(bset),
3508
796
            bset_to_bmap(context)));
3509
796
}
3510
3511
__isl_give isl_set *isl_set_gist_basic_set(__isl_take isl_set *set,
3512
  __isl_take isl_basic_set *context)
3513
2.01k
{
3514
2.01k
  return set_from_map(isl_map_gist_basic_map(set_to_map(set),
3515
2.01k
          bset_to_bmap(context)));
3516
2.01k
}
3517
3518
__isl_give isl_set *isl_set_gist_params_basic_set(__isl_take isl_set *set,
3519
  __isl_take isl_basic_set *context)
3520
0
{
3521
0
  isl_space *space = isl_set_get_space(set);
3522
0
  isl_basic_set *dom_context = isl_basic_set_universe(space);
3523
0
  dom_context = isl_basic_set_intersect_params(dom_context, context);
3524
0
  return isl_set_gist_basic_set(set, dom_context);
3525
0
}
3526
3527
__isl_give isl_set *isl_set_gist(__isl_take isl_set *set,
3528
  __isl_take isl_set *context)
3529
10.4k
{
3530
10.4k
  return set_from_map(isl_map_gist(set_to_map(set), set_to_map(context)));
3531
10.4k
}
3532
3533
/* Compute the gist of "bmap" with respect to the constraints "context"
3534
 * on the domain.
3535
 */
3536
__isl_give isl_basic_map *isl_basic_map_gist_domain(
3537
  __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *context)
3538
0
{
3539
0
  isl_space *space = isl_basic_map_get_space(bmap);
3540
0
  isl_basic_map *bmap_context = isl_basic_map_universe(space);
3541
0
3542
0
  bmap_context = isl_basic_map_intersect_domain(bmap_context, context);
3543
0
  return isl_basic_map_gist(bmap, bmap_context);
3544
0
}
3545
3546
__isl_give isl_map *isl_map_gist_domain(__isl_take isl_map *map,
3547
  __isl_take isl_set *context)
3548
5.98k
{
3549
5.98k
  isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3550
5.98k
  map_context = isl_map_intersect_domain(map_context, context);
3551
5.98k
  return isl_map_gist(map, map_context);
3552
5.98k
}
3553
3554
__isl_give isl_map *isl_map_gist_range(__isl_take isl_map *map,
3555
  __isl_take isl_set *context)
3556
86
{
3557
86
  isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3558
86
  map_context = isl_map_intersect_range(map_context, context);
3559
86
  return isl_map_gist(map, map_context);
3560
86
}
3561
3562
__isl_give isl_map *isl_map_gist_params(__isl_take isl_map *map,
3563
  __isl_take isl_set *context)
3564
28.2k
{
3565
28.2k
  isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3566
28.2k
  map_context = isl_map_intersect_params(map_context, context);
3567
28.2k
  return isl_map_gist(map, map_context);
3568
28.2k
}
3569
3570
__isl_give isl_set *isl_set_gist_params(__isl_take isl_set *set,
3571
  __isl_take isl_set *context)
3572
22.1k
{
3573
22.1k
  return isl_map_gist_params(set, context);
3574
22.1k
}
3575
3576
/* Quick check to see if two basic maps are disjoint.
3577
 * In particular, we reduce the equalities and inequalities of
3578
 * one basic map in the context of the equalities of the other
3579
 * basic map and check if we get a contradiction.
3580
 */
3581
isl_bool isl_basic_map_plain_is_disjoint(__isl_keep isl_basic_map *bmap1,
3582
  __isl_keep isl_basic_map *bmap2)
3583
22.2k
{
3584
22.2k
  struct isl_vec *v = NULL;
3585
22.2k
  int *elim = NULL;
3586
22.2k
  unsigned total;
3587
22.2k
  int i;
3588
22.2k
3589
22.2k
  if (
!bmap1 || 22.2k
!bmap222.2k
)
3590
0
    return isl_bool_error;
3591
22.2k
  
isl_assert22.2k
(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),22.2k
3592
22.2k
      return isl_bool_error);
3593
22.2k
  
if (22.2k
bmap1->n_div || 22.2k
bmap2->n_div21.4k
)
3594
988
    return isl_bool_false;
3595
21.2k
  
if (21.2k
!bmap1->n_eq && 21.2k
!bmap2->n_eq8.38k
)
3596
7.23k
    return isl_bool_false;
3597
21.2k
3598
21.2k
  total = isl_space_dim(bmap1->dim, isl_dim_all);
3599
14.0k
  if (total == 0)
3600
0
    return isl_bool_false;
3601
14.0k
  v = isl_vec_alloc(bmap1->ctx, 1 + total);
3602
14.0k
  if (!v)
3603
0
    goto error;
3604
14.0k
  
elim = 14.0k
isl_alloc_array14.0k
(bmap1->ctx, int, total);
3605
14.0k
  if (!elim)
3606
0
    goto error;
3607
14.0k
  compute_elimination_index(bmap1, elim);
3608
29.6k
  for (i = 0; 
i < bmap2->n_eq29.6k
;
++i15.6k
)
{16.0k
3609
16.0k
    int reduced;
3610
16.0k
    reduced = reduced_using_equalities(v->block.data, bmap2->eq[i],
3611
16.0k
              bmap1, elim);
3612
16.0k
    if (
reduced && 16.0k
!7.09k
isl_int_is_zero7.09k
(v->block.data[0]) &&
3613
1.16k
        isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3614
411
      goto disjoint;
3615
16.0k
  }
3616
122k
  
for (i = 0; 13.6k
i < bmap2->n_ineq122k
;
++i109k
)
{114k
3617
114k
    int reduced;
3618
114k
    reduced = reduced_using_equalities(v->block.data,
3619
114k
            bmap2->ineq[i], bmap1, elim);
3620
114k
    if (
reduced && 114k
isl_int_is_neg6.54k
(v->block.data[0]) &&
3621
5.22k
        isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3622
4.70k
      goto disjoint;
3623
114k
  }
3624
13.6k
  compute_elimination_index(bmap2, elim);
3625
69.8k
  for (i = 0; 
i < bmap1->n_ineq69.8k
;
++i60.9k
)
{63.4k
3626
63.4k
    int reduced;
3627
63.4k
    reduced = reduced_using_equalities(v->block.data,
3628
63.4k
            bmap1->ineq[i], bmap2, elim);
3629
63.4k
    if (
reduced && 63.4k
isl_int_is_neg3.51k
(v->block.data[0]) &&
3630
2.72k
        isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3631
2.48k
      goto disjoint;
3632
63.4k
  }
3633
8.90k
  isl_vec_free(v);
3634
6.42k
  free(elim);
3635
8.90k
  return isl_bool_false;
3636
8.90k
disjoint:
3637
7.60k
  isl_vec_free(v);
3638
7.60k
  free(elim);
3639
8.90k
  return isl_bool_true;
3640
8.90k
error:
3641
0
  isl_vec_free(v);
3642
0
  free(elim);
3643
8.90k
  return isl_bool_error;
3644
22.2k
}
3645
3646
int isl_basic_set_plain_is_disjoint(__isl_keep isl_basic_set *bset1,
3647
  __isl_keep isl_basic_set *bset2)
3648
0
{
3649
0
  return isl_basic_map_plain_is_disjoint(bset_to_bmap(bset1),
3650
0
                bset_to_bmap(bset2));
3651
0
}
3652
3653
/* Does "test" hold for all pairs of basic maps in "map1" and "map2"?
3654
 */
3655
static isl_bool all_pairs(__isl_keep isl_map *map1, __isl_keep isl_map *map2,
3656
  isl_bool (*test)(__isl_keep isl_basic_map *bmap1,
3657
    __isl_keep isl_basic_map *bmap2))
3658
17.0k
{
3659
17.0k
  int i, j;
3660
17.0k
3661
17.0k
  if (
!map1 || 17.0k
!map217.0k
)
3662
0
    return isl_bool_error;
3663
17.0k
3664
26.5k
  
for (i = 0; 17.0k
i < map1->n26.5k
;
++i9.49k
)
{20.4k
3665
31.7k
    for (j = 0; 
j < map2->n31.7k
;
++j11.2k
)
{22.2k
3666
22.2k
      isl_bool d = test(map1->p[i], map2->p[j]);
3667
22.2k
      if (d != isl_bool_true)
3668
10.9k
        return d;
3669
22.2k
    }
3670
20.4k
  }
3671
17.0k
3672
6.06k
  return isl_bool_true;
3673
17.0k
}
3674
3675
/* Are "map1" and "map2" obviously disjoint, based on information
3676
 * that can be derived without looking at the individual basic maps?
3677
 *
3678
 * In particular, if one of them is empty or if they live in different spaces
3679
 * (ignoring parameters), then they are clearly disjoint.
3680
 */
3681
static isl_bool isl_map_plain_is_disjoint_global(__isl_keep isl_map *map1,
3682
  __isl_keep isl_map *map2)
3683
35.7k
{
3684
35.7k
  isl_bool disjoint;
3685
35.7k
  isl_bool match;
3686
35.7k
3687
35.7k
  if (
!map1 || 35.7k
!map235.7k
)
3688
0
    return isl_bool_error;
3689
35.7k
3690
35.7k
  disjoint = isl_map_plain_is_empty(map1);
3691
35.7k
  if (
disjoint < 0 || 35.7k
disjoint35.7k
)
3692
4.58k
    return disjoint;
3693
35.7k
3694
35.7k
  disjoint = isl_map_plain_is_empty(map2);
3695
31.1k
  if (
disjoint < 0 || 31.1k
disjoint31.1k
)
3696
8.38k
    return disjoint;
3697
31.1k
3698
31.1k
  match = isl_space_tuple_is_equal(map1->dim, isl_dim_in,
3699
22.7k
        map2->dim, isl_dim_in);
3700
22.7k
  if (
match < 0 || 22.7k
!match22.7k
)
3701
0
    
return match < 0 ? 0
isl_bool_error0
:
isl_bool_true0
;
3702
22.7k
3703
22.7k
  match = isl_space_tuple_is_equal(map1->dim, isl_dim_out,
3704
22.7k
        map2->dim, isl_dim_out);
3705
22.7k
  if (
match < 0 || 22.7k
!match22.7k
)
3706
0
    
return match < 0 ? 0
isl_bool_error0
:
isl_bool_true0
;
3707
22.7k
3708
22.7k
  return isl_bool_false;
3709
35.7k
}
3710
3711
/* Are "map1" and "map2" obviously disjoint?
3712
 *
3713
 * If one of them is empty or if they live in different spaces (ignoring
3714
 * parameters), then they are clearly disjoint.
3715
 * This is checked by isl_map_plain_is_disjoint_global.
3716
 *
3717
 * If they have different parameters, then we skip any further tests.
3718
 *
3719
 * If they are obviously equal, but not obviously empty, then we will
3720
 * not be able to detect if they are disjoint.
3721
 *
3722
 * Otherwise we check if each basic map in "map1" is obviously disjoint
3723
 * from each basic map in "map2".
3724
 */
3725
isl_bool isl_map_plain_is_disjoint(__isl_keep isl_map *map1,
3726
  __isl_keep isl_map *map2)
3727
0
{
3728
0
  isl_bool disjoint;
3729
0
  isl_bool intersect;
3730
0
  isl_bool match;
3731
0
3732
0
  disjoint = isl_map_plain_is_disjoint_global(map1, map2);
3733
0
  if (
disjoint < 0 || 0
disjoint0
)
3734
0
    return disjoint;
3735
0
3736
0
  match = isl_map_has_equal_params(map1, map2);
3737
0
  if (
match < 0 || 0
!match0
)
3738
0
    
return match < 0 ? 0
isl_bool_error0
:
isl_bool_false0
;
3739
0
3740
0
  intersect = isl_map_plain_is_equal(map1, map2);
3741
0
  if (
intersect < 0 || 0
intersect0
)
3742
0
    
return intersect < 0 ? 0
isl_bool_error0
:
isl_bool_false0
;
3743
0
3744
0
  return all_pairs(map1, map2, &isl_basic_map_plain_is_disjoint);
3745
0
}
3746
3747
/* Are "map1" and "map2" disjoint?
3748
 * The parameters are assumed to have been aligned.
3749
 *
3750
 * In particular, check whether all pairs of basic maps are disjoint.
3751
 */
3752
static isl_bool isl_map_is_disjoint_aligned(__isl_keep isl_map *map1,
3753
  __isl_keep isl_map *map2)
3754
17.0k
{
3755
17.0k
  return all_pairs(map1, map2, &isl_basic_map_is_disjoint);
3756
17.0k
}
3757
3758
/* Are "map1" and "map2" disjoint?
3759
 *
3760
 * They are disjoint if they are "obviously disjoint" or if one of them
3761
 * is empty.  Otherwise, they are not disjoint if one of them is universal.
3762
 * If the two inputs are (obviously) equal and not empty, then they are
3763
 * not disjoint.
3764
 * If none of these cases apply, then check if all pairs of basic maps
3765
 * are disjoint after aligning the parameters.
3766
 */
3767
isl_bool isl_map_is_disjoint(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
3768
35.7k
{
3769
35.7k
  isl_bool disjoint;
3770
35.7k
  isl_bool intersect;
3771
35.7k
3772
35.7k
  disjoint = isl_map_plain_is_disjoint_global(map1, map2);
3773
35.7k
  if (
disjoint < 0 || 35.7k
disjoint35.7k
)
3774
12.9k
    return disjoint;
3775
35.7k
3776
35.7k
  disjoint = isl_map_is_empty(map1);
3777
22.7k
  if (
disjoint < 0 || 22.7k
disjoint22.7k
)
3778
0
    return disjoint;
3779
22.7k
3780
22.7k
  disjoint = isl_map_is_empty(map2);
3781
22.7k
  if (
disjoint < 0 || 22.7k
disjoint22.7k
)
3782
0
    return disjoint;
3783
22.7k
3784
22.7k
  intersect = isl_map_plain_is_universe(map1);
3785
22.7k
  if (
intersect < 0 || 22.7k
intersect22.7k
)
3786
3.45k
    
return intersect < 0 ? 3.45k
isl_bool_error0
:
isl_bool_false3.45k
;
3787
22.7k
3788
22.7k
  intersect = isl_map_plain_is_universe(map2);
3789
19.2k
  if (
intersect < 0 || 19.2k
intersect19.2k
)
3790
1.62k
    
return intersect < 0 ? 1.62k
isl_bool_error0
:
isl_bool_false1.62k
;
3791
19.2k
3792
19.2k
  intersect = isl_map_plain_is_equal(map1, map2);
3793
17.6k
  if (
intersect < 0 || 17.6k
intersect17.6k
)
3794
625
    return isl_bool_not(intersect);
3795
17.6k
3796
17.6k
  return isl_map_align_params_map_map_and_test(map1, map2,
3797
17.6k
            &isl_map_is_disjoint_aligned);
3798
35.7k
}
3799
3800
/* Are "bmap1" and "bmap2" disjoint?
3801
 *
3802
 * They are disjoint if they are "obviously disjoint" or if one of them
3803
 * is empty.  Otherwise, they are not disjoint if one of them is universal.
3804
 * If none of these cases apply, we compute the intersection and see if
3805
 * the result is empty.
3806
 */
3807
isl_bool isl_basic_map_is_disjoint(__isl_keep isl_basic_map *bmap1,
3808
  __isl_keep isl_basic_map *bmap2)
3809
22.2k
{
3810
22.2k
  isl_bool disjoint;
3811
22.2k
  isl_bool intersect;
3812
22.2k
  isl_basic_map *test;
3813
22.2k
3814
22.2k
  disjoint = isl_basic_map_plain_is_disjoint(bmap1, bmap2);
3815
22.2k
  if (
disjoint < 0 || 22.2k
disjoint22.2k
)
3816
7.60k
    return disjoint;
3817
22.2k
3818
22.2k
  disjoint = isl_basic_map_is_empty(bmap1);
3819
14.6k
  if (
disjoint < 0 || 14.6k
disjoint14.6k
)
3820
0
    return disjoint;
3821
14.6k
3822
14.6k
  disjoint = isl_basic_map_is_empty(bmap2);
3823
14.6k
  if (
disjoint < 0 || 14.6k
disjoint14.6k
)
3824
0
    return disjoint;
3825
14.6k
3826
14.6k
  intersect = isl_basic_map_plain_is_universe(bmap1);
3827
14.6k
  if (
intersect < 0 || 14.6k
intersect14.6k
)
3828
0
    
return intersect < 0 ? 0
isl_bool_error0
:
isl_bool_false0
;
3829
14.6k
3830
14.6k
  intersect = isl_basic_map_plain_is_universe(bmap2);
3831
14.6k
  if (
intersect < 0 || 14.6k
intersect14.6k
)
3832
0
    
return intersect < 0 ? 0
isl_bool_error0
:
isl_bool_false0
;
3833
14.6k
3834
14.6k
  test = isl_basic_map_intersect(isl_basic_map_copy(bmap1),
3835
14.6k
    isl_basic_map_copy(bmap2));
3836
14.6k
  disjoint = isl_basic_map_is_empty(test);
3837
14.6k
  isl_basic_map_free(test);
3838
14.6k
3839
14.6k
  return disjoint;
3840
22.2k
}
3841
3842
/* Are "bset1" and "bset2" disjoint?
3843
 */
3844
isl_bool isl_basic_set_is_disjoint(__isl_keep isl_basic_set *bset1,
3845
  __isl_keep isl_basic_set *bset2)
3846
21
{
3847
21
  return isl_basic_map_is_disjoint(bset1, bset2);
3848
21
}
3849
3850
isl_bool isl_set_plain_is_disjoint(__isl_keep isl_set *set1,
3851
  __isl_keep isl_set *set2)
3852
0
{
3853
0
  return isl_map_plain_is_disjoint(set_to_map(set1), set_to_map(set2));
3854
0
}
3855
3856
/* Are "set1" and "set2" disjoint?
3857
 */
3858
isl_bool isl_set_is_disjoint(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
3859
11.3k
{
3860
11.3k
  return isl_map_is_disjoint(set1, set2);
3861
11.3k
}
3862
3863
/* Is "v" equal to 0, 1 or -1?
3864
 */
3865
static int is_zero_or_one(isl_int v)
3866
173
{
3867
173
  return 
isl_int_is_zero173
(v) ||
isl_int_is_one58
(v) ||
isl_int_is_negone34
(v);
3868
173
}
3869
3870
/* Check if we can combine a given div with lower bound l and upper
3871
 * bound u with some other div and if so return that other div.
3872
 * Otherwise return -1.
3873
 *
3874
 * We first check that
3875
 *  - the bounds are opposites of each other (except for the constant
3876
 *    term)
3877
 *  - the bounds do not reference any other div
3878
 *  - no div is defined in terms of this div
3879
 *
3880
 * Let m be the size of the range allowed on the div by the bounds.
3881
 * That is, the bounds are of the form
3882
 *
3883
 *  e <= a <= e + m - 1
3884
 *
3885
 * with e some expression in the other variables.
3886
 * We look for another div b such that no third div is defined in terms
3887
 * of this second div b and such that in any constraint that contains
3888
 * a (except for the given lower and upper bound), also contains b
3889
 * with a coefficient that is m times that of b.
3890
 * That is, all constraints (except for the lower and upper bound)
3891
 * are of the form
3892
 *
3893
 *  e + f (a + m b) >= 0
3894
 *
3895
 * Furthermore, in the constraints that only contain b, the coefficient
3896
 * of b should be equal to 1 or -1.
3897
 * If so, we return b so that "a + m b" can be replaced by
3898
 * a single div "c = a + m b".
3899
 */
3900
static int div_find_coalesce(struct isl_basic_map *bmap, int *pairs,
3901
  unsigned div, unsigned l, unsigned u)
3902
1.16k
{
3903
1.16k
  int i, j;
3904
1.16k
  unsigned dim;
3905
1.16k
  int coalesce = -1;
3906
1.16k
3907
1.16k
  if (bmap->n_div <= 1)
3908
354
    return -1;
3909
1.16k
  dim = isl_space_dim(bmap->dim, isl_dim_all);
3910
813
  if (isl_seq_first_non_zero(bmap->ineq[l] + 1 + dim, div) != -1)
3911
547
    return -1;
3912
266
  
if (266
isl_seq_first_non_zero(bmap->ineq[l] + 1 + dim + div + 1,266
3913
266
           bmap->n_div - div - 1) != -1)
3914
29
    return -1;
3915
237
  
if (237
!isl_seq_is_neg(bmap->ineq[l] + 1, bmap->ineq[u] + 1,237
3916
237
          dim + bmap->n_div))
3917
128
    return -1;
3918
237
3919
371
  
for (i = 0; 109
i < bmap->n_div371
;
++i262
)
{262
3920
262
    if (isl_int_is_zero(bmap->div[i][0]))
3921
196
      continue;
3922
66
    
if (66
!66
isl_int_is_zero66
(bmap->div[i][1 + 1 + dim + div]))
3923
0
      return -1;
3924
109
  }
3925
109
3926
109
  
isl_int_add109
(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0]);109
3927
109
  if (
isl_int_is_neg109
(bmap->ineq[l][0]))
{0
3928
0
    isl_int_sub(bmap->ineq[l][0],
3929
0
          bmap->ineq[l][0], bmap->ineq[u][0]);
3930
0
    bmap = isl_basic_map_copy(bmap);
3931
0
    bmap = isl_basic_map_set_to_empty(bmap);
3932
0
    isl_basic_map_free(bmap);
3933
0
    return -1;
3934
109
  }
3935
109
  
isl_int_add_ui109
(bmap->ineq[l][0], bmap->ineq[l][0], 1);109
3936
369
  for (i = 0; 
i < bmap->n_div369
;
++i260
)
{261
3937
261
    if (i == div)
3938
108
      continue;
3939
153
    
if (153
!pairs[i]153
)
3940
84
      continue;
3941
261
    
for (j = 0; 69
j < bmap->n_div261
;
++j192
)
{192
3942
192
      if (isl_int_is_zero(bmap->div[j][0]))
3943
159
        continue;
3944
33
      
if (33
!33
isl_int_is_zero33
(bmap->div[j][1 + 1 + dim + i]))
3945
0
        break;
3946
69
    }
3947
69
    if (j < bmap->n_div)
3948
0
      continue;
3949
300
    
for (j = 0; 69
j < bmap->n_ineq300
;
++j231
)
{299
3950
299
      int valid;
3951
299
      if (
j == l || 299
j == u261
)
3952
76
        continue;
3953
223
      
if (223
isl_int_is_zero223
(bmap->ineq[j][1 + dim + div]))
{173
3954
173
        if (is_zero_or_one(bmap->ineq[j][1 + dim + i]))
3955
151
          continue;
3956
22
        break;
3957
223
      }
3958
50
      
if (50
isl_int_is_zero50
(bmap->ineq[j][1 + dim + i]))
3959
35
        break;
3960
15
      
isl_int_mul15
(bmap->ineq[j][1 + dim + div],15
3961
15
            bmap->ineq[j][1 + dim + div],
3962
15
            bmap->ineq[l][0]);
3963
15
      valid = isl_int_eq(bmap->ineq[j][1 + dim + div],
3964
15
             bmap->ineq[j][1 + dim + i]);
3965
15
      isl_int_divexact(bmap->ineq[j][1 + dim + div],
3966
15
           bmap->ineq[j][1 + dim + div],
3967
15
           bmap->ineq[l][0]);
3968
15
      if (!valid)
3969
11
        break;
3970
69
    }
3971
69
    if (j < bmap->n_ineq)
3972
68
      continue;
3973
69
    coalesce = i;
3974
69
    break;
3975
109
  }
3976
109
  isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
3977
109
  isl_int_sub(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0]);
3978
109
  return coalesce;
3979
1.16k
}
3980
3981
/* Internal data structure used during the construction and/or evaluation of
3982
 * an inequality that ensures that a pair of bounds always allows
3983
 * for an integer value.
3984
 *
3985
 * "tab" is the tableau in which the inequality is evaluated.  It may
3986
 * be NULL until it is actually needed.
3987
 * "v" contains the inequality coefficients.
3988
 * "g", "fl" and "fu" are temporary scalars used during the construction and
3989
 * evaluation.
3990
 */
3991
struct test_ineq_data {
3992
  struct isl_tab *tab;
3993
  isl_vec *v;
3994
  isl_int g;
3995
  isl_int fl;
3996
  isl_int fu;
3997
};
3998
3999
/* Free all the memory allocated by the fields of "data".
4000
 */
4001
static void test_ineq_data_clear(struct test_ineq_data *data)
4002
1.51k
{
4003
1.51k
  isl_tab_free(data->tab);
4004
1.51k
  isl_vec_free(data->v);
4005
1.51k
  isl_int_clear(data->g);
4006
1.51k
  isl_int_clear(data->fl);
4007
1.51k
  isl_int_clear(data->fu);
4008
1.51k
}
4009
4010
/* Is the inequality stored in data->v satisfied by "bmap"?
4011
 * That is, does it only attain non-negative values?
4012
 * data->tab is a tableau corresponding to "bmap".
4013
 */
4014
static isl_bool test_ineq_is_satisfied(__isl_keep isl_basic_map *bmap,
4015
  struct test_ineq_data *data)
4016
1.41k
{
4017
1.41k
  isl_ctx *ctx;
4018
1.41k
  enum isl_lp_result res;
4019
1.41k
4020
1.41k
  ctx = isl_basic_map_get_ctx(bmap);
4021
1.41k
  if (!data->tab)
4022
1.09k
    data->tab = isl_tab_from_basic_map(bmap, 0);
4023
1.41k
  res = isl_tab_min(data->tab, data->v->el, ctx->one, &data->g, NULL, 0);
4024
1.41k
  if (res == isl_lp_error)
4025
0
    return isl_bool_error;
4026
1.41k
  
return res == isl_lp_ok && 1.41k
isl_int_is_nonneg1.41k
(data->g);
4027
1.41k
}
4028
4029
/* Given a lower and an upper bound on div i, do they always allow
4030
 * for an integer value of the given div?
4031
 * Determine this property by constructing an inequality
4032
 * such that the property is guaranteed when the inequality is nonnegative.
4033
 * The lower bound is inequality l, while the upper bound is inequality u.
4034
 * The constructed inequality is stored in data->v.
4035
 *
4036
 * Let the upper bound be
4037
 *
4038
 *  -n_u a + e_u >= 0
4039
 *
4040
 * and the lower bound
4041
 *
4042
 *  n_l a + e_l >= 0
4043
 *
4044
 * Let n_u = f_u g and n_l = f_l g, with g = gcd(n_u, n_l).
4045
 * We have
4046
 *
4047
 *  - f_u e_l <= f_u f_l g a <= f_l e_u
4048
 *
4049
 * Since all variables are integer valued, this is equivalent to
4050
 *
4051
 *  - f_u e_l - (f_u - 1) <= f_u f_l g a <= f_l e_u + (f_l - 1)
4052
 *
4053
 * If this interval is at least f_u f_l g, then it contains at least
4054
 * one integer value for a.
4055
 * That is, the test constraint is
4056
 *
4057
 *  f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 >= f_u f_l g
4058
 *
4059
 * or
4060
 *
4061
 *  f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 - f_u f_l g >= 0
4062
 *
4063
 * If the coefficients of f_l e_u + f_u e_l have a common divisor g',
4064
 * then the constraint can be scaled down by a factor g',
4065
 * with the constant term replaced by
4066
 * floor((f_l e_{u,0} + f_u e_{l,0} + f_l - 1 + f_u - 1 + 1 - f_u f_l g)/g').
4067
 * Note that the result of applying Fourier-Motzkin to this pair
4068
 * of constraints is
4069
 *
4070
 *  f_l e_u + f_u e_l >= 0
4071
 *
4072
 * If the constant term of the scaled down version of this constraint,
4073
 * i.e., floor((f_l e_{u,0} + f_u e_{l,0})/g') is equal to the constant
4074
 * term of the scaled down test constraint, then the test constraint
4075
 * is known to hold and no explicit evaluation is required.
4076
 * This is essentially the Omega test.
4077
 *
4078
 * If the test constraint consists of only a constant term, then
4079
 * it is sufficient to look at the sign of this constant term.
4080
 */
4081
static isl_bool int_between_bounds(__isl_keep isl_basic_map *bmap, int i,
4082
  int l, int u, struct test_ineq_data *data)
4083
3.18k
{
4084
3.18k
  unsigned offset, n_div;
4085
3.18k
  offset = isl_basic_map_offset(bmap, isl_dim_div);
4086
3.18k
  n_div = isl_basic_map_dim(bmap, isl_dim_div);
4087
3.18k
4088
3.18k
  isl_int_gcd(data->g,
4089
3.18k
        bmap->ineq[l][offset + i], bmap->ineq[u][offset + i]);
4090
3.18k
  isl_int_divexact(data->fl, bmap->ineq[l][offset + i], data->g);
4091
3.18k
  isl_int_divexact(data->fu, bmap->ineq[u][offset + i], data->g);
4092
3.18k
  isl_int_neg(data->fu, data->fu);
4093
3.18k
  isl_seq_combine(data->v->el, data->fl, bmap->ineq[u],
4094
3.18k
      data->fu, bmap->ineq[l], offset + n_div);
4095
3.18k
  isl_int_mul(data->g, data->g, data->fl);
4096
3.18k
  isl_int_mul(data->g, data->g, data->fu);
4097
3.18k
  isl_int_sub(data->g, data->g, data->fl);
4098
3.18k
  isl_int_sub(data->g, data->g, data->fu);
4099
3.18k
  isl_int_add_ui(data->g, data->g, 1);
4100
3.18k
  isl_int_sub(data->fl, data->v->el[0], data->g);
4101
3.18k
4102
3.18k
  isl_seq_gcd(data->v->el + 1, offset - 1 + n_div, &data->g);
4103
3.18k
  if (isl_int_is_zero(data->g))
4104
952
    
return 952
isl_int_is_nonneg952
(data->fl);
4105
2.22k
  
if (2.22k
isl_int_is_one2.22k
(data->g))
{1.07k
4106
1.07k
    isl_int_set(data->v->el[0], data->fl);
4107
1.07k
    return test_ineq_is_satisfied(bmap, data);
4108
2.22k
  }
4109
1.15k
  
isl_int_fdiv_q1.15k
(data->fl, data->fl, data->g);1.15k
4110
1.15k
  isl_int_fdiv_q(data->v->el[0], data->v->el[0], data->g);
4111
1.15k
  if (isl_int_eq(data->fl, data->v->el[0]))
4112
812
    return isl_bool_true;
4113
345
  
isl_int_set345
(data->v->el[0], data->fl);345
4114
345
  isl_seq_scale_down(data->v->el + 1, data->v->el + 1, data->g,
4115
345
          offset - 1 + n_div);
4116
345
4117
1.15k
  return test_ineq_is_satisfied(bmap, data);
4118
3.18k
}
4119
4120
/* Remove more kinds of divs that are not strictly needed.
4121
 * In particular, if all pairs of lower and upper bounds on a div
4122
 * are such that they allow at least one integer value of the div,
4123
 * then we can eliminate the div using Fourier-Motzkin without
4124
 * introducing any spurious solutions.
4125
 *
4126
 * If at least one of the two constraints has a unit coefficient for the div,
4127
 * then the presence of such a value is guaranteed so there is no need to check.
4128
 * In particular, the value attained by the bound with unit coefficient
4129
 * can serve as this intermediate value.
4130
 */
4131
static __isl_give isl_basic_map *drop_more_redundant_divs(
4132
  __isl_take isl_basic_map *bmap, __isl_take int *pairs, int n)
4133
1.51k
{
4134
1.51k
  isl_ctx *ctx;
4135
1.51k
  struct test_ineq_data data = { NULL, NULL };
4136
1.51k
  unsigned off, n_div;
4137
1.51k
  int remove = -1;
4138
1.51k
4139
1.51k
  isl_int_init(data.g);
4140
1.51k
  isl_int_init(data.fl);
4141
1.51k
  isl_int_init(data.fu);
4142
1.51k
4143
1.51k
  if (!bmap)
4144
0
    goto error;
4145
1.51k
4146
1.51k
  ctx = isl_basic_map_get_ctx(bmap);
4147
1.51k
  off = isl_basic_map_offset(bmap, isl_dim_div);
4148
1.51k
  n_div = isl_basic_map_dim(bmap, isl_dim_div);
4149
1.51k
  data.v = isl_vec_alloc(ctx, off + n_div);
4150
1.51k
  if (!data.v)
4151
0
    goto error;
4152
1.51k
4153
2.71k
  
while (1.51k
n > 02.71k
)
{1.91k
4154
1.91k
    int i, l, u;
4155
1.91k
    int best = -1;
4156
1.91k
    isl_bool has_int;
4157
1.91k
4158
6.91k
    for (i = 0; 
i < n_div6.91k
;
++i5.00k
)
{5.00k
4159
5.00k
      if (!pairs[i])
4160
2.46k
        continue;
4161
2.54k
      
if (2.54k
best >= 0 && 2.54k
pairs[best] <= pairs[i]634
)
4162
501
        continue;
4163
2.54k
      best = i;
4164
2.04k
    }
4165
1.91k
4166
1.91k
    i = best;
4167
14.9k
    for (l = 0; 
l < bmap->n_ineq14.9k
;
++l13.0k
)
{14.2k
4168
14.2k
      if (
!14.2k
isl_int_is_pos14.2k
(bmap->ineq[l][off + i]))
4169
11.1k
        continue;
4170
3.05k
      
if (3.05k
isl_int_is_one3.05k
(bmap->ineq[l][off + i]))
4171
847
        continue;
4172
21.1k
      
for (u = 0; 2.21k
u < bmap->n_ineq21.1k
;
++u18.9k
)
{20.1k
4173
20.1k
        if (
!20.1k
isl_int_is_neg20.1k
(bmap->ineq[u][off + i]))
4174
16.0k
          continue;
4175
4.11k
        
if (4.11k
isl_int_is_negone4.11k
(bmap->ineq[u][off + i]))
4176
932
          continue;
4177
4.11k
        has_int = int_between_bounds(bmap, i, l, u,
4178
3.18k
                &data);
4179
3.18k
        if (has_int < 0)
4180
0
          goto error;
4181
3.18k
        
if (3.18k
data.tab && 3.18k
data.tab->empty2.53k
)
4182
0
          break;
4183
3.18k
        
if (3.18k
!has_int3.18k
)
4184
1.20k
          break;
4185
3.18k
      }
4186
2.21k
      
if (2.21k
u < bmap->n_ineq2.21k
)
4187
1.20k
        break;
4188
2.21k
    }
4189
1.91k
    
if (1.91k
data.tab && 1.91k
data.tab->empty1.46k
)
{0
4190
0
      bmap = isl_basic_map_set_to_empty(bmap);
4191
0
      break;
4192
1.91k
    }
4193
1.91k
    
if (1.91k
l == bmap->n_ineq1.91k
)
{709
4194
709
      remove = i;
4195
709
      break;
4196
1.91k
    }
4197
1.91k
    pairs[i] = 0;
4198
1.20k
    --n;
4199
1.51k
  }
4200
1.51k
4201
1.51k
  test_ineq_data_clear(&data);
4202
1.51k
4203
1.51k
  free(pairs);
4204
1.51k
4205
1.51k
  if (remove < 0)
4206
808
    return bmap;
4207
1.51k
4208
1.51k
  bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, remove, 1);
4209
1.51k
  return isl_basic_map_drop_redundant_divs(bmap);
4210
1.51k
error:
4211
0
  free(pairs);
4212
0
  isl_basic_map_free(bmap);
4213
0
  test_ineq_data_clear(&data);
4214
1.51k
  return NULL;
4215
1.51k
}
4216
4217
/* Given a pair of divs div1 and div2 such that, except for the lower bound l
4218
 * and the upper bound u, div1 always occurs together with div2 in the form
4219
 * (div1 + m div2), where m is the constant range on the variable div1
4220
 * allowed by l and u, replace the pair div1 and div2 by a single
4221
 * div that is equal to div1 + m div2.
4222
 *
4223
 * The new div will appear in the location that contains div2.
4224
 * We need to modify all constraints that contain
4225
 * div2 = (div - div1) / m
4226
 * The coefficient of div2 is known to be equal to 1 or -1.
4227
 * (If a constraint does not contain div2, it will also not contain div1.)
4228
 * If the constraint also contains div1, then we know they appear
4229
 * as f (div1 + m div2) and we can simply replace (div1 + m div2) by div,
4230
 * i.e., the coefficient of div is f.
4231
 *
4232
 * Otherwise, we first need to introduce div1 into the constraint.
4233
 * Let l be
4234
 *
4235
 *  div1 + f >=0
4236
 *
4237
 * and u
4238
 *
4239
 *  -div1 + f' >= 0
4240
 *
4241
 * A lower bound on div2
4242
 *
4243
 *  div2 + t >= 0
4244
 *
4245
 * can be replaced by
4246
 *
4247
 *  m div2 + div1 + m t + f >= 0
4248
 *
4249
 * An upper bound
4250
 *
4251
 *  -div2 + t >= 0
4252
 *
4253
 * can be replaced by
4254
 *
4255
 *  -(m div2 + div1) + m t + f' >= 0
4256
 *
4257
 * These constraint are those that we would obtain from eliminating
4258
 * div1 using Fourier-Motzkin.
4259
 *
4260
 * After all constraints have been modified, we drop the lower and upper
4261
 * bound and then drop div1.
4262
 * Since the new div is only placed in the same location that used
4263
 * to store div2, but otherwise has a different meaning, any possible
4264
 * explicit representation of the original div2 is removed.
4265
 */
4266
static __isl_give isl_basic_map *coalesce_divs(__isl_take isl_basic_map *bmap,
4267
  unsigned div1, unsigned div2, unsigned l, unsigned u)
4268
1
{
4269
1
  isl_ctx *ctx;
4270
1
  isl_int m;
4271
1
  unsigned dim, total;
4272
1
  int i;
4273
1
4274
1
  ctx = isl_basic_map_get_ctx(bmap);
4275
1
4276
1
  dim = isl_space_dim(bmap->dim, isl_dim_all);
4277
1
  total = 1 + dim + bmap->n_div;
4278
1
4279
1
  isl_int_init(m);
4280
1
  isl_int_add(m, bmap->ineq[l][0], bmap->ineq[u][0]);
4281
1
  isl_int_add_ui(m, m, 1);
4282
1
4283
7
  for (i = 0; 
i < bmap->n_ineq7
;
++i6
)
{6
4284
6
    if (
i == l || 6
i == u5
)
4285
2
      continue;
4286
4
    
if (4
isl_int_is_zero4
(bmap->ineq[i][1 + dim + div2]))
4287
0
      continue;
4288
4
    
if (4
isl_int_is_zero4
(bmap->ineq[i][1 + dim + div1]))
{2
4289
2
      if (isl_int_is_pos(bmap->ineq[i][1 + dim + div2]))
4290
2
        isl_seq_combine(bmap->ineq[i], m, bmap->ineq[i],
4291
2
            ctx->one, bmap->ineq[l], total);
4292
2
      else
4293
2
        isl_seq_combine(bmap->ineq[i], m, bmap->ineq[i],
4294
2
            ctx->one, bmap->ineq[u], total);
4295
4
    }
4296
4
    isl_int_set(bmap->ineq[i][1 + dim + div2],
4297
4
          bmap->ineq[i][1 + dim + div1]);
4298
4
    isl_int_set_si(bmap->ineq[i][1 + dim + div1], 0);
4299
4
  }
4300
1
4301
1
  isl_int_clear(m);
4302
1
  if (
l > u1
)
{0
4303
0
    isl_basic_map_drop_inequality(bmap, l);
4304
0
    isl_basic_map_drop_inequality(bmap, u);
4305
1
  } else {
4306
1
    isl_basic_map_drop_inequality(bmap, u);
4307
1
    isl_basic_map_drop_inequality(bmap, l);
4308
1
  }
4309
1
  bmap = isl_basic_map_mark_div_unknown(bmap, div2);
4310
1
  bmap = isl_basic_map_drop_div(bmap, div1);
4311
1
  return bmap;
4312
1
}
4313
4314
/* First check if we can coalesce any pair of divs and
4315
 * then continue with dropping more redundant divs.
4316
 *
4317
 * We loop over all pairs of lower and upper bounds on a div
4318
 * with coefficient 1 and -1, respectively, check if there
4319
 * is any other div "c" with which we can coalesce the div
4320
 * and if so, perform the coalescing.
4321
 */
4322
static __isl_give isl_basic_map *coalesce_or_drop_more_redundant_divs(
4323
  __isl_take isl_basic_map *bmap, int *pairs, int n)
4324
1.51k
{
4325
1.51k
  int i, l, u;
4326
1.51k
  unsigned dim;
4327
1.51k
4328
1.51k
  dim = isl_space_dim(bmap->dim, isl_dim_all);
4329
1.51k
4330
5.12k
  for (i = 0; 
i < bmap->n_div5.12k
;
++i3.60k
)
{3.60k
4331
3.60k
    if (!pairs[i])
4332
1.60k
      continue;
4333
25.0k
    
for (l = 0; 2.00k
l < bmap->n_ineq25.0k
;
++l23.0k
)
{23.0k
4334
23.0k
      if (
!23.0k
isl_int_is_one23.0k
(bmap->ineq[l][1 + dim + i]))
4335
21.8k
        continue;
4336
12.8k
      
for (u = 0; 1.15k
u < bmap->n_ineq12.8k
;
++u11.7k
)
{11.7k
4337
11.7k
        int c;
4338
11.7k
4339
11.7k
        if (
!11.7k
isl_int_is_negone11.7k
(bmap->ineq[u][1+dim+i]))
4340
10.5k
          continue;
4341
11.7k
        c = div_find_coalesce(bmap, pairs, i, l, u);
4342
1.16k
        if (c < 0)
4343
1.16k
          continue;
4344
1.16k
        free(pairs);
4345
1
        bmap = coalesce_divs(bmap, i, c, l, u);
4346
1.16k
        return isl_basic_map_drop_redundant_divs(bmap);
4347
1.16k
      }
4348
2.00k
    }
4349
2.00k
  }
4350
1.51k
4351
1.51k
  
if (1.51k
ISL_F_ISSET1.51k
(bmap, ISL_BASIC_MAP_EMPTY))
{0
4352
0
    free(pairs);
4353
0
    return bmap;
4354
1.51k
  }
4355
1.51k
4356
1.51k
  return drop_more_redundant_divs(bmap, pairs, n);
4357
1.51k
}
4358
4359
/* Are the "n" coefficients starting at "first" of inequality constraints
4360
 * "i" and "j" of "bmap" equal to each other?
4361
 */
4362
static int is_parallel_part(__isl_keep isl_basic_map *bmap, int i, int j,
4363
  int first, int n)
4364
450
{
4365
450
  return isl_seq_eq(bmap->ineq[i] + first, bmap->ineq[j] + first, n);
4366
450
}
4367
4368
/* Are the "n" coefficients starting at "first" of inequality constraints
4369
 * "i" and "j" of "bmap" opposite to each other?
4370
 */
4371
static int is_opposite_part(__isl_keep isl_basic_map *bmap, int i, int j,
4372
  int first, int n)
4373
4.05k
{
4374
4.05k
  return isl_seq_is_neg(bmap->ineq[i] + first, bmap->ineq[j] + first, n);
4375
4.05k
}
4376
4377
/* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4378
 * apart from the constant term?
4379
 */
4380
static isl_bool is_opposite(__isl_keep isl_basic_map *bmap, int i, int j)
4381
3.58k
{
4382
3.58k
  unsigned total;
4383
3.58k
4384
3.58k
  total = isl_basic_map_dim(bmap, isl_dim_all);
4385
3.58k
  return is_opposite_part(bmap, i, j, 1, total);
4386
3.58k
}
4387
4388
/* Are inequality constraints "i" and "j" of "bmap" equal to each other,
4389
 * apart from the constant term and the coefficient at position "pos"?
4390
 */
4391
static int is_parallel_except(__isl_keep isl_basic_map *bmap, int i, int j,
4392
  int pos)
4393
427
{
4394
427
  unsigned total;
4395
427
4396
427
  total = isl_basic_map_dim(bmap, isl_dim_all);
4397
427
  return is_parallel_part(bmap, i, j, 1, pos - 1) &&
4398
23
    is_parallel_part(bmap, i, j, pos + 1, total - pos);
4399
427
}
4400
4401
/* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4402
 * apart from the constant term and the coefficient at position "pos"?
4403
 */
4404
static int is_opposite_except(__isl_keep isl_basic_map *bmap, int i, int j,
4405
  int pos)
4406
392
{
4407
392
  unsigned total;
4408
392
4409
392
  total = isl_basic_map_dim(bmap, isl_dim_all);
4410
392
  return is_opposite_part(bmap, i, j, 1, pos - 1) &&
4411
75
    is_opposite_part(bmap, i, j, pos + 1, total - pos);
4412
392
}
4413
4414
/* Restart isl_basic_map_drop_redundant_divs after "bmap" has
4415
 * been modified, simplying it if "simplify" is set.
4416
 * Free the temporary data structure "pairs" that was associated
4417
 * to the old version of "bmap".
4418
 */
4419
static __isl_give isl_basic_map *drop_redundant_divs_again(
4420
  __isl_take isl_basic_map *bmap, __isl_take int *pairs, int simplify)
4421
3.72k
{
4422
3.72k
  if (simplify)
4423
1.12k
    bmap = isl_basic_map_simplify(bmap);
4424
3.72k
  free(pairs);
4425
3.72k
  return isl_basic_map_drop_redundant_divs(bmap);
4426
3.72k
}
4427
4428
/* Is "div" the single unknown existentially quantified variable
4429
 * in inequality constraint "ineq" of "bmap"?
4430
 * "div" is known to have a non-zero coefficient in "ineq".
4431
 */
4432
static isl_bool single_unknown(__isl_keep isl_basic_map *bmap, int ineq,
4433
  int div)
4434
1.44k
{
4435
1.44k
  int i;
4436
1.44k
  unsigned n_div, o_div;
4437
1.44k
  isl_bool known;
4438
1.44k
4439
1.44k
  known = isl_basic_map_div_is_known(bmap, div);
4440
1.44k
  if (
known < 0 || 1.44k
known1.44k
)
4441
146
    return isl_bool_not(known);
4442
1.44k
  n_div = isl_basic_map_dim(bmap, isl_dim_div);
4443
1.29k
  if (n_div == 1)
4444
803
    return isl_bool_true;
4445
1.29k
  o_div = isl_basic_map_offset(bmap, isl_dim_div);
4446
1.73k
  for (i = 0; 
i < n_div1.73k
;
++i1.24k
)
{1.28k
4447
1.28k
    isl_bool known;
4448
1.28k
4449
1.28k
    if (i == div)
4450
473
      continue;
4451
811
    
if (811
isl_int_is_zero811
(bmap->ineq[ineq][o_div + i]))
4452
760
      continue;
4453
811
    known = isl_basic_map_div_is_known(bmap, i);
4454
51
    if (
known < 0 || 51
!known51
)
4455
39
      return known;
4456
493
  }
4457
493
4458
454
  return isl_bool_true;
4459
1.44k
}
4460
4461
/* Does integer division "div" have coefficient 1 in inequality constraint
4462
 * "ineq" of "map"?
4463
 */
4464
static isl_bool has_coef_one(__isl_keep isl_basic_map *bmap, int div, int ineq)
4465
1.25k
{
4466
1.25k
  unsigned o_div;
4467
1.25k
4468
1.25k
  o_div = isl_basic_map_offset(bmap, isl_dim_div);
4469
1.25k
  if (isl_int_is_one(bmap->ineq[ineq][o_div + div]))
4470
1.12k
    return isl_bool_true;
4471
1.25k
4472
133
  return isl_bool_false;
4473
1.25k
}
4474
4475
/* Turn inequality constraint "ineq" of "bmap" into an equality and
4476
 * then try and drop redundant divs again,
4477
 * freeing the temporary data structure "pairs" that was associated
4478
 * to the old version of "bmap".
4479
 */
4480
static __isl_give isl_basic_map *set_eq_and_try_again(
4481
  __isl_take isl_basic_map *bmap, int ineq, __isl_take int *pairs)
4482
1.12k
{
4483
1.12k
  bmap = isl_basic_map_cow(bmap);
4484
1.12k
  isl_basic_map_inequality_to_equality(bmap, ineq);
4485
1.12k
  return drop_redundant_divs_again(bmap, pairs, 1);
4486
1.12k
}
4487
4488
/* Drop the integer division at position "div", along with the two
4489
 * inequality constraints "ineq1" and "ineq2" in which it appears
4490
 * from "bmap" and then try and drop redundant divs again,
4491
 * freeing the temporary data structure "pairs" that was associated
4492
 * to the old version of "bmap".
4493
 */
4494
static __isl_give isl_basic_map *drop_div_and_try_again(
4495
  __isl_take isl_basic_map *bmap, int div, int ineq1, int ineq2,
4496
  __isl_take int *pairs)
4497
865
{
4498
865
  if (
ineq1 > ineq2865
)
{518
4499
518
    isl_basic_map_drop_inequality(bmap, ineq1);
4500
518
    isl_basic_map_drop_inequality(bmap, ineq2);
4501
865
  } else {
4502
347
    isl_basic_map_drop_inequality(bmap, ineq2);
4503
347
    isl_basic_map_drop_inequality(bmap, ineq1);
4504
865
  }
4505
865
  bmap = isl_basic_map_drop_div(bmap, div);
4506
865
  return drop_redundant_divs_again(bmap, pairs, 0);
4507
865
}
4508
4509
/* Given two inequality constraints
4510
 *
4511
 *  f(x) + n d + c >= 0,    (ineq)
4512
 *
4513
 * with d the variable at position "pos", and
4514
 *
4515
 *  f(x) + c0 >= 0,     (lower)
4516
 *
4517
 * compute the maximal value of the lower bound ceil((-f(x) - c)/n)
4518
 * determined by the first constraint.
4519
 * That is, store
4520
 *
4521
 *  ceil((c0 - c)/n)
4522
 *
4523
 * in *l.
4524
 */
4525
static void lower_bound_from_parallel(__isl_keep isl_basic_map *bmap,
4526
  int ineq, int lower, int pos, isl_int *l)
4527
16
{
4528
16
  isl_int_neg(*l, bmap->ineq[ineq][0]);
4529
16
  isl_int_add(*l, *l, bmap->ineq[lower][0]);
4530
16
  isl_int_cdiv_q(*l, *l, bmap->ineq[ineq][pos]);
4531
16
}
4532
4533
/* Given two inequality constraints
4534
 *
4535
 *  f(x) + n d + c >= 0,    (ineq)
4536
 *
4537
 * with d the variable at position "pos", and
4538
 *
4539
 *  -f(x) - c0 >= 0,    (upper)
4540
 *
4541
 * compute the minimal value of the lower bound ceil((-f(x) - c)/n)
4542
 * determined by the first constraint.
4543
 * That is, store
4544
 *
4545
 *  ceil((-c1 - c)/n)
4546
 *
4547
 * in *u.
4548
 */
4549
static void lower_bound_from_opposite(__isl_keep isl_basic_map *bmap,
4550
  int ineq, int upper, int pos, isl_int *u)
4551
13
{
4552
13
  isl_int_neg(*u, bmap->ineq[ineq][0]);
4553
13
  isl_int_sub(*u, *u, bmap->ineq[upper][0]);
4554
13
  isl_int_cdiv_q(*u, *u, bmap->ineq[ineq][pos]);
4555
13
}
4556
4557
/* Given a lower bound constraint "ineq" on "div" in "bmap",
4558
 * does the corresponding lower bound have a fixed value in "bmap"?
4559
 *
4560
 * In particular, "ineq" is of the form
4561
 *
4562
 *  f(x) + n d + c >= 0
4563
 *
4564
 * with n > 0, c the constant term and
4565
 * d the existentially quantified variable "div".
4566
 * That is, the lower bound is
4567
 *
4568
 *  ceil((-f(x) - c)/n)
4569
 *
4570
 * Look for a pair of constraints
4571
 *
4572
 *  f(x) + c0 >= 0
4573
 *  -f(x) + c1 >= 0
4574
 *
4575
 * i.e., -c1 <= -f(x) <= c0, that fix ceil((-f(x) - c)/n) to a constant value.
4576
 * That is, check that
4577
 *
4578
 *  ceil((-c1 - c)/n) = ceil((c0 - c)/n)
4579
 *
4580
 * If so, return the index of inequality f(x) + c0 >= 0.
4581
 * Otherwise, return -1.
4582
 */
4583
static int lower_bound_is_cst(__isl_keep isl_basic_map *bmap, int div, int ineq)
4584
133
{
4585
133
  int i;
4586
133
  int lower = -1, upper = -1;
4587
133
  unsigned o_div;
4588
133
  isl_int l, u;
4589
133
  int equal;
4590
133
4591
133
  o_div = isl_basic_map_offset(bmap, isl_dim_div);
4592
864
  for (i = 0; 
i < bmap->n_ineq && 864
(lower < 0 || 742
upper < 056
);
++i731
)
{731
4593
731
    if (i == ineq)
4594
122
      continue;
4595
609
    
if (609
!609
isl_int_is_zero609
(bmap->ineq[i][o_div + div]))
4596
153
      continue;
4597
456
    
if (456
lower < 0 &&456
4598
456
        
is_parallel_except(bmap, ineq, i, o_div + div)427
)
{19
4599
19
      lower = i;
4600
19
      continue;
4601
456
    }
4602
437
    
if (437
upper < 0 &&437
4603
437
        
is_opposite_except(bmap, ineq, i, o_div + div)392
)
{72
4604
72
      upper = i;
4605
437
    }
4606
437
  }
4607
133
4608
133
  if (
lower < 0 || 133
upper < 019
)
4609
120
    return -1;
4610
133
4611
13
  
isl_int_init13
(l);13
4612
13
  isl_int_init(u);
4613
13
4614
13
  lower_bound_from_parallel(bmap, ineq, lower, o_div + div, &l);
4615
13
  lower_bound_from_opposite(bmap, ineq, upper, o_div + div, &u);
4616
13
4617
13
  equal = isl_int_eq(l, u);
4618
13
4619
13
  isl_int_clear(l);
4620
13
  isl_int_clear(u);
4621
13
4622
13
  return equal ? 
lower3
:
-110
;
4623
133
}
4624
4625
/* Given a lower bound constraint "ineq" on the existentially quantified
4626
 * variable "div", such that the corresponding lower bound has
4627
 * a fixed value in "bmap", assign this fixed value to the variable and
4628
 * then try and drop redundant divs again,
4629
 * freeing the temporary data structure "pairs" that was associated
4630
 * to the old version of "bmap".
4631
 * "lower" determines the constant value for the lower bound.
4632
 *
4633
 * In particular, "ineq" is of the form
4634
 *
4635
 *  f(x) + n d + c >= 0,
4636
 *
4637
 * while "lower" is of the form
4638
 *
4639
 *  f(x) + c0 >= 0
4640
 *
4641
 * The lower bound is ceil((-f(x) - c)/n) and its constant value
4642
 * is ceil((c0 - c)/n).
4643
 */
4644
static __isl_give isl_basic_map *fix_cst_lower(__isl_take isl_basic_map *bmap,
4645
  int div, int ineq, int lower, int *pairs)
4646
3
{
4647
3
  isl_int c;
4648
3
  unsigned o_div;
4649
3
4650
3
  isl_int_init(c);
4651
3
4652
3
  o_div = isl_basic_map_offset(bmap, isl_dim_div);
4653
3
  lower_bound_from_parallel(bmap, ineq, lower, o_div + div, &c);
4654
3
  bmap = isl_basic_map_fix(bmap, isl_dim_div, div, c);
4655
3
  free(pairs);
4656
3
4657
3
  isl_int_clear(c);
4658
3
4659
3
  return isl_basic_map_drop_redundant_divs(bmap);
4660
3
}
4661
4662
/* Remove divs that are not strictly needed based on the inequality
4663
 * constraints.
4664
 * In particular, if a div only occurs positively (or negatively)
4665
 * in constraints, then it can simply be dropped.
4666
 * Also, if a div occurs in only two constraints and if moreover
4667
 * those two constraints are opposite to each other, except for the constant
4668
 * term and if the sum of the constant terms is such that for any value
4669
 * of the other values, there is always at least one integer value of the
4670
 * div, i.e., if one plus this sum is greater than or equal to
4671
 * the (absolute value) of the coefficient of the div in the constraints,
4672
 * then we can also simply drop the div.
4673
 *
4674
 * If an existentially quantified variable does not have an explicit
4675
 * representation, appears in only a single lower bound that does not
4676
 * involve any other such existentially quantified variables and appears
4677
 * in this lower bound with coefficient 1,
4678
 * then fix the variable to the value of the lower bound.  That is,
4679
 * turn the inequality into an equality.
4680
 * If for any value of the other variables, there is any value
4681
 * for the existentially quantified variable satisfying the constraints,
4682
 * then this lower bound also satisfies the constraints.
4683
 * It is therefore safe to pick this lower bound.
4684
 *
4685
 * The same reasoning holds even if the coefficient is not one.
4686
 * However, fixing the variable to the value of the lower bound may
4687
 * in general introduce an extra integer division, in which case
4688
 * it may be better to pick another value.
4689
 * If this integer division has a known constant value, then plugging
4690
 * in this constant value removes the existentially quantified variable
4691
 * completely.  In particular, if the lower bound is of the form
4692
 * ceil((-f(x) - c)/n) and there are two constraints, f(x) + c0 >= 0 and
4693
 * -f(x) + c1 >= 0 such that ceil((-c1 - c)/n) = ceil((c0 - c)/n),
4694
 * then the existentially quantified variable can be assigned this
4695
 * shared value.
4696
 *
4697
 * We skip divs that appear in equalities or in the definition of other divs.
4698
 * Divs that appear in the definition of other divs usually occur in at least
4699
 * 4 constraints, but the constraints may have been simplified.
4700
 *
4701
 * If any divs are left after these simple checks then we move on
4702
 * to more complicated cases in drop_more_redundant_divs.
4703
 */
4704
static __isl_give isl_basic_map *isl_basic_map_drop_redundant_divs_ineq(
4705
  __isl_take isl_basic_map *bmap)
4706
176k
{
4707
176k
  int i, j;
4708
176k
  unsigned off;
4709
176k
  int *pairs = NULL;
4710
176k
  int n = 0;
4711
176k
4712
176k
  if (!bmap)
4713
0
    goto error;
4714
176k
  
if (176k
bmap->n_div == 0176k
)
4715
166k
    return bmap;
4716
176k
4717
176k
  off = isl_space_dim(bmap->dim, isl_dim_all);
4718
10.3k
  pairs = isl_calloc_array(bmap->ctx, int, bmap->n_div);
4719
10.3k
  if (!pairs)
4720
0
    goto error;
4721
10.3k
4722
21.0k
  
for (i = 0; 10.3k
i < bmap->n_div21.0k
;
++i10.6k
)
{14.3k
4723
14.3k
    int pos, neg;
4724
14.3k
    int last_pos, last_neg;
4725
14.3k
    int redundant;
4726
14.3k
    int defined;
4727
14.3k
    isl_bool opp, set_div;
4728
14.3k
4729
14.3k
    defined = !isl_int_is_zero(bmap->div[i][0]);
4730
35.2k
    for (j = i; 
j < bmap->n_div35.2k
;
++j20.8k
)
4731
21.8k
      
if (21.8k
!21.8k
isl_int_is_zero21.8k
(bmap->div[j][1 + 1 + off + i]))
4732
970
        break;
4733
14.3k
    if (j < bmap->n_div)
4734
970
      continue;
4735
21.9k
    
for (j = 0; 13.4k
j < bmap->n_eq21.9k
;
++j8.49k
)
4736
14.3k
      
if (14.3k
!14.3k
isl_int_is_zero14.3k
(bmap->eq[j][1 + off + i]))
4737
5.84k
        break;
4738
13.4k
    if (j < bmap->n_eq)
4739
5.84k
      continue;
4740
13.4k
    ++n;
4741
7.57k
    pos = neg = 0;
4742
59.2k
    for (j = 0; 
j < bmap->n_ineq59.2k
;
++j51.6k
)
{51.6k
4743
51.6k
      if (
isl_int_is_pos51.6k
(bmap->ineq[j][1 + off + i]))
{8.54k
4744
8.54k
        last_pos = j;
4745
8.54k
        ++pos;
4746
51.6k
      }
4747
51.6k
      if (
isl_int_is_neg51.6k
(bmap->ineq[j][1 + off + i]))
{8.47k
4748
8.47k
        last_neg = j;
4749
8.47k
        ++neg;
4750
51.6k
      }
4751
51.6k
    }
4752
7.57k
    pairs[i] = pos * neg;
4753
7.57k
    if (
pairs[i] == 07.57k
)
{1.73k
4754
1.81k
      for (j = bmap->n_ineq - 1; 
j >= 01.81k
;
--j78
)
4755
78
        
if (78
!78
isl_int_is_zero78
(bmap->ineq[j][1+off+i]))
4756
37
          isl_basic_map_drop_inequality(bmap, j);
4757
1.73k
      bmap = isl_basic_map_drop_div(bmap, i);
4758
1.73k
      return drop_redundant_divs_again(bmap, pairs, 0);
4759
7.57k
    }
4760
5.84k
    
if (5.84k
pairs[i] != 15.84k
)
4761
2.26k
      opp = isl_bool_false;
4762
5.84k
    else
4763
3.58k
      opp = is_opposite(bmap, last_pos, last_neg);
4764
5.84k
    if (opp < 0)
4765
0
      goto error;
4766
5.84k
    
if (5.84k
!opp5.84k
)
{3.35k
4767
3.35k
      int lower;
4768
3.35k
      isl_bool single, one;
4769
3.35k
4770
3.35k
      if (pos != 1)
4771
1.91k
        continue;
4772
3.35k
      single = single_unknown(bmap, last_pos, i);
4773
1.44k
      if (single < 0)
4774
0
        goto error;
4775
1.44k
      
if (1.44k
!single1.44k
)
4776
185
        continue;
4777
1.44k
      one = has_coef_one(bmap, i, last_pos);
4778
1.25k
      if (one < 0)
4779
0
        goto error;
4780
1.25k
      
if (1.25k
one1.25k
)
4781
1.25k
        return set_eq_and_try_again(bmap, last_pos,
4782
1.25k
                  pairs);
4783
1.25k
      lower = lower_bound_is_cst(bmap, i, last_pos);
4784
133
      if (lower >= 0)
4785
133
        return fix_cst_lower(bmap, i, last_pos, lower,
4786
133
            pairs);
4787
130
      continue;
4788
5.84k
    }
4789
5.84k
4790
2.49k
    
isl_int_add2.49k
(bmap->ineq[last_pos][0],2.49k
4791
2.49k
          bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
4792
2.49k
    isl_int_add_ui(bmap->ineq[last_pos][0],
4793
2.49k
             bmap->ineq[last_pos][0], 1);
4794
2.49k
    redundant = isl_int_ge(bmap->ineq[last_pos][0],
4795
2.49k
        bmap->ineq[last_pos][1+off+i]);
4796
2.49k
    isl_int_sub_ui(bmap->ineq[last_pos][0],
4797
2.49k
             bmap->ineq[last_pos][0], 1);
4798
2.49k
    isl_int_sub(bmap->ineq[last_pos][0],
4799
2.49k
          bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
4800
2.49k
    if (redundant)
4801
2.49k
      return drop_div_and_try_again(bmap, i,
4802
2.49k
                last_pos, last_neg, pairs);
4803
1.62k
    
if (1.62k
defined1.62k
)
4804
1.61k
      set_div = isl_bool_false;
4805
1.62k
    else
4806
9
      set_div = ok_to_set_div_from_bound(bmap, i, last_pos);
4807
1.62k