Coverage Report

Created: 2017-08-18 19:41

/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/llvm/tools/polly/lib/External/isl/isl_affine_hull.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2008-2009 Katholieke Universiteit Leuven
3
 * Copyright 2010      INRIA Saclay
4
 * Copyright 2012      Ecole Normale Superieure
5
 *
6
 * Use of this software is governed by the MIT license
7
 *
8
 * Written by Sven Verdoolaege, K.U.Leuven, Departement
9
 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10
 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
11
 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
12
 * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
13
 */
14
15
#include <isl_ctx_private.h>
16
#include <isl_map_private.h>
17
#include <isl_seq.h>
18
#include <isl/set.h>
19
#include <isl/lp.h>
20
#include <isl/map.h>
21
#include "isl_equalities.h"
22
#include "isl_sample.h"
23
#include "isl_tab.h"
24
#include <isl_mat_private.h>
25
#include <isl_vec_private.h>
26
27
#include <bset_to_bmap.c>
28
#include <bset_from_bmap.c>
29
#include <set_to_map.c>
30
#include <set_from_map.c>
31
32
__isl_give isl_basic_map *isl_basic_map_implicit_equalities(
33
  __isl_take isl_basic_map *bmap)
34
198k
{
35
198k
  struct isl_tab *tab;
36
198k
37
198k
  if (!bmap)
38
0
    return bmap;
39
198k
40
198k
  bmap = isl_basic_map_gauss(bmap, NULL);
41
198k
  if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
42
785
    return bmap;
43
197k
  
if (197k
ISL_F_ISSET197k
(bmap, ISL_BASIC_MAP_NO_IMPLICIT))
44
5.13k
    return bmap;
45
192k
  
if (192k
bmap->n_ineq <= 1192k
)
46
24.3k
    return bmap;
47
192k
48
192k
  tab = isl_tab_from_basic_map(bmap, 0);
49
168k
  if (isl_tab_detect_implicit_equalities(tab) < 0)
50
0
    goto error;
51
168k
  bmap = isl_basic_map_update_from_tab(bmap, tab);
52
168k
  isl_tab_free(tab);
53
168k
  bmap = isl_basic_map_gauss(bmap, NULL);
54
168k
  ISL_F_SET(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
55
168k
  return bmap;
56
168k
error:
57
0
  isl_tab_free(tab);
58
0
  isl_basic_map_free(bmap);
59
168k
  return NULL;
60
198k
}
61
62
struct isl_basic_set *isl_basic_set_implicit_equalities(
63
            struct isl_basic_set *bset)
64
180k
{
65
180k
  return bset_from_bmap(
66
180k
    isl_basic_map_implicit_equalities(bset_to_bmap(bset)));
67
180k
}
68
69
/* Make eq[row][col] of both bmaps equal so we can add the row
70
 * add the column to the common matrix.
71
 * Note that because of the echelon form, the columns of row row
72
 * after column col are zero.
73
 */
74
static void set_common_multiple(
75
  struct isl_basic_set *bset1, struct isl_basic_set *bset2,
76
  unsigned row, unsigned col)
77
283k
{
78
283k
  isl_int m, c;
79
283k
80
283k
  if (isl_int_eq(bset1->eq[row][col], bset2->eq[row][col]))
81
245k
    return;
82
283k
83
38.0k
  
isl_int_init38.0k
(c);38.0k
84
38.0k
  isl_int_init(m);
85
38.0k
  isl_int_lcm(m, bset1->eq[row][col], bset2->eq[row][col]);
86
38.0k
  isl_int_divexact(c, m, bset1->eq[row][col]);
87
38.0k
  isl_seq_scale(bset1->eq[row], bset1->eq[row], c, col+1);
88
38.0k
  isl_int_divexact(c, m, bset2->eq[row][col]);
89
38.0k
  isl_seq_scale(bset2->eq[row], bset2->eq[row], c, col+1);
90
38.0k
  isl_int_clear(c);
91
38.0k
  isl_int_clear(m);
92
283k
}
93
94
/* Delete a given equality, moving all the following equalities one up.
95
 */
96
static void delete_row(struct isl_basic_set *bset, unsigned row)
97
416k
{
98
416k
  isl_int *t;
99
416k
  int r;
100
416k
101
416k
  t = bset->eq[row];
102
416k
  bset->n_eq--;
103
738k
  for (r = row; 
r < bset->n_eq738k
;
++r321k
)
104
321k
    bset->eq[r] = bset->eq[r+1];
105
416k
  bset->eq[bset->n_eq] = t;
106
416k
}
107
108
/* Make first row entries in column col of bset1 identical to
109
 * those of bset2, using the fact that entry bset1->eq[row][col]=a
110
 * is non-zero.  Initially, these elements of bset1 are all zero.
111
 * For each row i < row, we set
112
 *    A[i] = a * A[i] + B[i][col] * A[row]
113
 *    B[i] = a * B[i]
114
 * so that
115
 *    A[i][col] = B[i][col] = a * old(B[i][col])
116
 */
117
static void construct_column(
118
  struct isl_basic_set *bset1, struct isl_basic_set *bset2,
119
  unsigned row, unsigned col)
120
175k
{
121
175k
  int r;
122
175k
  isl_int a;
123
175k
  isl_int b;
124
175k
  unsigned total;
125
175k
126
175k
  isl_int_init(a);
127
175k
  isl_int_init(b);
128
175k
  total = 1 + isl_basic_set_n_dim(bset1);
129
515k
  for (r = 0; 
r < row515k
;
++r339k
)
{339k
130
339k
    if (isl_int_is_zero(bset2->eq[r][col]))
131
328k
      continue;
132
10.5k
    
isl_int_gcd10.5k
(b, bset2->eq[r][col], bset1->eq[row][col]);10.5k
133
10.5k
    isl_int_divexact(a, bset1->eq[row][col], b);
134
10.5k
    isl_int_divexact(b, bset2->eq[r][col], b);
135
10.5k
    isl_seq_combine(bset1->eq[r], a, bset1->eq[r],
136
10.5k
                b, bset1->eq[row], total);
137
10.5k
    isl_seq_scale(bset2->eq[r], bset2->eq[r], a, total);
138
175k
  }
139
175k
  isl_int_clear(a);
140
175k
  isl_int_clear(b);
141
175k
  delete_row(bset1, row);
142
175k
}
143
144
/* Make first row entries in column col of bset1 identical to
145
 * those of bset2, using only these entries of the two matrices.
146
 * Let t be the last row with different entries.
147
 * For each row i < t, we set
148
 *  A[i] = (A[t][col]-B[t][col]) * A[i] + (B[i][col]-A[i][col) * A[t]
149
 *  B[i] = (A[t][col]-B[t][col]) * B[i] + (B[i][col]-A[i][col) * B[t]
150
 * so that
151
 *  A[i][col] = B[i][col] = old(A[t][col]*B[i][col]-A[i][col]*B[t][col])
152
 */
153
static int transform_column(
154
  struct isl_basic_set *bset1, struct isl_basic_set *bset2,
155
  unsigned row, unsigned col)
156
170k
{
157
170k
  int i, t;
158
170k
  isl_int a, b, g;
159
170k
  unsigned total;
160
170k
161
200k
  for (t = row-1; 
t >= 0200k
;
--t29.2k
)
162
149k
    
if (149k
isl_int_ne149k
(bset1->eq[t][col], bset2->eq[t][col]))
163
120k
      break;
164
170k
  if (t < 0)
165
50.3k
    return 0;
166
170k
167
170k
  total = 1 + isl_basic_set_n_dim(bset1);
168
120k
  isl_int_init(a);
169
120k
  isl_int_init(b);
170
120k
  isl_int_init(g);
171
120k
  isl_int_sub(b, bset1->eq[t][col], bset2->eq[t][col]);
172
262k
  for (i = 0; 
i < t262k
;
++i142k
)
{142k
173
142k
    isl_int_sub(a, bset2->eq[i][col], bset1->eq[i][col]);
174
142k
    isl_int_gcd(g, a, b);
175
142k
    isl_int_divexact(a, a, g);
176
142k
    isl_int_divexact(g, b, g);
177
142k
    isl_seq_combine(bset1->eq[i], g, bset1->eq[i], a, bset1->eq[t],
178
142k
        total);
179
142k
    isl_seq_combine(bset2->eq[i], g, bset2->eq[i], a, bset2->eq[t],
180
142k
        total);
181
142k
  }
182
120k
  isl_int_clear(a);
183
120k
  isl_int_clear(b);
184
120k
  isl_int_clear(g);
185
120k
  delete_row(bset1, t);
186
120k
  delete_row(bset2, t);
187
170k
  return 1;
188
170k
}
189
190
/* The implementation is based on Section 5.2 of Michael Karr,
191
 * "Affine Relationships Among Variables of a Program",
192
 * except that the echelon form we use starts from the last column
193
 * and that we are dealing with integer coefficients.
194
 */
195
static struct isl_basic_set *affine_hull(
196
  struct isl_basic_set *bset1, struct isl_basic_set *bset2)
197
135k
{
198
135k
  unsigned total;
199
135k
  int col;
200
135k
  int row;
201
135k
202
135k
  if (
!bset1 || 135k
!bset2135k
)
203
0
    goto error;
204
135k
205
135k
  total = 1 + isl_basic_set_n_dim(bset1);
206
135k
207
135k
  row = 0;
208
765k
  for (col = total-1; 
col >= 0765k
;
--col630k
)
{630k
209
630k
    int is_zero1 = row >= bset1->n_eq ||
210
308k
      isl_int_is_zero(bset1->eq[row][col]);
211
630k
    int is_zero2 = row >= bset2->n_eq ||
212
458k
      isl_int_is_zero(bset2->eq[row][col]);
213
630k
    if (
!is_zero1 && 630k
!is_zero2284k
)
{283k
214
283k
      set_common_multiple(bset1, bset2, row, col);
215
283k
      ++row;
216
630k
    } else 
if (346k
!is_zero1 && 346k
is_zero21.55k
)
{1.55k
217
1.55k
      construct_column(bset1, bset2, row, col);
218
346k
    } else 
if (345k
is_zero1 && 345k
!is_zero2345k
)
{174k
219
174k
      construct_column(bset2, bset1, row, col);
220
345k
    } else {
221
170k
      if (transform_column(bset1, bset2, row, col))
222
120k
        --row;
223
630k
    }
224
630k
  }
225
135k
  isl_assert(bset1->ctx, row == bset1->n_eq, goto error);
226
135k
  isl_basic_set_free(bset2);
227
135k
  bset1 = isl_basic_set_normalize_constraints(bset1);
228
135k
  return bset1;
229
135k
error:
230
0
  isl_basic_set_free(bset1);
231
0
  isl_basic_set_free(bset2);
232
135k
  return NULL;
233
135k
}
234
235
/* Find an integer point in the set represented by "tab"
236
 * that lies outside of the equality "eq" e(x) = 0.
237
 * If "up" is true, look for a point satisfying e(x) - 1 >= 0.
238
 * Otherwise, look for a point satisfying -e(x) - 1 >= 0 (i.e., e(x) <= -1).
239
 * The point, if found, is returned.
240
 * If no point can be found, a zero-length vector is returned.
241
 *
242
 * Before solving an ILP problem, we first check if simply
243
 * adding the normal of the constraint to one of the known
244
 * integer points in the basic set represented by "tab"
245
 * yields another point inside the basic set.
246
 *
247
 * The caller of this function ensures that the tableau is bounded or
248
 * that tab->basis and tab->n_unbounded have been set appropriately.
249
 */
250
static struct isl_vec *outside_point(struct isl_tab *tab, isl_int *eq, int up)
251
14.6k
{
252
14.6k
  struct isl_ctx *ctx;
253
14.6k
  struct isl_vec *sample = NULL;
254
14.6k
  struct isl_tab_undo *snap;
255
14.6k
  unsigned dim;
256
14.6k
257
14.6k
  if (!tab)
258
0
    return NULL;
259
14.6k
  ctx = tab->mat->ctx;
260
14.6k
261
14.6k
  dim = tab->n_var;
262
14.6k
  sample = isl_vec_alloc(ctx, 1 + dim);
263
14.6k
  if (!sample)
264
0
    return NULL;
265
14.6k
  
isl_int_set_si14.6k
(sample->el[0], 1);14.6k
266
14.6k
  isl_seq_combine(sample->el + 1,
267
14.6k
    ctx->one, tab->bmap->sample->el + 1,
268
14.6k
    up ? 
ctx->one10.0k
:
ctx->negone4.58k
, eq + 1, dim);
269
14.6k
  if (isl_basic_map_contains(tab->bmap, sample))
270
68
    return sample;
271
14.6k
  isl_vec_free(sample);
272
14.5k
  sample = NULL;
273
14.5k
274
14.5k
  snap = isl_tab_snap(tab);
275
14.5k
276
14.5k
  if (!up)
277
4.56k
    isl_seq_neg(eq, eq, 1 + dim);
278
14.5k
  isl_int_sub_ui(eq[0], eq[0], 1);
279
14.5k
280
14.5k
  if (isl_tab_extend_cons(tab, 1) < 0)
281
0
    goto error;
282
14.5k
  
if (14.5k
isl_tab_add_ineq(tab, eq) < 014.5k
)
283
0
    goto error;
284
14.5k
285
14.5k
  sample = isl_tab_sample(tab);
286
14.5k
287
14.5k
  isl_int_add_ui(eq[0], eq[0], 1);
288
14.5k
  if (!up)
289
4.56k
    isl_seq_neg(eq, eq, 1 + dim);
290
14.5k
291
14.5k
  if (
sample && 14.5k
isl_tab_rollback(tab, snap) < 014.5k
)
292
0
    goto error;
293
14.5k
294
14.5k
  return sample;
295
14.5k
error:
296
0
  isl_vec_free(sample);
297
14.5k
  return NULL;
298
14.6k
}
299
300
__isl_give isl_basic_set *isl_basic_set_recession_cone(
301
  __isl_take isl_basic_set *bset)
302
180k
{
303
180k
  int i;
304
180k
305
180k
  bset = isl_basic_set_cow(bset);
306
180k
  if (!bset)
307
0
    return NULL;
308
180k
  
isl_assert180k
(bset->ctx, bset->n_div == 0, goto error);180k
309
180k
310
180k
  
for (i = 0; 180k
i < bset->n_eq180k
;
++i302
)
311
302
    isl_int_set_si(bset->eq[i][0], 0);
312
180k
313
1.14M
  for (i = 0; 
i < bset->n_ineq1.14M
;
++i965k
)
314
965k
    isl_int_set_si(bset->ineq[i][0], 0);
315
180k
316
180k
  ISL_F_CLR(bset, ISL_BASIC_SET_NO_IMPLICIT);
317
180k
  return isl_basic_set_implicit_equalities(bset);
318
180k
error:
319
0
  isl_basic_set_free(bset);
320
180k
  return NULL;
321
180k
}
322
323
/* Move "sample" to a point that is one up (or down) from the original
324
 * point in dimension "pos".
325
 */
326
static void adjacent_point(__isl_keep isl_vec *sample, int pos, int up)
327
545k
{
328
545k
  if (up)
329
272k
    isl_int_add_ui(sample->el[1 + pos], sample->el[1 + pos], 1);
330
545k
  else
331
272k
    isl_int_sub_ui(sample->el[1 + pos], sample->el[1 + pos], 1);
332
545k
}
333
334
/* Check if any points that are adjacent to "sample" also belong to "bset".
335
 * If so, add them to "hull" and return the updated hull.
336
 *
337
 * Before checking whether and adjacent point belongs to "bset", we first
338
 * check whether it already belongs to "hull" as this test is typically
339
 * much cheaper.
340
 */
341
static __isl_give isl_basic_set *add_adjacent_points(
342
  __isl_take isl_basic_set *hull, __isl_take isl_vec *sample,
343
  __isl_keep isl_basic_set *bset)
344
63.2k
{
345
63.2k
  int i, up;
346
63.2k
  int dim;
347
63.2k
348
63.2k
  if (!sample)
349
0
    goto error;
350
63.2k
351
63.2k
  dim = isl_basic_set_dim(hull, isl_dim_set);
352
63.2k
353
210k
  for (i = 0; 
i < dim210k
;
++i147k
)
{147k
354
302k
    for (up = 0; 
up <= 1302k
;
++up155k
)
{272k
355
272k
      int contains;
356
272k
      isl_basic_set *point;
357
272k
358
272k
      adjacent_point(sample, i, up);
359
272k
      contains = isl_basic_set_contains(hull, sample);
360
272k
      if (contains < 0)
361
0
        goto error;
362
272k
      
if (272k
contains272k
)
{30.2k
363
30.2k
        adjacent_point(sample, i, !up);
364
30.2k
        continue;
365
272k
      }
366
272k
      contains = isl_basic_set_contains(bset, sample);
367
242k
      if (contains < 0)
368
0
        goto error;
369
242k
      
if (242k
contains242k
)
{116k
370
116k
        point = isl_basic_set_from_vec(
371
116k
              isl_vec_copy(sample));
372
116k
        hull = affine_hull(hull, point);
373
242k
      }
374
242k
      adjacent_point(sample, i, !up);
375
242k
      if (contains)
376
116k
        break;
377
242k
    }
378
147k
  }
379
63.2k
380
63.2k
  isl_vec_free(sample);
381
63.2k
382
63.2k
  return hull;
383
63.2k
error:
384
0
  isl_vec_free(sample);
385
0
  isl_basic_set_free(hull);
386
63.2k
  return NULL;
387
63.2k
}
388
389
/* Extend an initial (under-)approximation of the affine hull of basic
390
 * set represented by the tableau "tab"
391
 * by looking for points that do not satisfy one of the equalities
392
 * in the current approximation and adding them to that approximation
393
 * until no such points can be found any more.
394
 *
395
 * The caller of this function ensures that "tab" is bounded or
396
 * that tab->basis and tab->n_unbounded have been set appropriately.
397
 *
398
 * "bset" may be either NULL or the basic set represented by "tab".
399
 * If "bset" is not NULL, we check for any point we find if any
400
 * of its adjacent points also belong to "bset".
401
 */
402
static __isl_give isl_basic_set *extend_affine_hull(struct isl_tab *tab,
403
  __isl_take isl_basic_set *hull, __isl_keep isl_basic_set *bset)
404
57.2k
{
405
57.2k
  int i, j;
406
57.2k
  unsigned dim;
407
57.2k
408
57.2k
  if (
!tab || 57.2k
!hull57.2k
)
409
0
    goto error;
410
57.2k
411
57.2k
  dim = tab->n_var;
412
57.2k
413
57.2k
  if (isl_tab_extend_cons(tab, 2 * dim + 1) < 0)
414
0
    goto error;
415
57.2k
416
64.7k
  
for (i = 0; 57.2k
i < dim64.7k
;
++i7.49k
)
{64.6k
417
64.6k
    struct isl_vec *sample;
418
64.6k
    struct isl_basic_set *point;
419
67.1k
    for (j = 0; 
j < hull->n_eq67.1k
;
++j2.54k
)
{10.0k
420
10.0k
      sample = outside_point(tab, hull->eq[j], 1);
421
10.0k
      if (!sample)
422
0
        goto error;
423
10.0k
      
if (10.0k
sample->size > 010.0k
)
424
5.44k
        break;
425
10.0k
      isl_vec_free(sample);
426
4.58k
      sample = outside_point(tab, hull->eq[j], 0);
427
4.58k
      if (!sample)
428
0
        goto error;
429
4.58k
      
if (4.58k
sample->size > 04.58k
)
430
2.04k
        break;
431
4.58k
      isl_vec_free(sample);
432
2.54k
433
2.54k
      if (isl_tab_add_eq(tab, hull->eq[j]) < 0)
434
0
        goto error;
435
64.6k
    }
436
64.6k
    
if (64.6k
j == hull->n_eq64.6k
)
437
57.1k
      break;
438
7.49k
    
if (7.49k
tab->samples &&7.49k
439
997
        isl_tab_add_sample(tab, isl_vec_copy(sample)) < 0)
440
0
      hull = isl_basic_set_free(hull);
441
7.49k
    if (bset)
442
7.49k
      hull = add_adjacent_points(hull, isl_vec_copy(sample),
443
7.49k
                bset);
444
7.49k
    point = isl_basic_set_from_vec(sample);
445
7.49k
    hull = affine_hull(hull, point);
446
7.49k
    if (!hull)
447
0
      return NULL;
448
57.2k
  }
449
57.2k
450
57.2k
  return hull;
451
57.2k
error:
452
0
  isl_basic_set_free(hull);
453
57.2k
  return NULL;
454
57.2k
}
455
456
/* Construct an initial underapproximation of the hull of "bset"
457
 * from "sample" and any of its adjacent points that also belong to "bset".
458
 */
459
static __isl_give isl_basic_set *initialize_hull(__isl_keep isl_basic_set *bset,
460
  __isl_take isl_vec *sample)
461
56.7k
{
462
56.7k
  isl_basic_set *hull;
463
56.7k
464
56.7k
  hull = isl_basic_set_from_vec(isl_vec_copy(sample));
465
56.7k
  hull = add_adjacent_points(hull, sample, bset);
466
56.7k
467
56.7k
  return hull;
468
56.7k
}
469
470
/* Look for all equalities satisfied by the integer points in bset,
471
 * which is assumed to be bounded.
472
 *
473
 * The equalities are obtained by successively looking for
474
 * a point that is affinely independent of the points found so far.
475
 * In particular, for each equality satisfied by the points so far,
476
 * we check if there is any point on a hyperplane parallel to the
477
 * corresponding hyperplane shifted by at least one (in either direction).
478
 */
479
static struct isl_basic_set *uset_affine_hull_bounded(struct isl_basic_set *bset)
480
56.8k
{
481
56.8k
  struct isl_vec *sample = NULL;
482
56.8k
  struct isl_basic_set *hull;
483
56.8k
  struct isl_tab *tab = NULL;
484
56.8k
  unsigned dim;
485
56.8k
486
56.8k
  if (isl_basic_set_plain_is_empty(bset))
487
0
    return bset;
488
56.8k
489
56.8k
  dim = isl_basic_set_n_dim(bset);
490
56.8k
491
56.8k
  if (
bset->sample && 56.8k
bset->sample->size == 1 + dim31.0k
)
{20.2k
492
20.2k
    int contains = isl_basic_set_contains(bset, bset->sample);
493
20.2k
    if (contains < 0)
494
0
      goto error;
495
20.2k
    
if (20.2k
contains20.2k
)
{20.0k
496
20.0k
      if (dim == 0)
497
0
        return bset;
498
20.0k
      sample = isl_vec_copy(bset->sample);
499
20.2k
    } else {
500
234
      isl_vec_free(bset->sample);
501
234
      bset->sample = NULL;
502
20.2k
    }
503
56.8k
  }
504
56.8k
505
56.8k
  tab = isl_tab_from_basic_set(bset, 1);
506
56.8k
  if (!tab)
507
0
    goto error;
508
56.8k
  
if (56.8k
tab->empty56.8k
)
{116
509
116
    isl_tab_free(tab);
510
116
    isl_vec_free(sample);
511
116
    return isl_basic_set_set_to_empty(bset);
512
56.8k
  }
513
56.8k
514
56.7k
  
if (56.7k
!sample56.7k
)
{36.7k
515
36.7k
    struct isl_tab_undo *snap;
516
36.7k
    snap = isl_tab_snap(tab);
517
36.7k
    sample = isl_tab_sample(tab);
518
36.7k
    if (isl_tab_rollback(tab, snap) < 0)
519
0
      goto error;
520
36.7k
    isl_vec_free(tab->bmap->sample);
521
36.7k
    tab->bmap->sample = isl_vec_copy(sample);
522
56.7k
  }
523
56.7k
524
56.7k
  
if (56.7k
!sample56.7k
)
525
0
    goto error;
526
56.7k
  
if (56.7k
sample->size == 056.7k
)
{15
527
15
    isl_tab_free(tab);
528
15
    isl_vec_free(sample);
529
15
    return isl_basic_set_set_to_empty(bset);
530
56.7k
  }
531
56.7k
532
56.7k
  hull = initialize_hull(bset, sample);
533
56.7k
534
56.7k
  hull = extend_affine_hull(tab, hull, bset);
535
56.7k
  isl_basic_set_free(bset);
536
56.7k
  isl_tab_free(tab);
537
56.7k
538
56.7k
  return hull;
539
56.7k
error:
540
0
  isl_vec_free(sample);
541
0
  isl_tab_free(tab);
542
0
  isl_basic_set_free(bset);
543
56.7k
  return NULL;
544
56.8k
}
545
546
/* Given an unbounded tableau and an integer point satisfying the tableau,
547
 * construct an initial affine hull containing the recession cone
548
 * shifted to the given point.
549
 *
550
 * The unbounded directions are taken from the last rows of the basis,
551
 * which is assumed to have been initialized appropriately.
552
 */
553
static __isl_give isl_basic_set *initial_hull(struct isl_tab *tab,
554
  __isl_take isl_vec *vec)
555
379
{
556
379
  int i;
557
379
  int k;
558
379
  struct isl_basic_set *bset = NULL;
559
379
  struct isl_ctx *ctx;
560
379
  unsigned dim;
561
379
562
379
  if (
!vec || 379
!tab379
)
563
0
    return NULL;
564
379
  ctx = vec->ctx;
565
379
  isl_assert(ctx, vec->size != 0, goto error);
566
379
567
379
  bset = isl_basic_set_alloc(ctx, 0, vec->size - 1, 0, vec->size - 1, 0);
568
379
  if (!bset)
569
0
    goto error;
570
379
  dim = isl_basic_set_n_dim(bset) - tab->n_unbounded;
571
1.45k
  for (i = 0; 
i < dim1.45k
;
++i1.07k
)
{1.07k
572
1.07k
    k = isl_basic_set_alloc_equality(bset);
573
1.07k
    if (k < 0)
574
0
      goto error;
575
1.07k
    isl_seq_cpy(bset->eq[k] + 1, tab->basis->row[1 + i] + 1,
576
1.07k
          vec->size - 1);
577
1.07k
    isl_seq_inner_product(bset->eq[k] + 1, vec->el +1,
578
1.07k
              vec->size - 1, &bset->eq[k][0]);
579
1.07k
    isl_int_neg(bset->eq[k][0], bset->eq[k][0]);
580
1.07k
  }
581
379
  bset->sample = vec;
582
379
  bset = isl_basic_set_gauss(bset, NULL);
583
379
584
379
  return bset;
585
379
error:
586
0
  isl_basic_set_free(bset);
587
0
  isl_vec_free(vec);
588
379
  return NULL;
589
379
}
590
591
/* Given a tableau of a set and a tableau of the corresponding
592
 * recession cone, detect and add all equalities to the tableau.
593
 * If the tableau is bounded, then we can simply keep the
594
 * tableau in its state after the return from extend_affine_hull.
595
 * However, if the tableau is unbounded, then
596
 * isl_tab_set_initial_basis_with_cone will add some additional
597
 * constraints to the tableau that have to be removed again.
598
 * In this case, we therefore rollback to the state before
599
 * any constraints were added and then add the equalities back in.
600
 */
601
struct isl_tab *isl_tab_detect_equalities(struct isl_tab *tab,
602
  struct isl_tab *tab_cone)
603
508
{
604
508
  int j;
605
508
  struct isl_vec *sample;
606
508
  struct isl_basic_set *hull = NULL;
607
508
  struct isl_tab_undo *snap;
608
508
609
508
  if (
!tab || 508
!tab_cone508
)
610
0
    goto error;
611
508
612
508
  snap = isl_tab_snap(tab);
613
508
614
508
  isl_mat_free(tab->basis);
615
508
  tab->basis = NULL;
616
508
617
508
  isl_assert(tab->mat->ctx, tab->bmap, goto error);
618
508
  
isl_assert508
(tab->mat->ctx, tab->samples, goto error);508
619
508
  
isl_assert508
(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var, goto error);508
620
508
  
isl_assert508
(tab->mat->ctx, tab->n_sample > tab->n_outside, goto error);508
621
508
622
508
  
if (508
isl_tab_set_initial_basis_with_cone(tab, tab_cone) < 0508
)
623
0
    goto error;
624
508
625
508
  sample = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
626
508
  if (!sample)
627
0
    goto error;
628
508
629
508
  isl_seq_cpy(sample->el, tab->samples->row[tab->n_outside], sample->size);
630
508
631
508
  isl_vec_free(tab->bmap->sample);
632
508
  tab->bmap->sample = isl_vec_copy(sample);
633
508
634
508
  if (tab->n_unbounded == 0)
635
129
    hull = isl_basic_set_from_vec(isl_vec_copy(sample));
636
508
  else
637
379
    hull = initial_hull(tab, isl_vec_copy(sample));
638
508
639
833
  for (j = tab->n_outside + 1; 
j < tab->n_sample833
;
++j325
)
{325
640
325
    isl_seq_cpy(sample->el, tab->samples->row[j], sample->size);
641
325
    hull = affine_hull(hull,
642
325
        isl_basic_set_from_vec(isl_vec_copy(sample)));
643
508
  }
644
508
645
508
  isl_vec_free(sample);
646
508
647
508
  hull = extend_affine_hull(tab, hull, NULL);
648
508
  if (!hull)
649
0
    goto error;
650
508
651
508
  
if (508
tab->n_unbounded == 0508
)
{129
652
129
    isl_basic_set_free(hull);
653
129
    return tab;
654
508
  }
655
508
656
379
  
if (379
isl_tab_rollback(tab, snap) < 0379
)
657
0
    goto error;
658
379
659
379
  
if (379
hull->n_eq > tab->n_zero379
)
{40
660
126
    for (j = 0; 
j < hull->n_eq126
;
++j86
)
{86
661
86
      isl_seq_normalize(tab->mat->ctx, hull->eq[j], 1 + tab->n_var);
662
86
      if (isl_tab_add_eq(tab, hull->eq[j]) < 0)
663
0
        goto error;
664
86
    }
665
379
  }
666
379
667
379
  isl_basic_set_free(hull);
668
379
669
379
  return tab;
670
379
error:
671
0
  isl_basic_set_free(hull);
672
0
  isl_tab_free(tab);
673
379
  return NULL;
674
508
}
675
676
/* Compute the affine hull of "bset", where "cone" is the recession cone
677
 * of "bset".
678
 *
679
 * We first compute a unimodular transformation that puts the unbounded
680
 * directions in the last dimensions.  In particular, we take a transformation
681
 * that maps all equalities to equalities (in HNF) on the first dimensions.
682
 * Let x be the original dimensions and y the transformed, with y_1 bounded
683
 * and y_2 unbounded.
684
 *
685
 *         [ y_1 ]      [ y_1 ]   [ Q_1 ]
686
 *  x = U  [ y_2 ]      [ y_2 ] = [ Q_2 ] x
687
 *
688
 * Let's call the input basic set S.  We compute S' = preimage(S, U)
689
 * and drop the final dimensions including any constraints involving them.
690
 * This results in set S''.
691
 * Then we compute the affine hull A'' of S''.
692
 * Let F y_1 >= g be the constraint system of A''.  In the transformed
693
 * space the y_2 are unbounded, so we can add them back without any constraints,
694
 * resulting in
695
 *
696
 *            [ y_1 ]
697
 *    [ F 0 ] [ y_2 ] >= g
698
 * or
699
 *            [ Q_1 ]
700
 *    [ F 0 ] [ Q_2 ] x >= g
701
 * or
702
 *    F Q_1 x >= g
703
 *
704
 * The affine hull in the original space is then obtained as
705
 * A = preimage(A'', Q_1).
706
 */
707
static struct isl_basic_set *affine_hull_with_cone(struct isl_basic_set *bset,
708
  struct isl_basic_set *cone)
709
45.6k
{
710
45.6k
  unsigned total;
711
45.6k
  unsigned cone_dim;
712
45.6k
  struct isl_basic_set *hull;
713
45.6k
  struct isl_mat *M, *U, *Q;
714
45.6k
715
45.6k
  if (
!bset || 45.6k
!cone45.6k
)
716
0
    goto error;
717
45.6k
718
45.6k
  total = isl_basic_set_total_dim(cone);
719
45.6k
  cone_dim = total - cone->n_eq;
720
45.6k
721
45.6k
  M = isl_mat_sub_alloc6(bset->ctx, cone->eq, 0, cone->n_eq, 1, total);
722
45.6k
  M = isl_mat_left_hermite(M, 0, &U, &Q);
723
45.6k
  if (!M)
724
0
    goto error;
725
45.6k
  isl_mat_free(M);
726
45.6k
727
45.6k
  U = isl_mat_lin_to_aff(U);
728
45.6k
  bset = isl_basic_set_preimage(bset, isl_mat_copy(U));
729
45.6k
730
45.6k
  bset = isl_basic_set_drop_constraints_involving(bset, total - cone_dim,
731
45.6k
              cone_dim);
732
45.6k
  bset = isl_basic_set_drop_dims(bset, total - cone_dim, cone_dim);
733
45.6k
734
45.6k
  Q = isl_mat_lin_to_aff(Q);
735
45.6k
  Q = isl_mat_drop_rows(Q, 1 + total - cone_dim, cone_dim);
736
45.6k
737
45.6k
  if (
bset && 45.6k
bset->sample45.6k
&&
bset->sample->size == 1 + total22.6k
)
738
15.4k
    bset->sample = isl_mat_vec_product(isl_mat_copy(Q), bset->sample);
739
45.6k
740
45.6k
  hull = uset_affine_hull_bounded(bset);
741
45.6k
742
45.6k
  if (
!hull45.6k
)
{0
743
0
    isl_mat_free(Q);
744
0
    isl_mat_free(U);
745
45.6k
  } else {
746
45.6k
    struct isl_vec *sample = isl_vec_copy(hull->sample);
747
45.6k
    U = isl_mat_drop_cols(U, 1 + total - cone_dim, cone_dim);
748
45.6k
    if (
sample && 45.6k
sample->size > 045.6k
)
749
45.6k
      sample = isl_mat_vec_product(U, sample);
750
45.6k
    else
751
37
      isl_mat_free(U);
752
45.6k
    hull = isl_basic_set_preimage(hull, Q);
753
45.6k
    if (
hull45.6k
)
{45.6k
754
45.6k
      isl_vec_free(hull->sample);
755
45.6k
      hull->sample = sample;
756
45.6k
    } else
757
0
      isl_vec_free(sample);
758
45.6k
  }
759
45.6k
760
45.6k
  isl_basic_set_free(cone);
761
45.6k
762
45.6k
  return hull;
763
45.6k
error:
764
0
  isl_basic_set_free(bset);
765
0
  isl_basic_set_free(cone);
766
45.6k
  return NULL;
767
45.6k
}
768
769
/* Look for all equalities satisfied by the integer points in bset,
770
 * which is assumed not to have any explicit equalities.
771
 *
772
 * The equalities are obtained by successively looking for
773
 * a point that is affinely independent of the points found so far.
774
 * In particular, for each equality satisfied by the points so far,
775
 * we check if there is any point on a hyperplane parallel to the
776
 * corresponding hyperplane shifted by at least one (in either direction).
777
 *
778
 * Before looking for any outside points, we first compute the recession
779
 * cone.  The directions of this recession cone will always be part
780
 * of the affine hull, so there is no need for looking for any points
781
 * in these directions.
782
 * In particular, if the recession cone is full-dimensional, then
783
 * the affine hull is simply the whole universe.
784
 */
785
static struct isl_basic_set *uset_affine_hull(struct isl_basic_set *bset)
786
75.8k
{
787
75.8k
  struct isl_basic_set *cone;
788
75.8k
789
75.8k
  if (isl_basic_set_plain_is_empty(bset))
790
6
    return bset;
791
75.8k
792
75.8k
  cone = isl_basic_set_recession_cone(isl_basic_set_copy(bset));
793
75.8k
  if (!cone)
794
0
    goto error;
795
75.8k
  
if (75.8k
cone->n_eq == 075.8k
)
{18.9k
796
18.9k
    isl_space *space;
797
18.9k
    space = isl_basic_set_get_space(bset);
798
18.9k
    isl_basic_set_free(cone);
799
18.9k
    isl_basic_set_free(bset);
800
18.9k
    return isl_basic_set_universe(space);
801
75.8k
  }
802
75.8k
803
56.8k
  
if (56.8k
cone->n_eq < isl_basic_set_total_dim(cone)56.8k
)
804
45.6k
    return affine_hull_with_cone(bset, cone);
805
56.8k
806
56.8k
  isl_basic_set_free(cone);
807
56.8k
  return uset_affine_hull_bounded(bset);
808
56.8k
error:
809
0
  isl_basic_set_free(bset);
810
56.8k
  return NULL;
811
75.8k
}
812
813
/* Look for all equalities satisfied by the integer points in bmap
814
 * that are independent of the equalities already explicitly available
815
 * in bmap.
816
 *
817
 * We first remove all equalities already explicitly available,
818
 * then look for additional equalities in the reduced space
819
 * and then transform the result to the original space.
820
 * The original equalities are _not_ added to this set.  This is
821
 * the responsibility of the calling function.
822
 * The resulting basic set has all meaning about the dimensions removed.
823
 * In particular, dimensions that correspond to existential variables
824
 * in bmap and that are found to be fixed are not removed.
825
 */
826
static struct isl_basic_set *equalities_in_underlying_set(
827
            struct isl_basic_map *bmap)
828
75.8k
{
829
75.8k
  struct isl_mat *T1 = NULL;
830
75.8k
  struct isl_mat *T2 = NULL;
831
75.8k
  struct isl_basic_set *bset = NULL;
832
75.8k
  struct isl_basic_set *hull = NULL;
833
75.8k
834
75.8k
  bset = isl_basic_map_underlying_set(bmap);
835
75.8k
  if (!bset)
836
0
    return NULL;
837
75.8k
  
if (75.8k
bset->n_eq75.8k
)
838
18.3k
    bset = isl_basic_set_remove_equalities(bset, &T1, &T2);
839
75.8k
  if (!bset)
840
0
    goto error;
841
75.8k
842
75.8k
  hull = uset_affine_hull(bset);
843
75.8k
  if (!T2)
844
57.4k
    return hull;
845
75.8k
846
18.3k
  
if (18.3k
!hull18.3k
)
{0
847
0
    isl_mat_free(T1);
848
0
    isl_mat_free(T2);
849
18.3k
  } else {
850
18.3k
    struct isl_vec *sample = isl_vec_copy(hull->sample);
851
18.3k
    if (
sample && 18.3k
sample->size > 014.0k
)
852
14.0k
      sample = isl_mat_vec_product(T1, sample);
853
18.3k
    else
854
4.31k
      isl_mat_free(T1);
855
18.3k
    hull = isl_basic_set_preimage(hull, T2);
856
18.3k
    if (
hull18.3k
)
{18.3k
857
18.3k
      isl_vec_free(hull->sample);
858
18.3k
      hull->sample = sample;
859
18.3k
    } else
860
0
      isl_vec_free(sample);
861
18.3k
  }
862
18.3k
863
75.8k
  return hull;
864
75.8k
error:
865
0
  isl_mat_free(T1);
866
0
  isl_mat_free(T2);
867
0
  isl_basic_set_free(bset);
868
0
  isl_basic_set_free(hull);
869
75.8k
  return NULL;
870
75.8k
}
871
872
/* Detect and make explicit all equalities satisfied by the (integer)
873
 * points in bmap.
874
 */
875
__isl_give isl_basic_map *isl_basic_map_detect_equalities(
876
  __isl_take isl_basic_map *bmap)
877
529k
{
878
529k
  int i, j;
879
529k
  struct isl_basic_set *hull = NULL;
880
529k
881
529k
  if (!bmap)
882
0
    return NULL;
883
529k
  
if (529k
bmap->n_ineq == 0529k
)
884
354k
    return bmap;
885
175k
  
if (175k
ISL_F_ISSET175k
(bmap, ISL_BASIC_MAP_EMPTY))
886
0
    return bmap;
887
175k
  
if (175k
ISL_F_ISSET175k
(bmap, ISL_BASIC_MAP_ALL_EQUALITIES))
888
81.5k
    return bmap;
889
93.9k
  
if (93.9k
ISL_F_ISSET93.9k
(bmap, ISL_BASIC_MAP_RATIONAL))
890
18.1k
    return isl_basic_map_implicit_equalities(bmap);
891
93.9k
892
93.9k
  hull = equalities_in_underlying_set(isl_basic_map_copy(bmap));
893
75.8k
  if (!hull)
894
0
    goto error;
895
75.8k
  
if (75.8k
ISL_F_ISSET75.8k
(hull, ISL_BASIC_SET_EMPTY))
{137
896
137
    isl_basic_set_free(hull);
897
137
    return isl_basic_map_set_to_empty(bmap);
898
75.8k
  }
899
75.8k
  bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim), 0,
900
75.7k
          hull->n_eq, 0);
