Coverage Report

Created: 2017-04-27 19:33

/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
struct isl_basic_map *isl_basic_map_implicit_equalities(
33
            struct isl_basic_map *bmap)
34
163k
{
35
163k
  struct isl_tab *tab;
36
163k
37
163k
  if (!bmap)
38
0
    return bmap;
39
163k
40
163k
  bmap = isl_basic_map_gauss(bmap, NULL);
41
163k
  if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
42
785
    return bmap;
43
163k
  
if (163k
ISL_F_ISSET163k
(bmap, ISL_BASIC_MAP_NO_IMPLICIT))
44
5.13k
    return bmap;
45
158k
  
if (158k
bmap->n_ineq <= 1158k
)
46
20.6k
    return bmap;
47
158k
48
137k
  tab = isl_tab_from_basic_map(bmap, 0);
49
137k
  if (isl_tab_detect_implicit_equalities(tab) < 0)
50
0
    goto error;
51
137k
  bmap = isl_basic_map_update_from_tab(bmap, tab);
52
137k
  isl_tab_free(tab);
53
137k
  bmap = isl_basic_map_gauss(bmap, NULL);
54
137k
  ISL_F_SET(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
55
137k
  return bmap;
56
0
error:
57
0
  isl_tab_free(tab);
58
0
  isl_basic_map_free(bmap);
59
0
  return NULL;
60
137k
}
61
62
struct isl_basic_set *isl_basic_set_implicit_equalities(
63
            struct isl_basic_set *bset)
64
145k
{
65
145k
  return bset_from_bmap(
66
145k
    isl_basic_map_implicit_equalities(bset_to_bmap(bset)));
67
145k
}
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
208k
{
78
208k
  isl_int m, c;
79
208k
80
208k
  if (isl_int_eq(bset1->eq[row][col], bset2->eq[row][col]))
81
177k
    return;
82
208k
83
31.0k
  
isl_int_init31.0k
(c);31.0k
84
31.0k
  isl_int_init(m);
85
31.0k
  isl_int_lcm(m, bset1->eq[row][col], bset2->eq[row][col]);
86
31.0k
  isl_int_divexact(c, m, bset1->eq[row][col]);
87
31.0k
  isl_seq_scale(bset1->eq[row], bset1->eq[row], c, col+1);
88
31.0k
  isl_int_divexact(c, m, bset2->eq[row][col]);
89
31.0k
  isl_seq_scale(bset2->eq[row], bset2->eq[row], c, col+1);
90
31.0k
  isl_int_clear(c);
91
31.0k
  isl_int_clear(m);
92
31.0k
}
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
309k
{
98
309k
  isl_int *t;
99
309k
  int r;
100
309k
101
309k
  t = bset->eq[row];
102
309k
  bset->n_eq--;
103
573k
  for (r = row; 
r < bset->n_eq573k
;
++r264k
)
104
264k
    bset->eq[r] = bset->eq[r+1];
105
309k
  bset->eq[bset->n_eq] = t;
106
309k
}
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
127k
{
121
127k
  int r;
122
127k
  isl_int a;
123
127k
  isl_int b;
124
127k
  unsigned total;
125
127k
126
127k
  isl_int_init(a);
127
127k
  isl_int_init(b);
128
127k
  total = 1 + isl_basic_set_n_dim(bset1);
129
381k
  for (r = 0; 
r < row381k
;
++r253k
)
{253k
130
253k
    if (isl_int_is_zero(bset2->eq[r][col]))
131
242k
      continue;
132
11.4k
    
isl_int_gcd11.4k
(b, bset2->eq[r][col], bset1->eq[row][col]);11.4k
133
11.4k
    isl_int_divexact(a, bset1->eq[row][col], b);
134
11.4k
    isl_int_divexact(b, bset2->eq[r][col], b);
135
11.4k
    isl_seq_combine(bset1->eq[r], a, bset1->eq[r],
136
11.4k
                b, bset1->eq[row], total);
137
11.4k
    isl_seq_scale(bset2->eq[r], bset2->eq[r], a, total);
138
11.4k
  }
139
127k
  isl_int_clear(a);
140
127k
  isl_int_clear(b);
141
127k
  delete_row(bset1, row);
142
127k
}
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
133k
{
157
133k
  int i, t;
158
133k
  isl_int a, b, g;
159
133k
  unsigned total;
160
133k
161
157k
  for (t = row-1; 
t >= 0157k
;
--t24.2k
)
162
115k
    
if (115k
isl_int_ne115k
(bset1->eq[t][col], bset2->eq[t][col]))
163
90.7k
      break;
164
133k
  if (t < 0)
165
42.3k
    return 0;
166
133k
167
90.7k
  total = 1 + isl_basic_set_n_dim(bset1);
168
90.7k
  isl_int_init(a);
169
90.7k
  isl_int_init(b);
170
90.7k
  isl_int_init(g);
171
90.7k
  isl_int_sub(b, bset1->eq[t][col], bset2->eq[t][col]);
172
190k
  for (i = 0; 
i < t190k
;
++i99.4k
)
{99.4k
173
99.4k
    isl_int_sub(a, bset2->eq[i][col], bset1->eq[i][col]);
174
99.4k
    isl_int_gcd(g, a, b);
175
99.4k
    isl_int_divexact(a, a, g);
176
99.4k
    isl_int_divexact(g, b, g);
177
99.4k
    isl_seq_combine(bset1->eq[i], g, bset1->eq[i], a, bset1->eq[t],
178
99.4k
        total);
179
99.4k
    isl_seq_combine(bset2->eq[i], g, bset2->eq[i], a, bset2->eq[t],
180
99.4k
        total);
181
99.4k
  }
182
90.7k
  isl_int_clear(a);
183
90.7k
  isl_int_clear(b);
184
90.7k
  isl_int_clear(g);
185
90.7k
  delete_row(bset1, t);
186
90.7k
  delete_row(bset2, t);
187
90.7k
  return 1;
188
133k
}
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
103k
{
198
103k
  unsigned total;
199
103k
  int col;
200
103k
  int row;
201
103k
202
103k
  if (
!bset1 || 103k
!bset2103k
)
203
0
    goto error;
204
103k
205
103k
  total = 1 + isl_basic_set_n_dim(bset1);
206
103k
207
103k
  row = 0;
208
572k
  for (col = total-1; 
col >= 0572k
;
--col469k
)
{469k
209
469k
    int is_zero1 = row >= bset1->n_eq ||
210
230k
      isl_int_is_zero(bset1->eq[row][col]);
211
469k
    int is_zero2 = row >= bset2->n_eq ||
212
335k
      isl_int_is_zero(bset2->eq[row][col]);
213
469k
    if (
!is_zero1 && 469k
!is_zero2210k
)
{208k
214
208k
      set_common_multiple(bset1, bset2, row, col);
215
208k
      ++row;
216
260k
    } else 
if (260k
!is_zero1 && 260k
is_zero21.54k
)
{1.54k
217
1.54k
      construct_column(bset1, bset2, row, col);
218
259k
    } else 
if (259k
is_zero1 && 259k
!is_zero2259k
)
{126k
219
126k
      construct_column(bset2, bset1, row, col);
220
133k
    } else {
221
133k
      if (transform_column(bset1, bset2, row, col))
222
90.7k
        --row;
223
133k
    }
224
469k
  }
225
103k
  isl_assert(bset1->ctx, row == bset1->n_eq, goto error);
226
103k
  isl_basic_set_free(bset2);
227
103k
  bset1 = isl_basic_set_normalize_constraints(bset1);
228
103k
  return bset1;
229
0
error:
230
0
  isl_basic_set_free(bset1);
231
0
  isl_basic_set_free(bset2);
232
0
  return NULL;
233
103k
}
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
10.6k
{
252
10.6k
  struct isl_ctx *ctx;
253
10.6k
  struct isl_vec *sample = NULL;
254
10.6k
  struct isl_tab_undo *snap;
255
10.6k
  unsigned dim;
256
10.6k
257
10.6k
  if (!tab)
258
0
    return NULL;
259
10.6k
  ctx = tab->mat->ctx;
260
10.6k
261
10.6k
  dim = tab->n_var;
262
10.6k
  sample = isl_vec_alloc(ctx, 1 + dim);
263
10.6k
  if (!sample)
264
0
    return NULL;
265
10.6k
  
isl_int_set_si10.6k
(sample->el[0], 1);10.6k
266
10.6k
  isl_seq_combine(sample->el + 1,
267
10.6k
    ctx->one, tab->bmap->sample->el + 1,
268
7.39k
    up ? 
ctx->one7.39k
:
ctx->negone3.24k
, eq + 1, dim);
269
10.6k
  if (isl_basic_map_contains(tab->bmap, sample))
270
5
    return sample;
271
10.6k
  isl_vec_free(sample);
272
10.6k
  sample = NULL;
273
10.6k
274
10.6k
  snap = isl_tab_snap(tab);
275
10.6k
276
10.6k
  if (!up)
277
3.24k
    isl_seq_neg(eq, eq, 1 + dim);
278
10.6k
  isl_int_sub_ui(eq[0], eq[0], 1);
279
10.6k
280
10.6k
  if (isl_tab_extend_cons(tab, 1) < 0)
281
0
    goto error;
282
10.6k
  
if (10.6k
isl_tab_add_ineq(tab, eq) < 010.6k
)
283
0
    goto error;
284
10.6k
285
10.6k
  sample = isl_tab_sample(tab);
286
10.6k
287
10.6k
  isl_int_add_ui(eq[0], eq[0], 1);
288
10.6k
  if (!up)
289
3.24k
    isl_seq_neg(eq, eq, 1 + dim);
290
10.6k
291
10.6k
  if (
sample && 10.6k
isl_tab_rollback(tab, snap) < 010.6k
)
292
0
    goto error;
293
10.6k
294
10.6k
  return sample;
295
0
error:
296
0
  isl_vec_free(sample);
297
0
  return NULL;
298
10.6k
}
299
300
struct isl_basic_set *isl_basic_set_recession_cone(struct isl_basic_set *bset)
301
145k
{
302
145k
  int i;
303
145k
304
145k
  bset = isl_basic_set_cow(bset);
305
145k
  if (!bset)
306
0
    return NULL;
307
145k
  
isl_assert145k
(bset->ctx, bset->n_div == 0, goto error);145k
308
145k
309
145k
  
for (i = 0; 145k
i < bset->n_eq145k
;
++i195
)
310
195
    isl_int_set_si(bset->eq[i][0], 0);
311
145k
312
867k
  for (i = 0; 
i < bset->n_ineq867k
;
++i721k
)
313
721k
    isl_int_set_si(bset->ineq[i][0], 0);
314
145k
315
145k
  ISL_F_CLR(bset, ISL_BASIC_SET_NO_IMPLICIT);
316
145k
  return isl_basic_set_implicit_equalities(bset);
317
0
error:
318
0
  isl_basic_set_free(bset);
319
0
  return NULL;
320
145k
}
321
322
/* Move "sample" to a point that is one up (or down) from the original
323
 * point in dimension "pos".
324
 */
325
static void adjacent_point(__isl_keep isl_vec *sample, int pos, int up)
326
411k
{
327
411k
  if (up)
328
205k
    isl_int_add_ui(sample->el[1 + pos], sample->el[1 + pos], 1);
329
411k
  else
330
205k
    isl_int_sub_ui(sample->el[1 + pos], sample->el[1 + pos], 1);
331
411k
}
332
333
/* Check if any points that are adjacent to "sample" also belong to "bset".
334
 * If so, add them to "hull" and return the updated hull.
335
 *
336
 * Before checking whether and adjacent point belongs to "bset", we first
337
 * check whether it already belongs to "hull" as this test is typically
338
 * much cheaper.
339
 */
340
static __isl_give isl_basic_set *add_adjacent_points(
341
  __isl_take isl_basic_set *hull, __isl_take isl_vec *sample,
342
  __isl_keep isl_basic_set *bset)
343
50.0k
{
344
50.0k
  int i, up;
345
50.0k
  int dim;
346
50.0k
347
50.0k
  if (!sample)
348
0
    goto error;
349
50.0k
350
50.0k
  dim = isl_basic_set_dim(hull, isl_dim_set);
351
50.0k
352
160k
  for (i = 0; 
i < dim160k
;
++i110k
)
{110k
353
228k
    for (up = 0; 
up <= 1228k
;
++up117k
)
{205k
354
205k
      int contains;
355
205k
      isl_basic_set *point;
356
205k
357
205k
      adjacent_point(sample, i, up);
358
205k
      contains = isl_basic_set_contains(hull, sample);
359
205k
      if (contains < 0)
360
0
        goto error;
361
205k
      
if (205k
contains205k
)
{23.0k
362
23.0k
        adjacent_point(sample, i, !up);
363
23.0k
        continue;
364
23.0k
      }
365
182k
      contains = isl_basic_set_contains(bset, sample);
366
182k
      if (contains < 0)
367
0
        goto error;
368
182k
      
if (182k
contains182k
)
{88.0k
369
88.0k
        point = isl_basic_set_from_vec(
370
88.0k
              isl_vec_copy(sample));
371
88.0k
        hull = affine_hull(hull, point);
372
88.0k
      }
373
182k
      adjacent_point(sample, i, !up);
374
182k
      if (contains)
375
88.0k
        break;
376
182k
    }
377
110k
  }
378
50.0k
379
50.0k
  isl_vec_free(sample);
380
50.0k
381
50.0k
  return hull;
382
0
error:
383
0
  isl_vec_free(sample);
384
0
  isl_basic_set_free(hull);
385
0
  return NULL;
386
50.0k
}
387
388
/* Extend an initial (under-)approximation of the affine hull of basic
389
 * set represented by the tableau "tab"
390
 * by looking for points that do not satisfy one of the equalities
391
 * in the current approximation and adding them to that approximation
392
 * until no such points can be found any more.
393
 *
394
 * The caller of this function ensures that "tab" is bounded or
395
 * that tab->basis and tab->n_unbounded have been set appropriately.
396
 *
397
 * "bset" may be either NULL or the basic set represented by "tab".
398
 * If "bset" is not NULL, we check for any point we find if any
399
 * of its adjacent points also belong to "bset".
400
 */
401
static __isl_give isl_basic_set *extend_affine_hull(struct isl_tab *tab,
402
  __isl_take isl_basic_set *hull, __isl_keep isl_basic_set *bset)
403
45.5k
{
404
45.5k
  int i, j;
405
45.5k
  unsigned dim;
406
45.5k
407
45.5k
  if (
!tab || 45.5k
!hull45.5k
)
408
0
    goto error;
409
45.5k
410
45.5k
  dim = tab->n_var;
411
45.5k
412
45.5k
  if (isl_tab_extend_cons(tab, 2 * dim + 1) < 0)
413
0
    goto error;
414
45.5k
415
50.8k
  
for (i = 0; 45.5k
i < dim50.8k
;
++i5.26k
)
{50.7k
416
50.7k
    struct isl_vec *sample;
417
50.7k
    struct isl_basic_set *point;
418
52.9k
    for (j = 0; 
j < hull->n_eq52.9k
;
++j2.12k
)
{7.39k
419
7.39k
      sample = outside_point(tab, hull->eq[j], 1);
420
7.39k
      if (!sample)
421
0
        goto error;
422
7.39k
      
if (7.39k
sample->size > 07.39k
)
423
4.14k
        break;
424
3.24k
      isl_vec_free(sample);
425
3.24k
      sample = outside_point(tab, hull->eq[j], 0);
426
3.24k
      if (!sample)
427
0
        goto error;
428
3.24k
      
if (3.24k
sample->size > 03.24k
)
429
1.11k
        break;
430
2.12k
      isl_vec_free(sample);
431
2.12k
432
2.12k
      if (isl_tab_add_eq(tab, hull->eq[j]) < 0)
433
0
        goto error;
434
2.12k
    }
435
50.7k
    
if (50.7k
j == hull->n_eq50.7k
)
436
45.5k
      break;
437
5.26k
    
if (5.26k
tab->samples &&5.26k
438
389
        isl_tab_add_sample(tab, isl_vec_copy(sample)) < 0)
439
0
      hull = isl_basic_set_free(hull);
440
5.26k
    if (bset)
441
4.87k
      hull = add_adjacent_points(hull, isl_vec_copy(sample),
442
4.87k
                bset);
443
5.26k
    point = isl_basic_set_from_vec(sample);
444
5.26k
    hull = affine_hull(hull, point);
445
5.26k
    if (!hull)
446
0
      return NULL;
447
5.26k
  }
448
45.5k
449
45.5k
  return hull;
450
0
error:
451
0
  isl_basic_set_free(hull);
452
0
  return NULL;
453
45.5k
}
454
455
/* Construct an initial underapproximation of the hull of "bset"
456
 * from "sample" and any of its adjacent points that also belong to "bset".
457
 */
458
static __isl_give isl_basic_set *initialize_hull(__isl_keep isl_basic_set *bset,
459
  __isl_take isl_vec *sample)
460
45.1k
{
461
45.1k
  isl_basic_set *hull;
462
45.1k
463
45.1k
  hull = isl_basic_set_from_vec(isl_vec_copy(sample));
464
45.1k
  hull = add_adjacent_points(hull, sample, bset);
465
45.1k
466
45.1k
  return hull;
467
45.1k
}
468
469
/* Look for all equalities satisfied by the integer points in bset,
470
 * which is assumed to be bounded.
471
 *
472
 * The equalities are obtained by successively looking for
473
 * a point that is affinely independent of the points found so far.
474
 * In particular, for each equality satisfied by the points so far,
475
 * we check if there is any point on a hyperplane parallel to the
476
 * corresponding hyperplane shifted by at least one (in either direction).
477
 */
478
static struct isl_basic_set *uset_affine_hull_bounded(struct isl_basic_set *bset)
479
45.2k
{
480
45.2k
  struct isl_vec *sample = NULL;
481
45.2k
  struct isl_basic_set *hull;
482
45.2k
  struct isl_tab *tab = NULL;
483
45.2k
  unsigned dim;
484
45.2k
485
45.2k
  if (isl_basic_set_plain_is_empty(bset))
486
0
    return bset;
487
45.2k
488
45.2k
  dim = isl_basic_set_n_dim(bset);
489
45.2k
490
45.2k
  if (
bset->sample && 45.2k
bset->sample->size == 1 + dim22.9k
)
{15.6k
491
15.6k
    int contains = isl_basic_set_contains(bset, bset->sample);
492
15.6k
    if (contains < 0)
493
0
      goto error;
494
15.6k
    
if (15.6k
contains15.6k
)
{15.4k
495
15.4k
      if (dim == 0)
496
0
        return bset;
497
15.4k
      sample = isl_vec_copy(bset->sample);
498
221
    } else {
499
221
      isl_vec_free(bset->sample);
500
221
      bset->sample = NULL;
501
221
    }
502
15.6k
  }
503
45.2k
504
45.2k
  tab = isl_tab_from_basic_set(bset, 1);
505
45.2k
  if (!tab)
506
0
    goto error;
507
45.2k
  
if (45.2k
tab->empty45.2k
)
{78
508
78
    isl_tab_free(tab);
509
78
    isl_vec_free(sample);
510
78
    return isl_basic_set_set_to_empty(bset);
511
78
  }
512
45.2k
513
45.1k
  
if (45.1k
!sample45.1k
)
{29.7k
514
29.7k
    struct isl_tab_undo *snap;
515
29.7k
    snap = isl_tab_snap(tab);
516
29.7k
    sample = isl_tab_sample(tab);
517
29.7k
    if (isl_tab_rollback(tab, snap) < 0)
518
0
      goto error;
519
29.7k
    isl_vec_free(tab->bmap->sample);
520
29.7k
    tab->bmap->sample = isl_vec_copy(sample);
521
29.7k
  }
522
45.1k
523
45.1k
  
if (45.1k
!sample45.1k
)
524
0
    goto error;
525
45.1k
  
if (45.1k
sample->size == 045.1k
)
{2
526
2
    isl_tab_free(tab);
527
2
    isl_vec_free(sample);
528
2
    return isl_basic_set_set_to_empty(bset);
529
2
  }
530
45.1k
531
45.1k
  hull = initialize_hull(bset, sample);
532
45.1k
533
45.1k
  hull = extend_affine_hull(tab, hull, bset);
534
45.1k
  isl_basic_set_free(bset);
535
45.1k
  isl_tab_free(tab);
536
45.1k
537
45.1k
  return hull;
538
0
error:
539
0
  isl_vec_free(sample);
540
0
  isl_tab_free(tab);
541
0
  isl_basic_set_free(bset);
542
0
  return NULL;
543
45.1k
}
544
545
/* Given an unbounded tableau and an integer point satisfying the tableau,
546
 * construct an initial affine hull containing the recession cone
547
 * shifted to the given point.
548
 *
549
 * The unbounded directions are taken from the last rows of the basis,
550
 * which is assumed to have been initialized appropriately.
551
 */
552
static __isl_give isl_basic_set *initial_hull(struct isl_tab *tab,
553
  __isl_take isl_vec *vec)
554
324
{
555
324
  int i;
556
324
  int k;
557
324
  struct isl_basic_set *bset = NULL;
558
324
  struct isl_ctx *ctx;
559
324
  unsigned dim;
560
324
561
324
  if (
!vec || 324
!tab324
)
562
0
    return NULL;
563
324
  ctx = vec->ctx;
564
324
  isl_assert(ctx, vec->size != 0, goto error);
565
324
566
324
  bset = isl_basic_set_alloc(ctx, 0, vec->size - 1, 0, vec->size - 1, 0);
567
324
  if (!bset)
568
0
    goto error;
569
324
  dim = isl_basic_set_n_dim(bset) - tab->n_unbounded;
570
1.77k
  for (i = 0; 
i < dim1.77k
;
++i1.44k
)
{1.44k
571
1.44k
    k = isl_basic_set_alloc_equality(bset);
572
1.44k
    if (k < 0)
573
0
      goto error;
574
1.44k
    isl_seq_cpy(bset->eq[k] + 1, tab->basis->row[1 + i] + 1,
575
1.44k
          vec->size - 1);
576
1.44k
    isl_seq_inner_product(bset->eq[k] + 1, vec->el +1,
577
1.44k
              vec->size - 1, &bset->eq[k][0]);
578
1.44k
    isl_int_neg(bset->eq[k][0], bset->eq[k][0]);
579
1.44k
  }
580
324
  bset->sample = vec;
581
324
  bset = isl_basic_set_gauss(bset, NULL);
582
324
583
324
  return bset;
584
0
error:
585
0
  isl_basic_set_free(bset);
586
0
  isl_vec_free(vec);
587
0
  return NULL;
588
324
}
589
590
/* Given a tableau of a set and a tableau of the corresponding
591
 * recession cone, detect and add all equalities to the tableau.
592
 * If the tableau is bounded, then we can simply keep the
593
 * tableau in its state after the return from extend_affine_hull.
594
 * However, if the tableau is unbounded, then
595
 * isl_tab_set_initial_basis_with_cone will add some additional
596
 * constraints to the tableau that have to be removed again.
597
 * In this case, we therefore rollback to the state before
598
 * any constraints were added and then add the equalities back in.
599
 */
600
struct isl_tab *isl_tab_detect_equalities(struct isl_tab *tab,
601
  struct isl_tab *tab_cone)
602
370
{
603
370
  int j;
604
370
  struct isl_vec *sample;
605
370
  struct isl_basic_set *hull = NULL;
606
370
  struct isl_tab_undo *snap;
607
370
608
370
  if (
!tab || 370
!tab_cone370
)
609
0
    goto error;
610
370
611
370
  snap = isl_tab_snap(tab);
612
370
613
370
  isl_mat_free(tab->basis);
614
370
  tab->basis = NULL;
615
370
616
370
  isl_assert(tab->mat->ctx, tab->bmap, goto error);
617
370
  
isl_assert370
(tab->mat->ctx, tab->samples, goto error);370
618
370
  
isl_assert370
(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var, goto error);370
619
370
  
isl_assert370
(tab->mat->ctx, tab->n_sample > tab->n_outside, goto error);370
620
370
621
370
  
if (370
isl_tab_set_initial_basis_with_cone(tab, tab_cone) < 0370
)
622
0
    goto error;
623
370
624
370
  sample = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
625
370
  if (!sample)
626
0
    goto error;
627
370
628
370
  isl_seq_cpy(sample->el, tab->samples->row[tab->n_outside], sample->size);
629
370
630
370
  isl_vec_free(tab->bmap->sample);
631
370
  tab->bmap->sample = isl_vec_copy(sample);
632
370
633
370
  if (tab->n_unbounded == 0)
634
46
    hull = isl_basic_set_from_vec(isl_vec_copy(sample));
635
370
  else
636
324
    hull = initial_hull(tab, isl_vec_copy(sample));
637
370
638
625
  for (j = tab->n_outside + 1; 
j < tab->n_sample625
;
++j255
)
{255
639
255
    isl_seq_cpy(sample->el, tab->samples->row[j], sample->size);
640
255
    hull = affine_hull(hull,
641
255
        isl_basic_set_from_vec(isl_vec_copy(sample)));
642
255
  }
643
370
644
370
  isl_vec_free(sample);
645
370
646
370
  hull = extend_affine_hull(tab, hull, NULL);
647
370
  if (!hull)
648
0
    goto error;
649
370
650
370
  
if (370
tab->n_unbounded == 0370
)
{46
651
46
    isl_basic_set_free(hull);
652
46
    return tab;
653
46
  }
654
370
655
324
  
if (324
isl_tab_rollback(tab, snap) < 0324
)
656
0
    goto error;
657
324
658
324
  
if (324
hull->n_eq > tab->n_zero324
)
{162
659
942
    for (j = 0; 
j < hull->n_eq942
;
++j780
)
{780
660
780
      isl_seq_normalize(tab->mat->ctx, hull->eq[j], 1 + tab->n_var);
661
780
      if (isl_tab_add_eq(tab, hull->eq[j]) < 0)
662
0
        goto error;
663
780
    }
664
162
  }
665
324
666
324
  isl_basic_set_free(hull);
667
324
668
324
  return tab;
669
0
error:
670
0
  isl_basic_set_free(hull);
671
0
  isl_tab_free(tab);
672
0
  return NULL;
673
324
}
674
675
/* Compute the affine hull of "bset", where "cone" is the recession cone
676
 * of "bset".
677
 *
678
 * We first compute a unimodular transformation that puts the unbounded
679
 * directions in the last dimensions.  In particular, we take a transformation
680
 * that maps all equalities to equalities (in HNF) on the first dimensions.
681
 * Let x be the original dimensions and y the transformed, with y_1 bounded
682
 * and y_2 unbounded.
683
 *
684
 *         [ y_1 ]      [ y_1 ]   [ Q_1 ]
685
 *  x = U  [ y_2 ]      [ y_2 ] = [ Q_2 ] x
686
 *
687
 * Let's call the input basic set S.  We compute S' = preimage(S, U)
688
 * and drop the final dimensions including any constraints involving them.
689
 * This results in set S''.
690
 * Then we compute the affine hull A'' of S''.
691
 * Let F y_1 >= g be the constraint system of A''.  In the transformed
692
 * space the y_2 are unbounded, so we can add them back without any constraints,
693
 * resulting in
694
 *
695
 *            [ y_1 ]
696
 *    [ F 0 ] [ y_2 ] >= g
697
 * or
698
 *            [ Q_1 ]
699
 *    [ F 0 ] [ Q_2 ] x >= g
700
 * or
701
 *    F Q_1 x >= g
702
 *
703
 * The affine hull in the original space is then obtained as
704
 * A = preimage(A'', Q_1).
705
 */
706
static struct isl_basic_set *affine_hull_with_cone(struct isl_basic_set *bset,
707
  struct isl_basic_set *cone)
708
36.9k
{
709
36.9k
  unsigned total;
710
36.9k
  unsigned cone_dim;
711
36.9k
  struct isl_basic_set *hull;
712
36.9k
  struct isl_mat *M, *U, *Q;
713
36.9k
714
36.9k
  if (
!bset || 36.9k
!cone36.9k
)
715
0
    goto error;
716
36.9k
717
36.9k
  total = isl_basic_set_total_dim(cone);
718
36.9k
  cone_dim = total - cone->n_eq;
719
36.9k
720
36.9k
  M = isl_mat_sub_alloc6(bset->ctx, cone->eq, 0, cone->n_eq, 1, total);
721
36.9k
  M = isl_mat_left_hermite(M, 0, &U, &Q);
722
36.9k
  if (!M)
723
0
    goto error;
724
36.9k
  isl_mat_free(M);
725
36.9k
726
36.9k
  U = isl_mat_lin_to_aff(U);
727
36.9k
  bset = isl_basic_set_preimage(bset, isl_mat_copy(U));
728
36.9k
729
36.9k
  bset = isl_basic_set_drop_constraints_involving(bset, total - cone_dim,
730
36.9k
              cone_dim);
731
36.9k
  bset = isl_basic_set_drop_dims(bset, total - cone_dim, cone_dim);
732
36.9k
733
36.9k
  Q = isl_mat_lin_to_aff(Q);
734
36.9k
  Q = isl_mat_drop_rows(Q, 1 + total - cone_dim, cone_dim);
735
36.9k
736
36.9k
  if (
bset && 36.9k
bset->sample36.9k
&&
bset->sample->size == 1 + total16.5k
)
737
11.8k
    bset->sample = isl_mat_vec_product(isl_mat_copy(Q), bset->sample);
738
36.9k
739
36.9k
  hull = uset_affine_hull_bounded(bset);
740
36.9k
741
36.9k
  if (
!hull36.9k
)
{0
742
0
    isl_mat_free(Q);
743
0
    isl_mat_free(U);
744
36.9k
  } else {
745
36.9k
    struct isl_vec *sample = isl_vec_copy(hull->sample);
746
36.9k
    U = isl_mat_drop_cols(U, 1 + total - cone_dim, cone_dim);
747
36.9k
    if (
sample && 36.9k
sample->size > 036.9k
)
748
36.9k
      sample = isl_mat_vec_product(U, sample);
749
36.9k
    else
750
36
      isl_mat_free(U);
751
36.9k
    hull = isl_basic_set_preimage(hull, Q);
752
36.9k
    if (
hull36.9k
)
{36.9k
753
36.9k
      isl_vec_free(hull->sample);
754
36.9k
      hull->sample = sample;
755
36.9k
    } else
756
0
      isl_vec_free(sample);
757
36.9k
  }
758
36.9k
759
36.9k
  isl_basic_set_free(cone);
760
36.9k
761
36.9k
  return hull;
762
0
error:
763
0
  isl_basic_set_free(bset);
764
0
  isl_basic_set_free(cone);
765
0
  return NULL;
766
36.9k
}
767
768
/* Look for all equalities satisfied by the integer points in bset,
769
 * which is assumed not to have any explicit equalities.
770
 *
771
 * The equalities are obtained by successively looking for
772
 * a point that is affinely independent of the points found so far.
773
 * In particular, for each equality satisfied by the points so far,
774
 * we check if there is any point on a hyperplane parallel to the
775
 * corresponding hyperplane shifted by at least one (in either direction).
776
 *
777
 * Before looking for any outside points, we first compute the recession
778
 * cone.  The directions of this recession cone will always be part
779
 * of the affine hull, so there is no need for looking for any points
780
 * in these directions.
781
 * In particular, if the recession cone is full-dimensional, then
782
 * the affine hull is simply the whole universe.
783
 */
784
static struct isl_basic_set *uset_affine_hull(struct isl_basic_set *bset)
785
59.7k
{
786
59.7k
  struct isl_basic_set *cone;
787
59.7k
788
59.7k
  if (isl_basic_set_plain_is_empty(bset))
789
5
    return bset;
790
59.7k
791
59.7k
  cone = isl_basic_set_recession_cone(isl_basic_set_copy(bset));
792
59.7k
  if (!cone)
793
0
    goto error;
794
59.7k
  
if (59.7k
cone->n_eq == 059.7k
)
{14.4k
795
14.4k
    isl_space *space;
796
14.4k
    space = isl_basic_set_get_space(bset);
797
14.4k
    isl_basic_set_free(cone);
798
14.4k
    isl_basic_set_free(bset);
799
14.4k
    return isl_basic_set_universe(space);
800
14.4k
  }
801
59.7k
802
45.2k
  
if (45.2k
cone->n_eq < isl_basic_set_total_dim(cone)45.2k
)
803
36.9k
    return affine_hull_with_cone(bset, cone);
804
45.2k
805
8.27k
  isl_basic_set_free(cone);
806
8.27k
  return uset_affine_hull_bounded(bset);
807
0
error:
808
0
  isl_basic_set_free(bset);
809
0
  return NULL;
810
45.2k
}
811
812
/* Look for all equalities satisfied by the integer points in bmap
813
 * that are independent of the equalities already explicitly available
814
 * in bmap.
815
 *
816
 * We first remove all equalities already explicitly available,
817
 * then look for additional equalities in the reduced space
818
 * and then transform the result to the original space.
819
 * The original equalities are _not_ added to this set.  This is
820
 * the responsibility of the calling function.
821
 * The resulting basic set has all meaning about the dimensions removed.
822
 * In particular, dimensions that correspond to existential variables
823
 * in bmap and that are found to be fixed are not removed.
824
 */
825
static struct isl_basic_set *equalities_in_underlying_set(
826
            struct isl_basic_map *bmap)
827
59.7k
{
828
59.7k
  struct isl_mat *T1 = NULL;
829
59.7k
  struct isl_mat *T2 = NULL;
830
59.7k
  struct isl_basic_set *bset = NULL;
831
59.7k
  struct isl_basic_set *hull = NULL;
832
59.7k
833
59.7k
  bset = isl_basic_map_underlying_set(bmap);
834
59.7k
  if (!bset)
835
0
    return NULL;
836
59.7k
  
if (59.7k
bset->n_eq59.7k
)
837
12.3k
    bset = isl_basic_set_remove_equalities(bset, &T1, &T2);
838
59.7k
  if (!bset)
839
0
    goto error;
840
59.7k
841
59.7k
  hull = uset_affine_hull(bset);
842
59.7k
  if (!T2)
843
47.3k
    return hull;
844
59.7k
845
12.3k
  
if (12.3k
!hull12.3k
)
{0
846
0
    isl_mat_free(T1);
847
0
    isl_mat_free(T2);
848
12.3k
  } else {
849
12.3k
    struct isl_vec *sample = isl_vec_copy(hull->sample);
850
12.3k
    if (
sample && 12.3k
sample->size > 09.67k
)
851
9.67k
      sample = isl_mat_vec_product(T1, sample);
852
12.3k
    else
853
2.69k
      isl_mat_free(T1);
854
12.3k
    hull = isl_basic_set_preimage(hull, T2);
855
12.3k
    if (
hull12.3k
)
{12.3k
856
12.3k
      isl_vec_free(hull->sample);
857
12.3k
      hull->sample = sample;
858
12.3k
    } else
859
0
      isl_vec_free(sample);
860
12.3k
  }
861
12.3k
862
12.3k
  return hull;
863
0
error:
864
0
  isl_mat_free(T1);
865
0
  isl_mat_free(T2);
866
0
  isl_basic_set_free(bset);
867
0
  isl_basic_set_free(hull);
868
0
  return NULL;
869
59.7k
}
870
871
/* Detect and make explicit all equalities satisfied by the (integer)
872
 * points in bmap.
873
 */
874
struct isl_basic_map *isl_basic_map_detect_equalities(
875
            struct isl_basic_map *bmap)
876
454k
{
877
454k
  int i, j;
878
454k
  struct isl_basic_set *hull = NULL;
879
454k
880
454k
  if (!bmap)
881
0
    return NULL;
882
454k
  
if (454k
bmap->n_ineq == 0454k
)
883
313k
    return bmap;
884
141k
  
if (141k
ISL_F_ISSET141k
(bmap, ISL_BASIC_MAP_EMPTY))
885
0
    return bmap;
886
141k
  
if (141k
ISL_F_ISSET141k
(bmap, ISL_BASIC_MAP_ALL_EQUALITIES))
887
63.4k
    return bmap;
888
77.8k
  
if (77.8k
ISL_F_ISSET77.8k
(bmap, ISL_BASIC_MAP_RATIONAL))
889
18.1k
    return isl_basic_map_implicit_equalities(bmap);
890
77.8k
891
59.7k
  hull = equalities_in_underlying_set(isl_basic_map_copy(bmap));
892
59.7k
  if (!hull)
893
0
    goto error;
894
59.7k
  
if (59.7k
ISL_F_ISSET59.7k
(hull, ISL_BASIC_SET_EMPTY))
{85
895
85
    isl_basic_set_free(hull);
896
85
    return isl_basic_map_set_to_empty(bmap);
897
85
  }
898
59.6k
  bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim), 0,
899
59.6k
          hull->n_eq, 0);
