Coverage Report

Created: 2017-11-23 03:11

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/polly/lib/External/isl/isl_coalesce.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-2013 Ecole Normale Superieure
5
 * Copyright 2014      INRIA Rocquencourt
6
 * Copyright 2016      INRIA Paris
7
 *
8
 * Use of this software is governed by the MIT license
9
 *
10
 * Written by Sven Verdoolaege, K.U.Leuven, Departement
11
 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
12
 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
13
 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France 
14
 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
15
 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
16
 * B.P. 105 - 78153 Le Chesnay, France
17
 * and Centre de Recherche Inria de Paris, 2 rue Simone Iff - Voie DQ12,
18
 * CS 42112, 75589 Paris Cedex 12, France
19
 */
20
21
#include <isl_ctx_private.h>
22
#include "isl_map_private.h"
23
#include <isl_seq.h>
24
#include <isl/options.h>
25
#include "isl_tab.h"
26
#include <isl_mat_private.h>
27
#include <isl_local_space_private.h>
28
#include <isl_val_private.h>
29
#include <isl_vec_private.h>
30
#include <isl_aff_private.h>
31
#include <isl_equalities.h>
32
#include <isl_constraint_private.h>
33
34
#include <set_to_map.c>
35
#include <set_from_map.c>
36
37
586k
#define STATUS_ERROR    -1
38
170k
#define STATUS_REDUNDANT   1
39
498k
#define STATUS_VALID     2
40
495k
#define STATUS_SEPARATE    3
41
294k
#define STATUS_CUT     4
42
126k
#define STATUS_ADJ_EQ    5
43
153k
#define STATUS_ADJ_INEQ    6
44
45
static int status_in(isl_int *ineq, struct isl_tab *tab)
46
408k
{
47
408k
  enum isl_ineq_type type = isl_tab_ineq_type(tab, ineq);
48
408k
  switch (type) {
49
408k
  default:
50
0
  case isl_ineq_error:    return STATUS_ERROR;
51
201k
  case isl_ineq_redundant:  return STATUS_VALID;
52
33.9k
  case isl_ineq_separate:   return STATUS_SEPARATE;
53
129k
  case isl_ineq_cut:    return STATUS_CUT;
54
10.9k
  case isl_ineq_adj_eq:   return STATUS_ADJ_EQ;
55
32.2k
  case isl_ineq_adj_ineq:   return STATUS_ADJ_INEQ;
56
0
  }
57
0
}
58
59
/* Compute the position of the equalities of basic map "bmap_i"
60
 * with respect to the basic map represented by "tab_j".
61
 * The resulting array has twice as many entries as the number
62
 * of equalities corresponding to the two inequalities to which
63
 * each equality corresponds.
64
 */
65
static int *eq_status_in(__isl_keep isl_basic_map *bmap_i,
66
  struct isl_tab *tab_j)
67
84.9k
{
68
84.9k
  int k, l;
69
84.9k
  int *eq = isl_calloc_array(bmap_i->ctx, int, 2 * bmap_i->n_eq);
70
84.9k
  unsigned dim;
71
84.9k
72
84.9k
  if (!eq)
73
0
    return NULL;
74
84.9k
75
84.9k
  dim = isl_basic_map_total_dim(bmap_i);
76
144k
  for (k = 0; k < bmap_i->n_eq; 
++k59.9k
) {
77
179k
    for (l = 0; l < 2; 
++l119k
) {
78
119k
      isl_seq_neg(bmap_i->eq[k], bmap_i->eq[k], 1+dim);
79
119k
      eq[2 * k + l] = status_in(bmap_i->eq[k], tab_j);
80
119k
      if (eq[2 * k + l] == STATUS_ERROR)
81
119k
        
goto error0
;
82
119k
    }
83
59.9k
  }
84
84.9k
85
84.9k
  return eq;
86
0
error:
87
0
  free(eq);
88
0
  return NULL;
89
84.9k
}
90
91
/* Compute the position of the inequalities of basic map "bmap_i"
92
 * (also represented by "tab_i", if not NULL) with respect to the basic map
93
 * represented by "tab_j".
94
 */
95
static int *ineq_status_in(__isl_keep isl_basic_map *bmap_i,
96
  struct isl_tab *tab_i, struct isl_tab *tab_j)
97
110k
{
98
110k
  int k;
99
110k
  unsigned n_eq = bmap_i->n_eq;
100
110k
  int *ineq = isl_calloc_array(bmap_i->ctx, int, bmap_i->n_ineq);
101
110k
102
110k
  if (!ineq)
103
0
    return NULL;
104
110k
105
361k
  
for (k = 0; 110k
k < bmap_i->n_ineq;
++k250k
) {
106
275k
    if (tab_i && 
isl_tab_is_redundant(tab_i, n_eq + k)256k
) {
107
9.77k
      ineq[k] = STATUS_REDUNDANT;
108
9.77k
      continue;
109
9.77k
    }
110
265k
    ineq[k] = status_in(bmap_i->ineq[k], tab_j);
111
265k
    if (ineq[k] == STATUS_ERROR)
112
265k
      
goto error0
;
113
265k
    if (ineq[k] == STATUS_SEPARATE)
114
265k
      
break24.6k
;
115
275k
  }
116
110k
117
110k
  return ineq;
118
0
error:
119
0
  free(ineq);
120
0
  return NULL;
121
110k
}
122
123
static int any(int *con, unsigned len, int status)
124
669k
{
125
669k
  int i;
126
669k
127
1.86M
  for (i = 0; i < len ; 
++i1.19M
)
128
1.26M
    if (con[i] == status)
129
63.4k
      return 1;
130
669k
  
return 0605k
;
131
669k
}
132
133
/* Return the first position of "status" in the list "con" of length "len".
134
 * Return -1 if there is no such entry.
135
 */
136
static int find(int *con, unsigned len, int status)
137
4.56k
{
138
4.56k
  int i;
139
4.56k
140
11.7k
  for (i = 0; i < len ; 
++i7.18k
)
141
11.7k
    if (con[i] == status)
142
4.56k
      return i;
143
4.56k
  
return -10
;
144
4.56k
}
145
146
static int count(int *con, unsigned len, int status)
147
76.4k
{
148
76.4k
  int i;
149
76.4k
  int c = 0;
150
76.4k
151
254k
  for (i = 0; i < len ; 
++i177k
)
152
177k
    if (con[i] == status)
153
63.8k
      c++;
154
76.4k
  return c;
155
76.4k
}
156
157
static int all(int *con, unsigned len, int status)
158
129k
{
159
129k
  int i;
160
129k
161
194k
  for (i = 0; i < len ; 
++i65.8k
) {
162
135k
    if (con[i] == STATUS_REDUNDANT)
163
135k
      
continue1.18k
;
164
134k
    if (con[i] != status)
165
69.3k
      return 0;
166
135k
  }
167
129k
  
return 159.6k
;
168
129k
}
169
170
/* Internal information associated to a basic map in a map
171
 * that is to be coalesced by isl_map_coalesce.
172
 *
173
 * "bmap" is the basic map itself (or NULL if "removed" is set)
174
 * "tab" is the corresponding tableau (or NULL if "removed" is set)
175
 * "hull_hash" identifies the affine space in which "bmap" lives.
176
 * "removed" is set if this basic map has been removed from the map
177
 * "simplify" is set if this basic map may have some unknown integer
178
 * divisions that were not present in the input basic maps.  The basic
179
 * map should then be simplified such that we may be able to find
180
 * a definition among the constraints.
181
 *
182
 * "eq" and "ineq" are only set if we are currently trying to coalesce
183
 * this basic map with another basic map, in which case they represent
184
 * the position of the inequalities of this basic map with respect to
185
 * the other basic map.  The number of elements in the "eq" array
186
 * is twice the number of equalities in the "bmap", corresponding
187
 * to the two inequalities that make up each equality.
188
 */
189
struct isl_coalesce_info {
190
  isl_basic_map *bmap;
191
  struct isl_tab *tab;
192
  uint32_t hull_hash;
193
  int removed;
194
  int simplify;
195
  int *eq;
196
  int *ineq;
197
};
198
199
/* Are all non-redundant constraints of the basic map represented by "info"
200
 * either valid or cut constraints with respect to the other basic map?
201
 */
202
static int all_valid_or_cut(struct isl_coalesce_info *info)
203
535
{
204
535
  int i;
205
535
206
2.68k
  for (i = 0; i < 2 * info->bmap->n_eq; 
++i2.15k
) {
207
2.15k
    if (info->eq[i] == STATUS_REDUNDANT)
208
2.15k
      
continue0
;
209
2.15k
    if (info->eq[i] == STATUS_VALID)
210
2.15k
      
continue1.51k
;
211
642
    if (info->eq[i] == STATUS_CUT)
212
642
      continue;
213
0
    return 0;
214
0
  }
215
535
216
1.21k
  
for (i = 0; 535
i < info->bmap->n_ineq;
++i676
) {
217
1.16k
    if (info->ineq[i] == STATUS_REDUNDANT)
218
1.16k
      
continue22
;
219
1.14k
    if (info->ineq[i] == STATUS_VALID)
220
1.14k
      
continue570
;
221
576
    if (info->ineq[i] == STATUS_CUT)
222
576
      
continue84
;
223
492
    return 0;
224
492
  }
225
535
226
535
  
return 143
;
227
535
}
228
229
/* Compute the hash of the (apparent) affine hull of info->bmap (with
230
 * the existentially quantified variables removed) and store it
231
 * in info->hash.
232
 */
233
static int coalesce_info_set_hull_hash(struct isl_coalesce_info *info)
234
57.7k
{
235
57.7k
  isl_basic_map *hull;
236
57.7k
  unsigned n_div;
237
57.7k
238
57.7k
  hull = isl_basic_map_copy(info->bmap);
239
57.7k
  hull = isl_basic_map_plain_affine_hull(hull);
240
57.7k
  n_div = isl_basic_map_dim(hull, isl_dim_div);
241
57.7k
  hull = isl_basic_map_drop_constraints_involving_dims(hull,
242
57.7k
              isl_dim_div, 0, n_div);
243
57.7k
  info->hull_hash = isl_basic_map_get_hash(hull);
244
57.7k
  isl_basic_map_free(hull);
245
57.7k
246
57.7k
  return hull ? 0 : 
-10
;
247
57.7k
}
248
249
/* Free all the allocated memory in an array
250
 * of "n" isl_coalesce_info elements.
251
 */
252
static void clear_coalesce_info(int n, struct isl_coalesce_info *info)
253
24.1k
{
254
24.1k
  int i;
255
24.1k
256
24.1k
  if (!info)
257
0
    return;
258
24.1k
259
81.8k
  
for (i = 0; 24.1k
i < n;
++i57.7k
) {
260
57.7k
    isl_basic_map_free(info[i].bmap);
261
57.7k
    isl_tab_free(info[i].tab);
262
57.7k
  }
263
24.1k
264
24.1k
  free(info);
265
24.1k
}
266
267
/* Drop the basic map represented by "info".
268
 * That is, clear the memory associated to the entry and
269
 * mark it as having been removed.
270
 */
271
static void drop(struct isl_coalesce_info *info)
272
9.97k
{
273
9.97k
  info->bmap = isl_basic_map_free(info->bmap);
274
9.97k
  isl_tab_free(info->tab);
275
9.97k
  info->tab = NULL;
276
9.97k
  info->removed = 1;
277
9.97k
}
278
279
/* Exchange the information in "info1" with that in "info2".
280
 */
281
static void exchange(struct isl_coalesce_info *info1,
282
  struct isl_coalesce_info *info2)
283
278
{
284
278
  struct isl_coalesce_info info;
285
278
286
278
  info = *info1;
287
278
  *info1 = *info2;
288
278
  *info2 = info;
289
278
}
290
291
/* This type represents the kind of change that has been performed
292
 * while trying to coalesce two basic maps.
293
 *
294
 * isl_change_none: nothing was changed
295
 * isl_change_drop_first: the first basic map was removed
296
 * isl_change_drop_second: the second basic map was removed
297
 * isl_change_fuse: the two basic maps were replaced by a new basic map.
298
 */
299
enum isl_change {
300
  isl_change_error = -1,
301
  isl_change_none = 0,
302
  isl_change_drop_first,
303
  isl_change_drop_second,
304
  isl_change_fuse,
305
};
306
307
/* Update "change" based on an interchange of the first and the second
308
 * basic map.  That is, interchange isl_change_drop_first and
309
 * isl_change_drop_second.
310
 */
311
static enum isl_change invert_change(enum isl_change change)
312
34
{
313
34
  switch (change) {
314
34
  case isl_change_error:
315
0
    return isl_change_error;
316
34
  case isl_change_none:
317
0
    return isl_change_none;
318
34
  case isl_change_drop_first:
319
1
    return isl_change_drop_second;
320
34
  case isl_change_drop_second:
321
4
    return isl_change_drop_first;
322
34
  case isl_change_fuse:
323
29
    return isl_change_fuse;
324
0
  }
325
0
326
0
  return isl_change_error;
327
0
}
328
329
/* Add the valid constraints of the basic map represented by "info"
330
 * to "bmap".  "len" is the size of the constraints.
331
 * If only one of the pair of inequalities that make up an equality
332
 * is valid, then add that inequality.
333
 */
334
static __isl_give isl_basic_map *add_valid_constraints(
335
  __isl_take isl_basic_map *bmap, struct isl_coalesce_info *info,
336
  unsigned len)
337
2.31k
{
338
2.31k
  int k, l;
339
2.31k
340
2.31k
  if (!bmap)
341
0
    return NULL;
342
2.31k
343
6.02k
  
for (k = 0; 2.31k
k < info->bmap->n_eq;
++k3.71k
) {
344
3.71k
    if (info->eq[2 * k] == STATUS_VALID &&
345
3.71k
        
info->eq[2 * k + 1] == 2.71k
STATUS_VALID2.71k
) {
346
1.80k
      l = isl_basic_map_alloc_equality(bmap);
347
1.80k
      if (l < 0)
348
0
        return isl_basic_map_free(bmap);
349
1.80k
      isl_seq_cpy(bmap->eq[l], info->bmap->eq[k], len);
350
1.90k
    } else if (info->eq[2 * k] == STATUS_VALID) {
351
912
      l = isl_basic_map_alloc_inequality(bmap);
352
912
      if (l < 0)
353
0
        return isl_basic_map_free(bmap);
354
912
      isl_seq_neg(bmap->ineq[l], info->bmap->eq[k], len);
355
992
    } else if (info->eq[2 * k + 1] == STATUS_VALID) {
356
987
      l = isl_basic_map_alloc_inequality(bmap);
357
987
      if (l < 0)
358
0
        return isl_basic_map_free(bmap);
359
987
      isl_seq_cpy(bmap->ineq[l], info->bmap->eq[k], len);
360
987
    }
361
3.71k
  }
362
2.31k
363
7.94k
  
for (k = 0; 2.31k
k < info->bmap->n_ineq;
++k5.63k
) {
364
5.63k
    if (info->ineq[k] != STATUS_VALID)
365
5.63k
      
continue1.73k
;
366
3.90k
    l = isl_basic_map_alloc_inequality(bmap);
367
3.90k
    if (l < 0)
368
0
      return isl_basic_map_free(bmap);
369
3.90k
    isl_seq_cpy(bmap->ineq[l], info->bmap->ineq[k], len);
370
3.90k
  }
371
2.31k
372
2.31k
  return bmap;
373
2.31k
}
374
375
/* Is "bmap" defined by a number of (non-redundant) constraints that
376
 * is greater than the number of constraints of basic maps i and j combined?
377
 * Equalities are counted as two inequalities.
378
 */
379
static int number_of_constraints_increases(int i, int j,
380
  struct isl_coalesce_info *info,
381
  __isl_keep isl_basic_map *bmap, struct isl_tab *tab)
382
29
{
383
29
  int k, n_old, n_new;
384
29
385
29
  n_old = 2 * info[i].bmap->n_eq + info[i].bmap->n_ineq;
386
29
  n_old += 2 * info[j].bmap->n_eq + info[j].bmap->n_ineq;
387
29
388
29
  n_new = 2 * bmap->n_eq;
389
265
  for (k = 0; k < bmap->n_ineq; 
++k236
)
390
236
    if (!isl_tab_is_redundant(tab, bmap->n_eq + k))
391
129
      ++n_new;
392
29
393
29
  return n_new > n_old;
394
29
}
395
396
/* Replace the pair of basic maps i and j by the basic map bounded
397
 * by the valid constraints in both basic maps and the constraints
398
 * in extra (if not NULL).
399
 * Place the fused basic map in the position that is the smallest of i and j.
400
 *
401
 * If "detect_equalities" is set, then look for equalities encoded
402
 * as pairs of inequalities.
403
 * If "check_number" is set, then the original basic maps are only
404
 * replaced if the total number of constraints does not increase.
405
 * While the number of integer divisions in the two basic maps
406
 * is assumed to be the same, the actual definitions may be different.
407
 * We only copy the definition from one of the basic map if it is
408
 * the same as that of the other basic map.  Otherwise, we mark
409
 * the integer division as unknown and simplify the basic map
410
 * in an attempt to recover the integer division definition.
411
 */
412
static enum isl_change fuse(int i, int j, struct isl_coalesce_info *info,
413
  __isl_keep isl_mat *extra, int detect_equalities, int check_number)
414
1.19k
{
415
1.19k
  int k, l;
416
1.19k
  struct isl_basic_map *fused = NULL;
417
1.19k
  struct isl_tab *fused_tab = NULL;
418
1.19k
  unsigned total = isl_basic_map_total_dim(info[i].bmap);
419
1.19k
  unsigned extra_rows = extra ? 
extra->n_row643
:
0556
;
420
1.19k
  unsigned n_eq, n_ineq;
421
1.19k
  int simplify = 0;
422
1.19k
423
1.19k
  if (j < i)
424
43
    return fuse(j, i, info, extra, detect_equalities, check_number);
425
1.15k
426
1.15k
  n_eq = info[i].bmap->n_eq + info[j].bmap->n_eq;
427
1.15k
  n_ineq = info[i].bmap->n_ineq + info[j].bmap->n_ineq;
428
1.15k
  fused = isl_basic_map_alloc_space(isl_space_copy(info[i].bmap->dim),
429
1.15k
        info[i].bmap->n_div, n_eq, n_eq + n_ineq + extra_rows);
430
1.15k
  fused = add_valid_constraints(fused, &info[i], 1 + total);
431
1.15k
  fused = add_valid_constraints(fused, &info[j], 1 + total);
432
1.15k
  if (!fused)
433
0
    goto error;
434
1.15k
  if (ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_RATIONAL) &&
435
1.15k
      
ISL_F_ISSET0
(info[j].bmap, ISL_BASIC_MAP_RATIONAL))
436
1.15k
    ISL_F_SET(fused, ISL_BASIC_MAP_RATIONAL);
437
1.15k
438
1.23k
  for (k = 0; k < info[i].bmap->n_div; 
++k77
) {
439
77
    int l = isl_basic_map_alloc_div(fused);
440
77
    if (l < 0)
441
0
      goto error;
442
77
    if (isl_seq_eq(info[i].bmap->div[k], info[j].bmap->div[k],
443
77
        1 + 1 + total)) {
444
77
      isl_seq_cpy(fused->div[l], info[i].bmap->div[k],
445
77
        1 + 1 + total);
446
77
    } else {
447
0
      isl_int_set_si(fused->div[l][0], 0);
448
0
      simplify = 1;
449
0
    }
450
77
  }
451
1.15k
452
3.19k
  
for (k = 0; 1.15k
k < extra_rows;
++k2.04k
) {
453
2.04k
    l = isl_basic_map_alloc_inequality(fused);
454
2.04k
    if (l < 0)
455
0
      goto error;
456
2.04k
    isl_seq_cpy(fused->ineq[l], extra->row[k], 1 + total);
457
2.04k
  }
458
1.15k
459
1.15k
  if (detect_equalities)
460
315
    fused = isl_basic_map_detect_inequality_pairs(fused, NULL);
461
1.15k
  fused = isl_basic_map_gauss(fused, NULL);
462
1.15k
  if (simplify || info[j].simplify) {
463
0
    fused = isl_basic_map_simplify(fused);
464
0
    info[i].simplify = 0;
465
0
  }
466
1.15k
  fused = isl_basic_map_finalize(fused);
467
1.15k
468
1.15k
  fused_tab = isl_tab_from_basic_map(fused, 0);
469
1.15k
  if (isl_tab_detect_redundant(fused_tab) < 0)
470
0
    goto error;
471
1.15k
472
1.15k
  if (check_number &&
473
1.15k
      
number_of_constraints_increases(i, j, info, fused, fused_tab)29
) {
474
0
    isl_tab_free(fused_tab);
475
0
    isl_basic_map_free(fused);
476
0
    return isl_change_none;
477
0
  }
478
1.15k
479
1.15k
  isl_basic_map_free(info[i].bmap);
480
1.15k
  info[i].bmap = fused;
481
1.15k
  isl_tab_free(info[i].tab);
482
1.15k
  info[i].tab = fused_tab;
483
1.15k
  drop(&info[j]);
484
1.15k
485
1.15k
  return isl_change_fuse;
486
0
error:
487
0
  isl_tab_free(fused_tab);
488
0
  isl_basic_map_free(fused);
489
0
  return isl_change_error;
490
1.19k
}
491
492
/* Given a pair of basic maps i and j such that all constraints are either
493
 * "valid" or "cut", check if the facets corresponding to the "cut"
494
 * constraints of i lie entirely within basic map j.
495
 * If so, replace the pair by the basic map consisting of the valid
496
 * constraints in both basic maps.
497
 * Checking whether the facet lies entirely within basic map j
498
 * is performed by checking whether the constraints of basic map j
499
 * are valid for the facet.  These tests are performed on a rational
500
 * tableau to avoid the theoretical possibility that a constraint
501
 * that was considered to be a cut constraint for the entire basic map i
502
 * happens to be considered to be a valid constraint for the facet,
503
 * even though it cuts off the same rational points.
504
 *
505
 * To see that we are not introducing any extra points, call the
506
 * two basic maps A and B and the resulting map U and let x
507
 * be an element of U \setminus ( A \cup B ).
508
 * A line connecting x with an element of A \cup B meets a facet F
509
 * of either A or B.  Assume it is a facet of B and let c_1 be
510
 * the corresponding facet constraint.  We have c_1(x) < 0 and
511
 * so c_1 is a cut constraint.  This implies that there is some
512
 * (possibly rational) point x' satisfying the constraints of A
513
 * and the opposite of c_1 as otherwise c_1 would have been marked
514
 * valid for A.  The line connecting x and x' meets a facet of A
515
 * in a (possibly rational) point that also violates c_1, but this
516
 * is impossible since all cut constraints of B are valid for all
517
 * cut facets of A.
518
 * In case F is a facet of A rather than B, then we can apply the
519
 * above reasoning to find a facet of B separating x from A \cup B first.
520
 */