901
77.6k
  for (i = 0; 
i < hull->n_eq77.6k
;
++i1.90k
)
{1.90k
902
1.90k
    j = isl_basic_map_alloc_equality(bmap);
903
1.90k
    if (j < 0)
904
0
      goto error;
905
1.90k
    isl_seq_cpy(bmap->eq[j], hull->eq[i],
906
1.90k
        1 + isl_basic_set_total_dim(hull));
907
75.7k
  }
908
75.7k
  isl_vec_free(bmap->sample);
909
75.7k
  bmap->sample = isl_vec_copy(hull->sample);
910
75.7k
  isl_basic_set_free(hull);
911
75.7k
  ISL_F_SET(bmap, ISL_BASIC_MAP_NO_IMPLICIT | ISL_BASIC_MAP_ALL_EQUALITIES);
912
75.7k
  bmap = isl_basic_map_simplify(bmap);
913
75.7k
  return isl_basic_map_finalize(bmap);
914
75.7k
error:
915
0
  isl_basic_set_free(hull);
916
0
  isl_basic_map_free(bmap);
917
75.7k
  return NULL;
918
529k
}
919
920
__isl_give isl_basic_set *isl_basic_set_detect_equalities(
921
            __isl_take isl_basic_set *bset)
922
32.7k
{
923
32.7k
  return bset_from_bmap(
924
32.7k
    isl_basic_map_detect_equalities(bset_to_bmap(bset)));
925
32.7k
}
926
927
__isl_give isl_map *isl_map_detect_equalities(__isl_take isl_map *map)
928
159k
{
929
159k
  return isl_map_inline_foreach_basic_map(map,
930
159k
              &isl_basic_map_detect_equalities);
931
159k
}
932
933
__isl_give isl_set *isl_set_detect_equalities(__isl_take isl_set *set)
934
14.3k
{
935
14.3k
  return set_from_map(isl_map_detect_equalities(set_to_map(set)));
936
14.3k
}
937
938
/* Return the superset of "bmap" described by the equalities
939
 * satisfied by "bmap" that are already known.
940
 */
