isl_stream: accept "@" token
[platform/upstream/isl.git] / isl_tab.c
index b3ebc49..749951c 100644 (file)
--- a/isl_tab.c
+++ b/isl_tab.c
@@ -1,3 +1,12 @@
+/*
+ * 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_mat.h"
 #include "isl_map_private.h"
 #include "isl_tab.h"
@@ -177,7 +186,7 @@ void isl_tab_free(struct isl_tab *tab)
        free_undo(tab);
        isl_mat_free(tab->mat);
        isl_vec_free(tab->dual);
-       isl_basic_set_free(tab->bset);
+       isl_basic_map_free(tab->bmap);
        free(tab->var);
        free(tab->con);
        free(tab->row_var);
@@ -702,7 +711,7 @@ static void find_pivot(struct isl_tab *tab,
  * This means
  *     - it represents an inequality or a variable
  *     - that is the sum of a non-negative sample value and a positive
- *       combination of zero or more non-negative variables.
+ *       combination of zero or more non-negative constraints.
  */
 int isl_tab_row_is_redundant(struct isl_tab *tab, int row)
 {
@@ -720,6 +729,8 @@ int isl_tab_row_is_redundant(struct isl_tab *tab, int row)
        for (i = tab->n_dead; i < tab->n_col; ++i) {
                if (isl_int_is_zero(tab->mat->row[row][off + i]))
                        continue;
+               if (tab->col_var[i] >= 0)
+                       return 0;
                if (isl_int_is_neg(tab->mat->row[row][off + i]))
                        return 0;
                if (!var_from_col(tab, i)->is_nonneg)
@@ -746,6 +757,8 @@ static void swap_rows(struct isl_tab *tab, int row1, int row2)
 }
 
 static int push_union(struct isl_tab *tab,
+       enum isl_tab_undo_type type, union isl_tab_undo_val u) WARN_UNUSED;
+static int push_union(struct isl_tab *tab,
        enum isl_tab_undo_type type, union isl_tab_undo_val u)
 {
        struct isl_tab_undo *undo;
@@ -797,6 +810,13 @@ int isl_tab_push_basis(struct isl_tab *tab)
        return push_union(tab, isl_tab_undo_saved_basis, u);
 }
 
+int isl_tab_push_callback(struct isl_tab *tab, struct isl_tab_callback *callback)
+{
+       union isl_tab_undo_val u;
+       u.callback = callback;
+       return push_union(tab, isl_tab_undo_callback, u);
+}
+
 struct isl_tab *isl_tab_init_samples(struct isl_tab *tab)
 {
        if (!tab)
@@ -914,17 +934,35 @@ int isl_tab_mark_redundant(struct isl_tab *tab, int row)
        }
 }
 
-struct isl_tab *isl_tab_mark_empty(struct isl_tab *tab)
+int isl_tab_mark_empty(struct isl_tab *tab)
 {
        if (!tab)
-               return NULL;
+               return -1;
        if (!tab->empty && tab->need_undo)
-               if (isl_tab_push(tab, isl_tab_undo_empty) < 0) {
-                       isl_tab_free(tab);
-                       return NULL;
-               }
+               if (isl_tab_push(tab, isl_tab_undo_empty) < 0)
+                       return -1;
        tab->empty = 1;
-       return tab;
+       return 0;
+}
+
+int isl_tab_freeze_constraint(struct isl_tab *tab, int con)
+{
+       struct isl_tab_var *var;
+
+       if (!tab)
+               return -1;
+
+       var = &tab->con[con];
+       if (var->frozen)
+               return 0;
+       if (var->index < 0)
+               return 0;
+       var->frozen = 1;
+
+       if (tab->need_undo)
+               return isl_tab_push_var(tab, isl_tab_undo_freeze, var);
+
+       return 0;
 }
 
 /* Update the rows signs after a pivot of "row" and "col", with "row_sgn"
@@ -1099,6 +1137,7 @@ int isl_tab_pivot(struct isl_tab *tab, int row, int col)
  * If sgn = 0, then the variable is unbounded in both directions,
  * and we pivot with any row we can find.
  */