900
60.5k
  for (i = 0; 
i < hull->n_eq60.5k
;
++i906
)
{906
901
906
    j = isl_basic_map_alloc_equality(bmap);
902
906
    if (j < 0)
903
0
      goto error;
904
906
    isl_seq_cpy(bmap->eq[j], hull->eq[i],
905
906
        1 + isl_basic_set_total_dim(hull));
906
906
  }
907
59.6k
  isl_vec_free(bmap->sample);
908
59.6k
  bmap->sample = isl_vec_copy(hull->sample);
909
59.6k
  isl_basic_set_free(hull);
910
59.6k
  ISL_F_SET(bmap, ISL_BASIC_MAP_NO_IMPLICIT | ISL_BASIC_MAP_ALL_EQUALITIES);
911
59.6k
  bmap = isl_basic_map_simplify(bmap);
912
59.6k
  return isl_basic_map_finalize(bmap);
913
0
error:
914
0
  isl_basic_set_free(hull);
915
0
  isl_basic_map_free(bmap);
916
0
  return NULL;
917
59.6k
}
918
919
__isl_give isl_basic_set *isl_basic_set_detect_equalities(
920
            __isl_take isl_basic_set *bset)
921
27.8k
{
922
27.8k
  return bset_from_bmap(
923
27.8k
    isl_basic_map_detect_equalities(bset_to_bmap(bset)));
924
27.8k
}
925
926
__isl_give isl_map *isl_map_detect_equalities(__isl_take isl_map *map)
927
133k
{
928
133k
  return isl_map_inline_foreach_basic_map(map,
929
133k
              &isl_basic_map_detect_equalities);
930
133k
}
931
932
__isl_give isl_set *isl_set_detect_equalities(__isl_take isl_set *set)
933
7.73k
{
934
7.73k
  return set_from_map(isl_map_detect_equalities(set_to_map(set)));
935
7.73k
}
936
937
/* Return the superset of "bmap" described by the equalities
938
 * satisfied by "bmap" that are already known.
939
 */
