isl_pw_aff_set_rational: avoid invalid access on error
[platform/upstream/isl.git] / isl_tab.c
index 28af80e..6089f3a 100644 (file)
--- a/isl_tab.c
+++ b/isl_tab.c
@@ -1,7 +1,7 @@
 /*
  * Copyright 2008-2009 Katholieke Universiteit Leuven
  *
- * Use of this software is governed by the GNU LGPLv2.1 license
+ * 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
@@ -12,6 +12,7 @@
 #include "isl_map_private.h"
 #include "isl_tab.h"
 #include <isl/seq.h>
+#include <isl_config.h>
 
 /*
  * The implementation of tableaus in this file was inspired by Section 8
@@ -938,7 +939,7 @@ int isl_tab_mark_redundant(struct isl_tab *tab, int row)
        struct isl_tab_var *var = isl_tab_var_from_row(tab, row);
        var->is_redundant = 1;
        isl_assert(tab->mat->ctx, row >= tab->n_redundant, return -1);
-       if (tab->need_undo || tab->row_var[row] >= 0) {
+       if (tab->preserve || tab->need_undo || tab->row_var[row] >= 0) {
                if (tab->row_var[row] >= 0 && !var->is_nonneg) {
                        var->is_nonneg = 1;
                        if (isl_tab_push_var(tab, isl_tab_undo_nonneg, var) < 0)
@@ -1187,6 +1188,11 @@ static int to_row(struct isl_tab *tab, struct isl_tab_var *var, int sign)
        return isl_tab_pivot(tab, r, var->index);
 }
 
+/* Check whether all variables that are marked as non-negative
+ * also have a non-negative sample value.  This function is not
+ * called from the current code but is useful during debugging.
+ */
+static void check_table(struct isl_tab *tab) __attribute__ ((unused));
 static void check_table(struct isl_tab *tab)
 {
        int i;
@@ -2178,8 +2184,8 @@ int isl_tab_add_div(struct isl_tab *tab, __isl_keep isl_vec *div,
        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);
+       tab->bmap = isl_basic_map_extend_space(tab->bmap,
+               isl_basic_map_get_space(tab->bmap), 1, 0, 2);
        k = isl_basic_map_alloc_div(tab->bmap);
        if (k < 0)
                return -1;
@@ -2193,7 +2199,13 @@ int isl_tab_add_div(struct isl_tab *tab, __isl_keep isl_vec *div,
        return r;
 }
 
-struct isl_tab *isl_tab_from_basic_map(struct isl_basic_map *bmap)
+/* If "track" is set, then we want to keep track of all constraints in tab
+ * in its bmap field.  This field is initialized from a copy of "bmap",
+ * so we need to make sure that all constraints in "bmap" also appear
+ * in the constructed tab.
+ */
+__isl_give struct isl_tab *isl_tab_from_basic_map(
+       __isl_keep isl_basic_map *bmap, int track)
 {
        int i;
        struct isl_tab *tab;
@@ -2205,11 +2217,12 @@ struct isl_tab *isl_tab_from_basic_map(struct isl_basic_map *bmap)
                            isl_basic_map_total_dim(bmap), 0);
        if (!tab)
                return NULL;
+       tab->preserve = track;
        tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
        if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)) {
                if (isl_tab_mark_empty(tab) < 0)
                        goto error;
-               return tab;
+               goto done;
        }
        for (i = 0; i < bmap->n_eq; ++i) {
                tab = add_eq(tab, bmap->eq[i]);
@@ -2220,17 +2233,21 @@ struct isl_tab *isl_tab_from_basic_map(struct isl_basic_map *bmap)
                if (isl_tab_add_ineq(tab, bmap->ineq[i]) < 0)
                        goto error;
                if (tab->empty)
-                       return tab;
+                       goto done;
        }
+done:
+       if (track && isl_tab_track_bmap(tab, isl_basic_map_copy(bmap)) < 0)
+               goto error;
        return tab;
 error:
        isl_tab_free(tab);
        return NULL;
 }
 