+static int to_row(struct isl_tab *tab, struct isl_tab_var *var, int sign) WARN_UNUSED;
 static int to_row(struct isl_tab *tab, struct isl_tab_var *var, int sign)
 {
        int r;
@@ -1448,6 +1487,7 @@ int isl_tab_kill_col(struct isl_tab *tab, int col)
  * then also be written as the negative sum of non-negative variables
  * and must therefore also be zero.
  */
+static int close_row(struct isl_tab *tab, struct isl_tab_var *var) WARN_UNUSED;
 static int close_row(struct isl_tab *tab, struct isl_tab_var *var)
 {
        int j;
@@ -1620,25 +1660,25 @@ static int drop_col(struct isl_tab *tab, int col)
 /* Add inequality "ineq" and check if it conflicts with the
  * previously added constraints or if it is obviously redundant.
  */
-struct isl_tab *isl_tab_add_ineq(struct isl_tab *tab, isl_int *ineq)
+int isl_tab_add_ineq(struct isl_tab *tab, isl_int *ineq)
 {
        int r;
        int sgn;
        isl_int cst;
 
        if (!tab)
-               return NULL;
-       if (tab->bset) {
-               struct isl_basic_set *bset = tab->bset;
+               return -1;
+       if (tab->bmap) {
+               struct isl_basic_map *bmap = tab->bmap;
 
-               isl_assert(tab->mat->ctx, tab->n_eq == bset->n_eq, goto error);
+               isl_assert(tab->mat->ctx, tab->n_eq == bmap->n_eq, return -1);
                isl_assert(tab->mat->ctx,
-                           tab->n_con == bset->n_eq + bset->n_ineq, goto error);
-               tab->bset = isl_basic_set_add_ineq(tab->bset, ineq);
-               if (isl_tab_push(tab, isl_tab_undo_bset_ineq) < 0)
-                       goto error;
-               if (!tab->bset)
-                       goto error;
+                           tab->n_con == bmap->n_eq + bmap->n_ineq, return -1);
+               tab->bmap = isl_basic_map_add_ineq(tab->bmap, ineq);
+               if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
+                       return -1;
+               if (!tab->bmap)
+                       return -1;
        }
        if (tab->cone) {
                isl_int_init(cst);
@@ -1650,33 +1690,31 @@ struct isl_tab *isl_tab_add_ineq(struct isl_tab *tab, isl_int *ineq)
                isl_int_clear(cst);
        }
        if (r < 0)
-               goto error;
+               return -1;
        tab->con[r].is_nonneg = 1;
        if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
-               goto error;
+               return -1;
        if (isl_tab_row_is_redundant(tab, tab->con[r].index)) {
                if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
-                       goto error;
-               return tab;
+                       return -1;
+               return 0;
        }
 
        sgn = restore_row(tab, &tab->con[r]);
        if (sgn < -1)
-               goto error;
+               return -1;
        if (sgn < 0)
                return isl_tab_mark_empty(tab);
        if (tab->con[r].is_row && isl_tab_row_is_redundant(tab, tab->con[r].index))
                if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
-                       goto error;
-       return tab;
-error:
-       isl_tab_free(tab);
-       return NULL;
+                       return -1;
+       return 0;
 }
 
 /* Pivot a non-negative variable down until it reaches the value zero
  * and then pivot the variable into a column position.
  */
+static int to_col(struct isl_tab *tab, struct isl_tab_var *var) WARN_UNUSED;
 static int to_col(struct isl_tab *tab, struct isl_tab_var *var)
 {
        int i;
@@ -1849,16 +1887,16 @@ struct isl_tab *isl_tab_add_eq(struct isl_tab *tab, isl_int *eq)
                return tab;
        }
 
-       if (tab->bset) {
-               tab->bset = isl_basic_set_add_ineq(tab->bset, eq);
-               if (isl_tab_push(tab, isl_tab_undo_bset_ineq) < 0)
+       if (tab->bmap) {
+               tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
+               if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
                        goto error;
                isl_seq_neg(eq, eq, 1 + tab->n_var);
-               tab->bset = isl_basic_set_add_ineq(tab->bset, eq);
+               tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
                isl_seq_neg(eq, eq, 1 + tab->n_var);
-               if (isl_tab_push(tab, isl_tab_undo_bset_ineq) < 0)
+               if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
                        goto error;
-               if (!tab->bset)
+               if (!tab->bmap)
                        goto error;
                if (add_zero_row(tab) < 0)
                        goto error;
@@ -1877,8 +1915,11 @@ struct isl_tab *isl_tab_add_eq(struct isl_tab *tab, isl_int *eq)
                sgn = sign_of_max(tab, var);
                if (sgn < -1)
                        goto error;
-               if (sgn < 0)
-                       return isl_tab_mark_empty(tab);
+               if (sgn < 0) {
+                       if (isl_tab_mark_empty(tab) < 0)
+                               goto error;
+                       return tab;
+               }
        }
 
        var->is_nonneg = 1;
@@ -1894,6 +1935,150 @@ error:
        return NULL;
 }
 
+/* Construct and return an inequality that expresses an upper bound
+ * on the given div.
+ * In particular, if the div is given by
+ *
+ *     d = floor(e/m)
+ *
+ * then the inequality expresses
+ *
+ *     m d <= e
+ */
+static struct isl_vec *ineq_for_div(struct isl_basic_map *bmap, unsigned div)
+{
+       unsigned total;
+       unsigned div_pos;
+       struct isl_vec *ineq;
+
+       if (!bmap)
+               return NULL;
+
+       total = isl_basic_map_total_dim(bmap);
+       div_pos = 1 + total - bmap->n_div + div;
+
+       ineq = isl_vec_alloc(bmap->ctx, 1 + total);
+       if (!ineq)
+               return NULL;
+
+       isl_seq_cpy(ineq->el, bmap->div[div] + 1, 1 + total);
+       isl_int_neg(ineq->el[div_pos], bmap->div[div][0]);
+       return ineq;
+}
+
+/* For a div d = floor(f/m), add the constraints
+ *
+ *             f - m d >= 0
+ *             -(f-(m-1)) + m d >= 0
+ *
+ * Note that the second constraint is the negation of
+ *
+ *             f - m d >= m
+ *
+ * If add_ineq is not NULL, then this function is used
+ * instead of isl_tab_add_ineq to effectively add the inequalities.
+ */
+static int add_div_constraints(struct isl_tab *tab, unsigned div,
+       int (*add_ineq)(void *user, isl_int *), void *user)
+{
+       unsigned total;
+       unsigned div_pos;
+       struct isl_vec *ineq;
+
+       total = isl_basic_map_total_dim(tab->bmap);
+       div_pos = 1 + total - tab->bmap->n_div + div;
+
+       ineq = ineq_for_div(tab->bmap, div);
+       if (!ineq)
+               goto error;
+
+       if (add_ineq) {
+               if (add_ineq(user, ineq->el) < 0)
+                       goto error;
+       } else {
+               if (isl_tab_add_ineq(tab, ineq->el) < 0)
+                       goto error;
+       }
+
+       isl_seq_neg(ineq->el, tab->bmap->div[div] + 1, 1 + total);
+       isl_int_set(ineq->el[div_pos], tab->bmap->div[div][0]);
+       isl_int_add(ineq->el[0], ineq->el[0], ineq->el[div_pos]);
+       isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
+
+       if (add_ineq) {
+               if (add_ineq(user, ineq->el) < 0)
+                       goto error;
+       } else {
+               if (isl_tab_add_ineq(tab, ineq->el) < 0)
+                       goto error;
+       }
+
+       isl_vec_free(ineq);
+
+       return 0;
+error:
+       isl_vec_free(ineq);
+       return -1;
+}
+
+/* Add an extra div, prescrived by "div" to the tableau and
+ * the associated bmap (which is assumed to be non-NULL).
+ *
+ * If add_ineq is not NULL, then this function is used instead
+ * of isl_tab_add_ineq to add the div constraints.
+ * This complication is needed because the code in isl_tab_pip
+ * wants to perform some extra processing when an inequality
+ * is added to the tableau.
+ */
+int isl_tab_add_div(struct isl_tab *tab, __isl_keep isl_vec *div,
+       int (*add_ineq)(void *user, isl_int *), void *user)
+{
+       int i;
+       int r;
+       int k;
+       int nonneg;
+
+       if (!tab || !div)
+               return -1;
+
+       isl_assert(tab->mat->ctx, tab->bmap, return -1);
+
+       for (i = 0; i < tab->n_var; ++i) {
+               if (isl_int_is_neg(div->el[2 + i]))
+                       break;
+               if (isl_int_is_zero(div->el[2 + i]))
+                       continue;
+               if (!tab->var[i].is_nonneg)
+                       break;
+       }
+       nonneg = i == tab->n_var && !isl_int_is_neg(div->el[1]);
+
+       if (isl_tab_extend_cons(tab, 3) < 0)
+               return -1;
+       if (isl_tab_extend_vars(tab, 1) < 0)
+               return -1;
+       r = isl_tab_allocate_var(tab);
+       if (r < 0)
+               return -1;
+
+       if (nonneg)
+               tab->var[r].is_nonneg = 1;
+
+       tab->bmap = isl_basic_map_extend_dim(tab->bmap,
+               isl_basic_map_get_dim(tab->bmap), 1, 0, 2);
+       k = isl_basic_map_alloc_div(tab->bmap);
+       if (k < 0)
+               return -1;
+       isl_seq_cpy(tab->bmap->div[k], div->el, div->size);
+       if (isl_tab_push(tab, isl_tab_undo_bmap_div) < 0)
+               return -1;
+
+       if (add_div_constraints(tab, k, add_ineq, user) < 0)
+               return -1;
+
+       return r;
+}
+
 struct isl_tab *isl_tab_from_basic_map(struct isl_basic_map *bmap)
 {
        int i;
@@ -1907,19 +2092,26 @@ struct isl_tab *isl_tab_from_basic_map(struct isl_basic_map *bmap)
        if (!tab)
                return NULL;
        tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
-       if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
-               return isl_tab_mark_empty(tab);
+       if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)) {
+               if (isl_tab_mark_empty(tab) < 0)
+                       goto error;
+               return tab;
+       }
        for (i = 0; i < bmap->n_eq; ++i) {
                tab = add_eq(tab, bmap->eq[i]);
                if (!tab)
                        return tab;
        }
        for (i = 0; i < bmap->n_ineq; ++i) {
-               tab = isl_tab_add_ineq(tab, bmap->ineq[i]);
-               if (!tab || tab->empty)
+               if (isl_tab_add_ineq(tab, bmap->ineq[i]) < 0)
+                       goto error;
+               if (tab->empty)
                        return tab;
        }
        return tab;
+error:
+       isl_tab_free(tab);
+       return NULL;
 }
 
 struct isl_tab *isl_tab_from_basic_set(struct isl_basic_set *bset)