521
static enum isl_change check_facets(int i, int j,
522
  struct isl_coalesce_info *info)
523
10.4k
{
524
10.4k
  int k, l;
525
10.4k
  struct isl_tab_undo *snap, *snap2;
526
10.4k
  unsigned n_eq = info[i].bmap->n_eq;
527
10.4k
528
10.4k
  snap = isl_tab_snap(info[i].tab);
529
10.4k
  if (isl_tab_mark_rational(info[i].tab) < 0)
530
0
    return isl_change_error;
531
10.4k
  snap2 = isl_tab_snap(info[i].tab);
532
10.4k
533
11.7k
  for (k = 0; k < info[i].bmap->n_ineq; 
++k1.26k
) {
534
11.6k
    if (info[i].ineq[k] != STATUS_CUT)
535
11.6k
      
continue1.23k
;
536
10.4k
    if (isl_tab_select_facet(info[i].tab, n_eq + k) < 0)
537
0
      return isl_change_error;
538
11.8k
    
for (l = 0; 10.4k
l < info[j].bmap->n_ineq;
++l1.43k
) {
539
11.8k
      int stat;
540
11.8k
      if (info[j].ineq[l] != STATUS_CUT)
541
11.8k
        
continue1.39k
;
542
10.4k
      stat = status_in(info[j].bmap->ineq[l], info[i].tab);
543
10.4k
      if (stat < 0)
544
0
        return isl_change_error;
545
10.4k
      if (stat != STATUS_VALID)
546
10.4k
        
break10.4k
;
547
11.8k
    }
548
10.4k
    if (isl_tab_rollback(info[i].tab, snap2) < 0)
549
0
      return isl_change_error;
550
10.4k
    if (l < info[j].bmap->n_ineq)
551
10.4k
      break;
552
11.6k
  }
553
10.4k
554
10.4k
  if (k < info[i].bmap->n_ineq) {
555
10.4k
    if (isl_tab_rollback(info[i].tab, snap) < 0)
556
0
      return isl_change_error;
557
10.4k
    return isl_change_none;
558
10.4k
  }
559
31
  return fuse(i, j, info, NULL, 0, 0);
560
31
}
561
562
/* Check if info->bmap contains the basic map represented
563
 * by the tableau "tab".
564
 * For each equality, we check both the constraint itself
565
 * (as an inequality) and its negation.  Make sure the
566
 * equality is returned to its original state before returning.
567
 */
568
static isl_bool contains(struct isl_coalesce_info *info, struct isl_tab *tab)
569
2.09k
{
570
2.09k
  int k;
571
2.09k
  unsigned dim;
572
2.09k
  isl_basic_map *bmap = info->bmap;
573
2.09k
574
2.09k
  dim = isl_basic_map_total_dim(bmap);
575
6.17k
  for (k = 0; k < bmap->n_eq; 
++k4.07k
) {
576
4.51k
    int stat;
577
4.51k
    isl_seq_neg(bmap->eq[k], bmap->eq[k], 1 + dim);
578
4.51k
    stat = status_in(bmap->eq[k], tab);
579
4.51k
    isl_seq_neg(bmap->eq[k], bmap->eq[k], 1 + dim);
580
4.51k
    if (stat < 0)
581
0
      return isl_bool_error;
582
4.51k
    if (stat != STATUS_VALID)
583
4.51k
      
return isl_bool_false390
;
584
4.12k
    stat = status_in(bmap->eq[k], tab);
585
4.12k
    if (stat < 0)
586
0
      return isl_bool_error;
587
4.12k
    if (stat != STATUS_VALID)
588
4.12k
      
return isl_bool_false48
;
589
4.51k
  }
590
2.09k
591
4.64k
  
for (k = 0; 1.66k
k < bmap->n_ineq;
++k2.98k
) {
592
4.03k
    int stat;
593
4.03k
    if (info->ineq[k] == STATUS_REDUNDANT)
594
4.03k
      
continue178
;
595
3.85k
    stat = status_in(bmap->ineq[k], tab);
596
3.85k
    if (stat < 0)
597
0
      return isl_bool_error;
598
3.85k
    if (stat != STATUS_VALID)
599
3.85k
      
return isl_bool_false1.04k
;
600
4.03k
  }
601
1.66k
  
return isl_bool_true614
;
602
2.09k
}
603
604
/* Basic map "i" has an inequality (say "k") that is adjacent
605
 * to some inequality of basic map "j".  All the other inequalities
606
 * are valid for "j".
607
 * Check if basic map "j" forms an extension of basic map "i".
608
 *
609
 * Note that this function is only called if some of the equalities or
610
 * inequalities of basic map "j" do cut basic map "i".  The function is
611
 * correct even if there are no such cut constraints, but in that case
612
 * the additional checks performed by this function are overkill.
613
 *
614
 * In particular, we replace constraint k, say f >= 0, by constraint
615
 * f <= -1, add the inequalities of "j" that are valid for "i"
616
 * and check if the result is a subset of basic map "j".
617
 * To improve the chances of the subset relation being detected,
618
 * any variable that only attains a single integer value
619
 * in the tableau of "i" is first fixed to that value.
620
 * If the result is a subset, then we know that this result is exactly equal
621
 * to basic map "j" since all its constraints are valid for basic map "j".
622
 * By combining the valid constraints of "i" (all equalities and all
623
 * inequalities except "k") and the valid constraints of "j" we therefore
624
 * obtain a basic map that is equal to their union.
625
 * In this case, there is no need to perform a rollback of the tableau
626
 * since it is going to be destroyed in fuse().
627
 *
628
 *
629
 *  |\__      |\__
630
 *  |   \__     |   \__
631
 *  |      \_ =>  |      \__
632
 *  |_______| _   |_________\
633
 *
634
 *
635
 *  |\      |\
636
 *  | \     | \
637
 *  |  \      |  \
638
 *  |  |      |   \
639
 *  |  ||\    =>      |    \
640
 *  |  || \     |     \
641
 *  |  ||  |    |      |
642
 *  |__||_/     |_____/
643
 */
644
static enum isl_change is_adj_ineq_extension(int i, int j,
645
  struct isl_coalesce_info *info)
646
412
{
647
412
  int k;
648
412
  struct isl_tab_undo *snap;
649
412
  unsigned n_eq = info[i].bmap->n_eq;
650
412
  unsigned total = isl_basic_map_total_dim(info[i].bmap);
651
412
  isl_stat r;
652
412
  isl_bool super;
653
412
654
412
  if (isl_tab_extend_cons(info[i].tab, 1 + info[j].bmap->n_ineq) < 0)
655
0
    return isl_change_error;
656
412
657
412
  k = find(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_INEQ);
658
412
  if (k < 0)
659
412
    
isl_die0
(isl_basic_map_get_ctx(info[i].bmap), isl_error_internal,
660
412
      "info[i].ineq should have exactly one STATUS_ADJ_INEQ",
661
412
      return isl_change_error);
662
412
663
412
  snap = isl_tab_snap(info[i].tab);
664
412
665
412
  if (isl_tab_unrestrict(info[i].tab, n_eq + k) < 0)
666
0
    return isl_change_error;
667
412
668
412
  isl_seq_neg(info[i].bmap->ineq[k], info[i].bmap->ineq[k], 1 + total);
669
412
  isl_int_sub_ui(info[i].bmap->ineq[k][0], info[i].bmap->ineq[k][0], 1);
670
412
  r = isl_tab_add_ineq(info[i].tab, info[i].bmap->ineq[k]);
671
412
  isl_seq_neg(info[i].bmap->ineq[k], info[i].bmap->ineq[k], 1 + total);
672
412
  isl_int_sub_ui(info[i].bmap->ineq[k][0], info[i].bmap->ineq[k][0], 1);
673
412
  if (r < 0)
674
0
    return isl_change_error;
675
412
676
2.00k
  
for (k = 0; 412
k < info[j].bmap->n_ineq;
++k1.59k
) {
677
1.59k
    if (info[j].ineq[k] != STATUS_VALID)
678
1.59k
      
continue872
;
679
723
    if (isl_tab_add_ineq(info[i].tab, info[j].bmap->ineq[k]) < 0)
680
0
      return isl_change_error;
681
1.59k
  }
682
412
  if (isl_tab_detect_constants(info[i].tab) < 0)
683
0
    return isl_change_error;
684
412
685
412
  super = contains(&info[j], info[i].tab);
686
412
  if (super < 0)
687
0
    return isl_change_error;
688
412
  if (super)
689
23
    return fuse(i, j, info, NULL, 0, 0);
690
389
691
389
  if (isl_tab_rollback(info[i].tab, snap) < 0)
692
0
    return isl_change_error;
693
389
694
389
  return isl_change_none;
695
389
}
696
697
698
/* Both basic maps have at least one inequality with and adjacent
699
 * (but opposite) inequality in the other basic map.
700
 * Check that there are no cut constraints and that there is only
701
 * a single pair of adjacent inequalities.
702
 * If so, we can replace the pair by a single basic map described
703
 * by all but the pair of adjacent inequalities.
704
 * Any additional points introduced lie strictly between the two
705
 * adjacent hyperplanes and can therefore be integral.
706
 *
707
 *        ____        _____
708
 *       /    ||\    /     \
709
 *      /     || \    /       \
710
 *      \     ||  \ =>  \        \
711
 *       \    ||  /    \       /
712
 *        \___||_/      \_____/
713
 *
714
 * The test for a single pair of adjancent inequalities is important
715
 * for avoiding the combination of two basic maps like the following
716
 *
717
 *       /|
718
 *      / |
719
 *     /__|
720
 *         _____
721
 *         |   |
722
 *         |   |
723
 *         |___|
724
 *
725
 * If there are some cut constraints on one side, then we may
726
 * still be able to fuse the two basic maps, but we need to perform
727
 * some additional checks in is_adj_ineq_extension.
728
 */
729
static enum isl_change check_adj_ineq(int i, int j,
730
  struct isl_coalesce_info *info)
731
8.13k
{
732
8.13k
  int count_i, count_j;
733
8.13k
  int cut_i, cut_j;
734
8.13k
735
8.13k
  count_i = count(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_INEQ);
736
8.13k
  count_j = count(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_INEQ);
737
8.13k
738
8.13k
  if (count_i != 1 && 
count_j != 11.82k
)
739
1.79k
    return isl_change_none;
740
6.33k
741
6.33k
  cut_i = any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT) ||
742
6.33k
    
any(info[i].ineq, info[i].bmap->n_ineq, 6.21k
STATUS_CUT6.21k
);
743
6.33k
  cut_j = any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_CUT) ||
744
6.33k
    
any(info[j].ineq, info[j].bmap->n_ineq, 6.14k
STATUS_CUT6.14k
);
745
6.33k
746
6.33k
  if (!cut_i && 
!cut_j784
&&
count_i == 1490
&&
count_j == 1490
)
747
490
    return fuse(i, j, info, NULL, 0, 0);
748
5.84k
749
5.84k
  if (count_i == 1 && 
!cut_i5.81k
)
750
289
    return is_adj_ineq_extension(i, j, info);
751
5.56k
752
5.56k
  if (count_j == 1 && 
!cut_j5.54k
)
753
114
    return is_adj_ineq_extension(j, i, info);
754
5.44k
755
5.44k
  return isl_change_none;
756
5.44k
}
757
758
/* Given an affine transformation matrix "T", does row "row" represent
759
 * anything other than a unit vector (possibly shifted by a constant)
760
 * that is not involved in any of the other rows?
761
 *
762
 * That is, if a constraint involves the variable corresponding to
763
 * the row, then could its preimage by "T" have any coefficients
764
 * that are different from those in the original constraint?
765
 */
766
static int not_unique_unit_row(__isl_keep isl_mat *T, int row)
767
8.37k
{
768
8.37k
  int i, j;
769
8.37k
  int len = T->n_col - 1;
770
8.37k
771
8.37k
  i = isl_seq_first_non_zero(T->row[row] + 1, len);
772
8.37k
  if (i < 0)
773
1.22k
    return 1;
774
7.14k
  if (!isl_int_is_one(T->row[row][1 + i]) &&
775
7.14k
      
!125
isl_int_is_negone125
(T->row[row][1 + i]))
776
7.14k
    
return 1109
;
777
7.04k
778
7.04k
  j = isl_seq_first_non_zero(T->row[row] + 1 + i + 1, len - (i + 1));
779
7.04k
  if (j >= 0)
780
4
    return 1;
781
7.03k
782
52.3k
  
for (j = 1; 7.03k
j < T->n_row;
++j45.3k
) {
783
46.1k
    if (j == row)
784
6.62k
      continue;
785
39.5k
    if (!isl_int_is_zero(T->row[j][1 + i]))
786
39.5k
      
return 1821
;
787
46.1k
  }
788
7.03k
789
7.03k
  
return 06.21k
;
790
8.37k
}
791
792
/* Does inequality constraint "ineq" of "bmap" involve any of
793
 * the variables marked in "affected"?
794
 * "total" is the total number of variables, i.e., the number
795
 * of entries in "affected".
796
 */
797
static isl_bool is_affected(__isl_keep isl_basic_map *bmap, int ineq,
798
  int *affected, int total)
799
3.40k
{
800
3.40k
  int i;
801
3.40k
802
15.6k
  for (i = 0; i < total; 
++i12.2k
) {
803
13.6k
    if (!affected[i])
804
9.83k
      continue;
805
3.80k
    if (!isl_int_is_zero(bmap->ineq[ineq][1 + i]))
806
3.80k
      
return isl_bool_true1.40k
;
807
13.6k
  }
808
3.40k
809
3.40k
  
return isl_bool_false1.99k
;
810
3.40k
}
811
812
/* Given the compressed version of inequality constraint "ineq"
813
 * of info->bmap in "v", check if the constraint can be tightened,
814
 * where the compression is based on an equality constraint valid
815
 * for info->tab.
816
 * If so, add the tightened version of the inequality constraint
817
 * to info->tab.  "v" may be modified by this function.
818
 *
819
 * That is, if the compressed constraint is of the form
820
 *
821
 *  m f() + c >= 0
822
 *
823
 * with 0 < c < m, then it is equivalent to
824
 *
825
 *  f() >= 0
826
 *
827
 * This means that c can also be subtracted from the original,
828
 * uncompressed constraint without affecting the integer points
829
 * in info->tab.  Add this tightened constraint as an extra row
830
 * to info->tab to make this information explicitly available.
831
 */
832
static __isl_give isl_vec *try_tightening(struct isl_coalesce_info *info,
833
  int ineq, __isl_take isl_vec *v)
834
1.40k
{
835
1.40k
  isl_ctx *ctx;
836
1.40k
  isl_stat r;
837
1.40k
838
1.40k
  if (!v)
839
0
    return NULL;
840
1.40k
841
1.40k
  ctx = isl_vec_get_ctx(v);
842
1.40k
  isl_seq_gcd(v->el + 1, v->size - 1, &ctx->normalize_gcd);
843
1.40k
  if (isl_int_is_zero(ctx->normalize_gcd) ||
844
1.40k
      
isl_int_is_one1.03k
(ctx->normalize_gcd)) {
845
1.22k
    return v;
846
1.22k
  }
847
179
848
179
  v = isl_vec_cow(v);
849
179
  if (!v)
850
0
    return NULL;
851
179
852
179
  isl_int_fdiv_r(v->el[0], v->el[0], ctx->normalize_gcd);
853
179
  if (isl_int_is_zero(v->el[0]))
854
179
    
return v69
;
855
110
856
110
  if (isl_tab_extend_cons(info->tab, 1) < 0)
857
0
    return isl_vec_free(v);
858
110
859
110
  isl_int_sub(info->bmap->ineq[ineq][0],
860
110
        info->bmap->ineq[ineq][0], v->el[0]);
861
110
  r = isl_tab_add_ineq(info->tab, info->bmap->ineq[ineq]);
862
110
  isl_int_add(info->bmap->ineq[ineq][0],
863
110
        info->bmap->ineq[ineq][0], v->el[0]);
864
110
865
110
  if (r < 0)
866
0
    return isl_vec_free(v);
867
110
868
110
  return v;
869
110
}
870
871
/* Tighten the (non-redundant) constraints on the facet represented
872
 * by info->tab.
873
 * In particular, on input, info->tab represents the result
874
 * of relaxing the "n" inequality constraints of info->bmap in "relaxed"
875
 * by one, i.e., replacing f_i >= 0 by f_i + 1 >= 0, and then
876
 * replacing the one at index "l" by the corresponding equality,
877
 * i.e., f_k + 1 = 0, with k = relaxed[l].
878
 *
879
 * Compute a variable compression from the equality constraint f_k + 1 = 0
880
 * and use it to tighten the other constraints of info->bmap
881
 * (that is, all constraints that have not been relaxed),
882
 * updating info->tab (and leaving info->bmap untouched).
883
 * The compression handles essentially two cases, one where a variable
884
 * is assigned a fixed value and can therefore be eliminated, and one
885
 * where one variable is a shifted multiple of some other variable and
886
 * can therefore be replaced by that multiple.
887
 * Gaussian elimination would also work for the first case, but for
888
 * the second case, the effectiveness would depend on the order
889
 * of the variables.
890
 * After compression, some of the constraints may have coefficients
891
 * with a common divisor.  If this divisor does not divide the constant
892
 * term, then the constraint can be tightened.
893
 * The tightening is performed on the tableau info->tab by introducing
894
 * extra (temporary) constraints.
895
 *
896
 * Only constraints that are possibly affected by the compression are
897
 * considered.  In particular, if the constraint only involves variables
898
 * that are directly mapped to a distinct set of other variables, then
899
 * no common divisor can be introduced and no tightening can occur.
900
 *
901
 * It is important to only consider the non-redundant constraints
902
 * since the facet constraint has been relaxed prior to the call
903
 * to this function, meaning that the constraints that were redundant
904
 * prior to the relaxation may no longer be redundant.
905
 * These constraints will be ignored in the fused result, so
906
 * the fusion detection should not exploit them.
907
 */
908
static isl_stat tighten_on_relaxed_facet(struct isl_coalesce_info *info,
909
  int n, int *relaxed, int l)
910
1.68k
{
911
1.68k
  unsigned total;
912
1.68k
  isl_ctx *ctx;
913
1.68k
  isl_vec *v = NULL;
914
1.68k
  isl_mat *T;
915
1.68k
  int i;
916
1.68k
  int k;
917
1.68k
  int *affected;
918
1.68k
919
1.68k
  k = relaxed[l];
920
1.68k
  ctx = isl_basic_map_get_ctx(info->bmap);
921
1.68k
  total = isl_basic_map_total_dim(info->bmap);
922
1.68k
  isl_int_add_ui(info->bmap->ineq[k][0], info->bmap->ineq[k][0], 1);
923
1.68k
  T = isl_mat_sub_alloc6(ctx, info->bmap->ineq, k, 1, 0, 1 + total);
924
1.68k
  T = isl_mat_variable_compression(T, NULL);
925
1.68k
  isl_int_sub_ui(info->bmap->ineq[k][0], info->bmap->ineq[k][0], 1);
926
1.68k
  if (!T)
927
0
    return isl_stat_error;
928
1.68k
  if (T->n_col == 0) {
929
0
    isl_mat_free(T);
930
0
    return isl_stat_ok;
931
0
  }
932
1.68k
933
1.68k
  affected = isl_alloc_array(ctx, int, total);
934
1.68k
  if (!affected)
935
0
    goto error;
936
1.68k
937
10.0k
  
for (i = 0; 1.68k
i < total;
++i8.37k
)
938
8.37k
    affected[i] = not_unique_unit_row(T, 1 + i);
939
1.68k
940
7.85k
  for (i = 0; i < info->bmap->n_ineq; 
++i6.16k
) {
941
6.16k
    isl_bool handle;
942
6.16k
    if (any(relaxed, n, i))
943
1.72k
      continue;
944
4.44k
    if (info->ineq[i] == STATUS_REDUNDANT)
945
4.44k
      
continue1.03k
;
946
3.40k
    handle = is_affected(info->bmap, i, affected, total);
947
3.40k
    if (handle < 0)
948
0
      goto error;
949
3.40k
    if (!handle)
950
1.99k
      continue;
951
1.40k
    v = isl_vec_alloc(ctx, 1 + total);
952
1.40k
    if (!v)
953
0
      goto error;
954
1.40k
    isl_seq_cpy(v->el, info->bmap->ineq[i], 1 + total);
955
1.40k
    v = isl_vec_mat_product(v, isl_mat_copy(T));
956
1.40k
    v = try_tightening(info, i, v);
957
1.40k
    isl_vec_free(v);
958
1.40k
    if (!v)
959
0
      goto error;
960
6.16k
  }
961
1.68k
962
1.68k
  isl_mat_free(T);
963
1.68k
  free(affected);
964
1.68k
  return isl_stat_ok;
965
0
error:
966
0
  isl_mat_free(T);
967
0
  free(affected);
968
0
  return isl_stat_error;
969
1.68k
}
970
971
/* Replace the basic maps "i" and "j" by an extension of "i"
972
 * along the "n" inequality constraints in "relax" by one.
973
 * The tableau info[i].tab has already been extended.
974
 * Extend info[i].bmap accordingly by relaxing all constraints in "relax"
975
 * by one.
976
 * Each integer division that does not have exactly the same
977
 * definition in "i" and "j" is marked unknown and the basic map
978
 * is scheduled to be simplified in an attempt to recover
979
 * the integer division definition.
980
 * Place the extension in the position that is the smallest of i and j.
981
 */
982
static enum isl_change extend(int i, int j, int n, int *relax,
983
  struct isl_coalesce_info *info)
