Coverage Report

Created: 2017-03-28 09:59

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