941
__isl_give isl_basic_map *isl_basic_map_plain_affine_hull(
942
  __isl_take isl_basic_map *bmap)
943
413k
{
944
413k
  bmap = isl_basic_map_cow(bmap);
945
413k
  if (bmap)
946
413k
    isl_basic_map_free_inequality(bmap, bmap->n_ineq);
947
413k
  bmap = isl_basic_map_finalize(bmap);
948
413k
  return bmap;
949
413k
}
950
951
/* Return the superset of "bset" described by the equalities
952
 * satisfied by "bset" that are already known.
953
 */
954
__isl_give isl_basic_set *isl_basic_set_plain_affine_hull(
955
  __isl_take isl_basic_set *bset)
956
32.7k
{
957
32.7k
  return isl_basic_map_plain_affine_hull(bset);
958
32.7k
}
959
960
/* After computing the rational affine hull (by detecting the implicit
961
 * equalities), we compute the additional equalities satisfied by
962
 * the integer points (if any) and add the original equalities back in.
963
 */
964
__isl_give isl_basic_map *isl_basic_map_affine_hull(
965
  __isl_take isl_basic_map *bmap)
966
309k
{
967
309k
  bmap = isl_basic_map_detect_equalities(bmap);
968
309k
  bmap = isl_basic_map_plain_affine_hull(bmap);
969
309k
  return bmap;
970
309k
}
971
972
struct isl_basic_set *isl_basic_set_affine_hull(struct isl_basic_set *bset)
973
7.96k
{
974
7.96k
  return bset_from_bmap(isl_basic_map_affine_hull(bset_to_bmap(bset)));
975
7.96k
}
976
977
/* Given a rational affine matrix "M", add stride constraints to "bmap"
978
 * that ensure that
979
 *
980
 *    M(x)
981
 *
982
 * is an integer vector.  The variables x include all the variables
983
 * of "bmap" except the unknown divs.
984
 *
985
 * If d is the common denominator of M, then we need to impose that
986
 *
987
 *    d M(x) = 0  mod d
988
 *
989
 * or
990
 *
991
 *    exists alpha : d M(x) = d alpha
992
 *
993
 * This function is similar to add_strides in isl_morph.c
994
 */
