Coverage Report

Created: 2018-06-24 14:39

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