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 if (isl_tab_select_facet(tabs
[i
], n_eq
+ k
) < 0)
275 for (l
= 0; l
< map
->p
[j
]->n_ineq
; ++l
) {
277 if (ineq_j
[l
] != STATUS_CUT
)
279 stat
= status_in(map
->p
[j
]->ineq
[l
], tabs
[i
]);
280 if (stat
!= STATUS_VALID
)
283 if (isl_tab_rollback(tabs
[i
], snap
) < 0)
285 if (l
< map
->p
[j
]->n_ineq
)
289 if (k
< map
->p
[i
]->n_ineq
)
292 return fuse(map
, i
, j
, tabs
, NULL
, ineq_i
, NULL
, ineq_j
, NULL
);
295 /* Both basic maps have at least one inequality with and adjacent
296 * (but opposite) inequality in the other basic map.
297 * Check that there are no cut constraints and that there is only
298 * a single pair of adjacent inequalities.
299 * If so, we can replace the pair by a single basic map described
300 * by all but the pair of adjacent inequalities.
301 * Any additional points introduced lie strictly between the two
302 * adjacent hyperplanes and can therefore be integral.
311 * The test for a single pair of adjancent inequalities is important
312 * for avoiding the combination of two basic maps like the following
322 static int check_adj_ineq(struct isl_map
*map
, int i
, int j
,
323 struct isl_tab
**tabs
, int *ineq_i
, int *ineq_j
)
327 if (any(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_CUT
) ||
328 any(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_CUT
))
331 else if (count(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_ADJ_INEQ
) == 1 &&
332 count(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_ADJ_INEQ
) == 1)
333 changed
= fuse(map
, i
, j
, tabs
, NULL
, ineq_i
, NULL
, ineq_j
, NULL
);
334 /* else ADJ INEQ TOO MANY */
339 /* Check if basic map "i" contains the basic map represented
340 * by the tableau "tab".
342 static int contains(struct isl_map
*map
, int i
, int *ineq_i
,
348 dim
= isl_basic_map_total_dim(map
->p
[i
]);
349 for (k
= 0; k
< map
->p
[i
]->n_eq
; ++k
) {
350 for (l
= 0; l
< 2; ++l
) {
352 isl_seq_neg(map
->p
[i
]->eq
[k
], map
->p
[i
]->eq
[k
], 1+dim
);
353 stat
= status_in(map
->p
[i
]->eq
[k
], tab
);
354 if (stat
!= STATUS_VALID
)
359 for (k
= 0; k
< map
->p
[i
]->n_ineq
; ++k
) {
361 if (ineq_i
[k
] == STATUS_REDUNDANT
)
363 stat
= status_in(map
->p
[i
]->ineq
[k
], tab
);
364 if (stat
!= STATUS_VALID
)
370 /* Basic map "i" has an inequality "k" that is adjacent to some equality
371 * of basic map "j". All the other inequalities are valid for "j".
372 * Check if basic map "j" forms an extension of basic map "i".
374 * In particular, we relax constraint "k", compute the corresponding
375 * facet and check whether it is included in the other basic map.
376 * If so, we know that relaxing the constraint extends the basic
377 * map with exactly the other basic map (we already know that this
378 * other basic map is included in the extension, because there
379 * were no "cut" inequalities in "i") and we can replace the
380 * two basic maps by thie extension.
388 static int is_extension(struct isl_map
*map
, int i
, int j
, int k
,
389 struct isl_tab
**tabs
, int *eq_i
, int *ineq_i
, int *eq_j
, int *ineq_j
)
393 struct isl_tab_undo
*snap
, *snap2
;
394 unsigned n_eq
= map
->p
[i
]->n_eq
;
396 snap
= isl_tab_snap(tabs
[i
]);
397 tabs
[i
] = isl_tab_relax(tabs
[i
], n_eq
+ k
);
398 snap2
= isl_tab_snap(tabs
[i
]);
399 if (isl_tab_select_facet(tabs
[i
], n_eq
+ k
) < 0)
401 super
= contains(map
, j
, ineq_j
, tabs
[i
]);
403 if (isl_tab_rollback(tabs
[i
], snap2
) < 0)
405 map
->p
[i
] = isl_basic_map_cow(map
->p
[i
]);
408 isl_int_add_ui(map
->p
[i
]->ineq
[k
][0], map
->p
[i
]->ineq
[k
][0], 1);
409 ISL_F_SET(map
->p
[i
], ISL_BASIC_MAP_FINAL
);
413 if (isl_tab_rollback(tabs
[i
], snap
) < 0)
419 /* For each non-redundant constraint in "bmap" (as determined by "tab"),
420 * wrap the constraint around "bound" such that it includes the whole
421 * set "set" and append the resulting constraint to "wraps".
422 * "wraps" is assumed to have been pre-allocated to the appropriate size.
423 * wraps->n_row is the number of actual wrapped constraints that have
425 * If any of the wrapping problems results in a constraint that is
426 * identical to "bound", then this means that "set" is unbounded in such
427 * way that no wrapping is possible. If this happens then wraps->n_row
430 static int add_wraps(__isl_keep isl_mat
*wraps
, __isl_keep isl_basic_map
*bmap
,
431 struct isl_tab
*tab
, isl_int
*bound
, __isl_keep isl_set
*set
)
435 unsigned total
= isl_basic_map_total_dim(bmap
);
439 for (l
= 0; l
< bmap
->n_ineq
; ++l
) {
440 if (isl_seq_is_neg(bound
, bmap
->ineq
[l
], 1 + total
))
442 if (isl_seq_eq(bound
, bmap
->ineq
[l
], 1 + total
))
444 if (isl_tab_is_redundant(tab
, bmap
->n_eq
+ l
))
447 isl_seq_cpy(wraps
->row
[w
], bound
, 1 + total
);
448 if (!isl_set_wrap_facet(set
, wraps
->row
[w
], bmap
->ineq
[l
]))
450 if (isl_seq_eq(wraps
->row
[w
], bound
, 1 + total
))
454 for (l
= 0; l
< bmap
->n_eq
; ++l
) {
455 if (isl_seq_is_neg(bound
, bmap
->eq
[l
], 1 + total
))
457 if (isl_seq_eq(bound
, bmap
->eq
[l
], 1 + total
))
460 isl_seq_cpy(wraps
->row
[w
], bound
, 1 + total
);
461 isl_seq_neg(wraps
->row
[w
+ 1], bmap
->eq
[l
], 1 + total
);
462 if (!isl_set_wrap_facet(set
, wraps
->row
[w
], wraps
->row
[w
+ 1]))
464 if (isl_seq_eq(wraps
->row
[w
], bound
, 1 + total
))
468 isl_seq_cpy(wraps
->row
[w
], bound
, 1 + total
);
469 if (!isl_set_wrap_facet(set
, wraps
->row
[w
], bmap
->eq
[l
]))
471 if (isl_seq_eq(wraps
->row
[w
], bound
, 1 + total
))
483 /* Check if the constraints in "wraps" from "first" until the last
484 * are all valid for the basic set represented by "tab".
485 * If not, wraps->n_row is set to zero.
487 static int check_wraps(__isl_keep isl_mat
*wraps
, int first
,
492 for (i
= first
; i
< wraps
->n_row
; ++i
) {
493 enum isl_ineq_type type
;
494 type
= isl_tab_ineq_type(tab
, wraps
->row
[i
]);
495 if (type
== isl_ineq_error
)
497 if (type
== isl_ineq_redundant
)
506 /* Return a set that corresponds to the non-redudant constraints
507 * (as recorded in tab) of bmap.
509 * It's important to remove the redundant constraints as some
510 * of the other constraints may have been modified after the
511 * constraints were marked redundant.
512 * In particular, a constraint may have been relaxed.
513 * Redundant constraints are ignored when a constraint is relaxed
514 * and should therefore continue to be ignored ever after.
515 * Otherwise, the relaxation might be thwarted by some of
518 static __isl_give isl_set
*set_from_updated_bmap(__isl_keep isl_basic_map
*bmap
,
521 bmap
= isl_basic_map_copy(bmap
);
522 bmap
= isl_basic_map_cow(bmap
);
523 bmap
= isl_basic_map_update_from_tab(bmap
, tab
);
524 return isl_set_from_basic_set(isl_basic_map_underlying_set(bmap
));
527 /* Given a basic set i with a constraint k that is adjacent to either the
528 * whole of basic set j or a facet of basic set j, check if we can wrap
529 * both the facet corresponding to k and the facet of j (or the whole of j)
530 * around their ridges to include the other set.
531 * If so, replace the pair of basic sets by their union.
533 * All constraints of i (except k) are assumed to be valid for j.
535 * However, the constraints of j may not be valid for i and so
536 * we have to check that the wrapping constraints for j are valid for i.
538 * In the case where j has a facet adjacent to i, tab[j] is assumed
539 * to have been restricted to this facet, so that the non-redundant
540 * constraints in tab[j] are the ridges of the facet.
541 * Note that for the purpose of wrapping, it does not matter whether
542 * we wrap the ridges of i around the whole of j or just around
543 * the facet since all the other constraints are assumed to be valid for j.
544 * In practice, we wrap to include the whole of j.
553 static int can_wrap_in_facet(struct isl_map
*map
, int i
, int j
, int k
,
554 struct isl_tab
**tabs
, int *eq_i
, int *ineq_i
, int *eq_j
, int *ineq_j
)
557 struct isl_mat
*wraps
= NULL
;
558 struct isl_set
*set_i
= NULL
;
559 struct isl_set
*set_j
= NULL
;
560 struct isl_vec
*bound
= NULL
;
561 unsigned total
= isl_basic_map_total_dim(map
->p
[i
]);
562 struct isl_tab_undo
*snap
;
565 set_i
= set_from_updated_bmap(map
->p
[i
], tabs
[i
]);
566 set_j
= set_from_updated_bmap(map
->p
[j
], tabs
[j
]);
567 wraps
= isl_mat_alloc(map
->ctx
, 2 * (map
->p
[i
]->n_eq
+ map
->p
[j
]->n_eq
) +
568 map
->p
[i
]->n_ineq
+ map
->p
[j
]->n_ineq
,
570 bound
= isl_vec_alloc(map
->ctx
, 1 + total
);
571 if (!set_i
|| !set_j
|| !wraps
|| !bound
)
574 isl_seq_cpy(bound
->el
, map
->p
[i
]->ineq
[k
], 1 + total
);
575 isl_int_add_ui(bound
->el
[0], bound
->el
[0], 1);
577 isl_seq_cpy(wraps
->row
[0], bound
->el
, 1 + total
);
580 if (add_wraps(wraps
, map
->p
[j
], tabs
[j
], bound
->el
, set_i
) < 0)
585 snap
= isl_tab_snap(tabs
[i
]);
587 if (isl_tab_select_facet(tabs
[i
], map
->p
[i
]->n_eq
+ k
) < 0)
589 if (isl_tab_detect_redundant(tabs
[i
]) < 0)
592 isl_seq_neg(bound
->el
, map
->p
[i
]->ineq
[k
], 1 + total
);
595 if (add_wraps(wraps
, map
->p
[i
], tabs
[i
], bound
->el
, set_j
) < 0)
598 if (isl_tab_rollback(tabs
[i
], snap
) < 0)
600 if (check_wraps(wraps
, n
, tabs
[i
]) < 0)
605 changed
= fuse(map
, i
, j
, tabs
, eq_i
, ineq_i
, eq_j
, ineq_j
, wraps
);
624 /* Set the is_redundant property of the "n" constraints in "cuts",
626 * This is a fairly tricky operation as it bypasses isl_tab.c.
627 * The reason we want to temporarily mark some constraints redundant
628 * is that we want to ignore them in add_wraps.
630 * Initially all cut constraints are non-redundant, but the
631 * selection of a facet right before the call to this function
632 * may have made some of them redundant.
633 * Likewise, the same constraints are marked non-redundant
634 * in the second call to this function, before they are officially
635 * made non-redundant again in the subsequent rollback.
637 static void set_is_redundant(struct isl_tab
*tab
, unsigned n_eq
,
638 int *cuts
, int n
, int k
, int v
)
642 for (l
= 0; l
< n
; ++l
) {
645 tab
->con
[n_eq
+ cuts
[l
]].is_redundant
= v
;
649 /* Given a pair of basic maps i and j such that j stick out
650 * of i at n cut constraints, each time by at most one,
651 * try to compute wrapping constraints and replace the two
652 * basic maps by a single basic map.
653 * The other constraints of i are assumed to be valid for j.
655 * The facets of i corresponding to the cut constraints are
656 * wrapped around their ridges, except those ridges determined
657 * by any of the other cut constraints.
658 * The intersections of cut constraints need to be ignored
659 * as the result of wrapping on cur constraint around another
660 * would result in a constraint cutting the union.
661 * In each case, the facets are wrapped to include the union
662 * of the two basic maps.
664 * The pieces of j that lie at an offset of exactly one from
665 * one of the cut constraints of i are wrapped around their edges.
666 * Here, there is no need to ignore intersections because we
667 * are wrapping around the union of the two basic maps.
669 * If any wrapping fails, i.e., if we cannot wrap to touch
670 * the union, then we give up.
671 * Otherwise, the pair of basic maps is replaced by their union.
673 static int wrap_in_facets(struct isl_map
*map
, int i
, int j
,
674 int *cuts
, int n
, struct isl_tab
**tabs
,
675 int *eq_i
, int *ineq_i
, int *eq_j
, int *ineq_j
)
678 isl_mat
*wraps
= NULL
;
680 isl_vec
*bound
= NULL
;
681 unsigned total
= isl_basic_map_total_dim(map
->p
[i
]);
684 struct isl_tab_undo
*snap_i
, *snap_j
;
686 if (isl_tab_extend_cons(tabs
[j
], 1) < 0)
689 max_wrap
= 2 * (map
->p
[i
]->n_eq
+ map
->p
[j
]->n_eq
) +
690 map
->p
[i
]->n_ineq
+ map
->p
[j
]->n_ineq
;
693 set
= isl_set_union(set_from_updated_bmap(map
->p
[i
], tabs
[i
]),
694 set_from_updated_bmap(map
->p
[j
], tabs
[j
]));
695 wraps
= isl_mat_alloc(map
->ctx
, max_wrap
, 1 + total
);
696 bound
= isl_vec_alloc(map
->ctx
, 1 + total
);
697 if (!set
|| !wraps
|| !bound
)
700 snap_i
= isl_tab_snap(tabs
[i
]);
701 snap_j
= isl_tab_snap(tabs
[j
]);
705 for (k
= 0; k
< n
; ++k
) {
706 if (isl_tab_select_facet(tabs
[i
], map
->p
[i
]->n_eq
+ cuts
[k
]) < 0)
708 if (isl_tab_detect_redundant(tabs
[i
]) < 0)
710 set_is_redundant(tabs
[i
], map
->p
[i
]->n_eq
, cuts
, n
, k
, 1);
712 isl_seq_neg(bound
->el
, map
->p
[i
]->ineq
[cuts
[k
]], 1 + total
);
713 if (add_wraps(wraps
, map
->p
[i
], tabs
[i
], bound
->el
, set
) < 0)
716 set_is_redundant(tabs
[i
], map
->p
[i
]->n_eq
, cuts
, n
, k
, 0);
717 if (isl_tab_rollback(tabs
[i
], snap_i
) < 0)
723 isl_seq_cpy(bound
->el
, map
->p
[i
]->ineq
[cuts
[k
]], 1 + total
);
724 isl_int_add_ui(bound
->el
[0], bound
->el
[0], 1);
725 if (isl_tab_add_eq(tabs
[j
], bound
->el
) < 0)
727 if (isl_tab_detect_redundant(tabs
[j
]) < 0)
730 if (!tabs
[j
]->empty
&&
731 add_wraps(wraps
, map
->p
[j
], tabs
[j
], bound
->el
, set
) < 0)
734 if (isl_tab_rollback(tabs
[j
], snap_j
) < 0)
742 changed
= fuse(map
, i
, j
, tabs
,
743 eq_i
, ineq_i
, eq_j
, ineq_j
, wraps
);
757 /* Given two basic sets i and j such that i has not cut equalities,
758 * check if relaxing all the cut inequalities of i by one turns
759 * them into valid constraint for j and check if we can wrap in
760 * the bits that are sticking out.
761 * If so, replace the pair by their union.
763 * We first check if all relaxed cut inequalities of i are valid for j
764 * and then try to wrap in the intersections of the relaxed cut inequalities
767 * During this wrapping, we consider the points of j that lie at a distance
768 * of exactly 1 from i. In particular, we ignore the points that lie in
769 * between this lower-dimensional space and the basic map i.
770 * We can therefore only apply this to integer maps.
796 * Wrapping can fail if the result of wrapping one of the facets
797 * around its edges does not produce any new facet constraint.
798 * In particular, this happens when we try to wrap in unbounded sets.
800 * _______________________________________________________________________
804 * |_| |_________________________________________________________________
807 * The following is not an acceptable result of coalescing the above two
808 * sets as it includes extra integer points.
809 * _______________________________________________________________________
814 * \______________________________________________________________________
816 static int can_wrap_in_set(struct isl_map
*map
, int i
, int j
,
817 struct isl_tab
**tabs
, int *eq_i
, int *ineq_i
, int *eq_j
, int *ineq_j
)
824 if (ISL_F_ISSET(map
->p
[i
], ISL_BASIC_MAP_RATIONAL
) ||
825 ISL_F_ISSET(map
->p
[j
], ISL_BASIC_MAP_RATIONAL
))
828 n
= count(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_CUT
);
832 cuts
= isl_alloc_array(map
->ctx
, int, n
);
836 for (k
= 0, m
= 0; m
< n
; ++k
) {
837 enum isl_ineq_type type
;
839 if (ineq_i
[k
] != STATUS_CUT
)
842 isl_int_add_ui(map
->p
[i
]->ineq
[k
][0], map
->p
[i
]->ineq
[k
][0], 1);
843 type
= isl_tab_ineq_type(tabs
[j
], map
->p
[i
]->ineq
[k
]);
844 isl_int_sub_ui(map
->p
[i
]->ineq
[k
][0], map
->p
[i
]->ineq
[k
][0], 1);
845 if (type
== isl_ineq_error
)
847 if (type
!= isl_ineq_redundant
)
854 changed
= wrap_in_facets(map
, i
, j
, cuts
, n
, tabs
,
855 eq_i
, ineq_i
, eq_j
, ineq_j
);
865 /* Check if either i or j has a single cut constraint that can
866 * be used to wrap in (a facet of) the other basic set.
867 * if so, replace the pair by their union.
869 static int check_wrap(struct isl_map
*map
, int i
, int j
,
870 struct isl_tab
**tabs
, int *eq_i
, int *ineq_i
, int *eq_j
, int *ineq_j
)
874 if (!any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_CUT
))
875 changed
= can_wrap_in_set(map
, i
, j
, tabs
,
876 eq_i
, ineq_i
, eq_j
, ineq_j
);
880 if (!any(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_CUT
))
881 changed
= can_wrap_in_set(map
, j
, i
, tabs
,
882 eq_j
, ineq_j
, eq_i
, ineq_i
);
886 /* At least one of the basic maps has an equality that is adjacent
887 * to inequality. Make sure that only one of the basic maps has
888 * such an equality and that the other basic map has exactly one
889 * inequality adjacent to an equality.
890 * We call the basic map that has the inequality "i" and the basic
891 * map that has the equality "j".
892 * If "i" has any "cut" (in)equality, then relaxing the inequality
893 * by one would not result in a basic map that contains the other
896 static int check_adj_eq(struct isl_map
*map
, int i
, int j
,
897 struct isl_tab
**tabs
, int *eq_i
, int *ineq_i
, int *eq_j
, int *ineq_j
)
902 if (any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_ADJ_INEQ
) &&
903 any(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_ADJ_INEQ
))
904 /* ADJ EQ TOO MANY */
907 if (any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_ADJ_INEQ
))
908 return check_adj_eq(map
, j
, i
, tabs
,
909 eq_j
, ineq_j
, eq_i
, ineq_i
);
911 /* j has an equality adjacent to an inequality in i */
913 if (any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_CUT
))
915 if (any(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_CUT
))
918 if (count(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_ADJ_INEQ
) != 1 ||
919 count(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_ADJ_EQ
) != 1 ||
920 any(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_ADJ_EQ
) ||
921 any(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_ADJ_INEQ
) ||
922 any(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_ADJ_INEQ
))
923 /* ADJ EQ TOO MANY */
926 for (k
= 0; k
< map
->p
[i
]->n_ineq
; ++k
)
927 if (ineq_i
[k
] == STATUS_ADJ_EQ
)
930 changed
= is_extension(map
, i
, j
, k
, tabs
, eq_i
, ineq_i
, eq_j
, ineq_j
);
934 changed
= can_wrap_in_facet(map
, i
, j
, k
, tabs
, eq_i
, ineq_i
, eq_j
, ineq_j
);
939 /* Check if the union of the given pair of basic maps
940 * can be represented by a single basic map.
941 * If so, replace the pair by the single basic map and return 1.
942 * Otherwise, return 0;
944 * We first check the effect of each constraint of one basic map
945 * on the other basic map.
946 * The constraint may be
947 * redundant the constraint is redundant in its own
948 * basic map and should be ignore and removed
950 * valid all (integer) points of the other basic map
951 * satisfy the constraint
952 * separate no (integer) point of the other basic map
953 * satisfies the constraint
954 * cut some but not all points of the other basic map
955 * satisfy the constraint
956 * adj_eq the given constraint is adjacent (on the outside)
957 * to an equality of the other basic map
958 * adj_ineq the given constraint is adjacent (on the outside)
959 * to an inequality of the other basic map
961 * We consider six cases in which we can replace the pair by a single
962 * basic map. We ignore all "redundant" constraints.
964 * 1. all constraints of one basic map are valid
965 * => the other basic map is a subset and can be removed
967 * 2. all constraints of both basic maps are either "valid" or "cut"
968 * and the facets corresponding to the "cut" constraints
969 * of one of the basic maps lies entirely inside the other basic map
970 * => the pair can be replaced by a basic map consisting
971 * of the valid constraints in both basic maps
973 * 3. there is a single pair of adjacent inequalities
974 * (all other constraints are "valid")
975 * => the pair can be replaced by a basic map consisting
976 * of the valid constraints in both basic maps
978 * 4. there is a single adjacent pair of an inequality and an equality,
979 * the other constraints of the basic map containing the inequality are
980 * "valid". Moreover, if the inequality the basic map is relaxed
981 * and then turned into an equality, then resulting facet lies
982 * entirely inside the other basic map
983 * => the pair can be replaced by the basic map containing
984 * the inequality, with the inequality relaxed.
986 * 5. there is a single adjacent pair of an inequality and an equality,
987 * the other constraints of the basic map containing the inequality are
988 * "valid". Moreover, the facets corresponding to both
989 * the inequality and the equality can be wrapped around their
990 * ridges to include the other basic map
991 * => the pair can be replaced by a basic map consisting
992 * of the valid constraints in both basic maps together
993 * with all wrapping constraints
995 * 6. one of the basic maps extends beyond the other by at most one.
996 * Moreover, the facets corresponding to the cut constraints and
997 * the pieces of the other basic map at offset one from these cut
998 * constraints can be wrapped around their ridges to include
999 * the unione of the two basic maps
1000 * => the pair can be replaced by a basic map consisting
1001 * of the valid constraints in both basic maps together
1002 * with all wrapping constraints
1004 * Throughout the computation, we maintain a collection of tableaus
1005 * corresponding to the basic maps. When the basic maps are dropped
1006 * or combined, the tableaus are modified accordingly.
1008 static int coalesce_pair(struct isl_map
*map
, int i
, int j
,
1009 struct isl_tab
**tabs
)
1017 eq_i
= eq_status_in(map
, i
, j
, tabs
);
1018 if (any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_ERROR
))
1020 if (any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_SEPARATE
))
1023 eq_j
= eq_status_in(map
, j
, i
, tabs
);
1024 if (any(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_ERROR
))
1026 if (any(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_SEPARATE
))
1029 ineq_i
= ineq_status_in(map
, i
, j
, tabs
);
1030 if (any(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_ERROR
))
1032 if (any(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_SEPARATE
))
1035 ineq_j
= ineq_status_in(map
, j
, i
, tabs
);
1036 if (any(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_ERROR
))
1038 if (any(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_SEPARATE
))
1041 if (all(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_VALID
) &&
1042 all(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_VALID
)) {
1045 } else if (all(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_VALID
) &&
1046 all(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_VALID
)) {
1049 } else if (any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_ADJ_EQ
) ||
1050 any(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_ADJ_EQ
)) {
1052 } else if (any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_ADJ_INEQ
) ||
1053 any(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_ADJ_INEQ
)) {
1054 changed
= check_adj_eq(map
, i
, j
, tabs
,
1055 eq_i
, ineq_i
, eq_j
, ineq_j
);
1056 } else if (any(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_ADJ_EQ
) ||
1057 any(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_ADJ_EQ
)) {
1060 } else if (any(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_ADJ_INEQ
) ||
1061 any(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_ADJ_INEQ
)) {
1062 if (!any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_CUT
) &&
1063 !any(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_CUT
))
1064 changed
= check_adj_ineq(map
, i
, j
, tabs
,
1067 if (!any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_CUT
) &&
1068 !any(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_CUT
))
1069 changed
= check_facets(map
, i
, j
, tabs
, ineq_i
, ineq_j
);
1071 changed
= check_wrap(map
, i
, j
, tabs
,
1072 eq_i
, ineq_i
, eq_j
, ineq_j
);
1089 static struct isl_map
*coalesce(struct isl_map
*map
, struct isl_tab
**tabs
)
1093 for (i
= map
->n
- 2; i
>= 0; --i
)
1095 for (j
= i
+ 1; j
< map
->n
; ++j
) {
1097 changed
= coalesce_pair(map
, i
, j
, tabs
);
1109 /* For each pair of basic maps in the map, check if the union of the two
1110 * can be represented by a single basic map.
1111 * If so, replace the pair by the single basic map and start over.
1113 struct isl_map
*isl_map_coalesce(struct isl_map
*map
)
1117 struct isl_tab
**tabs
= NULL
;
1125 map
= isl_map_align_divs(map
);
1127 tabs
= isl_calloc_array(map
->ctx
, struct isl_tab
*, map
->n
);
1132 for (i
= 0; i
< map
->n
; ++i
) {
1133 tabs
[i
] = isl_tab_from_basic_map(map
->p
[i
]);
1136 if (!ISL_F_ISSET(map
->p
[i
], ISL_BASIC_MAP_NO_IMPLICIT
))
1137 if (isl_tab_detect_implicit_equalities(tabs
[i
]) < 0)
1139 if (!ISL_F_ISSET(map
->p
[i
], ISL_BASIC_MAP_NO_REDUNDANT
))
1140 if (isl_tab_detect_redundant(tabs
[i
]) < 0)
1143 for (i
= map
->n
- 1; i
>= 0; --i
)
1147 map
= coalesce(map
, tabs
);
1150 for (i
= 0; i
< map
->n
; ++i
) {
1151 map
->p
[i
] = isl_basic_map_update_from_tab(map
->p
[i
],
1153 map
->p
[i
] = isl_basic_map_finalize(map
->p
[i
]);
1156 ISL_F_SET(map
->p
[i
], ISL_BASIC_MAP_NO_IMPLICIT
);
1157 ISL_F_SET(map
->p
[i
], ISL_BASIC_MAP_NO_REDUNDANT
);
1160 for (i
= 0; i
< n
; ++i
)
1161 isl_tab_free(tabs
[i
]);
1168 for (i
= 0; i
< n
; ++i
)
1169 isl_tab_free(tabs
[i
]);
1174 /* For each pair of basic sets in the set, check if the union of the two
1175 * can be represented by a single basic set.
1176 * If so, replace the pair by the single basic set and start over.
1178 struct isl_set
*isl_set_coalesce(struct isl_set
*set
)
1180 return (struct isl_set
*)isl_map_coalesce((struct isl_map
*)set
);