995
static __isl_give isl_basic_map *add_strides(__isl_take isl_basic_map *bmap,
996
  __isl_keep isl_mat *M, int n_known)
997
0
{
998
0
  int i, div, k;
999
0
  isl_int gcd;
1000
0
1001
0
  if (isl_int_is_one(M->row[0][0]))
1002
0
    return bmap;
1003
0
1004
0
  bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
1005
0
          M->n_row - 1, M->n_row - 1, 0);
1006
0
1007
0
  isl_int_init(gcd);
1008
0
  for (i = 1; 
i < M->n_row0
;
++i0
)
{0
1009
0
    isl_seq_gcd(M->row[i], M->n_col, &gcd);
1010
0
    if (isl_int_is_divisible_by(gcd, M->row[0][0]))
1011
0
      continue;
1012
0
    div = isl_basic_map_alloc_div(bmap);
1013
0
    if (div < 0)
1014
0
      goto error;
1015
0
    
isl_int_set_si0
(bmap->div[div][0], 0);0
1016
0
    k = isl_basic_map_alloc_equality(bmap);
1017
0
    if (k < 0)
1018
0
      goto error;
1019
0
    isl_seq_cpy(bmap->eq[k], M->row[i], M->n_col);
1020
0
    isl_seq_clr(bmap->eq[k] + M->n_col, bmap->n_div - n_known);
1021
0
    isl_int_set(bmap->eq[k][M->n_col - n_known + div],
1022
0
          M->row[0][0]);
1023
0
  }