984
577
{
985
577
  int l;
986
577
  unsigned total;
987
577
988
577
  info[i].bmap = isl_basic_map_cow(info[i].bmap);
989
577
  if (!info[i].bmap)
990
0
    return isl_change_error;
991
577
  total = isl_basic_map_total_dim(info[i].bmap);
992
640
  for (l = 0; l < info[i].bmap->n_div; 
++l63
)
993
577
    
if (63
!isl_seq_eq(info[i].bmap->div[l],
994
63
        info[j].bmap->div[l], 1 + 1 + total)) {
995
22
      isl_int_set_si(info[i].bmap->div[l][0], 0);
996
63
      info[i].simplify = 1;
997
63
    }
998
1.15k
  for (l = 0; l < n; 
++l581
)
999
581
    isl_int_add_ui(info[i].bmap->ineq[relax[l]][0],
1000
577
        info[i].bmap->ineq[relax[l]][0], 1);
1001
577
  ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_FINAL);
1002
577
  drop(&info[j]);
1003
577
  if (j < i)
1004
278
    exchange(&info[i], &info[j]);
1005
577
  return isl_change_fuse;
1006
577
}
1007
1008
/* Basic map "i" has "n" inequality constraints (collected in "relax")
1009
 * that are such that they include basic map "j" if they are relaxed
1010
 * by one.  All the other inequalities are valid for "j".
1011
 * Check if basic map "j" forms an extension of basic map "i".
1012
 *
1013
 * In particular, relax the constraints in "relax", compute the corresponding
1014
 * facets one by one and check whether each of these is included
1015
 * in the other basic map.
1016
 * Before testing for inclusion, the constraints on each facet
1017
 * are tightened to increase the chance of an inclusion being detected.
1018
 * (Adding the valid constraints of "j" to the tableau of "i", as is done
1019
 * in is_adj_ineq_extension, may further increase those chances, but this
1020
 * is not currently done.)
1021
 * If each facet is included, we know that relaxing the constraints extends
1022
 * the basic map with exactly the other basic map (we already know that this
1023
 * other basic map is included in the extension, because all other
1024
 * inequality constraints are valid of "j") and we can replace the
1025
 * two basic maps by this extension.
1026
 *
1027
 * If any of the relaxed constraints turn out to be redundant, then bail out.
1028
 * isl_tab_select_facet refuses to handle such constraints.  It may be
1029
 * possible to handle them anyway by making a distinction between
1030
 * redundant constraints with a corresponding facet that still intersects
1031
 * the set (allowing isl_tab_select_facet to handle them) and
1032
 * those where the facet does not intersect the set (which can be ignored
1033
 * because the empty facet is trivially included in the other disjunct).
1034
 * However, relaxed constraints that turn out to be redundant should
1035
 * be fairly rare and no such instance has been reported where
1036
 * coalescing would be successful.
1037
 *        ____        _____
1038
 *       /    ||     /     |
1039
 *      /     ||      /      |
1040
 *      \     ||    =>  \      |
1041
 *       \    ||     \     |
1042
 *        \___||      \____|
1043
 *
1044
 *
1045
 *   \      |\
1046
 *  |\\     | \
1047
 *  | \\      |  \
1048
 *  |  |    =>  |  /
1049
 *  | /     | /
1050
 *  |/      |/
1051
 */
1052
static enum isl_change is_relaxed_extension(int i, int j, int n, int *relax,
1053
  struct isl_coalesce_info *info)
1054
1.67k
{
1055
1.67k
  int l;
1056
1.67k
  isl_bool super;
1057
1.67k
  struct isl_tab_undo *snap, *snap2;
1058
1.67k
  unsigned n_eq = info[i].bmap->n_eq;
1059
1.67k
1060
3.36k
  for (l = 0; l < n; 
++l1.69k
)
1061
1.69k
    if (isl_tab_is_equality(info[i].tab, n_eq + relax[l]))
1062
0
      return isl_change_none;
1063
1.67k
1064
1.67k
  snap = isl_tab_snap(info[i].tab);
1065
3.36k
  for (l = 0; l < n; 
++l1.69k
)
1066
1.69k
    if (isl_tab_relax(info[i].tab, n_eq + relax[l]) < 0)
1067
0
      return isl_change_error;
1068
3.36k
  
for (l = 0; 1.67k
l < n;
++l1.69k
) {
1069
1.69k
    if (!isl_tab_is_redundant(info[i].tab, n_eq + relax[l]))
1070
1.69k
      continue;
1071
0
    if (isl_tab_rollback(info[i].tab, snap) < 0)
1072
0
      return isl_change_error;
1073
0
    return isl_change_none;
1074
0
  }
1075
1.67k
  snap2 = isl_tab_snap(info[i].tab);
1076
2.26k
  for (l = 0; l < n; 
++l591
) {
1077
1.68k
    if (isl_tab_rollback(info[i].tab, snap2) < 0)
1078
0
      return isl_change_error;
1079
1.68k
    if (isl_tab_select_facet(info[i].tab, n_eq + relax[l]) < 0)
1080
0
      return isl_change_error;
1081
1.68k
    if (tighten_on_relaxed_facet(&info[i], n, relax, l) < 0)
1082
0
      return isl_change_error;
1083
1.68k
    super = contains(&info[j], info[i].tab);
1084
1.68k
    if (super < 0)
1085
0
      return isl_change_error;
1086
1.68k
    if (super)
1087
591
      continue;
1088
1.09k
    if (isl_tab_rollback(info[i].tab, snap) < 0)
1089
0
      return isl_change_error;
1090
1.09k
    return isl_change_none;
1091
1.09k
  }
1092
1.67k
1093
1.67k
  
if (577
isl_tab_rollback(info[i].tab, snap2) < 0577
)
1094
0
    return isl_change_error;
1095
577
  return extend(i, j, n, relax, info);
1096
577
}
1097
1098
/* Data structure that keeps track of the wrapping constraints
1099
 * and of information to bound the coefficients of those constraints.
1100
 *
1101
 * bound is set if we want to apply a bound on the coefficients
1102
 * mat contains the wrapping constraints
1103
 * max is the bound on the coefficients (if bound is set)
1104
 */
1105
struct isl_wraps {
1106
  int bound;
1107
  isl_mat *mat;
1108
  isl_int max;
1109
};
1110
1111
/* Update wraps->max to be greater than or equal to the coefficients
1112
 * in the equalities and inequalities of info->bmap that can be removed
1113
 * if we end up applying wrapping.
1114
 */
1115
static isl_stat wraps_update_max(struct isl_wraps *wraps,
1116
  struct isl_coalesce_info *info)
1117
7.32k
{
1118
7.32k
  int k;
1119
7.32k
  isl_int max_k;
1120
7.32k
  unsigned total = isl_basic_map_total_dim(info->bmap);
1121
7.32k
1122
7.32k
  isl_int_init(max_k);
1123
7.32k
1124
26.4k
  for (k = 0; k < info->bmap->n_eq; 
++k19.1k
) {
1125
19.1k
    if (info->eq[2 * k] == STATUS_VALID &&
1126
19.1k
        
info->eq[2 * k + 1] == 13.1k
STATUS_VALID13.1k
)
1127
19.1k
      
continue8.65k
;
1128
10.4k
    isl_seq_abs_max(info->bmap->eq[k] + 1, total, &max_k);
1129
10.4k
    if (isl_int_abs_gt(max_k, wraps->max))
1130
10.4k
      
isl_int_set2.28k
(wraps->max, max_k);
1131
19.1k
  }
1132
7.32k
1133
28.4k
  for (k = 0; k < info->bmap->n_ineq; 
++k21.0k
) {
1134
21.0k
    if (info->ineq[k] == STATUS_VALID ||
1135
21.0k
        
info->ineq[k] == 9.01k
STATUS_REDUNDANT9.01k
)
1136
21.0k
      
continue14.4k
;
1137
6.67k
    isl_seq_abs_max(info->bmap->ineq[k] + 1, total, &max_k);
1138
6.67k
    if (isl_int_abs_gt(max_k, wraps->max))
1139
6.67k
      
isl_int_set1.49k
(wraps->max, max_k);
1140
21.0k
  }
1141
7.32k
1142
7.32k
  isl_int_clear(max_k);
1143
7.32k
1144
7.32k
  return isl_stat_ok;
1145
7.32k
}
1146
1147
/* Initialize the isl_wraps data structure.
1148
 * If we want to bound the coefficients of the wrapping constraints,
1149
 * we set wraps->max to the largest coefficient
1150
 * in the equalities and inequalities that can be removed if we end up
1151
 * applying wrapping.
1152
 */
1153
static isl_stat wraps_init(struct isl_wraps *wraps, __isl_take isl_mat *mat,
1154
  struct isl_coalesce_info *info, int i, int j)
1155
3.66k
{
1156
3.66k
  isl_ctx *ctx;
1157
3.66k
1158
3.66k
  wraps->bound = 0;
1159
3.66k
  wraps->mat = mat;
1160
3.66k
  if (!mat)
1161
0
    return isl_stat_error;
1162
3.66k
  ctx = isl_mat_get_ctx(mat);
1163
3.66k
  wraps->bound = isl_options_get_coalesce_bounded_wrapping(ctx);
1164
3.66k
  if (!wraps->bound)
1165
5
    return isl_stat_ok;
1166
3.66k
  isl_int_init(wraps->max);
1167
3.66k
  isl_int_set_si(wraps->max, 0);
1168
3.66k
  if (wraps_update_max(wraps, &info[i]) < 0)
1169
0
    return isl_stat_error;
1170
3.66k
  if (wraps_update_max(wraps, &info[j]) < 0)
1171
0
    return isl_stat_error;
1172
3.66k
1173
3.66k
  return isl_stat_ok;
1174
3.66k
}
1175
1176
/* Free the contents of the isl_wraps data structure.
1177
 */
1178
static void wraps_free(struct isl_wraps *wraps)
1179
3.66k
{
1180
3.66k
  isl_mat_free(wraps->mat);
1181
3.66k
  if (wraps->bound)
1182
3.66k
    
isl_int_clear3.66k
(wraps->max);
1183
3.66k
}
1184
1185
/* Is the wrapping constraint in row "row" allowed?
1186
 *
1187
 * If wraps->bound is set, we check that none of the coefficients
1188
 * is greater than wraps->max.
1189
 */
1190
static int allow_wrap(struct isl_wraps *wraps, int row)
1191
2.25k
{
1192
2.25k
  int i;
1193
2.25k
1194
2.25k
  if (!wraps->bound)
1195
13
    return 1;
1196
2.23k
1197
13.3k
  
for (i = 1; 2.23k
i < wraps->mat->n_col;
++i11.1k
)
1198
11.4k
    if (isl_int_abs_gt(wraps->mat->row[row][i], wraps->max))
1199
11.4k
      
return 0313
;
1200
2.23k
1201
2.23k
  
return 11.92k
;
1202
2.25k
}
1203
1204
/* Wrap "ineq" (or its opposite if "negate" is set) around "bound"
1205
 * to include "set" and add the result in position "w" of "wraps".
1206
 * "len" is the total number of coefficients in "bound" and "ineq".
1207
 * Return 1 on success, 0 on failure and -1 on error.
1208
 * Wrapping can fail if the result of wrapping is equal to "bound"
1209
 * or if we want to bound the sizes of the coefficients and
1210
 * the wrapped constraint does not satisfy this bound.
1211
 */
1212
static int add_wrap(struct isl_wraps *wraps, int w, isl_int *bound,
1213
  isl_int *ineq, unsigned len, __isl_keep isl_set *set, int negate)
1214
4.98k
{
1215
4.98k
  isl_seq_cpy(wraps->mat->row[w], bound, len);
1216
4.98k
  if (negate) {
1217
941
    isl_seq_neg(wraps->mat->row[w + 1], ineq, len);
1218
941
    ineq = wraps->mat->row[w + 1];
1219
941
  }
1220
4.98k
  if (!isl_set_wrap_facet(set, wraps->mat->row[w], ineq))
1221
0
    return -1;
1222
4.98k
  if (isl_seq_eq(wraps->mat->row[w], bound, len))
1223
2.73k
    return 0;
1224
2.25k
  if (!allow_wrap(wraps, w))
1225
313
    return 0;
1226
1.93k
  return 1;
1227
1.93k
}
1228
1229
/* For each constraint in info->bmap that is not redundant (as determined
1230
 * by info->tab) and that is not a valid constraint for the other basic map,
1231
 * wrap the constraint around "bound" such that it includes the whole
1232
 * set "set" and append the resulting constraint to "wraps".
1233
 * Note that the constraints that are valid for the other basic map
1234
 * will be added to the combined basic map by default, so there is
1235
 * no need to wrap them.
1236
 * The caller wrap_in_facets even relies on this function not wrapping
1237
 * any constraints that are already valid.
1238
 * "wraps" is assumed to have been pre-allocated to the appropriate size.
1239
 * wraps->n_row is the number of actual wrapped constraints that have
1240
 * been added.
1241
 * If any of the wrapping problems results in a constraint that is
1242
 * identical to "bound", then this means that "set" is unbounded in such
1243
 * way that no wrapping is possible.  If this happens then wraps->n_row
1244
 * is reset to zero.
1245
 * Similarly, if we want to bound the coefficients of the wrapping
1246
 * constraints and a newly added wrapping constraint does not
1247
 * satisfy the bound, then wraps->n_row is also reset to zero.
1248
 */
1249
static isl_stat add_wraps(struct isl_wraps *wraps,
1250
  struct isl_coalesce_info *info, isl_int *bound, __isl_keep isl_set *set)
1251
4.70k
{
1252
4.70k
  int l, m;
1253
4.70k
  int w;
1254
4.70k
  int added;
1255
4.70k
  isl_basic_map *bmap = info->bmap;
1256
4.70k
  unsigned len = 1 + isl_basic_map_total_dim(bmap);
1257
4.70k
1258
4.70k
  w = wraps->mat->n_row;
1259
4.70k
1260
12.9k
  for (l = 0; l < bmap->n_ineq; 
++l8.25k
) {
1261
10.8k
    if (info->ineq[l] == STATUS_VALID ||
1262
10.8k
        
info->ineq[l] == 4.30k
STATUS_REDUNDANT4.30k
)
1263
10.8k
      
continue7.39k
;
1264
3.50k
    if (isl_seq_is_neg(bound, bmap->ineq[l], len))
1265
207
      continue;
1266
3.29k
    if (isl_seq_eq(bound, bmap->ineq[l], len))
1267
0
      continue;
1268
3.29k
    if (isl_tab_is_redundant(info->tab, bmap->n_eq + l))
1269
24
      continue;
1270
3.27k
1271
3.27k
    added = add_wrap(wraps, w, bound, bmap->ineq[l], len, set, 0);
1272
3.27k
    if (added < 0)
1273
0
      return isl_stat_error;
1274
3.27k
    if (!added)
1275
2.64k
      goto unbounded;
1276
630
    ++w;
1277
630
  }
1278
6.56k
  
for (l = 0; 2.06k
l < bmap->n_eq;
++l4.49k
) {
1279
4.90k
    if (isl_seq_is_neg(bound, bmap->eq[l], len))
1280
692
      continue;
1281
4.21k
    if (isl_seq_eq(bound, bmap->eq[l], len))
1282
854
      continue;
1283
3.35k
1284
9.44k
    
for (m = 0; 3.35k
m < 2;
++m6.08k
) {
1285
6.49k
      if (info->eq[2 * l + m] == STATUS_VALID)
1286
6.49k
        
continue4.77k
;
1287
1.71k
      added = add_wrap(wraps, w, bound, bmap->eq[l], len,
1288
1.71k
          set, !m);
1289
1.71k
      if (added < 0)
1290
0
        return isl_stat_error;
1291
1.71k
      if (!added)
1292
405
        goto unbounded;
1293
1.30k
      ++w;
1294
1.30k
    }
1295
4.90k
  }
1296
2.06k
1297
2.06k
  wraps->mat->n_row = w;
1298
1.66k
  return isl_stat_ok;
1299
3.04k
unbounded:
1300
3.04k
  wraps->mat->n_row = 0;
1301
3.04k
  return isl_stat_ok;
1302
4.70k
}
1303
1304
/* Check if the constraints in "wraps" from "first" until the last
1305
 * are all valid for the basic set represented by "tab".
1306
 * If not, wraps->n_row is set to zero.
1307
 */
1308
static int check_wraps(__isl_keep isl_mat *wraps, int first,
1309
  struct isl_tab *tab)
1310
231
{
1311
231
  int i;
1312
231
1313
241
  for (i = first; i < wraps->n_row; 
++i10
) {
1314
18
    enum isl_ineq_type type;
1315
18
    type = isl_tab_ineq_type(tab, wraps->row[i]);
1316
18
    if (type == isl_ineq_error)
1317
0
      return -1;
1318
18
    if (type == isl_ineq_redundant)
1319
10
      continue;
1320
8
    wraps->n_row = 0;
1321
8
    return 0;
1322
8
  }
1323
231
1324
231
  
return 0223
;
1325
231
}
1326
1327
/* Return a set that corresponds to the non-redundant constraints
1328
 * (as recorded in tab) of bmap.
1329
 *
1330
 * It's important to remove the redundant constraints as some
1331
 * of the other constraints may have been modified after the
1332
 * constraints were marked redundant.
1333
 * In particular, a constraint may have been relaxed.
1334
 * Redundant constraints are ignored when a constraint is relaxed
1335
 * and should therefore continue to be ignored ever after.
1336
 * Otherwise, the relaxation might be thwarted by some of
1337
 * these constraints.
1338
 *
1339
 * Update the underlying set to ensure that the dimension doesn't change.
1340
 * Otherwise the integer divisions could get dropped if the tab
1341
 * turns out to be empty.
1342
 */
1343
static __isl_give isl_set *set_from_updated_bmap(__isl_keep isl_basic_map *bmap,
1344
  struct isl_tab *tab)
1345
7.24k
{
1346
7.24k
  isl_basic_set *bset;
1347
7.24k
1348
7.24k
  bmap = isl_basic_map_copy(bmap);
1349
7.24k
  bset = isl_basic_map_underlying_set(bmap);
1350
7.24k
  bset = isl_basic_set_cow(bset);
1351
7.24k
  bset = isl_basic_set_update_from_tab(bset, tab);
1352
7.24k
  return isl_set_from_basic_set(bset);
1353
7.24k
}
1354
1355
/* Wrap the constraints of info->bmap that bound the facet defined
1356
 * by inequality "k" around (the opposite of) this inequality to
1357
 * include "set".  "bound" may be used to store the negated inequality.
1358
 * Since the wrapped constraints are not guaranteed to contain the whole
1359
 * of info->bmap, we check them in check_wraps.
1360
 * If any of the wrapped constraints turn out to be invalid, then
1361
 * check_wraps will reset wrap->n_row to zero.
1362
 */
1363
static isl_stat add_wraps_around_facet(struct isl_wraps *wraps,
1364
  struct isl_coalesce_info *info, int k, isl_int *bound,
1365
  __isl_keep isl_set *set)
1366
231
{
1367
231
  struct isl_tab_undo *snap;
1368
231
  int n;
1369
231
  unsigned total = isl_basic_map_total_dim(info->bmap);
1370
231
1371
231
  snap = isl_tab_snap(info->tab);
1372
231
1373
231
  if (isl_tab_select_facet(info->tab, info->bmap->n_eq + k) < 0)
1374
0
    return isl_stat_error;
1375
231
  if (isl_tab_detect_redundant(info->tab) < 0)
1376
0
    return isl_stat_error;
1377
231
1378
231
  isl_seq_neg(bound, info->bmap->ineq[k], 1 + total);
1379
231
1380
231
  n = wraps->mat->n_row;
1381
231
  if (add_wraps(wraps, info, bound, set) < 0)
1382
0
    return isl_stat_error;
1383
231
1384
231
  if (isl_tab_rollback(info->tab, snap) < 0)
1385
0
    return isl_stat_error;
1386
231
  if (check_wraps(wraps->mat, n, info->tab) < 0)
1387
0
    return isl_stat_error;
1388
231
1389
231
  return isl_stat_ok;
1390
231
}
1391
1392
/* Given a basic set i with a constraint k that is adjacent to
1393
 * basic set j, check if we can wrap
1394
 * both the facet corresponding to k (if "wrap_facet" is set) and basic map j
1395
 * (always) around their ridges to include the other set.
1396
 * If so, replace the pair of basic sets by their union.
1397
 *
1398
 * All constraints of i (except k) are assumed to be valid or
1399
 * cut constraints for j.
1400
 * Wrapping the cut constraints to include basic map j may result
1401
 * in constraints that are no longer valid of basic map i
1402
 * we have to check that the resulting wrapping constraints are valid for i.
1403
 * If "wrap_facet" is not set, then all constraints of i (except k)
1404
 * are assumed to be valid for j.
1405
 *        ____        _____
1406
 *       /    |      /     \
1407
 *      /     ||      /      |
1408
 *      \     ||    =>  \      |
1409
 *       \    ||     \     |
1410
 *        \___||      \____|
1411
 *
1412
 */
1413
static enum isl_change can_wrap_in_facet(int i, int j, int k,
1414
  struct isl_coalesce_info *info, int wrap_facet)