940
__isl_give isl_basic_map *isl_basic_map_plain_affine_hull(
941
  __isl_take isl_basic_map *bmap)
942
364k
{
943
364k
  bmap = isl_basic_map_cow(bmap);
944
364k
  if (bmap)
945
364k
    isl_basic_map_free_inequality(bmap, bmap->n_ineq);
946
364k
  bmap = isl_basic_map_finalize(bmap);
947
364k
  return bmap;
948
364k
}
949
950
/* Return the superset of "bset" described by the equalities
951
 * satisfied by "bset" that are already known.
952
 */
953
__isl_give isl_basic_set *isl_basic_set_plain_affine_hull(
954
  __isl_take isl_basic_set *bset)
955
27.7k
{
956
27.7k
  return isl_basic_map_plain_affine_hull(bset);
957
27.7k
}
958
959
/* After computing the rational affine hull (by detecting the implicit
960
 * equalities), we compute the additional equalities satisfied by
961
 * the integer points (if any) and add the original equalities back in.
962
 */
963
struct isl_basic_map *isl_basic_map_affine_hull(struct isl_basic_map *bmap)
964
271k
{
965
271k
  bmap = isl_basic_map_detect_equalities(bmap);
966
271k
  bmap = isl_basic_map_plain_affine_hull(bmap);
967
271k
  return bmap;
968
271k
}
969
970
struct isl_basic_set *isl_basic_set_affine_hull(struct isl_basic_set *bset)
971
6.50k
{
972
6.50k
  return bset_from_bmap(isl_basic_map_affine_hull(bset_to_bmap(bset)));
973
6.50k
}
974
975
/* Given a rational affine matrix "M", add stride constraints to "bmap"
976
 * that ensure that
977
 *
978
 *    M(x)
979
 *
980
 * is an integer vector.  The variables x include all the variables
981
 * of "bmap" except the unknown divs.
982
 *
983
 * If d is the common denominator of M, then we need to impose that
984
 *
985
 *    d M(x) = 0  mod d
986
 *
987
 * or
988
 *
989
 *    exists alpha : d M(x) = d alpha
990
 *
991
 * This function is similar to add_strides in isl_morph.c
992
 */