1024
0
  
isl_int_clear0
(gcd);0
1025
0
1026
0
  return bmap;
1027
0
error:
1028
0
  isl_int_clear(gcd);
1029
0
  isl_basic_map_free(bmap);
1030
0
  return NULL;
1031
0
}
1032
1033
/* If there are any equalities that involve (multiple) unknown divs,
1034
 * then extract the stride information encoded by those equalities
1035
 * and make it explicitly available in "bmap".
1036
 *
1037
 * We first sort the divs so that the unknown divs appear last and
1038
 * then we count how many equalities involve these divs.
1039
 *
1040
 * Let these equalities be of the form
1041
 *
1042
 *    A(x) + B y = 0
1043
 *
1044
 * where y represents the unknown divs and x the remaining variables.
1045
 * Let [H 0] be the Hermite Normal Form of B, i.e.,
1046
 *
1047
 *    B = [H 0] Q
1048
 *
1049
 * Then x is a solution of the equalities iff
1050
 *
1051
 *    H^-1 A(x) (= - [I 0] Q y)
1052
 *
1053
 * is an integer vector.  Let d be the common denominator of H^-1.
1054
 * We impose
1055
 *
1056
 *    d H^-1 A(x) = d alpha
1057
 *
1058
 * in add_strides, with alpha fresh existentially quantified variables.
1059
 */