-struct isl_tab *isl_tab_from_basic_set(struct isl_basic_set *bset)
+__isl_give struct isl_tab *isl_tab_from_basic_set(
+       __isl_keep isl_basic_set *bset, int track)
 {
-       return isl_tab_from_basic_map((struct isl_basic_map *)bset);
+       return isl_tab_from_basic_map(bset, track);
 }
 
 /* Construct a tableau corresponding to the recession cone of "bset".
@@ -2539,6 +2556,13 @@ struct isl_tab *isl_tab_relax(struct isl_tab *tab, int con)
 
        var = &tab->con[con];
 
+       if (var->is_row && (var->index < 0 || var->index < tab->n_redundant))
+               isl_die(tab->mat->ctx, isl_error_invalid,
+                       "cannot relax redundant constraint", goto error);
+       if (!var->is_row && (var->index < 0 || var->index < tab->n_dead))
+               isl_die(tab->mat->ctx, isl_error_invalid,
+                       "cannot relax dead constraint", goto error);
+
        if (!var->is_row && !max_is_manifestly_unbounded(tab, var))
                if (to_row(tab, var, 1) < 0)
                        goto error;
@@ -2582,7 +2606,6 @@ int isl_tab_select_facet(struct isl_tab *tab, int con)
 
 static int may_be_equality(struct isl_tab *tab, int row)
 {
-       unsigned off = 2 + tab->M;
        return tab->rational ? isl_int_is_zero(tab->mat->row[row][1])
                             : isl_int_lt(tab->mat->row[row][1],
                                            tab->mat->row[row][0]);
@@ -2928,7 +2951,7 @@ static int perform_undo_var(struct isl_tab *tab, struct isl_tab_undo *undo) WARN
 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);
-       switch(undo->type) {
+       switch (undo->type) {
        case isl_tab_undo_nonneg:
                var->is_nonneg = 0;
                break;
@@ -2966,6 +2989,10 @@ static int perform_undo_var(struct isl_tab *tab, struct isl_tab_undo *undo)
                break;
        case isl_tab_undo_relax:
                return unrelax(tab, var);
+       default:
+               isl_die(tab->mat->ctx, isl_error_internal,
+                       "perform_undo_var called on invalid undo record",
+                       return -1);
        }
 
        return 0;
@@ -3222,12 +3249,21 @@ error:
 
 int isl_tab_track_bmap(struct isl_tab *tab, __isl_take isl_basic_map *bmap)
 {
+       bmap = isl_basic_map_cow(bmap);
        if (!tab || !bmap)
                goto error;
 
-       isl_assert(tab->mat->ctx, tab->n_eq == bmap->n_eq, return -1);
+       if (tab->empty) {
+               bmap = isl_basic_map_set_to_empty(bmap);
+               if (!bmap)
+                       goto error;
+               tab->bmap = bmap;
+               return 0;
+       }
+
+       isl_assert(tab->mat->ctx, tab->n_eq == bmap->n_eq, goto error);
        isl_assert(tab->mat->ctx,
-                   tab->n_con == bmap->n_eq + bmap->n_ineq, return -1);
+                   tab->n_con == bmap->n_eq + bmap->n_ineq, goto error);
 
        tab->bmap = bmap;
 
@@ -3250,7 +3286,8 @@ __isl_keep isl_basic_set *isl_tab_peek_bset(struct isl_tab *tab)
        return (isl_basic_set *)tab->bmap;
 }
 
-void isl_tab_dump(struct isl_tab *tab, FILE *out, int indent)
+static void isl_tab_print_internal(__isl_keep struct isl_tab *tab,
+       FILE *out, int indent)
 {
        unsigned r, c;
        int i;
@@ -3319,9 +3356,14 @@ void isl_tab_dump(struct isl_tab *tab, FILE *out, int indent)
        tab->mat->n_row = tab->n_row;
        c = tab->mat->n_col;
        tab->mat->n_col = 2 + tab->M + tab->n_col;
-       isl_mat_dump(tab->mat, out, indent);
+       isl_mat_print_internal(tab->mat, out, indent);
        tab->mat->n_row = r;
        tab->mat->n_col = c;
        if (tab->bmap)
                isl_basic_map_print_internal(tab->bmap, out, indent);
 }
+
+void isl_tab_dump(__isl_keep struct isl_tab *tab)
+{
+       isl_tab_print_internal(tab, stderr, 0);
+}