993
static __isl_give isl_basic_map *add_strides(__isl_take isl_basic_map *bmap,
994
  __isl_keep isl_mat *M, int n_known)
995
0
{
996
0
  int i, div, k;
997
0
  isl_int gcd;
998
0
999
0
  if (isl_int_is_one(M->row[0][0]))
1000
0
    return bmap;
1001
0
1002
0
  bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
1003
0
          M->n_row - 1, M->n_row - 1, 0);
1004
0
1005
0
  isl_int_init(gcd);
1006
0
  for (i = 1; 
i < M->n_row0
;
++i0
)
{0
1007
0
    isl_seq_gcd(M->row[i], M->n_col, &gcd);
1008
0
    if (isl_int_is_divisible_by(gcd, M->row[0][0]))
1009
0
      continue;
1010
0
    div = isl_basic_map_alloc_div(bmap);
1011
0
    if (div < 0)
1012
0
      goto error;
1013
0
    
isl_int_set_si0
(bmap->div[div][0], 0);0
1014
0
    k = isl_basic_map_alloc_equality(bmap);
1015
0
    if (k < 0)
1016
0
      goto error;
1017
0
    isl_seq_cpy(bmap->eq[k], M->row[i], M->n_col);
1018
0
    isl_seq_clr(bmap->eq[k] + M->n_col, bmap->n_div - n_known);
1019
0
    isl_int_set(bmap->eq[k][M->n_col - n_known + div],
1020
0
          M->row[0][0]);
1021
0
  }