1060
static __isl_give isl_basic_map *isl_basic_map_make_strides_explicit(
1061
  __isl_take isl_basic_map *bmap)
1062
301k
{
1063
301k
  int known;
1064
301k
  int n_known;
1065
301k
  int n, n_col;
1066
301k
  int total;
1067
301k
  isl_ctx *ctx;
1068
301k
  isl_mat *A, *B, *M;
1069
301k
1070
301k
  known = isl_basic_map_divs_known(bmap);
1071
301k
  if (known < 0)
1072
0
    return isl_basic_map_free(bmap);
1073
301k
  
if (301k
known301k
)
1074
301k
    return bmap;
1075
301k
  bmap = isl_basic_map_sort_divs(bmap);
1076
0
  bmap = isl_basic_map_gauss(bmap, NULL);
1077
0
  if (!bmap)
1078
0
    return NULL;
1079
0
1080
0
  
for (n_known = 0; 0
n_known < bmap->n_div0
;
++n_known0
)
1081
0
    
if (0
isl_int_is_zero0
(bmap->div[n_known][0]))
1082
0
      break;
1083
0
  ctx = isl_basic_map_get_ctx(bmap);
1084
0
  total = isl_space_dim(bmap->dim, isl_dim_all);
1085
0
  for (n = 0; 
n < bmap->n_eq0
;
++n0
)
1086
0
    
if (0
isl_seq_first_non_zero(bmap->eq[n] + 1 + total + n_known,0
1087
0
              bmap->n_div - n_known) == -1)
1088
0
      break;
1089
0
  if (n == 0)
1090
0
    return bmap;
1091
0
  B = isl_mat_sub_alloc6(ctx, bmap->eq, 0, n, 0, 1 + total + n_known);
1092
0
  n_col = bmap->n_div - n_known;
1093
0
  A = isl_mat_sub_alloc6(ctx, bmap->eq, 0, n, 1 + total + n_known, n_col);
1094
0
  A = isl_mat_left_hermite(A, 0, NULL, NULL);
1095
0
  A = isl_mat_drop_cols(A, n, n_col - n);
1096
0
  A = isl_mat_lin_to_aff(A);
1097
0
  A = isl_mat_right_inverse(A);
1098
0
  B = isl_mat_insert_zero_rows(B, 0, 1);
1099
0
  B = isl_mat_set_element_si(B, 0, 0, 1);
1100
0
  M = isl_mat_product(A, B);
1101
0
  if (!M)
1102
0
    return isl_basic_map_free(bmap);
1103
0
  bmap = add_strides(bmap, M, n_known);
1104
0
  bmap = isl_basic_map_gauss(bmap, NULL);
1105
0
  isl_mat_free(M);
1106
0
1107
0
  return bmap;
1108
301k
}
1109
1110
/* Compute the affine hull of each basic map in "map" separately
1111
 * and make all stride information explicit so that we can remove
1112
 * all unknown divs without losing this information.
1113
 * The result is also guaranteed to be gaussed.
1114
 *
1115
 * In simple cases where a div is determined by an equality,
1116
 * calling isl_basic_map_gauss is enough to make the stride information
1117
 * explicit, as it will derive an explicit representation for the div
1118
 * from the equality.  If, however, the stride information
1119
 * is encoded through multiple unknown divs then we need to make
1120
 * some extra effort in isl_basic_map_make_strides_explicit.
1121
 */
