-#include "isl_set.h"
-#include "isl_map.h"
+/*
+ * Copyright 2008-2009 Katholieke Universiteit Leuven
+ *
+ * Use of this software is governed by the GNU LGPLv2.1 license
+ *
+ * Written by Sven Verdoolaege, K.U.Leuven, Departement
+ * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
+ */
+
+#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)
{
static int add_inequality(struct isl_ctx *ctx,
struct isl_basic_map *bmap, int *pos, PipVector *vec)
{
- int i = isl_basic_map_alloc_inequality(ctx, bmap);
+ unsigned nparam = isl_basic_map_n_param(bmap);
+ unsigned n_in = isl_basic_map_n_in(bmap);
+ unsigned n_out = isl_basic_map_n_out(bmap);
+ unsigned n_div = isl_basic_map_n_div(bmap);
+ int i = isl_basic_map_alloc_inequality(bmap);
if (i < 0)
return -1;
copy_constraint_from(bmap->ineq[i], vec,
- bmap->nparam, bmap->n_in, bmap->n_out, bmap->extra, pos);
+ nparam, n_in, n_out, n_div, pos);
return i;
}
struct isl_basic_map *bmap, int *pos, PipNewparm *p, unsigned div)
{
int i, j;
- unsigned div_pos = 1 + bmap->nparam + bmap->n_in + bmap->n_out + div;
- unsigned total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->extra;
+ unsigned total = isl_basic_map_total_dim(bmap);
+ unsigned div_pos = 1 + total - bmap->n_div + div;
i = add_inequality(ctx, bmap, pos, p->vector);
if (i < 0)
copy_values_from(&bmap->ineq[i][div_pos], &p->deno, 1);
isl_int_neg(bmap->ineq[i][div_pos], bmap->ineq[i][div_pos]);
- j = isl_basic_map_alloc_inequality(ctx, bmap);
+ j = isl_basic_map_alloc_inequality(bmap);
if (j < 0)
return -1;
isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
unsigned var, PipVector *vec)
{
int i;
+ unsigned nparam = isl_basic_map_n_param(bmap);
+ unsigned n_in = isl_basic_map_n_in(bmap);
+ unsigned n_out = isl_basic_map_n_out(bmap);
- isl_assert(ctx, var < bmap->n_out, return -1);
+ isl_assert(ctx, var < n_out, return -1);
- i = isl_basic_map_alloc_equality(ctx, bmap);
+ i = isl_basic_map_alloc_equality(bmap);
if (i < 0)
return -1;
copy_constraint_from(bmap->eq[i], vec,
- bmap->nparam, bmap->n_in, bmap->n_out, bmap->extra, pos);
- isl_int_set_si(bmap->eq[i][1+bmap->nparam+bmap->n_in+var], -1);
+ nparam, n_in, n_out, bmap->extra, pos);
+ isl_int_set_si(bmap->eq[i][1+nparam+n_in+var], -1);
return i;
}
struct isl_basic_map *bmap, int *pos, PipNewparm *p)
{
int i, j;
+ unsigned nparam = isl_basic_map_n_param(bmap);
+ unsigned n_in = isl_basic_map_n_in(bmap);
+ unsigned n_out = isl_basic_map_n_out(bmap);
- i = isl_basic_map_alloc_div(ctx, bmap);
+ i = isl_basic_map_alloc_div(bmap);
if (i < 0)
return -1;
copy_constraint_from(bmap->div[i]+1, p->vector,
- bmap->nparam, bmap->n_in, bmap->n_out, bmap->extra, pos);
+ nparam, n_in, n_out, bmap->extra, pos);
copy_values_from(bmap->div[i], &p->deno, 1);
for (j = 0; j < i; ++j)
if (isl_seq_eq(bmap->div[i], bmap->div[j],
- 1+1+bmap->nparam+bmap->n_in+bmap->n_out+j)) {
- isl_basic_map_free_div(ctx, bmap, 1);
+ 1+1+isl_basic_map_total_dim(bmap)+j)) {
+ isl_basic_map_free_div(bmap, 1);
return j;
}
struct isl_map *map)
{
PipNewparm *p;
- int error;
struct isl_basic_map *bmap = data->bmap;
unsigned old_n_div = bmap->n_div;
+ unsigned nparam = isl_basic_map_n_param(bmap);
+ unsigned n_in = isl_basic_map_n_in(bmap);
+ unsigned n_out = isl_basic_map_n_out(bmap);
if (!map)
goto error;
for (p = q->newparm; p; p = p->next) {
int pos;
- unsigned pip_param = bmap->nparam + bmap->n_in;
+ unsigned pip_param = nparam + n_in;
pos = find_div(data->ctx, bmap, data->pos, p);
if (pos < 0)
}
if (q->condition) {
- int j;
int pos = add_inequality(data->ctx, bmap, data->pos,
q->condition);
if (pos < 0)
goto error;
map = scan_quast_r(data, q->next_then, map);
- if (isl_inequality_negate(data->ctx, bmap, pos))
+ if (isl_inequality_negate(bmap, pos))
goto error;
map = scan_quast_r(data, q->next_else, map);
- if (isl_basic_map_free_inequality(data->ctx, bmap, 1))
+ if (isl_basic_map_free_inequality(bmap, 1))
goto error;
} else if (q->list) {
PipList *l;
/* if bmap->n_out is zero, we are only interested in the domains
* where a solution exists and not in the actual solution
*/
- for (j = 0, l = q->list; j < bmap->n_out && l; ++j, l = l->next)
+ for (j = 0, l = q->list; j < n_out && l; ++j, l = l->next)
if (add_equality(data->ctx, bmap, data->pos, j,
l->vector) < 0)
goto error;
- map = isl_map_add(data->ctx, map,
- isl_basic_map_copy(data->ctx, bmap));
- if (isl_basic_map_free_equality(data->ctx, bmap,
- bmap->n_out))
+ 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(data->ctx,
- isl_basic_map_copy(data->ctx, bmap));
- bset = isl_basic_set_drop_vars(data->ctx, bset,
- bmap->n_in, bmap->n_out);
+ 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->ctx, *data->rest, bset);
+ *data->rest = isl_set_add_basic_set(*data->rest, bset);
}
- if (isl_basic_map_free_inequality(data->ctx, bmap,
- 2*(bmap->n_div - old_n_div)))
+ if (isl_basic_map_free_inequality(bmap, 2*(bmap->n_div - old_n_div)))
goto error;
- if (isl_basic_map_free_div(data->ctx, bmap,
- bmap->n_div - old_n_div))
+ if (isl_basic_map_free_div(bmap, bmap->n_div - old_n_div))
goto error;
return map;
error:
- isl_map_free(data->ctx, map);
+ isl_map_free(map);
return NULL;
}
/*
- * 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_dim_size(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,
+ struct isl_dim *keep_dim,
struct isl_basic_set *context,
struct isl_set **rest)
{
int max_depth;
int n_sol, n_nosol;
struct scan_data data;
- struct isl_map *map;
+ struct isl_map *map = NULL;
+ struct isl_dim *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;
- nparam = context->nparam;
- pip_param = nparam + context->dim;
+ dim = isl_basic_set_n_dim(context);
+ nparam = isl_basic_set_n_param(context);
+ keep = isl_dim_size(keep_dim, isl_dim_out);
+ pip_param = nparam + dim;
max_depth = 0;
n_sol = 0;
nexist -= pip_param-1;
if (rest) {
- *rest = isl_set_alloc(data.ctx, nparam, context->dim, n_nosol,
+ *rest = isl_set_alloc_dim(isl_dim_copy(context->dim), n_nosol,
ISL_MAP_DISJOINT);
if (!*rest)
goto error;
}
- map = isl_map_alloc(data.ctx, nparam, context->dim, keep, n_sol,
+ map = isl_map_alloc_dim(isl_dim_copy(keep_dim), n_sol,
ISL_MAP_DISJOINT);
if (!map)
goto error;
- data.bmap = isl_basic_map_from_basic_set(ctx, context,
- context->dim, 0);
- data.bmap = isl_basic_map_extend(data.ctx, data.bmap,
- nparam, data.bmap->n_in, keep, nexist, keep, max_depth+2*nexist);
+ dims = isl_dim_reverse(isl_dim_copy(context->dim));
+ data.bmap = isl_basic_map_from_basic_set(context, dims);
+ data.bmap = isl_basic_map_extend_dim(data.bmap,
+ keep_dim, nexist, keep, max_depth+2*nexist);
if (!data.bmap)
- goto error;
+ goto error2;
if (data.bmap->extra) {
int i;
}
map = scan_quast_r(&data, q, map);
- map = isl_map_finalize(ctx, map);
+ map = isl_map_finalize(map);
if (!map)
- goto error;
+ goto error2;
if (rest) {
- *rest = isl_set_finalize(ctx, *rest);
+ *rest = isl_set_finalize(*rest);
if (!*rest)
- goto error;
+ goto error2;
}
- isl_basic_map_free(data.ctx, data.bmap);
+ isl_basic_map_free(data.bmap);
if (data.pos)
free(data.pos);
return map;
error:
+ isl_basic_set_free(context);
+ isl_dim_free(keep_dim);
+error2:
if (data.pos)
free(data.pos);
- isl_basic_map_free(data.ctx, data.bmap);
- isl_map_free(ctx, map);
+ isl_basic_map_free(data.bmap);
+ isl_map_free(map);
if (rest) {
- isl_set_free(ctx, *rest);
+ isl_set_free(*rest);
*rest = NULL;
}
return NULL;
unsigned ncol;
PipMatrix *M;
unsigned off;
- unsigned pip_var = bmap->nparam + bmap->n_in + bmap->n_out
- + bmap->n_div - pip_param;
- unsigned pip_dim = pip_var - bmap->n_div;
+ unsigned pip_var = isl_basic_map_total_dim(bmap) - pip_param;
nrow = extra_front + bmap->n_eq + bmap->n_ineq;
ncol = 1 + extra_front + pip_var + pip_param + extra_back + 1;
return M;
}
-static struct isl_map *extremum_on(struct isl_ctx *ctx,
+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)
{
PipOptions *options;
PipQuast *sol;
struct isl_map *map;
+ struct isl_ctx *ctx;
PipMatrix *domain = NULL, *context = NULL;
+ unsigned nparam, n_in, n_out;
+
+ bmap = isl_basic_map_detect_equalities(bmap);
+ if (!bmap || !dom)
+ goto error;
- isl_assert(ctx, bmap->nparam == dom->nparam, goto error);
- isl_assert(ctx, bmap->n_in == dom->dim, goto error);
+ ctx = bmap->ctx;
+ isl_assert(ctx, isl_basic_map_compatible_domain(bmap, dom), goto error);
+ 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, bmap->nparam + bmap->n_in,
- 0, dom->n_div);
+ domain = isl_basic_map_to_pip(bmap, nparam + n_in, 0, dom->n_div);
if (!domain)
goto error;
context = isl_basic_map_to_pip((struct isl_basic_map *)dom, 0, 0, 0);
if (sol) {
struct isl_basic_set *copy;
- copy = isl_basic_set_copy(ctx, dom);
- map = isl_map_from_quast(ctx, sol, bmap->n_out, copy, empty);
+ copy = isl_basic_set_copy(dom);
+ map = isl_map_from_quast(ctx, sol,
+ isl_dim_copy(bmap->dim), copy, empty);
} else {
- map = isl_map_empty(ctx, bmap->nparam, bmap->n_in, bmap->n_out);
+ map = isl_map_empty_like_basic_map(bmap);
if (empty)
*empty = NULL;
}
if (!map)
goto error;
if (map->n == 0 && empty) {
- isl_set_free(ctx, *empty);
- *empty = isl_set_from_basic_set(ctx, dom);
+ isl_set_free(*empty);
+ *empty = isl_set_from_basic_set(dom);
} else
- isl_basic_set_free(ctx, dom);
- isl_basic_map_free(ctx, bmap);
+ isl_basic_set_free(dom);
+ isl_basic_map_free(bmap);
pip_quast_free(sol);
pip_options_free(options);
pip_matrix_free(domain);
if (context)
pip_matrix_free(context);
- isl_basic_map_free(ctx, bmap);
- isl_basic_set_free(ctx, dom);
- return NULL;
-}
-
-struct isl_map *isl_pip_basic_map_lexmax(struct isl_ctx *ctx,
- struct isl_basic_map *bmap, struct isl_basic_set *dom,
- struct isl_set **empty)
-{
- return extremum_on(ctx, bmap, dom, empty, 1);
-}
-
-struct isl_map *isl_pip_basic_map_lexmin(struct isl_ctx *ctx,
- struct isl_basic_map *bmap, struct isl_basic_set *dom,
- struct isl_set **empty)
-{
- return extremum_on(ctx, bmap, dom, empty, 0);
-}
-
-struct isl_map *isl_pip_basic_map_compute_divs(struct isl_ctx *ctx,
- struct isl_basic_map *bmap)
-{
- PipMatrix *domain = NULL, *context = NULL;
- PipOptions *options;
- PipQuast *sol;
- struct isl_map *map;
- struct isl_set *set;
- struct isl_basic_set *dom;
- unsigned n_in;
- unsigned n_out;
-
- if (!bmap)
- goto error;
-
- n_in = bmap->n_in;
- n_out = bmap->n_out;
-
- domain = isl_basic_map_to_pip(bmap, bmap->nparam + n_in + n_out, 0, 0);
- if (!domain)
- goto error;
- context = pip_matrix_alloc(0, bmap->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, bmap->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);
-
- isl_basic_map_free(ctx, bmap);
-
- set = isl_map_domain(ctx, map);
-
- return isl_map_from_set(ctx, set, n_in, n_out);
-error:
- if (domain)
- pip_matrix_free(domain);
- if (context)
- pip_matrix_free(context);
- isl_basic_set_free(ctx, dom);
- isl_basic_map_free(ctx, bmap);
+ isl_basic_map_free(bmap);
+ isl_basic_set_free(dom);
return NULL;
}