1022
0
  
isl_int_clear0
(gcd);0
1023
0
1024
0
  return bmap;
1025
0
error:
1026
0
  isl_int_clear(gcd);
1027
0
  isl_basic_map_free(bmap);
1028
0
  return NULL;
1029
0
}
1030
1031
/* If there are any equalities that involve (multiple) unknown divs,
1032
 * then extract the stride information encoded by those equalities
1033
 * and make it explicitly available in "bmap".
1034
 *
1035
 * We first sort the divs so that the unknown divs appear last and
1036
 * then we count how many equalities involve these divs.
1037
 *
1038
 * Let these equalities be of the form
1039
 *
1040
 *    A(x) + B y = 0
1041
 *
1042
 * where y represents the unknown divs and x the remaining variables.
1043
 * Let [H 0] be the Hermite Normal Form of B, i.e.,
1044
 *
1045
 *    B = [H 0] Q
1046
 *
1047
 * Then x is a solution of the equalities iff
1048
 *
1049
 *    H^-1 A(x) (= - [I 0] Q y)
1050
 *
1051
 * is an integer vector.  Let d be the common denominator of H^-1.
1052
 * We impose
1053
 *
1054
 *    d H^-1 A(x) = d alpha
1055
 *
1056
 * in add_strides, with alpha fresh existentially quantified variables.
1057
 */