1122
static __isl_give isl_map *isl_map_local_affine_hull(__isl_take isl_map *map)
1123
278k
{
1124
278k
  int i;
1125
278k
1126
278k
  map = isl_map_cow(map);
1127
278k
  if (!map)
1128
0
    return NULL;
1129
278k
1130
579k
  
for (i = 0; 278k
i < map->n579k
;
++i301k
)
{301k
1131
301k
    map->p[i] = isl_basic_map_affine_hull(map->p[i]);
1132
301k
    map->p[i] = isl_basic_map_gauss(map->p[i], NULL);
1133
301k
    map->p[i] = isl_basic_map_make_strides_explicit(map->p[i]);
1134
301k
    if (!map->p[i])
1135
0
      return isl_map_free(map);
1136
301k
  }
1137
278k
1138
278k
  return map;
1139
278k
}
1140
1141
static __isl_give isl_set *isl_set_local_affine_hull(__isl_take isl_set *set)
1142
139k
{
1143
139k
  return isl_map_local_affine_hull(set);
1144
139k
}
1145
1146
/* Return an empty basic map living in the same space as "map".
1147
 */
1148
static __isl_give isl_basic_map *replace_map_by_empty_basic_map(
1149
  __isl_take isl_map *map)
1150
0
{
1151
0
  isl_space *space;
1152
0
1153
0
  space = isl_map_get_space(map);
1154
0
  isl_map_free(map);
1155
0
  return isl_basic_map_empty(space);
1156
0
}
1157
1158
/* Compute the affine hull of "map".
1159
 *
1160
 * We first compute the affine hull of each basic map separately.
1161
 * Then we align the divs and recompute the affine hulls of the basic
1162
 * maps since some of them may now have extra divs.
1163
 * In order to avoid performing parametric integer programming to
1164
 * compute explicit expressions for the divs, possible leading to
1165
 * an explosion in the number of basic maps, we first drop all unknown
1166
 * divs before aligning the divs.  Note that isl_map_local_affine_hull tries
1167
 * to make sure that all stride information is explicitly available
1168
 * in terms of known divs.  This involves calling isl_basic_set_gauss,
1169
 * which is also needed because affine_hull assumes its input has been gaussed,
1170
 * while isl_map_affine_hull may be called on input that has not been gaussed,
1171
 * in particular from initial_facet_constraint.
1172
 * Similarly, align_divs may reorder some divs so that we need to
1173
 * gauss the result again.
1174
 * Finally, we combine the individual affine hulls into a single
1175
 * affine hull.
1176
 */
