Coverage Report

Created: 2017-08-21 19:50

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