1058
static __isl_give isl_basic_map *isl_basic_map_make_strides_explicit(
1059
  __isl_take isl_basic_map *bmap)
1060
265k
{
1061
265k
  int known;
1062
265k
  int n_known;
1063
265k
  int n, n_col;
1064
265k
  int total;
1065
265k
  isl_ctx *ctx;
1066
265k
  isl_mat *A, *B, *M;
1067
265k
1068
265k
  known = isl_basic_map_divs_known(bmap);
1069
265k
  if (known < 0)
1070
0
    return isl_basic_map_free(bmap);
1071
265k
  
if (265k
known265k
)
1072
265k
    return bmap;
1073
0
  bmap = isl_basic_map_sort_divs(bmap);
1074
0
  bmap = isl_basic_map_gauss(bmap, NULL);
1075
0
  if (!bmap)
1076
0
    return NULL;
1077
0
1078
0
  
for (n_known = 0; 0
n_known < bmap->n_div0
;
++n_known0
)
1079
0
    
if (0
isl_int_is_zero0
(bmap->div[n_known][0]))
1080
0
      break;
1081
0
  ctx = isl_basic_map_get_ctx(bmap);
1082
0
  total = isl_space_dim(bmap->dim, isl_dim_all);
1083
0
  for (n = 0; 
n < bmap->n_eq0
;
++n0
)
1084
0
    
if (0
isl_seq_first_non_zero(bmap->eq[n] + 1 + total + n_known,0
1085
0
              bmap->n_div - n_known) == -1)
1086
0
      break;
1087
0
  if (n == 0)
1088
0
    return bmap;
1089
0
  B = isl_mat_sub_alloc6(ctx, bmap->eq, 0, n, 0, 1 + total + n_known);
1090
0
  n_col = bmap->n_div - n_known;
1091
0
  A = isl_mat_sub_alloc6(ctx, bmap->eq, 0, n, 1 + total + n_known, n_col);
1092
0
  A = isl_mat_left_hermite(A, 0, NULL, NULL);
1093
0
  A = isl_mat_drop_cols(A, n, n_col - n);
1094
0
  A = isl_mat_lin_to_aff(A);
1095
0
  A = isl_mat_right_inverse(A);
1096
0
  B = isl_mat_insert_zero_rows(B, 0, 1);
1097
0
  B = isl_mat_set_element_si(B, 0, 0, 1);
1098
0
  M = isl_mat_product(A, B);
1099
0
  if (!M)
1100
0
    return isl_basic_map_free(bmap);
1101
0
  bmap = add_strides(bmap, M, n_known);
1102
0
  bmap = isl_basic_map_gauss(bmap, NULL);
1103
0
  isl_mat_free(M);
1104
0
1105
0
  return bmap;
1106
0
}
1107
1108
/* Compute the affine hull of each basic map in "map" separately
1109
 * and make all stride information explicit so that we can remove
1110
 * all unknown divs without losing this information.
1111
 * The result is also guaranteed to be gaussed.
1112
 *
1113
 * In simple cases where a div is determined by an equality,
1114
 * calling isl_basic_map_gauss is enough to make the stride information
1115
 * explicit, as it will derive an explicit representation for the div
1116
 * from the equality.  If, however, the stride information
1117
 * is encoded through multiple unknown divs then we need to make
1118
 * some extra effort in isl_basic_map_make_strides_explicit.
1119
 */