1177
__isl_give isl_basic_map *isl_map_affine_hull(__isl_take isl_map *map)
1178
139k
{
1179
139k
  struct isl_basic_map *model = NULL;
1180
139k
  struct isl_basic_map *hull = NULL;
1181
139k
  struct isl_set *set;
1182
139k
  isl_basic_set *bset;
1183
139k
1184
139k
  map = isl_map_detect_equalities(map);
1185
139k
  map = isl_map_local_affine_hull(map);
1186
139k
  map = isl_map_remove_empty_parts(map);
1187
139k
  map = isl_map_remove_unknown_divs(map);
1188
139k
  map = isl_map_align_divs_internal(map);
1189
139k
1190
139k
  if (!map)
1191
0
    return NULL;
1192
139k
1193
139k
  
if (139k
map->n == 0139k
)
1194
0
    return replace_map_by_empty_basic_map(map);
1195
139k
1196
139k
  model = isl_basic_map_copy(map->p[0]);
1197
139k
  set = isl_map_underlying_set(map);
1198
139k
  set = isl_set_cow(set);
1199
139k
  set = isl_set_local_affine_hull(set);
1200
139k
  if (!set)
1201
0
    goto error;
1202
139k
1203
149k
  
while (139k
set->n > 1149k
)
1204
10.7k
    set->p[0] = affine_hull(set->p[0], set->p[--set->n]);
1205
139k
1206
139k
  bset = isl_basic_set_copy(set->p[0]);
1207
139k
  hull = isl_basic_map_overlying_set(bset, model);
1208
139k
  isl_set_free(set);
1209
139k
  hull = isl_basic_map_simplify(hull);
1210
139k
  return isl_basic_map_finalize(hull);
1211
139k
error:
1212
0
  isl_basic_map_free(model);
1213
0
  isl_set_free(set);
1214
139k
  return NULL;
1215
139k
}
1216
1217
struct isl_basic_set *isl_set_affine_hull(struct isl_set *set)
1218
136k
{
1219
136k
  return bset_from_bmap(isl_map_affine_hull(set_to_map(set)));
1220
136k
}