@@ -2115,6 +2307,8 @@ struct isl_basic_map *isl_basic_map_update_from_tab(struct isl_basic_map *bmap,
                        else if (isl_tab_is_redundant(tab, n_eq + i))
                                isl_basic_map_drop_inequality(bmap, i);
                }
+       if (bmap->n_eq != n_eq)
+               isl_basic_map_gauss(bmap, NULL);
        if (!tab->rational &&
            !bmap->sample && isl_tab_sample_is_integer(tab))
                bmap->sample = extract_integer_sample(tab);
@@ -2181,8 +2375,11 @@ static struct isl_tab *cut_to_hyperplane(struct isl_tab *tab,
        sgn = sign_of_max(tab, &tab->con[r]);
        if (sgn < -1)
                goto error;
-       if (sgn < 0)
-               return isl_tab_mark_empty(tab);
+       if (sgn < 0) {
+               if (isl_tab_mark_empty(tab) < 0)
+                       goto error;
+               return tab;
+       }
        tab->con[r].is_nonneg = 1;
        if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
                goto error;
@@ -2379,17 +2576,17 @@ static int con_is_redundant(struct isl_tab *tab, struct isl_tab_var *var)
  * If not, we mark the row as being redundant (assuming it hasn't
  * been detected as being obviously redundant in the mean time).
  */
-struct isl_tab *isl_tab_detect_redundant(struct isl_tab *tab)
+int isl_tab_detect_redundant(struct isl_tab *tab)
 {
        int i;
        unsigned n_marked;
 
        if (!tab)
-               return NULL;
+               return -1;
        if (tab->empty)
-               return tab;
+               return 0;
        if (tab->n_redundant == tab->n_row)
-               return tab;
+               return 0;
 
        n_marked = 0;
        for (i = tab->n_redundant; i < tab->n_row; ++i) {
@@ -2426,10 +2623,10 @@ struct isl_tab *isl_tab_detect_redundant(struct isl_tab *tab)
                n_marked--;
                red = con_is_redundant(tab, var);
                if (red < 0)
-                       goto error;
+                       return -1;
                if (red && !var->is_redundant)
                        if (isl_tab_mark_redundant(tab, var->index) < 0)
-                               goto error;
+                               return -1;
                for (i = tab->n_dead; i < tab->n_col; ++i) {
                        var = var_from_col(tab, i);
                        if (!var->marked)
@@ -2441,10 +2638,7 @@ struct isl_tab *isl_tab_detect_redundant(struct isl_tab *tab)
                }
        }
 
-       return tab;
-error:
-       isl_tab_free(tab);
-       return NULL;
+       return 0;
 }
 
 int isl_tab_is_equality(struct isl_tab *tab, int con)
@@ -2567,6 +2761,7 @@ struct isl_tab_undo *isl_tab_snap(struct isl_tab *tab)
 
 /* Undo the operation performed by isl_tab_relax.
  */
+static int unrelax(struct isl_tab *tab, struct isl_tab_var *var) WARN_UNUSED;
 static int unrelax(struct isl_tab *tab, struct isl_tab_var *var)
 {
        unsigned off = 2 + tab->M;
@@ -2593,6 +2788,7 @@ static int unrelax(struct isl_tab *tab, struct isl_tab_var *var)
        return 0;
 }
 
+static int perform_undo_var(struct isl_tab *tab, struct isl_tab_undo *undo) WARN_UNUSED;
 static int perform_undo_var(struct isl_tab *tab, struct isl_tab_undo *undo)
 {
        struct isl_tab_var *var = var_from_index(tab, undo->u.var_index);
@@ -2604,6 +2800,9 @@ static int perform_undo_var(struct isl_tab *tab, struct isl_tab_undo *undo)
                var->is_redundant = 0;
                tab->n_redundant--;
                break;
+       case isl_tab_undo_freeze:
+               var->frozen = 0;
+               break;
        case isl_tab_undo_zero:
                var->is_zero = 0;
                if (!var->is_row)
@@ -2715,6 +2914,7 @@ static void drop_samples_since(struct isl_tab *tab, int n)
        }
 }
 