1120
static __isl_give isl_map *isl_map_local_affine_hull(__isl_take isl_map *map)
1121
244k
{
1122
244k
  int i;
1123
244k
1124
244k
  map = isl_map_cow(map);
1125
244k
  if (!map)
1126
0
    return NULL;
1127
244k
1128
510k
  
for (i = 0; 244k
i < map->n510k
;
++i265k
)
{265k
1129
265k
    map->p[i] = isl_basic_map_affine_hull(map->p[i]);
1130
265k
    map->p[i] = isl_basic_map_gauss(map->p[i], NULL);
1131
265k
    map->p[i] = isl_basic_map_make_strides_explicit(map->p[i]);
1132
265k
    if (!map->p[i])
1133
0
      return isl_map_free(map);
1134
265k
  }
1135
244k
1136
244k
  return map;
1137
244k
}
1138
1139
static __isl_give isl_set *isl_set_local_affine_hull(__isl_take isl_set *set)
1140
122k
{
1141
122k
  return isl_map_local_affine_hull(set);
1142
122k
}
1143
1144
/* Return an empty basic map living in the same space as "map".
1145
 */
1146
static __isl_give isl_basic_map *replace_map_by_empty_basic_map(
1147
  __isl_take isl_map *map)
1148
1
{
1149
1
  isl_space *space;
1150
1
1151
1
  space = isl_map_get_space(map);
1152
1
  isl_map_free(map);
1153
1
  return isl_basic_map_empty(space);
1154
1
}
1155
1156
/* Compute the affine hull of "map".
1157
 *
1158
 * We first compute the affine hull of each basic map separately.
1159
 * Then we align the divs and recompute the affine hulls of the basic
1160
 * maps since some of them may now have extra divs.
1161
 * In order to avoid performing parametric integer programming to
1162
 * compute explicit expressions for the divs, possible leading to
1163
 * an explosion in the number of basic maps, we first drop all unknown
1164
 * divs before aligning the divs.  Note that isl_map_local_affine_hull tries
1165
 * to make sure that all stride information is explicitly available
1166
 * in terms of known divs.  This involves calling isl_basic_set_gauss,
1167
 * which is also needed because affine_hull assumes its input has been gaussed,
1168
 * while isl_map_affine_hull may be called on input that has not been gaussed,
1169
 * in particular from initial_facet_constraint.
1170
 * Similarly, align_divs may reorder some divs so that we need to
1171
 * gauss the result again.
1172
 * Finally, we combine the individual affine hulls into a single
1173
 * affine hull.
1174
 */
