2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
5 * Use of this software is governed by the GNU LGPLv2.1 license
7 * Written by Sven Verdoolaege, K.U.Leuven, Departement
8 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
10 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
13 #include "isl_map_private.h"
17 #define STATUS_ERROR -1
18 #define STATUS_REDUNDANT 1
19 #define STATUS_VALID 2
20 #define STATUS_SEPARATE 3
22 #define STATUS_ADJ_EQ 5
23 #define STATUS_ADJ_INEQ 6
25 static int status_in(isl_int
*ineq
, struct isl_tab
*tab
)
27 enum isl_ineq_type type
= isl_tab_ineq_type(tab
, ineq
);
30 case isl_ineq_error
: return STATUS_ERROR
;
31 case isl_ineq_redundant
: return STATUS_VALID
;
32 case isl_ineq_separate
: return STATUS_SEPARATE
;
33 case isl_ineq_cut
: return STATUS_CUT
;
34 case isl_ineq_adj_eq
: return STATUS_ADJ_EQ
;
35 case isl_ineq_adj_ineq
: return STATUS_ADJ_INEQ
;
39 /* Compute the position of the equalities of basic map "i"
40 * with respect to basic map "j".
41 * The resulting array has twice as many entries as the number
42 * of equalities corresponding to the two inequalties to which
43 * each equality corresponds.
45 static int *eq_status_in(struct isl_map
*map
, int i
, int j
,
46 struct isl_tab
**tabs
)
49 int *eq
= isl_calloc_array(map
->ctx
, int, 2 * map
->p
[i
]->n_eq
);
52 dim
= isl_basic_map_total_dim(map
->p
[i
]);
53 for (k
= 0; k
< map
->p
[i
]->n_eq
; ++k
) {
54 for (l
= 0; l
< 2; ++l
) {
55 isl_seq_neg(map
->p
[i
]->eq
[k
], map
->p
[i
]->eq
[k
], 1+dim
);
56 eq
[2 * k
+ l
] = status_in(map
->p
[i
]->eq
[k
], tabs
[j
]);
57 if (eq
[2 * k
+ l
] == STATUS_ERROR
)
60 if (eq
[2 * k
] == STATUS_SEPARATE
||
61 eq
[2 * k
+ 1] == STATUS_SEPARATE
)
71 /* Compute the position of the inequalities of basic map "i"
72 * with respect to basic map "j".
74 static int *ineq_status_in(struct isl_map
*map
, int i
, int j
,
75 struct isl_tab
**tabs
)
78 unsigned n_eq
= map
->p
[i
]->n_eq
;
79 int *ineq
= isl_calloc_array(map
->ctx
, int, map
->p
[i
]->n_ineq
);
81 for (k
= 0; k
< map
->p
[i
]->n_ineq
; ++k
) {
82 if (isl_tab_is_redundant(tabs
[i
], n_eq
+ k
)) {
83 ineq
[k
] = STATUS_REDUNDANT
;
86 ineq
[k
] = status_in(map
->p
[i
]->ineq
[k
], tabs
[j
]);
87 if (ineq
[k
] == STATUS_ERROR
)
89 if (ineq
[k
] == STATUS_SEPARATE
)
99 static int any(int *con
, unsigned len
, int status
)
103 for (i
= 0; i
< len
; ++i
)
104 if (con
[i
] == status
)
109 static int count(int *con
, unsigned len
, int status
)
114 for (i
= 0; i
< len
; ++i
)
115 if (con
[i
] == status
)
120 static int all(int *con
, unsigned len
, int status
)
124 for (i
= 0; i
< len
; ++i
) {
125 if (con
[i
] == STATUS_REDUNDANT
)
127 if (con
[i
] != status
)
133 static void drop(struct isl_map
*map
, int i
, struct isl_tab
**tabs
)
135 isl_basic_map_free(map
->p
[i
]);
136 isl_tab_free(tabs
[i
]);
138 if (i
!= map
->n
- 1) {
139 map
->p
[i
] = map
->p
[map
->n
- 1];
140 tabs
[i
] = tabs
[map
->n
- 1];
142 tabs
[map
->n
- 1] = NULL
;
146 /* Replace the pair of basic maps i and j by the basic map bounded
147 * by the valid constraints in both basic maps and the constraint
148 * in extra (if not NULL).
150 static int fuse(struct isl_map
*map
, int i
, int j
,
151 struct isl_tab
**tabs
, int *eq_i
, int *ineq_i
, int *eq_j
, int *ineq_j
,
152 __isl_keep isl_mat
*extra
)
155 struct isl_basic_map
*fused
= NULL
;
156 struct isl_tab
*fused_tab
= NULL
;
157 unsigned total
= isl_basic_map_total_dim(map
->p
[i
]);
158 unsigned extra_rows
= extra
? extra
->n_row
: 0;
160 fused
= isl_basic_map_alloc_dim(isl_dim_copy(map
->p
[i
]->dim
),
162 map
->p
[i
]->n_eq
+ map
->p
[j
]->n_eq
,
163 map
->p
[i
]->n_ineq
+ map
->p
[j
]->n_ineq
+ extra_rows
);
167 for (k
= 0; k
< map
->p
[i
]->n_eq
; ++k
) {
168 if (eq_i
&& (eq_i
[2 * k
] != STATUS_VALID
||
169 eq_i
[2 * k
+ 1] != STATUS_VALID
))
171 l
= isl_basic_map_alloc_equality(fused
);
174 isl_seq_cpy(fused
->eq
[l
], map
->p
[i
]->eq
[k
], 1 + total
);
177 for (k
= 0; k
< map
->p
[j
]->n_eq
; ++k
) {
178 if (eq_j
&& (eq_j
[2 * k
] != STATUS_VALID
||
179 eq_j
[2 * k
+ 1] != STATUS_VALID
))
181 l
= isl_basic_map_alloc_equality(fused
);
184 isl_seq_cpy(fused
->eq
[l
], map
->p
[j
]->eq
[k
], 1 + total
);
187 for (k
= 0; k
< map
->p
[i
]->n_ineq
; ++k
) {
188 if (ineq_i
[k
] != STATUS_VALID
)
190 l
= isl_basic_map_alloc_inequality(fused
);
193 isl_seq_cpy(fused
->ineq
[l
], map
->p
[i
]->ineq
[k
], 1 + total
);
196 for (k
= 0; k
< map
->p
[j
]->n_ineq
; ++k
) {
197 if (ineq_j
[k
] != STATUS_VALID
)
199 l
= isl_basic_map_alloc_inequality(fused
);
202 isl_seq_cpy(fused
->ineq
[l
], map
->p
[j
]->ineq
[k
], 1 + total
);
205 for (k
= 0; k
< map
->p
[i
]->n_div
; ++k
) {
206 int l
= isl_basic_map_alloc_div(fused
);
209 isl_seq_cpy(fused
->div
[l
], map
->p
[i
]->div
[k
], 1 + 1 + total
);
212 for (k
= 0; k
< extra_rows
; ++k
) {
213 l
= isl_basic_map_alloc_inequality(fused
);
216 isl_seq_cpy(fused
->ineq
[l
], extra
->row
[k
], 1 + total
);
219 fused
= isl_basic_map_gauss(fused
, NULL
);
220 ISL_F_SET(fused
, ISL_BASIC_MAP_FINAL
);
221 if (ISL_F_ISSET(map
->p
[i
], ISL_BASIC_MAP_RATIONAL
) &&
222 ISL_F_ISSET(map
->p
[j
], ISL_BASIC_MAP_RATIONAL
))
223 ISL_F_SET(fused
, ISL_BASIC_MAP_RATIONAL
);
225 fused_tab
= isl_tab_from_basic_map(fused
);
226 if (isl_tab_detect_redundant(fused_tab
) < 0)
229 isl_basic_map_free(map
->p
[i
]);
231 isl_tab_free(tabs
[i
]);
237 isl_tab_free(fused_tab
);
238 isl_basic_map_free(fused
);
242 /* Given a pair of basic maps i and j such that all constraints are either
243 * "valid" or "cut", check if the facets corresponding to the "cut"
244 * constraints of i lie entirely within basic map j.
245 * If so, replace the pair by the basic map consisting of the valid
246 * constraints in both basic maps.
248 * To see that we are not introducing any extra points, call the
249 * two basic maps A and B and the resulting map U and let x
250 * be an element of U \setminus ( A \cup B ).
251 * Then there is a pair of cut constraints c_1 and c_2 in A and B such that x
252 * violates them. Let X be the intersection of U with the opposites
253 * of these constraints. Then x \in X.
254 * The facet corresponding to c_1 contains the corresponding facet of A.
255 * This facet is entirely contained in B, so c_2 is valid on the facet.
256 * However, since it is also (part of) a facet of X, -c_2 is also valid
257 * on the facet. This means c_2 is saturated on the facet, so c_1 and
258 * c_2 must be opposites of each other, but then x could not violate
261 static int check_facets(struct isl_map
*map
, int i
, int j
,
262 struct isl_tab
**tabs
, int *ineq_i
, int *ineq_j
)
265 struct isl_tab_undo
*snap
;
266 unsigned n_eq
= map
->p
[i
]->n_eq
;
268 snap
= isl_tab_snap(tabs
[i
]);
270 for (k
= 0; k
< map
->p
[i
]->n_ineq
; ++k
) {
271 if (ineq_i
[k
] != STATUS_CUT
)
273 tabs
[i
] = isl_tab_select_facet(tabs
[i
], n_eq
+ k
);
274 for (l
= 0; l
< map
->p
[j
]->n_ineq
; ++l
) {
276 if (ineq_j
[l
] != STATUS_CUT
)
278 stat
= status_in(map
->p
[j
]->ineq
[l
], tabs
[i
]);
279 if (stat
!= STATUS_VALID
)
282 if (isl_tab_rollback(tabs
[i
], snap
) < 0)
284 if (l
< map
->p
[j
]->n_ineq
)
288 if (k
< map
->p
[i
]->n_ineq
)
291 return fuse(map
, i
, j
, tabs
, NULL
, ineq_i
, NULL
, ineq_j
, NULL
);
294 /* Both basic maps have at least one inequality with and adjacent
295 * (but opposite) inequality in the other basic map.
296 * Check that there are no cut constraints and that there is only
297 * a single pair of adjacent inequalities.
298 * If so, we can replace the pair by a single basic map described
299 * by all but the pair of adjacent inequalities.
300 * Any additional points introduced lie strictly between the two
301 * adjacent hyperplanes and can therefore be integral.
310 * The test for a single pair of adjancent inequalities is important
311 * for avoiding the combination of two basic maps like the following
321 static int check_adj_ineq(struct isl_map
*map
, int i
, int j
,
322 struct isl_tab
**tabs
, int *ineq_i
, int *ineq_j
)
326 if (any(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_CUT
) ||
327 any(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_CUT
))
330 else if (count(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_ADJ_INEQ
) == 1 &&
331 count(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_ADJ_INEQ
) == 1)
332 changed
= fuse(map
, i
, j
, tabs
, NULL
, ineq_i
, NULL
, ineq_j
, NULL
);
333 /* else ADJ INEQ TOO MANY */
338 /* Check if basic map "i" contains the basic map represented
339 * by the tableau "tab".
341 static int contains(struct isl_map
*map
, int i
, int *ineq_i
,
347 dim
= isl_basic_map_total_dim(map
->p
[i
]);
348 for (k
= 0; k
< map
->p
[i
]->n_eq
; ++k
) {
349 for (l
= 0; l
< 2; ++l
) {
351 isl_seq_neg(map
->p
[i
]->eq
[k
], map
->p
[i
]->eq
[k
], 1+dim
);
352 stat
= status_in(map
->p
[i
]->eq
[k
], tab
);
353 if (stat
!= STATUS_VALID
)
358 for (k
= 0; k
< map
->p
[i
]->n_ineq
; ++k
) {
360 if (ineq_i
[k
] == STATUS_REDUNDANT
)
362 stat
= status_in(map
->p
[i
]->ineq
[k
], tab
);
363 if (stat
!= STATUS_VALID
)
369 /* Basic map "i" has an inequality "k" that is adjacent to some equality
370 * of basic map "j". All the other inequalities are valid for "j".
371 * Check if basic map "j" forms an extension of basic map "i".
373 * In particular, we relax constraint "k", compute the corresponding
374 * facet and check whether it is included in the other basic map.
375 * If so, we know that relaxing the constraint extends the basic
376 * map with exactly the other basic map (we already know that this
377 * other basic map is included in the extension, because there
378 * were no "cut" inequalities in "i") and we can replace the
379 * two basic maps by thie extension.
387 static int is_extension(struct isl_map
*map
, int i
, int j
, int k
,
388 struct isl_tab
**tabs
, int *eq_i
, int *ineq_i
, int *eq_j
, int *ineq_j
)
392 struct isl_tab_undo
*snap
, *snap2
;
393 unsigned n_eq
= map
->p
[i
]->n_eq
;
395 snap
= isl_tab_snap(tabs
[i
]);
396 tabs
[i
] = isl_tab_relax(tabs
[i
], n_eq
+ k
);
397 snap2
= isl_tab_snap(tabs
[i
]);
398 tabs
[i
] = isl_tab_select_facet(tabs
[i
], n_eq
+ k
);
399 super
= contains(map
, j
, ineq_j
, tabs
[i
]);
401 if (isl_tab_rollback(tabs
[i
], snap2
) < 0)
403 map
->p
[i
] = isl_basic_map_cow(map
->p
[i
]);
406 isl_int_add_ui(map
->p
[i
]->ineq
[k
][0], map
->p
[i
]->ineq
[k
][0], 1);
407 ISL_F_SET(map
->p
[i
], ISL_BASIC_MAP_FINAL
);
411 if (isl_tab_rollback(tabs
[i
], snap
) < 0)
417 /* For each non-redundant constraint in "bmap" (as determined by "tab"),
418 * wrap the constraint around "bound" such that it includes the whole
419 * set "set" and append the resulting constraint to "wraps".
420 * "wraps" is assumed to have been pre-allocated to the appropriate size.
421 * wraps->n_row is the number of actual wrapped constraints that have
423 * If any of the wrapping problems results in a constraint that is
424 * identical to "bound", then this means that "set" is unbounded in such
425 * way that no wrapping is possible. If this happens then wraps->n_row
428 static int add_wraps(__isl_keep isl_mat
*wraps
, __isl_keep isl_basic_map
*bmap
,
429 struct isl_tab
*tab
, isl_int
*bound
, __isl_keep isl_set
*set
)
433 unsigned total
= isl_basic_map_total_dim(bmap
);
437 for (l
= 0; l
< bmap
->n_ineq
; ++l
) {
438 if (isl_seq_is_neg(bound
, bmap
->ineq
[l
], 1 + total
))
440 if (isl_seq_eq(bound
, bmap
->ineq
[l
], 1 + total
))
442 if (isl_tab_is_redundant(tab
, bmap
->n_eq
+ l
))
445 isl_seq_cpy(wraps
->row
[w
], bound
, 1 + total
);
446 if (!isl_set_wrap_facet(set
, wraps
->row
[w
], bmap
->ineq
[l
]))
448 if (isl_seq_eq(wraps
->row
[w
], bound
, 1 + total
))
452 for (l
= 0; l
< bmap
->n_eq
; ++l
) {
453 if (isl_seq_is_neg(bound
, bmap
->eq
[l
], 1 + total
))
455 if (isl_seq_eq(bound
, bmap
->eq
[l
], 1 + total
))
458 isl_seq_cpy(wraps
->row
[w
], bound
, 1 + total
);
459 isl_seq_neg(wraps
->row
[w
+ 1], bmap
->eq
[l
], 1 + total
);
460 if (!isl_set_wrap_facet(set
, wraps
->row
[w
], wraps
->row
[w
+ 1]))
462 if (isl_seq_eq(wraps
->row
[w
], bound
, 1 + total
))
466 isl_seq_cpy(wraps
->row
[w
], bound
, 1 + total
);
467 if (!isl_set_wrap_facet(set
, wraps
->row
[w
], bmap
->eq
[l
]))
469 if (isl_seq_eq(wraps
->row
[w
], bound
, 1 + total
))
481 /* Check if the constraints in "wraps" from "first" until the last
482 * are all valid for the basic set represented by "tab".
483 * If not, wraps->n_row is set to zero.
485 static int check_wraps(__isl_keep isl_mat
*wraps
, int first
,
490 for (i
= first
; i
< wraps
->n_row
; ++i
) {
491 enum isl_ineq_type type
;
492 type
= isl_tab_ineq_type(tab
, wraps
->row
[i
]);
493 if (type
== isl_ineq_error
)
495 if (type
== isl_ineq_redundant
)
504 /* Return a set that corresponds to the non-redudant constraints
505 * (as recorded in tab) of bmap.
507 * It's important to remove the redundant constraints as some
508 * of the other constraints may have been modified after the
509 * constraints were marked redundant.
510 * In particular, a constraint may have been relaxed.
511 * Redundant constraints are ignored when a constraint is relaxed
512 * and should therefore continue to be ignored ever after.
513 * Otherwise, the relaxation might be thwarted by some of
516 static __isl_give isl_set
*set_from_updated_bmap(__isl_keep isl_basic_map
*bmap
,
519 bmap
= isl_basic_map_copy(bmap
);
520 bmap
= isl_basic_map_cow(bmap
);
521 bmap
= isl_basic_map_update_from_tab(bmap
, tab
);
522 return isl_set_from_basic_set(isl_basic_map_underlying_set(bmap
));
525 /* Given a basic set i with a constraint k that is adjacent to either the
526 * whole of basic set j or a facet of basic set j, check if we can wrap
527 * both the facet corresponding to k and the facet of j (or the whole of j)
528 * around their ridges to include the other set.
529 * If so, replace the pair of basic sets by their union.
531 * All constraints of i (except k) are assumed to be valid for j.
533 * However, the constraints of j may not be valid for i and so
534 * we have to check that the wrapping constraints for j are valid for i.
536 * In the case where j has a facet adjacent to i, tab[j] is assumed
537 * to have been restricted to this facet, so that the non-redundant
538 * constraints in tab[j] are the ridges of the facet.
539 * Note that for the purpose of wrapping, it does not matter whether
540 * we wrap the ridges of i around the whole of j or just around
541 * the facet since all the other constraints are assumed to be valid for j.
542 * In practice, we wrap to include the whole of j.
551 static int can_wrap_in_facet(struct isl_map
*map
, int i
, int j
, int k
,
552 struct isl_tab
**tabs
, int *eq_i
, int *ineq_i
, int *eq_j
, int *ineq_j
)
555 struct isl_mat
*wraps
= NULL
;
556 struct isl_set
*set_i
= NULL
;
557 struct isl_set
*set_j
= NULL
;
558 struct isl_vec
*bound
= NULL
;
559 unsigned total
= isl_basic_map_total_dim(map
->p
[i
]);
560 struct isl_tab_undo
*snap
;
563 set_i
= set_from_updated_bmap(map
->p
[i
], tabs
[i
]);
564 set_j
= set_from_updated_bmap(map
->p
[j
], tabs
[j
]);
565 wraps
= isl_mat_alloc(map
->ctx
, 2 * (map
->p
[i
]->n_eq
+ map
->p
[j
]->n_eq
) +
566 map
->p
[i
]->n_ineq
+ map
->p
[j
]->n_ineq
,
568 bound
= isl_vec_alloc(map
->ctx
, 1 + total
);
569 if (!set_i
|| !set_j
|| !wraps
|| !bound
)
572 isl_seq_cpy(bound
->el
, map
->p
[i
]->ineq
[k
], 1 + total
);
573 isl_int_add_ui(bound
->el
[0], bound
->el
[0], 1);
575 isl_seq_cpy(wraps
->row
[0], bound
->el
, 1 + total
);
578 if (add_wraps(wraps
, map
->p
[j
], tabs
[j
], bound
->el
, set_i
) < 0)
583 snap
= isl_tab_snap(tabs
[i
]);
585 tabs
[i
] = isl_tab_select_facet(tabs
[i
], map
->p
[i
]->n_eq
+ k
);
586 if (isl_tab_detect_redundant(tabs
[i
]) < 0)
589 isl_seq_neg(bound
->el
, map
->p
[i
]->ineq
[k
], 1 + total
);
592 if (add_wraps(wraps
, map
->p
[i
], tabs
[i
], bound
->el
, set_j
) < 0)
595 if (isl_tab_rollback(tabs
[i
], snap
) < 0)
597 if (check_wraps(wraps
, n
, tabs
[i
]) < 0)
602 changed
= fuse(map
, i
, j
, tabs
, eq_i
, ineq_i
, eq_j
, ineq_j
, wraps
);
621 /* Set the is_redundant property of the "n" constraints in "cuts",
623 * This is a fairly tricky operation as it bypasses isl_tab.c.
624 * The reason we want to temporarily mark some constraints redundant
625 * is that we want to ignore them in add_wraps.
627 * Initially all cut constraints are non-redundant, but the
628 * selection of a facet right before the call to this function
629 * may have made some of them redundant.
630 * Likewise, the same constraints are marked non-redundant
631 * in the second call to this function, before they are officially
632 * made non-redundant again in the subsequent rollback.
634 static void set_is_redundant(struct isl_tab
*tab
, unsigned n_eq
,
635 int *cuts
, int n
, int k
, int v
)
639 for (l
= 0; l
< n
; ++l
) {
642 tab
->con
[n_eq
+ cuts
[l
]].is_redundant
= v
;
646 /* Given a pair of basic maps i and j such that j stick out
647 * of i at n cut constraints, each time by at most one,
648 * try to compute wrapping constraints and replace the two
649 * basic maps by a single basic map.
650 * The other constraints of i are assumed to be valid for j.
652 * The facets of i corresponding to the cut constraints are
653 * wrapped around their ridges, except those ridges determined
654 * by any of the other cut constraints.
655 * The intersections of cut constraints need to be ignored
656 * as the result of wrapping on cur constraint around another
657 * would result in a constraint cutting the union.
658 * In each case, the facets are wrapped to include the union
659 * of the two basic maps.
661 * The pieces of j that lie at an offset of exactly one from
662 * one of the cut constraints of i are wrapped around their edges.
663 * Here, there is no need to ignore intersections because we
664 * are wrapping around the union of the two basic maps.
666 * If any wrapping fails, i.e., if we cannot wrap to touch
667 * the union, then we give up.
668 * Otherwise, the pair of basic maps is replaced by their union.
670 static int wrap_in_facets(struct isl_map
*map
, int i
, int j
,
671 int *cuts
, int n
, struct isl_tab
**tabs
,
672 int *eq_i
, int *ineq_i
, int *eq_j
, int *ineq_j
)
675 isl_mat
*wraps
= NULL
;
677 isl_vec
*bound
= NULL
;
678 unsigned total
= isl_basic_map_total_dim(map
->p
[i
]);
681 struct isl_tab_undo
*snap_i
, *snap_j
;
683 if (isl_tab_extend_cons(tabs
[j
], 1) < 0)
686 max_wrap
= 2 * (map
->p
[i
]->n_eq
+ map
->p
[j
]->n_eq
) +
687 map
->p
[i
]->n_ineq
+ map
->p
[j
]->n_ineq
;
690 set
= isl_set_union(set_from_updated_bmap(map
->p
[i
], tabs
[i
]),
691 set_from_updated_bmap(map
->p
[j
], tabs
[j
]));
692 wraps
= isl_mat_alloc(map
->ctx
, max_wrap
, 1 + total
);
693 bound
= isl_vec_alloc(map
->ctx
, 1 + total
);
694 if (!set
|| !wraps
|| !bound
)
697 snap_i
= isl_tab_snap(tabs
[i
]);
698 snap_j
= isl_tab_snap(tabs
[j
]);
702 for (k
= 0; k
< n
; ++k
) {
703 tabs
[i
] = isl_tab_select_facet(tabs
[i
],
704 map
->p
[i
]->n_eq
+ cuts
[k
]);
705 if (isl_tab_detect_redundant(tabs
[i
]) < 0)
707 set_is_redundant(tabs
[i
], map
->p
[i
]->n_eq
, cuts
, n
, k
, 1);
709 isl_seq_neg(bound
->el
, map
->p
[i
]->ineq
[cuts
[k
]], 1 + total
);
710 if (add_wraps(wraps
, map
->p
[i
], tabs
[i
], bound
->el
, set
) < 0)
713 set_is_redundant(tabs
[i
], map
->p
[i
]->n_eq
, cuts
, n
, k
, 0);
714 if (isl_tab_rollback(tabs
[i
], snap_i
) < 0)
720 isl_seq_cpy(bound
->el
, map
->p
[i
]->ineq
[cuts
[k
]], 1 + total
);
721 isl_int_add_ui(bound
->el
[0], bound
->el
[0], 1);
722 tabs
[j
] = isl_tab_add_eq(tabs
[j
], bound
->el
);
723 if (isl_tab_detect_redundant(tabs
[j
]) < 0)
726 if (!tabs
[j
]->empty
&&
727 add_wraps(wraps
, map
->p
[j
], tabs
[j
], bound
->el
, set
) < 0)
730 if (isl_tab_rollback(tabs
[j
], snap_j
) < 0)
738 changed
= fuse(map
, i
, j
, tabs
,
739 eq_i
, ineq_i
, eq_j
, ineq_j
, wraps
);
753 /* Given two basic sets i and j such that i has not cut equalities,
754 * check if relaxing all the cut inequalities of i by one turns
755 * them into valid constraint for j and check if we can wrap in
756 * the bits that are sticking out.
757 * If so, replace the pair by their union.
759 * We first check if all relaxed cut inequalities of i are valid for j
760 * and then try to wrap in the intersections of the relaxed cut inequalities
763 * During this wrapping, we consider the points of j that lie at a distance
764 * of exactly 1 from i. In particular, we ignore the points that lie in
765 * between this lower-dimensional space and the basic map i.
766 * We can therefore only apply this to integer maps.
792 * Wrapping can fail if the result of wrapping one of the facets
793 * around its edges does not produce any new facet constraint.
794 * In particular, this happens when we try to wrap in unbounded sets.
796 * _______________________________________________________________________
800 * |_| |_________________________________________________________________
803 * The following is not an acceptable result of coalescing the above two
804 * sets as it includes extra integer points.
805 * _______________________________________________________________________
810 * \______________________________________________________________________
812 static int can_wrap_in_set(struct isl_map
*map
, int i
, int j
,
813 struct isl_tab
**tabs
, int *eq_i
, int *ineq_i
, int *eq_j
, int *ineq_j
)
820 if (ISL_F_ISSET(map
->p
[i
], ISL_BASIC_MAP_RATIONAL
) ||
821 ISL_F_ISSET(map
->p
[j
], ISL_BASIC_MAP_RATIONAL
))
824 n
= count(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_CUT
);
828 cuts
= isl_alloc_array(map
->ctx
, int, n
);
832 for (k
= 0, m
= 0; m
< n
; ++k
) {
833 enum isl_ineq_type type
;
835 if (ineq_i
[k
] != STATUS_CUT
)
838 isl_int_add_ui(map
->p
[i
]->ineq
[k
][0], map
->p
[i
]->ineq
[k
][0], 1);
839 type
= isl_tab_ineq_type(tabs
[j
], map
->p
[i
]->ineq
[k
]);
840 isl_int_sub_ui(map
->p
[i
]->ineq
[k
][0], map
->p
[i
]->ineq
[k
][0], 1);
841 if (type
== isl_ineq_error
)
843 if (type
!= isl_ineq_redundant
)
850 changed
= wrap_in_facets(map
, i
, j
, cuts
, n
, tabs
,
851 eq_i
, ineq_i
, eq_j
, ineq_j
);
861 /* Check if either i or j has a single cut constraint that can
862 * be used to wrap in (a facet of) the other basic set.
863 * if so, replace the pair by their union.
865 static int check_wrap(struct isl_map
*map
, int i
, int j
,
866 struct isl_tab
**tabs
, int *eq_i
, int *ineq_i
, int *eq_j
, int *ineq_j
)
870 if (!any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_CUT
))
871 changed
= can_wrap_in_set(map
, i
, j
, tabs
,
872 eq_i
, ineq_i
, eq_j
, ineq_j
);
876 if (!any(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_CUT
))
877 changed
= can_wrap_in_set(map
, j
, i
, tabs
,
878 eq_j
, ineq_j
, eq_i
, ineq_i
);
882 /* At least one of the basic maps has an equality that is adjacent
883 * to inequality. Make sure that only one of the basic maps has
884 * such an equality and that the other basic map has exactly one
885 * inequality adjacent to an equality.
886 * We call the basic map that has the inequality "i" and the basic
887 * map that has the equality "j".
888 * If "i" has any "cut" (in)equality, then relaxing the inequality
889 * by one would not result in a basic map that contains the other
892 static int check_adj_eq(struct isl_map
*map
, int i
, int j
,
893 struct isl_tab
**tabs
, int *eq_i
, int *ineq_i
, int *eq_j
, int *ineq_j
)
898 if (any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_ADJ_INEQ
) &&
899 any(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_ADJ_INEQ
))
900 /* ADJ EQ TOO MANY */
903 if (any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_ADJ_INEQ
))
904 return check_adj_eq(map
, j
, i
, tabs
,
905 eq_j
, ineq_j
, eq_i
, ineq_i
);
907 /* j has an equality adjacent to an inequality in i */
909 if (any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_CUT
))
911 if (any(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_CUT
))
914 if (count(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_ADJ_INEQ
) != 1 ||
915 count(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_ADJ_EQ
) != 1 ||
916 any(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_ADJ_EQ
) ||
917 any(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_ADJ_INEQ
) ||
918 any(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_ADJ_INEQ
))
919 /* ADJ EQ TOO MANY */
922 for (k
= 0; k
< map
->p
[i
]->n_ineq
; ++k
)
923 if (ineq_i
[k
] == STATUS_ADJ_EQ
)
926 changed
= is_extension(map
, i
, j
, k
, tabs
, eq_i
, ineq_i
, eq_j
, ineq_j
);
930 changed
= can_wrap_in_facet(map
, i
, j
, k
, tabs
, eq_i
, ineq_i
, eq_j
, ineq_j
);
935 /* Check if the union of the given pair of basic maps
936 * can be represented by a single basic map.
937 * If so, replace the pair by the single basic map and return 1.
938 * Otherwise, return 0;
940 * We first check the effect of each constraint of one basic map
941 * on the other basic map.
942 * The constraint may be
943 * redundant the constraint is redundant in its own
944 * basic map and should be ignore and removed
946 * valid all (integer) points of the other basic map
947 * satisfy the constraint
948 * separate no (integer) point of the other basic map
949 * satisfies the constraint
950 * cut some but not all points of the other basic map
951 * satisfy the constraint
952 * adj_eq the given constraint is adjacent (on the outside)
953 * to an equality of the other basic map
954 * adj_ineq the given constraint is adjacent (on the outside)
955 * to an inequality of the other basic map
957 * We consider six cases in which we can replace the pair by a single
958 * basic map. We ignore all "redundant" constraints.
960 * 1. all constraints of one basic map are valid
961 * => the other basic map is a subset and can be removed
963 * 2. all constraints of both basic maps are either "valid" or "cut"
964 * and the facets corresponding to the "cut" constraints
965 * of one of the basic maps lies entirely inside the other basic map
966 * => the pair can be replaced by a basic map consisting
967 * of the valid constraints in both basic maps
969 * 3. there is a single pair of adjacent inequalities
970 * (all other constraints are "valid")
971 * => the pair can be replaced by a basic map consisting
972 * of the valid constraints in both basic maps
974 * 4. there is a single adjacent pair of an inequality and an equality,
975 * the other constraints of the basic map containing the inequality are
976 * "valid". Moreover, if the inequality the basic map is relaxed
977 * and then turned into an equality, then resulting facet lies
978 * entirely inside the other basic map
979 * => the pair can be replaced by the basic map containing
980 * the inequality, with the inequality relaxed.
982 * 5. there is a single adjacent pair of an inequality and an equality,
983 * the other constraints of the basic map containing the inequality are
984 * "valid". Moreover, the facets corresponding to both
985 * the inequality and the equality can be wrapped around their
986 * ridges to include the other basic map
987 * => the pair can be replaced by a basic map consisting
988 * of the valid constraints in both basic maps together
989 * with all wrapping constraints
991 * 6. one of the basic maps extends beyond the other by at most one.
992 * Moreover, the facets corresponding to the cut constraints and
993 * the pieces of the other basic map at offset one from these cut
994 * constraints can be wrapped around their ridges to include
995 * the unione of the two basic maps
996 * => the pair can be replaced by a basic map consisting
997 * of the valid constraints in both basic maps together
998 * with all wrapping constraints
1000 * Throughout the computation, we maintain a collection of tableaus
1001 * corresponding to the basic maps. When the basic maps are dropped
1002 * or combined, the tableaus are modified accordingly.
1004 static int coalesce_pair(struct isl_map
*map
, int i
, int j
,
1005 struct isl_tab
**tabs
)
1013 eq_i
= eq_status_in(map
, i
, j
, tabs
);
1014 if (any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_ERROR
))
1016 if (any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_SEPARATE
))
1019 eq_j
= eq_status_in(map
, j
, i
, tabs
);
1020 if (any(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_ERROR
))
1022 if (any(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_SEPARATE
))
1025 ineq_i
= ineq_status_in(map
, i
, j
, tabs
);
1026 if (any(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_ERROR
))
1028 if (any(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_SEPARATE
))
1031 ineq_j
= ineq_status_in(map
, j
, i
, tabs
);
1032 if (any(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_ERROR
))
1034 if (any(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_SEPARATE
))
1037 if (all(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_VALID
) &&
1038 all(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_VALID
)) {
1041 } else if (all(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_VALID
) &&
1042 all(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_VALID
)) {
1045 } else if (any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_ADJ_EQ
) ||
1046 any(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_ADJ_EQ
)) {
1048 } else if (any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_ADJ_INEQ
) ||
1049 any(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_ADJ_INEQ
)) {
1050 changed
= check_adj_eq(map
, i
, j
, tabs
,
1051 eq_i
, ineq_i
, eq_j
, ineq_j
);
1052 } else if (any(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_ADJ_EQ
) ||
1053 any(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_ADJ_EQ
)) {
1056 } else if (any(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_ADJ_INEQ
) ||
1057 any(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_ADJ_INEQ
)) {
1058 if (!any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_CUT
) &&
1059 !any(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_CUT
))
1060 changed
= check_adj_ineq(map
, i
, j
, tabs
,
1063 if (!any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_CUT
) &&
1064 !any(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_CUT
))
1065 changed
= check_facets(map
, i
, j
, tabs
, ineq_i
, ineq_j
);
1067 changed
= check_wrap(map
, i
, j
, tabs
,
1068 eq_i
, ineq_i
, eq_j
, ineq_j
);
1085 static struct isl_map
*coalesce(struct isl_map
*map
, struct isl_tab
**tabs
)
1089 for (i
= map
->n
- 2; i
>= 0; --i
)
1091 for (j
= i
+ 1; j
< map
->n
; ++j
) {
1093 changed
= coalesce_pair(map
, i
, j
, tabs
);
1105 /* For each pair of basic maps in the map, check if the union of the two
1106 * can be represented by a single basic map.
1107 * If so, replace the pair by the single basic map and start over.
1109 struct isl_map
*isl_map_coalesce(struct isl_map
*map
)
1113 struct isl_tab
**tabs
= NULL
;
1121 map
= isl_map_align_divs(map
);
1123 tabs
= isl_calloc_array(map
->ctx
, struct isl_tab
*, map
->n
);
1128 for (i
= 0; i
< map
->n
; ++i
) {
1129 tabs
[i
] = isl_tab_from_basic_map(map
->p
[i
]);
1132 if (!ISL_F_ISSET(map
->p
[i
], ISL_BASIC_MAP_NO_IMPLICIT
))
1133 tabs
[i
] = isl_tab_detect_implicit_equalities(tabs
[i
]);
1134 if (!ISL_F_ISSET(map
->p
[i
], ISL_BASIC_MAP_NO_REDUNDANT
))
1135 if (isl_tab_detect_redundant(tabs
[i
]) < 0)
1138 for (i
= map
->n
- 1; i
>= 0; --i
)
1142 map
= coalesce(map
, tabs
);
1145 for (i
= 0; i
< map
->n
; ++i
) {
1146 map
->p
[i
] = isl_basic_map_update_from_tab(map
->p
[i
],
1148 map
->p
[i
] = isl_basic_map_finalize(map
->p
[i
]);
1151 ISL_F_SET(map
->p
[i
], ISL_BASIC_MAP_NO_IMPLICIT
);
1152 ISL_F_SET(map
->p
[i
], ISL_BASIC_MAP_NO_REDUNDANT
);
1155 for (i
= 0; i
< n
; ++i
)
1156 isl_tab_free(tabs
[i
]);
1163 for (i
= 0; i
< n
; ++i
)
1164 isl_tab_free(tabs
[i
]);
1169 /* For each pair of basic sets in the set, check if the union of the two
1170 * can be represented by a single basic set.
1171 * If so, replace the pair by the single basic set and start over.
1173 struct isl_set
*isl_set_coalesce(struct isl_set
*set
)
1175 return (struct isl_set
*)isl_map_coalesce((struct isl_map
*)set
);