1415
1.33k
{
1416
1.33k
  enum isl_change change = isl_change_none;
1417
1.33k
  struct isl_wraps wraps;
1418
1.33k
  isl_ctx *ctx;
1419
1.33k
  isl_mat *mat;
1420
1.33k
  struct isl_set *set_i = NULL;
1421
1.33k
  struct isl_set *set_j = NULL;
1422
1.33k
  struct isl_vec *bound = NULL;
1423
1.33k
  unsigned total = isl_basic_map_total_dim(info[i].bmap);
1424
1.33k
1425
1.33k
  set_i = set_from_updated_bmap(info[i].bmap, info[i].tab);
1426
1.33k
  set_j = set_from_updated_bmap(info[j].bmap, info[j].tab);
1427
1.33k
  ctx = isl_basic_map_get_ctx(info[i].bmap);
1428
1.33k
  mat = isl_mat_alloc(ctx, 2 * (info[i].bmap->n_eq + info[j].bmap->n_eq) +
1429
1.33k
            info[i].bmap->n_ineq + info[j].bmap->n_ineq,
1430
1.33k
            1 + total);
1431
1.33k
  if (wraps_init(&wraps, mat, info, i, j) < 0)
1432
0
    goto error;
1433
1.33k
  bound = isl_vec_alloc(ctx, 1 + total);
1434
1.33k
  if (!set_i || !set_j || !bound)
1435
0
    goto error;
1436
1.33k
1437
1.33k
  isl_seq_cpy(bound->el, info[i].bmap->ineq[k], 1 + total);
1438
1.33k
  isl_int_add_ui(bound->el[0], bound->el[0], 1);
1439
1.33k
1440
1.33k
  isl_seq_cpy(wraps.mat->row[0], bound->el, 1 + total);
1441
1.33k
  wraps.mat->n_row = 1;
1442
1.33k
1443
1.33k
  if (add_wraps(&wraps, &info[j], bound->el, set_i) < 0)
1444
0
    goto error;
1445
1.33k
  if (!wraps.mat->n_row)
1446
1.03k
    goto unbounded;
1447
303
1448
303
  if (wrap_facet) {
1449
231
    if (add_wraps_around_facet(&wraps, &info[i], k,
1450
231
              bound->el, set_j) < 0)
1451
0
      goto error;
1452
231
    if (!wraps.mat->n_row)
1453
221
      goto unbounded;
1454
82
  }
1455
82
1456
82
  change = fuse(i, j, info, wraps.mat, 0, 0);
1457
82
1458
1.33k
unbounded:
1459
1.33k
  wraps_free(&wraps);
1460
1.33k
1461
1.33k
  isl_set_free(set_i);
1462
1.33k
  isl_set_free(set_j);
1463
1.33k
1464
1.33k
  isl_vec_free(bound);
1465
1.33k
1466
1.33k
  return change;
1467
0
error:
1468
0
  wraps_free(&wraps);
1469
0
  isl_vec_free(bound);
1470
0
  isl_set_free(set_i);
1471
0
  isl_set_free(set_j);
1472
0
  return isl_change_error;
1473
1.33k
}
1474
1475
/* Given a cut constraint t(x) >= 0 of basic map i, stored in row "w"
1476
 * of wrap.mat, replace it by its relaxed version t(x) + 1 >= 0, and
1477
 * add wrapping constraints to wrap.mat for all constraints
1478
 * of basic map j that bound the part of basic map j that sticks out
1479
 * of the cut constraint.
1480
 * "set_i" is the underlying set of basic map i.
1481
 * If any wrapping fails, then wraps->mat.n_row is reset to zero.
1482
 *
1483
 * In particular, we first intersect basic map j with t(x) + 1 = 0.
1484
 * If the result is empty, then t(x) >= 0 was actually a valid constraint
1485
 * (with respect to the integer points), so we add t(x) >= 0 instead.
1486
 * Otherwise, we wrap the constraints of basic map j that are not
1487
 * redundant in this intersection and that are not already valid
1488
 * for basic map i over basic map i.
1489
 * Note that it is sufficient to wrap the constraints to include
1490
 * basic map i, because we will only wrap the constraints that do
1491
 * not include basic map i already.  The wrapped constraint will
1492
 * therefore be more relaxed compared to the original constraint.
1493
 * Since the original constraint is valid for basic map j, so is
1494
 * the wrapped constraint.
1495
 */
1496
static isl_stat wrap_in_facet(struct isl_wraps *wraps, int w,
1497
  struct isl_coalesce_info *info_j, __isl_keep isl_set *set_i,
1498
  struct isl_tab_undo *snap)
1499
95
{
1500
95
  isl_int_add_ui(wraps->mat->row[w][0], wraps->mat->row[w][0], 1);
1501
95
  if (isl_tab_add_eq(info_j->tab, wraps->mat->row[w]) < 0)
1502
0
    return isl_stat_error;
1503
95
  if (isl_tab_detect_redundant(info_j->tab) < 0)
1504
0
    return isl_stat_error;
1505
95
1506
95
  if (info_j->tab->empty)
1507
95
    
isl_int_sub_ui0
(wraps->mat->row[w][0], wraps->mat->row[w][0], 1);
1508
95
  else if (add_wraps(wraps, info_j, wraps->mat->row[w], set_i) < 0)
1509
0
    return isl_stat_error;
1510
95
1511
95
  if (isl_tab_rollback(info_j->tab, snap) < 0)
1512
0
    return isl_stat_error;
1513
95
1514
95
  return isl_stat_ok;
1515
95
}
1516
1517
/* Given a pair of basic maps i and j such that j sticks out
1518
 * of i at n cut constraints, each time by at most one,
1519
 * try to compute wrapping constraints and replace the two
1520
 * basic maps by a single basic map.
1521
 * The other constraints of i are assumed to be valid for j.
1522
 * "set_i" is the underlying set of basic map i.
1523
 * "wraps" has been initialized to be of the right size.
1524
 *
1525
 * For each cut constraint t(x) >= 0 of i, we add the relaxed version
1526
 * t(x) + 1 >= 0, along with wrapping constraints for all constraints
1527
 * of basic map j that bound the part of basic map j that sticks out
1528
 * of the cut constraint.
1529
 *
1530
 * If any wrapping fails, i.e., if we cannot wrap to touch
1531
 * the union, then we give up.
1532
 * Otherwise, the pair of basic maps is replaced by their union.
1533
 */
1534
static enum isl_change try_wrap_in_facets(int i, int j,
1535
  struct isl_coalesce_info *info, struct isl_wraps *wraps,
1536
  __isl_keep isl_set *set_i)
1537
89
{
1538
89
  int k, l, w;
1539
89
  unsigned total;
1540
89
  struct isl_tab_undo *snap;
1541
89
1542
89
  total = isl_basic_map_total_dim(info[i].bmap);
1543
89
1544
89
  snap = isl_tab_snap(info[j].tab);
1545
89
1546
89
  wraps->mat->n_row = 0;
1547
89
1548
126
  for (k = 0; k < info[i].bmap->n_eq; 
++k37
) {
1549
142
    for (l = 0; l < 2; 
++l81
) {
1550
105
      if (info[i].eq[2 * k + l] != STATUS_CUT)
1551
105
        
continue70
;
1552
35
      w = wraps->mat->n_row++;
1553
35
      if (l == 0)
1554
21
        isl_seq_neg(wraps->mat->row[w],
1555
21
              info[i].bmap->eq[k], 1 + total);
1556
14
      else
1557
14
        isl_seq_cpy(wraps->mat->row[w],
1558
14
              info[i].bmap->eq[k], 1 + total);
1559
35
      if (wrap_in_facet(wraps, w, &info[j], set_i, snap) < 0)
1560
0
        return isl_change_error;
1561
35
1562
35
      if (!wraps->mat->n_row)
1563
24
        return isl_change_none;
1564
105
    }
1565
61
  }
1566
89
1567
189
  
for (k = 0; 65
k < info[i].bmap->n_ineq;
++k124
) {
1568
160
    if (info[i].ineq[k] != STATUS_CUT)
1569
160
      
continue100
;
1570
60
    w = wraps->mat->n_row++;
1571
60
    isl_seq_cpy(wraps->mat->row[w],
1572
60
          info[i].bmap->ineq[k], 1 + total);
1573
60
    if (wrap_in_facet(wraps, w, &info[j], set_i, snap) < 0)
1574
0
      return isl_change_error;
1575
60
1576
60
    if (!wraps->mat->n_row)
1577
36
      return isl_change_none;
1578
160
  }
1579
65
1580
65
  
return fuse(i, j, info, wraps->mat, 0, 1)29
;
1581
89
}
1582
1583
/* Given a pair of basic maps i and j such that j sticks out
1584
 * of i at n cut constraints, each time by at most one,
1585
 * try to compute wrapping constraints and replace the two
1586
 * basic maps by a single basic map.
1587
 * The other constraints of i are assumed to be valid for j.
1588
 *
1589
 * The core computation is performed by try_wrap_in_facets.
1590
 * This function simply extracts an underlying set representation
1591
 * of basic map i and initializes the data structure for keeping
1592
 * track of wrapping constraints.
1593
 */
1594
static enum isl_change wrap_in_facets(int i, int j, int n,
1595
  struct isl_coalesce_info *info)
1596
89
{
1597
89
  enum isl_change change = isl_change_none;
1598
89
  struct isl_wraps wraps;
1599
89
  isl_ctx *ctx;
1600
89
  isl_mat *mat;
1601
89
  isl_set *set_i = NULL;
1602
89
  unsigned total = isl_basic_map_total_dim(info[i].bmap);
1603
89
  int max_wrap;
1604
89
1605
89
  if (isl_tab_extend_cons(info[j].tab, 1) < 0)
1606
0
    return isl_change_error;
1607
89
1608
89
  max_wrap = 1 + 2 * info[j].bmap->n_eq + info[j].bmap->n_ineq;
1609
89
  max_wrap *= n;
1610
89
1611
89
  set_i = set_from_updated_bmap(info[i].bmap, info[i].tab);
1612
89
  ctx = isl_basic_map_get_ctx(info[i].bmap);
1613
89
  mat = isl_mat_alloc(ctx, max_wrap, 1 + total);
1614
89
  if (wraps_init(&wraps, mat, info, i, j) < 0)
1615
0
    goto error;
1616
89
  if (!set_i)
1617
0
    goto error;
1618
89
1619
89
  change = try_wrap_in_facets(i, j, info, &wraps, set_i);
1620
89
1621
89
  wraps_free(&wraps);
1622
89
  isl_set_free(set_i);
1623
89
1624
89
  return change;
1625
0
error:
1626
0
  wraps_free(&wraps);
1627
0
  isl_set_free(set_i);
1628
0
  return isl_change_error;
1629
89
}
1630
1631
/* Return the effect of inequality "ineq" on the tableau "tab",
1632
 * after relaxing the constant term of "ineq" by one.
1633
 */
1634
static enum isl_ineq_type type_of_relaxed(struct isl_tab *tab, isl_int *ineq)
1635
26.9k
{
1636
26.9k
  enum isl_ineq_type type;
1637
26.9k
1638
26.9k
  isl_int_add_ui(ineq[0], ineq[0], 1);
1639
26.9k
  type = isl_tab_ineq_type(tab, ineq);
1640
26.9k
  isl_int_sub_ui(ineq[0], ineq[0], 1);
1641
26.9k
1642
26.9k
  return type;
1643
26.9k
}
1644
1645
/* Given two basic sets i and j,
1646
 * check if relaxing all the cut constraints of i by one turns
1647
 * them into valid constraint for j and check if we can wrap in
1648
 * the bits that are sticking out.
1649
 * If so, replace the pair by their union.
1650
 *
1651
 * We first check if all relaxed cut inequalities of i are valid for j
1652
 * and then try to wrap in the intersections of the relaxed cut inequalities
1653
 * with j.
1654
 *
1655
 * During this wrapping, we consider the points of j that lie at a distance
1656
 * of exactly 1 from i.  In particular, we ignore the points that lie in
1657
 * between this lower-dimensional space and the basic map i.
1658
 * We can therefore only apply this to integer maps.
1659
 *        ____        _____
1660
 *       / ___|_     /     \
1661
 *      / |    |      /      |
1662
 *      \ |    |    =>  \      |
1663
 *       \|____|     \     |
1664
 *        \___|       \____/
1665
 *
1666
 *   _____       ______
1667
 *  | ____|_    |      \
1668
 *  | |     |   |       |
1669
 *  | | | =>  |       |
1670
 *  |_|     |   |       |
1671
 *    |_____|    \______|
1672
 *
1673
 *   _______
1674
 *  |       |
1675
 *  |  |\   |
1676
 *  |  | \  |
1677
 *  |  |  \ |
1678
 *  |  |   \|
1679
 *  |  |    \
1680
 *  |  |_____\
1681
 *  |       |
1682
 *  |_______|
1683
 *
1684
 * Wrapping can fail if the result of wrapping one of the facets
1685
 * around its edges does not produce any new facet constraint.
1686
 * In particular, this happens when we try to wrap in unbounded sets.
1687
 *
1688
 *   _______________________________________________________________________
1689
 *  |
1690
 *  |  ___
1691
 *  | |   |
1692
 *  |_|   |_________________________________________________________________
1693
 *    |___|
1694
 *
1695
 * The following is not an acceptable result of coalescing the above two
1696
 * sets as it includes extra integer points.
1697
 *   _______________________________________________________________________
1698
 *  |
1699
 *  |     
1700
 *  |      
1701
 *  |
1702
 *   \______________________________________________________________________
1703
 */
1704
static enum isl_change can_wrap_in_set(int i, int j,
1705
  struct isl_coalesce_info *info)
1706
31.4k
{
1707
31.4k
  int k, l;
1708
31.4k
  int n;
1709
31.4k
  unsigned total;
1710
31.4k
1711
31.4k
  if (ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_RATIONAL) ||
1712
31.4k
      
ISL_F_ISSET26.6k
(info[j].bmap, ISL_BASIC_MAP_RATIONAL))
1713
31.4k
    
return isl_change_none4.79k
;
1714
26.6k
1715
26.6k
  n = count(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT);
1716
26.6k
  n += count(info[i].ineq, info[i].bmap->n_ineq, STATUS_CUT);
1717
26.6k
  if (n == 0)
1718
0
    return isl_change_none;
1719
26.6k
1720
26.6k
  total = isl_basic_map_total_dim(info[i].bmap);
1721
26.9k
  for (k = 0; k < info[i].bmap->n_eq; 
++k294
) {
1722
6.71k
    for (l = 0; l < 2; 
++l746
) {
1723
6.41k
      enum isl_ineq_type type;
1724
6.41k
1725
6.41k
      if (info[i].eq[2 * k + l] != STATUS_CUT)
1726
6.41k
        
continue660
;
1727
5.75k
1728
5.75k
      if (l == 0)
1729
5.56k
        isl_seq_neg(info[i].bmap->eq[k],
1730
5.56k
              info[i].bmap->eq[k], 1 + total);
1731
5.75k
      type = type_of_relaxed(info[j].tab,
1732
5.75k
              info[i].bmap->eq[k]);
1733
5.75k
      if (l == 0)
1734
5.56k
        isl_seq_neg(info[i].bmap->eq[k],
1735
5.56k
              info[i].bmap->eq[k], 1 + total);
1736
5.75k
      if (type == isl_ineq_error)
1737
0
        return isl_change_error;
1738
5.75k
      if (type != isl_ineq_redundant)
1739
5.67k
        return isl_change_none;
1740
6.41k
    }
1741
5.96k
  }
1742
26.6k
1743
26.6k
  
for (k = 0; 20.9k
k < info[i].bmap->n_ineq23.8k
;
++k2.85k
) {
1744
23.7k
    enum isl_ineq_type type;
1745
23.7k
1746
23.7k
    if (info[i].ineq[k] != STATUS_CUT)
1747
23.7k
      
continue2.76k
;
1748
20.9k
1749
20.9k
    type = type_of_relaxed(info[j].tab, info[i].bmap->ineq[k]);
1750
20.9k
    if (type == isl_ineq_error)
1751
0
      return isl_change_error;
1752
20.9k
    if (type != isl_ineq_redundant)
1753
20.8k
      return isl_change_none;
1754
23.7k
  }
1755
20.9k
1756
20.9k
  
return wrap_in_facets(i, j, n, info)89
;
1757
31.4k
}
1758
1759
/* Check if either i or j has only cut constraints that can
1760
 * be used to wrap in (a facet of) the other basic set.
1761
 * if so, replace the pair by their union.
1762
 */
1763
static enum isl_change check_wrap(int i, int j, struct isl_coalesce_info *info)
1764
15.7k
{
1765
15.7k
  enum isl_change change = isl_change_none;
1766
15.7k
1767
15.7k
  change = can_wrap_in_set(i, j, info);
1768
15.7k
  if (change != isl_change_none)
1769
25
    return change;
1770
15.6k
1771
15.6k
  change = can_wrap_in_set(j, i, info);
1772
15.6k
  return change;
1773
15.6k
}
1774
1775
/* Check if all inequality constraints of "i" that cut "j" cease
1776
 * to be cut constraints if they are relaxed by one.
1777
 * If so, collect the cut constraints in "list".
1778
 * The caller is responsible for allocating "list".
1779
 */
1780
static isl_bool all_cut_by_one(int i, int j, struct isl_coalesce_info *info,
1781
  int *list)
1782
256
{
1783
256
  int l, n;
1784
256
1785
256
  n = 0;
1786
848
  for (l = 0; l < info[i].bmap->n_ineq; 
++l592
) {
1787
829
    enum isl_ineq_type type;
1788
829
1789
829
    if (info[i].ineq[l] != STATUS_CUT)
1790
829
      
continue568
;
1791
261
    type = type_of_relaxed(info[j].tab, info[i].bmap->ineq[l]);
1792
261
    if (type == isl_ineq_error)
1793
0
      return isl_bool_error;
1794
261
    if (type != isl_ineq_redundant)
1795
237
      return isl_bool_false;
1796
24
    list[n++] = l;
1797
24
  }
1798
256
1799
256
  
return isl_bool_true19
;
1800
256
}
1801
1802
/* Given two basic maps such that "j" has at least one equality constraint
1803
 * that is adjacent to an inequality constraint of "i" and such that "i" has
1804
 * exactly one inequality constraint that is adjacent to an equality
1805
 * constraint of "j", check whether "i" can be extended to include "j" or
1806
 * whether "j" can be wrapped into "i".
1807
 * All remaining constraints of "i" and "j" are assumed to be valid
1808
 * or cut constraints of the other basic map.
1809
 * However, none of the equality constraints of "i" are cut constraints.
1810
 *
1811
 * If "i" has any "cut" inequality constraints, then check if relaxing
1812
 * each of them by one is sufficient for them to become valid.
1813
 * If so, check if the inequality constraint adjacent to an equality
1814
 * constraint of "j" along with all these cut constraints
1815
 * can be relaxed by one to contain exactly "j".
1816
 * Otherwise, or if this fails, check if "j" can be wrapped into "i".
1817
 */
1818
static enum isl_change check_single_adj_eq(int i, int j,
1819
  struct isl_coalesce_info *info)
1820
1.91k
{
1821
1.91k
  enum isl_change change = isl_change_none;
1822
1.91k
  int k;
1823
1.91k
  int n_cut;
1824
1.91k
  int *relax;
1825
1.91k
  isl_ctx *ctx;
1826
1.91k
  isl_bool try_relax;
1827
1.91k
1828
1.91k
  n_cut = count(info[i].ineq, info[i].bmap->n_ineq, STATUS_CUT);
1829
1.91k
1830
1.91k
  k = find(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_EQ);
1831
1.91k
1832
1.91k
  if (n_cut > 0) {
1833
256
    ctx = isl_basic_map_get_ctx(info[i].bmap);
1834
256
    relax = isl_calloc_array(ctx, int, 1 + n_cut);
1835
256
    if (!relax)
1836
0
      return isl_change_error;
1837
256
    relax[0] = k;
1838
256
    try_relax = all_cut_by_one(i, j, info, relax + 1);
1839
256
    if (try_relax < 0)
1840
0
      change = isl_change_error;
1841
1.65k
  } else {
1842
1.65k
    try_relax = isl_bool_true;
1843
1.65k
    relax = &k;
1844
1.65k
  }
1845
1.91k
  if (try_relax && 
change == isl_change_none1.67k
)
1846
1.67k
    change = is_relaxed_extension(i, j, 1 + n_cut, relax, info);
1847
1.91k
  if (n_cut > 0)
1848
256
    free(relax);
1849
1.91k
  if (change != isl_change_none)
1850
577
    return change;
1851
1.33k
1852
1.33k
  change = can_wrap_in_facet(i, j, k, info, n_cut > 0);
1853
1.33k
1854
1.33k
  return change;
1855
1.33k
}
1856
1857
/* At least one of the basic maps has an equality that is adjacent
1858
 * to inequality.  Make sure that only one of the basic maps has
1859
 * such an equality and that the other basic map has exactly one
1860
 * inequality adjacent to an equality.
1861
 * If the other basic map does not have such an inequality, then
1862
 * check if all its constraints are either valid or cut constraints
1863
 * and, if so, try wrapping in the first map into the second.
1864
 * Otherwise, try to extend one basic map with the other or
1865
 * wrap one basic map in the other.
1866
 */
1867
static enum isl_change check_adj_eq(int i, int j,
1868
  struct isl_coalesce_info *info)
1869
3.61k
{
1870
3.61k
  if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_INEQ) &&
1871
3.61k
      
any(info[j].eq, 2 * info[j].bmap->n_eq, 792
STATUS_ADJ_INEQ792
))
1872
133
    /* ADJ EQ TOO MANY */
1873
133
    return isl_change_none;
1874
3.48k
1875
3.48k
  if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_INEQ))
1876
659
    return check_adj_eq(j, i, info);
1877
2.82k
1878
2.82k
  /* j has an equality adjacent to an inequality in i */
1879
2.82k
1880
2.82k
  if (count(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_EQ) != 1) {
1881
535
    if (all_valid_or_cut(&info[i]))
1882
43
      return can_wrap_in_set(i, j, info);
1883
492
    return isl_change_none;
1884
492
  }
1885
2.29k
  if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT))
1886
366
    return isl_change_none;
1887
1.92k
  if (any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_EQ) ||
1888
1.92k
      any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_INEQ) ||
1889
1.92k
      
any(info[j].ineq, info[j].bmap->n_ineq, 1.91k
STATUS_ADJ_INEQ1.91k
))
1890
14
    /* ADJ EQ TOO MANY */
1891
14
    return isl_change_none;
1892
1.91k
1893
1.91k
  return check_single_adj_eq(i, j, info);
1894
1.91k
}
1895
1896
/* The two basic maps lie on adjacent hyperplanes.  In particular,
1897
 * basic map "i" has an equality that lies parallel to basic map "j".
1898
 * Check if we can wrap the facets around the parallel hyperplanes
1899
 * to include the other set.
1900
 *
1901
 * We perform basically the same operations as can_wrap_in_facet,
1902
 * except that we don't need to select a facet of one of the sets.
1903
 *        _
1904
 *  \\      \\
1905
 *   \\   =>   \\
1906
 *    \       \|
1907
 *
1908
 * If there is more than one equality of "i" adjacent to an equality of "j",
1909
 * then the result will satisfy one or more equalities that are a linear
1910
 * combination of these equalities.  These will be encoded as pairs
1911
 * of inequalities in the wrapping constraints and need to be made
1912
 * explicit.
1913
 */
1914
static enum isl_change check_eq_adj_eq(int i, int j,
1915
  struct isl_coalesce_info *info)
