Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2008-2009 Katholieke Universiteit Leuven |
| 3 | * Copyright 2010 INRIA Saclay |
| 4 | * Copyright 2012-2014 Ecole Normale Superieure |
| 5 | * Copyright 2014 INRIA Rocquencourt |
Tobias Grosser | ccae1ee | 2016-12-22 23:08:57 +0000 | [diff] [blame] | 6 | * Copyright 2016 INRIA Paris |
Michael Kruse | e0b34f3 | 2016-05-04 14:41:36 +0000 | [diff] [blame] | 7 | * Copyright 2016 Sven Verdoolaege |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8 | * |
| 9 | * Use of this software is governed by the MIT license |
| 10 | * |
| 11 | * Written by Sven Verdoolaege, K.U.Leuven, Departement |
| 12 | * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium |
| 13 | * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite, |
| 14 | * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France |
| 15 | * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France |
| 16 | * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt, |
| 17 | * B.P. 105 - 78153 Le Chesnay, France |
Tobias Grosser | ccae1ee | 2016-12-22 23:08:57 +0000 | [diff] [blame] | 18 | * and Centre de Recherche Inria de Paris, 2 rue Simone Iff - Voie DQ12, |
| 19 | * CS 42112, 75589 Paris Cedex 12, France |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #include <string.h> |
| 23 | #include <isl_ctx_private.h> |
| 24 | #include <isl_map_private.h> |
| 25 | #include <isl_blk.h> |
Tobias Grosser | 3e6070e | 2015-05-09 09:37:30 +0000 | [diff] [blame] | 26 | #include <isl/constraint.h> |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 27 | #include "isl_space_private.h" |
| 28 | #include "isl_equalities.h" |
| 29 | #include <isl_lp_private.h> |
| 30 | #include <isl_seq.h> |
| 31 | #include <isl/set.h> |
| 32 | #include <isl/map.h> |
| 33 | #include <isl_reordering.h> |
| 34 | #include "isl_sample.h" |
| 35 | #include <isl_sort.h> |
| 36 | #include "isl_tab.h" |
| 37 | #include <isl/vec.h> |
| 38 | #include <isl_mat_private.h> |
| 39 | #include <isl_vec_private.h> |
| 40 | #include <isl_dim_map.h> |
| 41 | #include <isl_local_space_private.h> |
| 42 | #include <isl_aff_private.h> |
| 43 | #include <isl_options_private.h> |
| 44 | #include <isl_morph.h> |
| 45 | #include <isl_val_private.h> |
| 46 | #include <isl/deprecated/map_int.h> |
| 47 | #include <isl/deprecated/set_int.h> |
| 48 | |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 49 | #include <bset_to_bmap.c> |
| 50 | #include <bset_from_bmap.c> |
| 51 | #include <set_to_map.c> |
| 52 | #include <set_from_map.c> |
| 53 | |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 54 | static unsigned n(__isl_keep isl_space *dim, enum isl_dim_type type) |
| 55 | { |
| 56 | switch (type) { |
| 57 | case isl_dim_param: return dim->nparam; |
| 58 | case isl_dim_in: return dim->n_in; |
| 59 | case isl_dim_out: return dim->n_out; |
| 60 | case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out; |
| 61 | default: return 0; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | static unsigned pos(__isl_keep isl_space *dim, enum isl_dim_type type) |
| 66 | { |
| 67 | switch (type) { |
| 68 | case isl_dim_param: return 1; |
| 69 | case isl_dim_in: return 1 + dim->nparam; |
| 70 | case isl_dim_out: return 1 + dim->nparam + dim->n_in; |
| 71 | default: return 0; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | unsigned isl_basic_map_dim(__isl_keep isl_basic_map *bmap, |
| 76 | enum isl_dim_type type) |
| 77 | { |
| 78 | if (!bmap) |
| 79 | return 0; |
| 80 | switch (type) { |
| 81 | case isl_dim_cst: return 1; |
| 82 | case isl_dim_param: |
| 83 | case isl_dim_in: |
| 84 | case isl_dim_out: return isl_space_dim(bmap->dim, type); |
| 85 | case isl_dim_div: return bmap->n_div; |
| 86 | case isl_dim_all: return isl_basic_map_total_dim(bmap); |
| 87 | default: return 0; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | unsigned isl_map_dim(__isl_keep isl_map *map, enum isl_dim_type type) |
| 92 | { |
| 93 | return map ? n(map->dim, type) : 0; |
| 94 | } |
| 95 | |
| 96 | unsigned isl_set_dim(__isl_keep isl_set *set, enum isl_dim_type type) |
| 97 | { |
| 98 | return set ? n(set->dim, type) : 0; |
| 99 | } |
| 100 | |
| 101 | unsigned isl_basic_map_offset(struct isl_basic_map *bmap, |
| 102 | enum isl_dim_type type) |
| 103 | { |
Michael Kruse | ffb3278 | 2016-08-17 15:24:45 +0000 | [diff] [blame] | 104 | isl_space *space; |
| 105 | |
| 106 | if (!bmap) |
| 107 | return 0; |
| 108 | |
| 109 | space = bmap->dim; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 110 | switch (type) { |
| 111 | case isl_dim_cst: return 0; |
| 112 | case isl_dim_param: return 1; |
Michael Kruse | ffb3278 | 2016-08-17 15:24:45 +0000 | [diff] [blame] | 113 | case isl_dim_in: return 1 + space->nparam; |
| 114 | case isl_dim_out: return 1 + space->nparam + space->n_in; |
| 115 | case isl_dim_div: return 1 + space->nparam + space->n_in + |
| 116 | space->n_out; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 117 | default: return 0; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | unsigned isl_basic_set_offset(struct isl_basic_set *bset, |
| 122 | enum isl_dim_type type) |
| 123 | { |
| 124 | return isl_basic_map_offset(bset, type); |
| 125 | } |
| 126 | |
| 127 | static unsigned map_offset(struct isl_map *map, enum isl_dim_type type) |
| 128 | { |
| 129 | return pos(map->dim, type); |
| 130 | } |
| 131 | |
| 132 | unsigned isl_basic_set_dim(__isl_keep isl_basic_set *bset, |
| 133 | enum isl_dim_type type) |
| 134 | { |
| 135 | return isl_basic_map_dim(bset, type); |
| 136 | } |
| 137 | |
| 138 | unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set *bset) |
| 139 | { |
| 140 | return isl_basic_set_dim(bset, isl_dim_set); |
| 141 | } |
| 142 | |
| 143 | unsigned isl_basic_set_n_param(__isl_keep isl_basic_set *bset) |
| 144 | { |
| 145 | return isl_basic_set_dim(bset, isl_dim_param); |
| 146 | } |
| 147 | |
| 148 | unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset) |
| 149 | { |
| 150 | if (!bset) |
| 151 | return 0; |
| 152 | return isl_space_dim(bset->dim, isl_dim_all) + bset->n_div; |
| 153 | } |
| 154 | |
| 155 | unsigned isl_set_n_dim(__isl_keep isl_set *set) |
| 156 | { |
| 157 | return isl_set_dim(set, isl_dim_set); |
| 158 | } |
| 159 | |
| 160 | unsigned isl_set_n_param(__isl_keep isl_set *set) |
| 161 | { |
| 162 | return isl_set_dim(set, isl_dim_param); |
| 163 | } |
| 164 | |
| 165 | unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap) |
| 166 | { |
| 167 | return bmap ? bmap->dim->n_in : 0; |
| 168 | } |
| 169 | |
| 170 | unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap) |
| 171 | { |
| 172 | return bmap ? bmap->dim->n_out : 0; |
| 173 | } |
| 174 | |
| 175 | unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap) |
| 176 | { |
| 177 | return bmap ? bmap->dim->nparam : 0; |
| 178 | } |
| 179 | |
| 180 | unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap) |
| 181 | { |
| 182 | return bmap ? bmap->n_div : 0; |
| 183 | } |
| 184 | |
| 185 | unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap) |
| 186 | { |
| 187 | return bmap ? isl_space_dim(bmap->dim, isl_dim_all) + bmap->n_div : 0; |
| 188 | } |
| 189 | |
| 190 | unsigned isl_map_n_in(const struct isl_map *map) |
| 191 | { |
| 192 | return map ? map->dim->n_in : 0; |
| 193 | } |
| 194 | |
| 195 | unsigned isl_map_n_out(const struct isl_map *map) |
| 196 | { |
| 197 | return map ? map->dim->n_out : 0; |
| 198 | } |
| 199 | |
| 200 | unsigned isl_map_n_param(const struct isl_map *map) |
| 201 | { |
| 202 | return map ? map->dim->nparam : 0; |
| 203 | } |
| 204 | |
| 205 | int isl_map_compatible_domain(struct isl_map *map, struct isl_set *set) |
| 206 | { |
| 207 | int m; |
| 208 | if (!map || !set) |
| 209 | return -1; |
| 210 | m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param); |
| 211 | if (m < 0 || !m) |
| 212 | return m; |
| 213 | return isl_space_tuple_is_equal(map->dim, isl_dim_in, |
| 214 | set->dim, isl_dim_set); |
| 215 | } |
| 216 | |
Tobias Grosser | 932ec01 | 2016-07-06 09:11:00 +0000 | [diff] [blame] | 217 | isl_bool isl_basic_map_compatible_domain(__isl_keep isl_basic_map *bmap, |
| 218 | __isl_keep isl_basic_set *bset) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 219 | { |
Tobias Grosser | 932ec01 | 2016-07-06 09:11:00 +0000 | [diff] [blame] | 220 | isl_bool m; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 221 | if (!bmap || !bset) |
Tobias Grosser | 932ec01 | 2016-07-06 09:11:00 +0000 | [diff] [blame] | 222 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 223 | m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param); |
| 224 | if (m < 0 || !m) |
| 225 | return m; |
| 226 | return isl_space_tuple_is_equal(bmap->dim, isl_dim_in, |
| 227 | bset->dim, isl_dim_set); |
| 228 | } |
| 229 | |
| 230 | int isl_map_compatible_range(__isl_keep isl_map *map, __isl_keep isl_set *set) |
| 231 | { |
| 232 | int m; |
| 233 | if (!map || !set) |
| 234 | return -1; |
| 235 | m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param); |
| 236 | if (m < 0 || !m) |
| 237 | return m; |
| 238 | return isl_space_tuple_is_equal(map->dim, isl_dim_out, |
| 239 | set->dim, isl_dim_set); |
| 240 | } |
| 241 | |
| 242 | int isl_basic_map_compatible_range(struct isl_basic_map *bmap, |
| 243 | struct isl_basic_set *bset) |
| 244 | { |
| 245 | int m; |
| 246 | if (!bmap || !bset) |
| 247 | return -1; |
| 248 | m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param); |
| 249 | if (m < 0 || !m) |
| 250 | return m; |
| 251 | return isl_space_tuple_is_equal(bmap->dim, isl_dim_out, |
| 252 | bset->dim, isl_dim_set); |
| 253 | } |
| 254 | |
| 255 | isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap) |
| 256 | { |
| 257 | return bmap ? bmap->ctx : NULL; |
| 258 | } |
| 259 | |
| 260 | isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset) |
| 261 | { |
| 262 | return bset ? bset->ctx : NULL; |
| 263 | } |
| 264 | |
| 265 | isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map) |
| 266 | { |
| 267 | return map ? map->ctx : NULL; |
| 268 | } |
| 269 | |
| 270 | isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set) |
| 271 | { |
| 272 | return set ? set->ctx : NULL; |
| 273 | } |
| 274 | |
| 275 | __isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap) |
| 276 | { |
| 277 | if (!bmap) |
| 278 | return NULL; |
| 279 | return isl_space_copy(bmap->dim); |
| 280 | } |
| 281 | |
| 282 | __isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset) |
| 283 | { |
| 284 | if (!bset) |
| 285 | return NULL; |
| 286 | return isl_space_copy(bset->dim); |
| 287 | } |
| 288 | |
| 289 | /* Extract the divs in "bmap" as a matrix. |
| 290 | */ |
| 291 | __isl_give isl_mat *isl_basic_map_get_divs(__isl_keep isl_basic_map *bmap) |
| 292 | { |
| 293 | int i; |
| 294 | isl_ctx *ctx; |
| 295 | isl_mat *div; |
| 296 | unsigned total; |
| 297 | unsigned cols; |
| 298 | |
| 299 | if (!bmap) |
| 300 | return NULL; |
| 301 | |
| 302 | ctx = isl_basic_map_get_ctx(bmap); |
| 303 | total = isl_space_dim(bmap->dim, isl_dim_all); |
| 304 | cols = 1 + 1 + total + bmap->n_div; |
| 305 | div = isl_mat_alloc(ctx, bmap->n_div, cols); |
| 306 | if (!div) |
| 307 | return NULL; |
| 308 | |
| 309 | for (i = 0; i < bmap->n_div; ++i) |
| 310 | isl_seq_cpy(div->row[i], bmap->div[i], cols); |
| 311 | |
| 312 | return div; |
| 313 | } |
| 314 | |
| 315 | /* Extract the divs in "bset" as a matrix. |
| 316 | */ |
| 317 | __isl_give isl_mat *isl_basic_set_get_divs(__isl_keep isl_basic_set *bset) |
| 318 | { |
| 319 | return isl_basic_map_get_divs(bset); |
| 320 | } |
| 321 | |
| 322 | __isl_give isl_local_space *isl_basic_map_get_local_space( |
| 323 | __isl_keep isl_basic_map *bmap) |
| 324 | { |
| 325 | isl_mat *div; |
| 326 | |
| 327 | if (!bmap) |
| 328 | return NULL; |
| 329 | |
| 330 | div = isl_basic_map_get_divs(bmap); |
| 331 | return isl_local_space_alloc_div(isl_space_copy(bmap->dim), div); |
| 332 | } |
| 333 | |
| 334 | __isl_give isl_local_space *isl_basic_set_get_local_space( |
| 335 | __isl_keep isl_basic_set *bset) |
| 336 | { |
| 337 | return isl_basic_map_get_local_space(bset); |
| 338 | } |
| 339 | |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 340 | /* For each known div d = floor(f/m), add the constraints |
| 341 | * |
| 342 | * f - m d >= 0 |
Tobias Grosser | 07b2095 | 2016-06-12 04:30:40 +0000 | [diff] [blame] | 343 | * -(f-(m-1)) + m d >= 0 |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 344 | * |
| 345 | * Do not finalize the result. |
| 346 | */ |
| 347 | static __isl_give isl_basic_map *add_known_div_constraints( |
| 348 | __isl_take isl_basic_map *bmap) |
| 349 | { |
| 350 | int i; |
| 351 | unsigned n_div; |
| 352 | |
| 353 | if (!bmap) |
| 354 | return NULL; |
| 355 | n_div = isl_basic_map_dim(bmap, isl_dim_div); |
| 356 | if (n_div == 0) |
| 357 | return bmap; |
| 358 | bmap = isl_basic_map_cow(bmap); |
| 359 | bmap = isl_basic_map_extend_constraints(bmap, 0, 2 * n_div); |
| 360 | if (!bmap) |
| 361 | return NULL; |
| 362 | for (i = 0; i < n_div; ++i) { |
| 363 | if (isl_int_is_zero(bmap->div[i][0])) |
| 364 | continue; |
| 365 | if (isl_basic_map_add_div_constraints(bmap, i) < 0) |
| 366 | return isl_basic_map_free(bmap); |
| 367 | } |
| 368 | |
| 369 | return bmap; |
| 370 | } |
| 371 | |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 372 | __isl_give isl_basic_map *isl_basic_map_from_local_space( |
| 373 | __isl_take isl_local_space *ls) |
| 374 | { |
| 375 | int i; |
| 376 | int n_div; |
| 377 | isl_basic_map *bmap; |
| 378 | |
| 379 | if (!ls) |
| 380 | return NULL; |
| 381 | |
| 382 | n_div = isl_local_space_dim(ls, isl_dim_div); |
| 383 | bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls), |
| 384 | n_div, 0, 2 * n_div); |
| 385 | |
| 386 | for (i = 0; i < n_div; ++i) |
| 387 | if (isl_basic_map_alloc_div(bmap) < 0) |
| 388 | goto error; |
| 389 | |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 390 | for (i = 0; i < n_div; ++i) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 391 | isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col); |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 392 | bmap = add_known_div_constraints(bmap); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 393 | |
| 394 | isl_local_space_free(ls); |
| 395 | return bmap; |
| 396 | error: |
| 397 | isl_local_space_free(ls); |
| 398 | isl_basic_map_free(bmap); |
| 399 | return NULL; |
| 400 | } |
| 401 | |
| 402 | __isl_give isl_basic_set *isl_basic_set_from_local_space( |
| 403 | __isl_take isl_local_space *ls) |
| 404 | { |
| 405 | return isl_basic_map_from_local_space(ls); |
| 406 | } |
| 407 | |
| 408 | __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map) |
| 409 | { |
| 410 | if (!map) |
| 411 | return NULL; |
| 412 | return isl_space_copy(map->dim); |
| 413 | } |
| 414 | |
| 415 | __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set) |
| 416 | { |
| 417 | if (!set) |
| 418 | return NULL; |
| 419 | return isl_space_copy(set->dim); |
| 420 | } |
| 421 | |
| 422 | __isl_give isl_basic_map *isl_basic_map_set_tuple_name( |
| 423 | __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s) |
| 424 | { |
| 425 | bmap = isl_basic_map_cow(bmap); |
| 426 | if (!bmap) |
| 427 | return NULL; |
| 428 | bmap->dim = isl_space_set_tuple_name(bmap->dim, type, s); |
| 429 | if (!bmap->dim) |
| 430 | goto error; |
| 431 | bmap = isl_basic_map_finalize(bmap); |
| 432 | return bmap; |
| 433 | error: |
| 434 | isl_basic_map_free(bmap); |
| 435 | return NULL; |
| 436 | } |
| 437 | |
| 438 | __isl_give isl_basic_set *isl_basic_set_set_tuple_name( |
| 439 | __isl_take isl_basic_set *bset, const char *s) |
| 440 | { |
| 441 | return isl_basic_map_set_tuple_name(bset, isl_dim_set, s); |
| 442 | } |
| 443 | |
| 444 | const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap, |
| 445 | enum isl_dim_type type) |
| 446 | { |
| 447 | return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL; |
| 448 | } |
| 449 | |
| 450 | __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map, |
| 451 | enum isl_dim_type type, const char *s) |
| 452 | { |
| 453 | int i; |
| 454 | |
| 455 | map = isl_map_cow(map); |
| 456 | if (!map) |
| 457 | return NULL; |
| 458 | |
| 459 | map->dim = isl_space_set_tuple_name(map->dim, type, s); |
| 460 | if (!map->dim) |
| 461 | goto error; |
| 462 | |
| 463 | for (i = 0; i < map->n; ++i) { |
| 464 | map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s); |
| 465 | if (!map->p[i]) |
| 466 | goto error; |
| 467 | } |
| 468 | |
| 469 | return map; |
| 470 | error: |
| 471 | isl_map_free(map); |
| 472 | return NULL; |
| 473 | } |
| 474 | |
| 475 | /* Replace the identifier of the tuple of type "type" by "id". |
| 476 | */ |
| 477 | __isl_give isl_basic_map *isl_basic_map_set_tuple_id( |
| 478 | __isl_take isl_basic_map *bmap, |
| 479 | enum isl_dim_type type, __isl_take isl_id *id) |
| 480 | { |
| 481 | bmap = isl_basic_map_cow(bmap); |
| 482 | if (!bmap) |
| 483 | goto error; |
| 484 | bmap->dim = isl_space_set_tuple_id(bmap->dim, type, id); |
| 485 | if (!bmap->dim) |
| 486 | return isl_basic_map_free(bmap); |
| 487 | bmap = isl_basic_map_finalize(bmap); |
| 488 | return bmap; |
| 489 | error: |
| 490 | isl_id_free(id); |
| 491 | return NULL; |
| 492 | } |
| 493 | |
| 494 | /* Replace the identifier of the tuple by "id". |
| 495 | */ |
| 496 | __isl_give isl_basic_set *isl_basic_set_set_tuple_id( |
| 497 | __isl_take isl_basic_set *bset, __isl_take isl_id *id) |
| 498 | { |
| 499 | return isl_basic_map_set_tuple_id(bset, isl_dim_set, id); |
| 500 | } |
| 501 | |
| 502 | /* Does the input or output tuple have a name? |
| 503 | */ |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 504 | isl_bool isl_map_has_tuple_name(__isl_keep isl_map *map, enum isl_dim_type type) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 505 | { |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 506 | return map ? isl_space_has_tuple_name(map->dim, type) : isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | const char *isl_map_get_tuple_name(__isl_keep isl_map *map, |
| 510 | enum isl_dim_type type) |
| 511 | { |
| 512 | return map ? isl_space_get_tuple_name(map->dim, type) : NULL; |
| 513 | } |
| 514 | |
| 515 | __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set, |
| 516 | const char *s) |
| 517 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 518 | return set_from_map(isl_map_set_tuple_name(set_to_map(set), |
| 519 | isl_dim_set, s)); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map, |
| 523 | enum isl_dim_type type, __isl_take isl_id *id) |
| 524 | { |
| 525 | map = isl_map_cow(map); |
| 526 | if (!map) |
| 527 | goto error; |
| 528 | |
| 529 | map->dim = isl_space_set_tuple_id(map->dim, type, id); |
| 530 | |
| 531 | return isl_map_reset_space(map, isl_space_copy(map->dim)); |
| 532 | error: |
| 533 | isl_id_free(id); |
| 534 | return NULL; |
| 535 | } |
| 536 | |
| 537 | __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set, |
| 538 | __isl_take isl_id *id) |
| 539 | { |
| 540 | return isl_map_set_tuple_id(set, isl_dim_set, id); |
| 541 | } |
| 542 | |
| 543 | __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map, |
| 544 | enum isl_dim_type type) |
| 545 | { |
| 546 | map = isl_map_cow(map); |
| 547 | if (!map) |
| 548 | return NULL; |
| 549 | |
| 550 | map->dim = isl_space_reset_tuple_id(map->dim, type); |
| 551 | |
| 552 | return isl_map_reset_space(map, isl_space_copy(map->dim)); |
| 553 | } |
| 554 | |
| 555 | __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set) |
| 556 | { |
| 557 | return isl_map_reset_tuple_id(set, isl_dim_set); |
| 558 | } |
| 559 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 560 | isl_bool isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 561 | { |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 562 | return map ? isl_space_has_tuple_id(map->dim, type) : isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map, |
| 566 | enum isl_dim_type type) |
| 567 | { |
| 568 | return map ? isl_space_get_tuple_id(map->dim, type) : NULL; |
| 569 | } |
| 570 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 571 | isl_bool isl_set_has_tuple_id(__isl_keep isl_set *set) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 572 | { |
| 573 | return isl_map_has_tuple_id(set, isl_dim_set); |
| 574 | } |
| 575 | |
| 576 | __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set) |
| 577 | { |
| 578 | return isl_map_get_tuple_id(set, isl_dim_set); |
| 579 | } |
| 580 | |
| 581 | /* Does the set tuple have a name? |
| 582 | */ |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 583 | isl_bool isl_set_has_tuple_name(__isl_keep isl_set *set) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 584 | { |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 585 | if (!set) |
| 586 | return isl_bool_error; |
| 587 | return isl_space_has_tuple_name(set->dim, isl_dim_set); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | |
| 591 | const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset) |
| 592 | { |
| 593 | return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL; |
| 594 | } |
| 595 | |
| 596 | const char *isl_set_get_tuple_name(__isl_keep isl_set *set) |
| 597 | { |
| 598 | return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL; |
| 599 | } |
| 600 | |
| 601 | const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap, |
| 602 | enum isl_dim_type type, unsigned pos) |
| 603 | { |
| 604 | return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL; |
| 605 | } |
| 606 | |
| 607 | const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset, |
| 608 | enum isl_dim_type type, unsigned pos) |
| 609 | { |
| 610 | return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL; |
| 611 | } |
| 612 | |
| 613 | /* Does the given dimension have a name? |
| 614 | */ |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 615 | isl_bool isl_map_has_dim_name(__isl_keep isl_map *map, |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 616 | enum isl_dim_type type, unsigned pos) |
| 617 | { |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 618 | if (!map) |
| 619 | return isl_bool_error; |
| 620 | return isl_space_has_dim_name(map->dim, type, pos); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | const char *isl_map_get_dim_name(__isl_keep isl_map *map, |
| 624 | enum isl_dim_type type, unsigned pos) |
| 625 | { |
| 626 | return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL; |
| 627 | } |
| 628 | |
| 629 | const char *isl_set_get_dim_name(__isl_keep isl_set *set, |
| 630 | enum isl_dim_type type, unsigned pos) |
| 631 | { |
| 632 | return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL; |
| 633 | } |
| 634 | |
| 635 | /* Does the given dimension have a name? |
| 636 | */ |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 637 | isl_bool isl_set_has_dim_name(__isl_keep isl_set *set, |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 638 | enum isl_dim_type type, unsigned pos) |
| 639 | { |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 640 | if (!set) |
| 641 | return isl_bool_error; |
| 642 | return isl_space_has_dim_name(set->dim, type, pos); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 643 | } |
| 644 | |
| 645 | __isl_give isl_basic_map *isl_basic_map_set_dim_name( |
| 646 | __isl_take isl_basic_map *bmap, |
| 647 | enum isl_dim_type type, unsigned pos, const char *s) |
| 648 | { |
| 649 | bmap = isl_basic_map_cow(bmap); |
| 650 | if (!bmap) |
| 651 | return NULL; |
| 652 | bmap->dim = isl_space_set_dim_name(bmap->dim, type, pos, s); |
| 653 | if (!bmap->dim) |
| 654 | goto error; |
| 655 | return isl_basic_map_finalize(bmap); |
| 656 | error: |
| 657 | isl_basic_map_free(bmap); |
| 658 | return NULL; |
| 659 | } |
| 660 | |
| 661 | __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map, |
| 662 | enum isl_dim_type type, unsigned pos, const char *s) |
| 663 | { |
| 664 | int i; |
| 665 | |
| 666 | map = isl_map_cow(map); |
| 667 | if (!map) |
| 668 | return NULL; |
| 669 | |
| 670 | map->dim = isl_space_set_dim_name(map->dim, type, pos, s); |
| 671 | if (!map->dim) |
| 672 | goto error; |
| 673 | |
| 674 | for (i = 0; i < map->n; ++i) { |
| 675 | map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s); |
| 676 | if (!map->p[i]) |
| 677 | goto error; |
| 678 | } |
| 679 | |
| 680 | return map; |
| 681 | error: |
| 682 | isl_map_free(map); |
| 683 | return NULL; |
| 684 | } |
| 685 | |
| 686 | __isl_give isl_basic_set *isl_basic_set_set_dim_name( |
| 687 | __isl_take isl_basic_set *bset, |
| 688 | enum isl_dim_type type, unsigned pos, const char *s) |
| 689 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 690 | return bset_from_bmap(isl_basic_map_set_dim_name(bset_to_bmap(bset), |
| 691 | type, pos, s)); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set, |
| 695 | enum isl_dim_type type, unsigned pos, const char *s) |
| 696 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 697 | return set_from_map(isl_map_set_dim_name(set_to_map(set), |
| 698 | type, pos, s)); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 699 | } |
| 700 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 701 | isl_bool isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap, |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 702 | enum isl_dim_type type, unsigned pos) |
| 703 | { |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 704 | if (!bmap) |
| 705 | return isl_bool_error; |
| 706 | return isl_space_has_dim_id(bmap->dim, type, pos); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | __isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_set *bset, |
| 710 | enum isl_dim_type type, unsigned pos) |
| 711 | { |
| 712 | return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL; |
| 713 | } |
| 714 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 715 | isl_bool isl_map_has_dim_id(__isl_keep isl_map *map, |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 716 | enum isl_dim_type type, unsigned pos) |
| 717 | { |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 718 | return map ? isl_space_has_dim_id(map->dim, type, pos) : isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map, |
| 722 | enum isl_dim_type type, unsigned pos) |
| 723 | { |
| 724 | return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL; |
| 725 | } |
| 726 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 727 | isl_bool isl_set_has_dim_id(__isl_keep isl_set *set, |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 728 | enum isl_dim_type type, unsigned pos) |
| 729 | { |
| 730 | return isl_map_has_dim_id(set, type, pos); |
| 731 | } |
| 732 | |
| 733 | __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set, |
| 734 | enum isl_dim_type type, unsigned pos) |
| 735 | { |
| 736 | return isl_map_get_dim_id(set, type, pos); |
| 737 | } |
| 738 | |
| 739 | __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map, |
| 740 | enum isl_dim_type type, unsigned pos, __isl_take isl_id *id) |
| 741 | { |
| 742 | map = isl_map_cow(map); |
| 743 | if (!map) |
| 744 | goto error; |
| 745 | |
| 746 | map->dim = isl_space_set_dim_id(map->dim, type, pos, id); |
| 747 | |
| 748 | return isl_map_reset_space(map, isl_space_copy(map->dim)); |
| 749 | error: |
| 750 | isl_id_free(id); |
| 751 | return NULL; |
| 752 | } |
| 753 | |
| 754 | __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set, |
| 755 | enum isl_dim_type type, unsigned pos, __isl_take isl_id *id) |
| 756 | { |
| 757 | return isl_map_set_dim_id(set, type, pos, id); |
| 758 | } |
| 759 | |
| 760 | int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type, |
| 761 | __isl_keep isl_id *id) |
| 762 | { |
| 763 | if (!map) |
| 764 | return -1; |
| 765 | return isl_space_find_dim_by_id(map->dim, type, id); |
| 766 | } |
| 767 | |
| 768 | int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type, |
| 769 | __isl_keep isl_id *id) |
| 770 | { |
| 771 | return isl_map_find_dim_by_id(set, type, id); |
| 772 | } |
| 773 | |
| 774 | /* Return the position of the dimension of the given type and name |
| 775 | * in "bmap". |
| 776 | * Return -1 if no such dimension can be found. |
| 777 | */ |
| 778 | int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map *bmap, |
| 779 | enum isl_dim_type type, const char *name) |
| 780 | { |
| 781 | if (!bmap) |
| 782 | return -1; |
| 783 | return isl_space_find_dim_by_name(bmap->dim, type, name); |
| 784 | } |
| 785 | |
| 786 | int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type, |
| 787 | const char *name) |
| 788 | { |
| 789 | if (!map) |
| 790 | return -1; |
| 791 | return isl_space_find_dim_by_name(map->dim, type, name); |
| 792 | } |
| 793 | |
| 794 | int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type, |
| 795 | const char *name) |
| 796 | { |
| 797 | return isl_map_find_dim_by_name(set, type, name); |
| 798 | } |
| 799 | |
| 800 | /* Reset the user pointer on all identifiers of parameters and tuples |
| 801 | * of the space of "map". |
| 802 | */ |
| 803 | __isl_give isl_map *isl_map_reset_user(__isl_take isl_map *map) |
| 804 | { |
| 805 | isl_space *space; |
| 806 | |
| 807 | space = isl_map_get_space(map); |
| 808 | space = isl_space_reset_user(space); |
| 809 | map = isl_map_reset_space(map, space); |
| 810 | |
| 811 | return map; |
| 812 | } |
| 813 | |
| 814 | /* Reset the user pointer on all identifiers of parameters and tuples |
| 815 | * of the space of "set". |
| 816 | */ |
| 817 | __isl_give isl_set *isl_set_reset_user(__isl_take isl_set *set) |
| 818 | { |
| 819 | return isl_map_reset_user(set); |
| 820 | } |
| 821 | |
| 822 | int isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap) |
| 823 | { |
| 824 | if (!bmap) |
| 825 | return -1; |
| 826 | return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL); |
| 827 | } |
| 828 | |
Tobias Grosser | 5652c9c | 2016-10-01 19:46:51 +0000 | [diff] [blame] | 829 | /* Has "map" been marked as a rational map? |
| 830 | * In particular, have all basic maps in "map" been marked this way? |
| 831 | * An empty map is not considered to be rational. |
| 832 | * Maps where only some of the basic maps are marked rational |
| 833 | * are not allowed. |
| 834 | */ |
| 835 | isl_bool isl_map_is_rational(__isl_keep isl_map *map) |
| 836 | { |
| 837 | int i; |
| 838 | isl_bool rational; |
| 839 | |
| 840 | if (!map) |
| 841 | return isl_bool_error; |
| 842 | if (map->n == 0) |
| 843 | return isl_bool_false; |
| 844 | rational = isl_basic_map_is_rational(map->p[0]); |
| 845 | if (rational < 0) |
| 846 | return rational; |
| 847 | for (i = 1; i < map->n; ++i) { |
| 848 | isl_bool rational_i; |
| 849 | |
| 850 | rational_i = isl_basic_map_is_rational(map->p[i]); |
| 851 | if (rational_i < 0) |
| 852 | return rational; |
| 853 | if (rational != rational_i) |
| 854 | isl_die(isl_map_get_ctx(map), isl_error_unsupported, |
| 855 | "mixed rational and integer basic maps " |
| 856 | "not supported", return isl_bool_error); |
| 857 | } |
| 858 | |
| 859 | return rational; |
| 860 | } |
| 861 | |
| 862 | /* Has "set" been marked as a rational set? |
| 863 | * In particular, have all basic set in "set" been marked this way? |
| 864 | * An empty set is not considered to be rational. |
| 865 | * Sets where only some of the basic sets are marked rational |
| 866 | * are not allowed. |
| 867 | */ |
| 868 | isl_bool isl_set_is_rational(__isl_keep isl_set *set) |
| 869 | { |
| 870 | return isl_map_is_rational(set); |
| 871 | } |
| 872 | |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 873 | int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset) |
| 874 | { |
| 875 | return isl_basic_map_is_rational(bset); |
| 876 | } |
| 877 | |
| 878 | /* Does "bmap" contain any rational points? |
| 879 | * |
| 880 | * If "bmap" has an equality for each dimension, equating the dimension |
| 881 | * to an integer constant, then it has no rational points, even if it |
| 882 | * is marked as rational. |
| 883 | */ |
| 884 | int isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap) |
| 885 | { |
| 886 | int has_rational = 1; |
| 887 | unsigned total; |
| 888 | |
| 889 | if (!bmap) |
| 890 | return -1; |
| 891 | if (isl_basic_map_plain_is_empty(bmap)) |
| 892 | return 0; |
| 893 | if (!isl_basic_map_is_rational(bmap)) |
| 894 | return 0; |
| 895 | bmap = isl_basic_map_copy(bmap); |
| 896 | bmap = isl_basic_map_implicit_equalities(bmap); |
| 897 | if (!bmap) |
| 898 | return -1; |
| 899 | total = isl_basic_map_total_dim(bmap); |
| 900 | if (bmap->n_eq == total) { |
| 901 | int i, j; |
| 902 | for (i = 0; i < bmap->n_eq; ++i) { |
| 903 | j = isl_seq_first_non_zero(bmap->eq[i] + 1, total); |
| 904 | if (j < 0) |
| 905 | break; |
| 906 | if (!isl_int_is_one(bmap->eq[i][1 + j]) && |
| 907 | !isl_int_is_negone(bmap->eq[i][1 + j])) |
| 908 | break; |
| 909 | j = isl_seq_first_non_zero(bmap->eq[i] + 1 + j + 1, |
| 910 | total - j - 1); |
| 911 | if (j >= 0) |
| 912 | break; |
| 913 | } |
| 914 | if (i == bmap->n_eq) |
| 915 | has_rational = 0; |
| 916 | } |
| 917 | isl_basic_map_free(bmap); |
| 918 | |
| 919 | return has_rational; |
| 920 | } |
| 921 | |
| 922 | /* Does "map" contain any rational points? |
| 923 | */ |
| 924 | int isl_map_has_rational(__isl_keep isl_map *map) |
| 925 | { |
| 926 | int i; |
| 927 | int has_rational; |
| 928 | |
| 929 | if (!map) |
| 930 | return -1; |
| 931 | for (i = 0; i < map->n; ++i) { |
| 932 | has_rational = isl_basic_map_has_rational(map->p[i]); |
| 933 | if (has_rational < 0) |
| 934 | return -1; |
| 935 | if (has_rational) |
| 936 | return 1; |
| 937 | } |
| 938 | return 0; |
| 939 | } |
| 940 | |
| 941 | /* Does "set" contain any rational points? |
| 942 | */ |
| 943 | int isl_set_has_rational(__isl_keep isl_set *set) |
| 944 | { |
| 945 | return isl_map_has_rational(set); |
| 946 | } |
| 947 | |
| 948 | /* Is this basic set a parameter domain? |
| 949 | */ |
| 950 | int isl_basic_set_is_params(__isl_keep isl_basic_set *bset) |
| 951 | { |
| 952 | if (!bset) |
| 953 | return -1; |
| 954 | return isl_space_is_params(bset->dim); |
| 955 | } |
| 956 | |
| 957 | /* Is this set a parameter domain? |
| 958 | */ |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 959 | isl_bool isl_set_is_params(__isl_keep isl_set *set) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 960 | { |
| 961 | if (!set) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 962 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 963 | return isl_space_is_params(set->dim); |
| 964 | } |
| 965 | |
| 966 | /* Is this map actually a parameter domain? |
| 967 | * Users should never call this function. Outside of isl, |
| 968 | * a map can never be a parameter domain. |
| 969 | */ |
| 970 | int isl_map_is_params(__isl_keep isl_map *map) |
| 971 | { |
| 972 | if (!map) |
| 973 | return -1; |
| 974 | return isl_space_is_params(map->dim); |
| 975 | } |
| 976 | |
| 977 | static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx, |
| 978 | struct isl_basic_map *bmap, unsigned extra, |
| 979 | unsigned n_eq, unsigned n_ineq) |
| 980 | { |
| 981 | int i; |
| 982 | size_t row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + extra; |
| 983 | |
| 984 | bmap->ctx = ctx; |
| 985 | isl_ctx_ref(ctx); |
| 986 | |
| 987 | bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size); |
| 988 | if (isl_blk_is_error(bmap->block)) |
| 989 | goto error; |
| 990 | |
| 991 | bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq); |
| 992 | if ((n_ineq + n_eq) && !bmap->ineq) |
| 993 | goto error; |
| 994 | |
| 995 | if (extra == 0) { |
| 996 | bmap->block2 = isl_blk_empty(); |
| 997 | bmap->div = NULL; |
| 998 | } else { |
| 999 | bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size)); |
| 1000 | if (isl_blk_is_error(bmap->block2)) |
| 1001 | goto error; |
| 1002 | |
| 1003 | bmap->div = isl_alloc_array(ctx, isl_int *, extra); |
| 1004 | if (!bmap->div) |
| 1005 | goto error; |
| 1006 | } |
| 1007 | |
| 1008 | for (i = 0; i < n_ineq + n_eq; ++i) |
| 1009 | bmap->ineq[i] = bmap->block.data + i * row_size; |
| 1010 | |
| 1011 | for (i = 0; i < extra; ++i) |
| 1012 | bmap->div[i] = bmap->block2.data + i * (1 + row_size); |
| 1013 | |
| 1014 | bmap->ref = 1; |
| 1015 | bmap->flags = 0; |
| 1016 | bmap->c_size = n_eq + n_ineq; |
| 1017 | bmap->eq = bmap->ineq + n_ineq; |
| 1018 | bmap->extra = extra; |
| 1019 | bmap->n_eq = 0; |
| 1020 | bmap->n_ineq = 0; |
| 1021 | bmap->n_div = 0; |
| 1022 | bmap->sample = NULL; |
| 1023 | |
| 1024 | return bmap; |
| 1025 | error: |
| 1026 | isl_basic_map_free(bmap); |
| 1027 | return NULL; |
| 1028 | } |
| 1029 | |
| 1030 | struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx, |
| 1031 | unsigned nparam, unsigned dim, unsigned extra, |
| 1032 | unsigned n_eq, unsigned n_ineq) |
| 1033 | { |
| 1034 | struct isl_basic_map *bmap; |
| 1035 | isl_space *space; |
| 1036 | |
| 1037 | space = isl_space_set_alloc(ctx, nparam, dim); |
| 1038 | if (!space) |
| 1039 | return NULL; |
| 1040 | |
| 1041 | bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq); |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 1042 | return bset_from_bmap(bmap); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 1043 | } |
| 1044 | |
| 1045 | struct isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim, |
| 1046 | unsigned extra, unsigned n_eq, unsigned n_ineq) |
| 1047 | { |
| 1048 | struct isl_basic_map *bmap; |
| 1049 | if (!dim) |
| 1050 | return NULL; |
| 1051 | isl_assert(dim->ctx, dim->n_in == 0, goto error); |
| 1052 | bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq); |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 1053 | return bset_from_bmap(bmap); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 1054 | error: |
| 1055 | isl_space_free(dim); |
| 1056 | return NULL; |
| 1057 | } |
| 1058 | |
| 1059 | struct isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *dim, |
| 1060 | unsigned extra, unsigned n_eq, unsigned n_ineq) |
| 1061 | { |
| 1062 | struct isl_basic_map *bmap; |
| 1063 | |
| 1064 | if (!dim) |
| 1065 | return NULL; |
| 1066 | bmap = isl_calloc_type(dim->ctx, struct isl_basic_map); |
| 1067 | if (!bmap) |
| 1068 | goto error; |
| 1069 | bmap->dim = dim; |
| 1070 | |
| 1071 | return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq); |
| 1072 | error: |
| 1073 | isl_space_free(dim); |
| 1074 | return NULL; |
| 1075 | } |
| 1076 | |
| 1077 | struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx, |
| 1078 | unsigned nparam, unsigned in, unsigned out, unsigned extra, |
| 1079 | unsigned n_eq, unsigned n_ineq) |
| 1080 | { |
| 1081 | struct isl_basic_map *bmap; |
| 1082 | isl_space *dim; |
| 1083 | |
| 1084 | dim = isl_space_alloc(ctx, nparam, in, out); |
| 1085 | if (!dim) |
| 1086 | return NULL; |
| 1087 | |
| 1088 | bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq); |
| 1089 | return bmap; |
| 1090 | } |
| 1091 | |
| 1092 | static void dup_constraints( |
| 1093 | struct isl_basic_map *dst, struct isl_basic_map *src) |
| 1094 | { |
| 1095 | int i; |
| 1096 | unsigned total = isl_basic_map_total_dim(src); |
| 1097 | |
| 1098 | for (i = 0; i < src->n_eq; ++i) { |
| 1099 | int j = isl_basic_map_alloc_equality(dst); |
| 1100 | isl_seq_cpy(dst->eq[j], src->eq[i], 1+total); |
| 1101 | } |
| 1102 | |
| 1103 | for (i = 0; i < src->n_ineq; ++i) { |
| 1104 | int j = isl_basic_map_alloc_inequality(dst); |
| 1105 | isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total); |
| 1106 | } |
| 1107 | |
| 1108 | for (i = 0; i < src->n_div; ++i) { |
| 1109 | int j = isl_basic_map_alloc_div(dst); |
| 1110 | isl_seq_cpy(dst->div[j], src->div[i], 1+1+total); |
| 1111 | } |
| 1112 | ISL_F_SET(dst, ISL_BASIC_SET_FINAL); |
| 1113 | } |
| 1114 | |
| 1115 | struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap) |
| 1116 | { |
| 1117 | struct isl_basic_map *dup; |
| 1118 | |
| 1119 | if (!bmap) |
| 1120 | return NULL; |
| 1121 | dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim), |
| 1122 | bmap->n_div, bmap->n_eq, bmap->n_ineq); |
| 1123 | if (!dup) |
| 1124 | return NULL; |
| 1125 | dup_constraints(dup, bmap); |
| 1126 | dup->flags = bmap->flags; |
| 1127 | dup->sample = isl_vec_copy(bmap->sample); |
| 1128 | return dup; |
| 1129 | } |
| 1130 | |
| 1131 | struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset) |
| 1132 | { |
| 1133 | struct isl_basic_map *dup; |
| 1134 | |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 1135 | dup = isl_basic_map_dup(bset_to_bmap(bset)); |
| 1136 | return bset_from_bmap(dup); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 1137 | } |
| 1138 | |
| 1139 | struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset) |
| 1140 | { |
| 1141 | if (!bset) |
| 1142 | return NULL; |
| 1143 | |
| 1144 | if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) { |
| 1145 | bset->ref++; |
| 1146 | return bset; |
| 1147 | } |
| 1148 | return isl_basic_set_dup(bset); |
| 1149 | } |
| 1150 | |
| 1151 | struct isl_set *isl_set_copy(struct isl_set *set) |
| 1152 | { |
| 1153 | if (!set) |
| 1154 | return NULL; |
| 1155 | |
| 1156 | set->ref++; |
| 1157 | return set; |
| 1158 | } |
| 1159 | |
| 1160 | struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap) |
| 1161 | { |
| 1162 | if (!bmap) |
| 1163 | return NULL; |
| 1164 | |
| 1165 | if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) { |
| 1166 | bmap->ref++; |
| 1167 | return bmap; |
| 1168 | } |
| 1169 | bmap = isl_basic_map_dup(bmap); |
| 1170 | if (bmap) |
| 1171 | ISL_F_SET(bmap, ISL_BASIC_SET_FINAL); |
| 1172 | return bmap; |
| 1173 | } |
| 1174 | |
| 1175 | struct isl_map *isl_map_copy(struct isl_map *map) |
| 1176 | { |
| 1177 | if (!map) |
| 1178 | return NULL; |
| 1179 | |
| 1180 | map->ref++; |
| 1181 | return map; |
| 1182 | } |
| 1183 | |
| 1184 | __isl_null isl_basic_map *isl_basic_map_free(__isl_take isl_basic_map *bmap) |
| 1185 | { |
| 1186 | if (!bmap) |
| 1187 | return NULL; |
| 1188 | |
| 1189 | if (--bmap->ref > 0) |
| 1190 | return NULL; |
| 1191 | |
| 1192 | isl_ctx_deref(bmap->ctx); |
| 1193 | free(bmap->div); |
| 1194 | isl_blk_free(bmap->ctx, bmap->block2); |
| 1195 | free(bmap->ineq); |
| 1196 | isl_blk_free(bmap->ctx, bmap->block); |
| 1197 | isl_vec_free(bmap->sample); |
| 1198 | isl_space_free(bmap->dim); |
| 1199 | free(bmap); |
| 1200 | |
| 1201 | return NULL; |
| 1202 | } |
| 1203 | |
| 1204 | __isl_null isl_basic_set *isl_basic_set_free(__isl_take isl_basic_set *bset) |
| 1205 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 1206 | return isl_basic_map_free(bset_to_bmap(bset)); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 1207 | } |
| 1208 | |
| 1209 | static int room_for_con(struct isl_basic_map *bmap, unsigned n) |
| 1210 | { |
| 1211 | return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size; |
| 1212 | } |
| 1213 | |
| 1214 | __isl_give isl_map *isl_map_align_params_map_map_and( |
| 1215 | __isl_take isl_map *map1, __isl_take isl_map *map2, |
| 1216 | __isl_give isl_map *(*fn)(__isl_take isl_map *map1, |
| 1217 | __isl_take isl_map *map2)) |
| 1218 | { |
| 1219 | if (!map1 || !map2) |
| 1220 | goto error; |
| 1221 | if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param)) |
| 1222 | return fn(map1, map2); |
| 1223 | if (!isl_space_has_named_params(map1->dim) || |
| 1224 | !isl_space_has_named_params(map2->dim)) |
| 1225 | isl_die(map1->ctx, isl_error_invalid, |
| 1226 | "unaligned unnamed parameters", goto error); |
| 1227 | map1 = isl_map_align_params(map1, isl_map_get_space(map2)); |
| 1228 | map2 = isl_map_align_params(map2, isl_map_get_space(map1)); |
| 1229 | return fn(map1, map2); |
| 1230 | error: |
| 1231 | isl_map_free(map1); |
| 1232 | isl_map_free(map2); |
| 1233 | return NULL; |
| 1234 | } |
| 1235 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 1236 | isl_bool isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1, |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 1237 | __isl_keep isl_map *map2, |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 1238 | isl_bool (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2)) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 1239 | { |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 1240 | isl_bool r; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 1241 | |
| 1242 | if (!map1 || !map2) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 1243 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 1244 | if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param)) |
| 1245 | return fn(map1, map2); |
| 1246 | if (!isl_space_has_named_params(map1->dim) || |
| 1247 | !isl_space_has_named_params(map2->dim)) |
| 1248 | isl_die(map1->ctx, isl_error_invalid, |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 1249 | "unaligned unnamed parameters", return isl_bool_error); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 1250 | map1 = isl_map_copy(map1); |
| 1251 | map2 = isl_map_copy(map2); |
| 1252 | map1 = isl_map_align_params(map1, isl_map_get_space(map2)); |
| 1253 | map2 = isl_map_align_params(map2, isl_map_get_space(map1)); |
| 1254 | r = fn(map1, map2); |
| 1255 | isl_map_free(map1); |
| 1256 | isl_map_free(map2); |
| 1257 | return r; |
| 1258 | } |
| 1259 | |
| 1260 | int isl_basic_map_alloc_equality(struct isl_basic_map *bmap) |
| 1261 | { |
| 1262 | struct isl_ctx *ctx; |
| 1263 | if (!bmap) |
| 1264 | return -1; |
| 1265 | ctx = bmap->ctx; |
| 1266 | isl_assert(ctx, room_for_con(bmap, 1), return -1); |
| 1267 | isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size, |
| 1268 | return -1); |
| 1269 | ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED); |
| 1270 | ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT); |
| 1271 | ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT); |
| 1272 | ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES); |
| 1273 | ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS); |
| 1274 | if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) { |
| 1275 | isl_int *t; |
| 1276 | int j = isl_basic_map_alloc_inequality(bmap); |
| 1277 | if (j < 0) |
| 1278 | return -1; |
| 1279 | t = bmap->ineq[j]; |
| 1280 | bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1]; |
| 1281 | bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1]; |
| 1282 | bmap->eq[-1] = t; |
| 1283 | bmap->n_eq++; |
| 1284 | bmap->n_ineq--; |
| 1285 | bmap->eq--; |
| 1286 | return 0; |
| 1287 | } |
| 1288 | isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap), |
| 1289 | bmap->extra - bmap->n_div); |
| 1290 | return bmap->n_eq++; |
| 1291 | } |
| 1292 | |
| 1293 | int isl_basic_set_alloc_equality(struct isl_basic_set *bset) |
| 1294 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 1295 | return isl_basic_map_alloc_equality(bset_to_bmap(bset)); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 1296 | } |
| 1297 | |
| 1298 | int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n) |
| 1299 | { |
| 1300 | if (!bmap) |
| 1301 | return -1; |
| 1302 | isl_assert(bmap->ctx, n <= bmap->n_eq, return -1); |
| 1303 | bmap->n_eq -= n; |
| 1304 | return 0; |
| 1305 | } |
| 1306 | |
| 1307 | int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n) |
| 1308 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 1309 | return isl_basic_map_free_equality(bset_to_bmap(bset), n); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 1310 | } |
| 1311 | |
| 1312 | int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos) |
| 1313 | { |
| 1314 | isl_int *t; |
| 1315 | if (!bmap) |
| 1316 | return -1; |
| 1317 | isl_assert(bmap->ctx, pos < bmap->n_eq, return -1); |
| 1318 | |
| 1319 | if (pos != bmap->n_eq - 1) { |
| 1320 | t = bmap->eq[pos]; |
| 1321 | bmap->eq[pos] = bmap->eq[bmap->n_eq - 1]; |
| 1322 | bmap->eq[bmap->n_eq - 1] = t; |
| 1323 | } |
| 1324 | bmap->n_eq--; |
| 1325 | return 0; |
| 1326 | } |
| 1327 | |
| 1328 | int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos) |
| 1329 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 1330 | return isl_basic_map_drop_equality(bset_to_bmap(bset), pos); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 1331 | } |
| 1332 | |
| 1333 | /* Turn inequality "pos" of "bmap" into an equality. |
| 1334 | * |
| 1335 | * In particular, we move the inequality in front of the equalities |
| 1336 | * and move the last inequality in the position of the moved inequality. |
| 1337 | * Note that isl_tab_make_equalities_explicit depends on this particular |
| 1338 | * change in the ordering of the constraints. |
| 1339 | */ |
| 1340 | void isl_basic_map_inequality_to_equality( |
| 1341 | struct isl_basic_map *bmap, unsigned pos) |
| 1342 | { |
| 1343 | isl_int *t; |
| 1344 | |
| 1345 | t = bmap->ineq[pos]; |
| 1346 | bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1]; |
| 1347 | bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1]; |
| 1348 | bmap->eq[-1] = t; |
| 1349 | bmap->n_eq++; |
| 1350 | bmap->n_ineq--; |
| 1351 | bmap->eq--; |
| 1352 | ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT); |
| 1353 | ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED); |
| 1354 | ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS); |
| 1355 | ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES); |
| 1356 | } |
| 1357 | |
| 1358 | static int room_for_ineq(struct isl_basic_map *bmap, unsigned n) |
| 1359 | { |
| 1360 | return bmap->n_ineq + n <= bmap->eq - bmap->ineq; |
| 1361 | } |
| 1362 | |
| 1363 | int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap) |
| 1364 | { |
| 1365 | struct isl_ctx *ctx; |
| 1366 | if (!bmap) |
| 1367 | return -1; |
| 1368 | ctx = bmap->ctx; |
| 1369 | isl_assert(ctx, room_for_ineq(bmap, 1), return -1); |
| 1370 | ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT); |
| 1371 | ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT); |
| 1372 | ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED); |
| 1373 | ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES); |
| 1374 | isl_seq_clr(bmap->ineq[bmap->n_ineq] + |
| 1375 | 1 + isl_basic_map_total_dim(bmap), |
| 1376 | bmap->extra - bmap->n_div); |
| 1377 | return bmap->n_ineq++; |
| 1378 | } |
| 1379 | |
| 1380 | int isl_basic_set_alloc_inequality(struct isl_basic_set *bset) |
| 1381 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 1382 | return isl_basic_map_alloc_inequality(bset_to_bmap(bset)); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 1383 | } |
| 1384 | |
| 1385 | int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n) |
| 1386 | { |
| 1387 | if (!bmap) |
| 1388 | return -1; |
| 1389 | isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1); |
| 1390 | bmap->n_ineq -= n; |
| 1391 | return 0; |
| 1392 | } |
| 1393 | |
| 1394 | int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n) |
| 1395 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 1396 | return isl_basic_map_free_inequality(bset_to_bmap(bset), n); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 1397 | } |
| 1398 | |
| 1399 | int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos) |
| 1400 | { |
| 1401 | isl_int *t; |
| 1402 | if (!bmap) |
| 1403 | return -1; |
| 1404 | isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1); |
| 1405 | |
| 1406 | if (pos != bmap->n_ineq - 1) { |
| 1407 | t = bmap->ineq[pos]; |
| 1408 | bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1]; |
| 1409 | bmap->ineq[bmap->n_ineq - 1] = t; |
| 1410 | ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED); |
| 1411 | } |
| 1412 | bmap->n_ineq--; |
| 1413 | return 0; |
| 1414 | } |
| 1415 | |
| 1416 | int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos) |
| 1417 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 1418 | return isl_basic_map_drop_inequality(bset_to_bmap(bset), pos); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 1419 | } |
| 1420 | |
| 1421 | __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap, |
| 1422 | isl_int *eq) |
| 1423 | { |
| 1424 | int k; |
| 1425 | |
| 1426 | bmap = isl_basic_map_extend_constraints(bmap, 1, 0); |
| 1427 | if (!bmap) |
| 1428 | return NULL; |
| 1429 | k = isl_basic_map_alloc_equality(bmap); |
| 1430 | if (k < 0) |
| 1431 | goto error; |
| 1432 | isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap)); |
| 1433 | return bmap; |
| 1434 | error: |
| 1435 | isl_basic_map_free(bmap); |
| 1436 | return NULL; |
| 1437 | } |
| 1438 | |
| 1439 | __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset, |
| 1440 | isl_int *eq) |
| 1441 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 1442 | return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset), eq)); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 1443 | } |
| 1444 | |
| 1445 | __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap, |
| 1446 | isl_int *ineq) |
| 1447 | { |
| 1448 | int k; |
| 1449 | |
| 1450 | bmap = isl_basic_map_extend_constraints(bmap, 0, 1); |
| 1451 | if (!bmap) |
| 1452 | return NULL; |
| 1453 | k = isl_basic_map_alloc_inequality(bmap); |
| 1454 | if (k < 0) |
| 1455 | goto error; |
| 1456 | isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap)); |
| 1457 | return bmap; |
| 1458 | error: |
| 1459 | isl_basic_map_free(bmap); |
| 1460 | return NULL; |
| 1461 | } |
| 1462 | |
| 1463 | __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset, |
| 1464 | isl_int *ineq) |
| 1465 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 1466 | return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset), ineq)); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 1467 | } |
| 1468 | |
| 1469 | int isl_basic_map_alloc_div(struct isl_basic_map *bmap) |
| 1470 | { |
| 1471 | if (!bmap) |
| 1472 | return -1; |
| 1473 | isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1); |
| 1474 | isl_seq_clr(bmap->div[bmap->n_div] + |
| 1475 | 1 + 1 + isl_basic_map_total_dim(bmap), |
| 1476 | bmap->extra - bmap->n_div); |
| 1477 | ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS); |
| 1478 | return bmap->n_div++; |
| 1479 | } |
| 1480 | |
| 1481 | int isl_basic_set_alloc_div(struct isl_basic_set *bset) |
| 1482 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 1483 | return isl_basic_map_alloc_div(bset_to_bmap(bset)); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 1484 | } |
| 1485 | |
Tobias Grosser | 9ec4f95 | 2016-07-20 16:53:07 +0000 | [diff] [blame] | 1486 | /* Insert an extra integer division, prescribed by "div", to "bmap" |
| 1487 | * at (integer division) position "pos". |
| 1488 | * |
| 1489 | * The integer division is first added at the end and then moved |
| 1490 | * into the right position. |
| 1491 | */ |
| 1492 | __isl_give isl_basic_map *isl_basic_map_insert_div( |
| 1493 | __isl_take isl_basic_map *bmap, int pos, __isl_keep isl_vec *div) |
| 1494 | { |
| 1495 | int i, k, n_div; |
| 1496 | |
| 1497 | bmap = isl_basic_map_cow(bmap); |
| 1498 | if (!bmap || !div) |
| 1499 | return isl_basic_map_free(bmap); |
| 1500 | |
| 1501 | if (div->size != 1 + 1 + isl_basic_map_dim(bmap, isl_dim_all)) |
| 1502 | isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid, |
| 1503 | "unexpected size", return isl_basic_map_free(bmap)); |
| 1504 | n_div = isl_basic_map_dim(bmap, isl_dim_div); |
| 1505 | if (pos < 0 || pos > n_div) |
| 1506 | isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid, |
| 1507 | "invalid position", return isl_basic_map_free(bmap)); |
| 1508 | |
| 1509 | bmap = isl_basic_map_extend_space(bmap, |
| 1510 | isl_basic_map_get_space(bmap), 1, 0, 2); |
| 1511 | k = isl_basic_map_alloc_div(bmap); |
| 1512 | if (k < 0) |
| 1513 | return isl_basic_map_free(bmap); |
| 1514 | isl_seq_cpy(bmap->div[k], div->el, div->size); |
| 1515 | isl_int_set_si(bmap->div[k][div->size], 0); |
| 1516 | |
| 1517 | for (i = k; i > pos; --i) |
| 1518 | isl_basic_map_swap_div(bmap, i, i - 1); |
| 1519 | |
| 1520 | return bmap; |
| 1521 | } |
| 1522 | |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 1523 | int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n) |
| 1524 | { |
| 1525 | if (!bmap) |
| 1526 | return -1; |
| 1527 | isl_assert(bmap->ctx, n <= bmap->n_div, return -1); |
| 1528 | bmap->n_div -= n; |
| 1529 | return 0; |
| 1530 | } |
| 1531 | |
| 1532 | int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n) |
| 1533 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 1534 | return isl_basic_map_free_div(bset_to_bmap(bset), n); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 1535 | } |
| 1536 | |
| 1537 | /* Copy constraint from src to dst, putting the vars of src at offset |
| 1538 | * dim_off in dst and the divs of src at offset div_off in dst. |
| 1539 | * If both sets are actually map, then dim_off applies to the input |
| 1540 | * variables. |
| 1541 | */ |
| 1542 | static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst, |
| 1543 | struct isl_basic_map *src_map, isl_int *src, |
| 1544 | unsigned in_off, unsigned out_off, unsigned div_off) |
| 1545 | { |
| 1546 | unsigned src_nparam = isl_basic_map_n_param(src_map); |
| 1547 | unsigned dst_nparam = isl_basic_map_n_param(dst_map); |
| 1548 | unsigned src_in = isl_basic_map_n_in(src_map); |
| 1549 | unsigned dst_in = isl_basic_map_n_in(dst_map); |
| 1550 | unsigned src_out = isl_basic_map_n_out(src_map); |
| 1551 | unsigned dst_out = isl_basic_map_n_out(dst_map); |
| 1552 | isl_int_set(dst[0], src[0]); |
| 1553 | isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam)); |
| 1554 | if (dst_nparam > src_nparam) |
| 1555 | isl_seq_clr(dst+1+src_nparam, |
| 1556 | dst_nparam - src_nparam); |
| 1557 | isl_seq_clr(dst+1+dst_nparam, in_off); |
| 1558 | isl_seq_cpy(dst+1+dst_nparam+in_off, |
| 1559 | src+1+src_nparam, |
| 1560 | isl_min(dst_in-in_off, src_in)); |
| 1561 | if (dst_in-in_off > src_in) |
| 1562 | isl_seq_clr(dst+1+dst_nparam+in_off+src_in, |
| 1563 | dst_in - in_off - src_in); |
| 1564 | isl_seq_clr(dst+1+dst_nparam+dst_in, out_off); |
| 1565 | isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off, |
| 1566 | src+1+src_nparam+src_in, |
| 1567 | isl_min(dst_out-out_off, src_out)); |
| 1568 | if (dst_out-out_off > src_out) |
| 1569 | isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out, |
| 1570 | dst_out - out_off - src_out); |
| 1571 | isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off); |
| 1572 | isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off, |
| 1573 | src+1+src_nparam+src_in+src_out, |
| 1574 | isl_min(dst_map->extra-div_off, src_map->n_div)); |
| 1575 | if (dst_map->n_div-div_off > src_map->n_div) |
| 1576 | isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+ |
| 1577 | div_off+src_map->n_div, |
| 1578 | dst_map->n_div - div_off - src_map->n_div); |
| 1579 | } |
| 1580 | |
| 1581 | static void copy_div(struct isl_basic_map *dst_map, isl_int *dst, |
| 1582 | struct isl_basic_map *src_map, isl_int *src, |
| 1583 | unsigned in_off, unsigned out_off, unsigned div_off) |
| 1584 | { |
| 1585 | isl_int_set(dst[0], src[0]); |
| 1586 | copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off); |
| 1587 | } |
| 1588 | |
| 1589 | static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1, |
| 1590 | struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos) |
| 1591 | { |
| 1592 | int i; |
| 1593 | unsigned div_off; |
| 1594 | |
| 1595 | if (!bmap1 || !bmap2) |
| 1596 | goto error; |
| 1597 | |
| 1598 | div_off = bmap1->n_div; |
| 1599 | |
| 1600 | for (i = 0; i < bmap2->n_eq; ++i) { |
| 1601 | int i1 = isl_basic_map_alloc_equality(bmap1); |
| 1602 | if (i1 < 0) |
| 1603 | goto error; |
| 1604 | copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i], |
| 1605 | i_pos, o_pos, div_off); |
| 1606 | } |
| 1607 | |
| 1608 | for (i = 0; i < bmap2->n_ineq; ++i) { |
| 1609 | int i1 = isl_basic_map_alloc_inequality(bmap1); |
| 1610 | if (i1 < 0) |
| 1611 | goto error; |
| 1612 | copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i], |
| 1613 | i_pos, o_pos, div_off); |
| 1614 | } |
| 1615 | |
| 1616 | for (i = 0; i < bmap2->n_div; ++i) { |
| 1617 | int i1 = isl_basic_map_alloc_div(bmap1); |
| 1618 | if (i1 < 0) |
| 1619 | goto error; |
| 1620 | copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i], |
| 1621 | i_pos, o_pos, div_off); |
| 1622 | } |
| 1623 | |
| 1624 | isl_basic_map_free(bmap2); |
| 1625 | |
| 1626 | return bmap1; |
| 1627 | |
| 1628 | error: |
| 1629 | isl_basic_map_free(bmap1); |
| 1630 | isl_basic_map_free(bmap2); |
| 1631 | return NULL; |
| 1632 | } |
| 1633 | |
| 1634 | struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1, |
| 1635 | struct isl_basic_set *bset2, unsigned pos) |
| 1636 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 1637 | return bset_from_bmap(add_constraints(bset_to_bmap(bset1), |
| 1638 | bset_to_bmap(bset2), 0, pos)); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 1639 | } |
| 1640 | |
| 1641 | struct isl_basic_map *isl_basic_map_extend_space(struct isl_basic_map *base, |
| 1642 | __isl_take isl_space *dim, unsigned extra, |
| 1643 | unsigned n_eq, unsigned n_ineq) |
| 1644 | { |
| 1645 | struct isl_basic_map *ext; |
| 1646 | unsigned flags; |
| 1647 | int dims_ok; |
| 1648 | |
| 1649 | if (!dim) |
| 1650 | goto error; |
| 1651 | |
| 1652 | if (!base) |
| 1653 | goto error; |
| 1654 | |
| 1655 | dims_ok = isl_space_is_equal(base->dim, dim) && |
| 1656 | base->extra >= base->n_div + extra; |
| 1657 | |
| 1658 | if (dims_ok && room_for_con(base, n_eq + n_ineq) && |
| 1659 | room_for_ineq(base, n_ineq)) { |
| 1660 | isl_space_free(dim); |
| 1661 | return base; |
| 1662 | } |
| 1663 | |
| 1664 | isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error); |
| 1665 | isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error); |
| 1666 | isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error); |
| 1667 | extra += base->extra; |
| 1668 | n_eq += base->n_eq; |
| 1669 | n_ineq += base->n_ineq; |
| 1670 | |
| 1671 | ext = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq); |
| 1672 | dim = NULL; |
| 1673 | if (!ext) |
| 1674 | goto error; |
| 1675 | |
| 1676 | if (dims_ok) |
| 1677 | ext->sample = isl_vec_copy(base->sample); |
| 1678 | flags = base->flags; |
| 1679 | ext = add_constraints(ext, base, 0, 0); |
| 1680 | if (ext) { |
| 1681 | ext->flags = flags; |
| 1682 | ISL_F_CLR(ext, ISL_BASIC_SET_FINAL); |
| 1683 | } |
| 1684 | |
| 1685 | return ext; |
| 1686 | |
| 1687 | error: |
| 1688 | isl_space_free(dim); |
| 1689 | isl_basic_map_free(base); |
| 1690 | return NULL; |
| 1691 | } |
| 1692 | |
| 1693 | struct isl_basic_set *isl_basic_set_extend_space(struct isl_basic_set *base, |
| 1694 | __isl_take isl_space *dim, unsigned extra, |
| 1695 | unsigned n_eq, unsigned n_ineq) |
| 1696 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 1697 | return bset_from_bmap(isl_basic_map_extend_space(bset_to_bmap(base), |
| 1698 | dim, extra, n_eq, n_ineq)); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 1699 | } |
| 1700 | |
| 1701 | struct isl_basic_map *isl_basic_map_extend_constraints( |
| 1702 | struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq) |
| 1703 | { |
| 1704 | if (!base) |
| 1705 | return NULL; |
| 1706 | return isl_basic_map_extend_space(base, isl_space_copy(base->dim), |
| 1707 | 0, n_eq, n_ineq); |
| 1708 | } |
| 1709 | |
| 1710 | struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base, |
| 1711 | unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra, |
| 1712 | unsigned n_eq, unsigned n_ineq) |
| 1713 | { |
| 1714 | struct isl_basic_map *bmap; |
| 1715 | isl_space *dim; |
| 1716 | |
| 1717 | if (!base) |
| 1718 | return NULL; |
| 1719 | dim = isl_space_alloc(base->ctx, nparam, n_in, n_out); |
| 1720 | if (!dim) |
| 1721 | goto error; |
| 1722 | |
| 1723 | bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq); |
| 1724 | return bmap; |
| 1725 | error: |
| 1726 | isl_basic_map_free(base); |
| 1727 | return NULL; |
| 1728 | } |
| 1729 | |
| 1730 | struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base, |
| 1731 | unsigned nparam, unsigned dim, unsigned extra, |
| 1732 | unsigned n_eq, unsigned n_ineq) |
| 1733 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 1734 | return bset_from_bmap(isl_basic_map_extend(bset_to_bmap(base), |
| 1735 | nparam, 0, dim, extra, n_eq, n_ineq)); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 1736 | } |
| 1737 | |
| 1738 | struct isl_basic_set *isl_basic_set_extend_constraints( |
| 1739 | struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq) |
| 1740 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 1741 | isl_basic_map *bmap = bset_to_bmap(base); |
| 1742 | bmap = isl_basic_map_extend_constraints(bmap, n_eq, n_ineq); |
| 1743 | return bset_from_bmap(bmap); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 1744 | } |
| 1745 | |
| 1746 | struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset) |
| 1747 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 1748 | return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset))); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 1749 | } |
| 1750 | |
| 1751 | struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap) |
| 1752 | { |
| 1753 | if (!bmap) |
| 1754 | return NULL; |
| 1755 | |
| 1756 | if (bmap->ref > 1) { |
| 1757 | bmap->ref--; |
| 1758 | bmap = isl_basic_map_dup(bmap); |
| 1759 | } |
Tobias Grosser | 1fa7b97 | 2015-02-16 19:33:40 +0000 | [diff] [blame] | 1760 | if (bmap) { |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 1761 | ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL); |
Tobias Grosser | 1fa7b97 | 2015-02-16 19:33:40 +0000 | [diff] [blame] | 1762 | ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS); |
| 1763 | } |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 1764 | return bmap; |
| 1765 | } |
| 1766 | |
Tobias Grosser | 07b2095 | 2016-06-12 04:30:40 +0000 | [diff] [blame] | 1767 | /* Clear all cached information in "map", either because it is about |
| 1768 | * to be modified or because it is being freed. |
| 1769 | * Always return the same pointer that is passed in. |
| 1770 | * This is needed for the use in isl_map_free. |
| 1771 | */ |
| 1772 | static __isl_give isl_map *clear_caches(__isl_take isl_map *map) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 1773 | { |
Tobias Grosser | 07b2095 | 2016-06-12 04:30:40 +0000 | [diff] [blame] | 1774 | isl_basic_map_free(map->cached_simple_hull[0]); |
| 1775 | isl_basic_map_free(map->cached_simple_hull[1]); |
| 1776 | map->cached_simple_hull[0] = NULL; |
| 1777 | map->cached_simple_hull[1] = NULL; |
| 1778 | return map; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 1779 | } |
| 1780 | |
Tobias Grosser | 07b2095 | 2016-06-12 04:30:40 +0000 | [diff] [blame] | 1781 | struct isl_set *isl_set_cow(struct isl_set *set) |
| 1782 | { |
| 1783 | return isl_map_cow(set); |
| 1784 | } |
| 1785 | |
| 1786 | /* Return an isl_map that is equal to "map" and that has only |
| 1787 | * a single reference. |
| 1788 | * |
| 1789 | * If the original input already has only one reference, then |
| 1790 | * simply return it, but clear all cached information, since |
| 1791 | * it may be rendered invalid by the operations that will be |
| 1792 | * performed on the result. |
| 1793 | * |
| 1794 | * Otherwise, create a duplicate (without any cached information). |
| 1795 | */ |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 1796 | struct isl_map *isl_map_cow(struct isl_map *map) |
| 1797 | { |
| 1798 | if (!map) |
| 1799 | return NULL; |
| 1800 | |
| 1801 | if (map->ref == 1) |
Tobias Grosser | 07b2095 | 2016-06-12 04:30:40 +0000 | [diff] [blame] | 1802 | return clear_caches(map); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 1803 | map->ref--; |
| 1804 | return isl_map_dup(map); |
| 1805 | } |
| 1806 | |
| 1807 | static void swap_vars(struct isl_blk blk, isl_int *a, |
| 1808 | unsigned a_len, unsigned b_len) |
| 1809 | { |
| 1810 | isl_seq_cpy(blk.data, a+a_len, b_len); |
| 1811 | isl_seq_cpy(blk.data+b_len, a, a_len); |
| 1812 | isl_seq_cpy(a, blk.data, b_len+a_len); |
| 1813 | } |
| 1814 | |
| 1815 | static __isl_give isl_basic_map *isl_basic_map_swap_vars( |
| 1816 | __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2) |
| 1817 | { |
| 1818 | int i; |
| 1819 | struct isl_blk blk; |
| 1820 | |
| 1821 | if (!bmap) |
| 1822 | goto error; |
| 1823 | |
| 1824 | isl_assert(bmap->ctx, |
| 1825 | pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error); |
| 1826 | |
| 1827 | if (n1 == 0 || n2 == 0) |
| 1828 | return bmap; |
| 1829 | |
| 1830 | bmap = isl_basic_map_cow(bmap); |
| 1831 | if (!bmap) |
| 1832 | return NULL; |
| 1833 | |
| 1834 | blk = isl_blk_alloc(bmap->ctx, n1 + n2); |
| 1835 | if (isl_blk_is_error(blk)) |
| 1836 | goto error; |
| 1837 | |
| 1838 | for (i = 0; i < bmap->n_eq; ++i) |
| 1839 | swap_vars(blk, |
| 1840 | bmap->eq[i] + pos, n1, n2); |
| 1841 | |
| 1842 | for (i = 0; i < bmap->n_ineq; ++i) |
| 1843 | swap_vars(blk, |
| 1844 | bmap->ineq[i] + pos, n1, n2); |
| 1845 | |
| 1846 | for (i = 0; i < bmap->n_div; ++i) |
| 1847 | swap_vars(blk, |
| 1848 | bmap->div[i]+1 + pos, n1, n2); |
| 1849 | |
| 1850 | isl_blk_free(bmap->ctx, blk); |
| 1851 | |
| 1852 | ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED); |
| 1853 | bmap = isl_basic_map_gauss(bmap, NULL); |
| 1854 | return isl_basic_map_finalize(bmap); |
| 1855 | error: |
| 1856 | isl_basic_map_free(bmap); |
| 1857 | return NULL; |
| 1858 | } |
| 1859 | |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 1860 | struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap) |
| 1861 | { |
| 1862 | int i = 0; |
| 1863 | unsigned total; |
| 1864 | if (!bmap) |
| 1865 | goto error; |
| 1866 | total = isl_basic_map_total_dim(bmap); |
| 1867 | isl_basic_map_free_div(bmap, bmap->n_div); |
| 1868 | isl_basic_map_free_inequality(bmap, bmap->n_ineq); |
| 1869 | if (bmap->n_eq > 0) |
| 1870 | isl_basic_map_free_equality(bmap, bmap->n_eq-1); |
| 1871 | else { |
| 1872 | i = isl_basic_map_alloc_equality(bmap); |
| 1873 | if (i < 0) |
| 1874 | goto error; |
| 1875 | } |
| 1876 | isl_int_set_si(bmap->eq[i][0], 1); |
| 1877 | isl_seq_clr(bmap->eq[i]+1, total); |
| 1878 | ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY); |
| 1879 | isl_vec_free(bmap->sample); |
| 1880 | bmap->sample = NULL; |
| 1881 | return isl_basic_map_finalize(bmap); |
| 1882 | error: |
| 1883 | isl_basic_map_free(bmap); |
| 1884 | return NULL; |
| 1885 | } |
| 1886 | |
| 1887 | struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset) |
| 1888 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 1889 | return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset))); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 1890 | } |
| 1891 | |
| 1892 | /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints |
| 1893 | * of "bmap"). |
| 1894 | */ |
| 1895 | static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b) |
| 1896 | { |
| 1897 | isl_int *t = bmap->div[a]; |
| 1898 | bmap->div[a] = bmap->div[b]; |
| 1899 | bmap->div[b] = t; |
| 1900 | } |
| 1901 | |
| 1902 | /* Swap divs "a" and "b" in "bmap" and adjust the constraints and |
| 1903 | * div definitions accordingly. |
| 1904 | */ |
| 1905 | void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b) |
| 1906 | { |
| 1907 | int i; |
| 1908 | unsigned off = isl_space_dim(bmap->dim, isl_dim_all); |
| 1909 | |
| 1910 | swap_div(bmap, a, b); |
| 1911 | |
| 1912 | for (i = 0; i < bmap->n_eq; ++i) |
| 1913 | isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]); |
| 1914 | |
| 1915 | for (i = 0; i < bmap->n_ineq; ++i) |
| 1916 | isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]); |
| 1917 | |
| 1918 | for (i = 0; i < bmap->n_div; ++i) |
| 1919 | isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]); |
| 1920 | ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED); |
| 1921 | } |
| 1922 | |
Tobias Grosser | 932ec01 | 2016-07-06 09:11:00 +0000 | [diff] [blame] | 1923 | /* Swap divs "a" and "b" in "bset" and adjust the constraints and |
| 1924 | * div definitions accordingly. |
| 1925 | */ |
| 1926 | void isl_basic_set_swap_div(__isl_keep isl_basic_set *bset, int a, int b) |
| 1927 | { |
| 1928 | isl_basic_map_swap_div(bset, a, b); |
| 1929 | } |
| 1930 | |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 1931 | /* Eliminate the specified n dimensions starting at first from the |
| 1932 | * constraints, without removing the dimensions from the space. |
| 1933 | * If the set is rational, the dimensions are eliminated using Fourier-Motzkin. |
| 1934 | */ |
| 1935 | __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map, |
| 1936 | enum isl_dim_type type, unsigned first, unsigned n) |
| 1937 | { |
| 1938 | int i; |
| 1939 | |
| 1940 | if (!map) |
| 1941 | return NULL; |
| 1942 | if (n == 0) |
| 1943 | return map; |
| 1944 | |
| 1945 | if (first + n > isl_map_dim(map, type) || first + n < first) |
| 1946 | isl_die(map->ctx, isl_error_invalid, |
| 1947 | "index out of bounds", goto error); |
| 1948 | |
| 1949 | map = isl_map_cow(map); |
| 1950 | if (!map) |
| 1951 | return NULL; |
| 1952 | |
| 1953 | for (i = 0; i < map->n; ++i) { |
| 1954 | map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n); |
| 1955 | if (!map->p[i]) |
| 1956 | goto error; |
| 1957 | } |
| 1958 | return map; |
| 1959 | error: |
| 1960 | isl_map_free(map); |
| 1961 | return NULL; |
| 1962 | } |
| 1963 | |
| 1964 | /* Eliminate the specified n dimensions starting at first from the |
| 1965 | * constraints, without removing the dimensions from the space. |
| 1966 | * If the set is rational, the dimensions are eliminated using Fourier-Motzkin. |
| 1967 | */ |
| 1968 | __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set, |
| 1969 | enum isl_dim_type type, unsigned first, unsigned n) |
| 1970 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 1971 | return set_from_map(isl_map_eliminate(set_to_map(set), type, first, n)); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 1972 | } |
| 1973 | |
| 1974 | /* Eliminate the specified n dimensions starting at first from the |
| 1975 | * constraints, without removing the dimensions from the space. |
| 1976 | * If the set is rational, the dimensions are eliminated using Fourier-Motzkin. |
| 1977 | */ |
| 1978 | __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set, |
| 1979 | unsigned first, unsigned n) |
| 1980 | { |
| 1981 | return isl_set_eliminate(set, isl_dim_set, first, n); |
| 1982 | } |
| 1983 | |
| 1984 | __isl_give isl_basic_map *isl_basic_map_remove_divs( |
| 1985 | __isl_take isl_basic_map *bmap) |
| 1986 | { |
| 1987 | if (!bmap) |
| 1988 | return NULL; |
| 1989 | bmap = isl_basic_map_eliminate_vars(bmap, |
| 1990 | isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div); |
| 1991 | if (!bmap) |
| 1992 | return NULL; |
| 1993 | bmap->n_div = 0; |
| 1994 | return isl_basic_map_finalize(bmap); |
| 1995 | } |
| 1996 | |
| 1997 | __isl_give isl_basic_set *isl_basic_set_remove_divs( |
| 1998 | __isl_take isl_basic_set *bset) |
| 1999 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 2000 | return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset))); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 2001 | } |
| 2002 | |
| 2003 | __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map) |
| 2004 | { |
| 2005 | int i; |
| 2006 | |
| 2007 | if (!map) |
| 2008 | return NULL; |
| 2009 | if (map->n == 0) |
| 2010 | return map; |
| 2011 | |
| 2012 | map = isl_map_cow(map); |
| 2013 | if (!map) |
| 2014 | return NULL; |
| 2015 | |
| 2016 | for (i = 0; i < map->n; ++i) { |
| 2017 | map->p[i] = isl_basic_map_remove_divs(map->p[i]); |
| 2018 | if (!map->p[i]) |
| 2019 | goto error; |
| 2020 | } |
| 2021 | return map; |
| 2022 | error: |
| 2023 | isl_map_free(map); |
| 2024 | return NULL; |
| 2025 | } |
| 2026 | |
| 2027 | __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set) |
| 2028 | { |
| 2029 | return isl_map_remove_divs(set); |
| 2030 | } |
| 2031 | |
| 2032 | struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap, |
| 2033 | enum isl_dim_type type, unsigned first, unsigned n) |
| 2034 | { |
| 2035 | if (!bmap) |
| 2036 | return NULL; |
| 2037 | isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type), |
| 2038 | goto error); |
| 2039 | if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type)) |
| 2040 | return bmap; |
| 2041 | bmap = isl_basic_map_eliminate_vars(bmap, |
| 2042 | isl_basic_map_offset(bmap, type) - 1 + first, n); |
| 2043 | if (!bmap) |
| 2044 | return bmap; |
| 2045 | if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div) |
| 2046 | return bmap; |
| 2047 | bmap = isl_basic_map_drop(bmap, type, first, n); |
| 2048 | return bmap; |
| 2049 | error: |
| 2050 | isl_basic_map_free(bmap); |
| 2051 | return NULL; |
| 2052 | } |
| 2053 | |
| 2054 | /* Return true if the definition of the given div (recursively) involves |
| 2055 | * any of the given variables. |
| 2056 | */ |
| 2057 | static int div_involves_vars(__isl_keep isl_basic_map *bmap, int div, |
| 2058 | unsigned first, unsigned n) |
| 2059 | { |
| 2060 | int i; |
| 2061 | unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div); |
| 2062 | |
| 2063 | if (isl_int_is_zero(bmap->div[div][0])) |
| 2064 | return 0; |
| 2065 | if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0) |
| 2066 | return 1; |
| 2067 | |
| 2068 | for (i = bmap->n_div - 1; i >= 0; --i) { |
| 2069 | if (isl_int_is_zero(bmap->div[div][1 + div_offset + i])) |
| 2070 | continue; |
| 2071 | if (div_involves_vars(bmap, i, first, n)) |
| 2072 | return 1; |
| 2073 | } |
| 2074 | |
| 2075 | return 0; |
| 2076 | } |
| 2077 | |
| 2078 | /* Try and add a lower and/or upper bound on "div" to "bmap" |
| 2079 | * based on inequality "i". |
| 2080 | * "total" is the total number of variables (excluding the divs). |
| 2081 | * "v" is a temporary object that can be used during the calculations. |
| 2082 | * If "lb" is set, then a lower bound should be constructed. |
| 2083 | * If "ub" is set, then an upper bound should be constructed. |
| 2084 | * |
| 2085 | * The calling function has already checked that the inequality does not |
| 2086 | * reference "div", but we still need to check that the inequality is |
| 2087 | * of the right form. We'll consider the case where we want to construct |
| 2088 | * a lower bound. The construction of upper bounds is similar. |
| 2089 | * |
| 2090 | * Let "div" be of the form |
| 2091 | * |
| 2092 | * q = floor((a + f(x))/d) |
| 2093 | * |
| 2094 | * We essentially check if constraint "i" is of the form |
| 2095 | * |
| 2096 | * b + f(x) >= 0 |
| 2097 | * |
| 2098 | * so that we can use it to derive a lower bound on "div". |
| 2099 | * However, we allow a slightly more general form |
| 2100 | * |
| 2101 | * b + g(x) >= 0 |
| 2102 | * |
| 2103 | * with the condition that the coefficients of g(x) - f(x) are all |
| 2104 | * divisible by d. |
| 2105 | * Rewriting this constraint as |
| 2106 | * |
| 2107 | * 0 >= -b - g(x) |
| 2108 | * |
| 2109 | * adding a + f(x) to both sides and dividing by d, we obtain |
| 2110 | * |
| 2111 | * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d |
| 2112 | * |
| 2113 | * Taking the floor on both sides, we obtain |
| 2114 | * |
| 2115 | * q >= floor((a-b)/d) + (f(x)-g(x))/d |
| 2116 | * |
| 2117 | * or |
| 2118 | * |
| 2119 | * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0 |
| 2120 | * |
| 2121 | * In the case of an upper bound, we construct the constraint |
| 2122 | * |
| 2123 | * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0 |
| 2124 | * |
| 2125 | */ |
| 2126 | static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq( |
| 2127 | __isl_take isl_basic_map *bmap, int div, int i, |
| 2128 | unsigned total, isl_int v, int lb, int ub) |
| 2129 | { |
| 2130 | int j; |
| 2131 | |
| 2132 | for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) { |
| 2133 | if (lb) { |
| 2134 | isl_int_sub(v, bmap->ineq[i][1 + j], |
| 2135 | bmap->div[div][1 + 1 + j]); |
| 2136 | lb = isl_int_is_divisible_by(v, bmap->div[div][0]); |
| 2137 | } |
| 2138 | if (ub) { |
| 2139 | isl_int_add(v, bmap->ineq[i][1 + j], |
| 2140 | bmap->div[div][1 + 1 + j]); |
| 2141 | ub = isl_int_is_divisible_by(v, bmap->div[div][0]); |
| 2142 | } |
| 2143 | } |
| 2144 | if (!lb && !ub) |
| 2145 | return bmap; |
| 2146 | |
| 2147 | bmap = isl_basic_map_cow(bmap); |
| 2148 | bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub); |
| 2149 | if (lb) { |
| 2150 | int k = isl_basic_map_alloc_inequality(bmap); |
| 2151 | if (k < 0) |
| 2152 | goto error; |
| 2153 | for (j = 0; j < 1 + total + bmap->n_div; ++j) { |
| 2154 | isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j], |
| 2155 | bmap->div[div][1 + j]); |
| 2156 | isl_int_cdiv_q(bmap->ineq[k][j], |
| 2157 | bmap->ineq[k][j], bmap->div[div][0]); |
| 2158 | } |
| 2159 | isl_int_set_si(bmap->ineq[k][1 + total + div], 1); |
| 2160 | } |
| 2161 | if (ub) { |
| 2162 | int k = isl_basic_map_alloc_inequality(bmap); |
| 2163 | if (k < 0) |
| 2164 | goto error; |
| 2165 | for (j = 0; j < 1 + total + bmap->n_div; ++j) { |
| 2166 | isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j], |
| 2167 | bmap->div[div][1 + j]); |
| 2168 | isl_int_fdiv_q(bmap->ineq[k][j], |
| 2169 | bmap->ineq[k][j], bmap->div[div][0]); |
| 2170 | } |
| 2171 | isl_int_set_si(bmap->ineq[k][1 + total + div], -1); |
| 2172 | } |
| 2173 | |
| 2174 | ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED); |
| 2175 | return bmap; |
| 2176 | error: |
| 2177 | isl_basic_map_free(bmap); |
| 2178 | return NULL; |
| 2179 | } |
| 2180 | |
| 2181 | /* This function is called right before "div" is eliminated from "bmap" |
| 2182 | * using Fourier-Motzkin. |
| 2183 | * Look through the constraints of "bmap" for constraints on the argument |
| 2184 | * of the integer division and use them to construct constraints on the |
| 2185 | * integer division itself. These constraints can then be combined |
| 2186 | * during the Fourier-Motzkin elimination. |
| 2187 | * Note that it is only useful to introduce lower bounds on "div" |
| 2188 | * if "bmap" already contains upper bounds on "div" as the newly |
| 2189 | * introduce lower bounds can then be combined with the pre-existing |
| 2190 | * upper bounds. Similarly for upper bounds. |
| 2191 | * We therefore first check if "bmap" contains any lower and/or upper bounds |
| 2192 | * on "div". |
| 2193 | * |
| 2194 | * It is interesting to note that the introduction of these constraints |
| 2195 | * can indeed lead to more accurate results, even when compared to |
| 2196 | * deriving constraints on the argument of "div" from constraints on "div". |
| 2197 | * Consider, for example, the set |
| 2198 | * |
| 2199 | * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k } |
| 2200 | * |
| 2201 | * The second constraint can be rewritten as |
| 2202 | * |
| 2203 | * 2 * [(-i-2j+3)/4] + k >= 0 |
| 2204 | * |
| 2205 | * from which we can derive |
| 2206 | * |
| 2207 | * -i - 2j + 3 >= -2k |
| 2208 | * |
| 2209 | * or |
| 2210 | * |
| 2211 | * i + 2j <= 3 + 2k |
| 2212 | * |
| 2213 | * Combined with the first constraint, we obtain |
| 2214 | * |
| 2215 | * -3 <= 3 + 2k or k >= -3 |
| 2216 | * |
| 2217 | * If, on the other hand we derive a constraint on [(i+2j)/4] from |
| 2218 | * the first constraint, we obtain |
| 2219 | * |
| 2220 | * [(i + 2j)/4] >= [-3/4] = -1 |
| 2221 | * |
| 2222 | * Combining this constraint with the second constraint, we obtain |
| 2223 | * |
| 2224 | * k >= -2 |
| 2225 | */ |
| 2226 | static __isl_give isl_basic_map *insert_bounds_on_div( |
| 2227 | __isl_take isl_basic_map *bmap, int div) |
| 2228 | { |
| 2229 | int i; |
| 2230 | int check_lb, check_ub; |
| 2231 | isl_int v; |
| 2232 | unsigned total; |
| 2233 | |
| 2234 | if (!bmap) |
| 2235 | return NULL; |
| 2236 | |
| 2237 | if (isl_int_is_zero(bmap->div[div][0])) |
| 2238 | return bmap; |
| 2239 | |
| 2240 | total = isl_space_dim(bmap->dim, isl_dim_all); |
| 2241 | |
| 2242 | check_lb = 0; |
| 2243 | check_ub = 0; |
| 2244 | for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) { |
| 2245 | int s = isl_int_sgn(bmap->ineq[i][1 + total + div]); |
| 2246 | if (s > 0) |
| 2247 | check_ub = 1; |
| 2248 | if (s < 0) |
| 2249 | check_lb = 1; |
| 2250 | } |
| 2251 | |
| 2252 | if (!check_lb && !check_ub) |
| 2253 | return bmap; |
| 2254 | |
| 2255 | isl_int_init(v); |
| 2256 | |
| 2257 | for (i = 0; bmap && i < bmap->n_ineq; ++i) { |
| 2258 | if (!isl_int_is_zero(bmap->ineq[i][1 + total + div])) |
| 2259 | continue; |
| 2260 | |
| 2261 | bmap = insert_bounds_on_div_from_ineq(bmap, div, i, total, v, |
| 2262 | check_lb, check_ub); |
| 2263 | } |
| 2264 | |
| 2265 | isl_int_clear(v); |
| 2266 | |
| 2267 | return bmap; |
| 2268 | } |
| 2269 | |
| 2270 | /* Remove all divs (recursively) involving any of the given dimensions |
| 2271 | * in their definitions. |
| 2272 | */ |
| 2273 | __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims( |
| 2274 | __isl_take isl_basic_map *bmap, |
| 2275 | enum isl_dim_type type, unsigned first, unsigned n) |
| 2276 | { |
| 2277 | int i; |
| 2278 | |
| 2279 | if (!bmap) |
| 2280 | return NULL; |
| 2281 | isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type), |
| 2282 | goto error); |
| 2283 | first += isl_basic_map_offset(bmap, type); |
| 2284 | |
| 2285 | for (i = bmap->n_div - 1; i >= 0; --i) { |
| 2286 | if (!div_involves_vars(bmap, i, first, n)) |
| 2287 | continue; |
| 2288 | bmap = insert_bounds_on_div(bmap, i); |
| 2289 | bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1); |
| 2290 | if (!bmap) |
| 2291 | return NULL; |
| 2292 | i = bmap->n_div; |
| 2293 | } |
| 2294 | |
| 2295 | return bmap; |
| 2296 | error: |
| 2297 | isl_basic_map_free(bmap); |
| 2298 | return NULL; |
| 2299 | } |
| 2300 | |
| 2301 | __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims( |
| 2302 | __isl_take isl_basic_set *bset, |
| 2303 | enum isl_dim_type type, unsigned first, unsigned n) |
| 2304 | { |
| 2305 | return isl_basic_map_remove_divs_involving_dims(bset, type, first, n); |
| 2306 | } |
| 2307 | |
| 2308 | __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map, |
| 2309 | enum isl_dim_type type, unsigned first, unsigned n) |
| 2310 | { |
| 2311 | int i; |
| 2312 | |
| 2313 | if (!map) |
| 2314 | return NULL; |
| 2315 | if (map->n == 0) |
| 2316 | return map; |
| 2317 | |
| 2318 | map = isl_map_cow(map); |
| 2319 | if (!map) |
| 2320 | return NULL; |
| 2321 | |
| 2322 | for (i = 0; i < map->n; ++i) { |
| 2323 | map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i], |
| 2324 | type, first, n); |
| 2325 | if (!map->p[i]) |
| 2326 | goto error; |
| 2327 | } |
| 2328 | return map; |
| 2329 | error: |
| 2330 | isl_map_free(map); |
| 2331 | return NULL; |
| 2332 | } |
| 2333 | |
| 2334 | __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set, |
| 2335 | enum isl_dim_type type, unsigned first, unsigned n) |
| 2336 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 2337 | return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set), |
| 2338 | type, first, n)); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 2339 | } |
| 2340 | |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 2341 | /* Does the description of "bmap" depend on the specified dimensions? |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 2342 | * We also check whether the dimensions appear in any of the div definitions. |
| 2343 | * In principle there is no need for this check. If the dimensions appear |
| 2344 | * in a div definition, they also appear in the defining constraints of that |
| 2345 | * div. |
| 2346 | */ |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 2347 | isl_bool isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap, |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 2348 | enum isl_dim_type type, unsigned first, unsigned n) |
| 2349 | { |
| 2350 | int i; |
| 2351 | |
| 2352 | if (!bmap) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 2353 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 2354 | |
| 2355 | if (first + n > isl_basic_map_dim(bmap, type)) |
| 2356 | isl_die(bmap->ctx, isl_error_invalid, |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 2357 | "index out of bounds", return isl_bool_error); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 2358 | |
| 2359 | first += isl_basic_map_offset(bmap, type); |
| 2360 | for (i = 0; i < bmap->n_eq; ++i) |
| 2361 | if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 2362 | return isl_bool_true; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 2363 | for (i = 0; i < bmap->n_ineq; ++i) |
| 2364 | if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 2365 | return isl_bool_true; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 2366 | for (i = 0; i < bmap->n_div; ++i) { |
| 2367 | if (isl_int_is_zero(bmap->div[i][0])) |
| 2368 | continue; |
| 2369 | if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 2370 | return isl_bool_true; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 2371 | } |
| 2372 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 2373 | return isl_bool_false; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 2374 | } |
| 2375 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 2376 | isl_bool isl_map_involves_dims(__isl_keep isl_map *map, |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 2377 | enum isl_dim_type type, unsigned first, unsigned n) |
| 2378 | { |
| 2379 | int i; |
| 2380 | |
| 2381 | if (!map) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 2382 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 2383 | |
| 2384 | if (first + n > isl_map_dim(map, type)) |
| 2385 | isl_die(map->ctx, isl_error_invalid, |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 2386 | "index out of bounds", return isl_bool_error); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 2387 | |
| 2388 | for (i = 0; i < map->n; ++i) { |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 2389 | isl_bool involves = isl_basic_map_involves_dims(map->p[i], |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 2390 | type, first, n); |
| 2391 | if (involves < 0 || involves) |
| 2392 | return involves; |
| 2393 | } |
| 2394 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 2395 | return isl_bool_false; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 2396 | } |
| 2397 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 2398 | isl_bool isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset, |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 2399 | enum isl_dim_type type, unsigned first, unsigned n) |
| 2400 | { |
| 2401 | return isl_basic_map_involves_dims(bset, type, first, n); |
| 2402 | } |
| 2403 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 2404 | isl_bool isl_set_involves_dims(__isl_keep isl_set *set, |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 2405 | enum isl_dim_type type, unsigned first, unsigned n) |
| 2406 | { |
| 2407 | return isl_map_involves_dims(set, type, first, n); |
| 2408 | } |
| 2409 | |
Tobias Grosser | 932ec01 | 2016-07-06 09:11:00 +0000 | [diff] [blame] | 2410 | /* Does local variable "div" of "bmap" have a complete explicit representation? |
| 2411 | * Having a complete explicit representation requires not only |
| 2412 | * an explicit representation, but also that all local variables |
| 2413 | * that appear in this explicit representation in turn have |
| 2414 | * a complete explicit representation. |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 2415 | */ |
Tobias Grosser | 932ec01 | 2016-07-06 09:11:00 +0000 | [diff] [blame] | 2416 | isl_bool isl_basic_map_div_is_known(__isl_keep isl_basic_map *bmap, int div) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 2417 | { |
| 2418 | int i; |
| 2419 | unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div); |
Tobias Grosser | 932ec01 | 2016-07-06 09:11:00 +0000 | [diff] [blame] | 2420 | isl_bool marked; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 2421 | |
Tobias Grosser | 932ec01 | 2016-07-06 09:11:00 +0000 | [diff] [blame] | 2422 | marked = isl_basic_map_div_is_marked_unknown(bmap, div); |
| 2423 | if (marked < 0 || marked) |
| 2424 | return isl_bool_not(marked); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 2425 | |
| 2426 | for (i = bmap->n_div - 1; i >= 0; --i) { |
Tobias Grosser | 932ec01 | 2016-07-06 09:11:00 +0000 | [diff] [blame] | 2427 | isl_bool known; |
| 2428 | |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 2429 | if (isl_int_is_zero(bmap->div[div][1 + div_offset + i])) |
| 2430 | continue; |
Tobias Grosser | 932ec01 | 2016-07-06 09:11:00 +0000 | [diff] [blame] | 2431 | known = isl_basic_map_div_is_known(bmap, i); |
| 2432 | if (known < 0 || !known) |
| 2433 | return known; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 2434 | } |
| 2435 | |
Tobias Grosser | 932ec01 | 2016-07-06 09:11:00 +0000 | [diff] [blame] | 2436 | return isl_bool_true; |
| 2437 | } |
| 2438 | |
| 2439 | /* Does local variable "div" of "bset" have a complete explicit representation? |
| 2440 | */ |
| 2441 | isl_bool isl_basic_set_div_is_known(__isl_keep isl_basic_set *bset, int div) |
| 2442 | { |
| 2443 | return isl_basic_map_div_is_known(bset, div); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 2444 | } |
| 2445 | |
| 2446 | /* Remove all divs that are unknown or defined in terms of unknown divs. |
| 2447 | */ |
| 2448 | __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs( |
| 2449 | __isl_take isl_basic_map *bmap) |
| 2450 | { |
| 2451 | int i; |
| 2452 | |
| 2453 | if (!bmap) |
| 2454 | return NULL; |
| 2455 | |
| 2456 | for (i = bmap->n_div - 1; i >= 0; --i) { |
Tobias Grosser | 932ec01 | 2016-07-06 09:11:00 +0000 | [diff] [blame] | 2457 | if (isl_basic_map_div_is_known(bmap, i)) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 2458 | continue; |
| 2459 | bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1); |
| 2460 | if (!bmap) |
| 2461 | return NULL; |
| 2462 | i = bmap->n_div; |
| 2463 | } |
| 2464 | |
| 2465 | return bmap; |
| 2466 | } |
| 2467 | |
| 2468 | /* Remove all divs that are unknown or defined in terms of unknown divs. |
| 2469 | */ |
| 2470 | __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs( |
| 2471 | __isl_take isl_basic_set *bset) |
| 2472 | { |
| 2473 | return isl_basic_map_remove_unknown_divs(bset); |
| 2474 | } |
| 2475 | |
| 2476 | __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map) |
| 2477 | { |
| 2478 | int i; |
| 2479 | |
| 2480 | if (!map) |
| 2481 | return NULL; |
| 2482 | if (map->n == 0) |
| 2483 | return map; |
| 2484 | |
| 2485 | map = isl_map_cow(map); |
| 2486 | if (!map) |
| 2487 | return NULL; |
| 2488 | |
| 2489 | for (i = 0; i < map->n; ++i) { |
| 2490 | map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]); |
| 2491 | if (!map->p[i]) |
| 2492 | goto error; |
| 2493 | } |
| 2494 | return map; |
| 2495 | error: |
| 2496 | isl_map_free(map); |
| 2497 | return NULL; |
| 2498 | } |
| 2499 | |
| 2500 | __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set) |
| 2501 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 2502 | return set_from_map(isl_map_remove_unknown_divs(set_to_map(set))); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 2503 | } |
| 2504 | |
| 2505 | __isl_give isl_basic_set *isl_basic_set_remove_dims( |
| 2506 | __isl_take isl_basic_set *bset, |
| 2507 | enum isl_dim_type type, unsigned first, unsigned n) |
| 2508 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 2509 | isl_basic_map *bmap = bset_to_bmap(bset); |
| 2510 | bmap = isl_basic_map_remove_dims(bmap, type, first, n); |
| 2511 | return bset_from_bmap(bmap); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 2512 | } |
| 2513 | |
| 2514 | struct isl_map *isl_map_remove_dims(struct isl_map *map, |
| 2515 | enum isl_dim_type type, unsigned first, unsigned n) |
| 2516 | { |
| 2517 | int i; |
| 2518 | |
| 2519 | if (n == 0) |
| 2520 | return map; |
| 2521 | |
| 2522 | map = isl_map_cow(map); |
| 2523 | if (!map) |
| 2524 | return NULL; |
| 2525 | isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error); |
| 2526 | |
| 2527 | for (i = 0; i < map->n; ++i) { |
| 2528 | map->p[i] = isl_basic_map_eliminate_vars(map->p[i], |
| 2529 | isl_basic_map_offset(map->p[i], type) - 1 + first, n); |
| 2530 | if (!map->p[i]) |
| 2531 | goto error; |
| 2532 | } |
| 2533 | map = isl_map_drop(map, type, first, n); |
| 2534 | return map; |
| 2535 | error: |
| 2536 | isl_map_free(map); |
| 2537 | return NULL; |
| 2538 | } |
| 2539 | |
| 2540 | __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset, |
| 2541 | enum isl_dim_type type, unsigned first, unsigned n) |
| 2542 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 2543 | return set_from_map(isl_map_remove_dims(set_to_map(bset), |
| 2544 | type, first, n)); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 2545 | } |
| 2546 | |
| 2547 | /* Project out n inputs starting at first using Fourier-Motzkin */ |
| 2548 | struct isl_map *isl_map_remove_inputs(struct isl_map *map, |
| 2549 | unsigned first, unsigned n) |
| 2550 | { |
| 2551 | return isl_map_remove_dims(map, isl_dim_in, first, n); |
| 2552 | } |
| 2553 | |
| 2554 | static void dump_term(struct isl_basic_map *bmap, |
| 2555 | isl_int c, int pos, FILE *out) |
| 2556 | { |
| 2557 | const char *name; |
| 2558 | unsigned in = isl_basic_map_n_in(bmap); |
| 2559 | unsigned dim = in + isl_basic_map_n_out(bmap); |
| 2560 | unsigned nparam = isl_basic_map_n_param(bmap); |
| 2561 | if (!pos) |
| 2562 | isl_int_print(out, c, 0); |
| 2563 | else { |
| 2564 | if (!isl_int_is_one(c)) |
| 2565 | isl_int_print(out, c, 0); |
| 2566 | if (pos < 1 + nparam) { |
| 2567 | name = isl_space_get_dim_name(bmap->dim, |
| 2568 | isl_dim_param, pos - 1); |
| 2569 | if (name) |
| 2570 | fprintf(out, "%s", name); |
| 2571 | else |
| 2572 | fprintf(out, "p%d", pos - 1); |
| 2573 | } else if (pos < 1 + nparam + in) |
| 2574 | fprintf(out, "i%d", pos - 1 - nparam); |
| 2575 | else if (pos < 1 + nparam + dim) |
| 2576 | fprintf(out, "o%d", pos - 1 - nparam - in); |
| 2577 | else |
| 2578 | fprintf(out, "e%d", pos - 1 - nparam - dim); |
| 2579 | } |
| 2580 | } |
| 2581 | |
| 2582 | static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c, |
| 2583 | int sign, FILE *out) |
| 2584 | { |
| 2585 | int i; |
| 2586 | int first; |
| 2587 | unsigned len = 1 + isl_basic_map_total_dim(bmap); |
| 2588 | isl_int v; |
| 2589 | |
| 2590 | isl_int_init(v); |
| 2591 | for (i = 0, first = 1; i < len; ++i) { |
| 2592 | if (isl_int_sgn(c[i]) * sign <= 0) |
| 2593 | continue; |
| 2594 | if (!first) |
| 2595 | fprintf(out, " + "); |
| 2596 | first = 0; |
| 2597 | isl_int_abs(v, c[i]); |
| 2598 | dump_term(bmap, v, i, out); |
| 2599 | } |
| 2600 | isl_int_clear(v); |
| 2601 | if (first) |
| 2602 | fprintf(out, "0"); |
| 2603 | } |
| 2604 | |
| 2605 | static void dump_constraint(struct isl_basic_map *bmap, isl_int *c, |
| 2606 | const char *op, FILE *out, int indent) |
| 2607 | { |
| 2608 | int i; |
| 2609 | |
| 2610 | fprintf(out, "%*s", indent, ""); |
| 2611 | |
| 2612 | dump_constraint_sign(bmap, c, 1, out); |
| 2613 | fprintf(out, " %s ", op); |
| 2614 | dump_constraint_sign(bmap, c, -1, out); |
| 2615 | |
| 2616 | fprintf(out, "\n"); |
| 2617 | |
| 2618 | for (i = bmap->n_div; i < bmap->extra; ++i) { |
| 2619 | if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i])) |
| 2620 | continue; |
| 2621 | fprintf(out, "%*s", indent, ""); |
| 2622 | fprintf(out, "ERROR: unused div coefficient not zero\n"); |
| 2623 | abort(); |
| 2624 | } |
| 2625 | } |
| 2626 | |
| 2627 | static void dump_constraints(struct isl_basic_map *bmap, |
| 2628 | isl_int **c, unsigned n, |
| 2629 | const char *op, FILE *out, int indent) |
| 2630 | { |
| 2631 | int i; |
| 2632 | |
| 2633 | for (i = 0; i < n; ++i) |
| 2634 | dump_constraint(bmap, c[i], op, out, indent); |
| 2635 | } |
| 2636 | |
| 2637 | static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out) |
| 2638 | { |
| 2639 | int j; |
| 2640 | int first = 1; |
| 2641 | unsigned total = isl_basic_map_total_dim(bmap); |
| 2642 | |
| 2643 | for (j = 0; j < 1 + total; ++j) { |
| 2644 | if (isl_int_is_zero(exp[j])) |
| 2645 | continue; |
| 2646 | if (!first && isl_int_is_pos(exp[j])) |
| 2647 | fprintf(out, "+"); |
| 2648 | dump_term(bmap, exp[j], j, out); |
| 2649 | first = 0; |
| 2650 | } |
| 2651 | } |
| 2652 | |
| 2653 | static void dump(struct isl_basic_map *bmap, FILE *out, int indent) |
| 2654 | { |
| 2655 | int i; |
| 2656 | |
| 2657 | dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent); |
| 2658 | dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent); |
| 2659 | |
| 2660 | for (i = 0; i < bmap->n_div; ++i) { |
| 2661 | fprintf(out, "%*s", indent, ""); |
| 2662 | fprintf(out, "e%d = [(", i); |
| 2663 | dump_affine(bmap, bmap->div[i]+1, out); |
| 2664 | fprintf(out, ")/"); |
| 2665 | isl_int_print(out, bmap->div[i][0], 0); |
| 2666 | fprintf(out, "]\n"); |
| 2667 | } |
| 2668 | } |
| 2669 | |
| 2670 | void isl_basic_set_print_internal(struct isl_basic_set *bset, |
| 2671 | FILE *out, int indent) |
| 2672 | { |
| 2673 | if (!bset) { |
| 2674 | fprintf(out, "null basic set\n"); |
| 2675 | return; |
| 2676 | } |
| 2677 | |
| 2678 | fprintf(out, "%*s", indent, ""); |
| 2679 | fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n", |
| 2680 | bset->ref, bset->dim->nparam, bset->dim->n_out, |
| 2681 | bset->extra, bset->flags); |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 2682 | dump(bset_to_bmap(bset), out, indent); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 2683 | } |
| 2684 | |
| 2685 | void isl_basic_map_print_internal(struct isl_basic_map *bmap, |
| 2686 | FILE *out, int indent) |
| 2687 | { |
| 2688 | if (!bmap) { |
| 2689 | fprintf(out, "null basic map\n"); |
| 2690 | return; |
| 2691 | } |
| 2692 | |
| 2693 | fprintf(out, "%*s", indent, ""); |
| 2694 | fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, " |
| 2695 | "flags: %x, n_name: %d\n", |
| 2696 | bmap->ref, |
| 2697 | bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out, |
| 2698 | bmap->extra, bmap->flags, bmap->dim->n_id); |
| 2699 | dump(bmap, out, indent); |
| 2700 | } |
| 2701 | |
| 2702 | int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos) |
| 2703 | { |
| 2704 | unsigned total; |
| 2705 | if (!bmap) |
| 2706 | return -1; |
| 2707 | total = isl_basic_map_total_dim(bmap); |
| 2708 | isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1); |
| 2709 | isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total); |
| 2710 | isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1); |
| 2711 | ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED); |
| 2712 | return 0; |
| 2713 | } |
| 2714 | |
Tobias Grosser | 07b2095 | 2016-06-12 04:30:40 +0000 | [diff] [blame] | 2715 | __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *space, int n, |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 2716 | unsigned flags) |
| 2717 | { |
Tobias Grosser | 07b2095 | 2016-06-12 04:30:40 +0000 | [diff] [blame] | 2718 | if (!space) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 2719 | return NULL; |
Tobias Grosser | 07b2095 | 2016-06-12 04:30:40 +0000 | [diff] [blame] | 2720 | if (isl_space_dim(space, isl_dim_in) != 0) |
| 2721 | isl_die(isl_space_get_ctx(space), isl_error_invalid, |
| 2722 | "set cannot have input dimensions", goto error); |
| 2723 | return isl_map_alloc_space(space, n, flags); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 2724 | error: |
Tobias Grosser | 07b2095 | 2016-06-12 04:30:40 +0000 | [diff] [blame] | 2725 | isl_space_free(space); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 2726 | return NULL; |
| 2727 | } |
| 2728 | |
| 2729 | struct isl_set *isl_set_alloc(struct isl_ctx *ctx, |
| 2730 | unsigned nparam, unsigned dim, int n, unsigned flags) |
| 2731 | { |
| 2732 | struct isl_set *set; |
| 2733 | isl_space *dims; |
| 2734 | |
| 2735 | dims = isl_space_alloc(ctx, nparam, 0, dim); |
| 2736 | if (!dims) |
| 2737 | return NULL; |
| 2738 | |
| 2739 | set = isl_set_alloc_space(dims, n, flags); |
| 2740 | return set; |
| 2741 | } |
| 2742 | |
| 2743 | /* Make sure "map" has room for at least "n" more basic maps. |
| 2744 | */ |
| 2745 | struct isl_map *isl_map_grow(struct isl_map *map, int n) |
| 2746 | { |
| 2747 | int i; |
| 2748 | struct isl_map *grown = NULL; |
| 2749 | |
| 2750 | if (!map) |
| 2751 | return NULL; |
| 2752 | isl_assert(map->ctx, n >= 0, goto error); |
| 2753 | if (map->n + n <= map->size) |
| 2754 | return map; |
| 2755 | grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags); |
| 2756 | if (!grown) |
| 2757 | goto error; |
| 2758 | for (i = 0; i < map->n; ++i) { |
| 2759 | grown->p[i] = isl_basic_map_copy(map->p[i]); |
| 2760 | if (!grown->p[i]) |
| 2761 | goto error; |
| 2762 | grown->n++; |
| 2763 | } |
| 2764 | isl_map_free(map); |
| 2765 | return grown; |
| 2766 | error: |
| 2767 | isl_map_free(grown); |
| 2768 | isl_map_free(map); |
| 2769 | return NULL; |
| 2770 | } |
| 2771 | |
| 2772 | /* Make sure "set" has room for at least "n" more basic sets. |
| 2773 | */ |
| 2774 | struct isl_set *isl_set_grow(struct isl_set *set, int n) |
| 2775 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 2776 | return set_from_map(isl_map_grow(set_to_map(set), n)); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 2777 | } |
| 2778 | |
| 2779 | struct isl_set *isl_set_dup(struct isl_set *set) |
| 2780 | { |
| 2781 | int i; |
| 2782 | struct isl_set *dup; |
| 2783 | |
| 2784 | if (!set) |
| 2785 | return NULL; |
| 2786 | |
| 2787 | dup = isl_set_alloc_space(isl_space_copy(set->dim), set->n, set->flags); |
| 2788 | if (!dup) |
| 2789 | return NULL; |
| 2790 | for (i = 0; i < set->n; ++i) |
| 2791 | dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i])); |
| 2792 | return dup; |
| 2793 | } |
| 2794 | |
| 2795 | struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset) |
| 2796 | { |
| 2797 | return isl_map_from_basic_map(bset); |
| 2798 | } |
| 2799 | |
| 2800 | struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap) |
| 2801 | { |
| 2802 | struct isl_map *map; |
| 2803 | |
| 2804 | if (!bmap) |
| 2805 | return NULL; |
| 2806 | |
| 2807 | map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT); |
| 2808 | return isl_map_add_basic_map(map, bmap); |
| 2809 | } |
| 2810 | |
| 2811 | __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set, |
| 2812 | __isl_take isl_basic_set *bset) |
| 2813 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 2814 | return set_from_map(isl_map_add_basic_map(set_to_map(set), |
| 2815 | bset_to_bmap(bset))); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 2816 | } |
| 2817 | |
| 2818 | __isl_null isl_set *isl_set_free(__isl_take isl_set *set) |
| 2819 | { |
Tobias Grosser | 07b2095 | 2016-06-12 04:30:40 +0000 | [diff] [blame] | 2820 | return isl_map_free(set); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 2821 | } |
| 2822 | |
| 2823 | void isl_set_print_internal(struct isl_set *set, FILE *out, int indent) |
| 2824 | { |
| 2825 | int i; |
| 2826 | |
| 2827 | if (!set) { |
| 2828 | fprintf(out, "null set\n"); |
| 2829 | return; |
| 2830 | } |
| 2831 | |
| 2832 | fprintf(out, "%*s", indent, ""); |
| 2833 | fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n", |
| 2834 | set->ref, set->n, set->dim->nparam, set->dim->n_out, |
| 2835 | set->flags); |
| 2836 | for (i = 0; i < set->n; ++i) { |
| 2837 | fprintf(out, "%*s", indent, ""); |
| 2838 | fprintf(out, "basic set %d:\n", i); |
| 2839 | isl_basic_set_print_internal(set->p[i], out, indent+4); |
| 2840 | } |
| 2841 | } |
| 2842 | |
| 2843 | void isl_map_print_internal(struct isl_map *map, FILE *out, int indent) |
| 2844 | { |
| 2845 | int i; |
| 2846 | |
| 2847 | if (!map) { |
| 2848 | fprintf(out, "null map\n"); |
| 2849 | return; |
| 2850 | } |
| 2851 | |
| 2852 | fprintf(out, "%*s", indent, ""); |
| 2853 | fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, " |
| 2854 | "flags: %x, n_name: %d\n", |
| 2855 | map->ref, map->n, map->dim->nparam, map->dim->n_in, |
| 2856 | map->dim->n_out, map->flags, map->dim->n_id); |
| 2857 | for (i = 0; i < map->n; ++i) { |
| 2858 | fprintf(out, "%*s", indent, ""); |
| 2859 | fprintf(out, "basic map %d:\n", i); |
| 2860 | isl_basic_map_print_internal(map->p[i], out, indent+4); |
| 2861 | } |
| 2862 | } |
| 2863 | |
| 2864 | struct isl_basic_map *isl_basic_map_intersect_domain( |
| 2865 | struct isl_basic_map *bmap, struct isl_basic_set *bset) |
| 2866 | { |
| 2867 | struct isl_basic_map *bmap_domain; |
| 2868 | |
| 2869 | if (!bmap || !bset) |
| 2870 | goto error; |
| 2871 | |
| 2872 | isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param, |
| 2873 | bset->dim, isl_dim_param), goto error); |
| 2874 | |
| 2875 | if (isl_space_dim(bset->dim, isl_dim_set) != 0) |
| 2876 | isl_assert(bset->ctx, |
| 2877 | isl_basic_map_compatible_domain(bmap, bset), goto error); |
| 2878 | |
| 2879 | bmap = isl_basic_map_cow(bmap); |
| 2880 | if (!bmap) |
| 2881 | goto error; |
| 2882 | bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim), |
| 2883 | bset->n_div, bset->n_eq, bset->n_ineq); |
| 2884 | bmap_domain = isl_basic_map_from_domain(bset); |
| 2885 | bmap = add_constraints(bmap, bmap_domain, 0, 0); |
| 2886 | |
| 2887 | bmap = isl_basic_map_simplify(bmap); |
| 2888 | return isl_basic_map_finalize(bmap); |
| 2889 | error: |
| 2890 | isl_basic_map_free(bmap); |
| 2891 | isl_basic_set_free(bset); |
| 2892 | return NULL; |
| 2893 | } |
| 2894 | |
| 2895 | struct isl_basic_map *isl_basic_map_intersect_range( |
| 2896 | struct isl_basic_map *bmap, struct isl_basic_set *bset) |
| 2897 | { |
| 2898 | struct isl_basic_map *bmap_range; |
| 2899 | |
| 2900 | if (!bmap || !bset) |
| 2901 | goto error; |
| 2902 | |
| 2903 | isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param, |
| 2904 | bset->dim, isl_dim_param), goto error); |
| 2905 | |
| 2906 | if (isl_space_dim(bset->dim, isl_dim_set) != 0) |
| 2907 | isl_assert(bset->ctx, |
| 2908 | isl_basic_map_compatible_range(bmap, bset), goto error); |
| 2909 | |
Tobias Grosser | a67ac97 | 2016-02-26 11:35:12 +0000 | [diff] [blame] | 2910 | if (isl_basic_set_plain_is_universe(bset)) { |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 2911 | isl_basic_set_free(bset); |
| 2912 | return bmap; |
| 2913 | } |
| 2914 | |
| 2915 | bmap = isl_basic_map_cow(bmap); |
| 2916 | if (!bmap) |
| 2917 | goto error; |
| 2918 | bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim), |
| 2919 | bset->n_div, bset->n_eq, bset->n_ineq); |
Tobias Grosser | eab0943e | 2016-11-22 21:31:59 +0000 | [diff] [blame] | 2920 | bmap_range = bset_to_bmap(bset); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 2921 | bmap = add_constraints(bmap, bmap_range, 0, 0); |
| 2922 | |
| 2923 | bmap = isl_basic_map_simplify(bmap); |
| 2924 | return isl_basic_map_finalize(bmap); |
| 2925 | error: |
| 2926 | isl_basic_map_free(bmap); |
| 2927 | isl_basic_set_free(bset); |
| 2928 | return NULL; |
| 2929 | } |
| 2930 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 2931 | isl_bool isl_basic_map_contains(__isl_keep isl_basic_map *bmap, |
| 2932 | __isl_keep isl_vec *vec) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 2933 | { |
| 2934 | int i; |
| 2935 | unsigned total; |
| 2936 | isl_int s; |
| 2937 | |
| 2938 | if (!bmap || !vec) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 2939 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 2940 | |
| 2941 | total = 1 + isl_basic_map_total_dim(bmap); |
| 2942 | if (total != vec->size) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 2943 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 2944 | |
| 2945 | isl_int_init(s); |
| 2946 | |
| 2947 | for (i = 0; i < bmap->n_eq; ++i) { |
| 2948 | isl_seq_inner_product(vec->el, bmap->eq[i], total, &s); |
| 2949 | if (!isl_int_is_zero(s)) { |
| 2950 | isl_int_clear(s); |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 2951 | return isl_bool_false; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 2952 | } |
| 2953 | } |
| 2954 | |
| 2955 | for (i = 0; i < bmap->n_ineq; ++i) { |
| 2956 | isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s); |
| 2957 | if (isl_int_is_neg(s)) { |
| 2958 | isl_int_clear(s); |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 2959 | return isl_bool_false; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 2960 | } |
| 2961 | } |
| 2962 | |
| 2963 | isl_int_clear(s); |
| 2964 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 2965 | return isl_bool_true; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 2966 | } |
| 2967 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 2968 | isl_bool isl_basic_set_contains(__isl_keep isl_basic_set *bset, |
| 2969 | __isl_keep isl_vec *vec) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 2970 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 2971 | return isl_basic_map_contains(bset_to_bmap(bset), vec); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 2972 | } |
| 2973 | |
| 2974 | struct isl_basic_map *isl_basic_map_intersect( |
| 2975 | struct isl_basic_map *bmap1, struct isl_basic_map *bmap2) |
| 2976 | { |
| 2977 | struct isl_vec *sample = NULL; |
| 2978 | |
| 2979 | if (!bmap1 || !bmap2) |
| 2980 | goto error; |
| 2981 | |
| 2982 | isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param, |
| 2983 | bmap2->dim, isl_dim_param), goto error); |
| 2984 | if (isl_space_dim(bmap1->dim, isl_dim_all) == |
| 2985 | isl_space_dim(bmap1->dim, isl_dim_param) && |
| 2986 | isl_space_dim(bmap2->dim, isl_dim_all) != |
| 2987 | isl_space_dim(bmap2->dim, isl_dim_param)) |
| 2988 | return isl_basic_map_intersect(bmap2, bmap1); |
| 2989 | |
| 2990 | if (isl_space_dim(bmap2->dim, isl_dim_all) != |
| 2991 | isl_space_dim(bmap2->dim, isl_dim_param)) |
| 2992 | isl_assert(bmap1->ctx, |
| 2993 | isl_space_is_equal(bmap1->dim, bmap2->dim), goto error); |
| 2994 | |
Tobias Grosser | fb3fb0a | 2015-11-21 20:48:39 +0000 | [diff] [blame] | 2995 | if (isl_basic_map_plain_is_empty(bmap1)) { |
| 2996 | isl_basic_map_free(bmap2); |
| 2997 | return bmap1; |
| 2998 | } |
| 2999 | if (isl_basic_map_plain_is_empty(bmap2)) { |
| 3000 | isl_basic_map_free(bmap1); |
| 3001 | return bmap2; |
| 3002 | } |
| 3003 | |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 3004 | if (bmap1->sample && |
| 3005 | isl_basic_map_contains(bmap1, bmap1->sample) > 0 && |
| 3006 | isl_basic_map_contains(bmap2, bmap1->sample) > 0) |
| 3007 | sample = isl_vec_copy(bmap1->sample); |
| 3008 | else if (bmap2->sample && |
| 3009 | isl_basic_map_contains(bmap1, bmap2->sample) > 0 && |
| 3010 | isl_basic_map_contains(bmap2, bmap2->sample) > 0) |
| 3011 | sample = isl_vec_copy(bmap2->sample); |
| 3012 | |
| 3013 | bmap1 = isl_basic_map_cow(bmap1); |
| 3014 | if (!bmap1) |
| 3015 | goto error; |
| 3016 | bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim), |
| 3017 | bmap2->n_div, bmap2->n_eq, bmap2->n_ineq); |
| 3018 | bmap1 = add_constraints(bmap1, bmap2, 0, 0); |
| 3019 | |
| 3020 | if (!bmap1) |
| 3021 | isl_vec_free(sample); |
| 3022 | else if (sample) { |
| 3023 | isl_vec_free(bmap1->sample); |
| 3024 | bmap1->sample = sample; |
| 3025 | } |
| 3026 | |
| 3027 | bmap1 = isl_basic_map_simplify(bmap1); |
| 3028 | return isl_basic_map_finalize(bmap1); |
| 3029 | error: |
| 3030 | if (sample) |
| 3031 | isl_vec_free(sample); |
| 3032 | isl_basic_map_free(bmap1); |
| 3033 | isl_basic_map_free(bmap2); |
| 3034 | return NULL; |
| 3035 | } |
| 3036 | |
| 3037 | struct isl_basic_set *isl_basic_set_intersect( |
| 3038 | struct isl_basic_set *bset1, struct isl_basic_set *bset2) |
| 3039 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 3040 | return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1), |
| 3041 | bset_to_bmap(bset2))); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 3042 | } |
| 3043 | |
| 3044 | __isl_give isl_basic_set *isl_basic_set_intersect_params( |
| 3045 | __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2) |
| 3046 | { |
| 3047 | return isl_basic_set_intersect(bset1, bset2); |
| 3048 | } |
| 3049 | |
| 3050 | /* Special case of isl_map_intersect, where both map1 and map2 |
| 3051 | * are convex, without any divs and such that either map1 or map2 |
| 3052 | * contains a single constraint. This constraint is then simply |
| 3053 | * added to the other map. |
| 3054 | */ |
| 3055 | static __isl_give isl_map *map_intersect_add_constraint( |
| 3056 | __isl_take isl_map *map1, __isl_take isl_map *map2) |
| 3057 | { |
| 3058 | isl_assert(map1->ctx, map1->n == 1, goto error); |
| 3059 | isl_assert(map2->ctx, map1->n == 1, goto error); |
| 3060 | isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error); |
| 3061 | isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error); |
| 3062 | |
| 3063 | if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1) |
| 3064 | return isl_map_intersect(map2, map1); |
| 3065 | |
| 3066 | isl_assert(map2->ctx, |
| 3067 | map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error); |
| 3068 | |
| 3069 | map1 = isl_map_cow(map1); |
| 3070 | if (!map1) |
| 3071 | goto error; |
| 3072 | if (isl_map_plain_is_empty(map1)) { |
| 3073 | isl_map_free(map2); |
| 3074 | return map1; |
| 3075 | } |
| 3076 | map1->p[0] = isl_basic_map_cow(map1->p[0]); |
| 3077 | if (map2->p[0]->n_eq == 1) |
| 3078 | map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]); |
| 3079 | else |
| 3080 | map1->p[0] = isl_basic_map_add_ineq(map1->p[0], |
| 3081 | map2->p[0]->ineq[0]); |
| 3082 | |
| 3083 | map1->p[0] = isl_basic_map_simplify(map1->p[0]); |
| 3084 | map1->p[0] = isl_basic_map_finalize(map1->p[0]); |
| 3085 | if (!map1->p[0]) |
| 3086 | goto error; |
| 3087 | |
| 3088 | if (isl_basic_map_plain_is_empty(map1->p[0])) { |
| 3089 | isl_basic_map_free(map1->p[0]); |
| 3090 | map1->n = 0; |
| 3091 | } |
| 3092 | |
| 3093 | isl_map_free(map2); |
| 3094 | |
| 3095 | return map1; |
| 3096 | error: |
| 3097 | isl_map_free(map1); |
| 3098 | isl_map_free(map2); |
| 3099 | return NULL; |
| 3100 | } |
| 3101 | |
| 3102 | /* map2 may be either a parameter domain or a map living in the same |
| 3103 | * space as map1. |
| 3104 | */ |
| 3105 | static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1, |
| 3106 | __isl_take isl_map *map2) |
| 3107 | { |
| 3108 | unsigned flags = 0; |
| 3109 | isl_map *result; |
| 3110 | int i, j; |
| 3111 | |
| 3112 | if (!map1 || !map2) |
| 3113 | goto error; |
| 3114 | |
| 3115 | if ((isl_map_plain_is_empty(map1) || |
| 3116 | isl_map_plain_is_universe(map2)) && |
| 3117 | isl_space_is_equal(map1->dim, map2->dim)) { |
| 3118 | isl_map_free(map2); |
| 3119 | return map1; |
| 3120 | } |
| 3121 | if ((isl_map_plain_is_empty(map2) || |
| 3122 | isl_map_plain_is_universe(map1)) && |
| 3123 | isl_space_is_equal(map1->dim, map2->dim)) { |
| 3124 | isl_map_free(map1); |
| 3125 | return map2; |
| 3126 | } |
| 3127 | |
| 3128 | if (map1->n == 1 && map2->n == 1 && |
| 3129 | map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 && |
| 3130 | isl_space_is_equal(map1->dim, map2->dim) && |
| 3131 | (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 || |
| 3132 | map2->p[0]->n_eq + map2->p[0]->n_ineq == 1)) |
| 3133 | return map_intersect_add_constraint(map1, map2); |
| 3134 | |
| 3135 | if (isl_space_dim(map2->dim, isl_dim_all) != |
| 3136 | isl_space_dim(map2->dim, isl_dim_param)) |
| 3137 | isl_assert(map1->ctx, |
| 3138 | isl_space_is_equal(map1->dim, map2->dim), goto error); |
| 3139 | |
| 3140 | if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) && |
| 3141 | ISL_F_ISSET(map2, ISL_MAP_DISJOINT)) |
| 3142 | ISL_FL_SET(flags, ISL_MAP_DISJOINT); |
| 3143 | |
| 3144 | result = isl_map_alloc_space(isl_space_copy(map1->dim), |
| 3145 | map1->n * map2->n, flags); |
| 3146 | if (!result) |
| 3147 | goto error; |
| 3148 | for (i = 0; i < map1->n; ++i) |
| 3149 | for (j = 0; j < map2->n; ++j) { |
| 3150 | struct isl_basic_map *part; |
| 3151 | part = isl_basic_map_intersect( |
| 3152 | isl_basic_map_copy(map1->p[i]), |
| 3153 | isl_basic_map_copy(map2->p[j])); |
| 3154 | if (isl_basic_map_is_empty(part) < 0) |
| 3155 | part = isl_basic_map_free(part); |
| 3156 | result = isl_map_add_basic_map(result, part); |
| 3157 | if (!result) |
| 3158 | goto error; |
| 3159 | } |
| 3160 | isl_map_free(map1); |
| 3161 | isl_map_free(map2); |
| 3162 | return result; |
| 3163 | error: |
| 3164 | isl_map_free(map1); |
| 3165 | isl_map_free(map2); |
| 3166 | return NULL; |
| 3167 | } |
| 3168 | |
| 3169 | static __isl_give isl_map *map_intersect(__isl_take isl_map *map1, |
| 3170 | __isl_take isl_map *map2) |
| 3171 | { |
| 3172 | if (!map1 || !map2) |
| 3173 | goto error; |
| 3174 | if (!isl_space_is_equal(map1->dim, map2->dim)) |
| 3175 | isl_die(isl_map_get_ctx(map1), isl_error_invalid, |
| 3176 | "spaces don't match", goto error); |
| 3177 | return map_intersect_internal(map1, map2); |
| 3178 | error: |
| 3179 | isl_map_free(map1); |
| 3180 | isl_map_free(map2); |
| 3181 | return NULL; |
| 3182 | } |
| 3183 | |
| 3184 | __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1, |
| 3185 | __isl_take isl_map *map2) |
| 3186 | { |
| 3187 | return isl_map_align_params_map_map_and(map1, map2, &map_intersect); |
| 3188 | } |
| 3189 | |
| 3190 | struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2) |
| 3191 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 3192 | return set_from_map(isl_map_intersect(set_to_map(set1), |
| 3193 | set_to_map(set2))); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 3194 | } |
| 3195 | |
| 3196 | /* map_intersect_internal accepts intersections |
| 3197 | * with parameter domains, so we can just call that function. |
| 3198 | */ |
| 3199 | static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map, |
| 3200 | __isl_take isl_set *params) |
| 3201 | { |
| 3202 | return map_intersect_internal(map, params); |
| 3203 | } |
| 3204 | |
| 3205 | __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1, |
| 3206 | __isl_take isl_map *map2) |
| 3207 | { |
| 3208 | return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params); |
| 3209 | } |
| 3210 | |
| 3211 | __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set, |
| 3212 | __isl_take isl_set *params) |
| 3213 | { |
| 3214 | return isl_map_intersect_params(set, params); |
| 3215 | } |
| 3216 | |
| 3217 | struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap) |
| 3218 | { |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 3219 | isl_space *space; |
| 3220 | unsigned pos, n1, n2; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 3221 | |
| 3222 | if (!bmap) |
| 3223 | return NULL; |
| 3224 | bmap = isl_basic_map_cow(bmap); |
| 3225 | if (!bmap) |
| 3226 | return NULL; |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 3227 | space = isl_space_reverse(isl_space_copy(bmap->dim)); |
| 3228 | pos = isl_basic_map_offset(bmap, isl_dim_in); |
| 3229 | n1 = isl_basic_map_dim(bmap, isl_dim_in); |
| 3230 | n2 = isl_basic_map_dim(bmap, isl_dim_out); |
| 3231 | bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2); |
| 3232 | return isl_basic_map_reset_space(bmap, space); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 3233 | } |
| 3234 | |
| 3235 | static __isl_give isl_basic_map *basic_map_space_reset( |
| 3236 | __isl_take isl_basic_map *bmap, enum isl_dim_type type) |
| 3237 | { |
| 3238 | isl_space *space; |
| 3239 | |
| 3240 | if (!bmap) |
| 3241 | return NULL; |
| 3242 | if (!isl_space_is_named_or_nested(bmap->dim, type)) |
| 3243 | return bmap; |
| 3244 | |
| 3245 | space = isl_basic_map_get_space(bmap); |
| 3246 | space = isl_space_reset(space, type); |
| 3247 | bmap = isl_basic_map_reset_space(bmap, space); |
| 3248 | return bmap; |
| 3249 | } |
| 3250 | |
| 3251 | __isl_give isl_basic_map *isl_basic_map_insert_dims( |
| 3252 | __isl_take isl_basic_map *bmap, enum isl_dim_type type, |
| 3253 | unsigned pos, unsigned n) |
| 3254 | { |
| 3255 | isl_space *res_dim; |
| 3256 | struct isl_basic_map *res; |
| 3257 | struct isl_dim_map *dim_map; |
| 3258 | unsigned total, off; |
| 3259 | enum isl_dim_type t; |
| 3260 | |
| 3261 | if (n == 0) |
| 3262 | return basic_map_space_reset(bmap, type); |
| 3263 | |
| 3264 | if (!bmap) |
| 3265 | return NULL; |
| 3266 | |
| 3267 | res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n); |
| 3268 | |
| 3269 | total = isl_basic_map_total_dim(bmap) + n; |
| 3270 | dim_map = isl_dim_map_alloc(bmap->ctx, total); |
| 3271 | off = 0; |
| 3272 | for (t = isl_dim_param; t <= isl_dim_out; ++t) { |
| 3273 | if (t != type) { |
| 3274 | isl_dim_map_dim(dim_map, bmap->dim, t, off); |
| 3275 | } else { |
| 3276 | unsigned size = isl_basic_map_dim(bmap, t); |
| 3277 | isl_dim_map_dim_range(dim_map, bmap->dim, t, |
| 3278 | 0, pos, off); |
| 3279 | isl_dim_map_dim_range(dim_map, bmap->dim, t, |
| 3280 | pos, size - pos, off + pos + n); |
| 3281 | } |
| 3282 | off += isl_space_dim(res_dim, t); |
| 3283 | } |
| 3284 | isl_dim_map_div(dim_map, bmap, off); |
| 3285 | |
| 3286 | res = isl_basic_map_alloc_space(res_dim, |
| 3287 | bmap->n_div, bmap->n_eq, bmap->n_ineq); |
| 3288 | if (isl_basic_map_is_rational(bmap)) |
| 3289 | res = isl_basic_map_set_rational(res); |
| 3290 | if (isl_basic_map_plain_is_empty(bmap)) { |
| 3291 | isl_basic_map_free(bmap); |
| 3292 | free(dim_map); |
| 3293 | return isl_basic_map_set_to_empty(res); |
| 3294 | } |
| 3295 | res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map); |
| 3296 | return isl_basic_map_finalize(res); |
| 3297 | } |
| 3298 | |
| 3299 | __isl_give isl_basic_set *isl_basic_set_insert_dims( |
| 3300 | __isl_take isl_basic_set *bset, |
| 3301 | enum isl_dim_type type, unsigned pos, unsigned n) |
| 3302 | { |
| 3303 | return isl_basic_map_insert_dims(bset, type, pos, n); |
| 3304 | } |
| 3305 | |
Tobias Grosser | e0f8d59 | 2015-05-13 13:10:13 +0000 | [diff] [blame] | 3306 | __isl_give isl_basic_map *isl_basic_map_add_dims(__isl_take isl_basic_map *bmap, |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 3307 | enum isl_dim_type type, unsigned n) |
| 3308 | { |
| 3309 | if (!bmap) |
| 3310 | return NULL; |
| 3311 | return isl_basic_map_insert_dims(bmap, type, |
| 3312 | isl_basic_map_dim(bmap, type), n); |
| 3313 | } |
| 3314 | |
| 3315 | __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset, |
| 3316 | enum isl_dim_type type, unsigned n) |
| 3317 | { |
| 3318 | if (!bset) |
| 3319 | return NULL; |
| 3320 | isl_assert(bset->ctx, type != isl_dim_in, goto error); |
Tobias Grosser | e0f8d59 | 2015-05-13 13:10:13 +0000 | [diff] [blame] | 3321 | return isl_basic_map_add_dims(bset, type, n); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 3322 | error: |
| 3323 | isl_basic_set_free(bset); |
| 3324 | return NULL; |
| 3325 | } |
| 3326 | |
| 3327 | static __isl_give isl_map *map_space_reset(__isl_take isl_map *map, |
| 3328 | enum isl_dim_type type) |
| 3329 | { |
| 3330 | isl_space *space; |
| 3331 | |
| 3332 | if (!map || !isl_space_is_named_or_nested(map->dim, type)) |
| 3333 | return map; |
| 3334 | |
| 3335 | space = isl_map_get_space(map); |
| 3336 | space = isl_space_reset(space, type); |
| 3337 | map = isl_map_reset_space(map, space); |
| 3338 | return map; |
| 3339 | } |
| 3340 | |
| 3341 | __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map, |
| 3342 | enum isl_dim_type type, unsigned pos, unsigned n) |
| 3343 | { |
| 3344 | int i; |
| 3345 | |
| 3346 | if (n == 0) |
| 3347 | return map_space_reset(map, type); |
| 3348 | |
| 3349 | map = isl_map_cow(map); |
| 3350 | if (!map) |
| 3351 | return NULL; |
| 3352 | |
| 3353 | map->dim = isl_space_insert_dims(map->dim, type, pos, n); |
| 3354 | if (!map->dim) |
| 3355 | goto error; |
| 3356 | |
| 3357 | for (i = 0; i < map->n; ++i) { |
| 3358 | map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n); |
| 3359 | if (!map->p[i]) |
| 3360 | goto error; |
| 3361 | } |
| 3362 | |
| 3363 | return map; |
| 3364 | error: |
| 3365 | isl_map_free(map); |
| 3366 | return NULL; |
| 3367 | } |
| 3368 | |
| 3369 | __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set, |
| 3370 | enum isl_dim_type type, unsigned pos, unsigned n) |
| 3371 | { |
| 3372 | return isl_map_insert_dims(set, type, pos, n); |
| 3373 | } |
| 3374 | |
| 3375 | __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map, |
| 3376 | enum isl_dim_type type, unsigned n) |
| 3377 | { |
| 3378 | if (!map) |
| 3379 | return NULL; |
| 3380 | return isl_map_insert_dims(map, type, isl_map_dim(map, type), n); |
| 3381 | } |
| 3382 | |
| 3383 | __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set, |
| 3384 | enum isl_dim_type type, unsigned n) |
| 3385 | { |
| 3386 | if (!set) |
| 3387 | return NULL; |
| 3388 | isl_assert(set->ctx, type != isl_dim_in, goto error); |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 3389 | return set_from_map(isl_map_add_dims(set_to_map(set), type, n)); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 3390 | error: |
| 3391 | isl_set_free(set); |
| 3392 | return NULL; |
| 3393 | } |
| 3394 | |
| 3395 | __isl_give isl_basic_map *isl_basic_map_move_dims( |
| 3396 | __isl_take isl_basic_map *bmap, |
| 3397 | enum isl_dim_type dst_type, unsigned dst_pos, |
| 3398 | enum isl_dim_type src_type, unsigned src_pos, unsigned n) |
| 3399 | { |
| 3400 | struct isl_dim_map *dim_map; |
| 3401 | struct isl_basic_map *res; |
| 3402 | enum isl_dim_type t; |
| 3403 | unsigned total, off; |
| 3404 | |
| 3405 | if (!bmap) |
| 3406 | return NULL; |
| 3407 | if (n == 0) |
| 3408 | return bmap; |
| 3409 | |
| 3410 | isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type), |
| 3411 | goto error); |
| 3412 | |
| 3413 | if (dst_type == src_type && dst_pos == src_pos) |
| 3414 | return bmap; |
| 3415 | |
| 3416 | isl_assert(bmap->ctx, dst_type != src_type, goto error); |
| 3417 | |
| 3418 | if (pos(bmap->dim, dst_type) + dst_pos == |
| 3419 | pos(bmap->dim, src_type) + src_pos + |
| 3420 | ((src_type < dst_type) ? n : 0)) { |
| 3421 | bmap = isl_basic_map_cow(bmap); |
| 3422 | if (!bmap) |
| 3423 | return NULL; |
| 3424 | |
| 3425 | bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos, |
| 3426 | src_type, src_pos, n); |
| 3427 | if (!bmap->dim) |
| 3428 | goto error; |
| 3429 | |
| 3430 | bmap = isl_basic_map_finalize(bmap); |
| 3431 | |
| 3432 | return bmap; |
| 3433 | } |
| 3434 | |
| 3435 | total = isl_basic_map_total_dim(bmap); |
| 3436 | dim_map = isl_dim_map_alloc(bmap->ctx, total); |
| 3437 | |
| 3438 | off = 0; |
| 3439 | for (t = isl_dim_param; t <= isl_dim_out; ++t) { |
| 3440 | unsigned size = isl_space_dim(bmap->dim, t); |
| 3441 | if (t == dst_type) { |
| 3442 | isl_dim_map_dim_range(dim_map, bmap->dim, t, |
| 3443 | 0, dst_pos, off); |
| 3444 | off += dst_pos; |
| 3445 | isl_dim_map_dim_range(dim_map, bmap->dim, src_type, |
| 3446 | src_pos, n, off); |
| 3447 | off += n; |
| 3448 | isl_dim_map_dim_range(dim_map, bmap->dim, t, |
| 3449 | dst_pos, size - dst_pos, off); |
| 3450 | off += size - dst_pos; |
| 3451 | } else if (t == src_type) { |
| 3452 | isl_dim_map_dim_range(dim_map, bmap->dim, t, |
| 3453 | 0, src_pos, off); |
| 3454 | off += src_pos; |
| 3455 | isl_dim_map_dim_range(dim_map, bmap->dim, t, |
| 3456 | src_pos + n, size - src_pos - n, off); |
| 3457 | off += size - src_pos - n; |
| 3458 | } else { |
| 3459 | isl_dim_map_dim(dim_map, bmap->dim, t, off); |
| 3460 | off += size; |
| 3461 | } |
| 3462 | } |
| 3463 | isl_dim_map_div(dim_map, bmap, off); |
| 3464 | |
| 3465 | res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap), |
| 3466 | bmap->n_div, bmap->n_eq, bmap->n_ineq); |
| 3467 | bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map); |
| 3468 | if (!bmap) |
| 3469 | goto error; |
| 3470 | |
| 3471 | bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos, |
| 3472 | src_type, src_pos, n); |
| 3473 | if (!bmap->dim) |
| 3474 | goto error; |
| 3475 | |
| 3476 | ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED); |
| 3477 | bmap = isl_basic_map_gauss(bmap, NULL); |
| 3478 | bmap = isl_basic_map_finalize(bmap); |
| 3479 | |
| 3480 | return bmap; |
| 3481 | error: |
| 3482 | isl_basic_map_free(bmap); |
| 3483 | return NULL; |
| 3484 | } |
| 3485 | |
| 3486 | __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset, |
| 3487 | enum isl_dim_type dst_type, unsigned dst_pos, |
| 3488 | enum isl_dim_type src_type, unsigned src_pos, unsigned n) |
| 3489 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 3490 | isl_basic_map *bmap = bset_to_bmap(bset); |
| 3491 | bmap = isl_basic_map_move_dims(bmap, dst_type, dst_pos, |
| 3492 | src_type, src_pos, n); |
| 3493 | return bset_from_bmap(bmap); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 3494 | } |
| 3495 | |
| 3496 | __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set, |
| 3497 | enum isl_dim_type dst_type, unsigned dst_pos, |
| 3498 | enum isl_dim_type src_type, unsigned src_pos, unsigned n) |
| 3499 | { |
| 3500 | if (!set) |
| 3501 | return NULL; |
| 3502 | isl_assert(set->ctx, dst_type != isl_dim_in, goto error); |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 3503 | return set_from_map(isl_map_move_dims(set_to_map(set), |
| 3504 | dst_type, dst_pos, src_type, src_pos, n)); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 3505 | error: |
| 3506 | isl_set_free(set); |
| 3507 | return NULL; |
| 3508 | } |
| 3509 | |
| 3510 | __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map, |
| 3511 | enum isl_dim_type dst_type, unsigned dst_pos, |
| 3512 | enum isl_dim_type src_type, unsigned src_pos, unsigned n) |
| 3513 | { |
| 3514 | int i; |
| 3515 | |
| 3516 | if (!map) |
| 3517 | return NULL; |
| 3518 | if (n == 0) |
| 3519 | return map; |
| 3520 | |
| 3521 | isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type), |
| 3522 | goto error); |
| 3523 | |
| 3524 | if (dst_type == src_type && dst_pos == src_pos) |
| 3525 | return map; |
| 3526 | |
| 3527 | isl_assert(map->ctx, dst_type != src_type, goto error); |
| 3528 | |
| 3529 | map = isl_map_cow(map); |
| 3530 | if (!map) |
| 3531 | return NULL; |
| 3532 | |
| 3533 | map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n); |
| 3534 | if (!map->dim) |
| 3535 | goto error; |
| 3536 | |
| 3537 | for (i = 0; i < map->n; ++i) { |
| 3538 | map->p[i] = isl_basic_map_move_dims(map->p[i], |
| 3539 | dst_type, dst_pos, |
| 3540 | src_type, src_pos, n); |
| 3541 | if (!map->p[i]) |
| 3542 | goto error; |
| 3543 | } |
| 3544 | |
| 3545 | return map; |
| 3546 | error: |
| 3547 | isl_map_free(map); |
| 3548 | return NULL; |
| 3549 | } |
| 3550 | |
| 3551 | /* Move the specified dimensions to the last columns right before |
| 3552 | * the divs. Don't change the dimension specification of bmap. |
| 3553 | * That's the responsibility of the caller. |
| 3554 | */ |
| 3555 | static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap, |
| 3556 | enum isl_dim_type type, unsigned first, unsigned n) |
| 3557 | { |
| 3558 | struct isl_dim_map *dim_map; |
| 3559 | struct isl_basic_map *res; |
| 3560 | enum isl_dim_type t; |
| 3561 | unsigned total, off; |
| 3562 | |
| 3563 | if (!bmap) |
| 3564 | return NULL; |
| 3565 | if (pos(bmap->dim, type) + first + n == |
| 3566 | 1 + isl_space_dim(bmap->dim, isl_dim_all)) |
| 3567 | return bmap; |
| 3568 | |
| 3569 | total = isl_basic_map_total_dim(bmap); |
| 3570 | dim_map = isl_dim_map_alloc(bmap->ctx, total); |
| 3571 | |
| 3572 | off = 0; |
| 3573 | for (t = isl_dim_param; t <= isl_dim_out; ++t) { |
| 3574 | unsigned size = isl_space_dim(bmap->dim, t); |
| 3575 | if (t == type) { |
| 3576 | isl_dim_map_dim_range(dim_map, bmap->dim, t, |
| 3577 | 0, first, off); |
| 3578 | off += first; |
| 3579 | isl_dim_map_dim_range(dim_map, bmap->dim, t, |
| 3580 | first, n, total - bmap->n_div - n); |
| 3581 | isl_dim_map_dim_range(dim_map, bmap->dim, t, |
| 3582 | first + n, size - (first + n), off); |
| 3583 | off += size - (first + n); |
| 3584 | } else { |
| 3585 | isl_dim_map_dim(dim_map, bmap->dim, t, off); |
| 3586 | off += size; |
| 3587 | } |
| 3588 | } |
| 3589 | isl_dim_map_div(dim_map, bmap, off + n); |
| 3590 | |
| 3591 | res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap), |
| 3592 | bmap->n_div, bmap->n_eq, bmap->n_ineq); |
| 3593 | res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map); |
| 3594 | return res; |
| 3595 | } |
| 3596 | |
| 3597 | /* Insert "n" rows in the divs of "bmap". |
| 3598 | * |
| 3599 | * The number of columns is not changed, which means that the last |
| 3600 | * dimensions of "bmap" are being reintepreted as the new divs. |
| 3601 | * The space of "bmap" is not adjusted, however, which means |
| 3602 | * that "bmap" is left in an inconsistent state. Removing "n" dimensions |
| 3603 | * from the space of "bmap" is the responsibility of the caller. |
| 3604 | */ |
| 3605 | static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap, |
| 3606 | int n) |
| 3607 | { |
| 3608 | int i; |
| 3609 | size_t row_size; |
| 3610 | isl_int **new_div; |
| 3611 | isl_int *old; |
| 3612 | |
| 3613 | bmap = isl_basic_map_cow(bmap); |
| 3614 | if (!bmap) |
| 3615 | return NULL; |
| 3616 | |
| 3617 | row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra; |
| 3618 | old = bmap->block2.data; |
| 3619 | bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2, |
| 3620 | (bmap->extra + n) * (1 + row_size)); |
| 3621 | if (!bmap->block2.data) |
| 3622 | return isl_basic_map_free(bmap); |
| 3623 | new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n); |
| 3624 | if (!new_div) |
| 3625 | return isl_basic_map_free(bmap); |
| 3626 | for (i = 0; i < n; ++i) { |
| 3627 | new_div[i] = bmap->block2.data + |
| 3628 | (bmap->extra + i) * (1 + row_size); |
| 3629 | isl_seq_clr(new_div[i], 1 + row_size); |
| 3630 | } |
| 3631 | for (i = 0; i < bmap->extra; ++i) |
| 3632 | new_div[n + i] = bmap->block2.data + (bmap->div[i] - old); |
| 3633 | free(bmap->div); |
| 3634 | bmap->div = new_div; |
| 3635 | bmap->n_div += n; |
| 3636 | bmap->extra += n; |
| 3637 | |
| 3638 | return bmap; |
| 3639 | } |
| 3640 | |
Tobias Grosser | a67ac97 | 2016-02-26 11:35:12 +0000 | [diff] [blame] | 3641 | /* Drop constraints from "bmap" that only involve the variables |
| 3642 | * of "type" in the range [first, first + n] that are not related |
| 3643 | * to any of the variables outside that interval. |
| 3644 | * These constraints cannot influence the values for the variables |
| 3645 | * outside the interval, except in case they cause "bmap" to be empty. |
| 3646 | * Only drop the constraints if "bmap" is known to be non-empty. |
| 3647 | */ |
| 3648 | static __isl_give isl_basic_map *drop_irrelevant_constraints( |
| 3649 | __isl_take isl_basic_map *bmap, enum isl_dim_type type, |
| 3650 | unsigned first, unsigned n) |
| 3651 | { |
| 3652 | int i; |
| 3653 | int *groups; |
| 3654 | unsigned dim, n_div; |
| 3655 | isl_bool non_empty; |
| 3656 | |
| 3657 | non_empty = isl_basic_map_plain_is_non_empty(bmap); |
| 3658 | if (non_empty < 0) |
| 3659 | return isl_basic_map_free(bmap); |
| 3660 | if (!non_empty) |
| 3661 | return bmap; |
| 3662 | |
| 3663 | dim = isl_basic_map_dim(bmap, isl_dim_all); |
| 3664 | n_div = isl_basic_map_dim(bmap, isl_dim_div); |
| 3665 | groups = isl_calloc_array(isl_basic_map_get_ctx(bmap), int, dim); |
| 3666 | if (!groups) |
| 3667 | return isl_basic_map_free(bmap); |
| 3668 | first += isl_basic_map_offset(bmap, type) - 1; |
| 3669 | for (i = 0; i < first; ++i) |
| 3670 | groups[i] = -1; |
| 3671 | for (i = first + n; i < dim - n_div; ++i) |
| 3672 | groups[i] = -1; |
| 3673 | |
| 3674 | bmap = isl_basic_map_drop_unrelated_constraints(bmap, groups); |
| 3675 | |
| 3676 | return bmap; |
| 3677 | } |
| 3678 | |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 3679 | /* Turn the n dimensions of type type, starting at first |
| 3680 | * into existentially quantified variables. |
Tobias Grosser | a67ac97 | 2016-02-26 11:35:12 +0000 | [diff] [blame] | 3681 | * |
| 3682 | * If a subset of the projected out variables are unrelated |
| 3683 | * to any of the variables that remain, then the constraints |
| 3684 | * involving this subset are simply dropped first. |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 3685 | */ |
| 3686 | __isl_give isl_basic_map *isl_basic_map_project_out( |
| 3687 | __isl_take isl_basic_map *bmap, |
| 3688 | enum isl_dim_type type, unsigned first, unsigned n) |
| 3689 | { |
| 3690 | if (n == 0) |
| 3691 | return basic_map_space_reset(bmap, type); |
Tobias Grosser | a67ac97 | 2016-02-26 11:35:12 +0000 | [diff] [blame] | 3692 | if (type == isl_dim_div) |
| 3693 | isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid, |
| 3694 | "cannot project out existentially quantified variables", |
| 3695 | return isl_basic_map_free(bmap)); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 3696 | |
Tobias Grosser | a67ac97 | 2016-02-26 11:35:12 +0000 | [diff] [blame] | 3697 | bmap = drop_irrelevant_constraints(bmap, type, first, n); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 3698 | if (!bmap) |
| 3699 | return NULL; |
| 3700 | |
| 3701 | if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) |
| 3702 | return isl_basic_map_remove_dims(bmap, type, first, n); |
| 3703 | |
| 3704 | isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type), |
| 3705 | goto error); |
| 3706 | |
| 3707 | bmap = move_last(bmap, type, first, n); |
| 3708 | bmap = isl_basic_map_cow(bmap); |
| 3709 | bmap = insert_div_rows(bmap, n); |
| 3710 | if (!bmap) |
| 3711 | return NULL; |
| 3712 | |
| 3713 | bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n); |
| 3714 | if (!bmap->dim) |
| 3715 | goto error; |
| 3716 | bmap = isl_basic_map_simplify(bmap); |
| 3717 | bmap = isl_basic_map_drop_redundant_divs(bmap); |
| 3718 | return isl_basic_map_finalize(bmap); |
| 3719 | error: |
| 3720 | isl_basic_map_free(bmap); |
| 3721 | return NULL; |
| 3722 | } |
| 3723 | |
| 3724 | /* Turn the n dimensions of type type, starting at first |
| 3725 | * into existentially quantified variables. |
| 3726 | */ |
| 3727 | struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset, |
| 3728 | enum isl_dim_type type, unsigned first, unsigned n) |
| 3729 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 3730 | return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset), |
| 3731 | type, first, n)); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 3732 | } |
| 3733 | |
| 3734 | /* Turn the n dimensions of type type, starting at first |
| 3735 | * into existentially quantified variables. |
| 3736 | */ |
| 3737 | __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map, |
| 3738 | enum isl_dim_type type, unsigned first, unsigned n) |
| 3739 | { |
| 3740 | int i; |
| 3741 | |
| 3742 | if (!map) |
| 3743 | return NULL; |
| 3744 | |
| 3745 | if (n == 0) |
| 3746 | return map_space_reset(map, type); |
| 3747 | |
| 3748 | isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error); |
| 3749 | |
| 3750 | map = isl_map_cow(map); |
| 3751 | if (!map) |
| 3752 | return NULL; |
| 3753 | |
| 3754 | map->dim = isl_space_drop_dims(map->dim, type, first, n); |
| 3755 | if (!map->dim) |
| 3756 | goto error; |
| 3757 | |
| 3758 | for (i = 0; i < map->n; ++i) { |
| 3759 | map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n); |
| 3760 | if (!map->p[i]) |
| 3761 | goto error; |
| 3762 | } |
| 3763 | |
| 3764 | return map; |
| 3765 | error: |
| 3766 | isl_map_free(map); |
| 3767 | return NULL; |
| 3768 | } |
| 3769 | |
| 3770 | /* Turn the n dimensions of type type, starting at first |
| 3771 | * into existentially quantified variables. |
| 3772 | */ |
| 3773 | __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set, |
| 3774 | enum isl_dim_type type, unsigned first, unsigned n) |
| 3775 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 3776 | return set_from_map(isl_map_project_out(set_to_map(set), |
| 3777 | type, first, n)); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 3778 | } |
| 3779 | |
Michael Kruse | e0b34f3 | 2016-05-04 14:41:36 +0000 | [diff] [blame] | 3780 | /* Return a map that projects the elements in "set" onto their |
| 3781 | * "n" set dimensions starting at "first". |
| 3782 | * "type" should be equal to isl_dim_set. |
| 3783 | */ |
| 3784 | __isl_give isl_map *isl_set_project_onto_map(__isl_take isl_set *set, |
| 3785 | enum isl_dim_type type, unsigned first, unsigned n) |
| 3786 | { |
| 3787 | int i; |
| 3788 | int dim; |
| 3789 | isl_map *map; |
| 3790 | |
| 3791 | if (!set) |
| 3792 | return NULL; |
| 3793 | if (type != isl_dim_set) |
| 3794 | isl_die(isl_set_get_ctx(set), isl_error_invalid, |
| 3795 | "only set dimensions can be projected out", goto error); |
| 3796 | dim = isl_set_dim(set, isl_dim_set); |
| 3797 | if (first + n > dim || first + n < first) |
| 3798 | isl_die(isl_set_get_ctx(set), isl_error_invalid, |
| 3799 | "index out of bounds", goto error); |
| 3800 | |
| 3801 | map = isl_map_from_domain(set); |
| 3802 | map = isl_map_add_dims(map, isl_dim_out, n); |
| 3803 | for (i = 0; i < n; ++i) |
| 3804 | map = isl_map_equate(map, isl_dim_in, first + i, |
| 3805 | isl_dim_out, i); |
| 3806 | return map; |
| 3807 | error: |
| 3808 | isl_set_free(set); |
| 3809 | return NULL; |
| 3810 | } |
| 3811 | |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 3812 | static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n) |
| 3813 | { |
| 3814 | int i, j; |
| 3815 | |
| 3816 | for (i = 0; i < n; ++i) { |
| 3817 | j = isl_basic_map_alloc_div(bmap); |
| 3818 | if (j < 0) |
| 3819 | goto error; |
| 3820 | isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap)); |
| 3821 | } |
| 3822 | return bmap; |
| 3823 | error: |
| 3824 | isl_basic_map_free(bmap); |
| 3825 | return NULL; |
| 3826 | } |
| 3827 | |
| 3828 | struct isl_basic_map *isl_basic_map_apply_range( |
| 3829 | struct isl_basic_map *bmap1, struct isl_basic_map *bmap2) |
| 3830 | { |
| 3831 | isl_space *dim_result = NULL; |
| 3832 | struct isl_basic_map *bmap; |
| 3833 | unsigned n_in, n_out, n, nparam, total, pos; |
| 3834 | struct isl_dim_map *dim_map1, *dim_map2; |
| 3835 | |
| 3836 | if (!bmap1 || !bmap2) |
| 3837 | goto error; |
| 3838 | if (!isl_space_match(bmap1->dim, isl_dim_param, |
| 3839 | bmap2->dim, isl_dim_param)) |
| 3840 | isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid, |
| 3841 | "parameters don't match", goto error); |
| 3842 | if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_out, |
| 3843 | bmap2->dim, isl_dim_in)) |
| 3844 | isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid, |
| 3845 | "spaces don't match", goto error); |
| 3846 | |
| 3847 | dim_result = isl_space_join(isl_space_copy(bmap1->dim), |
| 3848 | isl_space_copy(bmap2->dim)); |
| 3849 | |
| 3850 | n_in = isl_basic_map_n_in(bmap1); |
| 3851 | n_out = isl_basic_map_n_out(bmap2); |
| 3852 | n = isl_basic_map_n_out(bmap1); |
| 3853 | nparam = isl_basic_map_n_param(bmap1); |
| 3854 | |
| 3855 | total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n; |
| 3856 | dim_map1 = isl_dim_map_alloc(bmap1->ctx, total); |
| 3857 | dim_map2 = isl_dim_map_alloc(bmap1->ctx, total); |
| 3858 | isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0); |
| 3859 | isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0); |
| 3860 | isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam); |
| 3861 | isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in); |
| 3862 | isl_dim_map_div(dim_map1, bmap1, pos += n_out); |
| 3863 | isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div); |
| 3864 | isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div); |
| 3865 | isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos); |
| 3866 | |
| 3867 | bmap = isl_basic_map_alloc_space(dim_result, |
| 3868 | bmap1->n_div + bmap2->n_div + n, |
| 3869 | bmap1->n_eq + bmap2->n_eq, |
| 3870 | bmap1->n_ineq + bmap2->n_ineq); |
| 3871 | bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1); |
| 3872 | bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2); |
| 3873 | bmap = add_divs(bmap, n); |
| 3874 | bmap = isl_basic_map_simplify(bmap); |
| 3875 | bmap = isl_basic_map_drop_redundant_divs(bmap); |
| 3876 | return isl_basic_map_finalize(bmap); |
| 3877 | error: |
| 3878 | isl_basic_map_free(bmap1); |
| 3879 | isl_basic_map_free(bmap2); |
| 3880 | return NULL; |
| 3881 | } |
| 3882 | |
| 3883 | struct isl_basic_set *isl_basic_set_apply( |
| 3884 | struct isl_basic_set *bset, struct isl_basic_map *bmap) |
| 3885 | { |
| 3886 | if (!bset || !bmap) |
| 3887 | goto error; |
| 3888 | |
| 3889 | isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset), |
| 3890 | goto error); |
| 3891 | |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 3892 | return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset), |
| 3893 | bmap)); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 3894 | error: |
| 3895 | isl_basic_set_free(bset); |
| 3896 | isl_basic_map_free(bmap); |
| 3897 | return NULL; |
| 3898 | } |
| 3899 | |
| 3900 | struct isl_basic_map *isl_basic_map_apply_domain( |
| 3901 | struct isl_basic_map *bmap1, struct isl_basic_map *bmap2) |
| 3902 | { |
| 3903 | if (!bmap1 || !bmap2) |
| 3904 | goto error; |
| 3905 | |
| 3906 | isl_assert(bmap1->ctx, |
| 3907 | isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error); |
| 3908 | isl_assert(bmap1->ctx, |
| 3909 | isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2), |
| 3910 | goto error); |
| 3911 | |
| 3912 | bmap1 = isl_basic_map_reverse(bmap1); |
| 3913 | bmap1 = isl_basic_map_apply_range(bmap1, bmap2); |
| 3914 | return isl_basic_map_reverse(bmap1); |
| 3915 | error: |
| 3916 | isl_basic_map_free(bmap1); |
| 3917 | isl_basic_map_free(bmap2); |
| 3918 | return NULL; |
| 3919 | } |
| 3920 | |
| 3921 | /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map |
| 3922 | * A \cap B -> f(A) + f(B) |
| 3923 | */ |
| 3924 | struct isl_basic_map *isl_basic_map_sum( |
| 3925 | struct isl_basic_map *bmap1, struct isl_basic_map *bmap2) |
| 3926 | { |
| 3927 | unsigned n_in, n_out, nparam, total, pos; |
| 3928 | struct isl_basic_map *bmap = NULL; |
| 3929 | struct isl_dim_map *dim_map1, *dim_map2; |
| 3930 | int i; |
| 3931 | |
| 3932 | if (!bmap1 || !bmap2) |
| 3933 | goto error; |
| 3934 | |
| 3935 | isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), |
| 3936 | goto error); |
| 3937 | |
| 3938 | nparam = isl_basic_map_n_param(bmap1); |
| 3939 | n_in = isl_basic_map_n_in(bmap1); |
| 3940 | n_out = isl_basic_map_n_out(bmap1); |
| 3941 | |
| 3942 | total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out; |
| 3943 | dim_map1 = isl_dim_map_alloc(bmap1->ctx, total); |
| 3944 | dim_map2 = isl_dim_map_alloc(bmap2->ctx, total); |
| 3945 | isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0); |
| 3946 | isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos); |
| 3947 | isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam); |
| 3948 | isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos); |
| 3949 | isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out); |
| 3950 | isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div); |
| 3951 | isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div); |
| 3952 | isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out); |
| 3953 | |
| 3954 | bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim), |
| 3955 | bmap1->n_div + bmap2->n_div + 2 * n_out, |
| 3956 | bmap1->n_eq + bmap2->n_eq + n_out, |
| 3957 | bmap1->n_ineq + bmap2->n_ineq); |
| 3958 | for (i = 0; i < n_out; ++i) { |
| 3959 | int j = isl_basic_map_alloc_equality(bmap); |
| 3960 | if (j < 0) |
| 3961 | goto error; |
| 3962 | isl_seq_clr(bmap->eq[j], 1+total); |
| 3963 | isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1); |
| 3964 | isl_int_set_si(bmap->eq[j][1+pos+i], 1); |
| 3965 | isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1); |
| 3966 | } |
| 3967 | bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1); |
| 3968 | bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2); |
| 3969 | bmap = add_divs(bmap, 2 * n_out); |
| 3970 | |
| 3971 | bmap = isl_basic_map_simplify(bmap); |
| 3972 | return isl_basic_map_finalize(bmap); |
| 3973 | error: |
| 3974 | isl_basic_map_free(bmap); |
| 3975 | isl_basic_map_free(bmap1); |
| 3976 | isl_basic_map_free(bmap2); |
| 3977 | return NULL; |
| 3978 | } |
| 3979 | |
| 3980 | /* Given two maps A -> f(A) and B -> g(B), construct a map |
| 3981 | * A \cap B -> f(A) + f(B) |
| 3982 | */ |
| 3983 | struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2) |
| 3984 | { |
| 3985 | struct isl_map *result; |
| 3986 | int i, j; |
| 3987 | |
| 3988 | if (!map1 || !map2) |
| 3989 | goto error; |
| 3990 | |
| 3991 | isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error); |
| 3992 | |
| 3993 | result = isl_map_alloc_space(isl_space_copy(map1->dim), |
| 3994 | map1->n * map2->n, 0); |
| 3995 | if (!result) |
| 3996 | goto error; |
| 3997 | for (i = 0; i < map1->n; ++i) |
| 3998 | for (j = 0; j < map2->n; ++j) { |
| 3999 | struct isl_basic_map *part; |
| 4000 | part = isl_basic_map_sum( |
| 4001 | isl_basic_map_copy(map1->p[i]), |
| 4002 | isl_basic_map_copy(map2->p[j])); |
| 4003 | if (isl_basic_map_is_empty(part)) |
| 4004 | isl_basic_map_free(part); |
| 4005 | else |
| 4006 | result = isl_map_add_basic_map(result, part); |
| 4007 | if (!result) |
| 4008 | goto error; |
| 4009 | } |
| 4010 | isl_map_free(map1); |
| 4011 | isl_map_free(map2); |
| 4012 | return result; |
| 4013 | error: |
| 4014 | isl_map_free(map1); |
| 4015 | isl_map_free(map2); |
| 4016 | return NULL; |
| 4017 | } |
| 4018 | |
| 4019 | __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1, |
| 4020 | __isl_take isl_set *set2) |
| 4021 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 4022 | return set_from_map(isl_map_sum(set_to_map(set1), set_to_map(set2))); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 4023 | } |
| 4024 | |
| 4025 | /* Given a basic map A -> f(A), construct A -> -f(A). |
| 4026 | */ |
| 4027 | struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap) |
| 4028 | { |
| 4029 | int i, j; |
| 4030 | unsigned off, n; |
| 4031 | |
| 4032 | bmap = isl_basic_map_cow(bmap); |
| 4033 | if (!bmap) |
| 4034 | return NULL; |
| 4035 | |
| 4036 | n = isl_basic_map_dim(bmap, isl_dim_out); |
| 4037 | off = isl_basic_map_offset(bmap, isl_dim_out); |
| 4038 | for (i = 0; i < bmap->n_eq; ++i) |
| 4039 | for (j = 0; j < n; ++j) |
| 4040 | isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]); |
| 4041 | for (i = 0; i < bmap->n_ineq; ++i) |
| 4042 | for (j = 0; j < n; ++j) |
| 4043 | isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]); |
| 4044 | for (i = 0; i < bmap->n_div; ++i) |
| 4045 | for (j = 0; j < n; ++j) |
| 4046 | isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]); |
| 4047 | bmap = isl_basic_map_gauss(bmap, NULL); |
| 4048 | return isl_basic_map_finalize(bmap); |
| 4049 | } |
| 4050 | |
| 4051 | __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset) |
| 4052 | { |
| 4053 | return isl_basic_map_neg(bset); |
| 4054 | } |
| 4055 | |
| 4056 | /* Given a map A -> f(A), construct A -> -f(A). |
| 4057 | */ |
| 4058 | struct isl_map *isl_map_neg(struct isl_map *map) |
| 4059 | { |
| 4060 | int i; |
| 4061 | |
| 4062 | map = isl_map_cow(map); |
| 4063 | if (!map) |
| 4064 | return NULL; |
| 4065 | |
| 4066 | for (i = 0; i < map->n; ++i) { |
| 4067 | map->p[i] = isl_basic_map_neg(map->p[i]); |
| 4068 | if (!map->p[i]) |
| 4069 | goto error; |
| 4070 | } |
| 4071 | |
| 4072 | return map; |
| 4073 | error: |
| 4074 | isl_map_free(map); |
| 4075 | return NULL; |
| 4076 | } |
| 4077 | |
| 4078 | __isl_give isl_set *isl_set_neg(__isl_take isl_set *set) |
| 4079 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 4080 | return set_from_map(isl_map_neg(set_to_map(set))); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 4081 | } |
| 4082 | |
| 4083 | /* Given a basic map A -> f(A) and an integer d, construct a basic map |
| 4084 | * A -> floor(f(A)/d). |
| 4085 | */ |
| 4086 | struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap, |
| 4087 | isl_int d) |
| 4088 | { |
| 4089 | unsigned n_in, n_out, nparam, total, pos; |
| 4090 | struct isl_basic_map *result = NULL; |
| 4091 | struct isl_dim_map *dim_map; |
| 4092 | int i; |
| 4093 | |
| 4094 | if (!bmap) |
| 4095 | return NULL; |
| 4096 | |
| 4097 | nparam = isl_basic_map_n_param(bmap); |
| 4098 | n_in = isl_basic_map_n_in(bmap); |
| 4099 | n_out = isl_basic_map_n_out(bmap); |
| 4100 | |
| 4101 | total = nparam + n_in + n_out + bmap->n_div + n_out; |
| 4102 | dim_map = isl_dim_map_alloc(bmap->ctx, total); |
| 4103 | isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0); |
| 4104 | isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam); |
| 4105 | isl_dim_map_div(dim_map, bmap, pos += n_in + n_out); |
| 4106 | isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div); |
| 4107 | |
| 4108 | result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim), |
| 4109 | bmap->n_div + n_out, |
| 4110 | bmap->n_eq, bmap->n_ineq + 2 * n_out); |
| 4111 | result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map); |
| 4112 | result = add_divs(result, n_out); |
| 4113 | for (i = 0; i < n_out; ++i) { |
| 4114 | int j; |
| 4115 | j = isl_basic_map_alloc_inequality(result); |
| 4116 | if (j < 0) |
| 4117 | goto error; |
| 4118 | isl_seq_clr(result->ineq[j], 1+total); |
| 4119 | isl_int_neg(result->ineq[j][1+nparam+n_in+i], d); |
| 4120 | isl_int_set_si(result->ineq[j][1+pos+i], 1); |
| 4121 | j = isl_basic_map_alloc_inequality(result); |
| 4122 | if (j < 0) |
| 4123 | goto error; |
| 4124 | isl_seq_clr(result->ineq[j], 1+total); |
| 4125 | isl_int_set(result->ineq[j][1+nparam+n_in+i], d); |
| 4126 | isl_int_set_si(result->ineq[j][1+pos+i], -1); |
| 4127 | isl_int_sub_ui(result->ineq[j][0], d, 1); |
| 4128 | } |
| 4129 | |
| 4130 | result = isl_basic_map_simplify(result); |
| 4131 | return isl_basic_map_finalize(result); |
| 4132 | error: |
| 4133 | isl_basic_map_free(result); |
| 4134 | return NULL; |
| 4135 | } |
| 4136 | |
| 4137 | /* Given a map A -> f(A) and an integer d, construct a map |
| 4138 | * A -> floor(f(A)/d). |
| 4139 | */ |
| 4140 | struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d) |
| 4141 | { |
| 4142 | int i; |
| 4143 | |
| 4144 | map = isl_map_cow(map); |
| 4145 | if (!map) |
| 4146 | return NULL; |
| 4147 | |
| 4148 | ISL_F_CLR(map, ISL_MAP_DISJOINT); |
| 4149 | ISL_F_CLR(map, ISL_MAP_NORMALIZED); |
| 4150 | for (i = 0; i < map->n; ++i) { |
| 4151 | map->p[i] = isl_basic_map_floordiv(map->p[i], d); |
| 4152 | if (!map->p[i]) |
| 4153 | goto error; |
| 4154 | } |
| 4155 | |
| 4156 | return map; |
| 4157 | error: |
| 4158 | isl_map_free(map); |
| 4159 | return NULL; |
| 4160 | } |
| 4161 | |
| 4162 | /* Given a map A -> f(A) and an integer d, construct a map |
| 4163 | * A -> floor(f(A)/d). |
| 4164 | */ |
| 4165 | __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map, |
| 4166 | __isl_take isl_val *d) |
| 4167 | { |
| 4168 | if (!map || !d) |
| 4169 | goto error; |
| 4170 | if (!isl_val_is_int(d)) |
| 4171 | isl_die(isl_val_get_ctx(d), isl_error_invalid, |
| 4172 | "expecting integer denominator", goto error); |
| 4173 | map = isl_map_floordiv(map, d->n); |
| 4174 | isl_val_free(d); |
| 4175 | return map; |
| 4176 | error: |
| 4177 | isl_map_free(map); |
| 4178 | isl_val_free(d); |
| 4179 | return NULL; |
| 4180 | } |
| 4181 | |
| 4182 | static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos) |
| 4183 | { |
| 4184 | int i; |
| 4185 | unsigned nparam; |
| 4186 | unsigned n_in; |
| 4187 | |
| 4188 | i = isl_basic_map_alloc_equality(bmap); |
| 4189 | if (i < 0) |
| 4190 | goto error; |
| 4191 | nparam = isl_basic_map_n_param(bmap); |
| 4192 | n_in = isl_basic_map_n_in(bmap); |
| 4193 | isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap)); |
| 4194 | isl_int_set_si(bmap->eq[i][1+nparam+pos], -1); |
| 4195 | isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1); |
| 4196 | return isl_basic_map_finalize(bmap); |
| 4197 | error: |
| 4198 | isl_basic_map_free(bmap); |
| 4199 | return NULL; |
| 4200 | } |
| 4201 | |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 4202 | /* Add a constraint to "bmap" expressing i_pos < o_pos |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 4203 | */ |
| 4204 | static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos) |
| 4205 | { |
| 4206 | int i; |
| 4207 | unsigned nparam; |
| 4208 | unsigned n_in; |
| 4209 | |
| 4210 | i = isl_basic_map_alloc_inequality(bmap); |
| 4211 | if (i < 0) |
| 4212 | goto error; |
| 4213 | nparam = isl_basic_map_n_param(bmap); |
| 4214 | n_in = isl_basic_map_n_in(bmap); |
| 4215 | isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap)); |
| 4216 | isl_int_set_si(bmap->ineq[i][0], -1); |
| 4217 | isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1); |
| 4218 | isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1); |
| 4219 | return isl_basic_map_finalize(bmap); |
| 4220 | error: |
| 4221 | isl_basic_map_free(bmap); |
| 4222 | return NULL; |
| 4223 | } |
| 4224 | |
| 4225 | /* Add a constraint to "bmap" expressing i_pos <= o_pos |
| 4226 | */ |
| 4227 | static __isl_give isl_basic_map *var_less_or_equal( |
| 4228 | __isl_take isl_basic_map *bmap, unsigned pos) |
| 4229 | { |
| 4230 | int i; |
| 4231 | unsigned nparam; |
| 4232 | unsigned n_in; |
| 4233 | |
| 4234 | i = isl_basic_map_alloc_inequality(bmap); |
| 4235 | if (i < 0) |
| 4236 | goto error; |
| 4237 | nparam = isl_basic_map_n_param(bmap); |
| 4238 | n_in = isl_basic_map_n_in(bmap); |
| 4239 | isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap)); |
| 4240 | isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1); |
| 4241 | isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1); |
| 4242 | return isl_basic_map_finalize(bmap); |
| 4243 | error: |
| 4244 | isl_basic_map_free(bmap); |
| 4245 | return NULL; |
| 4246 | } |
| 4247 | |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 4248 | /* Add a constraint to "bmap" expressing i_pos > o_pos |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 4249 | */ |
| 4250 | static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos) |
| 4251 | { |
| 4252 | int i; |
| 4253 | unsigned nparam; |
| 4254 | unsigned n_in; |
| 4255 | |
| 4256 | i = isl_basic_map_alloc_inequality(bmap); |
| 4257 | if (i < 0) |
| 4258 | goto error; |
| 4259 | nparam = isl_basic_map_n_param(bmap); |
| 4260 | n_in = isl_basic_map_n_in(bmap); |
| 4261 | isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap)); |
| 4262 | isl_int_set_si(bmap->ineq[i][0], -1); |
| 4263 | isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1); |
| 4264 | isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1); |
| 4265 | return isl_basic_map_finalize(bmap); |
| 4266 | error: |
| 4267 | isl_basic_map_free(bmap); |
| 4268 | return NULL; |
| 4269 | } |
| 4270 | |
| 4271 | /* Add a constraint to "bmap" expressing i_pos >= o_pos |
| 4272 | */ |
| 4273 | static __isl_give isl_basic_map *var_more_or_equal( |
| 4274 | __isl_take isl_basic_map *bmap, unsigned pos) |
| 4275 | { |
| 4276 | int i; |
| 4277 | unsigned nparam; |
| 4278 | unsigned n_in; |
| 4279 | |
| 4280 | i = isl_basic_map_alloc_inequality(bmap); |
| 4281 | if (i < 0) |
| 4282 | goto error; |
| 4283 | nparam = isl_basic_map_n_param(bmap); |
| 4284 | n_in = isl_basic_map_n_in(bmap); |
| 4285 | isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap)); |
| 4286 | isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1); |
| 4287 | isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1); |
| 4288 | return isl_basic_map_finalize(bmap); |
| 4289 | error: |
| 4290 | isl_basic_map_free(bmap); |
| 4291 | return NULL; |
| 4292 | } |
| 4293 | |
| 4294 | __isl_give isl_basic_map *isl_basic_map_equal( |
| 4295 | __isl_take isl_space *dim, unsigned n_equal) |
| 4296 | { |
| 4297 | int i; |
| 4298 | struct isl_basic_map *bmap; |
| 4299 | bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0); |
| 4300 | if (!bmap) |
| 4301 | return NULL; |
| 4302 | for (i = 0; i < n_equal && bmap; ++i) |
| 4303 | bmap = var_equal(bmap, i); |
| 4304 | return isl_basic_map_finalize(bmap); |
| 4305 | } |
| 4306 | |
| 4307 | /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos] |
| 4308 | */ |
| 4309 | __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim, |
| 4310 | unsigned pos) |
| 4311 | { |
| 4312 | int i; |
| 4313 | struct isl_basic_map *bmap; |
| 4314 | bmap = isl_basic_map_alloc_space(dim, 0, pos, 1); |
| 4315 | if (!bmap) |
| 4316 | return NULL; |
| 4317 | for (i = 0; i < pos && bmap; ++i) |
| 4318 | bmap = var_equal(bmap, i); |
| 4319 | if (bmap) |
| 4320 | bmap = var_less(bmap, pos); |
| 4321 | return isl_basic_map_finalize(bmap); |
| 4322 | } |
| 4323 | |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 4324 | /* Return a relation on "dim" expressing i_[0..pos] <<= o_[0..pos] |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 4325 | */ |
| 4326 | __isl_give isl_basic_map *isl_basic_map_less_or_equal_at( |
| 4327 | __isl_take isl_space *dim, unsigned pos) |
| 4328 | { |
| 4329 | int i; |
| 4330 | isl_basic_map *bmap; |
| 4331 | |
| 4332 | bmap = isl_basic_map_alloc_space(dim, 0, pos, 1); |
| 4333 | for (i = 0; i < pos; ++i) |
| 4334 | bmap = var_equal(bmap, i); |
| 4335 | bmap = var_less_or_equal(bmap, pos); |
| 4336 | return isl_basic_map_finalize(bmap); |
| 4337 | } |
| 4338 | |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 4339 | /* Return a relation on "dim" expressing i_pos > o_pos |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 4340 | */ |
| 4341 | __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim, |
| 4342 | unsigned pos) |
| 4343 | { |
| 4344 | int i; |
| 4345 | struct isl_basic_map *bmap; |
| 4346 | bmap = isl_basic_map_alloc_space(dim, 0, pos, 1); |
| 4347 | if (!bmap) |
| 4348 | return NULL; |
| 4349 | for (i = 0; i < pos && bmap; ++i) |
| 4350 | bmap = var_equal(bmap, i); |
| 4351 | if (bmap) |
| 4352 | bmap = var_more(bmap, pos); |
| 4353 | return isl_basic_map_finalize(bmap); |
| 4354 | } |
| 4355 | |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 4356 | /* Return a relation on "dim" expressing i_[0..pos] >>= o_[0..pos] |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 4357 | */ |
| 4358 | __isl_give isl_basic_map *isl_basic_map_more_or_equal_at( |
| 4359 | __isl_take isl_space *dim, unsigned pos) |
| 4360 | { |
| 4361 | int i; |
| 4362 | isl_basic_map *bmap; |
| 4363 | |
| 4364 | bmap = isl_basic_map_alloc_space(dim, 0, pos, 1); |
| 4365 | for (i = 0; i < pos; ++i) |
| 4366 | bmap = var_equal(bmap, i); |
| 4367 | bmap = var_more_or_equal(bmap, pos); |
| 4368 | return isl_basic_map_finalize(bmap); |
| 4369 | } |
| 4370 | |
| 4371 | static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims, |
| 4372 | unsigned n, int equal) |
| 4373 | { |
| 4374 | struct isl_map *map; |
| 4375 | int i; |
| 4376 | |
| 4377 | if (n == 0 && equal) |
| 4378 | return isl_map_universe(dims); |
| 4379 | |
| 4380 | map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT); |
| 4381 | |
| 4382 | for (i = 0; i + 1 < n; ++i) |
| 4383 | map = isl_map_add_basic_map(map, |
| 4384 | isl_basic_map_less_at(isl_space_copy(dims), i)); |
| 4385 | if (n > 0) { |
| 4386 | if (equal) |
| 4387 | map = isl_map_add_basic_map(map, |
| 4388 | isl_basic_map_less_or_equal_at(dims, n - 1)); |
| 4389 | else |
| 4390 | map = isl_map_add_basic_map(map, |
| 4391 | isl_basic_map_less_at(dims, n - 1)); |
| 4392 | } else |
| 4393 | isl_space_free(dims); |
| 4394 | |
| 4395 | return map; |
| 4396 | } |
| 4397 | |
| 4398 | static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal) |
| 4399 | { |
| 4400 | if (!dims) |
| 4401 | return NULL; |
| 4402 | return map_lex_lte_first(dims, dims->n_out, equal); |
| 4403 | } |
| 4404 | |
| 4405 | __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n) |
| 4406 | { |
| 4407 | return map_lex_lte_first(dim, n, 0); |
| 4408 | } |
| 4409 | |
| 4410 | __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n) |
| 4411 | { |
| 4412 | return map_lex_lte_first(dim, n, 1); |
| 4413 | } |
| 4414 | |
| 4415 | __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim) |
| 4416 | { |
| 4417 | return map_lex_lte(isl_space_map_from_set(set_dim), 0); |
| 4418 | } |
| 4419 | |
| 4420 | __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim) |
| 4421 | { |
| 4422 | return map_lex_lte(isl_space_map_from_set(set_dim), 1); |
| 4423 | } |
| 4424 | |
| 4425 | static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims, |
| 4426 | unsigned n, int equal) |
| 4427 | { |
| 4428 | struct isl_map *map; |
| 4429 | int i; |
| 4430 | |
| 4431 | if (n == 0 && equal) |
| 4432 | return isl_map_universe(dims); |
| 4433 | |
| 4434 | map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT); |
| 4435 | |
| 4436 | for (i = 0; i + 1 < n; ++i) |
| 4437 | map = isl_map_add_basic_map(map, |
| 4438 | isl_basic_map_more_at(isl_space_copy(dims), i)); |
| 4439 | if (n > 0) { |
| 4440 | if (equal) |
| 4441 | map = isl_map_add_basic_map(map, |
| 4442 | isl_basic_map_more_or_equal_at(dims, n - 1)); |
| 4443 | else |
| 4444 | map = isl_map_add_basic_map(map, |
| 4445 | isl_basic_map_more_at(dims, n - 1)); |
| 4446 | } else |
| 4447 | isl_space_free(dims); |
| 4448 | |
| 4449 | return map; |
| 4450 | } |
| 4451 | |
| 4452 | static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal) |
| 4453 | { |
| 4454 | if (!dims) |
| 4455 | return NULL; |
| 4456 | return map_lex_gte_first(dims, dims->n_out, equal); |
| 4457 | } |
| 4458 | |
| 4459 | __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n) |
| 4460 | { |
| 4461 | return map_lex_gte_first(dim, n, 0); |
| 4462 | } |
| 4463 | |
| 4464 | __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n) |
| 4465 | { |
| 4466 | return map_lex_gte_first(dim, n, 1); |
| 4467 | } |
| 4468 | |
| 4469 | __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim) |
| 4470 | { |
| 4471 | return map_lex_gte(isl_space_map_from_set(set_dim), 0); |
| 4472 | } |
| 4473 | |
| 4474 | __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim) |
| 4475 | { |
| 4476 | return map_lex_gte(isl_space_map_from_set(set_dim), 1); |
| 4477 | } |
| 4478 | |
| 4479 | __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1, |
| 4480 | __isl_take isl_set *set2) |
| 4481 | { |
| 4482 | isl_map *map; |
| 4483 | map = isl_map_lex_le(isl_set_get_space(set1)); |
| 4484 | map = isl_map_intersect_domain(map, set1); |
| 4485 | map = isl_map_intersect_range(map, set2); |
| 4486 | return map; |
| 4487 | } |
| 4488 | |
| 4489 | __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1, |
| 4490 | __isl_take isl_set *set2) |
| 4491 | { |
| 4492 | isl_map *map; |
| 4493 | map = isl_map_lex_lt(isl_set_get_space(set1)); |
| 4494 | map = isl_map_intersect_domain(map, set1); |
| 4495 | map = isl_map_intersect_range(map, set2); |
| 4496 | return map; |
| 4497 | } |
| 4498 | |
| 4499 | __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1, |
| 4500 | __isl_take isl_set *set2) |
| 4501 | { |
| 4502 | isl_map *map; |
| 4503 | map = isl_map_lex_ge(isl_set_get_space(set1)); |
| 4504 | map = isl_map_intersect_domain(map, set1); |
| 4505 | map = isl_map_intersect_range(map, set2); |
| 4506 | return map; |
| 4507 | } |
| 4508 | |
| 4509 | __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1, |
| 4510 | __isl_take isl_set *set2) |
| 4511 | { |
| 4512 | isl_map *map; |
| 4513 | map = isl_map_lex_gt(isl_set_get_space(set1)); |
| 4514 | map = isl_map_intersect_domain(map, set1); |
| 4515 | map = isl_map_intersect_range(map, set2); |
| 4516 | return map; |
| 4517 | } |
| 4518 | |
| 4519 | __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1, |
| 4520 | __isl_take isl_map *map2) |
| 4521 | { |
| 4522 | isl_map *map; |
| 4523 | map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1))); |
| 4524 | map = isl_map_apply_domain(map, isl_map_reverse(map1)); |
| 4525 | map = isl_map_apply_range(map, isl_map_reverse(map2)); |
| 4526 | return map; |
| 4527 | } |
| 4528 | |
| 4529 | __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1, |
| 4530 | __isl_take isl_map *map2) |
| 4531 | { |
| 4532 | isl_map *map; |
| 4533 | map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1))); |
| 4534 | map = isl_map_apply_domain(map, isl_map_reverse(map1)); |
| 4535 | map = isl_map_apply_range(map, isl_map_reverse(map2)); |
| 4536 | return map; |
| 4537 | } |
| 4538 | |
| 4539 | __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1, |
| 4540 | __isl_take isl_map *map2) |
| 4541 | { |
| 4542 | isl_map *map; |
| 4543 | map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1))); |
| 4544 | map = isl_map_apply_domain(map, isl_map_reverse(map1)); |
| 4545 | map = isl_map_apply_range(map, isl_map_reverse(map2)); |
| 4546 | return map; |
| 4547 | } |
| 4548 | |
| 4549 | __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1, |
| 4550 | __isl_take isl_map *map2) |
| 4551 | { |
| 4552 | isl_map *map; |
| 4553 | map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1))); |
| 4554 | map = isl_map_apply_domain(map, isl_map_reverse(map1)); |
| 4555 | map = isl_map_apply_range(map, isl_map_reverse(map2)); |
| 4556 | return map; |
| 4557 | } |
| 4558 | |
Tobias Grosser | eab0943e | 2016-11-22 21:31:59 +0000 | [diff] [blame] | 4559 | static __isl_give isl_basic_map *basic_map_from_basic_set( |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 4560 | __isl_take isl_basic_set *bset, __isl_take isl_space *dim) |
| 4561 | { |
| 4562 | struct isl_basic_map *bmap; |
| 4563 | |
| 4564 | bset = isl_basic_set_cow(bset); |
| 4565 | if (!bset || !dim) |
| 4566 | goto error; |
| 4567 | |
Tobias Grosser | eab0943e | 2016-11-22 21:31:59 +0000 | [diff] [blame] | 4568 | isl_assert(bset->ctx, isl_space_compatible_internal(bset->dim, dim), |
| 4569 | goto error); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 4570 | isl_space_free(bset->dim); |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 4571 | bmap = bset_to_bmap(bset); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 4572 | bmap->dim = dim; |
| 4573 | return isl_basic_map_finalize(bmap); |
| 4574 | error: |
| 4575 | isl_basic_set_free(bset); |
| 4576 | isl_space_free(dim); |
| 4577 | return NULL; |
| 4578 | } |
| 4579 | |
Tobias Grosser | eab0943e | 2016-11-22 21:31:59 +0000 | [diff] [blame] | 4580 | __isl_give isl_basic_map *isl_basic_map_from_basic_set( |
| 4581 | __isl_take isl_basic_set *bset, __isl_take isl_space *space) |
| 4582 | { |
| 4583 | return basic_map_from_basic_set(bset, space); |
| 4584 | } |
| 4585 | |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 4586 | /* For a div d = floor(f/m), add the constraint |
| 4587 | * |
| 4588 | * f - m d >= 0 |
| 4589 | */ |
| 4590 | static int add_upper_div_constraint(__isl_keep isl_basic_map *bmap, |
| 4591 | unsigned pos, isl_int *div) |
| 4592 | { |
| 4593 | int i; |
| 4594 | unsigned total = isl_basic_map_total_dim(bmap); |
| 4595 | |
| 4596 | i = isl_basic_map_alloc_inequality(bmap); |
| 4597 | if (i < 0) |
| 4598 | return -1; |
| 4599 | isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total); |
| 4600 | isl_int_neg(bmap->ineq[i][1 + pos], div[0]); |
| 4601 | |
| 4602 | return 0; |
| 4603 | } |
| 4604 | |
| 4605 | /* For a div d = floor(f/m), add the constraint |
| 4606 | * |
Tobias Grosser | 07b2095 | 2016-06-12 04:30:40 +0000 | [diff] [blame] | 4607 | * -(f-(m-1)) + m d >= 0 |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 4608 | */ |
| 4609 | static int add_lower_div_constraint(__isl_keep isl_basic_map *bmap, |
| 4610 | unsigned pos, isl_int *div) |
| 4611 | { |
| 4612 | int i; |
| 4613 | unsigned total = isl_basic_map_total_dim(bmap); |
| 4614 | |
| 4615 | i = isl_basic_map_alloc_inequality(bmap); |
| 4616 | if (i < 0) |
| 4617 | return -1; |
| 4618 | isl_seq_neg(bmap->ineq[i], div + 1, 1 + total); |
| 4619 | isl_int_set(bmap->ineq[i][1 + pos], div[0]); |
| 4620 | isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]); |
| 4621 | isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1); |
| 4622 | |
| 4623 | return 0; |
| 4624 | } |
| 4625 | |
| 4626 | /* For a div d = floor(f/m), add the constraints |
| 4627 | * |
| 4628 | * f - m d >= 0 |
Tobias Grosser | 07b2095 | 2016-06-12 04:30:40 +0000 | [diff] [blame] | 4629 | * -(f-(m-1)) + m d >= 0 |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 4630 | * |
| 4631 | * Note that the second constraint is the negation of |
| 4632 | * |
Tobias Grosser | 07b2095 | 2016-06-12 04:30:40 +0000 | [diff] [blame] | 4633 | * f - m d >= m |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 4634 | */ |
| 4635 | int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap, |
| 4636 | unsigned pos, isl_int *div) |
| 4637 | { |
| 4638 | if (add_upper_div_constraint(bmap, pos, div) < 0) |
| 4639 | return -1; |
| 4640 | if (add_lower_div_constraint(bmap, pos, div) < 0) |
| 4641 | return -1; |
| 4642 | return 0; |
| 4643 | } |
| 4644 | |
| 4645 | int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset, |
| 4646 | unsigned pos, isl_int *div) |
| 4647 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 4648 | return isl_basic_map_add_div_constraints_var(bset_to_bmap(bset), |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 4649 | pos, div); |
| 4650 | } |
| 4651 | |
| 4652 | int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div) |
| 4653 | { |
| 4654 | unsigned total = isl_basic_map_total_dim(bmap); |
| 4655 | unsigned div_pos = total - bmap->n_div + div; |
| 4656 | |
| 4657 | return isl_basic_map_add_div_constraints_var(bmap, div_pos, |
| 4658 | bmap->div[div]); |
| 4659 | } |
| 4660 | |
| 4661 | /* For each known div d = floor(f/m), add the constraints |
| 4662 | * |
| 4663 | * f - m d >= 0 |
Tobias Grosser | 07b2095 | 2016-06-12 04:30:40 +0000 | [diff] [blame] | 4664 | * -(f-(m-1)) + m d >= 0 |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 4665 | * |
| 4666 | * Remove duplicate constraints in case of some these div constraints |
| 4667 | * already appear in "bmap". |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 4668 | */ |
| 4669 | __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints( |
| 4670 | __isl_take isl_basic_map *bmap) |
| 4671 | { |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 4672 | unsigned n_div; |
| 4673 | |
| 4674 | if (!bmap) |
| 4675 | return NULL; |
| 4676 | n_div = isl_basic_map_dim(bmap, isl_dim_div); |
| 4677 | if (n_div == 0) |
| 4678 | return bmap; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 4679 | |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 4680 | bmap = add_known_div_constraints(bmap); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 4681 | bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0); |
| 4682 | bmap = isl_basic_map_finalize(bmap); |
| 4683 | return bmap; |
| 4684 | } |
| 4685 | |
| 4686 | /* Add the div constraint of sign "sign" for div "div" of "bmap". |
| 4687 | * |
| 4688 | * In particular, if this div is of the form d = floor(f/m), |
| 4689 | * then add the constraint |
| 4690 | * |
| 4691 | * f - m d >= 0 |
| 4692 | * |
| 4693 | * if sign < 0 or the constraint |
| 4694 | * |
Tobias Grosser | 07b2095 | 2016-06-12 04:30:40 +0000 | [diff] [blame] | 4695 | * -(f-(m-1)) + m d >= 0 |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 4696 | * |
| 4697 | * if sign > 0. |
| 4698 | */ |
| 4699 | int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map *bmap, |
| 4700 | unsigned div, int sign) |
| 4701 | { |
| 4702 | unsigned total; |
| 4703 | unsigned div_pos; |
| 4704 | |
| 4705 | if (!bmap) |
| 4706 | return -1; |
| 4707 | |
| 4708 | total = isl_basic_map_total_dim(bmap); |
| 4709 | div_pos = total - bmap->n_div + div; |
| 4710 | |
| 4711 | if (sign < 0) |
| 4712 | return add_upper_div_constraint(bmap, div_pos, bmap->div[div]); |
| 4713 | else |
| 4714 | return add_lower_div_constraint(bmap, div_pos, bmap->div[div]); |
| 4715 | } |
| 4716 | |
| 4717 | int isl_basic_set_add_div_constraints(struct isl_basic_set *bset, unsigned div) |
| 4718 | { |
| 4719 | return isl_basic_map_add_div_constraints(bset, div); |
| 4720 | } |
| 4721 | |
| 4722 | struct isl_basic_set *isl_basic_map_underlying_set( |
| 4723 | struct isl_basic_map *bmap) |
| 4724 | { |
| 4725 | if (!bmap) |
| 4726 | goto error; |
| 4727 | if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 && |
| 4728 | bmap->n_div == 0 && |
| 4729 | !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) && |
| 4730 | !isl_space_is_named_or_nested(bmap->dim, isl_dim_out)) |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 4731 | return bset_from_bmap(bmap); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 4732 | bmap = isl_basic_map_cow(bmap); |
| 4733 | if (!bmap) |
| 4734 | goto error; |
| 4735 | bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div); |
| 4736 | if (!bmap->dim) |
| 4737 | goto error; |
| 4738 | bmap->extra -= bmap->n_div; |
| 4739 | bmap->n_div = 0; |
| 4740 | bmap = isl_basic_map_finalize(bmap); |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 4741 | return bset_from_bmap(bmap); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 4742 | error: |
| 4743 | isl_basic_map_free(bmap); |
| 4744 | return NULL; |
| 4745 | } |
| 4746 | |
| 4747 | __isl_give isl_basic_set *isl_basic_set_underlying_set( |
| 4748 | __isl_take isl_basic_set *bset) |
| 4749 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 4750 | return isl_basic_map_underlying_set(bset_to_bmap(bset)); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 4751 | } |
| 4752 | |
| 4753 | /* Replace each element in "list" by the result of applying |
Tobias Grosser | 1fa7b97 | 2015-02-16 19:33:40 +0000 | [diff] [blame] | 4754 | * isl_basic_map_underlying_set to the element. |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 4755 | */ |
Tobias Grosser | 1fa7b97 | 2015-02-16 19:33:40 +0000 | [diff] [blame] | 4756 | __isl_give isl_basic_set_list *isl_basic_map_list_underlying_set( |
| 4757 | __isl_take isl_basic_map_list *list) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 4758 | { |
| 4759 | int i, n; |
| 4760 | |
| 4761 | if (!list) |
| 4762 | return NULL; |
| 4763 | |
Tobias Grosser | 1fa7b97 | 2015-02-16 19:33:40 +0000 | [diff] [blame] | 4764 | n = isl_basic_map_list_n_basic_map(list); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 4765 | for (i = 0; i < n; ++i) { |
Tobias Grosser | 1fa7b97 | 2015-02-16 19:33:40 +0000 | [diff] [blame] | 4766 | isl_basic_map *bmap; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 4767 | isl_basic_set *bset; |
| 4768 | |
Tobias Grosser | 1fa7b97 | 2015-02-16 19:33:40 +0000 | [diff] [blame] | 4769 | bmap = isl_basic_map_list_get_basic_map(list, i); |
| 4770 | bset = isl_basic_set_underlying_set(bmap); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 4771 | list = isl_basic_set_list_set_basic_set(list, i, bset); |
| 4772 | } |
| 4773 | |
| 4774 | return list; |
| 4775 | } |
| 4776 | |
| 4777 | struct isl_basic_map *isl_basic_map_overlying_set( |
| 4778 | struct isl_basic_set *bset, struct isl_basic_map *like) |
| 4779 | { |
| 4780 | struct isl_basic_map *bmap; |
| 4781 | struct isl_ctx *ctx; |
| 4782 | unsigned total; |
| 4783 | int i; |
| 4784 | |
| 4785 | if (!bset || !like) |
| 4786 | goto error; |
| 4787 | ctx = bset->ctx; |
| 4788 | isl_assert(ctx, bset->n_div == 0, goto error); |
| 4789 | isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error); |
| 4790 | isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like), |
| 4791 | goto error); |
Tobias Grosser | 6e3ba33 | 2015-08-11 11:31:18 +0000 | [diff] [blame] | 4792 | if (like->n_div == 0) { |
| 4793 | isl_space *space = isl_basic_map_get_space(like); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 4794 | isl_basic_map_free(like); |
Tobias Grosser | 6e3ba33 | 2015-08-11 11:31:18 +0000 | [diff] [blame] | 4795 | return isl_basic_map_reset_space(bset, space); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 4796 | } |
| 4797 | bset = isl_basic_set_cow(bset); |
| 4798 | if (!bset) |
| 4799 | goto error; |
| 4800 | total = bset->dim->n_out + bset->extra; |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 4801 | bmap = bset_to_bmap(bset); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 4802 | isl_space_free(bmap->dim); |
| 4803 | bmap->dim = isl_space_copy(like->dim); |
| 4804 | if (!bmap->dim) |
| 4805 | goto error; |
| 4806 | bmap->n_div = like->n_div; |
| 4807 | bmap->extra += like->n_div; |
| 4808 | if (bmap->extra) { |
| 4809 | unsigned ltotal; |
| 4810 | isl_int **div; |
| 4811 | ltotal = total - bmap->extra + like->extra; |
| 4812 | if (ltotal > total) |
| 4813 | ltotal = total; |
| 4814 | bmap->block2 = isl_blk_extend(ctx, bmap->block2, |
| 4815 | bmap->extra * (1 + 1 + total)); |
| 4816 | if (isl_blk_is_error(bmap->block2)) |
| 4817 | goto error; |
| 4818 | div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra); |
| 4819 | if (!div) |
| 4820 | goto error; |
| 4821 | bmap->div = div; |
| 4822 | for (i = 0; i < bmap->extra; ++i) |
| 4823 | bmap->div[i] = bmap->block2.data + i * (1 + 1 + total); |
| 4824 | for (i = 0; i < like->n_div; ++i) { |
| 4825 | isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal); |
| 4826 | isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal); |
| 4827 | } |
| 4828 | bmap = isl_basic_map_add_known_div_constraints(bmap); |
| 4829 | } |
| 4830 | isl_basic_map_free(like); |
| 4831 | bmap = isl_basic_map_simplify(bmap); |
| 4832 | bmap = isl_basic_map_finalize(bmap); |
| 4833 | return bmap; |
| 4834 | error: |
| 4835 | isl_basic_map_free(like); |
| 4836 | isl_basic_set_free(bset); |
| 4837 | return NULL; |
| 4838 | } |
| 4839 | |
| 4840 | struct isl_basic_set *isl_basic_set_from_underlying_set( |
| 4841 | struct isl_basic_set *bset, struct isl_basic_set *like) |
| 4842 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 4843 | return bset_from_bmap(isl_basic_map_overlying_set(bset, |
| 4844 | bset_to_bmap(like))); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 4845 | } |
| 4846 | |
| 4847 | struct isl_set *isl_set_from_underlying_set( |
| 4848 | struct isl_set *set, struct isl_basic_set *like) |
| 4849 | { |
| 4850 | int i; |
| 4851 | |
| 4852 | if (!set || !like) |
| 4853 | goto error; |
| 4854 | isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like), |
| 4855 | goto error); |
| 4856 | if (isl_space_is_equal(set->dim, like->dim) && like->n_div == 0) { |
| 4857 | isl_basic_set_free(like); |
| 4858 | return set; |
| 4859 | } |
| 4860 | set = isl_set_cow(set); |
| 4861 | if (!set) |
| 4862 | goto error; |
| 4863 | for (i = 0; i < set->n; ++i) { |
| 4864 | set->p[i] = isl_basic_set_from_underlying_set(set->p[i], |
| 4865 | isl_basic_set_copy(like)); |
| 4866 | if (!set->p[i]) |
| 4867 | goto error; |
| 4868 | } |
| 4869 | isl_space_free(set->dim); |
| 4870 | set->dim = isl_space_copy(like->dim); |
| 4871 | if (!set->dim) |
| 4872 | goto error; |
| 4873 | isl_basic_set_free(like); |
| 4874 | return set; |
| 4875 | error: |
| 4876 | isl_basic_set_free(like); |
| 4877 | isl_set_free(set); |
| 4878 | return NULL; |
| 4879 | } |
| 4880 | |
| 4881 | struct isl_set *isl_map_underlying_set(struct isl_map *map) |
| 4882 | { |
| 4883 | int i; |
| 4884 | |
| 4885 | map = isl_map_cow(map); |
| 4886 | if (!map) |
| 4887 | return NULL; |
| 4888 | map->dim = isl_space_cow(map->dim); |
| 4889 | if (!map->dim) |
| 4890 | goto error; |
| 4891 | |
| 4892 | for (i = 1; i < map->n; ++i) |
| 4893 | isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div, |
| 4894 | goto error); |
| 4895 | for (i = 0; i < map->n; ++i) { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 4896 | map->p[i] = bset_to_bmap( |
| 4897 | isl_basic_map_underlying_set(map->p[i])); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 4898 | if (!map->p[i]) |
| 4899 | goto error; |
| 4900 | } |
| 4901 | if (map->n == 0) |
| 4902 | map->dim = isl_space_underlying(map->dim, 0); |
| 4903 | else { |
| 4904 | isl_space_free(map->dim); |
| 4905 | map->dim = isl_space_copy(map->p[0]->dim); |
| 4906 | } |
| 4907 | if (!map->dim) |
| 4908 | goto error; |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 4909 | return set_from_map(map); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 4910 | error: |
| 4911 | isl_map_free(map); |
| 4912 | return NULL; |
| 4913 | } |
| 4914 | |
| 4915 | struct isl_set *isl_set_to_underlying_set(struct isl_set *set) |
| 4916 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 4917 | return set_from_map(isl_map_underlying_set(set_to_map(set))); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 4918 | } |
| 4919 | |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 4920 | /* Replace the space of "bmap" by "space". |
| 4921 | * |
| 4922 | * If the space of "bmap" is identical to "space" (including the identifiers |
| 4923 | * of the input and output dimensions), then simply return the original input. |
| 4924 | */ |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 4925 | __isl_give isl_basic_map *isl_basic_map_reset_space( |
Tobias Grosser | 6e3ba33 | 2015-08-11 11:31:18 +0000 | [diff] [blame] | 4926 | __isl_take isl_basic_map *bmap, __isl_take isl_space *space) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 4927 | { |
Tobias Grosser | 6e3ba33 | 2015-08-11 11:31:18 +0000 | [diff] [blame] | 4928 | isl_bool equal; |
| 4929 | |
| 4930 | if (!bmap) |
| 4931 | goto error; |
| 4932 | equal = isl_space_is_equal(bmap->dim, space); |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 4933 | if (equal >= 0 && equal) |
| 4934 | equal = isl_space_match(bmap->dim, isl_dim_in, |
| 4935 | space, isl_dim_in); |
| 4936 | if (equal >= 0 && equal) |
| 4937 | equal = isl_space_match(bmap->dim, isl_dim_out, |
| 4938 | space, isl_dim_out); |
Tobias Grosser | 6e3ba33 | 2015-08-11 11:31:18 +0000 | [diff] [blame] | 4939 | if (equal < 0) |
| 4940 | goto error; |
| 4941 | if (equal) { |
| 4942 | isl_space_free(space); |
| 4943 | return bmap; |
| 4944 | } |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 4945 | bmap = isl_basic_map_cow(bmap); |
Tobias Grosser | 6e3ba33 | 2015-08-11 11:31:18 +0000 | [diff] [blame] | 4946 | if (!bmap || !space) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 4947 | goto error; |
| 4948 | |
| 4949 | isl_space_free(bmap->dim); |
Tobias Grosser | 6e3ba33 | 2015-08-11 11:31:18 +0000 | [diff] [blame] | 4950 | bmap->dim = space; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 4951 | |
| 4952 | bmap = isl_basic_map_finalize(bmap); |
| 4953 | |
| 4954 | return bmap; |
| 4955 | error: |
| 4956 | isl_basic_map_free(bmap); |
Tobias Grosser | 6e3ba33 | 2015-08-11 11:31:18 +0000 | [diff] [blame] | 4957 | isl_space_free(space); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 4958 | return NULL; |
| 4959 | } |
| 4960 | |
| 4961 | __isl_give isl_basic_set *isl_basic_set_reset_space( |
| 4962 | __isl_take isl_basic_set *bset, __isl_take isl_space *dim) |
| 4963 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 4964 | return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset), |
| 4965 | dim)); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 4966 | } |
| 4967 | |
| 4968 | __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map, |
| 4969 | __isl_take isl_space *dim) |
| 4970 | { |
| 4971 | int i; |
| 4972 | |
| 4973 | map = isl_map_cow(map); |
| 4974 | if (!map || !dim) |
| 4975 | goto error; |
| 4976 | |
| 4977 | for (i = 0; i < map->n; ++i) { |
| 4978 | map->p[i] = isl_basic_map_reset_space(map->p[i], |
| 4979 | isl_space_copy(dim)); |
| 4980 | if (!map->p[i]) |
| 4981 | goto error; |
| 4982 | } |
| 4983 | isl_space_free(map->dim); |
| 4984 | map->dim = dim; |
| 4985 | |
| 4986 | return map; |
| 4987 | error: |
| 4988 | isl_map_free(map); |
| 4989 | isl_space_free(dim); |
| 4990 | return NULL; |
| 4991 | } |
| 4992 | |
| 4993 | __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set, |
| 4994 | __isl_take isl_space *dim) |
| 4995 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 4996 | return set_from_map(isl_map_reset_space(set_to_map(set), dim)); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 4997 | } |
| 4998 | |
| 4999 | /* Compute the parameter domain of the given basic set. |
| 5000 | */ |
| 5001 | __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset) |
| 5002 | { |
| 5003 | isl_space *space; |
| 5004 | unsigned n; |
| 5005 | |
| 5006 | if (isl_basic_set_is_params(bset)) |
| 5007 | return bset; |
| 5008 | |
| 5009 | n = isl_basic_set_dim(bset, isl_dim_set); |
| 5010 | bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n); |
| 5011 | space = isl_basic_set_get_space(bset); |
| 5012 | space = isl_space_params(space); |
| 5013 | bset = isl_basic_set_reset_space(bset, space); |
| 5014 | return bset; |
| 5015 | } |
| 5016 | |
| 5017 | /* Construct a zero-dimensional basic set with the given parameter domain. |
| 5018 | */ |
| 5019 | __isl_give isl_basic_set *isl_basic_set_from_params( |
| 5020 | __isl_take isl_basic_set *bset) |
| 5021 | { |
| 5022 | isl_space *space; |
| 5023 | space = isl_basic_set_get_space(bset); |
| 5024 | space = isl_space_set_from_params(space); |
| 5025 | bset = isl_basic_set_reset_space(bset, space); |
| 5026 | return bset; |
| 5027 | } |
| 5028 | |
| 5029 | /* Compute the parameter domain of the given set. |
| 5030 | */ |
| 5031 | __isl_give isl_set *isl_set_params(__isl_take isl_set *set) |
| 5032 | { |
| 5033 | isl_space *space; |
| 5034 | unsigned n; |
| 5035 | |
| 5036 | if (isl_set_is_params(set)) |
| 5037 | return set; |
| 5038 | |
| 5039 | n = isl_set_dim(set, isl_dim_set); |
| 5040 | set = isl_set_project_out(set, isl_dim_set, 0, n); |
| 5041 | space = isl_set_get_space(set); |
| 5042 | space = isl_space_params(space); |
| 5043 | set = isl_set_reset_space(set, space); |
| 5044 | return set; |
| 5045 | } |
| 5046 | |
| 5047 | /* Construct a zero-dimensional set with the given parameter domain. |
| 5048 | */ |
| 5049 | __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set) |
| 5050 | { |
| 5051 | isl_space *space; |
| 5052 | space = isl_set_get_space(set); |
| 5053 | space = isl_space_set_from_params(space); |
| 5054 | set = isl_set_reset_space(set, space); |
| 5055 | return set; |
| 5056 | } |
| 5057 | |
| 5058 | /* Compute the parameter domain of the given map. |
| 5059 | */ |
| 5060 | __isl_give isl_set *isl_map_params(__isl_take isl_map *map) |
| 5061 | { |
| 5062 | isl_space *space; |
| 5063 | unsigned n; |
| 5064 | |
| 5065 | n = isl_map_dim(map, isl_dim_in); |
| 5066 | map = isl_map_project_out(map, isl_dim_in, 0, n); |
| 5067 | n = isl_map_dim(map, isl_dim_out); |
| 5068 | map = isl_map_project_out(map, isl_dim_out, 0, n); |
| 5069 | space = isl_map_get_space(map); |
| 5070 | space = isl_space_params(space); |
| 5071 | map = isl_map_reset_space(map, space); |
| 5072 | return map; |
| 5073 | } |
| 5074 | |
| 5075 | struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap) |
| 5076 | { |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 5077 | isl_space *space; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 5078 | unsigned n_out; |
| 5079 | |
| 5080 | if (!bmap) |
| 5081 | return NULL; |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 5082 | space = isl_space_domain(isl_basic_map_get_space(bmap)); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 5083 | |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 5084 | n_out = isl_basic_map_n_out(bmap); |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 5085 | bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 5086 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 5087 | return isl_basic_map_reset_space(bmap, space); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 5088 | } |
| 5089 | |
| 5090 | int isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap) |
| 5091 | { |
| 5092 | if (!bmap) |
| 5093 | return -1; |
| 5094 | return isl_space_may_be_set(bmap->dim); |
| 5095 | } |
| 5096 | |
| 5097 | /* Is this basic map actually a set? |
| 5098 | * Users should never call this function. Outside of isl, |
| 5099 | * the type should indicate whether something is a set or a map. |
| 5100 | */ |
| 5101 | int isl_basic_map_is_set(__isl_keep isl_basic_map *bmap) |
| 5102 | { |
| 5103 | if (!bmap) |
| 5104 | return -1; |
| 5105 | return isl_space_is_set(bmap->dim); |
| 5106 | } |
| 5107 | |
| 5108 | struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap) |
| 5109 | { |
| 5110 | if (!bmap) |
| 5111 | return NULL; |
| 5112 | if (isl_basic_map_is_set(bmap)) |
| 5113 | return bmap; |
| 5114 | return isl_basic_map_domain(isl_basic_map_reverse(bmap)); |
| 5115 | } |
| 5116 | |
| 5117 | __isl_give isl_basic_map *isl_basic_map_domain_map( |
| 5118 | __isl_take isl_basic_map *bmap) |
| 5119 | { |
| 5120 | int i, k; |
| 5121 | isl_space *dim; |
| 5122 | isl_basic_map *domain; |
| 5123 | int nparam, n_in, n_out; |
| 5124 | unsigned total; |
| 5125 | |
| 5126 | nparam = isl_basic_map_dim(bmap, isl_dim_param); |
| 5127 | n_in = isl_basic_map_dim(bmap, isl_dim_in); |
| 5128 | n_out = isl_basic_map_dim(bmap, isl_dim_out); |
| 5129 | |
| 5130 | dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap))); |
| 5131 | domain = isl_basic_map_universe(dim); |
| 5132 | |
| 5133 | bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap)); |
| 5134 | bmap = isl_basic_map_apply_range(bmap, domain); |
| 5135 | bmap = isl_basic_map_extend_constraints(bmap, n_in, 0); |
| 5136 | |
| 5137 | total = isl_basic_map_total_dim(bmap); |
| 5138 | |
| 5139 | for (i = 0; i < n_in; ++i) { |
| 5140 | k = isl_basic_map_alloc_equality(bmap); |
| 5141 | if (k < 0) |
| 5142 | goto error; |
| 5143 | isl_seq_clr(bmap->eq[k], 1 + total); |
| 5144 | isl_int_set_si(bmap->eq[k][1 + nparam + i], -1); |
| 5145 | isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1); |
| 5146 | } |
| 5147 | |
| 5148 | bmap = isl_basic_map_gauss(bmap, NULL); |
| 5149 | return isl_basic_map_finalize(bmap); |
| 5150 | error: |
| 5151 | isl_basic_map_free(bmap); |
| 5152 | return NULL; |
| 5153 | } |
| 5154 | |
| 5155 | __isl_give isl_basic_map *isl_basic_map_range_map( |
| 5156 | __isl_take isl_basic_map *bmap) |
| 5157 | { |
| 5158 | int i, k; |
| 5159 | isl_space *dim; |
| 5160 | isl_basic_map *range; |
| 5161 | int nparam, n_in, n_out; |
| 5162 | unsigned total; |
| 5163 | |
| 5164 | nparam = isl_basic_map_dim(bmap, isl_dim_param); |
| 5165 | n_in = isl_basic_map_dim(bmap, isl_dim_in); |
| 5166 | n_out = isl_basic_map_dim(bmap, isl_dim_out); |
| 5167 | |
| 5168 | dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap))); |
| 5169 | range = isl_basic_map_universe(dim); |
| 5170 | |
| 5171 | bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap)); |
| 5172 | bmap = isl_basic_map_apply_range(bmap, range); |
| 5173 | bmap = isl_basic_map_extend_constraints(bmap, n_out, 0); |
| 5174 | |
| 5175 | total = isl_basic_map_total_dim(bmap); |
| 5176 | |
| 5177 | for (i = 0; i < n_out; ++i) { |
| 5178 | k = isl_basic_map_alloc_equality(bmap); |
| 5179 | if (k < 0) |
| 5180 | goto error; |
| 5181 | isl_seq_clr(bmap->eq[k], 1 + total); |
| 5182 | isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1); |
| 5183 | isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1); |
| 5184 | } |
| 5185 | |
| 5186 | bmap = isl_basic_map_gauss(bmap, NULL); |
| 5187 | return isl_basic_map_finalize(bmap); |
| 5188 | error: |
| 5189 | isl_basic_map_free(bmap); |
| 5190 | return NULL; |
| 5191 | } |
| 5192 | |
| 5193 | int isl_map_may_be_set(__isl_keep isl_map *map) |
| 5194 | { |
| 5195 | if (!map) |
| 5196 | return -1; |
| 5197 | return isl_space_may_be_set(map->dim); |
| 5198 | } |
| 5199 | |
| 5200 | /* Is this map actually a set? |
| 5201 | * Users should never call this function. Outside of isl, |
| 5202 | * the type should indicate whether something is a set or a map. |
| 5203 | */ |
| 5204 | int isl_map_is_set(__isl_keep isl_map *map) |
| 5205 | { |
| 5206 | if (!map) |
| 5207 | return -1; |
| 5208 | return isl_space_is_set(map->dim); |
| 5209 | } |
| 5210 | |
| 5211 | struct isl_set *isl_map_range(struct isl_map *map) |
| 5212 | { |
| 5213 | int i; |
| 5214 | struct isl_set *set; |
| 5215 | |
| 5216 | if (!map) |
| 5217 | goto error; |
| 5218 | if (isl_map_is_set(map)) |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 5219 | return set_from_map(map); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 5220 | |
| 5221 | map = isl_map_cow(map); |
| 5222 | if (!map) |
| 5223 | goto error; |
| 5224 | |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 5225 | set = set_from_map(map); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 5226 | set->dim = isl_space_range(set->dim); |
| 5227 | if (!set->dim) |
| 5228 | goto error; |
| 5229 | for (i = 0; i < map->n; ++i) { |
| 5230 | set->p[i] = isl_basic_map_range(map->p[i]); |
| 5231 | if (!set->p[i]) |
| 5232 | goto error; |
| 5233 | } |
| 5234 | ISL_F_CLR(set, ISL_MAP_DISJOINT); |
| 5235 | ISL_F_CLR(set, ISL_SET_NORMALIZED); |
| 5236 | return set; |
| 5237 | error: |
| 5238 | isl_map_free(map); |
| 5239 | return NULL; |
| 5240 | } |
| 5241 | |
| 5242 | __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map) |
| 5243 | { |
| 5244 | int i; |
| 5245 | |
| 5246 | map = isl_map_cow(map); |
| 5247 | if (!map) |
| 5248 | return NULL; |
| 5249 | |
| 5250 | map->dim = isl_space_domain_map(map->dim); |
| 5251 | if (!map->dim) |
| 5252 | goto error; |
| 5253 | for (i = 0; i < map->n; ++i) { |
| 5254 | map->p[i] = isl_basic_map_domain_map(map->p[i]); |
| 5255 | if (!map->p[i]) |
| 5256 | goto error; |
| 5257 | } |
| 5258 | ISL_F_CLR(map, ISL_MAP_DISJOINT); |
| 5259 | ISL_F_CLR(map, ISL_MAP_NORMALIZED); |
| 5260 | return map; |
| 5261 | error: |
| 5262 | isl_map_free(map); |
| 5263 | return NULL; |
| 5264 | } |
| 5265 | |
| 5266 | __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map) |
| 5267 | { |
| 5268 | int i; |
| 5269 | isl_space *range_dim; |
| 5270 | |
| 5271 | map = isl_map_cow(map); |
| 5272 | if (!map) |
| 5273 | return NULL; |
| 5274 | |
| 5275 | range_dim = isl_space_range(isl_map_get_space(map)); |
| 5276 | range_dim = isl_space_from_range(range_dim); |
| 5277 | map->dim = isl_space_from_domain(isl_space_wrap(map->dim)); |
| 5278 | map->dim = isl_space_join(map->dim, range_dim); |
| 5279 | if (!map->dim) |
| 5280 | goto error; |
| 5281 | for (i = 0; i < map->n; ++i) { |
| 5282 | map->p[i] = isl_basic_map_range_map(map->p[i]); |
| 5283 | if (!map->p[i]) |
| 5284 | goto error; |
| 5285 | } |
| 5286 | ISL_F_CLR(map, ISL_MAP_DISJOINT); |
| 5287 | ISL_F_CLR(map, ISL_MAP_NORMALIZED); |
| 5288 | return map; |
| 5289 | error: |
| 5290 | isl_map_free(map); |
| 5291 | return NULL; |
| 5292 | } |
| 5293 | |
| 5294 | /* Given a wrapped map of the form A[B -> C], |
| 5295 | * return the map A[B -> C] -> B. |
| 5296 | */ |
| 5297 | __isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set) |
| 5298 | { |
| 5299 | isl_id *id; |
| 5300 | isl_map *map; |
| 5301 | |
| 5302 | if (!set) |
| 5303 | return NULL; |
| 5304 | if (!isl_set_has_tuple_id(set)) |
| 5305 | return isl_map_domain_map(isl_set_unwrap(set)); |
| 5306 | |
| 5307 | id = isl_set_get_tuple_id(set); |
| 5308 | map = isl_map_domain_map(isl_set_unwrap(set)); |
| 5309 | map = isl_map_set_tuple_id(map, isl_dim_in, id); |
| 5310 | |
| 5311 | return map; |
| 5312 | } |
| 5313 | |
| 5314 | __isl_give isl_map *isl_map_from_set(__isl_take isl_set *set, |
| 5315 | __isl_take isl_space *dim) |
| 5316 | { |
| 5317 | int i; |
| 5318 | struct isl_map *map = NULL; |
| 5319 | |
| 5320 | set = isl_set_cow(set); |
| 5321 | if (!set || !dim) |
| 5322 | goto error; |
Tobias Grosser | eab0943e | 2016-11-22 21:31:59 +0000 | [diff] [blame] | 5323 | isl_assert(set->ctx, isl_space_compatible_internal(set->dim, dim), |
| 5324 | goto error); |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 5325 | map = set_to_map(set); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 5326 | for (i = 0; i < set->n; ++i) { |
Tobias Grosser | eab0943e | 2016-11-22 21:31:59 +0000 | [diff] [blame] | 5327 | map->p[i] = basic_map_from_basic_set( |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 5328 | set->p[i], isl_space_copy(dim)); |
| 5329 | if (!map->p[i]) |
| 5330 | goto error; |
| 5331 | } |
| 5332 | isl_space_free(map->dim); |
| 5333 | map->dim = dim; |
| 5334 | return map; |
| 5335 | error: |
| 5336 | isl_space_free(dim); |
| 5337 | isl_set_free(set); |
| 5338 | return NULL; |
| 5339 | } |
| 5340 | |
| 5341 | __isl_give isl_basic_map *isl_basic_map_from_domain( |
| 5342 | __isl_take isl_basic_set *bset) |
| 5343 | { |
| 5344 | return isl_basic_map_reverse(isl_basic_map_from_range(bset)); |
| 5345 | } |
| 5346 | |
| 5347 | __isl_give isl_basic_map *isl_basic_map_from_range( |
| 5348 | __isl_take isl_basic_set *bset) |
| 5349 | { |
| 5350 | isl_space *space; |
| 5351 | space = isl_basic_set_get_space(bset); |
| 5352 | space = isl_space_from_range(space); |
| 5353 | bset = isl_basic_set_reset_space(bset, space); |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 5354 | return bset_to_bmap(bset); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 5355 | } |
| 5356 | |
| 5357 | /* Create a relation with the given set as range. |
| 5358 | * The domain of the created relation is a zero-dimensional |
| 5359 | * flat anonymous space. |
| 5360 | */ |
| 5361 | __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set) |
| 5362 | { |
| 5363 | isl_space *space; |
| 5364 | space = isl_set_get_space(set); |
| 5365 | space = isl_space_from_range(space); |
| 5366 | set = isl_set_reset_space(set, space); |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 5367 | return set_to_map(set); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 5368 | } |
| 5369 | |
| 5370 | /* Create a relation with the given set as domain. |
| 5371 | * The range of the created relation is a zero-dimensional |
| 5372 | * flat anonymous space. |
| 5373 | */ |
| 5374 | __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set) |
| 5375 | { |
| 5376 | return isl_map_reverse(isl_map_from_range(set)); |
| 5377 | } |
| 5378 | |
| 5379 | __isl_give isl_basic_map *isl_basic_map_from_domain_and_range( |
| 5380 | __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range) |
| 5381 | { |
| 5382 | return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range); |
| 5383 | } |
| 5384 | |
| 5385 | __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain, |
| 5386 | __isl_take isl_set *range) |
| 5387 | { |
| 5388 | return isl_map_apply_range(isl_map_reverse(domain), range); |
| 5389 | } |
| 5390 | |
Tobias Grosser | 07b2095 | 2016-06-12 04:30:40 +0000 | [diff] [blame] | 5391 | /* Return a newly allocated isl_map with given space and flags and |
| 5392 | * room for "n" basic maps. |
| 5393 | * Make sure that all cached information is cleared. |
| 5394 | */ |
| 5395 | __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *space, int n, |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 5396 | unsigned flags) |
| 5397 | { |
| 5398 | struct isl_map *map; |
| 5399 | |
Tobias Grosser | 07b2095 | 2016-06-12 04:30:40 +0000 | [diff] [blame] | 5400 | if (!space) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 5401 | return NULL; |
| 5402 | if (n < 0) |
Tobias Grosser | 07b2095 | 2016-06-12 04:30:40 +0000 | [diff] [blame] | 5403 | isl_die(space->ctx, isl_error_internal, |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 5404 | "negative number of basic maps", goto error); |
Tobias Grosser | 07b2095 | 2016-06-12 04:30:40 +0000 | [diff] [blame] | 5405 | map = isl_calloc(space->ctx, struct isl_map, |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 5406 | sizeof(struct isl_map) + |
| 5407 | (n - 1) * sizeof(struct isl_basic_map *)); |
| 5408 | if (!map) |
| 5409 | goto error; |
| 5410 | |
Tobias Grosser | 07b2095 | 2016-06-12 04:30:40 +0000 | [diff] [blame] | 5411 | map->ctx = space->ctx; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 5412 | isl_ctx_ref(map->ctx); |
| 5413 | map->ref = 1; |
| 5414 | map->size = n; |
| 5415 | map->n = 0; |
Tobias Grosser | 07b2095 | 2016-06-12 04:30:40 +0000 | [diff] [blame] | 5416 | map->dim = space; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 5417 | map->flags = flags; |
| 5418 | return map; |
| 5419 | error: |
Tobias Grosser | 07b2095 | 2016-06-12 04:30:40 +0000 | [diff] [blame] | 5420 | isl_space_free(space); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 5421 | return NULL; |
| 5422 | } |
| 5423 | |
| 5424 | struct isl_map *isl_map_alloc(struct isl_ctx *ctx, |
| 5425 | unsigned nparam, unsigned in, unsigned out, int n, |
| 5426 | unsigned flags) |
| 5427 | { |
| 5428 | struct isl_map *map; |
| 5429 | isl_space *dims; |
| 5430 | |
| 5431 | dims = isl_space_alloc(ctx, nparam, in, out); |
| 5432 | if (!dims) |
| 5433 | return NULL; |
| 5434 | |
| 5435 | map = isl_map_alloc_space(dims, n, flags); |
| 5436 | return map; |
| 5437 | } |
| 5438 | |
| 5439 | __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim) |
| 5440 | { |
| 5441 | struct isl_basic_map *bmap; |
| 5442 | bmap = isl_basic_map_alloc_space(dim, 0, 1, 0); |
| 5443 | bmap = isl_basic_map_set_to_empty(bmap); |
| 5444 | return bmap; |
| 5445 | } |
| 5446 | |
| 5447 | __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim) |
| 5448 | { |
| 5449 | struct isl_basic_set *bset; |
| 5450 | bset = isl_basic_set_alloc_space(dim, 0, 1, 0); |
| 5451 | bset = isl_basic_set_set_to_empty(bset); |
| 5452 | return bset; |
| 5453 | } |
| 5454 | |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 5455 | __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim) |
| 5456 | { |
| 5457 | struct isl_basic_map *bmap; |
| 5458 | bmap = isl_basic_map_alloc_space(dim, 0, 0, 0); |
| 5459 | bmap = isl_basic_map_finalize(bmap); |
| 5460 | return bmap; |
| 5461 | } |
| 5462 | |
| 5463 | __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim) |
| 5464 | { |
| 5465 | struct isl_basic_set *bset; |
| 5466 | bset = isl_basic_set_alloc_space(dim, 0, 0, 0); |
| 5467 | bset = isl_basic_set_finalize(bset); |
| 5468 | return bset; |
| 5469 | } |
| 5470 | |
| 5471 | __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim) |
| 5472 | { |
| 5473 | int i; |
| 5474 | unsigned total = isl_space_dim(dim, isl_dim_all); |
| 5475 | isl_basic_map *bmap; |
| 5476 | |
| 5477 | bmap= isl_basic_map_alloc_space(dim, 0, 0, total); |
| 5478 | for (i = 0; i < total; ++i) { |
| 5479 | int k = isl_basic_map_alloc_inequality(bmap); |
| 5480 | if (k < 0) |
| 5481 | goto error; |
| 5482 | isl_seq_clr(bmap->ineq[k], 1 + total); |
| 5483 | isl_int_set_si(bmap->ineq[k][1 + i], 1); |
| 5484 | } |
| 5485 | return bmap; |
| 5486 | error: |
| 5487 | isl_basic_map_free(bmap); |
| 5488 | return NULL; |
| 5489 | } |
| 5490 | |
| 5491 | __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim) |
| 5492 | { |
| 5493 | return isl_basic_map_nat_universe(dim); |
| 5494 | } |
| 5495 | |
| 5496 | __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim) |
| 5497 | { |
| 5498 | return isl_map_from_basic_map(isl_basic_map_nat_universe(dim)); |
| 5499 | } |
| 5500 | |
| 5501 | __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim) |
| 5502 | { |
| 5503 | return isl_map_nat_universe(dim); |
| 5504 | } |
| 5505 | |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 5506 | __isl_give isl_map *isl_map_empty(__isl_take isl_space *dim) |
| 5507 | { |
| 5508 | return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT); |
| 5509 | } |
| 5510 | |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 5511 | __isl_give isl_set *isl_set_empty(__isl_take isl_space *dim) |
| 5512 | { |
| 5513 | return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT); |
| 5514 | } |
| 5515 | |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 5516 | __isl_give isl_map *isl_map_universe(__isl_take isl_space *dim) |
| 5517 | { |
| 5518 | struct isl_map *map; |
| 5519 | if (!dim) |
| 5520 | return NULL; |
| 5521 | map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT); |
| 5522 | map = isl_map_add_basic_map(map, isl_basic_map_universe(dim)); |
| 5523 | return map; |
| 5524 | } |
| 5525 | |
| 5526 | __isl_give isl_set *isl_set_universe(__isl_take isl_space *dim) |
| 5527 | { |
| 5528 | struct isl_set *set; |
| 5529 | if (!dim) |
| 5530 | return NULL; |
| 5531 | set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT); |
| 5532 | set = isl_set_add_basic_set(set, isl_basic_set_universe(dim)); |
| 5533 | return set; |
| 5534 | } |
| 5535 | |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 5536 | struct isl_map *isl_map_dup(struct isl_map *map) |
| 5537 | { |
| 5538 | int i; |
| 5539 | struct isl_map *dup; |
| 5540 | |
| 5541 | if (!map) |
| 5542 | return NULL; |
| 5543 | dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags); |
| 5544 | for (i = 0; i < map->n; ++i) |
| 5545 | dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i])); |
| 5546 | return dup; |
| 5547 | } |
| 5548 | |
| 5549 | __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map, |
| 5550 | __isl_take isl_basic_map *bmap) |
| 5551 | { |
| 5552 | if (!bmap || !map) |
| 5553 | goto error; |
| 5554 | if (isl_basic_map_plain_is_empty(bmap)) { |
| 5555 | isl_basic_map_free(bmap); |
| 5556 | return map; |
| 5557 | } |
| 5558 | isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error); |
| 5559 | isl_assert(map->ctx, map->n < map->size, goto error); |
| 5560 | map->p[map->n] = bmap; |
| 5561 | map->n++; |
| 5562 | ISL_F_CLR(map, ISL_MAP_NORMALIZED); |
| 5563 | return map; |
| 5564 | error: |
| 5565 | if (map) |
| 5566 | isl_map_free(map); |
| 5567 | if (bmap) |
| 5568 | isl_basic_map_free(bmap); |
| 5569 | return NULL; |
| 5570 | } |
| 5571 | |
| 5572 | __isl_null isl_map *isl_map_free(__isl_take isl_map *map) |
| 5573 | { |
| 5574 | int i; |
| 5575 | |
| 5576 | if (!map) |
| 5577 | return NULL; |
| 5578 | |
| 5579 | if (--map->ref > 0) |
| 5580 | return NULL; |
| 5581 | |
Tobias Grosser | 07b2095 | 2016-06-12 04:30:40 +0000 | [diff] [blame] | 5582 | clear_caches(map); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 5583 | isl_ctx_deref(map->ctx); |
| 5584 | for (i = 0; i < map->n; ++i) |
| 5585 | isl_basic_map_free(map->p[i]); |
| 5586 | isl_space_free(map->dim); |
| 5587 | free(map); |
| 5588 | |
| 5589 | return NULL; |
| 5590 | } |
| 5591 | |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 5592 | static struct isl_basic_map *isl_basic_map_fix_pos_si( |
| 5593 | struct isl_basic_map *bmap, unsigned pos, int value) |
| 5594 | { |
| 5595 | int j; |
| 5596 | |
| 5597 | bmap = isl_basic_map_cow(bmap); |
| 5598 | bmap = isl_basic_map_extend_constraints(bmap, 1, 0); |
| 5599 | j = isl_basic_map_alloc_equality(bmap); |
| 5600 | if (j < 0) |
| 5601 | goto error; |
| 5602 | isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap)); |
| 5603 | isl_int_set_si(bmap->eq[j][pos], -1); |
| 5604 | isl_int_set_si(bmap->eq[j][0], value); |
| 5605 | bmap = isl_basic_map_simplify(bmap); |
| 5606 | return isl_basic_map_finalize(bmap); |
| 5607 | error: |
| 5608 | isl_basic_map_free(bmap); |
| 5609 | return NULL; |
| 5610 | } |
| 5611 | |
| 5612 | static __isl_give isl_basic_map *isl_basic_map_fix_pos( |
| 5613 | __isl_take isl_basic_map *bmap, unsigned pos, isl_int value) |
| 5614 | { |
| 5615 | int j; |
| 5616 | |
| 5617 | bmap = isl_basic_map_cow(bmap); |
| 5618 | bmap = isl_basic_map_extend_constraints(bmap, 1, 0); |
| 5619 | j = isl_basic_map_alloc_equality(bmap); |
| 5620 | if (j < 0) |
| 5621 | goto error; |
| 5622 | isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap)); |
| 5623 | isl_int_set_si(bmap->eq[j][pos], -1); |
| 5624 | isl_int_set(bmap->eq[j][0], value); |
| 5625 | bmap = isl_basic_map_simplify(bmap); |
| 5626 | return isl_basic_map_finalize(bmap); |
| 5627 | error: |
| 5628 | isl_basic_map_free(bmap); |
| 5629 | return NULL; |
| 5630 | } |
| 5631 | |
| 5632 | struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap, |
| 5633 | enum isl_dim_type type, unsigned pos, int value) |
| 5634 | { |
| 5635 | if (!bmap) |
| 5636 | return NULL; |
| 5637 | isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error); |
| 5638 | return isl_basic_map_fix_pos_si(bmap, |
| 5639 | isl_basic_map_offset(bmap, type) + pos, value); |
| 5640 | error: |
| 5641 | isl_basic_map_free(bmap); |
| 5642 | return NULL; |
| 5643 | } |
| 5644 | |
| 5645 | __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap, |
| 5646 | enum isl_dim_type type, unsigned pos, isl_int value) |
| 5647 | { |
| 5648 | if (!bmap) |
| 5649 | return NULL; |
| 5650 | isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error); |
| 5651 | return isl_basic_map_fix_pos(bmap, |
| 5652 | isl_basic_map_offset(bmap, type) + pos, value); |
| 5653 | error: |
| 5654 | isl_basic_map_free(bmap); |
| 5655 | return NULL; |
| 5656 | } |
| 5657 | |
| 5658 | /* Fix the value of the variable at position "pos" of type "type" of "bmap" |
| 5659 | * to be equal to "v". |
| 5660 | */ |
| 5661 | __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap, |
| 5662 | enum isl_dim_type type, unsigned pos, __isl_take isl_val *v) |
| 5663 | { |
| 5664 | if (!bmap || !v) |
| 5665 | goto error; |
| 5666 | if (!isl_val_is_int(v)) |
| 5667 | isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid, |
| 5668 | "expecting integer value", goto error); |
| 5669 | if (pos >= isl_basic_map_dim(bmap, type)) |
| 5670 | isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid, |
| 5671 | "index out of bounds", goto error); |
| 5672 | pos += isl_basic_map_offset(bmap, type); |
| 5673 | bmap = isl_basic_map_fix_pos(bmap, pos, v->n); |
| 5674 | isl_val_free(v); |
| 5675 | return bmap; |
| 5676 | error: |
| 5677 | isl_basic_map_free(bmap); |
| 5678 | isl_val_free(v); |
| 5679 | return NULL; |
| 5680 | } |
| 5681 | |
| 5682 | /* Fix the value of the variable at position "pos" of type "type" of "bset" |
| 5683 | * to be equal to "v". |
| 5684 | */ |
| 5685 | __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset, |
| 5686 | enum isl_dim_type type, unsigned pos, __isl_take isl_val *v) |
| 5687 | { |
| 5688 | return isl_basic_map_fix_val(bset, type, pos, v); |
| 5689 | } |
| 5690 | |
| 5691 | struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset, |
| 5692 | enum isl_dim_type type, unsigned pos, int value) |
| 5693 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 5694 | return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset), |
| 5695 | type, pos, value)); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 5696 | } |
| 5697 | |
| 5698 | __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset, |
| 5699 | enum isl_dim_type type, unsigned pos, isl_int value) |
| 5700 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 5701 | return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset), |
| 5702 | type, pos, value)); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 5703 | } |
| 5704 | |
| 5705 | struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap, |
| 5706 | unsigned input, int value) |
| 5707 | { |
| 5708 | return isl_basic_map_fix_si(bmap, isl_dim_in, input, value); |
| 5709 | } |
| 5710 | |
| 5711 | struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset, |
| 5712 | unsigned dim, int value) |
| 5713 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 5714 | return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset), |
| 5715 | isl_dim_set, dim, value)); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 5716 | } |
| 5717 | |
| 5718 | static int remove_if_empty(__isl_keep isl_map *map, int i) |
| 5719 | { |
| 5720 | int empty = isl_basic_map_plain_is_empty(map->p[i]); |
| 5721 | |
| 5722 | if (empty < 0) |
| 5723 | return -1; |
| 5724 | if (!empty) |
| 5725 | return 0; |
| 5726 | |
| 5727 | isl_basic_map_free(map->p[i]); |
| 5728 | if (i != map->n - 1) { |
| 5729 | ISL_F_CLR(map, ISL_MAP_NORMALIZED); |
| 5730 | map->p[i] = map->p[map->n - 1]; |
| 5731 | } |
| 5732 | map->n--; |
| 5733 | |
| 5734 | return 0; |
| 5735 | } |
| 5736 | |
| 5737 | /* Perform "fn" on each basic map of "map", where we may not be holding |
| 5738 | * the only reference to "map". |
| 5739 | * In particular, "fn" should be a semantics preserving operation |
| 5740 | * that we want to apply to all copies of "map". We therefore need |
| 5741 | * to be careful not to modify "map" in a way that breaks "map" |
| 5742 | * in case anything goes wrong. |
| 5743 | */ |
| 5744 | __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map, |
| 5745 | __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap)) |
| 5746 | { |
| 5747 | struct isl_basic_map *bmap; |
| 5748 | int i; |
| 5749 | |
| 5750 | if (!map) |
| 5751 | return NULL; |
| 5752 | |
| 5753 | for (i = map->n - 1; i >= 0; --i) { |
| 5754 | bmap = isl_basic_map_copy(map->p[i]); |
| 5755 | bmap = fn(bmap); |
| 5756 | if (!bmap) |
| 5757 | goto error; |
| 5758 | isl_basic_map_free(map->p[i]); |
| 5759 | map->p[i] = bmap; |
| 5760 | if (remove_if_empty(map, i) < 0) |
| 5761 | goto error; |
| 5762 | } |
| 5763 | |
| 5764 | return map; |
| 5765 | error: |
| 5766 | isl_map_free(map); |
| 5767 | return NULL; |
| 5768 | } |
| 5769 | |
| 5770 | struct isl_map *isl_map_fix_si(struct isl_map *map, |
| 5771 | enum isl_dim_type type, unsigned pos, int value) |
| 5772 | { |
| 5773 | int i; |
| 5774 | |
| 5775 | map = isl_map_cow(map); |
| 5776 | if (!map) |
| 5777 | return NULL; |
| 5778 | |
| 5779 | isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error); |
| 5780 | for (i = map->n - 1; i >= 0; --i) { |
| 5781 | map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value); |
| 5782 | if (remove_if_empty(map, i) < 0) |
| 5783 | goto error; |
| 5784 | } |
| 5785 | ISL_F_CLR(map, ISL_MAP_NORMALIZED); |
| 5786 | return map; |
| 5787 | error: |
| 5788 | isl_map_free(map); |
| 5789 | return NULL; |
| 5790 | } |
| 5791 | |
| 5792 | __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set, |
| 5793 | enum isl_dim_type type, unsigned pos, int value) |
| 5794 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 5795 | return set_from_map(isl_map_fix_si(set_to_map(set), type, pos, value)); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 5796 | } |
| 5797 | |
| 5798 | __isl_give isl_map *isl_map_fix(__isl_take isl_map *map, |
| 5799 | enum isl_dim_type type, unsigned pos, isl_int value) |
| 5800 | { |
| 5801 | int i; |
| 5802 | |
| 5803 | map = isl_map_cow(map); |
| 5804 | if (!map) |
| 5805 | return NULL; |
| 5806 | |
| 5807 | isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error); |
| 5808 | for (i = 0; i < map->n; ++i) { |
| 5809 | map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value); |
| 5810 | if (!map->p[i]) |
| 5811 | goto error; |
| 5812 | } |
| 5813 | ISL_F_CLR(map, ISL_MAP_NORMALIZED); |
| 5814 | return map; |
| 5815 | error: |
| 5816 | isl_map_free(map); |
| 5817 | return NULL; |
| 5818 | } |
| 5819 | |
| 5820 | __isl_give isl_set *isl_set_fix(__isl_take isl_set *set, |
| 5821 | enum isl_dim_type type, unsigned pos, isl_int value) |
| 5822 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 5823 | return set_from_map(isl_map_fix(set_to_map(set), type, pos, value)); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 5824 | } |
| 5825 | |
| 5826 | /* Fix the value of the variable at position "pos" of type "type" of "map" |
| 5827 | * to be equal to "v". |
| 5828 | */ |
| 5829 | __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map, |
| 5830 | enum isl_dim_type type, unsigned pos, __isl_take isl_val *v) |
| 5831 | { |
| 5832 | int i; |
| 5833 | |
| 5834 | map = isl_map_cow(map); |
| 5835 | if (!map || !v) |
| 5836 | goto error; |
| 5837 | |
| 5838 | if (!isl_val_is_int(v)) |
| 5839 | isl_die(isl_map_get_ctx(map), isl_error_invalid, |
| 5840 | "expecting integer value", goto error); |
| 5841 | if (pos >= isl_map_dim(map, type)) |
| 5842 | isl_die(isl_map_get_ctx(map), isl_error_invalid, |
| 5843 | "index out of bounds", goto error); |
| 5844 | for (i = map->n - 1; i >= 0; --i) { |
| 5845 | map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos, |
| 5846 | isl_val_copy(v)); |
| 5847 | if (remove_if_empty(map, i) < 0) |
| 5848 | goto error; |
| 5849 | } |
| 5850 | ISL_F_CLR(map, ISL_MAP_NORMALIZED); |
| 5851 | isl_val_free(v); |
| 5852 | return map; |
| 5853 | error: |
| 5854 | isl_map_free(map); |
| 5855 | isl_val_free(v); |
| 5856 | return NULL; |
| 5857 | } |
| 5858 | |
| 5859 | /* Fix the value of the variable at position "pos" of type "type" of "set" |
| 5860 | * to be equal to "v". |
| 5861 | */ |
| 5862 | __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set, |
| 5863 | enum isl_dim_type type, unsigned pos, __isl_take isl_val *v) |
| 5864 | { |
| 5865 | return isl_map_fix_val(set, type, pos, v); |
| 5866 | } |
| 5867 | |
| 5868 | struct isl_map *isl_map_fix_input_si(struct isl_map *map, |
| 5869 | unsigned input, int value) |
| 5870 | { |
| 5871 | return isl_map_fix_si(map, isl_dim_in, input, value); |
| 5872 | } |
| 5873 | |
| 5874 | struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value) |
| 5875 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 5876 | return set_from_map(isl_map_fix_si(set_to_map(set), |
| 5877 | isl_dim_set, dim, value)); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 5878 | } |
| 5879 | |
| 5880 | static __isl_give isl_basic_map *basic_map_bound_si( |
| 5881 | __isl_take isl_basic_map *bmap, |
| 5882 | enum isl_dim_type type, unsigned pos, int value, int upper) |
| 5883 | { |
| 5884 | int j; |
| 5885 | |
| 5886 | if (!bmap) |
| 5887 | return NULL; |
| 5888 | isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error); |
| 5889 | pos += isl_basic_map_offset(bmap, type); |
| 5890 | bmap = isl_basic_map_cow(bmap); |
| 5891 | bmap = isl_basic_map_extend_constraints(bmap, 0, 1); |
| 5892 | j = isl_basic_map_alloc_inequality(bmap); |
| 5893 | if (j < 0) |
| 5894 | goto error; |
| 5895 | isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap)); |
| 5896 | if (upper) { |
| 5897 | isl_int_set_si(bmap->ineq[j][pos], -1); |
| 5898 | isl_int_set_si(bmap->ineq[j][0], value); |
| 5899 | } else { |
| 5900 | isl_int_set_si(bmap->ineq[j][pos], 1); |
| 5901 | isl_int_set_si(bmap->ineq[j][0], -value); |
| 5902 | } |
| 5903 | bmap = isl_basic_map_simplify(bmap); |
| 5904 | return isl_basic_map_finalize(bmap); |
| 5905 | error: |
| 5906 | isl_basic_map_free(bmap); |
| 5907 | return NULL; |
| 5908 | } |
| 5909 | |
| 5910 | __isl_give isl_basic_map *isl_basic_map_lower_bound_si( |
| 5911 | __isl_take isl_basic_map *bmap, |
| 5912 | enum isl_dim_type type, unsigned pos, int value) |
| 5913 | { |
| 5914 | return basic_map_bound_si(bmap, type, pos, value, 0); |
| 5915 | } |
| 5916 | |
| 5917 | /* Constrain the values of the given dimension to be no greater than "value". |
| 5918 | */ |
| 5919 | __isl_give isl_basic_map *isl_basic_map_upper_bound_si( |
| 5920 | __isl_take isl_basic_map *bmap, |
| 5921 | enum isl_dim_type type, unsigned pos, int value) |
| 5922 | { |
| 5923 | return basic_map_bound_si(bmap, type, pos, value, 1); |
| 5924 | } |
| 5925 | |
| 5926 | struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset, |
| 5927 | unsigned dim, isl_int value) |
| 5928 | { |
| 5929 | int j; |
| 5930 | |
| 5931 | bset = isl_basic_set_cow(bset); |
| 5932 | bset = isl_basic_set_extend_constraints(bset, 0, 1); |
| 5933 | j = isl_basic_set_alloc_inequality(bset); |
| 5934 | if (j < 0) |
| 5935 | goto error; |
| 5936 | isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset)); |
| 5937 | isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1); |
| 5938 | isl_int_neg(bset->ineq[j][0], value); |
| 5939 | bset = isl_basic_set_simplify(bset); |
| 5940 | return isl_basic_set_finalize(bset); |
| 5941 | error: |
| 5942 | isl_basic_set_free(bset); |
| 5943 | return NULL; |
| 5944 | } |
| 5945 | |
| 5946 | static __isl_give isl_map *map_bound_si(__isl_take isl_map *map, |
| 5947 | enum isl_dim_type type, unsigned pos, int value, int upper) |
| 5948 | { |
| 5949 | int i; |
| 5950 | |
| 5951 | map = isl_map_cow(map); |
| 5952 | if (!map) |
| 5953 | return NULL; |
| 5954 | |
| 5955 | isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error); |
| 5956 | for (i = 0; i < map->n; ++i) { |
| 5957 | map->p[i] = basic_map_bound_si(map->p[i], |
| 5958 | type, pos, value, upper); |
| 5959 | if (!map->p[i]) |
| 5960 | goto error; |
| 5961 | } |
| 5962 | ISL_F_CLR(map, ISL_MAP_NORMALIZED); |
| 5963 | return map; |
| 5964 | error: |
| 5965 | isl_map_free(map); |
| 5966 | return NULL; |
| 5967 | } |
| 5968 | |
| 5969 | __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map, |
| 5970 | enum isl_dim_type type, unsigned pos, int value) |
| 5971 | { |
| 5972 | return map_bound_si(map, type, pos, value, 0); |
| 5973 | } |
| 5974 | |
| 5975 | __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map, |
| 5976 | enum isl_dim_type type, unsigned pos, int value) |
| 5977 | { |
| 5978 | return map_bound_si(map, type, pos, value, 1); |
| 5979 | } |
| 5980 | |
| 5981 | __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set, |
| 5982 | enum isl_dim_type type, unsigned pos, int value) |
| 5983 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 5984 | return set_from_map(isl_map_lower_bound_si(set_to_map(set), |
| 5985 | type, pos, value)); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 5986 | } |
| 5987 | |
| 5988 | __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set, |
| 5989 | enum isl_dim_type type, unsigned pos, int value) |
| 5990 | { |
| 5991 | return isl_map_upper_bound_si(set, type, pos, value); |
| 5992 | } |
| 5993 | |
| 5994 | /* Bound the given variable of "bmap" from below (or above is "upper" |
| 5995 | * is set) to "value". |
| 5996 | */ |
| 5997 | static __isl_give isl_basic_map *basic_map_bound( |
| 5998 | __isl_take isl_basic_map *bmap, |
| 5999 | enum isl_dim_type type, unsigned pos, isl_int value, int upper) |
| 6000 | { |
| 6001 | int j; |
| 6002 | |
| 6003 | if (!bmap) |
| 6004 | return NULL; |
| 6005 | if (pos >= isl_basic_map_dim(bmap, type)) |
| 6006 | isl_die(bmap->ctx, isl_error_invalid, |
| 6007 | "index out of bounds", goto error); |
| 6008 | pos += isl_basic_map_offset(bmap, type); |
| 6009 | bmap = isl_basic_map_cow(bmap); |
| 6010 | bmap = isl_basic_map_extend_constraints(bmap, 0, 1); |
| 6011 | j = isl_basic_map_alloc_inequality(bmap); |
| 6012 | if (j < 0) |
| 6013 | goto error; |
| 6014 | isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap)); |
| 6015 | if (upper) { |
| 6016 | isl_int_set_si(bmap->ineq[j][pos], -1); |
| 6017 | isl_int_set(bmap->ineq[j][0], value); |
| 6018 | } else { |
| 6019 | isl_int_set_si(bmap->ineq[j][pos], 1); |
| 6020 | isl_int_neg(bmap->ineq[j][0], value); |
| 6021 | } |
| 6022 | bmap = isl_basic_map_simplify(bmap); |
| 6023 | return isl_basic_map_finalize(bmap); |
| 6024 | error: |
| 6025 | isl_basic_map_free(bmap); |
| 6026 | return NULL; |
| 6027 | } |
| 6028 | |
| 6029 | /* Bound the given variable of "map" from below (or above is "upper" |
| 6030 | * is set) to "value". |
| 6031 | */ |
| 6032 | static __isl_give isl_map *map_bound(__isl_take isl_map *map, |
| 6033 | enum isl_dim_type type, unsigned pos, isl_int value, int upper) |
| 6034 | { |
| 6035 | int i; |
| 6036 | |
| 6037 | map = isl_map_cow(map); |
| 6038 | if (!map) |
| 6039 | return NULL; |
| 6040 | |
| 6041 | if (pos >= isl_map_dim(map, type)) |
| 6042 | isl_die(map->ctx, isl_error_invalid, |
| 6043 | "index out of bounds", goto error); |
| 6044 | for (i = map->n - 1; i >= 0; --i) { |
| 6045 | map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper); |
| 6046 | if (remove_if_empty(map, i) < 0) |
| 6047 | goto error; |
| 6048 | } |
| 6049 | ISL_F_CLR(map, ISL_MAP_NORMALIZED); |
| 6050 | return map; |
| 6051 | error: |
| 6052 | isl_map_free(map); |
| 6053 | return NULL; |
| 6054 | } |
| 6055 | |
| 6056 | __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map, |
| 6057 | enum isl_dim_type type, unsigned pos, isl_int value) |
| 6058 | { |
| 6059 | return map_bound(map, type, pos, value, 0); |
| 6060 | } |
| 6061 | |
| 6062 | __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map, |
| 6063 | enum isl_dim_type type, unsigned pos, isl_int value) |
| 6064 | { |
| 6065 | return map_bound(map, type, pos, value, 1); |
| 6066 | } |
| 6067 | |
| 6068 | __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set, |
| 6069 | enum isl_dim_type type, unsigned pos, isl_int value) |
| 6070 | { |
| 6071 | return isl_map_lower_bound(set, type, pos, value); |
| 6072 | } |
| 6073 | |
| 6074 | __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set, |
| 6075 | enum isl_dim_type type, unsigned pos, isl_int value) |
| 6076 | { |
| 6077 | return isl_map_upper_bound(set, type, pos, value); |
| 6078 | } |
| 6079 | |
| 6080 | /* Force the values of the variable at position "pos" of type "type" of "set" |
| 6081 | * to be no smaller than "value". |
| 6082 | */ |
| 6083 | __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set, |
| 6084 | enum isl_dim_type type, unsigned pos, __isl_take isl_val *value) |
| 6085 | { |
| 6086 | if (!value) |
| 6087 | goto error; |
| 6088 | if (!isl_val_is_int(value)) |
| 6089 | isl_die(isl_set_get_ctx(set), isl_error_invalid, |
| 6090 | "expecting integer value", goto error); |
| 6091 | set = isl_set_lower_bound(set, type, pos, value->n); |
| 6092 | isl_val_free(value); |
| 6093 | return set; |
| 6094 | error: |
| 6095 | isl_val_free(value); |
| 6096 | isl_set_free(set); |
| 6097 | return NULL; |
| 6098 | } |
| 6099 | |
| 6100 | /* Force the values of the variable at position "pos" of type "type" of "set" |
| 6101 | * to be no greater than "value". |
| 6102 | */ |
| 6103 | __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set, |
| 6104 | enum isl_dim_type type, unsigned pos, __isl_take isl_val *value) |
| 6105 | { |
| 6106 | if (!value) |
| 6107 | goto error; |
| 6108 | if (!isl_val_is_int(value)) |
| 6109 | isl_die(isl_set_get_ctx(set), isl_error_invalid, |
| 6110 | "expecting integer value", goto error); |
| 6111 | set = isl_set_upper_bound(set, type, pos, value->n); |
| 6112 | isl_val_free(value); |
| 6113 | return set; |
| 6114 | error: |
| 6115 | isl_val_free(value); |
| 6116 | isl_set_free(set); |
| 6117 | return NULL; |
| 6118 | } |
| 6119 | |
| 6120 | struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim, |
| 6121 | isl_int value) |
| 6122 | { |
| 6123 | int i; |
| 6124 | |
| 6125 | set = isl_set_cow(set); |
| 6126 | if (!set) |
| 6127 | return NULL; |
| 6128 | |
| 6129 | isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error); |
| 6130 | for (i = 0; i < set->n; ++i) { |
| 6131 | set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value); |
| 6132 | if (!set->p[i]) |
| 6133 | goto error; |
| 6134 | } |
| 6135 | return set; |
| 6136 | error: |
| 6137 | isl_set_free(set); |
| 6138 | return NULL; |
| 6139 | } |
| 6140 | |
| 6141 | struct isl_map *isl_map_reverse(struct isl_map *map) |
| 6142 | { |
| 6143 | int i; |
| 6144 | |
| 6145 | map = isl_map_cow(map); |
| 6146 | if (!map) |
| 6147 | return NULL; |
| 6148 | |
| 6149 | map->dim = isl_space_reverse(map->dim); |
| 6150 | if (!map->dim) |
| 6151 | goto error; |
| 6152 | for (i = 0; i < map->n; ++i) { |
| 6153 | map->p[i] = isl_basic_map_reverse(map->p[i]); |
| 6154 | if (!map->p[i]) |
| 6155 | goto error; |
| 6156 | } |
| 6157 | ISL_F_CLR(map, ISL_MAP_NORMALIZED); |
| 6158 | return map; |
| 6159 | error: |
| 6160 | isl_map_free(map); |
| 6161 | return NULL; |
| 6162 | } |
| 6163 | |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 6164 | #undef TYPE |
| 6165 | #define TYPE isl_pw_multi_aff |
| 6166 | #undef SUFFIX |
| 6167 | #define SUFFIX _pw_multi_aff |
| 6168 | #undef EMPTY |
| 6169 | #define EMPTY isl_pw_multi_aff_empty |
| 6170 | #undef ADD |
| 6171 | #define ADD isl_pw_multi_aff_union_add |
| 6172 | #include "isl_map_lexopt_templ.c" |
| 6173 | |
| 6174 | /* Given a map "map", compute the lexicographically minimal |
| 6175 | * (or maximal) image element for each domain element in dom, |
| 6176 | * in the form of an isl_pw_multi_aff. |
Tobias Grosser | 37034db | 2016-03-25 19:38:18 +0000 | [diff] [blame] | 6177 | * If "empty" is not NULL, then set *empty to those elements in dom that |
| 6178 | * do not have an image element. |
Tobias Grosser | 932ec01 | 2016-07-06 09:11:00 +0000 | [diff] [blame] | 6179 | * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum |
| 6180 | * should be computed over the domain of "map". "empty" is also NULL |
| 6181 | * in this case. |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 6182 | * |
| 6183 | * We first compute the lexicographically minimal or maximal element |
| 6184 | * in the first basic map. This results in a partial solution "res" |
| 6185 | * and a subset "todo" of dom that still need to be handled. |
| 6186 | * We then consider each of the remaining maps in "map" and successively |
| 6187 | * update both "res" and "todo". |
Tobias Grosser | 37034db | 2016-03-25 19:38:18 +0000 | [diff] [blame] | 6188 | * If "empty" is NULL, then the todo sets are not needed and therefore |
| 6189 | * also not computed. |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 6190 | */ |
| 6191 | static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff( |
| 6192 | __isl_take isl_map *map, __isl_take isl_set *dom, |
Tobias Grosser | 932ec01 | 2016-07-06 09:11:00 +0000 | [diff] [blame] | 6193 | __isl_give isl_set **empty, unsigned flags) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 6194 | { |
| 6195 | int i; |
Tobias Grosser | 932ec01 | 2016-07-06 09:11:00 +0000 | [diff] [blame] | 6196 | int full; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 6197 | isl_pw_multi_aff *res; |
| 6198 | isl_set *todo; |
| 6199 | |
Tobias Grosser | 932ec01 | 2016-07-06 09:11:00 +0000 | [diff] [blame] | 6200 | full = ISL_FL_ISSET(flags, ISL_OPT_FULL); |
| 6201 | if (!map || (!full && !dom)) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 6202 | goto error; |
| 6203 | |
| 6204 | if (isl_map_plain_is_empty(map)) { |
| 6205 | if (empty) |
| 6206 | *empty = dom; |
| 6207 | else |
| 6208 | isl_set_free(dom); |
| 6209 | return isl_pw_multi_aff_from_map(map); |
| 6210 | } |
| 6211 | |
| 6212 | res = basic_map_partial_lexopt_pw_multi_aff( |
| 6213 | isl_basic_map_copy(map->p[0]), |
Tobias Grosser | 932ec01 | 2016-07-06 09:11:00 +0000 | [diff] [blame] | 6214 | isl_set_copy(dom), empty, flags); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 6215 | |
Tobias Grosser | 37034db | 2016-03-25 19:38:18 +0000 | [diff] [blame] | 6216 | if (empty) |
| 6217 | todo = *empty; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 6218 | for (i = 1; i < map->n; ++i) { |
| 6219 | isl_pw_multi_aff *res_i; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 6220 | |
| 6221 | res_i = basic_map_partial_lexopt_pw_multi_aff( |
| 6222 | isl_basic_map_copy(map->p[i]), |
Tobias Grosser | 932ec01 | 2016-07-06 09:11:00 +0000 | [diff] [blame] | 6223 | isl_set_copy(dom), empty, flags); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 6224 | |
Tobias Grosser | 932ec01 | 2016-07-06 09:11:00 +0000 | [diff] [blame] | 6225 | if (ISL_FL_ISSET(flags, ISL_OPT_MAX)) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 6226 | res = isl_pw_multi_aff_union_lexmax(res, res_i); |
| 6227 | else |
| 6228 | res = isl_pw_multi_aff_union_lexmin(res, res_i); |
| 6229 | |
Tobias Grosser | 37034db | 2016-03-25 19:38:18 +0000 | [diff] [blame] | 6230 | if (empty) |
| 6231 | todo = isl_set_intersect(todo, *empty); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 6232 | } |
| 6233 | |
| 6234 | isl_set_free(dom); |
| 6235 | isl_map_free(map); |
| 6236 | |
| 6237 | if (empty) |
| 6238 | *empty = todo; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 6239 | |
| 6240 | return res; |
| 6241 | error: |
| 6242 | if (empty) |
| 6243 | *empty = NULL; |
| 6244 | isl_set_free(dom); |
| 6245 | isl_map_free(map); |
| 6246 | return NULL; |
| 6247 | } |
| 6248 | |
| 6249 | #undef TYPE |
| 6250 | #define TYPE isl_map |
| 6251 | #undef SUFFIX |
| 6252 | #define SUFFIX |
| 6253 | #undef EMPTY |
| 6254 | #define EMPTY isl_map_empty |
| 6255 | #undef ADD |
| 6256 | #define ADD isl_map_union_disjoint |
| 6257 | #include "isl_map_lexopt_templ.c" |
| 6258 | |
| 6259 | /* Given a map "map", compute the lexicographically minimal |
Michael Kruse | e0b34f3 | 2016-05-04 14:41:36 +0000 | [diff] [blame] | 6260 | * (or maximal) image element for each domain element in "dom", |
| 6261 | * in the form of an isl_map. |
| 6262 | * If "empty" is not NULL, then set *empty to those elements in "dom" that |
| 6263 | * do not have an image element. |
Tobias Grosser | 932ec01 | 2016-07-06 09:11:00 +0000 | [diff] [blame] | 6264 | * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum |
| 6265 | * should be computed over the domain of "map". "empty" is also NULL |
| 6266 | * in this case. |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 6267 | * |
Michael Kruse | e0b34f3 | 2016-05-04 14:41:36 +0000 | [diff] [blame] | 6268 | * If the input consists of more than one disjunct, then first |
| 6269 | * compute the desired result in the form of an isl_pw_multi_aff and |
| 6270 | * then convert that into an isl_map. |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 6271 | * |
Michael Kruse | e0b34f3 | 2016-05-04 14:41:36 +0000 | [diff] [blame] | 6272 | * This function used to have an explicit implementation in terms |
| 6273 | * of isl_maps, but it would continually intersect the domains of |
| 6274 | * partial results with the complement of the domain of the next |
| 6275 | * partial solution, potentially leading to an explosion in the number |
| 6276 | * of disjuncts if there are several disjuncts in the input. |
| 6277 | * An even earlier implementation of this function would look for |
| 6278 | * better results in the domain of the partial result and for extra |
| 6279 | * results in the complement of this domain, which would lead to |
| 6280 | * even more splintering. |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 6281 | */ |
| 6282 | static __isl_give isl_map *isl_map_partial_lexopt_aligned( |
Tobias Grosser | 932ec01 | 2016-07-06 09:11:00 +0000 | [diff] [blame] | 6283 | __isl_take isl_map *map, __isl_take isl_set *dom, |
| 6284 | __isl_give isl_set **empty, unsigned flags) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 6285 | { |
Tobias Grosser | 932ec01 | 2016-07-06 09:11:00 +0000 | [diff] [blame] | 6286 | int full; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 6287 | struct isl_map *res; |
Michael Kruse | e0b34f3 | 2016-05-04 14:41:36 +0000 | [diff] [blame] | 6288 | isl_pw_multi_aff *pma; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 6289 | |
Tobias Grosser | 932ec01 | 2016-07-06 09:11:00 +0000 | [diff] [blame] | 6290 | full = ISL_FL_ISSET(flags, ISL_OPT_FULL); |
| 6291 | if (!map || (!full && !dom)) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 6292 | goto error; |
| 6293 | |
| 6294 | if (isl_map_plain_is_empty(map)) { |
| 6295 | if (empty) |
| 6296 | *empty = dom; |
| 6297 | else |
| 6298 | isl_set_free(dom); |
| 6299 | return map; |
| 6300 | } |
| 6301 | |
Michael Kruse | e0b34f3 | 2016-05-04 14:41:36 +0000 | [diff] [blame] | 6302 | if (map->n == 1) { |
| 6303 | res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]), |
Tobias Grosser | 932ec01 | 2016-07-06 09:11:00 +0000 | [diff] [blame] | 6304 | dom, empty, flags); |
Michael Kruse | e0b34f3 | 2016-05-04 14:41:36 +0000 | [diff] [blame] | 6305 | isl_map_free(map); |
| 6306 | return res; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 6307 | } |
| 6308 | |
Tobias Grosser | 932ec01 | 2016-07-06 09:11:00 +0000 | [diff] [blame] | 6309 | pma = isl_map_partial_lexopt_aligned_pw_multi_aff(map, dom, empty, |
| 6310 | flags); |
Michael Kruse | e0b34f3 | 2016-05-04 14:41:36 +0000 | [diff] [blame] | 6311 | return isl_map_from_pw_multi_aff(pma); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 6312 | error: |
| 6313 | if (empty) |
| 6314 | *empty = NULL; |
| 6315 | isl_set_free(dom); |
| 6316 | isl_map_free(map); |
| 6317 | return NULL; |
| 6318 | } |
| 6319 | |
| 6320 | __isl_give isl_map *isl_map_partial_lexmax( |
| 6321 | __isl_take isl_map *map, __isl_take isl_set *dom, |
| 6322 | __isl_give isl_set **empty) |
| 6323 | { |
Tobias Grosser | 932ec01 | 2016-07-06 09:11:00 +0000 | [diff] [blame] | 6324 | return isl_map_partial_lexopt(map, dom, empty, ISL_OPT_MAX); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 6325 | } |
| 6326 | |
| 6327 | __isl_give isl_map *isl_map_partial_lexmin( |
| 6328 | __isl_take isl_map *map, __isl_take isl_set *dom, |
| 6329 | __isl_give isl_set **empty) |
| 6330 | { |
| 6331 | return isl_map_partial_lexopt(map, dom, empty, 0); |
| 6332 | } |
| 6333 | |
| 6334 | __isl_give isl_set *isl_set_partial_lexmin( |
| 6335 | __isl_take isl_set *set, __isl_take isl_set *dom, |
| 6336 | __isl_give isl_set **empty) |
| 6337 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 6338 | return set_from_map(isl_map_partial_lexmin(set_to_map(set), |
| 6339 | dom, empty)); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 6340 | } |
| 6341 | |
| 6342 | __isl_give isl_set *isl_set_partial_lexmax( |
| 6343 | __isl_take isl_set *set, __isl_take isl_set *dom, |
| 6344 | __isl_give isl_set **empty) |
| 6345 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 6346 | return set_from_map(isl_map_partial_lexmax(set_to_map(set), |
| 6347 | dom, empty)); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 6348 | } |
| 6349 | |
Tobias Grosser | 932ec01 | 2016-07-06 09:11:00 +0000 | [diff] [blame] | 6350 | /* Compute the lexicographic minimum (or maximum if "flags" includes |
| 6351 | * ISL_OPT_MAX) of "bset" over its parametric domain. |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 6352 | */ |
Tobias Grosser | 932ec01 | 2016-07-06 09:11:00 +0000 | [diff] [blame] | 6353 | __isl_give isl_set *isl_basic_set_lexopt(__isl_take isl_basic_set *bset, |
| 6354 | unsigned flags) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 6355 | { |
Tobias Grosser | 932ec01 | 2016-07-06 09:11:00 +0000 | [diff] [blame] | 6356 | return isl_basic_map_lexopt(bset, flags); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 6357 | } |
| 6358 | |
| 6359 | __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap) |
| 6360 | { |
Tobias Grosser | 932ec01 | 2016-07-06 09:11:00 +0000 | [diff] [blame] | 6361 | return isl_basic_map_lexopt(bmap, ISL_OPT_MAX); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 6362 | } |
| 6363 | |
| 6364 | __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset) |
| 6365 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 6366 | return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset))); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 6367 | } |
| 6368 | |
| 6369 | __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset) |
| 6370 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 6371 | return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset))); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 6372 | } |
| 6373 | |
Tobias Grosser | 932ec01 | 2016-07-06 09:11:00 +0000 | [diff] [blame] | 6374 | /* Compute the lexicographic minimum of "bset" over its parametric domain |
| 6375 | * for the purpose of quantifier elimination. |
| 6376 | * That is, find an explicit representation for all the existentially |
| 6377 | * quantified variables in "bset" by computing their lexicographic |
| 6378 | * minimum. |
| 6379 | */ |
| 6380 | static __isl_give isl_set *isl_basic_set_lexmin_compute_divs( |
| 6381 | __isl_take isl_basic_set *bset) |
| 6382 | { |
| 6383 | return isl_basic_set_lexopt(bset, ISL_OPT_QE); |
| 6384 | } |
| 6385 | |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 6386 | /* Extract the first and only affine expression from list |
| 6387 | * and then add it to *pwaff with the given dom. |
| 6388 | * This domain is known to be disjoint from other domains |
| 6389 | * because of the way isl_basic_map_foreach_lexmax works. |
| 6390 | */ |
| 6391 | static int update_dim_opt(__isl_take isl_basic_set *dom, |
| 6392 | __isl_take isl_aff_list *list, void *user) |
| 6393 | { |
| 6394 | isl_ctx *ctx = isl_basic_set_get_ctx(dom); |
| 6395 | isl_aff *aff; |
| 6396 | isl_pw_aff **pwaff = user; |
| 6397 | isl_pw_aff *pwaff_i; |
| 6398 | |
| 6399 | if (!list) |
| 6400 | goto error; |
| 6401 | if (isl_aff_list_n_aff(list) != 1) |
| 6402 | isl_die(ctx, isl_error_internal, |
| 6403 | "expecting single element list", goto error); |
| 6404 | |
| 6405 | aff = isl_aff_list_get_aff(list, 0); |
| 6406 | pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff); |
| 6407 | |
| 6408 | *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i); |
| 6409 | |
| 6410 | isl_aff_list_free(list); |
| 6411 | |
| 6412 | return 0; |
| 6413 | error: |
| 6414 | isl_basic_set_free(dom); |
| 6415 | isl_aff_list_free(list); |
| 6416 | return -1; |
| 6417 | } |
| 6418 | |
| 6419 | /* Given a basic map with one output dimension, compute the minimum or |
| 6420 | * maximum of that dimension as an isl_pw_aff. |
| 6421 | * |
| 6422 | * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt |
| 6423 | * call update_dim_opt on each leaf of the result. |
| 6424 | */ |
| 6425 | static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap, |
| 6426 | int max) |
| 6427 | { |
| 6428 | isl_space *dim = isl_basic_map_get_space(bmap); |
| 6429 | isl_pw_aff *pwaff; |
| 6430 | int r; |
| 6431 | |
| 6432 | dim = isl_space_from_domain(isl_space_domain(dim)); |
| 6433 | dim = isl_space_add_dims(dim, isl_dim_out, 1); |
| 6434 | pwaff = isl_pw_aff_empty(dim); |
| 6435 | |
| 6436 | r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff); |
| 6437 | if (r < 0) |
| 6438 | return isl_pw_aff_free(pwaff); |
| 6439 | |
| 6440 | return pwaff; |
| 6441 | } |
| 6442 | |
| 6443 | /* Compute the minimum or maximum of the given output dimension |
| 6444 | * as a function of the parameters and the input dimensions, |
| 6445 | * but independently of the other output dimensions. |
| 6446 | * |
| 6447 | * We first project out the other output dimension and then compute |
| 6448 | * the "lexicographic" maximum in each basic map, combining the results |
| 6449 | * using isl_pw_aff_union_max. |
| 6450 | */ |
| 6451 | static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos, |
| 6452 | int max) |
| 6453 | { |
| 6454 | int i; |
| 6455 | isl_pw_aff *pwaff; |
| 6456 | unsigned n_out; |
| 6457 | |
| 6458 | n_out = isl_map_dim(map, isl_dim_out); |
| 6459 | map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1)); |
| 6460 | map = isl_map_project_out(map, isl_dim_out, 0, pos); |
| 6461 | if (!map) |
| 6462 | return NULL; |
| 6463 | |
| 6464 | if (map->n == 0) { |
| 6465 | isl_space *dim = isl_map_get_space(map); |
| 6466 | isl_map_free(map); |
| 6467 | return isl_pw_aff_empty(dim); |
| 6468 | } |
| 6469 | |
| 6470 | pwaff = basic_map_dim_opt(map->p[0], max); |
| 6471 | for (i = 1; i < map->n; ++i) { |
| 6472 | isl_pw_aff *pwaff_i; |
| 6473 | |
| 6474 | pwaff_i = basic_map_dim_opt(map->p[i], max); |
| 6475 | pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max); |
| 6476 | } |
| 6477 | |
| 6478 | isl_map_free(map); |
| 6479 | |
| 6480 | return pwaff; |
| 6481 | } |
| 6482 | |
Tobias Grosser | 932ec01 | 2016-07-06 09:11:00 +0000 | [diff] [blame] | 6483 | /* Compute the minimum of the given output dimension as a function of the |
| 6484 | * parameters and input dimensions, but independently of |
| 6485 | * the other output dimensions. |
| 6486 | */ |
| 6487 | __isl_give isl_pw_aff *isl_map_dim_min(__isl_take isl_map *map, int pos) |
| 6488 | { |
| 6489 | return map_dim_opt(map, pos, 0); |
| 6490 | } |
| 6491 | |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 6492 | /* Compute the maximum of the given output dimension as a function of the |
| 6493 | * parameters and input dimensions, but independently of |
| 6494 | * the other output dimensions. |
| 6495 | */ |
| 6496 | __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos) |
| 6497 | { |
| 6498 | return map_dim_opt(map, pos, 1); |
| 6499 | } |
| 6500 | |
| 6501 | /* Compute the minimum or maximum of the given set dimension |
| 6502 | * as a function of the parameters, |
| 6503 | * but independently of the other set dimensions. |
| 6504 | */ |
| 6505 | static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos, |
| 6506 | int max) |
| 6507 | { |
| 6508 | return map_dim_opt(set, pos, max); |
| 6509 | } |
| 6510 | |
| 6511 | /* Compute the maximum of the given set dimension as a function of the |
| 6512 | * parameters, but independently of the other set dimensions. |
| 6513 | */ |
| 6514 | __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos) |
| 6515 | { |
| 6516 | return set_dim_opt(set, pos, 1); |
| 6517 | } |
| 6518 | |
| 6519 | /* Compute the minimum of the given set dimension as a function of the |
| 6520 | * parameters, but independently of the other set dimensions. |
| 6521 | */ |
| 6522 | __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos) |
| 6523 | { |
| 6524 | return set_dim_opt(set, pos, 0); |
| 6525 | } |
| 6526 | |
| 6527 | /* Apply a preimage specified by "mat" on the parameters of "bset". |
| 6528 | * bset is assumed to have only parameters and divs. |
| 6529 | */ |
| 6530 | static struct isl_basic_set *basic_set_parameter_preimage( |
| 6531 | struct isl_basic_set *bset, struct isl_mat *mat) |
| 6532 | { |
| 6533 | unsigned nparam; |
| 6534 | |
| 6535 | if (!bset || !mat) |
| 6536 | goto error; |
| 6537 | |
| 6538 | bset->dim = isl_space_cow(bset->dim); |
| 6539 | if (!bset->dim) |
| 6540 | goto error; |
| 6541 | |
| 6542 | nparam = isl_basic_set_dim(bset, isl_dim_param); |
| 6543 | |
| 6544 | isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error); |
| 6545 | |
| 6546 | bset->dim->nparam = 0; |
| 6547 | bset->dim->n_out = nparam; |
| 6548 | bset = isl_basic_set_preimage(bset, mat); |
| 6549 | if (bset) { |
| 6550 | bset->dim->nparam = bset->dim->n_out; |
| 6551 | bset->dim->n_out = 0; |
| 6552 | } |
| 6553 | return bset; |
| 6554 | error: |
| 6555 | isl_mat_free(mat); |
| 6556 | isl_basic_set_free(bset); |
| 6557 | return NULL; |
| 6558 | } |
| 6559 | |
| 6560 | /* Apply a preimage specified by "mat" on the parameters of "set". |
| 6561 | * set is assumed to have only parameters and divs. |
| 6562 | */ |
Tobias Grosser | 37034db | 2016-03-25 19:38:18 +0000 | [diff] [blame] | 6563 | static __isl_give isl_set *set_parameter_preimage(__isl_take isl_set *set, |
| 6564 | __isl_take isl_mat *mat) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 6565 | { |
Tobias Grosser | 37034db | 2016-03-25 19:38:18 +0000 | [diff] [blame] | 6566 | isl_space *space; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 6567 | unsigned nparam; |
| 6568 | |
| 6569 | if (!set || !mat) |
| 6570 | goto error; |
| 6571 | |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 6572 | nparam = isl_set_dim(set, isl_dim_param); |
| 6573 | |
Tobias Grosser | 37034db | 2016-03-25 19:38:18 +0000 | [diff] [blame] | 6574 | if (mat->n_row != 1 + nparam) |
| 6575 | isl_die(isl_set_get_ctx(set), isl_error_internal, |
| 6576 | "unexpected number of rows", goto error); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 6577 | |
Tobias Grosser | 37034db | 2016-03-25 19:38:18 +0000 | [diff] [blame] | 6578 | space = isl_set_get_space(set); |
| 6579 | space = isl_space_move_dims(space, isl_dim_set, 0, |
| 6580 | isl_dim_param, 0, nparam); |
| 6581 | set = isl_set_reset_space(set, space); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 6582 | set = isl_set_preimage(set, mat); |
Tobias Grosser | 37034db | 2016-03-25 19:38:18 +0000 | [diff] [blame] | 6583 | nparam = isl_set_dim(set, isl_dim_out); |
| 6584 | space = isl_set_get_space(set); |
| 6585 | space = isl_space_move_dims(space, isl_dim_param, 0, |
| 6586 | isl_dim_out, 0, nparam); |
| 6587 | set = isl_set_reset_space(set, space); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 6588 | return set; |
| 6589 | error: |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 6590 | isl_mat_free(mat); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 6591 | isl_set_free(set); |
| 6592 | return NULL; |
| 6593 | } |
| 6594 | |
| 6595 | /* Intersect the basic set "bset" with the affine space specified by the |
| 6596 | * equalities in "eq". |
| 6597 | */ |
| 6598 | static struct isl_basic_set *basic_set_append_equalities( |
| 6599 | struct isl_basic_set *bset, struct isl_mat *eq) |
| 6600 | { |
| 6601 | int i, k; |
| 6602 | unsigned len; |
| 6603 | |
| 6604 | if (!bset || !eq) |
| 6605 | goto error; |
| 6606 | |
| 6607 | bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0, |
| 6608 | eq->n_row, 0); |
| 6609 | if (!bset) |
| 6610 | goto error; |
| 6611 | |
| 6612 | len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra; |
| 6613 | for (i = 0; i < eq->n_row; ++i) { |
| 6614 | k = isl_basic_set_alloc_equality(bset); |
| 6615 | if (k < 0) |
| 6616 | goto error; |
| 6617 | isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col); |
| 6618 | isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col); |
| 6619 | } |
| 6620 | isl_mat_free(eq); |
| 6621 | |
| 6622 | bset = isl_basic_set_gauss(bset, NULL); |
| 6623 | bset = isl_basic_set_finalize(bset); |
| 6624 | |
| 6625 | return bset; |
| 6626 | error: |
| 6627 | isl_mat_free(eq); |
| 6628 | isl_basic_set_free(bset); |
| 6629 | return NULL; |
| 6630 | } |
| 6631 | |
| 6632 | /* Intersect the set "set" with the affine space specified by the |
| 6633 | * equalities in "eq". |
| 6634 | */ |
| 6635 | static struct isl_set *set_append_equalities(struct isl_set *set, |
| 6636 | struct isl_mat *eq) |
| 6637 | { |
| 6638 | int i; |
| 6639 | |
| 6640 | if (!set || !eq) |
| 6641 | goto error; |
| 6642 | |
| 6643 | for (i = 0; i < set->n; ++i) { |
| 6644 | set->p[i] = basic_set_append_equalities(set->p[i], |
| 6645 | isl_mat_copy(eq)); |
| 6646 | if (!set->p[i]) |
| 6647 | goto error; |
| 6648 | } |
| 6649 | isl_mat_free(eq); |
| 6650 | return set; |
| 6651 | error: |
| 6652 | isl_mat_free(eq); |
| 6653 | isl_set_free(set); |
| 6654 | return NULL; |
| 6655 | } |
| 6656 | |
| 6657 | /* Given a basic set "bset" that only involves parameters and existentially |
| 6658 | * quantified variables, return the index of the first equality |
| 6659 | * that only involves parameters. If there is no such equality then |
| 6660 | * return bset->n_eq. |
| 6661 | * |
| 6662 | * This function assumes that isl_basic_set_gauss has been called on "bset". |
| 6663 | */ |
| 6664 | static int first_parameter_equality(__isl_keep isl_basic_set *bset) |
| 6665 | { |
| 6666 | int i, j; |
| 6667 | unsigned nparam, n_div; |
| 6668 | |
| 6669 | if (!bset) |
| 6670 | return -1; |
| 6671 | |
| 6672 | nparam = isl_basic_set_dim(bset, isl_dim_param); |
| 6673 | n_div = isl_basic_set_dim(bset, isl_dim_div); |
| 6674 | |
| 6675 | for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) { |
| 6676 | if (!isl_int_is_zero(bset->eq[i][1 + nparam + j])) |
| 6677 | ++i; |
| 6678 | } |
| 6679 | |
| 6680 | return i; |
| 6681 | } |
| 6682 | |
| 6683 | /* Compute an explicit representation for the existentially quantified |
| 6684 | * variables in "bset" by computing the "minimal value" of the set |
| 6685 | * variables. Since there are no set variables, the computation of |
| 6686 | * the minimal value essentially computes an explicit representation |
| 6687 | * of the non-empty part(s) of "bset". |
| 6688 | * |
| 6689 | * The input only involves parameters and existentially quantified variables. |
| 6690 | * All equalities among parameters have been removed. |
| 6691 | * |
| 6692 | * Since the existentially quantified variables in the result are in general |
| 6693 | * going to be different from those in the input, we first replace |
| 6694 | * them by the minimal number of variables based on their equalities. |
| 6695 | * This should simplify the parametric integer programming. |
| 6696 | */ |
| 6697 | static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset) |
| 6698 | { |
| 6699 | isl_morph *morph1, *morph2; |
| 6700 | isl_set *set; |
| 6701 | unsigned n; |
| 6702 | |
| 6703 | if (!bset) |
| 6704 | return NULL; |
| 6705 | if (bset->n_eq == 0) |
Tobias Grosser | 932ec01 | 2016-07-06 09:11:00 +0000 | [diff] [blame] | 6706 | return isl_basic_set_lexmin_compute_divs(bset); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 6707 | |
| 6708 | morph1 = isl_basic_set_parameter_compression(bset); |
| 6709 | bset = isl_morph_basic_set(isl_morph_copy(morph1), bset); |
| 6710 | bset = isl_basic_set_lift(bset); |
| 6711 | morph2 = isl_basic_set_variable_compression(bset, isl_dim_set); |
| 6712 | bset = isl_morph_basic_set(morph2, bset); |
| 6713 | n = isl_basic_set_dim(bset, isl_dim_set); |
| 6714 | bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n); |
| 6715 | |
Tobias Grosser | 932ec01 | 2016-07-06 09:11:00 +0000 | [diff] [blame] | 6716 | set = isl_basic_set_lexmin_compute_divs(bset); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 6717 | |
| 6718 | set = isl_morph_set(isl_morph_inverse(morph1), set); |
| 6719 | |
| 6720 | return set; |
| 6721 | } |
| 6722 | |
| 6723 | /* Project the given basic set onto its parameter domain, possibly introducing |
| 6724 | * new, explicit, existential variables in the constraints. |
| 6725 | * The input has parameters and (possibly implicit) existential variables. |
| 6726 | * The output has the same parameters, but only |
| 6727 | * explicit existentially quantified variables. |
| 6728 | * |
| 6729 | * The actual projection is performed by pip, but pip doesn't seem |
| 6730 | * to like equalities very much, so we first remove the equalities |
| 6731 | * among the parameters by performing a variable compression on |
| 6732 | * the parameters. Afterward, an inverse transformation is performed |
| 6733 | * and the equalities among the parameters are inserted back in. |
| 6734 | * |
| 6735 | * The variable compression on the parameters may uncover additional |
| 6736 | * equalities that were only implicit before. We therefore check |
| 6737 | * if there are any new parameter equalities in the result and |
| 6738 | * if so recurse. The removal of parameter equalities is required |
| 6739 | * for the parameter compression performed by base_compute_divs. |
| 6740 | */ |
| 6741 | static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset) |
| 6742 | { |
| 6743 | int i; |
| 6744 | struct isl_mat *eq; |
| 6745 | struct isl_mat *T, *T2; |
| 6746 | struct isl_set *set; |
| 6747 | unsigned nparam; |
| 6748 | |
| 6749 | bset = isl_basic_set_cow(bset); |
| 6750 | if (!bset) |
| 6751 | return NULL; |
| 6752 | |
| 6753 | if (bset->n_eq == 0) |
| 6754 | return base_compute_divs(bset); |
| 6755 | |
| 6756 | bset = isl_basic_set_gauss(bset, NULL); |
| 6757 | if (!bset) |
| 6758 | return NULL; |
| 6759 | if (isl_basic_set_plain_is_empty(bset)) |
| 6760 | return isl_set_from_basic_set(bset); |
| 6761 | |
| 6762 | i = first_parameter_equality(bset); |
| 6763 | if (i == bset->n_eq) |
| 6764 | return base_compute_divs(bset); |
| 6765 | |
| 6766 | nparam = isl_basic_set_dim(bset, isl_dim_param); |
| 6767 | eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i, |
| 6768 | 0, 1 + nparam); |
| 6769 | eq = isl_mat_cow(eq); |
| 6770 | T = isl_mat_variable_compression(isl_mat_copy(eq), &T2); |
| 6771 | if (T && T->n_col == 0) { |
| 6772 | isl_mat_free(T); |
| 6773 | isl_mat_free(T2); |
| 6774 | isl_mat_free(eq); |
| 6775 | bset = isl_basic_set_set_to_empty(bset); |
| 6776 | return isl_set_from_basic_set(bset); |
| 6777 | } |
| 6778 | bset = basic_set_parameter_preimage(bset, T); |
| 6779 | |
| 6780 | i = first_parameter_equality(bset); |
| 6781 | if (!bset) |
| 6782 | set = NULL; |
| 6783 | else if (i == bset->n_eq) |
| 6784 | set = base_compute_divs(bset); |
| 6785 | else |
| 6786 | set = parameter_compute_divs(bset); |
| 6787 | set = set_parameter_preimage(set, T2); |
| 6788 | set = set_append_equalities(set, eq); |
| 6789 | return set; |
| 6790 | } |
| 6791 | |
| 6792 | /* Insert the divs from "ls" before those of "bmap". |
| 6793 | * |
| 6794 | * The number of columns is not changed, which means that the last |
| 6795 | * dimensions of "bmap" are being reintepreted as the divs from "ls". |
| 6796 | * The caller is responsible for removing the same number of dimensions |
| 6797 | * from the space of "bmap". |
| 6798 | */ |
| 6799 | static __isl_give isl_basic_map *insert_divs_from_local_space( |
| 6800 | __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls) |
| 6801 | { |
| 6802 | int i; |
| 6803 | int n_div; |
| 6804 | int old_n_div; |
| 6805 | |
| 6806 | n_div = isl_local_space_dim(ls, isl_dim_div); |
| 6807 | if (n_div == 0) |
| 6808 | return bmap; |
| 6809 | |
| 6810 | old_n_div = bmap->n_div; |
| 6811 | bmap = insert_div_rows(bmap, n_div); |
| 6812 | if (!bmap) |
| 6813 | return NULL; |
| 6814 | |
| 6815 | for (i = 0; i < n_div; ++i) { |
| 6816 | isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col); |
| 6817 | isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div); |
| 6818 | } |
| 6819 | |
| 6820 | return bmap; |
| 6821 | } |
| 6822 | |
| 6823 | /* Replace the space of "bmap" by the space and divs of "ls". |
| 6824 | * |
| 6825 | * If "ls" has any divs, then we simplify the result since we may |
| 6826 | * have discovered some additional equalities that could simplify |
| 6827 | * the div expressions. |
| 6828 | */ |
| 6829 | static __isl_give isl_basic_map *basic_replace_space_by_local_space( |
| 6830 | __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls) |
| 6831 | { |
| 6832 | int n_div; |
| 6833 | |
| 6834 | bmap = isl_basic_map_cow(bmap); |
| 6835 | if (!bmap || !ls) |
| 6836 | goto error; |
| 6837 | |
| 6838 | n_div = isl_local_space_dim(ls, isl_dim_div); |
| 6839 | bmap = insert_divs_from_local_space(bmap, ls); |
| 6840 | if (!bmap) |
| 6841 | goto error; |
| 6842 | |
| 6843 | isl_space_free(bmap->dim); |
| 6844 | bmap->dim = isl_local_space_get_space(ls); |
| 6845 | if (!bmap->dim) |
| 6846 | goto error; |
| 6847 | |
| 6848 | isl_local_space_free(ls); |
| 6849 | if (n_div > 0) |
| 6850 | bmap = isl_basic_map_simplify(bmap); |
| 6851 | bmap = isl_basic_map_finalize(bmap); |
| 6852 | return bmap; |
| 6853 | error: |
| 6854 | isl_basic_map_free(bmap); |
| 6855 | isl_local_space_free(ls); |
| 6856 | return NULL; |
| 6857 | } |
| 6858 | |
| 6859 | /* Replace the space of "map" by the space and divs of "ls". |
| 6860 | */ |
| 6861 | static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map, |
| 6862 | __isl_take isl_local_space *ls) |
| 6863 | { |
| 6864 | int i; |
| 6865 | |
| 6866 | map = isl_map_cow(map); |
| 6867 | if (!map || !ls) |
| 6868 | goto error; |
| 6869 | |
| 6870 | for (i = 0; i < map->n; ++i) { |
| 6871 | map->p[i] = basic_replace_space_by_local_space(map->p[i], |
| 6872 | isl_local_space_copy(ls)); |
| 6873 | if (!map->p[i]) |
| 6874 | goto error; |
| 6875 | } |
| 6876 | isl_space_free(map->dim); |
| 6877 | map->dim = isl_local_space_get_space(ls); |
| 6878 | if (!map->dim) |
| 6879 | goto error; |
| 6880 | |
| 6881 | isl_local_space_free(ls); |
| 6882 | return map; |
| 6883 | error: |
| 6884 | isl_local_space_free(ls); |
| 6885 | isl_map_free(map); |
| 6886 | return NULL; |
| 6887 | } |
| 6888 | |
| 6889 | /* Compute an explicit representation for the existentially |
| 6890 | * quantified variables for which do not know any explicit representation yet. |
| 6891 | * |
| 6892 | * We first sort the existentially quantified variables so that the |
| 6893 | * existentially quantified variables for which we already have an explicit |
| 6894 | * representation are placed before those for which we do not. |
| 6895 | * The input dimensions, the output dimensions and the existentially |
| 6896 | * quantified variables for which we already have an explicit |
| 6897 | * representation are then turned into parameters. |
| 6898 | * compute_divs returns a map with the same parameters and |
| 6899 | * no input or output dimensions and the dimension specification |
| 6900 | * is reset to that of the input, including the existentially quantified |
| 6901 | * variables for which we already had an explicit representation. |
| 6902 | */ |
| 6903 | static struct isl_map *compute_divs(struct isl_basic_map *bmap) |
| 6904 | { |
| 6905 | struct isl_basic_set *bset; |
| 6906 | struct isl_set *set; |
| 6907 | struct isl_map *map; |
| 6908 | isl_space *dim; |
| 6909 | isl_local_space *ls; |
| 6910 | unsigned nparam; |
| 6911 | unsigned n_in; |
| 6912 | unsigned n_out; |
Tobias Grosser | 37034db | 2016-03-25 19:38:18 +0000 | [diff] [blame] | 6913 | int n_known; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 6914 | int i; |
| 6915 | |
| 6916 | bmap = isl_basic_map_sort_divs(bmap); |
| 6917 | bmap = isl_basic_map_cow(bmap); |
| 6918 | if (!bmap) |
| 6919 | return NULL; |
| 6920 | |
Tobias Grosser | 37034db | 2016-03-25 19:38:18 +0000 | [diff] [blame] | 6921 | n_known = isl_basic_map_first_unknown_div(bmap); |
| 6922 | if (n_known < 0) |
| 6923 | return isl_map_from_basic_map(isl_basic_map_free(bmap)); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 6924 | |
| 6925 | nparam = isl_basic_map_dim(bmap, isl_dim_param); |
| 6926 | n_in = isl_basic_map_dim(bmap, isl_dim_in); |
| 6927 | n_out = isl_basic_map_dim(bmap, isl_dim_out); |
| 6928 | dim = isl_space_set_alloc(bmap->ctx, |
| 6929 | nparam + n_in + n_out + n_known, 0); |
| 6930 | if (!dim) |
| 6931 | goto error; |
| 6932 | |
| 6933 | ls = isl_basic_map_get_local_space(bmap); |
| 6934 | ls = isl_local_space_drop_dims(ls, isl_dim_div, |
| 6935 | n_known, bmap->n_div - n_known); |
| 6936 | if (n_known > 0) { |
| 6937 | for (i = n_known; i < bmap->n_div; ++i) |
| 6938 | swap_div(bmap, i - n_known, i); |
| 6939 | bmap->n_div -= n_known; |
| 6940 | bmap->extra -= n_known; |
| 6941 | } |
| 6942 | bmap = isl_basic_map_reset_space(bmap, dim); |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 6943 | bset = bset_from_bmap(bmap); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 6944 | |
| 6945 | set = parameter_compute_divs(bset); |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 6946 | map = set_to_map(set); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 6947 | map = replace_space_by_local_space(map, ls); |
| 6948 | |
| 6949 | return map; |
| 6950 | error: |
| 6951 | isl_basic_map_free(bmap); |
| 6952 | return NULL; |
| 6953 | } |
| 6954 | |
Tobias Grosser | 37034db | 2016-03-25 19:38:18 +0000 | [diff] [blame] | 6955 | /* Remove the explicit representation of local variable "div", |
| 6956 | * if there is any. |
| 6957 | */ |
| 6958 | __isl_give isl_basic_map *isl_basic_map_mark_div_unknown( |
| 6959 | __isl_take isl_basic_map *bmap, int div) |
| 6960 | { |
Tobias Grosser | 932ec01 | 2016-07-06 09:11:00 +0000 | [diff] [blame] | 6961 | isl_bool unknown; |
Tobias Grosser | 37034db | 2016-03-25 19:38:18 +0000 | [diff] [blame] | 6962 | |
Tobias Grosser | 932ec01 | 2016-07-06 09:11:00 +0000 | [diff] [blame] | 6963 | unknown = isl_basic_map_div_is_marked_unknown(bmap, div); |
| 6964 | if (unknown < 0) |
Tobias Grosser | 37034db | 2016-03-25 19:38:18 +0000 | [diff] [blame] | 6965 | return isl_basic_map_free(bmap); |
Tobias Grosser | 932ec01 | 2016-07-06 09:11:00 +0000 | [diff] [blame] | 6966 | if (unknown) |
Tobias Grosser | 37034db | 2016-03-25 19:38:18 +0000 | [diff] [blame] | 6967 | return bmap; |
| 6968 | |
| 6969 | bmap = isl_basic_map_cow(bmap); |
| 6970 | if (!bmap) |
| 6971 | return NULL; |
| 6972 | isl_int_set_si(bmap->div[div][0], 0); |
| 6973 | return bmap; |
| 6974 | } |
| 6975 | |
Tobias Grosser | 932ec01 | 2016-07-06 09:11:00 +0000 | [diff] [blame] | 6976 | /* Is local variable "div" of "bmap" marked as not having an explicit |
| 6977 | * representation? |
| 6978 | * Note that even if "div" is not marked in this way and therefore |
| 6979 | * has an explicit representation, this representation may still |
| 6980 | * depend (indirectly) on other local variables that do not |
| 6981 | * have an explicit representation. |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 6982 | */ |
Tobias Grosser | 932ec01 | 2016-07-06 09:11:00 +0000 | [diff] [blame] | 6983 | isl_bool isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map *bmap, |
| 6984 | int div) |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 6985 | { |
| 6986 | if (!bmap) |
| 6987 | return isl_bool_error; |
| 6988 | if (div < 0 || div >= isl_basic_map_dim(bmap, isl_dim_div)) |
| 6989 | isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid, |
| 6990 | "position out of bounds", return isl_bool_error); |
Tobias Grosser | 932ec01 | 2016-07-06 09:11:00 +0000 | [diff] [blame] | 6991 | return isl_int_is_zero(bmap->div[div][0]); |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 6992 | } |
| 6993 | |
Tobias Grosser | 37034db | 2016-03-25 19:38:18 +0000 | [diff] [blame] | 6994 | /* Return the position of the first local variable that does not |
| 6995 | * have an explicit representation. |
| 6996 | * Return the total number of local variables if they all have |
| 6997 | * an explicit representation. |
| 6998 | * Return -1 on error. |
| 6999 | */ |
| 7000 | int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map *bmap) |
| 7001 | { |
| 7002 | int i; |
| 7003 | |
| 7004 | if (!bmap) |
| 7005 | return -1; |
| 7006 | |
| 7007 | for (i = 0; i < bmap->n_div; ++i) { |
| 7008 | if (!isl_basic_map_div_is_known(bmap, i)) |
| 7009 | return i; |
| 7010 | } |
| 7011 | return bmap->n_div; |
| 7012 | } |
| 7013 | |
Tobias Grosser | 9ec4f95 | 2016-07-20 16:53:07 +0000 | [diff] [blame] | 7014 | /* Return the position of the first local variable that does not |
| 7015 | * have an explicit representation. |
| 7016 | * Return the total number of local variables if they all have |
| 7017 | * an explicit representation. |
| 7018 | * Return -1 on error. |
| 7019 | */ |
| 7020 | int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set *bset) |
| 7021 | { |
| 7022 | return isl_basic_map_first_unknown_div(bset); |
| 7023 | } |
| 7024 | |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 7025 | /* Does "bmap" have an explicit representation for all local variables? |
| 7026 | */ |
| 7027 | isl_bool isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7028 | { |
Tobias Grosser | 37034db | 2016-03-25 19:38:18 +0000 | [diff] [blame] | 7029 | int first, n; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7030 | |
Tobias Grosser | 37034db | 2016-03-25 19:38:18 +0000 | [diff] [blame] | 7031 | n = isl_basic_map_dim(bmap, isl_dim_div); |
| 7032 | first = isl_basic_map_first_unknown_div(bmap); |
| 7033 | if (first < 0) |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 7034 | return isl_bool_error; |
Tobias Grosser | 37034db | 2016-03-25 19:38:18 +0000 | [diff] [blame] | 7035 | return first == n; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7036 | } |
| 7037 | |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 7038 | /* Do all basic maps in "map" have an explicit representation |
| 7039 | * for all local variables? |
| 7040 | */ |
| 7041 | isl_bool isl_map_divs_known(__isl_keep isl_map *map) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7042 | { |
| 7043 | int i; |
| 7044 | |
| 7045 | if (!map) |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 7046 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7047 | |
| 7048 | for (i = 0; i < map->n; ++i) { |
| 7049 | int known = isl_basic_map_divs_known(map->p[i]); |
| 7050 | if (known <= 0) |
| 7051 | return known; |
| 7052 | } |
| 7053 | |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 7054 | return isl_bool_true; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7055 | } |
| 7056 | |
| 7057 | /* If bmap contains any unknown divs, then compute explicit |
| 7058 | * expressions for them. However, this computation may be |
| 7059 | * quite expensive, so first try to remove divs that aren't |
| 7060 | * strictly needed. |
| 7061 | */ |
| 7062 | struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap) |
| 7063 | { |
| 7064 | int known; |
| 7065 | struct isl_map *map; |
| 7066 | |
| 7067 | known = isl_basic_map_divs_known(bmap); |
| 7068 | if (known < 0) |
| 7069 | goto error; |
| 7070 | if (known) |
| 7071 | return isl_map_from_basic_map(bmap); |
| 7072 | |
| 7073 | bmap = isl_basic_map_drop_redundant_divs(bmap); |
| 7074 | |
| 7075 | known = isl_basic_map_divs_known(bmap); |
| 7076 | if (known < 0) |
| 7077 | goto error; |
| 7078 | if (known) |
| 7079 | return isl_map_from_basic_map(bmap); |
| 7080 | |
| 7081 | map = compute_divs(bmap); |
| 7082 | return map; |
| 7083 | error: |
| 7084 | isl_basic_map_free(bmap); |
| 7085 | return NULL; |
| 7086 | } |
| 7087 | |
| 7088 | struct isl_map *isl_map_compute_divs(struct isl_map *map) |
| 7089 | { |
| 7090 | int i; |
| 7091 | int known; |
| 7092 | struct isl_map *res; |
| 7093 | |
| 7094 | if (!map) |
| 7095 | return NULL; |
| 7096 | if (map->n == 0) |
| 7097 | return map; |
| 7098 | |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 7099 | known = isl_map_divs_known(map); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7100 | if (known < 0) { |
| 7101 | isl_map_free(map); |
| 7102 | return NULL; |
| 7103 | } |
| 7104 | if (known) |
| 7105 | return map; |
| 7106 | |
| 7107 | res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0])); |
| 7108 | for (i = 1 ; i < map->n; ++i) { |
| 7109 | struct isl_map *r2; |
| 7110 | r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i])); |
| 7111 | if (ISL_F_ISSET(map, ISL_MAP_DISJOINT)) |
| 7112 | res = isl_map_union_disjoint(res, r2); |
| 7113 | else |
| 7114 | res = isl_map_union(res, r2); |
| 7115 | } |
| 7116 | isl_map_free(map); |
| 7117 | |
| 7118 | return res; |
| 7119 | } |
| 7120 | |
| 7121 | struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset) |
| 7122 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 7123 | return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset))); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7124 | } |
| 7125 | |
| 7126 | struct isl_set *isl_set_compute_divs(struct isl_set *set) |
| 7127 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 7128 | return set_from_map(isl_map_compute_divs(set_to_map(set))); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7129 | } |
| 7130 | |
| 7131 | struct isl_set *isl_map_domain(struct isl_map *map) |
| 7132 | { |
| 7133 | int i; |
| 7134 | struct isl_set *set; |
| 7135 | |
| 7136 | if (!map) |
| 7137 | goto error; |
| 7138 | |
| 7139 | map = isl_map_cow(map); |
| 7140 | if (!map) |
| 7141 | return NULL; |
| 7142 | |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 7143 | set = set_from_map(map); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7144 | set->dim = isl_space_domain(set->dim); |
| 7145 | if (!set->dim) |
| 7146 | goto error; |
| 7147 | for (i = 0; i < map->n; ++i) { |
| 7148 | set->p[i] = isl_basic_map_domain(map->p[i]); |
| 7149 | if (!set->p[i]) |
| 7150 | goto error; |
| 7151 | } |
| 7152 | ISL_F_CLR(set, ISL_MAP_DISJOINT); |
| 7153 | ISL_F_CLR(set, ISL_SET_NORMALIZED); |
| 7154 | return set; |
| 7155 | error: |
| 7156 | isl_map_free(map); |
| 7157 | return NULL; |
| 7158 | } |
| 7159 | |
| 7160 | /* Return the union of "map1" and "map2", where we assume for now that |
| 7161 | * "map1" and "map2" are disjoint. Note that the basic maps inside |
| 7162 | * "map1" or "map2" may not be disjoint from each other. |
| 7163 | * Also note that this function is also called from isl_map_union, |
| 7164 | * which takes care of handling the situation where "map1" and "map2" |
| 7165 | * may not be disjoint. |
| 7166 | * |
| 7167 | * If one of the inputs is empty, we can simply return the other input. |
| 7168 | * Similarly, if one of the inputs is universal, then it is equal to the union. |
| 7169 | */ |
| 7170 | static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1, |
| 7171 | __isl_take isl_map *map2) |
| 7172 | { |
| 7173 | int i; |
| 7174 | unsigned flags = 0; |
| 7175 | struct isl_map *map = NULL; |
| 7176 | int is_universe; |
| 7177 | |
| 7178 | if (!map1 || !map2) |
| 7179 | goto error; |
| 7180 | |
| 7181 | if (!isl_space_is_equal(map1->dim, map2->dim)) |
| 7182 | isl_die(isl_map_get_ctx(map1), isl_error_invalid, |
| 7183 | "spaces don't match", goto error); |
| 7184 | |
| 7185 | if (map1->n == 0) { |
| 7186 | isl_map_free(map1); |
| 7187 | return map2; |
| 7188 | } |
| 7189 | if (map2->n == 0) { |
| 7190 | isl_map_free(map2); |
| 7191 | return map1; |
| 7192 | } |
| 7193 | |
| 7194 | is_universe = isl_map_plain_is_universe(map1); |
| 7195 | if (is_universe < 0) |
| 7196 | goto error; |
| 7197 | if (is_universe) { |
| 7198 | isl_map_free(map2); |
| 7199 | return map1; |
| 7200 | } |
| 7201 | |
| 7202 | is_universe = isl_map_plain_is_universe(map2); |
| 7203 | if (is_universe < 0) |
| 7204 | goto error; |
| 7205 | if (is_universe) { |
| 7206 | isl_map_free(map1); |
| 7207 | return map2; |
| 7208 | } |
| 7209 | |
| 7210 | if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) && |
| 7211 | ISL_F_ISSET(map2, ISL_MAP_DISJOINT)) |
| 7212 | ISL_FL_SET(flags, ISL_MAP_DISJOINT); |
| 7213 | |
| 7214 | map = isl_map_alloc_space(isl_space_copy(map1->dim), |
| 7215 | map1->n + map2->n, flags); |
| 7216 | if (!map) |
| 7217 | goto error; |
| 7218 | for (i = 0; i < map1->n; ++i) { |
| 7219 | map = isl_map_add_basic_map(map, |
| 7220 | isl_basic_map_copy(map1->p[i])); |
| 7221 | if (!map) |
| 7222 | goto error; |
| 7223 | } |
| 7224 | for (i = 0; i < map2->n; ++i) { |
| 7225 | map = isl_map_add_basic_map(map, |
| 7226 | isl_basic_map_copy(map2->p[i])); |
| 7227 | if (!map) |
| 7228 | goto error; |
| 7229 | } |
| 7230 | isl_map_free(map1); |
| 7231 | isl_map_free(map2); |
| 7232 | return map; |
| 7233 | error: |
| 7234 | isl_map_free(map); |
| 7235 | isl_map_free(map1); |
| 7236 | isl_map_free(map2); |
| 7237 | return NULL; |
| 7238 | } |
| 7239 | |
| 7240 | /* Return the union of "map1" and "map2", where "map1" and "map2" are |
| 7241 | * guaranteed to be disjoint by the caller. |
| 7242 | * |
| 7243 | * Note that this functions is called from within isl_map_make_disjoint, |
| 7244 | * so we have to be careful not to touch the constraints of the inputs |
| 7245 | * in any way. |
| 7246 | */ |
| 7247 | __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1, |
| 7248 | __isl_take isl_map *map2) |
| 7249 | { |
| 7250 | return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint); |
| 7251 | } |
| 7252 | |
| 7253 | /* Return the union of "map1" and "map2", where "map1" and "map2" may |
| 7254 | * not be disjoint. The parameters are assumed to have been aligned. |
| 7255 | * |
| 7256 | * We currently simply call map_union_disjoint, the internal operation |
| 7257 | * of which does not really depend on the inputs being disjoint. |
| 7258 | * If the result contains more than one basic map, then we clear |
| 7259 | * the disjoint flag since the result may contain basic maps from |
| 7260 | * both inputs and these are not guaranteed to be disjoint. |
| 7261 | * |
| 7262 | * As a special case, if "map1" and "map2" are obviously equal, |
| 7263 | * then we simply return "map1". |
| 7264 | */ |
| 7265 | static __isl_give isl_map *map_union_aligned(__isl_take isl_map *map1, |
| 7266 | __isl_take isl_map *map2) |
| 7267 | { |
| 7268 | int equal; |
| 7269 | |
| 7270 | if (!map1 || !map2) |
| 7271 | goto error; |
| 7272 | |
| 7273 | equal = isl_map_plain_is_equal(map1, map2); |
| 7274 | if (equal < 0) |
| 7275 | goto error; |
| 7276 | if (equal) { |
| 7277 | isl_map_free(map2); |
| 7278 | return map1; |
| 7279 | } |
| 7280 | |
| 7281 | map1 = map_union_disjoint(map1, map2); |
| 7282 | if (!map1) |
| 7283 | return NULL; |
| 7284 | if (map1->n > 1) |
| 7285 | ISL_F_CLR(map1, ISL_MAP_DISJOINT); |
| 7286 | return map1; |
| 7287 | error: |
| 7288 | isl_map_free(map1); |
| 7289 | isl_map_free(map2); |
| 7290 | return NULL; |
| 7291 | } |
| 7292 | |
| 7293 | /* Return the union of "map1" and "map2", where "map1" and "map2" may |
| 7294 | * not be disjoint. |
| 7295 | */ |
| 7296 | __isl_give isl_map *isl_map_union(__isl_take isl_map *map1, |
| 7297 | __isl_take isl_map *map2) |
| 7298 | { |
| 7299 | return isl_map_align_params_map_map_and(map1, map2, &map_union_aligned); |
| 7300 | } |
| 7301 | |
| 7302 | struct isl_set *isl_set_union_disjoint( |
| 7303 | struct isl_set *set1, struct isl_set *set2) |
| 7304 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 7305 | return set_from_map(isl_map_union_disjoint(set_to_map(set1), |
| 7306 | set_to_map(set2))); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7307 | } |
| 7308 | |
| 7309 | struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2) |
| 7310 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 7311 | return set_from_map(isl_map_union(set_to_map(set1), set_to_map(set2))); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7312 | } |
| 7313 | |
| 7314 | /* Apply "fn" to pairs of elements from "map" and "set" and collect |
| 7315 | * the results. |
| 7316 | * |
| 7317 | * "map" and "set" are assumed to be compatible and non-NULL. |
| 7318 | */ |
| 7319 | static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map, |
| 7320 | __isl_take isl_set *set, |
| 7321 | __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap, |
| 7322 | __isl_take isl_basic_set *bset)) |
| 7323 | { |
| 7324 | unsigned flags = 0; |
| 7325 | struct isl_map *result; |
| 7326 | int i, j; |
| 7327 | |
| 7328 | if (isl_set_plain_is_universe(set)) { |
| 7329 | isl_set_free(set); |
| 7330 | return map; |
| 7331 | } |
| 7332 | |
| 7333 | if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) && |
| 7334 | ISL_F_ISSET(set, ISL_MAP_DISJOINT)) |
| 7335 | ISL_FL_SET(flags, ISL_MAP_DISJOINT); |
| 7336 | |
| 7337 | result = isl_map_alloc_space(isl_space_copy(map->dim), |
| 7338 | map->n * set->n, flags); |
| 7339 | for (i = 0; result && i < map->n; ++i) |
| 7340 | for (j = 0; j < set->n; ++j) { |
| 7341 | result = isl_map_add_basic_map(result, |
| 7342 | fn(isl_basic_map_copy(map->p[i]), |
| 7343 | isl_basic_set_copy(set->p[j]))); |
| 7344 | if (!result) |
| 7345 | break; |
| 7346 | } |
| 7347 | |
| 7348 | isl_map_free(map); |
| 7349 | isl_set_free(set); |
| 7350 | return result; |
| 7351 | } |
| 7352 | |
| 7353 | static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map, |
| 7354 | __isl_take isl_set *set) |
| 7355 | { |
| 7356 | if (!map || !set) |
| 7357 | goto error; |
| 7358 | |
| 7359 | if (!isl_map_compatible_range(map, set)) |
| 7360 | isl_die(set->ctx, isl_error_invalid, |
| 7361 | "incompatible spaces", goto error); |
| 7362 | |
| 7363 | return map_intersect_set(map, set, &isl_basic_map_intersect_range); |
| 7364 | error: |
| 7365 | isl_map_free(map); |
| 7366 | isl_set_free(set); |
| 7367 | return NULL; |
| 7368 | } |
| 7369 | |
| 7370 | __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map, |
| 7371 | __isl_take isl_set *set) |
| 7372 | { |
| 7373 | return isl_map_align_params_map_map_and(map, set, &map_intersect_range); |
| 7374 | } |
| 7375 | |
| 7376 | static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map, |
| 7377 | __isl_take isl_set *set) |
| 7378 | { |
| 7379 | if (!map || !set) |
| 7380 | goto error; |
| 7381 | |
| 7382 | if (!isl_map_compatible_domain(map, set)) |
| 7383 | isl_die(set->ctx, isl_error_invalid, |
| 7384 | "incompatible spaces", goto error); |
| 7385 | |
| 7386 | return map_intersect_set(map, set, &isl_basic_map_intersect_domain); |
| 7387 | error: |
| 7388 | isl_map_free(map); |
| 7389 | isl_set_free(set); |
| 7390 | return NULL; |
| 7391 | } |
| 7392 | |
| 7393 | __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map, |
| 7394 | __isl_take isl_set *set) |
| 7395 | { |
| 7396 | return isl_map_align_params_map_map_and(map, set, |
| 7397 | &map_intersect_domain); |
| 7398 | } |
| 7399 | |
| 7400 | static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1, |
| 7401 | __isl_take isl_map *map2) |
| 7402 | { |
| 7403 | if (!map1 || !map2) |
| 7404 | goto error; |
| 7405 | map1 = isl_map_reverse(map1); |
| 7406 | map1 = isl_map_apply_range(map1, map2); |
| 7407 | return isl_map_reverse(map1); |
| 7408 | error: |
| 7409 | isl_map_free(map1); |
| 7410 | isl_map_free(map2); |
| 7411 | return NULL; |
| 7412 | } |
| 7413 | |
| 7414 | __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1, |
| 7415 | __isl_take isl_map *map2) |
| 7416 | { |
| 7417 | return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain); |
| 7418 | } |
| 7419 | |
| 7420 | static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1, |
| 7421 | __isl_take isl_map *map2) |
| 7422 | { |
| 7423 | isl_space *dim_result; |
| 7424 | struct isl_map *result; |
| 7425 | int i, j; |
| 7426 | |
| 7427 | if (!map1 || !map2) |
| 7428 | goto error; |
| 7429 | |
| 7430 | dim_result = isl_space_join(isl_space_copy(map1->dim), |
| 7431 | isl_space_copy(map2->dim)); |
| 7432 | |
| 7433 | result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0); |
| 7434 | if (!result) |
| 7435 | goto error; |
| 7436 | for (i = 0; i < map1->n; ++i) |
| 7437 | for (j = 0; j < map2->n; ++j) { |
| 7438 | result = isl_map_add_basic_map(result, |
| 7439 | isl_basic_map_apply_range( |
| 7440 | isl_basic_map_copy(map1->p[i]), |
| 7441 | isl_basic_map_copy(map2->p[j]))); |
| 7442 | if (!result) |
| 7443 | goto error; |
| 7444 | } |
| 7445 | isl_map_free(map1); |
| 7446 | isl_map_free(map2); |
| 7447 | if (result && result->n <= 1) |
| 7448 | ISL_F_SET(result, ISL_MAP_DISJOINT); |
| 7449 | return result; |
| 7450 | error: |
| 7451 | isl_map_free(map1); |
| 7452 | isl_map_free(map2); |
| 7453 | return NULL; |
| 7454 | } |
| 7455 | |
| 7456 | __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1, |
| 7457 | __isl_take isl_map *map2) |
| 7458 | { |
| 7459 | return isl_map_align_params_map_map_and(map1, map2, &map_apply_range); |
| 7460 | } |
| 7461 | |
| 7462 | /* |
| 7463 | * returns range - domain |
| 7464 | */ |
| 7465 | struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap) |
| 7466 | { |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7467 | isl_space *target_space; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7468 | struct isl_basic_set *bset; |
| 7469 | unsigned dim; |
| 7470 | unsigned nparam; |
| 7471 | int i; |
| 7472 | |
| 7473 | if (!bmap) |
| 7474 | goto error; |
| 7475 | isl_assert(bmap->ctx, isl_space_tuple_is_equal(bmap->dim, isl_dim_in, |
| 7476 | bmap->dim, isl_dim_out), |
| 7477 | goto error); |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7478 | target_space = isl_space_domain(isl_basic_map_get_space(bmap)); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7479 | dim = isl_basic_map_n_in(bmap); |
| 7480 | nparam = isl_basic_map_n_param(bmap); |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7481 | bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap)); |
| 7482 | bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim); |
| 7483 | bmap = isl_basic_map_extend_constraints(bmap, dim, 0); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7484 | for (i = 0; i < dim; ++i) { |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7485 | int j = isl_basic_map_alloc_equality(bmap); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7486 | if (j < 0) { |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7487 | bmap = isl_basic_map_free(bmap); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7488 | break; |
| 7489 | } |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7490 | isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap)); |
| 7491 | isl_int_set_si(bmap->eq[j][1+nparam+i], 1); |
| 7492 | isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1); |
| 7493 | isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7494 | } |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7495 | bset = isl_basic_map_domain(bmap); |
| 7496 | bset = isl_basic_set_reset_space(bset, target_space); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7497 | return bset; |
| 7498 | error: |
| 7499 | isl_basic_map_free(bmap); |
| 7500 | return NULL; |
| 7501 | } |
| 7502 | |
| 7503 | /* |
| 7504 | * returns range - domain |
| 7505 | */ |
| 7506 | __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map) |
| 7507 | { |
| 7508 | int i; |
| 7509 | isl_space *dim; |
| 7510 | struct isl_set *result; |
| 7511 | |
| 7512 | if (!map) |
| 7513 | return NULL; |
| 7514 | |
| 7515 | isl_assert(map->ctx, isl_space_tuple_is_equal(map->dim, isl_dim_in, |
| 7516 | map->dim, isl_dim_out), |
| 7517 | goto error); |
| 7518 | dim = isl_map_get_space(map); |
| 7519 | dim = isl_space_domain(dim); |
| 7520 | result = isl_set_alloc_space(dim, map->n, 0); |
| 7521 | if (!result) |
| 7522 | goto error; |
| 7523 | for (i = 0; i < map->n; ++i) |
| 7524 | result = isl_set_add_basic_set(result, |
| 7525 | isl_basic_map_deltas(isl_basic_map_copy(map->p[i]))); |
| 7526 | isl_map_free(map); |
| 7527 | return result; |
| 7528 | error: |
| 7529 | isl_map_free(map); |
| 7530 | return NULL; |
| 7531 | } |
| 7532 | |
| 7533 | /* |
| 7534 | * returns [domain -> range] -> range - domain |
| 7535 | */ |
| 7536 | __isl_give isl_basic_map *isl_basic_map_deltas_map( |
| 7537 | __isl_take isl_basic_map *bmap) |
| 7538 | { |
| 7539 | int i, k; |
| 7540 | isl_space *dim; |
| 7541 | isl_basic_map *domain; |
| 7542 | int nparam, n; |
| 7543 | unsigned total; |
| 7544 | |
| 7545 | if (!isl_space_tuple_is_equal(bmap->dim, isl_dim_in, |
| 7546 | bmap->dim, isl_dim_out)) |
| 7547 | isl_die(bmap->ctx, isl_error_invalid, |
| 7548 | "domain and range don't match", goto error); |
| 7549 | |
| 7550 | nparam = isl_basic_map_dim(bmap, isl_dim_param); |
| 7551 | n = isl_basic_map_dim(bmap, isl_dim_in); |
| 7552 | |
| 7553 | dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap))); |
| 7554 | domain = isl_basic_map_universe(dim); |
| 7555 | |
| 7556 | bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap)); |
| 7557 | bmap = isl_basic_map_apply_range(bmap, domain); |
| 7558 | bmap = isl_basic_map_extend_constraints(bmap, n, 0); |
| 7559 | |
| 7560 | total = isl_basic_map_total_dim(bmap); |
| 7561 | |
| 7562 | for (i = 0; i < n; ++i) { |
| 7563 | k = isl_basic_map_alloc_equality(bmap); |
| 7564 | if (k < 0) |
| 7565 | goto error; |
| 7566 | isl_seq_clr(bmap->eq[k], 1 + total); |
| 7567 | isl_int_set_si(bmap->eq[k][1 + nparam + i], 1); |
| 7568 | isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1); |
| 7569 | isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1); |
| 7570 | } |
| 7571 | |
| 7572 | bmap = isl_basic_map_gauss(bmap, NULL); |
| 7573 | return isl_basic_map_finalize(bmap); |
| 7574 | error: |
| 7575 | isl_basic_map_free(bmap); |
| 7576 | return NULL; |
| 7577 | } |
| 7578 | |
| 7579 | /* |
| 7580 | * returns [domain -> range] -> range - domain |
| 7581 | */ |
| 7582 | __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map) |
| 7583 | { |
| 7584 | int i; |
| 7585 | isl_space *domain_dim; |
| 7586 | |
| 7587 | if (!map) |
| 7588 | return NULL; |
| 7589 | |
| 7590 | if (!isl_space_tuple_is_equal(map->dim, isl_dim_in, |
| 7591 | map->dim, isl_dim_out)) |
| 7592 | isl_die(map->ctx, isl_error_invalid, |
| 7593 | "domain and range don't match", goto error); |
| 7594 | |
| 7595 | map = isl_map_cow(map); |
| 7596 | if (!map) |
| 7597 | return NULL; |
| 7598 | |
| 7599 | domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map))); |
| 7600 | map->dim = isl_space_from_domain(isl_space_wrap(map->dim)); |
| 7601 | map->dim = isl_space_join(map->dim, domain_dim); |
| 7602 | if (!map->dim) |
| 7603 | goto error; |
| 7604 | for (i = 0; i < map->n; ++i) { |
| 7605 | map->p[i] = isl_basic_map_deltas_map(map->p[i]); |
| 7606 | if (!map->p[i]) |
| 7607 | goto error; |
| 7608 | } |
| 7609 | ISL_F_CLR(map, ISL_MAP_NORMALIZED); |
| 7610 | return map; |
| 7611 | error: |
| 7612 | isl_map_free(map); |
| 7613 | return NULL; |
| 7614 | } |
| 7615 | |
| 7616 | static __isl_give isl_basic_map *basic_map_identity(__isl_take isl_space *dims) |
| 7617 | { |
| 7618 | struct isl_basic_map *bmap; |
| 7619 | unsigned nparam; |
| 7620 | unsigned dim; |
| 7621 | int i; |
| 7622 | |
| 7623 | if (!dims) |
| 7624 | return NULL; |
| 7625 | |
| 7626 | nparam = dims->nparam; |
| 7627 | dim = dims->n_out; |
| 7628 | bmap = isl_basic_map_alloc_space(dims, 0, dim, 0); |
| 7629 | if (!bmap) |
| 7630 | goto error; |
| 7631 | |
| 7632 | for (i = 0; i < dim; ++i) { |
| 7633 | int j = isl_basic_map_alloc_equality(bmap); |
| 7634 | if (j < 0) |
| 7635 | goto error; |
| 7636 | isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap)); |
| 7637 | isl_int_set_si(bmap->eq[j][1+nparam+i], 1); |
| 7638 | isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1); |
| 7639 | } |
| 7640 | return isl_basic_map_finalize(bmap); |
| 7641 | error: |
| 7642 | isl_basic_map_free(bmap); |
| 7643 | return NULL; |
| 7644 | } |
| 7645 | |
| 7646 | __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim) |
| 7647 | { |
| 7648 | if (!dim) |
| 7649 | return NULL; |
| 7650 | if (dim->n_in != dim->n_out) |
| 7651 | isl_die(dim->ctx, isl_error_invalid, |
| 7652 | "number of input and output dimensions needs to be " |
| 7653 | "the same", goto error); |
| 7654 | return basic_map_identity(dim); |
| 7655 | error: |
| 7656 | isl_space_free(dim); |
| 7657 | return NULL; |
| 7658 | } |
| 7659 | |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7660 | __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim) |
| 7661 | { |
| 7662 | return isl_map_from_basic_map(isl_basic_map_identity(dim)); |
| 7663 | } |
| 7664 | |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7665 | __isl_give isl_map *isl_set_identity(__isl_take isl_set *set) |
| 7666 | { |
| 7667 | isl_space *dim = isl_set_get_space(set); |
| 7668 | isl_map *id; |
| 7669 | id = isl_map_identity(isl_space_map_from_set(dim)); |
| 7670 | return isl_map_intersect_range(id, set); |
| 7671 | } |
| 7672 | |
| 7673 | /* Construct a basic set with all set dimensions having only non-negative |
| 7674 | * values. |
| 7675 | */ |
| 7676 | __isl_give isl_basic_set *isl_basic_set_positive_orthant( |
| 7677 | __isl_take isl_space *space) |
| 7678 | { |
| 7679 | int i; |
| 7680 | unsigned nparam; |
| 7681 | unsigned dim; |
| 7682 | struct isl_basic_set *bset; |
| 7683 | |
| 7684 | if (!space) |
| 7685 | return NULL; |
| 7686 | nparam = space->nparam; |
| 7687 | dim = space->n_out; |
| 7688 | bset = isl_basic_set_alloc_space(space, 0, 0, dim); |
| 7689 | if (!bset) |
| 7690 | return NULL; |
| 7691 | for (i = 0; i < dim; ++i) { |
| 7692 | int k = isl_basic_set_alloc_inequality(bset); |
| 7693 | if (k < 0) |
| 7694 | goto error; |
| 7695 | isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset)); |
| 7696 | isl_int_set_si(bset->ineq[k][1 + nparam + i], 1); |
| 7697 | } |
| 7698 | return bset; |
| 7699 | error: |
| 7700 | isl_basic_set_free(bset); |
| 7701 | return NULL; |
| 7702 | } |
| 7703 | |
| 7704 | /* Construct the half-space x_pos >= 0. |
| 7705 | */ |
| 7706 | static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim, |
| 7707 | int pos) |
| 7708 | { |
| 7709 | int k; |
| 7710 | isl_basic_set *nonneg; |
| 7711 | |
| 7712 | nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1); |
| 7713 | k = isl_basic_set_alloc_inequality(nonneg); |
| 7714 | if (k < 0) |
| 7715 | goto error; |
| 7716 | isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg)); |
| 7717 | isl_int_set_si(nonneg->ineq[k][pos], 1); |
| 7718 | |
| 7719 | return isl_basic_set_finalize(nonneg); |
| 7720 | error: |
| 7721 | isl_basic_set_free(nonneg); |
| 7722 | return NULL; |
| 7723 | } |
| 7724 | |
| 7725 | /* Construct the half-space x_pos <= -1. |
| 7726 | */ |
| 7727 | static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos) |
| 7728 | { |
| 7729 | int k; |
| 7730 | isl_basic_set *neg; |
| 7731 | |
| 7732 | neg = isl_basic_set_alloc_space(dim, 0, 0, 1); |
| 7733 | k = isl_basic_set_alloc_inequality(neg); |
| 7734 | if (k < 0) |
| 7735 | goto error; |
| 7736 | isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg)); |
| 7737 | isl_int_set_si(neg->ineq[k][0], -1); |
| 7738 | isl_int_set_si(neg->ineq[k][pos], -1); |
| 7739 | |
| 7740 | return isl_basic_set_finalize(neg); |
| 7741 | error: |
| 7742 | isl_basic_set_free(neg); |
| 7743 | return NULL; |
| 7744 | } |
| 7745 | |
| 7746 | __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set, |
| 7747 | enum isl_dim_type type, unsigned first, unsigned n) |
| 7748 | { |
| 7749 | int i; |
Tobias Grosser | e0f8d59 | 2015-05-13 13:10:13 +0000 | [diff] [blame] | 7750 | unsigned offset; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7751 | isl_basic_set *nonneg; |
| 7752 | isl_basic_set *neg; |
| 7753 | |
| 7754 | if (!set) |
| 7755 | return NULL; |
| 7756 | if (n == 0) |
| 7757 | return set; |
| 7758 | |
| 7759 | isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error); |
| 7760 | |
Tobias Grosser | e0f8d59 | 2015-05-13 13:10:13 +0000 | [diff] [blame] | 7761 | offset = pos(set->dim, type); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7762 | for (i = 0; i < n; ++i) { |
| 7763 | nonneg = nonneg_halfspace(isl_set_get_space(set), |
Tobias Grosser | e0f8d59 | 2015-05-13 13:10:13 +0000 | [diff] [blame] | 7764 | offset + first + i); |
| 7765 | neg = neg_halfspace(isl_set_get_space(set), offset + first + i); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7766 | |
| 7767 | set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg)); |
| 7768 | } |
| 7769 | |
| 7770 | return set; |
| 7771 | error: |
| 7772 | isl_set_free(set); |
| 7773 | return NULL; |
| 7774 | } |
| 7775 | |
| 7776 | static int foreach_orthant(__isl_take isl_set *set, int *signs, int first, |
| 7777 | int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user), |
| 7778 | void *user) |
| 7779 | { |
| 7780 | isl_set *half; |
| 7781 | |
| 7782 | if (!set) |
| 7783 | return -1; |
| 7784 | if (isl_set_plain_is_empty(set)) { |
| 7785 | isl_set_free(set); |
| 7786 | return 0; |
| 7787 | } |
| 7788 | if (first == len) |
| 7789 | return fn(set, signs, user); |
| 7790 | |
| 7791 | signs[first] = 1; |
| 7792 | half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set), |
| 7793 | 1 + first)); |
| 7794 | half = isl_set_intersect(half, isl_set_copy(set)); |
| 7795 | if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0) |
| 7796 | goto error; |
| 7797 | |
| 7798 | signs[first] = -1; |
| 7799 | half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set), |
| 7800 | 1 + first)); |
| 7801 | half = isl_set_intersect(half, set); |
| 7802 | return foreach_orthant(half, signs, first + 1, len, fn, user); |
| 7803 | error: |
| 7804 | isl_set_free(set); |
| 7805 | return -1; |
| 7806 | } |
| 7807 | |
| 7808 | /* Call "fn" on the intersections of "set" with each of the orthants |
| 7809 | * (except for obviously empty intersections). The orthant is identified |
| 7810 | * by the signs array, with each entry having value 1 or -1 according |
| 7811 | * to the sign of the corresponding variable. |
| 7812 | */ |
| 7813 | int isl_set_foreach_orthant(__isl_keep isl_set *set, |
| 7814 | int (*fn)(__isl_take isl_set *orthant, int *signs, void *user), |
| 7815 | void *user) |
| 7816 | { |
| 7817 | unsigned nparam; |
| 7818 | unsigned nvar; |
| 7819 | int *signs; |
| 7820 | int r; |
| 7821 | |
| 7822 | if (!set) |
| 7823 | return -1; |
| 7824 | if (isl_set_plain_is_empty(set)) |
| 7825 | return 0; |
| 7826 | |
| 7827 | nparam = isl_set_dim(set, isl_dim_param); |
| 7828 | nvar = isl_set_dim(set, isl_dim_set); |
| 7829 | |
| 7830 | signs = isl_alloc_array(set->ctx, int, nparam + nvar); |
| 7831 | |
| 7832 | r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar, |
| 7833 | fn, user); |
| 7834 | |
| 7835 | free(signs); |
| 7836 | |
| 7837 | return r; |
| 7838 | } |
| 7839 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7840 | isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7841 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 7842 | return isl_map_is_equal(set_to_map(set1), set_to_map(set2)); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7843 | } |
| 7844 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7845 | isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1, |
| 7846 | __isl_keep isl_basic_map *bmap2) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7847 | { |
| 7848 | int is_subset; |
| 7849 | struct isl_map *map1; |
| 7850 | struct isl_map *map2; |
| 7851 | |
| 7852 | if (!bmap1 || !bmap2) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7853 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7854 | |
| 7855 | map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1)); |
| 7856 | map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2)); |
| 7857 | |
| 7858 | is_subset = isl_map_is_subset(map1, map2); |
| 7859 | |
| 7860 | isl_map_free(map1); |
| 7861 | isl_map_free(map2); |
| 7862 | |
| 7863 | return is_subset; |
| 7864 | } |
| 7865 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7866 | isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1, |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7867 | __isl_keep isl_basic_set *bset2) |
| 7868 | { |
| 7869 | return isl_basic_map_is_subset(bset1, bset2); |
| 7870 | } |
| 7871 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7872 | isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1, |
| 7873 | __isl_keep isl_basic_map *bmap2) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7874 | { |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7875 | isl_bool is_subset; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7876 | |
| 7877 | if (!bmap1 || !bmap2) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7878 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7879 | is_subset = isl_basic_map_is_subset(bmap1, bmap2); |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7880 | if (is_subset != isl_bool_true) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7881 | return is_subset; |
| 7882 | is_subset = isl_basic_map_is_subset(bmap2, bmap1); |
| 7883 | return is_subset; |
| 7884 | } |
| 7885 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7886 | isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_set *bset1, |
| 7887 | __isl_keep isl_basic_set *bset2) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7888 | { |
| 7889 | return isl_basic_map_is_equal( |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 7890 | bset_to_bmap(bset1), bset_to_bmap(bset2)); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7891 | } |
| 7892 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7893 | isl_bool isl_map_is_empty(__isl_keep isl_map *map) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7894 | { |
| 7895 | int i; |
| 7896 | int is_empty; |
| 7897 | |
| 7898 | if (!map) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7899 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7900 | for (i = 0; i < map->n; ++i) { |
| 7901 | is_empty = isl_basic_map_is_empty(map->p[i]); |
| 7902 | if (is_empty < 0) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7903 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7904 | if (!is_empty) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7905 | return isl_bool_false; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7906 | } |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7907 | return isl_bool_true; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7908 | } |
| 7909 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7910 | isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7911 | { |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7912 | return map ? map->n == 0 : isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7913 | } |
| 7914 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7915 | isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7916 | { |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7917 | return set ? set->n == 0 : isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7918 | } |
| 7919 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7920 | isl_bool isl_set_is_empty(__isl_keep isl_set *set) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7921 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 7922 | return isl_map_is_empty(set_to_map(set)); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7923 | } |
| 7924 | |
| 7925 | int isl_map_has_equal_space(__isl_keep isl_map *map1, __isl_keep isl_map *map2) |
| 7926 | { |
| 7927 | if (!map1 || !map2) |
| 7928 | return -1; |
| 7929 | |
| 7930 | return isl_space_is_equal(map1->dim, map2->dim); |
| 7931 | } |
| 7932 | |
| 7933 | int isl_set_has_equal_space(__isl_keep isl_set *set1, __isl_keep isl_set *set2) |
| 7934 | { |
| 7935 | if (!set1 || !set2) |
| 7936 | return -1; |
| 7937 | |
| 7938 | return isl_space_is_equal(set1->dim, set2->dim); |
| 7939 | } |
| 7940 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7941 | static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7942 | { |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7943 | isl_bool is_subset; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7944 | |
| 7945 | if (!map1 || !map2) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7946 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7947 | is_subset = isl_map_is_subset(map1, map2); |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7948 | if (is_subset != isl_bool_true) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7949 | return is_subset; |
| 7950 | is_subset = isl_map_is_subset(map2, map1); |
| 7951 | return is_subset; |
| 7952 | } |
| 7953 | |
Tobias Grosser | 932ec01 | 2016-07-06 09:11:00 +0000 | [diff] [blame] | 7954 | /* Is "map1" equal to "map2"? |
| 7955 | * |
| 7956 | * First check if they are obviously equal. |
| 7957 | * If not, then perform a more detailed analysis. |
| 7958 | */ |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7959 | isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7960 | { |
Tobias Grosser | 932ec01 | 2016-07-06 09:11:00 +0000 | [diff] [blame] | 7961 | isl_bool equal; |
| 7962 | |
| 7963 | equal = isl_map_plain_is_equal(map1, map2); |
| 7964 | if (equal < 0 || equal) |
| 7965 | return equal; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7966 | return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal); |
| 7967 | } |
| 7968 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7969 | isl_bool isl_basic_map_is_strict_subset( |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7970 | struct isl_basic_map *bmap1, struct isl_basic_map *bmap2) |
| 7971 | { |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7972 | isl_bool is_subset; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7973 | |
| 7974 | if (!bmap1 || !bmap2) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7975 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7976 | is_subset = isl_basic_map_is_subset(bmap1, bmap2); |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7977 | if (is_subset != isl_bool_true) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7978 | return is_subset; |
| 7979 | is_subset = isl_basic_map_is_subset(bmap2, bmap1); |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7980 | if (is_subset == isl_bool_error) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7981 | return is_subset; |
| 7982 | return !is_subset; |
| 7983 | } |
| 7984 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7985 | isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1, |
| 7986 | __isl_keep isl_map *map2) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7987 | { |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7988 | isl_bool is_subset; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7989 | |
| 7990 | if (!map1 || !map2) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7991 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7992 | is_subset = isl_map_is_subset(map1, map2); |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7993 | if (is_subset != isl_bool_true) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7994 | return is_subset; |
| 7995 | is_subset = isl_map_is_subset(map2, map1); |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 7996 | if (is_subset == isl_bool_error) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 7997 | return is_subset; |
| 7998 | return !is_subset; |
| 7999 | } |
| 8000 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 8001 | isl_bool isl_set_is_strict_subset(__isl_keep isl_set *set1, |
| 8002 | __isl_keep isl_set *set2) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8003 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 8004 | return isl_map_is_strict_subset(set_to_map(set1), set_to_map(set2)); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8005 | } |
| 8006 | |
Tobias Grosser | a67ac97 | 2016-02-26 11:35:12 +0000 | [diff] [blame] | 8007 | /* Is "bmap" obviously equal to the universe with the same space? |
| 8008 | * |
| 8009 | * That is, does it not have any constraints? |
| 8010 | */ |
| 8011 | isl_bool isl_basic_map_plain_is_universe(__isl_keep isl_basic_map *bmap) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8012 | { |
| 8013 | if (!bmap) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 8014 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8015 | return bmap->n_eq == 0 && bmap->n_ineq == 0; |
| 8016 | } |
| 8017 | |
Tobias Grosser | a67ac97 | 2016-02-26 11:35:12 +0000 | [diff] [blame] | 8018 | /* Is "bset" obviously equal to the universe with the same space? |
| 8019 | */ |
| 8020 | isl_bool isl_basic_set_plain_is_universe(__isl_keep isl_basic_set *bset) |
| 8021 | { |
| 8022 | return isl_basic_map_plain_is_universe(bset); |
| 8023 | } |
| 8024 | |
| 8025 | /* If "c" does not involve any existentially quantified variables, |
| 8026 | * then set *univ to false and abort |
| 8027 | */ |
| 8028 | static isl_stat involves_divs(__isl_take isl_constraint *c, void *user) |
| 8029 | { |
| 8030 | isl_bool *univ = user; |
| 8031 | unsigned n; |
| 8032 | |
| 8033 | n = isl_constraint_dim(c, isl_dim_div); |
| 8034 | *univ = isl_constraint_involves_dims(c, isl_dim_div, 0, n); |
| 8035 | isl_constraint_free(c); |
| 8036 | if (*univ < 0 || !*univ) |
| 8037 | return isl_stat_error; |
| 8038 | return isl_stat_ok; |
| 8039 | } |
| 8040 | |
| 8041 | /* Is "bmap" equal to the universe with the same space? |
| 8042 | * |
| 8043 | * First check if it is obviously equal to the universe. |
| 8044 | * If not and if there are any constraints not involving |
| 8045 | * existentially quantified variables, then it is certainly |
| 8046 | * not equal to the universe. |
| 8047 | * Otherwise, check if the universe is a subset of "bmap". |
| 8048 | */ |
| 8049 | isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap) |
| 8050 | { |
| 8051 | isl_bool univ; |
| 8052 | isl_basic_map *test; |
| 8053 | |
| 8054 | univ = isl_basic_map_plain_is_universe(bmap); |
| 8055 | if (univ < 0 || univ) |
| 8056 | return univ; |
| 8057 | if (isl_basic_map_dim(bmap, isl_dim_div) == 0) |
| 8058 | return isl_bool_false; |
| 8059 | univ = isl_bool_true; |
| 8060 | if (isl_basic_map_foreach_constraint(bmap, &involves_divs, &univ) < 0 && |
| 8061 | univ) |
| 8062 | return isl_bool_error; |
| 8063 | if (univ < 0 || !univ) |
| 8064 | return univ; |
| 8065 | test = isl_basic_map_universe(isl_basic_map_get_space(bmap)); |
| 8066 | univ = isl_basic_map_is_subset(test, bmap); |
| 8067 | isl_basic_map_free(test); |
| 8068 | return univ; |
| 8069 | } |
| 8070 | |
| 8071 | /* Is "bset" equal to the universe with the same space? |
| 8072 | */ |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 8073 | isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_set *bset) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8074 | { |
Tobias Grosser | a67ac97 | 2016-02-26 11:35:12 +0000 | [diff] [blame] | 8075 | return isl_basic_map_is_universe(bset); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8076 | } |
| 8077 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 8078 | isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8079 | { |
| 8080 | int i; |
| 8081 | |
| 8082 | if (!map) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 8083 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8084 | |
| 8085 | for (i = 0; i < map->n; ++i) { |
Tobias Grosser | a67ac97 | 2016-02-26 11:35:12 +0000 | [diff] [blame] | 8086 | isl_bool r = isl_basic_map_plain_is_universe(map->p[i]); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8087 | if (r < 0 || r) |
| 8088 | return r; |
| 8089 | } |
| 8090 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 8091 | return isl_bool_false; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8092 | } |
| 8093 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 8094 | isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8095 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 8096 | return isl_map_plain_is_universe(set_to_map(set)); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8097 | } |
| 8098 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 8099 | isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8100 | { |
| 8101 | struct isl_basic_set *bset = NULL; |
| 8102 | struct isl_vec *sample = NULL; |
Tobias Grosser | a67ac97 | 2016-02-26 11:35:12 +0000 | [diff] [blame] | 8103 | isl_bool empty, non_empty; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8104 | |
| 8105 | if (!bmap) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 8106 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8107 | |
| 8108 | if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 8109 | return isl_bool_true; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8110 | |
Tobias Grosser | a67ac97 | 2016-02-26 11:35:12 +0000 | [diff] [blame] | 8111 | if (isl_basic_map_plain_is_universe(bmap)) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 8112 | return isl_bool_false; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8113 | |
| 8114 | if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) { |
| 8115 | struct isl_basic_map *copy = isl_basic_map_copy(bmap); |
| 8116 | copy = isl_basic_map_remove_redundancies(copy); |
| 8117 | empty = isl_basic_map_plain_is_empty(copy); |
| 8118 | isl_basic_map_free(copy); |
| 8119 | return empty; |
| 8120 | } |
| 8121 | |
Tobias Grosser | a67ac97 | 2016-02-26 11:35:12 +0000 | [diff] [blame] | 8122 | non_empty = isl_basic_map_plain_is_non_empty(bmap); |
| 8123 | if (non_empty < 0) |
| 8124 | return isl_bool_error; |
| 8125 | if (non_empty) |
| 8126 | return isl_bool_false; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8127 | isl_vec_free(bmap->sample); |
| 8128 | bmap->sample = NULL; |
| 8129 | bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap)); |
| 8130 | if (!bset) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 8131 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8132 | sample = isl_basic_set_sample_vec(bset); |
| 8133 | if (!sample) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 8134 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8135 | empty = sample->size == 0; |
| 8136 | isl_vec_free(bmap->sample); |
| 8137 | bmap->sample = sample; |
| 8138 | if (empty) |
| 8139 | ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY); |
| 8140 | |
| 8141 | return empty; |
| 8142 | } |
| 8143 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 8144 | isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8145 | { |
| 8146 | if (!bmap) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 8147 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8148 | return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY); |
| 8149 | } |
| 8150 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 8151 | isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8152 | { |
| 8153 | if (!bset) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 8154 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8155 | return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY); |
| 8156 | } |
| 8157 | |
Tobias Grosser | a67ac97 | 2016-02-26 11:35:12 +0000 | [diff] [blame] | 8158 | /* Is "bmap" known to be non-empty? |
| 8159 | * |
| 8160 | * That is, is the cached sample still valid? |
| 8161 | */ |
| 8162 | isl_bool isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map *bmap) |
| 8163 | { |
| 8164 | unsigned total; |
| 8165 | |
| 8166 | if (!bmap) |
| 8167 | return isl_bool_error; |
| 8168 | if (!bmap->sample) |
| 8169 | return isl_bool_false; |
| 8170 | total = 1 + isl_basic_map_total_dim(bmap); |
| 8171 | if (bmap->sample->size != total) |
| 8172 | return isl_bool_false; |
| 8173 | return isl_basic_map_contains(bmap, bmap->sample); |
| 8174 | } |
| 8175 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 8176 | isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8177 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 8178 | return isl_basic_map_is_empty(bset_to_bmap(bset)); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8179 | } |
| 8180 | |
| 8181 | struct isl_map *isl_basic_map_union( |
| 8182 | struct isl_basic_map *bmap1, struct isl_basic_map *bmap2) |
| 8183 | { |
| 8184 | struct isl_map *map; |
| 8185 | if (!bmap1 || !bmap2) |
| 8186 | goto error; |
| 8187 | |
| 8188 | isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error); |
| 8189 | |
| 8190 | map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0); |
| 8191 | if (!map) |
| 8192 | goto error; |
| 8193 | map = isl_map_add_basic_map(map, bmap1); |
| 8194 | map = isl_map_add_basic_map(map, bmap2); |
| 8195 | return map; |
| 8196 | error: |
| 8197 | isl_basic_map_free(bmap1); |
| 8198 | isl_basic_map_free(bmap2); |
| 8199 | return NULL; |
| 8200 | } |
| 8201 | |
| 8202 | struct isl_set *isl_basic_set_union( |
| 8203 | struct isl_basic_set *bset1, struct isl_basic_set *bset2) |
| 8204 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 8205 | return set_from_map(isl_basic_map_union(bset_to_bmap(bset1), |
| 8206 | bset_to_bmap(bset2))); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8207 | } |
| 8208 | |
| 8209 | /* Order divs such that any div only depends on previous divs */ |
| 8210 | struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap) |
| 8211 | { |
| 8212 | int i; |
| 8213 | unsigned off; |
| 8214 | |
| 8215 | if (!bmap) |
| 8216 | return NULL; |
| 8217 | |
| 8218 | off = isl_space_dim(bmap->dim, isl_dim_all); |
| 8219 | |
| 8220 | for (i = 0; i < bmap->n_div; ++i) { |
| 8221 | int pos; |
| 8222 | if (isl_int_is_zero(bmap->div[i][0])) |
| 8223 | continue; |
| 8224 | pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i, |
| 8225 | bmap->n_div-i); |
| 8226 | if (pos == -1) |
| 8227 | continue; |
Tobias Grosser | 37034db | 2016-03-25 19:38:18 +0000 | [diff] [blame] | 8228 | if (pos == 0) |
| 8229 | isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal, |
| 8230 | "integer division depends on itself", |
| 8231 | return isl_basic_map_free(bmap)); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8232 | isl_basic_map_swap_div(bmap, i, i + pos); |
| 8233 | --i; |
| 8234 | } |
| 8235 | return bmap; |
| 8236 | } |
| 8237 | |
| 8238 | struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset) |
| 8239 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 8240 | return bset_from_bmap(isl_basic_map_order_divs(bset_to_bmap(bset))); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8241 | } |
| 8242 | |
| 8243 | __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map) |
| 8244 | { |
| 8245 | int i; |
| 8246 | |
| 8247 | if (!map) |
| 8248 | return 0; |
| 8249 | |
| 8250 | for (i = 0; i < map->n; ++i) { |
| 8251 | map->p[i] = isl_basic_map_order_divs(map->p[i]); |
| 8252 | if (!map->p[i]) |
| 8253 | goto error; |
| 8254 | } |
| 8255 | |
| 8256 | return map; |
| 8257 | error: |
| 8258 | isl_map_free(map); |
| 8259 | return NULL; |
| 8260 | } |
| 8261 | |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 8262 | /* Sort the local variables of "bset". |
| 8263 | */ |
| 8264 | __isl_give isl_basic_set *isl_basic_set_sort_divs( |
| 8265 | __isl_take isl_basic_set *bset) |
| 8266 | { |
| 8267 | return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset))); |
| 8268 | } |
| 8269 | |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8270 | /* Apply the expansion computed by isl_merge_divs. |
| 8271 | * The expansion itself is given by "exp" while the resulting |
| 8272 | * list of divs is given by "div". |
Tobias Grosser | a67ac97 | 2016-02-26 11:35:12 +0000 | [diff] [blame] | 8273 | * |
Tobias Grosser | 07b2095 | 2016-06-12 04:30:40 +0000 | [diff] [blame] | 8274 | * Move the integer divisions of "bmap" into the right position |
Tobias Grosser | a67ac97 | 2016-02-26 11:35:12 +0000 | [diff] [blame] | 8275 | * according to "exp" and then introduce the additional integer |
| 8276 | * divisions, adding div constraints. |
| 8277 | * The moving should be done first to avoid moving coefficients |
| 8278 | * in the definitions of the extra integer divisions. |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8279 | */ |
Tobias Grosser | 07b2095 | 2016-06-12 04:30:40 +0000 | [diff] [blame] | 8280 | __isl_give isl_basic_map *isl_basic_map_expand_divs( |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 8281 | __isl_take isl_basic_map *bmap, __isl_take isl_mat *div, int *exp) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8282 | { |
| 8283 | int i, j; |
| 8284 | int n_div; |
| 8285 | |
Tobias Grosser | 07b2095 | 2016-06-12 04:30:40 +0000 | [diff] [blame] | 8286 | bmap = isl_basic_map_cow(bmap); |
| 8287 | if (!bmap || !div) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8288 | goto error; |
| 8289 | |
Tobias Grosser | 07b2095 | 2016-06-12 04:30:40 +0000 | [diff] [blame] | 8290 | if (div->n_row < bmap->n_div) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8291 | isl_die(isl_mat_get_ctx(div), isl_error_invalid, |
| 8292 | "not an expansion", goto error); |
| 8293 | |
Tobias Grosser | 07b2095 | 2016-06-12 04:30:40 +0000 | [diff] [blame] | 8294 | n_div = bmap->n_div; |
| 8295 | bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim), |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8296 | div->n_row - n_div, 0, |
| 8297 | 2 * (div->n_row - n_div)); |
| 8298 | |
| 8299 | for (i = n_div; i < div->n_row; ++i) |
Tobias Grosser | 07b2095 | 2016-06-12 04:30:40 +0000 | [diff] [blame] | 8300 | if (isl_basic_map_alloc_div(bmap) < 0) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8301 | goto error; |
| 8302 | |
Tobias Grosser | a67ac97 | 2016-02-26 11:35:12 +0000 | [diff] [blame] | 8303 | for (j = n_div - 1; j >= 0; --j) { |
| 8304 | if (exp[j] == j) |
| 8305 | break; |
Tobias Grosser | 07b2095 | 2016-06-12 04:30:40 +0000 | [diff] [blame] | 8306 | isl_basic_map_swap_div(bmap, j, exp[j]); |
Tobias Grosser | a67ac97 | 2016-02-26 11:35:12 +0000 | [diff] [blame] | 8307 | } |
| 8308 | j = 0; |
| 8309 | for (i = 0; i < div->n_row; ++i) { |
| 8310 | if (j < n_div && exp[j] == i) { |
| 8311 | j++; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8312 | } else { |
Tobias Grosser | 07b2095 | 2016-06-12 04:30:40 +0000 | [diff] [blame] | 8313 | isl_seq_cpy(bmap->div[i], div->row[i], div->n_col); |
Tobias Grosser | 932ec01 | 2016-07-06 09:11:00 +0000 | [diff] [blame] | 8314 | if (isl_basic_map_div_is_marked_unknown(bmap, i)) |
Tobias Grosser | 07b2095 | 2016-06-12 04:30:40 +0000 | [diff] [blame] | 8315 | continue; |
| 8316 | if (isl_basic_map_add_div_constraints(bmap, i) < 0) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8317 | goto error; |
| 8318 | } |
| 8319 | } |
| 8320 | |
| 8321 | isl_mat_free(div); |
Tobias Grosser | 07b2095 | 2016-06-12 04:30:40 +0000 | [diff] [blame] | 8322 | return bmap; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8323 | error: |
Tobias Grosser | 07b2095 | 2016-06-12 04:30:40 +0000 | [diff] [blame] | 8324 | isl_basic_map_free(bmap); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8325 | isl_mat_free(div); |
| 8326 | return NULL; |
| 8327 | } |
| 8328 | |
Tobias Grosser | 07b2095 | 2016-06-12 04:30:40 +0000 | [diff] [blame] | 8329 | /* Apply the expansion computed by isl_merge_divs. |
| 8330 | * The expansion itself is given by "exp" while the resulting |
| 8331 | * list of divs is given by "div". |
| 8332 | */ |
| 8333 | __isl_give isl_basic_set *isl_basic_set_expand_divs( |
| 8334 | __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp) |
| 8335 | { |
| 8336 | return isl_basic_map_expand_divs(bset, div, exp); |
| 8337 | } |
| 8338 | |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8339 | /* Look for a div in dst that corresponds to the div "div" in src. |
| 8340 | * The divs before "div" in src and dst are assumed to be the same. |
| 8341 | * |
| 8342 | * Returns -1 if no corresponding div was found and the position |
| 8343 | * of the corresponding div in dst otherwise. |
| 8344 | */ |
| 8345 | static int find_div(struct isl_basic_map *dst, |
| 8346 | struct isl_basic_map *src, unsigned div) |
| 8347 | { |
| 8348 | int i; |
| 8349 | |
| 8350 | unsigned total = isl_space_dim(src->dim, isl_dim_all); |
| 8351 | |
| 8352 | isl_assert(dst->ctx, div <= dst->n_div, return -1); |
| 8353 | for (i = div; i < dst->n_div; ++i) |
| 8354 | if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) && |
| 8355 | isl_seq_first_non_zero(dst->div[i]+1+1+total+div, |
| 8356 | dst->n_div - div) == -1) |
| 8357 | return i; |
| 8358 | return -1; |
| 8359 | } |
| 8360 | |
| 8361 | /* Align the divs of "dst" to those of "src", adding divs from "src" |
| 8362 | * if needed. That is, make sure that the first src->n_div divs |
| 8363 | * of the result are equal to those of src. |
| 8364 | * |
| 8365 | * The result is not finalized as by design it will have redundant |
| 8366 | * divs if any divs from "src" were copied. |
| 8367 | */ |
| 8368 | __isl_give isl_basic_map *isl_basic_map_align_divs( |
| 8369 | __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src) |
| 8370 | { |
| 8371 | int i; |
| 8372 | int known, extended; |
| 8373 | unsigned total; |
| 8374 | |
| 8375 | if (!dst || !src) |
| 8376 | return isl_basic_map_free(dst); |
| 8377 | |
| 8378 | if (src->n_div == 0) |
| 8379 | return dst; |
| 8380 | |
| 8381 | known = isl_basic_map_divs_known(src); |
| 8382 | if (known < 0) |
| 8383 | return isl_basic_map_free(dst); |
| 8384 | if (!known) |
| 8385 | isl_die(isl_basic_map_get_ctx(src), isl_error_invalid, |
| 8386 | "some src divs are unknown", |
| 8387 | return isl_basic_map_free(dst)); |
| 8388 | |
| 8389 | src = isl_basic_map_order_divs(src); |
| 8390 | |
| 8391 | extended = 0; |
| 8392 | total = isl_space_dim(src->dim, isl_dim_all); |
| 8393 | for (i = 0; i < src->n_div; ++i) { |
| 8394 | int j = find_div(dst, src, i); |
| 8395 | if (j < 0) { |
| 8396 | if (!extended) { |
| 8397 | int extra = src->n_div - i; |
| 8398 | dst = isl_basic_map_cow(dst); |
| 8399 | if (!dst) |
| 8400 | return NULL; |
| 8401 | dst = isl_basic_map_extend_space(dst, |
| 8402 | isl_space_copy(dst->dim), |
| 8403 | extra, 0, 2 * extra); |
| 8404 | extended = 1; |
| 8405 | } |
| 8406 | j = isl_basic_map_alloc_div(dst); |
| 8407 | if (j < 0) |
| 8408 | return isl_basic_map_free(dst); |
| 8409 | isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i); |
| 8410 | isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i); |
| 8411 | if (isl_basic_map_add_div_constraints(dst, j) < 0) |
| 8412 | return isl_basic_map_free(dst); |
| 8413 | } |
| 8414 | if (j != i) |
| 8415 | isl_basic_map_swap_div(dst, i, j); |
| 8416 | } |
| 8417 | return dst; |
| 8418 | } |
| 8419 | |
| 8420 | struct isl_basic_set *isl_basic_set_align_divs( |
| 8421 | struct isl_basic_set *dst, struct isl_basic_set *src) |
| 8422 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 8423 | return bset_from_bmap(isl_basic_map_align_divs(bset_to_bmap(dst), |
| 8424 | bset_to_bmap(src))); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8425 | } |
| 8426 | |
| 8427 | struct isl_map *isl_map_align_divs(struct isl_map *map) |
| 8428 | { |
| 8429 | int i; |
| 8430 | |
| 8431 | if (!map) |
| 8432 | return NULL; |
| 8433 | if (map->n == 0) |
| 8434 | return map; |
| 8435 | map = isl_map_compute_divs(map); |
| 8436 | map = isl_map_cow(map); |
| 8437 | if (!map) |
| 8438 | return NULL; |
| 8439 | |
| 8440 | for (i = 1; i < map->n; ++i) |
| 8441 | map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]); |
| 8442 | for (i = 1; i < map->n; ++i) { |
| 8443 | map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]); |
| 8444 | if (!map->p[i]) |
| 8445 | return isl_map_free(map); |
| 8446 | } |
| 8447 | |
| 8448 | ISL_F_CLR(map, ISL_MAP_NORMALIZED); |
| 8449 | return map; |
| 8450 | } |
| 8451 | |
| 8452 | struct isl_set *isl_set_align_divs(struct isl_set *set) |
| 8453 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 8454 | return set_from_map(isl_map_align_divs(set_to_map(set))); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8455 | } |
| 8456 | |
Tobias Grosser | 1fa7b97 | 2015-02-16 19:33:40 +0000 | [diff] [blame] | 8457 | /* Align the divs of the basic maps in "map" to those |
| 8458 | * of the basic maps in "list", as well as to the other basic maps in "map". |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8459 | * The elements in "list" are assumed to have known divs. |
| 8460 | */ |
Tobias Grosser | 1fa7b97 | 2015-02-16 19:33:40 +0000 | [diff] [blame] | 8461 | __isl_give isl_map *isl_map_align_divs_to_basic_map_list( |
| 8462 | __isl_take isl_map *map, __isl_keep isl_basic_map_list *list) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8463 | { |
| 8464 | int i, n; |
| 8465 | |
Tobias Grosser | 1fa7b97 | 2015-02-16 19:33:40 +0000 | [diff] [blame] | 8466 | map = isl_map_compute_divs(map); |
| 8467 | map = isl_map_cow(map); |
| 8468 | if (!map || !list) |
| 8469 | return isl_map_free(map); |
| 8470 | if (map->n == 0) |
| 8471 | return map; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8472 | |
Tobias Grosser | 1fa7b97 | 2015-02-16 19:33:40 +0000 | [diff] [blame] | 8473 | n = isl_basic_map_list_n_basic_map(list); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8474 | for (i = 0; i < n; ++i) { |
Tobias Grosser | 1fa7b97 | 2015-02-16 19:33:40 +0000 | [diff] [blame] | 8475 | isl_basic_map *bmap; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8476 | |
Tobias Grosser | 1fa7b97 | 2015-02-16 19:33:40 +0000 | [diff] [blame] | 8477 | bmap = isl_basic_map_list_get_basic_map(list, i); |
| 8478 | map->p[0] = isl_basic_map_align_divs(map->p[0], bmap); |
| 8479 | isl_basic_map_free(bmap); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8480 | } |
Tobias Grosser | 1fa7b97 | 2015-02-16 19:33:40 +0000 | [diff] [blame] | 8481 | if (!map->p[0]) |
| 8482 | return isl_map_free(map); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8483 | |
Tobias Grosser | 1fa7b97 | 2015-02-16 19:33:40 +0000 | [diff] [blame] | 8484 | return isl_map_align_divs(map); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8485 | } |
| 8486 | |
Tobias Grosser | 1fa7b97 | 2015-02-16 19:33:40 +0000 | [diff] [blame] | 8487 | /* Align the divs of each element of "list" to those of "bmap". |
| 8488 | * Both "bmap" and the elements of "list" are assumed to have known divs. |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8489 | */ |
Tobias Grosser | 1fa7b97 | 2015-02-16 19:33:40 +0000 | [diff] [blame] | 8490 | __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map( |
| 8491 | __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8492 | { |
| 8493 | int i, n; |
| 8494 | |
Tobias Grosser | 1fa7b97 | 2015-02-16 19:33:40 +0000 | [diff] [blame] | 8495 | if (!list || !bmap) |
| 8496 | return isl_basic_map_list_free(list); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8497 | |
Tobias Grosser | 1fa7b97 | 2015-02-16 19:33:40 +0000 | [diff] [blame] | 8498 | n = isl_basic_map_list_n_basic_map(list); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8499 | for (i = 0; i < n; ++i) { |
Tobias Grosser | 1fa7b97 | 2015-02-16 19:33:40 +0000 | [diff] [blame] | 8500 | isl_basic_map *bmap_i; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8501 | |
Tobias Grosser | 1fa7b97 | 2015-02-16 19:33:40 +0000 | [diff] [blame] | 8502 | bmap_i = isl_basic_map_list_get_basic_map(list, i); |
| 8503 | bmap_i = isl_basic_map_align_divs(bmap_i, bmap); |
| 8504 | list = isl_basic_map_list_set_basic_map(list, i, bmap_i); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8505 | } |
| 8506 | |
| 8507 | return list; |
| 8508 | } |
| 8509 | |
| 8510 | static __isl_give isl_set *set_apply( __isl_take isl_set *set, |
| 8511 | __isl_take isl_map *map) |
| 8512 | { |
| 8513 | if (!set || !map) |
| 8514 | goto error; |
| 8515 | isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error); |
| 8516 | map = isl_map_intersect_domain(map, set); |
| 8517 | set = isl_map_range(map); |
| 8518 | return set; |
| 8519 | error: |
| 8520 | isl_set_free(set); |
| 8521 | isl_map_free(map); |
| 8522 | return NULL; |
| 8523 | } |
| 8524 | |
| 8525 | __isl_give isl_set *isl_set_apply( __isl_take isl_set *set, |
| 8526 | __isl_take isl_map *map) |
| 8527 | { |
| 8528 | return isl_map_align_params_map_map_and(set, map, &set_apply); |
| 8529 | } |
| 8530 | |
| 8531 | /* There is no need to cow as removing empty parts doesn't change |
| 8532 | * the meaning of the set. |
| 8533 | */ |
| 8534 | struct isl_map *isl_map_remove_empty_parts(struct isl_map *map) |
| 8535 | { |
| 8536 | int i; |
| 8537 | |
| 8538 | if (!map) |
| 8539 | return NULL; |
| 8540 | |
| 8541 | for (i = map->n - 1; i >= 0; --i) |
| 8542 | remove_if_empty(map, i); |
| 8543 | |
| 8544 | return map; |
| 8545 | } |
| 8546 | |
| 8547 | struct isl_set *isl_set_remove_empty_parts(struct isl_set *set) |
| 8548 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 8549 | return set_from_map(isl_map_remove_empty_parts(set_to_map(set))); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8550 | } |
| 8551 | |
Tobias Grosser | eab0943e | 2016-11-22 21:31:59 +0000 | [diff] [blame] | 8552 | static __isl_give isl_basic_map *map_copy_basic_map(__isl_keep isl_map *map) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8553 | { |
| 8554 | struct isl_basic_map *bmap; |
| 8555 | if (!map || map->n == 0) |
| 8556 | return NULL; |
| 8557 | bmap = map->p[map->n-1]; |
| 8558 | isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL); |
| 8559 | return isl_basic_map_copy(bmap); |
| 8560 | } |
| 8561 | |
Tobias Grosser | eab0943e | 2016-11-22 21:31:59 +0000 | [diff] [blame] | 8562 | __isl_give isl_basic_map *isl_map_copy_basic_map(__isl_keep isl_map *map) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8563 | { |
Tobias Grosser | eab0943e | 2016-11-22 21:31:59 +0000 | [diff] [blame] | 8564 | return map_copy_basic_map(map); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8565 | } |
| 8566 | |
Tobias Grosser | eab0943e | 2016-11-22 21:31:59 +0000 | [diff] [blame] | 8567 | struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set) |
| 8568 | { |
| 8569 | return bset_from_bmap(map_copy_basic_map(set_to_map(set))); |
| 8570 | } |
| 8571 | |
| 8572 | static __isl_give isl_map *map_drop_basic_map(__isl_take isl_map *map, |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8573 | __isl_keep isl_basic_map *bmap) |
| 8574 | { |
| 8575 | int i; |
| 8576 | |
| 8577 | if (!map || !bmap) |
| 8578 | goto error; |
| 8579 | for (i = map->n-1; i >= 0; --i) { |
| 8580 | if (map->p[i] != bmap) |
| 8581 | continue; |
| 8582 | map = isl_map_cow(map); |
| 8583 | if (!map) |
| 8584 | goto error; |
| 8585 | isl_basic_map_free(map->p[i]); |
| 8586 | if (i != map->n-1) { |
| 8587 | ISL_F_CLR(map, ISL_SET_NORMALIZED); |
| 8588 | map->p[i] = map->p[map->n-1]; |
| 8589 | } |
| 8590 | map->n--; |
| 8591 | return map; |
| 8592 | } |
| 8593 | return map; |
| 8594 | error: |
| 8595 | isl_map_free(map); |
| 8596 | return NULL; |
| 8597 | } |
| 8598 | |
Tobias Grosser | eab0943e | 2016-11-22 21:31:59 +0000 | [diff] [blame] | 8599 | __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map, |
| 8600 | __isl_keep isl_basic_map *bmap) |
| 8601 | { |
| 8602 | return map_drop_basic_map(map, bmap); |
| 8603 | } |
| 8604 | |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8605 | struct isl_set *isl_set_drop_basic_set(struct isl_set *set, |
| 8606 | struct isl_basic_set *bset) |
| 8607 | { |
Tobias Grosser | eab0943e | 2016-11-22 21:31:59 +0000 | [diff] [blame] | 8608 | return set_from_map(map_drop_basic_map(set_to_map(set), |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 8609 | bset_to_bmap(bset))); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8610 | } |
| 8611 | |
| 8612 | /* Given two basic sets bset1 and bset2, compute the maximal difference |
| 8613 | * between the values of dimension pos in bset1 and those in bset2 |
| 8614 | * for any common value of the parameters and dimensions preceding pos. |
| 8615 | */ |
| 8616 | static enum isl_lp_result basic_set_maximal_difference_at( |
| 8617 | __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2, |
| 8618 | int pos, isl_int *opt) |
| 8619 | { |
Tobias Grosser | eab0943e | 2016-11-22 21:31:59 +0000 | [diff] [blame] | 8620 | isl_basic_map *bmap1; |
| 8621 | isl_basic_map *bmap2; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8622 | struct isl_ctx *ctx; |
| 8623 | struct isl_vec *obj; |
| 8624 | unsigned total; |
| 8625 | unsigned nparam; |
Tobias Grosser | eab0943e | 2016-11-22 21:31:59 +0000 | [diff] [blame] | 8626 | unsigned dim1; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8627 | enum isl_lp_result res; |
| 8628 | |
| 8629 | if (!bset1 || !bset2) |
| 8630 | return isl_lp_error; |
| 8631 | |
| 8632 | nparam = isl_basic_set_n_param(bset1); |
| 8633 | dim1 = isl_basic_set_n_dim(bset1); |
Tobias Grosser | eab0943e | 2016-11-22 21:31:59 +0000 | [diff] [blame] | 8634 | |
| 8635 | bmap1 = isl_basic_map_from_range(isl_basic_set_copy(bset1)); |
| 8636 | bmap2 = isl_basic_map_from_range(isl_basic_set_copy(bset2)); |
| 8637 | bmap1 = isl_basic_map_move_dims(bmap1, isl_dim_in, 0, |
| 8638 | isl_dim_out, 0, pos); |
| 8639 | bmap2 = isl_basic_map_move_dims(bmap2, isl_dim_in, 0, |
| 8640 | isl_dim_out, 0, pos); |
| 8641 | bmap1 = isl_basic_map_range_product(bmap1, bmap2); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8642 | if (!bmap1) |
Tobias Grosser | eab0943e | 2016-11-22 21:31:59 +0000 | [diff] [blame] | 8643 | return isl_lp_error; |
| 8644 | |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8645 | total = isl_basic_map_total_dim(bmap1); |
| 8646 | ctx = bmap1->ctx; |
| 8647 | obj = isl_vec_alloc(ctx, 1 + total); |
| 8648 | if (!obj) |
Tobias Grosser | eab0943e | 2016-11-22 21:31:59 +0000 | [diff] [blame] | 8649 | goto error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8650 | isl_seq_clr(obj->block.data, 1 + total); |
| 8651 | isl_int_set_si(obj->block.data[1+nparam+pos], 1); |
| 8652 | isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1); |
| 8653 | res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one, |
| 8654 | opt, NULL, NULL); |
| 8655 | isl_basic_map_free(bmap1); |
| 8656 | isl_vec_free(obj); |
| 8657 | return res; |
| 8658 | error: |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8659 | isl_basic_map_free(bmap1); |
| 8660 | return isl_lp_error; |
| 8661 | } |
| 8662 | |
| 8663 | /* Given two _disjoint_ basic sets bset1 and bset2, check whether |
| 8664 | * for any common value of the parameters and dimensions preceding pos |
| 8665 | * in both basic sets, the values of dimension pos in bset1 are |
| 8666 | * smaller or larger than those in bset2. |
| 8667 | * |
| 8668 | * Returns |
| 8669 | * 1 if bset1 follows bset2 |
| 8670 | * -1 if bset1 precedes bset2 |
| 8671 | * 0 if bset1 and bset2 are incomparable |
| 8672 | * -2 if some error occurred. |
| 8673 | */ |
| 8674 | int isl_basic_set_compare_at(struct isl_basic_set *bset1, |
| 8675 | struct isl_basic_set *bset2, int pos) |
| 8676 | { |
| 8677 | isl_int opt; |
| 8678 | enum isl_lp_result res; |
| 8679 | int cmp; |
| 8680 | |
| 8681 | isl_int_init(opt); |
| 8682 | |
| 8683 | res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt); |
| 8684 | |
| 8685 | if (res == isl_lp_empty) |
| 8686 | cmp = 0; |
| 8687 | else if ((res == isl_lp_ok && isl_int_is_pos(opt)) || |
| 8688 | res == isl_lp_unbounded) |
| 8689 | cmp = 1; |
| 8690 | else if (res == isl_lp_ok && isl_int_is_neg(opt)) |
| 8691 | cmp = -1; |
| 8692 | else |
| 8693 | cmp = -2; |
| 8694 | |
| 8695 | isl_int_clear(opt); |
| 8696 | return cmp; |
| 8697 | } |
| 8698 | |
| 8699 | /* Given two basic sets bset1 and bset2, check whether |
| 8700 | * for any common value of the parameters and dimensions preceding pos |
| 8701 | * there is a value of dimension pos in bset1 that is larger |
| 8702 | * than a value of the same dimension in bset2. |
| 8703 | * |
| 8704 | * Return |
| 8705 | * 1 if there exists such a pair |
| 8706 | * 0 if there is no such pair, but there is a pair of equal values |
| 8707 | * -1 otherwise |
| 8708 | * -2 if some error occurred. |
| 8709 | */ |
| 8710 | int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1, |
| 8711 | __isl_keep isl_basic_set *bset2, int pos) |
| 8712 | { |
| 8713 | isl_int opt; |
| 8714 | enum isl_lp_result res; |
| 8715 | int cmp; |
| 8716 | |
| 8717 | isl_int_init(opt); |
| 8718 | |
| 8719 | res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt); |
| 8720 | |
| 8721 | if (res == isl_lp_empty) |
| 8722 | cmp = -1; |
| 8723 | else if ((res == isl_lp_ok && isl_int_is_pos(opt)) || |
| 8724 | res == isl_lp_unbounded) |
| 8725 | cmp = 1; |
| 8726 | else if (res == isl_lp_ok && isl_int_is_neg(opt)) |
| 8727 | cmp = -1; |
| 8728 | else if (res == isl_lp_ok) |
| 8729 | cmp = 0; |
| 8730 | else |
| 8731 | cmp = -2; |
| 8732 | |
| 8733 | isl_int_clear(opt); |
| 8734 | return cmp; |
| 8735 | } |
| 8736 | |
| 8737 | /* Given two sets set1 and set2, check whether |
| 8738 | * for any common value of the parameters and dimensions preceding pos |
| 8739 | * there is a value of dimension pos in set1 that is larger |
| 8740 | * than a value of the same dimension in set2. |
| 8741 | * |
| 8742 | * Return |
| 8743 | * 1 if there exists such a pair |
| 8744 | * 0 if there is no such pair, but there is a pair of equal values |
| 8745 | * -1 otherwise |
| 8746 | * -2 if some error occurred. |
| 8747 | */ |
| 8748 | int isl_set_follows_at(__isl_keep isl_set *set1, |
| 8749 | __isl_keep isl_set *set2, int pos) |
| 8750 | { |
| 8751 | int i, j; |
| 8752 | int follows = -1; |
| 8753 | |
| 8754 | if (!set1 || !set2) |
| 8755 | return -2; |
| 8756 | |
| 8757 | for (i = 0; i < set1->n; ++i) |
| 8758 | for (j = 0; j < set2->n; ++j) { |
| 8759 | int f; |
| 8760 | f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos); |
| 8761 | if (f == 1 || f == -2) |
| 8762 | return f; |
| 8763 | if (f > follows) |
| 8764 | follows = f; |
| 8765 | } |
| 8766 | |
| 8767 | return follows; |
| 8768 | } |
| 8769 | |
| 8770 | static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map *bmap, |
| 8771 | unsigned pos, isl_int *val) |
| 8772 | { |
| 8773 | int i; |
| 8774 | int d; |
| 8775 | unsigned total; |
| 8776 | |
| 8777 | if (!bmap) |
| 8778 | return -1; |
| 8779 | total = isl_basic_map_total_dim(bmap); |
| 8780 | for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) { |
| 8781 | for (; d+1 > pos; --d) |
| 8782 | if (!isl_int_is_zero(bmap->eq[i][1+d])) |
| 8783 | break; |
| 8784 | if (d != pos) |
| 8785 | continue; |
| 8786 | if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1) |
| 8787 | return 0; |
| 8788 | if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1) |
| 8789 | return 0; |
| 8790 | if (!isl_int_is_one(bmap->eq[i][1+d])) |
| 8791 | return 0; |
| 8792 | if (val) |
| 8793 | isl_int_neg(*val, bmap->eq[i][0]); |
| 8794 | return 1; |
| 8795 | } |
| 8796 | return 0; |
| 8797 | } |
| 8798 | |
| 8799 | static int isl_map_plain_has_fixed_var(__isl_keep isl_map *map, |
| 8800 | unsigned pos, isl_int *val) |
| 8801 | { |
| 8802 | int i; |
| 8803 | isl_int v; |
| 8804 | isl_int tmp; |
| 8805 | int fixed; |
| 8806 | |
| 8807 | if (!map) |
| 8808 | return -1; |
| 8809 | if (map->n == 0) |
| 8810 | return 0; |
| 8811 | if (map->n == 1) |
| 8812 | return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val); |
| 8813 | isl_int_init(v); |
| 8814 | isl_int_init(tmp); |
| 8815 | fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v); |
| 8816 | for (i = 1; fixed == 1 && i < map->n; ++i) { |
| 8817 | fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp); |
| 8818 | if (fixed == 1 && isl_int_ne(tmp, v)) |
| 8819 | fixed = 0; |
| 8820 | } |
| 8821 | if (val) |
| 8822 | isl_int_set(*val, v); |
| 8823 | isl_int_clear(tmp); |
| 8824 | isl_int_clear(v); |
| 8825 | return fixed; |
| 8826 | } |
| 8827 | |
| 8828 | static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set *bset, |
| 8829 | unsigned pos, isl_int *val) |
| 8830 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 8831 | return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset), |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8832 | pos, val); |
| 8833 | } |
| 8834 | |
| 8835 | static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos, |
| 8836 | isl_int *val) |
| 8837 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 8838 | return isl_map_plain_has_fixed_var(set_to_map(set), pos, val); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8839 | } |
| 8840 | |
| 8841 | int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap, |
| 8842 | enum isl_dim_type type, unsigned pos, isl_int *val) |
| 8843 | { |
| 8844 | if (pos >= isl_basic_map_dim(bmap, type)) |
| 8845 | return -1; |
| 8846 | return isl_basic_map_plain_has_fixed_var(bmap, |
| 8847 | isl_basic_map_offset(bmap, type) - 1 + pos, val); |
| 8848 | } |
| 8849 | |
| 8850 | /* If "bmap" obviously lies on a hyperplane where the given dimension |
| 8851 | * has a fixed value, then return that value. |
| 8852 | * Otherwise return NaN. |
| 8853 | */ |
| 8854 | __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed( |
| 8855 | __isl_keep isl_basic_map *bmap, |
| 8856 | enum isl_dim_type type, unsigned pos) |
| 8857 | { |
| 8858 | isl_ctx *ctx; |
| 8859 | isl_val *v; |
| 8860 | int fixed; |
| 8861 | |
| 8862 | if (!bmap) |
| 8863 | return NULL; |
| 8864 | ctx = isl_basic_map_get_ctx(bmap); |
| 8865 | v = isl_val_alloc(ctx); |
| 8866 | if (!v) |
| 8867 | return NULL; |
| 8868 | fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n); |
| 8869 | if (fixed < 0) |
| 8870 | return isl_val_free(v); |
| 8871 | if (fixed) { |
| 8872 | isl_int_set_si(v->d, 1); |
| 8873 | return v; |
| 8874 | } |
| 8875 | isl_val_free(v); |
| 8876 | return isl_val_nan(ctx); |
| 8877 | } |
| 8878 | |
| 8879 | int isl_map_plain_is_fixed(__isl_keep isl_map *map, |
| 8880 | enum isl_dim_type type, unsigned pos, isl_int *val) |
| 8881 | { |
| 8882 | if (pos >= isl_map_dim(map, type)) |
| 8883 | return -1; |
| 8884 | return isl_map_plain_has_fixed_var(map, |
| 8885 | map_offset(map, type) - 1 + pos, val); |
| 8886 | } |
| 8887 | |
| 8888 | /* If "map" obviously lies on a hyperplane where the given dimension |
| 8889 | * has a fixed value, then return that value. |
| 8890 | * Otherwise return NaN. |
| 8891 | */ |
| 8892 | __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map, |
| 8893 | enum isl_dim_type type, unsigned pos) |
| 8894 | { |
| 8895 | isl_ctx *ctx; |
| 8896 | isl_val *v; |
| 8897 | int fixed; |
| 8898 | |
| 8899 | if (!map) |
| 8900 | return NULL; |
| 8901 | ctx = isl_map_get_ctx(map); |
| 8902 | v = isl_val_alloc(ctx); |
| 8903 | if (!v) |
| 8904 | return NULL; |
| 8905 | fixed = isl_map_plain_is_fixed(map, type, pos, &v->n); |
| 8906 | if (fixed < 0) |
| 8907 | return isl_val_free(v); |
| 8908 | if (fixed) { |
| 8909 | isl_int_set_si(v->d, 1); |
| 8910 | return v; |
| 8911 | } |
| 8912 | isl_val_free(v); |
| 8913 | return isl_val_nan(ctx); |
| 8914 | } |
| 8915 | |
| 8916 | /* If "set" obviously lies on a hyperplane where the given dimension |
| 8917 | * has a fixed value, then return that value. |
| 8918 | * Otherwise return NaN. |
| 8919 | */ |
| 8920 | __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set, |
| 8921 | enum isl_dim_type type, unsigned pos) |
| 8922 | { |
| 8923 | return isl_map_plain_get_val_if_fixed(set, type, pos); |
| 8924 | } |
| 8925 | |
| 8926 | int isl_set_plain_is_fixed(__isl_keep isl_set *set, |
| 8927 | enum isl_dim_type type, unsigned pos, isl_int *val) |
| 8928 | { |
| 8929 | return isl_map_plain_is_fixed(set, type, pos, val); |
| 8930 | } |
| 8931 | |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8932 | /* Check if dimension dim has fixed value and if so and if val is not NULL, |
| 8933 | * then return this fixed value in *val. |
| 8934 | */ |
| 8935 | int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset, |
| 8936 | unsigned dim, isl_int *val) |
| 8937 | { |
| 8938 | return isl_basic_set_plain_has_fixed_var(bset, |
| 8939 | isl_basic_set_n_param(bset) + dim, val); |
| 8940 | } |
| 8941 | |
| 8942 | /* Check if dimension dim has fixed value and if so and if val is not NULL, |
| 8943 | * then return this fixed value in *val. |
| 8944 | */ |
| 8945 | int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set, |
| 8946 | unsigned dim, isl_int *val) |
| 8947 | { |
| 8948 | return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val); |
| 8949 | } |
| 8950 | |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 8951 | /* Check if input variable in has fixed value and if so and if val is not NULL, |
| 8952 | * then return this fixed value in *val. |
| 8953 | */ |
| 8954 | int isl_map_plain_input_is_fixed(__isl_keep isl_map *map, |
| 8955 | unsigned in, isl_int *val) |
| 8956 | { |
| 8957 | return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val); |
| 8958 | } |
| 8959 | |
| 8960 | /* Check if dimension dim has an (obvious) fixed lower bound and if so |
| 8961 | * and if val is not NULL, then return this lower bound in *val. |
| 8962 | */ |
| 8963 | int isl_basic_set_plain_dim_has_fixed_lower_bound( |
| 8964 | __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val) |
| 8965 | { |
| 8966 | int i, i_eq = -1, i_ineq = -1; |
| 8967 | isl_int *c; |
| 8968 | unsigned total; |
| 8969 | unsigned nparam; |
| 8970 | |
| 8971 | if (!bset) |
| 8972 | return -1; |
| 8973 | total = isl_basic_set_total_dim(bset); |
| 8974 | nparam = isl_basic_set_n_param(bset); |
| 8975 | for (i = 0; i < bset->n_eq; ++i) { |
| 8976 | if (isl_int_is_zero(bset->eq[i][1+nparam+dim])) |
| 8977 | continue; |
| 8978 | if (i_eq != -1) |
| 8979 | return 0; |
| 8980 | i_eq = i; |
| 8981 | } |
| 8982 | for (i = 0; i < bset->n_ineq; ++i) { |
| 8983 | if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim])) |
| 8984 | continue; |
| 8985 | if (i_eq != -1 || i_ineq != -1) |
| 8986 | return 0; |
| 8987 | i_ineq = i; |
| 8988 | } |
| 8989 | if (i_eq == -1 && i_ineq == -1) |
| 8990 | return 0; |
| 8991 | c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq]; |
| 8992 | /* The coefficient should always be one due to normalization. */ |
| 8993 | if (!isl_int_is_one(c[1+nparam+dim])) |
| 8994 | return 0; |
| 8995 | if (isl_seq_first_non_zero(c+1, nparam+dim) != -1) |
| 8996 | return 0; |
| 8997 | if (isl_seq_first_non_zero(c+1+nparam+dim+1, |
| 8998 | total - nparam - dim - 1) != -1) |
| 8999 | return 0; |
| 9000 | if (val) |
| 9001 | isl_int_neg(*val, c[0]); |
| 9002 | return 1; |
| 9003 | } |
| 9004 | |
| 9005 | int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set, |
| 9006 | unsigned dim, isl_int *val) |
| 9007 | { |
| 9008 | int i; |
| 9009 | isl_int v; |
| 9010 | isl_int tmp; |
| 9011 | int fixed; |
| 9012 | |
| 9013 | if (!set) |
| 9014 | return -1; |
| 9015 | if (set->n == 0) |
| 9016 | return 0; |
| 9017 | if (set->n == 1) |
| 9018 | return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0], |
| 9019 | dim, val); |
| 9020 | isl_int_init(v); |
| 9021 | isl_int_init(tmp); |
| 9022 | fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0], |
| 9023 | dim, &v); |
| 9024 | for (i = 1; fixed == 1 && i < set->n; ++i) { |
| 9025 | fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i], |
| 9026 | dim, &tmp); |
| 9027 | if (fixed == 1 && isl_int_ne(tmp, v)) |
| 9028 | fixed = 0; |
| 9029 | } |
| 9030 | if (val) |
| 9031 | isl_int_set(*val, v); |
| 9032 | isl_int_clear(tmp); |
| 9033 | isl_int_clear(v); |
| 9034 | return fixed; |
| 9035 | } |
| 9036 | |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 9037 | /* Return -1 if the constraint "c1" should be sorted before "c2" |
| 9038 | * and 1 if it should be sorted after "c2". |
| 9039 | * Return 0 if the two constraints are the same (up to the constant term). |
| 9040 | * |
| 9041 | * In particular, if a constraint involves later variables than another |
| 9042 | * then it is sorted after this other constraint. |
| 9043 | * uset_gist depends on constraints without existentially quantified |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 9044 | * variables sorting first. |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 9045 | * |
| 9046 | * For constraints that have the same latest variable, those |
| 9047 | * with the same coefficient for this latest variable (first in absolute value |
| 9048 | * and then in actual value) are grouped together. |
| 9049 | * This is useful for detecting pairs of constraints that can |
| 9050 | * be chained in their printed representation. |
| 9051 | * |
| 9052 | * Finally, within a group, constraints are sorted according to |
| 9053 | * their coefficients (excluding the constant term). |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 9054 | */ |
| 9055 | static int sort_constraint_cmp(const void *p1, const void *p2, void *arg) |
| 9056 | { |
| 9057 | isl_int **c1 = (isl_int **) p1; |
| 9058 | isl_int **c2 = (isl_int **) p2; |
| 9059 | int l1, l2; |
| 9060 | unsigned size = *(unsigned *) arg; |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 9061 | int cmp; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 9062 | |
| 9063 | l1 = isl_seq_last_non_zero(*c1 + 1, size); |
| 9064 | l2 = isl_seq_last_non_zero(*c2 + 1, size); |
| 9065 | |
| 9066 | if (l1 != l2) |
| 9067 | return l1 - l2; |
| 9068 | |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 9069 | cmp = isl_int_abs_cmp((*c1)[1 + l1], (*c2)[1 + l1]); |
| 9070 | if (cmp != 0) |
| 9071 | return cmp; |
| 9072 | cmp = isl_int_cmp((*c1)[1 + l1], (*c2)[1 + l1]); |
| 9073 | if (cmp != 0) |
| 9074 | return -cmp; |
| 9075 | |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 9076 | return isl_seq_cmp(*c1 + 1, *c2 + 1, size); |
| 9077 | } |
| 9078 | |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 9079 | /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2" |
| 9080 | * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2" |
| 9081 | * and 0 if the two constraints are the same (up to the constant term). |
| 9082 | */ |
| 9083 | int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map *bmap, |
| 9084 | isl_int *c1, isl_int *c2) |
| 9085 | { |
| 9086 | unsigned total; |
| 9087 | |
| 9088 | if (!bmap) |
| 9089 | return -2; |
| 9090 | total = isl_basic_map_total_dim(bmap); |
| 9091 | return sort_constraint_cmp(&c1, &c2, &total); |
| 9092 | } |
| 9093 | |
| 9094 | __isl_give isl_basic_map *isl_basic_map_sort_constraints( |
| 9095 | __isl_take isl_basic_map *bmap) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 9096 | { |
| 9097 | unsigned total; |
| 9098 | |
| 9099 | if (!bmap) |
| 9100 | return NULL; |
| 9101 | if (bmap->n_ineq == 0) |
| 9102 | return bmap; |
| 9103 | if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED)) |
| 9104 | return bmap; |
| 9105 | total = isl_basic_map_total_dim(bmap); |
| 9106 | if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *), |
| 9107 | &sort_constraint_cmp, &total) < 0) |
| 9108 | return isl_basic_map_free(bmap); |
| 9109 | return bmap; |
| 9110 | } |
| 9111 | |
| 9112 | __isl_give isl_basic_set *isl_basic_set_sort_constraints( |
| 9113 | __isl_take isl_basic_set *bset) |
| 9114 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 9115 | isl_basic_map *bmap = bset_to_bmap(bset); |
| 9116 | return bset_from_bmap(isl_basic_map_sort_constraints(bmap)); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 9117 | } |
| 9118 | |
| 9119 | struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap) |
| 9120 | { |
| 9121 | if (!bmap) |
| 9122 | return NULL; |
| 9123 | if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED)) |
| 9124 | return bmap; |
| 9125 | bmap = isl_basic_map_remove_redundancies(bmap); |
| 9126 | bmap = isl_basic_map_sort_constraints(bmap); |
| 9127 | if (bmap) |
| 9128 | ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED); |
| 9129 | return bmap; |
| 9130 | } |
| 9131 | |
| 9132 | struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset) |
| 9133 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 9134 | return bset_from_bmap(isl_basic_map_normalize(bset_to_bmap(bset))); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 9135 | } |
| 9136 | |
| 9137 | int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1, |
| 9138 | const __isl_keep isl_basic_map *bmap2) |
| 9139 | { |
| 9140 | int i, cmp; |
| 9141 | unsigned total; |
| 9142 | |
Tobias Grosser | 3e6070e | 2015-05-09 09:37:30 +0000 | [diff] [blame] | 9143 | if (!bmap1 || !bmap2) |
| 9144 | return -1; |
| 9145 | |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 9146 | if (bmap1 == bmap2) |
| 9147 | return 0; |
| 9148 | if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) != |
| 9149 | ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL)) |
| 9150 | return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1; |
| 9151 | if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2)) |
| 9152 | return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2); |
| 9153 | if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2)) |
| 9154 | return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2); |
| 9155 | if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2)) |
| 9156 | return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2); |
| 9157 | if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) && |
| 9158 | ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY)) |
| 9159 | return 0; |
| 9160 | if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY)) |
| 9161 | return 1; |
| 9162 | if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY)) |
| 9163 | return -1; |
| 9164 | if (bmap1->n_eq != bmap2->n_eq) |
| 9165 | return bmap1->n_eq - bmap2->n_eq; |
| 9166 | if (bmap1->n_ineq != bmap2->n_ineq) |
| 9167 | return bmap1->n_ineq - bmap2->n_ineq; |
| 9168 | if (bmap1->n_div != bmap2->n_div) |
| 9169 | return bmap1->n_div - bmap2->n_div; |
| 9170 | total = isl_basic_map_total_dim(bmap1); |
| 9171 | for (i = 0; i < bmap1->n_eq; ++i) { |
| 9172 | cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total); |
| 9173 | if (cmp) |
| 9174 | return cmp; |
| 9175 | } |
| 9176 | for (i = 0; i < bmap1->n_ineq; ++i) { |
| 9177 | cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total); |
| 9178 | if (cmp) |
| 9179 | return cmp; |
| 9180 | } |
| 9181 | for (i = 0; i < bmap1->n_div; ++i) { |
| 9182 | cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total); |
| 9183 | if (cmp) |
| 9184 | return cmp; |
| 9185 | } |
| 9186 | return 0; |
| 9187 | } |
| 9188 | |
| 9189 | int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set *bset1, |
| 9190 | const __isl_keep isl_basic_set *bset2) |
| 9191 | { |
| 9192 | return isl_basic_map_plain_cmp(bset1, bset2); |
| 9193 | } |
| 9194 | |
| 9195 | int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2) |
| 9196 | { |
| 9197 | int i, cmp; |
| 9198 | |
| 9199 | if (set1 == set2) |
| 9200 | return 0; |
| 9201 | if (set1->n != set2->n) |
| 9202 | return set1->n - set2->n; |
| 9203 | |
| 9204 | for (i = 0; i < set1->n; ++i) { |
| 9205 | cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]); |
| 9206 | if (cmp) |
| 9207 | return cmp; |
| 9208 | } |
| 9209 | |
| 9210 | return 0; |
| 9211 | } |
| 9212 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 9213 | isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1, |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 9214 | __isl_keep isl_basic_map *bmap2) |
| 9215 | { |
Tobias Grosser | e0f8d59 | 2015-05-13 13:10:13 +0000 | [diff] [blame] | 9216 | if (!bmap1 || !bmap2) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 9217 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 9218 | return isl_basic_map_plain_cmp(bmap1, bmap2) == 0; |
| 9219 | } |
| 9220 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 9221 | isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1, |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 9222 | __isl_keep isl_basic_set *bset2) |
| 9223 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 9224 | return isl_basic_map_plain_is_equal(bset_to_bmap(bset1), |
| 9225 | bset_to_bmap(bset2)); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 9226 | } |
| 9227 | |
| 9228 | static int qsort_bmap_cmp(const void *p1, const void *p2) |
| 9229 | { |
| 9230 | const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1; |
| 9231 | const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2; |
| 9232 | |
| 9233 | return isl_basic_map_plain_cmp(bmap1, bmap2); |
| 9234 | } |
| 9235 | |
| 9236 | /* Sort the basic maps of "map" and remove duplicate basic maps. |
| 9237 | * |
| 9238 | * While removing basic maps, we make sure that the basic maps remain |
| 9239 | * sorted because isl_map_normalize expects the basic maps of the result |
| 9240 | * to be sorted. |
| 9241 | */ |
| 9242 | static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map) |
| 9243 | { |
| 9244 | int i, j; |
| 9245 | |
| 9246 | map = isl_map_remove_empty_parts(map); |
| 9247 | if (!map) |
| 9248 | return NULL; |
| 9249 | qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp); |
| 9250 | for (i = map->n - 1; i >= 1; --i) { |
| 9251 | if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i])) |
| 9252 | continue; |
| 9253 | isl_basic_map_free(map->p[i-1]); |
| 9254 | for (j = i; j < map->n; ++j) |
| 9255 | map->p[j - 1] = map->p[j]; |
| 9256 | map->n--; |
| 9257 | } |
| 9258 | |
| 9259 | return map; |
| 9260 | } |
| 9261 | |
| 9262 | /* Remove obvious duplicates among the basic maps of "map". |
| 9263 | * |
| 9264 | * Unlike isl_map_normalize, this function does not remove redundant |
| 9265 | * constraints and only removes duplicates that have exactly the same |
| 9266 | * constraints in the input. It does sort the constraints and |
| 9267 | * the basic maps to ease the detection of duplicates. |
| 9268 | * |
| 9269 | * If "map" has already been normalized or if the basic maps are |
| 9270 | * disjoint, then there can be no duplicates. |
| 9271 | */ |
| 9272 | __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map) |
| 9273 | { |
| 9274 | int i; |
| 9275 | isl_basic_map *bmap; |
| 9276 | |
| 9277 | if (!map) |
| 9278 | return NULL; |
| 9279 | if (map->n <= 1) |
| 9280 | return map; |
| 9281 | if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT)) |
| 9282 | return map; |
| 9283 | for (i = 0; i < map->n; ++i) { |
| 9284 | bmap = isl_basic_map_copy(map->p[i]); |
| 9285 | bmap = isl_basic_map_sort_constraints(bmap); |
| 9286 | if (!bmap) |
| 9287 | return isl_map_free(map); |
| 9288 | isl_basic_map_free(map->p[i]); |
| 9289 | map->p[i] = bmap; |
| 9290 | } |
| 9291 | |
| 9292 | map = sort_and_remove_duplicates(map); |
| 9293 | return map; |
| 9294 | } |
| 9295 | |
| 9296 | /* We normalize in place, but if anything goes wrong we need |
| 9297 | * to return NULL, so we need to make sure we don't change the |
| 9298 | * meaning of any possible other copies of map. |
| 9299 | */ |
| 9300 | __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map) |
| 9301 | { |
| 9302 | int i; |
| 9303 | struct isl_basic_map *bmap; |
| 9304 | |
| 9305 | if (!map) |
| 9306 | return NULL; |
| 9307 | if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED)) |
| 9308 | return map; |
| 9309 | for (i = 0; i < map->n; ++i) { |
| 9310 | bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i])); |
| 9311 | if (!bmap) |
| 9312 | goto error; |
| 9313 | isl_basic_map_free(map->p[i]); |
| 9314 | map->p[i] = bmap; |
| 9315 | } |
| 9316 | |
| 9317 | map = sort_and_remove_duplicates(map); |
| 9318 | if (map) |
| 9319 | ISL_F_SET(map, ISL_MAP_NORMALIZED); |
| 9320 | return map; |
| 9321 | error: |
| 9322 | isl_map_free(map); |
| 9323 | return NULL; |
| 9324 | } |
| 9325 | |
| 9326 | struct isl_set *isl_set_normalize(struct isl_set *set) |
| 9327 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 9328 | return set_from_map(isl_map_normalize(set_to_map(set))); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 9329 | } |
| 9330 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 9331 | isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1, |
| 9332 | __isl_keep isl_map *map2) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 9333 | { |
| 9334 | int i; |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 9335 | isl_bool equal; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 9336 | |
| 9337 | if (!map1 || !map2) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 9338 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 9339 | |
| 9340 | if (map1 == map2) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 9341 | return isl_bool_true; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 9342 | if (!isl_space_is_equal(map1->dim, map2->dim)) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 9343 | return isl_bool_false; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 9344 | |
| 9345 | map1 = isl_map_copy(map1); |
| 9346 | map2 = isl_map_copy(map2); |
| 9347 | map1 = isl_map_normalize(map1); |
| 9348 | map2 = isl_map_normalize(map2); |
| 9349 | if (!map1 || !map2) |
| 9350 | goto error; |
| 9351 | equal = map1->n == map2->n; |
| 9352 | for (i = 0; equal && i < map1->n; ++i) { |
| 9353 | equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]); |
| 9354 | if (equal < 0) |
| 9355 | goto error; |
| 9356 | } |
| 9357 | isl_map_free(map1); |
| 9358 | isl_map_free(map2); |
| 9359 | return equal; |
| 9360 | error: |
| 9361 | isl_map_free(map1); |
| 9362 | isl_map_free(map2); |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 9363 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 9364 | } |
| 9365 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 9366 | isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1, |
| 9367 | __isl_keep isl_set *set2) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 9368 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 9369 | return isl_map_plain_is_equal(set_to_map(set1), set_to_map(set2)); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 9370 | } |
| 9371 | |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 9372 | /* Return an interval that ranges from min to max (inclusive) |
| 9373 | */ |
| 9374 | struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx, |
| 9375 | isl_int min, isl_int max) |
| 9376 | { |
| 9377 | int k; |
| 9378 | struct isl_basic_set *bset = NULL; |
| 9379 | |
| 9380 | bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2); |
| 9381 | if (!bset) |
| 9382 | goto error; |
| 9383 | |
| 9384 | k = isl_basic_set_alloc_inequality(bset); |
| 9385 | if (k < 0) |
| 9386 | goto error; |
| 9387 | isl_int_set_si(bset->ineq[k][1], 1); |
| 9388 | isl_int_neg(bset->ineq[k][0], min); |
| 9389 | |
| 9390 | k = isl_basic_set_alloc_inequality(bset); |
| 9391 | if (k < 0) |
| 9392 | goto error; |
| 9393 | isl_int_set_si(bset->ineq[k][1], -1); |
| 9394 | isl_int_set(bset->ineq[k][0], max); |
| 9395 | |
| 9396 | return bset; |
| 9397 | error: |
| 9398 | isl_basic_set_free(bset); |
| 9399 | return NULL; |
| 9400 | } |
| 9401 | |
Tobias Grosser | 1fa7b97 | 2015-02-16 19:33:40 +0000 | [diff] [blame] | 9402 | /* Return the basic maps in "map" as a list. |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 9403 | */ |
Tobias Grosser | 1fa7b97 | 2015-02-16 19:33:40 +0000 | [diff] [blame] | 9404 | __isl_give isl_basic_map_list *isl_map_get_basic_map_list( |
| 9405 | __isl_keep isl_map *map) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 9406 | { |
| 9407 | int i; |
| 9408 | isl_ctx *ctx; |
Tobias Grosser | 1fa7b97 | 2015-02-16 19:33:40 +0000 | [diff] [blame] | 9409 | isl_basic_map_list *list; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 9410 | |
Tobias Grosser | 1fa7b97 | 2015-02-16 19:33:40 +0000 | [diff] [blame] | 9411 | if (!map) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 9412 | return NULL; |
Tobias Grosser | 1fa7b97 | 2015-02-16 19:33:40 +0000 | [diff] [blame] | 9413 | ctx = isl_map_get_ctx(map); |
| 9414 | list = isl_basic_map_list_alloc(ctx, map->n); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 9415 | |
Tobias Grosser | 1fa7b97 | 2015-02-16 19:33:40 +0000 | [diff] [blame] | 9416 | for (i = 0; i < map->n; ++i) { |
| 9417 | isl_basic_map *bmap; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 9418 | |
Tobias Grosser | 1fa7b97 | 2015-02-16 19:33:40 +0000 | [diff] [blame] | 9419 | bmap = isl_basic_map_copy(map->p[i]); |
| 9420 | list = isl_basic_map_list_add(list, bmap); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 9421 | } |
| 9422 | |
| 9423 | return list; |
| 9424 | } |
| 9425 | |
| 9426 | /* Return the intersection of the elements in the non-empty list "list". |
| 9427 | * All elements are assumed to live in the same space. |
| 9428 | */ |
Tobias Grosser | 1fa7b97 | 2015-02-16 19:33:40 +0000 | [diff] [blame] | 9429 | __isl_give isl_basic_map *isl_basic_map_list_intersect( |
| 9430 | __isl_take isl_basic_map_list *list) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 9431 | { |
| 9432 | int i, n; |
Tobias Grosser | 1fa7b97 | 2015-02-16 19:33:40 +0000 | [diff] [blame] | 9433 | isl_basic_map *bmap; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 9434 | |
| 9435 | if (!list) |
| 9436 | return NULL; |
Tobias Grosser | 1fa7b97 | 2015-02-16 19:33:40 +0000 | [diff] [blame] | 9437 | n = isl_basic_map_list_n_basic_map(list); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 9438 | if (n < 1) |
Tobias Grosser | 1fa7b97 | 2015-02-16 19:33:40 +0000 | [diff] [blame] | 9439 | isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid, |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 9440 | "expecting non-empty list", goto error); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 9441 | |
Tobias Grosser | 1fa7b97 | 2015-02-16 19:33:40 +0000 | [diff] [blame] | 9442 | bmap = isl_basic_map_list_get_basic_map(list, 0); |
| 9443 | for (i = 1; i < n; ++i) { |
| 9444 | isl_basic_map *bmap_i; |
| 9445 | |
| 9446 | bmap_i = isl_basic_map_list_get_basic_map(list, i); |
| 9447 | bmap = isl_basic_map_intersect(bmap, bmap_i); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 9448 | } |
| 9449 | |
Tobias Grosser | 1fa7b97 | 2015-02-16 19:33:40 +0000 | [diff] [blame] | 9450 | isl_basic_map_list_free(list); |
| 9451 | return bmap; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 9452 | error: |
Tobias Grosser | 1fa7b97 | 2015-02-16 19:33:40 +0000 | [diff] [blame] | 9453 | isl_basic_map_list_free(list); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 9454 | return NULL; |
| 9455 | } |
| 9456 | |
Tobias Grosser | 1fa7b97 | 2015-02-16 19:33:40 +0000 | [diff] [blame] | 9457 | /* Return the intersection of the elements in the non-empty list "list". |
| 9458 | * All elements are assumed to live in the same space. |
| 9459 | */ |
| 9460 | __isl_give isl_basic_set *isl_basic_set_list_intersect( |
| 9461 | __isl_take isl_basic_set_list *list) |
| 9462 | { |
| 9463 | return isl_basic_map_list_intersect(list); |
| 9464 | } |
| 9465 | |
Tobias Grosser | eab0943e | 2016-11-22 21:31:59 +0000 | [diff] [blame] | 9466 | /* Return the union of the elements of "list". |
| 9467 | * The list is required to have at least one element. |
| 9468 | */ |
| 9469 | __isl_give isl_set *isl_basic_set_list_union( |
| 9470 | __isl_take isl_basic_set_list *list) |
| 9471 | { |
| 9472 | int i, n; |
| 9473 | isl_space *space; |
| 9474 | isl_basic_set *bset; |
| 9475 | isl_set *set; |
| 9476 | |
| 9477 | if (!list) |
| 9478 | return NULL; |
| 9479 | n = isl_basic_set_list_n_basic_set(list); |
| 9480 | if (n < 1) |
| 9481 | isl_die(isl_basic_set_list_get_ctx(list), isl_error_invalid, |
| 9482 | "expecting non-empty list", goto error); |
| 9483 | |
| 9484 | bset = isl_basic_set_list_get_basic_set(list, 0); |
| 9485 | space = isl_basic_set_get_space(bset); |
| 9486 | isl_basic_set_free(bset); |
| 9487 | |
| 9488 | set = isl_set_alloc_space(space, n, 0); |
| 9489 | for (i = 0; i < n; ++i) { |
| 9490 | bset = isl_basic_set_list_get_basic_set(list, i); |
| 9491 | set = isl_set_add_basic_set(set, bset); |
| 9492 | } |
| 9493 | |
| 9494 | isl_basic_set_list_free(list); |
| 9495 | return set; |
| 9496 | error: |
| 9497 | isl_basic_set_list_free(list); |
| 9498 | return NULL; |
| 9499 | } |
| 9500 | |
Tobias Grosser | a67ac97 | 2016-02-26 11:35:12 +0000 | [diff] [blame] | 9501 | /* Return the union of the elements in the non-empty list "list". |
| 9502 | * All elements are assumed to live in the same space. |
| 9503 | */ |
| 9504 | __isl_give isl_set *isl_set_list_union(__isl_take isl_set_list *list) |
| 9505 | { |
| 9506 | int i, n; |
| 9507 | isl_set *set; |
| 9508 | |
| 9509 | if (!list) |
| 9510 | return NULL; |
| 9511 | n = isl_set_list_n_set(list); |
| 9512 | if (n < 1) |
| 9513 | isl_die(isl_set_list_get_ctx(list), isl_error_invalid, |
| 9514 | "expecting non-empty list", goto error); |
| 9515 | |
| 9516 | set = isl_set_list_get_set(list, 0); |
| 9517 | for (i = 1; i < n; ++i) { |
| 9518 | isl_set *set_i; |
| 9519 | |
| 9520 | set_i = isl_set_list_get_set(list, i); |
| 9521 | set = isl_set_union(set, set_i); |
| 9522 | } |
| 9523 | |
| 9524 | isl_set_list_free(list); |
| 9525 | return set; |
| 9526 | error: |
| 9527 | isl_set_list_free(list); |
| 9528 | return NULL; |
| 9529 | } |
| 9530 | |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 9531 | /* Return the Cartesian product of the basic sets in list (in the given order). |
| 9532 | */ |
| 9533 | __isl_give isl_basic_set *isl_basic_set_list_product( |
| 9534 | __isl_take struct isl_basic_set_list *list) |
| 9535 | { |
| 9536 | int i; |
| 9537 | unsigned dim; |
| 9538 | unsigned nparam; |
| 9539 | unsigned extra; |
| 9540 | unsigned n_eq; |
| 9541 | unsigned n_ineq; |
| 9542 | struct isl_basic_set *product = NULL; |
| 9543 | |
| 9544 | if (!list) |
| 9545 | goto error; |
| 9546 | isl_assert(list->ctx, list->n > 0, goto error); |
| 9547 | isl_assert(list->ctx, list->p[0], goto error); |
| 9548 | nparam = isl_basic_set_n_param(list->p[0]); |
| 9549 | dim = isl_basic_set_n_dim(list->p[0]); |
| 9550 | extra = list->p[0]->n_div; |
| 9551 | n_eq = list->p[0]->n_eq; |
| 9552 | n_ineq = list->p[0]->n_ineq; |
| 9553 | for (i = 1; i < list->n; ++i) { |
| 9554 | isl_assert(list->ctx, list->p[i], goto error); |
| 9555 | isl_assert(list->ctx, |
| 9556 | nparam == isl_basic_set_n_param(list->p[i]), goto error); |
| 9557 | dim += isl_basic_set_n_dim(list->p[i]); |
| 9558 | extra += list->p[i]->n_div; |
| 9559 | n_eq += list->p[i]->n_eq; |
| 9560 | n_ineq += list->p[i]->n_ineq; |
| 9561 | } |
| 9562 | product = isl_basic_set_alloc(list->ctx, nparam, dim, extra, |
| 9563 | n_eq, n_ineq); |
| 9564 | if (!product) |
| 9565 | goto error; |
| 9566 | dim = 0; |
| 9567 | for (i = 0; i < list->n; ++i) { |
| 9568 | isl_basic_set_add_constraints(product, |
| 9569 | isl_basic_set_copy(list->p[i]), dim); |
| 9570 | dim += isl_basic_set_n_dim(list->p[i]); |
| 9571 | } |
| 9572 | isl_basic_set_list_free(list); |
| 9573 | return product; |
| 9574 | error: |
| 9575 | isl_basic_set_free(product); |
| 9576 | isl_basic_set_list_free(list); |
| 9577 | return NULL; |
| 9578 | } |
| 9579 | |
| 9580 | struct isl_basic_map *isl_basic_map_product( |
| 9581 | struct isl_basic_map *bmap1, struct isl_basic_map *bmap2) |
| 9582 | { |
| 9583 | isl_space *dim_result = NULL; |
| 9584 | struct isl_basic_map *bmap; |
| 9585 | unsigned in1, in2, out1, out2, nparam, total, pos; |
| 9586 | struct isl_dim_map *dim_map1, *dim_map2; |
| 9587 | |
| 9588 | if (!bmap1 || !bmap2) |
| 9589 | goto error; |
| 9590 | |
| 9591 | isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param, |
| 9592 | bmap2->dim, isl_dim_param), goto error); |
| 9593 | dim_result = isl_space_product(isl_space_copy(bmap1->dim), |
| 9594 | isl_space_copy(bmap2->dim)); |
| 9595 | |
| 9596 | in1 = isl_basic_map_n_in(bmap1); |
| 9597 | in2 = isl_basic_map_n_in(bmap2); |
| 9598 | out1 = isl_basic_map_n_out(bmap1); |
| 9599 | out2 = isl_basic_map_n_out(bmap2); |
| 9600 | nparam = isl_basic_map_n_param(bmap1); |
| 9601 | |
| 9602 | total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div; |
| 9603 | dim_map1 = isl_dim_map_alloc(bmap1->ctx, total); |
| 9604 | dim_map2 = isl_dim_map_alloc(bmap1->ctx, total); |
| 9605 | isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0); |
| 9606 | isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0); |
| 9607 | isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam); |
| 9608 | isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1); |
| 9609 | isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2); |
| 9610 | isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1); |
| 9611 | isl_dim_map_div(dim_map1, bmap1, pos += out2); |
| 9612 | isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div); |
| 9613 | |
| 9614 | bmap = isl_basic_map_alloc_space(dim_result, |
| 9615 | bmap1->n_div + bmap2->n_div, |
| 9616 | bmap1->n_eq + bmap2->n_eq, |
| 9617 | bmap1->n_ineq + bmap2->n_ineq); |
| 9618 | bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1); |
| 9619 | bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2); |
| 9620 | bmap = isl_basic_map_simplify(bmap); |
| 9621 | return isl_basic_map_finalize(bmap); |
| 9622 | error: |
| 9623 | isl_basic_map_free(bmap1); |
| 9624 | isl_basic_map_free(bmap2); |
| 9625 | return NULL; |
| 9626 | } |
| 9627 | |
| 9628 | __isl_give isl_basic_map *isl_basic_map_flat_product( |
| 9629 | __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2) |
| 9630 | { |
| 9631 | isl_basic_map *prod; |
| 9632 | |
| 9633 | prod = isl_basic_map_product(bmap1, bmap2); |
| 9634 | prod = isl_basic_map_flatten(prod); |
| 9635 | return prod; |
| 9636 | } |
| 9637 | |
| 9638 | __isl_give isl_basic_set *isl_basic_set_flat_product( |
| 9639 | __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2) |
| 9640 | { |
| 9641 | return isl_basic_map_flat_range_product(bset1, bset2); |
| 9642 | } |
| 9643 | |
| 9644 | __isl_give isl_basic_map *isl_basic_map_domain_product( |
| 9645 | __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2) |
| 9646 | { |
| 9647 | isl_space *space_result = NULL; |
| 9648 | isl_basic_map *bmap; |
| 9649 | unsigned in1, in2, out, nparam, total, pos; |
| 9650 | struct isl_dim_map *dim_map1, *dim_map2; |
| 9651 | |
| 9652 | if (!bmap1 || !bmap2) |
| 9653 | goto error; |
| 9654 | |
| 9655 | space_result = isl_space_domain_product(isl_space_copy(bmap1->dim), |
| 9656 | isl_space_copy(bmap2->dim)); |
| 9657 | |
| 9658 | in1 = isl_basic_map_dim(bmap1, isl_dim_in); |
| 9659 | in2 = isl_basic_map_dim(bmap2, isl_dim_in); |
| 9660 | out = isl_basic_map_dim(bmap1, isl_dim_out); |
| 9661 | nparam = isl_basic_map_dim(bmap1, isl_dim_param); |
| 9662 | |
| 9663 | total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div; |
| 9664 | dim_map1 = isl_dim_map_alloc(bmap1->ctx, total); |
| 9665 | dim_map2 = isl_dim_map_alloc(bmap1->ctx, total); |
| 9666 | isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0); |
| 9667 | isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0); |
| 9668 | isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam); |
| 9669 | isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1); |
| 9670 | isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2); |
| 9671 | isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos); |
| 9672 | isl_dim_map_div(dim_map1, bmap1, pos += out); |
| 9673 | isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div); |
| 9674 | |
| 9675 | bmap = isl_basic_map_alloc_space(space_result, |
| 9676 | bmap1->n_div + bmap2->n_div, |
| 9677 | bmap1->n_eq + bmap2->n_eq, |
| 9678 | bmap1->n_ineq + bmap2->n_ineq); |
| 9679 | bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1); |
| 9680 | bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2); |
| 9681 | bmap = isl_basic_map_simplify(bmap); |
| 9682 | return isl_basic_map_finalize(bmap); |
| 9683 | error: |
| 9684 | isl_basic_map_free(bmap1); |
| 9685 | isl_basic_map_free(bmap2); |
| 9686 | return NULL; |
| 9687 | } |
| 9688 | |
| 9689 | __isl_give isl_basic_map *isl_basic_map_range_product( |
| 9690 | __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2) |
| 9691 | { |
Tobias Grosser | 5652c9c | 2016-10-01 19:46:51 +0000 | [diff] [blame] | 9692 | int rational; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 9693 | isl_space *dim_result = NULL; |
| 9694 | isl_basic_map *bmap; |
| 9695 | unsigned in, out1, out2, nparam, total, pos; |
| 9696 | struct isl_dim_map *dim_map1, *dim_map2; |
| 9697 | |
Tobias Grosser | 5652c9c | 2016-10-01 19:46:51 +0000 | [diff] [blame] | 9698 | rational = isl_basic_map_is_rational(bmap1); |
| 9699 | if (rational >= 0 && rational) |
| 9700 | rational = isl_basic_map_is_rational(bmap2); |
| 9701 | if (!bmap1 || !bmap2 || rational < 0) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 9702 | goto error; |
| 9703 | |
| 9704 | if (!isl_space_match(bmap1->dim, isl_dim_param, |
| 9705 | bmap2->dim, isl_dim_param)) |
| 9706 | isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid, |
| 9707 | "parameters don't match", goto error); |
| 9708 | |
| 9709 | dim_result = isl_space_range_product(isl_space_copy(bmap1->dim), |
| 9710 | isl_space_copy(bmap2->dim)); |
| 9711 | |
| 9712 | in = isl_basic_map_dim(bmap1, isl_dim_in); |
| 9713 | out1 = isl_basic_map_n_out(bmap1); |
| 9714 | out2 = isl_basic_map_n_out(bmap2); |
| 9715 | nparam = isl_basic_map_n_param(bmap1); |
| 9716 | |
| 9717 | total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div; |
| 9718 | dim_map1 = isl_dim_map_alloc(bmap1->ctx, total); |
| 9719 | dim_map2 = isl_dim_map_alloc(bmap1->ctx, total); |
| 9720 | isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0); |
| 9721 | isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0); |
| 9722 | isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam); |
| 9723 | isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos); |
| 9724 | isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in); |
| 9725 | isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1); |
| 9726 | isl_dim_map_div(dim_map1, bmap1, pos += out2); |
| 9727 | isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div); |
| 9728 | |
| 9729 | bmap = isl_basic_map_alloc_space(dim_result, |
| 9730 | bmap1->n_div + bmap2->n_div, |
| 9731 | bmap1->n_eq + bmap2->n_eq, |
| 9732 | bmap1->n_ineq + bmap2->n_ineq); |
| 9733 | bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1); |
| 9734 | bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2); |
Tobias Grosser | 5652c9c | 2016-10-01 19:46:51 +0000 | [diff] [blame] | 9735 | if (rational) |
| 9736 | bmap = isl_basic_map_set_rational(bmap); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 9737 | bmap = isl_basic_map_simplify(bmap); |
| 9738 | return isl_basic_map_finalize(bmap); |
| 9739 | error: |
| 9740 | isl_basic_map_free(bmap1); |
| 9741 | isl_basic_map_free(bmap2); |
| 9742 | return NULL; |
| 9743 | } |
| 9744 | |
| 9745 | __isl_give isl_basic_map *isl_basic_map_flat_range_product( |
| 9746 | __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2) |
| 9747 | { |
| 9748 | isl_basic_map *prod; |
| 9749 | |
| 9750 | prod = isl_basic_map_range_product(bmap1, bmap2); |
| 9751 | prod = isl_basic_map_flatten_range(prod); |
| 9752 | return prod; |
| 9753 | } |
| 9754 | |
| 9755 | /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2" |
| 9756 | * and collect the results. |
| 9757 | * The result live in the space obtained by calling "space_product" |
| 9758 | * on the spaces of "map1" and "map2". |
| 9759 | * If "remove_duplicates" is set then the result may contain duplicates |
| 9760 | * (even if the inputs do not) and so we try and remove the obvious |
| 9761 | * duplicates. |
| 9762 | */ |
| 9763 | static __isl_give isl_map *map_product(__isl_take isl_map *map1, |
| 9764 | __isl_take isl_map *map2, |
| 9765 | __isl_give isl_space *(*space_product)(__isl_take isl_space *left, |
| 9766 | __isl_take isl_space *right), |
| 9767 | __isl_give isl_basic_map *(*basic_map_product)( |
| 9768 | __isl_take isl_basic_map *left, |
| 9769 | __isl_take isl_basic_map *right), |
| 9770 | int remove_duplicates) |
| 9771 | { |
| 9772 | unsigned flags = 0; |
| 9773 | struct isl_map *result; |
| 9774 | int i, j; |
| 9775 | |
| 9776 | if (!map1 || !map2) |
| 9777 | goto error; |
| 9778 | |
| 9779 | isl_assert(map1->ctx, isl_space_match(map1->dim, isl_dim_param, |
| 9780 | map2->dim, isl_dim_param), goto error); |
| 9781 | |
| 9782 | if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) && |
| 9783 | ISL_F_ISSET(map2, ISL_MAP_DISJOINT)) |
| 9784 | ISL_FL_SET(flags, ISL_MAP_DISJOINT); |
| 9785 | |
| 9786 | result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim), |
| 9787 | isl_space_copy(map2->dim)), |
| 9788 | map1->n * map2->n, flags); |
| 9789 | if (!result) |
| 9790 | goto error; |
| 9791 | for (i = 0; i < map1->n; ++i) |
| 9792 | for (j = 0; j < map2->n; ++j) { |
| 9793 | struct isl_basic_map *part; |
| 9794 | part = basic_map_product(isl_basic_map_copy(map1->p[i]), |
| 9795 | isl_basic_map_copy(map2->p[j])); |
| 9796 | if (isl_basic_map_is_empty(part)) |
| 9797 | isl_basic_map_free(part); |
| 9798 | else |
| 9799 | result = isl_map_add_basic_map(result, part); |
| 9800 | if (!result) |
| 9801 | goto error; |
| 9802 | } |
| 9803 | if (remove_duplicates) |
| 9804 | result = isl_map_remove_obvious_duplicates(result); |
| 9805 | isl_map_free(map1); |
| 9806 | isl_map_free(map2); |
| 9807 | return result; |
| 9808 | error: |
| 9809 | isl_map_free(map1); |
| 9810 | isl_map_free(map2); |
| 9811 | return NULL; |
| 9812 | } |
| 9813 | |
| 9814 | /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D] |
| 9815 | */ |
| 9816 | static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1, |
| 9817 | __isl_take isl_map *map2) |
| 9818 | { |
| 9819 | return map_product(map1, map2, &isl_space_product, |
| 9820 | &isl_basic_map_product, 0); |
| 9821 | } |
| 9822 | |
| 9823 | __isl_give isl_map *isl_map_product(__isl_take isl_map *map1, |
| 9824 | __isl_take isl_map *map2) |
| 9825 | { |
| 9826 | return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned); |
| 9827 | } |
| 9828 | |
| 9829 | /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D) |
| 9830 | */ |
| 9831 | __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1, |
| 9832 | __isl_take isl_map *map2) |
| 9833 | { |
| 9834 | isl_map *prod; |
| 9835 | |
| 9836 | prod = isl_map_product(map1, map2); |
| 9837 | prod = isl_map_flatten(prod); |
| 9838 | return prod; |
| 9839 | } |
| 9840 | |
| 9841 | /* Given two set A and B, construct its Cartesian product A x B. |
| 9842 | */ |
| 9843 | struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2) |
| 9844 | { |
| 9845 | return isl_map_range_product(set1, set2); |
| 9846 | } |
| 9847 | |
| 9848 | __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1, |
| 9849 | __isl_take isl_set *set2) |
| 9850 | { |
| 9851 | return isl_map_flat_range_product(set1, set2); |
| 9852 | } |
| 9853 | |
| 9854 | /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D) |
| 9855 | */ |
| 9856 | static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1, |
| 9857 | __isl_take isl_map *map2) |
| 9858 | { |
| 9859 | return map_product(map1, map2, &isl_space_domain_product, |
| 9860 | &isl_basic_map_domain_product, 1); |
| 9861 | } |
| 9862 | |
| 9863 | /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D] |
| 9864 | */ |
| 9865 | static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1, |
| 9866 | __isl_take isl_map *map2) |
| 9867 | { |
| 9868 | return map_product(map1, map2, &isl_space_range_product, |
| 9869 | &isl_basic_map_range_product, 1); |
| 9870 | } |
| 9871 | |
| 9872 | __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1, |
| 9873 | __isl_take isl_map *map2) |
| 9874 | { |
| 9875 | return isl_map_align_params_map_map_and(map1, map2, |
| 9876 | &map_domain_product_aligned); |
| 9877 | } |
| 9878 | |
| 9879 | __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1, |
| 9880 | __isl_take isl_map *map2) |
| 9881 | { |
| 9882 | return isl_map_align_params_map_map_and(map1, map2, |
| 9883 | &map_range_product_aligned); |
| 9884 | } |
| 9885 | |
| 9886 | /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C. |
| 9887 | */ |
| 9888 | __isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map) |
| 9889 | { |
| 9890 | isl_space *space; |
| 9891 | int total1, keep1, total2, keep2; |
| 9892 | |
| 9893 | if (!map) |
| 9894 | return NULL; |
| 9895 | if (!isl_space_domain_is_wrapping(map->dim) || |
| 9896 | !isl_space_range_is_wrapping(map->dim)) |
| 9897 | isl_die(isl_map_get_ctx(map), isl_error_invalid, |
| 9898 | "not a product", return isl_map_free(map)); |
| 9899 | |
| 9900 | space = isl_map_get_space(map); |
| 9901 | total1 = isl_space_dim(space, isl_dim_in); |
| 9902 | total2 = isl_space_dim(space, isl_dim_out); |
| 9903 | space = isl_space_factor_domain(space); |
| 9904 | keep1 = isl_space_dim(space, isl_dim_in); |
| 9905 | keep2 = isl_space_dim(space, isl_dim_out); |
| 9906 | map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1); |
| 9907 | map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2); |
| 9908 | map = isl_map_reset_space(map, space); |
| 9909 | |
| 9910 | return map; |
| 9911 | } |
| 9912 | |
| 9913 | /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D. |
| 9914 | */ |
| 9915 | __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map) |
| 9916 | { |
| 9917 | isl_space *space; |
| 9918 | int total1, keep1, total2, keep2; |
| 9919 | |
| 9920 | if (!map) |
| 9921 | return NULL; |
| 9922 | if (!isl_space_domain_is_wrapping(map->dim) || |
| 9923 | !isl_space_range_is_wrapping(map->dim)) |
| 9924 | isl_die(isl_map_get_ctx(map), isl_error_invalid, |
| 9925 | "not a product", return isl_map_free(map)); |
| 9926 | |
| 9927 | space = isl_map_get_space(map); |
| 9928 | total1 = isl_space_dim(space, isl_dim_in); |
| 9929 | total2 = isl_space_dim(space, isl_dim_out); |
| 9930 | space = isl_space_factor_range(space); |
| 9931 | keep1 = isl_space_dim(space, isl_dim_in); |
| 9932 | keep2 = isl_space_dim(space, isl_dim_out); |
| 9933 | map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1); |
| 9934 | map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2); |
| 9935 | map = isl_map_reset_space(map, space); |
| 9936 | |
| 9937 | return map; |
| 9938 | } |
| 9939 | |
| 9940 | /* Given a map of the form [A -> B] -> C, return the map A -> C. |
| 9941 | */ |
| 9942 | __isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map) |
| 9943 | { |
| 9944 | isl_space *space; |
| 9945 | int total, keep; |
| 9946 | |
| 9947 | if (!map) |
| 9948 | return NULL; |
| 9949 | if (!isl_space_domain_is_wrapping(map->dim)) |
| 9950 | isl_die(isl_map_get_ctx(map), isl_error_invalid, |
| 9951 | "domain is not a product", return isl_map_free(map)); |
| 9952 | |
| 9953 | space = isl_map_get_space(map); |
| 9954 | total = isl_space_dim(space, isl_dim_in); |
| 9955 | space = isl_space_domain_factor_domain(space); |
| 9956 | keep = isl_space_dim(space, isl_dim_in); |
| 9957 | map = isl_map_project_out(map, isl_dim_in, keep, total - keep); |
| 9958 | map = isl_map_reset_space(map, space); |
| 9959 | |
| 9960 | return map; |
| 9961 | } |
| 9962 | |
| 9963 | /* Given a map of the form [A -> B] -> C, return the map B -> C. |
| 9964 | */ |
| 9965 | __isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map) |
| 9966 | { |
| 9967 | isl_space *space; |
| 9968 | int total, keep; |
| 9969 | |
| 9970 | if (!map) |
| 9971 | return NULL; |
| 9972 | if (!isl_space_domain_is_wrapping(map->dim)) |
| 9973 | isl_die(isl_map_get_ctx(map), isl_error_invalid, |
| 9974 | "domain is not a product", return isl_map_free(map)); |
| 9975 | |
| 9976 | space = isl_map_get_space(map); |
| 9977 | total = isl_space_dim(space, isl_dim_in); |
| 9978 | space = isl_space_domain_factor_range(space); |
| 9979 | keep = isl_space_dim(space, isl_dim_in); |
| 9980 | map = isl_map_project_out(map, isl_dim_in, 0, total - keep); |
| 9981 | map = isl_map_reset_space(map, space); |
| 9982 | |
| 9983 | return map; |
| 9984 | } |
| 9985 | |
| 9986 | /* Given a map A -> [B -> C], extract the map A -> B. |
| 9987 | */ |
| 9988 | __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map) |
| 9989 | { |
| 9990 | isl_space *space; |
| 9991 | int total, keep; |
| 9992 | |
| 9993 | if (!map) |
| 9994 | return NULL; |
| 9995 | if (!isl_space_range_is_wrapping(map->dim)) |
| 9996 | isl_die(isl_map_get_ctx(map), isl_error_invalid, |
| 9997 | "range is not a product", return isl_map_free(map)); |
| 9998 | |
| 9999 | space = isl_map_get_space(map); |
| 10000 | total = isl_space_dim(space, isl_dim_out); |
| 10001 | space = isl_space_range_factor_domain(space); |
| 10002 | keep = isl_space_dim(space, isl_dim_out); |
| 10003 | map = isl_map_project_out(map, isl_dim_out, keep, total - keep); |
| 10004 | map = isl_map_reset_space(map, space); |
| 10005 | |
| 10006 | return map; |
| 10007 | } |
| 10008 | |
| 10009 | /* Given a map A -> [B -> C], extract the map A -> C. |
| 10010 | */ |
| 10011 | __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map) |
| 10012 | { |
| 10013 | isl_space *space; |
| 10014 | int total, keep; |
| 10015 | |
| 10016 | if (!map) |
| 10017 | return NULL; |
| 10018 | if (!isl_space_range_is_wrapping(map->dim)) |
| 10019 | isl_die(isl_map_get_ctx(map), isl_error_invalid, |
| 10020 | "range is not a product", return isl_map_free(map)); |
| 10021 | |
| 10022 | space = isl_map_get_space(map); |
| 10023 | total = isl_space_dim(space, isl_dim_out); |
| 10024 | space = isl_space_range_factor_range(space); |
| 10025 | keep = isl_space_dim(space, isl_dim_out); |
| 10026 | map = isl_map_project_out(map, isl_dim_out, 0, total - keep); |
| 10027 | map = isl_map_reset_space(map, space); |
| 10028 | |
| 10029 | return map; |
| 10030 | } |
| 10031 | |
| 10032 | /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D) |
| 10033 | */ |
| 10034 | __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1, |
| 10035 | __isl_take isl_map *map2) |
| 10036 | { |
| 10037 | isl_map *prod; |
| 10038 | |
| 10039 | prod = isl_map_domain_product(map1, map2); |
| 10040 | prod = isl_map_flatten_domain(prod); |
| 10041 | return prod; |
| 10042 | } |
| 10043 | |
| 10044 | /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D) |
| 10045 | */ |
| 10046 | __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1, |
| 10047 | __isl_take isl_map *map2) |
| 10048 | { |
| 10049 | isl_map *prod; |
| 10050 | |
| 10051 | prod = isl_map_range_product(map1, map2); |
| 10052 | prod = isl_map_flatten_range(prod); |
| 10053 | return prod; |
| 10054 | } |
| 10055 | |
| 10056 | uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap) |
| 10057 | { |
| 10058 | int i; |
| 10059 | uint32_t hash = isl_hash_init(); |
| 10060 | unsigned total; |
| 10061 | |
| 10062 | if (!bmap) |
| 10063 | return 0; |
| 10064 | bmap = isl_basic_map_copy(bmap); |
| 10065 | bmap = isl_basic_map_normalize(bmap); |
| 10066 | if (!bmap) |
| 10067 | return 0; |
| 10068 | total = isl_basic_map_total_dim(bmap); |
| 10069 | isl_hash_byte(hash, bmap->n_eq & 0xFF); |
| 10070 | for (i = 0; i < bmap->n_eq; ++i) { |
| 10071 | uint32_t c_hash; |
| 10072 | c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total); |
| 10073 | isl_hash_hash(hash, c_hash); |
| 10074 | } |
| 10075 | isl_hash_byte(hash, bmap->n_ineq & 0xFF); |
| 10076 | for (i = 0; i < bmap->n_ineq; ++i) { |
| 10077 | uint32_t c_hash; |
| 10078 | c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total); |
| 10079 | isl_hash_hash(hash, c_hash); |
| 10080 | } |
| 10081 | isl_hash_byte(hash, bmap->n_div & 0xFF); |
| 10082 | for (i = 0; i < bmap->n_div; ++i) { |
| 10083 | uint32_t c_hash; |
| 10084 | if (isl_int_is_zero(bmap->div[i][0])) |
| 10085 | continue; |
| 10086 | isl_hash_byte(hash, i & 0xFF); |
| 10087 | c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total); |
| 10088 | isl_hash_hash(hash, c_hash); |
| 10089 | } |
| 10090 | isl_basic_map_free(bmap); |
| 10091 | return hash; |
| 10092 | } |
| 10093 | |
| 10094 | uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset) |
| 10095 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 10096 | return isl_basic_map_get_hash(bset_to_bmap(bset)); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10097 | } |
| 10098 | |
| 10099 | uint32_t isl_map_get_hash(__isl_keep isl_map *map) |
| 10100 | { |
| 10101 | int i; |
| 10102 | uint32_t hash; |
| 10103 | |
| 10104 | if (!map) |
| 10105 | return 0; |
| 10106 | map = isl_map_copy(map); |
| 10107 | map = isl_map_normalize(map); |
| 10108 | if (!map) |
| 10109 | return 0; |
| 10110 | |
| 10111 | hash = isl_hash_init(); |
| 10112 | for (i = 0; i < map->n; ++i) { |
| 10113 | uint32_t bmap_hash; |
| 10114 | bmap_hash = isl_basic_map_get_hash(map->p[i]); |
| 10115 | isl_hash_hash(hash, bmap_hash); |
| 10116 | } |
| 10117 | |
| 10118 | isl_map_free(map); |
| 10119 | |
| 10120 | return hash; |
| 10121 | } |
| 10122 | |
| 10123 | uint32_t isl_set_get_hash(__isl_keep isl_set *set) |
| 10124 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 10125 | return isl_map_get_hash(set_to_map(set)); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10126 | } |
| 10127 | |
| 10128 | /* Check if the value for dimension dim is completely determined |
| 10129 | * by the values of the other parameters and variables. |
| 10130 | * That is, check if dimension dim is involved in an equality. |
| 10131 | */ |
| 10132 | int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim) |
| 10133 | { |
| 10134 | int i; |
| 10135 | unsigned nparam; |
| 10136 | |
| 10137 | if (!bset) |
| 10138 | return -1; |
| 10139 | nparam = isl_basic_set_n_param(bset); |
| 10140 | for (i = 0; i < bset->n_eq; ++i) |
| 10141 | if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim])) |
| 10142 | return 1; |
| 10143 | return 0; |
| 10144 | } |
| 10145 | |
| 10146 | /* Check if the value for dimension dim is completely determined |
| 10147 | * by the values of the other parameters and variables. |
| 10148 | * That is, check if dimension dim is involved in an equality |
| 10149 | * for each of the subsets. |
| 10150 | */ |
| 10151 | int isl_set_dim_is_unique(struct isl_set *set, unsigned dim) |
| 10152 | { |
| 10153 | int i; |
| 10154 | |
| 10155 | if (!set) |
| 10156 | return -1; |
| 10157 | for (i = 0; i < set->n; ++i) { |
| 10158 | int unique; |
| 10159 | unique = isl_basic_set_dim_is_unique(set->p[i], dim); |
| 10160 | if (unique != 1) |
| 10161 | return unique; |
| 10162 | } |
| 10163 | return 1; |
| 10164 | } |
| 10165 | |
| 10166 | /* Return the number of basic maps in the (current) representation of "map". |
| 10167 | */ |
| 10168 | int isl_map_n_basic_map(__isl_keep isl_map *map) |
| 10169 | { |
| 10170 | return map ? map->n : 0; |
| 10171 | } |
| 10172 | |
| 10173 | int isl_set_n_basic_set(__isl_keep isl_set *set) |
| 10174 | { |
| 10175 | return set ? set->n : 0; |
| 10176 | } |
| 10177 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10178 | isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map, |
| 10179 | isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10180 | { |
| 10181 | int i; |
| 10182 | |
| 10183 | if (!map) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10184 | return isl_stat_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10185 | |
| 10186 | for (i = 0; i < map->n; ++i) |
| 10187 | if (fn(isl_basic_map_copy(map->p[i]), user) < 0) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10188 | return isl_stat_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10189 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10190 | return isl_stat_ok; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10191 | } |
| 10192 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10193 | isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set, |
| 10194 | isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10195 | { |
| 10196 | int i; |
| 10197 | |
| 10198 | if (!set) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10199 | return isl_stat_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10200 | |
| 10201 | for (i = 0; i < set->n; ++i) |
| 10202 | if (fn(isl_basic_set_copy(set->p[i]), user) < 0) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10203 | return isl_stat_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10204 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10205 | return isl_stat_ok; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10206 | } |
| 10207 | |
Tobias Grosser | 4db5531 | 2015-06-30 08:22:14 +0000 | [diff] [blame] | 10208 | /* Return a list of basic sets, the union of which is equal to "set". |
| 10209 | */ |
| 10210 | __isl_give isl_basic_set_list *isl_set_get_basic_set_list( |
| 10211 | __isl_keep isl_set *set) |
| 10212 | { |
| 10213 | int i; |
| 10214 | isl_basic_set_list *list; |
| 10215 | |
| 10216 | if (!set) |
| 10217 | return NULL; |
| 10218 | |
| 10219 | list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n); |
| 10220 | for (i = 0; i < set->n; ++i) { |
| 10221 | isl_basic_set *bset; |
| 10222 | |
| 10223 | bset = isl_basic_set_copy(set->p[i]); |
| 10224 | list = isl_basic_set_list_add(list, bset); |
| 10225 | } |
| 10226 | |
| 10227 | return list; |
| 10228 | } |
| 10229 | |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10230 | __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset) |
| 10231 | { |
| 10232 | isl_space *dim; |
| 10233 | |
| 10234 | if (!bset) |
| 10235 | return NULL; |
| 10236 | |
| 10237 | bset = isl_basic_set_cow(bset); |
| 10238 | if (!bset) |
| 10239 | return NULL; |
| 10240 | |
| 10241 | dim = isl_basic_set_get_space(bset); |
| 10242 | dim = isl_space_lift(dim, bset->n_div); |
| 10243 | if (!dim) |
| 10244 | goto error; |
| 10245 | isl_space_free(bset->dim); |
| 10246 | bset->dim = dim; |
| 10247 | bset->extra -= bset->n_div; |
| 10248 | bset->n_div = 0; |
| 10249 | |
| 10250 | bset = isl_basic_set_finalize(bset); |
| 10251 | |
| 10252 | return bset; |
| 10253 | error: |
| 10254 | isl_basic_set_free(bset); |
| 10255 | return NULL; |
| 10256 | } |
| 10257 | |
| 10258 | __isl_give isl_set *isl_set_lift(__isl_take isl_set *set) |
| 10259 | { |
| 10260 | int i; |
| 10261 | isl_space *dim; |
| 10262 | unsigned n_div; |
| 10263 | |
| 10264 | set = isl_set_align_divs(set); |
| 10265 | |
| 10266 | if (!set) |
| 10267 | return NULL; |
| 10268 | |
| 10269 | set = isl_set_cow(set); |
| 10270 | if (!set) |
| 10271 | return NULL; |
| 10272 | |
| 10273 | n_div = set->p[0]->n_div; |
| 10274 | dim = isl_set_get_space(set); |
| 10275 | dim = isl_space_lift(dim, n_div); |
| 10276 | if (!dim) |
| 10277 | goto error; |
| 10278 | isl_space_free(set->dim); |
| 10279 | set->dim = dim; |
| 10280 | |
| 10281 | for (i = 0; i < set->n; ++i) { |
| 10282 | set->p[i] = isl_basic_set_lift(set->p[i]); |
| 10283 | if (!set->p[i]) |
| 10284 | goto error; |
| 10285 | } |
| 10286 | |
| 10287 | return set; |
| 10288 | error: |
| 10289 | isl_set_free(set); |
| 10290 | return NULL; |
| 10291 | } |
| 10292 | |
| 10293 | __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set) |
| 10294 | { |
| 10295 | isl_space *dim; |
| 10296 | struct isl_basic_map *bmap; |
| 10297 | unsigned n_set; |
| 10298 | unsigned n_div; |
| 10299 | unsigned n_param; |
| 10300 | unsigned total; |
| 10301 | int i, k, l; |
| 10302 | |
| 10303 | set = isl_set_align_divs(set); |
| 10304 | |
| 10305 | if (!set) |
| 10306 | return NULL; |
| 10307 | |
| 10308 | dim = isl_set_get_space(set); |
| 10309 | if (set->n == 0 || set->p[0]->n_div == 0) { |
| 10310 | isl_set_free(set); |
| 10311 | return isl_map_identity(isl_space_map_from_set(dim)); |
| 10312 | } |
| 10313 | |
| 10314 | n_div = set->p[0]->n_div; |
| 10315 | dim = isl_space_map_from_set(dim); |
| 10316 | n_param = isl_space_dim(dim, isl_dim_param); |
| 10317 | n_set = isl_space_dim(dim, isl_dim_in); |
| 10318 | dim = isl_space_extend(dim, n_param, n_set, n_set + n_div); |
| 10319 | bmap = isl_basic_map_alloc_space(dim, 0, n_set, 2 * n_div); |
| 10320 | for (i = 0; i < n_set; ++i) |
| 10321 | bmap = var_equal(bmap, i); |
| 10322 | |
| 10323 | total = n_param + n_set + n_set + n_div; |
| 10324 | for (i = 0; i < n_div; ++i) { |
| 10325 | k = isl_basic_map_alloc_inequality(bmap); |
| 10326 | if (k < 0) |
| 10327 | goto error; |
| 10328 | isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param); |
| 10329 | isl_seq_clr(bmap->ineq[k]+1+n_param, n_set); |
| 10330 | isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set, |
| 10331 | set->p[0]->div[i]+1+1+n_param, n_set + n_div); |
| 10332 | isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i], |
| 10333 | set->p[0]->div[i][0]); |
| 10334 | |
| 10335 | l = isl_basic_map_alloc_inequality(bmap); |
| 10336 | if (l < 0) |
| 10337 | goto error; |
| 10338 | isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total); |
| 10339 | isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0], |
| 10340 | set->p[0]->div[i][0]); |
| 10341 | isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1); |
| 10342 | } |
| 10343 | |
| 10344 | isl_set_free(set); |
| 10345 | bmap = isl_basic_map_simplify(bmap); |
| 10346 | bmap = isl_basic_map_finalize(bmap); |
| 10347 | return isl_map_from_basic_map(bmap); |
| 10348 | error: |
| 10349 | isl_set_free(set); |
| 10350 | isl_basic_map_free(bmap); |
| 10351 | return NULL; |
| 10352 | } |
| 10353 | |
| 10354 | int isl_basic_set_size(__isl_keep isl_basic_set *bset) |
| 10355 | { |
| 10356 | unsigned dim; |
| 10357 | int size = 0; |
| 10358 | |
| 10359 | if (!bset) |
| 10360 | return -1; |
| 10361 | |
| 10362 | dim = isl_basic_set_total_dim(bset); |
| 10363 | size += bset->n_eq * (1 + dim); |
| 10364 | size += bset->n_ineq * (1 + dim); |
| 10365 | size += bset->n_div * (2 + dim); |
| 10366 | |
| 10367 | return size; |
| 10368 | } |
| 10369 | |
| 10370 | int isl_set_size(__isl_keep isl_set *set) |
| 10371 | { |
| 10372 | int i; |
| 10373 | int size = 0; |
| 10374 | |
| 10375 | if (!set) |
| 10376 | return -1; |
| 10377 | |
| 10378 | for (i = 0; i < set->n; ++i) |
| 10379 | size += isl_basic_set_size(set->p[i]); |
| 10380 | |
| 10381 | return size; |
| 10382 | } |
| 10383 | |
| 10384 | /* Check if there is any lower bound (if lower == 0) and/or upper |
| 10385 | * bound (if upper == 0) on the specified dim. |
| 10386 | */ |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10387 | static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap, |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10388 | enum isl_dim_type type, unsigned pos, int lower, int upper) |
| 10389 | { |
| 10390 | int i; |
| 10391 | |
| 10392 | if (!bmap) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10393 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10394 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10395 | isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), |
| 10396 | return isl_bool_error); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10397 | |
| 10398 | pos += isl_basic_map_offset(bmap, type); |
| 10399 | |
| 10400 | for (i = 0; i < bmap->n_div; ++i) { |
| 10401 | if (isl_int_is_zero(bmap->div[i][0])) |
| 10402 | continue; |
| 10403 | if (!isl_int_is_zero(bmap->div[i][1 + pos])) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10404 | return isl_bool_true; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10405 | } |
| 10406 | |
| 10407 | for (i = 0; i < bmap->n_eq; ++i) |
| 10408 | if (!isl_int_is_zero(bmap->eq[i][pos])) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10409 | return isl_bool_true; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10410 | |
| 10411 | for (i = 0; i < bmap->n_ineq; ++i) { |
| 10412 | int sgn = isl_int_sgn(bmap->ineq[i][pos]); |
| 10413 | if (sgn > 0) |
| 10414 | lower = 1; |
| 10415 | if (sgn < 0) |
| 10416 | upper = 1; |
| 10417 | } |
| 10418 | |
| 10419 | return lower && upper; |
| 10420 | } |
| 10421 | |
| 10422 | int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap, |
| 10423 | enum isl_dim_type type, unsigned pos) |
| 10424 | { |
| 10425 | return basic_map_dim_is_bounded(bmap, type, pos, 0, 0); |
| 10426 | } |
| 10427 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10428 | isl_bool isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap, |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10429 | enum isl_dim_type type, unsigned pos) |
| 10430 | { |
| 10431 | return basic_map_dim_is_bounded(bmap, type, pos, 0, 1); |
| 10432 | } |
| 10433 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10434 | isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap, |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10435 | enum isl_dim_type type, unsigned pos) |
| 10436 | { |
| 10437 | return basic_map_dim_is_bounded(bmap, type, pos, 1, 0); |
| 10438 | } |
| 10439 | |
| 10440 | int isl_map_dim_is_bounded(__isl_keep isl_map *map, |
| 10441 | enum isl_dim_type type, unsigned pos) |
| 10442 | { |
| 10443 | int i; |
| 10444 | |
| 10445 | if (!map) |
| 10446 | return -1; |
| 10447 | |
| 10448 | for (i = 0; i < map->n; ++i) { |
| 10449 | int bounded; |
| 10450 | bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos); |
| 10451 | if (bounded < 0 || !bounded) |
| 10452 | return bounded; |
| 10453 | } |
| 10454 | |
| 10455 | return 1; |
| 10456 | } |
| 10457 | |
| 10458 | /* Return 1 if the specified dim is involved in both an upper bound |
| 10459 | * and a lower bound. |
| 10460 | */ |
| 10461 | int isl_set_dim_is_bounded(__isl_keep isl_set *set, |
| 10462 | enum isl_dim_type type, unsigned pos) |
| 10463 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 10464 | return isl_map_dim_is_bounded(set_to_map(set), type, pos); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10465 | } |
| 10466 | |
| 10467 | /* Does "map" have a bound (according to "fn") for any of its basic maps? |
| 10468 | */ |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10469 | static isl_bool has_any_bound(__isl_keep isl_map *map, |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10470 | enum isl_dim_type type, unsigned pos, |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10471 | isl_bool (*fn)(__isl_keep isl_basic_map *bmap, |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10472 | enum isl_dim_type type, unsigned pos)) |
| 10473 | { |
| 10474 | int i; |
| 10475 | |
| 10476 | if (!map) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10477 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10478 | |
| 10479 | for (i = 0; i < map->n; ++i) { |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10480 | isl_bool bounded; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10481 | bounded = fn(map->p[i], type, pos); |
| 10482 | if (bounded < 0 || bounded) |
| 10483 | return bounded; |
| 10484 | } |
| 10485 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10486 | return isl_bool_false; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10487 | } |
| 10488 | |
| 10489 | /* Return 1 if the specified dim is involved in any lower bound. |
| 10490 | */ |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10491 | isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set, |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10492 | enum isl_dim_type type, unsigned pos) |
| 10493 | { |
| 10494 | return has_any_bound(set, type, pos, |
| 10495 | &isl_basic_map_dim_has_lower_bound); |
| 10496 | } |
| 10497 | |
| 10498 | /* Return 1 if the specified dim is involved in any upper bound. |
| 10499 | */ |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10500 | isl_bool isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set, |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10501 | enum isl_dim_type type, unsigned pos) |
| 10502 | { |
| 10503 | return has_any_bound(set, type, pos, |
| 10504 | &isl_basic_map_dim_has_upper_bound); |
| 10505 | } |
| 10506 | |
| 10507 | /* Does "map" have a bound (according to "fn") for all of its basic maps? |
| 10508 | */ |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10509 | static isl_bool has_bound(__isl_keep isl_map *map, |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10510 | enum isl_dim_type type, unsigned pos, |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10511 | isl_bool (*fn)(__isl_keep isl_basic_map *bmap, |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10512 | enum isl_dim_type type, unsigned pos)) |
| 10513 | { |
| 10514 | int i; |
| 10515 | |
| 10516 | if (!map) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10517 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10518 | |
| 10519 | for (i = 0; i < map->n; ++i) { |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10520 | isl_bool bounded; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10521 | bounded = fn(map->p[i], type, pos); |
| 10522 | if (bounded < 0 || !bounded) |
| 10523 | return bounded; |
| 10524 | } |
| 10525 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10526 | return isl_bool_true; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10527 | } |
| 10528 | |
| 10529 | /* Return 1 if the specified dim has a lower bound (in each of its basic sets). |
| 10530 | */ |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10531 | isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set, |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10532 | enum isl_dim_type type, unsigned pos) |
| 10533 | { |
| 10534 | return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound); |
| 10535 | } |
| 10536 | |
| 10537 | /* Return 1 if the specified dim has an upper bound (in each of its basic sets). |
| 10538 | */ |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10539 | isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set, |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10540 | enum isl_dim_type type, unsigned pos) |
| 10541 | { |
| 10542 | return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound); |
| 10543 | } |
| 10544 | |
| 10545 | /* For each of the "n" variables starting at "first", determine |
| 10546 | * the sign of the variable and put the results in the first "n" |
| 10547 | * elements of the array "signs". |
| 10548 | * Sign |
| 10549 | * 1 means that the variable is non-negative |
| 10550 | * -1 means that the variable is non-positive |
| 10551 | * 0 means the variable attains both positive and negative values. |
| 10552 | */ |
| 10553 | int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset, |
| 10554 | unsigned first, unsigned n, int *signs) |
| 10555 | { |
| 10556 | isl_vec *bound = NULL; |
| 10557 | struct isl_tab *tab = NULL; |
| 10558 | struct isl_tab_undo *snap; |
| 10559 | int i; |
| 10560 | |
| 10561 | if (!bset || !signs) |
| 10562 | return -1; |
| 10563 | |
| 10564 | bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset)); |
| 10565 | tab = isl_tab_from_basic_set(bset, 0); |
| 10566 | if (!bound || !tab) |
| 10567 | goto error; |
| 10568 | |
| 10569 | isl_seq_clr(bound->el, bound->size); |
| 10570 | isl_int_set_si(bound->el[0], -1); |
| 10571 | |
| 10572 | snap = isl_tab_snap(tab); |
| 10573 | for (i = 0; i < n; ++i) { |
| 10574 | int empty; |
| 10575 | |
| 10576 | isl_int_set_si(bound->el[1 + first + i], -1); |
| 10577 | if (isl_tab_add_ineq(tab, bound->el) < 0) |
| 10578 | goto error; |
| 10579 | empty = tab->empty; |
| 10580 | isl_int_set_si(bound->el[1 + first + i], 0); |
| 10581 | if (isl_tab_rollback(tab, snap) < 0) |
| 10582 | goto error; |
| 10583 | |
| 10584 | if (empty) { |
| 10585 | signs[i] = 1; |
| 10586 | continue; |
| 10587 | } |
| 10588 | |
| 10589 | isl_int_set_si(bound->el[1 + first + i], 1); |
| 10590 | if (isl_tab_add_ineq(tab, bound->el) < 0) |
| 10591 | goto error; |
| 10592 | empty = tab->empty; |
| 10593 | isl_int_set_si(bound->el[1 + first + i], 0); |
| 10594 | if (isl_tab_rollback(tab, snap) < 0) |
| 10595 | goto error; |
| 10596 | |
| 10597 | signs[i] = empty ? -1 : 0; |
| 10598 | } |
| 10599 | |
| 10600 | isl_tab_free(tab); |
| 10601 | isl_vec_free(bound); |
| 10602 | return 0; |
| 10603 | error: |
| 10604 | isl_tab_free(tab); |
| 10605 | isl_vec_free(bound); |
| 10606 | return -1; |
| 10607 | } |
| 10608 | |
| 10609 | int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset, |
| 10610 | enum isl_dim_type type, unsigned first, unsigned n, int *signs) |
| 10611 | { |
| 10612 | if (!bset || !signs) |
| 10613 | return -1; |
| 10614 | isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type), |
| 10615 | return -1); |
| 10616 | |
| 10617 | first += pos(bset->dim, type) - 1; |
| 10618 | return isl_basic_set_vars_get_sign(bset, first, n, signs); |
| 10619 | } |
| 10620 | |
| 10621 | /* Is it possible for the integer division "div" to depend (possibly |
| 10622 | * indirectly) on any output dimensions? |
| 10623 | * |
| 10624 | * If the div is undefined, then we conservatively assume that it |
| 10625 | * may depend on them. |
| 10626 | * Otherwise, we check if it actually depends on them or on any integer |
| 10627 | * divisions that may depend on them. |
| 10628 | */ |
| 10629 | static int div_may_involve_output(__isl_keep isl_basic_map *bmap, int div) |
| 10630 | { |
| 10631 | int i; |
| 10632 | unsigned n_out, o_out; |
| 10633 | unsigned n_div, o_div; |
| 10634 | |
| 10635 | if (isl_int_is_zero(bmap->div[div][0])) |
| 10636 | return 1; |
| 10637 | |
| 10638 | n_out = isl_basic_map_dim(bmap, isl_dim_out); |
| 10639 | o_out = isl_basic_map_offset(bmap, isl_dim_out); |
| 10640 | |
| 10641 | if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1) |
| 10642 | return 1; |
| 10643 | |
| 10644 | n_div = isl_basic_map_dim(bmap, isl_dim_div); |
| 10645 | o_div = isl_basic_map_offset(bmap, isl_dim_div); |
| 10646 | |
| 10647 | for (i = 0; i < n_div; ++i) { |
| 10648 | if (isl_int_is_zero(bmap->div[div][1 + o_div + i])) |
| 10649 | continue; |
| 10650 | if (div_may_involve_output(bmap, i)) |
| 10651 | return 1; |
| 10652 | } |
| 10653 | |
| 10654 | return 0; |
| 10655 | } |
| 10656 | |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 10657 | /* Return the first integer division of "bmap" in the range |
| 10658 | * [first, first + n[ that may depend on any output dimensions and |
| 10659 | * that has a non-zero coefficient in "c" (where the first coefficient |
| 10660 | * in "c" corresponds to integer division "first"). |
| 10661 | */ |
| 10662 | static int first_div_may_involve_output(__isl_keep isl_basic_map *bmap, |
| 10663 | isl_int *c, int first, int n) |
| 10664 | { |
| 10665 | int k; |
| 10666 | |
| 10667 | if (!bmap) |
| 10668 | return -1; |
| 10669 | |
| 10670 | for (k = first; k < first + n; ++k) { |
| 10671 | if (isl_int_is_zero(c[k])) |
| 10672 | continue; |
| 10673 | if (div_may_involve_output(bmap, k)) |
| 10674 | return k; |
| 10675 | } |
| 10676 | |
| 10677 | return first + n; |
| 10678 | } |
| 10679 | |
| 10680 | /* Look for a pair of inequality constraints in "bmap" of the form |
| 10681 | * |
| 10682 | * -l + i >= 0 or i >= l |
| 10683 | * and |
| 10684 | * n + l - i >= 0 or i <= l + n |
| 10685 | * |
| 10686 | * with n < "m" and i the output dimension at position "pos". |
| 10687 | * (Note that n >= 0 as otherwise the two constraints would conflict.) |
| 10688 | * Furthermore, "l" is only allowed to involve parameters, input dimensions |
| 10689 | * and earlier output dimensions, as well as integer divisions that do |
| 10690 | * not involve any of the output dimensions. |
| 10691 | * |
| 10692 | * Return the index of the first inequality constraint or bmap->n_ineq |
| 10693 | * if no such pair can be found. |
| 10694 | */ |
| 10695 | static int find_modulo_constraint_pair(__isl_keep isl_basic_map *bmap, |
| 10696 | int pos, isl_int m) |
| 10697 | { |
| 10698 | int i, j; |
| 10699 | isl_ctx *ctx; |
| 10700 | unsigned total; |
| 10701 | unsigned n_div, o_div; |
| 10702 | unsigned n_out, o_out; |
| 10703 | int less; |
| 10704 | |
| 10705 | if (!bmap) |
| 10706 | return -1; |
| 10707 | |
| 10708 | ctx = isl_basic_map_get_ctx(bmap); |
| 10709 | total = isl_basic_map_total_dim(bmap); |
| 10710 | n_out = isl_basic_map_dim(bmap, isl_dim_out); |
| 10711 | o_out = isl_basic_map_offset(bmap, isl_dim_out); |
| 10712 | n_div = isl_basic_map_dim(bmap, isl_dim_div); |
| 10713 | o_div = isl_basic_map_offset(bmap, isl_dim_div); |
| 10714 | for (i = 0; i < bmap->n_ineq; ++i) { |
| 10715 | if (!isl_int_abs_eq(bmap->ineq[i][o_out + pos], ctx->one)) |
| 10716 | continue; |
| 10717 | if (isl_seq_first_non_zero(bmap->ineq[i] + o_out + pos + 1, |
| 10718 | n_out - (pos + 1)) != -1) |
| 10719 | continue; |
| 10720 | if (first_div_may_involve_output(bmap, bmap->ineq[i] + o_div, |
| 10721 | 0, n_div) < n_div) |
| 10722 | continue; |
| 10723 | for (j = i + 1; j < bmap->n_ineq; ++j) { |
| 10724 | if (!isl_int_abs_eq(bmap->ineq[j][o_out + pos], |
| 10725 | ctx->one)) |
| 10726 | continue; |
| 10727 | if (!isl_seq_is_neg(bmap->ineq[i] + 1, |
| 10728 | bmap->ineq[j] + 1, total)) |
| 10729 | continue; |
| 10730 | break; |
| 10731 | } |
| 10732 | if (j >= bmap->n_ineq) |
| 10733 | continue; |
| 10734 | isl_int_add(bmap->ineq[i][0], |
| 10735 | bmap->ineq[i][0], bmap->ineq[j][0]); |
| 10736 | less = isl_int_abs_lt(bmap->ineq[i][0], m); |
| 10737 | isl_int_sub(bmap->ineq[i][0], |
| 10738 | bmap->ineq[i][0], bmap->ineq[j][0]); |
| 10739 | if (!less) |
| 10740 | continue; |
| 10741 | if (isl_int_is_one(bmap->ineq[i][o_out + pos])) |
| 10742 | return i; |
| 10743 | else |
| 10744 | return j; |
| 10745 | } |
| 10746 | |
| 10747 | return bmap->n_ineq; |
| 10748 | } |
| 10749 | |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10750 | /* Return the index of the equality of "bmap" that defines |
| 10751 | * the output dimension "pos" in terms of earlier dimensions. |
| 10752 | * The equality may also involve integer divisions, as long |
| 10753 | * as those integer divisions are defined in terms of |
| 10754 | * parameters or input dimensions. |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 10755 | * In this case, *div is set to the number of integer divisions and |
| 10756 | * *ineq is set to the number of inequality constraints (provided |
| 10757 | * div and ineq are not NULL). |
| 10758 | * |
| 10759 | * The equality may also involve a single integer division involving |
| 10760 | * the output dimensions (typically only output dimension "pos") as |
| 10761 | * long as the coefficient of output dimension "pos" is 1 or -1 and |
| 10762 | * there is a pair of constraints i >= l and i <= l + n, with i referring |
| 10763 | * to output dimension "pos", l an expression involving only earlier |
| 10764 | * dimensions and n smaller than the coefficient of the integer division |
| 10765 | * in the equality. In this case, the output dimension can be defined |
| 10766 | * in terms of a modulo expression that does not involve the integer division. |
| 10767 | * *div is then set to this single integer division and |
| 10768 | * *ineq is set to the index of constraint i >= l. |
| 10769 | * |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10770 | * Return bmap->n_eq if there is no such equality. |
| 10771 | * Return -1 on error. |
| 10772 | */ |
| 10773 | int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap, |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 10774 | int pos, int *div, int *ineq) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10775 | { |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 10776 | int j, k, l; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10777 | unsigned n_out, o_out; |
| 10778 | unsigned n_div, o_div; |
| 10779 | |
| 10780 | if (!bmap) |
| 10781 | return -1; |
| 10782 | |
| 10783 | n_out = isl_basic_map_dim(bmap, isl_dim_out); |
| 10784 | o_out = isl_basic_map_offset(bmap, isl_dim_out); |
| 10785 | n_div = isl_basic_map_dim(bmap, isl_dim_div); |
| 10786 | o_div = isl_basic_map_offset(bmap, isl_dim_div); |
| 10787 | |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 10788 | if (ineq) |
| 10789 | *ineq = bmap->n_ineq; |
| 10790 | if (div) |
| 10791 | *div = n_div; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10792 | for (j = 0; j < bmap->n_eq; ++j) { |
| 10793 | if (isl_int_is_zero(bmap->eq[j][o_out + pos])) |
| 10794 | continue; |
| 10795 | if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1, |
| 10796 | n_out - (pos + 1)) != -1) |
| 10797 | continue; |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 10798 | k = first_div_may_involve_output(bmap, bmap->eq[j] + o_div, |
| 10799 | 0, n_div); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10800 | if (k >= n_div) |
| 10801 | return j; |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 10802 | if (!isl_int_is_one(bmap->eq[j][o_out + pos]) && |
| 10803 | !isl_int_is_negone(bmap->eq[j][o_out + pos])) |
| 10804 | continue; |
| 10805 | if (first_div_may_involve_output(bmap, bmap->eq[j] + o_div, |
| 10806 | k + 1, n_div - (k+1)) < n_div) |
| 10807 | continue; |
| 10808 | l = find_modulo_constraint_pair(bmap, pos, |
| 10809 | bmap->eq[j][o_div + k]); |
| 10810 | if (l < 0) |
| 10811 | return -1; |
| 10812 | if (l >= bmap->n_ineq) |
| 10813 | continue; |
| 10814 | if (div) |
| 10815 | *div = k; |
| 10816 | if (ineq) |
| 10817 | *ineq = l; |
| 10818 | return j; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10819 | } |
| 10820 | |
| 10821 | return bmap->n_eq; |
| 10822 | } |
| 10823 | |
| 10824 | /* Check if the given basic map is obviously single-valued. |
| 10825 | * In particular, for each output dimension, check that there is |
| 10826 | * an equality that defines the output dimension in terms of |
| 10827 | * earlier dimensions. |
| 10828 | */ |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10829 | isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10830 | { |
| 10831 | int i; |
| 10832 | unsigned n_out; |
| 10833 | |
| 10834 | if (!bmap) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10835 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10836 | |
| 10837 | n_out = isl_basic_map_dim(bmap, isl_dim_out); |
| 10838 | |
| 10839 | for (i = 0; i < n_out; ++i) { |
| 10840 | int eq; |
| 10841 | |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 10842 | eq = isl_basic_map_output_defining_equality(bmap, i, |
| 10843 | NULL, NULL); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10844 | if (eq < 0) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10845 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10846 | if (eq >= bmap->n_eq) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10847 | return isl_bool_false; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10848 | } |
| 10849 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10850 | return isl_bool_true; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10851 | } |
| 10852 | |
| 10853 | /* Check if the given basic map is single-valued. |
| 10854 | * We simply compute |
| 10855 | * |
| 10856 | * M \circ M^-1 |
| 10857 | * |
| 10858 | * and check if the result is a subset of the identity mapping. |
| 10859 | */ |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10860 | isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10861 | { |
| 10862 | isl_space *space; |
| 10863 | isl_basic_map *test; |
| 10864 | isl_basic_map *id; |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10865 | isl_bool sv; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10866 | |
| 10867 | sv = isl_basic_map_plain_is_single_valued(bmap); |
| 10868 | if (sv < 0 || sv) |
| 10869 | return sv; |
| 10870 | |
| 10871 | test = isl_basic_map_reverse(isl_basic_map_copy(bmap)); |
| 10872 | test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap)); |
| 10873 | |
| 10874 | space = isl_basic_map_get_space(bmap); |
| 10875 | space = isl_space_map_from_set(isl_space_range(space)); |
| 10876 | id = isl_basic_map_identity(space); |
| 10877 | |
| 10878 | sv = isl_basic_map_is_subset(test, id); |
| 10879 | |
| 10880 | isl_basic_map_free(test); |
| 10881 | isl_basic_map_free(id); |
| 10882 | |
| 10883 | return sv; |
| 10884 | } |
| 10885 | |
| 10886 | /* Check if the given map is obviously single-valued. |
| 10887 | */ |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10888 | isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10889 | { |
| 10890 | if (!map) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10891 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10892 | if (map->n == 0) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10893 | return isl_bool_true; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10894 | if (map->n >= 2) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10895 | return isl_bool_false; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10896 | |
| 10897 | return isl_basic_map_plain_is_single_valued(map->p[0]); |
| 10898 | } |
| 10899 | |
| 10900 | /* Check if the given map is single-valued. |
| 10901 | * We simply compute |
| 10902 | * |
| 10903 | * M \circ M^-1 |
| 10904 | * |
| 10905 | * and check if the result is a subset of the identity mapping. |
| 10906 | */ |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10907 | isl_bool isl_map_is_single_valued(__isl_keep isl_map *map) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10908 | { |
| 10909 | isl_space *dim; |
| 10910 | isl_map *test; |
| 10911 | isl_map *id; |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10912 | isl_bool sv; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10913 | |
| 10914 | sv = isl_map_plain_is_single_valued(map); |
| 10915 | if (sv < 0 || sv) |
| 10916 | return sv; |
| 10917 | |
| 10918 | test = isl_map_reverse(isl_map_copy(map)); |
| 10919 | test = isl_map_apply_range(test, isl_map_copy(map)); |
| 10920 | |
| 10921 | dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map))); |
| 10922 | id = isl_map_identity(dim); |
| 10923 | |
| 10924 | sv = isl_map_is_subset(test, id); |
| 10925 | |
| 10926 | isl_map_free(test); |
| 10927 | isl_map_free(id); |
| 10928 | |
| 10929 | return sv; |
| 10930 | } |
| 10931 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10932 | isl_bool isl_map_is_injective(__isl_keep isl_map *map) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10933 | { |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10934 | isl_bool in; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10935 | |
| 10936 | map = isl_map_copy(map); |
| 10937 | map = isl_map_reverse(map); |
| 10938 | in = isl_map_is_single_valued(map); |
| 10939 | isl_map_free(map); |
| 10940 | |
| 10941 | return in; |
| 10942 | } |
| 10943 | |
| 10944 | /* Check if the given map is obviously injective. |
| 10945 | */ |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10946 | isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10947 | { |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10948 | isl_bool in; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10949 | |
| 10950 | map = isl_map_copy(map); |
| 10951 | map = isl_map_reverse(map); |
| 10952 | in = isl_map_plain_is_single_valued(map); |
| 10953 | isl_map_free(map); |
| 10954 | |
| 10955 | return in; |
| 10956 | } |
| 10957 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10958 | isl_bool isl_map_is_bijective(__isl_keep isl_map *map) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10959 | { |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10960 | isl_bool sv; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10961 | |
| 10962 | sv = isl_map_is_single_valued(map); |
| 10963 | if (sv < 0 || !sv) |
| 10964 | return sv; |
| 10965 | |
| 10966 | return isl_map_is_injective(map); |
| 10967 | } |
| 10968 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 10969 | isl_bool isl_set_is_singleton(__isl_keep isl_set *set) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10970 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 10971 | return isl_map_is_single_valued(set_to_map(set)); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 10972 | } |
| 10973 | |
Michael Kruse | e0b34f3 | 2016-05-04 14:41:36 +0000 | [diff] [blame] | 10974 | /* Does "map" only map elements to themselves? |
| 10975 | * |
| 10976 | * If the domain and range spaces are different, then "map" |
| 10977 | * is considered not to be an identity relation, even if it is empty. |
| 10978 | * Otherwise, construct the maximal identity relation and |
| 10979 | * check whether "map" is a subset of this relation. |
| 10980 | */ |
| 10981 | isl_bool isl_map_is_identity(__isl_keep isl_map *map) |
| 10982 | { |
| 10983 | isl_space *space; |
| 10984 | isl_map *id; |
| 10985 | isl_bool equal, is_identity; |
| 10986 | |
| 10987 | space = isl_map_get_space(map); |
| 10988 | equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out); |
| 10989 | isl_space_free(space); |
| 10990 | if (equal < 0 || !equal) |
| 10991 | return equal; |
| 10992 | |
| 10993 | id = isl_map_identity(isl_map_get_space(map)); |
| 10994 | is_identity = isl_map_is_subset(map, id); |
| 10995 | isl_map_free(id); |
| 10996 | |
| 10997 | return is_identity; |
| 10998 | } |
| 10999 | |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11000 | int isl_map_is_translation(__isl_keep isl_map *map) |
| 11001 | { |
| 11002 | int ok; |
| 11003 | isl_set *delta; |
| 11004 | |
| 11005 | delta = isl_map_deltas(isl_map_copy(map)); |
| 11006 | ok = isl_set_is_singleton(delta); |
| 11007 | isl_set_free(delta); |
| 11008 | |
| 11009 | return ok; |
| 11010 | } |
| 11011 | |
| 11012 | static int unique(isl_int *p, unsigned pos, unsigned len) |
| 11013 | { |
| 11014 | if (isl_seq_first_non_zero(p, pos) != -1) |
| 11015 | return 0; |
| 11016 | if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1) |
| 11017 | return 0; |
| 11018 | return 1; |
| 11019 | } |
| 11020 | |
| 11021 | int isl_basic_set_is_box(__isl_keep isl_basic_set *bset) |
| 11022 | { |
| 11023 | int i, j; |
| 11024 | unsigned nvar; |
| 11025 | unsigned ovar; |
| 11026 | |
| 11027 | if (!bset) |
| 11028 | return -1; |
| 11029 | |
| 11030 | if (isl_basic_set_dim(bset, isl_dim_div) != 0) |
| 11031 | return 0; |
| 11032 | |
| 11033 | nvar = isl_basic_set_dim(bset, isl_dim_set); |
| 11034 | ovar = isl_space_offset(bset->dim, isl_dim_set); |
| 11035 | for (j = 0; j < nvar; ++j) { |
| 11036 | int lower = 0, upper = 0; |
| 11037 | for (i = 0; i < bset->n_eq; ++i) { |
| 11038 | if (isl_int_is_zero(bset->eq[i][1 + ovar + j])) |
| 11039 | continue; |
| 11040 | if (!unique(bset->eq[i] + 1 + ovar, j, nvar)) |
| 11041 | return 0; |
| 11042 | break; |
| 11043 | } |
| 11044 | if (i < bset->n_eq) |
| 11045 | continue; |
| 11046 | for (i = 0; i < bset->n_ineq; ++i) { |
| 11047 | if (isl_int_is_zero(bset->ineq[i][1 + ovar + j])) |
| 11048 | continue; |
| 11049 | if (!unique(bset->ineq[i] + 1 + ovar, j, nvar)) |
| 11050 | return 0; |
| 11051 | if (isl_int_is_pos(bset->ineq[i][1 + ovar + j])) |
| 11052 | lower = 1; |
| 11053 | else |
| 11054 | upper = 1; |
| 11055 | } |
| 11056 | if (!lower || !upper) |
| 11057 | return 0; |
| 11058 | } |
| 11059 | |
| 11060 | return 1; |
| 11061 | } |
| 11062 | |
| 11063 | int isl_set_is_box(__isl_keep isl_set *set) |
| 11064 | { |
| 11065 | if (!set) |
| 11066 | return -1; |
| 11067 | if (set->n != 1) |
| 11068 | return 0; |
| 11069 | |
| 11070 | return isl_basic_set_is_box(set->p[0]); |
| 11071 | } |
| 11072 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 11073 | isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11074 | { |
| 11075 | if (!bset) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 11076 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11077 | |
| 11078 | return isl_space_is_wrapping(bset->dim); |
| 11079 | } |
| 11080 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 11081 | isl_bool isl_set_is_wrapping(__isl_keep isl_set *set) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11082 | { |
| 11083 | if (!set) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 11084 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11085 | |
| 11086 | return isl_space_is_wrapping(set->dim); |
| 11087 | } |
| 11088 | |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 11089 | /* Modify the space of "map" through a call to "change". |
| 11090 | * If "can_change" is set (not NULL), then first call it to check |
| 11091 | * if the modification is allowed, printing the error message "cannot_change" |
| 11092 | * if it is not. |
| 11093 | */ |
| 11094 | static __isl_give isl_map *isl_map_change_space(__isl_take isl_map *map, |
| 11095 | isl_bool (*can_change)(__isl_keep isl_map *map), |
| 11096 | const char *cannot_change, |
| 11097 | __isl_give isl_space *(*change)(__isl_take isl_space *space)) |
| 11098 | { |
| 11099 | isl_bool ok; |
| 11100 | isl_space *space; |
| 11101 | |
| 11102 | if (!map) |
| 11103 | return NULL; |
| 11104 | |
| 11105 | ok = can_change ? can_change(map) : isl_bool_true; |
| 11106 | if (ok < 0) |
| 11107 | return isl_map_free(map); |
| 11108 | if (!ok) |
| 11109 | isl_die(isl_map_get_ctx(map), isl_error_invalid, cannot_change, |
| 11110 | return isl_map_free(map)); |
| 11111 | |
| 11112 | space = change(isl_map_get_space(map)); |
| 11113 | map = isl_map_reset_space(map, space); |
| 11114 | |
| 11115 | return map; |
| 11116 | } |
| 11117 | |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11118 | /* Is the domain of "map" a wrapped relation? |
| 11119 | */ |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 11120 | isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11121 | { |
| 11122 | if (!map) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 11123 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11124 | |
| 11125 | return isl_space_domain_is_wrapping(map->dim); |
| 11126 | } |
| 11127 | |
| 11128 | /* Is the range of "map" a wrapped relation? |
| 11129 | */ |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 11130 | isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11131 | { |
| 11132 | if (!map) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 11133 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11134 | |
| 11135 | return isl_space_range_is_wrapping(map->dim); |
| 11136 | } |
| 11137 | |
| 11138 | __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap) |
| 11139 | { |
| 11140 | bmap = isl_basic_map_cow(bmap); |
| 11141 | if (!bmap) |
| 11142 | return NULL; |
| 11143 | |
| 11144 | bmap->dim = isl_space_wrap(bmap->dim); |
| 11145 | if (!bmap->dim) |
| 11146 | goto error; |
| 11147 | |
| 11148 | bmap = isl_basic_map_finalize(bmap); |
| 11149 | |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 11150 | return bset_from_bmap(bmap); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11151 | error: |
| 11152 | isl_basic_map_free(bmap); |
| 11153 | return NULL; |
| 11154 | } |
| 11155 | |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 11156 | /* Given a map A -> B, return the set (A -> B). |
| 11157 | */ |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11158 | __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map) |
| 11159 | { |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 11160 | return isl_map_change_space(map, NULL, NULL, &isl_space_wrap); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11161 | } |
| 11162 | |
| 11163 | __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset) |
| 11164 | { |
| 11165 | bset = isl_basic_set_cow(bset); |
| 11166 | if (!bset) |
| 11167 | return NULL; |
| 11168 | |
| 11169 | bset->dim = isl_space_unwrap(bset->dim); |
| 11170 | if (!bset->dim) |
| 11171 | goto error; |
| 11172 | |
| 11173 | bset = isl_basic_set_finalize(bset); |
| 11174 | |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 11175 | return bset_to_bmap(bset); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11176 | error: |
| 11177 | isl_basic_set_free(bset); |
| 11178 | return NULL; |
| 11179 | } |
| 11180 | |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 11181 | /* Given a set (A -> B), return the map A -> B. |
| 11182 | * Error out if "set" is not of the form (A -> B). |
| 11183 | */ |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11184 | __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set) |
| 11185 | { |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 11186 | return isl_map_change_space(set, &isl_set_is_wrapping, |
| 11187 | "not a wrapping set", &isl_space_unwrap); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11188 | } |
| 11189 | |
| 11190 | __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap, |
| 11191 | enum isl_dim_type type) |
| 11192 | { |
| 11193 | if (!bmap) |
| 11194 | return NULL; |
| 11195 | |
| 11196 | if (!isl_space_is_named_or_nested(bmap->dim, type)) |
| 11197 | return bmap; |
| 11198 | |
| 11199 | bmap = isl_basic_map_cow(bmap); |
| 11200 | if (!bmap) |
| 11201 | return NULL; |
| 11202 | |
| 11203 | bmap->dim = isl_space_reset(bmap->dim, type); |
| 11204 | if (!bmap->dim) |
| 11205 | goto error; |
| 11206 | |
| 11207 | bmap = isl_basic_map_finalize(bmap); |
| 11208 | |
| 11209 | return bmap; |
| 11210 | error: |
| 11211 | isl_basic_map_free(bmap); |
| 11212 | return NULL; |
| 11213 | } |
| 11214 | |
| 11215 | __isl_give isl_map *isl_map_reset(__isl_take isl_map *map, |
| 11216 | enum isl_dim_type type) |
| 11217 | { |
| 11218 | int i; |
| 11219 | |
| 11220 | if (!map) |
| 11221 | return NULL; |
| 11222 | |
| 11223 | if (!isl_space_is_named_or_nested(map->dim, type)) |
| 11224 | return map; |
| 11225 | |
| 11226 | map = isl_map_cow(map); |
| 11227 | if (!map) |
| 11228 | return NULL; |
| 11229 | |
| 11230 | for (i = 0; i < map->n; ++i) { |
| 11231 | map->p[i] = isl_basic_map_reset(map->p[i], type); |
| 11232 | if (!map->p[i]) |
| 11233 | goto error; |
| 11234 | } |
| 11235 | map->dim = isl_space_reset(map->dim, type); |
| 11236 | if (!map->dim) |
| 11237 | goto error; |
| 11238 | |
| 11239 | return map; |
| 11240 | error: |
| 11241 | isl_map_free(map); |
| 11242 | return NULL; |
| 11243 | } |
| 11244 | |
| 11245 | __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap) |
| 11246 | { |
| 11247 | if (!bmap) |
| 11248 | return NULL; |
| 11249 | |
| 11250 | if (!bmap->dim->nested[0] && !bmap->dim->nested[1]) |
| 11251 | return bmap; |
| 11252 | |
| 11253 | bmap = isl_basic_map_cow(bmap); |
| 11254 | if (!bmap) |
| 11255 | return NULL; |
| 11256 | |
| 11257 | bmap->dim = isl_space_flatten(bmap->dim); |
| 11258 | if (!bmap->dim) |
| 11259 | goto error; |
| 11260 | |
| 11261 | bmap = isl_basic_map_finalize(bmap); |
| 11262 | |
| 11263 | return bmap; |
| 11264 | error: |
| 11265 | isl_basic_map_free(bmap); |
| 11266 | return NULL; |
| 11267 | } |
| 11268 | |
| 11269 | __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset) |
| 11270 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 11271 | return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset))); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11272 | } |
| 11273 | |
| 11274 | __isl_give isl_basic_map *isl_basic_map_flatten_domain( |
| 11275 | __isl_take isl_basic_map *bmap) |
| 11276 | { |
| 11277 | if (!bmap) |
| 11278 | return NULL; |
| 11279 | |
| 11280 | if (!bmap->dim->nested[0]) |
| 11281 | return bmap; |
| 11282 | |
| 11283 | bmap = isl_basic_map_cow(bmap); |
| 11284 | if (!bmap) |
| 11285 | return NULL; |
| 11286 | |
| 11287 | bmap->dim = isl_space_flatten_domain(bmap->dim); |
| 11288 | if (!bmap->dim) |
| 11289 | goto error; |
| 11290 | |
| 11291 | bmap = isl_basic_map_finalize(bmap); |
| 11292 | |
| 11293 | return bmap; |
| 11294 | error: |
| 11295 | isl_basic_map_free(bmap); |
| 11296 | return NULL; |
| 11297 | } |
| 11298 | |
| 11299 | __isl_give isl_basic_map *isl_basic_map_flatten_range( |
| 11300 | __isl_take isl_basic_map *bmap) |
| 11301 | { |
| 11302 | if (!bmap) |
| 11303 | return NULL; |
| 11304 | |
| 11305 | if (!bmap->dim->nested[1]) |
| 11306 | return bmap; |
| 11307 | |
| 11308 | bmap = isl_basic_map_cow(bmap); |
| 11309 | if (!bmap) |
| 11310 | return NULL; |
| 11311 | |
| 11312 | bmap->dim = isl_space_flatten_range(bmap->dim); |
| 11313 | if (!bmap->dim) |
| 11314 | goto error; |
| 11315 | |
| 11316 | bmap = isl_basic_map_finalize(bmap); |
| 11317 | |
| 11318 | return bmap; |
| 11319 | error: |
| 11320 | isl_basic_map_free(bmap); |
| 11321 | return NULL; |
| 11322 | } |
| 11323 | |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 11324 | /* Remove any internal structure from the spaces of domain and range of "map". |
| 11325 | */ |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11326 | __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map) |
| 11327 | { |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11328 | if (!map) |
| 11329 | return NULL; |
| 11330 | |
| 11331 | if (!map->dim->nested[0] && !map->dim->nested[1]) |
| 11332 | return map; |
| 11333 | |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 11334 | return isl_map_change_space(map, NULL, NULL, &isl_space_flatten); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11335 | } |
| 11336 | |
| 11337 | __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set) |
| 11338 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 11339 | return set_from_map(isl_map_flatten(set_to_map(set))); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11340 | } |
| 11341 | |
| 11342 | __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set) |
| 11343 | { |
| 11344 | isl_space *dim, *flat_dim; |
| 11345 | isl_map *map; |
| 11346 | |
| 11347 | dim = isl_set_get_space(set); |
| 11348 | flat_dim = isl_space_flatten(isl_space_copy(dim)); |
| 11349 | map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim)); |
| 11350 | map = isl_map_intersect_domain(map, set); |
| 11351 | |
| 11352 | return map; |
| 11353 | } |
| 11354 | |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 11355 | /* Remove any internal structure from the space of the domain of "map". |
| 11356 | */ |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11357 | __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map) |
| 11358 | { |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11359 | if (!map) |
| 11360 | return NULL; |
| 11361 | |
| 11362 | if (!map->dim->nested[0]) |
| 11363 | return map; |
| 11364 | |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 11365 | return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_domain); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11366 | } |
| 11367 | |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 11368 | /* Remove any internal structure from the space of the range of "map". |
| 11369 | */ |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11370 | __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map) |
| 11371 | { |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11372 | if (!map) |
| 11373 | return NULL; |
| 11374 | |
| 11375 | if (!map->dim->nested[1]) |
| 11376 | return map; |
| 11377 | |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 11378 | return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_range); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11379 | } |
| 11380 | |
| 11381 | /* Reorder the dimensions of "bmap" according to the given dim_map |
Tobias Grosser | a7f1e04 | 2016-02-04 06:19:12 +0000 | [diff] [blame] | 11382 | * and set the dimension specification to "dim" and |
| 11383 | * perform Gaussian elimination on the result. |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11384 | */ |
| 11385 | __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap, |
| 11386 | __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map) |
| 11387 | { |
| 11388 | isl_basic_map *res; |
| 11389 | unsigned flags; |
| 11390 | |
| 11391 | bmap = isl_basic_map_cow(bmap); |
| 11392 | if (!bmap || !dim || !dim_map) |
| 11393 | goto error; |
| 11394 | |
| 11395 | flags = bmap->flags; |
| 11396 | ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL); |
| 11397 | ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED); |
| 11398 | ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS); |
| 11399 | res = isl_basic_map_alloc_space(dim, |
| 11400 | bmap->n_div, bmap->n_eq, bmap->n_ineq); |
| 11401 | res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map); |
| 11402 | if (res) |
| 11403 | res->flags = flags; |
Tobias Grosser | a7f1e04 | 2016-02-04 06:19:12 +0000 | [diff] [blame] | 11404 | res = isl_basic_map_gauss(res, NULL); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11405 | res = isl_basic_map_finalize(res); |
| 11406 | return res; |
| 11407 | error: |
| 11408 | free(dim_map); |
| 11409 | isl_basic_map_free(bmap); |
| 11410 | isl_space_free(dim); |
| 11411 | return NULL; |
| 11412 | } |
| 11413 | |
| 11414 | /* Reorder the dimensions of "map" according to given reordering. |
| 11415 | */ |
| 11416 | __isl_give isl_map *isl_map_realign(__isl_take isl_map *map, |
| 11417 | __isl_take isl_reordering *r) |
| 11418 | { |
| 11419 | int i; |
| 11420 | struct isl_dim_map *dim_map; |
| 11421 | |
| 11422 | map = isl_map_cow(map); |
| 11423 | dim_map = isl_dim_map_from_reordering(r); |
| 11424 | if (!map || !r || !dim_map) |
| 11425 | goto error; |
| 11426 | |
| 11427 | for (i = 0; i < map->n; ++i) { |
| 11428 | struct isl_dim_map *dim_map_i; |
| 11429 | |
| 11430 | dim_map_i = isl_dim_map_extend(dim_map, map->p[i]); |
| 11431 | |
| 11432 | map->p[i] = isl_basic_map_realign(map->p[i], |
| 11433 | isl_space_copy(r->dim), dim_map_i); |
| 11434 | |
| 11435 | if (!map->p[i]) |
| 11436 | goto error; |
| 11437 | } |
| 11438 | |
| 11439 | map = isl_map_reset_space(map, isl_space_copy(r->dim)); |
| 11440 | |
| 11441 | isl_reordering_free(r); |
| 11442 | free(dim_map); |
| 11443 | return map; |
| 11444 | error: |
| 11445 | free(dim_map); |
| 11446 | isl_map_free(map); |
| 11447 | isl_reordering_free(r); |
| 11448 | return NULL; |
| 11449 | } |
| 11450 | |
| 11451 | __isl_give isl_set *isl_set_realign(__isl_take isl_set *set, |
| 11452 | __isl_take isl_reordering *r) |
| 11453 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 11454 | return set_from_map(isl_map_realign(set_to_map(set), r)); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11455 | } |
| 11456 | |
| 11457 | __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map, |
| 11458 | __isl_take isl_space *model) |
| 11459 | { |
| 11460 | isl_ctx *ctx; |
| 11461 | |
| 11462 | if (!map || !model) |
| 11463 | goto error; |
| 11464 | |
| 11465 | ctx = isl_space_get_ctx(model); |
| 11466 | if (!isl_space_has_named_params(model)) |
| 11467 | isl_die(ctx, isl_error_invalid, |
| 11468 | "model has unnamed parameters", goto error); |
| 11469 | if (!isl_space_has_named_params(map->dim)) |
| 11470 | isl_die(ctx, isl_error_invalid, |
| 11471 | "relation has unnamed parameters", goto error); |
| 11472 | if (!isl_space_match(map->dim, isl_dim_param, model, isl_dim_param)) { |
| 11473 | isl_reordering *exp; |
| 11474 | |
| 11475 | model = isl_space_drop_dims(model, isl_dim_in, |
| 11476 | 0, isl_space_dim(model, isl_dim_in)); |
| 11477 | model = isl_space_drop_dims(model, isl_dim_out, |
| 11478 | 0, isl_space_dim(model, isl_dim_out)); |
| 11479 | exp = isl_parameter_alignment_reordering(map->dim, model); |
| 11480 | exp = isl_reordering_extend_space(exp, isl_map_get_space(map)); |
| 11481 | map = isl_map_realign(map, exp); |
| 11482 | } |
| 11483 | |
| 11484 | isl_space_free(model); |
| 11485 | return map; |
| 11486 | error: |
| 11487 | isl_space_free(model); |
| 11488 | isl_map_free(map); |
| 11489 | return NULL; |
| 11490 | } |
| 11491 | |
| 11492 | __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set, |
| 11493 | __isl_take isl_space *model) |
| 11494 | { |
| 11495 | return isl_map_align_params(set, model); |
| 11496 | } |
| 11497 | |
| 11498 | /* Align the parameters of "bmap" to those of "model", introducing |
| 11499 | * additional parameters if needed. |
| 11500 | */ |
| 11501 | __isl_give isl_basic_map *isl_basic_map_align_params( |
| 11502 | __isl_take isl_basic_map *bmap, __isl_take isl_space *model) |
| 11503 | { |
| 11504 | isl_ctx *ctx; |
| 11505 | |
| 11506 | if (!bmap || !model) |
| 11507 | goto error; |
| 11508 | |
| 11509 | ctx = isl_space_get_ctx(model); |
| 11510 | if (!isl_space_has_named_params(model)) |
| 11511 | isl_die(ctx, isl_error_invalid, |
| 11512 | "model has unnamed parameters", goto error); |
| 11513 | if (!isl_space_has_named_params(bmap->dim)) |
| 11514 | isl_die(ctx, isl_error_invalid, |
| 11515 | "relation has unnamed parameters", goto error); |
| 11516 | if (!isl_space_match(bmap->dim, isl_dim_param, model, isl_dim_param)) { |
| 11517 | isl_reordering *exp; |
| 11518 | struct isl_dim_map *dim_map; |
| 11519 | |
| 11520 | model = isl_space_drop_dims(model, isl_dim_in, |
| 11521 | 0, isl_space_dim(model, isl_dim_in)); |
| 11522 | model = isl_space_drop_dims(model, isl_dim_out, |
| 11523 | 0, isl_space_dim(model, isl_dim_out)); |
| 11524 | exp = isl_parameter_alignment_reordering(bmap->dim, model); |
| 11525 | exp = isl_reordering_extend_space(exp, |
| 11526 | isl_basic_map_get_space(bmap)); |
| 11527 | dim_map = isl_dim_map_from_reordering(exp); |
| 11528 | bmap = isl_basic_map_realign(bmap, |
| 11529 | exp ? isl_space_copy(exp->dim) : NULL, |
| 11530 | isl_dim_map_extend(dim_map, bmap)); |
| 11531 | isl_reordering_free(exp); |
| 11532 | free(dim_map); |
| 11533 | } |
| 11534 | |
| 11535 | isl_space_free(model); |
| 11536 | return bmap; |
| 11537 | error: |
| 11538 | isl_space_free(model); |
| 11539 | isl_basic_map_free(bmap); |
| 11540 | return NULL; |
| 11541 | } |
| 11542 | |
| 11543 | /* Align the parameters of "bset" to those of "model", introducing |
| 11544 | * additional parameters if needed. |
| 11545 | */ |
| 11546 | __isl_give isl_basic_set *isl_basic_set_align_params( |
| 11547 | __isl_take isl_basic_set *bset, __isl_take isl_space *model) |
| 11548 | { |
| 11549 | return isl_basic_map_align_params(bset, model); |
| 11550 | } |
| 11551 | |
| 11552 | __isl_give isl_mat *isl_basic_map_equalities_matrix( |
| 11553 | __isl_keep isl_basic_map *bmap, enum isl_dim_type c1, |
| 11554 | enum isl_dim_type c2, enum isl_dim_type c3, |
| 11555 | enum isl_dim_type c4, enum isl_dim_type c5) |
| 11556 | { |
| 11557 | enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 }; |
| 11558 | struct isl_mat *mat; |
| 11559 | int i, j, k; |
| 11560 | int pos; |
| 11561 | |
| 11562 | if (!bmap) |
| 11563 | return NULL; |
| 11564 | mat = isl_mat_alloc(bmap->ctx, bmap->n_eq, |
| 11565 | isl_basic_map_total_dim(bmap) + 1); |
| 11566 | if (!mat) |
| 11567 | return NULL; |
| 11568 | for (i = 0; i < bmap->n_eq; ++i) |
| 11569 | for (j = 0, pos = 0; j < 5; ++j) { |
| 11570 | int off = isl_basic_map_offset(bmap, c[j]); |
| 11571 | for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) { |
| 11572 | isl_int_set(mat->row[i][pos], |
| 11573 | bmap->eq[i][off + k]); |
| 11574 | ++pos; |
| 11575 | } |
| 11576 | } |
| 11577 | |
| 11578 | return mat; |
| 11579 | } |
| 11580 | |
| 11581 | __isl_give isl_mat *isl_basic_map_inequalities_matrix( |
| 11582 | __isl_keep isl_basic_map *bmap, enum isl_dim_type c1, |
| 11583 | enum isl_dim_type c2, enum isl_dim_type c3, |
| 11584 | enum isl_dim_type c4, enum isl_dim_type c5) |
| 11585 | { |
| 11586 | enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 }; |
| 11587 | struct isl_mat *mat; |
| 11588 | int i, j, k; |
| 11589 | int pos; |
| 11590 | |
| 11591 | if (!bmap) |
| 11592 | return NULL; |
| 11593 | mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq, |
| 11594 | isl_basic_map_total_dim(bmap) + 1); |
| 11595 | if (!mat) |
| 11596 | return NULL; |
| 11597 | for (i = 0; i < bmap->n_ineq; ++i) |
| 11598 | for (j = 0, pos = 0; j < 5; ++j) { |
| 11599 | int off = isl_basic_map_offset(bmap, c[j]); |
| 11600 | for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) { |
| 11601 | isl_int_set(mat->row[i][pos], |
| 11602 | bmap->ineq[i][off + k]); |
| 11603 | ++pos; |
| 11604 | } |
| 11605 | } |
| 11606 | |
| 11607 | return mat; |
| 11608 | } |
| 11609 | |
| 11610 | __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices( |
| 11611 | __isl_take isl_space *dim, |
| 11612 | __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1, |
| 11613 | enum isl_dim_type c2, enum isl_dim_type c3, |
| 11614 | enum isl_dim_type c4, enum isl_dim_type c5) |
| 11615 | { |
| 11616 | enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 }; |
| 11617 | isl_basic_map *bmap; |
| 11618 | unsigned total; |
| 11619 | unsigned extra; |
| 11620 | int i, j, k, l; |
| 11621 | int pos; |
| 11622 | |
| 11623 | if (!dim || !eq || !ineq) |
| 11624 | goto error; |
| 11625 | |
| 11626 | if (eq->n_col != ineq->n_col) |
| 11627 | isl_die(dim->ctx, isl_error_invalid, |
| 11628 | "equalities and inequalities matrices should have " |
| 11629 | "same number of columns", goto error); |
| 11630 | |
| 11631 | total = 1 + isl_space_dim(dim, isl_dim_all); |
| 11632 | |
| 11633 | if (eq->n_col < total) |
| 11634 | isl_die(dim->ctx, isl_error_invalid, |
| 11635 | "number of columns too small", goto error); |
| 11636 | |
| 11637 | extra = eq->n_col - total; |
| 11638 | |
| 11639 | bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra, |
| 11640 | eq->n_row, ineq->n_row); |
| 11641 | if (!bmap) |
| 11642 | goto error; |
| 11643 | for (i = 0; i < extra; ++i) { |
| 11644 | k = isl_basic_map_alloc_div(bmap); |
| 11645 | if (k < 0) |
| 11646 | goto error; |
| 11647 | isl_int_set_si(bmap->div[k][0], 0); |
| 11648 | } |
| 11649 | for (i = 0; i < eq->n_row; ++i) { |
| 11650 | l = isl_basic_map_alloc_equality(bmap); |
| 11651 | if (l < 0) |
| 11652 | goto error; |
| 11653 | for (j = 0, pos = 0; j < 5; ++j) { |
| 11654 | int off = isl_basic_map_offset(bmap, c[j]); |
| 11655 | for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) { |
| 11656 | isl_int_set(bmap->eq[l][off + k], |
| 11657 | eq->row[i][pos]); |
| 11658 | ++pos; |
| 11659 | } |
| 11660 | } |
| 11661 | } |
| 11662 | for (i = 0; i < ineq->n_row; ++i) { |
| 11663 | l = isl_basic_map_alloc_inequality(bmap); |
| 11664 | if (l < 0) |
| 11665 | goto error; |
| 11666 | for (j = 0, pos = 0; j < 5; ++j) { |
| 11667 | int off = isl_basic_map_offset(bmap, c[j]); |
| 11668 | for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) { |
| 11669 | isl_int_set(bmap->ineq[l][off + k], |
| 11670 | ineq->row[i][pos]); |
| 11671 | ++pos; |
| 11672 | } |
| 11673 | } |
| 11674 | } |
| 11675 | |
| 11676 | isl_space_free(dim); |
| 11677 | isl_mat_free(eq); |
| 11678 | isl_mat_free(ineq); |
| 11679 | |
| 11680 | bmap = isl_basic_map_simplify(bmap); |
| 11681 | return isl_basic_map_finalize(bmap); |
| 11682 | error: |
| 11683 | isl_space_free(dim); |
| 11684 | isl_mat_free(eq); |
| 11685 | isl_mat_free(ineq); |
| 11686 | return NULL; |
| 11687 | } |
| 11688 | |
| 11689 | __isl_give isl_mat *isl_basic_set_equalities_matrix( |
| 11690 | __isl_keep isl_basic_set *bset, enum isl_dim_type c1, |
| 11691 | enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4) |
| 11692 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 11693 | return isl_basic_map_equalities_matrix(bset_to_bmap(bset), |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11694 | c1, c2, c3, c4, isl_dim_in); |
| 11695 | } |
| 11696 | |
| 11697 | __isl_give isl_mat *isl_basic_set_inequalities_matrix( |
| 11698 | __isl_keep isl_basic_set *bset, enum isl_dim_type c1, |
| 11699 | enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4) |
| 11700 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 11701 | return isl_basic_map_inequalities_matrix(bset_to_bmap(bset), |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11702 | c1, c2, c3, c4, isl_dim_in); |
| 11703 | } |
| 11704 | |
| 11705 | __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices( |
| 11706 | __isl_take isl_space *dim, |
| 11707 | __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1, |
| 11708 | enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4) |
| 11709 | { |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 11710 | isl_basic_map *bmap; |
| 11711 | bmap = isl_basic_map_from_constraint_matrices(dim, eq, ineq, |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11712 | c1, c2, c3, c4, isl_dim_in); |
Tobias Grosser | 06e1592 | 2016-11-16 11:06:47 +0000 | [diff] [blame] | 11713 | return bset_from_bmap(bmap); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11714 | } |
| 11715 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 11716 | isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11717 | { |
| 11718 | if (!bmap) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 11719 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11720 | |
| 11721 | return isl_space_can_zip(bmap->dim); |
| 11722 | } |
| 11723 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 11724 | isl_bool isl_map_can_zip(__isl_keep isl_map *map) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11725 | { |
| 11726 | if (!map) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 11727 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11728 | |
| 11729 | return isl_space_can_zip(map->dim); |
| 11730 | } |
| 11731 | |
| 11732 | /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map |
| 11733 | * (A -> C) -> (B -> D). |
| 11734 | */ |
| 11735 | __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap) |
| 11736 | { |
| 11737 | unsigned pos; |
| 11738 | unsigned n1; |
| 11739 | unsigned n2; |
| 11740 | |
| 11741 | if (!bmap) |
| 11742 | return NULL; |
| 11743 | |
| 11744 | if (!isl_basic_map_can_zip(bmap)) |
| 11745 | isl_die(bmap->ctx, isl_error_invalid, |
| 11746 | "basic map cannot be zipped", goto error); |
| 11747 | pos = isl_basic_map_offset(bmap, isl_dim_in) + |
| 11748 | isl_space_dim(bmap->dim->nested[0], isl_dim_in); |
| 11749 | n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out); |
| 11750 | n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in); |
| 11751 | bmap = isl_basic_map_cow(bmap); |
| 11752 | bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2); |
| 11753 | if (!bmap) |
| 11754 | return NULL; |
| 11755 | bmap->dim = isl_space_zip(bmap->dim); |
| 11756 | if (!bmap->dim) |
| 11757 | goto error; |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 11758 | bmap = isl_basic_map_mark_final(bmap); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11759 | return bmap; |
| 11760 | error: |
| 11761 | isl_basic_map_free(bmap); |
| 11762 | return NULL; |
| 11763 | } |
| 11764 | |
| 11765 | /* Given a map (A -> B) -> (C -> D), return the corresponding map |
| 11766 | * (A -> C) -> (B -> D). |
| 11767 | */ |
| 11768 | __isl_give isl_map *isl_map_zip(__isl_take isl_map *map) |
| 11769 | { |
| 11770 | int i; |
| 11771 | |
| 11772 | if (!map) |
| 11773 | return NULL; |
| 11774 | |
| 11775 | if (!isl_map_can_zip(map)) |
| 11776 | isl_die(map->ctx, isl_error_invalid, "map cannot be zipped", |
| 11777 | goto error); |
| 11778 | |
| 11779 | map = isl_map_cow(map); |
| 11780 | if (!map) |
| 11781 | return NULL; |
| 11782 | |
| 11783 | for (i = 0; i < map->n; ++i) { |
| 11784 | map->p[i] = isl_basic_map_zip(map->p[i]); |
| 11785 | if (!map->p[i]) |
| 11786 | goto error; |
| 11787 | } |
| 11788 | |
| 11789 | map->dim = isl_space_zip(map->dim); |
| 11790 | if (!map->dim) |
| 11791 | goto error; |
| 11792 | |
| 11793 | return map; |
| 11794 | error: |
| 11795 | isl_map_free(map); |
| 11796 | return NULL; |
| 11797 | } |
| 11798 | |
| 11799 | /* Can we apply isl_basic_map_curry to "bmap"? |
| 11800 | * That is, does it have a nested relation in its domain? |
| 11801 | */ |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 11802 | isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11803 | { |
| 11804 | if (!bmap) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 11805 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11806 | |
| 11807 | return isl_space_can_curry(bmap->dim); |
| 11808 | } |
| 11809 | |
| 11810 | /* Can we apply isl_map_curry to "map"? |
| 11811 | * That is, does it have a nested relation in its domain? |
| 11812 | */ |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 11813 | isl_bool isl_map_can_curry(__isl_keep isl_map *map) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11814 | { |
| 11815 | if (!map) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 11816 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11817 | |
| 11818 | return isl_space_can_curry(map->dim); |
| 11819 | } |
| 11820 | |
| 11821 | /* Given a basic map (A -> B) -> C, return the corresponding basic map |
| 11822 | * A -> (B -> C). |
| 11823 | */ |
| 11824 | __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap) |
| 11825 | { |
| 11826 | |
| 11827 | if (!bmap) |
| 11828 | return NULL; |
| 11829 | |
| 11830 | if (!isl_basic_map_can_curry(bmap)) |
| 11831 | isl_die(bmap->ctx, isl_error_invalid, |
| 11832 | "basic map cannot be curried", goto error); |
| 11833 | bmap = isl_basic_map_cow(bmap); |
| 11834 | if (!bmap) |
| 11835 | return NULL; |
| 11836 | bmap->dim = isl_space_curry(bmap->dim); |
| 11837 | if (!bmap->dim) |
| 11838 | goto error; |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 11839 | bmap = isl_basic_map_mark_final(bmap); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11840 | return bmap; |
| 11841 | error: |
| 11842 | isl_basic_map_free(bmap); |
| 11843 | return NULL; |
| 11844 | } |
| 11845 | |
| 11846 | /* Given a map (A -> B) -> C, return the corresponding map |
| 11847 | * A -> (B -> C). |
| 11848 | */ |
| 11849 | __isl_give isl_map *isl_map_curry(__isl_take isl_map *map) |
| 11850 | { |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 11851 | return isl_map_change_space(map, &isl_map_can_curry, |
| 11852 | "map cannot be curried", &isl_space_curry); |
| 11853 | } |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11854 | |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 11855 | /* Can isl_map_range_curry be applied to "map"? |
| 11856 | * That is, does it have a nested relation in its range, |
| 11857 | * the domain of which is itself a nested relation? |
| 11858 | */ |
| 11859 | isl_bool isl_map_can_range_curry(__isl_keep isl_map *map) |
| 11860 | { |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11861 | if (!map) |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 11862 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11863 | |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 11864 | return isl_space_can_range_curry(map->dim); |
| 11865 | } |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11866 | |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 11867 | /* Given a map A -> ((B -> C) -> D), return the corresponding map |
| 11868 | * A -> (B -> (C -> D)). |
| 11869 | */ |
| 11870 | __isl_give isl_map *isl_map_range_curry(__isl_take isl_map *map) |
| 11871 | { |
| 11872 | return isl_map_change_space(map, &isl_map_can_range_curry, |
| 11873 | "map range cannot be curried", |
| 11874 | &isl_space_range_curry); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11875 | } |
| 11876 | |
| 11877 | /* Can we apply isl_basic_map_uncurry to "bmap"? |
| 11878 | * That is, does it have a nested relation in its domain? |
| 11879 | */ |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 11880 | isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11881 | { |
| 11882 | if (!bmap) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 11883 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11884 | |
| 11885 | return isl_space_can_uncurry(bmap->dim); |
| 11886 | } |
| 11887 | |
| 11888 | /* Can we apply isl_map_uncurry to "map"? |
| 11889 | * That is, does it have a nested relation in its domain? |
| 11890 | */ |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 11891 | isl_bool isl_map_can_uncurry(__isl_keep isl_map *map) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11892 | { |
| 11893 | if (!map) |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 11894 | return isl_bool_error; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11895 | |
| 11896 | return isl_space_can_uncurry(map->dim); |
| 11897 | } |
| 11898 | |
| 11899 | /* Given a basic map A -> (B -> C), return the corresponding basic map |
| 11900 | * (A -> B) -> C. |
| 11901 | */ |
| 11902 | __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap) |
| 11903 | { |
| 11904 | |
| 11905 | if (!bmap) |
| 11906 | return NULL; |
| 11907 | |
| 11908 | if (!isl_basic_map_can_uncurry(bmap)) |
| 11909 | isl_die(bmap->ctx, isl_error_invalid, |
| 11910 | "basic map cannot be uncurried", |
| 11911 | return isl_basic_map_free(bmap)); |
| 11912 | bmap = isl_basic_map_cow(bmap); |
| 11913 | if (!bmap) |
| 11914 | return NULL; |
| 11915 | bmap->dim = isl_space_uncurry(bmap->dim); |
| 11916 | if (!bmap->dim) |
| 11917 | return isl_basic_map_free(bmap); |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 11918 | bmap = isl_basic_map_mark_final(bmap); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11919 | return bmap; |
| 11920 | } |
| 11921 | |
| 11922 | /* Given a map A -> (B -> C), return the corresponding map |
| 11923 | * (A -> B) -> C. |
| 11924 | */ |
| 11925 | __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map) |
| 11926 | { |
Michael Kruse | 959a8dc | 2016-01-15 15:54:45 +0000 | [diff] [blame] | 11927 | return isl_map_change_space(map, &isl_map_can_uncurry, |
| 11928 | "map cannot be uncurried", &isl_space_uncurry); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11929 | } |
| 11930 | |
| 11931 | /* Construct a basic map mapping the domain of the affine expression |
| 11932 | * to a one-dimensional range prescribed by the affine expression. |
Tobias Grosser | 5652c9c | 2016-10-01 19:46:51 +0000 | [diff] [blame] | 11933 | * If "rational" is set, then construct a rational basic map. |
Tobias Grosser | 37034db | 2016-03-25 19:38:18 +0000 | [diff] [blame] | 11934 | * |
| 11935 | * A NaN affine expression cannot be converted to a basic map. |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11936 | */ |
Tobias Grosser | 5652c9c | 2016-10-01 19:46:51 +0000 | [diff] [blame] | 11937 | static __isl_give isl_basic_map *isl_basic_map_from_aff2( |
| 11938 | __isl_take isl_aff *aff, int rational) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11939 | { |
| 11940 | int k; |
| 11941 | int pos; |
Tobias Grosser | 37034db | 2016-03-25 19:38:18 +0000 | [diff] [blame] | 11942 | isl_bool is_nan; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11943 | isl_local_space *ls; |
Tobias Grosser | 37034db | 2016-03-25 19:38:18 +0000 | [diff] [blame] | 11944 | isl_basic_map *bmap = NULL; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11945 | |
| 11946 | if (!aff) |
| 11947 | return NULL; |
Tobias Grosser | 37034db | 2016-03-25 19:38:18 +0000 | [diff] [blame] | 11948 | is_nan = isl_aff_is_nan(aff); |
| 11949 | if (is_nan < 0) |
| 11950 | goto error; |
| 11951 | if (is_nan) |
| 11952 | isl_die(isl_aff_get_ctx(aff), isl_error_invalid, |
| 11953 | "cannot convert NaN", goto error); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11954 | |
| 11955 | ls = isl_aff_get_local_space(aff); |
| 11956 | bmap = isl_basic_map_from_local_space(ls); |
| 11957 | bmap = isl_basic_map_extend_constraints(bmap, 1, 0); |
| 11958 | k = isl_basic_map_alloc_equality(bmap); |
| 11959 | if (k < 0) |
| 11960 | goto error; |
| 11961 | |
| 11962 | pos = isl_basic_map_offset(bmap, isl_dim_out); |
| 11963 | isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos); |
| 11964 | isl_int_neg(bmap->eq[k][pos], aff->v->el[0]); |
| 11965 | isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos, |
| 11966 | aff->v->size - (pos + 1)); |
| 11967 | |
| 11968 | isl_aff_free(aff); |
Tobias Grosser | 5652c9c | 2016-10-01 19:46:51 +0000 | [diff] [blame] | 11969 | if (rational) |
| 11970 | bmap = isl_basic_map_set_rational(bmap); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11971 | bmap = isl_basic_map_finalize(bmap); |
| 11972 | return bmap; |
| 11973 | error: |
| 11974 | isl_aff_free(aff); |
| 11975 | isl_basic_map_free(bmap); |
| 11976 | return NULL; |
| 11977 | } |
| 11978 | |
Tobias Grosser | 5652c9c | 2016-10-01 19:46:51 +0000 | [diff] [blame] | 11979 | /* Construct a basic map mapping the domain of the affine expression |
| 11980 | * to a one-dimensional range prescribed by the affine expression. |
| 11981 | */ |
| 11982 | __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff) |
| 11983 | { |
| 11984 | return isl_basic_map_from_aff2(aff, 0); |
| 11985 | } |
| 11986 | |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 11987 | /* Construct a map mapping the domain of the affine expression |
| 11988 | * to a one-dimensional range prescribed by the affine expression. |
| 11989 | */ |
| 11990 | __isl_give isl_map *isl_map_from_aff(__isl_take isl_aff *aff) |
| 11991 | { |
| 11992 | isl_basic_map *bmap; |
| 11993 | |
| 11994 | bmap = isl_basic_map_from_aff(aff); |
| 11995 | return isl_map_from_basic_map(bmap); |
| 11996 | } |
| 11997 | |
| 11998 | /* Construct a basic map mapping the domain the multi-affine expression |
| 11999 | * to its range, with each dimension in the range equated to the |
| 12000 | * corresponding affine expression. |
Tobias Grosser | 5652c9c | 2016-10-01 19:46:51 +0000 | [diff] [blame] | 12001 | * If "rational" is set, then construct a rational basic map. |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 12002 | */ |
Tobias Grosser | 5652c9c | 2016-10-01 19:46:51 +0000 | [diff] [blame] | 12003 | __isl_give isl_basic_map *isl_basic_map_from_multi_aff2( |
| 12004 | __isl_take isl_multi_aff *maff, int rational) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 12005 | { |
| 12006 | int i; |
| 12007 | isl_space *space; |
| 12008 | isl_basic_map *bmap; |
| 12009 | |
| 12010 | if (!maff) |
| 12011 | return NULL; |
| 12012 | |
| 12013 | if (isl_space_dim(maff->space, isl_dim_out) != maff->n) |
| 12014 | isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal, |
| 12015 | "invalid space", goto error); |
| 12016 | |
| 12017 | space = isl_space_domain(isl_multi_aff_get_space(maff)); |
| 12018 | bmap = isl_basic_map_universe(isl_space_from_domain(space)); |
Tobias Grosser | 5652c9c | 2016-10-01 19:46:51 +0000 | [diff] [blame] | 12019 | if (rational) |
| 12020 | bmap = isl_basic_map_set_rational(bmap); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 12021 | |
| 12022 | for (i = 0; i < maff->n; ++i) { |
| 12023 | isl_aff *aff; |
| 12024 | isl_basic_map *bmap_i; |
| 12025 | |
| 12026 | aff = isl_aff_copy(maff->p[i]); |
Tobias Grosser | 5652c9c | 2016-10-01 19:46:51 +0000 | [diff] [blame] | 12027 | bmap_i = isl_basic_map_from_aff2(aff, rational); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 12028 | |
| 12029 | bmap = isl_basic_map_flat_range_product(bmap, bmap_i); |
| 12030 | } |
| 12031 | |
| 12032 | bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff)); |
| 12033 | |
| 12034 | isl_multi_aff_free(maff); |
| 12035 | return bmap; |
| 12036 | error: |
| 12037 | isl_multi_aff_free(maff); |
| 12038 | return NULL; |
| 12039 | } |
| 12040 | |
Tobias Grosser | 5652c9c | 2016-10-01 19:46:51 +0000 | [diff] [blame] | 12041 | /* Construct a basic map mapping the domain the multi-affine expression |
| 12042 | * to its range, with each dimension in the range equated to the |
| 12043 | * corresponding affine expression. |
| 12044 | */ |
| 12045 | __isl_give isl_basic_map *isl_basic_map_from_multi_aff( |
| 12046 | __isl_take isl_multi_aff *ma) |
| 12047 | { |
| 12048 | return isl_basic_map_from_multi_aff2(ma, 0); |
| 12049 | } |
| 12050 | |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 12051 | /* Construct a map mapping the domain the multi-affine expression |
| 12052 | * to its range, with each dimension in the range equated to the |
| 12053 | * corresponding affine expression. |
| 12054 | */ |
| 12055 | __isl_give isl_map *isl_map_from_multi_aff(__isl_take isl_multi_aff *maff) |
| 12056 | { |
| 12057 | isl_basic_map *bmap; |
| 12058 | |
| 12059 | bmap = isl_basic_map_from_multi_aff(maff); |
| 12060 | return isl_map_from_basic_map(bmap); |
| 12061 | } |
| 12062 | |
| 12063 | /* Construct a basic map mapping a domain in the given space to |
| 12064 | * to an n-dimensional range, with n the number of elements in the list, |
| 12065 | * where each coordinate in the range is prescribed by the |
| 12066 | * corresponding affine expression. |
| 12067 | * The domains of all affine expressions in the list are assumed to match |
| 12068 | * domain_dim. |
| 12069 | */ |
| 12070 | __isl_give isl_basic_map *isl_basic_map_from_aff_list( |
| 12071 | __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list) |
| 12072 | { |
| 12073 | int i; |
| 12074 | isl_space *dim; |
| 12075 | isl_basic_map *bmap; |
| 12076 | |
| 12077 | if (!list) |
| 12078 | return NULL; |
| 12079 | |
| 12080 | dim = isl_space_from_domain(domain_dim); |
| 12081 | bmap = isl_basic_map_universe(dim); |
| 12082 | |
| 12083 | for (i = 0; i < list->n; ++i) { |
| 12084 | isl_aff *aff; |
| 12085 | isl_basic_map *bmap_i; |
| 12086 | |
| 12087 | aff = isl_aff_copy(list->p[i]); |
| 12088 | bmap_i = isl_basic_map_from_aff(aff); |
| 12089 | |
| 12090 | bmap = isl_basic_map_flat_range_product(bmap, bmap_i); |
| 12091 | } |
| 12092 | |
| 12093 | isl_aff_list_free(list); |
| 12094 | return bmap; |
| 12095 | } |
| 12096 | |
| 12097 | __isl_give isl_set *isl_set_equate(__isl_take isl_set *set, |
| 12098 | enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2) |
| 12099 | { |
| 12100 | return isl_map_equate(set, type1, pos1, type2, pos2); |
| 12101 | } |
| 12102 | |
| 12103 | /* Construct a basic map where the given dimensions are equal to each other. |
| 12104 | */ |
| 12105 | static __isl_give isl_basic_map *equator(__isl_take isl_space *space, |
| 12106 | enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2) |
| 12107 | { |
| 12108 | isl_basic_map *bmap = NULL; |
| 12109 | int i; |
| 12110 | |
| 12111 | if (!space) |
| 12112 | return NULL; |
| 12113 | |
| 12114 | if (pos1 >= isl_space_dim(space, type1)) |
| 12115 | isl_die(isl_space_get_ctx(space), isl_error_invalid, |
| 12116 | "index out of bounds", goto error); |
| 12117 | if (pos2 >= isl_space_dim(space, type2)) |
| 12118 | isl_die(isl_space_get_ctx(space), isl_error_invalid, |
| 12119 | "index out of bounds", goto error); |
| 12120 | |
| 12121 | if (type1 == type2 && pos1 == pos2) |
| 12122 | return isl_basic_map_universe(space); |
| 12123 | |
| 12124 | bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0); |
| 12125 | i = isl_basic_map_alloc_equality(bmap); |
| 12126 | if (i < 0) |
| 12127 | goto error; |
| 12128 | isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap)); |
| 12129 | pos1 += isl_basic_map_offset(bmap, type1); |
| 12130 | pos2 += isl_basic_map_offset(bmap, type2); |
| 12131 | isl_int_set_si(bmap->eq[i][pos1], -1); |
| 12132 | isl_int_set_si(bmap->eq[i][pos2], 1); |
| 12133 | bmap = isl_basic_map_finalize(bmap); |
| 12134 | isl_space_free(space); |
| 12135 | return bmap; |
| 12136 | error: |
| 12137 | isl_space_free(space); |
| 12138 | isl_basic_map_free(bmap); |
| 12139 | return NULL; |
| 12140 | } |
| 12141 | |
| 12142 | /* Add a constraint imposing that the given two dimensions are equal. |
| 12143 | */ |
| 12144 | __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap, |
| 12145 | enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2) |
| 12146 | { |
| 12147 | isl_basic_map *eq; |
| 12148 | |
| 12149 | eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2); |
| 12150 | |
| 12151 | bmap = isl_basic_map_intersect(bmap, eq); |
| 12152 | |
| 12153 | return bmap; |
| 12154 | } |
| 12155 | |
| 12156 | /* Add a constraint imposing that the given two dimensions are equal. |
| 12157 | */ |
| 12158 | __isl_give isl_map *isl_map_equate(__isl_take isl_map *map, |
| 12159 | enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2) |
| 12160 | { |
| 12161 | isl_basic_map *bmap; |
| 12162 | |
| 12163 | bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2); |
| 12164 | |
| 12165 | map = isl_map_intersect(map, isl_map_from_basic_map(bmap)); |
| 12166 | |
| 12167 | return map; |
| 12168 | } |
| 12169 | |
| 12170 | /* Add a constraint imposing that the given two dimensions have opposite values. |
| 12171 | */ |
| 12172 | __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map, |
| 12173 | enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2) |
| 12174 | { |
| 12175 | isl_basic_map *bmap = NULL; |
| 12176 | int i; |
| 12177 | |
| 12178 | if (!map) |
| 12179 | return NULL; |
| 12180 | |
| 12181 | if (pos1 >= isl_map_dim(map, type1)) |
| 12182 | isl_die(map->ctx, isl_error_invalid, |
| 12183 | "index out of bounds", goto error); |
| 12184 | if (pos2 >= isl_map_dim(map, type2)) |
| 12185 | isl_die(map->ctx, isl_error_invalid, |
| 12186 | "index out of bounds", goto error); |
| 12187 | |
| 12188 | bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0); |
| 12189 | i = isl_basic_map_alloc_equality(bmap); |
| 12190 | if (i < 0) |
| 12191 | goto error; |
| 12192 | isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap)); |
| 12193 | pos1 += isl_basic_map_offset(bmap, type1); |
| 12194 | pos2 += isl_basic_map_offset(bmap, type2); |
| 12195 | isl_int_set_si(bmap->eq[i][pos1], 1); |
| 12196 | isl_int_set_si(bmap->eq[i][pos2], 1); |
| 12197 | bmap = isl_basic_map_finalize(bmap); |
| 12198 | |
| 12199 | map = isl_map_intersect(map, isl_map_from_basic_map(bmap)); |
| 12200 | |
| 12201 | return map; |
| 12202 | error: |
| 12203 | isl_basic_map_free(bmap); |
| 12204 | isl_map_free(map); |
| 12205 | return NULL; |
| 12206 | } |
| 12207 | |
| 12208 | /* Construct a constraint imposing that the value of the first dimension is |
| 12209 | * greater than or equal to that of the second. |
| 12210 | */ |
| 12211 | static __isl_give isl_constraint *constraint_order_ge( |
| 12212 | __isl_take isl_space *space, enum isl_dim_type type1, int pos1, |
| 12213 | enum isl_dim_type type2, int pos2) |
| 12214 | { |
| 12215 | isl_constraint *c; |
| 12216 | |
| 12217 | if (!space) |
| 12218 | return NULL; |
| 12219 | |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 12220 | c = isl_constraint_alloc_inequality(isl_local_space_from_space(space)); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 12221 | |
| 12222 | if (pos1 >= isl_constraint_dim(c, type1)) |
| 12223 | isl_die(isl_constraint_get_ctx(c), isl_error_invalid, |
| 12224 | "index out of bounds", return isl_constraint_free(c)); |
| 12225 | if (pos2 >= isl_constraint_dim(c, type2)) |
| 12226 | isl_die(isl_constraint_get_ctx(c), isl_error_invalid, |
| 12227 | "index out of bounds", return isl_constraint_free(c)); |
| 12228 | |
| 12229 | if (type1 == type2 && pos1 == pos2) |
| 12230 | return c; |
| 12231 | |
| 12232 | c = isl_constraint_set_coefficient_si(c, type1, pos1, 1); |
| 12233 | c = isl_constraint_set_coefficient_si(c, type2, pos2, -1); |
| 12234 | |
| 12235 | return c; |
| 12236 | } |
| 12237 | |
| 12238 | /* Add a constraint imposing that the value of the first dimension is |
| 12239 | * greater than or equal to that of the second. |
| 12240 | */ |
| 12241 | __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap, |
| 12242 | enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2) |
| 12243 | { |
| 12244 | isl_constraint *c; |
| 12245 | isl_space *space; |
| 12246 | |
| 12247 | if (type1 == type2 && pos1 == pos2) |
| 12248 | return bmap; |
| 12249 | space = isl_basic_map_get_space(bmap); |
| 12250 | c = constraint_order_ge(space, type1, pos1, type2, pos2); |
| 12251 | bmap = isl_basic_map_add_constraint(bmap, c); |
| 12252 | |
| 12253 | return bmap; |
| 12254 | } |
| 12255 | |
| 12256 | /* Add a constraint imposing that the value of the first dimension is |
| 12257 | * greater than or equal to that of the second. |
| 12258 | */ |
| 12259 | __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map, |
| 12260 | enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2) |
| 12261 | { |
| 12262 | isl_constraint *c; |
| 12263 | isl_space *space; |
| 12264 | |
| 12265 | if (type1 == type2 && pos1 == pos2) |
| 12266 | return map; |
| 12267 | space = isl_map_get_space(map); |
| 12268 | c = constraint_order_ge(space, type1, pos1, type2, pos2); |
| 12269 | map = isl_map_add_constraint(map, c); |
| 12270 | |
| 12271 | return map; |
| 12272 | } |
| 12273 | |
| 12274 | /* Add a constraint imposing that the value of the first dimension is |
| 12275 | * less than or equal to that of the second. |
| 12276 | */ |
| 12277 | __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map, |
| 12278 | enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2) |
| 12279 | { |
| 12280 | return isl_map_order_ge(map, type2, pos2, type1, pos1); |
| 12281 | } |
| 12282 | |
| 12283 | /* Construct a basic map where the value of the first dimension is |
| 12284 | * greater than that of the second. |
| 12285 | */ |
| 12286 | static __isl_give isl_basic_map *greator(__isl_take isl_space *space, |
| 12287 | enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2) |
| 12288 | { |
| 12289 | isl_basic_map *bmap = NULL; |
| 12290 | int i; |
| 12291 | |
| 12292 | if (!space) |
| 12293 | return NULL; |
| 12294 | |
| 12295 | if (pos1 >= isl_space_dim(space, type1)) |
| 12296 | isl_die(isl_space_get_ctx(space), isl_error_invalid, |
| 12297 | "index out of bounds", goto error); |
| 12298 | if (pos2 >= isl_space_dim(space, type2)) |
| 12299 | isl_die(isl_space_get_ctx(space), isl_error_invalid, |
| 12300 | "index out of bounds", goto error); |
| 12301 | |
| 12302 | if (type1 == type2 && pos1 == pos2) |
| 12303 | return isl_basic_map_empty(space); |
| 12304 | |
| 12305 | bmap = isl_basic_map_alloc_space(space, 0, 0, 1); |
| 12306 | i = isl_basic_map_alloc_inequality(bmap); |
| 12307 | if (i < 0) |
| 12308 | return isl_basic_map_free(bmap); |
| 12309 | isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap)); |
| 12310 | pos1 += isl_basic_map_offset(bmap, type1); |
| 12311 | pos2 += isl_basic_map_offset(bmap, type2); |
| 12312 | isl_int_set_si(bmap->ineq[i][pos1], 1); |
| 12313 | isl_int_set_si(bmap->ineq[i][pos2], -1); |
| 12314 | isl_int_set_si(bmap->ineq[i][0], -1); |
| 12315 | bmap = isl_basic_map_finalize(bmap); |
| 12316 | |
| 12317 | return bmap; |
| 12318 | error: |
| 12319 | isl_space_free(space); |
| 12320 | isl_basic_map_free(bmap); |
| 12321 | return NULL; |
| 12322 | } |
| 12323 | |
| 12324 | /* Add a constraint imposing that the value of the first dimension is |
| 12325 | * greater than that of the second. |
| 12326 | */ |
| 12327 | __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap, |
| 12328 | enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2) |
| 12329 | { |
| 12330 | isl_basic_map *gt; |
| 12331 | |
| 12332 | gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2); |
| 12333 | |
| 12334 | bmap = isl_basic_map_intersect(bmap, gt); |
| 12335 | |
| 12336 | return bmap; |
| 12337 | } |
| 12338 | |
| 12339 | /* Add a constraint imposing that the value of the first dimension is |
| 12340 | * greater than that of the second. |
| 12341 | */ |
| 12342 | __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map, |
| 12343 | enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2) |
| 12344 | { |
| 12345 | isl_basic_map *bmap; |
| 12346 | |
| 12347 | bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2); |
| 12348 | |
| 12349 | map = isl_map_intersect(map, isl_map_from_basic_map(bmap)); |
| 12350 | |
| 12351 | return map; |
| 12352 | } |
| 12353 | |
| 12354 | /* Add a constraint imposing that the value of the first dimension is |
| 12355 | * smaller than that of the second. |
| 12356 | */ |
| 12357 | __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map, |
| 12358 | enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2) |
| 12359 | { |
| 12360 | return isl_map_order_gt(map, type2, pos2, type1, pos1); |
| 12361 | } |
| 12362 | |
| 12363 | __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap, |
| 12364 | int pos) |
| 12365 | { |
| 12366 | isl_aff *div; |
| 12367 | isl_local_space *ls; |
| 12368 | |
| 12369 | if (!bmap) |
| 12370 | return NULL; |
| 12371 | |
| 12372 | if (!isl_basic_map_divs_known(bmap)) |
| 12373 | isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid, |
| 12374 | "some divs are unknown", return NULL); |
| 12375 | |
| 12376 | ls = isl_basic_map_get_local_space(bmap); |
| 12377 | div = isl_local_space_get_div(ls, pos); |
| 12378 | isl_local_space_free(ls); |
| 12379 | |
| 12380 | return div; |
| 12381 | } |
| 12382 | |
| 12383 | __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset, |
| 12384 | int pos) |
| 12385 | { |
| 12386 | return isl_basic_map_get_div(bset, pos); |
| 12387 | } |
| 12388 | |
| 12389 | /* Plug in "subs" for dimension "type", "pos" of "bset". |
| 12390 | * |
| 12391 | * Let i be the dimension to replace and let "subs" be of the form |
| 12392 | * |
| 12393 | * f/d |
| 12394 | * |
| 12395 | * Any integer division with a non-zero coefficient for i, |
| 12396 | * |
| 12397 | * floor((a i + g)/m) |
| 12398 | * |
| 12399 | * is replaced by |
| 12400 | * |
| 12401 | * floor((a f + d g)/(m d)) |
| 12402 | * |
| 12403 | * Constraints of the form |
| 12404 | * |
| 12405 | * a i + g |
| 12406 | * |
| 12407 | * are replaced by |
| 12408 | * |
| 12409 | * a f + d g |
| 12410 | * |
| 12411 | * We currently require that "subs" is an integral expression. |
| 12412 | * Handling rational expressions may require us to add stride constraints |
| 12413 | * as we do in isl_basic_set_preimage_multi_aff. |
| 12414 | */ |
| 12415 | __isl_give isl_basic_set *isl_basic_set_substitute( |
| 12416 | __isl_take isl_basic_set *bset, |
| 12417 | enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs) |
| 12418 | { |
| 12419 | int i; |
| 12420 | isl_int v; |
| 12421 | isl_ctx *ctx; |
| 12422 | |
| 12423 | if (bset && isl_basic_set_plain_is_empty(bset)) |
| 12424 | return bset; |
| 12425 | |
| 12426 | bset = isl_basic_set_cow(bset); |
| 12427 | if (!bset || !subs) |
| 12428 | goto error; |
| 12429 | |
| 12430 | ctx = isl_basic_set_get_ctx(bset); |
| 12431 | if (!isl_space_is_equal(bset->dim, subs->ls->dim)) |
| 12432 | isl_die(ctx, isl_error_invalid, |
| 12433 | "spaces don't match", goto error); |
| 12434 | if (isl_local_space_dim(subs->ls, isl_dim_div) != 0) |
| 12435 | isl_die(ctx, isl_error_unsupported, |
| 12436 | "cannot handle divs yet", goto error); |
| 12437 | if (!isl_int_is_one(subs->v->el[0])) |
| 12438 | isl_die(ctx, isl_error_invalid, |
| 12439 | "can only substitute integer expressions", goto error); |
| 12440 | |
| 12441 | pos += isl_basic_set_offset(bset, type); |
| 12442 | |
| 12443 | isl_int_init(v); |
| 12444 | |
| 12445 | for (i = 0; i < bset->n_eq; ++i) { |
| 12446 | if (isl_int_is_zero(bset->eq[i][pos])) |
| 12447 | continue; |
| 12448 | isl_int_set(v, bset->eq[i][pos]); |
| 12449 | isl_int_set_si(bset->eq[i][pos], 0); |
| 12450 | isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i], |
| 12451 | v, subs->v->el + 1, subs->v->size - 1); |
| 12452 | } |
| 12453 | |
| 12454 | for (i = 0; i < bset->n_ineq; ++i) { |
| 12455 | if (isl_int_is_zero(bset->ineq[i][pos])) |
| 12456 | continue; |
| 12457 | isl_int_set(v, bset->ineq[i][pos]); |
| 12458 | isl_int_set_si(bset->ineq[i][pos], 0); |
| 12459 | isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i], |
| 12460 | v, subs->v->el + 1, subs->v->size - 1); |
| 12461 | } |
| 12462 | |
| 12463 | for (i = 0; i < bset->n_div; ++i) { |
| 12464 | if (isl_int_is_zero(bset->div[i][1 + pos])) |
| 12465 | continue; |
| 12466 | isl_int_set(v, bset->div[i][1 + pos]); |
| 12467 | isl_int_set_si(bset->div[i][1 + pos], 0); |
| 12468 | isl_seq_combine(bset->div[i] + 1, |
| 12469 | subs->v->el[0], bset->div[i] + 1, |
| 12470 | v, subs->v->el + 1, subs->v->size - 1); |
| 12471 | isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]); |
| 12472 | } |
| 12473 | |
| 12474 | isl_int_clear(v); |
| 12475 | |
| 12476 | bset = isl_basic_set_simplify(bset); |
| 12477 | return isl_basic_set_finalize(bset); |
| 12478 | error: |
| 12479 | isl_basic_set_free(bset); |
| 12480 | return NULL; |
| 12481 | } |
| 12482 | |
| 12483 | /* Plug in "subs" for dimension "type", "pos" of "set". |
| 12484 | */ |
| 12485 | __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set, |
| 12486 | enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs) |
| 12487 | { |
| 12488 | int i; |
| 12489 | |
| 12490 | if (set && isl_set_plain_is_empty(set)) |
| 12491 | return set; |
| 12492 | |
| 12493 | set = isl_set_cow(set); |
| 12494 | if (!set || !subs) |
| 12495 | goto error; |
| 12496 | |
| 12497 | for (i = set->n - 1; i >= 0; --i) { |
| 12498 | set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs); |
| 12499 | if (remove_if_empty(set, i) < 0) |
| 12500 | goto error; |
| 12501 | } |
| 12502 | |
| 12503 | return set; |
| 12504 | error: |
| 12505 | isl_set_free(set); |
| 12506 | return NULL; |
| 12507 | } |
| 12508 | |
| 12509 | /* Check if the range of "ma" is compatible with the domain or range |
| 12510 | * (depending on "type") of "bmap". |
| 12511 | * Return -1 if anything is wrong. |
| 12512 | */ |
| 12513 | static int check_basic_map_compatible_range_multi_aff( |
| 12514 | __isl_keep isl_basic_map *bmap, enum isl_dim_type type, |
| 12515 | __isl_keep isl_multi_aff *ma) |
| 12516 | { |
| 12517 | int m; |
| 12518 | isl_space *ma_space; |
| 12519 | |
| 12520 | ma_space = isl_multi_aff_get_space(ma); |
| 12521 | |
| 12522 | m = isl_space_match(bmap->dim, isl_dim_param, ma_space, isl_dim_param); |
| 12523 | if (m < 0) |
| 12524 | goto error; |
| 12525 | if (!m) |
| 12526 | isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid, |
| 12527 | "parameters don't match", goto error); |
| 12528 | m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out); |
| 12529 | if (m < 0) |
| 12530 | goto error; |
| 12531 | if (!m) |
| 12532 | isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid, |
| 12533 | "spaces don't match", goto error); |
| 12534 | |
| 12535 | isl_space_free(ma_space); |
| 12536 | return m; |
| 12537 | error: |
| 12538 | isl_space_free(ma_space); |
| 12539 | return -1; |
| 12540 | } |
| 12541 | |
| 12542 | /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before" |
| 12543 | * coefficients before the transformed range of dimensions, |
| 12544 | * the "n_after" coefficients after the transformed range of dimensions |
| 12545 | * and the coefficients of the other divs in "bmap". |
| 12546 | */ |
| 12547 | static int set_ma_divs(__isl_keep isl_basic_map *bmap, |
| 12548 | __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div) |
| 12549 | { |
| 12550 | int i; |
| 12551 | int n_param; |
| 12552 | int n_set; |
| 12553 | isl_local_space *ls; |
| 12554 | |
| 12555 | if (n_div == 0) |
| 12556 | return 0; |
| 12557 | |
| 12558 | ls = isl_aff_get_domain_local_space(ma->p[0]); |
| 12559 | if (!ls) |
| 12560 | return -1; |
| 12561 | |
| 12562 | n_param = isl_local_space_dim(ls, isl_dim_param); |
| 12563 | n_set = isl_local_space_dim(ls, isl_dim_set); |
| 12564 | for (i = 0; i < n_div; ++i) { |
| 12565 | int o_bmap = 0, o_ls = 0; |
| 12566 | |
| 12567 | isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param); |
| 12568 | o_bmap += 1 + 1 + n_param; |
| 12569 | o_ls += 1 + 1 + n_param; |
| 12570 | isl_seq_clr(bmap->div[i] + o_bmap, n_before); |
| 12571 | o_bmap += n_before; |
| 12572 | isl_seq_cpy(bmap->div[i] + o_bmap, |
| 12573 | ls->div->row[i] + o_ls, n_set); |
| 12574 | o_bmap += n_set; |
| 12575 | o_ls += n_set; |
| 12576 | isl_seq_clr(bmap->div[i] + o_bmap, n_after); |
| 12577 | o_bmap += n_after; |
| 12578 | isl_seq_cpy(bmap->div[i] + o_bmap, |
| 12579 | ls->div->row[i] + o_ls, n_div); |
| 12580 | o_bmap += n_div; |
| 12581 | o_ls += n_div; |
| 12582 | isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div); |
| 12583 | if (isl_basic_set_add_div_constraints(bmap, i) < 0) |
| 12584 | goto error; |
| 12585 | } |
| 12586 | |
| 12587 | isl_local_space_free(ls); |
| 12588 | return 0; |
| 12589 | error: |
| 12590 | isl_local_space_free(ls); |
| 12591 | return -1; |
| 12592 | } |
| 12593 | |
| 12594 | /* How many stride constraints does "ma" enforce? |
| 12595 | * That is, how many of the affine expressions have a denominator |
| 12596 | * different from one? |
| 12597 | */ |
| 12598 | static int multi_aff_strides(__isl_keep isl_multi_aff *ma) |
| 12599 | { |
| 12600 | int i; |
| 12601 | int strides = 0; |
| 12602 | |
| 12603 | for (i = 0; i < ma->n; ++i) |
| 12604 | if (!isl_int_is_one(ma->p[i]->v->el[0])) |
| 12605 | strides++; |
| 12606 | |
| 12607 | return strides; |
| 12608 | } |
| 12609 | |
| 12610 | /* For each affine expression in ma of the form |
| 12611 | * |
| 12612 | * x_i = (f_i y + h_i)/m_i |
| 12613 | * |
| 12614 | * with m_i different from one, add a constraint to "bmap" |
| 12615 | * of the form |
| 12616 | * |
| 12617 | * f_i y + h_i = m_i alpha_i |
| 12618 | * |
| 12619 | * with alpha_i an additional existentially quantified variable. |
Tobias Grosser | a67ac97 | 2016-02-26 11:35:12 +0000 | [diff] [blame] | 12620 | * |
| 12621 | * The input variables of "ma" correspond to a subset of the variables |
| 12622 | * of "bmap". There are "n_before" variables in "bmap" before this |
| 12623 | * subset and "n_after" variables after this subset. |
| 12624 | * The integer divisions of the affine expressions in "ma" are assumed |
| 12625 | * to have been aligned. There are "n_div_ma" of them and |
| 12626 | * they appear first in "bmap", straight after the "n_after" variables. |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 12627 | */ |
| 12628 | static __isl_give isl_basic_map *add_ma_strides( |
| 12629 | __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma, |
Tobias Grosser | a67ac97 | 2016-02-26 11:35:12 +0000 | [diff] [blame] | 12630 | int n_before, int n_after, int n_div_ma) |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 12631 | { |
| 12632 | int i, k; |
| 12633 | int div; |
| 12634 | int total; |
| 12635 | int n_param; |
| 12636 | int n_in; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 12637 | |
| 12638 | total = isl_basic_map_total_dim(bmap); |
| 12639 | n_param = isl_multi_aff_dim(ma, isl_dim_param); |
| 12640 | n_in = isl_multi_aff_dim(ma, isl_dim_in); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 12641 | for (i = 0; i < ma->n; ++i) { |
| 12642 | int o_bmap = 0, o_ma = 1; |
| 12643 | |
| 12644 | if (isl_int_is_one(ma->p[i]->v->el[0])) |
| 12645 | continue; |
| 12646 | div = isl_basic_map_alloc_div(bmap); |
| 12647 | k = isl_basic_map_alloc_equality(bmap); |
| 12648 | if (div < 0 || k < 0) |
| 12649 | goto error; |
| 12650 | isl_int_set_si(bmap->div[div][0], 0); |
| 12651 | isl_seq_cpy(bmap->eq[k] + o_bmap, |
| 12652 | ma->p[i]->v->el + o_ma, 1 + n_param); |
| 12653 | o_bmap += 1 + n_param; |
| 12654 | o_ma += 1 + n_param; |
| 12655 | isl_seq_clr(bmap->eq[k] + o_bmap, n_before); |
| 12656 | o_bmap += n_before; |
| 12657 | isl_seq_cpy(bmap->eq[k] + o_bmap, |
| 12658 | ma->p[i]->v->el + o_ma, n_in); |
| 12659 | o_bmap += n_in; |
| 12660 | o_ma += n_in; |
| 12661 | isl_seq_clr(bmap->eq[k] + o_bmap, n_after); |
| 12662 | o_bmap += n_after; |
| 12663 | isl_seq_cpy(bmap->eq[k] + o_bmap, |
Tobias Grosser | a67ac97 | 2016-02-26 11:35:12 +0000 | [diff] [blame] | 12664 | ma->p[i]->v->el + o_ma, n_div_ma); |
| 12665 | o_bmap += n_div_ma; |
| 12666 | o_ma += n_div_ma; |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 12667 | isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap); |
| 12668 | isl_int_neg(bmap->eq[k][1 + total], ma->p[i]->v->el[0]); |
| 12669 | total++; |
| 12670 | } |
| 12671 | |
| 12672 | return bmap; |
| 12673 | error: |
| 12674 | isl_basic_map_free(bmap); |
| 12675 | return NULL; |
| 12676 | } |
| 12677 | |
| 12678 | /* Replace the domain or range space (depending on "type) of "space" by "set". |
| 12679 | */ |
| 12680 | static __isl_give isl_space *isl_space_set(__isl_take isl_space *space, |
| 12681 | enum isl_dim_type type, __isl_take isl_space *set) |
| 12682 | { |
| 12683 | if (type == isl_dim_in) { |
| 12684 | space = isl_space_range(space); |
| 12685 | space = isl_space_map_from_domain_and_range(set, space); |
| 12686 | } else { |
| 12687 | space = isl_space_domain(space); |
| 12688 | space = isl_space_map_from_domain_and_range(space, set); |
| 12689 | } |
| 12690 | |
| 12691 | return space; |
| 12692 | } |
| 12693 | |
| 12694 | /* Compute the preimage of the domain or range (depending on "type") |
| 12695 | * of "bmap" under the function represented by "ma". |
| 12696 | * In other words, plug in "ma" in the domain or range of "bmap". |
| 12697 | * The result is a basic map that lives in the same space as "bmap" |
| 12698 | * except that the domain or range has been replaced by |
| 12699 | * the domain space of "ma". |
| 12700 | * |
| 12701 | * If bmap is represented by |
| 12702 | * |
| 12703 | * A(p) + S u + B x + T v + C(divs) >= 0, |
| 12704 | * |
| 12705 | * where u and x are input and output dimensions if type == isl_dim_out |
| 12706 | * while x and v are input and output dimensions if type == isl_dim_in, |
| 12707 | * and ma is represented by |
| 12708 | * |
| 12709 | * x = D(p) + F(y) + G(divs') |
| 12710 | * |
| 12711 | * then the result is |
| 12712 | * |
| 12713 | * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0 |
| 12714 | * |
| 12715 | * The divs in the input set are similarly adjusted. |
| 12716 | * In particular |
| 12717 | * |
| 12718 | * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i) |
| 12719 | * |
| 12720 | * becomes |
| 12721 | * |
| 12722 | * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v + |
| 12723 | * B_i G(divs') + c_i(divs))/n_i) |
| 12724 | * |
| 12725 | * If bmap is not a rational map and if F(y) involves any denominators |
| 12726 | * |
| 12727 | * x_i = (f_i y + h_i)/m_i |
| 12728 | * |
| 12729 | * then additional constraints are added to ensure that we only |
| 12730 | * map back integer points. That is we enforce |
| 12731 | * |
| 12732 | * f_i y + h_i = m_i alpha_i |
| 12733 | * |
| 12734 | * with alpha_i an additional existentially quantified variable. |
| 12735 | * |
| 12736 | * We first copy over the divs from "ma". |
| 12737 | * Then we add the modified constraints and divs from "bmap". |
| 12738 | * Finally, we add the stride constraints, if needed. |
| 12739 | */ |
| 12740 | __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff( |
| 12741 | __isl_take isl_basic_map *bmap, enum isl_dim_type type, |
| 12742 | __isl_take isl_multi_aff *ma) |
| 12743 | { |
| 12744 | int i, k; |
| 12745 | isl_space *space; |
| 12746 | isl_basic_map *res = NULL; |
| 12747 | int n_before, n_after, n_div_bmap, n_div_ma; |
| 12748 | isl_int f, c1, c2, g; |
| 12749 | int rational, strides; |
| 12750 | |
| 12751 | isl_int_init(f); |
| 12752 | isl_int_init(c1); |
| 12753 | isl_int_init(c2); |
| 12754 | isl_int_init(g); |
| 12755 | |
| 12756 | ma = isl_multi_aff_align_divs(ma); |
| 12757 | if (!bmap || !ma) |
| 12758 | goto error; |
| 12759 | if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0) |
| 12760 | goto error; |
| 12761 | |
| 12762 | if (type == isl_dim_in) { |
| 12763 | n_before = 0; |
| 12764 | n_after = isl_basic_map_dim(bmap, isl_dim_out); |
| 12765 | } else { |
| 12766 | n_before = isl_basic_map_dim(bmap, isl_dim_in); |
| 12767 | n_after = 0; |
| 12768 | } |
| 12769 | n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div); |
| 12770 | n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0; |
| 12771 | |
| 12772 | space = isl_multi_aff_get_domain_space(ma); |
| 12773 | space = isl_space_set(isl_basic_map_get_space(bmap), type, space); |
| 12774 | rational = isl_basic_map_is_rational(bmap); |
| 12775 | strides = rational ? 0 : multi_aff_strides(ma); |
| 12776 | res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides, |
| 12777 | bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma); |
| 12778 | if (rational) |
| 12779 | res = isl_basic_map_set_rational(res); |
| 12780 | |
| 12781 | for (i = 0; i < n_div_ma + n_div_bmap; ++i) |
| 12782 | if (isl_basic_map_alloc_div(res) < 0) |
| 12783 | goto error; |
| 12784 | |
| 12785 | if (set_ma_divs(res, ma, n_before, n_after, n_div_ma) < 0) |
| 12786 | goto error; |
| 12787 | |
| 12788 | for (i = 0; i < bmap->n_eq; ++i) { |
| 12789 | k = isl_basic_map_alloc_equality(res); |
| 12790 | if (k < 0) |
| 12791 | goto error; |
| 12792 | isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before, |
| 12793 | n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0); |
| 12794 | } |
| 12795 | |
| 12796 | for (i = 0; i < bmap->n_ineq; ++i) { |
| 12797 | k = isl_basic_map_alloc_inequality(res); |
| 12798 | if (k < 0) |
| 12799 | goto error; |
| 12800 | isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before, |
| 12801 | n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0); |
| 12802 | } |
| 12803 | |
| 12804 | for (i = 0; i < bmap->n_div; ++i) { |
| 12805 | if (isl_int_is_zero(bmap->div[i][0])) { |
| 12806 | isl_int_set_si(res->div[n_div_ma + i][0], 0); |
| 12807 | continue; |
| 12808 | } |
| 12809 | isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma, |
| 12810 | n_before, n_after, n_div_ma, n_div_bmap, |
| 12811 | f, c1, c2, g, 1); |
| 12812 | } |
| 12813 | |
| 12814 | if (strides) |
Tobias Grosser | a67ac97 | 2016-02-26 11:35:12 +0000 | [diff] [blame] | 12815 | res = add_ma_strides(res, ma, n_before, n_after, n_div_ma); |
Tobias Grosser | 52a2523 | 2015-02-04 20:55:43 +0000 | [diff] [blame] | 12816 | |
| 12817 | isl_int_clear(f); |
| 12818 | isl_int_clear(c1); |
| 12819 | isl_int_clear(c2); |
| 12820 | isl_int_clear(g); |
| 12821 | isl_basic_map_free(bmap); |
| 12822 | isl_multi_aff_free(ma); |
| 12823 | res = isl_basic_set_simplify(res); |
| 12824 | return isl_basic_map_finalize(res); |
| 12825 | error: |
| 12826 | isl_int_clear(f); |
| 12827 | isl_int_clear(c1); |
| 12828 | isl_int_clear(c2); |
| 12829 | isl_int_clear(g); |
| 12830 | isl_basic_map_free(bmap); |
| 12831 | isl_multi_aff_free(ma); |
| 12832 | isl_basic_map_free(res); |
| 12833 | return NULL; |
| 12834 | } |
| 12835 | |
| 12836 | /* Compute the preimage of "bset" under the function represented by "ma". |
| 12837 | * In other words, plug in "ma" in "bset". The result is a basic set |
| 12838 | * that lives in the domain space of "ma". |
| 12839 | */ |
| 12840 | __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff( |
| 12841 | __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma) |
| 12842 | { |
| 12843 | return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma); |
| 12844 | } |
| 12845 | |
| 12846 | /* Compute the preimage of the domain of "bmap" under the function |
| 12847 | * represented by "ma". |
| 12848 | * In other words, plug in "ma" in the domain of "bmap". |
| 12849 | * The result is a basic map that lives in the same space as "bmap" |
| 12850 | * except that the domain has been replaced by the domain space of "ma". |
| 12851 | */ |
| 12852 | __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff( |
| 12853 | __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma) |
| 12854 | { |
| 12855 | return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma); |
| 12856 | } |
| 12857 | |
| 12858 | /* Compute the preimage of the range of "bmap" under the function |
| 12859 | * represented by "ma". |
| 12860 | * In other words, plug in "ma" in the range of "bmap". |
| 12861 | * The result is a basic map that lives in the same space as "bmap" |
| 12862 | * except that the range has been replaced by the domain space of "ma". |
| 12863 | */ |
| 12864 | __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff( |
| 12865 | __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma) |
| 12866 | { |
| 12867 | return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma); |
| 12868 | } |
| 12869 | |
| 12870 | /* Check if the range of "ma" is compatible with the domain or range |
| 12871 | * (depending on "type") of "map". |
| 12872 | * Return -1 if anything is wrong. |
| 12873 | */ |
| 12874 | static int check_map_compatible_range_multi_aff( |
| 12875 | __isl_keep isl_map *map, enum isl_dim_type type, |
| 12876 | __isl_keep isl_multi_aff *ma) |
| 12877 | { |
| 12878 | int m; |
| 12879 | isl_space *ma_space; |
| 12880 | |
| 12881 | ma_space = isl_multi_aff_get_space(ma); |
| 12882 | m = isl_space_tuple_is_equal(map->dim, type, ma_space, isl_dim_out); |
| 12883 | isl_space_free(ma_space); |
| 12884 | if (m >= 0 && !m) |
| 12885 | isl_die(isl_map_get_ctx(map), isl_error_invalid, |
| 12886 | "spaces don't match", return -1); |
| 12887 | return m; |
| 12888 | } |
| 12889 | |
| 12890 | /* Compute the preimage of the domain or range (depending on "type") |
| 12891 | * of "map" under the function represented by "ma". |
| 12892 | * In other words, plug in "ma" in the domain or range of "map". |
| 12893 | * The result is a map that lives in the same space as "map" |
| 12894 | * except that the domain or range has been replaced by |
| 12895 | * the domain space of "ma". |
| 12896 | * |
| 12897 | * The parameters are assumed to have been aligned. |
| 12898 | */ |
| 12899 | static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map, |
| 12900 | enum isl_dim_type type, __isl_take isl_multi_aff *ma) |
| 12901 | { |
| 12902 | int i; |
| 12903 | isl_space *space; |
| 12904 | |
| 12905 | map = isl_map_cow(map); |
| 12906 | ma = isl_multi_aff_align_divs(ma); |
| 12907 | if (!map || !ma) |
| 12908 | goto error; |
| 12909 | if (check_map_compatible_range_multi_aff(map, type, ma) < 0) |
| 12910 | goto error; |
| 12911 | |
| 12912 | for (i = 0; i < map->n; ++i) { |
| 12913 | map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type, |
| 12914 | isl_multi_aff_copy(ma)); |
| 12915 | if (!map->p[i]) |
| 12916 | goto error; |
| 12917 | } |
| 12918 | |
| 12919 | space = isl_multi_aff_get_domain_space(ma); |
| 12920 | space = isl_space_set(isl_map_get_space(map), type, space); |
| 12921 | |
| 12922 | isl_space_free(map->dim); |
| 12923 | map->dim = space; |
| 12924 | if (!map->dim) |
| 12925 | goto error; |
| 12926 | |
| 12927 | isl_multi_aff_free(ma); |
| 12928 | if (map->n > 1) |
| 12929 | ISL_F_CLR(map, ISL_MAP_DISJOINT); |
| 12930 | ISL_F_CLR(map, ISL_SET_NORMALIZED); |
| 12931 | return map; |
| 12932 | error: |
| 12933 | isl_multi_aff_free(ma); |
| 12934 | isl_map_free(map); |
| 12935 | return NULL; |
| 12936 | } |
| 12937 | |
| 12938 | /* Compute the preimage of the domain or range (depending on "type") |
| 12939 | * of "map" under the function represented by "ma". |
| 12940 | * In other words, plug in "ma" in the domain or range of "map". |
| 12941 | * The result is a map that lives in the same space as "map" |
| 12942 | * except that the domain or range has been replaced by |
| 12943 | * the domain space of "ma". |
| 12944 | */ |
| 12945 | __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map, |
| 12946 | enum isl_dim_type type, __isl_take isl_multi_aff *ma) |
| 12947 | { |
| 12948 | if (!map || !ma) |
| 12949 | goto error; |
| 12950 | |
| 12951 | if (isl_space_match(map->dim, isl_dim_param, ma->space, isl_dim_param)) |
| 12952 | return map_preimage_multi_aff(map, type, ma); |
| 12953 | |
| 12954 | if (!isl_space_has_named_params(map->dim) || |
| 12955 | !isl_space_has_named_params(ma->space)) |
| 12956 | isl_die(map->ctx, isl_error_invalid, |
| 12957 | "unaligned unnamed parameters", goto error); |
| 12958 | map = isl_map_align_params(map, isl_multi_aff_get_space(ma)); |
| 12959 | ma = isl_multi_aff_align_params(ma, isl_map_get_space(map)); |
| 12960 | |
| 12961 | return map_preimage_multi_aff(map, type, ma); |
| 12962 | error: |
| 12963 | isl_multi_aff_free(ma); |
| 12964 | return isl_map_free(map); |
| 12965 | } |
| 12966 | |
| 12967 | /* Compute the preimage of "set" under the function represented by "ma". |
| 12968 | * In other words, plug in "ma" in "set". The result is a set |
| 12969 | * that lives in the domain space of "ma". |
| 12970 | */ |
| 12971 | __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set, |
| 12972 | __isl_take isl_multi_aff *ma) |
| 12973 | { |
| 12974 | return isl_map_preimage_multi_aff(set, isl_dim_set, ma); |
| 12975 | } |
| 12976 | |
| 12977 | /* Compute the preimage of the domain of "map" under the function |
| 12978 | * represented by "ma". |
| 12979 | * In other words, plug in "ma" in the domain of "map". |
| 12980 | * The result is a map that lives in the same space as "map" |
| 12981 | * except that the domain has been replaced by the domain space of "ma". |
| 12982 | */ |
| 12983 | __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map, |
| 12984 | __isl_take isl_multi_aff *ma) |
| 12985 | { |
| 12986 | return isl_map_preimage_multi_aff(map, isl_dim_in, ma); |
| 12987 | } |
| 12988 | |
| 12989 | /* Compute the preimage of the range of "map" under the function |
| 12990 | * represented by "ma". |
| 12991 | * In other words, plug in "ma" in the range of "map". |
| 12992 | * The result is a map that lives in the same space as "map" |
| 12993 | * except that the range has been replaced by the domain space of "ma". |
| 12994 | */ |
| 12995 | __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map, |
| 12996 | __isl_take isl_multi_aff *ma) |
| 12997 | { |
| 12998 | return isl_map_preimage_multi_aff(map, isl_dim_out, ma); |
| 12999 | } |
| 13000 | |
| 13001 | /* Compute the preimage of "map" under the function represented by "pma". |
| 13002 | * In other words, plug in "pma" in the domain or range of "map". |
| 13003 | * The result is a map that lives in the same space as "map", |
| 13004 | * except that the space of type "type" has been replaced by |
| 13005 | * the domain space of "pma". |
| 13006 | * |
| 13007 | * The parameters of "map" and "pma" are assumed to have been aligned. |
| 13008 | */ |
| 13009 | static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned( |
| 13010 | __isl_take isl_map *map, enum isl_dim_type type, |
| 13011 | __isl_take isl_pw_multi_aff *pma) |
| 13012 | { |
| 13013 | int i; |
| 13014 | isl_map *res; |
| 13015 | |
| 13016 | if (!pma) |
| 13017 | goto error; |
| 13018 | |
| 13019 | if (pma->n == 0) { |
| 13020 | isl_pw_multi_aff_free(pma); |
| 13021 | res = isl_map_empty(isl_map_get_space(map)); |
| 13022 | isl_map_free(map); |
| 13023 | return res; |
| 13024 | } |
| 13025 | |
| 13026 | res = isl_map_preimage_multi_aff(isl_map_copy(map), type, |
| 13027 | isl_multi_aff_copy(pma->p[0].maff)); |
| 13028 | if (type == isl_dim_in) |
| 13029 | res = isl_map_intersect_domain(res, |
| 13030 | isl_map_copy(pma->p[0].set)); |
| 13031 | else |
| 13032 | res = isl_map_intersect_range(res, |
| 13033 | isl_map_copy(pma->p[0].set)); |
| 13034 | |
| 13035 | for (i = 1; i < pma->n; ++i) { |
| 13036 | isl_map *res_i; |
| 13037 | |
| 13038 | res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type, |
| 13039 | isl_multi_aff_copy(pma->p[i].maff)); |
| 13040 | if (type == isl_dim_in) |
| 13041 | res_i = isl_map_intersect_domain(res_i, |
| 13042 | isl_map_copy(pma->p[i].set)); |
| 13043 | else |
| 13044 | res_i = isl_map_intersect_range(res_i, |
| 13045 | isl_map_copy(pma->p[i].set)); |
| 13046 | res = isl_map_union(res, res_i); |
| 13047 | } |
| 13048 | |
| 13049 | isl_pw_multi_aff_free(pma); |
| 13050 | isl_map_free(map); |
| 13051 | return res; |
| 13052 | error: |
| 13053 | isl_pw_multi_aff_free(pma); |
| 13054 | isl_map_free(map); |
| 13055 | return NULL; |
| 13056 | } |
| 13057 | |
| 13058 | /* Compute the preimage of "map" under the function represented by "pma". |
| 13059 | * In other words, plug in "pma" in the domain or range of "map". |
| 13060 | * The result is a map that lives in the same space as "map", |
| 13061 | * except that the space of type "type" has been replaced by |
| 13062 | * the domain space of "pma". |
| 13063 | */ |
| 13064 | __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map, |
| 13065 | enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma) |
| 13066 | { |
| 13067 | if (!map || !pma) |
| 13068 | goto error; |
| 13069 | |
| 13070 | if (isl_space_match(map->dim, isl_dim_param, pma->dim, isl_dim_param)) |
| 13071 | return isl_map_preimage_pw_multi_aff_aligned(map, type, pma); |
| 13072 | |
| 13073 | if (!isl_space_has_named_params(map->dim) || |
| 13074 | !isl_space_has_named_params(pma->dim)) |
| 13075 | isl_die(map->ctx, isl_error_invalid, |
| 13076 | "unaligned unnamed parameters", goto error); |
| 13077 | map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma)); |
| 13078 | pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map)); |
| 13079 | |
| 13080 | return isl_map_preimage_pw_multi_aff_aligned(map, type, pma); |
| 13081 | error: |
| 13082 | isl_pw_multi_aff_free(pma); |
| 13083 | return isl_map_free(map); |
| 13084 | } |
| 13085 | |
| 13086 | /* Compute the preimage of "set" under the function represented by "pma". |
| 13087 | * In other words, plug in "pma" in "set". The result is a set |
| 13088 | * that lives in the domain space of "pma". |
| 13089 | */ |
| 13090 | __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set, |
| 13091 | __isl_take isl_pw_multi_aff *pma) |
| 13092 | { |
| 13093 | return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma); |
| 13094 | } |
| 13095 | |
| 13096 | /* Compute the preimage of the domain of "map" under the function |
| 13097 | * represented by "pma". |
| 13098 | * In other words, plug in "pma" in the domain of "map". |
| 13099 | * The result is a map that lives in the same space as "map", |
| 13100 | * except that domain space has been replaced by the domain space of "pma". |
| 13101 | */ |
| 13102 | __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff( |
| 13103 | __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma) |
| 13104 | { |
| 13105 | return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma); |
| 13106 | } |
| 13107 | |
| 13108 | /* Compute the preimage of the range of "map" under the function |
| 13109 | * represented by "pma". |
| 13110 | * In other words, plug in "pma" in the range of "map". |
| 13111 | * The result is a map that lives in the same space as "map", |
| 13112 | * except that range space has been replaced by the domain space of "pma". |
| 13113 | */ |
| 13114 | __isl_give isl_map *isl_map_preimage_range_pw_multi_aff( |
| 13115 | __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma) |
| 13116 | { |
| 13117 | return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma); |
| 13118 | } |
| 13119 | |
| 13120 | /* Compute the preimage of "map" under the function represented by "mpa". |
| 13121 | * In other words, plug in "mpa" in the domain or range of "map". |
| 13122 | * The result is a map that lives in the same space as "map", |
| 13123 | * except that the space of type "type" has been replaced by |
| 13124 | * the domain space of "mpa". |
| 13125 | * |
| 13126 | * If the map does not involve any constraints that refer to the |
| 13127 | * dimensions of the substituted space, then the only possible |
| 13128 | * effect of "mpa" on the map is to map the space to a different space. |
| 13129 | * We create a separate isl_multi_aff to effectuate this change |
| 13130 | * in order to avoid spurious splitting of the map along the pieces |
| 13131 | * of "mpa". |
| 13132 | */ |
| 13133 | __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map, |
| 13134 | enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa) |
| 13135 | { |
| 13136 | int n; |
| 13137 | isl_pw_multi_aff *pma; |
| 13138 | |
| 13139 | if (!map || !mpa) |
| 13140 | goto error; |
| 13141 | |
| 13142 | n = isl_map_dim(map, type); |
| 13143 | if (!isl_map_involves_dims(map, type, 0, n)) { |
| 13144 | isl_space *space; |
| 13145 | isl_multi_aff *ma; |
| 13146 | |
| 13147 | space = isl_multi_pw_aff_get_space(mpa); |
| 13148 | isl_multi_pw_aff_free(mpa); |
| 13149 | ma = isl_multi_aff_zero(space); |
| 13150 | return isl_map_preimage_multi_aff(map, type, ma); |
| 13151 | } |
| 13152 | |
| 13153 | pma = isl_pw_multi_aff_from_multi_pw_aff(mpa); |
| 13154 | return isl_map_preimage_pw_multi_aff(map, type, pma); |
| 13155 | error: |
| 13156 | isl_map_free(map); |
| 13157 | isl_multi_pw_aff_free(mpa); |
| 13158 | return NULL; |
| 13159 | } |
| 13160 | |
| 13161 | /* Compute the preimage of "map" under the function represented by "mpa". |
| 13162 | * In other words, plug in "mpa" in the domain "map". |
| 13163 | * The result is a map that lives in the same space as "map", |
| 13164 | * except that domain space has been replaced by the domain space of "mpa". |
| 13165 | */ |
| 13166 | __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff( |
| 13167 | __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa) |
| 13168 | { |
| 13169 | return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa); |
| 13170 | } |
| 13171 | |
| 13172 | /* Compute the preimage of "set" by the function represented by "mpa". |
| 13173 | * In other words, plug in "mpa" in "set". |
| 13174 | */ |
| 13175 | __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set, |
| 13176 | __isl_take isl_multi_pw_aff *mpa) |
| 13177 | { |
| 13178 | return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa); |
| 13179 | } |
Michael Kruse | 1b8eb41 | 2016-12-06 14:37:39 +0000 | [diff] [blame] | 13180 | |
| 13181 | /* Is the point "inner" internal to inequality constraint "ineq" |
| 13182 | * of "bset"? |
| 13183 | * The point is considered to be internal to the inequality constraint, |
| 13184 | * if it strictly lies on the positive side of the inequality constraint, |
| 13185 | * or if it lies on the constraint and the constraint is lexico-positive. |
| 13186 | */ |
| 13187 | static isl_bool is_internal(__isl_keep isl_vec *inner, |
| 13188 | __isl_keep isl_basic_set *bset, int ineq) |
| 13189 | { |
| 13190 | isl_ctx *ctx; |
| 13191 | int pos; |
| 13192 | unsigned total; |
| 13193 | |
| 13194 | if (!inner || !bset) |
| 13195 | return isl_bool_error; |
| 13196 | |
| 13197 | ctx = isl_basic_set_get_ctx(bset); |
| 13198 | isl_seq_inner_product(inner->el, bset->ineq[ineq], inner->size, |
| 13199 | &ctx->normalize_gcd); |
| 13200 | if (!isl_int_is_zero(ctx->normalize_gcd)) |
| 13201 | return isl_int_is_nonneg(ctx->normalize_gcd); |
| 13202 | |
| 13203 | total = isl_basic_set_dim(bset, isl_dim_all); |
| 13204 | pos = isl_seq_first_non_zero(bset->ineq[ineq] + 1, total); |
| 13205 | return isl_int_is_pos(bset->ineq[ineq][1 + pos]); |
| 13206 | } |
| 13207 | |
| 13208 | /* Tighten the inequality constraints of "bset" that are outward with respect |
| 13209 | * to the point "vec". |
| 13210 | * That is, tighten the constraints that are not satisfied by "vec". |
| 13211 | * |
| 13212 | * "vec" is a point internal to some superset S of "bset" that is used |
| 13213 | * to make the subsets of S disjoint, by tightening one half of the constraints |
| 13214 | * that separate two subsets. In particular, the constraints of S |
| 13215 | * are all satisfied by "vec" and should not be tightened. |
| 13216 | * Of the internal constraints, those that have "vec" on the outside |
| 13217 | * are tightened. The shared facet is included in the adjacent subset |
| 13218 | * with the opposite constraint. |
| 13219 | * For constraints that saturate "vec", this criterion cannot be used |
| 13220 | * to determine which of the two sides should be tightened. |
| 13221 | * Instead, the sign of the first non-zero coefficient is used |
| 13222 | * to make this choice. Note that this second criterion is never used |
| 13223 | * on the constraints of S since "vec" is interior to "S". |
| 13224 | */ |
| 13225 | __isl_give isl_basic_set *isl_basic_set_tighten_outward( |
| 13226 | __isl_take isl_basic_set *bset, __isl_keep isl_vec *vec) |
| 13227 | { |
| 13228 | int j; |
| 13229 | |
| 13230 | bset = isl_basic_set_cow(bset); |
| 13231 | if (!bset) |
| 13232 | return NULL; |
| 13233 | for (j = 0; j < bset->n_ineq; ++j) { |
| 13234 | isl_bool internal; |
| 13235 | |
| 13236 | internal = is_internal(vec, bset, j); |
| 13237 | if (internal < 0) |
| 13238 | return isl_basic_set_free(bset); |
| 13239 | if (internal) |
| 13240 | continue; |
| 13241 | isl_int_sub_ui(bset->ineq[j][0], bset->ineq[j][0], 1); |
| 13242 | } |
| 13243 | |
| 13244 | return bset; |
| 13245 | } |