1175
__isl_give isl_basic_map *isl_map_affine_hull(__isl_take isl_map *map)
1176
122k
{
1177
122k
  struct isl_basic_map *model = NULL;
1178
122k
  struct isl_basic_map *hull = NULL;
1179
122k
  struct isl_set *set;
1180
122k
  isl_basic_set *bset;
1181
122k
1182
122k
  map = isl_map_detect_equalities(map);
1183
122k
  map = isl_map_local_affine_hull(map);
1184
122k
  map = isl_map_remove_empty_parts(map);
1185
122k
  map = isl_map_remove_unknown_divs(map);
1186
122k
  map = isl_map_align_divs_internal(map);
1187
122k
1188
122k
  if (!map)
1189
0
    return NULL;
1190
122k
1191
122k
  
if (122k
map->n == 0122k
)
1192
1
    return replace_map_by_empty_basic_map(map);
1193
122k
1194
122k
  model = isl_basic_map_copy(map->p[0]);
1195
122k
  set = isl_map_underlying_set(map);
1196
122k
  set = isl_set_cow(set);
1197
122k
  set = isl_set_local_affine_hull(set);
1198
122k
  if (!set)
1199
0
    goto error;
1200
122k
1201
132k
  
while (122k
set->n > 1132k
)
1202
9.79k
    set->p[0] = affine_hull(set->p[0], set->p[--set->n]);
1203
122k
1204
122k
  bset = isl_basic_set_copy(set->p[0]);
1205
122k
  hull = isl_basic_map_overlying_set(bset, model);
1206
122k
  isl_set_free(set);
1207
122k
  hull = isl_basic_map_simplify(hull);
1208
122k
  return isl_basic_map_finalize(hull);
1209
0
error:
1210
0
  isl_basic_map_free(model);
1211
0
  isl_set_free(set);
1212
0
  return NULL;
1213
122k
}
1214
1215
struct isl_basic_set *isl_set_affine_hull(struct isl_set *set)
1216
120k
{
1217
120k
  return bset_from_bmap(isl_map_affine_hull(set_to_map(set)));
1218
120k
}