+static int perform_undo(struct isl_tab *tab, struct isl_tab_undo *undo) WARN_UNUSED;
 static int perform_undo(struct isl_tab *tab, struct isl_tab_undo *undo)
 {
        switch (undo->type) {
@@ -2723,16 +2923,17 @@ static int perform_undo(struct isl_tab *tab, struct isl_tab_undo *undo)
                break;
        case isl_tab_undo_nonneg:
        case isl_tab_undo_redundant:
+       case isl_tab_undo_freeze:
        case isl_tab_undo_zero:
        case isl_tab_undo_allocate:
        case isl_tab_undo_relax:
                return perform_undo_var(tab, undo);
-       case isl_tab_undo_bset_eq:
-               return isl_basic_set_free_equality(tab->bset, 1);
-       case isl_tab_undo_bset_ineq:
-               return isl_basic_set_free_inequality(tab->bset, 1);
-       case isl_tab_undo_bset_div:
-               if (isl_basic_set_free_div(tab->bset, 1) < 0)
+       case isl_tab_undo_bmap_eq:
+               return isl_basic_map_free_equality(tab->bmap, 1);
+       case isl_tab_undo_bmap_ineq:
+               return isl_basic_map_free_inequality(tab->bmap, 1);
+       case isl_tab_undo_bmap_div:
+               if (isl_basic_map_free_div(tab->bmap, 1) < 0)
                        return -1;
                if (tab->samples)
                        tab->samples->n_col--;
@@ -2747,6 +2948,8 @@ static int perform_undo(struct isl_tab *tab, struct isl_tab_undo *undo)
        case isl_tab_undo_saved_samples:
                drop_samples_since(tab, undo->u.n);
                break;
+       case isl_tab_undo_callback:
+               return undo->u.callback->run(undo->u.callback);
        default:
                isl_assert(tab->mat->ctx, 0, return -1);
        }
@@ -2878,6 +3081,36 @@ error:
        return isl_ineq_error;
 }
 
