Home | History | Annotate | Line # | Download | only in dist
      1  1.1  mrg /*
      2  1.1  mrg  * Copyright 2008-2009 Katholieke Universiteit Leuven
      3  1.1  mrg  * Copyright 2010      INRIA Saclay
      4  1.1  mrg  * Copyright 2012-2013 Ecole Normale Superieure
      5  1.1  mrg  * Copyright 2014      INRIA Rocquencourt
      6  1.1  mrg  * Copyright 2016      INRIA Paris
      7  1.1  mrg  * Copyright 2020      Cerebras Systems
      8  1.1  mrg  *
      9  1.1  mrg  * Use of this software is governed by the MIT license
     10  1.1  mrg  *
     11  1.1  mrg  * Written by Sven Verdoolaege, K.U.Leuven, Departement
     12  1.1  mrg  * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
     13  1.1  mrg  * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
     14  1.1  mrg  * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
     15  1.1  mrg  * and Ecole Normale Superieure, 45 rue dUlm, 75230 Paris, France
     16  1.1  mrg  * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
     17  1.1  mrg  * B.P. 105 - 78153 Le Chesnay, France
     18  1.1  mrg  * and Centre de Recherche Inria de Paris, 2 rue Simone Iff - Voie DQ12,
     19  1.1  mrg  * CS 42112, 75589 Paris Cedex 12, France
     20  1.1  mrg  * and Cerebras Systems, 175 S San Antonio Rd, Los Altos, CA, USA
     21  1.1  mrg  */
     22  1.1  mrg 
     23  1.1  mrg #include <isl_ctx_private.h>
     24  1.1  mrg #include "isl_map_private.h"
     25  1.1  mrg #include <isl_seq.h>
     26  1.1  mrg #include <isl/options.h>
     27  1.1  mrg #include "isl_tab.h"
     28  1.1  mrg #include <isl_mat_private.h>
     29  1.1  mrg #include <isl_local_space_private.h>
     30  1.1  mrg #include <isl_val_private.h>
     31  1.1  mrg #include <isl_vec_private.h>
     32  1.1  mrg #include <isl_aff_private.h>
     33  1.1  mrg #include <isl_equalities.h>
     34  1.1  mrg #include <isl_constraint_private.h>
     35  1.1  mrg 
     36  1.1  mrg #include <set_to_map.c>
     37  1.1  mrg #include <set_from_map.c>
     38  1.1  mrg 
     39  1.1  mrg #define STATUS_ERROR		-1
     40  1.1  mrg #define STATUS_REDUNDANT	 1
     41  1.1  mrg #define STATUS_VALID	 	 2
     42  1.1  mrg #define STATUS_SEPARATE	 	 3
     43  1.1  mrg #define STATUS_CUT	 	 4
     44  1.1  mrg #define STATUS_ADJ_EQ	 	 5
     45  1.1  mrg #define STATUS_ADJ_INEQ	 	 6
     46  1.1  mrg 
     47  1.1  mrg static int status_in(isl_int *ineq, struct isl_tab *tab)
     48  1.1  mrg {
     49  1.1  mrg 	enum isl_ineq_type type = isl_tab_ineq_type(tab, ineq);
     50  1.1  mrg 	switch (type) {
     51  1.1  mrg 	default:
     52  1.1  mrg 	case isl_ineq_error:		return STATUS_ERROR;
     53  1.1  mrg 	case isl_ineq_redundant:	return STATUS_VALID;
     54  1.1  mrg 	case isl_ineq_separate:		return STATUS_SEPARATE;
     55  1.1  mrg 	case isl_ineq_cut:		return STATUS_CUT;
     56  1.1  mrg 	case isl_ineq_adj_eq:		return STATUS_ADJ_EQ;
     57  1.1  mrg 	case isl_ineq_adj_ineq:		return STATUS_ADJ_INEQ;
     58  1.1  mrg 	}
     59  1.1  mrg }
     60  1.1  mrg 
     61  1.1  mrg /* Compute the position of the equalities of basic map "bmap_i"
     62  1.1  mrg  * with respect to the basic map represented by "tab_j".
     63  1.1  mrg  * The resulting array has twice as many entries as the number
     64  1.1  mrg  * of equalities corresponding to the two inequalities to which
     65  1.1  mrg  * each equality corresponds.
     66  1.1  mrg  */
     67  1.1  mrg static int *eq_status_in(__isl_keep isl_basic_map *bmap_i,
     68  1.1  mrg 	struct isl_tab *tab_j)
     69  1.1  mrg {
     70  1.1  mrg 	int k, l;
     71  1.1  mrg 	int *eq;
     72  1.1  mrg 	isl_size dim;
     73  1.1  mrg 
     74  1.1  mrg 	dim = isl_basic_map_dim(bmap_i, isl_dim_all);
     75  1.1  mrg 	if (dim < 0)
     76  1.1  mrg 		return NULL;
     77  1.1  mrg 
     78  1.1  mrg 	eq = isl_calloc_array(bmap_i->ctx, int, 2 * bmap_i->n_eq);
     79  1.1  mrg 	if (!eq)
     80  1.1  mrg 		return NULL;
     81  1.1  mrg 
     82  1.1  mrg 	for (k = 0; k < bmap_i->n_eq; ++k) {
     83  1.1  mrg 		for (l = 0; l < 2; ++l) {
     84  1.1  mrg 			isl_seq_neg(bmap_i->eq[k], bmap_i->eq[k], 1+dim);
     85  1.1  mrg 			eq[2 * k + l] = status_in(bmap_i->eq[k], tab_j);
     86  1.1  mrg 			if (eq[2 * k + l] == STATUS_ERROR)
     87  1.1  mrg 				goto error;
     88  1.1  mrg 		}
     89  1.1  mrg 	}
     90  1.1  mrg 
     91  1.1  mrg 	return eq;
     92  1.1  mrg error:
     93  1.1  mrg 	free(eq);
     94  1.1  mrg 	return NULL;
     95  1.1  mrg }
     96  1.1  mrg 
     97  1.1  mrg /* Compute the position of the inequalities of basic map "bmap_i"
     98  1.1  mrg  * (also represented by "tab_i", if not NULL) with respect to the basic map
     99  1.1  mrg  * represented by "tab_j".
    100  1.1  mrg  */
    101  1.1  mrg static int *ineq_status_in(__isl_keep isl_basic_map *bmap_i,
    102  1.1  mrg 	struct isl_tab *tab_i, struct isl_tab *tab_j)
    103  1.1  mrg {
    104  1.1  mrg 	int k;
    105  1.1  mrg 	unsigned n_eq = bmap_i->n_eq;
    106  1.1  mrg 	int *ineq = isl_calloc_array(bmap_i->ctx, int, bmap_i->n_ineq);
    107  1.1  mrg 
    108  1.1  mrg 	if (!ineq)
    109  1.1  mrg 		return NULL;
    110  1.1  mrg 
    111  1.1  mrg 	for (k = 0; k < bmap_i->n_ineq; ++k) {
    112  1.1  mrg 		if (tab_i && isl_tab_is_redundant(tab_i, n_eq + k)) {
    113  1.1  mrg 			ineq[k] = STATUS_REDUNDANT;
    114  1.1  mrg 			continue;
    115  1.1  mrg 		}
    116  1.1  mrg 		ineq[k] = status_in(bmap_i->ineq[k], tab_j);
    117  1.1  mrg 		if (ineq[k] == STATUS_ERROR)
    118  1.1  mrg 			goto error;
    119  1.1  mrg 		if (ineq[k] == STATUS_SEPARATE)
    120  1.1  mrg 			break;
    121  1.1  mrg 	}
    122  1.1  mrg 
    123  1.1  mrg 	return ineq;
    124  1.1  mrg error:
    125  1.1  mrg 	free(ineq);
    126  1.1  mrg 	return NULL;
    127  1.1  mrg }
    128  1.1  mrg 
    129  1.1  mrg static int any(int *con, unsigned len, int status)
    130  1.1  mrg {
    131  1.1  mrg 	int i;
    132  1.1  mrg 
    133  1.1  mrg 	for (i = 0; i < len ; ++i)
    134  1.1  mrg 		if (con[i] == status)
    135  1.1  mrg 			return 1;
    136  1.1  mrg 	return 0;
    137  1.1  mrg }
    138  1.1  mrg 
    139  1.1  mrg /* Return the first position of "status" in the list "con" of length "len".
    140  1.1  mrg  * Return -1 if there is no such entry.
    141  1.1  mrg  */
    142  1.1  mrg static int find(int *con, unsigned len, int status)
    143  1.1  mrg {
    144  1.1  mrg 	int i;
    145  1.1  mrg 
    146  1.1  mrg 	for (i = 0; i < len ; ++i)
    147  1.1  mrg 		if (con[i] == status)
    148  1.1  mrg 			return i;
    149  1.1  mrg 	return -1;
    150  1.1  mrg }
    151  1.1  mrg 
    152  1.1  mrg static int count(int *con, unsigned len, int status)
    153  1.1  mrg {
    154  1.1  mrg 	int i;
    155  1.1  mrg 	int c = 0;
    156  1.1  mrg 
    157  1.1  mrg 	for (i = 0; i < len ; ++i)
    158  1.1  mrg 		if (con[i] == status)
    159  1.1  mrg 			c++;
    160  1.1  mrg 	return c;
    161  1.1  mrg }
    162  1.1  mrg 
    163  1.1  mrg static int all(int *con, unsigned len, int status)
    164  1.1  mrg {
    165  1.1  mrg 	int i;
    166  1.1  mrg 
    167  1.1  mrg 	for (i = 0; i < len ; ++i) {
    168  1.1  mrg 		if (con[i] == STATUS_REDUNDANT)
    169  1.1  mrg 			continue;
    170  1.1  mrg 		if (con[i] != status)
    171  1.1  mrg 			return 0;
    172  1.1  mrg 	}
    173  1.1  mrg 	return 1;
    174  1.1  mrg }
    175  1.1  mrg 
    176  1.1  mrg /* Internal information associated to a basic map in a map
    177  1.1  mrg  * that is to be coalesced by isl_map_coalesce.
    178  1.1  mrg  *
    179  1.1  mrg  * "bmap" is the basic map itself (or NULL if "removed" is set)
    180  1.1  mrg  * "tab" is the corresponding tableau (or NULL if "removed" is set)
    181  1.1  mrg  * "hull_hash" identifies the affine space in which "bmap" lives.
    182  1.1  mrg  * "modified" is set if this basic map may not be identical
    183  1.1  mrg  * to any of the basic maps in the input.
    184  1.1  mrg  * "removed" is set if this basic map has been removed from the map
    185  1.1  mrg  * "simplify" is set if this basic map may have some unknown integer
    186  1.1  mrg  * divisions that were not present in the input basic maps.  The basic
    187  1.1  mrg  * map should then be simplified such that we may be able to find
    188  1.1  mrg  * a definition among the constraints.
    189  1.1  mrg  *
    190  1.1  mrg  * "eq" and "ineq" are only set if we are currently trying to coalesce
    191  1.1  mrg  * this basic map with another basic map, in which case they represent
    192  1.1  mrg  * the position of the inequalities of this basic map with respect to
    193  1.1  mrg  * the other basic map.  The number of elements in the "eq" array
    194  1.1  mrg  * is twice the number of equalities in the "bmap", corresponding
    195  1.1  mrg  * to the two inequalities that make up each equality.
    196  1.1  mrg  */
    197  1.1  mrg struct isl_coalesce_info {
    198  1.1  mrg 	isl_basic_map *bmap;
    199  1.1  mrg 	struct isl_tab *tab;
    200  1.1  mrg 	uint32_t hull_hash;
    201  1.1  mrg 	int modified;
    202  1.1  mrg 	int removed;
    203  1.1  mrg 	int simplify;
    204  1.1  mrg 	int *eq;
    205  1.1  mrg 	int *ineq;
    206  1.1  mrg };
    207  1.1  mrg 
    208  1.1  mrg /* Is there any (half of an) equality constraint in the description
    209  1.1  mrg  * of the basic map represented by "info" that
    210  1.1  mrg  * has position "status" with respect to the other basic map?
    211  1.1  mrg  */
    212  1.1  mrg static int any_eq(struct isl_coalesce_info *info, int status)
    213  1.1  mrg {
    214  1.1  mrg 	isl_size n_eq;
    215  1.1  mrg 
    216  1.1  mrg 	n_eq = isl_basic_map_n_equality(info->bmap);
    217  1.1  mrg 	return any(info->eq, 2 * n_eq, status);
    218  1.1  mrg }
    219  1.1  mrg 
    220  1.1  mrg /* Is there any inequality constraint in the description
    221  1.1  mrg  * of the basic map represented by "info" that
    222  1.1  mrg  * has position "status" with respect to the other basic map?
    223  1.1  mrg  */
    224  1.1  mrg static int any_ineq(struct isl_coalesce_info *info, int status)
    225  1.1  mrg {
    226  1.1  mrg 	isl_size n_ineq;
    227  1.1  mrg 
    228  1.1  mrg 	n_ineq = isl_basic_map_n_inequality(info->bmap);
    229  1.1  mrg 	return any(info->ineq, n_ineq, status);
    230  1.1  mrg }
    231  1.1  mrg 
    232  1.1  mrg /* Return the position of the first half on an equality constraint
    233  1.1  mrg  * in the description of the basic map represented by "info" that
    234  1.1  mrg  * has position "status" with respect to the other basic map.
    235  1.1  mrg  * The returned value is twice the position of the equality constraint
    236  1.1  mrg  * plus zero for the negative half and plus one for the positive half.
    237  1.1  mrg  * Return -1 if there is no such entry.
    238  1.1  mrg  */
    239  1.1  mrg static int find_eq(struct isl_coalesce_info *info, int status)
    240  1.1  mrg {
    241  1.1  mrg 	isl_size n_eq;
    242  1.1  mrg 
    243  1.1  mrg 	n_eq = isl_basic_map_n_equality(info->bmap);
    244  1.1  mrg 	return find(info->eq, 2 * n_eq, status);
    245  1.1  mrg }
    246  1.1  mrg 
    247  1.1  mrg /* Return the position of the first inequality constraint in the description
    248  1.1  mrg  * of the basic map represented by "info" that
    249  1.1  mrg  * has position "status" with respect to the other basic map.
    250  1.1  mrg  * Return -1 if there is no such entry.
    251  1.1  mrg  */
    252  1.1  mrg static int find_ineq(struct isl_coalesce_info *info, int status)
    253  1.1  mrg {
    254  1.1  mrg 	isl_size n_ineq;
    255  1.1  mrg 
    256  1.1  mrg 	n_ineq = isl_basic_map_n_inequality(info->bmap);
    257  1.1  mrg 	return find(info->ineq, n_ineq, status);
    258  1.1  mrg }
    259  1.1  mrg 
    260  1.1  mrg /* Return the number of (halves of) equality constraints in the description
    261  1.1  mrg  * of the basic map represented by "info" that
    262  1.1  mrg  * have position "status" with respect to the other basic map.
    263  1.1  mrg  */
    264  1.1  mrg static int count_eq(struct isl_coalesce_info *info, int status)
    265  1.1  mrg {
    266  1.1  mrg 	isl_size n_eq;
    267  1.1  mrg 
    268  1.1  mrg 	n_eq = isl_basic_map_n_equality(info->bmap);
    269  1.1  mrg 	return count(info->eq, 2 * n_eq, status);
    270  1.1  mrg }
    271  1.1  mrg 
    272  1.1  mrg /* Return the number of inequality constraints in the description
    273  1.1  mrg  * of the basic map represented by "info" that
    274  1.1  mrg  * have position "status" with respect to the other basic map.
    275  1.1  mrg  */
    276  1.1  mrg static int count_ineq(struct isl_coalesce_info *info, int status)
    277  1.1  mrg {
    278  1.1  mrg 	isl_size n_ineq;
    279  1.1  mrg 
    280  1.1  mrg 	n_ineq = isl_basic_map_n_inequality(info->bmap);
    281  1.1  mrg 	return count(info->ineq, n_ineq, status);
    282  1.1  mrg }
    283  1.1  mrg 
    284  1.1  mrg /* Are all non-redundant constraints of the basic map represented by "info"
    285  1.1  mrg  * either valid or cut constraints with respect to the other basic map?
    286  1.1  mrg  */
    287  1.1  mrg static int all_valid_or_cut(struct isl_coalesce_info *info)
    288  1.1  mrg {
    289  1.1  mrg 	int i;
    290  1.1  mrg 
    291  1.1  mrg 	for (i = 0; i < 2 * info->bmap->n_eq; ++i) {
    292  1.1  mrg 		if (info->eq[i] == STATUS_REDUNDANT)
    293  1.1  mrg 			continue;
    294  1.1  mrg 		if (info->eq[i] == STATUS_VALID)
    295  1.1  mrg 			continue;
    296  1.1  mrg 		if (info->eq[i] == STATUS_CUT)
    297  1.1  mrg 			continue;
    298  1.1  mrg 		return 0;
    299  1.1  mrg 	}
    300  1.1  mrg 
    301  1.1  mrg 	for (i = 0; i < info->bmap->n_ineq; ++i) {
    302  1.1  mrg 		if (info->ineq[i] == STATUS_REDUNDANT)
    303  1.1  mrg 			continue;
    304  1.1  mrg 		if (info->ineq[i] == STATUS_VALID)
    305  1.1  mrg 			continue;
    306  1.1  mrg 		if (info->ineq[i] == STATUS_CUT)
    307  1.1  mrg 			continue;
    308  1.1  mrg 		return 0;
    309  1.1  mrg 	}
    310  1.1  mrg 
    311  1.1  mrg 	return 1;
    312  1.1  mrg }
    313  1.1  mrg 
    314  1.1  mrg /* Compute the hash of the (apparent) affine hull of info->bmap (with
    315  1.1  mrg  * the existentially quantified variables removed) and store it
    316  1.1  mrg  * in info->hash.
    317  1.1  mrg  */
    318  1.1  mrg static int coalesce_info_set_hull_hash(struct isl_coalesce_info *info)
    319  1.1  mrg {
    320  1.1  mrg 	isl_basic_map *hull;
    321  1.1  mrg 	isl_size n_div;
    322  1.1  mrg 
    323  1.1  mrg 	hull = isl_basic_map_copy(info->bmap);
    324  1.1  mrg 	hull = isl_basic_map_plain_affine_hull(hull);
    325  1.1  mrg 	n_div = isl_basic_map_dim(hull, isl_dim_div);
    326  1.1  mrg 	if (n_div < 0)
    327  1.1  mrg 		hull = isl_basic_map_free(hull);
    328  1.1  mrg 	hull = isl_basic_map_drop_constraints_involving_dims(hull,
    329  1.1  mrg 							isl_dim_div, 0, n_div);
    330  1.1  mrg 	info->hull_hash = isl_basic_map_get_hash(hull);
    331  1.1  mrg 	isl_basic_map_free(hull);
    332  1.1  mrg 
    333  1.1  mrg 	return hull ? 0 : -1;
    334  1.1  mrg }
    335  1.1  mrg 
    336  1.1  mrg /* Free all the allocated memory in an array
    337  1.1  mrg  * of "n" isl_coalesce_info elements.
    338  1.1  mrg  */
    339  1.1  mrg static void clear_coalesce_info(int n, struct isl_coalesce_info *info)
    340  1.1  mrg {
    341  1.1  mrg 	int i;
    342  1.1  mrg 
    343  1.1  mrg 	if (!info)
    344  1.1  mrg 		return;
    345  1.1  mrg 
    346  1.1  mrg 	for (i = 0; i < n; ++i) {
    347  1.1  mrg 		isl_basic_map_free(info[i].bmap);
    348  1.1  mrg 		isl_tab_free(info[i].tab);
    349  1.1  mrg 	}
    350  1.1  mrg 
    351  1.1  mrg 	free(info);
    352  1.1  mrg }
    353  1.1  mrg 
    354  1.1  mrg /* Clear the memory associated to "info".
    355  1.1  mrg  */
    356  1.1  mrg static void clear(struct isl_coalesce_info *info)
    357  1.1  mrg {
    358  1.1  mrg 	info->bmap = isl_basic_map_free(info->bmap);
    359  1.1  mrg 	isl_tab_free(info->tab);
    360  1.1  mrg 	info->tab = NULL;
    361  1.1  mrg }
    362  1.1  mrg 
    363  1.1  mrg /* Drop the basic map represented by "info".
    364  1.1  mrg  * That is, clear the memory associated to the entry and
    365  1.1  mrg  * mark it as having been removed.
    366  1.1  mrg  */
    367  1.1  mrg static void drop(struct isl_coalesce_info *info)
    368  1.1  mrg {
    369  1.1  mrg 	clear(info);
    370  1.1  mrg 	info->removed = 1;
    371  1.1  mrg }
    372  1.1  mrg 
    373  1.1  mrg /* Exchange the information in "info1" with that in "info2".
    374  1.1  mrg  */
    375  1.1  mrg static void exchange(struct isl_coalesce_info *info1,
    376  1.1  mrg 	struct isl_coalesce_info *info2)
    377  1.1  mrg {
    378  1.1  mrg 	struct isl_coalesce_info info;
    379  1.1  mrg 
    380  1.1  mrg 	info = *info1;
    381  1.1  mrg 	*info1 = *info2;
    382  1.1  mrg 	*info2 = info;
    383  1.1  mrg }
    384  1.1  mrg 
    385  1.1  mrg /* This type represents the kind of change that has been performed
    386  1.1  mrg  * while trying to coalesce two basic maps.
    387  1.1  mrg  *
    388  1.1  mrg  * isl_change_none: nothing was changed
    389  1.1  mrg  * isl_change_drop_first: the first basic map was removed
    390  1.1  mrg  * isl_change_drop_second: the second basic map was removed
    391  1.1  mrg  * isl_change_fuse: the two basic maps were replaced by a new basic map.
    392  1.1  mrg  */
    393  1.1  mrg enum isl_change {
    394  1.1  mrg 	isl_change_error = -1,
    395  1.1  mrg 	isl_change_none = 0,
    396  1.1  mrg 	isl_change_drop_first,
    397  1.1  mrg 	isl_change_drop_second,
    398  1.1  mrg 	isl_change_fuse,
    399  1.1  mrg };
    400  1.1  mrg 
    401  1.1  mrg /* Update "change" based on an interchange of the first and the second
    402  1.1  mrg  * basic map.  That is, interchange isl_change_drop_first and
    403  1.1  mrg  * isl_change_drop_second.
    404  1.1  mrg  */
    405  1.1  mrg static enum isl_change invert_change(enum isl_change change)
    406  1.1  mrg {
    407  1.1  mrg 	switch (change) {
    408  1.1  mrg 	case isl_change_error:
    409  1.1  mrg 		return isl_change_error;
    410  1.1  mrg 	case isl_change_none:
    411  1.1  mrg 		return isl_change_none;
    412  1.1  mrg 	case isl_change_drop_first:
    413  1.1  mrg 		return isl_change_drop_second;
    414  1.1  mrg 	case isl_change_drop_second:
    415  1.1  mrg 		return isl_change_drop_first;
    416  1.1  mrg 	case isl_change_fuse:
    417  1.1  mrg 		return isl_change_fuse;
    418  1.1  mrg 	}
    419  1.1  mrg 
    420  1.1  mrg 	return isl_change_error;
    421  1.1  mrg }
    422  1.1  mrg 
    423  1.1  mrg /* Add the valid constraints of the basic map represented by "info"
    424  1.1  mrg  * to "bmap".  "len" is the size of the constraints.
    425  1.1  mrg  * If only one of the pair of inequalities that make up an equality
    426  1.1  mrg  * is valid, then add that inequality.
    427  1.1  mrg  */
    428  1.1  mrg static __isl_give isl_basic_map *add_valid_constraints(
    429  1.1  mrg 	__isl_take isl_basic_map *bmap, struct isl_coalesce_info *info,
    430  1.1  mrg 	unsigned len)
    431  1.1  mrg {
    432  1.1  mrg 	int k, l;
    433  1.1  mrg 
    434  1.1  mrg 	if (!bmap)
    435  1.1  mrg 		return NULL;
    436  1.1  mrg 
    437  1.1  mrg 	for (k = 0; k < info->bmap->n_eq; ++k) {
    438  1.1  mrg 		if (info->eq[2 * k] == STATUS_VALID &&
    439  1.1  mrg 		    info->eq[2 * k + 1] == STATUS_VALID) {
    440  1.1  mrg 			l = isl_basic_map_alloc_equality(bmap);
    441  1.1  mrg 			if (l < 0)
    442  1.1  mrg 				return isl_basic_map_free(bmap);
    443  1.1  mrg 			isl_seq_cpy(bmap->eq[l], info->bmap->eq[k], len);
    444  1.1  mrg 		} else if (info->eq[2 * k] == STATUS_VALID) {
    445  1.1  mrg 			l = isl_basic_map_alloc_inequality(bmap);
    446  1.1  mrg 			if (l < 0)
    447  1.1  mrg 				return isl_basic_map_free(bmap);
    448  1.1  mrg 			isl_seq_neg(bmap->ineq[l], info->bmap->eq[k], len);
    449  1.1  mrg 		} else if (info->eq[2 * k + 1] == STATUS_VALID) {
    450  1.1  mrg 			l = isl_basic_map_alloc_inequality(bmap);
    451  1.1  mrg 			if (l < 0)
    452  1.1  mrg 				return isl_basic_map_free(bmap);
    453  1.1  mrg 			isl_seq_cpy(bmap->ineq[l], info->bmap->eq[k], len);
    454  1.1  mrg 		}
    455  1.1  mrg 	}
    456  1.1  mrg 
    457  1.1  mrg 	for (k = 0; k < info->bmap->n_ineq; ++k) {
    458  1.1  mrg 		if (info->ineq[k] != STATUS_VALID)
    459  1.1  mrg 			continue;
    460  1.1  mrg 		l = isl_basic_map_alloc_inequality(bmap);
    461  1.1  mrg 		if (l < 0)
    462  1.1  mrg 			return isl_basic_map_free(bmap);
    463  1.1  mrg 		isl_seq_cpy(bmap->ineq[l], info->bmap->ineq[k], len);
    464  1.1  mrg 	}
    465  1.1  mrg 
    466  1.1  mrg 	return bmap;
    467  1.1  mrg }
    468  1.1  mrg 
    469  1.1  mrg /* Is "bmap" defined by a number of (non-redundant) constraints that
    470  1.1  mrg  * is greater than the number of constraints of basic maps i and j combined?
    471  1.1  mrg  * Equalities are counted as two inequalities.
    472  1.1  mrg  */
    473  1.1  mrg static int number_of_constraints_increases(int i, int j,
    474  1.1  mrg 	struct isl_coalesce_info *info,
    475  1.1  mrg 	__isl_keep isl_basic_map *bmap, struct isl_tab *tab)
    476  1.1  mrg {
    477  1.1  mrg 	int k, n_old, n_new;
    478  1.1  mrg 
    479  1.1  mrg 	n_old = 2 * info[i].bmap->n_eq + info[i].bmap->n_ineq;
    480  1.1  mrg 	n_old += 2 * info[j].bmap->n_eq + info[j].bmap->n_ineq;
    481  1.1  mrg 
    482  1.1  mrg 	n_new = 2 * bmap->n_eq;
    483  1.1  mrg 	for (k = 0; k < bmap->n_ineq; ++k)
    484  1.1  mrg 		if (!isl_tab_is_redundant(tab, bmap->n_eq + k))
    485  1.1  mrg 			++n_new;
    486  1.1  mrg 
    487  1.1  mrg 	return n_new > n_old;
    488  1.1  mrg }
    489  1.1  mrg 
    490  1.1  mrg /* Replace the pair of basic maps i and j by the basic map bounded
    491  1.1  mrg  * by the valid constraints in both basic maps and the constraints
    492  1.1  mrg  * in extra (if not NULL).
    493  1.1  mrg  * Place the fused basic map in the position that is the smallest of i and j.
    494  1.1  mrg  *
    495  1.1  mrg  * If "detect_equalities" is set, then look for equalities encoded
    496  1.1  mrg  * as pairs of inequalities.
    497  1.1  mrg  * If "check_number" is set, then the original basic maps are only
    498  1.1  mrg  * replaced if the total number of constraints does not increase.
    499  1.1  mrg  * While the number of integer divisions in the two basic maps
    500  1.1  mrg  * is assumed to be the same, the actual definitions may be different.
    501  1.1  mrg  * We only copy the definition from one of the basic maps if it is
    502  1.1  mrg  * the same as that of the other basic map.  Otherwise, we mark
    503  1.1  mrg  * the integer division as unknown and simplify the basic map
    504  1.1  mrg  * in an attempt to recover the integer division definition.
    505  1.1  mrg  * If any extra constraints get introduced, then these may
    506  1.1  mrg  * involve integer divisions with a unit coefficient.
    507  1.1  mrg  * Eliminate those that do not appear with any other coefficient
    508  1.1  mrg  * in other constraints, to ensure they get eliminated completely,
    509  1.1  mrg  * improving the chances of further coalescing.
    510  1.1  mrg  */
    511  1.1  mrg static enum isl_change fuse(int i, int j, struct isl_coalesce_info *info,
    512  1.1  mrg 	__isl_keep isl_mat *extra, int detect_equalities, int check_number)
    513  1.1  mrg {
    514  1.1  mrg 	int k, l;
    515  1.1  mrg 	struct isl_basic_map *fused = NULL;
    516  1.1  mrg 	struct isl_tab *fused_tab = NULL;
    517  1.1  mrg 	isl_size total = isl_basic_map_dim(info[i].bmap, isl_dim_all);
    518  1.1  mrg 	unsigned extra_rows = extra ? extra->n_row : 0;
    519  1.1  mrg 	unsigned n_eq, n_ineq;
    520  1.1  mrg 	int simplify = 0;
    521  1.1  mrg 
    522  1.1  mrg 	if (total < 0)
    523  1.1  mrg 		return isl_change_error;
    524  1.1  mrg 	if (j < i)
    525  1.1  mrg 		return fuse(j, i, info, extra, detect_equalities, check_number);
    526  1.1  mrg 
    527  1.1  mrg 	n_eq = info[i].bmap->n_eq + info[j].bmap->n_eq;
    528  1.1  mrg 	n_ineq = info[i].bmap->n_ineq + info[j].bmap->n_ineq;
    529  1.1  mrg 	fused = isl_basic_map_alloc_space(isl_space_copy(info[i].bmap->dim),
    530  1.1  mrg 		    info[i].bmap->n_div, n_eq, n_eq + n_ineq + extra_rows);
    531  1.1  mrg 	fused = add_valid_constraints(fused, &info[i], 1 + total);
    532  1.1  mrg 	fused = add_valid_constraints(fused, &info[j], 1 + total);
    533  1.1  mrg 	if (!fused)
    534  1.1  mrg 		goto error;
    535  1.1  mrg 	if (ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_RATIONAL) &&
    536  1.1  mrg 	    ISL_F_ISSET(info[j].bmap, ISL_BASIC_MAP_RATIONAL))
    537  1.1  mrg 		ISL_F_SET(fused, ISL_BASIC_MAP_RATIONAL);
    538  1.1  mrg 
    539  1.1  mrg 	for (k = 0; k < info[i].bmap->n_div; ++k) {
    540  1.1  mrg 		int l = isl_basic_map_alloc_div(fused);
    541  1.1  mrg 		if (l < 0)
    542  1.1  mrg 			goto error;
    543  1.1  mrg 		if (isl_seq_eq(info[i].bmap->div[k], info[j].bmap->div[k],
    544  1.1  mrg 				1 + 1 + total)) {
    545  1.1  mrg 			isl_seq_cpy(fused->div[l], info[i].bmap->div[k],
    546  1.1  mrg 				1 + 1 + total);
    547  1.1  mrg 		} else {
    548  1.1  mrg 			isl_int_set_si(fused->div[l][0], 0);
    549  1.1  mrg 			simplify = 1;
    550  1.1  mrg 		}
    551  1.1  mrg 	}
    552  1.1  mrg 
    553  1.1  mrg 	for (k = 0; k < extra_rows; ++k) {
    554  1.1  mrg 		l = isl_basic_map_alloc_inequality(fused);
    555  1.1  mrg 		if (l < 0)
    556  1.1  mrg 			goto error;
    557  1.1  mrg 		isl_seq_cpy(fused->ineq[l], extra->row[k], 1 + total);
    558  1.1  mrg 	}
    559  1.1  mrg 
    560  1.1  mrg 	if (detect_equalities)
    561  1.1  mrg 		fused = isl_basic_map_detect_inequality_pairs(fused, NULL);
    562  1.1  mrg 	fused = isl_basic_map_gauss(fused, NULL);
    563  1.1  mrg 	if (simplify || info[j].simplify) {
    564  1.1  mrg 		fused = isl_basic_map_simplify(fused);
    565  1.1  mrg 		info[i].simplify = 0;
    566  1.1  mrg 	} else if (extra_rows > 0) {
    567  1.1  mrg 		fused = isl_basic_map_eliminate_pure_unit_divs(fused);
    568  1.1  mrg 	}
    569  1.1  mrg 	fused = isl_basic_map_finalize(fused);
    570  1.1  mrg 
    571  1.1  mrg 	fused_tab = isl_tab_from_basic_map(fused, 0);
    572  1.1  mrg 	if (isl_tab_detect_redundant(fused_tab) < 0)
    573  1.1  mrg 		goto error;
    574  1.1  mrg 
    575  1.1  mrg 	if (check_number &&
    576  1.1  mrg 	    number_of_constraints_increases(i, j, info, fused, fused_tab)) {
    577  1.1  mrg 		isl_tab_free(fused_tab);
    578  1.1  mrg 		isl_basic_map_free(fused);
    579  1.1  mrg 		return isl_change_none;
    580  1.1  mrg 	}
    581  1.1  mrg 
    582  1.1  mrg 	clear(&info[i]);
    583  1.1  mrg 	info[i].bmap = fused;
    584  1.1  mrg 	info[i].tab = fused_tab;
    585  1.1  mrg 	info[i].modified = 1;
    586  1.1  mrg 	drop(&info[j]);
    587  1.1  mrg 
    588  1.1  mrg 	return isl_change_fuse;
    589  1.1  mrg error:
    590  1.1  mrg 	isl_tab_free(fused_tab);
    591  1.1  mrg 	isl_basic_map_free(fused);
    592  1.1  mrg 	return isl_change_error;
    593  1.1  mrg }
    594  1.1  mrg 
    595  1.1  mrg /* Given a pair of basic maps i and j such that all constraints are either
    596  1.1  mrg  * "valid" or "cut", check if the facets corresponding to the "cut"
    597  1.1  mrg  * constraints of i lie entirely within basic map j.
    598  1.1  mrg  * If so, replace the pair by the basic map consisting of the valid
    599  1.1  mrg  * constraints in both basic maps.
    600  1.1  mrg  * Checking whether the facet lies entirely within basic map j
    601  1.1  mrg  * is performed by checking whether the constraints of basic map j
    602  1.1  mrg  * are valid for the facet.  These tests are performed on a rational
    603  1.1  mrg  * tableau to avoid the theoretical possibility that a constraint
    604  1.1  mrg  * that was considered to be a cut constraint for the entire basic map i
    605  1.1  mrg  * happens to be considered to be a valid constraint for the facet,
    606  1.1  mrg  * even though it cuts off the same rational points.
    607  1.1  mrg  *
    608  1.1  mrg  * To see that we are not introducing any extra points, call the
    609  1.1  mrg  * two basic maps A and B and the resulting map U and let x
    610  1.1  mrg  * be an element of U \setminus ( A \cup B ).
    611  1.1  mrg  * A line connecting x with an element of A \cup B meets a facet F
    612  1.1  mrg  * of either A or B.  Assume it is a facet of B and let c_1 be
    613  1.1  mrg  * the corresponding facet constraint.  We have c_1(x) < 0 and
    614  1.1  mrg  * so c_1 is a cut constraint.  This implies that there is some
    615  1.1  mrg  * (possibly rational) point x' satisfying the constraints of A
    616  1.1  mrg  * and the opposite of c_1 as otherwise c_1 would have been marked
    617  1.1  mrg  * valid for A.  The line connecting x and x' meets a facet of A
    618  1.1  mrg  * in a (possibly rational) point that also violates c_1, but this
    619  1.1  mrg  * is impossible since all cut constraints of B are valid for all
    620  1.1  mrg  * cut facets of A.
    621  1.1  mrg  * In case F is a facet of A rather than B, then we can apply the
    622  1.1  mrg  * above reasoning to find a facet of B separating x from A \cup B first.
    623  1.1  mrg  */
    624  1.1  mrg static enum isl_change check_facets(int i, int j,
    625  1.1  mrg 	struct isl_coalesce_info *info)
    626  1.1  mrg {
    627  1.1  mrg 	int k, l;
    628  1.1  mrg 	struct isl_tab_undo *snap, *snap2;
    629  1.1  mrg 	unsigned n_eq = info[i].bmap->n_eq;
    630  1.1  mrg 
    631  1.1  mrg 	snap = isl_tab_snap(info[i].tab);
    632  1.1  mrg 	if (isl_tab_mark_rational(info[i].tab) < 0)
    633  1.1  mrg 		return isl_change_error;
    634  1.1  mrg 	snap2 = isl_tab_snap(info[i].tab);
    635  1.1  mrg 
    636  1.1  mrg 	for (k = 0; k < info[i].bmap->n_ineq; ++k) {
    637  1.1  mrg 		if (info[i].ineq[k] != STATUS_CUT)
    638  1.1  mrg 			continue;
    639  1.1  mrg 		if (isl_tab_select_facet(info[i].tab, n_eq + k) < 0)
    640  1.1  mrg 			return isl_change_error;
    641  1.1  mrg 		for (l = 0; l < info[j].bmap->n_ineq; ++l) {
    642  1.1  mrg 			int stat;
    643  1.1  mrg 			if (info[j].ineq[l] != STATUS_CUT)
    644  1.1  mrg 				continue;
    645  1.1  mrg 			stat = status_in(info[j].bmap->ineq[l], info[i].tab);
    646  1.1  mrg 			if (stat < 0)
    647  1.1  mrg 				return isl_change_error;
    648  1.1  mrg 			if (stat != STATUS_VALID)
    649  1.1  mrg 				break;
    650  1.1  mrg 		}
    651  1.1  mrg 		if (isl_tab_rollback(info[i].tab, snap2) < 0)
    652  1.1  mrg 			return isl_change_error;
    653  1.1  mrg 		if (l < info[j].bmap->n_ineq)
    654  1.1  mrg 			break;
    655  1.1  mrg 	}
    656  1.1  mrg 
    657  1.1  mrg 	if (k < info[i].bmap->n_ineq) {
    658  1.1  mrg 		if (isl_tab_rollback(info[i].tab, snap) < 0)
    659  1.1  mrg 			return isl_change_error;
    660  1.1  mrg 		return isl_change_none;
    661  1.1  mrg 	}
    662  1.1  mrg 	return fuse(i, j, info, NULL, 0, 0);
    663  1.1  mrg }
    664  1.1  mrg 
    665  1.1  mrg /* Check if info->bmap contains the basic map represented
    666  1.1  mrg  * by the tableau "tab".
    667  1.1  mrg  * For each equality, we check both the constraint itself
    668  1.1  mrg  * (as an inequality) and its negation.  Make sure the
    669  1.1  mrg  * equality is returned to its original state before returning.
    670  1.1  mrg  */
    671  1.1  mrg static isl_bool contains(struct isl_coalesce_info *info, struct isl_tab *tab)
    672  1.1  mrg {
    673  1.1  mrg 	int k;
    674  1.1  mrg 	isl_size dim;
    675  1.1  mrg 	isl_basic_map *bmap = info->bmap;
    676  1.1  mrg 
    677  1.1  mrg 	dim = isl_basic_map_dim(bmap, isl_dim_all);
    678  1.1  mrg 	if (dim < 0)
    679  1.1  mrg 		return isl_bool_error;
    680  1.1  mrg 	for (k = 0; k < bmap->n_eq; ++k) {
    681  1.1  mrg 		int stat;
    682  1.1  mrg 		isl_seq_neg(bmap->eq[k], bmap->eq[k], 1 + dim);
    683  1.1  mrg 		stat = status_in(bmap->eq[k], tab);
    684  1.1  mrg 		isl_seq_neg(bmap->eq[k], bmap->eq[k], 1 + dim);
    685  1.1  mrg 		if (stat < 0)
    686  1.1  mrg 			return isl_bool_error;
    687  1.1  mrg 		if (stat != STATUS_VALID)
    688  1.1  mrg 			return isl_bool_false;
    689  1.1  mrg 		stat = status_in(bmap->eq[k], tab);
    690  1.1  mrg 		if (stat < 0)
    691  1.1  mrg 			return isl_bool_error;
    692  1.1  mrg 		if (stat != STATUS_VALID)
    693  1.1  mrg 			return isl_bool_false;
    694  1.1  mrg 	}
    695  1.1  mrg 
    696  1.1  mrg 	for (k = 0; k < bmap->n_ineq; ++k) {
    697  1.1  mrg 		int stat;
    698  1.1  mrg 		if (info->ineq[k] == STATUS_REDUNDANT)
    699  1.1  mrg 			continue;
    700  1.1  mrg 		stat = status_in(bmap->ineq[k], tab);
    701  1.1  mrg 		if (stat < 0)
    702  1.1  mrg 			return isl_bool_error;
    703  1.1  mrg 		if (stat != STATUS_VALID)
    704  1.1  mrg 			return isl_bool_false;
    705  1.1  mrg 	}
    706  1.1  mrg 	return isl_bool_true;
    707  1.1  mrg }
    708  1.1  mrg 
    709  1.1  mrg /* Basic map "i" has an inequality "k" that is adjacent
    710  1.1  mrg  * to some inequality of basic map "j".  All the other inequalities
    711  1.1  mrg  * are valid for "j".
    712  1.1  mrg  * If not NULL, then "extra" contains extra wrapping constraints that are valid
    713  1.1  mrg  * for both "i" and "j".
    714  1.1  mrg  * Check if basic map "j" forms an extension of basic map "i",
    715  1.1  mrg  * taking into account the extra constraints, if any.
    716  1.1  mrg  *
    717  1.1  mrg  * Note that this function is only called if some of the equalities or
    718  1.1  mrg  * inequalities of basic map "j" do cut basic map "i".  The function is
    719  1.1  mrg  * correct even if there are no such cut constraints, but in that case
    720  1.1  mrg  * the additional checks performed by this function are overkill.
    721  1.1  mrg  *
    722  1.1  mrg  * In particular, we replace constraint k, say f >= 0, by constraint
    723  1.1  mrg  * f <= -1, add the inequalities of "j" that are valid for "i",
    724  1.1  mrg  * as well as the "extra" constraints, if any,
    725  1.1  mrg  * and check if the result is a subset of basic map "j".
    726  1.1  mrg  * To improve the chances of the subset relation being detected,
    727  1.1  mrg  * any variable that only attains a single integer value
    728  1.1  mrg  * in the tableau of "i" is first fixed to that value.
    729  1.1  mrg  * If the result is a subset, then we know that this result is exactly equal
    730  1.1  mrg  * to basic map "j" since all its constraints are valid for basic map "j".
    731  1.1  mrg  * By combining the valid constraints of "i" (all equalities and all
    732  1.1  mrg  * inequalities except "k"), the valid constraints of "j" and
    733  1.1  mrg  * the "extra" constraints, if any, we therefore
    734  1.1  mrg  * obtain a basic map that is equal to their union.
    735  1.1  mrg  * In this case, there is no need to perform a rollback of the tableau
    736  1.1  mrg  * since it is going to be destroyed in fuse().
    737  1.1  mrg  *
    738  1.1  mrg  *
    739  1.1  mrg  *	|\__			|\__
    740  1.1  mrg  *	|   \__			|   \__
    741  1.1  mrg  *	|      \_	=>	|      \__
    742  1.1  mrg  *	|_______| _		|_________\
    743  1.1  mrg  *
    744  1.1  mrg  *
    745  1.1  mrg  *	|\			|\
    746  1.1  mrg  *	| \			| \
    747  1.1  mrg  *	|  \			|  \
    748  1.1  mrg  *	|  |			|   \
    749  1.1  mrg  *	|  ||\		=>      |    \
    750  1.1  mrg  *	|  || \			|     \
    751  1.1  mrg  *	|  ||  |		|      |
    752  1.1  mrg  *	|__||_/			|_____/
    753  1.1  mrg  *
    754  1.1  mrg  *
    755  1.1  mrg  *	_______			 _______
    756  1.1  mrg  *     |       | __		|       \__
    757  1.1  mrg  *     |       ||__|	=>	|        __|
    758  1.1  mrg  *     |_______|		|_______/
    759  1.1  mrg  */
    760  1.1  mrg static enum isl_change is_adj_ineq_extension_with_wraps(int i, int j, int k,
    761  1.1  mrg 	struct isl_coalesce_info *info, __isl_keep isl_mat *extra)
    762  1.1  mrg {
    763  1.1  mrg 	struct isl_tab_undo *snap;
    764  1.1  mrg 	isl_size n_eq_i, n_ineq_j, n_extra;
    765  1.1  mrg 	isl_size total = isl_basic_map_dim(info[i].bmap, isl_dim_all);
    766  1.1  mrg 	isl_stat r;
    767  1.1  mrg 	isl_bool super;
    768  1.1  mrg 
    769  1.1  mrg 	if (total < 0)
    770  1.1  mrg 		return isl_change_error;
    771  1.1  mrg 
    772  1.1  mrg 	n_eq_i = isl_basic_map_n_equality(info[i].bmap);
    773  1.1  mrg 	n_ineq_j = isl_basic_map_n_inequality(info[j].bmap);
    774  1.1  mrg 	n_extra = isl_mat_rows(extra);
    775  1.1  mrg 	if (n_eq_i < 0 || n_ineq_j < 0 || n_extra < 0)
    776  1.1  mrg 		return isl_change_error;
    777  1.1  mrg 
    778  1.1  mrg 	if (isl_tab_extend_cons(info[i].tab, 1 + n_ineq_j + n_extra) < 0)
    779  1.1  mrg 		return isl_change_error;
    780  1.1  mrg 
    781  1.1  mrg 	snap = isl_tab_snap(info[i].tab);
    782  1.1  mrg 
    783  1.1  mrg 	if (isl_tab_unrestrict(info[i].tab, n_eq_i + k) < 0)
    784  1.1  mrg 		return isl_change_error;
    785  1.1  mrg 
    786  1.1  mrg 	isl_seq_neg(info[i].bmap->ineq[k], info[i].bmap->ineq[k], 1 + total);
    787  1.1  mrg 	isl_int_sub_ui(info[i].bmap->ineq[k][0], info[i].bmap->ineq[k][0], 1);
    788  1.1  mrg 	r = isl_tab_add_ineq(info[i].tab, info[i].bmap->ineq[k]);
    789  1.1  mrg 	isl_seq_neg(info[i].bmap->ineq[k], info[i].bmap->ineq[k], 1 + total);
    790  1.1  mrg 	isl_int_sub_ui(info[i].bmap->ineq[k][0], info[i].bmap->ineq[k][0], 1);
    791  1.1  mrg 	if (r < 0)
    792  1.1  mrg 		return isl_change_error;
    793  1.1  mrg 
    794  1.1  mrg 	for (k = 0; k < n_ineq_j; ++k) {
    795  1.1  mrg 		if (info[j].ineq[k] != STATUS_VALID)
    796  1.1  mrg 			continue;
    797  1.1  mrg 		if (isl_tab_add_ineq(info[i].tab, info[j].bmap->ineq[k]) < 0)
    798  1.1  mrg 			return isl_change_error;
    799  1.1  mrg 	}
    800  1.1  mrg 	for (k = 0; k < n_extra; ++k) {
    801  1.1  mrg 		if (isl_tab_add_ineq(info[i].tab, extra->row[k]) < 0)
    802  1.1  mrg 			return isl_change_error;
    803  1.1  mrg 	}
    804  1.1  mrg 	if (isl_tab_detect_constants(info[i].tab) < 0)
    805  1.1  mrg 		return isl_change_error;
    806  1.1  mrg 
    807  1.1  mrg 	super = contains(&info[j], info[i].tab);
    808  1.1  mrg 	if (super < 0)
    809  1.1  mrg 		return isl_change_error;
    810  1.1  mrg 	if (super)
    811  1.1  mrg 		return fuse(i, j, info, extra, 0, 0);
    812  1.1  mrg 
    813  1.1  mrg 	if (isl_tab_rollback(info[i].tab, snap) < 0)
    814  1.1  mrg 		return isl_change_error;
    815  1.1  mrg 
    816  1.1  mrg 	return isl_change_none;
    817  1.1  mrg }
    818  1.1  mrg 
    819  1.1  mrg /* Given an affine transformation matrix "T", does row "row" represent
    820  1.1  mrg  * anything other than a unit vector (possibly shifted by a constant)
    821  1.1  mrg  * that is not involved in any of the other rows?
    822  1.1  mrg  *
    823  1.1  mrg  * That is, if a constraint involves the variable corresponding to
    824  1.1  mrg  * the row, then could its preimage by "T" have any coefficients
    825  1.1  mrg  * that are different from those in the original constraint?
    826  1.1  mrg  */
    827  1.1  mrg static int not_unique_unit_row(__isl_keep isl_mat *T, int row)
    828  1.1  mrg {
    829  1.1  mrg 	int i, j;
    830  1.1  mrg 	int len = T->n_col - 1;
    831  1.1  mrg 
    832  1.1  mrg 	i = isl_seq_first_non_zero(T->row[row] + 1, len);
    833  1.1  mrg 	if (i < 0)
    834  1.1  mrg 		return 1;
    835  1.1  mrg 	if (!isl_int_is_one(T->row[row][1 + i]) &&
    836  1.1  mrg 	    !isl_int_is_negone(T->row[row][1 + i]))
    837  1.1  mrg 		return 1;
    838  1.1  mrg 
    839  1.1  mrg 	j = isl_seq_first_non_zero(T->row[row] + 1 + i + 1, len - (i + 1));
    840  1.1  mrg 	if (j >= 0)
    841  1.1  mrg 		return 1;
    842  1.1  mrg 
    843  1.1  mrg 	for (j = 1; j < T->n_row; ++j) {
    844  1.1  mrg 		if (j == row)
    845  1.1  mrg 			continue;
    846  1.1  mrg 		if (!isl_int_is_zero(T->row[j][1 + i]))
    847  1.1  mrg 			return 1;
    848  1.1  mrg 	}
    849  1.1  mrg 
    850  1.1  mrg 	return 0;
    851  1.1  mrg }
    852  1.1  mrg 
    853  1.1  mrg /* Does inequality constraint "ineq" of "bmap" involve any of
    854  1.1  mrg  * the variables marked in "affected"?
    855  1.1  mrg  * "total" is the total number of variables, i.e., the number
    856  1.1  mrg  * of entries in "affected".
    857  1.1  mrg  */
    858  1.1  mrg static isl_bool is_affected(__isl_keep isl_basic_map *bmap, int ineq,
    859  1.1  mrg 	int *affected, int total)
    860  1.1  mrg {
    861  1.1  mrg 	int i;
    862  1.1  mrg 
    863  1.1  mrg 	for (i = 0; i < total; ++i) {
    864  1.1  mrg 		if (!affected[i])
    865  1.1  mrg 			continue;
    866  1.1  mrg 		if (!isl_int_is_zero(bmap->ineq[ineq][1 + i]))
    867  1.1  mrg 			return isl_bool_true;
    868  1.1  mrg 	}
    869  1.1  mrg 
    870  1.1  mrg 	return isl_bool_false;
    871  1.1  mrg }
    872  1.1  mrg 
    873  1.1  mrg /* Given the compressed version of inequality constraint "ineq"
    874  1.1  mrg  * of info->bmap in "v", check if the constraint can be tightened,
    875  1.1  mrg  * where the compression is based on an equality constraint valid
    876  1.1  mrg  * for info->tab.
    877  1.1  mrg  * If so, add the tightened version of the inequality constraint
    878  1.1  mrg  * to info->tab.  "v" may be modified by this function.
    879  1.1  mrg  *
    880  1.1  mrg  * That is, if the compressed constraint is of the form
    881  1.1  mrg  *
    882  1.1  mrg  *	m f() + c >= 0
    883  1.1  mrg  *
    884  1.1  mrg  * with 0 < c < m, then it is equivalent to
    885  1.1  mrg  *
    886  1.1  mrg  *	f() >= 0
    887  1.1  mrg  *
    888  1.1  mrg  * This means that c can also be subtracted from the original,
    889  1.1  mrg  * uncompressed constraint without affecting the integer points
    890  1.1  mrg  * in info->tab.  Add this tightened constraint as an extra row
    891  1.1  mrg  * to info->tab to make this information explicitly available.
    892  1.1  mrg  */
    893  1.1  mrg static __isl_give isl_vec *try_tightening(struct isl_coalesce_info *info,
    894  1.1  mrg 	int ineq, __isl_take isl_vec *v)
    895  1.1  mrg {
    896  1.1  mrg 	isl_ctx *ctx;
    897  1.1  mrg 	isl_stat r;
    898  1.1  mrg 
    899  1.1  mrg 	if (!v)
    900  1.1  mrg 		return NULL;
    901  1.1  mrg 
    902  1.1  mrg 	ctx = isl_vec_get_ctx(v);
    903  1.1  mrg 	isl_seq_gcd(v->el + 1, v->size - 1, &ctx->normalize_gcd);
    904  1.1  mrg 	if (isl_int_is_zero(ctx->normalize_gcd) ||
    905  1.1  mrg 	    isl_int_is_one(ctx->normalize_gcd)) {
    906  1.1  mrg 		return v;
    907  1.1  mrg 	}
    908  1.1  mrg 
    909  1.1  mrg 	v = isl_vec_cow(v);
    910  1.1  mrg 	if (!v)
    911  1.1  mrg 		return NULL;
    912  1.1  mrg 
    913  1.1  mrg 	isl_int_fdiv_r(v->el[0], v->el[0], ctx->normalize_gcd);
    914  1.1  mrg 	if (isl_int_is_zero(v->el[0]))
    915  1.1  mrg 		return v;
    916  1.1  mrg 
    917  1.1  mrg 	if (isl_tab_extend_cons(info->tab, 1) < 0)
    918  1.1  mrg 		return isl_vec_free(v);
    919  1.1  mrg 
    920  1.1  mrg 	isl_int_sub(info->bmap->ineq[ineq][0],
    921  1.1  mrg 		    info->bmap->ineq[ineq][0], v->el[0]);
    922  1.1  mrg 	r = isl_tab_add_ineq(info->tab, info->bmap->ineq[ineq]);
    923  1.1  mrg 	isl_int_add(info->bmap->ineq[ineq][0],
    924  1.1  mrg 		    info->bmap->ineq[ineq][0], v->el[0]);
    925  1.1  mrg 
    926  1.1  mrg 	if (r < 0)
    927  1.1  mrg 		return isl_vec_free(v);
    928  1.1  mrg 
    929  1.1  mrg 	return v;
    930  1.1  mrg }
    931  1.1  mrg 
    932  1.1  mrg /* Tighten the (non-redundant) constraints on the facet represented
    933  1.1  mrg  * by info->tab.
    934  1.1  mrg  * In particular, on input, info->tab represents the result
    935  1.1  mrg  * of relaxing the "n" inequality constraints of info->bmap in "relaxed"
    936  1.1  mrg  * by one, i.e., replacing f_i >= 0 by f_i + 1 >= 0, and then
    937  1.1  mrg  * replacing the one at index "l" by the corresponding equality,
    938  1.1  mrg  * i.e., f_k + 1 = 0, with k = relaxed[l].
    939  1.1  mrg  *
    940  1.1  mrg  * Compute a variable compression from the equality constraint f_k + 1 = 0
    941  1.1  mrg  * and use it to tighten the other constraints of info->bmap
    942  1.1  mrg  * (that is, all constraints that have not been relaxed),
    943  1.1  mrg  * updating info->tab (and leaving info->bmap untouched).
    944  1.1  mrg  * The compression handles essentially two cases, one where a variable
    945  1.1  mrg  * is assigned a fixed value and can therefore be eliminated, and one
    946  1.1  mrg  * where one variable is a shifted multiple of some other variable and
    947  1.1  mrg  * can therefore be replaced by that multiple.
    948  1.1  mrg  * Gaussian elimination would also work for the first case, but for
    949  1.1  mrg  * the second case, the effectiveness would depend on the order
    950  1.1  mrg  * of the variables.
    951  1.1  mrg  * After compression, some of the constraints may have coefficients
    952  1.1  mrg  * with a common divisor.  If this divisor does not divide the constant
    953  1.1  mrg  * term, then the constraint can be tightened.
    954  1.1  mrg  * The tightening is performed on the tableau info->tab by introducing
    955  1.1  mrg  * extra (temporary) constraints.
    956  1.1  mrg  *
    957  1.1  mrg  * Only constraints that are possibly affected by the compression are
    958  1.1  mrg  * considered.  In particular, if the constraint only involves variables
    959  1.1  mrg  * that are directly mapped to a distinct set of other variables, then
    960  1.1  mrg  * no common divisor can be introduced and no tightening can occur.
    961  1.1  mrg  *
    962  1.1  mrg  * It is important to only consider the non-redundant constraints
    963  1.1  mrg  * since the facet constraint has been relaxed prior to the call
    964  1.1  mrg  * to this function, meaning that the constraints that were redundant
    965  1.1  mrg  * prior to the relaxation may no longer be redundant.
    966  1.1  mrg  * These constraints will be ignored in the fused result, so
    967  1.1  mrg  * the fusion detection should not exploit them.
    968  1.1  mrg  */
    969  1.1  mrg static isl_stat tighten_on_relaxed_facet(struct isl_coalesce_info *info,
    970  1.1  mrg 	int n, int *relaxed, int l)
    971  1.1  mrg {
    972  1.1  mrg 	isl_size total;
    973  1.1  mrg 	isl_ctx *ctx;
    974  1.1  mrg 	isl_vec *v = NULL;
    975  1.1  mrg 	isl_mat *T;
    976  1.1  mrg 	int i;
    977  1.1  mrg 	int k;
    978  1.1  mrg 	int *affected;
    979  1.1  mrg 
    980  1.1  mrg 	k = relaxed[l];
    981  1.1  mrg 	ctx = isl_basic_map_get_ctx(info->bmap);
    982  1.1  mrg 	total = isl_basic_map_dim(info->bmap, isl_dim_all);
    983  1.1  mrg 	if (total < 0)
    984  1.1  mrg 		return isl_stat_error;
    985  1.1  mrg 	isl_int_add_ui(info->bmap->ineq[k][0], info->bmap->ineq[k][0], 1);
    986  1.1  mrg 	T = isl_mat_sub_alloc6(ctx, info->bmap->ineq, k, 1, 0, 1 + total);
    987  1.1  mrg 	T = isl_mat_variable_compression(T, NULL);
    988  1.1  mrg 	isl_int_sub_ui(info->bmap->ineq[k][0], info->bmap->ineq[k][0], 1);
    989  1.1  mrg 	if (!T)
    990  1.1  mrg 		return isl_stat_error;
    991  1.1  mrg 	if (T->n_col == 0) {
    992  1.1  mrg 		isl_mat_free(T);
    993  1.1  mrg 		return isl_stat_ok;
    994  1.1  mrg 	}
    995  1.1  mrg 
    996  1.1  mrg 	affected = isl_alloc_array(ctx, int, total);
    997  1.1  mrg 	if (!affected)
    998  1.1  mrg 		goto error;
    999  1.1  mrg 
   1000  1.1  mrg 	for (i = 0; i < total; ++i)
   1001  1.1  mrg 		affected[i] = not_unique_unit_row(T, 1 + i);
   1002  1.1  mrg 
   1003  1.1  mrg 	for (i = 0; i < info->bmap->n_ineq; ++i) {
   1004  1.1  mrg 		isl_bool handle;
   1005  1.1  mrg 		if (any(relaxed, n, i))
   1006  1.1  mrg 			continue;
   1007  1.1  mrg 		if (info->ineq[i] == STATUS_REDUNDANT)
   1008  1.1  mrg 			continue;
   1009  1.1  mrg 		handle = is_affected(info->bmap, i, affected, total);
   1010  1.1  mrg 		if (handle < 0)
   1011  1.1  mrg 			goto error;
   1012  1.1  mrg 		if (!handle)
   1013  1.1  mrg 			continue;
   1014  1.1  mrg 		v = isl_vec_alloc(ctx, 1 + total);
   1015  1.1  mrg 		if (!v)
   1016  1.1  mrg 			goto error;
   1017  1.1  mrg 		isl_seq_cpy(v->el, info->bmap->ineq[i], 1 + total);
   1018  1.1  mrg 		v = isl_vec_mat_product(v, isl_mat_copy(T));
   1019  1.1  mrg 		v = try_tightening(info, i, v);
   1020  1.1  mrg 		isl_vec_free(v);
   1021  1.1  mrg 		if (!v)
   1022  1.1  mrg 			goto error;
   1023  1.1  mrg 	}
   1024  1.1  mrg 
   1025  1.1  mrg 	isl_mat_free(T);
   1026  1.1  mrg 	free(affected);
   1027  1.1  mrg 	return isl_stat_ok;
   1028  1.1  mrg error:
   1029  1.1  mrg 	isl_mat_free(T);
   1030  1.1  mrg 	free(affected);
   1031  1.1  mrg 	return isl_stat_error;
   1032  1.1  mrg }
   1033  1.1  mrg 
   1034  1.1  mrg /* Replace the basic maps "i" and "j" by an extension of "i"
   1035  1.1  mrg  * along the "n" inequality constraints in "relax" by one.
   1036  1.1  mrg  * The tableau info[i].tab has already been extended.
   1037  1.1  mrg  * Extend info[i].bmap accordingly by relaxing all constraints in "relax"
   1038  1.1  mrg  * by one.
   1039  1.1  mrg  * Each integer division that does not have exactly the same
   1040  1.1  mrg  * definition in "i" and "j" is marked unknown and the basic map
   1041  1.1  mrg  * is scheduled to be simplified in an attempt to recover
   1042  1.1  mrg  * the integer division definition.
   1043  1.1  mrg  * Place the extension in the position that is the smallest of i and j.
   1044  1.1  mrg  */
   1045  1.1  mrg static enum isl_change extend(int i, int j, int n, int *relax,
   1046  1.1  mrg 	struct isl_coalesce_info *info)
   1047  1.1  mrg {
   1048  1.1  mrg 	int l;
   1049  1.1  mrg 	isl_size total;
   1050  1.1  mrg 
   1051  1.1  mrg 	info[i].bmap = isl_basic_map_cow(info[i].bmap);
   1052  1.1  mrg 	total = isl_basic_map_dim(info[i].bmap, isl_dim_all);
   1053  1.1  mrg 	if (total < 0)
   1054  1.1  mrg 		return isl_change_error;
   1055  1.1  mrg 	for (l = 0; l < info[i].bmap->n_div; ++l)
   1056  1.1  mrg 		if (!isl_seq_eq(info[i].bmap->div[l],
   1057  1.1  mrg 				info[j].bmap->div[l], 1 + 1 + total)) {
   1058  1.1  mrg 			isl_int_set_si(info[i].bmap->div[l][0], 0);
   1059  1.1  mrg 			info[i].simplify = 1;
   1060  1.1  mrg 		}
   1061  1.1  mrg 	for (l = 0; l < n; ++l)
   1062  1.1  mrg 		isl_int_add_ui(info[i].bmap->ineq[relax[l]][0],
   1063  1.1  mrg 				info[i].bmap->ineq[relax[l]][0], 1);
   1064  1.1  mrg 	ISL_F_CLR(info[i].bmap, ISL_BASIC_MAP_NO_REDUNDANT);
   1065  1.1  mrg 	ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_FINAL);
   1066  1.1  mrg 	drop(&info[j]);
   1067  1.1  mrg 	info[i].modified = 1;
   1068  1.1  mrg 	if (j < i)
   1069  1.1  mrg 		exchange(&info[i], &info[j]);
   1070  1.1  mrg 	return isl_change_fuse;
   1071  1.1  mrg }
   1072  1.1  mrg 
   1073  1.1  mrg /* Basic map "i" has "n" inequality constraints (collected in "relax")
   1074  1.1  mrg  * that are such that they include basic map "j" if they are relaxed
   1075  1.1  mrg  * by one.  All the other inequalities are valid for "j".
   1076  1.1  mrg  * Check if basic map "j" forms an extension of basic map "i".
   1077  1.1  mrg  *
   1078  1.1  mrg  * In particular, relax the constraints in "relax", compute the corresponding
   1079  1.1  mrg  * facets one by one and check whether each of these is included
   1080  1.1  mrg  * in the other basic map.
   1081  1.1  mrg  * Before testing for inclusion, the constraints on each facet
   1082  1.1  mrg  * are tightened to increase the chance of an inclusion being detected.
   1083  1.1  mrg  * (Adding the valid constraints of "j" to the tableau of "i", as is done
   1084  1.1  mrg  * in is_adj_ineq_extension, may further increase those chances, but this
   1085  1.1  mrg  * is not currently done.)
   1086  1.1  mrg  * If each facet is included, we know that relaxing the constraints extends
   1087  1.1  mrg  * the basic map with exactly the other basic map (we already know that this
   1088  1.1  mrg  * other basic map is included in the extension, because all other
   1089  1.1  mrg  * inequality constraints are valid of "j") and we can replace the
   1090  1.1  mrg  * two basic maps by this extension.
   1091  1.1  mrg  *
   1092  1.1  mrg  * If any of the relaxed constraints turn out to be redundant, then bail out.
   1093  1.1  mrg  * isl_tab_select_facet refuses to handle such constraints.  It may be
   1094  1.1  mrg  * possible to handle them anyway by making a distinction between
   1095  1.1  mrg  * redundant constraints with a corresponding facet that still intersects
   1096  1.1  mrg  * the set (allowing isl_tab_select_facet to handle them) and
   1097  1.1  mrg  * those where the facet does not intersect the set (which can be ignored
   1098  1.1  mrg  * because the empty facet is trivially included in the other disjunct).
   1099  1.1  mrg  * However, relaxed constraints that turn out to be redundant should
   1100  1.1  mrg  * be fairly rare and no such instance has been reported where
   1101  1.1  mrg  * coalescing would be successful.
   1102  1.1  mrg  *        ____			  _____
   1103  1.1  mrg  *       /    || 		 /     |
   1104  1.1  mrg  *      /     ||  		/      |
   1105  1.1  mrg  *      \     ||   	=>	\      |
   1106  1.1  mrg  *       \    ||		 \     |
   1107  1.1  mrg  *        \___||		  \____|
   1108  1.1  mrg  *
   1109  1.1  mrg  *
   1110  1.1  mrg  *	 \			|\
   1111  1.1  mrg  *	|\\			| \
   1112  1.1  mrg  *	| \\			|  \
   1113  1.1  mrg  *	|  |		=>	|  /
   1114  1.1  mrg  *	| /			| /
   1115  1.1  mrg  *	|/			|/
   1116  1.1  mrg  */
   1117  1.1  mrg static enum isl_change is_relaxed_extension(int i, int j, int n, int *relax,
   1118  1.1  mrg 	struct isl_coalesce_info *info)
   1119  1.1  mrg {
   1120  1.1  mrg 	int l;
   1121  1.1  mrg 	isl_bool super;
   1122  1.1  mrg 	struct isl_tab_undo *snap, *snap2;
   1123  1.1  mrg 	unsigned n_eq = info[i].bmap->n_eq;
   1124  1.1  mrg 
   1125  1.1  mrg 	for (l = 0; l < n; ++l)
   1126  1.1  mrg 		if (isl_tab_is_equality(info[i].tab, n_eq + relax[l]))
   1127  1.1  mrg 			return isl_change_none;
   1128  1.1  mrg 
   1129  1.1  mrg 	snap = isl_tab_snap(info[i].tab);
   1130  1.1  mrg 	for (l = 0; l < n; ++l)
   1131  1.1  mrg 		if (isl_tab_relax(info[i].tab, n_eq + relax[l]) < 0)
   1132  1.1  mrg 			return isl_change_error;
   1133  1.1  mrg 	for (l = 0; l < n; ++l) {
   1134  1.1  mrg 		if (!isl_tab_is_redundant(info[i].tab, n_eq + relax[l]))
   1135  1.1  mrg 			continue;
   1136  1.1  mrg 		if (isl_tab_rollback(info[i].tab, snap) < 0)
   1137  1.1  mrg 			return isl_change_error;
   1138  1.1  mrg 		return isl_change_none;
   1139  1.1  mrg 	}
   1140  1.1  mrg 	snap2 = isl_tab_snap(info[i].tab);
   1141  1.1  mrg 	for (l = 0; l < n; ++l) {
   1142  1.1  mrg 		if (isl_tab_rollback(info[i].tab, snap2) < 0)
   1143  1.1  mrg 			return isl_change_error;
   1144  1.1  mrg 		if (isl_tab_select_facet(info[i].tab, n_eq + relax[l]) < 0)
   1145  1.1  mrg 			return isl_change_error;
   1146  1.1  mrg 		if (tighten_on_relaxed_facet(&info[i], n, relax, l) < 0)
   1147  1.1  mrg 			return isl_change_error;
   1148  1.1  mrg 		super = contains(&info[j], info[i].tab);
   1149  1.1  mrg 		if (super < 0)
   1150  1.1  mrg 			return isl_change_error;
   1151  1.1  mrg 		if (super)
   1152  1.1  mrg 			continue;
   1153  1.1  mrg 		if (isl_tab_rollback(info[i].tab, snap) < 0)
   1154  1.1  mrg 			return isl_change_error;
   1155  1.1  mrg 		return isl_change_none;
   1156  1.1  mrg 	}
   1157  1.1  mrg 
   1158  1.1  mrg 	if (isl_tab_rollback(info[i].tab, snap2) < 0)
   1159  1.1  mrg 		return isl_change_error;
   1160  1.1  mrg 	return extend(i, j, n, relax, info);
   1161  1.1  mrg }
   1162  1.1  mrg 
   1163  1.1  mrg /* Data structure that keeps track of the wrapping constraints
   1164  1.1  mrg  * and of information to bound the coefficients of those constraints.
   1165  1.1  mrg  *
   1166  1.1  mrg  * "failed" is set if wrapping has failed.
   1167  1.1  mrg  * bound is set if we want to apply a bound on the coefficients
   1168  1.1  mrg  * mat contains the wrapping constraints
   1169  1.1  mrg  * max is the bound on the coefficients (if bound is set)
   1170  1.1  mrg  */
   1171  1.1  mrg struct isl_wraps {
   1172  1.1  mrg 	int failed;
   1173  1.1  mrg 	int bound;
   1174  1.1  mrg 	isl_mat *mat;
   1175  1.1  mrg 	isl_int max;
   1176  1.1  mrg };
   1177  1.1  mrg 
   1178  1.1  mrg /* Update wraps->max to be greater than or equal to the coefficients
   1179  1.1  mrg  * in the equalities and inequalities of info->bmap that can be removed
   1180  1.1  mrg  * if we end up applying wrapping.
   1181  1.1  mrg  */
   1182  1.1  mrg static isl_stat wraps_update_max(struct isl_wraps *wraps,
   1183  1.1  mrg 	struct isl_coalesce_info *info)
   1184  1.1  mrg {
   1185  1.1  mrg 	int k;
   1186  1.1  mrg 	isl_int max_k;
   1187  1.1  mrg 	isl_size total = isl_basic_map_dim(info->bmap, isl_dim_all);
   1188  1.1  mrg 
   1189  1.1  mrg 	if (total < 0)
   1190  1.1  mrg 		return isl_stat_error;
   1191  1.1  mrg 	isl_int_init(max_k);
   1192  1.1  mrg 
   1193  1.1  mrg 	for (k = 0; k < info->bmap->n_eq; ++k) {
   1194  1.1  mrg 		if (info->eq[2 * k] == STATUS_VALID &&
   1195  1.1  mrg 		    info->eq[2 * k + 1] == STATUS_VALID)
   1196  1.1  mrg 			continue;
   1197  1.1  mrg 		isl_seq_abs_max(info->bmap->eq[k] + 1, total, &max_k);
   1198  1.1  mrg 		if (isl_int_abs_gt(max_k, wraps->max))
   1199  1.1  mrg 			isl_int_set(wraps->max, max_k);
   1200  1.1  mrg 	}
   1201  1.1  mrg 
   1202  1.1  mrg 	for (k = 0; k < info->bmap->n_ineq; ++k) {
   1203  1.1  mrg 		if (info->ineq[k] == STATUS_VALID ||
   1204  1.1  mrg 		    info->ineq[k] == STATUS_REDUNDANT)
   1205  1.1  mrg 			continue;
   1206  1.1  mrg 		isl_seq_abs_max(info->bmap->ineq[k] + 1, total, &max_k);
   1207  1.1  mrg 		if (isl_int_abs_gt(max_k, wraps->max))
   1208  1.1  mrg 			isl_int_set(wraps->max, max_k);
   1209  1.1  mrg 	}
   1210  1.1  mrg 
   1211  1.1  mrg 	isl_int_clear(max_k);
   1212  1.1  mrg 
   1213  1.1  mrg 	return isl_stat_ok;
   1214  1.1  mrg }
   1215  1.1  mrg 
   1216  1.1  mrg /* Initialize the isl_wraps data structure.
   1217  1.1  mrg  * If we want to bound the coefficients of the wrapping constraints,
   1218  1.1  mrg  * we set wraps->max to the largest coefficient
   1219  1.1  mrg  * in the equalities and inequalities that can be removed if we end up
   1220  1.1  mrg  * applying wrapping.
   1221  1.1  mrg  */
   1222  1.1  mrg static isl_stat wraps_init(struct isl_wraps *wraps, __isl_take isl_mat *mat,
   1223  1.1  mrg 	struct isl_coalesce_info *info, int i, int j)
   1224  1.1  mrg {
   1225  1.1  mrg 	isl_ctx *ctx;
   1226  1.1  mrg 
   1227  1.1  mrg 	wraps->failed = 0;
   1228  1.1  mrg 	wraps->bound = 0;
   1229  1.1  mrg 	wraps->mat = mat;
   1230  1.1  mrg 	if (!mat)
   1231  1.1  mrg 		return isl_stat_error;
   1232  1.1  mrg 	wraps->mat->n_row = 0;
   1233  1.1  mrg 	ctx = isl_mat_get_ctx(mat);
   1234  1.1  mrg 	wraps->bound = isl_options_get_coalesce_bounded_wrapping(ctx);
   1235  1.1  mrg 	if (!wraps->bound)
   1236  1.1  mrg 		return isl_stat_ok;
   1237  1.1  mrg 	isl_int_init(wraps->max);
   1238  1.1  mrg 	isl_int_set_si(wraps->max, 0);
   1239  1.1  mrg 	if (wraps_update_max(wraps, &info[i]) < 0)
   1240  1.1  mrg 		return isl_stat_error;
   1241  1.1  mrg 	if (wraps_update_max(wraps, &info[j]) < 0)
   1242  1.1  mrg 		return isl_stat_error;
   1243  1.1  mrg 
   1244  1.1  mrg 	return isl_stat_ok;
   1245  1.1  mrg }
   1246  1.1  mrg 
   1247  1.1  mrg /* Free the contents of the isl_wraps data structure.
   1248  1.1  mrg  */
   1249  1.1  mrg static void wraps_free(struct isl_wraps *wraps)
   1250  1.1  mrg {
   1251  1.1  mrg 	isl_mat_free(wraps->mat);
   1252  1.1  mrg 	if (wraps->bound)
   1253  1.1  mrg 		isl_int_clear(wraps->max);
   1254  1.1  mrg }
   1255  1.1  mrg 
   1256  1.1  mrg /* Mark the wrapping as failed.
   1257  1.1  mrg  */
   1258  1.1  mrg static isl_stat wraps_mark_failed(struct isl_wraps *wraps)
   1259  1.1  mrg {
   1260  1.1  mrg 	wraps->failed = 1;
   1261  1.1  mrg 	return isl_stat_ok;
   1262  1.1  mrg }
   1263  1.1  mrg 
   1264  1.1  mrg /* Is the wrapping constraint in row "row" allowed?
   1265  1.1  mrg  *
   1266  1.1  mrg  * If wraps->bound is set, we check that none of the coefficients
   1267  1.1  mrg  * is greater than wraps->max.
   1268  1.1  mrg  */
   1269  1.1  mrg static int allow_wrap(struct isl_wraps *wraps, int row)
   1270  1.1  mrg {
   1271  1.1  mrg 	int i;
   1272  1.1  mrg 
   1273  1.1  mrg 	if (!wraps->bound)
   1274  1.1  mrg 		return 1;
   1275  1.1  mrg 
   1276  1.1  mrg 	for (i = 1; i < wraps->mat->n_col; ++i)
   1277  1.1  mrg 		if (isl_int_abs_gt(wraps->mat->row[row][i], wraps->max))
   1278  1.1  mrg 			return 0;
   1279  1.1  mrg 
   1280  1.1  mrg 	return 1;
   1281  1.1  mrg }
   1282  1.1  mrg 
   1283  1.1  mrg /* Wrap "ineq" (or its opposite if "negate" is set) around "bound"
   1284  1.1  mrg  * to include "set" and add the result in position "w" of "wraps".
   1285  1.1  mrg  * "len" is the total number of coefficients in "bound" and "ineq".
   1286  1.1  mrg  * Return 1 on success, 0 on failure and -1 on error.
   1287  1.1  mrg  * Wrapping can fail if the result of wrapping is equal to "bound"
   1288  1.1  mrg  * or if we want to bound the sizes of the coefficients and
   1289  1.1  mrg  * the wrapped constraint does not satisfy this bound.
   1290  1.1  mrg  */
   1291  1.1  mrg static int add_wrap(struct isl_wraps *wraps, int w, isl_int *bound,
   1292  1.1  mrg 	isl_int *ineq, unsigned len, __isl_keep isl_set *set, int negate)
   1293  1.1  mrg {
   1294  1.1  mrg 	isl_seq_cpy(wraps->mat->row[w], bound, len);
   1295  1.1  mrg 	if (negate) {
   1296  1.1  mrg 		isl_seq_neg(wraps->mat->row[w + 1], ineq, len);
   1297  1.1  mrg 		ineq = wraps->mat->row[w + 1];
   1298  1.1  mrg 	}
   1299  1.1  mrg 	if (!isl_set_wrap_facet(set, wraps->mat->row[w], ineq))
   1300  1.1  mrg 		return -1;
   1301  1.1  mrg 	if (isl_seq_eq(wraps->mat->row[w], bound, len))
   1302  1.1  mrg 		return 0;
   1303  1.1  mrg 	if (!allow_wrap(wraps, w))
   1304  1.1  mrg 		return 0;
   1305  1.1  mrg 	return 1;
   1306  1.1  mrg }
   1307  1.1  mrg 
   1308  1.1  mrg /* This function has two modes of operations.
   1309  1.1  mrg  *
   1310  1.1  mrg  * If "add_valid" is set, then all the constraints of info->bmap
   1311  1.1  mrg  * (except the opposite of "bound") are valid for the other basic map.
   1312  1.1  mrg  * In this case, attempts are made to wrap some of these valid constraints
   1313  1.1  mrg  * to more tightly fit around "set".  Only successful wrappings are recorded
   1314  1.1  mrg  * and failed wrappings are ignored.
   1315  1.1  mrg  *
   1316  1.1  mrg  * If "add_valid" is not set, then some of the constraints of info->bmap
   1317  1.1  mrg  * are not valid for the other basic map, and only those are considered
   1318  1.1  mrg  * for wrapping.  In this case all attempted wrappings need to succeed.
   1319  1.1  mrg  * Otherwise "wraps" is marked as failed.
   1320  1.1  mrg  * Note that the constraints that are valid for the other basic map
   1321  1.1  mrg  * will be added to the combined basic map by default, so there is
   1322  1.1  mrg  * no need to wrap them.
   1323  1.1  mrg  * The caller wrap_in_facets even relies on this function not wrapping
   1324  1.1  mrg  * any constraints that are already valid.
   1325  1.1  mrg  *
   1326  1.1  mrg  * Only consider constraints that are not redundant (as determined
   1327  1.1  mrg  * by info->tab) and that are valid or invalid depending on "add_valid".
   1328  1.1  mrg  * Wrap each constraint around "bound" such that it includes the whole
   1329  1.1  mrg  * set "set" and append the resulting constraint to "wraps".
   1330  1.1  mrg  * "wraps" is assumed to have been pre-allocated to the appropriate size.
   1331  1.1  mrg  * wraps->n_row is the number of actual wrapped constraints that have
   1332  1.1  mrg  * been added.
   1333  1.1  mrg  * If any of the wrapping problems results in a constraint that is
   1334  1.1  mrg  * identical to "bound", then this means that "set" is unbounded in such
   1335  1.1  mrg  * a way that no wrapping is possible.
   1336  1.1  mrg  * Similarly, if we want to bound the coefficients of the wrapping
   1337  1.1  mrg  * constraints and a newly added wrapping constraint does not
   1338  1.1  mrg  * satisfy the bound, then the wrapping is considered to have failed.
   1339  1.1  mrg  * Note though that "wraps" is only marked failed if "add_valid" is not set.
   1340  1.1  mrg  */
   1341  1.1  mrg static isl_stat add_selected_wraps(struct isl_wraps *wraps,
   1342  1.1  mrg 	struct isl_coalesce_info *info, isl_int *bound, __isl_keep isl_set *set,
   1343  1.1  mrg 	int add_valid)
   1344  1.1  mrg {
   1345  1.1  mrg 	int l, m;
   1346  1.1  mrg 	int w;
   1347  1.1  mrg 	int added;
   1348  1.1  mrg 	isl_basic_map *bmap = info->bmap;
   1349  1.1  mrg 	isl_size total = isl_basic_map_dim(bmap, isl_dim_all);
   1350  1.1  mrg 	unsigned len = 1 + total;
   1351  1.1  mrg 
   1352  1.1  mrg 	if (total < 0)
   1353  1.1  mrg 		return isl_stat_error;
   1354  1.1  mrg 
   1355  1.1  mrg 	w = wraps->mat->n_row;
   1356  1.1  mrg 
   1357  1.1  mrg 	for (l = 0; l < bmap->n_ineq; ++l) {
   1358  1.1  mrg 		int is_valid = info->ineq[l] == STATUS_VALID;
   1359  1.1  mrg 		if ((!add_valid && is_valid) ||
   1360  1.1  mrg 		    info->ineq[l] == STATUS_REDUNDANT)
   1361  1.1  mrg 			continue;
   1362  1.1  mrg 		if (isl_seq_is_neg(bound, bmap->ineq[l], len))
   1363  1.1  mrg 			continue;
   1364  1.1  mrg 		if (isl_seq_eq(bound, bmap->ineq[l], len))
   1365  1.1  mrg 			continue;
   1366  1.1  mrg 		if (isl_tab_is_redundant(info->tab, bmap->n_eq + l))
   1367  1.1  mrg 			continue;
   1368  1.1  mrg 
   1369  1.1  mrg 		added = add_wrap(wraps, w, bound, bmap->ineq[l], len, set, 0);
   1370  1.1  mrg 		if (added < 0)
   1371  1.1  mrg 			return isl_stat_error;
   1372  1.1  mrg 		if (!added && !is_valid)
   1373  1.1  mrg 			goto unbounded;
   1374  1.1  mrg 		if (added)
   1375  1.1  mrg 			++w;
   1376  1.1  mrg 	}
   1377  1.1  mrg 	for (l = 0; l < bmap->n_eq; ++l) {
   1378  1.1  mrg 		if (isl_seq_is_neg(bound, bmap->eq[l], len))
   1379  1.1  mrg 			continue;
   1380  1.1  mrg 		if (isl_seq_eq(bound, bmap->eq[l], len))
   1381  1.1  mrg 			continue;
   1382  1.1  mrg 
   1383  1.1  mrg 		for (m = 0; m < 2; ++m) {
   1384  1.1  mrg 			if (info->eq[2 * l + m] == STATUS_VALID)
   1385  1.1  mrg 				continue;
   1386  1.1  mrg 			added = add_wrap(wraps, w, bound, bmap->eq[l], len,
   1387  1.1  mrg 					set, !m);
   1388  1.1  mrg 			if (added < 0)
   1389  1.1  mrg 				return isl_stat_error;
   1390  1.1  mrg 			if (!added)
   1391  1.1  mrg 				goto unbounded;
   1392  1.1  mrg 			++w;
   1393  1.1  mrg 		}
   1394  1.1  mrg 	}
   1395  1.1  mrg 
   1396  1.1  mrg 	wraps->mat->n_row = w;
   1397  1.1  mrg 	return isl_stat_ok;
   1398  1.1  mrg unbounded:
   1399  1.1  mrg 	return wraps_mark_failed(wraps);
   1400  1.1  mrg }
   1401  1.1  mrg 
   1402  1.1  mrg /* For each constraint in info->bmap that is not redundant (as determined
   1403  1.1  mrg  * by info->tab) and that is not a valid constraint for the other basic map,
   1404  1.1  mrg  * wrap the constraint around "bound" such that it includes the whole
   1405  1.1  mrg  * set "set" and append the resulting constraint to "wraps".
   1406  1.1  mrg  * Note that the constraints that are valid for the other basic map
   1407  1.1  mrg  * will be added to the combined basic map by default, so there is
   1408  1.1  mrg  * no need to wrap them.
   1409  1.1  mrg  * The caller wrap_in_facets even relies on this function not wrapping
   1410  1.1  mrg  * any constraints that are already valid.
   1411  1.1  mrg  * "wraps" is assumed to have been pre-allocated to the appropriate size.
   1412  1.1  mrg  * wraps->n_row is the number of actual wrapped constraints that have
   1413  1.1  mrg  * been added.
   1414  1.1  mrg  * If any of the wrapping problems results in a constraint that is
   1415  1.1  mrg  * identical to "bound", then this means that "set" is unbounded in such
   1416  1.1  mrg  * a way that no wrapping is possible.  If this happens then "wraps"
   1417  1.1  mrg  * is marked as failed.
   1418  1.1  mrg  * Similarly, if we want to bound the coefficients of the wrapping
   1419  1.1  mrg  * constraints and a newly added wrapping constraint does not
   1420  1.1  mrg  * satisfy the bound, then "wraps" is also marked as failed.
   1421  1.1  mrg  */
   1422  1.1  mrg static isl_stat add_wraps(struct isl_wraps *wraps,
   1423  1.1  mrg 	struct isl_coalesce_info *info, isl_int *bound, __isl_keep isl_set *set)
   1424  1.1  mrg {
   1425  1.1  mrg 	return add_selected_wraps(wraps, info, bound, set, 0);
   1426  1.1  mrg }
   1427  1.1  mrg 
   1428  1.1  mrg /* Check if the constraints in "wraps" from "first" until the last
   1429  1.1  mrg  * are all valid for the basic set represented by "tab",
   1430  1.1  mrg  * dropping the invalid constraints if "keep" is set and
   1431  1.1  mrg  * marking the wrapping as failed if "keep" is not set and
   1432  1.1  mrg  * any constraint turns out to be invalid.
   1433  1.1  mrg  */
   1434  1.1  mrg static isl_stat check_wraps(struct isl_wraps *wraps, int first,
   1435  1.1  mrg 	struct isl_tab *tab, int keep)
   1436  1.1  mrg {
   1437  1.1  mrg 	int i;
   1438  1.1  mrg 
   1439  1.1  mrg 	for (i = wraps->mat->n_row - 1; i >= first; --i) {
   1440  1.1  mrg 		enum isl_ineq_type type;
   1441  1.1  mrg 		type = isl_tab_ineq_type(tab, wraps->mat->row[i]);
   1442  1.1  mrg 		if (type == isl_ineq_error)
   1443  1.1  mrg 			return isl_stat_error;
   1444  1.1  mrg 		if (type == isl_ineq_redundant)
   1445  1.1  mrg 			continue;
   1446  1.1  mrg 		if (!keep)
   1447  1.1  mrg 			return wraps_mark_failed(wraps);
   1448  1.1  mrg 		wraps->mat = isl_mat_drop_rows(wraps->mat, i, 1);
   1449  1.1  mrg 		if (!wraps->mat)
   1450  1.1  mrg 			return isl_stat_error;
   1451  1.1  mrg 	}
   1452  1.1  mrg 
   1453  1.1  mrg 	return isl_stat_ok;
   1454  1.1  mrg }
   1455  1.1  mrg 
   1456  1.1  mrg /* Return a set that corresponds to the non-redundant constraints
   1457  1.1  mrg  * (as recorded in tab) of bmap.
   1458  1.1  mrg  *
   1459  1.1  mrg  * It's important to remove the redundant constraints as some
   1460  1.1  mrg  * of the other constraints may have been modified after the
   1461  1.1  mrg  * constraints were marked redundant.
   1462  1.1  mrg  * In particular, a constraint may have been relaxed.
   1463  1.1  mrg  * Redundant constraints are ignored when a constraint is relaxed
   1464  1.1  mrg  * and should therefore continue to be ignored ever after.
   1465  1.1  mrg  * Otherwise, the relaxation might be thwarted by some of
   1466  1.1  mrg  * these constraints.
   1467  1.1  mrg  *
   1468  1.1  mrg  * Update the underlying set to ensure that the dimension doesn't change.
   1469  1.1  mrg  * Otherwise the integer divisions could get dropped if the tab
   1470  1.1  mrg  * turns out to be empty.
   1471  1.1  mrg  */
   1472  1.1  mrg static __isl_give isl_set *set_from_updated_bmap(__isl_keep isl_basic_map *bmap,
   1473  1.1  mrg 	struct isl_tab *tab)
   1474  1.1  mrg {
   1475  1.1  mrg 	isl_basic_set *bset;
   1476  1.1  mrg 
   1477  1.1  mrg 	bmap = isl_basic_map_copy(bmap);
   1478  1.1  mrg 	bset = isl_basic_map_underlying_set(bmap);
   1479  1.1  mrg 	bset = isl_basic_set_cow(bset);
   1480  1.1  mrg 	bset = isl_basic_set_update_from_tab(bset, tab);
   1481  1.1  mrg 	return isl_set_from_basic_set(bset);
   1482  1.1  mrg }
   1483  1.1  mrg 
   1484  1.1  mrg /* Does "info" have any cut constraints that are redundant?
   1485  1.1  mrg  */
   1486  1.1  mrg static isl_bool has_redundant_cuts(struct isl_coalesce_info *info)
   1487  1.1  mrg {
   1488  1.1  mrg 	int l;
   1489  1.1  mrg 	isl_size n_eq, n_ineq;
   1490  1.1  mrg 
   1491  1.1  mrg 	n_eq = isl_basic_map_n_equality(info->bmap);
   1492  1.1  mrg 	n_ineq = isl_basic_map_n_inequality(info->bmap);
   1493  1.1  mrg 	if (n_eq < 0 || n_ineq < 0)
   1494  1.1  mrg 		return isl_bool_error;
   1495  1.1  mrg 	for (l = 0; l < n_ineq; ++l) {
   1496  1.1  mrg 		int red;
   1497  1.1  mrg 
   1498  1.1  mrg 		if (info->ineq[l] != STATUS_CUT)
   1499  1.1  mrg 			continue;
   1500  1.1  mrg 		red = isl_tab_is_redundant(info->tab, n_eq + l);
   1501  1.1  mrg 		if (red < 0)
   1502  1.1  mrg 			return isl_bool_error;
   1503  1.1  mrg 		if (red)
   1504  1.1  mrg 			return isl_bool_true;
   1505  1.1  mrg 	}
   1506  1.1  mrg 
   1507  1.1  mrg 	return isl_bool_false;
   1508  1.1  mrg }
   1509  1.1  mrg 
   1510  1.1  mrg /* Wrap some constraints of info->bmap that bound the facet defined
   1511  1.1  mrg  * by inequality "k" around (the opposite of) this inequality to
   1512  1.1  mrg  * include "set".  "bound" may be used to store the negated inequality.
   1513  1.1  mrg  *
   1514  1.1  mrg  * If "add_valid" is set, then all ridges are already valid and
   1515  1.1  mrg  * the purpose is to wrap "set" more tightly.  In this case,
   1516  1.1  mrg  * wrapping doesn't fail, although it is possible that no constraint
   1517  1.1  mrg  * gets wrapped.
   1518  1.1  mrg  *
   1519  1.1  mrg  * If "add_valid" is not set, then some of the ridges are cut constraints
   1520  1.1  mrg  * and only those are wrapped around "set".
   1521  1.1  mrg  *
   1522  1.1  mrg  * Since the wrapped constraints are not guaranteed to contain the whole
   1523  1.1  mrg  * of info->bmap, we check them in check_wraps.
   1524  1.1  mrg  * If any of the wrapped constraints turn out to be invalid, then
   1525  1.1  mrg  * check_wraps will mark "wraps" as failed if "add_valid" is not set.
   1526  1.1  mrg  * If "add_valid" is set, then the offending constraints are
   1527  1.1  mrg  * simply removed.
   1528  1.1  mrg  *
   1529  1.1  mrg  * If the facet turns out to be empty, then no wrapping can be performed.
   1530  1.1  mrg  * This is considered a failure, unless "add_valid" is set.
   1531  1.1  mrg  *
   1532  1.1  mrg  * If any of the cut constraints of info->bmap turn out
   1533  1.1  mrg  * to be redundant with respect to other constraints
   1534  1.1  mrg  * then these will neither be wrapped nor added directly to the result.
   1535  1.1  mrg  * The result may therefore not be correct.
   1536  1.1  mrg  * Skip wrapping and mark "wraps" as failed in this case.
   1537  1.1  mrg  */
   1538  1.1  mrg static isl_stat add_selected_wraps_around_facet(struct isl_wraps *wraps,
   1539  1.1  mrg 	struct isl_coalesce_info *info, int k, isl_int *bound,
   1540  1.1  mrg 	__isl_keep isl_set *set, int add_valid)
   1541  1.1  mrg {
   1542  1.1  mrg 	isl_bool nowrap;
   1543  1.1  mrg 	struct isl_tab_undo *snap;
   1544  1.1  mrg 	int n;
   1545  1.1  mrg 	isl_size total = isl_basic_map_dim(info->bmap, isl_dim_all);
   1546  1.1  mrg 
   1547  1.1  mrg 	if (total < 0)
   1548  1.1  mrg 		return isl_stat_error;
   1549  1.1  mrg 
   1550  1.1  mrg 	snap = isl_tab_snap(info->tab);
   1551  1.1  mrg 
   1552  1.1  mrg 	if (isl_tab_select_facet(info->tab, info->bmap->n_eq + k) < 0)
   1553  1.1  mrg 		return isl_stat_error;
   1554  1.1  mrg 	if (isl_tab_detect_redundant(info->tab) < 0)
   1555  1.1  mrg 		return isl_stat_error;
   1556  1.1  mrg 	if (info->tab->empty) {
   1557  1.1  mrg 		if (isl_tab_rollback(info->tab, snap) < 0)
   1558  1.1  mrg 			return isl_stat_error;
   1559  1.1  mrg 		if (!add_valid)
   1560  1.1  mrg 			return wraps_mark_failed(wraps);
   1561  1.1  mrg 		return isl_stat_ok;
   1562  1.1  mrg 	}
   1563  1.1  mrg 	nowrap = has_redundant_cuts(info);
   1564  1.1  mrg 	if (nowrap < 0)
   1565  1.1  mrg 		return isl_stat_error;
   1566  1.1  mrg 
   1567  1.1  mrg 	n = wraps->mat->n_row;
   1568  1.1  mrg 	if (!nowrap) {
   1569  1.1  mrg 		isl_seq_neg(bound, info->bmap->ineq[k], 1 + total);
   1570  1.1  mrg 
   1571  1.1  mrg 		if (add_selected_wraps(wraps, info, bound, set, add_valid) < 0)
   1572  1.1  mrg 			return isl_stat_error;
   1573  1.1  mrg 	}
   1574  1.1  mrg 
   1575  1.1  mrg 	if (isl_tab_rollback(info->tab, snap) < 0)
   1576  1.1  mrg 		return isl_stat_error;
   1577  1.1  mrg 	if (nowrap)
   1578  1.1  mrg 		return wraps_mark_failed(wraps);
   1579  1.1  mrg 	if (check_wraps(wraps, n, info->tab, add_valid) < 0)
   1580  1.1  mrg 		return isl_stat_error;
   1581  1.1  mrg 
   1582  1.1  mrg 	return isl_stat_ok;
   1583  1.1  mrg }
   1584  1.1  mrg 
   1585  1.1  mrg /* Wrap the constraints of info->bmap that bound the facet defined
   1586  1.1  mrg  * by inequality "k" around (the opposite of) this inequality to
   1587  1.1  mrg  * include "set".  "bound" may be used to store the negated inequality.
   1588  1.1  mrg  * If any of the wrapped constraints turn out to be invalid for info->bmap
   1589  1.1  mrg  * itself, then mark "wraps" as failed.
   1590  1.1  mrg  */
   1591  1.1  mrg static isl_stat add_wraps_around_facet(struct isl_wraps *wraps,
   1592  1.1  mrg 	struct isl_coalesce_info *info, int k, isl_int *bound,
   1593  1.1  mrg 	__isl_keep isl_set *set)
   1594  1.1  mrg {
   1595  1.1  mrg 	return add_selected_wraps_around_facet(wraps, info, k, bound, set, 0);
   1596  1.1  mrg }
   1597  1.1  mrg 
   1598  1.1  mrg /* Wrap the (valid) constraints of info->bmap that bound the facet defined
   1599  1.1  mrg  * by inequality "k" around (the opposite of) this inequality to
   1600  1.1  mrg  * include "set" more tightly.
   1601  1.1  mrg  * "bound" may be used to store the negated inequality.
   1602  1.1  mrg  * Remove any wrapping constraints that turn out to be invalid
   1603  1.1  mrg  * for info->bmap itself.
   1604  1.1  mrg  */
   1605  1.1  mrg static isl_stat add_valid_wraps_around_facet(struct isl_wraps *wraps,
   1606  1.1  mrg 	struct isl_coalesce_info *info, int k, isl_int *bound,
   1607  1.1  mrg 	__isl_keep isl_set *set)
   1608  1.1  mrg {
   1609  1.1  mrg 	return add_selected_wraps_around_facet(wraps, info, k, bound, set, 1);
   1610  1.1  mrg }
   1611  1.1  mrg 
   1612  1.1  mrg /* Basic map "i" has an inequality (say "k") that is adjacent
   1613  1.1  mrg  * to some inequality of basic map "j".  All the other inequalities
   1614  1.1  mrg  * are valid for "j".
   1615  1.1  mrg  * Check if basic map "j" forms an extension of basic map "i".
   1616  1.1  mrg  *
   1617  1.1  mrg  * Note that this function is only called if some of the equalities or
   1618  1.1  mrg  * inequalities of basic map "j" do cut basic map "i".  The function is
   1619  1.1  mrg  * correct even if there are no such cut constraints, but in that case
   1620  1.1  mrg  * the additional checks performed by this function are overkill.
   1621  1.1  mrg  *
   1622  1.1  mrg  * First try and wrap the ridges of "k" around "j".
   1623  1.1  mrg  * Note that those ridges are already valid for "j",
   1624  1.1  mrg  * but the wrapped versions may wrap "j" more tightly,
   1625  1.1  mrg  * increasing the chances of "j" being detected as an extension of "i"
   1626  1.1  mrg  */
   1627  1.1  mrg static enum isl_change is_adj_ineq_extension(int i, int j,
   1628  1.1  mrg 	struct isl_coalesce_info *info)
   1629  1.1  mrg {
   1630  1.1  mrg 	int k;
   1631  1.1  mrg 	enum isl_change change;
   1632  1.1  mrg 	isl_size total;
   1633  1.1  mrg 	isl_size n_eq_i, n_ineq_i;
   1634  1.1  mrg 	struct isl_wraps wraps;
   1635  1.1  mrg 	isl_ctx *ctx;
   1636  1.1  mrg 	isl_mat *mat;
   1637  1.1  mrg 	isl_vec *bound;
   1638  1.1  mrg 	isl_set *set_j;
   1639  1.1  mrg 	isl_stat r;
   1640  1.1  mrg 
   1641  1.1  mrg 	k = find_ineq(&info[i], STATUS_ADJ_INEQ);
   1642  1.1  mrg 	if (k < 0)
   1643  1.1  mrg 		isl_die(isl_basic_map_get_ctx(info[i].bmap), isl_error_internal,
   1644  1.1  mrg 			"info[i].ineq should have exactly one STATUS_ADJ_INEQ",
   1645  1.1  mrg 			return isl_change_error);
   1646  1.1  mrg 
   1647  1.1  mrg 	total = isl_basic_map_dim(info[i].bmap, isl_dim_all);
   1648  1.1  mrg 	n_eq_i = isl_basic_map_n_equality(info[i].bmap);
   1649  1.1  mrg 	n_ineq_i = isl_basic_map_n_inequality(info[i].bmap);
   1650  1.1  mrg 	if (total < 0 || n_eq_i < 0 || n_ineq_i < 0)
   1651  1.1  mrg 		return isl_change_error;
   1652  1.1  mrg 
   1653  1.1  mrg 	set_j = set_from_updated_bmap(info[j].bmap, info[j].tab);
   1654  1.1  mrg 	ctx = isl_basic_map_get_ctx(info[i].bmap);
   1655  1.1  mrg 	bound = isl_vec_alloc(ctx, 1 + total);
   1656  1.1  mrg 	mat = isl_mat_alloc(ctx, 2 * n_eq_i + n_ineq_i, 1 + total);
   1657  1.1  mrg 	if (wraps_init(&wraps, mat, info, i, j) < 0)
   1658  1.1  mrg 		goto error;
   1659  1.1  mrg 	if (!bound || !set_j)
   1660  1.1  mrg 		goto error;
   1661  1.1  mrg 	r = add_valid_wraps_around_facet(&wraps, &info[i], k, bound->el, set_j);
   1662  1.1  mrg 	if (r < 0)
   1663  1.1  mrg 		goto error;
   1664  1.1  mrg 
   1665  1.1  mrg 	change = is_adj_ineq_extension_with_wraps(i, j, k, info, wraps.mat);
   1666  1.1  mrg 
   1667  1.1  mrg 	wraps_free(&wraps);
   1668  1.1  mrg 	isl_vec_free(bound);
   1669  1.1  mrg 	isl_set_free(set_j);
   1670  1.1  mrg 
   1671  1.1  mrg 	return change;
   1672  1.1  mrg error:
   1673  1.1  mrg 	wraps_free(&wraps);
   1674  1.1  mrg 	isl_vec_free(bound);
   1675  1.1  mrg 	isl_set_free(set_j);
   1676  1.1  mrg 	return isl_change_error;
   1677  1.1  mrg }
   1678  1.1  mrg 
   1679  1.1  mrg /* Both basic maps have at least one inequality with and adjacent
   1680  1.1  mrg  * (but opposite) inequality in the other basic map.
   1681  1.1  mrg  * Check that there are no cut constraints and that there is only
   1682  1.1  mrg  * a single pair of adjacent inequalities.
   1683  1.1  mrg  * If so, we can replace the pair by a single basic map described
   1684  1.1  mrg  * by all but the pair of adjacent inequalities.
   1685  1.1  mrg  * Any additional points introduced lie strictly between the two
   1686  1.1  mrg  * adjacent hyperplanes and can therefore be integral.
   1687  1.1  mrg  *
   1688  1.1  mrg  *        ____			  _____
   1689  1.1  mrg  *       /    ||\		 /     \
   1690  1.1  mrg  *      /     || \		/       \
   1691  1.1  mrg  *      \     ||  \	=>	\        \
   1692  1.1  mrg  *       \    ||  /		 \       /
   1693  1.1  mrg  *        \___||_/		  \_____/
   1694  1.1  mrg  *
   1695  1.1  mrg  * The test for a single pair of adjacent inequalities is important
   1696  1.1  mrg  * for avoiding the combination of two basic maps like the following
   1697  1.1  mrg  *
   1698  1.1  mrg  *       /|
   1699  1.1  mrg  *      / |
   1700  1.1  mrg  *     /__|
   1701  1.1  mrg  *         _____
   1702  1.1  mrg  *         |   |
   1703  1.1  mrg  *         |   |
   1704  1.1  mrg  *         |___|
   1705  1.1  mrg  *
   1706  1.1  mrg  * If there are some cut constraints on one side, then we may
   1707  1.1  mrg  * still be able to fuse the two basic maps, but we need to perform
   1708  1.1  mrg  * some additional checks in is_adj_ineq_extension.
   1709  1.1  mrg  */
   1710  1.1  mrg static enum isl_change check_adj_ineq(int i, int j,
   1711  1.1  mrg 	struct isl_coalesce_info *info)
   1712  1.1  mrg {
   1713  1.1  mrg 	int count_i, count_j;
   1714  1.1  mrg 	int cut_i, cut_j;
   1715  1.1  mrg 
   1716  1.1  mrg 	count_i = count_ineq(&info[i], STATUS_ADJ_INEQ);
   1717  1.1  mrg 	count_j = count_ineq(&info[j], STATUS_ADJ_INEQ);
   1718  1.1  mrg 
   1719  1.1  mrg 	if (count_i != 1 && count_j != 1)
   1720  1.1  mrg 		return isl_change_none;
   1721  1.1  mrg 
   1722  1.1  mrg 	cut_i = any_eq(&info[i], STATUS_CUT) || any_ineq(&info[i], STATUS_CUT);
   1723  1.1  mrg 	cut_j = any_eq(&info[j], STATUS_CUT) || any_ineq(&info[j], STATUS_CUT);
   1724  1.1  mrg 
   1725  1.1  mrg 	if (!cut_i && !cut_j && count_i == 1 && count_j == 1)
   1726  1.1  mrg 		return fuse(i, j, info, NULL, 0, 0);
   1727  1.1  mrg 
   1728  1.1  mrg 	if (count_i == 1 && !cut_i)
   1729  1.1  mrg 		return is_adj_ineq_extension(i, j, info);
   1730  1.1  mrg 
   1731  1.1  mrg 	if (count_j == 1 && !cut_j)
   1732  1.1  mrg 		return is_adj_ineq_extension(j, i, info);
   1733  1.1  mrg 
   1734  1.1  mrg 	return isl_change_none;
   1735  1.1  mrg }
   1736  1.1  mrg 
   1737  1.1  mrg /* Given a basic set i with a constraint k that is adjacent to
   1738  1.1  mrg  * basic set j, check if we can wrap
   1739  1.1  mrg  * both the facet corresponding to k (if "wrap_facet" is set) and basic map j
   1740  1.1  mrg  * (always) around their ridges to include the other set.
   1741  1.1  mrg  * If so, replace the pair of basic sets by their union.
   1742  1.1  mrg  *
   1743  1.1  mrg  * All constraints of i (except k) are assumed to be valid or
   1744  1.1  mrg  * cut constraints for j.
   1745  1.1  mrg  * Wrapping the cut constraints to include basic map j may result
   1746  1.1  mrg  * in constraints that are no longer valid of basic map i
   1747  1.1  mrg  * we have to check that the resulting wrapping constraints are valid for i.
   1748  1.1  mrg  * If "wrap_facet" is not set, then all constraints of i (except k)
   1749  1.1  mrg  * are assumed to be valid for j.
   1750  1.1  mrg  *        ____			  _____
   1751  1.1  mrg  *       /    | 		 /     \
   1752  1.1  mrg  *      /     ||  		/      |
   1753  1.1  mrg  *      \     ||   	=>	\      |
   1754  1.1  mrg  *       \    ||		 \     |
   1755  1.1  mrg  *        \___||		  \____|
   1756  1.1  mrg  *
   1757  1.1  mrg  */
   1758  1.1  mrg static enum isl_change can_wrap_in_facet(int i, int j, int k,
   1759  1.1  mrg 	struct isl_coalesce_info *info, int wrap_facet)
   1760  1.1  mrg {
   1761  1.1  mrg 	enum isl_change change = isl_change_none;
   1762  1.1  mrg 	struct isl_wraps wraps;
   1763  1.1  mrg 	isl_ctx *ctx;
   1764  1.1  mrg 	isl_mat *mat;
   1765  1.1  mrg 	struct isl_set *set_i = NULL;
   1766  1.1  mrg 	struct isl_set *set_j = NULL;
   1767  1.1  mrg 	struct isl_vec *bound = NULL;
   1768  1.1  mrg 	isl_size total = isl_basic_map_dim(info[i].bmap, isl_dim_all);
   1769  1.1  mrg 
   1770  1.1  mrg 	if (total < 0)
   1771  1.1  mrg 		return isl_change_error;
   1772  1.1  mrg 	set_i = set_from_updated_bmap(info[i].bmap, info[i].tab);
   1773  1.1  mrg 	set_j = set_from_updated_bmap(info[j].bmap, info[j].tab);
   1774  1.1  mrg 	ctx = isl_basic_map_get_ctx(info[i].bmap);
   1775  1.1  mrg 	mat = isl_mat_alloc(ctx, 2 * (info[i].bmap->n_eq + info[j].bmap->n_eq) +
   1776  1.1  mrg 				    info[i].bmap->n_ineq + info[j].bmap->n_ineq,
   1777  1.1  mrg 				    1 + total);
   1778  1.1  mrg 	if (wraps_init(&wraps, mat, info, i, j) < 0)
   1779  1.1  mrg 		goto error;
   1780  1.1  mrg 	bound = isl_vec_alloc(ctx, 1 + total);
   1781  1.1  mrg 	if (!set_i || !set_j || !bound)
   1782  1.1  mrg 		goto error;
   1783  1.1  mrg 
   1784  1.1  mrg 	isl_seq_cpy(bound->el, info[i].bmap->ineq[k], 1 + total);
   1785  1.1  mrg 	isl_int_add_ui(bound->el[0], bound->el[0], 1);
   1786  1.1  mrg 	isl_seq_normalize(ctx, bound->el, 1 + total);
   1787  1.1  mrg 
   1788  1.1  mrg 	isl_seq_cpy(wraps.mat->row[0], bound->el, 1 + total);
   1789  1.1  mrg 	wraps.mat->n_row = 1;
   1790  1.1  mrg 
   1791  1.1  mrg 	if (add_wraps(&wraps, &info[j], bound->el, set_i) < 0)
   1792  1.1  mrg 		goto error;
   1793  1.1  mrg 	if (wraps.failed)
   1794  1.1  mrg 		goto unbounded;
   1795  1.1  mrg 
   1796  1.1  mrg 	if (wrap_facet) {
   1797  1.1  mrg 		if (add_wraps_around_facet(&wraps, &info[i], k,
   1798  1.1  mrg 					    bound->el, set_j) < 0)
   1799  1.1  mrg 			goto error;
   1800  1.1  mrg 		if (wraps.failed)
   1801  1.1  mrg 			goto unbounded;
   1802  1.1  mrg 	}
   1803  1.1  mrg 
   1804  1.1  mrg 	change = fuse(i, j, info, wraps.mat, 0, 0);
   1805  1.1  mrg 
   1806  1.1  mrg unbounded:
   1807  1.1  mrg 	wraps_free(&wraps);
   1808  1.1  mrg 
   1809  1.1  mrg 	isl_set_free(set_i);
   1810  1.1  mrg 	isl_set_free(set_j);
   1811  1.1  mrg 
   1812  1.1  mrg 	isl_vec_free(bound);
   1813  1.1  mrg 
   1814  1.1  mrg 	return change;
   1815  1.1  mrg error:
   1816  1.1  mrg 	wraps_free(&wraps);
   1817  1.1  mrg 	isl_vec_free(bound);
   1818  1.1  mrg 	isl_set_free(set_i);
   1819  1.1  mrg 	isl_set_free(set_j);
   1820  1.1  mrg 	return isl_change_error;
   1821  1.1  mrg }
   1822  1.1  mrg 
   1823  1.1  mrg /* Given a cut constraint t(x) >= 0 of basic map i, stored in row "w"
   1824  1.1  mrg  * of wrap.mat, replace it by its relaxed version t(x) + 1 >= 0, and
   1825  1.1  mrg  * add wrapping constraints to wrap.mat for all constraints
   1826  1.1  mrg  * of basic map j that bound the part of basic map j that sticks out
   1827  1.1  mrg  * of the cut constraint.
   1828  1.1  mrg  * "set_i" is the underlying set of basic map i.
   1829  1.1  mrg  * If any wrapping fails, then wraps->mat.n_row is reset to zero.
   1830  1.1  mrg  *
   1831  1.1  mrg  * In particular, we first intersect basic map j with t(x) + 1 = 0.
   1832  1.1  mrg  * If the result is empty, then t(x) >= 0 was actually a valid constraint
   1833  1.1  mrg  * (with respect to the integer points), so we add t(x) >= 0 instead.
   1834  1.1  mrg  * Otherwise, we wrap the constraints of basic map j that are not
   1835  1.1  mrg  * redundant in this intersection and that are not already valid
   1836  1.1  mrg  * for basic map i over basic map i.
   1837  1.1  mrg  * Note that it is sufficient to wrap the constraints to include
   1838  1.1  mrg  * basic map i, because we will only wrap the constraints that do
   1839  1.1  mrg  * not include basic map i already.  The wrapped constraint will
   1840  1.1  mrg  * therefore be more relaxed compared to the original constraint.
   1841  1.1  mrg  * Since the original constraint is valid for basic map j, so is
   1842  1.1  mrg  * the wrapped constraint.
   1843  1.1  mrg  */
   1844  1.1  mrg static isl_stat wrap_in_facet(struct isl_wraps *wraps, int w,
   1845  1.1  mrg 	struct isl_coalesce_info *info_j, __isl_keep isl_set *set_i,
   1846  1.1  mrg 	struct isl_tab_undo *snap)
   1847  1.1  mrg {
   1848  1.1  mrg 	isl_int_add_ui(wraps->mat->row[w][0], wraps->mat->row[w][0], 1);
   1849  1.1  mrg 	if (isl_tab_add_eq(info_j->tab, wraps->mat->row[w]) < 0)
   1850  1.1  mrg 		return isl_stat_error;
   1851  1.1  mrg 	if (isl_tab_detect_redundant(info_j->tab) < 0)
   1852  1.1  mrg 		return isl_stat_error;
   1853  1.1  mrg 
   1854  1.1  mrg 	if (info_j->tab->empty)
   1855  1.1  mrg 		isl_int_sub_ui(wraps->mat->row[w][0], wraps->mat->row[w][0], 1);
   1856  1.1  mrg 	else if (add_wraps(wraps, info_j, wraps->mat->row[w], set_i) < 0)
   1857  1.1  mrg 		return isl_stat_error;
   1858  1.1  mrg 
   1859  1.1  mrg 	if (isl_tab_rollback(info_j->tab, snap) < 0)
   1860  1.1  mrg 		return isl_stat_error;
   1861  1.1  mrg 
   1862  1.1  mrg 	return isl_stat_ok;
   1863  1.1  mrg }
   1864  1.1  mrg 
   1865  1.1  mrg /* Given a pair of basic maps i and j such that j sticks out
   1866  1.1  mrg  * of i at n cut constraints, each time by at most one,
   1867  1.1  mrg  * try to compute wrapping constraints and replace the two
   1868  1.1  mrg  * basic maps by a single basic map.
   1869  1.1  mrg  * The other constraints of i are assumed to be valid for j.
   1870  1.1  mrg  * "set_i" is the underlying set of basic map i.
   1871  1.1  mrg  * "wraps" has been initialized to be of the right size.
   1872  1.1  mrg  *
   1873  1.1  mrg  * For each cut constraint t(x) >= 0 of i, we add the relaxed version
   1874  1.1  mrg  * t(x) + 1 >= 0, along with wrapping constraints for all constraints
   1875  1.1  mrg  * of basic map j that bound the part of basic map j that sticks out
   1876  1.1  mrg  * of the cut constraint.
   1877  1.1  mrg  *
   1878  1.1  mrg  * If any wrapping fails, i.e., if we cannot wrap to touch
   1879  1.1  mrg  * the union, then we give up.
   1880  1.1  mrg  * Otherwise, the pair of basic maps is replaced by their union.
   1881  1.1  mrg  */
   1882  1.1  mrg static enum isl_change try_wrap_in_facets(int i, int j,
   1883  1.1  mrg 	struct isl_coalesce_info *info, struct isl_wraps *wraps,
   1884  1.1  mrg 	__isl_keep isl_set *set_i)
   1885  1.1  mrg {
   1886  1.1  mrg 	int k, l, w;
   1887  1.1  mrg 	isl_size total;
   1888  1.1  mrg 	struct isl_tab_undo *snap;
   1889  1.1  mrg 
   1890  1.1  mrg 	total = isl_basic_map_dim(info[i].bmap, isl_dim_all);
   1891  1.1  mrg 	if (total < 0)
   1892  1.1  mrg 		return isl_change_error;
   1893  1.1  mrg 
   1894  1.1  mrg 	snap = isl_tab_snap(info[j].tab);
   1895  1.1  mrg 
   1896  1.1  mrg 	for (k = 0; k < info[i].bmap->n_eq; ++k) {
   1897  1.1  mrg 		for (l = 0; l < 2; ++l) {
   1898  1.1  mrg 			if (info[i].eq[2 * k + l] != STATUS_CUT)
   1899  1.1  mrg 				continue;
   1900  1.1  mrg 			w = wraps->mat->n_row++;
   1901  1.1  mrg 			if (l == 0)
   1902  1.1  mrg 				isl_seq_neg(wraps->mat->row[w],
   1903  1.1  mrg 					    info[i].bmap->eq[k], 1 + total);
   1904  1.1  mrg 			else
   1905  1.1  mrg 				isl_seq_cpy(wraps->mat->row[w],
   1906  1.1  mrg 					    info[i].bmap->eq[k], 1 + total);
   1907  1.1  mrg 			if (wrap_in_facet(wraps, w, &info[j], set_i, snap) < 0)
   1908  1.1  mrg 				return isl_change_error;
   1909  1.1  mrg 
   1910  1.1  mrg 			if (wraps->failed)
   1911  1.1  mrg 				return isl_change_none;
   1912  1.1  mrg 		}
   1913  1.1  mrg 	}
   1914  1.1  mrg 
   1915  1.1  mrg 	for (k = 0; k < info[i].bmap->n_ineq; ++k) {
   1916  1.1  mrg 		if (info[i].ineq[k] != STATUS_CUT)
   1917  1.1  mrg 			continue;
   1918  1.1  mrg 		w = wraps->mat->n_row++;
   1919  1.1  mrg 		isl_seq_cpy(wraps->mat->row[w],
   1920  1.1  mrg 			    info[i].bmap->ineq[k], 1 + total);
   1921  1.1  mrg 		if (wrap_in_facet(wraps, w, &info[j], set_i, snap) < 0)
   1922  1.1  mrg 			return isl_change_error;
   1923  1.1  mrg 
   1924  1.1  mrg 		if (wraps->failed)
   1925  1.1  mrg 			return isl_change_none;
   1926  1.1  mrg 	}
   1927  1.1  mrg 
   1928  1.1  mrg 	return fuse(i, j, info, wraps->mat, 0, 1);
   1929  1.1  mrg }
   1930  1.1  mrg 
   1931  1.1  mrg /* Given a pair of basic maps i and j such that j sticks out
   1932  1.1  mrg  * of i at n cut constraints, each time by at most one,
   1933  1.1  mrg  * try to compute wrapping constraints and replace the two
   1934  1.1  mrg  * basic maps by a single basic map.
   1935  1.1  mrg  * The other constraints of i are assumed to be valid for j.
   1936  1.1  mrg  *
   1937  1.1  mrg  * The core computation is performed by try_wrap_in_facets.
   1938  1.1  mrg  * This function simply extracts an underlying set representation
   1939  1.1  mrg  * of basic map i and initializes the data structure for keeping
   1940  1.1  mrg  * track of wrapping constraints.
   1941  1.1  mrg  */
   1942  1.1  mrg static enum isl_change wrap_in_facets(int i, int j, int n,
   1943  1.1  mrg 	struct isl_coalesce_info *info)
   1944  1.1  mrg {
   1945  1.1  mrg 	enum isl_change change = isl_change_none;
   1946  1.1  mrg 	struct isl_wraps wraps;
   1947  1.1  mrg 	isl_ctx *ctx;
   1948  1.1  mrg 	isl_mat *mat;
   1949  1.1  mrg 	isl_set *set_i = NULL;
   1950  1.1  mrg 	isl_size total = isl_basic_map_dim(info[i].bmap, isl_dim_all);
   1951  1.1  mrg 	int max_wrap;
   1952  1.1  mrg 
   1953  1.1  mrg 	if (total < 0)
   1954  1.1  mrg 		return isl_change_error;
   1955  1.1  mrg 	if (isl_tab_extend_cons(info[j].tab, 1) < 0)
   1956  1.1  mrg 		return isl_change_error;
   1957  1.1  mrg 
   1958  1.1  mrg 	max_wrap = 1 + 2 * info[j].bmap->n_eq + info[j].bmap->n_ineq;
   1959  1.1  mrg 	max_wrap *= n;
   1960  1.1  mrg 
   1961  1.1  mrg 	set_i = set_from_updated_bmap(info[i].bmap, info[i].tab);
   1962  1.1  mrg 	ctx = isl_basic_map_get_ctx(info[i].bmap);
   1963  1.1  mrg 	mat = isl_mat_alloc(ctx, max_wrap, 1 + total);
   1964  1.1  mrg 	if (wraps_init(&wraps, mat, info, i, j) < 0)
   1965  1.1  mrg 		goto error;
   1966  1.1  mrg 	if (!set_i)
   1967  1.1  mrg 		goto error;
   1968  1.1  mrg 
   1969  1.1  mrg 	change = try_wrap_in_facets(i, j, info, &wraps, set_i);
   1970  1.1  mrg 
   1971  1.1  mrg 	wraps_free(&wraps);
   1972  1.1  mrg 	isl_set_free(set_i);
   1973  1.1  mrg 
   1974  1.1  mrg 	return change;
   1975  1.1  mrg error:
   1976  1.1  mrg 	wraps_free(&wraps);
   1977  1.1  mrg 	isl_set_free(set_i);
   1978  1.1  mrg 	return isl_change_error;
   1979  1.1  mrg }
   1980  1.1  mrg 
   1981  1.1  mrg /* Return the effect of inequality "ineq" on the tableau "tab",
   1982  1.1  mrg  * after relaxing the constant term of "ineq" by one.
   1983  1.1  mrg  */
   1984  1.1  mrg static enum isl_ineq_type type_of_relaxed(struct isl_tab *tab, isl_int *ineq)
   1985  1.1  mrg {
   1986  1.1  mrg 	enum isl_ineq_type type;
   1987  1.1  mrg 
   1988  1.1  mrg 	isl_int_add_ui(ineq[0], ineq[0], 1);
   1989  1.1  mrg 	type = isl_tab_ineq_type(tab, ineq);
   1990  1.1  mrg 	isl_int_sub_ui(ineq[0], ineq[0], 1);
   1991  1.1  mrg 
   1992  1.1  mrg 	return type;
   1993  1.1  mrg }
   1994  1.1  mrg 
   1995  1.1  mrg /* Given two basic sets i and j,
   1996  1.1  mrg  * check if relaxing all the cut constraints of i by one turns
   1997  1.1  mrg  * them into valid constraint for j and check if we can wrap in
   1998  1.1  mrg  * the bits that are sticking out.
   1999  1.1  mrg  * If so, replace the pair by their union.
   2000  1.1  mrg  *
   2001  1.1  mrg  * We first check if all relaxed cut inequalities of i are valid for j
   2002  1.1  mrg  * and then try to wrap in the intersections of the relaxed cut inequalities
   2003  1.1  mrg  * with j.
   2004  1.1  mrg  *
   2005  1.1  mrg  * During this wrapping, we consider the points of j that lie at a distance
   2006  1.1  mrg  * of exactly 1 from i.  In particular, we ignore the points that lie in
   2007  1.1  mrg  * between this lower-dimensional space and the basic map i.
   2008  1.1  mrg  * We can therefore only apply this to integer maps.
   2009  1.1  mrg  *        ____			  _____
   2010  1.1  mrg  *       / ___|_		 /     \
   2011  1.1  mrg  *      / |    |  		/      |
   2012  1.1  mrg  *      \ |    |   	=>	\      |
   2013  1.1  mrg  *       \|____|		 \     |
   2014  1.1  mrg  *        \___| 		  \____/
   2015  1.1  mrg  *
   2016  1.1  mrg  *	 _____			 ______
   2017  1.1  mrg  *	| ____|_		|      \
   2018  1.1  mrg  *	| |     |		|       |
   2019  1.1  mrg  *	| |	|	=>	|       |
   2020  1.1  mrg  *	|_|     |		|       |
   2021  1.1  mrg  *	  |_____|		 \______|
   2022  1.1  mrg  *
   2023  1.1  mrg  *	 _______
   2024  1.1  mrg  *	|       |
   2025  1.1  mrg  *	|  |\   |
   2026  1.1  mrg  *	|  | \  |
   2027  1.1  mrg  *	|  |  \ |
   2028  1.1  mrg  *	|  |   \|
   2029  1.1  mrg  *	|  |    \
   2030  1.1  mrg  *	|  |_____\
   2031  1.1  mrg  *	|       |
   2032  1.1  mrg  *	|_______|
   2033  1.1  mrg  *
   2034  1.1  mrg  * Wrapping can fail if the result of wrapping one of the facets
   2035  1.1  mrg  * around its edges does not produce any new facet constraint.
   2036  1.1  mrg  * In particular, this happens when we try to wrap in unbounded sets.
   2037  1.1  mrg  *
   2038  1.1  mrg  *	 _______________________________________________________________________
   2039  1.1  mrg  *	|
   2040  1.1  mrg  *	|  ___
   2041  1.1  mrg  *	| |   |
   2042  1.1  mrg  *	|_|   |_________________________________________________________________
   2043  1.1  mrg  *	  |___|
   2044  1.1  mrg  *
   2045  1.1  mrg  * The following is not an acceptable result of coalescing the above two
   2046  1.1  mrg  * sets as it includes extra integer points.
   2047  1.1  mrg  *	 _______________________________________________________________________
   2048  1.1  mrg  *	|
   2049  1.1  mrg  *	|
   2050  1.1  mrg  *	|
   2051  1.1  mrg  *	|
   2052  1.1  mrg  *	 \______________________________________________________________________
   2053  1.1  mrg  */
   2054  1.1  mrg static enum isl_change can_wrap_in_set(int i, int j,
   2055  1.1  mrg 	struct isl_coalesce_info *info)
   2056  1.1  mrg {
   2057  1.1  mrg 	int k, l;
   2058  1.1  mrg 	int n;
   2059  1.1  mrg 	isl_size total;
   2060  1.1  mrg 
   2061  1.1  mrg 	if (ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_RATIONAL) ||
   2062  1.1  mrg 	    ISL_F_ISSET(info[j].bmap, ISL_BASIC_MAP_RATIONAL))
   2063  1.1  mrg 		return isl_change_none;
   2064  1.1  mrg 
   2065  1.1  mrg 	n = count_eq(&info[i], STATUS_CUT) + count_ineq(&info[i], STATUS_CUT);
   2066  1.1  mrg 	if (n == 0)
   2067  1.1  mrg 		return isl_change_none;
   2068  1.1  mrg 
   2069  1.1  mrg 	total = isl_basic_map_dim(info[i].bmap, isl_dim_all);
   2070  1.1  mrg 	if (total < 0)
   2071  1.1  mrg 		return isl_change_error;
   2072  1.1  mrg 	for (k = 0; k < info[i].bmap->n_eq; ++k) {
   2073  1.1  mrg 		for (l = 0; l < 2; ++l) {
   2074  1.1  mrg 			enum isl_ineq_type type;
   2075  1.1  mrg 
   2076  1.1  mrg 			if (info[i].eq[2 * k + l] != STATUS_CUT)
   2077  1.1  mrg 				continue;
   2078  1.1  mrg 
   2079  1.1  mrg 			if (l == 0)
   2080  1.1  mrg 				isl_seq_neg(info[i].bmap->eq[k],
   2081  1.1  mrg 					    info[i].bmap->eq[k], 1 + total);
   2082  1.1  mrg 			type = type_of_relaxed(info[j].tab,
   2083  1.1  mrg 					    info[i].bmap->eq[k]);
   2084  1.1  mrg 			if (l == 0)
   2085  1.1  mrg 				isl_seq_neg(info[i].bmap->eq[k],
   2086  1.1  mrg 					    info[i].bmap->eq[k], 1 + total);
   2087  1.1  mrg 			if (type == isl_ineq_error)
   2088  1.1  mrg 				return isl_change_error;
   2089  1.1  mrg 			if (type != isl_ineq_redundant)
   2090  1.1  mrg 				return isl_change_none;
   2091  1.1  mrg 		}
   2092  1.1  mrg 	}
   2093  1.1  mrg 
   2094  1.1  mrg 	for (k = 0; k < info[i].bmap->n_ineq; ++k) {
   2095  1.1  mrg 		enum isl_ineq_type type;
   2096  1.1  mrg 
   2097  1.1  mrg 		if (info[i].ineq[k] != STATUS_CUT)
   2098  1.1  mrg 			continue;
   2099  1.1  mrg 
   2100  1.1  mrg 		type = type_of_relaxed(info[j].tab, info[i].bmap->ineq[k]);
   2101  1.1  mrg 		if (type == isl_ineq_error)
   2102  1.1  mrg 			return isl_change_error;
   2103  1.1  mrg 		if (type != isl_ineq_redundant)
   2104  1.1  mrg 			return isl_change_none;
   2105  1.1  mrg 	}
   2106  1.1  mrg 
   2107  1.1  mrg 	return wrap_in_facets(i, j, n, info);
   2108  1.1  mrg }
   2109  1.1  mrg 
   2110  1.1  mrg /* Check if either i or j has only cut constraints that can
   2111  1.1  mrg  * be used to wrap in (a facet of) the other basic set.
   2112  1.1  mrg  * if so, replace the pair by their union.
   2113  1.1  mrg  */
   2114  1.1  mrg static enum isl_change check_wrap(int i, int j, struct isl_coalesce_info *info)
   2115  1.1  mrg {
   2116  1.1  mrg 	enum isl_change change = isl_change_none;
   2117  1.1  mrg 
   2118  1.1  mrg 	change = can_wrap_in_set(i, j, info);
   2119  1.1  mrg 	if (change != isl_change_none)
   2120  1.1  mrg 		return change;
   2121  1.1  mrg 
   2122  1.1  mrg 	change = can_wrap_in_set(j, i, info);
   2123  1.1  mrg 	return change;
   2124  1.1  mrg }
   2125  1.1  mrg 
   2126  1.1  mrg /* Check if all inequality constraints of "i" that cut "j" cease
   2127  1.1  mrg  * to be cut constraints if they are relaxed by one.
   2128  1.1  mrg  * If so, collect the cut constraints in "list".
   2129  1.1  mrg  * The caller is responsible for allocating "list".
   2130  1.1  mrg  */
   2131  1.1  mrg static isl_bool all_cut_by_one(int i, int j, struct isl_coalesce_info *info,
   2132  1.1  mrg 	int *list)
   2133  1.1  mrg {
   2134  1.1  mrg 	int l, n;
   2135  1.1  mrg 
   2136  1.1  mrg 	n = 0;
   2137  1.1  mrg 	for (l = 0; l < info[i].bmap->n_ineq; ++l) {
   2138  1.1  mrg 		enum isl_ineq_type type;
   2139  1.1  mrg 
   2140  1.1  mrg 		if (info[i].ineq[l] != STATUS_CUT)
   2141  1.1  mrg 			continue;
   2142  1.1  mrg 		type = type_of_relaxed(info[j].tab, info[i].bmap->ineq[l]);
   2143  1.1  mrg 		if (type == isl_ineq_error)
   2144  1.1  mrg 			return isl_bool_error;
   2145  1.1  mrg 		if (type != isl_ineq_redundant)
   2146  1.1  mrg 			return isl_bool_false;
   2147  1.1  mrg 		list[n++] = l;
   2148  1.1  mrg 	}
   2149  1.1  mrg 
   2150  1.1  mrg 	return isl_bool_true;
   2151  1.1  mrg }
   2152  1.1  mrg 
   2153  1.1  mrg /* Given two basic maps such that "j" has at least one equality constraint
   2154  1.1  mrg  * that is adjacent to an inequality constraint of "i" and such that "i" has
   2155  1.1  mrg  * exactly one inequality constraint that is adjacent to an equality
   2156  1.1  mrg  * constraint of "j", check whether "i" can be extended to include "j" or
   2157  1.1  mrg  * whether "j" can be wrapped into "i".
   2158  1.1  mrg  * All remaining constraints of "i" and "j" are assumed to be valid
   2159  1.1  mrg  * or cut constraints of the other basic map.
   2160  1.1  mrg  * However, none of the equality constraints of "i" are cut constraints.
   2161  1.1  mrg  *
   2162  1.1  mrg  * If "i" has any "cut" inequality constraints, then check if relaxing
   2163  1.1  mrg  * each of them by one is sufficient for them to become valid.
   2164  1.1  mrg  * If so, check if the inequality constraint adjacent to an equality
   2165  1.1  mrg  * constraint of "j" along with all these cut constraints
   2166  1.1  mrg  * can be relaxed by one to contain exactly "j".
   2167  1.1  mrg  * Otherwise, or if this fails, check if "j" can be wrapped into "i".
   2168  1.1  mrg  */
   2169  1.1  mrg static enum isl_change check_single_adj_eq(int i, int j,
   2170  1.1  mrg 	struct isl_coalesce_info *info)
   2171  1.1  mrg {
   2172  1.1  mrg 	enum isl_change change = isl_change_none;
   2173  1.1  mrg 	int k;
   2174  1.1  mrg 	int n_cut;
   2175  1.1  mrg 	int *relax;
   2176  1.1  mrg 	isl_ctx *ctx;
   2177  1.1  mrg 	isl_bool try_relax;
   2178  1.1  mrg 
   2179  1.1  mrg 	n_cut = count_ineq(&info[i], STATUS_CUT);
   2180  1.1  mrg 
   2181  1.1  mrg 	k = find_ineq(&info[i], STATUS_ADJ_EQ);
   2182  1.1  mrg 
   2183  1.1  mrg 	if (n_cut > 0) {
   2184  1.1  mrg 		ctx = isl_basic_map_get_ctx(info[i].bmap);
   2185  1.1  mrg 		relax = isl_calloc_array(ctx, int, 1 + n_cut);
   2186  1.1  mrg 		if (!relax)
   2187  1.1  mrg 			return isl_change_error;
   2188  1.1  mrg 		relax[0] = k;
   2189  1.1  mrg 		try_relax = all_cut_by_one(i, j, info, relax + 1);
   2190  1.1  mrg 		if (try_relax < 0)
   2191  1.1  mrg 			change = isl_change_error;
   2192  1.1  mrg 	} else {
   2193  1.1  mrg 		try_relax = isl_bool_true;
   2194  1.1  mrg 		relax = &k;
   2195  1.1  mrg 	}
   2196  1.1  mrg 	if (try_relax && change == isl_change_none)
   2197  1.1  mrg 		change = is_relaxed_extension(i, j, 1 + n_cut, relax, info);
   2198  1.1  mrg 	if (n_cut > 0)
   2199  1.1  mrg 		free(relax);
   2200  1.1  mrg 	if (change != isl_change_none)
   2201  1.1  mrg 		return change;
   2202  1.1  mrg 
   2203  1.1  mrg 	change = can_wrap_in_facet(i, j, k, info, n_cut > 0);
   2204  1.1  mrg 
   2205  1.1  mrg 	return change;
   2206  1.1  mrg }
   2207  1.1  mrg 
   2208  1.1  mrg /* At least one of the basic maps has an equality that is adjacent
   2209  1.1  mrg  * to an inequality.  Make sure that only one of the basic maps has
   2210  1.1  mrg  * such an equality and that the other basic map has exactly one
   2211  1.1  mrg  * inequality adjacent to an equality.
   2212  1.1  mrg  * If the other basic map does not have such an inequality, then
   2213  1.1  mrg  * check if all its constraints are either valid or cut constraints
   2214  1.1  mrg  * and, if so, try wrapping in the first map into the second.
   2215  1.1  mrg  * Otherwise, try to extend one basic map with the other or
   2216  1.1  mrg  * wrap one basic map in the other.
   2217  1.1  mrg  */
   2218  1.1  mrg static enum isl_change check_adj_eq(int i, int j,
   2219  1.1  mrg 	struct isl_coalesce_info *info)
   2220  1.1  mrg {
   2221  1.1  mrg 	if (any_eq(&info[i], STATUS_ADJ_INEQ) &&
   2222  1.1  mrg 	    any_eq(&info[j], STATUS_ADJ_INEQ))
   2223  1.1  mrg 		/* ADJ EQ TOO MANY */
   2224  1.1  mrg 		return isl_change_none;
   2225  1.1  mrg 
   2226  1.1  mrg 	if (any_eq(&info[i], STATUS_ADJ_INEQ))
   2227  1.1  mrg 		return check_adj_eq(j, i, info);
   2228  1.1  mrg 
   2229  1.1  mrg 	/* j has an equality adjacent to an inequality in i */
   2230  1.1  mrg 
   2231  1.1  mrg 	if (count_ineq(&info[i], STATUS_ADJ_EQ) != 1) {
   2232  1.1  mrg 		if (all_valid_or_cut(&info[i]))
   2233  1.1  mrg 			return can_wrap_in_set(i, j, info);
   2234  1.1  mrg 		return isl_change_none;
   2235  1.1  mrg 	}
   2236  1.1  mrg 	if (any_eq(&info[i], STATUS_CUT))
   2237  1.1  mrg 		return isl_change_none;
   2238  1.1  mrg 	if (any_ineq(&info[j], STATUS_ADJ_EQ) ||
   2239  1.1  mrg 	    any_ineq(&info[i], STATUS_ADJ_INEQ) ||
   2240  1.1  mrg 	    any_ineq(&info[j], STATUS_ADJ_INEQ))
   2241  1.1  mrg 		/* ADJ EQ TOO MANY */
   2242  1.1  mrg 		return isl_change_none;
   2243  1.1  mrg 
   2244  1.1  mrg 	return check_single_adj_eq(i, j, info);
   2245  1.1  mrg }
   2246  1.1  mrg 
   2247  1.1  mrg /* Disjunct "j" lies on a hyperplane that is adjacent to disjunct "i".
   2248  1.1  mrg  * In particular, disjunct "i" has an inequality constraint that is adjacent
   2249  1.1  mrg  * to a (combination of) equality constraint(s) of disjunct "j",
   2250  1.1  mrg  * but disjunct "j" has no explicit equality constraint adjacent
   2251  1.1  mrg  * to an inequality constraint of disjunct "i".
   2252  1.1  mrg  *
   2253  1.1  mrg  * Disjunct "i" is already known not to have any equality constraints
   2254  1.1  mrg  * that are adjacent to an equality or inequality constraint.
   2255  1.1  mrg  * Check that, other than the inequality constraint mentioned above,
   2256  1.1  mrg  * all other constraints of disjunct "i" are valid for disjunct "j".
   2257  1.1  mrg  * If so, try and wrap in disjunct "j".
   2258  1.1  mrg  */
   2259  1.1  mrg static enum isl_change check_ineq_adj_eq(int i, int j,
   2260  1.1  mrg 	struct isl_coalesce_info *info)
   2261  1.1  mrg {
   2262  1.1  mrg 	int k;
   2263  1.1  mrg 
   2264  1.1  mrg 	if (any_eq(&info[i], STATUS_CUT))
   2265  1.1  mrg 		return isl_change_none;
   2266  1.1  mrg 	if (any_ineq(&info[i], STATUS_CUT))
   2267  1.1  mrg 		return isl_change_none;
   2268  1.1  mrg 	if (any_ineq(&info[i], STATUS_ADJ_INEQ))
   2269  1.1  mrg 		return isl_change_none;
   2270  1.1  mrg 	if (count_ineq(&info[i], STATUS_ADJ_EQ) != 1)
   2271  1.1  mrg 		return isl_change_none;
   2272  1.1  mrg 
   2273  1.1  mrg 	k = find_ineq(&info[i], STATUS_ADJ_EQ);
   2274  1.1  mrg 
   2275  1.1  mrg 	return can_wrap_in_facet(i, j, k, info, 0);
   2276  1.1  mrg }
   2277  1.1  mrg 
   2278  1.1  mrg /* The two basic maps lie on adjacent hyperplanes.  In particular,
   2279  1.1  mrg  * basic map "i" has an equality that lies parallel to basic map "j".
   2280  1.1  mrg  * Check if we can wrap the facets around the parallel hyperplanes
   2281  1.1  mrg  * to include the other set.
   2282  1.1  mrg  *
   2283  1.1  mrg  * We perform basically the same operations as can_wrap_in_facet,
   2284  1.1  mrg  * except that we don't need to select a facet of one of the sets.
   2285  1.1  mrg  *				_
   2286  1.1  mrg  *	\\			\\
   2287  1.1  mrg  *	 \\		=>	 \\
   2288  1.1  mrg  *	  \			  \|
   2289  1.1  mrg  *
   2290  1.1  mrg  * If there is more than one equality of "i" adjacent to an equality of "j",
   2291  1.1  mrg  * then the result will satisfy one or more equalities that are a linear
   2292  1.1  mrg  * combination of these equalities.  These will be encoded as pairs
   2293  1.1  mrg  * of inequalities in the wrapping constraints and need to be made
   2294  1.1  mrg  * explicit.
   2295  1.1  mrg  */
   2296  1.1  mrg static enum isl_change check_eq_adj_eq(int i, int j,
   2297  1.1  mrg 	struct isl_coalesce_info *info)
   2298  1.1  mrg {
   2299  1.1  mrg 	int k;
   2300  1.1  mrg 	enum isl_change change = isl_change_none;
   2301  1.1  mrg 	int detect_equalities = 0;
   2302  1.1  mrg 	struct isl_wraps wraps;
   2303  1.1  mrg 	isl_ctx *ctx;
   2304  1.1  mrg 	isl_mat *mat;
   2305  1.1  mrg 	struct isl_set *set_i = NULL;
   2306  1.1  mrg 	struct isl_set *set_j = NULL;
   2307  1.1  mrg 	struct isl_vec *bound = NULL;
   2308  1.1  mrg 	isl_size total = isl_basic_map_dim(info[i].bmap, isl_dim_all);
   2309  1.1  mrg 
   2310  1.1  mrg 	if (total < 0)
   2311  1.1  mrg 		return isl_change_error;
   2312  1.1  mrg 	if (count_eq(&info[i], STATUS_ADJ_EQ) != 1)
   2313  1.1  mrg 		detect_equalities = 1;
   2314  1.1  mrg 
   2315  1.1  mrg 	k = find_eq(&info[i], STATUS_ADJ_EQ);
   2316  1.1  mrg 
   2317  1.1  mrg 	set_i = set_from_updated_bmap(info[i].bmap, info[i].tab);
   2318  1.1  mrg 	set_j = set_from_updated_bmap(info[j].bmap, info[j].tab);
   2319  1.1  mrg 	ctx = isl_basic_map_get_ctx(info[i].bmap);
   2320  1.1  mrg 	mat = isl_mat_alloc(ctx, 2 * (info[i].bmap->n_eq + info[j].bmap->n_eq) +
   2321  1.1  mrg 				    info[i].bmap->n_ineq + info[j].bmap->n_ineq,
   2322  1.1  mrg 				    1 + total);
   2323  1.1  mrg 	if (wraps_init(&wraps, mat, info, i, j) < 0)
   2324  1.1  mrg 		goto error;
   2325  1.1  mrg 	bound = isl_vec_alloc(ctx, 1 + total);
   2326  1.1  mrg 	if (!set_i || !set_j || !bound)
   2327  1.1  mrg 		goto error;
   2328  1.1  mrg 
   2329  1.1  mrg 	if (k % 2 == 0)
   2330  1.1  mrg 		isl_seq_neg(bound->el, info[i].bmap->eq[k / 2], 1 + total);
   2331  1.1  mrg 	else
   2332  1.1  mrg 		isl_seq_cpy(bound->el, info[i].bmap->eq[k / 2], 1 + total);
   2333  1.1  mrg 	isl_int_add_ui(bound->el[0], bound->el[0], 1);
   2334  1.1  mrg 
   2335  1.1  mrg 	isl_seq_cpy(wraps.mat->row[0], bound->el, 1 + total);
   2336  1.1  mrg 	wraps.mat->n_row = 1;
   2337  1.1  mrg 
   2338  1.1  mrg 	if (add_wraps(&wraps, &info[j], bound->el, set_i) < 0)
   2339  1.1  mrg 		goto error;
   2340  1.1  mrg 	if (wraps.failed)
   2341  1.1  mrg 		goto unbounded;
   2342  1.1  mrg 
   2343  1.1  mrg 	isl_int_sub_ui(bound->el[0], bound->el[0], 1);
   2344  1.1  mrg 	isl_seq_neg(bound->el, bound->el, 1 + total);
   2345  1.1  mrg 
   2346  1.1  mrg 	isl_seq_cpy(wraps.mat->row[wraps.mat->n_row], bound->el, 1 + total);
   2347  1.1  mrg 	wraps.mat->n_row++;
   2348  1.1  mrg 
   2349  1.1  mrg 	if (add_wraps(&wraps, &info[i], bound->el, set_j) < 0)
   2350  1.1  mrg 		goto error;
   2351  1.1  mrg 	if (wraps.failed)
   2352  1.1  mrg 		goto unbounded;
   2353  1.1  mrg 
   2354  1.1  mrg 	change = fuse(i, j, info, wraps.mat, detect_equalities, 0);
   2355  1.1  mrg 
   2356  1.1  mrg 	if (0) {
   2357  1.1  mrg error:		change = isl_change_error;
   2358  1.1  mrg 	}
   2359  1.1  mrg unbounded:
   2360  1.1  mrg 
   2361  1.1  mrg 	wraps_free(&wraps);
   2362  1.1  mrg 	isl_set_free(set_i);
   2363  1.1  mrg 	isl_set_free(set_j);
   2364  1.1  mrg 	isl_vec_free(bound);
   2365  1.1  mrg 
   2366  1.1  mrg 	return change;
   2367  1.1  mrg }
   2368  1.1  mrg 
   2369  1.1  mrg /* Initialize the "eq" and "ineq" fields of "info".
   2370  1.1  mrg  */
   2371  1.1  mrg static void init_status(struct isl_coalesce_info *info)
   2372  1.1  mrg {
   2373  1.1  mrg 	info->eq = info->ineq = NULL;
   2374  1.1  mrg }
   2375  1.1  mrg 
   2376  1.1  mrg /* Set info->eq to the positions of the equalities of info->bmap
   2377  1.1  mrg  * with respect to the basic map represented by "tab".
   2378  1.1  mrg  * If info->eq has already been computed, then do not compute it again.
   2379  1.1  mrg  */
   2380  1.1  mrg static void set_eq_status_in(struct isl_coalesce_info *info,
   2381  1.1  mrg 	struct isl_tab *tab)
   2382  1.1  mrg {
   2383  1.1  mrg 	if (info->eq)
   2384  1.1  mrg 		return;
   2385  1.1  mrg 	info->eq = eq_status_in(info->bmap, tab);
   2386  1.1  mrg }
   2387  1.1  mrg 
   2388  1.1  mrg /* Set info->ineq to the positions of the inequalities of info->bmap
   2389  1.1  mrg  * with respect to the basic map represented by "tab".
   2390  1.1  mrg  * If info->ineq has already been computed, then do not compute it again.
   2391  1.1  mrg  */
   2392  1.1  mrg static void set_ineq_status_in(struct isl_coalesce_info *info,
   2393  1.1  mrg 	struct isl_tab *tab)
   2394  1.1  mrg {
   2395  1.1  mrg 	if (info->ineq)
   2396  1.1  mrg 		return;
   2397  1.1  mrg 	info->ineq = ineq_status_in(info->bmap, info->tab, tab);
   2398  1.1  mrg }
   2399  1.1  mrg 
   2400  1.1  mrg /* Free the memory allocated by the "eq" and "ineq" fields of "info".
   2401  1.1  mrg  * This function assumes that init_status has been called on "info" first,
   2402  1.1  mrg  * after which the "eq" and "ineq" fields may or may not have been
   2403  1.1  mrg  * assigned a newly allocated array.
   2404  1.1  mrg  */
   2405  1.1  mrg static void clear_status(struct isl_coalesce_info *info)
   2406  1.1  mrg {
   2407  1.1  mrg 	free(info->eq);
   2408  1.1  mrg 	free(info->ineq);
   2409  1.1  mrg }
   2410  1.1  mrg 
   2411  1.1  mrg /* Are all inequality constraints of the basic map represented by "info"
   2412  1.1  mrg  * valid for the other basic map, except for a single constraint
   2413  1.1  mrg  * that is adjacent to an inequality constraint of the other basic map?
   2414  1.1  mrg  */
   2415  1.1  mrg static int all_ineq_valid_or_single_adj_ineq(struct isl_coalesce_info *info)
   2416  1.1  mrg {
   2417  1.1  mrg 	int i;
   2418  1.1  mrg 	int k = -1;
   2419  1.1  mrg 
   2420  1.1  mrg 	for (i = 0; i < info->bmap->n_ineq; ++i) {
   2421  1.1  mrg 		if (info->ineq[i] == STATUS_REDUNDANT)
   2422  1.1  mrg 			continue;
   2423  1.1  mrg 		if (info->ineq[i] == STATUS_VALID)
   2424  1.1  mrg 			continue;
   2425  1.1  mrg 		if (info->ineq[i] != STATUS_ADJ_INEQ)
   2426  1.1  mrg 			return 0;
   2427  1.1  mrg 		if (k != -1)
   2428  1.1  mrg 			return 0;
   2429  1.1  mrg 		k = i;
   2430  1.1  mrg 	}
   2431  1.1  mrg 
   2432  1.1  mrg 	return k != -1;
   2433  1.1  mrg }
   2434  1.1  mrg 
   2435  1.1  mrg /* Basic map "i" has one or more equality constraints that separate it
   2436  1.1  mrg  * from basic map "j".  Check if it happens to be an extension
   2437  1.1  mrg  * of basic map "j".
   2438  1.1  mrg  * In particular, check that all constraints of "j" are valid for "i",
   2439  1.1  mrg  * except for one inequality constraint that is adjacent
   2440  1.1  mrg  * to an inequality constraints of "i".
   2441  1.1  mrg  * If so, check for "i" being an extension of "j" by calling
   2442  1.1  mrg  * is_adj_ineq_extension.
   2443  1.1  mrg  *
   2444  1.1  mrg  * Clean up the memory allocated for keeping track of the status
   2445  1.1  mrg  * of the constraints before returning.
   2446  1.1  mrg  */
   2447  1.1  mrg static enum isl_change separating_equality(int i, int j,
   2448  1.1  mrg 	struct isl_coalesce_info *info)
   2449  1.1  mrg {
   2450  1.1  mrg 	enum isl_change change = isl_change_none;
   2451  1.1  mrg 
   2452  1.1  mrg 	if (all(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_VALID) &&
   2453  1.1  mrg 	    all_ineq_valid_or_single_adj_ineq(&info[j]))
   2454  1.1  mrg 		change = is_adj_ineq_extension(j, i, info);
   2455  1.1  mrg 
   2456  1.1  mrg 	clear_status(&info[i]);
   2457  1.1  mrg 	clear_status(&info[j]);
   2458  1.1  mrg 	return change;
   2459  1.1  mrg }
   2460  1.1  mrg 
   2461  1.1  mrg /* Check if the union of the given pair of basic maps
   2462  1.1  mrg  * can be represented by a single basic map.
   2463  1.1  mrg  * If so, replace the pair by the single basic map and return
   2464  1.1  mrg  * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
   2465  1.1  mrg  * Otherwise, return isl_change_none.
   2466  1.1  mrg  * The two basic maps are assumed to live in the same local space.
   2467  1.1  mrg  * The "eq" and "ineq" fields of info[i] and info[j] are assumed
   2468  1.1  mrg  * to have been initialized by the caller, either to NULL or
   2469  1.1  mrg  * to valid information.
   2470  1.1  mrg  *
   2471  1.1  mrg  * We first check the effect of each constraint of one basic map
   2472  1.1  mrg  * on the other basic map.
   2473  1.1  mrg  * The constraint may be
   2474  1.1  mrg  *	redundant	the constraint is redundant in its own
   2475  1.1  mrg  *			basic map and should be ignore and removed
   2476  1.1  mrg  *			in the end
   2477  1.1  mrg  *	valid		all (integer) points of the other basic map
   2478  1.1  mrg  *			satisfy the constraint
   2479  1.1  mrg  *	separate	no (integer) point of the other basic map
   2480  1.1  mrg  *			satisfies the constraint
   2481  1.1  mrg  *	cut		some but not all points of the other basic map
   2482  1.1  mrg  *			satisfy the constraint
   2483  1.1  mrg  *	adj_eq		the given constraint is adjacent (on the outside)
   2484  1.1  mrg  *			to an equality of the other basic map
   2485  1.1  mrg  *	adj_ineq	the given constraint is adjacent (on the outside)
   2486  1.1  mrg  *			to an inequality of the other basic map
   2487  1.1  mrg  *
   2488  1.1  mrg  * We consider seven cases in which we can replace the pair by a single
   2489  1.1  mrg  * basic map.  We ignore all "redundant" constraints.
   2490  1.1  mrg  *
   2491  1.1  mrg  *	1. all constraints of one basic map are valid
   2492  1.1  mrg  *		=> the other basic map is a subset and can be removed
   2493  1.1  mrg  *
   2494  1.1  mrg  *	2. all constraints of both basic maps are either "valid" or "cut"
   2495  1.1  mrg  *	   and the facets corresponding to the "cut" constraints
   2496  1.1  mrg  *	   of one of the basic maps lies entirely inside the other basic map
   2497  1.1  mrg  *		=> the pair can be replaced by a basic map consisting
   2498  1.1  mrg  *		   of the valid constraints in both basic maps
   2499  1.1  mrg  *
   2500  1.1  mrg  *	3. there is a single pair of adjacent inequalities
   2501  1.1  mrg  *	   (all other constraints are "valid")
   2502  1.1  mrg  *		=> the pair can be replaced by a basic map consisting
   2503  1.1  mrg  *		   of the valid constraints in both basic maps
   2504  1.1  mrg  *
   2505  1.1  mrg  *	4. one basic map has a single adjacent inequality, while the other
   2506  1.1  mrg  *	   constraints are "valid".  The other basic map has some
   2507  1.1  mrg  *	   "cut" constraints, but replacing the adjacent inequality by
   2508  1.1  mrg  *	   its opposite and adding the valid constraints of the other
   2509  1.1  mrg  *	   basic map results in a subset of the other basic map
   2510  1.1  mrg  *		=> the pair can be replaced by a basic map consisting
   2511  1.1  mrg  *		   of the valid constraints in both basic maps
   2512  1.1  mrg  *
   2513  1.1  mrg  *	5. there is a single adjacent pair of an inequality and an equality,
   2514  1.1  mrg  *	   the other constraints of the basic map containing the inequality are
   2515  1.1  mrg  *	   "valid".  Moreover, if the inequality the basic map is relaxed
   2516  1.1  mrg  *	   and then turned into an equality, then resulting facet lies
   2517  1.1  mrg  *	   entirely inside the other basic map
   2518  1.1  mrg  *		=> the pair can be replaced by the basic map containing
   2519  1.1  mrg  *		   the inequality, with the inequality relaxed.
   2520  1.1  mrg  *
   2521  1.1  mrg  *	6. there is a single inequality adjacent to an equality,
   2522  1.1  mrg  *	   the other constraints of the basic map containing the inequality are
   2523  1.1  mrg  *	   "valid".  Moreover, the facets corresponding to both
   2524  1.1  mrg  *	   the inequality and the equality can be wrapped around their
   2525  1.1  mrg  *	   ridges to include the other basic map
   2526  1.1  mrg  *		=> the pair can be replaced by a basic map consisting
   2527  1.1  mrg  *		   of the valid constraints in both basic maps together
   2528  1.1  mrg  *		   with all wrapping constraints
   2529  1.1  mrg  *
   2530  1.1  mrg  *	7. one of the basic maps extends beyond the other by at most one.
   2531  1.1  mrg  *	   Moreover, the facets corresponding to the cut constraints and
   2532  1.1  mrg  *	   the pieces of the other basic map at offset one from these cut
   2533  1.1  mrg  *	   constraints can be wrapped around their ridges to include
   2534  1.1  mrg  *	   the union of the two basic maps
   2535  1.1  mrg  *		=> the pair can be replaced by a basic map consisting
   2536  1.1  mrg  *		   of the valid constraints in both basic maps together
   2537  1.1  mrg  *		   with all wrapping constraints
   2538  1.1  mrg  *
   2539  1.1  mrg  *	8. the two basic maps live in adjacent hyperplanes.  In principle
   2540  1.1  mrg  *	   such sets can always be combined through wrapping, but we impose
   2541  1.1  mrg  *	   that there is only one such pair, to avoid overeager coalescing.
   2542  1.1  mrg  *
   2543  1.1  mrg  * Throughout the computation, we maintain a collection of tableaus
   2544  1.1  mrg  * corresponding to the basic maps.  When the basic maps are dropped
   2545  1.1  mrg  * or combined, the tableaus are modified accordingly.
   2546  1.1  mrg  */
   2547  1.1  mrg static enum isl_change coalesce_local_pair_reuse(int i, int j,
   2548  1.1  mrg 	struct isl_coalesce_info *info)
   2549  1.1  mrg {
   2550  1.1  mrg 	enum isl_change change = isl_change_none;
   2551  1.1  mrg 
   2552  1.1  mrg 	set_ineq_status_in(&info[i], info[j].tab);
   2553  1.1  mrg 	if (info[i].bmap->n_ineq && !info[i].ineq)
   2554  1.1  mrg 		goto error;
   2555  1.1  mrg 	if (any_ineq(&info[i], STATUS_ERROR))
   2556  1.1  mrg 		goto error;
   2557  1.1  mrg 	if (any_ineq(&info[i], STATUS_SEPARATE))
   2558  1.1  mrg 		goto done;
   2559  1.1  mrg 
   2560  1.1  mrg 	set_ineq_status_in(&info[j], info[i].tab);
   2561  1.1  mrg 	if (info[j].bmap->n_ineq && !info[j].ineq)
   2562  1.1  mrg 		goto error;
   2563  1.1  mrg 	if (any_ineq(&info[j], STATUS_ERROR))
   2564  1.1  mrg 		goto error;
   2565  1.1  mrg 	if (any_ineq(&info[j], STATUS_SEPARATE))
   2566  1.1  mrg 		goto done;
   2567  1.1  mrg 
   2568  1.1  mrg 	set_eq_status_in(&info[i], info[j].tab);
   2569  1.1  mrg 	if (info[i].bmap->n_eq && !info[i].eq)
   2570  1.1  mrg 		goto error;
   2571  1.1  mrg 	if (any_eq(&info[i], STATUS_ERROR))
   2572  1.1  mrg 		goto error;
   2573  1.1  mrg 
   2574  1.1  mrg 	set_eq_status_in(&info[j], info[i].tab);
   2575  1.1  mrg 	if (info[j].bmap->n_eq && !info[j].eq)
   2576  1.1  mrg 		goto error;
   2577  1.1  mrg 	if (any_eq(&info[j], STATUS_ERROR))
   2578  1.1  mrg 		goto error;
   2579  1.1  mrg 
   2580  1.1  mrg 	if (any_eq(&info[i], STATUS_SEPARATE))
   2581  1.1  mrg 		return separating_equality(i, j, info);
   2582  1.1  mrg 	if (any_eq(&info[j], STATUS_SEPARATE))
   2583  1.1  mrg 		return separating_equality(j, i, info);
   2584  1.1  mrg 
   2585  1.1  mrg 	if (all(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_VALID) &&
   2586  1.1  mrg 	    all(info[i].ineq, info[i].bmap->n_ineq, STATUS_VALID)) {
   2587  1.1  mrg 		drop(&info[j]);
   2588  1.1  mrg 		change = isl_change_drop_second;
   2589  1.1  mrg 	} else if (all(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_VALID) &&
   2590  1.1  mrg 		   all(info[j].ineq, info[j].bmap->n_ineq, STATUS_VALID)) {
   2591  1.1  mrg 		drop(&info[i]);
   2592  1.1  mrg 		change = isl_change_drop_first;
   2593  1.1  mrg 	} else if (any_eq(&info[i], STATUS_ADJ_EQ)) {
   2594  1.1  mrg 		change = check_eq_adj_eq(i, j, info);
   2595  1.1  mrg 	} else if (any_eq(&info[j], STATUS_ADJ_EQ)) {
   2596  1.1  mrg 		change = check_eq_adj_eq(j, i, info);
   2597  1.1  mrg 	} else if (any_eq(&info[i], STATUS_ADJ_INEQ) ||
   2598  1.1  mrg 		   any_eq(&info[j], STATUS_ADJ_INEQ)) {
   2599  1.1  mrg 		change = check_adj_eq(i, j, info);
   2600  1.1  mrg 	} else if (any_ineq(&info[i], STATUS_ADJ_EQ)) {
   2601  1.1  mrg 		change = check_ineq_adj_eq(i, j, info);
   2602  1.1  mrg 	} else if (any_ineq(&info[j], STATUS_ADJ_EQ)) {
   2603  1.1  mrg 		change = check_ineq_adj_eq(j, i, info);
   2604  1.1  mrg 	} else if (any_ineq(&info[i], STATUS_ADJ_INEQ) ||
   2605  1.1  mrg 		   any_ineq(&info[j], STATUS_ADJ_INEQ)) {
   2606  1.1  mrg 		change = check_adj_ineq(i, j, info);
   2607  1.1  mrg 	} else {
   2608  1.1  mrg 		if (!any_eq(&info[i], STATUS_CUT) &&
   2609  1.1  mrg 		    !any_eq(&info[j], STATUS_CUT))
   2610  1.1  mrg 			change = check_facets(i, j, info);
   2611  1.1  mrg 		if (change == isl_change_none)
   2612  1.1  mrg 			change = check_wrap(i, j, info);
   2613  1.1  mrg 	}
   2614  1.1  mrg 
   2615  1.1  mrg done:
   2616  1.1  mrg 	clear_status(&info[i]);
   2617  1.1  mrg 	clear_status(&info[j]);
   2618  1.1  mrg 	return change;
   2619  1.1  mrg error:
   2620  1.1  mrg 	clear_status(&info[i]);
   2621  1.1  mrg 	clear_status(&info[j]);
   2622  1.1  mrg 	return isl_change_error;
   2623  1.1  mrg }
   2624  1.1  mrg 
   2625  1.1  mrg /* Check if the union of the given pair of basic maps
   2626  1.1  mrg  * can be represented by a single basic map.
   2627  1.1  mrg  * If so, replace the pair by the single basic map and return
   2628  1.1  mrg  * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
   2629  1.1  mrg  * Otherwise, return isl_change_none.
   2630  1.1  mrg  * The two basic maps are assumed to live in the same local space.
   2631  1.1  mrg  */
   2632  1.1  mrg static enum isl_change coalesce_local_pair(int i, int j,
   2633  1.1  mrg 	struct isl_coalesce_info *info)
   2634  1.1  mrg {
   2635  1.1  mrg 	init_status(&info[i]);
   2636  1.1  mrg 	init_status(&info[j]);
   2637  1.1  mrg 	return coalesce_local_pair_reuse(i, j, info);
   2638  1.1  mrg }
   2639  1.1  mrg 
   2640  1.1  mrg /* Shift the integer division at position "div" of the basic map
   2641  1.1  mrg  * represented by "info" by "shift".
   2642  1.1  mrg  *
   2643  1.1  mrg  * That is, if the integer division has the form
   2644  1.1  mrg  *
   2645  1.1  mrg  *	floor(f(x)/d)
   2646  1.1  mrg  *
   2647  1.1  mrg  * then replace it by
   2648  1.1  mrg  *
   2649  1.1  mrg  *	floor((f(x) + shift * d)/d) - shift
   2650  1.1  mrg  */
   2651  1.1  mrg static isl_stat shift_div(struct isl_coalesce_info *info, int div,
   2652  1.1  mrg 	isl_int shift)
   2653  1.1  mrg {
   2654  1.1  mrg 	isl_size total, n_div;
   2655  1.1  mrg 
   2656  1.1  mrg 	info->bmap = isl_basic_map_shift_div(info->bmap, div, 0, shift);
   2657  1.1  mrg 	if (!info->bmap)
   2658  1.1  mrg 		return isl_stat_error;
   2659  1.1  mrg 
   2660  1.1  mrg 	total = isl_basic_map_dim(info->bmap, isl_dim_all);
   2661  1.1  mrg 	n_div = isl_basic_map_dim(info->bmap, isl_dim_div);
   2662  1.1  mrg 	if (total < 0 || n_div < 0)
   2663  1.1  mrg 		return isl_stat_error;
   2664  1.1  mrg 	total -= n_div;
   2665  1.1  mrg 	if (isl_tab_shift_var(info->tab, total + div, shift) < 0)
   2666  1.1  mrg 		return isl_stat_error;
   2667  1.1  mrg 
   2668  1.1  mrg 	return isl_stat_ok;
   2669  1.1  mrg }
   2670  1.1  mrg 
   2671  1.1  mrg /* If the integer division at position "div" is defined by an equality,
   2672  1.1  mrg  * i.e., a stride constraint, then change the integer division expression
   2673  1.1  mrg  * to have a constant term equal to zero.
   2674  1.1  mrg  *
   2675  1.1  mrg  * Let the equality constraint be
   2676  1.1  mrg  *
   2677  1.1  mrg  *	c + f + m a = 0
   2678  1.1  mrg  *
   2679  1.1  mrg  * The integer division expression is then typically of the form
   2680  1.1  mrg  *
   2681  1.1  mrg  *	a = floor((-f - c')/m)
   2682  1.1  mrg  *
   2683  1.1  mrg  * The integer division is first shifted by t = floor(c/m),
   2684  1.1  mrg  * turning the equality constraint into
   2685  1.1  mrg  *
   2686  1.1  mrg  *	c - m floor(c/m) + f + m a' = 0
   2687  1.1  mrg  *
   2688  1.1  mrg  * i.e.,
   2689  1.1  mrg  *
   2690  1.1  mrg  *	(c mod m) + f + m a' = 0
   2691  1.1  mrg  *
   2692  1.1  mrg  * That is,
   2693  1.1  mrg  *
   2694  1.1  mrg  *	a' = (-f - (c mod m))/m = floor((-f)/m)
   2695  1.1  mrg  *
   2696  1.1  mrg  * because a' is an integer and 0 <= (c mod m) < m.
   2697  1.1  mrg  * The constant term of a' can therefore be zeroed out,
   2698  1.1  mrg  * but only if the integer division expression is of the expected form.
   2699  1.1  mrg  */
   2700  1.1  mrg static isl_stat normalize_stride_div(struct isl_coalesce_info *info, int div)
   2701  1.1  mrg {
   2702  1.1  mrg 	isl_bool defined, valid;
   2703  1.1  mrg 	isl_stat r;
   2704  1.1  mrg 	isl_constraint *c;
   2705  1.1  mrg 	isl_int shift, stride;
   2706  1.1  mrg 
   2707  1.1  mrg 	defined = isl_basic_map_has_defining_equality(info->bmap, isl_dim_div,
   2708  1.1  mrg 							div, &c);
   2709  1.1  mrg 	if (defined < 0)
   2710  1.1  mrg 		return isl_stat_error;
   2711  1.1  mrg 	if (!defined)
   2712  1.1  mrg 		return isl_stat_ok;
   2713  1.1  mrg 	if (!c)
   2714  1.1  mrg 		return isl_stat_error;
   2715  1.1  mrg 	valid = isl_constraint_is_div_equality(c, div);
   2716  1.1  mrg 	isl_int_init(shift);
   2717  1.1  mrg 	isl_int_init(stride);
   2718  1.1  mrg 	isl_constraint_get_constant(c, &shift);
   2719  1.1  mrg 	isl_constraint_get_coefficient(c, isl_dim_div, div, &stride);
   2720  1.1  mrg 	isl_int_fdiv_q(shift, shift, stride);
   2721  1.1  mrg 	r = shift_div(info, div, shift);
   2722  1.1  mrg 	isl_int_clear(stride);
   2723  1.1  mrg 	isl_int_clear(shift);
   2724  1.1  mrg 	isl_constraint_free(c);
   2725  1.1  mrg 	if (r < 0 || valid < 0)
   2726  1.1  mrg 		return isl_stat_error;
   2727  1.1  mrg 	if (!valid)
   2728  1.1  mrg 		return isl_stat_ok;
   2729  1.1  mrg 	info->bmap = isl_basic_map_set_div_expr_constant_num_si_inplace(
   2730  1.1  mrg 							    info->bmap, div, 0);
   2731  1.1  mrg 	if (!info->bmap)
   2732  1.1  mrg 		return isl_stat_error;
   2733  1.1  mrg 	return isl_stat_ok;
   2734  1.1  mrg }
   2735  1.1  mrg 
   2736  1.1  mrg /* The basic maps represented by "info1" and "info2" are known
   2737  1.1  mrg  * to have the same number of integer divisions.
   2738  1.1  mrg  * Check if pairs of integer divisions are equal to each other
   2739  1.1  mrg  * despite the fact that they differ by a rational constant.
   2740  1.1  mrg  *
   2741  1.1  mrg  * In particular, look for any pair of integer divisions that
   2742  1.1  mrg  * only differ in their constant terms.
   2743  1.1  mrg  * If either of these integer divisions is defined
   2744  1.1  mrg  * by stride constraints, then modify it to have a zero constant term.
   2745  1.1  mrg  * If both are defined by stride constraints then in the end they will have
   2746  1.1  mrg  * the same (zero) constant term.
   2747  1.1  mrg  */
   2748  1.1  mrg static isl_stat harmonize_stride_divs(struct isl_coalesce_info *info1,
   2749  1.1  mrg 	struct isl_coalesce_info *info2)
   2750  1.1  mrg {
   2751  1.1  mrg 	int i;
   2752  1.1  mrg 	isl_size n;
   2753  1.1  mrg 
   2754  1.1  mrg 	n = isl_basic_map_dim(info1->bmap, isl_dim_div);
   2755  1.1  mrg 	if (n < 0)
   2756  1.1  mrg 		return isl_stat_error;
   2757  1.1  mrg 	for (i = 0; i < n; ++i) {
   2758  1.1  mrg 		isl_bool known, harmonize;
   2759  1.1  mrg 
   2760  1.1  mrg 		known = isl_basic_map_div_is_known(info1->bmap, i);
   2761  1.1  mrg 		if (known >= 0 && known)
   2762  1.1  mrg 			known = isl_basic_map_div_is_known(info2->bmap, i);
   2763  1.1  mrg 		if (known < 0)
   2764  1.1  mrg 			return isl_stat_error;
   2765  1.1  mrg 		if (!known)
   2766  1.1  mrg 			continue;
   2767  1.1  mrg 		harmonize = isl_basic_map_equal_div_expr_except_constant(
   2768  1.1  mrg 					    info1->bmap, i, info2->bmap, i);
   2769  1.1  mrg 		if (harmonize < 0)
   2770  1.1  mrg 			return isl_stat_error;
   2771  1.1  mrg 		if (!harmonize)
   2772  1.1  mrg 			continue;
   2773  1.1  mrg 		if (normalize_stride_div(info1, i) < 0)
   2774  1.1  mrg 			return isl_stat_error;
   2775  1.1  mrg 		if (normalize_stride_div(info2, i) < 0)
   2776  1.1  mrg 			return isl_stat_error;
   2777  1.1  mrg 	}
   2778  1.1  mrg 
   2779  1.1  mrg 	return isl_stat_ok;
   2780  1.1  mrg }
   2781  1.1  mrg 
   2782  1.1  mrg /* If "shift" is an integer constant, then shift the integer division
   2783  1.1  mrg  * at position "div" of the basic map represented by "info" by "shift".
   2784  1.1  mrg  * If "shift" is not an integer constant, then do nothing.
   2785  1.1  mrg  * If "shift" is equal to zero, then no shift needs to be performed either.
   2786  1.1  mrg  *
   2787  1.1  mrg  * That is, if the integer division has the form
   2788  1.1  mrg  *
   2789  1.1  mrg  *	floor(f(x)/d)
   2790  1.1  mrg  *
   2791  1.1  mrg  * then replace it by
   2792  1.1  mrg  *
   2793  1.1  mrg  *	floor((f(x) + shift * d)/d) - shift
   2794  1.1  mrg  */
   2795  1.1  mrg static isl_stat shift_if_cst_int(struct isl_coalesce_info *info, int div,
   2796  1.1  mrg 	__isl_keep isl_aff *shift)
   2797  1.1  mrg {
   2798  1.1  mrg 	isl_bool cst;
   2799  1.1  mrg 	isl_stat r;
   2800  1.1  mrg 	isl_int d;
   2801  1.1  mrg 	isl_val *c;
   2802  1.1  mrg 
   2803  1.1  mrg 	cst = isl_aff_is_cst(shift);
   2804  1.1  mrg 	if (cst < 0 || !cst)
   2805  1.1  mrg 		return cst < 0 ? isl_stat_error : isl_stat_ok;
   2806  1.1  mrg 
   2807  1.1  mrg 	c = isl_aff_get_constant_val(shift);
   2808  1.1  mrg 	cst = isl_val_is_int(c);
   2809  1.1  mrg 	if (cst >= 0 && cst)
   2810  1.1  mrg 		cst = isl_bool_not(isl_val_is_zero(c));
   2811  1.1  mrg 	if (cst < 0 || !cst) {
   2812  1.1  mrg 		isl_val_free(c);
   2813  1.1  mrg 		return cst < 0 ? isl_stat_error : isl_stat_ok;
   2814  1.1  mrg 	}
   2815  1.1  mrg 
   2816  1.1  mrg 	isl_int_init(d);
   2817  1.1  mrg 	r = isl_val_get_num_isl_int(c, &d);
   2818  1.1  mrg 	if (r >= 0)
   2819  1.1  mrg 		r = shift_div(info, div, d);
   2820  1.1  mrg 	isl_int_clear(d);
   2821  1.1  mrg 
   2822  1.1  mrg 	isl_val_free(c);
   2823  1.1  mrg 
   2824  1.1  mrg 	return r;
   2825  1.1  mrg }
   2826  1.1  mrg 
   2827  1.1  mrg /* Check if some of the divs in the basic map represented by "info1"
   2828  1.1  mrg  * are shifts of the corresponding divs in the basic map represented
   2829  1.1  mrg  * by "info2", taking into account the equality constraints "eq1" of "info1"
   2830  1.1  mrg  * and "eq2" of "info2".  If so, align them with those of "info2".
   2831  1.1  mrg  * "info1" and "info2" are assumed to have the same number
   2832  1.1  mrg  * of integer divisions.
   2833  1.1  mrg  *
   2834  1.1  mrg  * An integer division is considered to be a shift of another integer
   2835  1.1  mrg  * division if, after simplification with respect to the equality
   2836  1.1  mrg  * constraints of the other basic map, one is equal to the other
   2837  1.1  mrg  * plus a constant.
   2838  1.1  mrg  *
   2839  1.1  mrg  * In particular, for each pair of integer divisions, if both are known,
   2840  1.1  mrg  * have the same denominator and are not already equal to each other,
   2841  1.1  mrg  * simplify each with respect to the equality constraints
   2842  1.1  mrg  * of the other basic map.  If the difference is an integer constant,
   2843  1.1  mrg  * then move this difference outside.
   2844  1.1  mrg  * That is, if, after simplification, one integer division is of the form
   2845  1.1  mrg  *
   2846  1.1  mrg  *	floor((f(x) + c_1)/d)
   2847  1.1  mrg  *
   2848  1.1  mrg  * while the other is of the form
   2849  1.1  mrg  *
   2850  1.1  mrg  *	floor((f(x) + c_2)/d)
   2851  1.1  mrg  *
   2852  1.1  mrg  * and n = (c_2 - c_1)/d is an integer, then replace the first
   2853  1.1  mrg  * integer division by
   2854  1.1  mrg  *
   2855  1.1  mrg  *	floor((f_1(x) + c_1 + n * d)/d) - n,
   2856  1.1  mrg  *
   2857  1.1  mrg  * where floor((f_1(x) + c_1 + n * d)/d) = floor((f2(x) + c_2)/d)
   2858  1.1  mrg  * after simplification with respect to the equality constraints.
   2859  1.1  mrg  */
   2860  1.1  mrg static isl_stat harmonize_divs_with_hulls(struct isl_coalesce_info *info1,
   2861  1.1  mrg 	struct isl_coalesce_info *info2, __isl_keep isl_basic_set *eq1,
   2862  1.1  mrg 	__isl_keep isl_basic_set *eq2)
   2863  1.1  mrg {
   2864  1.1  mrg 	int i;
   2865  1.1  mrg 	isl_size total;
   2866  1.1  mrg 	isl_local_space *ls1, *ls2;
   2867  1.1  mrg 
   2868  1.1  mrg 	total = isl_basic_map_dim(info1->bmap, isl_dim_all);
   2869  1.1  mrg 	if (total < 0)
   2870  1.1  mrg 		return isl_stat_error;
   2871  1.1  mrg 	ls1 = isl_local_space_wrap(isl_basic_map_get_local_space(info1->bmap));
   2872  1.1  mrg 	ls2 = isl_local_space_wrap(isl_basic_map_get_local_space(info2->bmap));
   2873  1.1  mrg 	for (i = 0; i < info1->bmap->n_div; ++i) {
   2874  1.1  mrg 		isl_stat r;
   2875  1.1  mrg 		isl_aff *div1, *div2;
   2876  1.1  mrg 
   2877  1.1  mrg 		if (!isl_local_space_div_is_known(ls1, i) ||
   2878  1.1  mrg 		    !isl_local_space_div_is_known(ls2, i))
   2879  1.1  mrg 			continue;
   2880  1.1  mrg 		if (isl_int_ne(info1->bmap->div[i][0], info2->bmap->div[i][0]))
   2881  1.1  mrg 			continue;
   2882  1.1  mrg 		if (isl_seq_eq(info1->bmap->div[i] + 1,
   2883  1.1  mrg 				info2->bmap->div[i] + 1, 1 + total))
   2884  1.1  mrg 			continue;
   2885  1.1  mrg 		div1 = isl_local_space_get_div(ls1, i);
   2886  1.1  mrg 		div2 = isl_local_space_get_div(ls2, i);
   2887  1.1  mrg 		div1 = isl_aff_substitute_equalities(div1,
   2888  1.1  mrg 						    isl_basic_set_copy(eq2));
   2889  1.1  mrg 		div2 = isl_aff_substitute_equalities(div2,
   2890  1.1  mrg 						    isl_basic_set_copy(eq1));
   2891  1.1  mrg 		div2 = isl_aff_sub(div2, div1);
   2892  1.1  mrg 		r = shift_if_cst_int(info1, i, div2);
   2893  1.1  mrg 		isl_aff_free(div2);
   2894  1.1  mrg 		if (r < 0)
   2895  1.1  mrg 			break;
   2896  1.1  mrg 	}
   2897  1.1  mrg 	isl_local_space_free(ls1);
   2898  1.1  mrg 	isl_local_space_free(ls2);
   2899  1.1  mrg 
   2900  1.1  mrg 	if (i < info1->bmap->n_div)
   2901  1.1  mrg 		return isl_stat_error;
   2902  1.1  mrg 	return isl_stat_ok;
   2903  1.1  mrg }
   2904  1.1  mrg 
   2905  1.1  mrg /* Check if some of the divs in the basic map represented by "info1"
   2906  1.1  mrg  * are shifts of the corresponding divs in the basic map represented
   2907  1.1  mrg  * by "info2".  If so, align them with those of "info2".
   2908  1.1  mrg  * Only do this if "info1" and "info2" have the same number
   2909  1.1  mrg  * of integer divisions.
   2910  1.1  mrg  *
   2911  1.1  mrg  * An integer division is considered to be a shift of another integer
   2912  1.1  mrg  * division if, after simplification with respect to the equality
   2913  1.1  mrg  * constraints of the other basic map, one is equal to the other
   2914  1.1  mrg  * plus a constant.
   2915  1.1  mrg  *
   2916  1.1  mrg  * First check if pairs of integer divisions are equal to each other
   2917  1.1  mrg  * despite the fact that they differ by a rational constant.
   2918  1.1  mrg  * If so, try and arrange for them to have the same constant term.
   2919  1.1  mrg  *
   2920  1.1  mrg  * Then, extract the equality constraints and continue with
   2921  1.1  mrg  * harmonize_divs_with_hulls.
   2922  1.1  mrg  *
   2923  1.1  mrg  * If the equality constraints of both basic maps are the same,
   2924  1.1  mrg  * then there is no need to perform any shifting since
   2925  1.1  mrg  * the coefficients of the integer divisions should have been
   2926  1.1  mrg  * reduced in the same way.
   2927  1.1  mrg  */
   2928  1.1  mrg static isl_stat harmonize_divs(struct isl_coalesce_info *info1,
   2929  1.1  mrg 	struct isl_coalesce_info *info2)
   2930  1.1  mrg {
   2931  1.1  mrg 	isl_bool equal;
   2932  1.1  mrg 	isl_basic_map *bmap1, *bmap2;
   2933  1.1  mrg 	isl_basic_set *eq1, *eq2;
   2934  1.1  mrg 	isl_stat r;
   2935  1.1  mrg 
   2936  1.1  mrg 	if (!info1->bmap || !info2->bmap)
   2937  1.1  mrg 		return isl_stat_error;
   2938  1.1  mrg 
   2939  1.1  mrg 	if (info1->bmap->n_div != info2->bmap->n_div)
   2940  1.1  mrg 		return isl_stat_ok;
   2941  1.1  mrg 	if (info1->bmap->n_div == 0)
   2942  1.1  mrg 		return isl_stat_ok;
   2943  1.1  mrg 
   2944  1.1  mrg 	if (harmonize_stride_divs(info1, info2) < 0)
   2945  1.1  mrg 		return isl_stat_error;
   2946  1.1  mrg 
   2947  1.1  mrg 	bmap1 = isl_basic_map_copy(info1->bmap);
   2948  1.1  mrg 	bmap2 = isl_basic_map_copy(info2->bmap);
   2949  1.1  mrg 	eq1 = isl_basic_map_wrap(isl_basic_map_plain_affine_hull(bmap1));
   2950  1.1  mrg 	eq2 = isl_basic_map_wrap(isl_basic_map_plain_affine_hull(bmap2));
   2951  1.1  mrg 	equal = isl_basic_set_plain_is_equal(eq1, eq2);
   2952  1.1  mrg 	if (equal < 0)
   2953  1.1  mrg 		r = isl_stat_error;
   2954  1.1  mrg 	else if (equal)
   2955  1.1  mrg 		r = isl_stat_ok;
   2956  1.1  mrg 	else
   2957  1.1  mrg 		r = harmonize_divs_with_hulls(info1, info2, eq1, eq2);
   2958  1.1  mrg 	isl_basic_set_free(eq1);
   2959  1.1  mrg 	isl_basic_set_free(eq2);
   2960  1.1  mrg 
   2961  1.1  mrg 	return r;
   2962  1.1  mrg }
   2963  1.1  mrg 
   2964  1.1  mrg /* Do the two basic maps live in the same local space, i.e.,
   2965  1.1  mrg  * do they have the same (known) divs?
   2966  1.1  mrg  * If either basic map has any unknown divs, then we can only assume
   2967  1.1  mrg  * that they do not live in the same local space.
   2968  1.1  mrg  */
   2969  1.1  mrg static isl_bool same_divs(__isl_keep isl_basic_map *bmap1,
   2970  1.1  mrg 	__isl_keep isl_basic_map *bmap2)
   2971  1.1  mrg {
   2972  1.1  mrg 	int i;
   2973  1.1  mrg 	isl_bool known;
   2974  1.1  mrg 	isl_size total;
   2975  1.1  mrg 
   2976  1.1  mrg 	if (!bmap1 || !bmap2)
   2977  1.1  mrg 		return isl_bool_error;
   2978  1.1  mrg 	if (bmap1->n_div != bmap2->n_div)
   2979  1.1  mrg 		return isl_bool_false;
   2980  1.1  mrg 
   2981  1.1  mrg 	if (bmap1->n_div == 0)
   2982  1.1  mrg 		return isl_bool_true;
   2983  1.1  mrg 
   2984  1.1  mrg 	known = isl_basic_map_divs_known(bmap1);
   2985  1.1  mrg 	if (known < 0 || !known)
   2986  1.1  mrg 		return known;
   2987  1.1  mrg 	known = isl_basic_map_divs_known(bmap2);
   2988  1.1  mrg 	if (known < 0 || !known)
   2989  1.1  mrg 		return known;
   2990  1.1  mrg 
   2991  1.1  mrg 	total = isl_basic_map_dim(bmap1, isl_dim_all);
   2992  1.1  mrg 	if (total < 0)
   2993  1.1  mrg 		return isl_bool_error;
   2994  1.1  mrg 	for (i = 0; i < bmap1->n_div; ++i)
   2995  1.1  mrg 		if (!isl_seq_eq(bmap1->div[i], bmap2->div[i], 2 + total))
   2996  1.1  mrg 			return isl_bool_false;
   2997  1.1  mrg 
   2998  1.1  mrg 	return isl_bool_true;
   2999  1.1  mrg }
   3000  1.1  mrg 
   3001  1.1  mrg /* Assuming that "tab" contains the equality constraints and
   3002  1.1  mrg  * the initial inequality constraints of "bmap", copy the remaining
   3003  1.1  mrg  * inequality constraints of "bmap" to "Tab".
   3004  1.1  mrg  */
   3005  1.1  mrg static isl_stat copy_ineq(struct isl_tab *tab, __isl_keep isl_basic_map *bmap)
   3006  1.1  mrg {
   3007  1.1  mrg 	int i, n_ineq;
   3008  1.1  mrg 
   3009  1.1  mrg 	if (!bmap)
   3010  1.1  mrg 		return isl_stat_error;
   3011  1.1  mrg 
   3012  1.1  mrg 	n_ineq = tab->n_con - tab->n_eq;
   3013  1.1  mrg 	for (i = n_ineq; i < bmap->n_ineq; ++i)
   3014  1.1  mrg 		if (isl_tab_add_ineq(tab, bmap->ineq[i]) < 0)
   3015  1.1  mrg 			return isl_stat_error;
   3016  1.1  mrg 
   3017  1.1  mrg 	return isl_stat_ok;
   3018  1.1  mrg }
   3019  1.1  mrg 
   3020  1.1  mrg /* Description of an integer division that is added
   3021  1.1  mrg  * during an expansion.
   3022  1.1  mrg  * "pos" is the position of the corresponding variable.
   3023  1.1  mrg  * "cst" indicates whether this integer division has a fixed value.
   3024  1.1  mrg  * "val" contains the fixed value, if the value is fixed.
   3025  1.1  mrg  */
   3026  1.1  mrg struct isl_expanded {
   3027  1.1  mrg 	int pos;
   3028  1.1  mrg 	isl_bool cst;
   3029  1.1  mrg 	isl_int val;
   3030  1.1  mrg };
   3031  1.1  mrg 
   3032  1.1  mrg /* For each of the "n" integer division variables "expanded",
   3033  1.1  mrg  * if the variable has a fixed value, then add two inequality
   3034  1.1  mrg  * constraints expressing the fixed value.
   3035  1.1  mrg  * Otherwise, add the corresponding div constraints.
   3036  1.1  mrg  * The caller is responsible for removing the div constraints
   3037  1.1  mrg  * that it added for all these "n" integer divisions.
   3038  1.1  mrg  *
   3039  1.1  mrg  * The div constraints and the pair of inequality constraints
   3040  1.1  mrg  * forcing the fixed value cannot both be added for a given variable
   3041  1.1  mrg  * as the combination may render some of the original constraints redundant.
   3042  1.1  mrg  * These would then be ignored during the coalescing detection,
   3043  1.1  mrg  * while they could remain in the fused result.
   3044  1.1  mrg  *
   3045  1.1  mrg  * The two added inequality constraints are
   3046  1.1  mrg  *
   3047  1.1  mrg  *	-a + v >= 0
   3048  1.1  mrg  *	a - v >= 0
   3049  1.1  mrg  *
   3050  1.1  mrg  * with "a" the variable and "v" its fixed value.
   3051  1.1  mrg  * The facet corresponding to one of these two constraints is selected
   3052  1.1  mrg  * in the tableau to ensure that the pair of inequality constraints
   3053  1.1  mrg  * is treated as an equality constraint.
   3054  1.1  mrg  *
   3055  1.1  mrg  * The information in info->ineq is thrown away because it was
   3056  1.1  mrg  * computed in terms of div constraints, while some of those
   3057  1.1  mrg  * have now been replaced by these pairs of inequality constraints.
   3058  1.1  mrg  */
   3059  1.1  mrg static isl_stat fix_constant_divs(struct isl_coalesce_info *info,
   3060  1.1  mrg 	int n, struct isl_expanded *expanded)
   3061  1.1  mrg {
   3062  1.1  mrg 	unsigned o_div;
   3063  1.1  mrg 	int i;
   3064  1.1  mrg 	isl_vec *ineq;
   3065  1.1  mrg 
   3066  1.1  mrg 	o_div = isl_basic_map_offset(info->bmap, isl_dim_div) - 1;
   3067  1.1  mrg 	ineq = isl_vec_alloc(isl_tab_get_ctx(info->tab), 1 + info->tab->n_var);
   3068  1.1  mrg 	if (!ineq)
   3069  1.1  mrg 		return isl_stat_error;
   3070  1.1  mrg 	isl_seq_clr(ineq->el + 1, info->tab->n_var);
   3071  1.1  mrg 
   3072  1.1  mrg 	for (i = 0; i < n; ++i) {
   3073  1.1  mrg 		if (!expanded[i].cst) {
   3074  1.1  mrg 			info->bmap = isl_basic_map_extend_constraints(
   3075  1.1  mrg 						info->bmap, 0, 2);
   3076  1.1  mrg 			info->bmap = isl_basic_map_add_div_constraints(
   3077  1.1  mrg 					info->bmap, expanded[i].pos - o_div);
   3078  1.1  mrg 		} else {
   3079  1.1  mrg 			isl_int_set_si(ineq->el[1 + expanded[i].pos], -1);
   3080  1.1  mrg 			isl_int_set(ineq->el[0], expanded[i].val);
   3081  1.1  mrg 			info->bmap = isl_basic_map_add_ineq(info->bmap,
   3082  1.1  mrg 								ineq->el);
   3083  1.1  mrg 			isl_int_set_si(ineq->el[1 + expanded[i].pos], 1);
   3084  1.1  mrg 			isl_int_neg(ineq->el[0], expanded[i].val);
   3085  1.1  mrg 			info->bmap = isl_basic_map_add_ineq(info->bmap,
   3086  1.1  mrg 								ineq->el);
   3087  1.1  mrg 			isl_int_set_si(ineq->el[1 + expanded[i].pos], 0);
   3088  1.1  mrg 		}
   3089  1.1  mrg 		if (copy_ineq(info->tab, info->bmap) < 0)
   3090  1.1  mrg 			break;
   3091  1.1  mrg 		if (expanded[i].cst &&
   3092  1.1  mrg 		    isl_tab_select_facet(info->tab, info->tab->n_con - 1) < 0)
   3093  1.1  mrg 			break;
   3094  1.1  mrg 	}
   3095  1.1  mrg 
   3096  1.1  mrg 	isl_vec_free(ineq);
   3097  1.1  mrg 
   3098  1.1  mrg 	clear_status(info);
   3099  1.1  mrg 	init_status(info);
   3100  1.1  mrg 
   3101  1.1  mrg 	return i < n ? isl_stat_error : isl_stat_ok;
   3102  1.1  mrg }
   3103  1.1  mrg 
   3104  1.1  mrg /* Insert the "n" integer division variables "expanded"
   3105  1.1  mrg  * into info->tab and info->bmap and
   3106  1.1  mrg  * update info->ineq with respect to the redundant constraints
   3107  1.1  mrg  * in the resulting tableau.
   3108  1.1  mrg  * "bmap" contains the result of this insertion in info->bmap,
   3109  1.1  mrg  * while info->bmap is the original version
   3110  1.1  mrg  * of "bmap", i.e., the one that corresponds to the current
   3111  1.1  mrg  * state of info->tab.  The number of constraints in info->bmap
   3112  1.1  mrg  * is assumed to be the same as the number of constraints
   3113  1.1  mrg  * in info->tab.  This is required to be able to detect
   3114  1.1  mrg  * the extra constraints in "bmap".
   3115  1.1  mrg  *
   3116  1.1  mrg  * In particular, introduce extra variables corresponding
   3117  1.1  mrg  * to the extra integer divisions and add the div constraints
   3118  1.1  mrg  * that were added to "bmap" after info->tab was created
   3119  1.1  mrg  * from info->bmap.
   3120  1.1  mrg  * Furthermore, check if these extra integer divisions happen
   3121  1.1  mrg  * to attain a fixed integer value in info->tab.
   3122  1.1  mrg  * If so, replace the corresponding div constraints by pairs
   3123  1.1  mrg  * of inequality constraints that fix these
   3124  1.1  mrg  * integer divisions to their single integer values.
   3125  1.1  mrg  * Replace info->bmap by "bmap" to match the changes to info->tab.
   3126  1.1  mrg  * info->ineq was computed without a tableau and therefore
   3127  1.1  mrg  * does not take into account the redundant constraints
   3128  1.1  mrg  * in the tableau.  Mark them here.
   3129  1.1  mrg  * There is no need to check the newly added div constraints
   3130  1.1  mrg  * since they cannot be redundant.
   3131  1.1  mrg  * The redundancy check is not performed when constants have been discovered
   3132  1.1  mrg  * since info->ineq is completely thrown away in this case.
   3133  1.1  mrg  */
   3134  1.1  mrg static isl_stat tab_insert_divs(struct isl_coalesce_info *info,
   3135  1.1  mrg 	int n, struct isl_expanded *expanded, __isl_take isl_basic_map *bmap)
   3136  1.1  mrg {
   3137  1.1  mrg 	int i, n_ineq;
   3138  1.1  mrg 	unsigned n_eq;
   3139  1.1  mrg 	struct isl_tab_undo *snap;
   3140  1.1  mrg 	int any;
   3141  1.1  mrg 
   3142  1.1  mrg 	if (!bmap)
   3143  1.1  mrg 		return isl_stat_error;
   3144  1.1  mrg 	if (info->bmap->n_eq + info->bmap->n_ineq != info->tab->n_con)
   3145  1.1  mrg 		isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
   3146  1.1  mrg 			"original tableau does not correspond "
   3147  1.1  mrg 			"to original basic map", goto error);
   3148  1.1  mrg 
   3149  1.1  mrg 	if (isl_tab_extend_vars(info->tab, n) < 0)
   3150  1.1  mrg 		goto error;
   3151  1.1  mrg 	if (isl_tab_extend_cons(info->tab, 2 * n) < 0)
   3152  1.1  mrg 		goto error;
   3153  1.1  mrg 
   3154  1.1  mrg 	for (i = 0; i < n; ++i) {
   3155  1.1  mrg 		if (isl_tab_insert_var(info->tab, expanded[i].pos) < 0)
   3156  1.1  mrg 			goto error;
   3157  1.1  mrg 	}
   3158  1.1  mrg 
   3159  1.1  mrg 	snap = isl_tab_snap(info->tab);
   3160  1.1  mrg 
   3161  1.1  mrg 	n_ineq = info->tab->n_con - info->tab->n_eq;
   3162  1.1  mrg 	if (copy_ineq(info->tab, bmap) < 0)
   3163  1.1  mrg 		goto error;
   3164  1.1  mrg 
   3165  1.1  mrg 	isl_basic_map_free(info->bmap);
   3166  1.1  mrg 	info->bmap = bmap;
   3167  1.1  mrg 
   3168  1.1  mrg 	any = 0;
   3169  1.1  mrg 	for (i = 0; i < n; ++i) {
   3170  1.1  mrg 		expanded[i].cst = isl_tab_is_constant(info->tab,
   3171  1.1  mrg 					    expanded[i].pos, &expanded[i].val);
   3172  1.1  mrg 		if (expanded[i].cst < 0)
   3173  1.1  mrg 			return isl_stat_error;
   3174  1.1  mrg 		if (expanded[i].cst)
   3175  1.1  mrg 			any = 1;
   3176  1.1  mrg 	}
   3177  1.1  mrg 
   3178  1.1  mrg 	if (any) {
   3179  1.1  mrg 		if (isl_tab_rollback(info->tab, snap) < 0)
   3180  1.1  mrg 			return isl_stat_error;
   3181  1.1  mrg 		info->bmap = isl_basic_map_cow(info->bmap);
   3182  1.1  mrg 		info->bmap = isl_basic_map_free_inequality(info->bmap, 2 * n);
   3183  1.1  mrg 		if (!info->bmap)
   3184  1.1  mrg 			return isl_stat_error;
   3185  1.1  mrg 
   3186  1.1  mrg 		return fix_constant_divs(info, n, expanded);
   3187  1.1  mrg 	}
   3188  1.1  mrg 
   3189  1.1  mrg 	n_eq = info->bmap->n_eq;
   3190  1.1  mrg 	for (i = 0; i < n_ineq; ++i) {
   3191  1.1  mrg 		if (isl_tab_is_redundant(info->tab, n_eq + i))
   3192  1.1  mrg 			info->ineq[i] = STATUS_REDUNDANT;
   3193  1.1  mrg 	}
   3194  1.1  mrg 
   3195  1.1  mrg 	return isl_stat_ok;
   3196  1.1  mrg error:
   3197  1.1  mrg 	isl_basic_map_free(bmap);
   3198  1.1  mrg 	return isl_stat_error;
   3199  1.1  mrg }
   3200  1.1  mrg 
   3201  1.1  mrg /* Expand info->tab and info->bmap in the same way "bmap" was expanded
   3202  1.1  mrg  * in isl_basic_map_expand_divs using the expansion "exp" and
   3203  1.1  mrg  * update info->ineq with respect to the redundant constraints
   3204  1.1  mrg  * in the resulting tableau. info->bmap is the original version
   3205  1.1  mrg  * of "bmap", i.e., the one that corresponds to the current
   3206  1.1  mrg  * state of info->tab.  The number of constraints in info->bmap
   3207  1.1  mrg  * is assumed to be the same as the number of constraints
   3208  1.1  mrg  * in info->tab.  This is required to be able to detect
   3209  1.1  mrg  * the extra constraints in "bmap".
   3210  1.1  mrg  *
   3211  1.1  mrg  * Extract the positions where extra local variables are introduced
   3212  1.1  mrg  * from "exp" and call tab_insert_divs.
   3213  1.1  mrg  */
   3214  1.1  mrg static isl_stat expand_tab(struct isl_coalesce_info *info, int *exp,
   3215  1.1  mrg 	__isl_take isl_basic_map *bmap)
   3216  1.1  mrg {
   3217  1.1  mrg 	isl_ctx *ctx;
   3218  1.1  mrg 	struct isl_expanded *expanded;
   3219  1.1  mrg 	int i, j, k, n;
   3220  1.1  mrg 	int extra_var;
   3221  1.1  mrg 	isl_size total, n_div;
   3222  1.1  mrg 	unsigned pos;
   3223  1.1  mrg 	isl_stat r;
   3224  1.1  mrg 
   3225  1.1  mrg 	total = isl_basic_map_dim(bmap, isl_dim_all);
   3226  1.1  mrg 	n_div = isl_basic_map_dim(bmap, isl_dim_div);
   3227  1.1  mrg 	if (total < 0 || n_div < 0)
   3228  1.1  mrg 		return isl_stat_error;
   3229  1.1  mrg 	pos = total - n_div;
   3230  1.1  mrg 	extra_var = total - info->tab->n_var;
   3231  1.1  mrg 	n = n_div - extra_var;
   3232  1.1  mrg 
   3233  1.1  mrg 	ctx = isl_basic_map_get_ctx(bmap);
   3234  1.1  mrg 	expanded = isl_calloc_array(ctx, struct isl_expanded, extra_var);
   3235  1.1  mrg 	if (extra_var && !expanded)
   3236  1.1  mrg 		goto error;
   3237  1.1  mrg 
   3238  1.1  mrg 	i = 0;
   3239  1.1  mrg 	k = 0;
   3240  1.1  mrg 	for (j = 0; j < n_div; ++j) {
   3241  1.1  mrg 		if (i < n && exp[i] == j) {
   3242  1.1  mrg 			++i;
   3243  1.1  mrg 			continue;
   3244  1.1  mrg 		}
   3245  1.1  mrg 		expanded[k++].pos = pos + j;
   3246  1.1  mrg 	}
   3247  1.1  mrg 
   3248  1.1  mrg 	for (k = 0; k < extra_var; ++k)
   3249  1.1  mrg 		isl_int_init(expanded[k].val);
   3250  1.1  mrg 
   3251  1.1  mrg 	r = tab_insert_divs(info, extra_var, expanded, bmap);
   3252  1.1  mrg 
   3253  1.1  mrg 	for (k = 0; k < extra_var; ++k)
   3254  1.1  mrg 		isl_int_clear(expanded[k].val);
   3255  1.1  mrg 	free(expanded);
   3256  1.1  mrg 
   3257  1.1  mrg 	return r;
   3258  1.1  mrg error:
   3259  1.1  mrg 	isl_basic_map_free(bmap);
   3260  1.1  mrg 	return isl_stat_error;
   3261  1.1  mrg }
   3262  1.1  mrg 
   3263  1.1  mrg /* Check if the union of the basic maps represented by info[i] and info[j]
   3264  1.1  mrg  * can be represented by a single basic map,
   3265  1.1  mrg  * after expanding the divs of info[i] to match those of info[j].
   3266  1.1  mrg  * If so, replace the pair by the single basic map and return
   3267  1.1  mrg  * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
   3268  1.1  mrg  * Otherwise, return isl_change_none.
   3269  1.1  mrg  *
   3270  1.1  mrg  * The caller has already checked for info[j] being a subset of info[i].
   3271  1.1  mrg  * If some of the divs of info[j] are unknown, then the expanded info[i]
   3272  1.1  mrg  * will not have the corresponding div constraints.  The other patterns
   3273  1.1  mrg  * therefore cannot apply.  Skip the computation in this case.
   3274  1.1  mrg  *
   3275  1.1  mrg  * The expansion is performed using the divs "div" and expansion "exp"
   3276  1.1  mrg  * computed by the caller.
   3277  1.1  mrg  * info[i].bmap has already been expanded and the result is passed in
   3278  1.1  mrg  * as "bmap".
   3279  1.1  mrg  * The "eq" and "ineq" fields of info[i] reflect the status of
   3280  1.1  mrg  * the constraints of the expanded "bmap" with respect to info[j].tab.
   3281  1.1  mrg  * However, inequality constraints that are redundant in info[i].tab
   3282  1.1  mrg  * have not yet been marked as such because no tableau was available.
   3283  1.1  mrg  *
   3284  1.1  mrg  * Replace info[i].bmap by "bmap" and expand info[i].tab as well,
   3285  1.1  mrg  * updating info[i].ineq with respect to the redundant constraints.
   3286  1.1  mrg  * Then try and coalesce the expanded info[i] with info[j],
   3287  1.1  mrg  * reusing the information in info[i].eq and info[i].ineq.
   3288  1.1  mrg  * If this does not result in any coalescing or if it results in info[j]
   3289  1.1  mrg  * getting dropped (which should not happen in practice, since the case
   3290  1.1  mrg  * of info[j] being a subset of info[i] has already been checked by
   3291  1.1  mrg  * the caller), then revert info[i] to its original state.
   3292  1.1  mrg  */
   3293  1.1  mrg static enum isl_change coalesce_expand_tab_divs(__isl_take isl_basic_map *bmap,
   3294  1.1  mrg 	int i, int j, struct isl_coalesce_info *info, __isl_keep isl_mat *div,
   3295  1.1  mrg 	int *exp)
   3296  1.1  mrg {
   3297  1.1  mrg 	isl_bool known;
   3298  1.1  mrg 	isl_basic_map *bmap_i;
   3299  1.1  mrg 	struct isl_tab_undo *snap;
   3300  1.1  mrg 	enum isl_change change = isl_change_none;
   3301  1.1  mrg 
   3302  1.1  mrg 	known = isl_basic_map_divs_known(info[j].bmap);
   3303  1.1  mrg 	if (known < 0 || !known) {
   3304  1.1  mrg 		clear_status(&info[i]);
   3305  1.1  mrg 		isl_basic_map_free(bmap);
   3306  1.1  mrg 		return known < 0 ? isl_change_error : isl_change_none;
   3307  1.1  mrg 	}
   3308  1.1  mrg 
   3309  1.1  mrg 	bmap_i = isl_basic_map_copy(info[i].bmap);
   3310  1.1  mrg 	snap = isl_tab_snap(info[i].tab);
   3311  1.1  mrg 	if (expand_tab(&info[i], exp, bmap) < 0)
   3312  1.1  mrg 		change = isl_change_error;
   3313  1.1  mrg 
   3314  1.1  mrg 	init_status(&info[j]);
   3315  1.1  mrg 	if (change == isl_change_none)
   3316  1.1  mrg 		change = coalesce_local_pair_reuse(i, j, info);
   3317  1.1  mrg 	else
   3318  1.1  mrg 		clear_status(&info[i]);
   3319  1.1  mrg 	if (change != isl_change_none && change != isl_change_drop_second) {
   3320  1.1  mrg 		isl_basic_map_free(bmap_i);
   3321  1.1  mrg 	} else {
   3322  1.1  mrg 		isl_basic_map_free(info[i].bmap);
   3323  1.1  mrg 		info[i].bmap = bmap_i;
   3324  1.1  mrg 
   3325  1.1  mrg 		if (isl_tab_rollback(info[i].tab, snap) < 0)
   3326  1.1  mrg 			change = isl_change_error;
   3327  1.1  mrg 	}
   3328  1.1  mrg 
   3329  1.1  mrg 	return change;
   3330  1.1  mrg }
   3331  1.1  mrg 
   3332  1.1  mrg /* Check if the union of "bmap" and the basic map represented by info[j]
   3333  1.1  mrg  * can be represented by a single basic map,
   3334  1.1  mrg  * after expanding the divs of "bmap" to match those of info[j].
   3335  1.1  mrg  * If so, replace the pair by the single basic map and return
   3336  1.1  mrg  * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
   3337  1.1  mrg  * Otherwise, return isl_change_none.
   3338  1.1  mrg  *
   3339  1.1  mrg  * In particular, check if the expanded "bmap" contains the basic map
   3340  1.1  mrg  * represented by the tableau info[j].tab.
   3341  1.1  mrg  * The expansion is performed using the divs "div" and expansion "exp"
   3342  1.1  mrg  * computed by the caller.
   3343  1.1  mrg  * Then we check if all constraints of the expanded "bmap" are valid for
   3344  1.1  mrg  * info[j].tab.
   3345  1.1  mrg  *
   3346  1.1  mrg  * If "i" is not equal to -1, then "bmap" is equal to info[i].bmap.
   3347  1.1  mrg  * In this case, the positions of the constraints of info[i].bmap
   3348  1.1  mrg  * with respect to the basic map represented by info[j] are stored
   3349  1.1  mrg  * in info[i].
   3350  1.1  mrg  *
   3351  1.1  mrg  * If the expanded "bmap" does not contain the basic map
   3352  1.1  mrg  * represented by the tableau info[j].tab and if "i" is not -1,
   3353  1.1  mrg  * i.e., if the original "bmap" is info[i].bmap, then expand info[i].tab
   3354  1.1  mrg  * as well and check if that results in coalescing.
   3355  1.1  mrg  */
   3356  1.1  mrg static enum isl_change coalesce_with_expanded_divs(
   3357  1.1  mrg 	__isl_keep isl_basic_map *bmap, int i, int j,
   3358  1.1  mrg 	struct isl_coalesce_info *info, __isl_keep isl_mat *div, int *exp)
   3359  1.1  mrg {
   3360  1.1  mrg 	enum isl_change change = isl_change_none;
   3361  1.1  mrg 	struct isl_coalesce_info info_local, *info_i;
   3362  1.1  mrg 
   3363  1.1  mrg 	info_i = i >= 0 ? &info[i] : &info_local;
   3364  1.1  mrg 	init_status(info_i);
   3365  1.1  mrg 	bmap = isl_basic_map_copy(bmap);
   3366  1.1  mrg 	bmap = isl_basic_map_expand_divs(bmap, isl_mat_copy(div), exp);
   3367  1.1  mrg 	bmap = isl_basic_map_mark_final(bmap);
   3368  1.1  mrg 
   3369  1.1  mrg 	if (!bmap)
   3370  1.1  mrg 		goto error;
   3371  1.1  mrg 
   3372  1.1  mrg 	info_local.bmap = bmap;
   3373  1.1  mrg 	info_i->eq = eq_status_in(bmap, info[j].tab);
   3374  1.1  mrg 	if (bmap->n_eq && !info_i->eq)
   3375  1.1  mrg 		goto error;
   3376  1.1  mrg 	if (any_eq(info_i, STATUS_ERROR))
   3377  1.1  mrg 		goto error;
   3378  1.1  mrg 	if (any_eq(info_i, STATUS_SEPARATE))
   3379  1.1  mrg 		goto done;
   3380  1.1  mrg 
   3381  1.1  mrg 	info_i->ineq = ineq_status_in(bmap, NULL, info[j].tab);
   3382  1.1  mrg 	if (bmap->n_ineq && !info_i->ineq)
   3383  1.1  mrg 		goto error;
   3384  1.1  mrg 	if (any_ineq(info_i, STATUS_ERROR))
   3385  1.1  mrg 		goto error;
   3386  1.1  mrg 	if (any_ineq(info_i, STATUS_SEPARATE))
   3387  1.1  mrg 		goto done;
   3388  1.1  mrg 
   3389  1.1  mrg 	if (all(info_i->eq, 2 * bmap->n_eq, STATUS_VALID) &&
   3390  1.1  mrg 	    all(info_i->ineq, bmap->n_ineq, STATUS_VALID)) {
   3391  1.1  mrg 		drop(&info[j]);
   3392  1.1  mrg 		change = isl_change_drop_second;
   3393  1.1  mrg 	}
   3394  1.1  mrg 
   3395  1.1  mrg 	if (change == isl_change_none && i != -1)
   3396  1.1  mrg 		return coalesce_expand_tab_divs(bmap, i, j, info, div, exp);
   3397  1.1  mrg 
   3398  1.1  mrg done:
   3399  1.1  mrg 	isl_basic_map_free(bmap);
   3400  1.1  mrg 	clear_status(info_i);
   3401  1.1  mrg 	return change;
   3402  1.1  mrg error:
   3403  1.1  mrg 	isl_basic_map_free(bmap);
   3404  1.1  mrg 	clear_status(info_i);
   3405  1.1  mrg 	return isl_change_error;
   3406  1.1  mrg }
   3407  1.1  mrg 
   3408  1.1  mrg /* Check if the union of "bmap_i" and the basic map represented by info[j]
   3409  1.1  mrg  * can be represented by a single basic map,
   3410  1.1  mrg  * after aligning the divs of "bmap_i" to match those of info[j].
   3411  1.1  mrg  * If so, replace the pair by the single basic map and return
   3412  1.1  mrg  * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
   3413  1.1  mrg  * Otherwise, return isl_change_none.
   3414  1.1  mrg  *
   3415  1.1  mrg  * In particular, check if "bmap_i" contains the basic map represented by
   3416  1.1  mrg  * info[j] after aligning the divs of "bmap_i" to those of info[j].
   3417  1.1  mrg  * Note that this can only succeed if the number of divs of "bmap_i"
   3418  1.1  mrg  * is smaller than (or equal to) the number of divs of info[j].
   3419  1.1  mrg  *
   3420  1.1  mrg  * We first check if the divs of "bmap_i" are all known and form a subset
   3421  1.1  mrg  * of those of info[j].bmap.  If so, we pass control over to
   3422  1.1  mrg  * coalesce_with_expanded_divs.
   3423  1.1  mrg  *
   3424  1.1  mrg  * If "i" is not equal to -1, then "bmap" is equal to info[i].bmap.
   3425  1.1  mrg  */
   3426  1.1  mrg static enum isl_change coalesce_after_aligning_divs(
   3427  1.1  mrg 	__isl_keep isl_basic_map *bmap_i, int i, int j,
   3428  1.1  mrg 	struct isl_coalesce_info *info)
   3429  1.1  mrg {
   3430  1.1  mrg 	isl_bool known;
   3431  1.1  mrg 	isl_mat *div_i, *div_j, *div;
   3432  1.1  mrg 	int *exp1 = NULL;
   3433  1.1  mrg 	int *exp2 = NULL;
   3434  1.1  mrg 	isl_ctx *ctx;
   3435  1.1  mrg 	enum isl_change change;
   3436  1.1  mrg 
   3437  1.1  mrg 	known = isl_basic_map_divs_known(bmap_i);
   3438  1.1  mrg 	if (known < 0)
   3439  1.1  mrg 		return isl_change_error;
   3440  1.1  mrg 	if (!known)
   3441  1.1  mrg 		return isl_change_none;
   3442  1.1  mrg 
   3443  1.1  mrg 	ctx = isl_basic_map_get_ctx(bmap_i);
   3444  1.1  mrg 
   3445  1.1  mrg 	div_i = isl_basic_map_get_divs(bmap_i);
   3446  1.1  mrg 	div_j = isl_basic_map_get_divs(info[j].bmap);
   3447  1.1  mrg 
   3448  1.1  mrg 	if (!div_i || !div_j)
   3449  1.1  mrg 		goto error;
   3450  1.1  mrg 
   3451  1.1  mrg 	exp1 = isl_alloc_array(ctx, int, div_i->n_row);
   3452  1.1  mrg 	exp2 = isl_alloc_array(ctx, int, div_j->n_row);
   3453  1.1  mrg 	if ((div_i->n_row && !exp1) || (div_j->n_row && !exp2))
   3454  1.1  mrg 		goto error;
   3455  1.1  mrg 
   3456  1.1  mrg 	div = isl_merge_divs(div_i, div_j, exp1, exp2);
   3457  1.1  mrg 	if (!div)
   3458  1.1  mrg 		goto error;
   3459  1.1  mrg 
   3460  1.1  mrg 	if (div->n_row == div_j->n_row)
   3461  1.1  mrg 		change = coalesce_with_expanded_divs(bmap_i,
   3462  1.1  mrg 							i, j, info, div, exp1);
   3463  1.1  mrg 	else
   3464  1.1  mrg 		change = isl_change_none;
   3465  1.1  mrg 
   3466  1.1  mrg 	isl_mat_free(div);
   3467  1.1  mrg 
   3468  1.1  mrg 	isl_mat_free(div_i);
   3469  1.1  mrg 	isl_mat_free(div_j);
   3470  1.1  mrg 
   3471  1.1  mrg 	free(exp2);
   3472  1.1  mrg 	free(exp1);
   3473  1.1  mrg 
   3474  1.1  mrg 	return change;
   3475  1.1  mrg error:
   3476  1.1  mrg 	isl_mat_free(div_i);
   3477  1.1  mrg 	isl_mat_free(div_j);
   3478  1.1  mrg 	free(exp1);
   3479  1.1  mrg 	free(exp2);
   3480  1.1  mrg 	return isl_change_error;
   3481  1.1  mrg }
   3482  1.1  mrg 
   3483  1.1  mrg /* Check if basic map "j" is a subset of basic map "i" after
   3484  1.1  mrg  * exploiting the extra equalities of "j" to simplify the divs of "i".
   3485  1.1  mrg  * If so, remove basic map "j" and return isl_change_drop_second.
   3486  1.1  mrg  *
   3487  1.1  mrg  * If "j" does not have any equalities or if they are the same
   3488  1.1  mrg  * as those of "i", then we cannot exploit them to simplify the divs.
   3489  1.1  mrg  * Similarly, if there are no divs in "i", then they cannot be simplified.
   3490  1.1  mrg  * If, on the other hand, the affine hulls of "i" and "j" do not intersect,
   3491  1.1  mrg  * then "j" cannot be a subset of "i".
   3492  1.1  mrg  *
   3493  1.1  mrg  * Otherwise, we intersect "i" with the affine hull of "j" and then
   3494  1.1  mrg  * check if "j" is a subset of the result after aligning the divs.
   3495  1.1  mrg  * If so, then "j" is definitely a subset of "i" and can be removed.
   3496  1.1  mrg  * Note that if after intersection with the affine hull of "j".
   3497  1.1  mrg  * "i" still has more divs than "j", then there is no way we can
   3498  1.1  mrg  * align the divs of "i" to those of "j".
   3499  1.1  mrg  */
   3500  1.1  mrg static enum isl_change coalesce_subset_with_equalities(int i, int j,
   3501  1.1  mrg 	struct isl_coalesce_info *info)
   3502  1.1  mrg {
   3503  1.1  mrg 	isl_basic_map *hull_i, *hull_j, *bmap_i;
   3504  1.1  mrg 	int equal, empty;
   3505  1.1  mrg 	enum isl_change change;
   3506  1.1  mrg 
   3507  1.1  mrg 	if (info[j].bmap->n_eq == 0)
   3508  1.1  mrg 		return isl_change_none;
   3509  1.1  mrg 	if (info[i].bmap->n_div == 0)
   3510  1.1  mrg 		return isl_change_none;
   3511  1.1  mrg 
   3512  1.1  mrg 	hull_i = isl_basic_map_copy(info[i].bmap);
   3513  1.1  mrg 	hull_i = isl_basic_map_plain_affine_hull(hull_i);
   3514  1.1  mrg 	hull_j = isl_basic_map_copy(info[j].bmap);
   3515  1.1  mrg 	hull_j = isl_basic_map_plain_affine_hull(hull_j);
   3516  1.1  mrg 
   3517  1.1  mrg 	hull_j = isl_basic_map_intersect(hull_j, isl_basic_map_copy(hull_i));
   3518  1.1  mrg 	equal = isl_basic_map_plain_is_equal(hull_i, hull_j);
   3519  1.1  mrg 	empty = isl_basic_map_plain_is_empty(hull_j);
   3520  1.1  mrg 	isl_basic_map_free(hull_i);
   3521  1.1  mrg 
   3522  1.1  mrg 	if (equal < 0 || equal || empty < 0 || empty) {
   3523  1.1  mrg 		isl_basic_map_free(hull_j);
   3524  1.1  mrg 		if (equal < 0 || empty < 0)
   3525  1.1  mrg 			return isl_change_error;
   3526  1.1  mrg 		return isl_change_none;
   3527  1.1  mrg 	}
   3528  1.1  mrg 
   3529  1.1  mrg 	bmap_i = isl_basic_map_copy(info[i].bmap);
   3530  1.1  mrg 	bmap_i = isl_basic_map_intersect(bmap_i, hull_j);
   3531  1.1  mrg 	if (!bmap_i)
   3532  1.1  mrg 		return isl_change_error;
   3533  1.1  mrg 
   3534  1.1  mrg 	if (bmap_i->n_div > info[j].bmap->n_div) {
   3535  1.1  mrg 		isl_basic_map_free(bmap_i);
   3536  1.1  mrg 		return isl_change_none;
   3537  1.1  mrg 	}
   3538  1.1  mrg 
   3539  1.1  mrg 	change = coalesce_after_aligning_divs(bmap_i, -1, j, info);
   3540  1.1  mrg 
   3541  1.1  mrg 	isl_basic_map_free(bmap_i);
   3542  1.1  mrg 
   3543  1.1  mrg 	return change;
   3544  1.1  mrg }
   3545  1.1  mrg 
   3546  1.1  mrg /* Check if the union of the basic maps represented by info[i] and info[j]
   3547  1.1  mrg  * can be represented by a single basic map, by aligning or equating
   3548  1.1  mrg  * their integer divisions.
   3549  1.1  mrg  * If so, replace the pair by the single basic map and return
   3550  1.1  mrg  * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
   3551  1.1  mrg  * Otherwise, return isl_change_none.
   3552  1.1  mrg  *
   3553  1.1  mrg  * Note that we only perform any test if the number of divs is different
   3554  1.1  mrg  * in the two basic maps.  In case the number of divs is the same,
   3555  1.1  mrg  * we have already established that the divs are different
   3556  1.1  mrg  * in the two basic maps.
   3557  1.1  mrg  * In particular, if the number of divs of basic map i is smaller than
   3558  1.1  mrg  * the number of divs of basic map j, then we check if j is a subset of i
   3559  1.1  mrg  * and vice versa.
   3560  1.1  mrg  */
   3561  1.1  mrg static enum isl_change coalesce_divs(int i, int j,
   3562  1.1  mrg 	struct isl_coalesce_info *info)
   3563  1.1  mrg {
   3564  1.1  mrg 	enum isl_change change = isl_change_none;
   3565  1.1  mrg 
   3566  1.1  mrg 	if (info[i].bmap->n_div < info[j].bmap->n_div)
   3567  1.1  mrg 		change = coalesce_after_aligning_divs(info[i].bmap, i, j, info);
   3568  1.1  mrg 	if (change != isl_change_none)
   3569  1.1  mrg 		return change;
   3570  1.1  mrg 
   3571  1.1  mrg 	if (info[j].bmap->n_div < info[i].bmap->n_div)
   3572  1.1  mrg 		change = coalesce_after_aligning_divs(info[j].bmap, j, i, info);
   3573  1.1  mrg 	if (change != isl_change_none)
   3574  1.1  mrg 		return invert_change(change);
   3575  1.1  mrg 
   3576  1.1  mrg 	change = coalesce_subset_with_equalities(i, j, info);
   3577  1.1  mrg 	if (change != isl_change_none)
   3578  1.1  mrg 		return change;
   3579  1.1  mrg 
   3580  1.1  mrg 	change = coalesce_subset_with_equalities(j, i, info);
   3581  1.1  mrg 	if (change != isl_change_none)
   3582  1.1  mrg 		return invert_change(change);
   3583  1.1  mrg 
   3584  1.1  mrg 	return isl_change_none;
   3585  1.1  mrg }
   3586  1.1  mrg 
   3587  1.1  mrg /* Does "bmap" involve any divs that themselves refer to divs?
   3588  1.1  mrg  */
   3589  1.1  mrg static isl_bool has_nested_div(__isl_keep isl_basic_map *bmap)
   3590  1.1  mrg {
   3591  1.1  mrg 	int i;
   3592  1.1  mrg 	isl_size total;
   3593  1.1  mrg 	isl_size n_div;
   3594  1.1  mrg 
   3595  1.1  mrg 	total = isl_basic_map_dim(bmap, isl_dim_all);
   3596  1.1  mrg 	n_div = isl_basic_map_dim(bmap, isl_dim_div);
   3597  1.1  mrg 	if (total < 0 || n_div < 0)
   3598  1.1  mrg 		return isl_bool_error;
   3599  1.1  mrg 	total -= n_div;
   3600  1.1  mrg 
   3601  1.1  mrg 	for (i = 0; i < n_div; ++i)
   3602  1.1  mrg 		if (isl_seq_first_non_zero(bmap->div[i] + 2 + total,
   3603  1.1  mrg 					    n_div) != -1)
   3604  1.1  mrg 			return isl_bool_true;
   3605  1.1  mrg 
   3606  1.1  mrg 	return isl_bool_false;
   3607  1.1  mrg }
   3608  1.1  mrg 
   3609  1.1  mrg /* Return a list of affine expressions, one for each integer division
   3610  1.1  mrg  * in "bmap_i".  For each integer division that also appears in "bmap_j",
   3611  1.1  mrg  * the affine expression is set to NaN.  The number of NaNs in the list
   3612  1.1  mrg  * is equal to the number of integer divisions in "bmap_j".
   3613  1.1  mrg  * For the other integer divisions of "bmap_i", the corresponding
   3614  1.1  mrg  * element in the list is a purely affine expression equal to the integer
   3615  1.1  mrg  * division in "hull".
   3616  1.1  mrg  * If no such list can be constructed, then the number of elements
   3617  1.1  mrg  * in the returned list is smaller than the number of integer divisions
   3618  1.1  mrg  * in "bmap_i".
   3619  1.1  mrg  * The integer division of "bmap_i" and "bmap_j" are assumed to be known and
   3620  1.1  mrg  * not contain any nested divs.
   3621  1.1  mrg  */
   3622  1.1  mrg static __isl_give isl_aff_list *set_up_substitutions(
   3623  1.1  mrg 	__isl_keep isl_basic_map *bmap_i, __isl_keep isl_basic_map *bmap_j,
   3624  1.1  mrg 	__isl_take isl_basic_map *hull)
   3625  1.1  mrg {
   3626  1.1  mrg 	isl_size n_div_i, n_div_j, total;
   3627  1.1  mrg 	isl_ctx *ctx;
   3628  1.1  mrg 	isl_local_space *ls;
   3629  1.1  mrg 	isl_basic_set *wrap_hull;
   3630  1.1  mrg 	isl_aff *aff_nan;
   3631  1.1  mrg 	isl_aff_list *list;
   3632  1.1  mrg 	int i, j;
   3633  1.1  mrg 
   3634  1.1  mrg 	n_div_i = isl_basic_map_dim(bmap_i, isl_dim_div);
   3635  1.1  mrg 	n_div_j = isl_basic_map_dim(bmap_j, isl_dim_div);
   3636  1.1  mrg 	total = isl_basic_map_dim(bmap_i, isl_dim_all);
   3637  1.1  mrg 	if (!hull || n_div_i < 0 || n_div_j < 0 || total < 0)
   3638  1.1  mrg 		return NULL;
   3639  1.1  mrg 
   3640  1.1  mrg 	ctx = isl_basic_map_get_ctx(hull);
   3641  1.1  mrg 	total -= n_div_i;
   3642  1.1  mrg 
   3643  1.1  mrg 	ls = isl_basic_map_get_local_space(bmap_i);
   3644  1.1  mrg 	ls = isl_local_space_wrap(ls);
   3645  1.1  mrg 	wrap_hull = isl_basic_map_wrap(hull);
   3646  1.1  mrg 
   3647  1.1  mrg 	aff_nan = isl_aff_nan_on_domain(isl_local_space_copy(ls));
   3648  1.1  mrg 	list = isl_aff_list_alloc(ctx, n_div_i);
   3649  1.1  mrg 
   3650  1.1  mrg 	j = 0;
   3651  1.1  mrg 	for (i = 0; i < n_div_i; ++i) {
   3652  1.1  mrg 		isl_aff *aff;
   3653  1.1  mrg 		isl_size n_div;
   3654  1.1  mrg 
   3655  1.1  mrg 		if (j < n_div_j &&
   3656  1.1  mrg 		    isl_basic_map_equal_div_expr_part(bmap_i, i, bmap_j, j,
   3657  1.1  mrg 						    0, 2 + total)) {
   3658  1.1  mrg 			++j;
   3659  1.1  mrg 			list = isl_aff_list_add(list, isl_aff_copy(aff_nan));
   3660  1.1  mrg 			continue;
   3661  1.1  mrg 		}
   3662  1.1  mrg 		if (n_div_i - i <= n_div_j - j)
   3663  1.1  mrg 			break;
   3664  1.1  mrg 
   3665  1.1  mrg 		aff = isl_local_space_get_div(ls, i);
   3666  1.1  mrg 		aff = isl_aff_substitute_equalities(aff,
   3667  1.1  mrg 						isl_basic_set_copy(wrap_hull));
   3668  1.1  mrg 		aff = isl_aff_floor(aff);
   3669  1.1  mrg 		n_div = isl_aff_dim(aff, isl_dim_div);
   3670  1.1  mrg 		if (n_div < 0)
   3671  1.1  mrg 			goto error;
   3672  1.1  mrg 		if (n_div != 0) {
   3673  1.1  mrg 			isl_aff_free(aff);
   3674  1.1  mrg 			break;
   3675  1.1  mrg 		}
   3676  1.1  mrg 
   3677  1.1  mrg 		list = isl_aff_list_add(list, aff);
   3678  1.1  mrg 	}
   3679  1.1  mrg 
   3680  1.1  mrg 	isl_aff_free(aff_nan);
   3681  1.1  mrg 	isl_local_space_free(ls);
   3682  1.1  mrg 	isl_basic_set_free(wrap_hull);
   3683  1.1  mrg 
   3684  1.1  mrg 	return list;
   3685  1.1  mrg error:
   3686  1.1  mrg 	isl_aff_free(aff_nan);
   3687  1.1  mrg 	isl_local_space_free(ls);
   3688  1.1  mrg 	isl_basic_set_free(wrap_hull);
   3689  1.1  mrg 	isl_aff_list_free(list);
   3690  1.1  mrg 	return NULL;
   3691  1.1  mrg }
   3692  1.1  mrg 
   3693  1.1  mrg /* Add variables to info->bmap and info->tab corresponding to the elements
   3694  1.1  mrg  * in "list" that are not set to NaN.
   3695  1.1  mrg  * "extra_var" is the number of these elements.
   3696  1.1  mrg  * "dim" is the offset in the variables of "tab" where we should
   3697  1.1  mrg  * start considering the elements in "list".
   3698  1.1  mrg  * When this function returns, the total number of variables in "tab"
   3699  1.1  mrg  * is equal to "dim" plus the number of elements in "list".
   3700  1.1  mrg  *
   3701  1.1  mrg  * The newly added existentially quantified variables are not given
   3702  1.1  mrg  * an explicit representation because the corresponding div constraints
   3703  1.1  mrg  * do not appear in info->bmap.  These constraints are not added
   3704  1.1  mrg  * to info->bmap because for internal consistency, they would need to
   3705  1.1  mrg  * be added to info->tab as well, where they could combine with the equality
   3706  1.1  mrg  * that is added later to result in constraints that do not hold
   3707  1.1  mrg  * in the original input.
   3708  1.1  mrg  */
   3709  1.1  mrg static isl_stat add_sub_vars(struct isl_coalesce_info *info,
   3710  1.1  mrg 	__isl_keep isl_aff_list *list, int dim, int extra_var)
   3711  1.1  mrg {
   3712  1.1  mrg 	int i, j, d;
   3713  1.1  mrg 	isl_size n;
   3714  1.1  mrg 
   3715  1.1  mrg 	info->bmap = isl_basic_map_cow(info->bmap);
   3716  1.1  mrg 	info->bmap = isl_basic_map_extend(info->bmap, extra_var, 0, 0);
   3717  1.1  mrg 	n = isl_aff_list_n_aff(list);
   3718  1.1  mrg 	if (!info->bmap || n < 0)
   3719  1.1  mrg 		return isl_stat_error;
   3720  1.1  mrg 	for (i = 0; i < n; ++i) {
   3721  1.1  mrg 		int is_nan;
   3722  1.1  mrg 		isl_aff *aff;
   3723  1.1  mrg 
   3724  1.1  mrg 		aff = isl_aff_list_get_aff(list, i);
   3725  1.1  mrg 		is_nan = isl_aff_is_nan(aff);
   3726  1.1  mrg 		isl_aff_free(aff);
   3727  1.1  mrg 		if (is_nan < 0)
   3728  1.1  mrg 			return isl_stat_error;
   3729  1.1  mrg 		if (is_nan)
   3730  1.1  mrg 			continue;
   3731  1.1  mrg 
   3732  1.1  mrg 		if (isl_tab_insert_var(info->tab, dim + i) < 0)
   3733  1.1  mrg 			return isl_stat_error;
   3734  1.1  mrg 		d = isl_basic_map_alloc_div(info->bmap);
   3735  1.1  mrg 		if (d < 0)
   3736  1.1  mrg 			return isl_stat_error;
   3737  1.1  mrg 		info->bmap = isl_basic_map_mark_div_unknown(info->bmap, d);
   3738  1.1  mrg 		for (j = d; j > i; --j)
   3739  1.1  mrg 			info->bmap = isl_basic_map_swap_div(info->bmap,
   3740  1.1  mrg 							    j - 1, j);
   3741  1.1  mrg 		if (!info->bmap)
   3742  1.1  mrg 			return isl_stat_error;
   3743  1.1  mrg 	}
   3744  1.1  mrg 
   3745  1.1  mrg 	return isl_stat_ok;
   3746  1.1  mrg }
   3747  1.1  mrg 
   3748  1.1  mrg /* For each element in "list" that is not set to NaN, fix the corresponding
   3749  1.1  mrg  * variable in "tab" to the purely affine expression defined by the element.
   3750  1.1  mrg  * "dim" is the offset in the variables of "tab" where we should
   3751  1.1  mrg  * start considering the elements in "list".
   3752  1.1  mrg  *
   3753  1.1  mrg  * This function assumes that a sufficient number of rows and
   3754  1.1  mrg  * elements in the constraint array are available in the tableau.
   3755  1.1  mrg  */
   3756  1.1  mrg static isl_stat add_sub_equalities(struct isl_tab *tab,
   3757  1.1  mrg 	__isl_keep isl_aff_list *list, int dim)
   3758  1.1  mrg {
   3759  1.1  mrg 	int i;
   3760  1.1  mrg 	isl_size n;
   3761  1.1  mrg 	isl_ctx *ctx;
   3762  1.1  mrg 	isl_vec *sub;
   3763  1.1  mrg 	isl_aff *aff;
   3764  1.1  mrg 
   3765  1.1  mrg 	n = isl_aff_list_n_aff(list);
   3766  1.1  mrg 	if (n < 0)
   3767  1.1  mrg 		return isl_stat_error;
   3768  1.1  mrg 
   3769  1.1  mrg 	ctx = isl_tab_get_ctx(tab);
   3770  1.1  mrg 	sub = isl_vec_alloc(ctx, 1 + dim + n);
   3771  1.1  mrg 	if (!sub)
   3772  1.1  mrg 		return isl_stat_error;
   3773  1.1  mrg 	isl_seq_clr(sub->el + 1 + dim, n);
   3774  1.1  mrg 
   3775  1.1  mrg 	for (i = 0; i < n; ++i) {
   3776  1.1  mrg 		aff = isl_aff_list_get_aff(list, i);
   3777  1.1  mrg 		if (!aff)
   3778  1.1  mrg 			goto error;
   3779  1.1  mrg 		if (isl_aff_is_nan(aff)) {
   3780  1.1  mrg 			isl_aff_free(aff);
   3781  1.1  mrg 			continue;
   3782  1.1  mrg 		}
   3783  1.1  mrg 		isl_seq_cpy(sub->el, aff->v->el + 1, 1 + dim);
   3784  1.1  mrg 		isl_int_neg(sub->el[1 + dim + i], aff->v->el[0]);
   3785  1.1  mrg 		if (isl_tab_add_eq(tab, sub->el) < 0)
   3786  1.1  mrg 			goto error;
   3787  1.1  mrg 		isl_int_set_si(sub->el[1 + dim + i], 0);
   3788  1.1  mrg 		isl_aff_free(aff);
   3789  1.1  mrg 	}
   3790  1.1  mrg 
   3791  1.1  mrg 	isl_vec_free(sub);
   3792  1.1  mrg 	return isl_stat_ok;
   3793  1.1  mrg error:
   3794  1.1  mrg 	isl_aff_free(aff);
   3795  1.1  mrg 	isl_vec_free(sub);
   3796  1.1  mrg 	return isl_stat_error;
   3797  1.1  mrg }
   3798  1.1  mrg 
   3799  1.1  mrg /* Add variables to info->tab and info->bmap corresponding to the elements
   3800  1.1  mrg  * in "list" that are not set to NaN.  The value of the added variable
   3801  1.1  mrg  * in info->tab is fixed to the purely affine expression defined by the element.
   3802  1.1  mrg  * "dim" is the offset in the variables of info->tab where we should
   3803  1.1  mrg  * start considering the elements in "list".
   3804  1.1  mrg  * When this function returns, the total number of variables in info->tab
   3805  1.1  mrg  * is equal to "dim" plus the number of elements in "list".
   3806  1.1  mrg  */
   3807  1.1  mrg static isl_stat add_subs(struct isl_coalesce_info *info,
   3808  1.1  mrg 	__isl_keep isl_aff_list *list, int dim)
   3809  1.1  mrg {
   3810  1.1  mrg 	int extra_var;
   3811  1.1  mrg 	isl_size n;
   3812  1.1  mrg 
   3813  1.1  mrg 	n = isl_aff_list_n_aff(list);
   3814  1.1  mrg 	if (n < 0)
   3815  1.1  mrg 		return isl_stat_error;
   3816  1.1  mrg 
   3817  1.1  mrg 	extra_var = n - (info->tab->n_var - dim);
   3818  1.1  mrg 
   3819  1.1  mrg 	if (isl_tab_extend_vars(info->tab, extra_var) < 0)
   3820  1.1  mrg 		return isl_stat_error;
   3821  1.1  mrg 	if (isl_tab_extend_cons(info->tab, 2 * extra_var) < 0)
   3822  1.1  mrg 		return isl_stat_error;
   3823  1.1  mrg 	if (add_sub_vars(info, list, dim, extra_var) < 0)
   3824  1.1  mrg 		return isl_stat_error;
   3825  1.1  mrg 
   3826  1.1  mrg 	return add_sub_equalities(info->tab, list, dim);
   3827  1.1  mrg }
   3828  1.1  mrg 
   3829  1.1  mrg /* Coalesce basic map "j" into basic map "i" after adding the extra integer
   3830  1.1  mrg  * divisions in "i" but not in "j" to basic map "j", with values
   3831  1.1  mrg  * specified by "list".  The total number of elements in "list"
   3832  1.1  mrg  * is equal to the number of integer divisions in "i", while the number
   3833  1.1  mrg  * of NaN elements in the list is equal to the number of integer divisions
   3834  1.1  mrg  * in "j".
   3835  1.1  mrg  *
   3836  1.1  mrg  * If no coalescing can be performed, then we need to revert basic map "j"
   3837  1.1  mrg  * to its original state.  We do the same if basic map "i" gets dropped
   3838  1.1  mrg  * during the coalescing, even though this should not happen in practice
   3839  1.1  mrg  * since we have already checked for "j" being a subset of "i"
   3840  1.1  mrg  * before we reach this stage.
   3841  1.1  mrg  */
   3842  1.1  mrg static enum isl_change coalesce_with_subs(int i, int j,
   3843  1.1  mrg 	struct isl_coalesce_info *info, __isl_keep isl_aff_list *list)
   3844  1.1  mrg {
   3845  1.1  mrg 	isl_basic_map *bmap_j;
   3846  1.1  mrg 	struct isl_tab_undo *snap;
   3847  1.1  mrg 	isl_size dim, n_div;
   3848  1.1  mrg 	enum isl_change change;
   3849  1.1  mrg 
   3850  1.1  mrg 	bmap_j = isl_basic_map_copy(info[j].bmap);
   3851  1.1  mrg 	snap = isl_tab_snap(info[j].tab);
   3852  1.1  mrg 
   3853  1.1  mrg 	dim = isl_basic_map_dim(bmap_j, isl_dim_all);
   3854  1.1  mrg 	n_div = isl_basic_map_dim(bmap_j, isl_dim_div);
   3855  1.1  mrg 	if (dim < 0 || n_div < 0)
   3856  1.1  mrg 		goto error;
   3857  1.1  mrg 	dim -= n_div;
   3858  1.1  mrg 	if (add_subs(&info[j], list, dim) < 0)
   3859  1.1  mrg 		goto error;
   3860  1.1  mrg 
   3861  1.1  mrg 	change = coalesce_local_pair(i, j, info);
   3862  1.1  mrg 	if (change != isl_change_none && change != isl_change_drop_first) {
   3863  1.1  mrg 		isl_basic_map_free(bmap_j);
   3864  1.1  mrg 	} else {
   3865  1.1  mrg 		isl_basic_map_free(info[j].bmap);
   3866  1.1  mrg 		info[j].bmap = bmap_j;
   3867  1.1  mrg 
   3868  1.1  mrg 		if (isl_tab_rollback(info[j].tab, snap) < 0)
   3869  1.1  mrg 			return isl_change_error;
   3870  1.1  mrg 	}
   3871  1.1  mrg 
   3872  1.1  mrg 	return change;
   3873  1.1  mrg error:
   3874  1.1  mrg 	isl_basic_map_free(bmap_j);
   3875  1.1  mrg 	return isl_change_error;
   3876  1.1  mrg }
   3877  1.1  mrg 
   3878  1.1  mrg /* Check if we can coalesce basic map "j" into basic map "i" after copying
   3879  1.1  mrg  * those extra integer divisions in "i" that can be simplified away
   3880  1.1  mrg  * using the extra equalities in "j".
   3881  1.1  mrg  * All divs are assumed to be known and not contain any nested divs.
   3882  1.1  mrg  *
   3883  1.1  mrg  * We first check if there are any extra equalities in "j" that we
   3884  1.1  mrg  * can exploit.  Then we check if every integer division in "i"
   3885  1.1  mrg  * either already appears in "j" or can be simplified using the
   3886  1.1  mrg  * extra equalities to a purely affine expression.
   3887  1.1  mrg  * If these tests succeed, then we try to coalesce the two basic maps
   3888  1.1  mrg  * by introducing extra dimensions in "j" corresponding to
   3889  1.1  mrg  * the extra integer divisions "i" fixed to the corresponding
   3890  1.1  mrg  * purely affine expression.
   3891  1.1  mrg  */
   3892  1.1  mrg static enum isl_change check_coalesce_into_eq(int i, int j,
   3893  1.1  mrg 	struct isl_coalesce_info *info)
   3894  1.1  mrg {
   3895  1.1  mrg 	isl_size n_div_i, n_div_j, n;
   3896  1.1  mrg 	isl_basic_map *hull_i, *hull_j;
   3897  1.1  mrg 	isl_bool equal, empty;
   3898  1.1  mrg 	isl_aff_list *list;
   3899  1.1  mrg 	enum isl_change change;
   3900  1.1  mrg 
   3901  1.1  mrg 	n_div_i = isl_basic_map_dim(info[i].bmap, isl_dim_div);
   3902  1.1  mrg 	n_div_j = isl_basic_map_dim(info[j].bmap, isl_dim_div);
   3903  1.1  mrg 	if (n_div_i < 0 || n_div_j < 0)
   3904  1.1  mrg 		return isl_change_error;
   3905  1.1  mrg 	if (n_div_i <= n_div_j)
   3906  1.1  mrg 		return isl_change_none;
   3907  1.1  mrg 	if (info[j].bmap->n_eq == 0)
   3908  1.1  mrg 		return isl_change_none;
   3909  1.1  mrg 
   3910  1.1  mrg 	hull_i = isl_basic_map_copy(info[i].bmap);
   3911  1.1  mrg 	hull_i = isl_basic_map_plain_affine_hull(hull_i);
   3912  1.1  mrg 	hull_j = isl_basic_map_copy(info[j].bmap);
   3913  1.1  mrg 	hull_j = isl_basic_map_plain_affine_hull(hull_j);
   3914  1.1  mrg 
   3915  1.1  mrg 	hull_j = isl_basic_map_intersect(hull_j, isl_basic_map_copy(hull_i));
   3916  1.1  mrg 	equal = isl_basic_map_plain_is_equal(hull_i, hull_j);
   3917  1.1  mrg 	empty = isl_basic_map_plain_is_empty(hull_j);
   3918  1.1  mrg 	isl_basic_map_free(hull_i);
   3919  1.1  mrg 
   3920  1.1  mrg 	if (equal < 0 || empty < 0)
   3921  1.1  mrg 		goto error;
   3922  1.1  mrg 	if (equal || empty) {
   3923  1.1  mrg 		isl_basic_map_free(hull_j);
   3924  1.1  mrg 		return isl_change_none;
   3925  1.1  mrg 	}
   3926  1.1  mrg 
   3927  1.1  mrg 	list = set_up_substitutions(info[i].bmap, info[j].bmap, hull_j);
   3928  1.1  mrg 	if (!list)
   3929  1.1  mrg 		return isl_change_error;
   3930  1.1  mrg 	n = isl_aff_list_n_aff(list);
   3931  1.1  mrg 	if (n < 0)
   3932  1.1  mrg 		change = isl_change_error;
   3933  1.1  mrg 	else if (n < n_div_i)
   3934  1.1  mrg 		change = isl_change_none;
   3935  1.1  mrg 	else
   3936  1.1  mrg 		change = coalesce_with_subs(i, j, info, list);
   3937  1.1  mrg 
   3938  1.1  mrg 	isl_aff_list_free(list);
   3939  1.1  mrg 
   3940  1.1  mrg 	return change;
   3941  1.1  mrg error:
   3942  1.1  mrg 	isl_basic_map_free(hull_j);
   3943  1.1  mrg 	return isl_change_error;
   3944  1.1  mrg }
   3945  1.1  mrg 
   3946  1.1  mrg /* Check if we can coalesce basic maps "i" and "j" after copying
   3947  1.1  mrg  * those extra integer divisions in one of the basic maps that can
   3948  1.1  mrg  * be simplified away using the extra equalities in the other basic map.
   3949  1.1  mrg  * We require all divs to be known in both basic maps.
   3950  1.1  mrg  * Furthermore, to simplify the comparison of div expressions,
   3951  1.1  mrg  * we do not allow any nested integer divisions.
   3952  1.1  mrg  */
   3953  1.1  mrg static enum isl_change check_coalesce_eq(int i, int j,
   3954  1.1  mrg 	struct isl_coalesce_info *info)
   3955  1.1  mrg {
   3956  1.1  mrg 	isl_bool known, nested;
   3957  1.1  mrg 	enum isl_change change;
   3958  1.1  mrg 
   3959  1.1  mrg 	known = isl_basic_map_divs_known(info[i].bmap);
   3960  1.1  mrg 	if (known < 0 || !known)
   3961  1.1  mrg 		return known < 0 ? isl_change_error : isl_change_none;
   3962  1.1  mrg 	known = isl_basic_map_divs_known(info[j].bmap);
   3963  1.1  mrg 	if (known < 0 || !known)
   3964  1.1  mrg 		return known < 0 ? isl_change_error : isl_change_none;
   3965  1.1  mrg 	nested = has_nested_div(info[i].bmap);
   3966  1.1  mrg 	if (nested < 0 || nested)
   3967  1.1  mrg 		return nested < 0 ? isl_change_error : isl_change_none;
   3968  1.1  mrg 	nested = has_nested_div(info[j].bmap);
   3969  1.1  mrg 	if (nested < 0 || nested)
   3970  1.1  mrg 		return nested < 0 ? isl_change_error : isl_change_none;
   3971  1.1  mrg 
   3972  1.1  mrg 	change = check_coalesce_into_eq(i, j, info);
   3973  1.1  mrg 	if (change != isl_change_none)
   3974  1.1  mrg 		return change;
   3975  1.1  mrg 	change = check_coalesce_into_eq(j, i, info);
   3976  1.1  mrg 	if (change != isl_change_none)
   3977  1.1  mrg 		return invert_change(change);
   3978  1.1  mrg 
   3979  1.1  mrg 	return isl_change_none;
   3980  1.1  mrg }
   3981  1.1  mrg 
   3982  1.1  mrg /* Check if the union of the given pair of basic maps
   3983  1.1  mrg  * can be represented by a single basic map.
   3984  1.1  mrg  * If so, replace the pair by the single basic map and return
   3985  1.1  mrg  * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
   3986  1.1  mrg  * Otherwise, return isl_change_none.
   3987  1.1  mrg  *
   3988  1.1  mrg  * We first check if the two basic maps live in the same local space,
   3989  1.1  mrg  * after aligning the divs that differ by only an integer constant.
   3990  1.1  mrg  * If so, we do the complete check.  Otherwise, we check if they have
   3991  1.1  mrg  * the same number of integer divisions and can be coalesced, if one is
   3992  1.1  mrg  * an obvious subset of the other or if the extra integer divisions
   3993  1.1  mrg  * of one basic map can be simplified away using the extra equalities
   3994  1.1  mrg  * of the other basic map.
   3995  1.1  mrg  *
   3996  1.1  mrg  * Note that trying to coalesce pairs of disjuncts with the same
   3997  1.1  mrg  * number, but different local variables may drop the explicit
   3998  1.1  mrg  * representation of some of these local variables.
   3999  1.1  mrg  * This operation is therefore not performed when
   4000  1.1  mrg  * the "coalesce_preserve_locals" option is set.
   4001  1.1  mrg  */
   4002  1.1  mrg static enum isl_change coalesce_pair(int i, int j,
   4003  1.1  mrg 	struct isl_coalesce_info *info)
   4004  1.1  mrg {
   4005  1.1  mrg 	int preserve;
   4006  1.1  mrg 	isl_bool same;
   4007  1.1  mrg 	enum isl_change change;
   4008  1.1  mrg 	isl_ctx *ctx;
   4009  1.1  mrg 
   4010  1.1  mrg 	if (harmonize_divs(&info[i], &info[j]) < 0)
   4011  1.1  mrg 		return isl_change_error;
   4012  1.1  mrg 	same = same_divs(info[i].bmap, info[j].bmap);
   4013  1.1  mrg 	if (same < 0)
   4014  1.1  mrg 		return isl_change_error;
   4015  1.1  mrg 	if (same)
   4016  1.1  mrg 		return coalesce_local_pair(i, j, info);
   4017  1.1  mrg 
   4018  1.1  mrg 	ctx = isl_basic_map_get_ctx(info[i].bmap);
   4019  1.1  mrg 	preserve = isl_options_get_coalesce_preserve_locals(ctx);
   4020  1.1  mrg 	if (!preserve && info[i].bmap->n_div == info[j].bmap->n_div) {
   4021  1.1  mrg 		change = coalesce_local_pair(i, j, info);
   4022  1.1  mrg 		if (change != isl_change_none)
   4023  1.1  mrg 			return change;
   4024  1.1  mrg 	}
   4025  1.1  mrg 
   4026  1.1  mrg 	change = coalesce_divs(i, j, info);
   4027  1.1  mrg 	if (change != isl_change_none)
   4028  1.1  mrg 		return change;
   4029  1.1  mrg 
   4030  1.1  mrg 	return check_coalesce_eq(i, j, info);
   4031  1.1  mrg }
   4032  1.1  mrg 
   4033  1.1  mrg /* Return the maximum of "a" and "b".
   4034  1.1  mrg  */
   4035  1.1  mrg static int isl_max(int a, int b)
   4036  1.1  mrg {
   4037  1.1  mrg 	return a > b ? a : b;
   4038  1.1  mrg }
   4039  1.1  mrg 
   4040  1.1  mrg /* Pairwise coalesce the basic maps in the range [start1, end1[ of "info"
   4041  1.1  mrg  * with those in the range [start2, end2[, skipping basic maps
   4042  1.1  mrg  * that have been removed (either before or within this function).
   4043  1.1  mrg  *
   4044  1.1  mrg  * For each basic map i in the first range, we check if it can be coalesced
   4045  1.1  mrg  * with respect to any previously considered basic map j in the second range.
   4046  1.1  mrg  * If i gets dropped (because it was a subset of some j), then
   4047  1.1  mrg  * we can move on to the next basic map.
   4048  1.1  mrg  * If j gets dropped, we need to continue checking against the other
   4049  1.1  mrg  * previously considered basic maps.
   4050  1.1  mrg  * If the two basic maps got fused, then we recheck the fused basic map
   4051  1.1  mrg  * against the previously considered basic maps, starting at i + 1
   4052  1.1  mrg  * (even if start2 is greater than i + 1).
   4053  1.1  mrg  */
   4054  1.1  mrg static int coalesce_range(isl_ctx *ctx, struct isl_coalesce_info *info,
   4055  1.1  mrg 	int start1, int end1, int start2, int end2)
   4056  1.1  mrg {
   4057  1.1  mrg 	int i, j;
   4058  1.1  mrg 
   4059  1.1  mrg 	for (i = end1 - 1; i >= start1; --i) {
   4060  1.1  mrg 		if (info[i].removed)
   4061  1.1  mrg 			continue;
   4062  1.1  mrg 		for (j = isl_max(i + 1, start2); j < end2; ++j) {
   4063  1.1  mrg 			enum isl_change changed;
   4064  1.1  mrg 
   4065  1.1  mrg 			if (info[j].removed)
   4066  1.1  mrg 				continue;
   4067  1.1  mrg 			if (info[i].removed)
   4068  1.1  mrg 				isl_die(ctx, isl_error_internal,
   4069  1.1  mrg 					"basic map unexpectedly removed",
   4070  1.1  mrg 					return -1);
   4071  1.1  mrg 			changed = coalesce_pair(i, j, info);
   4072  1.1  mrg 			switch (changed) {
   4073  1.1  mrg 			case isl_change_error:
   4074  1.1  mrg 				return -1;
   4075  1.1  mrg 			case isl_change_none:
   4076  1.1  mrg 			case isl_change_drop_second:
   4077  1.1  mrg 				continue;
   4078  1.1  mrg 			case isl_change_drop_first:
   4079  1.1  mrg 				j = end2;
   4080  1.1  mrg 				break;
   4081  1.1  mrg 			case isl_change_fuse:
   4082  1.1  mrg 				j = i;
   4083  1.1  mrg 				break;
   4084  1.1  mrg 			}
   4085  1.1  mrg 		}
   4086  1.1  mrg 	}
   4087  1.1  mrg 
   4088  1.1  mrg 	return 0;
   4089  1.1  mrg }
   4090  1.1  mrg 
   4091  1.1  mrg /* Pairwise coalesce the basic maps described by the "n" elements of "info".
   4092  1.1  mrg  *
   4093  1.1  mrg  * We consider groups of basic maps that live in the same apparent
   4094  1.1  mrg  * affine hull and we first coalesce within such a group before we
   4095  1.1  mrg  * coalesce the elements in the group with elements of previously
   4096  1.1  mrg  * considered groups.  If a fuse happens during the second phase,
   4097  1.1  mrg  * then we also reconsider the elements within the group.
   4098  1.1  mrg  */
   4099  1.1  mrg static int coalesce(isl_ctx *ctx, int n, struct isl_coalesce_info *info)
   4100  1.1  mrg {
   4101  1.1  mrg 	int start, end;
   4102  1.1  mrg 
   4103  1.1  mrg 	for (end = n; end > 0; end = start) {
   4104  1.1  mrg 		start = end - 1;
   4105  1.1  mrg 		while (start >= 1 &&
   4106  1.1  mrg 		    info[start - 1].hull_hash == info[start].hull_hash)
   4107  1.1  mrg 			start--;
   4108  1.1  mrg 		if (coalesce_range(ctx, info, start, end, start, end) < 0)
   4109  1.1  mrg 			return -1;
   4110  1.1  mrg 		if (coalesce_range(ctx, info, start, end, end, n) < 0)
   4111  1.1  mrg 			return -1;
   4112  1.1  mrg 	}
   4113  1.1  mrg 
   4114  1.1  mrg 	return 0;
   4115  1.1  mrg }
   4116  1.1  mrg 
   4117  1.1  mrg /* Update the basic maps in "map" based on the information in "info".
   4118  1.1  mrg  * In particular, remove the basic maps that have been marked removed and
   4119  1.1  mrg  * update the others based on the information in the corresponding tableau.
   4120  1.1  mrg  * Since we detected implicit equalities without calling
   4121  1.1  mrg  * isl_basic_map_gauss, we need to do it now.
   4122  1.1  mrg  * Also call isl_basic_map_simplify if we may have lost the definition
   4123  1.1  mrg  * of one or more integer divisions.
   4124  1.1  mrg  * If a basic map is still equal to the one from which the corresponding "info"
   4125  1.1  mrg  * entry was created, then redundant constraint and
   4126  1.1  mrg  * implicit equality constraint detection have been performed
   4127  1.1  mrg  * on the corresponding tableau and the basic map can be marked as such.
   4128  1.1  mrg  */
   4129  1.1  mrg static __isl_give isl_map *update_basic_maps(__isl_take isl_map *map,
   4130  1.1  mrg 	int n, struct isl_coalesce_info *info)
   4131  1.1  mrg {
   4132  1.1  mrg 	int i;
   4133  1.1  mrg 
   4134  1.1  mrg 	if (!map)
   4135  1.1  mrg 		return NULL;
   4136  1.1  mrg 
   4137  1.1  mrg 	for (i = n - 1; i >= 0; --i) {
   4138  1.1  mrg 		if (info[i].removed) {
   4139  1.1  mrg 			isl_basic_map_free(map->p[i]);
   4140  1.1  mrg 			if (i != map->n - 1)
   4141  1.1  mrg 				map->p[i] = map->p[map->n - 1];
   4142  1.1  mrg 			map->n--;
   4143  1.1  mrg 			continue;
   4144  1.1  mrg 		}
   4145  1.1  mrg 
   4146  1.1  mrg 		info[i].bmap = isl_basic_map_update_from_tab(info[i].bmap,
   4147  1.1  mrg 							info[i].tab);
   4148  1.1  mrg 		info[i].bmap = isl_basic_map_gauss(info[i].bmap, NULL);
   4149  1.1  mrg 		if (info[i].simplify)
   4150  1.1  mrg 			info[i].bmap = isl_basic_map_simplify(info[i].bmap);
   4151  1.1  mrg 		info[i].bmap = isl_basic_map_finalize(info[i].bmap);
   4152  1.1  mrg 		if (!info[i].bmap)
   4153  1.1  mrg 			return isl_map_free(map);
   4154  1.1  mrg 		if (!info[i].modified) {
   4155  1.1  mrg 			ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_NO_IMPLICIT);
   4156  1.1  mrg 			ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_NO_REDUNDANT);
   4157  1.1  mrg 		}
   4158  1.1  mrg 		isl_basic_map_free(map->p[i]);
   4159  1.1  mrg 		map->p[i] = info[i].bmap;
   4160  1.1  mrg 		info[i].bmap = NULL;
   4161  1.1  mrg 	}
   4162  1.1  mrg 
   4163  1.1  mrg 	return map;
   4164  1.1  mrg }
   4165  1.1  mrg 
   4166  1.1  mrg /* For each pair of basic maps in the map, check if the union of the two
   4167  1.1  mrg  * can be represented by a single basic map.
   4168  1.1  mrg  * If so, replace the pair by the single basic map and start over.
   4169  1.1  mrg  *
   4170  1.1  mrg  * We factor out any (hidden) common factor from the constraint
   4171  1.1  mrg  * coefficients to improve the detection of adjacent constraints.
   4172  1.1  mrg  * Note that this function does not call isl_basic_map_gauss,
   4173  1.1  mrg  * but it does make sure that only a single copy of the basic map
   4174  1.1  mrg  * is affected.  This means that isl_basic_map_gauss may have
   4175  1.1  mrg  * to be called at the end of the computation (in update_basic_maps)
   4176  1.1  mrg  * on this single copy to ensure that
   4177  1.1  mrg  * the basic maps are not left in an unexpected state.
   4178  1.1  mrg  *
   4179  1.1  mrg  * Since we are constructing the tableaus of the basic maps anyway,
   4180  1.1  mrg  * we exploit them to detect implicit equalities and redundant constraints.
   4181  1.1  mrg  * This also helps the coalescing as it can ignore the redundant constraints.
   4182  1.1  mrg  * In order to avoid confusion, we make all implicit equalities explicit
   4183  1.1  mrg  * in the basic maps.  If the basic map only has a single reference
   4184  1.1  mrg  * (this happens in particular if it was modified by
   4185  1.1  mrg  * isl_basic_map_reduce_coefficients), then isl_basic_map_gauss
   4186  1.1  mrg  * does not get called on the result.  The call to
   4187  1.1  mrg  * isl_basic_map_gauss in update_basic_maps resolves this as well.
   4188  1.1  mrg  * For each basic map, we also compute the hash of the apparent affine hull
   4189  1.1  mrg  * for use in coalesce.
   4190  1.1  mrg  */
   4191  1.1  mrg __isl_give isl_map *isl_map_coalesce(__isl_take isl_map *map)
   4192  1.1  mrg {
   4193  1.1  mrg 	int i;
   4194  1.1  mrg 	unsigned n;
   4195  1.1  mrg 	isl_ctx *ctx;
   4196  1.1  mrg 	struct isl_coalesce_info *info = NULL;
   4197  1.1  mrg 
   4198  1.1  mrg 	map = isl_map_remove_empty_parts(map);
   4199  1.1  mrg 	if (!map)
   4200  1.1  mrg 		return NULL;
   4201  1.1  mrg 
   4202  1.1  mrg 	if (map->n <= 1)
   4203  1.1  mrg 		return map;
   4204  1.1  mrg 
   4205  1.1  mrg 	ctx = isl_map_get_ctx(map);
   4206  1.1  mrg 	map = isl_map_sort_divs(map);
   4207  1.1  mrg 	map = isl_map_cow(map);
   4208  1.1  mrg 
   4209  1.1  mrg 	if (!map)
   4210  1.1  mrg 		return NULL;
   4211  1.1  mrg 
   4212  1.1  mrg 	n = map->n;
   4213  1.1  mrg 
   4214  1.1  mrg 	info = isl_calloc_array(map->ctx, struct isl_coalesce_info, n);
   4215  1.1  mrg 	if (!info)
   4216  1.1  mrg 		goto error;
   4217  1.1  mrg 
   4218  1.1  mrg 	for (i = 0; i < map->n; ++i) {
   4219  1.1  mrg 		map->p[i] = isl_basic_map_reduce_coefficients(map->p[i]);
   4220  1.1  mrg 		if (!map->p[i])
   4221  1.1  mrg 			goto error;
   4222  1.1  mrg 		info[i].bmap = isl_basic_map_copy(map->p[i]);
   4223  1.1  mrg 		info[i].tab = isl_tab_from_basic_map(info[i].bmap, 0);
   4224  1.1  mrg 		if (!info[i].tab)
   4225  1.1  mrg 			goto error;
   4226  1.1  mrg 		if (!ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_NO_IMPLICIT))
   4227  1.1  mrg 			if (isl_tab_detect_implicit_equalities(info[i].tab) < 0)
   4228  1.1  mrg 				goto error;
   4229  1.1  mrg 		info[i].bmap = isl_tab_make_equalities_explicit(info[i].tab,
   4230  1.1  mrg 								info[i].bmap);
   4231  1.1  mrg 		if (!info[i].bmap)
   4232  1.1  mrg 			goto error;
   4233  1.1  mrg 		if (!ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_NO_REDUNDANT))
   4234  1.1  mrg 			if (isl_tab_detect_redundant(info[i].tab) < 0)
   4235  1.1  mrg 				goto error;
   4236  1.1  mrg 		if (coalesce_info_set_hull_hash(&info[i]) < 0)
   4237  1.1  mrg 			goto error;
   4238  1.1  mrg 	}
   4239  1.1  mrg 	for (i = map->n - 1; i >= 0; --i)
   4240  1.1  mrg 		if (info[i].tab->empty)
   4241  1.1  mrg 			drop(&info[i]);
   4242  1.1  mrg 
   4243  1.1  mrg 	if (coalesce(ctx, n, info) < 0)
   4244  1.1  mrg 		goto error;
   4245  1.1  mrg 
   4246  1.1  mrg 	map = update_basic_maps(map, n, info);
   4247  1.1  mrg 
   4248  1.1  mrg 	clear_coalesce_info(n, info);
   4249  1.1  mrg 
   4250  1.1  mrg 	return map;
   4251  1.1  mrg error:
   4252  1.1  mrg 	clear_coalesce_info(n, info);
   4253  1.1  mrg 	isl_map_free(map);
   4254  1.1  mrg 	return NULL;
   4255  1.1  mrg }
   4256  1.1  mrg 
   4257  1.1  mrg /* For each pair of basic sets in the set, check if the union of the two
   4258  1.1  mrg  * can be represented by a single basic set.
   4259  1.1  mrg  * If so, replace the pair by the single basic set and start over.
   4260  1.1  mrg  */
   4261  1.1  mrg __isl_give isl_set *isl_set_coalesce(__isl_take isl_set *set)
   4262  1.1  mrg {
   4263  1.1  mrg 	return set_from_map(isl_map_coalesce(set_to_map(set)));
   4264  1.1  mrg }
   4265