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"
16 #include <isl_mat_private.h>
18 #define STATUS_ERROR -1
19 #define STATUS_REDUNDANT 1
20 #define STATUS_VALID 2
21 #define STATUS_SEPARATE 3
23 #define STATUS_ADJ_EQ 5
24 #define STATUS_ADJ_INEQ 6
26 static int status_in(isl_int
*ineq
, struct isl_tab
*tab
)
28 enum isl_ineq_type type
= isl_tab_ineq_type(tab
, ineq
);
31 case isl_ineq_error
: return STATUS_ERROR
;
32 case isl_ineq_redundant
: return STATUS_VALID
;
33 case isl_ineq_separate
: return STATUS_SEPARATE
;
34 case isl_ineq_cut
: return STATUS_CUT
;
35 case isl_ineq_adj_eq
: return STATUS_ADJ_EQ
;
36 case isl_ineq_adj_ineq
: return STATUS_ADJ_INEQ
;
40 /* Compute the position of the equalities of basic map "i"
41 * with respect to basic map "j".
42 * The resulting array has twice as many entries as the number
43 * of equalities corresponding to the two inequalties to which
44 * each equality corresponds.
46 static int *eq_status_in(struct isl_map
*map
, int i
, int j
,
47 struct isl_tab
**tabs
)
50 int *eq
= isl_calloc_array(map
->ctx
, int, 2 * map
->p
[i
]->n_eq
);
53 dim
= isl_basic_map_total_dim(map
->p
[i
]);
54 for (k
= 0; k
< map
->p
[i
]->n_eq
; ++k
) {
55 for (l
= 0; l
< 2; ++l
) {
56 isl_seq_neg(map
->p
[i
]->eq
[k
], map
->p
[i
]->eq
[k
], 1+dim
);
57 eq
[2 * k
+ l
] = status_in(map
->p
[i
]->eq
[k
], tabs
[j
]);
58 if (eq
[2 * k
+ l
] == STATUS_ERROR
)
61 if (eq
[2 * k
] == STATUS_SEPARATE
||
62 eq
[2 * k
+ 1] == STATUS_SEPARATE
)
72 /* Compute the position of the inequalities of basic map "i"
73 * with respect to basic map "j".
75 static int *ineq_status_in(struct isl_map
*map
, int i
, int j
,
76 struct isl_tab
**tabs
)
79 unsigned n_eq
= map
->p
[i
]->n_eq
;
80 int *ineq
= isl_calloc_array(map
->ctx
, int, map
->p
[i
]->n_ineq
);
82 for (k
= 0; k
< map
->p
[i
]->n_ineq
; ++k
) {
83 if (isl_tab_is_redundant(tabs
[i
], n_eq
+ k
)) {
84 ineq
[k
] = STATUS_REDUNDANT
;
87 ineq
[k
] = status_in(map
->p
[i
]->ineq
[k
], tabs
[j
]);
88 if (ineq
[k
] == STATUS_ERROR
)
90 if (ineq
[k
] == STATUS_SEPARATE
)
100 static int any(int *con
, unsigned len
, int status
)
104 for (i
= 0; i
< len
; ++i
)
105 if (con
[i
] == status
)
110 static int count(int *con
, unsigned len
, int status
)
115 for (i
= 0; i
< len
; ++i
)
116 if (con
[i
] == status
)
121 static int all(int *con
, unsigned len
, int status
)
125 for (i
= 0; i
< len
; ++i
) {
126 if (con
[i
] == STATUS_REDUNDANT
)
128 if (con
[i
] != status
)
134 static void drop(struct isl_map
*map
, int i
, struct isl_tab
**tabs
)
136 isl_basic_map_free(map
->p
[i
]);
137 isl_tab_free(tabs
[i
]);
139 if (i
!= map
->n
- 1) {
140 map
->p
[i
] = map
->p
[map
->n
- 1];
141 tabs
[i
] = tabs
[map
->n
- 1];
143 tabs
[map
->n
- 1] = NULL
;
147 /* Replace the pair of basic maps i and j by the basic map bounded
148 * by the valid constraints in both basic maps and the constraint
149 * in extra (if not NULL).
151 static int fuse(struct isl_map
*map
, int i
, int j
,
152 struct isl_tab
**tabs
, int *eq_i
, int *ineq_i
, int *eq_j
, int *ineq_j
,
153 __isl_keep isl_mat
*extra
)
156 struct isl_basic_map
*fused
= NULL
;
157 struct isl_tab
*fused_tab
= NULL
;
158 unsigned total
= isl_basic_map_total_dim(map
->p
[i
]);
159 unsigned extra_rows
= extra
? extra
->n_row
: 0;
161 fused
= isl_basic_map_alloc_space(isl_space_copy(map
->p
[i
]->dim
),
163 map
->p
[i
]->n_eq
+ map
->p
[j
]->n_eq
,
164 map
->p
[i
]->n_ineq
+ map
->p
[j
]->n_ineq
+ extra_rows
);
168 for (k
= 0; k
< map
->p
[i
]->n_eq
; ++k
) {
169 if (eq_i
&& (eq_i
[2 * k
] != STATUS_VALID
||
170 eq_i
[2 * k
+ 1] != STATUS_VALID
))
172 l
= isl_basic_map_alloc_equality(fused
);
175 isl_seq_cpy(fused
->eq
[l
], map
->p
[i
]->eq
[k
], 1 + total
);
178 for (k
= 0; k
< map
->p
[j
]->n_eq
; ++k
) {
179 if (eq_j
&& (eq_j
[2 * k
] != STATUS_VALID
||
180 eq_j
[2 * k
+ 1] != STATUS_VALID
))
182 l
= isl_basic_map_alloc_equality(fused
);
185 isl_seq_cpy(fused
->eq
[l
], map
->p
[j
]->eq
[k
], 1 + total
);
188 for (k
= 0; k
< map
->p
[i
]->n_ineq
; ++k
) {
189 if (ineq_i
[k
] != STATUS_VALID
)
191 l
= isl_basic_map_alloc_inequality(fused
);
194 isl_seq_cpy(fused
->ineq
[l
], map
->p
[i
]->ineq
[k
], 1 + total
);
197 for (k
= 0; k
< map
->p
[j
]->n_ineq
; ++k
) {
198 if (ineq_j
[k
] != STATUS_VALID
)
200 l
= isl_basic_map_alloc_inequality(fused
);
203 isl_seq_cpy(fused
->ineq
[l
], map
->p
[j
]->ineq
[k
], 1 + total
);
206 for (k
= 0; k
< map
->p
[i
]->n_div
; ++k
) {
207 int l
= isl_basic_map_alloc_div(fused
);
210 isl_seq_cpy(fused
->div
[l
], map
->p
[i
]->div
[k
], 1 + 1 + total
);
213 for (k
= 0; k
< extra_rows
; ++k
) {
214 l
= isl_basic_map_alloc_inequality(fused
);
217 isl_seq_cpy(fused
->ineq
[l
], extra
->row
[k
], 1 + total
);
220 fused
= isl_basic_map_gauss(fused
, NULL
);
221 ISL_F_SET(fused
, ISL_BASIC_MAP_FINAL
);
222 if (ISL_F_ISSET(map
->p
[i
], ISL_BASIC_MAP_RATIONAL
) &&
223 ISL_F_ISSET(map
->p
[j
], ISL_BASIC_MAP_RATIONAL
))
224 ISL_F_SET(fused
, ISL_BASIC_MAP_RATIONAL
);
226 fused_tab
= isl_tab_from_basic_map(fused
, 0);
227 if (isl_tab_detect_redundant(fused_tab
) < 0)
230 isl_basic_map_free(map
->p
[i
]);
232 isl_tab_free(tabs
[i
]);
238 isl_tab_free(fused_tab
);
239 isl_basic_map_free(fused
);
243 /* Given a pair of basic maps i and j such that all constraints are either
244 * "valid" or "cut", check if the facets corresponding to the "cut"
245 * constraints of i lie entirely within basic map j.
246 * If so, replace the pair by the basic map consisting of the valid
247 * constraints in both basic maps.
249 * To see that we are not introducing any extra points, call the
250 * two basic maps A and B and the resulting map U and let x
251 * be an element of U \setminus ( A \cup B ).
252 * Then there is a pair of cut constraints c_1 and c_2 in A and B such that x
253 * violates them. Let X be the intersection of U with the opposites
254 * of these constraints. Then x \in X.
255 * The facet corresponding to c_1 contains the corresponding facet of A.
256 * This facet is entirely contained in B, so c_2 is valid on the facet.
257 * However, since it is also (part of) a facet of X, -c_2 is also valid
258 * on the facet. This means c_2 is saturated on the facet, so c_1 and
259 * c_2 must be opposites of each other, but then x could not violate
262 static int check_facets(struct isl_map
*map
, int i
, int j
,
263 struct isl_tab
**tabs
, int *ineq_i
, int *ineq_j
)
266 struct isl_tab_undo
*snap
;
267 unsigned n_eq
= map
->p
[i
]->n_eq
;
269 snap
= isl_tab_snap(tabs
[i
]);
271 for (k
= 0; k
< map
->p
[i
]->n_ineq
; ++k
) {
272 if (ineq_i
[k
] != STATUS_CUT
)
274 if (isl_tab_select_facet(tabs
[i
], n_eq
+ k
) < 0)
276 for (l
= 0; l
< map
->p
[j
]->n_ineq
; ++l
) {
278 if (ineq_j
[l
] != STATUS_CUT
)
280 stat
= status_in(map
->p
[j
]->ineq
[l
], tabs
[i
]);
281 if (stat
!= STATUS_VALID
)
284 if (isl_tab_rollback(tabs
[i
], snap
) < 0)
286 if (l
< map
->p
[j
]->n_ineq
)
290 if (k
< map
->p
[i
]->n_ineq
)
293 return fuse(map
, i
, j
, tabs
, NULL
, ineq_i
, NULL
, ineq_j
, NULL
);
296 /* Both basic maps have at least one inequality with and adjacent
297 * (but opposite) inequality in the other basic map.
298 * Check that there are no cut constraints and that there is only
299 * a single pair of adjacent inequalities.
300 * If so, we can replace the pair by a single basic map described
301 * by all but the pair of adjacent inequalities.
302 * Any additional points introduced lie strictly between the two
303 * adjacent hyperplanes and can therefore be integral.
312 * The test for a single pair of adjancent inequalities is important
313 * for avoiding the combination of two basic maps like the following
323 static int check_adj_ineq(struct isl_map
*map
, int i
, int j
,
324 struct isl_tab
**tabs
, int *ineq_i
, int *ineq_j
)
328 if (any(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_CUT
) ||
329 any(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_CUT
))
332 else if (count(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_ADJ_INEQ
) == 1 &&
333 count(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_ADJ_INEQ
) == 1)
334 changed
= fuse(map
, i
, j
, tabs
, NULL
, ineq_i
, NULL
, ineq_j
, NULL
);
335 /* else ADJ INEQ TOO MANY */
340 /* Check if basic map "i" contains the basic map represented
341 * by the tableau "tab".
343 static int contains(struct isl_map
*map
, int i
, int *ineq_i
,
349 dim
= isl_basic_map_total_dim(map
->p
[i
]);
350 for (k
= 0; k
< map
->p
[i
]->n_eq
; ++k
) {
351 for (l
= 0; l
< 2; ++l
) {
353 isl_seq_neg(map
->p
[i
]->eq
[k
], map
->p
[i
]->eq
[k
], 1+dim
);
354 stat
= status_in(map
->p
[i
]->eq
[k
], tab
);
355 if (stat
!= STATUS_VALID
)
360 for (k
= 0; k
< map
->p
[i
]->n_ineq
; ++k
) {
362 if (ineq_i
[k
] == STATUS_REDUNDANT
)
364 stat
= status_in(map
->p
[i
]->ineq
[k
], tab
);
365 if (stat
!= STATUS_VALID
)
371 /* Basic map "i" has an inequality "k" that is adjacent to some equality
372 * of basic map "j". All the other inequalities are valid for "j".
373 * Check if basic map "j" forms an extension of basic map "i".
375 * In particular, we relax constraint "k", compute the corresponding
376 * facet and check whether it is included in the other basic map.
377 * If so, we know that relaxing the constraint extends the basic
378 * map with exactly the other basic map (we already know that this
379 * other basic map is included in the extension, because there
380 * were no "cut" inequalities in "i") and we can replace the
381 * two basic maps by thie extension.
389 static int is_extension(struct isl_map
*map
, int i
, int j
, int k
,
390 struct isl_tab
**tabs
, int *eq_i
, int *ineq_i
, int *eq_j
, int *ineq_j
)
394 struct isl_tab_undo
*snap
, *snap2
;
395 unsigned n_eq
= map
->p
[i
]->n_eq
;
397 if (isl_tab_is_equality(tabs
[i
], n_eq
+ k
))
400 snap
= isl_tab_snap(tabs
[i
]);
401 tabs
[i
] = isl_tab_relax(tabs
[i
], n_eq
+ k
);
402 snap2
= isl_tab_snap(tabs
[i
]);
403 if (isl_tab_select_facet(tabs
[i
], n_eq
+ k
) < 0)
405 super
= contains(map
, j
, ineq_j
, tabs
[i
]);
407 if (isl_tab_rollback(tabs
[i
], snap2
) < 0)
409 map
->p
[i
] = isl_basic_map_cow(map
->p
[i
]);
412 isl_int_add_ui(map
->p
[i
]->ineq
[k
][0], map
->p
[i
]->ineq
[k
][0], 1);
413 ISL_F_SET(map
->p
[i
], ISL_BASIC_MAP_FINAL
);
417 if (isl_tab_rollback(tabs
[i
], snap
) < 0)
423 /* For each non-redundant constraint in "bmap" (as determined by "tab"),
424 * wrap the constraint around "bound" such that it includes the whole
425 * set "set" and append the resulting constraint to "wraps".
426 * "wraps" is assumed to have been pre-allocated to the appropriate size.
427 * wraps->n_row is the number of actual wrapped constraints that have
429 * If any of the wrapping problems results in a constraint that is
430 * identical to "bound", then this means that "set" is unbounded in such
431 * way that no wrapping is possible. If this happens then wraps->n_row
434 static int add_wraps(__isl_keep isl_mat
*wraps
, __isl_keep isl_basic_map
*bmap
,
435 struct isl_tab
*tab
, isl_int
*bound
, __isl_keep isl_set
*set
)
439 unsigned total
= isl_basic_map_total_dim(bmap
);
443 for (l
= 0; l
< bmap
->n_ineq
; ++l
) {
444 if (isl_seq_is_neg(bound
, bmap
->ineq
[l
], 1 + total
))
446 if (isl_seq_eq(bound
, bmap
->ineq
[l
], 1 + total
))
448 if (isl_tab_is_redundant(tab
, bmap
->n_eq
+ l
))
451 isl_seq_cpy(wraps
->row
[w
], bound
, 1 + total
);
452 if (!isl_set_wrap_facet(set
, wraps
->row
[w
], bmap
->ineq
[l
]))
454 if (isl_seq_eq(wraps
->row
[w
], bound
, 1 + total
))
458 for (l
= 0; l
< bmap
->n_eq
; ++l
) {
459 if (isl_seq_is_neg(bound
, bmap
->eq
[l
], 1 + total
))
461 if (isl_seq_eq(bound
, bmap
->eq
[l
], 1 + total
))
464 isl_seq_cpy(wraps
->row
[w
], bound
, 1 + total
);
465 isl_seq_neg(wraps
->row
[w
+ 1], bmap
->eq
[l
], 1 + total
);
466 if (!isl_set_wrap_facet(set
, wraps
->row
[w
], wraps
->row
[w
+ 1]))
468 if (isl_seq_eq(wraps
->row
[w
], bound
, 1 + total
))
472 isl_seq_cpy(wraps
->row
[w
], bound
, 1 + total
);
473 if (!isl_set_wrap_facet(set
, wraps
->row
[w
], bmap
->eq
[l
]))
475 if (isl_seq_eq(wraps
->row
[w
], bound
, 1 + total
))
487 /* Check if the constraints in "wraps" from "first" until the last
488 * are all valid for the basic set represented by "tab".
489 * If not, wraps->n_row is set to zero.
491 static int check_wraps(__isl_keep isl_mat
*wraps
, int first
,
496 for (i
= first
; i
< wraps
->n_row
; ++i
) {
497 enum isl_ineq_type type
;
498 type
= isl_tab_ineq_type(tab
, wraps
->row
[i
]);
499 if (type
== isl_ineq_error
)
501 if (type
== isl_ineq_redundant
)
510 /* Return a set that corresponds to the non-redudant constraints
511 * (as recorded in tab) of bmap.
513 * It's important to remove the redundant constraints as some
514 * of the other constraints may have been modified after the
515 * constraints were marked redundant.
516 * In particular, a constraint may have been relaxed.
517 * Redundant constraints are ignored when a constraint is relaxed
518 * and should therefore continue to be ignored ever after.
519 * Otherwise, the relaxation might be thwarted by some of
522 static __isl_give isl_set
*set_from_updated_bmap(__isl_keep isl_basic_map
*bmap
,
525 bmap
= isl_basic_map_copy(bmap
);
526 bmap
= isl_basic_map_cow(bmap
);
527 bmap
= isl_basic_map_update_from_tab(bmap
, tab
);
528 return isl_set_from_basic_set(isl_basic_map_underlying_set(bmap
));
531 /* Given a basic set i with a constraint k that is adjacent to either the
532 * whole of basic set j or a facet of basic set j, check if we can wrap
533 * both the facet corresponding to k and the facet of j (or the whole of j)
534 * around their ridges to include the other set.
535 * If so, replace the pair of basic sets by their union.
537 * All constraints of i (except k) are assumed to be valid for j.
539 * However, the constraints of j may not be valid for i and so
540 * we have to check that the wrapping constraints for j are valid for i.
542 * In the case where j has a facet adjacent to i, tab[j] is assumed
543 * to have been restricted to this facet, so that the non-redundant
544 * constraints in tab[j] are the ridges of the facet.
545 * Note that for the purpose of wrapping, it does not matter whether
546 * we wrap the ridges of i around the whole of j or just around
547 * the facet since all the other constraints are assumed to be valid for j.
548 * In practice, we wrap to include the whole of j.
557 static int can_wrap_in_facet(struct isl_map
*map
, int i
, int j
, int k
,
558 struct isl_tab
**tabs
, int *eq_i
, int *ineq_i
, int *eq_j
, int *ineq_j
)
561 struct isl_mat
*wraps
= NULL
;
562 struct isl_set
*set_i
= NULL
;
563 struct isl_set
*set_j
= NULL
;
564 struct isl_vec
*bound
= NULL
;
565 unsigned total
= isl_basic_map_total_dim(map
->p
[i
]);
566 struct isl_tab_undo
*snap
;
569 set_i
= set_from_updated_bmap(map
->p
[i
], tabs
[i
]);
570 set_j
= set_from_updated_bmap(map
->p
[j
], tabs
[j
]);
571 wraps
= isl_mat_alloc(map
->ctx
, 2 * (map
->p
[i
]->n_eq
+ map
->p
[j
]->n_eq
) +
572 map
->p
[i
]->n_ineq
+ map
->p
[j
]->n_ineq
,
574 bound
= isl_vec_alloc(map
->ctx
, 1 + total
);
575 if (!set_i
|| !set_j
|| !wraps
|| !bound
)
578 isl_seq_cpy(bound
->el
, map
->p
[i
]->ineq
[k
], 1 + total
);
579 isl_int_add_ui(bound
->el
[0], bound
->el
[0], 1);
581 isl_seq_cpy(wraps
->row
[0], bound
->el
, 1 + total
);
584 if (add_wraps(wraps
, map
->p
[j
], tabs
[j
], bound
->el
, set_i
) < 0)
589 snap
= isl_tab_snap(tabs
[i
]);
591 if (isl_tab_select_facet(tabs
[i
], map
->p
[i
]->n_eq
+ k
) < 0)
593 if (isl_tab_detect_redundant(tabs
[i
]) < 0)
596 isl_seq_neg(bound
->el
, map
->p
[i
]->ineq
[k
], 1 + total
);
599 if (add_wraps(wraps
, map
->p
[i
], tabs
[i
], bound
->el
, set_j
) < 0)
602 if (isl_tab_rollback(tabs
[i
], snap
) < 0)
604 if (check_wraps(wraps
, n
, tabs
[i
]) < 0)
609 changed
= fuse(map
, i
, j
, tabs
, eq_i
, ineq_i
, eq_j
, ineq_j
, wraps
);
628 /* Set the is_redundant property of the "n" constraints in "cuts",
630 * This is a fairly tricky operation as it bypasses isl_tab.c.
631 * The reason we want to temporarily mark some constraints redundant
632 * is that we want to ignore them in add_wraps.
634 * Initially all cut constraints are non-redundant, but the
635 * selection of a facet right before the call to this function
636 * may have made some of them redundant.
637 * Likewise, the same constraints are marked non-redundant
638 * in the second call to this function, before they are officially
639 * made non-redundant again in the subsequent rollback.
641 static void set_is_redundant(struct isl_tab
*tab
, unsigned n_eq
,
642 int *cuts
, int n
, int k
, int v
)
646 for (l
= 0; l
< n
; ++l
) {
649 tab
->con
[n_eq
+ cuts
[l
]].is_redundant
= v
;
653 /* Given a pair of basic maps i and j such that j sticks out
654 * of i at n cut constraints, each time by at most one,
655 * try to compute wrapping constraints and replace the two
656 * basic maps by a single basic map.
657 * The other constraints of i are assumed to be valid for j.
659 * The facets of i corresponding to the cut constraints are
660 * wrapped around their ridges, except those ridges determined
661 * by any of the other cut constraints.
662 * The intersections of cut constraints need to be ignored
663 * as the result of wrapping one cut constraint around another
664 * would result in a constraint cutting the union.
665 * In each case, the facets are wrapped to include the union
666 * of the two basic maps.
668 * The pieces of j that lie at an offset of exactly one from
669 * one of the cut constraints of i are wrapped around their edges.
670 * Here, there is no need to ignore intersections because we
671 * are wrapping around the union of the two basic maps.
673 * If any wrapping fails, i.e., if we cannot wrap to touch
674 * the union, then we give up.
675 * Otherwise, the pair of basic maps is replaced by their union.
677 static int wrap_in_facets(struct isl_map
*map
, int i
, int j
,
678 int *cuts
, int n
, struct isl_tab
**tabs
,
679 int *eq_i
, int *ineq_i
, int *eq_j
, int *ineq_j
)
682 isl_mat
*wraps
= NULL
;
684 isl_vec
*bound
= NULL
;
685 unsigned total
= isl_basic_map_total_dim(map
->p
[i
]);
688 struct isl_tab_undo
*snap_i
, *snap_j
;
690 if (isl_tab_extend_cons(tabs
[j
], 1) < 0)
693 max_wrap
= 2 * (map
->p
[i
]->n_eq
+ map
->p
[j
]->n_eq
) +
694 map
->p
[i
]->n_ineq
+ map
->p
[j
]->n_ineq
;
697 set
= isl_set_union(set_from_updated_bmap(map
->p
[i
], tabs
[i
]),
698 set_from_updated_bmap(map
->p
[j
], tabs
[j
]));
699 wraps
= isl_mat_alloc(map
->ctx
, max_wrap
, 1 + total
);
700 bound
= isl_vec_alloc(map
->ctx
, 1 + total
);
701 if (!set
|| !wraps
|| !bound
)
704 snap_i
= isl_tab_snap(tabs
[i
]);
705 snap_j
= isl_tab_snap(tabs
[j
]);
709 for (k
= 0; k
< n
; ++k
) {
710 if (isl_tab_select_facet(tabs
[i
], map
->p
[i
]->n_eq
+ cuts
[k
]) < 0)
712 if (isl_tab_detect_redundant(tabs
[i
]) < 0)
714 set_is_redundant(tabs
[i
], map
->p
[i
]->n_eq
, cuts
, n
, k
, 1);
716 isl_seq_neg(bound
->el
, map
->p
[i
]->ineq
[cuts
[k
]], 1 + total
);
717 if (!tabs
[i
]->empty
&&
718 add_wraps(wraps
, map
->p
[i
], tabs
[i
], bound
->el
, set
) < 0)
721 set_is_redundant(tabs
[i
], map
->p
[i
]->n_eq
, cuts
, n
, k
, 0);
722 if (isl_tab_rollback(tabs
[i
], snap_i
) < 0)
730 isl_seq_cpy(bound
->el
, map
->p
[i
]->ineq
[cuts
[k
]], 1 + total
);
731 isl_int_add_ui(bound
->el
[0], bound
->el
[0], 1);
732 if (isl_tab_add_eq(tabs
[j
], bound
->el
) < 0)
734 if (isl_tab_detect_redundant(tabs
[j
]) < 0)
737 if (!tabs
[j
]->empty
&&
738 add_wraps(wraps
, map
->p
[j
], tabs
[j
], bound
->el
, set
) < 0)
741 if (isl_tab_rollback(tabs
[j
], snap_j
) < 0)
749 changed
= fuse(map
, i
, j
, tabs
,
750 eq_i
, ineq_i
, eq_j
, ineq_j
, wraps
);
764 /* Given two basic sets i and j such that i has no cut equalities,
765 * check if relaxing all the cut inequalities of i by one turns
766 * them into valid constraint for j and check if we can wrap in
767 * the bits that are sticking out.
768 * If so, replace the pair by their union.
770 * We first check if all relaxed cut inequalities of i are valid for j
771 * and then try to wrap in the intersections of the relaxed cut inequalities
774 * During this wrapping, we consider the points of j that lie at a distance
775 * of exactly 1 from i. In particular, we ignore the points that lie in
776 * between this lower-dimensional space and the basic map i.
777 * We can therefore only apply this to integer maps.
803 * Wrapping can fail if the result of wrapping one of the facets
804 * around its edges does not produce any new facet constraint.
805 * In particular, this happens when we try to wrap in unbounded sets.
807 * _______________________________________________________________________
811 * |_| |_________________________________________________________________
814 * The following is not an acceptable result of coalescing the above two
815 * sets as it includes extra integer points.
816 * _______________________________________________________________________
821 * \______________________________________________________________________
823 static int can_wrap_in_set(struct isl_map
*map
, int i
, int j
,
824 struct isl_tab
**tabs
, int *eq_i
, int *ineq_i
, int *eq_j
, int *ineq_j
)
831 if (ISL_F_ISSET(map
->p
[i
], ISL_BASIC_MAP_RATIONAL
) ||
832 ISL_F_ISSET(map
->p
[j
], ISL_BASIC_MAP_RATIONAL
))
835 n
= count(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_CUT
);
839 cuts
= isl_alloc_array(map
->ctx
, int, n
);
843 for (k
= 0, m
= 0; m
< n
; ++k
) {
844 enum isl_ineq_type type
;
846 if (ineq_i
[k
] != STATUS_CUT
)
849 isl_int_add_ui(map
->p
[i
]->ineq
[k
][0], map
->p
[i
]->ineq
[k
][0], 1);
850 type
= isl_tab_ineq_type(tabs
[j
], map
->p
[i
]->ineq
[k
]);
851 isl_int_sub_ui(map
->p
[i
]->ineq
[k
][0], map
->p
[i
]->ineq
[k
][0], 1);
852 if (type
== isl_ineq_error
)
854 if (type
!= isl_ineq_redundant
)
861 changed
= wrap_in_facets(map
, i
, j
, cuts
, n
, tabs
,
862 eq_i
, ineq_i
, eq_j
, ineq_j
);
872 /* Check if either i or j has a single cut constraint that can
873 * be used to wrap in (a facet of) the other basic set.
874 * if so, replace the pair by their union.
876 static int check_wrap(struct isl_map
*map
, int i
, int j
,
877 struct isl_tab
**tabs
, int *eq_i
, int *ineq_i
, int *eq_j
, int *ineq_j
)
881 if (!any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_CUT
))
882 changed
= can_wrap_in_set(map
, i
, j
, tabs
,
883 eq_i
, ineq_i
, eq_j
, ineq_j
);
887 if (!any(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_CUT
))
888 changed
= can_wrap_in_set(map
, j
, i
, tabs
,
889 eq_j
, ineq_j
, eq_i
, ineq_i
);
893 /* At least one of the basic maps has an equality that is adjacent
894 * to inequality. Make sure that only one of the basic maps has
895 * such an equality and that the other basic map has exactly one
896 * inequality adjacent to an equality.
897 * We call the basic map that has the inequality "i" and the basic
898 * map that has the equality "j".
899 * If "i" has any "cut" (in)equality, then relaxing the inequality
900 * by one would not result in a basic map that contains the other
903 static int check_adj_eq(struct isl_map
*map
, int i
, int j
,
904 struct isl_tab
**tabs
, int *eq_i
, int *ineq_i
, int *eq_j
, int *ineq_j
)
909 if (any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_ADJ_INEQ
) &&
910 any(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_ADJ_INEQ
))
911 /* ADJ EQ TOO MANY */
914 if (any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_ADJ_INEQ
))
915 return check_adj_eq(map
, j
, i
, tabs
,
916 eq_j
, ineq_j
, eq_i
, ineq_i
);
918 /* j has an equality adjacent to an inequality in i */
920 if (any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_CUT
))
922 if (any(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_CUT
))
925 if (count(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_ADJ_EQ
) != 1 ||
926 any(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_ADJ_EQ
) ||
927 any(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_ADJ_INEQ
) ||
928 any(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_ADJ_INEQ
))
929 /* ADJ EQ TOO MANY */
932 for (k
= 0; k
< map
->p
[i
]->n_ineq
; ++k
)
933 if (ineq_i
[k
] == STATUS_ADJ_EQ
)
936 changed
= is_extension(map
, i
, j
, k
, tabs
, eq_i
, ineq_i
, eq_j
, ineq_j
);
940 if (count(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_ADJ_INEQ
) != 1)
943 changed
= can_wrap_in_facet(map
, i
, j
, k
, tabs
, eq_i
, ineq_i
, eq_j
, ineq_j
);
948 /* The two basic maps lie on adjacent hyperplanes. In particular,
949 * basic map "i" has an equality that lies parallel to basic map "j".
950 * Check if we can wrap the facets around the parallel hyperplanes
951 * to include the other set.
953 * We perform basically the same operations as can_wrap_in_facet,
954 * except that we don't need to select a facet of one of the sets.
960 * We only allow one equality of "i" to be adjacent to an equality of "j"
961 * to avoid coalescing
963 * [m, n] -> { [x, y] -> [x, 1 + y] : x >= 1 and y >= 1 and
964 * x <= 10 and y <= 10;
965 * [x, y] -> [1 + x, y] : x >= 1 and x <= 20 and
966 * y >= 5 and y <= 15 }
970 * [m, n] -> { [x, y] -> [x2, y2] : x >= 1 and 10y2 <= 20 - x + 10y and
971 * 4y2 >= 5 + 3y and 5y2 <= 15 + 4y and
972 * y2 <= 1 + x + y - x2 and y2 >= y and
973 * y2 >= 1 + x + y - x2 }
975 static int check_eq_adj_eq(struct isl_map
*map
, int i
, int j
,
976 struct isl_tab
**tabs
, int *eq_i
, int *ineq_i
, int *eq_j
, int *ineq_j
)
980 struct isl_mat
*wraps
= NULL
;
981 struct isl_set
*set_i
= NULL
;
982 struct isl_set
*set_j
= NULL
;
983 struct isl_vec
*bound
= NULL
;
984 unsigned total
= isl_basic_map_total_dim(map
->p
[i
]);
986 if (count(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_ADJ_EQ
) != 1)
989 for (k
= 0; k
< 2 * map
->p
[i
]->n_eq
; ++k
)
990 if (eq_i
[k
] == STATUS_ADJ_EQ
)
993 set_i
= set_from_updated_bmap(map
->p
[i
], tabs
[i
]);
994 set_j
= set_from_updated_bmap(map
->p
[j
], tabs
[j
]);
995 wraps
= isl_mat_alloc(map
->ctx
, 2 * (map
->p
[i
]->n_eq
+ map
->p
[j
]->n_eq
) +
996 map
->p
[i
]->n_ineq
+ map
->p
[j
]->n_ineq
,
998 bound
= isl_vec_alloc(map
->ctx
, 1 + total
);
999 if (!set_i
|| !set_j
|| !wraps
|| !bound
)
1003 isl_seq_neg(bound
->el
, map
->p
[i
]->eq
[k
/ 2], 1 + total
);
1005 isl_seq_cpy(bound
->el
, map
->p
[i
]->eq
[k
/ 2], 1 + total
);
1006 isl_int_add_ui(bound
->el
[0], bound
->el
[0], 1);
1008 isl_seq_cpy(wraps
->row
[0], bound
->el
, 1 + total
);
1011 if (add_wraps(wraps
, map
->p
[j
], tabs
[j
], bound
->el
, set_i
) < 0)
1016 isl_int_sub_ui(bound
->el
[0], bound
->el
[0], 1);
1017 isl_seq_neg(bound
->el
, bound
->el
, 1 + total
);
1019 isl_seq_cpy(wraps
->row
[wraps
->n_row
], bound
->el
, 1 + total
);
1022 if (add_wraps(wraps
, map
->p
[i
], tabs
[i
], bound
->el
, set_j
) < 0)
1027 changed
= fuse(map
, i
, j
, tabs
, eq_i
, ineq_i
, eq_j
, ineq_j
, wraps
);
1030 error
: changed
= -1;
1034 isl_mat_free(wraps
);
1035 isl_set_free(set_i
);
1036 isl_set_free(set_j
);
1037 isl_vec_free(bound
);
1042 /* Check if the union of the given pair of basic maps
1043 * can be represented by a single basic map.
1044 * If so, replace the pair by the single basic map and return 1.
1045 * Otherwise, return 0;
1047 * We first check the effect of each constraint of one basic map
1048 * on the other basic map.
1049 * The constraint may be
1050 * redundant the constraint is redundant in its own
1051 * basic map and should be ignore and removed
1053 * valid all (integer) points of the other basic map
1054 * satisfy the constraint
1055 * separate no (integer) point of the other basic map
1056 * satisfies the constraint
1057 * cut some but not all points of the other basic map
1058 * satisfy the constraint
1059 * adj_eq the given constraint is adjacent (on the outside)
1060 * to an equality of the other basic map
1061 * adj_ineq the given constraint is adjacent (on the outside)
1062 * to an inequality of the other basic map
1064 * We consider seven cases in which we can replace the pair by a single
1065 * basic map. We ignore all "redundant" constraints.
1067 * 1. all constraints of one basic map are valid
1068 * => the other basic map is a subset and can be removed
1070 * 2. all constraints of both basic maps are either "valid" or "cut"
1071 * and the facets corresponding to the "cut" constraints
1072 * of one of the basic maps lies entirely inside the other basic map
1073 * => the pair can be replaced by a basic map consisting
1074 * of the valid constraints in both basic maps
1076 * 3. there is a single pair of adjacent inequalities
1077 * (all other constraints are "valid")
1078 * => the pair can be replaced by a basic map consisting
1079 * of the valid constraints in both basic maps
1081 * 4. there is a single adjacent pair of an inequality and an equality,
1082 * the other constraints of the basic map containing the inequality are
1083 * "valid". Moreover, if the inequality the basic map is relaxed
1084 * and then turned into an equality, then resulting facet lies
1085 * entirely inside the other basic map
1086 * => the pair can be replaced by the basic map containing
1087 * the inequality, with the inequality relaxed.
1089 * 5. there is a single adjacent pair of an inequality and an equality,
1090 * the other constraints of the basic map containing the inequality are
1091 * "valid". Moreover, the facets corresponding to both
1092 * the inequality and the equality can be wrapped around their
1093 * ridges to include the other basic map
1094 * => the pair can be replaced by a basic map consisting
1095 * of the valid constraints in both basic maps together
1096 * with all wrapping constraints
1098 * 6. one of the basic maps extends beyond the other by at most one.
1099 * Moreover, the facets corresponding to the cut constraints and
1100 * the pieces of the other basic map at offset one from these cut
1101 * constraints can be wrapped around their ridges to include
1102 * the union of the two basic maps
1103 * => the pair can be replaced by a basic map consisting
1104 * of the valid constraints in both basic maps together
1105 * with all wrapping constraints
1107 * 7. the two basic maps live in adjacent hyperplanes. In principle
1108 * such sets can always be combined through wrapping, but we impose
1109 * that there is only one such pair, to avoid overeager coalescing.
1111 * Throughout the computation, we maintain a collection of tableaus
1112 * corresponding to the basic maps. When the basic maps are dropped
1113 * or combined, the tableaus are modified accordingly.
1115 static int coalesce_pair(struct isl_map
*map
, int i
, int j
,
1116 struct isl_tab
**tabs
)
1124 eq_i
= eq_status_in(map
, i
, j
, tabs
);
1127 if (any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_ERROR
))
1129 if (any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_SEPARATE
))
1132 eq_j
= eq_status_in(map
, j
, i
, tabs
);
1135 if (any(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_ERROR
))
1137 if (any(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_SEPARATE
))
1140 ineq_i
= ineq_status_in(map
, i
, j
, tabs
);
1143 if (any(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_ERROR
))
1145 if (any(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_SEPARATE
))
1148 ineq_j
= ineq_status_in(map
, j
, i
, tabs
);
1151 if (any(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_ERROR
))
1153 if (any(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_SEPARATE
))
1156 if (all(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_VALID
) &&
1157 all(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_VALID
)) {
1160 } else if (all(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_VALID
) &&
1161 all(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_VALID
)) {
1164 } else if (any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_ADJ_EQ
)) {
1165 changed
= check_eq_adj_eq(map
, i
, j
, tabs
,
1166 eq_i
, ineq_i
, eq_j
, ineq_j
);
1167 } else if (any(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_ADJ_EQ
)) {
1168 changed
= check_eq_adj_eq(map
, j
, i
, tabs
,
1169 eq_j
, ineq_j
, eq_i
, ineq_i
);
1170 } else if (any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_ADJ_INEQ
) ||
1171 any(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_ADJ_INEQ
)) {
1172 changed
= check_adj_eq(map
, i
, j
, tabs
,
1173 eq_i
, ineq_i
, eq_j
, ineq_j
);
1174 } else if (any(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_ADJ_EQ
) ||
1175 any(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_ADJ_EQ
)) {
1178 } else if (any(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_ADJ_INEQ
) ||
1179 any(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_ADJ_INEQ
)) {
1180 if (!any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_CUT
) &&
1181 !any(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_CUT
))
1182 changed
= check_adj_ineq(map
, i
, j
, tabs
,
1185 if (!any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_CUT
) &&
1186 !any(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_CUT
))
1187 changed
= check_facets(map
, i
, j
, tabs
, ineq_i
, ineq_j
);
1189 changed
= check_wrap(map
, i
, j
, tabs
,
1190 eq_i
, ineq_i
, eq_j
, ineq_j
);
1207 static struct isl_map
*coalesce(struct isl_map
*map
, struct isl_tab
**tabs
)
1211 for (i
= map
->n
- 2; i
>= 0; --i
)
1213 for (j
= i
+ 1; j
< map
->n
; ++j
) {
1215 changed
= coalesce_pair(map
, i
, j
, tabs
);
1227 /* For each pair of basic maps in the map, check if the union of the two
1228 * can be represented by a single basic map.
1229 * If so, replace the pair by the single basic map and start over.
1231 struct isl_map
*isl_map_coalesce(struct isl_map
*map
)
1235 struct isl_tab
**tabs
= NULL
;
1243 map
= isl_map_align_divs(map
);
1245 tabs
= isl_calloc_array(map
->ctx
, struct isl_tab
*, map
->n
);
1250 for (i
= 0; i
< map
->n
; ++i
) {
1251 tabs
[i
] = isl_tab_from_basic_map(map
->p
[i
], 0);
1254 if (!ISL_F_ISSET(map
->p
[i
], ISL_BASIC_MAP_NO_IMPLICIT
))
1255 if (isl_tab_detect_implicit_equalities(tabs
[i
]) < 0)
1257 if (!ISL_F_ISSET(map
->p
[i
], ISL_BASIC_MAP_NO_REDUNDANT
))
1258 if (isl_tab_detect_redundant(tabs
[i
]) < 0)
1261 for (i
= map
->n
- 1; i
>= 0; --i
)
1265 map
= coalesce(map
, tabs
);
1268 for (i
= 0; i
< map
->n
; ++i
) {
1269 map
->p
[i
] = isl_basic_map_update_from_tab(map
->p
[i
],
1271 map
->p
[i
] = isl_basic_map_finalize(map
->p
[i
]);
1274 ISL_F_SET(map
->p
[i
], ISL_BASIC_MAP_NO_IMPLICIT
);
1275 ISL_F_SET(map
->p
[i
], ISL_BASIC_MAP_NO_REDUNDANT
);
1278 for (i
= 0; i
< n
; ++i
)
1279 isl_tab_free(tabs
[i
]);
1286 for (i
= 0; i
< n
; ++i
)
1287 isl_tab_free(tabs
[i
]);
1293 /* For each pair of basic sets in the set, check if the union of the two
1294 * can be represented by a single basic set.
1295 * If so, replace the pair by the single basic set and start over.
1297 struct isl_set
*isl_set_coalesce(struct isl_set
*set
)
1299 return (struct isl_set
*)isl_map_coalesce((struct isl_map
*)set
);