1916
2.24k
{
1917
2.24k
  int k;
1918
2.24k
  enum isl_change change = isl_change_none;
1919
2.24k
  int detect_equalities = 0;
1920
2.24k
  struct isl_wraps wraps;
1921
2.24k
  isl_ctx *ctx;
1922
2.24k
  isl_mat *mat;
1923
2.24k
  struct isl_set *set_i = NULL;
1924
2.24k
  struct isl_set *set_j = NULL;
1925
2.24k
  struct isl_vec *bound = NULL;
1926
2.24k
  unsigned total = isl_basic_map_total_dim(info[i].bmap);
1927
2.24k
1928
2.24k
  if (count(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_EQ) != 1)
1929
539
    detect_equalities = 1;
1930
2.24k
1931
2.24k
  k = find(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_EQ);
1932
2.24k
1933
2.24k
  set_i = set_from_updated_bmap(info[i].bmap, info[i].tab);
1934
2.24k
  set_j = set_from_updated_bmap(info[j].bmap, info[j].tab);
1935
2.24k
  ctx = isl_basic_map_get_ctx(info[i].bmap);
1936
2.24k
  mat = isl_mat_alloc(ctx, 2 * (info[i].bmap->n_eq + info[j].bmap->n_eq) +
1937
2.24k
            info[i].bmap->n_ineq + info[j].bmap->n_ineq,
1938
2.24k
            1 + total);
1939
2.24k
  if (wraps_init(&wraps, mat, info, i, j) < 0)
1940
0
    goto error;
1941
2.24k
  bound = isl_vec_alloc(ctx, 1 + total);
1942
2.24k
  if (!set_i || !set_j || !bound)
1943
0
    goto error;
1944
2.24k
1945
2.24k
  if (k % 2 == 0)
1946
943
    isl_seq_neg(bound->el, info[i].bmap->eq[k / 2], 1 + total);
1947
1.30k
  else
1948
1.30k
    isl_seq_cpy(bound->el, info[i].bmap->eq[k / 2], 1 + total);
1949
2.24k
  isl_int_add_ui(bound->el[0], bound->el[0], 1);
1950
2.24k
1951
2.24k
  isl_seq_cpy(wraps.mat->row[0], bound->el, 1 + total);
1952
2.24k
  wraps.mat->n_row = 1;
1953
2.24k
1954
2.24k
  if (add_wraps(&wraps, &info[j], bound->el, set_i) < 0)
1955
0
    goto error;
1956
2.24k
  if (!wraps.mat->n_row)
1957
1.44k
    goto unbounded;
1958
805
1959
805
  isl_int_sub_ui(bound->el[0], bound->el[0], 1);
1960
805
  isl_seq_neg(bound->el, bound->el, 1 + total);
1961
805
1962
805
  isl_seq_cpy(wraps.mat->row[wraps.mat->n_row], bound->el, 1 + total);
1963
805
  wraps.mat->n_row++;
1964
805
1965
805
  if (add_wraps(&wraps, &info[i], bound->el, set_j) < 0)
1966
0
    goto error;
1967
805
  if (!wraps.mat->n_row)
1968
304
    goto unbounded;
1969
501
1970
501
  change = fuse(i, j, info, wraps.mat, detect_equalities, 0);
1971
501
1972
501
  if (0) {
1973
0
error:    change = isl_change_error;
1974
0
  }
1975
2.24k
unbounded:
1976
2.24k
1977
2.24k
  wraps_free(&wraps);
1978
2.24k
  isl_set_free(set_i);
1979
2.24k
  isl_set_free(set_j);
1980
2.24k
  isl_vec_free(bound);
1981
2.24k
1982
2.24k
  return change;
1983
2.24k
}
1984
1985
/* Initialize the "eq" and "ineq" fields of "info".
1986
 */
1987
static void init_status(struct isl_coalesce_info *info)
1988
129k
{
1989
129k
  info->eq = info->ineq = NULL;
1990
129k
}
1991
1992
/* Set info->eq to the positions of the equalities of info->bmap
1993
 * with respect to the basic map represented by "tab".
1994
 * If info->eq has already been computed, then do not compute it again.
1995
 */
1996
static void set_eq_status_in(struct isl_coalesce_info *info,
1997
  struct isl_tab *tab)
1998
80.2k
{
1999
80.2k
  if (info->eq)
2000
2.73k
    return;
2001
77.5k
  info->eq = eq_status_in(info->bmap, tab);
2002
77.5k
}
2003
2004
/* Set info->ineq to the positions of the inequalities of info->bmap
2005
 * with respect to the basic map represented by "tab".
2006
 * If info->ineq has already been computed, then do not compute it again.
2007
 */
2008
static void set_ineq_status_in(struct isl_coalesce_info *info,
2009
  struct isl_tab *tab)
2010
106k
{
2011
106k
  if (info->ineq)
2012
2.73k
    return;
2013
103k
  info->ineq = ineq_status_in(info->bmap, info->tab, tab);
2014
103k
}
2015
2016
/* Free the memory allocated by the "eq" and "ineq" fields of "info".
2017
 * This function assumes that init_status has been called on "info" first,
2018
 * after which the "eq" and "ineq" fields may or may not have been
2019
 * assigned a newly allocated array.
2020
 */
2021
static void clear_status(struct isl_coalesce_info *info)
2022
129k
{
2023
129k
  free(info->eq);
2024
129k
  free(info->ineq);
2025
129k
}
2026
2027
/* Are all inequality constraints of the basic map represented by "info"
2028
 * valid for the other basic map, except for a single constraint
2029
 * that is adjacent to an inequality constraint of the other basic map?
2030
 */
2031
static int all_ineq_valid_or_single_adj_ineq(struct isl_coalesce_info *info)
2032
42
{
2033
42
  int i;
2034
42
  int k = -1;
2035
42
2036
154
  for (i = 0; i < info->bmap->n_ineq; 
++i112
) {
2037
145
    if (info->ineq[i] == STATUS_REDUNDANT)
2038
145
      
continue20
;
2039
125
    if (info->ineq[i] == STATUS_VALID)
2040
125
      
continue77
;
2041
48
    if (info->ineq[i] != STATUS_ADJ_INEQ)
2042
48
      
return 032
;
2043
16
    if (k != -1)
2044
1
      return 0;
2045
15
    k = i;
2046
15
  }
2047
42
2048
42
  
return k != -19
;
2049
42
}
2050
2051
/* Basic map "i" has one or more equality constraints that separate it
2052
 * from basic map "j".  Check if it happens to be an extension
2053
 * of basic map "j".
2054
 * In particular, check that all constraints of "j" are valid for "i",
2055
 * except for one inequality constraint that is adjacent
2056
 * to an inequality constraints of "i".
2057
 * If so, check for "i" being an extension of "j" by calling
2058
 * is_adj_ineq_extension.
2059
 *
2060
 * Clean up the memory allocated for keeping track of the status
2061
 * of the constraints before returning.
2062
 */
2063
static enum isl_change separating_equality(int i, int j,
2064
  struct isl_coalesce_info *info)
2065
5.06k
{
2066
5.06k
  enum isl_change change = isl_change_none;
2067
5.06k
2068
5.06k
  if (all(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_VALID) &&
2069
5.06k
      
all_ineq_valid_or_single_adj_ineq(&info[j])42
)
2070
9
    change = is_adj_ineq_extension(j, i, info);
2071
5.06k
2072
5.06k
  clear_status(&info[i]);
2073
5.06k
  clear_status(&info[j]);
2074
5.06k
  return change;
2075
5.06k
}
2076
2077
/* Check if the union of the given pair of basic maps
2078
 * can be represented by a single basic map.
2079
 * If so, replace the pair by the single basic map and return
2080
 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2081
 * Otherwise, return isl_change_none.
2082
 * The two basic maps are assumed to live in the same local space.
2083
 * The "eq" and "ineq" fields of info[i] and info[j] are assumed
2084
 * to have been initialized by the caller, either to NULL or
2085
 * to valid information.
2086
 *
2087
 * We first check the effect of each constraint of one basic map
2088
 * on the other basic map.
2089
 * The constraint may be
2090
 *  redundant the constraint is redundant in its own
2091
 *      basic map and should be ignore and removed
2092
 *      in the end
2093
 *  valid   all (integer) points of the other basic map
2094
 *      satisfy the constraint
2095
 *  separate  no (integer) point of the other basic map
2096
 *      satisfies the constraint
2097
 *  cut   some but not all points of the other basic map
2098
 *      satisfy the constraint
2099
 *  adj_eq    the given constraint is adjacent (on the outside)
2100
 *      to an equality of the other basic map
2101
 *  adj_ineq  the given constraint is adjacent (on the outside)
2102
 *      to an inequality of the other basic map
2103
 *
2104
 * We consider seven cases in which we can replace the pair by a single
2105
 * basic map.  We ignore all "redundant" constraints.
2106
 *
2107
 *  1. all constraints of one basic map are valid
2108
 *    => the other basic map is a subset and can be removed
2109
 *
2110
 *  2. all constraints of both basic maps are either "valid" or "cut"
2111
 *     and the facets corresponding to the "cut" constraints
2112
 *     of one of the basic maps lies entirely inside the other basic map
2113
 *    => the pair can be replaced by a basic map consisting
2114
 *       of the valid constraints in both basic maps
2115
 *
2116
 *  3. there is a single pair of adjacent inequalities
2117
 *     (all other constraints are "valid")
2118
 *    => the pair can be replaced by a basic map consisting
2119
 *       of the valid constraints in both basic maps
2120
 *
2121
 *  4. one basic map has a single adjacent inequality, while the other
2122
 *     constraints are "valid".  The other basic map has some
2123
 *     "cut" constraints, but replacing the adjacent inequality by
2124
 *     its opposite and adding the valid constraints of the other
2125
 *     basic map results in a subset of the other basic map
2126
 *    => the pair can be replaced by a basic map consisting
2127
 *       of the valid constraints in both basic maps
2128
 *
2129
 *  5. there is a single adjacent pair of an inequality and an equality,
2130
 *     the other constraints of the basic map containing the inequality are
2131
 *     "valid".  Moreover, if the inequality the basic map is relaxed
2132
 *     and then turned into an equality, then resulting facet lies
2133
 *     entirely inside the other basic map
2134
 *    => the pair can be replaced by the basic map containing
2135
 *       the inequality, with the inequality relaxed.
2136
 *
2137
 *  6. there is a single adjacent pair of an inequality and an equality,
2138
 *     the other constraints of the basic map containing the inequality are
2139
 *     "valid".  Moreover, the facets corresponding to both
2140
 *     the inequality and the equality can be wrapped around their
2141
 *     ridges to include the other basic map
2142
 *    => the pair can be replaced by a basic map consisting
2143
 *       of the valid constraints in both basic maps together
2144
 *       with all wrapping constraints
2145
 *
2146
 *  7. one of the basic maps extends beyond the other by at most one.
2147
 *     Moreover, the facets corresponding to the cut constraints and
2148
 *     the pieces of the other basic map at offset one from these cut
2149
 *     constraints can be wrapped around their ridges to include
2150
 *     the union of the two basic maps
2151
 *    => the pair can be replaced by a basic map consisting
2152
 *       of the valid constraints in both basic maps together
2153
 *       with all wrapping constraints
2154
 *
2155
 *  8. the two basic maps live in adjacent hyperplanes.  In principle
2156
 *     such sets can always be combined through wrapping, but we impose
2157
 *     that there is only one such pair, to avoid overeager coalescing.
2158
 *
2159
 * Throughout the computation, we maintain a collection of tableaus
2160
 * corresponding to the basic maps.  When the basic maps are dropped
2161
 * or combined, the tableaus are modified accordingly.
2162
 */
2163
static enum isl_change coalesce_local_pair_reuse(int i, int j,
2164
  struct isl_coalesce_info *info)
2165
62.6k
{
2166
62.6k
  enum isl_change change = isl_change_none;
2167
62.6k
2168
62.6k
  set_ineq_status_in(&info[i], info[j].tab);
2169
62.6k
  if (info[i].bmap->n_ineq && 
!info[i].ineq55.7k
)
2170
0
    goto error;
2171
62.6k
  if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ERROR))
2172
0
    goto error;
2173
62.6k
  if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_SEPARATE))
2174
18.8k
    goto done;
2175
43.7k
2176
43.7k
  set_ineq_status_in(&info[j], info[i].tab);
2177
43.7k
  if (info[j].bmap->n_ineq && 
!info[j].ineq37.5k
)
2178
0
    goto error;
2179
43.7k
  if (any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ERROR))
2180
0
    goto error;
2181
43.7k
  if (any(info[j].ineq, info[j].bmap->n_ineq, STATUS_SEPARATE))
2182
3.59k
    goto done;
2183
40.1k
2184
40.1k
  set_eq_status_in(&info[i], info[j].tab);
2185
40.1k
  if (info[i].bmap->n_eq && 
!info[i].eq18.0k
)
2186
0
    goto error;
2187
40.1k
  if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ERROR))
2188
0
    goto error;
2189
40.1k
2190
40.1k
  set_eq_status_in(&info[j], info[i].tab);
2191
40.1k
  if (info[j].bmap->n_eq && 
!info[j].eq17.9k
)
2192
0
    goto error;
2193
40.1k
  if (any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ERROR))
2194
0
    goto error;
2195
40.1k
2196
40.1k
  if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_SEPARATE))
2197
4.65k
    return separating_equality(i, j, info);
2198
35.4k
  if (any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_SEPARATE))
2199
403
    return separating_equality(j, i, info);
2200
35.0k
2201
35.0k
  if (all(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_VALID) &&
2202
35.0k
      
all(info[i].ineq, info[i].bmap->n_ineq, 23.9k
STATUS_VALID23.9k
)) {
2203
2.85k
    drop(&info[j]);
2204
2.85k
    change = isl_change_drop_second;
2205
32.2k
  } else if (all(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_VALID) &&
2206
32.2k
       
all(info[j].ineq, info[j].bmap->n_ineq, 22.3k
STATUS_VALID22.3k
)) {
2207
2.93k
    drop(&info[i]);
2208
2.93k
    change = isl_change_drop_first;
2209
29.2k
  } else if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_EQ)) {
2210
1.93k
    change = check_eq_adj_eq(i, j, info);
2211
27.3k
  } else if (any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ADJ_EQ)) {
2212
310
    change = check_eq_adj_eq(j, i, info);
2213
27.0k
  } else if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_INEQ) ||
2214
27.0k
       
any(info[j].eq, 2 * info[j].bmap->n_eq, 26.2k
STATUS_ADJ_INEQ26.2k
)) {
2215
2.95k
    change = check_adj_eq(i, j, info);
2216
24.0k
  } else if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_EQ) ||
2217
24.0k
       
any(info[j].ineq, info[j].bmap->n_ineq, 23.9k
STATUS_ADJ_EQ23.9k
)) {
2218
211
    /* Can't happen */
2219
211
    /* BAD ADJ INEQ */
2220
23.8k
  } else if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_INEQ) ||
2221
23.8k
       
any(info[j].ineq, info[j].bmap->n_ineq, 15.7k
STATUS_ADJ_INEQ15.7k
)) {
2222
8.13k
    change = check_adj_ineq(i, j, info);
2223
15.7k
  } else {
2224
15.7k
    if (!any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT) &&
2225
15.7k
        
!any(info[j].eq, 2 * info[j].bmap->n_eq, 10.5k
STATUS_CUT10.5k
))
2226
10.4k
      change = check_facets(i, j, info);
2227
15.7k
    if (change == isl_change_none)
2228
15.7k
      change = check_wrap(i, j, info);
2229
32.2k
  }
2230
35.0k
2231
57.5k
done:
2232
57.5k
  clear_status(&info[i]);
2233
57.5k
  clear_status(&info[j]);
2234
57.5k
  return change;
2235
0
error:
2236
0
  clear_status(&info[i]);
2237
0
  clear_status(&info[j]);
2238
0
  return isl_change_error;
2239
62.6k
}
2240
2241
/* Check if the union of the given pair of basic maps
2242
 * can be represented by a single basic map.
2243
 * If so, replace the pair by the single basic map and return
2244
 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2245
 * Otherwise, return isl_change_none.
2246
 * The two basic maps are assumed to live in the same local space.
2247
 */
2248
static enum isl_change coalesce_local_pair(int i, int j,
2249
  struct isl_coalesce_info *info)
2250
59.7k
{
2251
59.7k
  init_status(&info[i]);
2252
59.7k
  init_status(&info[j]);
2253
59.7k
  return coalesce_local_pair_reuse(i, j, info);
2254
59.7k
}
2255
2256
/* Shift the integer division at position "div" of the basic map
2257
 * represented by "info" by "shift".
2258
 *
2259
 * That is, if the integer division has the form
2260
 *
2261
 *  floor(f(x)/d)
2262
 *
2263
 * then replace it by
2264
 *
2265
 *  floor((f(x) + shift * d)/d) - shift
2266
 */
2267
static isl_stat shift_div(struct isl_coalesce_info *info, int div,
2268
  isl_int shift)
2269
136
{
2270
136
  unsigned total;
2271
136
2272
136
  info->bmap = isl_basic_map_shift_div(info->bmap, div, 0, shift);
2273
136
  if (!info->bmap)
2274
0
    return isl_stat_error;
2275
136
2276
136
  total = isl_basic_map_dim(info->bmap, isl_dim_all);
2277
136
  total -= isl_basic_map_dim(info->bmap, isl_dim_div);
2278
136
  if (isl_tab_shift_var(info->tab, total + div, shift) < 0)
2279
0
    return isl_stat_error;
2280
136
2281
136
  return isl_stat_ok;
2282
136
}
2283
2284
/* If the integer division at position "div" is defined by an equality,
2285
 * i.e., a stride constraint, then change the integer division expression
2286
 * to have a constant term equal to zero.
2287
 *
2288
 * Let the equality constraint be
2289
 *
2290
 *  c + f + m a = 0
2291
 *
2292
 * The integer division expression is then of the form
2293
 *
2294
 *  a = floor((-f - c')/m)
2295
 *
2296
 * The integer division is first shifted by t = floor(c/m),
2297
 * turning the equality constraint into
2298
 *
2299
 *  c - m floor(c/m) + f + m a' = 0
2300
 *
2301
 * i.e.,
2302
 *
2303
 *  (c mod m) + f + m a' = 0
2304
 *
2305
 * That is,
2306
 *
2307
 *  a' = (-f - (c mod m))/m = floor((-f)/m)
2308
 *
2309
 * because a' is an integer and 0 <= (c mod m) < m.
2310
 * The constant term of a' can therefore be zeroed out.
2311
 */
2312
static isl_stat normalize_stride_div(struct isl_coalesce_info *info, int div)
2313
254
{
2314
254
  isl_bool defined;
2315
254
  isl_stat r;
2316
254
  isl_constraint *c;
2317
254
  isl_int shift, stride;
2318
254
2319
254
  defined = isl_basic_map_has_defining_equality(info->bmap, isl_dim_div,
2320
254
              div, &c);
2321
254
  if (defined < 0)
2322
0
    return isl_stat_error;
2323
254
  if (!defined)
2324
121
    return isl_stat_ok;
2325
133
  if (!c)
2326
0
    return isl_stat_error;
2327
133
  isl_int_init(shift);
2328
133
  isl_int_init(stride);
2329
133
  isl_constraint_get_constant(c, &shift);
2330
133
  isl_constraint_get_coefficient(c, isl_dim_div, div, &stride);
2331
133
  isl_int_fdiv_q(shift, shift, stride);
2332
133
  r = shift_div(info, div, shift);
2333
133
  isl_int_clear(stride);
2334
133
  isl_int_clear(shift);
2335
133
  isl_constraint_free(c);
2336
133
  if (r < 0)
2337
0
    return isl_stat_error;
2338
133
  info->bmap = isl_basic_map_set_div_expr_constant_num_si_inplace(
2339
133
                  info->bmap, div, 0);
2340
133
  if (!info->bmap)
2341
0
    return isl_stat_error;
2342
133
  return isl_stat_ok;
2343
133
}
2344
2345
/* The basic maps represented by "info1" and "info2" are known
2346
 * to have the same number of integer divisions.
2347
 * Check if pairs of integer divisions are equal to each other
2348
 * despite the fact that they differ by a rational constant.
2349
 *
2350
 * In particular, look for any pair of integer divisions that
2351
 * only differ in their constant terms.
2352
 * If either of these integer divisions is defined
2353
 * by stride constraints, then modify it to have a zero constant term.
2354
 * If both are defined by stride constraints then in the end they will have
2355
 * the same (zero) constant term.
2356
 */
2357
static isl_stat harmonize_stride_divs(struct isl_coalesce_info *info1,
2358
  struct isl_coalesce_info *info2)
2359
4.14k
{
2360
4.14k
  int i, n;
2361
4.14k
2362
4.14k
  n = isl_basic_map_dim(info1->bmap, isl_dim_div);
2363
8.37k
  for (i = 0; i < n; 
++i4.23k
) {
2364
4.23k
    isl_bool known, harmonize;
2365
4.23k
2366
4.23k
    known = isl_basic_map_div_is_known(info1->bmap, i);
2367
4.23k
    if (known >= 0 && known)
2368
4.22k
      known = isl_basic_map_div_is_known(info2->bmap, i);
2369
4.23k
    if (known < 0)
2370
0
      return isl_stat_error;
2371
4.23k
    if (!known)
2372
9
      continue;
2373
4.22k
    harmonize = isl_basic_map_equal_div_expr_except_constant(
2374
4.22k
              info1->bmap, i, info2->bmap, i);
2375
4.22k
    if (harmonize < 0)
2376
0
      return isl_stat_error;
2377
4.22k
    if (!harmonize)
2378
4.09k
      continue;
2379
127
    if (normalize_stride_div(info1, i) < 0)
2380
0
      return isl_stat_error;
2381
127
    if (normalize_stride_div(info2, i) < 0)
2382
0
      return isl_stat_error;
2383
4.23k
  }
2384
4.14k
2385
4.14k
  return isl_stat_ok;
2386
4.14k
}
2387
2388
/* If "shift" is an integer constant, then shift the integer division
2389
 * at position "div" of the basic map represented by "info" by "shift".
2390
 * If "shift" is not an integer constant, then do nothing.
2391
 * If "shift" is equal to zero, then no shift needs to be performed either.
2392
 *
2393
 * That is, if the integer division has the form
2394
 *
2395
 *  floor(f(x)/d)
2396
 *
2397
 * then replace it by
2398
 *
2399
 *  floor((f(x) + shift * d)/d) - shift
2400
 */
2401
static isl_stat shift_if_cst_int(struct isl_coalesce_info *info, int div,
2402
  __isl_keep isl_aff *shift)
