isl_basic_set_opt: avoid invalid access on error path
[platform/upstream/isl.git] / isl_map_piplib.c
index 0c9a113..165fdae 100644 (file)
@@ -1,9 +1,20 @@
-#include "isl_set.h"
-#include "isl_map.h"
-#include "isl_seq.h"
+/*
+ * Copyright 2008-2009 Katholieke Universiteit Leuven
+ *
+ * Use of this software is governed by the MIT license
+ *
+ * Written by Sven Verdoolaege, K.U.Leuven, Departement
+ * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
+ */
+
+#include <isl_ctx_private.h>
+#include <isl_map_private.h>
+#include <isl/set.h>
+#include <isl/map.h>
+#include <isl/mat.h>
+#include <isl/seq.h>
 #include "isl_piplib.h"
 #include "isl_map_piplib.h"
-#include "isl_map_private.h"
 
 static void copy_values_from(isl_int *dst, Entier *src, unsigned n)
 {
@@ -221,17 +232,16 @@ static struct isl_map *scan_quast_r(struct scan_data *data, PipQuast *q,
                        if (add_equality(data->ctx, bmap, data->pos, j,
                                                l->vector) < 0)
                                goto error;
-               map = isl_map_add(map, isl_basic_map_copy(bmap));
+               map = isl_map_add_basic_map(map, isl_basic_map_copy(bmap));
                if (isl_basic_map_free_equality(bmap, n_out))
                        goto error;
-       } else if (map->n && data->rest) {
-               /* not interested in rest if no sol */
+       } else if (data->rest) {
                struct isl_basic_set *bset;
                bset = isl_basic_set_from_basic_map(isl_basic_map_copy(bmap));
                bset = isl_basic_set_drop_dims(bset, n_in, n_out);
                if (!bset)
                        goto error;
-               *data->rest = isl_set_add(*data->rest, bset);
+               *data->rest = isl_set_add_basic_set(*data->rest, bset);
        }
 
        if (isl_basic_map_free_inequality(bmap, 2*(bmap->n_div - old_n_div)))
@@ -245,11 +255,12 @@ error:
 }
 
 /*
- * Returns a map with "context" as domain and as range the first
- * "keep" variables in the quast lists.
+ * Returns a map of dimension "keep_dim" with "context" as domain and
+ * as range the first "isl_space_dim(keep_dim, isl_dim_out)" variables
+ * in the quast lists.
  */
 static struct isl_map *isl_map_from_quast(struct isl_ctx *ctx, PipQuast *q,
-               unsigned keep,
+               isl_space *keep_dim,
                struct isl_basic_set *context,
                struct isl_set **rest)
 {
@@ -258,21 +269,23 @@ static struct isl_map *isl_map_from_quast(struct isl_ctx *ctx, PipQuast *q,
        int             max_depth;
        int             n_sol, n_nosol;
        struct scan_data        data;
-       struct isl_map          *map;
-       struct isl_dim          *dims;
+       struct isl_map          *map = NULL;
+       isl_space               *dims;
        unsigned                nparam;
        unsigned                dim;
+       unsigned                keep;
 
        data.ctx = ctx;
        data.rest = rest;
        data.bmap = NULL;
        data.pos = NULL;
 
-       if (!context)
+       if (!context || !keep_dim)
                goto error;
 
        dim = isl_basic_set_n_dim(context);
        nparam = isl_basic_set_n_param(context);
+       keep = isl_space_dim(keep_dim, isl_dim_out);
        pip_param = nparam + dim;
 
        max_depth = 0;
@@ -283,22 +296,22 @@ static struct isl_map *isl_map_from_quast(struct isl_ctx *ctx, PipQuast *q,
        nexist -= pip_param-1;
 
        if (rest) {
-               *rest = isl_set_alloc(data.ctx, nparam, dim, n_nosol,
+               *rest = isl_set_alloc_space(isl_space_copy(context->dim), n_nosol,
                                        ISL_MAP_DISJOINT);
                if (!*rest)
                        goto error;
        }
-       map = isl_map_alloc(data.ctx, nparam, dim, keep, n_sol,
+       map = isl_map_alloc_space(isl_space_copy(keep_dim), n_sol,
                                ISL_MAP_DISJOINT);
        if (!map)
                goto error;
 
-       dims = isl_dim_reverse(isl_dim_copy(context->dim));
+       dims = isl_space_reverse(isl_space_copy(context->dim));
        data.bmap = isl_basic_map_from_basic_set(context, dims);
-       data.bmap = isl_basic_map_extend(data.bmap,
-           nparam, dim, keep, nexist, keep, max_depth+2*nexist);
+       data.bmap = isl_basic_map_extend_space(data.bmap,
+               keep_dim, nexist, keep, max_depth+2*nexist);
        if (!data.bmap)
-               goto error;
+               goto error2;
 
        if (data.bmap->extra) {
                int i;
@@ -312,17 +325,20 @@ static struct isl_map *isl_map_from_quast(struct isl_ctx *ctx, PipQuast *q,
        map = scan_quast_r(&data, q, map);
        map = isl_map_finalize(map);
        if (!map)
-               goto error;
+               goto error2;
        if (rest) {
                *rest = isl_set_finalize(*rest);
                if (!*rest)
-                       goto error;
+                       goto error2;
        }
        isl_basic_map_free(data.bmap);
        if (data.pos)
                free(data.pos);
        return map;
 error:
+       isl_basic_set_free(context);
+       isl_space_free(keep_dim);
+error2:
        if (data.pos)
                free(data.pos);
        isl_basic_map_free(data.bmap);
@@ -382,7 +398,14 @@ PipMatrix *isl_basic_map_to_pip(struct isl_basic_map *bmap, unsigned pip_param,
        return M;
 }
 
-static struct isl_map *extremum_on(
+PipMatrix *isl_basic_set_to_pip(struct isl_basic_set *bset, unsigned pip_param,
+                        unsigned extra_front, unsigned extra_back)
+{
+       return isl_basic_map_to_pip((struct isl_basic_map *)bset,
+                                       pip_param, extra_front, extra_back);
+}
+
+struct isl_map *isl_pip_basic_map_lexopt(
                struct isl_basic_map *bmap, struct isl_basic_set *dom,
                struct isl_set **empty, int max)
 {
@@ -393,6 +416,7 @@ static struct isl_map *extremum_on(
        PipMatrix *domain = NULL, *context = NULL;
        unsigned         nparam, n_in, n_out;
 
+       bmap = isl_basic_map_detect_equalities(bmap);
        if (!bmap || !dom)
                goto error;
 
@@ -419,7 +443,8 @@ static struct isl_map *extremum_on(
        if (sol) {
                struct isl_basic_set *copy;
                copy = isl_basic_set_copy(dom);
-               map = isl_map_from_quast(ctx, sol, n_out, copy, empty);
+               map = isl_map_from_quast(ctx, sol,
+                               isl_space_copy(bmap->dim), copy, empty);
        } else {
                map = isl_map_empty_like_basic_map(bmap);
                if (empty)
@@ -449,76 +474,3 @@ error:
        isl_basic_set_free(dom);
        return NULL;
 }
-
-struct isl_map *isl_pip_basic_map_lexmax(
-               struct isl_basic_map *bmap, struct isl_basic_set *dom,
-               struct isl_set **empty)
-{
-       return extremum_on(bmap, dom, empty, 1);
-}
-
-struct isl_map *isl_pip_basic_map_lexmin(
-               struct isl_basic_map *bmap, struct isl_basic_set *dom,
-               struct isl_set **empty)
-{
-       return extremum_on(bmap, dom, empty, 0);
-}
-
-struct isl_map *isl_pip_basic_map_compute_divs(struct isl_basic_map *bmap)
-{
-       PipMatrix *domain = NULL, *context = NULL;
-       PipOptions      *options;
-       PipQuast        *sol;
-       struct isl_ctx  *ctx;
-       struct isl_dim  *dim;
-       struct isl_map  *map;
-       struct isl_set  *set;
-       struct isl_basic_set    *dom;
-       unsigned         nparam;
-       unsigned         n_in;
-       unsigned         n_out;
-
-       if (!bmap)
-               goto error;
-
-       ctx = bmap->ctx;
-       nparam = isl_basic_map_n_param(bmap);
-       n_in = isl_basic_map_n_in(bmap);
-       n_out = isl_basic_map_n_out(bmap);
-
-       domain = isl_basic_map_to_pip(bmap, nparam + n_in + n_out, 0, 0);
-       if (!domain)
-               goto error;
-       context = pip_matrix_alloc(0, nparam + n_in + n_out + 2);
-       if (!context)
-               goto error;
-
-       options = pip_options_init();
-       options->Simplify = 1;
-       options->Urs_unknowns = -1;
-       options->Urs_parms = -1;
-       sol = pip_solve(domain, context, -1, options);
-
-       dom = isl_basic_set_alloc(ctx, nparam, n_in + n_out, 0, 0, 0);
-       map = isl_map_from_quast(ctx, sol, 0, dom, NULL);
-
-       pip_quast_free(sol);
-       pip_options_free(options);
-       pip_matrix_free(domain);
-       pip_matrix_free(context);
-
-       dim = isl_dim_copy(bmap->dim);
-       isl_basic_map_free(bmap);
-
-       set = isl_map_domain(map);
-
-       return isl_map_from_set(set, dim);
-error:
-       if (domain)
-               pip_matrix_free(domain);
-       if (context)
-               pip_matrix_free(context);
-       isl_basic_set_free(dom);
-       isl_basic_map_free(bmap);
-       return NULL;
-}