+ isl_basic_set_free(hull);
+ return NULL;
+}
+
+/* Detect and make explicit all equalities satisfied by the (integer)
+ * points in bmap.
+ */
+struct isl_basic_map *isl_basic_map_detect_equalities(
+ struct isl_basic_map *bmap)
+{
+ int i, j;
+ struct isl_basic_set *hull = NULL;
+
+ if (!bmap)
+ return NULL;
+ if (bmap->n_ineq == 0)
+ return bmap;
+ if (F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
+ return bmap;
+ if (F_ISSET(bmap, ISL_BASIC_MAP_ALL_EQUALITIES))
+ return bmap;
+ if (F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
+ return isl_basic_map_implicit_equalities(bmap);
+
+ hull = equalities_in_underlying_set(isl_basic_map_copy(bmap));
+ if (!hull)
+ goto error;
+ bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim), 0,
+ hull->n_eq, 0);
+ for (i = 0; i < hull->n_eq; ++i) {
+ j = isl_basic_map_alloc_equality(bmap);
+ if (j < 0)
+ goto error;
+ isl_seq_cpy(bmap->eq[j], hull->eq[i],
+ 1 + isl_basic_set_total_dim(hull));
+ }
+ isl_basic_set_free(hull);
+ F_SET(bmap, ISL_BASIC_MAP_NO_IMPLICIT | ISL_BASIC_MAP_ALL_EQUALITIES);
+ bmap = isl_basic_map_simplify(bmap);
+ return isl_basic_map_finalize(bmap);
+error:
+ isl_basic_set_free(hull);
+ isl_basic_map_free(bmap);
+ return NULL;
+}
+
+/* After computing the rational affine hull (by detecting the implicit
+ * equalities), we compute the additional equalities satisfied by
+ * the integer points (if any) and add the original equalities back in.
+ */
+struct isl_basic_map *isl_basic_map_affine_hull(struct isl_basic_map *bmap)
+{
+ struct isl_basic_set *hull = NULL;
+
+ bmap = isl_basic_map_implicit_equalities(bmap);
+ if (!bmap)
+ return NULL;
+ if (bmap->n_ineq == 0)
+ return bmap;
+
+ if (F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
+ bmap = isl_basic_map_cow(bmap);
+ isl_basic_map_free_inequality(bmap, bmap->n_ineq);
+ return bmap;
+ }
+
+ hull = equalities_in_underlying_set(isl_basic_map_copy(bmap));
+ if (!hull)
+ goto error;
+