Coverage Report

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