2403
16
{
2404
16
  isl_bool cst;
2405
16
  isl_stat r;
2406
16
  isl_int d;
2407
16
  isl_val *c;
2408
16
2409
16
  cst = isl_aff_is_cst(shift);
2410
16
  if (cst < 0 || !cst)
2411
12
    return cst < 0 ? 
isl_stat_error0
: isl_stat_ok;
2412
4
2413
4
  c = isl_aff_get_constant_val(shift);
2414
4
  cst = isl_val_is_int(c);
2415
4
  if (cst >= 0 && cst)
2416
3
    cst = isl_bool_not(isl_val_is_zero(c));
2417
4
  if (cst < 0 || !cst) {
2418
1
    isl_val_free(c);
2419
1
    return cst < 0 ? 
isl_stat_error0
: isl_stat_ok;
2420
1
  }
2421
3
2422
3
  isl_int_init(d);
2423
3
  r = isl_val_get_num_isl_int(c, &d);
2424
3
  if (r >= 0)
2425
3
    r = shift_div(info, div, d);
2426
3
  isl_int_clear(d);
2427
16
2428
16
  isl_val_free(c);
2429
16
2430
16
  return r;
2431
16
}
2432
2433
/* Check if some of the divs in the basic map represented by "info1"
2434
 * are shifts of the corresponding divs in the basic map represented
2435
 * by "info2", taking into account the equality constraints "eq1" of "info1"
2436
 * and "eq2" of "info2".  If so, align them with those of "info2".
2437
 * "info1" and "info2" are assumed to have the same number
2438
 * of integer divisions.
2439
 *
2440
 * An integer division is considered to be a shift of another integer
2441
 * division if, after simplification with respect to the equality
2442
 * constraints of the other basic map, one is equal to the other
2443
 * plus a constant.
2444
 *
2445
 * In particular, for each pair of integer divisions, if both are known,
2446
 * have the same denominator and are not already equal to each other,
2447
 * simplify each with respect to the equality constraints
2448
 * of the other basic map.  If the difference is an integer constant,
2449
 * then move this difference outside.
2450
 * That is, if, after simplification, one integer division is of the form
2451
 *
2452
 *  floor((f(x) + c_1)/d)
2453
 *
2454
 * while the other is of the form
2455
 *
2456
 *  floor((f(x) + c_2)/d)
2457
 *
2458
 * and n = (c_2 - c_1)/d is an integer, then replace the first
2459
 * integer division by
2460
 *
2461
 *  floor((f_1(x) + c_1 + n * d)/d) - n,
2462
 *
2463
 * where floor((f_1(x) + c_1 + n * d)/d) = floor((f2(x) + c_2)/d)
2464
 * after simplification with respect to the equality constraints.
2465
 */
2466
static isl_stat harmonize_divs_with_hulls(struct isl_coalesce_info *info1,
2467
  struct isl_coalesce_info *info2, __isl_keep isl_basic_set *eq1,
2468
  __isl_keep isl_basic_set *eq2)
2469
189
{
2470
189
  int i;
2471
189
  int total;
2472
189
  isl_local_space *ls1, *ls2;
2473
189
2474
189
  total = isl_basic_map_total_dim(info1->bmap);
2475
189
  ls1 = isl_local_space_wrap(isl_basic_map_get_local_space(info1->bmap));
2476
189
  ls2 = isl_local_space_wrap(isl_basic_map_get_local_space(info2->bmap));
2477
402
  for (i = 0; i < info1->bmap->n_div; 
++i213
) {
2478
213
    isl_stat r;
2479
213
    isl_aff *div1, *div2;
2480
213
2481
213
    if (!isl_local_space_div_is_known(ls1, i) ||
2482
213
        
!isl_local_space_div_is_known(ls2, i)204
)
2483
9
      continue;
2484
204
    if (isl_int_ne(info1->bmap->div[i][0], info2->bmap->div[i][0]))
2485
204
      
continue44
;
2486
160
    if (isl_seq_eq(info1->bmap->div[i] + 1,
2487
160
        info2->bmap->div[i] + 1, 1 + total))
2488
144
      continue;
2489
16
    div1 = isl_local_space_get_div(ls1, i);
2490
16
    div2 = isl_local_space_get_div(ls2, i);
2491
16
    div1 = isl_aff_substitute_equalities(div1,
2492
16
                isl_basic_set_copy(eq2));
2493
16
    div2 = isl_aff_substitute_equalities(div2,
2494
16
                isl_basic_set_copy(eq1));
2495
16
    div2 = isl_aff_sub(div2, div1);
2496
16
    r = shift_if_cst_int(info1, i, div2);
2497
16
    isl_aff_free(div2);
2498
16
    if (r < 0)
2499
0
      break;
2500
213
  }
2501
189
  isl_local_space_free(ls1);
2502
189
  isl_local_space_free(ls2);
2503
189
2504
189
  if (i < info1->bmap->n_div)
2505
0
    return isl_stat_error;
2506
189
  return isl_stat_ok;
2507
189
}
2508
2509
/* Check if some of the divs in the basic map represented by "info1"
2510
 * are shifts of the corresponding divs in the basic map represented
2511
 * by "info2".  If so, align them with those of "info2".
2512
 * Only do this if "info1" and "info2" have the same number
2513
 * of integer divisions.
2514
 *
2515
 * An integer division is considered to be a shift of another integer
2516
 * division if, after simplification with respect to the equality
2517
 * constraints of the other basic map, one is equal to the other
2518
 * plus a constant.
2519
 *
2520
 * First check if pairs of integer divisions are equal to each other
2521
 * despite the fact that they differ by a rational constant.
2522
 * If so, try and arrange for them to have the same constant term.
2523
 *
2524
 * Then, extract the equality constraints and continue with
2525
 * harmonize_divs_with_hulls.
2526
 *
2527
 * If the equality constraints of both basic maps are the same,
2528
 * then there is no need to perform any shifting since
2529
 * the coefficients of the integer divisions should have been
2530
 * reduced in the same way.
2531
 */
2532
static isl_stat harmonize_divs(struct isl_coalesce_info *info1,
2533
  struct isl_coalesce_info *info2)
2534
67.1k
{
2535
67.1k
  isl_bool equal;
2536
67.1k
  isl_basic_map *bmap1, *bmap2;
2537
67.1k
  isl_basic_set *eq1, *eq2;
2538
67.1k
  isl_stat r;
2539
67.1k
2540
67.1k
  if (!info1->bmap || !info2->bmap)
2541
0
    return isl_stat_error;
2542
67.1k
2543
67.1k
  if (info1->bmap->n_div != info2->bmap->n_div)
2544
7.35k
    return isl_stat_ok;
2545
59.7k
  if (info1->bmap->n_div == 0)
2546
55.6k
    return isl_stat_ok;
2547
4.14k
2548
4.14k
  if (harmonize_stride_divs(info1, info2) < 0)
2549
0
    return isl_stat_error;
2550
4.14k
2551
4.14k
  bmap1 = isl_basic_map_copy(info1->bmap);
2552
4.14k
  bmap2 = isl_basic_map_copy(info2->bmap);
2553
4.14k
  eq1 = isl_basic_map_wrap(isl_basic_map_plain_affine_hull(bmap1));
2554
4.14k
  eq2 = isl_basic_map_wrap(isl_basic_map_plain_affine_hull(bmap2));
2555
4.14k
  equal = isl_basic_set_plain_is_equal(eq1, eq2);
2556
4.14k
  if (equal < 0)
2557
0
    r = isl_stat_error;
2558
4.14k
  else if (equal)
2559
3.95k
    r = isl_stat_ok;
2560
189
  else
2561
189
    r = harmonize_divs_with_hulls(info1, info2, eq1, eq2);
2562
67.1k
  isl_basic_set_free(eq1);
2563
67.1k
  isl_basic_set_free(eq2);
2564
67.1k
2565
67.1k
  return r;
2566
67.1k
}
2567
2568
/* Do the two basic maps live in the same local space, i.e.,
2569
 * do they have the same (known) divs?
2570
 * If either basic map has any unknown divs, then we can only assume
2571
 * that they do not live in the same local space.
2572
 */
2573
static isl_bool same_divs(__isl_keep isl_basic_map *bmap1,
2574
  __isl_keep isl_basic_map *bmap2)
2575
67.1k
{
2576
67.1k
  int i;
2577
67.1k
  isl_bool known;
2578
67.1k
  int total;
2579
67.1k
2580
67.1k
  if (!bmap1 || !bmap2)
2581
0
    return isl_bool_error;
2582
67.1k
  if (bmap1->n_div != bmap2->n_div)
2583
7.35k
    return isl_bool_false;
2584
59.7k
2585
59.7k
  if (bmap1->n_div == 0)
2586
55.6k
    return isl_bool_true;
2587
4.14k
2588
4.14k
  known = isl_basic_map_divs_known(bmap1);
2589
4.14k
  if (known < 0 || !known)
2590
9
    return known;
2591
4.13k
  known = isl_basic_map_divs_known(bmap2);
2592
4.13k
  if (known < 0 || !known)
2593
0
    return known;
2594
4.13k
2595
4.13k
  total = isl_basic_map_total_dim(bmap1);
2596
8.22k
  for (i = 0; i < bmap1->n_div; 
++i4.08k
)
2597
4.18k
    if (!isl_seq_eq(bmap1->div[i], bmap2->div[i], 2 + total))
2598
100
      return 0;
2599
4.13k
2600
4.13k
  
return 14.03k
;
2601
67.1k
}
2602
2603
/* Assuming that "tab" contains the equality constraints and
2604
 * the initial inequality constraints of "bmap", copy the remaining
2605
 * inequality constraints of "bmap" to "Tab".
2606
 */
2607
static isl_stat copy_ineq(struct isl_tab *tab, __isl_keep isl_basic_map *bmap)
2608
2.92k
{
2609
2.92k
  int i, n_ineq;
2610
2.92k
2611
2.92k
  if (!bmap)
2612
0
    return isl_stat_error;
2613
2.92k
2614
2.92k
  n_ineq = tab->n_con - tab->n_eq;
2615
8.85k
  for (i = n_ineq; i < bmap->n_ineq; 
++i5.93k
)
2616
5.93k
    if (isl_tab_add_ineq(tab, bmap->ineq[i]) < 0)
2617
0
      return isl_stat_error;
2618
2.92k
2619
2.92k
  return isl_stat_ok;
2620
2.92k
}
2621
2622
/* Description of an integer division that is added
2623
 * during an expansion.
2624
 * "pos" is the position of the corresponding variable.
2625
 * "cst" indicates whether this integer division has a fixed value.
2626
 * "val" contains the fixed value, if the value is fixed.
2627
 */
2628
struct isl_expanded {
2629
  int pos;
2630
  isl_bool cst;
2631
  isl_int val;
2632
};
2633
2634
/* For each of the "n" integer division variables "expanded",
2635
 * if the variable has a fixed value, then add two inequality
2636
 * constraints expressing the fixed value.
2637
 * Otherwise, add the corresponding div constraints.
2638
 * The caller is responsible for removing the div constraints
2639
 * that it added for all these "n" integer divisions.
2640
 *
2641
 * The div constraints and the pair of inequality constraints
2642
 * forcing the fixed value cannot both be added for a given variable
2643
 * as the combination may render some of the original constraints redundant.
2644
 * These would then be ignored during the coalescing detection,
2645
 * while they could remain in the fused result.
2646
 *
2647
 * The two added inequality constraints are
2648
 *
2649
 *  -a + v >= 0
2650
 *  a - v >= 0
2651
 *
2652
 * with "a" the variable and "v" its fixed value.
2653
 * The facet corresponding to one of these two constraints is selected
2654
 * in the tableau to ensure that the pair of inequality constraints
2655
 * is treated as an equality constraint.
2656
 *
2657
 * The information in info->ineq is thrown away because it was
2658
 * computed in terms of div constraints, while some of those
2659
 * have now been replaced by these pairs of inequality constraints.
2660
 */
2661
static isl_stat fix_constant_divs(struct isl_coalesce_info *info,
2662
  int n, struct isl_expanded *expanded)
2663
73
{
2664
73
  unsigned o_div;
2665
73
  int i;
2666
73
  isl_vec *ineq;
2667
73
2668
73
  o_div = isl_basic_map_offset(info->bmap, isl_dim_div) - 1;
2669
73
  ineq = isl_vec_alloc(isl_tab_get_ctx(info->tab), 1 + info->tab->n_var);
2670
73
  if (!ineq)
2671
0
    return isl_stat_error;
2672
73
  isl_seq_clr(ineq->el + 1, info->tab->n_var);
2673
73
2674
183
  for (i = 0; i < n; 
++i110
) {
2675
110
    if (!expanded[i].cst) {
2676
8
      info->bmap = isl_basic_map_extend_constraints(
2677
8
            info->bmap, 0, 2);
2678
8
      if (isl_basic_map_add_div_constraints(info->bmap,
2679
8
            expanded[i].pos - o_div) < 0)
2680
0
        break;
2681
102
    } else {
2682
102
      isl_int_set_si(ineq->el[1 + expanded[i].pos], -1);
2683
102
      isl_int_set(ineq->el[0], expanded[i].val);
2684
102
      info->bmap = isl_basic_map_add_ineq(info->bmap,
2685
102
                ineq->el);
2686
102
      isl_int_set_si(ineq->el[1 + expanded[i].pos], 1);
2687
102
      isl_int_neg(ineq->el[0], expanded[i].val);
2688
102
      info->bmap = isl_basic_map_add_ineq(info->bmap,
2689
102
                ineq->el);
2690
102
      isl_int_set_si(ineq->el[1 + expanded[i].pos], 0);
2691
102
    }
2692
110
    if (copy_ineq(info->tab, info->bmap) < 0)
2693
0
      break;
2694
110
    if (expanded[i].cst &&
2695
110
        
isl_tab_select_facet(info->tab, info->tab->n_con - 1) < 0102
)
2696
0
      break;
2697
110
  }
2698
73
2699
73
  isl_vec_free(ineq);
2700
73
2701
73
  clear_status(info);
2702
73
  init_status(info);
2703
73
2704
73
  return i < n ? 
isl_stat_error0
: isl_stat_ok;
2705
73
}
2706
2707
/* Insert the "n" integer division variables "expanded"
2708
 * into info->tab and info->bmap and
2709
 * update info->ineq with respect to the redundant constraints
2710
 * in the resulting tableau.
2711
 * "bmap" contains the result of this insertion in info->bmap,
2712
 * while info->bmap is the original version
2713
 * of "bmap", i.e., the one that corresponds to the current
2714
 * state of info->tab.  The number of constraints in info->bmap
2715
 * is assumed to be the same as the number of constraints
2716
 * in info->tab.  This is required to be able to detect
2717
 * the extra constraints in "bmap".
2718
 *
2719
 * In particular, introduce extra variables corresponding
2720
 * to the extra integer divisions and add the div constraints
2721
 * that were added to "bmap" after info->tab was created
2722
 * from info->bmap.
2723
 * Furthermore, check if these extra integer divisions happen
2724
 * to attain a fixed integer value in info->tab.
2725
 * If so, replace the corresponding div constraints by pairs
2726
 * of inequality constraints that fix these
2727
 * integer divisions to their single integer values.
2728
 * Replace info->bmap by "bmap" to match the changes to info->tab.
2729
 * info->ineq was computed without a tableau and therefore
2730
 * does not take into account the redundant constraints
2731
 * in the tableau.  Mark them here.
2732
 * There is no need to check the newly added div constraints
2733
 * since they cannot be redundant.
2734
 * The redundancy check is not performed when constants have been discovered
2735
 * since info->ineq is completely thrown away in this case.
2736
 */
2737
static isl_stat tab_insert_divs(struct isl_coalesce_info *info,
2738
  int n, struct isl_expanded *expanded, __isl_take isl_basic_map *bmap)
2739
2.81k
{
2740
2.81k
  int i, n_ineq;
2741
2.81k
  unsigned n_eq;
2742
2.81k
  struct isl_tab_undo *snap;
2743
2.81k
  int any;
2744
2.81k
2745
2.81k
  if (!bmap)
2746
0
    return isl_stat_error;
2747
2.81k
  if (info->bmap->n_eq + info->bmap->n_ineq != info->tab->n_con)
2748
2.81k
    
isl_die0
(isl_basic_map_get_ctx(bmap), isl_error_internal,
2749
2.81k
      "original tableau does not correspond "
2750
2.81k
      "to original basic map", goto error);
2751
2.81k
2752
2.81k
  if (isl_tab_extend_vars(info->tab, n) < 0)
2753
0
    goto error;
2754
2.81k
  if (isl_tab_extend_cons(info->tab, 2 * n) < 0)
2755
0
    goto error;
2756
2.81k
2757
5.66k
  
for (i = 0; 2.81k
i < n;
++i2.85k
) {
2758
2.85k
    if (isl_tab_insert_var(info->tab, expanded[i].pos) < 0)
2759
0
      goto error;
2760
2.85k
  }
2761
2.81k
2762
2.81k
  snap = isl_tab_snap(info->tab);
2763
2.81k
2764
2.81k
  n_ineq = info->tab->n_con - info->tab->n_eq;
2765
2.81k
  if (copy_ineq(info->tab, bmap) < 0)
2766
0
    goto error;
2767
2.81k
2768
2.81k
  isl_basic_map_free(info->bmap);
2769
2.81k
  info->bmap = bmap;
2770
2.81k
2771
2.81k
  any = 0;
2772
5.66k
  for (i = 0; i < n; 
++i2.85k
) {
2773
2.85k
    expanded[i].cst = isl_tab_is_constant(info->tab,
2774
2.85k
              expanded[i].pos, &expanded[i].val);
2775
2.85k
    if (expanded[i].cst < 0)
2776
0
      return isl_stat_error;
2777
2.85k
    if (expanded[i].cst)
2778
102
      any = 1;
2779
2.85k
  }
2780
2.81k
2781
2.81k
  if (any) {
2782
73
    if (isl_tab_rollback(info->tab, snap) < 0)
2783
0
      return isl_stat_error;
2784
73
    info->bmap = isl_basic_map_cow(info->bmap);
2785
73
    if (isl_basic_map_free_inequality(info->bmap, 2 * n) < 0)
2786
0
      return isl_stat_error;
2787
73
2788
73
    return fix_constant_divs(info, n, expanded);
2789
73
  }
2790
2.73k
2791
2.73k
  n_eq = info->bmap->n_eq;
2792
5.63k
  for (i = 0; i < n_ineq; 
++i2.89k
) {
2793
2.89k
    if (isl_tab_is_redundant(info->tab, n_eq + i))
2794
10
      info->ineq[i] = STATUS_REDUNDANT;
2795
2.89k
  }
2796
2.73k
2797
2.73k
  return isl_stat_ok;
2798
0
error:
2799
0
  isl_basic_map_free(bmap);
2800
0
  return isl_stat_error;
2801
2.81k
}
2802
2803
/* Expand info->tab and info->bmap in the same way "bmap" was expanded
2804
 * in isl_basic_map_expand_divs using the expansion "exp" and
2805
 * update info->ineq with respect to the redundant constraints
2806
 * in the resulting tableau. info->bmap is the original version
2807
 * of "bmap", i.e., the one that corresponds to the current
2808
 * state of info->tab.  The number of constraints in info->bmap
2809
 * is assumed to be the same as the number of constraints
2810
 * in info->tab.  This is required to be able to detect
2811
 * the extra constraints in "bmap".
2812
 *
2813
 * Extract the positions where extra local variables are introduced
2814
 * from "exp" and call tab_insert_divs.
2815
 */
2816
static isl_stat expand_tab(struct isl_coalesce_info *info, int *exp,
2817
  __isl_take isl_basic_map *bmap)
2818
2.81k
{
2819
2.81k
  isl_ctx *ctx;
2820
2.81k
  struct isl_expanded *expanded;
2821
2.81k
  int i, j, k, n;
2822
2.81k
  int extra_var;
2823
2.81k
  unsigned total, pos, n_div;
2824
2.81k
  isl_stat r;
2825
2.81k
2826
2.81k
  total = isl_basic_map_dim(bmap, isl_dim_all);
2827
2.81k
  n_div = isl_basic_map_dim(bmap, isl_dim_div);
2828
2.81k
  pos = total - n_div;
2829
2.81k
  extra_var = total - info->tab->n_var;
2830
2.81k
  n = n_div - extra_var;
2831
2.81k
2832
2.81k
  ctx = isl_basic_map_get_ctx(bmap);
2833
2.81k
  expanded = isl_calloc_array(ctx, struct isl_expanded, extra_var);
2834
2.81k
  if (extra_var && !expanded)
2835
0
    goto error;
2836
2.81k
2837
2.81k
  i = 0;
2838
2.81k
  k = 0;
2839
5.71k
  for (j = 0; j < n_div; 
++j2.90k
) {
2840
2.90k
    if (i < n && 
exp[i] == j89
) {
2841
44
      ++i;
2842
44
      continue;
2843
44
    }
2844
2.85k
    expanded[k++].pos = pos + j;
2845
2.85k
  }
2846
2.81k
2847
5.66k
  for (k = 0; k < extra_var; 
++k2.85k
)
2848
2.85k
    isl_int_init(expanded[k].val);
2849
2.81k
2850
2.81k
  r = tab_insert_divs(info, extra_var, expanded, bmap);
2851
2.81k
2852
5.66k
  for (k = 0; k < extra_var; 
++k2.85k
)
2853
2.85k
    isl_int_clear(expanded[k].val);
2854
2.81k
  free(expanded);
2855
2.81k
2856
2.81k
  return r;
2857
0
error:
2858
0
  isl_basic_map_free(bmap);
2859
0
  return isl_stat_error;
2860
2.81k
}
2861
2862
/* Check if the union of the basic maps represented by info[i] and info[j]
2863
 * can be represented by a single basic map,
2864
 * after expanding the divs of info[i] to match those of info[j].
2865
 * If so, replace the pair by the single basic map and return
2866
 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2867
 * Otherwise, return isl_change_none.
2868
 *
2869
 * The caller has already checked for info[j] being a subset of info[i].
2870
 * If some of the divs of info[j] are unknown, then the expanded info[i]
2871
 * will not have the corresponding div constraints.  The other patterns
2872
 * therefore cannot apply.  Skip the computation in this case.
2873
 *
2874
 * The expansion is performed using the divs "div" and expansion "exp"
2875
 * computed by the caller.
2876
 * info[i].bmap has already been expanded and the result is passed in
2877
 * as "bmap".
2878
 * The "eq" and "ineq" fields of info[i] reflect the status of
2879
 * the constraints of the expanded "bmap" with respect to info[j].tab.
2880
 * However, inequality constraints that are redundant in info[i].tab
2881
 * have not yet been marked as such because no tableau was available.
2882
 *
2883
 * Replace info[i].bmap by "bmap" and expand info[i].tab as well,
2884
 * updating info[i].ineq with respect to the redundant constraints.
2885
 * Then try and coalesce the expanded info[i] with info[j],
2886
 * reusing the information in info[i].eq and info[i].ineq.
2887
 * If this does not result in any coalescing or if it results in info[j]
2888
 * getting dropped (which should not happen in practice, since the case
2889
 * of info[j] being a subset of info[i] has already been checked by
2890
 * the caller), then revert info[i] to its original state.
2891
 */