+int isl_tab_track_bmap(struct isl_tab *tab, __isl_take isl_basic_map *bmap)
+{
+       if (!tab || !bmap)
+               goto error;
+
+       isl_assert(tab->mat->ctx, tab->n_eq == bmap->n_eq, return -1);
+       isl_assert(tab->mat->ctx,
+                   tab->n_con == bmap->n_eq + bmap->n_ineq, return -1);
+
+       tab->bmap = bmap;
+
+       return 0;
+error:
+       isl_basic_map_free(bmap);
+       return -1;
+}
+
+int isl_tab_track_bset(struct isl_tab *tab, __isl_take isl_basic_set *bset)
+{
+       return isl_tab_track_bmap(tab, (isl_basic_map *)bset);
+}
+
+__isl_keep isl_basic_set *isl_tab_peek_bset(struct isl_tab *tab)
+{
+       if (!tab)
+               return NULL;
+
+       return (isl_basic_set *)tab->bmap;
+}
+
 void isl_tab_dump(struct isl_tab *tab, FILE *out, int indent)
 {
        unsigned r, c;
@@ -2950,6 +3183,6 @@ void isl_tab_dump(struct isl_tab *tab, FILE *out, int indent)
        isl_mat_dump(tab->mat, out, indent);
        tab->mat->n_row = r;
        tab->mat->n_col = c;
-       if (tab->bset)
-               isl_basic_set_dump(tab->bset, out, indent);
+       if (tab->bmap)
+               isl_basic_map_dump(tab->bmap, out, indent);
 }