plug memory leak in isl_mat_product
[platform/upstream/isl.git] / isl_tab_pip.c
index a2cf3a2..b63d3b7 100644 (file)
@@ -10,6 +10,7 @@
  * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France 
  */
 
+#include <isl_ctx_private.h>
 #include "isl_map_private.h"
 #include <isl/seq.h>
 #include "isl_tab.h"
@@ -39,7 +40,7 @@
  * then the initial sample value may be chosen equal to zero.
  * However, we will not make this assumption.  Instead, we apply
  * the "big parameter" trick.  Any variable x is then not directly
- * used in the tableau, but instead it its represented by another
+ * used in the tableau, but instead it is represented by another
  * variable x' = M + x, where M is an arbitrarily large (positive)
  * value.  x' is therefore always non-negative, whatever the value of x.
  * Taking as initial sample value x' = 0 corresponds to x = -M,
@@ -2257,11 +2258,25 @@ static int context_lex_get_div(struct isl_context *context, struct isl_tab *tab,
        return get_div(tab, context, div);
 }
 
+/* Add a div specified by "div" to the context tableau and return
+ * 1 if the div is obviously non-negative.
+ * context_tab_add_div will always return 1, because all variables
+ * in a isl_context_lex tableau are non-negative.
+ * However, if we are using a big parameter in the context, then this only
+ * reflects the non-negativity of the variable used to _encode_ the
+ * div, i.e., div' = M + div, so we can't draw any conclusions.
+ */
 static int context_lex_add_div(struct isl_context *context, struct isl_vec *div)
 {
        struct isl_context_lex *clex = (struct isl_context_lex *)context;
-       return context_tab_add_div(clex->tab, div,
+       int nonneg;
+       nonneg = context_tab_add_div(clex->tab, div,
                                        context_lex_add_ineq_wrap, context);
+       if (nonneg < 0)
+               return -1;
+       if (clex->tab->M)
+               return 0;
+       return nonneg;
 }
 
 static int context_lex_detect_equalities(struct isl_context *context,
@@ -3946,7 +3961,10 @@ static int constraint_equal(const void *entry, const void *val)
 }
 
 /* Check whether "bmap" has a pair of constraints that have
- * the same coefficients for the "output" variables, i.e.,
+ * the same coefficients for the output variables.
+ * Note that the coefficients of the existentially quantified
+ * variables need to be zero since the existentially quantified
+ * of the result are usually not the same as those of the input.
  * the isl_dim_out and isl_dim_div dimensions.
  * If so, return 1 and return the row indices of the two constraints
  * in *first and *second.
@@ -3959,6 +3977,8 @@ static int parallel_constraints(__isl_keep isl_basic_map *bmap,
        struct isl_hash_table *table = NULL;
        struct isl_hash_table_entry *entry;
        struct isl_constraint_equal_info info;
+       unsigned n_out;
+       unsigned n_div;
 
        ctx = isl_basic_map_get_ctx(bmap);
        table = isl_hash_table_alloc(ctx, bmap->n_ineq);
@@ -3968,12 +3988,16 @@ static int parallel_constraints(__isl_keep isl_basic_map *bmap,
        info.n_in = isl_basic_map_dim(bmap, isl_dim_param) +
                    isl_basic_map_dim(bmap, isl_dim_in);
        info.bmap = bmap;
-       info.n_out = isl_basic_map_dim(bmap, isl_dim_all) - info.n_in;
+       n_out = isl_basic_map_dim(bmap, isl_dim_out);
+       n_div = isl_basic_map_dim(bmap, isl_dim_div);
+       info.n_out = n_out + n_div;
        for (i = 0; i < bmap->n_ineq; ++i) {
                uint32_t hash;
 
                info.val = bmap->ineq[i] + 1 + info.n_in;
-               if (isl_seq_first_non_zero(info.val, info.n_out) < 0)
+               if (isl_seq_first_non_zero(info.val, n_out) < 0)
+                       continue;
+               if (isl_seq_first_non_zero(info.val + n_out, n_div) >= 0)
                        continue;
                hash = isl_seq_get_hash(info.val, info.n_out);
                entry = isl_hash_table_find(ctx, table, hash,
@@ -4242,7 +4266,7 @@ static __isl_give isl_map *basic_map_partial_lexopt(
  *
  *     a x >= -u >= -b_i(p)
  *
- * Moreover, m = min_i(b_i(p)) satisfied the constraints on u and can
+ * Moreover, m = min_i(b_i(p)) satisfies the constraints on u and can
  * therefore be plugged into the solution.
  */
 static __isl_give isl_map *basic_map_partial_lexopt_symm(