2892
static enum isl_change coalesce_expand_tab_divs(__isl_take isl_basic_map *bmap,
2893
  int i, int j, struct isl_coalesce_info *info, __isl_keep isl_mat *div,
2894
  int *exp)
2895
2.82k
{
2896
2.82k
  isl_bool known;
2897
2.82k
  isl_basic_map *bmap_i;
2898
2.82k
  struct isl_tab_undo *snap;
2899
2.82k
  enum isl_change change = isl_change_none;
2900
2.82k
2901
2.82k
  known = isl_basic_map_divs_known(info[j].bmap);
2902
2.82k
  if (known < 0 || !known) {
2903
11
    clear_status(&info[i]);
2904
11
    isl_basic_map_free(bmap);
2905
11
    return known < 0 ? 
isl_change_error0
: isl_change_none;
2906
11
  }
2907
2.81k
2908
2.81k
  bmap_i = isl_basic_map_copy(info[i].bmap);
2909
2.81k
  snap = isl_tab_snap(info[i].tab);
2910
2.81k
  if (expand_tab(&info[i], exp, bmap) < 0)
2911
0
    change = isl_change_error;
2912
2.81k
2913
2.81k
  init_status(&info[j]);
2914
2.81k
  if (change == isl_change_none)
2915
2.81k
    change = coalesce_local_pair_reuse(i, j, info);
2916
0
  else
2917
0
    clear_status(&info[i]);
2918
2.81k
  if (change != isl_change_none && 
change != isl_change_drop_second675
) {
2919
675
    isl_basic_map_free(bmap_i);
2920
2.13k
  } else {
2921
2.13k
    isl_basic_map_free(info[i].bmap);
2922
2.13k
    info[i].bmap = bmap_i;
2923
2.13k
2924
2.13k
    if (isl_tab_rollback(info[i].tab, snap) < 0)
2925
0
      change = isl_change_error;
2926
2.13k
  }
2927
2.82k
2928
2.82k
  return change;
2929
2.82k
}
2930
2931
/* Check if the union of "bmap" and the basic map represented by info[j]
2932
 * can be represented by a single basic map,
2933
 * after expanding the divs of "bmap" to match those of info[j].
2934
 * If so, replace the pair by the single basic map and return
2935
 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2936
 * Otherwise, return isl_change_none.
2937
 *
2938
 * In particular, check if the expanded "bmap" contains the basic map
2939
 * represented by the tableau info[j].tab.
2940
 * The expansion is performed using the divs "div" and expansion "exp"
2941
 * computed by the caller.
2942
 * Then we check if all constraints of the expanded "bmap" are valid for
2943
 * info[j].tab.
2944
 *
2945
 * If "i" is not equal to -1, then "bmap" is equal to info[i].bmap.
2946
 * In this case, the positions of the constraints of info[i].bmap
2947
 * with respect to the basic map represented by info[j] are stored
2948
 * in info[i].
2949
 *
2950
 * If the expanded "bmap" does not contain the basic map
2951
 * represented by the tableau info[j].tab and if "i" is not -1,
2952
 * i.e., if the original "bmap" is info[i].bmap, then expand info[i].tab
2953
 * as well and check if that results in coalescing.
2954
 */
2955
static enum isl_change coalesce_with_expanded_divs(
2956
  __isl_keep isl_basic_map *bmap, int i, int j,
2957
  struct isl_coalesce_info *info, __isl_keep isl_mat *div, int *exp)
2958
7.41k
{
2959
7.41k
  enum isl_change change = isl_change_none;
2960
7.41k
  struct isl_coalesce_info info_local, *info_i;
2961
7.41k
2962
7.41k
  info_i = i >= 0 ? 
&info[i]7.34k
:
&info_local72
;
2963
7.41k
  init_status(info_i);
2964
7.41k
  bmap = isl_basic_map_copy(bmap);
2965
7.41k
  bmap = isl_basic_map_expand_divs(bmap, isl_mat_copy(div), exp);
2966
7.41k
  bmap = isl_basic_map_mark_final(bmap);
2967
7.41k
2968
7.41k
  if (!bmap)
2969
0
    goto error;
2970
7.41k
2971
7.41k
  info_i->eq = eq_status_in(bmap, info[j].tab);
2972
7.41k
  if (bmap->n_eq && 
!info_i->eq271
)
2973
0
    goto error;
2974
7.41k
  if (any(info_i->eq, 2 * bmap->n_eq, STATUS_ERROR))
2975
0
    goto error;
2976
7.41k
  if (any(info_i->eq, 2 * bmap->n_eq, STATUS_SEPARATE))
2977
33
    goto done;
2978
7.38k
2979
7.38k
  info_i->ineq = ineq_status_in(bmap, NULL, info[j].tab);
2980
7.38k
  if (bmap->n_ineq && 
!info_i->ineq7.33k
)
2981
0
    goto error;
2982
7.38k
  if (any(info_i->ineq, bmap->n_ineq, STATUS_ERROR))
2983
0
    goto error;
2984
7.38k
  if (any(info_i->ineq, bmap->n_ineq, STATUS_SEPARATE))
2985
2.11k
    goto done;
2986
5.26k
2987
5.26k
  if (all(info_i->eq, 2 * bmap->n_eq, STATUS_VALID) &&
2988
5.26k
      
all(info_i->ineq, bmap->n_ineq, 5.10k
STATUS_VALID5.10k
)) {
2989
2.38k
    drop(&info[j]);
2990
2.38k
    change = isl_change_drop_second;
2991
2.38k
  }
2992
5.26k
2993
5.26k
  if (change == isl_change_none && 
i != -12.88k
)
2994
2.82k
    return coalesce_expand_tab_divs(bmap, i, j, info, div, exp);
2995
4.59k
2996
4.59k
done:
2997
4.59k
  isl_basic_map_free(bmap);
2998
4.59k
  clear_status(info_i);
2999
4.59k
  return change;
3000
0
error:
3001
0
  isl_basic_map_free(bmap);
3002
0
  clear_status(info_i);
3003
0
  return isl_change_error;
3004
7.41k
}
3005
3006
/* Check if the union of "bmap_i" and the basic map represented by info[j]
3007
 * can be represented by a single basic map,
3008
 * after aligning the divs of "bmap_i" to match those of info[j].
3009
 * If so, replace the pair by the single basic map and return
3010
 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
3011
 * Otherwise, return isl_change_none.
3012
 *
3013
 * In particular, check if "bmap_i" contains the basic map represented by
3014
 * info[j] after aligning the divs of "bmap_i" to those of info[j].
3015
 * Note that this can only succeed if the number of divs of "bmap_i"
3016
 * is smaller than (or equal to) the number of divs of info[j].
3017
 *
3018
 * We first check if the divs of "bmap_i" are all known and form a subset
3019
 * of those of info[j].bmap.  If so, we pass control over to
3020
 * coalesce_with_expanded_divs.
3021
 *
3022
 * If "i" is not equal to -1, then "bmap" is equal to info[i].bmap.
3023
 */
3024
static enum isl_change coalesce_after_aligning_divs(
3025
  __isl_keep isl_basic_map *bmap_i, int i, int j,
3026
  struct isl_coalesce_info *info)
3027
7.50k
{
3028
7.50k
  int known;
3029
7.50k
  isl_mat *div_i, *div_j, *div;
3030
7.50k
  int *exp1 = NULL;
3031
7.50k
  int *exp2 = NULL;
3032
7.50k
  isl_ctx *ctx;
3033
7.50k
  enum isl_change change;
3034
7.50k
3035
7.50k
  known = isl_basic_map_divs_known(bmap_i);
3036
7.50k
  if (known < 0 || !known)
3037
0
    return known;
3038
7.50k
3039
7.50k
  ctx = isl_basic_map_get_ctx(bmap_i);
3040
7.50k
3041
7.50k
  div_i = isl_basic_map_get_divs(bmap_i);
3042
7.50k
  div_j = isl_basic_map_get_divs(info[j].bmap);
3043
7.50k
3044
7.50k
  if (!div_i || !div_j)
3045
0
    goto error;
3046
7.50k
3047
7.50k
  exp1 = isl_alloc_array(ctx, int, div_i->n_row);
3048
7.50k
  exp2 = isl_alloc_array(ctx, int, div_j->n_row);
3049
7.50k
  if ((div_i->n_row && 
!exp1172
) || (div_j->n_row &&
!exp27.46k
))
3050
0
    goto error;
3051
7.50k
3052
7.50k
  div = isl_merge_divs(div_i, div_j, exp1, exp2);
3053
7.50k
  if (!div)
3054
0
    goto error;
3055
7.50k
3056
7.50k
  if (div->n_row == div_j->n_row)
3057
7.41k
    change = coalesce_with_expanded_divs(bmap_i,
3058
7.41k
              i, j, info, div, exp1);
3059
94
  else
3060
94
    change = isl_change_none;
3061
7.50k
3062
7.50k
  isl_mat_free(div);
3063
7.50k
3064
7.50k
  isl_mat_free(div_i);
3065
7.50k
  isl_mat_free(div_j);
3066
7.50k
3067
7.50k
  free(exp2);
3068
7.50k
  free(exp1);
3069
7.50k
3070
7.50k
  return change;
3071
0
error:
3072
0
  isl_mat_free(div_i);
3073
0
  isl_mat_free(div_j);
3074
0
  free(exp1);
3075
0
  free(exp2);
3076
0
  return isl_change_error;
3077
7.50k
}
3078
3079
/* Check if basic map "j" is a subset of basic map "i" after
3080
 * exploiting the extra equalities of "j" to simplify the divs of "i".
3081
 * If so, remove basic map "j" and return isl_change_drop_second.
3082
 *
3083
 * If "j" does not have any equalities or if they are the same
3084
 * as those of "i", then we cannot exploit them to simplify the divs.
3085
 * Similarly, if there are no divs in "i", then they cannot be simplified.
3086
 * If, on the other hand, the affine hulls of "i" and "j" do not intersect,
3087
 * then "j" cannot be a subset of "i".
3088
 *
3089
 * Otherwise, we intersect "i" with the affine hull of "j" and then
3090
 * check if "j" is a subset of the result after aligning the divs.
3091
 * If so, then "j" is definitely a subset of "i" and can be removed.
3092
 * Note that if after intersection with the affine hull of "j".
3093
 * "i" still has more divs than "j", then there is no way we can
3094
 * align the divs of "i" to those of "j".
3095
 */
3096
static enum isl_change coalesce_subset_with_equalities(int i, int j,
3097
  struct isl_coalesce_info *info)
3098
8.81k
{
3099
8.81k
  isl_basic_map *hull_i, *hull_j, *bmap_i;
3100
8.81k
  int equal, empty;
3101
8.81k
  enum isl_change change;
3102
8.81k
3103
8.81k
  if (info[j].bmap->n_eq == 0)
3104
8.34k
    return isl_change_none;
3105
472
  if (info[i].bmap->n_div == 0)
3106
121
    return isl_change_none;
3107
351
3108
351
  hull_i = isl_basic_map_copy(info[i].bmap);
3109
351
  hull_i = isl_basic_map_plain_affine_hull(hull_i);
3110
351
  hull_j = isl_basic_map_copy(info[j].bmap);
3111
351
  hull_j = isl_basic_map_plain_affine_hull(hull_j);
3112
351
3113
351
  hull_j = isl_basic_map_intersect(hull_j, isl_basic_map_copy(hull_i));
3114
351
  equal = isl_basic_map_plain_is_equal(hull_i, hull_j);
3115
351
  empty = isl_basic_map_plain_is_empty(hull_j);
3116
351
  isl_basic_map_free(hull_i);
3117
351
3118
351
  if (equal < 0 || equal || 
empty < 0268
||
empty268
) {
3119
147
    isl_basic_map_free(hull_j);
3120
147
    if (equal < 0 || empty < 0)
3121
0
      return isl_change_error;
3122
147
    return isl_change_none;
3123
147
  }
3124
204
3125
204
  bmap_i = isl_basic_map_copy(info[i].bmap);
3126
204
  bmap_i = isl_basic_map_intersect(bmap_i, hull_j);
3127
204
  if (!bmap_i)
3128
0
    return isl_change_error;
3129
204
3130
204
  if (bmap_i->n_div > info[j].bmap->n_div) {
3131
54
    isl_basic_map_free(bmap_i);
3132
54
    return isl_change_none;
3133
54
  }
3134
150
3135
150
  change = coalesce_after_aligning_divs(bmap_i, -1, j, info);
3136
150
3137
150
  isl_basic_map_free(bmap_i);
3138
150
3139
150
  return change;
3140
150
}
3141
3142
/* Check if the union of and the basic maps represented by info[i] and info[j]
3143
 * can be represented by a single basic map, by aligning or equating
3144
 * their integer divisions.
3145
 * If so, replace the pair by the single basic map and return
3146
 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
3147
 * Otherwise, return isl_change_none.
3148
 *
3149
 * Note that we only perform any test if the number of divs is different
3150
 * in the two basic maps.  In case the number of divs is the same,
3151
 * we have already established that the divs are different
3152
 * in the two basic maps.
3153
 * In particular, if the number of divs of basic map i is smaller than
3154
 * the number of divs of basic map j, then we check if j is a subset of i
3155
 * and vice versa.
3156
 */
3157
static enum isl_change coalesce_divs(int i, int j,
3158
  struct isl_coalesce_info *info)
3159
7.46k
{
3160
7.46k
  enum isl_change change = isl_change_none;
3161
7.46k
3162
7.46k
  if (info[i].bmap->n_div < info[j].bmap->n_div)
3163
6.91k
    change = coalesce_after_aligning_divs(info[i].bmap, i, j, info);
3164
7.46k
  if (change != isl_change_none)
3165
3.03k
    return change;
3166
4.42k
3167
4.42k
  if (info[j].bmap->n_div < info[i].bmap->n_div)
3168
441
    change = coalesce_after_aligning_divs(info[j].bmap, j, i, info);
3169
4.42k
  if (change != isl_change_none)
3170
21
    return invert_change(change);
3171
4.40k
3172
4.40k
  change = coalesce_subset_with_equalities(i, j, info);
3173
4.40k
  if (change != isl_change_none)
3174
3
    return change;
3175
4.40k
3176
4.40k
  change = coalesce_subset_with_equalities(j, i, info);
3177
4.40k
  if (change != isl_change_none)
3178
0
    return invert_change(change);
3179
4.40k
3180
4.40k
  return isl_change_none;
3181
4.40k
}
3182
3183
/* Does "bmap" involve any divs that themselves refer to divs?
3184
 */
3185
static isl_bool has_nested_div(__isl_keep isl_basic_map *bmap)
3186
8.70k
{
3187
8.70k
  int i;
3188
8.70k
  unsigned total;
3189
8.70k
  unsigned n_div;
3190
8.70k
3191
8.70k
  total = isl_basic_map_dim(bmap, isl_dim_all);
3192
8.70k
  n_div = isl_basic_map_dim(bmap, isl_dim_div);
3193
8.70k
  total -= n_div;
3194
8.70k
3195
13.4k
  for (i = 0; i < n_div; 
++i4.70k
)
3196
8.70k
    
if (4.70k
isl_seq_first_non_zero(bmap->div[i] + 2 + total,
3197
4.70k
              n_div) != -1)
3198
4
      return isl_bool_true;
3199
8.70k
3200
8.70k
  
return isl_bool_false8.70k
;
3201
8.70k
}
3202
3203
/* Return a list of affine expressions, one for each integer division
3204
 * in "bmap_i".  For each integer division that also appears in "bmap_j",
3205
 * the affine expression is set to NaN.  The number of NaNs in the list
3206
 * is equal to the number of integer divisions in "bmap_j".
3207
 * For the other integer divisions of "bmap_i", the corresponding
3208
 * element in the list is a purely affine expression equal to the integer
3209
 * division in "hull".
3210
 * If no such list can be constructed, then the number of elements
3211
 * in the returned list is smaller than the number of integer divisions
3212
 * in "bmap_i".
3213
 */
3214
static __isl_give isl_aff_list *set_up_substitutions(
3215
  __isl_keep isl_basic_map *bmap_i, __isl_keep isl_basic_map *bmap_j,
3216
  __isl_take isl_basic_map *hull)
3217
76
{
3218
76
  unsigned n_div_i, n_div_j, total;
3219
76
  isl_ctx *ctx;
3220
76
  isl_local_space *ls;
3221
76
  isl_basic_set *wrap_hull;
3222
76
  isl_aff *aff_nan;
3223
76
  isl_aff_list *list;
3224
76
  int i, j;
3225
76
3226
76
  if (!hull)
3227
0
    return NULL;
3228
76
3229
76
  ctx = isl_basic_map_get_ctx(hull);
3230
76
3231
76
  n_div_i = isl_basic_map_dim(bmap_i, isl_dim_div);
3232
76
  n_div_j = isl_basic_map_dim(bmap_j, isl_dim_div);
3233
76
  total = isl_basic_map_total_dim(bmap_i) - n_div_i;
3234
76
3235
76
  ls = isl_basic_map_get_local_space(bmap_i);
3236
76
  ls = isl_local_space_wrap(ls);
3237
76
  wrap_hull = isl_basic_map_wrap(hull);
3238
76
3239
76
  aff_nan = isl_aff_nan_on_domain(isl_local_space_copy(ls));
3240
76
  list = isl_aff_list_alloc(ctx, n_div_i);
3241
76
3242
76
  j = 0;
3243
134
  for (i = 0; i < n_div_i; 
++i58
) {
3244
98
    isl_aff *aff;
3245
98
3246
98
    if (j < n_div_j &&
3247
98
        isl_basic_map_equal_div_expr_part(bmap_i, i, bmap_j, j,
3248
27
                0, 2 + total)) {
3249
6
      ++j;
3250
6
      list = isl_aff_list_add(list, isl_aff_copy(aff_nan));
3251
6
      continue;
3252
6
    }
3253
92
    if (n_div_i - i <= n_div_j - j)
3254
0
      break;
3255
92
3256
92
    aff = isl_local_space_get_div(ls, i);
3257
92
    aff = isl_aff_substitute_equalities(aff,
3258
92
            isl_basic_set_copy(wrap_hull));
3259
92
    aff = isl_aff_floor(aff);
3260
92
    if (!aff)
3261
0
      goto error;
3262
92
    if (isl_aff_dim(aff, isl_dim_div) != 0) {
3263
40
      isl_aff_free(aff);
3264
40
      break;
3265
40
    }
3266
52
3267
52
    list = isl_aff_list_add(list, aff);
3268
52
  }
3269
76
3270
76
  isl_aff_free(aff_nan);
3271
76
  isl_local_space_free(ls);
3272
76
  isl_basic_set_free(wrap_hull);
3273
76
3274
76
  return list;
3275
0
error:
3276
0
  isl_aff_free(aff_nan);
3277
0
  isl_local_space_free(ls);
3278
0
  isl_basic_set_free(wrap_hull);
3279
0
  isl_aff_list_free(list);
3280
0
  return NULL;
3281
76
}
3282
3283
/* Add variables to info->bmap and info->tab corresponding to the elements
3284
 * in "list" that are not set to NaN.
3285
 * "extra_var" is the number of these elements.
3286
 * "dim" is the offset in the variables of "tab" where we should
3287
 * start considering the elements in "list".
3288
 * When this function returns, the total number of variables in "tab"
3289
 * is equal to "dim" plus the number of elements in "list".
3290
 *
3291
 * The newly added existentially quantified variables are not given
3292
 * an explicit representation because the corresponding div constraints
3293
 * do not appear in info->bmap.  These constraints are not added
3294
 * to info->bmap because for internal consistency, they would need to
3295
 * be added to info->tab as well, where they could combine with the equality
3296
 * that is added later to result in constraints that do not hold
3297
 * in the original input.
3298
 */
3299
static isl_stat add_sub_vars(struct isl_coalesce_info *info,
3300
  __isl_keep isl_aff_list *list, int dim, int extra_var)
3301
36
{
3302
36
  int i, j, n, d;
3303
36
  isl_space *space;
3304
36
3305
36
  space = isl_basic_map_get_space(info->bmap);
3306
36
  info->bmap = isl_basic_map_cow(info->bmap);
3307
36
  info->bmap = isl_basic_map_extend_space(info->bmap, space,
3308
36
            extra_var, 0, 0);
3309
36
  if (!info->bmap)
3310
0
    return isl_stat_error;
3311
36
  n = isl_aff_list_n_aff(list);
3312
89
  for (i = 0; i < n; 
++i53
) {
3313
53
    int is_nan;
3314
53
    isl_aff *aff;
3315
53
3316
53
    aff = isl_aff_list_get_aff(list, i);
3317
53
    is_nan = isl_aff_is_nan(aff);
3318
53
    isl_aff_free(aff);
3319
53
    if (is_nan < 0)
3320
0
      return isl_stat_error;
3321
53
    if (is_nan)
3322
5
      continue;
3323
48
3324
48
    if (isl_tab_insert_var(info->tab, dim + i) < 0)
3325
0
      return isl_stat_error;
3326
48
    d = isl_basic_map_alloc_div(info->bmap);
3327
48
    if (d < 0)
3328
0
      return isl_stat_error;
3329
48
    info->bmap = isl_basic_map_mark_div_unknown(info->bmap, d);
3330
48
    if (!info->bmap)
3331
0
      return isl_stat_error;
3332
62
    
for (j = d; 48
j > i;
--j14
)
3333
48
      
isl_basic_map_swap_div(info->bmap, j - 1, j)14
;
3334
53
  }
3335
36
3336
36
  return isl_stat_ok;
3337
36
}
3338
3339
/* For each element in "list" that is not set to NaN, fix the corresponding
3340
 * variable in "tab" to the purely affine expression defined by the element.
3341
 * "dim" is the offset in the variables of "tab" where we should
3342
 * start considering the elements in "list".
3343
 *
3344
 * This function assumes that a sufficient number of rows and
3345
 * elements in the constraint array are available in the tableau.
3346
 */
