+/* Set the is_redundant property of the "n" constraints in "cuts",
+ * except "k" to "v".
+ * This is a fairly tricky operation as it bypasses isl_tab.c.
+ * The reason we want to temporarily mark some constraints redundant
+ * is that we want to ignore them in add_wraps.
+ *
+ * Initially all cut constraints are non-redundant, but the
+ * selection of a facet right before the call to this function
+ * may have made some of them redundant.
+ * Likewise, the same constraints are marked non-redundant
+ * in the second call to this function, before they are officially
+ * made non-redundant again in the subsequent rollback.
+ */
+static void set_is_redundant(struct isl_tab *tab, unsigned n_eq,
+ int *cuts, int n, int k, int v)
+{
+ int l;
+
+ for (l = 0; l < n; ++l) {
+ if (l == k)
+ continue;
+ tab->con[n_eq + cuts[l]].is_redundant = v;
+ }
+}
+
+/* Given a pair of basic maps i and j such that j sticks out
+ * of i at n cut constraints, each time by at most one,
+ * try to compute wrapping constraints and replace the two
+ * basic maps by a single basic map.
+ * The other constraints of i are assumed to be valid for j.
+ *
+ * The facets of i corresponding to the cut constraints are
+ * wrapped around their ridges, except those ridges determined
+ * by any of the other cut constraints.
+ * The intersections of cut constraints need to be ignored
+ * as the result of wrapping one cut constraint around another
+ * would result in a constraint cutting the union.
+ * In each case, the facets are wrapped to include the union
+ * of the two basic maps.
+ *
+ * The pieces of j that lie at an offset of exactly one from
+ * one of the cut constraints of i are wrapped around their edges.
+ * Here, there is no need to ignore intersections because we
+ * are wrapping around the union of the two basic maps.
+ *
+ * If any wrapping fails, i.e., if we cannot wrap to touch
+ * the union, then we give up.
+ * Otherwise, the pair of basic maps is replaced by their union.
+ */
+static int wrap_in_facets(struct isl_map *map, int i, int j,
+ int *cuts, int n, struct isl_tab **tabs,
+ int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
+{
+ int changed = 0;
+ struct isl_wraps wraps;
+ isl_mat *mat;
+ isl_set *set = NULL;
+ isl_vec *bound = NULL;
+ unsigned total = isl_basic_map_total_dim(map->p[i]);
+ int max_wrap;
+ int k;
+ struct isl_tab_undo *snap_i, *snap_j;
+
+ if (isl_tab_extend_cons(tabs[j], 1) < 0)
+ goto error;
+
+ max_wrap = 2 * (map->p[i]->n_eq + map->p[j]->n_eq) +
+ map->p[i]->n_ineq + map->p[j]->n_ineq;
+ max_wrap *= n;
+
+ set = isl_set_union(set_from_updated_bmap(map->p[i], tabs[i]),
+ set_from_updated_bmap(map->p[j], tabs[j]));
+ mat = isl_mat_alloc(map->ctx, max_wrap, 1 + total);
+ wraps_init(&wraps, mat, map, i, j, eq_i, ineq_i, eq_j, ineq_j);
+ bound = isl_vec_alloc(map->ctx, 1 + total);
+ if (!set || !wraps.mat || !bound)
+ goto error;
+
+ snap_i = isl_tab_snap(tabs[i]);
+ snap_j = isl_tab_snap(tabs[j]);
+
+ wraps.mat->n_row = 0;
+
+ for (k = 0; k < n; ++k) {
+ if (isl_tab_select_facet(tabs[i], map->p[i]->n_eq + cuts[k]) < 0)
+ goto error;
+ if (isl_tab_detect_redundant(tabs[i]) < 0)
+ goto error;
+ set_is_redundant(tabs[i], map->p[i]->n_eq, cuts, n, k, 1);
+
+ isl_seq_neg(bound->el, map->p[i]->ineq[cuts[k]], 1 + total);
+ if (!tabs[i]->empty &&
+ add_wraps(&wraps, map->p[i], tabs[i], bound->el, set) < 0)
+ goto error;
+
+ set_is_redundant(tabs[i], map->p[i]->n_eq, cuts, n, k, 0);
+ if (isl_tab_rollback(tabs[i], snap_i) < 0)
+ goto error;
+
+ if (tabs[i]->empty)
+ break;
+ if (!wraps.mat->n_row)
+ break;
+
+ isl_seq_cpy(bound->el, map->p[i]->ineq[cuts[k]], 1 + total);
+ isl_int_add_ui(bound->el[0], bound->el[0], 1);
+ if (isl_tab_add_eq(tabs[j], bound->el) < 0)
+ goto error;
+ if (isl_tab_detect_redundant(tabs[j]) < 0)
+ goto error;
+
+ if (!tabs[j]->empty &&
+ add_wraps(&wraps, map->p[j], tabs[j], bound->el, set) < 0)
+ goto error;
+
+ if (isl_tab_rollback(tabs[j], snap_j) < 0)
+ goto error;
+
+ if (!wraps.mat->n_row)
+ break;
+ }
+
+ if (k == n)
+ changed = fuse(map, i, j, tabs,
+ eq_i, ineq_i, eq_j, ineq_j, wraps.mat);
+
+ isl_vec_free(bound);
+ wraps_free(&wraps);
+ isl_set_free(set);
+
+ return changed;
+error:
+ isl_vec_free(bound);
+ wraps_free(&wraps);
+ isl_set_free(set);
+ return -1;
+}
+
+/* Given two basic sets i and j such that i has no cut equalities,
+ * check if relaxing all the cut inequalities of i by one turns
+ * them into valid constraint for j and check if we can wrap in
+ * the bits that are sticking out.