3347
static int add_sub_equalities(struct isl_tab *tab,
3348
  __isl_keep isl_aff_list *list, int dim)
3349
36
{
3350
36
  int i, n;
3351
36
  isl_ctx *ctx;
3352
36
  isl_vec *sub;
3353
36
  isl_aff *aff;
3354
36
3355
36
  n = isl_aff_list_n_aff(list);
3356
36
3357
36
  ctx = isl_tab_get_ctx(tab);
3358
36
  sub = isl_vec_alloc(ctx, 1 + dim + n);
3359
36
  if (!sub)
3360
0
    return -1;
3361
36
  isl_seq_clr(sub->el + 1 + dim, n);
3362
36
3363
89
  for (i = 0; i < n; 
++i53
) {
3364
53
    aff = isl_aff_list_get_aff(list, i);
3365
53
    if (!aff)
3366
0
      goto error;
3367
53
    if (isl_aff_is_nan(aff)) {
3368
5
      isl_aff_free(aff);
3369
5
      continue;
3370
5
    }
3371
48
    isl_seq_cpy(sub->el, aff->v->el + 1, 1 + dim);
3372
48
    isl_int_neg(sub->el[1 + dim + i], aff->v->el[0]);
3373
48
    if (isl_tab_add_eq(tab, sub->el) < 0)
3374
0
      goto error;
3375
48
    isl_int_set_si(sub->el[1 + dim + i], 0);
3376
48
    isl_aff_free(aff);
3377
48
  }
3378
36
3379
36
  isl_vec_free(sub);
3380
36
  return 0;
3381
0
error:
3382
0
  isl_aff_free(aff);
3383
0
  isl_vec_free(sub);
3384
0
  return -1;
3385
36
}
3386
3387
/* Add variables to info->tab and info->bmap corresponding to the elements
3388
 * in "list" that are not set to NaN.  The value of the added variable
3389
 * in info->tab is fixed to the purely affine expression defined by the element.
3390
 * "dim" is the offset in the variables of info->tab where we should
3391
 * start considering the elements in "list".
3392
 * When this function returns, the total number of variables in info->tab
3393
 * is equal to "dim" plus the number of elements in "list".
3394
 */
3395
static int add_subs(struct isl_coalesce_info *info,
3396
  __isl_keep isl_aff_list *list, int dim)
3397
36
{
3398
36
  int extra_var;
3399
36
  int n;
3400
36
3401
36
  if (!list)
3402
0
    return -1;
3403
36
3404
36
  n = isl_aff_list_n_aff(list);
3405
36
  extra_var = n - (info->tab->n_var - dim);
3406
36
3407
36
  if (isl_tab_extend_vars(info->tab, extra_var) < 0)
3408
0
    return -1;
3409
36
  if (isl_tab_extend_cons(info->tab, 2 * extra_var) < 0)
3410
0
    return -1;
3411
36
  if (add_sub_vars(info, list, dim, extra_var) < 0)
3412
0
    return -1;
3413
36
3414
36
  return add_sub_equalities(info->tab, list, dim);
3415
36
}
3416
3417
/* Coalesce basic map "j" into basic map "i" after adding the extra integer
3418
 * divisions in "i" but not in "j" to basic map "j", with values
3419
 * specified by "list".  The total number of elements in "list"
3420
 * is equal to the number of integer divisions in "i", while the number
3421
 * of NaN elements in the list is equal to the number of integer divisions
3422
 * in "j".
3423
 *
3424
 * If no coalescing can be performed, then we need to revert basic map "j"
3425
 * to its original state.  We do the same if basic map "i" gets dropped
3426
 * during the coalescing, even though this should not happen in practice
3427
 * since we have already checked for "j" being a subset of "i"
3428
 * before we reach this stage.
3429
 */
3430
static enum isl_change coalesce_with_subs(int i, int j,
3431
  struct isl_coalesce_info *info, __isl_keep isl_aff_list *list)
3432
36
{
3433
36
  isl_basic_map *bmap_j;
3434
36
  struct isl_tab_undo *snap;
3435
36
  unsigned dim;
3436
36
  enum isl_change change;
3437
36
3438
36
  bmap_j = isl_basic_map_copy(info[j].bmap);
3439
36
  snap = isl_tab_snap(info[j].tab);
3440
36
3441
36
  dim = isl_basic_map_dim(bmap_j, isl_dim_all);
3442
36
  dim -= isl_basic_map_dim(bmap_j, isl_dim_div);
3443
36
  if (add_subs(&info[j], list, dim) < 0)
3444
0
    goto error;
3445
36
3446
36
  change = coalesce_local_pair(i, j, info);
3447
36
  if (change != isl_change_none && 
change != isl_change_drop_first18
) {
3448
18
    isl_basic_map_free(bmap_j);
3449
18
  } else {
3450
18
    isl_basic_map_free(info[j].bmap);
3451
18
    info[j].bmap = bmap_j;
3452
18
3453
18
    if (isl_tab_rollback(info[j].tab, snap) < 0)
3454
0
      return isl_change_error;
3455
36
  }
3456
36
3457
36
  return change;
3458
0
error:
3459
0
  isl_basic_map_free(bmap_j);
3460
0
  return isl_change_error;
3461
36
}
3462
3463
/* Check if we can coalesce basic map "j" into basic map "i" after copying
3464
 * those extra integer divisions in "i" that can be simplified away
3465
 * using the extra equalities in "j".
3466
 * All divs are assumed to be known and not contain any nested divs.
3467
 *
3468
 * We first check if there are any extra equalities in "j" that we
3469
 * can exploit.  Then we check if every integer division in "i"
3470
 * either already appears in "j" or can be simplified using the
3471
 * extra equalities to a purely affine expression.
3472
 * If these tests succeed, then we try to coalesce the two basic maps
3473
 * by introducing extra dimensions in "j" corresponding to
3474
 * the extra integer divsisions "i" fixed to the corresponding
3475
 * purely affine expression.
3476
 */
3477
static enum isl_change check_coalesce_into_eq(int i, int j,
3478
  struct isl_coalesce_info *info)
3479
8.69k
{
3480
8.69k
  unsigned n_div_i, n_div_j;
3481
8.69k
  isl_basic_map *hull_i, *hull_j;
3482
8.69k
  int equal, empty;
3483
8.69k
  isl_aff_list *list;
3484
8.69k
  enum isl_change change;
3485
8.69k
3486
8.69k
  n_div_i = isl_basic_map_dim(info[i].bmap, isl_dim_div);
3487
8.69k
  n_div_j = isl_basic_map_dim(info[j].bmap, isl_dim_div);
3488
8.69k
  if (n_div_i <= n_div_j)
3489
4.44k
    return isl_change_none;
3490
4.25k
  if (info[j].bmap->n_eq == 0)
3491
4.12k
    return isl_change_none;
3492
133
3493
133
  hull_i = isl_basic_map_copy(info[i].bmap);
3494
133
  hull_i = isl_basic_map_plain_affine_hull(hull_i);
3495
133
  hull_j = isl_basic_map_copy(info[j].bmap);
3496
133
  hull_j = isl_basic_map_plain_affine_hull(hull_j);
3497
133
3498
133
  hull_j = isl_basic_map_intersect(hull_j, isl_basic_map_copy(hull_i));
3499
133
  equal = isl_basic_map_plain_is_equal(hull_i, hull_j);
3500
133
  empty = isl_basic_map_plain_is_empty(hull_j);
3501
133
  isl_basic_map_free(hull_i);
3502
133
3503
133
  if (equal < 0 || empty < 0)
3504
0
    goto error;
3505
133
  if (equal || 
empty93
) {
3506
57
    isl_basic_map_free(hull_j);
3507
57
    return isl_change_none;
3508
57
  }
3509
76
3510
76
  list = set_up_substitutions(info[i].bmap, info[j].bmap, hull_j);
3511
76
  if (!list)
3512
0
    return isl_change_error;
3513
76
  if (isl_aff_list_n_aff(list) < n_div_i)
3514
40
    change = isl_change_none;
3515
36
  else
3516
36
    change = coalesce_with_subs(i, j, info, list);
3517
76
3518
76
  isl_aff_list_free(list);
3519
76
3520
76
  return change;
3521
0
error:
3522
0
  isl_basic_map_free(hull_j);
3523
0
  return isl_change_error;
3524
8.69k
}
3525
3526
/* Check if we can coalesce basic maps "i" and "j" after copying
3527
 * those extra integer divisions in one of the basic maps that can
3528
 * be simplified away using the extra equalities in the other basic map.
3529
 * We require all divs to be known in both basic maps.
3530
 * Furthermore, to simplify the comparison of div expressions,
3531
 * we do not allow any nested integer divisions.
3532
 */
3533
static enum isl_change check_coalesce_eq(int i, int j,
3534
  struct isl_coalesce_info *info)
3535
4.40k
{
3536
4.40k
  isl_bool known, nested;
3537
4.40k
  enum isl_change change;
3538
4.40k
3539
4.40k
  known = isl_basic_map_divs_known(info[i].bmap);
3540
4.40k
  if (known < 0 || !known)
3541
35
    return known < 0 ? 
isl_change_error0
: isl_change_none;
3542
4.37k
  known = isl_basic_map_divs_known(info[j].bmap);
3543
4.37k
  if (known < 0 || !known)
3544
15
    return known < 0 ? 
isl_change_error0
: isl_change_none;
3545
4.35k
  nested = has_nested_div(info[i].bmap);
3546
4.35k
  if (nested < 0 || nested)
3547
1
    return nested < 0 ? 
isl_change_error0
: isl_change_none;
3548
4.35k
  nested = has_nested_div(info[j].bmap);
3549
4.35k
  if (nested < 0 || nested)
3550
3
    return nested < 0 ? 
isl_change_error0
: isl_change_none;
3551
4.35k
3552
4.35k
  change = check_coalesce_into_eq(i, j, info);
3553
4.35k
  if (change != isl_change_none)
3554
5
    return change;
3555
4.34k
  change = check_coalesce_into_eq(j, i, info);
3556
4.34k
  if (change != isl_change_none)
3557
13
    return invert_change(change);
3558
4.33k
3559
4.33k
  return isl_change_none;
3560
4.33k
}
3561
3562
/* Check if the union of the given pair of basic maps
3563
 * can be represented by a single basic map.
3564
 * If so, replace the pair by the single basic map and return
3565
 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
3566
 * Otherwise, return isl_change_none.
3567
 *
3568
 * We first check if the two basic maps live in the same local space,
3569
 * after aligning the divs that differ by only an integer constant.
3570
 * If so, we do the complete check.  Otherwise, we check if they have
3571
 * the same number of integer divisions and can be coalesced, if one is
3572
 * an obvious subset of the other or if the extra integer divisions
3573
 * of one basic map can be simplified away using the extra equalities
3574
 * of the other basic map.
3575
 */
3576
static enum isl_change coalesce_pair(int i, int j,
3577
  struct isl_coalesce_info *info)
3578
67.1k
{
3579
67.1k
  isl_bool same;
3580
67.1k
  enum isl_change change;
3581
67.1k
3582
67.1k
  if (harmonize_divs(&info[i], &info[j]) < 0)
3583
0
    return isl_change_error;
3584
67.1k
  same = same_divs(info[i].bmap, info[j].bmap);
3585
67.1k
  if (same < 0)
3586
0
    return isl_change_error;
3587
67.1k
  if (same)
3588
59.6k
    return coalesce_local_pair(i, j, info);
3589
7.46k
3590
7.46k
  if (info[i].bmap->n_div == info[j].bmap->n_div) {
3591
109
    change = coalesce_local_pair(i, j, info);
3592
109
    if (change != isl_change_none)
3593
6
      return change;
3594
7.46k
  }
3595
7.46k
3596
7.46k
  change = coalesce_divs(i, j, info);
3597
7.46k
  if (change != isl_change_none)
3598
3.05k
    return change;
3599
4.40k
3600
4.40k
  return check_coalesce_eq(i, j, info);
3601
4.40k
}
3602
3603
/* Return the maximum of "a" and "b".
3604
 */
3605
static int isl_max(int a, int b)
3606
111k
{
3607
111k
  return a > b ? 
a57.7k
:
b53.4k
;
3608
111k
}
3609
3610
/* Pairwise coalesce the basic maps in the range [start1, end1[ of "info"
3611
 * with those in the range [start2, end2[, skipping basic maps
3612
 * that have been removed (either before or within this function).
3613
 *
3614
 * For each basic map i in the first range, we check if it can be coalesced
3615
 * with respect to any previously considered basic map j in the second range.
3616
 * If i gets dropped (because it was a subset of some j), then
3617
 * we can move on to the next basic map.
3618
 * If j gets dropped, we need to continue checking against the other
3619
 * previously considered basic maps.
3620
 * If the two basic maps got fused, then we recheck the fused basic map
3621
 * against the previously considered basic maps, starting at i + 1
3622
 * (even if start2 is greater than i + 1).
3623
 */
3624
static int coalesce_range(isl_ctx *ctx, struct isl_coalesce_info *info,
3625
  int start1, int end1, int start2, int end2)
3626
81.2k
{
3627
81.2k
  int i, j;
3628
81.2k
3629
196k
  for (i = end1 - 1; i >= start1; 
--i115k
) {
3630
115k
    if (info[i].removed)
3631
4.46k
      continue;
3632
191k
    
for (j = isl_max(i + 1, start2); 111k
j < end2;
++j80.1k
) {
3633
80.1k
      enum isl_change changed;
3634
80.1k
3635
80.1k
      if (info[j].removed)
3636
13.0k
        continue;
3637
67.1k
      if (info[i].removed)
3638
67.1k
        
isl_die0
(ctx, isl_error_internal,
3639
67.1k
          "basic map unexpectedly removed",
3640
67.1k
          return -1);
3641
67.1k
      changed = coalesce_pair(i, j, info);
3642
67.1k
      switch (changed) {
3643
67.1k
      case isl_change_error:
3644
0
        return -1;
3645
67.1k
      case isl_change_none:
3646
62.4k
      case isl_change_drop_second:
3647
62.4k
        continue;
3648
62.4k
      case isl_change_drop_first:
3649
2.93k
        j = end2;
3650
2.93k
        break;
3651
62.4k
      case isl_change_fuse:
3652
1.73k
        j = i;
3653
1.73k
        break;
3654
80.1k
      }
3655
80.1k
    }
3656
115k
  }
3657
81.2k
3658
81.2k
  return 0;
3659
81.2k
}
3660
3661
/* Pairwise coalesce the basic maps described by the "n" elements of "info".
3662
 *
3663
 * We consider groups of basic maps that live in the same apparent
3664
 * affine hull and we first coalesce within such a group before we
3665
 * coalesce the elements in the group with elements of previously
3666
 * considered groups.  If a fuse happens during the second phase,
3667
 * then we also reconsider the elements within the group.
3668
 */
3669
static int coalesce(isl_ctx *ctx, int n, struct isl_coalesce_info *info)
3670
24.1k
{
3671
24.1k
  int start, end;
3672
24.1k
3673
64.7k
  for (end = n; end > 0; 
end = start40.6k
) {
3674
40.6k
    start = end - 1;
3675
57.7k
    while (start >= 1 &&
3676
57.7k
        
info[start - 1].hull_hash == info[start].hull_hash33.6k
)
3677
40.6k
      
start--17.1k
;
3678
40.6k
    if (coalesce_range(ctx, info, start, end, start, end) < 0)
3679
0
      return -1;
3680
40.6k
    if (coalesce_range(ctx, info, start, end, end, n) < 0)
3681
0
      return -1;
3682
40.6k
  }
3683
24.1k
3684
24.1k
  return 0;
3685
24.1k
}
3686
3687
/* Update the basic maps in "map" based on the information in "info".
3688
 * In particular, remove the basic maps that have been marked removed and
3689
 * update the others based on the information in the corresponding tableau.
3690
 * Since we detected implicit equalities without calling
3691
 * isl_basic_map_gauss, we need to do it now.
3692
 * Also call isl_basic_map_simplify if we may have lost the definition
3693
 * of one or more integer divisions.
3694
 */
3695
static __isl_give isl_map *update_basic_maps(__isl_take isl_map *map,
3696
  int n, struct isl_coalesce_info *info)
3697
24.1k
{
3698
24.1k
  int i;
3699
24.1k
3700
24.1k
  if (!map)
3701
0
    return NULL;
3702
24.1k
3703
81.8k
  
for (i = n - 1; 24.1k
i >= 0;
--i57.7k
) {
3704
57.7k
    if (info[i].removed) {
3705
9.97k
      isl_basic_map_free(map->p[i]);
3706
9.97k
      if (i != map->n - 1)
3707
3.67k
        map->p[i] = map->p[map->n - 1];
3708
9.97k
      map->n--;
3709
9.97k
      continue;
3710
9.97k
    }
3711
47.8k
3712
47.8k
    info[i].bmap = isl_basic_map_update_from_tab(info[i].bmap,
3713
47.8k
              info[i].tab);
3714
47.8k
    info[i].bmap = isl_basic_map_gauss(info[i].bmap, NULL);
3715
47.8k
    if (info[i].simplify)
3716
22
      info[i].bmap = isl_basic_map_simplify(info[i].bmap);
3717
47.8k
    info[i].bmap = isl_basic_map_finalize(info[i].bmap);
3718
47.8k
    if (!info[i].bmap)
3719
0
      return isl_map_free(map);
3720
47.8k
    ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_NO_IMPLICIT);
3721
47.8k
    ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_NO_REDUNDANT);
3722
57.7k
    isl_basic_map_free(map->p[i]);
3723
57.7k
    map->p[i] = info[i].bmap;
3724
57.7k
    info[i].bmap = NULL;
3725
57.7k
  }
3726
24.1k
3727
24.1k
  return map;
3728
24.1k
}
3729
3730
/* For each pair of basic maps in the map, check if the union of the two
3731
 * can be represented by a single basic map.
3732
 * If so, replace the pair by the single basic map and start over.
3733
 *
3734
 * We factor out any (hidden) common factor from the constraint
3735
 * coefficients to improve the detection of adjacent constraints.
3736
 *
3737
 * Since we are constructing the tableaus of the basic maps anyway,
3738
 * we exploit them to detect implicit equalities and redundant constraints.
3739
 * This also helps the coalescing as it can ignore the redundant constraints.
3740
 * In order to avoid confusion, we make all implicit equalities explicit
3741
 * in the basic maps.  We don't call isl_basic_map_gauss, though,
3742
 * as that may affect the number of constraints.
3743
 * This means that we have to call isl_basic_map_gauss at the end
3744
 * of the computation (in update_basic_maps) to ensure that
3745
 * the basic maps are not left in an unexpected state.
3746
 * For each basic map, we also compute the hash of the apparent affine hull
3747
 * for use in coalesce.
3748
 */
3749
__isl_give isl_map *isl_map_coalesce(__isl_take isl_map *map)
3750
107k
{
3751
107k
  int i;
3752
107k
  unsigned n;
3753
107k
  isl_ctx *ctx;
3754
107k
  struct isl_coalesce_info *info = NULL;
3755
107k
3756
107k
  map = isl_map_remove_empty_parts(map);
3757
107k
  if (!map)
3758
0
    return NULL;
3759
107k
3760
107k
  if (map->n <= 1)
3761
83.7k
    return map;
3762
24.1k
3763
24.1k
  ctx = isl_map_get_ctx(map);
3764
24.1k
  map = isl_map_sort_divs(map);
3765
24.1k
  map = isl_map_cow(map);
3766
24.1k
3767
24.1k
  if (!map)
3768
0
    return NULL;
3769
24.1k
3770
24.1k
  n = map->n;
3771
24.1k
3772
24.1k
  info = isl_calloc_array(map->ctx, struct isl_coalesce_info, n);
3773
24.1k
  if (!info)
3774
0
    goto error;
3775
24.1k
3776
81.8k
  
for (i = 0; 24.1k
i < map->n;
++i57.7k
) {
3777
57.7k
    map->p[i] = isl_basic_map_reduce_coefficients(map->p[i]);
3778
57.7k
    if (!map->p[i])
3779
0
      goto error;
3780
57.7k
    info[i].bmap = isl_basic_map_copy(map->p[i]);
3781
57.7k
    info[i].tab = isl_tab_from_basic_map(info[i].bmap, 0);
3782
57.7k
    if (!info[i].tab)
3783
0
      goto error;
3784
57.7k
    if (!ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_NO_IMPLICIT))
3785
57.7k
      
if (32.3k
isl_tab_detect_implicit_equalities(info[i].tab) < 032.3k
)
3786
0
        goto error;
3787
57.7k
    info[i].bmap = isl_tab_make_equalities_explicit(info[i].tab,
3788
57.7k
                info[i].bmap);
3789
57.7k
    if (!info[i].bmap)
3790
0
      goto error;
3791
57.7k
    if (!ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_NO_REDUNDANT))
3792
57.7k
      
if (35.0k
isl_tab_detect_redundant(info[i].tab) < 035.0k
)
3793
0
        goto error;
3794
57.7k
    if (coalesce_info_set_hull_hash(&info[i]) < 0)
3795
0
      goto error;
3796
57.7k
  }
3797
81.8k
  
for (i = map->n - 1; 24.1k
i >= 0;
--i57.7k
)
3798
57.7k
    if (info[i].tab->empty)
3799
75
      drop(&info[i]);
3800
24.1k
3801
24.1k
  if (coalesce(ctx, n, info) < 0)
3802
0
    goto error;
3803
24.1k
3804
24.1k
  map = update_basic_maps(map, n, info);
3805
24.1k
3806
24.1k
  clear_coalesce_info(n, info);
3807
24.1k
3808
24.1k
  return map;
3809
0
error:
3810
0
  clear_coalesce_info(n, info);
3811
0
  isl_map_free(map);
3812
0
  return NULL;
3813
107k
}
3814
3815
/* For each pair of basic sets in the set, check if the union of the two
3816
 * can be represented by a single basic set.
3817
 * If so, replace the pair by the single basic set and start over.
3818
 */
3819
struct isl_set *isl_set_coalesce(struct isl_set *set)
3820
93.5k
{
3821
93.5k
  return set_from_map(isl_map_coalesce(set_to_map(set)));
3822
93.5k
}